Linux PCI subsystem development
 help / color / mirror / Atom feed
* [PATCH] PCI: Don't block config access while waiting for device locks in bus reset
@ 2026-11-20  8:00 Ionut Nechita (Wind River)
  2026-11-20  8:15 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Ionut Nechita (Wind River) @ 2026-11-20  8:00 UTC (permalink / raw)
  To: Bjorn Helgaas
  Cc: linux-pci, linux-kernel, Dan Williams, Keith Busch, Dave Jiang,
	Alex Williamson, Ionut Nechita, stable

From: Ionut Nechita <ionut.nechita@windriver.com>

__pci_bus_lock(), used by bus and slot resets, locks the bridge and every
device below it with pci_dev_lock(), one device after the other.  Each
pci_dev_lock() takes the device lock and then blocks config access to the
device.  If another task holds the device lock of a device further down
the list, the reset sleeps with config access already blocked to the
bridge and to every device locked before it, and every user config read
of those devices sleeps in pci_wait_cfg() for as long.

This is the same problem as the one fixed for pci_reset_function(): a
driver unbind of a function held open by a vfio user waits in
vfio_unregister_group_dev() with the device lock held until the user
releases the device.  A bus or slot reset that reaches that device during
error recovery (pci_bus_error_reset()) keeps config access to the bridge
blocked meanwhile, and lspci hangs in D state.

Take all device locks first, from the top of the tree down as before, and
block config access to the bridge and the devices only once all of them
are held.  The order in which the device locks are taken is unchanged.

The trylock variant does not sleep on a device lock, and the unlock path
releases each device's config access and device lock together, as
before, so neither is changed.

Fixes: a4e772898f8b ("PCI: Add missing bridge lock to pci_bus_lock()")
Cc: stable@vger.kernel.org
Signed-off-by: Ionut Nechita <ionut.nechita@windriver.com>
---
 drivers/pci/pci.c | 47 +++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 41 insertions(+), 6 deletions(-)

diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index d35741eb3..ffeeef045 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -5432,28 +5432,63 @@ static bool pci_bus_resettable(struct pci_bus *bus)
 	return true;
 }
 
-static void pci_bus_lock(struct pci_bus *bus);
 static void pci_bus_unlock(struct pci_bus *bus);
 static int pci_bus_trylock(struct pci_bus *bus);
 
-/* Lock devices from the top of the tree down */
-static void __pci_bus_lock(struct pci_bus *bus, struct pci_slot *slot)
+/* Take the device locks from the top of the tree down */
+static void __pci_bus_device_lock(struct pci_bus *bus, struct pci_slot *slot)
+{
+	struct pci_dev *dev, *bridge = bus->self;
+
+	if (bridge)
+		device_lock(&bridge->dev);
+
+	list_for_each_entry(dev, &bus->devices, bus_list) {
+		if (slot && (!dev->slot || dev->slot != slot))
+			continue;
+		if (dev->subordinate)
+			__pci_bus_device_lock(dev->subordinate, NULL);
+		else
+			device_lock(&dev->dev);
+	}
+}
+
+/* Block config access from the top of the tree down */
+static void __pci_bus_cfg_access_lock(struct pci_bus *bus,
+				      struct pci_slot *slot)
 {
 	struct pci_dev *dev, *bridge = bus->self;
 
 	if (bridge)
-		pci_dev_lock(bridge);
+		pci_cfg_access_lock(bridge);
 
 	list_for_each_entry(dev, &bus->devices, bus_list) {
 		if (slot && (!dev->slot || dev->slot != slot))
 			continue;
 		if (dev->subordinate)
-			pci_bus_lock(dev->subordinate);
+			__pci_bus_cfg_access_lock(dev->subordinate, NULL);
 		else
-			pci_dev_lock(dev);
+			pci_cfg_access_lock(dev);
 	}
 }
 
+/*
+ * Lock devices from the top of the tree down.
+ *
+ * Take all device locks before blocking config access to any device.
+ * device_lock() can sleep for as long as another task holds it, e.g. a
+ * driver unbind waiting for a vfio user to release the device.  If config
+ * access to the bridge and the devices locked so far were already blocked
+ * at that point, every config read of them (lspci, sysfs "config") would
+ * sleep in pci_wait_cfg() for the same time.  The order in which the
+ * device locks are taken is unchanged.
+ */
+static void __pci_bus_lock(struct pci_bus *bus, struct pci_slot *slot)
+{
+	__pci_bus_device_lock(bus, slot);
+	__pci_bus_cfg_access_lock(bus, slot);
+}
+
 /* Unlock devices from the bottom of the tree up */
 static void __pci_bus_unlock(struct pci_bus *bus, struct pci_slot *slot)
 {
-- 
2.56.0


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

* Re: [PATCH] PCI: Don't block config access while waiting for device locks in bus reset
  2026-11-20  8:00 [PATCH] PCI: Don't block config access while waiting for device locks in bus reset Ionut Nechita (Wind River)
@ 2026-11-20  8:15 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-20  8:15 UTC (permalink / raw)
  To: Ionut Nechita (Wind River); +Cc: linux-pci

> From: Ionut Nechita <ionut.nechita@windriver.com>
> 
> __pci_bus_lock(), used by bus and slot resets, locks the bridge and every
> device below it with pci_dev_lock(), one device after the other.  Each
> pci_dev_lock() takes the device lock and then blocks config access to the
> device.  If another task holds the device lock of a device further down
> the list, the reset sleeps with config access already blocked to the
> bridge and to every device locked before it, and every user config read
> of those devices sleeps in pci_wait_cfg() for as long.
> 
> This is the same problem as the one fixed for pci_reset_function(): a
> driver unbind of a function held open by a vfio user waits in
> vfio_unregister_group_dev() with the device lock held until the user
> releases the device.  A bus or slot reset that reaches that device during
> error recovery (pci_bus_error_reset()) keeps config access to the bridge
> blocked meanwhile, and lspci hangs in D state.
> 
> Take all device locks first, from the top of the tree down as before, and
> block config access to the bridge and the devices only once all of them
> are held.  The order in which the device locks are taken is unchanged.
> 
> The trylock variant does not sleep on a device lock, and the unlock path
> releases each device's config access and device lock together, as
> before, so neither is changed.
> 
> Fixes: a4e772898f8b ("PCI: Add missing bridge lock to pci_bus_lock()")
> Cc: stable@vger.kernel.org
> Signed-off-by: Ionut Nechita <ionut.nechita@windriver.com>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261120080031.56565-1-ionut.nechita@windriver.com?part=1


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

end of thread, other threads:[~2026-11-20  8:15 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20  8:00 [PATCH] PCI: Don't block config access while waiting for device locks in bus reset Ionut Nechita (Wind River)
2026-11-20  8:15 ` sashiko-bot

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