From patchwork Mon Oct 2 02:37:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Al Viro X-Patchwork-Id: 13405519 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D19C2E784A7 for ; Mon, 2 Oct 2023 02:37:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229990AbjJBChQ (ORCPT ); Sun, 1 Oct 2023 22:37:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34332 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229935AbjJBChP (ORCPT ); Sun, 1 Oct 2023 22:37:15 -0400 Received: from zeniv.linux.org.uk (zeniv.linux.org.uk [IPv6:2a03:a000:7:0:5054:ff:fe1c:15ff]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 33FC9C9 for ; Sun, 1 Oct 2023 19:37:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=linux.org.uk; s=zeniv-20220401; h=Sender:In-Reply-To:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=+qRw2nig3APcRxOSe394947Vm6w5yWReQ0ggdVYj3xA=; b=uA44cOVCuiHmIhoWPPIJwrAgeG VehtUOPWTC+Vkbqt8BJinaG7VnvGv3D1BgZEheAHUrkw+AMgpWaFuAHm599AILmROC+Z7n2vLJNVg Xam224y9QIdlkC8SXeAFYB2GDyo6ESKxxHxZWvXvRRqerf9HmfbJkrx6CdU1AmRn85cdj7I5++y3e jyTuQbTUV3ccliXLckGtGBmTZMxjLl94B3+kiF1wvKpsPzceWCKvMJHT82Y9uWhUQTBMHAqg6igde Zb56D7MbOdYf//sHcrvft2djsXCPpKmXOiP0ZmX8k9bpdkiPumQGlAJTwqx8FpGNjYRONz1wMEXN2 A2bt5A5Q==; Received: from viro by zeniv.linux.org.uk with local (Exim 4.96 #2 (Red Hat Linux)) id 1qn8o3-00EE0M-0r; Mon, 02 Oct 2023 02:37:11 +0000 Date: Mon, 2 Oct 2023 03:37:11 +0100 From: Al Viro To: linux-fsdevel@vger.kernel.org Cc: Christian Brauner , Christoph Hellwig , Linus Torvalds , Namjae Jeon , David Sterba , David Howells , Miklos Szeredi , Amir Goldstein , Trond Myklebust , Bob Peterson , Steve French , Luis Chamberlain Subject: [PATCH 15/15] overlayfs: make use of ->layers safe in rcu pathwalk Message-ID: <20231002023711.GP3389589@ZenIV> References: <20231002022815.GQ800259@ZenIV> <20231002022846.GA3389589@ZenIV> <20231002023613.GN3389589@ZenIV> <20231002023643.GO3389589@ZenIV> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20231002023643.GO3389589@ZenIV> Sender: Al Viro Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org ovl_permission() accesses ->layers[...].mnt; we can't have ->layers freed without an RCU delay on fs shutdown. Fortunately, kern_unmount_array() used to drop those mounts does include an RCU delay, so freeing is delayed; unfortunately, the array passed to kern_unmount_array() is formed by mangling ->layers contents and that happens without any delays. Use a separate array instead; local if we have a few layers, kmalloc'ed if there's a lot of them. If allocation fails, fall back to kern_unmount() for individual mounts; it's not a fast path by any stretch of imagination. Signed-off-by: Al Viro --- fs/overlayfs/ovl_entry.h | 1 - fs/overlayfs/params.c | 26 ++++++++++++++++++++------ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/fs/overlayfs/ovl_entry.h b/fs/overlayfs/ovl_entry.h index e9539f98e86a..618b63bb7987 100644 --- a/fs/overlayfs/ovl_entry.h +++ b/fs/overlayfs/ovl_entry.h @@ -30,7 +30,6 @@ struct ovl_sb { }; struct ovl_layer { - /* ovl_free_fs() relies on @mnt being the first member! */ struct vfsmount *mnt; /* Trap in ovl inode cache */ struct inode *trap; diff --git a/fs/overlayfs/params.c b/fs/overlayfs/params.c index b9355bb6d75a..ab594fd407b4 100644 --- a/fs/overlayfs/params.c +++ b/fs/overlayfs/params.c @@ -738,8 +738,15 @@ int ovl_init_fs_context(struct fs_context *fc) void ovl_free_fs(struct ovl_fs *ofs) { struct vfsmount **mounts; + struct vfsmount *m[16]; + unsigned n = ofs->numlayer; unsigned i; + if (n > 16) + mounts = kmalloc_array(n, sizeof(struct mount *), GFP_KERNEL); + else + mounts = m; + iput(ofs->workbasedir_trap); iput(ofs->indexdir_trap); iput(ofs->workdir_trap); @@ -752,14 +759,21 @@ void ovl_free_fs(struct ovl_fs *ofs) if (ofs->upperdir_locked) ovl_inuse_unlock(ovl_upper_mnt(ofs)->mnt_root); - /* Hack! Reuse ofs->layers as a vfsmount array before freeing it */ - mounts = (struct vfsmount **) ofs->layers; - for (i = 0; i < ofs->numlayer; i++) { + for (i = 0; i < n; i++) { iput(ofs->layers[i].trap); - mounts[i] = ofs->layers[i].mnt; - kfree(ofs->layers[i].name); + if (unlikely(!mounts)) + kern_unmount(ofs->layers[i].mnt); + else + mounts[i] = ofs->layers[i].mnt; } - kern_unmount_array(mounts, ofs->numlayer); + if (mounts) { + kern_unmount_array(mounts, n); + if (mounts != m) + kfree(mounts); + } + // by this point we had an RCU delay from kern_unmount{_array,}() + for (i = 0; i < n; i++) + kfree(ofs->layers[i].name); kfree(ofs->layers); for (i = 0; i < ofs->numfs; i++) free_anon_bdev(ofs->fs[i].pseudo_dev);