* [PATCH 01/15] PCI: Release all resources before restoring them in BAR resize rollback
[not found] <20261201155844.2278-1-ilpo.jarvinen@linux.intel.com>
@ 2026-12-01 15:58 ` Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 02/15] PCI: Set new BAR Size only after releasing the BARs for BAR resize Ilpo Järvinen
` (6 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Ilpo Järvinen @ 2026-12-01 15:58 UTC (permalink / raw)
To: Bjorn Helgaas, Lorenzo Pieralisi, Rob Herring,
Krzysztof Wilczyński, Alex Deucher, Christian König,
Jani Nikula, Joonas Lahtinen, Rodrigo Vivi, Tvrtko Ursulin,
Matthew Brost, Thomas Hellström, Michał Winiarski,
Ville Syrjälä, David Airlie, Simona Vetter, dri-devel,
amd-gfx, intel-gfx, intel-xe, Ilpo Järvinen, linux-pci,
linux-kernel
Cc: stable
When BAR resize fails, pci_do_resource_release_and_resize() rolls back
by releasing, restoring and claiming resource one at a time. A resource
that has not been released yet can block restoring another resource into
its original place, which leaves some resources unassigned.
Release all saved resources first and only then restore and claim them.
Fixes: 337b1b566db0 ("PCI: Fix restoring BARs on BAR resize rollback path")
Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Cc: stable@vger.kernel.org
---
drivers/pci/setup-bus.c | 21 +++++++++++++++++----
1 file changed, 17 insertions(+), 4 deletions(-)
diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
index ed16ef7c26fa..b69041fbbd39 100644
--- a/drivers/pci/setup-bus.c
+++ b/drivers/pci/setup-bus.c
@@ -2449,16 +2449,29 @@ int pci_do_resource_release_and_resize(struct pci_dev *pdev, int resno, int size
*/
pci_rebar_set_size(pdev, resno, old);
+ /*
+ * Release all resources first, otherwise a resource not yet released
+ * could block restoring another resource into its original place.
+ */
list_for_each_entry(dev_res, &saved, list) {
struct resource *res = dev_res->res;
struct pci_dev *dev = dev_res->dev;
i = pci_resource_num(dev, res);
- if (resource_assigned(res)) {
- release_child_resources(res);
- pci_release_resource(dev, i);
- }
+ if (!resource_assigned(res))
+ continue;
+
+ release_child_resources(res);
+ pci_release_resource(dev, i);
+ }
+
+ /* Restore parents before their children */
+ list_for_each_entry(dev_res, &saved, list) {
+ struct resource *res = dev_res->res;
+ struct pci_dev *dev = dev_res->dev;
+
+ i = pci_resource_num(dev, res);
pci_dev_res_restore(dev_res);
--
2.47.3
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 02/15] PCI: Set new BAR Size only after releasing the BARs for BAR resize
[not found] <20261201155844.2278-1-ilpo.jarvinen@linux.intel.com>
2026-12-01 15:58 ` [PATCH 01/15] PCI: Release all resources before restoring them in BAR resize rollback Ilpo Järvinen
@ 2026-12-01 15:58 ` Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 03/15] PCI: Abort BAR resize only if saving the resized BAR fails Ilpo Järvinen
` (5 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Ilpo Järvinen @ 2026-12-01 15:58 UTC (permalink / raw)
To: Bjorn Helgaas, Lorenzo Pieralisi, Rob Herring,
Krzysztof Wilczyński, Alex Deucher, Christian König,
Jani Nikula, Joonas Lahtinen, Rodrigo Vivi, Tvrtko Ursulin,
Matthew Brost, Thomas Hellström, Michał Winiarski,
Ville Syrjälä, David Airlie, Simona Vetter, dri-devel,
amd-gfx, intel-gfx, intel-xe, Ilpo Järvinen, linux-pci,
linux-kernel
Cc: stable
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@linux.intel.com>
Cc: stable@vger.kernel.org
---
drivers/pci/setup-bus.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
index b69041fbbd39..608ca7afbfb8 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,6 +2412,10 @@ 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) {
--
2.47.3
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 03/15] PCI: Abort BAR resize only if saving the resized BAR fails
[not found] <20261201155844.2278-1-ilpo.jarvinen@linux.intel.com>
2026-12-01 15:58 ` [PATCH 01/15] PCI: Release all resources before restoring them in BAR resize rollback Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 02/15] PCI: Set new BAR Size only after releasing the BARs for BAR resize Ilpo Järvinen
@ 2026-12-01 15:58 ` Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 04/15] PCI: Consistently use Header Type to detect bridge headers Ilpo Järvinen
` (4 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Ilpo Järvinen @ 2026-12-01 15:58 UTC (permalink / raw)
To: Bjorn Helgaas, Lorenzo Pieralisi, Rob Herring,
Krzysztof Wilczyński, Alex Deucher, Christian König,
Jani Nikula, Joonas Lahtinen, Rodrigo Vivi, Tvrtko Ursulin,
Matthew Brost, Thomas Hellström, Michał Winiarski,
Ville Syrjälä, David Airlie, Simona Vetter, dri-devel,
amd-gfx, intel-gfx, intel-xe, linux-pci, linux-kernel
Cc: Ilpo Järvinen
pci_do_resource_release_and_resize() saves and releases the BARs of the
device that share the bridge window with the BAR being resized, and
rolls back if saving any of them fails. Only the resized BAR has to be
saved and released for the resize to be possible. Another BAR that
cannot be saved remains assigned, which may prevent the bridge window
from being adjusted, but resize may succeed in some cases despite it.
Roll back only if saving the resized BAR fails. Leave the other BARs
that cannot be saved in place and continue.
Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
---
drivers/pci/setup-bus.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
index 608ca7afbfb8..ea0039fb6ad0 100644
--- a/drivers/pci/setup-bus.c
+++ b/drivers/pci/setup-bus.c
@@ -2407,8 +2407,11 @@ int pci_do_resource_release_and_resize(struct pci_dev *pdev, int resno, int size
continue;
ret = pci_dev_res_add_to_list(&saved, pdev, r, 0, 0);
- if (ret)
- goto restore;
+ if (ret) {
+ if (i == resno)
+ goto restore;
+ continue;
+ }
pci_release_resource(pdev, i);
}
--
2.47.3
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 04/15] PCI: Consistently use Header Type to detect bridge headers
[not found] <20261201155844.2278-1-ilpo.jarvinen@linux.intel.com>
` (2 preceding siblings ...)
2026-12-01 15:58 ` [PATCH 03/15] PCI: Abort BAR resize only if saving the resized BAR fails Ilpo Järvinen
@ 2026-12-01 15:58 ` Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 09/15] PCI: Do not release resources with children in pci_release_resource() Ilpo Järvinen
` (3 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Ilpo Järvinen @ 2026-12-01 15:58 UTC (permalink / raw)
To: Bjorn Helgaas, Lorenzo Pieralisi, Rob Herring,
Krzysztof Wilczyński, Alex Deucher, Christian König,
Jani Nikula, Joonas Lahtinen, Rodrigo Vivi, Tvrtko Ursulin,
Matthew Brost, Thomas Hellström, Michał Winiarski,
Ville Syrjälä, David Airlie, Simona Vetter, dri-devel,
amd-gfx, intel-gfx, intel-xe, Honghui Zhang, linux-pci,
linux-kernel
Cc: Ilpo Järvinen, stable
The Header Type decides the layout of the config space. A Type 0 header
is used by endpoints and a Type 1 header by bridges, which have the
bridge window registers. Earlier, Type 2 was also used for CardBus
bridges. The Class Code only describes the function of the device.
Commit b2fb5cc57469 ("PCI: Rely on config space header type, not class
code") converted bridge window sizing and assignment to use the Header
Type, but __pci_bridge_assign_resources(), pci_claim_bridge_resource()
and pci_bus_release_bridge_resources() still use the Class Code to
detect bridges. Using an inconsistent classifier creates corner cases
in programming, claiming, and releasing the bridge windows of Type 1
bridges whose Class Code is not PCI_CLASS_BRIDGE_PCI.
Consistently use the Header Type to detect bridges also in those
functions. If filtering out Type 1 devices with an unexpected Class Code
is desirable, it should be done in pci_setup_device() which already
does similar filtering for the other Header Types.
Fixes: b2fb5cc57469 ("PCI: Rely on config space header type, not class code")
Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Cc: stable@vger.kernel.org
---
This patch supercedes the change in the cardbus series:
https://lore.kernel.org/linux-pci/20261109210714.3862-3-ilpo.jarvinen@linux.intel.com/
drivers/pci/setup-bus.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
index ea0039fb6ad0..c1033191571c 100644
--- a/drivers/pci/setup-bus.c
+++ b/drivers/pci/setup-bus.c
@@ -948,7 +948,7 @@ int pci_claim_bridge_resource(struct pci_dev *bridge, int i)
if (pci_claim_resource(bridge, i) == 0)
return 0; /* Claimed the window */
- if ((bridge->class >> 8) != PCI_CLASS_BRIDGE_PCI)
+ if (bridge->hdr_type != PCI_HEADER_TYPE_BRIDGE)
return 0;
if (i > PCI_BRIDGE_PREF_MEM_WINDOW)
@@ -1636,12 +1636,12 @@ static void __pci_bridge_assign_resources(struct pci_dev *bridge,
__pci_bus_assign_resources(b, add_list, fail_head);
- switch (bridge->class >> 8) {
- case PCI_CLASS_BRIDGE_PCI:
+ switch (bridge->hdr_type) {
+ case PCI_HEADER_TYPE_BRIDGE:
pci_setup_bridge(b);
break;
- case PCI_CLASS_BRIDGE_CARDBUS:
+ case PCI_HEADER_TYPE_CARDBUS:
pci_setup_cardbus_bridge(b);
break;
@@ -1701,7 +1701,7 @@ static void pci_bus_release_bridge_resources(struct pci_bus *bus,
is_leaf_bridge = false;
- if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
+ if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE)
continue;
if (rel_type != whole_subtree)
@@ -1718,7 +1718,7 @@ static void pci_bus_release_bridge_resources(struct pci_bus *bus,
if (pci_is_root_bus(bus))
return;
- if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
+ if (bus->self->hdr_type != PCI_HEADER_TYPE_BRIDGE)
return;
if ((rel_type == whole_subtree) || is_leaf_bridge)
--
2.47.3
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 09/15] PCI: Do not release resources with children in pci_release_resource()
[not found] <20261201155844.2278-1-ilpo.jarvinen@linux.intel.com>
` (3 preceding siblings ...)
2026-12-01 15:58 ` [PATCH 04/15] PCI: Consistently use Header Type to detect bridge headers Ilpo Järvinen
@ 2026-12-01 15:58 ` Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 13/15] PCI: Split pci_resource_post_release() out of pci_release_resource() Ilpo Järvinen
` (2 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: Ilpo Järvinen @ 2026-12-01 15:58 UTC (permalink / raw)
To: Bjorn Helgaas, Lorenzo Pieralisi, Rob Herring,
Krzysztof Wilczyński, Alex Deucher, Christian König,
Jani Nikula, Joonas Lahtinen, Rodrigo Vivi, Tvrtko Ursulin,
Matthew Brost, Thomas Hellström, Michał Winiarski,
Ville Syrjälä, David Airlie, Simona Vetter, dri-devel,
amd-gfx, intel-gfx, intel-xe, linux-pci, linux-kernel
Cc: Ilpo Järvinen
pci_release_resource() releases the resource using release_resource()
which detaches the resource from the resource tree together with its
descendants. The descendants remain attached to the released resource
and keep their addresses. Once the resource is assigned elsewhere, the
descendants may end up outside of their parent, corrupting the resource
tree.
None of the callers wants that. pbus_reassign_bridge_resources() only
releases bridge windows that have no children. The BAR resize rollback
releases the children before releasing the resource itself. The BARs
released before BAR resize, however, are released even if they contain
regions requested by a driver, and the resize may then move such a BAR
from underneath those regions.
Use release_leaf_resource() in pci_release_resource() to return -EBUSY
if the resource has children. Remove a BAR that cannot be released from
the saved resources so that the BAR resize rollback leaves it alone, and
fail BAR resize if it is the resized BAR.
pbus_reassign_bridge_resources() checks for children without holding
resource_lock, so a child may still appear before the release. Remove
such a bridge window from the saved resources too so that the rollback
does not release the resources underneath it.
As the release can now fail, log both outcomes.
Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
---
drivers/pci/setup-bus.c | 10 ++++++++--
drivers/pci/setup-res.c | 12 ++++++++----
2 files changed, 16 insertions(+), 6 deletions(-)
diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
index c1033191571c..e5bbde75337d 100644
--- a/drivers/pci/setup-bus.c
+++ b/drivers/pci/setup-bus.c
@@ -2330,7 +2330,8 @@ static int pbus_reassign_bridge_resources(struct pci_bus *bus, struct resource *
if (ret)
return ret;
- pci_release_resource(bridge, i);
+ if (pci_release_resource(bridge, i))
+ pci_dev_res_remove_from_list(saved, res);
} else {
const char *res_name = pci_resource_name(bridge, i);
@@ -2412,7 +2413,12 @@ int pci_do_resource_release_and_resize(struct pci_dev *pdev, int resno, int size
goto restore;
continue;
}
- pci_release_resource(pdev, i);
+ ret = pci_release_resource(pdev, i);
+ if (ret) {
+ pci_dev_res_remove_from_list(&saved, r);
+ if (i == resno)
+ goto restore;
+ }
}
ret = pci_rebar_set_size(pdev, resno, size);
diff --git a/drivers/pci/setup-res.c b/drivers/pci/setup-res.c
index 376f09630a4a..1317d645a178 100644
--- a/drivers/pci/setup-res.c
+++ b/drivers/pci/setup-res.c
@@ -472,11 +472,15 @@ int pci_release_resource(struct pci_dev *dev, int resno)
if (!res->parent)
return 0;
- pci_info(dev, "%s %pR: releasing\n", res_name, res);
-
- ret = release_resource(res);
- if (ret)
+ ret = release_leaf_resource(res);
+ if (ret) {
+ pci_info(dev, "%s %pR: can't release; contains resources in use\n",
+ res_name, res);
return ret;
+ }
+
+ pci_info(dev, "%s %pR: released\n", res_name, res);
+
res->end = resource_size(res) - 1;
res->start = 0;
res->flags |= IORESOURCE_UNSET;
--
2.47.3
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 13/15] PCI: Split pci_resource_post_release() out of pci_release_resource()
[not found] <20261201155844.2278-1-ilpo.jarvinen@linux.intel.com>
` (4 preceding siblings ...)
2026-12-01 15:58 ` [PATCH 09/15] PCI: Do not release resources with children in pci_release_resource() Ilpo Järvinen
@ 2026-12-01 15:58 ` Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 14/15] PCI: Add iterators for devices and resources in bus windows Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 15/15] PCI: Release only PCI resources when releasing a resource tree Ilpo Järvinen
7 siblings, 0 replies; 9+ messages in thread
From: Ilpo Järvinen @ 2026-12-01 15:58 UTC (permalink / raw)
To: Bjorn Helgaas, Lorenzo Pieralisi, Rob Herring,
Krzysztof Wilczyński, Alex Deucher, Christian König,
Jani Nikula, Joonas Lahtinen, Rodrigo Vivi, Tvrtko Ursulin,
Matthew Brost, Thomas Hellström, Michał Winiarski,
Ville Syrjälä, David Airlie, Simona Vetter, dri-devel,
amd-gfx, intel-gfx, intel-xe, linux-pci, linux-kernel
Cc: Ilpo Järvinen
pci_release_resource() updates the resource after removing it from the
resource tree. Releasing PCI resources in other ways needs the same
update.
Split the update into pci_resource_post_release().
No functional changes.
Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
---
drivers/pci/setup-res.c | 19 ++++++++++++++-----
1 file changed, 14 insertions(+), 5 deletions(-)
diff --git a/drivers/pci/setup-res.c b/drivers/pci/setup-res.c
index 1317d645a178..a56e0cc3db58 100644
--- a/drivers/pci/setup-res.c
+++ b/drivers/pci/setup-res.c
@@ -463,6 +463,19 @@ int pci_reassign_resource(struct pci_dev *dev, int resno,
return 0;
}
+/* Update @resno of @dev after it has been removed from the resource tree */
+static void pci_resource_post_release(struct pci_dev *dev, int resno)
+{
+ struct resource *res = pci_resource_n(dev, resno);
+ const char *res_name = pci_resource_name(dev, resno);
+
+ pci_info(dev, "%s %pR: released\n", res_name, res);
+
+ res->end = resource_size(res) - 1;
+ res->start = 0;
+ res->flags |= IORESOURCE_UNSET;
+}
+
int pci_release_resource(struct pci_dev *dev, int resno)
{
struct resource *res = pci_resource_n(dev, resno);
@@ -479,11 +492,7 @@ int pci_release_resource(struct pci_dev *dev, int resno)
return ret;
}
- pci_info(dev, "%s %pR: released\n", res_name, res);
-
- res->end = resource_size(res) - 1;
- res->start = 0;
- res->flags |= IORESOURCE_UNSET;
+ pci_resource_post_release(dev, resno);
return 0;
}
--
2.47.3
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 14/15] PCI: Add iterators for devices and resources in bus windows
[not found] <20261201155844.2278-1-ilpo.jarvinen@linux.intel.com>
` (5 preceding siblings ...)
2026-12-01 15:58 ` [PATCH 13/15] PCI: Split pci_resource_post_release() out of pci_release_resource() Ilpo Järvinen
@ 2026-12-01 15:58 ` Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 15/15] PCI: Release only PCI resources when releasing a resource tree Ilpo Järvinen
7 siblings, 0 replies; 9+ messages in thread
From: Ilpo Järvinen @ 2026-12-01 15:58 UTC (permalink / raw)
To: Bjorn Helgaas, Lorenzo Pieralisi, Rob Herring,
Krzysztof Wilczyński, Alex Deucher, Christian König,
Jani Nikula, Joonas Lahtinen, Rodrigo Vivi, Tvrtko Ursulin,
Matthew Brost, Thomas Hellström, Michał Winiarski,
Ville Syrjälä, David Airlie, Simona Vetter, dri-devel,
amd-gfx, intel-gfx, intel-xe, linux-pci, linux-kernel
Cc: Ilpo Järvinen
A transparent (subtractive decode) bridge also forwards addresses that
fall outside of its own windows, so the resources of the devices behind
it may reside directly in the windows of the bus the bridge is on. Code
that looks for the resources within the windows of a bus has to walk the
devices behind transparent bridges too.
Add pci_bus_for_each_window_dev() that walks the devices on a bus and on
the buses below its transparent bridges, and
pci_bus_for_each_window_resource() that walks the resources of those
devices that reside directly in a given window of the bus.
Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
---
drivers/pci/bus.c | 33 +++++++++++++++++++++++++++++++++
drivers/pci/pci.h | 22 ++++++++++++++++++++++
2 files changed, 55 insertions(+)
diff --git a/drivers/pci/bus.c b/drivers/pci/bus.c
index 655ed53436d3..f70b4cfc07bf 100644
--- a/drivers/pci/bus.c
+++ b/drivers/pci/bus.c
@@ -490,6 +490,39 @@ void pci_walk_bus_locked(struct pci_bus *top, int (*cb)(struct pci_dev *, void *
__pci_walk_bus(top, cb, userdata);
}
+/* Return the device after @dev for pci_bus_for_each_window_dev() */
+struct pci_dev *pci_bus_next_window_dev(struct pci_bus *bus,
+ struct pci_dev *dev)
+{
+ struct pci_dev *child;
+ struct pci_bus *b;
+
+ if (!dev)
+ return list_first_entry_or_null(&bus->devices, struct pci_dev,
+ bus_list);
+
+ if (dev->transparent && dev->subordinate) {
+ child = list_first_entry_or_null(&dev->subordinate->devices,
+ struct pci_dev, bus_list);
+ if (child)
+ return child;
+ }
+
+ /*
+ * Ascend back from transparent buses, but no higher than the original
+ * bus.
+ */
+ b = dev->bus;
+ while (list_is_last(&dev->bus_list, &b->devices)) {
+ if (b == bus)
+ return NULL;
+ dev = b->self;
+ b = dev->bus;
+ }
+
+ return list_next_entry(dev, bus_list);
+}
+
struct pci_bus *pci_bus_get(struct pci_bus *bus)
{
if (bus)
diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
index 77efa4c5c960..9d0cbe6c8991 100644
--- a/drivers/pci/pci.h
+++ b/drivers/pci/pci.h
@@ -6,6 +6,7 @@
#include <linux/align.h>
#include <linux/bitfield.h>
#include <linux/pci.h>
+#include <linux/util_macros.h>
#include <trace/events/pci.h>
struct pcie_tlp_log;
@@ -546,6 +547,27 @@ bool pci_bus_clip_resource(struct pci_dev *dev, int idx);
void pci_walk_bus_locked(struct pci_bus *top,
int (*cb)(struct pci_dev *, void *),
void *userdata);
+struct pci_dev *pci_bus_next_window_dev(struct pci_bus *bus,
+ struct pci_dev *dev);
+
+/*
+ * Iterate over the devices whose resources may reside directly in the
+ * windows of @bus: the devices on @bus and, recursively, the devices
+ * below its transparent bridges. A transparent bridge also forwards
+ * accesses outside of its own windows.
+ */
+#define pci_bus_for_each_window_dev(bus, dev) \
+ for (dev = pci_bus_next_window_dev(bus, NULL); dev; \
+ dev = pci_bus_next_window_dev(bus, dev))
+
+/*
+ * Iterate over the resources of the devices of
+ * pci_bus_for_each_window_dev() that reside directly in window @win.
+ */
+#define pci_bus_for_each_window_resource(bus, win, dev, res) \
+ pci_bus_for_each_window_dev(bus, dev) \
+ pci_dev_for_each_resource(dev, res) \
+ for_each_if((res)->parent == (win))
const char *pci_resource_name(struct pci_dev *dev, unsigned int i);
bool pci_resource_is_optional(const struct pci_dev *dev, int resno);
--
2.47.3
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 15/15] PCI: Release only PCI resources when releasing a resource tree
[not found] <20261201155844.2278-1-ilpo.jarvinen@linux.intel.com>
` (6 preceding siblings ...)
2026-12-01 15:58 ` [PATCH 14/15] PCI: Add iterators for devices and resources in bus windows Ilpo Järvinen
@ 2026-12-01 15:58 ` Ilpo Järvinen
2026-12-02 0:22 ` Ilpo Järvinen
7 siblings, 1 reply; 9+ messages in thread
From: Ilpo Järvinen @ 2026-12-01 15:58 UTC (permalink / raw)
To: Bjorn Helgaas, Lorenzo Pieralisi, Rob Herring,
Krzysztof Wilczyński, Alex Deucher, Christian König,
Jani Nikula, Joonas Lahtinen, Rodrigo Vivi, Tvrtko Ursulin,
Matthew Brost, Thomas Hellström, Michał Winiarski,
Ville Syrjälä, David Airlie, Simona Vetter, dri-devel,
amd-gfx, intel-gfx, intel-xe, linux-pci, linux-kernel
Cc: Ilpo Järvinen
pci_bridge_release_resources() and the BAR resize rollback release
everything underneath a bridge window using release_child_resources().
Releasing children does not differentiate between PCI resources and
other resources, such as regions requested by a driver, so resources
that are in use can be ripped out. Unlike with pci_release_resource(),
IORESOURCE_UNSET is not set for the released resources, and the resource
core logs them only as "release child resource %pR", which gives no hint
of the PCI device they belong to.
Add pci_release_resource_tree() that releases a PCI resource and,
recursively, the resources of the devices on the subordinate bus that
reside underneath it. The resources of the devices behind a transparent
bridge may also reside directly in the window above the bridge. Walk the
PCI hierarchy to find those resources and compare their number with the
number of children in the resource tree. If anything else resides
underneath, such as a region requested by a driver or the BARs of
enabled VFs, fail without releasing anything. Hold resource_lock across
the check and the release so that no resource can be requested in
between.
Update each released resource with pci_resource_post_release(), which
also logs the release with the PCI device and the resource name.
Use pci_release_resource_tree() in pci_bridge_release_resources() and in
the BAR resize rollback. All saved resources were released once before
the BAR resize, so releasing them in the rollback is not expected to
fail. Warn and skip restoring a resource if it does.
Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
---
drivers/pci/pci.h | 1 +
drivers/pci/setup-bus.c | 12 ++---
drivers/pci/setup-res.c | 97 +++++++++++++++++++++++++++++++++++++++++
3 files changed, 104 insertions(+), 6 deletions(-)
diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
index 9d0cbe6c8991..09457fc8e11d 100644
--- a/drivers/pci/pci.h
+++ b/drivers/pci/pci.h
@@ -604,6 +604,7 @@ struct resource *pbus_select_window(struct pci_bus *bus,
const struct resource *res);
void pci_reassigndev_resource_alignment(struct pci_dev *dev);
void pci_disable_bridge_window(struct pci_dev *dev);
+int pci_release_resource_tree(struct pci_dev *dev, int resno);
struct pci_bus *pci_bus_get(struct pci_bus *bus);
void pci_bus_put(struct pci_bus *bus);
diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
index e5bbde75337d..18c8db2a6f28 100644
--- a/drivers/pci/setup-bus.c
+++ b/drivers/pci/setup-bus.c
@@ -1666,10 +1666,7 @@ static void pci_bridge_release_resources(struct pci_bus *bus,
idx = pci_resource_num(dev, b_win);
- /* If there are children, release them all */
- release_child_resources(b_win);
-
- ret = pci_release_resource(dev, idx);
+ ret = pci_release_resource_tree(dev, idx);
if (ret)
return;
@@ -2471,8 +2468,7 @@ int pci_do_resource_release_and_resize(struct pci_dev *pdev, int resno, int size
if (!resource_assigned(res))
continue;
- release_child_resources(res);
- pci_release_resource(dev, i);
+ pci_release_resource_tree(dev, i);
}
/* Restore parents before their children */
@@ -2482,6 +2478,10 @@ int pci_do_resource_release_and_resize(struct pci_dev *pdev, int resno, int size
i = pci_resource_num(dev, res);
+ /* All saved resources were released once before the resize */
+ if (WARN_ON_ONCE(resource_assigned(res)))
+ continue;
+
pci_dev_res_restore(dev_res);
if (pci_claim_resource(dev, i))
diff --git a/drivers/pci/setup-res.c b/drivers/pci/setup-res.c
index a56e0cc3db58..8440c780bc92 100644
--- a/drivers/pci/setup-res.c
+++ b/drivers/pci/setup-res.c
@@ -498,6 +498,103 @@ int pci_release_resource(struct pci_dev *dev, int resno)
}
EXPORT_SYMBOL(pci_release_resource);
+/*
+ * Check whether @res of @dev can be released together with everything
+ * underneath it. Directly underneath @res, only resources of the devices
+ * on the subordinate bus of @dev, or behind transparent bridges on it, are
+ * allowed, and the same applies to each of them in turn. Anything else,
+ * such as a region requested by a driver or the BARs of enabled VFs, pins
+ * @res in place.
+ */
+static bool pci_can_release_resource_tree(struct pci_dev *dev,
+ struct resource *res)
+{
+ unsigned int children = 0, pci_children = 0;
+ struct pci_bus *bus = dev->subordinate;
+ struct pci_dev *child;
+ struct resource *r;
+
+ lockdep_assert_held_write(&resource_lock);
+
+ resource_tree_for_each(res, r, true)
+ children++;
+
+ if (!children)
+ return true;
+
+ if (bus) {
+ pci_bus_for_each_window_resource(bus, res, child, r) {
+ pci_children++;
+ if (!pci_can_release_resource_tree(child, r))
+ return false;
+ }
+ }
+
+ if (children != pci_children) {
+ int resno = pci_resource_num(dev, res);
+ const char *res_name = pci_resource_name(dev, resno);
+
+ pci_info(dev, "%s %pR: can't release; contains resources in use\n",
+ res_name, res);
+ return false;
+ }
+
+ return true;
+}
+
+static void __pci_release_resource_tree(struct pci_dev *dev,
+ struct resource *res)
+{
+ int resno = pci_resource_num(dev, res);
+ struct pci_bus *bus = dev->subordinate;
+ struct pci_dev *child;
+ struct resource *r;
+
+ lockdep_assert_held_write(&resource_lock);
+
+ if (bus) {
+ pci_bus_for_each_window_resource(bus, res, child, r)
+ __pci_release_resource_tree(child, r);
+ }
+
+ if (release_resource_locked(res)) {
+ const char *res_name = pci_resource_name(dev, resno);
+
+ pci_dbg(dev, "%s %pR: can't release\n", res_name, res);
+ return;
+ }
+
+ pci_resource_post_release(dev, resno);
+}
+
+/**
+ * pci_release_resource_tree - Release a PCI resource and its PCI descendants
+ * @dev: PCI device
+ * @resno: Resource number
+ *
+ * Release @resno of @dev and, recursively, the resources of the devices
+ * on the subordinate bus underneath it, including those behind transparent
+ * bridges. If anything else resides underneath, nothing is released.
+ *
+ * Return: 0 on success, or -EBUSY if @resno could not be released.
+ */
+int pci_release_resource_tree(struct pci_dev *dev, int resno)
+{
+ struct resource *res = pci_resource_n(dev, resno);
+
+ guard(write_lock)(&resource_lock);
+
+ if (!resource_assigned(res))
+ return 0;
+
+ if (!pci_can_release_resource_tree(dev, res))
+ return -EBUSY;
+
+ __pci_release_resource_tree(dev, res);
+
+ return 0;
+}
+
int pci_enable_resources(struct pci_dev *dev, int mask)
{
u16 cmd, old_cmd;
--
2.47.3
^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH 15/15] PCI: Release only PCI resources when releasing a resource tree
2026-12-01 15:58 ` [PATCH 15/15] PCI: Release only PCI resources when releasing a resource tree Ilpo Järvinen
@ 2026-12-02 0:22 ` Ilpo Järvinen
0 siblings, 0 replies; 9+ messages in thread
From: Ilpo Järvinen @ 2026-12-02 0:22 UTC (permalink / raw)
To: Bjorn Helgaas, Anthony Pighin
Cc: Lorenzo Pieralisi, Rob Herring, Krzysztof Wilczyński,
Alex Deucher, Christian König, Jani Nikula, Joonas Lahtinen,
Rodrigo Vivi, Tvrtko Ursulin, Matthew Brost,
Thomas Hellström, Michał Winiarski,
Ville Syrjälä, David Airlie, Simona Vetter, dri-devel,
amd-gfx, intel-gfx, intel-xe, linux-pci, LKML
[-- Attachment #1: Type: text/plain, Size: 7532 bytes --]
On Tue, 1 Dec 2026, Ilpo Järvinen wrote:
> pci_bridge_release_resources() and the BAR resize rollback release
> everything underneath a bridge window using release_child_resources().
> Releasing children does not differentiate between PCI resources and
> other resources, such as regions requested by a driver, so resources
> that are in use can be ripped out. Unlike with pci_release_resource(),
> IORESOURCE_UNSET is not set for the released resources, and the resource
> core logs them only as "release child resource %pR", which gives no hint
> of the PCI device they belong to.
>
> Add pci_release_resource_tree() that releases a PCI resource and,
> recursively, the resources of the devices on the subordinate bus that
> reside underneath it. The resources of the devices behind a transparent
> bridge may also reside directly in the window above the bridge. Walk the
> PCI hierarchy to find those resources and compare their number with the
> number of children in the resource tree. If anything else resides
> underneath, such as a region requested by a driver or the BARs of
> enabled VFs, fail without releasing anything. Hold resource_lock across
> the check and the release so that no resource can be requested in
> between.
>
> Update each released resource with pci_resource_post_release(), which
> also logs the release with the PCI device and the resource name.
>
> Use pci_release_resource_tree() in pci_bridge_release_resources() and in
> the BAR resize rollback. All saved resources were released once before
> the BAR resize, so releasing them in the rollback is not expected to
> fail. Warn and skip restoring a resource if it does.
>
> Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
While doing some follow-up work, I realized we've actually seen device
resources getting ripped off the resource tree not that far back. So here
are a few extra tags related to that realization:
Fixes: 5009b46025ac ("PCI: add pci_bridge_release_resources and pci_bus_release_bridge_resources")
Link: https://lore.kernel.org/linux-pci/20260727193714.598562-1-anthony.pighin@nokia.com/
Reported-by: Anthony Pighin <anthony.pighin@nokia.com>
--
i.
> ---
> drivers/pci/pci.h | 1 +
> drivers/pci/setup-bus.c | 12 ++---
> drivers/pci/setup-res.c | 97 +++++++++++++++++++++++++++++++++++++++++
> 3 files changed, 104 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
> index 9d0cbe6c8991..09457fc8e11d 100644
> --- a/drivers/pci/pci.h
> +++ b/drivers/pci/pci.h
> @@ -604,6 +604,7 @@ struct resource *pbus_select_window(struct pci_bus *bus,
> const struct resource *res);
> void pci_reassigndev_resource_alignment(struct pci_dev *dev);
> void pci_disable_bridge_window(struct pci_dev *dev);
> +int pci_release_resource_tree(struct pci_dev *dev, int resno);
> struct pci_bus *pci_bus_get(struct pci_bus *bus);
> void pci_bus_put(struct pci_bus *bus);
>
> diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
> index e5bbde75337d..18c8db2a6f28 100644
> --- a/drivers/pci/setup-bus.c
> +++ b/drivers/pci/setup-bus.c
> @@ -1666,10 +1666,7 @@ static void pci_bridge_release_resources(struct pci_bus *bus,
>
> idx = pci_resource_num(dev, b_win);
>
> - /* If there are children, release them all */
> - release_child_resources(b_win);
> -
> - ret = pci_release_resource(dev, idx);
> + ret = pci_release_resource_tree(dev, idx);
> if (ret)
> return;
>
> @@ -2471,8 +2468,7 @@ int pci_do_resource_release_and_resize(struct pci_dev *pdev, int resno, int size
> if (!resource_assigned(res))
> continue;
>
> - release_child_resources(res);
> - pci_release_resource(dev, i);
> + pci_release_resource_tree(dev, i);
> }
>
> /* Restore parents before their children */
> @@ -2482,6 +2478,10 @@ int pci_do_resource_release_and_resize(struct pci_dev *pdev, int resno, int size
>
> i = pci_resource_num(dev, res);
>
> + /* All saved resources were released once before the resize */
> + if (WARN_ON_ONCE(resource_assigned(res)))
> + continue;
> +
> pci_dev_res_restore(dev_res);
>
> if (pci_claim_resource(dev, i))
> diff --git a/drivers/pci/setup-res.c b/drivers/pci/setup-res.c
> index a56e0cc3db58..8440c780bc92 100644
> --- a/drivers/pci/setup-res.c
> +++ b/drivers/pci/setup-res.c
> @@ -498,6 +498,103 @@ int pci_release_resource(struct pci_dev *dev, int resno)
> }
> EXPORT_SYMBOL(pci_release_resource);
>
> +/*
> + * Check whether @res of @dev can be released together with everything
> + * underneath it. Directly underneath @res, only resources of the devices
> + * on the subordinate bus of @dev, or behind transparent bridges on it, are
> + * allowed, and the same applies to each of them in turn. Anything else,
> + * such as a region requested by a driver or the BARs of enabled VFs, pins
> + * @res in place.
> + */
> +static bool pci_can_release_resource_tree(struct pci_dev *dev,
> + struct resource *res)
> +{
> + unsigned int children = 0, pci_children = 0;
> + struct pci_bus *bus = dev->subordinate;
> + struct pci_dev *child;
> + struct resource *r;
> +
> + lockdep_assert_held_write(&resource_lock);
> +
> + resource_tree_for_each(res, r, true)
> + children++;
> +
> + if (!children)
> + return true;
> +
> + if (bus) {
> + pci_bus_for_each_window_resource(bus, res, child, r) {
> + pci_children++;
> + if (!pci_can_release_resource_tree(child, r))
> + return false;
> + }
> + }
> +
> + if (children != pci_children) {
> + int resno = pci_resource_num(dev, res);
> + const char *res_name = pci_resource_name(dev, resno);
> +
> + pci_info(dev, "%s %pR: can't release; contains resources in use\n",
> + res_name, res);
> + return false;
> + }
> +
> + return true;
> +}
> +
> +static void __pci_release_resource_tree(struct pci_dev *dev,
> + struct resource *res)
> +{
> + int resno = pci_resource_num(dev, res);
> + struct pci_bus *bus = dev->subordinate;
> + struct pci_dev *child;
> + struct resource *r;
> +
> + lockdep_assert_held_write(&resource_lock);
> +
> + if (bus) {
> + pci_bus_for_each_window_resource(bus, res, child, r)
> + __pci_release_resource_tree(child, r);
> + }
> +
> + if (release_resource_locked(res)) {
> + const char *res_name = pci_resource_name(dev, resno);
> +
> + pci_dbg(dev, "%s %pR: can't release\n", res_name, res);
> + return;
> + }
> +
> + pci_resource_post_release(dev, resno);
> +}
> +
> +/**
> + * pci_release_resource_tree - Release a PCI resource and its PCI descendants
> + * @dev: PCI device
> + * @resno: Resource number
> + *
> + * Release @resno of @dev and, recursively, the resources of the devices
> + * on the subordinate bus underneath it, including those behind transparent
> + * bridges. If anything else resides underneath, nothing is released.
> + *
> + * Return: 0 on success, or -EBUSY if @resno could not be released.
> + */
> +int pci_release_resource_tree(struct pci_dev *dev, int resno)
> +{
> + struct resource *res = pci_resource_n(dev, resno);
> +
> + guard(write_lock)(&resource_lock);
> +
> + if (!resource_assigned(res))
> + return 0;
> +
> + if (!pci_can_release_resource_tree(dev, res))
> + return -EBUSY;
> +
> + __pci_release_resource_tree(dev, res);
> +
> + return 0;
> +}
> +
> int pci_enable_resources(struct pci_dev *dev, int mask)
> {
> u16 cmd, old_cmd;
>
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2026-12-02 0:23 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <20261201155844.2278-1-ilpo.jarvinen@linux.intel.com>
2026-12-01 15:58 ` [PATCH 01/15] PCI: Release all resources before restoring them in BAR resize rollback Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 02/15] PCI: Set new BAR Size only after releasing the BARs for BAR resize Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 03/15] PCI: Abort BAR resize only if saving the resized BAR fails Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 04/15] PCI: Consistently use Header Type to detect bridge headers Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 09/15] PCI: Do not release resources with children in pci_release_resource() Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 13/15] PCI: Split pci_resource_post_release() out of pci_release_resource() Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 14/15] PCI: Add iterators for devices and resources in bus windows Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 15/15] PCI: Release only PCI resources when releasing a resource tree Ilpo Järvinen
2026-12-02 0:22 ` Ilpo Järvinen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox