From: Josef Bacik <josef@bacik.io>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Tejun Heo <tj@kernel.org>
Cc: Christian Brauner <brauner@kernel.org>,
driver-core@lists.linux.dev, linux-fsdevel@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: [PATCH 1/2] kernfs: deactivate a new node before publishing its id
Date: Wed, 02 Dec 2026 16:45:13 +0000 [thread overview]
Message-ID: <20261202-b4-kernfs-new-node-v1-1-25b4eb0468a2@bacik.io> (raw)
In-Reply-To: <20261202-b4-kernfs-new-node-v1-0-25b4eb0468a2@bacik.io>
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@bacik.io>
---
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);
next prev parent reply other threads:[~2026-12-02 16:50 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 16:45 [PATCH 0/2] kernfs: fix two races in __kernfs_new_node() Josef Bacik
2026-12-02 16:45 ` Josef Bacik [this message]
2026-12-02 16:45 ` [PATCH 2/2] kernfs: RCU free a new node that was already in the idr Josef Bacik
2026-12-03 19:25 ` [PATCH 0/2] kernfs: fix two races in __kernfs_new_node() Tejun Heo
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=20261202-b4-kernfs-new-node-v1-1-25b4eb0468a2@bacik.io \
--to=josef@bacik.io \
--cc=brauner@kernel.org \
--cc=driver-core@lists.linux.dev \
--cc=gregkh@linuxfoundation.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=tj@kernel.org \
/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
all inboxes | Powered by JetHome®