Re: [PATCH] usb: gadget: f_fs: only dequeue ep0req when it is queued
From: Greg Kroah-Hartman
Date: Fri Nov 20 2026 - 05:20:05 EST
On Tue, Sep 08, 2026 at 04:56:38PM +0000, Cole Munz wrote:
> Commit ce405d561b02 ("usb: gadget: f_fs: Ensure ep0req is dequeued
> before free_request") made functionfs_unbind() call usb_ep_dequeue()
> on ep0req unconditionally before freeing it. That closed a real
> use-after-free: a reader or writer parked in __ffs_ep0_queue_wait()
> holds ffs->mutex and waits on ep0req_completion while the request is
> in flight, and unbind has to force it to complete so that thread wakes
> up, returns, and drops the mutex before the request is freed.
>
> Problem is, unbind now dequeues every single time it runs, and the
> parked-reader case is rare. Far more often ep0req already completed
> on its own, or it was never queued in the first place because bind
> failed before any control transfer reached us. dwc3 is not quiet
> about the second case. dwc3_gadget_ep_dequeue() logs "request %p was
> not queued to %s" whenever the request isn't sitting on one of the
> endpoint's lists, so ordinary gadget teardown, and any boot where the
> host never finished enumeration before the driver unbound, prints
> that error right before the "failed to start ...: -19" that follows.
>
> The fix is to track whether ep0req is actually queued on ep0, and
> only call usb_ep_dequeue() in functionfs_unbind() when it is.
> ep0req_queued gets set right before usb_ep_queue() in
> __ffs_ep0_queue_wait(), cleared again if that call fails, and cleared
> in ffs_ep0_complete() once the request comes back. The parked-reader
> case still works the same way it always did: the flag stays true for
> as long as the thread sits blocked in
> wait_for_completion_interruptible(), so unbind still finds it queued
> and still dequeues. In the other two cases the flag reads false and
> unbind skips a call that was doing nothing anyway.
>
> Fixes: ce405d561b02 ("usb: gadget: f_fs: Ensure ep0req is dequeued before free_request")
> Assisted-by: LLM sparse
> Signed-off-by: Cole Munz <Munzzyy1@xxxxxxxxx>
> ---
> This is the f_fs-side fix Thinh suggested on the dwc3 thread rather than
> changing dwc3_gadget_ep_dequeue()'s return, and it also quiets the boot
> case that the dwc3 change did not:
> https://lore.kernel.org/linux-usb/aptrIbp-p6RNGd9X@vbox/
>
> Built for arm64, f_fs.o compiles with no new warnings, sparse clean,
> checkpatch --strict reports nothing. I can't flash a gadget here, so the
> runtime side is reasoned from source, not tested on hardware.
Please test it on real hardware before submitting it again.
thanks,
greg k-h