22c213
From 4ef2c464a54b0b618d933641ac0a7012e629fed9 Mon Sep 17 00:00:00 2001
22c213
From: Maxim Levitsky <mlevitsk@redhat.com>
22c213
Date: Wed, 11 Mar 2020 10:51:42 +0000
22c213
Subject: [PATCH 01/20] block/nbd: Fix hang in .bdrv_close()
22c213
22c213
RH-Author: Maxim Levitsky <mlevitsk@redhat.com>
22c213
Message-id: <20200311105147.13208-2-mlevitsk@redhat.com>
22c213
Patchwork-id: 94224
22c213
O-Subject: [RHEL-AV-8.2.0 qemu-kvm PATCH v2 1/6] block/nbd: Fix hang in .bdrv_close()
22c213
Bugzilla: 1640894
22c213
RH-Acked-by: Stefano Garzarella <sgarzare@redhat.com>
22c213
RH-Acked-by: John Snow <jsnow@redhat.com>
22c213
RH-Acked-by: Max Reitz <mreitz@redhat.com>
22c213
22c213
From: Max Reitz <mreitz@redhat.com>
22c213
22c213
When nbd_close() is called from a coroutine, the connection_co never
22c213
gets to run, and thus nbd_teardown_connection() hangs.
22c213
22c213
This is because aio_co_enter() only puts the connection_co into the main
22c213
coroutine's wake-up queue, so this main coroutine needs to yield and
22c213
wait for connection_co to terminate.
22c213
22c213
Suggested-by: Kevin Wolf <kwolf@redhat.com>
22c213
Signed-off-by: Max Reitz <mreitz@redhat.com>
22c213
Message-Id: <20200122164532.178040-2-mreitz@redhat.com>
22c213
Reviewed-by: Eric Blake <eblake@redhat.com>
22c213
Reviewed-by: Maxim Levitsky <mlevitsk@redhat.com>
22c213
Signed-off-by: Max Reitz <mreitz@redhat.com>
22c213
(cherry picked from commit 78c81a3f108870d325b0a39d88711366afe6f703)
22c213
Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com>
22c213
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
22c213
---
22c213
 block/nbd.c | 14 +++++++++++++-
22c213
 1 file changed, 13 insertions(+), 1 deletion(-)
22c213
22c213
diff --git a/block/nbd.c b/block/nbd.c
22c213
index 5f18f78..a73f0d9 100644
22c213
--- a/block/nbd.c
22c213
+++ b/block/nbd.c
22c213
@@ -70,6 +70,7 @@ typedef struct BDRVNBDState {
22c213
     CoMutex send_mutex;
22c213
     CoQueue free_sema;
22c213
     Coroutine *connection_co;
22c213
+    Coroutine *teardown_co;
22c213
     QemuCoSleepState *connection_co_sleep_ns_state;
22c213
     bool drained;
22c213
     bool wait_drained_end;
22c213
@@ -203,7 +204,15 @@ static void nbd_teardown_connection(BlockDriverState *bs)
22c213
             qemu_co_sleep_wake(s->connection_co_sleep_ns_state);
22c213
         }
22c213
     }
22c213
-    BDRV_POLL_WHILE(bs, s->connection_co);
22c213
+    if (qemu_in_coroutine()) {
22c213
+        s->teardown_co = qemu_coroutine_self();
22c213
+        /* connection_co resumes us when it terminates */
22c213
+        qemu_coroutine_yield();
22c213
+        s->teardown_co = NULL;
22c213
+    } else {
22c213
+        BDRV_POLL_WHILE(bs, s->connection_co);
22c213
+    }
22c213
+    assert(!s->connection_co);
22c213
 }
22c213
 
22c213
 static bool nbd_client_connecting(BDRVNBDState *s)
22c213
@@ -395,6 +404,9 @@ static coroutine_fn void nbd_connection_entry(void *opaque)
22c213
         s->ioc = NULL;
22c213
     }
22c213
 
22c213
+    if (s->teardown_co) {
22c213
+        aio_co_wake(s->teardown_co);
22c213
+    }
22c213
     aio_wait_kick();
22c213
 }
22c213
 
22c213
-- 
22c213
1.8.3.1
22c213