[PATCH v2] usb: gadget: f_mass_storage: don't hold opts->lock across unregister_gadget_item()
From: Jiayi Zhang
Date: Sun Dec 06 2026 - 08:41:57 EST
fsg_lun_drop() takes fsg_opts->lock and, when the function is linked
into a configuration, calls unregister_gadget_item(), which takes
gi->lock. config_usb_cfg_link() and config_usb_cfg_unlink() take the
two locks in the opposite order: they hold gi->lock across
usb_get_function() and usb_put_function(), which end in fsg_alloc() and
fsg_free(), and both take fsg_opts->lock:
CPU0 (rmdir lun.1) CPU1 (rm c.1/mass_storage.0)
fsg_lun_drop() config_usb_cfg_unlink()
mutex_lock(&fsg_opts->lock) mutex_lock(&gi->lock)
usb_put_function()
fsg_free()
mutex_lock(&fsg_opts->lock)
unregister_gadget_item()
mutex_lock(&gi->lock)
Both tasks then sleep forever. The rmdir task also holds
gadget_subsys.su_mutex, so every later mkdir and rmdir under usb_gadget
hangs too. Linking the function into a second configuration instead of
the unlink deadlocks the same way through fsg_alloc(). lockdep reports
the dependency:
WARNING: possible circular locking dependency detected
...
-> #1 (&opts->lock){+.+.}-{4:4}:
fsg_alloc+0x92/0x200 drivers/usb/gadget/function/f_mass_storage.c:3569
usb_get_function+0x63/0xb0 drivers/usb/gadget/functions.c:61
config_usb_cfg_link+0x148/0x260 drivers/usb/gadget/configfs.c:469
configfs_symlink+0x59a/0x1030 fs/configfs/symlink.c:196
...
-> #0 (&gi->lock){+.+.}-{4:4}:
...
unregister_gadget_item+0x31/0xd0 drivers/usb/gadget/configfs.c:2087
fsg_lun_drop+0xa8/0x270 drivers/usb/gadget/function/f_mass_storage.c:3362
configfs_rmdir+0x85f/0x950 fs/configfs/dir.c:1567
...
Drop fsg_opts->lock across unregister_gadget_item(), as ffs_closed()
does in f_fs, and take it again for the LUN removal. What can run in
between does not affect the code that follows: link and unlink only
change refcnt, which is not read again. Once an unlink has dropped
refcnt to zero, the stall and num_buffers attributes can be written
again, but they change common->can_stall and the buffers, which the LUN
removal does not touch. The LUNs of this function cannot be created or
removed meanwhile, because configfs_rmdir() holds the inode lock of the
function directory and su_mutex, and attribute accesses to the LUN
being removed already fail with -ENOENT. fsg_opts and the gadget stay
allocated: the function directory cannot be removed while its inode is
locked and it still has this LUN, and the gadget cannot be removed
while it has the function.
Fixes: cee51c33f52e ("usb: gadget: configfs: add mutex lock before unregister gadget")
Cc: stable@xxxxxxxxxxxxxxx
Reported-by: syzbot+4d1ffdc8360c3ce80fea@xxxxxxxxxxxxxxxxxxxxxxxxx
Closes: https://syzkaller.appspot.com/bug?extid=4d1ffdc8360c3ce80fea
Signed-off-by: Jiayi Zhang <zhangjiayi1014@xxxxxxxxx>
---
v2: changelog only, the code is the same as v1. The paragraph on why
dropping the lock is safe is corrected as pointed out by the Sashiko
review of v1: the stall and num_buffers attributes can be written once
an unlink has dropped refcnt to zero (safe because the LUN removal
does not use those fields), and attribute accesses to a LUN that is
being removed fail with -ENOENT, not -ENODEV.
v1: https://lore.kernel.org/all/20261206131609.1790879-1-zhangjiayi1014@xxxxxxxxx/
syzbot reported this on linux-6.6.y and reproduced it on mainline
7f063b2f17ea with the same C reproducer; the lockdep excerpt above is
from the mainline run. The patch applies to usb-testing, linux-next and
linux-6.6.y.
Writing UDC can bind the gadget again between unregister_gadget_item()
and the LUN removal; that window already exists without this patch
(the bind path does not take fsg_opts->lock), and this patch does not
change it.
Tested in QEMU (2 CPUs, mainline 7f063b2f17ea, syzbot's upstream config:
PREEMPT_RT, lockdep, KASAN), 5 instances of up to 10 minutes each:
- syzbot's C reproducer, which is single-threaded and only creates the
lock dependency: lockdep reported it in 5 of 5 instances without the
patch and in none of 5 with it.
- a test program that runs, in each round, rmdir of lun.1 in one thread
against removal of the c.1 link or a link into a second configuration
in another: without the patch all 5 instances reported the circular
dependency; on a non-RT 7.4-rc3 kernel both threads also stayed
blocked in the first round and the hung task detector reported them.
With the patch, 5 instances ran it in a loop for 10 minutes each with
no report.
- with the gadget bound to dummy_udc, rmdir of lun.1 unbinds the gadget
and writing UDC binds it again: 5 rounds on each of 5 instances with
the patch, no report.
Build tested with GCC 8.5.0 and W=1 (drivers/usb/gadget/function/
f_mass_storage.o, drivers/usb/gadget/configfs.o): no warnings.
AI tools assisted with the analysis, the code and the changelog.
drivers/usb/gadget/function/f_mass_storage.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/drivers/usb/gadget/function/f_mass_storage.c b/drivers/usb/gadget/function/f_mass_storage.c
index 52314a0..3676be4 100644
--- a/drivers/usb/gadget/function/f_mass_storage.c
+++ b/drivers/usb/gadget/function/f_mass_storage.c
@@ -3372,7 +3372,10 @@ static void fsg_lun_drop(struct config_group *group, struct config_item *item)
struct config_item *gadget;
gadget = group->cg_item.ci_parent->ci_parent;
+ /* config_usb_cfg_link/unlink() take gi->lock before fsg_opts->lock */
+ mutex_unlock(&fsg_opts->lock);
unregister_gadget_item(gadget);
+ mutex_lock(&fsg_opts->lock);
}
fsg_common_remove_lun(lun_opts->lun);
--
2.53.0