block: Use bdrv_unref_child() for all children in bdrv_close()
ping
Quoted 56 lines · Alberto Garcia
bdrv_unref_child()does the following things:- Updates the
child->bs->inherits_frompointer.
- Callsbdrv_detach_child()to remove the BdrvChild frombs->children.
- Callsbdrv_unref()to unref the child BlockDriverState.When
bdrv_unref_child()was introduced in commit33a604075cit was not used inbdrv_close()because the drivers that had additional children (like quorum or blkverify) had already calledbdrv_unref()on their children during their own close functions.This was changed later (in
0bd6e91a7efor quorum, in3e586be0b2for blkverify) so there's no reason not to usebdrv_unref_child()inbdrv_close()anymore.After this there's also no need to remove
bs->backingandbs->fileseparately from the rest of the children, sobdrv_close()can be simplified.Signed-off-by Alberto Garcia <[email protected]>--- block.c | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-)block.c +3
-13@@ -3842,22 +3842,12 @@ static void bdrv_close(BlockDriverState *bs) bs->drv =NULL ; } -bdrv_set_backing_hd (bs,NULL , &error_abort); - -if (bs->file !=NULL ) { -bdrv_unref_child (bs, bs->file); - bs->file =NULL ; - } - QLIST_FOREACH_SAFE(child, &bs->children, next, next) { -/* TODO Remove bdrv_unref() from drivers' close function and use -* bdrv_unref_child() here */ -if (child->bs->inherits_from == bs) { - child->bs->inherits_from =NULL ; - } -bdrv_detach_child (child); +bdrv_unref_child (bs, child); } + bs->backing =NULL ; + bs->file =NULL ;g_free (bs->opaque); bs->opaque =NULL ;atomic_set (&bs->copy_on_read,0 );Signature · 1 line
2.11.0
Max Reitz
AppliedQuoted 6 lines · Alberto Garcia
bdrv_unref_child()does the following things:- Updates the
child->bs->inherits_frompointer.
- Callsbdrv_detach_child()to remove the BdrvChild frombs->children.
- Callsbdrv_unref()to unref the child BlockDriverState.
Quoted 8 lines
When
bdrv_unref_child()was introduced in commit33a604075cit was not used inbdrv_close()because the drivers that had additional children (like quorum or blkverify) had already calledbdrv_unref()on their children during their own close functions.This was changed later (in
0bd6e91a7efor quorum, in3e586be0b2for blkverify) so there's no reason not to usebdrv_unref_child()inbdrv_close()anymore.
Hm. Sounds reasonable. But the commit dates are interesting. The TODO was introduced in commit 33a604075c51, so actually Kevin wrote that patch just one day before the patches for vmdk, quorum, and blkverify. They hit the mailing list two months apart, though (July vs. September 2015).
But still I wonder whether there was really no other reason nobody resolved this TODO until now.
Hm. Perhaps because it looks like we want to keep the children list central to the block layer so no block driver would have to ever even call bdrv_unref_child(). But that sounds overly complicated for no gain; and as long as e.g. quorum has the children list in its own state object, it should unref all children when it deletes the list, because that's the right thing to do.
So, yeah, I'm a bit wary, but I can't see why not.
Quoted 8 lines
After this there's also no need to remove
bs->backingandbs->fileseparately from the rest of the children, sobdrv_close()can be simplified.Signed-off-by Alberto Garcia <[email protected]>--- block.c | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-)
Thanks, applied to my block branch:
https://git.xanclic.moe/XanClic/qemu/commits/branch/block
Max
Quoted 15 lines · Alberto Garcia
bdrv_unref_child()does the following things:- Updates the
child->bs->inherits_frompointer.
- Callsbdrv_detach_child()to remove the BdrvChild frombs->children.
- Callsbdrv_unref()to unref the child BlockDriverState.When
bdrv_unref_child()was introduced in commit33a604075cit was not used inbdrv_close()because the drivers that had additional children (like quorum or blkverify) had already calledbdrv_unref()on their children during their own close functions.This was changed later (in
0bd6e91a7efor quorum, in3e586be0b2for blkverify) so there's no reason not to usebdrv_unref_child()inbdrv_close()anymore.
Quoted 8 lines
After this there's also no need to remove
bs->backingandbs->fileseparately from the rest of the children, sobdrv_close()can be simplified.Signed-off-by Alberto Garcia <[email protected]>--- block.c | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-)
Hm, I’m afraid this breaks make check:
$ make -j 4 && make tests/test-bdrv-drain && gtester tests/test-bdrv-drain
TEST: tests/test-bdrv-drain... (pid=22321)
test-bdrv-drain: block.c:5397: bdrv_unref: Assertion `bs->refcnt > 0' failed.
(gdb) bt
#0 0x00007f15c7ffc57f in raise () from /lib64/libc.so.6 #1 0x00007f15c7fe6895 in abort () from /lib64/libc.so.6 #2 0x00007f15c7fe6769 in __assert_fail_base.cold.0 () from /lib64/libc.so.6 #3 0x00007f15c7ff4a26 in __assert_fail () from /lib64/libc.so.6 #4 0x00005562a11b0a00 in bdrv_unref (bs=0x5562a3370c30) at block.c:5397 #5 bdrv_unref (bs=0x5562a3370c30) at block.c:5392 #6 0x00005562a11a7f8f in test_detach_indirect (by_parent_cb=<optimized out>) at tests/test-bdrv-drain.c:1442 #7 0x00007f15c8766fca in ?? () from /lib64/libglib-2.0.so.0 #8 0x00007f15c8766e84 in ?? () from /lib64/libglib-2.0.so.0 #9 0x00007f15c8766e84 in ?? () from /lib64/libglib-2.0.so.0
#10 0x00007f15c8767282 in g_test_run_suite () from /lib64/libglib-2.0.so.0 #11 0x00007f15c87672a5 in g_test_run () from /lib64/libglib-2.0.so.0 #12 0x00005562a11a5b77 in main (argc=<optimized out>, argv=<optimized out>) at tests/test-bdrv-drain.c:1617
So I’ll dequeue this patch for now.
Max