Skip to contentSkip to conversation

nbd/server: Quiesce coroutines on context switch

PATCH In review Sergio Lopez 2 participants 4 messages Feb 1, 2021

lore
Share & tools
lore
#0 lore raw

v4 cover nbd/server: Quiesce coroutines on context switch

This series allows the NBD server to properly switch between AIO contexts, having quiesced recv_coroutine and send_coroutine before doing the transition.

We need this because we send back devices running in IO Thread owned contexts to the main context when stopping the data plane, something that can happen multiple times during the lifetime of a VM (usually during the boot sequence or on a reboot), and we drag the NBD server of the correspoing export with it.

While there, fix also a problem caused by a cross-dependency between closing the export's client connections and draining the block layer. The visible effect of this problem was QEMU getting hung when the guest request a power off while there's an active NBD client.

v4:
- Call to blk_exp_close_all() from qemu-nbd and qemu-storage-daemon too. (Kevin Wolf)

v3:
- Drop already merged "block: Honor blk_set_aio_context() context requirements" and "nbd/server: Quiesce coroutines on context switch"
- Change the strategy for avoiding processing BDS twice to adding every child and parent to the ignore list in advance before processing them. (Kevin Wolf)
- Replace "nbd/server: Quiesce coroutines on context switch" with "block: move blk_exp_close_all() to qemu_cleanup()"

v2:
- Replace "virtio-blk: Acquire context while switching them on dataplane start" with "block: Honor blk_set_aio_context() context requirements" (Kevin Wolf)
- Add "block: Avoid processing BDS twice in bdrv_set_aio_context_ignore()"
- Add "block: Close block exports in two steps"
- Rename nbd_read_eof() to nbd_server_read_eof() (Eric Blake)
- Fix double space and typo in comment. (Eric Blake)

Sergio Lopez (2):
block: Avoid processing BDS twice in bdrv_set_aio_context_ignore()
block: move blk_exp_close_all() to qemu_cleanup()

 block.c                              | 35 +++++++++++++++++++++-------
 qemu-nbd.c                           |  1 +
 softmmu/runstate.c                   |  9 +++++++
 storage-daemon/qemu-storage-daemon.c |  1 +
 4 files changed, 38 insertions(+), 8 deletions(-)
Signature · 1 line
2.26.2
#2 lore raw

PATCH v4 2/2 block: move blk_exp_close_all() to qemu_cleanup()

Move blk_exp_close_all() from bdrv_close() to qemu_cleanup(), before bdrv_drain_all_begin().

Export drivers may have coroutines yielding at some point in the block layer, so we need to shut them down before draining the block layer, as otherwise they may get stuck blk_wait_while_drained().

RHBZ: https://bugzilla.redhat.com/show_bug.cgi?id=1900505

---
 block.c                              | 1 -
 qemu-nbd.c                           | 1 +
 softmmu/runstate.c                   | 9 +++++++++
 storage-daemon/qemu-storage-daemon.c | 1 +
 4 files changed, 11 insertions(+), 1 deletion(-)
block.c +0 -1
index 3da99312db..9682c82fa8 100644
@@ -4435,7 +4435,6 @@ static void bdrv_close(BlockDriverState *bs)
 void bdrv_close_all(void)
 {
     assert(job_next(NULL) == NULL);
-    blk_exp_close_all();
 
     /* Drop references from requests still in flight, such as canceled block
      * jobs whose AIO context has not been polled yet */
qemu-nbd.c +1 -0
index 0d513cb38c..608c63e82a 100644
@@ -503,6 +503,7 @@ static const char *socket_activation_validate_opts(const char *device,
 static void qemu_nbd_shutdown(void)
 {
     job_cancel_sync_all();
+    blk_exp_close_all();
     bdrv_close_all();
 }
 
softmmu/runstate.c +9 -0
index 6177693a30..ac4b2e2540 100644
@@ -25,6 +25,7 @@
 #include "qemu/osdep.h"
 #include "audio/audio.h"
 #include "block/block.h"
+#include "block/export.h"
 #include "chardev/char.h"
 #include "crypto/cipher.h"
 #include "crypto/init.h"
@@ -783,6 +784,14 @@ void qemu_cleanup(void)
      */
     migration_shutdown();
 
+    /*
+     * Close the exports before draining the block layer. The export
+     * drivers may have coroutines yielding on it, so we need to clean
+     * them up before the drain, as otherwise they may be get stuck in
+     * blk_wait_while_drained().
+     */
+    blk_exp_close_all();
+
     /*
      * We must cancel all block jobs while the block layer is drained,
      * or cancelling will be affected by throttling and thus may block
storage-daemon/qemu-storage-daemon.c +1 -0
index e0c87edbdd..d8d172cc60 100644
@@ -314,6 +314,7 @@ int main(int argc, char *argv[])
         main_loop_wait(false);
     }
 
+    blk_exp_close_all();
     bdrv_drain_all_begin();
     bdrv_close_all();
 
Signature · 1 line
2.26.2
#1 lore raw

PATCH v4 1/2 block: Avoid processing BDS twice in bdrv_set_aio_context_ignore()

In reply to Sergio Lopez

Some graphs may contain an indirect reference to the first BDS in the chain that can be reached while walking it bottom->up from one its children.

Doubling-processing of a BDS is especially problematic for the aio_notifiers, as they might attempt to work on both the old and the new AIO contexts.

To avoid this problem, add every child and parent to the ignore list before actually processing them.

---
 block.c | 34 +++++++++++++++++++++++++++-------
 1 file changed, 27 insertions(+), 7 deletions(-)
block.c +27 -7
index 8b9d457546..3da99312db 100644
@@ -6414,7 +6414,10 @@ void bdrv_set_aio_context_ignore(BlockDriverState *bs,
                                  AioContext *new_context, GSList **ignore)
 {
     AioContext *old_context = bdrv_get_aio_context(bs);
-    BdrvChild *child;
+    GSList *children_to_process = NULL;
+    GSList *parents_to_process = NULL;
+    GSList *entry;
+    BdrvChild *child, *parent;
 
     g_assert(qemu_get_current_aio_context() == qemu_get_aio_context());
 
@@ -6429,16 +6432,33 @@ void bdrv_set_aio_context_ignore(BlockDriverState *bs,
             continue;
         }
         *ignore = g_slist_prepend(*ignore, child);
-        bdrv_set_aio_context_ignore(child->bs, new_context, ignore);
+        children_to_process = g_slist_prepend(children_to_process, child);
     }
-    QLIST_FOREACH(child, &bs->parents, next_parent) {
-        if (g_slist_find(*ignore, child)) {
+
+    QLIST_FOREACH(parent, &bs->parents, next_parent) {
+        if (g_slist_find(*ignore, parent)) {
             continue;
         }
-        assert(child->klass->set_aio_ctx);
-        *ignore = g_slist_prepend(*ignore, child);
-        child->klass->set_aio_ctx(child, new_context, ignore);
+        *ignore = g_slist_prepend(*ignore, parent);
+        parents_to_process = g_slist_prepend(parents_to_process, parent);
+    }
+
+    for (entry = children_to_process;
+         entry != NULL;
+         entry = g_slist_next(entry)) {
+        child = entry->data;
+        bdrv_set_aio_context_ignore(child->bs, new_context, ignore);
+    }
+    g_slist_free(children_to_process);
+
+    for (entry = parents_to_process;
+         entry != NULL;
+         entry = g_slist_next(entry)) {
+        parent = entry->data;
+        assert(parent->klass->set_aio_ctx);
+        parent->klass->set_aio_ctx(parent, new_context, ignore);
     }
+    g_slist_free(parents_to_process);
 
     bdrv_detach_aio_context(bs);
 
Signature · 1 line
2.26.2
Reviewed-by on the v4 series
#3 lore raw
In reply to Sergio Lopez

Am 01.02.2021 um 13:50 hat Sergio Lopez geschrieben:

Quoted 8 lines

This series allows the NBD server to properly switch between AIO contexts, having quiesced recv_coroutine and send_coroutine before doing the transition.

We need this because we send back devices running in IO Thread owned contexts to the main context when stopping the data plane, something that can happen multiple times during the lifetime of a VM (usually during the boot sequence or on a reboot), and we drag the NBD server of the correspoing export with it.

Quoted 4 lines

While there, fix also a problem caused by a cross-dependency between closing the export's client connections and draining the block layer. The visible effect of this problem was QEMU getting hung when the guest request a power off while there's an active NBD client.