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