[PATCH v8 08/12] kernfs: hold kernfs_iattr_rwsem when initializing a new inode

From: Jori Koolstra

Date: Sat Oct 24 2026 - 17:35:31 EST


kernfs_refresh_inode() copies the uid, gid, timestamps and nlink of a
kernfs_node into its VFS inode. Since commit 9caf69614225 ("kernfs:
Introduce separate rwsem to protect inode attributes.") these are
protected by kernfs_iattr_rwsem. kernfs_init_inode() calls
kernfs_refresh_inode(), but was not converted. Its callers in turn hold
kernfs_rwsem but this does not exclude a concurrent
kernfs_setattr()/kernfs_iop_setattr().

Take kernfs_iattr_rwsem for reading around the refresh in
kernfs_init_inode(). This nests inside kernfs_rwsem, which matches
kernfs_link_sibling() and kernfs_unlink_sibling().

Fixes: 9caf69614225 ("kernfs: Introduce separate rwsem to protect inode attributes.")
Signed-off-by: Jori Koolstra <jkoolstra@xxxxxxxxx>
---
fs/kernfs/inode.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/fs/kernfs/inode.c b/fs/kernfs/inode.c
index 6630b29d7c07..3854ed5725e1 100644
--- a/fs/kernfs/inode.c
+++ b/fs/kernfs/inode.c
@@ -204,7 +204,8 @@ static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)
inode->i_generation = kernfs_gen(kn);

set_default_inode_attr(inode, kn->mode);
- kernfs_refresh_inode(kn, inode);
+ scoped_guard(rwsem_read, &kernfs_root(kn)->kernfs_iattr_rwsem)
+ kernfs_refresh_inode(kn, inode);

/* initialize inode according to type */
switch (kernfs_type(kn)) {
--
2.56.0