From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from AS8PR04CU009.outbound.protection.outlook.com (mail-westeuropeazon11021113.outbound.protection.outlook.com [52.101.70.113]) by lore.virtuozzo.com (Postfix) with ESMTPS id 9F0468015A for ; Tue, 8 Sep 2026 12:55:16 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=LCQSVStiaJsaMM6HrSlGqBrmkAgER/S0yCRh0V/jqcrjA7TkUudVw6vDK1A9KAUyvE4vQQ5vo0Q5gnSZZb8A5Vbyr4UZCT28peSUdlnZ+p758yn9RY0YnzItXcQAv34qsTSHEtod7lHoeYASe7wnyPfocSiOvFn/9Y/tZ8IJ3/nRZgj7YVQeAb/N9HEkIop24vdWZG8Z3WBPJDCeB0AN/2roACe4bX73be4AvUR1elbGoTMnRlynTMzlHuHjphFtHncwhwyR9IXMI0YXIHqLNbd8gyGuVVOYKSN9Q9k547WiEBdT9PzKDNX2cc4RgyhjsGvVB0ogeBQwvIVcqgC11Q== 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=2/aZ7CFT9+zFX/zyQAg64w6T3nX9zi29psb2JG81ddw=; b=R2s3NHnjrBPJow2llvDuBFQUaQ0uYxnF7Z5kR/nTTsONsim7+vxsBjrDM6ggJeM4uxYtRU9ZzIf6GfgmPY+lg3PQpEmsct+I9BNUBgaUz6FIH6AecxIbiARk8I5G5XvUgGGe7z3C3r+6fRWTN36/PoDL3m3UJBCFJvngdrGgyJoR53uYGG4hU6XkLEwBoACTSF/nnap6iJk+mbA5H92BdhHiFgNP75E6U7MQGRQgm5ayouvUaranX+fZr+Y8FVOQkuBWniXz3MTPX/N6DA4YhVrBHILRpE319L03E6i0Xgp1vD6Uv13gZiQto8QJnDsXy65k4yCjYlW2D9YfY1zLrg== 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=2/aZ7CFT9+zFX/zyQAg64w6T3nX9zi29psb2JG81ddw=; b=J9qK/mPLxwBeNWx4saHxJ4/cWvjwA/xoSHc6ZYtHbJ3PwK4n4iJmpY1ogm0EbMYCS4EGtfEm8qMrbk7/3DvxB1e0leK2Kbp+QxsF3jk+u7VFkA0XhZjHzeu4JUYSHld5mUuHMlwSRo70kGx0ZYBLFjO1NWShuL+Jusvr/P+CKYbk5gUqBWwX/UQ2jrXPW0grLqa/SBQRsutBDugs0aqV/8VXJY/Dvjyyho1nApxxIMjD6SXtqtCHxnW2o8DB7Pafe5+HOaIyT04rwOqVPAi98hIRJQJUUbysNIBIvlS2pGeyXirB6+j8n7atU0v+iB5+yC26nPO85H5A8bTDKfnWyA== Received: from CPAP307CA0002.DNKP307.PROD.OUTLOOK.COM (2603:10a6:380:3::6) by AS2PR08MB10324.eurprd08.prod.outlook.com (2603:10a6:20b:5e7::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.406.7; Tue, 8 Sep 2026 12:55:10 +0000 Received: from CPH1EPF00000521.eurprd05.prod.outlook.com (2603:10a6:380:3:cafe::4b) by CPAP307CA0002.outlook.office365.com (2603:10a6:380:3::6) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.382.11 via Frontend Transport; Tue, 8 Sep 2026 12:55:10 +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 CPH1EPF00000521.mail.protection.outlook.com (10.167.241.132) 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 12:55:10 +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=2/aZ7CFT9+zFX/zyQAg64w6T3nX9zi29psb2JG81ddw=; b=m+BFBpxIdeCr DV7q1l+x1ByQwV0JASrzhybeP14hztm/cLVTAOiuUPDFTXTyh/DZwoxtYDVYOarvWuYYM4M1qjq2e jtBpyvBiLj/SidWHDVuz04+n7Sm6mLoUKh8JlGjLS52fVNiIGXjRT3DIdv1hIHSBP9ukifG+6Ym8e 2madv5THuacIxnFWXwzMxqyp33OXPiY9zFqC79XstwAFouA1e62g0lb/5SelrF1tQGvUi9qNczsy+ GJQFXKKKeq3xAFFnyQLc824y33VGFNmkFXdLlRRHNX5Zc3/DO4ADXZmU1lNGuDqshjTrt+WCb8fsi 4oS2K6mnJ0ViEd+KRnl5aw==; 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 1x3vJP-009opv-1Q; Tue, 08 Sep 2026 14:54:59 +0200 From: Andrey Zhadchenko 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 Message-ID: <20260908125208.278402-5-andrey.zhadchenko@virtuozzo.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20260908125208.278402-1-andrey.zhadchenko@virtuozzo.com> References: <20260908125208.278402-1-andrey.zhadchenko@virtuozzo.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CPH1EPF00000521:EE_|AS2PR08MB10324:EE_ X-MS-Office365-Filtering-Correlation-Id: 9a8c6095-ab87-44f1-a952-08df0da86a76 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|36860700016|376014|1800799024|82310400026|23010399003|13003099007|5113699003|3023799007|10067099003|57112099003|18002099003|22082099003; X-Microsoft-Antispam-Message-Info: /i7B0aFpYQsZsxxmLQORGyg7cXhSZWOuURHfl2hUnn8qs7sIsa6/FJkv6EzLqmX28GaNtuRUrDJbdutCvh673m24mzKbZC2HGRfmn8UVmf8c7Vgdmu+dyPFIyIOZigeNhNL/euoZ1K5QMjNXthQ3I8SJDokUdTeQlkGSUbuJ3jwmZZhcX+I5JkPqbNzug0Ehcpg6MJRmQYqxjYmyI31cZIZslSVZehQr33ZN7pX1B7uCJP+S4EHLceKg+URwGCjd6HI5LgWcoZD3jdI5AeJyfwEMA3cfoxA6WYtBD8dzK7mBzTXw4Kj2C5O2JotzTI1AMKwy9GmFTTwp0IkJhPNBMN5inKlp8W9OGyIOan+6d4WnSSPc0Qi5xnx0Ql4APD+QR+lWWLmkDJtGD6HtLSoy6EsxZdENYltd7fPFSsSMLoV6GUQLO3eYfCcoaAOahcF/nIgwJNAii1ur7Ecr139NRrkKjH/9610ebbi4/+6QQnJ253/JiEPkwB3sUh/5LXGtZ3STHUoqEfUO9ZAzGaINyOAxlntBdfvO9nvvLql3dAhJ+k+iLiFpWLQkl0dNHjQmK7w4xjfK48mzE9ljN/tjrxFkWAg9TNWgv9kCEss1nD0yOBfr9ebmaVrCmtTltuHRfgXXHEd5q+YY2KCX8t7RdmvX+bK+3UyaGKf0W900XE5rVpIgMfTcmo7soKsZ3I1GCLs1LiQ5AvoI6R3YczOi6SBj9ugsUQz0p/c+uxuXcQPcdlbLCIpIatapc6Zej/hi8n5vHgj1apmOKIpdtCJjqwG5fJ5rB6aYGTpbucxrgWaoetMsxLEMhmzq6m+ihdI9DsNkNvxU28vAVlH/UM+0Pn011ovglWiXFgw8Isn1pitPen2sRR2s0F1OwkBMDwV9p02/wgd1g60gt53c73Rsr7tIdWbcDs6T4i5ROyQ293A= 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)(36860700016)(376014)(1800799024)(82310400026)(23010399003)(13003099007)(5113699003)(3023799007)(10067099003)(57112099003)(18002099003)(22082099003);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Qzi5iQMbVury0GWxepSazU10KWHSFSsKCVuq6t6gzUhCC78k5jcR5FKCQrzw9GiPt0cx94WzQGufF4OCi48q4T9HabslCcSML+1PlAqG64CZn+TPIdgjfqUEooT9iTi6YpG8o2vpsr1QAYozN7N5QW0KxJPILIX8+xJv3CiqpqoB36EUb3uyJgsIl1D24KsCpv2t5iiLTQuIJydur99R14H0nqLcTvBAanmpNpTDAlRs8rl3SHnBKv2dhruqaX6180ggNUS4r6eMniNYloTHGWFqsRCO6n5IJfS+RlFrcBo+qcH2wB2jH/XuTPJumXUHB82jLB7ThhXgao3av0uSs1GUgJe+2HUR9ufHF4N6wo0TaQ0RNKYx2rBqPg8ikvU5gTwJTGpSSAnf47O8Lo5sRGl33D/HUcRpyaYQ8AGsrWTso+G3t4ScnVP2ZogDwE8B X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Sep 2026 12:55:10.0563 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 9a8c6095-ab87-44f1-a952-08df0da86a76 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: CPH1EPF00000521.eurprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS2PR08MB10324 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 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 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 #include +#include #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