Blame SOURCES/kvm-blockjob-reimplement-block_job_sleep_ns-to-allow-can.patch

4a2fec
From b3526a8557aac40fa4f4520518a1193cc92598e6 Mon Sep 17 00:00:00 2001
4a2fec
From: Jeffrey Cody <jcody@redhat.com>
4a2fec
Date: Thu, 30 Nov 2017 22:49:15 +0100
4a2fec
Subject: [PATCH 11/21] blockjob: reimplement block_job_sleep_ns to allow
4a2fec
 cancellation
4a2fec
4a2fec
RH-Author: Jeffrey Cody <jcody@redhat.com>
4a2fec
Message-id: <51b0c9ece9db7a164c4e7ccc90a7d51f786ceaa2.1511985875.git.jcody@redhat.com>
4a2fec
Patchwork-id: 78049
4a2fec
O-Subject: [RHV7.5 qemu-kvm-rhev PATCH 11/11] blockjob: reimplement block_job_sleep_ns to allow cancellation
4a2fec
Bugzilla: 1506531
4a2fec
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
4a2fec
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
4a2fec
RH-Acked-by: John Snow <jsnow@redhat.com>
4a2fec
4a2fec
From: Paolo Bonzini <pbonzini@redhat.com>
4a2fec
4a2fec
This reverts the effects of commit 4afeffc857 ("blockjob: do not allow
4a2fec
coroutine double entry or entry-after-completion", 2017-11-21)
4a2fec
4a2fec
This fixed the symptom of a bug rather than the root cause. Canceling the
4a2fec
wait on a sleeping blockjob coroutine is generally fine, we just need to
4a2fec
make it work correctly across AioContexts.  To do so, use a QEMUTimer
4a2fec
that calls block_job_enter.  Use a mutex to ensure that block_job_enter
4a2fec
synchronizes correctly with block_job_sleep_ns.
4a2fec
4a2fec
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
4a2fec
Tested-By: Jeff Cody <jcody@redhat.com>
4a2fec
Reviewed-by: Fam Zheng <famz@redhat.com>
4a2fec
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
4a2fec
Reviewed-by: Jeff Cody <jcody@redhat.com>
4a2fec
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
4a2fec
(cherry picked from commit fc24908e7d232b79c2a6f0363f52bda18dedb57b)
4a2fec
Signed-off-by: Jeff Cody <jcody@redhat.com>
4a2fec
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
4a2fec
---
4a2fec
 blockjob.c                   | 63 ++++++++++++++++++++++++++++++++++++--------
4a2fec
 include/block/blockjob.h     |  8 +++++-
4a2fec
 include/block/blockjob_int.h |  4 +--
4a2fec
 3 files changed, 61 insertions(+), 14 deletions(-)
4a2fec
4a2fec
diff --git a/blockjob.c b/blockjob.c
4a2fec
index e960b3e..84f526a 100644
4a2fec
--- a/blockjob.c
4a2fec
+++ b/blockjob.c
4a2fec
@@ -37,6 +37,26 @@
4a2fec
 #include "qemu/timer.h"
4a2fec
 #include "qapi-event.h"
4a2fec
 
4a2fec
+/* Right now, this mutex is only needed to synchronize accesses to job->busy
4a2fec
+ * and job->sleep_timer, such as concurrent calls to block_job_do_yield and
4a2fec
+ * block_job_enter. */
4a2fec
+static QemuMutex block_job_mutex;
4a2fec
+
4a2fec
+static void block_job_lock(void)
4a2fec
+{
4a2fec
+    qemu_mutex_lock(&block_job_mutex);
4a2fec
+}
4a2fec
+
4a2fec
+static void block_job_unlock(void)
4a2fec
+{
4a2fec
+    qemu_mutex_unlock(&block_job_mutex);
4a2fec
+}
4a2fec
+
4a2fec
+static void __attribute__((__constructor__)) block_job_init(void)
4a2fec
+{
4a2fec
+    qemu_mutex_init(&block_job_mutex);
4a2fec
+}
4a2fec
+
4a2fec
 static void block_job_event_cancelled(BlockJob *job);
4a2fec
 static void block_job_event_completed(BlockJob *job, const char *msg);
4a2fec
 
4a2fec
@@ -161,6 +181,7 @@ void block_job_unref(BlockJob *job)
4a2fec
         blk_unref(job->blk);
4a2fec
         error_free(job->blocker);
4a2fec
         g_free(job->id);
4a2fec
+        assert(!timer_pending(&job->sleep_timer));
4a2fec
         g_free(job);
4a2fec
     }
