From nobody Sat Nov 15 05:33:49 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1755511511; cv=none; d=zohomail.com; s=zohoarc; b=FIcg5hNgZ7+MmEBywvAfbpuTUOJF4ZtXiaBIjsfks1eETAp+YpFxNs1KQw/VfpbDfZ/R30h0a/imL2d36gWzGG9SFhlow13Zfchu6Li/RR1bWsXIvjUf1uaYPvPXsqXrQsdbKuOzsSyyNIzrWkRqlakaUfLrdIh+ENA2keq0TvM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755511511; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=HgbSuxjhhsCf1K+75YmZPJ6FQ8pRFd+H6aW/YuZ18g8=; b=YVM2pTzpF4nhKjRSqmQfu+qVAtWBnC0Wd6zDvY6zw65Y1B1Eepn1veHY8by/r4BZQOG9xK/0CTSr3ofW5wUlCyaG41XIXl5Arf2pGSsKWwzuouPjbyLcEfCo6GUcG/SnIEUy8QVOqQMlbcu3r1eJMwUjZw4YhbWbBv0TSFgItkA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1755511511885373.59880618690727; Mon, 18 Aug 2025 03:05:11 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1unwj1-00048E-JN; Mon, 18 Aug 2025 06:04:23 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1unwix-00047r-3P for qemu-devel@nongnu.org; Mon, 18 Aug 2025 06:04:21 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1unwit-0002q5-H3 for qemu-devel@nongnu.org; Mon, 18 Aug 2025 06:04:18 -0400 Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-456-PV9bGtxPNwyLnAolXRvA9g-1; Mon, 18 Aug 2025 06:04:09 -0400 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 660671800366; Mon, 18 Aug 2025 10:04:08 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.225.45]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 580741800280; Mon, 18 Aug 2025 10:04:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755511452; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=HgbSuxjhhsCf1K+75YmZPJ6FQ8pRFd+H6aW/YuZ18g8=; b=Pk/DruWK98R7KpJj189G0hqOTq9l+4n1lYkvlh5+IPnBOT+JVW60XcgvYLMV9V2sMh9avO dX1QRRiBHlsKx9IHxMMaIyTxXbbDiEyx7AQfKV7+cNEjaXkdUfZr1vn7EC31Sz+PuvPomn +SA3uVBVYim3PIrgwYiclYZRDGNMkAM= X-MC-Unique: PV9bGtxPNwyLnAolXRvA9g-1 X-Mimecast-MFC-AGG-ID: PV9bGtxPNwyLnAolXRvA9g_1755511448 From: Albert Esteve To: qemu-devel@nongnu.org Cc: david@redhat.com, "Michael S. Tsirkin" , hi@alyssa.is, jasowang@redhat.com, Laurent Vivier , dbassey@redhat.com, Stefano Garzarella , Paolo Bonzini , stefanha@redhat.com, stevensd@chromium.org, Fabiano Rosas , =?UTF-8?q?Alex=20Benn=C3=A9e?= , slp@redhat.com, Albert Esteve Subject: [PATCH v7 1/8] vhost-user: Add VirtIO Shared Memory map request Date: Mon, 18 Aug 2025 12:03:46 +0200 Message-ID: <20250818100353.1560655-2-aesteve@redhat.com> In-Reply-To: <20250818100353.1560655-1-aesteve@redhat.com> References: <20250818100353.1560655-1-aesteve@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=aesteve@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=-1, RCVD_IN_MSPIKE_WL=-0.01, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1755511514224124100 Content-Type: text/plain; charset="utf-8" Add SHMEM_MAP/UNMAP requests to vhost-user for dynamic management of VIRTIO Shared Memory mappings. This implementation introduces VhostUserShmemObject as an intermediate QOM parent for MemoryRegions created for SHMEM_MAP requests. This object provides reference-counted lifecycle management with automatic cleanup. This request allows backends to dynamically map file descriptors into a VIRTIO Shared Memory Regions identified by their shmid. Maps are created using memory_region_init_ram_device_ptr() with configurable read/write permissions, and the resulting MemoryRegions are added as subregions to the shmem container region. The mapped memory is then advertised to the guest VIRTIO drivers as a base address plus offset for reading and writting according to the requested mmap flags. The backend can unmap memory ranges within a given VIRTIO Shared Memory Region to free resources. Upon receiving this message, the frontend removes the MemoryRegion as a subregion and automatically unreferences the associated VhostUserShmemObject, triggering cleanup if no other references exist. Error handling has been improved to ensure consistent behavior across handlers that manage their own vhost_user_send_resp() calls. Since these handlers clear the VHOST_USER_NEED_REPLY_MASK flag, explicit error checking ensures proper connection closure on failures, maintaining the expected error flow. Note the memory region commit for these operations needs to be delayed until after we respond to the backend to avoid deadlocks. Signed-off-by: Albert Esteve --- hw/virtio/meson.build | 1 + hw/virtio/vhost-user-shmem.c | 134 ++++++++++++++ hw/virtio/vhost-user.c | 207 +++++++++++++++++++++- hw/virtio/virtio.c | 109 ++++++++++++ include/hw/virtio/vhost-user-shmem.h | 75 ++++++++ include/hw/virtio/virtio.h | 93 ++++++++++ subprojects/libvhost-user/libvhost-user.c | 70 ++++++++ subprojects/libvhost-user/libvhost-user.h | 54 ++++++ 8 files changed, 741 insertions(+), 2 deletions(-) create mode 100644 hw/virtio/vhost-user-shmem.c create mode 100644 include/hw/virtio/vhost-user-shmem.h diff --git a/hw/virtio/meson.build b/hw/virtio/meson.build index 3ea7b3cec8..5efcf70b75 100644 --- a/hw/virtio/meson.build +++ b/hw/virtio/meson.build @@ -20,6 +20,7 @@ if have_vhost # fixme - this really should be generic specific_virtio_ss.add(files('vhost-user.c')) system_virtio_ss.add(files('vhost-user-base.c')) + system_virtio_ss.add(files('vhost-user-shmem.c')) =20 # MMIO Stubs system_virtio_ss.add(files('vhost-user-device.c')) diff --git a/hw/virtio/vhost-user-shmem.c b/hw/virtio/vhost-user-shmem.c new file mode 100644 index 0000000000..1d763b56b6 --- /dev/null +++ b/hw/virtio/vhost-user-shmem.c @@ -0,0 +1,134 @@ +/* + * VHost-user Shared Memory Object + * + * Copyright Red Hat, Inc. 2025 + * + * Authors: + * Albert Esteve + * + * This work is licensed under the terms of the GNU GPL, version 2 or late= r. + * See the COPYING file in the top-level directory. + */ + +#include "qemu/osdep.h" +#include "hw/virtio/vhost-user-shmem.h" +#include "system/memory.h" +#include "qapi/error.h" +#include "qemu/error-report.h" +#include "trace.h" + +/** + * VhostUserShmemObject + * + * An intermediate QOM object that manages individual shared memory mappin= gs + * created by VHOST_USER_BACKEND_SHMEM_MAP requests. It acts as a parent f= or + * MemoryRegion objects, providing proper lifecycle management with refere= nce + * counting. When the object is unreferenced and its reference count drops + * to zero, it automatically cleans up the MemoryRegion and unmaps the mem= ory. + */ + +static void vhost_user_shmem_object_finalize(Object *obj); +static void vhost_user_shmem_object_instance_init(Object *obj); + +static const TypeInfo vhost_user_shmem_object_info =3D { + .name =3D TYPE_VHOST_USER_SHMEM_OBJECT, + .parent =3D TYPE_OBJECT, + .instance_size =3D sizeof(VhostUserShmemObject), + .instance_init =3D vhost_user_shmem_object_instance_init, + .instance_finalize =3D vhost_user_shmem_object_finalize, +}; + +static void vhost_user_shmem_object_instance_init(Object *obj) +{ + VhostUserShmemObject *shmem_obj =3D VHOST_USER_SHMEM_OBJECT(obj); + + shmem_obj->shmid =3D 0; + shmem_obj->fd =3D -1; + shmem_obj->shm_offset =3D 0; + shmem_obj->len =3D 0; + shmem_obj->mr =3D NULL; +} + +static void vhost_user_shmem_object_finalize(Object *obj) +{ + VhostUserShmemObject *shmem_obj =3D VHOST_USER_SHMEM_OBJECT(obj); + + /* Clean up MemoryRegion if it exists */ + if (shmem_obj->mr) { + /* Unparent the MemoryRegion to trigger cleanup */ + object_unparent(OBJECT(shmem_obj->mr)); + shmem_obj->mr =3D NULL; + } + + /* Close file descriptor */ + if (shmem_obj->fd >=3D 0) { + close(shmem_obj->fd); + shmem_obj->fd =3D -1; + } +} + +VhostUserShmemObject *vhost_user_shmem_object_new(uint8_t shmid, + int fd, + uint64_t fd_offset, + uint64_t shm_offset, + uint64_t len, + uint16_t flags) +{ + VhostUserShmemObject *shmem_obj; + MemoryRegion *mr; + g_autoptr(GString) mr_name =3D g_string_new(NULL); + uint32_t ram_flags; + Error *local_err =3D NULL; + + if (len =3D=3D 0) { + error_report("Shared memory mapping size cannot be zero"); + return NULL; + } + + fd =3D dup(fd); + if (fd < 0) { + error_report("Failed to duplicate fd: %s", strerror(errno)); + return NULL; + } + + /* Determine RAM flags */ + ram_flags =3D RAM_SHARED; + if (!(flags & VHOST_USER_FLAG_MAP_RW)) { + ram_flags |=3D RAM_READONLY_FD; + } + + /* Create the VhostUserShmemObject */ + shmem_obj =3D VHOST_USER_SHMEM_OBJECT( + object_new(TYPE_VHOST_USER_SHMEM_OBJECT)); + + /* Set up object properties */ + shmem_obj->shmid =3D shmid; + shmem_obj->fd =3D fd; + shmem_obj->shm_offset =3D shm_offset; + shmem_obj->len =3D len; + + /* Create MemoryRegion as a child of this object */ + mr =3D g_new0(MemoryRegion, 1); + g_string_printf(mr_name, "vhost-user-shmem-%d-%" PRIx64, shmid, shm_of= fset); + + /* Initialize MemoryRegion with file descriptor */ + if (!memory_region_init_ram_from_fd(mr, OBJECT(shmem_obj), mr_name->st= r, + len, ram_flags, fd, fd_offset, + &local_err)) { + error_report_err(local_err); + g_free(mr); + close(fd); + object_unref(OBJECT(shmem_obj)); + return NULL; + } + + shmem_obj->mr =3D mr; + return shmem_obj; +} + +static void vhost_user_shmem_register_types(void) +{ + type_register_static(&vhost_user_shmem_object_info); +} + +type_init(vhost_user_shmem_register_types) diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c index 1e1d6b0d6e..eb3ad728b0 100644 --- a/hw/virtio/vhost-user.c +++ b/hw/virtio/vhost-user.c @@ -11,6 +11,7 @@ #include "qemu/osdep.h" #include "qapi/error.h" #include "hw/virtio/virtio-dmabuf.h" +#include "hw/virtio/vhost-user-shmem.h" #include "hw/virtio/vhost.h" #include "hw/virtio/virtio-crypto.h" #include "hw/virtio/vhost-user.h" @@ -115,6 +116,8 @@ typedef enum VhostUserBackendRequest { VHOST_USER_BACKEND_SHARED_OBJECT_ADD =3D 6, VHOST_USER_BACKEND_SHARED_OBJECT_REMOVE =3D 7, VHOST_USER_BACKEND_SHARED_OBJECT_LOOKUP =3D 8, + VHOST_USER_BACKEND_SHMEM_MAP =3D 9, + VHOST_USER_BACKEND_SHMEM_UNMAP =3D 10, VHOST_USER_BACKEND_MAX } VhostUserBackendRequest; =20 @@ -192,6 +195,23 @@ typedef struct VhostUserShared { unsigned char uuid[16]; } VhostUserShared; =20 +/* For the flags field of VhostUserMMap */ +#define VHOST_USER_FLAG_MAP_RW (1u << 0) + +typedef struct { + /* VIRTIO Shared Memory Region ID */ + uint8_t shmid; + uint8_t padding[7]; + /* File offset */ + uint64_t fd_offset; + /* Offset within the VIRTIO Shared Memory Region */ + uint64_t shm_offset; + /* Size of the mapping */ + uint64_t len; + /* Flags for the mmap operation, from VHOST_USER_FLAG_MAP_* */ + uint16_t flags; +} VhostUserMMap; + typedef struct { VhostUserRequest request; =20 @@ -224,6 +244,7 @@ typedef union { VhostUserInflight inflight; VhostUserShared object; VhostUserTransferDeviceState transfer_state; + VhostUserMMap mmap; } VhostUserPayload; =20 typedef struct VhostUserMsg { @@ -1768,6 +1789,172 @@ vhost_user_backend_handle_shared_object_lookup(stru= ct vhost_user *u, return 0; } =20 +/** + * vhost_user_backend_handle_shmem_map() - Handle SHMEM_MAP backend request + * @dev: vhost device + * @ioc: QIOChannel for communication + * @hdr: vhost-user message header + * @payload: message payload containing mapping details + * @fd: file descriptor for the shared memory region + * + * Handles VHOST_USER_BACKEND_SHMEM_MAP requests from the backend. Creates + * a VhostUserShmemObject to manage the shared memory mapping and adds it + * to the appropriate VirtIO shared memory region. The VhostUserShmemObject + * serves as an intermediate parent for the MemoryRegion, ensuring proper + * lifecycle management with reference counting. + * + * Returns: 0 on success, negative errno on failure + */ +static int +vhost_user_backend_handle_shmem_map(struct vhost_dev *dev, + QIOChannel *ioc, + VhostUserHeader *hdr, + VhostUserPayload *payload, + int fd) +{ + VirtioSharedMemory *shmem; + VhostUserMMap *vu_mmap =3D &payload->mmap; + Error *local_err =3D NULL; + g_autoptr(GString) shm_name =3D g_string_new(NULL); + + if (fd < 0) { + error_report("Bad fd for map"); + return -EBADF; + } + + if (QSIMPLEQ_EMPTY(&dev->vdev->shmem_list)) { + error_report("Device has no VIRTIO Shared Memory Regions. " + "Requested ID: %d", vu_mmap->shmid); + return -EFAULT; + } + + shmem =3D virtio_find_shmem_region(dev->vdev, vu_mmap->shmid); + if (!shmem) { + error_report("VIRTIO Shared Memory Region at " + "ID %d not found or unitialized", vu_mmap->shmid); + return -EFAULT; + } + + if ((vu_mmap->shm_offset + vu_mmap->len) < vu_mmap->len || + (vu_mmap->shm_offset + vu_mmap->len) > shmem->mr.size) { + error_report("Bad offset/len for mmap %" PRIx64 "+%" PRIx64, + vu_mmap->shm_offset, vu_mmap->len); + return -EFAULT; + } + + g_string_printf(shm_name, "virtio-shm%i-%lu", + vu_mmap->shmid, vu_mmap->shm_offset); + + memory_region_transaction_begin(); + + /* Create VhostUserShmemObject as intermediate parent for MemoryRegion= */ + VhostUserShmemObject *shmem_obj =3D vhost_user_shmem_object_new( + vu_mmap->shmid, fd, vu_mmap->fd_offset, vu_mmap->shm_offset, + vu_mmap->len, vu_mmap->flags); + + if (!shmem_obj) { + memory_region_transaction_commit(); + return -EFAULT; + } + + /* Add the mapping using our VhostUserShmemObject as the parent */ + if (virtio_add_shmem_map(shmem, shmem_obj) !=3D 0) { + error_report("Failed to add shared memory mapping"); + object_unref(OBJECT(shmem_obj)); + memory_region_transaction_commit(); + return -EFAULT; + } + + if (hdr->flags & VHOST_USER_NEED_REPLY_MASK) { + payload->u64 =3D 0; + hdr->size =3D sizeof(payload->u64); + vhost_user_send_resp(ioc, hdr, payload, &local_err); + if (local_err) { + error_report_err(local_err); + memory_region_transaction_commit(); + return -EFAULT; + } + } + + memory_region_transaction_commit(); + + return 0; +} + +/** + * vhost_user_backend_handle_shmem_unmap() - Handle SHMEM_UNMAP backend re= quest + * @dev: vhost device + * @ioc: QIOChannel for communication + * @hdr: vhost-user message header + * @payload: message payload containing unmapping details + * + * Handles VHOST_USER_BACKEND_SHMEM_UNMAP requests from the backend. Remov= es + * the specified memory mapping from the VirtIO shared memory region. This + * automatically unreferences the associated VhostUserShmemObject, which m= ay + * trigger its finalization and cleanup (munmap, close fd) if no other + * references exist. + * + * Returns: 0 on success, negative errno on failure + */ +static int +vhost_user_backend_handle_shmem_unmap(struct vhost_dev *dev, + QIOChannel *ioc, + VhostUserHeader *hdr, + VhostUserPayload *payload) +{ + VirtioSharedMemory *shmem; + VirtioSharedMemoryMapping *mmap =3D NULL; + VhostUserMMap *vu_mmap =3D &payload->mmap; + Error *local_err =3D NULL; + + if (QSIMPLEQ_EMPTY(&dev->vdev->shmem_list)) { + error_report("Device has no VIRTIO Shared Memory Regions. " + "Requested ID: %d", vu_mmap->shmid); + return -EFAULT; + } + + shmem =3D virtio_find_shmem_region(dev->vdev, vu_mmap->shmid); + if (!shmem) { + error_report("VIRTIO Shared Memory Region at " + "ID %d not found or unitialized", vu_mmap->shmid); + return -EFAULT; + } + + if ((vu_mmap->shm_offset + vu_mmap->len) < vu_mmap->len || + (vu_mmap->shm_offset + vu_mmap->len) > shmem->mr.size) { + error_report("Bad offset/len for unmmap %" PRIx64 "+%" PRIx64, + vu_mmap->shm_offset, vu_mmap->len); + return -EFAULT; + } + + mmap =3D virtio_find_shmem_map(shmem, vu_mmap->shm_offset, vu_mmap->le= n); + if (!mmap) { + error_report("Shared memory mapping not found at offset %" PRIx64 + " with length %" PRIx64, + vu_mmap->shm_offset, vu_mmap->len); + return -EFAULT; + } + + memory_region_transaction_begin(); + memory_region_del_subregion(&shmem->mr, mmap->mem); + if (hdr->flags & VHOST_USER_NEED_REPLY_MASK) { + payload->u64 =3D 0; + hdr->size =3D sizeof(payload->u64); + vhost_user_send_resp(ioc, hdr, payload, &local_err); + if (local_err) { + error_report_err(local_err); + memory_region_transaction_commit(); + return -EFAULT; + } + } + memory_region_transaction_commit(); + + /* Free the MemoryRegion only after vhost_commit */ + virtio_del_shmem_map(shmem, vu_mmap->shm_offset, vu_mmap->len); + + return 0; +} + static void close_backend_channel(struct vhost_user *u) { g_source_destroy(u->backend_src); @@ -1833,8 +2020,24 @@ static gboolean backend_read(QIOChannel *ioc, GIOCon= dition condition, &payload.obje= ct); break; case VHOST_USER_BACKEND_SHARED_OBJECT_LOOKUP: - ret =3D vhost_user_backend_handle_shared_object_lookup(dev->opaque= , ioc, - &hdr, &payloa= d); + /* Handler manages its own response, check error and close connect= ion */ + if (vhost_user_backend_handle_shared_object_lookup(dev->opaque, io= c, + &hdr, &payload)= < 0) { + goto err; + } + break; + case VHOST_USER_BACKEND_SHMEM_MAP: + /* Handler manages its own response, check error and close connect= ion */ + if (vhost_user_backend_handle_shmem_map(dev, ioc, &hdr, &payload, + fd ? fd[0] : -1) < 0) { + goto err; + } + break; + case VHOST_USER_BACKEND_SHMEM_UNMAP: + /* Handler manages its own response, check error and close connect= ion */ + if (vhost_user_backend_handle_shmem_unmap(dev, ioc, &hdr, &payload= ) < 0) { + goto err; + } break; default: error_report("Received unexpected msg type: %d.", hdr.request); diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index 9a81ad912e..1ead5f653f 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -14,6 +14,7 @@ #include "qemu/osdep.h" #include "qapi/error.h" #include "qapi/qapi-commands-virtio.h" +#include "hw/virtio/vhost-user-shmem.h" #include "trace.h" #include "qemu/defer-call.h" #include "qemu/error-report.h" @@ -3045,6 +3046,100 @@ int virtio_save(VirtIODevice *vdev, QEMUFile *f) return vmstate_save_state(f, &vmstate_virtio, vdev, NULL); } =20 +VirtioSharedMemory *virtio_new_shmem_region(VirtIODevice *vdev, uint8_t sh= mid) +{ + VirtioSharedMemory *elem; + g_autofree char *name =3D NULL; + + elem =3D g_new0(VirtioSharedMemory, 1); + elem->shmid =3D shmid; + + /* Initialize embedded MemoryRegion as container for shmem mappings */ + name =3D g_strdup_printf("virtio-shmem-%d", shmid); + memory_region_init(&elem->mr, OBJECT(vdev), name, UINT64_MAX); + QTAILQ_INIT(&elem->mmaps); + QSIMPLEQ_INSERT_TAIL(&vdev->shmem_list, elem, entry); + return QSIMPLEQ_LAST(&vdev->shmem_list, VirtioSharedMemory, entry); +} + +VirtioSharedMemory *virtio_find_shmem_region(VirtIODevice *vdev, uint8_t s= hmid) +{ + VirtioSharedMemory *shmem, *next; + QSIMPLEQ_FOREACH_SAFE(shmem, &vdev->shmem_list, entry, next) { + if (shmem->shmid =3D=3D shmid) { + return shmem; + } + } + return NULL; +} + +int virtio_add_shmem_map(VirtioSharedMemory *shmem, + VhostUserShmemObject *shmem_obj) +{ + VirtioSharedMemoryMapping *mmap; + if (!shmem_obj) { + error_report("VhostUserShmemObject cannot be NULL"); + return -1; + } + if (!shmem_obj->mr) { + error_report("VhostUserShmemObject has no MemoryRegion"); + return -1; + } + + /* Validate boundaries against the VIRTIO shared memory region */ + if (shmem_obj->shm_offset + shmem_obj->len > shmem->mr.size) { + error_report("Memory exceeds the shared memory boundaries"); + return -1; + } + + /* Create the VirtioSharedMemoryMapping wrapper */ + mmap =3D g_new0(VirtioSharedMemoryMapping, 1); + mmap->mem =3D shmem_obj->mr; + mmap->offset =3D shmem_obj->shm_offset; + mmap->shmem_obj =3D shmem_obj; + + /* Take a reference on the VhostUserShmemObject */ + object_ref(OBJECT(shmem_obj)); + + /* Add as subregion to the VIRTIO shared memory */ + memory_region_add_subregion(&shmem->mr, mmap->offset, mmap->mem); + + /* Add to the mapped regions list */ + QTAILQ_INSERT_TAIL(&shmem->mmaps, mmap, link); + + return 0; +} + +VirtioSharedMemoryMapping *virtio_find_shmem_map(VirtioSharedMemory *shmem, + hwaddr offset, uint64_t size) +{ + VirtioSharedMemoryMapping *mmap; + QTAILQ_FOREACH(mmap, &shmem->mmaps, link) { + if (mmap->offset =3D=3D offset && mmap->mem->size =3D=3D size) { + return mmap; + } + } + return NULL; +} + +void virtio_del_shmem_map(VirtioSharedMemory *shmem, hwaddr offset, + uint64_t size) +{ + VirtioSharedMemoryMapping *mmap =3D virtio_find_shmem_map(shmem, offse= t, size); + if (mmap =3D=3D NULL) { + return; + } + + /* + * Unref the VhostUserShmemObject which will trigger automatic cleanup + * when the reference count reaches zero. + */ + object_unref(OBJECT(mmap->shmem_obj)); + + QTAILQ_REMOVE(&shmem->mmaps, mmap, link); + g_free(mmap); +} + /* A wrapper for use as a VMState .put function */ static int virtio_device_put(QEMUFile *f, void *opaque, size_t size, const VMStateField *field, JSONWriter *vmdes= c) @@ -3521,6 +3616,7 @@ void virtio_init(VirtIODevice *vdev, uint16_t device_= id, size_t config_size) NULL, virtio_vmstate_change, vdev); vdev->device_endian =3D virtio_default_endian(); vdev->use_guest_notifier_mask =3D true; + QSIMPLEQ_INIT(&vdev->shmem_list); } =20 /* @@ -4032,11 +4128,24 @@ static void virtio_device_free_virtqueues(VirtIODev= ice *vdev) static void virtio_device_instance_finalize(Object *obj) { VirtIODevice *vdev =3D VIRTIO_DEVICE(obj); + VirtioSharedMemory *shmem; =20 virtio_device_free_virtqueues(vdev); =20 g_free(vdev->config); g_free(vdev->vector_queues); + while (!QSIMPLEQ_EMPTY(&vdev->shmem_list)) { + shmem =3D QSIMPLEQ_FIRST(&vdev->shmem_list); + while (!QTAILQ_EMPTY(&shmem->mmaps)) { + VirtioSharedMemoryMapping *mmap_reg =3D QTAILQ_FIRST(&shmem->m= maps); + virtio_del_shmem_map(shmem, mmap_reg->offset, mmap_reg->mem->s= ize); + } + + /* Clean up the embedded MemoryRegion */ + object_unparent(OBJECT(&shmem->mr)); + QSIMPLEQ_REMOVE_HEAD(&vdev->shmem_list, entry); + g_free(shmem); + } } =20 static const Property virtio_properties[] =3D { diff --git a/include/hw/virtio/vhost-user-shmem.h b/include/hw/virtio/vhost= -user-shmem.h new file mode 100644 index 0000000000..1f8c7bdc1f --- /dev/null +++ b/include/hw/virtio/vhost-user-shmem.h @@ -0,0 +1,75 @@ +/* + * VHost-user Shared Memory Object + * + * Copyright Red Hat, Inc. 2025 + * + * Authors: + * Albert Esteve + * + * This work is licensed under the terms of the GNU GPL, version 2 or late= r. + * See the COPYING file in the top-level directory. + */ + +#ifndef VHOST_USER_SHMEM_H +#define VHOST_USER_SHMEM_H + +#include "qemu/osdep.h" +#include "qom/object.h" +#include "system/memory.h" +#include "qapi/error.h" + +/* vhost-user memory mapping flags */ +#define VHOST_USER_FLAG_MAP_RW (1u << 0) + +#define TYPE_VHOST_USER_SHMEM_OBJECT "vhost-user-shmem" +OBJECT_DECLARE_SIMPLE_TYPE(VhostUserShmemObject, VHOST_USER_SHMEM_OBJECT) + +/** + * VhostUserShmemObject: + * @parent: Parent object + * @shmid: VIRTIO Shared Memory Region ID + * @fd: File descriptor for the shared memory region + * @shm_offset: Offset within the VIRTIO Shared Memory Region + * @len: Size of the mapping + * @mr: MemoryRegion associated with this shared memory mapping + * + * An intermediate QOM object that manages individual shared memory mappin= gs + * created by VHOST_USER_BACKEND_SHMEM_MAP requests. It acts as a parent f= or + * MemoryRegion objects, providing proper lifecycle management with refere= nce + * counting. When the object is unreferenced and its reference count drops + * to zero, it automatically cleans up the MemoryRegion and unmaps the mem= ory. + */ +struct VhostUserShmemObject { + Object parent; + + uint8_t shmid; + int fd; + uint64_t shm_offset; + uint64_t len; + MemoryRegion *mr; +}; + +/** + * vhost_user_shmem_object_new() - Create a new VhostUserShmemObject + * @shmid: VIRTIO Shared Memory Region ID + * @fd: File descriptor for the shared memory + * @fd_offset: Offset within the file descriptor + * @shm_offset: Offset within the VIRTIO Shared Memory Region + * @len: Size of the mapping + * @flags: Mapping flags (VHOST_USER_FLAG_MAP_*) + * + * Creates a new VhostUserShmemObject that manages a shared memory mapping. + * The object will create a MemoryRegion using memory_region_init_ram_from= _fd() + * as a child object. When the object is finalized, it will automatically + * clean up the MemoryRegion and close the file descriptor. + * + * Return: A new VhostUserShmemObject on success, NULL on error. + */ +VhostUserShmemObject *vhost_user_shmem_object_new(uint8_t shmid, + int fd, + uint64_t fd_offset, + uint64_t shm_offset, + uint64_t len, + uint16_t flags); + +#endif /* VHOST_USER_SHMEM_H */ diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h index c594764f23..a563bbac2c 100644 --- a/include/hw/virtio/virtio.h +++ b/include/hw/virtio/virtio.h @@ -98,6 +98,26 @@ enum virtio_device_endian { VIRTIO_DEVICE_ENDIAN_BIG, }; =20 +struct VhostUserShmemObject; + +struct VirtioSharedMemoryMapping { + MemoryRegion *mem; + hwaddr offset; + QTAILQ_ENTRY(VirtioSharedMemoryMapping) link; + struct VhostUserShmemObject *shmem_obj; /* Intermediate parent object = */ +}; + +typedef struct VirtioSharedMemoryMapping VirtioSharedMemoryMapping; + +struct VirtioSharedMemory { + uint8_t shmid; + MemoryRegion mr; + QTAILQ_HEAD(, VirtioSharedMemoryMapping) mmaps; + QSIMPLEQ_ENTRY(VirtioSharedMemory) entry; +}; + +typedef struct VirtioSharedMemory VirtioSharedMemory; + /** * struct VirtIODevice - common VirtIO structure * @name: name of the device @@ -167,6 +187,8 @@ struct VirtIODevice */ EventNotifier config_notifier; bool device_iotlb_enabled; + /* Shared memory region for mappings. */ + QSIMPLEQ_HEAD(, VirtioSharedMemory) shmem_list; }; =20 struct VirtioDeviceClass { @@ -295,6 +317,77 @@ void virtio_notify(VirtIODevice *vdev, VirtQueue *vq); =20 int virtio_save(VirtIODevice *vdev, QEMUFile *f); =20 +/** + * virtio_new_shmem_region() - Create a new shared memory region + * @vdev: VirtIODevice + * @shmid: Shared memory ID + * + * Creates a new VirtioSharedMemory region for the given device and ID. + * The returned VirtioSharedMemory is owned by the VirtIODevice and will + * be automatically freed when the device is destroyed. The caller + * should not free the returned pointer. + * + * Returns: Pointer to the new VirtioSharedMemory region, or NULL on failu= re + */ +VirtioSharedMemory *virtio_new_shmem_region(VirtIODevice *vdev, uint8_t sh= mid); + +/** + * virtio_find_shmem_region() - Find an existing shared memory region + * @vdev: VirtIODevice + * @shmid: Shared memory ID to find + * + * Finds an existing VirtioSharedMemory region by ID. The returned pointer + * is owned by the VirtIODevice and should not be freed by the caller. + * + * Returns: Pointer to the VirtioSharedMemory region, or NULL if not found + */ +VirtioSharedMemory *virtio_find_shmem_region(VirtIODevice *vdev, uint8_t s= hmid); + +/** + * virtio_add_shmem_map() - Add a memory mapping to a shared region + * @shmem: VirtioSharedMemory region + * @shmem_obj: VhostUserShmemObject to add (takes a reference) + * + * Adds a memory mapping to the shared memory region. The VhostUserShmemOb= ject + * is added as a child of the mapping and will be automatically managed th= rough + * QOM reference counting. The mapping will be removed when + * virtio_del_shmem_map() is called or when the shared memory region is + * destroyed. + * + * Returns: 0 on success, negative errno on failure + */ +int virtio_add_shmem_map(VirtioSharedMemory *shmem, + struct VhostUserShmemObject *shmem_obj); + +/** + * virtio_find_shmem_map() - Find a memory mapping in a shared region + * @shmem: VirtioSharedMemory region + * @offset: Offset within the shared memory region + * @size: Size of the mapping to find + * + * Finds an existing memory mapping that covers the specified range. + * The returned VirtioSharedMemoryMapping is owned by the VirtioSharedMemo= ry + * region and should not be freed by the caller. + * + * Returns: Pointer to the VirtioSharedMemoryMapping, or NULL if not found + */ +VirtioSharedMemoryMapping *virtio_find_shmem_map(VirtioSharedMemory *shmem, + hwaddr offset, uint64_t size); + +/** + * virtio_del_shmem_map() - Remove a memory mapping from a shared region + * @shmem: VirtioSharedMemory region + * @offset: Offset of the mapping to remove + * @size: Size of the mapping to remove + * + * Removes a memory mapping from the shared memory region. This will + * automatically unref the associated VhostUserShmemObject, which may + * trigger its finalization and cleanup if no other references exist. + * The mapping's MemoryRegion will be properly unmapped and cleaned up. + */ +void virtio_del_shmem_map(VirtioSharedMemory *shmem, hwaddr offset, + uint64_t size); + extern const VMStateInfo virtio_vmstate_info; =20 #define VMSTATE_VIRTIO_DEVICE \ diff --git a/subprojects/libvhost-user/libvhost-user.c b/subprojects/libvho= st-user/libvhost-user.c index 9c630c2170..034cbfdc3c 100644 --- a/subprojects/libvhost-user/libvhost-user.c +++ b/subprojects/libvhost-user/libvhost-user.c @@ -1592,6 +1592,76 @@ vu_rm_shared_object(VuDev *dev, unsigned char uuid[U= UID_LEN]) return vu_send_message(dev, &msg); } =20 +bool +vu_shmem_map(VuDev *dev, uint8_t shmid, uint64_t fd_offset, + uint64_t shm_offset, uint64_t len, uint64_t flags, int fd) +{ + VhostUserMsg vmsg =3D { + .request =3D VHOST_USER_BACKEND_SHMEM_MAP, + .size =3D sizeof(vmsg.payload.mmap), + .flags =3D VHOST_USER_VERSION, + .payload.mmap =3D { + .shmid =3D shmid, + .fd_offset =3D fd_offset, + .shm_offset =3D shm_offset, + .len =3D len, + .flags =3D flags, + }, + .fd_num =3D 1, + .fds[0] =3D fd, + }; + + if (!vu_has_protocol_feature(dev, VHOST_USER_PROTOCOL_F_SHMEM)) { + return false; + } + + if (vu_has_protocol_feature(dev, VHOST_USER_PROTOCOL_F_REPLY_ACK)) { + vmsg.flags |=3D VHOST_USER_NEED_REPLY_MASK; + } + + pthread_mutex_lock(&dev->backend_mutex); + if (!vu_message_write(dev, dev->backend_fd, &vmsg)) { + pthread_mutex_unlock(&dev->backend_mutex); + return false; + } + + /* Also unlocks the backend_mutex */ + return vu_process_message_reply(dev, &vmsg); +} + +bool +vu_shmem_unmap(VuDev *dev, uint8_t shmid, uint64_t shm_offset, uint64_t le= n) +{ + VhostUserMsg vmsg =3D { + .request =3D VHOST_USER_BACKEND_SHMEM_UNMAP, + .size =3D sizeof(vmsg.payload.mmap), + .flags =3D VHOST_USER_VERSION, + .payload.mmap =3D { + .shmid =3D shmid, + .fd_offset =3D 0, + .shm_offset =3D shm_offset, + .len =3D len, + }, + }; + + if (!vu_has_protocol_feature(dev, VHOST_USER_PROTOCOL_F_SHMEM)) { + return false; + } + + if (vu_has_protocol_feature(dev, VHOST_USER_PROTOCOL_F_REPLY_ACK)) { + vmsg.flags |=3D VHOST_USER_NEED_REPLY_MASK; + } + + pthread_mutex_lock(&dev->backend_mutex); + if (!vu_message_write(dev, dev->backend_fd, &vmsg)) { + pthread_mutex_unlock(&dev->backend_mutex); + return false; + } + + /* Also unlocks the backend_mutex */ + return vu_process_message_reply(dev, &vmsg); +} + static bool vu_set_vring_call_exec(VuDev *dev, VhostUserMsg *vmsg) { diff --git a/subprojects/libvhost-user/libvhost-user.h b/subprojects/libvho= st-user/libvhost-user.h index 2ffc58c11b..26b710c92d 100644 --- a/subprojects/libvhost-user/libvhost-user.h +++ b/subprojects/libvhost-user/libvhost-user.h @@ -69,6 +69,8 @@ enum VhostUserProtocolFeature { /* Feature 16 is reserved for VHOST_USER_PROTOCOL_F_STATUS. */ /* Feature 17 reserved for VHOST_USER_PROTOCOL_F_XEN_MMAP. */ VHOST_USER_PROTOCOL_F_SHARED_OBJECT =3D 18, + /* Feature 19 is reserved for VHOST_USER_PROTOCOL_F_DEVICE_STATE */ + VHOST_USER_PROTOCOL_F_SHMEM =3D 20, VHOST_USER_PROTOCOL_F_MAX }; =20 @@ -127,6 +129,8 @@ typedef enum VhostUserBackendRequest { VHOST_USER_BACKEND_SHARED_OBJECT_ADD =3D 6, VHOST_USER_BACKEND_SHARED_OBJECT_REMOVE =3D 7, VHOST_USER_BACKEND_SHARED_OBJECT_LOOKUP =3D 8, + VHOST_USER_BACKEND_SHMEM_MAP =3D 9, + VHOST_USER_BACKEND_SHMEM_UNMAP =3D 10, VHOST_USER_BACKEND_MAX } VhostUserBackendRequest; =20 @@ -186,6 +190,23 @@ typedef struct VhostUserShared { unsigned char uuid[UUID_LEN]; } VhostUserShared; =20 +/* For the flags field of VhostUserMMap */ +#define VHOST_USER_FLAG_MAP_RW (1u << 0) + +typedef struct { + /* VIRTIO Shared Memory Region ID */ + uint8_t shmid; + uint8_t padding[7]; + /* File offset */ + uint64_t fd_offset; + /* Offset within the VIRTIO Shared Memory Region */ + uint64_t shm_offset; + /* Size of the mapping */ + uint64_t len; + /* Flags for the mmap operation, from VHOST_USER_FLAG_MAP_* */ + uint16_t flags; +} VhostUserMMap; + #define VU_PACKED __attribute__((packed)) =20 typedef struct VhostUserMsg { @@ -210,6 +231,7 @@ typedef struct VhostUserMsg { VhostUserVringArea area; VhostUserInflight inflight; VhostUserShared object; + VhostUserMMap mmap; } payload; =20 int fds[VHOST_MEMORY_BASELINE_NREGIONS]; @@ -593,6 +615,38 @@ bool vu_add_shared_object(VuDev *dev, unsigned char uu= id[UUID_LEN]); */ bool vu_rm_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN]); =20 +/** + * vu_shmem_map: + * @dev: a VuDev context + * @shmid: VIRTIO Shared Memory Region ID + * @fd_offset: File offset + * @shm_offset: Offset within the VIRTIO Shared Memory Region + * @len: Size of the mapping + * @flags: Flags for the mmap operation + * @fd: A file descriptor + * + * Advertises a new mapping to be made in a given VIRTIO Shared Memory Reg= ion. + * + * Returns: TRUE on success, FALSE on failure. + */ +bool vu_shmem_map(VuDev *dev, uint8_t shmid, uint64_t fd_offset, + uint64_t shm_offset, uint64_t len, uint64_t flags, int f= d); + +/** + * vu_shmem_unmap: + * @dev: a VuDev context + * @shmid: VIRTIO Shared Memory Region ID + * @fd_offset: File offset + * @len: Size of the mapping + * + * The front-end un-mmaps a given range in the VIRTIO Shared Memory Region + * with the requested `shmid`. + * + * Returns: TRUE on success, FALSE on failure. + */ +bool vu_shmem_unmap(VuDev *dev, uint8_t shmid, uint64_t shm_offset, + uint64_t len); + /** * vu_queue_set_notification: * @dev: a VuDev context --=20 2.49.0 From nobody Sat Nov 15 05:33:49 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1755511545; cv=none; d=zohomail.com; s=zohoarc; b=ZX71H0hQtm92b0avEZ8dbyh7Kqh1IBSvf9pycuvgJkF1NRH9dx0QWWo2bMtrnRGAg0dJRFuiLHp9HtiRwXEf6pTemd1if8uuHYyYlKoX9hXVYAJTX9ejsB6QLNgGZUmtbeGFlite+rhPQ3fgCT14tL/fkz2tuXkGokF7/Wp4dJ8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755511545; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=2JRarTR2AYg3ChsZq/h++rQDJXjy/c1zeADcSA56lqE=; b=HiC7NSO9yR8clUs9/xk/J36QVs/+Vj3H5q0TKArjgp3Wpln9ViAHlzOiCePN2z5g0m0DvntbR4GaaZKz2PZ5ZKe/XFGfc2NGOsx0NzRc75j8PzM5qrWT3lhG023RjRs52e0FwizQ5heCuzOvZUTAitsLwE7BzKoUPmX7ENMCKQM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1755511545428651.8787868820085; Mon, 18 Aug 2025 03:05:45 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1unwj2-00048F-7j; Mon, 18 Aug 2025 06:04:24 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1unwiy-00047z-Qp for qemu-devel@nongnu.org; Mon, 18 Aug 2025 06:04:21 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1unwix-0002qN-26 for qemu-devel@nongnu.org; Mon, 18 Aug 2025 06:04:20 -0400 Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-491-_GdlpImFN0qBf6V0d5CdWg-1; Mon, 18 Aug 2025 06:04:15 -0400 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 75EA7195608D; Mon, 18 Aug 2025 10:04:14 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.225.45]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 021EF1800280; Mon, 18 Aug 2025 10:04:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755511458; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2JRarTR2AYg3ChsZq/h++rQDJXjy/c1zeADcSA56lqE=; b=Q6+Bi/5KoJAXYo7btaEqle0IZR660WlZyoBWmUAnyEvcA61Mv/dlSOopP8zG+fjD4LM9pl So7YjA3X8cjtCXm8LZc6nn2WJ+bFzPfCmBf0A01O2TlxrkbbzsuUUhsOjBzXcwDuMqFqnN UrIZuhGvYaJZbNorT36LSRhX/eZ8EGE= X-MC-Unique: _GdlpImFN0qBf6V0d5CdWg-1 X-Mimecast-MFC-AGG-ID: _GdlpImFN0qBf6V0d5CdWg_1755511454 From: Albert Esteve To: qemu-devel@nongnu.org Cc: david@redhat.com, "Michael S. Tsirkin" , hi@alyssa.is, jasowang@redhat.com, Laurent Vivier , dbassey@redhat.com, Stefano Garzarella , Paolo Bonzini , stefanha@redhat.com, stevensd@chromium.org, Fabiano Rosas , =?UTF-8?q?Alex=20Benn=C3=A9e?= , slp@redhat.com, Albert Esteve Subject: [PATCH v7 2/8] vhost_user.rst: Align VhostUserMsg excerpt members Date: Mon, 18 Aug 2025 12:03:47 +0200 Message-ID: <20250818100353.1560655-3-aesteve@redhat.com> In-Reply-To: <20250818100353.1560655-1-aesteve@redhat.com> References: <20250818100353.1560655-1-aesteve@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=aesteve@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=-1, RCVD_IN_MSPIKE_WL=-0.01, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1755511547278116600 Content-Type: text/plain; charset="utf-8" Add missing members to the VhostUserMsg excerpt in the vhost-user spec documentation. Reviewed-by: David Hildenbrand Reviewed-by: Stefan Hajnoczi Signed-off-by: Albert Esteve --- docs/interop/vhost-user.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/interop/vhost-user.rst b/docs/interop/vhost-user.rst index 2e50f2ddfa..436a94c0ee 100644 --- a/docs/interop/vhost-user.rst +++ b/docs/interop/vhost-user.rst @@ -366,11 +366,15 @@ In QEMU the vhost-user message is implemented with th= e following struct: struct vhost_vring_state state; struct vhost_vring_addr addr; VhostUserMemory memory; + VhostUserMemRegMsg mem_reg; VhostUserLog log; struct vhost_iotlb_msg iotlb; VhostUserConfig config; + VhostUserCryptoSession session; VhostUserVringArea area; VhostUserInflight inflight; + VhostUserShared object; + VhostUserTransferDeviceState transfer_state; }; } QEMU_PACKED VhostUserMsg; =20 --=20 2.49.0 From nobody Sat Nov 15 05:33:49 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1755511511; cv=none; d=zohomail.com; s=zohoarc; b=MhAaLqJnJFHVivCsLoRW2Jom6cdSfETgdOS1UQBKJO65utcqcgAgdT389ksD3fqX0sO2PwH1KsZMUgfTN1biFX3grO1qD2vX3nmnsAZPK0R2GNyOqLa0CuXO+jtgonpNWXH3+Om1qiiLTfyIgwzv3SOH1Z5czWGMqJqSKM/esj0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755511511; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=56lEYxdS9u2mMEy3cKp25B8ggHhbMd+3/bl921MPr9g=; b=MGXtVBNTeKqWcaU4KMNVE4Pm22xYKr5HnbyXqPMk4Dxmx1w4aZO/VdO11W7R6g5SCY6qK4Dtqgd36HPrr6BLGXJkWIK2v8JULPEKpEkb4gajKehGnJmIQ5vNQOcETxxHcyzLxpDVIuoCkcvmo5UUZ3zf4bHYkAl4YWm0/jeNu4g= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1755511511289846.9738790526984; Mon, 18 Aug 2025 03:05:11 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1unwj6-00048y-Q3; Mon, 18 Aug 2025 06:04:28 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1unwj5-00048p-4g for qemu-devel@nongnu.org; Mon, 18 Aug 2025 06:04:27 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1unwj3-0002rE-EG for qemu-devel@nongnu.org; Mon, 18 Aug 2025 06:04:26 -0400 Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-247-Lcq5wbobM2Wc0n-Ep941KQ-1; Mon, 18 Aug 2025 06:04:23 -0400 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 1C8261954B19; Mon, 18 Aug 2025 10:04:21 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.225.45]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 2555C1800280; Mon, 18 Aug 2025 10:04:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755511464; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=56lEYxdS9u2mMEy3cKp25B8ggHhbMd+3/bl921MPr9g=; b=REft1Sav0tKnhIpNj96DWSdjElq8ciiiPYDVOJCYn1jGhaBexcwTKJhqty63/u66m4aAnP KGZY4es+w+6MWT/J+KO/rCTTamVSDUjhnIAjdWAgLQEXVYAEvQ7xmSSyBhTHOPMCBoVxdh QNztxfD0o3dWWIRAjdV6UP663UTxqPQ= X-MC-Unique: Lcq5wbobM2Wc0n-Ep941KQ-1 X-Mimecast-MFC-AGG-ID: Lcq5wbobM2Wc0n-Ep941KQ_1755511461 From: Albert Esteve To: qemu-devel@nongnu.org Cc: david@redhat.com, "Michael S. Tsirkin" , hi@alyssa.is, jasowang@redhat.com, Laurent Vivier , dbassey@redhat.com, Stefano Garzarella , Paolo Bonzini , stefanha@redhat.com, stevensd@chromium.org, Fabiano Rosas , =?UTF-8?q?Alex=20Benn=C3=A9e?= , slp@redhat.com, Albert Esteve Subject: [PATCH v7 3/8] vhost_user.rst: Add SHMEM_MAP/_UNMAP to spec Date: Mon, 18 Aug 2025 12:03:48 +0200 Message-ID: <20250818100353.1560655-4-aesteve@redhat.com> In-Reply-To: <20250818100353.1560655-1-aesteve@redhat.com> References: <20250818100353.1560655-1-aesteve@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=aesteve@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=-1, RCVD_IN_MSPIKE_WL=-0.01, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1755511520143116600 Content-Type: text/plain; charset="utf-8" Add SHMEM_MAP/_UNMAP request to the vhost-user spec documentation. Reviewed-by: Stefan Hajnoczi Signed-off-by: Albert Esteve --- docs/interop/vhost-user.rst | 58 +++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/docs/interop/vhost-user.rst b/docs/interop/vhost-user.rst index 436a94c0ee..dab9f3af42 100644 --- a/docs/interop/vhost-user.rst +++ b/docs/interop/vhost-user.rst @@ -350,6 +350,27 @@ Device state transfer parameters In the future, additional phases might be added e.g. to allow iterative migration while the device is running. =20 +MMAP request +^^^^^^^^^^^^ + ++-------+---------+-----------+------------+-----+-------+ +| shmid | padding | fd_offset | shm_offset | len | flags | ++-------+---------+-----------+------------+-----+-------+ + +:shmid: a 8-bit shared memory region identifier + +:fd_offset: a 64-bit offset of this area from the start + of the supplied file descriptor + +:shm_offset: a 64-bit offset from the start of the + pointed shared memory region + +:len: a 64-bit size of the memory to map + +:flags: a 64-bit value: + - 0: Pages are mapped read-only + - 1: Pages are mapped read-write + C structure ----------- =20 @@ -375,6 +396,7 @@ In QEMU the vhost-user message is implemented with the = following struct: VhostUserInflight inflight; VhostUserShared object; VhostUserTransferDeviceState transfer_state; + VhostUserMMap mmap; }; } QEMU_PACKED VhostUserMsg; =20 @@ -1057,6 +1079,7 @@ Protocol features #define VHOST_USER_PROTOCOL_F_XEN_MMAP 17 #define VHOST_USER_PROTOCOL_F_SHARED_OBJECT 18 #define VHOST_USER_PROTOCOL_F_DEVICE_STATE 19 + #define VHOST_USER_PROTOCOL_F_SHMEM 20 =20 Front-end message types ----------------------- @@ -1865,6 +1888,41 @@ is sent by the front-end. when the operation is successful, or non-zero otherwise. Note that if the operation fails, no fd is sent to the backend. =20 +``VHOST_USER_BACKEND_SHMEM_MAP`` + :id: 9 + :equivalent ioctl: N/A + :request payload: fd and ``struct VhostUserMMap`` + :reply payload: N/A + + When the ``VHOST_USER_PROTOCOL_F_SHMEM`` protocol feature has been + successfully negotiated, this message can be submitted by the backends to + advertise a new mapping to be made in a given VIRTIO Shared Memory Regio= n. + Upon receiving the message, the front-end will mmap the given fd into the + VIRTIO Shared Memory Region with the requested ``shmid``. + If``VHOST_USER_PROTOCOL_F_REPLY_ACK`` is negotiated, and + back-end set the ``VHOST_USER_NEED_REPLY`` flag, the front-end + must respond with zero when operation is successfully completed, + or non-zero otherwise. + + Mapping over an already existing map is not allowed and requests shall f= ail. + Therefore, the memory range in the request must correspond with a valid, + free region of the VIRTIO Shared Memory Region. Also, note that mappings + consume resources and that the request can fail when there are no resour= ces + available. + +``VHOST_USER_BACKEND_SHMEM_UNMAP`` + :id: 10 + :equivalent ioctl: N/A + :request payload: ``struct VhostUserMMap`` + :reply payload: N/A + + When the ``VHOST_USER_PROTOCOL_F_SHMEM`` protocol feature has been + successfully negotiated, this message can be submitted by the backends so + that the front-end un-mmaps a given range (``shm_offset``, ``len``) in t= he + VIRTIO Shared Memory Region with the requested ``shmid``. Note that the + given range shall correspond to the entirety of a valid mapped region. + A reply is generated indicating whether unmapping succeeded. + .. _reply_ack: =20 VHOST_USER_PROTOCOL_F_REPLY_ACK --=20 2.49.0 From nobody Sat Nov 15 05:33:49 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1755511515; cv=none; d=zohomail.com; s=zohoarc; b=YI5wgS2O43sUsV4t0HUc3xE6XmGXnmrM+1ZIkbszOtxFZ/QaL0zxIcTUxWAwveoTd4JDADl4nam7LNjOKXj2rOhrAkzXE4P1rOY3UiEEbfGV3SWPDSLl6QtQm+l5w9lWigX84mESls9k0mTzZsP3oOg1WRqT2amFWC+zTlMZr24= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755511515; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=P9I8pN3Ic6kx4QGFOkHjvVo+AEU5MXFerKiYPdyNHE0=; b=SEIgrrsBU/iNKLZvaFOm5LECfECGY4w3AkH281nUOBIGxeUG0UiYCVvl7UOjcLgzoC6uCjF4DpxKloFo6nbVTmDG5VIHxHAFGeVgVOvgqibcXPTdtZIy/HQrAwpLoxnIywi2Y2ATcNjfLnjm5Ioo+p42lktLT0ej+MbQ7doUtdQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1755511515331584.19340173649; Mon, 18 Aug 2025 03:05:15 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1unwjG-0004AK-MV; Mon, 18 Aug 2025 06:04:38 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1unwjD-00049q-K3 for qemu-devel@nongnu.org; Mon, 18 Aug 2025 06:04:35 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1unwjB-0002rq-Hu for qemu-devel@nongnu.org; Mon, 18 Aug 2025 06:04:34 -0400 Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-189-3ZZegC1bO4CYvQVjEz1PiA-1; Mon, 18 Aug 2025 06:04:29 -0400 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id D36971956096; Mon, 18 Aug 2025 10:04:27 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.225.45]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id A663B180028B; Mon, 18 Aug 2025 10:04:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755511472; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=P9I8pN3Ic6kx4QGFOkHjvVo+AEU5MXFerKiYPdyNHE0=; b=Dn6/4M20LXopSvSYzG15aYjIwF/i0lgvEcs5YfuDA59wLQhWXyK2oqis8j0YhkYZTrzRC6 ZXFHsoTMUUl5uTppgIsd78dGXvkw2U7PvT5AaAZxaGdHgyZYw68GxygTHoegBSuusPXhJv vAwOijznMhf13qLoe7K3kDAGFKvr5cg= X-MC-Unique: 3ZZegC1bO4CYvQVjEz1PiA-1 X-Mimecast-MFC-AGG-ID: 3ZZegC1bO4CYvQVjEz1PiA_1755511468 From: Albert Esteve To: qemu-devel@nongnu.org Cc: david@redhat.com, "Michael S. Tsirkin" , hi@alyssa.is, jasowang@redhat.com, Laurent Vivier , dbassey@redhat.com, Stefano Garzarella , Paolo Bonzini , stefanha@redhat.com, stevensd@chromium.org, Fabiano Rosas , =?UTF-8?q?Alex=20Benn=C3=A9e?= , slp@redhat.com, Albert Esteve Subject: [PATCH v7 4/8] vhost_user: Add frontend get_shmem_config command Date: Mon, 18 Aug 2025 12:03:49 +0200 Message-ID: <20250818100353.1560655-5-aesteve@redhat.com> In-Reply-To: <20250818100353.1560655-1-aesteve@redhat.com> References: <20250818100353.1560655-1-aesteve@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=aesteve@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=-1, RCVD_IN_MSPIKE_WL=-0.01, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1755511517736124100 Content-Type: text/plain; charset="utf-8" The frontend can use this command to retrieve VirtIO Shared Memory Regions configuration from the backend. The response contains the number of shared memory regions, their size, and shmid. This is useful when the frontend is unaware of specific backend type and configuration, for example, in the `vhost-user-device` case. Reviewed-by: Stefan Hajnoczi Signed-off-by: Albert Esteve --- hw/virtio/vhost-user.c | 43 +++++++++++++++++++++++++++++++ include/hw/virtio/vhost-backend.h | 10 +++++++ include/hw/virtio/vhost-user.h | 1 + include/hw/virtio/virtio.h | 2 ++ 4 files changed, 56 insertions(+) diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c index eb3ad728b0..d1bf162497 100644 --- a/hw/virtio/vhost-user.c +++ b/hw/virtio/vhost-user.c @@ -105,6 +105,7 @@ typedef enum VhostUserRequest { VHOST_USER_GET_SHARED_OBJECT =3D 41, VHOST_USER_SET_DEVICE_STATE_FD =3D 42, VHOST_USER_CHECK_DEVICE_STATE =3D 43, + VHOST_USER_GET_SHMEM_CONFIG =3D 44, VHOST_USER_MAX } VhostUserRequest; =20 @@ -139,6 +140,12 @@ typedef struct VhostUserMemRegMsg { VhostUserMemoryRegion region; } VhostUserMemRegMsg; =20 +typedef struct VhostUserShMemConfig { + uint32_t nregions; + uint32_t padding; + uint64_t memory_sizes[VIRTIO_MAX_SHMEM_REGIONS]; +} VhostUserShMemConfig; + typedef struct VhostUserLog { uint64_t mmap_size; uint64_t mmap_offset; @@ -245,6 +252,7 @@ typedef union { VhostUserShared object; VhostUserTransferDeviceState transfer_state; VhostUserMMap mmap; + VhostUserShMemConfig shmem; } VhostUserPayload; =20 typedef struct VhostUserMsg { @@ -3213,6 +3221,40 @@ static int vhost_user_check_device_state(struct vhos= t_dev *dev, Error **errp) return 0; } =20 +static int vhost_user_get_shmem_config(struct vhost_dev *dev, + int *nregions, + uint64_t *memory_sizes, + Error **errp) +{ + int ret; + VhostUserMsg msg =3D { + .hdr.request =3D VHOST_USER_GET_SHMEM_CONFIG, + .hdr.flags =3D VHOST_USER_VERSION, + }; + + if (!virtio_has_feature(dev->protocol_features, + VHOST_USER_PROTOCOL_F_SHMEM)) { + return 0; + } + + ret =3D vhost_user_write(dev, &msg, NULL, 0); + if (ret < 0) { + return ret; + } + + ret =3D vhost_user_read(dev, &msg); + if (ret < 0) { + return ret; + } + + assert(msg.payload.shmem.nregions <=3D VIRTIO_MAX_SHMEM_REGIONS); + *nregions =3D msg.payload.shmem.nregions; + memcpy(memory_sizes, + &msg.payload.shmem.memory_sizes, + sizeof(uint64_t) * msg.payload.shmem.nregions); + return 0; +} + const VhostOps user_ops =3D { .backend_type =3D VHOST_BACKEND_TYPE_USER, .vhost_backend_init =3D vhost_user_backend_init, @@ -3251,4 +3293,5 @@ const VhostOps user_ops =3D { .vhost_supports_device_state =3D vhost_user_supports_device_state, .vhost_set_device_state_fd =3D vhost_user_set_device_state_fd, .vhost_check_device_state =3D vhost_user_check_device_state, + .vhost_get_shmem_config =3D vhost_user_get_shmem_config, }; diff --git a/include/hw/virtio/vhost-backend.h b/include/hw/virtio/vhost-ba= ckend.h index d6df209a2f..42400b276e 100644 --- a/include/hw/virtio/vhost-backend.h +++ b/include/hw/virtio/vhost-backend.h @@ -159,6 +159,15 @@ typedef int (*vhost_set_device_state_fd_op)(struct vho= st_dev *dev, int *reply_fd, Error **errp); typedef int (*vhost_check_device_state_op)(struct vhost_dev *dev, Error **= errp); +/* + * Max regions is VIRTIO_MAX_SHMEM_REGIONS, so that is the maximum + * number of memory_sizes that will be accepted. + */ +typedef int (*vhost_get_shmem_config_op)(struct vhost_dev *dev, + int *nregions, + uint64_t *memory_sizes, + Error **errp); + =20 typedef struct VhostOps { VhostBackendType backend_type; @@ -214,6 +223,7 @@ typedef struct VhostOps { vhost_supports_device_state_op vhost_supports_device_state; vhost_set_device_state_fd_op vhost_set_device_state_fd; vhost_check_device_state_op vhost_check_device_state; + vhost_get_shmem_config_op vhost_get_shmem_config; } VhostOps; =20 int vhost_backend_update_device_iotlb(struct vhost_dev *dev, diff --git a/include/hw/virtio/vhost-user.h b/include/hw/virtio/vhost-user.h index 9a3f238b43..bacc7d184c 100644 --- a/include/hw/virtio/vhost-user.h +++ b/include/hw/virtio/vhost-user.h @@ -32,6 +32,7 @@ enum VhostUserProtocolFeature { /* Feature 17 reserved for VHOST_USER_PROTOCOL_F_XEN_MMAP. */ VHOST_USER_PROTOCOL_F_SHARED_OBJECT =3D 18, VHOST_USER_PROTOCOL_F_DEVICE_STATE =3D 19, + VHOST_USER_PROTOCOL_F_SHMEM =3D 20, VHOST_USER_PROTOCOL_F_MAX }; =20 diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h index a563bbac2c..2109186d7d 100644 --- a/include/hw/virtio/virtio.h +++ b/include/hw/virtio/virtio.h @@ -81,6 +81,8 @@ typedef struct VirtQueueElement =20 #define VIRTIO_NO_VECTOR 0xffff =20 +#define VIRTIO_MAX_SHMEM_REGIONS 256 + /* special index value used internally for config irqs */ #define VIRTIO_CONFIG_IRQ_IDX -1 =20 --=20 2.49.0 From nobody Sat Nov 15 05:33:49 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1755511552; cv=none; d=zohomail.com; s=zohoarc; b=EJf00Dk64HYpHDAnP7+Og/pWnDwBYb9GgKe3uYurcVPJl00OI2AvkxSTM+3OzcbEj5a48GES86C75MkNUN/60mwjeOy4+55TbvUdGTm0NSYPPNGyqg5Z/qtGfBfgnI3zu+Qug1Tv+Soedwty2u1kWOFMkhJsSnykVOBupaEOe6w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755511552; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=4hVX23j1qg89GM2XTJ2f9M0XbvXysR1u9GM3HK6Mb+g=; b=l2Xv8pRdBJ90fx97yWwrKDYoTDGpK9wpmjie/LrDrg0VfxzTMtXKXGryVXPRNw+YeXYM0lUNGHNjdCU7o5koVz+kuiZGuA9bfNI/hjkQ1stdbmWzGCz/eBXIpHBxdCp49ILdAO2loC/zf1ZvbF3czf7//0y41sjszN65djWrJiI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1755511552661454.8715944450546; Mon, 18 Aug 2025 03:05:52 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1unwjK-0004Ag-FV; Mon, 18 Aug 2025 06:04:42 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1unwjJ-0004AW-4S for qemu-devel@nongnu.org; Mon, 18 Aug 2025 06:04:41 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1unwjH-0002s8-Fz for qemu-devel@nongnu.org; Mon, 18 Aug 2025 06:04:40 -0400 Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-540-8i6lietbNsSNtOYsaam89w-1; Mon, 18 Aug 2025 06:04:35 -0400 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 22C6A195F165; Mon, 18 Aug 2025 10:04:34 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.225.45]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 6C5A8180047F; Mon, 18 Aug 2025 10:04:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755511478; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4hVX23j1qg89GM2XTJ2f9M0XbvXysR1u9GM3HK6Mb+g=; b=LqjVa5hdvVEVj71frxPxkt0HIoIVfT5JFwcNo8TaAglCwwS+By9VYu2SZm0k9TIvuzJYJu sfhUhQxwG0LmK/ubfhlmwBzJjgaEgTVHmj3VgYub+iMLrdxdkzA46xjIwjC5P9WEa3C++H VmFUf3VWGyCWbTOzeGz86J0prCqqlas= X-MC-Unique: 8i6lietbNsSNtOYsaam89w-1 X-Mimecast-MFC-AGG-ID: 8i6lietbNsSNtOYsaam89w_1755511474 From: Albert Esteve To: qemu-devel@nongnu.org Cc: david@redhat.com, "Michael S. Tsirkin" , hi@alyssa.is, jasowang@redhat.com, Laurent Vivier , dbassey@redhat.com, Stefano Garzarella , Paolo Bonzini , stefanha@redhat.com, stevensd@chromium.org, Fabiano Rosas , =?UTF-8?q?Alex=20Benn=C3=A9e?= , slp@redhat.com, Albert Esteve Subject: [PATCH v7 5/8] vhost_user.rst: Add GET_SHMEM_CONFIG message Date: Mon, 18 Aug 2025 12:03:50 +0200 Message-ID: <20250818100353.1560655-6-aesteve@redhat.com> In-Reply-To: <20250818100353.1560655-1-aesteve@redhat.com> References: <20250818100353.1560655-1-aesteve@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=aesteve@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=-1, RCVD_IN_MSPIKE_WL=-0.01, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1755511553829124100 Content-Type: text/plain; charset="utf-8" Add GET_SHMEM_CONFIG vhost-user frontend message to the spec documentation. Reviewed-by: Alyssa Ross Reviewed-by: Stefan Hajnoczi Signed-off-by: Albert Esteve --- docs/interop/vhost-user.rst | 39 +++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/docs/interop/vhost-user.rst b/docs/interop/vhost-user.rst index dab9f3af42..54d64ae75f 100644 --- a/docs/interop/vhost-user.rst +++ b/docs/interop/vhost-user.rst @@ -371,6 +371,20 @@ MMAP request - 0: Pages are mapped read-only - 1: Pages are mapped read-write =20 +VIRTIO Shared Memory Region configuration +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + ++-------------+---------+------------+----+--------------+ +| num regions | padding | mem size 0 | .. | mem size 255 | ++-------------+---------+------------+----+--------------+ + +:num regions: a 32-bit number of regions + +:padding: 32-bit + +:mem size: contains ``num regions`` 64-bit fields representing the size of= each + VIRTIO Shared Memory Region + C structure ----------- =20 @@ -397,6 +411,7 @@ In QEMU the vhost-user message is implemented with the = following struct: VhostUserShared object; VhostUserTransferDeviceState transfer_state; VhostUserMMap mmap; + VhostUserShMemConfig shmem; }; } QEMU_PACKED VhostUserMsg; =20 @@ -1754,6 +1769,30 @@ Front-end message types Using this function requires prior negotiation of the ``VHOST_USER_PROTOCOL_F_DEVICE_STATE`` feature. =20 +``VHOST_USER_GET_SHMEM_CONFIG`` + :id: 44 + :equivalent ioctl: N/A + :request payload: N/A + :reply payload: ``struct VhostUserShMemConfig`` + + When the ``VHOST_USER_PROTOCOL_F_SHMEM`` protocol feature has been + successfully negotiated, this message can be submitted by the front-end + to gather the VIRTIO Shared Memory Region configuration. The back-end wi= ll + respond with the number of VIRTIO Shared Memory Regions it requires, and + each shared memory region size in an array. The shared memory IDs are + represented by the array index. The information returned shall comply + with the following rules: + + * The shared information will remain valid and unchanged for the entire + lifetime of the connection. + + * The Shared Memory Region size must be a multiple of the page size + supported by mmap(2). + + * The size may be 0 if the region is unused. This can happen when the + device does not support an optional feature but does support a feature + that uses a higher shmid. + Back-end message types ---------------------- =20 --=20 2.49.0 From nobody Sat Nov 15 05:33:49 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1755511564; cv=none; d=zohomail.com; s=zohoarc; b=FxyhIvBBqL4UsiKV6jiCVyyUsY7aE9tnt2F+Hvn6H3zYNUAwRs7vCgJRm1oFnFmtdRRsvWUwzPFWgw8KmjupRaErwysTgw3y49iU9CH7iaDLwUqyT4K+/72dfAckObS0rWRbxkZqTnBC9dJdYsxOI88OEyAmLItH7puX6lB1/+I= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755511564; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=VcgyWEw0UZlhydwF41sGf+kzWAR2qTmet/4n/K4pS3U=; b=jBsv3Jia+q5eiAVm4u08m+bUYl/kGKyGHRQTLcOBIdEhp5opxKQ/OK7uBwEI4doEf0QA9iuI6lF7BJ+hWoXkHdOQmDPlFEDW1FgHYnxeJ+J+mqi05xJynQnvsO7RSUd95T2XuPcf60MNectJ2qveG3ji/e0YUCIt4MZ8RiEvrb4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1755511564719728.8677472871557; Mon, 18 Aug 2025 03:06:04 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1unwjP-0004BP-1A; Mon, 18 Aug 2025 06:04:47 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1unwjN-0004Ax-8i for qemu-devel@nongnu.org; Mon, 18 Aug 2025 06:04:45 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1unwjL-0002sI-Ez for qemu-devel@nongnu.org; Mon, 18 Aug 2025 06:04:45 -0400 Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-526-U4RiXNvvOGKFyRsKb3ZIvQ-1; Mon, 18 Aug 2025 06:04:41 -0400 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 5A8B61956086; Mon, 18 Aug 2025 10:04:40 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.225.45]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id ADDAC180047F; Mon, 18 Aug 2025 10:04:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755511482; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=VcgyWEw0UZlhydwF41sGf+kzWAR2qTmet/4n/K4pS3U=; b=VNaobZXR23TvSRZ1b1GyTVr1P8qmO8Uh4QLTztfh0cr1HKcMO3N026dPr1gIHo6mutD9TI +V+FbDXzAG4TRbqMdcehdEg0pKBy6jk4k+cqoXbt+lQJXH3XE2aJIjrIAqniwrO9k5MY5z R3sD2PHhaCewlYhMgAE5/BnDfF+CHyc= X-MC-Unique: U4RiXNvvOGKFyRsKb3ZIvQ-1 X-Mimecast-MFC-AGG-ID: U4RiXNvvOGKFyRsKb3ZIvQ_1755511480 From: Albert Esteve To: qemu-devel@nongnu.org Cc: david@redhat.com, "Michael S. Tsirkin" , hi@alyssa.is, jasowang@redhat.com, Laurent Vivier , dbassey@redhat.com, Stefano Garzarella , Paolo Bonzini , stefanha@redhat.com, stevensd@chromium.org, Fabiano Rosas , =?UTF-8?q?Alex=20Benn=C3=A9e?= , slp@redhat.com, Albert Esteve Subject: [PATCH v7 6/8] tests/qtest: Add GET_SHMEM validation test Date: Mon, 18 Aug 2025 12:03:51 +0200 Message-ID: <20250818100353.1560655-7-aesteve@redhat.com> In-Reply-To: <20250818100353.1560655-1-aesteve@redhat.com> References: <20250818100353.1560655-1-aesteve@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=aesteve@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=-1, RCVD_IN_MSPIKE_WL=-0.01, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1755511566075124100 Content-Type: text/plain; charset="utf-8" Improve vhost-user-test to properly validate VHOST_USER_GET_SHMEM_CONFIG message handling by directly simulating the message exchange. The test manually triggers the VHOST_USER_GET_SHMEM_CONFIG message by calling chr_read() with a crafted VhostUserMsg, allowing direct validation of the shmem configuration response handler. Added TestServerShmem structure to track shmem configuration state, including nregions_sent and sizes_sent arrays for comprehensive validation. The test verifies that the response contains the expected number of shared memory regions and their corresponding sizes. Signed-off-by: Albert Esteve --- tests/qtest/vhost-user-test.c | 91 +++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/tests/qtest/vhost-user-test.c b/tests/qtest/vhost-user-test.c index 75cb3e44b2..44a5e90b2e 100644 --- a/tests/qtest/vhost-user-test.c +++ b/tests/qtest/vhost-user-test.c @@ -88,6 +88,7 @@ typedef enum VhostUserRequest { VHOST_USER_SET_VRING_ENABLE =3D 18, VHOST_USER_GET_CONFIG =3D 24, VHOST_USER_SET_CONFIG =3D 25, + VHOST_USER_GET_SHMEM_CONFIG =3D 44, VHOST_USER_MAX } VhostUserRequest; =20 @@ -109,6 +110,20 @@ typedef struct VhostUserLog { uint64_t mmap_offset; } VhostUserLog; =20 +#define VIRTIO_MAX_SHMEM_REGIONS 256 + +typedef struct VhostUserShMemConfig { + uint32_t nregions; + uint32_t padding; + uint64_t memory_sizes[VIRTIO_MAX_SHMEM_REGIONS]; +} VhostUserShMemConfig; + +typedef struct TestServerShmem { + bool test_enabled; + uint32_t nregions_sent; + uint64_t sizes_sent[VIRTIO_MAX_SHMEM_REGIONS]; +} TestServerShmem; + typedef struct VhostUserMsg { VhostUserRequest request; =20 @@ -124,6 +139,7 @@ typedef struct VhostUserMsg { struct vhost_vring_addr addr; VhostUserMemory memory; VhostUserLog log; + VhostUserShMemConfig shmem; } payload; } QEMU_PACKED VhostUserMsg; =20 @@ -170,6 +186,7 @@ typedef struct TestServer { bool test_fail; int test_flags; int queues; + TestServerShmem shmem; struct vhost_user_ops *vu_ops; } TestServer; =20 @@ -513,6 +530,31 @@ static void chr_read(void *opaque, const uint8_t *buf,= int size) qos_printf("set_vring(%d)=3D%s\n", msg.payload.state.index, msg.payload.state.num ? "enabled" : "disabled"); break; + =20 + case VHOST_USER_GET_SHMEM_CONFIG: + if (!s->shmem.test_enabled) { + /* Reply with error if shmem feature not enabled */ + msg.flags |=3D VHOST_USER_REPLY_MASK; + msg.size =3D sizeof(uint64_t); + msg.payload.u64 =3D -1; /* Error */ + qemu_chr_fe_write_all(chr, (uint8_t *) &msg, VHOST_USER_HDR_SI= ZE + msg.size); + } else { + /* Reply with test shmem configuration */ + msg.flags |=3D VHOST_USER_REPLY_MASK; + msg.size =3D sizeof(VhostUserShMemConfig); + msg.payload.shmem.nregions =3D 2; /* Test with 2 regions */ + msg.payload.shmem.padding =3D 0; + msg.payload.shmem.memory_sizes[0] =3D 0x100000; /* 1MB */ + msg.payload.shmem.memory_sizes[1] =3D 0x200000; /* 2MB */ + =20 + /* Record what we're sending for test validation */ + s->shmem.nregions_sent =3D msg.payload.shmem.nregions; + s->shmem.sizes_sent[0] =3D msg.payload.shmem.memory_sizes[0]; + s->shmem.sizes_sent[1] =3D msg.payload.shmem.memory_sizes[1]; + =20 + qemu_chr_fe_write_all(chr, (uint8_t *) &msg, VHOST_USER_HDR_SI= ZE + msg.size); + } + break; =20 default: qos_printf("vhost-user: un-handled message: %d\n", msg.request); @@ -809,6 +851,22 @@ static void *vhost_user_test_setup_shm(GString *cmd_li= ne, void *arg) return server; } =20 +static void *vhost_user_test_setup_shmem_config(GString *cmd_line, void *a= rg) +{ + TestServer *server =3D test_server_new("vhost-user-test", arg); + test_server_listen(server); + + /* Enable shmem testing for this server */ + server->shmem.test_enabled =3D true; + + append_mem_opts(server, cmd_line, 256, TEST_MEMFD_SHM); + server->vu_ops->append_opts(server, cmd_line, ""); + + g_test_queue_destroy(vhost_user_test_cleanup, server); + + return server; +} + static void test_read_guest_mem(void *obj, void *arg, QGuestAllocator *all= oc) { TestServer *server =3D arg; @@ -1089,6 +1147,33 @@ static struct vhost_user_ops g_vu_net_ops =3D { .get_protocol_features =3D vu_net_get_protocol_features, }; =20 +/* Test function for VHOST_USER_GET_SHMEM_CONFIG message */ +static void test_shmem_config(void *obj, void *arg, QGuestAllocator *alloc) +{ + TestServer *s =3D arg; + =20 + g_assert_true(s->shmem.test_enabled); + =20 + g_mutex_lock(&s->data_mutex); + s->shmem.nregions_sent =3D 0; + s->shmem.sizes_sent[0] =3D 0; + s->shmem.sizes_sent[1] =3D 0; + g_mutex_unlock(&s->data_mutex); + =20 + VhostUserMsg msg =3D { + .request =3D VHOST_USER_GET_SHMEM_CONFIG, + .flags =3D VHOST_USER_VERSION, + .size =3D 0, + }; + chr_read(s, (uint8_t *) &msg, VHOST_USER_HDR_SIZE); + + g_mutex_lock(&s->data_mutex); + g_assert_cmpint(s->shmem.nregions_sent, =3D=3D, 2); + g_assert_cmpint(s->shmem.sizes_sent[0], =3D=3D, 0x100000); /* 1MB */ + g_assert_cmpint(s->shmem.sizes_sent[1], =3D=3D, 0x200000); /* 2MB */ + g_mutex_unlock(&s->data_mutex); +} + static void register_vhost_user_test(void) { QOSGraphTestOptions opts =3D { @@ -1136,6 +1221,12 @@ static void register_vhost_user_test(void) qos_add_test("vhost-user/multiqueue", "virtio-net", test_multiqueue, &opts); + =20 + opts.before =3D vhost_user_test_setup_shmem_config; + opts.edge.extra_device_opts =3D ""; + qos_add_test("vhost-user/shmem-config", + "virtio-net", + test_shmem_config, &opts); } libqos_init(register_vhost_user_test); =20 --=20 2.49.0 From nobody Sat Nov 15 05:33:49 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1755511544; cv=none; d=zohomail.com; s=zohoarc; b=KbAiqNvLrAn4EDHX9FB6cLlKQOQpTuBTMwpSZ0ktdMYJ6SFSuDvKvI7PDAhw98ZMF6vSme9cBFk/x8Ko76bxgNwJS3QFcZyNTql4x666o95EpwTaWwZwHJNDwEX0ENYIG/eVXGeik8z8qgpNNtZT5q09yncoXQcV/+WYY79PpbI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755511544; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=FXG6nBRyssLRsnDK38URuLwEwpEb4ieR4NV7wjgJN8Q=; b=hJGEBdieLr62E2357fOC2pMPJNhQE03WZz6f4YFhWvoU3DYLEJGfmxbZqGPBN6q9Kvwwt/Vf0AXg3bStrY4iVVlrNG41WOXPp5YX3QXeJxHoSGDIArI+ZJ85+Cz4Sj1y9lB3dmRNLuTogWOC4erHf21MDv3mNN2ig7gpSpUOOHg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1755511544848767.8040137607354; Mon, 18 Aug 2025 03:05:44 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1unwjW-0004Bw-Hg; Mon, 18 Aug 2025 06:04:54 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1unwjV-0004Bk-2z for qemu-devel@nongnu.org; Mon, 18 Aug 2025 06:04:53 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1unwjT-0002st-NZ for qemu-devel@nongnu.org; Mon, 18 Aug 2025 06:04:52 -0400 Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-323-2BNfWbfzMRCLuu5hRb7BpQ-1; Mon, 18 Aug 2025 06:04:47 -0400 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 643831800289; Mon, 18 Aug 2025 10:04:46 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.225.45]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id E07B01800280; Mon, 18 Aug 2025 10:04:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755511491; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=FXG6nBRyssLRsnDK38URuLwEwpEb4ieR4NV7wjgJN8Q=; b=d6cHodtRhWnKCFKv2s51Xu8/+Zya78TJOwppvHca0Xv3LcCRebC3hE3gdHYGRmLf/umXDJ IwE/mz32Rk8AxzDbR2BrhCRQg8PsBBk8DfZcrsvLVhk344V5xI0FvtFqfvYgYg/W5VMaZd TEeOsYW5CPUkEd2VS76R93cXdolg1ik= X-MC-Unique: 2BNfWbfzMRCLuu5hRb7BpQ-1 X-Mimecast-MFC-AGG-ID: 2BNfWbfzMRCLuu5hRb7BpQ_1755511486 From: Albert Esteve To: qemu-devel@nongnu.org Cc: david@redhat.com, "Michael S. Tsirkin" , hi@alyssa.is, jasowang@redhat.com, Laurent Vivier , dbassey@redhat.com, Stefano Garzarella , Paolo Bonzini , stefanha@redhat.com, stevensd@chromium.org, Fabiano Rosas , =?UTF-8?q?Alex=20Benn=C3=A9e?= , slp@redhat.com, Albert Esteve Subject: [PATCH v7 7/8] qmp: add shmem feature map Date: Mon, 18 Aug 2025 12:03:52 +0200 Message-ID: <20250818100353.1560655-8-aesteve@redhat.com> In-Reply-To: <20250818100353.1560655-1-aesteve@redhat.com> References: <20250818100353.1560655-1-aesteve@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=aesteve@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=-1, RCVD_IN_MSPIKE_WL=-0.01, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1755511545612116600 Content-Type: text/plain; charset="utf-8" Add new vhost-user protocol VHOST_USER_PROTOCOL_F_SHMEM feature to feature map. Reviewed-by: Stefan Hajnoczi Signed-off-by: Albert Esteve --- hw/virtio/virtio-qmp.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/hw/virtio/virtio-qmp.c b/hw/virtio/virtio-qmp.c index 3b6377cf0d..8c2cfd0916 100644 --- a/hw/virtio/virtio-qmp.c +++ b/hw/virtio/virtio-qmp.c @@ -127,6 +127,9 @@ static const qmp_virtio_feature_map_t vhost_user_protoc= ol_map[] =3D { FEATURE_ENTRY(VHOST_USER_PROTOCOL_F_DEVICE_STATE, \ "VHOST_USER_PROTOCOL_F_DEVICE_STATE: Backend device state tran= sfer " "supported"), + FEATURE_ENTRY(VHOST_USER_PROTOCOL_F_SHMEM, \ + "VHOST_USER_PROTOCOL_F_SHMEM: Backend shared memory mappin= g " + "supported"), { -1, "" } }; =20 --=20 2.49.0 From nobody Sat Nov 15 05:33:49 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1755511529; cv=none; d=zohomail.com; s=zohoarc; b=ACBF4U1uidXFcMLNIy5Ooq/InnMfgmHE0VIOViDKF7RVuOvGu6HHcYF71o5gbzPDCGZvXsYF/EXc07Q4FWPrw6UuF7YljxqtLM19BpgsGMPd0G5w+5ROHos0aoXgMJCjt4gakIej1M3Qf45TaFqbfyO9j3j44hDZKRzP8nRlkAI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755511529; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=ydA5+q4d6HWwx0EkrM5TjW/EQBVY8+I+lJ5UM8QCE3g=; b=YsQGYdIsEbg5hP03MhPsp8GMt/8CcmSNtcuONGVnTfGDvyBoDamqDdYz9FMUW6uR9maBYHo4/ZFPfSMx85JzU9GjQkvaf/Ur+aQnZW3GsevVe2mMu0Gu/nlISWeurFVwQSgI2gCiVKwcchmjiJT8Vbm++tBUsHug9venIsfyNoI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1755511529369214.68309946750048; Mon, 18 Aug 2025 03:05:29 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1unwje-0004Ch-A1; Mon, 18 Aug 2025 06:05:02 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1unwjc-0004CS-JV for qemu-devel@nongnu.org; Mon, 18 Aug 2025 06:05:00 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1unwja-0002tC-Ta for qemu-devel@nongnu.org; Mon, 18 Aug 2025 06:05:00 -0400 Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-325-8-3RfKDmNB6Dz-ypzEKLOA-1; Mon, 18 Aug 2025 06:04:53 -0400 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 4E852180034A; Mon, 18 Aug 2025 10:04:52 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.225.45]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id EEEAE180047F; Mon, 18 Aug 2025 10:04:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755511498; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ydA5+q4d6HWwx0EkrM5TjW/EQBVY8+I+lJ5UM8QCE3g=; b=TNLYJW5Vf0x2ccCj2swZKJIvORnIKr8C5CVG7uv6bMwCEXw29+PnQnmP0h+K0gvQeDcmoq HoKV/OviciBCP+KLEodfzfWwRd/vtDkYeibxa2xJNhWxur6usLJblgNVLA33eZp/+mLpP+ s9jQVLWrKAECLuNf6aIoiAG2cRebZU0= X-MC-Unique: 8-3RfKDmNB6Dz-ypzEKLOA-1 X-Mimecast-MFC-AGG-ID: 8-3RfKDmNB6Dz-ypzEKLOA_1755511492 From: Albert Esteve To: qemu-devel@nongnu.org Cc: david@redhat.com, "Michael S. Tsirkin" , hi@alyssa.is, jasowang@redhat.com, Laurent Vivier , dbassey@redhat.com, Stefano Garzarella , Paolo Bonzini , stefanha@redhat.com, stevensd@chromium.org, Fabiano Rosas , =?UTF-8?q?Alex=20Benn=C3=A9e?= , slp@redhat.com, Albert Esteve Subject: [PATCH v7 8/8] vhost-user-device: Add shared memory BAR Date: Mon, 18 Aug 2025 12:03:53 +0200 Message-ID: <20250818100353.1560655-9-aesteve@redhat.com> In-Reply-To: <20250818100353.1560655-1-aesteve@redhat.com> References: <20250818100353.1560655-1-aesteve@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=aesteve@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=-1, RCVD_IN_MSPIKE_WL=-0.01, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1755511531528116600 Content-Type: text/plain; charset="utf-8" Add shared memory BAR support to vhost-user-device-pci to enable direct file mapping for VIRTIO Shared Memory Regions. The implementation creates a consolidated shared memory BAR that contains all VIRTIO Shared Memory Regions as subregions. Each region is configured with its proper shmid, size, and offset within the BAR. The number and size of regions are retrieved via VHOST_USER_GET_SHMEM_CONFIG message sent by vhost-user-base during realization after virtio_init(). Specifiically, it uses BAR 3 to avoid conflicts, as it is currently unused. The shared memory BAR is only created when the backend supports VHOST_USER_PROTOCOL_F_SHMEM and has configured shared memory regions. This maintains backward compatibility with backends that do not support shared memory functionality. Signed-off-by: Albert Esteve --- hw/virtio/vhost-user-base.c | 49 +++++++++++++++++++++++++++++-- hw/virtio/vhost-user-device-pci.c | 34 +++++++++++++++++++-- 2 files changed, 78 insertions(+), 5 deletions(-) diff --git a/hw/virtio/vhost-user-base.c b/hw/virtio/vhost-user-base.c index ff67a020b4..932f9b5596 100644 --- a/hw/virtio/vhost-user-base.c +++ b/hw/virtio/vhost-user-base.c @@ -16,6 +16,7 @@ #include "hw/virtio/virtio-bus.h" #include "hw/virtio/vhost-user-base.h" #include "qemu/error-report.h" +#include "migration/blocker.h" =20 static void vub_start(VirtIODevice *vdev) { @@ -276,7 +277,9 @@ static void vub_device_realize(DeviceState *dev, Error = **errp) { VirtIODevice *vdev =3D VIRTIO_DEVICE(dev); VHostUserBase *vub =3D VHOST_USER_BASE(dev); - int ret; + uint64_t memory_sizes[VIRTIO_MAX_SHMEM_REGIONS]; + g_autofree char *name =3D NULL; + int i, ret, nregions; =20 if (!vub->chardev.chr) { error_setg(errp, "vhost-user-base: missing chardev"); @@ -319,7 +322,7 @@ static void vub_device_realize(DeviceState *dev, Error = **errp) =20 /* Allocate queues */ vub->vqs =3D g_ptr_array_sized_new(vub->num_vqs); - for (int i =3D 0; i < vub->num_vqs; i++) { + for (i =3D 0; i < vub->num_vqs; i++) { g_ptr_array_add(vub->vqs, virtio_add_queue(vdev, vub->vq_size, vub_handle_output)); @@ -333,11 +336,51 @@ static void vub_device_realize(DeviceState *dev, Erro= r **errp) VHOST_BACKEND_TYPE_USER, 0, errp); =20 if (ret < 0) { - do_vhost_user_cleanup(vdev, vub); + goto err; + } + + ret =3D vub->vhost_dev.vhost_ops->vhost_get_shmem_config(&vub->vhost_d= ev, + &nregions, + memory_sizes, + errp); + + if (ret < 0) { + goto err; + } + + for (i =3D 0; i < nregions; i++) { + if (memory_sizes[i]) { + if (vub->vhost_dev.migration_blocker =3D=3D NULL) { + error_setg(&vub->vhost_dev.migration_blocker, + "Migration disabled: devices with VIRTIO Shared Mem= ory " + "Regions do not support migration yet."); + ret =3D migrate_add_blocker_normal( + &vub->vhost_dev.migration_blocker, + errp); + + if (ret < 0) { + goto err; + } + } + + if (memory_sizes[i] % qemu_real_host_page_size() !=3D 0) { + error_setg(errp, "Shared memory %d size must be a power of= 2 " + "no smaller than the page size", i); + goto err; + } + + name =3D g_strdup_printf("vub-shm-%d", i); + memory_region_init(&virtio_new_shmem_region(vdev, i)->mr, + OBJECT(vdev), name, + memory_sizes[i]); + } } =20 qemu_chr_fe_set_handlers(&vub->chardev, NULL, NULL, vub_event, NULL, dev, NULL, true); + return; +err: + do_vhost_user_cleanup(vdev, vub); } =20 static void vub_device_unrealize(DeviceState *dev) diff --git a/hw/virtio/vhost-user-device-pci.c b/hw/virtio/vhost-user-devic= e-pci.c index f10bac874e..bac99e7c60 100644 --- a/hw/virtio/vhost-user-device-pci.c +++ b/hw/virtio/vhost-user-device-pci.c @@ -8,14 +8,18 @@ */ =20 #include "qemu/osdep.h" +#include "qapi/error.h" #include "hw/qdev-properties.h" #include "hw/virtio/vhost-user-base.h" #include "hw/virtio/virtio-pci.h" =20 +#define VIRTIO_DEVICE_PCI_SHMEM_BAR 3 + struct VHostUserDevicePCI { VirtIOPCIProxy parent_obj; =20 VHostUserBase vub; + MemoryRegion shmembar; }; =20 #define TYPE_VHOST_USER_DEVICE_PCI "vhost-user-device-pci-base" @@ -25,10 +29,36 @@ OBJECT_DECLARE_SIMPLE_TYPE(VHostUserDevicePCI, VHOST_US= ER_DEVICE_PCI) static void vhost_user_device_pci_realize(VirtIOPCIProxy *vpci_dev, Error = **errp) { VHostUserDevicePCI *dev =3D VHOST_USER_DEVICE_PCI(vpci_dev); - DeviceState *vdev =3D DEVICE(&dev->vub); + DeviceState *dev_state =3D DEVICE(&dev->vub); + VirtIODevice *vdev =3D VIRTIO_DEVICE(dev_state); + VirtioSharedMemory *shmem, *next; + uint64_t offset =3D 0, shmem_size =3D 0; =20 vpci_dev->nvectors =3D 1; - qdev_realize(vdev, BUS(&vpci_dev->bus), errp); + qdev_realize(dev_state, BUS(&vpci_dev->bus), errp); + + QSIMPLEQ_FOREACH_SAFE(shmem, &vdev->shmem_list, entry, next) { + if (shmem->mr.size > UINT64_MAX - shmem_size) { + error_setg(errp, "Total shared memory required overflow"); + return; + } + shmem_size =3D shmem_size + shmem->mr.size; + } + if (shmem_size) { + memory_region_init(&dev->shmembar, OBJECT(vpci_dev), + "vhost-device-pci-shmembar", shmem_size); + QSIMPLEQ_FOREACH_SAFE(shmem, &vdev->shmem_list, entry, next) { + memory_region_add_subregion(&dev->shmembar, offset, &shmem->mr= ); + virtio_pci_add_shm_cap(vpci_dev, VIRTIO_DEVICE_PCI_SHMEM_BAR, + offset, shmem->mr.size, shmem->shmid); + offset =3D offset + shmem->mr.size; + } + pci_register_bar(&vpci_dev->pci_dev, VIRTIO_DEVICE_PCI_SHMEM_BAR, + PCI_BASE_ADDRESS_SPACE_MEMORY | + PCI_BASE_ADDRESS_MEM_PREFETCH | + PCI_BASE_ADDRESS_MEM_TYPE_64, + &dev->shmembar); + } } =20 static void vhost_user_device_pci_class_init(ObjectClass *klass, --=20 2.49.0