Linux PCI subsystem development
 help / color / mirror / Atom feed
* [PATCH] PCI: Fix root port reset lock ordering
@ 2026-11-30 15:32 Paul Cercueil
  2026-11-30 15:45 ` sashiko-bot
  0 siblings, 1 reply; 3+ messages in thread
From: Paul Cercueil @ 2026-11-30 15:32 UTC (permalink / raw)
  To: Bjorn Helgaas
  Cc: Manivannan Sadhasivam, Thomas Petazzoni, linux-pci, Paul Cercueil,
	stable

Currently, pci_host_reset_root_port() calls pci_lock_rescan_remove(),
to protect pci_bus_error_reset().

The reset function then calls pci_reset_bridge(), then pci_slot_reset(),
pci_reset_hotplug_slot(), which will call the .reset_slot callback.
For a pciehp root port, this calls the pciehp_reset_slot() function,
which will grab the 'reset_lock'.

On the other hand, the pciehp IRQ handler pciehp_ist() gets the
'reset_lock' first, then calls pciehp_handle_presence_or_link_change(),
which will call pciehp_enable_slot(), __pciehp_enable_slot(),
board_added(), then finally pciehp_configure_device(), which will call
pci_lock_rescan_remove().

Address this ABBA locking issue by temporarily releasing the
'reset_lock' in pciehp_{un}configure_device, just to grab the rescan
lock before locking the 'reset_lock' once again.

Fixes: 4c99bace4f4e ("PCI: host-common: Add link down handling for Root Ports")
Cc: stable@vger.kernel.org
Signed-off-by: Paul Cercueil <paul.cercueil@bootlin.com>
---
 drivers/pci/hotplug/pciehp_pci.c | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)

diff --git a/drivers/pci/hotplug/pciehp_pci.c b/drivers/pci/hotplug/pciehp_pci.c
index 65e50bee1a8c..20549c83870e 100644
--- a/drivers/pci/hotplug/pciehp_pci.c
+++ b/drivers/pci/hotplug/pciehp_pci.c
@@ -21,11 +21,28 @@
 #include "../pci.h"
 #include "pciehp.h"
 
+static inline void pciehp_lock_rescan_remove(struct controller *ctrl)
+{
+	/*
+	 * pciehp_ist() calls us holding reset_lock, whereas the error-reset
+	 * path takes pci_rescan_remove_lock before reset_lock.  Drop the
+	 * reader to acquire the rescan lock as the outermost lock, then
+	 * reacquire reset_lock below it so the order rescan lock ->
+	 * reset_lock is preserved.
+	 */
+	lockdep_assert_held_read(&ctrl->reset_lock);
+	up_read(&ctrl->reset_lock);
+
+	pci_lock_rescan_remove();
+	down_read_nested(&ctrl->reset_lock, ctrl->depth);
+}
+
 /**
  * pciehp_configure_device() - enumerate PCI devices below a hotplug bridge
  * @ctrl: PCIe hotplug controller
  *
  * Enumerate PCI devices below a hotplug bridge and add them to the system.
+ * The caller must hold @ctrl->reset_lock for reading.
  * Return 0 on success, %-EEXIST if the devices are already enumerated or
  * %-ENODEV if enumeration failed.
  */
@@ -36,7 +53,7 @@ int pciehp_configure_device(struct controller *ctrl)
 	struct pci_bus *parent = bridge->subordinate;
 	int num, ret = 0;
 
-	pci_lock_rescan_remove();
+	pciehp_lock_rescan_remove(ctrl);
 
 	dev = pci_get_slot(parent, PCI_DEVFN(0, 0));
 	if (dev) {
@@ -91,6 +108,7 @@ int pciehp_configure_device(struct controller *ctrl)
  * Unbind PCI devices below a hotplug bridge from their drivers and remove
  * them from the system.  Safely removed devices are quiesced.  Surprise
  * removed devices are marked as such to prevent further accesses.
+ * The caller must hold @ctrl->reset_lock for reading.
  */
 void pciehp_unconfigure_device(struct controller *ctrl, bool presence)
 {
@@ -104,7 +122,7 @@ void pciehp_unconfigure_device(struct controller *ctrl, bool presence)
 	if (!presence)
 		pci_walk_bus(parent, pci_dev_set_disconnected, NULL);
 
-	pci_lock_rescan_remove();
+	pciehp_lock_rescan_remove(ctrl);
 
 	/*
 	 * Stopping an SR-IOV PF device removes all the associated VFs,
-- 
2.47.3


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

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

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 15:32 [PATCH] PCI: Fix root port reset lock ordering Paul Cercueil
2026-11-30 15:45 ` sashiko-bot
2026-11-30 16:10   ` Paul Cercueil

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