* [Devel] [PATCH hci-8.2] drivers/vhost/blk: return UNSUPP for write zeroes on a backend without it
@ 2026-11-24 13:12 Alexander Ivanov
2026-11-24 13:44 ` Konstantin Khorenko
2026-12-01 0:02 ` no-reply
0 siblings, 2 replies; 3+ messages in thread
From: Alexander Ivanov @ 2026-11-24 13:12 UTC (permalink / raw)
To: andrey.zhadchenko; +Cc: devel
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
Linux 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 the backend has no WRITE ZEROES support,
both when the request is parsed and when the bio fails. A request in
flight while support is lost fails with BLK_STS_NOTSUPP after a dm table
swap, or with BLK_STS_TARGET when dm or sd drop the limit on a target
error, so, 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. Users
that submit WRITE ZEROES bios directly, such as dm-kcopyd, still fail. Do
not check the range against the limit of the backend: the block layer
splits a WRITE ZEROES bio by it.
A VMM may therefore offer VIRTIO_BLK_F_WRITE_ZEROES without checking
the limit of the backend, and need not cap max_write_zeroes_sectors at
it. Offering it on a backend that never supports WRITE ZEROES still
costs the guest: it never withdraws the limit it read at probe, so
every WRITE ZEROES makes a round trip to the host before the fallback,
and dm-kcopyd, which zeroes with WRITE ZEROES whenever the limit is
non-zero, fails instead of writing zeroes itself.
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>
---
drivers/vhost/blk.c | 19 +++++++++++++++----
1 file changed, 15 insertions(+), 4 deletions(-)
diff --git a/drivers/vhost/blk.c b/drivers/vhost/blk.c
index d6917f0e662e..8ef2e530bf1a 100644
--- a/drivers/vhost/blk.c
+++ b/drivers/vhost/blk.c
@@ -154,6 +154,9 @@ static void vhost_blk_req_done(struct bio *bio)
int err;
err = blk_status_to_errno(bio->bi_status);
+ if (err && bio_op(bio) == REQ_OP_WRITE_ZEROES &&
+ !bdev_write_zeroes_sectors(bio->bi_bdev))
+ err = -EOPNOTSUPP;
if (err)
req->bio_err = err;
@@ -464,7 +467,6 @@ 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;
@@ -485,8 +487,8 @@ static int vhost_blk_parse_write_zeroes(struct vhost_blk_req *req, u8 *status)
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) ||
+ nr_sects > UINT_MAX >> SECTOR_SHIFT ||
(sector | nr_sects) & bs_mask ||
sector > bdev_nr_sectors(bdev) ||
nr_sects > bdev_nr_sectors(bdev) - sector) {
@@ -494,6 +496,9 @@ static int vhost_blk_parse_write_zeroes(struct vhost_blk_req *req, u8 *status)
return 0;
}
+ if (!bdev_write_zeroes_sectors(bdev))
+ return 0;
+
req->sector = sector;
req->len = (size_t)nr_sects << SECTOR_SHIFT;
req->bi_opf = REQ_OP_WRITE_ZEROES;
@@ -733,7 +738,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
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Devel] [PATCH hci-8.2] drivers/vhost/blk: return UNSUPP for write zeroes on a backend without it
2026-11-24 13:12 [Devel] [PATCH hci-8.2] drivers/vhost/blk: return UNSUPP for write zeroes on a backend without it Alexander Ivanov
@ 2026-11-24 13:44 ` Konstantin Khorenko
2026-12-01 0:02 ` no-reply
1 sibling, 0 replies; 3+ messages in thread
From: Konstantin Khorenko @ 2026-11-24 13:44 UTC (permalink / raw)
To: Alexander Ivanov; +Cc: devel
I won't apply this version, please send v2 with the changes below.
On Tue, 24 Nov 2026 14:12:59 +0100, Alexander Ivanov wrote:
> A VMM may therefore offer VIRTIO_BLK_F_WRITE_ZEROES without checking
> the limit of the backend, and need not cap max_write_zeroes_sectors at
> it.
- The code is fine. Change the VMM advice: offer the feature only when
the backend supports WRITE ZEROES at setup, just do not cap the limit.
- Say in the message that this replaces the contract of 3b2e28a4f6b7
and add its Fixes: line.
- Give the dropped nr_sects > max_sects check its own paragraph, and
keep each paragraph to four sentences.
- Send it in one series with the DISCARD v2, on top of it or under it,
with one way to test the op in vhost_blk_req_done().
The next sentence of the message already shows why the advice is
wrong: on a backend that never supports WRITE ZEROES, the guest keeps
the limit it read at probe for good. dm_kcopyd_zero() then picks WRITE
ZEROES, so dm-thin zeroing of new blocks and dm-snapshot fail in the
guest, where today they work because the feature is not offered. The
QEMU side is not written yet, so this sentence is what it will follow.
> splits a WRITE ZEROES bio by it.
3b2e28a4f6b7 ("drivers/vhost/blk: support VIRTIO_BLK_F_WRITE_ZEROES")
documented IOERR for a backend without WRITE ZEROES and a limit the
VMM sets from the backend; it is in released kernels, so this patch
needs
Fixes: 3b2e28a4f6b7 ("drivers/vhost/blk: support VIRTIO_BLK_F_WRITE_ZEROES")
Dropping the nr_sects > max_sects check is a change of its own: the
guest sees UINT_MAX when the VMM leaves the limit 0, so before this
patch every request above the backend limit got IOERR. That reason
belongs in its own paragraph. The second paragraph has five sentences
now, and the -EOPNOTSUPP outcome and the dm-kcopyd failure are each
said twice in the message.
The DISCARD series (still waiting for your answer to Andrey) rewrites
the same condition in vhost_blk_parse_write_zeroes() and adds an op
test to vhost_blk_req_done() with op_is_discard(req->bi_opf), while
this patch uses bio_op(bio). The two do not apply on each other, so
please put them in one series and use one form.
--
Konstantin Khorenko,
Virtuozzo Linux Kernel Team
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Devel] [PATCH hci-8.2] drivers/vhost/blk: return UNSUPP for write zeroes on a backend without it
2026-11-24 13:12 [Devel] [PATCH hci-8.2] drivers/vhost/blk: return UNSUPP for write zeroes on a backend without it Alexander Ivanov
2026-11-24 13:44 ` Konstantin Khorenko
@ 2026-12-01 0:02 ` no-reply
1 sibling, 0 replies; 3+ messages in thread
From: no-reply @ 2026-12-01 0:02 UTC (permalink / raw)
To: Alexander Ivanov, andrey.zhadchenko; +Cc: devel
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Low] vhost-blk: bug-fix commit lacks a Fixes: trailer for
3b2e28a4f6b7
- [Low] vhost-blk: WRITE ZEROES unsupported check reads bio->bi_bdev,
not the backend bdev
--
commit 988f2a38480141126e0d144196334015b35c2cb4
Author: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
> drivers/vhost/blk: return UNSUPP for write zeroes on a backend without it
>
> 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
> Linux 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 the backend has no WRITE ZEROES support,
> both when the request is parsed and when the bio fails. A request in
> flight while support is lost fails with BLK_STS_NOTSUPP after a dm table
> swap, or with BLK_STS_TARGET when dm or sd drop the limit on a target
> error, so, 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. Users
> that submit WRITE ZEROES bios directly, such as dm-kcopyd, still fail. Do
> not check the range against the limit of the backend: the block layer
> splits a WRITE ZEROES bio by it.
>
> A VMM may therefore offer VIRTIO_BLK_F_WRITE_ZEROES without checking
> the limit of the backend, and need not cap max_write_zeroes_sectors at
> it. Offering it on a backend that never supports WRITE ZEROES still
> costs the guest: it never withdraws the limit it read at probe, so
> every WRITE ZEROES makes a round trip to the host before the fallback,
> and dm-kcopyd, which zeroes with WRITE ZEROES whenever the limit is
> non-zero, fails instead of writing zeroes itself.
>
> 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>
[Severity: Low]
This fixes behavior introduced by an earlier commit: it undoes the
IOERR-on-zero-limit check and the blanket bio failure to IOERR mapping.
Should this include:
Fixes: 3b2e28a4f6b7 ("drivers/vhost/blk: support VIRTIO_BLK_F_WRITE_ZEROES")
> diff --git a/drivers/vhost/blk.c b/drivers/vhost/blk.c
> index d6917f0e662ef..8ef2e530bf1a4 100644
> --- a/drivers/vhost/blk.c
> +++ b/drivers/vhost/blk.c
> @@ -154,6 +154,9 @@ static void vhost_blk_req_done(struct bio *bio)
> int err;
>
> err = blk_status_to_errno(bio->bi_status);
> + if (err && bio_op(bio) == REQ_OP_WRITE_ZEROES &&
> + !bdev_write_zeroes_sectors(bio->bi_bdev))
> + err = -EOPNOTSUPP;
> if (err)
> req->bio_err = err;
[Severity: Low]
Is bio->bi_bdev the backend bdev here? vhost_blk_bio_make_simple()
allocates the bio against the backend bdev, and
vhost_blk_parse_write_zeroes() tests that same bdev. But a stacking
driver that remaps bios in place with bio_set_dev() (md raid0/linear with
iostats disabled, bcache) changes bi_bdev to a member device before
completion.
The completion check then tests a lower device's limit, so a failed WRITE
ZEROES can be reported as IOERR when it should be UNSUPP, or as UNSUPP
when it was a real I/O error. Would it be better to save the backend bdev
(or the result of the check) in the request at submit time and test that
in vhost_blk_req_done() or the host-kick handler?
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/20261124131259.498924-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] 3+ messages in thread
end of thread, other threads:[~2026-12-01 0:04 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-24 13:12 [Devel] [PATCH hci-8.2] drivers/vhost/blk: return UNSUPP for write zeroes on a backend without it Alexander Ivanov
2026-11-24 13:44 ` Konstantin Khorenko
2026-12-01 0:02 ` no-reply
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox