PCI: Solve two bridge window sizing issues
v3 cover PCI: Solve two bridge window sizing issues
Hi all,
Here's a series that contains two fixes to PCI bridge window sizing algorithm. Together, they should enable remove & rescan cycle to work for a PCI bus that has PCI devices with optional resources and/or disparity in BAR sizes.
For the second fix, I chose to expose find_resource_space() from kernel/resource.c because it should increase accuracy of the cannot-fit decision (currently that function is called find_resource()). In order to do that sensibly, a few improvements seemed in order to make its interface and name of the function sane before exposing it. Thus, the few extra patches on resource side.
v3:
- Removed "slot" wording
- Renamed find_empty_resource_slot() -> find_resource_space()
- find_resource_space() returns bool instead of int
- Added patch to convert literal 20 related to bridge win minimum alignment to __ffs(SZ_1M)
- Fixed kerneldoc missing "struct"
- Tweaked prints (one dbg -> info, added new dbg one for success case)
- Changelog tweaks
- Take account largest >> 1 (in alignment calc)
- Adjust to minor changes made into calculate_memsize()
- Take logs from more recent kernel to get rid of reg 0xXX
v2:
- Add "typedef" to kerneldoc to get correct formatting
- Use RESOURCE_SIZE_MAX instead of literal
- Remove unnecessary checks for io{port/mem}_resource
- Apply a few style tweaks from Andy
Ilpo Järvinen (8):
PCI: Fix resource double counting on remove & rescan
resource: Rename find_resource() to find_resource_space()
resource: Document find_resource_space() and resource_constraint
resource: Use typedef for alignf callback
resource: Handle simple alignment inside __find_resource_space()
resource: Export find_resource_space()
PCI: Make minimum bridge window alignment reference more obvious
PCI: Relax bridge window tail sizing rules
drivers/pci/bus.c | 10 +---- drivers/pci/setup-bus.c | 91 +++++++++++++++++++++++++++++++++++++---- include/linux/ioport.h | 44 ++++++++++++++++++-- include/linux/pci.h | 5 +-- kernel/resource.c | 68 ++++++++++++++---------------- 5 files changed, 157 insertions(+), 61 deletions(-)
Signature · 1 line
2.39.2
PATCH v3 1/8 PCI: Fix resource double counting on remove & rescan
pbus_size_mem() keeps the size of the optional resources in children_add_size. When calculating the PCI bridge window size, calculate_memsize() lower bounds size by old_size before adding children_add_size and performing the window size alignment. This results in double counting for the resources in children_add_size because old_size may be based on the previous size of the bridge window after it has already included children_add_size (that is, size1 in pbus_size_mem() from an earlier invocation of that function).
As a result, on repeated remove of the bus & rescan cycles the resource size keeps increasing when children_add_size is non-zero as can be seen from this extract:
iomem0: 23fffd00000-23fffdfffff : PCI Bus 0000:03 iomem1: 20000000000-200001fffff : PCI Bus 0000:03 iomem2: 20000000000-200002fffff : PCI Bus 0000:03 iomem3: 20000000000-200003fffff : PCI Bus 0000:03 iomem4: 20000000000-200004fffff : PCI Bus 0000:03
Solve the double counting by moving old_size check later in calculate_memsize() so that children_add_size is already accounted for.
After the patch, the bridge window retains its size as expected:
iomem0: 23fffd00000-23fffdfffff : PCI Bus 0000:03 iomem1: 20000000000-200000fffff : PCI Bus 0000:03 iomem2: 20000000000-200000fffff : PCI Bus 0000:03
--- drivers/pci/setup-bus.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/pci/setup-bus.c +2 -4
@@ -829,11 +829,9 @@ static resource_size_t calculate_memsize(resource_size_t size, size = min_size;if (old_size ==1 ) old_size =0 ; -if (size < old_size) - size = old_size; - size =ALIGN (max (size, add_size) + children_add_size, align); -return size; + size =max (size, add_size) + children_add_size; +return ALIGN (max (size, old_size), align); } resource_size_t __weak pcibios_window_alignment(struct pci_bus *bus,
Signature · 1 line
2.39.2
PATCH v3 2/8 resource: Rename find_resource() to find_resource_space()
In reply to Ilpo JärvinenRename find_resource() to find_resource_space() to better describe what the functions does. This is a preparation for exposing it beyond resource.c which is needed by PCI core. Also rename the __ variant to match the names.
--- kernel/resource.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-)
kernel/resource.c +11 -12
@@ -628,13 +628,12 @@ static void resource_clip(struct resource *res, resource_size_t min, }/* -* Find empty slot in the resource tree with the given range and +* Find empty space in the resource tree with the given range and * alignment constraints */ -static int __find_resource (struct resource *root,struct resource *old, -struct resource *new, -resource_size_t size, -struct resource_constraint *constraint) +static int __find_resource_space (struct resource *root,struct resource *old, +struct resource *new,resource_size_t size, +struct resource_constraint *constraint) {struct resource *this = root->child;struct resource tmp = *new, avail, alloc;
@@ -688,13 +687,13 @@ next: if (!this || this->end == root->end) }/* -* Find empty slot in the resource tree given range and alignment. +* Find empty space in the resource tree given range and alignment. */ -static int find_resource (struct resource *root,struct resource *new, -resource_size_t size, -struct resource_constraint *constraint) +static int find_resource_space (struct resource *root,struct resource *new, +resource_size_t size, +struct resource_constraint *constraint) { -return __find_resource (root,NULL , new, size, constraint); +return __find_resource_space (root,NULL , new, size, constraint); }/**
@@ -717,7 +716,7 @@ static int reallocate_resource(struct resource *root, struct resource *old,write_lock (&resource_lock); -if ((err =__find_resource (root, old, &new, newsize, constraint))) +if ((err =__find_resource_space (root, old, &new, newsize, constraint))) goto out;if (resource_contains (&new, old)) {
@@ -786,7 +785,7 @@ int allocate_resource(struct resource *root, struct resource *new, }write_lock (&resource_lock); - err =find_resource (root, new, size, &constraint); + err =find_resource_space (root, new, size, &constraint);if (err >=0 &&__request_resource (root, new)) err = -EBUSY ;write_unlock (&resource_lock);
Signature · 1 line
2.39.2
PATCH v3 3/8 resource: Document find_resource_space() and resource_constraint
In reply to Ilpo JärvinenDocument find_resource_space() and the struct resource_constraint as they are going to be exposed outside of resource.c.
--- kernel/resource.c | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-)
kernel/resource.c +26 -3
@@ -48,7 +48,19 @@ struct resource iomem_resource = { };EXPORT_SYMBOL (iomem_resource); -/* constraints to be met while allocating resources */ +/** +* struct resource_constraint - constraints to be met while searching empty +* resource space +* @min: The minimum address for the memory range +* @max: The maximum address for the memory range +* @align: Alignment for the start address of the empty space +* @alignf: Additional alignment constraints callback +* @alignf_data: Data provided for @alignf callback +* +* Contains the range and alignment constraints that have to be met during +* find_resource_space(). @alignf can be NULL indicating no alignment beyond +* @align is necessary. +*/ struct resource_constraint {resource_size_t min, max, align; resource_size_t (*alignf)(void *, const struct resource *,
@@ -686,8 +698,19 @@ next: if (!this || this->end == root->end)return -EBUSY ; } -/* -* Find empty space in the resource tree given range and alignment. +/** +* find_resource_space - Find empty space in the resource tree +* @root: Root resource descriptor +* @new: Resource descriptor awaiting an empty resource space +* @size: The minimum size of the empty space +* @constraint: The range and alignment constraints to be met +* +* Finds an empty space under @root in the resource tree satisfying range and +* alignment @constraints. +* +* Return: +* * %0 - if successful, @new members start, end, and flags are altered. +* * %-EBUSY - if no empty space was found. */ static int find_resource_space(struct resource *root, struct resource *new, resource_size_t size,
Signature · 1 line
2.39.2
PATCH v3 4/8 resource: Use typedef for alignf callback
In reply to Ilpo JärvinenTo make it simpler to declare resource constraint alignf callbacks, add typedef for it and document it.
--- drivers/pci/bus.c | 10 ++-------- include/linux/ioport.h | 22 ++++++++++++++++++---- include/linux/pci.h | 5 +---- kernel/resource.c | 8 ++------ 4 files changed, 23 insertions(+), 22 deletions(-)
drivers/pci/bus.c +2 -8
@@ -176,10 +176,7 @@ static void pci_clip_resource_to_region(struct pci_bus *bus,static int pci_bus_alloc_from_region (struct pci_bus *bus,struct resource *res,resource_size_t size,resource_size_t align,resource_size_t min,unsigned long type_mask, -resource_size_t (*alignf)(void *, -const struct resource *, -resource_size_t , -resource_size_t ), +resource_alignf alignf,void *alignf_data,struct pci_bus_region *region) {
@@ -250,10 +247,7 @@ static int pci_bus_alloc_from_region(struct pci_bus *bus, struct resource *res,int pci_bus_alloc_resource (struct pci_bus *bus,struct resource *res,resource_size_t size,resource_size_t align,resource_size_t min,unsigned long type_mask, -resource_size_t (*alignf)(void *, -const struct resource *, -resource_size_t , -resource_size_t ), +resource_alignf alignf,void *alignf_data) {#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
include/linux/ioport.h +18 -4
@@ -188,6 +188,23 @@ enum {#define DEFINE_RES_DMA (_dma) \ DEFINE_RES_DMA_NAMED((_dma), NULL) +/** +* typedef resource_alignf - Resource alignment callback +* @data: Private data used by the callback +* @res: Resource candidate range (an empty resource space) +* @size: The minimum size of the empty space +* @align: Alignment from the constraints +* +* Callback allows calculating resource placement and alignment beyond min, +* max, and align fields in the struct resource_constraint. +* +* Return: Start address for the resource. +*/ +typedef resource_size_t (*resource_alignf )(void *data, +const struct resource *res, +resource_size_t size, +resource_size_t align); +/* PC/ISA/whatever - the normal PC address spaces: IO and memory */ extern struct resource ioport_resource;extern struct resource iomem_resource;
@@ -207,10 +224,7 @@ extern void arch_remove_reservations(struct resource *avail);extern int allocate_resource (struct resource *root,struct resource *new,resource_size_t size,resource_size_t min,resource_size_t max,resource_size_t align, -resource_size_t (*alignf)(void *, -const struct resource *, -resource_size_t , -resource_size_t ), +resource_alignf alignf,void *alignf_data);struct resource *lookup_resource (struct resource *root,resource_size_t start); int adjust_resource(struct resource *res, resource_size_t start,
include/linux/pci.h +1 -4
@@ -1552,10 +1552,7 @@ int __must_check pci_bus_alloc_resource(struct pci_bus *bus, struct resource *res, resource_size_t size, resource_size_t align, resource_size_t min, unsigned long type_mask, - resource_size_t (*alignf)(void *, -const struct resource *, -resource_size_t , -resource_size_t ), + resource_alignf alignf, void *alignf_data);
kernel/resource.c +2 -6
@@ -63,8 +63,7 @@ EXPORT_SYMBOL(iomem_resource);*/ struct resource_constraint {resource_size_t min, max, align; -resource_size_t (*alignf)(void *,const struct resource *, -resource_size_t ,resource_size_t ); +resource_alignf alignf;void *alignf_data; };
@@ -783,10 +782,7 @@ static int reallocate_resource(struct resource *root, struct resource *old,int allocate_resource (struct resource *root,struct resource *new,resource_size_t size,resource_size_t min,resource_size_t max,resource_size_t align, -resource_size_t (*alignf)(void *, -const struct resource *, -resource_size_t , -resource_size_t ), +resource_alignf alignf,void *alignf_data) {int err;
Signature · 1 line
2.39.2
PATCH v3 5/8 resource: Handle simple alignment inside __find_resource_space()
In reply to Ilpo Järvinenallocate_resource() accepts ->alignf() callback to perform custom alignment beyond constraint->align. If alignf is NULL, simple_align_resource() is used which only returns avail->start (no change).
Using avail->start directly is natural and can be done with a conditional in __find_resource_space() instead which avoids unnecessarily using callback. It makes the code inside __find_resource_space() more obvious and removes the need for the caller to provide constraint->alignf unnecessarily.
This is preparation for exporting find_resource_space().
--- kernel/resource.c | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-)
kernel/resource.c +7 -13
@@ -621,14 +621,6 @@ void __weak arch_remove_reservations(struct resource *avail) { } -static resource_size_t simple_align_resource (void *data, -const struct resource *avail, -resource_size_t size, -resource_size_t align) -{ -return avail->start; -} -static void resource_clip (struct resource *res,resource_size_t min,resource_size_t max) {
@@ -648,6 +640,7 @@ static int __find_resource_space(struct resource *root, struct resource *old, {struct resource *this = root->child;struct resource tmp = *new, avail, alloc; +resource_alignf alignf = constraint->alignf; tmp.start = root->start;/*
@@ -676,8 +669,12 @@ static int __find_resource_space(struct resource *root, struct resource *old, avail.flags = new->flags & ~IORESOURCE_UNSET ;if (avail.start >= tmp.start) { alloc.flags = avail.flags; - alloc.start = constraint->alignf (constraint->alignf_data, &avail, - size, constraint->align); +if (alignf) { + alloc.start =alignf (constraint->alignf_data, + &avail, size, constraint->align); + }else { + alloc.start = avail.start; + } alloc.end = alloc.start + size -1 ;if (alloc.start <= alloc.end &&resource_contains (&avail, &alloc)) {
@@ -788,9 +785,6 @@ int allocate_resource(struct resource *root, struct resource *new,int err;struct resource_constraint constraint; -if (!alignf) - alignf = simple_align_resource; - constraint.min = min; constraint.max = max; constraint.align = align;
Signature · 1 line
2.39.2
PATCH v3 6/8 resource: Export find_resource_space()
In reply to Ilpo JärvinenPCI bridge window logic needs to find out in advance to the actual allocation if there is an empty space big enough to fit the window.
Export find_resource_space() for the purpose. Also move the struct resource_constraint into generic header to be able to use the new interface.
--- include/linux/ioport.h | 22 ++++++++++++++++++++++ kernel/resource.c | 26 ++++---------------------- 2 files changed, 26 insertions(+), 22 deletions(-)
include/linux/ioport.h +22 -0
@@ -205,6 +205,25 @@ typedef resource_size_t (*resource_alignf)(void *data,resource_size_t size, resource_size_t align); +/** +* struct resource_constraint - constraints to be met while searching empty +* resource space +* @min: The minimum address for the memory range +* @max: The maximum address for the memory range +* @align: Alignment for the start address of the empty space +* @alignf: Additional alignment constraints callback +* @alignf_data: Data provided for @alignf callback +* +* Contains the range and alignment constraints that have to be met during +* find_resource_space(). @alignf can be NULL indicating no alignment beyond +* @align is necessary. +*/ +struct resource_constraint { +resource_size_t min, max, align; +resource_alignf alignf; +void *alignf_data; +}; +/* PC/ISA/whatever - the normal PC address spaces: IO and memory */ extern struct resource ioport_resource;extern struct resource iomem_resource;
@@ -278,6 +297,9 @@ static inline bool resource_union(const struct resource *r1, const struct resourreturn true; } +int find_resource_space (struct resource *root,struct resource *new, +resource_size_t size,struct resource_constraint *constraint); +/* Convenience shorthand with allocation */ #define request_region (start,n,name) __request_region(&ioport_resource, (start), (n), (name), 0)#define request_muxed_region (start,n,name) __request_region(&ioport_resource, (start), (n), (name), IORESOURCE_MUXED)
kernel/resource.c +4 -22
@@ -48,25 +48,6 @@ struct resource iomem_resource = { };EXPORT_SYMBOL (iomem_resource); -/** -* struct resource_constraint - constraints to be met while searching empty -* resource space -* @min: The minimum address for the memory range -* @max: The maximum address for the memory range -* @align: Alignment for the start address of the empty space -* @alignf: Additional alignment constraints callback -* @alignf_data: Data provided for @alignf callback -* -* Contains the range and alignment constraints that have to be met during -* find_resource_space(). @alignf can be NULL indicating no alignment beyond -* @align is necessary. -*/ -struct resource_constraint { -resource_size_t min, max, align; -resource_alignf alignf; -void *alignf_data; -}; -static DEFINE_RWLOCK (resource_lock);static struct resource *next_resource (struct resource *p,bool skip_children)
@@ -708,12 +689,13 @@ next: if (!this || this->end == root->end)* * %0 - if successful, @new members start, end, and flags are altered. * * %-EBUSY - if no empty space was found. */ -static int find_resource_space (struct resource *root,struct resource *new, -resource_size_t size, -struct resource_constraint *constraint) +int find_resource_space (struct resource *root,struct resource *new, +resource_size_t size, +struct resource_constraint *constraint) {return __find_resource_space (root,NULL , new, size, constraint); } +EXPORT_SYMBOL_GPL (find_resource_space);/** * reallocate_resource - allocate a slot in the resource tree given range & alignment.
Signature · 1 line
2.39.2
PATCH v3 7/8 PCI: Make minimum bridge window alignment reference more obvious
In reply to Ilpo JärvinenCalculations related to bridge window size contain literal 20 that is the minimum alignment for a bridge window. Make the code more obvious by converting the literal 20 to __ffs(SZ_1MB).
--- drivers/pci/setup-bus.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/pci/setup-bus.c +4 -2
@@ -14,6 +14,7 @@* tighter packing. Prefetchable range support. */ +#include <linux/bitops.h> #include <linux/init.h> #include <linux/kernel.h> #include <linux/module.h>
@@ -21,6 +22,7 @@#include <linux/errno.h> #include <linux/ioport.h> #include <linux/cache.h> +#include <linux/sizes.h> #include <linux/slab.h> #include <linux/acpi.h> #include "pci.h"
@@ -957,7 +959,7 @@ static inline resource_size_t calculate_mem_align(resource_size_t *aligns,for (order =0 ; order <= max_order; order++) {resource_size_t align1 =1 ; - align1 <<= (order +20 ); + align1 <<= (order +__ffs (SZ_1M ));if (!align) min_align = align1;
@@ -1047,7 +1049,7 @@ static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,* resources. */ align =pci_resource_alignment (dev, r); - order =__ffs (align) -20 ; + order =__ffs (align) -__ffs (SZ_1M );if (order <0 ) order =0 ;if (order >=ARRAY_SIZE (aligns)) {
Signature · 1 line
2.39.2
Re: [PATCH v3 7/8] PCI: Make minimum bridge window alignment reference more obvious
Quoted 3 lines · Ilpo Järvinen
Calculations related to bridge window size contain literal 20 that is the minimum alignment for a bridge window. Make the code more obvious by converting the literal 20 to
__ffs(SZ_1MB).
I think that's SZ_1M not SZ_1MB :)
Quoted 1 line
Signed-off-by Ilpo Järvinen <[email protected]>
Looks good, may be even add a #define for this but either way,
Re: [PATCH v3 7/8] PCI: Make minimum bridge window alignment reference more obvious
Quoted 6 lines · Mika Westerberg
Quoted 3 lines · Ilpo Järvinen
Calculations related to bridge window size contain literal 20 that is the minimum alignment for a bridge window. Make the code more obvious by converting the literal 20 to
__ffs(SZ_1MB).I think that's
SZ_1MnotSZ_1MB:)
Of course, that's the only place which is not checked by the compiler so it's the place where I type it wrong and forget to use backspace. :-)
Quoted 3 lines
Quoted 1 line
Signed-off-by Ilpo Järvinen <[email protected]>Looks good, may be even add a #define for this but either way,
I considered that but I could not find a good name for the whole construct (with the __ffs() I mean). Perhaps PCI_BRIDGE_WINDOW_LSB could be an option but that feels somewhat clumsy to me.
Signature · 1 line
i.
Re: [PATCH v3 7/8] PCI: Make minimum bridge window alignment reference more obvious
In reply to Ilpo JärvinenQuoted 3 lines · Ilpo Järvinen
Calculations related to bridge window size contain literal 20 that is the minimum alignment for a bridge window. Make the code more obvious by converting the literal 20 to
__ffs(SZ_1MB).
...
drivers/pci/setup-bus.c static inline resource_size_t calculate_mem_align(resource_size_t *aligns, · line 962
Quoted 2 lines
drivers/pci/setup-bus.c +1
-1- align1 <<= (order +20 ); + align1 <<= (order +__ffs (SZ_1M ));
No need for outer parentheses.
...
drivers/pci/setup-bus.c static int pbus_size_mem(struct pci_bus *bus, unsigned long mask, · line 1052
Quoted 1 line
drivers/pci/setup-bus.c +1
-0+ order =__ffs (align) -__ffs (SZ_1M );
Yeah, would be nice to have something like
#define bit_distance(a, b) (ffs(a) - fls(b))
in bitops.h as we have a few users and I have heard about one more coming, but this is topic to another discussion. (Yuri, just FYI.)
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH v3 8/8 PCI: Relax bridge window tail sizing rules
In reply to Ilpo JärvinenDuring remove & rescan cycle, PCI subsystem will recalculate and adjust the bridge window sizing that was initially done by "BIOS". The size calculation is based on the required alignment of the largest resource among the downstream resources as per pbus_size_mem() (unimportant or zero parameters marked with "..."):
min_align = calculate_mem_align(aligns, max_order); size0 = calculate_memsize(size, ..., min_align);
inside calculate_memsize(), for the largest alignment:
min_align = align1 >> 1; ... return min_align;
and then in calculate_memsize():
return ALIGN(max(size, ...), align);
If the original bridge window sizing tried to conserve space, this will lead to massive increase of the required bridge window size when the downstream has a large disparity in BAR sizes. E.g., with 16MiB and 16GiB BARs this results in 24GiB bridge window size even if 16MiB BAR does not require gigabytes of space to fit.
When doing remove & rescan for a bus that contains such a PCI device, a larger bridge window is suddenly required on rescan but when there is a bridge window upstream that is already assigned based on the original size, it cannot be enlarged to the new requirement. This causes the allocation of the bridge window to fail (0x600000000 > 0x400ffffff):
pci 0000:02:01.0: PCI bridge to [bus 03]
pci 0000:02:01.0: bridge window [mem 0x40400000-0x405fffff]
pci 0000:02:01.0: bridge window [mem 0x6000000000-0x6400ffffff 64bit pref]
pci 0000:01:00.0: PCI bridge to [bus 02-04]
pci 0000:01:00.0: bridge window [mem 0x40400000-0x406fffff]
pci 0000:01:00.0: bridge window [mem 0x6000000000-0x6400ffffff 64bit pref]
pci 0000:03:00.0: device released
pci 0000:02:01.0: device released
pcieport 0000:01:00.0: scanning [bus 02-04] behind bridge, pass 0 pci 0000:02:01.0: PCI bridge to [bus 03]
pci 0000:02:01.0: bridge window [mem 0x40400000-0x405fffff]
pci 0000:02:01.0: bridge window [mem 0x6000000000-0x6400ffffff 64bit pref]
pci 0000:02:01.0: scanning [bus 03-03] behind bridge, pass 0 pci 0000:03:00.0: BAR 0 [mem 0x6400000000-0x6400ffffff 64bit pref] pci 0000:03:00.0: BAR 2 [mem 0x6000000000-0x63ffffffff 64bit pref] pci 0000:03:00.0: ROM [mem 0x40400000-0x405fffff pref]
pci 0000:02:01.0: PCI bridge to [bus 03]
pci 0000:02:01.0: scanning [bus 03-03] behind bridge, pass 1
pcieport 0000:01:00.0: scanning [bus 02-04] behind bridge, pass 1
pci 0000:02:01.0: bridge window [mem size 0x600000000 64bit pref]: can't assign; no space pci 0000:02:01.0: bridge window [mem size 0x600000000 64bit pref]: failed to assign pci 0000:02:01.0: bridge window [mem 0x40400000-0x405fffff]: assigned
pci 0000:03:00.0: BAR 2 [mem size 0x400000000 64bit pref]: can't assign; no space pci 0000:03:00.0: BAR 2 [mem size 0x400000000 64bit pref]: failed to assign pci 0000:03:00.0: BAR 0 [mem size 0x01000000 64bit pref]: can't assign; no space pci 0000:03:00.0: BAR 0 [mem size 0x01000000 64bit pref]: failed to assign pci 0000:03:00.0: ROM [mem 0x40400000-0x405fffff pref]: assigned
pci 0000:02:01.0: PCI bridge to [bus 03]
pci 0000:02:01.0: bridge window [mem 0x40400000-0x405fffff]
This is a major surprise for users who are suddenly left with a PCIe device that was working fine with the original bridge window sizing.
Even if the already assigned bridge window could be enlarged by reallocation in some cases (something the current code does not attempt to do), it is not possible in general case and the large amount of wasted space at the tail of the bridge window may lead to other resource exhaustion problems on Root Complex level (think of multiple PCIe cards with VFs and BAR size disparity in a single system).
PCI specifications only expect natural alignment for BARs (PCI Express Base Specification, rev. 6.1 sect. 7.5.1.2.1) and minimum of 1MiB alignment for the bridge window (PCI Express Base Specification, rev 6.1 sect. 7.5.1.3). The current bridge window tail alignment rule was introduced in the commit 5d0a8965aea9 ("[PATCH] 2.5.14: New PCI allocation code (alpha, arm, parisc) [2/2]") that only states:
"pbus_size_mem: core stuff; tested with randomly generated sets of resources". It does not explain the motivation for the extra tail space allocated that is not truly needed by the downstream resources. As such, it is far from clear if it ever has been required by any HW.
To prevent PCIe cards with BAR size disparity from becoming unusable after remove & rescan cycle, attempt to do a truly minimal allocation for memory resources if needed. First check if the normally calculated bridge window will not fit into an already assigned upstream resource. In such case, try with relaxed bridge window tail sizing rules instead where no extra tail space is requested beyond what the downstream resources require. Only enforce the alignment requirement of the bridge window itself (normally 1MiB).
With this patch, the resources are successfully allocated:
pci 0000:02:01.0: PCI bridge to [bus 03]
pci 0000:02:01.0: scanning [bus 03-03] behind bridge, pass 1
pcieport 0000:01:00.0: scanning [bus 02-04] behind bridge, pass 1
pcieport 0000:01:00.0: Assigned bridge window [mem 0x6000000000-0x6400ffffff 64bit pref] to [bus 02-04] cannot fit 0x600000000 required for 0000:02:01.0 bridging to [bus 03] pci 0000:02:01.0: bridge window [mem 0x6000000000-0x6400ffffff 64bit pref] to [bus 03] requires relaxed alignment rules
pcieport 0000:01:00.0: Assigned bridge window [mem 0x40400000-0x406fffff] to [bus 02-04] free space at [mem 0x40400000-0x405fffff]
pci 0000:02:01.0: bridge window [mem 0x6000000000-0x6400ffffff 64bit pref]: assigned
pci 0000:02:01.0: bridge window [mem 0x40400000-0x405fffff]: assigned
pci 0000:03:00.0: BAR 2 [mem 0x6000000000-0x63ffffffff 64bit pref]: assigned
pci 0000:03:00.0: BAR 0 [mem 0x6400000000-0x6400ffffff 64bit pref]: assigned
pci 0000:03:00.0: ROM [mem 0x40400000-0x405fffff pref]: assigned
pci 0000:02:01.0: PCI bridge to [bus 03]
pci 0000:02:01.0: bridge window [mem 0x40400000-0x405fffff]
pci 0000:02:01.0: bridge window [mem 0x6000000000-0x6400ffffff 64bit pref]
This patch draws inspiration from the initial investigations and work by Mika Westerberg.
--- drivers/pci/setup-bus.c | 79 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 77 insertions(+), 2 deletions(-)
drivers/pci/setup-bus.c +77 -2
@@ -22,6 +22,7 @@#include <linux/errno.h> #include <linux/ioport.h> #include <linux/cache.h> +#include <linux/limits.h> #include <linux/sizes.h> #include <linux/slab.h> #include <linux/acpi.h>
@@ -971,6 +972,67 @@ static inline resource_size_t calculate_mem_align(resource_size_t *aligns,return min_align; } +/** +* pbus_upstream_space_available - Check no upstream resource limits allocation +* @bus: The bus +* @mask: Mask the resource flag, then compare it with type +* @type: The type of resource from bridge +* @size: The size required from the bridge window +* @align: Required alignment for the resource +* +* Checks that @size can fit inside the upstream bridge resources that are +* already assigned. +* +* Return: %true if enough space is available on all assigned upstream +* resources. +*/ +static bool pbus_upstream_space_available (struct pci_bus *bus,unsigned long mask, +unsigned long type,resource_size_t size, +resource_size_t align) +{ +struct resource_constraint constraint = { + .max =RESOURCE_SIZE_MAX , + .align = align, + }; +struct pci_bus *downstream = bus; +struct resource *r; + +while ((bus = bus->parent)) { +if (pci_is_root_bus (bus)) +break ; + +pci_bus_for_each_resource (bus, r) { +if (!r || !r->parent || (r->flags & mask) != type) +continue ; + +if (resource_size (r) >= size) { +struct resource gap = {}; + +if (find_resource_space (r, &gap, size, &constraint) ==0 ) { + gap.flags = type; +pci_dbg (bus->self, +"Assigned bridge window %pR to %pR free space at %pR\n" , + r, &bus->busn_res, &gap); +return true; + } + } + +if (bus->self) { +pci_info (bus->self, +"Assigned bridge window %pR to %pR cannot fit 0x%llx required for %s bridging to %pR\n" , + r, &bus->busn_res, + (unsigned long long )size, +pci_name (downstream->self), + &downstream->busn_res); + } + +return false; + } + } + +return true; +} +/** * pbus_size_mem() - Size the memory window of a given bus *
@@ -997,7 +1059,7 @@ static int pbus_size_mem(struct pci_bus *bus, unsigned long mask, struct list_head *realloc_head) {struct pci_dev *dev; -resource_size_t min_align, align, size, size0, size1; +resource_size_t min_align, win_align, align, size, size0, size1;resource_size_t aligns[24 ];/* Alignments from 1MB to 8TB */ int order, max_order; struct resource *b_res = find_bus_resource_of_type(bus,
@@ -1076,10 +1138,23 @@ static int pbus_size_mem(struct pci_bus *bus, unsigned long mask, } } + win_align =window_alignment (bus, b_res->flags); min_align =calculate_mem_align (aligns, max_order); - min_align =max (min_align,window_alignment (bus, b_res->flags)); + min_align =max (min_align, win_align); size0 =calculate_memsize (size, min_size,0 ,0 ,resource_size (b_res), min_align); add_align =max (min_align, add_align); + +if (bus->self && size0 && + !pbus_upstream_space_available (bus, mask |IORESOURCE_PREFETCH , type, + size0, add_align)) { + min_align =1ULL << (max_order +__ffs (SZ_1M )); + min_align =max (min_align, win_align); + size0 =calculate_memsize (size, min_size,0 ,0 ,resource_size (b_res), win_align); + add_align = win_align; +pci_info (bus->self,"bridge window %pR to %pR requires relaxed alignment rules\n" , + b_res, &bus->busn_res); + } + size1 = (!realloc_head || (realloc_head && !add_size && !children_add_size)) ? size0 :calculate_memsize (size, min_size, add_size, children_add_size,resource_size (b_res), add_align);
Signature · 1 line
2.39.2
Re: [PATCH v3 8/8] PCI: Relax bridge window tail sizing rules
Quoted 110 lines · Ilpo Järvinen
During remove & rescan cycle, PCI subsystem will recalculate and adjust the bridge window sizing that was initially done by "BIOS". The size calculation is based on the required alignment of the largest resource among the downstream resources as per
pbus_size_mem()(unimportant or zero parameters marked with "..."):min_align = calculate_mem_align(aligns, max_order); size0 = calculate_memsize(size, ..., min_align);inside
calculate_memsize(), for the largest alignment:min_align = align1 >> 1; ... return min_align;and then in
calculate_memsize():return ALIGN(max(size, ...), align);If the original bridge window sizing tried to conserve space, this will lead to massive increase of the required bridge window size when the downstream has a large disparity in BAR sizes. E.g., with 16MiB and 16GiB BARs this results in 24GiB bridge window size even if 16MiB BAR does not require gigabytes of space to fit.
When doing remove & rescan for a bus that contains such a PCI device, a larger bridge window is suddenly required on rescan but when there is a bridge window upstream that is already assigned based on the original size, it cannot be enlarged to the new requirement. This causes the allocation of the bridge window to fail (
0x600000000>0x400ffffff):pci 0000:02:01.0: PCI bridge to [bus 03]
pci 0000:02:01.0: bridge window [mem 0x40400000-0x405fffff]
pci 0000:02:01.0: bridge window [mem 0x6000000000-0x6400ffffff 64bit pref]
pci 0000:01:00.0: PCI bridge to [bus 02-04]
pci 0000:01:00.0: bridge window [mem 0x40400000-0x406fffff]
pci 0000:01:00.0: bridge window [mem 0x6000000000-0x6400ffffff 64bit pref]pci 0000:03:00.0: device released
pci 0000:02:01.0: device released
pcieport 0000:01:00.0: scanning [bus 02-04] behind bridge, pass 0 pci 0000:02:01.0: PCI bridge to [bus 03]
pci 0000:02:01.0: bridge window [mem 0x40400000-0x405fffff]
pci 0000:02:01.0: bridge window [mem 0x6000000000-0x6400ffffff 64bit pref]
pci 0000:02:01.0: scanning [bus 03-03] behind bridge, pass 0 pci 0000:03:00.0: BAR 0 [mem 0x6400000000-0x6400ffffff 64bit pref] pci 0000:03:00.0: BAR 2 [mem 0x6000000000-0x63ffffffff 64bit pref] pci 0000:03:00.0: ROM [mem 0x40400000-0x405fffff pref]pci 0000:02:01.0: PCI bridge to [bus 03]
pci 0000:02:01.0: scanning [bus 03-03] behind bridge, pass 1
pcieport 0000:01:00.0: scanning [bus 02-04] behind bridge, pass 1
pci 0000:02:01.0: bridge window [mem size0x60000000064bit pref]: can't assign; no space pci 0000:02:01.0: bridge window [mem size0x60000000064bit pref]: failed to assign pci 0000:02:01.0: bridge window [mem 0x40400000-0x405fffff]: assigned
pci 0000:03:00.0: BAR 2 [mem size0x40000000064bit pref]: can't assign; no space pci 0000:03:00.0: BAR 2 [mem size0x40000000064bit pref]: failed to assign pci 0000:03:00.0: BAR 0 [mem size0x0100000064bit pref]: can't assign; no space pci 0000:03:00.0: BAR 0 [mem size0x0100000064bit pref]: failed to assign pci 0000:03:00.0: ROM [mem 0x40400000-0x405fffff pref]: assigned
pci 0000:02:01.0: PCI bridge to [bus 03]
pci 0000:02:01.0: bridge window [mem 0x40400000-0x405fffff]This is a major surprise for users who are suddenly left with a PCIe device that was working fine with the original bridge window sizing.
Even if the already assigned bridge window could be enlarged by reallocation in some cases (something the current code does not attempt to do), it is not possible in general case and the large amount of wasted space at the tail of the bridge window may lead to other resource exhaustion problems on Root Complex level (think of multiple PCIe cards with VFs and BAR size disparity in a single system).
PCI specifications only expect natural alignment for BARs (PCI Express Base Specification, rev. 6.1 sect. 7.5.1.2.1) and minimum of 1MiB alignment for the bridge window (PCI Express Base Specification, rev 6.1 sect. 7.5.1.3). The current bridge window tail alignment rule was introduced in the commit
5d0a8965aea9("[PATCH] 2.5.14: New PCI allocation code (alpha, arm, parisc) [2/2]") that only states:
"pbus_size_mem: core stuff; tested with randomly generated sets of resources". It does not explain the motivation for the extra tail space allocated that is not truly needed by the downstream resources. As such, it is far from clear if it ever has been required by any HW.To prevent PCIe cards with BAR size disparity from becoming unusable after remove & rescan cycle, attempt to do a truly minimal allocation for memory resources if needed. First check if the normally calculated bridge window will not fit into an already assigned upstream resource. In such case, try with relaxed bridge window tail sizing rules instead where no extra tail space is requested beyond what the downstream resources require. Only enforce the alignment requirement of the bridge window itself (normally 1MiB).
With this patch, the resources are successfully allocated:
pci 0000:02:01.0: PCI bridge to [bus 03]
pci 0000:02:01.0: scanning [bus 03-03] behind bridge, pass 1
pcieport 0000:01:00.0: scanning [bus 02-04] behind bridge, pass 1
pcieport 0000:01:00.0: Assigned bridge window [mem 0x6000000000-0x6400ffffff 64bit pref] to [bus 02-04] cannot fit0x600000000required for 0000:02:01.0 bridging to [bus 03] pci 0000:02:01.0: bridge window [mem 0x6000000000-0x6400ffffff 64bit pref] to [bus 03] requires relaxed alignment rules
pcieport 0000:01:00.0: Assigned bridge window [mem 0x40400000-0x406fffff] to [bus 02-04] free space at [mem 0x40400000-0x405fffff]
pci 0000:02:01.0: bridge window [mem 0x6000000000-0x6400ffffff 64bit pref]: assigned
pci 0000:02:01.0: bridge window [mem 0x40400000-0x405fffff]: assigned
pci 0000:03:00.0: BAR 2 [mem 0x6000000000-0x63ffffffff 64bit pref]: assigned
pci 0000:03:00.0: BAR 0 [mem 0x6400000000-0x6400ffffff 64bit pref]: assigned
pci 0000:03:00.0: ROM [mem 0x40400000-0x405fffff pref]: assigned
pci 0000:02:01.0: PCI bridge to [bus 03]
pci 0000:02:01.0: bridge window [mem 0x40400000-0x405fffff]
pci 0000:02:01.0: bridge window [mem 0x6000000000-0x6400ffffff 64bit pref]This patch draws inspiration from the initial investigations and work by Mika Westerberg.
...
drivers/pci/setup-bus.c static int pbus_size_mem(struct pci_bus *bus, unsigned long mask, · line 1150
Quoted 1 line
drivers/pci/setup-bus.c +1
-0+ min_align =1ULL << (max_order +__ffs (SZ_1M ));
In case of a new version of the series, this can utilise BIT_ULL().
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 7 lines · Ilpo Järvinen
Hi all,
Here's a series that contains two fixes to PCI bridge window sizing algorithm. Together, they should enable remove & rescan cycle to work for a PCI bus that has PCI devices with optional resources and/or disparity in BAR sizes.
Quoted 8 lines
For the second fix, I chose to expose
find_resource_space()fromkernel/resource.cbecause it should increase accuracy of the cannot-fit decision (currently that function is calledfind_resource()). In order to do that sensibly, a few improvements seemed in order to make its interface and name of the function sane before exposing it. Thus, the few extra patches on resource side.v3:
Hi Bjorn,
It's a bit unclear to me what is your view about the status of this series? I see you placed these first into some wip branches and then into resource branch but the state of the patches in patchwork is still marked as "New" nor have you sent any notice they'd have been "applied".
I'm thinking this from the perspective of whether I should send v4 with those minor comments from Andy addressed or not? I could also send those minor things as separate patches on top of the series if that's easier/better for you.
Signature · 1 line
i.
Quoted 36 lines
- Removed "slot" wording
- Renamedfind_empty_resource_slot()->find_resource_space()
-find_resource_space()returns bool instead of int
- Added patch to convert literal 20 related to bridge win minimum alignment to__ffs(SZ_1M)
- Fixed kerneldoc missing "struct"
- Tweaked prints (one dbg -> info, added new dbg one for success case)
- Changelog tweaks
- Take account largest >> 1 (in alignment calc)
- Adjust to minor changes made intocalculate_memsize()
- Take logs from more recent kernel to get rid of reg 0xXXv2:
- Add "typedef" to kerneldoc to get correct formatting
- UseRESOURCE_SIZE_MAXinstead of literal
- Remove unnecessary checks for io{port/mem}_resource
- Apply a few style tweaks from AndyIlpo Järvinen (8):
PCI: Fix resource double counting on remove & rescan
resource: Renamefind_resource()tofind_resource_space()
resource: Documentfind_resource_space()andresource_constraint
resource: Use typedef for alignf callback
resource: Handle simple alignment inside__find_resource_space()
resource: Exportfind_resource_space()
PCI: Make minimum bridge window alignment reference more obvious
PCI: Relax bridge window tail sizing rulesdrivers/pci/bus.c | 10 +---- drivers/pci/setup-bus.c | 91 +++++++++++++++++++++++++++++++++++++---- include/linux/ioport.h | 44 ++++++++++++++++++-- include/linux/pci.h | 5 +-- kernel/resource.c | 68 ++++++++++++++---------------- 5 files changed, 157 insertions(+), 61 deletions(-)
Quoted 25 lines · Ilpo Järvinen
Quoted 7 lines · Ilpo Järvinen
Hi all,
Here's a series that contains two fixes to PCI bridge window sizing algorithm. Together, they should enable remove & rescan cycle to work for a PCI bus that has PCI devices with optional resources and/or disparity in BAR sizes.
Quoted 8 lines
For the second fix, I chose to expose
find_resource_space()fromkernel/resource.cbecause it should increase accuracy of the cannot-fit decision (currently that function is calledfind_resource()). In order to do that sensibly, a few improvements seemed in order to make its interface and name of the function sane before exposing it. Thus, the few extra patches on resource side.v3:
Hi Bjorn,
It's a bit unclear to me what is your view about the status of this series? I see you placed these first into some wip branches and then into resource branch but the state of the patches in patchwork is still marked as "New" nor have you sent any notice they'd have been "applied".
Quoted 4 lines
I'm thinking this from the perspective of whether I should send v4 with those minor comments from Andy addressed or not? I could also send those minor things as separate patches on top of the series if that's easier/better for you.
Sorry, I dropped the ball in the middle here. The pci/resource branch has been in linux-next since May 29, but I forgot to send a note. If you want to tweak for Andy's comments, send an incremental patch and I'll be happy to squash it/them in.
Bjorn Helgaas
AppliedQuoted 43 lines · Ilpo Järvinen
Hi all,
Here's a series that contains two fixes to PCI bridge window sizing algorithm. Together, they should enable remove & rescan cycle to work for a PCI bus that has PCI devices with optional resources and/or disparity in BAR sizes.
For the second fix, I chose to expose
find_resource_space()fromkernel/resource.cbecause it should increase accuracy of the cannot-fit decision (currently that function is calledfind_resource()). In order to do that sensibly, a few improvements seemed in order to make its interface and name of the function sane before exposing it. Thus, the few extra patches on resource side.v3:
- Removed "slot" wording
- Renamedfind_empty_resource_slot()->find_resource_space()
-find_resource_space()returns bool instead of int
- Added patch to convert literal 20 related to bridge win minimum alignment to__ffs(SZ_1M)
- Fixed kerneldoc missing "struct"
- Tweaked prints (one dbg -> info, added new dbg one for success case)
- Changelog tweaks
- Take account largest >> 1 (in alignment calc)
- Adjust to minor changes made intocalculate_memsize()
- Take logs from more recent kernel to get rid of reg 0xXXv2:
- Add "typedef" to kerneldoc to get correct formatting
- UseRESOURCE_SIZE_MAXinstead of literal
- Remove unnecessary checks for io{port/mem}_resource
- Apply a few style tweaks from AndyIlpo Järvinen (8):
PCI: Fix resource double counting on remove & rescan
resource: Renamefind_resource()tofind_resource_space()
resource: Documentfind_resource_space()andresource_constraint
resource: Use typedef for alignf callback
resource: Handle simple alignment inside__find_resource_space()
resource: Exportfind_resource_space()
PCI: Make minimum bridge window alignment reference more obvious
PCI: Relax bridge window tail sizing rules
Quoted 6 lines
drivers/pci/bus.c | 10 +---- drivers/pci/setup-bus.c | 91 +++++++++++++++++++++++++++++++++++++---- include/linux/ioport.h | 44 ++++++++++++++++++-- include/linux/pci.h | 5 +-- kernel/resource.c | 68 ++++++++++++++---------------- 5 files changed, 157 insertions(+), 61 deletions(-)
Applied to pci/resource for v6.11, thanks!