Linux driver-core infrastructure
 help / color / mirror / Atom feed
From: Shakeel Butt <shakeel.butt@linux.dev>
To: Christian Brauner <brauner@kernel.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Tejun Heo <tj@kernel.org>, Jori Koolstra <jkoolstra@xs4all.nl>,
	Ian Kent <raven@themaw.net>,
	"T . J . Mercier" <tjmercier@google.com>,
	Meta kernel team <kernel-team@meta.com>,
	linux-fsdevel@vger.kernel.org, driver-core@lists.linux.dev,
	linux-kernel@vger.kernel.org
Subject: [PATCH 4/6] kernfs: don't take kernfs_iattr_rwsem to publish KERNFS_REMOVING
Date: Wed, 18 Nov 2026 14:01:51 -0800	[thread overview]
Message-ID: <20261118220154.2965621-5-shakeel.butt@linux.dev> (raw)
In-Reply-To: <20261118220154.2965621-1-shakeel.butt@linux.dev>

__kernfs_remove() takes kernfs_iattr_rwsem for writing while it marks
a subtree KERNFS_REMOVING, inside kernfs_rwsem.  Commit 507d8ce13f5b
("kernfs: Don't set_nlink for directories being removed") added it so
that a refresh would not set the i_nlink of a directory whose removal
has started.  The refresh no longer sets i_nlink, so the lock is not
needed.

The flag is only set here, under kernfs_rwsem.  Inode setup reads it
under kernfs_rwsem.  getattr reads it without and reports the old link
count or 0, either of which is right for a stat() racing the removal.

Drop the lock, mark the store and the load, and read the flags once in
kernfs_dir_nlink(), as the type bits share the word.

kernfs_iattr_rwsem write acquisitions (lock_stat), before -> after:

  5 dummy netdevs created and removed     1110 ->  740
  100 cgroup mkdirs and 100 rmdirs        4500 -> 3000

Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
---
 fs/kernfs/dir.c   | 10 ++++++----
 fs/kernfs/inode.c |  7 +++++--
 2 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
index ef23eac684c6..890aca5d8d70 100644
--- a/fs/kernfs/dir.c
+++ b/fs/kernfs/dir.c
@@ -1604,15 +1604,17 @@ static void __kernfs_remove(struct kernfs_node *kn)
 
 	pr_debug("kernfs %s: removing\n", kernfs_rcu_name(kn));
 
-	/* prevent new usage by marking all nodes removing and deactivating */
-	down_write(&kernfs_root(kn)->kernfs_iattr_rwsem);
+	/*
+	 * Prevent new usage by marking all nodes removing and deactivating.
+	 * KERNFS_REMOVING is only set here, under kernfs_rwsem.
+	 * kernfs_dir_nlink() reads it without that lock.
+	 */
 	pos = NULL;
 	while ((pos = kernfs_next_descendant_post(pos, kn))) {
-		pos->flags |= KERNFS_REMOVING;
+		WRITE_ONCE(pos->flags, pos->flags | KERNFS_REMOVING);
 		if (kernfs_active(pos))
 			atomic_add(KN_DEACTIVATED_BIAS, &pos->active);
 	}
-	up_write(&kernfs_root(kn)->kernfs_iattr_rwsem);
 
 	/* deactivate and unlink the subtree node-by-node */
 	do {
diff --git a/fs/kernfs/inode.c b/fs/kernfs/inode.c
index 2f463c0354a5..ca234f69882b 100644
--- a/fs/kernfs/inode.c
+++ b/fs/kernfs/inode.c
@@ -165,9 +165,12 @@ static inline void set_inode_attr(struct inode *inode,
  */
 static bool kernfs_dir_nlink(struct kernfs_node *kn, unsigned int *nlink)
 {
-	if (kernfs_type(kn) != KERNFS_DIR)
+	/* KERNFS_REMOVING can be set concurrently; read the flags once. */
+	unsigned short flags = READ_ONCE(kn->flags);
+
+	if ((flags & KERNFS_TYPE_MASK) != KERNFS_DIR)
 		return false;
-	*nlink = (kn->flags & KERNFS_REMOVING) ? 0 :
+	*nlink = (flags & KERNFS_REMOVING) ? 0 :
 		 READ_ONCE(kn->dir.subdirs) + 2;
 	return true;
 }
-- 
2.53.1-Meta


  parent reply	other threads:[~2026-11-18 22:02 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 22:01 [PATCH 0/6] kernfs: stop taking kernfs_iattr_rwsem for writing in mkdir/rmdir Shakeel Butt
2026-11-18 22:01 ` [PATCH 1/6] kernfs: report times and link count from the node in getattr Shakeel Butt
2026-11-18 22:01 ` [PATCH 2/6] kernfs: don't copy times and link count into the inode on refresh Shakeel Butt
2026-11-18 22:01 ` [PATCH 3/6] kernfs: don't take kernfs_iattr_rwsem for the directory counters Shakeel Butt
2026-11-18 22:01 ` Shakeel Butt [this message]
2026-11-18 22:01 ` [PATCH 5/6] kernfs: update a directory's times without kernfs_iattr_rwsem Shakeel Butt
2026-11-18 22:01 ` [PATCH 6/6] kernfs: don't take kernfs_iattr_rwsem in __kernfs_remove() Shakeel Butt

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=20261118220154.2965621-5-shakeel.butt@linux.dev \
    --to=shakeel.butt@linux.dev \
    --cc=brauner@kernel.org \
    --cc=driver-core@lists.linux.dev \
    --cc=gregkh@linuxfoundation.org \
    --cc=jkoolstra@xs4all.nl \
    --cc=kernel-team@meta.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=raven@themaw.net \
    --cc=tj@kernel.org \
    --cc=tjmercier@google.com \
    /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