Skip to contentSkip to conversation

block/mirror: change the semantic of 'force' of block-job-cancel

PATCH Superseded Liang Li 3 participants 5 messages Jan 30, 2018 Block layer

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Liang Li

author
#0 lore raw

PATCH 1/1 block/mirror: change the semantic of 'force' of block-job-cancel

When doing drive mirror to a low speed shared storage, if there was heavy BLK IO write workload in VM after the 'ready' event, drive mirror block job can't be canceled immediately, it would keep running until the heavy BLK IO workload stopped in the VM.

Because libvirt depends on block-job-cancel for block live migration, the current block-job-cancel has the semantic to make sure data is in sync after the 'ready' event. This semantic can't meet some requirement, for example, people may use drive mirror for realtime backup while need the ability of block live migration. If drive mirror can't not be cancelled immediately, it means block live migration need to wait, because libvirt make use drive mirror to implement block live migration and only one drive mirror block job is allowed at the same time for a give block dev.

We need a new interface for 'force cancel', which could quit block job immediately if don't care about whether data is in sync or not.

'force' is not used by libvirt currently, to make things simple, change it's semantic slightly, hope it will not break some use case which need its original semantic.

Cc Paolo Bonzini <[email protected]>
Cc Jeff Cody <[email protected]>
Cc Kevin Wolf <[email protected]>
Cc Max Reitz <[email protected]>
Cc Eric Blake <[email protected]>
Cc John Snow <[email protected]>
---
 block/mirror.c            |  9 +++------
 blockdev.c                |  4 ++--
 blockjob.c                | 11 ++++++-----
 hmp-commands.hx           |  3 ++-
 include/block/blockjob.h  |  9 ++++++++-
 qapi/block-core.json      |  6 ++++--
 tests/test-blockjob-txn.c |  8 ++++----
 7 files changed, 29 insertions(+), 21 deletions(-)
block/mirror.c +3 -6
index c9badc1..c22dff9 100644
@@ -869,11 +869,8 @@ static void coroutine_fn mirror_run(void *opaque)
 
         ret = 0;
         trace_mirror_before_sleep(s, cnt, s->synced, delay_ns);
-        if (!s->synced) {
-            block_job_sleep_ns(&s->common, delay_ns);
-            if (block_job_is_cancelled(&s->common)) {
-                break;
-            }
+        if (block_job_is_cancelled(&s->common) && s->common.force) {
+            break;
         } else if (!should_complete) {
             delay_ns = (s->in_flight == 0 && cnt == 0 ? SLICE_TIME : 0);
             block_job_sleep_ns(&s->common, delay_ns);
@@ -887,7 +884,7 @@ immediate_exit:
          * or it was cancelled prematurely so that we do not guarantee that
          * the target is a copy of the source.
          */
-        assert(ret < 0 || (!s->synced && block_job_is_cancelled(&s->common)));
+        assert(ret < 0 || block_job_is_cancelled(&s->common));
         assert(need_drain);
         mirror_wait_for_all_io(s);
     }
blockdev.c +2 -2
index 8e977ee..039f156 100644
@@ -145,7 +145,7 @@ void blockdev_mark_auto_del(BlockBackend *blk)
         aio_context_acquire(aio_context);
 
         if (bs->job) {
-            block_job_cancel(bs->job);
+            block_job_cancel(bs->job, false);
         }
 
         aio_context_release(aio_context);
@@ -3802,7 +3802,7 @@ void qmp_block_job_cancel(const char *device,
     }
 
     trace_qmp_block_job_cancel(job);
-    block_job_cancel(job);
+    block_job_cancel(job, force);
 out:
     aio_context_release(aio_context);
 }
blockjob.c +6 -5
index f5cea84..0aacb50 100644
@@ -365,7 +365,7 @@ static void block_job_completed_single(BlockJob *job)
     block_job_unref(job);
 }
 
-static void block_job_cancel_async(BlockJob *job)
+static void block_job_cancel_async(BlockJob *job, bool force)
 {
     if (job->iostatus != BLOCK_DEVICE_IO_STATUS_OK) {
         block_job_iostatus_reset(job);
@@ -376,6 +376,7 @@ static void block_job_cancel_async(BlockJob *job)
         job->pause_count--;
     }
     job->cancelled = true;
+    job->force = force;
 }
 
 static int block_job_finish_sync(BlockJob *job,
@@ -437,7 +438,7 @@ static void block_job_completed_txn_abort(BlockJob *job)
      * on the caller, so leave it. */
     QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
         if (other_job != job) {
-            block_job_cancel_async(other_job);
+            block_job_cancel_async(other_job, true);
         }
     }
     while (!QLIST_EMPTY(&txn->jobs)) {
@@ -542,10 +543,10 @@ void block_job_user_resume(BlockJob *job)
     }
 }
 
