Blame SOURCES/kvm-jobs-add-exit-shim.patch

1bdc94
From 8ca94edad4b07d6191bc4761a44d8289b0f8ad5d Mon Sep 17 00:00:00 2001
1bdc94
From: John Snow <jsnow@redhat.com>
1bdc94
Date: Mon, 10 Sep 2018 18:17:41 +0200
1bdc94
Subject: [PATCH 03/25] jobs: add exit shim
1bdc94
1bdc94
RH-Author: John Snow <jsnow@redhat.com>
1bdc94
Message-id: <20180910181803.11781-4-jsnow@redhat.com>
1bdc94
Patchwork-id: 82104
1bdc94
O-Subject: [RHEL-7.6 qemu-kvm-rhev PATCH 03/25] jobs: add exit shim
1bdc94
Bugzilla: 1626061
1bdc94
RH-Acked-by: Max Reitz <mreitz@redhat.com>
1bdc94
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
1bdc94
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
1bdc94
1bdc94
All jobs do the same thing when they leave their running loop:
1bdc94
- Store the return code in a structure
1bdc94
- wait to receive this structure in the main thread
1bdc94
- signal job completion via job_completed
1bdc94
1bdc94
Few jobs do anything beyond exactly this. Consolidate this exit
1bdc94
logic for a net reduction in SLOC.
1bdc94
1bdc94
More seriously, when we utilize job_defer_to_main_loop_bh to call
1bdc94
a function that calls job_completed, job_finalize_single will run
1bdc94
in a context where it has recursively taken the aio_context lock,
1bdc94
which can cause hangs if it puts down a reference that causes a flush.
1bdc94
1bdc94
You can observe this in practice by looking at mirror_exit's careful
1bdc94
placement of job_completed and bdrv_unref calls.
1bdc94
1bdc94
If we centralize job exiting, we can signal job completion from outside
1bdc94
of the aio_context, which should allow for job cleanup code to run with
1bdc94
only one lock, which makes cleanup callbacks less tricky to write.
1bdc94
1bdc94
Signed-off-by: John Snow <jsnow@redhat.com>
1bdc94
Reviewed-by: Max Reitz <mreitz@redhat.com>
1bdc94
Message-id: 20180830015734.19765-4-jsnow@redhat.com
1bdc94
Reviewed-by: Jeff Cody <jcody@redhat.com>
1bdc94
Signed-off-by: Max Reitz <mreitz@redhat.com>
1bdc94
(cherry picked from commit 00359a71d45a414ee47d8e423104dc0afd24ec65)
1bdc94
Signed-off-by: John Snow <jsnow@redhat.com>
1bdc94
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
1bdc94
---
1bdc94
 include/qemu/job.h | 11 +++++++++++
1bdc94
 job.c              | 18 ++++++++++++++++++
1bdc94
 2 files changed, 29 insertions(+)
1bdc94
1bdc94
diff --git a/include/qemu/job.h b/include/qemu/job.h
1bdc94
index 905dfdd..24b5f3f 100644
1bdc94
--- a/include/qemu/job.h
1bdc94
+++ b/include/qemu/job.h
1bdc94
@@ -209,6 +209,17 @@ struct JobDriver {
1bdc94
     void (*drain)(Job *job);
1bdc94
 
1bdc94
     /**
1bdc94
+     * If the callback is not NULL, exit will be invoked from the main thread
1bdc94
+     * when the job's coroutine has finished, but before transactional
1bdc94
+     * convergence; before @prepare or @abort.
1bdc94
+     *
1bdc94
+     * FIXME TODO: This callback is only temporary to transition remaining jobs
1bdc94
+     * to prepare/commit/abort/clean callbacks and will be removed before 3.1.
1bdc94
+     * is released.
1bdc94
+     */
1bdc94
+    void (*exit)(Job *job);
1bdc94
+
1bdc94
+    /**
1bdc94
      * If the callback is not NULL, prepare will be invoked when all the jobs
1bdc94
      * belonging to the same transaction complete; or upon this job's completion
1bdc94
      * if it is not in a transaction.
1bdc94
diff --git a/job.c b/job.c
1bdc94
index 020ee0d..a814857 100644
1bdc94
--- a/job.c
1bdc94
+++ b/job.c
1bdc94
@@ -530,6 +530,18 @@ void job_drain(Job *job)
1bdc94
     }
1bdc94
 }
1bdc94
 
1bdc94
+static void job_exit(void *opaque)
1bdc94
+{
1bdc94
+    Job *job = (Job *)opaque;
1bdc94
+    AioContext *aio_context = job->aio_context;
1bdc94
+
1bdc94
+    if (job->driver->exit) {
1bdc94
+        aio_context_acquire(aio_context);
1bdc94
+        job->driver->exit(job);
1bdc94
+        aio_context_release(aio_context);
1bdc94
+    }
1bdc94
+    job_completed(job, job->ret);
1bdc94
+}
1bdc94
 
1bdc94
 /**
1bdc94
  * All jobs must allow a pause point before entering their job proper. This
1bdc94
@@ -542,6 +554,12 @@ static void coroutine_fn job_co_entry(void *opaque)
1bdc94
     assert(job && job->driver && job->driver->run);
1bdc94
     job_pause_point(job);
1bdc94
     job->ret = job->driver->run(job, &job->err);
1bdc94
+    if (!job->deferred_to_main_loop) {
1bdc94
+        job->deferred_to_main_loop = true;
1bdc94
+        aio_bh_schedule_oneshot(qemu_get_aio_context(),
1bdc94
+                                job_exit,
1bdc94
+                                job);
1bdc94
+    }
1bdc94
 }
1bdc94
 
1bdc94
 
1bdc94
-- 
1bdc94
1.8.3.1
1bdc94