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

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