Skip to contentSkip to conversation

block: fix blk_aio_*() segfault when blk->root == NULL

PATCH Superseded Stefan Hajnoczi 3 participants 14 messages Feb 13, 2018 – Feb 15, 2018 Block layer

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover block: fix blk_aio_*() segfault when blk->root == NULL

v2:
* Introduce AIO_WAIT_WHILE() since aio_poll(ctx, true) is not allowed [Paolo]

Using bdrv_inc_in_flight(blk_bs(blk)) doesn't work since BlockBackend->root may be NULL.

This patch series solves the issue by adding an BlockBackend->in_flight counter so requests can be tracked even when there is no BlockDriverState.

This should fix the IDE and virtio-blk segfaults that have been encountered when there is no BlockDriverState.

The patch is based on work by Kevin Wolf.

Kevin Wolf (1):
block: test blk_aio_flush() with blk->root == NULL

Stefan Hajnoczi (3):
block: extract AIO_WAIT_WHILE() from BlockDriverState
block: add BlockBackend->in_flight counter Revert "IDE: Do not flush empty CDROM drives"

 tests/Makefile.include     |   2 +
 util/Makefile.objs         |   2 +-
 include/block/aio-wait.h   | 116 +++++++++++++++++++++++++++++++++++++++++++++
 include/block/block.h      |  40 +++-------------
 include/block/block_int.h  |   7 ++-
 block.c                    |   7 ++-
 block/block-backend.c      |  60 ++++++++++++++++++++---
 block/io.c                 |  10 +---
 hw/ide/core.c              |  10 +---
 tests/test-block-backend.c |  82 ++++++++++++++++++++++++++++++++
 util/aio-wait.c            |  40 ++++++++++++++++
 11 files changed, 313 insertions(+), 63 deletions(-)
 create mode 100644 include/block/aio-wait.h
 create mode 100644 tests/test-block-backend.c
 create mode 100644 util/aio-wait.c
Signature · 1 line
2.14.3
#1 lore raw

PATCH v2 1/4 block: extract AIO_WAIT_WHILE() from BlockDriverState

BlockDriverState has the BDRV_POLL_WHILE() macro to wait on event loop activity while a condition evaluates to true. This is used to implement synchronous operations where it acts as a condvar between the IOThread running the operation and the main loop waiting for the operation. It can also be called from the thread that owns the AioContext and in that case it's just a nested event loop.

BlockBackend needs this behavior but doesn't always have a BlockDriverState it can use. This patch extracts BDRV_POLL_WHILE() into the AioWait abstraction, which can be used with AioContext and isn't tied to BlockDriverState anymore.

This feature could be built directly into AioContext but then all users would kick the event loop even if they signal different conditions. Imagine an AioContext with many BlockDriverStates, each time a request completes any waiter would wake up and re-check their condition. It's nicer to keep a separate AioWait object for each condition instead.

Please see "block/aio-wait.h" for details on the API.

The name AIO_WAIT_WHILE() avoids the confusion between AIO_POLL_WHILE() and AioContext polling.

---
 util/Makefile.objs        |   2 +-
 include/block/aio-wait.h  | 116 ++++++++++++++++++++++++++++++++++++++++++++++
 include/block/block.h     |  40 +++-------------
 include/block/block_int.h |   7 ++-
 block.c                   |   5 ++
 block/io.c                |  10 +---
 util/aio-wait.c           |  40 ++++++++++++++++
 7 files changed, 174 insertions(+), 46 deletions(-)
 create mode 100644 include/block/aio-wait.h
 create mode 100644 util/aio-wait.c
util/Makefile.objs +1 -1
index 3fb611631f..ae90b9963d 100644
@@ -1,7 +1,7 @@
 util-obj-y = osdep.o cutils.o unicode.o qemu-timer-common.o
 util-obj-y += bufferiszero.o
 util-obj-y += lockcnt.o
-util-obj-y += aiocb.o async.o thread-pool.o qemu-timer.o
+util-obj-y += aiocb.o async.o aio-wait.o thread-pool.o qemu-timer.o
 util-obj-y += main-loop.o iohandler.o
 util-obj-$(CONFIG_POSIX) += aio-posix.o
 util-obj-$(CONFIG_POSIX) += compatfd.o
include/block/aio-wait.h new file +116 -0

116 lines not on this page · raw message

