* [PATCH] kernfs: clear i_nlink before deactivating removed nodes
@ 2026-11-20 20:59 Christian Brauner
2026-11-21 1:21 ` Shakeel Butt
0 siblings, 1 reply; 2+ messages in thread
From: Christian Brauner @ 2026-11-20 20:59 UTC (permalink / raw)
To: linux-fsdevel
Cc: Greg Kroah-Hartman, Tejun Heo, T.J. Mercier, driver-core,
Shakeel Butt, Jori Koolstra, Ian Kent, Meta kernel team, stable,
Christian Brauner (Amutable)
__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
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] kernfs: clear i_nlink before deactivating removed nodes
2026-11-20 20:59 [PATCH] kernfs: clear i_nlink before deactivating removed nodes Christian Brauner
@ 2026-11-21 1:21 ` Shakeel Butt
0 siblings, 0 replies; 2+ messages in thread
From: Shakeel Butt @ 2026-11-21 1:21 UTC (permalink / raw)
To: Christian Brauner
Cc: linux-fsdevel, Greg Kroah-Hartman, Tejun Heo, T.J. Mercier,
driver-core, Jori Koolstra, Ian Kent, Meta kernel team, stable
On Fri, Nov 20, 2026 at 09:59:59PM +0100, Christian Brauner wrote:
> __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>
Reviewed-by: Shakeel Butt <shakeel.butt@linux.dev>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-21 1:21 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20 20:59 [PATCH] kernfs: clear i_nlink before deactivating removed nodes Christian Brauner
2026-11-21 1:21 ` Shakeel Butt
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.