PCI: fix pci_bus_alloc_resource() hang, prefer positive decode

When a PCI bus has two resources with the same start/end, e.g.,

pci_bus 0000:04: resource 2 [mem 0xd0000000-0xd7ffffff pref]
pci_bus 0000:04: resource 7 [mem 0xd0000000-0xd7ffffff]

the previous pci_bus_find_resource_prev() implementation would alternate
between them forever:

pci_bus_find_resource_prev(... [mem 0xd0000000-0xd7ffffff pref])
returns [mem 0xd0000000-0xd7ffffff]
pci_bus_find_resource_prev(... [mem 0xd0000000-0xd7ffffff])
returns [mem 0xd0000000-0xd7ffffff pref]
pci_bus_find_resource_prev(... [mem 0xd0000000-0xd7ffffff pref])
returns [mem 0xd0000000-0xd7ffffff]
...

This happened because there was no ordering between two resources with the
same start and end. A resource that had the same start and end as the
cursor, but was not itself the cursor, was considered to be before the
cursor.

This patch fixes the hang by making a fixed ordering between any two
resources.

In addition, it tries to allocate from positively decoded regions before
using any subtractively decoded resources. This means we will use a
positive decode region before a subtractive decode one, even if it means
using a smaller address.

Reference: https://bugzilla.kernel.org/show_bug.cgi?id=22062
Reported-by: Borislav Petkov <bp@amd64.org>
Tested-by: Borislav Petkov <bp@amd64.org>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Bjorn Helgaas <bjorn.helgaas@hp.com>
Signed-off-by: Jesse Barnes <jbarnes@virtuousgeek.org>

authored by Bjorn Helgaas and committed by Jesse Barnes 82e3e767 97c145f7

+49 -21
+49 -21
drivers/pci/bus.c
··· 64 64 } 65 65 } 66 66 67 + static bool pci_bus_resource_better(struct resource *res1, bool pos1, 68 + struct resource *res2, bool pos2) 69 + { 70 + /* If exactly one is positive decode, always prefer that one */ 71 + if (pos1 != pos2) 72 + return pos1 ? true : false; 73 + 74 + /* Prefer the one that contains the highest address */ 75 + if (res1->end != res2->end) 76 + return (res1->end > res2->end) ? true : false; 77 + 78 + /* Otherwise, prefer the one with highest "center of gravity" */ 79 + if (res1->start != res2->start) 80 + return (res1->start > res2->start) ? true : false; 81 + 82 + /* Otherwise, choose one arbitrarily (but consistently) */ 83 + return (res1 > res2) ? true : false; 84 + } 85 + 86 + static bool pci_bus_resource_positive(struct pci_bus *bus, struct resource *res) 87 + { 88 + struct pci_bus_resource *bus_res; 89 + 90 + /* 91 + * This relies on the fact that pci_bus.resource[] refers to P2P or 92 + * CardBus bridge base/limit registers, which are always positively 93 + * decoded. The pci_bus.resources list contains host bridge or 94 + * subtractively decoded resources. 95 + */ 96 + list_for_each_entry(bus_res, &bus->resources, list) { 97 + if (bus_res->res == res) 98 + return (bus_res->flags & PCI_SUBTRACTIVE_DECODE) ? 99 + false : true; 100 + } 101 + return true; 102 + } 103 + 67 104 /* 68 - * Find the highest-address bus resource below the cursor "res". If the 69 - * cursor is NULL, return the highest resource. 105 + * Find the next-best bus resource after the cursor "res". If the cursor is 106 + * NULL, return the best resource. "Best" means that we prefer positive 107 + * decode regions over subtractive decode, then those at higher addresses. 70 108 */ 71 109 static struct resource *pci_bus_find_resource_prev(struct pci_bus *bus, 72 110 unsigned int type, 73 111 struct resource *res) 74 112 { 113 + bool res_pos, r_pos, prev_pos = false; 75 114 struct resource *r, *prev = NULL; 76 115 int i; 77 116 117 + res_pos = pci_bus_resource_positive(bus, res); 78 118 pci_bus_for_each_resource(bus, r, i) { 79 119 if (!r) 80 120 continue; ··· 122 82 if ((r->flags & IORESOURCE_TYPE_BITS) != type) 123 83 continue; 124 84 125 - /* If this resource is at or past the cursor, skip it */ 126 - if (res) { 127 - if (r == res) 128 - continue; 129 - if (r->end > res->end) 130 - continue; 131 - if (r->end == res->end && r->start > res->start) 132 - continue; 85 + r_pos = pci_bus_resource_positive(bus, r); 86 + if (!res || pci_bus_resource_better(res, res_pos, r, r_pos)) { 87 + if (!prev || pci_bus_resource_better(r, r_pos, 88 + prev, prev_pos)) { 89 + prev = r; 90 + prev_pos = r_pos; 91 + } 133 92 } 134 - 135 - if (!prev) 136 - prev = r; 137 - 138 - /* 139 - * A small resource is higher than a large one that ends at 140 - * the same address. 141 - */ 142 - if (r->end > prev->end || 143 - (r->end == prev->end && r->start > prev->start)) 144 - prev = r; 145 93 } 146 94 147 95 return prev;