PCI: Fix root port reset lock ordering issue
Subsystems (4)
PATCH v2 1/1 PCI: Fix root port reset lock ordering issue
Currently, pci_host_reset_root_port() calls pci_lock_rescan_remove(), to protect pci_bus_error_reset() racing against pciehp_unconfigure_device().
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().
This is a typical ABBA locking issue. However, the rescan lock is used in pci_host_reset_root_port() only to protect against the PCI device disappearing. We can keep this protection while fixing this locking issue by getting a reference to the PCI device instead.
---
Changelog:
v2:
- Grab a reference to the PCI device in pci_host_reset_root_port() instead of grabbing the rescan lock. This makes the ABBA locking issue disappear.
--- drivers/pci/controller/pci-host-common.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/pci/controller/pci-host-common.c +1 -2
@@ -327,11 +327,10 @@ EXPORT_SYMBOL_GPL(pci_host_common_d3cold_possible);static pci_ers_result_t pci_host_reset_root_port (struct pci_dev *dev) { + struct pci_dev *pdev __free(pci_dev_put) = pci_dev_get(dev);int ret; -pci_lock_rescan_remove (); ret =pci_bus_error_reset (dev); -pci_unlock_rescan_remove ();if (ret) {pci_err (dev,"Failed to reset Root Port: %d\n" , ret);return PCI_ERS_RESULT_DISCONNECT ;
Signature · 1 line
2.47.3
Quoted 3 lines · Paul Cercueil
Currently,
pci_host_reset_root_port()callspci_lock_rescan_remove(), to protectpci_bus_error_reset()racing againstpciehp_unconfigure_device().
Per Qiang, this is necessary to avoid NOC errors when accessing devices during reset, not because of a race with pciehp:
Hi Lukas,
Le mardi 01 décembre 2026 à 12:11 +0100, Lukas Wunner a écrit :
Quoted 9 lines · Paul Cercueil
Quoted 3 lines
Currently,
pci_host_reset_root_port()callspci_lock_rescan_remove(), to protectpci_bus_error_reset()racing againstpciehp_unconfigure_device().Per Qiang, this is necessary to avoid NOC errors when accessing devices during reset, not because of a race with pciehp:
Understood. I'll leave you guys to it.
Cheers,
-Paul
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] PCI: pci_host_reset_root_port() leaves subordinate bus devices unprotected - [High] PCI: ABBA…
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] PCI: pci_host_reset_root_port() leaves subordinate bus devices unprotected
- [High] PCI: ABBA deadlock remains between pci_reset_bridge() and pciehp_ist()
- [Medium] PCI: pci_host_reset_root_port() drops port ref inside pcie_do_recovery()
--
commit 46b7cfcd0b5a0c434fbfd94302c48b513dec7fea
Author: Paul Cercueil <[email protected]>
PCI: Fix root port reset lock ordering issue
This commit replaces pci_lock_rescan_remove() in pci_host_reset_root_port() with a pci_dev_get() reference on the Root Port to avoid an ABBA deadlock with pciehp's reset_lock.
Quoted 7 lines
drivers/pci/controller/pci-host-common.c +0
-0@@ -327,11 +327,10 @@ EXPORT_SYMBOL_GPL(pci_host_common_d3cold_possible);static pci_ers_result_t pci_host_reset_root_port (struct pci_dev *dev)
drivers/pci/controller/pci-host-common.c EXPORT_SYMBOL_GPL(pci_host_common_d3cold_possible); · line 336
Quoted 10 lines
drivers/pci/controller/pci-host-common.c +1
-2{ + struct pci_dev *pdev __free(pci_dev_put) = pci_dev_get(dev);int ret; -pci_lock_rescan_remove (); ret =pci_bus_error_reset (dev); -pci_unlock_rescan_remove ();if (ret) {pci_err (dev,"Failed to reset Root Port: %d\n" , ret);return PCI_ERS_RESULT_DISCONNECT ;
[Severity: High]
Does taking a reference on the Root Port dev leave its subordinate device list (dev->subordinate->devices) unprotected against pciehp_unconfigure_device() and pciehp_configure_device()?
dev here is the Root Port itself, whereas pciehp removes and frees child devices on dev->subordinate->devices. Inside pci_bus_error_reset() -> pci_reset_bridge() -> pci_slot_reset() (and pci_bus_reset()), pci_slot_resettable(), __pci_bus_lock() and __pci_bus_unlock() walk bus->devices without holding pci_rescan_remove_lock, pci_bus_sem or ctrl->reset_lock.
Meanwhile, pciehp_unconfigure_device() drops ctrl->reset_lock around pci_stop_and_remove_bus_device(), relying on pci_lock_rescan_remove() for serialization:
CPU0 CPU1pci_host_reset_root_port(dev) pciehp_ist()
pci_bus_error_reset(dev) pciehp_unconfigure_device()
... pci_lock_rescan_remove()
pci_slot_reset() up_read(&ctrl->reset_lock)
pci_slot_lock() pci_stop_and_remove_bus_device()
__pci_bus_lock() ...
list_for_each_entry(child, pci_destroy_dev(child)
&bus->devices, list_del(&child->bus_list)
bus_list) put_device(&child->dev)
pci_dev_lock(child) <-- use-after-freeSimilarly, if pciehp_configure_device() adds new devices via pci_scan_slot() and then drops ctrl->reset_lock around pci_bus_add_devices(), __pci_bus_unlock() can walk bus->devices and call pci_dev_unlock() on newly added child devices that __pci_bus_lock() never locked.
[Severity: High]
Does an ABBA deadlock still remain between pci_reset_bridge() and pciehp_ist() via pci_slot_mutex and ctrl->reset_lock?
Even without pci_rescan_remove_lock, the two paths described in the commit message still take pci_slot_mutex and ctrl->reset_lock in opposite orders:
CPU0 CPU1
---- ----lock(&pci_slot_mutex)
lock(&ctrl->reset_lock)
lock(&ctrl->reset_lock)
lock(&pci_slot_mutex)
*** DEADLOCK ***
CPU0: pci_host_reset_root_port() -> pci_bus_error_reset() ->
pci_reset_bridge() -> pci_slot_reset() -> pci_reset_hotplug_slot() ->
pciehp_reset_slot()
CPU1: pciehp_ist() -> ... -> pciehp_configure_device() -> pci_scan_slot() ->
pci_scan_single_device() -> pci_scan_device() -> pci_setup_device() ->
pci_dev_assign_slot()[Severity: Medium]
When CONFIG_PCIEAER is enabled, does scoping pci_dev_get(dev) to pci_host_reset_root_port() drop the Root Port reference before pcie_do_recovery() finishes using it?
pci_host_recover_root_port() passes pci_host_reset_root_port as the reset_subordinates callback to pcie_do_recovery(port, ...), and callers of pci_host_handle_link_down(port) do not hold a reference on port. Because pdev uses __free(pci_dev_put) inside pci_host_reset_root_port(), the reference is released as soon as reset_subordinates(bridge) returns, while pcie_do_recovery() continues to dereference bridge and dev during the post-reset walk and AER status cleanup.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1