Skip to contentSkip to conversation

PCI: release empty sibling bridge windows during window resize

PATCH Superseded Geramy Loveless 3 participants 4 messages Apr 28 – Jul 12 PCI

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 PCI: release empty sibling bridge windows during window resize

pbus_reassign_bridge_resources() refuses to release bridge windows that have child resources. On multi-port PCIe switches (e.g. Thunderbolt docks), empty sibling downstream ports hold small reservations that prevent the parent window from being freed and re-sized for rebar.

Walk descendants of the target window depth-first, saving and freeing each empty bridge window so the parent can then be released and grown.

Without this change, attempts to grow a window deep below a switch fabric leave nested empty windows pinning the ancestor:

pcieport 0000:65:00.0: bridge window [mem 0x8880000000-0x98800fffff
64bit pref]: not released, active children present
pcieport 0000:00:03.2: bridge window [mem 0x8880000000-0x98800fffff
64bit pref]: not released, active children present

With the bottom-up walk and upstream ancestry check, the chain unwinds and the rebar succeeds:

pcieport 0000:96:00.0: bridge window [...]: releasing pcieport 0000:95:00.0: bridge window [...]: releasing pcieport 0000:94:00.0: bridge window [...]: releasing pcieport 0000:65:00.0: bridge window [...]: releasing amdgpu 0000:97:00.0: BAR 0 [mem 0x9000000000-0x97ffffffff 64bit pref]:
assigned

---
v3:
- Restructure helper: outer loop only recurses; inner iterates bus resources via pci_bus_for_each_resource(). The release path is now unified, so the caller-side block in pbus_reassign_bridge_resources() is gone.
- Walk the resource tree upstream when checking ancestry of @b_win, so descendants in nested topologies are also released (Ilpo).
- Drop pci_resource_is_bridge_win() filter; bus resources are inherently bridge windows.
- Rename pci_bus_release_empty_bridge_resources() to pbus_release_empty_bridge_resources().
- Drop misleading "Uses PCI bus/device iterators..." paragraph from commit message.
- Kerneldoc tweaks per Ilpo's review.

v2:
- Use PCI bus/device iterators instead of walking raw resource tree.
- Save released resources to rollback list.
- Filter via pci_resource_is_bridge_win() (now removed in v3).
- Call release before the !res->child check.
- Check bridge->subordinate before recursion.

---
 drivers/pci/setup-bus.c | 63 +++++++++++++++++++++++++++++++++++++++--
 1 file changed, 61 insertions(+), 2 deletions(-)
drivers/pci/setup-bus.c +61 -2
index 4cf120ebe5a..0c1fb654c9c 100644
@@ -2292,6 +2292,60 @@ void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
 }
 EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
 
