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 v2 4/5] vhost-blk: watch the device for resize events
Date: Fri, 4 Sep 2026 16:21:54 +0300 [thread overview]
Message-ID: <20260904132155.180581-5-andrey.zhadchenko@virtuozzo.com> (raw)
In-Reply-To: <20260904132155.180581-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 (e.g. a
/dev/fdset/N path), 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 and do some message
filtering.
When the property is not set, capacity changes are not detected.
https://virtuozzo.atlassian.net/browse/VSTOR-143437
Signed-off-by: Andrey Zhadchenko <andrey.zhadchenko@virtuozzo.com>
---
v2:
- rework patch a bit: now we expect opened and filtered fd from
libvirt.
hw/block/vhost-blk.c | 181 ++++++++++++++++++++++++++++++++++
include/hw/virtio/vhost-blk.h | 4 +
2 files changed, 185 insertions(+)
diff --git a/hw/block/vhost-blk.c b/hw/block/vhost-blk.c
index 04a7013e52..7cb842a859 100644
--- a/hw/block/vhost-blk.c
+++ b/hw/block/vhost-blk.c
@@ -10,7 +10,9 @@
#include "qemu/osdep.h"
#include "qapi/error.h"
+#include "qemu/cutils.h"
#include "qemu/error-report.h"
+#include "qemu/main-loop.h"
#include "qom/object.h"
#include "hw/qdev-core.h"
#include "hw/boards.h"
@@ -25,6 +27,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)
@@ -228,6 +231,173 @@ static int vhost_blk_update_size(VHostBlk *s, bool *changed, Error **errp)
return 0;
}
+static void vhost_blk_resize_bh(void *opaque)
+{
+ VHostBlk *s = opaque;
+ Error *local_err = NULL;
+ bool changed;
+
+ if (vhost_blk_update_size(s, &changed, &local_err) < 0) {
+ error_report_err(local_err);
+ return;
+ }
+
+ if (changed) {
+ virtio_notify_config(VIRTIO_DEVICE(s));
+ }
+}
+
+/*
+ * 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[64 * 1024 + 1];
+
+ 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: unable to receive uevent: %s",
+ strerror(errno));
+ }
+ 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)
+{
+ 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;
+ }
+
+ return true;
+}
+
+static bool vhost_blk_uevent_attach(VHostBlk *s, Error **errp)
+{
+ if (!s->conf.ueventfd) {
+ return true;
+ }
+
+ s->uevent_fd = qemu_open(s->conf.ueventfd, O_RDWR, errp);
+ if (s->uevent_fd < 0) {
+ error_prepend(errp, "vhost-blk: unable to open uevent socket: ");
+ return false;
+ }
+
+ if (!vhost_blk_uevent_check(s->uevent_fd, s->conf.ueventfd, errp)) {
+ qemu_close(s->uevent_fd);
+ s->uevent_fd = -1;
+ return false;
+ }
+
+ 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 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;
+}
+
static bool vhost_blk_open_backend(VHostBlk *s, Error **errp)
{
BlockConf *conf = &s->conf.conf;
@@ -252,6 +422,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;
@@ -325,6 +496,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;
s->dev.vqs = g_new0(struct vhost_virtqueue, s->dev.nvqs);
@@ -374,6 +551,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);
}
@@ -386,6 +564,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);
@@ -476,6 +655,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),
@@ -523,6 +703,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-04 13:25 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-04 13:21 [QEMU HCI-8.0 PATCH v2 0/5] vhost-blk change backend setup Andrey Zhadchenko
2026-09-04 13:21 ` [QEMU HCI-8.0 PATCH v2 1/5] vhost-blk: do not double close vhostfd Andrey Zhadchenko
2026-09-04 15:33 ` Andrey Drobyshev
2026-09-04 13:21 ` [QEMU HCI-8.0 PATCH v2 2/5] vhost-blk: change backend setup Andrey Zhadchenko
2026-09-04 15:33 ` Andrey Drobyshev
2026-09-04 13:21 ` [QEMU HCI-8.0 PATCH v2 3/5] vhost-blk: add read-only flag Andrey Zhadchenko
2026-09-04 15:33 ` Andrey Drobyshev
2026-09-04 13:21 ` Andrey Zhadchenko [this message]
2026-09-04 15:33 ` [QEMU HCI-8.0 PATCH v2 4/5] vhost-blk: watch the device for resize events Andrey Drobyshev
2026-09-04 13:21 ` [QEMU HCI-8.0 PATCH v2 5/5] vhost-blk: preserve the uevent socket across cpr-exec Andrey Zhadchenko
2026-09-04 15:33 ` 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=20260904132155.180581-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.