* [PATCH] usb: chipidea: udc: clean up bounce buffer and DMA mapping on enqueue error
@ 2026-11-25 22:22 Elkin Cruz
2026-11-25 22:50 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Elkin Cruz @ 2026-11-25 22:22 UTC (permalink / raw)
To: Peter Chen, Greg Kroah-Hartman
Cc: linux-usb, Xu Yang, linux-kernel, llvm, irogers, Elkin Cruz,
stable
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@vger.kernel.org
Signed-off-by: Elkin Cruz <elkin@google.com>
---
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();
+ goto err_unmap;
+ }
done:
return ret;
+
+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);
+ }
+ 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
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH] usb: chipidea: udc: clean up bounce buffer and DMA mapping on enqueue error
2026-11-25 22:22 [PATCH] usb: chipidea: udc: clean up bounce buffer and DMA mapping on enqueue error Elkin Cruz
@ 2026-11-25 22:50 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-25 22:50 UTC (permalink / raw)
To: Elkin Cruz; +Cc: linux-usb
> 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@vger.kernel.org
> Signed-off-by: Elkin Cruz <elkin@google.com>
Sashiko has reviewed this patch and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261125222212.1434148-1-elkin@google.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-25 22:50 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25 22:22 [PATCH] usb: chipidea: udc: clean up bounce buffer and DMA mapping on enqueue error Elkin Cruz
2026-11-25 22:50 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox