* [PATCH v2] PCI: Don't move a window's start when it holds a non-power-of-2 resource
@ 2026-12-02 12:06 Greg Lamberson
2026-12-02 12:27 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Greg Lamberson @ 2026-12-02 12:06 UTC (permalink / raw)
To: Bjorn Helgaas; +Cc: Ilpo Järvinen, linux-pci, linux-kernel, regressions
pci_align_resource() moves a bridge window's start down by the part of
its size that isn't a multiple of its alignment, so the smaller
resources can fill the space before the first aligned address. That
relies on every resource below the window having a power-of-2 size. An
IOV resource is the VF BAR size times TotalVFs, which isn't a power of 2
unless TotalVFs is.
On an Intel Arc Pro B60, BAR 2 is 32 GiB and each of the seven VF BAR 2s
is 8 GiB, so the window needs 88 GiB plus 128 MiB, aligned to 32 GiB.
Its start is moved down by 24 GiB plus 128 MiB, BAR 2 is placed at the
lowest 32 GiB boundary in the window, and the 56 GiB IOV resource then
fits neither below nor above it. Since v7.1, enabling SR-IOV fails:
xe 0000:03:00.0: not enough MMIO resources for SR-IOV
xe 0000:03:00.0: [drm] PF: Failed to enable 5 VFs (-ENOMEM)
Keep the aligned start for a window that holds a resource whose size
isn't a power of 2, directly or behind child bridges.
Fixes: 9036bd0efcb6 ("PCI: Align head space better")
Cc: stable@vger.kernel.org # v7.1+
Signed-off-by: Greg Lamberson <greg@lamco.io>
---
Changes in v2:
- Look behind a child bridge window even when its size is still 0. A
window that holds only optional resources, such as SR-IOV BARs, is
sized 0 until its own bus is assigned, and v1 skipped it.
- Test the size as a resource_size_t. is_power_of_2() takes an unsigned
long, which truncates a 64-bit size on 32-bit kernels with
CONFIG_PHYS_ADDR_T_64BIT.
Both came from the Sashiko review of v1. It also pointed out that a
child bridge window whose total size isn't a power of 2 can meet the
same pattern. I've left that case alone: Most windows that hold more
than one device aren't a power of 2, and keeping the aligned start for
all of them would undo what 9036bd0efcb6 is for, which its message says
is sometimes required. Ilpo, you'll know better than I do whether that
case needs handling.
v1: https://lore.kernel.org/r/179619168710.49678.17788537403657229665@lamco.io
I tested v1 on Proxmox's 7.3.5 kernel with the patch applied, on an
AMD Raphael board with the B60 behind the card's own switch. With the
patch, VF BAR 2 is assigned at the same address v7.0 gives it and all
five VFs come up. Without it, the same kernel logs the two lines above.
The v2 changes don't alter what happens on that machine, and I haven't
booted v2 there. v2 applies cleanly to pci/next and mainline, and
setup-res.c builds with W=1 without warnings for x86_64 and for i386
with X86_PAE.
I used Claude (claude-opus-5-5) to trace the cause and write this
patch. I've reviewed the change and tested it on the affected machine.
drivers/pci/setup-res.c | 61 +++++++++++++++++++++++++++++++++++++++++
1 file changed, 61 insertions(+)
diff --git a/drivers/pci/setup-res.c b/drivers/pci/setup-res.c
index 376f09630a4a..5c86a680db89 100644
--- a/drivers/pci/setup-res.c
+++ b/drivers/pci/setup-res.c
@@ -260,10 +260,60 @@ resource_size_t pci_resource_alignment(const struct pci_dev *dev,
return resource_alignment(res);
}
+/*
+ * Whether @win of @bus->self holds, directly or behind child bridges, a
+ * resource whose size isn't a power of 2. An IOV resource is the VF BAR size
+ * times TotalVFs, so it's a power of 2 only when TotalVFs is.
+ */
+static bool pbus_window_holds_npot(struct pci_bus *bus,
+ const struct resource *win)
+{
+ struct pci_dev *dev;
+
+ list_for_each_entry(dev, &bus->devices, bus_list) {
+ struct resource *r;
+ int i;
+
+ pci_dev_for_each_resource(dev, r, i) {
+ resource_size_t size;
+
+ if (!r->flags || (r->flags & IORESOURCE_DISABLED))
+ continue;
+ if (pbus_select_window(bus, r) != win)
+ continue;
+ /*
+ * A child window that holds only optional resources,
+ * such as SR-IOV BARs, has size 0 until its own bus is
+ * assigned, so look behind it whatever its size.
+ */
+ if (pci_resource_is_bridge_win(i)) {
+ if (dev->subordinate &&
+ pbus_window_holds_npot(dev->subordinate, r))
+ return true;
+ continue;
+ }
+ /*
+ * is_power_of_2() takes an unsigned long, which would
+ * truncate a 64-bit resource_size_t on 32-bit kernels.
+ */
+ size = resource_size(r);
+ if (size & (size - 1))
+ return true;
+ }
+ }
+
+ return false;
+}
+
/*
* For mem bridge windows, try to relocate tail remainder space to space
* before res->start if there's enough free space there. This enables
* tighter packing for resources.
+ *
+ * The head can only be filled when every resource below the window has a
+ * power-of-2 size. A resource that isn't one may be larger than the head and
+ * still need its own alignment, so a window holding one keeps its aligned
+ * start.
*/
resource_size_t pci_align_resource(struct pci_dev *dev,
const struct resource *res,
@@ -290,6 +340,17 @@ resource_size_t pci_align_resource(struct pci_dev *dev,
if (empty_res->start > start_addr)
return res->start;
+ /*
+ * @res is the allocator's copy of the window, so find the window itself
+ * among the bridge's resources by type.
+ */
+ if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE && dev->subordinate &&
+ pbus_window_holds_npot(dev->subordinate,
+ pci_resource_n(dev, res->flags & IORESOURCE_PREFETCH ?
+ PCI_BRIDGE_PREF_MEM_WINDOW :
+ PCI_BRIDGE_MEM_WINDOW)))
+ return res->start;
+
pci_dbg(dev, "%pR: moving candidate start address below align to %llx\n",
res, (unsigned long long)start_addr);
return start_addr;
base-commit: 647e1e846730c6aad27e9b858df292f5d4377f63
--
2.47.3
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v2] PCI: Don't move a window's start when it holds a non-power-of-2 resource
2026-12-02 12:06 [PATCH v2] PCI: Don't move a window's start when it holds a non-power-of-2 resource Greg Lamberson
@ 2026-12-02 12:27 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-02 12:27 UTC (permalink / raw)
To: Greg Lamberson; +Cc: linux-pci
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] PCI: pbus_window_holds_npot() ignores non-power-of-2 child
bridge window size
--
commit eb9639b607c547d4e76987d498e27e3b03a3d942
Author: Greg Lamberson <greg@lamco.io>
PCI: Don't move a window's start when it holds a non-power-of-2 resource
This commit keeps the aligned start address in pci_align_resource() for a
bridge window that holds a resource with a non-power-of-2 size directly
or behind child bridges.
> diff --git a/drivers/pci/setup-res.c b/drivers/pci/setup-res.c
> index 376f09630a4ab..5c86a680db89f 100644
> --- a/drivers/pci/setup-res.c
> +++ b/drivers/pci/setup-res.c
> @@ -260,10 +260,60 @@ resource_size_t pci_resource_alignment(const struct pci_dev *dev,
> return resource_alignment(res);
> }
>
> +/*
> + * Whether @win of @bus->self holds, directly or behind child bridges, a
> + * resource whose size isn't a power of 2. An IOV resource is the VF BAR size
> + * times TotalVFs, so it's a power of 2 only when TotalVFs is.
> + */
> +static bool pbus_window_holds_npot(struct pci_bus *bus,
> + const struct resource *win)
> +{
> + struct pci_dev *dev;
> +
> + list_for_each_entry(dev, &bus->devices, bus_list) {
> + struct resource *r;
> + int i;
> +
> + pci_dev_for_each_resource(dev, r, i) {
> + resource_size_t size;
> +
> + if (!r->flags || (r->flags & IORESOURCE_DISABLED))
> + continue;
> + if (pbus_select_window(bus, r) != win)
> + continue;
> + /*
> + * A child window that holds only optional resources,
> + * such as SR-IOV BARs, has size 0 until its own bus is
> + * assigned, so look behind it whatever its size.
> + */
> + if (pci_resource_is_bridge_win(i)) {
> + if (dev->subordinate &&
> + pbus_window_holds_npot(dev->subordinate, r))
> + return true;
> + continue;
> + }
> + /*
> + * is_power_of_2() takes an unsigned long, which would
> + * truncate a 64-bit resource_size_t on 32-bit kernels.
> + */
> + size = resource_size(r);
> + if (size & (size - 1))
> + return true;
> + }
> + }
[Severity: Medium]
Does skipping the size & (size - 1) check on a child bridge window r when
pci_resource_is_bridge_win(i) is true leave the parent window susceptible
to the same splitting failure?
A child bridge window is allocated as a single contiguous resource inside
the parent window, and its total size need not be a power of 2 even when
every leaf BAR behind it is (for example, two 16 GiB BARs and an 8 GiB
BAR give a 40 GiB child window aligned to 8 GiB). Because r_size > align
for that child window, pbus_size_mem() excludes it from aligns[]:
drivers/pci/setup-bus.c:pbus_size_mem() {
...
if (r_size <= align)
aligns[order] += align;
...
}
If the parent bus also has a 32 GiB sibling BAR, the parent window is
sized to 72 GiB aligned to 32 GiB. When pbus_window_holds_npot() returns
false for the parent window, pci_align_resource() shifts the parent start
down by 8 GiB, and placing the 32 GiB sibling BAR at the first 32 GiB
boundary splits the parent window into 8 GiB head and 32 GiB tail
segments, neither of which can fit the 40 GiB child bridge window.
Note that checking size & (size - 1) on child bridge windows inside
pbus_window_holds_npot() only constrains parent windows containing a
child bridge window; a leaf bridge window is not a child of itself, so
relocating a leaf window that holds multiple power-of-2 BARs remains
unaffected.
--
Sashiko AI review · https://sashiko.dev/#/patchset/179621321433.1105012.12699444202985653339@lamco.io?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-02 12:27 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 12:06 [PATCH v2] PCI: Don't move a window's start when it holds a non-power-of-2 resource Greg Lamberson
2026-12-02 12:27 ` sashiko-bot
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.