Skip to contentSkip to conversation

configfs: give the root directory inode its own lockdep class

PATCH In review William Chuang 2 participants 3 messages Nov 17

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 configfs: give the root directory inode its own lockdep class

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 [email protected]
---
 fs/configfs/mount.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
fs/configfs/mount.c +10 -0
index d8cac1cbf..d20d2fd1e 100644
@@ -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 {
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] configfs: root inode lockdep class masks real rename() vs mkdir() deadlock --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] configfs: root inode lockdep class masks real rename() vs mkdir() deadlock
--

commit 058eebb48180eef94995fcd78f689f610830d617
Author: William Chuang <[email protected]>

configfs: give the root directory inode its own lockdep class

This commit assigns a dedicated lockdep class to the configfs root directory inode's i_rwsem to avoid a lockdep circular dependency report between USB gadget and target subsystems.

Quoted 21 lines
fs/configfs/mount.c +9 -0
index d8cac1cbf3bd5..d20d2fd1e1809 100644
@@ -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;

fs/configfs/mount.c static int configfs_fill_super(struct super_block *sb, struct fs_context *fc) · line 86

Quoted 4 lines
fs/configfs/mount.c +1 -0
@@ -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);

[Severity: High]
Does assigning a separate lockdep class to the configfs root inode's i_rwsem hide a real ABBA deadlock between renameat2() and mkdir()?

In filename_renameat2(), __start_renaming() -> lock_rename() -> lock_two_directories() runs before vfs_rename() checks old_dir->i_op->rename. When one parent directory is the configfs root and the other is a descendant directory (such as target/usb_gadget/naa.*), lock_two_directories() sees that the root is an ancestor and locks root_inode->i_rwsem before the descendant directory's i_rwsem:

fs/namei.c:lock_two_directories() {
	...
	r = d_ancestor(p2, p1);
	if (r) {
		inode_lock_nested(p2->d_inode, I_MUTEX_PARENT);
		inode_lock_nested(p1->d_inode, I_MUTEX_PARENT2);
		return r;
	}
}

Meanwhile, mkdir() on target/usb_gadget/naa.*/tpgt_1 locks those same two inodes in the opposite order:

CPU0 CPU1
---- ----
lock(&naa_inode->i_rwsem)

                                      lock(&root_inode->i_rwsem)
                                      lock(&naa_inode->i_rwsem)

lock(&root_inode->i_rwsem)

*** DEADLOCK ***

CPU0: do_mkdirat() -> configfs_mkdir() -> target_fabric_make_tpg() ->

      usbg_make_tpg() -> configfs_depend_item_unlocked()
CPU1: do_renameat2() -> filename_renameat2() -> __start_renaming() ->
      lock_rename() -> lock_two_directories()

Splitting root_inode->i_rwsem into configfs_root_inode_class prevents lockdep from detecting this lock ordering inversion until a rename() actually races with mkdir() or symlink().

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to William Chuang
Quoted 2 lines · Sashiko AI review

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