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

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