blockjob: Fix dead loop with block_job_finish_sync on dataplane disks
v2 cover blockjob: Fix dead loop with block_job_finish_sync on dataplane disks
v2: Fix the migration case. [Paolo]
Fam Zheng (2):
blockjob: Rename block_job_defer_to_main_loop
blockjob: Fix hang in block_job_finish_sync
block/backup.c | 2 +- block/commit.c | 2 +- block/mirror.c | 2 +- block/stream.c | 2 +- blockjob.c | 7 ++++--- include/block/blockjob.h | 14 +++++++++----- tests/test-blockjob-txn.c | 2 +- 7 files changed, 18 insertions(+), 13 deletions(-)
Signature · 1 line
2.4.3
PATCH v2 1/2 blockjob: Rename block_job_defer_to_main_loop
The next patch will make this function more restrictive than it is now, rename it and update comment to reflect the change.
--- block/backup.c | 2 +- block/commit.c | 2 +- block/mirror.c | 2 +- block/stream.c | 2 +- blockjob.c | 4 ++-- include/block/blockjob.h | 12 +++++++----- tests/test-blockjob-txn.c | 2 +- 7 files changed, 14 insertions(+), 12 deletions(-)
block/backup.c +1 -1
@@ -482,7 +482,7 @@ static void coroutine_fn backup_run(void *opaque) data =g_malloc (sizeof (*data)); data->ret = ret; -block_job_defer_to_main_loop (&job->common, backup_complete, data); +block_job_coroutine_complete (&job->common, backup_complete, data); } void backup_start(BlockDriverState *bs, BlockDriverState *target,
block/commit.c +1 -1
@@ -181,7 +181,7 @@ out: data =g_malloc (sizeof (*data)); data->ret = ret; -block_job_defer_to_main_loop (&s->common, commit_complete, data); +block_job_coroutine_complete (&s->common, commit_complete, data); }static void commit_set_speed (BlockJob *job,int64_t speed,Error **errp)
block/mirror.c +1 -1
@@ -626,7 +626,7 @@ immediate_exit:/* Before we switch to target in mirror_exit, make sure data doesn't * change. */ bdrv_drained_begin (s->common.bs); -block_job_defer_to_main_loop (&s->common, mirror_exit, data); +block_job_coroutine_complete (&s->common, mirror_exit, data); }static void mirror_set_speed (BlockJob *job,int64_t speed,Error **errp)
block/stream.c +1 -1
@@ -194,7 +194,7 @@ wait: data =g_malloc (sizeof (*data)); data->ret = ret; data->reached_end = sector_num == end; -block_job_defer_to_main_loop (&s->common, stream_complete, data); +block_job_coroutine_complete (&s->common, stream_complete, data); }static void stream_set_speed (BlockJob *job,int64_t speed,Error **errp)
blockjob.c +2 -2
@@ -471,7 +471,7 @@ static void block_job_defer_to_main_loop_bh(void *opaque)qemu_bh_delete (data->bh); -/* Prevent race with block_job_defer_to_main_loop() */ +/* Prevent race with block_job_coroutine_complete() */ aio_context_acquire (data->aio_context);/* Fetch BDS AioContext again, in case it has changed */
@@ -487,7 +487,7 @@ static void block_job_defer_to_main_loop_bh(void *opaque)g_free (data); } -void block_job_defer_to_main_loop (BlockJob *job, +void block_job_coroutine_complete (BlockJob *job,BlockJobDeferToMainLoopFn *fn,void *opaque) {
include/block/blockjob.h +7 -5
@@ -393,18 +393,20 @@ BlockErrorAction block_job_error_action(BlockJob *job, BlockDriverState *bs,typedef void BlockJobDeferToMainLoopFn (BlockJob *job,void *opaque);/** -* block_job_defer_to_main_loop: +* block_job_coroutine_complete: * @job: The job * @fn: The function to run in the main loop * @opaque: The opaque value that is passed to @fn * -* Execute a given function in the main loop with the BlockDriverState -* AioContext acquired. Block jobs must call bdrv_unref(), bdrv_close(), and -* anything that uses bdrv_drain_all() in the main loop. +* Complete the block job coroutine and execute a given function in the main +* loop with the BlockDriverState AioContext acquired. Block jobs must call +* bdrv_unref(), bdrv_close(), and anything that uses bdrv_drain_all() in the +* main loop. After calling this, the block job coroutine should complete right +* away, without doing any heavy operations such as I/O or block_job_yield(). * * The @job AioContext is held while @fn executes. */ -void block_job_defer_to_main_loop (BlockJob *job, +void block_job_coroutine_complete (BlockJob *job,BlockJobDeferToMainLoopFn *fn,void *opaque);
tests/test-blockjob-txn.c +1 -1
@@ -57,7 +57,7 @@ static void coroutine_fn test_block_job_run(void *opaque) } } -block_job_defer_to_main_loop (job, test_block_job_complete, +block_job_coroutine_complete (job, test_block_job_complete, (void *)(intptr_t )s->rc); }
Signature · 1 line
2.4.3
PATCH v2 2/2 blockjob: Fix hang in block_job_finish_sync
In reply to Fam ZhengWith a mirror job running on a virtio-blk dataplane disk, sending "q" to HMP will cause a dead loop in block_job_finish_sync.
This is because the aio_poll() only processes the AIO context of bs which has no more work to do, while the main loop BH that is scheduled for setting the job->completed flag is never processed.
Fix this by adding a "ctx" pointer in BlockJob structure, to track which context to poll for the block job to make progress (NULL means bdrv_get_aio_context(bs) is used).
--- blockjob.c | 3 ++- include/block/blockjob.h | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-)
blockjob.c +2 -1
@@ -304,7 +304,7 @@ static int block_job_finish_sync(BlockJob *job,return -EBUSY ; }while (!job->completed) { -aio_poll (bdrv_get_aio_context (bs), true); +aio_poll (job->ctx ? :bdrv_get_aio_context (job->bs), true); } ret = (job->cancelled && job->ret ==0 ) ? -ECANCELED : job->ret;block_job_unref (job);
@@ -497,6 +497,7 @@ void block_job_coroutine_complete(BlockJob *job, data->aio_context =bdrv_get_aio_context (job->bs); data->fn = fn; data->opaque = opaque; + job->ctx =qemu_get_aio_context ();qemu_bh_schedule (data->bh); }
include/block/blockjob.h +2 -0
@@ -92,6 +92,8 @@ struct BlockJob {*/ char *id; +AioContext *ctx; +/** * The coroutine that executes the job. If not NULL, it is * reentered when busy is false and the job is cancelled.
Signature · 1 line
2.4.3