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

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