* [PATCH hci-8.2] vhost-blk: advertise write zeroes with the backend limit
@ 2026-11-18 10:46 Alexander Ivanov
2026-11-18 10:52 ` no-reply
2026-11-19 12:40 ` Andrey Zhadchenko
0 siblings, 2 replies; 4+ messages in thread
From: Alexander Ivanov @ 2026-11-18 10:46 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. Do not offer VIRTIO_BLK_F_WRITE_ZEROES
when the backend reports no limit or 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>
---
hw/block/vhost-blk.c | 36 ++++++++++++++++++++++++++++++++++-
include/hw/virtio/vhost-blk.h | 1 +
2 files changed, 36 insertions(+), 1 deletion(-)
diff --git a/hw/block/vhost-blk.c b/hw/block/vhost-blk.c
index abddff02c6d..8fe7a46d191 100644
--- a/hw/block/vhost-blk.c
+++ b/hw/block/vhost-blk.c
@@ -521,6 +521,23 @@ static bool vhost_blk_uevent_attach(VHostBlk *s, Error **errp)
return true;
}
+static uint32_t vhost_blk_get_write_zeroes_sectors(VHostBlk *s)
+{
+ g_autofree char *path = NULL;
+ g_autofree char *val = NULL;
+ uint64_t bytes;
+
+ path = g_strdup_printf("/sys/dev/block/%u:%u/queue/write_zeroes_max_bytes",
+ major(s->backend_rdev), minor(s->backend_rdev));
+ if (!g_file_get_contents(path, &val, NULL, NULL) ||
+ qemu_strtou64(g_strstrip(val), NULL, 10, &bytes)) {
+ return 0;
+ }
+
+ /* The kernel issues the whole range as one bio */
+ return MIN(bytes >> BDRV_SECTOR_BITS, UINT32_MAX >> BDRV_SECTOR_BITS);
+}
+
static bool vhost_blk_open_backend(VHostBlk *s, Error **errp)
{
ERRP_GUARD();
@@ -562,6 +579,14 @@ 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)) {
+ s->max_write_zeroes_sectors = vhost_blk_get_write_zeroes_sectors(s);
+ 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 +747,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 +803,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 +823,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] 4+ messages in thread
* Re: [PATCH hci-8.2] vhost-blk: advertise write zeroes with the backend limit
2026-11-18 10:46 [PATCH hci-8.2] vhost-blk: advertise write zeroes with the backend limit Alexander Ivanov
@ 2026-11-18 10:52 ` no-reply
2026-11-20 13:17 ` Alexander Ivanov
2026-11-19 12:40 ` Andrey Zhadchenko
1 sibling, 1 reply; 4+ messages in thread
From: no-reply @ 2026-11-18 10:52 UTC (permalink / raw)
To: Alexander Ivanov, andrey.zhadchenko; +Cc: andrey.drobyshev, svt-core
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Low] vhost-blk: write-zeroes limit lookup fails for partition
backends
- [Low] vhost-blk: no warning when write-zeroes=on is dropped on
read-only backend
- [Low] vhost-blk: write zeroes limit sampled once and never revalidated
--
commit 66e3eda6a649795c315512f6c2bb40ad45109264
Author: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
vhost-blk: advertise write zeroes with the backend limit
When the write-zeroes property is on, vhost-blk 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. It does not offer VIRTIO_BLK_F_WRITE_ZEROES when the backend reports no limit or is read-only.
> diff --git a/hw/block/vhost-blk.c b/hw/block/vhost-blk.c
> index abddff02c6d92..8fe7a46d19166 100644
> --- a/hw/block/vhost-blk.c
> +++ b/hw/block/vhost-blk.c
> @@ -521,6 +521,23 @@ static bool vhost_blk_uevent_attach(VHostBlk *s, Error **errp)
> return true;
> }
>
> +static uint32_t vhost_blk_get_write_zeroes_sectors(VHostBlk *s)
> +{
> + g_autofree char *path = NULL;
> + g_autofree char *val = NULL;
> + uint64_t bytes;
> +
> + path = g_strdup_printf("/sys/dev/block/%u:%u/queue/write_zeroes_max_bytes",
> + major(s->backend_rdev), minor(s->backend_rdev));
> + if (!g_file_get_contents(path, &val, NULL, NULL) ||
> + qemu_strtou64(g_strstrip(val), NULL, 10, &bytes)) {
> + return 0;
> + }
> +
> + /* The kernel issues the whole range as one bio */
> + return MIN(bytes >> BDRV_SECTOR_BITS, UINT32_MAX >> BDRV_SECTOR_BITS);
> +}
> +
[Severity: Low]
Does this lookup work when the backend is a partition such as /dev/sda1 or /dev/nvme0n1p1? vhost_blk_open_backend() accepts any block device node. For a partition, /sys/dev/block/MAJ:MIN points at the partition, and the queue/ directory normally exists only on the whole-disk node. g_file_get_contents() then fails and the helper returns 0.
With write-zeroes=on, the feature is then silently withheld even though the underlying disk may support it. The caller also warns "does not support write zeroes", which is misleading when only the lookup failed. A read or parse failure cannot be told apart from a real limit of 0 either.
Could the helper fall back to the parent disk's queue/ directory? Otherwise, the warning could say the limit could not be determined, and the commit message could state the whole-disk restriction.
> @@ -562,6 +579,14 @@ 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)) {
> + s->max_write_zeroes_sectors = vhost_blk_get_write_zeroes_sectors(s);
> + if (!s->max_write_zeroes_sectors) {
> + warn_report("vhost-blk: '%s' does not support write zeroes",
> + s->conf.devpath);
> + }
> + }
> }
[Severity: Low]
Should a read-only backend also get a diagnostic? The probe and the warn_report() both sit inside the !s->conf.readonly block. With write-zeroes=on and read-only=on, max_write_zeroes_sectors stays 0 and vhost_blk_get_features() clears the bit with no message. A writable backend with no limit does get a warning.
[ ... ]
> @@ -775,6 +803,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]
Is it a problem that the limit is sampled only once, in vhost_blk_open_backend(), and never revalidated? Suppose a guest negotiated a larger limit on the source, and the destination backend has a smaller write_zeroes_max_bytes after migration or CPR. The destination reports its own value here, but nothing compares it with what the guest saw and no config-change notification is sent. The same applies to a runtime sysfs change, because the resize handler does not re-read the limit.
The guest could then submit a request larger than the real limit. That request goes to the kernel as a single bio and fails, so the guest sees an I/O error.
--
Sashiko AI review · https://sashiko.vzint.dev/#/patchset/20261118104654.3324703-1-alexander.ivanov@virtuozzo.com?part=1
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH hci-8.2] vhost-blk: advertise write zeroes with the backend limit
2026-11-18 10:46 [PATCH hci-8.2] vhost-blk: advertise write zeroes with the backend limit Alexander Ivanov
2026-11-18 10:52 ` no-reply
@ 2026-11-19 12:40 ` Andrey Zhadchenko
1 sibling, 0 replies; 4+ messages in thread
From: Andrey Zhadchenko @ 2026-11-19 12:40 UTC (permalink / raw)
To: Alexander Ivanov; +Cc: andrey.drobyshev, svt-core
Reviewed-by: Andrey Zhadchenko <andrey.zhadchenko@virtuozzo.com>
On 11/18/26 11:46, 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. Do not offer VIRTIO_BLK_F_WRITE_ZEROES
> when the backend reports no limit or 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>
> ---
> hw/block/vhost-blk.c | 36 ++++++++++++++++++++++++++++++++++-
> include/hw/virtio/vhost-blk.h | 1 +
> 2 files changed, 36 insertions(+), 1 deletion(-)
>
> diff --git a/hw/block/vhost-blk.c b/hw/block/vhost-blk.c
> index abddff02c6d..8fe7a46d191 100644
> --- a/hw/block/vhost-blk.c
> +++ b/hw/block/vhost-blk.c
> @@ -521,6 +521,23 @@ static bool vhost_blk_uevent_attach(VHostBlk *s, Error **errp)
> return true;
> }
>
> +static uint32_t vhost_blk_get_write_zeroes_sectors(VHostBlk *s)
> +{
> + g_autofree char *path = NULL;
> + g_autofree char *val = NULL;
> + uint64_t bytes;
> +
> + path = g_strdup_printf("/sys/dev/block/%u:%u/queue/write_zeroes_max_bytes",
> + major(s->backend_rdev), minor(s->backend_rdev));
> + if (!g_file_get_contents(path, &val, NULL, NULL) ||
> + qemu_strtou64(g_strstrip(val), NULL, 10, &bytes)) {
> + return 0;
> + }
> +
> + /* The kernel issues the whole range as one bio */
> + return MIN(bytes >> BDRV_SECTOR_BITS, UINT32_MAX >> BDRV_SECTOR_BITS);
> +}
> +
> static bool vhost_blk_open_backend(VHostBlk *s, Error **errp)
> {
> ERRP_GUARD();
> @@ -562,6 +579,14 @@ 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)) {
> + s->max_write_zeroes_sectors = vhost_blk_get_write_zeroes_sectors(s);
> + 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 +747,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 +803,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 +823,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;
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH hci-8.2] vhost-blk: advertise write zeroes with the backend limit
2026-11-18 10:52 ` no-reply
@ 2026-11-20 13:17 ` Alexander Ivanov
0 siblings, 0 replies; 4+ messages in thread
From: Alexander Ivanov @ 2026-11-20 13:17 UTC (permalink / raw)
To: no-reply, andrey.zhadchenko; +Cc: andrey.drobyshev, svt-core
>> diff --git a/hw/block/vhost-blk.c b/hw/block/vhost-blk.c
>> index abddff02c6d92..8fe7a46d19166 100644
>> --- a/hw/block/vhost-blk.c
>> +++ b/hw/block/vhost-blk.c
>> @@ -521,6 +521,23 @@ static bool vhost_blk_uevent_attach(VHostBlk *s, Error **errp)
>> return true;
>> }
>>
>> +static uint32_t vhost_blk_get_write_zeroes_sectors(VHostBlk *s)
>> +{
>> + g_autofree char *path = NULL;
>> + g_autofree char *val = NULL;
>> + uint64_t bytes;
>> +
>> + path = g_strdup_printf("/sys/dev/block/%u:%u/queue/write_zeroes_max_bytes",
>> + major(s->backend_rdev), minor(s->backend_rdev));
>> + if (!g_file_get_contents(path, &val, NULL, NULL) ||
>> + qemu_strtou64(g_strstrip(val), NULL, 10, &bytes)) {
>> + return 0;
>> + }
>> +
>> + /* The kernel issues the whole range as one bio */
>> + return MIN(bytes >> BDRV_SECTOR_BITS, UINT32_MAX >> BDRV_SECTOR_BITS);
>> +}
>> +
> [Severity: Low]
> Does this lookup work when the backend is a partition such as /dev/sda1 or /dev/nvme0n1p1? vhost_blk_open_backend() accepts any block device node. For a partition, /sys/dev/block/MAJ:MIN points at the partition, and the queue/ directory normally exists only on the whole-disk node. g_file_get_contents() then fails and the helper returns 0.
>
> With write-zeroes=on, the feature is then silently withheld even though the underlying disk may support it. The caller also warns "does not support write zeroes", which is misleading when only the lookup failed. A read or parse failure cannot be told apart from a real limit of 0 either.
>
> Could the helper fall back to the parent disk's queue/ directory? Otherwise, the warning could say the limit could not be determined, and the commit message could state the whole-disk restriction.
Fixed.
>> @@ -562,6 +579,14 @@ 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)) {
>> + s->max_write_zeroes_sectors = vhost_blk_get_write_zeroes_sectors(s);
>> + if (!s->max_write_zeroes_sectors) {
>> + warn_report("vhost-blk: '%s' does not support write zeroes",
>> + s->conf.devpath);
>> + }
>> + }
>> }
> [Severity: Low]
> Should a read-only backend also get a diagnostic? The probe and the warn_report() both sit inside the !s->conf.readonly block. With write-zeroes=on and read-only=on, max_write_zeroes_sectors stays 0 and vhost_blk_get_features() clears the bit with no message. A writable backend with no limit does get a warning.
With read-only=on the device offers VIRTIO_BLK_F_RO, so the guest never
issues write zeroes
and dropping the feature changes nothing for it. A warning there would
fire on every start for
a harmless combination (e.g. management setting write-zeroes=on for all
disks) with nothing
to fix. The writable case warns because the user asked for a feature and
silently did not get it.
>> @@ -775,6 +803,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]
> Is it a problem that the limit is sampled only once, in vhost_blk_open_backend(), and never revalidated? Suppose a guest negotiated a larger limit on the source, and the destination backend has a smaller write_zeroes_max_bytes after migration or CPR. The destination reports its own value here, but nothing compares it with what the guest saw and no config-change notification is sent. The same applies to a runtime sysfs change, because the resize handler does not re-read the limit.
>
> The guest could then submit a request larger than the real limit. That request goes to the kernel as a single bio and fails, so the guest sees an I/O error.
No change planned. CPR keeps the same device, a zero limit on the
destination already fails migration via the feature check, and a
config-change notification would not help since the Linux guest reads
the limit only at probe. Cross-host migration between backends with
different non-zero limits is rare and can get a separate vmstate check
if needed.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-20 13:17 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-18 10:46 [PATCH hci-8.2] vhost-blk: advertise write zeroes with the backend limit Alexander Ivanov
2026-11-18 10:52 ` no-reply
2026-11-20 13:17 ` Alexander Ivanov
2026-11-19 12:40 ` Andrey Zhadchenko
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.