From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47896) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bEzqI-0008Eu-TB for qemu-devel@nongnu.org; Mon, 20 Jun 2016 10:06:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bEzqC-00079u-Mg for qemu-devel@nongnu.org; Mon, 20 Jun 2016 10:06:22 -0400 Received: from mx1.redhat.com ([209.132.183.28]:48146) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bEzqC-00079l-A4 for qemu-devel@nongnu.org; Mon, 20 Jun 2016 10:06:16 -0400 From: Stefan Hajnoczi Date: Mon, 20 Jun 2016 15:05:29 +0100 Message-Id: <1466431531-17806-19-git-send-email-stefanha@redhat.com> In-Reply-To: <1466431531-17806-1-git-send-email-stefanha@redhat.com> References: <1466431531-17806-1-git-send-email-stefanha@redhat.com> Subject: [Qemu-devel] [PULL 18/20] blockjob: add AioContext attached callback List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Peter Maydell , Stefan Hajnoczi Block jobs that use additional BDSes or event loop resources need a callback to get their affairs in order when the AioContext is switched. Simple block jobs don't need an attach callback, they automatically work thanks to the generic attach/detach notifiers that this patch adds. Signed-off-by: Stefan Hajnoczi Reviewed-by: Paolo Bonzini Reviewed-by: Fam Zheng Message-id: 1466096189-6477-7-git-send-email-stefanha@redhat.com --- blockjob.c | 38 ++++++++++++++++++++++++++++++++++++++ include/block/blockjob.h | 7 +++++++ 2 files changed, 45 insertions(+) diff --git a/blockjob.c b/blockjob.c index 096a1b8..90c4e26 100644 --- a/blockjob.c +++ b/blockjob.c @@ -71,6 +71,38 @@ static AioContext *block_job_get_aio_context(BlockJob *job) blk_get_aio_context(job->blk); } +static void block_job_attached_aio_context(AioContext *new_context, + void *opaque) +{ + BlockJob *job = opaque; + + if (job->driver->attached_aio_context) { + job->driver->attached_aio_context(job, new_context); + } + + block_job_resume(job); +} + +static void block_job_detach_aio_context(void *opaque) +{ + BlockJob *job = opaque; + + /* In case the job terminates during aio_poll()... */ + block_job_ref(job); + + 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_unref(job); +} + void *block_job_create(const BlockJobDriver *driver, BlockDriverState *bs, int64_t speed, BlockCompletionFunc *cb, void *opaque, Error **errp) @@ -103,6 +135,9 @@ void *block_job_create(const BlockJobDriver *driver, BlockDriverState *bs, QLIST_INSERT_HEAD(&block_jobs, job, job_list); + blk_add_aio_context_notifier(blk, block_job_attached_aio_context, + block_job_detach_aio_context, job); + /* Only set speed when necessary to avoid NotSupported error */ if (speed != 0) { Error *local_err = NULL; @@ -128,6 +163,9 @@ void block_job_unref(BlockJob *job) BlockDriverState *bs = blk_bs(job->blk); bs->job = NULL; bdrv_op_unblock_all(bs, job->blocker); + blk_remove_aio_context_notifier(job->blk, + block_job_attached_aio_context, + block_job_detach_aio_context, job); blk_unref(job->blk); error_free(job->blocker); g_free(job->id); diff --git a/include/block/blockjob.h b/include/block/blockjob.h index 7739f37..7dc720c 100644 --- a/include/block/blockjob.h +++ b/include/block/blockjob.h @@ -84,6 +84,13 @@ typedef struct BlockJobDriver { * should be restarted from this callback. */ void coroutine_fn (*resume)(BlockJob *job); + + /* + * If the callback is not NULL, it will be invoked before the job is + * resumed in a new AioContext. This is the place to move any resources + * besides job->blk to the new AioContext. + */ + void (*attached_aio_context)(BlockJob *job, AioContext *new_context); } BlockJobDriver; /** -- 2.5.5