* [Devel] [PATCH hci-8.2 0/2] drivers/vhost/blk: support VIRTIO_BLK_F_DISCARD
@ 2026-11-23 8:40 Alexander Ivanov
2026-11-23 8:40 ` [Devel] [PATCH hci-8.2 1/2] drivers/vhost/blk: drop ring features already in VHOST_FEATURES Alexander Ivanov
2026-11-23 8:40 ` [Devel] [PATCH hci-8.2 2/2] drivers/vhost/blk: support VIRTIO_BLK_F_DISCARD Alexander Ivanov
0 siblings, 2 replies; 7+ messages in thread
From: Alexander Ivanov @ 2026-11-23 8:40 UTC (permalink / raw)
To: andrey.zhadchenko; +Cc: devel
vhost-blk does not offer VIRTIO_BLK_F_DISCARD, so a guest on vhost-blk
cannot release unused blocks of its disk, while virtio-blk in QEMU can.
Patch 1 drops the ring features that are already part of VHOST_FEATURES.
Patch 2 handles VIRTIO_BLK_T_DISCARD as a single-segment request sent
to the backend as one REQ_OP_DISCARD bio.
The QEMU side advertises the discard limits in the config space and is
sent separately.
https://virtuozzo.atlassian.net/browse/VSTOR-141683
Alexander Ivanov (2):
drivers/vhost/blk: drop ring features already in VHOST_FEATURES
drivers/vhost/blk: support VIRTIO_BLK_F_DISCARD
drivers/vhost/blk.c | 40 +++++++++++++++++++++++++++-------------
1 file changed, 27 insertions(+), 13 deletions(-)
--
2.53.0
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 7+ messages in thread
* [Devel] [PATCH hci-8.2 1/2] drivers/vhost/blk: drop ring features already in VHOST_FEATURES
2026-11-23 8:40 [Devel] [PATCH hci-8.2 0/2] drivers/vhost/blk: support VIRTIO_BLK_F_DISCARD Alexander Ivanov
@ 2026-11-23 8:40 ` Alexander Ivanov
2026-11-23 8:43 ` no-reply
2026-11-23 8:40 ` [Devel] [PATCH hci-8.2 2/2] drivers/vhost/blk: support VIRTIO_BLK_F_DISCARD Alexander Ivanov
1 sibling, 1 reply; 7+ messages in thread
From: Alexander Ivanov @ 2026-11-23 8:40 UTC (permalink / raw)
To: andrey.zhadchenko; +Cc: devel
VIRTIO_RING_F_INDIRECT_DESC and VIRTIO_RING_F_EVENT_IDX are part of
VHOST_FEATURES, there is no need to list them again.
https://virtuozzo.atlassian.net/browse/VSTOR-141683
Feature: vhost-blk: in-kernel accelerator for virtio-blk guests
Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
---
drivers/vhost/blk.c | 2 --
1 file changed, 2 deletions(-)
diff --git a/drivers/vhost/blk.c b/drivers/vhost/blk.c
index d6917f0e662e..816ce4cb77a2 100644
--- a/drivers/vhost/blk.c
+++ b/drivers/vhost/blk.c
@@ -26,8 +26,6 @@
enum {
VHOST_BLK_FEATURES = VHOST_FEATURES |
- (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_WRITE_ZEROES),
--
2.53.0
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 7+ messages in thread
* [Devel] [PATCH hci-8.2 2/2] drivers/vhost/blk: support VIRTIO_BLK_F_DISCARD
2026-11-23 8:40 [Devel] [PATCH hci-8.2 0/2] drivers/vhost/blk: support VIRTIO_BLK_F_DISCARD Alexander Ivanov
2026-11-23 8:40 ` [Devel] [PATCH hci-8.2 1/2] drivers/vhost/blk: drop ring features already in VHOST_FEATURES Alexander Ivanov
@ 2026-11-23 8:40 ` Alexander Ivanov
2026-11-23 8:45 ` no-reply
2026-11-23 13:36 ` Andrey Zhadchenko
1 sibling, 2 replies; 7+ messages in thread
From: Alexander Ivanov @ 2026-11-23 8:40 UTC (permalink / raw)
To: andrey.zhadchenko; +Cc: devel
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. The segment parser is shared and renamed
accordingly. Any flag set in a discard segment completes the request
with VIRTIO_BLK_S_UNSUPP, as the spec requires for UNMAP. A request
with more than one segment is unsupported, so the VMM must advertise
max_discard_seg = 1: a Linux guest reads zero there as seg_max and
merges adjacent discards.
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.
https://virtuozzo.atlassian.net/browse/VSTOR-141683
Feature: vhost-blk: in-kernel accelerator for virtio-blk guests
Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
---
drivers/vhost/blk.c | 38 +++++++++++++++++++++++++++-----------
1 file changed, 27 insertions(+), 11 deletions(-)
diff --git a/drivers/vhost/blk.c b/drivers/vhost/blk.c
index 816ce4cb77a2..811349b19500 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),
};
@@ -152,6 +153,8 @@ static void vhost_blk_req_done(struct bio *bio)
int err;
err = blk_status_to_errno(bio->bi_status);
+ if (err == -EOPNOTSUPP && op_is_discard(req->bi_opf))
+ err = 0;
if (err)
req->bio_err = err;
@@ -283,7 +286,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;
@@ -457,8 +461,10 @@ 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, u32 type,
+ u8 *status)
{
+ bool discard = type == VIRTIO_BLK_T_DISCARD;
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;
@@ -469,7 +475,8 @@ static int vhost_blk_parse_write_zeroes(struct vhost_blk_req *req, u8 *status)
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, discard ? VIRTIO_BLK_F_DISCARD :
+ VIRTIO_BLK_F_WRITE_ZEROES) ||
req->len != sizeof(seg))
return 0;
@@ -478,13 +485,14 @@ static int vhost_blk_parse_write_zeroes(struct vhost_blk_req *req, u8 *status)
return -EFAULT;
flags = le32_to_cpu(seg.flags);
- if (flags & ~VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP)
+ if (flags & ~(discard ? 0 : 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 ||
+ if (vhost_blk_backend_ro(f) ||
+ (!discard && (!max_sects || nr_sects > max_sects)) ||
+ nr_sects > UINT_MAX >> SECTOR_SHIFT ||
(sector | nr_sects) & bs_mask ||
sector > bdev_nr_sectors(bdev) ||
nr_sects > bdev_nr_sectors(bdev) - sector) {
@@ -494,9 +502,15 @@ 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;
- req->bi_opf = REQ_OP_WRITE_ZEROES;
- if (!(flags & VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP))
- req->bi_opf |= REQ_NOUNMAP;
+ if (discard) {
+ req->bi_opf = REQ_OP_DISCARD;
+ if (!bdev_max_discard_sectors(bdev))
+ req->len = 0;
+ } else {
+ 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;
@@ -541,7 +555,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 {
@@ -589,8 +604,9 @@ 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:
case VIRTIO_BLK_T_WRITE_ZEROES:
- ret = vhost_blk_parse_write_zeroes(req, &status);
+ ret = vhost_blk_parse_segment(req, type, &status);
if (ret)
break;
if (status == VIRTIO_BLK_S_OK && req->len) {
--
2.53.0
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Devel] [PATCH hci-8.2 1/2] drivers/vhost/blk: drop ring features already in VHOST_FEATURES
2026-11-23 8:40 ` [Devel] [PATCH hci-8.2 1/2] drivers/vhost/blk: drop ring features already in VHOST_FEATURES Alexander Ivanov
@ 2026-11-23 8:43 ` no-reply
0 siblings, 0 replies; 7+ messages in thread
From: no-reply @ 2026-11-23 8:43 UTC (permalink / raw)
To: Alexander Ivanov, andrey.zhadchenko; +Cc: devel
> VIRTIO_RING_F_INDIRECT_DESC and VIRTIO_RING_F_EVENT_IDX are part of
> VHOST_FEATURES, there is no need to list them again.
>
> https://virtuozzo.atlassian.net/browse/VSTOR-141683
> 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.
Carry the trailer below into the next revision and Sashiko will not review this patch again.
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
--
Sashiko AI review · https://sashiko.vzint.dev/#/patchset/20261123084014.176824-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] 7+ messages in thread
* Re: [Devel] [PATCH hci-8.2 2/2] drivers/vhost/blk: support VIRTIO_BLK_F_DISCARD
2026-11-23 8:40 ` [Devel] [PATCH hci-8.2 2/2] drivers/vhost/blk: support VIRTIO_BLK_F_DISCARD Alexander Ivanov
@ 2026-11-23 8:45 ` no-reply
2026-11-23 8:57 ` Alexander Ivanov
2026-11-23 13:36 ` Andrey Zhadchenko
1 sibling, 1 reply; 7+ messages in thread
From: no-reply @ 2026-11-23 8:45 UTC (permalink / raw)
To: Alexander Ivanov, andrey.zhadchenko; +Cc: devel
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] vhost-blk: discard of 4 GiB or more fails with IOERR, unlike
QEMU
--
commit 585dad915e7dad27e7f9cee3c2459ab7642245e2
Author: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
drivers/vhost/blk: support VIRTIO_BLK_F_DISCARD
This commit makes vhost-blk offer VIRTIO_BLK_F_DISCARD. A discard request is handled like write-zeroes: it takes a single segment, passes the same checks, and goes to the backend as one REQ_OP_DISCARD bio. A backend with no discard support, or an -EOPNOTSUPP from the bio, completes with VIRTIO_BLK_S_OK.
> diff --git a/drivers/vhost/blk.c b/drivers/vhost/blk.c
> index 816ce4cb77a2..811349b19500 100644
> --- a/drivers/vhost/blk.c
> +++ b/drivers/vhost/blk.c
[ ... ]
> @@ -478,13 +485,14 @@ static int vhost_blk_parse_write_zeroes(struct vhost_blk_req *req, u8 *status)
> - if (vhost_blk_backend_ro(f) || !max_sects ||
> - nr_sects > UINT_MAX >> SECTOR_SHIFT || nr_sects > max_sects ||
> + if (vhost_blk_backend_ro(f) ||
> + (!discard && (!max_sects || nr_sects > max_sects)) ||
> + nr_sects > UINT_MAX >> SECTOR_SHIFT ||
> (sector | nr_sects) & bs_mask ||
> sector > bdev_nr_sectors(bdev) ||
> nr_sects > bdev_nr_sectors(bdev) - sector) {
> *status = VIRTIO_BLK_S_IOERR;
[Severity: Low]
Can a valid discard of 4 GiB or more fail with VIRTIO_BLK_S_IOERR here? The commit message treats discard as a hint that is not checked against backend limits. For discard, though, the nr_sects > UINT_MAX >> SECTOR_SHIFT check still returns IOERR.
If the VMM leaves max_discard_sectors at 0, drivers/block/virtio_blk.c sets max_hw_discard_sectors to UINT_MAX. The guest can then merge discards well past this cap. With max_discard_seg = 1, the merged range goes out as a single segment. fstrim or blkdiscard on a large range would then report errors. QEMU does not fail these requests.
Should vhost_blk_bio_make() split a large discard across several bios? Alternatively, the commit message could tell the VMM to cap max_discard_sectors, as the write-zeroes commit did for its limit.
Carry the trailer below into the next revision and Sashiko will not review this patch again.
Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
--
Sashiko AI review · https://sashiko.vzint.dev/#/patchset/20261123084014.176824-1-alexander.ivanov@virtuozzo.com?part=2
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Devel] [PATCH hci-8.2 2/2] drivers/vhost/blk: support VIRTIO_BLK_F_DISCARD
2026-11-23 8:45 ` no-reply
@ 2026-11-23 8:57 ` Alexander Ivanov
0 siblings, 0 replies; 7+ messages in thread
From: Alexander Ivanov @ 2026-11-23 8:57 UTC (permalink / raw)
To: no-reply, andrey.zhadchenko; +Cc: devel
>> @@ -478,13 +485,14 @@ static int vhost_blk_parse_write_zeroes(struct vhost_blk_req *req, u8 *status)
>> - if (vhost_blk_backend_ro(f) || !max_sects ||
>> - nr_sects > UINT_MAX >> SECTOR_SHIFT || nr_sects > max_sects ||
>> + if (vhost_blk_backend_ro(f) ||
>> + (!discard && (!max_sects || nr_sects > max_sects)) ||
>> + nr_sects > UINT_MAX >> SECTOR_SHIFT ||
>> (sector | nr_sects) & bs_mask ||
>> sector > bdev_nr_sectors(bdev) ||
>> nr_sects > bdev_nr_sectors(bdev) - sector) {
>> *status = VIRTIO_BLK_S_IOERR;
> [Severity: Low]
> Can a valid discard of 4 GiB or more fail with VIRTIO_BLK_S_IOERR here? The commit message treats discard as a hint that is not checked against backend limits. For discard, though, the nr_sects > UINT_MAX >> SECTOR_SHIFT check still returns IOERR.
>
> If the VMM leaves max_discard_sectors at 0, drivers/block/virtio_blk.c sets max_hw_discard_sectors to UINT_MAX. The guest can then merge discards well past this cap. With max_discard_seg = 1, the merged range goes out as a single segment. fstrim or blkdiscard on a large range would then report errors. QEMU does not fail these requests.
>
> Should vhost_blk_bio_make() split a large discard across several bios? Alternatively, the commit message could tell the VMM to cap max_discard_sectors, as the write-zeroes commit did for its limit.
>
> Carry the trailer below into the next revision and Sashiko will not review this patch again.
>
> Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>
A Linux guest cannot send a discard of 4 GiB or more:
blk_queue_get_max_sectors()
caps discard requests at UINT_MAX >> SECTOR_SHIFT, whatever
max_discard_sectors says,
so fstrim/blkdiscard will not hit IOERR. QEMU never advertises 0 there
either, and it fails a
discard above its advertised limit with VIRTIO_BLK_S_IOERR too.
Splitting is not worth the
complexity.
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Devel] [PATCH hci-8.2 2/2] drivers/vhost/blk: support VIRTIO_BLK_F_DISCARD
2026-11-23 8:40 ` [Devel] [PATCH hci-8.2 2/2] drivers/vhost/blk: support VIRTIO_BLK_F_DISCARD Alexander Ivanov
2026-11-23 8:45 ` no-reply
@ 2026-11-23 13:36 ` Andrey Zhadchenko
1 sibling, 0 replies; 7+ messages in thread
From: Andrey Zhadchenko @ 2026-11-23 13:36 UTC (permalink / raw)
To: Alexander Ivanov; +Cc: devel
The idea looks good itself but vhost_blk_parse_write_zeroes() becomes
completely unreadable with these ternary-inside-coditions approach (and
many condiitons growing even more). Please rework this somehow.
On 11/23/26 09:40, Alexander Ivanov wrote:
> 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. The segment parser is shared and renamed
> accordingly. Any flag set in a discard segment completes the request
> with VIRTIO_BLK_S_UNSUPP, as the spec requires for UNMAP. A request
> with more than one segment is unsupported, so the VMM must advertise
> max_discard_seg = 1: a Linux guest reads zero there as seg_max and
> merges adjacent discards.
>
> 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.
>
> https://virtuozzo.atlassian.net/browse/VSTOR-141683
> Feature: vhost-blk: in-kernel accelerator for virtio-blk guests
> Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
> ---
> drivers/vhost/blk.c | 38 +++++++++++++++++++++++++++-----------
> 1 file changed, 27 insertions(+), 11 deletions(-)
>
> diff --git a/drivers/vhost/blk.c b/drivers/vhost/blk.c
> index 816ce4cb77a2..811349b19500 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),
> };
>
> @@ -152,6 +153,8 @@ static void vhost_blk_req_done(struct bio *bio)
> int err;
>
> err = blk_status_to_errno(bio->bi_status);
> + if (err == -EOPNOTSUPP && op_is_discard(req->bi_opf))
> + err = 0;
> if (err)
> req->bio_err = err;
>
> @@ -283,7 +286,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;
> @@ -457,8 +461,10 @@ 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, u32 type,
> + u8 *status)
> {
> + bool discard = type == VIRTIO_BLK_T_DISCARD;
> 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;
> @@ -469,7 +475,8 @@ static int vhost_blk_parse_write_zeroes(struct vhost_blk_req *req, u8 *status)
> 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, discard ? VIRTIO_BLK_F_DISCARD :
> + VIRTIO_BLK_F_WRITE_ZEROES) ||
> req->len != sizeof(seg))
> return 0;
>
> @@ -478,13 +485,14 @@ static int vhost_blk_parse_write_zeroes(struct vhost_blk_req *req, u8 *status)
> return -EFAULT;
>
> flags = le32_to_cpu(seg.flags);
> - if (flags & ~VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP)
> + if (flags & ~(discard ? 0 : 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 ||
> + if (vhost_blk_backend_ro(f) ||
> + (!discard && (!max_sects || nr_sects > max_sects)) ||
> + nr_sects > UINT_MAX >> SECTOR_SHIFT ||
> (sector | nr_sects) & bs_mask ||
> sector > bdev_nr_sectors(bdev) ||
> nr_sects > bdev_nr_sectors(bdev) - sector) {
> @@ -494,9 +502,15 @@ 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;
> - req->bi_opf = REQ_OP_WRITE_ZEROES;
> - if (!(flags & VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP))
> - req->bi_opf |= REQ_NOUNMAP;
> + if (discard) {
> + req->bi_opf = REQ_OP_DISCARD;
> + if (!bdev_max_discard_sectors(bdev))
> + req->len = 0;
> + } else {
> + 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;
> @@ -541,7 +555,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 {
> @@ -589,8 +604,9 @@ 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:
> case VIRTIO_BLK_T_WRITE_ZEROES:
> - ret = vhost_blk_parse_write_zeroes(req, &status);
> + ret = vhost_blk_parse_segment(req, type, &status);
> if (ret)
> break;
> if (status == VIRTIO_BLK_S_OK && req->len) {
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-11-23 13:39 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-23 8:40 [Devel] [PATCH hci-8.2 0/2] drivers/vhost/blk: support VIRTIO_BLK_F_DISCARD Alexander Ivanov
2026-11-23 8:40 ` [Devel] [PATCH hci-8.2 1/2] drivers/vhost/blk: drop ring features already in VHOST_FEATURES Alexander Ivanov
2026-11-23 8:43 ` no-reply
2026-11-23 8:40 ` [Devel] [PATCH hci-8.2 2/2] drivers/vhost/blk: support VIRTIO_BLK_F_DISCARD Alexander Ivanov
2026-11-23 8:45 ` no-reply
2026-11-23 8:57 ` Alexander Ivanov
2026-11-23 13:36 ` Andrey Zhadchenko
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.