From: sashiko-bot@kernel.org
To: "Vadim Yuzifovich" <vadim@yuzi.co>
Cc: linux-usb@vger.kernel.org
Subject: Re: [PATCH v3 1/3] usb: dwc2: gadget: restart descriptor DMA isochronous OUT endpoints after BNA
Date: Tue, 01 Dec 2026 15:04:39 +0000 [thread overview]
Message-ID: <sashiko-outbox-243551@kernel.org> (raw)
In-Reply-To: <20261201142920.49830-2-vadim@yuzi.co>
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] usb: dwc2: premature DAINTMSK unmasking in
dwc2_gadget_restart_isoc_out_ddma()
- [High] usb: dwc2: infinite completion loop on stale DMADONE
descriptors after BNA
- [Medium] usb: dwc2: synchronous request completion inside
dwc2_hsotg_ep_queue()
- [Medium] usb: dwc2: race with dwc2_hsotg_ep_disable() when dropping
lock on BNA giveback
--
commit 8d8dbfcf78b9934c4e5531108fea6adf6f917f7f
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 after a BNA
or endpoint-disabled interrupt by draining any packet held by the core and
rebuilding the descriptor chain, or masking the endpoint's interrupts
until the next request is queued.
> diff --git a/drivers/usb/dwc2/gadget.c b/drivers/usb/dwc2/gadget.c
> index 6c8a9f05fff00..8ca0667b4c852 100644
> --- a/drivers/usb/dwc2/gadget.c
> +++ b/drivers/usb/dwc2/gadget.c
[ ... ]
> @@ -970,6 +998,71 @@ static int dwc2_gadget_fill_isoc_desc(struct dwc2_hsotg_ep *hs_ep,
> return 0;
> }
>
> +static void dwc2_gadget_complete_isoc_request_ddma(struct dwc2_hsotg_ep *hs_ep);
> +
> +/*
> + * dwc2_gadget_isoc_out_held_packet - let the core finish an OUT packet it
> + * holds from before a BNA.
> + * @hs_ep: The isochronous OUT endpoint, its new chain written and descriptor 0
> + * just marked host ready, the endpoint not enabled yet.
[ ... ]
> + /*
> + * The completion may queue a request. Keep it out of this chain,
> + * which the caller builds again: as long as target_frame is initial,
> + * dwc2_hsotg_ep_queue() only adds the request to the queue.
> + */
> + target_frame = hs_ep->target_frame;
> + hs_ep->target_frame = TARGET_FRAME_INITIAL;
> + dwc2_gadget_complete_isoc_request_ddma(hs_ep);
> + hs_ep->target_frame = target_frame;
> +
> + return true;
> +}
[Severity: Medium]
Can this race with dwc2_hsotg_ep_disable() while hsotg->lock is dropped
during request giveback?
dwc2_gadget_complete_isoc_request_ddma() calls
dwc2_hsotg_complete_request(), which drops hsotg->lock around
usb_gadget_giveback_request():
CPU0 CPU1
dwc2_gadget_isoc_out_held_packet()
dwc2_gadget_complete_isoc_request_ddma()
dwc2_hsotg_complete_request()
spin_unlock(&hsotg->lock)
dwc2_hsotg_ep_disable_lock()
spin_lock_irqsave(&hsotg->lock)
dwc2_hsotg_ep_disable()
/* clears EPENA & USBACTEP */
hs_ep->isoc_out_stopped = 0
hs_ep->target_frame =
TARGET_FRAME_INITIAL
kill_all_requests()
spin_unlock_irqrestore(&hsotg->lock)
spin_lock(&hsotg->lock)
hs_ep->target_frame = target_frame
return true;
/* dwc2_gadget_start_isoc_ddma()
jumps to restart: and sets
isoc_out_stopped = 1 or re-enables
DOEPCTL on a disabled endpoint */
If CPU0 reacquires hsotg->lock while kill_all_requests() has dropped the
lock to give back an earlier request, dwc2_gadget_start_isoc_ddma() will
rebuild the chain and set DXEPCTL_EPENA on requests that
kill_all_requests() is actively terminating. The same unlocked window
exists after dwc2_gadget_complete_isoc_request_ddma() in
dwc2_gadget_handle_isoc_bna() and dwc2_gadget_handle_ep_disabled().
[ ... ]
> @@ -1485,13 +1611,32 @@ static int dwc2_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
> if (using_desc_dma(hs) && hs_ep->isochronous) {
> if (hs_ep->target_frame != TARGET_FRAME_INITIAL) {
> dma_addr_t dma_addr = hs_req->req.dma;
> + bool hold;
>
> if (hs_req->req.num_sgs) {
> WARN_ON(hs_req->req.num_sgs > 1);
> dma_addr = sg_dma_address(hs_req->req.sg);
> }
> +
> + /*
> + * After a BNA the core retries descriptor 0 for the
> + * packet it holds. Until the BNA handler has built
> + * the chain again, a request queued from a completion
> + * must not make that descriptor host ready, see
> + * dwc2_gadget_isoc_out_held_packet().
> + */
> + hold = !hs_ep->dir_in && !hs_ep->next_desc &&
> + !(dwc2_readl(hs, DOEPCTL(hs_ep->index)) &
> + DXEPCTL_EPENA);
> dwc2_gadget_fill_isoc_desc(hs_ep, dma_addr,
> - hs_req->req.length);
> + hs_req->req.length, hold);
> + } else if (hs_ep->isoc_out_stopped) {
> + /*
> + * A BNA stopped this OUT endpoint with nothing to
> + * restart it with. No OUTTKNEPDIS follows a BNA, so
> + * start the chain now.
> + */
> + dwc2_gadget_restart_isoc_out_ddma(hs_ep);
> }
> return 0;
> }
[Severity: Medium]
Does restarting a stopped endpoint here invoke req->complete()
synchronously inside usb_ep_queue()?
When hs_ep->isoc_out_stopped is set, hs_ep->queue was empty, so req is
placed into desc_list[0]. dwc2_gadget_restart_isoc_out_ddma() ->
dwc2_gadget_start_isoc_ddma() -> dwc2_gadget_isoc_out_held_packet() then
waits for the held packet to land in desc_list[0] and calls
dwc2_gadget_complete_isoc_request_ddma(), which completes req and runs
req->complete() before dwc2_hsotg_ep_queue() returns.
usb_ep_queue() forbids calling req->complete() from within usb_ep_queue()
because callers may hold locks across usb_ep_queue() that the completion
handler acquires, and the completion handler may free req before
usb_ep_queue() runs trace_usb_ep_queue(ep, req, ret).
> @@ -2259,26 +2404,102 @@ static void dwc2_gadget_complete_isoc_request_ddma(struct dwc2_hsotg_ep *hs_ep)
> }
> }
>
> +/*
> + * dwc2_gadget_restart_isoc_out_ddma - build the chain of a disabled ISOC OUT
> + * endpoint again from its queue and enable it.
> + * @hs_ep: The isochronous OUT endpoint, in its initial state.
> + *
> + * After a BNA the core keeps raising BNA for the endpoint, as fast as the
> + * handler clears it, and no OUTTKNEPDIS comes to restart the chain. So the
> + * chain is restarted here at once.
> + */
> +static void dwc2_gadget_restart_isoc_out_ddma(struct dwc2_hsotg_ep *hs_ep)
> +{
> + struct dwc2_hsotg *hsotg = hs_ep->parent;
> +
> + if (hs_ep->dir_in || list_empty(&hs_ep->queue) ||
> + hs_ep->target_frame != TARGET_FRAME_INITIAL ||
> + dwc2_readl(hsotg, DOEPCTL(hs_ep->index)) & DXEPCTL_EPENA)
> + return;
> +
> + /*
> + * Clear the mark first: a request queued from the completion of the
> + * held packet must only be added to the queue, not start a restart of
> + * its own inside this one.
> + */
> + dwc2_gadget_isoc_out_unstop(hs_ep);
> + hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
> + dwc2_gadget_start_isoc_ddma(hs_ep);
> +}
[Severity: High]
Does calling dwc2_gadget_isoc_out_unstop() here unmask the endpoint
interrupt in DAINTMSK before the chain is rebuilt and DXEPCTL_EPENA is
set?
dwc2_gadget_isoc_out_unstop() both clears hs_ep->isoc_out_stopped and
calls dwc2_hsotg_ctrl_epint(..., 1), which turns the second
dwc2_gadget_isoc_out_unstop() call at the end of
dwc2_gadget_start_isoc_ddma() into a no-op. When
dwc2_gadget_isoc_out_held_packet() drains the held packet into
desc_list[0] and drops hsotg->lock in dwc2_hsotg_complete_request(),
DAINTMSK is already unmasked while DXEPCTL_EPENA is still 0 and
hs_ep->compl_desc has not been incremented past 0:
CPU0 CPU1 (<hardirq>)
dwc2_hsotg_ep_queue()
dwc2_gadget_restart_isoc_out_ddma()
dwc2_gadget_isoc_out_unstop()
/* unmasks ep in DAINTMSK */
dwc2_gadget_start_isoc_ddma()
dwc2_gadget_isoc_out_held_packet()
/* desc_list[0] becomes DMADONE;
core asserts XFERCOMPL & BNA */
dwc2_gadget_complete_isoc_request_ddma()
dwc2_hsotg_complete_request()
spin_unlock(&hsotg->lock)
dwc2_hsotg_irq()
spin_lock(&hsotg->lock)
dwc2_hsotg_epint()
/* compl_desc is still 0 and
desc_list[0] is still DMADONE:
completes desc 0 a second time,
then handles BNA with EPENA == 0 */
Clearing hs_ep->isoc_out_stopped = 0 here without unmasking DAINTMSK until
dwc2_gadget_start_isoc_ddma() is about to set DXEPCTL_EPENA would keep the
endpoint interrupt masked across the held-packet giveback.
> +
> /*
> * dwc2_gadget_handle_isoc_bna - handle BNA interrupt for ISOC.
> * @hs_ep: The isochronous endpoint.
[ ... ]
> + running = hs_ep->target_frame != TARGET_FRAME_INITIAL;
> hs_ep->target_frame = TARGET_FRAME_INITIAL;
> + if (!hs_ep->dir_in && running)
> + dwc2_gadget_complete_isoc_request_ddma(hs_ep);
> hs_ep->next_desc = 0;
> hs_ep->compl_desc = 0;
[Severity: High]
Can dwc2_gadget_complete_isoc_request_ddma() loop forever on stale
DEV_DMA_BUFF_STS_DMADONE descriptors once the 256-entry descriptor ring
has wrapped?
dwc2_gadget_complete_isoc_request_ddma() never resets completed
descriptors to DEV_DMA_BUFF_STS_HBUSY and does not stop at
hs_ep->next_desc:
drivers/usb/dwc2/gadget.c:dwc2_gadget_complete_isoc_request_ddma() {
desc_sts = hs_ep->desc_list[hs_ep->compl_desc].status;
/* Process only descriptors with buffer status set to DMA done */
while ((desc_sts & DEV_DMA_BUFF_STS_MASK) >>
DEV_DMA_BUFF_STS_SHIFT == DEV_DMA_BUFF_STS_DMADONE) {
...
dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
hs_ep->compl_desc++;
if (hs_ep->compl_desc > (MAX_DMA_DESC_NUM_HS_ISOC - 1))
hs_ep->compl_desc = 0;
desc_sts = hs_ep->desc_list[hs_ep->compl_desc].status;
}
}
After 256 packets have completed, every descriptor in the ring has
DEV_DMA_BUFF_STS_DMADONE from the current or previous pass. During normal
XferCompl processing, hs_ep->target_frame is not TARGET_FRAME_INITIAL, so
when a completion callback re-queues a request via dwc2_hsotg_ep_queue(),
dwc2_gadget_fill_isoc_desc() overwrites desc_list[hs_ep->next_desc] with
DEV_DMA_BUFF_STS_HREADY before compl_desc reaches it.
Here (and in dwc2_gadget_handle_ep_disabled()), hs_ep->target_frame is set
to TARGET_FRAME_INITIAL before calling
dwc2_gadget_complete_isoc_request_ddma(). Requests re-queued from a
completion callback (as f_uac1 does) are appended to hs_ep->queue without
calling dwc2_gadget_fill_isoc_desc(), so desc_list[hs_ep->next_desc] keeps
its stale DEV_DMA_BUFF_STS_DMADONE status from the previous ring lap.
When compl_desc reaches next_desc, dwc2_gadget_complete_isoc_request_ddma()
keeps matching DEV_DMA_BUFF_STS_DMADONE and completes each newly re-queued
request in an infinite loop in hardirq context.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261201142920.49830-1-vadim@yuzi.co?part=1
next prev parent reply other threads:[~2026-12-01 15:04 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 14:29 [PATCH v3 0/3] usb: dwc2: gadget: descriptor DMA fixes for BNA, found on Sophgo SG2002 Vadim Yuzifovich
2026-12-01 14:29 ` [PATCH v3 1/3] usb: dwc2: gadget: restart descriptor DMA isochronous OUT endpoints after BNA Vadim Yuzifovich
2026-12-01 15:04 ` sashiko-bot [this message]
2026-12-01 14:29 ` [PATCH v3 2/3] usb: dwc2: gadget: restart descriptor DMA bulk and interrupt " Vadim Yuzifovich
2026-12-01 14:29 ` [PATCH v3 3/3] usb: dwc2: gadget: make descriptors visible before the endpoint is enabled Vadim Yuzifovich
2026-12-01 15:04 ` [PATCH v3 0/3] usb: dwc2: gadget: descriptor DMA fixes for BNA, found on Sophgo SG2002 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-243551@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