* [PATCH] usb: musb: gadget: don't flush a waiting RX packet when queueing a request
@ 2026-10-27 20:27 Elvira Khabirova
0 siblings, 0 replies; only message in thread
From: Elvira Khabirova @ 2026-10-27 20:27 UTC (permalink / raw)
To: Bin Liu, Greg Kroah-Hartman
Cc: Hubert Wiśniewski, linux-usb, linux-kernel, Elvira Khabirova
Since commit 3fc137386c46 ("usb: musb: Fix hardware lockup on first Rx
endpoint request"), musb_ep_restart() flushes the RX FIFO instead of
calling rxstate(), with the premise that in case of Ethernet, dropping
a couple of packets during gadget initialization is not an issue,
because it will only happen when the software is not yet ready.
But when using other functions with musb, for example, f_mass_storage
or f_acm, this code path will be used not only when the software is not
yet ready, but also mid-communication, and it will cause data loss.
Data loss happens on gadgets that don't always have requests queued.
The timeline of a data loss:
1. A function that does not have a lot of requests pre-queued
completes an OUT request. The queue is empty.
2. The next packet comes. MUSB auto-acks it, places it into the FIFO,
sets RXPKTRDY, raises an interrupt.
3. musb_g_rx() is called, sees the request queue is empty,
early returns.
4. The function queues the next request from the main fsg thread,
musb_ep_restart does FLUSHFIFO, packet is lost.
5. The packet was auto-acked by MUSB, so it is not resent by the host.
Next data chunk arrives with an IRQ like so:
musb_g_rx returns, queue empty
(drivers/usb/musb/musb_gadget.c:810)
musb_g_rx from musb_interrupt
(drivers/usb/musb/musb_core.c:1786)
musb_interrupt from dsps_interrupt
(drivers/usb/musb/musb_dsps.c:389)
and then it is dropped from the FIFO when usb_ep_queue() is called:
MUSB_RXCSR_FLUSHFIFO from musb_ep_restart
(drivers/usb/musb/musb_gadget.c:1174)
musb_ep_restart from musb_ep_restart_resume_work
(drivers/usb/musb/musb_gadget.c:1184)
musb_ep_restart_resume_work from musb_queue_resume_work
(drivers/usb/musb/musb_core.c:2277)
musb_queue_resume_work from musb_gadget_queue
(drivers/usb/musb/musb_gadget.c:1249)
musb_gadget_queue from usb_ep_queue
(drivers/usb/gadget/udc/core.c:306)
In case of f_mass_storage, it keeps waiting for the lost data;
the host then times out and resets the device. Since this is a race
condition, this can be seen as data transfers taking too long,
or as drives getting mounted but disappearing when data
is read/written, and so on.
usb 1-8.4: reset high-speed USB device number 31 using xhci_hcd
I/O error, dev sdb, sector 240 op 0x1:(WRITE) flags 0x804800 phys_seg 30 prio class 2
Buffer I/O error on dev sdb, logical block 30, lost async page write
When testing f_acm, this is seen as bytes loss on the tty.
Similar situation with f_hid, f_printer, f_fs.
Testing with commit 3fc137386c46 ("usb: musb: Fix hardware lockup on
first Rx endpoint request") reverted fixed these symptoms.
Move the data processing to system_bh_wq. Purpose of doing so is to run
in an interrupt context, honoring the usb_ep_queue docstring and avoiding
the lockup Hubert described in his commit message.
Alternative way of looking at this is it "re-triggers" the interrupt,
running the musb_g_rx that had to be skipped.
This issue was reproduced and the patch was tested on a BeagleBone Black
with f_mass_storage, f_acm, f_hid, f_printer, and f_fs.
Fixes: 3fc137386c46 ("usb: musb: Fix hardware lockup on first Rx endpoint request")
Signed-off-by: Elvira Khabirova <lineprinter0@gmail.com>
---
drivers/usb/musb/musb_gadget.c | 54 ++++++++++++++++++++++++++++++++--
drivers/usb/musb/musb_gadget.h | 3 ++
2 files changed, 54 insertions(+), 3 deletions(-)
diff --git a/drivers/usb/musb/musb_gadget.c b/drivers/usb/musb/musb_gadget.c
index 016d3f3fc1e0..ace1a99999ab 100644
--- a/drivers/usb/musb/musb_gadget.c
+++ b/drivers/usb/musb/musb_gadget.c
@@ -1111,6 +1111,8 @@ static int musb_gadget_disable(struct usb_ep *ep)
/* abort all pending DMA and requests */
nuke(musb_ep, -ESHUTDOWN);
+ if (cancel_work(&musb_ep->rx_kick))
+ pm_runtime_put_autosuspend(musb->controller);
musb_ep->desc = NULL;
musb_ep->end_point.desc = NULL;
@@ -1171,10 +1173,42 @@ void musb_ep_restart(struct musb *musb, struct musb_request *req)
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);
+ if (csr & MUSB_RXCSR_RXPKTRDY) {
+ pm_runtime_get_noresume(musb->controller);
+ if (!queue_work(system_bh_wq, &req->ep->rx_kick))
+ pm_runtime_put_noidle(musb->controller);
+ }
+ }
+}
+
+static void musb_g_rx_kick(struct work_struct *work)
+{
+ struct musb_ep *musb_ep = container_of(work, struct musb_ep, rx_kick);
+ struct musb *musb = musb_ep->musb;
+ unsigned long flags;
+ u8 epnum = musb_ep->current_epnum;
+ bool requeued = false;
+ u16 csr;
+
+ spin_lock_irqsave(&musb->lock, flags);
+
+ if (!musb_ep->desc)
+ goto out;
+
+ if (musb_ep->busy) {
+ requeued = queue_work(system_bh_wq, &musb_ep->rx_kick);
+ goto out;
}
+
+ musb_ep_select(musb->mregs, epnum);
+ csr = musb_readw(musb->endpoints[epnum].regs, MUSB_RXCSR);
+ if (csr & MUSB_RXCSR_RXPKTRDY)
+ musb_g_rx(musb, epnum);
+out:
+ spin_unlock_irqrestore(&musb->lock, flags);
+
+ if (!requeued)
+ pm_runtime_put_autosuspend(musb->controller);
}
static int musb_ep_restart_resume_work(struct musb *musb, void *data)
@@ -1701,6 +1735,7 @@ init_peripheral_ep(struct musb *musb, struct musb_ep *ep, u8 epnum, int is_in)
ep->is_in = is_in;
INIT_LIST_HEAD(&ep->req_list);
+ INIT_WORK(&ep->rx_kick, musb_g_rx_kick);
sprintf(ep->name, "ep%d%s", epnum,
(!epnum || hw_ep->is_shared_fifo) ? "" : (
@@ -1805,11 +1840,24 @@ int musb_gadget_setup(struct musb *musb)
void musb_gadget_cleanup(struct musb *musb)
{
+ int i;
+
if (musb->port_mode == MUSB_HOST)
return;
cancel_delayed_work_sync(&musb->gadget_work);
usb_del_gadget_udc(&musb->g);
+
+ for (i = 0; i < musb->nr_endpoints; i++) {
+ struct musb_hw_ep *hw_ep = &musb->endpoints[i];
+
+ if (hw_ep->ep_in.rx_kick.func &&
+ cancel_work_sync(&hw_ep->ep_in.rx_kick))
+ pm_runtime_put_noidle(musb->controller);
+ if (hw_ep->ep_out.rx_kick.func &&
+ cancel_work_sync(&hw_ep->ep_out.rx_kick))
+ pm_runtime_put_noidle(musb->controller);
+ }
}
/*
diff --git a/drivers/usb/musb/musb_gadget.h b/drivers/usb/musb/musb_gadget.h
index f49f25b3bf56..334e6f9220ac 100644
--- a/drivers/usb/musb/musb_gadget.h
+++ b/drivers/usb/musb/musb_gadget.h
@@ -11,6 +11,7 @@
#define __MUSB_GADGET_H
#include <linux/list.h>
+#include <linux/workqueue.h>
#if IS_ENABLED(CONFIG_USB_MUSB_GADGET) || IS_ENABLED(CONFIG_USB_MUSB_DUAL_ROLE)
extern irqreturn_t musb_g_ep0_irq(struct musb *);
@@ -93,6 +94,8 @@ struct musb_ep {
/* true if lock must be dropped but req_list may not be advanced */
u8 busy;
+ struct work_struct rx_kick;
+
u8 hb_mult;
};
base-commit: 40288c9206c17eb66a603262e06a58d300d0f279
--
2.55.0
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-10-27 20:28 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-27 20:27 [PATCH] usb: musb: gadget: don't flush a waiting RX packet when queueing a request Elvira Khabirova
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®