[PATCH 27/62] pci: Fix locking in pci_do_resource_release_and_resize() error paths

Bart Van Assche posted 62 patches 1 month ago
Only 30 patches received!
[PATCH 27/62] pci: Fix locking in pci_do_resource_release_and_resize() error paths
Posted by Bart Van Assche 1 month ago
From: Bart Van Assche <bvanassche@acm.org>

Make sure that down(&pci_bus_sem) has been called before
up(&pci_bus_sem) is called. This has been detected by the Clang
thread-safety analyzer. Compile-tested only.

Cc: Bjorn Helgaas <bhelgaas@google.com>
Cc: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Cc: linux-pci@vger.kernel.org
Fixes: 337b1b566db0 ("PCI: Fix restoring BARs on BAR resize rollback path")
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
 drivers/pci/setup-bus.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
index 61f769aaa2f6..3bb8cd9a581e 100644
--- a/drivers/pci/setup-bus.c
+++ b/drivers/pci/setup-bus.c
@@ -2346,6 +2346,7 @@ int pci_do_resource_release_and_resize(struct pci_dev *pdev, int resno, int size
 	if (ret)
 		return ret;
 
+	down_read(&pci_bus_sem);
 	pci_dev_for_each_resource(pdev, r, i) {
 		if (i >= PCI_BRIDGE_RESOURCES)
 			break;
@@ -2367,7 +2368,6 @@ int pci_do_resource_release_and_resize(struct pci_dev *pdev, int resno, int size
 	if (!bus->self)
 		goto out;
 
-	down_read(&pci_bus_sem);
 	ret = pbus_reassign_bridge_resources(bus, res, &saved);
 	if (ret)
 		goto restore;
Re: [PATCH 27/62] pci: Fix locking in pci_do_resource_release_and_resize() error paths
Posted by Ilpo Järvinen 1 month ago
On Mon, 23 Feb 2026, Bart Van Assche wrote:

> From: Bart Van Assche <bvanassche@acm.org>
> 
> Make sure that down(&pci_bus_sem) has been called before
> up(&pci_bus_sem) is called.

Before explaining the solution, it would be useful to mention how this 
problem can manifest itself (taking one of the early gotos).

> This has been detected by the Clang
> thread-safety analyzer. Compile-tested only.
> 
> Cc: Bjorn Helgaas <bhelgaas@google.com>
> Cc: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
> Cc: linux-pci@vger.kernel.org

These (obvious) Ccs are not needed in the commit message, if your workflow 
requires having them, please place them below the --- line so that the 
maintainer's tools remove them while applying the patch.

> Fixes: 337b1b566db0 ("PCI: Fix restoring BARs on BAR resize rollback path")
> Signed-off-by: Bart Van Assche <bvanassche@acm.org>
> ---
>  drivers/pci/setup-bus.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
> index 61f769aaa2f6..3bb8cd9a581e 100644
> --- a/drivers/pci/setup-bus.c
> +++ b/drivers/pci/setup-bus.c
> @@ -2346,6 +2346,7 @@ int pci_do_resource_release_and_resize(struct pci_dev *pdev, int resno, int size
>  	if (ret)
>  		return ret;
>  
> +	down_read(&pci_bus_sem);
>  	pci_dev_for_each_resource(pdev, r, i) {
>  		if (i >= PCI_BRIDGE_RESOURCES)
>  			break;
> @@ -2367,7 +2368,6 @@ int pci_do_resource_release_and_resize(struct pci_dev *pdev, int resno, int size
>  	if (!bus->self)
>  		goto out;
>  
> -	down_read(&pci_bus_sem);
>  	ret = pbus_reassign_bridge_resources(bus, res, &saved);
>  	if (ret)
>  		goto restore;

-- 
 i.