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 --- 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