Linux PCI subsystem development
 help / color / mirror / Atom feed
* [PATCH v2 01/16] PCI: Release all resources before restoring them in BAR resize rollback
       [not found] <20261205003957.3304-1-ilpo.jarvinen@linux.intel.com>
@ 2026-12-05  0:39 ` Ilpo Järvinen
  2026-12-05  0:39 ` [PATCH v2 02/16] PCI: Set new BAR Size only after releasing the BARs for BAR resize Ilpo Järvinen
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 18+ messages in thread
From: Ilpo Järvinen @ 2026-12-05  0:39 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, Anthony Pighin, 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 v2 02/16] PCI: Set new BAR Size only after releasing the BARs for BAR resize
       [not found] <20261205003957.3304-1-ilpo.jarvinen@linux.intel.com>
  2026-12-05  0:39 ` [PATCH v2 01/16] PCI: Release all resources before restoring them in BAR resize rollback Ilpo Järvinen
@ 2026-12-05  0:39 ` Ilpo Järvinen
  2026-12-05  0:39 ` [PATCH v2 03/16] PCI: Abort BAR resize only if saving the resized BAR fails Ilpo Järvinen
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 18+ messages in thread
From: Ilpo Järvinen @ 2026-12-05  0:39 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, Anthony Pighin, 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 | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
index b69041fbbd39..db16c8c2913d 100644
--- a/drivers/pci/setup-bus.c
+++ b/drivers/pci/setup-bus.c
@@ -2394,10 +2394,6 @@ int pci_do_resource_release_and_resize(struct pci_dev *pdev, int resno, int size
 	if (old < 0)
 		return old;
 
-	ret = pci_rebar_set_size(pdev, resno, size);
-	if (ret)
-		return ret;
-
 	down_read(&pci_bus_sem);
 
 	pci_dev_for_each_resource(pdev, r, i) {
@@ -2416,12 +2412,16 @@ int pci_do_resource_release_and_resize(struct pci_dev *pdev, int resno, int size
 		pci_release_resource(pdev, i);
 	}
 
+	ret = pci_rebar_set_size(pdev, resno, size);
+	if (ret)
+		goto restore;
+
 	pci_resize_resource_set_size(pdev, resno, size);
 
 	if (bridge) {
 		ret = pbus_reassign_bridge_resources(bus, res, &saved);
 		if (ret)
-			goto restore;
+			goto restore_size;
 	} else {
 		/* No bridge window to adjust; let the core reassign the bus. */
 		pci_bus_assign_resources(bus);
@@ -2429,7 +2429,7 @@ int pci_do_resource_release_and_resize(struct pci_dev *pdev, int resno, int size
 		list_for_each_entry(dev_res, &saved, list) {
 			if (!resource_assigned(dev_res->res)) {
 				ret = -ENOSPC;
-				goto restore;
+				goto restore_size;
 			}
 		}
 	}
@@ -2439,7 +2439,7 @@ int pci_do_resource_release_and_resize(struct pci_dev *pdev, int resno, int size
 	pci_dev_res_free_list(&saved);
 	return ret;
 
-restore:
+restore_size:
 	/*
 	 * Revert to the old configuration.
 	 *
@@ -2449,6 +2449,7 @@ int pci_do_resource_release_and_resize(struct pci_dev *pdev, int resno, int size
 	 */
 	pci_rebar_set_size(pdev, resno, old);
 
+restore:
 	/*
 	 * Release all resources first, otherwise a resource not yet released
 	 * could block restoring another resource into its original place.
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v2 03/16] PCI: Abort BAR resize only if saving the resized BAR fails
       [not found] <20261205003957.3304-1-ilpo.jarvinen@linux.intel.com>
  2026-12-05  0:39 ` [PATCH v2 01/16] PCI: Release all resources before restoring them in BAR resize rollback Ilpo Järvinen
  2026-12-05  0:39 ` [PATCH v2 02/16] PCI: Set new BAR Size only after releasing the BARs for BAR resize Ilpo Järvinen
@ 2026-12-05  0:39 ` Ilpo Järvinen
  2026-12-05  1:04   ` sashiko-bot
  2026-12-05  0:39 ` [PATCH v2 04/16] PCI: Consistently use Header Type to detect bridge headers Ilpo Järvinen
                   ` (5 subsequent siblings)
  8 siblings, 1 reply; 18+ messages in thread
From: Ilpo Järvinen @ 2026-12-05  0:39 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, Anthony Pighin, 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 db16c8c2913d..c18664b5e5a2 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 v2 04/16] PCI: Consistently use Header Type to detect bridge headers
       [not found] <20261205003957.3304-1-ilpo.jarvinen@linux.intel.com>
                   ` (2 preceding siblings ...)
  2026-12-05  0:39 ` [PATCH v2 03/16] PCI: Abort BAR resize only if saving the resized BAR fails Ilpo Järvinen
@ 2026-12-05  0:39 ` Ilpo Järvinen
  2026-12-05  1:04   ` sashiko-bot
  2026-12-05  0:39 ` [PATCH v2 09/16] PCI: Do not release resources with children in pci_release_resource() Ilpo Järvinen
                   ` (4 subsequent siblings)
  8 siblings, 1 reply; 18+ messages in thread
From: Ilpo Järvinen @ 2026-12-05  0:39 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, Anthony Pighin, 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 c18664b5e5a2..cf062a9ac9a9 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 v2 09/16] PCI: Do not release resources with children in pci_release_resource()
       [not found] <20261205003957.3304-1-ilpo.jarvinen@linux.intel.com>
                   ` (3 preceding siblings ...)
  2026-12-05  0:39 ` [PATCH v2 04/16] PCI: Consistently use Header Type to detect bridge headers Ilpo Järvinen
@ 2026-12-05  0:39 ` Ilpo Järvinen
  2026-12-05  1:04   ` sashiko-bot
  2026-12-05  0:39 ` [PATCH v2 13/16] PCI: Split pci_resource_post_release() out of pci_release_resource() Ilpo Järvinen
                   ` (3 subsequent siblings)
  8 siblings, 1 reply; 18+ messages in thread
From: Ilpo Järvinen @ 2026-12-05  0:39 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, Anthony Pighin, 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 cf062a9ac9a9..033f5fab7cc0 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 v2 13/16] PCI: Split pci_resource_post_release() out of pci_release_resource()
       [not found] <20261205003957.3304-1-ilpo.jarvinen@linux.intel.com>
                   ` (4 preceding siblings ...)
  2026-12-05  0:39 ` [PATCH v2 09/16] PCI: Do not release resources with children in pci_release_resource() Ilpo Järvinen
@ 2026-12-05  0:39 ` Ilpo Järvinen
  2026-12-05  0:39 ` [PATCH v2 14/16] PCI: Add iterators for devices and resources in bus windows Ilpo Järvinen
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 18+ messages in thread
From: Ilpo Järvinen @ 2026-12-05  0:39 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, Anthony Pighin, 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 v2 14/16] PCI: Add iterators for devices and resources in bus windows
       [not found] <20261205003957.3304-1-ilpo.jarvinen@linux.intel.com>
                   ` (5 preceding siblings ...)
  2026-12-05  0:39 ` [PATCH v2 13/16] PCI: Split pci_resource_post_release() out of pci_release_resource() Ilpo Järvinen
@ 2026-12-05  0:39 ` Ilpo Järvinen
  2026-12-05  1:02   ` sashiko-bot
  2026-12-05  0:39 ` [PATCH v2 15/16] PCI: Keep the range of fixed resources when releasing them Ilpo Järvinen
  2026-12-05  0:39 ` [PATCH v2 16/16] PCI: Release only PCI resources when releasing a resource tree Ilpo Järvinen
  8 siblings, 1 reply; 18+ messages in thread
From: Ilpo Järvinen @ 2026-12-05  0:39 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, Anthony Pighin, 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 v2 15/16] PCI: Keep the range of fixed resources when releasing them
       [not found] <20261205003957.3304-1-ilpo.jarvinen@linux.intel.com>
                   ` (6 preceding siblings ...)
  2026-12-05  0:39 ` [PATCH v2 14/16] PCI: Add iterators for devices and resources in bus windows Ilpo Järvinen
@ 2026-12-05  0:39 ` Ilpo Järvinen
  2026-12-05  0:39 ` [PATCH v2 16/16] PCI: Release only PCI resources when releasing a resource tree Ilpo Järvinen
  8 siblings, 0 replies; 18+ messages in thread
From: Ilpo Järvinen @ 2026-12-05  0:39 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, Anthony Pighin, linux-pci,
	linux-kernel
  Cc: Ilpo Järvinen

pci_resource_post_release() resets the start of a released resource to 0
and marks it IORESOURCE_UNSET. For an IORESOURCE_PCI_FIXED resource,
this discards the fixed range, whereas the rest of the resource fitting
and assignment code never changes the range of a fixed resource.

None of the callers of pci_release_resource() releases a fixed resource
in practice, but for consistency, return early from
pci_resource_post_release() for fixed resources to keep their range and
flags. The resource is already detached from the resource tree, so
pdev_assign_fixed_resources() can claim it again.

Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
---
 drivers/pci/setup-res.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/drivers/pci/setup-res.c b/drivers/pci/setup-res.c
index a56e0cc3db58..82efd4a76c15 100644
--- a/drivers/pci/setup-res.c
+++ b/drivers/pci/setup-res.c
@@ -471,6 +471,10 @@ static void pci_resource_post_release(struct pci_dev *dev, int resno)
 
 	pci_info(dev, "%s %pR: released\n", res_name, res);
 
+	/* Keep the range so that the fixed resource can be claimed again */
+	if (res->flags & IORESOURCE_PCI_FIXED)
+		return;
+
 	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 v2 16/16] PCI: Release only PCI resources when releasing a resource tree
       [not found] <20261205003957.3304-1-ilpo.jarvinen@linux.intel.com>
                   ` (7 preceding siblings ...)
  2026-12-05  0:39 ` [PATCH v2 15/16] PCI: Keep the range of fixed resources when releasing them Ilpo Järvinen
@ 2026-12-05  0:39 ` Ilpo Järvinen
  2026-12-05  1:11   ` sashiko-bot
  8 siblings, 1 reply; 18+ messages in thread
From: Ilpo Järvinen @ 2026-12-05  0:39 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, Anthony Pighin, Yinghai Lu,
	Jesse Barnes, Alex Chiang, 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. Also fail if any of the
resources is IORESOURCE_PCI_FIXED, as it cannot follow the window should
the window move. 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.

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>
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 | 107 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 114 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 033f5fab7cc0..e14066c6aa8d 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;
 
@@ -2472,8 +2469,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 */
@@ -2483,6 +2479,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 82efd4a76c15..848d05fbe76f 100644
--- a/drivers/pci/setup-res.c
+++ b/drivers/pci/setup-res.c
@@ -502,6 +502,113 @@ 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. So does an IORESOURCE_PCI_FIXED resource anywhere in the
+ * tree.
+ */
+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);
+
+	if (res->flags & IORESOURCE_PCI_FIXED) {
+		int resno = pci_resource_num(dev, res);
+		const char *res_name = pci_resource_name(dev, resno);
+
+		pci_info(dev, "%s %pR: can't release; fixed resource\n",
+			 res_name, res);
+		return false;
+	}
+
+	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

* Re: [PATCH v2 14/16] PCI: Add iterators for devices and resources in bus windows
  2026-12-05  0:39 ` [PATCH v2 14/16] PCI: Add iterators for devices and resources in bus windows Ilpo Järvinen
