Blame SOURCES/kvm-job-Avoid-deadlocks-in-job_completed_txn_abort.patch

1bdc94
From c933e15569c9d9b3db7031d8c54712f819816f39 Mon Sep 17 00:00:00 2001
1bdc94
From: Kevin Wolf <kwolf@redhat.com>
1bdc94
Date: Fri, 14 Sep 2018 10:55:39 +0200
1bdc94
Subject: [PATCH 48/49] job: Avoid deadlocks in job_completed_txn_abort()
1bdc94
1bdc94
RH-Author: Kevin Wolf <kwolf@redhat.com>
1bdc94
Message-id: <20180914105540.18077-42-kwolf@redhat.com>
1bdc94
Patchwork-id: 82194
1bdc94
O-Subject: [RHV-7.6 qemu-kvm-rhev PATCH 41/42] job: Avoid deadlocks in job_completed_txn_abort()
1bdc94
Bugzilla: 1601212
1bdc94
RH-Acked-by: John Snow <jsnow@redhat.com>
1bdc94
RH-Acked-by: Max Reitz <mreitz@redhat.com>
1bdc94
RH-Acked-by: Fam Zheng <famz@redhat.com>
1bdc94
1bdc94
Amongst others, job_finalize_single() calls the .prepare/.commit/.abort
1bdc94
callbacks of the individual job driver. Recently, their use was adapted
1bdc94
for all block jobs so that they involve code calling AIO_WAIT_WHILE()
1bdc94
now. Such code must be called under the AioContext lock for the
1bdc94
respective job, but without holding any other AioContext lock.
1bdc94
1bdc94
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
1bdc94
Reviewed-by: Max Reitz <mreitz@redhat.com>
1bdc94
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
1bdc94
---
1bdc94
 job.c | 16 +++++++++++-----
1bdc94
 1 file changed, 11 insertions(+), 5 deletions(-)
1bdc94
1bdc94
diff --git a/job.c b/job.c
1bdc94
index 42af9e2..5b53e43 100644
1bdc94
--- a/job.c
1bdc94
+++ b/job.c
1bdc94
@@ -713,6 +713,7 @@ static void job_cancel_async(Job *job, bool force)
1bdc94
 
1bdc94
 static void job_completed_txn_abort(Job *job)
1bdc94
 {
1bdc94
+    AioContext *outer_ctx = job->aio_context;
1bdc94
     AioContext *ctx;
1bdc94
     JobTxn *txn = job->txn;
1bdc94
     Job *other_job;
1bdc94
@@ -726,23 +727,26 @@ static void job_completed_txn_abort(Job *job)
1bdc94
     txn->aborting = true;
1bdc94
     job_txn_ref(txn);
1bdc94
 
1bdc94
-    /* We are the first failed job. Cancel other jobs. */
1bdc94
-    QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
1bdc94
-        ctx = other_job->aio_context;
1bdc94
-        aio_context_acquire(ctx);
1bdc94
-    }
1bdc94
+    /* We can only hold the single job's AioContext lock while calling
1bdc94
+     * job_finalize_single() because the finalization callbacks can involve
1bdc94
+     * calls of AIO_WAIT_WHILE(), which could deadlock otherwise. */
1bdc94
+    aio_context_release(outer_ctx);
1bdc94
 
1bdc94
     /* Other jobs are effectively cancelled by us, set the status for
1bdc94
      * them; this job, however, may or may not be cancelled, depending
1bdc94
      * on the caller, so leave it. */
1bdc94
     QLIST_FOREACH(other_job, &txn->jobs, txn_list) {
1bdc94
         if (other_job != job) {
1bdc94
+            ctx = other_job->aio_context;
1bdc94
+            aio_context_acquire(ctx);
1bdc94
             job_cancel_async(other_job, false);
1bdc94
+            aio_context_release(ctx);
1bdc94
         }
1bdc94
     }
1bdc94
     while (!QLIST_EMPTY(&txn->jobs)) {
1bdc94
         other_job = QLIST_FIRST(&txn->jobs);
1bdc94
         ctx = other_job->aio_context;
1bdc94
+        aio_context_acquire(ctx);
1bdc94
         if (!job_is_completed(other_job)) {
1bdc94
             assert(job_is_cancelled(other_job));
1bdc94
             job_finish_sync(other_job, NULL, NULL);
1bdc94
@@ -751,6 +755,8 @@ static void job_completed_txn_abort(Job *job)
1bdc94
         aio_context_release(ctx);
1bdc94
     }
1bdc94
 
1bdc94
+    aio_context_acquire(outer_ctx);
1bdc94
+
1bdc94
     job_txn_unref(txn);
1bdc94
 }
1bdc94
 
1bdc94
-- 
1bdc94
1.8.3.1
1bdc94