ae23c9
From 5da39ce2527a4c7e64543f1b0a3733a30cedbc9d Mon Sep 17 00:00:00 2001
ae23c9
From: Kevin Wolf <kwolf@redhat.com>
ae23c9
Date: Tue, 26 Jun 2018 09:48:10 +0200
ae23c9
Subject: [PATCH 102/268] job: Add job_sleep_ns()
ae23c9
ae23c9
RH-Author: Kevin Wolf <kwolf@redhat.com>
ae23c9
Message-id: <20180626094856.6924-28-kwolf@redhat.com>
ae23c9
Patchwork-id: 81117
ae23c9
O-Subject: [RHV-7.6 qemu-kvm-rhev PATCH v2 27/73] job: Add job_sleep_ns()
ae23c9
Bugzilla: 1513543
ae23c9
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
ae23c9
RH-Acked-by: Max Reitz <mreitz@redhat.com>
ae23c9
RH-Acked-by: Fam Zheng <famz@redhat.com>
ae23c9
ae23c9
There is nothing block layer specific about block_job_sleep_ns(), so
ae23c9
move the function to Job.
ae23c9
ae23c9
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
ae23c9
Reviewed-by: John Snow <jsnow@redhat.com>
ae23c9
Reviewed-by: Max Reitz <mreitz@redhat.com>
ae23c9
(cherry picked from commit 5d43e86e11f488fda7956b13160e0c0105a84845)
ae23c9
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
ae23c9
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
ae23c9
---
ae23c9
 block/backup.c               |  2 +-
ae23c9
 block/commit.c               |  2 +-
ae23c9
 block/mirror.c               |  4 ++--
ae23c9
 block/stream.c               |  2 +-
ae23c9
 blockjob.c                   | 27 ---------------------------
ae23c9
 include/block/blockjob_int.h | 11 -----------
ae23c9
 include/qemu/job.h           | 19 ++++++++++++++++++-
ae23c9
 job.c                        | 32 ++++++++++++++++++++++++++++++++
ae23c9
 tests/test-bdrv-drain.c      |  8 ++++----
ae23c9
 tests/test-blockjob-txn.c    |  2 +-
ae23c9
 tests/test-blockjob.c        |  2 +-
ae23c9
 11 files changed, 61 insertions(+), 50 deletions(-)
ae23c9
ae23c9
diff --git a/block/backup.c b/block/backup.c
ae23c9
index 7d9aad9..f3a4f7c 100644
ae23c9
--- a/block/backup.c
ae23c9
+++ b/block/backup.c
ae23c9
@@ -338,7 +338,7 @@ static bool coroutine_fn yield_and_check(BackupBlockJob *job)
ae23c9
      * return. Without a yield, the VM would not reboot. */
ae23c9
     delay_ns = block_job_ratelimit_get_delay(&job->common, job->bytes_read);
ae23c9
     job->bytes_read = 0;
ae23c9
-    block_job_sleep_ns(&job->common, delay_ns);
ae23c9
+    job_sleep_ns(&job->common.job, delay_ns);
ae23c9
 
ae23c9
     if (job_is_cancelled(&job->common.job)) {
ae23c9
         return true;
ae23c9
diff --git a/block/commit.c b/block/commit.c
ae23c9
index 2fbc310..1c6cb6c 100644
ae23c9
--- a/block/commit.c
ae23c9
+++ b/block/commit.c
ae23c9
@@ -172,7 +172,7 @@ static void coroutine_fn commit_run(void *opaque)
ae23c9
         /* Note that even when no rate limit is applied we need to yield
ae23c9
          * with no pending I/O here so that bdrv_drain_all() returns.
ae23c9
          */
ae23c9
-        block_job_sleep_ns(&s->common, delay_ns);
ae23c9
+        job_sleep_ns(&s->common.job, delay_ns);
ae23c9
         if (job_is_cancelled(&s->common.job)) {
ae23c9
             break;
ae23c9
         }
ae23c9
diff --git a/block/mirror.c b/block/mirror.c
ae23c9
index 95fc807..5d8f75c 100644
ae23c9
--- a/block/mirror.c
ae23c9
+++ b/block/mirror.c
ae23c9
@@ -595,7 +595,7 @@ static void mirror_throttle(MirrorBlockJob *s)
ae23c9
 
ae23c9
     if (now - s->last_pause_ns > BLOCK_JOB_SLICE_TIME) {
ae23c9
         s->last_pause_ns = now;
ae23c9
-        block_job_sleep_ns(&s->common, 0);
ae23c9
+        job_sleep_ns(&s->common.job, 0);
ae23c9
     } else {
ae23c9
         job_pause_point(&s->common.job);
ae23c9
     }
ae23c9
@@ -869,7 +869,7 @@ static void coroutine_fn mirror_run(void *opaque)
ae23c9
                         cnt == 0 ? BLOCK_JOB_SLICE_TIME : 0);
ae23c9
         }
ae23c9
         trace_mirror_before_sleep(s, cnt, s->synced, delay_ns);
ae23c9
-        block_job_sleep_ns(&s->common, delay_ns);
ae23c9
+        job_sleep_ns(&s->common.job, delay_ns);
ae23c9
         if (job_is_cancelled(&s->common.job) &&
ae23c9
             (!s->synced || s->common.force))
ae23c9
         {
ae23c9
diff --git a/block/stream.c b/block/stream.c
ae23c9
index 6d8b7b6..1faab02 100644
ae23c9
--- a/block/stream.c
ae23c9
+++ b/block/stream.c
ae23c9
@@ -140,7 +140,7 @@ static void coroutine_fn stream_run(void *opaque)
ae23c9
         /* Note that even when no rate limit is applied we need to yield
ae23c9
          * with no pending I/O here so that bdrv_drain_all() returns.
ae23c9
          */
ae23c9
-        block_job_sleep_ns(&s->common, delay_ns);
ae23c9
+        job_sleep_ns(&s->common.job, delay_ns);
ae23c9
         if (job_is_cancelled(&s->common.job)) {
ae23c9
             break;
ae23c9
         }
ae23c9
diff --git a/blockjob.c b/blockjob.c
ae23c9
index 313b1ff..4dc360c 100644
ae23c9
--- a/blockjob.c
ae23c9
+++ b/blockjob.c
ae23c9
@@ -181,7 +181,6 @@ void block_job_free(Job *job)
ae23c9
                                     block_job_detach_aio_context, bjob);
ae23c9
     blk_unref(bjob->blk);
ae23c9
     error_free(bjob->blocker);
ae23c9
-    assert(!timer_pending(&bjob->job.sleep_timer));
ae23c9
 }
ae23c9
 
ae23c9
 static void block_job_attached_aio_context(AioContext *new_context,
ae23c9
@@ -290,13 +289,6 @@ const BlockJobDriver *block_job_driver(BlockJob *job)
ae23c9
     return job->driver;
ae23c9
 }
ae23c9
 
ae23c9
-static void block_job_sleep_timer_cb(void *opaque)
ae23c9
-{
ae23c9
-    BlockJob *job = opaque;
ae23c9
-
ae23c9
-    block_job_enter(job);
ae23c9
-}
ae23c9
-
ae23c9
 static void block_job_decommission(BlockJob *job)
ae23c9
 {
ae23c9
     assert(job);
ae23c9
@@ -866,9 +858,6 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,
ae23c9
     job->opaque        = opaque;
ae23c9
     job->auto_finalize = !(flags & BLOCK_JOB_MANUAL_FINALIZE);
ae23c9
     job->auto_dismiss  = !(flags & BLOCK_JOB_MANUAL_DISMISS);
ae23c9
-    aio_timer_init(qemu_get_aio_context(), &job->job.sleep_timer,
ae23c9
-                   QEMU_CLOCK_REALTIME, SCALE_NS,
ae23c9
-                   block_job_sleep_timer_cb, job);
ae23c9
 
ae23c9
     error_setg(&job->blocker, "block device is in use by block job: %s",
ae23c9
                job_type_str(&job->job));
ae23c9
@@ -931,22 +920,6 @@ void block_job_enter(BlockJob *job)
ae23c9
     job_enter_cond(&job->job, NULL);
ae23c9
 }
