Skip to contentSkip to conversation

block: fix bdrv_next() skipping monitor-owned nodes

PATCH Applied Denis V. Lunev 5 participants 8 messages Aug 28 – Oct 2 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 block: fix bdrv_next() skipping monitor-owned nodes

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

it->bs is the cursor into monitor_bdrv_states for the second phase of bdrv_next(), so it has to be NULL when that phase starts. Commit f6d38c9f6d made the first phase store the node it returns there, to unreference the right one when the graph changes underneath. The cursor is now left pointing at the last BlockBackend root, so the second phase resumes from there instead of from the head of the list and never returns a node added before it.

A skipped node drops out of the vm_stop and migration handover paths:
not flushed, not inactivated, not snapshotted. bdrv_inactivate_all() still reports success, so the node keeps its image lock and the migration target cannot open the image. Only detached nodes are hit in practice, as these callers also recurse into children.

Reset the cursor when the second phase starts. old_bs is taken at the top of the function, so f6d38c9f6d keeps working.

Fixes f6d38c9f6d ("block-backend: fix edge case in bdrv_next() where BDS associated to BB changes")
Cc Fiona Ebner <[email protected]>
Cc Kevin Wolf <[email protected]>

---

Notes:
The iterator was correct from its introduction in 2016 until 2024:
it->bs was written only by the second phase, and the node to unreference came from the BlockBackend instead.

f6d38c9f6d's own reproducer does not fire for me, 240 runs across 8 timings, even with its fix reverted. Tried hard.

 block/block-backend.c                          |  1 +
 tests/qemu-iotests/tests/inactive-node-nbd     | 16 ++++++++++++++++
 tests/qemu-iotests/tests/inactive-node-nbd.out |  8 ++++++++
 3 files changed, 25 insertions(+)
block/block-backend.c +1 -0
index 37ba7e9fc4..55497c4551 100644
@@ -625,6 +625,7 @@ BlockDriverState *bdrv_next(BdrvNextIterator *it)
             return bs;
         }
         it->phase = BDRV_NEXT_MONITOR_OWNED;
