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 06E2480032 for ; Mon, 23 Nov 2026 08:41:47 +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 6AN8eU3v015136; Mon, 23 Nov 2026 11:40:31 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 6AN8eU3v015136 Authentication-Results: mail.openvz.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=virtuozzo.com header.i=@virtuozzo.com header.b="C6CEwL+i" 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 6AN8eIE5015084 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=FAIL) for ; Mon, 23 Nov 2026 11:40:18 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 6AN8eIE5015084 Received: by mail-ed1-f72.google.com with SMTP id 4fb4d7f45d1cf-6b56221d367so2562389a12.1 for ; Mon, 23 Nov 2026 00:40:18 -0800 (PST) X-Forwarded-Encrypted: i=2; AL+hErGk+srpP5cLzpQb3r4yp6KxPGIOsQEWU986CcTUR+rbmoteUH1F/571qqKpeTMcJKCtTGWp0Q==@openvz.org X-Gm-Message-State: APuU4Z9MfDbTLnVVjYcn3vcfVrz9ihZVriHptdmo+t1wNzmRSzoym1qx 0a9KJlNFjet/Y0a8sfueqgFFTjDvxVFfbPZNXPLrntLf68DoTPf40ydgzU+ATbKT5kUrna+fjx/ v/PF3dEeUb/9i6IZFUfYg/KDuFulkWA8ok1OeQI6ezTiJ9CEGBQ1PtA== X-Gm-Gg: AZFdTZcTLm2xxENKwc/G8j5tvYm33ESi7vNr7eynHsJUlSMm7TC6Vex9VGrq0besm71 AoR8widAuvHAiOGQ24hOPL2VhB43gdv8VSaxSQrmwQ9ap78tY0YpIFprEKapsSRQDkkm0PGyltn YvQubs0NO0oAQHer62SnCmDan1x2x2NAhF/mtnF2DVu95YyC0I41wYqYVt2Cn2WL/E+eN8PNYpZ t6QtspupIFhY5IHark+o6Z+sQj+580NeKCy4Ct667BY74cBF6NqyfwxhVaxQqKJuWf+1OasowBD 5SzeIXS8L8wak3zaEDPUE6nHYeJ37S40VSxdL5Wehm2JKdiV3Zpw5V4zqjtAHClydeTE0ZpJQKK ohhZHVTahjrDhXeIJF42csxN5zKA= X-Received: by 2002:a05:6402:210a:b0:6ba:4bf3:4a7 with SMTP id 4fb4d7f45d1cf-6ba4bf3066fmr6533536a12.23.1795423218168; Mon, 23 Nov 2026 00:40:18 -0800 (PST) X-Received: by 2002:a05:6402:210a:b0:6ba:4bf3:4a7 with SMTP id 4fb4d7f45d1cf-6ba4bf3066fmr6533519a12.23.1795423217633; Mon, 23 Nov 2026 00:40:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1795423217; cv=none; d=google.com; s=arc-20260327; b=o+pKO78md33Y1+s6Fzf3ENNvTuDzIg0pRv0a96/CkA+Lzanz+fSSdsZNg38mULQvti WMioCjvFfA1WNphcvwXeEAljPp7rKqq8JtezBXeA9O0qBYYA61UX8rJ98UXMqegWKiiK g8XpnaYXC9tiwqPdE1fr5HiBxchHq21bJAYbJoCY3qCAtkVeIGhV5RvapioCmpstiZm7 7oxSAwTEPswNI6kVG3GU12dPgBF0bruxH91W98jcd6wwJ/YWkVAA8WIqo4/B+HBcV6zE wSvxp2Jrul7G0TgagB1fu4mOthUgURxfkm5D1R887az0cYKVgQ73Ef+1eEZkkuRxWhub yh9g== 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=fh52bpWGIAxJtDr09mK2vBvoYMoS2dyKhU0y+nqtoKc=; fh=CMILZ+goaghq6B4UpCDhf0EQp6kYD0LAlmOkUiVCOvs=; b=T1j3uVvdFFS2q0u+Qg8u/Mgj07x7INr5m0wEVB3DFXBxZkRZ6z6vTnCjTPvirsEF3z SpqJz34QO0ZI/jtCc4t+0DBN4Wgl67UzPNVRLjyPyNN++mVEBIpI9/r+7xL4OGog4cJ5 gwuqDiaOx4yQmZZDRKqurS5URNpL/dkiRY/hFIwa5SF/+lPLsfStAvdw2D3SmmArsTul C9mGCjaX/dX1smhn8XpShuk+b5OR6MNxhgvDMfEZwgwe46wy7LZWBVAg/+FyG+8gjJYs mPPDj+x0FoyR1QDTb3iXcTPDoj4LM+xp/WbxRhQlQzzy9M7Dz1tFtwuKpgS+MFsfAQKA dxMw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@virtuozzo.com header.s=relay header.b=C6CEwL+i; 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-6ba581aee4fsi8076355a12.297.2026.11.23.00.40.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Nov 2026 00:40:17 -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=C6CEwL+i; 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=fh52bpWGIAxJtDr09mK2vBvoYMoS2dyKhU0y+nqtoKc=; b=C6CEwL+inOa2 LTqTgaYAXqtm/RcuYmdNZSmVVLUWp0lc9Vnu9A9zxyc1tSKe7OYxmpbXl7wqMCwWuMhUpgO+RrddQ 9hxEAlrxDzvtIR/Y/yzQeyBkZ4YHNgvOTz5I3XoBR7NDnE152+bCvZGh2MvHM7B8xTAvcA77bW/Ay PhhK6MmGKLyYvwAXiRrUq8qKIPT2fkuIDCF3Bpeu78hxZrYyIu1D315Q3+DNvZfm2+mMEe4W5L3Sc VHSGVsgXSXOA4Ly+0nT9qJPBfCg0k0R8eeiNWQb83xmdgxB4EBwjPhyx6YyQ1edtDG/a8dl2wv9fF 8NueWmerjjhwWQZDE6Lyow==; 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 1xVPar-00Ch1V-0m; Mon, 23 Nov 2026 09:40:09 +0100 From: Alexander Ivanov To: andrey.zhadchenko@virtuozzo.com Date: Mon, 23 Nov 2026 09:40:14 +0100 Message-ID: <20261123084014.176824-3-alexander.ivanov@virtuozzo.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261123084014.176824-1-alexander.ivanov@virtuozzo.com> References: <20261123084014.176824-1-alexander.ivanov@virtuozzo.com> MIME-Version: 1.0 X-OZ-Fwd: true Cc: devel@openvz.org Subject: [Devel] [PATCH hci-8.2 2/2] 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. The segment parser is shared and renamed accordingly. Any flag set in a discard segment completes the request with VIRTIO_BLK_S_UNSUPP, as the spec requires for UNMAP. A request with more than one segment is unsupported, so the VMM must advertise max_discard_seg = 1: a Linux guest reads zero there as seg_max and merges adjacent discards. A discard is a hint, and the guest takes the limits from the VMM, not from the backend. So the range is not checked against the backend discard limit, the bio gets split by it. 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. 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 | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/drivers/vhost/blk.c b/drivers/vhost/blk.c index 816ce4cb77a2..811349b19500 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), }; @@ -152,6 +153,8 @@ static void vhost_blk_req_done(struct bio *bio) int err; err = blk_status_to_errno(bio->bi_status); + if (err == -EOPNOTSUPP && op_is_discard(req->bi_opf)) + err = 0; if (err) req->bio_err = err; @@ -283,7 +286,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 || + op_is_discard(req->bi_opf)) { ret = vhost_blk_bio_make_simple(req, bdev); if (!ret) req->bio[0]->bi_iter.bi_size = req->len; @@ -457,8 +461,10 @@ 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, u32 type, + u8 *status) { + bool discard = type == VIRTIO_BLK_T_DISCARD; 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; @@ -469,7 +475,8 @@ static int vhost_blk_parse_write_zeroes(struct vhost_blk_req *req, u8 *status) 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, discard ? VIRTIO_BLK_F_DISCARD : + VIRTIO_BLK_F_WRITE_ZEROES) || req->len != sizeof(seg)) return 0; @@ -478,13 +485,14 @@ static int vhost_blk_parse_write_zeroes(struct vhost_blk_req *req, u8 *status) return -EFAULT; flags = le32_to_cpu(seg.flags); - if (flags & ~VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP) + if (flags & ~(discard ? 0 : 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 || + if (vhost_blk_backend_ro(f) || + (!discard && (!max_sects || nr_sects > max_sects)) || + nr_sects > UINT_MAX >> SECTOR_SHIFT || (sector | nr_sects) & bs_mask || sector > bdev_nr_sectors(bdev) || nr_sects > bdev_nr_sectors(bdev) - sector) { @@ -494,9 +502,15 @@ 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; - req->bi_opf = REQ_OP_WRITE_ZEROES; - if (!(flags & VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP)) - req->bi_opf |= REQ_NOUNMAP; + if (discard) { + req->bi_opf = REQ_OP_DISCARD; + if (!bdev_max_discard_sectors(bdev)) + req->len = 0; + } else { + 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; @@ -541,7 +555,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 { @@ -589,8 +604,9 @@ 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: case VIRTIO_BLK_T_WRITE_ZEROES: - ret = vhost_blk_parse_write_zeroes(req, &status); + ret = vhost_blk_parse_segment(req, type, &status); if (ret) break; if (status == VIRTIO_BLK_S_OK && req->len) { -- 2.53.0 _______________________________________________ Devel mailing list Devel@openvz.org https://lists.openvz.org/mailman/listinfo/devel