Skip to contentSkip to conversation

pci: fix resource leak in pci qdev and hot plug.

PATCH In review Isaku Yamahata 2 participants 6 messages Jun 22, 2010

lore
Share & tools
lore
#0 lore raw

v2 cover pci: fix resource leak in pci qdev and hot plug.

pci qdev exit code patch(pci_unregister_device) doesn't release all the resource allocated by pci qdev initialization.(pci_qdev_init) This patch series sorts it out and make pci::hotplug error code to the caller.

Isaku Yamahata (3):
pci: fix memory leak of PCIDevice::romfile.
pci: option rom clean up.
pci hotplug: make pci hotplug return value to caller.

 hw/pci.c |   26 ++++++++++++++++++++++----
 1 files changed, 22 insertions(+), 4 deletions(-)
#1 lore raw

PATCH v2 1/3 pci: fix memory leak of PCIDevice::romfile.

PCIDevice::romfile is allocatedi n pci_qdev_init(). But nowhere freed. Free it in do_pci_unregister_device().

---
 hw/pci.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
hw/pci.c +1 -0
index 7787005..7e5c539 100644
@@ -650,6 +650,7 @@ static void do_pci_unregister_device(PCIDevice *pci_dev)
     qemu_free_irqs(pci_dev->irq);
     pci_dev->bus->devices[pci_dev->devfn] = NULL;
     pci_config_free(pci_dev);
+    qemu_free(pci_dev->romfile);
 }
 
 PCIDevice *pci_register_device(PCIBus *bus, const char *name,
Signature · 1 line
1.6.6.1
#2 lore raw

PATCH v2 2/3 pci: option rom clean up.

In reply to Isaku Yamahata

When pci deivce is freed, the option rom isn't freed. Although qemu_ram_free() is nop right now in fact, pci layer should be prepared for it.

---
 hw/pci.c |   10 ++++++++++
 1 files changed, 10 insertions(+), 0 deletions(-)
hw/pci.c +10 -0
index 7e5c539..75acbd3 100644
@@ -74,6 +74,7 @@ static struct BusInfo pci_bus_info = {
 static void pci_update_mappings(PCIDevice *d);
 static void pci_set_irq(void *opaque, int irq_num, int level);
 static int pci_add_option_rom(PCIDevice *pdev);
+static void pci_del_option_rom(PCIDevice *pdev);
 
 static uint16_t pci_default_sub_vendor_id = PCI_SUBVENDOR_ID_REDHAT_QUMRANET;
 static uint16_t pci_default_sub_device_id = PCI_SUBDEVICE_ID_QEMU;
@@ -708,6 +709,7 @@ static int pci_unregister_device(DeviceState *dev)
         return ret;
 
     pci_unregister_io_regions(pci_dev);
+    pci_del_option_rom(pci_dev);
     do_pci_unregister_device(pci_dev);
     return 0;
 }
@@ -1759,6 +1761,14 @@ static int pci_add_option_rom(PCIDevice *pdev)
     return 0;
 }
 
+static void pci_del_option_rom(PCIDevice *pdev)
+{
+    if (pdev->rom_offset) {
+        qemu_ram_free(pdev->rom_offset);
+        pdev->rom_offset = 0;
+    }
+}
+
 /* Reserve space and add capability to the linked list in pci config space */
 int pci_add_capability_at_offset(PCIDevice *pdev, uint8_t cap_id,
                                  uint8_t offset, uint8_t size)
Signature · 1 line
1.6.6.1
#3 lore raw

PATCH v2 3/3 pci hotplug: make pci hotplug return value to caller.

In reply to Isaku Yamahata

make pci hotplug callback return value to caller. And when returning error, allocated resources are freed.

---
 hw/pci.c |   15 +++++++++++----
 1 files changed, 11 insertions(+), 4 deletions(-)
