* [PATCH 00/15] PCI: Resource release fixes and improvements
@ 2026-12-01 15:58 Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 01/15] PCI: Release all resources before restoring them in BAR resize rollback Ilpo Järvinen
` (15 more replies)
0 siblings, 16 replies; 18+ 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
Cc: linux-kernel, Ilpo Järvinen
Hi all,
Releasing a PCI resource does not care what resides underneath it. A
released bridge window takes its children along, including regions
requested by drivers, which can corrupt the resource tree once the
window and its resources are assigned elsewhere.
This series makes pci_release_resource() refuse resources with children
and adds pci_release_resource_tree() that releases only PCI resources.
The check and the release are done under resource_lock, which the
resource core now exposes. Exposing resource_lock is probably the most
debatable part; an alternative to this would be to pass check and
release callbacks to a new resource core function.
The first 5 patches are related fixes.
The most immediate effect of these changes is for driver initiated BAR
resize which could fail if there are any extra resources underneath
the one to be resized. My impression is that this is not the case with
any of the GPU drivers using BAR resize but it would be good still that
GPU people take a look and test as needed.
The series is a prerequisite for sizing already assigned bridge windows.
The resource fitting and assignment algorithm self-race conditions due
to insufficient locking are out-of-scope. They are fixed by this series:
https://lore.kernel.org/linux-pci/20261125230925.2441-1-ilpo.jarvinen@linux.intel.com/
... and another (unsubmitted) series on top of that to bring
pci_assign_resource() and pci_bus_alloc_resource() under the same lock.
Ilpo Järvinen (15):
PCI: Release all resources before restoring them in BAR resize
rollback
PCI: Set new BAR Size only after releasing the BARs for BAR resize
PCI: Abort BAR resize only if saving the resized BAR fails
PCI: Consistently use Header Type to detect bridge headers
resource: Check that the resource is in the tree when releasing it
resource: Add KUnit test for releasing a resource not in the tree
resource: Add release_leaf_resource()
resource: Add KUnit test for release_leaf_resource()
PCI: Do not release resources with children in pci_release_resource()
resource: Rename next_resource() and for_each_resource()
resource: Make resource_lock and resource tree traversal available
resource: Add release_resource_locked()
PCI: Split pci_resource_post_release() out of pci_release_resource()
PCI: Add iterators for devices and resources in bus windows
PCI: Release only PCI resources when releasing a resource tree
drivers/pci/bus.c | 33 +++++++++++
drivers/pci/pci.h | 23 ++++++++
drivers/pci/setup-bus.c | 66 ++++++++++++++-------
drivers/pci/setup-res.c | 124 +++++++++++++++++++++++++++++++++++++---
include/linux/ioport.h | 23 ++++++++
kernel/resource.c | 75 +++++++++++++++++-------
kernel/resource_kunit.c | 44 ++++++++++++++
7 files changed, 339 insertions(+), 49 deletions(-)
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
--
2.47.3
^ permalink raw reply [flat|nested] 18+ messages in thread
* [PATCH 01/15] PCI: Release all resources before restoring them in BAR resize rollback
2026-12-01 15:58 [PATCH 00/15] PCI: Resource release fixes and improvements Ilpo Järvinen
@ 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
` (14 subsequent siblings)
15 siblings, 0 replies; 18+ 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] 18+ messages in thread
* [PATCH 02/15] PCI: Set new BAR Size only after releasing the BARs for BAR resize
2026-12-01 15:58 [PATCH 00/15] PCI: Resource release fixes and improvements Ilpo Järvinen
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
` (13 subsequent siblings)
15 siblings, 0 replies; 18+ 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] 18+ messages in thread
* [PATCH 03/15] PCI: Abort BAR resize only if saving the resized BAR fails
2026-12-01 15:58 [PATCH 00/15] PCI: Resource release fixes and improvements Ilpo Järvinen
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
` (12 subsequent siblings)
15 siblings, 0 replies; 18+ 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] 18+ messages in thread
* [PATCH 04/15] PCI: Consistently use Header Type to detect bridge headers
2026-12-01 15:58 [PATCH 00/15] PCI: Resource release fixes and improvements Ilpo Järvinen
` (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 05/15] resource: Check that the resource is in the tree when releasing it Ilpo Järvinen
` (11 subsequent siblings)
15 siblings, 0 replies; 18+ 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] 18+ messages in thread
* [PATCH 05/15] resource: Check that the resource is in the tree when releasing it
2026-12-01 15:58 [PATCH 00/15] PCI: Resource release fixes and improvements Ilpo Järvinen
` (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 06/15] resource: Add KUnit test for releasing a resource not in the tree Ilpo Järvinen
` (10 subsequent siblings)
15 siblings, 0 replies; 18+ 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-kernel
Cc: Ilpo Järvinen, stable
__release_resource() dereferences the parent of the resource without
checking whether the resource is in a resource tree at all. Releasing
a resource that is not in the tree or has already been removed from it
results in a NULL pointer dereference. Callers cannot check the parent
beforehand without racing as release_resource() takes the resource lock
only internally.
Looping a BAR resize through sysfs against a rescan of the parent bus
on a QEMU guest released a PCI bridge window that had already been
released:
BUG: kernel NULL pointer dereference, address: 0000000000000038
Oops: Oops: 0000 [#1] SMP
CPU: 0 UID: 0 PID: 49 Comm: init Not tainted 7.4.0-rc1-00001-g4aa3acaaeb08 #1 PREEMPTLAZY
RIP: 0010:__release_resource+0x8/0x70
Call Trace:
release_resource+0x22/0x40
pci_release_resource+0x54/0x90
pci_do_resource_release_and_resize+0x1f3/0x3d0
__resource_resize_store+0xf0/0x180
kernfs_fop_write_iter+0xfe/0x150
vfs_write+0xf7/0x160
ksys_write+0x6c/0xa0
do_syscall_64+0x3b/0x1b0
entry_SYSCALL_64_after_hwframe+0x4b/0x53
The double release was due to a race in the PCI bridge window sizing
and resource assignment, which is fixed separately, but the resource
tree code should have prevented the NULL pointer dereference too.
Return -EINVAL if the resource has no parent.
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Cc: stable@vger.kernel.org
---
The PCI race fixes series mentioned in the changelog is here:
https://lore.kernel.org/linux-pci/20261125230925.2441-1-ilpo.jarvinen@linux.intel.com/
kernel/resource.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/kernel/resource.c b/kernel/resource.c
index 0c315c917fbb..68843852f342 100644
--- a/kernel/resource.c
+++ b/kernel/resource.c
@@ -218,6 +218,9 @@ static int __release_resource(struct resource *old, bool release_child)
{
struct resource *tmp, **p, *chd;
+ if (!old->parent)
+ return -EINVAL;
+
p = &old->parent->child;
for (;;) {
tmp = *p;
--
2.47.3
^ permalink raw reply related [flat|nested] 18+ messages in thread
* [PATCH 06/15] resource: Add KUnit test for releasing a resource not in the tree
2026-12-01 15:58 [PATCH 00/15] PCI: Resource release fixes and improvements Ilpo Järvinen
` (4 preceding siblings ...)
2026-12-01 15:58 ` [PATCH 05/15] resource: Check that the resource is in the tree when releasing it Ilpo Järvinen
@ 2026-12-01 15:58 ` Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 07/15] resource: Add release_leaf_resource() Ilpo Järvinen
` (9 subsequent siblings)
15 siblings, 0 replies; 18+ 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-kernel
Cc: Ilpo Järvinen
release_resource() returns -EINVAL for a resource that has no parent
instead of dereferencing the NULL parent pointer.
Add a test that releases a resource before it is inserted into a tree,
and releases it a second time after inserting and releasing it once.
Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
---
kernel/resource_kunit.c | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/kernel/resource_kunit.c b/kernel/resource_kunit.c
index 9eeb2b1a85c0..c8e49f757efa 100644
--- a/kernel/resource_kunit.c
+++ b/kernel/resource_kunit.c
@@ -292,10 +292,26 @@ static void resource_test_region_intersects(struct kunit *test)
IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE));
}
+static void resource_test_release_not_in_tree(struct kunit *test)
+{
+ struct resource root = DEFINE_RES_MEM_NAMED(0, SZ_64K, "test root");
+ struct resource res = DEFINE_RES_MEM_NAMED(0x1000, 0x100, "test res");
+
+ KUNIT_EXPECT_EQ(test, release_resource(&res), -EINVAL);
+
+ KUNIT_ASSERT_EQ(test, request_resource(&root, &res), 0);
+ KUNIT_EXPECT_EQ(test, release_resource(&res), 0);
+ KUNIT_EXPECT_NULL(test, res.parent);
+ KUNIT_EXPECT_NULL(test, root.child);
+
+ KUNIT_EXPECT_EQ(test, release_resource(&res), -EINVAL);
+}
+
static struct kunit_case resource_test_cases[] = {
KUNIT_CASE(resource_test_union),
KUNIT_CASE(resource_test_intersection),
KUNIT_CASE(resource_test_region_intersects),
+ KUNIT_CASE(resource_test_release_not_in_tree),
{}
};
--
2.47.3
^ permalink raw reply related [flat|nested] 18+ messages in thread
* [PATCH 07/15] resource: Add release_leaf_resource()
2026-12-01 15:58 [PATCH 00/15] PCI: Resource release fixes and improvements Ilpo Järvinen
` (5 preceding siblings ...)
2026-12-01 15:58 ` [PATCH 06/15] resource: Add KUnit test for releasing a resource not in the tree Ilpo Järvinen
@ 2026-12-01 15:58 ` Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 08/15] resource: Add KUnit test for release_leaf_resource() Ilpo Järvinen
` (8 subsequent siblings)
15 siblings, 0 replies; 18+ 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-kernel
Cc: Ilpo Järvinen
release_resource() releases a resource together with its descendants.
The descendants remain attached to the released resource and keep their
addresses, so when the resource is placed elsewhere afterwards, the
descendants may end up outside of their parent.
Add release_leaf_resource() that does not release a resource that has
children. Check for the children under resource_lock so that no child
can be added in between.
Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
---
include/linux/ioport.h | 1 +
kernel/resource.c | 20 ++++++++++++++++++++
2 files changed, 21 insertions(+)
diff --git a/include/linux/ioport.h b/include/linux/ioport.h
index f7930b3dfd0a..f38800fb957d 100644
--- a/include/linux/ioport.h
+++ b/include/linux/ioport.h
@@ -244,6 +244,7 @@ extern struct resource soft_reserve_resource;
extern struct resource *request_resource_conflict(struct resource *root, struct resource *new);
extern int request_resource(struct resource *root, struct resource *new);
extern int release_resource(struct resource *new);
+int release_leaf_resource(struct resource *old);
void release_child_resources(struct resource *new);
extern void reserve_region_with_split(struct resource *root,
resource_size_t start, resource_size_t end,
diff --git a/kernel/resource.c b/kernel/resource.c
index 68843852f342..d4a4bbc80190 100644
--- a/kernel/resource.c
+++ b/kernel/resource.c
@@ -326,6 +326,26 @@ int release_resource(struct resource *old)
EXPORT_SYMBOL(release_resource);
+/**
+ * release_leaf_resource - release a resource that has no children
+ * @old: resource pointer
+ *
+ * Unlike release_resource(), do not release @old if it has children as
+ * they would remain attached to @old with their addresses.
+ *
+ * Return: 0 on success, -EBUSY if @old has children, or -EINVAL if @old is
+ * not in a resource tree.
+ */
+int release_leaf_resource(struct resource *old)
+{
+ guard(write_lock)(&resource_lock);
+
+ if (old->child)
+ return -EBUSY;
+
+ return __release_resource(old, true);
+}
+
static bool is_type_match(struct resource *p, unsigned long flags, unsigned long desc)
{
return (p->flags & flags) == flags && (desc == IORES_DESC_NONE || desc == p->desc);
--
2.47.3
^ permalink raw reply related [flat|nested] 18+ messages in thread
* [PATCH 08/15] resource: Add KUnit test for release_leaf_resource()
2026-12-01 15:58 [PATCH 00/15] PCI: Resource release fixes and improvements Ilpo Järvinen
` (6 preceding siblings ...)
2026-12-01 15:58 ` [PATCH 07/15] resource: Add release_leaf_resource() 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
` (7 subsequent siblings)
15 siblings, 0 replies; 18+ 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-kernel
Cc: Ilpo Järvinen
release_leaf_resource() must not release a resource that has children,
and must leave the resource tree untouched when it refuses to release.
Add a test that checks the -EBUSY case with a child, the release once the
child is gone, and the -EINVAL case for a resource that is not in a tree.
Export release_leaf_resource() for the test when it is built as a module.
Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
---
kernel/resource.c | 3 +++
kernel/resource_kunit.c | 28 ++++++++++++++++++++++++++++
2 files changed, 31 insertions(+)
diff --git a/kernel/resource.c b/kernel/resource.c
index d4a4bbc80190..1b64a65fc591 100644
--- a/kernel/resource.c
+++ b/kernel/resource.c
@@ -10,6 +10,8 @@
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+#include <kunit/visibility.h>
+
#include <linux/export.h>
#include <linux/errno.h>
#include <linux/ioport.h>
@@ -345,6 +347,7 @@ int release_leaf_resource(struct resource *old)
return __release_resource(old, true);
}
+EXPORT_SYMBOL_IF_KUNIT(release_leaf_resource);
static bool is_type_match(struct resource *p, unsigned long flags, unsigned long desc)
{
diff --git a/kernel/resource_kunit.c b/kernel/resource_kunit.c
index c8e49f757efa..d24d5e83b734 100644
--- a/kernel/resource_kunit.c
+++ b/kernel/resource_kunit.c
@@ -307,11 +307,38 @@ static void resource_test_release_not_in_tree(struct kunit *test)
KUNIT_EXPECT_EQ(test, release_resource(&res), -EINVAL);
}
+static void resource_test_release_leaf(struct kunit *test)
+{
+ struct resource root = DEFINE_RES_MEM_NAMED(0, SZ_64K, "test root");
+ struct resource res = DEFINE_RES_MEM_NAMED(0x1000, 0x1000, "test res");
+ struct resource child = DEFINE_RES_MEM_NAMED(0x1100, 0x100, "test child");
+
+ KUNIT_EXPECT_EQ(test, release_leaf_resource(&res), -EINVAL);
+
+ KUNIT_ASSERT_EQ(test, request_resource(&root, &res), 0);
+ KUNIT_ASSERT_EQ(test, request_resource(&res, &child), 0);
+
+ KUNIT_EXPECT_EQ(test, release_leaf_resource(&res), -EBUSY);
+ KUNIT_EXPECT_PTR_EQ(test, root.child, &res);
+ KUNIT_EXPECT_PTR_EQ(test, res.parent, &root);
+ KUNIT_EXPECT_PTR_EQ(test, res.child, &child);
+ KUNIT_EXPECT_PTR_EQ(test, child.parent, &res);
+
+ KUNIT_EXPECT_EQ(test, release_leaf_resource(&child), 0);
+ KUNIT_EXPECT_NULL(test, child.parent);
+ KUNIT_EXPECT_NULL(test, res.child);
+
+ KUNIT_EXPECT_EQ(test, release_leaf_resource(&res), 0);
+ KUNIT_EXPECT_NULL(test, res.parent);
+ KUNIT_EXPECT_NULL(test, root.child);
+}
+
static struct kunit_case resource_test_cases[] = {
KUNIT_CASE(resource_test_union),
KUNIT_CASE(resource_test_intersection),
KUNIT_CASE(resource_test_region_intersects),
KUNIT_CASE(resource_test_release_not_in_tree),
+ KUNIT_CASE(resource_test_release_leaf),
{}
};
@@ -321,5 +348,6 @@ static struct kunit_suite resource_test_suite = {
};
kunit_test_suite(resource_test_suite);
+MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");
MODULE_DESCRIPTION("I/O Port & Memory Resource manager unit tests");
MODULE_LICENSE("GPL");
--
2.47.3
^ permalink raw reply related [flat|nested] 18+ messages in thread
* [PATCH 09/15] PCI: Do not release resources with children in pci_release_resource()
2026-12-01 15:58 [PATCH 00/15] PCI: Resource release fixes and improvements Ilpo Järvinen
` (7 preceding siblings ...)
2026-12-01 15:58 ` [PATCH 08/15] resource: Add KUnit test for release_leaf_resource() Ilpo Järvinen
@ 2026-12-01 15:58 ` Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 10/15] resource: Rename next_resource() and for_each_resource() Ilpo Järvinen
` (6 subsequent siblings)
15 siblings, 0 replies; 18+ 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] 18+ messages in thread
* [PATCH 10/15] resource: Rename next_resource() and for_each_resource()
2026-12-01 15:58 [PATCH 00/15] PCI: Resource release fixes and improvements Ilpo Järvinen
` (8 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 11/15] resource: Make resource_lock and resource tree traversal available Ilpo Järvinen
` (5 subsequent siblings)
15 siblings, 0 replies; 18+ 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-kernel
Cc: Ilpo Järvinen
The names of next_resource() and for_each_resource() do not tell that
they traverse the resource tree. for_each_resource() is also already
used by drbd for its own resources, which prevents making the macro
available outside of resource.c under that name.
Rename next_resource() to resource_tree_next() and for_each_resource()
to resource_tree_for_each().
No functional changes.
Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
---
kernel/resource.c | 20 ++++++++++----------
1 file changed, 10 insertions(+), 10 deletions(-)
diff --git a/kernel/resource.c b/kernel/resource.c
index 1b64a65fc591..8d6928c638ff 100644
--- a/kernel/resource.c
+++ b/kernel/resource.c
@@ -66,8 +66,8 @@ static DEFINE_RWLOCK(resource_lock);
* traversal. If @p is a descendant of @subtree_root, only traverse
* the subtree under @subtree_root.
*/
-static struct resource *next_resource(struct resource *p, bool skip_children,
- struct resource *subtree_root)
+static struct resource *resource_tree_next(struct resource *p, bool skip_children,
+ struct resource *subtree_root)
{
if (!skip_children && p->child)
return p->child;
@@ -86,9 +86,9 @@ static struct resource *next_resource(struct resource *p, bool skip_children,
* NOTE: '__p' is introduced to avoid shadowing '_p' outside of loop.
* And it is referenced to avoid unused variable warning.
*/
-#define for_each_resource(_root, _p, _skip_children) \
+#define resource_tree_for_each(_root, _p, _skip_children) \
for (typeof(_root) __root = (_root), __p = _p = __root->child; \
- __p && _p; _p = next_resource(_p, _skip_children, __root))
+ __p && _p; _p = resource_tree_next(_p, _skip_children, __root))
#ifdef CONFIG_PROC_FS
@@ -102,7 +102,7 @@ static void *r_start(struct seq_file *m, loff_t *pos)
loff_t l = *pos;
read_lock(&resource_lock);
- for_each_resource(root, p, false) {
+ resource_tree_for_each(root, p, false) {
if (l-- == 0)
break;
}
@@ -116,7 +116,7 @@ static void *r_next(struct seq_file *m, void *v, loff_t *pos)
(*pos)++;
- return (void *)next_resource(p, false, NULL);
+ return (void *)resource_tree_next(p, false, NULL);
}
static void r_stop(struct seq_file *m, void *v)
@@ -388,7 +388,7 @@ static int find_next_res(struct resource *parent, resource_size_t start,
read_lock(&resource_lock);
- for_each_resource(parent, p, skip_children) {
+ resource_tree_for_each(parent, p, skip_children) {
/* If we passed the resource we are looking for, stop */
if (p->start > end) {
p = NULL;
@@ -654,7 +654,7 @@ static int __region_intersects(struct resource *parent, resource_size_t start,
* |-- "System RAM" --||-- "CXL Window 0a" --|
*/
covered = false;
- for_each_resource(p, dp, false) {
+ resource_tree_for_each(p, dp, false) {
if (!resource_overlaps(dp, &res))
continue;
if (is_type_match(dp, flags, desc)) {
@@ -1869,7 +1869,7 @@ int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
int err = 0;
read_lock(&resource_lock);
- for_each_resource(&iomem_resource, p, false) {
+ resource_tree_for_each(&iomem_resource, p, false) {
/*
* We can probably skip the resources without
* IORESOURCE_IO attribute?
@@ -1918,7 +1918,7 @@ bool resource_is_exclusive(struct resource *root, u64 addr, resource_size_t size
struct resource *p;
read_lock(&resource_lock);
- for_each_resource(root, p, skip_children) {
+ resource_tree_for_each(root, p, skip_children) {
if (p->start >= addr + size)
break;
if (p->end < addr) {
--
2.47.3
^ permalink raw reply related [flat|nested] 18+ messages in thread
* [PATCH 11/15] resource: Make resource_lock and resource tree traversal available
2026-12-01 15:58 [PATCH 00/15] PCI: Resource release fixes and improvements Ilpo Järvinen
` (9 preceding siblings ...)
2026-12-01 15:58 ` [PATCH 10/15] resource: Rename next_resource() and for_each_resource() Ilpo Järvinen
@ 2026-12-01 15:58 ` Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 12/15] resource: Add release_resource_locked() Ilpo Järvinen
` (4 subsequent siblings)
15 siblings, 0 replies; 18+ 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-kernel
Cc: Ilpo Järvinen
Code that needs to look at the resource tree to decide whether it can
release a resource together with the resources underneath it has to
traverse the tree, and both the check and the release must be done
under resource_lock so that no resource gets added in between.
Make resource_lock and resource_tree_next() available through ioport.h
and move resource_tree_for_each() there. As resource_tree_next() is now
public, assert that resource_lock is held.
Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
---
include/linux/ioport.h | 21 +++++++++++++++++++++
kernel/resource.c | 21 +++++++--------------
2 files changed, 28 insertions(+), 14 deletions(-)
diff --git a/include/linux/ioport.h b/include/linux/ioport.h
index f38800fb957d..830d621907b7 100644
--- a/include/linux/ioport.h
+++ b/include/linux/ioport.h
@@ -14,6 +14,7 @@
#include <linux/bits.h>
#include <linux/compiler.h>
#include <linux/minmax.h>
+#include <linux/spinlock_types.h>
#include <linux/types.h>
/*
* Resources are tree-like, allowing
@@ -241,6 +242,26 @@ extern struct resource ioport_resource;
extern struct resource iomem_resource;
extern struct resource soft_reserve_resource;
+/* Protects all resource trees */
+extern rwlock_t resource_lock;
+
+struct resource *resource_tree_next(struct resource *p, bool skip_children,
+ struct resource *subtree_root);
+
+/*
+ * Traverse the resource subtree under @_root in pre-order, excluding
+ * @_root itself. With @_skip_children, only the children of @_root are
+ * traversed.
+ *
+ * Context: Caller must hold resource_lock.
+ *
+ * NOTE: '__p' is introduced to avoid shadowing '_p' outside of loop.
+ * And it is referenced to avoid unused variable warning.
+ */
+#define resource_tree_for_each(_root, _p, _skip_children) \
+ for (typeof(_root) __root = (_root), __p = _p = __root->child; \
+ __p && _p; _p = resource_tree_next(_p, _skip_children, __root))
+
extern struct resource *request_resource_conflict(struct resource *root, struct resource *new);
extern int request_resource(struct resource *root, struct resource *new);
extern int release_resource(struct resource *new);
diff --git a/kernel/resource.c b/kernel/resource.c
index 8d6928c638ff..fbcbdae99887 100644
--- a/kernel/resource.c
+++ b/kernel/resource.c
@@ -58,17 +58,21 @@ struct resource soft_reserve_resource = {
.flags = IORESOURCE_MEM,
};
-static DEFINE_RWLOCK(resource_lock);
+DEFINE_RWLOCK(resource_lock);
/*
* Return the next node of @p in pre-order tree traversal. If
* @skip_children is true, skip the descendant nodes of @p in
* traversal. If @p is a descendant of @subtree_root, only traverse
* the subtree under @subtree_root.
+ *
+ * Context: Caller must hold resource_lock.
*/
-static struct resource *resource_tree_next(struct resource *p, bool skip_children,
- struct resource *subtree_root)
+struct resource *resource_tree_next(struct resource *p, bool skip_children,
+ struct resource *subtree_root)
{
+ lockdep_assert_held(&resource_lock);
+
if (!skip_children && p->child)
return p->child;
while (!p->sibling && p->parent) {
@@ -79,17 +83,6 @@ static struct resource *resource_tree_next(struct resource *p, bool skip_childre
return p->sibling;
}
-/*
- * Traverse the resource subtree under @_root in pre-order, excluding
- * @_root itself.
- *
- * NOTE: '__p' is introduced to avoid shadowing '_p' outside of loop.
- * And it is referenced to avoid unused variable warning.
- */
-#define resource_tree_for_each(_root, _p, _skip_children) \
- for (typeof(_root) __root = (_root), __p = _p = __root->child; \
- __p && _p; _p = resource_tree_next(_p, _skip_children, __root))
-
#ifdef CONFIG_PROC_FS
enum { MAX_IORES_LEVEL = 8 };
--
2.47.3
^ permalink raw reply related [flat|nested] 18+ messages in thread
* [PATCH 12/15] resource: Add release_resource_locked()
2026-12-01 15:58 [PATCH 00/15] PCI: Resource release fixes and improvements Ilpo Järvinen
` (10 preceding siblings ...)
2026-12-01 15:58 ` [PATCH 11/15] resource: Make resource_lock and resource tree traversal available 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
` (3 subsequent siblings)
15 siblings, 0 replies; 18+ 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-kernel
Cc: Ilpo Järvinen
release_resource() takes resource_lock, so it cannot be used to release
resources while traversing the resource tree under the lock.
Add release_resource_locked() for callers that already hold
resource_lock for writing.
Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
---
include/linux/ioport.h | 1 +
kernel/resource.c | 16 ++++++++++++++++
2 files changed, 17 insertions(+)
diff --git a/include/linux/ioport.h b/include/linux/ioport.h
index 830d621907b7..60d15191bcc3 100644
--- a/include/linux/ioport.h
+++ b/include/linux/ioport.h
@@ -265,6 +265,7 @@ struct resource *resource_tree_next(struct resource *p, bool skip_children,
extern struct resource *request_resource_conflict(struct resource *root, struct resource *new);
extern int request_resource(struct resource *root, struct resource *new);
extern int release_resource(struct resource *new);
+int release_resource_locked(struct resource *old);
int release_leaf_resource(struct resource *old);
void release_child_resources(struct resource *new);
extern void reserve_region_with_split(struct resource *root,
diff --git a/kernel/resource.c b/kernel/resource.c
index fbcbdae99887..35bd2fb91619 100644
--- a/kernel/resource.c
+++ b/kernel/resource.c
@@ -321,6 +321,22 @@ int release_resource(struct resource *old)
EXPORT_SYMBOL(release_resource);
+/**
+ * release_resource_locked - release a previously reserved resource
+ * @old: resource pointer
+ *
+ * Like release_resource() but the caller must hold resource_lock for
+ * writing.
+ *
+ * Return: 0 on success, or -EINVAL if @old is not in a resource tree.
+ */
+int release_resource_locked(struct resource *old)
+{
+ lockdep_assert_held_write(&resource_lock);
+
+ return __release_resource(old, true);
+}
+
/**
* release_leaf_resource - release a resource that has no children
* @old: resource pointer
--
2.47.3
^ permalink raw reply related [flat|nested] 18+ messages in thread
* [PATCH 13/15] PCI: Split pci_resource_post_release() out of pci_release_resource()
2026-12-01 15:58 [PATCH 00/15] PCI: Resource release fixes and improvements Ilpo Järvinen
` (11 preceding siblings ...)
2026-12-01 15:58 ` [PATCH 12/15] resource: Add release_resource_locked() 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
` (2 subsequent siblings)
15 siblings, 0 replies; 18+ 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] 18+ messages in thread
* [PATCH 14/15] PCI: Add iterators for devices and resources in bus windows
2026-12-01 15:58 [PATCH 00/15] PCI: Resource release fixes and improvements Ilpo Järvinen
` (12 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
2026-12-01 16:56 ` ✓ i915.CI.BAT: success for PCI: Resource release fixes and improvements Patchwork
15 siblings, 0 replies; 18+ 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] 18+ messages in thread
* [PATCH 15/15] PCI: Release only PCI resources when releasing a resource tree
2026-12-01 15:58 [PATCH 00/15] PCI: Resource release fixes and improvements Ilpo Järvinen
` (13 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
2026-12-01 16:56 ` ✓ i915.CI.BAT: success for PCI: Resource release fixes and improvements Patchwork
15 siblings, 1 reply; 18+ 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] 18+ messages in thread
* ✓ i915.CI.BAT: success for PCI: Resource release fixes and improvements
2026-12-01 15:58 [PATCH 00/15] PCI: Resource release fixes and improvements Ilpo Järvinen
` (14 preceding siblings ...)
2026-12-01 15:58 ` [PATCH 15/15] PCI: Release only PCI resources when releasing a resource tree Ilpo Järvinen
@ 2026-12-01 16:56 ` Patchwork
15 siblings, 0 replies; 18+ messages in thread
From: Patchwork @ 2026-12-01 16:56 UTC (permalink / raw)
To: Ilpo Järvinen; +Cc: intel-gfx
[-- Attachment #1: Type: text/plain, Size: 1001 bytes --]
== Series Details ==
Series: PCI: Resource release fixes and improvements
URL : https://patchwork.freedesktop.org/series/180186/
State : success
== Summary ==
CI Bug Log - changes from CI_DRM_19647 -> Patchwork_180186v1
====================================================
Summary
-------
**SUCCESS**
No regressions found.
External URL: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_180186v1/index.html
Participating hosts (38 -> 36)
------------------------------
Missing (2): bat-dg2-13 bat-adls-6
Changes
-------
No changes found
Build changes
-------------
* Linux: CI_DRM_19647 -> Patchwork_180186v1
CI-20190529: 20190529
CI_DRM_19647: 84c2801ba68c91f613802f666010676d07847116 @ git://anongit.freedesktop.org/gfx-ci/linux
IGT_9220: 9220
Patchwork_180186v1: 84c2801ba68c91f613802f666010676d07847116 @ git://anongit.freedesktop.org/gfx-ci/linux
== Logs ==
For more details see: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_180186v1/index.html
[-- Attachment #2: Type: text/html, Size: 1566 bytes --]
^ permalink raw reply [flat|nested] 18+ 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; 18+ 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] 18+ messages in thread
end of thread, other threads:[~2026-12-02 0:23 UTC | newest]
Thread overview: 18+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 15:58 [PATCH 00/15] PCI: Resource release fixes and improvements Ilpo Järvinen
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 05/15] resource: Check that the resource is in the tree when releasing it Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 06/15] resource: Add KUnit test for releasing a resource not in the tree Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 07/15] resource: Add release_leaf_resource() Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 08/15] resource: Add KUnit test for release_leaf_resource() 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 10/15] resource: Rename next_resource() and for_each_resource() Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 11/15] resource: Make resource_lock and resource tree traversal available Ilpo Järvinen
2026-12-01 15:58 ` [PATCH 12/15] resource: Add release_resource_locked() 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
2026-12-01 16:56 ` ✓ i915.CI.BAT: success for PCI: Resource release fixes and improvements Patchwork
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox