make init_assigned_device call free_assigned_device on error, and then make free_assigned_device is static because it's only invoked in device-assigned.c. Acked-by: Mark McLoughlin <markmc@xxxxxxxxxx> Signed-off-by: Weidong Han <weidong.han@xxxxxxxxx> --- qemu/hw/device-assignment.c | 12 ++++++++---- qemu/hw/device-assignment.h | 1 - qemu/hw/pci-hotplug.c | 1 - 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/qemu/hw/device-assignment.c b/qemu/hw/device-assignment.c index e6d2352..6046fdd 100644 --- a/qemu/hw/device-assignment.c +++ b/qemu/hw/device-assignment.c @@ -443,7 +443,7 @@ again: static LIST_HEAD(, AssignedDevInfo) adev_head; -void free_assigned_device(AssignedDevInfo *adev) +static void free_assigned_device(AssignedDevInfo *adev) { AssignedDevice *dev = adev->assigned_dev; @@ -558,14 +558,14 @@ struct PCIDevice *init_assigned_device(AssignedDevInfo *adev, PCIBus *bus) if (get_real_device(dev, adev->bus, adev->dev, adev->func)) { fprintf(stderr, "%s: Error: Couldn't get real device (%s)!\n", __func__, adev->name); - return NULL; + goto out; } /* handle real device's MMIO/PIO BARs */ if (assigned_dev_register_regions(dev->real_device.regions, dev->real_device.region_number, dev)) - return NULL; + goto out; /* handle interrupt routing */ e_device = (dev->dev.devfn >> 3) & 0x1f; @@ -595,10 +595,14 @@ struct PCIDevice *init_assigned_device(AssignedDevInfo *adev, PCIBus *bus) if (r < 0) { fprintf(stderr, "Failed to assign device \"%s\" : %s\n", adev->name, strerror(-r)); - return NULL; + goto out; } return &dev->dev; + +out: + free_assigned_device(adev); + return NULL; } /* diff --git a/qemu/hw/device-assignment.h b/qemu/hw/device-assignment.h index f216bb0..6a9b9fa 100644 --- a/qemu/hw/device-assignment.h +++ b/qemu/hw/device-assignment.h @@ -94,7 +94,6 @@ struct AssignedDevInfo { int disable_iommu; }; -void free_assigned_device(AssignedDevInfo *adev); PCIDevice *init_assigned_device(AssignedDevInfo *adev, PCIBus *bus); AssignedDevInfo *add_assigned_device(const char *arg); void add_assigned_devices(PCIBus *bus, const char **devices, int n_devices); diff --git a/qemu/hw/pci-hotplug.c b/qemu/hw/pci-hotplug.c index 8c76453..65fafd1 100644 --- a/qemu/hw/pci-hotplug.c +++ b/qemu/hw/pci-hotplug.c @@ -143,7 +143,6 @@ static PCIDevice *qemu_pci_hot_assign_device(PCIBus *pci_bus, const char *opts) ret = init_assigned_device(adev, pci_bus); if (ret == NULL) { term_printf("Failed to assign device\n"); - free_assigned_device(adev); return NULL; } -- 1.6.0.4
Attachment:
0005-kvm-qemu-free-device-on-error-in-init_assigned_dev.patch
Description: 0005-kvm-qemu-free-device-on-error-in-init_assigned_dev.patch