Skip to content

Commit

Permalink
blockdev: mirror: avoid potential deadlock when using iothread
Browse files Browse the repository at this point in the history
The bdrv_getlength() function is a generated co-wrapper and uses
AIO_WAIT_WHILE() to wait for the spawned coroutine. AIO_WAIT_WHILE()
expects the lock to be acquired exactly once.

Fix a case where it may be acquired twice. This can happen when the
source node is explicitly specified as the @Replaces parameter or if the
source node is a filter node.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
Message-ID: <20231019131936.414246-4-f.ebner@proxmox.com>
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
  • Loading branch information
foxmox authored and kevmw committed Oct 31, 2023
1 parent e462c6d commit 3028238
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions blockdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -2968,6 +2968,7 @@ static void blockdev_mirror_common(const char *job_id, BlockDriverState *bs,

if (replaces) {
BlockDriverState *to_replace_bs;
AioContext *aio_context;
AioContext *replace_aio_context;
int64_t bs_size, replace_size;

Expand All @@ -2982,10 +2983,19 @@ static void blockdev_mirror_common(const char *job_id, BlockDriverState *bs,
return;
}

aio_context = bdrv_get_aio_context(bs);
replace_aio_context = bdrv_get_aio_context(to_replace_bs);
aio_context_acquire(replace_aio_context);
/*
* bdrv_getlength() is a co-wrapper and uses AIO_WAIT_WHILE. Be sure not
* to acquire the same AioContext twice.
*/
if (replace_aio_context != aio_context) {
aio_context_acquire(replace_aio_context);
}
replace_size = bdrv_getlength(to_replace_bs);
aio_context_release(replace_aio_context);
if (replace_aio_context != aio_context) {
aio_context_release(replace_aio_context);
}

if (replace_size < 0) {
error_setg_errno(errp, -replace_size,
Expand Down

0 comments on commit 3028238

Please sign in to comment.