[PATCH 6/6] kernfs: don't take kernfs_iattr_rwsem in __kernfs_remove()
From: Shakeel Butt
Date: Wed Nov 18 2026 - 17:02:53 EST
__kernfs_remove() takes kernfs_iattr_rwsem for writing around
kernfs_clear_inode_nlink() for every removed node. A cgroup rmdir
removes the directory and all its files, so it takes the lock once per
file, each time behind the readers. Our lock monitor saw rmdirs wait
in __kernfs_remove() for several seconds.
Commit eea5d2bb34ba ("kernfs: Send IN_DELETE_SELF and IN_IGNORED")
clears i_nlink inside this section, so a refresh in progress could not
set a removed directory's i_nlink again, the race described in commit
507d8ce13f5b ("kernfs: Don't set_nlink for directories being removed").
The refresh no longer sets i_nlink. Only inode setup does, under
kernfs_rwsem, which __kernfs_remove() holds for writing.
A racing fstat() of a file can read its i_nlink while it is cleared,
and sees 1 or 0, as on other filesystems. A directory reports its link
count from the node.
Drop the lock. rmdir no longer takes kernfs_iattr_rwsem for writing.
kernfs_iattr_rwsem write acquisitions (lock_stat), before -> after:
5 dummy netdevs created and removed 370 -> 0
100 cgroup mkdirs and 100 rmdirs 1500 -> 0
Signed-off-by: Shakeel Butt <shakeel.butt@xxxxxxxxx>
---
fs/kernfs/dir.c | 2 --
1 file changed, 2 deletions(-)
diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
index 8686219b826f..c1402a461405 100644
--- a/fs/kernfs/dir.c
+++ b/fs/kernfs/dir.c
@@ -1625,9 +1625,7 @@ static void __kernfs_remove(struct kernfs_node *kn)
* to decide who's responsible for cleanups.
*/
if (!parent || kernfs_unlink_sibling(pos)) {
- down_write(&kernfs_root(kn)->kernfs_iattr_rwsem);
kernfs_clear_inode_nlink(pos);
- up_write(&kernfs_root(kn)->kernfs_iattr_rwsem);
if (parent)
kernfs_dir_update_times(parent);
--
2.53.1-Meta