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=1796349803; b=bvKs4SO8+UKCeqgrViYzztsCq/aDcJf6k0LSwFWqUM3Gn7q2M/WrN4h6dg0FFMkqS373 eke+VTREP5HfHX6mR7iIk4eBCitAJZOCIP0Jg/NSTrOlqfe1M/K2DElpNf1qJz0HLVmPr hD/X8Y4H1FCfT3F4Uy8vSXIfrZJp4v2EK2cQdL4mRNwmXAZ24o1tvEW0nOsMqbrOMQbQD Ml3ohFE1ZfygRpQkDghnGIKT/pb5pygQjIv1RwUoUJTJ4aZDj8tOO0pAyoBX1S1RTYbTG 6M+d8w78+sFHBRUupnRE6AnWgxVfOPsYfvrqKbeJ6cu9ItaXix4BN97QXNHUsc7jzvg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796349803; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=cXwchu8B4YtPWKaJIz68q+LVSFwF/LU+bwIOqooX2S8=; b=ZTeD0z5BlVJVRBQGj3/s24b/WOj9dLAJ69tflRu/8KfDO9f+iVbbfC9JDq/zIwqfV9CD uoqDMFP+93kKDsLbIFnRHplwToaz/tLyHX+qfv/m7IjAImOPzapUEcXkp0bwFj6uT6cBX TEamcQ/bpRQXPycANGBfcUIANGPjpfMqhB+xJcWduHTRDLxabLENJKMCRjvvLJU8sTUOn VdebRbP2CLVPxjozEbQ/uK1b1dF5mcaqGW59ujfjVWDUDgA/0T7cQnsFDq4KTfs8En19N VhGKRFPDnaXz9jjpnIBAH9WrLusjIohc15rEvjfKxog+JG33+ALxZe80PU6F7t3wvYg== 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=opJTgiE9; 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 ABFD4606A322; Fri, 4 Dec 2026 02:03:23 +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=opJTgiE9 Received: by smtp.kernel.org (Postfix) with ESMTPSA id F24E91F000C2; Fri, 4 Dec 2026 02:03:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796349803; bh=cXwchu8B4YtPWKaJIz68q+LVSFwF/LU+bwIOqooX2S8=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=opJTgiE9x+YoYugykpy6S3Yo3+L0mqhv5qaWQJmPFWtfkTi+Aa1yGJg7kBTSAuDR2 12/pX9Dse5aG0fCwuaVcaWMGcUNjIOXeBuJzHtvLmCqw19Wvm8IjBlJAKfwxXt85/t RjjHHk7S+wWQYSiyqkKlKJQ315Ucs/3tg+OxPURpi/+c/MdTzg4ZwtyGEERDOiK1PN w7VpMI9eGnRfUjcJb1TDyX+gPtEk+HrsMtbRmxZLIlIDDmZkjwGu1SDeaTp9U7K5hp yji/M7gnMypsyxHt78TuL0yiHHmJBIlA80lSC+CgwUIuqidiPydAUkoVZFo55ds3rv LSBe630yklB3Q== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v8 5/6] usb: gadget: udc: crg: Add gadget and endpoint operations Reply-To: sashiko-reviews@lists.linux.dev To: "Petar Stepanovic" Cc: linux-usb@vger.kernel.org, devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org In-Reply-To: <20261203-axiado-ax3000-usb-device-controller-v8-5-d5ff04febd4d@axiado.com> References: <20261203-axiado-ax3000-usb-device-controller-v8-0-d5ff04febd4d@axiado.com> <20261203-axiado-ax3000-usb-device-controller-v8-5-d5ff04febd4d@axiado.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Fri, 04 Dec 2026 02:03:22 +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 10 potential issues to consider: - [High] usb: gadget: crg: unconditional bounce pages break DMABUF and fragment TDs - [High] usb: gadget: crg: DMA unmap and bounce copy run under udc_lock in req_done() - [High] usb: gadget: crg: crg_udc_ep_halt() nukes queued requests at SuperSpeed - [High] usb: gadget: crg: ep_dequeue() issues SET_TR_DQPTR on a halted endpoint - [High] usb: crg: crg_udc_ep_disable() returns -EBUSY while nuking causing UAF - [Medium] usb: gadget: crg: driver directly mutates core-owned usb_ep.enabled - [Medium] usb: gadget: crg: EP0 stall resets setup_status after dropping udc_lock - [Medium] usb: crg: O(Q) full-queue scan per completion in crg_udc_handle_xfer_event() - [Medium] usb: gadget: crg: ring underrun/overrun aborts queued isoc TD - [Low] usb: crg: duplicated connect setup in crg_udc_handle_port_status() -- commit afd89251cb13455bf25f01caa9f3cfbd8869573a Author: Petar Stepanovic usb: gadget: udc: crg: Add gadget and endpoint operations This patch implements the gadget and endpoint operations, transfer event and port status handling, bounce-buffer DMA mapping, and tracepoint definitions for the Axiado AX3000/AX3005 UDC driver. > diff --git a/drivers/usb/gadget/udc/crg/crg-udc-gadget.c b/drivers/usb/ga= dget/udc/crg/crg-udc-gadget.c > new file mode 100644 > index 0000000000000..c7e1fd47aa078 > --- /dev/null > +++ b/drivers/usb/gadget/udc/crg/crg-udc-gadget.c > @@ -0,0 +1,3563 @@ [ ... ] > + } else { > + ret =3D crg_udc_skip_td(udc_ep_ptr, udc_req); > + if (ret) > + goto recover; > + udc_req->usb_req.actual =3D > + crg_udc_actual_before(udc_ep_ptr, udc_req, pause_pt); > + crg_udc_req_done(udc_ep_ptr, udc_req, -ECONNRESET); > + } [Severity: High] Can crg_udc_ep_dequeue() issue CRG_CMD_SET_TR_DQPTR while the endpoint is still in EP_STATE_HALTED? At HS/FS speeds, crg_udc_ep_halt() leaves queued requests and their TRBs on the ring when halting an endpoint. Dequeuing the active request on that halted endpoint enters this branch and calls crg_udc_skip_td() -> crg_udc_epcx_set_dqptr(), which overwrites epcx->dw2/dw3 and issues CRG_CMD_SET_TR_DQPTR without clearing halt first. As crg_udc_ep_halt() notes, CRG_CMD_CLEAR_HALT must precede CRG_CMD_SET_TR_DQPTR on a halted endpoint. If the command fails here, the recover path marks halt_failed and schedules a full controller reset on a normal request dequeue. [ ... ] > + spin_lock_irqsave(&crg_udc->udc_lock, flags); > + /* A completion callback must not restart disable during giveback. */ > + if (udc_ep->nuking) { > + ret =3D -EBUSY; > + goto out; > + } [Severity: High] Can returning -EBUSY here cause a use-after-free and request leak when usb_ep_disable() races with crg_udc_ep_nuke()? usb_ep_disable() guarantees that all pending requests have completed before it returns. Callers such as bot_cleanup_old_alt() in f_tcm.c ignore its return value and immediately free request buffers and call usb_ep_free_request(). CPU0 CPU1 crg_udc_ep_nuke() udc_ep->nuking =3D true crg_udc_req_done(ep, req1) spin_unlock(&udc_lock) usb_gadget_giveback_request() usb_ep_disable(ep) crg_udc_ep_disable() if (udc_ep->nuking) return -EBUSY kfree(req2_buf) usb_ep_free_request(ep, req2) /* still queued -> leaked */ spin_lock(&udc_lock) crg_udc_req_done(ep, req2) crg_udc_unmap_request() <-- use-after-free on req2_buf req2->complete() <-- use-after-free on gadget state Because crg_udc_ep_nuke() is also called from crg_udc_reinit(), crg_udc_ep_halt(), and crg_udc_stop_error_ep(), an endpoint can also be left enabled or halted instead of disabled. When crg_udc_free_request() sees the still-queued request, it returns early and leaks it. [ ... ] > + ret =3D crg_udc_recover_endpoints(crg_udc, &flags); > + if (ret && udc_ep->ep_state =3D=3D EP_STATE_DISABLED && !udc_ep->nuking= && > + list_empty(&udc_ep->queue)) > + udc_ep->usb_ep.enabled =3D false; [Severity: Medium] Should this driver avoid writing to udc_ep->usb_ep.enabled directly? In drivers/usb/gadget/udc/core.c, usb_ep.enabled is managed exclusively by usb_ep_enable() and usb_ep_disable(), which only update it when the UDC callback returns 0. Clearing it here on a non-zero error return diverges from that contract. The driver also writes udc_ep->usb_ep.enabled in crg_udc_ep_enable(), crg_udc_ep_nuke(), and asynchronously via crg_udc_ep_clear_enabled() from crg_udc_reset_work(), crg_udc_reinit(), and crg_gadget_stop(). Clearing it from a workqueue races with gadget drivers calling usb_ep_disable(), which will then return 0 early without invoking ep->ops->disable(). [ ... ] > +int crg_udc_bounce_map_request(struct crg_udc_ep *udc_ep, > + struct crg_udc_request *udc_req, > + enum dma_data_direction dir, gfp_t gfp) > +{ > + struct device *dev =3D udc_ep->crg_udc->dev; > + struct usb_request *req =3D &udc_req->usb_req; > + struct crg_udc_bounce *bounce; > + int nents, ret; > + > + if (!req->length) > + return 0; > + if (udc_req->bounce) > + return -EBUSY; > + > + /* CPU copies require buffers the UDC is allowed to access. */ > + if (req->sg_was_mapped || req->dma_mapped) > + return -EOPNOTSUPP; [Severity: High] Does unconditionally bounce-buffering every transfer break FunctionFS DMABUF and severely throttle SuperSpeed throughput? The driver advertises gadget.sg_supported =3D true, which allows FunctionFS DMABUF attachment. When ffs_dmabuf_transfer() submits a pre-mapped scatterlist with sg_was_mapped =3D true, crg_udc_bounce_map_request() rejects it with -EOPNOTSUPP. For all other non-zero data requests, crg_udc_bounce_map_pages() allocates an sg_table and order-0 pages (often under GFP_ATOMIC, with __GFP_ZERO on DMA_FROM_DEVICE right before DMA overwrites them) and copies the full payload instead of using usb_gadget_map_request_by_dev(). Fragmenting contiguous buffers into PAGE_SIZE segments also hits the 32-entry plan[] TD cap in crg_udc_queue_trbs(), limiting each TD to 128 KB on 4 KB-page kernels. Because crg_udc_queue_pending_trbs() stops while req->td_pending is set, transfers over 128 KB cannot pipeline subsequent TDs and stall on the 1 ms moderated completion interrupts. [ ... ] > + switch (FIELD_GET(CRG_U3DC_PORTSC_SPEED_MASK, tmp)) { > + case CRG_U3DC_PORTSC_SPEED_SSP: > + speed =3D USB_SPEED_SUPER_PLUS; > + break; > + case CRG_U3DC_PORTSC_SPEED_SS: > + speed =3D USB_SPEED_SUPER; > + break; > + case CRG_U3DC_PORTSC_SPEED_HS: > + speed =3D USB_SPEED_HIGH; > + break; > + > + case CRG_U3DC_PORTSC_SPEED_FS: > + speed =3D USB_SPEED_FULL; > + break; > + > + case CRG_U3DC_PORTSC_SPEED_LS: > + speed =3D USB_SPEED_LOW; > + break; > + default: > + speed =3D USB_SPEED_UNKNOWN; > + goto csc_done; > + } > + crg_udc->gadget.speed =3D speed; > + > + ret =3D crg_udc_update_ep0_maxpacketsize(crg_udc); [Severity: Low] Could this speed-decode and EP0/connect setup block be factored into a shared helper? The same sequence (decoding CRG_U3DC_PORTSC_SPEED_*, assigning gadget.speed, calling crg_udc_update_ep0_maxpacketsize() with crg_udc_recover_port_failure() on error, setting connected =3D 1, and calling crg_udc_enable_setup()) appears verbatim earlier in crg_udc_handle_port_status() under the port_reset_pending branch. [ ... ] > + case CMPL_CODE_PROTOCOL_STALL: > + case CMPL_CODE_TRB_STALL: > + if (dci) { > + crg_udc_stop_error_ep(crg_udc, dci, -EPIPE); > + break; > + } > + if (!list_empty(&udc_ep_ptr->queue)) { > + udc_req_ptr =3D list_first_entry(&udc_ep_ptr->queue, > + struct crg_udc_request, > + queue); > + crg_udc_req_done(udc_ep_ptr, udc_req_ptr, -EPIPE); > + } > + crg_udc->setup_status =3D WAIT_FOR_SETUP; > + break; [Severity: Medium] Can setting setup_status =3D WAIT_FOR_SETUP after crg_udc_req_done() corrupt EP0 state if the completion callback or a concurrent caller queues on EP0? crg_udc_req_done() unlinks the request and drops udc_lock during giveback while setup_status still holds the pre-stall DATA_STAGE_* phase. A callback or concurrent usb_ep_queue() / usb_ep_set_halt() on EP0 can submit a new TRB during that window, only for setup_status to be unconditionally overwritten to WAIT_FOR_SETUP upon return without checking ep_generation, event_generation, or setup_tag. Unlike CMPL_CODE_SHORT_PKT, this branch also omits crg_udc_advance_dequeue_pt(udc_ep_ptr) before crg_udc_req_done(), leaving deq_pt behind enq_pt if a multi-TRB EP0 data stage stalls on a non-final TRB. Resetting setup_status and advancing deq_pt before calling crg_udc_req_done() avoids both issues. [ ... ] > + case CMPL_CODE_BABBLE_DETECTED_ERR: > + case CMPL_CODE_DATA_BUFFER_ERR: > + case CMPL_CODE_INVALID_STREAM_TYPE_ERR: > + case CMPL_CODE_RING_UNDERRUN: > + case CMPL_CODE_RING_OVERRUN: > + case CMPL_CODE_EVENT_RING_FULL_ERR: > + case CMPL_CODE_ISOCH_BUFFER_OVERRUN: > + case CMPL_CODE_USB_TRANS_ERR: > + case CMPL_CODE_TRB_ERR: > + default: > + /* Unknown codes are not proof that endpoint DMA has stopped. */ > + dev_err(crg_udc->dev, "transfer error, completion code 0x%x\n", > + comp_code); > + if (dci =3D=3D 0) { > + int ret; > + > + /* EP0 must stop DMA before completing a failed transfer. */ > + if (!list_empty(&udc_ep_ptr->queue)) { > + udc_req_ptr =3D list_first_entry(&udc_ep_ptr->queue, > + struct crg_udc_request, > + queue); > + if (udc_req_ptr->usb_req.status =3D=3D -EINPROGRESS) > + udc_req_ptr->usb_req.status =3D -EIO; > + } > + ret =3D crg_udc_abort_ep0_for_setup(crg_udc); > + if (ret) > + dev_err(crg_udc->dev, > + "failed to stop EP0 after transfer error: %d\n", > + ret); > + } else { > + crg_udc_stop_error_ep(crg_udc, dci, -EIO); > + } > + break; [Severity: Medium] Can CMPL_CODE_RING_UNDERRUN and CMPL_CODE_RING_OVERRUN abort a valid, newly queued isochronous request with -EIO? Ring underrun and overrun are empty-ring isochronous service-interval notifications whose event TRB pointer references enq_pt rather than a failed TD. If the gadget queues the next isochronous request at enq_pt before the IRQ drains the event, is_request_dequeued() matches the new request's first_trb and crg_udc_update_dequeue_pt() advances deq_pt past it. Routing those two codes into crg_udc_stop_error_ep(crg_udc, dci, -EIO) then stops the endpoint, skips the valid TD via crg_udc_skip_td(), and fails the request with -EIO. Exempting CMPL_CODE_RING_UNDERRUN and CMPL_CODE_RING_OVERRUN from dequeue-pointer updates and error teardown avoids disrupting isochronous streaming. [ ... ] > + /* A completed TD frees room, but this event may also have published > + * the next TD and filled the ring again. Recompute from the current > + * pointers: unconditionally clearing tran_ring_full loses that state. > + */ > + if (trbs_dequeued && dci && udc_ep_ptr->ep_state =3D=3D EP_STATE_RUNNIN= G && > + crg_udc_data_ep_current(udc_ep_ptr, ep_generation, event_generation= )) > + crg_udc_retry_pending_trbs(udc_ep_ptr); [Severity: Medium] Does calling crg_udc_retry_pending_trbs() on every non-EP0 completion trigger an O(Q) walk of udc_ep_ptr->queue under IRQ-disabled udc_lock? Whenever the ring has room, crg_udc_retry_pending_trbs() calls crg_udc_queue_pending_trbs(), which walks the entire queue from head to tail skipping requests with all_trbs_queued set. Because requests are scheduled in FIFO order, steady-state streaming with queue depth Q traverses all Q requests on every completion only to find nothing to submit. Checking in O(1) whether the queue is empty or its tail already has all_trbs_queued set (or tracking the first unscheduled request) would avoid the O(Q) per-completion scan. [ ... ] > + if (recovering || crg_udc->gadget.speed >=3D USB_SPEED_SUPER) { > + crg_udc_defer_trb_reuse(udc_ep_ptr); > + udc_ep_ptr->deq_pt =3D udc_ep_ptr->enq_pt; > + udc_ep_ptr->tran_ring_full =3D false; > + } > + udc_ep_ptr->ep_state =3D EP_STATE_HALTED; > + /* > + * A failed stop retained old DMA mappings even at HS/FS. > + * After a confirmed retry, all those requests can be given back. > + */ > + if (recovering || crg_udc->gadget.speed >=3D USB_SPEED_SUPER) > + crg_udc_ep_nuke(udc_ep_ptr, -ECONNRESET); [Severity: High] Does nuking queued requests on SuperSpeed halts break standard halt and clear-halt cycles? Standard USB gadget halt semantics keep queued requests intact across HALT and resume them on CLEAR_FEATURE(ENDPOINT_HALT), which is invisible to function drivers. Functions such as f_sourcesink treat -ECONNRESET as teardown and permanently free the request instead of requeuing it, leaving the endpoint stalled after the host clears the halt. Calling crg_udc_ep_nuke() synchronously from crg_udc_ep_halt() also runs request completion callbacks while gadget callers such as ffs_epfile_io() hold their own locks across usb_ep_set_halt(). [ ... ] > + } else { > + crg_udc_unmap_request(udc_ep, udc_req, udc_req->usb_req.actual); > + } > + > + if (udc_req->usb_req.complete) { > + crg_udc_callback_enter(crg_udc); > + spin_unlock(&crg_udc->udc_lock); > + usb_gadget_giveback_request(&udc_ep->usb_ep, &udc_req->usb_req); > + spin_lock(&crg_udc->udc_lock); > + crg_udc_callback_exit(crg_udc); > + } [Severity: High] Does calling crg_udc_unmap_request() before spin_unlock(&crg_udc->udc_lock) hold the global IRQ-disabled spinlock across DMA unmapping and full-payload bounce copies? crg_udc_unmap_request() runs dma_unmap_sgtable() or dma_unmap_single(), copies every received byte via kmap_local_page() and memcpy() for DMA_FROM_DEVICE transfers, and frees every bounce page. Under SuperSpeed workloads, doing this under udc_lock increases interrupt latency and stalls other CPUs calling ep_queue(), ep_dequeue(), or get_frame() across all endpoints. Because udc_req is already unlinked from udc_ep->queue and udc_lock is dropped immediately below for giveback, detaching the bounce pointer under udc_lock and unmapping, copying, and freeing it after releasing udc_lock would avoid holding the lock across this work. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261203-axiado-ax3= 000-usb-device-controller-v8-0-d5ff04febd4d@axiado.com?part=3D5