[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20260116125742.1890-4-ilpo.jarvinen@linux.intel.com>
Date: Fri, 16 Jan 2026 14:57:42 +0200
From: Ilpo Järvinen <ilpo.jarvinen@...ux.intel.com>
To: "Jinhui Guo" <guojinhui.liam@...edance.com>,
Keith Busch <kbusch@...nel.org>,
"Anthony Pighin (Nokia)" <anthony.pighin@...ia.com>,
Alex Williamson <alex@...zbot.org>,
Jonathan Cameron <Jonathan.Cameron@...wei.com>,
Bjorn Helgaas <bhelgaas@...gle.com>,
linux-pci@...r.kernel.org,
linux-kernel@...r.kernel.org
Cc: Ilpo Järvinen <ilpo.jarvinen@...ux.intel.com>
Subject: [PATCH 3/3] PCI: Consolidate pci_bus/slot_lock/unlock/trylock()
pci_bus/slot_lock/unlock/trylock() largely duplicate the bus iteration
loop with variation only due to slot filter handling. The only
differences in the loops is where the struct bus is found (directly in
the argument vs in slot->bus) and whether slot filter is applied. Those
difference are simple to handle using function parameters.
Consolidate the bus iteration loop to one place by creating
__pci_bus_{lock,unlock,trylock}() and call them from the non-underscore
locking functions.
Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@...ux.intel.com>
---
drivers/pci/pci.c | 103 ++++++++++++++++++++++------------------------
1 file changed, 49 insertions(+), 54 deletions(-)
diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index e1333539c7b7..622920c1529f 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -5242,13 +5242,18 @@ 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)
+static void __pci_bus_lock(struct pci_bus *bus, struct pci_slot *slot)
{
struct pci_dev *dev;
- pci_dev_lock(bus->self);
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);
else
@@ -5257,28 +5262,28 @@ static void pci_bus_lock(struct pci_bus *bus)
}
/* Unlock devices from the bottom of the tree up */
-static void pci_bus_unlock(struct pci_bus *bus)
+static void __pci_bus_unlock(struct pci_bus *bus, struct pci_slot *slot)
{
struct pci_dev *dev;
list_for_each_entry(dev, &bus->devices, bus_list) {
+ if (slot && (!dev->slot || dev->slot != slot))
+ continue;
if (dev->subordinate)
pci_bus_unlock(dev->subordinate);
else
pci_dev_unlock(dev);
}
- pci_dev_unlock(bus->self);
}
/* Return 1 on successful lock, 0 on contention */
-static int pci_bus_trylock(struct pci_bus *bus)
+static int __pci_bus_trylock(struct pci_bus *bus, struct pci_slot *slot)
{
struct pci_dev *dev;
- if (!pci_dev_trylock(bus->self))
- return 0;
-
list_for_each_entry(dev, &bus->devices, bus_list) {
+ if (slot && (!dev->slot || dev->slot != slot))
+ continue;
if (dev->subordinate) {
if (!pci_bus_trylock(dev->subordinate))
goto unlock;
@@ -5288,12 +5293,44 @@ static int pci_bus_trylock(struct pci_bus *bus)
return 1;
unlock:
- list_for_each_entry_continue_reverse(dev, &bus->devices, bus_list) {
+ list_for_each_entry_continue_reverse(dev,
+ &slot->bus->devices, bus_list) {
+ if (slot && (!dev->slot || dev->slot != slot))
+ continue;
if (dev->subordinate)
pci_bus_unlock(dev->subordinate);
else
pci_dev_unlock(dev);
}
+ return 0;
+}
+
+/* Lock devices from the top of the tree down */
+static void pci_bus_lock(struct pci_bus *bus)
+{
+ pci_dev_lock(bus->self);
+ __pci_bus_lock(bus, NULL);
+}
+
+/* Unlock devices from the bottom of the tree up */
+static void pci_bus_unlock(struct pci_bus *bus)
+{
+ __pci_bus_unlock(bus, NULL);
+ pci_dev_unlock(bus->self);
+}
+
+/* Return 1 on successful lock, 0 on contention */
+static int pci_bus_trylock(struct pci_bus *bus)
+{
+ if (!pci_dev_trylock(bus->self))
+ return 0;
+
+ if (!__pci_bus_trylock(bus, NULL))
+ goto unlock;
+
+ return 1;
+
+unlock:
pci_dev_unlock(bus->self);
return 0;
}
@@ -5321,61 +5358,19 @@ static bool pci_slot_resettable(struct pci_slot *slot)
/* Lock devices from the top of the tree down */
static void pci_slot_lock(struct pci_slot *slot)
{
- struct pci_dev *dev;
-
- list_for_each_entry(dev, &slot->bus->devices, bus_list) {
- if (!dev->slot || dev->slot != slot)
- continue;
- if (dev->subordinate)
- pci_bus_lock(dev->subordinate);
- else
- pci_dev_lock(dev);
- }
+ __pci_bus_lock(slot->bus, slot);
}
/* Unlock devices from the bottom of the tree up */
static void pci_slot_unlock(struct pci_slot *slot)
{
- struct pci_dev *dev;
-
- list_for_each_entry(dev, &slot->bus->devices, bus_list) {
- if (!dev->slot || dev->slot != slot)
- continue;
- if (dev->subordinate)
- pci_bus_unlock(dev->subordinate);
- else
- pci_dev_unlock(dev);
- }
+ __pci_bus_unlock(slot->bus, slot);
}
/* Return 1 on successful lock, 0 on contention */
static int pci_slot_trylock(struct pci_slot *slot)
{
- struct pci_dev *dev;
-
- list_for_each_entry(dev, &slot->bus->devices, bus_list) {
- if (!dev->slot || dev->slot != slot)
- continue;
- if (dev->subordinate) {
- if (!pci_bus_trylock(dev->subordinate)) {
- goto unlock;
- }
- } else if (!pci_dev_trylock(dev))
- goto unlock;
- }
- return 1;
-
-unlock:
- list_for_each_entry_continue_reverse(dev,
- &slot->bus->devices, bus_list) {
- if (!dev->slot || dev->slot != slot)
- continue;
- if (dev->subordinate)
- pci_bus_unlock(dev->subordinate);
- else
- pci_dev_unlock(dev);
- }
- return 0;
+ return __pci_bus_trylock(slot->bus, slot);
}
/*
--
2.39.5
Powered by blists - more mailing lists