Blame SOURCES/kvm-block-Drain-recursively-with-a-single-BDRV_POLL_WHIL.patch

1bdc94
From 3ff0f5aa671432b2c11c36818368ed1e40c95f5a Mon Sep 17 00:00:00 2001
1bdc94
From: Kevin Wolf <kwolf@redhat.com>
1bdc94
Date: Fri, 14 Sep 2018 10:55:08 +0200
1bdc94
Subject: [PATCH 17/49] block: Drain recursively with a single
1bdc94
 BDRV_POLL_WHILE()
1bdc94
1bdc94
RH-Author: Kevin Wolf <kwolf@redhat.com>
1bdc94
Message-id: <20180914105540.18077-11-kwolf@redhat.com>
1bdc94
Patchwork-id: 82163
1bdc94
O-Subject: [RHV-7.6 qemu-kvm-rhev PATCH 10/42] block: Drain recursively with a single BDRV_POLL_WHILE()
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
Anything can happen inside BDRV_POLL_WHILE(), including graph
1bdc94
changes that may interfere with its callers (e.g. child list iteration
1bdc94
in recursive callers of bdrv_do_drained_begin).
1bdc94
1bdc94
Switch to a single BDRV_POLL_WHILE() call for the whole subtree at the
1bdc94
end of bdrv_do_drained_begin() to avoid such effects. The recursion
1bdc94
happens now inside the loop condition. As the graph can only change
1bdc94
between bdrv_drain_poll() calls, but not inside of it, doing the
1bdc94
recursion here is safe.
1bdc94
1bdc94
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
1bdc94
(cherry picked from commit fe4f0614ef9e361dae12012d3c400657444836cf)
1bdc94
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
1bdc94
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
1bdc94
---
1bdc94
 block.c               |  2 +-
1bdc94
 block/io.c            | 63 ++++++++++++++++++++++++++++++++++++---------------
1bdc94
 include/block/block.h |  9 +++++---
1bdc94
 3 files changed, 52 insertions(+), 22 deletions(-)
1bdc94
1bdc94
diff --git a/block.c b/block.c
1bdc94
index 0d9698a..9afaf26 100644
1bdc94
--- a/block.c
1bdc94
+++ b/block.c
1bdc94
@@ -824,7 +824,7 @@ static void bdrv_child_cb_drained_begin(BdrvChild *child)
1bdc94
 static bool bdrv_child_cb_drained_poll(BdrvChild *child)
1bdc94
 {
1bdc94
     BlockDriverState *bs = child->opaque;
1bdc94
-    return bdrv_drain_poll(bs, NULL);
1bdc94
+    return bdrv_drain_poll(bs, false, NULL);
1bdc94
 }
1bdc94
 
1bdc94
 static void bdrv_child_cb_drained_end(BdrvChild *child)
1bdc94
diff --git a/block/io.c b/block/io.c
1bdc94
index a0e3461..442ded1 100644
1bdc94
--- a/block/io.c
1bdc94
+++ b/block/io.c
1bdc94
@@ -164,6 +164,7 @@ typedef struct {
1bdc94
     bool done;
1bdc94
     bool begin;
1bdc94
     bool recursive;
1bdc94
+    bool poll;
1bdc94
     BdrvChild *parent;
1bdc94
 } BdrvCoDrainData;
1bdc94
 
1bdc94
@@ -199,27 +200,42 @@ static void bdrv_drain_invoke(BlockDriverState *bs, bool begin)
1bdc94
 }
1bdc94
 
1bdc94
 /* Returns true if BDRV_POLL_WHILE() should go into a blocking aio_poll() */
1bdc94
-bool bdrv_drain_poll(BlockDriverState *bs, BdrvChild *ignore_parent)
1bdc94
+bool bdrv_drain_poll(BlockDriverState *bs, bool recursive,
1bdc94
+                     BdrvChild *ignore_parent)
1bdc94
 {
1bdc94
+    BdrvChild *child, *next;
1bdc94
+
1bdc94
     if (bdrv_parent_drained_poll(bs, ignore_parent)) {
1bdc94
         return true;
1bdc94
     }
1bdc94
 
1bdc94
-    return atomic_read(&bs->in_flight);
1bdc94
+    if (atomic_read(&bs->in_flight)) {
1bdc94
+        return true;
1bdc94
+    }
1bdc94
+
1bdc94
+    if (recursive) {
1bdc94
+        QLIST_FOREACH_SAFE(child, &bs->children, next, next) {
1bdc94
+            if (bdrv_drain_poll(child->bs, recursive, child)) {
1bdc94
+                return true;
1bdc94
+            }
1bdc94
+        }
1bdc94
+    }
1bdc94
+
1bdc94
+    return false;
1bdc94
 }
1bdc94
 
1bdc94
-static bool bdrv_drain_poll_top_level(BlockDriverState *bs,
1bdc94
+static bool bdrv_drain_poll_top_level(BlockDriverState *bs, bool recursive,
1bdc94
                                       BdrvChild *ignore_parent)
1bdc94
 {
1bdc94
     /* Execute pending BHs first and check everything else only after the BHs
1bdc94
      * have executed. */
1bdc94
     while (aio_poll(bs->aio_context, false));
1bdc94
 
1bdc94
-    return bdrv_drain_poll(bs, ignore_parent);
1bdc94
+    return bdrv_drain_poll(bs, recursive, ignore_parent);
1bdc94
 }
1bdc94
 
1bdc94
 static void bdrv_do_drained_begin(BlockDriverState *bs, bool recursive,
1bdc94
-                                  BdrvChild *parent);
1bdc94
+                                  BdrvChild *parent, bool poll);
1bdc94
 static void bdrv_do_drained_end(BlockDriverState *bs, bool recursive,
1bdc94
                                 BdrvChild *parent);
1bdc94
 
1bdc94
@@ -231,7 +247,7 @@ static void bdrv_co_drain_bh_cb(void *opaque)
1bdc94
 
1bdc94
     bdrv_dec_in_flight(bs);
1bdc94
     if (data->begin) {
1bdc94
-        bdrv_do_drained_begin(bs, data->recursive, data->parent);
1bdc94
+        bdrv_do_drained_begin(bs, data->recursive, data->parent, data->poll);
1bdc94
     } else {
1bdc94
         bdrv_do_drained_end(bs, data->recursive, data->parent);
1bdc94
     }
1bdc94
@@ -242,7 +258,7 @@ static void bdrv_co_drain_bh_cb(void *opaque)
1bdc94
 
1bdc94
 static void coroutine_fn bdrv_co_yield_to_drain(BlockDriverState *bs,
1bdc94
                                                 bool begin, bool recursive,
1bdc94
-                                                BdrvChild *parent)
1bdc94
+                                                BdrvChild *parent, bool poll)
1bdc94
 {
1bdc94
     BdrvCoDrainData data;
1bdc94
 
1bdc94
@@ -257,6 +273,7 @@ static void coroutine_fn bdrv_co_yield_to_drain(BlockDriverState *bs,
1bdc94
         .begin = begin,
1bdc94
         .recursive = recursive,
1bdc94
         .parent = parent,
1bdc94
+        .poll = poll,
1bdc94
     };
1bdc94
     bdrv_inc_in_flight(bs);
1bdc94
     aio_bh_schedule_oneshot(bdrv_get_aio_context(bs),
1bdc94
@@ -269,12 +286,12 @@ static void coroutine_fn bdrv_co_yield_to_drain(BlockDriverState *bs,
1bdc94
 }
1bdc94
 
1bdc94
 void bdrv_do_drained_begin(BlockDriverState *bs, bool recursive,
1bdc94
-                           BdrvChild *parent)
1bdc94
+                           BdrvChild *parent, bool poll)
1bdc94
 {
1bdc94
     BdrvChild *child, *next;
1bdc94
 
1bdc94
     if (qemu_in_coroutine()) {
1bdc94
-        bdrv_co_yield_to_drain(bs, true, recursive, parent);
1bdc94
+        bdrv_co_yield_to_drain(bs, true, recursive, parent, poll);
1bdc94
         return;
1bdc94
     }
1bdc94
 
1bdc94
@@ -286,25 +303,35 @@ void bdrv_do_drained_begin(BlockDriverState *bs, bool recursive,
1bdc94
     bdrv_parent_drained_begin(bs, parent);
1bdc94
     bdrv_drain_invoke(bs, true);
1bdc94
 
1bdc94
-    /* Wait for drained requests to finish */
1bdc94
-    BDRV_POLL_WHILE(bs, bdrv_drain_poll_top_level(bs, parent));
1bdc94
-
1bdc94
     if (recursive) {
1bdc94
         bs->recursive_quiesce_counter++;
1bdc94
         QLIST_FOREACH_SAFE(child, &bs->children, next, next) {
1bdc94
-            bdrv_do_drained_begin(child->bs, true, child);
1bdc94
+            bdrv_do_drained_begin(child->bs, true, child, false);
1bdc94
         }
1bdc94
     }
1bdc94
+
1bdc94
+    /*
1bdc94
+     * Wait for drained requests to finish.
1bdc94
+     *
1bdc94
+     * Calling BDRV_POLL_WHILE() only once for the top-level node is okay: The
1bdc94
+     * call is needed so things in this AioContext can make progress even
1bdc94
+     * though we don't return to the main AioContext loop - this automatically
1bdc94
+     * includes other nodes in the same AioContext and therefore all child
1bdc94
+     * nodes.
1bdc94
+     */
1bdc94
+    if (poll) {
1bdc94
+        BDRV_POLL_WHILE(bs, bdrv_drain_poll_top_level(bs, recursive, parent));
1bdc94
+    }
1bdc94
 }
1bdc94
 
1bdc94
 void bdrv_drained_begin(BlockDriverState *bs)
1bdc94
 {
1bdc94
-    bdrv_do_drained_begin(bs, false, NULL);
1bdc94
+    bdrv_do_drained_begin(bs, false, NULL, true);
1bdc94
 }
1bdc94
 
1bdc94
 void bdrv_subtree_drained_begin(BlockDriverState *bs)
1bdc94
 {
1bdc94
-    bdrv_do_drained_begin(bs, true, NULL);
1bdc94
+    bdrv_do_drained_begin(bs, true, NULL, true);
1bdc94
 }
1bdc94
 
1bdc94
 void bdrv_do_drained_end(BlockDriverState *bs, bool recursive,
1bdc94
@@ -314,7 +341,7 @@ void bdrv_do_drained_end(BlockDriverState *bs, bool recursive,
1bdc94
     int old_quiesce_counter;
1bdc94
 
1bdc94
     if (qemu_in_coroutine()) {
1bdc94
-        bdrv_co_yield_to_drain(bs, false, recursive, parent);
1bdc94
+        bdrv_co_yield_to_drain(bs, false, recursive, parent, false);
1bdc94
         return;
1bdc94
     }
1bdc94
     assert(bs->quiesce_counter > 0);
1bdc94
@@ -350,7 +377,7 @@ void bdrv_apply_subtree_drain(BdrvChild *child, BlockDriverState *new_parent)
1bdc94
     int i;
1bdc94
 
1bdc94
     for (i = 0; i < new_parent->recursive_quiesce_counter; i++) {
1bdc94
-        bdrv_do_drained_begin(child->bs, true, child);
1bdc94
+        bdrv_do_drained_begin(child->bs, true, child, true);
1bdc94
     }
1bdc94
 }
1bdc94
 
1bdc94
@@ -420,7 +447,7 @@ void bdrv_drain_all_begin(void)
1bdc94
         AioContext *aio_context = bdrv_get_aio_context(bs);
1bdc94
 
1bdc94
         aio_context_acquire(aio_context);
1bdc94
-        bdrv_do_drained_begin(bs, true, NULL);
1bdc94
+        bdrv_do_drained_begin(bs, true, NULL, true);
1bdc94
         aio_context_release(aio_context);
1bdc94
     }
1bdc94
 
1bdc94
diff --git a/include/block/block.h b/include/block/block.h
1bdc94
index 8c91d4c..2bbea7c 100644
1bdc94
--- a/include/block/block.h
1bdc94
+++ b/include/block/block.h
1bdc94
@@ -598,10 +598,13 @@ void bdrv_parent_drained_end(BlockDriverState *bs, BdrvChild *ignore);
1bdc94
 /**
1bdc94
  * bdrv_drain_poll:
1bdc94
  *
1bdc94
- * Poll for pending requests in @bs and its parents (except for
1bdc94
- * @ignore_parent). This is part of bdrv_drained_begin.
1bdc94
+ * Poll for pending requests in @bs, its parents (except for @ignore_parent),
1bdc94
+ * and if @recursive is true its children as well.
1bdc94
+ *
1bdc94
+ * This is part of bdrv_drained_begin.
1bdc94
  */
1bdc94
-bool bdrv_drain_poll(BlockDriverState *bs, BdrvChild *ignore_parent);
1bdc94
+bool bdrv_drain_poll(BlockDriverState *bs, bool recursive,
1bdc94
+                     BdrvChild *ignore_parent);
1bdc94
 
1bdc94
 /**
1bdc94
  * bdrv_drained_begin:
1bdc94
-- 
1bdc94
1.8.3.1
1bdc94