Skip to contentSkip to conversation

PCI: Fix root port reset lock ordering

PATCH In review Paul Cercueil 3 participants 4 messages Nov 30

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PCI: Fix root port reset lock ordering

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")
---
 drivers/pci/hotplug/pciehp_pci.c | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)
drivers/pci/hotplug/pciehp_pci.c +20 -2
index 65e50bee1a8c..20549c83870e 100644
@@ -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,
Signature · 1 line
2.47.3
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] PCI: pciehp: ABBA deadlock with pci_slot_mutex in pciehp_lock_rescan_remove() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] PCI: pciehp: ABBA deadlock with pci_slot_mutex in pciehp_lock_rescan_remove()
--

commit 70e71b321e54d0d7c6157b8dbdebfb52c2965df1
Author: Paul Cercueil <[email protected]>

PCI: Fix root port reset lock ordering

This commit adds pciehp_lock_rescan_remove() to temporarily release ctrl->reset_lock while acquiring pci_rescan_remove_lock in pciehp_configure_device() and pciehp_unconfigure_device().

Quoted 13 lines
drivers/pci/hotplug/pciehp_pci.c +5 -0
index 65e50bee1a8c0..20549c83870e2 100644
@@ -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

drivers/pci/hotplug/pciehp_pci.c line 38

Quoted 10 lines
drivers/pci/hotplug/pciehp_pci.c +10 -0
+	 * 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);
+}

[Severity: High]
Does re-acquiring ctrl->reset_lock here leave an ABBA deadlock with pci_slot_mutex?

CPU0 CPU1
---- ----
down_read(&ctrl->reset_lock)
mutex_lock(&pci_slot_mutex)
mutex_lock(&pci_slot_mutex)

                                        down_write(&ctrl->reset_lock)

*** DEADLOCK ***

CPU0: pciehp_ist() -> ... -> pciehp_configure_device() ->

      pciehp_lock_rescan_remove() -> pci_scan_slot() -> ... ->
      pci_setup_device() -> pci_dev_assign_slot()
CPU1: aer_root_reset() -> pci_bus_error_reset() -> pci_reset_bridge() ->
      pci_slot_reset() -> pci_reset_hotplug_slot() -> pciehp_reset_slot()

In pciehp_configure_device(), pci_scan_slot() and pci_hp_add_bridge() run while ctrl->reset_lock is held and take pci_slot_mutex via pci_dev_assign_slot(). Meanwhile, pci_reset_bridge() takes pci_slot_mutex before calling into pciehp_reset_slot(), which takes ctrl->reset_lock for writing.

In pci_host_reset_root_port(), where pci_rescan_remove_lock is held around pci_bus_error_reset(), the lock order is pci_rescan_remove_lock -> pci_slot_mutex -> ctrl->reset_lock, which triggers a lockdep circular locking splat against pciehp_configure_device() (pci_rescan_remove_lock -> ctrl->reset_lock -> pci_slot_mutex). Callers of pci_reset_bridge() that do not hold pci_rescan_remove_lock, such as aer_root_reset() and reset_subordinate_store(), can deadlock at runtime with pciehp_configure_device().

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw

Hi Sashiko,

Le lundi 30 novembre 2026 à 15:45 +0000, [email protected] a écrit :

Quoted 60 lines

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] PCI: pciehp: ABBA deadlock with pci_slot_mutex in pciehp_lock_rescan_remove()
--

commit 70e71b321e54d0d7c6157b8dbdebfb52c2965df1
Author: Paul Cercueil <[email protected]>

PCI: Fix root port reset lock ordering

This commit adds pciehp_lock_rescan_remove() to temporarily release ctrl->reset_lock while acquiring pci_rescan_remove_lock in pciehp_configure_device() and pciehp_unconfigure_device().

Quoted 15 lines
drivers/pci/hotplug/pciehp_pci.c +0 -0
index 65e50bee1a8c0..20549c83870e2 100644
@@ -21,11 +21,28 @@
  #include "../pci.h"
  #include "pciehp.h"

drivers/pci/hotplug/pciehp_pci.c +4 -0
+static inline void pciehp_lock_rescan_remove(struct controller *ctrl)
+{
+	/*
+	 * pciehp_ist() calls us holding reset_lock, whereas the error-

reset

drivers/pci/hotplug/pciehp_pci.c +1 -0
+	 * path takes pci_rescan_remove_lock before reset_lock.  Drop the

drivers/pci/hotplug/pciehp_pci.c line 38

Quoted 10 lines
drivers/pci/hotplug/pciehp_pci.c +10 -0
+	 * 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);
+}

[Severity: High]
Does re-acquiring ctrl->reset_lock here leave an ABBA deadlock with pci_slot_mutex?

CPU0                                    CPU1
----                                    ---- down_read(&ctrl->reset_lock)
                                        mutex_lock(&pci_slot_mutex) mutex_lock(&pci_slot_mutex)
                                        down_write(&ctrl->reset_lock)

 *** DEADLOCK ***

CPU0: pciehp_ist() -> ... -> pciehp_configure_device() -> pciehp_lock_rescan_remove() -> pci_scan_slot() -> ... -> pci_setup_device() -> pci_dev_assign_slot()
CPU1: aer_root_reset() -> pci_bus_error_reset() -> pci_reset_bridge() -> pci_slot_reset() -> pci_reset_hotplug_slot() -> pciehp_reset_slot()

Quoted 5 lines

In pciehp_configure_device(), pci_scan_slot() and pci_hp_add_bridge() run while ctrl->reset_lock is held and take pci_slot_mutex via pci_dev_assign_slot().  Meanwhile, pci_reset_bridge() takes pci_slot_mutex before calling into pciehp_reset_slot(), which takes ctrl->reset_lock for writing.

Ok, that's a deadlock, but it's also a pre-existing one and unrelated to my fix.

This patch only fixes the rescan lock / reset_lock inversion introduced by the link-down recovery path, so I'd like to keep it scoped to that.

Quoted 8 lines

In pci_host_reset_root_port(), where pci_rescan_remove_lock is held around pci_bus_error_reset(), the lock order is pci_rescan_remove_lock -> pci_slot_mutex -> ctrl->reset_lock, which triggers a lockdep circular locking splat against pciehp_configure_device() (pci_rescan_remove_lock -> ctrl->reset_lock -> pci_slot_mutex).  Callers of pci_reset_bridge() that do not hold pci_rescan_remove_lock, such as aer_root_reset() and reset_subordinate_store(), can deadlock at runtime with pciehp_configure_device().

This is BS. How do you see a deadlock here, while also saying that both pci_host_reset_root_port() and pciehp_configure_device() start by grabbing the rescan lock?

-Paul

#3 lore raw
In reply to Paul Cercueil

[cc += Qiang]

Quoted 14 lines · Paul Cercueil

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().

Quoted 3 lines

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.

Duplicate of:

https://lore.kernel.org/r/[email protected]/

Please see the discussion there.