Skip to contentSkip to conversation

block: Start/end drain on correct AioContext

PATCH Applied Hanna Reitz 2 participants 9 messages Nov 7, 2022 – Nov 10, 2022 Block layer

lore
Share & tools
lore
#0 lore raw

v2 cover block: Start/end drain on correct AioContext

Hi,

v1 cover letter:
https://lists.nongnu.org/archive/html/qemu-block/2022-09/msg00389.html

bdrv_replace_child_noperm() drains the child via bdrv_parent_drained_{begin,end}_single(). When it removes a child, the bdrv_parent_drained_end_single() at its end will be called on an empty child, making the BDRV_POLL_WHILE() in it poll the main AioContext (because c->bs is NULL).

That’s wrong, though, because it’s supposed to operate on the parent. bdrv_parent_drained_end_single_no_poll() will have scheduled any BHs in the parents’ AioContext, which may be anything, not necessarily the main context. Therefore, we must poll the parent’s context.

Patch 3 does this for both bdrv_parent_drained_{begin,end}_single(). Patch 1 ensures that we can legally call bdrv_child_get_parent_aio_context() from those I/O context functions, and patch 2 fixes blk_do_set_aio_context() to not cause an assertion failure if it beginning a drain can end up in blk_get_aio_context() before blk->ctx has been updated.

v2:
- Patch 1:
- Move bdrv_child_get_parent_aio_context() from block-global-state.h to block-io.h
- Move BdrvChildClass.get_parent_aio_context into the I/O code section
- Mark blk_root_get_parent_aio_context() as I/O code
- Mark child_job_get_parent_aio_context(), and lock the job mutex, which we now need to do (as of 3ed4f708fe1)
- Patch 2:
- Added comment similar to Kevin’s suggestion
(Still decided to take Kevin’s R-b, even though I didn’t use the literal suggestion, but made it a bit more verbose)

Hanna Reitz (3):
block: Make bdrv_child_get_parent_aio_context I/O
block-backend: Update ctx immediately after root
block: Start/end drain on correct AioContext

 include/block/block-global-state.h | 1 -
 include/block/block-io.h           | 2 ++
 include/block/block_int-common.h   | 4 ++--
 block.c                            | 2 +-
 block/block-backend.c              | 9 ++++++++-
 block/io.c                         | 6 ++++--
 blockjob.c                         | 3 ++-
 7 files changed, 19 insertions(+), 8 deletions(-)
Signature · 1 line
2.36.1
#1 lore raw

PATCH v2 1/3 block: Make bdrv_child_get_parent_aio_context I/O

We want to use bdrv_child_get_parent_aio_context() from bdrv_parent_drained_{begin,end}_single(), both of which are "I/O or GS" functions.

Prior to 3ed4f708fe1, all the implementations were I/O code anyway. 3ed4f708fe1 has put block jobs' AioContext field under the job mutex, so to make child_job_get_parent_aio_context() work in an I/O context, we need to take that lock there.

Furthermore, blk_root_get_parent_aio_context() is not marked as anything, but is safe to run in an I/O context, so mark it that way now. (blk_get_aio_context() is an I/O code function.)

With that done, all implementations explicitly are I/O code, so we can mark bdrv_child_get_parent_aio_context() as I/O code, too, so callers know it is safe to run from both GS and I/O contexts.

---
 include/block/block-global-state.h | 1 -
 include/block/block-io.h           | 2 ++
 include/block/block_int-common.h   | 4 ++--
 block.c                            | 2 +-
 block/block-backend.c              | 1 +
 blockjob.c                         | 3 ++-
 6 files changed, 8 insertions(+), 5 deletions(-)
include/block/block-global-state.h +0 -1
index bb42ed9559..c7bd4a2088 100644
@@ -220,7 +220,6 @@ void coroutine_fn bdrv_co_lock(BlockDriverState *bs);
  */
 void coroutine_fn bdrv_co_unlock(BlockDriverState *bs);
 
-AioContext *bdrv_child_get_parent_aio_context(BdrvChild *c);
 bool bdrv_child_change_aio_context(BdrvChild *c, AioContext *ctx,
                                    GHashTable *visited, Transaction *tran,
                                    Error **errp);
include/block/block-io.h +2 -0
index 770ddeb7c8..b099d7db45 100644
@@ -171,6 +171,8 @@ void bdrv_debug_event(BlockDriverState *bs, BlkdebugEvent event);
  */
 AioContext *bdrv_get_aio_context(BlockDriverState *bs);
 
