Blame SOURCES/kvm-blockjob-Wake-up-BDS-when-job-becomes-idle.patch

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