+/*
+ * pci_bus_release_empty_bridge_resources - Bottom-up release of bridge
+ * window resources in empty subtrees.
+ * @bus: PCI bus whose child bridges to process
+ * @b_win: Ancestor bridge window; only children of this resource are released
+ * @saved: List to save released resources for rollback
+ *
+ * Recurses depth-first into subordinate buses, then releases bridge windows
+ * on the way back up.  Each resource is individually saved before release so
+ * the entire operation can be rolled back.
+ */
+static void pci_bus_release_empty_bridge_resources(struct pci_bus *bus,
+						   struct resource *b_win,
+						   struct list_head *saved)
+{
+	struct pci_dev *dev;
+
+	list_for_each_entry(dev, &bus->devices, bus_list) {
+		struct resource *r;
+		unsigned int i;
+
+		if (!dev->subordinate)
+			continue;
+
+		/* Recurse first — release deepest resources before parents */
+		pci_bus_release_empty_bridge_resources(dev->subordinate,
+						      b_win, saved);
+
+		pci_dev_for_each_resource(dev, r, i) {
+			if (!pci_resource_is_bridge_win(i))
+				continue;
+
+			if (!resource_assigned(r))
+				continue;
+
+			if (r->parent != b_win)
+				continue;
+
+			if (r->child) {
+				const char *res_name = pci_resource_name(dev, i);
+
+				pci_info(dev, "%s %pR: not released, children still present\n",
+					 res_name, r);
+				continue;
+			}
+
+			if (pci_dev_res_add_to_list(saved, dev, r, 0, 0))
+				continue;
+
+			pci_release_resource(dev, i);
+		}
+	}
+}
+
 /*
  * Walk to the root bus, find the bridge window relevant for @res and
  * release it when possible. If the bridge window contains assigned
@@ -2316,7 +2370,12 @@ static int pbus_reassign_bridge_resources(struct pci_bus *bus, struct resource *
 
 		i = pci_resource_num(bridge, res);
 
-		/* Ignore BARs which are still in use */
+		/* Release empty sibling bridge windows bottom-up */
+		if (bridge->subordinate)
+			pci_bus_release_empty_bridge_resources(
+				bridge->subordinate, res, saved);
+
+		/* Ignore bridge windows which are still in use */
 		if (!res->child) {
 			ret = pci_dev_res_add_to_list(saved, bridge, res, 0, 0);
 			if (ret)
@@ -2327,7 +2386,7 @@ static int pbus_reassign_bridge_resources(struct pci_bus *bus, struct resource *
 			const char *res_name = pci_resource_name(bridge, i);
 
 			pci_warn(bridge,
-				 "%s %pR: was not released (still contains assigned resources)\n",
+				 "%s %pR: not released, active children present\n",
 				 res_name, res);
 		}
 
Signature · 1 line
2.43.0
#1 lore raw
Quoted 19 lines · Geramy Loveless

pbus_reassign_bridge_resources() refuses to release bridge windows that have child resources. On multi-port PCIe switches (e.g. Thunderbolt docks), empty sibling downstream ports hold small reservations that prevent the parent window from being freed and re-sized for rebar.

Walk descendants of the target window depth-first, saving and freeing each empty bridge window so the parent can then be released and grown.

Without this change, attempts to grow a window deep below a switch fabric leave nested empty windows pinning the ancestor:

pcieport 0000:65:00.0: bridge window [mem 0x8880000000-0x98800fffff
64bit pref]: not released, active children present
pcieport 0000:00:03.2: bridge window [mem 0x8880000000-0x98800fffff
64bit pref]: not released, active children present

With the bottom-up walk and upstream ancestry check, the chain unwinds and the rebar succeeds:

Quoted 6 lines

pcieport 0000:96:00.0: bridge window [...]: releasing pcieport 0000:95:00.0: bridge window [...]: releasing pcieport 0000:94:00.0: bridge window [...]: releasing pcieport 0000:65:00.0: bridge window [...]: releasing amdgpu 0000:97:00.0: BAR 0 [mem 0x9000000000-0x97ffffffff 64bit pref]:
assigned

That sounds reasonable and I can see the rollback store

But do we make sure that we can allocate all released child resources additional to the resized resource?

E.g. in the above example how is the new size of the bridge window determined? Does it include the released child resources so that they can be eventually be re-allocated when drivers start needing them?

Regards,
Christian.

Quoted 117 lines

---
v3:
- Restructure helper: outer loop only recurses; inner iterates bus resources via pci_bus_for_each_resource(). The release path is now unified, so the caller-side block in pbus_reassign_bridge_resources() is gone.
- Walk the resource tree upstream when checking ancestry of @b_win, so descendants in nested topologies are also released (Ilpo).
- Drop pci_resource_is_bridge_win() filter; bus resources are inherently bridge windows.
- Rename pci_bus_release_empty_bridge_resources() to pbus_release_empty_bridge_resources().
- Drop misleading "Uses PCI bus/device iterators..." paragraph from commit message.
- Kerneldoc tweaks per Ilpo's review.

v2:
- Use PCI bus/device iterators instead of walking raw resource tree.
- Save released resources to rollback list.
- Filter via pci_resource_is_bridge_win() (now removed in v3).
- Call release before the !res->child check.
- Check bridge->subordinate before recursion.

---
 drivers/pci/setup-bus.c | 63 +++++++++++++++++++++++++++++++++++++++--
 1 file changed, 61 insertions(+), 2 deletions(-)
drivers/pci/setup-bus.c +61 -2
index 4cf120ebe5a..0c1fb654c9c 100644
@@ -2292,6 +2292,60 @@ void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
 }
 EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
 
+/*
+ * pci_bus_release_empty_bridge_resources - Bottom-up release of bridge
+ * window resources in empty subtrees.
+ * @bus: PCI bus whose child bridges to process
+ * @b_win: Ancestor bridge window; only children of this resource are released
+ * @saved: List to save released resources for rollback
+ *
+ * Recurses depth-first into subordinate buses, then releases bridge windows
+ * on the way back up.  Each resource is individually saved before release so
+ * the entire operation can be rolled back.
+ */
+static void pci_bus_release_empty_bridge_resources(struct pci_bus *bus,
+						   struct resource *b_win,
+						   struct list_head *saved)
+{
+	struct pci_dev *dev;
+
+	list_for_each_entry(dev, &bus->devices, bus_list) {
+		struct resource *r;
+		unsigned int i;
+
+		if (!dev->subordinate)
+			continue;
+
+		/* Recurse first — release deepest resources before parents */
+		pci_bus_release_empty_bridge_resources(dev->subordinate,
+						      b_win, saved);
+
+		pci_dev_for_each_resource(dev, r, i) {
+			if (!pci_resource_is_bridge_win(i))
+				continue;
+
+			if (!resource_assigned(r))
+				continue;
+
+			if (r->parent != b_win)
+				continue;
+
+			if (r->child) {
+				const char *res_name = pci_resource_name(dev, i);
+
+				pci_info(dev, "%s %pR: not released, children still present\n",
+					 res_name, r);
+				continue;
+			}
+
+			if (pci_dev_res_add_to_list(saved, dev, r, 0, 0))
+				continue;
+
+			pci_release_resource(dev, i);
+		}
+	}
+}
+
 /*
  * Walk to the root bus, find the bridge window relevant for @res and
  * release it when possible. If the bridge window contains assigned
@@ -2316,7 +2370,12 @@ static int pbus_reassign_bridge_resources(struct pci_bus *bus, struct resource *
 
 		i = pci_resource_num(bridge, res);
 
-		/* Ignore BARs which are still in use */
+		/* Release empty sibling bridge windows bottom-up */
+		if (bridge->subordinate)
+			pci_bus_release_empty_bridge_resources(
+				bridge->subordinate, res, saved);
+
+		/* Ignore bridge windows which are still in use */
 		if (!res->child) {
 			ret = pci_dev_res_add_to_list(saved, bridge, res, 0, 0);
 			if (ret)
@@ -2327,7 +2386,7 @@ static int pbus_reassign_bridge_resources(struct pci_bus *bus, struct resource *
 			const char *res_name = pci_resource_name(bridge, i);
 
 			pci_warn(bridge,
-				 "%s %pR: was not released (still contains assigned resources)\n",
+				 "%s %pR: not released, active children present\n",
 				 res_name, res);
 		}
 
#2 lore raw
Quoted 27 lines · Geramy Loveless
Quoted 19 lines

pbus_reassign_bridge_resources() refuses to release bridge windows that have child resources. On multi-port PCIe switches (e.g. Thunderbolt docks), empty sibling downstream ports hold small reservations that prevent the parent window from being freed and re-sized for rebar.

Walk descendants of the target window depth-first, saving and freeing each empty bridge window so the parent can then be released and grown.

Without this change, attempts to grow a window deep below a switch fabric leave nested empty windows pinning the ancestor:

pcieport 0000:65:00.0: bridge window [mem 0x8880000000-0x98800fffff
64bit pref]: not released, active children present
pcieport 0000:00:03.2: bridge window [mem 0x8880000000-0x98800fffff
64bit pref]: not released, active children present

With the bottom-up walk and upstream ancestry check, the chain unwinds and the rebar succeeds:

Quoted 6 lines

pcieport 0000:96:00.0: bridge window [...]: releasing pcieport 0000:95:00.0: bridge window [...]: releasing pcieport 0000:94:00.0: bridge window [...]: releasing pcieport 0000:65:00.0: bridge window [...]: releasing amdgpu 0000:97:00.0: BAR 0 [mem 0x9000000000-0x97ffffffff 64bit pref]:
assigned

Quoted 3 lines

That sounds reasonable and I can see the rollback store

But do we make sure that we can allocate all released child resources additional to the resized resource?

I am not sure I will need to investigate to see if the other code in the kernel follows this path correctly, I would assume it does since the parent is being released by this code if there was no children, we are just releasing the children too if applicable essentially. Thats my thoughts they could be inaccurate though since i'm not extremely involved in linux kernel code until recently.

Quoted 2 lines

E.g. in the above example how is the new size of the bridge window determined? Does it include the released child resources so that they can be eventually be re-allocated when drivers start needing them?

This patch basically releases the children so the system that deals with allocating and changing window size can actually change it. So basically the only blocker to window resizing was this patch, whatever pcie already does in linux basically now can happen with the children released without further changes.
Does that make sense?

Basically our only job is to make sure to release the children and the rest of the linux kernel can continue forward and resize the window, the stopper was the children not getting released essentially.

Quoted 122 lines

Regards,
Christian.

Quoted 117 lines

---
v3:
- Restructure helper: outer loop only recurses; inner iterates bus resources via pci_bus_for_each_resource(). The release path is now unified, so the caller-side block in pbus_reassign_bridge_resources() is gone.
- Walk the resource tree upstream when checking ancestry of @b_win, so descendants in nested topologies are also released (Ilpo).
- Drop pci_resource_is_bridge_win() filter; bus resources are inherently bridge windows.
- Rename pci_bus_release_empty_bridge_resources() to pbus_release_empty_bridge_resources().
- Drop misleading "Uses PCI bus/device iterators..." paragraph from commit message.
- Kerneldoc tweaks per Ilpo's review.

v2:
- Use PCI bus/device iterators instead of walking raw resource tree.
- Save released resources to rollback list.
- Filter via pci_resource_is_bridge_win() (now removed in v3).
- Call release before the !res->child check.
- Check bridge->subordinate before recursion.

---
 drivers/pci/setup-bus.c | 63 +++++++++++++++++++++++++++++++++++++++--
 1 file changed, 61 insertions(+), 2 deletions(-)
drivers/pci/setup-bus.c +61 -2
index 4cf120ebe5a..0c1fb654c9c 100644
@@ -2292,6 +2292,60 @@ void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
 }
 EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
 
+/*
+ * pci_bus_release_empty_bridge_resources - Bottom-up release of bridge
+ * window resources in empty subtrees.
+ * @bus: PCI bus whose child bridges to process
+ * @b_win: Ancestor bridge window; only children of this resource are released
+ * @saved: List to save released resources for rollback
+ *
+ * Recurses depth-first into subordinate buses, then releases bridge windows
+ * on the way back up.  Each resource is individually saved before release so
+ * the entire operation can be rolled back.
+ */
+static void pci_bus_release_empty_bridge_resources(struct pci_bus *bus,
+                                                struct resource *b_win,
+                                                struct list_head *saved)
+{
+     struct pci_dev *dev;
+
+     list_for_each_entry(dev, &bus->devices, bus_list) {
+             struct resource *r;
+             unsigned int i;
+
+             if (!dev->subordinate)
+                     continue;
+
+             /* Recurse first — release deepest resources before parents */
+             pci_bus_release_empty_bridge_resources(dev->subordinate,
+                                                   b_win, saved);
+
+             pci_dev_for_each_resource(dev, r, i) {
+                     if (!pci_resource_is_bridge_win(i))
+                             continue;
+
+                     if (!resource_assigned(r))
+                             continue;
+
+                     if (r->parent != b_win)
+                             continue;
+
+                     if (r->child) {
+                             const char *res_name = pci_resource_name(dev, i);
+
+                             pci_info(dev, "%s %pR: not released, children still present\n",
+                                      res_name, r);
+                             continue;
+                     }
+
+                     if (pci_dev_res_add_to_list(saved, dev, r, 0, 0))
+                             continue;
+
+                     pci_release_resource(dev, i);
+             }
+     }
+}
+
 /*
  * Walk to the root bus, find the bridge window relevant for @res and
  * release it when possible. If the bridge window contains assigned
@@ -2316,7 +2370,12 @@ static int pbus_reassign_bridge_resources(struct pci_bus *bus, struct resource *
 
              i = pci_resource_num(bridge, res);
 
-             /* Ignore BARs which are still in use */
+             /* Release empty sibling bridge windows bottom-up */
+             if (bridge->subordinate)
+                     pci_bus_release_empty_bridge_resources(
+                             bridge->subordinate, res, saved);
+
+             /* Ignore bridge windows which are still in use */
              if (!res->child) {
                      ret = pci_dev_res_add_to_list(saved, bridge, res, 0, 0);
                      if (ret)
@@ -2327,7 +2386,7 @@ static int pbus_reassign_bridge_resources(struct pci_bus *bus, struct resource *
                      const char *res_name = pci_resource_name(bridge, i);
 
                      pci_warn(bridge,
-                              "%s %pR: was not released (still contains assigned resources)\n",
+                              "%s %pR: not released, active children present\n",
                               res_name, res);
              }
 
#3 lore raw

Any chance there's been any progress in this matter?

C

Quoted 222 lines · Geramy Loveless
Quoted 38 lines · Geramy Loveless
Quoted 29 lines

pbus_reassign_bridge_resources() refuses to release bridge windows that
have child resources. On multi-port PCIe switches (e.g. Thunderbolt
docks), empty sibling downstream ports hold small reservations that
prevent the parent window from being freed and re-sized for rebar.

Walk descendants of the target window depth-first, saving and freeing
each empty bridge window so the parent can then be released and grown.

Without this change, attempts to grow a window deep below a switch
fabric leave nested empty windows pinning the ancestor:

  pcieport 0000:65:00.0: bridge window [mem 0x8880000000- 0x98800fffff
    64bit pref]: not released, active children present pcieport 0000:00:03.2: bridge window [mem 0x8880000000- 0x98800fffff
    64bit pref]: not released, active children present

With the bottom-up walk and upstream ancestry check, the chain unwinds
and the rebar succeeds:

Quoted 7 lines

  pcieport 0000:96:00.0: bridge window [...]: releasing pcieport 0000:95:00.0: bridge window [...]: releasing pcieport 0000:94:00.0: bridge window [...]: releasing pcieport 0000:65:00.0: bridge window [...]: releasing amdgpu 0000:97:00.0: BAR 0 [mem 0x9000000000-0x97ffffffff 64bit pref]:
    assigned

Quoted 4 lines

That sounds reasonable and I can see the rollback store

But do we make sure that we can allocate all released child resources additional to the resized resource?

I am not sure I will need to investigate to see if the other code in the kernel follows this path correctly, I would assume it does since the parent is being released by this code if there was no children, we
are just releasing the children too if applicable essentially. Thats my thoughts they could be inaccurate though since i'm not extremely involved in linux kernel code until recently.

Quoted 5 lines

E.g. in the above example how is the new size of the bridge window determined? Does it include the released child resources so that they can be eventually be re-allocated when drivers start needing them?

This patch basically releases the children so the system that deals with allocating and changing window size can actually change it. So basically the only blocker to window resizing was this patch, whatever pcie already does in linux basically now can happen with the children released without further changes.
Does that make sense?

Basically our only job is to make sure to release the children and the
rest of the linux kernel can continue forward and resize the window, the stopper was the children not getting released essentially.

Quoted 154 lines

Regards,
Christian.

Quoted 149 lines

---
v3:
 - Restructure helper: outer loop only recurses; inner iterates bus
   resources via pci_bus_for_each_resource(). The release path is now
   unified, so the caller-side block in pbus_reassign_bridge_resources()
   is gone.
 - Walk the resource tree upstream when checking ancestry of @b_win,
   so descendants in nested topologies are also released (Ilpo).
 - Drop pci_resource_is_bridge_win() filter; bus resources are inherently bridge windows.
 - Rename pci_bus_release_empty_bridge_resources() to pbus_release_empty_bridge_resources().
 - Drop misleading "Uses PCI bus/device iterators..." paragraph from
   commit message.
 - Kerneldoc tweaks per Ilpo's review.

v2:
 - Use PCI bus/device iterators instead of walking raw resource tree.
 - Save released resources to rollback list.
 - Filter via pci_resource_is_bridge_win() (now removed in v3).
 - Call release before the !res->child check.
 - Check bridge->subordinate before recursion.
---
 drivers/pci/setup-bus.c | 63
+++++++++++++++++++++++++++++++++++++++--

 1 file changed, 61 insertions(+), 2 deletions(-)
drivers/pci/setup-bus.c +2 -0
index 4cf120ebe5a..0c1fb654c9c 100644
@@ -2292,6 +2292,60 @@ void
 pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
  }
  EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
 
