The Linux Kernel Mailing List
 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>,
	Andrew Morton <akpm@linux-foundation.org>,
	Ian Kent <raven@themaw.net>, Jori Koolstra <jkoolstra@xs4all.nl>,
	"T . J . Mercier" <tjmercier@google.com>,
	Meta kernel team <kernel-team@meta.com>,
	cgroups@vger.kernel.org, linux-fsdevel@vger.kernel.org,
	driver-core@lists.linux.dev, linux-kernel@vger.kernel.org
Subject: [PATCH 2/3] kernfs: add a sequence count to directories
Date: Fri,  4 Dec 2026 13:10:31 -0800	[thread overview]
Message-ID: <20261204211034.3436390-3-shakeel.butt@linux.dev> (raw)
In-Reply-To: <20261204211034.3436390-1-shakeel.butt@linux.dev>

To let readers walk a directory's children without kernfs_rwsem, give
each directory a sequence count that is bumped around every change to
its children rbtree.  kernfs_rwsem, held for writing, already
serializes the writers.  The write section disables preemption, so a
reader never spins on a preempted writer.

New children are published with rb_link_node_rcu(), and the rbtree's
own stores are WRITE_ONCE(), so a reader walking down the tree under
RCU always ends on a valid node, and the sequence count tells it when
to retry.  Rename writes kn->hash with WRITE_ONCE(), and
kernfs_name_compare() reads it and kn->ns with READ_ONCE().

subdirs becomes an unsigned int, so that without lockdep the new field
does not make kernfs_node bigger.  kernfs.h only needs the type, from
seqlock_types.h.

No functional change.

Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
---
 fs/kernfs/dir.c             | 22 ++++++++++++++--------
 fs/kernfs/kernfs-internal.h | 27 +++++++++++++++++++++++++++
 include/linux/kernfs.h      |  9 ++++++++-
 3 files changed, 49 insertions(+), 9 deletions(-)

diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
index 4cb505242acb..9acefb8713fd 100644
--- a/fs/kernfs/dir.c
+++ b/fs/kernfs/dir.c
@@ -348,12 +348,14 @@ static unsigned int kernfs_name_hash(const char *name,
 static int kernfs_name_compare(unsigned int hash, const char *name,
 			       const struct ns_common *ns, const struct kernfs_node *kn)
 {
+	/* readers without kernfs_rwsem can race with a rename */
+	unsigned int kn_hash = READ_ONCE(kn->hash);
 	u64 ns_id = kernfs_ns_id(ns);
-	u64 kn_ns_id = kernfs_ns_id(kn->ns);
+	u64 kn_ns_id = kernfs_ns_id(READ_ONCE(kn->ns));
 
-	if (hash < kn->hash)
+	if (hash < kn_hash)
 		return -1;
-	if (hash > kn->hash)
+	if (hash > kn_hash)
 		return 1;
 	if (ns_id < kn_ns_id)
 		return -1;
@@ -408,13 +410,15 @@ static int kernfs_link_sibling(struct kernfs_node *kn)
 	}
 
 	/* add new node and rebalance the tree */
-	rb_link_node(&kn->rb, parent, node);
+	kernfs_dir_write_begin(kn_parent);
+	rb_link_node_rcu(&kn->rb, parent, node);
 	rb_insert_color(&kn->rb, &kn_parent->dir.children);
 
 	/* successfully added, account subdir number */
 	if (kernfs_type(kn) == KERNFS_DIR)
 		WRITE_ONCE(kn_parent->dir.subdirs, kn_parent->dir.subdirs + 1);
 	kernfs_inc_rev(kn_parent);
+	kernfs_dir_write_end(kn_parent);
 
 	return 0;
 }
@@ -442,12 +446,14 @@ static bool kernfs_unlink_sibling(struct kernfs_node *kn)
 		return false;
 
 	kn_parent = kernfs_parent(kn);
+	kernfs_dir_write_begin(kn_parent);
 	if (kernfs_type(kn) == KERNFS_DIR)
 		WRITE_ONCE(kn_parent->dir.subdirs, kn_parent->dir.subdirs - 1);
 	kernfs_inc_rev(kn_parent);
 
 	rb_erase(&kn->rb, &kn_parent->dir.children);
 	RB_CLEAR_NODE(&kn->rb);
+	kernfs_dir_write_end(kn_parent);
 	return true;
 }
 
@@ -1056,7 +1062,7 @@ struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,
 	}
 
 	kn->priv = priv;
-	kn->dir.root = root;
+	kernfs_init_dir(kn, root);
 
 	root->syscall_ops = scops;
 	root->flags = flags;
