Skip to contentSkip to conversation

block: fix the size change notification in bdrv_replace_child_bs()

PATCH Applied Denis V. Lunev 2 participants 5 messages Nov 5 – Nov 17 Block layer

lore
Share & tools
lore
#0 lore raw

cover block: fix the size change notification in bdrv_replace_child_bs()

These address Kevin's review of the series which went in through Eric's block pull as 8b267376df87..28fafb3147f5:
https://lore.kernel.org/qemu-devel/[email protected]/

Tested with a local qtest, not included: with a second scsi-hd sharing the new node, master reports CAPACITY DATA HAS CHANGED to that device after qom-set on the first one, and with patch 3 it does not.

Cc Kevin Wolf <[email protected]>
Cc Hanna Reitz <[email protected]>
Cc Eric Blake <[email protected]>
Cc Philippe Mathieu-Daudé <[email protected]>

Denis V. Lunev (3):
block: read the old child length after draining in
bdrv_replace_child_bs()
block: add bdrv_child_notify_resize() to notify a single parent
block: notify only the replaced child in bdrv_replace_child_bs()

 block.c                      |  7 ++++---
 block/io.c                   | 14 +++++++++++---
 include/block/block_int-io.h |  8 +++++++-
 3 files changed, 22 insertions(+), 7 deletions(-)

base-commit: f71afb409a87c4ecd0f9d63a5963eb645dfab780

Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/3 block: read the old child length after draining in bdrv_replace_child_bs()

From: Denis V. Lunev <[email protected]>

bdrv_drained_begin() runs nested event loops, and old_bs can be resized from there by something other than the BlockBackend being switched: an export truncating it, or a block job completing. old_len read before the drain is then stale, and comparing it with new_len sends a notification the parent does not need or skips one it does.

Read it once both nodes are drained, so that the read, the comparison and the notification see the same sizes. A resize which completes in the drain notifies the parents of old_bs, @child included, on its own.

Fixes 8b267376df87 ("block: notify the parent when a replaced child has a different size")
Cc Kevin Wolf <[email protected]>
Cc Hanna Reitz <[email protected]>
Cc Eric Blake <[email protected]>
Cc Philippe Mathieu-Daudé <[email protected]>
---
 block.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
block.c +3 -2
index 006c341340..9df1a48c06 100644
@@ -5566,11 +5566,12 @@ int bdrv_replace_child_bs(BdrvChild *child, BlockDriverState *new_bs,
 
     GLOBAL_STATE_CODE();
 
-    old_len = bdrv_getlength(old_bs);
-
     bdrv_ref(old_bs);
     bdrv_drained_begin(old_bs);
     bdrv_drained_begin(new_bs);
+
+    old_len = bdrv_getlength(old_bs);
+
     bdrv_graph_wrlock();
 
     bdrv_replace_child_tran(child, new_bs, tran);
Signature · 1 line
2.53.0
#2 lore raw

PATCH 2/3 block: add bdrv_child_notify_resize() to notify a single parent

In reply to Denis V. Lunev

From: Denis V. Lunev <[email protected]>

bdrv_co_parent_cb_resize() calls the resize hook of every parent of a node. bdrv_replace_child_bs() needs to call it for one BdrvChild, and from outside a coroutine.

Move the call into bdrv_co_child_notify_resize(), make the loop use it, and add a co_wrapper for callers outside a coroutine.

No functional change.

Cc Kevin Wolf <[email protected]>
Cc Hanna Reitz <[email protected]>
Cc Eric Blake <[email protected]>
Cc Philippe Mathieu-Daudé <[email protected]>
---
 block/io.c                   | 14 +++++++++++---
 include/block/block_int-io.h |  9 +++++++++
 2 files changed, 20 insertions(+), 3 deletions(-)
block/io.c +11 -3
index 06b1fc13b9..ea77f34204 100644
@@ -3634,6 +3634,16 @@ int coroutine_fn bdrv_co_copy_range(BdrvChild *src, int64_t src_offset,
                                    bytes, read_flags, write_flags);
 }
 
