Skip to contentSkip to conversation

delete virtio queues in vhost-user-blk-unrealize

PATCH Superseded [email protected] 3 participants 7 messages Feb 13, 2020 – Feb 25, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover delete virtio queues in vhost-user-blk-unrealize

From: Pan Nengyuan <[email protected]>

This series patch fix memleaks when detaching vhost-user-blk device.
1. use old virtio_del_queue to fix memleaks, it's easier for stable branches to merge. As the discussion in https://lists.nongnu.org/archive/html/qemu-devel/2020-01/msg02903.html

2. convert virtio_del_queue to the new one(virtio_delete_queue).

Pan Nengyuan (2):
vhost-user-blk: delete virtioqueues in unrealize to fix memleaks
vhost-use-blk: convert to new virtio_delete_queue

 hw/block/vhost-user-blk.c          | 15 +++++++++++++--
 include/hw/virtio/vhost-user-blk.h |  1 +
 2 files changed, 14 insertions(+), 2 deletions(-)
Signature · 1 line
2.21.0.windows.1
#1 lore raw

PATCH 1/2 vhost-user-blk: delete virtioqueues in unrealize to fix memleaks

From: Pan Nengyuan <[email protected]>

virtio queues forgot to delete in unrealize, and aslo error path in realize, this patch fix these memleaks, the leak stack is as follow:

Direct leak of 114688 byte(s) in 16 object(s) allocated from:

    #0 0x7f24024fdbf0 in calloc (/lib64/libasan.so.3+0xcabf0)
    #1 0x7f2401642015 in g_malloc0 (/lib64/libglib-2.0.so.0+0x50015)
    #2 0x55ad175a6447 in virtio_add_queue /mnt/sdb/qemu/hw/virtio/virtio.c:2327
    #3 0x55ad17570cf9 in vhost_user_blk_device_realize /mnt/sdb/qemu/hw/block/vhost-user-blk.c:419
    #4 0x55ad175a3707 in virtio_device_realize /mnt/sdb/qemu/hw/virtio/virtio.c:3509
    #5 0x55ad176ad0d1 in device_set_realized /mnt/sdb/qemu/hw/core/qdev.c:876
    #6 0x55ad1781ff9d in property_set_bool /mnt/sdb/qemu/qom/object.c:2080
    #7 0x55ad178245ae in object_property_set_qobject /mnt/sdb/qemu/qom/qom-qobject.c:26
    #8 0x55ad17821eb4 in object_property_set_bool /mnt/sdb/qemu/qom/object.c:1338
    #9 0x55ad177aeed7 in virtio_pci_realize /mnt/sdb/qemu/hw/virtio/virtio-pci.c:1801
---
 hw/block/vhost-user-blk.c | 8 ++++++++
 1 file changed, 8 insertions(+)
hw/block/vhost-user-blk.c +8 -0
index d8c459c575..2eba8b9db0 100644
@@ -460,6 +460,9 @@ reconnect:
 virtio_err:
     g_free(s->vqs);
     g_free(s->inflight);
+    for (i = 0; i < s->num_queues; i++) {
+        virtio_del_queue(vdev, i);
+    }
     virtio_cleanup(vdev);
     vhost_user_cleanup(&s->vhost_user);
 }
@@ -468,6 +471,7 @@ static void vhost_user_blk_device_unrealize(DeviceState *dev, Error **errp)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VHostUserBlk *s = VHOST_USER_BLK(dev);
+    int i;
 
     virtio_set_status(vdev, 0);
     qemu_chr_fe_set_handlers(&s->chardev,  NULL, NULL, NULL,
@@ -476,6 +480,10 @@ static void vhost_user_blk_device_unrealize(DeviceState *dev, Error **errp)
     vhost_dev_free_inflight(s->inflight);
     g_free(s->vqs);
     g_free(s->inflight);
+
+    for (i = 0; i < s->num_queues; i++) {
+        virtio_del_queue(vdev, i);
+    }
     virtio_cleanup(vdev);
     vhost_user_cleanup(&s->vhost_user);
 }
Signature · 1 line
2.21.0.windows.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] vhost-user-blk: delete virtioqueues in unrealize to fix memleaks

Quoted 17 lines · On Thu, Feb 13, 2020 at 09:28:06AM +0800, [email protected] wrote:

From: Pan Nengyuan <[email protected]>

virtio queues forgot to delete in unrealize, and aslo error path in realize, this patch fix these memleaks, the leak stack is as follow:

