From: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
To: andrey.zhadchenko@virtuozzo.com
Cc: andrey.drobyshev@virtuozzo.com, svt-core@virtuozzo.com
Subject: [PATCH hci-8.2 v4 4/5] vhost-blk: pause the VM on a request failed with ENOSPC #VSTOR-152175
Date: Tue, 1 Dec 2026 10:18:17 +0100 [thread overview]
Message-ID: <20261201091818.1894308-5-alexander.ivanov@virtuozzo.com> (raw)
In-Reply-To: <20261201091818.1894308-1-alexander.ivanov@virtuozzo.com>
vhost-blk completes requests in the kernel, so werror and rerror have
no effect: dm-qcow2 mounted with report_enospc fails a write to a full
image at once, and the guest gets the error.
The kernel can hold such a request instead of completing it. Set the
hold policy from werror and rerror on every start, and pass an eventfd
the kernel signals on a held request. On the event stop the VM with the
io-error reason and emit BLOCK_IO_ERROR, as the block layer does for
other devices. The device field is the id of the proxy, which is the
disk alias libvirt looks the disk up by.
The kernel holds only ENOSPC, so the stop policy holds it too and
still reports other errors to the guest.
The kernel resubmits a held request by its vq and head, and only after
the backend is set anew. So read the list of held requests when vhost
stops, once the backend is unset and the list is final, and resubmit
them on the next start. VHOST_RESET_OWNER empties the list in the
kernel, and a device reset drops the saved one, as the guest rings are
gone.
A request may also be held while the VM is not running, when the stop
drains the requests in flight. Do not report it then: it is resubmitted
when the VM runs again, and pauses the VM if it fails again. A request
held while the guest is suspended is not reported either: a wakeup
resets the device, which drops it along with the rings.
Probe the ioctls at realize. On a kernel without them warn and run as
before.
https://virtuozzo.atlassian.net/browse/VSTOR-152175
Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
---
hw/block/vhost-blk.c | 177 +++++++++++++++++++++++++++++++++-
include/hw/virtio/vhost-blk.h | 9 ++
2 files changed, 185 insertions(+), 1 deletion(-)
diff --git a/hw/block/vhost-blk.c b/hw/block/vhost-blk.c
index 124885a2b30..19003def393 100644
--- a/hw/block/vhost-blk.c
+++ b/hw/block/vhost-blk.c
@@ -32,6 +32,102 @@
#include "migration/blocker.h"
#include "migration/cpr.h"
#include "system/runstate.h"
+#include "qapi/qapi-events-block-core.h"
+
+static uint64_t vhost_blk_error_policy(VHostBlk *s)
+{
+ uint64_t policy = 0;
+
+ if (s->conf.conf.werror != BLOCKDEV_ON_ERROR_REPORT) {
+ policy |= VHOST_BLK_HOLD_ENOSPC_WRITE;
+ }
+ if (s->conf.conf.rerror != BLOCKDEV_ON_ERROR_REPORT) {
+ policy |= VHOST_BLK_HOLD_ENOSPC_READ;
+ }
+ return policy;
+}
+
+static struct vhost_blk_held_reqs *vhost_blk_get_held(VHostBlk *s)
+{
+ uint32_t nr = s->conf.num_queues * s->conf.queue_size;
+ struct vhost_blk_held_reqs *reqs;
+
+ if (!s->hold_supported) {
+ return NULL;
+ }
+
+ reqs = g_malloc0(sizeof(*reqs) + nr * sizeof(reqs->reqs[0]));
+ reqs->nr = nr;
+ if (ioctl(s->vhostfd, VHOST_BLK_GET_HELD_REQS, reqs)) {
+ error_report("vhost-blk: unable to get held requests: %s",
+ strerror(errno));
+ g_free(reqs);
+ return NULL;
+ }
+ return reqs;
+}
+
+static void vhost_blk_save_held(VHostBlk *s)
+{
+ g_autofree struct vhost_blk_held_reqs *reqs = vhost_blk_get_held(s);
+ int i;
+
+ g_free(s->held);
+ s->held = NULL;
+ s->nr_held = reqs ? reqs->nr : 0;
+ if (!s->nr_held) {
+ return;
+ }
+
+ s->held = g_new(VHostBlkHeldReq, s->nr_held);
+ for (i = 0; i < s->nr_held; i++) {
+ s->held[i].vq = reqs->reqs[i].vq;
+ s->held[i].head = reqs->reqs[i].head;
+ }
+}
+
+static void vhost_blk_resubmit_held(VHostBlk *s)
+{
+ g_autofree struct vhost_blk_held_reqs *reqs = NULL;
+ uint32_t i, nr, done = 0;
+
+ if (!s->nr_held) {
+ return;
+ }
+
+ reqs = g_malloc0(sizeof(*reqs) + s->nr_held * sizeof(reqs->reqs[0]));
+
+ while (done < s->nr_held) {
+ nr = s->nr_held - done;
+ reqs->nr = nr;
+ for (i = 0; i < nr; i++) {
+ reqs->reqs[i].vq = s->held[done + i].vq;
+ reqs->reqs[i].head = s->held[done + i].head;
+ }
+
+ if (!ioctl(s->vhostfd, VHOST_BLK_RESUBMIT, reqs)) {
+ break;
+ }
+ if (reqs->nr >= nr) {
+ error_report("vhost-blk: unable to resubmit held requests: %s",
+ strerror(errno));
+ break;
+ }
+
+ done += reqs->nr;
+ if (errno == ENOMEM) {
+ continue;
+ }
+ error_report("vhost-blk: unable to resubmit held request "
+ "vq %u head %u: %s", s->held[done].vq,
+ s->held[done].head, strerror(errno));
+ done++;
+ }
+
+ g_free(s->held);
+ s->held = NULL;
+ s->nr_held = 0;
+}
/*
* Spread the virtqueues round-robin over nworkers kernel threads. Thread 0 is
@@ -115,6 +211,19 @@ static int vhost_blk_start(VirtIODevice *vdev)
return -ENOSYS;
}
+ if (s->hold_supported) {
+ uint64_t policy = vhost_blk_error_policy(s);
+ int fd = event_notifier_get_fd(&s->error_notifier);
+
+ /* VHOST_RESET_OWNER drops both */
+ if (ioctl(s->vhostfd, VHOST_BLK_SET_ERROR_POLICY, &policy) ||
+ ioctl(s->vhostfd, VHOST_BLK_SET_ERROR_FD, &fd)) {
+ error_report("vhost-blk: unable to set error policy");
+ ret = -EINVAL;
+ goto err_reset_owner;
+ }
+ }
+
if (nworkers > 1) {
ret = vhost_blk_set_workers(hdev, nworkers);
if (ret < 0) {
@@ -170,6 +279,8 @@ static int vhost_blk_start(VirtIODevice *vdev)
event_notifier_set(virtio_queue_get_host_notifier(virtio_get_queue(vdev, i)));
}
+ vhost_blk_resubmit_held(s);
+
s->vhost_started = true;
return 0;
@@ -216,6 +327,8 @@ static void vhost_blk_stop(VirtIODevice *vdev)
ret = ioctl(s->vhostfd, VHOST_BLK_SET_BACKEND, &backend);
assert(ret == 0);
+ vhost_blk_save_held(s);
+
vhost_dev_stop(hdev, vdev, false);
ret = k->set_guest_notifiers(qbus->parent, hdev->nvqs, false);
@@ -239,6 +352,16 @@ static void vhost_blk_reset(VirtIODevice *vdev)
}
}
+static void vhost_blk_device_reset(VirtIODevice *vdev)
+{
+ VHostBlk *s = VHOST_BLK(vdev);
+
+ vhost_blk_reset(vdev);
+ g_free(s->held);
+ s->held = NULL;
+ s->nr_held = 0;
+}
+
static int vhost_blk_set_status(VirtIODevice *vdev, uint8_t status)
{
int ret;
@@ -274,6 +397,38 @@ static void vhost_blk_vm_state(void *opaque, bool running, RunState state)
}
}
+static void vhost_blk_error_event(EventNotifier *n)
+{
+ VHostBlk *s = container_of(n, VHostBlk, error_notifier);
+ DeviceState *dev = DEVICE(s);
+ DeviceState *proxy = qdev_get_parent_bus(dev)->parent;
+ g_autofree struct vhost_blk_held_reqs *reqs = NULL;
+ g_autofree char *path = NULL;
+ struct vhost_blk_held_req *req;
+
+ if (!event_notifier_test_and_clear(n) || !s->vhost_started ||
+ !runstate_is_running()) {
+ return;
+ }
+
+ reqs = vhost_blk_get_held(s);
+ if (!reqs || !reqs->nr) {
+ return;
+ }
+ req = &reqs->reqs[reqs->nr - 1];
+ path = object_get_canonical_path(OBJECT(dev));
+
+ qemu_system_vmstop_request_prepare();
+ qapi_event_send_block_io_error(path, proxy->id ?: "", NULL,
+ req->type == VIRTIO_BLK_T_IN ?
+ IO_OPERATION_TYPE_READ :
+ IO_OPERATION_TYPE_WRITE,
+ BLOCK_ERROR_ACTION_STOP, true,
+ req->error == ENOSPC, strerror(req->error),
+ 0, 0);
+ qemu_system_vmstop_request(RUN_STATE_IO_ERROR);
+}
+
static bool vhost_blk_update_size(VHostBlk *s, Error **errp)
{
BlockConf *conf = &s->conf.conf;
@@ -720,6 +875,7 @@ static void vhost_blk_device_realize(DeviceState *dev, Error **errp)
VHostBlk *s = VHOST_BLK(vdev);
VhostBlkConf *conf = &s->conf;
struct vhost_virtqueue *vqs;
+ uint64_t policy = 0;
int i, ret;
if (!conf->devpath) {
@@ -796,6 +952,13 @@ static void vhost_blk_device_realize(DeviceState *dev, Error **errp)
s->mighand = qemu_add_vm_change_state_handler(vhost_blk_vm_state, vdev);
+ ret = event_notifier_init(&s->error_notifier, 0);
+ if (ret < 0) {
+ error_setg_errno(errp, -ret,
+ "vhost-blk: unable to create the error eventfd");
+ goto cleanup;
+ }
+
ret = vhost_dev_init(&s->dev, (void *)((size_t)s->vhostfd),
VHOST_BACKEND_TYPE_KERNEL, 0, NULL);
if (ret < 0) {
@@ -815,6 +978,14 @@ static void vhost_blk_device_realize(DeviceState *dev, Error **errp)
goto cleanup;
}
+ event_notifier_set_handler(&s->error_notifier, vhost_blk_error_event);
+ s->hold_supported = !ioctl(s->vhostfd, VHOST_BLK_SET_ERROR_POLICY,
+ &policy);
+ if (!s->hold_supported && vhost_blk_error_policy(s)) {
+ warn_report("vhost-blk: the kernel cannot hold failed requests, "
+ "werror and rerror have no effect");
+ }
+
return;
cleanup:
@@ -831,6 +1002,7 @@ cleanup:
s->backend_fd = -1;
}
vhost_blk_uevent_detach(s);
+ event_notifier_cleanup(&s->error_notifier);
for (i = 0; i < conf->num_queues; i++) {
virtio_del_queue(vdev, i);
}
@@ -848,6 +1020,9 @@ static void vhost_blk_device_unrealize(DeviceState *dev)
qemu_del_vm_change_state_handler(s->mighand);
vhost_blk_stop(vdev);
vhost_dev_cleanup(&s->dev);
+ event_notifier_set_handler(&s->error_notifier, NULL);
+ event_notifier_cleanup(&s->error_notifier);
+ g_free(s->held);
if (s->backend_fd >= 0) {
qemu_close(s->backend_fd);
s->backend_fd = -1;
@@ -999,7 +1174,7 @@ static void vhost_blk_class_init(ObjectClass *klass, const void *data)
vdc->get_config = vhost_blk_update_config;
vdc->get_features = vhost_blk_get_features;
vdc->set_status = vhost_blk_set_status;
- vdc->reset = vhost_blk_reset;
+ vdc->reset = vhost_blk_device_reset;
vdc->get_vhost = vhost_blk_get_vhost;
}
diff --git a/include/hw/virtio/vhost-blk.h b/include/hw/virtio/vhost-blk.h
index 9110462109f..dd6dd53c1c7 100644
--- a/include/hw/virtio/vhost-blk.h
+++ b/include/hw/virtio/vhost-blk.h
@@ -35,6 +35,11 @@ typedef struct VhostBlkConf {
char *serial;
} VhostBlkConf;
+typedef struct VHostBlkHeldReq {
+ uint16_t vq;
+ uint16_t head;
+} VHostBlkHeldReq;
+
typedef struct VHostBlk {
VirtIODevice parent_obj;
VhostBlkConf conf;
@@ -52,6 +57,10 @@ typedef struct VHostBlk {
bool discard;
QEMUBH *resize_bh;
Error *cpr_blocker;
+ EventNotifier error_notifier;
+ bool hold_supported;
+ VHostBlkHeldReq *held;
+ int32_t nr_held;
} VHostBlk;
#endif
--
2.53.0
next prev parent reply other threads:[~2026-12-01 9:18 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 9:18 [PATCH hci-8.2 v4 0/5] vhost-blk: support the werror/rerror policies Alexander Ivanov
2026-12-01 9:18 ` [PATCH hci-8.2 v4 1/5] linux-headers: add the vhost-blk held request ioctls #VSTOR-152175 Alexander Ivanov
2026-12-01 9:21 ` no-reply
2026-12-01 9:18 ` [PATCH hci-8.2 v4 2/5] vhost-blk: add the werror and rerror properties #VSTOR-152175 Alexander Ivanov
2026-12-01 9:18 ` [PATCH hci-8.2 v4 3/5] vhost-blk: stop vhost on every VM stop #VSTOR-152175 Alexander Ivanov
2026-12-01 9:24 ` no-reply
2026-12-01 9:18 ` Alexander Ivanov [this message]
2026-12-01 9:25 ` [PATCH hci-8.2 v4 4/5] vhost-blk: pause the VM on a request failed with ENOSPC #VSTOR-152175 no-reply
2026-12-01 9:41 ` Alexander Ivanov
2026-12-01 9:18 ` [PATCH hci-8.2 v4 5/5] vhost-blk: migrate the held requests #VSTOR-152175 Alexander Ivanov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261201091818.1894308-5-alexander.ivanov@virtuozzo.com \
--to=alexander.ivanov@virtuozzo.com \
--cc=andrey.drobyshev@virtuozzo.com \
--cc=andrey.zhadchenko@virtuozzo.com \
--cc=svt-core@virtuozzo.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.