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

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 may sleep 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.")
Signed-off-by: Ziheng Wei <zihengw03@163.com>
---
 drivers/usb/host/xhci-hub.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/host/xhci-hub.c b/drivers/usb/host/xhci-hub.c
index 470bafe18..4d76f7857 100644
--- a/drivers/usb/host/xhci-hub.c
+++ b/drivers/usb/host/xhci-hub.c
@@ -722,14 +722,17 @@ static int xhci_enter_test_mode(struct xhci_hcd *xhci, u16 test_mode, int portnu
 	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 @@ static int xhci_exit_test_mode(struct xhci_hcd *xhci)
 		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 @@ int xhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
 			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
-- 
2.43.0


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

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

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

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