From: Paul Menzel <pmenzel@molgen.mpg.de>
To: Mathias Nyman <mathias.nyman@intel.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Mathias Nyman <mathias.nyman@linux.intel.com>,
George D Sworo <george.d.sworo@intel.com>,
Matt DeVillier <matt.devillier@gmail.com>,
Paul Menzel <pmenzel@molgen.mpg.de>,
linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH 2/2] xhci: check for a pending command completion during command timeout
Date: Fri, 22 May 2026 10:58:27 +0200 [thread overview]
Message-ID: <20260522085828.24142-4-pmenzel@molgen.mpg.de> (raw)
In-Reply-To: <20260522085828.24142-2-pmenzel@molgen.mpg.de>
From: Mathias Nyman <mathias.nyman@linux.intel.com>
It's possible a command times out even if xHC hardware already completed
the command. Driver is unaware of the command completion if interrupt
handler is blocked for a long time.
Check if there is an unhandled command completion on the event ring during
command timeout.
In this case just give the command additional time to complete. There's no
point in aborting the command ring to move past a stuck command.
Signed-off-by: Mathias Nyman <mathias.nyman@linux.intel.com>
Signed-off-by: George D Sworo <george.d.sworo@intel.com>
Link: https://chromium.googlesource.com/chromiumos/third_party/kernel/+/478ab723af9414b0a2a2fbc59ac34f5d319a4fc3
[pmenzel: one adaptation for mainline 7.1: next_trb() uses the
2-argument form next_trb(&seg, &deq) — the mainline 7.1 signature
dropped the xhci and ring arguments present in the 6.12 source the
patch was ported from. xhci_pending_interrupt() is used directly as
it is now committed as the preceding prerequisite.]
Assisted-by: Claude Sonnet 4.6
[pmenzel: No devices with the problem available, but no regressions on
Dell XPS 13 9360 and QEMU 7.2.0.
qemu-system-x86_64 -enable-kvm -cpu host -m 3G -device qemu-xhci,id=xhci -device usb-storage,bus=xhci.0
xHCI host controller initialised cleanly, USB 3.0 SuperSpeed root
hubs and USB mass storage device enumerated without errors.
The specific race (command timeout with blocked interrupt handler)
cannot easily be forced in QEMU, but no regressions in the normal
command path were observed.]
Cc: Matt DeVillier <matt.devillier@gmail.com>
Signed-off-by: Paul Menzel <pmenzel@molgen.mpg.de>
---
drivers/usb/host/xhci-ring.c | 30 ++++++++++++++++++++++++++++++
1 file changed, 30 insertions(+)
diff --git a/drivers/usb/host/xhci-ring.c b/drivers/usb/host/xhci-ring.c
index e47e644b296e..7f023558af22 100644
--- a/drivers/usb/host/xhci-ring.c
+++ b/drivers/usb/host/xhci-ring.c
@@ -1714,6 +1714,28 @@ void xhci_cleanup_command_queue(struct xhci_hcd *xhci)
xhci_complete_del_and_free_cmd(cur_cmd, COMP_COMMAND_ABORTED, 0);
}
+static bool xhci_pending_command_completion(struct xhci_hcd *xhci)
+{
+ struct xhci_segment *seg = xhci->interrupters[0]->event_ring->deq_seg;
+ union xhci_trb *deq = xhci->interrupters[0]->event_ring->dequeue;
+ u32 deq_flags = le32_to_cpu(deq->event_cmd.flags);
+ u32 cycle = xhci->interrupters[0]->event_ring->cycle_state;
+ int i = 0;
+
+ /* Check if event ring contains an unhandled command completion */
+ while ((deq_flags & TRB_CYCLE) == cycle) {
+ if ((deq_flags & TRB_TYPE_BITMASK) == TRB_TYPE(TRB_COMPLETION))
+ return true;
+ if (last_trb_on_ring(xhci->interrupters[0]->event_ring, seg, deq))
+ cycle ^= 1;
+ next_trb(&seg, &deq);
+ deq_flags = le32_to_cpu(deq->event_cmd.flags);
+ if (i++ > TRBS_PER_SEGMENT)
+ break;
+ }
+ return false;
+}
+
void xhci_handle_command_timeout(struct work_struct *work)
{
struct xhci_hcd *xhci;
@@ -1736,6 +1758,14 @@ void xhci_handle_command_timeout(struct work_struct *work)
return;
}
+ /* Did hw complete the command but event handler was blocked? */
+ if (xhci_pending_interrupt(xhci) > 0 &&
+ xhci_pending_command_completion(xhci)) {
+ xhci_dbg(xhci, "Command timeout with unhandled command completion\n");
+ xhci_mod_cmd_timer(xhci);
+ goto time_out_completed;
+ }
+
cmd_field3 = le32_to_cpu(xhci->current_cmd->command_trb->generic.field[3]);
usbsts = readl(&xhci->op_regs->status);
xhci_dbg(xhci, "Command timeout, USBSTS:%s\n", xhci_decode_usbsts(str, usbsts));
--
2.53.0
next prev parent reply other threads:[~2026-05-22 9:01 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-22 8:58 [PATCH 1/2] xhci: add helper to check for pending interrupt Paul Menzel
2026-05-22 8:58 ` Paul Menzel [this message]
2026-05-22 9:23 ` [PATCH 2/2] xhci: check for a pending command completion during command timeout Greg Kroah-Hartman
2026-05-22 9:35 ` Paul Menzel
2026-05-22 10:39 ` Greg Kroah-Hartman
2026-05-22 11:11 ` Mathias Nyman
2026-05-22 12:38 ` Paul Menzel
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=20260522085828.24142-4-pmenzel@molgen.mpg.de \
--to=pmenzel@molgen.mpg.de \
--cc=george.d.sworo@intel.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=mathias.nyman@intel.com \
--cc=mathias.nyman@linux.intel.com \
--cc=matt.devillier@gmail.com \
/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
all inboxes | Powered by JetHome®