yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
ddf19c
From fdd1f3bf672ad8bb0a6db896ec8cbc797c31da1f Mon Sep 17 00:00:00 2001
ddf19c
From: Sergio Lopez Pascual <slp@redhat.com>
ddf19c
Date: Wed, 24 Jun 2020 13:24:53 -0400
ddf19c
Subject: [PATCH 11/12] virtio-blk: On restart, process queued requests in the
ddf19c
 proper context
ddf19c
ddf19c
RH-Author: Sergio Lopez Pascual <slp@redhat.com>
ddf19c
Message-id: <20200624132453.111276-3-slp@redhat.com>
ddf19c
Patchwork-id: 97798
ddf19c
O-Subject: [RHEL-AV-8.2.1 qemu-kvm PATCH 2/2] virtio-blk: On restart, process queued requests in the proper context
ddf19c
Bugzilla:
ddf19c
RH-Acked-by: John Snow <jsnow@redhat.com>
ddf19c
RH-Acked-by: Michael S. Tsirkin <mst@redhat.com>
ddf19c
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
ddf19c
ddf19c
On restart, we were scheduling a BH to process queued requests, which
ddf19c
would run before starting up the data plane, leading to those requests
ddf19c
being assigned and started on coroutines on the main context.
ddf19c
ddf19c
This could cause requests to be wrongly processed in parallel from
ddf19c
different threads (the main thread and the iothread managing the data
ddf19c
plane), potentially leading to multiple issues.
ddf19c
ddf19c
For example, stopping and resuming a VM multiple times while the guest
ddf19c
is generating I/O on a virtio_blk device can trigger a crash with a
ddf19c
stack tracing looking like this one:
ddf19c
ddf19c
<------>
ddf19c
 Thread 2 (Thread 0x7ff736765700 (LWP 1062503)):
ddf19c
 #0  0x00005567a13b99d6 in iov_memset
ddf19c
     (iov=0x6563617073206f4e, iov_cnt=1717922848, offset=516096, fillc=0, bytes=7018105756081554803)
ddf19c
     at util/iov.c:69
ddf19c
 #1  0x00005567a13bab73 in qemu_iovec_memset
ddf19c
     (qiov=0x7ff73ec99748, offset=516096, fillc=0, bytes=7018105756081554803) at util/iov.c:530
ddf19c
 #2  0x00005567a12f411c in qemu_laio_process_completion (laiocb=0x7ff6512ee6c0) at block/linux-aio.c:86
ddf19c
 #3  0x00005567a12f42ff in qemu_laio_process_completions (s=0x7ff7182e8420) at block/linux-aio.c:217
ddf19c
 #4  0x00005567a12f480d in ioq_submit (s=0x7ff7182e8420) at block/linux-aio.c:323
ddf19c
 #5  0x00005567a12f43d9 in qemu_laio_process_completions_and_submit (s=0x7ff7182e8420)
ddf19c
     at block/linux-aio.c:236
ddf19c
 #6  0x00005567a12f44c2 in qemu_laio_poll_cb (opaque=0x7ff7182e8430) at block/linux-aio.c:267
ddf19c
 #7  0x00005567a13aed83 in run_poll_handlers_once (ctx=0x5567a2b58c70, timeout=0x7ff7367645f8)
ddf19c
     at util/aio-posix.c:520
ddf19c
 #8  0x00005567a13aee9f in run_poll_handlers (ctx=0x5567a2b58c70, max_ns=16000, timeout=0x7ff7367645f8)
ddf19c
     at util/aio-posix.c:562
ddf19c
 #9  0x00005567a13aefde in try_poll_mode (ctx=0x5567a2b58c70, timeout=0x7ff7367645f8)
ddf19c
     at util/aio-posix.c:597
ddf19c
 #10 0x00005567a13af115 in aio_poll (ctx=0x5567a2b58c70, blocking=true) at util/aio-posix.c:639
ddf19c
 #11 0x00005567a109acca in iothread_run (opaque=0x5567a2b29760) at iothread.c:75
ddf19c
 #12 0x00005567a13b2790 in qemu_thread_start (args=0x5567a2b694c0) at util/qemu-thread-posix.c:519
ddf19c
 #13 0x00007ff73eedf2de in start_thread () at /lib64/libpthread.so.0
ddf19c
 #14 0x00007ff73ec10e83 in clone () at /lib64/libc.so.6
ddf19c
ddf19c
 Thread 1 (Thread 0x7ff743986f00 (LWP 1062500)):
ddf19c
 #0  0x00005567a13b99d6 in iov_memset
ddf19c
     (iov=0x6563617073206f4e, iov_cnt=1717922848, offset=516096, fillc=0, bytes=7018105756081554803)
ddf19c
     at util/iov.c:69
ddf19c
 #1  0x00005567a13bab73 in qemu_iovec_memset
ddf19c
     (qiov=0x7ff73ec99748, offset=516096, fillc=0, bytes=7018105756081554803) at util/iov.c:530
