kernfs: allocate inodes and dentry outside kernfs_rwsem
Subsystems (3)
PATCH v2 1/1 kernfs: allocate inodes and dentry outside kernfs_rwsem
In the Meta fleet, on 6.16 with the kernfs changes queued for 7.4 backported, we still see long cgroupfs stalls:
- oomd and below waited 72 seconds in kernfs_fop_readdir() while an out of memory container created cgroups. They resumed when the kernel OOM killer killed the container's workload.
- oomd waited 15 minutes in kernfs_iop_lookup(). PID 1 waited for kernfs_rwsem in cgroup_rmdir() while holding cgroup_mutex.
- A lock profiler sampling part of the fleet saw kernfs_iop_lookup() hold kernfs_rwsem across memcg reclaim on 26 hosts in 29 days, for up to 625 ms.
The profiler shows where these stalls come from. kernfs_iop_lookup() and the export ops allocate a new inode while holding kernfs_rwsem for read. The inode is charged to the caller's memcg. If that memcg is at memory.max, the allocation can stay in reclaim for a long time, sometimes until an OOM kill. A writer then queues on the lock and every new reader queues behind it. On cgroup2 this blocks every cgroupfs user on the machine.
Such lookups are common in a memcg that is out of memory. Its reclaim drops its kernfs dentries fast, and the files of a new cgroup have no inodes yet.
Allocate the inode with the lock dropped. kernfs_iget() first looks for an existing inode. If there is none, it pins the node, drops the lock, allocates, takes the lock again, and adds the inode only if the node is still active. So __kernfs_remove() still finds every inode it must clear i_nlink on, as commit dc1a0cc95737 ("kernfs: don't lose IN_DELETE_SELF when decoding a file handle") needs.
kernfs_get_parent_dentry() also held the lock across d_obtain_alias(), which allocates a dentry. Hold it only around kernfs_iget(), as __kernfs_fh_to_dentry() already does.
Tested on a 176-CPU machine. A task in a full 256 MB memcg with slow swap looks up the files of new cgroups. Worst waits in 60 seconds:
before after
oomd, for kernfs_rwsem 577 ms 0.7 ms below, for kernfs_rwsem 412 ms 1.0 ms systemd, for kernfs_rwsem 449 ms 17 us
readdir of another directory 1154 ms 3.2 ms
The lookup itself is still slow. The VFS still holds its directory's lock, so other work in that one directory still waits. Cold sysfs and cgroupfs walks are not slower.
kernfs_fill_super() still allocates the root inode under the lock. It runs once per superblock.
---
Changes since v1:
- dentry allocation outside kernfs_rwsem (Sashiko).
- Couple of comments updated.
fs/kernfs/dir.c | 11 +++--- fs/kernfs/inode.c | 69 +++++++++++++++++++++++++++++++++++++ fs/kernfs/kernfs-internal.h | 2 ++ fs/kernfs/mount.c | 18 +++++----- 4 files changed, 88 insertions(+), 12 deletions(-)
fs/kernfs/dir.c +7 -4
@@ -37,7 +37,7 @@ static bool __kernfs_active(struct kernfs_node *kn)return atomic_read (&kn->active) >=0 ; } -static bool kernfs_active (struct kernfs_node *kn) +bool kernfs_active (struct kernfs_node *kn) {lockdep_assert_held (&kernfs_root (kn)->kernfs_rwsem);return __kernfs_active (kn);
@@ -1269,9 +1269,12 @@ static struct dentry *kernfs_iop_lookup(struct inode *dir,up_read (&root->kernfs_rwsem);return NULL ; } - inode =kernfs_get_inode (dir->i_sb, kn); -if (!inode) - inode =ERR_PTR (-ENOMEM ); + inode =kernfs_iget (dir->i_sb, kn); +if (!inode) { +/* deactivated while kernfs_iget() dropped the lock */ +up_read (&root->kernfs_rwsem); +return NULL ; + } }/* * Needed for negative dentry validation.
fs/kernfs/inode.c +69 -0
75 lines not on this page · raw message
fs/kernfs/kernfs-internal.h +2 -0
@@ -186,6 +186,7 @@ int kernfs_iop_getattr(struct mnt_idmap *idmap, u32 request_mask, unsigned int query_flags);ssize_t kernfs_iop_listxattr (struct dentry *dentry,char *buf,size_t size);int __kernfs_setattr (struct kernfs_node *kn,const struct iattr *iattr); +struct inode *kernfs_iget (struct super_block *sb,struct kernfs_node *kn);/* * dir.c
@@ -194,6 +195,7 @@ extern const struct dentry_operations kernfs_dops;extern const struct file_operations kernfs_dir_fops;extern const struct inode_operations kernfs_dir_iops; +bool kernfs_active (struct kernfs_node *kn);struct kernfs_node *kernfs_get_active (struct kernfs_node *kn);void kernfs_put_active (struct kernfs_node *kn);int kernfs_add_one (struct kernfs_node *kn);
fs/kernfs/mount.c +10 -8
@@ -125,11 +125,9 @@ static struct dentry *__kernfs_fh_to_dentry(struct super_block *sb, }/* -* Hold kernfs_rwsem across the lookup as well as kernfs_get_inode(). -* __kernfs_remove() deactivates the subtree and clears i_nlink on its -* inodes under the write lock, so under the read lock either -* kernfs_find_and_get_node_by_id() refuses the node, or the inode is -* in the inode hash before the ilookup() pass goes looking for it. +* Hold kernfs_rwsem from the lookup through kernfs_iget(). Then +* __kernfs_remove() either finds the inode to clear i_nlink, or the +* node is already inactive and no inode is created. */ scoped_guard(rwsem_read, &info->root->kernfs_rwsem) { kn =kernfs_find_and_get_node_by_id (info->root, id);
@@ -146,7 +144,7 @@ static struct dentry *__kernfs_fh_to_dentry(struct super_block *sb,return ERR_PTR (-ESTALE ); } - inode =kernfs_get_inode (sb, kn); + inode =kernfs_iget (sb, kn);kernfs_put (kn); }
@@ -171,9 +169,13 @@ static struct dentry *kernfs_get_parent_dentry(struct dentry *child) {struct kernfs_node *kn =kernfs_dentry_node (child);struct kernfs_root *root =kernfs_root (kn); +struct inode *inode; + + scoped_guard(rwsem_read, &root->kernfs_rwsem) { + inode =kernfs_iget (child->d_sb,kernfs_parent (kn)); + } -guard (rwsem_read)(&root->kernfs_rwsem); -return d_obtain_alias (kernfs_get_inode (child->d_sb,kernfs_parent (kn))); +return d_obtain_alias (inode); }static const struct export_operations kernfs_export_ops = {
Signature · 1 line
2.53.0-Meta
Tejun Heo
maintainerHello, Shakeel.
Quoted 6 lines · Shakeel Butt
Allocate the inode with the lock dropped.
kernfs_iget()first looks for an existing inode. If there is none, it pins the node, drops the lock, allocates, takes the lock again, and adds the inode only if the node is still active. So__kernfs_remove()still finds every inode it must cleari_nlinkon, as commitdc1a0cc95737("kernfs: don't loseIN_DELETE_SELFwhen decoding a file handle") needs.
Would it be simpler to allocate the inode before taking kernfs_rwsem and free it if it ends up unused, as 8b64f32fee95 ("kernfs: allocate the new name outside kernfs_rwsem") and 5ad03af18434 ("kernfs: allocate the open node outside the open file mutex") do? Then the lock doesn't have to be dropped, and there's no need for the pin and the active recheck. The unused cases (negative lookups, inactive nodes, nodes which already have an inode) are exceptions, and an inode alloc and free isn't expensive.
The helper would then just run under kernfs_rwsem. Maybe kernfs_get_inode_locked() would be a better name for it?
kernfs_fill_super() can preallocate the same way, which gets rid of the last inode allocation under kernfs_rwsem. cgroup_may_write() allocates the inode under cgroup_mutex and could allocate before grabbing the mutex too.
Quoted 3 lines
kernfs_get_parent_dentry()also held the lock acrossd_obtain_alias(), which allocates a dentry. Hold it only aroundkernfs_iget(), as__kernfs_fh_to_dentry()already does.
This also adds the active test to ->get_parent. A parent which is being removed and has no inode now gets -ESTALE instead of a new inode with i_nlink 1, which closes the window dc1a0cc95737 ("kernfs: don't lose IN_DELETE_SELF when decoding a file handle") left open. Can you mention it in the description?
Thanks.
Signature · 1 line
tejun
Quoted 10 lines · Tejun Heo
Hello, Shakeel.
Quoted 6 lines · Shakeel Butt
Allocate the inode with the lock dropped.
kernfs_iget()first looks for an existing inode. If there is none, it pins the node, drops the lock, allocates, takes the lock again, and adds the inode only if the node is still active. So__kernfs_remove()still finds every inode it must cleari_nlinkon, as commitdc1a0cc95737("kernfs: don't loseIN_DELETE_SELFwhen decoding a file handle") needs.
Quoted 7 lines
Would it be simpler to allocate the inode before taking
kernfs_rwsemand free it if it ends up unused, as8b64f32fee95("kernfs: allocate the new name outsidekernfs_rwsem") and5ad03af18434("kernfs: allocate the open node outside the open file mutex") do? Then the lock doesn't have to be dropped, and there's no need for the pin and the active recheck. The unused cases (negative lookups, inactive nodes, nodes which already have an inode) are exceptions, and an inode alloc and free isn't expensive.
Thanks for taking a look and yes, it is much better and simpler.
Quoted 3 lines
The helper would then just run under
kernfs_rwsem. Maybekernfs_get_inode_locked()would be a better name for it?
Ack
Quoted 3 lines
kernfs_fill_super()can preallocate the same way, which gets rid of the last inode allocation underkernfs_rwsem.
Ack
Quoted 3 lines
cgroup_may_write()allocates
the inode undercgroup_mutexand could allocate before grabbing the mutex too.
Let me send this one as a separate patch.
Quoted 10 lines
Quoted 4 lines
kernfs_get_parent_dentry()also held the lock acrossd_obtain_alias(), which allocates a dentry. Hold it only aroundkernfs_iget(), as__kernfs_fh_to_dentry()already does.This also adds the active test to ->get_parent. A parent which is being removed and has no inode now gets
-ESTALEinstead of a new inode withi_nlink1, which closes the windowdc1a0cc95737("kernfs: don't loseIN_DELETE_SELFwhen decoding a file handle") left open. Can you mention it in the description?
Yes, I will do.
Thanks again for the review.