public inbox for linux-fsdevel@vger.kernel.org
 help / color / mirror / Atom feed
From: Christian Brauner <brauner@kernel.org>
To: linux-fsdevel@vger.kernel.org
Cc: Al Viro <viro@zeniv.linux.org.uk>,
	Miklos Szeredi <miklos@szeredi.hu>,
	 Jeff Layton <jlayton@kernel.org>,
	Josef Bacik <josef@toxicpanda.com>,
	 Seth Forshee <sforshee@kernel.org>,
	Christian Brauner <brauner@kernel.org>
Subject: [PATCH RFC 04/16] fs: add fastpath for dissolve_on_fput()
Date: Fri, 21 Feb 2025 14:13:03 +0100	[thread overview]
Message-ID: <20250221-brauner-open_tree-v1-4-dbcfcb98c676@kernel.org> (raw)
In-Reply-To: <20250221-brauner-open_tree-v1-0-dbcfcb98c676@kernel.org>

Instead of acquiring the namespace semaphore and the mount lock
everytime we close a file with FMODE_NEED_UNMOUNT set add a fastpath
that checks whether we need to at all. Most of the time the caller will
have attached the mount to the filesystem hierarchy and there's nothing
to do.

Signed-off-by: Christian Brauner <brauner@kernel.org>
---
 fs/namespace.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++------------
 1 file changed, 47 insertions(+), 12 deletions(-)

diff --git a/fs/namespace.c b/fs/namespace.c
index 7d0fa8ef8674..2cffcda8a48e 100644
--- a/fs/namespace.c
+++ b/fs/namespace.c
@@ -2246,22 +2246,57 @@ struct vfsmount *collect_mounts(const struct path *path)
 static void free_mnt_ns(struct mnt_namespace *);
 static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *, bool);
 
+static inline bool must_dissolve(struct mnt_namespace *mnt_ns)
+{
+	/*
+        * This mount belonged to an anonymous mount namespace
+        * but was moved to a non-anonymous mount namespace and
+        * then unmounted.
+        */
+	if (unlikely(!mnt_ns))
+		return false;
+
+	/*
+        * This mount belongs to a non-anonymous mount namespace
+        * and we know that such a mount can never transition to
+        * an anonymous mount namespace again.
+        */
+	if (!is_anon_ns(mnt_ns)) {
+		/*
+		 * A detached mount either belongs to an anonymous mount
+		 * namespace or a non-anonymous mount namespace. It
+		 * should never belong to something purely internal.
+		 */
+		VFS_WARN_ON_ONCE(mnt_ns == MNT_NS_INTERNAL);
+		return false;
+	}
+
+	return true;
+}
+
 void dissolve_on_fput(struct vfsmount *mnt)
 {
 	struct mnt_namespace *ns;
-	namespace_lock();
-	lock_mount_hash();
-	ns = real_mount(mnt)->mnt_ns;
-	if (ns) {
-		if (is_anon_ns(ns))
-			umount_tree(real_mount(mnt), UMOUNT_CONNECTED);
-		else
-			ns = NULL;
+	struct mount *m = real_mount(mnt);
+
+	scoped_guard(rcu) {
+		if (!must_dissolve(READ_ONCE(m->mnt_ns)))
+			return;
 	}
-	unlock_mount_hash();
-	namespace_unlock();
-	if (ns)
-		free_mnt_ns(ns);
+
+	scoped_guard(rwsem_write, &namespace_sem) {
+		ns = m->mnt_ns;
+		if (!must_dissolve(ns))
+			return;
+
+		lock_mount_hash();
+		umount_tree(m, UMOUNT_CONNECTED);
+		unlock_mount_hash();
+	}
+
+	/* Make sure we notice when we leak mounts. */
+	VFS_WARN_ON_ONCE(!mnt_ns_empty(ns));
+	free_mnt_ns(ns);
 }
 
 void drop_collected_mounts(struct vfsmount *mnt)

-- 
2.47.2


  parent reply	other threads:[~2025-02-21 13:13 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-21 13:12 [PATCH RFC 00/16] fs: expand abilities of anonymous mount namespaces Christian Brauner
2025-02-21 13:13 ` [PATCH RFC 01/16] fs: record sequence number of origin mount namespace Christian Brauner
2025-02-21 13:13 ` [PATCH RFC 02/16] fs: add mnt_ns_empty() helper Christian Brauner
2025-02-21 13:13 ` [PATCH RFC 03/16] fs: add assert for move_mount() Christian Brauner
2025-02-21 13:13 ` Christian Brauner [this message]
2025-02-21 13:13 ` [PATCH RFC 05/16] fs: add may_copy_tree() Christian Brauner
2025-02-21 13:13 ` [PATCH RFC 06/16] fs: create detached mounts from detached mounts Christian Brauner
2025-02-21 13:13 ` [PATCH RFC 07/16] selftests: " Christian Brauner
2025-02-21 13:13 ` [PATCH RFC 08/16] fs: support getname_maybe_null() in move_mount() Christian Brauner
2025-02-21 13:13 ` [PATCH RFC 09/16] fs: mount detached mounts onto detached mounts Christian Brauner
2025-02-21 13:13 ` [PATCH RFC 10/16] selftests: first test for mounting " Christian Brauner
2025-02-21 13:13 ` [PATCH RFC 11/16] selftests: second " Christian Brauner
2025-02-21 13:13 ` [PATCH RFC 12/16] selftests: third " Christian Brauner
2025-02-21 13:13 ` [PATCH RFC 13/16] selftests: fourth " Christian Brauner
2025-02-21 13:13 ` [PATCH RFC 14/16] selftests: fifth " Christian Brauner
2025-02-21 13:13 ` [PATCH RFC 15/16] selftests: sixth " Christian Brauner
2025-02-21 13:13 ` [PATCH RFC 16/16] selftests: seventh " Christian Brauner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250221-brauner-open_tree-v1-4-dbcfcb98c676@kernel.org \
    --to=brauner@kernel.org \
    --cc=jlayton@kernel.org \
    --cc=josef@toxicpanda.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=miklos@szeredi.hu \
    --cc=sforshee@kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox