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

26ba25
From 59f0fc4b8011298bc542eb23cca385d83d1963cd Mon Sep 17 00:00:00 2001
26ba25
From: Kevin Wolf <kwolf@redhat.com>
26ba25
Date: Wed, 10 Oct 2018 20:22:02 +0100
26ba25
Subject: [PATCH 36/49] block: Add missing locking in bdrv_co_drain_bh_cb()
26ba25
26ba25
RH-Author: Kevin Wolf <kwolf@redhat.com>
26ba25
Message-id: <20181010202213.7372-24-kwolf@redhat.com>
26ba25
Patchwork-id: 82613
26ba25
O-Subject: [RHEL-8 qemu-kvm PATCH 33/44] block: Add missing locking in bdrv_co_drain_bh_cb()
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
bdrv_do_drained_begin/end() assume that they are called with the
26ba25
AioContext lock of bs held. If we call drain functions from a coroutine
26ba25
with the AioContext lock held, we yield and schedule a BH to move out of
26ba25
coroutine context. This means that the lock for the home context of the
26ba25
coroutine is released and must be re-acquired in the bottom half.
26ba25
26ba25
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
26ba25
Reviewed-by: Max Reitz <mreitz@redhat.com>
26ba25
(cherry picked from commit aa1361d54aac43094b98024b8b6c804eb6e41661)
26ba25
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
26ba25
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
26ba25
---
26ba25
 block/io.c               | 15 +++++++++++++++
26ba25
 include/qemu/coroutine.h |  5 +++++
26ba25
 util/qemu-coroutine.c    |  5 +++++
26ba25
 3 files changed, 25 insertions(+)
26ba25
26ba25
diff --git a/block/io.c b/block/io.c
26ba25
index d404088..19db35e 100644
26ba25
--- a/block/io.c
26ba25
+++ b/block/io.c
26ba25
@@ -286,6 +286,18 @@ static void bdrv_co_drain_bh_cb(void *opaque)
26ba25
     BlockDriverState *bs = data->bs;
26ba25
 
26ba25
     if (bs) {
26ba25
+        AioContext *ctx = bdrv_get_aio_context(bs);
26ba25
+        AioContext *co_ctx = qemu_coroutine_get_aio_context(co);
26ba25
+
26ba25
+        /*
26ba25
+         * When the coroutine yielded, the lock for its home context was
26ba25
+         * released, so we need to re-acquire it here. If it explicitly
26ba25
+         * acquired a different context, the lock is still held and we don't
26ba25
+         * want to lock it a second time (or AIO_WAIT_WHILE() would hang).
26ba25
+         */
26ba25
+        if (ctx == co_ctx) {
26ba25
+            aio_context_acquire(ctx);
26ba25
+        }
26ba25
         bdrv_dec_in_flight(bs);
26ba25
         if (data->begin) {
26ba25
             bdrv_do_drained_begin(bs, data->recursive, data->parent,
26ba25
@@ -294,6 +306,9 @@ static void bdrv_co_drain_bh_cb(void *opaque)
26ba25
             bdrv_do_drained_end(bs, data->recursive, data->parent,
26ba25
                                 data->ignore_bds_parents);
26ba25
         }
26ba25
+        if (ctx == co_ctx) {
26ba25
+            aio_context_release(ctx);
26ba25
+        }
26ba25
     } else {
26ba25
         assert(data->begin);
26ba25
         bdrv_drain_all_begin();
26ba25
diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h
26ba25
index 6f8a487..9801e7f 100644
26ba25
--- a/include/qemu/coroutine.h
26ba25
+++ b/include/qemu/coroutine.h
26ba25
@@ -90,6 +90,11 @@ void qemu_aio_coroutine_enter(AioContext *ctx, Coroutine *co);
26ba25
 void coroutine_fn qemu_coroutine_yield(void);
26ba25
 
26ba25
 /**
26ba25
+ * Get the AioContext of the given coroutine
26ba25
+ */
26ba25
+AioContext *coroutine_fn qemu_coroutine_get_aio_context(Coroutine *co);
26ba25
+
26ba25
+/**
26ba25
  * Get the currently executing coroutine
26ba25
  */
26ba25
 Coroutine *coroutine_fn qemu_coroutine_self(void);
26ba25
diff --git a/util/qemu-coroutine.c b/util/qemu-coroutine.c
26ba25
index 1ba4191..2295928 100644
26ba25
--- a/util/qemu-coroutine.c
26ba25
+++ b/util/qemu-coroutine.c
26ba25
@@ -198,3 +198,8 @@ bool qemu_coroutine_entered(Coroutine *co)
26ba25
 {
26ba25
     return co->caller;
26ba25
 }
26ba25
+
26ba25
+AioContext *coroutine_fn qemu_coroutine_get_aio_context(Coroutine *co)
26ba25
+{
26ba25
+    return co->ctx;
26ba25
+}
26ba25
-- 
26ba25
1.8.3.1
26ba25