ae23c9
From f4414f5600168109da86e321f1651f71605cf9aa Mon Sep 17 00:00:00 2001
ae23c9
From: Kevin Wolf <kwolf@redhat.com>
ae23c9
Date: Wed, 10 Oct 2018 20:21:45 +0100
ae23c9
Subject: [PATCH 19/49] block: Allow AIO_WAIT_WHILE with NULL ctx
ae23c9
ae23c9
RH-Author: Kevin Wolf <kwolf@redhat.com>
ae23c9
Message-id: <20181010202213.7372-7-kwolf@redhat.com>
ae23c9
Patchwork-id: 82596
ae23c9
O-Subject: [RHEL-8 qemu-kvm PATCH 16/44] block: Allow AIO_WAIT_WHILE with NULL ctx
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_drain_all() wants to have a single polling loop for draining the
ae23c9
in-flight requests of all nodes. This means that the AIO_WAIT_WHILE()
ae23c9
condition relies on activity in multiple AioContexts, which is polled
ae23c9
from the mainloop context. We must therefore call AIO_WAIT_WHILE() from
ae23c9
the mainloop thread and use the AioWait notification mechanism.
ae23c9
ae23c9
Just randomly picking the AioContext of any non-mainloop thread would
ae23c9
work, but instead of bothering to find such a context in the caller, we
ae23c9
can just as well accept NULL for ctx.
ae23c9
ae23c9
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
ae23c9
(cherry picked from commit 4d22bbf4ef72583eefdf44db6bf9fc7683fbc4c2)
ae23c9
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
ae23c9
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
ae23c9
---
ae23c9
 include/block/aio-wait.h | 13 +++++++++----
ae23c9
 1 file changed, 9 insertions(+), 4 deletions(-)
ae23c9
ae23c9
diff --git a/include/block/aio-wait.h b/include/block/aio-wait.h
ae23c9
index 783d367..c85a62f 100644
ae23c9
--- a/include/block/aio-wait.h
ae23c9
+++ b/include/block/aio-wait.h
ae23c9
@@ -57,7 +57,8 @@ typedef struct {
ae23c9
 /**
ae23c9
  * AIO_WAIT_WHILE:
ae23c9
  * @wait: the aio wait object
ae23c9
- * @ctx: the aio context
ae23c9
+ * @ctx: the aio context, or NULL if multiple aio contexts (for which the
ae23c9
+ *       caller does not hold a lock) are involved in the polling condition.
ae23c9
  * @cond: wait while this conditional expression is true
ae23c9
  *
ae23c9
  * Wait while a condition is true.  Use this to implement synchronous
ae23c9
@@ -75,7 +76,7 @@ typedef struct {
ae23c9
     bool waited_ = false;                                          \
ae23c9
     AioWait *wait_ = (wait);                                       \
ae23c9
     AioContext *ctx_ = (ctx);                                      \
ae23c9
-    if (in_aio_context_home_thread(ctx_)) {                        \
ae23c9
+    if (ctx_ && in_aio_context_home_thread(ctx_)) {                \
ae23c9
         while ((cond)) {                                           \
ae23c9
             aio_poll(ctx_, true);                                  \
ae23c9
             waited_ = true;                                        \
ae23c9
@@ -86,9 +87,13 @@ typedef struct {
ae23c9
         /* Increment wait_->num_waiters before evaluating cond. */ \
ae23c9
         atomic_inc(&wait_->num_waiters);                           \
ae23c9
         while ((cond)) {                                           \
ae23c9
-            aio_context_release(ctx_);                             \
ae23c9
+            if (ctx_) {                                            \
ae23c9
+                aio_context_release(ctx_);                         \
ae23c9
+            }                                                      \
ae23c9
             aio_poll(qemu_get_aio_context(), true);                \
ae23c9
-            aio_context_acquire(ctx_);                             \
ae23c9
+            if (ctx_) {                                            \
ae23c9
+                aio_context_acquire(ctx_);                         \
ae23c9
+            }                                                      \
ae23c9
             waited_ = true;                                        \
ae23c9
         }                                                          \
ae23c9
         atomic_dec(&wait_->num_waiters);                           \
ae23c9
-- 
ae23c9
1.8.3.1
ae23c9