mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] usb: chipidea: udc: clean up bounce buffer and DMA mapping on enqueue error
@ 2026-11-25 22:22 Elkin Cruz
  0 siblings, 0 replies; only message 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	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-25 22:23 UTC | newest]

Thread overview: (only message) (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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®