26ba25
From c48802abf2f0912ce3c34775587f674b037939ac Mon Sep 17 00:00:00 2001
26ba25
From: Kevin Wolf <kwolf@redhat.com>
26ba25
Date: Wed, 10 Oct 2018 20:21:56 +0100
26ba25
Subject: [PATCH 30/49] blockjob: Wake up BDS when job becomes idle
26ba25
26ba25
RH-Author: Kevin Wolf <kwolf@redhat.com>
26ba25
Message-id: <20181010202213.7372-18-kwolf@redhat.com>
26ba25
Patchwork-id: 82610
26ba25
O-Subject: [RHEL-8 qemu-kvm PATCH 27/44] blockjob: Wake up BDS when job becomes idle
26ba25
Bugzilla: 1637976
26ba25
RH-Acked-by: Max Reitz <mreitz@redhat.com>
26ba25
RH-Acked-by: John Snow <jsnow@redhat.com>
26ba25
RH-Acked-by: Thomas Huth <thuth@redhat.com>
26ba25
26ba25
In the context of draining a BDS, the .drained_poll callback of block
26ba25
jobs is called. If this returns true (i.e. there is still some activity
26ba25
pending), the drain operation may call aio_poll() with blocking=true to
26ba25
wait for completion.
26ba25
26ba25
As soon as the pending activity is completed and the job finally arrives
26ba25
in a quiescent state (i.e. its coroutine either yields with busy=false
26ba25
or terminates), the block job must notify the aio_poll() loop to wake
26ba25
up, otherwise we get a deadlock if both are running in different
26ba25
threads.
26ba25
26ba25
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
26ba25
Reviewed-by: Fam Zheng <famz@redhat.com>
26ba25
Reviewed-by: Max Reitz <mreitz@redhat.com>
26ba25
(cherry picked from commit 34dc97b9a0e592bc466bdb0bbfe45d77304a72b6)
26ba25
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
26ba25
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
26ba25
---
26ba25
 blockjob.c               | 18 ++++++++++++++++++
26ba25
 include/block/blockjob.h | 13 +++++++++++++
26ba25
 include/qemu/job.h       |  3 +++
26ba25
 job.c                    |  7 +++++++
26ba25
 4 files changed, 41 insertions(+)
26ba25
26ba25
diff --git a/blockjob.c b/blockjob.c
26ba25
index be5903a..8d27e8e 100644
26ba25
--- a/blockjob.c
26ba25
+++ b/blockjob.c
26ba25
@@ -221,6 +221,22 @@ int block_job_add_bdrv(BlockJob *job, const char *name, BlockDriverState *bs,
26ba25
     return 0;
26ba25
 }
26ba25
 
26ba25
+void block_job_wakeup_all_bdrv(BlockJob *job)
26ba25
+{
26ba25
+    GSList *l;
26ba25
+
26ba25
+    for (l = job->nodes; l; l = l->next) {
26ba25
+        BdrvChild *c = l->data;
26ba25
+        bdrv_wakeup(c->bs);
26ba25
+    }
26ba25
+}
26ba25
+
26ba25
+static void block_job_on_idle(Notifier *n, void *opaque)
26ba25
+{
26ba25
+    BlockJob *job = opaque;
26ba25
+    block_job_wakeup_all_bdrv(job);
26ba25
+}
26ba25
+
26ba25
 bool block_job_is_internal(BlockJob *job)
26ba25
 {
26ba25
     return (job->job.id == NULL);
26ba25
@@ -419,6 +435,7 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,
26ba25
     job->finalize_completed_notifier.notify = block_job_event_completed;
26ba25
     job->pending_notifier.notify = block_job_event_pending;
26ba25
     job->ready_notifier.notify = block_job_event_ready;
26ba25
+    job->idle_notifier.notify = block_job_on_idle;
26ba25
 
26ba25
     notifier_list_add(&job->job.on_finalize_cancelled,
26ba25
                       &job->finalize_cancelled_notifier);
26ba25
@@ -426,6 +443,7 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,
26ba25
                       &job->finalize_completed_notifier);
26ba25
     notifier_list_add(&job->job.on_pending, &job->pending_notifier);
26ba25
     notifier_list_add(&job->job.on_ready, &job->ready_notifier);