ddf19c
 #2  0x00005567a12f411c in qemu_laio_process_completion (laiocb=0x7ff6512ee6c0) at block/linux-aio.c:86
ddf19c
 #3  0x00005567a12f42ff in qemu_laio_process_completions (s=0x7ff7182e8420) at block/linux-aio.c:217
ddf19c
 #4  0x00005567a12f480d in ioq_submit (s=0x7ff7182e8420) at block/linux-aio.c:323
ddf19c
 #5  0x00005567a12f4a2f in laio_do_submit (fd=19, laiocb=0x7ff5f4ff9ae0, offset=472363008, type=2)
ddf19c
     at block/linux-aio.c:375
ddf19c
 #6  0x00005567a12f4af2 in laio_co_submit
ddf19c
     (bs=0x5567a2b8c460, s=0x7ff7182e8420, fd=19, offset=472363008, qiov=0x7ff5f4ff9ca0, type=2)
ddf19c
     at block/linux-aio.c:394
ddf19c
 #7  0x00005567a12f1803 in raw_co_prw
ddf19c
     (bs=0x5567a2b8c460, offset=472363008, bytes=20480, qiov=0x7ff5f4ff9ca0, type=2)
ddf19c
     at block/file-posix.c:1892
ddf19c
 #8  0x00005567a12f1941 in raw_co_pwritev
ddf19c
     (bs=0x5567a2b8c460, offset=472363008, bytes=20480, qiov=0x7ff5f4ff9ca0, flags=0)
ddf19c
     at block/file-posix.c:1925
ddf19c
 #9  0x00005567a12fe3e1 in bdrv_driver_pwritev
ddf19c
     (bs=0x5567a2b8c460, offset=472363008, bytes=20480, qiov=0x7ff5f4ff9ca0, qiov_offset=0, flags=0)
ddf19c
     at block/io.c:1183
ddf19c
 #10 0x00005567a1300340 in bdrv_aligned_pwritev
ddf19c
     (child=0x5567a2b5b070, req=0x7ff5f4ff9db0, offset=472363008, bytes=20480, align=512, qiov=0x7ff72c0425b8, qiov_offset=0, flags=0) at block/io.c:1980
ddf19c
 #11 0x00005567a1300b29 in bdrv_co_pwritev_part
ddf19c
     (child=0x5567a2b5b070, offset=472363008, bytes=20480, qiov=0x7ff72c0425b8, qiov_offset=0, flags=0)
ddf19c
     at block/io.c:2137
ddf19c
 #12 0x00005567a12baba1 in qcow2_co_pwritev_task
ddf19c
     (bs=0x5567a2b92740, file_cluster_offset=472317952, offset=487305216, bytes=20480, qiov=0x7ff72c0425b8, qiov_offset=0, l2meta=0x0) at block/qcow2.c:2444
ddf19c
 #13 0x00005567a12bacdb in qcow2_co_pwritev_task_entry (task=0x5567a2b48540) at block/qcow2.c:2475
ddf19c
 #14 0x00005567a13167d8 in aio_task_co (opaque=0x5567a2b48540) at block/aio_task.c:45
ddf19c
 #15 0x00005567a13cf00c in coroutine_trampoline (i0=738245600, i1=32759) at util/coroutine-ucontext.c:115
ddf19c
 #16 0x00007ff73eb622e0 in __start_context () at /lib64/libc.so.6
ddf19c
 #17 0x00007ff6626f1350 in  ()
ddf19c
 #18 0x0000000000000000 in  ()
ddf19c
<------>
ddf19c
ddf19c
This is also known to cause crashes with this message (assertion
ddf19c
failed):
ddf19c
ddf19c
 aio_co_schedule: Co-routine was already scheduled in 'aio_co_schedule'
ddf19c
ddf19c
RHBZ: https://bugzilla.redhat.com/show_bug.cgi?id=1812765
ddf19c
Signed-off-by: Sergio Lopez <slp@redhat.com>
ddf19c
Message-Id: <20200603093240.40489-3-slp@redhat.com>
ddf19c
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
ddf19c
(cherry picked from commit 49b44549ace7890fffdf027fd3695218ee7f1121)
ddf19c
Signed-off-by: Sergio Lopez <slp@redhat.com>
ddf19c
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
ddf19c
---
ddf19c
 hw/block/dataplane/virtio-blk.c |  8 ++++++++
ddf19c
 hw/block/virtio-blk.c           | 18 ++++++++++++------
ddf19c
 include/hw/virtio/virtio-blk.h  |  2 +-
ddf19c
 3 files changed, 21 insertions(+), 7 deletions(-)
ddf19c
ddf19c
diff --git a/hw/block/dataplane/virtio-blk.c b/hw/block/dataplane/virtio-blk.c
ddf19c
index 119906a5fe..ac495fd72a 100644
ddf19c
--- a/hw/block/dataplane/virtio-blk.c
ddf19c
+++ b/hw/block/dataplane/virtio-blk.c
ddf19c
@@ -220,6 +220,9 @@ int virtio_blk_data_plane_start(VirtIODevice *vdev)
ddf19c
         goto fail_guest_notifiers;
ddf19c
     }
ddf19c
 
ddf19c
+    /* Process queued requests before the ones in vring */
ddf19c
+    virtio_blk_process_queued_requests(vblk, false);
ddf19c
+
ddf19c
     /* Kick right away to begin processing requests already in vring */
ddf19c
     for (i = 0; i < nvqs; i++) {
ddf19c
         VirtQueue *vq = virtio_get_queue(s->vdev, i);
ddf19c
@@ -239,6 +242,11 @@ int virtio_blk_data_plane_start(VirtIODevice *vdev)
ddf19c
     return 0;
ddf19c
 
ddf19c
   fail_guest_notifiers:
ddf19c
+    /*
ddf19c
+     * If we failed to set up the guest notifiers queued requests will be
ddf19c
+     * processed on the main context.
ddf19c
+     */
ddf19c
+    virtio_blk_process_queued_requests(vblk, false);
ddf19c
     vblk->dataplane_disabled = true;
ddf19c
     s->starting = false;
ddf19c
     vblk->dataplane_started = true;
ddf19c
diff --git a/hw/block/virtio-blk.c b/hw/block/virtio-blk.c
ddf19c
index 6ff29a05d6..493a263fa6 100644
ddf19c
--- a/hw/block/virtio-blk.c
ddf19c
+++ b/hw/block/virtio-blk.c
ddf19c
@@ -819,7 +819,7 @@ static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
ddf19c
     virtio_blk_handle_output_do(s, vq);
ddf19c
 }
ddf19c
 
ddf19c
-void virtio_blk_process_queued_requests(VirtIOBlock *s)
ddf19c
+void virtio_blk_process_queued_requests(VirtIOBlock *s, bool is_bh)
ddf19c
 {
ddf19c
     VirtIOBlockReq *req = s->rq;
ddf19c
     MultiReqBuffer mrb = {};
ddf19c
@@ -847,7 +847,9 @@ void virtio_blk_process_queued_requests(VirtIOBlock *s)
ddf19c
     if (mrb.num_reqs) {
ddf19c
         virtio_blk_submit_multireq(s->blk, &mrb);
ddf19c
     }
ddf19c
-    blk_dec_in_flight(s->conf.conf.blk);
ddf19c
+    if (is_bh) {
ddf19c
+        blk_dec_in_flight(s->conf.conf.blk);
ddf19c
+    }
ddf19c
     aio_context_release(blk_get_aio_context(s->conf.conf.blk));
ddf19c
 }
ddf19c
 
ddf19c
@@ -858,21 +860,25 @@ static void virtio_blk_dma_restart_bh(void *opaque)
ddf19c
     qemu_bh_delete(s->bh);
ddf19c
     s->bh = NULL;
ddf19c
 
ddf19c
-    virtio_blk_process_queued_requests(s);
ddf19c
+    virtio_blk_process_queued_requests(s, true);
ddf19c
 }
ddf19c
 
ddf19c
 static void virtio_blk_dma_restart_cb(void *opaque, int running,
ddf19c
                                       RunState state)
ddf19c
 {
ddf19c
     VirtIOBlock *s = opaque;
ddf19c
+    BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s)));
ddf19c
+    VirtioBusState *bus = VIRTIO_BUS(qbus);
ddf19c
 
ddf19c
     if (!running) {
ddf19c
         return;
ddf19c
     }
ddf19c
 
ddf19c
-    if (!s->bh) {
ddf19c
-        /* FIXME The data plane is not started yet, so these requests are
ddf19c
-         * processed in the main thread. */
ddf19c
+    /*
ddf19c
+     * If ioeventfd is enabled, don't schedule the BH here as queued
ddf19c
+     * requests will be processed while starting the data plane.
ddf19c
+     */
ddf19c
+    if (!s->bh && !virtio_bus_ioeventfd_enabled(bus)) {
ddf19c
         s->bh = aio_bh_new(blk_get_aio_context(s->conf.conf.blk),
ddf19c
                            virtio_blk_dma_restart_bh, s);
ddf19c
         blk_inc_in_flight(s->conf.conf.blk);
ddf19c
diff --git a/include/hw/virtio/virtio-blk.h b/include/hw/virtio/virtio-blk.h
ddf19c
index cf8eea2f58..e77f0db3b0 100644
ddf19c
--- a/include/hw/virtio/virtio-blk.h
ddf19c
+++ b/include/hw/virtio/virtio-blk.h
ddf19c
@@ -84,6 +84,6 @@ typedef struct MultiReqBuffer {
ddf19c
 } MultiReqBuffer;
ddf19c
 
ddf19c
 bool virtio_blk_handle_vq(VirtIOBlock *s, VirtQueue *vq);
ddf19c
-void virtio_blk_process_queued_requests(VirtIOBlock *s);
ddf19c
+void virtio_blk_process_queued_requests(VirtIOBlock *s, bool is_bh);
ddf19c
 
ddf19c
 #endif
ddf19c
-- 
ddf19c
2.27.0
ddf19c