Skip to contentSkip to conversation

block: update inserted/removed nodes from BlockRAMRegistrar

PATCH In review Stefan Hajnoczi 2 participants 7 messages Oct 7, 2025 – Oct 8, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover block: update inserted/removed nodes from BlockRAMRegistrar

v2:
- Add a tests/functional test case [Kevin]

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 (5):
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 tests/functional: add vdpa-blk blockdev-mirror test

 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                   |  73 +++++++-
 tests/unit/test-block-backend.c               | 164 ++++++++++++++++++
 tests/functional/x86_64/meson.build           |   1 +
 .../functional/x86_64/test_vdpa_blk_mirror.py | 118 +++++++++++++
 9 files changed, 461 insertions(+), 19 deletions(-)
 create mode 100755 tests/functional/x86_64/test_vdpa_blk_mirror.py
Signature · 1 line
2.51.0
#1 lore raw

PATCH v2 1/5 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 v2 2/5 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 v2 3/5 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 v2 4/5 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          | 61 +++++++++++++++++++++++++++-
 2 files changed, 61 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 +59 -2

94 lines not on this page · raw message

Signature · 1 line
2.51.0
#5 lore raw

PATCH v2 5/5 tests/functional: add vdpa-blk blockdev-mirror test

In reply to Stefan Hajnoczi

Add a test case that reproduces
https://issues.redhat.com/browse/RHEL-88175.

When the mirror blockjob completes, it replaces the original vdpa-blk blockdev node with a new vdpa-blk blockdev. This will only work if the BlockRAMRegistrar populates memory mappings (see the previous commit).

---
 tests/functional/x86_64/meson.build           |   1 +
 .../functional/x86_64/test_vdpa_blk_mirror.py | 118 ++++++++++++++++++
 2 files changed, 119 insertions(+)
 create mode 100755 tests/functional/x86_64/test_vdpa_blk_mirror.py
tests/functional/x86_64/meson.build +1 -0
index f78eec5e6c..dfe0e00190 100644
@@ -33,6 +33,7 @@ tests_x86_64_system_thorough = [
   'replay',
   'reverse_debug',
   'tuxrun',
+  'vdpa_blk_mirror',
   'vfio_user_client',
   'virtio_balloon',
   'virtio_gpu',
tests/functional/x86_64/test_vdpa_blk_mirror.py new file +118 -0

118 lines not on this page · raw message

Signature · 1 line
2.51.0
Tested-by on v2 5/5
#6 lore raw

Re: [PATCH v2 5/5] tests/functional: add vdpa-blk blockdev-mirror test

Quoted 7 lines · Stefan Hajnoczi

Add a test case that reproduces
https://issues.redhat.com/browse/RHEL-88175.

When the mirror blockjob completes, it replaces the original vdpa-blk blockdev node with a new vdpa-blk blockdev. This will only work if the BlockRAMRegistrar populates memory mappings (see the previous commit).

Quoted 6 lines
---
 tests/functional/x86_64/meson.build           |   1 +
 .../functional/x86_64/test_vdpa_blk_mirror.py | 118 ++++++++++++++++++
 2 files changed, 119 insertions(+)
 create mode 100755 tests/functional/x86_64/test_vdpa_blk_mirror.py

I tried running this test to see how it fared on my system. With the entire series applied (and after I set up passwordless sudo, to run instead of skip), the test passed. I then reverted the rest of the series, and the test hung, instead of timing out or printing an error message about a specific failure. Then I tried reapplying the rest of the series, but now the test hangs because whatever state the system was left in after the earlier failed test is preventing the re-run from starting; and it appears to be uninterruptible (SIGINT is not ending the hung test, and even ctrl-z is not letting me move the test into a background process; I had to resort to kill -9 from another terminal).

I'm less familiar with functional tests in general, but it might be nice to figure out a way to quickly report failure when testing without the rest of the series, rather than leaving the system in a wedged state.

Of course, since CI will never be running the test without the rest of the series in place, that is not a show-stopper for accepting this series as-is. I'm not even sure if adding a timeout to the dd command
[1] would help in tearing down the vdpa_sim_blk device on a test failure.

And it may not even be something that QEMU can do anything about - the whole point of the rest of the series is so that vdpa still has memory mappings after migration so it can complete I/O; without the rest of the series, the test is correctly proving that the migration lost the mappings and thus can't complete I/O, even if I have no idea how to force the kernel to relinquish the device when we know that the I/O won't ever happen.

So, I'm fine if you add:

even though I'm not comfortable with a Reviewed-by at this time.

Quoted 23 lines
tests/functional/x86_64/test_vdpa_blk_mirror.py +21 -0
 
+class VdpaBlk(LinuxKernelTest):
+
+    KERNEL_COMMAND_LINE = 'printk.time=0 console=ttyS0 rd.rescue'
+    ASSET_KERNEL = Asset(
+        ('https://archives.fedoraproject.org/pub/archive/fedora/linux/releases'
+         '/31/Server/x86_64/os/images/pxeboot/vmlinuz'),
+        'd4738d03dbbe083ca610d0821d0a8f1488bebbdccef54ce33e3adb35fda00129')
+    ASSET_INITRD = Asset(
+        ('https://archives.fedoraproject.org/pub/archive/fedora/linux/releases'
+         '/31/Server/x86_64/os/images/pxeboot/initrd.img'),
+        '277cd6c7adf77c7e63d73bbb2cded8ef9e2d3a2f100000e92ff1f8396513cd8b')
+    VDPA_DEV_1 = f'vdpa-{os.getpid()}-1'
+    VDPA_DEV_2 = f'vdpa-{os.getpid()}-2'
+
+    def setUp(self) -> None:
+        def create_vdpa_dev(name):
+            '''
+            Create a new vdpasim_blk device and return its vhost_vdpa device
+            path.
+            '''
+            run(f'sudo -n vdpa dev add mgmtdev vdpasim_blk name {name}')

tests/functional/x86_64/test_vdpa_blk_mirror.py line 57

Quoted 10 lines
tests/functional/x86_64/test_vdpa_blk_mirror.py +10 -0
+            sysfs_vhost_vdpa_dev_dir = \
+                glob.glob(f'/sys/bus/vdpa/devices/{name}/vhost-vdpa-*')[0]
+            vhost_dev_basename = os.path.basename(sysfs_vhost_vdpa_dev_dir)
+            vhost_dev_path = f'/dev/{vhost_dev_basename}'
+            run(f'sudo -n chown {os.getuid()}:{os.getgid()} {vhost_dev_path}')
+            return vhost_dev_path
+
+        try:
+            run('sudo -n modprobe vhost_vdpa')
+            run('sudo -n modprobe vdpa_sim_blk')

Once I did kill -9 on the hung test, manually trying this line fails with:

$ sudo -n modprobe vdpa_sim_blk
modprobe: ERROR: could not insert 'vdpa_sim_blk': Device or resource busy

as my evidence that something really did get wedged in trying to clean up after the hang. Even

$ sudo -n vdpa dev show

is hanging with no output, no response to ctrl-c or -z, and requires kill -9. Running it under strace ends at:

...
socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC) = 3
setsockopt(3, SOL_NETLINK, NETLINK_CAP_ACK, [1], 4) = 0
setsockopt(3, SOL_NETLINK, NETLINK_EXT_ACK, [1], 4) = 0
bind(3, {sa_family=AF_NETLINK, nl_pid=0, nl_groups=00000000}, 12) = 0 getsockname(3, {sa_family=AF_NETLINK, nl_pid=3694229, nl_groups=00000000}, [12]) = 0