-void block_job_cancel(BlockJob *job)
+void block_job_cancel(BlockJob *job, bool force)
 {
     if (block_job_started(job)) {
-        block_job_cancel_async(job);
+        block_job_cancel_async(job, force);
         block_job_enter(job);
     } else {
         block_job_completed(job, -ECANCELED);
@@ -557,7 +558,7 @@ void block_job_cancel(BlockJob *job)
  * function pointer casts there. */
 static void block_job_cancel_err(BlockJob *job, Error **errp)
 {
-    block_job_cancel(job);
+    block_job_cancel(job, false);
 }
 
 int block_job_cancel_sync(BlockJob *job)
hmp-commands.hx +2 -1
index 15620c9..c8bb414 100644
@@ -106,7 +106,8 @@ ETEXI
         .args_type  = "force:-f,device:B",
         .params     = "[-f] device",
         .help       = "stop an active background block operation (use -f"
-                      "\n\t\t\t if the operation is currently paused)",
+                      "\n\t\t\t if you want to abort the operation immediately"
+                      "\n\t\t\t instead of keep running until data is in sync )",
         .cmd        = hmp_block_job_cancel,
     },
 
include/block/blockjob.h +8 -1
index 00403d9..4a96c42 100644
@@ -63,6 +63,12 @@ typedef struct BlockJob {
     bool cancelled;
 
     /**
+     * Set to true if the job should be abort immediately without waiting
+     * for data is in sync.
+     */
+    bool force;
+
+    /**
      * Counter for pause request. If non-zero, the block job is either paused,
      * or if busy == true will pause itself as soon as possible.
      */
@@ -218,10 +224,11 @@ void block_job_start(BlockJob *job);
 /**
  * block_job_cancel:
  * @job: The job to be canceled.
+ * @force: Quit a job without waiting data is in sync.
  *
  * Asynchronously cancel the specified job.
  */
-void block_job_cancel(BlockJob *job);
+void block_job_cancel(BlockJob *job, bool force);
 
 /**
  * block_job_complete:
qapi/block-core.json +4 -2
index 8225308..7c4dbfe 100644
@@ -2098,8 +2098,10 @@
 #          the name of the parameter), but since QEMU 2.7 it can have
 #          other values.
 #
-# @force: whether to allow cancellation of a paused job (default
-#         false).  Since 1.3.
+# @force: #optional whether to allow cancellation a job without waiting data is
+#         in sync, please not that since 2.12 it's semantic is not exactly the
+#         same as before, from 1.3 to 2.11 it means whether to allow cancellation
+#         of a paused job (default false).  Since 1.3.
 #
 # Returns: Nothing on success
 #          If no background operation is active on this device, DeviceNotActive
tests/test-blockjob-txn.c +4 -4
index 3591c96..53daadb 100644
@@ -125,7 +125,7 @@ static void test_single_job(int expected)
     block_job_start(job);
 
     if (expected == -ECANCELED) {
-        block_job_cancel(job);
+        block_job_cancel(job, false);
     }
 
     while (result == -EINPROGRESS) {
@@ -173,10 +173,10 @@ static void test_pair_jobs(int expected1, int expected2)
     block_job_txn_unref(txn);
 
     if (expected1 == -ECANCELED) {
-        block_job_cancel(job1);
+        block_job_cancel(job1, false);
     }
     if (expected2 == -ECANCELED) {
-        block_job_cancel(job2);
+        block_job_cancel(job2, false);
     }
 
     while (result1 == -EINPROGRESS || result2 == -EINPROGRESS) {
@@ -231,7 +231,7 @@ static void test_pair_jobs_fail_cancel_race(void)
     block_job_start(job1);
     block_job_start(job2);
 
-    block_job_cancel(job1);
+    block_job_cancel(job1, false);
 
     /* Now make job2 finish before the main loop kicks jobs.  This simulates
      * the race between a pending kick and another job completing.
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 4 lines · Liang Li

When doing drive mirror to a low speed shared storage, if there was heavy BLK IO write workload in VM after the 'ready' event, drive mirror block job can't be canceled immediately, it would keep running until the heavy BLK IO workload stopped in the VM.

So far so good. But the grammar and explanation in the rest of the
commit is a bit hard to read; let me give a shot at an alternative wording:

Libvirt depends on the current block-job-cancel semantics, which is that when used without a flag after the 'ready' event, the command blocks until data is in sync. However, these semantics are awkward in other situations, for example, people may use drive mirror for realtime backups while still wanting to use block live migration. Libvirt cannot start a block live migration while another drive mirror is in progress, but the user would rather abandon the backup attempt as broken and proceed with the live migration than be stuck waiting for the current drive mirror backup to finish.

The drive-mirror command already includes a 'force' flag, which libvirt does not use, although it documented the flag as only being useful to quit a job which is paused. However, since quitting a paused job has the same effect as abandoning a backup in a non-paused job (namely, the destination file is not in sync, and the command completes immediately), we can just improve the documentation to make the force flag obviously useful.

Quoted 15 lines
Cc Paolo Bonzini <[email protected]>
Cc Jeff Cody <[email protected]>
Cc Kevin Wolf <[email protected]>
Cc Max Reitz <[email protected]>
Cc Eric Blake <[email protected]>
Cc John Snow <[email protected]>

---

+++ b/hmp-commands.hx

hmp-commands.hx ETEXI · line 110

Quoted 6 lines
hmp-commands.hx +2 -1
@@ -106,7 +106,8 @@ ETEXI
         .args_type  = "force:-f,device:B",
         .params     = "[-f] device",
         .help       = "stop an active background block operation (use -f"
-                      "\n\t\t\t if the operation is currently paused)",
+                      "\n\t\t\t if you want to abort the operation immediately"
+                      "\n\t\t\t instead of keep running until data is in sync )",

s/sync )/sync)/

include/block/blockjob.h typedef struct BlockJob { · line 66

Quoted 12 lines
hmp-commands.hx +0 -0
         .cmd        = hmp_block_job_cancel,
     },
 
include/block/blockjob.h +1 -0
index 00403d9..4a96c42 100644
@@ -63,6 +63,12 @@ typedef struct BlockJob {
     bool cancelled;
 
     /**
+     * Set to true if the job should be abort immediately without waiting

s/be //

include/block/blockjob.h typedef struct BlockJob { · line 67

Quoted 1 line
include/block/blockjob.h +1 -0
+     * for data is in sync.

s/is/to be/

include/block/blockjob.h void block_job_start(BlockJob *job); · line 227

Quoted 12 lines
include/block/blockjob.h +5 -0
+     */
+    bool force;
+
+    /**
      * Counter for pause request. If non-zero, the block job is either paused,
      * or if busy == true will pause itself as soon as possible.
      */
@@ -218,10 +224,11 @@ void block_job_start(BlockJob *job);
 /**
  * block_job_cancel:
  * @job: The job to be canceled.
+ * @force: Quit a job without waiting data is in sync.

s/data is/for data to be/

include/block/blockjob.h line 2101

Quoted 8 lines
include/block/blockjob.h +2 -2
+++ b/qapi/block-core.json
@@ -2098,8 +2098,10 @@
 #          the name of the parameter), but since QEMU 2.7 it can have
 #          other values.
 #
-# @force: whether to allow cancellation of a paused job (default
-#         false).  Since 1.3.
+# @force: #optional whether to allow cancellation a job without waiting data is

The '#optional' tag should no longer be added.

include/block/blockjob.h line 2104

Quoted 3 lines
include/block/blockjob.h +3 -0
+#         in sync, please not that since 2.12 it's semantic is not exactly the
+#         same as before, from 1.3 to 2.11 it means whether to allow cancellation
+#         of a paused job (default false).  Since 1.3.

Reads awkwardly. I suggest:

@force: If true, and the job has already emitted the event BLOCK_JOB_READY, abandon the job immediately (even if it is paused) instead of waiting for the destination to complete its final synchronization (since 1.3)

Quoted 25 lines
include/block/blockjob.h +4 -3
+++ b/tests/test-blockjob-txn.c
@@ -125,7 +125,7 @@ static void test_single_job(int expected)
     block_job_start(job);
 
     if (expected == -ECANCELED) {
-        block_job_cancel(job);
+        block_job_cancel(job, false);
     }
 
     while (result == -EINPROGRESS) {
@@ -173,10 +173,10 @@ static void test_pair_jobs(int expected1, int expected2)
     block_job_txn_unref(txn);
 
     if (expected1 == -ECANCELED) {
-        block_job_cancel(job1);
+        block_job_cancel(job1, false);
     }
     if (expected2 == -ECANCELED) {
-        block_job_cancel(job2);
+        block_job_cancel(job2, false);
     }
 
     while (result1 == -EINPROGRESS || result2 == -EINPROGRESS) {

include/block/blockjob.h static void test_pair_jobs_fail_cancel_race(void) · line 237

Quoted 8 lines
include/block/blockjob.h +1 -1
@@ -231,7 +231,7 @@ static void test_pair_jobs_fail_cancel_race(void)
     block_job_start(job1);
     block_job_start(job2);
 
-    block_job_cancel(job1);
+    block_job_cancel(job1, false);
 
     /* Now make job2 finish before the main loop kicks jobs.  This simulates
      * the race between a pending kick and another job completing.

The testsuite should probably also test block_job_cancel(..., true).

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#2 lore raw
In reply to Liang Li
Quoted 21 lines · Liang Li

When doing drive mirror to a low speed shared storage, if there was heavy BLK IO write workload in VM after the 'ready' event, drive mirror block job can't be canceled immediately, it would keep running until the heavy BLK IO workload stopped in the VM.

Because libvirt depends on block-job-cancel for block live migration, the current block-job-cancel has the semantic to make sure data is in sync after the 'ready' event. This semantic can't meet some requirement, for example, people may use drive mirror for realtime backup while need the ability of block live migration. If drive mirror can't not be cancelled immediately, it means block live migration need to wait, because libvirt make use drive mirror to implement block live migration and only one drive mirror block job is allowed at the same time for a give block dev.

We need a new interface for 'force cancel', which could quit block job immediately if don't care about whether data is in sync or not.

'force' is not used by libvirt currently, to make things simple, change it's semantic slightly, hope it will not break some use case which need its original semantic.

Quoted 9 lines
Cc Paolo Bonzini <[email protected]>
Cc Jeff Cody <[email protected]>
Cc Kevin Wolf <[email protected]>
Cc Max Reitz <[email protected]>
Cc Eric Blake <[email protected]>
Cc John Snow <[email protected]>

Just a note to JTC that this will conflict with my two series trying to refactor jobs:

(1) [Qemu-devel] [PATCH v2 00/13] blockjob: refactor mirror_throttle
(2) [Qemu-devel] [RFC v3 00/14] blockjobs: add explicit job management​

#3 lore raw
In reply to Liang Li
Quoted 45 lines · Liang Li

When doing drive mirror to a low speed shared storage, if there was heavy BLK IO write workload in VM after the 'ready' event, drive mirror block job can't be canceled immediately, it would keep running until the heavy BLK IO workload stopped in the VM.

Because libvirt depends on block-job-cancel for block live migration, the current block-job-cancel has the semantic to make sure data is in sync after the 'ready' event. This semantic can't meet some requirement, for example, people may use drive mirror for realtime backup while need the ability of block live migration. If drive mirror can't not be cancelled immediately, it means block live migration need to wait, because libvirt make use drive mirror to implement block live migration and only one drive mirror block job is allowed at the same time for a give block dev.

We need a new interface for 'force cancel', which could quit block job immediately if don't care about whether data is in sync or not.

'force' is not used by libvirt currently, to make things simple, change it's semantic slightly, hope it will not break some use case which need its original semantic.

Cc Paolo Bonzini <[email protected]>
Cc Jeff Cody <[email protected]>
Cc Kevin Wolf <[email protected]>
Cc Max Reitz <[email protected]>
Cc Eric Blake <[email protected]>
Cc John Snow <[email protected]>
---
 block/mirror.c            |  9 +++------
 blockdev.c                |  4 ++--
 blockjob.c                | 11 ++++++-----
 hmp-commands.hx           |  3 ++-
 include/block/blockjob.h  |  9 ++++++++-
 qapi/block-core.json      |  6 ++++--
 tests/test-blockjob-txn.c |  8 ++++----
 7 files changed, 29 insertions(+), 21 deletions(-)
block/mirror.c +0 -0
index c9badc1..c22dff9 100644

block/mirror.c static void coroutine_fn mirror_run(void *opaque) · line 873

Quoted 10 lines
block/mirror.c +2 -5
@@ -869,11 +869,8 @@ static void coroutine_fn mirror_run(void *opaque)
 
         ret = 0;
         trace_mirror_before_sleep(s, cnt, s->synced, delay_ns);
-        if (!s->synced) {
-            block_job_sleep_ns(&s->common, delay_ns);
-            if (block_job_is_cancelled(&s->common)) {
-                break;
-            }
+        if (block_job_is_cancelled(&s->common) && s->common.force) {
+            break;

what's the justification for removing the sleep in the case that !s->synced && !block_job_is_cancelled(...) ?

block/mirror.c immediate_exit: · line 887

Quoted 9 lines
block/mirror.c +1 -1
         } else if (!should_complete) {
             delay_ns = (s->in_flight == 0 && cnt == 0 ? SLICE_TIME : 0);
             block_job_sleep_ns(&s->common, delay_ns);
@@ -887,7 +884,7 @@ immediate_exit:
          * or it was cancelled prematurely so that we do not guarantee that
          * the target is a copy of the source.
          */
-        assert(ret < 0 || (!s->synced && block_job_is_cancelled(&s->common)));
+        assert(ret < 0 || block_job_is_cancelled(&s->common));

This assertion gets weaker in the case where force isn't provided, is that desired?

Quoted 39 lines
block/mirror.c +0 -0
         assert(need_drain);
         mirror_wait_for_all_io(s);
     }
blockdev.c +2 -2
index 8e977ee..039f156 100644
@@ -145,7 +145,7 @@ void blockdev_mark_auto_del(BlockBackend *blk)
         aio_context_acquire(aio_context);
 
         if (bs->job) {
-            block_job_cancel(bs->job);
+            block_job_cancel(bs->job, false);
         }
 
         aio_context_release(aio_context);
@@ -3802,7 +3802,7 @@ void qmp_block_job_cancel(const char *device,
     }
 
     trace_qmp_block_job_cancel(job);
-    block_job_cancel(job);
+    block_job_cancel(job, force);
 out:
     aio_context_release(aio_context);
 }
blockjob.c +1 -1
index f5cea84..0aacb50 100644
@@ -365,7 +365,7 @@ static void block_job_completed_single(BlockJob *job)
     block_job_unref(job);
 }
 
-static void block_job_cancel_async(BlockJob *job)
+static void block_job_cancel_async(BlockJob *job, bool force)
 {
     if (job->iostatus != BLOCK_DEVICE_IO_STATUS_OK) {
         block_job_iostatus_reset(job);

blockjob.c static void block_job_cancel_async(BlockJob *job) · line 381

Quoted 6 lines
blockjob.c +1 -0
@@ -376,6 +376,7 @@ static void block_job_cancel_async(BlockJob *job)
         job->pause_count--;
     }
     job->cancelled = true;
+    job->force = force;
 }
 

I suppose this is okay; we're setting a permanent property of the job as part of a limited operation.

Granted, the job should disappear afterwards, so it should never conflict, but it made me wonder for a moment.

What happens if we cancel with force = true and then immediately cancel again with force = false? What happens? Can it cause issues?

blockjob.c static void block_job_completed_txn_abort(BlockJob *job) · line 441

Quoted 7 lines
blockjob.c +1 -1
 static int block_job_finish_sync(BlockJob *job,
@@ -437,7 +438,7 @@ static void block_job_completed_txn_abort(BlockJob *job)
      * on the caller, so leave it. */
     QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
         if (other_job != job) {
-            block_job_cancel_async(other_job);
+            block_job_cancel_async(other_job, true);

What's the rationale for deciding that transactional cancellations are always force=true?

Granted, this doesn't matter currently because mirror isn't a transactional job, but that makes the decision all the more curious.

Quoted 114 lines
blockjob.c +3 -3
         }
     }
     while (!QLIST_EMPTY(&txn->jobs)) {
@@ -542,10 +543,10 @@ void block_job_user_resume(BlockJob *job)
     }
 }
 
