Linux PCI subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Qiang Yu" <qiang.yu@oss.qualcomm.com>
Cc: linux-pci@vger.kernel.org
Subject: Re: [PATCH RFC v2] PCI: pciehp: Fix AB-BA deadlocks between reset_lock, pci_rescan_remove_lock and pci_slot_mutex
Date: Thu, 26 Nov 2026 08:52:51 +0000	[thread overview]
Message-ID: <sashiko-outbox-235061@kernel.org> (raw)
In-Reply-To: <20261126-fix_deadlock_1126-v2-1-c0ed50bc7b04@oss.qualcomm.com>

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 <qiang.yu@oss.qualcomm.com>

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.

> diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
> index 7c6f2aba81443..211808f7bd1f6 100644
> --- a/drivers/pci/pci.c
> +++ b/drivers/pci/pci.c
> @@ -5750,6 +5750,13 @@ static int pci_reset_bridge(struct pci_dev *bridge, bool restore)
>  	if (!bus)
>  		return -ENOTTY;
>  
> +	/*
> +	 * 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-deadlock

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

[ ... ]

> @@ -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.
> +	 */
> +	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.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261126-fix_deadlock_1126-v2-1-c0ed50bc7b04@oss.qualcomm.com?part=1

      reply	other threads:[~2026-11-26  8:52 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26  8:26 [PATCH RFC v2] PCI: pciehp: Fix AB-BA deadlocks between reset_lock, pci_rescan_remove_lock and pci_slot_mutex Qiang Yu
2026-11-26  8:52 ` sashiko-bot [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-235061@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=qiang.yu@oss.qualcomm.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox