From: sashiko-bot@kernel.org
To: "Vadim Yuzifovich" <vadim@yuzi.co>
Cc: linux-usb@vger.kernel.org
Subject: Re: [PATCH v4 1/2] usb: dwc2: gadget: restart descriptor DMA isochronous OUT endpoints after BNA
Date: Sat, 05 Dec 2026 13:31:46 +0000 [thread overview]
Message-ID: <sashiko-outbox-251570@kernel.org> (raw)
In-Reply-To: <20261205130225.341741-2-vadim@yuzi.co>
Thank you for your contribution! Sashiko AI review found 12 potential
issues to consider:
- [High] usb: dwc2: unbounded BNA IRQ storm when queuing with
isoc_out_drain_busy
- [High] usb: dwc2: ep_disable() skips Rx FIFO drain before isoc_out_bna
is set
- [Medium] usb: dwc2: dwc2_gadget_isoc_out_drain() ignores budget when
desc 0 busy
- [Medium] usb: dwc2: drain budget exit leaves stopped isoc OUT endpoint
unmasked
- [Medium] usb: dwc2: missing EPENA check in
dwc2_gadget_handle_out_token_ep_disabled()
- [Medium] usb: dwc2: dwc2_gadget_start_isoc_ddma() marks desc 1..N-1
ready before DOEPDMA
- [Medium] usb: dwc2: dma_wmb() does not flush posted DOEPDMA write
before HREADY/udelay
- [Medium] usb: dwc2: dwc2_gadget_start_isoc_ddma() exposes desc 1..N-1
before restart
- [Medium] usb: dwc2: missing write barrier before polling desc->status
after HREADY
- [Medium] usb: dwc2: gadget: race between DXEPCTL_SNAK and in-flight
ISOC OUT drain
- [Medium] usb: dwc2: dwc2_gadget_isoc_out_drain() budget ignores fast
DMA completions
- [Low] usb: dwc2: trailing udelay(1) in
dwc2_gadget_isoc_out_wait_packet()
--
commit 80d6fd01c9d63259ec27d838061a3b843634e06e
Author: Vadim Yuzifovich <vadim@yuzi.co>
usb: dwc2: gadget: restart descriptor DMA isochronous OUT endpoints after BNA
This commit restarts descriptor DMA isochronous OUT endpoints directly from
the BNA and endpoint-disabled handlers instead of flushing the Rx FIFO,
waits for any held packet to land on descriptor 0, and drains held packets
into a scratch buffer when no requests are queued or when the endpoint is
disabled.
> diff --git a/drivers/usb/dwc2/gadget.c b/drivers/usb/dwc2/gadget.c
> index 7ef704568e090..fdbc9677aef29 100644
> --- a/drivers/usb/dwc2/gadget.c
> +++ b/drivers/usb/dwc2/gadget.c
> @@ -218,6 +218,53 @@ static void dwc2_hsotg_ctrl_epint(struct dwc2_hsotg *hsotg,
> local_irq_restore(flags);
> }
>
> +static void dwc2_gadget_isoc_out_stop(struct dwc2_hsotg_ep *hs_ep)
> +{
> + struct dwc2_hsotg *hsotg = hs_ep->parent;
> + u32 epctl_reg = DOEPCTL(hs_ep->index);
> +
> + hs_ep->isoc_out_stopped = 1;
> + dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, 0, 0);
> + dwc2_writel(hsotg, dwc2_readl(hsotg, epctl_reg) | DXEPCTL_SNAK,
> + epctl_reg);
> +}
[Severity: Medium]
Can setting DXEPCTL_SNAK without waiting for an in-flight OUT packet to
finish arriving leave a late packet stuck in the Rx FIFO?
Unlike dwc2_hsotg_ep_stop_xfr(), both dwc2_gadget_isoc_out_stop() and
dwc2_hsotg_ep_disable() only write DXEPCTL_SNAK and immediately call
dwc2_gadget_isoc_out_drain(). Setting SNAK rejects future OUT tokens, but
a packet already on the wire (up to ~17 us at High-Speed or ~682 us at
Full-Speed) does not become visible in the Rx FIFO until EOP.
If the 10 us probe in dwc2_gadget_isoc_out_drain() finishes first, it
clears isoc_out_bna and returns with the endpoint masked. The late packet
then sits stuck at the head of the shared Rx FIFO, stalling EP0 and other
OUT endpoints, and dwc2_hsotg_ep_disable() will skip draining it because
isoc_out_bna is already 0. Asserting DCTL_SGOUTNAK and waiting for
GINTSTS_GOUTNAKEFF before draining would ensure any in-flight packet has
landed first.
[ ... ]
> +static int dwc2_gadget_isoc_out_wait_packet(struct dwc2_hsotg_ep *hs_ep,
> + unsigned int *waited)
> +{
> + struct dwc2_dma_desc *desc = &hs_ep->desc_list[0];
> + int i;
> +
> + for (i = 0; i < 10; i++) {
> + if (READ_ONCE(desc->status) >> DEV_DMA_BUFF_STS_SHIFT !=
> + DEV_DMA_BUFF_STS_HREADY)
> + break;
> + udelay(1);
> + }
> + *waited += i;
> + if (i == 10)
> + return 0;
> +
> + for (i = 0; i < 100; i++) {
> + if (READ_ONCE(desc->status) >> DEV_DMA_BUFF_STS_SHIFT ==
> + DEV_DMA_BUFF_STS_DMADONE)
> + break;
> + udelay(1);
> + }
> + *waited += i;
> + if (i < 100)
> + return 1;
> +
> + dev_warn_ratelimited(hs_ep->parent->dev,
> + "%s: held packet not written in 100 us\n",
> + hs_ep->ep.name);
> + return -ETIMEDOUT;
> +}
[Severity: Low]
Should both polling loops re-check desc->status after the final udelay(1)?
On the last iteration each loop sleeps 1 us without checking whether
desc->status changed during that microsecond. In the first loop, if the
core claims descriptor 0 during that trailing delay, returning 0 causes
dwc2_gadget_isoc_out_drain() to overwrite desc->status with HBUSY and clear
isoc_out_bna while DMA owns it; in the second loop, a packet completing in
the 100th microsecond is misreported as -ETIMEDOUT. Using
read_poll_timeout_atomic() or checking desc->status once more after each
loop avoids the unchecked window.
[ ... ]
> +static void dwc2_gadget_isoc_out_drain(struct dwc2_hsotg_ep *hs_ep,
> + unsigned int budget)
> +{
> + struct dwc2_hsotg *hsotg = hs_ep->parent;
> + struct dwc2_dma_desc *desc = &hs_ep->desc_list[0];
> + u32 len = min_t(u32, hs_ep->ep.maxpacket, DWC2_ISOC_OUT_DRAIN_SIZE);
> + u32 hbusy = DEV_DMA_BUFF_STS_HBUSY << DEV_DMA_BUFF_STS_SHIFT;
> + unsigned int waited = 0;
> + u32 mask = 0;
> + int ret;
> +
> + if (!hs_ep->isoc_out_drain)
> + return;
> + if (!dwc2_gadget_isoc_out_drain_settled(hs_ep)) {
> + dwc2_gadget_isoc_out_drain_retry(hs_ep);
> + return;
> + }
[Severity: Medium]
Does dwc2_gadget_isoc_out_drain() abandon the caller's budget too early
when descriptor 0 is already busy or a packet times out?
dwc2_gadget_isoc_out_drain_settled() does a single non-waiting read of
desc->status, so if isoc_out_drain_busy is already set,
dwc2_gadget_isoc_out_drain() returns after 0 us even when
dwc2_hsotg_ep_disable() passes the 2000 us DWC2_ISOC_OUT_DRAIN_DISABLE_US
budget. A single 100 us timeout from dwc2_gadget_isoc_out_wait_packet()
below also returns immediately without using the remaining disable budget.
During dwc2_hsotg_ep_disable(), dwc2_gadget_isoc_out_drain_retry() then
needlessly unmasks DAINTMSK right before disable masks it again, and
dwc2_gadget_bna_state_reset() overwrites desc_list[0].status with HBUSY
while hardware DMA may still own descriptor 0 and packets remain stuck in
the Rx FIFO.
> + dwc2_gadget_get_desc_params(hs_ep, &mask);
> + /* A packet may take 110 us: do not start one past the budget. */
> + while (waited + 110 <= budget) {
> + desc->buf = hs_ep->isoc_out_drain_dma;
> + WRITE_ONCE(desc->status, hbusy | DEV_DMA_L |
> + ((len << DEV_DMA_NBYTES_SHIFT) & mask));
> + dwc2_writel(hsotg, hs_ep->desc_list_dma, DOEPDMA(hs_ep->index));
> + dma_wmb();
> + WRITE_ONCE(desc->status, (desc->status & ~DEV_DMA_BUFF_STS_MASK) |
> + DEV_DMA_BUFF_STS_HREADY << DEV_DMA_BUFF_STS_SHIFT);
[Severity: Medium]
Is dma_wmb() sufficient to flush the posted write to DOEPDMA before
descriptor 0 is marked HREADY and the 10 us wait begins?
dma_wmb() provides no ordering guarantees for MMIO writes (for example on
arm64, arm, or PCI/x86), and writeX() is not ordered before a subsequent
udelay() loop. The same pattern appears in dwc2_gadget_start_isoc_ddma().
If the posted DOEPDMA write has not reached the controller before the 10 us
poll runs, the core will not claim descriptor 0 in time, causing
dwc2_gadget_isoc_out_drain() to clear isoc_out_bna with a packet still
stuck in the Rx FIFO, or dwc2_gadget_start_isoc_ddma() to enable the chain
with a held packet pending. Reading back DOEPDMA with dwc2_readl() before
marking descriptor 0 HREADY flushes the posted write.
> + ret = dwc2_gadget_isoc_out_wait_packet(hs_ep, &waited);
[Severity: Medium]
Can fast DMA completions allow dwc2_gadget_isoc_out_drain() to spin past
its 200 us hardirq budget?
dwc2_gadget_isoc_out_wait_packet() only adds its udelay(1) loop counts to
*waited, so any packet that completes before the first udelay(1) adds 0 us,
ignoring coherent descriptor accesses, MMIO writes, and barriers. With a
default 2048-DWORD Rx FIFO able to buffer hundreds of short or zero-length
packets, draining a backlog of fast-completing packets can spin with
hsotg->lock held and interrupts disabled into the millisecond range.
Bounding the loop with a ktime_get() deadline would enforce the intended
budget.
[ ... ]
> + if (hs_ep->isoc_out_stopped)
> + dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, 0, 1);
> +}
[Severity: Medium]
Can unmasking the stopped endpoint on budget exit leave it permanently
unmasked if the last held packet was just drained?
If draining the last held packet takes more than 90 us, the loop exits with
waited + 110 > budget without seeing ret == 0, leaving isoc_out_bna set and
unmasking DAINTMSK without any retry cap. Because no held packet remains,
the core raises OUTTKNEPDIS on every subsequent host ISOC OUT token instead
of BNA.
dwc2_gadget_handle_out_token_ep_disabled() then calls
dwc2_gadget_start_isoc_ddma(), which sees an empty queue with again == false
and returns without re-masking DAINTMSK or clearing isoc_out_bna, leaving
the endpoint taking continuous OUTTKNEPDIS hardirqs until a request is
queued or the endpoint is disabled.
[ ... ]
> static void dwc2_gadget_start_isoc_ddma(struct dwc2_hsotg_ep *hs_ep)
> {
> [ ... ]
> + /*
> + * Descriptor 0 still belongs to the core after a drain that timed
> + * out. Try again on a later OUT token or BNA interrupt.
> + */
> + if (!hs_ep->dir_in && !dwc2_gadget_isoc_out_drain_settled(hs_ep)) {
> + hs_ep->target_frame = TARGET_FRAME_INITIAL;
> + return;
> + }
[Severity: High]
Can queuing a request while isoc_out_drain_busy is set trigger an unbounded
BNA interrupt storm?
When a drain times out and exhausts DWC2_ISOC_OUT_DRAIN_TRIES, the endpoint
is left stopped with interrupts masked. If the function driver later
queues a request, dwc2_hsotg_ep_queue() calls dwc2_gadget_isoc_out_unstop(),
which clears isoc_out_stopped and unmasks DAINTMSK even though
isoc_out_drain_busy is still set.
On the next BNA, dwc2_gadget_isoc_out_bna_reset() sees a non-empty queue
and calls dwc2_gadget_restart_isoc_out_ddma() ->
dwc2_gadget_start_isoc_ddma(), which hits this early return without
re-masking interrupts or checking isoc_out_drain_tries. Because the
endpoint stays disabled and unmasked while the core holds a packet, BNA
fires as fast as dwc2_hsotg_epint() clears DOEPINT, starving the CPU and
bypassing the DWC2_ISOC_OUT_DRAIN_TRIES safeguard.
[ ... ]
> ret = dwc2_gadget_fill_isoc_desc(hs_ep, dma_addr,
> - hs_req->req.length);
> - if (ret)
> + hs_req->req.length,
> + !hs_ep->next_desc);
> + /*
> + * Stop when the chain is full and next_desc has wrapped:
> + * descriptor 0 is still host busy here, so the next call
> + * would not see the chain as full and would fill it again.
> + */
> + if (ret || !hs_ep->next_desc)
> break;
> }
[Severity: Medium]
Can marking descriptors 1..N-1 host ready before resetting DOEPDMA let the
core claim a descriptor other than descriptor 0 for the held packet?
Only descriptor 0 is held HBUSY in this loop; descriptors 1..N-1 are marked
HREADY before DOEPDMA is written with hs_ep->desc_list_dma. If a prior BNA
stopped at descriptor k >= 1 (for instance after the 100 us held-packet
timeout fallback where descriptor 0 has L cleared, or during the window in
dwc2_gadget_fill_isoc_desc() between clearing L on descriptor k - 1 and
marking descriptor k HREADY), the polling DMA engine can claim descriptor k
while the next iteration clears DEV_DMA_L on it via a non-atomic
read-modify-write.
The held packet then lands in request k instead of request 0, races with
the L-bit clear, and is missed when dwc2_gadget_isoc_out_held_packet()
polls descriptor 0. Writing and flushing DOEPDMA after the HBUSY
initialization loop and before filling descriptors 1..N-1 avoids this
window.
> hs_ep->compl_desc = 0;
> depctl = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
> dma_reg = hs_ep->dir_in ? DIEPDMA(index) : DOEPDMA(index);
>
> /* write descriptor chain address to control register */
> dwc2_writel(hsotg, hs_ep->desc_list_dma, dma_reg);
>
> + desc = &hs_ep->desc_list[0];
> + dma_wmb();
> + WRITE_ONCE(desc->status, (desc->status & ~DEV_DMA_BUFF_STS_MASK) |
> + DEV_DMA_BUFF_STS_HREADY << DEV_DMA_BUFF_STS_SHIFT);
[Severity: Medium]
Is a write barrier needed after marking descriptor 0 HREADY before polling
desc->status in dwc2_gadget_isoc_out_wait_packet()?
Both here and in dwc2_gadget_isoc_out_drain(), dma_wmb() precedes the
HREADY store to coherent DMA memory, but no wmb()/mb() or MMIO write
follows it before the 10 us udelay(1) polling loop. On platforms with
write-combining coherent DMA memory or outer cache store buffers (such as
ARMv7 with PL310, arm64, RISC-V, or MIPS), the HREADY store can remain
buffered while the CPU polls.
If dwc2_gadget_isoc_out_wait_packet() falsely returns 0 as a result,
dwc2_gadget_start_isoc_ddma() enables the chain with a held packet pending
(reintroducing per-packet BNAs), and dwc2_gadget_isoc_out_drain() clears
isoc_out_bna while packets remain stuck in the Rx FIFO.
> + if (!hs_ep->dir_in && hs_ep->isoc_out_bna && !again) {
> + unsigned int count = hs_ep->disable_count;
> +
> + if (dwc2_gadget_isoc_out_held_packet(hs_ep)) {
> + /*
> + * dwc2_hsotg_ep_disable() may have run while the lock
> + * was dropped for the giveback, and the endpoint may
> + * have been enabled again since.
> + */
> + if (hs_ep->disable_count != count)
> + return;
> + again = true;
> + goto restart;
> + }
> + }
[Severity: Medium]
Can exposing descriptors 1..N-1 as HREADY with L cleared on descriptor 0
corrupt the chain when goto restart wipes all descriptors?
With at least two queued requests and multiple packets buffered in the Rx
FIFO (which a disabled ISOC OUT endpoint keeps accepting until NAKed),
filling descriptor 1 clears DEV_DMA_L on descriptor 0 and marks descriptor
1 HREADY. While dwc2_gadget_isoc_out_held_packet() drops hsotg->lock to
give back request 0, the core can advance into descriptor 1 for a second
buffered packet. dwc2_gadget_complete_isoc_request_ddma() stops as soon as
descriptor 1 is DMABUSY (or not yet DMADONE), and goto restart then
unconditionally overwrites all descriptors with HBUSY and refills them.
A subsequent DMA writeback to descriptor 1 can clobber the newly refilled
descriptor status or silently drop the second packet. Even on a core that
stops after descriptor 0, the !again guard handles only one held packet,
leaving a second buffered packet to trigger BNA as soon as EPENA is set.
[ ... ]
> @@ -2947,6 +3477,15 @@ static void dwc2_gadget_handle_out_token_ep_disabled(struct dwc2_hsotg_ep *ep)
> /* Start first ISO Out */
> ep->target_frame = hsotg->frame_number;
> dwc2_gadget_start_isoc_ddma(ep);
> + } else if (!(dwc2_readl(hsotg, DOEPCTL(ep->index)) &
> + DXEPCTL_EPENA)) {
> + /*
> + * The chain is taken for running, but the endpoint
> + * is disabled: the core stopped it on a BNA that was
> + * not handled as one, for example one seen with the
> + * endpoint still enabled. Restart it as after a BNA.
> + */
> + dwc2_gadget_isoc_out_bna_reset(ep, true);
> }
> return;
> }
[Severity: Medium]
Should the TARGET_FRAME_INITIAL branch also verify that DXEPCTL_EPENA is
clear before calling dwc2_gadget_start_isoc_ddma()?
When dwc2_gadget_handle_isoc_bna() sees a BNA with EPENA still set and
isoc_out_restarted clear, it resets target_frame to TARGET_FRAME_INITIAL
with start = false to defer restarting until EPENA clears. If
DXEPINT_OUTTKNEPDIS was latched in the same interrupt word,
dwc2_gadget_handle_out_token_ep_disabled() runs immediately afterward and
calls dwc2_gadget_start_isoc_ddma() without checking EPENA, overwriting
desc_list with HBUSY and reprogramming DOEPDMA while the endpoint is still
enabled in hardware.
[ ... ]
> @@ -4348,6 +4944,25 @@ static int dwc2_hsotg_ep_disable(struct usb_ep *ep)
>
> ctrl = dwc2_readl(hsotg, epctrl_reg);
>
> + /*
> + * An isochronous OUT endpoint disabled by a BNA may still hold packets
> + * at the head of the Rx FIFO, which EP0 shares, see
> + * dwc2_gadget_isoc_out_drain(). Once the endpoint is gone no BNA comes
> + * to drain on, so give the drain more time here. A Rx FIFO flush does
> + * not help: the core holds the packet for a DMA write that waits for
> + * a descriptor, and the flush waits for the AHB master to go idle.
> + */
> + if (using_desc_dma(hsotg) && hs_ep->isochronous && !dir_in &&
> + hs_ep->isoc_out_bna && !(ctrl & DXEPCTL_EPENA)) {
> + dwc2_writel(hsotg, ctrl | DXEPCTL_SNAK, epctrl_reg);
> + dwc2_gadget_isoc_out_drain(hs_ep,
> + DWC2_ISOC_OUT_DRAIN_DISABLE_US);
> + if (hs_ep->isoc_out_bna)
> + dev_warn(hsotg->dev,
> + "%s: packets left in the Rx FIFO at disable\n",
> + hs_ep->ep.name);
> + }
[Severity: High]
Can dwc2_hsotg_ep_disable() skip draining held packets from the Rx FIFO
when a BNA happens concurrently before hs_ep->isoc_out_bna is set?
dwc2_gadget_isoc_out_bna_reset() calls
dwc2_gadget_complete_isoc_request_ddma() before setting
hs_ep->isoc_out_bna = 1, dropping hsotg->lock for the giveback:
CPU0 CPU1
dwc2_hsotg_epint()
clear DOEPINT (including BNAINTR)
dwc2_gadget_isoc_out_bna_reset()
dwc2_gadget_complete_isoc_request_ddma()
spin_unlock(&hsotg->lock)
dwc2_hsotg_ep_disable()
isoc_out_bna == 0 <-- skips drain
disable_count++
spin_lock(&hsotg->lock)
if (disable_count != count)
return; <-- never sets isoc_out_bna or drains
The same gap occurs if dwc2_hsotg_ep_disable() runs during an XferCompl
giveback when XferCompl and BNAINTR arrive in the same interrupt, or before
dwc2_hsotg_epint() runs at all (for example when GINTSTS_USBRST calls
dwc2_hsotg_core_init_disconnected() -> dwc2_hsotg_ep_disable() before
GINTSTS_OEPINT is serviced). Undrained packets then stay at the head of
the shared Rx FIFO after disable, stalling EP0 and other OUT endpoints and
leaving GRSTCTL.AHBIDLE clear so subsequent core resets fail until reboot.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261205130225.341741-1-vadim@yuzi.co?part=1
next prev parent reply other threads:[~2026-12-05 13:31 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-05 13:02 [PATCH v4 0/2] usb: dwc2: gadget: descriptor DMA fixes for BNA, found on Sophgo SG2002 Vadim Yuzifovich
2026-12-05 13:02 ` [PATCH v4 1/2] usb: dwc2: gadget: restart descriptor DMA isochronous OUT endpoints after BNA Vadim Yuzifovich
2026-12-05 13:31 ` sashiko-bot [this message]
2026-12-05 13:02 ` [PATCH v4 2/2] usb: dwc2: gadget: restart descriptor DMA bulk and interrupt IN " Vadim Yuzifovich
2026-12-05 13:31 ` sashiko-bot
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-251570@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
--cc=vadim@yuzi.co \
/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