Skip to content

Commit 6cd5c9d

Browse files
committed
block: ignore_bds_parents parameter for drain functions
In the future, bdrv_drained_all_begin/end() will drain all invidiual nodes separately rather than whole subtrees. This means that we don't want to propagate the drain to all parents any more: If the parent is a BDS, it will already be drained separately. Recursing to all parents is unnecessary work and would make it an O(n²) operation. Prepare the drain function for the changed drain_all by adding an ignore_bds_parents parameter to the internal implementation that prevents the propagation of the drain to BDS parents. We still (have to) propagate it to non-BDS parents like BlockBackends or Jobs because those are not drained separately. Signed-off-by: Kevin Wolf <[email protected]>
1 parent c8ca33d commit 6cd5c9d

File tree

5 files changed

+78
-44
lines changed

5 files changed

+78
-44
lines changed

block.c

+7-4
Original file line numberDiff line numberDiff line change
@@ -818,13 +818,13 @@ static char *bdrv_child_get_parent_desc(BdrvChild *c)
818818
static void bdrv_child_cb_drained_begin(BdrvChild *child)
819819
{
820820
BlockDriverState *bs = child->opaque;
821-
bdrv_do_drained_begin_quiesce(bs, NULL);
821+
bdrv_do_drained_begin_quiesce(bs, NULL, false);
822822
}
823823

824824
static bool bdrv_child_cb_drained_poll(BdrvChild *child)
825825
{
826826
BlockDriverState *bs = child->opaque;
827-
return bdrv_drain_poll(bs, false, NULL);
827+
return bdrv_drain_poll(bs, false, NULL, false);
828828
}
829829

830830
static void bdrv_child_cb_drained_end(BdrvChild *child)
@@ -908,6 +908,7 @@ static void bdrv_inherited_options(int *child_flags, QDict *child_options,
908908
}
909909

910910
const BdrvChildRole child_file = {
911+
.parent_is_bds = true,
911912
.get_parent_desc = bdrv_child_get_parent_desc,
912913
.inherit_options = bdrv_inherited_options,
913914
.drained_begin = bdrv_child_cb_drained_begin,
@@ -933,6 +934,7 @@ static void bdrv_inherited_fmt_options(int *child_flags, QDict *child_options,
933934
}
934935

935936
const BdrvChildRole child_format = {
937+
.parent_is_bds = true,
936938
.get_parent_desc = bdrv_child_get_parent_desc,
937939
.inherit_options = bdrv_inherited_fmt_options,
938940
.drained_begin = bdrv_child_cb_drained_begin,
@@ -1051,6 +1053,7 @@ static int bdrv_backing_update_filename(BdrvChild *c, BlockDriverState *base,
10511053
}
10521054

10531055
const BdrvChildRole child_backing = {
1056+
.parent_is_bds = true,
10541057
.get_parent_desc = bdrv_child_get_parent_desc,
10551058
.attach = bdrv_backing_attach,
10561059
.detach = bdrv_backing_detach,
@@ -4957,7 +4960,7 @@ void bdrv_set_aio_context(BlockDriverState *bs, AioContext *new_context)
49574960
AioContext *ctx = bdrv_get_aio_context(bs);
49584961

49594962
aio_disable_external(ctx);
4960-
bdrv_parent_drained_begin(bs, NULL);
4963+
bdrv_parent_drained_begin(bs, NULL, false);
49614964
bdrv_drain(bs); /* ensure there are no in-flight requests */
49624965

49634966
while (aio_poll(ctx, false)) {
@@ -4971,7 +4974,7 @@ void bdrv_set_aio_context(BlockDriverState *bs, AioContext *new_context)
49714974
*/
49724975
aio_context_acquire(new_context);
49734976
bdrv_attach_aio_context(bs, new_context);
4974-
bdrv_parent_drained_end(bs, NULL);
4977+
bdrv_parent_drained_end(bs, NULL, false);
49754978
aio_enable_external(ctx);
49764979
aio_context_release(new_context);
49774980
}

block/io.c

+53-35
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,13 @@
4141
static int coroutine_fn bdrv_co_do_pwrite_zeroes(BlockDriverState *bs,
4242
int64_t offset, int bytes, BdrvRequestFlags flags);
4343

44-
void bdrv_parent_drained_begin(BlockDriverState *bs, BdrvChild *ignore)
44+
void bdrv_parent_drained_begin(BlockDriverState *bs, BdrvChild *ignore,
45+
bool ignore_bds_parents)
4546
{
4647
BdrvChild *c, *next;
4748

4849
QLIST_FOREACH_SAFE(c, &bs->parents, next_parent, next) {
49-
if (c == ignore) {
50+
if (c == ignore || (ignore_bds_parents && c->role->parent_is_bds)) {
5051
continue;
5152
}
5253
if (c->role->drained_begin) {
@@ -55,12 +56,13 @@ void bdrv_parent_drained_begin(BlockDriverState *bs, BdrvChild *ignore)
5556
}
5657
}
5758

58-
void bdrv_parent_drained_end(BlockDriverState *bs, BdrvChild *ignore)
59+
void bdrv_parent_drained_end(BlockDriverState *bs, BdrvChild *ignore,
60+
bool ignore_bds_parents)
5961
{
6062
BdrvChild *c, *next;
6163

6264
QLIST_FOREACH_SAFE(c, &bs->parents, next_parent, next) {
63-
if (c == ignore) {
65+
if (c == ignore || (ignore_bds_parents && c->role->parent_is_bds)) {
6466
continue;
6567
}
6668
if (c->role->drained_end) {
@@ -69,13 +71,14 @@ void bdrv_parent_drained_end(BlockDriverState *bs, BdrvChild *ignore)
6971
}
7072
}
7173

72-
static bool bdrv_parent_drained_poll(BlockDriverState *bs, BdrvChild *ignore)
74+
static bool bdrv_parent_drained_poll(BlockDriverState *bs, BdrvChild *ignore,
75+
bool ignore_bds_parents)
7376
{
7477
BdrvChild *c, *next;
7578
bool busy = false;
7679

7780
QLIST_FOREACH_SAFE(c, &bs->parents, next_parent, next) {
78-
if (c == ignore) {
81+
if (c == ignore || (ignore_bds_parents && c->role->parent_is_bds)) {
7982
continue;
8083
}
8184
if (c->role->drained_poll) {
@@ -167,6 +170,7 @@ typedef struct {
167170
bool recursive;
168171
bool poll;
169172
BdrvChild *parent;
173+
bool ignore_bds_parents;
170174
} BdrvCoDrainData;
171175

172176
static void coroutine_fn bdrv_drain_invoke_entry(void *opaque)
@@ -220,11 +224,11 @@ static void bdrv_drain_invoke(BlockDriverState *bs, bool begin)
220224

221225
/* Returns true if BDRV_POLL_WHILE() should go into a blocking aio_poll() */
222226
bool bdrv_drain_poll(BlockDriverState *bs, bool recursive,
223-
BdrvChild *ignore_parent)
227+
BdrvChild *ignore_parent, bool ignore_bds_parents)
224228
{
225229
BdrvChild *child, *next;
226230

227-
if (bdrv_parent_drained_poll(bs, ignore_parent)) {
231+
if (bdrv_parent_drained_poll(bs, ignore_parent, ignore_bds_parents)) {
228232
return true;
229233
}
230234

@@ -233,8 +237,9 @@ bool bdrv_drain_poll(BlockDriverState *bs, bool recursive,
233237
}
234238

235239
if (recursive) {
240+
assert(!ignore_bds_parents);
236241
QLIST_FOREACH_SAFE(child, &bs->children, next, next) {
237-
if (bdrv_drain_poll(child->bs, recursive, child)) {
242+
if (bdrv_drain_poll(child->bs, recursive, child, false)) {
238243
return true;
239244
}
240245
}
@@ -250,13 +255,14 @@ static bool bdrv_drain_poll_top_level(BlockDriverState *bs, bool recursive,
250255
* have executed. */
251256
while (aio_poll(bs->aio_context, false));
252257

253-
return bdrv_drain_poll(bs, recursive, ignore_parent);
258+
return bdrv_drain_poll(bs, recursive, ignore_parent, false);
254259
}
255260

256261
static void bdrv_do_drained_begin(BlockDriverState *bs, bool recursive,
257-
BdrvChild *parent, bool poll);
262+
BdrvChild *parent, bool ignore_bds_parents,
263+
bool poll);
258264
static void bdrv_do_drained_end(BlockDriverState *bs, bool recursive,
259-
BdrvChild *parent);
265+
BdrvChild *parent, bool ignore_bds_parents);
260266

261267
static void bdrv_co_drain_bh_cb(void *opaque)
262268
{
@@ -267,9 +273,11 @@ static void bdrv_co_drain_bh_cb(void *opaque)
267273
if (bs) {
268274
bdrv_dec_in_flight(bs);
269275
if (data->begin) {
270-
bdrv_do_drained_begin(bs, data->recursive, data->parent, data->poll);
276+
bdrv_do_drained_begin(bs, data->recursive, data->parent,
277+
data->ignore_bds_parents, data->poll);
271278
} else {
272-
bdrv_do_drained_end(bs, data->recursive, data->parent);
279+
bdrv_do_drained_end(bs, data->recursive, data->parent,
280+
data->ignore_bds_parents);
273281
}
274282
} else {
275283
assert(data->begin);
@@ -282,7 +290,9 @@ static void bdrv_co_drain_bh_cb(void *opaque)
282290

283291
static void coroutine_fn bdrv_co_yield_to_drain(BlockDriverState *bs,
284292
bool begin, bool recursive,
285-
BdrvChild *parent, bool poll)
293+
BdrvChild *parent,
294+
bool ignore_bds_parents,
295+
bool poll)
286296
{
287297
BdrvCoDrainData data;
288298

@@ -297,6 +307,7 @@ static void coroutine_fn bdrv_co_yield_to_drain(BlockDriverState *bs,
297307
.begin = begin,
298308
.recursive = recursive,
299309
.parent = parent,
310+
.ignore_bds_parents = ignore_bds_parents,
300311
.poll = poll,
301312
};
302313
if (bs) {
@@ -312,7 +323,7 @@ static void coroutine_fn bdrv_co_yield_to_drain(BlockDriverState *bs,
312323
}
313324

314325
void bdrv_do_drained_begin_quiesce(BlockDriverState *bs,
315-
BdrvChild *parent)
326+
BdrvChild *parent, bool ignore_bds_parents)
316327
{
317328
assert(!qemu_in_coroutine());
318329

@@ -321,26 +332,30 @@ void bdrv_do_drained_begin_quiesce(BlockDriverState *bs,
321332
aio_disable_external(bdrv_get_aio_context(bs));
322333
}
323334

324-
bdrv_parent_drained_begin(bs, parent);
335+
bdrv_parent_drained_begin(bs, parent, ignore_bds_parents);
325336
bdrv_drain_invoke(bs, true);
326337
}
327338

328339
static void bdrv_do_drained_begin(BlockDriverState *bs, bool recursive,
329-
BdrvChild *parent, bool poll)
340+
BdrvChild *parent, bool ignore_bds_parents,
341+
bool poll)
330342
{
331343
BdrvChild *child, *next;
332344

333345
if (qemu_in_coroutine()) {
334-
bdrv_co_yield_to_drain(bs, true, recursive, parent, poll);
346+
bdrv_co_yield_to_drain(bs, true, recursive, parent, ignore_bds_parents,
347+
poll);
335348
return;
336349
}
337350

338-
bdrv_do_drained_begin_quiesce(bs, parent);
351+
bdrv_do_drained_begin_quiesce(bs, parent, ignore_bds_parents);
339352

340353
if (recursive) {
354+
assert(!ignore_bds_parents);
341355
bs->recursive_quiesce_counter++;
342356
QLIST_FOREACH_SAFE(child, &bs->children, next, next) {
343-
bdrv_do_drained_begin(child->bs, true, child, false);
357+
bdrv_do_drained_begin(child->bs, true, child, ignore_bds_parents,
358+
false);
344359
}
345360
}
346361

@@ -354,64 +369,67 @@ static void bdrv_do_drained_begin(BlockDriverState *bs, bool recursive,
354369
* nodes.
355370
*/
356371
if (poll) {
372+
assert(!ignore_bds_parents);
357373
BDRV_POLL_WHILE(bs, bdrv_drain_poll_top_level(bs, recursive, parent));
358374
}
359375
}
360376

361377
void bdrv_drained_begin(BlockDriverState *bs)
362378
{
363-
bdrv_do_drained_begin(bs, false, NULL, true);
379+
bdrv_do_drained_begin(bs, false, NULL, false, true);
364380
}
365381

366382
void bdrv_subtree_drained_begin(BlockDriverState *bs)
367383
{
368-
bdrv_do_drained_begin(bs, true, NULL, true);
384+
bdrv_do_drained_begin(bs, true, NULL, false, true);
369385
}
370386

371-
void bdrv_do_drained_end(BlockDriverState *bs, bool recursive,
372-
BdrvChild *parent)
387+
static void bdrv_do_drained_end(BlockDriverState *bs, bool recursive,
388+
BdrvChild *parent, bool ignore_bds_parents)
373389
{
374390
BdrvChild *child, *next;
375391
int old_quiesce_counter;
376392

377393
if (qemu_in_coroutine()) {
378-
bdrv_co_yield_to_drain(bs, false, recursive, parent, false);
394+
bdrv_co_yield_to_drain(bs, false, recursive, parent, ignore_bds_parents,
395+
false);
379396
return;
380397
}
381398
assert(bs->quiesce_counter > 0);
382399
old_quiesce_counter = atomic_fetch_dec(&bs->quiesce_counter);
383400

384401
/* Re-enable things in child-to-parent order */
385402
bdrv_drain_invoke(bs, false);
386-
bdrv_parent_drained_end(bs, parent);
403+
bdrv_parent_drained_end(bs, parent, ignore_bds_parents);
387404
if (old_quiesce_counter == 1) {
388405
aio_enable_external(bdrv_get_aio_context(bs));
389406
}
390407

391408
if (recursive) {
409+
assert(!ignore_bds_parents);
392410
bs->recursive_quiesce_counter--;
393411
QLIST_FOREACH_SAFE(child, &bs->children, next, next) {
394-
bdrv_do_drained_end(child->bs, true, child);
412+
bdrv_do_drained_end(child->bs, true, child, ignore_bds_parents);
395413
}
396414
}
397415
}
398416

399417
void bdrv_drained_end(BlockDriverState *bs)
400418
{
401-
bdrv_do_drained_end(bs, false, NULL);
419+
bdrv_do_drained_end(bs, false, NULL, false);
402420
}
403421

404422
void bdrv_subtree_drained_end(BlockDriverState *bs)
405423
{
406-
bdrv_do_drained_end(bs, true, NULL);
424+
bdrv_do_drained_end(bs, true, NULL, false);
407425
}
408426

409427
void bdrv_apply_subtree_drain(BdrvChild *child, BlockDriverState *new_parent)
410428
{
411429
int i;
412430

413431
for (i = 0; i < new_parent->recursive_quiesce_counter; i++) {
414-
bdrv_do_drained_begin(child->bs, true, child, true);
432+
bdrv_do_drained_begin(child->bs, true, child, false, true);
415433
}
416434
}
417435

@@ -420,7 +438,7 @@ void bdrv_unapply_subtree_drain(BdrvChild *child, BlockDriverState *old_parent)
420438
int i;
421439

422440
for (i = 0; i < old_parent->recursive_quiesce_counter; i++) {
423-
bdrv_do_drained_end(child->bs, true, child);
441+
bdrv_do_drained_end(child->bs, true, child, false);
424442
}
425443
}
426444

@@ -472,7 +490,7 @@ void bdrv_drain_all_begin(void)
472490
BdrvNextIterator it;
473491

474492
if (qemu_in_coroutine()) {
475-
bdrv_co_yield_to_drain(NULL, true, false, NULL, true);
493+
bdrv_co_yield_to_drain(NULL, true, false, NULL, false, true);
476494
return;
477495
}
478496

@@ -486,7 +504,7 @@ void bdrv_drain_all_begin(void)
486504
AioContext *aio_context = bdrv_get_aio_context(bs);
487505

488506
aio_context_acquire(aio_context);
489-
bdrv_do_drained_begin(bs, true, NULL, true);
507+
bdrv_do_drained_begin(bs, true, NULL, false, true);
490508
aio_context_release(aio_context);
491509
}
492510

@@ -504,7 +522,7 @@ void bdrv_drain_all_end(void)
504522
AioContext *aio_context = bdrv_get_aio_context(bs);
505523

506524
aio_context_acquire(aio_context);
507-
bdrv_do_drained_end(bs, true, NULL);
525+
bdrv_do_drained_end(bs, true, NULL, false);
508526
aio_context_release(aio_context);
509527
}
510528
}

block/vvfat.c

+1
Original file line numberDiff line numberDiff line change
@@ -3134,6 +3134,7 @@ static void vvfat_qcow_options(int *child_flags, QDict *child_options,
31343134
}
31353135

31363136
static const BdrvChildRole child_vvfat_qcow = {
3137+
.parent_is_bds = true,
31373138
.inherit_options = vvfat_qcow_options,
31383139
};
31393140

0 commit comments

Comments
 (0)