Skip to contentSkip to conversation

block: update inserted/removed nodes from BlockRAMRegistrar

PATCH Superseded Stefan Hajnoczi 2 participants 7 messages Sep 30, 2025 – Oct 1, 2025 Block layer

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover block: update inserted/removed nodes from BlockRAMRegistrar

This patch series fixes a bug in BlockRAMRegistrar: it currently doesn't react to block graph changes and newly inserted nodes lack RAMBlock information needed to map I/O buffers. This is important for vdpa-blk devices because they rely on the ability to map I/O buffers.

Stefan Hajnoczi (4):
block: add BdrvChildClass->propagate_attach/detach() callbacks
block: add blk_add_attach/detach_notifier() APIs
block: rename RAMBlockRegistrar->notifier field
block: update inserted/removed nodes from BlockRAMRegistrar

 include/block/block_int-common.h            |  11 ++
 include/system/block-backend-global-state.h |   9 ++
 include/system/block-ram-registrar.h        |   4 +-
 block.c                                     |  56 +++++--
 block/block-backend.c                       |  44 ++++++
 block/block-ram-registrar.c                 |  75 ++++++++-
 tests/unit/test-block-backend.c             | 164 ++++++++++++++++++++
 7 files changed, 344 insertions(+), 19 deletions(-)
Signature · 1 line
2.51.0
#1 lore raw

PATCH 1/4 block: add BdrvChildClass->propagate_attach/detach() callbacks

bdrv_replace_child_noperm() is the heart of block graph changes. It is called when a node is inserted, removed, or replaced. These changes happen without notifying parents in the graph, so it is currently not possible to monitor changes.

Add BdrvChildClass callbacks to propagate attach/detach operations to the roots of the graph. The next commit will introduce a BlockBackend API for monitoring changes using this mechanism.

---
 include/block/block_int-common.h | 11 +++++++
 block.c                          | 56 +++++++++++++++++++++++++-------
 2 files changed, 56 insertions(+), 11 deletions(-)