include/block/block.h +7 -33
index 19b3ab9cb5..c4beda33a8 100644
@@ -2,6 +2,7 @@
 #define BLOCK_H
 
 #include "block/aio.h"
+#include "block/aio-wait.h"
 #include "qapi-types.h"
 #include "qemu/iov.h"
 #include "qemu/coroutine.h"
@@ -367,41 +368,14 @@ void bdrv_drain_all_begin(void);
 void bdrv_drain_all_end(void);
 void bdrv_drain_all(void);
 
+/* Returns NULL when bs == NULL */
+AioWait *bdrv_get_aio_wait(BlockDriverState *bs);
+
 #define BDRV_POLL_WHILE(bs, cond) ({                       \
-    bool waited_ = false;                                  \
-    bool busy_ = true;                                     \
     BlockDriverState *bs_ = (bs);                          \
-    AioContext *ctx_ = bdrv_get_aio_context(bs_);          \
-    if (aio_context_in_iothread(ctx_)) {                   \
-        while ((cond) || busy_) {                          \
-            busy_ = aio_poll(ctx_, (cond));                \
-            waited_ |= !!(cond) | busy_;                   \
-        }                                                  \
-    } else {                                               \
-        assert(qemu_get_current_aio_context() ==           \
-               qemu_get_aio_context());                    \
-        /* Ask bdrv_dec_in_flight to wake up the main      \
-         * QEMU AioContext.  Extra I/O threads never take  \
-         * other I/O threads' AioContexts (see for example \
-         * block_job_defer_to_main_loop for how to do it). \
-         */                                                \
-        assert(!bs_->wakeup);                              \
-        /* Set bs->wakeup before evaluating cond.  */      \
-        atomic_mb_set(&bs_->wakeup, true);                 \
-        while (busy_) {                                    \
-            if ((cond)) {                                  \
-                waited_ = busy_ = true;                    \
-                aio_context_release(ctx_);                 \
-                aio_poll(qemu_get_aio_context(), true);    \
-                aio_context_acquire(ctx_);                 \
-            } else {                                       \
-                busy_ = aio_poll(ctx_, false);             \
-                waited_ |= busy_;                          \
-            }                                              \
-        }                                                  \
-        atomic_set(&bs_->wakeup, false);                   \
-    }                                                      \
-    waited_; })
+    AIO_WAIT_WHILE(bdrv_get_aio_wait(bs_),                 \
+                   bdrv_get_aio_context(bs_),              \
+                   cond); })
 
 int bdrv_pdiscard(BlockDriverState *bs, int64_t offset, int bytes);
 int bdrv_co_pdiscard(BlockDriverState *bs, int64_t offset, int bytes);
include/block/block_int.h +3 -4
index 5ea63f8fa8..c1f6ee961a 100644
@@ -26,6 +26,7 @@
 
 #include "block/accounting.h"
 #include "block/block.h"
+#include "block/aio-wait.h"
 #include "qemu/queue.h"
 #include "qemu/coroutine.h"
 #include "qemu/stats64.h"
@@ -709,10 +710,8 @@ struct BlockDriverState {
     unsigned int in_flight;
     unsigned int serialising_in_flight;
 
-    /* Internal to BDRV_POLL_WHILE and bdrv_wakeup.  Accessed with atomic
-     * ops.
-     */
-    bool wakeup;
+    /* Kicked to signal main loop when a request completes. */
+    AioWait wait;
 
     /* counter for nested bdrv_io_plug.
      * Accessed with atomic ops.
block.c +5 -0
index 814e5a02da..9e4da81213 100644
@@ -4716,6 +4716,11 @@ AioContext *bdrv_get_aio_context(BlockDriverState *bs)
     return bs->aio_context;
 }
 
+AioWait *bdrv_get_aio_wait(BlockDriverState *bs)
+{
+    return bs ? &bs->wait : NULL;
+}
+
 void bdrv_coroutine_enter(BlockDriverState *bs, Coroutine *co)
 {
     aio_co_enter(bdrv_get_aio_context(bs), co);
block/io.c +2 -8
index 89d0745e95..69170ee234 100644
@@ -25,6 +25,7 @@
 #include "qemu/osdep.h"
 #include "trace.h"
 #include "sysemu/block-backend.h"
+#include "block/aio-wait.h"
 #include "block/blockjob.h"
 #include "block/blockjob_int.h"
 #include "block/block_int.h"
@@ -587,16 +588,9 @@ void bdrv_inc_in_flight(BlockDriverState *bs)
     atomic_inc(&bs->in_flight);
 }
 
-static void dummy_bh_cb(void *opaque)
-{
-}
-
 void bdrv_wakeup(BlockDriverState *bs)
 {
-    /* The barrier (or an atomic op) is in the caller.  */
-    if (atomic_read(&bs->wakeup)) {
-        aio_bh_schedule_oneshot(qemu_get_aio_context(), dummy_bh_cb, NULL);
-    }
+    aio_wait_kick(bdrv_get_aio_wait(bs));
 }
 
 void bdrv_dec_in_flight(BlockDriverState *bs)
