Skip to contentSkip to conversation

virtio-pci: fix memory leak from device realization failure

PATCH In review Zheng Huang 2 participants 4 messages Feb 28, 2025 – Mar 10, 2025 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH 1/1 virtio-pci: fix memory leak from device realization failure

This commit adds failback routine for virtio_pci_realize to fix the memory leak of an address space and the virtio-net device object. If the realization of the device failed, the address space should be destroyed too.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2845

---
 hw/virtio/virtio-pci.c | 3 +++
 1 file changed, 3 insertions(+)
hw/virtio/virtio-pci.c +3 -0
index c773a9130c..4b0d8cd90a 100644
@@ -2266,6 +2266,9 @@ static void virtio_pci_realize(PCIDevice *pci_dev, Error **errp)
     virtio_pci_bus_new(&proxy->bus, sizeof(proxy->bus), proxy);
     if (k->realize) {
         k->realize(proxy, errp);
+        if (*errp) {
+            address_space_destroy(&proxy->modern_cfg_mem_as);
+        }
     }
 }
 
Signature · 1 line
2.34.1
#2 lore raw

Hi Zheng,

Quoted 18 lines · Zheng Huang

This commit adds failback routine for virtio_pci_realize to fix the memory leak of an address space and the virtio-net device object. If the realization of the device failed, the address space should be destroyed too.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2845

---
  hw/virtio/virtio-pci.c | 3 +++
  1 file changed, 3 insertions(+)
hw/virtio/virtio-pci.c +0 -0
index c773a9130c..4b0d8cd90a 100644

hw/virtio/virtio-pci.c static void virtio_pci_realize(PCIDevice *pci_dev, Error **errp) · line 2274

Quoted 9 lines
hw/virtio/virtio-pci.c +3 -0
@@ -2266,6 +2266,9 @@ static void virtio_pci_realize(PCIDevice *pci_dev, Error **errp)
      virtio_pci_bus_new(&proxy->bus, sizeof(proxy->bus), proxy);
      if (k->realize) {
          k->realize(proxy, errp);
+        if (*errp) {
+            address_space_destroy(&proxy->modern_cfg_mem_as);
+        }
      }
  }
 

I think instead we want to add an instance_init in virtio_pci_class_init and move the address_space_init call from virtio_pci_realize there.

Regards,

Phil.

#3 lore raw

Hi Philippe,

Quoted 34 lines · Philippe Mathieu-Daudé

Hi Zheng,

Quoted 26 lines · Zheng Huang

This commit adds failback routine for virtio_pci_realize to fix the memory leak of an address space and the virtio-net device object. If the realization of the device failed, the address space should be destroyed too.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2845

---
  hw/virtio/virtio-pci.c | 3 +++

  1 file changed, 3 insertions(+)
hw/virtio/virtio-pci.c +3 -0
index c773a9130c..4b0d8cd90a 100644
@@ -2266,6 +2266,9 @@ static void virtio_pci_realize(PCIDevice *pci_dev, Error **errp)
       virtio_pci_bus_new(&proxy->bus, sizeof(proxy->bus), proxy);
       if (k->realize) {
           k->realize(proxy, errp);
+        if (*errp) {
+            address_space_destroy(&proxy->modern_cfg_mem_as);
+        }
       }
   }
Quoted 1 line

I think instead we want to add an instance_init in virtio_pci_class_init and move the address_space_init call from virtio_pci_realize there.

Quoted 3 lines

Regards,

Phil.

I have reviewed the relevant code again and found that if address_space_init is moved into instance_init, it will not be able to take follow-up actions such as free the AS if device realization failed, thus failing to address the issue. Additionally, I referred to the code for AS initialization and destruction in other devices and found that they are managed in device realize and unrealize handlers. Therefore, I still believe the previous approach is a better choice.

If there are other potential solutions or considerations that I might have missed, please let me know. I'm looking forward to hearing your thoughts!

Best regards,

Zheng.

#4 lore raw
In reply to Philippe Mathieu-Daudé

Hi Philippe,

Quoted 34 lines · Philippe Mathieu-Daudé

Hi Zheng,

Quoted 26 lines · Zheng Huang

This commit adds failback routine for virtio_pci_realize to fix the memory leak of an address space and the virtio-net device object. If the realization of the device failed, the address space should be destroyed too.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2845

---
  hw/virtio/virtio-pci.c | 3 +++

  1 file changed, 3 insertions(+)
hw/virtio/virtio-pci.c +3 -0
index c773a9130c..4b0d8cd90a 100644
@@ -2266,6 +2266,9 @@ static void virtio_pci_realize(PCIDevice *pci_dev, Error **errp)
       virtio_pci_bus_new(&proxy->bus, sizeof(proxy->bus), proxy);
       if (k->realize) {
           k->realize(proxy, errp);
+        if (*errp) {
+            address_space_destroy(&proxy->modern_cfg_mem_as);
+        }
       }
   }
Quoted 1 line

I think instead we want to add an instance_init in virtio_pci_class_init and move the address_space_init call from virtio_pci_realize there.

Quoted 3 lines

Regards,

Phil.

I have reviewed the relevant code again and found that if address_space_init is moved into instance_init, it will not be able to take follow-up actions such as free the AS if device realization failed, thus failing to address the issue. Additionally, I referred to the code for AS initialization and destruction in other devices and found that they are managed in device realize and unrealize handlers. Therefore, I still believe the previous approach is a better choice.

If there are other potential solutions or considerations that I might have missed, please let me know. I'm looking forward to hearing your thoughts!

Sorry to bother you again, but I wanted to follow up on my previous email. Apologize if this is inconvenient

Best regards,

Zheng.