blockjob: introduce .drain callback for jobs
PATCH 1/1 blockjob: introduce .drain callback for jobs
This is required to decouple block jobs from running in an AioContext. With multiqueue block devices, a BlockDriverState does not really belong to a single AioContext.
The solution is to first wait until all I/O operations are complete; then loop in the main thread for the block job to complete entirely.
---
This patch comes from the multiqueue series. It is self-contained so I'm sending it now; it achieves the purpose of getting rid of block_job_get_aio_context, so that any other use of it would be more visible. But if you all think it's premature, I'm okay with keeping it out some more.
block/backup.c | 16 ++++++++++++++++ block/mirror.c | 34 ++++++++++++++++++++++++++-------- blockjob.c | 37 ++++++++++++++++++++----------------- include/block/blockjob.h | 7 +++++++ 4 files changed, 69 insertions(+), 25 deletions(-)
block/backup.c +16 -0
@@ -300,6 +300,20 @@ void backup_cow_request_end(CowRequest *req)cow_request_end (req); } +static void backup_drain (BlockJob *job) +{ +BackupBlockJob *s =container_of (job, BackupBlockJob, common); + +/* Need to keep a reference in case blk_drain triggers execution +* of backup_complete... +*/ +if (s->target) { +blk_ref (s->target); +blk_drain (s->target); +blk_unref (s->target); + } +} +static const BlockJobDriver backup_job_driver = { .instance_size =sizeof (BackupBlockJob), .job_type =BLOCK_JOB_TYPE_BACKUP ,
@@ -307,6 +321,7 @@ static const BlockJobDriver backup_job_driver = { .commit = backup_commit, .abort = backup_abort, .attached_aio_context = backup_attached_aio_context, + .drain = backup_drain, }; static BlockErrorAction backup_error_action(BackupBlockJob *job,
@@ -331,6 +346,7 @@ static void backup_complete(BlockJob *job, void *opaque)BackupCompleteData *data = opaque;blk_unref (s->target); + s->target =NULL ;block_job_completed (job, data->ret);g_free (data);
block/mirror.c +26 -8
@@ -471,7 +471,11 @@ static void mirror_free_init(MirrorBlockJob *s) } } -static void mirror_drain (MirrorBlockJob *s) +/* This is also used for the .pause callback. There is no matching +* mirror_resume() because mirror_run() will begin iterating again +* when the job is resumed. +*/ +static void mirror_wait_for_all_io (MirrorBlockJob *s) {while (s->in_flight >0 ) {mirror_wait_for_io (s);
@@ -530,6 +534,7 @@ static void mirror_exit(BlockJob *job, void *opaque)g_free (s->replaces);bdrv_op_unblock_all (target_bs, s->common.blocker);blk_unref (s->target); + s->target =NULL ;block_job_completed (&s->common, data->ret);g_free (data);bdrv_drained_end (src);
@@ -584,7 +589,7 @@ static int coroutine_fn mirror_dirty_init(MirrorBlockJob *s) sector_num += nb_sectors; } -mirror_drain (s); +mirror_wait_for_all_io (s); }/* First part, loop on the sectors and initialize the dirty bitmap. */
@@ -802,7 +807,7 @@ immediate_exit:*/ assert (ret <0 || (!s->synced &&block_job_is_cancelled (&s->common)));assert (need_drain); -mirror_drain (s); +mirror_wait_for_all_io (s); }assert (s->in_flight ==0 );
@@ -887,14 +892,11 @@ static void mirror_complete(BlockJob *job, Error **errp)block_job_enter (&s->common); } -/* There is no matching mirror_resume() because mirror_run() will begin -* iterating again when the job is resumed. -*/ -static void coroutine_fn mirror_pause(BlockJob *job) +static void mirror_pause (BlockJob *job) {MirrorBlockJob *s =container_of (job, MirrorBlockJob, common); -mirror_drain (s); +mirror_wait_for_all_io (s); }static void mirror_attached_aio_context (BlockJob *job,AioContext *new_context)
@@ -904,6 +906,20 @@ static void mirror_attached_aio_context(BlockJob *job, AioContext *new_context)blk_set_aio_context (s->target, new_context); } +static void mirror_drain (BlockJob *job) +{ +MirrorBlockJob *s =container_of (job, MirrorBlockJob, common); + +/* Need to keep a reference in case blk_drain triggers execution +* of mirror_complete... +*/ +if (s->target) { +blk_ref (s->target); +blk_drain (s->target); +blk_unref (s->target); + } +} +static const BlockJobDriver mirror_job_driver = { .instance_size =sizeof (MirrorBlockJob), .job_type =BLOCK_JOB_TYPE_MIRROR ,
@@ -911,6 +927,7 @@ static const BlockJobDriver mirror_job_driver = { .complete = mirror_complete, .pause = mirror_pause, .attached_aio_context = mirror_attached_aio_context, + .drain = mirror_drain, };static const BlockJobDriver commit_active_job_driver = {
@@ -920,6 +937,7 @@ static const BlockJobDriver commit_active_job_driver = { .complete = mirror_complete, .pause = mirror_pause, .attached_aio_context = mirror_attached_aio_context, + .drain = mirror_drain, }; static void mirror_start_job(const char *job_id, BlockDriverState *bs,
blockjob.c +20 -17
@@ -74,17 +74,6 @@ BlockJob *block_job_get(const char *id)return NULL ; } -/* Normally the job runs in its BlockBackend's AioContext. The exception is -* block_job_defer_to_main_loop() where it runs in the QEMU main loop. Code -* that supports both cases uses this helper function. -*/ -static AioContext *block_job_get_aio_context (BlockJob *job) -{ -return job->deferred_to_main_loop ? -qemu_get_aio_context () : -blk_get_aio_context (job->blk); -} -static void block_job_attached_aio_context (AioContext *new_context,void *opaque) {
@@ -97,6 +86,17 @@ static void block_job_attached_aio_context(AioContext *new_context,block_job_resume (job); } +static void block_job_drain (BlockJob *job) +{ +/* If job is !job->busy this kicks it into the next pause point. */ +block_job_enter (job); + +blk_drain (job->blk); +if (job->driver->drain) { + job->driver->drain (job); + } +} +static void block_job_detach_aio_context (void *opaque) {BlockJob *job = opaque;
@@ -106,12 +106,8 @@ static void block_job_detach_aio_context(void *opaque)block_job_pause (job); -if (!job->paused) { -/* If job is !job->busy this kicks it into the next pause point. */ -block_job_enter (job); - }while (!job->paused && !job->completed) { -aio_poll (block_job_get_aio_context (job), true); +block_job_drain (job); }block_job_unref (job);
@@ -413,14 +409,21 @@ static int block_job_finish_sync(BlockJob *job,assert (blk_bs (job->blk)->job == job);block_job_ref (job); +finish (job, &local_err);if (local_err) {error_propagate (errp, local_err);block_job_unref (job);return -EBUSY ; } +/* block_job_drain calls block_job_enter, and it should be enough to +* induce progress until the job completes or moves to the main thread. +*/ +while (!job->deferred_to_main_loop && !job->completed) { +block_job_drain (job); + }while (!job->completed) { -aio_poll (block_job_get_aio_context (job), true); +aio_poll (qemu_get_aio_context (), true); } ret = (job->cancelled && job->ret ==0 ) ? -ECANCELED : job->ret;block_job_unref (job);
include/block/blockjob.h +7 -0
@@ -92,6 +92,13 @@ typedef struct BlockJobDriver {* besides job->blk to the new AioContext. */ void (*attached_aio_context)(BlockJob *job,AioContext *new_context); + +/* +* If the callback is not NULL, it will be invoked when the job has to be +* synchronously cancelled or completed; it should drain BlockDriverStates +* as required to ensure progress. +*/ +void (*drain)(BlockJob *job); } BlockJobDriver;/**
Signature · 1 line
2.7.4
PATCH 1/1 block: change drain to look only at one child at a time
bdrv_requests_pending is checking children to also wait until internal requests (such as metadata writes) have completed. However, checking children is in general overkill. Children requests can be of two kinds:
- requests caused by an operation on bs, e.g. a bdrv_aio_write to bs causing a write to bs->file->bs. In this case, the parent's in_flight count will always be incremented by at least one for every request in the child.
- asynchronous metadata writes or flushes. Such writes can be started even if bs's in_flight count is zero, but not after the .bdrv_drain callback has been invoked.
This patch therefore changes bdrv_drain to finish I/O in the parent (after which the parent's in_flight will be locked to zero), call bdrv_drain (after which the parent will not generate I/O on the child anymore), and then wait for internal I/O in the children to complete.
--- block/io.c | 54 ++++++++++++++++++++++++++++++++++++------------------ block/qed.c | 16 +++++++++++++++- 2 files changed, 51 insertions(+), 19 deletions(-)
block/io.c +36 -18
@@ -156,16 +156,33 @@ bool bdrv_requests_pending(BlockDriverState *bs)return false; } -static void bdrv_drain_recurse (BlockDriverState *bs) +static bool bdrv_drain_poll (BlockDriverState *bs) +{ +bool waited = false; + +while (atomic_read (&bs->in_flight) >0 ) { +aio_poll (bdrv_get_aio_context (bs), true); + waited = true; + } +return waited; +} + +static bool bdrv_drain_io_recurse (BlockDriverState *bs) {BdrvChild *child; +bool waited; + + waited =bdrv_drain_poll (bs);if (bs->drv && bs->drv->bdrv_drain) { bs->drv->bdrv_drain (bs); } + QLIST_FOREACH(child, &bs->children, next) { -bdrv_drain_recurse (child->bs); + waited |=bdrv_drain_io_recurse (child->bs); } + +return waited; } typedef struct {
@@ -174,14 +191,6 @@ typedef struct {bool done; } BdrvCoDrainData; -static void bdrv_drain_poll (BlockDriverState *bs) -{ -while (bdrv_requests_pending (bs)) { -/* Keep iterating */ -aio_poll (bdrv_get_aio_context (bs), true); - } -} -static void bdrv_co_drain_bh_cb (void *opaque) {BdrvCoDrainData *data = opaque;
@@ -215,6 +224,20 @@ static void coroutine_fn bdrv_co_yield_to_drain(BlockDriverState *bs)assert (data.done); } +static void bdrv_co_drain_io_recurse (BlockDriverState *bs) +{ +BdrvChild *child; + +bdrv_co_yield_to_drain (bs); +if (bs->drv && bs->drv->bdrv_drain) { + bs->drv->bdrv_drain (bs); + } + + QLIST_FOREACH(child, &bs->children, next) { +bdrv_co_drain_io_recurse (child->bs); + } +} +void bdrv_drained_begin (BlockDriverState *bs) {if (!bs->quiesce_counter++) {
@@ -223,11 +246,10 @@ void bdrv_drained_begin(BlockDriverState *bs) }bdrv_io_unplugged_begin (bs); -bdrv_drain_recurse (bs);if (qemu_in_coroutine ()) { -bdrv_co_yield_to_drain (bs); +bdrv_co_drain_io_recurse (bs); }else { -bdrv_drain_poll (bs); +bdrv_drain_io_recurse (bs); }bdrv_io_unplugged_end (bs); }
@@ -296,7 +318,6 @@ void bdrv_drain_all(void)aio_context_acquire (aio_context);bdrv_parent_drained_begin (bs);bdrv_io_unplugged_begin (bs); -bdrv_drain_recurse (bs);aio_context_release (aio_context);if (!g_slist_find (aio_ctxs, aio_context)) {
@@ -319,10 +340,7 @@ void bdrv_drain_all(void)aio_context_acquire (aio_context);for (bs =bdrv_first (&it); bs; bs =bdrv_next (&it)) {if (aio_context ==bdrv_get_aio_context (bs)) { -if (bdrv_requests_pending (bs)) { -aio_poll (aio_context, true); - waited = true; - } + waited |=bdrv_drain_io_recurse (bs); } }aio_context_release (aio_context);
block/qed.c +15 -1
@@ -336,7 +336,7 @@ static void qed_need_check_timer_cb(void *opaque)qed_plug_allocating_write_reqs (s);/* Ensure writes are on disk before clearing flag */ -bdrv_aio_flush (s->bs, qed_clear_need_check, s); +bdrv_aio_flush (s->bs->file->bs, qed_clear_need_check, s); }static void qed_start_need_check_timer (BDRVQEDState *s)
@@ -378,6 +378,19 @@ static void bdrv_qed_attach_aio_context(BlockDriverState *bs, } } +static void bdrv_qed_drain (BlockDriverState *bs) +{ +BDRVQEDState *s = bs->opaque; + +/* Fire the timer immediately in order to start doing I/O as soon as the +* header is flushed. +*/ +if (s->need_check_timer &&timer_pending (s->need_check_timer)) { +qed_cancel_need_check_timer (s); +qed_need_check_timer_cb (s); + } +} +static int bdrv_qed_open (BlockDriverState *bs,QDict *options,int flags,Error **errp) {
@@ -1668,6 +1681,7 @@ static BlockDriver bdrv_qed = { .bdrv_check = bdrv_qed_check, .bdrv_detach_aio_context = bdrv_qed_detach_aio_context, .bdrv_attach_aio_context = bdrv_qed_attach_aio_context, + .bdrv_drain = bdrv_qed_drain, };static void bdrv_qed_init (void )
Signature · 1 line
2.7.4
Re: [Qemu-devel] [PATCH] block: change drain to look only at one child at a time
Please disregard this, I'll send it soon as part of the correct series.
Paolo
Quoted 176 lines · Paolo Bonzini
bdrv_requests_pendingis checking children to also wait until internal requests (such as metadata writes) have completed. However, checking children is in general overkill. Children requests can be of two kinds:- requests caused by an operation on bs, e.g. a
bdrv_aio_writeto bs causing a write tobs->file->bs. In this case, the parent'sin_flightcount will always be incremented by at least one for every request in the child.- asynchronous metadata writes or flushes. Such writes can be started even if bs's
in_flightcount is zero, but not after the .bdrv_drain callback has been invoked.This patch therefore changes
bdrv_drainto finish I/O in the parent (after which the parent'sin_flightwill be locked to zero), callbdrv_drain(after which the parent will not generate I/O on the child anymore), and then wait for internal I/O in the children to complete.Signed-off-by Paolo Bonzini <[email protected]>--- block/io.c | 54 ++++++++++++++++++++++++++++++++++++------------------ block/qed.c | 16 +++++++++++++++- 2 files changed, 51 insertions(+), 19 deletions(-)block/io.c +36
-18@@ -156,16 +156,33 @@ bool bdrv_requests_pending(BlockDriverState *bs)return false; } -static void bdrv_drain_recurse (BlockDriverState *bs) +static bool bdrv_drain_poll (BlockDriverState *bs) +{ +bool waited = false; + +while (atomic_read (&bs->in_flight) >0 ) { +aio_poll (bdrv_get_aio_context (bs), true); + waited = true; + } +return waited; +} + +static bool bdrv_drain_io_recurse (BlockDriverState *bs) {BdrvChild *child; +bool waited; + + waited =bdrv_drain_poll (bs);if (bs->drv && bs->drv->bdrv_drain) { bs->drv->bdrv_drain (bs); } + QLIST_FOREACH(child, &bs->children, next) { -bdrv_drain_recurse (child->bs); + waited |=bdrv_drain_io_recurse (child->bs); } + +return waited; } typedef struct {@@ -174,14 +191,6 @@ typedef struct {bool done; } BdrvCoDrainData; -static void bdrv_drain_poll (BlockDriverState *bs) -{ -while (bdrv_requests_pending (bs)) { -/* Keep iterating */ -aio_poll (bdrv_get_aio_context (bs), true); - } -} -static void bdrv_co_drain_bh_cb (void *opaque) {BdrvCoDrainData *data = opaque;@@ -215,6 +224,20 @@ static void coroutine_fn bdrv_co_yield_to_drain(BlockDriverState *bs)assert (data.done); } +static void bdrv_co_drain_io_recurse (BlockDriverState *bs) +{ +BdrvChild *child; + +bdrv_co_yield_to_drain (bs); +if (bs->drv && bs->drv->bdrv_drain) { + bs->drv->bdrv_drain (bs); + } + + QLIST_FOREACH(child, &bs->children, next) { +bdrv_co_drain_io_recurse (child->bs); + } +} +void bdrv_drained_begin (BlockDriverState *bs) {if (!bs->quiesce_counter++) {@@ -223,11 +246,10 @@ void bdrv_drained_begin(BlockDriverState *bs) }bdrv_io_unplugged_begin (bs); -bdrv_drain_recurse (bs);if (qemu_in_coroutine ()) { -bdrv_co_yield_to_drain (bs); +bdrv_co_drain_io_recurse (bs); }else { -bdrv_drain_poll (bs); +bdrv_drain_io_recurse (bs); }bdrv_io_unplugged_end (bs); }@@ -296,7 +318,6 @@ void bdrv_drain_all(void)aio_context_acquire (aio_context);bdrv_parent_drained_begin (bs);bdrv_io_unplugged_begin (bs); -bdrv_drain_recurse (bs);aio_context_release (aio_context);if (!g_slist_find (aio_ctxs, aio_context)) {@@ -319,10 +340,7 @@ void bdrv_drain_all(void)aio_context_acquire (aio_context);for (bs =bdrv_first (&it); bs; bs =bdrv_next (&it)) {if (aio_context ==bdrv_get_aio_context (bs)) { -if (bdrv_requests_pending (bs)) { -aio_poll (aio_context, true); - waited = true; - } + waited |=bdrv_drain_io_recurse (bs); } }aio_context_release (aio_context);block/qed.c +15
-1@@ -336,7 +336,7 @@ static void qed_need_check_timer_cb(void *opaque)qed_plug_allocating_write_reqs (s);/* Ensure writes are on disk before clearing flag */ -bdrv_aio_flush (s->bs, qed_clear_need_check, s); +bdrv_aio_flush (s->bs->file->bs, qed_clear_need_check, s); }static void qed_start_need_check_timer (BDRVQEDState *s)@@ -378,6 +378,19 @@ static void bdrv_qed_attach_aio_context(BlockDriverState *bs, } } +static void bdrv_qed_drain (BlockDriverState *bs) +{ +BDRVQEDState *s = bs->opaque; + +/* Fire the timer immediately in order to start doing I/O as soon as the +* header is flushed. +*/ +if (s->need_check_timer &&timer_pending (s->need_check_timer)) { +qed_cancel_need_check_timer (s); +qed_need_check_timer_cb (s); + } +} +static int bdrv_qed_open (BlockDriverState *bs,QDict *options,int flags,Error **errp) {@@ -1668,6 +1681,7 @@ static BlockDriver bdrv_qed = { .bdrv_check = bdrv_qed_check, .bdrv_detach_aio_context = bdrv_qed_detach_aio_context, .bdrv_attach_aio_context = bdrv_qed_attach_aio_context, + .bdrv_drain = bdrv_qed_drain, };static void bdrv_qed_init (void )