mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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 2/2] kernfs: RCU free a new node that was already in the idr
Date: Wed, 02 Dec 2026 16:45:13 +0000	[thread overview]
Message-ID: <20261202-b4-kernfs-new-node-v1-2-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 read a node after the error path of
__kernfs_new_node() has freed it.

Commit 4207b556e62f ("kernfs: RCU protect kernfs_nodes and avoid
kernfs_idr_lock in kernfs_find_and_get_node_by_id()") moved the lookup
from kernfs_idr_lock to RCU and made kernfs_put() wait for a grace
period. The error path still frees the node right after idr_remove().

Fix by freeing the node with kernfs_free_rcu() there as well.

Fixes: 4207b556e62f ("kernfs: RCU protect kernfs_nodes and avoid kernfs_idr_lock in kernfs_find_and_get_node_by_id()")
Signed-off-by: Josef Bacik <josef@bacik.io>
---
 fs/kernfs/dir.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
index 9569588aac751..257c6082f1a82 100644
--- a/fs/kernfs/dir.c
+++ b/fs/kernfs/dir.c
@@ -718,14 +718,16 @@ static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
  err_out4:
 	RCU_INIT_POINTER(kn->__parent, NULL);
 	kernfs_put(parent);
-	if (kn->iattr) {
+	if (kn->iattr)
 		simple_xattrs_free(&root->xa_cache, &kn->iattr->xattrs, NULL);
-		kmem_cache_free(kernfs_iattrs_cache, kn->iattr);
-	}
  err_out3:
 	spin_lock(&root->kernfs_idr_lock);
 	idr_remove(&root->ino_idr, (u32)kernfs_ino(kn));
 	spin_unlock(&root->kernfs_idr_lock);
+	/* a lookup by id may still see @kn, this also frees @name and iattr */
+	call_rcu(&kn->rcu, kernfs_free_rcu);
+	return NULL;
+
  err_out2:
 	kmem_cache_free(kernfs_node_cache, kn);
  err_out1:

  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 1/2] kernfs: deactivate a new node before publishing its id 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-2-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®