Skip to contentSkip to conversation

job: don't drop job->paused while a pause is pending

PATCH Applied Denis V. Lunev 2 participants 6 messages Jun 23 – Jul 7

lore
Share & tools
lore
#0 lore raw

cover job: don't drop job->paused while a pause is pending

A guest can abort QEMU with

qemu-system-x86_64: ../job.c:379: job_set_aio_context: Assertion `job->paused || job_is_completed_locked(job)' failed.

when it touches a virtio-blk device whose BlockBackend lives in an iothread while a block job (drive-mirror / blockCopy) runs on the same node. It is guest-triggerable and reproduces on current master. It was reported against the qemu-kvm 10.0 downstream, hit by the live-migration validation suite when a blockdev-mirror (libvirt blockCopy) ran while the guest reset its virtio-blk device during boot.

Under overlapping drains job_pause_point_locked() clears job->paused on wake while a pause is still pending (pause_count > 0), and the concurrent AioContext change reads paused == false and asserts. Patch 1 carries the full interleaving and the fix; being in the job layer it also covers the other bdrv_try_change_aio_context() callers (x-blockdev-set-iothread, blockdev hot-plug/unplug, job completion), not just the virtio-blk reset path.

Patch 2 is a deterministic regression test in tests/unit/test-blockjob that fails without patch 1 and passes with it. It is illustrative only:
it pins the exact invariant patch 1 restores, but the fix stands on its own and the test may be dropped freely.

There is no iotests reproducer. iotests run under qtest, which has no vCPU, so the only way to change a running job's AioContext from outside its iothread is x-blockdev-set-iothread, which runs in the main loop under the BQL - the same lock the pause handshake takes - and the cross-thread window never opens on an idle qtest VM. Several designs (back-to-back set-iothread, pause/resume churn, two iothreads, a latency-injecting null-co feeding a throttled mirror) all pass on the unfixed build; a timing test that passes on broken code is worse than none.

To reproduce on master: in the guest, tightly unbind/rebind a virtio-blk PCI function backed by an iothread; on the host, keep a throttled blockdev-mirror alive on that disk's node. The assertion fires within seconds; with patch 1 it runs indefinitely.

checkpatch is clean; test-blockjob, test-bdrv-drain and test-block-iothread pass.

Denis V. Lunev (2):
job: keep job paused across overlapping pause requests tests/unit/test-blockjob: cover keeping a job paused while a pause is
pending

 job.c                      |   9 +++-
 tests/unit/test-blockjob.c | 100 +++++++++++++++++++++++++++++++++++++
 2 files changed, 108 insertions(+), 1 deletion(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/2 job: keep job paused across overlapping pause requests

job_pause_point_locked() sets job->paused before yielding and clears it unconditionally on wake, before re-checking whether a pause is still pending. job_pause() re-enters a parked job only while it is not yet paused, so the wake that resumes one comes from a drain *ending* (job_resume() -> job_enter_cond()). If the next drain begins before that wake runs, the woken coroutine clears job->paused while pause_count is already > 0 again:

AioContext change (BQL thread) job coroutine (iothread)
----------------------------- ------------------------
parked in job_pause_point():
paused=1, pause_count=1, yielded
drain ends -> job_resume():
pause_count = 0
job_enter_cond(): queue wake ..> (wake pending)
bdrv_try_change_aio_context():

    bdrv_drain_all_begin():
      job_pause() per node
      pause_count = N (> 0)
                                     wake runs, leaves job_do_yield():
                                       paused = 0  (pause_count == N)
    tran_commit -> job_set_aio_context():
      assert(paused || completed) --> abort: paused == 0

bdrv_try_change_aio_context() drains precisely to quiesce the job before changing its AioContext, but that brief paused==0 window trips the assertion. It is guest-triggerable: a virtio-blk reset (virtio_blk_stop_ioeventfd() -> blk_set_aio_context()) racing a running mirror/blockCopy job hits it, as do x-blockdev-set-iothread, blockdev hot-plug/unplug and job completion.

Keep job->paused set while a pause is still pending: loop the yield until job_should_pause_locked() is false (or the job is cancelled), and only then clear job->paused. Drained-state consumers then never observe a pending-pause job as unpaused.

---
 job.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
job.c +8 -1
index e747908472..d7220aaf30 100644
@@ -629,7 +629,14 @@ static void coroutine_fn job_pause_point_locked(Job *job)
                                     ? JOB_STATUS_STANDBY
                                     : JOB_STATUS_PAUSED);
         job->paused = true;
-        job_do_yield_locked(job, -1);
+        /*
+         * Stay paused across back-to-back pause requests: a transient
+         * paused == false while pause_count > 0 would be observed as
+         * "not paused" by job_set_aio_context() and other drain consumers.
+         */
+        do {
+            job_do_yield_locked(job, -1);
+        } while (job_should_pause_locked(job) && !job_is_cancelled_locked(job));
         job->paused = false;
         job_state_transition_locked(job, status);
     }
Signature · 1 line
2.53.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] job: keep job paused across overlapping pause requests

23.06.26 18:24, Denis V. Lunev пишет:

Quoted 31 lines

job_pause_point_locked() sets job->paused before yielding and clears it unconditionally on wake, before re-checking whether a pause is still pending. job_pause() re-enters a parked job only while it is not yet paused, so the wake that resumes one comes from a drainending (job_resume() -> job_enter_cond()). If the next drain begins before that wake runs, the woken coroutine clears job->paused while pause_count is already > 0 again:

AioContext change (BQL thread) job coroutine (iothread)
----------------------------- ------------------------
parked in job_pause_point():
paused=1, pause_count=1, yielded
drain ends -> job_resume():
pause_count = 0
job_enter_cond(): queue wake ..> (wake pending)
bdrv_try_change_aio_context():

     bdrv_drain_all_begin():
       job_pause() per node
       pause_count = N (> 0)
                                      wake runs, leaves job_do_yield():
                                        paused = 0  (pause_count == N)
     tran_commit -> job_set_aio_context():
       assert(paused || completed) --> abort: paused == 0

bdrv_try_change_aio_context() drains precisely to quiesce the job before changing its AioContext, but that brief paused==0 window trips the assertion. It is guest-triggerable: a virtio-blk reset (virtio_blk_stop_ioeventfd() -> blk_set_aio_context()) racing a running mirror/blockCopy job hits it, as do x-blockdev-set-iothread, blockdev hot-plug/unplug and job completion.

Quoted 6 lines

Keep job->paused set while a pause is still pending: loop the yield until job_should_pause_locked() is false (or the job is cancelled), and only then clear job->paused. Drained-state consumers then never observe a pending-pause job as unpaused.

Signature · 2 lines
Best regards,
Vladimir
#3 lore raw

PATCH 2/2 tests/unit/test-blockjob: cover keeping a job paused while a pause is pending

In reply to Denis V. Lunev

Add a regression test for the previous commit. A job that has reached its pause point is spuriously re-entered (job_enter()) while a pause is still pending (pause_count > 0), reproducing what an overlapping drain
does: one drain's job_resume() wakes the job while the next drain's job_pause() is already counted. The job must stay parked - it must not run job code or clear job->paused, or job_set_aio_context() could observe paused == false and abort.

The test counts the job's run-loop iterations: without the fix the re-entered job clears job->paused, runs one iteration and re-pauses, so the counter advances; with the fix it stays parked and the counter is unchanged. It runs in the main AioContext, so job_enter() is synchronous and the check is deterministic.

---
 tests/unit/test-blockjob.c | 100 +++++++++++++++++++++++++++++++++++++
 1 file changed, 100 insertions(+)
tests/unit/test-blockjob.c +100 -0
index abdbe4b835..3b77d19b91 100644
@@ -388,6 +388,105 @@ static void test_cancel_concluded(void)
     cancel_common(s);
 }
 
+typedef struct PauseCountJob {
+    BlockJob common;
+    int n;
+    bool should_complete;
+} PauseCountJob;
+
+static void pause_count_job_complete(Job *job, Error **errp)
+{
+    PauseCountJob *s = container_of(job, PauseCountJob, common.job);
+    s->should_complete = true;
+}
+
+static int coroutine_fn pause_count_job_run(Job *job, Error **errp)
+{
+    PauseCountJob *s = container_of(job, PauseCountJob, common.job);
+
+    while (!s->should_complete) {
+        if (job_is_cancelled(&s->common.job)) {
+            return 0;
+        }
+        s->n++;
+        /*
+         * Yields; while a pause is pending the yield is skipped and the job
+         * parks in job_pause_point() instead.
+         */
+        job_sleep_ns(&s->common.job, 10 * 1000 * 1000);
+    }
+
+    return 0;
+}
+
+static const BlockJobDriver pause_count_job_driver = {
+    .job_driver = {
+        .instance_size = sizeof(PauseCountJob),
+        .free          = block_job_free,
+        .user_resume   = block_job_user_resume,
+        .run           = pause_count_job_run,
+        .complete      = pause_count_job_complete,
+    },
+};
+
+/*
+ * A job that has reached its pause point must stay paused while a pause is
+ * still pending (pause_count > 0). An overlapping drain re-enters the job (one
+ * drain's job_resume() wakes it while the next drain's job_pause() is already
+ * counted); the job must not run or clear job->paused, otherwise
+ * job_set_aio_context() can observe paused == false and abort.
+ */
+static void test_pause_keeps_paused(void)
+{
+    BlockBackend *blk;
+    BlockJob *bjob;
+    PauseCountJob *s;
+    Job *job;
+    int n0;
+
+    blk = create_blk(NULL);
+    bjob = mk_job(blk, "job0", &pause_count_job_driver, true,
+                  JOB_MANUAL_FINALIZE | JOB_MANUAL_DISMISS);
+    s = container_of(bjob, PauseCountJob, common);
+    job = &bjob->job;
+    WITH_JOB_LOCK_GUARD() {
+        job_ref_locked(job);
+    }
+
+    job_start(job);
+
+    /* Pause the running job; it parks in job_pause_point() with paused set. */
+    WITH_JOB_LOCK_GUARD() {
+        job_pause_locked(job);
+        g_assert_true(job->paused);
+        g_assert_cmpint(job->status, ==, JOB_STATUS_PAUSED);
+    }
+    n0 = s->n;
+
+    /*
+     * Spurious wake while the pause is still pending. The job must stay parked:
+     * the bug clears job->paused, runs an iteration (s->n advances) and
+     * re-pauses, exposing a paused == false window.
+     */
+    job_enter(job);
+    WITH_JOB_LOCK_GUARD() {
+        g_assert_true(job->paused);
+    }
+    g_assert_cmpint(s->n, ==, n0);
+
+    /* Resume and tear down. */
+    WITH_JOB_LOCK_GUARD() {
+        job_resume_locked(job);
+    }
+    job_cancel_sync(job, true);
+    WITH_JOB_LOCK_GUARD() {
+        Job *dummy = job;
+        job_dismiss_locked(&dummy, &error_abort);
+        job_unref_locked(job);
+    }
+    destroy_blk(blk);
+}
+
 int main(int argc, char **argv)
 {
     qemu_init_main_loop(&error_abort);
@@ -402,5 +501,6 @@ int main(int argc, char **argv)
     g_test_add_func("/blockjob/cancel/standby", test_cancel_standby);
     g_test_add_func("/blockjob/cancel/pending", test_cancel_pending);
     g_test_add_func("/blockjob/cancel/concluded", test_cancel_concluded);
+    g_test_add_func("/blockjob/pause/keep_paused", test_pause_keeps_paused);
     return g_test_run();
 }
Signature · 1 line
2.53.0
Reviewed-by Tested-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] tests/unit/test-blockjob: cover keeping a job paused while a pause is pending

23.06.26 18:24, Denis V. Lunev пишет:

Quoted 8 lines

Add a regression test for the previous commit. A job that has reached its pause point is spuriously re-entered (job_enter()) while a pause is still pending (pause_count > 0), reproducing what an overlapping drain
does: one drain's job_resume() wakes the job while the next drain's job_pause() is already counted. The job must stay parked - it must not run job code or clear job->paused, or job_set_aio_context() could observe paused == false and abort.

Quoted 7 lines

The test counts the job's run-loop iterations: without the fix the re-entered job clears job->paused, runs one iteration and re-pauses, so the counter advances; with the fix it stays parked and the counter is unchanged. It runs in the main AioContext, so job_enter() is synchronous and the check is deterministic.

Signature · 2 lines
Best regards,
Vladimir
#5 lore raw
In reply to Denis V. Lunev
Quoted 43 lines · Denis V. Lunev

A guest can abort QEMU with

   qemu-system-x86_64: ../job.c:379: job_set_aio_context: Assertion
   `job->paused || job_is_completed_locked(job)' failed.

