* [PATCH hci-8.2 v2 1/2] vhost-blk: read any queue attribute of the backend from sysfs
2026-11-26 9:39 [PATCH hci-8.2 v2 0/2] vhost-blk: support discard Alexander Ivanov
@ 2026-11-26 9:39 ` Alexander Ivanov
2026-11-26 12:47 ` Andrey Drobyshev
2026-11-26 9:39 ` [PATCH hci-8.2 v2 2/2] vhost-blk: advertise discard on a backend that supports it Alexander Ivanov
1 sibling, 1 reply; 6+ messages in thread
From: Alexander Ivanov @ 2026-11-26 9:39 UTC (permalink / raw)
To: andrey.zhadchenko; +Cc: andrey.drobyshev, svt-core
Turn vhost_blk_has_write_zeroes() into vhost_blk_read_queue_attr(),
which takes the name of the limit to check, so that discard support can
be detected the same way. No functional change.
https://virtuozzo.atlassian.net/browse/VSTOR-141683
Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
---
hw/block/vhost-blk.c | 14 ++++++++------
1 file changed, 8 insertions(+), 6 deletions(-)
diff --git a/hw/block/vhost-blk.c b/hw/block/vhost-blk.c
index 870f3496908..96878de5bb4 100644
--- a/hw/block/vhost-blk.c
+++ b/hw/block/vhost-blk.c
@@ -586,8 +586,8 @@ static bool vhost_blk_uevent_attach(VHostBlk *s, Error **errp)
return true;
}
-static bool vhost_blk_has_write_zeroes(dev_t rdev, bool *supported,
- Error **errp)
+static bool vhost_blk_read_queue_attr(dev_t rdev, const char *attr,
+ bool *supported, Error **errp)
{
g_autofree char *dev = NULL;
g_autofree char *part = NULL;
@@ -599,8 +599,9 @@ static bool vhost_blk_has_write_zeroes(dev_t rdev, bool *supported,
dev = g_strdup_printf("/sys/dev/block/%u:%u", major(rdev), minor(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) ? "../" : "");
+ path = g_strdup_printf("%s/%squeue/%s", dev,
+ g_file_test(part, G_FILE_TEST_EXISTS) ? "../" : "",
+ attr);
if (!g_file_get_contents(path, &val, NULL, &gerr)) {
error_setg(errp, "%s", gerr->message);
return false;
@@ -661,8 +662,9 @@ static bool vhost_blk_open_backend(VHostBlk *s, Error **errp)
if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_WRITE_ZEROES)) {
Error *local_err = NULL;
- if (!vhost_blk_has_write_zeroes(s->backend_rdev, &s->write_zeroes,
- &local_err)) {
+ if (!vhost_blk_read_queue_attr(s->backend_rdev,
+ "write_zeroes_max_bytes",
+ &s->write_zeroes, &local_err)) {
warn_reportf_err(local_err, "vhost-blk: unable to read write "
"zeroes limit of '%s': ", s->conf.devpath);
} else if (!s->write_zeroes) {
--
2.53.0
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH hci-8.2 v2 2/2] vhost-blk: advertise discard on a backend that supports it
2026-11-26 9:39 [PATCH hci-8.2 v2 0/2] vhost-blk: support discard Alexander Ivanov
2026-11-26 9:39 ` [PATCH hci-8.2 v2 1/2] vhost-blk: read any queue attribute of the backend from sysfs Alexander Ivanov
@ 2026-11-26 9:39 ` Alexander Ivanov
2026-11-26 12:47 ` Andrey Drobyshev
1 sibling, 1 reply; 6+ messages in thread
From: Alexander Ivanov @ 2026-11-26 9:39 UTC (permalink / raw)
To: andrey.zhadchenko; +Cc: andrey.drobyshev, svt-core
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.
Handle discard the way write zeroes is handled. Read discard_max_bytes
of a writable backend from sysfs once at startup and use it only to
tell whether the backend supports discard. The kernel sends a discard
as one bio, which the block layer splits by the backend limit, so it
bounds the range only by the size of one bio, as it does for write
zeroes. Advertise the constant write zeroes uses, renamed to
VHOST_BLK_MAX_RANGE_SECTORS, one segment per request, as virtio-blk
does, and the logical block size as the alignment. Do not offer
VIRTIO_BLK_F_DISCARD when the backend reports no limit, the limit
cannot be read, or the backend is opened read-only: the kernel fails a
discard on a read-only backend with VIRTIO_BLK_S_IOERR.
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.
Warn about that at realize: a migration to such a node otherwise fails
only in virtio_load() with a generic "Features ... unsupported".
https://virtuozzo.atlassian.net/browse/VSTOR-141683
Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
---
hw/block/vhost-blk.c | 32 ++++++++++++++++++++++++++++++--
include/hw/virtio/vhost-blk.h | 3 ++-
2 files changed, 32 insertions(+), 3 deletions(-)
diff --git a/hw/block/vhost-blk.c b/hw/block/vhost-blk.c
index 96878de5bb4..c7db99c57e1 100644
--- a/hw/block/vhost-blk.c
+++ b/hw/block/vhost-blk.c
@@ -672,6 +672,20 @@ static bool vhost_blk_open_backend(VHostBlk *s, Error **errp)
s->conf.devpath);
}
}
+
+ if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_DISCARD)) {
+ Error *local_err = NULL;
+
+ if (!vhost_blk_read_queue_attr(s->backend_rdev,
+ "discard_max_bytes",
+ &s->discard, &local_err)) {
+ warn_reportf_err(local_err, "vhost-blk: unable to read discard "
+ "limit of '%s': ", s->conf.devpath);
+ } else if (!s->discard) {
+ warn_report("vhost-blk: '%s' does not support discard",
+ s->conf.devpath);
+ }
+ }
}
vhost_blk_update_size(s, errp);
@@ -769,6 +783,11 @@ static void vhost_blk_device_realize(DeviceState *dev, Error **errp)
goto cleanup;
}
+ if (virtio_has_feature(s->host_features, VIRTIO_BLK_F_DISCARD) &&
+ !virtio_has_feature(s->dev.features, VIRTIO_BLK_F_DISCARD)) {
+ warn_report("vhost-blk: kernel does not support discard");
+ }
+
return;
cleanup:
@@ -833,6 +852,9 @@ static uint64_t vhost_blk_get_features(VirtIODevice *vdev,
if (!s->write_zeroes) {
virtio_clear_feature(&features, VIRTIO_BLK_F_WRITE_ZEROES);
}
+ if (!s->discard) {
+ virtio_clear_feature(&features, VIRTIO_BLK_F_DISCARD);
+ }
virtio_add_feature(&features, VIRTIO_BLK_F_BLK_SIZE);
virtio_add_feature(&features, VIRTIO_BLK_F_SEG_MAX);
@@ -886,9 +908,16 @@ 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->discard) {
+ virtio_stl_p(vdev, &blkcfg.max_discard_sectors,
+ VHOST_BLK_MAX_RANGE_SECTORS);
+ virtio_stl_p(vdev, &blkcfg.discard_sector_alignment,
+ blk_size >> BDRV_SECTOR_BITS);
+ virtio_stl_p(vdev, &blkcfg.max_discard_seg, 1);
+ }
if (s->write_zeroes) {
virtio_stl_p(vdev, &blkcfg.max_write_zeroes_sectors,
- VHOST_BLK_MAX_WRITE_ZEROES_SECTORS);
+ VHOST_BLK_MAX_RANGE_SECTORS);
blkcfg.write_zeroes_may_unmap = 1;
virtio_stl_p(vdev, &blkcfg.max_write_zeroes_seg, 1);
}
@@ -912,7 +941,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,
diff --git a/include/hw/virtio/vhost-blk.h b/include/hw/virtio/vhost-blk.h
index f05715c3809..9110462109f 100644
--- a/include/hw/virtio/vhost-blk.h
+++ b/include/hw/virtio/vhost-blk.h
@@ -22,7 +22,7 @@
#define VHOST_BLK_AUTO_NUM_QUEUES UINT16_MAX
#define VHOST_BLK_MAX_QUEUES 32
/* Fits in one bio and is aligned to any logical block size */
-#define VHOST_BLK_MAX_WRITE_ZEROES_SECTORS (1U << 22)
+#define VHOST_BLK_MAX_RANGE_SECTORS (1U << 22)
typedef struct VhostBlkConf {
BlockConf conf;
@@ -49,6 +49,7 @@ typedef struct VHostBlk {
uint64_t length;
uint64_t backend_rdev;
bool write_zeroes;
+ bool discard;
QEMUBH *resize_bh;
Error *cpr_blocker;
} VHostBlk;
--
2.53.0
^ permalink raw reply [flat|nested] 6+ messages in thread