Skip to content

Commit 94f3391

Browse files
matnymangregkh
authored andcommitted
xhci: Fix failure to give back some cached cancelled URBs.
Only TDs with status TD_CLEARING_CACHE will be given back after cache is cleared with a set TR deq command. xhci_invalidate_cached_td() failed to set the TD_CLEARING_CACHE status for some cancelled TDs as it assumed an endpoint only needs to clear the TD it stopped on. This isn't always true. For example with streams enabled an endpoint may have several stream rings, each stopping on a different TDs. Note that if an endpoint has several stream rings, the current code will still only clear the cache of the stream pointed to by the last cancelled TD in the cancel list. This patch only focus on making sure all canceled TDs are given back, avoiding hung task after device removal. Another fix to solve clearing the caches of all stream rings with cancelled TDs is needed, but not as urgent. This issue was simultanously discovered and debugged by by Tao Wang, with a slightly different fix proposal. Fixes: 674f843 ("xhci: split handling halted endpoints into two steps") Cc: <[email protected]> #5.12 Reported-by: Tao Wang <[email protected]> Signed-off-by: Mathias Nyman <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 4843b4b commit 94f3391

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

drivers/usb/host/xhci-ring.c

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -942,17 +942,21 @@ static int xhci_invalidate_cancelled_tds(struct xhci_virt_ep *ep)
942942
td->urb->stream_id);
943943
hw_deq &= ~0xf;
944944

945-
if (td->cancel_status == TD_HALTED) {
946-
cached_td = td;
947-
} else if (trb_in_td(xhci, td->start_seg, td->first_trb,
948-
td->last_trb, hw_deq, false)) {
945+
if (td->cancel_status == TD_HALTED ||
946+
trb_in_td(xhci, td->start_seg, td->first_trb, td->last_trb, hw_deq, false)) {
949947
switch (td->cancel_status) {
950948
case TD_CLEARED: /* TD is already no-op */
951949
case TD_CLEARING_CACHE: /* set TR deq command already queued */
952950
break;
953951
case TD_DIRTY: /* TD is cached, clear it */
954952
case TD_HALTED:
955-
/* FIXME stream case, several stopped rings */
953+
td->cancel_status = TD_CLEARING_CACHE;
954+
if (cached_td)
955+
/* FIXME stream case, several stopped rings */
956+
xhci_dbg(xhci,
957+
"Move dq past stream %u URB %p instead of stream %u URB %p\n",
958+
td->urb->stream_id, td->urb,
959+
cached_td->urb->stream_id, cached_td->urb);
956960
cached_td = td;
957961
break;
958962
}
@@ -961,18 +965,24 @@ static int xhci_invalidate_cancelled_tds(struct xhci_virt_ep *ep)
961965
td->cancel_status = TD_CLEARED;
962966
}
963967
}
964-
if (cached_td) {
965-
cached_td->cancel_status = TD_CLEARING_CACHE;
966968

967-
err = xhci_move_dequeue_past_td(xhci, slot_id, ep->ep_index,
968-
cached_td->urb->stream_id,
969-
cached_td);
970-
/* Failed to move past cached td, try just setting it noop */
971-
if (err) {
972-
td_to_noop(xhci, ring, cached_td, false);
973-
cached_td->cancel_status = TD_CLEARED;
969+
/* If there's no need to move the dequeue pointer then we're done */
970+
if (!cached_td)
971+
return 0;
972+
973+
err = xhci_move_dequeue_past_td(xhci, slot_id, ep->ep_index,
974+
cached_td->urb->stream_id,
975+
cached_td);
976+
if (err) {
977+
/* Failed to move past cached td, just set cached TDs to no-op */
978+
list_for_each_entry_safe(td, tmp_td, &ep->cancelled_td_list, cancelled_td_list) {
979+
if (td->cancel_status != TD_CLEARING_CACHE)
980+
continue;
981+
xhci_dbg(xhci, "Failed to clear cancelled cached URB %p, mark clear anyway\n",
982+
td->urb);
983+
td_to_noop(xhci, ring, td, false);
984+
td->cancel_status = TD_CLEARED;
974985
}
975-
cached_td = NULL;
976986
}
977987
return 0;
978988
}

0 commit comments

Comments
 (0)