From: sashiko-bot@kernel.org
To: "Jaegyun Lee" <dreams200260@gmail.com>
Cc: linux-usb@vger.kernel.org, linux-sound@vger.kernel.org
Subject: Re: [PATCH v4] usb: gadget: midi2: serialize the IN endpoint transmit path
Date: Sat, 05 Dec 2026 02:52:41 +0000 [thread overview]
Message-ID: <sashiko-outbox-250958@kernel.org> (raw)
In-Reply-To: <179638876078.1420263.2904571847750437390@gmail.com>
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] usb: gadget: midi2: unbounded IRQ-off tx_lock hold and barrier
overhead
- [Medium] usb: gadget: midi2: f_midi2_start_eps() drops tx_pending
after unlocking
--
commit 8783d30e4bce8c594e55868e72c705df12dae801
Author: Jaegyun Lee <dreams200260@gmail.com>
usb: gadget: midi2: serialize the IN endpoint transmit path
This commit adds a per-endpoint spinlock and pending flag to serialize the
MIDI 1.0 and UMP IN transmit paths against concurrent completions and
endpoint request allocation and teardown.
> diff --git a/drivers/usb/gadget/function/f_midi2.c b/drivers/usb/gadget/function/f_midi2.c
> index 75036d4d60b35..0e24fb834f221 100644
> --- a/drivers/usb/gadget/function/f_midi2.c
> +++ b/drivers/usb/gadget/function/f_midi2.c
[ ... ]
> @@ -1052,6 +1056,48 @@ static void process_midi1_transmit(struct f_midi2 *midi2)
> }
> }
>
> +static void f_midi2_tx_do_once(struct f_midi2_usb_ep *usb_ep)
> +{
> + clear_bit(0, &usb_ep->tx_pending);
> + /* order the clear before the request scan below */
> + smp_mb__after_atomic();
> + if (usb_ep->ep)
> + process_ump_transmit(usb_ep->ep);
> + else
> + process_midi1_transmit(usb_ep->card);
> +}
> +
> +static void f_midi2_tx_run_blocking(struct f_midi2_usb_ep *usb_ep)
> +{
> + if (!usb_ep->usb_ep)
> + return;
> +
> + do {
> + scoped_guard(spinlock_irqsave, &usb_ep->tx_lock)
> + f_midi2_tx_do_once(usb_ep);
> + /* pairs with smp_mb__after_atomic() in f_midi2_tx_request() */
> + smp_mb();
> + } while (test_bit(0, &usb_ep->tx_pending));
> +}
> +
> +/*
> + * The UDC may complete an IN request inside usb_ep_queue(), i.e. while
> + * tx_lock is held by the transmitter. Such a completion only sets
> + * tx_pending, and the lock holder transmits again after unlocking.
> + */
> +static void f_midi2_tx_request(struct f_midi2_usb_ep *usb_ep)
> +{
> + set_bit(0, &usb_ep->tx_pending);
> + /* pairs with smp_mb() in f_midi2_tx_run_blocking() */
> + smp_mb__after_atomic();
> +
> + scoped_cond_guard(spinlock_irqsave_try, return, &usb_ep->tx_lock)
> + f_midi2_tx_do_once(usb_ep);
> + smp_mb();
> + if (test_bit(0, &usb_ep->tx_pending))
> + f_midi2_tx_run_blocking(usb_ep);
> +}
> +
> /* complete handler for MIDI1 EP-in requests */
> static void f_midi2_midi1_ep_in_complete(struct usb_ep *usb_ep,
> struct usb_request *req)
[Severity: High]
Can holding tx_lock with IRQs disabled across f_midi2_tx_do_once() spin
indefinitely with local interrupts off?
In altset 0, process_midi1_transmit() loops indefinitely calling
snd_ump_transmit() four bytes at a time. If userspace continuously writes
non-convertible UMP packets (such as Utility or Stream messages) or packets
for an unmapped group, the loop skips them without queueing a USB request.
A concurrent writer in snd_rawmidi_kernel_write1() can refill the ring buffer
faster than this 4-byte loop drains it, keeping tx_lock held with IRQs off
indefinitely and triggering RCU stalls, hard lockups, or spins in
f_midi2_set_alt() -> f_midi2_stop_eps().
Even during normal streaming, f_midi2_ep_in_complete() and
f_midi2_midi1_ep_in_complete() call put_empty_request() without taking
tx_lock, so cross-CPU completions can continuously replenish free_reqs and
keep both transmit loops running with IRQs disabled.
Separately, every uncontended IN completion in f_midi2_tx_request() now pays
two atomic bitops, three full memory barriers, and an IRQ-off spinlock cycle
per packet.
In addition, f_midi2_stop_eps() holds ep_in->tx_lock with IRQs disabled across
the unrelated f_midi2_drop_reqs(ep_out) calls, and f_midi2_start_eps() holds
it across up to 32 alloc_ep_req() allocations.
[ ... ]
> @@ -1284,7 +1332,8 @@ static int f_midi2_start_eps(struct f_midi2_usb_ep *ep_in,
> if (err)
> return err;
>
> - err = f_midi2_alloc_ep_reqs(ep_in);
> + scoped_guard(spinlock_irqsave, &ep_in->tx_lock)
> + err = f_midi2_alloc_ep_reqs(ep_in);
> if (err)
> return err;
> err = f_midi2_alloc_ep_reqs(ep_out);
[Severity: Medium]
Can f_midi2_start_eps() lose a pending transmit wakeup after releasing
ep_in->tx_lock?
f_midi2_tx_request() returns immediately when trylock fails, relying on the
current tx_lock holder to observe and drain tx_pending after unlocking. Here
f_midi2_set_alt() sets operation_mode and f_midi2_start_eps() enables ep_in
before taking tx_lock, and does not check tx_pending after releasing it:
CPU0 CPU1
f_midi2_set_alt()
f_midi2_start_eps()
f_midi2_start_ep(ep_in, fn)
lock(&ep_in->tx_lock)
f_midi2_alloc_ep_reqs(ep_in)
f_midi2_ep_in_complete()
f_midi2_tx_request(ep_in)
set_bit(0, &ep_in->tx_pending)
trylock(&ep_in->tx_lock) -> fails
return
unlock(&ep_in->tx_lock) <-- tx_pending never drained
If an IN completion runs on another CPU during f_midi2_alloc_ep_reqs(ep_in),
remaining buffered ALSA MIDI output stalls until the next userspace write
triggers f_midi2_ump_trigger(). Checking tx_pending after unlocking (or
calling f_midi2_tx_run_blocking() when set) would close this window.
--
Sashiko AI review · https://sashiko.dev/#/patchset/179638876078.1420263.2904571847750437390@gmail.com?part=1
prev parent reply other threads:[~2026-12-05 2:52 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-03 13:58 [PATCH v3] usb: gadget: midi2: serialize the IN endpoint transmit path Jaegyun Lee
2026-12-03 14:31 ` sashiko-bot
2026-12-03 17:30 ` Takashi Iwai
2026-12-05 2:24 ` [PATCH v4] " Jaegyun Lee
2026-12-05 2:52 ` sashiko-bot [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=sashiko-outbox-250958@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=dreams200260@gmail.com \
--cc=linux-sound@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox