All Virtuozzo development lists (kernel + QEMU)
 help / color / mirror / Atom feed
* [Devel] [PATCH hci-8.2 v4] drivers/vhost/blk: support VIRTIO_BLK_F_WRITE_ZEROES
@ 2026-11-18 11:08 Alexander Ivanov
  2026-11-18 11:13 ` no-reply
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Alexander Ivanov @ 2026-11-18 11:08 UTC (permalink / raw)
  To: andrey.zhadchenko; +Cc: devel

Handle VIRTIO_BLK_T_WRITE_ZEROES for parity with virtio-blk. The range
is sent to the backend as a single REQ_OP_WRITE_ZEROES bio, with
REQ_NOUNMAP unless the guest set the UNMAP flag.

The VMM advertises the feature only for a backend with WRITE ZEROES
support and sets max_write_zeroes_sectors from its limit, capped at
8388607 so that the range fits in one bio, and max_write_zeroes_seg
to 1.

The guest gets VIRTIO_BLK_S_UNSUPP when VIRTIO_BLK_F_WRITE_ZEROES was
not negotiated, for a payload other than exactly one segment or an
unknown flag. It gets VIRTIO_BLK_S_IOERR for a read-only backend, a
backend without WRITE ZEROES and a range that is unaligned, beyond the
capacity or above these limits.

https://virtuozzo.atlassian.net/browse/VSTOR-141684
Feature: vhost-blk: in-kernel accelerator for virtio-blk guests
Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
---
v4:
- one REQ_OP_WRITE_ZEROES bio per request: no chunks, no zero-page
  fallback, no vhost_blk_retry_zeroes().
- a backend without WRITE ZEROES and a range above
  bdev_write_zeroes_sectors() get VIRTIO_BLK_S_IOERR.
- commit message: the VMM advertises the feature only for a backend
  with WRITE ZEROES and sets max_write_zeroes_sectors from its limit,
  see the QEMU patch "vhost-blk: advertise write zeroes with the
  backend limit".
- patch 1 of v3 was sent separately.

v3:
- commit message: the VMM paragraph says what a large
  max_write_zeroes_sectors costs and that our QEMU does not advertise
  the feature yet.

v2:
- shorten the commit message, state parity with virtio-blk as the
  reason and what the VMM must advertise.
- issue chunks of at most 16 WRITE ZEROES bios and 1 GiB, try them
  with BLKDEV_ZERO_NOFALLBACK and fall back to 4 MiB chunks of zero
  pages.
- complete the request with VIRTIO_BLK_S_UNSUPP when
  VIRTIO_BLK_F_WRITE_ZEROES was not negotiated.

 drivers/vhost/blk.c | 70 +++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 68 insertions(+), 2 deletions(-)

diff --git a/drivers/vhost/blk.c b/drivers/vhost/blk.c
index 7975a0e63d65..d6917f0e662e 100644
--- a/drivers/vhost/blk.c
+++ b/drivers/vhost/blk.c
@@ -29,7 +29,8 @@ enum {
 			     (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
 			     (1ULL << VIRTIO_RING_F_EVENT_IDX) |
 			     (1ULL << VIRTIO_BLK_F_MQ) |
-			     (1ULL << VIRTIO_BLK_F_FLUSH),
+			     (1ULL << VIRTIO_BLK_F_FLUSH) |
+			     (1ULL << VIRTIO_BLK_F_WRITE_ZEROES),
 };
 
 /*
@@ -284,6 +285,13 @@ static int vhost_blk_bio_make(struct vhost_blk_req *req,
 	if (unlikely(req->bi_opf & REQ_PREFLUSH))
 		return vhost_blk_bio_make_simple(req, bdev);
 
+	if ((req->bi_opf & REQ_OP_MASK) == REQ_OP_WRITE_ZEROES) {
+		ret = vhost_blk_bio_make_simple(req, bdev);
+		if (!ret)
+			req->bio[0]->bi_iter.bi_size = req->len;
+		return ret;
+	}
+
 	/*
 	 * Requests with unaligned io vectors are not supported by underlined
 	 * block device, let's use an aligned intermediary "bounce buffer" to
@@ -451,6 +459,51 @@ static bool vhost_blk_backend_ro(struct file *f)
 	       bdev_read_only(I_BDEV(f->f_mapping->host));
 }
 
+static int vhost_blk_parse_write_zeroes(struct vhost_blk_req *req, u8 *status)
+{
+	struct file *f = vhost_vq_get_backend(&req->blk_vq->vq);
+	struct block_device *bdev = I_BDEV(f->f_mapping->host);
+	sector_t bs_mask = (bdev_logical_block_size(bdev) >> SECTOR_SHIFT) - 1;
+	unsigned int max_sects = bdev_write_zeroes_sectors(bdev);
+	struct virtio_blk_discard_write_zeroes seg;
+	struct iov_iter iter;
+	u32 flags, nr_sects;
+	sector_t sector;
+
+	*status = VIRTIO_BLK_S_UNSUPP;
+	if (!vhost_has_feature(&req->blk_vq->vq, VIRTIO_BLK_F_WRITE_ZEROES) ||
+	    req->len != sizeof(seg))
+		return 0;
+
+	iov_iter_init(&iter, ITER_SOURCE, req->iov, req->iov_nr, req->len);
+	if (copy_from_iter(&seg, sizeof(seg), &iter) != sizeof(seg))
+		return -EFAULT;
+
+	flags = le32_to_cpu(seg.flags);
+	if (flags & ~VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP)
+		return 0;
+
+	sector = le64_to_cpu(seg.sector);
+	nr_sects = le32_to_cpu(seg.num_sectors);
+	if (vhost_blk_backend_ro(f) || !max_sects ||
+	    nr_sects > UINT_MAX >> SECTOR_SHIFT || nr_sects > max_sects ||
+	    (sector | nr_sects) & bs_mask ||
+	    sector > bdev_nr_sectors(bdev) ||
+	    nr_sects > bdev_nr_sectors(bdev) - sector) {
+		*status = VIRTIO_BLK_S_IOERR;
+		return 0;
+	}
+
+	req->sector = sector;
+	req->len = (size_t)nr_sects << SECTOR_SHIFT;
+	req->bi_opf = REQ_OP_WRITE_ZEROES;
+	if (!(flags & VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP))
+		req->bi_opf |= REQ_NOUNMAP;
+
+	*status = VIRTIO_BLK_S_OK;
+	return 0;
+}
+
 static int vhost_blk_req_handle(struct vhost_virtqueue *vq,
 				struct virtio_blk_outhdr *hdr,
 				u16 head, u16 out, u16 in)
@@ -490,7 +543,7 @@ static int vhost_blk_req_handle(struct vhost_virtqueue *vq,
 	req->status[0].iov_base = st->iov_base + st->iov_len;
 	req->status[0].iov_len = sizeof(status);
 
-	if (type == VIRTIO_BLK_T_OUT) {
+	if (type == VIRTIO_BLK_T_OUT || type == VIRTIO_BLK_T_WRITE_ZEROES) {
 		data_iov = vq->iov;
 		data_nr = out;
 	} else {
@@ -538,6 +591,19 @@ static int vhost_blk_req_handle(struct vhost_virtqueue *vq,
 		req->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
 		ret = vhost_blk_req_submit(req);
 		break;
+	case VIRTIO_BLK_T_WRITE_ZEROES:
+		ret = vhost_blk_parse_write_zeroes(req, &status);
+		if (ret)
+			break;
+		if (status == VIRTIO_BLK_S_OK && req->len) {
+			ret = vhost_blk_req_submit(req);
+			break;
+		}
+		ret = vhost_blk_set_status(req, status);
+		if (ret)
+			break;
+		vhost_add_used_and_signal(&blk->dev, vq, head, sizeof(status));
+		break;
 	case VIRTIO_BLK_T_GET_ID:
 		len = min_t(size_t,
 			    strnlen(blk->serial, VIRTIO_BLK_ID_BYTES) + 1,
-- 
2.53.0

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

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

* Re: [Devel] [PATCH hci-8.2 v4] drivers/vhost/blk: support VIRTIO_BLK_F_WRITE_ZEROES
  2026-11-18 11:08 [Devel] [PATCH hci-8.2 v4] drivers/vhost/blk: support VIRTIO_BLK_F_WRITE_ZEROES Alexander Ivanov
@ 2026-11-18 11:13 ` no-reply
  2026-11-18 11:45 ` Andrey Zhadchenko
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: no-reply @ 2026-11-18 11:13 UTC (permalink / raw)
  To: Alexander Ivanov, andrey.zhadchenko; +Cc: devel

> Handle VIRTIO_BLK_T_WRITE_ZEROES for parity with virtio-blk. The range
> is sent to the backend as a single REQ_OP_WRITE_ZEROES bio, with
> REQ_NOUNMAP unless the guest set the UNMAP flag.
> 
> The VMM advertises the feature only for a backend with WRITE ZEROES
> support and sets max_write_zeroes_sectors from its limit, capped at
> 8388607 so that the range fits in one bio, and max_write_zeroes_seg
> to 1.
> 
> The guest gets VIRTIO_BLK_S_UNSUPP when VIRTIO_BLK_F_WRITE_ZEROES was
> not negotiated, for a payload other than exactly one segment or an
> unknown flag. It gets VIRTIO_BLK_S_IOERR for a read-only backend, a
> backend without WRITE ZEROES and a range that is unaligned, beyond the
> capacity or above these limits.
> 
> https://virtuozzo.atlassian.net/browse/VSTOR-141684
> Feature: vhost-blk: in-kernel accelerator for virtio-blk guests
> Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.vzint.dev/#/patchset/20261118110813.3333232-1-alexander.ivanov@virtuozzo.com?part=1


_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

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

* Re: [Devel] [PATCH hci-8.2 v4] drivers/vhost/blk: support VIRTIO_BLK_F_WRITE_ZEROES
  2026-11-18 11:08 [Devel] [PATCH hci-8.2 v4] drivers/vhost/blk: support VIRTIO_BLK_F_WRITE_ZEROES Alexander Ivanov
  2026-11-18 11:13 ` no-reply
