Skip to content

Commit 6cf42ca

Browse files
committed
block: Acquire AioContexts during bdrv_reopen_multiple()
As the BlockReopenQueue can contain nodes in multiple AioContexts, only one of which may be locked when AIO_WAIT_WHILE() can be called, we can't let the caller lock the right contexts. Instead, individually lock the AioContext of a single node when iterating the queue. Reintroduce bdrv_reopen() as a wrapper for reopening a single node that drains the node and temporarily drops the AioContext lock for bdrv_reopen_multiple(). Signed-off-by: Kevin Wolf <[email protected]> Reviewed-by: Vladimir Sementsov-Ogievskiy <[email protected]> Message-Id: <[email protected]> Signed-off-by: Kevin Wolf <[email protected]>
1 parent ab5b522 commit 6cf42ca

File tree

5 files changed

+57
-13
lines changed

5 files changed

+57
-13
lines changed

block.c

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4124,19 +4124,26 @@ void bdrv_reopen_queue_free(BlockReopenQueue *bs_queue)
41244124
*
41254125
* All affected nodes must be drained between bdrv_reopen_queue() and
41264126
* bdrv_reopen_multiple().
4127+
*
4128+
* To be called from the main thread, with all other AioContexts unlocked.
41274129
*/
41284130
int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp)
41294131
{
41304132
int ret = -1;
41314133
BlockReopenQueueEntry *bs_entry, *next;
4134+
AioContext *ctx;
41324135
Transaction *tran = tran_new();
41334136
g_autoptr(GHashTable) found = NULL;
41344137
g_autoptr(GSList) refresh_list = NULL;
41354138

4139+
assert(qemu_get_current_aio_context() == qemu_get_aio_context());
41364140
assert(bs_queue != NULL);
41374141

41384142
QTAILQ_FOREACH(bs_entry, bs_queue, entry) {
4143+
ctx = bdrv_get_aio_context(bs_entry->state.bs);
4144+
aio_context_acquire(ctx);
41394145
ret = bdrv_flush(bs_entry->state.bs);
4146+
aio_context_release(ctx);
41404147
if (ret < 0) {
41414148
error_setg_errno(errp, -ret, "Error flushing drive");
41424149
goto abort;
@@ -4145,7 +4152,10 @@ int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp)
41454152

41464153
QTAILQ_FOREACH(bs_entry, bs_queue, entry) {
41474154
assert(bs_entry->state.bs->quiesce_counter > 0);
4155+
ctx = bdrv_get_aio_context(bs_entry->state.bs);
4156+
aio_context_acquire(ctx);
41484157
ret = bdrv_reopen_prepare(&bs_entry->state, bs_queue, tran, errp);
4158+
aio_context_release(ctx);
41494159
if (ret < 0) {
41504160
goto abort;
41514161
}
@@ -4188,7 +4198,10 @@ int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp)
41884198
* to first element.
41894199
*/
41904200
QTAILQ_FOREACH_REVERSE(bs_entry, bs_queue, entry) {
4201+
ctx = bdrv_get_aio_context(bs_entry->state.bs);
4202+
aio_context_acquire(ctx);
41914203
bdrv_reopen_commit(&bs_entry->state);
4204+
aio_context_release(ctx);
41924205
}
41934206

41944207
tran_commit(tran);
@@ -4197,7 +4210,10 @@ int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp)
41974210
BlockDriverState *bs = bs_entry->state.bs;
41984211

41994212
if (bs->drv->bdrv_reopen_commit_post) {
4213+
ctx = bdrv_get_aio_context(bs);
4214+
aio_context_acquire(ctx);
42004215
bs->drv->bdrv_reopen_commit_post(&bs_entry->state);
4216+
aio_context_release(ctx);
42014217
}
42024218
}
42034219

@@ -4208,7 +4224,10 @@ int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp)
42084224
tran_abort(tran);
42094225
QTAILQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) {
42104226
if (bs_entry->prepared) {
4227+
ctx = bdrv_get_aio_context(bs_entry->state.bs);
4228+
aio_context_acquire(ctx);
42114229
bdrv_reopen_abort(&bs_entry->state);
4230+
aio_context_release(ctx);
42124231
}
42134232
}
42144233

@@ -4218,23 +4237,39 @@ int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp)
42184237
return ret;
42194238
}
42204239

