[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 15/20] blockjob: add pause points
From: |
Stefan Hajnoczi |
Subject: |
[Qemu-devel] [PULL 15/20] blockjob: add pause points |
Date: |
Mon, 20 Jun 2016 15:05:26 +0100 |
Block jobs are coroutines that usually perform I/O but sometimes also
sleep or yield. Currently only sleeping or yielded block jobs can be
paused. This means jobs that do not sleep or yield (using
block_job_yield()) are unaffected by block_job_pause().
Add block_job_pause_point() so that block jobs can mark quiescent points
that are suitable for pausing. This solves the problem that it can take
a block job a long time to pause if it is performing a long series of
I/O operations.
Transitioning to paused state involves a .pause()/.resume() callback.
These callbacks are used to ensure that I/O and event loop activity has
ceased while the job is at a pause point.
Note that this patch introduces a stricter pause state than previously.
The job->busy flag was incorrectly documented as a quiescent state
without I/O pending. This is violated by any job that has I/O pending
across sleep or block_job_yield(), like the mirror block job.
[Add missing block_job_should_pause() check to avoid deadlock after
job->driver->pause() in block_job_pause_point().
--Stefan]
Signed-off-by: Stefan Hajnoczi <address@hidden>
Reviewed-by: Paolo Bonzini <address@hidden>
Reviewed-by: Fam Zheng <address@hidden>
Message-id: address@hidden
---
blockjob.c | 38 +++++++++++++++++++++++++++++++++-----
include/block/blockjob.h | 35 ++++++++++++++++++++++++++++++++---
2 files changed, 65 insertions(+), 8 deletions(-)
diff --git a/blockjob.c b/blockjob.c
index 21cc3ee..874b573 100644
--- a/blockjob.c
+++ b/blockjob.c
@@ -257,6 +257,32 @@ static bool block_job_should_pause(BlockJob *job)
return job->pause_count > 0;
}
+void coroutine_fn block_job_pause_point(BlockJob *job)
+{
+ if (!block_job_should_pause(job)) {
+ return;
+ }
+ if (block_job_is_cancelled(job)) {
+ return;
+ }
+
+ if (job->driver->pause) {
+ job->driver->pause(job);
+ }
+
+ if (block_job_should_pause(job) && !block_job_is_cancelled(job)) {
+ job->paused = true;
+ job->busy = false;
+ qemu_coroutine_yield(); /* wait for block_job_resume() */
+ job->busy = true;
+ job->paused = false;
+ }
+
+ if (job->driver->resume) {
+ job->driver->resume(job);
+ }
+}
+
void block_job_resume(BlockJob *job)
{
assert(job->pause_count > 0);
@@ -364,11 +390,9 @@ void block_job_sleep_ns(BlockJob *job, QEMUClockType type,
int64_t ns)
if (!block_job_should_pause(job)) {
co_aio_sleep_ns(blk_get_aio_context(job->blk), type, ns);
}
- /* The job can be paused while sleeping, so check this again */
- if (block_job_should_pause(job)) {
- qemu_coroutine_yield();
- }
job->busy = true;
+
+ block_job_pause_point(job);
}
void block_job_yield(BlockJob *job)
@@ -381,8 +405,12 @@ void block_job_yield(BlockJob *job)
}
job->busy = false;
- qemu_coroutine_yield();
+ if (!block_job_should_pause(job)) {
+ qemu_coroutine_yield();
+ }
job->busy = true;
+
+ block_job_pause_point(job);
}
BlockJobInfo *block_job_query(BlockJob *job)
diff --git a/include/block/blockjob.h b/include/block/blockjob.h
index 8fcecf9..7739f37 100644
--- a/include/block/blockjob.h
+++ b/include/block/blockjob.h
@@ -70,6 +70,20 @@ typedef struct BlockJobDriver {
* never both.
*/
void (*abort)(BlockJob *job);
+
+ /**
+ * If the callback is not NULL, it will be invoked when the job transitions
+ * into the paused state. Paused jobs must not perform any asynchronous
+ * I/O or event loop activity. This callback is used to quiesce jobs.
+ */
+ void coroutine_fn (*pause)(BlockJob *job);
+
+ /**
+ * If the callback is not NULL, it will be invoked when the job transitions
+ * out of the paused state. Any asynchronous I/O or event loop activity
+ * should be restarted from this callback.
+ */
+ void coroutine_fn (*resume)(BlockJob *job);
} BlockJobDriver;
/**
@@ -119,13 +133,19 @@ struct BlockJob {
bool user_paused;
/**
- * Set to false by the job while it is in a quiescent state, where
- * no I/O is pending and the job has yielded on any condition
- * that is not detected by #aio_poll, such as a timer.
+ * Set to false by the job while the coroutine has yielded and may be
+ * re-entered by block_job_enter(). There may still be I/O or event loop
+ * activity pending.
*/
bool busy;
/**
+ * Set to true by the job while it is in a quiescent state, where
+ * no I/O or event loop activity is pending.
+ */
+ bool paused;
+
+ /**
* Set to true when the job is ready to be completed.
*/
bool ready;
@@ -299,6 +319,15 @@ bool block_job_is_cancelled(BlockJob *job);
BlockJobInfo *block_job_query(BlockJob *job);
/**
+ * block_job_pause_point:
+ * @job: The job that is ready to pause.
+ *
+ * Pause now if block_job_pause() has been called. Block jobs that perform
+ * lots of I/O must call this between requests so that the job can be paused.
+ */
+void coroutine_fn block_job_pause_point(BlockJob *job);
+
+/**
* block_job_pause:
* @job: The job to be paused.
*
--
2.5.5
- [Qemu-devel] [PULL 02/20] libqos: drop duplicated PCI vendor ID definition, (continued)
- [Qemu-devel] [PULL 02/20] libqos: drop duplicated PCI vendor ID definition, Stefan Hajnoczi, 2016/06/20
- [Qemu-devel] [PULL 04/20] libqos: drop duplicated virtio_ring.h bit definitions, Stefan Hajnoczi, 2016/06/20
- [Qemu-devel] [PULL 05/20] libqos: drop duplicated virtio_vring.h structs, Stefan Hajnoczi, 2016/06/20
- [Qemu-devel] [PULL 06/20] libqos: drop duplicated virtio_blk.h definitions, Stefan Hajnoczi, 2016/06/20
- [Qemu-devel] [PULL 08/20] libqos: drop duplicated virtio_pci.h definitions, Stefan Hajnoczi, 2016/06/20
- [Qemu-devel] [PULL 07/20] libqos: drop duplicated virtio_scsi.h definitions, Stefan Hajnoczi, 2016/06/20
- [Qemu-devel] [PULL 09/20] libqos: add qvirtqueue_cleanup(), Stefan Hajnoczi, 2016/06/20
- [Qemu-devel] [PULL 11/20] block: fix race in bdrv_co_discard with drive-mirror, Stefan Hajnoczi, 2016/06/20
- [Qemu-devel] [PULL 10/20] block: fixed BdrvTrackedRequest filling in bdrv_co_discard, Stefan Hajnoczi, 2016/06/20
- [Qemu-devel] [PULL 12/20] block: process before_write_notifiers in bdrv_co_discard, Stefan Hajnoczi, 2016/06/20
- [Qemu-devel] [PULL 15/20] blockjob: add pause points,
Stefan Hajnoczi <=
- [Qemu-devel] [PULL 14/20] blockjob: rename block_job_is_paused(), Stefan Hajnoczi, 2016/06/20
- [Qemu-devel] [PULL 13/20] blockjob: move iostatus reset out of block_job_enter(), Stefan Hajnoczi, 2016/06/20
- [Qemu-devel] [PULL 16/20] blockjob: add block_job_get_aio_context(), Stefan Hajnoczi, 2016/06/20
- [Qemu-devel] [PULL 17/20] block: use safe iteration over AioContext notifiers, Stefan Hajnoczi, 2016/06/20
- [Qemu-devel] [PULL 18/20] blockjob: add AioContext attached callback, Stefan Hajnoczi, 2016/06/20
- [Qemu-devel] [PULL 20/20] backup: follow AioContext change gracefully, Stefan Hajnoczi, 2016/06/20
- [Qemu-devel] [PULL 19/20] mirror: follow AioContext change gracefully, Stefan Hajnoczi, 2016/06/20
- Re: [Qemu-devel] [PULL 00/20] Block patches, Peter Maydell, 2016/06/20