virtio-pci: fix memory leak from device realization failure
A message that is not in the archive (sent off-list or not yet received): [email protected]
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
@@ -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
Hi Zheng,
Quoted 18 lines · Zheng Huang
This commit adds failback routine for
virtio_pci_realizeto 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
Signed-off-by Zheng Huang <[email protected]>--- hw/virtio/virtio-pci.c | 3 +++ 1 file changed, 3 insertions(+)hw/virtio/virtio-pci.c +0
-0
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.
Hi Philippe,
Quoted 34 lines · Philippe Mathieu-Daudé
Hi Zheng,
Quoted 26 lines · Zheng Huang
This commit adds failback routine for
virtio_pci_realizeto 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
Signed-off-by Zheng Huang <[email protected]>---
hw/virtio/virtio-pci.c| 3 +++1 file changed, 3 insertions(+)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); + } } }Quoted 1 line
I think instead we want to add an
instance_initinvirtio_pci_class_initand move theaddress_space_initcall fromvirtio_pci_realizethere.
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.
Hi Philippe,
Quoted 34 lines · Philippe Mathieu-Daudé
Hi Zheng,
Quoted 26 lines · Zheng Huang
This commit adds failback routine for
virtio_pci_realizeto 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
Signed-off-by Zheng Huang <[email protected]>---
hw/virtio/virtio-pci.c| 3 +++1 file changed, 3 insertions(+)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); + } } }Quoted 1 line
I think instead we want to add an
instance_initinvirtio_pci_class_initand move theaddress_space_initcall fromvirtio_pci_realizethere.
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.