Blame SOURCES/kvm-test-bdrv-drain-Test-that-bdrv_drain_invoke-doesn-t-.patch

357786
From eae61f64dccec3d6e32a7ba8d934da629fdd8595 Mon Sep 17 00:00:00 2001
357786
From: Kevin Wolf <kwolf@redhat.com>
357786
Date: Fri, 14 Sep 2018 10:55:13 +0200
357786
Subject: [PATCH 22/49] test-bdrv-drain: Test that bdrv_drain_invoke() doesn't
357786
 poll
357786
357786
RH-Author: Kevin Wolf <kwolf@redhat.com>
357786
Message-id: <20180914105540.18077-16-kwolf@redhat.com>
357786
Patchwork-id: 82167
357786
O-Subject: [RHV-7.6 qemu-kvm-rhev PATCH 15/42] test-bdrv-drain: Test that bdrv_drain_invoke() doesn't poll
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
This adds a test case that goes wrong if bdrv_drain_invoke() calls
357786
aio_poll().
357786
357786
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
357786
(cherry picked from commit 57320ca961c2e8488e1884b4ebbcb929b6901dc6)
357786
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
357786
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
357786
---
357786
 tests/test-bdrv-drain.c | 102 +++++++++++++++++++++++++++++++++++++++++-------
357786
 1 file changed, 88 insertions(+), 14 deletions(-)
357786
357786
diff --git a/tests/test-bdrv-drain.c b/tests/test-bdrv-drain.c
357786
index f786326..c4aa913 100644
357786
--- a/tests/test-bdrv-drain.c
357786
+++ b/tests/test-bdrv-drain.c
357786
@@ -34,12 +34,16 @@ static QemuEvent done_event;
357786
 typedef struct BDRVTestState {
357786
     int drain_count;
357786
     AioContext *bh_indirection_ctx;
357786
+    bool sleep_in_drain_begin;
357786
 } BDRVTestState;
357786
 
357786
 static void coroutine_fn bdrv_test_co_drain_begin(BlockDriverState *bs)
357786
 {
357786
     BDRVTestState *s = bs->opaque;
357786
     s->drain_count++;
357786
+    if (s->sleep_in_drain_begin) {
357786
+        qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, 100000);
357786
+    }
357786
 }
357786
 
357786
 static void coroutine_fn bdrv_test_co_drain_end(BlockDriverState *bs)
357786
@@ -80,6 +84,22 @@ static int coroutine_fn bdrv_test_co_preadv(BlockDriverState *bs,
357786
     return 0;
357786
 }
357786
 
357786
+static void bdrv_test_child_perm(BlockDriverState *bs, BdrvChild *c,
357786
+                                 const BdrvChildRole *role,
357786
+                                 BlockReopenQueue *reopen_queue,
357786
+                                 uint64_t perm, uint64_t shared,
357786
+                                 uint64_t *nperm, uint64_t *nshared)
357786
+{
357786
+    /* bdrv_format_default_perms() accepts only these two, so disguise
357786
+     * detach_by_driver_cb_role as one of them. */
357786
+    if (role != &child_file && role != &child_backing) {
357786
+        role = &child_file;
357786
+    }
357786
+
357786
+    bdrv_format_default_perms(bs, c, role, reopen_queue, perm, shared,
357786
+                              nperm, nshared);
357786
+}
357786
+
357786
 static BlockDriver bdrv_test = {
357786
     .format_name            = "test",
357786
     .instance_size          = sizeof(BDRVTestState),
357786
@@ -90,7 +110,7 @@ static BlockDriver bdrv_test = {
357786
     .bdrv_co_drain_begin    = bdrv_test_co_drain_begin,
357786
     .bdrv_co_drain_end      = bdrv_test_co_drain_end,
357786
 
357786
-    .bdrv_child_perm        = bdrv_format_default_perms,
357786
+    .bdrv_child_perm        = bdrv_test_child_perm,
357786
 };
357786
 
357786
 static void aio_ret_cb(void *opaque, int ret)
357786
@@ -982,13 +1002,14 @@ struct detach_by_parent_data {
357786
     BdrvChild *child_b;
357786
     BlockDriverState *c;
357786
     BdrvChild *child_c;
357786
+    bool by_parent_cb;
357786
 };
357786
+static struct detach_by_parent_data detach_by_parent_data;
357786
 
357786
-static void detach_by_parent_aio_cb(void *opaque, int ret)
357786
+static void detach_indirect_bh(void *opaque)
357786
 {
357786
     struct detach_by_parent_data *data = opaque;
357786
 
357786
-    g_assert_cmpint(ret, ==, 0);
357786
     bdrv_unref_child(data->parent_b, data->child_b);
357786
 
357786
     bdrv_ref(data->c);
357786
@@ -996,6 +1017,25 @@ static void detach_by_parent_aio_cb(void *opaque, int ret)
357786
                                       &child_file, &error_abort);
357786
 }
357786
 
357786
+static void detach_by_parent_aio_cb(void *opaque, int ret)
357786
+{
357786
+    struct detach_by_parent_data *data = &detach_by_parent_data;
357786
+
357786
+    g_assert_cmpint(ret, ==, 0);
357786
+    if (data->by_parent_cb) {
357786
+        detach_indirect_bh(data);
357786
+    }
357786
+}
357786
+
357786
+static void detach_by_driver_cb_drained_begin(BdrvChild *child)
357786
+{
357786
+    aio_bh_schedule_oneshot(qemu_get_current_aio_context(),
357786
+                            detach_indirect_bh, &detach_by_parent_data);
357786
+    child_file.drained_begin(child);
357786
+}
357786
+
357786
+static BdrvChildRole detach_by_driver_cb_role;
357786
+
357786
 /*
357786
  * Initial graph:
357786
  *
357786
@@ -1003,17 +1043,25 @@ static void detach_by_parent_aio_cb(void *opaque, int ret)
357786
  *    \ /   \
357786
  *     A     B     C
357786
  *
357786
- * PA has a pending write request whose callback changes the child nodes of PB:
357786
- * It removes B and adds C instead. The subtree of PB is drained, which will
357786
- * indirectly drain the write request, too.
357786
+ * by_parent_cb == true:  Test that parent callbacks don't poll
357786
+ *
357786
+ *     PA has a pending write request whose callback changes the child nodes of
357786
+ *     PB: It removes B and adds C instead. The subtree of PB is drained, which
357786
+ *     will indirectly drain the write request, too.
357786
+ *
357786
+ * by_parent_cb == false: Test that bdrv_drain_invoke() doesn't poll
357786
+ *
357786
+ *     PA's BdrvChildRole has a .drained_begin callback that schedules a BH
357786
+ *     that does the same graph change. If bdrv_drain_invoke() calls it, the
357786
+ *     state is messed up, but if it is only polled in the single
357786
+ *     BDRV_POLL_WHILE() at the end of the drain, this should work fine.
357786
  */
357786
-static void test_detach_by_parent_cb(void)
357786
+static void test_detach_indirect(bool by_parent_cb)
357786
 {
357786
     BlockBackend *blk;
357786
     BlockDriverState *parent_a, *parent_b, *a, *b, *c;
357786
     BdrvChild *child_a, *child_b;
357786
     BlockAIOCB *acb;
357786
-    struct detach_by_parent_data data;
357786
 
357786
     QEMUIOVector qiov;
357786
     struct iovec iov = {
357786
@@ -1022,6 +1070,12 @@ static void test_detach_by_parent_cb(void)
357786
     };
357786
     qemu_iovec_init_external(&qiov, &iov, 1);
357786
 
357786
+    if (!by_parent_cb) {
357786
+        detach_by_driver_cb_role = child_file;
357786
+        detach_by_driver_cb_role.drained_begin =
357786
+            detach_by_driver_cb_drained_begin;
357786
+    }
357786
+
357786
     /* Create all involved nodes */
357786
     parent_a = bdrv_new_open_driver(&bdrv_test, "parent-a", BDRV_O_RDWR,
357786
                                     &error_abort);
357786
@@ -1037,6 +1091,13 @@ static void test_detach_by_parent_cb(void)
357786
     blk_insert_bs(blk, parent_a, &error_abort);
357786
     bdrv_unref(parent_a);
357786
 
357786
+    /* If we want to get bdrv_drain_invoke() to call aio_poll(), the driver
357786
+     * callback must not return immediately. */
357786
+    if (!by_parent_cb) {
357786
+        BDRVTestState *s = parent_a->opaque;
357786
+        s->sleep_in_drain_begin = true;
357786
+    }
357786
+
357786
     /* Set child relationships */
357786
     bdrv_ref(b);
357786
     bdrv_ref(a);
357786
@@ -1044,7 +1105,9 @@ static void test_detach_by_parent_cb(void)
357786
     child_a = bdrv_attach_child(parent_b, a, "PB-A", &child_backing, &error_abort);
357786
 
357786
     bdrv_ref(a);
357786
-    bdrv_attach_child(parent_a, a, "PA-A", &child_file, &error_abort);
357786
+    bdrv_attach_child(parent_a, a, "PA-A",
357786
+                      by_parent_cb ? &child_file : &detach_by_driver_cb_role,
357786
+                      &error_abort);
357786
 
357786
     g_assert_cmpint(parent_a->refcnt, ==, 1);
357786
     g_assert_cmpint(parent_b->refcnt, ==, 1);
357786
@@ -1057,18 +1120,19 @@ static void test_detach_by_parent_cb(void)
357786
     g_assert(QLIST_NEXT(child_b, next) == NULL);
357786
 
357786
     /* Start the evil write request */
357786
-    data = (struct detach_by_parent_data) {
357786
+    detach_by_parent_data = (struct detach_by_parent_data) {
357786
         .parent_b = parent_b,
357786
         .child_b = child_b,
357786
         .c = c,
357786
+        .by_parent_cb = by_parent_cb,
357786
     };
357786
-    acb = blk_aio_preadv(blk, 0, &qiov, 0, detach_by_parent_aio_cb, &data);
357786
+    acb = blk_aio_preadv(blk, 0, &qiov, 0, detach_by_parent_aio_cb, NULL);
357786
     g_assert(acb != NULL);
357786
 
357786
     /* Drain and check the expected result */
357786
     bdrv_subtree_drained_begin(parent_b);
357786
 
357786
-    g_assert(data.child_c != NULL);
357786
+    g_assert(detach_by_parent_data.child_c != NULL);
357786
 
357786
     g_assert_cmpint(parent_a->refcnt, ==, 1);
357786
     g_assert_cmpint(parent_b->refcnt, ==, 1);
357786
@@ -1076,8 +1140,8 @@ static void test_detach_by_parent_cb(void)
357786
     g_assert_cmpint(b->refcnt, ==, 1);
357786
     g_assert_cmpint(c->refcnt, ==, 2);
357786
 
357786
-    g_assert(QLIST_FIRST(&parent_b->children) == data.child_c);
357786
-    g_assert(QLIST_NEXT(data.child_c, next) == child_a);
357786
+    g_assert(QLIST_FIRST(&parent_b->children) == detach_by_parent_data.child_c);
357786
+    g_assert(QLIST_NEXT(detach_by_parent_data.child_c, next) == child_a);
357786
     g_assert(QLIST_NEXT(child_a, next) == NULL);
357786
 
357786
     g_assert_cmpint(parent_a->quiesce_counter, ==, 1);
357786
@@ -1105,6 +1169,15 @@ static void test_detach_by_parent_cb(void)
357786
     bdrv_unref(c);
357786
 }
357786
 
357786
+static void test_detach_by_parent_cb(void)
357786
+{
357786
+    test_detach_indirect(true);
357786
+}
357786
+
357786
+static void test_detach_by_driver_cb(void)
357786
+{
357786
+    test_detach_indirect(false);
357786
+}
357786
 
357786
 int main(int argc, char **argv)
357786
 {
357786
@@ -1157,6 +1230,7 @@ int main(int argc, char **argv)
357786
     g_test_add_func("/bdrv-drain/detach/drain", test_detach_by_drain);
357786
     g_test_add_func("/bdrv-drain/detach/drain_subtree", test_detach_by_drain_subtree);
357786
     g_test_add_func("/bdrv-drain/detach/parent_cb", test_detach_by_parent_cb);
357786
+    g_test_add_func("/bdrv-drain/detach/driver_cb", test_detach_by_driver_cb);
357786
 
357786
     ret = g_test_run();
357786
     qemu_event_destroy(&done_event);
357786
-- 
357786
1.8.3.1
357786