From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail.openvz.org (mail.openvz.org [69.168.225.77]) by lore.virtuozzo.com (Postfix) with ESMTPS id 760EE803BB for ; Wed, 18 Nov 2026 11:09:36 +0000 (UTC) Received: from mail.openvz.org (localhost [127.0.0.1]) by mail.openvz.org (8.14.4/8.14.4) with ESMTP id 6AIB8JUO023810; Wed, 18 Nov 2026 14:08:19 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 6AIB8JUO023810 Authentication-Results: mail.openvz.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=virtuozzo.com header.i=@virtuozzo.com header.b="tz4EwCRV" Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by mail.openvz.org (8.14.4/8.14.4) with ESMTP id 6AIB8Han023806 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=FAIL) for ; Wed, 18 Nov 2026 14:08:17 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 6AIB8Han023806 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-4a589b28bddso483321f8f.0 for ; Wed, 18 Nov 2026 03:08:17 -0800 (PST) X-Forwarded-Encrypted: i=2; AL+hErHgYzUENtduSiNN8gBMyDNw6bEQPYuCtzbltD2Mcg0MNgyGvAdGXyc48rz4tMPERERYOyAhIQ==@openvz.org X-Gm-Message-State: APuU4Z/iq8Ufxq4+XtnBiyR8vam8p+ETBBn7svHab8ueCX/FnRpBc0gX qEaxoBzf7fgnYS4/gN4PNnkeNeGApgBVC3QZI4jrG2o8SsnEeXlkCvNjjJvv8B7rLLQQYiOSOEl GdVLkzXLVvYrfeBgRWjGeuxQ7tkf0bd1QDJwKat+ZhnrK1iON8q6AMA== X-Gm-Gg: AZFdTZcsDN2pY+XpFMtuSqx/IheKWRWf8t3iWMDW2iL4XohN+6D+PYQD2iWxwVyCTRy 8W1r16y7LBk5GDvwQTT0616CMoTup9Dzyog3Tw4KbddZjtedAlWUH5PzP+Nry9E+7MhVUHjYNyU eisdBr0dD30J+GY9iwoincUHCL07JTezo4u0IsD5qCE3ip6UWk0aoNlUA6QqEdHazEOCbSzGBaf w69FSlYWUzjPxMtlY5HsSxYAnMVvogifuWI7eA6EepLM2oQS7oRvFkfJRWvEWBTSFKwX8j4Jqj4 RBdDyzLgaH91Lw+fihIHpGtUUOpoPCWECg0TSyFiCCDcfFwYXPkUSnHGN3Ft9iCQPLvQHa9e0j6 LXWkm1sDZ9LEGn6G6Vz8RC3xvJvE= X-Received: by 2002:a05:6000:240d:b0:4a6:4687:233e with SMTP id ffacd0b85a97d-4a6468725f3mr4936235f8f.7.1795000097295; Wed, 18 Nov 2026 03:08:17 -0800 (PST) X-Received: by 2002:a05:6000:240d:b0:4a6:4687:233e with SMTP id ffacd0b85a97d-4a6468725f3mr4936175f8f.7.1795000096694; Wed, 18 Nov 2026 03:08:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1795000096; cv=none; d=google.com; s=arc-20260327; b=T7WpC/6STv7JHtFV7PuIBWUBFNY4VAlClfHlx1AS98AU5GE4Rq1vF3+AHRqwZqa42s J2NOh4D4OyFxh9T1aIF4YovT5LPRGmKw5vI76exyOwSDYjF0ryrAvAR1Q3KJMiShvqBA 5V4MouD0YteZpGKYH9PCIUo6+kAgiR6cczyoxGFAfY96vmCLT3rC9BA8eZ/k3RrNrpWh L+rLcP8rYpNVK9OJZCO1oNpmf4MNeYUDxQxnV4+oNaokCOMI+MeiVmRaeClxuBeYZoso Pn1IfJsT5izCjyfOfr+W/ttIoS8gDcnOyViIKL3nalY+Wnm9lfmrDQH51R20ZwKm5F8q 27Dw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:dkim-signature; bh=tbSpXxt+2B/hA3CWEf42wJ9Cwl2yU7ivt6HhDxrctSg=; fh=CMILZ+goaghq6B4UpCDhf0EQp6kYD0LAlmOkUiVCOvs=; b=B9FzR4BVtr5WVpmN07JI/IUVYJv0pQp7z4IJeQEQCxsmboTU79cjs/ZU8Rk4N2VoR7 0cTZ74Y5kgJxbQkRRtutwjokMMa+fzNhDdpo+OvblwNopCG9OwASTq8zuRQ2FUs768ZS B46kJk9+eTRMFTQ6XZRvcFQZV8rJfQGgeCSyOCwsX0/wQ++oSUOK0oVCXAQUrzAw/aaN 9gClLWIy/4Tz8uerA2laYfW+t46zP0krkvKQ3yzkOwHZjTQ8RLcpqoqLcVvMEp6GimTN 6Vo7QYNS3DPDOQgIEJ2aCA9M0JsdXm9DWnghaeTIFFZzIw05TgK7jyz31YUy9EM6xTWz EyYg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@virtuozzo.com header.s=relay header.b=tz4EwCRV; spf=pass (google.com: domain of alexander.ivanov@virtuozzo.com designates 130.117.225.111 as permitted sender) smtp.mailfrom=alexander.ivanov@virtuozzo.com; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=virtuozzo.com Received: from relay.virtuozzo.com (relay.virtuozzo.com. [130.117.225.111]) by mx.google.com with ESMTPS id ffacd0b85a97d-4a6573baab5si4759389f8f.95.2026.11.18.03.08.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Nov 2026 03:08:16 -0800 (PST) Received-SPF: pass (google.com: domain of alexander.ivanov@virtuozzo.com designates 130.117.225.111 as permitted sender) client-ip=130.117.225.111; Authentication-Results: mx.google.com; dkim=pass header.i=@virtuozzo.com header.s=relay header.b=tz4EwCRV; spf=pass (google.com: domain of alexander.ivanov@virtuozzo.com designates 130.117.225.111 as permitted sender) smtp.mailfrom=alexander.ivanov@virtuozzo.com; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=virtuozzo.com 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=tbSpXxt+2B/hA3CWEf42wJ9Cwl2yU7ivt6HhDxrctSg=; b=tz4EwCRVfT09 ph57EwTP30TctpSQLSZqbCWZpzncqC9UlmCrvm+p9nO1Xi7+Vx5KnKdqB868s5ZFDrhfT3H6uwGZI kAr5P4zLeRwTbPkKFsw+BQplqVdUcmK3Ah4wAMhlkaIod4Sc9C5gCa2EqOcRX42idY3vYmIDuviaS jWVKafH65mKcV5JzcEg233vhOs8NQ1KTJ8xBYNoF2il34sY9/qsXVc8PMHV7LmsQ+QUHCOEfHJWlz 64P5DbxtHf6c7en/KBsrFeEOspx/3Aft9UMl0yg7K49QruujAZjo/03A/qkUQ2/MuX4h9aSlc4BH1 wjcrPZvKVvmrVzOPQrVSjg==; 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 1xTdSA-002Umu-0p; Wed, 18 Nov 2026 12:08:15 +0100 From: Alexander Ivanov To: andrey.zhadchenko@virtuozzo.com Date: Wed, 18 Nov 2026 12:08:13 +0100 Message-ID: <20261118110813.3333232-1-alexander.ivanov@virtuozzo.com> X-Mailer: git-send-email 2.53.0 MIME-Version: 1.0 X-OZ-Fwd: true Cc: devel@openvz.org Subject: [Devel] [PATCH hci-8.2 v4] drivers/vhost/blk: support VIRTIO_BLK_F_WRITE_ZEROES X-BeenThere: devel@openvz.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: OpenVZ development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: devel-bounces@openvz.org Errors-To: devel-bounces@openvz.org Handle VIRTIO_BLK_T_WRITE_ZEROES for parity with virtio-blk. The range is sent to the backend as a single REQ_OP_WRITE_ZEROES bio, with REQ_NOUNMAP unless the guest set the UNMAP flag. The VMM advertises the feature only for a backend with WRITE ZEROES support and sets max_write_zeroes_sectors from its limit, capped at 8388607 so that the range fits in one bio, and max_write_zeroes_seg to 1. The guest gets VIRTIO_BLK_S_UNSUPP when VIRTIO_BLK_F_WRITE_ZEROES was not negotiated, for a payload other than exactly one segment or an unknown flag. It gets VIRTIO_BLK_S_IOERR for a read-only backend, a backend without WRITE ZEROES and a range that is unaligned, beyond the capacity or above these limits. https://virtuozzo.atlassian.net/browse/VSTOR-141684 Feature: vhost-blk: in-kernel accelerator for virtio-blk guests Signed-off-by: Alexander Ivanov --- v4: - one REQ_OP_WRITE_ZEROES bio per request: no chunks, no zero-page fallback, no vhost_blk_retry_zeroes(). - a backend without WRITE ZEROES and a range above bdev_write_zeroes_sectors() get VIRTIO_BLK_S_IOERR. - commit message: the VMM advertises the feature only for a backend with WRITE ZEROES and sets max_write_zeroes_sectors from its limit, see the QEMU patch "vhost-blk: advertise write zeroes with the backend limit". - patch 1 of v3 was sent separately. v3: - commit message: the VMM paragraph says what a large max_write_zeroes_sectors costs and that our QEMU does not advertise the feature yet. v2: - shorten the commit message, state parity with virtio-blk as the reason and what the VMM must advertise. - issue chunks of at most 16 WRITE ZEROES bios and 1 GiB, try them with BLKDEV_ZERO_NOFALLBACK and fall back to 4 MiB chunks of zero pages. - complete the request with VIRTIO_BLK_S_UNSUPP when VIRTIO_BLK_F_WRITE_ZEROES was not negotiated. drivers/vhost/blk.c | 70 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/drivers/vhost/blk.c b/drivers/vhost/blk.c index 7975a0e63d65..d6917f0e662e 100644 --- a/drivers/vhost/blk.c +++ b/drivers/vhost/blk.c @@ -29,7 +29,8 @@ enum { (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | (1ULL << VIRTIO_RING_F_EVENT_IDX) | (1ULL << VIRTIO_BLK_F_MQ) | - (1ULL << VIRTIO_BLK_F_FLUSH), + (1ULL << VIRTIO_BLK_F_FLUSH) | + (1ULL << VIRTIO_BLK_F_WRITE_ZEROES), }; /* @@ -284,6 +285,13 @@ static int vhost_blk_bio_make(struct vhost_blk_req *req, if (unlikely(req->bi_opf & REQ_PREFLUSH)) return vhost_blk_bio_make_simple(req, bdev); + if ((req->bi_opf & REQ_OP_MASK) == REQ_OP_WRITE_ZEROES) { + ret = vhost_blk_bio_make_simple(req, bdev); + if (!ret) + req->bio[0]->bi_iter.bi_size = req->len; + return ret; + } + /* * Requests with unaligned io vectors are not supported by underlined * block device, let's use an aligned intermediary "bounce buffer" to @@ -451,6 +459,51 @@ static bool vhost_blk_backend_ro(struct file *f) bdev_read_only(I_BDEV(f->f_mapping->host)); } +static int vhost_blk_parse_write_zeroes(struct vhost_blk_req *req, u8 *status) +{ + struct file *f = vhost_vq_get_backend(&req->blk_vq->vq); + struct block_device *bdev = I_BDEV(f->f_mapping->host); + sector_t bs_mask = (bdev_logical_block_size(bdev) >> SECTOR_SHIFT) - 1; + unsigned int max_sects = bdev_write_zeroes_sectors(bdev); + struct virtio_blk_discard_write_zeroes seg; + struct iov_iter iter; + u32 flags, nr_sects; + sector_t sector; + + *status = VIRTIO_BLK_S_UNSUPP; + if (!vhost_has_feature(&req->blk_vq->vq, VIRTIO_BLK_F_WRITE_ZEROES) || + req->len != sizeof(seg)) + return 0; + + iov_iter_init(&iter, ITER_SOURCE, req->iov, req->iov_nr, req->len); + if (copy_from_iter(&seg, sizeof(seg), &iter) != sizeof(seg)) + return -EFAULT; + + flags = le32_to_cpu(seg.flags); + if (flags & ~VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP) + return 0; + + sector = le64_to_cpu(seg.sector); + nr_sects = le32_to_cpu(seg.num_sectors); + if (vhost_blk_backend_ro(f) || !max_sects || + nr_sects > UINT_MAX >> SECTOR_SHIFT || nr_sects > max_sects || + (sector | nr_sects) & bs_mask || + sector > bdev_nr_sectors(bdev) || + nr_sects > bdev_nr_sectors(bdev) - sector) { + *status = VIRTIO_BLK_S_IOERR; + return 0; + } + + req->sector = sector; + req->len = (size_t)nr_sects << SECTOR_SHIFT; + req->bi_opf = REQ_OP_WRITE_ZEROES; + if (!(flags & VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP)) + req->bi_opf |= REQ_NOUNMAP; + + *status = VIRTIO_BLK_S_OK; + return 0; +} + static int vhost_blk_req_handle(struct vhost_virtqueue *vq, struct virtio_blk_outhdr *hdr, u16 head, u16 out, u16 in) @@ -490,7 +543,7 @@ static int vhost_blk_req_handle(struct vhost_virtqueue *vq, req->status[0].iov_base = st->iov_base + st->iov_len; req->status[0].iov_len = sizeof(status); - if (type == VIRTIO_BLK_T_OUT) { + if (type == VIRTIO_BLK_T_OUT || type == VIRTIO_BLK_T_WRITE_ZEROES) { data_iov = vq->iov; data_nr = out; } else { @@ -538,6 +591,19 @@ static int vhost_blk_req_handle(struct vhost_virtqueue *vq, req->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH; ret = vhost_blk_req_submit(req); break; + case VIRTIO_BLK_T_WRITE_ZEROES: + ret = vhost_blk_parse_write_zeroes(req, &status); + if (ret) + break; + if (status == VIRTIO_BLK_S_OK && req->len) { + ret = vhost_blk_req_submit(req); + break; + } + ret = vhost_blk_set_status(req, status); + if (ret) + break; + vhost_add_used_and_signal(&blk->dev, vq, head, sizeof(status)); + break; case VIRTIO_BLK_T_GET_ID: len = min_t(size_t, strnlen(blk->serial, VIRTIO_BLK_ID_BYTES) + 1, -- 2.53.0 _______________________________________________ Devel mailing list Devel@openvz.org https://lists.openvz.org/mailman/listinfo/devel