From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from AM0PR02CU008.outbound.protection.outlook.com (mail-westeuropeazon11023128.outbound.protection.outlook.com [52.101.72.128]) by lore.virtuozzo.com (Postfix) with ESMTPS id DAEAE80031 for ; Wed, 25 Nov 2026 17:08:31 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=jzOlS+2gXcpZnuMZsOaG7+Y1KW3o+cyuk5Qv0RtfwOtv134EAUU4NB3bxyCYJOLJ5pi7sIfjolM4UGIoDhu0v3kztye7lGO8nU9W+UBzqpWHgc/v21M9QCDTIMlAc2B0ofMpJVV7i3hZGCbdUZEVgzrkTgEz8F4OQ4xtL9X3zrn9kxKJJ9Pj5UG2ScMOBYGk39UnSDcTcmsX+mR6FmoSzK2yIOLQJ2NBmnPer40jHEDy8NKwtuBXYpa0kQrXZIWOQDt2EL9ykJTDumoXlti5TwcjbEnvSMuYDqdM1X3CMyx7UTydtH8aGK/dffezRGBFW0GJ4pJkc06rf8RoJ+zDCg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=i0zr3JuEpA1z+9Qvque0K6jbzzNIZi6NyJ2817rjrFM=; b=im79skUk9PZKQJtWUaKRgBJu/72d9Osr7YLl2oFM+y1QpQZEqr3dtQ5fWHfCLCV25Y4+4anY/c8WmOH9JYmDOjiv9neWOySbT3tgfSityMXIM1f/pXqmspQBEcCEVn8etmFtUjTk3gaHMAhWPclmPE9eldQyhM30KNQ3XiykVlEzh3Qg5GxYpWRX+UOX+0vpXQwmfLjzDGOTUKMF1Hk8eWrUy2LKKHWM32+ZvSx9zFY5nGCcSvR7b/b/UkZMKBxNqjf1eQ7uK3hK/a0j1tOUesWlGVvGeS16GbZSSC4JkQcUXkb1t4McGcm5LviEwhR3NqX/rtkH1DtzUd8x6cY01w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 130.117.225.111) smtp.rcpttodomain=virtuozzo.com smtp.mailfrom=virtuozzo.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=virtuozzo.com; dkim=pass (signature was verified) header.d=virtuozzo.com; arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=virtuozzo.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=i0zr3JuEpA1z+9Qvque0K6jbzzNIZi6NyJ2817rjrFM=; b=JpRyWLdQnFBTw/Dx2in4Gq9ZNY+BA7bJCKF328KcyoR/lqdWq1ldg7wWOjiQwpood/diEK/nH73i0M6k9/jVM7P44OdDWhLSL/Pl/UEdKtPH4bfVvjaJdbQ4VFIGLkQNbdrVAVfz55n0T3co/pntappxTLJJfV4nZxMYR2oKuTih6TLX874LoLiPv0I4Nzwj2tfEtPbyRrQ5Kj5GHrKrboj9GCZWbPjJCmMxxgjo8iRVUGh6YI+LGmlqiRb0JVmKl675DTxOFDbzNH1p6HhV3IoY+AZWcM5LI4doPpy70YcXFqxqMSU+R45TeXpt2ly+chwdJ0eUJJxuUZQVyBFmeA== Received: from AS4P190CA0046.EURP190.PROD.OUTLOOK.COM (2603:10a6:20b:656::29) by AS8PR08MB6263.eurprd08.prod.outlook.com (2603:10a6:20b:290::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.639.14; Wed, 25 Nov 2026 17:08:28 +0000 Received: from AMS0EPF0000056C.eurprd02.prod.outlook.com (2603:10a6:20b:656:cafe::ae) by AS4P190CA0046.outlook.office365.com (2603:10a6:20b:656::29) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.662.7 via Frontend Transport; Wed, 25 Nov 2026 17:08:28 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 130.117.225.111) smtp.mailfrom=virtuozzo.com; dkim=pass (signature was verified) header.d=virtuozzo.com;dmarc=pass action=none header.from=virtuozzo.com; Received-SPF: Pass (protection.outlook.com: domain of virtuozzo.com designates 130.117.225.111 as permitted sender) receiver=protection.outlook.com; client-ip=130.117.225.111; helo=relay.virtuozzo.com; pr=C Received: from relay.virtuozzo.com (130.117.225.111) by AMS0EPF0000056C.mail.protection.outlook.com (10.167.242.122) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.662.3 via Frontend Transport; Wed, 25 Nov 2026 17:08:26 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=virtuozzo.com; s=relay; h=MIME-Version:Message-ID:Date:Subject:From: Content-Type; bh=i0zr3JuEpA1z+9Qvque0K6jbzzNIZi6NyJ2817rjrFM=; b=HAc4swyODS7a YHvcjRVaxtGldIunMFRekY6ZlrzsvRuqY4WFKZFwcSV1/aoEVyB7qT5dCL9CikBA2P1BtG2uuTKVO 0w/1oqzS1C8Uuz5dG1sIGtea/+n9lewDKUXRLVK/N3kBHRplCqG/9ufFG877Sb3eKJ60Ig07yLh5v eppwLpEfmzZbhvNe+Ji4y66SLR8k08ntMsR95YlP76QjuhXb5SC10r6kbudbvpUyDj4nUhBzNjYae Tdf++h97M2mwLH6m+tXgSFgmS+yK3SRWFLn76fm5qTs2RjZAvkB25zWtTtLXP57wLEdOjYl9tLnOB TFXpk+AaiLUDg30HED/qyw==; Received: from ch-vpn.virtuozzo.com ([130.117.225.6] helo=sky) by relay.virtuozzo.com with esmtps (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1xWGTe-00A6tE-2p; Wed, 25 Nov 2026 18:08:25 +0100 From: Alexander Ivanov To: andrey.zhadchenko@virtuozzo.com Cc: andrey.drobyshev@virtuozzo.com, svt-core@virtuozzo.com Subject: [PATCH hci-8.2 v5] vhost-blk: advertise write zeroes on a backend that supports it Date: Wed, 25 Nov 2026 18:08:22 +0100 Message-ID: <20261125170822.827369-1-alexander.ivanov@virtuozzo.com> X-Mailer: git-send-email 2.53.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AMS0EPF0000056C:EE_|AS8PR08MB6263:EE_ X-MS-Office365-Filtering-Correlation-Id: 176e55a3-ea3e-4205-a328-08df4b16b894 X-LD-Processed: 0bc7f26d-0264-416e-a6fc-8352af79c58f,ExtAddr List-Id: svt-core@virtuozzo.com X-MS-Exchange-Transport-Rules-Loop: 1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|23010399003|376014|1800799024|36860700016|82310400026|10067099003|261021121427099003|56012099006|261022113327099003|261021123027099003|261009223027099003|261026121527099003|261028144727099003|261022105027099003|261026130827099003|261021135927099003|261021130227099003|261022075727099003|261021133027099003|261026140227099003|261102123227099003|261026135527099003|261022115727099003|18002099003|5113699003; X-Microsoft-Antispam-Message-Info: chPNxLJjpAhSxiReHO8nkpZuMMB9SqkVCIGL9kMKye3Xfpg+NK0u2lFxmxRNNZbGrzSvCvNKYndYLjZ1zSqnNHP9uAOHARaPUvuSke0jJo1Ytm5aSj3fS13dP3sXDQ5iAuLwAA6XgxWSZg2JgFZyFJuWGTvLDimh35t/7mhruO1Ou616VZNMLqQ0ULsIV3w/fWhMVFCsZHOMSQwLbEX0MdRSizFSEqnrsqPt6gCODCxkseMb6+aLqAYqD0si9XVkJJZebLf5LwytNdJv7yo3SwOTk/EPXdD8TsJ232Iqz1dHWj5lgiZflUbZVtsltGtKgM0qpFbIAMklqaUU4iL/rdxuRoil+jCGu72rMIJnbEAFSwzubNGNc+KQYVHnXg05K2FcSKm6SfYL+mmoXFRXWlzHcW3+u2mYPPpPR9MVpq0E0JvH2Tg5UWg+z7NXlz8YjLwHUvgx+2B+8+3Z0jnDN1b+rzVk4jXnHjVbrZNFHevGVXYrYq5sZ9GpPSUW3Xpdi0sE9Cz/Aw2kUOpyeHJWpkGQ3xJT2KEfk1N6gS8OxW2EIfKKVqG4lPpt+cdYDsRivnu8iPQ2FqvBdL9i/aJ1jLdthq7kksU9Qibuc8VMo0n8G/Dxo2sVhNC70BhnV9+ISfXazUac41jZ9W7eKug1TBKDokhdJysNv2Vlz72Odq5OI73l1Lu8BT8B3BO7DOkI2DS9ZQlpeUlM2TqmUAr48w== X-Forefront-Antispam-Report: CIP:130.117.225.111;CTRY:CH;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:relay.virtuozzo.com;PTR:relay.virtuozzo.com;CAT:NONE;SFS:(13230040)(23010399003)(376014)(1800799024)(36860700016)(82310400026)(10067099003)(261021121427099003)(56012099006)(261022113327099003)(261021123027099003)(261009223027099003)(261026121527099003)(261028144727099003)(261022105027099003)(261026130827099003)(261021135927099003)(261021130227099003)(261022075727099003)(261021133027099003)(261026140227099003)(261102123227099003)(261026135527099003)(261022115727099003)(18002099003)(5113699003);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: s3aFob8DXg01LKlt0pTh9JWklJMttS8YUK+aShvx9yCmdGLqpTOh059zO9dE3R4Of2xg9qEhHq/xZnifEqVt+zlKFJPhmY0Fd0DWGP0NyYSpXleM/0tKILtzwEzTmjH71W7CGavhOgZa5BCoN5Bc65cODergtJoh2zvUHDwhFm9hyxwp3j1oy5PPepn5iE12boQOlqBizYVj7MqpDVZjP2w67UV6LZzj087AZsz2ZpnuPgBIEw0tWp3/Ucpn7LCB/xGbIMIoqlvgvBkN1EJkaHR9aPZboCb8Jg4aoNb6AFIXZyYK43rPzCDyhdeyPqjSMO5Dy/smlzVrsCbxUaBYwwuQgMuKneXSYNcLNFrPZrOUBRlkTtS0F+F7SOnRsinMTuL/a9/p+st5wjRzx10WSDioavZE6yKkg0W7TgRsKSZceSahGTfOtLpX2eHAAair X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Nov 2026 17:08:26.7218 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 176e55a3-ea3e-4205-a328-08df4b16b894 X-MS-Exchange-CrossTenant-Id: 0bc7f26d-0264-416e-a6fc-8352af79c58f X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=0bc7f26d-0264-416e-a6fc-8352af79c58f;Ip=[130.117.225.111];Helo=[relay.virtuozzo.com] X-MS-Exchange-CrossTenant-AuthSource: AMS0EPF0000056C.eurprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR08MB6263 vhost-blk in the kernel now handles VIRTIO_BLK_T_WRITE_ZEROES. It sends each request to the backend as one WRITE ZEROES bio, which the block layer splits by the limit of the backend, and answers VIRTIO_BLK_S_UNSUPP when the backend has no WRITE ZEROES support. So the range of a request is bounded only by the size of one bio. With the write-zeroes property on, read write_zeroes_max_bytes of a writable backend from sysfs once at startup and use it only to tell whether the backend supports WRITE ZEROES. Then advertise the constant VHOST_BLK_MAX_WRITE_ZEROES_SECTORS: a power of two that fits in one bio and is aligned to any logical block size, so the guest never sends a range the kernel rejects. The limit itself can change later on a dm table reload, and a constant keeps the config the same on both sides of a migration. A partition has no queue/ directory of its own, so read the limit of the whole disk, whose queue it shares. Do not offer VIRTIO_BLK_F_WRITE_ZEROES when the backend reports no limit, the limit cannot be read, or the backend is opened read-only. The property stays off by default. A kernel without write zeroes support in vhost-blk does not offer the feature, so it is not negotiated there. https://virtuozzo.atlassian.net/browse/VSTOR-141684 Signed-off-by: Alexander Ivanov --- v5: - advertise the constant VHOST_BLK_MAX_WRITE_ZEROES_SECTORS instead of the backend limit: the kernel lets the block layer split the bio, and a constant survives a dm table reload and a migration. - read write_zeroes_max_bytes only to tell whether the backend supports WRITE ZEROES. - drop the BLKSSZGET call, the constant is aligned to any logical block size. v4: - go back to v2 and drop the v3 approach with a constant limit and a realize failure. - round the limit down to the logical block size of the backend, since the kernel fails a misaligned range. - report why the limit cannot be read in the warning. - rebase. v3: dropped v2: - read write_zeroes_max_bytes of the whole disk for a partition, which has no queue/ directory of its own. - do not advertise the feature and warn when the limit cannot be read. hw/block/vhost-blk.c | 54 ++++++++++++++++++++++++++++++++++- include/hw/virtio/vhost-blk.h | 3 ++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/hw/block/vhost-blk.c b/hw/block/vhost-blk.c index f88c1c304e8..870f3496908 100644 --- a/hw/block/vhost-blk.c +++ b/hw/block/vhost-blk.c @@ -586,6 +586,36 @@ 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) +{ + g_autofree char *dev = NULL; + g_autofree char *part = NULL; + g_autofree char *path = NULL; + g_autofree char *val = NULL; + g_autoptr(GError) gerr = NULL; + uint64_t bytes; + int ret; + + 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) ? "../" : ""); + if (!g_file_get_contents(path, &val, NULL, &gerr)) { + error_setg(errp, "%s", gerr->message); + return false; + } + + ret = qemu_strtou64(g_strstrip(val), NULL, 10, &bytes); + if (ret) { + error_setg_errno(errp, -ret, "unable to parse '%s'", path); + return false; + } + + *supported = bytes != 0; + return true; +} + static bool vhost_blk_open_backend(VHostBlk *s, Error **errp) { ERRP_GUARD(); @@ -627,6 +657,19 @@ 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)) { + Error *local_err = NULL; + + if (!vhost_blk_has_write_zeroes(s->backend_rdev, &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) { + warn_report("vhost-blk: '%s' does not support write zeroes", + s->conf.devpath); + } + } } vhost_blk_update_size(s, errp); @@ -785,6 +828,9 @@ static uint64_t vhost_blk_get_features(VirtIODevice *vdev, uint64_t res; features |= s->host_features; + if (!s->write_zeroes) { + virtio_clear_feature(&features, VIRTIO_BLK_F_WRITE_ZEROES); + } virtio_add_feature(&features, VIRTIO_BLK_F_BLK_SIZE); virtio_add_feature(&features, VIRTIO_BLK_F_SEG_MAX); @@ -838,6 +884,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->write_zeroes) { + virtio_stl_p(vdev, &blkcfg.max_write_zeroes_sectors, + VHOST_BLK_MAX_WRITE_ZEROES_SECTORS); + blkcfg.write_zeroes_may_unmap = 1; + virtio_stl_p(vdev, &blkcfg.max_write_zeroes_seg, 1); + } memcpy(config, &blkcfg, sizeof(struct virtio_blk_config)); } @@ -858,7 +910,7 @@ 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 and write-zeroes not yet implemented in kernel module */ +/* 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 059f1ebc701..f05715c3809 100644 --- a/include/hw/virtio/vhost-blk.h +++ b/include/hw/virtio/vhost-blk.h @@ -21,6 +21,8 @@ #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) typedef struct VhostBlkConf { BlockConf conf; @@ -46,6 +48,7 @@ typedef struct VHostBlk { bool vhost_started; uint64_t length; uint64_t backend_rdev; + bool write_zeroes; QEMUBH *resize_bh; Error *cpr_blocker; } VHostBlk; -- 2.53.0