Direct leak of 114688 byte(s) in 16 object(s) allocated from:

    #0 0x7f24024fdbf0 in calloc (/lib64/libasan.so.3+0xcabf0)
    #1 0x7f2401642015 in g_malloc0 (/lib64/libglib-2.0.so.0+0x50015)
    #2 0x55ad175a6447 in virtio_add_queue /mnt/sdb/qemu/hw/virtio/virtio.c:2327
    #3 0x55ad17570cf9 in vhost_user_blk_device_realize /mnt/sdb/qemu/hw/block/vhost-user-blk.c:419
    #4 0x55ad175a3707 in virtio_device_realize /mnt/sdb/qemu/hw/virtio/virtio.c:3509
    #5 0x55ad176ad0d1 in device_set_realized /mnt/sdb/qemu/hw/core/qdev.c:876
    #6 0x55ad1781ff9d in property_set_bool /mnt/sdb/qemu/qom/object.c:2080
    #7 0x55ad178245ae in object_property_set_qobject /mnt/sdb/qemu/qom/qom-qobject.c:26
    #8 0x55ad17821eb4 in object_property_set_bool /mnt/sdb/qemu/qom/object.c:1338
    #9 0x55ad177aeed7 in virtio_pci_realize /mnt/sdb/qemu/hw/virtio/virtio-pci.c:1801
Quoted 5 lines
---
 hw/block/vhost-user-blk.c | 8 ++++++++
 1 file changed, 8 insertions(+)
#3 lore raw

PATCH 2/2 vhost-use-blk: convert to new virtio_delete_queue

In reply to [email protected]

From: Pan Nengyuan <[email protected]>

use the new virtio_delete_queue function to cleanup.

---
 hw/block/vhost-user-blk.c          | 11 +++++++----
 include/hw/virtio/vhost-user-blk.h |  1 +
 2 files changed, 8 insertions(+), 4 deletions(-)
hw/block/vhost-user-blk.c +7 -4
index 2eba8b9db0..ed6a5cc03b 100644
@@ -420,9 +420,10 @@ static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp)
     virtio_init(vdev, "virtio-blk", VIRTIO_ID_BLOCK,
                 sizeof(struct virtio_blk_config));
 
+    s->virtqs = g_new0(VirtQueue *, s->num_queues);
     for (i = 0; i < s->num_queues; i++) {
-        virtio_add_queue(vdev, s->queue_size,
-                         vhost_user_blk_handle_output);
+        s->virtqs[i] = virtio_add_queue(vdev, s->queue_size,
+                                        vhost_user_blk_handle_output);
     }
 
     s->inflight = g_new0(struct vhost_inflight, 1);
@@ -461,8 +462,9 @@ virtio_err:
     g_free(s->vqs);
     g_free(s->inflight);
     for (i = 0; i < s->num_queues; i++) {
-        virtio_del_queue(vdev, i);
+        virtio_delete_queue(s->virtqs[i]);
     }
+    g_free(s->virtqs);
     virtio_cleanup(vdev);
     vhost_user_cleanup(&s->vhost_user);
 }
@@ -482,8 +484,9 @@ static void vhost_user_blk_device_unrealize(DeviceState *dev, Error **errp)
     g_free(s->inflight);
 
     for (i = 0; i < s->num_queues; i++) {
-        virtio_del_queue(vdev, i);
+        virtio_delete_queue(s->virtqs[i]);
     }
+    g_free(s->virtqs);
     virtio_cleanup(vdev);
     vhost_user_cleanup(&s->vhost_user);
 }
include/hw/virtio/vhost-user-blk.h +1 -0
index 108bfadeeb..f68911f6f0 100644
@@ -37,6 +37,7 @@ typedef struct VHostUserBlk {
     struct vhost_inflight *inflight;
     VhostUserState vhost_user;
     struct vhost_virtqueue *vqs;
+    VirtQueue **virtqs;
     guint watch;
     bool connected;
 } VHostUserBlk;
Signature · 1 line
2.21.0.windows.1
#4 lore raw

Re: [PATCH 2/2] vhost-use-blk: convert to new virtio_delete_queue

hw/block/vhost-user-blk.c static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp) · line 423

Quoted 9 lines · On Thu, Feb 13, 2020 at 09:28:07AM +0800, [email protected] wrote:
hw/block/vhost-user-blk.c +1 -0
index 2eba8b9db0..ed6a5cc03b 100644
@@ -420,9 +420,10 @@ static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp)
     virtio_init(vdev, "virtio-blk", VIRTIO_ID_BLOCK,
                 sizeof(struct virtio_blk_config));
 
+    s->virtqs = g_new0(VirtQueue *, s->num_queues);

Minor point, up to you if you want to change it: the array is fully initialized by the for loop in the next line. There is no need to clear the memory first:

s/g_new0/g_new/

