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

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