Skip to content

Commit df25461

Browse files
dns-at-aristabjorn-helgaas
authored andcommitted
PCI: switchtec: Fix stdev_release() crash after surprise hot remove
A PCI device hot removal may occur while stdev->cdev is held open. The call to stdev_release() then happens during close or exit, at a point way past switchtec_pci_remove(). Otherwise the last ref would vanish with the trailing put_device(), just before return. At that later point in time, the devm cleanup has already removed the stdev->mmio_mrpc mapping. Also, the stdev->pdev reference was not a counted one. Therefore, in DMA mode, the iowrite32() in stdev_release() will cause a fatal page fault, and the subsequent dma_free_coherent(), if reached, would pass a stale &stdev->pdev->dev pointer. Fix by moving MRPC DMA shutdown into switchtec_pci_remove(), after stdev_kill(). Counting the stdev->pdev ref is now optional, but may prevent future accidents. Reproducible via the script at https://lore.kernel.org/r/[email protected] Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Daniel Stodden <[email protected]> Signed-off-by: Bjorn Helgaas <[email protected]> Reviewed-by: Logan Gunthorpe <[email protected]> Reviewed-by: Dmitry Safonov <[email protected]>
1 parent b85ea95 commit df25461

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

drivers/pci/switch/switchtec.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,13 +1308,6 @@ static void stdev_release(struct device *dev)
13081308
{
13091309
struct switchtec_dev *stdev = to_stdev(dev);
13101310

1311-
if (stdev->dma_mrpc) {
1312-
iowrite32(0, &stdev->mmio_mrpc->dma_en);
1313-
flush_wc_buf(stdev);
1314-
writeq(0, &stdev->mmio_mrpc->dma_addr);
1315-
dma_free_coherent(&stdev->pdev->dev, sizeof(*stdev->dma_mrpc),
1316-
stdev->dma_mrpc, stdev->dma_mrpc_dma_addr);
1317-
}
13181311
kfree(stdev);
13191312
}
13201313

@@ -1358,7 +1351,7 @@ static struct switchtec_dev *stdev_create(struct pci_dev *pdev)
13581351
return ERR_PTR(-ENOMEM);
13591352

13601353
stdev->alive = true;
1361-
stdev->pdev = pdev;
1354+
stdev->pdev = pci_dev_get(pdev);
13621355
INIT_LIST_HEAD(&stdev->mrpc_queue);
13631356
mutex_init(&stdev->mrpc_mutex);
13641357
stdev->mrpc_busy = 0;
@@ -1391,6 +1384,7 @@ static struct switchtec_dev *stdev_create(struct pci_dev *pdev)
13911384
return stdev;
13921385

13931386
err_put:
1387+
pci_dev_put(stdev->pdev);
13941388
put_device(&stdev->dev);
13951389
return ERR_PTR(rc);
13961390
}
@@ -1644,6 +1638,18 @@ static int switchtec_init_pci(struct switchtec_dev *stdev,
16441638
return 0;
16451639
}
16461640

1641+
static void switchtec_exit_pci(struct switchtec_dev *stdev)
1642+
{
1643+
if (stdev->dma_mrpc) {
1644+
iowrite32(0, &stdev->mmio_mrpc->dma_en);
1645+
flush_wc_buf(stdev);
1646+
writeq(0, &stdev->mmio_mrpc->dma_addr);
1647+
dma_free_coherent(&stdev->pdev->dev, sizeof(*stdev->dma_mrpc),
1648+
stdev->dma_mrpc, stdev->dma_mrpc_dma_addr);
1649+
stdev->dma_mrpc = NULL;
1650+
}
1651+
}
1652+
16471653
static int switchtec_pci_probe(struct pci_dev *pdev,
16481654
const struct pci_device_id *id)
16491655
{
@@ -1703,6 +1709,9 @@ static void switchtec_pci_remove(struct pci_dev *pdev)
17031709
ida_free(&switchtec_minor_ida, MINOR(stdev->dev.devt));
17041710
dev_info(&stdev->dev, "unregistered.\n");
17051711
stdev_kill(stdev);
1712+
switchtec_exit_pci(stdev);
1713+
pci_dev_put(stdev->pdev);
1714+
stdev->pdev = NULL;
17061715
put_device(&stdev->dev);
17071716
}
17081717

0 commit comments

Comments
 (0)