@ 2026-12-05  1:02   ` sashiko-bot
  2026-12-05  1:12     ` Ilpo Järvinen
  0 siblings, 1 reply; 18+ messages in thread
From: sashiko-bot @ 2026-12-05  1:02 UTC (permalink / raw)
  To: Ilpo Järvinen, dri-devel, amd-gfx, intel-gfx, intel-xe,
	linux-pci

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] PCI: break inside pci_bus_for_each_window_resource() only exits
  inner loop
--

commit 73da7b0150edfd796b5aa15f8d77912a014f57a3
Author: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>

PCI: Add iterators for devices and resources in bus windows

This commit adds pci_bus_for_each_window_dev() and
pci_bus_for_each_window_resource() to iterate over devices on a bus (and
below its transparent bridges) and their resources that reside directly in
a given bus window.

> diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
> index 77efa4c5c960e..9d0cbe6c89917 100644
> --- a/drivers/pci/pci.h
> +++ b/drivers/pci/pci.h

[ ... ]

> @@ -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))

[Severity: Low]
Will a break statement inside pci_bus_for_each_window_resource() only exit
the inner pci_dev_for_each_resource() loop?

Because this macro nests two separate for loops, break only stops scanning
the current device's resources while the outer
pci_bus_for_each_window_dev() loop silently continues to the remaining
devices, executing the loop body again and overwriting dev and res.

Backing the iterator with a single-loop helper that finds the next matching
resource, or documenting that break is unsupported, would avoid this trap.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205003957.3304-1-ilpo.jarvinen@linux.intel.com?part=14

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2 03/16] PCI: Abort BAR resize only if saving the resized BAR fails
  2026-12-05  0:39 ` [PATCH v2 03/16] PCI: Abort BAR resize only if saving the resized BAR fails Ilpo Järvinen
@ 2026-12-05  1:04   ` sashiko-bot
  2026-12-05  1:25     ` Ilpo Järvinen
  0 siblings, 1 reply; 18+ messages in thread
From: sashiko-bot @ 2026-12-05  1:04 UTC (permalink / raw)
  To: Ilpo Järvinen, dri-devel, amd-gfx, intel-gfx, intel-xe,
	linux-pci

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] PCI: continuing after -ENOMEM in
  pci_do_resource_release_and_resize()
- [Medium] PCI: pci_do_resource_release_and_resize() skips unsaved
  unassigned BARs
--

commit 2e0d31c7b9377b1d5689204c149a99e359a637cb
Author: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>

PCI: Abort BAR resize only if saving the resized BAR fails

This commit changes pci_do_resource_release_and_resize() so that a
failure to save a BAR in the saved list only aborts and rolls back when
the failing BAR is the one being resized, leaving other BARs in place
and continuing.

> diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
> index db16c8c2913d6..c18664b5e5a25 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);
>  	}

[Severity: Medium]
Can continuing after pci_dev_res_add_to_list() fails with -ENOMEM turn a
clean rollback into a kernel panic?

pci_dev_res_add_to_list() only fails when kzalloc_obj() cannot allocate a
struct pci_dev_resource.  If that happens for a sibling BAR after the
resized BAR was already released, leaving the sibling BAR assigned keeps
b_win->child non-NULL.

pbus_reassign_bridge_resources() then skips its own save check and calls
into pdev_sort_resources(), which allocates the same struct pci_dev_resource
with kzalloc_obj() for the released resized BAR and panics on failure.

Even if the allocation failure is transient, leaving the sibling BAR
assigned pins the bridge window and can mask the -ENOMEM as -ENOSPC.

[Severity: Medium]
What happens if the unsaved sibling BAR is currently unassigned rather than
assigned?

Because this loop does not check resource_assigned(r), an unassigned BAR
with non-zero flags (for example, on a PCI_PROBE_ONLY platform) still
matches b_win via pbus_select_window().  Continuing on error leaves it
unassigned and absent from saved.

When b_win is not released (for example, when exclude_bars keeps another
BAR in place), pdev_sort_resources() includes that unassigned BAR and can
assign it into resno's vacated range while the enlarged resno fails with
-ENOSPC.

Rollback only releases resources tracked in saved and their children, so
neither b_win nor the sibling BAR is released.  pci_claim_resource() for
resno then fails with -EBUSY, leaving the resized BAR unassigned.

Checking (i == resno || !resource_assigned(r)) before jumping to restore
would avoid leaving an unassigned BAR untracked.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205003957.3304-1-ilpo.jarvinen@linux.intel.com?part=3

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2 09/16] PCI: Do not release resources with children in pci_release_resource()
  2026-12-05  0:39 ` [PATCH v2 09/16] PCI: Do not release resources with children in pci_release_resource() Ilpo Järvinen
@ 2026-12-05  1:04   ` sashiko-bot
  2026-12-05  1:29     ` Ilpo Järvinen
  0 siblings, 1 reply; 18+ messages in thread
From: sashiko-bot @ 2026-12-05  1:04 UTC (permalink / raw)
  To: Ilpo Järvinen, dri-devel, amd-gfx, intel-gfx, intel-xe,
	linux-pci

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] PCI: unchecked pci_release_resource() failure in
  __assign_resources_sorted()
- [Low] PCI: pci_release_resource() misreports -EINVAL as busy on parent
  TOCTOU race
- [Low] PCI: missing Fixes trailer for pci_release_resource() bug fix
--

commit ed0359ff94feed20c50628f709669e28b10a5020
Author: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>

> PCI: Do not release resources with children in pci_release_resource()
>
> 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>

[Severity: Low]
This looks like a bug fix for pci_release_resource() detaching resources
that still have children.  Should this include:

  Fixes: 8bb705e3e79d ("PCI: Add pci_resize_resource() for resizing BARs")

[ ... ]

> diff --git a/drivers/pci/setup-res.c b/drivers/pci/setup-res.c
> index 376f09630a4ab..1317d645a178a 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);

[Severity: Medium]
Now that pci_release_resource() can return -EBUSY, does
__assign_resources_sorted() need to handle a release failure too?

Its fallback path ignores the return value and leaves dev_res on head,
save_head, and realloc_head:

drivers/pci/setup-bus.c:__assign_resources_sorted() {
	...
	/* Release assigned resource */
	list_for_each_entry(dev_res, head, list) {
		res = dev_res->res;
		dev = dev_res->dev;

		pci_release_resource(dev, pci_resource_num(dev, res));
		pci_dev_res_restore(dev_res);
	}
	...
}