4a2fec
 }
4a2fec
@@ -287,6 +308,13 @@ static void coroutine_fn block_job_co_entry(void *opaque)
4a2fec
     job->driver->start(job);
4a2fec
 }
4a2fec
 
4a2fec
+static void block_job_sleep_timer_cb(void *opaque)
4a2fec
+{
4a2fec
+    BlockJob *job = opaque;
4a2fec
+
4a2fec
+    block_job_enter(job);
4a2fec
+}
4a2fec
+
4a2fec
 void block_job_start(BlockJob *job)
4a2fec
 {
4a2fec
     assert(job && !block_job_started(job) && job->paused &&
4a2fec
@@ -556,7 +584,7 @@ BlockJobInfo *block_job_query(BlockJob *job, Error **errp)
4a2fec
     info->type      = g_strdup(BlockJobType_lookup[job->driver->job_type]);
4a2fec
     info->device    = g_strdup(job->id);
4a2fec
     info->len       = job->len;
4a2fec
-    info->busy      = job->busy;
4a2fec
+    info->busy      = atomic_read(&job->busy);
4a2fec
     info->paused    = job->pause_count > 0;
4a2fec
     info->offset    = job->offset;
4a2fec
     info->speed     = job->speed;
4a2fec
@@ -664,6 +692,9 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,
4a2fec
     job->paused        = true;
4a2fec
     job->pause_count   = 1;
4a2fec
     job->refcnt        = 1;
4a2fec
+    aio_timer_init(qemu_get_aio_context(), &job->sleep_timer,
4a2fec
+                   QEMU_CLOCK_REALTIME, SCALE_NS,
4a2fec
+                   block_job_sleep_timer_cb, job);
4a2fec
 
4a2fec
     error_setg(&job->blocker, "block device is in use by block job: %s",
4a2fec
                BlockJobType_lookup[driver->job_type]);
4a2fec
@@ -729,9 +760,20 @@ static bool block_job_should_pause(BlockJob *job)
4a2fec
     return job->pause_count > 0;
4a2fec
 }
4a2fec
 
4a2fec
-static void block_job_do_yield(BlockJob *job)
4a2fec
+/* Yield, and schedule a timer to reenter the coroutine after @ns nanoseconds.
4a2fec
+ * Reentering the job coroutine with block_job_enter() before the timer has
4a2fec
+ * expired is allowed and cancels the timer.
4a2fec
+ *
4a2fec
+ * If @ns is (uint64_t) -1, no timer is scheduled and block_job_enter() must be
4a2fec
+ * called explicitly. */
4a2fec
+static void block_job_do_yield(BlockJob *job, uint64_t ns)
4a2fec
 {
4a2fec
+    block_job_lock();
4a2fec
+    if (ns != -1) {
4a2fec
+        timer_mod(&job->sleep_timer, ns);
4a2fec
+    }
4a2fec
     job->busy = false;
4a2fec
+    block_job_unlock();
4a2fec
     qemu_coroutine_yield();
4a2fec
 
4a2fec
     /* Set by block_job_enter before re-entering the coroutine.  */
4a2fec
@@ -755,7 +797,7 @@ void coroutine_fn block_job_pause_point(BlockJob *job)
4a2fec
 
4a2fec
     if (block_job_should_pause(job) && !block_job_is_cancelled(job)) {
4a2fec
         job->paused = true;
4a2fec
-        block_job_do_yield(job);
4a2fec
+        block_job_do_yield(job, -1);
4a2fec
         job->paused = false;
4a2fec
     }
4a2fec
 
4a2fec
@@ -785,11 +827,16 @@ void block_job_enter(BlockJob *job)
4a2fec
         return;
4a2fec
     }
4a2fec
 
4a2fec
+    block_job_lock();
4a2fec
     if (job->busy) {
4a2fec
+        block_job_unlock();
4a2fec
         return;
4a2fec
     }
4a2fec
 
4a2fec
+    assert(!job->deferred_to_main_loop);
4a2fec
+    timer_del(&job->sleep_timer);
4a2fec
     job->busy = true;
4a2fec
+    block_job_unlock();
4a2fec
     aio_co_wake(job->co);
4a2fec
 }