+void coroutine_fn bdrv_co_child_notify_resize(BdrvChild *c)
+{
+    IO_CODE();
+    assert_bdrv_graph_readable();
+
+    if (c->klass->resize) {
+        c->klass->resize(c);
+    }
+}
+
 void coroutine_fn bdrv_co_parent_cb_resize(BlockDriverState *bs)
 {
     BdrvChild *c;
@@ -3642,9 +3652,7 @@ void coroutine_fn bdrv_co_parent_cb_resize(BlockDriverState *bs)
     assert_bdrv_graph_readable();
 
     QLIST_FOREACH(c, &bs->parents, next_parent) {
-        if (c->klass->resize) {
-            c->klass->resize(c);
-        }
+        bdrv_co_child_notify_resize(c);
     }
 }
 
include/block/block_int-io.h +9 -0
index 95d0dd7a05..3cb72fecd4 100644
@@ -197,6 +197,15 @@ void bdrv_bsc_fill(BlockDriverState *bs, int64_t offset, int64_t bytes);
 void coroutine_fn GRAPH_RDLOCK
 bdrv_co_parent_cb_resize(BlockDriverState *bs);
 
+/*
+ * Notify the parent behind @c that the size of its child changed.
+ */
+void coroutine_fn GRAPH_RDLOCK
+bdrv_co_child_notify_resize(BdrvChild *c);
+
+void co_wrapper_bdrv_rdlock
+bdrv_child_notify_resize(BdrvChild *c);
+
 void co_wrapper_bdrv_rdlock
 bdrv_parent_cb_resize(BlockDriverState *bs);
 
Signature · 1 line
2.53.0
#3 lore raw

PATCH 3/3 block: notify only the replaced child in bdrv_replace_child_bs()

In reply to Denis V. Lunev

From: Denis V. Lunev <[email protected]>

bdrv_replace_child_bs() compares the sizes of the old and the new node to decide whether @child has to be told, and then notifies every parent of new_bs. The other parents already had that node and its size, so they get a resize event for nothing: a scsi-hd sharing the new node reports CAPACITY DATA HAS CHANGED to its guest although the capacity is the same.

Notify @child alone. bdrv_parent_cb_resize() has no user left, so drop it.

Fixes 8b267376df87 ("block: notify the parent when a replaced child has a different size")
Cc Kevin Wolf <[email protected]>
Cc Hanna Reitz <[email protected]>
Cc Eric Blake <[email protected]>
Cc Philippe Mathieu-Daudé <[email protected]>
---
 block.c                      | 2 +-
 include/block/block_int-io.h | 3 ---
 2 files changed, 1 insertion(+), 4 deletions(-)
block.c +1 -1
index 9df1a48c06..c3e7059556 100644
@@ -5590,7 +5590,7 @@ int bdrv_replace_child_bs(BdrvChild *child, BlockDriverState *new_bs,
         int64_t new_len = bdrv_getlength(new_bs);
 
         if (new_len >= 0 && new_len != old_len) {
-            bdrv_parent_cb_resize(new_bs);
+            bdrv_child_notify_resize(child);
         }
     }
 
include/block/block_int-io.h +0 -3
index 3cb72fecd4..6aa7b2184e 100644
@@ -206,7 +206,4 @@ bdrv_co_child_notify_resize(BdrvChild *c);
 void co_wrapper_bdrv_rdlock
 bdrv_child_notify_resize(BdrvChild *c);
 
-void co_wrapper_bdrv_rdlock
-bdrv_parent_cb_resize(BlockDriverState *bs);
-
 #endif /* BLOCK_INT_IO_H */
Signature · 1 line
2.53.0
Applied
#4 lore raw
In reply to Denis V. Lunev

Am 05.11.2026 um 21:37 hat Denis V. Lunev geschrieben:

Quoted 8 lines

These address Kevin's review of the series which went in through Eric's block pull as 8b267376df87..28fafb3147f5:
https://lore.kernel.org/qemu-devel/[email protected]/

Tested with a local qtest, not included: with a second scsi-hd sharing the new node, master reports CAPACITY DATA HAS CHANGED to that device after qom-set on the first one, and with patch 3 it does not.

Quoted 5 lines
Cc Kevin Wolf <[email protected]>
Cc Hanna Reitz <[email protected]>
Cc Eric Blake <[email protected]>
Cc Philippe Mathieu-Daudé <[email protected]>

Thanks, applied to the block branch.

Kevin