4221-
int bdrv_reopen_set_read_only(BlockDriverState *bs, bool read_only,
4222-
Error **errp)
4240+
int bdrv_reopen(BlockDriverState *bs, QDict *opts, bool keep_old_opts,
4241+
Error **errp)
42234242
{
4224-
int ret;
4243+
AioContext *ctx = bdrv_get_aio_context(bs);
42254244
BlockReopenQueue *queue;
4226-
QDict *opts = qdict_new();
4227-
4228-
qdict_put_bool(opts, BDRV_OPT_READ_ONLY, read_only);
4245+
int ret;
42294246

42304247
bdrv_subtree_drained_begin(bs);
4231-
queue = bdrv_reopen_queue(NULL, bs, opts, true);
4248+
if (ctx != qemu_get_aio_context()) {
4249+
aio_context_release(ctx);
4250+
}
4251+
4252+
queue = bdrv_reopen_queue(NULL, bs, opts, keep_old_opts);
42324253
ret = bdrv_reopen_multiple(queue, errp);
4254+
4255+
if (ctx != qemu_get_aio_context()) {
4256+
aio_context_acquire(ctx);
4257+
}
42334258
bdrv_subtree_drained_end(bs);
42344259

42354260
return ret;
42364261
}
42374262

4263+
int bdrv_reopen_set_read_only(BlockDriverState *bs, bool read_only,
4264+
Error **errp)
4265+
{
4266+
QDict *opts = qdict_new();
4267+
4268+
qdict_put_bool(opts, BDRV_OPT_READ_ONLY, read_only);
4269+
4270+
return bdrv_reopen(bs, opts, true, errp);
4271+
}
4272+
42384273
/*
42394274
* Take a BDRVReopenState and check if the value of 'backing' in the
42404275
* reopen_state->options QDict is valid or not.

block/replication.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,14 @@ static void reopen_backing_file(BlockDriverState *bs, bool writable,
390390
}
391391

392392
if (reopen_queue) {
393+
AioContext *ctx = bdrv_get_aio_context(bs);
394+
if (ctx != qemu_get_aio_context()) {
395+
aio_context_release(ctx);
396+
}
393397
bdrv_reopen_multiple(reopen_queue, errp);
398+
if (ctx != qemu_get_aio_context()) {
399+
aio_context_acquire(ctx);
400+
}
394401
}
395402

396403
bdrv_subtree_drained_end(s->hidden_disk->bs);

blockdev.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3592,8 +3592,13 @@ void qmp_x_blockdev_reopen(BlockdevOptions *options, Error **errp)
35923592
ctx = bdrv_get_aio_context(bs);
35933593
aio_context_acquire(ctx);
35943594
bdrv_subtree_drained_begin(bs);
3595+
aio_context_release(ctx);
3596+
35953597
queue = bdrv_reopen_queue(NULL, bs, qdict, false);
35963598
bdrv_reopen_multiple(queue, errp);
3599+
3600+
ctx = bdrv_get_aio_context(bs);
3601+
aio_context_acquire(ctx);
35973602
bdrv_subtree_drained_end(bs);
35983603
aio_context_release(ctx);
35993604

include/block/block.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,8 @@ BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue,
388388
bool keep_old_opts);
389389
void bdrv_reopen_queue_free(BlockReopenQueue *bs_queue);
390390
int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp);
391+
int bdrv_reopen(BlockDriverState *bs, QDict *opts, bool keep_old_opts,
392+
Error **errp);
391393
int bdrv_reopen_set_read_only(BlockDriverState *bs, bool read_only,
392394
Error **errp);
393395
int bdrv_pwrite_zeroes(BdrvChild *child, int64_t offset,

qemu-io-cmds.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2116,8 +2116,6 @@ static int reopen_f(BlockBackend *blk, int argc, char **argv)
21162116
bool writethrough = !blk_enable_write_cache(blk);
21172117
bool has_rw_option = false;
21182118
bool has_cache_option = false;
2119-
2120-
BlockReopenQueue *brq;
21212119
Error *local_err = NULL;
21222120

21232121
while ((c = getopt(argc, argv, "c:o:rw")) != -1) {
@@ -2210,10 +2208,7 @@ static int reopen_f(BlockBackend *blk, int argc, char **argv)
22102208
qdict_put_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, flags & BDRV_O_NO_FLUSH);
22112209
}
22122210

2213-
bdrv_subtree_drained_begin(bs);
2214-
brq = bdrv_reopen_queue(NULL, bs, opts, true);
2215-
bdrv_reopen_multiple(brq, &local_err);
2216-
bdrv_subtree_drained_end(bs);
2211+
bdrv_reopen(bs, opts, true, &local_err);
22172212

22182213
if (local_err) {
22192214
error_report_err(local_err);

0 commit comments

Comments
 (0)