Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796477512; b=1525hEAgHJN614FUXsMifE2nztOQxUmBlC8qoIr8quC01BcAPZrPEWJWubQjJtjrv9Do mJrxWGqnM1XzQHIQHrVx5mUXl9Hc2LBlRqwZtCIoJUCvk6e2xJzbXm7n8WIKuvb6ZJNhI Tsj9cSmNYjHmSZH6O1FQ+qcDKQuxByD3TLmlxD46qzS6Rr22fxakI1zQsatFNJVoM3iBO Tmgf8llwb5SAA+h4A7m2sdm+RZbWe7sotO5ex/5TPUC/NZOjrIxY9GUBnEX3AKP6oFOjI J/8/yAAKvmJmbnXtuj+ehhu08yUzXJBxGwhn0N8N/2yLYomxgPMJLigLEa3smrpSAxg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796477512; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=g6brB58zuqFFuVTds6a0uvNY7rg6R5YCZ0PWCbEuIYc=; b=ZLh1e8NzUGpx/H9KlNDNJZsBuVQMH4flDsHYsajvXVNXNquLjhTlxkmho10mNGQK4YKb FWgu1jzGhrKUVznUfDEsl1vb9WwJhopsVrHgzpBUwgB8ypV7a4MVJxb0vdE+A06dt5XNk 3f4AU7WARaJi8oBfDDE4/isU3ljFpMACp6T2PjtEjZ/6m9v9/J3EWSQZ+nqIBhBMXn0qM VxIFJ/zVzxkbrMaR/yzHDc7d7m84jNbL0+U9YrcJh0RUoG72h7o21gIq3I+4EJBM4ituG yMHSkDDI/6YpeWfhzhZionUO05S97fq2beIZHFROn3/+BPyEEXr1o7x8S7CpQlWBTXw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=DoAH7h1e; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7008A606A322 for ; Sat, 5 Dec 2026 13:31:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=DoAH7h1e Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3D7BD1F00936; Sat, 5 Dec 2026 13:31:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796477512; bh=g6brB58zuqFFuVTds6a0uvNY7rg6R5YCZ0PWCbEuIYc=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=DoAH7h1e3JwhI+bmk+J+BkB/uE2qXBMtcpg3WvLlAttXewSzcRhkdb6pGrnsd9lV1 gfiNpAMn8W2B/NlhounR75k0L6+m42TloBW6vxEFCcMXcThnpaXKYalst9IYjsvqZT yhNdyT5hIUolSGM/Xhe1FRcr8ZYBVrgbZ1ia+21YBzQZgkENVQmsfDtFErCYBuJJYw cKVAWjckQcu2gXkd2Il1yQMgjm4dyl2jgbCRZQ9E1T8os+QkSTGqTwzJOfx2l3YEHK 08G5PcgVHFxjdorUo4zjZYBROxCEuLI5IvN+leSigtNRq3JssDarU1Z4qGHhToImfg ojbQ2M9zDRvfg== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v4 2/2] usb: dwc2: gadget: restart descriptor DMA bulk and interrupt IN endpoints after BNA Reply-To: sashiko-reviews@lists.linux.dev To: "Vadim Yuzifovich" Cc: linux-usb@vger.kernel.org In-Reply-To: <20261205130225.341741-3-vadim@yuzi.co> References: <20261205130225.341741-1-vadim@yuzi.co> <20261205130225.341741-3-vadim@yuzi.co> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Sat, 05 Dec 2026 13:31:51 +0000 X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Thank you for your contribution! Sashiko AI review found 7 potential issues to consider: - [Medium] usb: dwc2: stale BNAINTR in dwc2_hsotg_epint() aborts restarted IN transfer - [Medium] usb: dwc2: dwc2_gadget_handle_nonisoc_bna() disables ineligible IN endpoint - [Medium] usb: dwc2: stale bna_pending aborts restarted IN transfer on next EPDISBLD - [Medium] usb: dwc2: broken bna_restarted tracking causes wrong TxFIFO flush behavior - [Medium] usb: dwc2: latched EPDISBLD and stale bna_restarted break ep_stop_xfr() - [Medium] usb: dwc2: dwc2_gadget_handle_nonisoc_bna() skips IN EP NAK handshake - [Medium] usb: dwc2: XferCompl clears bna_pending before EPDISBLD flushes next TxFIFO -- commit 41dd4823735d8cbadd17712a8c5f5ad25bf69eda Author: Vadim Yuzifovich usb: dwc2: gadget: restart descriptor DMA bulk and interrupt IN endpoints a= fter BNA This commit adds BNA interrupt recovery for non-EP0 bulk and interrupt IN endpoints in descriptor DMA mode, restarting the current request when the endpoint is disabled and the first descriptor is still host ready. > diff --git a/drivers/usb/dwc2/gadget.c b/drivers/usb/dwc2/gadget.c > index fdbc9677aef29..bd2e4f397f3c2 100644 > --- a/drivers/usb/dwc2/gadget.c > +++ b/drivers/usb/dwc2/gadget.c [ ... ] > @@ -2790,6 +2798,111 @@ static void dwc2_gadget_handle_isoc_bna(struct dw= c2_hsotg_ep *hs_ep) [ ... ] > +static void dwc2_gadget_handle_nonisoc_bna(struct dwc2_hsotg_ep *hs_ep, > + u32 ints) > +{ > + struct dwc2_hsotg *hsotg =3D hs_ep->parent; > + struct dwc2_hsotg_req *hs_req =3D hs_ep->req; > + u32 epctl_reg =3D hs_ep->dir_in ? DIEPCTL(hs_ep->index) : > + DOEPCTL(hs_ep->index); > + u32 epctl =3D dwc2_readl(hsotg, epctl_reg); > + u32 status =3D hs_ep->desc_list ? READ_ONCE(hs_ep->desc_list[0].status)= : 0; > + /* A request is current, and nothing else will move the endpoint on. */ > + bool stuck =3D hs_req && !hs_ep->halted && > + !(epctl & (DXEPCTL_EPENA | DXEPCTL_STALL)); > + bool restart =3D stuck && hs_ep->dir_in && hs_ep->desc_list && > + (!hs_req->req.num_sgs || !hs_req->req.actual) && > + status >> DEV_DMA_BUFF_STS_SHIFT =3D=3D DEV_DMA_BUFF_STS_HREADY; > + > + /* > + * Not disabled yet. Disable it, so that an endpoint-disabled > + * interrupt is sure to come, and restart from there, see > + * dwc2_gadget_handle_ep_disabled(). > + */ > + if (hs_ep->dir_in && hs_req && !hs_ep->halted && > + (epctl & DXEPCTL_EPENA)) { > + hs_ep->bna_pending =3D 1; > + dwc2_writel(hsotg, epctl | DXEPCTL_SNAK | DXEPCTL_EPDIS, > + epctl_reg); > + return; > + } [Severity: Medium] Can this disable an active IN endpoint that is not eligible for a restart? This branch writes DXEPCTL_SNAK | DXEPCTL_EPDIS before checking DXEPCTL_STA= LL, desc_list, scatter-gather req.actual, descriptor 0 HREADY status, or the DWC2_BNA_RESTARTS limit. If DMA has already taken descriptor 0 or the restart cap has been reached, dwc2_gadget_handle_ep_disabled() flushes the TxFIFO and the subsequent dwc2_gadget_handle_nonisoc_bna() refuses to resta= rt, leaving the endpoint stopped (or bypassing DWC2_BNA_RESTARTS if EPENA still reads set). [Severity: Medium] Is it safe to write DXEPCTL_SNAK | DXEPCTL_EPDIS directly without the IN endpoint NAK-effective handshake? In dwc2_hsotg_ep_stop_xfr(), disabling a non-stalled IN endpoint first waits for DXEPINT_INEPNAKEFF (for dedicated TxFIFOs or periodic IN) or asserts DCTL_SGNPINNAK and waits for GINTSTS_GINNAKEFF (for shared-FIFO bulk IN) before setting DXEPCTL_EPDIS. Skipping that handshake risks disrupting an in-flight IN transfer. In shared-FIFO mode (which dwc2_check_params() permits with g_dma_desc), dwc2_gadget_handle_ep_disabled() then flushes shared TxFIFO 0 with bna_restarted =3D=3D 0 while other non-periodic IN endpoints such as EP0 IN= were never quiesced via DCTL_SGNPINNAK. [ ... ] > + /* > + * An endpoint-disabled interrupt latched for this BNA and not handled > + * yet must not flush the data of the restarted request. > + */ > + hs_ep->bna_restarted =3D !(ints & DXEPINT_EPDISBLD) && > + (dwc2_readl(hsotg, DIEPINT(hs_ep->index)) & DXEPINT_EPDISBLD); [Severity: Medium] Does the !(ints & DXEPINT_EPDISBLD) check drop a newly latched DXEPINT_EPDISBLD on consecutive BNAs? dwc2_hsotg_epint() clears all bits in ints from DIEPINT at entry, so any DXEPINT_EPDISBLD read here is a newly latched, unhandled interrupt. If BNA= #1 left EPDISBLD pending and the restarted request immediately hits BNA #2 so that ints carries both DXEPINT_EPDISBLD and DXEPINT_BNAINTR, !(ints & DXEPINT_EPDISBLD) forces bna_restarted to 0 despite BNA #2's new EPDISBLD, causing the next dwc2_gadget_handle_ep_disabled() to flush the restarted request's TxFIFO. Separately, dwc2_hsotg_ep_sethalt() never clears bna_restarted or bna_pendi= ng, so if a halt is set and cleared before EPDISBLD is serviced, both halted and DXEPCTL_STALL are 0 in dwc2_gadget_handle_ep_disabled() and a stale bna_restarted skips the TxFIFO flush. Clearing an already-latched DXEPINT_EPDISBLD before restarting would avoid carrying bna_restarted across interrupts altogether. > + > + dev_warn_ratelimited(hsotg->dev, > + "%s: BNA, DxEPINT %08x DxEPCTL %08x descriptor %08x, request res= tarted\n", > + hs_ep->name, ints, epctl, status); > + dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true); > +} [Severity: Medium] Does leaving DXEPINT_EPDISBLD latched in DIEPINT while re-enabling the endpoint break dwc2_hsotg_ep_stop_xfr()? If dwc2_hsotg_ep_dequeue() or dwc2_hsotg_ep_disable() runs before the pendi= ng EPDISBLD interrupt is serviced, dwc2_hsotg_ep_stop_xfr() writes DXEPCTL_EPD= IS and polls DXEPINT_EPDISBLD, which immediately matches the stale BNA bit bef= ore hardware finishes disabling the endpoint. It then flushes the TxFIFO and dwc2_hsotg_complete_request() unmaps the DMA buffer while DMA may still be active. dwc2_hsotg_ep_stop_xfr() also clears DXEPINT_EPDISBLD directly without clearing bna_restarted or bna_pending, so bna_restarted stays 1 across futu= re requests and causes a later unrelated EPDISBLD in dwc2_gadget_handle_ep_disabled() to skip dwc2_hsotg_txfifo_flush(). [ ... ] > @@ -3408,6 +3529,12 @@ static void dwc2_gadget_handle_ep_disabled(struct = dwc2_hsotg_ep *hs_ep) > } > } > =20 > + /* See dwc2_gadget_handle_nonisoc_bna() */ > + if (hs_ep->bna_pending) { > + hs_ep->bna_pending =3D 0; > + dwc2_gadget_handle_nonisoc_bna(hs_ep, DXEPINT_EPDISBLD); > + } > + > if (!hs_ep->isochronous) > return; [Severity: Medium] Can a stale bna_pending survive a BNA restart and abort the restarted trans= fer on the subsequent EPDISBLD interrupt? IRQ 1: BNA with EPENA set dwc2_gadget_handle_nonisoc_bna() bna_pending =3D 1 write DXEPCTL_SNAK | DXEPCTL_EPDIS IRQ 2: BNAINTR read (EPDISBLD not latched yet) (hardware clears EPENA and latches EPDISBLD before DIEPCTL read) dwc2_gadget_handle_nonisoc_bna() EPENA is clear; bna_pending left at 1 bna_restarted =3D 1 dwc2_hsotg_start_req(..., true) /* continuing=3Dtrue keeps bna_pendin= g */ IRQ 3: EPDISBLD dwc2_gadget_handle_ep_disabled() bna_restarted =3D 0 /* skips TxFIFO flush */ bna_pending =3D=3D 1 -> dwc2_gadget_handle_nonisoc_bna() sees EPENA set -> writes SNAK | EPDIS <-- aborts restarted transfer Because dwc2_hsotg_start_req() only clears bna_pending when !continuing, clearing bna_pending when dwc2_gadget_handle_nonisoc_bna() sees EPENA clear would prevent IRQ 3 from aborting the restarted request. [ ... ] > @@ -3674,6 +3801,14 @@ static void dwc2_hsotg_epint(struct dwc2_hsotg *hs= otg, unsigned int idx, > __func__, dwc2_readl(hsotg, epctl_reg), > dwc2_readl(hsotg, epsiz_reg)); > =20 > + /* > + * See dwc2_gadget_handle_nonisoc_bna(). bna_restarted is left > + * to the endpoint-disabled handler below, which may still have > + * an interrupt of the BNA to handle. > + */ > + hs_ep->bna_restarts =3D 0; > + hs_ep->bna_pending =3D 0; > + > /* In DDMA handle isochronous requests separately */ [Severity: Medium] If XferCompl arrives while bna_pending is 1, does clearing bna_pending here allow the pending EPDISBLD to flush the next request's TxFIFO? When dwc2_gadget_handle_nonisoc_bna() writes DXEPCTL_SNAK | DXEPCTL_EPDIS w= ith bna_pending =3D 1, bna_restarted stays 0. If the in-flight transfer comple= tes before SNAK takes effect (for instance after a stale BNAINTR survived an ep_disable()/ep_enable() cycle and hit a newly queued request), DXEPINT_XFERCOMPL clears bna_pending and dwc2_hsotg_complete_in() starts the next request with data in the TxFIFO. When the pending DXEPINT_EPDISBLD then reaches dwc2_gadget_handle_ep_disabled() in the same or next interrupt, bna_restart= ed is 0, so it calls dwc2_hsotg_txfifo_flush() on the newly started request, corrupting its data or hanging the endpoint. [ ... ] > @@ -3780,12 +3915,20 @@ static void dwc2_hsotg_epint(struct dwc2_hsotg *h= sotg, unsigned int idx, > if (ints & DXEPINT_BACK2BACKSETUP) > dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__); > =20 > - if ((ints & DXEPINT_BNAINTR) && (!hs_ep->isochronous || dir_in)) { > + if ((ints & DXEPINT_BNAINTR) && hs_ep->isochronous && dir_in) { > dev_dbg(hsotg->dev, "%s: BNA interrupt\n", __func__); > - if (hs_ep->isochronous) > - dwc2_gadget_handle_isoc_bna(hs_ep); > + dwc2_gadget_handle_isoc_bna(hs_ep); > } > =20 > + /* > + * A bulk or interrupt endpoint is restarted here, after the > + * endpoint-disabled handler above has flushed the TxFIFO of an IN > + * endpoint. > + */ > + if ((ints & DXEPINT_BNAINTR) && !hs_ep->isochronous && > + using_desc_dma(hsotg) && idx) > + dwc2_gadget_handle_nonisoc_bna(hs_ep, ints); [Severity: Medium] Can a stale DXEPINT_BNAINTR bit in ints abort a transfer that was just star= ted earlier in dwc2_hsotg_epint()? ints is read once at the top of dwc2_hsotg_epint(). If DXEPINT_BNAINTR is = set alongside DXEPINT_EPDISBLD (with bna_pending set) or DXEPINT_XFERCOMPL, tho= se earlier branches already restart or start a transfer with DIEPCTL.EPENA set. Falling through to dwc2_gadget_handle_nonisoc_bna() then sees DXEPCTL_EPENA set from that fresh transfer and writes DXEPCTL_SNAK | DXEPCTL_EPDIS to abo= rt it; if DMA has already moved descriptor 0 off HREADY before the disable completes, the subsequent EPDISBLD flushes the TxFIFO and refuses to restar= t, leaving the endpoint stopped. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261205130225.3417= 41-1-vadim@yuzi.co?part=3D2