Skip to contentSkip to conversation

virtio-blk: fix issues with unified virtio-blk request handling

PATCH Applied Stefan Hajnoczi 2 participants 6 messages Jul 9, 2014

lore
Share & tools
lore
#0 lore raw

v3 cover virtio-blk: fix issues with unified virtio-blk request handling

v3:
* Add Christian's Tested-by: [Kevin]
* Resolved merge conflict in Patch 4 with qemu.git/master [Kevin]

This series fixes issues recently introduced when unifying virtio-blk dataplane's request handling with non-dataplane virtio-blk.

The problems include broken memory allocation for dataplane requests and a performance regression for non-dataplane. See the patches for details.

Stefan Hajnoczi (4):
virtio-blk: avoid dataplane VirtIOBlockReq early free
dataplane: do not free VirtQueueElement in vring_push()
virtio-blk: avoid g_slice_new0() for VirtIOBlockReq and
VirtQueueElement
virtio-blk: embed VirtQueueElement in VirtIOBlockReq

 hw/block/dataplane/virtio-blk.c     | 30 +++++++++++-----------
 hw/block/virtio-blk.c               | 50 ++++++++++++++++++-------------------
 hw/virtio/dataplane/vring.c         | 22 ++++++----------
 include/hw/virtio/dataplane/vring.h |  3 +--
 include/hw/virtio/virtio-blk.h      |  6 ++++-
 5 files changed, 53 insertions(+), 58 deletions(-)
Signature · 1 line
1.9.3
#1 lore raw

PATCH v3 1/4 virtio-blk: avoid dataplane VirtIOBlockReq early free

VirtIOBlockReq is freed later by virtio_blk_free_request() in hw/block/virtio-blk.c. Remove this extraneous g_slice_free().

This patch fixes the following segfault:

  0x00005555556373af in virtio_blk_rw_complete (opaque=0x5555565ff5e0, ret=0) at hw/block/virtio-blk.c:99
  99          bdrv_acct_done(req->dev->bs, &req->acct);
  (gdb) print req
  $1 = (VirtIOBlockReq *) 0x5555565ff5e0
  (gdb) print req->dev
  $2 = (VirtIOBlock *) 0x0
  (gdb) bt
  #0  0x00005555556373af in virtio_blk_rw_complete (opaque=0x5555565ff5e0, ret=0) at hw/block/virtio-blk.c:99
  #1  0x0000555555840ebe in bdrv_co_em_bh (opaque=0x5555566152d0) at block.c:4675
  #2  0x000055555583de77 in aio_bh_poll (ctx=ctx@entry=0x5555563a8150) at async.c:81
  #3  0x000055555584b7a7 in aio_poll (ctx=0x5555563a8150, blocking=blocking@entry=true) at aio-posix.c:188
  #4  0x00005555556e520e in iothread_run (opaque=0x5555563a7fd8) at iothread.c:41
  #5  0x00007ffff42ba124 in start_thread () from /usr/lib/libpthread.so.0
  #6  0x00007ffff16d14bd in clone () from /usr/lib/libc.so.6
---
 hw/block/dataplane/virtio-blk.c | 1 -
 1 file changed, 1 deletion(-)
hw/block/dataplane/virtio-blk.c +0 -1
index 4bc0729..bed9f13 100644
@@ -68,7 +68,6 @@ static void complete_request_vring(VirtIOBlockReq *req, unsigned char status)
     vring_push(&req->dev->dataplane->vring, req->elem,
                req->qiov.size + sizeof(*req->in));
     notify_guest(req->dev->dataplane);
-    g_slice_free(VirtIOBlockReq, req);
 }
 
 static void handle_notify(EventNotifier *e)
Signature · 1 line
1.9.3
#2 lore raw

PATCH v3 2/4 dataplane: do not free VirtQueueElement in vring_push()

In reply to Stefan Hajnoczi

VirtQueueElement is allocated in vring_pop() so it seems to make sense that vring_push() should free it. Alas, virtio-blk frees VirtQueueElement itself in virtio_blk_free_request().

This patch solves a double-free assertion in glib's g_slice_free().

Rename vring_free_element() to vring_unmap_element() since it no longer frees the VirtQueueElement.

---
 hw/virtio/dataplane/vring.c         | 9 ++++-----
 include/hw/virtio/dataplane/vring.h | 1 -
 2 files changed, 4 insertions(+), 6 deletions(-)
hw/virtio/dataplane/vring.c +4 -5
index 665a1ff..5d17d39 100644
@@ -272,7 +272,7 @@ static int get_indirect(Vring *vring, VirtQueueElement *elem,
     return 0;
 }
 
-void vring_free_element(VirtQueueElement *elem)
+static void vring_unmap_element(VirtQueueElement *elem)
 {
     int i;
 
@@ -287,8 +287,6 @@ void vring_free_element(VirtQueueElement *elem)
     for (i = 0; i < elem->in_num; i++) {
         vring_unmap(elem->in_sg[i].iov_base, true);
     }
-
-    g_slice_free(VirtQueueElement, elem);
 }
 
 /* This looks in the virtqueue and for the first available buffer, and converts
@@ -402,7 +400,8 @@ out:
         vring->broken = true;
     }
     if (elem) {
-        vring_free_element(elem);
+        vring_unmap_element(elem);
+        g_slice_free(VirtQueueElement, elem);
     }
     *p_elem = NULL;
     return ret;
@@ -418,7 +417,7 @@ void vring_push(Vring *vring, VirtQueueElement *elem, int len)
     unsigned int head = elem->index;
     uint16_t new;
 
-    vring_free_element(elem);
+    vring_unmap_element(elem);
 
     /* Don't touch vring if a fatal error occurred */
     if (vring->broken) {
include/hw/virtio/dataplane/vring.h +0 -1
index 63e7bf4..b23edd2 100644
@@ -55,6 +55,5 @@ bool vring_enable_notification(VirtIODevice *vdev, Vring *vring);
 bool vring_should_notify(VirtIODevice *vdev, Vring *vring);
 int vring_pop(VirtIODevice *vdev, Vring *vring, VirtQueueElement **elem);
 void vring_push(Vring *vring, VirtQueueElement *elem, int len);
-void vring_free_element(VirtQueueElement *elem);
 
 #endif /* VRING_H */
Signature · 1 line
1.9.3
#3 lore raw

PATCH v3 3/4 virtio-blk: avoid g_slice_new0() for VirtIOBlockReq and VirtQueueElement

In reply to Stefan Hajnoczi

In commit de6c8042ec55da18702fa51f09072fcaa315edc3 ("virtio-blk: Avoid zeroing every request structure") we avoided the 40 KB memset when allocating VirtIOBlockReq.

The memset was reintroduced in commit 671ec3f056559f22a2531a91dce3a258b9b5eb8a ("virtio-blk: Convert VirtIOBlockReq.elem to pointer").

It must be fixed again to avoid a performance regression.

---
 hw/block/virtio-blk.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
hw/block/virtio-blk.c +4 -2
index aec3146..b06a56d 100644
@@ -31,9 +31,11 @@
 
 static VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s)
 {
-    VirtIOBlockReq *req = g_slice_new0(VirtIOBlockReq);
+    VirtIOBlockReq *req = g_slice_new(VirtIOBlockReq);
     req->dev = s;
-    req->elem = g_slice_new0(VirtQueueElement);
+    req->qiov.size = 0;
+    req->next = NULL;
+    req->elem = g_slice_new(VirtQueueElement);
     return req;
 }
 
Signature · 1 line
1.9.3
#4 lore raw

PATCH v3 4/4 virtio-blk: embed VirtQueueElement in VirtIOBlockReq

In reply to Stefan Hajnoczi

The memory allocation between hw/block/virtio-blk.c, hw/block/dataplane/virtio-blk.c, and hw/virtio/dataplane/vring.c is messy. Structs are allocated in different files than they are freed in. This is risky and makes memory leaks easier.

Embed VirtQueueElement in VirtIOBlockReq to reduce the amount of memory allocation we need to juggle. This also makes vring.c and virtio.c slightly more similar.

---
 hw/block/dataplane/virtio-blk.c     | 29 +++++++++++------------
 hw/block/virtio-blk.c               | 46 ++++++++++++++++++-------------------
 hw/virtio/dataplane/vring.c         | 17 +++++---------
 include/hw/virtio/dataplane/vring.h |  2 +-
 include/hw/virtio/virtio-blk.h      |  6 ++++-
 5 files changed, 48 insertions(+), 52 deletions(-)