+AioContext *bdrv_child_get_parent_aio_context(BdrvChild *c);
+
 /**
  * Move the current coroutine to the AioContext of @bs and return the old
  * AioContext of the coroutine. Increase bs->in_flight so that draining @bs
include/block/block_int-common.h +2 -2
index 5a2cc077a0..31ae91e56e 100644
@@ -911,8 +911,6 @@ struct BdrvChildClass {
                            GHashTable *visited, Transaction *tran,
                            Error **errp);
 
-    AioContext *(*get_parent_aio_context)(BdrvChild *child);
-
     /*
      * I/O API functions. These functions are thread-safe.
      *
@@ -929,6 +927,8 @@ struct BdrvChildClass {
      */
     const char *(*get_name)(BdrvChild *child);
 
+    AioContext *(*get_parent_aio_context)(BdrvChild *child);
+
     /*
      * If this pair of functions is implemented, the parent doesn't issue new
      * requests after returning from .drained_begin() until .drained_end() is
block.c +1 -1
index 3bd594eb2a..2d6128d80a 100644
@@ -1533,7 +1533,7 @@ const BdrvChildClass child_of_bds = {
 
 AioContext *bdrv_child_get_parent_aio_context(BdrvChild *c)
 {
-    GLOBAL_STATE_CODE();
+    IO_CODE();
     return c->klass->get_parent_aio_context(c);
 }
 
block/block-backend.c +1 -0
index c0c7d56c8d..ed2f4b67a2 100644
@@ -311,6 +311,7 @@ static void blk_root_detach(BdrvChild *child)
 static AioContext *blk_root_get_parent_aio_context(BdrvChild *c)
 {
     BlockBackend *blk = c->opaque;
+    IO_CODE();
 
     return blk_get_aio_context(blk);
 }
blockjob.c +2 -1
index 2d86014fa5..f51d4e18f3 100644
@@ -173,7 +173,8 @@ static bool child_job_change_aio_ctx(BdrvChild *c, AioContext *ctx,
 static AioContext *child_job_get_parent_aio_context(BdrvChild *c)
 {
     BlockJob *job = c->opaque;
-    GLOBAL_STATE_CODE();
+    IO_CODE();
+    JOB_LOCK_GUARD();
 
     return job->job.aio_context;
 }
Signature · 1 line
2.36.1
#2 lore raw

PATCH v2 2/3 block-backend: Update ctx immediately after root

In reply to Hanna Reitz

blk_get_aio_context() asserts that blk->ctx is always equal to the root BDS's context (if there is a root BDS). Therefore, blk_do_set_aio_context() must update blk->ctx immediately after the root BDS's context has changed.

Without this patch, the next patch would break iotest 238, because bdrv_drained_begin() (called by blk_do_set_aio_context()) may then invoke bdrv_child_get_parent_aio_context() on the root child, i.e. blk_get_aio_context(). However, by this point, blk->ctx would not have been updated and thus differ from the root node's context. This patch fixes that.

---
 block/block-backend.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
block/block-backend.c +7 -1
index ed2f4b67a2..b48c91f4e1 100644
@@ -2158,6 +2158,11 @@ static int blk_do_set_aio_context(BlockBackend *blk, AioContext *new_context,
                 return ret;
             }
         }
+        /*
+         * Make blk->ctx consistent with the root node before we invoke any
+         * other operations like drain that might inquire blk->ctx
+         */
+        blk->ctx = new_context;
         if (tgm->throttle_state) {
             bdrv_drained_begin(bs);
             throttle_group_detach_aio_context(tgm);
@@ -2166,9 +2171,10 @@ static int blk_do_set_aio_context(BlockBackend *blk, AioContext *new_context,
         }
 
         bdrv_unref(bs);
+    } else {
+        blk->ctx = new_context;
     }
 
-    blk->ctx = new_context;
     return 0;
 }
 
Signature · 1 line
2.36.1
#3 lore raw

PATCH v2 3/3 block: Start/end drain on correct AioContext

In reply to Hanna Reitz

bdrv_parent_drained_{begin,end}_single() are supposed to operate on the parent, not on the child, so they should not attempt to get the context to poll from the child but the parent instead. BDRV_POLL_WHILE(c->bs) does get the context from the child, so we should replace it with AIO_WAIT_WHILE() on the parent's context instead.

This problem becomes apparent when bdrv_replace_child_noperm() invokes bdrv_parent_drained_end_single() after removing a child from a subgraph that is in an I/O thread. By the time bdrv_parent_drained_end_single() is called, child->bs is NULL, and so BDRV_POLL_WHILE(c->bs, ...) will poll the main loop instead of the I/O thread; but anything that bdrv_parent_drained_end_single_no_poll() may have scheduled is going to want to run in the I/O thread, but because we poll the main loop, the I/O thread is never unpaused, and nothing is run, resulting in a deadlock.

---
 block/io.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
