Fix virtio memleaks
cover Fix virtio memleaks
The memory allocated in virtio_common_init() wasn't being freed anywhere. Fix that.
The balloon handler wasn't unregistering its savevm section, adding an exit handler fixes that as well.
This patchset is on top of the two balloon series I've sent out yesterday and today.
Amit Shah (4):
virtio-balloon: Add exit handler, fix memleaks
virtio-blk: Fix memleak on exit
virtio-net: Fix potential use-after-free
virtio: Plug memleak by freeing vdev
hw/virtio-balloon.c | 9 +++++++++ hw/virtio-blk.c | 1 + hw/virtio-net.c | 2 +- hw/virtio-pci.c | 11 ++++++++++- hw/virtio.c | 1 + hw/virtio.h | 1 + 6 files changed, 23 insertions(+), 2 deletions(-)
Signature · 1 line
1.7.6
PATCH 1/4 virtio-balloon: Add exit handler, fix memleaks
Add an exit handler that will free up RAM and unregister the savevm section after a virtio-balloon device is unplugged.
--- hw/virtio-balloon.c | 9 +++++++++ hw/virtio-pci.c | 11 ++++++++++- hw/virtio.h | 1 + 3 files changed, 20 insertions(+), 1 deletions(-)
hw/virtio-balloon.c +9 -0
@@ -45,6 +45,7 @@ typedef struct VirtIOBalloonsize_t stats_vq_offset;MonitorCompletion *stats_callback;void *stats_opaque_callback_data; +DeviceState *qdev; } VirtIOBalloon;static VirtIOBalloon *to_virtio_balloon (VirtIODevice *vdev)
@@ -293,8 +294,16 @@ VirtIODevice *virtio_balloon_init(DeviceState *dev)reset_stats (s); + s->qdev = dev;register_savevm (dev,"virtio-balloon" ,-1 ,1 , virtio_balloon_save, virtio_balloon_load, s);return &s->vdev; } + +void virtio_balloon_exit (VirtIODevice *vdev) +{ +VirtIOBalloon *s =DO_UPCAST (VirtIOBalloon, vdev, vdev); +unregister_savevm (s->qdev,"virtio-balloon" , s); +virtio_cleanup (vdev); +}
hw/virtio-pci.c +10 -1
@@ -795,6 +795,15 @@ static int virtio_balloon_init_pci(PCIDevice *pci_dev)return 0 ; } +static int virtio_balloon_exit_pci (PCIDevice *pci_dev) +{ +VirtIOPCIProxy *proxy =DO_UPCAST (VirtIOPCIProxy, pci_dev, pci_dev); + +virtio_pci_stop_ioeventfd (proxy); +virtio_balloon_exit (proxy->vdev); +return virtio_exit_pci (pci_dev); +} +static PCIDeviceInfo virtio_info[] = { { .qdev.name ="virtio-blk-pci" ,
@@ -869,7 +878,7 @@ static PCIDeviceInfo virtio_info[] = { .qdev.alias = "virtio-balloon", .qdev.size = sizeof(VirtIOPCIProxy), .init = virtio_balloon_init_pci, - .exit = virtio_exit_pci, + .exit = virtio_balloon_exit_pci, .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET, .device_id = PCI_DEVICE_ID_VIRTIO_BALLOON, .revision = VIRTIO_PCI_ABI_VERSION,
hw/virtio.h +1 -0
@@ -213,6 +213,7 @@ VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf);void virtio_net_exit (VirtIODevice *vdev);void virtio_blk_exit (VirtIODevice *vdev);void virtio_serial_exit (VirtIODevice *vdev); +void virtio_balloon_exit (VirtIODevice *vdev);#define DEFINE_VIRTIO_COMMON_FEATURES (_state, _field) \ DEFINE_PROP_BIT("indirect_desc", _state, _field, \
Signature · 1 line
1.7.6
PATCH 2/4 virtio-blk: Fix memleak on exit
In reply to Amit ShahCalling virtio_cleanup() will free up memory allocated in virtio_common_init().
--- hw/virtio-blk.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
hw/virtio-blk.c +1 -0
@@ -594,4 +594,5 @@ void virtio_blk_exit(VirtIODevice *vdev) {VirtIOBlock *s =to_virtio_blk (vdev);unregister_savevm (s->qdev,"virtio-blk" , s); +virtio_cleanup (vdev); }
Signature · 1 line
1.7.6
Re: [Qemu-devel] [PATCH 2/4] virtio-blk: Fix memleak on exit
Am 27.07.2011 10:30, schrieb Amit Shah:
Quoted 4 lines
Calling
virtio_cleanup()will free up memory allocated invirtio_common_init().Signed-off-by Amit Shah <[email protected]>
PATCH 3/4 virtio-net: Fix potential use-after-free
In reply to Amit Shahvirtio_cleanup() will remove the VirtIONet struct that gets allocated via virtio_common_init(). Ensure we don't dereference the structure after calling the cleanup function.
--- hw/virtio-net.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
hw/virtio-net.c +1 -1
@@ -1073,6 +1073,6 @@ void virtio_net_exit(VirtIODevice *vdev)qemu_bh_delete (n->tx_bh); } -virtio_cleanup (&n->vdev);qemu_del_vlan_client (&n->nic->nc); +virtio_cleanup (&n->vdev); }
Signature · 1 line
1.7.6
Re: [Qemu-devel] [PATCH 3/4] virtio-net: Fix potential use-after-free
Quoted 5 lines · Amit Shah
virtio_cleanup()will remove the VirtIONet struct that gets allocated viavirtio_common_init(). Ensure we don't dereference the structure after calling the cleanup function.Signed-off-by Amit Shah <[email protected]>
I see. It's not a use after free but will be once
you make virtio_cleanup free the vdev?
Quoted 18 lines
--- hw/virtio-net.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)hw/virtio-net.c +1
-1@@ -1073,6 +1073,6 @@ void virtio_net_exit(VirtIODevice *vdev)qemu_bh_delete (n->tx_bh); } -virtio_cleanup (&n->vdev);qemu_del_vlan_client (&n->nic->nc); +virtio_cleanup (&n->vdev); }Signature · 1 line
1.7.6
Re: [Qemu-devel] [PATCH 3/4] virtio-net: Fix potential use-after-free
Quoted 9 lines · Michael S. Tsirkin
Quoted 5 lines · Amit Shah
virtio_cleanup()will remove the VirtIONet struct that gets allocated viavirtio_common_init(). Ensure we don't dereference the structure after calling the cleanup function.Signed-off-by Amit Shah <[email protected]>I see. It's not a use after free but will be once
you makevirtio_cleanupfree the vdev?
Yes, the next patch.
Amit
PATCH 4/4 virtio: Plug memleak by freeing vdev
In reply to Amit Shahvirtio_common_init() allocates RAM for the vdev struct (and any additional memory, depending on the size passed to the function). This memory wasn't being freed until now.
--- hw/virtio.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
hw/virtio.c +1 -0
@@ -834,6 +834,7 @@ void virtio_cleanup(VirtIODevice *vdev)if (vdev->config)qemu_free (vdev->config);qemu_free (vdev->vq); +qemu_free (vdev); }static void virtio_vmstate_change (void *opaque,int running,int reason)
Signature · 1 line
1.7.6
Quoted 8 lines · Amit Shah
The memory allocated in
virtio_common_init()wasn't being freed anywhere. Fix that.The balloon handler wasn't unregistering its savevm section, adding an exit handler fixes that as well.
This patchset is on top of the two balloon series I've sent out yesterday and today.
This looks good to me. What do you say I put patches 2-4 on my tree, and you put patch 1 on yours?
Quoted 17 lines
Amit Shah (4):
virtio-balloon: Add exit handler, fix memleaks
virtio-blk: Fix memleak on exit
virtio-net: Fix potential use-after-free
virtio: Plug memleak by freeing vdevhw/virtio-balloon.c | 9 +++++++++ hw/virtio-blk.c | 1 + hw/virtio-net.c | 2 +- hw/virtio-pci.c | 11 ++++++++++- hw/virtio.c | 1 + hw/virtio.h | 1 + 6 files changed, 23 insertions(+), 2 deletions(-)Signature · 1 line
1.7.6
Quoted 4 lines · Michael S. Tsirkin
Quoted 3 lines · Amit Shah
The memory allocated in
virtio_common_init()wasn't being freed anywhere. Fix that.
Quoted 8 lines
Quoted 5 lines
The balloon handler wasn't unregistering its savevm section, adding an exit handler fixes that as well.
This patchset is on top of the two balloon series I've sent out yesterday and today.
This looks good to me. What do you say I put patches 2-4 on my tree, and you put patch 1 on yours?
Sure.
Amit