* [PATCH hci-8.2 v2] vhost-blk: advertise write zeroes with the backend limit
@ 2026-11-20 13:23 Alexander Ivanov
2026-11-20 13:26 ` no-reply
2026-11-23 16:33 ` Andrey Drobyshev
0 siblings, 2 replies; 5+ messages in thread
From: Alexander Ivanov @ 2026-11-20 13:23 UTC (permalink / raw)
To: andrey.zhadchenko; +Cc: andrey.drobyshev, svt-core
vhost-blk in the kernel now handles VIRTIO_BLK_T_WRITE_ZEROES. It sends
each request to the backend as a single WRITE ZEROES bio and does not
fall back to writing zero pages, so the feature only works on a backend
that supports WRITE ZEROES and for a range that fits in one bio.
With the write-zeroes property on, read write_zeroes_max_bytes of a
writable backend from sysfs and advertise it as max_write_zeroes_sectors,
capped to what one bio can carry. A partition has no queue/ directory
of its own, so read the limit of the whole disk, whose queue it shares.
Do not offer VIRTIO_BLK_F_WRITE_ZEROES when the backend reports no limit,
the limit cannot be read, or the backend is opened read-only.
The property stays off by default. A kernel without write zeroes
support in vhost-blk does not offer the feature, so it is not
negotiated there.
https://virtuozzo.atlassian.net/browse/VSTOR-141684
Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
---
v2:
- read write_zeroes_max_bytes of the whole disk for a partition, which
has no queue/ directory of its own.
- do not advertise the feature and warn when the limit cannot be read.
hw/block/vhost-blk.c | 45 ++++++++++++++++++++++++++++++++++-
include/hw/virtio/vhost-blk.h | 1 +
2 files changed, 45 insertions(+), 1 deletion(-)
diff --git a/hw/block/vhost-blk.c b/hw/block/vhost-blk.c
index abddff02c6d..153cc7604d0 100644
--- a/hw/block/vhost-blk.c
+++ b/hw/block/vhost-blk.c
@@ -521,6 +521,30 @@ static bool vhost_blk_uevent_attach(VHostBlk *s, Error **errp)
return true;
}
+static int vhost_blk_get_write_zeroes_sectors(VHostBlk *s)
+{
+ g_autofree char *dev = NULL;
+ g_autofree char *part = NULL;
+ g_autofree char *path = NULL;
+ g_autofree char *val = NULL;
+ uint64_t bytes;
+
+ dev = g_strdup_printf("/sys/dev/block/%u:%u",
+ major(s->backend_rdev), minor(s->backend_rdev));
+ part = g_strdup_printf("%s/partition", dev);
+ path = g_strdup_printf("%s/%squeue/write_zeroes_max_bytes", dev,
+ g_file_test(part, G_FILE_TEST_EXISTS) ? "../" : "");
+ if (!g_file_get_contents(path, &val, NULL, NULL) ||
+ qemu_strtou64(g_strstrip(val), NULL, 10, &bytes)) {
+ return -1;
+ }
+
+ /* The kernel issues the whole range as one bio */
+ s->max_write_zeroes_sectors = MIN(bytes >> BDRV_SECTOR_BITS,
+ UINT32_MAX >> BDRV_SECTOR_BITS);
+ return 0;
+}
+
static bool vhost_blk_open_backend(VHostBlk *s, Error **errp)
{
ERRP_GUARD();
@@ -562,6 +586,16 @@ static bool vhost_blk_open_backend(VHostBlk *s, Error **errp)
s->conf.devpath);
goto fail;
}
+
+ if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_WRITE_ZEROES)) {
+ if (vhost_blk_get_write_zeroes_sectors(s)) {
+ warn_report("vhost-blk: unable to read write zeroes limit "
+ "of '%s'", s->conf.devpath);
+ } else if (!s->max_write_zeroes_sectors) {
+ warn_report("vhost-blk: '%s' does not support write zeroes",
+ s->conf.devpath);
+ }
+ }
}
vhost_blk_update_size(s, errp);
@@ -722,6 +756,9 @@ static uint64_t vhost_blk_get_features(VirtIODevice *vdev,
uint64_t res;
features |= s->host_features;
+ if (!s->max_write_zeroes_sectors) {
+ virtio_clear_feature(&features, VIRTIO_BLK_F_WRITE_ZEROES);
+ }
virtio_add_feature(&features, VIRTIO_BLK_F_BLK_SIZE);
virtio_add_feature(&features, VIRTIO_BLK_F_SEG_MAX);
@@ -775,6 +812,12 @@ static void vhost_blk_update_config(VirtIODevice *vdev, uint8_t *config)
blkcfg.physical_block_exp = get_physical_block_exp(conf);
blkcfg.alignment_offset = 0;
virtio_stw_p(vdev, &blkcfg.num_queues, s->conf.num_queues);
+ if (s->max_write_zeroes_sectors) {
+ virtio_stl_p(vdev, &blkcfg.max_write_zeroes_sectors,
+ s->max_write_zeroes_sectors);
+ blkcfg.write_zeroes_may_unmap = 1;
+ virtio_stl_p(vdev, &blkcfg.max_write_zeroes_seg, 1);
+ }
memcpy(config, &blkcfg, sizeof(struct virtio_blk_config));
}
@@ -789,7 +832,7 @@ static const Property vhost_blk_properties[] = {
DEFINE_PROP_UINT16("num-threads", VHostBlk, conf.num_threads, 1),
DEFINE_PROP_BIT64("flush", VHostBlk, host_features,
VIRTIO_BLK_F_FLUSH, true),
-/* Discard and write-zeroes not yet implemented in kernel module */
+/* Discard not yet implemented in kernel module */
DEFINE_PROP_BIT64("discard", VHostBlk, host_features,
VIRTIO_BLK_F_DISCARD, false),
DEFINE_PROP_BIT64("write-zeroes", VHostBlk, host_features,
diff --git a/include/hw/virtio/vhost-blk.h b/include/hw/virtio/vhost-blk.h
index 059f1ebc701..e4bcbe49127 100644
--- a/include/hw/virtio/vhost-blk.h
+++ b/include/hw/virtio/vhost-blk.h
@@ -46,6 +46,7 @@ typedef struct VHostBlk {
bool vhost_started;
uint64_t length;
uint64_t backend_rdev;
+ uint32_t max_write_zeroes_sectors;
QEMUBH *resize_bh;
Error *cpr_blocker;
} VHostBlk;
--
2.53.0
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH hci-8.2 v2] vhost-blk: advertise write zeroes with the backend limit
2026-11-20 13:23 [PATCH hci-8.2 v2] vhost-blk: advertise write zeroes with the backend limit Alexander Ivanov
@ 2026-11-20 13:26 ` no-reply
2026-11-20 13:34 ` Alexander Ivanov
2026-11-23 16:33 ` Andrey Drobyshev
1 sibling, 1 reply; 5+ messages in thread
From: no-reply @ 2026-11-20 13:26 UTC (permalink / raw)
To: Alexander Ivanov, andrey.zhadchenko; +Cc: andrey.drobyshev, svt-core
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] vhost-blk: write_zeroes_may_unmap=1 advertised without
justification
--
commit e9ba7478b40c6be915b18d378891abd29c85cbb5
Author: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
vhost-blk: advertise write zeroes with the backend limit
When the write-zeroes property is on, this reads write_zeroes_max_bytes of a writable backend from sysfs. It advertises that value as max_write_zeroes_sectors, capped to what one bio can carry. The feature is not offered when the backend reports no limit, the limit cannot be read, or the backend is read-only.
> diff --git a/hw/block/vhost-blk.c b/hw/block/vhost-blk.c
> index 8ecb6a441e434..3e5150db4a879 100644
> --- a/hw/block/vhost-blk.c
> +++ b/hw/block/vhost-blk.c
[ ... ]
> @@ -772,6 +809,12 @@ static void vhost_blk_update_config(VirtIODevice *vdev, uint8_t *config)
> blkcfg.physical_block_exp = get_physical_block_exp(conf);
> blkcfg.alignment_offset = 0;
> virtio_stw_p(vdev, &blkcfg.num_queues, s->conf.num_queues);
> + if (s->max_write_zeroes_sectors) {
> + virtio_stl_p(vdev, &blkcfg.max_write_zeroes_sectors,
> + s->max_write_zeroes_sectors);
> + blkcfg.write_zeroes_may_unmap = 1;
> + virtio_stl_p(vdev, &blkcfg.max_write_zeroes_seg, 1);
> + }
[Severity: Low]
This isn't a bug, but the commit message does not mention write_zeroes_may_unmap or max_write_zeroes_seg. Does the kernel vhost-blk write-zeroes path permit unmapping, so that always setting write_zeroes_may_unmap = 1 is correct? The message explains the single bio per request, which motivates max_write_zeroes_seg = 1. It would help to state both choices and the reason for each.
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/20261120132317.3867152-1-alexander.ivanov@virtuozzo.com?part=1
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH hci-8.2 v2] vhost-blk: advertise write zeroes with the backend limit
2026-11-20 13:26 ` no-reply
@ 2026-11-20 13:34 ` Alexander Ivanov
0 siblings, 0 replies; 5+ messages in thread
From: Alexander Ivanov @ 2026-11-20 13:34 UTC (permalink / raw)
To: no-reply, andrey.zhadchenko; +Cc: andrey.drobyshev, svt-core
> [Severity: Low]
> This isn't a bug, but the commit message does not mention write_zeroes_may_unmap or max_write_zeroes_seg. Does the kernel vhost-blk write-zeroes path permit unmapping, so that always setting write_zeroes_may_unmap = 1 is correct? The message explains the single bio per request, which motivates max_write_zeroes_seg = 1. It would help to state both choices and the reason for each.
>
> Carry the trailer below into the next revision and Sashiko will not review this patch again.
>
> Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
Both match virtio-blk and the kernel side, which takes one segment and
honours UNMAP. Leaving the message as is.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH hci-8.2 v2] vhost-blk: advertise write zeroes with the backend limit
2026-11-20 13:23 [PATCH hci-8.2 v2] vhost-blk: advertise write zeroes with the backend limit Alexander Ivanov
2026-11-20 13:26 ` no-reply
@ 2026-11-23 16:33 ` Andrey Drobyshev
2026-11-24 13:32 ` Alexander Ivanov
1 sibling, 1 reply; 5+ messages in thread
From: Andrey Drobyshev @ 2026-11-23 16:33 UTC (permalink / raw)
To: alexander.ivanov; +Cc: andrey.zhadchenko, svt-core
On Fri, Nov 20, 2026 at 14:23:17 +0100, Alexander Ivanov wrote:
> vhost-blk in the kernel now handles VIRTIO_BLK_T_WRITE_ZEROES. It sends
> each request to the backend as a single WRITE ZEROES bio and does not
> fall back to writing zero pages, so the feature only works on a backend
> that supports WRITE ZEROES and for a range that fits in one bio.
>
> With the write-zeroes property on, read write_zeroes_max_bytes of a
> writable backend from sysfs and advertise it as max_write_zeroes_sectors,
> capped to what one bio can carry. A partition has no queue/ directory
> of its own, so read the limit of the whole disk, whose queue it shares.
> Do not offer VIRTIO_BLK_F_WRITE_ZEROES when the backend reports no limit,
> the limit cannot be read, or the backend is opened read-only.
>
> The property stays off by default. A kernel without write zeroes
> support in vhost-blk does not offer the feature, so it is not
> negotiated there.
>
> https://virtuozzo.atlassian.net/browse/VSTOR-141684
>
> Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
> ---
> v2:
> - read write_zeroes_max_bytes of the whole disk for a partition, which
> has no queue/ directory of its own.
> - do not advertise the feature and warn when the limit cannot be read.
>
> hw/block/vhost-blk.c | 45 ++++++++++++++++++++++++++++++++++-
> include/hw/virtio/vhost-blk.h | 1 +
> 2 files changed, 45 insertions(+), 1 deletion(-)
>
> diff --git a/hw/block/vhost-blk.c b/hw/block/vhost-blk.c
> index abddff02c6d..153cc7604d0 100644
> --- a/hw/block/vhost-blk.c
> +++ b/hw/block/vhost-blk.c
> @@ -521,6 +521,30 @@ static bool vhost_blk_uevent_attach(VHostBlk *s, Error **errp)
> return true;
> }
>
> +static int vhost_blk_get_write_zeroes_sectors(VHostBlk *s)
> +{
> + g_autofree char *dev = NULL;
> + g_autofree char *part = NULL;
> + g_autofree char *path = NULL;
> + g_autofree char *val = NULL;
> + uint64_t bytes;
> +
> + dev = g_strdup_printf("/sys/dev/block/%u:%u",
> + major(s->backend_rdev), minor(s->backend_rdev));
> + part = g_strdup_printf("%s/partition", dev);
> + path = g_strdup_printf("%s/%squeue/write_zeroes_max_bytes", dev,
> + g_file_test(part, G_FILE_TEST_EXISTS) ? "../" : "");
> + if (!g_file_get_contents(path, &val, NULL, NULL) ||
> + qemu_strtou64(g_strstrip(val), NULL, 10, &bytes)) {
> + return -1;
> + }
(6) [nit] Bare -1 return, result stored in *s as a side effect, GError
from g_file_get_contents() dropped. The caller cannot say whether the
file is missing, unreadable or unparsable. Usual shape:
static bool vhost_blk_get_write_zeroes_sectors(VHostBlk *s,
uint32_t *sectors,
Error **errp)
with the caller reporting via warn_report_err(), or propagating the
error, see (2).
> +
> + /* The kernel issues the whole range as one bio */
> + s->max_write_zeroes_sectors = MIN(bytes >> BDRV_SECTOR_BITS,
> + UINT32_MAX >> BDRV_SECTOR_BITS);
(4) [low] The limit is not rounded down to the logical block size of
the backend. The kernel commit requires it:
The VMM must advertise the feature only for a backend with WRITE
ZEROES support and set max_write_zeroes_sectors from its limit,
capped at 8388607 so that the range fits in one bio and rounded
down to the logical block size, and max_write_zeroes_seg to 1.
vhost_blk_parse_write_zeroes() fails a request with
(sector | nr_sects) & bs_mask. 8388607 is odd, so whenever the cap
applies on a backend with a 4K logical block size, a guest that chunks
a large range at the advertised limit gets VIRTIO_BLK_S_IOERR on every
chunk. The cap applies to common limits: loop and nbd report exactly
4294966784 bytes. dm-qcow2 takes its logical block size from the block
device under the image's filesystem (qcow2_io_hints()), so a 4Kn host
disk gives 4096.
logical_block_size is still hard-coded to BDRV_SECTOR_SIZE for the
guest, so 4Kn backends have other problems, but this part is cheap:
int lbs;
if (ioctl(s->backend_fd, BLKSSZGET, &lbs) < 0) {
...
}
sectors = MIN(bytes >> BDRV_SECTOR_BITS,
UINT32_MAX >> BDRV_SECTOR_BITS);
sectors = QEMU_ALIGN_DOWN(sectors, lbs >> BDRV_SECTOR_BITS);
> + return 0;
> +}
> +
> static bool vhost_blk_open_backend(VHostBlk *s, Error **errp)
> {
> ERRP_GUARD();
> @@ -562,6 +586,16 @@ static bool vhost_blk_open_backend(VHostBlk *s, Error **errp)
> s->conf.devpath);
> goto fail;
> }
> +
> + if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_WRITE_ZEROES)) {
> + if (vhost_blk_get_write_zeroes_sectors(s)) {
> + warn_report("vhost-blk: unable to read write zeroes limit "
> + "of '%s'", s->conf.devpath);
> + } else if (!s->max_write_zeroes_sectors) {
> + warn_report("vhost-blk: '%s' does not support write zeroes",
> + s->conf.devpath);
> + }
> + }
(1) [major] The limit is sampled once here, but the table of the open
device is replaced at runtime, and the kernel checks the live limit.
Every backup of a running VM does this. ploop-backup calls
ploop_tg_init(path, "push_backup", ...), which does (ploop lib/dm.c,
paraphrased):
1 dmsetup reload ploopXXX --table "0 ... error"
2 dmsetup create ploopXXX.push_backup ... qcow2 ...
3 dmsetup reload ploopXXX --table \
"0 ... push_backup ... /dev/mapper/ploopXXX.push_backup"
4 dmsetup resume ploopXXX
The dev_t QEMU has open stops being a qcow2 target and becomes
push_backup stacked over a new dm-qcow2 device. dm-push-backup sets
only num_discard_bios (same for dm-tracking, which ploop copy stacks
the same way), and dm_table_supports_write_zeroes() requires
num_write_zeroes_bios on every target. The limit of ploopXXX therefore
drops to 0 for the duration of the backup regardless of dm-qcow2
underneath. Any target without it shows the effect: a loop device
reports 4294966784, dm-delay on top of it reports 0.
Consequences:
a) Running guest. QEMU keeps advertising the value from realize, the
guest keeps sending WRITE ZEROES, and the kernel does
max_sects = bdev_write_zeroes_sectors(bdev);
...
if (vhost_blk_backend_ro(f) || !max_sects || ...
*status = VIRTIO_BLK_S_IOERR;
Every such request fails until the backup ends. A 6.12 guest does
not hide it: blkdev_issue_zeroout() falls back to writing zero
pages only on -EOPNOTSUPP; -EIO goes to the caller (filesystem,
mkfs, fallocate on the block device).
b) QEMU realized while the stack is in place. qemu-update re-runs
realize in the new process, reads 0, and vhost_blk_get_features()
clears a bit the guest has already negotiated. virtio_load() then
fails with "Features 0x... unsupported. Allowed features: 0x...".
With cpr-exec that is after exec, with no old process to return to.
No check for an active ploop backup was found in libvirt's
qemuDomainGuestQemuUpdate(); other callers were not searched.
QEMU cannot fix this alone. Re-reading the limit on a uevent and
raising a config change interrupt does not help: Linux virtio-blk
re-reads only the capacity (virtblk_config_changed_work()), queue
limits are set once at probe. It needs one of the following on the
kernel side before the feature is enabled:
- dm-push-backup and dm-tracking pass WRITE ZEROES through with the
limit of the underlying dm-qcow2 device, so the limit of ploopXXX is
the same before, during and after the swap; or
- vhost-blk stops failing on the live limit (the block layer already
splits an oversized WRITE ZEROES bio; a zero limit could fall back
to writing zeroes or at least return VIRTIO_BLK_S_UNSUPP).
The commit message should state the invariant the patch relies on:
"the WRITE ZEROES limit of the backend device does not change while
QEMU has it open".
(2) [design] write-zeroes=on is silently best-effort, so the same
command line produces a different guest ABI depending on the host.
The feature is dropped in three cases: the limit cannot be read
(warning), the limit is 0 (warning), the kernel has no vhost-blk
support for it (masked by vhost_get_features(), no message).
Management cannot tell from the command line or from an error which
VMs have the feature.
Failure scenario, rolling upgrade: a VM started on an upgraded node
negotiates WRITE ZEROES. When it moves to a node that is not upgraded
yet (older vhost-blk, or older dm-qcow2 reporting 0), the destination
QEMU starts without the bit and says nothing; the migration fails only
at virtio_load() with the generic "Features ... unsupported", after RAM
has been transferred.
Conventional fix: OnOffAuto. "on" fails realize with a specific error
if the kernel or the backend cannot provide the feature, "auto" probes
quietly. A destination that cannot honour the feature then refuses to
start. If the bit property stays, the kernel case needs the same
warning as the other two.
> }
>
> vhost_blk_update_size(s, errp);
> @@ -722,6 +756,9 @@ static uint64_t vhost_blk_get_features(VirtIODevice *vdev,
> uint64_t res;
>
> features |= s->host_features;
> + if (!s->max_write_zeroes_sectors) {
> + virtio_clear_feature(&features, VIRTIO_BLK_F_WRITE_ZEROES);
> + }
This is where (1b) and (2) become a failed load: the bit depends on a
value read from the host at realize, and virtio_load() checks the
guest's features against it.
>
> virtio_add_feature(&features, VIRTIO_BLK_F_BLK_SIZE);
> virtio_add_feature(&features, VIRTIO_BLK_F_SEG_MAX);
> @@ -775,6 +812,12 @@ static void vhost_blk_update_config(VirtIODevice *vdev, uint8_t *config)
> blkcfg.physical_block_exp = get_physical_block_exp(conf);
> blkcfg.alignment_offset = 0;
> virtio_stw_p(vdev, &blkcfg.num_queues, s->conf.num_queues);
> + if (s->max_write_zeroes_sectors) {
> + virtio_stl_p(vdev, &blkcfg.max_write_zeroes_sectors,
> + s->max_write_zeroes_sectors);
> + blkcfg.write_zeroes_may_unmap = 1;
> + virtio_stl_p(vdev, &blkcfg.max_write_zeroes_seg, 1);
> + }
(3) [design] max_write_zeroes_sectors is guest-visible state derived
from the host; it is neither migrated nor checked on load.
If the destination backend reports a smaller limit than the source
(a different kernel's dm-qcow2 is enough), the feature bits match, the
load succeeds, and the guest keeps using the limit it read at probe on
the source. The destination kernel fails every range above its own
limit (nr_sects > max_sects -> VIRTIO_BLK_S_IOERR), and the guest is
not told until it reboots or rebinds the driver. Unlike (2), this
failure is silent.
Possible fixes:
- carry the advertised value in a vmstate subsection; on load keep
advertising the source's value if the destination backend can take
it, fail the load otherwise; or
- make it a property, as virtio-blk does with
max-write-zeroes-sectors, so that management pins one value per VM,
and fail realize if the backend limit is below it.
(5) [nit] The fields are filled in based on s->max_write_zeroes_sectors,
not on whether the feature is offered. On a kernel without vhost-blk
support the bit is masked by vhost_get_features(), but the config still
carries the limit, may_unmap and seg. Harmless by the spec, but it is
host-dependent config for no reason. Single source of truth:
if (virtio_host_has_feature(vdev, VIRTIO_BLK_F_WRITE_ZEROES)) {
>
> memcpy(config, &blkcfg, sizeof(struct virtio_blk_config));
> }
[...]
Summary: (1) blocks enabling the property, since every backup of the VM
becomes a window of guest I/O errors. (2) and (3) affect migration.
(4)-(6) are minor.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH hci-8.2 v2] vhost-blk: advertise write zeroes with the backend limit
2026-11-23 16:33 ` Andrey Drobyshev
@ 2026-11-24 13:32 ` Alexander Ivanov
0 siblings, 0 replies; 5+ messages in thread
From: Alexander Ivanov @ 2026-11-24 13:32 UTC (permalink / raw)
To: Andrey Drobyshev; +Cc: andrey.zhadchenko, svt-core
>> +static int vhost_blk_get_write_zeroes_sectors(VHostBlk *s)
>> +{
>> + g_autofree char *dev = NULL;
>> + g_autofree char *part = NULL;
>> + g_autofree char *path = NULL;
>> + g_autofree char *val = NULL;
>> + uint64_t bytes;
>> +
>> + dev = g_strdup_printf("/sys/dev/block/%u:%u",
>> + major(s->backend_rdev), minor(s->backend_rdev));
>> + part = g_strdup_printf("%s/partition", dev);
>> + path = g_strdup_printf("%s/%squeue/write_zeroes_max_bytes", dev,
>> + g_file_test(part, G_FILE_TEST_EXISTS) ? "../" : "");
>> + if (!g_file_get_contents(path, &val, NULL, NULL) ||
>> + qemu_strtou64(g_strstrip(val), NULL, 10, &bytes)) {
>> + return -1;
>> + }
> (6) [nit] Bare -1 return, result stored in *s as a side effect, GError
> from g_file_get_contents() dropped. The caller cannot say whether the
> file is missing, unreadable or unparsable. Usual shape:
>
> static bool vhost_blk_get_write_zeroes_sectors(VHostBlk *s,
> uint32_t *sectors,
> Error **errp)
>
> with the caller reporting via warn_report_err(), or propagating the
> error, see (2).
Since we send sectors pointer as an argument of the function, we
no longer need the VHostBlk pointer.
Changed to:
static bool vhost_blk_get_write_zeroes_sectors(dev_t rdev, uint32_t
*sectors,
Error **errp)
>> +
>> + /* The kernel issues the whole range as one bio */
>> + s->max_write_zeroes_sectors = MIN(bytes >> BDRV_SECTOR_BITS,
>> + UINT32_MAX >> BDRV_SECTOR_BITS);
> (4) [low] The limit is not rounded down to the logical block size of
> the backend. The kernel commit requires it:
>
> The VMM must advertise the feature only for a backend with WRITE
> ZEROES support and set max_write_zeroes_sectors from its limit,
> capped at 8388607 so that the range fits in one bio and rounded
> down to the logical block size, and max_write_zeroes_seg to 1.
>
> vhost_blk_parse_write_zeroes() fails a request with
> (sector | nr_sects) & bs_mask. 8388607 is odd, so whenever the cap
> applies on a backend with a 4K logical block size, a guest that chunks
> a large range at the advertised limit gets VIRTIO_BLK_S_IOERR on every
> chunk. The cap applies to common limits: loop and nbd report exactly
> 4294966784 bytes. dm-qcow2 takes its logical block size from the block
> device under the image's filesystem (qcow2_io_hints()), so a 4Kn host
> disk gives 4096.
>
> logical_block_size is still hard-coded to BDRV_SECTOR_SIZE for the
> guest, so 4Kn backends have other problems, but this part is cheap:
>
> int lbs;
>
> if (ioctl(s->backend_fd, BLKSSZGET, &lbs) < 0) {
> ...
> }
> sectors = MIN(bytes >> BDRV_SECTOR_BITS,
> UINT32_MAX >> BDRV_SECTOR_BITS);
> sectors = QEMU_ALIGN_DOWN(sectors, lbs >> BDRV_SECTOR_BITS);
Agreed, fixed. The limit is now rounded down to the logical block size
of the backend, taken with BLKSSZGET on the backend fd. It also covers
the uncapped case: loop reports UINT_MAX >> 9 sectors, which is odd as
well. If BLKSSZGET fails, the feature is not offered, as when the sysfs
limit cannot be read. A limit that rounds down to zero hits the existing
"does not support write zeroes" path.
>> + return 0;
>> +}
>> +
>> static bool vhost_blk_open_backend(VHostBlk *s, Error **errp)
>> {
>> ERRP_GUARD();
>> @@ -562,6 +586,16 @@ static bool vhost_blk_open_backend(VHostBlk *s, Error **errp)
>> s->conf.devpath);
>> goto fail;
>> }
>> +
>> + if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_WRITE_ZEROES)) {
>> + if (vhost_blk_get_write_zeroes_sectors(s)) {
>> + warn_report("vhost-blk: unable to read write zeroes limit "
>> + "of '%s'", s->conf.devpath);
>> + } else if (!s->max_write_zeroes_sectors) {
>> + warn_report("vhost-blk: '%s' does not support write zeroes",
>> + s->conf.devpath);
>> + }
>> + }
> (1) [major] The limit is sampled once here, but the table of the open
> device is replaced at runtime, and the kernel checks the live limit.
>
> Every backup of a running VM does this. ploop-backup calls
> ploop_tg_init(path, "push_backup", ...), which does (ploop lib/dm.c,
> paraphrased):
>
> 1 dmsetup reload ploopXXX --table "0 ... error"
> 2 dmsetup create ploopXXX.push_backup ... qcow2 ...
> 3 dmsetup reload ploopXXX --table \
> "0 ... push_backup ... /dev/mapper/ploopXXX.push_backup"
> 4 dmsetup resume ploopXXX
>
> The dev_t QEMU has open stops being a qcow2 target and becomes
> push_backup stacked over a new dm-qcow2 device. dm-push-backup sets
> only num_discard_bios (same for dm-tracking, which ploop copy stacks
> the same way), and dm_table_supports_write_zeroes() requires
> num_write_zeroes_bios on every target. The limit of ploopXXX therefore
> drops to 0 for the duration of the backup regardless of dm-qcow2
> underneath. Any target without it shows the effect: a loop device
> reports 4294966784, dm-delay on top of it reports 0.
>
> Consequences:
>
> a) Running guest. QEMU keeps advertising the value from realize, the
> guest keeps sending WRITE ZEROES, and the kernel does
>
> max_sects = bdev_write_zeroes_sectors(bdev);
> ...
> if (vhost_blk_backend_ro(f) || !max_sects || ...
> *status = VIRTIO_BLK_S_IOERR;
>
> Every such request fails until the backup ends. A 6.12 guest does
> not hide it: blkdev_issue_zeroout() falls back to writing zero
> pages only on -EOPNOTSUPP; -EIO goes to the caller (filesystem,
> mkfs, fallocate on the block device).
>
> b) QEMU realized while the stack is in place. qemu-update re-runs
> realize in the new process, reads 0, and vhost_blk_get_features()
> clears a bit the guest has already negotiated. virtio_load() then
> fails with "Features 0x... unsupported. Allowed features: 0x...".
> With cpr-exec that is after exec, with no old process to return to.
> No check for an active ploop backup was found in libvirt's
> qemuDomainGuestQemuUpdate(); other callers were not searched.
>
> QEMU cannot fix this alone. Re-reading the limit on a uevent and
> raising a config change interrupt does not help: Linux virtio-blk
> re-reads only the capacity (virtblk_config_changed_work()), queue
> limits are set once at probe. It needs one of the following on the
> kernel side before the feature is enabled:
>
> - dm-push-backup and dm-tracking pass WRITE ZEROES through with the
> limit of the underlying dm-qcow2 device, so the limit of ploopXXX is
> the same before, during and after the swap; or
> - vhost-blk stops failing on the live limit (the block layer already
> splits an oversized WRITE ZEROES bio; a zero limit could fall back
> to writing zeroes or at least return VIRTIO_BLK_S_UNSUPP).
>
> The commit message should state the invariant the patch relies on:
> "the WRITE ZEROES limit of the backend device does not change while
> QEMU has it open".
Added a kernel patch: [PATCH hci-8.2] drivers/vhost/blk: return UNSUPP
for write zeroes on a backend without it
Removed vhost_blk_get_write_zeroes_sectors().
>
> (2) [design] write-zeroes=on is silently best-effort, so the same
> command line produces a different guest ABI depending on the host.
>
> The feature is dropped in three cases: the limit cannot be read
> (warning), the limit is 0 (warning), the kernel has no vhost-blk
> support for it (masked by vhost_get_features(), no message).
> Management cannot tell from the command line or from an error which
> VMs have the feature.
>
> Failure scenario, rolling upgrade: a VM started on an upgraded node
> negotiates WRITE ZEROES. When it moves to a node that is not upgraded
> yet (older vhost-blk, or older dm-qcow2 reporting 0), the destination
> QEMU starts without the bit and says nothing; the migration fails only
> at virtio_load() with the generic "Features ... unsupported", after RAM
> has been transferred.
>
> Conventional fix: OnOffAuto. "on" fails realize with a specific error
> if the kernel or the backend cannot provide the feature, "auto" probes
> quietly. A destination that cannot honour the feature then refuses to
> start. If the bit property stays, the kernel case needs the same
> warning as the other two.
>
>> }
>>
>> vhost_blk_update_size(s, errp);
>> @@ -722,6 +756,9 @@ static uint64_t vhost_blk_get_features(VirtIODevice *vdev,
>> uint64_t res;
>>
>> features |= s->host_features;
>> + if (!s->max_write_zeroes_sectors) {
>> + virtio_clear_feature(&features, VIRTIO_BLK_F_WRITE_ZEROES);
>> + }
> This is where (1b) and (2) become a failed load: the bit depends on a
> value read from the host at realize, and virtio_load() checks the
> guest's features against it.
>
>> virtio_add_feature(&features, VIRTIO_BLK_F_BLK_SIZE);
>> virtio_add_feature(&features, VIRTIO_BLK_F_SEG_MAX);
>> @@ -775,6 +812,12 @@ static void vhost_blk_update_config(VirtIODevice *vdev, uint8_t *config)
>> blkcfg.physical_block_exp = get_physical_block_exp(conf);
>> blkcfg.alignment_offset = 0;
>> virtio_stw_p(vdev, &blkcfg.num_queues, s->conf.num_queues);
>> + if (s->max_write_zeroes_sectors) {
>> + virtio_stl_p(vdev, &blkcfg.max_write_zeroes_sectors,
>> + s->max_write_zeroes_sectors);
>> + blkcfg.write_zeroes_may_unmap = 1;
>> + virtio_stl_p(vdev, &blkcfg.max_write_zeroes_seg, 1);
>> + }
> (3) [design] max_write_zeroes_sectors is guest-visible state derived
> from the host; it is neither migrated nor checked on load.
>
> If the destination backend reports a smaller limit than the source
> (a different kernel's dm-qcow2 is enough), the feature bits match, the
> load succeeds, and the guest keeps using the limit it read at probe on
> the source. The destination kernel fails every range above its own
> limit (nr_sects > max_sects -> VIRTIO_BLK_S_IOERR), and the guest is
> not told until it reboots or rebinds the driver. Unlike (2), this
> failure is silent.
>
> Possible fixes:
>
> - carry the advertised value in a vmstate subsection; on load keep
> advertising the source's value if the destination backend can take
> it, fail the load otherwise; or
> - make it a property, as virtio-blk does with
> max-write-zeroes-sectors, so that management pins one value per VM,
> and fail realize if the backend limit is below it.
After the previous changes the limit is not derived from the backend:
vhost-blk rejects only ranges above UINT32_MAX >> 9 or not lbs-aligned,
and the block layer splits the bio to the queue limit, so a different
dm-qcow2 on the destination cannot cause IOERR. The lbs rounding
did make the value host-dependent; it is now a constant
(UINT32_MAX >> 9 aligned down to 64 KiB), so nothing needs migrating.
> (5) [nit] The fields are filled in based on s->max_write_zeroes_sectors,
> not on whether the feature is offered. On a kernel without vhost-blk
> support the bit is masked by vhost_get_features(), but the config still
> carries the limit, may_unmap and seg. Harmless by the spec, but it is
> host-dependent config for no reason. Single source of truth:
>
> if (virtio_host_has_feature(vdev, VIRTIO_BLK_F_WRITE_ZEROES)) {
>
>> memcpy(config, &blkcfg, sizeof(struct virtio_blk_config));
>> }
Moot, since realize fails without kernel support, but done anyway:
config is now filled under virtio_host_has_feature().
> [...]
>
> Summary: (1) blocks enabling the property, since every backup of the VM
> becomes a window of guest I/O errors. (2) and (3) affect migration.
> (4)-(6) are minor.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-24 13:32 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-20 13:23 [PATCH hci-8.2 v2] vhost-blk: advertise write zeroes with the backend limit Alexander Ivanov
2026-11-20 13:26 ` no-reply
2026-11-20 13:34 ` Alexander Ivanov
2026-11-23 16:33 ` Andrey Drobyshev
2026-11-24 13:32 ` Alexander Ivanov
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.