* [Devel] [PATCH hci-8.2] dm-qcow2: fix stale host data showing in a zero cluster after a write
@ 2026-11-23 19:13 Denis V. Lunev
2026-11-23 19:21 ` no-reply
2026-11-24 12:08 ` Konstantin Khorenko
0 siblings, 2 replies; 3+ messages in thread
From: Denis V. Lunev @ 2026-11-23 19:13 UTC (permalink / raw)
To: andrey.zhadchenko, alexander.ivanov, vasileios.almpanis; +Cc: devel
From: Denis V. Lunev <den@openvz.org>
An L2 entry with the zero flag may keep its host cluster, and a zero
subcluster of an extended L2 entry keeps its part of the host cluster.
Their host blocks may hold stale data: qemu leaves it there after a
zero write without unmap. A write that covers such a unit only in part
drops the zero flag or bit but writes only its own range, so the rest
of the unit reads back the stale data instead of zeroes.
prepare_l1l2_allocation() reuses the host cluster as it is, and the
backing file COW of a write whose other border is in an unallocated
subcluster copies only that subcluster.
Send such a write through the sliced COW, as a write with a border in
an unallocated unit over a backing image already goes. The slices of a
zero unit come from the map as zeroes, with no read from disk, and are
written into the host cluster in place together with the data, so the
cluster stays where it is. map->backing_file_cow becomes sliced_cow,
and qio_ext gets cow_in_place for the non-extended case, where the L2
entry is still rewritten. The first partial write into such a unit now
writes the whole unit; writes into other units take the path they did.
https://virtuozzo.atlassian.net/browse/VSTOR-156774
Fixes: 222b6f1291c1 ("dm-qcow2: Introduce driver to create block devices over QCOW2 files")
Feature: dm-qcow2: block device over QCOW2 files driver
Signed-off-by: Denis V. Lunev <den@openvz.org>
---
drivers/md/dm-qcow2-map.c | 69 +++++++++++++++++++++++++++++++--------
drivers/md/dm-qcow2.h | 1 +
2 files changed, 57 insertions(+), 13 deletions(-)
diff --git a/drivers/md/dm-qcow2-map.c b/drivers/md/dm-qcow2-map.c
index 853e7a1230ee..f603ac9158e2 100644
--- a/drivers/md/dm-qcow2-map.c
+++ b/drivers/md/dm-qcow2-map.c
@@ -56,7 +56,7 @@ struct qcow2_map {
* read" or "sublu is not allocated" may be set).
*/
bool data_clu_alloced:1;
- bool backing_file_cow:1;
+ bool sliced_cow:1;
u32 compressed_sectors;
u32 subclus_mask;
@@ -1784,6 +1784,43 @@ static bool qio_border_is_inside_unmapped_unit(struct qcow2 *qcow2,
return ret;
}
+/* Zero subclusters with a border of @qio inside, which keep host blocks */
+static u32 qio_border_zero_subclus_mask(struct qcow2 *qcow2, struct qio *qio,
+ struct qcow2_map *map)
+{
+ u64 start_off = bytes_off_in_cluster(qcow2, qio);
+ u64 end_off = start_off + qio->bi_iter.bi_size;
+ u32 zeroes = map->ext_l2 >> 32, mask = 0;
+ u8 start_bit, end_bit;
+
+ if (!map->data_clu_alloced || map->clu_is_cow)
+ return 0;
+
+ start_bit = qio_subclu_indexes(qcow2, qio, &end_bit);
+ if (SUBCLU_OFF(qcow2, start_off) && (zeroes & (1 << start_bit)))
+ mask |= 1 << start_bit;
+ if (SUBCLU_OFF(qcow2, end_off) && (zeroes & (1 << end_bit)))
+ mask |= 1 << end_bit;
+ return mask;
+}
+
+/*
+ * A zero cluster or subcluster may keep host blocks with stale data.
+ * A write that does not cover it entirely must zero the rest.
+ */
+static bool qio_border_is_inside_zero_unit(struct qcow2 *qcow2,
+ struct qio *qio,
+ struct qcow2_map *map)
+{
+ if (qio->bi_iter.bi_size == qcow2->clu_size)
+ return false;
+
+ if (!qcow2->ext_l2)
+ return map->prealloced;
+
+ return qio_border_zero_subclus_mask(qcow2, qio, map) != 0;
+}
+
static bool qio_is_fully_alloced(struct qcow2 *qcow2, struct qio *qio,
struct qcow2_map *map)
{
@@ -3559,19 +3596,23 @@ static void submit_read_sliced_clu(struct qcow2_map *map, struct qio *qio,
static void submit_read_sliced_cow_clu(struct qcow2_map *map, struct qio *qio)
{
struct qcow2 *qcow2 = map->qcow2;
+ struct qio_ext *ext = qio->ext;
u64 mask = 0;
WARN_ON_ONCE(!(map->level & L2_LEVEL));
+ if (map->data_clu_alloced && !map->clu_is_cow) {
+ ext->cow_in_place = true;
+ ext->allocated_clu_pos = map->data_clu_pos;
+ }
+
if (qcow2->ext_l2) {
mask = map->ext_l2 & ~((u64)map->subclus_mask << 32);
- qio->ext->new_ext_l2 = mask | map->subclus_mask;
- if (map->data_clu_alloced && !map->clu_is_cow) {
- qio->ext->only_set_ext_l2 = true;
- qio->ext->allocated_clu_pos = map->data_clu_pos;
- }
- qio->ext->cow_mask = calc_cow_mask(qcow2, map->ext_l2, qio,
- true, map->clu_is_cow, false);
+ ext->new_ext_l2 = mask | map->subclus_mask;
+ ext->only_set_ext_l2 = ext->cow_in_place;
+ ext->cow_mask = calc_cow_mask(qcow2, map->ext_l2, qio,
+ true, map->clu_is_cow, false);
+ ext->cow_mask |= qio_border_zero_subclus_mask(qcow2, qio, map);
}
submit_read_sliced_clu(map, qio, sliced_cow_read_complete);
@@ -3785,14 +3826,16 @@ static int handle_metadata(struct qcow2 *qcow2, struct qio **qio,
/* COW to compressed or shared with snapshot cluster */
ret = prepare_l1l2_replace(qcow2, *qio, map);
} else if ((map->level & L2_LEVEL) &&
- qio_border_is_inside_unmapped_unit(qcow2, *qio, map) &&
- maybe_mapped_in_lower_delta(qcow2, *qio)) {
+ ((qio_border_is_inside_unmapped_unit(qcow2, *qio, map) &&
+ maybe_mapped_in_lower_delta(qcow2, *qio)) ||
+ qio_border_is_inside_zero_unit(qcow2, *qio, map))) {
/*
* Backing file is about data COW, and it is
* never about metadata COW (unlike internal
* snapshots). Here is data COW on L2_LEVEL.
+ * A zero unit COWs zeroes over its host blocks.
*/
- map->backing_file_cow = true;
+ map->sliced_cow = true;
if (delay_if_l2_dirty(qcow2, map, qio))
ret = 0;
else
@@ -3937,7 +3980,7 @@ static void process_one_qio(struct qcow2 *qcow2, struct qio *qio)
} else { /* write */
if (unlikely(map.clu_is_cow && !(map.level & L2_LEVEL)))
submit_read_whole_cow_clu(&map, qio);
- else if (unlikely(map.clu_is_cow || map.backing_file_cow))
+ else if (unlikely(map.clu_is_cow || map.sliced_cow))
submit_read_sliced_cow_clu(&map, qio);
else
perform_rw_mapped(&map, qio);
@@ -4375,7 +4418,7 @@ static void process_cow_data_write(struct qcow2 *qcow2, struct list_head *cow_li
break;
ext = qio->ext;
- if (ext->only_set_ext_l2) {
+ if (ext->cow_in_place) {
WARN_ON_ONCE(ext->lx_level != L2_LEVEL);
pos = ext->allocated_clu_pos;
goto submit;
diff --git a/drivers/md/dm-qcow2.h b/drivers/md/dm-qcow2.h
index 4acddbe7c95f..c2460c2bea06 100644
--- a/drivers/md/dm-qcow2.h
+++ b/drivers/md/dm-qcow2.h
@@ -325,6 +325,7 @@ struct qio_ext {
u64 new_ext_l2;
u32 cow_mask;
bool only_set_ext_l2:1;
+ bool cow_in_place:1;
bool set_all_zeroes:1;
bool wait_writers:1;
--
2.55.0
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [Devel] [PATCH hci-8.2] dm-qcow2: fix stale host data showing in a zero cluster after a write
2026-11-23 19:13 [Devel] [PATCH hci-8.2] dm-qcow2: fix stale host data showing in a zero cluster after a write Denis V. Lunev
@ 2026-11-23 19:21 ` no-reply
2026-11-24 12:08 ` Konstantin Khorenko
1 sibling, 0 replies; 3+ messages in thread
From: no-reply @ 2026-11-23 19:21 UTC (permalink / raw)
To: Denis V. Lunev, andrey.zhadchenko, alexander.ivanov, vasileios.almpanis
Cc: devel
> From: Denis V. Lunev <den@openvz.org>
>
> An L2 entry with the zero flag may keep its host cluster, and a zero
> subcluster of an extended L2 entry keeps its part of the host cluster.
> Their host blocks may hold stale data: qemu leaves it there after a
> zero write without unmap. A write that covers such a unit only in part
> drops the zero flag or bit but writes only its own range, so the rest
> of the unit reads back the stale data instead of zeroes.
> prepare_l1l2_allocation() reuses the host cluster as it is, and the
> backing file COW of a write whose other border is in an unallocated
> subcluster copies only that subcluster.
>
> Send such a write through the sliced COW, as a write with a border in
> an unallocated unit over a backing image already goes. The slices of a
> zero unit come from the map as zeroes, with no read from disk, and are
> written into the host cluster in place together with the data, so the
> cluster stays where it is. map->backing_file_cow becomes sliced_cow,
> and qio_ext gets cow_in_place for the non-extended case, where the L2
> entry is still rewritten. The first partial write into such a unit now
> writes the whole unit; writes into other units take the path they did.
>
> https://virtuozzo.atlassian.net/browse/VSTOR-156774
>
> Fixes: 222b6f1291c1 ("dm-qcow2: Introduce driver to create block devices over QCOW2 files")
> Feature: dm-qcow2: block device over QCOW2 files driver
> Signed-off-by: Denis V. Lunev <den@openvz.org>
Sashiko has reviewed this patch and found no issues.
Carry the trailer below into the next revision and Sashiko will not review this patch again.
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
--
Sashiko AI review · https://sashiko.vzint.dev/#/patchset/20261123191317.199611-1-den@openvz.org?part=1
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [Devel] [PATCH hci-8.2] dm-qcow2: fix stale host data showing in a zero cluster after a write
2026-11-23 19:13 [Devel] [PATCH hci-8.2] dm-qcow2: fix stale host data showing in a zero cluster after a write Denis V. Lunev
2026-11-23 19:21 ` no-reply
@ 2026-11-24 12:08 ` Konstantin Khorenko
1 sibling, 0 replies; 3+ messages in thread
From: Konstantin Khorenko @ 2026-11-24 12:08 UTC (permalink / raw)
To: Denis V. Lunev; +Cc: devel
I won't apply this version, please send v2 with the changes below.
On Mon, 23 Nov 2026 20:13:17 +0100, Denis V. Lunev wrote:
> dm-qcow2: fix stale host data showing in a zero cluster after a write
- Cover an unallocated subcluster of an allocated cluster too, when
there is no backing image or the write is past its end: it shows the
same stale data.
- Share one border-mask helper with
qio_border_is_inside_unmapped_unit() instead of a third copy of the
border test.
- Keep the unchanged new_ext_l2 and calc_cow_mask() lines as they were.
- Commit message: drop the sentence that retells the rename, say why
cow_in_place is needed besides only_set_ext_l2, reword "whose other
border".
The zero unit part is right: the zero slices come from the map with no
disk read, the cluster is written in place, and on a non-extended image
the L2 entry is rewritten only after the data is on disk.
> - qio_border_is_inside_unmapped_unit(qcow2, *qio, map) &&
> - maybe_mapped_in_lower_delta(qcow2, *qio)) {
> + ((qio_border_is_inside_unmapped_unit(qcow2, *qio, map) &&
> + maybe_mapped_in_lower_delta(qcow2, *qio)) ||
> + qio_border_is_inside_zero_unit(qcow2, *qio, map))) {
A subcluster with neither the alloc nor the zero bit set, in an
allocated cluster, can keep old host data as well: qemu allocates a
cluster for a subcluster write, copies only that subcluster, and may
reuse a freed cluster for it. Without a backing image
maybe_mapped_in_lower_delta() is false and the zero mask is 0, so a 4K
write into such a subcluster goes to prepare_l1l2_allocation() and the
rest of the subcluster reads back the old data instead of zeroes. qemu
copies this subcluster type whole on a partial write, like a zero one.
The sliced path already zero-fills an unmapped slice when there is no
lower image, so taking an allocated cluster here as well should be
enough.
> +/* Zero subclusters with a border of @qio inside, which keep host blocks */
> +static u32 qio_border_zero_subclus_mask(struct qcow2 *qcow2, struct qio *qio,
> + struct qcow2_map *map)
This repeats the border test of qio_border_is_inside_unmapped_unit()
(and of calc_cow_mask()): qio_subclu_indexes(), then SUBCLU_OFF() and
a bit test for each end; only the mask differs. One helper that takes
the mask would serve both, and the case above becomes a mask change.
> + ext->new_ext_l2 = mask | map->subclus_mask;
> + ext->only_set_ext_l2 = ext->cow_in_place;
> + ext->cow_mask = calc_cow_mask(qcow2, map->ext_l2, qio,
> + true, map->clu_is_cow, false);
The new_ext_l2 and calc_cow_mask() lines change only from qio->ext-> to
ext->; please leave them as they were, so the diff shows the logic
change only.
> cluster stays where it is. map->backing_file_cow becomes sliced_cow,
> and qio_ext gets cow_in_place for the non-extended case, where the L2
> entry is still rewritten.
The rename needs no sentence. What the reader needs is why cow_in_place
exists: only_set_ext_l2 cannot mark an in-place write on a non-extended
image, where the L2 entry must still be rewritten to drop the zero
flag. It is set in both modes, so "for the non-extended case" reads as
if it were set only there.
> backing file COW of a write whose other border is in an unallocated
> subcluster copies only that subcluster.
"Other" than which border? Something like "a write with one border in
an unallocated subcluster and the other in a zero subcluster" says it
directly.
--
Konstantin Khorenko,
Virtuozzo Linux Kernel Team
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-24 12:10 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-23 19:13 [Devel] [PATCH hci-8.2] dm-qcow2: fix stale host data showing in a zero cluster after a write Denis V. Lunev
2026-11-23 19:21 ` no-reply
2026-11-24 12:08 ` Konstantin Khorenko
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.