From nobody Sat Oct 25 19:04:45 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=1760625689; cv=none; d=zohomail.com; s=zohoarc; b=hh8CQBa3BU179JdWxgzD4/wsKFVkOdq5cBcPDchZBPX6EuOCWkMcOPpgDkw6jPGvuBesESa4HWfSkztOCE20YAU9AF8SKDzjxIm93qqenvfe0ANa5DOAhw4khNYkr1I7/9XBt97TqbIgXWxcGXQ9ltfzDBhwNdJDGKZdYYrNSks= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1760625689; 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=gMuex8nJBNN5nuaYcZQNb6TWUFSQWDWPvyA91oXmmbE=; b=VIkU+Z7h2naQJQ16SGhQ/WBxbRRktWX4QNw+LmaC4Me9xLvlv2e5wOypiQWLfCkOJ71nGgexWy3Ib2TTwqtiB6AoPvsC/ckWV5VVhLHvHQxhe5+NWLgAPuSRE0rtN8+GIWVoqcmoRsrVtJcwTlKBBs/WvxdXpuvWLcmExwrx1GE= 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 1760625689827228.69568610094564; Thu, 16 Oct 2025 07:41:29 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1v9P88-0008Fw-A1; Thu, 16 Oct 2025 10:39:00 -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 1v9P84-0008ES-6p for qemu-devel@nongnu.org; Thu, 16 Oct 2025 10:38:57 -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 1v9P7x-0005Nf-61 for qemu-devel@nongnu.org; Thu, 16 Oct 2025 10:38:55 -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-330-xXxJjopFNZCiyaqLe70E7Q-1; Thu, 16 Oct 2025 10:38:43 -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 B7B601956094; Thu, 16 Oct 2025 14:38:41 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.225.164]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 151481800451; Thu, 16 Oct 2025 14:38:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1760625526; 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=gMuex8nJBNN5nuaYcZQNb6TWUFSQWDWPvyA91oXmmbE=; b=KUggHIitqTUbFCR8znbag4IZVy/anAGPynEzOlSTkdlLZ7fWEAA/K1I0RebeUVZOxqpBgu mZhWF/xfn/lXmyScJ/exgvYYmuDyRga3Pw2Syk8RfKL2FPoCa6GjC1iIKYcexTZy+/9kIe VUduvr//DlQaUdT2gJa/FS1DTRqwnzw= X-MC-Unique: xXxJjopFNZCiyaqLe70E7Q-1 X-Mimecast-MFC-AGG-ID: xXxJjopFNZCiyaqLe70E7Q_1760625522 From: Albert Esteve To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , hi@alyssa.is, stefanha@redhat.com, david@redhat.com, jasowang@redhat.com, dbassey@redhat.com, stevensd@chromium.org, Stefano Garzarella , Laurent Vivier , "Michael S. Tsirkin" , Paolo Bonzini , Fabiano Rosas , slp@redhat.com, manos.pitsidianakis@linaro.org, Albert Esteve Subject: [PATCH v10 1/7] vhost-user: Add VirtIO Shared Memory map request Date: Thu, 16 Oct 2025 16:38:21 +0200 Message-ID: <20251016143827.1850397-2-aesteve@redhat.com> In-Reply-To: <20251016143827.1850397-1-aesteve@redhat.com> References: <20251016143827.1850397-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: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.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_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_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: 1760625691097154100 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 VirtioSharedMemoryMapping as a unified QOM object that manages both the mapping metadata and MemoryRegion lifecycle. This object provides reference-counted lifecycle management with automatic cleanup of file descriptors and memory regions through QOM finalization. This request allows backends to dynamically map file descriptors into a VIRTIO Shared Memory Region identified by their shmid. Maps are created using memory_region_init_ram_from_fd() 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 VirtioSharedMemoryMapping object, 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 reply to the backend to avoid deadlocks. Otherwise, the MemoryListener would send a VHOST_USER_SET_MEM_TABLE message before the reply. Reviewed-by: Stefan Hajnoczi Signed-off-by: Albert Esteve Acked-by: Stefano Garzarella --- hw/virtio/vhost-user.c | 267 ++++++++++++++++++++++ hw/virtio/virtio.c | 199 ++++++++++++++++ include/hw/virtio/virtio.h | 135 +++++++++++ subprojects/libvhost-user/libvhost-user.c | 70 ++++++ subprojects/libvhost-user/libvhost-user.h | 54 +++++ 5 files changed, 725 insertions(+) diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c index 36c9c2e04d..890be55937 100644 --- a/hw/virtio/vhost-user.c +++ b/hw/virtio/vhost-user.c @@ -104,6 +104,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 @@ -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 @@ -136,6 +139,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; @@ -192,6 +201,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_* */ + uint64_t flags; +} VhostUserMMap; + typedef struct { VhostUserRequest request; =20 @@ -224,6 +250,8 @@ typedef union { VhostUserInflight inflight; VhostUserShared object; VhostUserTransferDeviceState transfer_state; + VhostUserMMap mmap; + VhostUserShMemConfig shmem; } VhostUserPayload; =20 typedef struct VhostUserMsg { @@ -1768,6 +1796,196 @@ 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; + VirtioSharedMemoryMapping *existing; + Error *local_err =3D NULL; + int ret =3D 0; + + if (fd < 0) { + error_report("Bad fd for map"); + ret =3D -EBADF; + goto send_reply; + } + + if (QSIMPLEQ_EMPTY(&dev->vdev->shmem_list)) { + error_report("Device has no VIRTIO Shared Memory Regions. " + "Requested ID: %d", vu_mmap->shmid); + ret =3D -EFAULT; + goto send_reply; + } + + 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 uninitialized", vu_mmap->shmid); + ret =3D -EFAULT; + goto send_reply; + } + + 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); + ret =3D -EFAULT; + goto send_reply; + } + + QTAILQ_FOREACH(existing, &shmem->mmaps, link) { + if (ranges_overlap(existing->offset, existing->len, + vu_mmap->shm_offset, vu_mmap->len)) { + error_report("VIRTIO Shared Memory mapping overlap"); + ret =3D -EFAULT; + goto send_reply; + } + } + + memory_region_transaction_begin(); + + /* Create VirtioSharedMemoryMapping object */ + VirtioSharedMemoryMapping *mapping =3D virtio_shared_memory_mapping_ne= w( + vu_mmap->shmid, fd, vu_mmap->fd_offset, vu_mmap->shm_offset, + vu_mmap->len, vu_mmap->flags & VHOST_USER_FLAG_MAP_RW); + + if (!mapping) { + ret =3D -EFAULT; + goto send_reply_commit; + } + + /* Add the mapping to the shared memory region */ + if (virtio_add_shmem_map(shmem, mapping) !=3D 0) { + error_report("Failed to add shared memory mapping"); + object_unref(OBJECT(mapping)); + ret =3D -EFAULT; + goto send_reply_commit; + } + +send_reply_commit: + /* Send reply and commit after transaction started */ + if (hdr->flags & VHOST_USER_NEED_REPLY_MASK) { + payload->u64 =3D !!ret; + hdr->size =3D sizeof(payload->u64); + if (!vhost_user_send_resp(ioc, hdr, payload, &local_err)) { + error_report_err(local_err); + memory_region_transaction_commit(); + return -EFAULT; + } + } + memory_region_transaction_commit(); + return 0; + +send_reply: + if (hdr->flags & VHOST_USER_NEED_REPLY_MASK) { + payload->u64 =3D !!ret; + hdr->size =3D sizeof(payload->u64); + if (!vhost_user_send_resp(ioc, hdr, payload, &local_err)) { + error_report_err(local_err); + return -EFAULT; + } + } + 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; + int ret =3D 0; + + if (QSIMPLEQ_EMPTY(&dev->vdev->shmem_list)) { + error_report("Device has no VIRTIO Shared Memory Regions. " + "Requested ID: %d", vu_mmap->shmid); + ret =3D -EFAULT; + goto send_reply; + } + + 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 uninitialized", vu_mmap->shmid); + ret =3D -EFAULT; + goto send_reply; + } + + 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); + ret =3D -EFAULT; + goto send_reply; + } + + 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); + ret =3D -EFAULT; + goto send_reply; + } + +send_reply: + if (hdr->flags & VHOST_USER_NEED_REPLY_MASK) { + payload->u64 =3D !!ret; + hdr->size =3D sizeof(payload->u64); + if (!vhost_user_send_resp(ioc, hdr, payload, &local_err)) { + error_report_err(local_err); + return -EFAULT; + } + } + + if (!ret && mmap) { + /* Free the MemoryRegion only after reply */ + 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); @@ -1836,6 +2054,19 @@ static gboolean backend_read(QIOChannel *ioc, GIOCon= dition condition, ret =3D vhost_user_backend_handle_shared_object_lookup(dev->opaque= , ioc, &hdr, &payloa= d); 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); ret =3D -EINVAL; @@ -3013,6 +3244,41 @@ 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)) { + *nregions =3D 0; + 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) * VIRTIO_MAX_SHMEM_REGIONS); + return 0; +} + const VhostOps user_ops =3D { .backend_type =3D VHOST_BACKEND_TYPE_USER, .vhost_backend_init =3D vhost_user_backend_init, @@ -3051,4 +3317,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/hw/virtio/virtio.c b/hw/virtio/virtio.c index 153ee0a0cf..f96ed43c18 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -3086,6 +3086,173 @@ int virtio_save(VirtIODevice *vdev, QEMUFile *f) return vmstate_save_state(f, &vmstate_virtio, vdev, NULL, &error_fatal= ); } =20 +VirtioSharedMemory *virtio_new_shmem_region(VirtIODevice *vdev, uint8_t sh= mid, uint64_t size) +{ + 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, size); + QTAILQ_INIT(&elem->mmaps); + QSIMPLEQ_INSERT_TAIL(&vdev->shmem_list, elem, entry); + return elem; +} + +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; +} + +static void virtio_shared_memory_mapping_instance_init(Object *obj) +{ + VirtioSharedMemoryMapping *mapping =3D VIRTIO_SHARED_MEMORY_MAPPING(ob= j); + + mapping->shmid =3D 0; + mapping->offset =3D 0; + mapping->len =3D 0; + mapping->mr =3D NULL; +} + +static void virtio_shared_memory_mapping_instance_finalize(Object *obj) +{ + VirtioSharedMemoryMapping *mapping =3D VIRTIO_SHARED_MEMORY_MAPPING(ob= j); + + /* Clean up MemoryRegion if it exists */ + if (mapping->mr) { + /* Unparent the MemoryRegion to trigger cleanup */ + object_unparent(OBJECT(mapping->mr)); + mapping->mr =3D NULL; + } +} + +VirtioSharedMemoryMapping *virtio_shared_memory_mapping_new(uint8_t shmid, + int fd, + uint64_t fd_of= fset, + uint64_t shm_o= ffset, + uint64_t len, + bool allow_wri= te) +{ + VirtioSharedMemoryMapping *mapping; + 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 (!allow_write) { + ram_flags |=3D RAM_READONLY_FD; + } + + /* Create the VirtioSharedMemoryMapping */ + mapping =3D VIRTIO_SHARED_MEMORY_MAPPING( + object_new(TYPE_VIRTIO_SHARED_MEMORY_MAPPING)); + + /* Set up object properties */ + mapping->shmid =3D shmid; + mapping->offset =3D shm_offset; + mapping->len =3D len; + + /* Create MemoryRegion as a child of this object */ + mr =3D g_new0(MemoryRegion, 1); + g_string_printf(mr_name, "virtio-shmem-%d-%" PRIx64, shmid, shm_offset= ); + + /* Initialize MemoryRegion with file descriptor */ + if (!memory_region_init_ram_from_fd(mr, OBJECT(mapping), mr_name->str, + len, ram_flags, fd, fd_offset, + &local_err)) { + error_report_err(local_err); + g_free(mr); + close(fd); + object_unref(OBJECT(mapping)); + return NULL; + } + + mapping->mr =3D mr; + return mapping; +} + +int virtio_add_shmem_map(VirtioSharedMemory *shmem, + VirtioSharedMemoryMapping *mapping) +{ + if (!mapping) { + error_report("VirtioSharedMemoryMapping cannot be NULL"); + return -1; + } + if (!mapping->mr) { + error_report("VirtioSharedMemoryMapping has no MemoryRegion"); + return -1; + } + + /* Validate boundaries against the VIRTIO shared memory region */ + if (mapping->offset + mapping->len > shmem->mr.size) { + error_report("Memory exceeds the shared memory boundaries"); + return -1; + } + + /* Add as subregion to the VIRTIO shared memory */ + memory_region_add_subregion(&shmem->mr, mapping->offset, mapping->mr); + + /* Add to the mapped regions list */ + QTAILQ_INSERT_TAIL(&shmem->mmaps, mapping, link); + + return 0; +} + +VirtioSharedMemoryMapping *virtio_find_shmem_map(VirtioSharedMemory *shmem, + hwaddr offset, uint64_t size) +{ + VirtioSharedMemoryMapping *mapping; + QTAILQ_FOREACH(mapping, &shmem->mmaps, link) { + if (mapping->offset =3D=3D offset && mapping->mr->size =3D=3D size= ) { + return mapping; + } + } + return NULL; +} + +void virtio_del_shmem_map(VirtioSharedMemory *shmem, hwaddr offset, + uint64_t size) +{ + VirtioSharedMemoryMapping *mapping =3D virtio_find_shmem_map(shmem, of= fset, size); + if (mapping =3D=3D NULL) { + return; + } + + /* + * Remove from memory region first + */ + memory_region_del_subregion(&shmem->mr, mapping->mr); + + /* + * Remove from list and unref the mapping which will trigger automatic= cleanup + * when the reference count reaches zero. + */ + QTAILQ_REMOVE(&shmem->mmaps, mapping, link); + object_unref(OBJECT(mapping)); +} + /* 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) @@ -3212,6 +3379,7 @@ void virtio_reset(void *opaque) { VirtIODevice *vdev =3D opaque; VirtioDeviceClass *k =3D VIRTIO_DEVICE_GET_CLASS(vdev); + VirtioSharedMemory *shmem; uint64_t features[VIRTIO_FEATURES_NU64S]; int i; =20 @@ -3251,6 +3419,14 @@ void virtio_reset(void *opaque) for (i =3D 0; i < VIRTIO_QUEUE_MAX; i++) { __virtio_queue_reset(vdev, i); } + + /* Mappings are removed to prevent stale fds from remaining open. */ + QSIMPLEQ_FOREACH(shmem, &vdev->shmem_list, entry) { + while (!QTAILQ_EMPTY(&shmem->mmaps)) { + VirtioSharedMemoryMapping *mapping =3D QTAILQ_FIRST(&shmem->mm= aps); + virtio_del_shmem_map(shmem, mapping->offset, mapping->mr->size= ); + } + } } =20 static void virtio_device_check_notification_compatibility(VirtIODevice *v= dev, @@ -3574,6 +3750,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 /* @@ -4085,11 +4262,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 *mapping =3D QTAILQ_FIRST(&shmem->mm= aps); + virtio_del_shmem_map(shmem, mapping->offset, mapping->mr->size= ); + } + + /* 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 { @@ -4455,9 +4645,18 @@ static const TypeInfo virtio_device_info =3D { .class_size =3D sizeof(VirtioDeviceClass), }; =20 +static const TypeInfo virtio_shared_memory_mapping_info =3D { + .name =3D TYPE_VIRTIO_SHARED_MEMORY_MAPPING, + .parent =3D TYPE_OBJECT, + .instance_size =3D sizeof(VirtioSharedMemoryMapping), + .instance_init =3D virtio_shared_memory_mapping_instance_init, + .instance_finalize =3D virtio_shared_memory_mapping_instance_finalize, +}; + static void virtio_register_types(void) { type_register_static(&virtio_device_info); + type_register_static(&virtio_shared_memory_mapping_info); } =20 type_init(virtio_register_types) diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h index d97529c3f1..3f6dfba321 100644 --- a/include/hw/virtio/virtio.h +++ b/include/hw/virtio/virtio.h @@ -99,6 +99,45 @@ enum virtio_device_endian { VIRTIO_DEVICE_ENDIAN_BIG, }; =20 +#define TYPE_VIRTIO_SHARED_MEMORY_MAPPING "virtio-shared-memory-mapping" +OBJECT_DECLARE_SIMPLE_TYPE(VirtioSharedMemoryMapping, VIRTIO_SHARED_MEMORY= _MAPPING) + +/** + * VirtioSharedMemoryMapping: + * @parent: Parent QOM object + * @shmid: VIRTIO Shared Memory Region ID =20 + * @fd: File descriptor for the shared memory region + * @offset: Offset within the VIRTIO Shared Memory Region + * @len: Size of the mapping + * @mr: MemoryRegion associated with this shared memory mapping + * @link: List entry for the shared memory region's mapping list + * + * A QOM object that represents an individual file descriptor-based shared + * memory mapping within a VIRTIO Shared Memory Region. It manages the + * MemoryRegion lifecycle and file descriptor cleanup through QOM reference + * counting. When the object is unreferenced and its reference count drops + * to zero, it automatically cleans up the MemoryRegion and closes the file + * descriptor. + */ +struct VirtioSharedMemoryMapping { + Object parent; + =20 + uint8_t shmid; + hwaddr offset; + uint64_t len; + MemoryRegion *mr; + QTAILQ_ENTRY(VirtioSharedMemoryMapping) link; +}; + +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 @@ -168,6 +207,8 @@ struct VirtIODevice */ EventNotifier config_notifier; bool device_iotlb_enabled; + /* Shared memory region for mappings. */ + QSIMPLEQ_HEAD(, VirtioSharedMemory) shmem_list; }; =20 struct VirtioDeviceClass { @@ -298,6 +339,100 @@ 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 + * @size: Size of the shared memory region + * + * 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, uint64_t size); + +/** + * 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_shared_memory_mapping_new() - Create a new VirtioSharedMemoryMap= ping + * @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 + * @allow_write: Whether to allow write access to the mapping + * + * Creates a new VirtioSharedMemoryMapping that manages a shared memory ma= pping. + * 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 VirtioSharedMemoryMapping on success, NULL on error. + */ +VirtioSharedMemoryMapping *virtio_shared_memory_mapping_new(uint8_t shmid, + int fd, + uint64_t fd_of= fset, + uint64_t shm_o= ffset, + uint64_t len, + bool allow_wri= te); + +/** + * virtio_add_shmem_map() - Add a memory mapping to a shared region + * @shmem: VirtioSharedMemory region + * @mapping: VirtioSharedMemoryMapping to add (transfers ownership) + * + * Adds a memory mapping to the shared memory region. The VirtioSharedMemo= ryMapping + * ownership is transferred to the shared memory region and will be automa= tically + * cleaned up through QOM reference counting 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, + VirtioSharedMemoryMapping *mapping); + +/** + * 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..6a2d0f9fae 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_* */ + uint64_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 Oct 25 19:04:45 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=1760625653; cv=none; d=zohomail.com; s=zohoarc; b=A/esXOi82R/cyb/edWzgd0q22DBKtb8M9WPcPdUGxIlchfz6ctjH9Bk3+kTxLeS1TArcunsMnvSzWBOLgz0fWuk7Zv6lU69Gua10xiaadkQF7At1tZJLE3cNSPKYAW+foPwBXAwx8KZYn2VPPtoDXOj6YhIzmgI1F7fmsvEfJkE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1760625653; 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=P/6dOE1ZFF5dwVpodzNhlxvFk3a1QgM7LmVbnfzb4KfzyKMk7SWQGo/GguYw/E2szI4HSXViUKWlYlI7a4h7LOk/C7HfWG9WQHxf/1vXUM3kXtKdYSmuv0UU+/ZcToQxXKlYiZx6TKXcTEr7uKHF9pMHqtH2fGg8abAZxrkbJcA= 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 1760625653055503.8884226889786; Thu, 16 Oct 2025 07:40:53 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1v9P8L-0008I7-AR; Thu, 16 Oct 2025 10:39:13 -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 1v9P8J-0008HB-9L for qemu-devel@nongnu.org; Thu, 16 Oct 2025 10:39:11 -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 1v9P84-0005Od-8z for qemu-devel@nongnu.org; Thu, 16 Oct 2025 10:39:09 -0400 Received: from mx-prod-mc-05.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-615-Op5XBTDeMMqum7kEGdysZg-1; Thu, 16 Oct 2025 10:38:50 -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-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 73CFD195609D; Thu, 16 Oct 2025 14:38:47 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.225.164]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 567891800451; Thu, 16 Oct 2025 14:38:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1760625533; 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=avcuj3DoxBO/KkUe7T0bXf5eyAcviWeoxaQLuxKzIzBOy13gIiMweCWTWx9zrhjKaC4HqV vfKqw9Jt8Mlispq6URhXGT5erdvTCKpUTSpYJuZJYWmfSnOlMlSO8xLta1GsI1krmc7IQi OEwFyGYhbHIeIq/6oATa7wVN3LO+c+k= X-MC-Unique: Op5XBTDeMMqum7kEGdysZg-1 X-Mimecast-MFC-AGG-ID: Op5XBTDeMMqum7kEGdysZg_1760625527 From: Albert Esteve To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , hi@alyssa.is, stefanha@redhat.com, david@redhat.com, jasowang@redhat.com, dbassey@redhat.com, stevensd@chromium.org, Stefano Garzarella , Laurent Vivier , "Michael S. Tsirkin" , Paolo Bonzini , Fabiano Rosas , slp@redhat.com, manos.pitsidianakis@linaro.org, Albert Esteve Subject: [PATCH v10 2/7] vhost_user.rst: Align VhostUserMsg excerpt members Date: Thu, 16 Oct 2025 16:38:22 +0200 Message-ID: <20251016143827.1850397-3-aesteve@redhat.com> In-Reply-To: <20251016143827.1850397-1-aesteve@redhat.com> References: <20251016143827.1850397-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: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no 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: 1760625654541154100 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 Acked-by: Stefano Garzarella --- 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 Oct 25 19:04:45 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=1760625590; cv=none; d=zohomail.com; s=zohoarc; b=n3rrj3XJ0G8g24j/9lajhJBt6Q42jwJTDwyzLfMhiSGHfaoZFxX/Tz//K13lnrtUlXSX0DbLzTP+gAcLO3KNUoYb+3mFRxd/LEX37v34IuwL6D6rRcIci5d2e0krHld/398Ovo37IO27MirI7KzeY+doRigPwUziq0RQpLIr0rg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1760625590; 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=RX7Yrvb6prfxl9xZykQ/sKAo/boWYkZ+vXxYT5rlr0c=; b=UZr7wqf6EuzdlGvYf2IV5OgEVlmbWHNzeytMp89619v3ok/ZooMXC6tzfMtMnHhG8p2393Frye4ffw4cJRY8PFpk9LIa8/Gsj9Run9/rTDG+Mnw2LsoNO8rBWxVQ4OnGHROze5ByYy1VNQ5ye94vgt3pBeVt0JxC5JRak6L4kIc= 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 1760625590923911.9217288929844; Thu, 16 Oct 2025 07:39:50 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1v9P8H-0008Gs-QU; Thu, 16 Oct 2025 10:39:09 -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 1v9P8F-0008Gb-Fw for qemu-devel@nongnu.org; Thu, 16 Oct 2025 10:39:07 -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 1v9P8A-0005Pg-HM for qemu-devel@nongnu.org; Thu, 16 Oct 2025 10:39:07 -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-398-2oOLIXn8Pbu-_eLrDPFhvg-1; Thu, 16 Oct 2025 10:38:57 -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 45CF91800669; Thu, 16 Oct 2025 14:38:54 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.225.164]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 058B51800451; Thu, 16 Oct 2025 14:38:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1760625538; 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=RX7Yrvb6prfxl9xZykQ/sKAo/boWYkZ+vXxYT5rlr0c=; b=endnxuraKOYosR0Srf6ycciu+aiQt2pmUcFGOeILL5OZY91UUBi6sW8XtxXfPMvHRQJYk+ VM0xbjHpX60cGA2XE8pdiaOcur8UDkEhzoIgKbWXENXKiAM4fhU9Yj9VlBzQdE+oo8VtgS usWJUHlbhZsthbKYM1hc3/ODWVRPZCQ= X-MC-Unique: 2oOLIXn8Pbu-_eLrDPFhvg-1 X-Mimecast-MFC-AGG-ID: 2oOLIXn8Pbu-_eLrDPFhvg_1760625534 From: Albert Esteve To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , hi@alyssa.is, stefanha@redhat.com, david@redhat.com, jasowang@redhat.com, dbassey@redhat.com, stevensd@chromium.org, Stefano Garzarella , Laurent Vivier , "Michael S. Tsirkin" , Paolo Bonzini , Fabiano Rosas , slp@redhat.com, manos.pitsidianakis@linaro.org, Albert Esteve Subject: [PATCH v10 3/7] vhost_user.rst: Add SHMEM_MAP/_UNMAP to spec Date: Thu, 16 Oct 2025 16:38:23 +0200 Message-ID: <20251016143827.1850397-4-aesteve@redhat.com> In-Reply-To: <20251016143827.1850397-1-aesteve@redhat.com> References: <20251016143827.1850397-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: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, 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_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_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: 1760625592336154100 Content-Type: text/plain; charset="utf-8" Add SHMEM_MAP/_UNMAP request to the vhost-user spec documentation. Reviewed-by: Stefan Hajnoczi Reviewed-by: Manos Pitsidianakis Signed-off-by: Albert Esteve Acked-by: Stefano Garzarella --- 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..8143d56419 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 Oct 25 19:04:45 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=1760625660; cv=none; d=zohomail.com; s=zohoarc; b=MBPcnfuRD5B012fVeMw+jq4svG+TSGQy7fbPZentluiQaB1LGm9L+0+d2mKK76U5wxl8QFBQY5WLUFosdATEs0tWcbakXUPnYwIADlo9Fk+JPcboW1UOiPS//PW+OSq3DLWr1NJZSPGz1duV9MzU7fkb3mgcqH218VWfMQTx66k= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1760625660; 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=/ShBJd7pr2QL1+bDad9zH4M11TP2RGj4jPPtrOcO4qo=; b=HS3e/I5sliBwWiXq16MbqPY98j/N+rzpE8cSy/0oIQuh0xOp6AfTuQ8XJ1FogAws+jtlndNiSmCNaQ0i4SocKc0t+49g4dpJq8ZnkfaaULKKh4rDvC8Ancx6OWapDtig+HZyiAkbMOUxEZJmZrNPTaE8HgwnHVRPviM0SFZBWDs= 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 1760625660845369.40780235224724; Thu, 16 Oct 2025 07:41:00 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1v9P8Q-0008Ia-8b; Thu, 16 Oct 2025 10:39:18 -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 1v9P8N-0008IM-8c for qemu-devel@nongnu.org; Thu, 16 Oct 2025 10:39:15 -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 1v9P8F-0005RD-Bx for qemu-devel@nongnu.org; Thu, 16 Oct 2025 10:39:14 -0400 Received: from mx-prod-mc-03.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-49-8tCOJCdTOxyCoIpI8IgOIw-1; Thu, 16 Oct 2025 10:39:02 -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-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 0998219560B7; Thu, 16 Oct 2025 14:39:01 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.225.164]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 06BF1180044F; Thu, 16 Oct 2025 14:38:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1760625545; 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=/ShBJd7pr2QL1+bDad9zH4M11TP2RGj4jPPtrOcO4qo=; b=JlSlbOwAFqchzuzwKzc8cNk11QI+Otqcckb1zOI70IAsVlAwllWoTgnQaphPqHN37BmDcw YhCzNBD7K+gehb7jWi3ElN1T3cNtmTbV7E8XbM1xnYq6pP/AOOixIsfIYGCtmBqlf5F7wh l9CYFExl+9PGNDrYxe01wrxthlRXzVc= X-MC-Unique: 8tCOJCdTOxyCoIpI8IgOIw-1 X-Mimecast-MFC-AGG-ID: 8tCOJCdTOxyCoIpI8IgOIw_1760625541 From: Albert Esteve To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , hi@alyssa.is, stefanha@redhat.com, david@redhat.com, jasowang@redhat.com, dbassey@redhat.com, stevensd@chromium.org, Stefano Garzarella , Laurent Vivier , "Michael S. Tsirkin" , Paolo Bonzini , Fabiano Rosas , slp@redhat.com, manos.pitsidianakis@linaro.org, Albert Esteve Subject: [PATCH v10 4/7] vhost_user: Add frontend get_shmem_config command Date: Thu, 16 Oct 2025 16:38:24 +0200 Message-ID: <20251016143827.1850397-5-aesteve@redhat.com> In-Reply-To: <20251016143827.1850397-1-aesteve@redhat.com> References: <20251016143827.1850397-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: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.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_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_PASS=-0.001, T_SPF_TEMPERROR=0.01 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: 1760625662714154100 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 Acked-by: Stefano Garzarella --- include/hw/virtio/vhost-backend.h | 10 ++++++++++ include/hw/virtio/vhost-user.h | 1 + include/hw/virtio/virtio.h | 2 ++ 3 files changed, 13 insertions(+) diff --git a/include/hw/virtio/vhost-backend.h b/include/hw/virtio/vhost-ba= ckend.h index ff94fa1734..ce58b4d2d6 100644 --- a/include/hw/virtio/vhost-backend.h +++ b/include/hw/virtio/vhost-backend.h @@ -163,6 +163,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; @@ -220,6 +229,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 3f6dfba321..bd6b16733d 100644 --- a/include/hw/virtio/virtio.h +++ b/include/hw/virtio/virtio.h @@ -82,6 +82,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 Oct 25 19:04:45 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=1760625612; cv=none; d=zohomail.com; s=zohoarc; b=DngNAXRWzyjQRFBwlYJ+2ohR6Ve5Amc6c2gP55PVcXdVI9Dz/MUAx12slq1+LrrkIafFlzOUA+z7UUBbVtBBNN7HIefsgqaMdKrhIWoyLiIsI6jFJJTH79GEiVp6BZA0TH/ytaInn+ANII1Fo+9KhxQcjpEISm8zQOsIoNRpNIo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1760625612; 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=w1a4qpt1G2tyRt3f6F/ulAO2oCZEdonXtlNtiv0il0s=; b=O4HgqikXWPSy6cMXLt/nfM+6qSEpd498TJjcUxZE5QEKzdrP5leH1sSSIFqO7fL1SavJNR3aquLrzh2cgVdg/Mbo0ZMWXnhwsY3INx7zXLohV2tIdghJzUMxnkK21Hu/D7dQDGQXhCl72LrwiWz2Pt9+ysagMcsXlFDzqFarEus= 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 1760625612748889.613842202753; Thu, 16 Oct 2025 07:40:12 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1v9P8Y-0008Kd-8k; Thu, 16 Oct 2025 10:39:26 -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 1v9P8X-0008KO-23 for qemu-devel@nongnu.org; Thu, 16 Oct 2025 10:39:25 -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 1v9P8P-0005U3-TH for qemu-devel@nongnu.org; Thu, 16 Oct 2025 10:39:24 -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-267-3srYVyy9O12QXatSJJSWrw-1; Thu, 16 Oct 2025 10:39:11 -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 86560187CC72; Thu, 16 Oct 2025 14:39:07 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.225.164]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 91BFA180057D; Thu, 16 Oct 2025 14:39:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1760625555; 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=w1a4qpt1G2tyRt3f6F/ulAO2oCZEdonXtlNtiv0il0s=; b=WTp2vmvUF4Zp62O6EERPiCPl1xoltnV76o8E4LAfqlRLoWIvAmzYyGUMDOTbSZhTyR2f7+ 5Jtjj8s8INtEUorQuFa2dHlzDIFn7Ts+mzl8DPvocZ69JxMN9YMAOiyLSwA6Bjm2/LGW2/ oT5L4HZQV1QJMtbt7Z+4WXHgfUxc80M= X-MC-Unique: 3srYVyy9O12QXatSJJSWrw-1 X-Mimecast-MFC-AGG-ID: 3srYVyy9O12QXatSJJSWrw_1760625550 From: Albert Esteve To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , hi@alyssa.is, stefanha@redhat.com, david@redhat.com, jasowang@redhat.com, dbassey@redhat.com, stevensd@chromium.org, Stefano Garzarella , Laurent Vivier , "Michael S. Tsirkin" , Paolo Bonzini , Fabiano Rosas , slp@redhat.com, manos.pitsidianakis@linaro.org, Albert Esteve Subject: [PATCH v10 5/7] vhost_user.rst: Add GET_SHMEM_CONFIG message Date: Thu, 16 Oct 2025 16:38:25 +0200 Message-ID: <20251016143827.1850397-6-aesteve@redhat.com> In-Reply-To: <20251016143827.1850397-1-aesteve@redhat.com> References: <20251016143827.1850397-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: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.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_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_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: 1760625613939158500 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 Acked-by: Stefano Garzarella --- 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 8143d56419..054c9a9866 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 Oct 25 19:04:45 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=1760625685; cv=none; d=zohomail.com; s=zohoarc; b=KaotrqAAQx0BJnNfk+CNC9KoPmPpHPx9h8z8NxRSNjMfCpnfOOUHhWun1zqWhiTboRKJIz5Sj0abI9WdwNNBSr9OjVHoCuNGvfzdZ2se78h9YdebV0WNjvs2SQxBgXI0k6fiU/gRKzGr6mrUP5o0VspZ1ktFbqFfb8Rb3TBljAk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1760625685; 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=0GihOsY9VpYYJ9edZB6MzyvGzMIBbZF7sNlbmGcCqOM=; b=d7wX+kmwBuBIRgoRHReJOmSuAASexki1jqnrQbO9OQoRw0nv0/bcOwjlFtH/7vSx6fC8FEnKv47sWEogvEeu4YUeFYHdCWkw6KB+UGs08o4wmX1RrNLOJ1yp711aSg7SV8/Wci2L+CcJBttHeDQN6Xe+DPKZlZDvq3oRmohHU7w= 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 1760625685954258.38830457249605; Thu, 16 Oct 2025 07:41:25 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1v9P8d-0008Mu-2B; Thu, 16 Oct 2025 10:39:31 -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 1v9P8b-0008MI-A5 for qemu-devel@nongnu.org; Thu, 16 Oct 2025 10:39:29 -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 1v9P8V-0005V3-Hq for qemu-devel@nongnu.org; Thu, 16 Oct 2025 10:39:28 -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-98-uW1yIkKMOxav9ibOVjoHAQ-1; Thu, 16 Oct 2025 10:39:17 -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 16A64180B5EA; Thu, 16 Oct 2025 14:39:14 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.225.164]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 0D35E1800581; Thu, 16 Oct 2025 14:39:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1760625560; 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=0GihOsY9VpYYJ9edZB6MzyvGzMIBbZF7sNlbmGcCqOM=; b=KEBrVi318KxhqssxoFeGkMnsBvjw55tpg7dWObMLlb+NC1l55aWTnthhasHCy63iseMneu hazC3LYgT6DvW8Bk78E/OsEQdN5mxBcRZCrb263axb/VjbOwv2uCxYslAWD2AzT+eb/z2r bEP4ibbQWq1dNe2+9aYP9PhndZ72mwA= X-MC-Unique: uW1yIkKMOxav9ibOVjoHAQ-1 X-Mimecast-MFC-AGG-ID: uW1yIkKMOxav9ibOVjoHAQ_1760625555 From: Albert Esteve To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , hi@alyssa.is, stefanha@redhat.com, david@redhat.com, jasowang@redhat.com, dbassey@redhat.com, stevensd@chromium.org, Stefano Garzarella , Laurent Vivier , "Michael S. Tsirkin" , Paolo Bonzini , Fabiano Rosas , slp@redhat.com, manos.pitsidianakis@linaro.org, Albert Esteve Subject: [PATCH v10 6/7] qmp: add shmem feature map Date: Thu, 16 Oct 2025 16:38:26 +0200 Message-ID: <20251016143827.1850397-7-aesteve@redhat.com> In-Reply-To: <20251016143827.1850397-1-aesteve@redhat.com> References: <20251016143827.1850397-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: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.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_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_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: 1760625688460158500 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 Acked-by: Stefano Garzarella --- 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 b338344c6c..b58a4b1e0e 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 Oct 25 19:04:45 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=1760625638; cv=none; d=zohomail.com; s=zohoarc; b=ESidi91NQ2tfC0x5QqElm+N5vjFYqRfZ0MHulOnGKm9zdy1GzsfnaRxWc3XHedgiN0o4s2gw8gpdfKwrfOGW+vw+GvKeKdovSzioe1aK/7YwYMCADCEJ4jDHVMBu/gCu2xBF1PRloyuPTQoVG2IV49RNOrayYX5x747w32lBGFo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1760625638; 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=8hSQoA8eIOc9C2oX1fUhQHDdUs8NERXQaVWuuDmmgYg=; b=J/2Xf4A0ahjtjvUMTYnPENdRIXN2iuglEifDRoKjo4ZiS4HC1b1OLekrHRJdjk/3KxlyAjv6MDc0MQpiB5A+1Oq+jmPKH855Fg6RBNGxI5jZDPUEunCF3fG4WeRSIULvix351nE17sUI6NZ16Q07/Pxuh7BMLbOm7xusnwLAv1o= 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 1760625637997719.5504410151337; Thu, 16 Oct 2025 07:40:37 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1v9P8i-0008Rr-Nk; Thu, 16 Oct 2025 10:39:36 -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 1v9P8g-0008Pk-PL for qemu-devel@nongnu.org; Thu, 16 Oct 2025 10:39:34 -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 1v9P8a-0005W1-TC for qemu-devel@nongnu.org; Thu, 16 Oct 2025 10:39:34 -0400 Received: from mx-prod-mc-08.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-318-eKgdMgQmPIWTJI2A18SAtQ-1; Thu, 16 Oct 2025 10:39:21 -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-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id CB30A18002C9; Thu, 16 Oct 2025 14:39:19 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.225.164]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 9E542180044F; Thu, 16 Oct 2025 14:39:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1760625564; 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=8hSQoA8eIOc9C2oX1fUhQHDdUs8NERXQaVWuuDmmgYg=; b=cjAv4VN+19ZIEhUsnWPkpXvBMgY8iWwwWLfFFW6pDw60Tk74DoGrGbLQxnuK0rd40k3Uwm SCG0fVwnSlNA1fsY3YOxQyV1il9I9X7tO6JJ9GkBtR/QX71CLzkPdqOqu6xZW37mNeO40w kGnPxtp7vB0jd81ItmoIrQIIUFTZQq8= X-MC-Unique: eKgdMgQmPIWTJI2A18SAtQ-1 X-Mimecast-MFC-AGG-ID: eKgdMgQmPIWTJI2A18SAtQ_1760625559 From: Albert Esteve To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , hi@alyssa.is, stefanha@redhat.com, david@redhat.com, jasowang@redhat.com, dbassey@redhat.com, stevensd@chromium.org, Stefano Garzarella , Laurent Vivier , "Michael S. Tsirkin" , Paolo Bonzini , Fabiano Rosas , slp@redhat.com, manos.pitsidianakis@linaro.org, Albert Esteve Subject: [PATCH v10 7/7] vhost-user-device: Add shared memory BAR Date: Thu, 16 Oct 2025 16:38:27 +0200 Message-ID: <20251016143827.1850397-8-aesteve@redhat.com> In-Reply-To: <20251016143827.1850397-1-aesteve@redhat.com> References: <20251016143827.1850397-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: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.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_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_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: 1760625640641154100 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. Reviewed-by: Stefan Hajnoczi Signed-off-by: Albert Esteve Acked-by: Stefano Garzarella --- hw/virtio/vhost-user-base.c | 47 ++++++++++++++++++++++++-- hw/virtio/vhost-user-test-device-pci.c | 39 +++++++++++++++++++-- 2 files changed, 81 insertions(+), 5 deletions(-) diff --git a/hw/virtio/vhost-user-base.c b/hw/virtio/vhost-user-base.c index ff67a020b4..82f49500e4 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, regions_processed =3D 0; =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,49 @@ 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 < VIRTIO_MAX_SHMEM_REGIONS && regions_processed < nreg= ions; i++) { + if (memory_sizes[i]) { + regions_processed++; + 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; + } + + virtio_new_shmem_region(vdev, i, 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-test-device-pci.c b/hw/virtio/vhost-user-= test-device-pci.c index b4ed0efb50..d010cb3205 100644 --- a/hw/virtio/vhost-user-test-device-pci.c +++ b/hw/virtio/vhost-user-test-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 4 + struct VHostUserDevicePCI { VirtIOPCIProxy parent_obj; =20 VHostUserBase vub; + MemoryRegion shmembar; }; =20 #define TYPE_VHOST_USER_TEST_DEVICE_PCI "vhost-user-test-device-pci-base" @@ -25,10 +29,41 @@ OBJECT_DECLARE_SIMPLE_TYPE(VHostUserDevicePCI, VHOST_US= ER_TEST_DEVICE_PCI) static void vhost_user_device_pci_realize(VirtIOPCIProxy *vpci_dev, Error = **errp) { VHostUserDevicePCI *dev =3D VHOST_USER_TEST_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->modern_mem_bar_idx =3D 2; 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) { + if (vpci_dev->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY) { + error_setg(errp, "modern-pio-notify is not supported due to PC= I BAR layout limitations"); + return; + } + 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