26ba25
+    notifier_list_add(&job->job.on_idle, &job->idle_notifier);
26ba25
 
26ba25
     error_setg(&job->blocker, "block device is in use by block job: %s",
26ba25
                job_type_str(&job->job));
26ba25
diff --git a/include/block/blockjob.h b/include/block/blockjob.h
26ba25
index 32c00b7..2290bbb 100644
26ba25
--- a/include/block/blockjob.h
26ba25
+++ b/include/block/blockjob.h
26ba25
@@ -70,6 +70,9 @@ typedef struct BlockJob {
26ba25
     /** Called when the job transitions to READY */
26ba25
     Notifier ready_notifier;
26ba25
 
26ba25
+    /** Called when the job coroutine yields or terminates */
26ba25
+    Notifier idle_notifier;
26ba25
+
26ba25
     /** BlockDriverStates that are involved in this block job */
26ba25
     GSList *nodes;
26ba25
 } BlockJob;
26ba25
@@ -119,6 +122,16 @@ int block_job_add_bdrv(BlockJob *job, const char *name, BlockDriverState *bs,
26ba25
 void block_job_remove_all_bdrv(BlockJob *job);
26ba25
 
26ba25
 /**
26ba25
+ * block_job_wakeup_all_bdrv:
26ba25
+ * @job: The block job
26ba25
+ *
26ba25
+ * Calls bdrv_wakeup() for all BlockDriverStates that have been added to the
26ba25
+ * job. This function is to be called whenever child_job_drained_poll() would
26ba25
+ * go from true to false to notify waiting drain requests.
26ba25
+ */
26ba25
+void block_job_wakeup_all_bdrv(BlockJob *job);
26ba25
+
26ba25
+/**
26ba25
  * block_job_set_speed:
26ba25
  * @job: The job to set the speed for.
26ba25
  * @speed: The new value
26ba25
diff --git a/include/qemu/job.h b/include/qemu/job.h
26ba25
index fdaa06f..407d549 100644
26ba25
--- a/include/qemu/job.h
26ba25
+++ b/include/qemu/job.h
26ba25
@@ -156,6 +156,9 @@ typedef struct Job {
26ba25
     /** Notifiers called when the job transitions to READY */
26ba25
     NotifierList on_ready;
26ba25
 
26ba25
+    /** Notifiers called when the job coroutine yields or terminates */
26ba25
+    NotifierList on_idle;
26ba25
+
26ba25
     /** Element of the list of jobs */
26ba25
     QLIST_ENTRY(Job) job_list;
26ba25
 
26ba25
diff --git a/job.c b/job.c
26ba25
index db53163..5a0ccc7 100644
26ba25
--- a/job.c
26ba25
+++ b/job.c
26ba25
@@ -397,6 +397,11 @@ static void job_event_ready(Job *job)
26ba25
     notifier_list_notify(&job->on_ready, job);
26ba25
 }
26ba25
 
26ba25
+static void job_event_idle(Job *job)
26ba25
+{
26ba25
+    notifier_list_notify(&job->on_idle, job);
26ba25
+}
26ba25
+
26ba25
 void job_enter_cond(Job *job, bool(*fn)(Job *job))
26ba25
 {
26ba25
     if (!job_started(job)) {
26ba25
@@ -442,6 +447,7 @@ static void coroutine_fn job_do_yield(Job *job, uint64_t ns)
26ba25
         timer_mod(&job->sleep_timer, ns);
26ba25
     }
26ba25
     job->busy = false;
26ba25
+    job_event_idle(job);
26ba25
     job_unlock();
26ba25
     qemu_coroutine_yield();
26ba25
 
26ba25
@@ -860,6 +866,7 @@ static void coroutine_fn job_co_entry(void *opaque)
26ba25
     assert(job && job->driver && job->driver->run);
26ba25
     job_pause_point(job);
26ba25
     job->ret = job->driver->run(job, &job->err);
26ba25
+    job_event_idle(job);
26ba25
     job->deferred_to_main_loop = true;
26ba25
     aio_bh_schedule_oneshot(qemu_get_aio_context(), job_exit, job);
26ba25
 }
26ba25
-- 
26ba25
1.8.3.1
26ba25