mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] configfs: give the root directory inode its own lockdep class
@ 2026-11-17  6:53 William Chuang
  2026-11-17 15:04 ` William Chuang
  0 siblings, 1 reply; 2+ messages in thread
From: William Chuang @ 2026-11-17  6:53 UTC (permalink / raw)
  To: Breno Leitao
  Cc: Andreas Hindborg, Greg Kroah-Hartman, linux-usb, linux-fsdevel,
	linux-kernel, syzbot+c75a7db5ca6ae96ed2d4, syzkaller-bugs

Directories created with mkdir(2) and the configfs root directory share
the default i_rwsem lock class of configfs; only default groups get
their own depth-based classes in configfs_set_inode_lock_class().
Lockdep therefore treats the root inode's lock and the lock of any
user-created directory as the same lock.

That produces a false circular dependency between the USB gadget and
target subsystems, which syzbot keeps reporting:

  (3) tpg_instances_lock -> configfs i_rwsem
       usbg_make_tpg() calls configfs_depend_item_unlocked(), which
       locks the configfs root inode for a target in another subsystem
  (2) func_lock -> tpg_instances_lock     (tcm_alloc_inst)
  (1) gadget_subsys.su_mutex -> func_lock (configfs_mkdir, function_make)
  (0) configfs i_rwsem -> gadget_subsys.su_mutex
       configfs_symlink() runs with the parent directory locked, and
       uvcg_streaming_header_allow_link() takes su_mutex

The inode in (0) is a directory under the gadget subsystem; the inode in
(3) is the configfs root.  A real deadlock would need something to take
a subsystem su_mutex while holding the root inode's lock, and nothing
does:

 - the root has only ->lookup and ->setattr, so mkdir, rmdir, symlink,
   unlink and rename in the root fail in the VFS before configfs runs;
 - configfs_lookup(), configfs_setattr(), configfs_readdir(),
   configfs_dir_open() and configfs_dir_close() take no subsystem lock;
 - configfs_register_subsystem(), configfs_unregister_subsystem(),
   configfs_depend_item() and configfs_depend_item_unlocked() lock the
   root but take no su_mutex while holding it.

Give the root inode its own class so that lockdep keeps the two apart.
This changes nothing without CONFIG_LOCKDEP.

Tested in QEMU (x86_64, PROVE_LOCKING) at 666539eec with an initramfs
that runs the three operations above (a UVC streaming header symlink,
mkdir functions/tcm.0, mkdir target/usb_gadget/naa.*/tpgt_1), then
readdir, chmod and mkdir on the configfs root, and tears everything
down again.  Without this patch lockdep reports the cycle above; with
it there is no report and every step gives the same result.

With the patch, the configfs kselftest passes (25/25, including its
symlink-versus-rmdir race test) with no lockdep report, as it does
without it.  Three processes looping for 60 seconds over tpgt_1
mkdir/rmdir (which locks the configfs root through
configfs_depend_item_unlocked()), the UVC symlink/unlink, and readdir
plus chmod of the configfs root never stalled, with or without the
patch; without it, lockdep reports the cycle during that run.  Without
CONFIG_LOCKDEP the patch changes no code: fs/configfs/mount.o
disassembles identically before and after.  It builds cleanly with
W=1.  Not tested: syzbot's own C reproducer.

Fixes: 4bb8548df632 ("usb: gadget: f_tcm: add configfs support")
Reported-by: syzbot+c75a7db5ca6ae96ed2d4@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=c75a7db5ca6ae96ed2d4
Signed-off-by: William Chuang <founder@logarcheon.com>
---
 fs/configfs/mount.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/fs/configfs/mount.c b/fs/configfs/mount.c
index d8cac1cbf..d20d2fd1e 100644
--- a/fs/configfs/mount.c
+++ b/fs/configfs/mount.c
@@ -58,6 +58,15 @@ static struct configfs_dirent configfs_root = {
 	.s_iattr	= NULL,
 };
 
+/*
+ * configfs_depend_item_unlocked() takes the root inode's lock while a
+ * subsystem's own locks may be held, but nothing takes a subsystem lock
+ * while holding the root inode's lock.  Directories created by mkdir(2)
+ * share the default i_rwsem class and are locked before their subsystem's
+ * su_mutex, so give the root inode a class of its own.
+ */
+static struct lock_class_key configfs_root_inode_class;
+
 static int configfs_fill_super(struct super_block *sb, struct fs_context *fc)
 {
 	struct inode *inode;
@@ -74,6 +83,7 @@ static int configfs_fill_super(struct super_block *sb, struct fs_context *fc)
 	if (inode) {
 		inode->i_op = &configfs_root_inode_operations;
 		inode->i_fop = &configfs_dir_operations;
+		lockdep_set_class(&inode->i_rwsem, &configfs_root_inode_class);
 		/* directory inodes start off with i_nlink == 2 (for "." entry) */
 		inc_nlink(inode);
 	} else {
-- 
2.43.0



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] configfs: give the root directory inode its own lockdep class
  2026-11-17  6:53 [PATCH] configfs: give the root directory inode its own lockdep class William Chuang
@ 2026-11-17 15:04 ` William Chuang
  0 siblings, 0 replies; 2+ messages in thread
From: William Chuang @ 2026-11-17 15:04 UTC (permalink / raw)
  To: Breno Leitao
  Cc: Andreas Hindborg, Greg Kroah-Hartman, linux-usb, linux-fsdevel,
	linux-kernel, syzbot+c75a7db5ca6ae96ed2d4, syzkaller-bugs

On Tue, Nov 17, 2026, Sashiko AI review wrote:
> Does assigning a separate lockdep class to the configfs root inode's
> i_rwsem hide a real ABBA deadlock between renameat2() and mkdir()?

Yes, and the deadlock is real.  Please drop this patch.

My analysis missed rename(): lock_rename() locks both parent
directories before vfs_rename() finds that configfs has no ->rename(),
and when one parent is the configfs root, the root is locked first.
mkdir() of target/usb_gadget/naa.<wwn>/tpgt_1 runs with the naa.<wwn>
directory locked by the VFS, and usbg_make_tpg() then locks the root in
configfs_depend_item_unlocked().  The two orders are opposite.

Reproduced on mainline 666539eec without this patch, in QEMU (x86_64,
2 CPUs): one process loops mkdir/rmdir of tpgt_1, another loops

  rename("/sys/kernel/config/nonexistent",
         "/sys/kernel/config/target/usb_gadget/naa.<wwn>/nonexistent")

Both hang within about a second.  sysrq-w shows:

  mkdir:  down_write
          configfs_depend_item_unlocked
          usbg_make_tpg
          target_fabric_make_tpg
          configfs_mkdir
          vfs_mkdir

  rename: down_write_nested
          lock_two_directories
          __start_renaming
          filename_renameat2

A third process doing readdir() of the configfs root then blocks in
configfs_dir_open().  I ran the test as root; I have not checked how
much privilege the rename() side needs.

So the root lock that configfs_depend_item_unlocked() takes for a
target in another subsystem, when called from a ->make_group()
callback, can deadlock against rename() whatever the su_mutex chain in
the syzbot report.  I don't have a fix to propose yet.  Is the root
lock there only to keep the target subsystem from being unregistered
during the dependency, or does it protect more than that?

Thanks,
William


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-17 15:04 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-17  6:53 [PATCH] configfs: give the root directory inode its own lockdep class William Chuang
2026-11-17 15:04 ` William Chuang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®