From: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
To: Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>
Cc: andrey.zhadchenko@virtuozzo.com, svt-core@virtuozzo.com
Subject: Re: [PATCH hci-8.2 v2] vhost-blk: advertise write zeroes with the backend limit
Date: Tue, 24 Nov 2026 14:32:16 +0100 [thread overview]
Message-ID: <3db56d05-bf1f-47bc-a2d0-79930709441e@virtuozzo.com> (raw)
In-Reply-To: <20261123163311.76889-1-andrey.drobyshev@virtuozzo.com>
>> +static int vhost_blk_get_write_zeroes_sectors(VHostBlk *s)
>> +{
>> + g_autofree char *dev = NULL;
>> + g_autofree char *part = NULL;
>> + g_autofree char *path = NULL;
>> + g_autofree char *val = NULL;
>> + uint64_t bytes;
>> +
>> + dev = g_strdup_printf("/sys/dev/block/%u:%u",
>> + major(s->backend_rdev), minor(s->backend_rdev));
>> + part = g_strdup_printf("%s/partition", dev);
>> + path = g_strdup_printf("%s/%squeue/write_zeroes_max_bytes", dev,
>> + g_file_test(part, G_FILE_TEST_EXISTS) ? "../" : "");
>> + if (!g_file_get_contents(path, &val, NULL, NULL) ||
>> + qemu_strtou64(g_strstrip(val), NULL, 10, &bytes)) {
>> + return -1;
>> + }
> (6) [nit] Bare -1 return, result stored in *s as a side effect, GError
> from g_file_get_contents() dropped. The caller cannot say whether the
> file is missing, unreadable or unparsable. Usual shape:
>
> static bool vhost_blk_get_write_zeroes_sectors(VHostBlk *s,
> uint32_t *sectors,
> Error **errp)
>
> with the caller reporting via warn_report_err(), or propagating the
> error, see (2).
Since we send sectors pointer as an argument of the function, we
no longer need the VHostBlk pointer.
Changed to:
static bool vhost_blk_get_write_zeroes_sectors(dev_t rdev, uint32_t
*sectors,
Error **errp)
>> +
>> + /* The kernel issues the whole range as one bio */
>> + s->max_write_zeroes_sectors = MIN(bytes >> BDRV_SECTOR_BITS,
>> + UINT32_MAX >> BDRV_SECTOR_BITS);
> (4) [low] The limit is not rounded down to the logical block size of
> the backend. The kernel commit requires it:
>
> The VMM must advertise the feature only for a backend with WRITE
> ZEROES support and set max_write_zeroes_sectors from its limit,
> capped at 8388607 so that the range fits in one bio and rounded
> down to the logical block size, and max_write_zeroes_seg to 1.
>
> vhost_blk_parse_write_zeroes() fails a request with
> (sector | nr_sects) & bs_mask. 8388607 is odd, so whenever the cap
> applies on a backend with a 4K logical block size, a guest that chunks
> a large range at the advertised limit gets VIRTIO_BLK_S_IOERR on every
> chunk. The cap applies to common limits: loop and nbd report exactly
> 4294966784 bytes. dm-qcow2 takes its logical block size from the block
> device under the image's filesystem (qcow2_io_hints()), so a 4Kn host
> disk gives 4096.
>
> logical_block_size is still hard-coded to BDRV_SECTOR_SIZE for the
> guest, so 4Kn backends have other problems, but this part is cheap:
>
> int lbs;
>
> if (ioctl(s->backend_fd, BLKSSZGET, &lbs) < 0) {
> ...
> }
> sectors = MIN(bytes >> BDRV_SECTOR_BITS,
> UINT32_MAX >> BDRV_SECTOR_BITS);
> sectors = QEMU_ALIGN_DOWN(sectors, lbs >> BDRV_SECTOR_BITS);
Agreed, fixed. The limit is now rounded down to the logical block size
of the backend, taken with BLKSSZGET on the backend fd. It also covers
the uncapped case: loop reports UINT_MAX >> 9 sectors, which is odd as
well. If BLKSSZGET fails, the feature is not offered, as when the sysfs
limit cannot be read. A limit that rounds down to zero hits the existing
"does not support write zeroes" path.
>> + return 0;
>> +}
>> +
>> static bool vhost_blk_open_backend(VHostBlk *s, Error **errp)
>> {
>> ERRP_GUARD();
>> @@ -562,6 +586,16 @@ static bool vhost_blk_open_backend(VHostBlk *s, Error **errp)
>> s->conf.devpath);
>> goto fail;
>> }
>> +
>> + if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_WRITE_ZEROES)) {
>> + if (vhost_blk_get_write_zeroes_sectors(s)) {
>> + warn_report("vhost-blk: unable to read write zeroes limit "
>> + "of '%s'", s->conf.devpath);
>> + } else if (!s->max_write_zeroes_sectors) {
>> + warn_report("vhost-blk: '%s' does not support write zeroes",
>> + s->conf.devpath);
>> + }
>> + }
> (1) [major] The limit is sampled once here, but the table of the open
> device is replaced at runtime, and the kernel checks the live limit.
>
> Every backup of a running VM does this. ploop-backup calls
> ploop_tg_init(path, "push_backup", ...), which does (ploop lib/dm.c,
> paraphrased):
>
> 1 dmsetup reload ploopXXX --table "0 ... error"
> 2 dmsetup create ploopXXX.push_backup ... qcow2 ...
> 3 dmsetup reload ploopXXX --table \
> "0 ... push_backup ... /dev/mapper/ploopXXX.push_backup"
> 4 dmsetup resume ploopXXX
>
> The dev_t QEMU has open stops being a qcow2 target and becomes
> push_backup stacked over a new dm-qcow2 device. dm-push-backup sets
> only num_discard_bios (same for dm-tracking, which ploop copy stacks
> the same way), and dm_table_supports_write_zeroes() requires
> num_write_zeroes_bios on every target. The limit of ploopXXX therefore
> drops to 0 for the duration of the backup regardless of dm-qcow2
> underneath. Any target without it shows the effect: a loop device
> reports 4294966784, dm-delay on top of it reports 0.
>
> Consequences:
>
> a) Running guest. QEMU keeps advertising the value from realize, the
> guest keeps sending WRITE ZEROES, and the kernel does
>
> max_sects = bdev_write_zeroes_sectors(bdev);
> ...
> if (vhost_blk_backend_ro(f) || !max_sects || ...
> *status = VIRTIO_BLK_S_IOERR;
>
> Every such request fails until the backup ends. A 6.12 guest does
> not hide it: blkdev_issue_zeroout() falls back to writing zero
> pages only on -EOPNOTSUPP; -EIO goes to the caller (filesystem,
> mkfs, fallocate on the block device).
>
> b) QEMU realized while the stack is in place. qemu-update re-runs
> realize in the new process, reads 0, and vhost_blk_get_features()
> clears a bit the guest has already negotiated. virtio_load() then
> fails with "Features 0x... unsupported. Allowed features: 0x...".
> With cpr-exec that is after exec, with no old process to return to.
> No check for an active ploop backup was found in libvirt's
> qemuDomainGuestQemuUpdate(); other callers were not searched.
>
> QEMU cannot fix this alone. Re-reading the limit on a uevent and
> raising a config change interrupt does not help: Linux virtio-blk
> re-reads only the capacity (virtblk_config_changed_work()), queue
> limits are set once at probe. It needs one of the following on the
> kernel side before the feature is enabled:
>
> - dm-push-backup and dm-tracking pass WRITE ZEROES through with the
> limit of the underlying dm-qcow2 device, so the limit of ploopXXX is
> the same before, during and after the swap; or
> - vhost-blk stops failing on the live limit (the block layer already
> splits an oversized WRITE ZEROES bio; a zero limit could fall back
> to writing zeroes or at least return VIRTIO_BLK_S_UNSUPP).
>
> The commit message should state the invariant the patch relies on:
> "the WRITE ZEROES limit of the backend device does not change while
> QEMU has it open".
Added a kernel patch: [PATCH hci-8.2] drivers/vhost/blk: return UNSUPP
for write zeroes on a backend without it
Removed vhost_blk_get_write_zeroes_sectors().
>
> (2) [design] write-zeroes=on is silently best-effort, so the same
> command line produces a different guest ABI depending on the host.
>
> The feature is dropped in three cases: the limit cannot be read
> (warning), the limit is 0 (warning), the kernel has no vhost-blk
> support for it (masked by vhost_get_features(), no message).
> Management cannot tell from the command line or from an error which
> VMs have the feature.
>
> Failure scenario, rolling upgrade: a VM started on an upgraded node
> negotiates WRITE ZEROES. When it moves to a node that is not upgraded
> yet (older vhost-blk, or older dm-qcow2 reporting 0), the destination
> QEMU starts without the bit and says nothing; the migration fails only
> at virtio_load() with the generic "Features ... unsupported", after RAM
> has been transferred.
>
> Conventional fix: OnOffAuto. "on" fails realize with a specific error
> if the kernel or the backend cannot provide the feature, "auto" probes
> quietly. A destination that cannot honour the feature then refuses to
> start. If the bit property stays, the kernel case needs the same
> warning as the other two.
>
>> }
>>
>> vhost_blk_update_size(s, errp);
>> @@ -722,6 +756,9 @@ static uint64_t vhost_blk_get_features(VirtIODevice *vdev,
>> uint64_t res;
>>
>> features |= s->host_features;
>> + if (!s->max_write_zeroes_sectors) {
>> + virtio_clear_feature(&features, VIRTIO_BLK_F_WRITE_ZEROES);
>> + }
> This is where (1b) and (2) become a failed load: the bit depends on a
> value read from the host at realize, and virtio_load() checks the
> guest's features against it.
>
>> virtio_add_feature(&features, VIRTIO_BLK_F_BLK_SIZE);
>> virtio_add_feature(&features, VIRTIO_BLK_F_SEG_MAX);
>> @@ -775,6 +812,12 @@ 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 (s->max_write_zeroes_sectors) {
>> + virtio_stl_p(vdev, &blkcfg.max_write_zeroes_sectors,
>> + s->max_write_zeroes_sectors);
>> + blkcfg.write_zeroes_may_unmap = 1;
>> + virtio_stl_p(vdev, &blkcfg.max_write_zeroes_seg, 1);
>> + }
> (3) [design] max_write_zeroes_sectors is guest-visible state derived
> from the host; it is neither migrated nor checked on load.
>
> If the destination backend reports a smaller limit than the source
> (a different kernel's dm-qcow2 is enough), the feature bits match, the
> load succeeds, and the guest keeps using the limit it read at probe on
> the source. The destination kernel fails every range above its own
> limit (nr_sects > max_sects -> VIRTIO_BLK_S_IOERR), and the guest is
> not told until it reboots or rebinds the driver. Unlike (2), this
> failure is silent.
>
> Possible fixes:
>
> - carry the advertised value in a vmstate subsection; on load keep
> advertising the source's value if the destination backend can take
> it, fail the load otherwise; or
> - make it a property, as virtio-blk does with
> max-write-zeroes-sectors, so that management pins one value per VM,
> and fail realize if the backend limit is below it.
After the previous changes the limit is not derived from the backend:
vhost-blk rejects only ranges above UINT32_MAX >> 9 or not lbs-aligned,
and the block layer splits the bio to the queue limit, so a different
dm-qcow2 on the destination cannot cause IOERR. The lbs rounding
did make the value host-dependent; it is now a constant
(UINT32_MAX >> 9 aligned down to 64 KiB), so nothing needs migrating.
> (5) [nit] The fields are filled in based on s->max_write_zeroes_sectors,
> not on whether the feature is offered. On a kernel without vhost-blk
> support the bit is masked by vhost_get_features(), but the config still
> carries the limit, may_unmap and seg. Harmless by the spec, but it is
> host-dependent config for no reason. Single source of truth:
>
> if (virtio_host_has_feature(vdev, VIRTIO_BLK_F_WRITE_ZEROES)) {
>
>> memcpy(config, &blkcfg, sizeof(struct virtio_blk_config));
>> }
Moot, since realize fails without kernel support, but done anyway:
config is now filled under virtio_host_has_feature().
> [...]
>
> Summary: (1) blocks enabling the property, since every backup of the VM
> becomes a window of guest I/O errors. (2) and (3) affect migration.
> (4)-(6) are minor.
prev parent reply other threads:[~2026-11-24 13:32 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-20 13:23 Alexander Ivanov
2026-11-20 13:26 ` no-reply
2026-11-20 13:34 ` Alexander Ivanov
2026-11-23 16:33 ` Andrey Drobyshev
2026-11-24 13:32 ` Alexander Ivanov [this message]
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=3db56d05-bf1f-47bc-a2d0-79930709441e@virtuozzo.com \
--to=alexander.ivanov@virtuozzo.com \
--cc=andrey.drobyshev@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.