From: Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>
To: alexander.ivanov@virtuozzo.com
Cc: andrey.zhadchenko@virtuozzo.com, svt-core@virtuozzo.com
Subject: Re: [PATCH hci-8.2] vhost-blk: advertise discard limits in the config space
Date: Mon, 23 Nov 2026 19:35:50 +0200 [thread overview]
Message-ID: <20261123173550.104355-1-andrey.drobyshev@virtuozzo.com> (raw)
In-Reply-To: <20261123084134.177261-1-alexander.ivanov@virtuozzo.com>
On Mon, Nov 23, 2026 at 09:41:34 +0100, Alexander Ivanov wrote:
> vhost-blk in the kernel now handles VIRTIO_BLK_T_DISCARD, but with the
> discard property on, the config space reports zero for max_discard_seg,
> max_discard_sectors and discard_sector_alignment. A Linux guest reads a
> zero max_discard_seg as seg_max and merges adjacent discards into one
> multi-segment request, which the kernel rejects with
> VIRTIO_BLK_S_UNSUPP.
>
> Advertise one segment per request, as virtio-blk does, a range that
> fits in the single bio the kernel issues, and the logical block size as
> the alignment. Unlike write zeroes, the backend discard limit is not
> read: the kernel splits the bio by it and completes a discard with
> VIRTIO_BLK_S_OK when the backend cannot do one.
>
> The property stays off by default. A kernel without discard support in
> vhost-blk does not offer the feature, so it is not negotiated there.
(3) [low] discard=on is dropped without any message on a kernel that
has no vhost-blk discard (masked by vhost_get_features()). Same
consequence as for write zeroes: a VM that negotiated the feature on an
upgraded node fails to migrate to a node that is not upgraded yet, and
only at virtio_load() with the generic "Features 0x... unsupported".
At least a warning at realize is needed; OnOffAuto with "on" failing
realize is the conventional fix.
>
> https://virtuozzo.atlassian.net/browse/VSTOR-141683
>
> Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
> ---
> hw/block/vhost-blk.c | 8 +++++++-
> 1 file changed, 7 insertions(+), 1 deletion(-)
>
> diff --git a/hw/block/vhost-blk.c b/hw/block/vhost-blk.c
> index 153cc7604d0..3f940e965ad 100644
> --- a/hw/block/vhost-blk.c
> +++ b/hw/block/vhost-blk.c
> @@ -812,6 +812,13 @@ static void vhost_blk_update_config(VirtIODevice *vdev, uint8_t *config)
> blkcfg.physical_block_exp = get_physical_block_exp(conf);
> blkcfg.alignment_offset = 0;
> virtio_stw_p(vdev, &blkcfg.num_queues, s->conf.num_queues);
> + if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_DISCARD)) {
(4) [nit] Gated on the property, not on the offered feature. On a
kernel without vhost-blk discard the bit is masked by
vhost_get_features(), but the three fields are still filled in.
Harmless by the spec; single source of truth is:
if (virtio_host_has_feature(vdev, VIRTIO_BLK_F_DISCARD)) {
(5) [nit] Discard is offered on a read-only backend, write zeroes is
not. The kernel fails such a discard with VIRTIO_BLK_S_IOERR. Linux
does not send discards to a read-only disk, but the two features should
behave the same.
> + virtio_stl_p(vdev, &blkcfg.max_discard_sectors,
> + UINT32_MAX >> BDRV_SECTOR_BITS);
(1) [medium, not measured] 4 GiB per request, while dm-qcow2 takes one
cluster per discard.
qcow2_io_hints() sets max_hw_discard_sectors to the cluster size; a
dm-qcow2 device with 1M clusters reports discard_max_bytes=1048576 and
nr_requests=256. The kernel does not check a discard against the
backend limit, it issues one bio and lets the block layer split it.
The split is synchronous in the submitter: vhost_blk_bio_send() calls
submit_bio() from the request handler, and bio_split_discard() /
bio_submit_split() cut one piece at a time and resubmit the rest in the
same context.
So one 4 GiB guest discard becomes about 4096 cluster discards issued
by the vhost worker, which sleeps on tag allocation once 256 of them
are queued. While it sleeps it does not serve the other virtqueues,
and with the default num-threads=1 that is every queue of the disk:
reads and writes wait behind the discard. The same backlog has to
drain in vhost_blk_stop() (VHOST_BLK_SET_BACKEND with fd -1 waits for
in-flight requests) under the BQL, which stretches pause, migration
downtime and qemu-update. How long a cluster discard takes on
dm-qcow2 was not measured, so the size of the stall is unknown; the
4096:1 ratio is from the code.
The commit message argues the limit need not be read because the
kernel splits by it. That makes the request correct, not cheap.
Advertising the backend limit lets the guest do the splitting, where
discards interleave with other I/O in its block layer:
max_discard_sectors = MIN(discard_max_bytes >> BDRV_SECTOR_BITS,
UINT32_MAX >> BDRV_SECTOR_BITS);
with queue/discard_max_bytes read the same way the write-zeroes patch
reads write_zeroes_max_bytes, and the current constant as the fallback
when the value is 0 or unreadable (the kernel completes a discard with
VIRTIO_BLK_S_OK when the backend has none). Unlike write zeroes, the
kernel does not enforce this limit, so a different value after
migration is harmless: a larger request is still split by the host.
Reading it from the host has no ABI cost here.
> + virtio_stl_p(vdev, &blkcfg.discard_sector_alignment,
> + blk_size >> BDRV_SECTOR_BITS);
(2) [low] 512 bytes, while the backend granularity is 4096.
blk_size is hard-coded to BDRV_SECTOR_SIZE for vhost-blk, so this is
always 1 sector. dm-qcow2 reports discard_granularity=4096 (the block
size of the filesystem under the image). "As virtio-blk does" does not
hold for this field: virtio-blk reports conf->discard_granularity taken
from the backend and falls back to the logical block size only when it
is unknown or report-discard-granularity is off. A virtio-blk disk on
a qcow2 image shows 4096 in the guest's queue/discard_granularity.
Consequences:
a) A discard smaller than a filesystem block frees nothing on the
host. For a partial cluster dm-qcow2 calls qcow2_punch_hole() on
the byte range (issue_discard()), so each such request costs a
fallocate() and releases no space.
b) The guest chunks large ranges at an odd size. Linux uses
round_down(UINT_MAX, discard_granularity) >> 9 per bio
(bio_discard_limit()), which is 8388607 sectors with a 512-byte
granularity. The kernel rejects a discard with
(sector | nr_sects) & bs_mask, so on a backend with a 4K logical
block size every full-size chunk, and every chunk after it since
the start is now odd, gets VIRTIO_BLK_S_IOERR: fstrim and
blkdiscard fail on any range of 4 GiB or more. With 4096
advertised the guest chunks at 8388600 sectors and stays aligned.
dm-qcow2 takes its logical block size from the block device under
the image's filesystem, so this needs a 4Kn disk on the host.
Read queue/discard_granularity and use it, with the logical block size
as the lower bound:
virtio_stl_p(vdev, &blkcfg.discard_sector_alignment,
MAX(blk_size, granularity) >> BDRV_SECTOR_BITS);
and round max_discard_sectors down to the same value. If no sysfs
read is wanted for this field, MAX(blk_size, 4096) already covers both
consequences.
> + virtio_stl_p(vdev, &blkcfg.max_discard_seg, 1);
> + }
> if (s->max_write_zeroes_sectors) {
> virtio_stl_p(vdev, &blkcfg.max_write_zeroes_sectors,
> s->max_write_zeroes_sectors);
> @@ -832,7 +839,6 @@ static const Property vhost_blk_properties[] = {
> DEFINE_PROP_UINT16("num-threads", VHostBlk, conf.num_threads, 1),
> DEFINE_PROP_BIT64("flush", VHostBlk, host_features,
> VIRTIO_BLK_F_FLUSH, true),
> -/* Discard not yet implemented in kernel module */
> DEFINE_PROP_BIT64("discard", VHostBlk, host_features,
> VIRTIO_BLK_F_DISCARD, false),
> DEFINE_PROP_BIT64("write-zeroes", VHostBlk, host_features,
Summary: (1) and (2) are the advertised values: the maximum is 4096
times what dm-qcow2 takes per discard, and the alignment is finer than
its granularity. (3) affects migration. (4)-(5) are nits.
next prev parent reply other threads:[~2026-11-23 17:36 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-23 8:41 Alexander Ivanov
2026-11-23 13:20 ` Andrey Zhadchenko
2026-11-23 17:35 ` Andrey Drobyshev [this message]
2026-11-26 9:35 ` Alexander Ivanov
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=20261123173550.104355-1-andrey.drobyshev@virtuozzo.com \
--to=andrey.drobyshev@virtuozzo.com \
--cc=alexander.ivanov@virtuozzo.com \
--cc=andrey.zhadchenko@virtuozzo.com \
--cc=svt-core@virtuozzo.com \
/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 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.