Re: [PATCH RFC] configfs: don't lock the root inode in configfs_depend_item_unlocked()

From: Greg KH

Date: Sun Nov 29 2026 - 07:16:01 EST


On Sun, Nov 29, 2026 at 10:02:18AM +0900, MacchaCat via B4 Relay wrote:
> From: MacchaCat <macchacat@xxxxx>
>
> configfs_depend_item_unlocked() is meant to be called from configfs
> callbacks. Directory operation callbacks such as ->make_group() run
> with a directory inode locked by the VFS. When the target item lives in
> another subsystem, configfs_depend_item_unlocked() additionally locks
> the configfs root inode, so that the target subsystem cannot be
> unregistered while it is looked up and the dependency is taken.
>
> >From such a callback, that nests the root inode lock inside a directory
> inode lock. A rename() whose two parent directories are the configfs
> root and a directory below it nests them the other way round:
> lock_rename() locks both parents before the last components are looked
> up and before vfs_rename() gets to fail, and lock_two_directories()
> locks the ancestor first. f_tcm runs into this on mkdir of
> target/usb_gadget/<wwn>/tpgt_N: configfs_mkdir() calls the
> ->make_group() callback, target_fabric_make_tpg(), with the VFS still
> holding the <wwn> directory lock, and its usbg_make_tpg() depends on the
> tcm function instance under usb_gadget/:
>
> mkdir(.../naa.X/tpgt_1) rename(".../config/a",
> ".../naa.X/a")
> inode_lock(naa.X) [VFS]
> lock_two_directories()
> inode_lock(root)
> inode_lock(naa.X)
> usbg_make_tpg()
> configfs_depend_item_unlocked()
> inode_lock(root)
>
> Sashiko pointed this inversion out in review, and William Chuang
> reproduced the hang: one process looping over mkdir/rmdir of tpgt_1 and
> another looping over rename() attempts like the one above (the source
> does not even exist) both hang within about a second. Giving the root
> inode its own lockdep class makes lockdep stop reporting the cycle, but
> the deadlock is still there. With that class split applied, the same
> two loops stop after the first mkdir of tpgt_1, lockdep reports
> nothing, and only the hung task detector notices:
>
> INFO: task rename_vs_mkdir:92 blocked for more than 30 seconds.
> down_write+0x1ae/0x1e0
> configfs_depend_item_unlocked+0xd8/0x5c0
> usbg_make_tpg+0x1f5/0x650
> target_fabric_make_tpg+0xa6/0x7f0
> configfs_mkdir+0x4ea/0xfe0
> vfs_mkdir+0x2b7/0x920
> INFO: task rename_vs_mkdir:93 blocked for more than 30 seconds.
> down_write_nested+0x1bd/0x1f0
> lock_two_directories+0x186/0x330
> __start_renaming+0xff/0x4e0
> filename_renameat2+0x5bf/0x920
>
> The root inode lock is not needed for what it protects here.
> Subsystem directories are added to and removed from the root's
> s_children under configfs_dirent_lock. A subsystem's dirent carries
> CONFIGFS_USET_CREATING until configfs_register_subsystem() has attached
> it, and configfs_unregister_subsystem() marks the subsystem's fragment
> dead before it takes the root inode lock, and later gives the dirent
> CONFIGFS_USET_DROPPING in configfs_detach_prep() under
> configfs_dirent_lock. So look the target subsystem up, check that it is
> neither being registered nor being unregistered, and take the
> dependency, all in one configfs_dirent_lock section, and don't take the
> root inode lock at all.
>
> The one s_type update of a subsystem dirent that was only covered by
> the root inode lock, setting CONFIGFS_USET_DIR in
> configfs_attach_group(), happens after the dirent is already on the
> root's s_children, so move it under configfs_dirent_lock too. frag_dead
> is now read without frag_sem here, so write it with WRITE_ONCE() in
> configfs_unregister_subsystem().
>
> A depend that used to wait for a concurrent register of the target
> subsystem now fails with -ENOENT while the subsystem is not on the
> root's s_children yet or still marked CONFIGFS_USET_CREATING; once
> configfs_dir_set_ready() has cleared that, it goes on to the usual scan
> for the target. One that races with unregister fails with -ENOENT once
> it observes the fragment marked dead or the dirent marked
> CONFIGFS_USET_DROPPING. As before, unregister marks the fragment dead
> before it takes the root inode lock.
> configfs_unregister_subsystem() still does not look at
> s_dependent_count; that is unchanged.
>
> The same-subsystem case and configfs_depend_item() are unchanged.
>
> Claude Opus 5.5 (Claude Code) was used to analyse the problem and to
> write the code, this changelog and the test programs. The result was
> reviewed by me and cross-checked with GPT-6.2-Sol and Gemini 4 Carbon.
>
> Fixes: d79d75b5c518 ("fs: configfs: Add unlocked version of configfs_depend_item()")
> Reported-by: William Chuang <founder@xxxxxxxxxxxxxx>
> Closes: https://lore.kernel.org/all/20261117150358.8149-1-founder@xxxxxxxxxxxxxx/
> Reported-by: syzbot+8c0a48f6c8357c9da819@xxxxxxxxxxxxxxxxxxxxxxxxx
> Closes: https://syzkaller.appspot.com/bug?extid=8c0a48f6c8357c9da819
> Signed-off-by: MacchaCat <macchacat@xxxxx>

Real name please.