Skip to content

Commit 4fb88c1

Browse files
Matthew Wilcoxjbarnes993
authored andcommitted
PCI: Make pci_scan_slot more robust
Yinghai pointed out that the new pci_scan_slot() crashes when called on an ARI-capable slot that is empty. Fix this by exiting early from pci_scan_slot if there is no device in the slot. Also make next_ari_func() robust against devices not existing in case the ARI capability is corrupt. ARI also requires that the devices be listed in order, so if we find a function listed that is out of order, stop scanning to prevent loops. Signed-off-by: Matthew Wilcox <matthew@wil.cx> Signed-off-by: Jesse Barnes <jbarnes@virtuousgeek.org>
1 parent 0bf01c3 commit 4fb88c1

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

‎drivers/pci/probe.c‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,11 +1222,19 @@ EXPORT_SYMBOL(pci_scan_single_device);
12221222
static unsigned next_ari_fn(struct pci_dev *dev, unsigned fn)
12231223
{
12241224
u16 cap;
1225-
unsigned pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI);
1225+
unsigned pos, next_fn;
1226+
1227+
if (!dev)
1228+
return 0;
1229+
1230+
pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI);
12261231
if (!pos)
12271232
return 0;
12281233
pci_read_config_word(dev, pos + 4, &cap);
1229-
return cap >> 8;
1234+
next_fn = cap >> 8;
1235+
if (next_fn <= fn)
1236+
return 0;
1237+
return next_fn;
12301238
}
12311239

12321240
static unsigned next_trad_fn(struct pci_dev *dev, unsigned fn)
@@ -1271,12 +1279,14 @@ int pci_scan_slot(struct pci_bus *bus, int devfn)
12711279
return 0; /* Already scanned the entire slot */
12721280

12731281
dev = pci_scan_single_device(bus, devfn);
1274-
if (dev && !dev->is_added) /* new device? */
1282+
if (!dev)
1283+
return 0;
1284+
if (!dev->is_added)
12751285
nr++;
12761286

12771287
if (pci_ari_enabled(bus))
12781288
next_fn = next_ari_fn;
1279-
else if (dev && dev->multifunction)
1289+
else if (dev->multifunction)
12801290
next_fn = next_trad_fn;
12811291

12821292
for (fn = next_fn(dev, 0); fn > 0; fn = next_fn(dev, fn)) {

0 commit comments

Comments
 (0)