* [PATCH] usb: musb: gadget: mask the MUSB IRQ while complete() runs
@ 2026-11-18 11:54 Alexandru Ardelean
2026-11-18 12:24 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Alexandru Ardelean @ 2026-11-18 11:54 UTC (permalink / raw)
To: linux-usb
Cc: stable, hubert.wisniewski.25632, gregkh, b-liu,
Alexandru Ardelean
This patch is 2-part (in one).
It reverts commit 3fc137386c46 ("usb: musb: Fix hardware lockup on first
Rx endpoint request") and implements the second possible fix suggested
in that commit.
Reverting that commit alone would bring back CVE-2024-56687 which it
addresses.
However, the fix causes a regression in cases where the USB gadget device
exposes itself as USB mass storage device. With that commit,
musb_ep_restart() flushes the RX FIFO instead of reading it. As a
consequence, a packet that came before the OUT request is lost.
With the mass storage gadget the host loses commands (CBW), times out
and resets the device; so, the PC never mounts the drive.
We've found the issue in kernel 6.12.57.
And we've identified the problem by reverting the driver to the 6.1.119
version (where this problem did not exist).
The same commit also offers an alternative fix which works fine for
the USB mass storage case.
i.e.
2. Disable MUSB interrupts in musb_g_giveback() before calling the
callback and re-enable them afterwars (by calling
musb_{dis,en}able_interrupts(), for example). This would ensure
that MUSB interrupts are not handled during the callback, even if
IRQs are enabled. In fact, it would allow IRQs to be enabled when
releasing the lock. However, this feels like an inelegant hack.
This change also implements that fix.
Fixes: 3fc137386c46 ("usb: musb: Fix hardware lockup on first Rx endpoint request")
Signed-off-by: Alexandru Ardelean <aardelean@taladin.ro>
---
drivers/usb/musb/musb_gadget.c | 16 ++++++----------
1 file changed, 6 insertions(+), 10 deletions(-)
diff --git a/drivers/usb/musb/musb_gadget.c b/drivers/usb/musb/musb_gadget.c
index 016d3f3fc1e05..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;
}
@@ -1162,19 +1165,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)
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] usb: musb: gadget: mask the MUSB IRQ while complete() runs
2026-11-18 11:54 [PATCH] usb: musb: gadget: mask the MUSB IRQ while complete() runs Alexandru Ardelean
@ 2026-11-18 12:24 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-18 12:24 UTC (permalink / raw)
To: Alexandru Ardelean, linux-usb
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] usb: musb: gadget: premature IRQ unmask in musb_g_giveback()
inside ISR
- [High] usb: musb: gadget: IRQ state corruption and self-deadlock in
musb_g_giveback()
- [High] usb: musb: gadget: musb_ep_restart() runs complete() inside
usb_ep_queue()
- [High] usb: musb: gadget: RX endpoint lockup from unstarted requests
and IRQ race
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [High] usb: musb: skipped DMA abort in musb_gadget_dequeue() when
ep->busy is set [1]
--
commit e287a2a9d7df9cb83cc08eb31d403c811c9fb5ba
Author: Alexandru Ardelean <aardelean@taladin.ro>
usb: musb: gadget: mask the MUSB IRQ while complete() runs
This commit reverts the RX FIFO flush in musb_ep_restart() from commit
3fc137386c46 ("usb: musb: Fix hardware lockup on first Rx endpoint
request") to call rxstate() again, and masks musb->nIrq across
usb_gadget_giveback_request() in musb_g_giveback().
> diff --git a/drivers/usb/musb/musb_gadget.c b/drivers/usb/musb/musb_gadget.c
> index 016d3f3fc1e05..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]
Does calling disable_irq_nosync() and enable_irq() on musb->nIrq here
unmask the hardware interrupt line while musb->isr is still running?
Most calls to musb_g_giveback() happen inside musb->isr (via
musb_interrupt() -> musb_g_rx(), musb_g_tx(), and musb_g_ep0_irq()).
When enable_irq(musb->nIrq) drops the disable depth back to 0,
irq_enable() unconditionally unmasks the line at the irqchip and clears
IRQD_IRQ_MASKED.
Under forced IRQ threading (threadirqs or CONFIG_PREEMPT_RT), where
musb->nIrq runs as an IRQS_ONESHOT threaded handler, clearing
IRQD_IRQ_MASKED causes irq_finalize_oneshot() to skip
unmask_threaded_irq() when musb->isr finishes:
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);
...
}
On IRQCHIP_EOI_THREADED interrupt controllers, chip->irq_eoi() is never
called and musb->nIrq is permanently wedged. On level-triggered
non-threaded interrupts, enable_irq() unmasks the line before glue ISRs
(such as tusb_musb_interrupt() or da8xx_musb_interrupt()) clear the
interrupt source after musb_interrupt() returns.
[Severity: High]
When musb_g_giveback() is called from process context via
musb_gadget_queue(), can musb->lock be re-acquired with local CPU
interrupts enabled, leading to a hardirq self-deadlock?
musb_gadget_queue() acquires musb->lock with spin_lock_irqsave(). When
musb_ep_restart() -> rxstate() completes a request immediately,
musb_g_giveback() drops musb->lock with plain spin_unlock(), leaving
local IRQs disabled when req->complete() is entered in process context.
In u_ether's rx_complete() -> netif_rx() -> __local_bh_enable_ip(), this
first triggers lockdep_assert_irqs_enabled(), and then leaves local IRQs
enabled upon return:
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()
rx_complete()
netif_rx()
__local_bh_enable_ip() <-- enables local IRQs
enable_irq(musb->nIrq)
spin_lock(&musb->lock) <-- local IRQs & nIrq enabled
<hardirq: musb->nIrq or dma_controller_irq()>
spin_lock_irqsave(&musb->lock) <-- spins forever
If an interrupt fires after enable_irq() and before ep->busy = busy, a
nested musb_g_giveback() also snapshots ep->busy as 1 and restores it to
1, leaving ep->busy permanently stuck.
[Severity: High]
Can the RX endpoint still lock up when req->complete() queues the next
RX request while ep->busy is set?
While musb_g_giveback() is running from rxstate(), ep->busy is 1. When
req->complete() (such as u_ether's rx_complete() -> rx_submit()) calls
usb_ep_queue(), musb_gadget_queue() adds the new request to
musb_ep->req_list but skips starting it because musb_ep->busy is 1:
drivers/usb/musb/musb_gadget.c:musb_gadget_queue() {
...
/* it this is the head of the queue, start i/o ... */
if (!musb_ep->busy && &request->list == musb_ep->req_list.next) {
status = musb_queue_resume_work(musb,
musb_ep_restart_resume_work,
request);
...
}
When musb_g_giveback() returns and restores ep->busy = 0, neither
rxstate() nor musb_ep_restart() checks next_request(musb_ep) to start
requests queued during the callback. Any further requests queued in a
caller loop (such as rx_fill()) then see &request->list !=
musb_ep->req_list.next and also skip musb_ep_restart().
In addition, musb->lock is dropped before disable_irq_nosync(musb->nIrq),
so on SMP another CPU can handle musb->nIrq in that window while
musb_ep->req_list is empty, clear the hardware interrupt flag, and leave
MUSB_RXCSR_RXPKTRDY set with no started request.
[Severity: High]
This problem wasn't introduced by this patch, but can [1]
musb_gadget_dequeue() skip aborting an active DMA transfer when called
while ep->busy is set in musb_g_giveback()?
drivers/usb/musb/musb_gadget.c:musb_gadget_dequeue() {
...
/* if the hardware doesn't have the request, easy ... */
if (musb_ep->req_list.next != &req->list || musb_ep->busy)
musb_g_giveback(musb_ep, request, -ECONNRESET);
/* ... else abort the dma transfer ... */
else if (is_dma_capable() && musb_ep->dma) {
...
}
musb_g_giveback() sets ep->busy = 1 before dropping musb->lock even when
giving back a non-head request that was never handed to hardware:
usb_ep_dequeue(ep, req2) /* req1 active in DMA at head */
musb_gadget_dequeue()
musb_g_giveback(musb_ep, req2, ...)
ep->busy = 1
spin_unlock(&musb->lock)
usb_gadget_giveback_request(..., req2)
...
usb_ep_dequeue(ep, req1)
musb_gadget_dequeue()
if (... || musb_ep->busy)
musb_g_giveback(musb_ep, req1, ...)
unmap_dma_buffer(req1, musb) <-- DMA still active
The same happens if another CPU dequeues req1 while musb_g_giveback()
has dropped musb->lock. Skipping channel_abort() unmaps req1's buffer
and completes the request while the DMA controller is still accessing it.
> @@ -1162,19 +1165,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);
> }
[Severity: High]
Does calling rxstate() from musb_ep_restart() invoke req->complete()
synchronously inside usb_ep_queue() when an RX packet is already waiting
in the FIFO?
In PIO mode, rxstate() unloads the waiting packet and calls
musb_g_giveback() -> usb_gadget_giveback_request() -> req->complete()
before musb_gadget_queue() and usb_ep_queue() return. This violates the
usb_ep_queue() contract (which forbids calling ->complete() from within
usb_ep_queue()) and breaks callers that update state or touch the
request after usb_ep_queue() returns, such as trace_usb_ep_queue() in
usb_ep_queue() or bot_enqueue_cmd_cbw():
drivers/usb/gadget/function/f_tcm.c:bot_enqueue_cmd_cbw() {
...
ret = usb_ep_queue(fu->ep_out, fu->cmd[0].req, GFP_ATOMIC);
if (!ret)
fu->flags |= USBG_BOT_CMD_PEND;
...
}
If bot_cmd_complete() runs inside usb_ep_queue(), it clears
USBG_BOT_CMD_PEND, and bot_enqueue_cmd_cbw() then sets USBG_BOT_CMD_PEND
again on return, stalling subsequent CBW submissions.
[1] https://sashiko.dev/#/bug/linux-8787b453-429b-40d3-8db7-9c949b0a6861
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118115435.49748-1-aardelean@taladin.ro?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-18 12:24 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-18 11:54 [PATCH] usb: musb: gadget: mask the MUSB IRQ while complete() runs Alexandru Ardelean
2026-11-18 12:24 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox