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

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