block: Simplify drain
v2 cover block: Simplify drain
I'm aware that exactly nobody has been looking forward to a series with this title, but it has to be. The way drain works means that we need to poll in bdrv_replace_child_noperm() and that makes things rather messy with Emanuele's multiqueue work because you must not poll while you hold the graph lock.
The other reason why it has to be is that drain is way too complex and there are too many different cases. Some simplification like this will hopefully make it considerably more maintainable. The diffstat probably tells something, too.
There are roughly speaking three parts in this series:
1. Make BlockDriver.bdrv_drained_begin/end() non-coroutine_fn again, which allows us to not poll on bdrv_drained_end() any more.
2. Remove subtree drains. They are a considerable complication in the whole drain machinery (in particular, they require polling in the BdrvChildClass.attach/detach() callbacks that are called during bdrv_replace_child_noperm()) and none of their users actually has a good reason to use them.
3. Finally get rid of polling in bdrv_replace_child_noperm() by requiring that the child is already drained by the caller and calling callbacks only once and not again for every nested drain section.
If necessary, a prefix of this series can be merged that covers only the first or the first two parts and it would still make sense.
v2:
- Rebased on master
- Patch 3: Removed left over _co parts in function names
- Patch 4: Updated function comments to reflect that we're not polling any more
- Patch 6 (new): Fix inconsistent AioContext locking for reopen code
- Patch 9 (was 8): Added comment to clarify when polling is allowed and the graph may change again
- Patch 11 (was 10):
* Reworded some comments and the commit message.
* Dropped a now unnecessary assertion that was dropped only in a later patch in v1 of the series.
* Changed 'int parent_quiesce_counter' into 'bool quiesced_parent'
- Patch 12 (was 11): Don't remove ignore_bds_parents from bdrv_drain_poll(), it is actually still a valid optimisation there that makes polling O(n) instead of O(n²)
- Patch 13 (new): Instead of only removing assert(!qemu_in_coroutine()) like in v1 of the series, drop out of coroutine context in bdrv_do_drained_begin_quiesce() just to be sure that we'll never get coroutine surprises in drain code.
- Patch 14 (was 12): More and reworded comments to make things hopefully a bit clearer
Kevin Wolf (15):
qed: Don't yield in bdrv_qed_co_drain_begin()
test-bdrv-drain: Don't yield in .bdrv_co_drained_begin/end()
block: Revert .bdrv_drained_begin/end to non-coroutine_fn
block: Remove drained_end_counter
block: Inline bdrv_drain_invoke()
block: Fix locking for bdrv_reopen_queue_child()
block: Drain invidual nodes during reopen
block: Don't use subtree drains in bdrv_drop_intermediate()
stream: Replace subtree drain with a single node drain
block: Remove subtree drains
block: Call drain callbacks only once
block: Remove ignore_bds_parents parameter from drain_begin/end.
block: Drop out of coroutine in bdrv_do_drained_begin_quiesce()
block: Don't poll in bdrv_replace_child_noperm()
block: Remove poll parameter from bdrv_parent_drained_begin_single()
include/block/block-global-state.h | 3 + include/block/block-io.h | 58 ++--- include/block/block_int-common.h | 25 +- include/block/block_int-io.h | 12 - block.c | 185 ++++++++++----- block/block-backend.c | 4 +- block/io.c | 290 +++++------------------ block/qed.c | 26 +- block/replication.c | 6 - block/stream.c | 26 +- block/throttle.c | 8 +- blockdev.c | 13 - blockjob.c | 2 +- tests/unit/test-bdrv-drain.c | 369 +++++++---------------------- 14 files changed, 340 insertions(+), 687 deletions(-)
Signature · 1 line
2.38.1
PATCH v2 1/15 qed: Don't yield in bdrv_qed_co_drain_begin()
We want to change .bdrv_co_drained_begin() back to be a non-coroutine callback, so in preparation, avoid yielding in its implementation.
Because we increase bs->in_flight and bdrv_drained_begin() polls, the behaviour is unchanged.
--- block/qed.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-)
block/qed.c +17 -3
@@ -282,9 +282,8 @@ static void coroutine_fn qed_unplug_allocating_write_reqs(BDRVQEDState *s)qemu_co_mutex_unlock (&s->table_lock); } -static void coroutine_fn qed_need_check_timer_entry(void *opaque) +static void coroutine_fn qed_need_check_timer(BDRVQEDState *s) { -BDRVQEDState *s = opaque;int ret;trace_qed_need_check_timer_cb (s);
@@ -310,9 +309,20 @@ static void coroutine_fn qed_need_check_timer_entry(void *opaque) (void ) ret; } +static void coroutine_fn qed_need_check_timer_entry(void *opaque) +{ +BDRVQEDState *s = opaque; + +qed_need_check_timer (opaque); +bdrv_dec_in_flight (s->bs); +} +static void qed_need_check_timer_cb (void *opaque) { +BDRVQEDState *s = opaque;Coroutine *co =qemu_coroutine_create (qed_need_check_timer_entry, opaque); + +bdrv_inc_in_flight (s->bs);qemu_coroutine_enter (co); }
@@ -363,8 +373,12 @@ static void coroutine_fn bdrv_qed_co_drain_begin(BlockDriverState *bs)* header is flushed. */ if (s->need_check_timer &&timer_pending (s->need_check_timer)) { +Coroutine *co; +qed_cancel_need_check_timer (s); -qed_need_check_timer_entry (s); + co =qemu_coroutine_create (qed_need_check_timer_entry, s); +bdrv_inc_in_flight (bs); +aio_co_enter (bdrv_get_aio_context (bs), co); } }
Signature · 1 line
2.38.1
PATCH v2 2/15 test-bdrv-drain: Don't yield in .bdrv_co_drained_begin/end()
In reply to Kevin WolfWe want to change .bdrv_co_drained_begin/end() back to be non-coroutine callbacks, so in preparation, avoid yielding in their implementation.
This does almost the same as the existing logic in bdrv_drain_invoke(), by creating and entering coroutines internally. However, since the test case is by far the heaviest user of coroutine code in drain callbacks, it is preferable to have the complexity in the test case rather than the drain core, which is already complicated enough without this.
The behaviour for bdrv_drain_begin() is unchanged because we increase bs->in_flight and this is still polled. However, bdrv_drain_end() doesn't wait for the spawned coroutine to complete any more. This is fine, we don't rely on bdrv_drain_end() restarting all operations immediately before the next aio_poll().
--- tests/unit/test-bdrv-drain.c | 64 ++++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 18 deletions(-)
tests/unit/test-bdrv-drain.c +46 -18
@@ -38,12 +38,22 @@ typedef struct BDRVTestState {bool sleep_in_drain_begin; } BDRVTestState; +static void coroutine_fn sleep_in_drain_begin(void *opaque) +{ +BlockDriverState *bs = opaque; + +qemu_co_sleep_ns (QEMU_CLOCK_REALTIME ,100000 ); +bdrv_dec_in_flight (bs); +} + static void coroutine_fn bdrv_test_co_drain_begin(BlockDriverState *bs) {BDRVTestState *s = bs->opaque; s->drain_count++;if (s->sleep_in_drain_begin) { -qemu_co_sleep_ns (QEMU_CLOCK_REALTIME ,100000 ); +Coroutine *co =qemu_coroutine_create (sleep_in_drain_begin, bs); +bdrv_inc_in_flight (bs); +aio_co_enter (bdrv_get_aio_context (bs), co); } }
@@ -1916,6 +1926,21 @@ static int coroutine_fn bdrv_replace_test_co_preadv(BlockDriverState *bs,return 0 ; } +static void coroutine_fn bdrv_replace_test_drain_co(void *opaque) +{ +BlockDriverState *bs = opaque; +BDRVReplaceTestState *s = bs->opaque; + +/* Keep waking io_co up until it is done */ +while (s->io_co) { +aio_co_wake (s->io_co); + s->io_co =NULL ; +qemu_coroutine_yield (); + } + s->drain_co =NULL ; +bdrv_dec_in_flight (bs); +} +/** * If .drain_count is 0, wake up .io_co if there is one; and set * .was_drained.
@@ -1926,20 +1951,27 @@ static void coroutine_fn bdrv_replace_test_co_drain_begin(BlockDriverState *bs)BDRVReplaceTestState *s = bs->opaque;if (!s->drain_count) { -/* Keep waking io_co up until it is done */ - s->drain_co =qemu_coroutine_self (); -while (s->io_co) { -aio_co_wake (s->io_co); - s->io_co =NULL ; -qemu_coroutine_yield (); - } - s->drain_co =NULL ; - + s->drain_co =qemu_coroutine_create (bdrv_replace_test_drain_co, bs); +bdrv_inc_in_flight (bs); +aio_co_enter (bdrv_get_aio_context (bs), s->drain_co); s->was_drained = true; } s->drain_count++; } +static void coroutine_fn bdrv_replace_test_read_entry(void *opaque) +{ +BlockDriverState *bs = opaque; +char data; +QEMUIOVector qiov =QEMU_IOVEC_INIT_BUF (qiov, &data,1 ); +int ret; + +/* Queue a read request post-drain */ + ret =bdrv_replace_test_co_preadv (bs,0 ,1 , &qiov,0 ); +g_assert (ret >=0 ); +bdrv_dec_in_flight (bs); +} +/** * Reduce .drain_count, set .was_undrained once it reaches 0. * If .drain_count reaches 0 and the node has a backing file, issue a
@@ -1951,17 +1983,13 @@ static void coroutine_fn bdrv_replace_test_co_drain_end(BlockDriverState *bs)g_assert (s->drain_count >0 );if (!--s->drain_count) { -int ret; - s->was_undrained = true;if (bs->backing) { -char data; -QEMUIOVector qiov =QEMU_IOVEC_INIT_BUF (qiov, &data,1 ); - -/* Queue a read request post-drain */ - ret =bdrv_replace_test_co_preadv (bs,0 ,1 , &qiov,0 ); -g_assert (ret >=0 ); +Coroutine *co =qemu_coroutine_create (bdrv_replace_test_read_entry, + bs); +bdrv_inc_in_flight (bs); +aio_co_enter (bdrv_get_aio_context (bs), co); } } }
Signature · 1 line
2.38.1
PATCH v2 3/15 block: Revert .bdrv_drained_begin/end to non-coroutine_fn
In reply to Kevin WolfPolling during bdrv_drained_end() can be problematic (and in the future, we may get cases for bdrv_drained_begin() where polling is forbidden, and we don't care about already in-flight requests, but just want to prevent new requests from arriving).
The .bdrv_drained_begin/end callbacks running in a coroutine is the only reason why we have to do this polling, so make them non-coroutine callbacks again. None of the callers actually yield any more.
This means that bdrv_drained_end() effectively doesn't poll any more, even if AIO_WAIT_WHILE() loops are still there (their condition is false from the beginning). This is generally not a problem, but in test-bdrv-drain, some additional explicit aio_poll() calls need to be added because the test case wants to verify the final state after BHs have executed.
--- include/block/block_int-common.h | 10 ++++--- block.c | 4 +-- block/io.c | 49 +++++--------------------------- block/qed.c | 6 ++-- block/throttle.c | 8 +++--- tests/unit/test-bdrv-drain.c | 18 ++++++------ 6 files changed, 32 insertions(+), 63 deletions(-)
include/block/block_int-common.h +6 -4
@@ -735,17 +735,19 @@ struct BlockDriver {void (*bdrv_io_unplug)(BlockDriverState *bs);/** -* bdrv_co_drain_begin is called if implemented in the beginning of a +* bdrv_drain_begin is called if implemented in the beginning of a * drain operation to drain and stop any internal sources of requests in * the driver. -* bdrv_co_drain_end is called if implemented at the end of the drain. +* bdrv_drain_end is called if implemented at the end of the drain. * * They should be used by the driver to e.g. manage scheduled I/O * requests, or toggle an internal state. After the end of the drain new * requests will continue normally. +* +* Implementations of both functions must not call aio_poll(). */ - void coroutine_fn (*bdrv_co_drain_begin)(BlockDriverState *bs); - void coroutine_fn (*bdrv_co_drain_end)(BlockDriverState *bs); +void (*bdrv_drain_begin)(BlockDriverState *bs); +void (*bdrv_drain_end)(BlockDriverState *bs);bool (*bdrv_supports_persistent_dirty_bitmap)(BlockDriverState *bs); bool coroutine_fn (*bdrv_co_can_store_new_dirty_bitmap)(
block.c +2 -2
@@ -1715,8 +1715,8 @@ static int bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv,assert (is_power_of_2 (bs->bl.request_alignment));for (i =0 ; i < bs->quiesce_counter; i++) { -if (drv->bdrv_co_drain_begin) { - drv->bdrv_co_drain_begin (bs); +if (drv->bdrv_drain_begin) { + drv->bdrv_drain_begin (bs); } }
block/io.c +7 -42
62 lines not on this page · raw message
block/qed.c +3 -3
@@ -262,7 +262,7 @@ static bool coroutine_fn qed_plug_allocating_write_reqs(BDRVQEDState *s)assert (!s->allocating_write_reqs_plugged);if (s->allocating_acb !=NULL ) {/* Another allocating write came concurrently. This cannot happen -* from bdrv_qed_co_drain_begin, but it can happen when the timer runs. +* from bdrv_qed_drain_begin, but it can happen when the timer runs. */ qemu_co_mutex_unlock (&s->table_lock);return false;
@@ -365,7 +365,7 @@ static void bdrv_qed_attach_aio_context(BlockDriverState *bs, } } -static void coroutine_fn bdrv_qed_co_drain_begin(BlockDriverState *bs) +static void bdrv_qed_drain_begin (BlockDriverState *bs) {BDRVQEDState *s = bs->opaque;
@@ -1661,7 +1661,7 @@ static BlockDriver bdrv_qed = { .bdrv_co_check = bdrv_qed_co_check, .bdrv_detach_aio_context = bdrv_qed_detach_aio_context, .bdrv_attach_aio_context = bdrv_qed_attach_aio_context, - .bdrv_co_drain_begin = bdrv_qed_co_drain_begin, + .bdrv_drain_begin = bdrv_qed_drain_begin, };static void bdrv_qed_init (void )
block/throttle.c +4 -4
@@ -214,7 +214,7 @@ static void throttle_reopen_abort(BDRVReopenState *reopen_state) reopen_state->opaque =NULL ; } -static void coroutine_fn throttle_co_drain_begin(BlockDriverState *bs) +static void throttle_drain_begin (BlockDriverState *bs) {ThrottleGroupMember *tgm = bs->opaque;if (qatomic_fetch_inc (&tgm->io_limits_disabled) ==0 ) {
@@ -222,7 +222,7 @@ static void coroutine_fn throttle_co_drain_begin(BlockDriverState *bs) } } -static void coroutine_fn throttle_co_drain_end(BlockDriverState *bs) +static void throttle_drain_end (BlockDriverState *bs) {ThrottleGroupMember *tgm = bs->opaque;assert (tgm->io_limits_disabled);
@@ -261,8 +261,8 @@ static BlockDriver bdrv_throttle = { .bdrv_reopen_commit = throttle_reopen_commit, .bdrv_reopen_abort = throttle_reopen_abort, - .bdrv_co_drain_begin = throttle_co_drain_begin, - .bdrv_co_drain_end = throttle_co_drain_end, + .bdrv_drain_begin = throttle_drain_begin, + .bdrv_drain_end = throttle_drain_end, .is_filter = true, .strong_runtime_opts = throttle_strong_runtime_opts,
tests/unit/test-bdrv-drain.c +10 -8
@@ -46,7 +46,7 @@ static void coroutine_fn sleep_in_drain_begin(void *opaque)bdrv_dec_in_flight (bs); } -static void coroutine_fn bdrv_test_co_drain_begin(BlockDriverState *bs) +static void bdrv_test_drain_begin (BlockDriverState *bs) {BDRVTestState *s = bs->opaque; s->drain_count++;
@@ -57,7 +57,7 @@ static void coroutine_fn bdrv_test_co_drain_begin(BlockDriverState *bs) } } -static void coroutine_fn bdrv_test_co_drain_end(BlockDriverState *bs) +static void bdrv_test_drain_end (BlockDriverState *bs) {BDRVTestState *s = bs->opaque; s->drain_count--;
@@ -111,8 +111,8 @@ static BlockDriver bdrv_test = { .bdrv_close = bdrv_test_close, .bdrv_co_preadv = bdrv_test_co_preadv, - .bdrv_co_drain_begin = bdrv_test_co_drain_begin, - .bdrv_co_drain_end = bdrv_test_co_drain_end, + .bdrv_drain_begin = bdrv_test_drain_begin, + .bdrv_drain_end = bdrv_test_drain_end, .bdrv_child_perm = bdrv_default_perms,
@@ -1703,6 +1703,7 @@ static void test_blockjob_commit_by_drained_end(void)bdrv_drained_begin (bs_child);g_assert (!job_has_completed);bdrv_drained_end (bs_child); +aio_poll (qemu_get_aio_context (), false);g_assert (job_has_completed);bdrv_unref (bs_parents[0 ]);
@@ -1858,6 +1859,7 @@ static void test_drop_intermediate_poll(void)g_assert (!job_has_completed); ret =bdrv_drop_intermediate (chain[1 ], chain[0 ],NULL ); +aio_poll (qemu_get_aio_context (), false);g_assert (ret ==0 );g_assert (job_has_completed);
@@ -1946,7 +1948,7 @@ static void coroutine_fn bdrv_replace_test_drain_co(void *opaque)* .was_drained. * Increment .drain_count. */ -static void coroutine_fn bdrv_replace_test_co_drain_begin(BlockDriverState *bs) +static void bdrv_replace_test_drain_begin (BlockDriverState *bs) {BDRVReplaceTestState *s = bs->opaque;
@@ -1977,7 +1979,7 @@ static void coroutine_fn bdrv_replace_test_read_entry(void *opaque)* If .drain_count reaches 0 and the node has a backing file, issue a * read request. */ -static void coroutine_fn bdrv_replace_test_co_drain_end(BlockDriverState *bs) +static void bdrv_replace_test_drain_end (BlockDriverState *bs) {BDRVReplaceTestState *s = bs->opaque;
@@ -2002,8 +2004,8 @@ static BlockDriver bdrv_replace_test = { .bdrv_close = bdrv_replace_test_close, .bdrv_co_preadv = bdrv_replace_test_co_preadv, - .bdrv_co_drain_begin = bdrv_replace_test_co_drain_begin, - .bdrv_co_drain_end = bdrv_replace_test_co_drain_end, + .bdrv_drain_begin = bdrv_replace_test_drain_begin, + .bdrv_drain_end = bdrv_replace_test_drain_end, .bdrv_child_perm = bdrv_default_perms, };
Signature · 1 line
2.38.1
PATCH v2 4/15 block: Remove drained_end_counter
In reply to Kevin Wolfdrained_end_counter is unused now, nobody changes its value any more. It can be removed.
In cases where we had two almost identical functions that only differed in whether the caller passes drained_end_counter, or whether they would poll for a local drained_end_counter to reach 0, these become a single function.
--- include/block/block-io.h | 24 -------- include/block/block_int-common.h | 6 +- block.c | 5 +- block/block-backend.c | 4 +- block/io.c | 98 ++++++++------------------------ blockjob.c | 2 +- 6 files changed, 30 insertions(+), 109 deletions(-)
include/block/block-io.h +0 -24
@@ -237,21 +237,6 @@ int coroutine_fn bdrv_co_copy_range(BdrvChild *src, int64_t src_offset, int64_t bytes, BdrvRequestFlags read_flags, BdrvRequestFlags write_flags); -/** -* bdrv_drained_end_no_poll: -* -* Same as bdrv_drained_end(), but do not poll for the subgraph to -* actually become unquiesced. Therefore, no graph changes will occur -* with this function. -* -* *drained_end_counter is incremented for every background operation -* that is scheduled, and will be decremented for every operation once -* it settles. The caller must poll until it reaches 0. The counter -* should be accessed using atomic operations only. -*/ -void bdrv_drained_end_no_poll (BlockDriverState *bs,int *drained_end_counter); - -/* * "I/O or GS" API functions. These functions can run without * the BQL, but only in one specific iothread/main loop.
@@ -311,9 +296,6 @@ void bdrv_parent_drained_begin_single(BdrvChild *c, bool poll);* bdrv_parent_drained_end_single: * * End a quiesced section for the parent of @c. -* -* This polls @bs's AioContext until all scheduled sub-drained_ends -* have settled, which may result in graph changes. */ void bdrv_parent_drained_end_single (BdrvChild *c);
@@ -361,12 +343,6 @@ void bdrv_subtree_drained_begin(BlockDriverState *bs);* bdrv_drained_end: * * End a quiescent section started by bdrv_drained_begin(). -* -* This polls @bs's AioContext until all scheduled sub-drained_ends -* have settled. On one hand, that may result in graph changes. On -* the other, this requires that the caller either runs in the main -* loop; or that all involved nodes (@bs and all of its parents) are -* in the caller's AioContext. */ void bdrv_drained_end (BlockDriverState *bs);
include/block/block_int-common.h +1 -5
@@ -939,15 +939,11 @@ struct BdrvChildClass {* These functions must not change the graph (and therefore also must not * call aio_poll(), which could change the graph indirectly). * -* If drained_end() schedules background operations, it must atomically -* increment *drained_end_counter for each such operation and atomically -* decrement it once the operation has settled. -* * Note that this can be nested. If drained_begin() was called twice, new * I/O is allowed only after drained_end() was called twice, too. */ void (*drained_begin)(BdrvChild *child); -void (*drained_end)(BdrvChild *child,int *drained_end_counter); +void (*drained_end)(BdrvChild *child);/* * Returns whether the parent has pending requests for the child. This
block.c +2 -3
@@ -1237,11 +1237,10 @@ static bool bdrv_child_cb_drained_poll(BdrvChild *child)return bdrv_drain_poll (bs, false,NULL , false); } -static void bdrv_child_cb_drained_end (BdrvChild *child, -int *drained_end_counter) +static void bdrv_child_cb_drained_end (BdrvChild *child) {BlockDriverState *bs = child->opaque; -bdrv_drained_end_no_poll (bs, drained_end_counter); +bdrv_drained_end (bs); }static int bdrv_child_cb_inactivate (BdrvChild *child)
block/block-backend.c +2 -2
@@ -129,7 +129,7 @@ static void blk_root_inherit_options(BdrvChildRole role, bool parent_is_format, }static void blk_root_drained_begin (BdrvChild *child);static bool blk_root_drained_poll (BdrvChild *child); -static void blk_root_drained_end (BdrvChild *child,int *drained_end_counter); +static void blk_root_drained_end (BdrvChild *child);static void blk_root_change_media (BdrvChild *child,bool load);static void blk_root_resize (BdrvChild *child);
@@ -2556,7 +2556,7 @@ static bool blk_root_drained_poll(BdrvChild *child)return busy || !!blk->in_flight; } -static void blk_root_drained_end (BdrvChild *child,int *drained_end_counter) +static void blk_root_drained_end (BdrvChild *child) {BlockBackend *blk = child->opaque;assert (blk->quiesce_counter);
block/io.c +24 -74
250 lines not on this page · raw message
blockjob.c +1 -1
@@ -120,7 +120,7 @@ static bool child_job_drained_poll(BdrvChild *c) } } -static void child_job_drained_end (BdrvChild *c,int *drained_end_counter) +static void child_job_drained_end (BdrvChild *c) {BlockJob *job = c->opaque;job_resume (&job->job);
Signature · 1 line
2.38.1
PATCH v2 5/15 block: Inline bdrv_drain_invoke()
In reply to Kevin Wolfbdrv_drain_invoke() has now two entirely separate cases that share no code any more and are selected depending on a bool parameter. Each case has only one caller. Just inline the function.
--- block/io.c | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-)
block/io.c +6 -17
@@ -242,21 +242,6 @@ typedef struct {bool ignore_bds_parents; } BdrvCoDrainData; -/* Recursively call BlockDriver.bdrv_drain_begin/end callbacks */ -static void bdrv_drain_invoke (BlockDriverState *bs,bool begin) -{ -if (!bs->drv || (begin && !bs->drv->bdrv_drain_begin) || - (!begin && !bs->drv->bdrv_drain_end)) { -return ; - } - -if (begin) { - bs->drv->bdrv_drain_begin (bs); - }else { - bs->drv->bdrv_drain_end (bs); - } -} -/* Returns true if BDRV_POLL_WHILE() should go into a blocking aio_poll() */ bool bdrv_drain_poll (BlockDriverState *bs,bool recursive,BdrvChild *ignore_parent,bool ignore_bds_parents)
@@ -390,7 +375,9 @@ void bdrv_do_drained_begin_quiesce(BlockDriverState *bs, }bdrv_parent_drained_begin (bs, parent, ignore_bds_parents); -bdrv_drain_invoke (bs, true); +if (bs->drv && bs->drv->bdrv_drain_begin) { + bs->drv->bdrv_drain_begin (bs); + } } static void bdrv_do_drained_begin(BlockDriverState *bs, bool recursive,
@@ -461,7 +448,9 @@ static void bdrv_do_drained_end(BlockDriverState *bs, bool recursive,assert (bs->quiesce_counter >0 );/* Re-enable things in child-to-parent order */ -bdrv_drain_invoke (bs, false); +if (bs->drv && bs->drv->bdrv_drain_end) { + bs->drv->bdrv_drain_end (bs); + }bdrv_parent_drained_end (bs, parent, ignore_bds_parents); old_quiesce_counter =qatomic_fetch_dec (&bs->quiesce_counter);
Signature · 1 line
2.38.1
PATCH v2 6/15 block: Fix locking for bdrv_reopen_queue_child()
In reply to Kevin WolfCallers don't agree whether bdrv_reopen_queue_child() should be called with the AioContext lock held or not. Standardise on holding the lock (as done by QMP blockdev-reopen and the replication block driver) and fix bdrv_reopen() to do the same.
--- block.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
block.c +5 -2
@@ -4153,6 +4153,8 @@ static bool bdrv_recurse_has_child(BlockDriverState *bs,* bs_queue, or the existing bs_queue being used. * * bs must be drained between bdrv_reopen_queue() and bdrv_reopen_multiple(). +* +* To be called with bs->aio_context locked. */ static BlockReopenQueue *bdrv_reopen_queue_child(BlockReopenQueue *bs_queue, BlockDriverState *bs,
@@ -4311,6 +4313,7 @@ static BlockReopenQueue *bdrv_reopen_queue_child(BlockReopenQueue *bs_queue,return bs_queue; } +/* To be called with bs->aio_context locked */ BlockReopenQueue *bdrv_reopen_queue (BlockReopenQueue *bs_queue,BlockDriverState *bs,QDict *options,bool keep_old_opts)
@@ -4475,11 +4478,11 @@ int bdrv_reopen(BlockDriverState *bs, QDict *opts, bool keep_old_opts,GLOBAL_STATE_CODE ();bdrv_subtree_drained_begin (bs); + queue =bdrv_reopen_queue (NULL , bs, opts, keep_old_opts); +if (ctx !=qemu_get_aio_context ()) {aio_context_release (ctx); } - - queue =bdrv_reopen_queue (NULL , bs, opts, keep_old_opts); ret =bdrv_reopen_multiple (queue, errp);if (ctx !=qemu_get_aio_context ()) {
Signature · 1 line
2.38.1
Re: [PATCH v2 06/15] block: Fix locking for bdrv_reopen_queue_child()
Quoted 6 lines · Kevin Wolf
Callers don't agree whether
bdrv_reopen_queue_child()should be called with the AioContext lock held or not. Standardise on holding the lock (as done by QMP blockdev-reopen and the replication block driver) and fixbdrv_reopen()to do the same.Signed-off-by Kevin Wolf<[email protected]>
Signature · 2 lines
Best regards, Vladimir
PATCH v2 7/15 block: Drain invidual nodes during reopen
In reply to Kevin Wolfbdrv_reopen() and friends use subtree drains as a lazy way of covering all the nodes they touch. Turns out that this lazy way is a lot more complicated than just draining the nodes individually, even not accounting for the additional complexity in the drain mechanism itself.
Simplify the code by switching to draining the individual nodes that are already managed in the BlockReopenQueue anyway.
--- block.c | 16 +++++++++------- block/replication.c | 6 ------ blockdev.c | 13 ------------- 3 files changed, 9 insertions(+), 26 deletions(-)
block.c +9 -7
@@ -4152,7 +4152,7 @@ static bool bdrv_recurse_has_child(BlockDriverState *bs,* returns a pointer to bs_queue, which is either the newly allocated * bs_queue, or the existing bs_queue being used. * -* bs must be drained between bdrv_reopen_queue() and bdrv_reopen_multiple(). +* bs is drained here and undrained by bdrv_reopen_queue_free(). * * To be called with bs->aio_context locked. */
@@ -4174,12 +4174,10 @@ static BlockReopenQueue *bdrv_reopen_queue_child(BlockReopenQueue *bs_queue,int flags;QemuOpts *opts; -/* Make sure that the caller remembered to use a drained section. This is -* important to avoid graph changes between the recursive queuing here and -* bdrv_reopen_multiple(). */ -assert (bs->quiesce_counter >0 );GLOBAL_STATE_CODE (); +bdrv_drained_begin (bs); +if (bs_queue ==NULL ) { bs_queue =g_new0 (BlockReopenQueue,1 );QTAILQ_INIT (bs_queue);
@@ -4330,6 +4328,12 @@ void bdrv_reopen_queue_free(BlockReopenQueue *bs_queue)if (bs_queue) {BlockReopenQueueEntry *bs_entry, *next; QTAILQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) { +AioContext *ctx =bdrv_get_aio_context (bs_entry->state.bs); + +aio_context_acquire (ctx); +bdrv_drained_end (bs_entry->state.bs); +aio_context_release (ctx); +qobject_unref (bs_entry->state.explicit_options);qobject_unref (bs_entry->state.options);g_free (bs_entry);
@@ -4477,7 +4481,6 @@ int bdrv_reopen(BlockDriverState *bs, QDict *opts, bool keep_old_opts,GLOBAL_STATE_CODE (); -bdrv_subtree_drained_begin (bs); queue =bdrv_reopen_queue (NULL , bs, opts, keep_old_opts);if (ctx !=qemu_get_aio_context ()) {
@@ -4488,7 +4491,6 @@ int bdrv_reopen(BlockDriverState *bs, QDict *opts, bool keep_old_opts,if (ctx !=qemu_get_aio_context ()) {aio_context_acquire (ctx); } -bdrv_subtree_drained_end (bs);return ret; }
block/replication.c +0 -6
@@ -374,9 +374,6 @@ static void reopen_backing_file(BlockDriverState *bs, bool writable, s->orig_secondary_read_only =bdrv_is_read_only (secondary_disk->bs); } -bdrv_subtree_drained_begin (hidden_disk->bs); -bdrv_subtree_drained_begin (secondary_disk->bs); -if (s->orig_hidden_read_only) {QDict *opts =qdict_new ();qdict_put_bool (opts,BDRV_OPT_READ_ONLY , !writable);
@@ -401,9 +398,6 @@ static void reopen_backing_file(BlockDriverState *bs, bool writable,aio_context_acquire (ctx); } } - -bdrv_subtree_drained_end (hidden_disk->bs); -bdrv_subtree_drained_end (secondary_disk->bs); }static void backup_job_cleanup (BlockDriverState *bs)
blockdev.c +0 -13
@@ -3547,8 +3547,6 @@ fail:void qmp_blockdev_reopen (BlockdevOptionsList *reopen_list,Error **errp) {BlockReopenQueue *queue =NULL ; -GSList *drained =NULL ; -GSList *p;/* Add each one of the BDS that we want to reopen to the queue */ for (; reopen_list !=NULL ; reopen_list = reopen_list->next) {
@@ -3585,9 +3583,7 @@ void qmp_blockdev_reopen(BlockdevOptionsList *reopen_list, Error **errp) ctx =bdrv_get_aio_context (bs);aio_context_acquire (ctx); -bdrv_subtree_drained_begin (bs); queue =bdrv_reopen_queue (queue, bs, qdict, false); - drained =g_slist_prepend (drained, bs);aio_context_release (ctx); }
@@ -3598,15 +3594,6 @@ void qmp_blockdev_reopen(BlockdevOptionsList *reopen_list, Error **errp) fail:bdrv_reopen_queue_free (queue); -for (p = drained; p; p = p->next) { -BlockDriverState *bs = p->data; -AioContext *ctx =bdrv_get_aio_context (bs); - -aio_context_acquire (ctx); -bdrv_subtree_drained_end (bs); -aio_context_release (ctx); - } -g_slist_free (drained); }void qmp_blockdev_del (const char *node_name,Error **errp)
Signature · 1 line
2.38.1
Re: [PATCH v2 07/15] block: Drain invidual nodes during reopen
in subject: s/invidual/individual/
Quoted 9 lines · Kevin Wolf
bdrv_reopen()and friends use subtree drains as a lazy way of covering all the nodes they touch. Turns out that this lazy way is a lot more complicated than just draining the nodes individually, even not accounting for the additional complexity in the drain mechanism itself.Simplify the code by switching to draining the individual nodes that are already managed in the BlockReopenQueue anyway.
Signed-off-by Kevin Wolf<[email protected]>
Signature · 2 lines
Best regards, Vladimir
PATCH v2 8/15 block: Don't use subtree drains in bdrv_drop_intermediate()
In reply to Kevin WolfInstead of using a subtree drain from the top node (which also drains child nodes of base that we're not even interested in), use a normal drain for base, which automatically drains all of the parents, too.
--- block.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
block.c +2 -2
@@ -5599,7 +5599,7 @@ int bdrv_drop_intermediate(BlockDriverState *top, BlockDriverState *base,GLOBAL_STATE_CODE ();bdrv_ref (top); -bdrv_subtree_drained_begin (top); +bdrv_drained_begin (base);if (!top->drv || !base->drv) { goto exit;
@@ -5672,7 +5672,7 @@ int bdrv_drop_intermediate(BlockDriverState *top, BlockDriverState *base, ret =0 ; exit: -bdrv_subtree_drained_end (top); +bdrv_drained_end (base);bdrv_unref (top);return ret; }
Signature · 1 line
2.38.1
PATCH v2 9/15 stream: Replace subtree drain with a single node drain
In reply to Kevin WolfThe subtree drain was introduced in commit b1e1af394d9 as a way to avoid graph changes between finding the base node and changing the block graph as necessary on completion of the image streaming job.
The block graph could change between these two points because bdrv_set_backing_hd() first drains the parent node, which involved polling and can do anything.
Subtree draining was an imperfect way to make this less likely (because with it, fewer callbacks are called during this window). Everyone agreed that it's not really the right solution, and it was only committed as a stopgap solution.
This replaces the subtree drain with a solution that simply drains the parent node before we try to find the base node, and then call a version of bdrv_set_backing_hd() that doesn't drain, but just asserts that the parent node is already drained.
This way, any graph changes caused by draining happen before we start looking at the graph and things stay consistent between finding the base node and changing the graph.
--- include/block/block-global-state.h | 3 +++ block.c | 17 ++++++++++++++--- block/stream.c | 26 ++++++++++++++++---------- 3 files changed, 33 insertions(+), 13 deletions(-)
include/block/block-global-state.h +3 -0
@@ -82,6 +82,9 @@ int bdrv_open_file_child(const char *filename,BlockDriverState *bdrv_open_blockdev_ref (BlockdevRef *ref,Error **errp);int bdrv_set_backing_hd (BlockDriverState *bs,BlockDriverState *backing_hd,Error **errp); +int bdrv_set_backing_hd_drained (BlockDriverState *bs, +BlockDriverState *backing_hd, +Error **errp);int bdrv_open_backing_file (BlockDriverState *bs,QDict *parent_options,const char *bdref_key,Error **errp); BlockDriverState *bdrv_open(const char *filename, const char *reference,
block.c +14 -3
@@ -3405,14 +3405,15 @@ static int bdrv_set_backing_noperm(BlockDriverState *bs,return bdrv_set_file_or_backing_noperm (bs, backing_hd, true, tran, errp); } -int bdrv_set_backing_hd (BlockDriverState *bs,BlockDriverState *backing_hd, -Error **errp) +int bdrv_set_backing_hd_drained (BlockDriverState *bs, +BlockDriverState *backing_hd, +Error **errp) {int ret;Transaction *tran =tran_new ();GLOBAL_STATE_CODE (); -bdrv_drained_begin (bs); +assert (bs->quiesce_counter >0 ); ret =bdrv_set_backing_noperm (bs, backing_hd, tran, errp);if (ret <0 ) {
@@ -3422,7 +3423,17 @@ int bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd, ret =bdrv_refresh_perms (bs, errp); out:tran_finalize (tran, ret); +return ret; +} +int bdrv_set_backing_hd (BlockDriverState *bs,BlockDriverState *backing_hd, +Error **errp) +{ +int ret; +GLOBAL_STATE_CODE (); + +bdrv_drained_begin (bs); + ret =bdrv_set_backing_hd_drained (bs, backing_hd, errp);bdrv_drained_end (bs);return ret;
block/stream.c +16 -10
@@ -64,13 +64,16 @@ static int stream_prepare(Job *job)bdrv_cor_filter_drop (s->cor_filter_bs); s->cor_filter_bs =NULL ; -bdrv_subtree_drained_begin (s->above_base); +/* +* bdrv_set_backing_hd() requires that unfiltered_bs is drained. Drain +* already here and use bdrv_set_backing_hd_drained() instead because +* the polling during drained_begin() might change the graph, and if we do +* this only later, we may end up working with the wrong base node (or it +* might even have gone away by the time we want to use it). +*/ +bdrv_drained_begin (unfiltered_bs); base =bdrv_filter_or_cow_bs (s->above_base); -if (base) { -bdrv_ref (base); - } - unfiltered_base =bdrv_skip_filters (base);if (bdrv_cow_child (unfiltered_bs)) {
@@ -82,7 +85,13 @@ static int stream_prepare(Job *job) } } -bdrv_set_backing_hd (unfiltered_bs, base, &local_err); +bdrv_set_backing_hd_drained (unfiltered_bs, base, &local_err); + +/* +* This call will do I/O, so the graph can change again from here on. +* We have already completed the graph change, so we are not in danger +* of operating on the wrong node any more if this happens. +*/ ret =bdrv_change_backing_file (unfiltered_bs, base_id, base_fmt, false);if (local_err) {error_report_err (local_err);
@@ -92,10 +101,7 @@ static int stream_prepare(Job *job) } out: -if (base) { -bdrv_unref (base); - } -bdrv_subtree_drained_end (s->above_base); +bdrv_drained_end (unfiltered_bs);return ret; }
Signature · 1 line
2.38.1
PATCH v2 10/15 block: Remove subtree drains
In reply to Kevin WolfSubtree drains are not used any more. Remove them.
After this, BdrvChildClass.attach/detach() don't poll any more.
--- include/block/block-io.h | 18 +-- include/block/block_int-common.h | 1 - include/block/block_int-io.h | 12 -- block.c | 20 +-- block/io.c | 121 +++----------- tests/unit/test-bdrv-drain.c | 261 ++----------------------------- 6 files changed, 44 insertions(+), 389 deletions(-)
include/block/block-io.h +3 -15
@@ -302,8 +302,7 @@ void bdrv_parent_drained_end_single(BdrvChild *c);/** * bdrv_drain_poll: * -* Poll for pending requests in @bs, its parents (except for @ignore_parent), -* and if @recursive is true its children as well (used for subtree drain). +* Poll for pending requests in @bs and its parents (except for @ignore_parent). * * If @ignore_bds_parents is true, parents that are BlockDriverStates must * ignore the drain request because they will be drained separately (used for
@@ -311,8 +310,8 @@ void bdrv_parent_drained_end_single(BdrvChild *c);* * This is part of bdrv_drained_begin. */ -bool bdrv_drain_poll (BlockDriverState *bs,bool recursive, -BdrvChild *ignore_parent,bool ignore_bds_parents); +bool bdrv_drain_poll (BlockDriverState *bs,BdrvChild *ignore_parent, +bool ignore_bds_parents);/** * bdrv_drained_begin:
@@ -333,12 +332,6 @@ void bdrv_drained_begin(BlockDriverState *bs);void bdrv_do_drained_begin_quiesce (BlockDriverState *bs,BdrvChild *parent,bool ignore_bds_parents); -/** -* Like bdrv_drained_begin, but recursively begins a quiesced section for -* exclusive access to all child nodes as well. -*/ -void bdrv_subtree_drained_begin (BlockDriverState *bs); -/** * bdrv_drained_end: *
@@ -346,9 +339,4 @@ void bdrv_subtree_drained_begin(BlockDriverState *bs);*/ void bdrv_drained_end (BlockDriverState *bs); -/** -* End a quiescent section started by bdrv_subtree_drained_begin(). -*/ -void bdrv_subtree_drained_end (BlockDriverState *bs); -#endif /* BLOCK_IO_H */
include/block/block_int-common.h +0 -1
@@ -1184,7 +1184,6 @@ struct BlockDriverState {/* Accessed with atomic ops. */ int quiesce_counter; -int recursive_quiesce_counter;unsigned int write_gen;/* Current data generation */
include/block/block_int-io.h +0 -12
@@ -179,16 +179,4 @@ void bdrv_bsc_invalidate_range(BlockDriverState *bs,*/ void bdrv_bsc_fill (BlockDriverState *bs,int64_t offset,int64_t bytes); - -/* -* "I/O or GS" API functions. These functions can run without -* the BQL, but only in one specific iothread/main loop. -* -* See include/block/block-io.h for more information about -* the "I/O or GS" API. -*/ - -void bdrv_apply_subtree_drain (BdrvChild *child,BlockDriverState *new_parent); -void bdrv_unapply_subtree_drain (BdrvChild *child,BlockDriverState *old_parent); -#endif /* BLOCK_INT_IO_H */
block.c +5 -15
@@ -1234,7 +1234,7 @@ static void bdrv_child_cb_drained_begin(BdrvChild *child)static bool bdrv_child_cb_drained_poll (BdrvChild *child) {BlockDriverState *bs = child->opaque; -return bdrv_drain_poll (bs, false,NULL , false); +return bdrv_drain_poll (bs,NULL , false); }static void bdrv_child_cb_drained_end (BdrvChild *child)
@@ -1484,8 +1484,6 @@ static void bdrv_child_cb_attach(BdrvChild *child)assert (!bs->file); bs->file = child; } - -bdrv_apply_subtree_drain (child, bs); }static void bdrv_child_cb_detach (BdrvChild *child)
@@ -1496,8 +1494,6 @@ static void bdrv_child_cb_detach(BdrvChild *child)bdrv_backing_detach (child); } -bdrv_unapply_subtree_drain (child, bs); -assert_bdrv_graph_writable (bs);QLIST_REMOVE (child, next);if (child == bs->backing) {
@@ -2853,9 +2849,6 @@ static void bdrv_replace_child_noperm(BdrvChild *child, }if (old_bs) { -/* Detach first so that the recursive drain sections coming from @child -* are already gone and we only end the drain sections that came from -* elsewhere. */ if (child->klass->detach) { child->klass->detach (child); }
@@ -2870,17 +2863,14 @@ static void bdrv_replace_child_noperm(BdrvChild *child,QLIST_INSERT_HEAD (&new_bs->parents, child, next_parent);/* -* Detaching the old node may have led to the new node's -* quiesce_counter having been decreased. Not a problem, we -* just need to recognize this here and then invoke -* drained_end appropriately more often. +* Polling in bdrv_parent_drained_begin_single() may have led to the new +* node's quiesce_counter having been decreased. Not a problem, we just +* need to recognize this here and then invoke drained_end appropriately +* more often. */ assert (new_bs->quiesce_counter <= new_bs_quiesce_counter); drain_saldo += new_bs->quiesce_counter - new_bs_quiesce_counter; -/* Attach only after starting new drained sections, so that recursive -* drain sections coming from @child don't get an extra .drained_begin -* callback. */ if (child->klass->attach) { child->klass->attach (child); }
block/io.c +26 -95
243 lines not on this page · raw message
tests/unit/test-bdrv-drain.c +10 -251
431 lines not on this page · raw message
Signature · 1 line
2.38.1
PATCH v2 11/15 block: Call drain callbacks only once
In reply to Kevin WolfWe only need to call both the BlockDriver's callback and the parent callbacks when going from undrained to drained or vice versa. A second drain section doesn't make a difference for the driver or the parent, they weren't supposed to send new requests before and after the second drain.
One thing that gets in the way is the 'ignore_bds_parents' parameter in bdrv_do_drained_begin_quiesce() and bdrv_do_drained_end(): It means that bdrv_drain_all_begin() increases bs->quiesce_counter, but does not quiesce the parent through BdrvChildClass callbacks. If an additional drain section is started now, bs->quiesce_counter will be non-zero, but we would still need to quiesce the parent through BdrvChildClass in order to keep things consistent (and unquiesce it on the matching bdrv_drained_end(), even though the counter would reach 0 yet as long as the bdrv_drain_all() section is still active).
Instead of keeping track of this, let's just get rid of the parameter. It was introduced in commit 6cd5c9d7b2d as an optimisation so that during bdrv_drain_all(), we wouldn't recursively drain all parents up to the root for each node, resulting in quadratic complexity. As it happens, calling the callbacks only once solves the same problem, so as of this patch, we'll still have O(n) complexity and ignore_bds_parents is not needed any more.
This patch only ignores the 'ignore_bds_parents' parameter. It will be removed in a separate patch.
--- include/block/block_int-common.h | 8 ++++---- block.c | 25 +++++++------------------ block/io.c | 30 ++++++++++++++++++------------ tests/unit/test-bdrv-drain.c | 16 ++++++++++------ 4 files changed, 39 insertions(+), 40 deletions(-)
include/block/block_int-common.h +4 -4
@@ -980,13 +980,13 @@ struct BdrvChild {bool frozen;/* -* How many times the parent of this child has been drained +* True if the parent of this child has been drained by this BdrvChild * (through klass->drained_*). -* Usually, this is equal to bs->quiesce_counter (potentially -* reduced by bdrv_drain_all_count). It may differ while the +* +* It is generally true if bs->quiesce_counter > 0. It may differ while the * child is entering or leaving a drained section. */ -int parent_quiesce_counter; +bool quiesced_parent;QLIST_ENTRY (BdrvChild) next;QLIST_ENTRY (BdrvChild) next_parent;
block.c +7 -18
@@ -2826,7 +2826,6 @@ static void bdrv_replace_child_noperm(BdrvChild *child, {BlockDriverState *old_bs = child->bs;int new_bs_quiesce_counter; -int drain_saldo;assert (!child->frozen);assert (old_bs != new_bs);
@@ -2836,16 +2835,13 @@ static void bdrv_replace_child_noperm(BdrvChild *child,assert (bdrv_get_aio_context (old_bs) ==bdrv_get_aio_context (new_bs)); } - new_bs_quiesce_counter = (new_bs ? new_bs->quiesce_counter :0 ); - drain_saldo = new_bs_quiesce_counter - child->parent_quiesce_counter; -/* * If the new child node is drained but the old one was not, flush * all outstanding requests to the old child node. */ -while (drain_saldo >0 && child->klass->drained_begin) { + new_bs_quiesce_counter = (new_bs ? new_bs->quiesce_counter :0 ); +if (new_bs_quiesce_counter && !child->quiesced_parent) {bdrv_parent_drained_begin_single (child, true); - drain_saldo--; }if (old_bs) {
@@ -2861,16 +2857,6 @@ static void bdrv_replace_child_noperm(BdrvChild *child,if (new_bs) {assert_bdrv_graph_writable (new_bs);QLIST_INSERT_HEAD (&new_bs->parents, child, next_parent); - -/* -* Polling in bdrv_parent_drained_begin_single() may have led to the new -* node's quiesce_counter having been decreased. Not a problem, we just -* need to recognize this here and then invoke drained_end appropriately -* more often. -*/ -assert (new_bs->quiesce_counter <= new_bs_quiesce_counter); - drain_saldo += new_bs->quiesce_counter - new_bs_quiesce_counter; -if (child->klass->attach) { child->klass->attach (child); }
@@ -2879,10 +2865,13 @@ static void bdrv_replace_child_noperm(BdrvChild *child,/* * If the old child node was drained but the new one is not, allow * requests to come in only after the new node has been attached. +* +* Update new_bs_quiesce_counter because bdrv_parent_drained_begin_single() +* polls, which could have changed the value. */ -while (drain_saldo <0 && child->klass->drained_end) { + new_bs_quiesce_counter = (new_bs ? new_bs->quiesce_counter :0 ); +if (!new_bs_quiesce_counter && child->quiesced_parent) {bdrv_parent_drained_end_single (child); - drain_saldo++; } }
block/io.c +18 -12
@@ -62,8 +62,9 @@ void bdrv_parent_drained_end_single(BdrvChild *c) {IO_OR_GS_CODE (); -assert (c->parent_quiesce_counter >0 ); - c->parent_quiesce_counter--; +assert (c->quiesced_parent); + c->quiesced_parent = false; +if (c->klass->drained_end) { c->klass->drained_end (c); }
@@ -110,7 +111,10 @@ 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++; + +assert (!c->quiesced_parent); + c->quiesced_parent = true; +if (c->klass->drained_begin) { c->klass->drained_begin (c); }
@@ -358,11 +362,12 @@ void bdrv_do_drained_begin_quiesce(BlockDriverState *bs,/* Stop things in parent-to-child order */ if (qatomic_fetch_inc (&bs->quiesce_counter) ==0 ) {aio_disable_external (bdrv_get_aio_context (bs)); - } -bdrv_parent_drained_begin (bs, parent, ignore_bds_parents); -if (bs->drv && bs->drv->bdrv_drain_begin) { - bs->drv->bdrv_drain_begin (bs); +/* TODO Remove ignore_bds_parents, we don't consider it any more */ +bdrv_parent_drained_begin (bs, parent, false); +if (bs->drv && bs->drv->bdrv_drain_begin) { + bs->drv->bdrv_drain_begin (bs); + } } }
@@ -413,13 +418,14 @@ static void bdrv_do_drained_end(BlockDriverState *bs, BdrvChild *parent,assert (bs->quiesce_counter >0 );/* Re-enable things in child-to-parent order */ -if (bs->drv && bs->drv->bdrv_drain_end) { - bs->drv->bdrv_drain_end (bs); - } -bdrv_parent_drained_end (bs, parent, ignore_bds_parents); - old_quiesce_counter =qatomic_fetch_dec (&bs->quiesce_counter);if (old_quiesce_counter ==1 ) { +if (bs->drv && bs->drv->bdrv_drain_end) { + bs->drv->bdrv_drain_end (bs); + } +/* TODO Remove ignore_bds_parents, we don't consider it any more */ +bdrv_parent_drained_end (bs, parent, false); +aio_enable_external (bdrv_get_aio_context (bs)); } }
tests/unit/test-bdrv-drain.c +10 -6
@@ -296,7 +296,11 @@ static void test_quiesce_common(enum drain_type drain_type, bool recursive)do_drain_begin (drain_type, bs); - g_assert_cmpint(bs->quiesce_counter, ==, 1); +if (drain_type ==BDRV_DRAIN_ALL ) { + g_assert_cmpint(bs->quiesce_counter, ==, 2); + }else { + g_assert_cmpint(bs->quiesce_counter, ==, 1); + } g_assert_cmpint(backing->quiesce_counter, ==, !!recursive);do_drain_end (drain_type, bs);
@@ -348,8 +352,8 @@ static void test_nested(void)for (outer =0 ; outer <DRAIN_TYPE_MAX ; outer++) {for (inner =0 ; inner <DRAIN_TYPE_MAX ; inner++) { -int backing_quiesce = (outer !=BDRV_DRAIN ) + - (inner !=BDRV_DRAIN ); +int backing_quiesce = (outer ==BDRV_DRAIN_ALL ) + + (inner ==BDRV_DRAIN_ALL ); g_assert_cmpint(bs->quiesce_counter, ==, 0); g_assert_cmpint(backing->quiesce_counter, ==, 0);
@@ -359,10 +363,10 @@ static void test_nested(void)do_drain_begin (outer, bs);do_drain_begin (inner, bs); - g_assert_cmpint(bs->quiesce_counter, ==, 2); + g_assert_cmpint(bs->quiesce_counter, ==, 2 + !!backing_quiesce); g_assert_cmpint(backing->quiesce_counter, ==, backing_quiesce); - g_assert_cmpint(s->drain_count, ==, 2); - g_assert_cmpint(backing_s->drain_count, ==, backing_quiesce); + g_assert_cmpint(s->drain_count, ==, 1); + g_assert_cmpint(backing_s->drain_count, ==, !!backing_quiesce);do_drain_end (inner, bs);do_drain_end (outer, bs);
Signature · 1 line
2.38.1
Re: [PATCH v2 11/15] block: Call drain callbacks only once
Quoted 6 lines · Kevin Wolf
We only need to call both the BlockDriver's callback and the parent callbacks when going from undrained to drained or vice versa. A second drain section doesn't make a difference for the driver or the parent, they weren't supposed to send new requests before and after the second drain.
Quoted 8 lines
One thing that gets in the way is the '
ignore_bds_parents' parameter inbdrv_do_drained_begin_quiesce()andbdrv_do_drained_end(): It means thatbdrv_drain_all_begin()increasesbs->quiesce_counter, but does not quiesce the parent through BdrvChildClass callbacks. If an additional drain section is started now,bs->quiesce_counterwill be non-zero, but we would still need to quiesce the parent through BdrvChildClass in order to keep things consistent (and unquiesce it on the matchingbdrv_drained_end(), even though the counter would reach 0 yet as long as
would reach -> would NOT reach
if I understand correctly)
Quoted 10 lines
the
bdrv_drain_all()section is still active).Instead of keeping track of this, let's just get rid of the parameter. It was introduced in commit
6cd5c9d7b2das an optimisation so that duringbdrv_drain_all(), we wouldn't recursively drain all parents up to the root for each node, resulting in quadratic complexity. As it happens, calling the callbacks only once solves the same problem, so as of this patch, we'll still have O(n) complexity andignore_bds_parentsis not needed any more.
Quoted 5 lines
This patch only ignores the '
ignore_bds_parents' parameter. It will be removed in a separate patch.Signed-off-by Kevin Wolf <[email protected]>Reviewed-by Hanna Reitz <[email protected]>
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH v2 11/15] block: Call drain callbacks only once
Am 25.11.2022 um 15:59 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 16 lines · Kevin Wolf
Quoted 6 lines
We only need to call both the BlockDriver's callback and the parent callbacks when going from undrained to drained or vice versa. A second drain section doesn't make a difference for the driver or the parent, they weren't supposed to send new requests before and after the second drain.
Quoted 8 lines
One thing that gets in the way is the '
ignore_bds_parents' parameter inbdrv_do_drained_begin_quiesce()andbdrv_do_drained_end(): It means thatbdrv_drain_all_begin()increasesbs->quiesce_counter, but does not quiesce the parent through BdrvChildClass callbacks. If an additional drain section is started now,bs->quiesce_counterwill be non-zero, but we would still need to quiesce the parent through BdrvChildClass in order to keep things consistent (and unquiesce it on the matchingbdrv_drained_end(), even though the counter would reach 0 yet as long as
Quoted 3 lines
would reach -> would NOT reach
if I understand correctly)
Yes, you're right. Thanks for catching it. Fixing this while applying the series.
Kevin
Quoted 25 lines
Quoted 10 lines
the
bdrv_drain_all()section is still active).Instead of keeping track of this, let's just get rid of the parameter. It was introduced in commit
6cd5c9d7b2das an optimisation so that duringbdrv_drain_all(), we wouldn't recursively drain all parents up to the root for each node, resulting in quadratic complexity. As it happens, calling the callbacks only once solves the same problem, so as of this patch, we'll still have O(n) complexity andignore_bds_parentsis not needed any more.Quoted 5 lines
This patch only ignores the '
ignore_bds_parents' parameter. It will be removed in a separate patch.Signed-off-by Kevin Wolf <[email protected]>Reviewed-by Hanna Reitz <[email protected]>Reviewed-by Vladimir Sementsov-Ogievskiy <[email protected]>Signature · 2 lines
Best regards, Vladimir
PATCH v2 12/15 block: Remove ignore_bds_parents parameter from drain_begin/end.
In reply to Kevin Wolfignore_bds_parents is now ignored during drain_begin and drain_end, so we can just remove it there. It is still a valid optimisation for drain_all in bdrv_drained_poll(), so leave it around there.
--- include/block/block-io.h | 3 +-- block.c | 2 +- block/io.c | 58 +++++++++++++++------------------------- 3 files changed, 24 insertions(+), 39 deletions(-)
include/block/block-io.h +1 -2
@@ -329,8 +329,7 @@ void bdrv_drained_begin(BlockDriverState *bs);* Quiesces a BDS like bdrv_drained_begin(), but does not wait for already * running requests to complete. */ -void bdrv_do_drained_begin_quiesce (BlockDriverState *bs, -BdrvChild *parent,bool ignore_bds_parents); +void bdrv_do_drained_begin_quiesce (BlockDriverState *bs,BdrvChild *parent);/** * bdrv_drained_end:
block.c +1 -1
@@ -1228,7 +1228,7 @@ static char *bdrv_child_get_parent_desc(BdrvChild *c)static void bdrv_child_cb_drained_begin (BdrvChild *child) {BlockDriverState *bs = child->opaque; -bdrv_do_drained_begin_quiesce (bs,NULL , false); +bdrv_do_drained_begin_quiesce (bs,NULL ); }static bool bdrv_child_cb_drained_poll (BdrvChild *child)
block/io.c +22 -36
191 lines not on this page · raw message
Signature · 1 line
2.38.1
Re: [PATCH v2 12/15] block: Remove ignore_bds_parents parameter from drain_begin/end.
Quoted 5 lines · Kevin Wolf
ignore_bds_parentsis now ignored duringdrain_beginanddrain_end, so we can just remove it there. It is still a valid optimisation fordrain_allinbdrv_drained_poll(), so leave it around there.Signed-off-by Kevin Wolf<[email protected]>
Signature · 2 lines
Best regards, Vladimir
PATCH v2 13/15 block: Drop out of coroutine in bdrv_do_drained_begin_quiesce()
In reply to Kevin WolfThe next patch adds a parent drain to bdrv_attach_child_common(), which shouldn't be, but is currently called from coroutines in some cases (e.g. .bdrv_co_create implementations generally open new nodes). Therefore, the assertion that we're not in a coroutine doesn't hold true any more.
We could just remove the assertion because there is nothing in the function that should be in conflict with running in a coroutine, but just to be on the safe side, we can reverse the caller relationship between bdrv_do_drained_begin() and bdrv_do_drained_begin_quiesce() so that the latter also just drops out of coroutine context and we can still be certain in the future that any drain code doesn't run in coroutines.
As a nice side effect, the structure of bdrv_do_drained_begin() is now symmetrical with bdrv_do_drained_end().
--- block/io.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-)
block/io.c +12 -13
@@ -346,10 +346,15 @@ static void coroutine_fn bdrv_co_yield_to_drain(BlockDriverState *bs, } } -void bdrv_do_drained_begin_quiesce (BlockDriverState *bs,BdrvChild *parent) +static void bdrv_do_drained_begin (BlockDriverState *bs,BdrvChild *parent, +bool poll) {IO_OR_GS_CODE (); -assert (!qemu_in_coroutine ()); + +if (qemu_in_coroutine ()) { +bdrv_co_yield_to_drain (bs, true, parent, poll); +return ; + }/* Stop things in parent-to-child order */ if (qatomic_fetch_inc (&bs->quiesce_counter) ==0 ) {
@@ -359,17 +364,6 @@ void bdrv_do_drained_begin_quiesce(BlockDriverState *bs, BdrvChild *parent) bs->drv->bdrv_drain_begin (bs); } } -} - -static void bdrv_do_drained_begin (BlockDriverState *bs,BdrvChild *parent, -bool poll) -{ -if (qemu_in_coroutine ()) { -bdrv_co_yield_to_drain (bs, true, parent, poll); -return ; - } - -bdrv_do_drained_begin_quiesce (bs, parent);/* * Wait for drained requests to finish.
@@ -385,6 +379,11 @@ static void bdrv_do_drained_begin(BlockDriverState *bs, BdrvChild *parent, } } +void bdrv_do_drained_begin_quiesce (BlockDriverState *bs,BdrvChild *parent) +{ +bdrv_do_drained_begin (bs, parent, false); +} +void bdrv_drained_begin (BlockDriverState *bs) {IO_OR_GS_CODE ();
Signature · 1 line
2.38.1
Re: [PATCH v2 13/15] block: Drop out of coroutine in bdrv_do_drained_begin_quiesce()
Quoted 13 lines · Kevin Wolf
The next patch adds a parent drain to
bdrv_attach_child_common(), which shouldn't be, but is currently called from coroutines in some cases (e.g. .bdrv_co_create implementations generally open new nodes). Therefore, the assertion that we're not in a coroutine doesn't hold true any more.We could just remove the assertion because there is nothing in the function that should be in conflict with running in a coroutine, but just to be on the safe side, we can reverse the caller relationship between
bdrv_do_drained_begin()andbdrv_do_drained_begin_quiesce()so that the latter also just drops out of coroutine context and we can still be certain in the future that any drain code doesn't run in coroutines.
Quoted 4 lines
As a nice side effect, the structure of
bdrv_do_drained_begin()is now symmetrical withbdrv_do_drained_end().Signed-off-by Kevin Wolf<[email protected]>
Signature · 2 lines
Best regards, Vladimir
PATCH v2 14/15 block: Don't poll in bdrv_replace_child_noperm()
In reply to Kevin WolfIn order to make sure that bdrv_replace_child_noperm() doesn't have to poll any more, get rid of the bdrv_parent_drained_begin_single() call.
This is possible now because we can require that the parent is already drained through the child in question when the function is called and we don't call the parent drain callbacks more than once.
The additional drain calls needed in callers cause the test case to run its code in the drain handler too early (bdrv_attach_child() drains now), so modify it to only enable the code after the test setup has completed.
--- include/block/block-io.h | 8 +++ block.c | 103 ++++++++++++++++++++++++++++++----- block/io.c | 2 +- tests/unit/test-bdrv-drain.c | 10 ++++ 4 files changed, 108 insertions(+), 15 deletions(-)
include/block/block-io.h +8 -0
@@ -292,6 +292,14 @@ bdrv_writev_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos);*/ void bdrv_parent_drained_begin_single (BdrvChild *c,bool poll); +/** +* bdrv_parent_drained_poll_single: +* +* Returns true if there is any pending activity to cease before @c can be +* called quiesced, false otherwise. +*/ +bool bdrv_parent_drained_poll_single (BdrvChild *c); +/** * bdrv_parent_drained_end_single: *
block.c +89 -14
170 lines not on this page · raw message
block/io.c +1 -1
@@ -81,7 +81,7 @@ static void bdrv_parent_drained_end(BlockDriverState *bs, BdrvChild *ignore) } } -static bool bdrv_parent_drained_poll_single (BdrvChild *c) +bool bdrv_parent_drained_poll_single (BdrvChild *c) {if (c->klass->drained_poll) {return c->klass->drained_poll (c);
tests/unit/test-bdrv-drain.c +10 -0
@@ -1654,6 +1654,7 @@ static void test_drop_intermediate_poll(void) typedef struct BDRVReplaceTestState { + bool setup_completed; bool was_drained; bool was_undrained; bool has_read;
@@ -1738,6 +1739,10 @@ static void bdrv_replace_test_drain_begin(BlockDriverState *bs) {BDRVReplaceTestState *s = bs->opaque; +if (!s->setup_completed) { +return ; + } +if (!s->drain_count) { s->drain_co =qemu_coroutine_create (bdrv_replace_test_drain_co, bs);bdrv_inc_in_flight (bs);
@@ -1769,6 +1774,10 @@ static void bdrv_replace_test_drain_end(BlockDriverState *bs) {BDRVReplaceTestState *s = bs->opaque; +if (!s->setup_completed) { +return ; + } +g_assert (s->drain_count >0 );if (!--s->drain_count) { s->was_undrained = true;
@@ -1867,6 +1876,7 @@ static void do_test_replace_child_mid_drain(int old_drain_count,bdrv_ref (old_child_bs);bdrv_attach_child (parent_bs, old_child_bs,"child" , &child_of_bds,BDRV_CHILD_COW , &error_abort); + parent_s->setup_completed = true;for (i =0 ; i < old_drain_count; i++) {bdrv_drained_begin (old_child_bs);
Signature · 1 line
2.38.1
Re: [PATCH v2 14/15] block: Don't poll in bdrv_replace_child_noperm()
Quoted 7 lines · Kevin Wolf
In order to make sure that
bdrv_replace_child_noperm()doesn't have to poll any more, get rid of thebdrv_parent_drained_begin_single()call.This is possible now because we can require that the parent is already drained through the child in question when the function is called and we don't call the parent drain callbacks more than once.
Quoted 6 lines
The additional drain calls needed in callers cause the test case to run its code in the drain handler too early (
bdrv_attach_child()drains now), so modify it to only enable the code after the test setup has completed.Signed-off-by Kevin Wolf <[email protected]>
It's still hard to keep this all in mind, so weak:
Quoted 30 lines
--- include/block/block-io.h | 8 +++ block.c | 103 ++++++++++++++++++++++++++++++----- block/io.c | 2 +- tests/unit/test-bdrv-drain.c | 10 ++++ 4 files changed, 108 insertions(+), 15 deletions(-)include/block/block-io.h +8
-0@@ -292,6 +292,14 @@ bdrv_writev_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos);*/ void bdrv_parent_drained_begin_single (BdrvChild *c,bool poll); +/** +* bdrv_parent_drained_poll_single: +* +* Returns true if there is any pending activity to cease before @c can be +* called quiesced, false otherwise. +*/ +bool bdrv_parent_drained_poll_single (BdrvChild *c); +/** * bdrv_parent_drained_end_single: * block.c +0
-0
[..]
block.c static void bdrv_replace_child_noperm(BdrvChild *child, · line 2910
Quoted 12 lines
block.c +3
-5@@ -2863,11 +2905,9 @@ static void bdrv_replace_child_noperm(BdrvChild *child, }/* -* If the old child node was drained but the new one is not, allow -* requests to come in only after the new node has been attached. -* -* Update new_bs_quiesce_counter because bdrv_parent_drained_begin_single() -* polls, which could have changed the value. +* If the parent was drained through this BdrvChild previously, but new_bs +* is not drained, allow requests to come in only after the new node has +* been attached.
As I understand,the main reason why we MUST do this automatic undrain, is the contract with the user:
User things that:
1. It's enough to drain node X to drain all its parents (thanks to recursion)
2. User should undrain exactly same nodes that was drained by hand, everything that was drained automatically would be automatically undrained.
So here we break the connection between X and its parent, therefore recursion will not help on final undrain. So, we should do the automation here.
I have an idea how to (probably) make things even more simple.
1. drop .quiesced_parent
2. consider the Full graph, including non-bds parents, and support having .quiesce_counter for non-bds parents. (probably need some structure to unify bds and non-bds nodes of the Full graph - Generic ndoe, that's not the first time we are saying about that)
3. drop any recursion and automatic drain/undrain
4. user is responsible to drain all the nodes and their parents as needed to proceed with some block graph modification
5. user keeps the list of all nodes that was drained to undrain them in the end
6. node may be drained only when all its parents are already drained (add an assertion)
And of course we need helpers for the user to do 4-6. That should work similar to permissions update. Add a function to produce a topologically sorted list of Generic nodes (starting from some node and include all its parents and their parents and so on), and simple functions that to drain/undrain of such list in a loop.
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH v2 14/15] block: Don't poll in bdrv_replace_child_noperm()
Am 25.11.2022 um 17:07 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 64 lines · Kevin Wolf
Quoted 7 lines
In order to make sure that
bdrv_replace_child_noperm()doesn't have to poll any more, get rid of thebdrv_parent_drained_begin_single()call.This is possible now because we can require that the parent is already drained through the child in question when the function is called and we don't call the parent drain callbacks more than once.
Quoted 6 lines
The additional drain calls needed in callers cause the test case to run its code in the drain handler too early (
bdrv_attach_child()drains now), so modify it to only enable the code after the test setup has completed.Signed-off-by Kevin Wolf <[email protected]>It's still hard to keep this all in mind, so weak:
Reviewed-by Vladimir Sementsov-Ogievskiy <[email protected]>Quoted 29 lines
--- include/block/block-io.h | 8 +++ block.c | 103 ++++++++++++++++++++++++++++++----- block/io.c | 2 +- tests/unit/test-bdrv-drain.c | 10 ++++ 4 files changed, 108 insertions(+), 15 deletions(-)include/block/block-io.h +8
-0@@ -292,6 +292,14 @@ bdrv_writev_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos);*/ void bdrv_parent_drained_begin_single (BdrvChild *c,bool poll); +/** +* bdrv_parent_drained_poll_single: +* +* Returns true if there is any pending activity to cease before @c can be +* called quiesced, false otherwise. +*/ +bool bdrv_parent_drained_poll_single (BdrvChild *c); +/** * bdrv_parent_drained_end_single: * block.c +0
-0[..]
block.c static void bdrv_replace_child_noperm(BdrvChild *child, · line 2909
Quoted 11 lines
block.c +3
-5@@ -2863,11 +2905,9 @@ static void bdrv_replace_child_noperm(BdrvChild *child, }/* -* If the old child node was drained but the new one is not, allow -* requests to come in only after the new node has been attached. -* -* Update new_bs_quiesce_counter because bdrv_parent_drained_begin_single() -* polls, which could have changed the value. +* If the parent was drained through this BdrvChild previously, but new_bs +* is not drained, allow requests to come in only after the new node has +* been attached. As I understand,the main reason why we MUST do this automatic undrain, is the contract with the user:
Quoted 6 lines
User things that:
1. It's enough to drain node X to drain all its parents (thanks to recursion)
2. User should undrain exactly same nodes that was drained by hand, everything that was drained automatically would be automatically undrained.So here we break the connection between X and its parent, therefore recursion will not help on final undrain. So, we should do the automation here.
Yes, I think that's the idea behind our interface.
Quoted 8 lines
I have an idea how to (probably) make things even more simple.
1. drop .quiesced_parent
2. consider the Full graph, including non-bds parents, and support having .quiesce_counter for non-bds parents. (probably need some structure to unify bds and non-bds nodes of the Full graph - Generic ndoe, that's not the first time we are saying about that)
3. drop any recursion and automatic drain/undrain
Quoted 7 lines
4. user is responsible to drain all the nodes and their parents as needed to proceed with some block graph modification
5. user keeps the list of all nodes that was drained to undrain them in the end
6. node may be drained only when all its parents are already drained (add an assertion)
And of course we need helpers for the user to do 4-6. That should work similar to permissions update. Add a function to produce a topologically sorted list of Generic nodes (starting from some node and include all its parents and their parents and so on), and simple functions that to drain/undrain of such list in a loop.
I understand your idea and it looks nice at the first sight.
However, on second thought, I'm not sure how easy and nice this would actually turn out: You will lose the invariant that if a node is drained, its parent will always be drained, too - it depends on the caller now. You also can't delete a node that is still drained, you need to keep it around for undraining. Same thing with graph modifications in a drained section: You won't automatically get a consistent state regarding drain in the new graph layout, so you would have to manually make sure that both old and new children are drained.
It feels like this will lead to new complications that might not be any easier to manage than the old ones.
Kevin
Re: [PATCH v2 14/15] block: Don't poll in bdrv_replace_child_noperm()
In reply to Kevin WolfQuoted 7 lines · Kevin Wolf
In order to make sure that
bdrv_replace_child_noperm()doesn't have to poll any more, get rid of thebdrv_parent_drained_begin_single()call.This is possible now because we can require that the parent is already drained through the child in question when the function is called and we don't call the parent drain callbacks more than once.
Quoted 6 lines
The additional drain calls needed in callers cause the test case to run its code in the drain handler too early (
bdrv_attach_child()drains now), so modify it to only enable the code after the test setup has completed.Signed-off-by Kevin Wolf<[email protected]>
I hate to bear bad news, but this breaks the Windows builds on github (msys-32bit, msys-64bit) with an obscure but 100% reproducible
51/88 qemu:unit / test-bdrv-drain ERROR 1.30s (exit status 3221225477 or signal 3221225349 SIGinvalid)
The exit status is 0xC0000005 aka a Windows SIGSEGV. With some luck it could be reproducible with Wine (but no gdb).
Paolo
Re: [PATCH v2 14/15] block: Don't poll in bdrv_replace_child_noperm()
Quoted 13 lines · Paolo Bonzini
Quoted 12 lines · Kevin Wolf
In order to make sure that
bdrv_replace_child_noperm()doesn't have to poll any more, get rid of thebdrv_parent_drained_begin_single()call.This is possible now because we can require that the parent is already drained through the child in question when the function is called and we don't call the parent drain callbacks more than once.
The additional drain calls needed in callers cause the test case to run its code in the drain handler too early (
bdrv_attach_child()drains now), so modify it to only enable the code after the test setup has completed.
Quoted 7 lines
Quoted 1 line
Signed-off-by Kevin Wolf<[email protected]>I hate to bear bad news, but this breaks the Windows builds on github (msys-32bit, msys-64bit) with an obscure but 100% reproducible
51/88 qemu:unit / test-bdrv-drain ERROR 1.30s (exit status 3221225477 or signal 3221225349 SIGinvalid)
The exit status is
0xC0000005aka a Windows SIGSEGV. With some luck it could be reproducible with Wine (but no gdb).
I am testing dropping this patch and squashing
block.c +3 -1
@@ -2870,7 +2870,9 @@ static void bdrv_replace_child_noperm(BdrvChild *child,*/ new_bs_quiesce_counter = (new_bs ? new_bs->quiesce_counter :0 );if (new_bs_quiesce_counter && !child->quiesced_parent) { -bdrv_parent_drained_begin_single (child, true); +bdrv_parent_drained_begin_single (child); +AIO_WAIT_WHILE (bdrv_child_get_parent_aio_context (child), +bdrv_parent_drained_poll_single (c)); }if (old_bs) {
in patch 15/15, which should at least allow me to keep a lot of the cleanups I had on top of this series.
Paolo
Re: [PATCH v2 14/15] block: Don't poll in bdrv_replace_child_noperm()
In reply to Paolo BonziniAm 09.12.2022 um 17:53 hat Paolo Bonzini geschrieben:
Quoted 15 lines · Kevin Wolf
Quoted 7 lines
In order to make sure that
bdrv_replace_child_noperm()doesn't have to poll any more, get rid of thebdrv_parent_drained_begin_single()call.This is possible now because we can require that the parent is already drained through the child in question when the function is called and we don't call the parent drain callbacks more than once.
Quoted 6 lines
The additional drain calls needed in callers cause the test case to run its code in the drain handler too early (
bdrv_attach_child()drains now), so modify it to only enable the code after the test setup has completed.Signed-off-by Kevin Wolf<[email protected]>
Quoted 8 lines
I hate to bear bad news, but this breaks the Windows builds on github (msys-32bit, msys-64bit) with an obscure but 100% reproducible
51/88 qemu:unit / test-bdrv-drain ERROR 1.30s (exit status
3221225477 or signal 3221225349 SIGinvalid)The exit status is
0xC0000005aka a Windows SIGSEGV. With some luck it could be reproducible with Wine (but no gdb).
I can reproduce it with mingw+wine (and actually gdb! Still a somewhat limited debugging environment, but not as bad as I imagined.)
I looks to me like this is a problem with the test case rather than the change per se. It seems to be fixed with this patch that is already posted as part of the next series:
[PATCH 09/18] test-bdrv-drain: Fix incorrrect drain assumptions
https://lists.gnu.org/archive/html/qemu-block/2022-12/msg00165.html
Kevin
Re: [PATCH v2 14/15] block: Don't poll in bdrv_replace_child_noperm()
Quoted 6 lines · Kevin Wolf
I looks to me like this is a problem with the test case rather than the change per se. It seems to be fixed with this patch that is already posted as part of the next series:
[PATCH 09/18] test-bdrv-drain: Fix incorrrect drain assumptions
https://lists.gnu.org/archive/html/qemu-block/2022-12/msg00165.html
Cool, thanks. I'll retest once the series hits block-next. In the meanwhile you can ignore the first three patches of http://patchew.org/QEMU/[email protected].
Paolo
PATCH v2 15/15 block: Remove poll parameter from bdrv_parent_drained_begin_single()
In reply to Kevin WolfAll callers of bdrv_parent_drained_begin_single() pass poll=false now, so we don't need the parameter any more.
--- include/block/block-io.h | 5 ++--- block.c | 4 ++-- block/io.c | 8 ++------ 3 files changed, 6 insertions(+), 11 deletions(-)
include/block/block-io.h +2 -3
@@ -287,10 +287,9 @@ bdrv_writev_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos);/** * bdrv_parent_drained_begin_single: * -* Begin a quiesced section for the parent of @c. If @poll is true, wait for -* any pending activity to cease. +* Begin a quiesced section for the parent of @c. */ -void bdrv_parent_drained_begin_single (BdrvChild *c,bool poll); +void bdrv_parent_drained_begin_single (BdrvChild *c);/** * bdrv_parent_drained_poll_single:
block.c +2 -2
@@ -2419,7 +2419,7 @@ static void bdrv_replace_child_abort(void *opaque)* new_bs drained when calling bdrv_replace_child_tran() is not a * requirement any more. */ -bdrv_parent_drained_begin_single (s->child, false); +bdrv_parent_drained_begin_single (s->child);assert (!bdrv_parent_drained_poll_single (s->child)); }assert (s->child->quiesced_parent);
@@ -3061,7 +3061,7 @@ static BdrvChild *bdrv_attach_child_common(BlockDriverState *child_bs,* a problem, we already did this), but it will still poll until the parent * is fully quiesced, so it will not be negatively affected either. */ -bdrv_parent_drained_begin_single (new_child, false); +bdrv_parent_drained_begin_single (new_child);bdrv_replace_child_noperm (new_child, child_bs);BdrvAttachChildCommonState *s =g_new (BdrvAttachChildCommonState,1 );
block/io.c +2 -6
@@ -53,7 +53,7 @@ static void bdrv_parent_drained_begin(BlockDriverState *bs, BdrvChild *ignore)if (c == ignore) {continue ; } -bdrv_parent_drained_begin_single (c, false); +bdrv_parent_drained_begin_single (c); } }
@@ -105,9 +105,8 @@ static bool bdrv_parent_drained_poll(BlockDriverState *bs, BdrvChild *ignore,return busy; } -void bdrv_parent_drained_begin_single (BdrvChild *c,bool poll) +void bdrv_parent_drained_begin_single (BdrvChild *c) { -AioContext *ctx =bdrv_child_get_parent_aio_context (c);IO_OR_GS_CODE ();assert (!c->quiesced_parent);
@@ -116,9 +115,6 @@ void bdrv_parent_drained_begin_single(BdrvChild *c, bool poll)if (c->klass->drained_begin) { c->klass->drained_begin (c); } -if (poll) { -AIO_WAIT_WHILE (ctx,bdrv_parent_drained_poll_single (c)); - } }static void bdrv_merge_limits (BlockLimits *dst,const BlockLimits *src)
Signature · 1 line
2.38.1
Re: [PATCH v2 15/15] block: Remove poll parameter from bdrv_parent_drained_begin_single()
Quoted 4 lines · Kevin Wolf
All callers of
bdrv_parent_drained_begin_single()pass poll=false now, so we don't need the parameter any more.Signed-off-by Kevin Wolf<[email protected]>
Signature · 2 lines
Best regards, Vladimir
Quoted 43 lines · Kevin Wolf
I'm aware that exactly nobody has been looking forward to a series with this title, but it has to be. The way drain works means that we need to poll in
bdrv_replace_child_noperm()and that makes things rather messy with Emanuele's multiqueue work because you must not poll while you hold the graph lock.The other reason why it has to be is that drain is way too complex and there are too many different cases. Some simplification like this will hopefully make it considerably more maintainable. The diffstat probably tells something, too.
There are roughly speaking three parts in this series:
1. Make BlockDriver.bdrv_drained_begin/end() non-coroutine_fn again, which allows us to not poll on
bdrv_drained_end()any more.2. Remove subtree drains. They are a considerable complication in the whole drain machinery (in particular, they require polling in the BdrvChildClass.attach/detach() callbacks that are called during
bdrv_replace_child_noperm()) and none of their users actually has a good reason to use them.3. Finally get rid of polling in
bdrv_replace_child_noperm()by requiring that the child is already drained by the caller and calling callbacks only once and not again for every nested drain section.If necessary, a prefix of this series can be merged that covers only the first or the first two parts and it would still make sense.
v2:
- Rebased on master
- Patch 3: Removed left over_coparts in function names
- Patch 4: Updated function comments to reflect that we're not polling any more
- Patch 6 (new): Fix inconsistent AioContext locking for reopen code
- Patch 9 (was 8): Added comment to clarify when polling is allowed and the graph may change again
- Patch 11 (was 10):
* Reworded some comments and the commit message.
* Dropped a now unnecessary assertion that was dropped only in a later patch in v1 of the series.
* Changed 'intparent_quiesce_counter' into 'boolquiesced_parent'
- Patch 12 (was 11): Don't removeignore_bds_parentsfrom
Quoted 8 lines
bdrv_drain_poll(), it is actually still a valid optimisation there that makes polling O(n) instead of O(n²)
- Patch 13 (new): Instead of only removing assert(!qemu_in_coroutine()) like in v1 of the series, drop out of coroutine context inbdrv_do_drained_begin_quiesce()just to be sure that we'll never get coroutine surprises in drain code.
- Patch 14 (was 12): More and reworded comments to make things hopefully a bit clearer
Thanks!
Am 18.11.2022 um 18:40 hat Kevin Wolf geschrieben:
Quoted 22 lines
I'm aware that exactly nobody has been looking forward to a series with this title, but it has to be. The way drain works means that we need to poll in
bdrv_replace_child_noperm()and that makes things rather messy with Emanuele's multiqueue work because you must not poll while you hold the graph lock.The other reason why it has to be is that drain is way too complex and there are too many different cases. Some simplification like this will hopefully make it considerably more maintainable. The diffstat probably tells something, too.
There are roughly speaking three parts in this series:
1. Make BlockDriver.bdrv_drained_begin/end() non-coroutine_fn again, which allows us to not poll on
bdrv_drained_end()any more.2. Remove subtree drains. They are a considerable complication in the whole drain machinery (in particular, they require polling in the BdrvChildClass.attach/detach() callbacks that are called during
bdrv_replace_child_noperm()) and none of their users actually has a good reason to use them.
Quoted 6 lines
3. Finally get rid of polling in
bdrv_replace_child_noperm()by requiring that the child is already drained by the caller and calling callbacks only once and not again for every nested drain section.If necessary, a prefix of this series can be merged that covers only the first or the first two parts and it would still make sense.
Thanks for the review, applied to block-next.
Kevin