From patchwork Thu Jan 26 20:57:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rik van Riel X-Patchwork-Id: 48893 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp483934wrn; Thu, 26 Jan 2023 13:02:49 -0800 (PST) X-Google-Smtp-Source: AMrXdXufBxuyd1lEy5g19XaDH3T8Z9Z9SU6AMZp5gIWG1DzwnKih7+4jy9MbIP6wN8muJvFglNAo X-Received: by 2002:a17:907:8a07:b0:7c1:5ee1:4c57 with SMTP id sc7-20020a1709078a0700b007c15ee14c57mr45560131ejc.8.1674766968782; Thu, 26 Jan 2023 13:02:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674766968; cv=none; d=google.com; s=arc-20160816; b=AUDwDrHvD3LaDhW2moyk7lJp9RbifiJ+Z7Q8Xefi5InL7glGMIUyiADXpuQ0Oc1XNw SaO7SdYG/b3jB5iGuLctcSw4SHld6Zn0y36vnInSgQNbn3RIv6lrtahk95vw7PDSR3S4 zQVKDVHkLIC4vpUbRGRWWPk3/Q+rLG7GOEKBJcu6+p1rrMLyDRKSR0D7GpKpiG3UbC8W K6diCPicYBLwuU3yEhkALvde7D796A10yVl2bC2mdh6F9o+uJn9cKVBdp8WgkW69L9nm 2ixw/adP7+VHOM6OmNtwr9yPdGo0JWju5CMwAsOcd6gyrE/dClDtnP1/18Wi+hJ9Hg75 wyOw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=jGvZZJZuQJdxoBz3CCm4G0l/dvMXn1lHJuCxsosHcXY=; b=iHUWEvZGYJmhT/sf1WGuK+8K3yodvcpRtByS7hs1IC5jfGfO3ZO043ju8i2jAAJ6MI d+0UxHyuUCXdLOhWwTMkyWdPrwwEhlLZBVqQ7n/QflUWp/1QPWVodA8W5Hi1s0gBU47f DIZUfHGGyOLI4hC1sRabN48jus2IUmosXwuqohbqlvGsI4aa5Cbz3oP628TOA3d1P4TY cg+zTtl+6+Au4jhsieKwpQpR9+U/hUIMWHlsey+PSl0uLEMv+QOO0ThVchynApM4ztwA 6vIkrHzPW9YHBJpH0BiUiqibp/7N5MbfJztXkLUuCGCddazNkHpTLr52Sze9+A86p8ad CkSg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id fp13-20020a1709069e0d00b00878560b5f52si2256838ejc.335.2023.01.26.13.02.24; Thu, 26 Jan 2023 13:02:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231586AbjAZU5x (ORCPT + 99 others); Thu, 26 Jan 2023 15:57:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232548AbjAZU5s (ORCPT ); Thu, 26 Jan 2023 15:57:48 -0500 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9275A2D6B; Thu, 26 Jan 2023 12:57:47 -0800 (PST) Received: from imladris.home.surriel.com ([10.0.13.28] helo=imladris.surriel.com) by shelob.surriel.com with esmtpsa (TLS1.2) tls TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1pL9JN-0001D5-1q; Thu, 26 Jan 2023 15:57:33 -0500 From: Rik van Riel To: linux-kernel@vger.kernel.org, viro@zeniv.linux.org.uk, kernel-team@meta.com, linux-fsdevel@vger.kernel.org, gscrivan@redhat.com Cc: Rik van Riel , Chris Mason Subject: [PATCH 1/2] ipc,namespace: make ipc namespace allocation wait for pending free Date: Thu, 26 Jan 2023 15:57:20 -0500 Message-Id: <20230126205721.582612-2-riel@surriel.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20230126205721.582612-1-riel@surriel.com> References: <20230126205721.582612-1-riel@surriel.com> MIME-Version: 1.0 Sender: riel@shelob.surriel.com X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756120448755885057?= X-GMAIL-MSGID: =?utf-8?q?1756120448755885057?= Currently the ipc namespace allocation will fail when there are ipc_namespace structures pending to be freed. This results in the simple test case below, as well as some real world workloads, to get allocation failures even when the number of ipc namespaces in actual use is way below the limit. int main() { int i; for (i = 0; i < 100000; i++) { if (unshare(CLONE_NEWIPC) < 0) error(EXIT_FAILURE, errno, "unshare"); } } Make the allocation of an ipc_namespace wait for pending frees, so it will succeed. real 6m19.197s user 0m0.041s sys 0m1.019s Signed-off-by: Rik van Riel Reported-by: Chris Mason --- ipc/namespace.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/ipc/namespace.c b/ipc/namespace.c index 8316ea585733..a26860a41dac 100644 --- a/ipc/namespace.c +++ b/ipc/namespace.c @@ -19,6 +19,12 @@ #include "util.h" +/* + * The work queue is used to avoid the cost of synchronize_rcu in kern_unmount. + */ +static void free_ipc(struct work_struct *unused); +static DECLARE_WORK(free_ipc_work, free_ipc); + static struct ucounts *inc_ipc_namespaces(struct user_namespace *ns) { return inc_ucount(ns, current_euid(), UCOUNT_IPC_NAMESPACES); @@ -37,9 +43,18 @@ static struct ipc_namespace *create_ipc_ns(struct user_namespace *user_ns, int err; err = -ENOSPC; + again: ucounts = inc_ipc_namespaces(user_ns); - if (!ucounts) + if (!ucounts) { + /* + * IPC namespaces are freed asynchronously, by free_ipc_work. + * If frees were pending, flush_work will wait, and + * return true. Fail the allocation if no frees are pending. + */ + if (flush_work(&free_ipc_work)) + goto again; goto fail; + } err = -ENOMEM; ns = kzalloc(sizeof(struct ipc_namespace), GFP_KERNEL_ACCOUNT); @@ -157,11 +172,6 @@ static void free_ipc(struct work_struct *unused) free_ipc_ns(n); } -/* - * The work queue is used to avoid the cost of synchronize_rcu in kern_unmount. - */ -static DECLARE_WORK(free_ipc_work, free_ipc); - /* * put_ipc_ns - drop a reference to an ipc namespace. * @ns: the namespace to put