ae23c9
 
ae23c9
-void block_job_sleep_ns(BlockJob *job, int64_t ns)
ae23c9
-{
ae23c9
-    assert(job->job.busy);
ae23c9
-
ae23c9
-    /* Check cancellation *before* setting busy = false, too!  */
ae23c9
-    if (job_is_cancelled(&job->job)) {
ae23c9
-        return;
ae23c9
-    }
ae23c9
-
ae23c9
-    if (!job_should_pause(&job->job)) {
ae23c9
-        job_do_yield(&job->job, qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + ns);
ae23c9
-    }
ae23c9
-
ae23c9
-    job_pause_point(&job->job);
ae23c9
-}
ae23c9
-
ae23c9
 void block_job_yield(BlockJob *job)
ae23c9
 {
ae23c9
     assert(job->job.busy);
ae23c9
diff --git a/include/block/blockjob_int.h b/include/block/blockjob_int.h
ae23c9
index 0a614a8..8937f5b 100644
ae23c9
--- a/include/block/blockjob_int.h
ae23c9
+++ b/include/block/blockjob_int.h
ae23c9
@@ -134,17 +134,6 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,
ae23c9
 void block_job_free(Job *job);
ae23c9
 
ae23c9
 /**
ae23c9
- * block_job_sleep_ns:
ae23c9
- * @job: The job that calls the function.
ae23c9
- * @ns: How many nanoseconds to stop for.
ae23c9
- *
ae23c9
- * Put the job to sleep (assuming that it wasn't canceled) for @ns
ae23c9
- * %QEMU_CLOCK_REALTIME nanoseconds.  Canceling the job will immediately
ae23c9
- * interrupt the wait.
ae23c9
- */
ae23c9
-void block_job_sleep_ns(BlockJob *job, int64_t ns);
ae23c9
-
ae23c9
-/**
ae23c9
  * block_job_yield:
ae23c9
  * @job: The job that calls the function.
ae23c9
  *
ae23c9
diff --git a/include/qemu/job.h b/include/qemu/job.h
ae23c9
index 9dcff12..509408f 100644
ae23c9
--- a/include/qemu/job.h
ae23c9
+++ b/include/qemu/job.h
ae23c9
@@ -58,7 +58,7 @@ typedef struct Job {
ae23c9
     Coroutine *co;
ae23c9
 
ae23c9
     /**
ae23c9
-     * Timer that is used by @block_job_sleep_ns. Accessed under job_mutex (in
ae23c9
+     * Timer that is used by @job_sleep_ns. Accessed under job_mutex (in
ae23c9
      * job.c).
ae23c9
      */
ae23c9
     QEMUTimer sleep_timer;
ae23c9
@@ -168,6 +168,13 @@ void job_enter_cond(Job *job, bool(*fn)(Job *job));
ae23c9
 void job_start(Job *job);
ae23c9
 
ae23c9
 /**
ae23c9
+ * @job: The job to enter.
ae23c9
+ *
ae23c9
+ * Continue the specified job by entering the coroutine.
ae23c9
+ */
ae23c9
+void job_enter(Job *job);
ae23c9
+
ae23c9
+/**
ae23c9
  * @job: The job that is ready to pause.
ae23c9
  *
ae23c9
  * Pause now if job_pause() has been called. Jobs that perform lots of I/O
ae23c9
@@ -175,6 +182,16 @@ void job_start(Job *job);
ae23c9
  */
ae23c9
 void coroutine_fn job_pause_point(Job *job);
ae23c9
 
ae23c9
+/**
ae23c9
+ * @job: The job that calls the function.
ae23c9
+ * @ns: How many nanoseconds to stop for.
ae23c9
+ *
ae23c9
+ * Put the job to sleep (assuming that it wasn't canceled) for @ns
ae23c9
+ * %QEMU_CLOCK_REALTIME nanoseconds.  Canceling the job will immediately
ae23c9
+ * interrupt the wait.
ae23c9
+ */
ae23c9
+void coroutine_fn job_sleep_ns(Job *job, int64_t ns);
ae23c9
+
ae23c9
 
ae23c9
 /** Returns the JobType of a given Job. */
ae23c9
 JobType job_type(const Job *job);
ae23c9
diff --git a/job.c b/job.c
ae23c9
index 78497fd..1b8cba1 100644
ae23c9
--- a/job.c
ae23c9
+++ b/job.c
ae23c9
@@ -152,6 +152,13 @@ Job *job_get(const char *id)
ae23c9
     return NULL;
ae23c9
 }
