do not drain while holding the graph lock
cover do not drain while holding the graph lock
This series is an attempt to fix a deadlock issue reported by Andrey here [0].
bdrv_drained_begin() polls and is not allowed to be called with the block graph lock held. Mark the function as GRAPH_UNLOCKED.
This does not catch the issue reported by Andrey, because there is a bdrv_graph_rdunlock_main_loop() before bdrv_drained_begin() in the function bdrv_change_aio_context(). That unlock is of course ineffective if the exclusive lock is held, but it seems to prevent TSA from finding the issue.
Thus the bdrv_drained_begin() call from inside bdrv_change_aio_context() needs to be moved up the call stack before acquiring the locks. This required the most changes, patch 09/11.
Granular draining is not trivially possible, because many of the affected functions can recursively call themselves.
In place where bdrv_drained_begin() calls were removed, assertions are added, checking the quiesced_counter to ensure that the nodes already got drained further up in the call stack.
In bdrv_graph_wrlock() there is a comment that it uses bdrv_drain_all_begin_nopoll() to make sure that constantly arriving new I/O doesn't cause starvation. The changes from this series are at odds with that, but there doesn't seem to be any (new) test failures.
[0]: https://lore.kernel.org/qemu-devel/[email protected]/
I did not CC the maintainers for blkverify, vmdk, etc. because this is just an RFC and the changes there are mechanical, for adapting some callers of block layer functions.
Andrey Drobyshev (1):
iotests/graph-changes-while-io: add test case with removal of lower
snapshot
Fiona Ebner (10):
block: remove outdated comments about AioContext locking
block: move drain outside of read-locked bdrv_reopen_queue_child() block/snapshot: move drain outside of read-locked
bdrv_snapshot_delete()
block: drain while unlocked in bdrv_reopen_parse_file_or_backing()
block: move drain outside of read-locked bdrv_inactivate_recurse()
blockdev: drain while unlocked in internal_snapshot_action()
blockdev: drain while unlocked in external_snapshot_action()
block: mark bdrv_drained_begin() as GRAPH_UNLOCKED
block: move drain out of bdrv_change_aio_context() block/graph-lock: add drain flag to bdrv_graph_wr{,un}lock
block.c | 170 +++++++++++------- block/backup.c | 4 +- block/blklogwrites.c | 8 +- block/blkverify.c | 4 +- block/block-backend.c | 8 +- block/commit.c | 16 +- block/graph-lock.c | 22 ++- block/mirror.c | 22 +-- block/qcow2.c | 4 +- block/quorum.c | 8 +- block/replication.c | 14 +- block/snapshot.c | 22 ++- block/stream.c | 18 +- block/vmdk.c | 20 +-- blockdev.c | 59 ++++-- blockjob.c | 12 +- include/block/block-global-state.h | 8 +- include/block/block-io.h | 3 +- include/block/graph-lock.h | 8 +- qemu-img.c | 2 + scripts/block-coroutine-wrapper.py | 4 +- .../qemu-iotests/tests/graph-changes-while-io | 101 ++++++++++- .../tests/graph-changes-while-io.out | 4 +- tests/unit/test-bdrv-drain.c | 40 ++--- tests/unit/test-bdrv-graph-mod.c | 20 +-- 25 files changed, 384 insertions(+), 217 deletions(-)
Signature · 1 line
2.39.5
PATCH 1/11 block: remove outdated comments about AioContext locking
AioContext locking was removed in commit b49f4755c7 ("block: remove AioContext locking").
--- block.c | 3 --- 1 file changed, 3 deletions(-)
block.c +0 -3
@@ -4359,8 +4359,6 @@ bdrv_recurse_has_child(BlockDriverState *bs, BlockDriverState *child)* bs_queue, or the existing bs_queue being used. * * bs is drained here and undrained by bdrv_reopen_queue_free(). -* -* To be called with bs->aio_context locked. */ static BlockReopenQueue * GRAPH_RDLOCK bdrv_reopen_queue_child(BlockReopenQueue *bs_queue, BlockDriverState *bs,
@@ -4519,7 +4517,6 @@ bdrv_reopen_queue_child(BlockReopenQueue *bs_queue, BlockDriverState *bs,return bs_queue; } -/* To be called with bs->aio_context locked */ BlockReopenQueue *bdrv_reopen_queue (BlockReopenQueue *bs_queue,BlockDriverState *bs,QDict *options,bool keep_old_opts)
Signature · 1 line
2.39.5
Re: [PATCH 01/11] block: remove outdated comments about AioContext locking
Am 08.05.2025 um 16:09 hat Fiona Ebner geschrieben:
Quoted 4 lines
AioContext locking was removed in commit
b49f4755c7("block: remove AioContext locking").Signed-off-by Fiona Ebner <[email protected]>
PATCH 2/11 block: move drain outside of read-locked bdrv_reopen_queue_child()
In reply to Fiona EbnerThis is in preparation to mark bdrv_drained_begin() as GRAPH_UNLOCKED.
More granular draining is not trivially possible, because bdrv_reopen_queue_child() can recursively call itself.
--- block.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-)
block.c +15 -9
@@ -4358,7 +4358,7 @@ bdrv_recurse_has_child(BlockDriverState *bs, BlockDriverState *child)* returns a pointer to bs_queue, which is either the newly allocated * bs_queue, or the existing bs_queue being used. * -* bs is drained here and undrained by bdrv_reopen_queue_free(). +* bs needs to be drained */ static BlockReopenQueue * GRAPH_RDLOCK bdrv_reopen_queue_child(BlockReopenQueue *bs_queue, BlockDriverState *bs,
@@ -4368,6 +4368,7 @@ bdrv_reopen_queue_child(BlockReopenQueue *bs_queue, BlockDriverState *bs, bool keep_old_opts) {assert (bs !=NULL ); +assert (qatomic_read (&bs->quiesce_counter) >0 );BlockReopenQueueEntry *bs_entry;BdrvChild *child;
@@ -4377,13 +4378,6 @@ bdrv_reopen_queue_child(BlockReopenQueue *bs_queue, BlockDriverState *bs,GLOBAL_STATE_CODE (); -/* -* Strictly speaking, draining is illegal under GRAPH_RDLOCK. We know that -* we've been called with bdrv_graph_rdlock_main_loop(), though, so it's ok -* in practice. -*/ -bdrv_drained_begin (bs); -if (bs_queue ==NULL ) { bs_queue =g_new0 (BlockReopenQueue,1 );QTAILQ_INIT (bs_queue);
@@ -4522,6 +4516,14 @@ BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue, QDict *options, bool keep_old_opts) {GLOBAL_STATE_CODE (); + +if (bs_queue ==NULL ) { +/* +* paired with bdrv_drain_all_end() in bdrv_reopen_queue_free(). +*/ +bdrv_drain_all_begin (); + } +GRAPH_RDLOCK_GUARD_MAINLOOP (); return bdrv_reopen_queue_child(bs_queue, bs, options, NULL, 0, false,
@@ -4534,12 +4536,16 @@ 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); }g_free (bs_queue); + +/* +* paired with bdrv_drain_all_begin() in bdrv_reopen_queue(). +*/ +bdrv_drain_all_end (); } }
Signature · 1 line
2.39.5
Re: [PATCH 02/11] block: move drain outside of read-locked bdrv_reopen_queue_child()
Am 08.05.2025 um 16:09 hat Fiona Ebner geschrieben:
Quoted 4 lines
This is in preparation to mark
bdrv_drained_begin()asGRAPH_UNLOCKED.More granular draining is not trivially possible, because
bdrv_reopen_queue_child()can recursively call itself.
Yeah, we would have to collect the affected nodes first and only then do whatever the draining is needed for. And consider that draining could invalidate the list of nodes so that we would have to start over...
Draining only selectively was probably premature optimisation anyway.
Quoted 19 lines
Signed-off-by Fiona Ebner <[email protected]>--- block.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-)block.c +1
-1@@ -4358,7 +4358,7 @@ bdrv_recurse_has_child(BlockDriverState *bs, BlockDriverState *child)* returns a pointer to bs_queue, which is either the newly allocated * bs_queue, or the existing bs_queue being used. * -* bs is drained here and undrained by bdrv_reopen_queue_free(). +* bs needs to be drained */ static BlockReopenQueue * GRAPH_RDLOCK bdrv_reopen_queue_child(BlockReopenQueue *bs_queue, BlockDriverState *bs,
block.c bdrv_reopen_queue_child(BlockReopenQueue *bs_queue, BlockDriverState *bs, · line 4371
Quoted 4 lines
block.c +1
-0@@ -4368,6 +4368,7 @@ bdrv_reopen_queue_child(BlockReopenQueue *bs_queue, BlockDriverState *bs, bool keep_old_opts) {assert (bs !=NULL ); +assert (qatomic_read (&bs->quiesce_counter) >0 );
BlockDriverState.quiesce_counter isn't accessed with atomics elsewhere. Did you confuse it with BlockBackend.quiesce_counter?
Quoted 18 lines
block.c +0
-7BlockReopenQueueEntry *bs_entry;BdrvChild *child;@@ -4377,13 +4378,6 @@ bdrv_reopen_queue_child(BlockReopenQueue *bs_queue, BlockDriverState *bs,GLOBAL_STATE_CODE (); -/* -* Strictly speaking, draining is illegal under GRAPH_RDLOCK. We know that -* we've been called with bdrv_graph_rdlock_main_loop(), though, so it's ok -* in practice. -*/ -bdrv_drained_begin (bs); -if (bs_queue ==NULL ) { bs_queue =g_new0 (BlockReopenQueue,1 );QTAILQ_INIT (bs_queue);
block.c BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue, · line 4524
Quoted 9 lines
block.c +6
-0@@ -4522,6 +4516,14 @@ BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue, QDict *options, bool keep_old_opts) {GLOBAL_STATE_CODE (); + +if (bs_queue ==NULL ) { +/* +* paired with bdrv_drain_all_end() in bdrv_reopen_queue_free(). +*/ +bdrv_drain_all_begin ();
Having this comment is a good idea. It fits on a single line, though.
Quoted 10 lines
block.c +2
-1+ } +GRAPH_RDLOCK_GUARD_MAINLOOP (); return bdrv_reopen_queue_child(bs_queue, bs, options, NULL, 0, false,@@ -4534,12 +4536,16 @@ 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);
block.c void bdrv_reopen_queue_free(BlockReopenQueue *bs_queue) · line 4548
Quoted 10 lines
block.c +5
-0qobject_unref (bs_entry->state.explicit_options);qobject_unref (bs_entry->state.options);g_free (bs_entry); }g_free (bs_queue); + +/* +* paired with bdrv_drain_all_begin() in bdrv_reopen_queue(). +*/ +bdrv_drain_all_end ();
This could be a single line comment, too.
Kevin
Re: [PATCH 02/11] block: move drain outside of read-locked bdrv_reopen_queue_child()
Am 14.05.25 um 18:36 schrieb Kevin Wolf:
Quoted 9 lines
Am 08.05.2025 um 16:09 hat Fiona Ebner geschrieben:
block.c bdrv_reopen_queue_child(BlockReopenQueue *bs_queue, BlockDriverState *bs, · line 4371
Quoted 5 lines
block.c +1
-0@@ -4368,6 +4368,7 @@ bdrv_reopen_queue_child(BlockReopenQueue *bs_queue, BlockDriverState *bs, bool keep_old_opts) {assert (bs !=NULL ); +assert (qatomic_read (&bs->quiesce_counter) >0 );BlockDriverState.quiesce_counter isn't accessed with atomics elsewhere. Did you confuse it with BlockBackend.quiesce_counter?
No, but I saw that it is modified via qatomic_fetch_inc/dec(). And those modifications are in bdrv_do_drained_begin/end() which are IO_OR_GS_CODE(). So isn't it more correct to read via atomics here?
The documentation in include/block/block_int-common.h for struct BlockDriverState also states:
Quoted 2 lines
/* Accessed with atomic ops. */
int quiesce_counter;Should I rather add a patch to have the other readers use atomics too?
Quoted 4 lines
Quoted 4 lines
block.c +0
-0@@ -4522,6 +4516,14 @@ BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue, QDict *options, bool keep_old_opts) {GLOBAL_STATE_CODE ();
Quoted 8 lines
block.c BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue, · line 4524
Quoted 6 lines
block.c +6
-0+ +if (bs_queue ==NULL ) { +/* +* paired with bdrv_drain_all_end() in bdrv_reopen_queue_free(). +*/ +bdrv_drain_all_begin ();Having this comment is a good idea. It fits on a single line, though.
Ack.
Quoted 15 lines
Quoted 15 lines
block.c +2
-1+ } +GRAPH_RDLOCK_GUARD_MAINLOOP (); return bdrv_reopen_queue_child(bs_queue, bs, options, NULL, 0, false,@@ -4534,12 +4536,16 @@ 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); }
Quoted 8 lines
block.c void bdrv_reopen_queue_free(BlockReopenQueue *bs_queue) · line 4548
Quoted 6 lines
block.c +5
-0g_free (bs_queue); + +/* +* paired with bdrv_drain_all_begin() in bdrv_reopen_queue(). +*/ +bdrv_drain_all_end ();This could be a single line comment, too.
Ack.
Best Regards,
Fiona
Re: [PATCH 02/11] block: move drain outside of read-locked bdrv_reopen_queue_child()
Am 15.05.2025 um 13:48 hat Fiona Ebner geschrieben:
Quoted 11 lines
Am 14.05.25 um 18:36 schrieb Kevin Wolf:
Quoted 9 lines
Am 08.05.2025 um 16:09 hat Fiona Ebner geschrieben:
block.c bdrv_reopen_queue_child(BlockReopenQueue *bs_queue, BlockDriverState *bs, · line 4371
Quoted 5 lines
block.c +1
-0@@ -4368,6 +4368,7 @@ bdrv_reopen_queue_child(BlockReopenQueue *bs_queue, BlockDriverState *bs, bool keep_old_opts) {assert (bs !=NULL ); +assert (qatomic_read (&bs->quiesce_counter) >0 );BlockDriverState.quiesce_counter isn't accessed with atomics elsewhere. Did you confuse it with BlockBackend.quiesce_counter?
Quoted 3 lines
No, but I saw that it is modified via qatomic_fetch_inc/dec(). And those modifications are in bdrv_do_drained_begin/end() which are
IO_OR_GS_CODE(). So isn't it more correct to read via atomics here?
Aha, I missed these two places. Looks like Paolo's commit 414c2ec wasn't very thorough with converting.
The commit message is also empty, so I don't know why we made this change. Both places are GLOBAL_STATE_CODE(), so I don't think we actually need atomics to synchronise these two places. Maybe there are other accesses in iothreads, but then those should have been using atomics, too.
Quoted 6 lines
The documentation in
include/block/block_int-common.hfor struct BlockDriverState also states:Quoted 2 lines
/* Accessed with atomic ops. */ int quiesce_counter;Should I rather add a patch to have the other readers use atomics too?
Either all accesses should use atomics or none of them. I'm not completely sure which way is the right one. Using atomics everywhere is the safe option, but I'm not sure if we ever access quiesce_counter outside of the main thread.
Kevin
Re: [PATCH 02/11] block: move drain outside of read-locked bdrv_reopen_queue_child()
Am 15.05.25 um 14:28 schrieb Kevin Wolf:
Quoted 16 lines
Am 15.05.2025 um 13:48 hat Fiona Ebner geschrieben:
Quoted 11 lines
Am 14.05.25 um 18:36 schrieb Kevin Wolf:
Quoted 9 lines
Am 08.05.2025 um 16:09 hat Fiona Ebner geschrieben:
block.c bdrv_reopen_queue_child(BlockReopenQueue *bs_queue, BlockDriverState *bs, · line 4371
Quoted 5 lines
block.c +1
-0@@ -4368,6 +4368,7 @@ bdrv_reopen_queue_child(BlockReopenQueue *bs_queue, BlockDriverState *bs, bool keep_old_opts) {assert (bs !=NULL ); +assert (qatomic_read (&bs->quiesce_counter) >0 );BlockDriverState.quiesce_counter isn't accessed with atomics elsewhere. Did you confuse it with BlockBackend.quiesce_counter?
Quoted 3 lines
No, but I saw that it is modified via qatomic_fetch_inc/dec(). And those modifications are in bdrv_do_drained_begin/end() which are
IO_OR_GS_CODE(). So isn't it more correct to read via atomics here?
Quoted 8 lines
Aha, I missed these two places. Looks like Paolo's commit 414c2ec wasn't very thorough with converting.
The commit message is also empty, so I don't know why we made this change. Both places are
GLOBAL_STATE_CODE(), so I don't think we actually need atomics to synchronise these two places. Maybe there are other accesses in iothreads, but then those should have been using atomics, too.
AFAICT, all accesses are either in a function with GLOBAL_STATE_CODE() directly or in a function with GRAPH_WRLOCK (in the cases of bdrv_replace_child_tran() and bdrv_replace_child_noperm()).
This does not change for the new accesses added by the series here.
Back then, there was no GLOBAL_STATE_CODE() macro yet, so I suppose this wasn't easy to verify.
Quoted 11 lines
Quoted 6 lines
The documentation in
include/block/block_int-common.hfor struct BlockDriverState also states:Quoted 2 lines
/* Accessed with atomic ops. */ int quiesce_counter;Should I rather add a patch to have the other readers use atomics too?
Either all accesses should use atomics or none of them. I'm not completely sure which way is the right one. Using atomics everywhere is the safe option, but I'm not sure if we ever access
quiesce_counteroutside of the main thread.
I'd add a patch dropping the atomic accesses and document that quiesce_counter can only be accessed in the main thread.
Best Regards,
Fiona
PATCH 3/11 block/snapshot: move drain outside of read-locked bdrv_snapshot_delete()
In reply to Fiona EbnerThis is in preparation to mark bdrv_drained_begin() as GRAPH_UNLOCKED.
More granular draining is not trivially possible, because bdrv_snapshot_delete() can recursively call itself.
--- block/snapshot.c | 18 ++++++++++++------ blockdev.c | 25 +++++++++++++++++-------- qemu-img.c | 2 ++ 3 files changed, 31 insertions(+), 14 deletions(-)
block/snapshot.c +12 -6
@@ -327,7 +327,7 @@ int bdrv_snapshot_goto(BlockDriverState *bs,/** * Delete an internal snapshot by @snapshot_id and @name. -* @bs: block device used in the operation +* @bs: block device used in the operation, needs to be drained * @snapshot_id: unique snapshot ID, or NULL * @name: snapshot name, or NULL * @errp: location to store error
@@ -356,6 +356,8 @@ int bdrv_snapshot_delete(BlockDriverState *bs,BlockDriverState *fallback_bs =bdrv_snapshot_fallback (bs);int ret; +assert (qatomic_read (&bs->quiesce_counter) >0 ); +GLOBAL_STATE_CODE ();if (!drv) {
@@ -368,9 +370,6 @@ int bdrv_snapshot_delete(BlockDriverState *bs,return -EINVAL ; } -/* drain all pending i/o before deleting snapshot */ -bdrv_drained_begin (bs); -if (drv->bdrv_snapshot_delete) { ret = drv->bdrv_snapshot_delete (bs, snapshot_id, name, errp); }else if (fallback_bs) {
@@ -382,7 +381,6 @@ int bdrv_snapshot_delete(BlockDriverState *bs, ret = -ENOTSUP ; } -bdrv_drained_end (bs);return ret; }
@@ -573,9 +571,13 @@ int bdrv_all_delete_snapshot(const char *name,GList *iterbdrvs;GLOBAL_STATE_CODE (); -GRAPH_RDLOCK_GUARD_MAINLOOP (); + +bdrv_drain_all_begin (); +bdrv_graph_rdlock_main_loop ();if (bdrv_all_get_snapshot_devices (has_devices, devices, &bdrvs, errp) <0 ) { +bdrv_graph_rdunlock_main_loop (); +bdrv_drain_all_end ();return -1 ; }
@@ -594,12 +596,16 @@ int bdrv_all_delete_snapshot(const char *name,if (ret <0 ) {error_prepend (errp,"Could not delete snapshot '%s' on '%s': " , name,bdrv_get_device_or_node_name (bs)); +bdrv_graph_rdunlock_main_loop (); +bdrv_drain_all_end ();return -1 ; } iterbdrvs = iterbdrvs->next; } +bdrv_graph_rdunlock_main_loop (); +bdrv_drain_all_end ();return 0 ; }
blockdev.c +17 -8
@@ -1132,39 +1132,41 @@ SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device,int ret;GLOBAL_STATE_CODE (); -GRAPH_RDLOCK_GUARD_MAINLOOP (); + +bdrv_drain_all_begin (); +bdrv_graph_rdlock_main_loop (); bs =qmp_get_root_bs (device, errp);if (!bs) { -return NULL ; + goto error; }if (!id && !name) {error_setg (errp,"Name or id must be provided" ); -return NULL ; + goto error; }if (bdrv_op_is_blocked (bs,BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE , errp)) { -return NULL ; + goto error; } ret =bdrv_snapshot_find_by_id_and_name (bs, id, name, &sn, &local_err);if (local_err) {error_propagate (errp, local_err); -return NULL ; + goto error; }if (!ret) {error_setg (errp,"Snapshot with id '%s' and name '%s' does not exist on " "device '%s'" ,STR_OR_NULL (id),STR_OR_NULL (name), device); -return NULL ; + goto error; }bdrv_snapshot_delete (bs, id, name, &local_err);if (local_err) {error_propagate (errp, local_err); -return NULL ; + goto error; } info =g_new0 (SnapshotInfo,1 );
@@ -1180,6 +1182,9 @@ SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device, info->has_icount = true; } +error: +bdrv_graph_rdunlock_main_loop (); +bdrv_drain_all_end ();return info; }
@@ -1295,12 +1300,14 @@ static void internal_snapshot_abort(void *opaque)Error *local_error =NULL ;GLOBAL_STATE_CODE (); -GRAPH_RDLOCK_GUARD_MAINLOOP ();if (!state->created) {return ; } +bdrv_drain_all_begin (); +bdrv_graph_rdlock_main_loop (); +if (bdrv_snapshot_delete (bs, sn->id_str, sn->name, &local_error) <0 ) {error_reportf_err (local_error, "Failed to delete snapshot with id '%s' and "
@@ -1308,6 +1315,8 @@ static void internal_snapshot_abort(void *opaque) sn->id_str, sn->name, bdrv_get_device_name(bs)); } +bdrv_graph_rdunlock_main_loop (); +bdrv_drain_all_end (); }static void internal_snapshot_clean (void *opaque)
qemu-img.c +2 -0
@@ -3505,6 +3505,7 @@ static int img_snapshot(int argc, char **argv)break ;case SNAPSHOT_DELETE : +bdrv_drain_all_begin ();bdrv_graph_rdlock_main_loop (); ret =bdrv_snapshot_find (bs, &sn, snapshot_name);if (ret <0 ) {
@@ -3520,6 +3521,7 @@ static int img_snapshot(int argc, char **argv) } }bdrv_graph_rdunlock_main_loop (); +bdrv_drain_all_end ();break ; }
Signature · 1 line
2.39.5
Re: [PATCH 03/11] block/snapshot: move drain outside of read-locked bdrv_snapshot_delete()
Am 08.05.2025 um 16:09 hat Fiona Ebner geschrieben:
Quoted 17 lines
This is in preparation to mark
bdrv_drained_begin()asGRAPH_UNLOCKED.More granular draining is not trivially possible, because
bdrv_snapshot_delete()can recursively call itself.Signed-off-by Fiona Ebner <[email protected]>--- block/snapshot.c | 18 ++++++++++++------ blockdev.c | 25 +++++++++++++++++-------- qemu-img.c | 2 ++ 3 files changed, 31 insertions(+), 14 deletions(-)block/snapshot.c +0
-0
block/snapshot.c int bdrv_snapshot_goto(BlockDriverState *bs, · line 330
Quoted 5 lines
block/snapshot.c +1
-1@@ -327,7 +327,7 @@ int bdrv_snapshot_goto(BlockDriverState *bs,/** * Delete an internal snapshot by @snapshot_id and @name. -* @bs: block device used in the operation +* @bs: block device used in the operation, needs to be drained
Forgot to add this piece of nitpicking on the previous patch: Other places say "must be drained", which I slightly prefer because of how RFC 2119 has "MUST", but not "NEEDS TO". Matter of taste, I guess, but if you agree, we could change it for the non-RFC series.
Quoted 34 lines
block/snapshot.c +2
-4* @snapshot_id: unique snapshot ID, or NULL * @name: snapshot name, or NULL * @errp: location to store error @@ -356,6 +356,8 @@ int bdrv_snapshot_delete(BlockDriverState *bs,BlockDriverState *fallback_bs =bdrv_snapshot_fallback (bs);int ret; +assert (qatomic_read (&bs->quiesce_counter) >0 ); +GLOBAL_STATE_CODE ();if (!drv) {@@ -368,9 +370,6 @@ int bdrv_snapshot_delete(BlockDriverState *bs,return -EINVAL ; } -/* drain all pending i/o before deleting snapshot */ -bdrv_drained_begin (bs); -if (drv->bdrv_snapshot_delete) { ret = drv->bdrv_snapshot_delete (bs, snapshot_id, name, errp); }else if (fallback_bs) {@@ -382,7 +381,6 @@ int bdrv_snapshot_delete(BlockDriverState *bs, ret = -ENOTSUP ; } -bdrv_drained_end (bs);return ret; }@@ -573,9 +571,13 @@ int bdrv_all_delete_snapshot(const char *name,GList *iterbdrvs;GLOBAL_STATE_CODE ();
block/snapshot.c int bdrv_all_delete_snapshot(const char *name, · line 582
Quoted 10 lines
block/snapshot.c +5
-1-GRAPH_RDLOCK_GUARD_MAINLOOP (); + +bdrv_drain_all_begin (); +bdrv_graph_rdlock_main_loop ();if (bdrv_all_get_snapshot_devices (has_devices, devices, &bdrvs, errp) <0 ) { +bdrv_graph_rdunlock_main_loop (); +bdrv_drain_all_end ();return -1 ; }
I think this wants to be changed into:
ret = bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp);
if (ret < 0) {
goto out;
}(Changing the return value from -1 to -errno is fine for the callers.)
block/snapshot.c int bdrv_all_delete_snapshot(const char *name, · line 602
Quoted 8 lines
block/snapshot.c +2
-0@@ -594,12 +596,16 @@ int bdrv_all_delete_snapshot(const char *name,if (ret <0 ) {error_prepend (errp,"Could not delete snapshot '%s' on '%s': " , name,bdrv_get_device_or_node_name (bs)); +bdrv_graph_rdunlock_main_loop (); +bdrv_drain_all_end ();return -1 ; }
Same here.
block/snapshot.c int bdrv_all_delete_snapshot(const char *name, · line 610
Quoted 8 lines
block/snapshot.c +2
-0iterbdrvs = iterbdrvs->next; } +bdrv_graph_rdunlock_main_loop (); +bdrv_drain_all_end ();return 0 ; }
Kevin
Re: [PATCH 03/11] block/snapshot: move drain outside of read-locked bdrv_snapshot_delete()
Am 14.05.25 um 18:52 schrieb Kevin Wolf:
Quoted 19 lines
Am 08.05.2025 um 16:09 hat Fiona Ebner geschrieben:
Quoted 18 lines
This is in preparation to mark
bdrv_drained_begin()asGRAPH_UNLOCKED.More granular draining is not trivially possible, because
bdrv_snapshot_delete()can recursively call itself.Signed-off-by Fiona Ebner <[email protected]>--- block/snapshot.c | 18 ++++++++++++------ blockdev.c | 25 +++++++++++++++++-------- qemu-img.c | 2 ++ 3 files changed, 31 insertions(+), 14 deletions(-)block/snapshot.c +0
-0@@ -327,7 +327,7 @@ int bdrv_snapshot_goto(BlockDriverState *bs,
Quoted 9 lines
block/snapshot.c int bdrv_snapshot_goto(BlockDriverState *bs, · line 330
Quoted 4 lines
block/snapshot.c +1
-1/** * Delete an internal snapshot by @snapshot_id and @name. -* @bs: block device used in the operation +* @bs: block device used in the operation, needs to be drained Forgot to add this piece of nitpicking on the previous patch: Other places say "must be drained", which I slightly prefer because of how RFC 2119 has "MUST", but not "NEEDS TO". Matter of taste, I guess, but if you agree, we could change it for the non-RFC series.
Sure, will do!
Quoted 24 lines
Quoted 4 lines
block/snapshot.c +0
-0@@ -573,9 +571,13 @@ int bdrv_all_delete_snapshot(const char *name,GList *iterbdrvs;GLOBAL_STATE_CODE ();block/snapshot.c int bdrv_all_delete_snapshot(const char *name, · line 582
Quoted 10 lines
block/snapshot.c +5
-1-GRAPH_RDLOCK_GUARD_MAINLOOP (); + +bdrv_drain_all_begin (); +bdrv_graph_rdlock_main_loop ();if (bdrv_all_get_snapshot_devices (has_devices, devices, &bdrvs, errp) <0 ) { +bdrv_graph_rdunlock_main_loop (); +bdrv_drain_all_end ();return -1 ; }I think this wants to be changed into:
ret = bdrv_all_get_snapshot_devices(has_devices, devices, &bdrvs, errp); if (ret < 0) { goto out; }(Changing the return value from -1 to -errno is fine for the callers.)
Quoted 10 lines
block/snapshot.c int bdrv_all_delete_snapshot(const char *name, · line 602
Quoted 8 lines
block/snapshot.c +2
-0@@ -594,12 +596,16 @@ int bdrv_all_delete_snapshot(const char *name,if (ret <0 ) {error_prepend (errp,"Could not delete snapshot '%s' on '%s': " , name,bdrv_get_device_or_node_name (bs)); +bdrv_graph_rdunlock_main_loop (); +bdrv_drain_all_end ();return -1 ; }Same here.
Ack.
Best Regards,
Fiona
PATCH 4/11 block: drain while unlocked in bdrv_reopen_parse_file_or_backing()
In reply to Fiona EbnerThis is in preparation to mark bdrv_drained_begin() as GRAPH_UNLOCKED.
--- block.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
block.c +3 -2
@@ -4805,10 +4805,11 @@ bdrv_reopen_parse_file_or_backing(BDRVReopenState *reopen_state,if (old_child_bs) {bdrv_ref (old_child_bs); +bdrv_graph_rdunlock_main_loop ();bdrv_drained_begin (old_child_bs); + }else { +bdrv_graph_rdunlock_main_loop (); } - -bdrv_graph_rdunlock_main_loop ();bdrv_graph_wrlock (); ret = bdrv_set_file_or_backing_noperm(bs, new_child_bs, is_backing,
Signature · 1 line
2.39.5
Re: [PATCH 04/11] block: drain while unlocked in bdrv_reopen_parse_file_or_backing()
Am 08.05.2025 um 16:09 hat Fiona Ebner geschrieben:
Quoted 12 lines
This is in preparation to mark
bdrv_drained_begin()asGRAPH_UNLOCKED.Signed-off-by Fiona Ebner <[email protected]>--- block.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)block.c +0
-0
block.c bdrv_reopen_parse_file_or_backing(BDRVReopenState *reopen_state, · line 4812
Quoted 8 lines
block.c +3
-0@@ -4805,10 +4805,11 @@ bdrv_reopen_parse_file_or_backing(BDRVReopenState *reopen_state,if (old_child_bs) {bdrv_ref (old_child_bs); +bdrv_graph_rdunlock_main_loop ();bdrv_drained_begin (old_child_bs); + }else { +bdrv_graph_rdunlock_main_loop (); }
This pattern is a bit ugly, so good that you get rid of it again later in the series.
block.c bdrv_reopen_parse_file_or_backing(BDRVReopenState *reopen_state, · line 4815
Quoted 5 lines
block.c +0
-2- -bdrv_graph_rdunlock_main_loop ();bdrv_graph_wrlock (); ret = bdrv_set_file_or_backing_noperm(bs, new_child_bs, is_backing,
PATCH 5/11 block: move drain outside of read-locked bdrv_inactivate_recurse()
In reply to Fiona EbnerThis is in preparation to mark bdrv_drained_begin() as GRAPH_UNLOCKED.
More granular draining is not trivially possible, because bdrv_inactivate_recurse() can recursively call itself.
--- block.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-)
block.c +15 -4
@@ -6991,6 +6991,8 @@ bdrv_inactivate_recurse(BlockDriverState *bs, bool top_level)int ret;uint64_t cumulative_perms, cumulative_shared_perms; +assert (qatomic_read (&bs->quiesce_counter) >0 ); +GLOBAL_STATE_CODE ();if (!bs->drv) {
@@ -7036,9 +7038,7 @@ bdrv_inactivate_recurse(BlockDriverState *bs, bool top_level)return -EPERM ; } -bdrv_drained_begin (bs); bs->open_flags |=BDRV_O_INACTIVE ; -bdrv_drained_end (bs);/* * Update permissions, they may differ for inactive nodes.
@@ -7063,14 +7063,20 @@ int bdrv_inactivate(BlockDriverState *bs, Error **errp)int ret;GLOBAL_STATE_CODE (); -GRAPH_RDLOCK_GUARD_MAINLOOP (); + +bdrv_drain_all_begin (); +bdrv_graph_rdlock_main_loop ();if (bdrv_has_bds_parent (bs, true)) {error_setg (errp,"Node has active parent node" ); +bdrv_graph_rdunlock_main_loop (); +bdrv_drain_all_end ();return -EPERM ; } ret =bdrv_inactivate_recurse (bs, true); +bdrv_graph_rdunlock_main_loop (); +bdrv_drain_all_end ();if (ret <0 ) {error_setg_errno (errp, -ret,"Failed to inactivate node" );return ret;
@@ -7086,7 +7092,9 @@ int bdrv_inactivate_all(void)int ret =0 ;GLOBAL_STATE_CODE (); -GRAPH_RDLOCK_GUARD_MAINLOOP (); + +bdrv_drain_all_begin (); +bdrv_graph_rdlock_main_loop ();for (bs =bdrv_first (&it); bs; bs =bdrv_next (&it)) {/* Nodes with BDS parents are covered by recursion from the last
@@ -7102,6 +7110,9 @@ int bdrv_inactivate_all(void) } } +bdrv_graph_rdunlock_main_loop (); +bdrv_drain_all_end (); +return ret; }
Signature · 1 line
2.39.5
Re: [PATCH 05/11] block: move drain outside of read-locked bdrv_inactivate_recurse()
Am 08.05.2025 um 16:09 hat Fiona Ebner geschrieben:
Quoted 45 lines
This is in preparation to mark
bdrv_drained_begin()asGRAPH_UNLOCKED.More granular draining is not trivially possible, because
bdrv_inactivate_recurse()can recursively call itself.Signed-off-by Fiona Ebner <[email protected]>--- block.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-)block.c +6
-3@@ -6991,6 +6991,8 @@ bdrv_inactivate_recurse(BlockDriverState *bs, bool top_level)int ret;uint64_t cumulative_perms, cumulative_shared_perms; +assert (qatomic_read (&bs->quiesce_counter) >0 ); +GLOBAL_STATE_CODE ();if (!bs->drv) {@@ -7036,9 +7038,7 @@ bdrv_inactivate_recurse(BlockDriverState *bs, bool top_level)return -EPERM ; } -bdrv_drained_begin (bs); bs->open_flags |=BDRV_O_INACTIVE ; -bdrv_drained_end (bs);/* * Update permissions, they may differ for inactive nodes. @@ -7063,14 +7063,20 @@ int bdrv_inactivate(BlockDriverState *bs, Error **errp)int ret;GLOBAL_STATE_CODE (); -GRAPH_RDLOCK_GUARD_MAINLOOP (); + +bdrv_drain_all_begin (); +bdrv_graph_rdlock_main_loop ();if (bdrv_has_bds_parent (bs, true)) {error_setg (errp,"Node has active parent node" ); +bdrv_graph_rdunlock_main_loop ();
block.c int bdrv_inactivate(BlockDriverState *bs, Error **errp) · line 7082
Quoted 10 lines
block.c +3
-0+bdrv_drain_all_end ();return -EPERM ; } ret =bdrv_inactivate_recurse (bs, true); +bdrv_graph_rdunlock_main_loop (); +bdrv_drain_all_end ();if (ret <0 ) {error_setg_errno (errp, -ret,"Failed to inactivate node" );return ret;
This might be another one that would become a bit clearer if you put the unlock/drain_end at the end and use a goto out for the error cases.
Kevin
PATCH 6/11 blockdev: drain while unlocked in internal_snapshot_action()
In reply to Fiona Ebner---
Could the bs associated to the device change because of polling when draining? If yes, does that mean we need to drain all in the beginning and not temporarily unlock?
blockdev.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
blockdev.c +5 -1
@@ -1216,7 +1216,7 @@ static void internal_snapshot_action(BlockdevSnapshotInternal *internal,int ret1;GLOBAL_STATE_CODE (); -GRAPH_RDLOCK_GUARD_MAINLOOP (); +bdrv_graph_rdlock_main_loop ();tran_add (tran, &internal_snapshot_drv, state);
@@ -1225,14 +1225,18 @@ static void internal_snapshot_action(BlockdevSnapshotInternal *internal, bs =qmp_get_root_bs (device, errp);if (!bs) { +bdrv_graph_rdunlock_main_loop ();return ; } state->bs = bs; +bdrv_graph_rdunlock_main_loop ();/* Paired with .clean() */ bdrv_drained_begin (bs); +GRAPH_RDLOCK_GUARD_MAINLOOP (); +if (bdrv_op_is_blocked (bs,BLOCK_OP_TYPE_INTERNAL_SNAPSHOT , errp)) {return ; }
Signature · 1 line
2.39.5
Re: [PATCH 06/11] blockdev: drain while unlocked in internal_snapshot_action()
Am 08.05.2025 um 16:09 hat Fiona Ebner geschrieben:
Quoted 6 lines
Signed-off-by Fiona Ebner <[email protected]>---
Could the bs associated to the device change because of polling when draining? If yes, does that mean we need to drain all in the beginning and not temporarily unlock?
I'm starting to hate this pattern. :-)
Maybe it would eventually be a good idea to have some helpers to deal with this common problem of "I want to drain all nodes that will be affected by the operation, but draining could change which nodes are part of it".
For this specific case, though, I think drain all is right simply because we may already rely on it: I suppose qmp_transaction() calls bdrv_drain_all() for a reason, but it should actually be a drain section. I'm pretty sure we're not just waiting for some requests to complete, but want to keep other users away. Maybe the graph lock actually covers whatever this drain was initially supposed to solve, but bdrv_drain_all() doesn't look right.
So how about making it bdrv_drain_all_begin/end() in qmp_transaction() just because that's the safe thing to do, and then we can just drop any individual drains in action implementations?
Kevin
Re: [PATCH 06/11] blockdev: drain while unlocked in internal_snapshot_action()
Am 14.05.25 um 19:26 schrieb Kevin Wolf:
Quoted 8 lines
Am 08.05.2025 um 16:09 hat Fiona Ebner geschrieben:
Quoted 6 lines
Signed-off-by Fiona Ebner <[email protected]>---
Could the bs associated to the device change because of polling when draining? If yes, does that mean we need to drain all in the beginning and not temporarily unlock?
Quoted 6 lines
I'm starting to hate this pattern. :-)
Maybe it would eventually be a good idea to have some helpers to deal with this common problem of "I want to drain all nodes that will be affected by the operation, but draining could change which nodes are part of it".
Yes, that would be nice :)
Quoted 11 lines
For this specific case, though, I think drain all is right simply because we may already rely on it: I suppose
qmp_transaction()callsbdrv_drain_all()for a reason, but it should actually be a drain section. I'm pretty sure we're not just waiting for some requests to complete, but want to keep other users away. Maybe the graph lock actually covers whatever this drain was initially supposed to solve, butbdrv_drain_all()doesn't look right.So how about making it bdrv_drain_all_begin/end() in
qmp_transaction()just because that's the safe thing to do, and then we can just drop any individual drains in action implementations?
Unfortunately, this does not work on its own. Among others, iotest 096 for qcow2 will hang, because bdrv_img_create() is called by external_snapshot_action() while drained and then qcow2_co_create() coroutine will hang in blk_co_do_pwritev_part()/blk_wait_while_drained(). So it seems having the more targeted drain here is important.
Would it be sensible to go with the "temporarily unlock" solution, but adding an extra check that the root bs of the device is still the one we drained after re-locking? And returning an error if not. At least for now, because the series is already getting quite big (in particular with the split of 09/11).
Best Regards,
Fiona
PATCH 7/11 blockdev: drain while unlocked in external_snapshot_action()
In reply to Fiona Ebner---
Could the bs associated to the device change because of polling when draining? If yes, does that mean we need to drain all in the beginning and not temporarily unlock?
blockdev.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
blockdev.c +4 -1
@@ -1368,7 +1368,7 @@ static void external_snapshot_action(TransactionAction *action,uint64_t perm, shared;/* TODO We'll eventually have to take a writer lock in this function */ -GRAPH_RDLOCK_GUARD_MAINLOOP (); +bdrv_graph_rdlock_main_loop ();tran_add (tran, &external_snapshot_drv, state);
@@ -1401,11 +1401,14 @@ static void external_snapshot_action(TransactionAction *action, state->old_bs =bdrv_lookup_bs (device, node_name, errp);if (!state->old_bs) { +bdrv_graph_rdunlock_main_loop ();return ; } +bdrv_graph_rdunlock_main_loop ();/* Paired with .clean() */ bdrv_drained_begin (state->old_bs); +GRAPH_RDLOCK_GUARD_MAINLOOP ();if (!bdrv_is_inserted (state->old_bs)) { error_setg(errp, "Device '%s' has no medium",
Signature · 1 line
2.39.5
PATCH 8/11 block: mark bdrv_drained_begin() as GRAPH_UNLOCKED
In reply to Fiona Ebnerbdrv_drained_begin() polls and is not allowed to be called with the block graph lock held. Mark the function as such.
---
This does not catch the issue reported by Andrey, because there is a bdrv_graph_rdunlock_main_loop() before bdrv_drained_begin() in the function bdrv_change_aio_context(). That unlock is of course ineffective if the exclusive lock is held, but it seems to prevent TSA from finiding the issue.
The next patch is concerned with that.
include/block/block-io.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
include/block/block-io.h +2 -1
@@ -429,7 +429,8 @@ bdrv_drain_poll(BlockDriverState *bs, BdrvChild *ignore_parent,* * This function can be recursive. */ -void bdrv_drained_begin (BlockDriverState *bs); +void GRAPH_UNLOCKED +bdrv_drained_begin (BlockDriverState *bs);/** * bdrv_do_drained_begin_quiesce:
Signature · 1 line
2.39.5
Re: [PATCH 08/11] block: mark bdrv_drained_begin() as GRAPH_UNLOCKED
Am 08.05.2025 um 16:09 hat Fiona Ebner geschrieben:
Quoted 5 lines
bdrv_drained_begin()polls and is not allowed to be called with the block graph lock held. Mark the function as such.Suggested-by Kevin Wolf <[email protected]>Signed-off-by Fiona Ebner <[email protected]>
Quoted 7 lines
---
This does not catch the issue reported by Andrey, because there is a
bdrv_graph_rdunlock_main_loop()beforebdrv_drained_begin()in the functionbdrv_change_aio_context(). That unlock is of course ineffective if the exclusive lock is held, but it seems to prevent TSA from finiding the issue.
Yes, GRAPH_UNLOCKED is weak. It doesn't actually prove that the graph is unlocked, but just flags direct callers that are known to hold the lock. So because bdrv_try_change_aio_context() and bdrv_change_aio_context() don't have TSA annotations, it misses the problem. If they had an annotation, it would find the problem. (If they were GRAPH_UNLOCKED, because they can't be called, or if they were GRAPH_RDLOCK/WRLOCK because of double locking.)
TSA does have negative requirements that would prove it, but that would essentially mean annotating everything in QEMU, even outside the block layer, down to main() as GRAPH_UNLOCKED. I don't think that's practical.
Hmm, or maybe it wouldn't be that bad if we could have something like assume_graph_unlocked()...? Not for this series anyway.
Quoted 10 lines
The next patch is concerned with that.
include/block/block-io.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)include/block/block-io.h +0
-0
include/block/block-io.h bdrv_drain_poll(BlockDriverState *bs, BdrvChild *ignore_parent, · line 433
Quoted 6 lines
include/block/block-io.h +2
-1@@ -429,7 +429,8 @@ bdrv_drain_poll(BlockDriverState *bs, BdrvChild *ignore_parent,* * This function can be recursive. */ -void bdrv_drained_begin (BlockDriverState *bs); +void GRAPH_UNLOCKED +bdrv_drained_begin (BlockDriverState *bs);
No need for the line break, it fits easily on a single line.
PATCH 9/11 block: move drain out of bdrv_change_aio_context()
In reply to Fiona EbnerWhile bdrv_drained_begin() is now marked as GRAPH_UNLOCKED, TSA is not able to catch a remaining instance where the function is called with the graph lock held in bdrv_change_aio_context(). This is because the call is preceded by a bdrv_graph_rdunlock_main_loop(), but for callers that hold the exclusive lock that does not actually release the lock.
In combination with block-stream, there is a deadlock that can happen because of this [0]. In particular, it can happen that
main thread IO thread
1. acquires write lock
in blk_co_do_preadv_part():
2. have non-zero blk->in_flight
3. try to acquire read lock
4. begin drain
Steps 3 and 4 might be switched. Draining will poll and get stuck, because it will see the non-zero in_flight counter. But the IO thread will not make any progress either, because it cannot acquire the read lock.
More granular draining is not trivially possible, because bdrv_change_aio_context() can recursively call itself e.g. via bdrv_child_change_aio_context().
[0]: https://lore.kernel.org/qemu-devel/[email protected]/
--- block.c | 91 ++++++++++++++++++++---------- block/backup.c | 2 + block/blklogwrites.c | 4 ++ block/blkverify.c | 2 + block/block-backend.c | 4 ++ block/commit.c | 4 ++ block/mirror.c | 5 ++ block/qcow2.c | 2 + block/quorum.c | 4 ++ block/replication.c | 7 +++ block/snapshot.c | 2 + block/stream.c | 10 ++-- block/vmdk.c | 10 ++++ blockdev.c | 17 ++++-- blockjob.c | 6 ++ include/block/block-global-state.h | 8 ++- tests/unit/test-bdrv-drain.c | 18 ++++++ tests/unit/test-bdrv-graph-mod.c | 10 ++++ 18 files changed, 164 insertions(+), 42 deletions(-)
block.c +60 -31
@@ -1720,12 +1720,14 @@ bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv, const char *node_name, open_failed: bs->drv =NULL ; +bdrv_drain_all_begin ();bdrv_graph_wrlock ();if (bs->file !=NULL ) {bdrv_unref_child (bs, bs->file);assert (!bs->file); }bdrv_graph_wrunlock (); +bdrv_drain_all_end ();g_free (bs->opaque); bs->opaque =NULL ;
@@ -3027,7 +3029,8 @@ static void GRAPH_WRLOCK bdrv_attach_child_common_abort(void *opaque)bdrv_replace_child_noperm (s->child,NULL );if (bdrv_get_aio_context (bs) != s->old_child_ctx) { -bdrv_try_change_aio_context (bs, s->old_child_ctx,NULL , &error_abort); +bdrv_try_change_aio_context_locked (bs, s->old_child_ctx,NULL , + &error_abort); }if (bdrv_child_get_parent_aio_context (s->child) != s->old_parent_ctx) {
@@ -3112,8 +3115,8 @@ bdrv_attach_child_common(BlockDriverState *child_bs, parent_ctx =bdrv_child_get_parent_aio_context (new_child);if (child_ctx != parent_ctx) {Error *local_err =NULL ; -int ret =bdrv_try_change_aio_context (child_bs, parent_ctx,NULL , - &local_err); +int ret =bdrv_try_change_aio_context_locked (child_bs, parent_ctx,NULL , + &local_err);if (ret <0 && child_class->change_aio_ctx) {Transaction *aio_ctx_tran =tran_new ();
@@ -3314,8 +3317,8 @@ void bdrv_root_unref_child(BdrvChild *child)* When the parent requiring a non-default AioContext is removed, the * node moves back to the main AioContext */ -bdrv_try_change_aio_context (child_bs,qemu_get_aio_context (),NULL , -NULL ); +bdrv_try_change_aio_context_locked (child_bs,qemu_get_aio_context (), +NULL ,NULL ); }bdrv_schedule_unref (child_bs);
@@ -3584,11 +3587,11 @@ int bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd,bdrv_graph_rdunlock_main_loop ();bdrv_ref (drain_bs); -bdrv_drained_begin (drain_bs); +bdrv_drain_all_begin ();bdrv_graph_wrlock (); ret =bdrv_set_backing_hd_drained (bs, backing_hd, errp);bdrv_graph_wrunlock (); -bdrv_drained_end (drain_bs); +bdrv_drain_all_end ();bdrv_unref (drain_bs);return ret;
@@ -3780,10 +3783,12 @@ static BdrvChild *bdrv_open_child_common(const char *filename,return NULL ; } +bdrv_drain_all_begin ();bdrv_graph_wrlock (); child =bdrv_attach_child (parent, bs, bdref_key, child_class, child_role, errp);bdrv_graph_wrunlock (); +bdrv_drain_all_end ();return child; }
@@ -4805,20 +4810,19 @@ bdrv_reopen_parse_file_or_backing(BDRVReopenState *reopen_state,if (old_child_bs) {bdrv_ref (old_child_bs); -bdrv_graph_rdunlock_main_loop (); -bdrv_drained_begin (old_child_bs); - }else { -bdrv_graph_rdunlock_main_loop (); } + +bdrv_graph_rdunlock_main_loop (); +bdrv_drain_all_begin ();bdrv_graph_wrlock (); ret =bdrv_set_file_or_backing_noperm (bs, new_child_bs, is_backing, tran, errp);bdrv_graph_wrunlock (); +bdrv_drain_all_end ();if (old_child_bs) { -bdrv_drained_end (old_child_bs);bdrv_unref (old_child_bs); }
@@ -5160,6 +5164,7 @@ static void bdrv_close(BlockDriverState *bs) bs->drv =NULL ; } +bdrv_drain_all_begin ();bdrv_graph_wrlock (); QLIST_FOREACH_SAFE(child, &bs->children, next, next) {bdrv_unref_child (bs, child);
@@ -5168,6 +5173,7 @@ static void bdrv_close(BlockDriverState *bs)assert (!bs->backing);assert (!bs->file);bdrv_graph_wrunlock (); +bdrv_drain_all_end ();g_free (bs->opaque); bs->opaque =NULL ;
@@ -5493,9 +5499,7 @@ int bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top,assert (!bs_new->backing);bdrv_graph_rdunlock_main_loop (); -bdrv_drained_begin (bs_top); -bdrv_drained_begin (bs_new); - +bdrv_drain_all_begin ();bdrv_graph_wrlock (); child = bdrv_attach_child_noperm(bs_new, bs_top, "backing",
@@ -5517,9 +5521,7 @@ out:bdrv_refresh_limits (bs_top,NULL ,NULL );bdrv_graph_wrunlock (); - -bdrv_drained_end (bs_top); -bdrv_drained_end (bs_new); +bdrv_drain_all_end ();return ret; }
@@ -7626,10 +7628,6 @@ bool bdrv_child_change_aio_context(BdrvChild *c, AioContext *ctx,static void bdrv_set_aio_context_clean (void *opaque) {BdrvStateSetAioContext *state = (BdrvStateSetAioContext *) opaque; -BlockDriverState *bs = (BlockDriverState *) state->bs; - -/* Paired with bdrv_drained_begin in bdrv_change_aio_context() */ -bdrv_drained_end (bs);g_free (state); }
@@ -7693,8 +7691,7 @@ static bool bdrv_change_aio_context(BlockDriverState *bs, AioContext *ctx, .bs = bs, }; -/* Paired with bdrv_drained_end in bdrv_set_aio_context_clean() */ -bdrv_drained_begin (bs); +assert (qatomic_read (&bs->quiesce_counter) >0 );tran_add (tran, &set_aio_context, state);
@@ -7702,14 +7699,12 @@ static bool bdrv_change_aio_context(BlockDriverState *bs, AioContext *ctx, }/* -* Change bs's and recursively all of its parents' and children's AioContext -* to the given new context, returning an error if that isn't possible. -* -* If ignore_child is not NULL, that child (and its subgraph) will not -* be touched. +* Use bdrv_try_change_aio_context() or bdrv_try_change_aio_context_locked(). */ -int bdrv_try_change_aio_context (BlockDriverState *bs,AioContext *ctx, -BdrvChild *ignore_child,Error **errp) +static int bdrv_try_change_aio_context_common (BlockDriverState *bs, +AioContext *ctx, +BdrvChild *ignore_child, +Error **errp) {Transaction *tran;GHashTable *visited;
@@ -7747,6 +7742,40 @@ int bdrv_try_change_aio_context(BlockDriverState *bs, AioContext *ctx,return 0 ; } +/* +* Change bs's and recursively all of its parents' and children's AioContext +* to the given new context, returning an error if that isn't possible. +* +* If ignore_child is not NULL, that child (and its subgraph) will not +* be touched. +*/ +int bdrv_try_change_aio_context (BlockDriverState *bs,AioContext *ctx, +BdrvChild *ignore_child,Error **errp) +{ +int ret; +bdrv_drain_all_begin (); + ret =bdrv_try_change_aio_context_common (bs, ctx, ignore_child, errp); +bdrv_drain_all_end (); +return ret; +} + +/* +* Change bs's and recursively all of its parents' and children's AioContext +* to the given new context, returning an error if that isn't possible. +* +* If ignore_child is not NULL, that child (and its subgraph) will not +* be touched. +* +* Called with the graph lock held. +* +* Called while all bs are drained. +*/ +int bdrv_try_change_aio_context_locked (BlockDriverState *bs,AioContext *ctx, +BdrvChild *ignore_child,Error **errp) +{ +return bdrv_try_change_aio_context_common (bs, ctx, ignore_child, errp); +} +void bdrv_add_aio_context_notifier (BlockDriverState *bs,void (*attached_aio_context)(AioContext *new_context,void *opaque),void (*detach_aio_context)(void *opaque),void *opaque)
block/backup.c +2 -0
@@ -497,10 +497,12 @@ BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs,block_copy_set_speed (bcs, speed);/* Required permissions are taken by copy-before-write filter target */ +bdrv_drain_all_begin ();bdrv_graph_wrlock ();block_job_add_bdrv (&job->common,"target" , target,0 ,BLK_PERM_ALL , &error_abort);bdrv_graph_wrunlock (); +bdrv_drain_all_end ();return &job->common;
block/blklogwrites.c +4 -0
@@ -281,9 +281,11 @@ static int blk_log_writes_open(BlockDriverState *bs, QDict *options, int flags, ret =0 ; fail_log:if (ret <0 ) { +bdrv_drain_all_begin ();bdrv_graph_wrlock ();bdrv_unref_child (bs, s->log_file);bdrv_graph_wrunlock (); +bdrv_drain_all_end (); s->log_file =NULL ;qemu_mutex_destroy (&s->mutex); }
@@ -296,10 +298,12 @@ static void blk_log_writes_close(BlockDriverState *bs) {BDRVBlkLogWritesState *s = bs->opaque; +bdrv_drain_all_begin ();bdrv_graph_wrlock ();bdrv_unref_child (bs, s->log_file); s->log_file =NULL ;bdrv_graph_wrunlock (); +bdrv_drain_all_end ();qemu_mutex_destroy (&s->mutex); }
block/blkverify.c +2 -0
@@ -151,10 +151,12 @@ static void blkverify_close(BlockDriverState *bs) {BDRVBlkverifyState *s = bs->opaque; +bdrv_drain_all_begin ();bdrv_graph_wrlock ();bdrv_unref_child (bs, s->test_file); s->test_file =NULL ;bdrv_graph_wrunlock (); +bdrv_drain_all_end (); } static int64_t coroutine_fn GRAPH_RDLOCK
block/block-backend.c +4 -0
@@ -889,9 +889,11 @@ void blk_remove_bs(BlockBackend *blk) root = blk->root; blk->root =NULL ; +bdrv_drain_all_begin ();bdrv_graph_wrlock ();bdrv_root_unref_child (root);bdrv_graph_wrunlock (); +bdrv_drain_all_end (); }/*
@@ -904,6 +906,7 @@ int blk_insert_bs(BlockBackend *blk, BlockDriverState *bs, Error **errp)GLOBAL_STATE_CODE ();bdrv_ref (bs); +bdrv_drain_all_begin ();bdrv_graph_wrlock ();if ((bs->open_flags &BDRV_O_INACTIVE ) &&blk_can_inactivate (blk)) {
@@ -919,6 +922,7 @@ int blk_insert_bs(BlockBackend *blk, BlockDriverState *bs, Error **errp)BDRV_CHILD_FILTERED |BDRV_CHILD_PRIMARY , perm, shared_perm, blk, errp);bdrv_graph_wrunlock (); +bdrv_drain_all_end ();if (blk->root ==NULL ) {return -EPERM ; }
block/commit.c +4 -0
@@ -342,6 +342,7 @@ void commit_start(const char *job_id, BlockDriverState *bs,* this is the responsibility of the interface (i.e. whoever calls * commit_start()). */ +bdrv_drain_all_begin ();bdrv_graph_wrlock (); s->base_overlay =bdrv_find_overlay (top, base);assert (s->base_overlay);
@@ -374,18 +375,21 @@ void commit_start(const char *job_id, BlockDriverState *bs, iter_shared_perms, errp);if (ret <0 ) {bdrv_graph_wrunlock (); +bdrv_drain_all_end (); goto fail; } }if (bdrv_freeze_backing_chain (commit_top_bs, base, errp) <0 ) {bdrv_graph_wrunlock (); +bdrv_drain_all_end (); goto fail; } s->chain_frozen = true; ret =block_job_add_bdrv (&s->common,"base" , base,0 ,BLK_PERM_ALL , errp);bdrv_graph_wrunlock (); +bdrv_drain_all_end ();if (ret <0 ) { goto fail;
block/mirror.c +5 -0
@@ -1904,6 +1904,7 @@ static BlockJob *mirror_start_job(*/ bdrv_disable_dirty_bitmap (s->dirty_bitmap); +bdrv_drain_all_begin ();bdrv_graph_wrlock (); ret = block_job_add_bdrv(&s->common, "source", bs, 0, BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE |
@@ -1911,6 +1912,7 @@ static BlockJob *mirror_start_job( errp);if (ret <0 ) {bdrv_graph_wrunlock (); +bdrv_drain_all_end (); goto fail; }
@@ -1956,16 +1958,19 @@ static BlockJob *mirror_start_job( iter_shared_perms, errp);if (ret <0 ) {bdrv_graph_wrunlock (); +bdrv_drain_all_end (); goto fail; } }if (bdrv_freeze_backing_chain (mirror_top_bs, target, errp) <0 ) {bdrv_graph_wrunlock (); +bdrv_drain_all_end (); goto fail; } }bdrv_graph_wrunlock (); +bdrv_drain_all_end ();QTAILQ_INIT (&s->ops_in_flight);
block/qcow2.c +2 -0
@@ -2821,9 +2821,11 @@ qcow2_do_close(BlockDriverState *bs, bool close_data_file)if (close_data_file &&has_data_file (bs)) {GLOBAL_STATE_CODE ();bdrv_graph_rdunlock_main_loop (); +bdrv_drain_all_begin ();bdrv_graph_wrlock ();bdrv_unref_child (bs, s->data_file);bdrv_graph_wrunlock (); +bdrv_drain_all_end (); s->data_file =NULL ;bdrv_graph_rdlock_main_loop (); }
block/quorum.c +4 -0
@@ -1037,6 +1037,7 @@ static int quorum_open(BlockDriverState *bs, QDict *options, int flags, close_exit:/* cleanup on error */ +bdrv_drain_all_begin ();bdrv_graph_wrlock ();for (i =0 ; i < s->num_children; i++) {if (!opened[i]) {
@@ -1045,6 +1046,7 @@ close_exit:bdrv_unref_child (bs, s->children[i]); }bdrv_graph_wrunlock (); +bdrv_drain_all_end ();g_free (s->children);g_free (opened); exit:
@@ -1057,11 +1059,13 @@ static void quorum_close(BlockDriverState *bs)BDRVQuorumState *s = bs->opaque;int i; +bdrv_drain_all_begin ();bdrv_graph_wrlock ();for (i =0 ; i < s->num_children; i++) {bdrv_unref_child (bs, s->children[i]); }bdrv_graph_wrunlock (); +bdrv_drain_all_end ();g_free (s->children); }
block/replication.c +7 -0
@@ -540,6 +540,7 @@ static void replication_start(ReplicationState *rs, ReplicationMode mode,return ; } +bdrv_drain_all_begin ();bdrv_graph_wrlock ();bdrv_ref (hidden_disk->bs);
@@ -549,6 +550,7 @@ static void replication_start(ReplicationState *rs, ReplicationMode mode,if (local_err) {error_propagate (errp, local_err);bdrv_graph_wrunlock (); +bdrv_drain_all_end ();return ; }
@@ -559,6 +561,7 @@ static void replication_start(ReplicationState *rs, ReplicationMode mode,if (local_err) {error_propagate (errp, local_err);bdrv_graph_wrunlock (); +bdrv_drain_all_end ();return ; }
@@ -571,12 +574,14 @@ static void replication_start(ReplicationState *rs, ReplicationMode mode, !check_top_bs(top_bs, bs)) {error_setg (errp,"No top_bs or it is invalid" );bdrv_graph_wrunlock (); +bdrv_drain_all_end ();reopen_backing_file (bs, false,NULL );return ; }bdrv_op_block_all (top_bs, s->blocker);bdrv_graph_wrunlock (); +bdrv_drain_all_end (); s->backup_job = backup_job_create( NULL, s->secondary_disk->bs, s->hidden_disk->bs,
@@ -649,12 +654,14 @@ static void replication_done(void *opaque, int ret)if (ret ==0 ) { s->stage =BLOCK_REPLICATION_DONE ; +bdrv_drain_all_begin ();bdrv_graph_wrlock ();bdrv_unref_child (bs, s->secondary_disk); s->secondary_disk =NULL ;bdrv_unref_child (bs, s->hidden_disk); s->hidden_disk =NULL ;bdrv_graph_wrunlock (); +bdrv_drain_all_end (); s->error =0 ; }else {
block/snapshot.c +2 -0
@@ -291,9 +291,11 @@ int bdrv_snapshot_goto(BlockDriverState *bs, }/* .bdrv_open() will re-attach it */ +bdrv_drain_all_begin ();bdrv_graph_wrlock ();bdrv_unref_child (bs, fallback);bdrv_graph_wrunlock (); +bdrv_drain_all_end (); ret =bdrv_snapshot_goto (fallback_bs, snapshot_id, errp);memset (bs->opaque,0 , drv->instance_size);
block/stream.c +6 -4
@@ -80,10 +80,9 @@ static int stream_prepare(Job *job)* 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); +bdrv_drain_all_begin ();if (unfiltered_bs_cow) {bdrv_ref (unfiltered_bs_cow); -bdrv_drained_begin (unfiltered_bs_cow); }bdrv_graph_rdlock_main_loop ();
@@ -124,10 +123,9 @@ static int stream_prepare(Job *job) out:if (unfiltered_bs_cow) { -bdrv_drained_end (unfiltered_bs_cow);bdrv_unref (unfiltered_bs_cow); } -bdrv_drained_end (unfiltered_bs); +bdrv_drain_all_end ();return ret; }
@@ -373,10 +371,12 @@ void stream_start(const char *job_id, BlockDriverState *bs,* already have our own plans. Also don't allow resize as the image size is * queried only at the job start and then cached. */ +bdrv_drain_all_begin ();bdrv_graph_wrlock ();if (block_job_add_bdrv (&s->common,"active node" , bs,0 , basic_flags |BLK_PERM_WRITE , errp)) {bdrv_graph_wrunlock (); +bdrv_drain_all_end (); goto fail; }
@@ -397,10 +397,12 @@ void stream_start(const char *job_id, BlockDriverState *bs, basic_flags, errp);if (ret <0 ) {bdrv_graph_wrunlock (); +bdrv_drain_all_end (); goto fail; } }bdrv_graph_wrunlock (); +bdrv_drain_all_end (); s->base_overlay = base_overlay; s->above_base = above_base;
block/vmdk.c +10 -0
@@ -271,6 +271,7 @@ static void vmdk_free_extents(BlockDriverState *bs)BDRVVmdkState *s = bs->opaque;VmdkExtent *e; +bdrv_drain_all_begin ();bdrv_graph_wrlock ();for (i =0 ; i < s->num_extents; i++) { e = &s->extents[i];
@@ -283,6 +284,7 @@ static void vmdk_free_extents(BlockDriverState *bs) } }bdrv_graph_wrunlock (); +bdrv_drain_all_end ();g_free (s->extents); }
@@ -1247,9 +1249,11 @@ vmdk_parse_extents(const char *desc, BlockDriverState *bs, QDict *options, 0, 0, 0, 0, 0, &extent, errp);if (ret <0 ) {bdrv_graph_rdunlock_main_loop (); +bdrv_drain_all_begin ();bdrv_graph_wrlock ();bdrv_unref_child (bs, extent_file);bdrv_graph_wrunlock (); +bdrv_drain_all_end ();bdrv_graph_rdlock_main_loop (); goto out; }
@@ -1266,9 +1270,11 @@ vmdk_parse_extents(const char *desc, BlockDriverState *bs, QDict *options,g_free (buf);if (ret) {bdrv_graph_rdunlock_main_loop (); +bdrv_drain_all_begin ();bdrv_graph_wrlock ();bdrv_unref_child (bs, extent_file);bdrv_graph_wrunlock (); +bdrv_drain_all_end ();bdrv_graph_rdlock_main_loop (); goto out; }
@@ -1277,9 +1283,11 @@ vmdk_parse_extents(const char *desc, BlockDriverState *bs, QDict *options, ret =vmdk_open_se_sparse (bs, extent_file, bs->open_flags, errp);if (ret) {bdrv_graph_rdunlock_main_loop (); +bdrv_drain_all_begin ();bdrv_graph_wrlock ();bdrv_unref_child (bs, extent_file);bdrv_graph_wrunlock (); +bdrv_drain_all_end ();bdrv_graph_rdlock_main_loop (); goto out; }
@@ -1287,9 +1295,11 @@ vmdk_parse_extents(const char *desc, BlockDriverState *bs, QDict *options, }else {error_setg (errp,"Unsupported extent type '%s'" , type);bdrv_graph_rdunlock_main_loop (); +bdrv_drain_all_begin ();bdrv_graph_wrlock ();bdrv_unref_child (bs, extent_file);bdrv_graph_wrunlock (); +bdrv_drain_all_end ();bdrv_graph_rdlock_main_loop (); ret = -ENOTSUP ; goto out;
blockdev.c +12 -5
@@ -3539,6 +3539,7 @@ void qmp_x_blockdev_change(const char *parent, const char *child,BlockDriverState *parent_bs, *new_bs =NULL ;BdrvChild *p_child; +bdrv_drain_all_begin ();bdrv_graph_wrlock (); parent_bs =bdrv_lookup_bs (parent, parent, errp);
@@ -3576,6 +3577,7 @@ void qmp_x_blockdev_change(const char *parent, const char *child, out:bdrv_graph_wrunlock (); +bdrv_drain_all_end (); }BlockJobInfoList *qmp_query_block_jobs (Error **errp)
@@ -3609,12 +3611,13 @@ void qmp_x_blockdev_set_iothread(const char *node_name, StrOrNull *iothread,AioContext *new_context;BlockDriverState *bs; -GRAPH_RDLOCK_GUARD_MAINLOOP (); +bdrv_drain_all_begin (); +bdrv_graph_rdlock_main_loop (); bs =bdrv_find_node (node_name);if (!bs) {error_setg (errp,"Failed to find node with node-name='%s'" , node_name); -return ; + goto out; }/* Protects against accidents. */
@@ -3622,14 +3625,14 @@ void qmp_x_blockdev_set_iothread(const char *node_name, StrOrNull *iothread,error_setg (errp,"Node %s is associated with a BlockBackend and could " "be in use (use force=true to override this check)" , node_name); -return ; + goto out; }if (iothread->type ==QTYPE_QSTRING ) {IOThread *obj =iothread_by_id (iothread->u.s);if (!obj) {error_setg (errp,"Cannot find iothread %s" , iothread->u.s); -return ; + goto out; } new_context =iothread_get_aio_context (obj);
@@ -3637,7 +3640,11 @@ void qmp_x_blockdev_set_iothread(const char *node_name, StrOrNull *iothread, new_context =qemu_get_aio_context (); } -bdrv_try_change_aio_context (bs, new_context,NULL , errp); +bdrv_try_change_aio_context_locked (bs, new_context,NULL , errp); + +out: +bdrv_graph_rdunlock_main_loop (); +bdrv_drain_all_end (); }QemuOptsList qemu_common_drive_opts = {
blockjob.c +6 -0
@@ -198,6 +198,7 @@ void block_job_remove_all_bdrv(BlockJob *job)* one to make sure that such a concurrent access does not attempt * to process an already freed BdrvChild. */ +bdrv_drain_all_begin ();bdrv_graph_wrlock ();while (job->nodes) {GSList *l = job->nodes;
@@ -211,6 +212,7 @@ void block_job_remove_all_bdrv(BlockJob *job)g_slist_free_1 (l); }bdrv_graph_wrunlock (); +bdrv_drain_all_end (); }bool block_job_has_bdrv (BlockJob *job,BlockDriverState *bs)
@@ -496,6 +498,7 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,int ret;GLOBAL_STATE_CODE (); +bdrv_drain_all_begin ();bdrv_graph_wrlock ();if (job_id ==NULL && !(flags &JOB_INTERNAL )) {
@@ -506,6 +509,7 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver, flags, cb, opaque, errp);if (job ==NULL ) {bdrv_graph_wrunlock (); +bdrv_drain_all_end ();return NULL ; }
@@ -544,10 +548,12 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver, }bdrv_graph_wrunlock (); +bdrv_drain_all_end ();return job; fail:bdrv_graph_wrunlock (); +bdrv_drain_all_end ();job_early_fail (&job->job);return NULL ; }
include/block/block-global-state.h +6 -2
@@ -277,8 +277,12 @@ bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag);bool bdrv_child_change_aio_context (BdrvChild *c,AioContext *ctx,GHashTable *visited,Transaction *tran,Error **errp); -int bdrv_try_change_aio_context (BlockDriverState *bs,AioContext *ctx, -BdrvChild *ignore_child,Error **errp); +int GRAPH_UNLOCKED +bdrv_try_change_aio_context (BlockDriverState *bs, AioContext *ctx, + BdrvChild *ignore_child, Error **errp); +int GRAPH_RDLOCK +bdrv_try_change_aio_context_locked (BlockDriverState *bs, AioContext *ctx, + BdrvChild *ignore_child, Error **errp); int GRAPH_RDLOCK bdrv_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz);int bdrv_probe_geometry (BlockDriverState *bs,HDGeometry *geo);
tests/unit/test-bdrv-drain.c +18 -0
@@ -772,9 +772,11 @@ static void test_blockjob_common_drain_node(enum drain_type drain_type, tjob->bs = src; job = &tjob->common; +bdrv_drain_all_begin ();bdrv_graph_wrlock ();block_job_add_bdrv (job,"target" , target,0 ,BLK_PERM_ALL , &error_abort);bdrv_graph_wrunlock (); +bdrv_drain_all_end ();switch (result) {case TEST_JOB_SUCCESS :
@@ -953,11 +955,13 @@ static void bdrv_test_top_close(BlockDriverState *bs) {BdrvChild *c, *next_c; +bdrv_drain_all_begin ();bdrv_graph_wrlock (); QLIST_FOREACH_SAFE(c, &bs->children, next, next_c) {bdrv_unref_child (bs, c); }bdrv_graph_wrunlock (); +bdrv_drain_all_end (); } static int coroutine_fn GRAPH_RDLOCK
@@ -1047,10 +1051,12 @@ static void do_test_delete_by_drain(bool detach_instead_of_delete, null_bs =bdrv_open ("null-co://" ,NULL ,NULL ,BDRV_O_RDWR |BDRV_O_PROTOCOL , &error_abort); +bdrv_drain_all_begin ();bdrv_graph_wrlock ();bdrv_attach_child (bs, null_bs,"null-child" , &child_of_bds,BDRV_CHILD_DATA , &error_abort);bdrv_graph_wrunlock (); +bdrv_drain_all_end ();/* This child will be the one to pass to requests through to, and * it will stall until a drain occurs */
@@ -1058,21 +1064,25 @@ static void do_test_delete_by_drain(bool detach_instead_of_delete, &error_abort); child_bs->total_sectors =65536 >>BDRV_SECTOR_BITS ;/* Takes our reference to child_bs */ +bdrv_drain_all_begin ();bdrv_graph_wrlock (); tts->wait_child =bdrv_attach_child (bs, child_bs,"wait-child" , &child_of_bds,BDRV_CHILD_DATA |BDRV_CHILD_PRIMARY , &error_abort);bdrv_graph_wrunlock (); +bdrv_drain_all_end ();/* This child is just there to be deleted * (for detach_instead_of_delete == true) */ null_bs =bdrv_open ("null-co://" ,NULL ,NULL ,BDRV_O_RDWR |BDRV_O_PROTOCOL , &error_abort); +bdrv_drain_all_begin ();bdrv_graph_wrlock ();bdrv_attach_child (bs, null_bs,"null-child" , &child_of_bds,BDRV_CHILD_DATA , &error_abort);bdrv_graph_wrunlock (); +bdrv_drain_all_end (); blk =blk_new (qemu_get_aio_context (),BLK_PERM_ALL ,BLK_PERM_ALL );blk_insert_bs (blk, bs, &error_abort);
@@ -1155,6 +1165,7 @@ static void no_coroutine_fn detach_indirect_bh(void *opaque)bdrv_dec_in_flight (data->child_b->bs); +bdrv_drain_all_begin ();bdrv_graph_wrlock ();bdrv_unref_child (data->parent_b, data->child_b);
@@ -1163,6 +1174,7 @@ static void no_coroutine_fn detach_indirect_bh(void *opaque) &child_of_bds,BDRV_CHILD_DATA , &error_abort);bdrv_graph_wrunlock (); +bdrv_drain_all_end (); } static void coroutine_mixed_fn detach_by_parent_aio_cb(void *opaque, int ret)
@@ -1260,6 +1272,7 @@ static void TSA_NO_TSA test_detach_indirect(bool by_parent_cb)/* Set child relationships */ bdrv_ref (b);bdrv_ref (a); +bdrv_drain_all_begin ();bdrv_graph_wrlock (); child_b =bdrv_attach_child (parent_b, b,"PB-B" , &child_of_bds,BDRV_CHILD_DATA , &error_abort);
@@ -1271,6 +1284,7 @@ static void TSA_NO_TSA test_detach_indirect(bool by_parent_cb) by_parent_cb ? &child_of_bds : &detach_by_driver_cb_class, BDRV_CHILD_DATA, &error_abort);bdrv_graph_wrunlock (); +bdrv_drain_all_end (); g_assert_cmpint(parent_a->refcnt, ==, 1); g_assert_cmpint(parent_b->refcnt, ==, 1);
@@ -1687,6 +1701,7 @@ static void test_drop_intermediate_poll(void)* Establish the chain last, so the chain links are the first * elements in the BDS.parents lists */ +bdrv_drain_all_begin ();bdrv_graph_wrlock ();for (i =0 ; i <3 ; i++) {if (i) {
@@ -1696,6 +1711,7 @@ static void test_drop_intermediate_poll(void) } }bdrv_graph_wrunlock (); +bdrv_drain_all_end (); job =block_job_create ("job" , &test_simple_job_driver,NULL , job_node,0 ,BLK_PERM_ALL ,0 ,0 ,NULL ,NULL , &error_abort);
@@ -1942,10 +1958,12 @@ static void do_test_replace_child_mid_drain(int old_drain_count, new_child_bs->total_sectors =1 ;bdrv_ref (old_child_bs); +bdrv_drain_all_begin ();bdrv_graph_wrlock ();bdrv_attach_child (parent_bs, old_child_bs,"child" , &child_of_bds,BDRV_CHILD_COW , &error_abort);bdrv_graph_wrunlock (); +bdrv_drain_all_end (); parent_s->setup_completed = true;for (i =0 ; i < old_drain_count; i++) {
tests/unit/test-bdrv-graph-mod.c +10 -0
@@ -137,10 +137,12 @@ static void test_update_perm_tree(void)blk_insert_bs (root, bs, &error_abort); +bdrv_drain_all_begin ();bdrv_graph_wrlock ();bdrv_attach_child (filter, bs,"child" , &child_of_bds,BDRV_CHILD_DATA , &error_abort);bdrv_graph_wrunlock (); +bdrv_drain_all_end (); ret =bdrv_append (filter, bs,NULL ); g_assert_cmpint(ret, <, 0);
@@ -204,11 +206,13 @@ static void test_should_update_child(void)bdrv_set_backing_hd (target, bs, &error_abort); +bdrv_drain_all_begin ();bdrv_graph_wrlock ();g_assert (target->backing->bs == bs);bdrv_attach_child (filter, target,"target" , &child_of_bds,BDRV_CHILD_DATA , &error_abort);bdrv_graph_wrunlock (); +bdrv_drain_all_end ();bdrv_append (filter, bs, &error_abort);bdrv_graph_rdlock_main_loop ();
@@ -244,6 +248,7 @@ static void test_parallel_exclusive_write(void)bdrv_ref (base);bdrv_ref (fl1); +bdrv_drain_all_begin ();bdrv_graph_wrlock (); bdrv_attach_child(top, fl1, "backing", &child_of_bds, BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY,
@@ -257,6 +262,7 @@ static void test_parallel_exclusive_write(void)bdrv_replace_node (fl1, fl2, &error_abort);bdrv_graph_wrunlock (); +bdrv_drain_all_end ();bdrv_drained_end (fl2);bdrv_drained_end (fl1);
@@ -363,6 +369,7 @@ static void test_parallel_perm_update(void)*/ bdrv_ref (base); +bdrv_drain_all_begin ();bdrv_graph_wrlock ();bdrv_attach_child (top, ws,"file" , &child_of_bds,BDRV_CHILD_DATA , &error_abort);
@@ -377,6 +384,7 @@ static void test_parallel_perm_update(void)BDRV_CHILD_FILTERED |BDRV_CHILD_PRIMARY , &error_abort);bdrv_graph_wrunlock (); +bdrv_drain_all_end ();/* Select fl1 as first child to be active */ s->selected = c_fl1;
@@ -430,11 +438,13 @@ static void test_append_greedy_filter(void)BlockDriverState *base =no_perm_node ("base" );BlockDriverState *fl =exclusive_writer_node ("fl1" ); +bdrv_drain_all_begin ();bdrv_graph_wrlock ();bdrv_attach_child (top, base,"backing" , &child_of_bds,BDRV_CHILD_FILTERED |BDRV_CHILD_PRIMARY , &error_abort);bdrv_graph_wrunlock (); +bdrv_drain_all_end ();bdrv_append (fl, base, &error_abort);bdrv_unref (fl);
Signature · 1 line
2.39.5
Re: [PATCH 09/11] block: move drain out of bdrv_change_aio_context()
Am 08.05.2025 um 16:09 hat Fiona Ebner geschrieben:
Quoted 5 lines
While
bdrv_drained_begin()is now marked asGRAPH_UNLOCKED, TSA is not able to catch a remaining instance where the function is called with the graph lock held inbdrv_change_aio_context(). This is because the call is preceded by abdrv_graph_rdunlock_main_loop(), but for callers that hold the exclusive lock that does not actually release the lock.
And releasing it wouldn't be correct anyway.
Quoted 49 lines
In combination with block-stream, there is a deadlock that can happen because of this [0]. In particular, it can happen that
main thread IO thread
1. acquires write lock
inblk_co_do_preadv_part():
2. have non-zeroblk->in_flight
3. try to acquire read lock
4. begin drainSteps 3 and 4 might be switched. Draining will poll and get stuck, because it will see the non-zero
in_flightcounter. But the IO thread will not make any progress either, because it cannot acquire the read lock.More granular draining is not trivially possible, because
bdrv_change_aio_context()can recursively call itself e.g. viabdrv_child_change_aio_context().[0]: https://lore.kernel.org/qemu-devel/[email protected]/
Reported-by Andrey Drobyshev <[email protected]>Signed-off-by Fiona Ebner <[email protected]>--- block.c | 91 ++++++++++++++++++++---------- block/backup.c | 2 + block/blklogwrites.c | 4 ++ block/blkverify.c | 2 + block/block-backend.c | 4 ++ block/commit.c | 4 ++ block/mirror.c | 5 ++ block/qcow2.c | 2 + block/quorum.c | 4 ++ block/replication.c | 7 +++ block/snapshot.c | 2 + block/stream.c | 10 ++-- block/vmdk.c | 10 ++++ blockdev.c | 17 ++++-- blockjob.c | 6 ++ include/block/block-global-state.h | 8 ++- tests/unit/test-bdrv-drain.c | 18 ++++++ tests/unit/test-bdrv-graph-mod.c | 10 ++++ 18 files changed, 164 insertions(+), 42 deletions(-)block.c +0
-0@@ -1720,12 +1720,14 @@ bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv, const char *node_name, open_failed:
block.c bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv, const char *node_name, · line 1730
Quoted 10 lines
block.c +2
-0bs->drv =NULL ; +bdrv_drain_all_begin ();bdrv_graph_wrlock ();if (bs->file !=NULL ) {bdrv_unref_child (bs, bs->file);assert (!bs->file); }bdrv_graph_wrunlock (); +bdrv_drain_all_end ();
I feel this patch is doing too much at once, pushing drain up three levels to all callers of bdrv_unref_child().
Please split it, doing only one level per patch, i.e. the first patch only marks bdrv_change_aio_context() GRAPH_RDLOCK, removes the double locking and moves the drain (which becomes drain_all) to bdrv_try_change_aio_context(). Then the next patch marks that one GRAPH_RDLOCK, etc.
This will make it more obvious that each step is done correctly. For example, checking this hunk I noticed that bdrv_unref_child() doesn't document that child->bs must be drained, but doing everything at once, it's not obvious if and which other functions have the same problem.
Quoted 7 lines
block.c +0
-3@@ -7702,14 +7699,12 @@ static bool bdrv_change_aio_context(BlockDriverState *bs, AioContext *ctx, }/* -* Change bs's and recursively all of its parents' and children's AioContext -* to the given new context, returning an error if that isn't possible. -*
block.c static bool bdrv_change_aio_context(BlockDriverState *bs, AioContext *ctx, · line 7707
Quoted 10 lines
block.c +5
-4-* If ignore_child is not NULL, that child (and its subgraph) will not -* be touched. +* Use bdrv_try_change_aio_context() or bdrv_try_change_aio_context_locked(). */ -int bdrv_try_change_aio_context (BlockDriverState *bs,AioContext *ctx, -BdrvChild *ignore_child,Error **errp) +static int bdrv_try_change_aio_context_common (BlockDriverState *bs, +AioContext *ctx, +BdrvChild *ignore_child, +Error **errp)
This is exactly the same as bdrv_try_change_aio_context_locked(), so this should be called bdrv_try_change_aio_context_locked() and the wrapper below should go away.
Don't forget to add TSA annotations to functions whose interface you touch (but I already mentioned this above).
block.c static bool bdrv_change_aio_context(BlockDriverState *bs, AioContext *ctx, · line 7710
Quoted 3 lines
block.c +0
-0{Transaction *tran;GHashTable *visited;
I'll defer review of the rest of the patch to the next version when it's split. As it is, it's too hard to understand where each specific change comes from. (That is, I could probably work it out for each individual hunk, but I'd lose the big picture of what I'm really looking at and if the changes are complete.)
Kevin
PATCH 10/11 block/graph-lock: add drain flag to bdrv_graph_wr{, un}lock
In reply to Fiona EbnerFor convenience, allow indicating whether the write-locked section should also be a drained section directly when taking the write lock.
---
In bdrv_graph_wrlock() there is a comment that it uses bdrv_drain_all_begin_nopoll() to make sure that constantly arriving new I/O doesn't cause starvation. The changes from this series are at odds with that, but there doesn't seem to be any (new) test failures.
The following script was used:
Quoted 4 lines
find . -name '*.c' -exec sed -i -z 's/bdrv_drain_all_begin();\n\s*bdrv_graph_wrlock();/bdrv_graph_wrlock(true);/g' {} ';' find . -name '*.c' -exec sed -i -z 's/bdrv_graph_wrunlock();\n\s*bdrv_drain_all_end();/bdrv_graph_wrunlock(true);/g' {} ';' find . -name '*.c' -exec sed -i -z 's/bdrv_graph_wrlock();/bdrv_graph_wrlock(false);/g' {} ';'
find . -name '*.c' -exec sed -i -z 's/bdrv_graph_wrunlock();/bdrv_graph_wrunlock(false);/g' {} ';'
block.c | 62 ++++++++++++------------------ block/backup.c | 6 +-- block/blklogwrites.c | 12 ++---- block/blkverify.c | 6 +-- block/block-backend.c | 12 ++---- block/commit.c | 20 ++++------ block/graph-lock.c | 22 ++++++++--- block/mirror.c | 27 ++++++------- block/qcow2.c | 6 +-- block/quorum.c | 12 ++---- block/replication.c | 21 ++++------ block/snapshot.c | 6 +-- block/stream.c | 16 +++----- block/vmdk.c | 30 +++++---------- blockdev.c | 10 ++--- blockjob.c | 18 +++------ include/block/graph-lock.h | 8 +++- scripts/block-coroutine-wrapper.py | 4 +- tests/unit/test-bdrv-drain.c | 58 ++++++++++------------------ tests/unit/test-bdrv-graph-mod.c | 30 +++++---------- 20 files changed, 152 insertions(+), 234 deletions(-)
block.c +25 -37
@@ -1720,14 +1720,12 @@ bdrv_open_driver(BlockDriverState *bs, BlockDriver *drv, const char *node_name, open_failed: bs->drv =NULL ; -bdrv_drain_all_begin (); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (true);if (bs->file !=NULL ) {bdrv_unref_child (bs, bs->file);assert (!bs->file); } -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true);g_free (bs->opaque); bs->opaque =NULL ;
@@ -3587,11 +3585,9 @@ int bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd,bdrv_graph_rdunlock_main_loop ();bdrv_ref (drain_bs); -bdrv_drain_all_begin (); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (true); ret =bdrv_set_backing_hd_drained (bs, backing_hd, errp); -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true);bdrv_unref (drain_bs);return ret;
@@ -3783,12 +3779,10 @@ static BdrvChild *bdrv_open_child_common(const char *filename,return NULL ; } -bdrv_drain_all_begin (); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (true); child =bdrv_attach_child (parent, bs, bdref_key, child_class, child_role, errp); -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true);return child; }
@@ -4640,9 +4634,9 @@ int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp)bdrv_reopen_commit (&bs_entry->state); } -bdrv_graph_wrlock (); +bdrv_graph_wrlock (false);tran_commit (tran); -bdrv_graph_wrunlock (); +bdrv_graph_wrunlock (false); QTAILQ_FOREACH_REVERSE(bs_entry, bs_queue, entry) {BlockDriverState *bs = bs_entry->state.bs;
@@ -4656,9 +4650,9 @@ int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp) goto cleanup; abort: -bdrv_graph_wrlock (); +bdrv_graph_wrlock (false);tran_abort (tran); -bdrv_graph_wrunlock (); +bdrv_graph_wrunlock (false); QTAILQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) {if (bs_entry->prepared) {
@@ -4813,14 +4807,12 @@ bdrv_reopen_parse_file_or_backing(BDRVReopenState *reopen_state, }bdrv_graph_rdunlock_main_loop (); -bdrv_drain_all_begin (); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (true); ret =bdrv_set_file_or_backing_noperm (bs, new_child_bs, is_backing, tran, errp); -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true);if (old_child_bs) {bdrv_unref (old_child_bs);
@@ -5164,16 +5156,14 @@ static void bdrv_close(BlockDriverState *bs) bs->drv =NULL ; } -bdrv_drain_all_begin (); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (true); QLIST_FOREACH_SAFE(child, &bs->children, next, next) {bdrv_unref_child (bs, child); }assert (!bs->backing);assert (!bs->file); -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true);g_free (bs->opaque); bs->opaque =NULL ;
@@ -5466,9 +5456,9 @@ int bdrv_drop_filter(BlockDriverState *bs, Error **errp)bdrv_graph_rdunlock_main_loop ();bdrv_drained_begin (child_bs); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (false); ret =bdrv_replace_node_common (bs, child_bs, true, true, errp); -bdrv_graph_wrunlock (); +bdrv_graph_wrunlock (false);bdrv_drained_end (child_bs);return ret;
@@ -5499,8 +5489,7 @@ int bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top,assert (!bs_new->backing);bdrv_graph_rdunlock_main_loop (); -bdrv_drain_all_begin (); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (true); child = bdrv_attach_child_noperm(bs_new, bs_top, "backing", &child_of_bds, bdrv_backing_role(bs_new),
@@ -5520,8 +5509,7 @@ out:tran_finalize (tran, ret);bdrv_refresh_limits (bs_top,NULL ,NULL ); -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true);return ret; }
@@ -5540,7 +5528,7 @@ int bdrv_replace_child_bs(BdrvChild *child, BlockDriverState *new_bs,bdrv_ref (old_bs);bdrv_drained_begin (old_bs);bdrv_drained_begin (new_bs); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (false);bdrv_replace_child_tran (child, new_bs, tran);
@@ -5551,7 +5539,7 @@ int bdrv_replace_child_bs(BdrvChild *child, BlockDriverState *new_bs,tran_finalize (tran, ret); -bdrv_graph_wrunlock (); +bdrv_graph_wrunlock (false);bdrv_drained_end (old_bs);bdrv_drained_end (new_bs);bdrv_unref (old_bs);
@@ -5633,9 +5621,9 @@ BlockDriverState *bdrv_insert_node(BlockDriverState *bs, QDict *options,bdrv_ref (bs);bdrv_drained_begin (bs);bdrv_drained_begin (new_node_bs); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (false); ret =bdrv_replace_node (bs, new_node_bs, errp); -bdrv_graph_wrunlock (); +bdrv_graph_wrunlock (false);bdrv_drained_end (new_node_bs);bdrv_drained_end (bs);bdrv_unref (bs);
@@ -5891,7 +5879,7 @@ int bdrv_drop_intermediate(BlockDriverState *top, BlockDriverState *base,bdrv_ref (top);bdrv_drained_begin (base); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (false);if (!top->drv || !base->drv) { goto exit_wrlock;
@@ -5931,7 +5919,7 @@ int bdrv_drop_intermediate(BlockDriverState *top, BlockDriverState *base,* That's a FIXME. */ bdrv_replace_node_common (top, base, false, false, &local_err); -bdrv_graph_wrunlock (); +bdrv_graph_wrunlock (false);if (local_err) {error_report_err (local_err);
@@ -5969,7 +5957,7 @@ int bdrv_drop_intermediate(BlockDriverState *top, BlockDriverState *base, goto exit; exit_wrlock: -bdrv_graph_wrunlock (); +bdrv_graph_wrunlock (false); exit:bdrv_drained_end (base);bdrv_unref (top);
block/backup.c +2 -4
@@ -497,12 +497,10 @@ BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs,block_copy_set_speed (bcs, speed);/* Required permissions are taken by copy-before-write filter target */ -bdrv_drain_all_begin (); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (true);block_job_add_bdrv (&job->common,"target" , target,0 ,BLK_PERM_ALL , &error_abort); -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true);return &job->common;
block/blklogwrites.c +4 -8
@@ -281,11 +281,9 @@ static int blk_log_writes_open(BlockDriverState *bs, QDict *options, int flags, ret =0 ; fail_log:if (ret <0 ) { -bdrv_drain_all_begin (); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (true);bdrv_unref_child (bs, s->log_file); -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true); s->log_file =NULL ;qemu_mutex_destroy (&s->mutex); }
@@ -298,12 +296,10 @@ static void blk_log_writes_close(BlockDriverState *bs) {BDRVBlkLogWritesState *s = bs->opaque; -bdrv_drain_all_begin (); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (true);bdrv_unref_child (bs, s->log_file); s->log_file =NULL ; -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true);qemu_mutex_destroy (&s->mutex); }
block/blkverify.c +2 -4
@@ -151,12 +151,10 @@ static void blkverify_close(BlockDriverState *bs) {BDRVBlkverifyState *s = bs->opaque; -bdrv_drain_all_begin (); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (true);bdrv_unref_child (bs, s->test_file); s->test_file =NULL ; -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true); } static int64_t coroutine_fn GRAPH_RDLOCK
block/block-backend.c +4 -8
@@ -889,11 +889,9 @@ void blk_remove_bs(BlockBackend *blk) root = blk->root; blk->root =NULL ; -bdrv_drain_all_begin (); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (true);bdrv_root_unref_child (root); -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true); }/*
@@ -906,8 +904,7 @@ int blk_insert_bs(BlockBackend *blk, BlockDriverState *bs, Error **errp)GLOBAL_STATE_CODE ();bdrv_ref (bs); -bdrv_drain_all_begin (); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (true);if ((bs->open_flags &BDRV_O_INACTIVE ) &&blk_can_inactivate (blk)) { blk->disable_perm = true;
@@ -921,8 +918,7 @@ int blk_insert_bs(BlockBackend *blk, BlockDriverState *bs, Error **errp) blk->root =bdrv_root_attach_child (bs,"root" , &child_root,BDRV_CHILD_FILTERED |BDRV_CHILD_PRIMARY , perm, shared_perm, blk, errp); -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true);if (blk->root ==NULL ) {return -EPERM ; }
block/commit.c +8 -12
@@ -102,9 +102,9 @@ static void commit_abort(Job *job)bdrv_graph_rdunlock_main_loop ();bdrv_drained_begin (commit_top_backing_bs); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (false);bdrv_replace_node (s->commit_top_bs, commit_top_backing_bs, &error_abort); -bdrv_graph_wrunlock (); +bdrv_graph_wrunlock (false);bdrv_drained_end (commit_top_backing_bs);bdrv_unref (s->commit_top_bs);
@@ -342,8 +342,7 @@ void commit_start(const char *job_id, BlockDriverState *bs,* this is the responsibility of the interface (i.e. whoever calls * commit_start()). */ -bdrv_drain_all_begin (); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (true); s->base_overlay =bdrv_find_overlay (top, base);assert (s->base_overlay);
@@ -374,22 +373,19 @@ void commit_start(const char *job_id, BlockDriverState *bs, ret =block_job_add_bdrv (&s->common,"intermediate node" , iter,0 , iter_shared_perms, errp);if (ret <0 ) { -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true); goto fail; } }if (bdrv_freeze_backing_chain (commit_top_bs, base, errp) <0 ) { -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true); goto fail; } s->chain_frozen = true; ret =block_job_add_bdrv (&s->common,"base" , base,0 ,BLK_PERM_ALL , errp); -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true);if (ret <0 ) { goto fail;
@@ -442,9 +438,9 @@ fail:* otherwise this would fail because of lack of permissions. */ if (commit_top_bs) {bdrv_drained_begin (top); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (false);bdrv_replace_node (commit_top_bs, top, &error_abort); -bdrv_graph_wrunlock (); +bdrv_graph_wrunlock (false);bdrv_drained_end (top); } }
block/graph-lock.c +17 -5
@@ -106,14 +106,20 @@ static uint32_t reader_count(void)return rd; } -void no_coroutine_fn bdrv_graph_wrlock(void) +void no_coroutine_fn bdrv_graph_wrlock(bool drain) {GLOBAL_STATE_CODE ();assert (!qatomic_read (&has_writer));assert (!qemu_in_coroutine ()); -/* Make sure that constantly arriving new I/O doesn't cause starvation */ -bdrv_drain_all_begin_nopoll (); +if (drain) { +bdrv_drain_all_begin (); + }else { +/* +* Make sure that constantly arriving new I/O doesn't cause starvation +*/ +bdrv_drain_all_begin_nopoll (); + }/* * reader_count == 0: this means writer will read has_reader as 1
@@ -139,10 +145,12 @@ void no_coroutine_fn bdrv_graph_wrlock(void)smp_mb (); }while (reader_count () >=1 ); -bdrv_drain_all_end (); +if (!drain) { +bdrv_drain_all_end (); + } } -void no_coroutine_fn bdrv_graph_wrunlock(void) +void no_coroutine_fn bdrv_graph_wrunlock(bool drain) {GLOBAL_STATE_CODE ();assert (qatomic_read (&has_writer));
@@ -168,6 +176,10 @@ void no_coroutine_fn bdrv_graph_wrunlock(void)* progress. */ aio_bh_poll (qemu_get_aio_context ()); + +if (drain) { +bdrv_drain_all_end (); + } } void coroutine_fn bdrv_graph_co_rdlock(void)
block/mirror.c +11 -16
@@ -762,7 +762,7 @@ static int mirror_exit_common(Job *job)* check for an op blocker on @to_replace, and we have our own * there. */ -bdrv_graph_wrlock (); +bdrv_graph_wrlock (false);if (bdrv_recurse_can_replace (src, to_replace)) {bdrv_replace_node (to_replace, target_bs, &local_err); }else {
@@ -771,7 +771,7 @@ static int mirror_exit_common(Job *job)"would not lead to an abrupt change of visible data" , to_replace->node_name, target_bs->node_name); } -bdrv_graph_wrunlock (); +bdrv_graph_wrunlock (false);bdrv_drained_end (to_replace);if (local_err) {error_report_err (local_err);
@@ -791,9 +791,9 @@ static int mirror_exit_common(Job *job)* valid. */ block_job_remove_all_bdrv (bjob); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (false);bdrv_replace_node (mirror_top_bs, mirror_top_bs->backing->bs, &error_abort); -bdrv_graph_wrunlock (); +bdrv_graph_wrunlock (false);if (abort && s->base_ro && !bdrv_is_read_only (target_bs)) {bdrv_reopen_set_read_only (target_bs, true,NULL );
@@ -1904,15 +1904,13 @@ static BlockJob *mirror_start_job(*/ bdrv_disable_dirty_bitmap (s->dirty_bitmap); -bdrv_drain_all_begin (); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (true); ret =block_job_add_bdrv (&s->common,"source" , bs,0 ,BLK_PERM_WRITE_UNCHANGED |BLK_PERM_WRITE |BLK_PERM_CONSISTENT_READ , errp);if (ret <0 ) { -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true); goto fail; }
@@ -1957,20 +1955,17 @@ static BlockJob *mirror_start_job( ret =block_job_add_bdrv (&s->common,"intermediate node" , iter,0 , iter_shared_perms, errp);if (ret <0 ) { -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true); goto fail; } }if (bdrv_freeze_backing_chain (mirror_top_bs, target, errp) <0 ) { -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true); goto fail; } } -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true);QTAILQ_INIT (&s->ops_in_flight);
@@ -1996,12 +1991,12 @@ fail: bs_opaque->stop = true;bdrv_drained_begin (bs); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (false);assert (mirror_top_bs->backing->bs == bs);bdrv_child_refresh_perms (mirror_top_bs, mirror_top_bs->backing, &error_abort);bdrv_replace_node (mirror_top_bs, bs, &error_abort); -bdrv_graph_wrunlock (); +bdrv_graph_wrunlock (false);bdrv_drained_end (bs);bdrv_unref (mirror_top_bs);
block/qcow2.c +2 -4
@@ -2821,11 +2821,9 @@ qcow2_do_close(BlockDriverState *bs, bool close_data_file)if (close_data_file &&has_data_file (bs)) {GLOBAL_STATE_CODE ();bdrv_graph_rdunlock_main_loop (); -bdrv_drain_all_begin (); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (true);bdrv_unref_child (bs, s->data_file); -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true); s->data_file =NULL ;bdrv_graph_rdlock_main_loop (); }
block/quorum.c +4 -8
@@ -1037,16 +1037,14 @@ static int quorum_open(BlockDriverState *bs, QDict *options, int flags, close_exit:/* cleanup on error */ -bdrv_drain_all_begin (); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (true);for (i =0 ; i < s->num_children; i++) {if (!opened[i]) {continue ; }bdrv_unref_child (bs, s->children[i]); } -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true);g_free (s->children);g_free (opened); exit:
@@ -1059,13 +1057,11 @@ static void quorum_close(BlockDriverState *bs)BDRVQuorumState *s = bs->opaque;int i; -bdrv_drain_all_begin (); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (true);for (i =0 ; i < s->num_children; i++) {bdrv_unref_child (bs, s->children[i]); } -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true);g_free (s->children); }
block/replication.c +7 -14
@@ -540,8 +540,7 @@ static void replication_start(ReplicationState *rs, ReplicationMode mode,return ; } -bdrv_drain_all_begin (); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (true);bdrv_ref (hidden_disk->bs); s->hidden_disk = bdrv_attach_child(bs, hidden_disk->bs, "hidden disk",
@@ -549,8 +548,7 @@ static void replication_start(ReplicationState *rs, ReplicationMode mode, &local_err);if (local_err) {error_propagate (errp, local_err); -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true);return ; }
@@ -560,8 +558,7 @@ static void replication_start(ReplicationState *rs, ReplicationMode mode, BDRV_CHILD_DATA, &local_err);if (local_err) {error_propagate (errp, local_err); -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true);return ; }
@@ -573,15 +570,13 @@ static void replication_start(ReplicationState *rs, ReplicationMode mode,if (!top_bs || !bdrv_is_root_node (top_bs) || !check_top_bs (top_bs, bs)) {error_setg (errp,"No top_bs or it is invalid" ); -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true);reopen_backing_file (bs, false,NULL );return ; }bdrv_op_block_all (top_bs, s->blocker); -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true); s->backup_job = backup_job_create( NULL, s->secondary_disk->bs, s->hidden_disk->bs,
@@ -654,14 +649,12 @@ static void replication_done(void *opaque, int ret)if (ret ==0 ) { s->stage =BLOCK_REPLICATION_DONE ; -bdrv_drain_all_begin (); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (true);bdrv_unref_child (bs, s->secondary_disk); s->secondary_disk =NULL ;bdrv_unref_child (bs, s->hidden_disk); s->hidden_disk =NULL ; -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true); s->error =0 ; }else {
block/snapshot.c +2 -4
@@ -291,11 +291,9 @@ int bdrv_snapshot_goto(BlockDriverState *bs, }/* .bdrv_open() will re-attach it */ -bdrv_drain_all_begin (); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (true);bdrv_unref_child (bs, fallback); -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true); ret =bdrv_snapshot_goto (fallback_bs, snapshot_id, errp);memset (bs->opaque,0 , drv->instance_size);
block/stream.c +6 -10
@@ -104,9 +104,9 @@ static int stream_prepare(Job *job) } } -bdrv_graph_wrlock (); +bdrv_graph_wrlock (false);bdrv_set_backing_hd_drained (unfiltered_bs, base, &local_err); -bdrv_graph_wrunlock (); +bdrv_graph_wrunlock (false);/* * This call will do I/O, so the graph can change again from here on.
@@ -371,12 +371,10 @@ void stream_start(const char *job_id, BlockDriverState *bs,* already have our own plans. Also don't allow resize as the image size is * queried only at the job start and then cached. */ -bdrv_drain_all_begin (); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (true);if (block_job_add_bdrv (&s->common,"active node" , bs,0 , basic_flags |BLK_PERM_WRITE , errp)) { -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true); goto fail; }
@@ -396,13 +394,11 @@ void stream_start(const char *job_id, BlockDriverState *bs, ret =block_job_add_bdrv (&s->common,"intermediate node" , iter,0 , basic_flags, errp);if (ret <0 ) { -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true); goto fail; } } -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true); s->base_overlay = base_overlay; s->above_base = above_base;
block/vmdk.c +10 -20
@@ -271,8 +271,7 @@ static void vmdk_free_extents(BlockDriverState *bs)BDRVVmdkState *s = bs->opaque;VmdkExtent *e; -bdrv_drain_all_begin (); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (true);for (i =0 ; i < s->num_extents; i++) { e = &s->extents[i];g_free (e->l1_table);
@@ -283,8 +282,7 @@ static void vmdk_free_extents(BlockDriverState *bs)bdrv_unref_child (bs, e->file); } } -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true);g_free (s->extents); }
@@ -1249,11 +1247,9 @@ vmdk_parse_extents(const char *desc, BlockDriverState *bs, QDict *options, 0, 0, 0, 0, 0, &extent, errp);if (ret <0 ) {bdrv_graph_rdunlock_main_loop (); -bdrv_drain_all_begin (); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (true);bdrv_unref_child (bs, extent_file); -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true);bdrv_graph_rdlock_main_loop (); goto out; }
@@ -1270,11 +1266,9 @@ vmdk_parse_extents(const char *desc, BlockDriverState *bs, QDict *options,g_free (buf);if (ret) {bdrv_graph_rdunlock_main_loop (); -bdrv_drain_all_begin (); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (true);bdrv_unref_child (bs, extent_file); -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true);bdrv_graph_rdlock_main_loop (); goto out; }
@@ -1283,11 +1277,9 @@ vmdk_parse_extents(const char *desc, BlockDriverState *bs, QDict *options, ret =vmdk_open_se_sparse (bs, extent_file, bs->open_flags, errp);if (ret) {bdrv_graph_rdunlock_main_loop (); -bdrv_drain_all_begin (); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (true);bdrv_unref_child (bs, extent_file); -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true);bdrv_graph_rdlock_main_loop (); goto out; }
@@ -1295,11 +1287,9 @@ vmdk_parse_extents(const char *desc, BlockDriverState *bs, QDict *options, }else {error_setg (errp,"Unsupported extent type '%s'" , type);bdrv_graph_rdunlock_main_loop (); -bdrv_drain_all_begin (); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (true);bdrv_unref_child (bs, extent_file); -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true);bdrv_graph_rdlock_main_loop (); ret = -ENOTSUP ; goto out;
blockdev.c +4 -6
@@ -1576,9 +1576,9 @@ static void external_snapshot_abort(void *opaque) }bdrv_drained_begin (state->new_bs); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (false);bdrv_replace_node (state->new_bs, state->old_bs, &error_abort); -bdrv_graph_wrunlock (); +bdrv_graph_wrunlock (false);bdrv_drained_end (state->new_bs);bdrv_unref (state->old_bs);/* bdrv_replace_node() ref'ed old_bs */
@@ -3539,8 +3539,7 @@ void qmp_x_blockdev_change(const char *parent, const char *child,BlockDriverState *parent_bs, *new_bs =NULL ;BdrvChild *p_child; -bdrv_drain_all_begin (); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (true); parent_bs =bdrv_lookup_bs (parent, parent, errp);if (!parent_bs) {
@@ -3576,8 +3575,7 @@ void qmp_x_blockdev_change(const char *parent, const char *child, } out: -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true); }BlockJobInfoList *qmp_query_block_jobs (Error **errp)
blockjob.c +6 -12
@@ -198,8 +198,7 @@ void block_job_remove_all_bdrv(BlockJob *job)* one to make sure that such a concurrent access does not attempt * to process an already freed BdrvChild. */ -bdrv_drain_all_begin (); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (true);while (job->nodes) {GSList *l = job->nodes;BdrvChild *c = l->data;
@@ -211,8 +210,7 @@ void block_job_remove_all_bdrv(BlockJob *job)g_slist_free_1 (l); } -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true); }bool block_job_has_bdrv (BlockJob *job,BlockDriverState *bs)
@@ -498,8 +496,7 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,int ret;GLOBAL_STATE_CODE (); -bdrv_drain_all_begin (); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (true);if (job_id ==NULL && !(flags &JOB_INTERNAL )) { job_id =bdrv_get_device_name (bs);
@@ -508,8 +505,7 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver, job =job_create (job_id, &driver->job_driver, txn,bdrv_get_aio_context (bs), flags, cb, opaque, errp);if (job ==NULL ) { -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true);return NULL ; }
@@ -547,13 +543,11 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver, goto fail; } -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true);return job; fail: -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true);job_early_fail (&job->job);return NULL ; }
include/block/graph-lock.h +6 -2
@@ -108,17 +108,21 @@ void unregister_aiocontext(AioContext *ctx);* * The wrlock can only be taken from the main loop, with BQL held, as only the * main loop is allowed to modify the graph. +* +* @drain whether bdrv_drain_all_begin() should be called before taking the lock */ void no_coroutine_fn TSA_ACQUIRE(graph_lock) TSA_NO_TSA -bdrv_graph_wrlock(void); +bdrv_graph_wrlock(bool drain); /* * bdrv_graph_wrunlock: * Write finished, reset global has_writer to 0 and restart * all readers that are waiting. + * + * @drain whether bdrv_drain_all_end() should be called after releasing the lock */ void no_coroutine_fn TSA_RELEASE(graph_lock) TSA_NO_TSA -bdrv_graph_wrunlock(void); +bdrv_graph_wrunlock(bool drain);/* * bdrv_graph_co_rdlock:
scripts/block-coroutine-wrapper.py +2 -2
@@ -259,8 +259,8 @@ def gen_no_co_wrapper(func: FuncDecl) -> str: graph_lock=' bdrv_graph_rdlock_main_loop();' graph_unlock=' bdrv_graph_rdunlock_main_loop();' elif func.graph_wrlock: - graph_lock=' bdrv_graph_wrlock();' - graph_unlock=' bdrv_graph_wrunlock();' + graph_lock=' bdrv_graph_wrlock(false);' + graph_unlock=' bdrv_graph_wrunlock(false);' return f"""\ /*
tests/unit/test-bdrv-drain.c +20 -38
150 lines not on this page · raw message
tests/unit/test-bdrv-graph-mod.c +10 -20
@@ -137,12 +137,10 @@ static void test_update_perm_tree(void)blk_insert_bs (root, bs, &error_abort); -bdrv_drain_all_begin (); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (true);bdrv_attach_child (filter, bs,"child" , &child_of_bds,BDRV_CHILD_DATA , &error_abort); -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true); ret =bdrv_append (filter, bs,NULL ); g_assert_cmpint(ret, <, 0);
@@ -206,13 +204,11 @@ static void test_should_update_child(void)bdrv_set_backing_hd (target, bs, &error_abort); -bdrv_drain_all_begin (); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (true);g_assert (target->backing->bs == bs);bdrv_attach_child (filter, target,"target" , &child_of_bds,BDRV_CHILD_DATA , &error_abort); -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true);bdrv_append (filter, bs, &error_abort);bdrv_graph_rdlock_main_loop ();
@@ -248,8 +244,7 @@ static void test_parallel_exclusive_write(void)bdrv_ref (base);bdrv_ref (fl1); -bdrv_drain_all_begin (); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (true);bdrv_attach_child (top, fl1,"backing" , &child_of_bds,BDRV_CHILD_FILTERED |BDRV_CHILD_PRIMARY , &error_abort);
@@ -261,8 +256,7 @@ static void test_parallel_exclusive_write(void) &error_abort);bdrv_replace_node (fl1, fl2, &error_abort); -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true);bdrv_drained_end (fl2);bdrv_drained_end (fl1);
@@ -369,8 +363,7 @@ static void test_parallel_perm_update(void)*/ bdrv_ref (base); -bdrv_drain_all_begin (); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (true);bdrv_attach_child (top, ws,"file" , &child_of_bds,BDRV_CHILD_DATA , &error_abort); c_fl1 = bdrv_attach_child(ws, fl1, "first", &child_of_bds,
@@ -383,8 +376,7 @@ static void test_parallel_perm_update(void)bdrv_attach_child (fl2, base,"backing" , &child_of_bds,BDRV_CHILD_FILTERED |BDRV_CHILD_PRIMARY , &error_abort); -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true);/* Select fl1 as first child to be active */ s->selected = c_fl1;
@@ -438,13 +430,11 @@ static void test_append_greedy_filter(void)BlockDriverState *base =no_perm_node ("base" );BlockDriverState *fl =exclusive_writer_node ("fl1" ); -bdrv_drain_all_begin (); -bdrv_graph_wrlock (); +bdrv_graph_wrlock (true);bdrv_attach_child (top, base,"backing" , &child_of_bds,BDRV_CHILD_FILTERED |BDRV_CHILD_PRIMARY , &error_abort); -bdrv_graph_wrunlock (); -bdrv_drain_all_end (); +bdrv_graph_wrunlock (true);bdrv_append (fl, base, &error_abort);bdrv_unref (fl);
Signature · 1 line
2.39.5
Re: [PATCH 10/11] block/graph-lock: add drain flag to bdrv_graph_wr{,un}lock
Am 08.05.2025 um 16:09 hat Fiona Ebner geschrieben:
Quoted 5 lines
For convenience, allow indicating whether the write-locked section should also be a drained section directly when taking the write lock.
Signed-off-by Fiona Ebner <[email protected]>
Good idea, the pattern seems to be common enough.
Quoted 4 lines
In
bdrv_graph_wrlock()there is a comment that it usesbdrv_drain_all_begin_nopoll()to make sure that constantly arriving new I/O doesn't cause starvation. The changes from this series are at odds with that, but there doesn't seem to be any (new) test failures.
I don't see why they are at odds with it? Draining an already drained node isn't a problem, it just increases the counter without doing anything else.
Quoted 34 lines
The following script was used:
Quoted 4 lines
find . -name '*.c' -exec sed -i -z 's/bdrv_drain_all_begin();\n\s*bdrv_graph_wrlock();/bdrv_graph_wrlock(true);/g' {} ';' find . -name '*.c' -exec sed -i -z 's/bdrv_graph_wrunlock();\n\s*bdrv_drain_all_end();/bdrv_graph_wrunlock(true);/g' {} ';' find . -name '*.c' -exec sed -i -z 's/bdrv_graph_wrlock();/bdrv_graph_wrlock(false);/g' {} ';'
find . -name '*.c' -exec sed -i -z 's/bdrv_graph_wrunlock();/bdrv_graph_wrunlock(false);/g' {} ';'block.c | 62 ++++++++++++------------------ block/backup.c | 6 +-- block/blklogwrites.c | 12 ++---- block/blkverify.c | 6 +-- block/block-backend.c | 12 ++---- block/commit.c | 20 ++++------ block/graph-lock.c | 22 ++++++++--- block/mirror.c | 27 ++++++------- block/qcow2.c | 6 +-- block/quorum.c | 12 ++---- block/replication.c | 21 ++++------ block/snapshot.c | 6 +-- block/stream.c | 16 +++----- block/vmdk.c | 30 +++++---------- blockdev.c | 10 ++--- blockjob.c | 18 +++------ include/block/graph-lock.h | 8 +++- scripts/block-coroutine-wrapper.py | 4 +- tests/unit/test-bdrv-drain.c | 58 ++++++++++------------------ tests/unit/test-bdrv-graph-mod.c | 30 +++++---------- 20 files changed, 152 insertions(+), 234 deletions(-)include/block/graph-lock.h +0
-0
include/block/graph-lock.h void unregister_aiocontext(AioContext *ctx); · line 115
Quoted 9 lines
include/block/graph-lock.h +3
-1@@ -108,17 +108,21 @@ void unregister_aiocontext(AioContext *ctx);* * The wrlock can only be taken from the main loop, with BQL held, as only the * main loop is allowed to modify the graph. +* +* @drain whether bdrv_drain_all_begin() should be called before taking the lock */ void no_coroutine_fn TSA_ACQUIRE(graph_lock) TSA_NO_TSA -bdrv_graph_wrlock(void); +bdrv_graph_wrlock(bool drain);
I would prefer having two separate functions instead of a bool parameter.
bdrv_graph_wrlock() could stay as it is, and bdrv_graph_wrlock_drained() could be the convenience wrapper that drains first.
include/block/graph-lock.h void unregister_aiocontext(AioContext *ctx); · line 125
Quoted 10 lines
include/block/graph-lock.h +3
-1/* * bdrv_graph_wrunlock: * Write finished, reset global has_writer to 0 and restart * all readers that are waiting. +* +* @drain whether bdrv_drain_all_end() should be called after releasing the lock */ void no_coroutine_fn TSA_RELEASE(graph_lock) TSA_NO_TSA -bdrv_graph_wrunlock(void); +bdrv_graph_wrunlock(bool drain);
Here I would prefer to only keep the old bdrv_graph_wrunlock() without a parameter. Can we just remember @drain from bdrv_graph_wrlock() in a global variable? This would prevent callers from mismatching lock and unlock variants (which TSA wouldn't be able to catch).
Kevin
Re: [PATCH 10/11] block/graph-lock: add drain flag to bdrv_graph_wr{,un}lock
Am 14.05.25 um 21:54 schrieb Kevin Wolf:
Quoted 9 lines
Am 08.05.2025 um 16:09 hat Fiona Ebner geschrieben:
Quoted 4 lines
In
bdrv_graph_wrlock()there is a comment that it usesbdrv_drain_all_begin_nopoll()to make sure that constantly arriving new I/O doesn't cause starvation. The changes from this series are at odds with that, but there doesn't seem to be any (new) test failures.I don't see why they are at odds with it? Draining an already drained node isn't a problem, it just increases the counter without doing anything else.
What I mean is: the introduction of calls to bdrv_drain_all_begin() before bdrv_drain_all_begin_nopoll() could introduce potential for starvation when there is constantly arriving new I/O. Or is this not true?
Quoted 18 lines
Quoted 5 lines
include/block/graph-lock.h +0
-0include/block/graph-lock.h void unregister_aiocontext(AioContext *ctx); · line 115
Quoted 9 lines
include/block/graph-lock.h +3
-1@@ -108,17 +108,21 @@ void unregister_aiocontext(AioContext *ctx);* * The wrlock can only be taken from the main loop, with BQL held, as only the * main loop is allowed to modify the graph. +* +* @drain whether bdrv_drain_all_begin() should be called before taking the lock */ void no_coroutine_fn TSA_ACQUIRE(graph_lock) TSA_NO_TSA -bdrv_graph_wrlock(void); +bdrv_graph_wrlock(bool drain);I would prefer having two separate functions instead of a bool parameter.
Quoted 2 lines
bdrv_graph_wrlock()could stay as it is, andbdrv_graph_wrlock_drained()could be the convenience wrapper that drains first.
Will do!
Quoted 7 lines
Quoted 7 lines
include/block/graph-lock.h +2
-0/* * bdrv_graph_wrunlock: * Write finished, reset global has_writer to 0 and restart * all readers that are waiting. +* +* @drain whether bdrv_drain_all_end() should be called after releasing the lock */
Quoted 8 lines
include/block/graph-lock.h void unregister_aiocontext(AioContext *ctx); · line 125
Quoted 3 lines
include/block/graph-lock.h +1
-1void no_coroutine_fn TSA_RELEASE(graph_lock) TSA_NO_TSA -bdrv_graph_wrunlock(void); +bdrv_graph_wrunlock(bool drain);Here I would prefer to only keep the old
bdrv_graph_wrunlock()without a parameter. Can we just remember @drain frombdrv_graph_wrlock()in a global variable? This would prevent callers from mismatching lock and unlock variants (which TSA wouldn't be able to catch).
Okay.
Best Regards,
Fiona
Re: [PATCH 10/11] block/graph-lock: add drain flag to bdrv_graph_wr{,un}lock
Quoted 7 lines · Fiona Ebner
Am 14.05.25 um 21:54 schrieb Kevin Wolf:
Quoted 6 lines
Am 08.05.2025 um 16:09 hat Fiona Ebner geschrieben:
Quoted 4 lines
In
bdrv_graph_wrlock()there is a comment that it usesbdrv_drain_all_begin_nopoll()to make sure that constantly arriving new I/O doesn't cause starvation. The changes from this series are at odds with that, but there doesn't seem to be any (new) test failures.
Quoted 7 lines
Quoted 3 lines
I don't see why they are at odds with it? Draining an already drained node isn't a problem, it just increases the counter without doing anything else.
What I mean is: the introduction of calls to
bdrv_drain_all_begin()beforebdrv_drain_all_begin_nopoll()could introduce potential for starvation when there is constantly arriving new I/O. Or is this not true?
Oh, I guess I know why I was confused now: I thought the comment is the rationale for why the _nopoll variant is used, but the comment is the rationale for the draining itself :)
Best Regards,
Fiona
Re: [PATCH 10/11] block/graph-lock: add drain flag to bdrv_graph_wr{,un}lock
Am 20.05.2025 um 08:09 hat Fiona Ebner geschrieben:
Quoted 12 lines · Fiona Ebner
Quoted 11 lines
Am 14.05.25 um 21:54 schrieb Kevin Wolf:
Quoted 9 lines
Am 08.05.2025 um 16:09 hat Fiona Ebner geschrieben:
Quoted 4 lines
In
bdrv_graph_wrlock()there is a comment that it usesbdrv_drain_all_begin_nopoll()to make sure that constantly arriving new I/O doesn't cause starvation. The changes from this series are at odds with that, but there doesn't seem to be any (new) test failures.I don't see why they are at odds with it? Draining an already drained node isn't a problem, it just increases the counter without doing anything else.
Quoted 7 lines
Quoted 3 lines
What I mean is: the introduction of calls to
bdrv_drain_all_begin()beforebdrv_drain_all_begin_nopoll()could introduce potential for starvation when there is constantly arriving new I/O. Or is this not true?Oh, I guess I know why I was confused now: I thought the comment is the rationale for why the
_nopollvariant is used, but the comment is the rationale for the draining itself :)
Ah, yes, it is! Or for both together, but in the sense why nopoll is good enough, not why it does something that the normal drain wouldn't do. The nopoll variant is used simply because we don't really need to have all requests drained, it's good enough if no new request are coming in.
Kevin
PATCH 11/11 iotests/graph-changes-while-io: add test case with removal of lower snapshot
In reply to Fiona EbnerFrom: Andrey Drobyshev <[email protected]>
This case is catching potential deadlock which takes place when job-dismiss is issued when I/O requests are processed in a separate iothread.
See https://mail.gnu.org/archive/html/qemu-devel/2025-04/msg04421.html
--- .../qemu-iotests/tests/graph-changes-while-io | 101 ++++++++++++++++-- .../tests/graph-changes-while-io.out | 4 +- 2 files changed, 96 insertions(+), 9 deletions(-)
tests/qemu-iotests/tests/graph-changes-while-io +94 -7
@@ -27,6 +27,8 @@ from iotests import imgfmt, qemu_img, qemu_img_create, qemu_io, \ top = os.path.join(iotests.test_dir, 'top.img') +snap1 = os.path.join(iotests.test_dir, 'snap1.img') +snap2 = os.path.join(iotests.test_dir, 'snap2.img') nbd_sock = os.path.join(iotests.sock_dir, 'nbd.sock')
@@ -58,6 +60,15 @@ class TestGraphChangesWhileIO(QMPTestCase): def tearDown(self) -> None: self.qsd.stop() + def _wait_for_blockjob(self, status) -> None: + done = False + while not done: + for event in self.qsd.get_qmp().get_events(wait=10.0): + if event['event'] != 'JOB_STATUS_CHANGE': + continue + if event['data']['status'] == status: + done = True + def test_blockdev_add_while_io(self) -> None: # Run qemu-img bench in the background bench_thr = Thread(target=do_qemu_img_bench)
@@ -116,13 +127,89 @@ class TestGraphChangesWhileIO(QMPTestCase): 'device': 'job0', }) - cancelled = False - while not cancelled: - for event in self.qsd.get_qmp().get_events(wait=10.0): - if event['event'] != 'JOB_STATUS_CHANGE': - continue - if event['data']['status'] == 'null': - cancelled = True + self._wait_for_blockjob('null') + + bench_thr.join() + + def test_remove_lower_snapshot_while_io(self) -> None: + # Run qemu-img bench in the background + bench_thr = Thread(target=do_qemu_img_bench, args=(100000, )) + bench_thr.start() + + # While I/O is performed on 'node0' node, consequently add 2 snapshots + # on top of it, then remove (commit) them starting from lower one. + while bench_thr.is_alive(): + # Recreate snapshot images on every iteration + qemu_img_create('-f', imgfmt, snap1, '1G') + qemu_img_create('-f', imgfmt, snap2, '1G') + + self.qsd.cmd('blockdev-add', { + 'driver': imgfmt, + 'node-name': 'snap1', + 'file': { + 'driver': 'file', + 'filename': snap1 + } + }) + + self.qsd.cmd('blockdev-snapshot', { + 'node': 'node0', + 'overlay': 'snap1', + }) + + self.qsd.cmd('blockdev-add', { + 'driver': imgfmt, + 'node-name': 'snap2', + 'file': { + 'driver': 'file', + 'filename': snap2 + } + }) + + self.qsd.cmd('blockdev-snapshot', { + 'node': 'snap1', + 'overlay': 'snap2', + }) + + self.qsd.cmd('block-commit', { + 'job-id': 'commit-snap1', + 'device': 'snap2', + 'top-node': 'snap1', + 'base-node': 'node0', + 'auto-finalize': True, + 'auto-dismiss': False, + }) + + self._wait_for_blockjob('concluded') + self.qsd.cmd('job-dismiss', { + 'id': 'commit-snap1', + }) + + self.qsd.cmd('block-commit', { + 'job-id': 'commit-snap2', + 'device': 'snap2', + 'top-node': 'snap2', + 'base-node': 'node0', + 'auto-finalize': True, + 'auto-dismiss': False, + }) + + self._wait_for_blockjob('ready') + self.qsd.cmd('job-complete', { + 'id': 'commit-snap2', + }) + + self._wait_for_blockjob('concluded') + self.qsd.cmd('job-dismiss', { + 'id': 'commit-snap2', + }) + + self.qsd.cmd('blockdev-del', { + 'node-name': 'snap1' + }) + self.qsd.cmd('blockdev-del', { + 'node-name': 'snap2' + }) bench_thr.join()
tests/qemu-iotests/tests/graph-changes-while-io.out +2 -2
@@ -1,5 +1,5 @@ -.. +... ---------------------------------------------------------------------- -Ran 2 tests +Ran 3 tests OK
Signature · 1 line
2.39.5
Re: [PATCH 11/11] iotests/graph-changes-while-io: add test case with removal of lower snapshot
Am 08.05.2025 um 16:09 hat Fiona Ebner geschrieben:
Quoted 8 lines
From: Andrey Drobyshev <[email protected]>
This case is catching potential deadlock which takes place when job-dismiss is issued when I/O requests are processed in a separate iothread.
See https://mail.gnu.org/archive/html/qemu-devel/2025-04/msg04421.html
Signed-off-by Andrey Drobyshev <[email protected]>
Keeping Andrey in From: is fine, but it does need your S-o-b if you submit the patch.
tests/qemu-iotests/tests/graph-changes-while-io from iotests import imgfmt, qemu_img, qemu_img_create, qemu_io, \ · line 32
Quoted 11 lines
tests/qemu-iotests/tests/graph-changes-while-io +2
-0@@ -27,6 +27,8 @@ from iotests import imgfmt, qemu_img, qemu_img_create, qemu_io, \ top = os.path.join(iotests.test_dir, 'top.img') +snap1 = os.path.join(iotests.test_dir, 'snap1.img') +snap2 = os.path.join(iotests.test_dir, 'snap2.img') nbd_sock = os.path.join(iotests.sock_dir, 'nbd.sock')
top could be reused for snap2, it serves the same purpose in the existing tests. Maybe mid for snap1 would make more sense then.
The only other problem I can see is that the test forgets to remove the overlay image files (preexisting for top).
It did reproduce the problem on the first attempt, though, so: