qcow2: Fix removal of list members from BDRVQcow2State.cluster_allocs
PATCH 1/1 qcow2: Fix removal of list members from BDRVQcow2State.cluster_allocs
When a write request needs to allocate new clusters (or change the L2 bitmap of existing ones) a QCowL2Meta structure is created so the L2 metadata can be later updated and any copy-on-write can be performed if necessary.
A write request can span a region consisting of an arbitrary combination of previously unallocated and allocated clusters, and if the unallocated ones can be put contiguous to the existing ones then QEMU will do so in order to minimize the number of write operations.
In practice this means that a write request has not just one but a number of QCowL2Meta structures. All of them are added to the cluster_allocs list that is stored in BDRVQcow2State and is used to detect overlapping requests. After the write request finishes all its associated QCowL2Meta are removed from that list. calculate_l2_meta() takes care of creating and putting those structures in the list, and qcow2_handle_l2meta() takes care of removing them.
The problem is that the error path in handle_alloc() also tries to remove an item in that list, a remnant from the time when this was handled there (that code would not even be correct anymore because it only removes one struct and not all the ones from the same write request).
This can trigger a double removal of the same item from the list, causing a crash. This is not easy to reproduce in practice because it requires that do_alloc_cluster_offset() fails after a successful previous allocation during the same write request, but it can be reproduced with the included test case.
As a last thing, this patch also removes the condition that l2meta->nb_clusters is not 0 in qcow2_handle_l2meta(). This was only necessary when that structure was allocated in the stack in order to detect if it had been initialized or not. This changed in commit f50f88b9fe and nowadays nb_clusters is guaranteed to be > 0.
--- block/qcow2-cluster.c | 3 -- block/qcow2.c | 4 +- tests/qemu-iotests/305 | 75 ++++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/305.out | 16 ++++++++ tests/qemu-iotests/group | 1 + 5 files changed, 93 insertions(+), 6 deletions(-) create mode 100755 tests/qemu-iotests/305 create mode 100644 tests/qemu-iotests/305.out
block/qcow2-cluster.c +0 -3
@@ -1709,9 +1709,6 @@ static int handle_alloc(BlockDriverState *bs, uint64_t guest_offset, out:qcow2_cache_put (s->l2_table_cache, (void **) &l2_slice); -if (ret <0 && *m && (*m)->nb_clusters >0 ) { -QLIST_REMOVE (*m, next_in_flight); - }return ret; }
block/qcow2.c +1 -3
@@ -2112,9 +2112,7 @@ static coroutine_fn int qcow2_handle_l2meta(BlockDriverState *bs, }/* Take the request off the list of running requests */ -if (l2meta->nb_clusters !=0 ) { -QLIST_REMOVE (l2meta, next_in_flight); - } +QLIST_REMOVE (l2meta, next_in_flight);qemu_co_queue_restart_all (&l2meta->dependent_requests);
tests/qemu-iotests/305 new file +75 -0
75 lines not on this page · raw message
tests/qemu-iotests/305.out new file +16 -0
@@ -0,0 +1,16 @@ +QA output created by 305 +### Create the image +Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1048576 +### Fill the first refcount block and one data cluster from the second +wrote 126976/126976 bytes at offset 0 +124 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +### Discard two of the last data clusters, leave one in the middle +discard 1024/1024 bytes at offset 123904 +1 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +discard 1024/1024 bytes at offset 125952 +1 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +### Corrupt the offset of the second refcount block +### Try to allocate the discarded clusters again +qcow2: Marking image as corrupt: Refblock offset 0x20600 unaligned (reftable index: 0x1); further corruption events will be suppressed +write failed: Input/output error +*** done
tests/qemu-iotests/group +1 -0
@@ -313,3 +313,4 @@ 302 quick 303 rw quick 304 rw quick +305 rw quick
Signature · 1 line
2.20.1
Quoted 30 lines · Alberto Garcia
When a write request needs to allocate new clusters (or change the L2 bitmap of existing ones) a QCowL2Meta structure is created so the L2 metadata can be later updated and any copy-on-write can be performed if necessary.
A write request can span a region consisting of an arbitrary combination of previously unallocated and allocated clusters, and if the unallocated ones can be put contiguous to the existing ones then QEMU will do so in order to minimize the number of write operations.
In practice this means that a write request has not just one but a number of QCowL2Meta structures. All of them are added to the
cluster_allocslist that is stored in BDRVQcow2State and is used to detect overlapping requests. After the write request finishes all its associated QCowL2Meta are removed from that list.calculate_l2_meta()takes care of creating and putting those structures in the list, andqcow2_handle_l2meta()takes care of removing them.The problem is that the error path in
handle_alloc()also tries to remove an item in that list, a remnant from the time when this was handled there (that code would not even be correct anymore because it only removes one struct and not all the ones from the same write request).This can trigger a double removal of the same item from the list, causing a crash. This is not easy to reproduce in practice because it requires that
do_alloc_cluster_offset()fails after a successful previous allocation during the same write request, but it can be reproduced with the included test case.
Quoted 5 lines
As a last thing, this patch also removes the condition that
l2meta->nb_clustersis not 0 inqcow2_handle_l2meta(). This was only necessary when that structure was allocated in the stack in order to detect if it had been initialized or not. This changed in commitf50f88b9feand nowadaysnb_clustersis guaranteed to be > 0.
On my search for /(\.|->)nb_clusters/ to verify this, I noticed this comment for qcow2_alloc_cluster_offset():
* If the cluster was already allocated, m->nb_clusters is set to 0 and
* other fields in m are meaningless.
Should that be dropped, too?
Quoted 10 lines
Signed-off-by Alberto Garcia <[email protected]>--- block/qcow2-cluster.c | 3 -- block/qcow2.c | 4 +- tests/qemu-iotests/305 | 75 ++++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/305.out | 16 ++++++++ tests/qemu-iotests/group | 1 + 5 files changed, 93 insertions(+), 6 deletions(-) create mode 100755 tests/qemu-iotests/305 create mode 100644 tests/qemu-iotests/305.out
[...]
Quoted 6 lines
tests/qemu-iotests/305 new file +0
-0@@ -0,0 +1,75 @@
[...]
tests/qemu-iotests/305 line 43
Quoted 4 lines
tests/qemu-iotests/305 new file +4
-0+_supported_fmt qcow2 +_supported_proto file +_supported_os Linux +_unsupported_imgopts cluster_size refcount_bits extended_l2
data_file, too, because the refcounts work differently there. (As in:
Not at all for data clusters.)
Also, compat(=0.10), because that wouldn’t allow -o refcount_bits=64.
tests/qemu-iotests/305
Quoted 2 lines
tests/qemu-iotests/305 new file +2
-0+ +refcount_table_offset=$((0x400))
I would like to suggest $(peek_file_be "$TEST_IMG" 48 8), to set an example for future generations; but not strictly necessary, of course. O:)
Anyway, at least with the _unsupported_imgopts line completed:
Quoted 7 lines · Max Reitz
On my search for /(\.|->)nb_clusters/ to verify this, I noticed this comment for
qcow2_alloc_cluster_offset():* If the cluster was already allocated,
m->nb_clustersis set to 0 and
* other fields in m are meaningless.Should that be dropped, too?
Actually the whole comment should be rewritten (the function does not allocale just one cluster), I'll send v2 with that and the rest of your proposed changes.
Berto