All Virtuozzo development lists (kernel + QEMU)
 help / color / mirror / Atom feed
* [PATCH hci-8.2 v3] vhost-blk: implement the write-zeroes property
@ 2026-11-24 13:40 Alexander Ivanov
  2026-11-24 15:34 ` Andrey Drobyshev
  0 siblings, 1 reply; 2+ messages in thread
From: Alexander Ivanov @ 2026-11-24 13:40 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 answers
VIRTIO_BLK_S_UNSUPP when the backend does not support WRITE ZEROES at
the time of the request, so the guest falls back to writing zeroes.

With the write-zeroes property on, offer VIRTIO_BLK_F_WRITE_ZEROES and
set max_write_zeroes_sectors to the largest range one bio can carry,
rounded down to 64 KiB. The kernel fails a range not aligned to the
logical block size of the backend, and a constant keeps the guest-visible
limit the same on every host. Do not offer the feature for a read-only
device.

Do not derive the feature or the limit from the WRITE ZEROES limit of
the backend. It changes while QEMU has the device open, e.g. when a dm
table is reloaded with a target that does not pass WRITE ZEROES through,
while the guest reads the config only at probe and the destination of a
migration or qemu-update must offer the features the guest has
negotiated.

Fail realize if the property is on and the kernel does not offer the
feature. Masking it silently would make the guest ABI depend on the
host: a migration to a node with an older kernel would start the
destination without the feature and fail only in virtio_load(), after
RAM has been transferred.

The property stays off by default. The first kernel version with the
support answered VIRTIO_BLK_S_IOERR for a backend without WRITE ZEROES:
do not turn the property on with it.

https://virtuozzo.atlassian.net/browse/VSTOR-141684

Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
---
v3:
- rename from "advertise write zeroes with the backend limit".
- do not read write_zeroes_max_bytes from sysfs: it changes while the
  device is open and the guest reads the config only at probe. The
  kernel now answers VIRTIO_BLK_S_UNSUPP on a backend without WRITE
  ZEROES, so advertise a constant limit, the same on every host.
- fail realize if the property is on and the kernel does not offer the
  feature, instead of masking it.
- rebase on the worker ioctl series.

 hw/block/vhost-blk.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/hw/block/vhost-blk.c b/hw/block/vhost-blk.c
index f88c1c304e8..24982ea7834 100644
--- a/hw/block/vhost-blk.c
+++ b/hw/block/vhost-blk.c
@@ -9,6 +9,7 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu/units.h"
 #include "qapi/error.h"
 #include "qemu/cutils.h"
 #include "qemu/error-report.h"
@@ -33,6 +34,11 @@
 #include "migration/cpr.h"
 #include "system/runstate.h"
 
+/* The kernel issues the range as one bio and fails it if not lbs-aligned */
+#define VHOST_BLK_MAX_WRITE_ZEROES_SECTORS \
+    QEMU_ALIGN_DOWN(UINT32_MAX >> BDRV_SECTOR_BITS, \
+                    (64 * KiB) >> BDRV_SECTOR_BITS)
+
 /*
  * Spread the virtqueues round-robin over nworkers kernel threads. Thread 0 is
  * the default worker VHOST_SET_OWNER created; vq 0 and every nworkers-th vq
@@ -724,6 +730,16 @@ static void vhost_blk_device_realize(DeviceState *dev, Error **errp)
         goto cleanup;
     }
 
+    if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_WRITE_ZEROES) &&
+        !virtio_has_feature(s->dev.features, VIRTIO_BLK_F_WRITE_ZEROES)) {
+        error_setg(errp, "vhost-blk: write-zeroes is not supported by the "
+                   "vhost-blk kernel module");
+        vhost_dev_cleanup(&s->dev);
+        /* vhost_dev_cleanup() closes vhostfd */
+        s->vhostfd = -1;
+        goto cleanup;
+    }
+
     return;
 
 cleanup:
@@ -795,6 +811,7 @@ static uint64_t vhost_blk_get_features(VirtIODevice *vdev,
 
     if (s->conf.readonly) {
         virtio_add_feature(&features, VIRTIO_BLK_F_RO);
+        virtio_clear_feature(&features, VIRTIO_BLK_F_WRITE_ZEROES);
     }
 
     if (s->conf.num_queues > 1) {
@@ -838,6 +855,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 (virtio_host_has_feature(vdev, VIRTIO_BLK_F_WRITE_ZEROES)) {
+        virtio_stl_p(vdev, &blkcfg.max_write_zeroes_sectors,
+                     VHOST_BLK_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));
 }
@@ -858,7 +881,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,
-- 
2.53.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-24 15:34 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-24 13:40 [PATCH hci-8.2 v3] vhost-blk: implement the write-zeroes property Alexander Ivanov
2026-11-24 15:34 ` Andrey Drobyshev

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.