From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from DB3PR0202CU003.outbound.protection.outlook.com (mail-northeuropeazon11020115.outbound.protection.outlook.com [52.101.84.115]) by lore.virtuozzo.com (Postfix) with ESMTPS id CB61680027 for ; Wed, 25 Nov 2026 13:02:55 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=WWP8tFrzOmP6X0oN/icbZQ80z4uhbnqeVkWK1mIm/aCM83EjU74jcieB49S2njIM/zd/gOqOfUUXdug0FVdp1ya2/zrosygVtV0FxTMhqGrrXulYAeOq3YeelRJ4uwOH8K6kSNItwAXtSjNRh2kKJCMAqTqKJV1Vu7Ge92gX1Ycdktvq3OYEe6QwQzQwSTxH1ualo97rQyjZGhUOw1RokNevOki/j0ZRDHxLNvpHyke/nh/vKMSImSPpT8K+e1yuZ7P51izl9bkLjRR2ITEh/YRSp5Hr1nDGrCyDlyDyGGVjxJkqENuYCrHbvJqlKmqWFXfNHxL3hCDLwO4DT1venA== 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=IOS4BUEPdLE/iOqlrL9zd5x26fZjIheC3jYGAeqOBSg=; b=mCdJZWAsfUUSwsbOq78dbhCm7tz47H6/mdgYYKQ+sa+0EKRelY4RIwFvN0vqLflzX0F9IsPm+RHxbK2MLDEqzVifNR7sDJbenzGY3+cnkUhPzNqZO2ihxtoYNppEVdne/i5dUKEmLFpLlGSRgguPmuIowpuTusvBoztsJTu15aUbg3nCuHRs9NzgMpovPCMeMLL0c5ctOJYpuQIUuerojT4zLhDw0ZEpriUCMVpK8QbU1N9udRpwh7l/RLFyqzjXhzRx6kk8MFMFY6Ixq+tvmKvcU+wm7t+d9AsY7detjKd5hU0HazSus24Vg9i+frPGb8zCAhacmmIDyhGAjs9caA== 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=IOS4BUEPdLE/iOqlrL9zd5x26fZjIheC3jYGAeqOBSg=; b=Tk1CTGQbxAG2UwhLeBv0oVwjP6PCNAeHnnLfz4bGrZUYyosXbrql6K7h/wFrCYQWVpXxnt4m4rJtnvO6NkMLYsIEqu3sp5oAzi229I65Tix5dJUD/+JNFYXrT2Z78OpwX499LZzUYeLftbOG07JF2wsPNS2vkz+Jl13EH7SG7TwESd+xlh1B5Dn57ldVxB+I2hWOPbqwmt6bOHbtwbpi3jGnZWlf8M2OYgsF+BwYEhGNXppBvn+5Esoc1JNplOWMpVpVAUwQ9yXXi300rn01rza1TmTM2tlRfo3X7tzpEh0F2ajO5IENv6TEiCxlWz8bYiv2Z00IsasVB2/j9xvYFQ== Received: from AS9PR06CA0779.eurprd06.prod.outlook.com (2603:10a6:20b:484::34) by AM0PR08MB5332.eurprd08.prod.outlook.com (2603:10a6:208:17e::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.662.6; Wed, 25 Nov 2026 13:02:49 +0000 Received: from AM2PEPF00070CF3.eurprd02.prod.outlook.com (2603:10a6:20b:484:cafe::8a) by AS9PR06CA0779.outlook.office365.com (2603:10a6:20b:484::34) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.662.6 via Frontend Transport; Wed, 25 Nov 2026 13:02:49 +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 AM2PEPF00070CF3.mail.protection.outlook.com (10.167.242.5) 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 13:02:48 +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=IOS4BUEPdLE/iOqlrL9zd5x26fZjIheC3jYGAeqOBSg=; b=ss73V/4QQD/j Kop/vTwjjxH3iUFv8DvvhIz9TK3VUM8GMKiPc4jBt2U6S71AkUbhN9hA07fThP+nbRUQql73qVFtY g530aQeqVdQlYC1PyAisSrrQaeHgvDjzYdWdMH+BLmt6rrE+ShnRo8C+gf7/WgcS/sOZOXcLg3ia8 QpdnJjhvu0oY2a9cUT/XgpCj9xFN35gEKw76zeVu8arWjE3HgLFwFCOoQ9sVgT4oFUdBbwZF2KgyS YFh6zIgxp63RlTEiME7OnncwIQCJLmYp+kFe8kJiIM8c+aHrawH6Z+cwTr5R/V5RaTQD7uUGNtvwv oWDqz/faFcQBYsR4bM/KJg==; 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 1xWCdw-008qZZ-2o; Wed, 25 Nov 2026 14:02:47 +0100 From: Alexander Ivanov To: andrey.zhadchenko@virtuozzo.com Cc: andrey.drobyshev@virtuozzo.com, svt-core@virtuozzo.com Subject: [PATCH hci-8.2 v4] vhost-blk: advertise write zeroes with the backend limit Date: Wed, 25 Nov 2026 14:02:45 +0100 Message-ID: <20261125130245.760786-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: AM2PEPF00070CF3:EE_|AM0PR08MB5332:EE_ X-MS-Office365-Filtering-Correlation-Id: 4489a03f-4284-4382-a50a-08df4af467ed X-LD-Processed: 0bc7f26d-0264-416e-a6fc-8352af79c58f,ExtAddr List-Id: svt-core@virtuozzo.com X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700016|376014|1800799024|23010399003|82310400026|10067099003|56012099006|261022075727099003|261022113327099003|261022105027099003|261021133027099003|261021130227099003|261021123027099003|261021121427099003|261022115727099003|261021135927099003|261026121527099003|261026140227099003|261026130827099003|261026135527099003|261028144727099003|261102123227099003|261009223027099003|18002099003|5113699003|13003099007; X-Microsoft-Antispam-Message-Info: A4aHEz06qswaYOH7v4TD2GTli8dmZ8QbgHLD3SlJv8+8VgOyXoGdNx0rwuYggZqilfpWhD8c3TcrDEAAX14UBQXs9gzDA4QhRZDmfTyECgMXIxE8mQgRzzo0vz67xbRCbZkMSY72w9XM1L1S6BC9qGsgRoouSyEX8i4px0TnKJb7jJyMNJeeEaltjbGAt8Q77/qHJw/hIZX4mQFoxsQIKoZurWXZ81ix4JOZng1Uhoi+AYunTzNuE/wmKSBKQwn/MAbLBa+k9g17ENJ42EcCCQIKxivA9VazgjbUocT7i9ApfEVx6R4pXyEB/gtkNKWCqcrNfmuRO/tF1IIjLLCPS8lqTD5i9XaiYcJIOglGKIVJZ/D+vsjFpmEhKGQodhvj6T77zlmvSp1bz5Qfp6mZriquZ09CfzV/zrT6WDm5Unw8YHPAkipVMCuAJdI15iaAgePWWohW7d57haiG35l6rZ7AQX2p0sJNMOZTb1CWGCbgvyz6vuP2aeT0lnVDmte+WskjJIXs9ddoqPdaa31SacFBESh7MSdoryxL6Ityw89UC1IltRv3rjki8jj6l6rrfA5am7sjpU8kfwg2Y2bGALOUdIZaQoj2/bKQDGuKP+eBSAlS60ZKoFgA/gx0fvp8Lr4pgYGpLq5ds/jQzXCxbHnuk/txL7wVQgY8Pu7c6NyIloEIhX0Me8peDmQn1XI48KK329s95SpaXuSrMwK8yg== 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)(36860700016)(376014)(1800799024)(23010399003)(82310400026)(10067099003)(56012099006)(261022075727099003)(261022113327099003)(261022105027099003)(261021133027099003)(261021130227099003)(261021123027099003)(261021121427099003)(261022115727099003)(261021135927099003)(261026121527099003)(261026140227099003)(261026130827099003)(261026135527099003)(261028144727099003)(261102123227099003)(261009223027099003)(18002099003)(5113699003)(13003099007);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: iMwMm/07qIK+JurWGu4J7o2CELD5kav3V9+i/EUYxqRSH6Z5qtwH5MMrBpjkdwsD6TK7ZROcHvA1tDIypptOYyJYeuzNeYJHA9BppXrRveOsSFLOE6YYoNPxmp12gFsiD2LSbd/iXwHWc2EG+WvW4onnna/n8FVvLdYSFHgzyFwk0eca3pVgpjR8fILTO10n4dsoCkdL5o+oeltQKKBNLbAwW2Hii1naqxypO8jbiWPFbh0octlBygX2DWxIMSNnZtK9khnnEEraqa6JoS3En8ZB7E9c5Zy10ucSuNNsNI3XVjCEASG8qiA4lOXxrUj5A8JEBmwUAcsU4MyeJ9nbfle1NVzMBqZYVgU7Dzl8AEzNDN4zMuZYKftOOqI9KL87c5XJDeTGf8uPbts+Yrl80QtyOARIL0pMlDF+q5eEzkSTc1/rqIwvzqJphkZGFEoO X-Auto-Response-Suppress: DR, OOF, AutoReply X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Nov 2026 13:02:48.5076 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 4489a03f-4284-4382-a50a-08df4af467ed 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: AM2PEPF00070CF3.eurprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM0PR08MB5332 vhost-blk in the kernel now handles VIRTIO_BLK_T_WRITE_ZEROES. It sends each request to the backend as a single WRITE ZEROES bio and does not fall back to writing zero pages, so the feature only works on a backend that supports WRITE ZEROES and for a range that fits in one bio. With the write-zeroes property on, read write_zeroes_max_bytes of a writable backend from sysfs and advertise it as max_write_zeroes_sectors, capped to what one bio can carry and rounded down to the logical block size of the backend, since the kernel fails a misaligned range. 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 --- 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 | 63 ++++++++++++++++++++++++++++++++++- include/hw/virtio/vhost-blk.h | 1 + 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/hw/block/vhost-blk.c b/hw/block/vhost-blk.c index f88c1c304e8..e9865994a9f 100644 --- a/hw/block/vhost-blk.c +++ b/hw/block/vhost-blk.c @@ -586,6 +586,44 @@ static bool vhost_blk_uevent_attach(VHostBlk *s, Error **errp) return true; } +static bool vhost_blk_get_write_zeroes_sectors(int fd, dev_t rdev, + uint32_t *sectors, 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 lbs; + int ret; + + if (ioctl(fd, BLKSSZGET, &lbs) < 0) { + error_setg_errno(errp, errno, "unable to get logical block size"); + return false; + } + + 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; + } + + /* The kernel issues the whole range as one bio */ + *sectors = MIN(bytes >> BDRV_SECTOR_BITS, UINT32_MAX >> BDRV_SECTOR_BITS); + *sectors = QEMU_ALIGN_DOWN(*sectors, lbs >> BDRV_SECTOR_BITS); + return true; +} + static bool vhost_blk_open_backend(VHostBlk *s, Error **errp) { ERRP_GUARD(); @@ -627,6 +665,20 @@ 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_get_write_zeroes_sectors(s->backend_fd, + s->backend_rdev, &s->max_write_zeroes_sectors, + &local_err)) { + warn_reportf_err(local_err, "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); + } + } } vhost_blk_update_size(s, errp); @@ -785,6 +837,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); + } virtio_add_feature(&features, VIRTIO_BLK_F_BLK_SIZE); virtio_add_feature(&features, VIRTIO_BLK_F_SEG_MAX); @@ -838,6 +893,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); + } memcpy(config, &blkcfg, sizeof(struct virtio_blk_config)); } @@ -858,7 +919,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..e4bcbe49127 100644 --- a/include/hw/virtio/vhost-blk.h +++ b/include/hw/virtio/vhost-blk.h @@ -46,6 +46,7 @@ typedef struct VHostBlk { bool vhost_started; uint64_t length; uint64_t backend_rdev; + uint32_t max_write_zeroes_sectors; QEMUBH *resize_bh; Error *cpr_blocker; } VHostBlk; -- 2.53.0