26ba25
From 42e244782774dc971c83c4fabc16f46c57d86f21 Mon Sep 17 00:00:00 2001
26ba25
From: Kevin Wolf <kwolf@redhat.com>
26ba25
Date: Wed, 10 Oct 2018 20:22:09 +0100
26ba25
Subject: [PATCH 43/49] job: Avoid deadlocks in job_completed_txn_abort()
26ba25
26ba25
RH-Author: Kevin Wolf <kwolf@redhat.com>
26ba25
Message-id: <20181010202213.7372-31-kwolf@redhat.com>
26ba25
Patchwork-id: 82622
26ba25
O-Subject: [RHEL-8 qemu-kvm PATCH 40/44] job: Avoid deadlocks in job_completed_txn_abort()
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
Amongst others, job_finalize_single() calls the .prepare/.commit/.abort
26ba25
callbacks of the individual job driver. Recently, their use was adapted
26ba25
for all block jobs so that they involve code calling AIO_WAIT_WHILE()
26ba25
now. Such code must be called under the AioContext lock for the
26ba25
respective job, but without holding any other AioContext lock.
26ba25
26ba25
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
26ba25
Reviewed-by: Max Reitz <mreitz@redhat.com>
26ba25
(cherry picked from commit 644f3a29bd4974aefd46d2adb5062d86063c8a50)
26ba25
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
26ba25
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
26ba25
---
26ba25
 job.c | 16 +++++++++++-----
26ba25
 1 file changed, 11 insertions(+), 5 deletions(-)
26ba25
26ba25
diff --git a/job.c b/job.c
26ba25
index 42af9e2..5b53e43 100644
26ba25
--- a/job.c
26ba25
+++ b/job.c
26ba25
@@ -713,6 +713,7 @@ static void job_cancel_async(Job *job, bool force)
26ba25
 
26ba25
 static void job_completed_txn_abort(Job *job)
26ba25
 {
26ba25
+    AioContext *outer_ctx = job->aio_context;
26ba25
     AioContext *ctx;
26ba25
     JobTxn *txn = job->txn;
26ba25
     Job *other_job;
26ba25
@@ -726,23 +727,26 @@ static void job_completed_txn_abort(Job *job)
26ba25
     txn->aborting = true;
26ba25
     job_txn_ref(txn);
26ba25
 
26ba25
-    /* We are the first failed job. Cancel other jobs. */
26ba25
-    QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
26ba25
-        ctx = other_job->aio_context;
26ba25
-        aio_context_acquire(ctx);
26ba25
-    }
26ba25
+    /* We can only hold the single job's AioContext lock while calling
26ba25
+     * job_finalize_single() because the finalization callbacks can involve
26ba25
+     * calls of AIO_WAIT_WHILE(), which could deadlock otherwise. */
26ba25
+    aio_context_release(outer_ctx);
26ba25
 
26ba25
     /* Other jobs are effectively cancelled by us, set the status for
26ba25
      * them; this job, however, may or may not be cancelled, depending
26ba25
      * on the caller, so leave it. */
26ba25
     QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
26ba25
         if (other_job != job) {
26ba25
+            ctx = other_job->aio_context;
26ba25
+            aio_context_acquire(ctx);
26ba25
             job_cancel_async(other_job, false);
26ba25
+            aio_context_release(ctx);
26ba25
         }
26ba25
     }
26ba25
     while (!QLIST_EMPTY(&txn->jobs)) {
26ba25
         other_job = QLIST_FIRST(&txn->jobs);
26ba25
         ctx = other_job->aio_context;
26ba25
+        aio_context_acquire(ctx);
26ba25
         if (!job_is_completed(other_job)) {
26ba25
             assert(job_is_cancelled(other_job));
26ba25
             job_finish_sync(other_job, NULL, NULL);
26ba25
@@ -751,6 +755,8 @@ static void job_completed_txn_abort(Job *job)
26ba25
         aio_context_release(ctx);
26ba25
     }
26ba25
 
26ba25
+    aio_context_acquire(outer_ctx);
26ba25
+
26ba25
     job_txn_unref(txn);
26ba25
 }
26ba25
 
26ba25
-- 
26ba25
1.8.3.1
26ba25