Because pci_do_resource_release_and_resize() only holds a read lock on
pci_bus_sem, a concurrent bus rescan can insert a child into a newly
assigned bridge window before that fallback loop runs:

CPU0                                  CPU1
pci_do_resource_release_and_resize()
  down_read(&pci_bus_sem)
  ...
  __assign_resources_sorted()
    assign_requested_resources_sorted()
      /* assigns bridge window res,
       * another resource fails */
                                      pci_rescan_bus()
                                        pci_assign_unassigned_bus_resources()
                                          down_read(&pci_bus_sem)
                                          ...
                                          allocate_resource()
                                            /* inserts child under res */
    pci_release_resource(...)  <-- -EBUSY, ignored
    pci_dev_res_restore(dev_res)  <-- WARN_ON_ONCE(resource_assigned(res))
    assign_requested_resources_sorted()
      pci_assign_resource(...)  <-- sets IORESOURCE_UNSET on live res

When release fails, res stays in the resource tree and hits
WARN_ON_ONCE(resource_assigned(res)) in pci_dev_res_restore().  The retry
then calls pci_assign_resource(), which sets IORESOURCE_UNSET before
reallocate_resource() fails with -EBUSY, leaving the live window marked
IORESOURCE_UNSET.

If a firmware address is available, pci_revert_fw_address() can also mutate
res->start and res->end without resource_lock while res is still linked in
the tree.  Removing dev_res from head, save_head, and realloc_head when
pci_release_resource() fails would avoid this.

> +	if (ret) {
> +		pci_info(dev, "%s %pR: can't release; contains resources in use\n",
> +			 res_name, res);
>  		return ret;
> +	}

[Severity: Low]
Should this distinguish -EINVAL from -EBUSY?

res->parent is checked without resource_lock, so if res is detached before
release_leaf_resource() acquires the lock, __release_resource() returns
-EINVAL instead of -EBUSY.  pci_release_resource() then logs that res
contains resources in use and returns -EINVAL rather than 0, causing
pci_do_resource_release_and_resize() to drop it from saved and abort the
resize if it is the resized BAR.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205003957.3304-1-ilpo.jarvinen@linux.intel.com?part=9

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2 04/16] PCI: Consistently use Header Type to detect bridge headers
  2026-12-05  0:39 ` [PATCH v2 04/16] PCI: Consistently use Header Type to detect bridge headers Ilpo Järvinen
@ 2026-12-05  1:04   ` sashiko-bot
  0 siblings, 0 replies; 18+ messages in thread
From: sashiko-bot @ 2026-12-05  1:04 UTC (permalink / raw)
  To: Ilpo Järvinen, dri-devel, amd-gfx, intel-gfx, intel-xe,
	linux-pci

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] PCI: Header Type bridge release conflicts with
  pdev_resources_assignable()
--

commit 41cfb3d22724c17ea21af1abcdf645f81bdde152
Author: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>

PCI: Consistently use Header Type to detect bridge headers

This commit updates pci_claim_bridge_resource(),
__pci_bridge_assign_resources(), and pci_bus_release_bridge_resources() to
check hdr_type instead of class when identifying PCI and CardBus bridges.

> diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
> index c18664b5e5a25..cf062a9ac9a93 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)

[Severity: High]
Can this permanently disable bridge windows on Type 1 bridges that
pdev_resources_assignable() skips?

pdev_resources_assignable() still filters by Class Code, returning false for
PCI_CLASS_NOT_DEFINED, PCI_CLASS_BRIDGE_HOST, and firmware-enabled
PCI_CLASS_SYSTEM_PIC devices.  During a retry,
pci_bus_release_bridge_resources() now releases and disables their windows.

On the next round, pdev_sort_resources() skips bus->self because
pdev_resources_assignable() is false, so the window is never reassigned and
downstream devices are stranded.

With add_list enabled, __pci_bus_size_bridges() still queues the window on
add_list, which pdev_sort_resources() leaves unconsumed and triggers
WARN_ON_ONCE(add_list && !list_empty(add_list)).

Similarly, __pci_bridge_assign_resources() now calls pci_setup_bridge() on
bridges skipped by pdev_assign_resources_sorted(), and
pci_claim_bridge_resource() clips and reprograms windows that
pdev_resources_assignable() refuses to manage.

