From: Christian Brauner <brauner@kernel.org>
To: linux-fsdevel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Tejun Heo <tj@kernel.org>, "T.J. Mercier" <tjmercier@google.com>,
driver-core@lists.linux.dev,
Shakeel Butt <shakeel.butt@linux.dev>,
Jori Koolstra <jkoolstra@xs4all.nl>, Ian Kent <raven@themaw.net>,
Meta kernel team <kernel-team@meta.com>,
stable@vger.kernel.org,
"Christian Brauner (Amutable)" <brauner@kernel.org>
Subject: [PATCH] kernfs: clear i_nlink before deactivating removed nodes
Date: Fri, 20 Nov 2026 21:59:59 +0100 [thread overview]
Message-ID: <20261120-work-kernfs-delete_self-v1-1-514a35d04431@kernel.org> (raw)
__kernfs_remove() deactivates the whole subtree first and only clears
i_nlink on a node's inodes later, once it unlinks the node from its
parent. In between kernfs_dop_revalidate() already fails for the node,
so a lookup that runs into one of its cached dentries kills the dentry
while i_nlink is still 1. For a file of a cgroup that is being removed:
rmdir("cg") stat("cg/cgroup.events")
css_clear_dir()
__kernfs_remove(cgroup.events)
deactivate the node
kernfs_dop_revalidate() == 0
d_invalidate()
dput()
dentry_unlink_inode()
i_nlink == 1, no IN_DELETE_SELF
kernfs_unlink_sibling()
kernfs_clear_inode_nlink()
i_nlink == 0, but the inode has no dentry left
The inotify watch keeps the inode alive, so nothing ever sends
IN_DELETE_SELF for it and the watch only goes away once the watcher
closes the inotify fd.
The window covers everything between the two loops in
__kernfs_remove(). kernfs_drain() drops kernfs_rwsem and waits for the
active references and open files of the node, and the clear itself
waits for kernfs_iattr_rwsem behind every concurrent permission check
and stat(). In a VM with a few threads stat()ing a cgroup and one of
its files while the cgroup is removed, IN_DELETE_SELF for the watched
file got lost in 16 to 33 out of 150 rmdirs, and it didn't show up even
after dropping the dcache.
So clear i_nlink in the loop that marks the subtree as being removed,
before a node turns inactive. kernfs_rwsem is held for writing there, so
no new inode can show up for these nodes while the loop runs, and once
a node is inactive kernfs_get_inode_locked() refuses to attach one. Two
overlapping removals may clear the same inode twice, which is fine.
Fixes: eea5d2bb34ba ("kernfs: Send IN_DELETE_SELF and IN_IGNORED")
Cc: stable@vger.kernel.org # v7.1+
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
fs/kernfs/dir.c | 12 +++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)
diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
index fb1dcf35d900d..aab5f5f12036d 100644
--- a/fs/kernfs/dir.c
+++ b/fs/kernfs/dir.c
@@ -1604,10 +1604,18 @@ static void __kernfs_remove(struct kernfs_node *kn)
pr_debug("kernfs %s: removing\n", kernfs_rcu_name(kn));
- /* prevent new usage by marking all nodes removing and deactivating */
+ /*
+ * Prevent new usage by marking all nodes removing and deactivating.
+ *
+ * Clear i_nlink before a node turns inactive. From then on
+ * kernfs_dop_revalidate() fails and a lookup can kill the node's
+ * dentries, and only a dentry dying with i_nlink at 0 sends
+ * IN_DELETE_SELF.
+ */
down_write(&kernfs_root(kn)->kernfs_iattr_rwsem);
pos = NULL;
while ((pos = kernfs_next_descendant_post(pos, kn))) {
+ kernfs_clear_inode_nlink(pos);
pos->flags |= KERNFS_REMOVING;
if (kernfs_active(pos))
atomic_add(KN_DEACTIVATED_BIAS, &pos->active);
@@ -1638,8 +1646,6 @@ static void __kernfs_remove(struct kernfs_node *kn)
down_write(&kernfs_root(kn)->kernfs_iattr_rwsem);
- kernfs_clear_inode_nlink(pos);
-
/* update timestamps on the parent */
if (ps_iattr) {
ktime_get_real_ts64(&ps_iattr->ia_ctime);
---
base-commit: ccf2a4692fed0fa77751d32d2690585127a152c5
change-id: 20261120-work-kernfs-delete_self-a1fdb64ebcce
next reply other threads:[~2026-11-20 21:00 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-20 20:59 Christian Brauner [this message]
2026-11-21 1:21 ` [PATCH] kernfs: clear i_nlink before deactivating removed nodes Shakeel Butt
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=20261120-work-kernfs-delete_self-v1-1-514a35d04431@kernel.org \
--to=brauner@kernel.org \
--cc=driver-core@lists.linux.dev \
--cc=gregkh@linuxfoundation.org \
--cc=jkoolstra@xs4all.nl \
--cc=kernel-team@meta.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=raven@themaw.net \
--cc=shakeel.butt@linux.dev \
--cc=stable@vger.kernel.org \
--cc=tj@kernel.org \
--cc=tjmercier@google.com \
/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