[Devel] [PATCH hci-8.2 v3 3/4] drivers/vhost/blk: return UNSUPP for write zeroes on a backend without it

Alexander Ivanov alexander.ivanov at virtuozzo.com
Wed Nov 25 14:57:02 MSK 2026


The guest reads max_write_zeroes_sectors once at probe, but the WRITE
ZEROES limit of the backend can change while it runs: a dm table reload
with a target that does not pass WRITE ZEROES through drops it to 0.
vhost-blk then fails every WRITE ZEROES with VIRTIO_BLK_S_IOERR, and a
Linux guest since v6.11 returns -EIO to the caller instead of writing
zeroes itself. The VMM cannot work around it: the limit it samples may
already be stale, and the destination of a migration must offer the
features the guest has negotiated whatever the limit there is.

Answer VIRTIO_BLK_S_UNSUPP when a WRITE ZEROES bio fails with
BLK_STS_NOTSUPP, whatever the limit of the backend: submit_bio_noacct()
fails a WRITE ZEROES bio with BLK_STS_NOTSUPP once the limit is 0, and a
backend that rejects the command while advertising it gets UNSUPP
instead of IOERR as well. A request in flight while support is lost may
fail with BLK_STS_TARGET instead, when dm or sd drop the limit on a
target error. As blkdev_issue_write_zeroes() does, treat any failure of
a WRITE ZEROES bio as unsupported once the limit of the backend is 0.

The guest gets -EOPNOTSUPP instead of -EIO, and blkdev_issue_zeroout()
falls back to writing zeroes unless called with BLKDEV_ZERO_NOFALLBACK.
dm-kcopyd submits WRITE ZEROES without such a fallback and still fails,
now with -EOPNOTSUPP.

This replaces the contract of 3b2e28a4f6b7, which answered
VIRTIO_BLK_S_IOERR for a backend without WRITE ZEROES. The VMM must
still offer VIRTIO_BLK_F_WRITE_ZEROES only for a backend that supports
it at setup: the guest never learns that the limit is 0, and dm-kcopyd
would fail dm-thin zeroing of new blocks and dm-snapshot.

Fixes: 3b2e28a4f6b7 ("drivers/vhost/blk: support VIRTIO_BLK_F_WRITE_ZEROES")
https://virtuozzo.atlassian.net/browse/VSTOR-141684

Feature: vhost-blk: in-kernel accelerator for virtio-blk guests
Signed-off-by: Alexander Ivanov <alexander.ivanov at virtuozzo.com>
---
 drivers/vhost/blk.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/drivers/vhost/blk.c b/drivers/vhost/blk.c
index 86b526c3f571..0aee05b3f524 100644
--- a/drivers/vhost/blk.c
+++ b/drivers/vhost/blk.c
@@ -149,9 +149,13 @@ static inline int vhost_blk_set_status(struct vhost_blk_req *req, u8 status)
 static void vhost_blk_req_done(struct bio *bio)
 {
 	struct vhost_blk_req *req = bio->bi_private;
+	struct block_device *bdev = I_BDEV(req->blk->backend->f_mapping->host);
 	int err;
 
 	err = blk_status_to_errno(bio->bi_status);
+	if (err && (req->bi_opf & REQ_OP_MASK) == REQ_OP_WRITE_ZEROES &&
+	    !bdev_write_zeroes_sectors(bdev))
+		err = -EOPNOTSUPP;
 	if (err)
 		req->bio_err = err;
 
@@ -730,7 +734,13 @@ static void vhost_blk_handle_host_kick(struct vhost_work *work)
 		}
 		vhost_blk_req_cleanup(req);
 
-		status = req->bio_err == 0 ?  VIRTIO_BLK_S_OK : VIRTIO_BLK_S_IOERR;
+		if (!req->bio_err)
+			status = VIRTIO_BLK_S_OK;
+		else if (req->bio_err == -EOPNOTSUPP &&
+			 (req->bi_opf & REQ_OP_MASK) == REQ_OP_WRITE_ZEROES)
+			status = VIRTIO_BLK_S_UNSUPP;
+		else
+			status = VIRTIO_BLK_S_IOERR;
 		if (vhost_blk_set_status(req, status)) {
 			vhostblk_vq_err(blk, vq, "Failed to write status");
 		} else {
-- 
2.53.0



More information about the Devel mailing list