From patchwork Thu Jan 26 20:57:21 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: 48892 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp483836wrn; Thu, 26 Jan 2023 13:02:37 -0800 (PST) X-Google-Smtp-Source: AK7set9bCWZL/O1PtEt6rnGc0e9mCjY8k7A46DPdIIJLG1M+0mhWQfnE2D99emaUCvk+RNaIOJ9d X-Received: by 2002:a50:ce4f:0:b0:4a0:aac4:1030 with SMTP id k15-20020a50ce4f000000b004a0aac41030mr7625888edj.28.1674766957687; Thu, 26 Jan 2023 13:02:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674766957; cv=none; d=google.com; s=arc-20160816; b=KjFWR0q08qdSGA5KiPG3CAdY3A39uV2sWtUTJVT7uc19dG9V5zqXeNe6mFFPrkls4X aWy8EtPOjlYWA3/Et55FTbg7wVtSsdI/lKuecufTB89AQeCsG5DYB/ko3QokxOJqY5cz 0h0NSPOs1zmMFHWwhEaVX3zjWLsjEII3qBMcPbiFnpYuw/61OGMnRsbN1+qUulNipV0B 0lqf87dZ6dbiJXmjVd2uxgdejlytmERQVJkjawLcQjnTscpcddmkbiLODhOS9NdsS5AT UgdfWsfWoSZulMQtyOcgMPvtaS0H5CkRk9JBfQGOvtoN7o1bGwDjrqccXTmJgAj+NY6/ Dseg== 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=lnyRt/c5hgAwfmEmpKXaa0DNWkN00bDT/gNtFj559NZJsSNbzEeQMrUu3KGUCofiPl aOx2J3n+rXxJFQ3UCCOn03Hfm7SaCsru2WNlcPZ6UR7b5bOUszjyBLSRVsmXGNOkArWT Kg9Sx35/Y5tJDg5KJ1n+Tnu6esQFzIqOSTTG1eQRcBhzs7fucmvh84HQvDFHEl9Kx3GR NFniDzvEAWXYXkhDZQsvqAhE5PB29Dw/41KiJJ+4Xr6gMqYdFGPy7fvSDo6M+DHO0Dax 4oO89bZqSHVLy8BCuSBQpbQ2Q6+pp8ybMm9tyfcV2gttyF9ApETZLZgA+imMzfXKLRMp BXBQ== 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 z4-20020a50eb44000000b00469bac8510csi2795803edp.583.2023.01.26.13.02.13; Thu, 26 Jan 2023 13:02:37 -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 S232530AbjAZU5v (ORCPT + 99 others); Thu, 26 Jan 2023 15:57:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59126 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232543AbjAZU5s (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 2F2CB2700; Thu, 26 Jan 2023 12:57:46 -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-2C; 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 2/2] ipc,namespace: batch free ipc_namespace structures Date: Thu, 26 Jan 2023 15:57:21 -0500 Message-Id: <20230126205721.582612-3-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?1756120437384302445?= X-GMAIL-MSGID: =?utf-8?q?1756120437384302445?= 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); }