util/aio-wait.c new file +40 -0
new file mode 100644
index 0000000000..a487cdb852
@@ -0,0 +1,40 @@
+/*
+ * AioContext wait support
+ *
+ * Copyright (C) 2018 Red Hat, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/main-loop.h"
+#include "block/aio-wait.h"
+
+static void dummy_bh_cb(void *opaque)
+{
+    /* The point is to make AIO_WAIT_WHILE()'s aio_poll() return */
+}
+
+void aio_wait_kick(AioWait *wait)
+{
+    /* The barrier (or an atomic op) is in the caller.  */
+    if (atomic_read(&wait->need_kick)) {
+        aio_bh_schedule_oneshot(qemu_get_aio_context(), dummy_bh_cb, NULL);
+    }
+}
Signature · 1 line
2.14.3
#2 lore raw

Re: [Qemu-devel] [PATCH v2 1/4] block: extract AIO_WAIT_WHILE() from BlockDriverState

Quoted 18 lines · Stefan Hajnoczi

BlockDriverState has the BDRV_POLL_WHILE() macro to wait on event loop activity while a condition evaluates to true. This is used to implement synchronous operations where it acts as a condvar between the IOThread running the operation and the main loop waiting for the operation. It can also be called from the thread that owns the AioContext and in that case it's just a nested event loop.

BlockBackend needs this behavior but doesn't always have a BlockDriverState it can use. This patch extracts BDRV_POLL_WHILE() into the AioWait abstraction, which can be used with AioContext and isn't tied to BlockDriverState anymore.

This feature could be built directly into AioContext but then all users would kick the event loop even if they signal different conditions. Imagine an AioContext with many BlockDriverStates, each time a request completes any waiter would wake up and re-check their condition. It's nicer to keep a separate AioWait object for each condition instead.

Quoted 7 lines

Please see "block/aio-wait.h" for details on the API.

The name AIO_WAIT_WHILE() avoids the confusion between AIO_POLL_WHILE() and AioContext polling.

---

Trying to understand here:

include/block/aio-wait.h line 83

Quoted 10 lines
include/block/aio-wait.h new file +10 -0
+#define AIO_WAIT_WHILE(wait, ctx, cond) ({                  \
+    bool waited_ = false;                                   \
+    bool busy_ = true;                                      \
+    AioWait *wait_ = (wait);                                \
+    AioContext *ctx_ = (ctx);                               \
+    if (aio_context_in_iothread(ctx_)) {                    \
+        while ((cond) || busy_) {                           \
+            busy_ = aio_poll(ctx_, (cond));                 \
+            waited_ |= !!(cond) | busy_;                    \
+        }                                                   \

If we are in an iothread already, we never dereference wait,

include/block/aio-wait.h line 87

Quoted 4 lines
include/block/aio-wait.h new file +4 -0
+    } else {                                                \
+        assert(qemu_get_current_aio_context() ==            \
+               qemu_get_aio_context());                     \
+        assert(!wait_->need_kick);                          \

but if we are in the main loop, wait must be non-NULL.

Quoted 10 lines

+++ b/include/block/block.h

include/block/block.h +1 -0
@@ -2,6 +2,7 @@
  #define BLOCK_H
 
  #include "block/aio.h"
+#include "block/aio-wait.h"
  #include "qapi-types.h"
  #include "qemu/iov.h"
  #include "qemu/coroutine.h"

include/block/block.h void bdrv_drain_all_begin(void); · line 372

Quoted 5 lines
include/block/block.h +2 -0
@@ -367,41 +368,14 @@ void bdrv_drain_all_begin(void);
  void bdrv_drain_all_end(void);
  void bdrv_drain_all(void);
 
+/* Returns NULL when bs == NULL */
+AioWait *bdrv_get_aio_wait(BlockDriverState *bs);

This can return NULL, so it is only ever safe to use in an iothread; because if it is used in the main loop,...

include/block/block.h void bdrv_drain_all_begin(void); · line 378

Quoted 6 lines
include/block/block.h +4 -0
+
  #define BDRV_POLL_WHILE(bs, cond) ({                       \
 
+    AIO_WAIT_WHILE(bdrv_get_aio_wait(bs_),                 \
+                   bdrv_get_aio_context(bs_),              \
+                   cond); })