include/block/block_int-common.h +11 -0
index 034c0634c8..0368a3191c 100644
@@ -963,6 +963,17 @@ struct BdrvChildClass {
     void GRAPH_RDLOCK_PTR (*activate)(BdrvChild *child, Error **errp);
     int GRAPH_RDLOCK_PTR (*inactivate)(BdrvChild *child);
 
+    /*
+     * Optional callbacks when a descendant (child, grandchild, etc) attaches
+     * or detaches a BlockDriverState. Allows monitoring changes to the graph.
+     *
+     * Called after ->attach() and before ->detach().
+     */
+    void GRAPH_WRLOCK_PTR (*propagate_attach)(BdrvChild *self,
+                                              BdrvChild *descendant);
+    void GRAPH_WRLOCK_PTR (*propagate_detach)(BdrvChild *self,
+                                              BdrvChild *descendant);
+
     void GRAPH_WRLOCK_PTR (*attach)(BdrvChild *child);
     void GRAPH_WRLOCK_PTR (*detach)(BdrvChild *child);
 
block.c +45 -11

80 lines not on this page · raw message

Signature · 1 line
2.51.0
#2 lore raw

PATCH 2/4 block: add blk_add_attach/detach_notifier() APIs

In reply to Stefan Hajnoczi

Add an API to receive a callback when a BlockDriverState is attached or detached from the block graph. This allows callers to track insert, remove, and replace operations.

Note that this API might be able to replace the blk_add_remove_bs_notifier() API if the callback checks that the root BDS is being removed and ignores all other calls. I think this is a bit messy though, so I kept blk_add_remove_bs_notifier().

---
 include/system/block-backend-global-state.h |   9 ++
 block/block-backend.c                       |  44 ++++++
 tests/unit/test-block-backend.c             | 164 ++++++++++++++++++++
 3 files changed, 217 insertions(+)
include/system/block-backend-global-state.h +9 -0
index c3849640df..e828f271ee 100644
@@ -97,6 +97,15 @@ void blk_remove_aio_context_notifier(BlockBackend *blk,
                                      void (*detach_aio_context)(void *),
                                      void *opaque);
 void blk_add_remove_bs_notifier(BlockBackend *blk, Notifier *notify);
+
+typedef struct {
+    BlockBackend *blk;
+    BlockDriverState *bs;
+} BlockBackendAttachDetachArgs;
+
+void blk_add_attach_notifier(BlockBackend *blk, Notifier *notify);
+void blk_add_detach_notifier(BlockBackend *blk, Notifier *notify);
+
 BlockBackendRootState *blk_get_root_state(BlockBackend *blk);
 void blk_update_root_state(BlockBackend *blk);
 bool blk_get_detect_zeroes_from_root_state(BlockBackend *blk);
block/block-backend.c +44 -0

80 lines not on this page · raw message

tests/unit/test-block-backend.c +164 -0

182 lines not on this page · raw message

Signature · 1 line
2.51.0
#3 lore raw

PATCH 3/4 block: rename RAMBlockRegistrar->notifier field

In reply to Stefan Hajnoczi

The 'notifier' field name will be confusing when the BlockBackend attach and detach notifiers are added in the next commit. Rename the field so it's clear that this is the RAMBlock notifier.

---
 include/system/block-ram-registrar.h |  2 +-
 block/block-ram-registrar.c          | 14 ++++++++------
 2 files changed, 9 insertions(+), 7 deletions(-)
include/system/block-ram-registrar.h +1 -1
index d8b2f7942b..76c157bd54 100644
@@ -21,7 +21,7 @@
  */
 typedef struct {
     BlockBackend *blk;
-    RAMBlockNotifier notifier;
+    RAMBlockNotifier ram_block_notifier;
     bool ok;
 } BlockRAMRegistrar;
 
block/block-ram-registrar.c +8 -6
index fcda2b86af..d5b84667a1 100644
@@ -12,7 +12,8 @@
 static void ram_block_added(RAMBlockNotifier *n, void *host, size_t size,
                             size_t max_size)
 {
-    BlockRAMRegistrar *r = container_of(n, BlockRAMRegistrar, notifier);
+    BlockRAMRegistrar *r =
+        container_of(n, BlockRAMRegistrar, ram_block_notifier);
     Error *err = NULL;
 
     if (!r->ok) {
@@ -21,7 +22,7 @@ static void ram_block_added(RAMBlockNotifier *n, void *host, size_t size,
 
     if (!blk_register_buf(r->blk, host, max_size, &err)) {
         error_report_err(err);
-        ram_block_notifier_remove(&r->notifier);
+        ram_block_notifier_remove(n);
         r->ok = false;
     }
 }
@@ -29,14 +30,15 @@ static void ram_block_added(RAMBlockNotifier *n, void *host, size_t size,
 static void ram_block_removed(RAMBlockNotifier *n, void *host, size_t size,
                               size_t max_size)
 {
-    BlockRAMRegistrar *r = container_of(n, BlockRAMRegistrar, notifier);
+    BlockRAMRegistrar *r =
+        container_of(n, BlockRAMRegistrar, ram_block_notifier);
     blk_unregister_buf(r->blk, host, max_size);
 }
 
 void blk_ram_registrar_init(BlockRAMRegistrar *r, BlockBackend *blk)
 {
     r->blk = blk;
-    r->notifier = (RAMBlockNotifier){
+    r->ram_block_notifier = (RAMBlockNotifier){
         .ram_block_added = ram_block_added,
         .ram_block_removed = ram_block_removed,
 
@@ -47,12 +49,12 @@ void blk_ram_registrar_init(BlockRAMRegistrar *r, BlockBackend *blk)
     };
     r->ok = true;
 
-    ram_block_notifier_add(&r->notifier);
+    ram_block_notifier_add(&r->ram_block_notifier);
 }
 
 void blk_ram_registrar_destroy(BlockRAMRegistrar *r)
 {
     if (r->ok) {
-        ram_block_notifier_remove(&r->notifier);
+        ram_block_notifier_remove(&r->ram_block_notifier);
     }
 }
Signature · 1 line
2.51.0
#4 lore raw

PATCH 4/4 block: update inserted/removed nodes from BlockRAMRegistrar

In reply to Stefan Hajnoczi

BlockRAMRegistrar ensures that RAMBlocks are registered with BlockDriverStates. This is essential for vdpa-blk because they need to know the memory mappings of I/O buffers. However, BlockRAMRegistrar is currently unaware of changes to the block graph and newly inserted nodes have no RAMBlocks registered.

Use the new blk_add_attach_notifier() and blk_add_detach_notifier() APIs to bring nodes up to speed when the graph changes. This fixes vdpa-blk across mirror and other operations that modify the block graph. Previously I/O would not succeed after a new node was inserted due to missing memory mappings.

---
 include/system/block-ram-registrar.h |  2 +
 block/block-ram-registrar.c          | 63 +++++++++++++++++++++++++++-
 2 files changed, 63 insertions(+), 2 deletions(-)
include/system/block-ram-registrar.h +2 -0
index 76c157bd54..292c197d1c 100644
@@ -22,6 +22,8 @@
 typedef struct {
     BlockBackend *blk;
     RAMBlockNotifier ram_block_notifier;
+    Notifier blk_attach_notifier;
+    Notifier blk_detach_notifier;
     bool ok;
 } BlockRAMRegistrar;
 
block/block-ram-registrar.c +61 -2

96 lines not on this page · raw message

Signature · 1 line
2.51.0
Reviewed-by on the v1 series
#5 lore raw
In reply to Stefan Hajnoczi
Quoted 10 lines · Stefan Hajnoczi

This patch series fixes a bug in BlockRAMRegistrar: it currently doesn't react to block graph changes and newly inserted nodes lack RAMBlock information needed to map I/O buffers. This is important for vdpa-blk devices because they rely on the ability to map I/O buffers.

Stefan Hajnoczi (4):
block: add BdrvChildClass->propagate_attach/detach() callbacks
block: add blk_add_attach/detach_notifier() APIs
block: rename RAMBlockRegistrar->notifier field
block: update inserted/removed nodes from BlockRAMRegistrar

Makes sense to me. For the series,

Quoted 14 lines
 include/block/block_int-common.h            |  11 ++
 include/system/block-backend-global-state.h |   9 ++
 include/system/block-ram-registrar.h        |   4 +-
 block.c                                     |  56 +++++--
 block/block-backend.c                       |  44 ++++++
 block/block-ram-registrar.c                 |  75 ++++++++-
 tests/unit/test-block-backend.c             | 164 ++++++++++++++++++++
 7 files changed, 344 insertions(+), 19 deletions(-)
Signature · 1 line
2.51.0
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org
#6 lore raw
In reply to Stefan Hajnoczi
Quoted 11 lines · Stefan Hajnoczi

This patch series fixes a bug in BlockRAMRegistrar: it currently doesn't react to block graph changes and newly inserted nodes lack RAMBlock information needed to map I/O buffers. This is important for vdpa-blk devices because they rely on the ability to map I/O buffers.

Stefan Hajnoczi (4):
block: add BdrvChildClass->propagate_attach/detach() callbacks
block: add blk_add_attach/detach_notifier() APIs
block: rename RAMBlockRegistrar->notifier field
block: update inserted/removed nodes from BlockRAMRegistrar

Quoted 8 lines
 include/block/block_int-common.h            |  11 ++
 include/system/block-backend-global-state.h |   9 ++
 include/system/block-ram-registrar.h        |   4 +-
 block.c                                     |  56 +++++--
 block/block-backend.c                       |  44 ++++++
 block/block-ram-registrar.c                 |  75 ++++++++-
 tests/unit/test-block-backend.c             | 164 ++++++++++++++++++++
 7 files changed, 344 insertions(+), 19 deletions(-)

I need to send a v2 because I forgot to squash in a compiler warning fix.

Stefan