From: Andrey Zhadchenko <andrey.zhadchenko@virtuozzo.com>
To: svt-core@virtuozzo.com
Cc: den@openvz.org, andrey.drobyshev@virtuozzo.com
Subject: [QEMU HCI-8.0 PATCH v4 4/5] vhost-blk: watch the device for resize events
Date: Tue, 8 Sep 2026 15:52:07 +0300 [thread overview]
Message-ID: <20260908125208.278402-5-andrey.zhadchenko@virtuozzo.com> (raw)
In-Reply-To: <20260908125208.278402-1-andrey.zhadchenko@virtuozzo.com>
Resize was tied to block node, which we removed a few patches ago.
Luckily we can make resize automated: receive an uevent socket
from the management layer via the new "ueventfd" property (a
monitor fd name passed with the 'getfd' QMP command), watch it for
relevant netlink messages and call virtio_notify_config() if we
detect a capacity change.
The socket is set up (possibly with filter) by management layer.
We only need to check that it is netlink, subscribed to kernel
uevents, and do some message filtering.
When the property is not set, capacity changes are not detected.
https://virtuozzo.atlassian.net/browse/VSTOR-143437
Reviewed-by: Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>
Signed-off-by: Andrey Zhadchenko <andrey.zhadchenko@virtuozzo.com>
---
v2:
- rework patch a bit: now we expect opened and filtered fd from
libvirt.
v3:
- accept a monitor fd name passed with getfd instead of a
/dev/fdset path
- verify that the socket is subscribed to kernel uevents
- detach the socket after a permanent receive error
- change buffer capacity to 4KiB
v4:
- keep the uevent socket local until it has been validated
hw/block/vhost-blk.c | 202 ++++++++++++++++++++++++++++++++++
include/hw/virtio/vhost-blk.h | 4 +
2 files changed, 206 insertions(+)
diff --git a/hw/block/vhost-blk.c b/hw/block/vhost-blk.c
index dc21f9fbbf..ef46c55665 100644
--- a/hw/block/vhost-blk.c
+++ b/hw/block/vhost-blk.c
@@ -10,7 +10,10 @@
#include "qemu/osdep.h"
#include "qapi/error.h"
+#include "qemu/cutils.h"
#include "qemu/error-report.h"
+#include "qemu/main-loop.h"
+#include "monitor/monitor.h"
#include "qom/object.h"
#include "hw/qdev-core.h"
#include "hw/boards.h"
@@ -25,6 +28,7 @@
#include "linux-headers/linux/vhost.h"
#include <sys/ioctl.h>
#include <linux/fs.h>
+#include <linux/netlink.h>
#include "system/runstate.h"
static int vhost_blk_start(VirtIODevice *vdev)
@@ -229,6 +233,193 @@ static bool vhost_blk_update_size(VHostBlk *s, Error **errp)
return changed;
}
+static void vhost_blk_resize_bh(void *opaque)
+{
+ VHostBlk *s = opaque;
+ Error *local_err = NULL;
+ bool changed;
+
+ changed = vhost_blk_update_size(s, &local_err);
+ if (local_err) {
+ error_report_err(local_err);
+ return;
+ }
+
+ if (changed) {
+ virtio_notify_config(VIRTIO_DEVICE(s));
+ }
+}
+
+static void vhost_blk_uevent_detach(VHostBlk *s)
+{
+ if (s->uevent_fd < 0) {
+ return;
+ }
+
+ qemu_set_fd_handler(s->uevent_fd, NULL, NULL, NULL);
+ qemu_bh_delete(s->resize_bh);
+ s->resize_bh = NULL;
+ qemu_close(s->uevent_fd);
+ s->uevent_fd = -1;
+}
+
+/*
+ * The uevent socket is created, bound and filtered by the management
+ * layer and passed to us via the "ueventfd" property.
+ */
+static void vhost_blk_uevent_read(void *opaque)
+{
+ VHostBlk *s = opaque;
+ char buffer[4096];
+
+ for (;;) {
+ struct sockaddr_nl source;
+ socklen_t source_len = sizeof(source);
+ uint64_t event_major = UINT64_MAX;
+ uint64_t event_minor = UINT64_MAX;
+ bool action_change = false;
+ bool subsystem_block = false;
+ bool resize = false;
+ char *field;
+ char *end;
+ ssize_t len;
+
+ memset(&source, 0, sizeof(source));
+ len = recvfrom(s->uevent_fd, buffer, sizeof(buffer) - 1,
+ MSG_DONTWAIT, (struct sockaddr *)&source, &source_len);
+ if (len < 0) {
+ if (errno == EINTR) {
+ continue;
+ }
+ if (errno == ENOBUFS) {
+ /* Some events may be dropped, just re-check */
+ qemu_bh_schedule(s->resize_bh);
+ continue;
+ }
+ if (errno != EAGAIN && errno != EWOULDBLOCK) {
+ error_report("vhost-blk: detaching uevent after the error: %s",
+ strerror(errno));
+ vhost_blk_uevent_detach(s);
+ }
+ return;
+ }
+
+ if (source.nl_family != AF_NETLINK || source.nl_pid != 0) {
+ continue;
+ }
+
+ buffer[len] = '\0';
+ field = buffer;
+ end = buffer + len;
+ while (field < end) {
+ size_t field_len = strnlen(field, end - field);
+
+ if (!strcmp(field, "ACTION=change")) {
+ action_change = true;
+ } else if (!strcmp(field, "SUBSYSTEM=block")) {
+ subsystem_block = true;
+ } else if (!strcmp(field, "RESIZE=1")) {
+ resize = true;
+ } else if (g_str_has_prefix(field, "MAJOR=")) {
+ uint64_t value;
+
+ if (!qemu_strtou64(field + strlen("MAJOR="), NULL, 10,
+ &value)) {
+ event_major = value;
+ }
+ } else if (g_str_has_prefix(field, "MINOR=")) {
+ uint64_t value;
+
+ if (!qemu_strtou64(field + strlen("MINOR="), NULL, 10,
+ &value)) {
+ event_minor = value;
+ }
+ }
+
+ if (field_len == end - field) {
+ break;
+ }
+ field += field_len + 1;
+ }
+
+ if (action_change && subsystem_block && resize &&
+ event_major == major(s->backend_rdev) &&
+ event_minor == minor(s->backend_rdev)) {
+ qemu_bh_schedule(s->resize_bh);
+ }
+ }
+}
+
+static bool vhost_blk_uevent_check(int fd, const char *src, Error **errp)
+{
+ struct sockaddr_nl addr;
+ socklen_t addrlen;
+ socklen_t optlen;
+ int domain;
+ int protocol;
+
+ optlen = sizeof(domain);
+ if (getsockopt(fd, SOL_SOCKET, SO_DOMAIN, &domain, &optlen) < 0) {
+ error_setg_errno(errp, errno, "vhost-blk: '%s' is not a socket", src);
+ return false;
+ }
+
+ optlen = sizeof(protocol);
+ if (getsockopt(fd, SOL_SOCKET, SO_PROTOCOL, &protocol, &optlen) < 0) {
+ error_setg_errno(errp, errno,
+ "vhost-blk: unable to get protocol of '%s'", src);
+ return false;
+ }
+
+ if (domain != AF_NETLINK || protocol != NETLINK_KOBJECT_UEVENT) {
+ error_setg(errp,
+ "vhost-blk: '%s' is not a NETLINK_KOBJECT_UEVENT socket",
+ src);
+ return false;
+ }
+
+ addrlen = sizeof(addr);
+ if (getsockname(fd, (struct sockaddr *)&addr, &addrlen) < 0) {
+ error_setg_errno(errp, errno,
+ "vhost-blk: unable to get address of '%s'", src);
+ return false;
+ }
+
+ if (addr.nl_groups != 1) {
+ error_setg(errp,
+ "vhost-blk: '%s' is not subscribed to kernel uevents",
+ src);
+ return false;
+ }
+
+ return true;
+}
+
+static bool vhost_blk_uevent_attach(VHostBlk *s, Error **errp)
+{
+ int fd;
+
+ if (!s->conf.ueventfd) {
+ return true;
+ }
+
+ fd = monitor_fd_param(monitor_cur(), s->conf.ueventfd, errp);
+ if (fd < 0) {
+ error_prepend(errp, "vhost-blk: unable to obtain uevent socket: ");
+ return false;
+ }
+
+ if (!vhost_blk_uevent_check(fd, s->conf.ueventfd, errp)) {
+ qemu_close(fd);
+ return false;
+ }
+
+ s->uevent_fd = fd;
+ s->resize_bh = qemu_bh_new(vhost_blk_resize_bh, s);
+ qemu_set_fd_handler(s->uevent_fd, vhost_blk_uevent_read, NULL, s);
+ return true;
+}
+
static bool vhost_blk_open_backend(VHostBlk *s, Error **errp)
{
ERRP_GUARD();
@@ -253,6 +444,7 @@ static bool vhost_blk_open_backend(VHostBlk *s, Error **errp)
s->conf.devpath);
goto fail;
}
+ s->backend_rdev = st.st_rdev;
if (!s->conf.readonly) {
int readonly;
@@ -328,6 +520,12 @@ static void vhost_blk_device_realize(DeviceState *dev, Error **errp)
return;
}
+ if (!vhost_blk_uevent_attach(s, errp)) {
+ qemu_close(s->backend_fd);
+ s->backend_fd = -1;
+ return;
+ }
+
s->dev.nvqs = conf->num_queues;
s->dev.max_queues = conf->num_queues;
vqs = g_new0(struct vhost_virtqueue, s->dev.nvqs);
@@ -378,6 +576,7 @@ cleanup:
qemu_close(s->backend_fd);
s->backend_fd = -1;
}
+ vhost_blk_uevent_detach(s);
for (i = 0; i < conf->num_queues; i++) {
virtio_del_queue(vdev, i);
}
@@ -390,6 +589,7 @@ static void vhost_blk_device_unrealize(DeviceState *dev)
VirtIODevice *vdev = VIRTIO_DEVICE(dev);
VHostBlk *s = VHOST_BLK(dev);
+ vhost_blk_uevent_detach(s);
qemu_del_vm_change_state_handler(s->mighand);
vhost_blk_set_status(vdev, 0);
vhost_dev_cleanup(&s->dev);
@@ -480,6 +680,7 @@ static void vhost_blk_update_config(VirtIODevice *vdev, uint8_t *config)
static const Property vhost_blk_properties[] = {
DEFINE_PROP_STRING("devpath", VHostBlk, conf.devpath),
+ DEFINE_PROP_STRING("ueventfd", VHostBlk, conf.ueventfd),
DEFINE_PROP_BOOL("read-only", VHostBlk, conf.readonly, false),
DEFINE_PROP_UINT16("num-queues", VHostBlk, conf.num_queues,
VHOST_BLK_AUTO_NUM_QUEUES),
@@ -527,6 +728,7 @@ static void vhost_blk_instance_init(Object *obj)
s->vhostfd = -1;
s->backend_fd = -1;
+ s->uevent_fd = -1;
device_add_bootindex_property(obj, &s->conf.conf.bootindex,
"bootindex", "/disk@0,0",
DEVICE(obj));
diff --git a/include/hw/virtio/vhost-blk.h b/include/hw/virtio/vhost-blk.h
index c6646f5845..e3ad92bc6e 100644
--- a/include/hw/virtio/vhost-blk.h
+++ b/include/hw/virtio/vhost-blk.h
@@ -25,6 +25,7 @@
typedef struct VhostBlkConf {
BlockConf conf;
char *devpath;
+ char *ueventfd;
bool readonly;
uint16_t num_queues;
uint16_t queue_size;
@@ -40,9 +41,12 @@ typedef struct VHostBlk {
uint64_t decided_features;
int vhostfd;
int backend_fd;
+ int uevent_fd;
struct vhost_dev dev;
bool vhost_started;
uint64_t length;
+ uint64_t backend_rdev;
+ QEMUBH *resize_bh;
} VHostBlk;
#endif
--
2.43.5
next prev parent reply other threads:[~2026-09-08 12:55 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-08 12:52 [QEMU HCI-8.0 PATCH v4 0/5] vhost-blk change backend setup Andrey Zhadchenko
2026-09-08 12:52 ` [QEMU HCI-8.0 PATCH v4 1/5] vhost-blk: improve vhost_blk_device_realize cleanup Andrey Zhadchenko
2026-09-08 12:52 ` [QEMU HCI-8.0 PATCH v4 2/5] vhost-blk: change backend setup Andrey Zhadchenko
2026-09-08 13:34 ` Andrey Drobyshev
2026-09-08 12:52 ` [QEMU HCI-8.0 PATCH v4 3/5] vhost-blk: add read-only flag Andrey Zhadchenko
2026-09-08 12:52 ` Andrey Zhadchenko [this message]
2026-09-08 12:52 ` [QEMU HCI-8.0 PATCH v4 5/5] vhost-blk: preserve the uevent socket across cpr-exec Andrey Zhadchenko
2026-09-08 13:34 ` Andrey Drobyshev
2026-09-08 13:34 ` [QEMU HCI-8.0 PATCH v4 0/5] vhost-blk change backend setup Andrey Drobyshev
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=20260908125208.278402-5-andrey.zhadchenko@virtuozzo.com \
--to=andrey.zhadchenko@virtuozzo.com \
--cc=andrey.drobyshev@virtuozzo.com \
--cc=den@openvz.org \
--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.