* [PATCH] configfs: avoid root inode lock in configfs_depend_item_unlocked()
@ 2026-12-05 1:55 Mostafa Amin via B4 Relay
2026-12-05 2:13 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Mostafa Amin via B4 Relay @ 2026-12-05 1:55 UTC (permalink / raw)
To: Breno Leitao, Andreas Hindborg, Krzysztof Opasiak,
Nicholas Bellinger
Cc: linux-kernel, target-devel, linux-usb,
syzbot+8277294f653f005e64d8, stable, Mostafa Amin
From: Mostafa Amin <mostafa.amin@windriver.com>
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 <mostafa.amin@windriver.com>
---
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 <mostafa.amin@windriver.com>
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH] configfs: avoid root inode lock in configfs_depend_item_unlocked()
2026-12-05 1:55 [PATCH] configfs: avoid root inode lock in configfs_depend_item_unlocked() Mostafa Amin via B4 Relay
@ 2026-12-05 2:13 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-05 2:13 UTC (permalink / raw)
To: Mostafa Amin; +Cc: target-devel, linux-usb
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] configfs: unbounded recursive subsystem scan can overflow
kernel stack
- [Medium] configfs: global spinlock held across full multi-subsystem
DFS on depend
--
commit 6f55b77b8b9ee91025222b4609539b128dd1c300
Author: Mostafa Amin <mostafa.amin@windriver.com>
configfs: avoid root inode lock in configfs_depend_item_unlocked()
This commit removes the configfs root inode lock and target->ci_group
traversal from configfs_depend_item_unlocked(), replacing them with a scan
across caller_subsys and all other subsystems under configfs_dirent_lock.
> diff --git a/fs/configfs/dir.c b/fs/configfs/dir.c
> index 0c80feec5926b..582c43929c00d 100644
> --- a/fs/configfs/dir.c
> +++ b/fs/configfs/dir.c
> @@ -1223,73 +1223,70 @@ void configfs_undepend_item(struct config_item *target)
[ ... ]
> + 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;
> + }
> + }
> }
[Severity: Medium]
Can scanning every registered subsystem with recursive
configfs_depend_prep() overflow the kernel stack?
Previously only target's subsystem was walked, but now any cross-subsystem
call (or -ENOENT miss) recurses into unrelated subsystems with no depth
limit.
In drivers/hwtracing/stm/policy.c, stp_policy_node_make() creates child
groups whose make_group callback points back to stp_policy_node_make(),
allowing arbitrarily deep directories. Walking a deep tree there overflows
the kernel stack while holding configfs_dirent_lock.
>
> + 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);
[Severity: Medium]
Does holding configfs_dirent_lock across the full multi-subsystem walk
introduce a lock-scalability regression?
Previously the spinlock was held only while scanning target's subsystem.
Now a cross-subsystem call like usbg_make_tpg() in f_tcm.c walks all of
caller_sd (the entire LIO target tree) and every preceding subsystem in
root_sd->s_children, visiting all attribute and directory dirents.
Because configfs_dirent_lock is a global spinlock serializing all configfs
lookup, readdir, mkdir, rmdir, and depend operations, holding it across
all mounted subsystems stalls unrelated configfs users and increases
scheduling latency as configfs trees grow.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204-fix-krn-2026-0008-v1-1-6dabad0e1a40@windriver.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-05 2:13 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05 1:55 [PATCH] configfs: avoid root inode lock in configfs_depend_item_unlocked() Mostafa Amin via B4 Relay
2026-12-05 2:13 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox