linux/drivers/usb
Neill Kapron 569dd7e5dc usb: gadget: f_fs: Prevent deadlock during ep0 read loop
Currently, ffs_ep0_read() holds ffs->mutex when it prepares to go to
sleep waiting for an event. When no setup events are pending, it calls
wait_event_interruptible_exclusive_locked_irq() with the mutex still
held. The wait macro deliberately drops the waitqueue spinlock before
sleeping but does not drop the mutex.

If a userspace daemon is polling ep0 via read() and the gadget is
asynchronously torn down via configfs (e.g., echo "" > UDC), a
deadlock can occur:

1. The configfs teardown calls functionfs_unbind(), which queues a
   FUNCTIONFS_UNBIND event.
2. The daemon wakes up, consumes the event, and drops the mutex.
3. However, if the daemon loops and immediately issues another read()
   before exiting, it reacquires ffs->mutex and again goes into an
   interruptible sleep.
4. Meanwhile, functionfs_unbind() continues execution and attempts to
   acquire ffs->mutex to tear down ep0req.
5. The kernel deadlocks because the configfs thread is stuck in an
   uninterruptible sleep waiting for the mutex, while the userspace
   daemon is in an interruptible sleep holding the mutex forever
   because no more events will arrive.

To fix this, we drop both the waitqueue spinlock and ffs->mutex before
going to sleep, and use wait_event_interruptible_exclusive() instead.
Upon waking up, we jump back to the `retry` label to safely reacquire
the mutex and re-evaluate the state machine. By not sleeping with
ffs->mutex held, we natively decouple gadget teardowns (which require
the mutex) from userspace polling.

Fixes: ddf8abd259 ("USB: f_fs: the FunctionFS driver")
Cc: stable@vger.kernel.org
Assisted-by: Antigravity:gemini-3.1-pro
Signed-off-by: Neill Kapron <nkapron@google.com>
Link: https://patch.msgid.link/20260724204117.4036015-1-nkapron@google.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
History 2026-08-14 10:48:31 +09:00
..
atm Merge 7.2-rc7 into usb-next 2026-08-14 10:17:11 +09:00
c67x00 Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
cdns3 Merge 7.2-rc7 into usb-next 2026-08-14 10:17:11 +09:00
chipidea Merge 7.2-rc5 into usb-next 2026-07-27 08:40:53 +02:00
class Merge 7.2-rc3 into usb-next 2026-07-13 07:09:28 +02:00
common USB: ulpi: fix memory leak on registration failure 2026-06-25 15:11:27 +01:00
core Merge 7.2-rc7 into usb-next 2026-08-14 10:17:11 +09:00
dwc2 Merge 7.2-rc3 into usb-next 2026-07-13 07:09:28 +02:00
dwc3 usb: xhci: standardize multi bit-field macros 2026-08-14 10:26:41 +09:00
early usb: early: xhci-dbc: Fix early_ioremap leak 2025-07-07 11:04:46 +02:00
fotg210 usb: free iso schedules on failed submit 2026-07-08 13:30:38 +02:00
gadget usb: gadget: f_fs: Prevent deadlock during ep0 read loop 2026-08-14 10:48:31 +09:00
host usb: xhci: Handle bogus TRB pointers in Missed Service Error events 2026-08-14 10:26:41 +09:00
image Merge 7.0-rc4 into usb-next 2026-03-16 11:50:48 +01:00
isp1760 Convert remaining multi-line kmalloc_obj/flex GFP_KERNEL uses 2026-02-22 08:26:33 -08:00
misc Merge 7.2-rc7 into usb-next 2026-08-14 10:17:11 +09:00
mon Convert more 'alloc_obj' cases to default GFP_KERNEL arguments 2026-02-21 20:03:00 -08:00
mtu3 Merge 7.2-rc3 into usb-next 2026-07-13 07:09:28 +02:00
musb Merge 7.2-rc5 into usb-next 2026-07-27 08:40:53 +02:00
phy USB: phy: fsl-usb: fix missing static keywords 2026-07-17 17:54:18 +02:00
renesas_usbhs usb: renesas_usbhs: Fix power-off ordering on unbind 2026-07-10 15:56:24 +02:00
roles usb: roles: get usb role switch from parent only for usb-b-connector 2026-03-11 16:22:22 +01:00
serial USB serial updates for 7.3-rc1 2026-08-14 10:25:23 +09:00
storage USB: storage: add NO_ATA_1X quirk for Longmai USB Key 2026-07-16 17:11:02 +02:00
typec usb: typec: thunderbolt: Disable work before freeing tbt on remove 2026-08-14 10:37:54 +09:00
usbip usbip: vudc: fix NULL deref in vep_dequeue() 2026-07-08 13:29:31 +02:00
Kconfig usb: pci-quirks: handle HAS_IOPORT dependency for AMD quirk 2023-10-02 16:19:12 +02:00
Makefile usb: cdns3: Add USBSSP platform driver support 2026-04-27 05:14:18 -06:00
usb-skeleton.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00