include/hw/virtio/vhost-user-blk.h typedef struct VHostUserBlk { · line 40

Quoted 9 lines
include/hw/virtio/vhost-user-blk.h +1 -0
index 108bfadeeb..f68911f6f0 100644
@@ -37,6 +37,7 @@ typedef struct VHostUserBlk {
     struct vhost_inflight *inflight;
     VhostUserState vhost_user;
     struct vhost_virtqueue *vqs;
+    VirtQueue **virtqs;

Both vqs and virtqs exist and are easily confused. Please rename vqs to vhost_vqs.

#5 lore raw

Re: [PATCH 2/2] vhost-use-blk: convert to new virtio_delete_queue

Quoted 9 lines · Stefan Hajnoczi
Quoted 8 lines · On Thu, Feb 13, 2020 at 09:28:07AM +0800, [email protected] wrote:
hw/block/vhost-user-blk.c +0 -0
index 2eba8b9db0..ed6a5cc03b 100644
@@ -420,9 +420,10 @@ static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp)
     virtio_init(vdev, "virtio-blk", VIRTIO_ID_BLOCK,
                 sizeof(struct virtio_blk_config));
 
Quoted 7 lines

hw/block/vhost-user-blk.c static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp) · line 423

Quoted 1 line
hw/block/vhost-user-blk.c +1 -0
+    s->virtqs = g_new0(VirtQueue *, s->num_queues);

Minor point, up to you if you want to change it: the array is fully initialized by the for loop in the next line. There is no need to clear the memory first:

s/g_new0/g_new/

OK, it's fine, I will change it.

Thanks.

Quoted 13 lines

include/hw/virtio/vhost-user-blk.h typedef struct VHostUserBlk { · line 40

Quoted 10 lines
include/hw/virtio/vhost-user-blk.h +1 -0
index 108bfadeeb..f68911f6f0 100644
@@ -37,6 +37,7 @@ typedef struct VHostUserBlk {
     struct vhost_inflight *inflight;
     VhostUserState vhost_user;
     struct vhost_virtqueue *vqs;
+    VirtQueue **virtqs;

Both vqs and virtqs exist and are easily confused. Please rename vqs to vhost_vqs.

OK, I will do it.

Thanks.

Quoted 1 line
#6 lore raw

Re: [PATCH 2/2] vhost-use-blk: convert to new virtio_delete_queue

In reply to [email protected]
Quoted 5 lines · On Thu, Feb 13, 2020 at 09:28:07AM +0800, [email protected] wrote:

From: Pan Nengyuan <[email protected]>

use the new virtio_delete_queue function to cleanup.

typo in subject use-blk->user-blk

Quoted 59 lines
---
 hw/block/vhost-user-blk.c          | 11 +++++++----
 include/hw/virtio/vhost-user-blk.h |  1 +
 2 files changed, 8 insertions(+), 4 deletions(-)
hw/block/vhost-user-blk.c +7 -4
index 2eba8b9db0..ed6a5cc03b 100644
@@ -420,9 +420,10 @@ static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp)
     virtio_init(vdev, "virtio-blk", VIRTIO_ID_BLOCK,
                 sizeof(struct virtio_blk_config));
 
+    s->virtqs = g_new0(VirtQueue *, s->num_queues);
     for (i = 0; i < s->num_queues; i++) {
-        virtio_add_queue(vdev, s->queue_size,
-                         vhost_user_blk_handle_output);
+        s->virtqs[i] = virtio_add_queue(vdev, s->queue_size,
+                                        vhost_user_blk_handle_output);
     }
 
     s->inflight = g_new0(struct vhost_inflight, 1);
@@ -461,8 +462,9 @@ virtio_err:
     g_free(s->vqs);
     g_free(s->inflight);
     for (i = 0; i < s->num_queues; i++) {
-        virtio_del_queue(vdev, i);
+        virtio_delete_queue(s->virtqs[i]);
     }
+    g_free(s->virtqs);
     virtio_cleanup(vdev);
     vhost_user_cleanup(&s->vhost_user);
 }
@@ -482,8 +484,9 @@ static void vhost_user_blk_device_unrealize(DeviceState *dev, Error **errp)
     g_free(s->inflight);
 
     for (i = 0; i < s->num_queues; i++) {
-        virtio_del_queue(vdev, i);
+        virtio_delete_queue(s->virtqs[i]);
     }
+    g_free(s->virtqs);
     virtio_cleanup(vdev);
     vhost_user_cleanup(&s->vhost_user);
 }
include/hw/virtio/vhost-user-blk.h +1 -0
index 108bfadeeb..f68911f6f0 100644
@@ -37,6 +37,7 @@ typedef struct VHostUserBlk {
     struct vhost_inflight *inflight;
     VhostUserState vhost_user;
     struct vhost_virtqueue *vqs;
+    VirtQueue **virtqs;
     guint watch;
     bool connected;
 } VHostUserBlk;
Signature · 1 line
2.21.0.windows.1