From mboxrd@z Thu Jan 1 00:00:00 1970 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=1796137480; b=W63JU+H03CyuMdPDbK+WST8LWGYwGjvsbs6+3w5YLyzxN49FlpMwIQ0rmfBYGRNQL3hU od3+GuWIoEvPgOKcNIDFsYmXuXLu7mn9JhV3pAJmhOFAyOXuMRZmLSA4BxbrCLEmOkjww 6bmq8R9QklP+zsaJzFxrow7brIhBmKdcRDv0ZotaSPWzPR3aJ7I/qmDid+ALdsWRk/V5H Jx+JVdYLp7e94H3l7SrgdhHdtq2UUeiYN7qtRKm0lrrcxaEP8WSSuES02W0ve8tceJd5S ECQmOj7OR8BTQnXNL2Ng9UNgY4C2s+a1hU3bihQv4DXEoj4pe4h4FW5YX+N0i2K8DLA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796137480; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=nGlW243kw8/x/Y7dL04tMceZB4fgtlwEDBA49QAqCJs=; b=5h0pPmgVaEkOJ5rnkaBYu/cnsucv3OFXcDrQRQ/kxBh+sSx1foJZX0lbnuuG23VPaMBH APe4UrHhjXBMOoK/eRksWez0ZXgGV90ZrPp95BDNcYHKfWG/OeIcbT8GVZ9E0SDxFJn8E xLzp7mUG4K+YagUGaH1t06xiGW01i0qbHGiBlHe57aBFG/PamHvj25MDI24GvXzI4ujdL I9UrGTg5Fi8JQ3lsHugNdQu1ugbjPYi1rtvsN9WH5q3w2oGTUTSXSmfhpRqevMnms9Y/o B/xvA9eyjEt7qYXpnihJPOLJeSO+Bl1JFyezw6+8QMEj1QWnhdZ9asaHjC2MkSZCAOA== 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=XnSx6bIj; 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 4247D606A339 for ; Tue, 1 Dec 2026 15:04:40 +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=XnSx6bIj Received: by smtp.kernel.org (Postfix) with ESMTPSA id E96741F000C1; Tue, 1 Dec 2026 15:04:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796137480; bh=nGlW243kw8/x/Y7dL04tMceZB4fgtlwEDBA49QAqCJs=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=XnSx6bIjOigco5qkmc+bTU57eZgrRd00zoiszFSoK7rZVYtH/bKMY8irQ0Skj6ret razdsQm5BmZiLvjbXB0laKd4yx2gBTMBIq5tZb2RtSeLOr+2araWLw77992FAg/nw9 nyu1ZMuwEQk3jJkN2go+oB1g2Rb8p1fYWj2KxP6pblWrq5+7Pfk0pJc+DyBHbRr5pR 2z+GCnNG4Loc6uEio2ETX4DdiYdaT9TRIVI+Do9GlHsF74y6rYnHkWNQuprYbrwvCB 7i9uTzPTffx98neo4J85WekzJm1b+9YHeG9wWUXu1XAzjq7QHDy9vJukM5JwAC5N1t 8MDylPtpKJs8Q== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v3 1/3] usb: dwc2: gadget: restart descriptor DMA isochronous OUT endpoints after BNA Reply-To: sashiko-reviews@lists.linux.dev To: "Vadim Yuzifovich" Cc: linux-usb@vger.kernel.org In-Reply-To: <20261201142920.49830-2-vadim@yuzi.co> References: <20261201142920.49830-1-vadim@yuzi.co> <20261201142920.49830-2-vadim@yuzi.co> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Tue, 01 Dec 2026 15:04:39 +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 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 usb: dwc2: gadget: restart descriptor DMA isochronous OUT endpoints after B= NA 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_hs= otg_ep *hs_ep, > return 0; > } > =20 > +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 descr= iptor 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 =3D hs_ep->target_frame; > + hs_ep->target_frame =3D TARGET_FRAME_INITIAL; > + dwc2_gadget_complete_isoc_request_ddma(hs_ep); > + hs_ep->target_frame =3D 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 =3D 0 hs_ep->target_frame =3D TARGET_FRAME_INITIAL kill_all_requests() spin_unlock_irqrestore(&hsotg->lock) spin_lock(&hsotg->lock) hs_ep->target_frame =3D target_frame return true; /* dwc2_gadget_start_isoc_ddma() jumps to restart: and sets isoc_out_stopped =3D 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 !=3D TARGET_FRAME_INITIAL) { > dma_addr_t dma_addr =3D hs_req->req.dma; > + bool hold; > =20 > if (hs_req->req.num_sgs) { > WARN_ON(hs_req->req.num_sgs > 1); > dma_addr =3D 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 =3D !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_dd= ma(struct dwc2_hsotg_ep *hs_ep) > } > } > =20 > +/* > + * dwc2_gadget_restart_isoc_out_ddma - build the chain of a disabled ISO= C 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 t= he > + * 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_e= p) > +{ > + struct dwc2_hsotg *hsotg =3D hs_ep->parent; > + > + if (hs_ep->dir_in || list_empty(&hs_ep->queue) || > + hs_ep->target_frame !=3D 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 =3D 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 () 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 =3D= =3D 0 */ Clearing hs_ep->isoc_out_stopped =3D 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 =3D hs_ep->target_frame !=3D TARGET_FRAME_INITIAL; > hs_ep->target_frame =3D TARGET_FRAME_INITIAL; > + if (!hs_ep->dir_in && running) > + dwc2_gadget_complete_isoc_request_ddma(hs_ep); > hs_ep->next_desc =3D 0; > hs_ep->compl_desc =3D 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 =3D 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 =3D=3D 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 =3D 0; desc_sts =3D 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. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261201142920.4983= 0-1-vadim@yuzi.co?part=3D1