-void block_job_cancel(BlockJob *job)
+void block_job_cancel(BlockJob *job, bool force)
 {
     if (block_job_started(job)) {
-        block_job_cancel_async(job);
+        block_job_cancel_async(job, force);
         block_job_enter(job);
     } else {
         block_job_completed(job, -ECANCELED);
@@ -557,7 +558,7 @@ void block_job_cancel(BlockJob *job)
  * function pointer casts there. */
 static void block_job_cancel_err(BlockJob *job, Error **errp)
 {
-    block_job_cancel(job);
+    block_job_cancel(job, false);
 }
 
 int block_job_cancel_sync(BlockJob *job)
hmp-commands.hx +2 -1
index 15620c9..c8bb414 100644
@@ -106,7 +106,8 @@ ETEXI
         .args_type  = "force:-f,device:B",
         .params     = "[-f] device",
         .help       = "stop an active background block operation (use -f"
-                      "\n\t\t\t if the operation is currently paused)",
+                      "\n\t\t\t if you want to abort the operation immediately"
+                      "\n\t\t\t instead of keep running until data is in sync )",
         .cmd        = hmp_block_job_cancel,
     },
 
include/block/blockjob.h +8 -1
index 00403d9..4a96c42 100644
@@ -63,6 +63,12 @@ typedef struct BlockJob {
     bool cancelled;
 
     /**
+     * Set to true if the job should be abort immediately without waiting
+     * for data is in sync.
+     */
+    bool force;
+
+    /**
      * Counter for pause request. If non-zero, the block job is either paused,
      * or if busy == true will pause itself as soon as possible.
      */
@@ -218,10 +224,11 @@ void block_job_start(BlockJob *job);
 /**
  * block_job_cancel:
  * @job: The job to be canceled.
+ * @force: Quit a job without waiting data is in sync.
  *
  * Asynchronously cancel the specified job.
  */
-void block_job_cancel(BlockJob *job);
+void block_job_cancel(BlockJob *job, bool force);
 
 /**
  * block_job_complete:
qapi/block-core.json +4 -2
index 8225308..7c4dbfe 100644
@@ -2098,8 +2098,10 @@
 #          the name of the parameter), but since QEMU 2.7 it can have
 #          other values.
 #
-# @force: whether to allow cancellation of a paused job (default
-#         false).  Since 1.3.
+# @force: #optional whether to allow cancellation a job without waiting data is
+#         in sync, please not that since 2.12 it's semantic is not exactly the
+#         same as before, from 1.3 to 2.11 it means whether to allow cancellation
+#         of a paused job (default false).  Since 1.3.
 #
 # Returns: Nothing on success
 #          If no background operation is active on this device, DeviceNotActive
tests/test-blockjob-txn.c +3 -3
index 3591c96..53daadb 100644
@@ -125,7 +125,7 @@ static void test_single_job(int expected)
     block_job_start(job);
 
     if (expected == -ECANCELED) {
-        block_job_cancel(job);
+        block_job_cancel(job, false);
     }
 
     while (result == -EINPROGRESS) {
@@ -173,10 +173,10 @@ static void test_pair_jobs(int expected1, int expected2)
     block_job_txn_unref(txn);
 
     if (expected1 == -ECANCELED) {
-        block_job_cancel(job1);
+        block_job_cancel(job1, false);
     }
     if (expected2 == -ECANCELED) {
-        block_job_cancel(job2);
+        block_job_cancel(job2, false);
     }
 
     while (result1 == -EINPROGRESS || result2 == -EINPROGRESS) {

tests/test-blockjob-txn.c static void test_pair_jobs_fail_cancel_race(void) · line 237

Quoted 8 lines
tests/test-blockjob-txn.c +1 -1
@@ -231,7 +231,7 @@ static void test_pair_jobs_fail_cancel_race(void)
     block_job_start(job1);
     block_job_start(job2);
 
-    block_job_cancel(job1);
+    block_job_cancel(job1, false);
 
     /* Now make job2 finish before the main loop kicks jobs.  This simulates
      * the race between a pending kick and another job completing.

--js

#4 lore raw
Quoted 260 lines · John Snow
Quoted 45 lines · Liang Li

When doing drive mirror to a low speed shared storage, if there was heavy BLK IO write workload in VM after the 'ready' event, drive mirror block job can't be canceled immediately, it would keep running until the heavy BLK IO workload stopped in the VM.

Because libvirt depends on block-job-cancel for block live migration, the current block-job-cancel has the semantic to make sure data is in sync after the 'ready' event. This semantic can't meet some requirement, for example, people may use drive mirror for realtime backup while need the ability of block live migration. If drive mirror can't not be cancelled immediately, it means block live migration need to wait, because libvirt make use drive mirror to implement block live migration and only one drive mirror block job is allowed at the same time for a give block dev.

We need a new interface for 'force cancel', which could quit block job immediately if don't care about whether data is in sync or not.

'force' is not used by libvirt currently, to make things simple, change it's semantic slightly, hope it will not break some use case which need its original semantic.

Cc Paolo Bonzini <[email protected]>
Cc Jeff Cody <[email protected]>
Cc Kevin Wolf <[email protected]>
Cc Max Reitz <[email protected]>
Cc Eric Blake <[email protected]>
Cc John Snow <[email protected]>
---
 block/mirror.c            |  9 +++------
 blockdev.c                |  4 ++--
 blockjob.c                | 11 ++++++-----
 hmp-commands.hx           |  3 ++-
 include/block/blockjob.h  |  9 ++++++++-
 qapi/block-core.json      |  6 ++++--
 tests/test-blockjob-txn.c |  8 ++++----
 7 files changed, 29 insertions(+), 21 deletions(-)
block/mirror.c +0 -0
index c9badc1..c22dff9 100644

block/mirror.c static void coroutine_fn mirror_run(void *opaque) · line 873

Quoted 10 lines
block/mirror.c +2 -5
@@ -869,11 +869,8 @@ static void coroutine_fn mirror_run(void *opaque)
 
         ret = 0;
         trace_mirror_before_sleep(s, cnt, s->synced, delay_ns);
-        if (!s->synced) {
-            block_job_sleep_ns(&s->common, delay_ns);
-            if (block_job_is_cancelled(&s->common)) {
-                break;
-            }
+        if (block_job_is_cancelled(&s->common) && s->common.force) {
+            break;

what's the justification for removing the sleep in the case that !s->synced && !block_job_is_cancelled(...) ?

block/mirror.c immediate_exit: · line 887

Quoted 9 lines
block/mirror.c +1 -1
         } else if (!should_complete) {
             delay_ns = (s->in_flight == 0 && cnt == 0 ? SLICE_TIME : 0);
             block_job_sleep_ns(&s->common, delay_ns);
@@ -887,7 +884,7 @@ immediate_exit:
          * or it was cancelled prematurely so that we do not guarantee that
          * the target is a copy of the source.
          */
-        assert(ret < 0 || (!s->synced && block_job_is_cancelled(&s->common)));
+        assert(ret < 0 || block_job_is_cancelled(&s->common));

This assertion gets weaker in the case where force isn't provided, is that desired?

Quoted 39 lines
block/mirror.c +0 -0
         assert(need_drain);
         mirror_wait_for_all_io(s);
     }
blockdev.c +2 -2
index 8e977ee..039f156 100644
@@ -145,7 +145,7 @@ void blockdev_mark_auto_del(BlockBackend *blk)
         aio_context_acquire(aio_context);
 
         if (bs->job) {
-            block_job_cancel(bs->job);
+            block_job_cancel(bs->job, false);
         }
 
         aio_context_release(aio_context);
@@ -3802,7 +3802,7 @@ void qmp_block_job_cancel(const char *device,
     }
 
     trace_qmp_block_job_cancel(job);
-    block_job_cancel(job);
+    block_job_cancel(job, force);
 out:
     aio_context_release(aio_context);
 }
