Blame SOURCES/kvm-block-Add-missing-locking-in-bdrv_co_drain_bh_cb.patch

357786
From e0603f9fadc9d8cabd0d47c09dc99880aafcc905 Mon Sep 17 00:00:00 2001
357786
From: Kevin Wolf <kwolf@redhat.com>
357786
Date: Fri, 14 Sep 2018 10:55:31 +0200
357786
Subject: [PATCH 40/49] block: Add missing locking in bdrv_co_drain_bh_cb()
357786
357786
RH-Author: Kevin Wolf <kwolf@redhat.com>
357786
Message-id: <20180914105540.18077-34-kwolf@redhat.com>
357786
Patchwork-id: 82185
357786
O-Subject: [RHV-7.6 qemu-kvm-rhev PATCH 33/42] block: Add missing locking in bdrv_co_drain_bh_cb()
357786
Bugzilla: 1601212
357786
RH-Acked-by: John Snow <jsnow@redhat.com>
357786
RH-Acked-by: Max Reitz <mreitz@redhat.com>
357786
RH-Acked-by: Fam Zheng <famz@redhat.com>
357786
357786
bdrv_do_drained_begin/end() assume that they are called with the
357786
AioContext lock of bs held. If we call drain functions from a coroutine
357786
with the AioContext lock held, we yield and schedule a BH to move out of
357786
coroutine context. This means that the lock for the home context of the
357786
coroutine is released and must be re-acquired in the bottom half.
357786
357786
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
357786
Reviewed-by: Max Reitz <mreitz@redhat.com>
357786
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
357786
---
357786
 block/io.c               | 15 +++++++++++++++
357786
 include/qemu/coroutine.h |  5 +++++
357786
 util/qemu-coroutine.c    |  5 +++++
357786
 3 files changed, 25 insertions(+)
357786
357786
diff --git a/block/io.c b/block/io.c
357786
index d404088..19db35e 100644
357786
--- a/block/io.c
357786
+++ b/block/io.c
357786
@@ -286,6 +286,18 @@ static void bdrv_co_drain_bh_cb(void *opaque)
357786
     BlockDriverState *bs = data->bs;
357786
 
357786
     if (bs) {
357786
+        AioContext *ctx = bdrv_get_aio_context(bs);
357786
+        AioContext *co_ctx = qemu_coroutine_get_aio_context(co);
357786
+
357786
+        /*
357786
+         * When the coroutine yielded, the lock for its home context was
357786
+         * released, so we need to re-acquire it here. If it explicitly
357786
+         * acquired a different context, the lock is still held and we don't
357786
+         * want to lock it a second time (or AIO_WAIT_WHILE() would hang).
357786
+         */
357786
+        if (ctx == co_ctx) {
357786
+            aio_context_acquire(ctx);
357786
+        }
357786
         bdrv_dec_in_flight(bs);
357786
         if (data->begin) {
357786
             bdrv_do_drained_begin(bs, data->recursive, data->parent,
357786
@@ -294,6 +306,9 @@ static void bdrv_co_drain_bh_cb(void *opaque)
357786
             bdrv_do_drained_end(bs, data->recursive, data->parent,
357786
                                 data->ignore_bds_parents);
357786
         }
357786
+        if (ctx == co_ctx) {
357786
+            aio_context_release(ctx);
357786
+        }
357786
     } else {
357786
         assert(data->begin);
357786
         bdrv_drain_all_begin();
357786
diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h
357786
index 6f8a487..9801e7f 100644
357786
--- a/include/qemu/coroutine.h
357786
+++ b/include/qemu/coroutine.h
357786
@@ -90,6 +90,11 @@ void qemu_aio_coroutine_enter(AioContext *ctx, Coroutine *co);
357786
 void coroutine_fn qemu_coroutine_yield(void);
357786
 
357786
 /**
357786
+ * Get the AioContext of the given coroutine
357786
+ */
357786
+AioContext *coroutine_fn qemu_coroutine_get_aio_context(Coroutine *co);
357786
+
357786
+/**
357786
  * Get the currently executing coroutine
357786
  */
357786
 Coroutine *coroutine_fn qemu_coroutine_self(void);
357786
diff --git a/util/qemu-coroutine.c b/util/qemu-coroutine.c
357786
index 1ba4191..2295928 100644
357786
--- a/util/qemu-coroutine.c
357786
+++ b/util/qemu-coroutine.c
357786
@@ -198,3 +198,8 @@ bool qemu_coroutine_entered(Coroutine *co)
357786
 {
357786
     return co->caller;
357786
 }
357786
+
357786
+AioContext *coroutine_fn qemu_coroutine_get_aio_context(Coroutine *co)
357786
+{
357786
+    return co->ctx;
357786
+}
357786
-- 
357786
1.8.3.1
357786