bus/pci: fix use after free on unplug
[dpdk.git] / drivers / bus / pci / pci_common.c
index 3406e03..4a3a87f 100644 (file)
@@ -226,16 +226,39 @@ rte_pci_probe_one_driver(struct rte_pci_driver *dr,
                        return -EINVAL;
                }
 
-               dev->driver = dr;
-       }
+               /* Allocate interrupt instance for pci device */
+               dev->intr_handle =
+                       rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_PRIVATE);
+               if (dev->intr_handle == NULL) {
+                       RTE_LOG(ERR, EAL,
+                               "Failed to create interrupt instance for %s\n",
+                               dev->device.name);
+                       return -ENOMEM;
+               }
 
-       if (!already_probed && (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)) {
-               /* map resources for devices that use igb_uio */
-               ret = rte_pci_map_device(dev);
-               if (ret != 0) {
-                       dev->driver = NULL;
-                       return ret;
+               dev->vfio_req_intr_handle =
+                       rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_PRIVATE);
+               if (dev->vfio_req_intr_handle == NULL) {
+                       rte_intr_instance_free(dev->intr_handle);
+                       dev->intr_handle = NULL;
+                       RTE_LOG(ERR, EAL,
+                               "Failed to create vfio req interrupt instance for %s\n",
+                               dev->device.name);
+                       return -ENOMEM;
+               }
+
+               if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
+                       ret = rte_pci_map_device(dev);
+                       if (ret != 0) {
+                               rte_intr_instance_free(dev->vfio_req_intr_handle);
+                               dev->vfio_req_intr_handle = NULL;
+                               rte_intr_instance_free(dev->intr_handle);
+                               dev->intr_handle = NULL;
+                               return ret;
+                       }
                }
+
+               dev->driver = dr;
        }
 
        RTE_LOG(INFO, EAL, "Probe PCI driver: %s (%x:%x) device: "PCI_PRI_FMT" (socket %i)\n",
@@ -255,6 +278,10 @@ rte_pci_probe_one_driver(struct rte_pci_driver *dr,
                        !(ret > 0 &&
                                (dr->drv_flags & RTE_PCI_DRV_KEEP_MAPPED_RES)))
                        rte_pci_unmap_device(dev);
+               rte_intr_instance_free(dev->vfio_req_intr_handle);
+               dev->vfio_req_intr_handle = NULL;
+               rte_intr_instance_free(dev->intr_handle);
+               dev->intr_handle = NULL;
        } else {
                dev->device.driver = &dr->driver;
        }
@@ -300,6 +327,11 @@ rte_pci_detach_dev(struct rte_pci_device *dev)
                /* unmap resources for devices that use igb_uio */
                rte_pci_unmap_device(dev);
 
+       rte_intr_instance_free(dev->intr_handle);
+       dev->intr_handle = NULL;
+       rte_intr_instance_free(dev->vfio_req_intr_handle);
+       dev->vfio_req_intr_handle = NULL;
+
        return 0;
 }