Skip to contentSkip to conversation

block-backend: Fix race when resuming queued requests

PATCH In review Kevin Wolf 4 participants 4 messages Nov 19, 2025 – Nov 24, 2025 Block layer

lore
Share & tools
lore

Kevin Wolf

author
#0 lore raw

PATCH 1/1 block-backend: Fix race when resuming queued requests

When new requests arrive at a BlockBackend that is currently drained, these requests are queued until the drain section ends.

There is a race window between blk_root_drained_end() waking up a queued request in an iothread from the main thread and blk_wait_while_drained() actually being woken up in the iothread and calling blk_in_flight(). If the BlockBackend is drained again during this window, drain won't wait for this request and it will sneak in when the BlockBackend is already supposed to be quiesced. This causes assertion failures in bdrv_drain_all_begin() and can have other unintended consequences.

Fix this by increasing the in_flight counter immediately when scheduling the request to be resumed so that the next drain will wait for it to complete.

---
 block/block-backend.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
block/block-backend.c +5 -3
index f8d6ba65c1..d6df369188 100644
@@ -1318,9 +1318,9 @@ static void coroutine_fn blk_wait_while_drained(BlockBackend *blk)
          * section.
          */
         qemu_mutex_lock(&blk->queued_requests_lock);
+        /* blk_root_drained_end() has the corresponding blk_inc_in_flight() */
         blk_dec_in_flight(blk);
         qemu_co_queue_wait(&blk->queued_requests, &blk->queued_requests_lock);
-        blk_inc_in_flight(blk);
         qemu_mutex_unlock(&blk->queued_requests_lock);
     }
 }
@@ -2767,9 +2767,11 @@ static void blk_root_drained_end(BdrvChild *child)
             blk->dev_ops->drained_end(blk->dev_opaque);
         }
         qemu_mutex_lock(&blk->queued_requests_lock);
-        while (qemu_co_enter_next(&blk->queued_requests,
-                                  &blk->queued_requests_lock)) {
+        while (!qemu_co_queue_empty(&blk->queued_requests)) {
             /* Resume all queued requests */
+            blk_inc_in_flight(blk);
+            qemu_co_enter_next(&blk->queued_requests,
+                               &blk->queued_requests_lock);
         }
         qemu_mutex_unlock(&blk->queued_requests_lock);
     }
Signature · 1 line
2.51.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 15 lines · Kevin Wolf

When new requests arrive at a BlockBackend that is currently drained, these requests are queued until the drain section ends.

There is a race window between blk_root_drained_end() waking up a queued request in an iothread from the main thread and blk_wait_while_drained() actually being woken up in the iothread and calling blk_in_flight(). If the BlockBackend is drained again during this window, drain won't wait for this request and it will sneak in when the BlockBackend is already supposed to be quiesced. This causes assertion failures in bdrv_drain_all_begin() and can have other unintended consequences.

Fix this by increasing the in_flight counter immediately when scheduling the request to be resumed so that the next drain will wait for it to complete.

Quoted 6 lines
---
  block/block-backend.c | 8 +++++---
  1 file changed, 5 insertions(+), 3 deletions(-)
Tested-by on v1 1/1
#2 lore raw
In reply to Kevin Wolf
Quoted 15 lines · Kevin Wolf

When new requests arrive at a BlockBackend that is currently drained, these requests are queued until the drain section ends.

There is a race window between blk_root_drained_end() waking up a queued request in an iothread from the main thread and blk_wait_while_drained() actually being woken up in the iothread and calling blk_in_flight(). If the BlockBackend is drained again during this window, drain won't wait for this request and it will sneak in when the BlockBackend is already supposed to be quiesced. This causes assertion failures in bdrv_drain_all_begin() and can have other unintended consequences.

Fix this by increasing the in_flight counter immediately when scheduling the request to be resumed so that the next drain will wait for it to complete.

Quoted 6 lines
---
 block/block-backend.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

I can confirm that the crash is no longer reproducible with this fix applied. Thanks for looking into this!

Reviewed-by on v1 1/1
#3 lore raw
In reply to Kevin Wolf

Am 19.11.25 um 6:27 PM schrieb Kevin Wolf:

Quoted 6 lines

When new requests arrive at a BlockBackend that is currently drained, these requests are queued until the drain section ends.

There is a race window between blk_root_drained_end() waking up a queued request in an iothread from the main thread and blk_wait_while_drained() actually being woken up in the iothread and calling blk_in_flight(). If

Small typo here: blk_inc_in_flight()

Quoted 5 lines

the BlockBackend is drained again during this window, drain won't wait for this request and it will sneak in when the BlockBackend is already supposed to be quiesced. This causes assertion failures in bdrv_drain_all_begin() and can have other unintended consequences.

Quoted 7 lines

Fix this by increasing the in_flight counter immediately when scheduling the request to be resumed so that the next drain will wait for it to complete.