From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from AS8PR04CU009.outbound.protection.outlook.com (mail-westeuropeazon11021120.outbound.protection.outlook.com [52.101.70.120]) by lore.virtuozzo.com (Postfix) with ESMTPS id 2C78280031 for ; Mon, 23 Nov 2026 17:36:01 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=FpC9jcvzgfIhGAsfB9lYqwTsq6Axf7gbH/HcSMOGoN/IbrIVZSmyolL6vRhBmUpGpmRukJHb81xK+bhdLd73Y7VJNbslA0+ca4/ZqB0676c/ZRX3vo83rhHJtEvTtDZnVpygHGQ9mFLR+QZ44FpS2WolC4l7GXKtrEGPgQgrT8l+ygsmyqvUvoGLxuWHCq7SeNZ+oWGKkShVB1Ex5tc7BiuYfkwBRgK9Jkasc3v8K0WR4bzcpX6wDMWNayfZPjcBFbmZNOTYY2QTE6bwf4QOzF6y5cHaJ3dsroCROv4VHZ5Rtl0Kxmp1WCygfVge8QtCdljWyJ3qIo7HVzJN62m//w== 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=g2hxjeLVSXXaqpizWm24wJg5wyo9XcDo4/8ZCDYoHHU=; b=WqC0EQXoZzz3Sjc+k4voNL7WYqpZy/tWdZmABlKpt5dNj1JZL7Gnl+yjivWefASAs8HzMxuwCBcmDc7HRpfb3DxlqgVlyB2mJmmz4hgaTykXCEWC0jlv+G9jYE5L99QI0//dz9gwIylSJNDHvmhCFT6VaFAA43SNsdYuiD1KPJXAgN8M+8U2N4Zehx/gsgRsYxSrWP5qHNI7taUYUCix3mr7xrYapxQuhMQ6UzPkIQnotsJZgW8v9ACs1vUYxKwTqkU7gN9A57OPLzIWaOONQVDgOZleNTgtSRqKl3FFrAkFooEzi0B2ukFLgHQQ/rytlxNtYbv0kFlUgQwDTI3UKw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=virtuozzo.com; dmarc=pass action=none header.from=virtuozzo.com; dkim=pass header.d=virtuozzo.com; arc=none 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=g2hxjeLVSXXaqpizWm24wJg5wyo9XcDo4/8ZCDYoHHU=; b=yuvk6FLXHYgcSc9XRGA4ds0Ee5er9RS2996yAmGz+8tl3XH30tN3xaN6RwFM/28KI4rAN3udgxG3xEbrUAgfQnbj910mGt2Vnf9UfRAKt++5DZzbq32qY0AnbxMIzewyBOViplYC60O9ur80vvLWJSkfyJUXP8AibFBvRJhhisQPAWTZ/Tt+uB6LtQRrGHPjof2q95HCAuErh+0qs2jZL8x37+LVSO8zjQn7v/sjy/JgyFJiPLw0uS6VvzRMJ4qVLxg3iayHWo7FbuKUij8+0EtoIAnmNFG3NNAeymFBc8nlkeeiQHdRPXPXr/JuDxz+v987Z2QyKgqZg/OdF7kqtw== Authentication-Results: mx.microsoft.com 1; dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=virtuozzo.com; Received: from VI0PR08MB10656.eurprd08.prod.outlook.com (2603:10a6:800:20a::12) by AS1PR08MB7401.eurprd08.prod.outlook.com (2603:10a6:20b:4c7::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.639.13; Mon, 23 Nov 2026 17:35:58 +0000 Received: from VI0PR08MB10656.eurprd08.prod.outlook.com ([fe80::4e37:b189:ddcd:3dd8]) by VI0PR08MB10656.eurprd08.prod.outlook.com ([fe80::4e37:b189:ddcd:3dd8%4]) with mapi id 15.21.0639.012; Mon, 23 Nov 2026 17:35:58 +0000 From: Andrey Drobyshev 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 Message-ID: <20261123173550.104355-1-andrey.drobyshev@virtuozzo.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20261123084134.177261-1-alexander.ivanov@virtuozzo.com> References: <20261123084134.177261-1-alexander.ivanov@virtuozzo.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-ClientProxiedBy: VI1PR03CA0054.eurprd03.prod.outlook.com (2603:10a6:803:50::25) To VI0PR08MB10656.eurprd08.prod.outlook.com (2603:10a6:800:20a::12) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: VI0PR08MB10656:EE_|AS1PR08MB7401:EE_ X-MS-Office365-Filtering-Correlation-Id: 294f62dd-7b65-436d-995a-08df49883bf6 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|23010399003|366016|376014|1800799024|52116014|5113699003|22082099003|18002099003|5023799004|56012099006|261028144727099003|261102123227099003|261026140227099003|261026121527099003|261021121427099003|261009223027099003|261022105027099003|261021123027099003|261022075727099003|261022115727099003|38350700014|10067099003; X-Microsoft-Antispam-Message-Info: FEw9KzeEMvjTk//x8xieR89m1vsksmR4059Mr1wDhetumsGomTjY6r3oAeAlGc0zgU1uTh2ZRJc0El1EQYQsVj4Rudlt1lzeu38Zgn45aOtaYAKzr3BtXrEOzRmPAO/lCuUKqqVyy+AezyJr2D5uasFz1ExE/blpRimm/aqKKY37kP8dWJaSz+JsAXiftrGqV5jvP8ToXxdtGSwkoeGqqoG0tJyVqhsDsmJCRehCYhDmtAK60bc+3QG2Q2ZqAJWDBosUPdyhho7JSftjiMvf4J7CwGjOzDvRKvKvbVVkPEoyeZ/KZe7GaqYEkADBlIQVIZGvKsBO2TchX3m3awX+jaV6XKPLontjdeVAE0z91WqzvGkWRjQaMRdHQSYH+wNZN1ofa27oPzBBpsxMoWITfuX98vSCtJ37BGvsFfSoM80ZDfEdXYulpbq8s1gXg2eyJp83l/Ry+bujsD5Ru8v6SpYgwEvQW8nnmC0GOnxEQVYYuCwKWQt5RWXkJgsWJaZRXt9rVV6PU5DNkWI02cuXZOBcUKi8a/TcrT07edlPOCUYwJoV/pRkXP1OdFkEFmmbyGswQBGhg3gQg0Btt+u5jySvQ7Gfgqx4d7s/sEKqLQ5pz4DcIRJ4mQT8tgmKYeRKj0K9w7YUE9ddXXa9OLvz0SN15u9xFYhriCbzF/Y4Zfk= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:VI0PR08MB10656.eurprd08.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(23010399003)(366016)(376014)(1800799024)(52116014)(5113699003)(22082099003)(18002099003)(5023799004)(56012099006)(261028144727099003)(261102123227099003)(261026140227099003)(261026121527099003)(261021121427099003)(261009223027099003)(261022105027099003)(261021123027099003)(261022075727099003)(261022115727099003)(38350700014)(10067099003);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?M2ZadDFWMjF5K2k0UWQ1VXNqRm8zek9mVmtNd0dHS051ZWVkbFZ6M3JJTUJK?= =?utf-8?B?Z2dTbzRKVVlOUHpBOWNKeE9UTVpoQXpHK0tVOEZZY3RNZmdZTGtUdEs2akYv?= =?utf-8?B?b0VBNDJQTWJ1T0lTcFRkNGZCMS9UMGdkV1JuME1LSlJxRmh1bjZ5NlJGSWw2?= =?utf-8?B?R21YNXNnU1ZHZ3dCUlpSOXF3NmdnNFk1cTQxY3VUMjNrUzEvYU5QTjJJeEEx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dmY0FVMUFCd3Zsd3A5REJoMDhrTFk5TjQyRWgyd096?= =?utf-8?B?ZS9ISUlSS0NKWExQZ2VRVWtnVk5YWEJVYlNyTzdBSnlrd2xZc1NrbzFkZytu?= =?utf-8?B?aTFsU3Z2OGZBK09BYkZiNnhFQnZvRU4xcFNkWVJaVVl3cUxJZE9IY3VJUDlM?= =?utf-8?B?bVZDY1oyUzUzZHIzVEd1QXl0WDdEK2NiZnBmN1c4bTRWbkJOUDlHdlZnZTNX?= =?utf-8?B?R0hxNkwwMlZFdC9FSVJzYlJiQTFET0pmSUdHVlNMa2VQbTlSYzRIem5LRE44?= =?utf-8?B?ZWdoWHVrZ1JhMXlDd3BMTXdaZ0VGSm9tRVltODUyb3ZsSGIycHJ0SENiQlFS?= =?utf-8?Q?ICZWQaR941n2VkWU=3D?= X-Auto-Response-Suppress: DR, OOF, AutoReply X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-Network-Message-Id: 294f62dd-7b65-436d-995a-08df49883bf6 X-MS-Exchange-CrossTenant-AuthSource: VI0PR08MB10656.eurprd08.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Nov 2026 17:35:58.3743 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 0bc7f26d-0264-416e-a6fc-8352af79c58f X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: QSqVnWKFEnLVS6k7+0SxSuqhJoS1H9COYQMcnLGl09WR25JAUitChmXD6IW2ka/x/8jJQGOfot0kK6Ua99mKWwuPBcw+jLoAg0KFeE6zyOY= X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS1PR08MB7401 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 > --- > 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.