Re: [PATCH] usb: chipidea: udc: clean up bounce buffer and DMA mapping on enqueue error

From: Xu Yang

Date: Tue Dec 01 2026 - 04:12:39 EST


On Wed, Nov 25, 2026 at 10:22:12PM +0000, Elkin Cruz wrote:
> In _hardware_enqueue(), sglist_do_bounce() allocates a bounce buffer and
> scatterlist table in hwreq->sgt and swaps hwreq->req.sg and
> hwreq->req.num_sgs to point at the bounce table. If
> usb_gadget_map_request_by_dev(), prepare_td_for_sg() /
> prepare_td_for_non_sg(), or hw_ep_prime() subsequently fails,
> _hardware_enqueue() returned without unwinding the bounce table, DMA
> mapping, or allocated TD nodes. Because failed requests are never added
> to hwep->qh.queue, _hardware_dequeue() and _ep_nuke() never clean them
> up, leaking the bounce buffer and leaving hwreq->req.sg corrupted.
>
> When hw_ep_prime() fails, flush the endpoint and restore TD_TERMINATE in
> the queue head and any linked predecessor TD before freeing allocated TD
> nodes, unmapping the request, and calling sglist_do_debounce(hwreq,
> false).
>
> Fixes: edfcc455c85c ("usb: chipidea: udc: create bounce buffer for problem sglist entries if possible")
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Elkin Cruz <elkin@xxxxxxxxxx>
> ---
>
> I am trying out new Clang static analysis checks for kernel bug
> classes, and they flagged this code as possibly buggy. The findings
> were triaged/fixed and commit descriptions were drafted with help of AI,
> then checked against linux-next (not runtime-tested on hardware).
>
> Thanks,
> Elkin
>
> drivers/usb/chipidea/udc.c | 29 ++++++++++++++++++++++++++---
> 1 file changed, 26 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/usb/chipidea/udc.c b/drivers/usb/chipidea/udc.c
> index 98658b65b936..dad7087fc0cf 100644
> --- a/drivers/usb/chipidea/udc.c
> +++ b/drivers/usb/chipidea/udc.c
> @@ -681,6 +681,7 @@ static int _hardware_enqueue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
> struct ci_hdrc *ci = hwep->ci;
> int ret = 0;
> struct td_node *firstnode, *lastnode;
> + struct td_node *prevlastnode = NULL;
> unsigned int bounced_size;
> struct scatterlist *sg;
>
> @@ -705,7 +706,7 @@ static int _hardware_enqueue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
> ret = usb_gadget_map_request_by_dev(ci->dev->parent,
> &hwreq->req, hwep->dir);
> if (ret)
> - return ret;
> + goto err_debounce;
>
> if (hwreq->sgt.sgl) {
> /* We've mapped a bigger buffer, now recover the actual size */
> @@ -719,7 +720,7 @@ static int _hardware_enqueue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
> ret = prepare_td_for_non_sg(hwep, hwreq);
>
> if (ret)
> - return ret;
> + goto err_unmap;
>
> lastnode = list_entry(hwreq->tds.prev,
> struct td_node, td);
> @@ -740,7 +741,6 @@ static int _hardware_enqueue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
> struct ci_hw_req *hwreqprev;
> int n = hw_ep_bit(hwep->num, hwep->dir);
> int tmp_stat;
> - struct td_node *prevlastnode;
> u32 next = firstnode->dma & TD_ADDR_MASK;
>
> hwreqprev = list_entry(hwep->qh.queue.prev,
> @@ -790,8 +790,31 @@ static int _hardware_enqueue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq)
>
> ret = hw_ep_prime(ci, hwep->num, hwep->dir,
> hwep->type == USB_ENDPOINT_XFER_CONTROL);
> + if (ret) {
> + hw_ep_flush(ci, hwep->num, hwep->dir);
> + hwep->qh.ptr->td.next = cpu_to_le32(TD_TERMINATE);
> + if (prevlastnode)
> + prevlastnode->ptr->next = cpu_to_le32(TD_TERMINATE);
> + /* Ensure dQH/dTD link termination is visible before freeing TDs */
> + wmb();

I think above are not needed, because EP is already in halt state before prime it.

> + goto err_unmap;
> + }
> done:
> return ret;

In this case, should 0 be returned directly? Otherwise, it will duplicate another
return line.

> +
> +err_unmap:
> + list_for_each_entry_safe(firstnode, lastnode, &hwreq->tds, td) {
> + dma_pool_free(hwep->td_pool, firstnode->ptr, firstnode->dma);
> + list_del_init(&firstnode->td);
> + firstnode->ptr = NULL;
> + kfree(firstnode);
> + }

Above pattern occures in many place, could a function be created to simplify it?

Thanks,
Xu Yang

> + usb_gadget_unmap_request_by_dev(ci->dev->parent,
> + &hwreq->req, hwep->dir);
> +err_debounce:
> + if (hwreq->sgt.sgl)
> + sglist_do_debounce(hwreq, false);
> + return ret;
> }
>
> /**
> --
> 2.56.1.839.g22a8290db9-goog
>