All Virtuozzo development lists (kernel + QEMU)
 help / color / mirror / Atom feed
* [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

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.