@ 2026-11-18 11:45 ` Andrey Zhadchenko
  2026-11-18 15:26 ` Konstantin Khorenko
  2026-11-18 15:26 ` Konstantin Khorenko
  3 siblings, 0 replies; 5+ messages in thread
From: Andrey Zhadchenko @ 2026-11-18 11:45 UTC (permalink / raw)
  To: Alexander Ivanov; +Cc: devel

Reviewed-by: Andrey Zhadchenko <andrey.zhadchenko@virtuozzo.com>

On 11/18/26 12:08, Alexander Ivanov wrote:
> Handle VIRTIO_BLK_T_WRITE_ZEROES for parity with virtio-blk. The range
> is sent to the backend as a single REQ_OP_WRITE_ZEROES bio, with
> REQ_NOUNMAP unless the guest set the UNMAP flag.
> 
> The VMM advertises the feature only for a backend with WRITE ZEROES
> support and sets max_write_zeroes_sectors from its limit, capped at
> 8388607 so that the range fits in one bio, and max_write_zeroes_seg
> to 1.
> 
> The guest gets VIRTIO_BLK_S_UNSUPP when VIRTIO_BLK_F_WRITE_ZEROES was
> not negotiated, for a payload other than exactly one segment or an
> unknown flag. It gets VIRTIO_BLK_S_IOERR for a read-only backend, a
> backend without WRITE ZEROES and a range that is unaligned, beyond the
> capacity or above these limits.
> 
> https://virtuozzo.atlassian.net/browse/VSTOR-141684
> Feature: vhost-blk: in-kernel accelerator for virtio-blk guests
> Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
> ---
> v4:
> - one REQ_OP_WRITE_ZEROES bio per request: no chunks, no zero-page
>    fallback, no vhost_blk_retry_zeroes().
> - a backend without WRITE ZEROES and a range above
>    bdev_write_zeroes_sectors() get VIRTIO_BLK_S_IOERR.
> - commit message: the VMM advertises the feature only for a backend
>    with WRITE ZEROES and sets max_write_zeroes_sectors from its limit,
>    see the QEMU patch "vhost-blk: advertise write zeroes with the
>    backend limit".
> - patch 1 of v3 was sent separately.
> 
> v3:
> - commit message: the VMM paragraph says what a large
>    max_write_zeroes_sectors costs and that our QEMU does not advertise
>    the feature yet.
> 
> v2:
> - shorten the commit message, state parity with virtio-blk as the
>    reason and what the VMM must advertise.
> - issue chunks of at most 16 WRITE ZEROES bios and 1 GiB, try them
>    with BLKDEV_ZERO_NOFALLBACK and fall back to 4 MiB chunks of zero
>    pages.
> - complete the request with VIRTIO_BLK_S_UNSUPP when
>    VIRTIO_BLK_F_WRITE_ZEROES was not negotiated.
> 
>   drivers/vhost/blk.c | 70 +++++++++++++++++++++++++++++++++++++++++++--
>   1 file changed, 68 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/vhost/blk.c b/drivers/vhost/blk.c
> index 7975a0e63d65..d6917f0e662e 100644
> --- a/drivers/vhost/blk.c
> +++ b/drivers/vhost/blk.c
> @@ -29,7 +29,8 @@ enum {
>   			     (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
>   			     (1ULL << VIRTIO_RING_F_EVENT_IDX) |
>   			     (1ULL << VIRTIO_BLK_F_MQ) |
> -			     (1ULL << VIRTIO_BLK_F_FLUSH),
> +			     (1ULL << VIRTIO_BLK_F_FLUSH) |
> +			     (1ULL << VIRTIO_BLK_F_WRITE_ZEROES),
>   };
>   
>   /*
> @@ -284,6 +285,13 @@ static int vhost_blk_bio_make(struct vhost_blk_req *req,
>   	if (unlikely(req->bi_opf & REQ_PREFLUSH))
>   		return vhost_blk_bio_make_simple(req, bdev);
>   
> +	if ((req->bi_opf & REQ_OP_MASK) == REQ_OP_WRITE_ZEROES) {
> +		ret = vhost_blk_bio_make_simple(req, bdev);
> +		if (!ret)
> +			req->bio[0]->bi_iter.bi_size = req->len;
> +		return ret;
> +	}
> +
>   	/*
>   	 * Requests with unaligned io vectors are not supported by underlined
>   	 * block device, let's use an aligned intermediary "bounce buffer" to
> @@ -451,6 +459,51 @@ static bool vhost_blk_backend_ro(struct file *f)
>   	       bdev_read_only(I_BDEV(f->f_mapping->host));
>   }
>   
> +static int vhost_blk_parse_write_zeroes(struct vhost_blk_req *req, u8 *status)
> +{
> +	struct file *f = vhost_vq_get_backend(&req->blk_vq->vq);
> +	struct block_device *bdev = I_BDEV(f->f_mapping->host);
> +	sector_t bs_mask = (bdev_logical_block_size(bdev) >> SECTOR_SHIFT) - 1;
> +	unsigned int max_sects = bdev_write_zeroes_sectors(bdev);
> +	struct virtio_blk_discard_write_zeroes seg;
> +	struct iov_iter iter;
> +	u32 flags, nr_sects;
> +	sector_t sector;
> +
> +	*status = VIRTIO_BLK_S_UNSUPP;
> +	if (!vhost_has_feature(&req->blk_vq->vq, VIRTIO_BLK_F_WRITE_ZEROES) ||
> +	    req->len != sizeof(seg))
> +		return 0;
> +
> +	iov_iter_init(&iter, ITER_SOURCE, req->iov, req->iov_nr, req->len);
> +	if (copy_from_iter(&seg, sizeof(seg), &iter) != sizeof(seg))
> +		return -EFAULT;
> +
> +	flags = le32_to_cpu(seg.flags);
> +	if (flags & ~VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP)
> +		return 0;
> +
> +	sector = le64_to_cpu(seg.sector);
> +	nr_sects = le32_to_cpu(seg.num_sectors);
> +	if (vhost_blk_backend_ro(f) || !max_sects ||
> +	    nr_sects > UINT_MAX >> SECTOR_SHIFT || nr_sects > max_sects ||
> +	    (sector | nr_sects) & bs_mask ||
> +	    sector > bdev_nr_sectors(bdev) ||
> +	    nr_sects > bdev_nr_sectors(bdev) - sector) {
> +		*status = VIRTIO_BLK_S_IOERR;
> +		return 0;
> +	}
> +
> +	req->sector = sector;
> +	req->len = (size_t)nr_sects << SECTOR_SHIFT;
> +	req->bi_opf = REQ_OP_WRITE_ZEROES;
> +	if (!(flags & VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP))
> +		req->bi_opf |= REQ_NOUNMAP;
> +
> +	*status = VIRTIO_BLK_S_OK;
> +	return 0;
> +}
> +
>   static int vhost_blk_req_handle(struct vhost_virtqueue *vq,
>   				struct virtio_blk_outhdr *hdr,
>   				u16 head, u16 out, u16 in)
> @@ -490,7 +543,7 @@ static int vhost_blk_req_handle(struct vhost_virtqueue *vq,
>   	req->status[0].iov_base = st->iov_base + st->iov_len;
>   	req->status[0].iov_len = sizeof(status);
>   
> -	if (type == VIRTIO_BLK_T_OUT) {
> +	if (type == VIRTIO_BLK_T_OUT || type == VIRTIO_BLK_T_WRITE_ZEROES) {
>   		data_iov = vq->iov;
>   		data_nr = out;
>   	} else {
> @@ -538,6 +591,19 @@ static int vhost_blk_req_handle(struct vhost_virtqueue *vq,
>   		req->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
>   		ret = vhost_blk_req_submit(req);
>   		break;
> +	case VIRTIO_BLK_T_WRITE_ZEROES:
> +		ret = vhost_blk_parse_write_zeroes(req, &status);
> +		if (ret)
> +			break;
> +		if (status == VIRTIO_BLK_S_OK && req->len) {
> +			ret = vhost_blk_req_submit(req);
> +			break;
> +		}
> +		ret = vhost_blk_set_status(req, status);
> +		if (ret)
> +			break;
> +		vhost_add_used_and_signal(&blk->dev, vq, head, sizeof(status));
> +		break;
>   	case VIRTIO_BLK_T_GET_ID:
>   		len = min_t(size_t,
>   			    strnlen(blk->serial, VIRTIO_BLK_ID_BYTES) + 1,

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

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

* Re: [Devel] [PATCH hci-8.2 v4] drivers/vhost/blk: support VIRTIO_BLK_F_WRITE_ZEROES
  2026-11-18 11:08 [Devel] [PATCH hci-8.2 v4] drivers/vhost/blk: support VIRTIO_BLK_F_WRITE_ZEROES Alexander Ivanov
  2026-11-18 11:13 ` no-reply
  2026-11-18 11:45 ` Andrey Zhadchenko
@ 2026-11-18 15:26 ` Konstantin Khorenko
  2026-11-18 15:26 ` Konstantin Khorenko
  3 siblings, 0 replies; 5+ messages in thread
From: Konstantin Khorenko @ 2026-11-18 15:26 UTC (permalink / raw)
  To: Alexander Ivanov; +Cc: devel

Applying with these fixes on my side:
- "The VMM advertises ... and sets" -> "The VMM must advertise ... and
  set": our QEMU does not do it yet, the QEMU patch is not in hci-dev
- the cap: "and rounded down to the logical block size", as 8388607
  sectors is not a multiple of a 4K block

On Wed, 18 Nov 2026 12:08:13 +0100, Alexander Ivanov wrote:
> The VMM advertises the feature only for a backend with WRITE ZEROES
> support and sets max_write_zeroes_sectors from its limit, capped at
> 8388607 so that the range fits in one bio, and max_write_zeroes_seg
> to 1.

All v3 items are done and the code is fine.

A possible follow-up, not needed for this patch: answer
VIRTIO_BLK_S_UNSUPP instead of VIRTIO_BLK_S_IOERR when the backend has
no WRITE ZEROES (and for a bio that fails with BLK_STS_NOTSUPP). I
asked for IOERR in v3, but a Linux guest since 39722a2f2bcd8 ("block:
limit the Write Zeroes to manually writing zeroes fallback") writes
zeroes itself only on -EOPNOTSUPP, which UNSUPP maps to. With IOERR,
BLKZEROOUT in the guest fails with -EIO once a host disk drops its
limit (sd and dm can do that at runtime).

Thanks.

-- 
Konstantin Khorenko,
Virtuozzo Linux Kernel Team

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

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

* Re: [Devel] [PATCH hci-8.2 v4] drivers/vhost/blk: support VIRTIO_BLK_F_WRITE_ZEROES
  2026-11-18 11:08 [Devel] [PATCH hci-8.2 v4] drivers/vhost/blk: support VIRTIO_BLK_F_WRITE_ZEROES Alexander Ivanov
                   ` (2 preceding siblings ...)
  2026-11-18 15:26 ` Konstantin Khorenko
@ 2026-11-18 15:26 ` Konstantin Khorenko
  3 siblings, 0 replies; 5+ messages in thread
From: Konstantin Khorenko @ 2026-11-18 15:26 UTC (permalink / raw)
  To: andrey.zhadchenko, Alexander Ivanov; +Cc: devel

On Wed, 18 Nov 2026 12:08:13 +0100, Alexander Ivanov wrote:
> drivers/vhost/blk: support VIRTIO_BLK_F_WRITE_ZEROES

Applied to branch-rh10-6.12.0-211.75.1.26.x.vz10-ovz, will be released in rh10-6.12.0-211.75.1.26.3.vz10, thanks!

[1/1] drivers/vhost/blk: support VIRTIO_BLK_F_WRITE_ZEROES
      https://bitbucket.org/virtuozzocore/vzkernel.vzs/commits/3b2e28a4f6b7

-- 
Konstantin Khorenko,
Virtuozzo Linux Kernel Team


_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

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

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

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-18 11:08 [Devel] [PATCH hci-8.2 v4] drivers/vhost/blk: support VIRTIO_BLK_F_WRITE_ZEROES Alexander Ivanov
2026-11-18 11:13 ` no-reply
2026-11-18 11:45 ` Andrey Zhadchenko
2026-11-18 15:26 ` Konstantin Khorenko
2026-11-18 15:26 ` Konstantin Khorenko

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.