Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

kernfs: Don't set_nlink for directories being removed

If a directory is already in the process of removal its i_nlink count
becomes irrelevant because its contents are also about to be removed and
any pending filesystem operations on it or its contents will soon start
to fail. So we can avoid setting it for directories already flagged for
removal.

This avoids a race in the next patch, which adds clearing of the i_nlink
count for kernfs nodes being removed to support inotify delete events.

Use protection from the kernfs_iattr_rwsem to avoid adding more
contention to the kernfs_rwsem for calls to kernfs_refresh_inode.

Signed-off-by: T.J. Mercier <tjmercier@google.com>
Tested-by: syzbot@syzkaller.appspotmail.com
Link: https://patch.msgid.link/20260225223404.783173-2-tjmercier@google.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

T.J. Mercier and committed by
Greg Kroah-Hartman
507d8ce1 f917dc56

+3 -1
+2
fs/kernfs/dir.c
··· 1491 1491 pr_debug("kernfs %s: removing\n", kernfs_rcu_name(kn)); 1492 1492 1493 1493 /* prevent new usage by marking all nodes removing and deactivating */ 1494 + down_write(&kernfs_root(kn)->kernfs_iattr_rwsem); 1494 1495 pos = NULL; 1495 1496 while ((pos = kernfs_next_descendant_post(pos, kn))) { 1496 1497 pos->flags |= KERNFS_REMOVING; 1497 1498 if (kernfs_active(pos)) 1498 1499 atomic_add(KN_DEACTIVATED_BIAS, &pos->active); 1499 1500 } 1501 + up_write(&kernfs_root(kn)->kernfs_iattr_rwsem); 1500 1502 1501 1503 /* deactivate and unlink the subtree node-by-node */ 1502 1504 do {
+1 -1
fs/kernfs/inode.c
··· 178 178 */ 179 179 set_inode_attr(inode, attrs); 180 180 181 - if (kernfs_type(kn) == KERNFS_DIR) 181 + if (kernfs_type(kn) == KERNFS_DIR && !(kn->flags & KERNFS_REMOVING)) 182 182 set_nlink(inode, kn->dir.subdirs + 2); 183 183 } 184 184