Linux USB
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Alexandru Ardelean" <aardelean@taladin.ro>, linux-usb@vger.kernel.org
Subject: Re: [PATCH v2 2/2] usb: musb: gadget: mask the MUSB IRQ while complete() runs
Date: Tue, 24 Nov 2026 15:23:41 +0000	[thread overview]
Message-ID: <sashiko-outbox-231400@kernel.org> (raw)
In-Reply-To: <20261124150344.69264-3-aardelean@taladin.ro>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] usb: musb: gadget: spin_lock() with IRQs enabled in
  musb_g_giveback()
- [High] usb: musb: gadget: enable_irq() in musb_g_giveback() unmasks
  active ISR
- [Medium] usb: musb: gadget: SMP race window around IRQ masking in
  musb_g_giveback()
- [Medium] usb: musb: gadget: per-request IRQ disable/enable overhead on
  hot path
--

commit 4f83c5c2c1d92794ca51395229cf5b4d310d1f0c
Author: Alexandru Ardelean <aardelean@taladin.ro>

usb: musb: gadget: mask the MUSB IRQ while complete() runs

This commit masks the MUSB controller IRQ around
usb_gadget_giveback_request() in musb_g_giveback() so that a request
completion callback that enables local interrupts does not allow an MUSB
interrupt to be serviced while the endpoint request queue is empty.

> diff --git a/drivers/usb/musb/musb_gadget.c b/drivers/usb/musb/musb_gadget.c
> index dd67ab97df9d8..5cb7eb9c56de0 100644
> --- a/drivers/usb/musb/musb_gadget.c
> +++ b/drivers/usb/musb/musb_gadget.c
> @@ -145,7 +145,10 @@ __acquires(ep->musb->lock)
>  		unmap_dma_buffer(req, musb);
>  
>  	trace_musb_req_gb(req);
> +	/* no MUSB interrupt during complete(), even if it enables IRQs */
> +	disable_irq_nosync(musb->nIrq);
>  	usb_gadget_giveback_request(&req->ep->end_point, &req->request);
> +	enable_irq(musb->nIrq);
>  	spin_lock(&musb->lock);
>  	ep->busy = busy;
>  }

[Severity: High]
Can calling plain spin_lock(&musb->lock) after enable_irq(musb->nIrq)
deadlock on musb->lock when req->complete() returns with local CPU
interrupts enabled?

Callers such as musb_gadget_queue() enter with local interrupts disabled
via spin_lock_irqsave(&musb->lock, lockflags), and musb_g_giveback() drops
the lock with plain spin_unlock(&musb->lock).  When musb_gadget_queue()
runs in process context and restarts an Rx endpoint via
musb_queue_resume_work() -> ... -> rxstate() -> musb_g_giveback(),
u_ether's rx_complete() calls netif_rx() -> local_bh_enable() ->
__local_bh_enable_ip():

kernel/softirq.c:__local_bh_enable_ip() {
	WARN_ON_ONCE(in_hardirq());
	lockdep_assert_irqs_enabled();
#ifdef CONFIG_TRACE_IRQFLAGS
	local_irq_disable();
#endif
	...
	preempt_count_dec();
#ifdef CONFIG_TRACE_IRQFLAGS
	local_irq_enable();
#endif
}

Besides triggering lockdep_assert_irqs_enabled() on entry because local
interrupts are still disabled after spin_unlock(&musb->lock),
__local_bh_enable_ip() returns with local interrupts enabled when
CONFIG_TRACE_IRQFLAGS is set (or whenever a completion callback leaves
IRQs enabled):

musb_gadget_queue()
  spin_lock_irqsave(&musb->lock, lockflags)
  ...
    musb_g_giveback()
      spin_unlock(&musb->lock)
      disable_irq_nosync(musb->nIrq)
      usb_gadget_giveback_request()
        ...
          __local_bh_enable_ip()
            local_irq_enable()
      enable_irq(musb->nIrq)
      spin_lock(&musb->lock)
      <hardirq>
        musb->isr()
          spin_lock_irqsave(&musb->lock, ...)  <-- spins forever

Because enable_irq(musb->nIrq) runs before spin_lock(&musb->lock) and
before ep->busy is restored, an MUSB interrupt firing between enable_irq()
and spin_lock() can also re-enter musb_g_giveback() while ep->busy is 1
and leave ep->busy stuck at 1.

[Severity: High]
Does calling enable_irq(musb->nIrq) from inside musb->isr unmask the
interrupt line while the handler is still running?

In enable_irq() -> __enable_irq() -> irq_startup() -> irq_enable(),
irq_enable() unconditionally unmasks the line at the irqchip and clears
IRQD_IRQ_MASKED:

kernel/irq/chip.c:irq_enable() {
	if (!irqd_irq_disabled(&desc->irq_data)) {
		unmask_irq(desc);
	} else {
		irq_state_clr_disabled(desc);
		if (desc->irq_data.chip->irq_enable) {
			desc->irq_data.chip->irq_enable(&desc->irq_data);
			irq_state_clr_masked(desc);
		} else {
			unmask_irq(desc);
		}
	}
}

When musb->nIrq runs as a forced-threaded oneshot interrupt (such as under
CONFIG_PREEMPT_RT or threadirqs), the flow handler masks the line and sets
IRQD_IRQ_MASKED until musb->isr finishes and irq_finalize_oneshot() runs:

kernel/irq/manage.c:irq_finalize_oneshot() {
	...
	if (!desc->threads_oneshot && !irqd_irq_disabled(&desc->irq_data) &&
	    irqd_irq_masked(&desc->irq_data))
		unmask_threaded_irq(desc);
	...
}

Because enable_irq() already cleared IRQD_IRQ_MASKED mid-handler,
irq_finalize_oneshot() skips unmask_threaded_irq(), which on
IRQCHIP_EOI_THREADED controllers skips chip->irq_eoi() and permanently
wedges the MUSB IRQ line.  On level-triggered glue drivers that clear or
EOI the interrupt only after musb_interrupt() returns (such as
da8xx_musb_interrupt() and tusb_musb_interrupt()), enable_irq() also
unmasks the hardware line before remaining endpoints are serviced and
before the glue interrupt source is cleared.

[Severity: Medium]
Can the Rx endpoint still lock up on SMP when musb->nIrq fires on another
CPU outside the disable_irq_nosync() / enable_irq() window?

musb_g_giveback() deletes the request from ep->req_list and drops
musb->lock before calling unmap_dma_buffer() and
disable_irq_nosync(musb->nIrq), and calls enable_irq(musb->nIrq) before
re-acquiring musb->lock and restoring ep->busy:

CPU0                                  CPU1
rxstate()
  musb_g_giveback()
    list_del(&req->list)
    ep->busy = 1
    spin_unlock(&musb->lock)
                                      <hardirq>
                                      musb->isr()
                                        spin_lock_irqsave(&musb->lock, ...)
                                        musb_g_rx()
                                          /* req_list empty: clears IRQ,
                                           * leaves RXPKTRDY set */
                                        spin_unlock_irqrestore(&musb->lock, ...)
    unmap_dma_buffer()
    disable_irq_nosync(musb->nIrq)
    usb_gadget_giveback_request()
      ...
        musb_gadget_queue()
          /* sees ep->busy == 1,
           * skips musb_ep_restart() */
    enable_irq(musb->nIrq)
    spin_lock(&musb->lock)
    ep->busy = 0                      <-- RXPKTRDY stays set, Rx wedged

When CPU1 takes the MUSB interrupt while musb->lock is dropped (or after
enable_irq() and before spin_lock()), musb_g_rx() sees an empty
ep->req_list and leaves MUSB_RXCSR_RXPKTRDY set, while musb_gadget_queue()
inside complete() sees ep->busy == 1 and does not restart the endpoint.

[Severity: Medium]
Does wrapping every usb_gadget_giveback_request() call in
disable_irq_nosync() and enable_irq() add unnecessary irqchip overhead on
the per-request hot path?

musb_g_giveback() runs for every completed TX, RX, and EP0 request, almost
always from hardirq context where netif_rx() does not touch bottom halves
or re-enable local interrupts.  Unconditionally disabling and enabling
musb->nIrq on every completion acquires desc->lock twice and programs the
irqchip to mask and unmask the hardware IRQ line on each packet.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261124150344.69264-1-aardelean@taladin.ro?part=2

      reply	other threads:[~2026-11-24 15:23 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 15:03 [PATCH v2 0/2] usb: musb: gadget: fix lost Rx packet on first OUT request Alexandru Ardelean
2026-11-24 15:03 ` [PATCH v2 1/2] Revert "usb: musb: Fix hardware lockup on first Rx endpoint request" Alexandru Ardelean
2026-11-24 15:16   ` sashiko-bot
2026-11-24 15:03 ` [PATCH v2 2/2] usb: musb: gadget: mask the MUSB IRQ while complete() runs Alexandru Ardelean
2026-11-24 15:23   ` 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-231400@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=aardelean@taladin.ro \
    --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