+        it->bs = NULL;
     }
 
     /* Then return the monitor-owned BDSes without a BB attached. Ignore all
tests/qemu-iotests/tests/inactive-node-nbd +16 -0
index 664157bfd0..ef876ae6be 100755
@@ -47,6 +47,7 @@ def node_is_active(_vm, node_name):
     return node['active']
 
 with iotests.FilePath('disk.img') as path, \
+     iotests.FilePath('detached.img') as detached_path, \
      iotests.FilePath('snap.qcow2') as snap_path, \
      iotests.FilePath('snap2.qcow2') as snap2_path, \
      iotests.FilePath('target.img') as target_path, \
@@ -58,6 +59,7 @@ with iotests.FilePath('disk.img') as path, \
     iotests.log('Preparing disk...')
     iotests.qemu_img_create('-f', iotests.imgfmt, path, img_size)
     iotests.qemu_img_create('-f', iotests.imgfmt, target_path, img_size)
+    iotests.qemu_img_create('-f', iotests.imgfmt, detached_path, img_size)
 
     iotests.qemu_img_create('-f', 'qcow2', '-b', path, '-F', iotests.imgfmt,
                             snap_path)
@@ -68,6 +70,9 @@ with iotests.FilePath('disk.img') as path, \
     vm.add_blockdev(f'file,node-name=disk-file,filename={path}')
     vm.add_blockdev(f'{iotests.imgfmt},file=disk-file,node-name=disk-fmt,'
                      'active=off')
+    vm.add_blockdev(f'file,node-name=detached-file,filename={detached_path}')
+    vm.add_blockdev(f'{iotests.imgfmt},file=detached-file,'
+                     'node-name=detached-fmt')
     vm.add_blockdev(f'file,node-name=target-file,filename={target_path}')
     vm.add_blockdev(f'{iotests.imgfmt},file=target-file,node-name=target-fmt')
     vm.add_blockdev(f'file,node-name=snap-file,filename={snap_path}')
@@ -297,6 +302,17 @@ with iotests.FilePath('disk.img') as path, \
     iotests.log('snap2-fmt active: %s' % node_is_active(vm, 'snap2-fmt'))
     iotests.log('target-fmt active: %s' % node_is_active(vm, 'target-fmt'))
 
+    iotests.log('\n=== Inactivating all nodes at once ===')
+
+    # detached-fmt has no parent and no BlockBackend, so nothing can reach it
+    # by recursion. It is only inactivated if bdrv_next() actually returns it.
+    vm.qmp_log('stop')
+    vm.qmp_log('blockdev-set-active', active=False)
+
+    iotests.log('detached-fmt active: %s' % node_is_active(vm, 'detached-fmt'))
+    iotests.log('detached-file active: %s'
+                % node_is_active(vm, 'detached-file'))
+
     iotests.log('\nShutting down...')
     vm.shutdown()
     log = vm.get_log()
tests/qemu-iotests/tests/inactive-node-nbd.out +8 -0
index a458b4fc05..48451dc0c5 100644
@@ -235,5 +235,13 @@ snap-fmt active: True
 snap2-fmt active: True
 target-fmt active: True
 
+=== Inactivating all nodes at once ===
+{"execute": "stop", "arguments": {}}
+{"return": {}}
+{"execute": "blockdev-set-active", "arguments": {"active": false}}
+{"return": {}}
+detached-fmt active: False
+detached-file active: False
+
 Shutting down...
 
Signature · 1 line
2.53.0
Reviewed-by Tested-by on v1 1/1
#1 lore raw

Hi Denis,

Am 29.08.26 um 12:20 AM schrieb Denis V. Lunev:

Quoted 16 lines

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

it->bs is the cursor into monitor_bdrv_states for the second phase of bdrv_next(), so it has to be NULL when that phase starts. Commit f6d38c9f6d made the first phase store the node it returns there, to unreference the right one when the graph changes underneath. The cursor is now left pointing at the last BlockBackend root, so the second phase resumes from there instead of from the head of the list and never returns a node added before it.

A skipped node drops out of the vm_stop and migration handover paths:
not flushed, not inactivated, not snapshotted. bdrv_inactivate_all() still reports success, so the node keeps its image lock and the migration target cannot open the image. Only detached nodes are hit in practice, as these callers also recurse into children.

Quoted 7 lines

Reset the cursor when the second phase starts. old_bs is taken at the top of the function, so f6d38c9f6d keeps working.

Fixes f6d38c9f6d ("block-backend: fix edge case in bdrv_next() where BDS associated to BB changes")
Cc Fiona Ebner <[email protected]>
Cc Kevin Wolf <[email protected]>

thank you for the fix!

Should also go into stable I suppose?

Quoted 9 lines

---

Notes:
The iterator was correct from its introduction in 2016 until 2024:
it->bs was written only by the second phase, and the node to unreference came from the BlockBackend instead.

f6d38c9f6d's own reproducer does not fire for me, 240 runs across 8 timings, even with its fix reverted. Tried hard.

The reproducer has a very short time window. I'm still able to trigger an assertion failure with commit f6d38c9f6d reverted, by putting in some 'query-version' commands in between the 'block-stream' and 'quit' commands, and running the script in a loop, e.g.:

[I] root@pve9a1 ~# cat ~/block-stream.sh
#!/bin/bash
rm -f /tmp/backing.qcow2
rm -f /tmp/top.qcow2
./qemu-img create /tmp/backing.qcow2 -f qcow2 64M
./qemu-io -c "write -P42 0x0 0x1" /tmp/backing.qcow2
./qemu-img create /tmp/top.qcow2 -f qcow2 64M -b /tmp/backing.qcow2 -F qcow2 ./qemu-system-x86_64 --qmp stdio \
--blockdev qcow2,node-name=node0,file.driver=file,file.filename=/tmp/top.qcow2 \
<<EOF
{"execute": "qmp_capabilities"}
{"execute": "block-stream", "arguments": { "job-id": "stream0", "device": "node0" } }
{"execute": "query-version"}
{"execute": "query-version"}
{"execute": "query-version"}
{"execute": "query-version"}
{"execute": "query-version"}
{"execute": "query-version"}
{"execute": "query-version"}
{"execute": "query-version"}
{"execute": "query-version"}
{"execute": "quit"}
EOF

Best Regards,
Fiona

#2 lore raw
Quoted 26 lines · Fiona Ebner

Hi Denis,

Am 29.08.26 um 12:20 AM schrieb Denis V. Lunev:

Quoted 16 lines

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

it->bs is the cursor into monitor_bdrv_states for the second phase of bdrv_next(), so it has to be NULL when that phase starts. Commit f6d38c9f6d made the first phase store the node it returns there, to unreference the right one when the graph changes underneath. The cursor is now left pointing at the last BlockBackend root, so the second phase resumes from there instead of from the head of the list and never returns a node added before it.

A skipped node drops out of the vm_stop and migration handover paths:
not flushed, not inactivated, not snapshotted. bdrv_inactivate_all() still reports success, so the node keeps its image lock and the migration target cannot open the image. Only detached nodes are hit in practice, as these callers also recurse into children.

Quoted 7 lines

Reset the cursor when the second phase starts. old_bs is taken at the top of the function, so f6d38c9f6d keeps working.

Fixes f6d38c9f6d ("block-backend: fix edge case in bdrv_next() where BDS associated to BB changes")
Cc Fiona Ebner <[email protected]>
Cc Kevin Wolf <[email protected]>
Quoted 6 lines

thank you for the fix!

Should also go into stable I suppose?

Unsure. We do not know real consequences, there are no reports. I found this problem by accident only testing migration with big trees a lot.

Den

#3 lore raw
Quoted 6 lines · Denis V. Lunev
Quoted 1 line · Fiona Ebner

Should also go into stable I suppose?

Unsure. We do not know real consequences, there are no reports. I found this problem by accident only testing migration with big trees a lot.

I came across the bug by accident while exploring the code.

It is easy to trigger with a plain -drive next to a detached -blockdev node. If the last BlockBackend root is not monitor-owned, it is not on monitor_bdrv_states, so bdrv_next_monitor_owned() returns NULL right away and the second phase returns no node at all, not only those added before it.

Reproducer:

  qemu-img create -f qcow2 a.qcow2 64M
  qemu-img create -f qcow2 b.qcow2 64M
  qemu-system-x86_64 -S -nodefaults -display none -monitor stdio \
    -drive file=a.qcow2,if=none,id=drv0 \
    -blockdev file,filename=b.qcow2,node-name=bfile \
    -blockdev qcow2,file=bfile,node-name=b0
  (qemu) savevm s1
  (qemu) info snapshots
  (qemu) quit
  qemu-img snapshot -l a.qcow2
  qemu-img snapshot -l b.qcow2

Without the fix, only a.qcow2 has snapshot s1 and savevm does not report an error. Since "info snapshots" uses the same iterator, it even lists s1 as present on all disks. With the fix, both images have it.

For savevm, only nodes without any parent are affected, since snapshots only consider nodes that are attached to a BlockBackend or have no parent at all. Such detached nodes are rare and often short-lived in practice I think, which would explain the lack of reports. Still, the failure is silent, it is a regression since 9.0, and the fix is a single line, so I think it is a good candidate for stable.

Best regards,
Erik

#4 lore raw
In reply to Denis V. Lunev
Quoted 108 lines · Denis V. Lunev

This email originated from an IP that might not be authorized by the domain it was sent from. Do not click links or open attachments unless it is an email you expected to receive.
From: Denis V. Lunev <[email protected]>

it->bs is the cursor into monitor_bdrv_states for the second phase of bdrv_next(), so it has to be NULL when that phase starts. Commit f6d38c9f6d made the first phase store the node it returns there, to unreference the right one when the graph changes underneath. The cursor is now left pointing at the last BlockBackend root, so the second phase resumes from there instead of from the head of the list and never returns a node added before it.

A skipped node drops out of the vm_stop and migration handover paths:
not flushed, not inactivated, not snapshotted. bdrv_inactivate_all() still reports success, so the node keeps its image lock and the migration target cannot open the image. Only detached nodes are hit in practice, as these callers also recurse into children.

Reset the cursor when the second phase starts. old_bs is taken at the top of the function, so f6d38c9f6d keeps working.

Fixes f6d38c9f6d ("block-backend: fix edge case in bdrv_next() where BDS associated to BB changes")
Cc Fiona Ebner <[email protected]>
Cc Kevin Wolf <[email protected]>

---

Notes:
The iterator was correct from its introduction in 2016 until 2024:
it->bs was written only by the second phase, and the node to unreference came from the BlockBackend instead.

f6d38c9f6d's own reproducer does not fire for me, 240 runs across 8 timings, even with its fix reverted. Tried hard.

 block/block-backend.c                          |  1 +
 tests/qemu-iotests/tests/inactive-node-nbd     | 16 ++++++++++++++++
 tests/qemu-iotests/tests/inactive-node-nbd.out |  8 ++++++++
 3 files changed, 25 insertions(+)
block/block-backend.c +1 -0
index 37ba7e9fc4..55497c4551 100644
@@ -625,6 +625,7 @@ BlockDriverState *bdrv_next(BdrvNextIterator *it)
             return bs;
         }
         it->phase = BDRV_NEXT_MONITOR_OWNED;
+        it->bs = NULL;
     }
 
     /* Then return the monitor-owned BDSes without a BB attached. Ignore all
tests/qemu-iotests/tests/inactive-node-nbd +16 -0
index 664157bfd0..ef876ae6be 100755
@@ -47,6 +47,7 @@ def node_is_active(_vm, node_name):
     return node['active']
 
 with iotests.FilePath('disk.img') as path, \
+     iotests.FilePath('detached.img') as detached_path, \
      iotests.FilePath('snap.qcow2') as snap_path, \
      iotests.FilePath('snap2.qcow2') as snap2_path, \
      iotests.FilePath('target.img') as target_path, \
@@ -58,6 +59,7 @@ with iotests.FilePath('disk.img') as path, \
     iotests.log('Preparing disk...')
     iotests.qemu_img_create('-f', iotests.imgfmt, path, img_size)
     iotests.qemu_img_create('-f', iotests.imgfmt, target_path, img_size)
+    iotests.qemu_img_create('-f', iotests.imgfmt, detached_path, img_size)
 
     iotests.qemu_img_create('-f', 'qcow2', '-b', path, '-F', iotests.imgfmt,
                             snap_path)
@@ -68,6 +70,9 @@ with iotests.FilePath('disk.img') as path, \
     vm.add_blockdev(f'file,node-name=disk-file,filename={path}')
     vm.add_blockdev(f'{iotests.imgfmt},file=disk-file,node-name=disk-fmt,'
                      'active=off')
+    vm.add_blockdev(f'file,node-name=detached-file,filename={detached_path}')
+    vm.add_blockdev(f'{iotests.imgfmt},file=detached-file,'
+                     'node-name=detached-fmt')
     vm.add_blockdev(f'file,node-name=target-file,filename={target_path}')
     vm.add_blockdev(f'{iotests.imgfmt},file=target-file,node-name=target-fmt')
     vm.add_blockdev(f'file,node-name=snap-file,filename={snap_path}')
@@ -297,6 +302,17 @@ with iotests.FilePath('disk.img') as path, \
     iotests.log('snap2-fmt active: %s' % node_is_active(vm, 'snap2-fmt'))
     iotests.log('target-fmt active: %s' % node_is_active(vm, 'target-fmt'))
 
+    iotests.log('\n=== Inactivating all nodes at once ===')
+
+    # detached-fmt has no parent and no BlockBackend, so nothing can reach it
+    # by recursion. It is only inactivated if bdrv_next() actually returns it.
+    vm.qmp_log('stop')
+    vm.qmp_log('blockdev-set-active', active=False)
+
+    iotests.log('detached-fmt active: %s' % node_is_active(vm, 'detached-fmt'))
+    iotests.log('detached-file active: %s'
+                % node_is_active(vm, 'detached-file'))
+
     iotests.log('\nShutting down...')
     vm.shutdown()
     log = vm.get_log()
tests/qemu-iotests/tests/inactive-node-nbd.out +0 -0
index a458b4fc05..48451dc0c5 100644
@@ -235,5 +235,13 @@ snap-fmt active: True
 snap2-fmt active: True
 target-fmt active: True
 

tests/qemu-iotests/tests/inactive-node-nbd.out snap-fmt active: True · line 247

Quoted 10 lines
tests/qemu-iotests/tests/inactive-node-nbd.out +8 -0
+=== Inactivating all nodes at once ===
+{"execute": "stop", "arguments": {}}
+{"return": {}}
+{"execute": "blockdev-set-active", "arguments": {"active": false}}
+{"return": {}}
+detached-fmt active: False
+detached-file active: False
+
 Shutting down...
 

Kevin,

have you had a chance to take a look?

Den

#5 lore raw
In reply to Denis V. Lunev
Quoted 108 lines · Denis V. Lunev

This email originated from an IP that might not be authorized by the domain it was sent from. Do not click links or open attachments unless it is an email you expected to receive.
From: Denis V. Lunev <[email protected]>

it->bs is the cursor into monitor_bdrv_states for the second phase of bdrv_next(), so it has to be NULL when that phase starts. Commit f6d38c9f6d made the first phase store the node it returns there, to unreference the right one when the graph changes underneath. The cursor is now left pointing at the last BlockBackend root, so the second phase resumes from there instead of from the head of the list and never returns a node added before it.

A skipped node drops out of the vm_stop and migration handover paths:
not flushed, not inactivated, not snapshotted. bdrv_inactivate_all() still reports success, so the node keeps its image lock and the migration target cannot open the image. Only detached nodes are hit in practice, as these callers also recurse into children.

Reset the cursor when the second phase starts. old_bs is taken at the top of the function, so f6d38c9f6d keeps working.

Fixes f6d38c9f6d ("block-backend: fix edge case in bdrv_next() where BDS associated to BB changes")
Cc Fiona Ebner <[email protected]>
Cc Kevin Wolf <[email protected]>

---

Notes:
The iterator was correct from its introduction in 2016 until 2024:
it->bs was written only by the second phase, and the node to unreference came from the BlockBackend instead.

f6d38c9f6d's own reproducer does not fire for me, 240 runs across 8 timings, even with its fix reverted. Tried hard.

 block/block-backend.c                          |  1 +
 tests/qemu-iotests/tests/inactive-node-nbd     | 16 ++++++++++++++++
 tests/qemu-iotests/tests/inactive-node-nbd.out |  8 ++++++++
 3 files changed, 25 insertions(+)
block/block-backend.c +1 -0
index 37ba7e9fc4..55497c4551 100644
@@ -625,6 +625,7 @@ BlockDriverState *bdrv_next(BdrvNextIterator *it)
             return bs;
         }
         it->phase = BDRV_NEXT_MONITOR_OWNED;
+        it->bs = NULL;
     }
 
     /* Then return the monitor-owned BDSes without a BB attached. Ignore all
tests/qemu-iotests/tests/inactive-node-nbd +16 -0
index 664157bfd0..ef876ae6be 100755
@@ -47,6 +47,7 @@ def node_is_active(_vm, node_name):
     return node['active']
 
 with iotests.FilePath('disk.img') as path, \
+     iotests.FilePath('detached.img') as detached_path, \
      iotests.FilePath('snap.qcow2') as snap_path, \
      iotests.FilePath('snap2.qcow2') as snap2_path, \
      iotests.FilePath('target.img') as target_path, \
@@ -58,6 +59,7 @@ with iotests.FilePath('disk.img') as path, \
     iotests.log('Preparing disk...')
     iotests.qemu_img_create('-f', iotests.imgfmt, path, img_size)
     iotests.qemu_img_create('-f', iotests.imgfmt, target_path, img_size)
+    iotests.qemu_img_create('-f', iotests.imgfmt, detached_path, img_size)
 
     iotests.qemu_img_create('-f', 'qcow2', '-b', path, '-F', iotests.imgfmt,
                             snap_path)
@@ -68,6 +70,9 @@ with iotests.FilePath('disk.img') as path, \
     vm.add_blockdev(f'file,node-name=disk-file,filename={path}')
     vm.add_blockdev(f'{iotests.imgfmt},file=disk-file,node-name=disk-fmt,'
                      'active=off')
+    vm.add_blockdev(f'file,node-name=detached-file,filename={detached_path}')
+    vm.add_blockdev(f'{iotests.imgfmt},file=detached-file,'
+                     'node-name=detached-fmt')
     vm.add_blockdev(f'file,node-name=target-file,filename={target_path}')
     vm.add_blockdev(f'{iotests.imgfmt},file=target-file,node-name=target-fmt')
     vm.add_blockdev(f'file,node-name=snap-file,filename={snap_path}')
@@ -297,6 +302,17 @@ with iotests.FilePath('disk.img') as path, \
     iotests.log('snap2-fmt active: %s' % node_is_active(vm, 'snap2-fmt'))
     iotests.log('target-fmt active: %s' % node_is_active(vm, 'target-fmt'))
 
+    iotests.log('\n=== Inactivating all nodes at once ===')
+
+    # detached-fmt has no parent and no BlockBackend, so nothing can reach it
+    # by recursion. It is only inactivated if bdrv_next() actually returns it.
+    vm.qmp_log('stop')
+    vm.qmp_log('blockdev-set-active', active=False)
+
+    iotests.log('detached-fmt active: %s' % node_is_active(vm, 'detached-fmt'))
+    iotests.log('detached-file active: %s'
+                % node_is_active(vm, 'detached-file'))
+
     iotests.log('\nShutting down...')
     vm.shutdown()
     log = vm.get_log()
tests/qemu-iotests/tests/inactive-node-nbd.out +0 -0
index a458b4fc05..48451dc0c5 100644
@@ -235,5 +235,13 @@ snap-fmt active: True
 snap2-fmt active: True
 target-fmt active: True
 

tests/qemu-iotests/tests/inactive-node-nbd.out snap-fmt active: True · line 247

Quoted 10 lines
tests/qemu-iotests/tests/inactive-node-nbd.out +8 -0
+=== Inactivating all nodes at once ===
+{"execute": "stop", "arguments": {}}
+{"return": {}}
+{"execute": "blockdev-set-active", "arguments": {"active": false}}
+{"return": {}}
+detached-fmt active: False
+detached-file active: False
+
 Shutting down...
 

ping

#6 lore raw
In reply to Denis V. Lunev
Quoted 108 lines · Denis V. Lunev

This email originated from an IP that might not be authorized by the domain it was sent from. Do not click links or open attachments unless it is an email you expected to receive.
From: Denis V. Lunev <[email protected]>

it->bs is the cursor into monitor_bdrv_states for the second phase of bdrv_next(), so it has to be NULL when that phase starts. Commit f6d38c9f6d made the first phase store the node it returns there, to unreference the right one when the graph changes underneath. The cursor is now left pointing at the last BlockBackend root, so the second phase resumes from there instead of from the head of the list and never returns a node added before it.

A skipped node drops out of the vm_stop and migration handover paths:
not flushed, not inactivated, not snapshotted. bdrv_inactivate_all() still reports success, so the node keeps its image lock and the migration target cannot open the image. Only detached nodes are hit in practice, as these callers also recurse into children.

Reset the cursor when the second phase starts. old_bs is taken at the top of the function, so f6d38c9f6d keeps working.

Fixes f6d38c9f6d ("block-backend: fix edge case in bdrv_next() where BDS associated to BB changes")
Cc Fiona Ebner <[email protected]>
Cc Kevin Wolf <[email protected]>

---

Notes:
The iterator was correct from its introduction in 2016 until 2024:
it->bs was written only by the second phase, and the node to unreference came from the BlockBackend instead.

f6d38c9f6d's own reproducer does not fire for me, 240 runs across 8 timings, even with its fix reverted. Tried hard.

 block/block-backend.c                          |  1 +
 tests/qemu-iotests/tests/inactive-node-nbd     | 16 ++++++++++++++++
 tests/qemu-iotests/tests/inactive-node-nbd.out |  8 ++++++++
 3 files changed, 25 insertions(+)
block/block-backend.c +1 -0
index 37ba7e9fc4..55497c4551 100644
@@ -625,6 +625,7 @@ BlockDriverState *bdrv_next(BdrvNextIterator *it)
             return bs;
         }
         it->phase = BDRV_NEXT_MONITOR_OWNED;
+        it->bs = NULL;
     }
 
     /* Then return the monitor-owned BDSes without a BB attached. Ignore all
tests/qemu-iotests/tests/inactive-node-nbd +16 -0
index 664157bfd0..ef876ae6be 100755
@@ -47,6 +47,7 @@ def node_is_active(_vm, node_name):
     return node['active']
 
 with iotests.FilePath('disk.img') as path, \
+     iotests.FilePath('detached.img') as detached_path, \
      iotests.FilePath('snap.qcow2') as snap_path, \
      iotests.FilePath('snap2.qcow2') as snap2_path, \
      iotests.FilePath('target.img') as target_path, \
@@ -58,6 +59,7 @@ with iotests.FilePath('disk.img') as path, \
     iotests.log('Preparing disk...')
     iotests.qemu_img_create('-f', iotests.imgfmt, path, img_size)
     iotests.qemu_img_create('-f', iotests.imgfmt, target_path, img_size)
+    iotests.qemu_img_create('-f', iotests.imgfmt, detached_path, img_size)
 
     iotests.qemu_img_create('-f', 'qcow2', '-b', path, '-F', iotests.imgfmt,
                             snap_path)
@@ -68,6 +70,9 @@ with iotests.FilePath('disk.img') as path, \
     vm.add_blockdev(f'file,node-name=disk-file,filename={path}')
     vm.add_blockdev(f'{iotests.imgfmt},file=disk-file,node-name=disk-fmt,'
                      'active=off')
+    vm.add_blockdev(f'file,node-name=detached-file,filename={detached_path}')
+    vm.add_blockdev(f'{iotests.imgfmt},file=detached-file,'
+                     'node-name=detached-fmt')
     vm.add_blockdev(f'file,node-name=target-file,filename={target_path}')
     vm.add_blockdev(f'{iotests.imgfmt},file=target-file,node-name=target-fmt')
     vm.add_blockdev(f'file,node-name=snap-file,filename={snap_path}')
@@ -297,6 +302,17 @@ with iotests.FilePath('disk.img') as path, \
     iotests.log('snap2-fmt active: %s' % node_is_active(vm, 'snap2-fmt'))
     iotests.log('target-fmt active: %s' % node_is_active(vm, 'target-fmt'))
 
+    iotests.log('\n=== Inactivating all nodes at once ===')
+
+    # detached-fmt has no parent and no BlockBackend, so nothing can reach it
+    # by recursion. It is only inactivated if bdrv_next() actually returns it.
+    vm.qmp_log('stop')
+    vm.qmp_log('blockdev-set-active', active=False)
+
+    iotests.log('detached-fmt active: %s' % node_is_active(vm, 'detached-fmt'))
+    iotests.log('detached-file active: %s'
+                % node_is_active(vm, 'detached-file'))
+
     iotests.log('\nShutting down...')
     vm.shutdown()
     log = vm.get_log()
tests/qemu-iotests/tests/inactive-node-nbd.out +0 -0
index a458b4fc05..48451dc0c5 100644
@@ -235,5 +235,13 @@ snap-fmt active: True
 snap2-fmt active: True
 target-fmt active: True
 

tests/qemu-iotests/tests/inactive-node-nbd.out snap-fmt active: True · line 247

Quoted 10 lines
tests/qemu-iotests/tests/inactive-node-nbd.out +8 -0
+=== Inactivating all nodes at once ===
+{"execute": "stop", "arguments": {}}
+{"return": {}}
+{"execute": "blockdev-set-active", "arguments": {"active": false}}
+{"return": {}}
+detached-fmt active: False
+detached-file active: False
+
 Shutting down...
 

ping

Applied
#7 lore raw
In reply to Denis V. Lunev

Am 29.08.2026 um 00:20 hat Denis V. Lunev geschrieben:

Quoted 16 lines

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

it->bs is the cursor into monitor_bdrv_states for the second phase of bdrv_next(), so it has to be NULL when that phase starts. Commit f6d38c9f6d made the first phase store the node it returns there, to unreference the right one when the graph changes underneath. The cursor is now left pointing at the last BlockBackend root, so the second phase resumes from there instead of from the head of the list and never returns a node added before it.

A skipped node drops out of the vm_stop and migration handover paths:
not flushed, not inactivated, not snapshotted. bdrv_inactivate_all() still reports success, so the node keeps its image lock and the migration target cannot open the image. Only detached nodes are hit in practice, as these callers also recurse into children.

Quoted 7 lines

Reset the cursor when the second phase starts. old_bs is taken at the top of the function, so f6d38c9f6d keeps working.

Fixes f6d38c9f6d ("block-backend: fix edge case in bdrv_next() where BDS associated to BB changes")
Cc Fiona Ebner <[email protected]>
Cc Kevin Wolf <[email protected]>

Thanks, applied to the block branch.

Kevin