Linux USB
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Michal Pecio" <michal.pecio@gmail.com>
Cc: linux-usb@vger.kernel.org
Subject: Re: [PATCH 2/4] usb: xhci: Give back stranded URBs on disconnected endpoints
Date: Thu, 29 Oct 2026 07:23:39 +0000	[thread overview]
Message-ID: <sashiko-outbox-188860@kernel.org> (raw)
In-Reply-To: <20261029075543.3fe902b3.michal.pecio@gmail.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] usb: xhci: uncleared deferred TDs in xhci_invalidate_cancelled_tds()
--

commit b1010543af7d4802d6d3abbb86ff8ccfdb2101da
Author: Michal Pecio <michal.pecio@gmail.com>

usb: xhci: Give back stranded URBs on disconnected endpoints

This commit gives back remaining URBs on endpoints marked with
EP_DROP_PENDING by cancelling remaining TDs before invalidation on EP0 and
calling xhci_kill_endpoint_urbs() after cancelled TD processing on other
endpoints.

> diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c
> index 698f994e43a55..49483eb55dc83 100644
> --- a/drivers/usb/host/xhci-ring.c
> +++ b/drivers/usb/host/xhci-ring.c

[ ... ]

> @@ -953,6 +954,11 @@ static void xhci_giveback_invalidated_tds(struct xhci_hcd *xhci, struct xhci_vir
>  		if (xhci->xhc_state & XHCI_STATE_DYING)
>  			return;
>  	}
> +
> +	/* Nuke stranded URBs if there is no one to give them back */
> +	if ((ep->ep_state & EP_DROP_PENDING) &&
> +	    !(ep->ep_state & (EP_HALTED | SET_DEQ_PENDING | EP_STOP_CMD_PENDING)))
> +		xhci_kill_endpoint_urbs(xhci, ep->vdev->slot_id, ep->ep_index);

[Severity: High]
Can this skip xhci_td_cleanup() and leak DMA bounce buffer mappings on
disconnected stream-enabled endpoints when multiple streams have cancelled
TDs stopped at their hardware dequeue pointers?

In drivers/usb/host/xhci-ring.c, xhci_invalidate_cancelled_tds() first
unlinks every TD on ep->cancelled_td_list from its transfer ring via
list_del_init(&td->td_list). When cancelled TDs are stopped at hw_deq across
multiple streams, the first TD becomes cached_td with TD_CLEARING_CACHE,
while TDs in other streams are marked TD_CLEARING_CACHE_DEFERRED:

drivers/usb/host/xhci-ring.c:xhci_invalidate_cancelled_tds() {
	...
	if (cached_td->urb->stream_id != td->urb->stream_id) {
		/* Multiple streams case, defer move dq */
		xhci_dbg(xhci,
			 "Move dq deferred: stream %u URB %p\n",
			 td->urb->stream_id, td->urb);
		td->cancel_status = TD_CLEARING_CACHE_DEFERRED;
		break;
	}
	...
}

Later in xhci_invalidate_cancelled_tds(), the early return for disconnected
non-control endpoints only clears cached_td:

	/* Don't bother fixing disconnected endpoints except EP0 */
	if (ep->ep_state & EP_DROP_PENDING && ep->ep_index != 0) {
		cached_td->cancel_status = TD_CLEARED;
		return 0;
	}

Any TDs in other streams remain in TD_CLEARING_CACHE_DEFERRED without a
Set TR Dequeue command queued.

When xhci_giveback_invalidated_tds() runs, those deferred TDs are skipped by
the first loop because td->cancel_status != TD_CLEARED, and are then handed
to xhci_kill_endpoint_urbs(). Because they were already removed from
ring->td_list, xhci_kill_ring_urbs() does not see them, and they fall
through to the ep->cancelled_td_list loop at the end of
xhci_kill_endpoint_urbs():

drivers/usb/host/xhci-ring.c:xhci_kill_endpoint_urbs() {
	...
	list_for_each_entry_safe(cur_td, tmp, &ep->cancelled_td_list,
			cancelled_td_list) {
		list_del_init(&cur_td->cancelled_td_list);
		inc_td_cnt(cur_td->urb);

		if (last_td_in_urb(cur_td))
			xhci_giveback_urb_in_irq(xhci, cur_td, -ESHUTDOWN);
	}
}

Unlike xhci_kill_ring_urbs() and xhci_td_cleanup(), this loop does not call
xhci_unmap_td_bounce_buffer() (leaving seg->bounce_dma mapped and
seg->bounce_len non-zero) and overwrites the cancelled URB's td->status with
-ESHUTDOWN. Additionally, if EP_HALTED is set on the endpoint, those
already-cancelled URBs will not be given back here either.

Should the EP_DROP_PENDING && ep->ep_index != 0 path in
xhci_invalidate_cancelled_tds() also mark any TD_CLEARING_CACHE_DEFERRED TDs
as TD_CLEARED (similar to the xhci_move_deq_to_next_td() error path right
below it) so that xhci_giveback_invalidated_tds() cleans them up normally?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261029075409.4b203658.michal.pecio@gmail.com?part=2

  reply	other threads:[~2026-10-29  7:23 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-24 17:47 [REGRESSION] usbhid -110 timeouts and reset loop for Keychron M5 (3434:d068) at boot since v7.3 Dennis Wissel
2026-10-24 20:34 ` Yiyu Zhou
2026-10-24 20:35 ` Yiyu Zhou
2026-10-24 23:10   ` Dennis Wissel
2026-10-25  8:59     ` Thorsten Leemhuis
2026-10-26  9:28       ` Mathias Nyman
2026-10-26  9:49         ` Thorsten Leemhuis
2026-10-26 10:47         ` Greg KH
2026-10-25 10:14     ` Michal Pecio
2026-10-25 11:11       ` Dennis Wissel
2026-10-26 11:15         ` Michal Pecio
2026-10-26 20:39           ` Yiyu Zhou
2026-10-27  7:29             ` Dennis Wissel
2026-10-27  8:48               ` [PATCH] xhci: keep ep0 blocked after disconnect until Address Device reloads it Yiyu Zhou
2026-10-27  9:10                 ` sashiko-bot
2026-10-27 10:36                 ` Mathias Nyman
2026-10-27 11:27                   ` Michal Pecio
2026-10-27 18:40                   ` Yiyu Zhou
2026-10-27 19:21                     ` [RFT PATCH 0/2] xhci: clean up endpoint rings after disconnect detect Mathias Nyman
2026-10-27 19:21                       ` [RFT PATCH 1/2] xhci: give back all TDs when skipping endpoint reset after disconnect Mathias Nyman
2026-10-27 19:21                       ` [RFT PATCH 2/2] xhci: clean up ep0 ring after disconnect instead of leaving stale TRBs Mathias Nyman
2026-10-27 23:05                       ` [RFT PATCH 0/2] xhci: clean up endpoint rings after disconnect detect Dennis Wissel
2026-10-29 14:07                         ` Mathias Nyman
2026-10-28  7:07                       ` Michal Pecio
2026-10-29  6:54                         ` [PATCH 0/4] Fix problems on disconnected devices in 7.3 Michal Pecio
2026-10-29  6:54                           ` [PATCH 1/4] usb: xhci: Fix TD invalidation on disconnected endpoints Michal Pecio
2026-10-29  7:12                             ` sashiko-bot
2026-10-29  6:55                           ` [PATCH 2/4] usb: xhci: Give back stranded URBs " Michal Pecio
2026-10-29  7:23                             ` sashiko-bot [this message]
2026-10-29  6:56                           ` [PATCH 3/4] usb: xhci: Give back URBs on halted " Michal Pecio
2026-10-29  7:19                             ` sashiko-bot
2026-10-29  6:57                           ` [PATCH 4/4] usb: xhci: Reject URBs to disconnected devices Michal Pecio
2026-10-29  7:24                             ` sashiko-bot
2026-10-29  4:18                       ` [RFT PATCH 0/2] xhci: clean up endpoint rings after disconnect detect Yiyu Zhou
2026-10-29 13:15                         ` Mathias Nyman
2026-10-27 10:58             ` [REGRESSION] usbhid -110 timeouts and reset loop for Keychron M5 (3434:d068) at boot since v7.3 Michal Pecio
2026-10-26  9:18       ` Mathias Nyman
2026-10-26  9:51         ` Michal Pecio
2026-10-26 11:38           ` Michal Pecio
2026-10-26 11:54             ` Mathias Nyman
2026-10-26 18:56           ` Alan Stern
2026-10-26 18:58             ` Alan Stern
2026-10-25  6:28 ` Greg KH

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-188860@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=michal.pecio@gmail.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox