From: sashiko-bot@kernel.org
To: "Petar Stepanovic" <pstepanovic@axiado.com>
Cc: linux-usb@vger.kernel.org, devicetree@vger.kernel.org,
robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH v8 5/6] usb: gadget: udc: crg: Add gadget and endpoint operations
Date: Fri, 04 Dec 2026 02:03:22 +0000 [thread overview]
Message-ID: <sashiko-outbox-248936@kernel.org> (raw)
In-Reply-To: <20261203-axiado-ax3000-usb-device-controller-v8-5-d5ff04febd4d@axiado.com>
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 <pstepanovic@axiado.com>
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/gadget/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 = crg_udc_skip_td(udc_ep_ptr, udc_req);
> + if (ret)
> + goto recover;
> + udc_req->usb_req.actual =
> + 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 = -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 = 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 = crg_udc_recover_endpoints(crg_udc, &flags);
> + if (ret && udc_ep->ep_state == EP_STATE_DISABLED && !udc_ep->nuking &&
> + list_empty(&udc_ep->queue))
> + udc_ep->usb_ep.enabled = 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 = udc_ep->crg_udc->dev;
> + struct usb_request *req = &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 = true, which allows FunctionFS
DMABUF attachment. When ffs_dmabuf_transfer() submits a pre-mapped
scatterlist with sg_was_mapped = 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 = USB_SPEED_SUPER_PLUS;
> + break;
> + case CRG_U3DC_PORTSC_SPEED_SS:
> + speed = USB_SPEED_SUPER;
> + break;
> + case CRG_U3DC_PORTSC_SPEED_HS:
> + speed = USB_SPEED_HIGH;
> + break;
> +
> + case CRG_U3DC_PORTSC_SPEED_FS:
> + speed = USB_SPEED_FULL;
> + break;
> +
> + case CRG_U3DC_PORTSC_SPEED_LS:
> + speed = USB_SPEED_LOW;
> + break;
> + default:
> + speed = USB_SPEED_UNKNOWN;
> + goto csc_done;
> + }
> + crg_udc->gadget.speed = speed;
> +
> + ret = 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 = 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 = 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 = WAIT_FOR_SETUP;
> + break;
[Severity: Medium]
Can setting setup_status = 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 == 0) {
> + int ret;
> +
> + /* EP0 must stop DMA before completing a failed transfer. */
> + if (!list_empty(&udc_ep_ptr->queue)) {
> + udc_req_ptr = list_first_entry(&udc_ep_ptr->queue,
> + struct crg_udc_request,
> + queue);
> + if (udc_req_ptr->usb_req.status == -EINPROGRESS)
> + udc_req_ptr->usb_req.status = -EIO;
> + }
> + ret = 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 == EP_STATE_RUNNING &&
> + 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 >= USB_SPEED_SUPER) {
> + crg_udc_defer_trb_reuse(udc_ep_ptr);
> + udc_ep_ptr->deq_pt = udc_ep_ptr->enq_pt;
> + udc_ep_ptr->tran_ring_full = false;
> + }
> + udc_ep_ptr->ep_state = 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 >= 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.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203-axiado-ax3000-usb-device-controller-v8-0-d5ff04febd4d@axiado.com?part=5
next prev parent reply other threads:[~2026-12-04 2:03 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-04 1:31 [PATCH v8 0/6] Axiado AX3000 and AX3005 USB Device Controller (UDC) Petar Stepanovic
2026-12-04 1:31 ` [PATCH v8 1/6] dt-bindings: usb: gadget: udc: add Axiado AX3000 and AX3005 UDC Petar Stepanovic
2026-12-04 1:31 ` [PATCH v8 2/6] usb: gadget: udc: crg: Add hardware definitions and structs Petar Stepanovic
2026-12-04 1:53 ` sashiko-bot
2026-12-04 1:31 ` [PATCH v8 3/6] usb: gadget: udc: crg: Add ring and transfer management Petar Stepanovic
2026-12-04 1:53 ` sashiko-bot
2026-12-04 1:31 ` [PATCH v8 4/6] usb: gadget: udc: crg: Add endpoint 0 handling Petar Stepanovic
2026-12-04 2:03 ` sashiko-bot
2026-12-04 1:31 ` [PATCH v8 5/6] usb: gadget: udc: crg: Add gadget and endpoint operations Petar Stepanovic
2026-12-04 2:03 ` sashiko-bot [this message]
2026-12-04 1:31 ` [PATCH v8 6/6] usb: gadget: udc: crg: Add core driver and build integration Petar Stepanovic
2026-12-04 2:07 ` 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-248936@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=pstepanovic@axiado.com \
--cc=robh@kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
/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