Should these paths also check pdev_resources_assignable(), or should
pdev_resources_assignable() be aligned with Header Type handling?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205003957.3304-1-ilpo.jarvinen@linux.intel.com?part=4

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2 16/16] PCI: Release only PCI resources when releasing a resource tree
  2026-12-05  0:39 ` [PATCH v2 16/16] PCI: Release only PCI resources when releasing a resource tree Ilpo Järvinen
@ 2026-12-05  1:11   ` sashiko-bot
  2026-12-05  1:57     ` Ilpo Järvinen
  0 siblings, 1 reply; 18+ messages in thread
From: sashiko-bot @ 2026-12-05  1:11 UTC (permalink / raw)
  To: Ilpo Järvinen, dri-devel, amd-gfx, intel-gfx, intel-xe,
	linux-pci

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] PCI: BAR resize rollback fails on IORESOURCE_PCI_FIXED
  resources
- [Medium] PCI: pci_release_resource_tree() walks bus->devices without
  pci_bus_sem
- [Medium] PCI: pci_release_resource_tree() calls pci_info() under
  resource_lock
- [Low] PCI: pci_bridge_release_resources() discards -EBUSY causing
  repeated rescans
--

commit b918bf89459fc478f5ebfe229a3a0bbeb92b6ca2
Author: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>

PCI: Release only PCI resources when releasing a resource tree

This commit adds pci_release_resource_tree() to check and release only PCI
resources underneath a bridge window under resource_lock, and switches
pci_bridge_release_resources() and BAR resize rollback to use it.

Link: https://lore.kernel.org/linux-pci/20260727193714.598562-1-anthony.pighin@nokia.com/

> diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
> index 033f5fab7cc06..e14066c6aa8d8 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;

[Severity: Low]
Should pci_bridge_release_resources() propagate the -EBUSY failure instead
of discarding ret?

When a descendant is pinned, pci_release_resource_tree() leaves b_win
assigned, so every ancestor bridge in pci_bus_release_bridge_resources(...,
whole_subtree) re-walks the same unreleased branch under resource_lock and
repeats the same pci_info() message.

Because b_win stays assigned, each remaining failed resource on that bus in
pci_prepare_next_assign_round() and each retry round in
pci_assign_unassigned_root_bus_resources() then repeats those rescans and
duplicate logs.

[ ... ]

> @@ -2472,8 +2469,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 */
> @@ -2483,6 +2479,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))

[Severity: Medium]
Can BAR resize rollback fail here when an IORESOURCE_PCI_FIXED resource
shares the resized bridge window?

Before resizing, pci_do_resource_release_and_resize() releases pdev's BARs
with pci_release_resource(), which allows releasing IORESOURCE_PCI_FIXED
resources.  During the failed resize attempt,
__pci_bus_assign_resources_one() -> pdev_assign_fixed_resources() re-claims
that fixed BAR (or claims a sibling's fixed resource inside a moved window).

On rollback, pci_can_release_resource_tree() rejects fixed resources and
ancestor windows containing them, so pci_release_resource_tree() returns
-EBUSY.  The restore loop then hits WARN_ON_ONCE(resource_assigned(res))
and skips restoring the window, stranding other BARs if the window moved.

Also, releasing an IORESOURCE_PCI_FIXED BAR via pci_release_resource() before
the resize can unpin its parent bridge window even on a successful resize.

> diff --git a/drivers/pci/setup-res.c b/drivers/pci/setup-res.c
> index 82efd4a76c153..848d05fbe76fe 100644
> --- a/drivers/pci/setup-res.c
> +++ b/drivers/pci/setup-res.c
> @@ -502,6 +502,113 @@ int pci_release_resource(struct pci_dev *dev, int resno)

[ ... ]

> +	if (bus) {
> +		pci_bus_for_each_window_resource(bus, res, child, r) {
> +			pci_children++;
> +			if (!pci_can_release_resource_tree(child, r))
> +				return false;
> +		}
> +	}

[Severity: Medium]
Is it safe to walk bus->devices here and in __pci_release_resource_tree()
without holding pci_bus_sem?

CPU0                              CPU1
pci_host_probe()
  ...
  pci_bridge_release_resources()
    pci_release_resource_tree()
      write_lock(&resource_lock)
                                  remove_store()
                                    ...
                                    pci_destroy_dev()
                                      down_write(&pci_bus_sem)
                                      list_del(&dev->bus_list)
                                      up_write(&pci_bus_sem)
      pci_bus_next_window_dev()
        reads dev->bus_list  <-- LIST_POISON1 / use-after-free

pci_do_resource_release_and_resize() holds down_read(&pci_bus_sem), but
pci_bridge_release_resources() does not, and callers like pci_host_probe()
or acpi_pci_root_add() reach it without pci_rescan_remove_lock either
(SR-IOV probe/remove paths also mutate bus->devices under pci_bus_sem).

pci_destroy_dev() unlinks dev->bus_list before acquiring resource_lock in
pci_free_resources(), so resource_lock does not prevent list corruption or
use-after-free.  Asserting lockdep_assert_held(&pci_bus_sem) here and
taking down_read(&pci_bus_sem) in pci_bridge_release_resources() fixes it.

[ ... ]

> +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);
> +}

[ ... ]

> +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;
> +}

[Severity: Medium]
Can holding write_lock(&resource_lock) across the pci_info() calls in
pci_resource_post_release() and pci_can_release_resource_tree() stall other
CPUs on slow consoles?

At default console_loglevel, LOGLEVEL_INFO messages flush synchronously on
legacy framebuffer or serial consoles via console_unlock(), holding the
spinning resource_lock with preemption disabled for tens to hundreds of
milliseconds while other CPUs spin in ioremap() or /proc/iomem.

Previously, release_child_resources() only logged at KERN_DEBUG under
resource_lock and pci_release_resource() called pci_info() after dropping
it.

Also, unlike pci_can_release_resource_tree(), __pci_release_resource_tree()
does not check res->child before entering pci_bus_for_each_window_resource(),
scanning subordinate devices even when res has no children.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205003957.3304-1-ilpo.jarvinen@linux.intel.com?part=16

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2 14/16] PCI: Add iterators for devices and resources in bus windows
  2026-12-05  1:02   ` sashiko-bot
@ 2026-12-05  1:12     ` Ilpo Järvinen
  0 siblings, 0 replies; 18+ messages in thread
From: Ilpo Järvinen @ 2026-12-05  1:12 UTC (permalink / raw)
  To: sashiko-reviews; +Cc: dri-devel, amd-gfx, intel-gfx, intel-xe, linux-pci

[-- Attachment #1: Type: text/plain, Size: 3211 bytes --]

On Sat, 5 Dec 2026, sashiko-bot@kernel.org wrote:

> Thank you for your contribution! Sashiko AI review found 1 potential
> issue to consider:
> - [Low] PCI: break inside pci_bus_for_each_window_resource() only exits
>   inner loop
> --
> 
> commit 73da7b0150edfd796b5aa15f8d77912a014f57a3
> Author: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
> 
> PCI: Add iterators for devices and resources in bus windows
> 
> This commit adds pci_bus_for_each_window_dev() and
> pci_bus_for_each_window_resource() to iterate over devices on a bus (and
> below its transparent bridges) and their resources that reside directly in
> a given bus window.
> 
> > diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
> > index 77efa4c5c960e..9d0cbe6c89917 100644
> > --- a/drivers/pci/pci.h
> > +++ b/drivers/pci/pci.h
> 
> [ ... ]
> 
> > @@ -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))
> 
> [Severity: Low]
> Will a break statement inside pci_bus_for_each_window_resource() only exit
> the inner pci_dev_for_each_resource() loop?
> 
> Because this macro nests two separate for loops, break only stops scanning
> the current device's resources while the outer
> pci_bus_for_each_window_dev() loop silently continues to the remaining
> devices, executing the loop body again and overwriting dev and res.
> 
> Backing the iterator with a single-loop helper that finds the next matching
> resource, or documenting that break is unsupported, would avoid this trap.

I'm aware of this but these loops generally don't want to use break. 
There is not really an use case for looking up a single child resource 
for a bridge window. This is not a problem with the current loops and I 
don't foresee it becoming a realistic problem.

Converting these tidy loops into next based iteration is nowhere near
as elegant as this.

C's syntax is lacking here so the macro cannot mark the inner loops 
transparent to a break statement (e.g. using a compiler attribute), which 
would fulfill the natural expectation by the looks of the macro.

If somebody wants, I can add a comment.

-- 
 i.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2 03/16] PCI: Abort BAR resize only if saving the resized BAR fails
  2026-12-05  1:04   ` sashiko-bot