blockjob.c +1 -1
index f5cea84..0aacb50 100644
@@ -365,7 +365,7 @@ static void block_job_completed_single(BlockJob *job)
     block_job_unref(job);
 }
 
-static void block_job_cancel_async(BlockJob *job)
+static void block_job_cancel_async(BlockJob *job, bool force)
 {
     if (job->iostatus != BLOCK_DEVICE_IO_STATUS_OK) {
         block_job_iostatus_reset(job);

blockjob.c static void block_job_cancel_async(BlockJob *job) · line 381

Quoted 6 lines
blockjob.c +1 -0
@@ -376,6 +376,7 @@ static void block_job_cancel_async(BlockJob *job)
         job->pause_count--;
     }
     job->cancelled = true;
+    job->force = force;
 }
 

I suppose this is okay; we're setting a permanent property of the job as part of a limited operation.

Granted, the job should disappear afterwards, so it should never conflict, but it made me wonder for a moment.

What happens if we cancel with force = true and then immediately cancel again with force = false? What happens? Can it cause issues?

blockjob.c static void block_job_completed_txn_abort(BlockJob *job) · line 441

Quoted 7 lines
blockjob.c +1 -1
 static int block_job_finish_sync(BlockJob *job,
@@ -437,7 +438,7 @@ static void block_job_completed_txn_abort(BlockJob *job)
      * on the caller, so leave it. */
     QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
         if (other_job != job) {
-            block_job_cancel_async(other_job);
+            block_job_cancel_async(other_job, true);

What's the rationale for deciding that transactional cancellations are always force=true?

Granted, this doesn't matter currently because mirror isn't a transactional job, but that makes the decision all the more curious.

Quoted 116 lines
blockjob.c +3 -3
         }
     }
     while (!QLIST_EMPTY(&txn->jobs)) {
@@ -542,10 +543,10 @@ void block_job_user_resume(BlockJob *job)
     }
 }
 
