[PATCH 1/2] kernfs: deactivate a new node before publishing its id
From: Josef Bacik
Date: Wed Dec 02 2026 - 11:50:28 EST
kernfs_find_and_get_node_by_id() can return a node that is still being
initialized, although it must fail for one that was never activated.
Commit c25491747b21 ("kernfs: Add KERNFS_REMOVING flags") made it test
kn->active instead of KERNFS_ACTIVATED. The flag is clear in a zeroed
node, but an active count of zero reads as active, and
__kernfs_new_node() only sets KN_DEACTIVATED_BIAS after the node is in
the idr and has its reference count.
Fix by setting kn->active before the node is added to the idr.
Fixes: c25491747b21 ("kernfs: Add KERNFS_REMOVING flags")
Signed-off-by: Josef Bacik <josef@xxxxxxxx>
---
fs/kernfs/dir.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
index 800af10d9511e..9569588aac751 100644
--- a/fs/kernfs/dir.c
+++ b/fs/kernfs/dir.c
@@ -668,6 +668,9 @@ static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
if (!kn)
goto err_out1;
+ /* must be set before the idr publishes @kn to lookups by id */
+ atomic_set(&kn->active, KN_DEACTIVATED_BIAS);
+
idr_preload(GFP_KERNEL);
spin_lock(&root->kernfs_idr_lock);
ret = idr_alloc_cyclic(&root->ino_idr, kn, 1, 0, GFP_ATOMIC);
@@ -683,7 +686,6 @@ static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
kn->id = (u64)id_highbits << 32 | ret;
atomic_set(&kn->count, 1);
- atomic_set(&kn->active, KN_DEACTIVATED_BIAS);
RB_CLEAR_NODE(&kn->rb);
rcu_assign_pointer(kn->name, name);