* [PATCH 0/3] kernfs: list directories without kernfs_rwsem
@ 2026-12-04 21:10 Shakeel Butt
2026-12-04 21:10 ` [PATCH 1/3] kernfs: don't look for open files of directories when draining Shakeel Butt
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Shakeel Butt @ 2026-12-04 21:10 UTC (permalink / raw)
To: Christian Brauner
Cc: Greg Kroah-Hartman, Tejun Heo, Andrew Morton, Ian Kent,
Jori Koolstra, T . J . Mercier, Meta kernel team, cgroups,
linux-fsdevel, driver-core, linux-kernel
kernfs_fop_readdir() takes kernfs_rwsem for reading for each entry, and
there is one kernfs_rwsem for the whole hierarchy. So a listing of any
cgroup directory waits whenever a writer anywhere in cgroupfs holds the
lock or waits for it, for example a mkdir that is preempted with the
lock held. Monitors such as oomd and below list cgroup directories all
the time. In a week of data from our 6.16 fleet, on 92% of the hosts
where oomd or below got stuck in a kernfs read, it was in
kernfs_fop_readdir().
This series lets readdir run under RCU with a sequence count in each
directory. Writers keep kernfs_rwsem, and a listing step falls back to
it only if the directory keeps changing under it.
It is one step toward kernfs readers that never wait for writers.
Andrew is working on another: keeping kernfs path walks in RCU walk,
with lockless permission checks. The two do not depend on each other
and apply together.
Patch 1 fixes a type confusion in kernfs_drain() that patch 2's layout
change would otherwise turn into an oops on lockdep kernels. Patch 2
adds the sequence count. Patch 3 lists without the lock.
In a 16 CPU VM, with 8 tasks listing cgroup directories of 36 entries
(ranges over two or three runs):
before after
with a SCHED_IDLE mkdir/rmdir loop sharing a CPU with a busy loop:
slowest listing 205-271 ms 0.19-0.86 ms
listings per second 48k-51k 1.26M-1.28M
with 4 mkdir/rmdir loops:
listings per second 96k-98k 1.26M-1.27M
mkdir+rmdir pairs per second 280-285 7.2k-7.4k
15 tasks listing one directory with no writers went from 157k-172k to
549k-690k listings per second. Listing /sys/class/net in the root
namespace, which skips 1000 devices in other namespaces, went from
20k-21k to 29k-30k per second.
Tested with the kernfs and cgroup selftests, and with listings checked
for missing and repeated entries while cgroup files, cgroups and network
devices are added, removed, renamed and moved between network
namespaces, on lockdep, KASAN and KCSAN kernels, and on a kernel that
takes the fallback for every step. Listings of a few sysfs and cgroupfs
directories are the same, in the same order, before and after.
The series is on vfs-7.5.kernfs.
Shakeel Butt (3):
kernfs: don't look for open files of directories when draining
kernfs: add a sequence count to directories
kernfs: list directories without kernfs_rwsem
fs/kernfs/dir.c | 304 ++++++++++++++++++++++++------------
fs/kernfs/file.c | 7 +
fs/kernfs/kernfs-internal.h | 27 ++++
include/linux/kernfs.h | 9 +-
4 files changed, 242 insertions(+), 105 deletions(-)
base-commit: 8900b8b7e0d8b3b754f547ea31bed1a8f6e4814a
--
2.53.1-Meta
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 1/3] kernfs: don't look for open files of directories when draining
2026-12-04 21:10 [PATCH 0/3] kernfs: list directories without kernfs_rwsem Shakeel Butt
@ 2026-12-04 21:10 ` Shakeel Butt
2026-12-04 21:10 ` [PATCH 2/3] kernfs: add a sequence count to directories Shakeel Butt
2026-12-04 21:10 ` [PATCH 3/3] kernfs: list directories without kernfs_rwsem Shakeel Butt
2 siblings, 0 replies; 4+ messages in thread
From: Shakeel Butt @ 2026-12-04 21:10 UTC (permalink / raw)
To: Christian Brauner
Cc: Greg Kroah-Hartman, Tejun Heo, Andrew Morton, Ian Kent,
Jori Koolstra, T . J . Mercier, Meta kernel team, cgroups,
linux-fsdevel, driver-core, linux-kernel
kernfs_drain() calls kernfs_should_drain_open_files() for every node it
removes, directories and symlinks included, and that reads
kn->attr.open from the node's union. For a directory, the field
overlaps dir.children.rb_node, which is NULL by the time a directory is
drained, as its children are removed first. So it works, but only
because of how kernfs_elem_dir is laid out. The next patch adds a
sequence count to it, and with lockdep, attr.open then overlaps data
that is not zero.
Only files have open nodes. Check the type first.
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
---
fs/kernfs/file.c | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c
index 08c29eb3f435..b141c09aadbe 100644
--- a/fs/kernfs/file.c
+++ b/fs/kernfs/file.c
@@ -837,6 +837,13 @@ bool kernfs_should_drain_open_files(struct kernfs_node *kn)
struct kernfs_open_node *on;
bool ret;
+ /*
+ * Only files have open nodes. For a directory or a symlink,
+ * @kn->attr.open would read another member of the union.
+ */
+ if (kernfs_type(kn) != KERNFS_FILE)
+ return false;
+
/*
* @kn being deactivated guarantees that @kn->attr.open can't change
* beneath us making the lockless test below safe.
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH 2/3] kernfs: add a sequence count to directories
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
2026-12-04 21:10 ` [PATCH 3/3] kernfs: list directories without kernfs_rwsem Shakeel Butt
2 siblings, 0 replies; 4+ messages in thread
From: Shakeel Butt @ 2026-12-04 21:10 UTC (permalink / raw)
To: Christian Brauner
Cc: Greg Kroah-Hartman, Tejun Heo, Andrew Morton, Ian Kent,
Jori Koolstra, T . J . Mercier, Meta kernel team, cgroups,
linux-fsdevel, driver-core, linux-kernel
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
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH 3/3] kernfs: list directories without kernfs_rwsem
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 ` [PATCH 2/3] kernfs: add a sequence count to directories Shakeel Butt
@ 2026-12-04 21:10 ` Shakeel Butt
2 siblings, 0 replies; 4+ messages in thread
From: Shakeel Butt @ 2026-12-04 21:10 UTC (permalink / raw)
To: Christian Brauner
Cc: Greg Kroah-Hartman, Tejun Heo, Andrew Morton, Ian Kent,
Jori Koolstra, T . J . Mercier, Meta kernel team, cgroups,
linux-fsdevel, driver-core, linux-kernel
kernfs_fop_readdir() takes kernfs_rwsem for reading for each entry.
There is one kernfs_rwsem for the whole hierarchy, so a listing of any
cgroup directory waits whenever a writer anywhere in cgroupfs holds the
lock or waits for it, for example a mkdir that is preempted with the
lock held. Monitors such as oomd and below list cgroup directories all
the time.
List under RCU instead, retrying on the directory's sequence count:
- Each step searches down the tree for the first visible entry after
the last one listed, and copies its name, ino, type and hash in the
same read section. rb_next() is not used, as it follows parent
pointers, which are not safe to follow without the lock.
- The cursor pinned when dir_emit() fails is used only if it is still
in the directory under its current key, which also shows that the
key was read consistently.
- A found entry is pinned with atomic_inc_not_zero(). If its count is
already zero, it has been unlinked and is being freed, and the
listing goes on after it.
- If the directory keeps changing, a step takes kernfs_rwsem after a
few attempts, so that a listing always makes progress.
The listing shows the same entries in the same order as before.
Skipping k hidden entries, dying ones or other namespaces', now takes k
searches, O(k log n) instead of O(k).
A lockless listing may read an entry's namespace up to an RCU grace
period after the entry is removed or renamed into another namespace.
That is safe for network namespaces, the only namespaces kernfs has:
cleanup_net() frees a namespace only in a later run, after the
synchronize_rcu() in ops_undo_list().
In a 16 CPU VM, 8 tasks listed cgroup directories of 36 entries while
a mkdir and rmdir loop ran at SCHED_IDLE on a CPU shared with a busy
loop, so that it was often preempted with the lock held. The slowest
listing went from 271 ms to 188 us, and listings per second from 51k to
1.26M. With 4 mkdir and rmdir loops at normal priority instead, the
readers went from 97k to 1.26M listings per second, and the writers
from 284 to 7440 mkdir and rmdir pairs per second. Listing
/sys/class/net in the root namespace, which skips 1000 devices in other
namespaces, went from 20.5k to 29.6k listings per second.
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
---
fs/kernfs/dir.c | 282 +++++++++++++++++++++++++++++++-----------------
1 file changed, 186 insertions(+), 96 deletions(-)
diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
index 9acefb8713fd..1cc5c10bead5 100644
--- a/fs/kernfs/dir.c
+++ b/fs/kernfs/dir.c
@@ -1964,140 +1964,230 @@ static int kernfs_dir_fop_release(struct inode *inode, struct file *filp)
}
/*
- * Find where a listing left off. @resumed says whether @pos is still that
- * entry; if not, the search falls back to @hash, keyed by @name if given.
+ * The walks below run under RCU, without kernfs_rwsem. They only go down
+ * the tree, which is safe during a change but may then miss an entry, so
+ * the caller retries if @parent->dir.seq changed.
*/
-static struct kernfs_node *kernfs_dir_pos(const struct ns_common *ns,
- struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos,
- const char *name, bool *resumed)
+
+/* the child of @parent with the key (@hash, @ns, @name) */
+static struct kernfs_node *kernfs_dir_find_key(struct kernfs_node *parent,
+ unsigned int hash,
+ const struct ns_common *ns,
+ const char *name)
{
- if (resumed)
- *resumed = false;
- if (pos) {
- /*
- * A rename keeps the hash if the new name hashes the same, so
- * check @name too. Otherwise the caller would step over the
- * entry now sitting where @pos used to be.
- */
- int valid = kernfs_active(pos) &&
- rcu_access_pointer(pos->__parent) == parent &&
- hash == pos->hash &&
- (!name || !strcmp(name, kernfs_rcu_name(pos)));
- kernfs_put(pos);
- if (!valid)
- pos = NULL;
- else if (resumed)
- *resumed = true;
+ struct rb_node *node = rcu_dereference_raw(parent->dir.children.rb_node);
+
+ while (node) {
+ struct kernfs_node *kn = rb_to_kn(node);
+ int result = kernfs_name_compare(hash, name, ns, kn);
+
+ if (result < 0)
+ node = rcu_dereference_raw(node->rb_left);
+ else if (result > 0)
+ node = rcu_dereference_raw(node->rb_right);
+ else
+ return kn;
}
- if (!pos && (hash > 1) && (hash < INT_MAX)) {
- struct rb_node *node = parent->dir.children.rb_node;
+ return NULL;
+}
- /*
- * Keep a node only on the way left, so the search ends on the
- * first entry after the key. An empty @name sorts before all
- * entries sharing the hash, so it lands on the first of them.
- */
- while (node) {
- struct kernfs_node *kn = rb_to_kn(node);
-
- if (kernfs_name_compare(hash, name ?: "", ns, kn) < 0) {
- pos = kn;
- node = node->rb_left;
- } else {
- node = node->rb_right;
- }
+/*
+ * The first child of @parent whose key is greater than (@hash, @ns, @name).
+ * An empty @name sorts before every name with the same hash and namespace.
+ */
+static struct kernfs_node *kernfs_dir_entry_after(struct kernfs_node *parent,
+ unsigned int hash,
+ const struct ns_common *ns,
+ const char *name)
+{
+ struct rb_node *node = rcu_dereference_raw(parent->dir.children.rb_node);
+ struct kernfs_node *pos = NULL;
+
+ /* keep a node only on the way left, so the walk ends on the first one */
+ while (node) {
+ struct kernfs_node *kn = rb_to_kn(node);
+
+ if (kernfs_name_compare(hash, name, ns, kn) < 0) {
+ pos = kn;
+ node = rcu_dereference_raw(node->rb_left);
+ } else {
+ node = rcu_dereference_raw(node->rb_right);
}
}
- /* Skip over entries which are dying/dead or in the wrong namespace */
- while (pos && (!kernfs_active(pos) ||
- kernfs_ns_id(pos->ns) != kernfs_ns_id(ns))) {
- struct rb_node *node = rb_next(&pos->rb);
- if (!node)
- pos = NULL;
- else
- pos = rb_to_kn(node);
- }
return pos;
}
-static struct kernfs_node *kernfs_dir_next_pos(const struct ns_common *ns,
- struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos,
- const char *name)
+/*
+ * Like kernfs_dir_entry_after(), but skip the children that a listing in
+ * namespace @ns does not show: dying or dead ones, and other namespaces'.
+ * Gives up early if the tree changed since @seq; the caller retries then.
+ */
+static struct kernfs_node *kernfs_dir_visible_after(struct kernfs_node *parent,
+ const struct ns_common *ns,
+ unsigned int hash,
+ const char *name,
+ unsigned int seq)
{
- bool resumed;
-
- pos = kernfs_dir_pos(ns, parent, hash, pos, name, &resumed);
- /* Step over @pos only if it survived; @name finds the spot if not. */
- if (pos && resumed) {
- do {
- struct rb_node *node = rb_next(&pos->rb);
- if (!node)
- pos = NULL;
- else
- pos = rb_to_kn(node);
- } while (pos && (!kernfs_active(pos) ||
- kernfs_ns_id(pos->ns) != kernfs_ns_id(ns)));
+ struct kernfs_node *pos = kernfs_dir_entry_after(parent, hash, ns, name);
+
+ while (pos && (!__kernfs_active(pos) ||
+ kernfs_ns_id(READ_ONCE(pos->ns)) != kernfs_ns_id(ns))) {
+ if (read_seqcount_retry(&parent->dir.seq, seq))
+ return NULL;
+ pos = kernfs_dir_entry_after(parent, READ_ONCE(pos->hash),
+ READ_ONCE(pos->ns),
+ kernfs_rcu_name(pos));
}
return pos;
}
+/*
+ * Whether @cursor, pinned when the previous call stopped, is still the entry
+ * to list at position @hash: active, in namespace @ns, and still a child of
+ * @parent under the key it has now. Finding it under that key also shows
+ * that the key was read consistently: a rename changes the key only while
+ * the node is out of the tree, and putting it back changes @parent->dir.seq.
+ * The namespace is compared first, so that only @ns is dereferenced: a
+ * renamed cursor's namespace may be gone.
+ */
+static bool kernfs_dir_cursor_valid(struct kernfs_node *parent,
+ struct kernfs_node *cursor,
+ const struct ns_common *ns, loff_t hash)
+{
+ unsigned int cursor_hash = READ_ONCE(cursor->hash);
+
+ return __kernfs_active(cursor) && cursor_hash == hash &&
+ READ_ONCE(cursor->ns) == ns &&
+ kernfs_dir_find_key(parent, cursor_hash, ns,
+ kernfs_rcu_name(cursor)) == cursor;
+}
+
+/*
+ * Lockless attempts at a step before kernfs_fop_readdir() takes kernfs_rwsem
+ * to stop the directory from changing.
+ */
+#define KERNFS_READDIR_TRIES 3
+
static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx)
{
+ char *buf1 __free(kfree) = NULL;
+ char *buf2 __free(kfree) = NULL;
struct dentry *dentry = file->f_path.dentry;
struct kernfs_node *parent = kernfs_dentry_node(dentry);
- struct kernfs_node *pos = file->private_data;
- char *name __free(kfree) = NULL;
- struct kernfs_root *root;
+ struct kernfs_node *cursor = file->private_data;
const struct ns_common *ns = NULL;
+ unsigned int key_hash = 0;
+ char *name, *key = NULL;
if (!dir_emit_dots(file, ctx))
return 0;
/*
- * One buffer for the call, holding the name of the entry the listing
- * is on. PATH_MAX: kernfs bounds no single name.
+ * Two buffers: the name of the entry found, and the name of the entry
+ * before it, which is where the search resumes. PATH_MAX: kernfs
+ * bounds no single name.
*/
- name = kmalloc(PATH_MAX, GFP_KERNEL);
- if (!name)
+ buf1 = kmalloc(PATH_MAX, GFP_KERNEL);
+ buf2 = kmalloc(PATH_MAX, GFP_KERNEL);
+ if (!buf1 || !buf2)
return -ENOMEM;
+ name = buf1;
- root = kernfs_root(parent);
- down_read(&root->kernfs_rwsem);
-
- if (kernfs_ns_enabled(parent))
+ /*
+ * KERNFS_NS is set before @parent has children and never cleared. The
+ * other bits in the word can change under us, as in
+ * kernfs_dop_revalidate().
+ */
+ if (data_race(kernfs_ns_enabled(parent)))
ns = kernfs_info(dentry->d_sb)->ns;
- for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos, NULL, NULL);
- pos;
- pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos, name)) {
- unsigned int type = fs_umode_to_dtype(pos->mode);
- ino_t ino = kernfs_ino(pos);
- int len;
+ /* the cursor's reference belongs to this call now */
+ file->private_data = NULL;
+
+ while (ctx->pos < INT_MAX) {
+ unsigned int pos_hash = 0, type = 0, seq;
+ struct kernfs_node *pos;
+ ino_t ino = 0;
+ bool pinned;
+ int len = 0, tries;
/*
- * The copy is also the resume key, so a truncated name would
- * resume here again. getname() caps a path, so only an
- * in-kernel caller can get here; end the listing instead.
+ * Find the entry and copy everything that is listed or resumed
+ * from in one read section, so that a rename in between cannot
+ * make the listing skip or repeat entries. If the directory
+ * keeps changing, stop it with kernfs_rwsem, so that the listing
+ * makes progress.
*/
- len = strscpy(name, kernfs_rcu_name(pos), PATH_MAX);
- if (WARN_ON_ONCE(len < 0))
- break;
+ rcu_read_lock();
+ for (tries = 0; ; tries++) {
+ if (tries == KERNFS_READDIR_TRIES) {
+ rcu_read_unlock();
+ down_read(&parent->dir.root->kernfs_rwsem);
+ rcu_read_lock();
+ }
+ seq = read_seqcount_begin(&parent->dir.seq);
+ if (cursor &&
+ kernfs_dir_cursor_valid(parent, cursor, ns, ctx->pos))
+ pos = cursor;
+ else if (key)
+ pos = kernfs_dir_visible_after(parent, ns, key_hash,
+ key, seq);
+ else
+ pos = kernfs_dir_visible_after(parent, ns, ctx->pos,
+ "", seq);
+ if (pos) {
+ len = strscpy(name, kernfs_rcu_name(pos), PATH_MAX);
+ ino = kernfs_ino(pos);
+ type = fs_umode_to_dtype(READ_ONCE(pos->mode));
+ pos_hash = READ_ONCE(pos->hash);
+ }
+ if (!read_seqcount_retry(&parent->dir.seq, seq))
+ break;
+ }
- ctx->pos = pos->hash;
- file->private_data = pos;
- kernfs_get(pos);
+ /* a node whose count is zero is unlinked and being freed */
+ pinned = pos && (pos == cursor || atomic_inc_not_zero(&pos->count));
+ rcu_read_unlock();
+ if (tries >= KERNFS_READDIR_TRIES)
+ up_read(&parent->dir.root->kernfs_rwsem);
+
+ /* the cursor's reference is now @pos's, or no longer needed */
+ if (cursor && pos != cursor)
+ kernfs_put(cursor);
+ cursor = NULL;
+ if (!pos)
+ break;
/*
- * dir_emit() can fault, so run it unlocked. @pos is pinned
- * above and kernfs_dir_pos() rechecks it on the way back.
+ * The name is also where the next search resumes, so a truncated
+ * one would resume here again. getname() caps a path, so only an
+ * in-kernel caller can get here; end the listing instead.
*/
- up_read(&root->kernfs_rwsem);
- if (!dir_emit(ctx, name, len, ino, type))
+ if (WARN_ON_ONCE(len < 0)) {
+ if (pinned)
+ kernfs_put(pos);
+ break;
+ }
+
+ /* resume after @pos, whose name is in @name */
+ key = name;
+ key_hash = pos_hash;
+ name = name == buf1 ? buf2 : buf1;
+
+ /* @pos was removed meanwhile: go on after it */
+ if (!pinned)
+ continue;
+
+ /* dir_emit() can fault, so run it unlocked; @pos is pinned */
+ ctx->pos = pos_hash;
+ file->private_data = pos;
+ if (!dir_emit(ctx, key, len, ino, type))
return 0;
- down_read(&root->kernfs_rwsem);
+ file->private_data = NULL;
+ kernfs_put(pos);
}
- up_read(&root->kernfs_rwsem);
- file->private_data = NULL;
+ /* the loop did not run if the position was already past the end */
+ kernfs_put(cursor);
ctx->pos = INT_MAX;
return 0;
}
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-12-04 21:11 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH 2/3] kernfs: add a sequence count to directories Shakeel Butt
2026-12-04 21:10 ` [PATCH 3/3] kernfs: list directories without kernfs_rwsem Shakeel Butt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox