Skip to contentSkip to conversation

usb: musb: gadget: fix lost Rx packet on first OUT request

PATCH New Alexandru Ardelean 2 participants 5 messages Nov 24

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover usb: musb: gadget: fix lost Rx packet on first OUT request

Commit 3fc137386c46 ("usb: musb: Fix hardware lockup on first Rx endpoint request") [1] made musb_ep_restart() flush the Rx FIFO instead of calling rxstate(). A packet received before the first OUT request is queued is then dropped. With the mass storage gadget that packet is the first command (CBW): the host times out and resets the device, and the drive never gets mounted. Seen on 6.12.57; version 6.1.119 (before the stable backport) works.

Patch 1 reverts that commit. Patch 2 fixes the original lockup the second way suggested in that commit: the MUSB IRQ is masked while the request's complete() callback runs.

Changelog v1 -> v2:
- Split the single patch into a revert and a separate fix, as requested by Greg [3].
- Reworded both commit messages; no code changes. The combined diff is identical to v1.

v1: [2]

[1] https://lore.kernel.org/r/[email protected]
[2] https://lore.kernel.org/linux-usb/[email protected]/
[3] https://lore.kernel.org/linux-usb/2026112008-dean-crabbing-a331@gregkh/

Alexandru Ardelean (2):
Revert "usb: musb: Fix hardware lockup on first Rx endpoint request"
usb: musb: gadget: mask the MUSB IRQ while complete() runs

 drivers/usb/musb/musb_gadget.c | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/2 Revert "usb: musb: Fix hardware lockup on first Rx endpoint request"

This reverts commit 3fc137386c4620305bbc2a216868c53f9245670a.

That commit made musb_ep_restart() flush the Rx FIFO instead of calling rxstate(), so a packet received before the first OUT request is queued gets dropped. The commit considered such a loss harmless, but the mass storage gadget has no retransmission: the host loses the first command (CBW), times out and resets the device, so the drive never gets mounted.

The regression was seen on 6.12.57; 6.1.119 (before the stable backport of that commit) works fine.

This brings back the Rx endpoint lockup that the reverted commit fixed; the next patch addresses it in a different way.

Fixes 3fc137386c46 ("usb: musb: Fix hardware lockup on first Rx endpoint request")
---
 drivers/usb/musb/musb_gadget.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)
drivers/usb/musb/musb_gadget.c +3 -10
index 016d3f3fc1e05..dd67ab97df9d8 100644
@@ -1162,19 +1162,12 @@ void musb_free_request(struct usb_ep *ep, struct usb_request *req)
  */
 void musb_ep_restart(struct musb *musb, struct musb_request *req)
 {
-	u16 csr;
-	void __iomem *epio = req->ep->hw_ep->regs;
-
 	trace_musb_req_start(req);
 	musb_ep_select(musb->mregs, req->epnum);
-	if (req->tx) {
+	if (req->tx)
 		txstate(musb, req);
-	} else {
-		csr = musb_readw(epio, MUSB_RXCSR);
-		csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_P_WZC_BITS;
-		musb_writew(epio, MUSB_RXCSR, csr);
-		musb_writew(epio, MUSB_RXCSR, csr);
-	}
+	else
+		rxstate(musb, req);
 }
 
 static int musb_ep_restart_resume_work(struct musb *musb, void *data)
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v2 1/2] Revert "usb: musb: Fix hardware lockup on first Rx endpoint request"

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] usb: musb: lock inversion in musb_ep_restart() via musb_run_resume_work() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] usb: musb: lock inversion in musb_ep_restart() via musb_run_resume_work()
--

commit d72e1bdec5562f8d7f92be2dcf471e3639a440ee
Author: Alexandru Ardelean <[email protected]>

Revert "usb: musb: Fix hardware lockup on first Rx endpoint request"

This commit reverts commit 3fc137386c46 ("usb: musb: Fix hardware lockup on first Rx endpoint request"), restoring the call to rxstate() in musb_ep_restart() for RX requests instead of flushing the Rx FIFO.

Quoted 15 lines
drivers/usb/musb/musb_gadget.c +1 -4
index 016d3f3fc1e05..dd67ab97df9d8 100644
@@ -1162,19 +1162,12 @@ void musb_free_request(struct usb_ep *ep, struct usb_request *req)
  */
 void musb_ep_restart(struct musb *musb, struct musb_request *req)
 {
-	u16 csr;
-	void __iomem *epio = req->ep->hw_ep->regs;
-
 	trace_musb_req_start(req);
 	musb_ep_select(musb->mregs, req->epnum);
-	if (req->tx) {
+	if (req->tx)

drivers/usb/musb/musb_gadget.c void musb_free_request(struct usb_ep *ep, struct usb_request *req) · line 1171

Quoted 10 lines
drivers/usb/musb/musb_gadget.c +2 -6
 		txstate(musb, req);
-	} else {
-		csr = musb_readw(epio, MUSB_RXCSR);
-		csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_P_WZC_BITS;
-		musb_writew(epio, MUSB_RXCSR, csr);
-		musb_writew(epio, MUSB_RXCSR, csr);
-	}
+	else
+		rxstate(musb, req);
 }

[Severity: Medium]
Can calling rxstate() from musb_ep_restart() invert the musb->lock -> musb->list_lock lock order and deadlock when invoked via musb_run_resume_work()?

When an RX request is queued while runtime suspended, musb_queue_resume_work() queues musb_ep_restart_resume_work() on musb->pending_list. On resume, musb_run_resume_work() runs with musb->lock and musb->list_lock held. If rxstate() completes the request synchronously (for example, a zero-length RX request or a PIO read that satisfies the transfer), it calls musb_g_giveback(), which drops and re-acquires musb->lock while musb->list_lock remains held:

CPU0 CPU1
---- ----
musb_runtime_resume()
lock(&musb->lock)
musb_run_resume_work()

    lock(&musb->list_lock)
    musb_ep_restart_resume_work()
      musb_ep_restart()
        rxstate()
          musb_g_giveback()
            unlock(&musb->lock)
            usb_gadget_giveback_request()
                                      musb_gadget_queue()
                                        lock(&musb->lock)
                                        musb_queue_resume_work()
                                          lock(&musb->list_lock)
            lock(&musb->lock)

*** DEADLOCK ***

If the gadget's completion callback instead re-queues a request via usb_ep_queue() -> musb_gadget_queue() -> musb_queue_resume_work(), the same CPU also deadlocks trying to acquire musb->list_lock recursively. This remains present after the follow-up commit "usb: musb: gadget: mask the MUSB IRQ while complete() runs".

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

PATCH v2 2/2 usb: musb: gadget: mask the MUSB IRQ while complete() runs

In reply to Alexandru Ardelean

A request's complete() callback can enable IRQs (e.g. u_ether's rx_complete() calls netif_rx(), which re-enables IRQs when re-enabling BHs). If that happens while the endpoint's request queue is empty, the MUSB interrupt is handled with no request to take the packet, the interrupt flag is cleared but RXPKTRDY stays set, and the Rx endpoint locks up.

Commit 3fc137386c46 ("usb: musb: Fix hardware lockup on first Rx endpoint request") listed several ways to fix this. Its second option was to keep MUSB interrupts from being handled while the callback runs. Implement that by masking the controller IRQ line around usb_gadget_giveback_request(), instead of flushing the Rx FIFO (which loses packets and was reverted).

disable_irq_nosync() is used because musb_g_giveback() is usually called from the MUSB interrupt handler itself.

Fixes baebdf48c360 ("net: dev: Makes sure netif_rx() can be invoked in any context.")
---
 drivers/usb/musb/musb_gadget.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/usb/musb/musb_gadget.c +3 -0
index dd67ab97df9d8..5cb7eb9c56de0 100644
@@ -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;
 }
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH v2 2/2] usb: musb: gadget: mask the MUSB IRQ while complete() runs

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…

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 <[email protected]>

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.

Quoted 5 lines
drivers/usb/musb/musb_gadget.c +0 -0
index dd67ab97df9d8..5cb7eb9c56de0 100644

drivers/usb/musb/musb_gadget.c __acquires(ep->musb->lock) · line 154

Quoted 10 lines
drivers/usb/musb/musb_gadget.c +3 -0
@@ -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.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2