From: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
To: andrey.zhadchenko@virtuozzo.com
Cc: devel@openvz.org
Subject: [Devel] [PATCH hci-8.2 v4] drivers/vhost/blk: support VIRTIO_BLK_F_WRITE_ZEROES
Date: Wed, 18 Nov 2026 12:08:13 +0100 [thread overview]
Message-ID: <20261118110813.3333232-1-alexander.ivanov@virtuozzo.com> (raw)
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
next reply other threads:[~2026-11-18 11:09 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-18 11:08 Alexander Ivanov [this message]
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
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=20261118110813.3333232-1-alexander.ivanov@virtuozzo.com \
--to=alexander.ivanov@virtuozzo.com \
--cc=andrey.zhadchenko@virtuozzo.com \
--cc=devel@openvz.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox