Skip to content

Commit fc9c0a9

Browse files
committed
blockjob: add pause points
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 <stefanha@redhat.com> Reviewed-by: Paolo Bonzini <pbonzini@redhat.com> Reviewed-by: Fam Zheng <famz@redhat.com> Message-id: 1466096189-6477-4-git-send-email-stefanha@redhat.com
1 parent a7f3b7f commit fc9c0a9

File tree

2 files changed

+65
-8
lines changed

2 files changed

+65
-8
lines changed

blockjob.c

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,32 @@ static bool block_job_should_pause(BlockJob *job)
257257
return job->pause_count > 0;
258258
}
259259

260+
void coroutine_fn block_job_pause_point(BlockJob *job)
261+
{
262+
if (!block_job_should_pause(job)) {
263+
return;
264+
}
265+
if (block_job_is_cancelled(job)) {
266+
return;
267+
}
268+
269+
if (job->driver->pause) {
270+
job->driver->pause(job);
271+
}
272+
273+
if (block_job_should_pause(job) && !block_job_is_cancelled(job)) {
274+
job->paused = true;
275+
job->busy = false;
276+
qemu_coroutine_yield(); /* wait for block_job_resume() */
277+
job->busy = true;
278+
job->paused = false;
279+
}
280+
281+
if (job->driver->resume) {
282+
job->driver->resume(job);
283+
}
284+
}
285+
260286
void block_job_resume(BlockJob *job)
261287
{
262288
assert(job->pause_count > 0);
@@ -364,11 +390,9 @@ void block_job_sleep_ns(BlockJob *job, QEMUClockType type, int64_t ns)
364390
if (!block_job_should_pause(job)) {
365391
co_aio_sleep_ns(blk_get_aio_context(job->blk), type, ns);
366392
}
367-
/* The job can be paused while sleeping, so check this again */
368-
if (block_job_should_pause(job)) {
369-
qemu_coroutine_yield();
370-
}
371393
job->busy = true;
394+
395+
block_job_pause_point(job);
372396
}
373397

374398
void block_job_yield(BlockJob *job)
@@ -381,8 +405,12 @@ void block_job_yield(BlockJob *job)
381405
}
382406

383407
job->busy = false;
384-
qemu_coroutine_yield();
408+
if (!block_job_should_pause(job)) {
409+
qemu_coroutine_yield();
410+
}
385411
job->busy = true;
412+
413+
block_job_pause_point(job);
386414
}
387415

388416
BlockJobInfo *block_job_query(BlockJob *job)

include/block/blockjob.h

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,20 @@ typedef struct BlockJobDriver {
7070
* never both.
7171
*/
7272
void (*abort)(BlockJob *job);
73+
74+
/**
75+
* If the callback is not NULL, it will be invoked when the job transitions
76+
* into the paused state. Paused jobs must not perform any asynchronous
77+
* I/O or event loop activity. This callback is used to quiesce jobs.
78+
*/
79+
void coroutine_fn (*pause)(BlockJob *job);
80+
81+
/**
82+
* If the callback is not NULL, it will be invoked when the job transitions
83+
* out of the paused state. Any asynchronous I/O or event loop activity
84+
* should be restarted from this callback.
85+
*/
86+
void coroutine_fn (*resume)(BlockJob *job);
7387
} BlockJobDriver;
7488

7589
/**
@@ -119,12 +133,18 @@ struct BlockJob {
119133
bool user_paused;
120134

121135
/**
122-
* Set to false by the job while it is in a quiescent state, where
123-
* no I/O is pending and the job has yielded on any condition
124-
* that is not detected by #aio_poll, such as a timer.
136+
* Set to false by the job while the coroutine has yielded and may be
137+
* re-entered by block_job_enter(). There may still be I/O or event loop
138+
* activity pending.
125139
*/
126140
bool busy;
127141

142+
/**
143+
* Set to true by the job while it is in a quiescent state, where
144+
* no I/O or event loop activity is pending.
145+
*/
146+
bool paused;
147+
128148
/**
129149
* Set to true when the job is ready to be completed.
130150
*/
@@ -298,6 +318,15 @@ bool block_job_is_cancelled(BlockJob *job);
298318
*/
299319
BlockJobInfo *block_job_query(BlockJob *job);
300320

321+
/**
322+
* block_job_pause_point:
323+
* @job: The job that is ready to pause.
324+
*
325+
* Pause now if block_job_pause() has been called. Block jobs that perform
326+
* lots of I/O must call this between requests so that the job can be paused.
327+
*/
328+
void coroutine_fn block_job_pause_point(BlockJob *job);
329+
301330
/**
302331
* block_job_pause:
303332
* @job: The job to be paused.

0 commit comments

Comments
 (0)