# Infinite Loop in PCI Library Auto Bus Enumeration

Reported by: Owned by: Jeffrey Hill Daniel Hellstrom normal score 4.11 normal

### Description

There are some infinite loops in the PCI Library Auto Bus Enumeration, fixed as follows. Also, there is a memory leak at where the pragma indicates.

```@@ -76,6 +76,7 @@ static void pci_res_insert(struct pci_res **root, struct pci_res *res)
* sorted after resulting boundary - the boundary after
* the resource.
*/
+  if ( size ) {
while (curr && (curr->boundary >= boundary)) {
if (curr->boundary == boundary) {
/* Find Resulting boundary of size */
@@ -97,6 +98,7 @@ static void pci_res_insert(struct pci_res **root, struct pci_res *res)
last = curr;
curr = curr->next;
}
+  }

if (last == NULL) {
/* Insert first in list */
@@ -106,6 +108,7 @@ static void pci_res_insert(struct pci_res **root, struct pci_res *res)
last->next = res;
res->next = curr;
}
+
}

#ifdef DEBUG
@@ -114,9 +117,9 @@ void pci_res_list_print(struct pci_res *root)
if (root == NULL)
return;

-	printf("RESOURCE LIST:\n");
+	DBG("RESOURCE LIST:\n");
while (root) {
-		printf(" SIZE: 0x%08x, BOUNDARY: 0x%08x\n", root->size,
+		DBG(" SIZE: 0x%08x, BOUNDARY: 0x%08x\n", root->size,
root->boundary);
root = root->next;
}
@@ -182,13 +185,13 @@ static void pci_res_reorder(struct pci_res *root)
* and next */
hole_size = start_next - start;

+		/* Detect dead hole */
+		if (hole_size > 0) {
/* Find Boundary of START */
hole_boundary = 1;
while ((start & hole_boundary) == 0)
hole_boundary = hole_boundary<<1;

-		/* Detect dead hole */
-		if (hole_size > 0) {
/* Step through list and try to find a resource that
* can fit into hole. Take into account hole start
* boundary and hole size.
@@ -509,6 +512,7 @@ static void pci_add_res_bus(struct pci_bus *bus, int type)
{
int tindex = type - 1;

+#pragma message ("memory leak likely, second pass through here")
/* Clear old resources */
bus->busres[tindex] = NULL;

@@ -580,7 +584,7 @@ static int pci_add_res_dev(struct pci_dev *dev, void *arg)
}
}

-	/* Normal PCI Device as max 6 BARs and a ROM Bar.
+	/* Normal PCI Device has max 6 BARs and a ROM Bar.
* Insert BARs into the sorted resource list.
*/
for (i = 0; i < DEV_RES_CNT; i++) {
@@ -621,7 +625,7 @@ static uint32_t pci_alloc_res(struct pci_bus *bus, int type,

if ((starttmp + res->size - 1) > end) {
/* Not enough memory available for this resource */
-			printk("PCI[%x:%x:%x]: DEV BAR%d (%d): no resource "
+			DBG("PCI[%x:%x:%x]: DEV BAR%d (%d): no resource "
"assigned\n",
PCI_DEV_EXPAND(dev->busdevfun),
@@ -725,7 +729,7 @@ static void pci_set_bar(struct pci_dev *dev, int residx)
tmp = ((res->end-1) & 0xffff0000) | (res->start >> 16);

DBG("PCI[%x:%x:%x]: BRIDGE BAR 0x%x: 0x%08x [0x30: 0x%x]\n",
-			PCI_DEV_EXPAND(pcidev), 0x1C, tmp, tmp2);
+			PCI_DEV_EXPAND(pcidev), 0x1C, tmp, tmp16);
PCI_CFG_W16(pcidev, 0x1C, tmp16);
PCI_CFG_W32(pcidev, 0x30, tmp);
} else if (is_bridge && (res->bar >= BRIDGE_RES_MEMIO)) {
```

### comment:1 Changed on Jun 29, 2017 at 4:15:15 PM by Jeffrey Hill

trace

```(gdb) bt
#0  pci_res_reorder (root=<optimized out>)
#3  0x0830cc4c in pci_for_each_child (bus=bus@entry=0x89c5114,
search=search@entry=0)
#4  0x0830c294 in pci_add_res_bus (type=2, bus=0x89c5114)
#6  0x0830cc4c in pci_for_each_child (bus=bus@entry=0x89c4f3c,
search=search@entry=0)
#7  0x0830c294 in pci_add_res_bus (type=2, bus=0x89c4f3c)
#9  0x0830cc4c in pci_for_each_child (bus=bus@entry=0x89c4e50,
search=search@entry=0)
#10 0x0830c294 in pci_add_res_bus (type=2, bus=0x89c4e50)
#12 0x0830cc4c in pci_for_each_child (bus=bus@entry=0x89c4624,
search=search@entry=0)
d.c:27
#13 0x0830c294 in pci_add_res_bus (type=2, bus=0x89c4624)
#15 0x0830cc4c in pci_for_each_child (bus=bus@entry=0x89c4538,
search=search@entry=0)
#16 0x0830c294 in pci_add_res_bus (type=2, bus=0x89c4538)
#18 0x0830cc4c in pci_for_each_child (bus=bus@entry=0x857c9cc <pci_hb>,
search=search@entry=0)
#19 0x0830c8bc in pci_add_res_bus (bus=<optimized out>, type=2)
#20 pci_config_auto ()
#21 0x080678b4 in pciLibInit ()
```
Last edited on Jun 29, 2017 at 4:18:00 PM by Jeffrey Hill (previous) (diff)

### comment:2 Changed on Jun 29, 2017 at 4:16:00 PM by Jeffrey Hill

Last edited on Jun 29, 2017 at 4:17:37 PM by Jeffrey Hill (previous) (diff)

### comment:3 Changed on Jun 29, 2017 at 4:17:58 PM by Joel Sherrill

Owner: changed from joel.sherrill@… to Daniel Hellstrom new → assigned

Daniel Hellstrom wrote this code and should review this ticket.

Note: See TracTickets for help on using tickets.