...we can pass NULL as the wait parameter, which will crash.

block.c AioContext *bdrv_get_aio_context(BlockDriverState *bs) · line 4722

Quoted 9 lines
block.c +5 -0
+++ b/block.c
@@ -4716,6 +4716,11 @@ AioContext *bdrv_get_aio_context(BlockDriverState *bs)
      return bs->aio_context;
  }
 
+AioWait *bdrv_get_aio_wait(BlockDriverState *bs)
+{
+    return bs ? &bs->wait : NULL;
+}

So, do we need documentation to that fact? Also,

block/io.c void bdrv_inc_in_flight(BlockDriverState *bs) · line 593

Quoted 9 lines
block/io.c +2 -4
+++ b/block/io.c
 
  void bdrv_wakeup(BlockDriverState *bs)
  {
-    /* The barrier (or an atomic op) is in the caller.  */
-    if (atomic_read(&bs->wakeup)) {
-        aio_bh_schedule_oneshot(qemu_get_aio_context(), dummy_bh_cb, NULL);
-    }
+    aio_wait_kick(bdrv_get_aio_wait(bs));

this is another case where passing NULL...

util/aio-wait.c line 37

Quoted 6 lines

+++ b/util/aio-wait.c

util/aio-wait.c new file +4 -0
+void aio_wait_kick(AioWait *wait)
+{
+    /* The barrier (or an atomic op) is in the caller.  */
+    if (atomic_read(&wait->need_kick)) {

...is bad. Does that mean bdrv_wakeup() can only be called when bs is non-NULL? Does that need documentation?

It may be that your patch is correct (as I'm not an expert on the rules in play here), but more comments may help. Or you may have a NULL dereference bug lurking. So at this point, I can't give R-b, even though the refactoring of the BDRV_POLL_WHILE() macro into a separate helper makes sense from the high level view.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#3 lore raw

Re: [Qemu-devel] [PATCH v2 1/4] block: extract AIO_WAIT_WHILE() from BlockDriverState

Quoted 14 lines · Eric Blake

Trying to understand here:

include/block/aio-wait.h line 83

Quoted 10 lines
include/block/aio-wait.h new file +10 -0
+#define AIO_WAIT_WHILE(wait, ctx, cond) ({                  \
+    bool waited_ = false;                                   \
+    bool busy_ = true;                                      \
+    AioWait *wait_ = (wait);                                \
+    AioContext *ctx_ = (ctx);                               \
+    if (aio_context_in_iothread(ctx_)) {                    \
+        while ((cond) || busy_) {                           \
+            busy_ = aio_poll(ctx_, (cond));                 \
+            waited_ |= !!(cond) | busy_;                    \
+        }                                                   \
Quoted 1 line

If we are in an iothread already, we never dereference wait,

No, the name and documentation for aio_context_in_iothread() is misleading. It actually means "does this AioContext belong to the current thread?", which is more general than just the IOThread case.

aio_context_in_iothread() returns true when:
1. We are the IOThread that owns ctx. <-- the case you thought of
2. We are the main loop and ctx == qemu_get_aio_context(). ^--- the sneaky case that BDRV_POLL_WHILE() has always relied on

Quoted 6 lines

include/block/aio-wait.h line 87

Quoted 4 lines
include/block/aio-wait.h new file +4 -0
+    } else {                                                \
+        assert(qemu_get_current_aio_context() ==            \
+               qemu_get_aio_context());                     \
+        assert(!wait_->need_kick);                          \

but if we are in the main loop, wait must be non-NULL.

The else statement only handles the case where the main loop is waiting for an IOThread AioContext.

s/in the main loop/in the main loop and ctx is an IOThread AioContext/

Quoted 18 lines
Quoted 10 lines

+++ b/include/block/block.h

include/block/block.h +1 -0
@@ -2,6 +2,7 @@
  #define BLOCK_H
  #include "block/aio.h"
+#include "block/aio-wait.h"
  #include "qapi-types.h"
  #include "qemu/iov.h"
  #include "qemu/coroutine.h"

include/block/block.h void bdrv_drain_all_begin(void); · line 371

Quoted 4 lines
include/block/block.h +2 -0
@@ -367,41 +368,14 @@ void bdrv_drain_all_begin(void);
  void bdrv_drain_all_end(void);
  void bdrv_drain_all(void);
+/* Returns NULL when bs == NULL */
+AioWait *bdrv_get_aio_wait(BlockDriverState *bs);

This can return NULL, so it is only ever safe to use in an iothread; because if it is used in the main loop,...

Quoted 8 lines

include/block/block.h void bdrv_drain_all_begin(void); · line 378

Quoted 6 lines
include/block/block.h +4 -0
+
  #define BDRV_POLL_WHILE(bs, cond) ({                       \
 
+    AIO_WAIT_WHILE(bdrv_get_aio_wait(bs_),                 \
+                   bdrv_get_aio_context(bs_),              \
+                   cond); })

...we can pass NULL as the wait parameter, which will crash.

It won't crash since if (aio_context_in_iothread(ctx_)) will take the true case when bs_ == NULL.

Quoted 11 lines

block/io.c void bdrv_inc_in_flight(BlockDriverState *bs) · line 593

Quoted 9 lines
block/io.c +2 -4
+++ b/block/io.c
 
  void bdrv_wakeup(BlockDriverState *bs)
  {
-    /* The barrier (or an atomic op) is in the caller.  */
-    if (atomic_read(&bs->wakeup)) {
-        aio_bh_schedule_oneshot(qemu_get_aio_context(), dummy_bh_cb, NULL);
-    }
+    aio_wait_kick(bdrv_get_aio_wait(bs));

this is another case where passing NULL...

bdrv_wakeup() is only called when bs != NULL.

I hope this explains things! The main issue that raised these questions was that aio_context_in_iothread() has a misleading name. Shall we rename it?

I'm having a hard time picking a new name because it must not be confused with AioContext acquire/release, which doesn't influence the "native" AioContext that the current thread has an affinity with.

#4 lore raw

Re: [Qemu-devel] [PATCH v2 1/4] block: extract AIO_WAIT_WHILE() from BlockDriverState

Quoted 17 lines · Stefan Hajnoczi
Quoted 14 lines · Eric Blake

Trying to understand here:

include/block/aio-wait.h line 83

Quoted 10 lines
include/block/aio-wait.h new file +10 -0
+#define AIO_WAIT_WHILE(wait, ctx, cond) ({                  \
+    bool waited_ = false;                                   \
+    bool busy_ = true;                                      \
+    AioWait *wait_ = (wait);                                \
+    AioContext *ctx_ = (ctx);                               \
+    if (aio_context_in_iothread(ctx_)) {                    \
+        while ((cond) || busy_) {                           \
+            busy_ = aio_poll(ctx_, (cond));                 \
+            waited_ |= !!(cond) | busy_;                    \
+        }                                                   \
Quoted 1 line

If we are in an iothread already, we never dereference wait,

Quoted 8 lines

No, the name and documentation for aio_context_in_iothread() is misleading. It actually means "does this AioContext belong to the current thread?", which is more general than just the IOThread case.

aio_context_in_iothread() returns true when:
1. We are the IOThread that owns ctx. <-- the case you thought of
2. We are the main loop and ctx == qemu_get_aio_context(). ^--- the sneaky case that BDRV_POLL_WHILE() has always relied on

Thanks, that helps.

Quoted 8 lines
Quoted 5 lines

include/block/block.h void bdrv_drain_all_begin(void); · line 378

Quoted 3 lines
include/block/block.h +3 -0
+    AIO_WAIT_WHILE(bdrv_get_aio_wait(bs_),                 \
+                   bdrv_get_aio_context(bs_),              \
+                   cond); })

...we can pass NULL as the wait parameter, which will crash.

It won't crash since if (aio_context_in_iothread(ctx_)) will take the true case when bs_ == NULL.

Okay, you've solved that one.

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

+++ b/block/io.c

   void bdrv_wakeup(BlockDriverState *bs)
   {
block/io.c +1 -4
-    /* The barrier (or an atomic op) is in the caller.  */
-    if (atomic_read(&bs->wakeup)) {
-        aio_bh_schedule_oneshot(qemu_get_aio_context(), dummy_bh_cb, NULL);
-    }
+    aio_wait_kick(bdrv_get_aio_wait(bs));
Quoted 3 lines
Quoted 1 line

this is another case where passing NULL...

bdrv_wakeup() is only called when bs != NULL.

And looks like we're safe, there, as well.

Quoted 4 lines

I hope this explains things! The main issue that raised these questions was that aio_context_in_iothread() has a misleading name. Shall we rename it?

Maybe, but that's a separate patch. What name would we bikeshed, maybe aio_context_correct_thread() (we are the correct thread if we are the iothread that owns ctx, or if we are the main thread and have properly acquired ctx) or aio_context_use_okay() (we can only use the ctx if we own it [native iothread] or have acquired it [main loop])

Quoted 5 lines

I'm having a hard time picking a new name because it must not be confused with AioContext acquire/release, which doesn't influence the "native" AioContext that the current thread has an affinity with.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#5 lore raw

Re: [Qemu-devel] [PATCH v2 1/4] block: extract AIO_WAIT_WHILE() from BlockDriverState

Quoted 10 lines · Eric Blake
Quoted 4 lines · Stefan Hajnoczi

On Tue, Feb 13, 2018 at 10:01:06AM -0600, Eric Blake wrote:
I hope this explains things! The main issue that raised these questions was that aio_context_in_iothread() has a misleading name. Shall we rename it?

Maybe, but that's a separate patch. What name would we bikeshed, maybe aio_context_correct_thread() (we are the correct thread if we are the iothread that owns ctx, or if we are the main thread and have properly acquired ctx)

Having acquired the AioContext does not make this function return true. The semantics are:
1. Current thread is the IOThread that runs the AioContext
2. Current thread is the main loop and the AioContext is the global AioContext.

The function tests whether the current thread is the "native" or "home" thread for this AioContext. Perhaps we could also call it the "poller" thread because only that thread is allowed to call aio_poll(ctx, true).

  if (aio_context_in_native_thread(ctx)) {
      ...
  } else {
      ...
  }

What do you think?

#6 lore raw

Re: [Qemu-devel] [PATCH v2 1/4] block: extract AIO_WAIT_WHILE() from BlockDriverState

Am 15.02.2018 um 10:27 hat Stefan Hajnoczi geschrieben:

Quoted 22 lines · Eric Blake
Quoted 10 lines · Stefan Hajnoczi
Quoted 4 lines · Eric Blake

I hope this explains things! The main issue that raised these questions was that aio_context_in_iothread() has a misleading name. Shall we rename it?

Maybe, but that's a separate patch. What name would we bikeshed, maybe aio_context_correct_thread() (we are the correct thread if we are the iothread that owns ctx, or if we are the main thread and have properly acquired ctx)

Having acquired the AioContext does not make this function return true. The semantics are:
1. Current thread is the IOThread that runs the AioContext
2. Current thread is the main loop and the AioContext is the global AioContext.

The function tests whether the current thread is the "native" or "home" thread for this AioContext. Perhaps we could also call it the "poller" thread because only that thread is allowed to call aio_poll(ctx, true).

Quoted 7 lines
  if (aio_context_in_native_thread(ctx)) {
      ...
  } else {
      ...
  }

What do you think?

"home" or "native" both work for me. Or if we want to keep the name short, maybe just changing the order and s/iothread/thread/ would be
enough: bool in_aio_context_thread(AioContext *ctx) - do you think that would still be prone to misunderstandings?

Kevin

#7 lore raw

Re: [Qemu-devel] [PATCH v2 1/4] block: extract AIO_WAIT_WHILE() from BlockDriverState

Quoted 16 lines · Kevin Wolf
Quoted 16 lines

Having acquired the AioContext does not make this function return true. The semantics are:
1. Current thread is the IOThread that runs the AioContext
2. Current thread is the main loop and the AioContext is the global AioContext.

The function tests whether the current thread is the "native" or "home" thread for this AioContext. Perhaps we could also call it the "poller" thread because only that thread is allowed to call aio_poll(ctx, true).

   if (aio_context_in_native_thread(ctx)) {
       ...
   } else {
       ...
   }
Quoted 6 lines
Quoted 1 line

What do you think?

"home" or "native" both work for me. Or if we want to keep the name short, maybe just changing the order and s/iothread/thread/ would be
enough: bool in_aio_context_thread(AioContext *ctx) - do you think that would still be prone to misunderstandings?

in_aio_context_home_thread() sounds slightly better than in_aio_context_thread(), but swapping the verb makes a nice difference where either name is better than what we currently have.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
Reviewed-by
#8 lore raw

Re: [Qemu-devel] [PATCH v2 1/4] block: extract AIO_WAIT_WHILE() from BlockDriverState

In reply to Eric Blake
Quoted 11 lines · Eric Blake
Quoted 7 lines · Stefan Hajnoczi

BlockDriverState has the BDRV_POLL_WHILE() macro to wait on event loop activity while a condition evaluates to true.  This is used to implement synchronous operations where it acts as a condvar between the IOThread running the operation and the main loop waiting for the operation.  It can also be called from the thread that owns the AioContext and in that case it's just a nested event loop.

It may be that your patch is correct (as I'm not an expert on the rules in play here), but more comments may help.  Or you may have a NULL dereference bug lurking.  So at this point, I can't give R-b, even though the refactoring of the BDRV_POLL_WHILE() macro into a separate helper makes sense from the high level view.

Okay, based on your responses, I can now give

although it may still help to do followups with better documentation and/or a rename of the confusing functions.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#9 lore raw

PATCH v2 2/4 block: add BlockBackend->in_flight counter

In reply to Stefan Hajnoczi

BlockBackend currently relies on BlockDriverState->in_flight to track requests for blk_drain(). There is a corner case where BlockDriverState->in_flight cannot be used though: blk->root can be NULL when there is no medium. This results in a segfault when the NULL pointer is dereferenced.

Introduce a BlockBackend->in_flight counter for aio requests so it works even when blk->root == NULL.

Based on a patch by Kevin Wolf <[email protected]>.

---
 block.c               |  2 +-
 block/block-backend.c | 60 +++++++++++++++++++++++++++++++++++++++++++++------
 2 files changed, 54 insertions(+), 8 deletions(-)
block.c +1 -1
index 9e4da81213..a83037c2a5 100644
@@ -4713,7 +4713,7 @@ out:
 
 AioContext *bdrv_get_aio_context(BlockDriverState *bs)
 {
-    return bs->aio_context;
+    return bs ? bs->aio_context : qemu_get_aio_context();
 }
 
 AioWait *bdrv_get_aio_wait(BlockDriverState *bs)
block/block-backend.c +53 -7

105 lines not on this page · raw message

Signature · 1 line
2.14.3
Reviewed-by on v2 2/4
#10 lore raw

Re: [Qemu-devel] [PATCH v2 2/4] block: add BlockBackend->in_flight counter

Quoted 9 lines · Stefan Hajnoczi

BlockBackend currently relies on BlockDriverState->in_flight to track requests for blk_drain(). There is a corner case where BlockDriverState->in_flight cannot be used though: blk->root can be NULL when there is no medium. This results in a segfault when the NULL pointer is dereferenced.

Introduce a BlockBackend->in_flight counter for aio requests so it works even when blk->root == NULL.

Quoted 9 lines

Based on a patch by Kevin Wolf <[email protected]>.

---
  block.c               |  2 +-
  block/block-backend.c | 60 +++++++++++++++++++++++++++++++++++++++++++++------
  2 files changed, 54 insertions(+), 8 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#11 lore raw

PATCH v2 3/4 block: test blk_aio_flush() with blk->root == NULL

In reply to Stefan Hajnoczi

From: Kevin Wolf <[email protected]>

This patch adds test cases for the scenario where blk_aio_flush() is called on a BlockBackend with no root. Calling drain afterwards should complete the requests with -ENOMEDIUM.

---
 tests/Makefile.include     |  2 ++
 tests/test-block-backend.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 84 insertions(+)
 create mode 100644 tests/test-block-backend.c
tests/Makefile.include +2 -0
index f41da235ae..1a457c29bc 100644
@@ -83,6 +83,7 @@ gcov-files-test-hbitmap-y = blockjob.c
 check-unit-y += tests/test-bdrv-drain$(EXESUF)
 check-unit-y += tests/test-blockjob$(EXESUF)
 check-unit-y += tests/test-blockjob-txn$(EXESUF)
+check-unit-y += tests/test-block-backend$(EXESUF)
 check-unit-y += tests/test-x86-cpuid$(EXESUF)
 # all code tested by test-x86-cpuid is inside topology.h
 gcov-files-test-x86-cpuid-y =
@@ -609,6 +610,7 @@ tests/test-throttle$(EXESUF): tests/test-throttle.o $(test-block-obj-y)
 tests/test-bdrv-drain$(EXESUF): tests/test-bdrv-drain.o $(test-block-obj-y) $(test-util-obj-y)
 tests/test-blockjob$(EXESUF): tests/test-blockjob.o $(test-block-obj-y) $(test-util-obj-y)
 tests/test-blockjob-txn$(EXESUF): tests/test-blockjob-txn.o $(test-block-obj-y) $(test-util-obj-y)
+tests/test-block-backend$(EXESUF): tests/test-block-backend.o $(test-block-obj-y) $(test-util-obj-y)
 tests/test-thread-pool$(EXESUF): tests/test-thread-pool.o $(test-block-obj-y)
 tests/test-iov$(EXESUF): tests/test-iov.o $(test-util-obj-y)
 tests/test-hbitmap$(EXESUF): tests/test-hbitmap.o $(test-util-obj-y) $(test-crypto-obj-y)
tests/test-block-backend.c new file +82 -0

82 lines not on this page · raw message

Signature · 1 line
2.14.3
#12 lore raw

PATCH v2 4/4 Revert "IDE: Do not flush empty CDROM drives"

In reply to Stefan Hajnoczi

This reverts commit 4da97120d51a4383aa96d741a2b837f8c4bbcd0b.

blk_aio_flush() now handles the blk->root == NULL case, so we no longer need this workaround.

---
 hw/ide/core.c | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)
hw/ide/core.c +1 -9
index 257b429381..139c843514 100644
@@ -1087,15 +1087,7 @@ static void ide_flush_cache(IDEState *s)
     s->status |= BUSY_STAT;
     ide_set_retry(s);
     block_acct_start(blk_get_stats(s->blk), &s->acct, 0, BLOCK_ACCT_FLUSH);
-
-    if (blk_bs(s->blk)) {
-        s->pio_aiocb = blk_aio_flush(s->blk, ide_flush_cb, s);
-    } else {
-        /* XXX blk_aio_flush() crashes when blk_bs(blk) is NULL, remove this
-         * temporary workaround when blk_aio_*() functions handle NULL blk_bs.
-         */
-        ide_flush_cb(s, 0);
-    }
+    s->pio_aiocb = blk_aio_flush(s->blk, ide_flush_cb, s);
 }
 
 static void ide_cfata_metadata_inquiry(IDEState *s)
Signature · 1 line
2.14.3
#13 lore raw
In reply to Stefan Hajnoczi
Quoted 22 lines · Stefan Hajnoczi

v2:
* Introduce AIO_WAIT_WHILE() since aio_poll(ctx, true) is not allowed [Paolo]

Using bdrv_inc_in_flight(blk_bs(blk)) doesn't work since BlockBackend->root may be NULL.

This patch series solves the issue by adding an BlockBackend->in_flight counter so requests can be tracked even when there is no BlockDriverState.

This should fix the IDE and virtio-blk segfaults that have been encountered when there is no BlockDriverState.

The patch is based on work by Kevin Wolf.

Kevin Wolf (1):
block: test blk_aio_flush() with blk->root == NULL

Stefan Hajnoczi (3):
block: extract AIO_WAIT_WHILE() from BlockDriverState
block: add BlockBackend->in_flight counter Revert "IDE: Do not flush empty CDROM drives"

Quoted 15 lines
 tests/Makefile.include     |   2 +
 util/Makefile.objs         |   2 +-
 include/block/aio-wait.h   | 116 +++++++++++++++++++++++++++++++++++++++++++++
 include/block/block.h      |  40 +++-------------
 include/block/block_int.h  |   7 ++-
 block.c                    |   7 ++-
 block/block-backend.c      |  60 ++++++++++++++++++++---
 block/io.c                 |  10 +---
 hw/ide/core.c              |  10 +---
 tests/test-block-backend.c |  82 ++++++++++++++++++++++++++++++++
 util/aio-wait.c            |  40 ++++++++++++++++
 11 files changed, 313 insertions(+), 63 deletions(-)
 create mode 100644 include/block/aio-wait.h
 create mode 100644 tests/test-block-backend.c
 create mode 100644 util/aio-wait.c

Eric has posted R-b for all patches.

Kevin or Paolo: Are you happy with this series?