ae23c9
 
ae23c9
+static void job_sleep_timer_cb(void *opaque)
ae23c9
+{
ae23c9
+    Job *job = opaque;
ae23c9
+
ae23c9
+    job_enter(job);
ae23c9
+}
ae23c9
+
ae23c9
 void *job_create(const char *job_id, const JobDriver *driver, AioContext *ctx,
ae23c9
                  Error **errp)
ae23c9
 {
ae23c9
@@ -178,6 +185,9 @@ void *job_create(const char *job_id, const JobDriver *driver, AioContext *ctx,
ae23c9
     job->pause_count   = 1;
ae23c9
 
ae23c9
     job_state_transition(job, JOB_STATUS_CREATED);
ae23c9
+    aio_timer_init(qemu_get_aio_context(), &job->sleep_timer,
ae23c9
+                   QEMU_CLOCK_REALTIME, SCALE_NS,
ae23c9
+                   job_sleep_timer_cb, job);
ae23c9
 
ae23c9
     QLIST_INSERT_HEAD(&jobs, job, job_list);
ae23c9
 
ae23c9
@@ -193,6 +203,7 @@ void job_unref(Job *job)
ae23c9
 {
ae23c9
     if (--job->refcnt == 0) {
ae23c9
         assert(job->status == JOB_STATUS_NULL);
ae23c9
+        assert(!timer_pending(&job->sleep_timer));
ae23c9
 
ae23c9
         if (job->driver->free) {
ae23c9
             job->driver->free(job);
ae23c9
@@ -232,6 +243,11 @@ void job_enter_cond(Job *job, bool(*fn)(Job *job))
ae23c9
     aio_co_wake(job->co);
ae23c9
 }
ae23c9
 
ae23c9
+void job_enter(Job *job)
ae23c9
+{
ae23c9
+    job_enter_cond(job, NULL);
ae23c9
+}
ae23c9
+
ae23c9
 /* Yield, and schedule a timer to reenter the coroutine after @ns nanoseconds.
ae23c9
  * Reentering the job coroutine with block_job_enter() before the timer has
ae23c9
  * expired is allowed and cancels the timer.
ae23c9
@@ -283,6 +299,22 @@ void coroutine_fn job_pause_point(Job *job)
ae23c9
     }
ae23c9
 }
ae23c9
 
ae23c9
+void coroutine_fn job_sleep_ns(Job *job, int64_t ns)
ae23c9
+{
ae23c9
+    assert(job->busy);
ae23c9
+
ae23c9
+    /* Check cancellation *before* setting busy = false, too!  */
ae23c9
+    if (job_is_cancelled(job)) {
ae23c9
+        return;
ae23c9
+    }
ae23c9
+
ae23c9
+    if (!job_should_pause(job)) {
ae23c9
+        job_do_yield(job, qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + ns);
ae23c9
+    }
ae23c9
+
ae23c9
+    job_pause_point(job);
ae23c9
+}
ae23c9
+
ae23c9
 /**
ae23c9
  * All jobs must allow a pause point before entering their job proper. This
ae23c9
  * ensures that jobs can be paused prior to being started, then resumed later.
ae23c9
diff --git a/tests/test-bdrv-drain.c b/tests/test-bdrv-drain.c
ae23c9
index c9f2f9b..50232f5 100644
ae23c9
--- a/tests/test-bdrv-drain.c
ae23c9
+++ b/tests/test-bdrv-drain.c
ae23c9
@@ -508,7 +508,7 @@ static void coroutine_fn test_job_start(void *opaque)
ae23c9
 
ae23c9
     block_job_event_ready(&s->common);
ae23c9
     while (!s->should_complete) {
ae23c9
-        block_job_sleep_ns(&s->common, 100000);
ae23c9
+        job_sleep_ns(&s->common.job, 100000);
ae23c9
     }
ae23c9
 
ae23c9
     job_defer_to_main_loop(&s->common.job, test_job_completed, NULL);
ae23c9
@@ -553,7 +553,7 @@ static void test_blockjob_common(enum drain_type drain_type)
ae23c9
 
ae23c9
     g_assert_cmpint(job->job.pause_count, ==, 0);
ae23c9
     g_assert_false(job->job.paused);
ae23c9
-    g_assert_false(job->job.busy); /* We're in block_job_sleep_ns() */
ae23c9
+    g_assert_false(job->job.busy); /* We're in job_sleep_ns() */
ae23c9
 
ae23c9
     do_drain_begin(drain_type, src);
ae23c9
 
ae23c9
@@ -571,7 +571,7 @@ static void test_blockjob_common(enum drain_type drain_type)
ae23c9
 
ae23c9
     g_assert_cmpint(job->job.pause_count, ==, 0);
ae23c9
     g_assert_false(job->job.paused);
ae23c9
-    g_assert_false(job->job.busy); /* We're in block_job_sleep_ns() */
ae23c9
+    g_assert_false(job->job.busy); /* We're in job_sleep_ns() */
ae23c9
 
ae23c9
     do_drain_begin(drain_type, target);
ae23c9
 
ae23c9
@@ -589,7 +589,7 @@ static void test_blockjob_common(enum drain_type drain_type)
ae23c9
 
ae23c9
     g_assert_cmpint(job->job.pause_count, ==, 0);
ae23c9
     g_assert_false(job->job.paused);
ae23c9
-    g_assert_false(job->job.busy); /* We're in block_job_sleep_ns() */
ae23c9
+    g_assert_false(job->job.busy); /* We're in job_sleep_ns() */
ae23c9
 
ae23c9
     ret = block_job_complete_sync(job, &error_abort);
ae23c9
     g_assert_cmpint(ret, ==, 0);
ae23c9
diff --git a/tests/test-blockjob-txn.c b/tests/test-blockjob-txn.c
ae23c9
index 323e154..0e6162b 100644
ae23c9
--- a/tests/test-blockjob-txn.c
ae23c9
+++ b/tests/test-blockjob-txn.c
ae23c9
@@ -45,7 +45,7 @@ static void coroutine_fn test_block_job_run(void *opaque)
ae23c9
 
ae23c9
     while (s->iterations--) {
ae23c9
         if (s->use_timer) {
ae23c9
-            block_job_sleep_ns(job, 0);
ae23c9
+            job_sleep_ns(&job->job, 0);
ae23c9
         } else {
ae23c9
             block_job_yield(job);
ae23c9
         }
ae23c9
diff --git a/tests/test-blockjob.c b/tests/test-blockjob.c
ae23c9
index 1d18325..b329bd5 100644
ae23c9
--- a/tests/test-blockjob.c
ae23c9
+++ b/tests/test-blockjob.c
ae23c9
@@ -188,7 +188,7 @@ static void coroutine_fn cancel_job_start(void *opaque)
ae23c9
             block_job_event_ready(&s->common);
ae23c9
         }
ae23c9
 
ae23c9
-        block_job_sleep_ns(&s->common, 100000);
ae23c9
+        job_sleep_ns(&s->common.job, 100000);
ae23c9
     }
ae23c9
 
ae23c9
  defer:
ae23c9
-- 
ae23c9
1.8.3.1
ae23c9