From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from AM0PR83CU005.outbound.protection.outlook.com (mail-westeuropeazon11020127.outbound.protection.outlook.com [52.101.69.127]) by lore.virtuozzo.com (Postfix) with ESMTPS id 3F19E8015D for ; Tue, 8 Sep 2026 01:07:55 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=OhbQKDBROPsbw7DvZqaipGsU80Jwg9DlvlTZZVp0aJoulV6ER4VqcfYZgFI5xD3w5g00mjg5tUkINrXtFRO0bitw37Q3VobbGfOtS2aMz2bjWzhpNLlXD781Hw2STXriHI0bphLQF8o8Vske/M1NkSSpcM1T7F/0pQ279k3q9FFZiJewbuNs4FBTybpzS7zZrpzxnpdHCGvs6yvfHKyeJFJcpueKD2FAeoetAi59XSM1BMUoJRDKnPQyvLqEkPvQ6BIynWccz4xD2z+vg0jd4LyZd4pm7rM8WFDB+A3Q3wIMTiYi2lCrRpbQV4n3ZIawifeYKMc2pMNn5RQwPhquXg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=GHKCkZzndcawPQVaiOXfniTtPS8XrmsGC896vRaJe4U=; b=jDSGsr68CqCliXeZ/FTshyVgaAc6aftbNawS7rZwCjPTK6+dyjTUOAbRGtUoByLwzg7pRF2tLNjCTRX/A6K5SXXG50H10MUCB7/+oSgv5CDVZgjyPOuy191+camxuiyP6rokfjK6aQlyG3iDjjFbE8pbt+SdvhmTkts0y4S42/lqKFreylUvPUe37ef8z9bfYLlgtOmuYkndrWm4mDlIWPj2aEIHI02OsxvszpRE2f5ZWrkd7iOXX3v7mxQsP5yeiGVva0qxl/p9eMvv09qnZYjhIOPbH0G9u1BmAG9xb4aMPlAgSvUg+yn+e+StYTfF6watyowcds5ANLG7ThoiMA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 130.117.225.111) smtp.rcpttodomain=virtuozzo.com smtp.mailfrom=virtuozzo.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=virtuozzo.com; dkim=pass (signature was verified) header.d=virtuozzo.com; arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=virtuozzo.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=GHKCkZzndcawPQVaiOXfniTtPS8XrmsGC896vRaJe4U=; b=KT+tVOzEr6AT4Jj0C9e/abciZ5Lpl/LaZc5T3LLZZcOaBQno6DVI9tgf8m4NuKqPocB90dNx5fVY9ZkVBSEr1+eR5QZ0c2SWuJkQnK+aOHti2vLRSGMwbI/pyK3LNTC3GTkI+RbBDJ/YeBWjNRO9mkYCJIWwNhO4ae6R8AO/N4f/5bv95EmqLDVtISIF0Ucy56/AxFf0Cx/eLd4pUD7eeYG+u6rUGNK0Qb06W91OPliAu7ZXMAzvg+bzu4ONQhZx+RBdrmeI+4YqojXBGLfsEa9MoKjXhpomGhffAzCdaf33pURh19GBr8i8dvX73bpAf+jOklGSBu9A54RFlZn3Qg== Received: from DB8PR06CA0044.eurprd06.prod.outlook.com (2603:10a6:10:120::18) by DU4PR08MB11175.eurprd08.prod.outlook.com (2603:10a6:10:574::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.406.6; Tue, 8 Sep 2026 01:07:50 +0000 Received: from DU2PEPF00028D12.eurprd03.prod.outlook.com (2603:10a6:10:120:cafe::3f) by DB8PR06CA0044.outlook.office365.com (2603:10a6:10:120::18) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.382.15 via Frontend Transport; Tue, 8 Sep 2026 01:07:50 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 130.117.225.111) smtp.mailfrom=virtuozzo.com; dkim=pass (signature was verified) header.d=virtuozzo.com;dmarc=pass action=none header.from=virtuozzo.com; Received-SPF: Pass (protection.outlook.com: domain of virtuozzo.com designates 130.117.225.111 as permitted sender) receiver=protection.outlook.com; client-ip=130.117.225.111; helo=relay.virtuozzo.com; pr=C Received: from relay.virtuozzo.com (130.117.225.111) by DU2PEPF00028D12.mail.protection.outlook.com (10.167.242.26) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.406.5 via Frontend Transport; Tue, 8 Sep 2026 01:07:49 +0000 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=GHKCkZzndcawPQVaiOXfniTtPS8XrmsGC896vRaJe4U=; b=Bv35vHkL5zOP kVXy0dEtZsmw7WA9z+31CDlKopmF72J4IWYxORzymWz8GwKy9e5ebvioqBlsrZ3s753eixqGMDCdz yCqyDU2XRcw5KDmwWkTLvkVIqV3AN35JL8ec9MOOzUOtXcJjRNvG3/zqPvDAFNc9H36geUpSK6/aF busfao4ifJX+/MlQlyQo1VPM4dTIvlmCl0p4++kRNOIpkKkitWUrCUF3LNSIyXuPRN5EiJaE75Ewo v0p2MNJOJofrdijcZgyV6MCEuOtO/Q1dxjK3UommXMLRLQjRs6YsfqTL83Hh+I/J7nE8zWnmTTbC3 a9mrOI06oqIu/ahxlLVMPw==; Received: from [130.117.225.5] (helo=vz9-demens-1.aci.vzint.dev) by relay.virtuozzo.com with esmtp (Exim 4.96) (envelope-from ) id 1x3kGt-00DU8a-0p; Tue, 08 Sep 2026 03:07:39 +0200 From: Andrey Zhadchenko To: svt-core@virtuozzo.com Cc: den@openvz.org, andrey.drobyshev@virtuozzo.com Subject: [QEMU HCI-8.0 PATCH v3 4/5] vhost-blk: watch the device for resize events Date: Tue, 8 Sep 2026 04:04:54 +0300 Message-ID: <20260908010455.249679-5-andrey.zhadchenko@virtuozzo.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20260908010455.249679-1-andrey.zhadchenko@virtuozzo.com> References: <20260908010455.249679-1-andrey.zhadchenko@virtuozzo.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DU2PEPF00028D12:EE_|DU4PR08MB11175:EE_ X-MS-Office365-Filtering-Correlation-Id: e27da2d8-e7c6-4e0a-55eb-08df0d4599b5 List-Id: svt-core@virtuozzo.com X-LD-Processed: 0bc7f26d-0264-416e-a6fc-8352af79c58f,ExtAddr X-MS-Exchange-Transport-Rules-Loop: 1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|36860700016|1800799024|23010399003|82310400026|5113699003|10067099003|3023799007|57112099003|18002099003|22082099003|13003099007; X-Microsoft-Antispam-Message-Info: vEZYOGUeswWTry8toWbZ7W4XG890Hqda5DFntartrfgmaGrNAhQGkRveT2Z++xpnWrDRk/aJEW/wf78OT6n/y8k2Sqysxk+4RsN+KTVSzWqQoOJqbvhSo1lsxfO/IdCKT6nY5qSYcHPWy72e+eOTqr8qW6qMPydyIElUDWxQDE8CSYRvU8LzYQfJl96J87hl9vIKiO02DsFdidYWak1Gf4DW39vSvpgeM24bbv02KFKhkBWptd5OpmblJW3h/4EOFHVWsxUMH5MiBtiOWDcOFDYor555yyS0KmIK/Se6Dpfx6pv74TMDymS8/nAMLByYDEXn8FE/5BsUJK6ZA4dwQR9/1tYiDCvjXkHDbL2RAhlYx8socw5cgosW+VOFNZXYeCytH9F4+cpfQvhDGtsVfoTXOkwxLfJAe+WeqNLuRtevI36+SypJrfDPpULVWDa/BPrcvm1p8qtGnm2QRk8ANXgWF6mgxbtEyO5+oxHAIGZmWjobzvNKnJlbWUNOrWdPLQ6TQjkMxwzbPnKy8JBeOn86ntPrVRJkDdSq0Lw0lcnFaTqA05PDBLadd0VxQwW/jh/ySMxIKjNIHCSnv5//ZoB6SxAby2Oc+pyRH4413Vukf4zkxoQGJ7dCmm6ppz7kW5ZnzwfVbkHqqyxoIzStdxWbc38sYYz5DoeGa4MU6zNzYmByTFvOIAXlxzbaS046wO+X0akNSUZ18WudVAUvxxaif2X/OJCBZhNkkRwn84jhbU09ANBxYqm1HX9HLTdtrXByyweXerpakX9L+okKCTVR3/15u5gN4oupakSJpTKBZ+S3vBtqs8yQ3S3YhCuVctyD9Bz0f45MWq8vW4tG09bD6mDzuV+mYjIdyvyF90P1pw8wGZbTAtGfOeh8TeKnw9FnFMr0jvKt50ZbdQTJOW8cVD4I73lSKAvE6SMzYiA= X-Forefront-Antispam-Report: CIP:130.117.225.111;CTRY:CH;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:relay.virtuozzo.com;PTR:relay.virtuozzo.com;CAT:NONE;SFS:(13230040)(376014)(36860700016)(1800799024)(23010399003)(82310400026)(5113699003)(10067099003)(3023799007)(57112099003)(18002099003)(22082099003)(13003099007);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: f7/lea40Kr8aCXnzsidh5SReQDkHs6MihfZ8L9HDAUSTQECQ4AqpJK4afY+OEoMLS+caVjyOpimlpMWp8OGvfez8oiCasOZ0I5LGK0dPFhpPgvkzsdozAdIwPkj+exsJlVEoAfW23Ea5GZ2HcCDzcH2omnkq+ANLyhEYbVwRQJogBuEfoiim2Ry54//JnpfZP7zo+fIIpBKaLliIKg20Yscsqp00xbUEicOtyOLTqTd3syo4+EfKErCC7i2YxNmhfcv0J2JjshjOIupImsgjoNyCYYl8Ydsi8EKKRkiPyktL9UYCbEU4+0RGtKIQXtTkuPHU0vPhAs6cB5itzuUqyTB6YboK5ZuSBZPfzjI12pC5Naok9k6SxH/CgTkV7/Rl1SdTs0eKGR6J8D5kpFGWqFA98t9RdNyL2rzLq1n4ROIeUkDM8z/anGf5VS25Q02d X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Sep 2026 01:07:49.2378 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e27da2d8-e7c6-4e0a-55eb-08df0d4599b5 X-MS-Exchange-CrossTenant-Id: 0bc7f26d-0264-416e-a6fc-8352af79c58f X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=0bc7f26d-0264-416e-a6fc-8352af79c58f;Ip=[130.117.225.111];Helo=[relay.virtuozzo.com] X-MS-Exchange-CrossTenant-AuthSource: DU2PEPF00028D12.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DU4PR08MB11175 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 Signed-off-by: Andrey Zhadchenko --- 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 hw/block/vhost-blk.c | 200 ++++++++++++++++++++++++++++++++++ include/hw/virtio/vhost-blk.h | 4 + 2 files changed, 204 insertions(+) diff --git a/hw/block/vhost-blk.c b/hw/block/vhost-blk.c index 797743b2c1..a69bdcb551 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 #include +#include #include "system/runstate.h" static int vhost_blk_start(VirtIODevice *vdev) @@ -227,6 +231,191 @@ 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) +{ + if (!s->conf.ueventfd) { + return true; + } + + s->uevent_fd = monitor_fd_param(monitor_cur(), s->conf.ueventfd, errp); + if (s->uevent_fd < 0) { + error_prepend(errp, "vhost-blk: unable to obtain 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 bool vhost_blk_open_backend(VHostBlk *s, Error **errp) { ERRP_GUARD(); @@ -251,6 +440,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; @@ -326,6 +516,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); @@ -376,6 +572,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); } @@ -388,6 +585,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); @@ -478,6 +676,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), @@ -525,6 +724,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