[PATCH v2 02/16] PCI: Set new BAR Size only after releasing the BARs for BAR resize
From: Ilpo Järvinen
Date: Fri Dec 04 2026 - 19:40:49 EST
pci_do_resource_release_and_resize() sets the new BAR Size before it
saves and releases the BARs that share the bridge window with the BAR
being resized. The BAR Size decides which bits of the Base Address
Register are read-only, so the address in the resized BAR may lose its
low bits right away.
Saving the BARs needs memory allocations, which can fail. If that
happens before the resized BAR has been saved, the BAR resize rollback
restores the old BAR Size but cannot restore the address of the resized
BAR, which leaves the BAR and its resource out of sync.
Nothing requires setting the new BAR Size that early. Set it only after
all the BARs have been saved and released, right before the resource is
resized.
Fixes: 337b1b566db0 ("PCI: Fix restoring BARs on BAR resize rollback path")
Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@xxxxxxxxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
---
drivers/pci/setup-bus.c | 15 ++++++++-------
1 file changed, 8 insertions(+), 7 deletions(-)
diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
index b69041fbbd39..db16c8c2913d 100644
--- a/drivers/pci/setup-bus.c
+++ b/drivers/pci/setup-bus.c
@@ -2394,10 +2394,6 @@ int pci_do_resource_release_and_resize(struct pci_dev *pdev, int resno, int size
if (old < 0)
return old;
- ret = pci_rebar_set_size(pdev, resno, size);
- if (ret)
- return ret;
-
down_read(&pci_bus_sem);
pci_dev_for_each_resource(pdev, r, i) {
@@ -2416,12 +2412,16 @@ int pci_do_resource_release_and_resize(struct pci_dev *pdev, int resno, int size
pci_release_resource(pdev, i);
}
+ ret = pci_rebar_set_size(pdev, resno, size);
+ if (ret)
+ goto restore;
+
pci_resize_resource_set_size(pdev, resno, size);
if (bridge) {
ret = pbus_reassign_bridge_resources(bus, res, &saved);
if (ret)
- goto restore;
+ goto restore_size;
} else {
/* No bridge window to adjust; let the core reassign the bus. */
pci_bus_assign_resources(bus);
@@ -2429,7 +2429,7 @@ int pci_do_resource_release_and_resize(struct pci_dev *pdev, int resno, int size
list_for_each_entry(dev_res, &saved, list) {
if (!resource_assigned(dev_res->res)) {
ret = -ENOSPC;
- goto restore;
+ goto restore_size;
}
}
}
@@ -2439,7 +2439,7 @@ int pci_do_resource_release_and_resize(struct pci_dev *pdev, int resno, int size
pci_dev_res_free_list(&saved);
return ret;
-restore:
+restore_size:
/*
* Revert to the old configuration.
*
@@ -2449,6 +2449,7 @@ int pci_do_resource_release_and_resize(struct pci_dev *pdev, int resno, int size
*/
pci_rebar_set_size(pdev, resno, old);
+restore:
/*
* Release all resources first, otherwise a resource not yet released
* could block restoring another resource into its original place.
--
2.47.3