when it touches a virtio-blk device whose BlockBackend lives in an iothread while a block job (drive-mirror / blockCopy) runs on the same node. It is guest-triggerable and reproduces on current master. It was reported against the qemu-kvm 10.0 downstream, hit by the live-migration validation suite when a blockdev-mirror (libvirt blockCopy) ran while the guest reset its virtio-blk device during boot.

Under overlapping drains job_pause_point_locked() clears job->paused on wake while a pause is still pending (pause_count > 0), and the concurrent AioContext change reads paused == false and asserts. Patch 1 carries the full interleaving and the fix; being in the job layer it also covers the other bdrv_try_change_aio_context() callers (x-blockdev-set-iothread, blockdev hot-plug/unplug, job completion), not just the virtio-blk reset path.

Patch 2 is a deterministic regression test in tests/unit/test-blockjob that fails without patch 1 and passes with it. It is illustrative only:
it pins the exact invariant patch 1 restores, but the fix stands on its own and the test may be dropped freely.

There is no iotests reproducer. iotests run under qtest, which has no vCPU, so the only way to change a running job's AioContext from outside its iothread is x-blockdev-set-iothread, which runs in the main loop under the BQL - the same lock the pause handshake takes - and the cross-thread window never opens on an idle qtest VM. Several designs (back-to-back set-iothread, pause/resume churn, two iothreads, a latency-injecting null-co feeding a throttled mirror) all pass on the unfixed build; a timing test that passes on broken code is worse than none.

To reproduce on master: in the guest, tightly unbind/rebind a virtio-blk PCI function backed by an iothread; on the host, keep a throttled blockdev-mirror alive on that disk's node. The assertion fires within seconds; with patch 1 it runs indefinitely.

checkpatch is clean; test-blockjob, test-bdrv-drain and test-block-iothread pass.

Quoted 9 lines

Denis V. Lunev (2):
job: keep job paused across overlapping pause requests tests/unit/test-blockjob: cover keeping a job paused while a pause is
pending

  job.c                      |   9 +++-
  tests/unit/test-blockjob.c | 100 +++++++++++++++++++++++++++++++++++++
  2 files changed, 108 insertions(+), 1 deletion(-)

Thanks, applied to my block branch.

Signature · 2 lines
Best regards,
Vladimir