From: Mostafa Amin syzbot reported a circular locking dependency when unlinking a UVC gadget configfs symlink after creating a USB gadget TCM target portal group (tpgt_1): CPU0 (vfs_unlink / vfs_rmdir) CPU1 (configfs_mkdir tpgt_1) ----------------------------- ---------------------------- inode_lock(target) [i_mutex_key] mutex_lock(&target_subsys->su_mutex) usbg_make_tpg() mutex_lock(&tpg_instances_lock) configfs_depend_item_unlocked() inode_lock(configfs_root) [i_mutex_key] uvcg_streaming_class_drop_link() mutex_lock(&gadget_subsys.su_mutex) [-> func_lock -> tpg_instances_lock] configfs_depend_item_unlocked() was added specifically for configfs callbacks where VFS inode locks and caller_subsys->su_mutex are already held. However, when target belongs to a different configfs subsystem than caller_subsys (as in f_tcm's usbg_make_tpg() depending on a usb_gadget function item), it still acquires the configfs root directory inode_lock(). That inverts the VFS/configfs lock order (inode_lock -> su_mutex -> driver locks), triggering lockdep cycles both across subsystems (via tpg_instances_lock into gadget_subsys.su_mutex) and within caller_subsys itself on a subsequent configfs_rmdir(). In addition, walking target->ci_group without target's su_mutex races with concurrent mkdir()/rmdir() of target in the other subsystem, where ci_group can be NULL and cause a NULL-pointer dereference. Since caller_subsys is pinned by the active VFS callback, configfs is guaranteed mounted and caller_subsys's dentry gives direct access to both caller_sd and root_sd. Perform the subsystem lookup, CONFIGFS_USET_CREATING / CONFIGFS_USET_DROPPING validation, configfs_depend_prep() traversal, and s_dependent_count increment atomically under configfs_dirent_lock without taking the root inode_lock or dereferencing target->ci_group. Fixes: d79d75b5c518 ("fs: configfs: Add unlocked version of configfs_depend_item()") Reported-by: syzbot+8277294f653f005e64d8@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=8277294f653f005e64d8 Cc: stable@vger.kernel.org Signed-off-by: Mostafa Amin --- Tested on v7.4-rc4 (x86_64 QEMU/KVM, 4 vCPUs, CONFIG_PROVE_LOCKING=y, CONFIG_KASAN=y, CONFIG_UBSAN=y, CONFIG_DEBUG_ATOMIC_SLEEP=y, CONFIG_DEBUG_KMEMLEAK=y, panic_on_warn=1): - Unmodified syzbot C reproducer (f_tcm tpgt_1 + f_uvc symlink unlink): 3/3 crashes before fix, 0/3 crashes (~1,609 iterations) after fix. - Bounded C reproducer with full configfs teardown: 3/3 crashes before fix, 0/10 crashes (10 boots x 50 cycles) after fix, 3/3 crashes with fix reverted. - Cross-subsystem regression & 4-thread SMP race suite (verifying tcm.usb0 -EBUSY while depended upon by tpgt_1, UVC-free rmdir(tpgt_1) + rmdir(tcm.usb0), and concurrent mkdir/rmdir/symlink/unlink across usb_gadget and target): crashes before fix (lockdep + ci_group null-ptr-deref), 0/5 crashes (x3 iterations) after fix. - W=1 build of fs/configfs/ on x86_64 debug (CONFIG_CONFIGFS_FS=y), x86_64 allmodconfig (CONFIG_CONFIGFS_FS=m), and arm64 Clang: 0 warnings. Not tested: sparse/smatch/coccinelle (not installed on test host), non-x86_64 runtime. --- fs/configfs/dir.c | 101 ++++++++++++++++++++++++++---------------------------- 1 file changed, 49 insertions(+), 52 deletions(-) diff --git a/fs/configfs/dir.c b/fs/configfs/dir.c index 0c80feec5926..582c43929c00 100644 --- a/fs/configfs/dir.c +++ b/fs/configfs/dir.c @@ -1223,73 +1223,70 @@ void configfs_undepend_item(struct config_item *target) EXPORT_SYMBOL(configfs_undepend_item); /* - * caller_subsys is a caller's subsystem not target's. This is used to - * determine if we should lock root and check subsys or not. When we are - * in the same subsystem as our target there is no need to do locking as - * we know that subsys is valid and is not unregistered during this function - * as we are called from callback of one of his children and VFS holds a lock - * on some inode. Otherwise we have to lock our root to ensure that target's - * subsystem it is not unregistered during this function. + * caller_subsys is a caller's subsystem, not target's. Since this helper is + * meant for configfs callbacks of caller_subsys, the VFS already holds an + * inode lock (and possibly caller_subsys->su_mutex), keeping configfs pinned + * and preventing caller_subsys from being unregistered. + * + * Do not acquire the configfs root inode lock here: taking it under + * caller_subsys->su_mutex or a child directory's inode lock inverts the + * VFS/configfs lock hierarchy (inode_lock -> su_mutex). Also avoid chasing + * target->ci_group, which is only serialized by target's su_mutex and may be + * NULL while target is being attached or detached in another subsystem. + * + * Instead, hold configfs_dirent_lock across the subsystem lookup, tree scan + * and s_dependent_count update; configfs_dirent_lock serializes against + * subsystem registration/unregistration (CONFIGFS_USET_CREATING / + * CONFIGFS_USET_DROPPING) as well as mkdir() and rmdir(). */ int configfs_depend_item_unlocked(struct configfs_subsystem *caller_subsys, struct config_item *target) { - struct configfs_subsystem *target_subsys; - struct config_group *root, *parent; - struct configfs_dirent *subsys_sd; + struct configfs_dirent *root_sd, *caller_sd, *subsys_sd, *p; + struct dentry *subsys_dentry; int ret = -ENOENT; /* Disallow this function for configfs root */ if (configfs_is_root(target)) return -EINVAL; - parent = target->ci_group; - /* - * This may happen when someone is trying to depend root - * directory of some subsystem - */ - if (configfs_is_root(&parent->cg_item)) { - target_subsys = to_configfs_subsystem(to_config_group(target)); - root = parent; - } else { - target_subsys = parent->cg_subsys; - /* Find a cofnigfs root as we may need it for locking */ - for (root = parent; !configfs_is_root(&root->cg_item); - root = root->cg_item.ci_group) - ; - } + if (WARN_ON_ONCE(!caller_subsys)) + return -EINVAL; - if (target_subsys != caller_subsys) { - /* - * We are in other configfs subsystem, so we have to do - * additional locking to prevent other subsystem from being - * unregistered - */ - inode_lock(d_inode(root->cg_item.ci_dentry)); + subsys_dentry = caller_subsys->su_group.cg_item.ci_dentry; + if (WARN_ON_ONCE(!subsys_dentry)) + return -EINVAL; - /* - * As we are trying to depend item from other subsystem - * we have to check if this subsystem is still registered - */ - subsys_sd = configfs_find_subsys_dentry( - root->cg_item.ci_dentry->d_fsdata, - &target_subsys->su_group.cg_item); - if (!subsys_sd) - goto out_root_unlock; - } else { - subsys_sd = target_subsys->su_group.cg_item.ci_dentry->d_fsdata; + caller_sd = subsys_dentry->d_fsdata; + root_sd = subsys_dentry->d_parent->d_fsdata; + + spin_lock(&configfs_dirent_lock); + + if (!(caller_sd->s_type & (CONFIGFS_USET_DROPPING | + CONFIGFS_USET_CREATING))) + ret = configfs_depend_prep(caller_sd, target); + + if (ret) { + list_for_each_entry(subsys_sd, &root_sd->s_children, + s_sibling) { + if (subsys_sd == caller_sd) + continue; + if ((subsys_sd->s_type & CONFIGFS_DIR) && + !(subsys_sd->s_type & (CONFIGFS_USET_DROPPING | + CONFIGFS_USET_CREATING))) { + ret = configfs_depend_prep(subsys_sd, target); + if (!ret) + break; + } + } } - /* Now we can execute core of depend item */ - ret = configfs_do_depend_item(subsys_sd, target); + if (!ret) { + p = target->ci_dentry->d_fsdata; + p->s_dependent_count += 1; + } - if (target_subsys != caller_subsys) -out_root_unlock: - /* - * We were called from subsystem other than our target so we - * took some locks so now it's time to release them - */ - inode_unlock(d_inode(root->cg_item.ci_dentry)); + spin_unlock(&configfs_dirent_lock); return ret; } --- base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f change-id: 20261204-fix-krn-2026-0008-aea167dcd992 Best regards, -- Mostafa Amin