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

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