@ 2026-12-05  1:25     ` Ilpo Järvinen
  0 siblings, 0 replies; 18+ messages in thread
From: Ilpo Järvinen @ 2026-12-05  1:25 UTC (permalink / raw)
  To: sashiko-reviews; +Cc: dri-devel, amd-gfx, intel-gfx, intel-xe, linux-pci

[-- Attachment #1: Type: text/plain, Size: 3652 bytes --]

On Sat, 5 Dec 2026, sashiko-bot@kernel.org wrote:

> Thank you for your contribution! Sashiko AI review found 2 potential
> issues to consider:
> - [Medium] PCI: continuing after -ENOMEM in
>   pci_do_resource_release_and_resize()
> - [Medium] PCI: pci_do_resource_release_and_resize() skips unsaved
>   unassigned BARs
> --
> 
> commit 2e0d31c7b9377b1d5689204c149a99e359a637cb
> Author: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
> 
> PCI: Abort BAR resize only if saving the resized BAR fails
> 
> This commit changes pci_do_resource_release_and_resize() so that a
> failure to save a BAR in the saved list only aborts and rolls back when
> the failing BAR is the one being resized, leaving other BARs in place
> and continuing.
> 
> > diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
> > index db16c8c2913d6..c18664b5e5a25 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);
> >  	}
> 
> [Severity: Medium]
> Can continuing after pci_dev_res_add_to_list() fails with -ENOMEM turn a
> clean rollback into a kernel panic?

It can, like boot can too. One day, I'll remove that panic() but it isn't 
going to be in this series. There are no shortage of more important things 
to address than this panic().

> pci_dev_res_add_to_list() only fails when kzalloc_obj() cannot allocate a
> struct pci_dev_resource.  If that happens for a sibling BAR after the
> resized BAR was already released, leaving the sibling BAR assigned keeps
> b_win->child non-NULL.
> 
> pbus_reassign_bridge_resources() then skips its own save check and calls
> into pdev_sort_resources(), which allocates the same struct pci_dev_resource
> with kzalloc_obj() for the released resized BAR and panics on failure.
> 
> Even if the allocation failure is transient, leaving the sibling BAR
> assigned pins the bridge window and can mask the -ENOMEM as -ENOSPC.
> 
> [Severity: Medium]
> What happens if the unsaved sibling BAR is currently unassigned rather than
> assigned?
> 
> Because this loop does not check resource_assigned(r), an unassigned BAR
> with non-zero flags (for example, on a PCI_PROBE_ONLY platform) still
> matches b_win via pbus_select_window().  Continuing on error leaves it
> unassigned and absent from saved.
>
> When b_win is not released (for example, when exclude_bars keeps another
> BAR in place), pdev_sort_resources() includes that unassigned BAR and can
> assign it into resno's vacated range while the enlarged resno fails with
> -ENOSPC.
> 
> Rollback only releases resources tracked in saved and their children, so
> neither b_win nor the sibling BAR is released.  pci_claim_resource() for
> resno then fails with -EBUSY, leaving the resized BAR unassigned.
> 
> Checking (i == resno || !resource_assigned(r)) before jumping to restore
> would avoid leaving an unassigned BAR untracked.

Generally, if a BAR is not assigned, pci_enable_resources() would have 
already refused to enable the device, so it's hard to see how this would 
make things significantly worse or better. Not sure what could be salvaged 
in the suggested rollback scenario as it only trades one resource failure 
to another.

And it still has the alloc fail precondition too.

-- 
 i.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2 09/16] PCI: Do not release resources with children in pci_release_resource()
  2026-12-05  1:04   ` sashiko-bot
