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

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