From: Andrey Zhadchenko <andrey.zhadchenko@virtuozzo.com>
To: "Denis V. Lunev" <den@openvz.org>,
alexander.ivanov@virtuozzo.com, vasileios.almpanis@virtuozzo.com
Cc: devel@openvz.org
Subject: Re: [Devel] [PATCH hci-8.2 v2] dm-qcow2: fix stale host data showing in a zero cluster after a write
Date: Wed, 25 Nov 2026 02:07:01 +0100 [thread overview]
Message-ID: <aa088997-cd4b-4534-9ed9-2f29ef883c6f@virtuozzo.com> (raw)
In-Reply-To: <20261124124613.455047-1-den@openvz.org>
On 11/24/26 13:46, Denis V. Lunev wrote:
> 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. An unallocated subcluster of an allocated
> cluster may hold stale data as well, since qemu allocates a cluster for
> a write into one subcluster and may reuse a freed one for it. A write
> that covers such a unit only in part drops the zero flag or bit, or
> sets the alloc 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 with one border in an unallocated
> subcluster and the other in a zero subcluster copies only the
> unallocated one.
>
> Send such a write through the sliced COW, as a write with a border in
> an unallocated unit over a backing image already goes; an unallocated
> subcluster is taken only when the write starts outside the backing data.
> The slices 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. only_set_ext_l2 cannot mark such an in-place
> write on an image without extended L2 entries, where the L2 entry must
> still be rewritten to drop the zero flag, so qio_ext gets cow_in_place,
> set in both modes.
>
> As in qemu, the first partial write into such a unit now writes the
> whole unit. On an image with extended L2 entries and no backing data
> this includes the first partial write into each subcluster of a cluster
> dm-qcow2 allocated and zeroed itself, which the driver cannot tell from
> one allocated by qemu.
>
> 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>
> ---
> v2: also cover an unallocated subcluster of an allocated cluster without
> backing data, share the border mask helper with
> qio_border_is_inside_unmapped_unit(), leave the unchanged lines alone
> and reword the message, per Konstantin's review.
> v1: http://lore.virtuozzo.com/kernel/20261123191317.199611-1-den@openvz.org/
>
> drivers/md/dm-qcow2-map.c | 87 ++++++++++++++++++++++++++++++---------
> drivers/md/dm-qcow2.h | 1 +
> 2 files changed, 69 insertions(+), 19 deletions(-)
>
> diff --git a/drivers/md/dm-qcow2-map.c b/drivers/md/dm-qcow2-map.c
> index 853e7a1230ee..2ab14fd939c8 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;
> @@ -1756,14 +1756,28 @@ static u32 qio_all_zeroes_size(struct qcow2 *qcow2, struct qio *qio,
> return qio_subclus_covered_start_size(qcow2, qio, map->ext_l2 >> 32);
> }
>
> +/* Subclusters of @mask that a border of @qio falls inside */
> +static u32 qio_border_subclus_mask(struct qcow2 *qcow2, struct qio *qio,
> + u32 mask)
> +{
> + u64 start_off = bytes_off_in_cluster(qcow2, qio);
> + u64 end_off = start_off + qio->bi_iter.bi_size;
> + u8 start_bit, end_bit;
> + u32 ret = 0;
> +
> + start_bit = qio_subclu_indexes(qcow2, qio, &end_bit);
> + if (SUBCLU_OFF(qcow2, start_off) && (mask & (1 << start_bit)))
> + ret |= 1 << start_bit;
nit: explicit 1U is better here
> + if (SUBCLU_OFF(qcow2, end_off) && (mask & (1 << end_bit)))
> + ret |= 1 << end_bit;
> + return ret;
> +}
> +
> static bool qio_border_is_inside_unmapped_unit(struct qcow2 *qcow2,
> struct qio *qio,
> struct qcow2_map *map)
> {
> - u64 start_off, end_off;
> - u8 start_bit, end_bit;
> u32 mapped_mask;
> - bool ret;
>
> if (WARN_ON_ONCE(!(map->level & L2_LEVEL)))
> return false;
> @@ -1774,14 +1788,44 @@ static bool qio_border_is_inside_unmapped_unit(struct qcow2 *qcow2,
> if (!qcow2->ext_l2)
> return !map->data_clu_alloced && !map->all_zeroes;
>
> - start_bit = qio_subclu_indexes(qcow2, qio, &end_bit);
> - start_off = bytes_off_in_cluster(qcow2, qio);
> - end_off = start_off + qio->bi_iter.bi_size;
> mapped_mask = (u32)map->ext_l2 | (map->ext_l2 >> 32);
> + return qio_border_subclus_mask(qcow2, qio, ~mapped_mask) != 0;
> +}
>
> - ret = SUBCLU_OFF(qcow2, start_off) != 0 && ((1 << start_bit) & ~mapped_mask);
> - ret |= SUBCLU_OFF(qcow2, end_off) != 0 && ((1 << end_bit) & ~mapped_mask);
> - return ret;
> +/*
> + * Subclusters of an allocated cluster with a border of @qio inside, which
> + * read as zeroes over host blocks: zero ones, and unallocated ones when
> + * @qio starts outside the backing data.
> + */
> +static u32 qio_border_stale_subclus_mask(struct qcow2 *qcow2, struct qio *qio,
> + struct qcow2_map *map)
> +{
> + u32 zeroes = map->ext_l2 >> 32;
> + u32 mask = zeroes;
> +
> + if (!map->data_clu_alloced || map->clu_is_cow)
> + return 0;
> + if (!maybe_mapped_in_lower_delta(qcow2, qio))
> + mask |= ~((u32)map->ext_l2 | zeroes);
I think this is unneeded. We can just use ~(u32)map->ext_l2 as mask in
qio_border_subclus_mask():
- In submit_read_sliced_cow_clu(), calc_cow_mask(..., true, ...)
already sets unallocated border subclusters unconditionally
- In handle_metadata(), when maybe_mapped_in_lower_delta() is true,
unallocated border subclusters are already matched by
qio_border_is_inside_unmapped_unit() && maybe_mapped_in_lower_delta()
(which is also what catches a qio that starts < lower->hdr.size in an
allocated subcluster and ends > lower->hdr.size inside an unallocated
subcluster)
> + return qio_border_subclus_mask(qcow2, qio, mask);
> +}
> +
> +/*
> + * A zero cluster, or a zero or unallocated subcluster of an allocated
> + * cluster, 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_stale_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_stale_subclus_mask(qcow2, qio, map) != 0;
> }
>
> static bool qio_is_fully_alloced(struct qcow2 *qcow2, struct qio *qio,
> @@ -3563,15 +3607,18 @@ static void submit_read_sliced_cow_clu(struct qcow2_map *map, struct qio *qio)
>
> WARN_ON_ONCE(!(map->level & L2_LEVEL));
>
> + if (map->data_clu_alloced && !map->clu_is_cow) {
> + qio->ext->cow_in_place = true;
> + qio->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->only_set_ext_l2 = qio->ext->cow_in_place;
With this patch, every first partial write into a subcluster of an
allocated ext_l2 cluster sets ext->only_set_ext_l2 = true. In
process_indexes_write() WARN_ON_ONCE(ret < 0) assumed the first
prepare_l_entry_update() already allocated lx_md->wbd and wbd->pe_page.
When ext->only_set_ext_l2 jumps to set_ext_l2:, that call is the first
one and can return -ENOMEM (alloc_wbd() / alloc_page()), which then
warns and either NULL-derefs lx_md->wbd or completes qio without
updating ext_l2.
Would be nice if you fix it as a separate patch.
> qio->ext->cow_mask = calc_cow_mask(qcow2, map->ext_l2, qio,
> true, map->clu_is_cow, false);
> + qio->ext->cow_mask |= qio_border_stale_subclus_mask(qcow2, qio, map);
> }
>
> submit_read_sliced_clu(map, qio, sliced_cow_read_complete);
> @@ -3785,14 +3832,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_stale_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 stale 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 +3986,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 +4424,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;
copy-on-write-in-place sounds oxymoronic. I think you can remove this
and just check ext->allocated_clu_pos as it is set only from
submit_read_sliced_cow_clu.
Or at least rename this to something more appropriate like 'reuse_clu'
> bool set_all_zeroes:1;
> bool wait_writers:1;
>
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
next prev parent reply other threads:[~2026-11-25 1:08 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 12:46 Denis V. Lunev
2026-11-24 13:16 ` Konstantin Khorenko
2026-11-25 1:07 ` Andrey Zhadchenko [this message]
2026-12-01 0:06 ` no-reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aa088997-cd4b-4534-9ed9-2f29ef883c6f@virtuozzo.com \
--to=andrey.zhadchenko@virtuozzo.com \
--cc=alexander.ivanov@virtuozzo.com \
--cc=den@openvz.org \
--cc=devel@openvz.org \
--cc=vasileios.almpanis@virtuozzo.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.