Quoted 6 lines
tests/functional/x86_64/test_vdpa_blk_mirror.py +6 -0
+
+            self.vhost_dev_1_path = create_vdpa_dev(self.VDPA_DEV_1)
+            self.vhost_dev_2_path = create_vdpa_dev(self.VDPA_DEV_2)
+        except subprocess.CalledProcessError:
+            self.skipTest('Failed to set up vdpa_blk device')
+

tests/functional/x86_64/test_vdpa_blk_mirror.py line 73

Quoted 10 lines
tests/functional/x86_64/test_vdpa_blk_mirror.py +10 -0
+        super().setUp()
+
+    def tearDown(self) -> None:
+        super().tearDown()
+
+        try:
+            run(f'sudo -n vdpa dev del {self.VDPA_DEV_2}')
+            run(f'sudo -n vdpa dev del {self.VDPA_DEV_1}')
+            run('sudo -n modprobe --remove vdpa_sim_blk')
+            run('sudo -n modprobe --remove vhost_vdpa')

so these cleanups are not happening because of whatever else already wedged.

Quoted 30 lines
tests/functional/x86_64/test_vdpa_blk_mirror.py +30 -0
+        except subprocess.CalledProcessError:
+            pass # ignore failures
+
+    def test_mirror(self) -> None:
+        '''
+        Check that I/O works after a mirror blockjob pivots. See
+        https://issues.redhat.com/browse/RHEL-88175.
+        '''
+        kernel_path = self.ASSET_KERNEL.fetch()
+        initrd_path = self.ASSET_INITRD.fetch()
+
+        self.vm.add_args('-m', '1G')
+        self.vm.add_args('-object', 'memory-backend-memfd,id=mem,size=1G')
+        self.vm.add_args('-machine', 'pc,accel=kvm:tcg,memory-backend=mem')
+        self.vm.add_args('-append', self.KERNEL_COMMAND_LINE)
+        self.vm.add_args('-blockdev',
+            'virtio-blk-vhost-vdpa,node-name=vdpa-blk-0,' +
+            f'path={self.vhost_dev_1_path},cache.direct=on')
+        self.vm.add_args('-device', 'virtio-blk-pci,drive=vdpa-blk-0')
+
+        self.launch_kernel(kernel_path, initrd_path,
+                           wait_for='# ')
+
+        self.vm.cmd('blockdev-add',
+                    driver='virtio-blk-vhost-vdpa',
+                    node_name='vdpa-blk-1',
+                    path=self.vhost_dev_2_path,
+                    cache={'direct': True})
+        self.vm.cmd('blockdev-mirror',
+                    device='vdpa-blk-0',

tests/functional/x86_64/test_vdpa_blk_mirror.py line 113

Quoted 10 lines
tests/functional/x86_64/test_vdpa_blk_mirror.py +10 -0
+                    job_id='mirror0',
+                    target='vdpa-blk-1',
+                    sync='full',
+                    target_is_zero=True)
+        self.vm.event_wait('BLOCK_JOB_READY')
+        self.vm.cmd('block-job-complete',
+                    device='mirror0')
+
+        exec_command_and_wait_for_pattern(self,
+            'dd if=/dev/vda of=/dev/null iflag=direct bs=4k count=1',

[1] This might be the spot where adding a timeout command would help the guest relinquish control of the block device, but that is still not obvious to me whether it would also be enough for the test to fail cleanly and allow a clean restart.

Quoted 8 lines
tests/functional/x86_64/test_vdpa_blk_mirror.py +5 -0
+            '4096 bytes (4.1 kB, 4.0 KiB) copied')
+
+
+if __name__ == '__main__':
+    LinuxKernelTest.main()
Signature · 1 line
2.51.0
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org