* [Devel] [PATCH hci-8.2] dm-qcow2: report the unit L2 can unmap as discard granularity
@ 2026-11-25 11:16 Vasileios Anagnostoulis
2026-11-25 18:17 ` Denis V. Lunev
2026-12-01 0:13 ` no-reply
0 siblings, 2 replies; 3+ messages in thread
From: Vasileios Anagnostoulis @ 2026-11-25 11:16 UTC (permalink / raw)
To: Konstantin Khorenko, Denis V. Lunev; +Cc: devel
qcow2_io_hints() reports the block size of the host file system as
discard_granularity and one cluster as max_hw_discard_sectors.
bio_split_discard() cuts a longer discard into pieces of
max_discard_sectors and aligns each split point only to
discard_granularity, i.e. to 4K. A discard that does not start on a
cluster boundary is therefore cut at the same offset inside every
cluster it spans.
dm-qcow2 unmaps only a subcluster (with extended_l2) or a cluster
(without it) that a single discard qio covers entirely. The unit under
each split point is covered by two qios, each only in part, so neither
unmaps it: its host blocks are punched, but it stays allocated in L2
and the cluster stays allocated in the refcount table. Every long
discard leaves such a unit behind in each cluster it spans, whether it
comes from the FITRIM of ploop-balloon discard or from fstrim inside a
Container.
On a 512M image with 1M clusters and extended_l2, one FITRIM after
freeing half of ext4 left 83 clusters that ext4 no longer uses mapped,
each with exactly one 32K subcluster still allocated.
Report the unit L2 can unmap as discard_granularity: the subcluster
with extended_l2, the cluster otherwise. bio_split_discard() then cuts
discards on unit boundaries. dm-ploop already reports its cluster.
https://virtuozzo.atlassian.net/browse/VSTOR-154496
Fixes: 2a728299abdb ("dm-qcow2: Introduce driver to create block devices over QCOW2 files")
Feature: dm-qcow2: block device over QCOW2 files driver
Signed-off-by: Vasileios Anagnostoulis <vasileios.anagnostoulis@virtuozzo.com>
---
Tested on 6.12.0-211.75.1.24.1.vz10, with dm-qcow2 rebuilt from the
rh10-6.12.0-211.75.1.24.1.vz10 sources plus this patch and loaded in
place of the stock module. qcow2_io_hints() and the discard path are
the same there as on hci-8.2. Ploop test suite (ploop.ovz, test/):
# python3 -m pytest tests/test_ploop_balloon.py -k shrunk_offline \
--format qcow2 --bug-id VSTOR-154496
test_ploop_balloon_discard_should_release_removed_data_when_image_shrunk_offline PASSED
This is the reproducer from VSTOR-154496 (512M image, 1M clusters,
extended_l2). Without the fix, 83 clusters that ext4 no longer used
stayed mapped after the FITRIM.
# python3 -m pytest tests/test_ploop_balloon.py --format qcow2
33 passed, 23 deselected
The reproducer is among the deselected tests: the suite runs it only
with --bug-id, as above.
---
drivers/md/dm-qcow2-target.c | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/drivers/md/dm-qcow2-target.c b/drivers/md/dm-qcow2-target.c
index d445bfe1232a..a343266fb1be 100644
--- a/drivers/md/dm-qcow2-target.c
+++ b/drivers/md/dm-qcow2-target.c
@@ -1117,10 +1117,15 @@ static void qcow2_io_hints(struct dm_target *ti, struct queue_limits *limits)
if (sb->s_bdev)
block_size = bdev_logical_block_size(sb->s_bdev);
/*
- * Even if this is less than discard_granularity of bdev,
- * we can free a block on filesystem.
+ * Report the unit L2 can unmap: the subcluster with extended L2,
+ * the cluster otherwise. The block layer aligns discard split
+ * points to this granularity only; with a smaller one, a long
+ * discard that starts mid-cluster is split at the same offset
+ * inside every cluster, and the (sub)cluster under each split is
+ * covered only in part by both pieces, so neither unmaps it.
*/
- limits->discard_granularity = sb->s_blocksize;
+ limits->discard_granularity = qcow2->ext_l2 ? qcow2->subclu_size :
+ qcow2->clu_size;
limits->max_hw_discard_sectors = to_sector(qcow2->clu_size);
limits->max_discard_segments = 1;
---
base-commit: aef96420256e57bf1291859a844800a462f2790e
change-id: 20261125-br-vstor-154496-42580b50cfb2
Best regards,
--
Vasileios Anagnostoulis <vasileios.anagnostoulis@virtuozzo.com>
_______________________________________________
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: report the unit L2 can unmap as discard granularity
2026-11-25 11:16 [Devel] [PATCH hci-8.2] dm-qcow2: report the unit L2 can unmap as discard granularity Vasileios Anagnostoulis
@ 2026-11-25 18:17 ` Denis V. Lunev
2026-12-01 0:13 ` no-reply
1 sibling, 0 replies; 3+ messages in thread
From: Denis V. Lunev @ 2026-11-25 18:17 UTC (permalink / raw)
To: Vasileios Anagnostoulis, Konstantin Khorenko, Denis V. Lunev; +Cc: devel
On 11/25/26 12:16, Vasileios Anagnostoulis wrote:
> This is the first email you've received from this external sender.
> Do not click links or open attachments unless it is an email you expected to receive.
> qcow2_io_hints() reports the block size of the host file system as
> discard_granularity and one cluster as max_hw_discard_sectors.
> bio_split_discard() cuts a longer discard into pieces of
> max_discard_sectors and aligns each split point only to
> discard_granularity, i.e. to 4K. A discard that does not start on a
> cluster boundary is therefore cut at the same offset inside every
> cluster it spans.
>
> dm-qcow2 unmaps only a subcluster (with extended_l2) or a cluster
> (without it) that a single discard qio covers entirely. The unit under
> each split point is covered by two qios, each only in part, so neither
> unmaps it: its host blocks are punched, but it stays allocated in L2
> and the cluster stays allocated in the refcount table. Every long
> discard leaves such a unit behind in each cluster it spans, whether it
> comes from the FITRIM of ploop-balloon discard or from fstrim inside a
> Container.
>
> On a 512M image with 1M clusters and extended_l2, one FITRIM after
> freeing half of ext4 left 83 clusters that ext4 no longer uses mapped,
> each with exactly one 32K subcluster still allocated.
>
> Report the unit L2 can unmap as discard_granularity: the subcluster
> with extended_l2, the cluster otherwise. bio_split_discard() then cuts
> discards on unit boundaries. dm-ploop already reports its cluster.
>
> https://virtuozzo.atlassian.net/browse/VSTOR-154496
>
> Fixes: 2a728299abdb ("dm-qcow2: Introduce driver to create block devices over QCOW2 files")
> Feature: dm-qcow2: block device over QCOW2 files driver
> Signed-off-by: Vasileios Anagnostoulis <vasileios.anagnostoulis@virtuozzo.com>
> ---
> Tested on 6.12.0-211.75.1.24.1.vz10, with dm-qcow2 rebuilt from the
> rh10-6.12.0-211.75.1.24.1.vz10 sources plus this patch and loaded in
> place of the stock module. qcow2_io_hints() and the discard path are
> the same there as on hci-8.2. Ploop test suite (ploop.ovz, test/):
>
> # python3 -m pytest tests/test_ploop_balloon.py -k shrunk_offline \
> --format qcow2 --bug-id VSTOR-154496
> test_ploop_balloon_discard_should_release_removed_data_when_image_shrunk_offline PASSED
>
> This is the reproducer from VSTOR-154496 (512M image, 1M clusters,
> extended_l2). Without the fix, 83 clusters that ext4 no longer used
> stayed mapped after the FITRIM.
>
> # python3 -m pytest tests/test_ploop_balloon.py --format qcow2
> 33 passed, 23 deselected
>
> The reproducer is among the deselected tests: the suite runs it only
> with --bug-id, as above.
> ---
> drivers/md/dm-qcow2-target.c | 11 ++++++++---
> 1 file changed, 8 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/md/dm-qcow2-target.c b/drivers/md/dm-qcow2-target.c
> index d445bfe1232a..a343266fb1be 100644
> --- a/drivers/md/dm-qcow2-target.c
> +++ b/drivers/md/dm-qcow2-target.c
> @@ -1117,10 +1117,15 @@ static void qcow2_io_hints(struct dm_target *ti, struct queue_limits *limits)
> if (sb->s_bdev)
> block_size = bdev_logical_block_size(sb->s_bdev);
> /*
> - * Even if this is less than discard_granularity of bdev,
> - * we can free a block on filesystem.
> + * Report the unit L2 can unmap: the subcluster with extended L2,
> + * the cluster otherwise. The block layer aligns discard split
> + * points to this granularity only; with a smaller one, a long
> + * discard that starts mid-cluster is split at the same offset
> + * inside every cluster, and the (sub)cluster under each split is
> + * covered only in part by both pieces, so neither unmaps it.
> */
> - limits->discard_granularity = sb->s_blocksize;
> + limits->discard_granularity = qcow2->ext_l2 ? qcow2->subclu_size :
> + qcow2->clu_size;
> limits->max_hw_discard_sectors = to_sector(qcow2->clu_size);
> limits->max_discard_segments = 1;
>
>
> ---
> base-commit: aef96420256e57bf1291859a844800a462f2790e
> change-id: 20261125-br-vstor-154496-42580b50cfb2
>
> Best regards,
run /kslop over this please :-)
_______________________________________________
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: report the unit L2 can unmap as discard granularity
2026-11-25 11:16 [Devel] [PATCH hci-8.2] dm-qcow2: report the unit L2 can unmap as discard granularity Vasileios Anagnostoulis
2026-11-25 18:17 ` Denis V. Lunev
@ 2026-12-01 0:13 ` no-reply
1 sibling, 0 replies; 3+ messages in thread
From: no-reply @ 2026-12-01 0:13 UTC (permalink / raw)
To: Vasileios Anagnostoulis, Konstantin Khorenko, Denis V. Lunev; +Cc: devel
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] dm-qcow2: larger discard_granularity stops fstrim reclaiming
sub-unit extents
--
commit f99effe256258fbc884b886dfde94f2336c8f3b2
Author: Vasileios Anagnostoulis <vasileios.anagnostoulis@virtuozzo.com>
dm-qcow2: report the unit L2 can unmap as discard granularity
The patch changes qcow2_io_hints() to report the L2 unmap unit as discard_granularity. That unit is the subcluster with extended_l2 and the cluster otherwise. Before, it reported the host filesystem block size.
> diff --git a/drivers/md/dm-qcow2-target.c b/drivers/md/dm-qcow2-target.c
> index d445bfe1232a6..a343266fb1beb 100644
> --- a/drivers/md/dm-qcow2-target.c
> +++ b/drivers/md/dm-qcow2-target.c
> @@ -1117,10 +1117,15 @@ static void qcow2_io_hints(struct dm_target *ti, struct queue_limits *limits)
> if (sb->s_bdev)
> block_size = bdev_logical_block_size(sb->s_bdev);
> /*
> - * Even if this is less than discard_granularity of bdev,
> - * we can free a block on filesystem.
> + * Report the unit L2 can unmap: the subcluster with extended L2,
> + * the cluster otherwise. The block layer aligns discard split
> + * points to this granularity only; with a smaller one, a long
> + * discard that starts mid-cluster is split at the same offset
> + * inside every cluster, and the (sub)cluster under each split is
> + * covered only in part by both pieces, so neither unmaps it.
> */
> - limits->discard_granularity = sb->s_blocksize;
> + limits->discard_granularity = qcow2->ext_l2 ? qcow2->subclu_size :
> + qcow2->clu_size;
[Severity: Low]
Does this change drop host block reclaim for free extents smaller than the new granularity?
discard_granularity is also the minimum trim size that filesystems use. For example, ext4_trim_fs() raises minlen to the granularity, so free extents shorter than one cluster (or one subcluster with extended_l2) no longer reach dm-qcow2. Before the patch, such extents reached issue_discard(). It punched the host blocks even though L2 was not updated. With 1M clusters and no extended_l2, free runs under 1M are no longer reclaimed on the host.
The removed comment said the 4K granularity was deliberate ("we can free a block on filesystem"). Neither the new comment nor the commit message mentions this cost. Could they state that the trade-off is intended?
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/20261125-br-vstor-154496-v1-1-dd8d86661647@virtuozzo.com?part=1
_______________________________________________
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-12-01 0:14 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-25 11:16 [Devel] [PATCH hci-8.2] dm-qcow2: report the unit L2 can unmap as discard granularity Vasileios Anagnostoulis
2026-11-25 18:17 ` Denis V. Lunev
2026-12-01 0:13 ` no-reply
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox