Linux USB
 help / color / mirror / Atom feed
* [PATCH] usb: gadget: f_midi: restart IN transfers after SET_INTERFACE
@ 2026-11-29 14:05 Jaegyun Lee
  2026-11-29 14:17 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Jaegyun Lee @ 2026-11-29 14:05 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Neill Kapron
  Cc: Jaegyun Lee, linux-usb, linux-kernel, stable

When the host sends SET_INTERFACE for the MIDIStreaming interface,
f_midi_set_alt() calls f_midi_start_ep(), which starts with
usb_ep_disable(). f_midi_disable() is not called first, so the IN
requests stay in midi->in_req_fifo. usb_ep_disable() gives back the
queued IN requests with an error status, but f_midi_complete() only
resets req->length on a successful completion; the -ECONNABORTED/
-ECONNRESET/-ESHUTDOWN branch handles the OUT endpoint only:

	if (ep == midi->out_ep) {
		f_midi_handle_out_data(ep, req);
		/* We don't need to free IN requests because it's handled
		 * by the midi->in_req_fifo. */
		free_ep_req(ep, req);
	}
	return;

If any of them was in flight when the host sent SET_INTERFACE, it
stays in the fifo with req->length > 0; once it reaches the head,
f_midi_do_transmit() returns without queueing anything:

	if (req->length > 0)
		return 0;

Since nothing is queued on the re-enabled IN endpoint, no completion
ever resets a length and MIDI output from the gadget stays stopped
until the function is disabled (disconnect or SET_CONFIGURATION).
A writer on the gadget's rawmidi device blocks in snd_rawmidi_write().

To reproduce, fill the IN requests from the gadget while the host does
not read, then send SET_INTERFACE(1, 0) from the host and read bulk IN.

Mark IN requests given back with an error as free, and restart
transmission after SET_INTERFACE.

Fixes: e1e3d7ec5da3 ("usb: gadget: f_midi: pre-allocate IN requests")
Cc: stable@vger.kernel.org
Assisted-by: ZCode:GLM-5.4
Signed-off-by: Jaegyun Lee <dreams200260@gmail.com>
---
 drivers/usb/gadget/function/f_midi.c |    4 ++++
 1 file changed, 4 insertions(+)

diff --git a/drivers/usb/gadget/function/f_midi.c b/drivers/usb/gadget/function/f_midi.c
--- a/drivers/usb/gadget/function/f_midi.c
+++ b/drivers/usb/gadget/function/f_midi.c
@@ -299,6 +299,8 @@ f_midi_complete(struct usb_ep *ep, struc
 			/* We don't need to free IN requests because it's handled
 			 * by the midi->in_req_fifo. */
 			free_ep_req(ep, req);
+		} else if (ep == midi->in_ep) {
+			req->length = 0;
 		}
 		return;
 
@@ -412,6 +414,8 @@ static int f_midi_set_alt(struct usb_fun
 		}
 	}
 
+	queue_work(system_highpri_wq, &midi->work);
+
 	return 0;
 }
 

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] usb: gadget: f_midi: restart IN transfers after SET_INTERFACE
  2026-11-29 14:05 [PATCH] usb: gadget: f_midi: restart IN transfers after SET_INTERFACE Jaegyun Lee
@ 2026-11-29 14:17 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-29 14:17 UTC (permalink / raw)
  To: Jaegyun Lee; +Cc: linux-usb

Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [Critical] usb: gadget: f_midi: use-after-free race in
  f_midi_complete() [1]
--

commit e9b2887e99b3e3e822534d3a7636ce9d94ac700a
Author: Jaegyun Lee <dreams200260@gmail.com>

usb: gadget: f_midi: restart IN transfers after SET_INTERFACE

This commit resets req->length on aborted IN requests in f_midi_complete()
and schedules midi->work at the end of f_midi_set_alt() so IN transfers
resume after SET_INTERFACE.

> diff --git a/drivers/usb/gadget/function/f_midi.c b/drivers/usb/gadget/function/f_midi.c
> index 28d2e9db2450d..3e71ccb173a3a 100644
> --- a/drivers/usb/gadget/function/f_midi.c
> +++ b/drivers/usb/gadget/function/f_midi.c
> @@ -299,6 +299,8 @@ f_midi_complete(struct usb_ep *ep, struct usb_request *req)
>  			/* We don't need to free IN requests because it's handled
>  			 * by the midi->in_req_fifo. */
>  			free_ep_req(ep, req);
> +		} else if (ep == midi->in_ep) {
> +			req->length = 0;
>  		}
>  		return;

[Severity: Critical]
This problem wasn't introduced by this patch, but can f_midi_complete() hit
a use-after-free on req [1] when f_midi_disable() runs concurrently?

When an IN request is queued, its pointer stays in midi->in_req_fifo.  On
normal completion, the UDC driver drops its controller lock before calling
usb_gadget_giveback_request(), and usb_ep_disable() does not wait for
in-flight completion callbacks to finish:

CPU0                                CPU1
usb_gadget_giveback_request()
  f_midi_complete()
                                    f_midi_disable()
                                      usb_ep_disable(midi->in_ep)
                                      while (kfifo_get(&midi->in_req_fifo,
                                                       &req))
                                        free_ep_req(midi->in_ep, req)
    req->length = 0;  <-- use-after-free
    queue_work(..., &midi->work);

If a disconnect or configuration change calls f_midi_disable() while an IN
completion is entering f_midi_complete(), f_midi_disable() drains
midi->in_req_fifo and frees req before f_midi_complete() writes to
req->length and queues midi->work.

[1] https://sashiko.dev/#/bug/linux-22a882f3-c221-4b3e-aa74-b2c3c35d00ab

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261129140555.651497-1-dreams200260@gmail.com?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-29 14:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-29 14:05 [PATCH] usb: gadget: f_midi: restart IN transfers after SET_INTERFACE Jaegyun Lee
2026-11-29 14:17 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox