Skip to contentSkip to conversation

nbd/server: Quiesce coroutines on context switch

PATCH Superseded Sergio Lopez 3 participants 9 messages Jan 21, 2021 – Feb 1, 2021

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 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.

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 +++++++++++++++++++++++++++--------
 softmmu/runstate.c |  9 +++++++++
 2 files changed, 36 insertions(+), 8 deletions(-)
Signature · 1 line
2.26.2
#1 lore raw

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

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 v3 1/2
#2 lore raw

Re: [PATCH v3 1/2] block: Avoid processing BDS twice in bdrv_set_aio_context_ignore()

Quoted 2 lines · 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

one of its

Quoted 3 lines

children.

Doubling-processing of a BDS is especially problematic for the

Double-processing

Quoted 12 lines

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(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#3 lore raw

Re: [PATCH v3 1/2] block: Avoid processing BDS twice in bdrv_set_aio_context_ignore()

In reply to Sergio Lopez

Am 21.01.2021 um 18:06 hat Sergio Lopez geschrieben:

Quoted 8 lines

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.

Quoted 8 lines

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(-)

The patch looks correct to me, I'm just wondering about one thing:

block.c void bdrv_set_aio_context_ignore(BlockDriverState *bs, · line 6418

Quoted 11 lines
block.c +2 -1
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;

Why do we need these separate lists? Can't we just iterate over bs->parents/children a second time? I don't think the graph can change between the first and the second loop (and if it could, the result would be broken anyway).

Kevin

#4 lore raw

Re: [PATCH v3 1/2] block: Avoid processing BDS twice in bdrv_set_aio_context_ignore()

Quoted 20 lines · Kevin Wolf

Am 21.01.2021 um 18:06 hat Sergio Lopez geschrieben:

Quoted 8 lines

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.

Quoted 8 lines

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(-)

The patch looks correct to me, I'm just wondering about one thing:

Quoted 16 lines

block.c void bdrv_set_aio_context_ignore(BlockDriverState *bs, · line 6418

Quoted 11 lines
block.c +2 -1
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;

Why do we need these separate lists? Can't we just iterate over bs->parents/children a second time? I don't think the graph can change between the first and the second loop (and if it could, the result would be broken anyway).

It's not strictly needed, but this makes the code more readable by making our intentions clearer. To my eyes, at least.

Sergio.

#5 lore raw

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

In reply to Sergio Lopez

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 -
 softmmu/runstate.c | 9 +++++++++
 2 files changed, 9 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 */
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
Signature · 1 line
2.26.2
Reviewed-by on v3 2/2
#6 lore raw

Re: [PATCH v3 2/2] block: move blk_exp_close_all() to qemu_cleanup()

Quoted 6 lines · Sergio Lopez

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().

stuck in

Quoted 10 lines

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

---
 block.c            | 1 -
 softmmu/runstate.c | 9 +++++++++
 2 files changed, 9 insertions(+), 1 deletion(-)

softmmu/runstate.c void qemu_cleanup(void) · line 790

Quoted 7 lines
softmmu/runstate.c +4 -0
@@ -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

s/be //

softmmu/runstate.c void qemu_cleanup(void) · line 797

Quoted 8 lines
softmmu/runstate.c +4 -0
+     * 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
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#7 lore raw

Re: [PATCH v3 2/2] block: move blk_exp_close_all() to qemu_cleanup()

In reply to Sergio Lopez

Am 21.01.2021 um 18:07 hat Sergio Lopez geschrieben:

Quoted 9 lines

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

This patch loses the call in qemu-nbd and qemu-storage-daemon.

Kevin

#8 lore raw

Re: [PATCH v3 2/2] block: move blk_exp_close_all() to qemu_cleanup()

Quoted 4 lines · Kevin Wolf

Am 21.01.2021 um 18:07 hat Sergio Lopez geschrieben:

Quoted 3 lines

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

Quoted 8 lines
Quoted 6 lines

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

This patch loses the call in qemu-nbd and qemu-storage-daemon.

You're right, I'll prepare a v4 right away.

Thanks,
Sergio.