@@ -1125,7 +1131,7 @@ struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
 	if (!kn)
 		return ERR_PTR(-ENOMEM);
 
-	kn->dir.root = parent->dir.root;
+	kernfs_init_dir(kn, parent->dir.root);
 	kn->ns = ns;
 	kn->priv = priv;
 
@@ -1158,7 +1164,7 @@ struct kernfs_node *kernfs_create_empty_dir(struct kernfs_node *parent,
 		return ERR_PTR(-ENOMEM);
 
 	kn->flags |= KERNFS_EMPTY_DIR;
-	kn->dir.root = parent->dir.root;
+	kernfs_init_dir(kn, parent->dir.root);
 	kn->ns = NULL;
 	kn->priv = NULL;
 
@@ -1936,7 +1942,7 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
 	if (reparent)
 		kernfs_put(old_parent);
 
-	kn->hash = kernfs_name_hash(new_name ?: old_name, kn->ns);
+	WRITE_ONCE(kn->hash, kernfs_name_hash(new_name ?: old_name, kn->ns));
 	kernfs_link_sibling(kn);
 
 	if (new_name && !is_kernel_rodata((unsigned long)old_name))
diff --git a/fs/kernfs/kernfs-internal.h b/fs/kernfs/kernfs-internal.h
index 9bba1fc8acfd..00e57f3c1397 100644
--- a/fs/kernfs/kernfs-internal.h
+++ b/fs/kernfs/kernfs-internal.h
@@ -17,6 +17,7 @@
 #include <linux/xattr.h>
 
 #include <linux/kernfs.h>
+#include <linux/seqlock.h>
 #include <linux/fs_context.h>
 
 struct kernfs_iattrs {
@@ -170,6 +171,32 @@ static inline bool kernfs_dir_changed(struct kernfs_node *parent,
 	return READ_ONCE(parent->dir.rev) != READ_ONCE(dentry->d_time);
 }
 
+static inline void kernfs_init_dir(struct kernfs_node *kn,
+				   struct kernfs_root *root)
+{
+	kn->dir.root = root;
+	seqcount_init(&kn->dir.seq);
+}
+
+/*
+ * Bracket a change to @dir's children, so that readers walking them without
+ * kernfs_rwsem retry.  kernfs_rwsem serializes the writers.  The section
+ * must not be preempted, or those readers would spin until the writer runs
+ * again.
+ */
+static inline void kernfs_dir_write_begin(struct kernfs_node *dir)
+{
+	lockdep_assert_held_write(&dir->dir.root->kernfs_rwsem);
+	preempt_disable();
+	write_seqcount_begin(&dir->dir.seq);
+}
+
+static inline void kernfs_dir_write_end(struct kernfs_node *dir)
+{
+	write_seqcount_end(&dir->dir.seq);
+	preempt_enable();
+}
+
 extern const struct super_operations kernfs_sops;
 extern struct kmem_cache *kernfs_node_cache, *kernfs_iattrs_cache;
 
diff --git a/include/linux/kernfs.h b/include/linux/kernfs.h
index 803294a67e47..5d0c1122cb8d 100644
--- a/include/linux/kernfs.h
+++ b/include/linux/kernfs.h
@@ -18,6 +18,7 @@
 #include <linux/uidgid.h>
 #include <linux/wait.h>
 #include <linux/rwsem.h>
+#include <linux/seqlock_types.h>
 #include <linux/cache.h>
 
 struct file;
@@ -160,7 +161,13 @@ enum kernfs_root_flag {
 
 /* type-specific structures for kernfs_node union members */
 struct kernfs_elem_dir {
-	unsigned long		subdirs;
+	unsigned int		subdirs;
+	/*
+	 * Changes to @children are made with kernfs_rwsem held for writing
+	 * and inside a write section of @seq, so that readers without the
+	 * lock can walk @children under RCU and retry if it changed.
+	 */
+	seqcount_t		seq;
 	/* children rbtree starts here and goes through kn->rb */
 	struct rb_root		children;
 
-- 
2.53.1-Meta


  parent reply	other threads:[~2026-12-04 21:11 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 21:10 [PATCH 0/3] kernfs: list directories without kernfs_rwsem Shakeel Butt
2026-12-04 21:10 ` [PATCH 1/3] kernfs: don't look for open files of directories when draining Shakeel Butt
2026-12-04 21:10 ` Shakeel Butt [this message]
2026-12-04 21:10 ` [PATCH 3/3] kernfs: list directories without kernfs_rwsem 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=20261204211034.3436390-3-shakeel.butt@linux.dev \
    --to=shakeel.butt@linux.dev \
    --cc=akpm@linux-foundation.org \
    --cc=brauner@kernel.org \
    --cc=cgroups@vger.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