PCI: pciehp: Fix AB-BA deadlocks between reset_lock, pci_rescan_remove_lock and pci_slot_mutex
Subsystems (4)
RFC PATCH v2 1/1 PCI: pciehp: Fix AB-BA deadlocks between reset_lock, pci_rescan_remove_lock and pci_slot_mutex
Commit 5b3f7b7d062b ("PCI: pciehp: Avoid slot access during reset") introduced ctrl->reset_lock to serialize a Secondary Bus Reset (SBR) with the pciehp code paths that read the Link Active and Presence Detect bits, both of which flap during an SBR.
pciehp_ist() holds reset_lock across the whole event handling, including enumerating or de-enumerating the hotplugged devices. That nests two PCI core locks inside reset_lock, each taken in the opposite order by a concurrent SBR, causing two AB-BA deadlocks.
First, reset_lock vs pci_rescan_remove_lock. pciehp takes them as reset_lock -> pci_rescan_remove_lock:
pciehp_ist() # down_read(reset_lock)
pciehp_handle_presence_or_link_change()
pciehp_enable_slot()
board_added()
pciehp_configure_device()
pci_lock_rescan_remove() # pci_rescan_remove_lock
A Root Port reset from link-down recovery takes them the other way round, pci_rescan_remove_lock -> reset_lock (the boot path, via pci_host_probe(), takes the same reverse order):
qcom_pcie_global_irq_thread()
pci_host_handle_link_down()
pci_host_reset_root_port()
pci_lock_rescan_remove() # pci_rescan_remove_lock
pci_bus_error_reset()
pci_reset_bridge()
pci_slot_reset()
pci_reset_hotplug_slot()
pciehp_reset_slot() # down_write(reset_lock)
Second, reset_lock vs pci_slot_mutex. pciehp takes them as reset_lock -> pci_slot_mutex:
pciehp_ist() # down_read(reset_lock)
pciehp_handle_presence_or_link_change()
pciehp_configure_device()
pci_scan_slot()
pci_scan_single_device()
pci_device_add()
pci_dev_assign_slot()
mutex_lock(&pci_slot_mutex) # pci_slot_mutex
An SBR of the same hierarchy (AER- or link-down-induced Root Port reset, or a sysfs "reset_subordinate" request) takes them the other way round, pci_slot_mutex -> reset_lock:
pci_bus_error_reset() / pci_try_reset_bridge()
pci_reset_bridge()
mutex_lock(&pci_slot_mutex) # pci_slot_mutex
pci_slot_reset()
pci_slot_lock() pci_reset_hotplug_slot()
pciehp_reset_slot() # down_write(reset_lock)
The second deadlock constrains the fix: pci_reset_bridge() must hold pci_slot_mutex to walk the slot list before it can reach pciehp_reset_slot(), and pciehp cannot reorder that. As long as pciehp holds reset_lock while descending into pci_scan_slot(), the reverse nesting is unavoidable regardless of pci_rescan_remove_lock ordering.
reset_lock only needs to protect the register reads against a concurrent SBR, not the enumeration itself. Fix both deadlocks by dropping reset_lock across the whole scan/remove section in pciehp_configure_device()/pciehp_unconfigure_device(), extending what commit f5eff5591b8f ("PCI: pciehp: Fix AB-BA deadlock between reset_lock and device_lock") already did around driver binding alone.
Without reset_lock there, an SBR can again race pci_scan_slot() and leave a register unreadable during enumeration, so the device fails to be enumerated correctly. Hand that serialization to pci_rescan_remove_lock, which already spans the scan.
pci_reset_bridge() and pci_reset_bus() issue the SBR unconditionally, with no check on what else is on the bus, so they need pci_rescan_remove_lock. pci_host_reset_root_port() no longer takes the lock itself, since pci_reset_bridge() now does.
pci_reset_function()/pci_try_reset_function() are left without the
lock: their SBR fallback (pci_dev_reset_slot_function(), then pci_parent_bus_reset()) only fires once it confirms the device is the sole occupant of its bus, so it cannot race the scan that populates that bus with a sibling device. And being able to call pci_reset_function(dev) at all means dev is already fully enumerated.
vmd_enable_domain() calls the new pci_reset_bus_unlocked(), skipping pci_rescan_remove_lock, since it runs from vmd_probe() with device_lock already held and taking the lock there would invert the lock order. This is safe because there is no concurrent scanner to
race: the VMD bridge was found by the initial root bus scan, and any hotplug controller below it does not exist yet at this point. Neither that scan nor pci_scan_child_bus() earlier in this same function was ever protected by reset_lock.
---
Changes in v2:
- Rebase on latest pci main branch
- Link to v1: https://lore.kernel.org/all/[email protected]/
--- drivers/pci/controller/pci-host-common.c | 2 - drivers/pci/controller/vmd.c | 2 +- drivers/pci/hotplug/pciehp_pci.c | 24 ++++++------ drivers/pci/pci.c | 64 +++++++++++++++++++++++++++++--- include/linux/pci.h | 1 + 5 files changed, 73 insertions(+), 20 deletions(-)
drivers/pci/controller/pci-host-common.c +0 -2
@@ -329,9 +329,7 @@ static pci_ers_result_t pci_host_reset_root_port(struct pci_dev *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 ;
drivers/pci/controller/vmd.c +1 -1
@@ -1112,7 +1112,7 @@ static int vmd_enable_domain(struct vmd_dev *vmd, unsigned long features)if (!list_empty (&child->devices)) { dev =list_first_entry (&child->devices, struct pci_dev, bus_list); - ret =pci_reset_bus (dev); + ret =pci_reset_bus_unlocked (dev);if (ret)pci_warn (dev,"can't reset device: %d\n" , ret);
drivers/pci/hotplug/pciehp_pci.c +12 -12
@@ -36,6 +36,11 @@ int pciehp_configure_device(struct controller *ctrl)struct pci_bus *parent = bridge->subordinate;int num, ret =0 ; +/* +* Release reset_lock before rescan/remove +* to avoid AB-BA deadlock with pci_rescan_remove_lock. +*/ +up_read (&ctrl->reset_lock);pci_lock_rescan_remove (); dev =pci_get_slot (parent,PCI_DEVFN (0 ,0 ));
@@ -64,13 +69,7 @@ int pciehp_configure_device(struct controller *ctrl)pci_assign_unassigned_bridge_resources (bridge);pcie_bus_configure_settings (parent); -/* -* Release reset_lock during driver binding -* to avoid AB-BA deadlock with device_lock. -*/ -up_read (&ctrl->reset_lock);pci_bus_add_devices (parent); -down_read_nested (&ctrl->reset_lock, ctrl->depth); dev =pci_get_slot (parent,PCI_DEVFN (0 ,0 )); ctrl->dsn =pci_get_dsn (dev);
@@ -78,6 +77,7 @@ int pciehp_configure_device(struct controller *ctrl) out:pci_unlock_rescan_remove (); +down_read_nested (&ctrl->reset_lock, ctrl->depth);return ret; }
@@ -104,6 +104,11 @@ void pciehp_unconfigure_device(struct controller *ctrl, bool presence)if (!presence)pci_walk_bus (parent, pci_dev_set_disconnected,NULL ); +/* +* Release reset_lock before rescan/remove +* to avoid AB-BA deadlock with pci_rescan_remove_lock. +*/ +up_read (&ctrl->reset_lock);pci_lock_rescan_remove ();/*
@@ -116,13 +121,7 @@ void pciehp_unconfigure_device(struct controller *ctrl, bool presence) bus_list) {pci_dev_get (dev); -/* -* Release reset_lock during driver unbinding -* to avoid AB-BA deadlock with device_lock. -*/ -up_read (&ctrl->reset_lock);pci_stop_and_remove_bus_device (dev); -down_read_nested (&ctrl->reset_lock, ctrl->depth);/* * Ensure that no new Requests will be generated from
@@ -138,4 +137,5 @@ void pciehp_unconfigure_device(struct controller *ctrl, bool presence) }pci_unlock_rescan_remove (); +down_read_nested (&ctrl->reset_lock, ctrl->depth); }
drivers/pci/pci.c +59 -5
97 lines not on this page · raw message
include/linux/pci.h +1 -0
@@ -1492,6 +1492,7 @@ int pci_try_reset_function(struct pci_dev *dev);int pci_probe_reset_slot (struct pci_slot *slot);int pci_probe_reset_bus (struct pci_bus *bus);int pci_reset_bus (struct pci_dev *dev); +int pci_reset_bus_unlocked (struct pci_dev *dev);void pci_reset_secondary_bus (struct pci_dev *dev);void pcibios_reset_secondary_bus (struct pci_dev *dev);void pci_update_resource (struct pci_dev *dev,int resno);
---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261126-fix_deadlock_1126-7b9bd5dade50
Best regards,
Signature · 1 line
Qiang Yu <[email protected]>
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 5 potential issues to consider: - [High] PCI: self-deadlock on pci_rescan_remove_lock in pci_reset_bridge() on EEH - [High] PCI…
Thank you for your contribution! Sashiko AI review found 5 potential issues to consider:
- [High] PCI: self-deadlock on pci_rescan_remove_lock in pci_reset_bridge() on EEH
- [High] PCI: self-deadlock in pci_reset_bus() from pci_fixup_final GPU reset quirk
- [High] PCI: deadlock between pci_reset_bus() and VFIO dev_set->lock or device close
- [High] PCI: AB-BA deadlocks in r8169 and mlx5 from blocking lock in pci_reset_bus()
- [Medium] PCI: AB-BA lock inversion between sysfs kn->active and pci_rescan_remove_lock
--
commit 7491161cc2c0678f53562522b13cdb8a3d4bfd7d
Author: Qiang Yu <[email protected]>
PCI: pciehp: Fix AB-BA deadlocks between reset_lock, pci_rescan_remove_lock and pci_slot_mutex
This commit drops ctrl->reset_lock across device enumeration and removal in pciehp and serializes secondary bus resets in pci_reset_bridge() and pci_reset_bus() with pci_rescan_remove_lock.
Quoted 8 lines
drivers/pci/pci.c +0
-0@@ -5750,6 +5750,13 @@ static int pci_reset_bridge(struct pci_dev *bridge, bool restore)if (!bus)return -ENOTTY ;
drivers/pci/pci.c static int pci_reset_bridge(struct pci_dev *bridge, bool restore) · line 5762
Quoted 10 lines
drivers/pci/pci.c +7
-0+/* +* The reset below may issue a Secondary Bus Reset, which races with +* pciehp enumerating a newly inserted device. Serialize via +* pci_rescan_remove_lock; callers must not already hold it. +*/ +lockdep_assert_not_held (&pci_rescan_remove_lock); +pci_lock_rescan_remove ();mutex_lock (&pci_slot_mutex);if (list_empty (&bus->slots)) goto bus_reset;
[Severity: High]
Does taking pci_rescan_remove_lock in pci_reset_bridge() self-deadlock during PowerPC EEH error recovery?
eeh_handle_normal_event() acquires pci_lock_rescan_remove() at entry and holds it while resetting the frozen PE:
eeh_handle_normal_event()
pci_lock_rescan_remove()
eeh_reset_device()
eeh_pe_reset_full()
eeh_pe_reset()
pnv_eeh_reset()
pci_bus_error_reset()
pci_reset_bridge()
lockdep_assert_not_held(&pci_rescan_remove_lock)
pci_lock_rescan_remove() <-- self-deadlockWhen a non-PHB, non-VF PE on a non-root bus undergoes a hot reset on PowerNV, the EEH recovery thread wedges permanently on pci_rescan_remove_lock and blocks all subsequent PCI hotplug, rescan and removal operations.
[Severity: Medium]
Does acquiring pci_rescan_remove_lock in pci_reset_bridge() also invert the lock order against the sysfs active reference (kn->active) for reset_subordinate?
Writing to a bridge's reset_subordinate sysfs attribute runs reset_subordinate_store() without calling sysfs_break_active_protection(), while concurrent removal of that bridge or an ancestor holds pci_rescan_remove_lock and waits for the attribute's kn->active to drain:
CPU0 CPU1
---- ----
pciehp_unconfigure_device()
pci_lock_rescan_remove()reset_subordinate_store() ...
/* holds kn->active */ device_del(&pdev->dev)
pci_try_reset_bridge() ...
pci_reset_bridge() kernfs_drain()
pci_lock_rescan_remove() /* waits for kn->active */*** DEADLOCK ***
The same kn->active -> pci_rescan_remove_lock inversion occurs in lpfc_board_mode_store() -> lpfc_reset_pci_bus() -> pci_reset_bus().
[ ... ]
Quoted 35 lines
drivers/pci/pci.c +21
-3@@ -5805,19 +5816,62 @@ int pci_probe_reset_bus(struct pci_bus *bus) }EXPORT_SYMBOL_GPL (pci_probe_reset_bus); +/* +* Core of pci_reset_bus(), run with pci_rescan_remove_lock already held or +* known not to be needed. See pci_reset_bus_unlocked() for the latter case. +*/ +static int __pci_reset_bus (struct pci_dev *pdev) +{ +return (!pci_probe_reset_slot (pdev->slot)) ? +pci_try_reset_slot (pdev->slot) :pci_try_reset_bus (pdev->bus); +} +/** * pci_reset_bus - Try to reset a PCI bus * @pdev: top level PCI device to reset via slot/bus * -* Same as above except return -EAGAIN if the bus cannot be locked +* Same as above except this blocks until pci_rescan_remove_lock can be +* acquired, and still returns -EAGAIN if the underlying slot/bus device +* lock cannot be taken. */ int pci_reset_bus (struct pci_dev *pdev) { -return (!pci_probe_reset_slot (pdev->slot)) ? -pci_try_reset_slot (pdev->slot) :pci_try_reset_bus (pdev->bus); +int rc; + +/* +* pci_try_reset_slot()/pci_try_reset_bus() below may issue a +* Secondary Bus Reset, which races with concurrent bus scanning. +* Serialize against that via pci_rescan_remove_lock, taken before +* the slot's/bus's device locks to match the lock order used by +* pciehp.
drivers/pci/pci.c int pci_probe_reset_bus(struct pci_bus *bus) · line 5856
Quoted 10 lines
drivers/pci/pci.c +8
-0+*/ +pci_lock_rescan_remove (); + + rc =__pci_reset_bus (pdev); + +pci_unlock_rescan_remove (); + +return rc; }EXPORT_SYMBOL_GPL (pci_reset_bus);
[Severity: High]
Can pci_reset_bus() self-deadlock when called from quirk_reset_lenovo_thinkpad_p50_nvgpu()?
quirk_reset_lenovo_thinkpad_p50_nvgpu() is a DECLARE_PCI_FIXUP_CLASS_FINAL quirk that calls pci_reset_bus(pdev):
drivers/pci/quirks.c:quirk_reset_lenovo_thinkpad_p50_nvgpu() {
...
if (ioread32(map + 0x2240c) & 0x2) {
pci_info(pdev, FW_BUG "GPU left initialized by EFI, resetting\n");
ret = pci_reset_bus(pdev);
...
}After boot, pci_bus_add_devices() -> pci_bus_add_device() runs final fixups while callers such as pci_rescan_bus() or pciehp_configure_device() already hold pci_rescan_remove_lock:
pci_rescan_bus()
pci_lock_rescan_remove() pci_bus_add_devices()
pci_bus_add_device()
pci_fixup_device(pci_fixup_final, dev)
quirk_reset_lenovo_thinkpad_p50_nvgpu()
pci_reset_bus()
pci_lock_rescan_remove() <-- self-deadlock
Should quirk_reset_lenovo_thinkpad_p50_nvgpu() call pci_reset_bus_unlocked(pdev) like vmd_enable_domain()?
[Severity: High]
Does blocking on pci_rescan_remove_lock in pci_reset_bus() deadlock when an open VFIO PCI device is hot-unplugged or removed via sysfs?
Previously, pci_reset_bus() only attempted non-blocking pci_slot_trylock()/pci_bus_trylock() calls and returned -EAGAIN if a removal thread held device_lock(&pdev->dev). When a VFIO device is removed while still open, vfio_unregister_group_dev() holds pci_rescan_remove_lock and device_lock(&pdev->dev) while waiting for userspace to close the device fd and complete device->comp. Closing the fd runs vfio_pci_core_disable(), where pci_dev_trylock(pdev) fails and falls back to vfio_pci_dev_set_try_reset() -> pci_reset_bus(pdev) before device->comp is signaled:
CPU0 CPU1
---- ----pciehp_unconfigure_device()
pci_lock_rescan_remove()
pci_stop_and_remove_bus_device()
...
device_lock(&pdev->dev)
vfio_pci_remove()
...
vfio_unregister_group_dev()
/* waits on device->comp */
vfio_device_fops_release()
...
vfio_pci_core_disable()
/* pci_dev_trylock() fails */
vfio_pci_dev_set_try_reset()
pci_reset_bus()
pci_lock_rescan_remove()*** DEADLOCK ***
In addition, both vfio_pci_dev_set_try_reset() and vfio_pci_dev_set_hot_reset() call pci_reset_bus() under dev_set->lock (dev_set->lock -> pci_rescan_remove_lock), inverting the pci_rescan_remove_lock -> dev_set->lock order taken by vfio_pci_probe() -> vfio_assign_device_set() and vfio_pci_remove() -> vfio_release_device_set().
[Severity: High]
Does blocking on pci_rescan_remove_lock in pci_reset_bus() also deadlock against workqueue flushing during device removal in r8169 and mlx5?
In r8169, when MMIO reads all-ones (such as on surprise removal), rtl_task() calls pci_reset_bus(tp->pci_dev). Meanwhile, hot-unplug or sysfs removal holds pci_rescan_remove_lock and device_lock(&pdev->dev) and calls rtl_remove_one(), waiting for rtl_task() to finish:
CPU0 CPU1 (kworker)
---- --------------pciehp_unconfigure_device()
pci_lock_rescan_remove()
pci_stop_and_remove_bus_device()
...
rtl_remove_one()
disable_work_sync(&tp->wk.work)
rtl_task()
pci_reset_bus()
pci_lock_rescan_remove()*** DEADLOCK ***
Before this change, pci_reset_bus() saw device_lock(&pdev->dev) held by rtl_remove_one() and returned -EAGAIN immediately. The same deadlock occurs in mlx5 between remove_one() -> mlx5_drain_fw_reset() ( holding pci_rescan_remove_lock) and the firmware reset work items calling mlx5_pci_reset_bus() -> pci_reset_bus(), as well as an ABBA lock inversion between devl_lock(devlink) in mlx5_devlink_reload_fw_activate() -> mlx5_pci_reset_bus() and probe_one()/remove_one() acquiring devl_lock(devlink) under pci_rescan_remove_lock.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 9 lines · Qiang Yu
Commit
5b3f7b7d062b("PCI: pciehp: Avoid slot access during reset") introducedctrl->reset_lockto serialize a Secondary Bus Reset (SBR) with the pciehp code paths that read the Link Active and Presence Detect bits, both of which flap during an SBR.
pciehp_ist()holdsreset_lockacross the whole event handling, including enumerating or de-enumerating the hotplugged devices. That nests two PCI core locks insidereset_lock, each taken in the opposite order by a concurrent SBR, causing two AB-BA deadlocks.
It looks like you're trying to fix two separate deadlocks in a single patch. Can this be split in two patches?
Quoted 4 lines
A Root Port reset from link-down recovery takes them the other way round,
pci_rescan_remove_lock->reset_lock(the boot path, viapci_host_probe(), takes the same reverse order):
Quoted 9 lines
qcom_pcie_global_irq_thread()
pci_host_handle_link_down()
pci_host_reset_root_port()
pci_lock_rescan_remove()#pci_rescan_remove_lock
pci_bus_error_reset()
pci_reset_bridge()
pci_slot_reset()
pci_reset_hotplug_slot()
pciehp_reset_slot()#down_write(reset_lock)
Why is it necessary to lock rescan/remove for a reset? This looks wrong. I suggest removing pci_lock_rescan_remove() from pci_host_reset_root_port().
Thanks,
Lukas
Hi Lukas,
Le dimanche 29 novembre 2026 à 22:06 +0100, Lukas Wunner a écrit :
Quoted 28 lines · Qiang Yu
Quoted 9 lines
Commit
5b3f7b7d062b("PCI: pciehp: Avoid slot access during reset") introducedctrl->reset_lockto serialize a Secondary Bus Reset (SBR) with the pciehp code paths that read the Link Active and Presence Detect bits, both of which flap during an SBR.
pciehp_ist()holdsreset_lockacross the whole event handling, including enumerating or de-enumerating the hotplugged devices. That nests two PCI core locks insidereset_lock, each taken in the opposite order by a concurrent SBR, causing two AB-BA deadlocks.It looks like you're trying to fix two separate deadlocks in a single patch. Can this be split in two patches?
Quoted 4 lines
A Root Port reset from link-down recovery takes them the other way round,
pci_rescan_remove_lock->reset_lock(the boot path, viapci_host_probe(), takes the same reverse order):Quoted 9 lines
qcom_pcie_global_irq_thread()
pci_host_handle_link_down()
pci_host_reset_root_port()
pci_lock_rescan_remove() #pci_rescan_remove_lockpci_bus_error_reset()
pci_reset_bridge()
pci_slot_reset()
pci_reset_hotplug_slot()
pciehp_reset_slot() #down_write(reset_lock)
Quoted 3 lines
Why is it necessary to lock rescan/remove for a reset? This looks wrong. I suggest removing
pci_lock_rescan_remove()frompci_host_reset_root_port().
Manivannan said in the v7's cover letter [1]:
"Added pci_{lock/unlock}_rescan_remove() to guard pci_bus_error_reset() as the device could be removed in-between due to Native hotplug interrupt."
On the pciehp side,
pciehp_unconfigure_device()
pci_lock_rescan_remove()
pci_stop_and_remove_bus_device()
pci_remove_bus_device()
pci_destroy_dev()
So if you don't get the unscan lock in the reset path, which can happen in parallel, you risk a use-after-free.
Cheers,
-Paul
Quoted 13 lines · Paul Cercueil
Quoted 3 lines · Lukas Wunner
Why is it necessary to lock rescan/remove for a reset? This looks wrong. I suggest removing
pci_lock_rescan_remove()frompci_host_reset_root_port().Manivannan said in the v7's cover letter:
https://lore.kernel.org/linux-pci/[email protected]/"Added pci_{lock/unlock}_rescan_remove() to guard
pci_bus_error_reset()as the device could be removed in-between due to Native hotplug interrupt."On the pciehp side,
Quoted 8 lines
pciehp_unconfigure_device()
pci_lock_rescan_remove()
pci_stop_and_remove_bus_device()
pci_remove_bus_device()
pci_destroy_dev()So if you don't get the unscan lock in the reset path, which can happen in parallel, you risk a use-after-free.
The proper approach to avoid a use after free is to acquire a reference on the pci_dev which is at risk of being removed in parallel.
E.g. in drivers/pci/pcie/aer.c, add_error_device() acquires a reference on the error-reporting device and that reference is released in aer_process_err_devices().
It's unclear to me which device you expect to be removed in parallel:
The Root Port? That can't happen via pciehp. The Root Port sits at the top of the hierarchy, there's no hotplug-capable port above it which pciehp could bind to.
I guess the Root Port could be removed if the user exercises the "remove" attribute in sysfs. But again, the right way to protect against that is to acquire a reference on the Root Port.
But maybe I'm missing something and you want to protect a different device than the Root Port against removal?
I've gone through the review feedback to v6 of Mani's series and I don't see anyone reporting a use-after-free, so it's unclear to me why the pci_lock_rescan_remove() was added in v7.
Thanks,
Lukas
Le mardi 01 décembre 2026 à 11:08 +0100, Lukas Wunner a écrit :
Quoted 31 lines · Paul Cercueil
Quoted 14 lines · Lukas Wunner
Quoted 3 lines
Why is it necessary to lock rescan/remove for a reset? This looks wrong. I suggest removing
pci_lock_rescan_remove()frompci_host_reset_root_port().Manivannan said in the v7's cover letter:
https://lore.kernel.org/linux-pci/[email protected]/"Added pci_{lock/unlock}_rescan_remove() to guard
pci_bus_error_reset()as the
device could be removed in-between due to Native hotplug interrupt."On the pciehp side,
Quoted 8 lines
pciehp_unconfigure_device()
pci_lock_rescan_remove()
pci_stop_and_remove_bus_device()
pci_remove_bus_device()
pci_destroy_dev()So if you don't get the unscan lock in the reset path, which can happen in parallel, you risk a use-after-free.
The proper approach to avoid a use after free is to acquire a reference on the
pci_devwhich is at risk of being removed in parallel.E.g. in
drivers/pci/pcie/aer.c,add_error_device()acquires a reference on the error-reporting device and that reference is released inaer_process_err_devices().
Quoted 8 lines
It's unclear to me which device you expect to be removed in parallel:
The Root Port? That can't happen via pciehp. The Root Port sits at the top of the hierarchy, there's no hotplug-capable port above it which pciehp could bind to.I guess the Root Port could be removed if the user exercises the "remove" attribute in sysfs. But again, the right way to protect against that is to acquire a reference on the Root Port.
Yes, I agree. I was just trying to show why the pci_lock_rescan_remove() was used there, I am not arguing that it was the best solution.
Quoted 6 lines
But maybe I'm missing something and you want to protect a different device than the Root Port against removal?
I've gone through the review feedback to v6 of Mani's series and I don't see anyone reporting a use-after-free, so it's unclear to me why the
pci_lock_rescan_remove()was added in v7.
I'll send a patch to change it to a reference grab.
Cheers,
-Paul
Quoted 44 lines · Paul Cercueil
Le mardi 01 décembre 2026 à 11:08 +0100, Lukas Wunner a écrit :
Quoted 31 lines · Paul Cercueil
Quoted 14 lines · Lukas Wunner
Quoted 3 lines
Why is it necessary to lock rescan/remove for a reset? This looks wrong. I suggest removing
pci_lock_rescan_remove()frompci_host_reset_root_port().Manivannan said in the v7's cover letter:
https://lore.kernel.org/linux-pci/[email protected]/"Added pci_{lock/unlock}_rescan_remove() to guard
pci_bus_error_reset()as the
device could be removed in-between due to Native hotplug interrupt."On the pciehp side,
Quoted 8 lines
pciehp_unconfigure_device()
pci_lock_rescan_remove()
pci_stop_and_remove_bus_device()
pci_remove_bus_device()
pci_destroy_dev()So if you don't get the unscan lock in the reset path, which can happen in parallel, you risk a use-after-free.
The proper approach to avoid a use after free is to acquire a reference on the
pci_devwhich is at risk of being removed in parallel.E.g. in
drivers/pci/pcie/aer.c,add_error_device()acquires a reference on the error-reporting device and that reference is released inaer_process_err_devices().Quoted 8 lines
It's unclear to me which device you expect to be removed in parallel:
The Root Port? That can't happen via pciehp. The Root Port sits at the top of the hierarchy, there's no hotplug-capable port above it which pciehp could bind to.I guess the Root Port could be removed if the user exercises the "remove" attribute in sysfs. But again, the right way to protect against that is to acquire a reference on the Root Port.
Yes, I agree. I was just trying to show why the
pci_lock_rescan_remove()was used there, I am not arguing that it was the best solution.
Quoted 9 lines
Quoted 6 lines
But maybe I'm missing something and you want to protect a different device than the Root Port against removal?
I've gone through the review feedback to v6 of Mani's series and I don't see anyone reporting a use-after-free, so it's unclear to me why the
pci_lock_rescan_remove()was added in v7.I'll send a patch to change it to a reference grab.
Add a reference alone doesn't cover the traversal: pci_destroy_dev() does list_del(&dev->bus_list), poisoning the field list_for_each_entry() follows to advance. So the iterator can still land on LIST_POISON1 with every pci_dev pinned.
None of the walks in the reset paths holds pci_bus_sem, eg. __pci_bus_lock and pci_bus_resettable.
- Qiang Yu
Quoted 6 lines · Lukas Wunner
Quoted 5 lines · Qiang Yu
Commit
5b3f7b7d062b("PCI: pciehp: Avoid slot access during reset") introducedctrl->reset_lockto serialize a Secondary Bus Reset (SBR) with the pciehp code paths that read the Link Active and Presence Detect bits, both of which flap during an SBR.
Quoted 8 lines
Quoted 4 lines
pciehp_ist()holdsreset_lockacross the whole event handling, including enumerating or de-enumerating the hotplugged devices. That nests two PCI core locks insidereset_lock, each taken in the opposite order by a concurrent SBR, causing two AB-BA deadlocks.It looks like you're trying to fix two separate deadlocks in a single patch. Can this be split in two patches?
Both deadlocks are fixed by the same hunk: in pciehp_configure_device() and pciehp_unconfigure_device(), the up_read() / down_read_nested() pair is moved so that it wraps the whole pci_lock_rescan_remove() section. reset_lock then no longer nests pci_rescan_remove_lock or pci_slot_mutex, which breaks both AB-BA chains at once.
What that hunk gives up is the serialization between a bridge reset and bus scanning, which reset_lock used to provide. The pci.c changes add it back via pci_rescan_remove_lock, so they belong with the same hunk -- hence one patch.
Quoted 14 lines
Quoted 4 lines
A Root Port reset from link-down recovery takes them the other way round,
pci_rescan_remove_lock->reset_lock(the boot path, viapci_host_probe(), takes the same reverse order):Quoted 9 lines
qcom_pcie_global_irq_thread()
pci_host_handle_link_down()
pci_host_reset_root_port()
pci_lock_rescan_remove()#pci_rescan_remove_lock
pci_bus_error_reset()
pci_reset_bridge()
pci_slot_reset()
pci_reset_hotplug_slot()
pciehp_reset_slot()#down_write(reset_lock)
Quoted 4 lines
Why is it necessary to lock rescan/remove for a reset? This looks wrong. I suggest removing
pci_lock_rescan_remove()frompci_host_reset_root_port().
It's needed because on qcom the reset goes through host->reset_root_port() -> qcom_pcie_reset_root_port() which powers down the PHY, disables clocks and regulators before re-training. So a concurrent bus scan would access config space and lead to NOC error in that window.
- Qiang Yu
Quoted 16 lines · Qiang Yu
Quoted 15 lines · Lukas Wunner
Quoted 13 lines · Qiang Yu
A Root Port reset from link-down recovery takes them the other way round,
pci_rescan_remove_lock->reset_lock(the boot path, viapci_host_probe(), takes the same reverse order):
qcom_pcie_global_irq_thread()
pci_host_handle_link_down()
pci_host_reset_root_port()
pci_lock_rescan_remove()#pci_rescan_remove_lock
pci_bus_error_reset()
pci_reset_bridge()
pci_slot_reset()
pci_reset_hotplug_slot()
pciehp_reset_slot()#down_write(reset_lock)
Quoted 8 lines
Quoted 3 lines
Why is it necessary to lock rescan/remove for a reset? This looks wrong. I suggest removing
pci_lock_rescan_remove()frompci_host_reset_root_port().It's needed because on qcom the reset goes through
host->reset_root_port()->qcom_pcie_reset_root_port()which powers down the PHY, disables clocks and regulators before re-training. So a concurrent bus scan would access config space and lead to NOC error in that window.
Doesn't that apply to *any* access to the devices during reset? Why prevent a bus scan specifically?
If this is Qualcomm-specific, why is it in a generic helper?
If the Root Port is hotplug-capable, pciehp is prevented from de-enumeration and re-enumeration by the mechanism introduced by 2af781a9edc4, so why acquire an extra lock to prevent the same thing?
I assume the NOC error causes an NMI? Shouldn't the NMI handler catch that and handle it gracefully?
Thanks,
Lukas
Quoted 21 lines · Lukas Wunner
Quoted 20 lines · Qiang Yu
Quoted 18 lines · Lukas Wunner
Quoted 4 lines · Qiang Yu
A Root Port reset from link-down recovery takes them the other way round,
pci_rescan_remove_lock->reset_lock(the boot path, viapci_host_probe(), takes the same reverse order):Quoted 9 lines
qcom_pcie_global_irq_thread()
pci_host_handle_link_down()
pci_host_reset_root_port()
pci_lock_rescan_remove()#pci_rescan_remove_lock
pci_bus_error_reset()
pci_reset_bridge()
pci_slot_reset()
pci_reset_hotplug_slot()
pciehp_reset_slot()#down_write(reset_lock)Why is it necessary to lock rescan/remove for a reset? This looks wrong. I suggest removing
pci_lock_rescan_remove()frompci_host_reset_root_port().
Quoted 8 lines
Quoted 4 lines
It's needed because on qcom the reset goes through
host->reset_root_port()->qcom_pcie_reset_root_port()which powers down the PHY, disables clocks and regulators before re-training. So a concurrent bus scan would access config space and lead to NOC error in that window.Doesn't that apply to *any* access to the devices during reset? Why prevent a bus scan specifically?
It does apply to any access, and I'm not claiming a bus scan is special. The point is that the other accesses are already accounted for and a bus scan isn't.
For AER and link-down recovery, drivers are asked to quiesce MMIO via the error_detected callback before the reset. After a hotplug removal, pciehp marks the devices disconnected and config space access is short-circuited in pci_{read,write}_config_*().
We obviously can't defend against arbitrary accesses -- userspace poking config space or an MMIO mapping directly, say. But every legitimate in-kernel access during a reset should be covered one way or another, and a bus scan currently isn't. If there are others, I'd say they want fixing too.
Quoted 2 lines
If this is Qualcomm-specific, why is it in a generic helper?
The race isn't qcom-specific, the severity is. qcom's .reset_root_port() is just an alternative way of resetting the link and is reached from the generic pcibios_reset_secondary_bus(). A plain SBR racing a bus scan leaves the device mis-enumerated; the same race on qcom takes the controller's clocks and regulators away mid-scan.
Quoted 4 lines
If the Root Port is hotplug-capable, pciehp is prevented from de-enumeration and re-enumeration by the mechanism introduced by
2af781a9edc4, so why acquire an extra lock to prevent the same thing?
I don't think it prevents the same thing. 2af781a9edc4 filters the spurious hotplug events that an SBR causes, it doesn't stop an SBR from happening in the middle of pci_scan_slot() or pci_bus_add_devices().
What stops the SBR there is pciehp_reset_slot() blocking on down_write(reset_lock) -- which is what 5b3f7b7d062b introduced reset_lock for.
Quoted 3 lines
I assume the NOC error causes an NMI? Shouldn't the NMI handler catch that and handle it gracefully?
This NOC error always ends in a crash.
- Qiang Yu