Blame SOURCES/kvm-test-bdrv-drain-bdrv_drain-works-with-cross-AioConte.patch

357786
From 6ad2df3cb4ec0fbabc5d3e1beab138a071415bba Mon Sep 17 00:00:00 2001
357786
From: Kevin Wolf <kwolf@redhat.com>
357786
Date: Fri, 14 Sep 2018 10:54:59 +0200
357786
Subject: [PATCH 08/49] test-bdrv-drain: bdrv_drain() works with
357786
 cross-AioContext events
357786
357786
RH-Author: Kevin Wolf <kwolf@redhat.com>
357786
Message-id: <20180914105540.18077-2-kwolf@redhat.com>
357786
Patchwork-id: 82152
357786
O-Subject: [RHV-7.6 qemu-kvm-rhev PATCH 01/42] test-bdrv-drain: bdrv_drain() works with cross-AioContext events
357786
Bugzilla: 1601212
357786
RH-Acked-by: John Snow <jsnow@redhat.com>
357786
RH-Acked-by: Max Reitz <mreitz@redhat.com>
357786
RH-Acked-by: Fam Zheng <famz@redhat.com>
357786
357786
As long as nobody keeps the other I/O thread from working, there is no
357786
reason why bdrv_drain() wouldn't work with cross-AioContext events. The
357786
key is that the root request we're waiting for is in the AioContext
357786
we're polling (which it always is for bdrv_drain()) so that aio_poll()
357786
is woken up in the end.
357786
357786
Add a test case that shows that it works. Remove the comment in
357786
bdrv_drain() that claims otherwise.
357786
357786
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
357786
(cherry picked from commit bb6756895459f181e2f25e877d3d7a10c297b5c8)
357786
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
357786
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
357786
---
357786
 block/io.c              |   4 --
357786
 tests/test-bdrv-drain.c | 187 +++++++++++++++++++++++++++++++++++++++++++++++-
357786
 2 files changed, 186 insertions(+), 5 deletions(-)
357786
357786
diff --git a/block/io.c b/block/io.c
357786
index bb617de..7e0a169 100644
357786
--- a/block/io.c
357786
+++ b/block/io.c
357786
@@ -369,10 +369,6 @@ void bdrv_unapply_subtree_drain(BdrvChild *child, BlockDriverState *old_parent)
357786
  *
357786
  * Note that unlike bdrv_drain_all(), the caller must hold the BlockDriverState
357786
  * AioContext.
357786
- *
357786
- * Only this BlockDriverState's AioContext is run, so in-flight requests must
357786
- * not depend on events in other AioContexts.  In that case, use
357786
- * bdrv_drain_all() instead.
357786
  */
357786
 void coroutine_fn bdrv_co_drain(BlockDriverState *bs)
357786
 {
357786
diff --git a/tests/test-bdrv-drain.c b/tests/test-bdrv-drain.c
357786
index 403446e..dee0a10 100644
357786
--- a/tests/test-bdrv-drain.c
357786
+++ b/tests/test-bdrv-drain.c
357786
@@ -27,9 +27,13 @@
357786
 #include "block/blockjob_int.h"
357786
 #include "sysemu/block-backend.h"
357786
 #include "qapi/error.h"
357786
+#include "iothread.h"
357786
+
357786
+static QemuEvent done_event;
357786
 
357786
 typedef struct BDRVTestState {
357786
     int drain_count;
357786
+    AioContext *bh_indirection_ctx;
357786
 } BDRVTestState;
357786
 
357786
 static void coroutine_fn bdrv_test_co_drain_begin(BlockDriverState *bs)
357786
@@ -50,16 +54,29 @@ static void bdrv_test_close(BlockDriverState *bs)
357786
     g_assert_cmpint(s->drain_count, >, 0);
357786
 }
357786
 
357786
+static void co_reenter_bh(void *opaque)
357786
+{
357786
+    aio_co_wake(opaque);
357786
+}
357786
+
357786
 static int coroutine_fn bdrv_test_co_preadv(BlockDriverState *bs,
357786
                                             uint64_t offset, uint64_t bytes,
357786
                                             QEMUIOVector *qiov, int flags)
357786
 {
357786
+    BDRVTestState *s = bs->opaque;
357786
+
357786
     /* We want this request to stay until the polling loop in drain waits for
357786
      * it to complete. We need to sleep a while as bdrv_drain_invoke() comes
357786
      * first and polls its result, too, but it shouldn't accidentally complete
357786
      * this request yet. */
357786
     qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 100000);
357786
 