block/io.c +4 -2
index 34b30e304e..b9424024f9 100644
@@ -71,9 +71,10 @@ static void bdrv_parent_drained_end_single_no_poll(BdrvChild *c,
 void bdrv_parent_drained_end_single(BdrvChild *c)
 {
     int drained_end_counter = 0;
+    AioContext *ctx = bdrv_child_get_parent_aio_context(c);
     IO_OR_GS_CODE();
     bdrv_parent_drained_end_single_no_poll(c, &drained_end_counter);
-    BDRV_POLL_WHILE(c->bs, qatomic_read(&drained_end_counter) > 0);
+    AIO_WAIT_WHILE(ctx, qatomic_read(&drained_end_counter) > 0);
 }
 
 static void bdrv_parent_drained_end(BlockDriverState *bs, BdrvChild *ignore,
@@ -116,13 +117,14 @@ static bool bdrv_parent_drained_poll(BlockDriverState *bs, BdrvChild *ignore,
 
 void bdrv_parent_drained_begin_single(BdrvChild *c, bool poll)
 {
+    AioContext *ctx = bdrv_child_get_parent_aio_context(c);
     IO_OR_GS_CODE();
     c->parent_quiesce_counter++;
     if (c->klass->drained_begin) {
         c->klass->drained_begin(c);
     }
     if (poll) {
-        BDRV_POLL_WHILE(c->bs, bdrv_parent_drained_poll_single(c));
+        AIO_WAIT_WHILE(ctx, bdrv_parent_drained_poll_single(c));
     }
 }
 
Signature · 1 line
2.36.1
#4 lore raw
In reply to Hanna Reitz

Am 07.11.2022 um 16:13 hat Hanna Reitz geschrieben:

Quoted 16 lines

Hi,

v1 cover letter:
https://lists.nongnu.org/archive/html/qemu-block/2022-09/msg00389.html

bdrv_replace_child_noperm() drains the child via bdrv_parent_drained_{begin,end}_single(). When it removes a child, the bdrv_parent_drained_end_single() at its end will be called on an empty child, making the BDRV_POLL_WHILE() in it poll the main AioContext (because c->bs is NULL).

That’s wrong, though, because it’s supposed to operate on the parent. bdrv_parent_drained_end_single_no_poll() will have scheduled any BHs in the parents’ AioContext, which may be anything, not necessarily the main context. Therefore, we must poll the parent’s context.

Quoted 6 lines

Patch 3 does this for both bdrv_parent_drained_{begin,end}_single(). Patch 1 ensures that we can legally call bdrv_child_get_parent_aio_context() from those I/O context functions, and patch 2 fixes blk_do_set_aio_context() to not cause an assertion failure if it beginning a drain can end up in blk_get_aio_context() before blk->ctx has been updated.

Hmm, I may have unintentionally made this series obsolete with the drain series I sent today. The poll instances that you're fixing simply don't exist any more after it.

Can you check if the bug is gone with my series? I would hope so, but if not, we would probably need to apply a fix in a different place.

Kevin

#5 lore raw

Am 08.11.2022 um 15:13 hat Kevin Wolf geschrieben:

Quoted 24 lines

Am 07.11.2022 um 16:13 hat Hanna Reitz geschrieben:

Quoted 16 lines

Hi,

v1 cover letter:
https://lists.nongnu.org/archive/html/qemu-block/2022-09/msg00389.html

bdrv_replace_child_noperm() drains the child via bdrv_parent_drained_{begin,end}_single(). When it removes a child, the bdrv_parent_drained_end_single() at its end will be called on an empty child, making the BDRV_POLL_WHILE() in it poll the main AioContext (because c->bs is NULL).

That’s wrong, though, because it’s supposed to operate on the parent. bdrv_parent_drained_end_single_no_poll() will have scheduled any BHs in the parents’ AioContext, which may be anything, not necessarily the main context. Therefore, we must poll the parent’s context.

Quoted 6 lines

Patch 3 does this for both bdrv_parent_drained_{begin,end}_single(). Patch 1 ensures that we can legally call bdrv_child_get_parent_aio_context() from those I/O context functions, and patch 2 fixes blk_do_set_aio_context() to not cause an assertion failure if it beginning a drain can end up in blk_get_aio_context() before blk->ctx has been updated.

Quoted 6 lines

Hmm, I may have unintentionally made this series obsolete with the drain series I sent today. The poll instances that you're fixing simply don't exist any more after it.

Can you check if the bug is gone with my series? I would hope so, but if not, we would probably need to apply a fix in a different place.

Actually, on second thoughts, we'd probably still apply your patches as a fix for 7.2 and then have my patches which would get rid of the polling only in block-next. Patch 1 and 2 of this series would stay in the tree, and that seems to make sense to me, too. So obsolete was not the right word.

But it would still be interesting to see if my series fixes the bug, too, because otherwise it might introduce a regression later.

By the way, is the bug hard to test in a test case? If this series had one, I could just have run it myself against my tree.

Kevin

Applied
#6 lore raw
In reply to Hanna Reitz

Am 07.11.2022 um 16:13 hat Hanna Reitz geschrieben:

Quoted 16 lines

Hi,

v1 cover letter:
https://lists.nongnu.org/archive/html/qemu-block/2022-09/msg00389.html

bdrv_replace_child_noperm() drains the child via bdrv_parent_drained_{begin,end}_single(). When it removes a child, the bdrv_parent_drained_end_single() at its end will be called on an empty child, making the BDRV_POLL_WHILE() in it poll the main AioContext (because c->bs is NULL).

That’s wrong, though, because it’s supposed to operate on the parent. bdrv_parent_drained_end_single_no_poll() will have scheduled any BHs in the parents’ AioContext, which may be anything, not necessarily the main context. Therefore, we must poll the parent’s context.

Quoted 6 lines

Patch 3 does this for both bdrv_parent_drained_{begin,end}_single(). Patch 1 ensures that we can legally call bdrv_child_get_parent_aio_context() from those I/O context functions, and patch 2 fixes blk_do_set_aio_context() to not cause an assertion failure if it beginning a drain can end up in blk_get_aio_context() before blk->ctx has been updated.

Thanks, applied to the block branch.

I would still be interested in a test case as a follow-up.

Kevin

#7 lore raw
Quoted 17 lines · Kevin Wolf

Am 07.11.2022 um 16:13 hat Hanna Reitz geschrieben:

Quoted 16 lines

Hi,

v1 cover letter:
https://lists.nongnu.org/archive/html/qemu-block/2022-09/msg00389.html

bdrv_replace_child_noperm() drains the child via bdrv_parent_drained_{begin,end}_single(). When it removes a child, the bdrv_parent_drained_end_single() at its end will be called on an empty child, making the BDRV_POLL_WHILE() in it poll the main AioContext (because c->bs is NULL).

That’s wrong, though, because it’s supposed to operate on the parent. bdrv_parent_drained_end_single_no_poll() will have scheduled any BHs in the parents’ AioContext, which may be anything, not necessarily the main context. Therefore, we must poll the parent’s context.

Quoted 7 lines
Quoted 6 lines

Patch 3 does this for both bdrv_parent_drained_{begin,end}_single(). Patch 1 ensures that we can legally call bdrv_child_get_parent_aio_context() from those I/O context functions, and patch 2 fixes blk_do_set_aio_context() to not cause an assertion failure if it beginning a drain can end up in blk_get_aio_context() before blk->ctx has been updated.

Thanks, applied to the block branch.

Thanks!

I tested your drain series, and it does indeed fix the bug, too. (Sorry for the delay, I thought it’d take less time to write an iotest...)

Quoted 1 line

I would still be interested in a test case as a follow-up.

Got it working now and sent as “tests/stream-under-throttle: New test”.

Hanna

#8 lore raw

Am 10.11.2022 um 17:11 hat Hanna Reitz geschrieben:

Quoted 24 lines · Kevin Wolf
Quoted 23 lines

Am 07.11.2022 um 16:13 hat Hanna Reitz geschrieben:

Quoted 22 lines

Hi,

v1 cover letter:
https://lists.nongnu.org/archive/html/qemu-block/2022-09/msg00389.html

bdrv_replace_child_noperm() drains the child via bdrv_parent_drained_{begin,end}_single(). When it removes a child, the bdrv_parent_drained_end_single() at its end will be called on an empty child, making the BDRV_POLL_WHILE() in it poll the main AioContext (because c->bs is NULL).

That’s wrong, though, because it’s supposed to operate on the parent. bdrv_parent_drained_end_single_no_poll() will have scheduled any BHs in the parents’ AioContext, which may be anything, not necessarily the main context. Therefore, we must poll the parent’s context.

Patch 3 does this for both bdrv_parent_drained_{begin,end}_single(). Patch 1 ensures that we can legally call bdrv_child_get_parent_aio_context() from those I/O context functions, and patch 2 fixes blk_do_set_aio_context() to not cause an assertion failure if it beginning a drain can end up in blk_get_aio_context() before blk->ctx has been updated.

Quoted 7 lines
Quoted 1 line

Thanks, applied to the block branch.

Thanks!

I tested your drain series, and it does indeed fix the bug, too. (Sorry for the delay, I thought it’d take less time to write an iotest...)

Thanks for testing, it's good to have this confirmed.

Quoted 3 lines
Quoted 1 line

I would still be interested in a test case as a follow-up.

Got it working now and sent as “tests/stream-under-throttle: New test”.

Great! I'll take a look.

Kevin