From: Andrey Zhadchenko <andrey.zhadchenko@virtuozzo.com>
To: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
Cc: andrey.drobyshev@virtuozzo.com, svt-core@virtuozzo.com
Subject: Re: [PATCH hci-8.2 v5] vhost-blk: advertise write zeroes on a backend that supports it
Date: Thu, 26 Nov 2026 12:33:10 +0100 [thread overview]
Message-ID: <464770d1-1358-4541-a352-56a9922b39bd@virtuozzo.com> (raw)
In-Reply-To: <20261125170822.827369-1-alexander.ivanov@virtuozzo.com>
Reviewed-by: Andrey Zhadchenko <andrey.zhadchenko@virtuozzo.com>
On 11/25/26 18:08, Alexander Ivanov wrote:
> vhost-blk in the kernel now handles VIRTIO_BLK_T_WRITE_ZEROES. It sends
> each request to the backend as one WRITE ZEROES bio, which the block
> layer splits by the limit of the backend, and answers
> VIRTIO_BLK_S_UNSUPP when the backend has no WRITE ZEROES support. So the
> range of a request is bounded only by the size of one bio.
>
> With the write-zeroes property on, read write_zeroes_max_bytes of a
> writable backend from sysfs once at startup and use it only to tell
> whether the backend supports WRITE ZEROES. Then advertise the constant
> VHOST_BLK_MAX_WRITE_ZEROES_SECTORS: a power of two that fits in one bio
> and is aligned to any logical block size, so the guest never sends a
> range the kernel rejects. The limit itself can change later on a dm
> table reload, and a constant keeps the config the same on both sides of
> a migration. 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>
> ---
> v5:
> - advertise the constant VHOST_BLK_MAX_WRITE_ZEROES_SECTORS instead of
> the backend limit: the kernel lets the block layer split the bio, and
> a constant survives a dm table reload and a migration.
> - read write_zeroes_max_bytes only to tell whether the backend supports
> WRITE ZEROES.
> - drop the BLKSSZGET call, the constant is aligned to any logical block
> size.
>
> v4:
> - go back to v2 and drop the v3 approach with a constant limit and a
> realize failure.
> - round the limit down to the logical block size of the backend, since
> the kernel fails a misaligned range.
> - report why the limit cannot be read in the warning.
> - rebase.
>
> v3: dropped
>
> 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 | 54 ++++++++++++++++++++++++++++++++++-
> include/hw/virtio/vhost-blk.h | 3 ++
> 2 files changed, 56 insertions(+), 1 deletion(-)
>
> diff --git a/hw/block/vhost-blk.c b/hw/block/vhost-blk.c
> index f88c1c304e8..870f3496908 100644
> --- a/hw/block/vhost-blk.c
> +++ b/hw/block/vhost-blk.c
> @@ -586,6 +586,36 @@ static bool vhost_blk_uevent_attach(VHostBlk *s, Error **errp)
> return true;
> }
>
> +static bool vhost_blk_has_write_zeroes(dev_t rdev, bool *supported,
> + Error **errp)
> +{
> + g_autofree char *dev = NULL;
> + g_autofree char *part = NULL;
> + g_autofree char *path = NULL;
> + g_autofree char *val = NULL;
> + g_autoptr(GError) gerr = NULL;
> + uint64_t bytes;
> + int ret;
> +
> + dev = g_strdup_printf("/sys/dev/block/%u:%u", major(rdev), minor(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, &gerr)) {
> + error_setg(errp, "%s", gerr->message);
> + return false;
> + }
> +
> + ret = qemu_strtou64(g_strstrip(val), NULL, 10, &bytes);
> + if (ret) {
> + error_setg_errno(errp, -ret, "unable to parse '%s'", path);
> + return false;
> + }
> +
> + *supported = bytes != 0;
> + return true;
> +}
> +
> static bool vhost_blk_open_backend(VHostBlk *s, Error **errp)
> {
> ERRP_GUARD();
> @@ -627,6 +657,19 @@ 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)) {
> + Error *local_err = NULL;
> +
> + if (!vhost_blk_has_write_zeroes(s->backend_rdev, &s->write_zeroes,
> + &local_err)) {
> + warn_reportf_err(local_err, "vhost-blk: unable to read write "
> + "zeroes limit of '%s': ", s->conf.devpath);
> + } else if (!s->write_zeroes) {
> + warn_report("vhost-blk: '%s' does not support write zeroes",
> + s->conf.devpath);
> + }
> + }
> }
>
> vhost_blk_update_size(s, errp);
> @@ -785,6 +828,9 @@ static uint64_t vhost_blk_get_features(VirtIODevice *vdev,
> uint64_t res;
>
> features |= s->host_features;
> + if (!s->write_zeroes) {
> + 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);
> @@ -838,6 +884,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->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 +910,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..f05715c3809 100644
> --- a/include/hw/virtio/vhost-blk.h
> +++ b/include/hw/virtio/vhost-blk.h
> @@ -21,6 +21,8 @@
>
> #define VHOST_BLK_AUTO_NUM_QUEUES UINT16_MAX
> #define VHOST_BLK_MAX_QUEUES 32
> +/* Fits in one bio and is aligned to any logical block size */
> +#define VHOST_BLK_MAX_WRITE_ZEROES_SECTORS (1U << 22)
>
> typedef struct VhostBlkConf {
> BlockConf conf;
> @@ -46,6 +48,7 @@ typedef struct VHostBlk {
> bool vhost_started;
> uint64_t length;
> uint64_t backend_rdev;
> + bool write_zeroes;
> QEMUBH *resize_bh;
> Error *cpr_blocker;
> } VHostBlk;
next prev parent reply other threads:[~2026-11-26 11:33 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 17:08 Alexander Ivanov
2026-11-26 11:33 ` Andrey Zhadchenko [this message]
2026-11-26 11:49 ` Andrey Drobyshev
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=464770d1-1358-4541-a352-56a9922b39bd@virtuozzo.com \
--to=andrey.zhadchenko@virtuozzo.com \
--cc=alexander.ivanov@virtuozzo.com \
--cc=andrey.drobyshev@virtuozzo.com \
--cc=svt-core@virtuozzo.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.