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