-void block_job_cancel(BlockJob *job)
+void block_job_cancel(BlockJob *job, bool force)
 {
     if (block_job_started(job)) {
-        block_job_cancel_async(job);
+        block_job_cancel_async(job, force);
         block_job_enter(job);
     } else {
         block_job_completed(job, -ECANCELED);
@@ -557,7 +558,7 @@ void block_job_cancel(BlockJob *job)
  * function pointer casts there. */
 static void block_job_cancel_err(BlockJob *job, Error **errp)
 {
-    block_job_cancel(job);
+    block_job_cancel(job, false);
 }
 
 int block_job_cancel_sync(BlockJob *job)
hmp-commands.hx +2 -1
index 15620c9..c8bb414 100644
@@ -106,7 +106,8 @@ ETEXI
         .args_type  = "force:-f,device:B",
         .params     = "[-f] device",
         .help       = "stop an active background block operation (use -f"
-                      "\n\t\t\t if the operation is currently paused)",
+                      "\n\t\t\t if you want to abort the operation immediately"
+                      "\n\t\t\t instead of keep running until data is in sync )",
         .cmd        = hmp_block_job_cancel,
     },
 
include/block/blockjob.h +8 -1
index 00403d9..4a96c42 100644
@@ -63,6 +63,12 @@ typedef struct BlockJob {
     bool cancelled;
 
     /**
+     * Set to true if the job should be abort immediately without waiting
+     * for data is in sync.
+     */
+    bool force;
+
+    /**
      * Counter for pause request. If non-zero, the block job is either paused,
      * or if busy == true will pause itself as soon as possible.
      */
@@ -218,10 +224,11 @@ void block_job_start(BlockJob *job);
 /**
  * block_job_cancel:
  * @job: The job to be canceled.
+ * @force: Quit a job without waiting data is in sync.
  *
  * Asynchronously cancel the specified job.
  */
-void block_job_cancel(BlockJob *job);
+void block_job_cancel(BlockJob *job, bool force);
 
 /**
  * block_job_complete:
qapi/block-core.json +4 -2
index 8225308..7c4dbfe 100644
@@ -2098,8 +2098,10 @@
 #          the name of the parameter), but since QEMU 2.7 it can have
 #          other values.
 #
-# @force: whether to allow cancellation of a paused job (default
-#         false).  Since 1.3.
+# @force: #optional whether to allow cancellation a job without waiting data is
+#         in sync, please not that since 2.12 it's semantic is not exactly the
+#         same as before, from 1.3 to 2.11 it means whether to allow cancellation
+#         of a paused job (default false).  Since 1.3.
 #
 # Returns: Nothing on success
 #          If no background operation is active on this device, DeviceNotActive
tests/test-blockjob-txn.c +3 -3
index 3591c96..53daadb 100644
@@ -125,7 +125,7 @@ static void test_single_job(int expected)
     block_job_start(job);
 
     if (expected == -ECANCELED) {
-        block_job_cancel(job);
+        block_job_cancel(job, false);
     }
 
     while (result == -EINPROGRESS) {
@@ -173,10 +173,10 @@ static void test_pair_jobs(int expected1, int expected2)
     block_job_txn_unref(txn);
 
     if (expected1 == -ECANCELED) {
-        block_job_cancel(job1);
+        block_job_cancel(job1, false);
     }
     if (expected2 == -ECANCELED) {
-        block_job_cancel(job2);
+        block_job_cancel(job2, false);
     }
 
     while (result1 == -EINPROGRESS || result2 == -EINPROGRESS) {
@@ -231,7 +231,7 @@ static void test_pair_jobs_fail_cancel_race(void)
     block_job_start(job1);
     block_job_start(job2);
 
Quoted 9 lines

tests/test-blockjob-txn.c static void test_pair_jobs_fail_cancel_race(void) · line 237

Quoted 6 lines
tests/test-blockjob-txn.c +1 -1
-    block_job_cancel(job1);
+    block_job_cancel(job1, false);
 
     /* Now make job2 finish before the main loop kicks jobs.  This simulates
      * the race between a pending kick and another job completing.

--js

This patch also causes an interesting regression in iotest 185:

185 1s ... - output mismatch (see 185.out.bad)
--- /home/bos/jhuston/src/qemu/tests/qemu-iotests/185.out 2017-12-21
16:15:50.879455552 -0500
+++ /home/bos/jhuston/src/qemu/bin/git/tests/qemu-iotests/185.out.bad

2018-01-30 15:38:47.936925014 -0500

@@ -28,16 +28,18 @@
{"return": {}}
{"return": {}}
{"timestamp": {"seconds": TIMESTAMP, "microseconds": TIMESTAMP},
"event": "SHUTDOWN", "data": {"guest": false}} -{"timestamp": {"seconds": TIMESTAMP, "microseconds": TIMESTAMP}, "event": "BLOCK_JOB_CANCELLED", "data": {"device": "disk", "len":
4194304, "offset": 4194304, "speed": 65536, "type": "commit"}} +{"timestamp": {"seconds": TIMESTAMP, "microseconds": TIMESTAMP}, "event": "BLOCK_JOB_READY", "data": {"device": "disk", "len": 4194304, "offset": 4194304, "speed": 65536, "type": "commit"}} +{"timestamp": {"seconds": TIMESTAMP, "microseconds": TIMESTAMP}, "event": "BLOCK_JOB_COMPLETED", "data": {"device": "disk", "len":
4194304, "offset": 4194304, "speed": 65536, "type": "commit"}}

=== Start mirror job and exit qemu ===

{"return": {}}
Formatting 'TEST_DIR/t.qcow2.copy', fmt=qcow2 size=67108864
cluster_size=65536 lazy_refcounts=off refcount_bits=16
{"return": {}}
+{"timestamp": {"seconds": TIMESTAMP, "microseconds": TIMESTAMP}, "event": "BLOCK_JOB_READY", "data": {"device": "disk", "len": 4194304, "offset": 4194304, "speed": 65536, "type": "mirror"}}
{"return": {}}
{"timestamp": {"seconds": TIMESTAMP, "microseconds": TIMESTAMP},
"event": "SHUTDOWN", "data": {"guest": false}} -{"timestamp": {"seconds": TIMESTAMP, "microseconds": TIMESTAMP}, "event": "BLOCK_JOB_CANCELLED", "data": {"device": "disk", "len":
4194304, "offset": 4194304, "speed": 65536, "type": "mirror"}} +{"timestamp": {"seconds": TIMESTAMP, "microseconds": TIMESTAMP}, "event": "BLOCK_JOB_COMPLETED", "data": {"device": "disk", "len":
4194304, "offset": 4194304, "speed": 65536, "type": "mirror"}}