Linux USB
 help / color / mirror / Atom feed
* [PATCH v2] usb: xhci: drop xhci->lock around pm_runtime calls in test mode
@ 2026-11-30 13:42 Ziheng Wei
  2026-11-30 13:48 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Ziheng Wei @ 2026-11-30 13:42 UTC (permalink / raw)
  To: Mathias Nyman, Greg Kroah-Hartman; +Cc: linux-usb, linux-kernel, stable

xhci_enter_test_mode() calls pm_runtime_forbid() and xhci_exit_test_mode()
calls pm_runtime_allow() while holding the IRQ-safe spinlock xhci->lock.
Both runtime PM helpers take dev->power.lock with spin_lock_irq() and
unconditionally re-enable hard IRQs (and pm_runtime_forbid() may also
sleep via rpm_resume() if the controller is not irq-safe), triggering
lockdep HARDIRQ-ON-W warnings and potential deadlock.

Drop and reacquire xhci->lock around pm_runtime_forbid() and
pm_runtime_allow(), matching the existing lock-drop pattern in
xhci_enter_test_mode().

Found by LLM-assisted static analysis; the xhci_enter_test_mode()
HARDIRQ-ON-W lockdep warning was reproduced in QEMU with PROVE_LOCKING
and the fix was compile-tested on x86_64.

Fixes: 0f1d832ed1fb ("usb: xhci: Add port test modes support for usb2.")
Cc: stable@vger.kernel.org
Signed-off-by: Ziheng Wei <zihengw03@163.com>
---
v1 -> v2: add Cc: stable@vger.kernel.org alongside the Fixes tag.

diff --git a/drivers/usb/host/xhci-hub.c b/drivers/usb/host/xhci-hub.c
--- a/drivers/usb/host/xhci-hub.c
+++ b/drivers/usb/host/xhci-hub.c
@@ -722,14 +722,17 @@
 	if (retval)
 		return retval;
 	/* Disable runtime PM for test mode */
+	spin_unlock_irqrestore(&xhci->lock, *flags);
 	pm_runtime_forbid(xhci_to_hcd(xhci)->self.controller);
+	spin_lock_irqsave(&xhci->lock, *flags);
 	/* Set PORTPMSC.PTC field to enter selected test mode */
 	xhci_dbg(xhci, "Enter Test Mode: %u, Port_id=%d\n", test_mode, portnum + 1);
 	xhci_port_set_test_mode(xhci, test_mode, portnum);
 	return retval;
 }
 
-static int xhci_exit_test_mode(struct xhci_hcd *xhci)
+static int xhci_exit_test_mode(struct xhci_hcd *xhci, unsigned long *flags)
+	__must_hold(&xhci->lock)
 {
 	int retval;
 
@@ -743,7 +746,9 @@
 		if (retval)
 			return retval;
 	}
+	spin_unlock_irqrestore(&xhci->lock, *flags);
 	pm_runtime_allow(xhci_to_hcd(xhci)->self.controller);
+	spin_lock_irqsave(&xhci->lock, *flags);
 	xhci->test_mode = 0;
 	return xhci_reset(xhci, XHCI_RESET_SHORT_USEC);
 }
@@ -1584,7 +1589,7 @@
 			xhci_set_port_power(xhci, port, false, &flags);
 			break;
 		case USB_PORT_FEAT_TEST:
-			retval = xhci_exit_test_mode(xhci);
+			retval = xhci_exit_test_mode(xhci, &flags);
 			break;
 		default:
 			goto error;

base-commit: 97d73c8251226cf27cc6656b4c542869bb8c69fa


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-30 13:48 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 13:42 [PATCH v2] usb: xhci: drop xhci->lock around pm_runtime calls in test mode Ziheng Wei
2026-11-30 13:48 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox