Skip to contentSkip to conversation

PCI: Reserve prefetchable window headroom for Resizable BARs

PATCH In review Geramy Loveless 3 participants 3 messages Aug 29 – Nov 15

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 PCI: Reserve prefetchable window headroom for Resizable BARs

Firmware typically sizes prefetchable bridge windows for the boot-time BAR size. Behind a fixed (non-hotplug) PCIe switch fabric, there is then no room for a driver to grow a Resizable BAR afterwards: every window from the leaf up to the root is sized for the small BAR, so pci_resize_resource() fails with -ENOSPC.

Furthermore, a small prefetchable BAR (e.g., a 2 MiB doorbell) sharing a bridge's single prefetchable window with a much larger one (e.g., a 32 GiB VRAM BAR) pushes the required window size past the large BAR's alignment. Because bridge windows round up to a power of two, this forces a massive alignment waste (e.g., 32 GiB + 2 MiB rounds up to a 64 GiB window, wasting ~32 GiB per GPU).

This patch solves both issues to enable ReBAR on cascaded switch fabrics:

1. Reserve Headroom:
Reserve prefetchable window headroom for the maximum size of each downstream Resizable BAR during the bridge sizing pass. The device BAR and hardware ReBAR are left at their boot size to prevent tearing down firmware-loaded state (e.g., AMD R9700 PSP) before the driver binds.

2. Pack Oversized Windows (BAR Demotion):
The PCI-to-PCI Bridge spec (r1.2, sec 3.2.5) permits a prefetchable BAR to be assigned from the non-prefetchable window. By clearing the PREFETCH flag on small control BARs during enumeration, they are placed below 4 GiB. The prefetchable window is then cleanly sized to the large BAR alone.

Why this is safe for 32-bit (< 4G) MMIO space:
To prevent exhausting legacy < 4G space, demotion is strictly bounded. It only triggers beside a massive prefetchable BAR (>= 64 MiB), and the demoted BAR is strictly capped at 16 MiB (PCI_BAR_PACK_CAP). Even on an 8x GPU system, this consumes at most 128 MiB of < 4G space.

Fallback / Alignment logic:
If the 32-bit space is exhausted, small BARs remain in the prefetchable window. The alignment logic gracefully handles this by rounding the bridge window up to the next power of two (e.g., growing to 64 GiB to fit a 32 GiB BAR + BAR2) to maintain PCIe compatibility, ensuring sibling GPUs land in properly aligned slots.

Reserving is safe by default: __assign_resources_sorted() satisfies all required resources first. Use pci=no_rebar_reserve and pci=no_bar_demote to restore previous behaviours.

Testing Context:
- ASUS K14PG-D24
- 2x EPYC 9354
- 8x Radeon AI PRO R9700 (Navi 48, 1002:7551)
- Broadcom PEX890xx switches
- Kernel base: 45c13f3f9e3b (tested with SEV-SNP passthrough=1)

---
 .../admin-guide/kernel-parameters.txt         |   9 ++
 drivers/pci/pci.c                             |  15 ++
 drivers/pci/pci.h                             |   2 +
 drivers/pci/quirks.c                          |  89 +++++++++++
 drivers/pci/setup-bus.c                       | 142 ++++++++++++++++++
 5 files changed, 257 insertions(+)
Documentation/admin-guide/kernel-parameters.txt +9 -0
index 37006fc3eac3..7360407dd4b8 100644
@@ -5246,6 +5246,15 @@ Kernel parameters
 		hpbussize=nn	The minimum amount of additional bus numbers
 				reserved for buses below a hotplug bridge.
 				Default is 1.
+		no_bar_demote	Do not demote small prefetchable BARs below 4 GiB to pack oversized prefetchable bridge windows.
+		no_rebar_reserve	Do not reserve prefetchable bridge-window
+				space for the maximum size of downstream Resizable
+				BARs. By default such space is reserved so a driver
+				can grow a BAR later (e.g. GPU VRAM BAR) even behind
+				fixed PCIe switch fabrics that firmware sized for the
+				boot-time BAR; the device BAR is left at its boot size
+				for the driver to resize and only bridge windows are
+				enlarged. Use this to restore the old behaviour.
 		realloc=	Enable/disable reallocating PCI bridge resources
 				if allocations done by BIOS are too small to
 				accommodate resources required by all child
drivers/pci/pci.c +15 -0
index b2879a6be5f8..0bb4ad570171 100644
@@ -120,6 +120,17 @@ unsigned long pci_hotplug_mmio_pref_size = DEFAULT_HOTPLUG_MMIO_PREF_SIZE;
 #define DEFAULT_HOTPLUG_BUS_SIZE	1
 unsigned long pci_hotplug_bus_size = DEFAULT_HOTPLUG_BUS_SIZE;
 
+/*
+ * Prefetchable bridge-window headroom is reserved for the maximum size of
+ * downstream Resizable BARs by default, so a driver can grow one later
+ * (e.g. amdgpu VRAM BAR) even behind fixed PCIe switch fabrics.
+ * pci=no_rebar_reserve disables this.
+ */
+bool pci_rebar_no_reserve;
+
+/* pci=no_bar_demote disables demoting small prefetchable BARs below 4 GiB. */
+bool pci_bar_demote_disabled;
+
 
 /* PCIe MPS/MRRS strategy; can be overridden by kernel command-line param */
 enum pcie_bus_config_types pcie_bus_config = PCIE_BUS_DEFAULT;
@@ -6840,6 +6851,10 @@ static int __init pci_setup(char *str)
 					simple_strtoul(str + 10, &str, 0);
 				if (pci_hotplug_bus_size > 0xff)
 					pci_hotplug_bus_size = DEFAULT_HOTPLUG_BUS_SIZE;
+			} else if (!strncmp(str, "no_rebar_reserve", 16)) {
+				pci_rebar_no_reserve = true;
+			} else if (!strncmp(str, "no_bar_demote", 13)) {
+				pci_bar_demote_disabled = true;
 			} else if (!strncmp(str, "pcie_bus_tune_off", 17)) {
 				pcie_bus_config = PCIE_BUS_TUNE_OFF;
 			} else if (!strncmp(str, "pcie_bus_safe", 13)) {
drivers/pci/pci.h +2 -0
index ba3c3fddddc2..0f21972b674a 100644
@@ -409,6 +409,8 @@ extern unsigned long pci_hotplug_io_size;
 extern unsigned long pci_hotplug_mmio_size;
 extern unsigned long pci_hotplug_mmio_pref_size;
 extern unsigned long pci_hotplug_bus_size;
+extern bool pci_rebar_no_reserve;
+extern bool pci_bar_demote_disabled;
 
 static inline bool pci_is_cardbus_bridge(struct pci_dev *dev)
 {
drivers/pci/quirks.c +89 -0

92 lines not on this page · raw message

drivers/pci/setup-bus.c +142 -0

187 lines not on this page · raw message

base-commit: 45c13f3f9e3bb15fd89ff2864c6f627a3b4b4229

Signature · 1 line
2.53.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] The `resource_size_roundup_pow2` function invokes undefined behavior on 32-bit systems by shifting…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] The resource_size_roundup_pow2 function invokes undefined behavior on 32-bit systems by shifting a 32-bit integer by 32 or more.
- [High] Concurrent execution of pci_pack_low_reserve during PCIe hotplug or asynchronous probing causes data races on the static budget array, leading to memory corruption.
- [Medium] The pci_pack_low_reserve function permanently leaks budget state and stores stale root bus pointers across device unbinds/hot-removals. --

commit 3d18f0cac0d66de7c502abb13e0bec070d238ae4
Author: Geramy Loveless <[email protected]>

PCI: Reserve prefetchable window headroom for Resizable BARs

This commit reserves prefetchable window headroom during bridge sizing for the maximum size of downstream Resizable BARs, allowing a driver to grow a Resizable BAR later. It also demotes small prefetchable BARs to the non-prefetchable window when sharing with a massive prefetchable BAR.

Quoted 4 lines
drivers/pci/quirks.c +0 -0
index de9bbccda21fd..1094f618bb1ff 100644

[ ... ]

Quoted 10 lines
drivers/pci/quirks.c +10 -0
+/* Demote only if the host's 32-bit aperture has room (tracked per host). */
+static bool pci_pack_low_reserve(struct pci_dev *dev, resource_size_t bytes)
+{
+	static struct { struct pci_bus *root; u64 used; } budget[32];
+	static int nbudget;
+	struct pci_bus *root = dev->bus;
+	struct resource *r;
+	u64 aperture = 0, *usedp = NULL;
+	int i;
+

[ ... ]

drivers/pci/quirks.c static void pci_mask_replay_timer_timeout(struct pci_dev *pdev) · line 6458

Quoted 2 lines
drivers/pci/quirks.c +2 -0
+	for (i = 0; i < nbudget; i++) {
+		if (budget[i].root == root) {

[Severity: Medium]
If a root bus is hot-removed, could its memory address be reallocated to a new root bus, causing false matches here and inheriting the exhausted budget from the stale pointer?

drivers/pci/quirks.c static void pci_mask_replay_timer_timeout(struct pci_dev *pdev) · line 6469

Quoted 11 lines
drivers/pci/quirks.c +11 -0
+			usedp = &budget[i].used;
+			break;
+		}
+	}
+	if (!usedp) {
+		if (nbudget >= (int)ARRAY_SIZE(budget))
+			return false;
+		budget[nbudget].root = root;
+		budget[nbudget].used = 0;
+		usedp = &budget[nbudget].used;
+		nbudget++;

[Severity: High]
Could concurrent execution of pci_pack_low_reserve() during PCIe hotplug or asynchronous probing cause data races here?

There are no synchronization primitives protecting the static budget array and nbudget counter. If two concurrent threads attempt to insert a new root bus when nbudget reaches the array size limit, could both bypass the bounds check in a Time-of-Check to Time-of-Use (TOCTOU) race and cause an out-of-bounds write, corrupting the kernel BSS section?

drivers/pci/quirks.c static void pci_mask_replay_timer_timeout(struct pci_dev *pdev) · line 6473

Quoted 4 lines
drivers/pci/quirks.c +4 -0
+	}
+	if (*usedp + bytes > aperture / 2)
+		return false;
+	*usedp += bytes;

[Severity: Medium]
Since there is no teardown or release hook to decrement the usage when a device is removed, does this continuously accumulate the budget usage?

If a device is repeatedly hot-unplugged and re-plugged, or if a software bus rescan is triggered via sysfs, does this permanently leak the tracked budget until a reboot?

Quoted 2 lines
drivers/pci/quirks.c +2 -0
+	return true;
+}