357786
+    if (s->bh_indirection_ctx) {
357786
+        aio_bh_schedule_oneshot(s->bh_indirection_ctx, co_reenter_bh,
357786
+                                qemu_coroutine_self());
357786
+        qemu_coroutine_yield();
357786
+    }
357786
+
357786
     return 0;
357786
 }
357786
 
357786
@@ -490,6 +507,164 @@ static void test_graph_change(void)
357786
     blk_unref(blk_b);
357786
 }
357786
 
357786
+struct test_iothread_data {
357786
+    BlockDriverState *bs;
357786
+    enum drain_type drain_type;
357786
+    int *aio_ret;
357786
+};
357786
+
357786
+static void test_iothread_drain_entry(void *opaque)
357786
+{
357786
+    struct test_iothread_data *data = opaque;
357786
+
357786
+    aio_context_acquire(bdrv_get_aio_context(data->bs));
357786
+    do_drain_begin(data->drain_type, data->bs);
357786
+    g_assert_cmpint(*data->aio_ret, ==, 0);
357786
+    do_drain_end(data->drain_type, data->bs);
357786
+    aio_context_release(bdrv_get_aio_context(data->bs));
357786
+
357786
+    qemu_event_set(&done_event);
357786
+}
357786
+
357786
+static void test_iothread_aio_cb(void *opaque, int ret)
357786
+{
357786
+    int *aio_ret = opaque;
357786
+    *aio_ret = ret;
357786
+    qemu_event_set(&done_event);
357786
+}
357786
+
357786
+/*
357786
+ * Starts an AIO request on a BDS that runs in the AioContext of iothread 1.
357786
+ * The request involves a BH on iothread 2 before it can complete.
357786
+ *
357786
+ * @drain_thread = 0 means that do_drain_begin/end are called from the main
357786
+ * thread, @drain_thread = 1 means that they are called from iothread 1. Drain
357786
+ * for this BDS cannot be called from iothread 2 because only the main thread
357786
+ * may do cross-AioContext polling.
357786
+ */
357786
+static void test_iothread_common(enum drain_type drain_type, int drain_thread)
357786
+{
357786
+    BlockBackend *blk;
357786
+    BlockDriverState *bs;
357786
+    BDRVTestState *s;
357786
+    BlockAIOCB *acb;
357786
+    int aio_ret;
357786
+    struct test_iothread_data data;
357786
+
357786
+    IOThread *a = iothread_new();
357786
+    IOThread *b = iothread_new();
357786
+    AioContext *ctx_a = iothread_get_aio_context(a);
357786
+    AioContext *ctx_b = iothread_get_aio_context(b);
357786
+
357786
+    QEMUIOVector qiov;
357786
+    struct iovec iov = {
357786
+        .iov_base = NULL,
357786
+        .iov_len = 0,
357786
+    };
357786
+    qemu_iovec_init_external(&qiov, &iov, 1);
357786
+
357786
+    /* bdrv_drain_all() may only be called from the main loop thread */
357786
+    if (drain_type == BDRV_DRAIN_ALL && drain_thread != 0) {
357786
+        goto out;
357786
+    }
357786
+
357786
+    blk = blk_new(BLK_PERM_ALL, BLK_PERM_ALL);
357786
+    bs = bdrv_new_open_driver(&bdrv_test, "test-node", BDRV_O_RDWR,
357786
+                              &error_abort);
357786
+    s = bs->opaque;
357786
+    blk_insert_bs(blk, bs, &error_abort);
357786
+
357786
+    blk_set_aio_context(blk, ctx_a);
357786
+    aio_context_acquire(ctx_a);
357786
+
357786
+    s->bh_indirection_ctx = ctx_b;
357786
+
357786
+    aio_ret = -EINPROGRESS;
357786
+    if (drain_thread == 0) {
357786
+        acb = blk_aio_preadv(blk, 0, &qiov, 0, test_iothread_aio_cb, &aio_ret);
357786
+    } else {
357786
+        acb = blk_aio_preadv(blk, 0, &qiov, 0, aio_ret_cb, &aio_ret);
357786
+    }
357786
+    g_assert(acb != NULL);
357786
+    g_assert_cmpint(aio_ret, ==, -EINPROGRESS);
357786
+
357786
+    aio_context_release(ctx_a);
357786
+
357786
+    data = (struct test_iothread_data) {
357786
+        .bs         = bs,
357786
+        .drain_type = drain_type,
357786
+        .aio_ret    = &aio_ret,
357786
+    };
357786
+
357786
+    switch (drain_thread) {
357786
+    case 0:
357786
+        if (drain_type != BDRV_DRAIN_ALL) {
357786
+            aio_context_acquire(ctx_a);
357786
+        }
357786
+
357786
+        /* The request is running on the IOThread a. Draining its block device
357786
+         * will make sure that it has completed as far as the BDS is concerned,
357786
+         * but the drain in this thread can continue immediately after
357786
+         * bdrv_dec_in_flight() and aio_ret might be assigned only slightly
357786
+         * later. */
357786
+        qemu_event_reset(&done_event);
357786
+        do_drain_begin(drain_type, bs);
357786
+        g_assert_cmpint(bs->in_flight, ==, 0);
357786
+
357786
+        if (drain_type != BDRV_DRAIN_ALL) {
357786
+            aio_context_release(ctx_a);
357786
+        }
357786
+        qemu_event_wait(&done_event);
357786
+        if (drain_type != BDRV_DRAIN_ALL) {
357786
+            aio_context_acquire(ctx_a);
357786
+        }
357786
+
357786
+        g_assert_cmpint(aio_ret, ==, 0);
357786
+        do_drain_end(drain_type, bs);
357786
+
357786
+        if (drain_type != BDRV_DRAIN_ALL) {
357786
+            aio_context_release(ctx_a);
357786
+        }
357786
+        break;
357786
+    case 1:
357786
+        qemu_event_reset(&done_event);
357786
+        aio_bh_schedule_oneshot(ctx_a, test_iothread_drain_entry, &data);
357786
+        qemu_event_wait(&done_event);
357786
+        break;
357786
+    default:
357786
+        g_assert_not_reached();
357786
+    }
357786
+
357786
+    aio_context_acquire(ctx_a);
357786
+    blk_set_aio_context(blk, qemu_get_aio_context());
357786
+    aio_context_release(ctx_a);
357786
+
357786
+    bdrv_unref(bs);
357786
+    blk_unref(blk);
357786
+
357786
+out:
357786
+    iothread_join(a);
357786
+    iothread_join(b);
357786
+}
357786
+
357786
+static void test_iothread_drain_all(void)
357786
+{
357786
+    test_iothread_common(BDRV_DRAIN_ALL, 0);
357786
+    test_iothread_common(BDRV_DRAIN_ALL, 1);
357786
+}
357786
+
357786
+static void test_iothread_drain(void)
357786
+{
357786
+    test_iothread_common(BDRV_DRAIN, 0);
357786
+    test_iothread_common(BDRV_DRAIN, 1);
357786
+}
357786
+
357786
+static void test_iothread_drain_subtree(void)
357786
+{
357786
+    test_iothread_common(BDRV_SUBTREE_DRAIN, 0);
357786
+    test_iothread_common(BDRV_SUBTREE_DRAIN, 1);
357786
+}
357786
+
357786
 
357786
 typedef struct TestBlockJob {
357786
     BlockJob common;
357786
@@ -613,10 +788,13 @@ static void test_blockjob_drain_subtree(void)
357786
 
357786
 int main(int argc, char **argv)
357786
 {
357786
+    int ret;
357786
+
357786
     bdrv_init();
357786
     qemu_init_main_loop(&error_abort);
357786
 
357786
     g_test_init(&argc, &argv, NULL);
357786
+    qemu_event_init(&done_event, false);
357786
 
357786
     g_test_add_func("/bdrv-drain/driver-cb/drain_all", test_drv_cb_drain_all);
357786
     g_test_add_func("/bdrv-drain/driver-cb/drain", test_drv_cb_drain);
357786
@@ -643,10 +821,17 @@ int main(int argc, char **argv)
357786
     g_test_add_func("/bdrv-drain/multiparent", test_multiparent);
357786
     g_test_add_func("/bdrv-drain/graph-change", test_graph_change);
357786
 
357786
+    g_test_add_func("/bdrv-drain/iothread/drain_all", test_iothread_drain_all);
357786
+    g_test_add_func("/bdrv-drain/iothread/drain", test_iothread_drain);
357786
+    g_test_add_func("/bdrv-drain/iothread/drain_subtree",
357786
+                    test_iothread_drain_subtree);
357786
+
357786
     g_test_add_func("/bdrv-drain/blockjob/drain_all", test_blockjob_drain_all);
357786
     g_test_add_func("/bdrv-drain/blockjob/drain", test_blockjob_drain);
357786
     g_test_add_func("/bdrv-drain/blockjob/drain_subtree",
357786
                     test_blockjob_drain_subtree);
357786
 
357786
-    return g_test_run();
357786
+    ret = g_test_run();
357786
+    qemu_event_destroy(&done_event);
357786
+    return ret;
357786
 }
357786
-- 
357786
1.8.3.1
357786