Skip to contentSkip to conversation

block: make BDRV_POLL_WHILE() re-entrancy safe

PATCH Superseded Stefan Hajnoczi 3 participants 4 messages Mar 6, 2018 – Mar 7, 2018

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 block: make BDRV_POLL_WHILE() re-entrancy safe

Nested BDRV_POLL_WHILE() calls can occur. Currently assert(!bs_->wakeup) will fail when this happens.

This patch converts bs->wakeup from bool to a counter.

Nesting works correctly because outer BDRV_POLL_WHILE() callers evaluate the condition again after the inner caller completes (invoking the inner caller counts as aio_poll() progress).

---
Fu Weiwei: Please retest and let us know if this fixes the assertion failure you hit. Thanks!

---
 include/block/block.h     | 7 +++----
 include/block/block_int.h | 2 +-
 2 files changed, 4 insertions(+), 5 deletions(-)
include/block/block.h +3 -4
index fac401ba3e..990b97f0ad 100644
@@ -385,9 +385,8 @@ void bdrv_drain_all(void);
          * 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);                 \
+        /* Increment bs->wakeup before evaluating cond. */ \
+        atomic_inc(&bs_->wakeup);                          \
         while (busy_) {                                    \
             if ((cond)) {                                  \
                 waited_ = busy_ = true;                    \
@@ -399,7 +398,7 @@ void bdrv_drain_all(void);
                 waited_ |= busy_;                          \
             }                                              \
         }                                                  \
-        atomic_set(&bs_->wakeup, false);                   \
+        atomic_dec(&bs_->wakeup);                          \
     }                                                      \
     waited_; })
 
include/block/block_int.h +1 -1
index 5ea63f8fa8..0f360c0ed5 100644
@@ -712,7 +712,7 @@ struct BlockDriverState {
     /* Internal to BDRV_POLL_WHILE and bdrv_wakeup.  Accessed with atomic
      * ops.
      */
-    bool wakeup;
+    unsigned wakeup;
 
     /* counter for nested bdrv_io_plug.
      * Accessed with atomic ops.
Signature · 1 line
2.14.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 51 lines · Stefan Hajnoczi

Nested BDRV_POLL_WHILE() calls can occur. Currently assert(!bs_->wakeup) will fail when this happens.

This patch converts bs->wakeup from bool to a counter.

Nesting works correctly because outer BDRV_POLL_WHILE() callers evaluate the condition again after the inner caller completes (invoking the inner caller counts as aio_poll() progress).

---
Fu Weiwei: Please retest and let us know if this fixes the assertion failure you hit. Thanks!

---
 include/block/block.h     | 7 +++----
 include/block/block_int.h | 2 +-
 2 files changed, 4 insertions(+), 5 deletions(-)
include/block/block.h +3 -4
index fac401ba3e..990b97f0ad 100644
@@ -385,9 +385,8 @@ void bdrv_drain_all(void);
          * 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);                 \
+        /* Increment bs->wakeup before evaluating cond. */ \
+        atomic_inc(&bs_->wakeup);                          \
         while (busy_) {                                    \
             if ((cond)) {                                  \
                 waited_ = busy_ = true;                    \
@@ -399,7 +398,7 @@ void bdrv_drain_all(void);
                 waited_ |= busy_;                          \
             }                                              \
         }                                                  \
-        atomic_set(&bs_->wakeup, false);                   \
+        atomic_dec(&bs_->wakeup);                          \
     }                                                      \
     waited_; })
 
include/block/block_int.h +0 -0
index 5ea63f8fa8..0f360c0ed5 100644

include/block/block_int.h struct BlockDriverState { · line 718

Quoted 8 lines
include/block/block_int.h +1 -1
@@ -712,7 +712,7 @@ struct BlockDriverState {
     /* Internal to BDRV_POLL_WHILE and bdrv_wakeup.  Accessed with atomic
      * ops.
      */
-    bool wakeup;
+    unsigned wakeup;
 
     /* counter for nested bdrv_io_plug.
      * Accessed with atomic ops.

At least, the assertion made it fail fast. :)

Paolo

#2 lore raw
In reply to Stefan Hajnoczi

Am 06.03.2018 um 11:53 hat Stefan Hajnoczi geschrieben:

Quoted 5 lines

Nested BDRV_POLL_WHILE() calls can occur. Currently assert(!bs_->wakeup) will fail when this happens.

This patch converts bs->wakeup from bool to a counter.

Quoted 7 lines

Nesting works correctly because outer BDRV_POLL_WHILE() callers evaluate the condition again after the inner caller completes (invoking the inner caller counts as aio_poll() progress).

Doesn't this conflict with your own AIO_WAIT_WHILE() patch?

Kevin

#3 lore raw
Quoted 10 lines · Kevin Wolf

Am 06.03.2018 um 11:53 hat Stefan Hajnoczi geschrieben:

Quoted 9 lines

Nested BDRV_POLL_WHILE() calls can occur. Currently assert(!bs_->wakeup) will fail when this happens.

This patch converts bs->wakeup from bool to a counter.

Nesting works correctly because outer BDRV_POLL_WHILE() callers evaluate the condition again after the inner caller completes (invoking the inner caller counts as aio_poll() progress).

Quoted 5 lines
Quoted 3 lines

Doesn't this conflict with your own AIO_WAIT_WHILE() patch?

Yes, I wanted this patch to be easy for Weiwei to test without dependencies.

AIO_WAIT_WHILE() has just hit qemu.git/master, so I'll rebase and send a v2.

Stefan