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 184FE80031 for ; Wed, 25 Nov 2026 14:23:31 +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 6APEMFh5028425; Wed, 25 Nov 2026 17:22:16 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 6APEMFh5028425 Authentication-Results: mail.openvz.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=virtuozzo.com header.i=@virtuozzo.com header.b="uNgHD7I+" Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by mail.openvz.org (8.14.4/8.14.4) with ESMTP id 6APELnrc028346 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=FAIL) for ; Wed, 25 Nov 2026 17:21:50 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 6APELnrc028346 Received: by mail-ed1-f72.google.com with SMTP id 4fb4d7f45d1cf-6b6fb975053so78589a12.0 for ; Wed, 25 Nov 2026 06:21:50 -0800 (PST) X-Forwarded-Encrypted: i=2; AL+hErESFs39n2Yp4Tez75GYN6uwBLieQPKXJmCFp1DCr7R6liOCUYCWSRTCFpWEEiwF7yRvj4fG2A==@openvz.org X-Gm-Message-State: APuU4Z/tzDObngA/+EozY1jQxnXjhwcnRfAVA5IIGYWWHVhv4BURJNB2 wapfqcvl0SyK5L2PFjQKyxivoPCiDCT43vcGo8NuZYrA54shySZbgsUkJp8YDH+An3cDjGGR0CT 7yKYVBbuIkiPXCJSHlk/2VN3KFkvNAtvrDlO3zqyfS8CCnmZwfljVEA== X-Gm-Gg: AZFdTZcuU4cCrRu1Ho8h6960YGv7cmNc/AfCZb9G+6NLh17KMQaM3NKb11ewHVPJmGu 5CJVFKq1V0qsdcsMIpKL1aFqj0t+CC9ESTYr7bI0bXjk2QGfNcoc9yx06N0FysSdK5/tkHmjMKS m+5/gffb6l1kK6rfmacuDDtG/BgaV6U0glPQBgTRoydWNpbTs2CIVksj/HoVoqLlZqtC5Tdq8tg UXwnFcvMMTbmuhIjqLsElexu6YcZZ3vBspcs2VdD+8zdCgXj3R6eN0kDa3YCFeBklTVOBwJn1Vg 68/2e3mZqi3Ri4CprTZPvQJ1Xt32OM0B2Coop32FW5/C5pbK0BbKEe0ikAu3MUnCTmZZP0wkkLr cJUU0hiIxTWXYKDq+ACCQIM8zRwo= X-Received: by 2002:a05:6402:895:b0:6ba:ad4:2c43 with SMTP id 4fb4d7f45d1cf-6ba37c7a76fmr10139614a12.5.1795616509666; Wed, 25 Nov 2026 06:21:49 -0800 (PST) X-Received: by 2002:a05:6402:895:b0:6ba:ad4:2c43 with SMTP id 4fb4d7f45d1cf-6ba37c7a76fmr10139583a12.5.1795616509198; Wed, 25 Nov 2026 06:21:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1795616509; cv=none; d=google.com; s=arc-20260327; b=LQphfVpqGY+DByK3pmdz628gnUsumCoD5zS1EWdx2aDxKGaR29jjb2prxpC7B1iG44 GI3NRUqWI97Btzi+tMwOuG0WWVfwyHRYYaYq+mS2QBFySO3KHyzmXjCsb2mYKmltHd1g ijx0vdi/o9nZdKwU/RS0ixkZH8UsJ9gI5BkXbQ8Ev20oWQAQdKGYbixyvlM4A5VET9Ey RWEpPsY7PBwMCkFJSLEW5YLONyRzMUBvBtEpkYrY6Hhkg2L7BfEzqUjwx7F4RO9WhBN9 KGoVkWeHwvK7L3H5NObw9dVLvRc6wlOnW4qmAwityrBq9vJX/m+R3igT32d/CG0cm4Rx 6gFw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=uTtkdeTYgcBLPz5WbMQEv0ZZKf/pLgqqoiwKkZDmklc=; fh=CMILZ+goaghq6B4UpCDhf0EQp6kYD0LAlmOkUiVCOvs=; b=YPZerUR3hXfmGve3lDW8hoD0Ph+A8ghZ+JzhwTMnfVLB5608YuLcYu53F+JNiACYqR Give8kCc4x98sToCVV4Cd3H+lmk32j35dzzayDUO3+rOOjn3RENab+3DS9IuFwDpGPm7 OOfbiGvLWkAduA7zedJuYvBWQIdmEEo/l7Q8piRXsWE+f4xmTKtVMNF5LPyt0v2sth9N YAJGAZgBR+SRJhIJ3X4+OdpWIEX7dR0Q91RfJIa9Q/YjgwD57Ua6QFmmHMdvGesdT164 Bmldmn6o2XsS7WCKqr220y40PXvPLlIw2/U0oCfZ2tQ30hU877yCFonV7cHOQDj9/7qp KWNA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@virtuozzo.com header.s=relay header.b=uNgHD7I+; 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 4fb4d7f45d1cf-6ba3738ac21si15253909a12.170.2026.11.25.06.21.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Nov 2026 06:21:49 -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=uNgHD7I+; 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=uTtkdeTYgcBLPz5WbMQEv0ZZKf/pLgqqoiwKkZDmklc=; b=uNgHD7I+ufMg WGMR7QCNc7CBZGrplaNiRnMmjlx588QzReq9xDvCjrIfPpUfkT/+hI1F9lRwysU3ZDEs63FcDZ/EN 8TCCnGnaKkaSF9giw9ollpo0lc94PbNlLvZkDtenxX69bpv8fhK9r8rQalgiXnGoHiuvePBWtra8p Qdk5yAlIhndRlNscji4liaSPxzWP21Ds3aVTeyQhL02RI9066NtpoyyABnQeBi4iV9TgM2S2yEo/O NCmJwjOF0ixwwJRPm0bNOrhw3OJnDiFU0DxXe0y8j7pw3loDoKM9D9BtPcvltVSSX7M7lq5tMXWmz FXiM7WHyzWWPa5rjaECliQ==; 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 1xWDsP-009Fgm-1A; Wed, 25 Nov 2026 15:21:48 +0100 From: Alexander Ivanov To: andrey.zhadchenko@virtuozzo.com Date: Wed, 25 Nov 2026 15:21:45 +0100 Message-ID: <20261125142145.786278-5-alexander.ivanov@virtuozzo.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261125142145.786278-1-alexander.ivanov@virtuozzo.com> References: <20261125142145.786278-1-alexander.ivanov@virtuozzo.com> MIME-Version: 1.0 X-OZ-Fwd: true Cc: devel@openvz.org Subject: [Devel] [PATCH hci-8.2 v4 4/4] drivers/vhost/blk: support VIRTIO_BLK_F_DISCARD 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 virtio-blk in QEMU supports discard, while vhost-blk does not offer VIRTIO_BLK_F_DISCARD, so a guest on vhost-blk cannot release unused blocks of its disk. Handle VIRTIO_BLK_T_DISCARD like a write-zeroes request: a single segment taken from the device-readable part, the same read-only, alignment and capacity checks, and the range sent to the backend as a single REQ_OP_DISCARD bio. Any flag set in a discard segment completes the request with VIRTIO_BLK_S_UNSUPP, as the spec requires for UNMAP. The VMM must set max_discard_seg to 1: a request with more than one segment is unsupported, and a Linux guest reads zero there as seg_max and merges discards into multi-range requests. It must also cap max_discard_sectors at 8388607 rounded down to the logical block size, so that the range fits in one bio. QEMU with the discard property on reports zero for both until "vhost-blk: advertise discard limits in the config space", so it needs that change. Nor may the VMM offer VIRTIO_BLK_F_DISCARD for a drive whose discards are to be ignored (QEMU discard=ignore): vhost-blk sends every discard to the backend. The guest takes the limits from the VMM, not from the backend, so the range is not checked against the backend discard limit: the block layer splits the bio by it. A discard is a hint, so, as QEMU does, complete the request with VIRTIO_BLK_S_OK without issuing it when the backend has no discard support, and treat -EOPNOTSUPP from the bio as success: the limit can drop to zero after the check, e.g. on a dm table reload, and a stacked driver may reject a discard while still advertising one. A discard in flight while support is lost may fail with BLK_STS_TARGET instead, when dm or sd drop the limit on a target error. So any failure of a discard counts as success once the backend discard limit is 0. https://virtuozzo.atlassian.net/browse/VSTOR-141683 Feature: vhost-blk: in-kernel accelerator for virtio-blk guests Signed-off-by: Alexander Ivanov --- drivers/vhost/blk.c | 96 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 74 insertions(+), 22 deletions(-) diff --git a/drivers/vhost/blk.c b/drivers/vhost/blk.c index 5b8522729d18..bfd4bde0bcff 100644 --- a/drivers/vhost/blk.c +++ b/drivers/vhost/blk.c @@ -28,6 +28,7 @@ enum { VHOST_BLK_FEATURES = VHOST_FEATURES | (1ULL << VIRTIO_BLK_F_MQ) | (1ULL << VIRTIO_BLK_F_FLUSH) | + (1ULL << VIRTIO_BLK_F_DISCARD) | (1ULL << VIRTIO_BLK_F_WRITE_ZEROES), }; @@ -155,10 +156,14 @@ static void vhost_blk_req_done(struct bio *bio) err = blk_status_to_errno(bio->bi_status); if (err) { bdev = I_BDEV(req->blk->backend->f_mapping->host); - if ((req->bi_opf & REQ_OP_MASK) == REQ_OP_WRITE_ZEROES && - !bdev_write_zeroes_sectors(bdev)) + if ((req->bi_opf & REQ_OP_MASK) == REQ_OP_DISCARD && + (err == -EOPNOTSUPP || !bdev_max_discard_sectors(bdev))) + err = 0; + else if ((req->bi_opf & REQ_OP_MASK) == REQ_OP_WRITE_ZEROES && + !bdev_write_zeroes_sectors(bdev)) err = -EOPNOTSUPP; - req->bio_err = err; + if (err) + req->bio_err = err; } /* @@ -289,7 +294,8 @@ 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) { + if ((req->bi_opf & REQ_OP_MASK) == REQ_OP_WRITE_ZEROES || + (req->bi_opf & REQ_OP_MASK) == REQ_OP_DISCARD) { ret = vhost_blk_bio_make_simple(req, bdev); if (!ret) req->bio[0]->bi_iter.bi_size = req->len; @@ -463,18 +469,19 @@ 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) +static int vhost_blk_parse_segment(struct vhost_blk_req *req, struct file *f, + unsigned int feature, u32 flags_mask, + u32 *flags, 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; struct virtio_blk_discard_write_zeroes seg; struct iov_iter iter; - u32 flags, nr_sects; + u32 nr_sects; sector_t sector; *status = VIRTIO_BLK_S_UNSUPP; - if (!vhost_has_feature(&req->blk_vq->vq, VIRTIO_BLK_F_WRITE_ZEROES) || + if (!vhost_has_feature(&req->blk_vq->vq, feature) || req->len != sizeof(seg)) return 0; @@ -482,8 +489,8 @@ static int vhost_blk_parse_write_zeroes(struct vhost_blk_req *req, u8 *status) 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) + *flags = le32_to_cpu(seg.flags); + if (*flags & ~flags_mask) return 0; sector = le64_to_cpu(seg.sector); @@ -499,14 +506,61 @@ static int vhost_blk_parse_write_zeroes(struct vhost_blk_req *req, u8 *status) req->sector = sector; req->len = (size_t)nr_sects << SECTOR_SHIFT; + *status = VIRTIO_BLK_S_OK; + return 0; +} + +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); + u32 flags; + int ret; + + ret = vhost_blk_parse_segment(req, f, VIRTIO_BLK_F_WRITE_ZEROES, + VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP, + &flags, status); + if (ret || *status != VIRTIO_BLK_S_OK) + return ret; + req->bi_opf = REQ_OP_WRITE_ZEROES; if (!(flags & VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP)) req->bi_opf |= REQ_NOUNMAP; + return 0; +} - *status = VIRTIO_BLK_S_OK; +static int vhost_blk_parse_discard(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); + u32 flags; + int ret; + + ret = vhost_blk_parse_segment(req, f, VIRTIO_BLK_F_DISCARD, + 0, &flags, status); + if (ret || *status != VIRTIO_BLK_S_OK) + return ret; + + req->bi_opf = REQ_OP_DISCARD; + if (!bdev_max_discard_sectors(bdev)) + req->len = 0; return 0; } +static int vhost_blk_req_submit_or_complete(struct vhost_blk_req *req, + u8 status) +{ + int ret; + + if (status == VIRTIO_BLK_S_OK && req->len) + return vhost_blk_req_submit(req); + + ret = vhost_blk_set_status(req, status); + if (!ret) + vhost_add_used_and_signal(&req->blk->dev, &req->blk_vq->vq, + req->head, sizeof(status)); + return ret; +} + static int vhost_blk_req_handle(struct vhost_virtqueue *vq, struct virtio_blk_outhdr *hdr, u16 head, u16 out, u16 in) @@ -546,7 +600,8 @@ 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 || type == VIRTIO_BLK_T_WRITE_ZEROES) { + if (type == VIRTIO_BLK_T_OUT || type == VIRTIO_BLK_T_DISCARD || + type == VIRTIO_BLK_T_WRITE_ZEROES) { data_iov = vq->iov; data_nr = out; } else { @@ -594,18 +649,15 @@ 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_DISCARD: + ret = vhost_blk_parse_discard(req, &status); + if (!ret) + ret = vhost_blk_req_submit_or_complete(req, status); + 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)); + if (!ret) + ret = vhost_blk_req_submit_or_complete(req, status); break; case VIRTIO_BLK_T_GET_ID: len = min_t(size_t, -- 2.53.0 _______________________________________________ Devel mailing list Devel@openvz.org https://lists.openvz.org/mailman/listinfo/devel