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

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