[ ... ]

Quoted 4 lines
drivers/pci/setup-bus.c +0 -0
index e8c94aa1d3c12..d8b8cb35b8bb6 100644

[ ... ]

drivers/pci/setup-bus.c static bool pbus_size_mem_optional(struct pci_dev *dev, int resno, · line 1286

Quoted 5 lines
drivers/pci/setup-bus.c +5 -0
+/* roundup_pow_of_two() is unsigned long and truncates 64-bit sizes on 32-bit. */
+static resource_size_t resource_size_roundup_pow2(resource_size_t n)
+{
+	if (n && (n & (n - 1)))
+		n = (resource_size_t)1 << fls64(n);

[Severity: High]
Does this shift operation in resource_size_roundup_pow2() invoke undefined behavior on 32-bit systems?

If the requested bridge window size n is greater than 2GB (e.g. a 2GB BAR plus an additional smaller BAR) on a system where resource_size_t is 32-bit, fls64(n) will return 32. Since 1 is cast to a 32-bit type (resource_size_t), shifting it by 32 or more results in undefined behavior in C.

Quoted 2 lines
drivers/pci/setup-bus.c +2 -0
+	return n;
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Tested-by on v3 1/1
#2 lore raw
In reply to Geramy Loveless

Hi Geramy,

Quoted 3 lines · Geramy Loveless

Firmware typically sizes prefetchable bridge windows for the boot-time BAR size. Behind a fixed (non-hotplug) PCIe switch fabric, there is then no room for a driver to grow a Resizable BAR afterwards

I've been running v3 since 27 October on a different kind of fabric from yours, a USB4/Thunderbolt eGPU, and it gives the GPU its full BAR there too. In case the data point is useful:

Host: Minisforum UM890 Pro (AMD Ryzen 9 8945HS, Hawk Point),

         BIOS 1.06
  Dock:  Minisforum DEG2 (Intel JHL9480 Barlow Ridge TB5 switch) over
         USB4, link 2 x 10 Gb/s

GPU: Intel Arc B580 12 GB, xe driver
Kernel: 7.3.4 with v3 applied (it applies cleanly)

The firmware gives the USB4 root port 0000:00:03.1 a 64 GiB prefetchable window, but the tunnel is enumerated as a hotplug event about 12 seconds after boot, and without the patch the switch windows below it were sized at 256 MiB, so the B580's BAR2 stayed at 256 MiB and a manual resize failed.

With v3 the headroom is reserved down the chain:

pci 0000:6a:01.0: bridge window [mem 0x400000000-0x40fffffff 64bit pref] to [bus 6b] add_size 3f0000000 add_align 400000000 pci 0000:69:00.0: bridge window [mem 0x400000000-0x7ffffffff 64bit pref] to [bus 6a-6c] add_size 3f0000000 add_align 400000000

and xe then resizes the BAR when it binds:

xe 0000:6b:00.0: [drm] Attempting to resize bar from 256MiB -> 16384MiB xe 0000:6b:00.0: [drm] BAR2 resized to 16384MiB

Both bridges above the GPU (69:00.0 and 6a:01.0) end up with 16 GiB prefetchable windows, and the whole 12 GB of VRAM is CPU-visible. It has been stable as a daily VAAPI/compute card since then. On 31 October I also unplugged and re-plugged the dock while the system was running: it re-enumerated, xe re-bound and BAR2 came back at 16 GiB.

One interaction that may be worth knowing about: with pci=hpmmioprefsize=16G also on the command line, the TB5 switch's three empty downstream (daisy-chain) ports each reserved 16 GiB as well. That overflowed the 64 GiB root-port window, and the B580's BAR2 was briefly assigned at 16 GiB and then rolled back with -ENOSPC. Without that parameter it works as described above.

Thanks for the patch,
James