Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH] PCI: Fix root port reset lock ordering
@ 2026-11-30 15:32 Paul Cercueil
       [not found] ` <sashiko-outbox-241514@kernel.org>
  2026-11-30 17:16 ` Lukas Wunner
  0 siblings, 2 replies; 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

* Re: [PATCH] PCI: Fix root port reset lock ordering
       [not found] ` <sashiko-outbox-241514@kernel.org>
@ 2026-11-30 16:10   ` Paul Cercueil
  0 siblings, 0 replies; 3+ messages in thread
From: Paul Cercueil @ 2026-11-30 16:10 UTC (permalink / raw)
  To: sashiko-reviews
  Cc: linux-pci, Bjorn Helgaas, Manivannan Sadhasivam, Thomas Petazzoni,
	stable

Hi Sashiko,

Le lundi 30 novembre 2026 à 15:45 +0000, sashiko-bot@kernel.org a écrit :
> 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 <paul.cercueil@bootlin.com>
> 
> 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().
> 
> > diff --git a/drivers/pci/hotplug/pciehp_pci.c
> > b/drivers/pci/hotplug/pciehp_pci.c
> > index 65e50bee1a8c0..20549c83870e2 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);
> > +}
> 
> [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.

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.

> 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

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

* Re: [PATCH] PCI: Fix root port reset lock ordering
  2026-11-30 15:32 [PATCH] PCI: Fix root port reset lock ordering Paul Cercueil
       [not found] ` <sashiko-outbox-241514@kernel.org>
@ 2026-11-30 17:16 ` Lukas Wunner
  1 sibling, 0 replies; 3+ messages in thread
From: Lukas Wunner @ 2026-11-30 17:16 UTC (permalink / raw)
  To: Paul Cercueil
  Cc: Bjorn Helgaas, Manivannan Sadhasivam, Thomas Petazzoni, linux-pci,
	stable, Qiang Yu

[cc += Qiang]

On Mon, Nov 30, 2026 at 04:32:18PM +0100, Paul Cercueil wrote:
> 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.

Duplicate of:

https://lore.kernel.org/r/20261126-fix_deadlock_1126-v2-1-c0ed50bc7b04@oss.qualcomm.com/

Please see the discussion there.

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

end of thread, other threads:[~2026-11-30 17:16 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
     [not found] ` <sashiko-outbox-241514@kernel.org>
2026-11-30 16:10   ` Paul Cercueil
2026-11-30 17:16 ` Lukas Wunner

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