[PATCH 0/3] kernfs: do less work under the kernfs_rwsem write lock

Shakeel Butt posted 3 patches 1 week, 5 days ago
fs/kernfs/dir.c | 32 ++++++++++++++++++++++++++------
1 file changed, 26 insertions(+), 6 deletions(-)
[PATCH 0/3] kernfs: do less work under the kernfs_rwsem write lock
Posted by Shakeel Butt 1 week, 5 days ago
kernfs_rwsem is per kernfs root, so sysfs and cgroupfs have one each for
the whole machine, and every create, remove and rename takes it for
writing.  Whatever a writer does while holding it, everyone else waits
for.  These three patches take work out of that section.

Patch 1: kernfs_add_one() links the node in, drops the lock, then calls
kernfs_activate(), which takes it again.  sysfs does not create nodes
deactivated, so it pays two write locks for every file and directory.
Activate before dropping the lock.  lock_stat says 1200 -> 830 write
acquisitions for creating and destroying five dummy netdevs.

Patch 2: kernfs_rename_ns() calls kstrdup_const() under the lock.  It is
the only GFP_KERNEL allocation under the write lock in kernfs, so a
rename can enter reclaim with the lock held.  Allocate before the lock.

Patch 3: kernfs_rename_ns() also frees the old name under the lock with
kfree_rcu_mightsleep().  When its batching allocation fails, which is
what happens under memory pressure, it falls back to synchronize_rcu().
A rename can then wait out a grace period with the lock held.  Free
after the unlock.

Tested with the kernfs selftests under KASAN and lockdep.

Shakeel Butt (3):
  kernfs: activate a new node without dropping kernfs_rwsem
  kernfs: allocate the new name outside kernfs_rwsem
  kernfs: free the old name outside kernfs_rwsem

 fs/kernfs/dir.c | 32 ++++++++++++++++++++++++++------
 1 file changed, 26 insertions(+), 6 deletions(-)


base-commit: 7f24d0f60b1087dbc28d68398957c18774e5a7d8
-- 
2.53.0-Meta
Re: [PATCH 0/3] kernfs: do less work under the kernfs_rwsem write lock
Posted by Tejun Heo 3 days, 16 hours ago
Sorry about the delay.

On Sat, Sep 12, 2026 at 07:14:50PM -0700, Shakeel Butt wrote:
> kernfs_rwsem is per kernfs root, so sysfs and cgroupfs have one each for
> the whole machine, and every create, remove and rename takes it for
> writing.  Whatever a writer does while holding it, everyone else waits
> for.  These three patches take work out of that section.
> 
> Patch 1: kernfs_add_one() links the node in, drops the lock, then calls
> kernfs_activate(), which takes it again.  sysfs does not create nodes
> deactivated, so it pays two write locks for every file and directory.
> Activate before dropping the lock.  lock_stat says 1200 -> 830 write
> acquisitions for creating and destroying five dummy netdevs.
> 
> Patch 2: kernfs_rename_ns() calls kstrdup_const() under the lock.  It is
> the only GFP_KERNEL allocation under the write lock in kernfs, so a
> rename can enter reclaim with the lock held.  Allocate before the lock.
> 
> Patch 3: kernfs_rename_ns() also frees the old name under the lock with
> kfree_rcu_mightsleep().  When its batching allocation fails, which is
> what happens under memory pressure, it falls back to synchronize_rcu().
> A rename can then wait out a grace period with the lock held.  Free
> after the unlock.
> 
> Tested with the kernfs selftests under KASAN and lockdep.
> 
> Shakeel Butt (3):
>   kernfs: activate a new node without dropping kernfs_rwsem
>   kernfs: allocate the new name outside kernfs_rwsem
>   kernfs: free the old name outside kernfs_rwsem

Acked-by: Tejun Heo <tj@kernel.org>

Thanks.

-- 
tejun