[Devel] [PATCH hci-8.2 v3 4/4] drivers/vhost/blk: support VIRTIO_BLK_F_DISCARD

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


virtio-blk in QEMU supports discard, while vhost-blk does not offer
VIRTIO_BLK_F_DISCARD, so a guest on vhost-blk cannot release unused
blocks of its disk.

Handle VIRTIO_BLK_T_DISCARD like a write-zeroes request: a single
segment taken from the device-readable part, the same read-only,
alignment and capacity checks, and the range sent to the backend as a
single REQ_OP_DISCARD bio. Any flag set in a discard segment completes
the request with VIRTIO_BLK_S_UNSUPP, as the spec requires for UNMAP.

The VMM must set max_discard_seg to 1: a request with more than one
segment is unsupported, and a Linux guest reads zero there as seg_max
and merges discards into multi-range requests. It must also cap
max_discard_sectors at 8388607 so that the range fits in one bio. QEMU
with the discard property on reports zero for both until
"vhost-blk: advertise discard limits in the config space", so it needs
that change.

A discard is a hint, and the guest takes the limits from the VMM, not
from the backend. So the range is not checked against the backend
discard limit: the bio gets split by it. As QEMU does, complete the
request with VIRTIO_BLK_S_OK without issuing it when the backend has no
discard support, and treat -EOPNOTSUPP from the bio as success: the
limit can drop to zero after the check, e.g. on a dm table reload, and a
stacked driver may reject a discard while still advertising one.

A discard in flight while support is lost may fail with BLK_STS_TARGET
instead, when dm or sd drop the limit on a target error. So any failure
of a discard counts as success once the backend discard limit is 0.

https://virtuozzo.atlassian.net/browse/VSTOR-141683

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

diff --git a/drivers/vhost/blk.c b/drivers/vhost/blk.c
index 0aee05b3f524..134f627adfe5 100644
--- a/drivers/vhost/blk.c
+++ b/drivers/vhost/blk.c
@@ -28,6 +28,7 @@ enum {
 	VHOST_BLK_FEATURES = VHOST_FEATURES |
 			     (1ULL << VIRTIO_BLK_F_MQ) |
 			     (1ULL << VIRTIO_BLK_F_FLUSH) |
+			     (1ULL << VIRTIO_BLK_F_DISCARD) |
 			     (1ULL << VIRTIO_BLK_F_WRITE_ZEROES),
 };
 
@@ -153,6 +154,9 @@ static void vhost_blk_req_done(struct bio *bio)
 	int err;
 
 	err = blk_status_to_errno(bio->bi_status);
+	if (err && op_is_discard(req->bi_opf) &&
+	    (err == -EOPNOTSUPP || !bdev_max_discard_sectors(bdev)))
+		err = 0;
 	if (err && (req->bi_opf & REQ_OP_MASK) == REQ_OP_WRITE_ZEROES &&
 	    !bdev_write_zeroes_sectors(bdev))
 		err = -EOPNOTSUPP;
@@ -287,7 +291,8 @@ 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) {
+	if ((req->bi_opf & REQ_OP_MASK) == REQ_OP_WRITE_ZEROES ||
+	    op_is_discard(req->bi_opf)) {
 		ret = vhost_blk_bio_make_simple(req, bdev);
 		if (!ret)
 			req->bio[0]->bi_iter.bi_size = req->len;
@@ -461,18 +466,20 @@ 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)
+static int vhost_blk_parse_segment(struct vhost_blk_req *req,
+				   struct block_device *bdev,
+				   unsigned int feature, u32 flags_mask,
+				   u32 *flags, 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;
 	struct virtio_blk_discard_write_zeroes seg;
 	struct iov_iter iter;
-	u32 flags, nr_sects;
+	u32 nr_sects;
 	sector_t sector;
 
 	*status = VIRTIO_BLK_S_UNSUPP;
-	if (!vhost_has_feature(&req->blk_vq->vq, VIRTIO_BLK_F_WRITE_ZEROES) ||
+	if (!vhost_has_feature(&req->blk_vq->vq, feature) ||
 	    req->len != sizeof(seg))
 		return 0;
 
@@ -480,8 +487,8 @@ static int vhost_blk_parse_write_zeroes(struct vhost_blk_req *req, u8 *status)
 	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)
+	*flags = le32_to_cpu(seg.flags);
+	if (*flags & ~flags_mask)
 		return 0;
 
 	sector = le64_to_cpu(seg.sector);
@@ -497,14 +504,62 @@ static int vhost_blk_parse_write_zeroes(struct vhost_blk_req *req, u8 *status)
 
 	req->sector = sector;
 	req->len = (size_t)nr_sects << SECTOR_SHIFT;
+	*status = VIRTIO_BLK_S_OK;
+	return 0;
+}
+
+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);
+	u32 flags;
+	int ret;
+
+	ret = vhost_blk_parse_segment(req, bdev, VIRTIO_BLK_F_WRITE_ZEROES,
+				      VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP,
+				      &flags, status);
+	if (ret || *status != VIRTIO_BLK_S_OK)
+		return ret;
+
 	req->bi_opf = REQ_OP_WRITE_ZEROES;
 	if (!(flags & VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP))
 		req->bi_opf |= REQ_NOUNMAP;
+	return 0;
+}
 
-	*status = VIRTIO_BLK_S_OK;
+static int vhost_blk_parse_discard(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);
+	u32 flags;
+	int ret;
+
+	ret = vhost_blk_parse_segment(req, bdev, VIRTIO_BLK_F_DISCARD,
+				      0, &flags, status);
+	if (ret || *status != VIRTIO_BLK_S_OK)
+		return ret;
+
+	req->bi_opf = REQ_OP_DISCARD;
+	if (!bdev_max_discard_sectors(bdev))
+		req->len = 0;
 	return 0;
 }
 
+static int vhost_blk_req_submit_or_complete(struct vhost_blk_req *req,
+					    u8 status)
+{
+	int ret;
+
+	if (status == VIRTIO_BLK_S_OK && req->len)
+		return vhost_blk_req_submit(req);
+
+	ret = vhost_blk_set_status(req, status);
+	if (!ret)
+		vhost_add_used_and_signal(&req->blk->dev, &req->blk_vq->vq,
+					  req->head, sizeof(status));
+	return ret;
+}
+
 static int vhost_blk_req_handle(struct vhost_virtqueue *vq,
 				struct virtio_blk_outhdr *hdr,
 				u16 head, u16 out, u16 in)
@@ -544,7 +599,8 @@ 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 || type == VIRTIO_BLK_T_WRITE_ZEROES) {
+	if (type == VIRTIO_BLK_T_OUT || type == VIRTIO_BLK_T_DISCARD ||
+	    type == VIRTIO_BLK_T_WRITE_ZEROES) {
 		data_iov = vq->iov;
 		data_nr = out;
 	} else {
@@ -592,18 +648,15 @@ 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_DISCARD:
+		ret = vhost_blk_parse_discard(req, &status);
+		if (!ret)
+			ret = vhost_blk_req_submit_or_complete(req, status);
+		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));
+		if (!ret)
+			ret = vhost_blk_req_submit_or_complete(req, status);
 		break;
 	case VIRTIO_BLK_T_GET_ID:
 		len = min_t(size_t,
-- 
2.53.0



More information about the Devel mailing list