usb: chipidea: udc: clean up bounce buffer and DMA mapping on enqueue error
Subsystems (3)
PATCH 1/1 usb: chipidea: udc: clean up bounce buffer and DMA mapping on enqueue error
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).
---
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(-)
drivers/usb/chipidea/udc.c +26 -3
@@ -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; }/**
Signature · 1 line
2.56.1.839.g22a8290db9-goog
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 10 lines
In
_hardware_enqueue(),sglist_do_bounce()allocates a bounce buffer and scatterlist table inhwreq->sgtand swapshwreq->req.sgandhwreq->req.num_sgsto point at the bounce table. Ifusb_gadget_map_request_by_dev(),prepare_td_for_sg()/prepare_td_for_non_sg(), orhw_ep_prime()subsequently fails,_hardware_enqueue()returned without unwinding the bounce table, DMA mapping, or allocated TD nodes. Because failed requests are never added tohwep->qh.queue,_hardware_dequeue()and_ep_nuke()never clean them up, leaking the bounce buffer and leavinghwreq->req.sgcorrupted.
Quoted 8 lines
When
hw_ep_prime()fails, flush the endpoint and restoreTD_TERMINATEin the queue head and any linked predecessor TD before freeing allocated TD nodes, unmapping the request, and callingsglist_do_debounce(hwreq, false).Fixes edfcc455c85c ("usb: chipidea: udc: create bounce buffer for problem sglist entries if possible")Signed-off-by Elkin Cruz <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 70 lines · Elkin Cruz
In
_hardware_enqueue(),sglist_do_bounce()allocates a bounce buffer and scatterlist table inhwreq->sgtand swapshwreq->req.sgandhwreq->req.num_sgsto point at the bounce table. Ifusb_gadget_map_request_by_dev(),prepare_td_for_sg()/prepare_td_for_non_sg(), orhw_ep_prime()subsequently fails,_hardware_enqueue()returned without unwinding the bounce table, DMA mapping, or allocated TD nodes. Because failed requests are never added tohwep->qh.queue,_hardware_dequeue()and_ep_nuke()never clean them up, leaking the bounce buffer and leavinghwreq->req.sgcorrupted.When
hw_ep_prime()fails, flush the endpoint and restoreTD_TERMINATEin the queue head and any linked predecessor TD before freeing allocated TD nodes, unmapping the request, and callingsglist_do_debounce(hwreq, false).Fixes edfcc455c85c ("usb: chipidea: udc: create bounce buffer for problem sglist entries if possible")Signed-off-by Elkin Cruz <[email protected]>---
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,
Elkindrivers/usb/chipidea/udc.c | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-)drivers/usb/chipidea/udc.c +3
-3@@ -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,
drivers/usb/chipidea/udc.c static int _hardware_enqueue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq) · line 799
Quoted 10 lines
drivers/usb/chipidea/udc.c +7
-0@@ -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.
drivers/usb/chipidea/udc.c static int _hardware_enqueue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq) · line 803
Quoted 4 lines
drivers/usb/chipidea/udc.c +2
-0+ goto err_unmap; + } done:return ret;
In this case, should 0 be returned directly? Otherwise, it will duplicate another return line.
drivers/usb/chipidea/udc.c static int _hardware_enqueue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq) · line 811
Quoted 8 lines
drivers/usb/chipidea/udc.c +8
-0+ +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
Quoted 12 lines
drivers/usb/chipidea/udc.c +6
-0+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; }/** Signature · 1 line
2.56.1.839.g22a8290db9-goog