block: Simplify drain
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.
Kevin Wolf (13):
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: 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 functions
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 | 52 +--- include/block/block_int-common.h | 17 +- include/block/block_int-io.h | 12 - block.c | 132 ++++++----- block/block-backend.c | 4 +- block/io.c | 281 ++++------------------ block/qed.c | 24 +- block/replication.c | 6 - block/stream.c | 20 +- block/throttle.c | 6 +- blockdev.c | 13 - blockjob.c | 2 +- tests/unit/test-bdrv-drain.c | 369 +++++++---------------------- 14 files changed, 270 insertions(+), 671 deletions(-)
Signature · 1 line
2.38.1
PATCH 1/13 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 2/13 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 3/13 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 | 4 +-- block/throttle.c | 6 ++-- tests/unit/test-bdrv-drain.c | 18 ++++++------ 6 files changed, 30 insertions(+), 61 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
@@ -1705,8 +1705,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 +2 -2
@@ -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_co_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_co_drain_begin, };static void bdrv_qed_init (void )
block/throttle.c +3 -3
@@ -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_co_drain_begin (BlockDriverState *bs) {ThrottleGroupMember *tgm = bs->opaque;if (qatomic_fetch_inc (&tgm->io_limits_disabled) ==0 ) {
@@ -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_co_drain_begin, + .bdrv_drain_end = throttle_co_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 7/13 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
@@ -5581,7 +5581,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;
@@ -5654,7 +5654,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 12/13 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 child is already drained when the function is called (it better be, having in-flight requests while modifying the graph isn't going to end well!) 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 | 72 +++++++++++++++++++++++++----------- block/io.c | 2 +- tests/unit/test-bdrv-drain.c | 10 +++++ 4 files changed, 70 insertions(+), 22 deletions(-)
include/block/block-io.h +8 -0
@@ -290,6 +290,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 +51 -21
139 lines not on this page · raw message
block/io.c +1 -1
@@ -80,7 +80,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
PATCH 11/13 block: Remove ignore_bds_parents parameter from drain functions
In reply to Kevin Wolfignore_bds_parents is now ignored, so we can just remove it.
--- include/block/block-io.h | 10 ++---- block.c | 4 +-- block/io.c | 78 +++++++++++++++------------------------- 3 files changed, 32 insertions(+), 60 deletions(-)
include/block/block-io.h +2 -8
@@ -305,14 +305,9 @@ void bdrv_parent_drained_end_single(BdrvChild *c);* * 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 -* drain_all). -* * This is part of bdrv_drained_begin. */ -bool bdrv_drain_poll (BlockDriverState *bs,BdrvChild *ignore_parent, -bool ignore_bds_parents); +bool bdrv_drain_poll (BlockDriverState *bs,BdrvChild *ignore_parent);/** * bdrv_drained_begin:
@@ -330,8 +325,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 +2 -2
@@ -1218,13 +1218,13 @@ 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) {BlockDriverState *bs = child->opaque; -return bdrv_drain_poll (bs,NULL , false); +return bdrv_drain_poll (bs,NULL ); }static void bdrv_child_cb_drained_end (BdrvChild *child)
block/io.c +28 -50
235 lines not on this page · raw message
Signature · 1 line
2.38.1
PATCH 4/13 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 | 15 ----- include/block/block_int-common.h | 6 +- block.c | 5 +- block/block-backend.c | 4 +- block/io.c | 97 ++++++++------------------------ blockjob.c | 2 +- 6 files changed, 30 insertions(+), 99 deletions(-)
include/block/block-io.h +0 -15
@@ -235,21 +235,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.
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
@@ -1227,11 +1227,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);
@@ -2549,7 +2549,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 -73
249 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 10/13 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(): If it is true for the first drain, bs->quiesce_counter will be non-zero, but the parent callbacks still haven't been called, so a second drain where it is false would still have to call them.
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.
--- block.c | 13 ++++++------- block/io.c | 24 +++++++++++++----------- tests/unit/test-bdrv-drain.c | 16 ++++++++++------ 3 files changed, 29 insertions(+), 24 deletions(-)
block.c +6 -7
@@ -2816,7 +2816,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);
@@ -2827,15 +2826,13 @@ static void bdrv_replace_child_noperm(BdrvChild *child, } 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) { +if (new_bs_quiesce_counter && !child->parent_quiesce_counter) {bdrv_parent_drained_begin_single (child, true); - drain_saldo--; }if (old_bs) {
@@ -2859,7 +2856,6 @@ static void bdrv_replace_child_noperm(BdrvChild *child,* 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);
@@ -2869,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->parent_quiesce_counter) {bdrv_parent_drained_end_single (child); - drain_saldo++; } }
block/io.c +13 -11
@@ -62,7 +62,7 @@ void bdrv_parent_drained_end_single(BdrvChild *c) {IO_OR_GS_CODE (); -assert (c->parent_quiesce_counter >0 ); +assert (c->parent_quiesce_counter ==1 ); c->parent_quiesce_counter--;if (c->klass->drained_end) { c->klass->drained_end (c);
@@ -109,6 +109,7 @@ static bool bdrv_parent_drained_poll(BlockDriverState *bs, BdrvChild *ignore,void bdrv_parent_drained_begin_single (BdrvChild *c,bool poll) {IO_OR_GS_CODE (); +assert (c->parent_quiesce_counter ==0 ); c->parent_quiesce_counter++;if (c->klass->drained_begin) { c->klass->drained_begin (c);
@@ -352,16 +353,16 @@ void bdrv_do_drained_begin_quiesce(BlockDriverState *bs, BdrvChild *parent, bool ignore_bds_parents) {IO_OR_GS_CODE (); -assert (!qemu_in_coroutine ());/* 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); + } } }
@@ -412,13 +413,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
PATCH 5/13 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
@@ -241,21 +241,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)
@@ -389,7 +374,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,
@@ -460,7 +447,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 9/13 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
@@ -303,8 +303,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
@@ -312,8 +311,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:
@@ -334,12 +333,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: *
@@ -353,9 +346,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
@@ -1224,7 +1224,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)
@@ -1474,8 +1474,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)
@@ -1486,8 +1484,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) {
@@ -2843,9 +2839,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); }
@@ -2860,17 +2853,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 6/13 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 | 11 ++++------- block/replication.c | 6 ------ blockdev.c | 13 ------------- 3 files changed, 4 insertions(+), 26 deletions(-)
block.c +4 -7
@@ -4142,7 +4142,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(). */ static BlockReopenQueue *bdrv_reopen_queue_child(BlockReopenQueue *bs_queue, BlockDriverState *bs,
@@ -4162,12 +4162,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);
@@ -4317,6 +4315,7 @@ void bdrv_reopen_queue_free(BlockReopenQueue *bs_queue)if (bs_queue) {BlockReopenQueueEntry *bs_entry, *next; QTAILQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) { +bdrv_drained_end (bs_entry->state.bs);qobject_unref (bs_entry->state.explicit_options);qobject_unref (bs_entry->state.options);g_free (bs_entry);
@@ -4464,7 +4463,6 @@ int bdrv_reopen(BlockDriverState *bs, QDict *opts, bool keep_old_opts,GLOBAL_STATE_CODE (); -bdrv_subtree_drained_begin (bs);if (ctx !=qemu_get_aio_context ()) {aio_context_release (ctx); }
@@ -4475,7 +4473,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
PATCH 8/13 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 | 20 ++++++++++---------- 3 files changed, 27 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
@@ -3395,14 +3395,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 ) {
@@ -3412,7 +3413,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 +10 -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,7 @@ 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); ret =bdrv_change_backing_file (unfiltered_bs, base_id, base_fmt, false);if (local_err) {error_report_err (local_err);
@@ -92,10 +95,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 13/13 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 | 7 ++----- 3 files changed, 6 insertions(+), 10 deletions(-)
include/block/block-io.h +2 -3
@@ -285,10 +285,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
@@ -2409,7 +2409,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->parent_quiesce_counter);
@@ -3016,7 +3016,7 @@ static BdrvChild *bdrv_attach_child_common(BlockDriverState *child_bs,* bdrv_replace_child_noperm() will undrain it if the child node is not * drained. The child was only just created, so polling is not necessary. */ -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 -5
@@ -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); } }
@@ -103,7 +103,7 @@ 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) {IO_OR_GS_CODE ();assert (c->parent_quiesce_counter ==0 );
@@ -111,9 +111,6 @@ void bdrv_parent_drained_begin_single(BdrvChild *c, bool poll)if (c->klass->drained_begin) { c->klass->drained_begin (c); } -if (poll) { -BDRV_POLL_WHILE (c->bs,bdrv_parent_drained_poll_single (c)); - } }static void bdrv_merge_limits (BlockLimits *dst,const BlockLimits *src)
Signature · 1 line
2.38.1
Re: [PATCH 01/13] qed: Don't yield in bdrv_qed_co_drain_begin()
In reply to Kevin WolfQuoted 7 lines · Kevin Wolf
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_flightandbdrv_drained_begin()polls, the behaviour is unchanged.Signed-off-by Kevin Wolf<[email protected]>
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH 01/13] qed: Don't yield in bdrv_qed_co_drain_begin()
In reply to Kevin Wolfblock/qed.c static void coroutine_fn qed_need_check_timer_entry(void *opaque) · line 317
Quoted 13 lines · Kevin Wolf
int ret; trace_qed_need_check_timer_cb(s);block/qed.c +6
-0@@ -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);
hmm, one question: don't we need aio_wait_kick() call here?
Quoted 3 lines
block/qed.c +2
-0+} +static void qed_need_che
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH 02/13] test-bdrv-drain: Don't yield in .bdrv_co_drained_begin/end()
In reply to Kevin WolfQuoted 56 lines · Kevin Wolf
We 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 increasebs->in_flightand 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 onbdrv_drain_end()restarting all operations immediately before the nextaio_poll().Signed-off-by Kevin Wolf <[email protected]>--- tests/unit/test-bdrv-drain.c | 64 ++++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 18 deletions(-)tests/unit/test-bdrv-drain.c +15
-1@@ -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;
tests/unit/test-bdrv-drain.c static int coroutine_fn bdrv_replace_test_co_preadv(BlockDriverState *bs, · line 1942
Quoted 10 lines
tests/unit/test-bdrv-drain.c +10
-0+ +/* 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); +}
Same question, don't we need aio_wait_kick() after decrement in_flight.
Also, seems we have here extra waiting level: a special coroutine that waits in a loop.
Could we just do in .drain_begin:
if (s->io_co) {
bdrv_inc_in_flight(bs);
}
and in .co_preadv instead of waking s->drain_co simply
if (s->drain_count == 1) {
bdrv_dec_in_flight(bs);
aio_wait_kick();
}
or even better, do inc in_flight when io_co becomes not NULL.
Quoted 63 lines
tests/unit/test-bdrv-drain.c +21
-17+/** * 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 · 2 lines
Best regards, Vladimir
Re: [PATCH 01/13] qed: Don't yield in bdrv_qed_co_drain_begin()
In reply to Vladimir Sementsov-OgievskiyAm 09.11.2022 um 10:27 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 6 lines · Kevin Wolf
Quoted 5 lines
int ret; trace_qed_need_check_timer_cb(s);block/qed.c +0
-0@@ -310,9 +309,20 @@ static void coroutine_fn qed_need_check_timer_entry(void *opaque) (void ) ret; }
Quoted 8 lines
block/qed.c static void coroutine_fn qed_need_check_timer_entry(void *opaque) · line 317
Quoted 6 lines
block/qed.c +6
-0+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);hmm, one question: don't we need
aio_wait_kick()call here?
bdrv_dec_in_flight() already calls aio_wait_kick() internally, so any places that use it don't need a separate aio_wait_kick().
Kevin
Re: [PATCH 02/13] test-bdrv-drain: Don't yield in .bdrv_co_drained_begin/end()
In reply to Vladimir Sementsov-OgievskiyAm 09.11.2022 um 11:50 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 75 lines · Kevin Wolf
Quoted 53 lines
We 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 increasebs->in_flightand 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 onbdrv_drain_end()restarting all operations immediately before the nextaio_poll().Signed-off-by Kevin Wolf <[email protected]>--- tests/unit/test-bdrv-drain.c | 64 ++++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 18 deletions(-)tests/unit/test-bdrv-drain.c +15
-1@@ -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;tests/unit/test-bdrv-drain.c static int coroutine_fn bdrv_replace_test_co_preadv(BlockDriverState *bs, · line 1941
Quoted 10 lines
tests/unit/test-bdrv-drain.c +10
-0+ +/* 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); +}Same question, don't we need
aio_wait_kick()after decrementin_flight.Also, seems we have here extra waiting level: a special coroutine that waits in a loop.
Could we just do in .drain_begin:
if (s->io_co) { bdrv_inc_in_flight(bs); }
Quoted 8 lines
and in .co_preadv instead of waking
s->drain_cosimplyif (
s->drain_count== 1) {
bdrv_dec_in_flight(bs);
aio_wait_kick();
}or even better, do inc
in_flightwhenio_cobecomes not NULL.
I just did the minimal transformation of the existing code in the test case.
These test cases often test specific interactions between coroutines, so I could imagine that the additional yield is not just some inefficient code, but coroutines that yield multiple times could actually be the scenario that is supposed to be tested.
I didn't check it for this one, but making test cases more efficient isn't automatically a good thing if they then end up not testing certain code paths any more. So if you intend to make a change here, it would need a careful analysis of all test cases that use the driver.
Kevin
Re: [PATCH 02/13] test-bdrv-drain: Don't yield in .bdrv_co_drained_begin/end()
In reply to Kevin WolfQuoted 9 lines · Kevin Wolf
We 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.
Quoted 7 lines
The behaviour for
bdrv_drain_begin()is unchanged because we increasebs->in_flightand 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 onbdrv_drain_end()restarting all operations immediately before the nextaio_poll().Signed-off-by Kevin Wolf<[email protected]>
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH 03/13] block: Revert .bdrv_drained_begin/end to non-coroutine_fn
In reply to Kevin WolfQuoted 9 lines · Kevin Wolf
Polling during
bdrv_drained_end()can be problematic (and in the future, we may get cases forbdrv_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.
Quoted 6 lines
This means that
bdrv_drained_end()effectively doesn't poll any more, even ifAIO_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 explicitaio_poll()calls need to be added because the test case wants to verify the final state after BHs have executed.
So, drained_end_counter is always zero since this commit (and is removed in the next one).
Quoted 11 lines
Signed-off-by Kevin Wolf <[email protected]>--- include/block/block_int-common.h | 10 ++++--- block.c | 4 +-- block/io.c | 49 +++++--------------------------- block/qed.c | 4 +-- block/throttle.c | 6 ++-- tests/unit/test-bdrv-drain.c | 18 ++++++------ 6 files changed, 30 insertions(+), 61 deletions(-)
[..]
Quoted 12 lines
block/qed.c +1
-1@@ -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_co_drain_begin (BlockDriverState *bs) {BDRVQEDState *s = bs->opaque;
block/qed.c static BlockDriver bdrv_qed = { · line 1664
Quoted 5 lines
block/qed.c +1
-1@@ -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_co_drain_begin,
Rename to bdrv_qed_drain_begin without _co_, as for tests ?
block/throttle.c static void throttle_reopen_abort(BDRVReopenState *reopen_state) · line 217
Quoted 13 lines
block/qed.c +0
-0};static void bdrv_qed_init (void )block/throttle.c +1
-1@@ -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_co_drain_begin (BlockDriverState *bs)
and here.
And you didn't drop coroutine_fn for throttle_co_drain_end
with that fixed:
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH 04/13] block: Remove drained_end_counter
In reply to Kevin WolfQuoted 9 lines · Kevin Wolf
drained_end_counteris 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 localdrained_end_counterto reach 0, these become a single function.Signed-off-by Kevin Wolf <[email protected]>
[..]
Quoted 2 lines
/* Recursively call BlockDriver.bdrv_drain_begin/end callbacks */
Not about this patch, but what is recursive in bdrv_drain_invoke() ?
Quoted 6 lines
block/io.c +1
-2-static void bdrv_drain_invoke (BlockDriverState *bs,bool begin, -int *drained_end_counter) +static void bdrv_drain_invoke (BlockDriverState *bs,bool begin) {if (!bs->drv || (begin && !bs->drv->bdrv_drain_begin) || (!begin && !bs->drv->bdrv_drain_end)) {
[..]
Quoted 5 lines
/**
* This function does not poll, nor must any of its recursively called
- * functions. The*drained_end_counterpointee will be incremented
- * once
Seems that is wrong already after previous commit.. Not critical
Quoted 22 lines
for every background operation scheduled, and decremented once
- * the operation settles. Therefore, the pointer must remain valid
- * until the pointee reaches 0. That implies that whoever sets up the
- * pointee has to poll until it is 0.
- *
- * We use atomic operations to access*drained_end_counter, because
- * (1) when called frombdrv_set_aio_context_ignore(), the subgraph of
- * @bs may contain nodes in different AioContexts,
- * (2)bdrv_drain_all_end()uses the same counter for all nodes,
- * regardless of which AioContext they are in.
+ * functions.
*/
static void bdrv_do_drained_end(BlockDriverState *bs, bool recursive,block/io.c +1
-4- BdrvChild *parent, bool ignore_bds_parents, - int *drained_end_counter) + BdrvChild *parent, bool ignore_bds_parents) {BdrvChild *child;int old_quiesce_counter; -assert (drained_end_counter !=NULL ); -
[..]
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH 05/13] block: Inline bdrv_drain_invoke()
In reply to Kevin WolfQuoted 5 lines · Kevin Wolf
bdrv_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.Signed-off-by Kevin Wolf<[email protected]>
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH 06/13] block: Drain invidual nodes during reopen
In reply to Kevin WolfIn subject: individual
Quoted 15 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]>--- block.c | 11 ++++------- block/replication.c | 6 ------ blockdev.c | 13 ------------- 3 files changed, 4 insertions(+), 26 deletions(-)
[..]
blockdev.c void qmp_blockdev_reopen(BlockdevOptionsList *reopen_list, Error **errp) · line 3605
Quoted 6 lines
bdrv_reopen_queue_free(queue);blockdev.c +0
-5-for (p = drained; p; p = p->next) { -BlockDriverState *bs = p->data; -AioContext *ctx =bdrv_get_aio_context (bs); - -aio_context_acquire (ctx);
In bdrv_reopen_queue_free() we don't have this acquire()/release() pair around bdrv_drained_end(). We don't need it anymore?
Quoted 7 lines
blockdev.c +0
-4-bdrv_subtree_drained_end (bs); -aio_context_release (ctx); - } -g_slist_free (drained); }void qmp_blockdev_del (const char *node_name,Error **errp)
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH 07/13] block: Don't use subtree drains in bdrv_drop_intermediate()
In reply to Kevin WolfQuoted 5 lines · Kevin Wolf
Instead 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.
Signed-off-by Kevin Wolf<[email protected]>
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH 08/13] stream: Replace subtree drain with a single node drain
In reply to Kevin WolfQuoted 23 lines · Kevin Wolf
The subtree drain was introduced in commit
b1e1af394d9as 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.
Signed-off-by Kevin Wolf <[email protected]>
[..]
Quoted 10 lines
base = bdrv_filter_or_cow_bs(s->above_base);block/stream.c +0
-4-if (base) { -bdrv_ref (base); - } - unfiltered_base =bdrv_skip_filters (base);if (bdrv_cow_child (unfiltered_bs)) {
block/stream.c static int stream_prepare(Job *job) · line 89
Quoted 6 lines
block/stream.c +1
-1@@ -82,7 +85,7 @@ 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); ret =bdrv_change_backing_file (unfiltered_bs, base_id, base_fmt, false);
If we have yield points / polls during bdrv_set_backing_hd_drained() and bdrv_change_backing_file(), it's still bad and another graph-modifying operation may interleave. But b1e1af394d9 reports only polling in bdrv_set_backing_hd(), so I think it's OK to not care about other cases.
Quoted 4 lines
block/stream.c +0
-0if (local_err) {error_report_err (local_err);@@ -92,10 +95,7 @@ static int stream_prepare(Job *job) }
block/stream.c static int stream_prepare(Job *job) · line 101
Quoted 10 lines
block/stream.c +1
-4out: -if (base) { -bdrv_unref (base); - } -bdrv_subtree_drained_end (s->above_base); +bdrv_drained_end (unfiltered_bs);return ret; }
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH 09/13] block: Remove subtree drains
In reply to Kevin WolfQuoted 5 lines · Kevin Wolf
Subtree drains are not used any more. Remove them.
After this, BdrvChildClass.attach/detach() don't poll any more.
Signed-off-by Kevin Wolf<[email protected]>
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH 10/13] block: Call drain callbacks only once
In reply to Kevin WolfQuoted 44 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.
One thing that gets in the way is the '
ignore_bds_parents' parameter inbdrv_do_drained_begin_quiesce()andbdrv_do_drained_end(): If it is true for the first drain,bs->quiesce_counterwill be non-zero, but the parent callbacks still haven't been called, so a second drain where it is false would still have to call them.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.This patch only ignores the '
ignore_bds_parents' parameter. It will be removed in a separate patch.Signed-off-by Kevin Wolf <[email protected]>--- block.c | 13 ++++++------- block/io.c | 24 +++++++++++++----------- tests/unit/test-bdrv-drain.c | 16 ++++++++++------ 3 files changed, 29 insertions(+), 24 deletions(-)block.c +0
-1@@ -2816,7 +2816,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);@@ -2827,15 +2826,13 @@ static void bdrv_replace_child_noperm(BdrvChild *child, }
block.c static void bdrv_replace_child_noperm(BdrvChild *child, · line 2834
Quoted 10 lines
block.c +1
-2new_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) { +if (new_bs_quiesce_counter && !child->parent_quiesce_counter) {
Looks like checking for child->klass->drained_begin was a wrong thing even prepatch?
Also, parent_quiesce_counter actually becomes a boolean variable.. Should we stress it by new type and name?
block.c static void bdrv_replace_child_noperm(BdrvChild *child, · line 2857
Quoted 8 lines
block.c +0
-1bdrv_parent_drained_begin_single (child, true); - drain_saldo--; }if (old_bs) {@@ -2859,7 +2856,6 @@ static void bdrv_replace_child_noperm(BdrvChild *child,* more often. */
the comment above ^^^ should be updated, we are not going to call drained_end more than once anyway
block.c static void bdrv_replace_child_noperm(BdrvChild *child, · line 2858
Quoted 1 line
block.c +0
-0assert (new_bs->quiesce_counter <= new_bs_quiesce_counter);
do we still need this assertion and the comment at all?
Quoted 42 lines
block.c +5
-3- drain_saldo += new_bs->quiesce_counter - new_bs_quiesce_counter;if (child->klass->attach) { child->klass->attach (child);@@ -2869,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->parent_quiesce_counter) {bdrv_parent_drained_end_single (child); - drain_saldo++; } }block/io.c +2
-1@@ -62,7 +62,7 @@ void bdrv_parent_drained_end_single(BdrvChild *c) {IO_OR_GS_CODE (); -assert (c->parent_quiesce_counter >0 ); +assert (c->parent_quiesce_counter ==1 ); c->parent_quiesce_counter--;if (c->klass->drained_end) { c->klass->drained_end (c);@@ -109,6 +109,7 @@ static bool bdrv_parent_drained_poll(BlockDriverState *bs, BdrvChild *ignore,void bdrv_parent_drained_begin_single (BdrvChild *c,bool poll) {IO_OR_GS_CODE (); +assert (c->parent_quiesce_counter ==0 ); c->parent_quiesce_counter++;if (c->klass->drained_begin) { c->klass->drained_begin (c);
block/io.c void bdrv_do_drained_begin_quiesce(BlockDriverState *bs, · line 355
Quoted 4 lines
block/io.c +0
-1@@ -352,16 +353,16 @@ void bdrv_do_drained_begin_quiesce(BlockDriverState *bs, BdrvChild *parent, bool ignore_bds_parents) {IO_OR_GS_CODE (); -assert (!qemu_in_coroutine ());
why that is dropped? seems unrelated to the commit
Quoted 18 lines
block/io.c +5
-4/* 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); + } } }
block/io.c static void bdrv_do_drained_end(BlockDriverState *bs, BdrvChild *parent, · line 415
Quoted 3 lines
block/io.c +0
-0@@ -412,13 +413,14 @@ static void bdrv_do_drained_end(BlockDriverState *bs, BdrvChild *parent,assert (bs->quiesce_counter >0 );/* Re-enable things in child-to-parent order */
the comment should be moved too, I think
Quoted 58 lines
block/io.c +6
-5-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 · 2 lines
Best regards, Vladimir
Re: [PATCH 10/13] block: Call drain callbacks only once
In reply to Kevin WolfQuoted 5 lines · Kevin Wolf
One thing that gets in the way is the '
ignore_bds_parents' parameter inbdrv_do_drained_begin_quiesce()andbdrv_do_drained_end(): If it is true for the first drain,bs->quiesce_counterwill be non-zero, but the parent callbacks still haven't been called, so a second drain where it is false would still have to call them.
This paragraph breaks my brain :/
Still, I understand the new concept and believe that dropping ignore_bds_parents and just call the callbacks once (and stop the recursion when we found quiesce_counter > 0) is a good way.
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH 11/13] block: Remove ignore_bds_parents parameter from drain functions
In reply to Kevin WolfQuoted 1 line · Kevin Wolf
ignore_bds_parentsis now ignored, so we can just remove it.> Signed-off-by: Kevin Wolf<[email protected]>
Not obvious to me that they are ignored, some logic is still here. Maybe it's all do nothing finally.
Still I believe that we should get rid of ignore_bds_parents anyway at this point, when we've change the recursion concept in the previous commit.
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH 01/13] qed: Don't yield in bdrv_qed_co_drain_begin()
In reply to Kevin WolfQuoted 22 lines · Kevin Wolf
block/qed.c +11
-0@@ -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); }
block/qed.c static void coroutine_fn bdrv_qed_co_drain_begin(BlockDriverState *bs) · line 380
Quoted 9 lines
block/qed.c +4
-1@@ -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);
Please include comments that indicate where inc/dec are paired. This is like pairing memory barriers where it can be very hard to know after the code has been written (and modified).
Re: [PATCH 03/13] block: Revert .bdrv_drained_begin/end to non-coroutine_fn
In reply to Kevin WolfQuoted 17 lines · Kevin Wolf
Polling during
bdrv_drained_end()can be problematic (and in the future, we may get cases forbdrv_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 ifAIO_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 explicitaio_poll()calls need to be added because the test case wants to verify the final state after BHs have executed.Signed-off-by Kevin Wolf <[email protected]>
Quoted 8 lines
--- include/block/block_int-common.h | 10 ++++--- block.c | 4 +-- block/io.c | 49 +++++--------------------------- block/qed.c | 4 +-- block/throttle.c | 6 ++-- tests/unit/test-bdrv-drain.c | 18 ++++++------ 6 files changed, 30 insertions(+), 61 deletions(-)
Wow, surprisingly little has to change to make these non-coroutine_fn.
Quoted 116 lines
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@@ -1705,8 +1705,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@@ -250,55 +250,20 @@ typedef struct {int *drained_end_counter; } BdrvCoDrainData; -static void coroutine_fn bdrv_drain_invoke_entry(void *opaque) -{ -BdrvCoDrainData *data = opaque; -BlockDriverState *bs = data->bs; - -if (data->begin) { - bs->drv->bdrv_co_drain_begin (bs); - }else { - bs->drv->bdrv_co_drain_end (bs); - } - -/* Set data->done and decrement drained_end_counter before bdrv_wakeup() */ -qatomic_mb_set (&data->done, true); -if (!data->begin) { -qatomic_dec (data->drained_end_counter); - } -bdrv_dec_in_flight (bs); - -g_free (data); -} - -/* Recursively call BlockDriver.bdrv_co_drain_begin/end callbacks */ +/* Recursively call BlockDriver.bdrv_drain_begin/end callbacks */ static void bdrv_drain_invoke (BlockDriverState *bs,bool begin,int *drained_end_counter) { -BdrvCoDrainData *data; - -if (!bs->drv || (begin && !bs->drv->bdrv_co_drain_begin) || - (!begin && !bs->drv->bdrv_co_drain_end)) { +if (!bs->drv || (begin && !bs->drv->bdrv_drain_begin) || + (!begin && !bs->drv->bdrv_drain_end)) {return ; } - data =g_new (BdrvCoDrainData,1 ); - *data = (BdrvCoDrainData ) { - .bs = bs, - .done = false, - .begin = begin, - .drained_end_counter = drained_end_counter, - }; - -if (!begin) { -qatomic_inc (drained_end_counter); +if (begin) { + bs->drv->bdrv_drain_begin (bs); + }else { + bs->drv->bdrv_drain_end (bs); } - -/* Make sure the driver callback completes during the polling phase for -* drain_begin. */ -bdrv_inc_in_flight (bs); - data->co =qemu_coroutine_create (bdrv_drain_invoke_entry, data); -aio_co_schedule (bdrv_get_aio_context (bs), data->co); }/* Returns true if BDRV_POLL_WHILE() should go into a blocking aio_poll() */ block/qed.c +0
-0
block/qed.c static void bdrv_qed_attach_aio_context(BlockDriverState *bs, · line 368
Quoted 5 lines
block/qed.c +1
-1@@ -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_co_drain_begin (BlockDriverState *bs)
This function needs to be renamed s/_co_//.
Quoted 17 lines
block/qed.c +1
-1{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_co_drain_begin, };static void bdrv_qed_init (void )block/throttle.c +0
-0
block/throttle.c static void throttle_reopen_abort(BDRVReopenState *reopen_state) · line 217
Quoted 5 lines
block/throttle.c +1
-1@@ -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_co_drain_begin (BlockDriverState *bs)
Same here.
block/throttle.c static BlockDriver bdrv_throttle = { · line 265
Quoted 9 lines
block/throttle.c +0
-2{ThrottleGroupMember *tgm = bs->opaque;if (qatomic_fetch_inc (&tgm->io_limits_disabled) ==0 ) {@@ -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,
Is throttle_co_drain_end() still marked coroutine_fn? It also need to be renamed to throttle_drain_end().
Quoted 86 lines
block/throttle.c +2
-0+ .bdrv_drain_begin = throttle_co_drain_begin, + .bdrv_drain_end = throttle_co_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
Re: [PATCH 08/13] stream: Replace subtree drain with a single node drain
In reply to Vladimir Sementsov-OgievskiyAm 09.11.2022 um 17:52 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 38 lines · Kevin Wolf
Quoted 23 lines
The subtree drain was introduced in commit
b1e1af394d9as 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.
Signed-off-by Kevin Wolf <[email protected]>[..]
Quoted 11 lines
base = bdrv_filter_or_cow_bs(s->above_base);block/stream.c +0
-5-if (base) { -bdrv_ref (base); - } - unfiltered_base =bdrv_skip_filters (base);if (bdrv_cow_child (unfiltered_bs)) {@@ -82,7 +85,7 @@ static int stream_prepare(Job *job) } } -bdrv_set_backing_hd (unfiltered_bs, base, &local_err);
Quoted 8 lines
block/stream.c static int stream_prepare(Job *job) · line 89
Quoted 2 lines
block/stream.c +1
-0+bdrv_set_backing_hd_drained (unfiltered_bs, base, &local_err); ret =bdrv_change_backing_file (unfiltered_bs, base_id, base_fmt, false);If we have yield points / polls during
bdrv_set_backing_hd_drained()andbdrv_change_backing_file(), it's still bad and another graph-modifying operation may interleave. Butb1e1af394d9reports only polling inbdrv_set_backing_hd(), so I think it's OK to not care about other cases.
At this point in the series, bdrv_replace_child_noperm() can indeed still poll. I'm not sure how bad it is, but at this point we're already reconfiguring the graph with two specific nodes and somehow this poll hasn't caused problems in the past. Anyway, at the end of the series, there isn't be any polling left in bdrv_set_backing_hd_drained(), as far as I can tell.
bdrv_change_backing_file() will certainly poll because it does I/O to the image file. However, the change to the graph is completed at that point, so I don't think it's a problem. Do you think it would be worth putting a comment before bdrv_change_backing_file() that mentions that the graph may change again from here on, but we've completed the graph change?
Quoted 6 lines
Quoted 6 lines
block/stream.c +0
-1if (local_err) {error_report_err (local_err);@@ -92,10 +95,7 @@ static int stream_prepare(Job *job) } out: -if (base) {
Quoted 8 lines
block/stream.c static int stream_prepare(Job *job) · line 100
Quoted 6 lines
block/stream.c +1
-3-bdrv_unref (base); - } -bdrv_subtree_drained_end (s->above_base); +bdrv_drained_end (unfiltered_bs);return ret; }Reviewed-by Vladimir Sementsov-Ogievskiy <[email protected]>
Thanks.
Kevin
Re: [PATCH 01/13] qed: Don't yield in bdrv_qed_co_drain_begin()
In reply to Stefan HajnocziAm 09.11.2022 um 22:49 hat Stefan Hajnoczi geschrieben:
Quoted 22 lines · Kevin Wolf
Quoted 21 lines
block/qed.c +11
-0@@ -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); }
Quoted 14 lines
block/qed.c static void coroutine_fn bdrv_qed_co_drain_begin(BlockDriverState *bs) · line 380
Quoted 10 lines
block/qed.c +4
-1@@ -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);Please include comments that indicate where inc/dec are paired. This is like pairing memory barriers where it can be very hard to know after the code has been written (and modified).
I can do this, of course, if you like to have it in qed. However, it's not something we're doing elsewhere.
bdrv_inc/dec_in_flight() are a lot simpler than barriers which synchronise two completely independently running tasks. You just need to follow the control flow from the inc() and you'll find the dec(). They are much more similar to taking and releasing a lock than to barriers.
Callbacks always make the code a little harder to read, but personally I think inc() before scheduling a new coroutine and then dec() at the end of its entry function is a very obvious pattern that exists in other places, too.
Kevin
Re: [PATCH 08/13] stream: Replace subtree drain with a single node drain
In reply to Kevin WolfQuoted 52 lines · Kevin Wolf
Am 09.11.2022 um 17:52 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 36 lines · Kevin Wolf
Quoted 23 lines
The subtree drain was introduced in commit
b1e1af394d9as 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.
Signed-off-by Kevin Wolf<[email protected]>[..]
Quoted 10 lines
base = bdrv_filter_or_cow_bs(s->above_base);block/stream.c +0
-4-if (base) { -bdrv_ref (base); - } - unfiltered_base =bdrv_skip_filters (base);if (bdrv_cow_child (unfiltered_bs)) {@@ -82,7 +85,7 @@ static int stream_prepare(Job *job) } }Quoted 8 lines
block/stream.c static int stream_prepare(Job *job) · line 89
Quoted 3 lines
block/stream.c +1
-1-bdrv_set_backing_hd (unfiltered_bs, base, &local_err); +bdrv_set_backing_hd_drained (unfiltered_bs, base, &local_err); ret =bdrv_change_backing_file (unfiltered_bs, base_id, base_fmt, false);If we have yield points / polls during
bdrv_set_backing_hd_drained()andbdrv_change_backing_file(), it's still bad and another graph-modifying operation may interleave. Butb1e1af394d9reports only polling inbdrv_set_backing_hd(), so I think it's OK to not care about other cases.At this point in the series,
bdrv_replace_child_noperm()can indeed still poll. I'm not sure how bad it is, but at this point we're already reconfiguring the graph with two specific nodes and somehow this poll hasn't caused problems in the past. Anyway, at the end of the series, there isn't be any polling left inbdrv_set_backing_hd_drained(), as far as I can tell.
Quoted 7 lines
bdrv_change_backing_file()will certainly poll because it does I/O to the image file. However, the change to the graph is completed at that point, so I don't think it's a problem. Do you think it would be worth putting a comment beforebdrv_change_backing_file()that mentions that the graph may change again from here on, but we've completed the graph change?
Comment won't hurt. I think theoretically that's possible that we
1. change the graph
2. yield in bdrv_change_backing_file
3. switch to another graph-modifying operation, change backing file and do another bdrv_change_backing_file()
4. return to bdrv_change_backing_file() of [2] and write wrong backing file to metadata
And the only solution for such things that I can imagine is a kind of global graph-modifying lock, which should be held around the whole graph modifying operation, including writing metadata. Probably, we shouldn't care until we have real bug reports of it. Actually I hope that the only user who start stream and commit jobs in parallel on same backing-chain is our iotests :)
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH 08/13] stream: Replace subtree drain with a single node drain
Am 10.11.2022 um 12:25 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 63 lines · Kevin Wolf
Quoted 52 lines
Am 09.11.2022 um 17:52 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 36 lines · Kevin Wolf
Quoted 23 lines
The subtree drain was introduced in commit
b1e1af394d9as 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.
Signed-off-by Kevin Wolf<[email protected]>[..]
Quoted 10 lines
base = bdrv_filter_or_cow_bs(s->above_base);block/stream.c +0
-4-if (base) { -bdrv_ref (base); - } - unfiltered_base =bdrv_skip_filters (base);if (bdrv_cow_child (unfiltered_bs)) {@@ -82,7 +85,7 @@ static int stream_prepare(Job *job) } }Quoted 8 lines
block/stream.c static int stream_prepare(Job *job) · line 89
Quoted 3 lines
block/stream.c +1
-1-bdrv_set_backing_hd (unfiltered_bs, base, &local_err); +bdrv_set_backing_hd_drained (unfiltered_bs, base, &local_err); ret =bdrv_change_backing_file (unfiltered_bs, base_id, base_fmt, false);If we have yield points / polls during
bdrv_set_backing_hd_drained()andbdrv_change_backing_file(), it's still bad and another graph-modifying operation may interleave. Butb1e1af394d9reports only polling inbdrv_set_backing_hd(), so I think it's OK to not care about other cases.At this point in the series,
bdrv_replace_child_noperm()can indeed still poll. I'm not sure how bad it is, but at this point we're already reconfiguring the graph with two specific nodes and somehow this poll hasn't caused problems in the past. Anyway, at the end of the series, there isn't be any polling left inbdrv_set_backing_hd_drained(), as far as I can tell.Quoted 7 lines
bdrv_change_backing_file()will certainly poll because it does I/O to the image file. However, the change to the graph is completed at that point, so I don't think it's a problem. Do you think it would be worth putting a comment beforebdrv_change_backing_file()that mentions that the graph may change again from here on, but we've completed the graph change?Comment won't hurt. I think theoretically that's possible that we
Quoted 8 lines
1. change the graph
2. yield inbdrv_change_backing_file
3. switch to another graph-modifying operation, change backing file and do anotherbdrv_change_backing_file()
4. return tobdrv_change_backing_file()of [2] and write wrong backing file to metadataAnd the only solution for such things that I can imagine is a kind of global graph-modifying lock, which should be held around the whole graph modifying operation, including writing metadata.
Actually, I don't think this is the case. The problem that you get here is just that we haven't really defined what happens when you get two concurrent .bdrv_change_backing_file requests. To solve this, you don't need to lock the whole graph, you just need to order the updates at the block driver level instead of doing them in parallel, so that we know that the last .bdrv_change_backing_file call wins. I think taking s->lock in qcow2 would already achieve this (but still lock more than is strictly necessary).
Quoted 3 lines
Probably, we shouldn't care until we have real bug reports of it. Actually I hope that the only user who start stream and commit jobs in parallel on same backing-chain is our iotests :)
Yes, it sounds very theoretical. :-)
Kevin
Re: [PATCH 05/13] block: Inline bdrv_drain_invoke()
In reply to Kevin WolfQuoted 8 lines · Kevin Wolf
bdrv_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.Signed-off-by Kevin Wolf <[email protected]>--- block/io.c | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-)
Quoted 44 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.
Kevin Wolf (13):
qed: Don't yield inbdrv_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: Removedrained_end_counter
block: Inlinebdrv_drain_invoke()
block: Drain invidual nodes during reopen
block: Don't use subtree drains inbdrv_drop_intermediate()
stream: Replace subtree drain with a single node drain
block: Remove subtree drains
block: Call drain callbacks only once
block: Removeignore_bds_parentsparameter from drain functions
block: Don't poll inbdrv_replace_child_noperm()
block: Remove poll parameter frombdrv_parent_drained_begin_single()
Quoted 15 lines
include/block/block-global-state.h | 3 + include/block/block-io.h | 52 +--- include/block/block_int-common.h | 17 +- include/block/block_int-io.h | 12 - block.c | 132 ++++++----- block/block-backend.c | 4 +- block/io.c | 281 ++++------------------ block/qed.c | 24 +- block/replication.c | 6 - block/stream.c | 20 +- block/throttle.c | 6 +- blockdev.c | 13 - blockjob.c | 2 +- tests/unit/test-bdrv-drain.c | 369 +++++++---------------------- 14 files changed, 270 insertions(+), 671 deletions(-)
I have looked through all patches but don't understand the code well enough to give an opinion or spot bugs. Removing subtree drains and aio_poll() in bdrv_replace_child_noperm() are nice.
Re: [PATCH 01/13] qed: Don't yield in bdrv_qed_co_drain_begin()
In reply to Kevin WolfAm 08/11/2022 um 13:37 schrieb Kevin Wolf:
Quoted 8 lines
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_flightandbdrv_drained_begin()polls, the behaviour is unchanged.Signed-off-by Kevin Wolf <[email protected]>
Re: [PATCH 02/13] test-bdrv-drain: Don't yield in .bdrv_co_drained_begin/end()
In reply to Kevin WolfAm 08/11/2022 um 13:37 schrieb Kevin Wolf:
Quoted 9 lines
We 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.
Quoted 8 lines
The behaviour for
bdrv_drain_begin()is unchanged because we increasebs->in_flightand 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 onbdrv_drain_end()restarting all operations immediately before the nextaio_poll().Signed-off-by Kevin Wolf <[email protected]>
Re: [PATCH 03/13] block: Revert .bdrv_drained_begin/end to non-coroutine_fn
In reply to Kevin WolfAm 08/11/2022 um 13:37 schrieb Kevin Wolf:
Quoted 9 lines
Polling during
bdrv_drained_end()can be problematic (and in the future, we may get cases forbdrv_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.
Quoted 9 lines
This means that
bdrv_drained_end()effectively doesn't poll any more, even ifAIO_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 explicitaio_poll()calls need to be added because the test case wants to verify the final state after BHs have executed.Signed-off-by Kevin Wolf <[email protected]>
Re: [PATCH 05/13] block: Inline bdrv_drain_invoke()
In reply to Kevin WolfAm 08/11/2022 um 13:37 schrieb Kevin Wolf:
Quoted 6 lines
bdrv_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.Signed-off-by Kevin Wolf <[email protected]>
Re: [PATCH 04/13] block: Remove drained_end_counter
In reply to Kevin WolfAm 08/11/2022 um 13:37 schrieb Kevin Wolf:
Quoted 10 lines
drained_end_counteris 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 localdrained_end_counterto reach 0, these become a single function.Signed-off-by Kevin Wolf <[email protected]>
Re: [PATCH 12/13] block: Don't poll in bdrv_replace_child_noperm()
In reply to Kevin WolfAm 08/11/2022 um 13:37 schrieb Kevin Wolf:
Quoted 142 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 child is already drained when the function is called (it better be, having in-flight requests while modifying the graph isn't going to end well!) 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.Signed-off-by Kevin Wolf <[email protected]>--- include/block/block-io.h | 8 ++++ block.c | 72 +++++++++++++++++++++++++----------- block/io.c | 2 +- tests/unit/test-bdrv-drain.c | 10 +++++ 4 files changed, 70 insertions(+), 22 deletions(-)include/block/block-io.h +8
-0@@ -290,6 +290,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 +28
-21@@ -2399,6 +2399,20 @@ static void bdrv_replace_child_abort(void *opaque)GLOBAL_STATE_CODE ();/* old_bs reference is transparently moved from @s to @s->child */ +if (!s->child->bs) { +/* +* The parents were undrained when removing old_bs from the child. New +* requests can't have been made, though, because the child was empty. +* +* TODO Make bdrv_replace_child_noperm() transactionable to avoid +* undraining the parent in the first place. Once this is done, having +* new_bs drained when calling bdrv_replace_child_tran() is not a +* requirement any more. +*/ +bdrv_parent_drained_begin_single (s->child, false); +assert (!bdrv_parent_drained_poll_single (s->child)); + } +assert (s->child->parent_quiesce_counter);bdrv_replace_child_noperm (s->child, s->old_bs);bdrv_unref (new_bs); }@@ -2414,12 +2428,20 @@ static TransactionActionDrv bdrv_replace_child_drv = {* * Note: real unref of old_bs is done only on commit. * +* Both child and new_bs (if non-NULL) must be drained. new_bs must be kept +* drained until the transaction is completed (this automatically implies that +* child remains drained, too). +* * The function doesn't update permissions, caller is responsible for this. */ static void bdrv_replace_child_tran (BdrvChild *child,BlockDriverState *new_bs,Transaction *tran) {BdrvReplaceChildState *s =g_new (BdrvReplaceChildState,1 ); + +assert (child->parent_quiesce_counter); +assert (!new_bs || new_bs->quiesce_counter); + *s = (BdrvReplaceChildState ) { .child = child, .old_bs = child->bs,@@ -2818,6 +2840,12 @@ static void bdrv_replace_child_noperm(BdrvChild *child,int new_bs_quiesce_counter;assert (!child->frozen); +/* +* When removing the child, it's the callers responsibility to make sure +* that no requests are in flight any more. Usually the parent is drained, +* but not through child->parent_quiesce_counter. +*/ +assert (!new_bs || child->parent_quiesce_counter);assert (old_bs != new_bs);GLOBAL_STATE_CODE ();@@ -2825,16 +2853,6 @@ 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 ); - -/* -* If the new child node is drained but the old one was not, flush -* all outstanding requests to the old child node. -*/ -if (new_bs_quiesce_counter && !child->parent_quiesce_counter) { -bdrv_parent_drained_begin_single (child, true); - } -if (old_bs) {if (child->klass->detach) { child->klass->detach (child);@@ -2849,14 +2867,6 @@ static void bdrv_replace_child_noperm(BdrvChild *child,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); -if (child->klass->attach) { child->klass->attach (child); }@@ -2865,9 +2875,6 @@ 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. */ new_bs_quiesce_counter = (new_bs ? new_bs->quiesce_counter :0 );if (!new_bs_quiesce_counter && child->parent_quiesce_counter) {
block.c static BdrvChild *bdrv_attach_child_common(BlockDriverState *child_bs, · line 3018
Quoted 8 lines
block.c +5
-0@@ -3004,6 +3011,12 @@ static BdrvChild *bdrv_attach_child_common(BlockDriverState *child_bs, }bdrv_ref (child_bs); +/* +* Let every new BdrvChild start drained, inserting it in the graph with +* bdrv_replace_child_noperm() will undrain it if the child node is not +* drained. The child was only just created, so polling is not necessary. +*/
I think there's a better way to write this, I find it complicated to read.
Also I don't really understand how you cover the case where we are replacing a child with another one (so both old and new are not-null and not newly created), and old for example could (?) have a drained counter greater than new.
Before we had all the draining saldo stuff, but now it's gone.
Thank you,
Emanuele
Quoted 99 lines
block.c +18
-0+bdrv_parent_drained_begin_single (new_child, false);bdrv_replace_child_noperm (new_child, child_bs);BdrvAttachChildCommonState *s =g_new (BdrvAttachChildCommonState,1 );@@ -5053,7 +5066,10 @@ static void bdrv_remove_child(BdrvChild *child, Transaction *tran) }if (child->bs) { +BlockDriverState *bs = child->bs; +bdrv_drained_begin (bs);bdrv_replace_child_tran (child,NULL , tran); +bdrv_drained_end (bs); }tran_add (tran, &bdrv_remove_child_drv, child);@@ -5070,6 +5086,15 @@ static void bdrv_remove_filter_or_cow_child(BlockDriverState *bs,bdrv_remove_child (bdrv_filter_or_cow_child (bs), tran); } +static void undrain_on_clean_cb (void *opaque) +{ +bdrv_drained_end (opaque); +} + +static TransactionActionDrv undrain_on_clean = { + .clean = undrain_on_clean_cb, +}; + static int bdrv_replace_node_noperm(BlockDriverState *from, BlockDriverState *to, bool auto_skip, Transaction *tran,@@ -5079,6 +5104,11 @@ static int bdrv_replace_node_noperm(BlockDriverState *from,GLOBAL_STATE_CODE (); +bdrv_drained_begin (from); +bdrv_drained_begin (to); +tran_add (tran, &undrain_on_clean, from); +tran_add (tran, &undrain_on_clean, to); + QLIST_FOREACH_SAFE(c, &from->parents, next_parent, next) {assert (c->bs == from);if (!should_update_child (c, to)) {block/io.c +1
-1@@ -80,7 +80,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);
Am 08/11/2022 um 13:37 schrieb Kevin Wolf:
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.
I added by Reviewed-by where I felt confortable with the code, the other parts I am not enough confident to review them.
But yes if this works it will be very helpful for the AioContext lock removal!
Thank you,
Emanuele
Quoted 32 lines
Kevin Wolf (13):
qed: Don't yield inbdrv_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: Removedrained_end_counter
block: Inlinebdrv_drain_invoke()
block: Drain invidual nodes during reopen
block: Don't use subtree drains inbdrv_drop_intermediate()
stream: Replace subtree drain with a single node drain
block: Remove subtree drains
block: Call drain callbacks only once
block: Removeignore_bds_parentsparameter from drain functions
block: Don't poll inbdrv_replace_child_noperm()
block: Remove poll parameter frombdrv_parent_drained_begin_single()include/block/block-global-state.h | 3 + include/block/block-io.h | 52 +--- include/block/block_int-common.h | 17 +- include/block/block_int-io.h | 12 - block.c | 132 ++++++----- block/block-backend.c | 4 +- block/io.c | 281 ++++------------------ block/qed.c | 24 +- block/replication.c | 6 - block/stream.c | 20 +- block/throttle.c | 6 +- blockdev.c | 13 - blockjob.c | 2 +- tests/unit/test-bdrv-drain.c | 369 +++++++---------------------- 14 files changed, 270 insertions(+), 671 deletions(-)
Re: [PATCH 04/13] block: Remove drained_end_counter
In reply to Vladimir Sementsov-OgievskiyAm 09.11.2022 um 15:44 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 11 lines · Kevin Wolf
Quoted 9 lines
drained_end_counteris 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 localdrained_end_counterto reach 0, these become a single function.Signed-off-by Kevin Wolf <[email protected]>
Quoted 7 lines
Reviewed-by Vladimir Sementsov-Ogievskiy <[email protected]>[..]
Quoted 1 line
/* Recursively call BlockDriver.bdrv_drain_begin/end callbacks */
Not about this patch, but what is recursive in
bdrv_drain_invoke()?
Nothing today, but it used to be the case. Looks like I forgot to remove the comment in commit 7d40d9ef five years ago.
Quoted 9 lines
Quoted 6 lines
block/io.c +1
-2-static void bdrv_drain_invoke (BlockDriverState *bs,bool begin, -int *drained_end_counter) +static void bdrv_drain_invoke (BlockDriverState *bs,bool begin) {if (!bs->drv || (begin && !bs->drv->bdrv_drain_begin) || (!begin && !bs->drv->bdrv_drain_end)) {[..]
Quoted 6 lines
Quoted 4 lines
/**
* This function does not poll, nor must any of its recursively called
- * functions. The*drained_end_counterpointee will be incremented
- * onceSeems that is wrong already after previous commit.. Not critical
I think it's technically correct: We don't schedule background operations any more, so any statement about them is true. :-)
You're right, it could be updated in the previous commit, but maybe it's easier to read the patches when you need to verify my claim only in this patch. As you said, it doesn't matter much anyway, at the end of the series it's gone.
Kevin
Quoted 28 lines
Quoted 21 lines
for every background operation scheduled, and decremented once
- * the operation settles. Therefore, the pointer must remain valid
- * until the pointee reaches 0. That implies that whoever sets up the
- * pointee has to poll until it is 0.
- *
- * We use atomic operations to access*drained_end_counter, because
- * (1) when called frombdrv_set_aio_context_ignore(), the subgraph of
- * @bs may contain nodes in different AioContexts,
- * (2)bdrv_drain_all_end()uses the same counter for all nodes,
- * regardless of which AioContext they are in.
+ * functions.
*/
static void bdrv_do_drained_end(BlockDriverState *bs, bool recursive,block/io.c +1
-4- BdrvChild *parent, bool ignore_bds_parents, - int *drained_end_counter) + BdrvChild *parent, bool ignore_bds_parents) {BdrvChild *child;int old_quiesce_counter; -assert (drained_end_counter !=NULL ); -[..]
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH 06/13] block: Drain invidual nodes during reopen
In reply to Vladimir Sementsov-OgievskiyAm 09.11.2022 um 17:00 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 28 lines
In subject: individual
Quoted 15 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]>--- block.c | 11 ++++------- block/replication.c | 6 ------ blockdev.c | 13 ------------- 3 files changed, 4 insertions(+), 26 deletions(-)[..]
blockdev.c void qmp_blockdev_reopen(BlockdevOptionsList *reopen_list, Error **errp) · line 3605
Quoted 6 lines
bdrv_reopen_queue_free(queue);blockdev.c +0
-5-for (p = drained; p; p = p->next) { -BlockDriverState *bs = p->data; -AioContext *ctx =bdrv_get_aio_context (bs); - -aio_context_acquire (ctx);
Quoted 2 lines
In
bdrv_reopen_queue_free()we don't have this acquire()/release() pair aroundbdrv_drained_end(). We don't need it anymore?
Good catch, I think we do.
Reopen is a bit messy with AioContext locks. I think the rule is supposed to be that bdrv_reopen_queue() requires that the lock for bs->aio_context is held, and bdrv_reopen_multiple() requires that no AioContext lock is held, right?
Because the former is not actually true: qmp_blockdev_reopen() and the 'replication' block driver do indeed take the lock, but bdrv_reopen() drops it for both functions!
So I think we also need an additional fix for bdrv_reopen() to drop the lock only after calling bdrv_reopen_queue(). It may not have made a difference before, but now that we call bdrv_drained_begin() in it, it seems important.
Kevin
Re: [PATCH 02/13] test-bdrv-drain: Don't yield in .bdrv_co_drained_begin/end()
In reply to Kevin WolfQuoted 15 lines · Kevin Wolf
We 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 increasebs->in_flightand 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 onbdrv_drain_end()restarting all operations immediately before the nextaio_poll().
Quoted 4 lines
Signed-off-by Kevin Wolf <[email protected]>--- tests/unit/test-bdrv-drain.c | 64 ++++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 18 deletions(-)
Re: [PATCH 09/13] block: Remove subtree drains
In reply to Kevin WolfQuoted 5 lines · Kevin Wolf
Subtree drains are not used any more. Remove them.
After this, BdrvChildClass.attach/detach() don't poll any more.
Signed-off-by Kevin Wolf <[email protected]>
Quoted 8 lines
--- 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(-)
Re: [PATCH 10/13] block: Call drain callbacks only once
In reply to Kevin WolfQuoted 23 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.
One thing that gets in the way is the '
ignore_bds_parents' parameter inbdrv_do_drained_begin_quiesce()andbdrv_do_drained_end(): If it is true for the first drain,bs->quiesce_counterwill be non-zero, but the parent callbacks still haven't been called, so a second drain where it is false would still have to call them.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.This patch only ignores the '
ignore_bds_parents' parameter. It will be removed in a separate patch.
Quoted 6 lines
Signed-off-by Kevin Wolf <[email protected]>--- block.c | 13 ++++++------- block/io.c | 24 +++++++++++++----------- tests/unit/test-bdrv-drain.c | 16 ++++++++++------ 3 files changed, 29 insertions(+), 24 deletions(-)
I too would like parent_quiesce_counter to become bool parent_quiesced, but:
Re: [PATCH 04/13] block: Remove drained_end_counter
In reply to Kevin WolfQuoted 9 lines · Kevin Wolf
drained_end_counteris 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 localdrained_end_counterto reach 0, these become a single function.Signed-off-by Kevin Wolf <[email protected]>
Quoted 8 lines
--- include/block/block-io.h | 15 ----- include/block/block_int-common.h | 6 +- block.c | 5 +- block/block-backend.c | 4 +- block/io.c | 97 ++++++++------------------------ blockjob.c | 2 +- 6 files changed, 30 insertions(+), 99 deletions(-)
The comments on bdrv_drained_end() and bdrv_parent_drained_end_single() in include/block/block-io.h still say that they poll some AioContext “which may result in a graph change”. That’s no longer the case, though, so those paragraphs should be dropped, I think.
Apart from that, looks good.
Hanna
Re: [PATCH 12/13] block: Don't poll in bdrv_replace_child_noperm()
In reply to Kevin WolfQuoted 13 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 child is already drained when the function is called (it better be, having in-flight requests while modifying the graph isn't going to end well!) 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
Signed-off-by Kevin Wolf <[email protected]>--- include/block/block-io.h | 8 ++++ block.c | 72 +++++++++++++++++++++++++----------- block/io.c | 2 +- tests/unit/test-bdrv-drain.c | 10 +++++ 4 files changed, 70 insertions(+), 22 deletions(-)
I find this change complicated. I understand it’s the point of the series, but I find it difficult to grasp. But I guess there can be no drain series without such a patch.
As usual, I was very skeptical of the code at first, and over time slowly realized that I’m mostly confused by the comments, and the code seems fine. Ah, well.
[...]
Quoted 4 lines
block.c +0
-0
[...]
block.c static TransactionActionDrv bdrv_replace_child_drv = { · line 2433
Quoted 7 lines
block.c +3
-0@@ -2414,12 +2428,20 @@ static TransactionActionDrv bdrv_replace_child_drv = {* * Note: real unref of old_bs is done only on commit. * +* Both child and new_bs (if non-NULL) must be drained. new_bs must be kept +* drained until the transaction is completed (this automatically implies that +* child remains drained, too).
I find “child” extremely ambiguous. The problem is that there generally is no way to drain a BdrvChild object, is there? You can only drain the BDS in it, which then drains the parent through the BdrvChild object. Historically, I don’t think there was ever a place where we cared about the BdrvChild object between the two to be drained, was there? I mean, now there apparently is, in bdrv_child_attach_common(), but that’s a different story.
So the problem is that “draining a BdrvChild object” generally appears in the context of bdrv_parent_drained_*() functions, i.e. actually functions draining the parent. Which makes it a bit confusing to refer to a BdrvChild object just as “child”.
I know that “child” here refers to the variable (or does it not?), but that is why I really prefer marking variables that are just plain English words, e.g. as @child or child, so it’s clear they are a name and not a noun.
In any case, because the concept is generally to drain the child->bs instead of the BdrvChild object directly, I understand the comment to mean: “Both the old child (child->bs) and new_bs (if non-NULL) must be drained. new_bs must be kept drained until the transaction is completed. This implies that the parent too will be kept drained until the transaction is completed by the BdrvChild object child.”
Or am I misunderstanding something, and the distinction between child and child->bs and the parent node is important here? (Would be good to know. :))
block.c static void bdrv_replace_child_noperm(BdrvChild *child,
Quoted 15 lines
block.c +5
-0+* * The function doesn't update permissions, caller is responsible for this. */ static void bdrv_replace_child_tran (BdrvChild *child,BlockDriverState *new_bs,Transaction *tran) {BdrvReplaceChildState *s =g_new (BdrvReplaceChildState,1 ); + +assert (child->parent_quiesce_counter); +assert (!new_bs || new_bs->quiesce_counter); + *s = (BdrvReplaceChildState ) { .child = child, .old_bs = child->bs,@@ -2818,6 +2840,12 @@ static void bdrv_replace_child_noperm(BdrvChild *child,
This function now has its callers fulfill kind of a complicated contract. I would prefer that to be written out in a doc comment, especially because it sounds like the assertions can’t cover everything (i.e. callers that remove a child are required to have stopped issuing requests to that child, but they are free to do that in any way they want, so no assertion will check for it here).
block.c static void bdrv_replace_child_noperm(BdrvChild *child, · line 2847
Quoted 8 lines
block.c +5
-0int new_bs_quiesce_counter;assert (!child->frozen); +/* +* When removing the child, it's the callers responsibility to make sure +* that no requests are in flight any more. Usually the parent is drained, +* but not through child->parent_quiesce_counter. +*/
When I see a comment above an assertion, I immediately assume it is going to describe what the assertion checks. Unless I’m misunderstanding something (again?), this comment actually describes what the assertion *does not* check. I find that confusing, especially because the comment leads with “it’s the caller’s responsibility”, which to me implies “and that’s why we check it here in this assertion”, because assertions are there to verify that contracts are met.
The assertion verifies that the parent must be drained (through @child), unless the child is removed, which case isn’t covered by the assertion. That “isn’t covered” is then described by the comment, right?
I’d prefer the comment to lead with describing what the assertion does check, and then transitioning to “But in case the child is removed, we ignore that, and just note that it’s the caller’s responsibility to...”.
Also, the comment doesn’t explicitly say why we don’t check it in the assertion. It says “usually” and “child->parent_quiesce_counter”, which implies “can’t get any information from child->parent_quiesce_counter, and regardless, callers can do what they want do achieve quiescing in regards to this child, so there’s nothing we can check”. It feels like we can just say outright that there’s an informal contract that we can’t formally verify here, but callers naturally still must adhere to it. It would be interesting to know (and note) why that is, though, i.e. why we can’t have parents be drained through the BdrvChild object for the child that is being removed.
I understand the intention behind the assertion to be: “We require the parent not to have in-flight requests to the BdrvChild object manipulated here. In most cases, we verify that by requiring the parent be drained through this BdrvChild object. However, when a child is being removed, we skip formal verification, because we leave callers free in deciding how to ensure that no requests are in flight. Usually, they will still have the parent be drained (even if not through this BdrvChild object), but we don’t require that.”
I may well be wrong, but then it would be good for a comment to correct me. :)
(Interestingly, because bdrv_replace_child_noperm() no longer polls itself, it can’t know for sure that child->parent_quiesce_counter > 0 means that there are no requests in flight.)
Quoted 3 lines
block.c +1
-0+assert (!new_bs || child->parent_quiesce_counter);assert (old_bs != new_bs);GLOBAL_STATE_CODE ();
[...]
block.c static void bdrv_replace_child_noperm(BdrvChild *child, · line 2876
Quoted 3 lines
block.c +0
-0@@ -2865,9 +2875,6 @@ static void bdrv_replace_child_noperm(BdrvChild *child,/* * If the old child node was drained but the new one is not, allow
This now also covers the case where there was no old child node, but the parent was simply drained via an empty BdrvChild by the caller.
Quoted 8 lines
block.c +0
-3* 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. */ new_bs_quiesce_counter = (new_bs ? new_bs->quiesce_counter :0 );if (!new_bs_quiesce_counter && child->parent_quiesce_counter) {
block.c static BdrvChild *bdrv_attach_child_common(BlockDriverState *child_bs, · line 3017
Quoted 7 lines
block.c +4
-0@@ -3004,6 +3011,12 @@ static BdrvChild *bdrv_attach_child_common(BlockDriverState *child_bs, }bdrv_ref (child_bs); +/* +* Let every new BdrvChild start drained, inserting it in the graph with +* bdrv_replace_child_noperm() will undrain it if the child node is not +* drained. The child was only just created, so polling is not necessary.
I feel like this is hiding some complexity. Unless I missed something, draining a BdrvChild always meant draining the parent. But here, it absolutely does not mean that, and maybe that deserves a big warning sign?
Beginning a drain without poll means quiescing. You assert that there can be no requests to the new child, which I agree on[1]. The combination of no new requests coming in, and no requests being there at this point is what being drained means. So @new_child is indeed “drained”.
But the parent isn’t drained, because it isn’t polled. There may still be requests in flight to its other children. That’s really interesting, and I found it extremely confusing until I wrote ten paragraphs in reply here and scrapped most of them again. Whenever I find this to be my reaction to something, I really wish for a detailed comment that explains the situation.
I would like the comment to:
- Expand on what “only just created” means. As it’s written, that could mean relying on a race condition. At which point would the parent be able to send requests? (I assume either the .attach() in bdrv_replace_child_noperm(), or when this function returns, whichever comes first. (The former always comes first.))
- Say in more detail that calling bdrv_parent_drained_begin_single() without polling will quiesce the parent, preventing new requests from appearing.
- Note that because there are no requests in flight, and because no new requests can then appear, the BdrvChild is drained.
- Note that the parent is only quiesced, not drained, and may still have requests in flight to other children, but naturally we don’t care about them.
I feel like the comment tries to hide all that complexity simply by avoiding the word “parent”.
[1] As far as I can piece together, no requests to the new child can have started yet, because this function creates the BdrvChild object, so before it is returned to the caller (or BdrvChildClass.attach() is called in bdrv_replace_child_noperm()), the block driver won’t generate requests to it.
Hanna
Quoted 5 lines
block.c +2
-0+*/ +bdrv_parent_drained_begin_single (new_child, false);bdrv_replace_child_noperm (new_child, child_bs);BdrvAttachChildCommonState *s =g_new (BdrvAttachChildCommonState,1 );
Re: [PATCH 08/13] stream: Replace subtree drain with a single node drain
In reply to Kevin WolfQuoted 22 lines · Kevin Wolf
The subtree drain was introduced in commit
b1e1af394d9as 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.
Quoted 6 lines
Signed-off-by Kevin Wolf <[email protected]>--- include/block/block-global-state.h | 3 +++ block.c | 17 ++++++++++++++--- block/stream.c | 20 ++++++++++---------- 3 files changed, 27 insertions(+), 13 deletions(-)
Re: [PATCH 13/13] block: Remove poll parameter from bdrv_parent_drained_begin_single()
In reply to Kevin WolfQuoted 9 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]>--- include/block/block-io.h | 5 ++--- block.c | 4 ++-- block/io.c | 7 ++----- 3 files changed, 6 insertions(+), 10 deletions(-)
Well, “drained_begin” does not mean “drain”, so...
Re: [PATCH 01/13] qed: Don't yield in bdrv_qed_co_drain_begin()
In reply to Kevin WolfQuoted 10 lines · Kevin Wolf
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_flightandbdrv_drained_begin()polls, the behaviour is unchanged.Signed-off-by Kevin Wolf <[email protected]>--- block/qed.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-)
Re: [PATCH 07/13] block: Don't use subtree drains in bdrv_drop_intermediate()
In reply to Kevin WolfQuoted 8 lines · Kevin Wolf
Instead 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.
Signed-off-by Kevin Wolf <[email protected]>--- block.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Re: [PATCH 10/13] block: Call drain callbacks only once
In reply to Vladimir Sementsov-OgievskiyAm 09.11.2022 um 19:05 hat Vladimir Sementsov-Ogievskiy geschrieben:
Quoted 47 lines · Kevin Wolf
Quoted 46 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.
One thing that gets in the way is the '
ignore_bds_parents' parameter inbdrv_do_drained_begin_quiesce()andbdrv_do_drained_end(): If it is true for the first drain,bs->quiesce_counterwill be non-zero, but the parent callbacks still haven't been called, so a second drain where it is false would still have to call them.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.This patch only ignores the '
ignore_bds_parents' parameter. It will be removed in a separate patch.Signed-off-by Kevin Wolf <[email protected]>--- block.c | 13 ++++++------- block/io.c | 24 +++++++++++++----------- tests/unit/test-bdrv-drain.c | 16 ++++++++++------ 3 files changed, 29 insertions(+), 24 deletions(-)block.c +0
-2@@ -2816,7 +2816,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);@@ -2827,15 +2826,13 @@ static void bdrv_replace_child_noperm(BdrvChild *child, } new_bs_quiesce_counter = (new_bs ? new_bs->quiesce_counter :0 ); - drain_saldo = new_bs_quiesce_counter - child->parent_quiesce_counter;/*
Quoted 8 lines
block.c static void bdrv_replace_child_noperm(BdrvChild *child, · line 2834
Quoted 5 lines
block.c +1
-1* 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) { +if (new_bs_quiesce_counter && !child->parent_quiesce_counter) {Looks like checking for
child->klass->drained_beginwas a wrong thing even prepatch?
I'm not sure if it was strictly wrong in practice, but at least unnecessary. It would have been wrong if a BdrvChildClass implemented for example .drained_begin, but not .drain_end. But I think we always implement all three of .drained_begin/poll/end or none of them.
Quoted 2 lines
Also,
parent_quiesce_counteractually becomes a boolean variable.. Should we stress it by new type and name?
Ok, but I would do that in a separate patch. Maybe 'bool drains_parent'.
Quoted 11 lines
block.c static void bdrv_replace_child_noperm(BdrvChild *child, · line 2857
Quoted 7 lines
block.c +0
-1bdrv_parent_drained_begin_single (child, true); - drain_saldo--; }if (old_bs) {@@ -2859,7 +2856,6 @@ static void bdrv_replace_child_noperm(BdrvChild *child,* more often. */ the comment above ^^^ should be updated, we are not going to call
drained_endmore than once anyway
Quoted 3 lines
block.c static void bdrv_replace_child_noperm(BdrvChild *child, · line 2858
Quoted 1 line
block.c +0
-0assert (new_bs->quiesce_counter <= new_bs_quiesce_counter);do we still need this assertion and the comment at all?
Patch 12 removes both, but I can do it already here.
Quoted 37 lines
Quoted 37 lines
block.c +5
-3- drain_saldo += new_bs->quiesce_counter - new_bs_quiesce_counter;if (child->klass->attach) { child->klass->attach (child);@@ -2869,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->parent_quiesce_counter) {bdrv_parent_drained_end_single (child); - drain_saldo++; } }block/io.c +2
-1@@ -62,7 +62,7 @@ void bdrv_parent_drained_end_single(BdrvChild *c) {IO_OR_GS_CODE (); -assert (c->parent_quiesce_counter >0 ); +assert (c->parent_quiesce_counter ==1 ); c->parent_quiesce_counter--;if (c->klass->drained_end) { c->klass->drained_end (c);@@ -109,6 +109,7 @@ static bool bdrv_parent_drained_poll(BlockDriverState *bs, BdrvChild *ignore,void bdrv_parent_drained_begin_single (BdrvChild *c,bool poll) {IO_OR_GS_CODE (); +assert (c->parent_quiesce_counter ==0 ); c->parent_quiesce_counter++;if (c->klass->drained_begin) {
Quoted 8 lines
block/io.c void bdrv_do_drained_begin_quiesce(BlockDriverState *bs, · line 355
Quoted 6 lines
block/io.c +0
-1c->klass->drained_begin (c);@@ -352,16 +353,16 @@ void bdrv_do_drained_begin_quiesce(BlockDriverState *bs, BdrvChild *parent, bool ignore_bds_parents) {IO_OR_GS_CODE (); -assert (!qemu_in_coroutine ());why that is dropped? seems unrelated to the commit
I'm sure I added it because I actually got an assertion failure, but I can't reproduce it on this commit now. At the end of the series tests do fail without this removed. I'll double check which commit is right one to remove it.
Quoted 11 lines
Quoted 11 lines
block/io.c +4
-4/* 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);
Quoted 8 lines
block/io.c static void bdrv_do_drained_end(BlockDriverState *bs, BdrvChild *parent, · line 414
Quoted 6 lines
block/io.c +1
-0+ } } }@@ -412,13 +413,14 @@ static void bdrv_do_drained_end(BlockDriverState *bs, BdrvChild *parent,assert (bs->quiesce_counter >0 );/* Re-enable things in child-to-parent order */ the comment should be moved too, I think
It is the same place as in bdrv_do_drained_begin_quiesce().
Quoted 8 lines
Quoted 8 lines
block/io.c +1
-5-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) {
Quoted 8 lines
block/io.c static void bdrv_do_drained_end(BlockDriverState *bs, BdrvChild *parent, · line 426
Quoted 8 lines
block/io.c +5
-0+ 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)); } }
Kevin
Re: [PATCH 11/13] block: Remove ignore_bds_parents parameter from drain functions
In reply to Kevin WolfQuoted 8 lines · Kevin Wolf
ignore_bds_parentsis now ignored, so we can just remove it.Signed-off-by Kevin Wolf <[email protected]>--- include/block/block-io.h | 10 ++---- block.c | 4 +-- block/io.c | 78 +++++++++++++++------------------------- 3 files changed, 32 insertions(+), 60 deletions(-)
Re: [PATCH 03/13] block: Revert .bdrv_drained_begin/end to non-coroutine_fn
In reply to Kevin WolfQuoted 17 lines · Kevin Wolf
Polling during
bdrv_drained_end()can be problematic (and in the future, we may get cases forbdrv_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 ifAIO_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 explicitaio_poll()calls need to be added because the test case wants to verify the final state after BHs have executed.Signed-off-by Kevin Wolf <[email protected]>
Quoted 8 lines
--- include/block/block_int-common.h | 10 ++++--- block.c | 4 +-- block/io.c | 49 +++++--------------------------- block/qed.c | 4 +-- block/throttle.c | 6 ++-- tests/unit/test-bdrv-drain.c | 18 ++++++------ 6 files changed, 30 insertions(+), 61 deletions(-)
As the others have already suggested, I’d too drop the _co_ in qed and throttle, and the coroutine_fn in throttle. With that done:
Re: [PATCH 05/13] block: Inline bdrv_drain_invoke()
In reply to Kevin WolfQuoted 8 lines · Kevin Wolf
bdrv_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.Signed-off-by Kevin Wolf <[email protected]>--- block/io.c | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-)
Re: [PATCH 12/13] block: Don't poll in bdrv_replace_child_noperm()
In reply to Hanna ReitzAm 14.11.2022 um 21:22 hat Hanna Reitz geschrieben:
Quoted 22 lines · Kevin Wolf
Quoted 13 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 child is already drained when the function is called (it better be, having in-flight requests while modifying the graph isn't going to end well!) 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
Signed-off-by Kevin Wolf <[email protected]>--- include/block/block-io.h | 8 ++++ block.c | 72 +++++++++++++++++++++++++----------- block/io.c | 2 +- tests/unit/test-bdrv-drain.c | 10 +++++ 4 files changed, 70 insertions(+), 22 deletions(-)
Quoted 7 lines
I find this change complicated. I understand it’s the point of the series, but I find it difficult to grasp. But I guess there can be no drain series without such a patch.
As usual, I was very skeptical of the code at first, and over time slowly realized that I’m mostly confused by the comments, and the code seems fine. Ah, well.
I spent a while thinking about how to do things differently, but I think my conclusion is that only improving the comments is probably the best.
The real condition in bdrv_replace_child_noperm() is: If you want to change the BdrvChild to point to a drained node with child->bs, either child->parent_quiesce_counter must already be non-zero or you must be able to increase it and keep the parent's quiesce_counter consistent with that, but without polling or starting new requests.
This patch generalised the condition to non-drained child nodes as well just because it's easier to verify when it's a condition that applies always. It also picked the first option, child->parent_quiesce_counter already being non-zero.
If we wanted to implement the second option, the potential problem is in the "without starting new requests" condition, because .drained_begin can start new requests on the child (e.g. restarting throttled requests, so that we can actually drain the request queue).
Note that we're not even interested in draining the request queue here, we already assume that the parent doesn't have active requests on the child (otherwise we would always have gotten crashes). We just want to get the counters to agree with each other.
Maybe the most reasonable approach for this would be formally requiring that .drained_begin both in BdrvChildClass and BlockDriver not do anything if the thing in question is already quiesced (I think this is true in practice; for BlockDriver probably only after the earlier patches in this series) and then assert(bdrv_parent_is_drained(child)) in bdrv_replace_child_noperm(), which would require a new BdrvChildClass callback. Then the bdrv_parent_drained_begin_single() call could stay around, but wouldn't have to poll any more.
Of course, all of the drains in this patch would have to stay anyway to make sure that the parent is already drained. So I'm not sure if it's any simpler or better in any way than requiring that the parent was already drained through _this_ BdrvChild.
What do you think?
Quoted 9 lines
[...]
Quoted 4 lines
block.c +0
-0[...]
Quoted 15 lines
block.c static TransactionActionDrv bdrv_replace_child_drv = { · line 2433
Quoted 7 lines
block.c +3
-0@@ -2414,12 +2428,20 @@ static TransactionActionDrv bdrv_replace_child_drv = {* * Note: real unref of old_bs is done only on commit. * +* Both child and new_bs (if non-NULL) must be drained. new_bs must be kept +* drained until the transaction is completed (this automatically implies that +* child remains drained, too). I find “child” extremely ambiguous. The problem is that there generally is no way to drain a BdrvChild object, is there? You can only drain the BDS in it, which then drains the parent through the BdrvChild object. Historically, I don’t think there was ever a place where we cared about the BdrvChild object between the two to be drained, was there? I mean, now there apparently is, in
bdrv_child_attach_common(), but that’s a different story.
I think we've always cared about the parent drain happening through the BdrvChild, though, at least since your commit 804db8ea which introduced BdrvChild.parent_quiesce_counter.
Whether or not calling the BdrvChild drained in this case is probably more a question of terminology.
If we want to avoid calling a BdrvChild drained, I guess I could require child->bs to be drained instead, which implies the condition we're really interested in.
Quoted 8 lines
So the problem is that “draining a BdrvChild object” generally appears in the context of bdrv_parent_drained_*() functions, i.e. actually functions draining the parent. Which makes it a bit confusing to refer to a BdrvChild object just as “child”.
I know that “child” here refers to the variable (or does it not?), but that is why I really prefer marking variables that are just plain English words, e.g. as @child or
child, so it’s clear they are a name and not a noun.
That's fair, I should add that @. (Yes, it does.)
Quoted 10 lines
In any case, because the concept is generally to drain the
child->bsinstead of the BdrvChild object directly, I understand the comment to mean:
“Both the old child (child->bs) andnew_bs(if non-NULL) must be drained.new_bsmust be kept drained until the transaction is completed. This implies that the parent too will be kept drained until the transaction is completed by the BdrvChild objectchild.”Or am I misunderstanding something, and the distinction between
childandchild->bsand the parent node is important here? (Would be good to know. :))
I'm not sure how a transaction "is completed by the BdrvChild object" (isn't it the caller that finalises the transaction?), but I think otherwise that's equivalent to what I was trying to express.
Oh, is it just the word order that confused me, and you really mean "will be kept drained by the BdrvChild object until the transation is completed (by the caller)"?
Quoted 14 lines
Quoted 14 lines
block.c +5
-0+* * The function doesn't update permissions, caller is responsible for this. */ static void bdrv_replace_child_tran (BdrvChild *child,BlockDriverState *new_bs,Transaction *tran) {BdrvReplaceChildState *s =g_new (BdrvReplaceChildState,1 ); + +assert (child->parent_quiesce_counter); +assert (!new_bs || new_bs->quiesce_counter); + *s = (BdrvReplaceChildState ) { .child = child, .old_bs = child->bs,
Quoted 8 lines
block.c static void bdrv_replace_child_noperm(BdrvChild *child,
Quoted 1 line
block.c +0
-0@@ -2818,6 +2840,12 @@ static void bdrv_replace_child_noperm(BdrvChild *child,This function now has its callers fulfill kind of a complicated contract. I would prefer that to be written out in a doc comment, especially because it sounds like the assertions can’t cover everything (i.e. callers that remove a child are required to have stopped issuing requests to that child, but they are free to do that in any way they want, so no assertion will check for it here).
Ok, I can add a comment.
I don't think the contract is complicated: The parent has to be drained through this BdrvChild (because new_bs could already be drained), except for new_bs == NULL which is obviously not attaching the child to a drained node.
Quoted 8 lines
block.c static void bdrv_replace_child_noperm(BdrvChild *child, · line 2847
Quoted 7 lines
block.c +5
-0int new_bs_quiesce_counter;assert (!child->frozen); +/* +* When removing the child, it's the callers responsibility to make sure +* that no requests are in flight any more. Usually the parent is drained, +* but not through child->parent_quiesce_counter. +*/
Quoted 7 lines
When I see a comment above an assertion, I immediately assume it is going to describe what the assertion checks. Unless I’m misunderstanding something (again?), this comment actually describes what the assertion *does not* check. I find that confusing, especially because the comment leads with “it’s the caller’s responsibility”, which to me implies “and that’s why we check it here in this assertion”, because assertions are there to verify that contracts are met.
The comment is bad, I must have been confused myself while writing it.
The logic here isn't even about request in flights. It's true that there must be none, but that's a separate requirement. What it is about is maintaining consistency between child->parent_quiesce_counter and the parent's own quiesce_counter in case of switching to a drained node, without having to poll - which is only possible if the parent is already drained.
That we require the parent to be drained through this specific BdrvChild is a choice with the intention to keep things simpler, as explained above.
Quoted 8 lines
The assertion verifies that the parent must be drained (through @child), unless the child is removed, which case isn’t covered by the assertion. That “isn’t covered” is then described by the comment, right?
I’d prefer the comment to lead with describing what the assertion does check, and then transitioning to “But in case the child is removed, we ignore that, and just note that it’s the caller’s responsibility to...”.
Quoted 10 lines
Also, the comment doesn’t explicitly say why we don’t check it in the assertion. It says “usually” and “child->parent_quiesce_counter”, which implies “can’t get any information from
child->parent_quiesce_counter, and regardless, callers can do what they want do achieve quiescing in regards to this child, so there’s nothing we can check”. It feels like we can just say outright that there’s an informal contract that we can’t formally verify here, but callers naturally still must adhere to it. It would be interesting to know (and note) why that is, though, i.e. why we can’t have parents be drained through the BdrvChild object for the child that is being removed.
We could require that, it would just be more complicated for the callers that pass a constant NULL, for no real benefit.
Quoted 20 lines
I understand the intention behind the assertion to be: “We require the parent not to have in-flight requests to the BdrvChild object manipulated here. In most cases, we verify that by requiring the parent be drained through this BdrvChild object. However, when a child is being removed, we skip formal verification, because we leave callers free in deciding how to ensure that no requests are in flight. Usually, they will still have the parent be drained (even if not through this BdrvChild object), but we don’t require that.”
I may well be wrong, but then it would be good for a comment to correct me. :)
(Interestingly, because
bdrv_replace_child_noperm()no longer polls itself, it can’t know for sure thatchild->parent_quiesce_counter > 0means that there are no requests in flight.)block.c static void bdrv_replace_child_noperm(BdrvChild *child, · line 2850
Quoted 3 lines
block.c +1
-0+assert (!new_bs || child->parent_quiesce_counter);assert (old_bs != new_bs);GLOBAL_STATE_CODE ();
Quoted 8 lines
[...]
block.c static void bdrv_replace_child_noperm(BdrvChild *child, · line 2876
Quoted 3 lines
block.c +0
-0@@ -2865,9 +2875,6 @@ static void bdrv_replace_child_noperm(BdrvChild *child,/* * If the old child node was drained but the new one is not, allow This now also covers the case where there was no old child node, but the parent was simply drained via an empty BdrvChild by the caller.
I'm not sure how to express this concisely if we want to avoid calling the BdrvChild itself drained.
Quoted 30 lines
Quoted 8 lines
block.c +0
-3* 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. */ new_bs_quiesce_counter = (new_bs ? new_bs->quiesce_counter :0 );if (!new_bs_quiesce_counter && child->parent_quiesce_counter) {block.c static BdrvChild *bdrv_attach_child_common(BlockDriverState *child_bs, · line 3016
Quoted 6 lines
block.c +4
-0@@ -3004,6 +3011,12 @@ static BdrvChild *bdrv_attach_child_common(BlockDriverState *child_bs, }bdrv_ref (child_bs); +/* +* Let every new BdrvChild start drained, inserting it in the graph with +* bdrv_replace_child_noperm() will undrain it if the child node is not +* drained. The child was only just created, so polling is not necessary. I feel like this is hiding some complexity. Unless I missed something, draining a BdrvChild always meant draining the parent. But here, it absolutely does not mean that, and maybe that deserves a big warning sign?
Beginning a drain without poll means quiescing. You assert that there can be no requests to the new child, which I agree on[1]. The combination of no new requests coming in, and no requests being there at this point is what being drained means. So @new_child is indeed “drained”.
But the parent isn’t drained, because it isn’t polled. There may still be requests in flight to its other children. That’s really interesting, and I found it extremely confusing until I wrote ten paragraphs in reply here and scrapped most of them again. Whenever I find this to be my reaction to something, I really wish for a detailed comment that explains the situation.
Quoted 6 lines
I would like the comment to:
- Expand on what “only just created” means. As it’s written, that could mean relying on a race condition. At which point would the parent be able to send requests? (I assume either the .attach() inbdrv_replace_child_noperm(), or when this function returns, whichever comes first. (The former always comes first.))
I don't think .attach is supposed to create requests - even less so while the BdrvChild is drained. It may schedule a BH, but that won't be executed until this function returns.
This is not documented explicitly, maybe we should document it.
I suppose .drained_end can create requests in general, but it wouldn't make sense to me if it did that for a new child. It generally just resumes operations that were stopped because of the drain, but there was no operation on a new child yet.
Quoted 7 lines
- Say in more detail that calling
bdrv_parent_drained_begin_single()without polling will quiesce the parent, preventing new requests from appearing.
- Note that because there are no requests in flight, and because no new requests can then appear, the BdrvChild is drained.
- Note that the parent is only quiesced, not drained, and may still have requests in flight to other children, but naturally we don’t care about them.
All of this is true, but at the same time not related to what the bdrv_parent_drained_begin_single() call is meant for - increasing the parent's quiesce_count from the BdrvChild before calling bdrv_replace_child_noperm(), so that we don't have to do it inside of that function where we don't know that we don't have to poll.
That there are no requests in flight when you change child->bs is a requirement that we already had before this patch.
If it feels better to you, we could even just poll here (and drop patch 13 because it would still be used).
The part that is important in the context of Emanuele's patches that will follow is that we poll outside of a bdrv_graph_wrlock/wrunlock() section. This might mean that we'd have to pull the polling further down into the callers in the long run. Emanuele's current patches only put the lock in bdrv_replace_child_noperm(), but generally speaking you wouldn't want the graph to change between two related changes, so I'm almost sure that the lock will be taken in callers in the future.
Quoted 8 lines
I feel like the comment tries to hide all that complexity simply by avoiding the word “parent”.
[1] As far as I can piece together, no requests to the new child can have started yet, because this function creates the BdrvChild object, so before it is returned to the caller (or
BdrvChildClass.attach()is called inbdrv_replace_child_noperm()), the block driver won’t generate requests to it.
Kevin