4a2fec
 
4a2fec
@@ -807,14 +854,8 @@ void block_job_sleep_ns(BlockJob *job, int64_t ns)
4a2fec
         return;
4a2fec
     }
4a2fec
 
4a2fec
-    /* We need to leave job->busy set here, because when we have
4a2fec
-     * put a coroutine to 'sleep', we have scheduled it to run in
4a2fec
-     * the future.  We cannot enter that same coroutine again before
4a2fec
-     * it wakes and runs, otherwise we risk double-entry or entry after
4a2fec
-     * completion. */
4a2fec
     if (!block_job_should_pause(job)) {
4a2fec
-        co_aio_sleep_ns(blk_get_aio_context(job->blk),
4a2fec
-                        QEMU_CLOCK_REALTIME, ns);
4a2fec
+        block_job_do_yield(job, qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + ns);
4a2fec
     }
4a2fec
 
4a2fec
     block_job_pause_point(job);
4a2fec
@@ -830,7 +871,7 @@ void block_job_yield(BlockJob *job)
4a2fec
     }
4a2fec
 
4a2fec
     if (!block_job_should_pause(job)) {
4a2fec
-        block_job_do_yield(job);
4a2fec
+        block_job_do_yield(job, -1);
4a2fec
     }
4a2fec
 
4a2fec
     block_job_pause_point(job);
4a2fec
diff --git a/include/block/blockjob.h b/include/block/blockjob.h
4a2fec
index 67c0968..00403d9 100644
4a2fec
--- a/include/block/blockjob.h
4a2fec
+++ b/include/block/blockjob.h
4a2fec
@@ -77,7 +77,7 @@ typedef struct BlockJob {
4a2fec
     /**
4a2fec
      * Set to false by the job while the coroutine has yielded and may be
4a2fec
      * re-entered by block_job_enter().  There may still be I/O or event loop
4a2fec
-     * activity pending.
4a2fec
+     * activity pending.  Accessed under block_job_mutex (in blockjob.c).
4a2fec
      */
4a2fec
     bool busy;
4a2fec
 
4a2fec
@@ -135,6 +135,12 @@ typedef struct BlockJob {
4a2fec
      */
4a2fec
     int ret;
4a2fec
 
4a2fec
+    /**
4a2fec
+     * Timer that is used by @block_job_sleep_ns. Accessed under
4a2fec
+     * block_job_mutex (in blockjob.c).
4a2fec
+     */
4a2fec
+    QEMUTimer sleep_timer;
4a2fec
+
4a2fec
     /** Non-NULL if this job is part of a transaction */
4a2fec
     BlockJobTxn *txn;
4a2fec
     QLIST_ENTRY(BlockJob) txn_list;
4a2fec
diff --git a/include/block/blockjob_int.h b/include/block/blockjob_int.h
4a2fec
index f7ab183..c9b23b0 100644
4a2fec
--- a/include/block/blockjob_int.h
4a2fec
+++ b/include/block/blockjob_int.h
4a2fec
@@ -142,8 +142,8 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,
4a2fec
  * @ns: How many nanoseconds to stop for.
4a2fec
  *
4a2fec
  * Put the job to sleep (assuming that it wasn't canceled) for @ns
4a2fec
- * %QEMU_CLOCK_REALTIME nanoseconds.  Canceling the job will not interrupt
4a2fec
- * the wait, so the cancel will not process until the coroutine wakes up.
4a2fec
+ * %QEMU_CLOCK_REALTIME nanoseconds.  Canceling the job will immediately
4a2fec
+ * interrupt the wait.
4a2fec
  */
4a2fec
 void block_job_sleep_ns(BlockJob *job, int64_t ns);
4a2fec
 
4a2fec
-- 
4a2fec
1.8.3.1
4a2fec