Skip to content

Commit d25d850

Browse files
Wesley Chenggregkh
authored andcommitted
usb: dwc3: gadget: Use list_replace_init() before traversing lists
The list_for_each_entry_safe() macro saves the current item (n) and the item after (n+1), so that n can be safely removed without corrupting the list. However, when traversing the list and removing items using gadget giveback, the DWC3 lock is briefly released, allowing other routines to execute. There is a situation where, while items are being removed from the cancelled_list using dwc3_gadget_ep_cleanup_cancelled_requests(), the pullup disable routine is running in parallel (due to UDC unbind). As the cleanup routine removes n, and the pullup disable removes n+1, once the cleanup retakes the DWC3 lock, it references a request who was already removed/handled. With list debug enabled, this leads to a panic. Ensure all instances of the macro are replaced where gadget giveback is used. Example call stack: Thread#1: __dwc3_gadget_ep_set_halt() - CLEAR HALT -> dwc3_gadget_ep_cleanup_cancelled_requests() ->list_for_each_entry_safe() ->dwc3_gadget_giveback(n) ->dwc3_gadget_del_and_unmap_request()- n deleted[cancelled_list] ->spin_unlock ->Thread#2 executes ... ->dwc3_gadget_giveback(n+1) ->Already removed! Thread#2: dwc3_gadget_pullup() ->waiting for dwc3 spin_lock ... ->Thread#1 released lock ->dwc3_stop_active_transfers() ->dwc3_remove_requests() ->fetches n+1 item from cancelled_list (n removed by Thread#1) ->dwc3_gadget_giveback() ->dwc3_gadget_del_and_unmap_request()- n+1 deleted[cancelled_list] ->spin_unlock Fix this condition by utilizing list_replace_init(), and traversing through a local copy of the current elements in the endpoint lists. This will also set the parent list as empty, so if another thread is also looping through the list, it will be empty on the next iteration. Fixes: d4f1afe ("usb: dwc3: gadget: move requests to cancelled_list") Cc: stable <[email protected]> Acked-by: Felipe Balbi <[email protected]> Signed-off-by: Wesley Cheng <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent b47b0b6 commit d25d850

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

drivers/usb/dwc3/gadget.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,9 +1741,13 @@ static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep)
17411741
{
17421742
struct dwc3_request *req;
17431743
struct dwc3_request *tmp;
1744+
struct list_head local;
17441745
struct dwc3 *dwc = dep->dwc;
17451746

1746-
list_for_each_entry_safe(req, tmp, &dep->cancelled_list, list) {
1747+
restart:
1748+
list_replace_init(&dep->cancelled_list, &local);
1749+
1750+
list_for_each_entry_safe(req, tmp, &local, list) {
17471751
dwc3_gadget_ep_skip_trbs(dep, req);
17481752
switch (req->status) {
17491753
case DWC3_REQUEST_STATUS_DISCONNECTED:
@@ -1761,6 +1765,9 @@ static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep)
17611765
break;
17621766
}
17631767
}
1768+
1769+
if (!list_empty(&dep->cancelled_list))
1770+
goto restart;
17641771
}
17651772

17661773
static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
@@ -2958,15 +2965,22 @@ static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep,
29582965
{
29592966
struct dwc3_request *req;
29602967
struct dwc3_request *tmp;
2968+
struct list_head local;
29612969

2962-
list_for_each_entry_safe(req, tmp, &dep->started_list, list) {
2970+
restart:
2971+
list_replace_init(&dep->started_list, &local);
2972+
2973+
list_for_each_entry_safe(req, tmp, &local, list) {
29632974
int ret;
29642975

29652976
ret = dwc3_gadget_ep_cleanup_completed_request(dep, event,
29662977
req, status);
29672978
if (ret)
29682979
break;
29692980
}
2981+
2982+
if (!list_empty(&dep->started_list))
2983+
goto restart;
29702984
}
29712985

29722986
static bool dwc3_gadget_ep_should_continue(struct dwc3_ep *dep)

0 commit comments

Comments
 (0)