Skip to contentSkip to conversation

PCI: Add lock and reference count in pci_get_domain_bus_and_slot()

PATCH In review Kaushlendra Kumar 2 participants 2 messages Oct 1, 2025 PCI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PCI: Add lock and reference count in pci_get_domain_bus_and_slot()

Add proper locking and reference counting to pci_get_domain_bus_and_slot during PCI device enumeration. The function now holds pci_bus_sem during device iteration and properly increments the device reference count before returning.

---
 drivers/pci/search.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/pci/search.c +7 -2
index 53840634fbfc..dc49d3db69a4 100644
@@ -230,12 +230,17 @@ struct pci_dev *pci_get_domain_bus_and_slot(int domain, unsigned int bus,
 {
 	struct pci_dev *dev = NULL;
 
+	down_read(&pci_bus_sem);
 	for_each_pci_dev(dev) {
 		if (pci_domain_nr(dev->bus) == domain &&
 		    (dev->bus->number == bus && dev->devfn == devfn))
-			return dev;
+			goto out;
 	}
-	return NULL;
+	dev = NULL;
+ out:
+	pci_dev_get(dev);
+	up_read(&pci_bus_sem);
+	return dev;
 }
 EXPORT_SYMBOL(pci_get_domain_bus_and_slot);
 
Signature · 1 line
2.34.1
#1 lore raw
Quoted 1 line · Kaushlendra Kumar

Add proper locking and reference counting to pci_get_domain_bus_and_slot

Add () to any function names.

Quoted 3 lines

during PCI device enumeration. The function now holds pci_bus_sem during device iteration and properly increments the device reference count before returning.

These two sentences duplicate each other. Could you state things just once with the more precise explanation that is in the 2nd sentence.

Also, I'd prefer to see the problem described in a paragraph preceeding this solution paragraph/sentence.

Fixes tag?

Quoted 10 lines
---
 drivers/pci/search.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/pci/search.c +0 -0
index 53840634fbfc..dc49d3db69a4 100644

drivers/pci/search.c struct pci_dev *pci_get_domain_bus_and_slot(int domain, unsigned int bus, · line 233

Quoted 4 lines
drivers/pci/search.c +1 -0
@@ -230,12 +230,17 @@ struct pci_dev *pci_get_domain_bus_and_slot(int domain, unsigned int bus,
 {
 	struct pci_dev *dev = NULL;
 
+	down_read(&pci_bus_sem);

How about using

guard(rwsem_read)(&pci_bus_sem);

?

I think that would look a lot cleaner overall.

drivers/pci/search.c struct pci_dev *pci_get_domain_bus_and_slot(int domain, unsigned int bus, · line 241

Quoted 10 lines
drivers/pci/search.c +4 -2
 	for_each_pci_dev(dev) {
 		if (pci_domain_nr(dev->bus) == domain &&
 		    (dev->bus->number == bus && dev->devfn == devfn))
-			return dev;
+			goto out;
 	}
-	return NULL;
+	dev = NULL;
+ out:
+	pci_dev_get(dev);

So now you're incrementing the reference count but there's no code in this patch to counterdecrement it? Are all callers already decrementing it (which leads to underflows w/o this patch)? This needs to be explained in the changelog text.

Quoted 4 lines
drivers/pci/search.c +2 -0
+	up_read(&pci_bus_sem);
+	return dev;
 }
 EXPORT_SYMBOL(pci_get_domain_bus_and_slot);
Signature · 1 line
 i.