hw/block/dataplane/virtio-blk.c +14 -15
index bed9f13..227bb15 100644
@@ -65,7 +65,7 @@ static void complete_request_vring(VirtIOBlockReq *req, unsigned char status)
 {
     stb_p(&req->in->status, status);
 
-    vring_push(&req->dev->dataplane->vring, req->elem,
+    vring_push(&req->dev->dataplane->vring, &req->elem,
                req->qiov.size + sizeof(*req->in));
     notify_guest(req->dev->dataplane);
 }
@@ -74,33 +74,32 @@ static void handle_notify(EventNotifier *e)
 {
     VirtIOBlockDataPlane *s = container_of(e, VirtIOBlockDataPlane,
                                            host_notifier);
-
-    VirtQueueElement *elem;
-    VirtIOBlockReq *req;
-    int ret;
-    MultiReqBuffer mrb = {
-        .num_writes = 0,
-    };
+    VirtIOBlock *vblk = VIRTIO_BLK(s->vdev);
 
     event_notifier_test_and_clear(&s->host_notifier);
     bdrv_io_plug(s->blk->conf.bs);
     for (;;) {
+        MultiReqBuffer mrb = {
+            .num_writes = 0,
+        };
+        int ret;
+
         /* Disable guest->host notifies to avoid unnecessary vmexits */
         vring_disable_notification(s->vdev, &s->vring);
 
         for (;;) {
-            ret = vring_pop(s->vdev, &s->vring, &elem);
+            VirtIOBlockReq *req = virtio_blk_alloc_request(vblk);
+
+            ret = vring_pop(s->vdev, &s->vring, &req->elem);
             if (ret < 0) {
-                assert(elem == NULL);
+                virtio_blk_free_request(req);
                 break; /* no more requests */
             }
 
-            trace_virtio_blk_data_plane_process_request(s, elem->out_num,
-                                                        elem->in_num, elem->index);
+            trace_virtio_blk_data_plane_process_request(s, req->elem.out_num,
+                                                        req->elem.in_num,
+                                                        req->elem.index);
 
-            req = g_slice_new(VirtIOBlockReq);
-            req->dev = VIRTIO_BLK(s->vdev);
-            req->elem = elem;
             virtio_blk_handle_request(req, &mrb);
         }
 
hw/block/virtio-blk.c +22 -24

116 lines not on this page · raw message

hw/virtio/dataplane/vring.c +6 -11
index 5d17d39..67cb2b8 100644
@@ -301,14 +301,16 @@ static void vring_unmap_element(VirtQueueElement *elem)
  * Stolen from linux/drivers/vhost/vhost.c.
  */
 int vring_pop(VirtIODevice *vdev, Vring *vring,
-              VirtQueueElement **p_elem)
+              VirtQueueElement *elem)
 {
     struct vring_desc desc;
     unsigned int i, head, found = 0, num = vring->vr.num;
     uint16_t avail_idx, last_avail_idx;
-    VirtQueueElement *elem = NULL;
     int ret;
 
+    /* Initialize elem so it can be safely unmapped */
+    elem->in_num = elem->out_num = 0;
+
     /* If there was a fatal error then refuse operation */
     if (vring->broken) {
         ret = -EFAULT;
@@ -340,10 +342,8 @@ int vring_pop(VirtIODevice *vdev, Vring *vring,
      * the index we've seen. */
     head = vring->vr.avail->ring[last_avail_idx % num];
 
-    elem = g_slice_new(VirtQueueElement);
     elem->index = head;
-    elem->in_num = elem->out_num = 0;
-    
+
     /* If their number is silly, that's an error. */
     if (unlikely(head >= num)) {
         error_report("Guest says index %u > %u is available", head, num);
@@ -391,7 +391,6 @@ int vring_pop(VirtIODevice *vdev, Vring *vring,
 
     /* On success, increment avail index. */
     vring->last_avail_idx++;
-    *p_elem = elem;
     return head;
 
 out:
@@ -399,11 +398,7 @@ out:
     if (ret == -EFAULT) {
         vring->broken = true;
     }
-    if (elem) {
-        vring_unmap_element(elem);
-        g_slice_free(VirtQueueElement, elem);
-    }
-    *p_elem = NULL;
+    vring_unmap_element(elem);
     return ret;
 }
 
include/hw/virtio/dataplane/vring.h +1 -1
index b23edd2..af73ee2 100644
@@ -53,7 +53,7 @@ void vring_teardown(Vring *vring, VirtIODevice *vdev, int n);
 void vring_disable_notification(VirtIODevice *vdev, Vring *vring);
 bool vring_enable_notification(VirtIODevice *vdev, Vring *vring);
 bool vring_should_notify(VirtIODevice *vdev, Vring *vring);
-int vring_pop(VirtIODevice *vdev, Vring *vring, VirtQueueElement **elem);
+int vring_pop(VirtIODevice *vdev, Vring *vring, VirtQueueElement *elem);
 void vring_push(Vring *vring, VirtQueueElement *elem, int len);
 
 #endif /* VRING_H */
include/hw/virtio/virtio-blk.h +5 -1
index b3080a2..afb7b8d 100644
@@ -144,7 +144,7 @@ typedef struct MultiReqBuffer {
 
 typedef struct VirtIOBlockReq {
     VirtIOBlock *dev;
-    VirtQueueElement *elem;
+    VirtQueueElement elem;
     struct virtio_blk_inhdr *in;
     struct virtio_blk_outhdr out;
     QEMUIOVector qiov;
@@ -152,6 +152,10 @@ typedef struct VirtIOBlockReq {
     BlockAcctCookie acct;
 } VirtIOBlockReq;
 
+VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s);
+
+void virtio_blk_free_request(VirtIOBlockReq *req);
+
 int virtio_blk_handle_scsi_req(VirtIOBlock *blk,
                                VirtQueueElement *elem);
 
Signature · 1 line
1.9.3
Applied
#5 lore raw
In reply to Stefan Hajnoczi

Am 09.07.2014 um 10:05 hat Stefan Hajnoczi geschrieben:

Quoted 9 lines

v3:
* Add Christian's Tested-by: [Kevin]
* Resolved merge conflict in Patch 4 with qemu.git/master [Kevin]

This series fixes issues recently introduced when unifying virtio-blk dataplane's request handling with non-dataplane virtio-blk.

The problems include broken memory allocation for dataplane requests and a performance regression for non-dataplane. See the patches for details.

Thanks, applied to the block branch.

Kevin