@ 2026-12-05  1:29     ` Ilpo Järvinen
  0 siblings, 0 replies; 18+ messages in thread
From: Ilpo Järvinen @ 2026-12-05  1:29 UTC (permalink / raw)
  To: sashiko-reviews; +Cc: dri-devel, amd-gfx, intel-gfx, intel-xe, linux-pci

[-- Attachment #1: Type: text/plain, Size: 5949 bytes --]

On Sat, 5 Dec 2026, sashiko-bot@kernel.org wrote:

> Thank you for your contribution! Sashiko AI review found 3 potential
> issues to consider:
> - [Medium] PCI: unchecked pci_release_resource() failure in
>   __assign_resources_sorted()
> - [Low] PCI: pci_release_resource() misreports -EINVAL as busy on parent
>   TOCTOU race
> - [Low] PCI: missing Fixes trailer for pci_release_resource() bug fix
> --
> 
> commit ed0359ff94feed20c50628f709669e28b10a5020
> Author: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
> 
> > PCI: Do not release resources with children in pci_release_resource()
> >
> > 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>
> 
> [Severity: Low]
> This looks like a bug fix for pci_release_resource() detaching resources
> that still have children.  Should this include:
> 
>   Fixes: 8bb705e3e79d ("PCI: Add pci_resize_resource() for resizing BARs")
> 
> [ ... ]
> 
> > diff --git a/drivers/pci/setup-res.c b/drivers/pci/setup-res.c
> > index 376f09630a4ab..1317d645a178a 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);
> 
> [Severity: Medium]
> Now that pci_release_resource() can return -EBUSY, does
> __assign_resources_sorted() need to handle a release failure too?
> 
> Its fallback path ignores the return value and leaves dev_res on head,
> save_head, and realloc_head:
> 
> drivers/pci/setup-bus.c:__assign_resources_sorted() {
> 	...
> 	/* Release assigned resource */
> 	list_for_each_entry(dev_res, head, list) {
> 		res = dev_res->res;
> 		dev = dev_res->dev;
> 
> 		pci_release_resource(dev, pci_resource_num(dev, res));
> 		pci_dev_res_restore(dev_res);
> 	}
> 	...
> }
> 
> Because pci_do_resource_release_and_resize() only holds a read lock on
> pci_bus_sem, a concurrent bus rescan can insert a child into a newly
> assigned bridge window before that fallback loop runs:
> 
> CPU0                                  CPU1
> pci_do_resource_release_and_resize()
>   down_read(&pci_bus_sem)
>   ...
>   __assign_resources_sorted()
>     assign_requested_resources_sorted()
>       /* assigns bridge window res,
>        * another resource fails */
>                                       pci_rescan_bus()
>                                         pci_assign_unassigned_bus_resources()
>                                           down_read(&pci_bus_sem)
>                                           ...
>                                           allocate_resource()
>                                             /* inserts child under res */
>     pci_release_resource(...)  <-- -EBUSY, ignored
>     pci_dev_res_restore(dev_res)  <-- WARN_ON_ONCE(resource_assigned(res))
>     assign_requested_resources_sorted()
>       pci_assign_resource(...)  <-- sets IORESOURCE_UNSET on live res
> 
> When release fails, res stays in the resource tree and hits
> WARN_ON_ONCE(resource_assigned(res)) in pci_dev_res_restore().  The retry
> then calls pci_assign_resource(), which sets IORESOURCE_UNSET before
> reallocate_resource() fails with -EBUSY, leaving the live window marked
> IORESOURCE_UNSET.
> 
> If a firmware address is available, pci_revert_fw_address() can also mutate
> res->start and res->end without resource_lock while res is still linked in
> the tree.  Removing dev_res from head, save_head, and realloc_head when
> pci_release_resource() fails would avoid this.
> 
> > +	if (ret) {
> > +		pci_info(dev, "%s %pR: can't release; contains resources in use\n",
> > +			 res_name, res);
> >  		return ret;
> > +	}
> 
> [Severity: Low]
> Should this distinguish -EINVAL from -EBUSY?
> 
> res->parent is checked without resource_lock, so if res is detached before
> release_leaf_resource() acquires the lock, __release_resource() returns
> -EINVAL instead of -EBUSY.  pci_release_resource() then logs that res
> contains resources in use and returns -EINVAL rather than 0, causing
> pci_do_resource_release_and_resize() to drop it from saved and abort the
> resize if it is the resized BAR.

Neither of the problems is in the scope for this series, see the cover 
letter for details (which sashiko clearly failed to do :-( ).

-- 
 i.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH v2 16/16] PCI: Release only PCI resources when releasing a resource tree
  2026-12-05  1:11   ` sashiko-bot
@ 2026-12-05  1:57     ` Ilpo Järvinen
  0 siblings, 0 replies; 18+ messages in thread
From: Ilpo Järvinen @ 2026-12-05  1:57 UTC (permalink / raw)
  To: sashiko-reviews; +Cc: dri-devel, amd-gfx, intel-gfx, intel-xe, linux-pci

[-- Attachment #1: Type: text/plain, Size: 8388 bytes --]

On Sat, 5 Dec 2026, sashiko-bot@kernel.org wrote:

> Thank you for your contribution! Sashiko AI review found 4 potential
> issues to consider:
> - [Medium] PCI: BAR resize rollback fails on IORESOURCE_PCI_FIXED
>   resources
> - [Medium] PCI: pci_release_resource_tree() walks bus->devices without
>   pci_bus_sem
> - [Medium] PCI: pci_release_resource_tree() calls pci_info() under
>   resource_lock
> - [Low] PCI: pci_bridge_release_resources() discards -EBUSY causing
>   repeated rescans
> --
> 
> commit b918bf89459fc478f5ebfe229a3a0bbeb92b6ca2
> Author: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
> 
> PCI: Release only PCI resources when releasing a resource tree
> 
> This commit adds pci_release_resource_tree() to check and release only PCI
> resources underneath a bridge window under resource_lock, and switches
> pci_bridge_release_resources() and BAR resize rollback to use it.
> 
> Link: https://lore.kernel.org/linux-pci/20260727193714.598562-1-anthony.pighin@nokia.com/
> 
> > diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
> > index 033f5fab7cc06..e14066c6aa8d8 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;
> 
> [Severity: Low]
> Should pci_bridge_release_resources() propagate the -EBUSY failure instead
> of discarding ret?
> 
> When a descendant is pinned, pci_release_resource_tree() leaves b_win
> assigned, so every ancestor bridge in pci_bus_release_bridge_resources(...,
> whole_subtree) re-walks the same unreleased branch under resource_lock and
> repeats the same pci_info() message.
> 
> Because b_win stays assigned, each remaining failed resource on that bus in
> pci_prepare_next_assign_round() and each retry round in
> pci_assign_unassigned_root_bus_resources() then repeats those rescans and
> duplicate logs.

I'll change this.

> [ ... ]
> 
> > @@ -2472,8 +2469,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 */
> > @@ -2483,6 +2479,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))
> 
> [Severity: Medium]
> Can BAR resize rollback fail here when an IORESOURCE_PCI_FIXED resource
> shares the resized bridge window?
> 
> Before resizing, pci_do_resource_release_and_resize() releases pdev's BARs
> with pci_release_resource(), which allows releasing IORESOURCE_PCI_FIXED
> resources.  During the failed resize attempt,
> __pci_bus_assign_resources_one() -> pdev_assign_fixed_resources() re-claims
> that fixed BAR (or claims a sibling's fixed resource inside a moved window).
> 
> On rollback, pci_can_release_resource_tree() rejects fixed resources and
> ancestor windows containing them, so pci_release_resource_tree() returns
> -EBUSY. 

Fix one case, the other starts to complain. I suppose I'll just add 
override which skips the FIXED check in case of rollback.

Generally, I'd want to make FIXED resources behave pretty normally when it 
comes to assign/release, but so that it imposes restriction on parent
window address range. But that cannot be done sensibly until after this 
and a handful of other series I've pending... Until then, they need to
pin windows.

-- 
 i.

> The restore loop then hits WARN_ON_ONCE(resource_assigned(res))
> and skips restoring the window, stranding other BARs if the window moved.
> 
> Also, releasing an IORESOURCE_PCI_FIXED BAR via pci_release_resource() before
> the resize can unpin its parent bridge window even on a successful resize.
> 
> > diff --git a/drivers/pci/setup-res.c b/drivers/pci/setup-res.c
> > index 82efd4a76c153..848d05fbe76fe 100644
> > --- a/drivers/pci/setup-res.c
> > +++ b/drivers/pci/setup-res.c
> > @@ -502,6 +502,113 @@ int pci_release_resource(struct pci_dev *dev, int resno)
> 
> [ ... ]
> 
> > +	if (bus) {
> > +		pci_bus_for_each_window_resource(bus, res, child, r) {
> > +			pci_children++;
> > +			if (!pci_can_release_resource_tree(child, r))
> > +				return false;
> > +		}
> > +	}
> 
> [Severity: Medium]
> Is it safe to walk bus->devices here and in __pci_release_resource_tree()
> without holding pci_bus_sem?
> 
> CPU0                              CPU1
> pci_host_probe()
>   ...
>   pci_bridge_release_resources()
>     pci_release_resource_tree()
>       write_lock(&resource_lock)
>                                   remove_store()
>                                     ...
>                                     pci_destroy_dev()
>                                       down_write(&pci_bus_sem)
>                                       list_del(&dev->bus_list)
>                                       up_write(&pci_bus_sem)
>       pci_bus_next_window_dev()
>         reads dev->bus_list  <-- LIST_POISON1 / use-after-free
> 
> pci_do_resource_release_and_resize() holds down_read(&pci_bus_sem), but
> pci_bridge_release_resources() does not, and callers like pci_host_probe()
> or acpi_pci_root_add() reach it without pci_rescan_remove_lock either
> (SR-IOV probe/remove paths also mutate bus->devices under pci_bus_sem).
> 
> pci_destroy_dev() unlinks dev->bus_list before acquiring resource_lock in
> pci_free_resources(), so resource_lock does not prevent list corruption or
> use-after-free.  Asserting lockdep_assert_held(&pci_bus_sem) here and
> taking down_read(&pci_bus_sem) in pci_bridge_release_resources() fixes it.
> 
> [ ... ]
> 
> > +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);
> > +}
> 
> [ ... ]
> 
> > +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;
> > +}
> 
> [Severity: Medium]
> Can holding write_lock(&resource_lock) across the pci_info() calls in
> pci_resource_post_release() and pci_can_release_resource_tree() stall other
> CPUs on slow consoles?
> 
> At default console_loglevel, LOGLEVEL_INFO messages flush synchronously on
> legacy framebuffer or serial consoles via console_unlock(), holding the
> spinning resource_lock with preemption disabled for tens to hundreds of
> milliseconds while other CPUs spin in ioremap() or /proc/iomem.
> 
> Previously, release_child_resources() only logged at KERN_DEBUG under
> resource_lock and pci_release_resource() called pci_info() after dropping
> it.
> 
> Also, unlike pci_can_release_resource_tree(), __pci_release_resource_tree()
> does not check res->child before entering pci_bus_for_each_window_resource(),
> scanning subordinate devices even when res has no children.
> 
> 

^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2026-12-05  1:57 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20261205003957.3304-1-ilpo.jarvinen@linux.intel.com>
2026-12-05  0:39 ` [PATCH v2 01/16] PCI: Release all resources before restoring them in BAR resize rollback Ilpo Järvinen
2026-12-05  0:39 ` [PATCH v2 02/16] PCI: Set new BAR Size only after releasing the BARs for BAR resize Ilpo Järvinen
2026-12-05  0:39 ` [PATCH v2 03/16] PCI: Abort BAR resize only if saving the resized BAR fails Ilpo Järvinen
2026-12-05  1:04   ` sashiko-bot
2026-12-05  1:25     ` Ilpo Järvinen
2026-12-05  0:39 ` [PATCH v2 04/16] PCI: Consistently use Header Type to detect bridge headers Ilpo Järvinen
2026-12-05  1:04   ` sashiko-bot
2026-12-05  0:39 ` [PATCH v2 09/16] PCI: Do not release resources with children in pci_release_resource() Ilpo Järvinen
2026-12-05  1:04   ` sashiko-bot
2026-12-05  1:29     ` Ilpo Järvinen
2026-12-05  0:39 ` [PATCH v2 13/16] PCI: Split pci_resource_post_release() out of pci_release_resource() Ilpo Järvinen
2026-12-05  0:39 ` [PATCH v2 14/16] PCI: Add iterators for devices and resources in bus windows Ilpo Järvinen
2026-12-05  1:02   ` sashiko-bot
2026-12-05  1:12     ` Ilpo Järvinen
2026-12-05  0:39 ` [PATCH v2 15/16] PCI: Keep the range of fixed resources when releasing them Ilpo Järvinen
2026-12-05  0:39 ` [PATCH v2 16/16] PCI: Release only PCI resources when releasing a resource tree Ilpo Järvinen
2026-12-05  1:11   ` sashiko-bot
2026-12-05  1:57     ` 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