hw/pci.c +11 -4
index 75acbd3..76418c5 100644
@@ -1626,8 +1626,16 @@ static int pci_qdev_init(DeviceState *qdev, DeviceInfo *base)
         pci_dev->romfile = qemu_strdup(info->romfile);
     pci_add_option_rom(pci_dev);
 
-    if (qdev->hotplugged)
-        bus->hotplug(bus->hotplug_qdev, pci_dev, 1);
+    if (qdev->hotplugged) {
+        rc = bus->hotplug(bus->hotplug_qdev, pci_dev, 1);
+        if (rc != 0) {
+            int rc1 = pci_unregister_device(&pci_dev->qdev);
+            if (rc1) {
+                error_report("pci_unregister_device failed in pci_qdev_init.\n");
+            }
+            return rc;
+        }
+    }
     return 0;
 }
 
@@ -1635,8 +1643,7 @@ static int pci_unplug_device(DeviceState *qdev)
 {
     PCIDevice *dev = DO_UPCAST(PCIDevice, qdev, qdev);
 
-    dev->bus->hotplug(dev->bus->hotplug_qdev, dev, 0);
-    return 0;
+    return dev->bus->hotplug(dev->bus->hotplug_qdev, dev, 0);
 }
 
 void pci_qdev_register(PCIDeviceInfo *info)
Signature · 1 line
1.6.6.1
#4 lore raw

[Qemu-devel] Re: [PATCH v2 3/3] pci hotplug: make pci hotplug return value to caller.

Quoted 15 lines · Isaku Yamahata

make pci hotplug callback return value to caller. And when returning error, allocated resources are freed.

---
 hw/pci.c |   15 +++++++++++----
 1 files changed, 11 insertions(+), 4 deletions(-)
hw/pci.c +0 -0
index 75acbd3..76418c5 100644
@@ -1626,8 +1626,16 @@ static int pci_qdev_init(DeviceState *qdev, DeviceInfo *base)
         pci_dev->romfile = qemu_strdup(info->romfile);
     pci_add_option_rom(pci_dev);

hw/pci.c static int pci_qdev_init(DeviceState *qdev, DeviceInfo *base) · line 1635

Quoted 10 lines
hw/pci.c +7 -2
 
-    if (qdev->hotplugged)
-        bus->hotplug(bus->hotplug_qdev, pci_dev, 1);
+    if (qdev->hotplugged) {
+        rc = bus->hotplug(bus->hotplug_qdev, pci_dev, 1);
+        if (rc != 0) {
+            int rc1 = pci_unregister_device(&pci_dev->qdev);
+            if (rc1) {
+                error_report("pci_unregister_device failed in pci_qdev_init.\n");
+            }

This should be assert.

Quoted 18 lines
hw/pci.c +4 -2
+            return rc;
+        }
+    }
     return 0;
 }
 
@@ -1635,8 +1643,7 @@ static int pci_unplug_device(DeviceState *qdev)
 {
     PCIDevice *dev = DO_UPCAST(PCIDevice, qdev, qdev);
 
-    dev->bus->hotplug(dev->bus->hotplug_qdev, dev, 0);
-    return 0;
+    return dev->bus->hotplug(dev->bus->hotplug_qdev, dev, 0);
 }
 
 void pci_qdev_register(PCIDeviceInfo *info)
Signature · 1 line
1.6.6.1
#5 lore raw
In reply to Isaku Yamahata
Quoted 8 lines · Isaku Yamahata

pci qdev exit code patch(pci_unregister_device) doesn't release all the resource allocated by pci qdev initialization.(pci_qdev_init) This patch series sorts it out and make pci::hotplug error code to the caller.

Isaku Yamahata (3):
pci: fix memory leak of PCIDevice::romfile.
pci: option rom clean up.
pci hotplug: make pci hotplug return value to caller.

Applied (tweaked 3/3 to assert on unregister:
if we can not cleanup, we are in an unknown state).

Quoted 2 lines
 hw/pci.c |   26 ++++++++++++++++++++++----
 1 files changed, 22 insertions(+), 4 deletions(-)