+/*
+ * pci_bus_release_empty_bridge_resources - Bottom-up release of

bridge
+ * window resources in empty subtrees.
+ * @bus: PCI bus whose child bridges to process
+ * @b_win: Ancestor bridge window; only children of this resource are released
+ * @saved: List to save released resources for rollback
+ *
+ * Recurses depth-first into subordinate buses, then releases bridge windows
+ * on the way back up.  Each resource is individually saved before release so
+ * the entire operation can be rolled back.
+ */
+static void pci_bus_release_empty_bridge_resources(struct pci_bus *bus,
+                                                struct resource *b_win,
+                                                struct list_head *saved)

drivers/pci/setup-bus.c +11 -0
+{
+     struct pci_dev *dev;
+
+     list_for_each_entry(dev, &bus->devices, bus_list) {
+             struct resource *r;
+             unsigned int i;
+
+             if (!dev->subordinate)
+                     continue;
+
+             /* Recurse first — release deepest resources before

parents */
+             pci_bus_release_empty_bridge_resources(dev-

Quoted 1 line

subordinate,

+                                                   b_win, saved);

drivers/pci/setup-bus.c +13 -0
+
+             pci_dev_for_each_resource(dev, r, i) {
+                     if (!pci_resource_is_bridge_win(i))
+                             continue;
+
+                     if (!resource_assigned(r))
+                             continue;
+
+                     if (r->parent != b_win)
+                             continue;
+
+                     if (r->child) {
+                             const char *res_name =

pci_resource_name(dev, i);
+
+                             pci_info(dev, "%s %pR: not released, children still present\n",
+                                      res_name, r);
+                             continue;
+                     }
+
+                     if (pci_dev_res_add_to_list(saved, dev, r, 0, 0))
+                             continue;

drivers/pci/setup-bus.c +6 -0
+
+                     pci_release_resource(dev, i);
+             }
+     }
+}
+

 /*
  * Walk to the root bus, find the bridge window relevant for @res and
  * release it when possible. If the bridge window contains assigned

drivers/pci/setup-bus.c +0 -0
@@ -2316,7 +2370,12 @@ static int

pbus_reassign_bridge_resources(struct pci_bus *bus, struct resource *

              i = pci_resource_num(bridge, res);

-             /* Ignore BARs which are still in use */ +             /* Release empty sibling bridge windows bottom-up */
+             if (bridge->subordinate)
+                     pci_bus_release_empty_bridge_resources( +                             bridge->subordinate, res, saved); +
+             /* Ignore bridge windows which are still in use */ if (!res->child) {
                      ret = pci_dev_res_add_to_list(saved, bridge, res, 0, 0);
                      if (ret)

drivers/pci/setup-bus.c +0 -0
@@ -2327,7 +2386,7 @@ static int

pbus_reassign_bridge_resources(struct pci_bus *bus, struct resource *
                      const char *res_name = pci_resource_name(bridge, i);

                      pci_warn(bridge,
-                              "%s %pR: was not released (still contains assigned resources)\n",
+                              "%s %pR: not released, active children present\n",
                               res_name, res);
              }