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 51E6B80067 for ; Tue, 1 Dec 2026 12:51:20 +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 6B1Cnx0k008054; Tue, 1 Dec 2026 15:50:01 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 6B1Cnx0k008054 Authentication-Results: mail.openvz.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=virtuozzo.com header.i=@virtuozzo.com header.b="TlG5oc7g" Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) by mail.openvz.org (8.14.4/8.14.4) with ESMTP id 6B1CnaiC007994 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=FAIL) for ; Tue, 1 Dec 2026 15:49:37 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 6B1CnaiC007994 Received: by mail-ej1-f69.google.com with SMTP id a640c23a62f3a-c506f007ae2so252203466b.1 for ; Tue, 01 Dec 2026 04:49:37 -0800 (PST) X-Forwarded-Encrypted: i=2; AL+hErGL5E5UpDNZ2ww6wcENsJTss+nr9IC4F0uTu6cztywp5QfuDeZ6xI+B3iF2SIgZw2vkdo273g==@openvz.org X-Gm-Message-State: APuU4Z8kUJRqrit3gZQdbV+3rpht/2qm5FRoVsT4Ri+otOrxVSUkOr5b ca7jzRv4cPqpQfui9uEFJWvPo96YaqdmpsGeNdHA441vXxhGMUVYJnLA4yEq3RmZ0TbRoIqbpBM CBJkZ0mkZrfx59ehn08+Lm3G5+nSnzO33Ry3DdMRvXnevCglXMLBgvQ== X-Gm-Gg: AZFdTZd9t9y/YHILp0Ixyr/ESfSU5W1wTC7U7jR+si4CgOK2xrU6ro1sizbSUlYl1XC uwT9E56zumOUeK0PDR5k3LcqnYjEeAt+zDqRw1ephIcDVoH19F3VUZaSct8VgRVf7yxvNkIdror 9qI/4DA4q7ysyatMrb0vVJ+fhIKF4dQMKSHffqWrJ6ID9mDWc92GVZTirm35FaEiZBnNoOkbyQM QOyGPJ4GdjjE8m+oWsNSuZ71uVuubB2rx5zN/b05oGR8JwmafaEgFEzmlgPk/XeAV3eRYFOOV5J nzWP/N3eqdiyVq3ES5ozyfXHEZoWf4OnGGQnWqdsZ1JVSsUM5ajzVadcshB53pwgM89TJqaEfaT 0lhFGaooDFu7dKKUeQrzD+bXiSpQ= X-Received: by 2002:a17:906:57d0:b0:c40:d920:746f with SMTP id a640c23a62f3a-c513aadedddmr2460877866b.46.1796129376368; Tue, 01 Dec 2026 04:49:36 -0800 (PST) X-Received: by 2002:a17:906:57d0:b0:c40:d920:746f with SMTP id a640c23a62f3a-c513aadedddmr2460876366b.46.1796129375824; Tue, 01 Dec 2026 04:49:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1796129375; cv=none; d=google.com; s=arc-20260327; b=SMk6PjdotROyTrYg+dSAm2fwaylEvct7FibY1il1aszFujiJ28hx/8MO5q8cxQrYfv eOoVafX3IEAnczTAs6EBH+EgIrjrbcEkdyiNOj0CDag7O/kyex00fnqvyg/+o+vp/pTP qPwNqbl9lZYT+tNZODcwsHb+m4bu0vCDtJWuJKoyeRw7m64NMXR0n7Olf/psTSW1UxqE ChxUPwz8F2KeZji32smLkKhPp5u4nBNcRfX++vBtiYXXGaLg/FBOj7P0kEdSEtuxJdp0 q3zr/2KCI8rkvOGtLVM0vFaNUo5io2UeUIYVpisrlYu6hFZchGp0vbO5bSNgpxF59gdW RiBA== 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=sR/QkmoA8QN26xt+mjd6azb2YGkG5mRkw/jGGc21OiY=; fh=CMILZ+goaghq6B4UpCDhf0EQp6kYD0LAlmOkUiVCOvs=; b=Dja43il/l2QrqXvPwKrncDny30ni+ZlOM6ylkY2lFtyTgJaFGI4NTQfjjSL2jeJlFH ZAvvcuEA9glho6HwnV/2+uXA3ZODLFQKbWgzY9Sz7qrj+QBMAik1W7Or6qfBpHuZ+PZe dMTOAhAw/VX++PxloV4CpxmxRe1Y/FYHcmWEYw3ABAMcnpN7HosJlfYj4Gb9FWFg+fyj fax0QnWVbCbupJSz81QIbUmTjUJscSY2bqN8E9cJOu7+TY3pNEidS+WX0Kle7G02X8g9 Tqshgt2emr2RVQ3iwJBt3rOCGYrgR17c16B7yBHu46PjQAPNWS4fMWVvOkVkmNsQgHIE U1rQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@virtuozzo.com header.s=relay header.b=TlG5oc7g; 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 a640c23a62f3a-c51bf4423casi1170939366b.172.2026.12.01.04.49.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 04:49:35 -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=TlG5oc7g; 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=sR/QkmoA8QN26xt+mjd6azb2YGkG5mRkw/jGGc21OiY=; b=TlG5oc7gd1o6 TSCpgAX7mw6laMVcb6AZ7vuRxNwMUk8nTZmWW1rsk+4RBYiipBdg/PUGAvnbkDu7+YT77OYAHDfRV IB9ETeQORbqvDxlcSeevnYLID4tgYnrHDLmne7Qg5pVu3IQiYV4gyhzvVQQGUfSLlwVr4f4D7TjQf vB03xeRxjEPEI72ahPdfyT9odJI5taep7IwS5+XXT6LVuQUHqQHb4mDBWucXTnT/NkytWe2NdgUOv ubY4narb+kv6yKTvU8bcROPKbCrkFw4maz7O+BKjQl96pxWFfz7IcZ66W3F8dAf7R7E0GVOLnm0Eh Nn2xmMqwwCI5HXxSRxSsyw==; 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 1xYNII-004oY7-39; Tue, 01 Dec 2026 13:49:25 +0100 From: Alexander Ivanov To: andrey.zhadchenko@virtuozzo.com Date: Tue, 1 Dec 2026 13:49:30 +0100 Message-ID: <20261201124931.1969095-4-alexander.ivanov@virtuozzo.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261201124931.1969095-1-alexander.ivanov@virtuozzo.com> References: <20261201124931.1969095-1-alexander.ivanov@virtuozzo.com> MIME-Version: 1.0 X-OZ-Fwd: true Cc: devel@openvz.org Subject: [Devel] [PATCH hci-8.2 v4 3/4] drivers/vhost/blk: hold requests failed with ENOSPC 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 When the image of a vhost-blk disk runs out of space, dm-qcow2 or dm-ploop with report_enospc fails the bio with ENOSPC, and vhost-blk passes it to the guest as VIRTIO_BLK_S_IOERR. The guest gets a write error, where QEMU with werror=enospc would pause the VM until the space is freed. Let QEMU choose with VHOST_BLK_SET_ERROR_POLICY the requests to hold on ENOSPC: writes (with flushes, write zeroes and discards, as werror covers them in QEMU) and reads. A held request is not put to the used ring, the eventfd set with VHOST_BLK_SET_ERROR_FD is signalled, and QEMU reads the list of held requests with VHOST_BLK_GET_HELD_REQS to pause the VM and resubmit them on resume. A request split into several bios is held only if none of them failed with an error other than ENOSPC. A held request is not counted as inflight, so that unsetting the backend does not wait for it. While the backend is set its head stays taken, so a guest reusing it gets the same error as for a head in flight. The request slots are freed with the backend, so after a restart a guest reusing the head gets it accepted; only that guest is hurt. The list is kept when the backend is unset, as the stop path of QEMU must read it after all requests in flight are completed. It is emptied by setting a new backend, when the vrings and their heads may be different, and by VHOST_RESET_OWNER together with the policy and the eventfd. A failing VHOST_BLK_SET_BACKEND keeps it and adds the requests held by the vqs it had started, as QEMU still has to resubmit the saved heads. The new ioctls are allowed only to the owner and run under dev.mutex, so that they do not race with VHOST_RESET_OWNER dropping the eventfd. https://virtuozzo.atlassian.net/browse/VSTOR-152175 Feature: vhost-blk: in-kernel accelerator for virtio-blk guests Signed-off-by: Alexander Ivanov Reviewed-by: Sashiko --- drivers/vhost/blk.c | 179 ++++++++++++++++++++++++++++++- include/uapi/linux/vhost.h | 20 ++++ include/uapi/linux/vhost_types.h | 21 ++++ 3 files changed, 217 insertions(+), 3 deletions(-) diff --git a/drivers/vhost/blk.c b/drivers/vhost/blk.c index d8ea714f71ae..f67d8f9fee75 100644 --- a/drivers/vhost/blk.c +++ b/drivers/vhost/blk.c @@ -51,6 +51,9 @@ enum { #define NR_INLINE 16 +#define VHOST_BLK_HOLD_ALL (VHOST_BLK_HOLD_ENOSPC_WRITE | \ + VHOST_BLK_HOLD_ENOSPC_READ) + #define vhostblk_vq_err(blk, vq, fmt, ...) do { \ pr_warn_ratelimited(KBUILD_MODNAME "(%d) : " fmt, \ blk->owner_pid, ##__VA_ARGS__); \ @@ -76,6 +79,7 @@ struct vhost_blk_req { sector_t sector; int bi_opf; + u32 type; u16 head; size_t len; int bio_err; @@ -105,6 +109,11 @@ struct vhost_blk { struct file *backend; char serial[VIRTIO_BLK_ID_BYTES]; pid_t owner_pid; + u64 error_policy; + struct mutex held_mutex; + struct eventfd_ctx *error_ctx; + struct vhost_blk_held_req *held; + unsigned int nr_held; }; static int move_iovec(struct iovec *from, struct iovec *to, @@ -162,9 +171,11 @@ static void vhost_blk_req_done(struct bio *bio) else if ((req->bi_opf & REQ_OP_MASK) == REQ_OP_WRITE_ZEROES && !bdev_write_zeroes_sectors(bdev)) err = -EOPNOTSUPP; - if (err) - req->bio_err = err; } + if (err == -ENOSPC) + cmpxchg(&req->bio_err, 0, err); + else if (err) + WRITE_ONCE(req->bio_err, err); /* * Bounce buffer adds kvec to bio (instead of user backed memory), @@ -587,8 +598,9 @@ static int vhost_blk_req_handle(struct vhost_virtqueue *vq, req->sector = vhost64_to_cpu(vq, hdr->sector); req->iov = blk_vq->iov; req->bio_err = 0; + req->type = vhost32_to_cpu(vq, hdr->type); in_len = iov_length(vq->iov + out, in); - type = vhost32_to_cpu(vq, hdr->type); + type = req->type; if (in_len < sizeof(status) || in_len > INT_MAX) return -EINVAL; @@ -770,6 +782,29 @@ static void vhost_blk_handle_guest_kick(struct vhost_work *work) mutex_unlock(&vq->mutex); } +static bool vhost_blk_hold_req(struct vhost_blk_req *req) +{ + struct vhost_blk *blk = req->blk; + u64 flag = req->bi_opf == REQ_OP_READ ? VHOST_BLK_HOLD_ENOSPC_READ : + VHOST_BLK_HOLD_ENOSPC_WRITE; + + if (req->bio_err != -ENOSPC || !(READ_ONCE(blk->error_policy) & flag)) + return false; + + mutex_lock(&blk->held_mutex); + blk->held[blk->nr_held++] = (struct vhost_blk_held_req) { + .vq = req->blk_vq - blk->vqs, + .head = req->head, + .error = -req->bio_err, + .type = req->type, + }; + if (blk->error_ctx) + eventfd_signal(blk->error_ctx); + mutex_unlock(&blk->held_mutex); + + return true; +} + static void vhost_blk_handle_host_kick(struct vhost_work *work) { struct vhost_blk_vq *blk_vq; @@ -799,6 +834,12 @@ static void vhost_blk_handle_host_kick(struct vhost_work *work) } vhost_blk_req_cleanup(req); + /* req->inflight stays set: the guest must not reuse the head */ + if (vhost_blk_hold_req(req)) { + forget_request(req); + continue; + } + if (!req->bio_err) status = VIRTIO_BLK_S_OK; else if (req->bio_err == -EOPNOTSUPP && @@ -905,11 +946,36 @@ static void vhost_blk_drop_backend(struct vhost_blk *blk) blk->backend = NULL; } +static void vhost_blk_reset_held(struct vhost_blk *blk, + struct vhost_blk_held_req *held, + unsigned int nr_held) +{ + mutex_lock(&blk->held_mutex); + kvfree(blk->held); + blk->held = held; + blk->nr_held = nr_held; + mutex_unlock(&blk->held_mutex); +} + static int vhost_blk_setup_vqs(struct vhost_blk *blk) { + struct vhost_blk_held_req *held; struct vhost_virtqueue *vq; + unsigned int nr = 0, saved = blk->nr_held; int ret, i; + for (i = 0; i < VHOST_BLK_VQ_MAX; i++) { + vq = &blk->vqs[i].vq; + if (vhost_vq_is_setup(vq)) + nr += vq->num; + } + + held = kvcalloc(saved + nr, sizeof(*held), GFP_KERNEL); + if (!held) + return -ENOMEM; + memcpy(held, blk->held, saved * sizeof(*held)); + vhost_blk_reset_held(blk, held, saved); + for (i = 0; i < VHOST_BLK_VQ_MAX; i++) { vq = &blk->vqs[i].vq; @@ -929,6 +995,10 @@ static int vhost_blk_setup_vqs(struct vhost_blk *blk) return ret; } + mutex_lock(&blk->held_mutex); + blk->nr_held -= saved; + memmove(blk->held, blk->held + saved, blk->nr_held * sizeof(*held)); + mutex_unlock(&blk->held_mutex); return 0; } @@ -962,6 +1032,7 @@ static int vhost_blk_open(struct inode *inode, struct file *file) blk->during_flush = 0; blk->new_req_bin = 0; spin_lock_init(&blk->flush_lock); + mutex_init(&blk->held_mutex); init_waitqueue_head(&blk->flush_wait); blk->owner_pid = current->pid; @@ -981,11 +1052,21 @@ static int vhost_blk_open(struct inode *inode, struct file *file) return ret; } +static void vhost_blk_reset_errors(struct vhost_blk *blk) +{ + vhost_blk_reset_held(blk, NULL, 0); + WRITE_ONCE(blk->error_policy, 0); + if (blk->error_ctx) + eventfd_ctx_put(blk->error_ctx); + blk->error_ctx = NULL; +} + static int vhost_blk_release(struct inode *inode, struct file *f) { struct vhost_blk *blk = f->private_data; vhost_blk_drop_backend(blk); + vhost_blk_reset_errors(blk); vhost_dev_stop(&blk->dev); vhost_dev_cleanup(&blk->dev); kfree(blk->dev.vqs); @@ -1084,6 +1165,7 @@ static long vhost_blk_reset_owner(struct vhost_blk *blk) goto done; } vhost_blk_drop_backend(blk); + vhost_blk_reset_errors(blk); vhost_dev_stop(&blk->dev); vhost_dev_reset_owner(&blk->dev, umem); @@ -1092,6 +1174,88 @@ static long vhost_blk_reset_owner(struct vhost_blk *blk) return err; } +static long vhost_blk_set_error_fd(struct vhost_blk *blk, int __user *argp) +{ + struct eventfd_ctx *ctx = NULL; + int fd; + + if (get_user(fd, argp)) + return -EFAULT; + + if (fd != -1) { + ctx = eventfd_ctx_fdget(fd); + if (IS_ERR(ctx)) + return PTR_ERR(ctx); + } + + mutex_lock(&blk->held_mutex); + swap(ctx, blk->error_ctx); + mutex_unlock(&blk->held_mutex); + + if (ctx) + eventfd_ctx_put(ctx); + return 0; +} + +static int vhost_blk_get_held_nr(struct vhost_blk_held_reqs __user *argp, + u32 *nr) +{ + struct vhost_blk_held_reqs hdr; + + if (copy_from_user(&hdr, argp, sizeof(hdr))) + return -EFAULT; + if (hdr.reserved) + return -EINVAL; + *nr = hdr.nr; + return 0; +} + +static long vhost_blk_get_held_reqs(struct vhost_blk *blk, + struct vhost_blk_held_reqs __user *argp) +{ + u32 nr, nr_held; + long ret; + + ret = vhost_blk_get_held_nr(argp, &nr); + if (ret) + return ret; + + mutex_lock(&blk->held_mutex); + nr_held = blk->nr_held; + mutex_unlock(&blk->held_mutex); + + if (nr < nr_held) + ret = -E2BIG; + else if (copy_to_user(argp->reqs, blk->held, + nr_held * sizeof(*blk->held))) + return -EFAULT; + + if (put_user(nr_held, &argp->nr)) + return -EFAULT; + return ret; +} + +static long vhost_blk_error_ioctl(struct vhost_blk *blk, unsigned int ioctl, + void __user *argp) +{ + u64 policy; + + switch (ioctl) { + case VHOST_BLK_SET_ERROR_POLICY: + if (get_user(policy, (u64 __user *)argp)) + return -EFAULT; + if (policy & ~VHOST_BLK_HOLD_ALL) + return -EOPNOTSUPP; + WRITE_ONCE(blk->error_policy, policy); + return 0; + case VHOST_BLK_SET_ERROR_FD: + return vhost_blk_set_error_fd(blk, argp); + case VHOST_BLK_GET_HELD_REQS: + return vhost_blk_get_held_reqs(blk, argp); + } + return -ENOIOCTLCMD; +} + static long vhost_blk_ioctl(struct file *f, unsigned int ioctl, unsigned long arg) { @@ -1111,6 +1275,15 @@ static long vhost_blk_ioctl(struct file *f, unsigned int ioctl, if (copy_from_user(blk->serial, argp, VIRTIO_BLK_ID_BYTES)) return -EFAULT; return 0; + case VHOST_BLK_SET_ERROR_POLICY: + case VHOST_BLK_SET_ERROR_FD: + case VHOST_BLK_GET_HELD_REQS: + mutex_lock(&blk->dev.mutex); + ret = vhost_dev_check_owner(&blk->dev); + if (!ret) + ret = vhost_blk_error_ioctl(blk, ioctl, argp); + mutex_unlock(&blk->dev.mutex); + return ret; case VHOST_GET_FEATURES: features = VHOST_BLK_FEATURES; if (copy_to_user(featurep, &features, sizeof(features))) diff --git a/include/uapi/linux/vhost.h b/include/uapi/linux/vhost.h index 4371703948b9..87acc4737347 100644 --- a/include/uapi/linux/vhost.h +++ b/include/uapi/linux/vhost.h @@ -276,4 +276,24 @@ #define VHOST_BLK_SET_BACKEND _IOW(VHOST_VIRTIO, 0xFF, \ struct vhost_vring_file) + +/* + * Hold a request failing with an error selected by VHOST_BLK_HOLD_* flags + * instead of completing it. The guest gets its head back only through + * VHOST_BLK_RESUBMIT. + */ +#define VHOST_BLK_SET_ERROR_POLICY _IOW(VHOST_VIRTIO, 0xFD, __u64) + +/* An eventfd signalled on every held request, -1 to unset */ +#define VHOST_BLK_SET_ERROR_FD _IOW(VHOST_VIRTIO, 0xFC, int) + +/* + * nr is the capacity of reqs on input and the number of held requests on + * output; -E2BIG if they do not fit. reserved must be 0. The list is final + * once the backend is unset, and is emptied by VHOST_RESET_OWNER and by a + * successful VHOST_BLK_SET_BACKEND with fd >= 0. A failing one keeps it and + * adds the requests held by the vqs it had started, so read it again after. + */ +#define VHOST_BLK_GET_HELD_REQS _IOWR(VHOST_VIRTIO, 0xFB, \ + struct vhost_blk_held_reqs) #endif diff --git a/include/uapi/linux/vhost_types.h b/include/uapi/linux/vhost_types.h index 1c39cc5f5a31..c0fbdfd29e1d 100644 --- a/include/uapi/linux/vhost_types.h +++ b/include/uapi/linux/vhost_types.h @@ -198,4 +198,25 @@ struct vhost_vdpa_iova_range { /* IOTLB don't flush memory mapping across device reset */ #define VHOST_BACKEND_F_IOTLB_PERSIST 0x8 +/* + * VHOST_BLK_SET_ERROR_POLICY flags. ENOSPC_WRITE covers every request but a + * read: writes, flushes, write zeroes and discards, as werror does in QEMU. + */ +#define VHOST_BLK_HOLD_ENOSPC_WRITE (1ULL << 0) +#define VHOST_BLK_HOLD_ENOSPC_READ (1ULL << 1) + +struct vhost_blk_held_req { + __u16 vq; + __u16 head; + __s32 error; /* positive errno */ + __u32 type; /* VIRTIO_BLK_T_* */ + __u32 reserved; +}; + +struct vhost_blk_held_reqs { + __u32 nr; + __u32 reserved; + struct vhost_blk_held_req reqs[]; +}; + #endif -- 2.53.0 _______________________________________________ Devel mailing list Devel@openvz.org https://lists.openvz.org/mailman/listinfo/devel