diff mbox series

[RESEND] PCI: fix recursive device locking

Message ID 20240709195716.3354637-1-kbusch@meta.com (mailing list archive)
State Superseded
Delegated to: Bjorn Helgaas
Headers show
Series [RESEND] PCI: fix recursive device locking | expand

Commit Message

Keith Busch July 9, 2024, 7:57 p.m. UTC
From: Keith Busch <kbusch@kernel.org>

If one of the bus' devices has subordinates, the recursive call locks
itself, so no need to lock the device before the recursion or it will
surely deadlock.

Fixes: dbc5b5c0d268f87 ("PCI: Add missing bridge lock to pci_bus_lock()"
Reviewed-by: Dan Williams <dan.j.williams@intel.com>
Signed-off-by: Keith Busch <kbusch@kernel.org>
---
Sending to the correct mailing list this time, and fixed up two typos
in the commit message.

 drivers/pci/pci.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

Comments

Keith Busch July 11, 2024, 7:21 p.m. UTC | #1
On Tue, Jul 09, 2024 at 12:57:16PM -0700, Keith Busch wrote:
> @@ -5488,9 +5488,10 @@ static void pci_bus_lock(struct pci_bus *bus)
>  
>  	pci_dev_lock(bus->self);
>  	list_for_each_entry(dev, &bus->devices, bus_list) {
> -		pci_dev_lock(dev);
>  		if (dev->subordinate)
>  			pci_bus_lock(dev->subordinate);
> +		else
> +			pci_dev_lock(dev);
>  	}
>  }
>  
> @@ -5502,7 +5503,8 @@ static void pci_bus_unlock(struct pci_bus *bus)
>  	list_for_each_entry(dev, &bus->devices, bus_list) {
>  		if (dev->subordinate)
>  			pci_bus_unlock(dev->subordinate);
> -		pci_dev_unlock(dev);
> +		else
> +			pci_dev_unlock(dev);
>  	}
>  	pci_dev_unlock(bus->self);
>  }

I realized pci_slot_lock() has the same problem. I wasn't able to test
that path from not having a pcie topology with a subordinate on the slot
device, but it follows the same pattern. Same thing with
pci_bus_trylock() for that matter, so I will make a new version.
Dan Williams July 11, 2024, 8:08 p.m. UTC | #2
Keith Busch wrote:
> On Tue, Jul 09, 2024 at 12:57:16PM -0700, Keith Busch wrote:
> > @@ -5488,9 +5488,10 @@ static void pci_bus_lock(struct pci_bus *bus)
> >  
> >  	pci_dev_lock(bus->self);
> >  	list_for_each_entry(dev, &bus->devices, bus_list) {
> > -		pci_dev_lock(dev);
> >  		if (dev->subordinate)
> >  			pci_bus_lock(dev->subordinate);
> > +		else
> > +			pci_dev_lock(dev);
> >  	}
> >  }
> >  
> > @@ -5502,7 +5503,8 @@ static void pci_bus_unlock(struct pci_bus *bus)
> >  	list_for_each_entry(dev, &bus->devices, bus_list) {
> >  		if (dev->subordinate)
> >  			pci_bus_unlock(dev->subordinate);
> > -		pci_dev_unlock(dev);
> > +		else
> > +			pci_dev_unlock(dev);
> >  	}
> >  	pci_dev_unlock(bus->self);
> >  }
> 
> I realized pci_slot_lock() has the same problem. I wasn't able to test
> that path from not having a pcie topology with a subordinate on the slot
> device, but it follows the same pattern. Same thing with
> pci_bus_trylock() for that matter, so I will make a new version.

I can take another look at that one as well, but feel free to carry my
Reviewed-by tag on that one, and just trust that I'll scream if I notice
something late.
Dan Williams July 11, 2024, 8:10 p.m. UTC | #3
Dan Williams wrote:
> > I realized pci_slot_lock() has the same problem. I wasn't able to test
> > that path from not having a pcie topology with a subordinate on the slot
> > device, but it follows the same pattern. Same thing with
> > pci_bus_trylock() for that matter, so I will make a new version.
> 
> I can take another look at that one as well, but feel free to carry my
> Reviewed-by tag on that one, and just trust that I'll scream if I notice
> something late.

Disregard, I mistook this as comment about v2 rather than v1. v2 is good
to go.
diff mbox series

Patch

diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index df550953fa260..5ab13bf5a3caa 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -5488,9 +5488,10 @@  static void pci_bus_lock(struct pci_bus *bus)
 
 	pci_dev_lock(bus->self);
 	list_for_each_entry(dev, &bus->devices, bus_list) {
-		pci_dev_lock(dev);
 		if (dev->subordinate)
 			pci_bus_lock(dev->subordinate);
+		else
+			pci_dev_lock(dev);
 	}
 }
 
@@ -5502,7 +5503,8 @@  static void pci_bus_unlock(struct pci_bus *bus)
 	list_for_each_entry(dev, &bus->devices, bus_list) {
 		if (dev->subordinate)
 			pci_bus_unlock(dev->subordinate);
-		pci_dev_unlock(dev);
+		else
+			pci_dev_unlock(dev);
 	}
 	pci_dev_unlock(bus->self);
 }