From patchwork Fri Jan 27 01:15:34 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: 49012 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp587434wrn; Thu, 26 Jan 2023 17:35:13 -0800 (PST) X-Google-Smtp-Source: AMrXdXtJQMX4LMNfM4mAJULPkk9U8myH9+h4LLqR4NRQyeizQGFBbL/IXBsd6Z7qJ6vMtPgBoviW X-Received: by 2002:a05:6402:e01:b0:49b:65cc:faa6 with SMTP id h1-20020a0564020e0100b0049b65ccfaa6mr49937103edh.16.1674783312914; Thu, 26 Jan 2023 17:35:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674783312; cv=none; d=google.com; s=arc-20160816; b=I3oYZAPlr3wwdjGE7sp0CEy371j7FQ1wIPSRoPlhFhnI69Mt3/zxgx+IWH6HTEtJmT xrOzMxpx+CFQqaKv4bCh3pemvMFV9OCtY32+rhj637qR9LWLLirDbUSSQzHo2QDWSvhw JUVVxuJ+zMC0je02YHCLdG/KhC/xdC+cqMPzQqucpNYFIY3XFJUPvDLkzparu/vQZwoI KhAd1RItPq7eeqdLWJY1WT/40xtq1CLOKR/cImlUeOYxGY6TMvvVoxsQWYSTnstrZAwl nBSh6WqNKS57AhsDCjFj2QJUrs2G8W7kuywt9p9Ur/mfrXDjwIf0D/5tRkRmkqmD8/XZ 6moQ== 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=sQNAr8DPmFBD6RZJOJOHwajw1uRKUnVyisvA/+hWIOcR+ir4FtvvHGYQy/3/Ezlt3l CJQ4e13J6BfZCEHVdWWOR1x4P2XyH7uaaqVjKL+8rOykXy25akjOTlJREbcTLgiLBnCV VieudYKqwUeS4CLKiN0Km5F8CJs+0aZBbuIv7AKgL6/5JZ7QmqgQMLJympg7yVXSKYcb SewisAj8zN2v+/k7SGGen8T7MuxprZP0S4M7aUsIFy7roZkIPahT55R/jIPTSpbh2dwZ gPU/YclHReFVK+ISPFd87fecTklxfjcU5KN4cL/4i0WfAKSuJpaOQ8HtBbsRAJCoJPlJ rc6g== 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 n21-20020a05640205d500b00486b6ed8fa9si3504669edx.374.2023.01.26.17.34.46; Thu, 26 Jan 2023 17:35:12 -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 S233068AbjA0BP4 (ORCPT + 99 others); Thu, 26 Jan 2023 20:15:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49282 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231540AbjA0BPv (ORCPT ); Thu, 26 Jan 2023 20:15:51 -0500 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CB6F56F21C; Thu, 26 Jan 2023 17:15:49 -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 1pLDLC-00038z-2q; Thu, 26 Jan 2023 20:15:42 -0500 From: Rik van Riel To: viro@zeniv.linux.org.uk, linux-kernel@vger.kernel.org, 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 20:15:34 -0500 Message-Id: <20230127011535.1265297-2-riel@surriel.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20230127011535.1265297-1-riel@surriel.com> References: <20230127011535.1265297-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?1756137587414091923?= X-GMAIL-MSGID: =?utf-8?q?1756137587414091923?= 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 From patchwork Fri Jan 27 01:15:35 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: 49011 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp584626wrn; Thu, 26 Jan 2023 17:27:44 -0800 (PST) X-Google-Smtp-Source: AMrXdXtRBJGuuljM9qdIKkrh2MXUt1830deWCl4pwkZGPRwi/VASEGxVmkMTFOLdXj+Ac9NwI8eR X-Received: by 2002:a17:907:75e7:b0:871:3919:cbea with SMTP id jz7-20020a17090775e700b008713919cbeamr38799114ejc.54.1674782864544; Thu, 26 Jan 2023 17:27:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674782864; cv=none; d=google.com; s=arc-20160816; b=DyeGoRi+d96HYjbOqmT/8j+X4lFl81TSqX7pLyJPvMyj9Me9xZaC/25FIUE2OdGxsX rcB8ubkhOinqgJL95UwWCMpdGkDEjoHH1/FJw3uzoQa3VUj6b579kOJpj8/0dp5AO3XY JeZLakZCNz4r4USq4Str+EveQW1MntRkuCpvyiVgeF1SNOAqJCdIWtQilWV5AWZiRqV7 +ujU5q6mUHlxKibkX3ZdN+uyxrRQREZdGP1d+Q8gSoQuTzVJthjuqi8cj4kxgtuY/tbN eVO5bicexvcIxSjwpucmOj7p8q6Je2Lu5mJQQak4Z4liHgoGyAbKcBYtx2ZyXRbKpKSR uE9A== 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=uD54bMAjiPzpxW2HL1R0H23WWShRtgJCKHko4iP+Bis=; b=O/DAV72MJwbVy6+X0gRwDF9/f0ES6j0epsVhJH5In8eiqHpRMK6IWw9q3OLyyzAfw0 MsAXOW95rn5IkpNwFVztXvnH7/70rFsPZGpDNSKtG3oLmxogOrklAGoTm6/vc3Rzh6Qo +zH67kOxIpMBtVg6o1ep8gHjsRrDq6tALmvaZrkqRlm/6GHVcD6PT1LYMBnocuojShNr nHxONAv1noRAfkNHUXzpDQoVgA7IMTfZdj/xWu57FzIbdF2JedFVHy4An3Z7+1vA90jX izYGqMEHbc8pFOAj2akzjbcJPkW+dmwFyAMFnySpuwh22kFDM4oer6VfVIuM3uOgtjuR tEZA== 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 gj31-20020a170907741f00b0087139670f21si2515613ejc.913.2023.01.26.17.27.20; Thu, 26 Jan 2023 17:27:44 -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 S232894AbjA0BPx (ORCPT + 99 others); Thu, 26 Jan 2023 20:15:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49262 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229844AbjA0BPu (ORCPT ); Thu, 26 Jan 2023 20:15:50 -0500 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CB7836F223; Thu, 26 Jan 2023 17:15:49 -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 1pLDLC-00038z-2y; Thu, 26 Jan 2023 20:15:42 -0500 From: Rik van Riel To: viro@zeniv.linux.org.uk, linux-kernel@vger.kernel.org, kernel-team@meta.com, linux-fsdevel@vger.kernel.org, gscrivan@redhat.com Cc: Rik van Riel , Chris Mason Subject: [PATCH 2/2] ipc,namespace: batch free ipc_namespace structures Date: Thu, 26 Jan 2023 20:15:35 -0500 Message-Id: <20230127011535.1265297-3-riel@surriel.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20230127011535.1265297-1-riel@surriel.com> References: <20230127011535.1265297-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?1756137117061748156?= X-GMAIL-MSGID: =?utf-8?q?1756137117061748156?= Instead of waiting for an RCU grace period between each ipc_namespace structure that is being freed, wait an RCU grace period for every batch of ipc_namespace structures. Thanks to Al Viro for the suggestion of the helper function. This speeds up the run time of the test case that allocates ipc_namespaces in a loop from 6 minutes, to a little over 1 second: real 0m1.192s user 0m0.038s sys 0m1.152s Signed-off-by: Rik van Riel Reported-by: Chris Mason Suggested-by: Al Viro --- fs/namespace.c | 10 ++++++++++ include/linux/mount.h | 1 + ipc/namespace.c | 13 ++++++++++--- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/fs/namespace.c b/fs/namespace.c index ab467ee58341..296432ba3716 100644 --- a/fs/namespace.c +++ b/fs/namespace.c @@ -1397,6 +1397,16 @@ struct vfsmount *mntget(struct vfsmount *mnt) } EXPORT_SYMBOL(mntget); +/* + * Make a mount point inaccessible to new lookups. + * Because there may still be current users, the caller MUST WAIT + * for an RCU grace period before destroying the mount point. + */ +void mnt_make_shortterm(struct vfsmount *mnt) +{ + real_mount(mnt)->mnt_ns = NULL; +} + /** * path_is_mountpoint() - Check if path is a mount in the current namespace. * @path: path to check diff --git a/include/linux/mount.h b/include/linux/mount.h index 62475996fac6..ec55a031aa8c 100644 --- a/include/linux/mount.h +++ b/include/linux/mount.h @@ -88,6 +88,7 @@ extern void mnt_drop_write(struct vfsmount *mnt); extern void mnt_drop_write_file(struct file *file); extern void mntput(struct vfsmount *mnt); extern struct vfsmount *mntget(struct vfsmount *mnt); +extern void mnt_make_shortterm(struct vfsmount *mnt); extern struct vfsmount *mnt_clone_internal(const struct path *path); extern bool __mnt_is_readonly(struct vfsmount *mnt); extern bool mnt_may_suid(struct vfsmount *mnt); diff --git a/ipc/namespace.c b/ipc/namespace.c index a26860a41dac..6ecc30effd3e 100644 --- a/ipc/namespace.c +++ b/ipc/namespace.c @@ -145,10 +145,11 @@ void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids, static void free_ipc_ns(struct ipc_namespace *ns) { - /* mq_put_mnt() waits for a grace period as kern_unmount() - * uses synchronize_rcu(). + /* + * Caller needs to wait for an RCU grace period to have passed + * after making the mount point inaccessible to new accesses. */ - mq_put_mnt(ns); + mntput(ns->mq_mnt); sem_exit_ns(ns); msg_exit_ns(ns); shm_exit_ns(ns); @@ -168,6 +169,12 @@ static void free_ipc(struct work_struct *unused) struct llist_node *node = llist_del_all(&free_ipc_list); struct ipc_namespace *n, *t; + llist_for_each_entry_safe(n, t, node, mnt_llist) + mnt_make_shortterm(n->mq_mnt); + + /* Wait for any last users to have gone away. */ + synchronize_rcu(); + llist_for_each_entry_safe(n, t, node, mnt_llist) free_ipc_ns(n); }