[PATCH 2/3] kernfs: add a sequence count to directories
From: Shakeel Butt
Date: Fri Dec 04 2026 - 16:11:06 EST
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@xxxxxxxxx>
---
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