From nobody Sat Nov 15 16:07:31 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=1749480562; cv=none; d=zohomail.com; s=zohoarc; b=JduFDn5/KwWTin7fRQ6Ae6JvRpBy6G6+6iR2uy5RQJDmJOcAN0lyTjBmoTR1KyZJMuNEkc8+u0zmmggzI2VHQ49yqY70d0H15L0iAFS0JhF/gRC44ePU+AFwSF0vxnZWL3uJzvIfr7+KqeaarZx7xFHfsS8C5ithMzaObVhjmeQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749480562; 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=cSRvFPrucGq8avWh7bq40Vvge+7p2t9JoEZpR4AiD1U=; b=KoXhHgFHuB1ToqAoCtgLAc+O3yJC4v1JG06rlUGEPrHvq6d6QgXE9iNEGCizxplo793sw2e2nwGSFSorpZa/mu11pTaXI3UgBTgkEf1Ltfqgg0FB/Hse3ZvVrDncgY1X6NaRMpU/YO89gSu4TFip2DR5sIcdhUd//PDShIhW7Bg= 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 1749480562809430.08401557039997; Mon, 9 Jun 2025 07:49:22 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uOdoD-0005Kh-IQ; Mon, 09 Jun 2025 10:49: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 1uOdo9-0005IT-K7 for qemu-devel@nongnu.org; Mon, 09 Jun 2025 10:49:05 -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 1uOdo6-0008OW-Ky for qemu-devel@nongnu.org; Mon, 09 Jun 2025 10:49:05 -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-124-ISnaNOUjM66GFsnpi1b3IQ-1; Mon, 09 Jun 2025 10:47:43 -0400 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 0F2AB19560B0; Mon, 9 Jun 2025 14:47:42 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.44.33.48]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 86A3619560AF; Mon, 9 Jun 2025 14:47:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1749480541; 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=cSRvFPrucGq8avWh7bq40Vvge+7p2t9JoEZpR4AiD1U=; b=TZvWJKouRC4u+u/lVuFKPS7VXf67sEJM707GDHBEcFCDeG72a9JwvhQPfnljYZsgZkgcFw sR2E9o3lEL3zGD2Z8dE/XCCIEzb8pZnQuY2i2x4e9Fd6zWYxYN2UD+BaOsuD0KKOF0T7EI PF07FWmpaXPydjFOKlzhmMfNRpv7Bbs= X-MC-Unique: ISnaNOUjM66GFsnpi1b3IQ-1 X-Mimecast-MFC-AGG-ID: ISnaNOUjM66GFsnpi1b3IQ_1749480462 From: Albert Esteve To: qemu-devel@nongnu.org Cc: stefanha@redhat.com, slp@redhat.com, david@redhat.com, "Michael S. Tsirkin" , Stefano Garzarella , jasowang@redhat.com, stevensd@chromium.org, hi@alyssa.is, =?UTF-8?q?Alex=20Benn=C3=A9e?= , Albert Esteve Subject: [PATCH v5 1/7] vhost-user: Add VirtIO Shared Memory map request Date: Mon, 9 Jun 2025 16:47:23 +0200 Message-ID: <20250609144729.884027-2-aesteve@redhat.com> In-Reply-To: <20250609144729.884027-1-aesteve@redhat.com> References: <20250609144729.884027-1-aesteve@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 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_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=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: 1749480564225116600 Content-Type: text/plain; charset="utf-8" Add SHMEM_MAP/UNMAP requests to vhost-user to handle VIRTIO Shared Memory mappings. This request allows backends to dynamically map fds into a VIRTIO Shared Memory Region indentified by its `shmid`. The map is performed by calling `memory_region_init_ram_from_fd` and adding the new region as a subregion of the shmem container MR. Then, the fd memory is advertised to the driver as a base addres + offset, so it can be read/written (depending on the mmap flags requested) while it is valid. The backend can unmap the memory range in a given VIRTIO Shared Memory Region (again, identified by its `shmid`), to free it. Upon receiving this message, the front-end must delete the MR as a subregion of the shmem container region and free its resources. Note that commit all these operations need to be delayed to after we respond the request to the backend to avoid deadlocks. The device model needs to create VirtSharedMemory instances for the VirtIO Shared Memory Regions and add them to the `VirtIODevice` instance. Signed-off-by: Albert Esteve --- hw/virtio/vhost-user.c | 150 ++++++++++++++++++++++ hw/virtio/virtio.c | 97 ++++++++++++++ include/hw/virtio/virtio.h | 29 +++++ subprojects/libvhost-user/libvhost-user.c | 70 ++++++++++ subprojects/libvhost-user/libvhost-user.h | 54 ++++++++ 5 files changed, 400 insertions(+) diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c index 1e1d6b0d6e..9c635fb928 100644 --- a/hw/virtio/vhost-user.c +++ b/hw/virtio/vhost-user.c @@ -115,6 +115,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 +194,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 +243,7 @@ typedef union { VhostUserInflight inflight; VhostUserShared object; VhostUserTransferDeviceState transfer_state; + VhostUserMMap mmap; } VhostUserPayload; =20 typedef struct VhostUserMsg { @@ -1768,6 +1788,129 @@ vhost_user_backend_handle_shared_object_lookup(stru= ct vhost_user *u, return 0; } =20 +static int +vhost_user_backend_handle_shmem_map(struct vhost_dev *dev, + QIOChannel *ioc, + VhostUserHeader *hdr, + VhostUserPayload *payload, + int fd) +{ + uint32_t ram_flags; + VirtSharedMemory *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(); + ram_flags =3D RAM_SHARED | + ((vu_mmap->flags & VHOST_USER_FLAG_MAP_RW) ? 0 : RAM_READO= NLY); + if (virtio_add_shmem_map(shmem, shm_name->str, vu_mmap->shm_offset, + vu_mmap->fd_offset, vu_mmap->len, ram_flags, + fd) !=3D 0) { + 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; +} + +static int +vhost_user_backend_handle_shmem_unmap(struct vhost_dev *dev, + QIOChannel *ioc, + VhostUserHeader *hdr, + VhostUserPayload *payload) +{ + VirtSharedMemory *shmem; + MappedMemoryRegion *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) { + 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); @@ -1836,6 +1979,13 @@ 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: + ret =3D vhost_user_backend_handle_shmem_map(dev, ioc, &hdr, &paylo= ad, + fd ? fd[0] : -1); + break; + case VHOST_USER_BACKEND_SHMEM_UNMAP: + ret =3D vhost_user_backend_handle_shmem_unmap(dev, ioc, &hdr, &pay= load); + break; default: error_report("Received unexpected msg type: %d.", hdr.request); ret =3D -EINVAL; diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index 5534251e01..208ad11685 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -3035,6 +3035,92 @@ int virtio_save(VirtIODevice *vdev, QEMUFile *f) return vmstate_save_state(f, &vmstate_virtio, vdev, NULL); } =20 +VirtSharedMemory *virtio_new_shmem_region(VirtIODevice *vdev, uint8_t shmi= d) +{ + VirtSharedMemory *elem; + elem =3D g_new0(VirtSharedMemory, 1); + elem->shmid =3D shmid; + elem->mr =3D g_new0(MemoryRegion, 1); + QTAILQ_INIT(&elem->mmaps); + QSIMPLEQ_INSERT_TAIL(&vdev->shmem_list, elem, entry); + return QSIMPLEQ_LAST(&vdev->shmem_list, VirtSharedMemory, entry); +} + +VirtSharedMemory *virtio_find_shmem_region(VirtIODevice *vdev, uint8_t shm= id) +{ + VirtSharedMemory *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(VirtSharedMemory *shmem, const char *shm_name, + hwaddr shm_offset, hwaddr fd_offset, uint64_t si= ze, + uint32_t ram_flags, int fd) +{ + Error *err =3D NULL; + MappedMemoryRegion *mmap; + fd =3D dup(fd); + if (fd < 0) { + error_report("Failed to duplicate fd: %s", strerror(errno)); + return -1; + } + + if (shm_offset + size > shmem->mr->size) { + error_report("Memory exceeds the shared memory boundaries"); + close(fd); + return -1; + } + + mmap =3D g_new0(MappedMemoryRegion, 1); + mmap->mem =3D g_new0(MemoryRegion, 1); + mmap->offset =3D shm_offset; + memory_region_init_ram_from_fd(mmap->mem, + OBJECT(shmem->mr), + shm_name, size, ram_flags, + fd, fd_offset, &err); + if (err) { + error_report_err(err); + close(fd); + g_free(mmap->mem); + g_free(mmap); + return -1; + } + memory_region_add_subregion(shmem->mr, shm_offset, mmap->mem); + + QTAILQ_INSERT_TAIL(&shmem->mmaps, mmap, link); + + return 0; +} + +MappedMemoryRegion *virtio_find_shmem_map(VirtSharedMemory *shmem, + hwaddr offset, uint64_t size) +{ + MappedMemoryRegion *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(VirtSharedMemory *shmem, hwaddr offset, + uint64_t size) +{ + MappedMemoryRegion *mmap =3D virtio_find_shmem_map(shmem, offset, size= ); + if (mmap =3D=3D NULL) { + return; + } + + object_unparent(OBJECT(mmap->mem)); + 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) @@ -3511,6 +3597,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 /* @@ -4022,11 +4109,21 @@ static void virtio_device_free_virtqueues(VirtIODev= ice *vdev) static void virtio_device_instance_finalize(Object *obj) { VirtIODevice *vdev =3D VIRTIO_DEVICE(obj); + VirtSharedMemory *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)) { + MappedMemoryRegion *mmap_reg =3D QTAILQ_FIRST(&shmem->mmaps); + virtio_del_shmem_map(shmem, mmap_reg->offset, mmap_reg->mem->s= ize); + } + QSIMPLEQ_REMOVE_HEAD(&vdev->shmem_list, entry); + g_free(shmem); + } } =20 static const Property virtio_properties[] =3D { diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h index 214d4a77e9..331dbcfbe0 100644 --- a/include/hw/virtio/virtio.h +++ b/include/hw/virtio/virtio.h @@ -98,6 +98,23 @@ enum virtio_device_endian { VIRTIO_DEVICE_ENDIAN_BIG, }; =20 +struct MappedMemoryRegion { + MemoryRegion *mem; + hwaddr offset; + QTAILQ_ENTRY(MappedMemoryRegion) link; +}; + +typedef struct MappedMemoryRegion MappedMemoryRegion; + +struct VirtSharedMemory { + uint8_t shmid; + MemoryRegion *mr; + QTAILQ_HEAD(, MappedMemoryRegion) mmaps; + QSIMPLEQ_ENTRY(VirtSharedMemory) entry; +}; + +typedef struct VirtSharedMemory VirtSharedMemory; + /** * struct VirtIODevice - common VirtIO structure * @name: name of the device @@ -167,6 +184,8 @@ struct VirtIODevice */ EventNotifier config_notifier; bool device_iotlb_enabled; + /* Shared memory region for mappings. */ + QSIMPLEQ_HEAD(, VirtSharedMemory) shmem_list; }; =20 struct VirtioDeviceClass { @@ -289,6 +308,16 @@ void virtio_notify(VirtIODevice *vdev, VirtQueue *vq); =20 int virtio_save(VirtIODevice *vdev, QEMUFile *f); =20 +VirtSharedMemory *virtio_new_shmem_region(VirtIODevice *vdev, uint8_t shmi= d); +VirtSharedMemory *virtio_find_shmem_region(VirtIODevice *vdev, uint8_t shm= id); +int virtio_add_shmem_map(VirtSharedMemory *shmem, const char *shm_name, + hwaddr shm_offset, hwaddr fd_offset, uint64_t si= ze, + uint32_t ram_flags, int fd); +MappedMemoryRegion *virtio_find_shmem_map(VirtSharedMemory *shmem, + hwaddr offset, uint64_t size); +void virtio_del_shmem_map(VirtSharedMemory *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 16:07:31 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=1749480572; cv=none; d=zohomail.com; s=zohoarc; b=G3GFxp6EUj48s5sJ/6hdZpSzIyQ4Wm7jKuABX6V9zhxEqpefF0WAS119b8+boj8z8yWfmxjQzGKP55gJ8fdAZLIQWWKrB6rMHqEUkE+auQ4Oa0g5sZyFby9yXOLKOuhQQEzpYvDSw498rA49FwSiIepIQPKwynfn3GmYSbzOiQE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749480572; 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=Q9WoYWT7QmoCaKiCPxzWUSdnyY1fpPD1j6seb0gjwAM=; b=LvOgSU76AIk89b7NSGMhMQ7ZaBm6ES187l6J2g4Z5Pmdjc641/ONo+pQU4Ssi3HkB5l4yVV/Uw8+adVac4u/b0rxNu60ptVFkNUt+Sk0cfnXO7K6cw4rF3QJHVjMNUJyp12TWM2HuNlDyg1sGA8HFmuqnek8wksWPtDtQnfTrZo= 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 1749480572811130.80220836715114; Mon, 9 Jun 2025 07:49:32 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uOdoK-0005MN-D4; Mon, 09 Jun 2025 10:49:16 -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 1uOdo8-0005II-FY for qemu-devel@nongnu.org; Mon, 09 Jun 2025 10:49:05 -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 1uOdo6-0008Oh-LC for qemu-devel@nongnu.org; Mon, 09 Jun 2025 10:49:04 -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-426-c0sxBrjDOcaVHXd9xxitmA-1; Mon, 09 Jun 2025 10:47:48 -0400 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 C0F501956080; Mon, 9 Jun 2025 14:47:46 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.44.33.48]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id B0A8C19560AF; Mon, 9 Jun 2025 14:47:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1749480541; 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=Q9WoYWT7QmoCaKiCPxzWUSdnyY1fpPD1j6seb0gjwAM=; b=ZjiSjAEiVcjPwKYY0LbrTei2cByvZKEfc7ZpISVDrHkIORrI4yLuYH8JJMiTeRDevl+dFW EVeMd0VYJYctZHROOFfmzNQlNLus7uARicneW/8TgE5wHkS54sjkJcuVis+ul0IfqtIhok D7NFRvhLd0PNDF/8JSAtfYYFYoocsxQ= X-MC-Unique: c0sxBrjDOcaVHXd9xxitmA-1 X-Mimecast-MFC-AGG-ID: c0sxBrjDOcaVHXd9xxitmA_1749480467 From: Albert Esteve To: qemu-devel@nongnu.org Cc: stefanha@redhat.com, slp@redhat.com, david@redhat.com, "Michael S. Tsirkin" , Stefano Garzarella , jasowang@redhat.com, stevensd@chromium.org, hi@alyssa.is, =?UTF-8?q?Alex=20Benn=C3=A9e?= , Albert Esteve Subject: [PATCH v5 2/7] vhost_user.rst: Align VhostUserMsg excerpt members Date: Mon, 9 Jun 2025 16:47:24 +0200 Message-ID: <20250609144729.884027-3-aesteve@redhat.com> In-Reply-To: <20250609144729.884027-1-aesteve@redhat.com> References: <20250609144729.884027-1-aesteve@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 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_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=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: 1749480573975116600 Content-Type: text/plain; charset="utf-8" Add missing members to the VhostUserMsg excerpt in the vhost-user spec documentation. Reviewed-by: Stefan Hajnoczi Signed-off-by: Albert Esteve Reviewed-by: David Hildenbrand --- 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 16:07:31 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=1749480605; cv=none; d=zohomail.com; s=zohoarc; b=NpLPSb3nbchK7YAqyzMCPN/Bxo1vJfiwgkl+zyzLAw+g5c1uHQr9bTirXiK1h/B7HCyMzCJiugN/V0N1HXLueKgcQdLWy1BPZZGHX1zxYBeRziiNrWbq9gWTtE5KhvHD6RGHoCEypz4twckl4vejAjv6seKF/yfaBoR6QgMgDAU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749480605; 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=ZvOd00GowewmzBnbnqRYjZuswI4gvnr0bfGLIS0ijts=; b=NF6odxH9dsdJFk7gYsM6L3F4yS4i6sfS4kFa6f1BbnH2wYAjzrr9GDss1VRNt31mcxB5IjvL+GSL/doMzmsK62v+1bZm+tAMoG2/egt8Fa50U3c1BOkUMsn02ShbdvLaaEfbishG1K2Y5Lo+5VrFEe8mWkiOl0tLVJ5/WE3bev0= 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 1749480605264781.0118633783164; Mon, 9 Jun 2025 07:50:05 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uOdoP-0005Oj-5U; Mon, 09 Jun 2025 10:49:21 -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 1uOdoA-0005Jd-C5 for qemu-devel@nongnu.org; Mon, 09 Jun 2025 10:49:07 -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 1uOdo8-0008P1-8z for qemu-devel@nongnu.org; Mon, 09 Jun 2025 10:49:05 -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-235-spJQ8L0bPRKXzf7ytvzLQg-1; Mon, 09 Jun 2025 10:47:53 -0400 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 1FA5E1808985; Mon, 9 Jun 2025 14:47:52 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.44.33.48]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 8F76319560A3; Mon, 9 Jun 2025 14:47:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1749480543; 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=ZvOd00GowewmzBnbnqRYjZuswI4gvnr0bfGLIS0ijts=; b=Yor0BpSamK/UrGvm5nBkn+tBNq0bDAt5x4Qh0YSVrh2qy5PWZ6Z57hLW74RpNY/sYBSwqo fOGMRaM3/kvYAXN3SRU45JpAcxki8Rlo11et27+iasx2+uj1cnGtFrnJZ2SPslEF+5A4Bc 5bhCDiTbaQWDrLE3YezSmip7asuDegY= X-MC-Unique: spJQ8L0bPRKXzf7ytvzLQg-1 X-Mimecast-MFC-AGG-ID: spJQ8L0bPRKXzf7ytvzLQg_1749480472 From: Albert Esteve To: qemu-devel@nongnu.org Cc: stefanha@redhat.com, slp@redhat.com, david@redhat.com, "Michael S. Tsirkin" , Stefano Garzarella , jasowang@redhat.com, stevensd@chromium.org, hi@alyssa.is, =?UTF-8?q?Alex=20Benn=C3=A9e?= , Albert Esteve Subject: [PATCH v5 3/7] vhost_user.rst: Add SHMEM_MAP/_UNMAP to spec Date: Mon, 9 Jun 2025 16:47:25 +0200 Message-ID: <20250609144729.884027-4-aesteve@redhat.com> In-Reply-To: <20250609144729.884027-1-aesteve@redhat.com> References: <20250609144729.884027-1-aesteve@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 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_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=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: 1749480608422116600 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 | 55 +++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/docs/interop/vhost-user.rst b/docs/interop/vhost-user.rst index 436a94c0ee..b623284819 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,38 @@ 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``. A reply is + generated indicating whether mapping succeeded. + + Mapping over an already existing map is not allowed and request shall fa= il. + 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-mmap a given range (``shm_offset``, ``len``) in the + 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 16:07:31 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=1749480613; cv=none; d=zohomail.com; s=zohoarc; b=CLMcsISmSS1TJlROdncpFApS8eivDa5pAR6EjAU6Z0MQMrk9XbuiDNxrUsSoiUnycQpQpD9V/YYjGhOpP6gJuTgRC4fP5JaSe/0VdfGcxRhkXQNk3fckb153QngZ4wF098WAey5KO79ElUgqBCZnwxaAVGHIdf58j/H1lJGAGUo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749480613; 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=KT3vvxjLk6V9gUarKGHNRXLEYA8GLmdNyr7W4+VtIG0=; b=FT3CQGul6LSjb+aQDzFEBDTMTknRbYymLuSHj+NIE6WoPYgjjYPy5zBN09ThyH843N4UJQg0mg4MX4RuUljs2kahwIfGdfU7HWDSzAMz3HvEvF1D8JG9jcrLhnF2Qs6EtVbhFBoh4J/JM6tDiZ2dP/ygT+/mr9nOrKKjxvqTnp8= 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 1749480613528272.22289459593185; Mon, 9 Jun 2025 07:50:13 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uOdoQ-0005Tv-6i; Mon, 09 Jun 2025 10:49:22 -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 1uOdoF-0005LL-Cw for qemu-devel@nongnu.org; Mon, 09 Jun 2025 10:49: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 1uOdoB-0008Py-B4 for qemu-devel@nongnu.org; Mon, 09 Jun 2025 10:49:10 -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-501-I1uB4aFqNIGNQR0brSJLzg-1; Mon, 09 Jun 2025 10:47:58 -0400 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 1DDF41955F3B; Mon, 9 Jun 2025 14:47:57 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.44.33.48]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id A840019560A3; Mon, 9 Jun 2025 14:47:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1749480546; 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=KT3vvxjLk6V9gUarKGHNRXLEYA8GLmdNyr7W4+VtIG0=; b=G3EzMDyfkJASP7oLHo/AXHQQHTU+Ej7FT2TKPcL13HpohS2eBDmAL/IxnEUWhc4D3W+sKc DOcg9Or+cc3p9RZBAcF/k5c/tK8PTXx8o2jHjgz0A84BOJ1KkQs64jeLmM6tuXF2jZDaZ8 5v5gKkMbilE1YETtbBH99U7eUR6QEk4= X-MC-Unique: I1uB4aFqNIGNQR0brSJLzg-1 X-Mimecast-MFC-AGG-ID: I1uB4aFqNIGNQR0brSJLzg_1749480477 From: Albert Esteve To: qemu-devel@nongnu.org Cc: stefanha@redhat.com, slp@redhat.com, david@redhat.com, "Michael S. Tsirkin" , Stefano Garzarella , jasowang@redhat.com, stevensd@chromium.org, hi@alyssa.is, =?UTF-8?q?Alex=20Benn=C3=A9e?= , Albert Esteve Subject: [PATCH v5 4/7] vhost_user: Add frontend get_shmem_config command Date: Mon, 9 Jun 2025 16:47:26 +0200 Message-ID: <20250609144729.884027-5-aesteve@redhat.com> In-Reply-To: <20250609144729.884027-1-aesteve@redhat.com> References: <20250609144729.884027-1-aesteve@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 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_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=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: 1749480614621116600 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. Signed-off-by: Albert Esteve Reviewed-by: Stefan Hajnoczi --- 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 9c635fb928..f32da60ac4 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 @@ -138,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; @@ -244,6 +251,7 @@ typedef union { VhostUserShared object; VhostUserTransferDeviceState transfer_state; VhostUserMMap mmap; + VhostUserShMemConfig shmem; } VhostUserPayload; =20 typedef struct VhostUserMsg { @@ -3160,6 +3168,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, @@ -3198,4 +3240,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 331dbcfbe0..5d0c084f57 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 16:07:31 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=1749480580; cv=none; d=zohomail.com; s=zohoarc; b=oEp8M4F8g0X9CdudpwJozZSg27TiS3FvCFwJyoJlLy4NtZ1PwE3ciOo5SCg1Jxh64Bo0zEBFFoHlIE1CTSqrFznQivxHj/k1461I5qLiz9ij0LJImg3qAT8KLy6O1ZK18/sS/C/hP0sgLCKZNdn6eJhGSxz2vbz3Z7bmF9nyltA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749480580; 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=lamwwFlqRhI+OjcWjKGQFm+FekTstOZMuuli+U61lJU=; b=V8tbnEYHToKC4jUkcFRzgEiNl7BbGZVpfwZW+mR3mJCdw6KhhNpuaVAaDEP6A5RfBUu/iwwf+rvQwGYsemtqgnCTAH9lMwya8Qt0r0NHLyBAcDexwd0jTL1GC0CsDf6aLjCCDpH78nQicCDUJv9mKAesXQ1leniKpcp0B3kWZlM= 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 1749480580874542.9509451432227; Mon, 9 Jun 2025 07:49:40 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uOdoS-0005Zk-QP; Mon, 09 Jun 2025 10:49:25 -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 1uOdoG-0005LV-R0 for qemu-devel@nongnu.org; Mon, 09 Jun 2025 10:49: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 1uOdoF-0008Qj-5n for qemu-devel@nongnu.org; Mon, 09 Jun 2025 10:49:12 -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-663-zLievVK_NT6NhrfUtAdUCw-1; Mon, 09 Jun 2025 10:48:02 -0400 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 E2F0A1955F41; Mon, 9 Jun 2025 14:48:01 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.44.33.48]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id B0FBD19560A3; Mon, 9 Jun 2025 14:47:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1749480550; 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=lamwwFlqRhI+OjcWjKGQFm+FekTstOZMuuli+U61lJU=; b=BMmp+l6KgZR2Jh5L7q34NFHsBUFlq4bDkcywq0/qblNCgv2cRwL4/vXchGJwBreLYhEl6H KwOpGMlwfShg+A7BUXh61MceefBWR4oo99HKErJqJzEidlfSwPV2YVGzIHSOEyTZoTnDrI D1zuqb6fJN6CLWC1B+3cnQfo3y8jlos= X-MC-Unique: zLievVK_NT6NhrfUtAdUCw-1 X-Mimecast-MFC-AGG-ID: zLievVK_NT6NhrfUtAdUCw_1749480482 From: Albert Esteve To: qemu-devel@nongnu.org Cc: stefanha@redhat.com, slp@redhat.com, david@redhat.com, "Michael S. Tsirkin" , Stefano Garzarella , jasowang@redhat.com, stevensd@chromium.org, hi@alyssa.is, =?UTF-8?q?Alex=20Benn=C3=A9e?= , Albert Esteve Subject: [PATCH v5 5/7] vhost_user.rst: Add GET_SHMEM_CONFIG message Date: Mon, 9 Jun 2025 16:47:27 +0200 Message-ID: <20250609144729.884027-6-aesteve@redhat.com> In-Reply-To: <20250609144729.884027-1-aesteve@redhat.com> References: <20250609144729.884027-1-aesteve@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 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_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=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: 1749480582385116600 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 b623284819..223bfe7cd7 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 16:07:31 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=1749480604; cv=none; d=zohomail.com; s=zohoarc; b=hXvCPaiW7SIhfTc77V8a7hNxZR9+lmGCnnXphPfBjq78A3ykZEJRJ1G5qg56PcAWYbKEouLDqgyZPTtTldGrKAAGPtiGxPM9SIeA3veAnGLdwXunDXcObCFzh47qC1Lyp+xAPJhIVezXyl9FOLePGRSi0wkRqjMRzSbkpkriy6I= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749480604; 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=d1Qq7CfjKxLhDflccFAwknMXEZKSWxZb1yFuXl9Iqxr+q6awk1wkOCATS69jd4ZIIDz3lMrBJ4rT7DAJ8memRRC3TFqG3G+DV/tEV7ECLzb608094M45EiC/UEGJD64/MADmBJZJLyZzEUE3zzpDoqYu5okI0j+ls47ca/B+WQU= 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 1749480604437698.5404528979408; Mon, 9 Jun 2025 07:50:04 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uOdoT-0005bh-Dx; Mon, 09 Jun 2025 10:49:25 -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 1uOdoL-0005NX-7h for qemu-devel@nongnu.org; Mon, 09 Jun 2025 10:49:17 -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 1uOdoI-0008RQ-KY for qemu-devel@nongnu.org; Mon, 09 Jun 2025 10:49:16 -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-290-WyhdsUb2NjWfvuP5B9yXkQ-1; Mon, 09 Jun 2025 10:48:07 -0400 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 A165019560A6; Mon, 9 Jun 2025 14:48:06 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.44.33.48]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 80AEA19560A3; Mon, 9 Jun 2025 14:48:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1749480552; 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=FXmtO+y1WQRwhNdU0nXnGMvrNxGchRmvNy5E8zS5NVaJGlZ2HGjIT7n7OsAPhPrKlxy7mb +S9FDJ+6YlQd3pMUC9ERD8FoBrhiSFAAog4717CBTJfYOr8XCN0HVBkh20xbxoGiMEMzQQ SLndgfoTl/RZ0ud0Ncp53wj02KrlLPE= X-MC-Unique: WyhdsUb2NjWfvuP5B9yXkQ-1 X-Mimecast-MFC-AGG-ID: WyhdsUb2NjWfvuP5B9yXkQ_1749480486 From: Albert Esteve To: qemu-devel@nongnu.org Cc: stefanha@redhat.com, slp@redhat.com, david@redhat.com, "Michael S. Tsirkin" , Stefano Garzarella , jasowang@redhat.com, stevensd@chromium.org, hi@alyssa.is, =?UTF-8?q?Alex=20Benn=C3=A9e?= , Albert Esteve Subject: [PATCH v5 6/7] qmp: add shmem feature map Date: Mon, 9 Jun 2025 16:47:28 +0200 Message-ID: <20250609144729.884027-7-aesteve@redhat.com> In-Reply-To: <20250609144729.884027-1-aesteve@redhat.com> References: <20250609144729.884027-1-aesteve@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 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_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=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: 1749480606341116600 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 16:07:31 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=1749480593; cv=none; d=zohomail.com; s=zohoarc; b=lldkA/VKoS3ayKLiBhSBhR7kYC5Wm2N9XBRrvIFxtQhE6c1EpJVBIajxf5WjZ4schcLfErWFmOmy25t/q6yExC4rhYarWP3/e6ks/z1DASvsxuPgZRISJx9AYx0imutExxPVm4lGRMzqPUD2KD9fgOEzi3lc2dj/oZMAWarcjFw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1749480593; 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=5pa8ZLIFcm7ZQbS69wa/35MlV33QTaHbyfkUUJ8TIbM=; b=dKu/wLrpQOJQWaqEhkwQWtQ9bajeM7mDD0xHDxDX87+XDXGvsbMSpZUnmV1yAw5N5T04vkKrIB3p1UUoZy2d6II4COPKQeYYbF5gQTthIqizbNJlEnC5mabqBEmTRHw7SH1pWCWeC1ry+1gkEriSou2jdU+3B9EAeA1l67WdUYo= 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 1749480593492629.28357092675; Mon, 9 Jun 2025 07:49:53 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uOdoU-0005dK-24; Mon, 09 Jun 2025 10:49: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 1uOdoR-0005Xc-Se for qemu-devel@nongnu.org; Mon, 09 Jun 2025 10:49:24 -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 1uOdoP-0008TE-VT for qemu-devel@nongnu.org; Mon, 09 Jun 2025 10:49:23 -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-524-YJPmxx9yMleE6tyF7bOdFA-1; Mon, 09 Jun 2025 10:48:13 -0400 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 6E2DA1956094; Mon, 9 Jun 2025 14:48:11 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.44.33.48]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 3961119560A3; Mon, 9 Jun 2025 14:48:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1749480561; 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=5pa8ZLIFcm7ZQbS69wa/35MlV33QTaHbyfkUUJ8TIbM=; b=GMHXOBVTCxKbwnOLNjttHqQDnrlsZTKMVy9XbFFuAvPRkDO8Q/qU7KmVvSrt5lV2N82HSv /3m8dnWBV/EjwePxFEo4a129CBl1VbybMyGXhxqMXgLMDGUe0gYn8wv8Gl9oAnToTEg8B4 tm6pt4XavKe8HbwMIsI73VUpM7axDm0= X-MC-Unique: YJPmxx9yMleE6tyF7bOdFA-1 X-Mimecast-MFC-AGG-ID: YJPmxx9yMleE6tyF7bOdFA_1749480491 From: Albert Esteve To: qemu-devel@nongnu.org Cc: stefanha@redhat.com, slp@redhat.com, david@redhat.com, "Michael S. Tsirkin" , Stefano Garzarella , jasowang@redhat.com, stevensd@chromium.org, hi@alyssa.is, =?UTF-8?q?Alex=20Benn=C3=A9e?= , Albert Esteve Subject: [PATCH v5 7/7] vhost-user-devive: Add shmem BAR Date: Mon, 9 Jun 2025 16:47:29 +0200 Message-ID: <20250609144729.884027-8-aesteve@redhat.com> In-Reply-To: <20250609144729.884027-1-aesteve@redhat.com> References: <20250609144729.884027-1-aesteve@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 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_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=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: 1749480594466116600 Content-Type: text/plain; charset="utf-8" Add a shmem BAR block in the vhost-user-device, which files can be directly mapped into. The number, shmid, and size of the VIRTIO Shared Memory subregions is retrieved through a get_shmem_config message sent by the vhost-user-base module on the realize step, after virtio_init(). By default, if VHOST_USER_PROTOCOL_F_SHMEM feature is not supported by the backend, there is no cache. Signed-off-by: Albert Esteve --- hw/virtio/vhost-user-base.c | 47 +++++++++++++++++++++++++++++-- hw/virtio/vhost-user-device-pci.c | 34 ++++++++++++++++++++-- 2 files changed, 76 insertions(+), 5 deletions(-) diff --git a/hw/virtio/vhost-user-base.c b/hw/virtio/vhost-user-base.c index ff67a020b4..e86e391fa5 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,8 @@ 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]; + int i, ret, nregions; =20 if (!vub->chardev.chr) { error_setg(errp, "vhost-user-base: missing chardev"); @@ -319,7 +321,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 +335,50 @@ 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; + } + + memory_region_init(virtio_new_shmem_region(vdev, i)->mr, + OBJECT(vdev), "vub-shm-" + 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-device-pci.c b/hw/virtio/vhost-user-devic= e-pci.c index f10bac874e..eeb52671a0 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 2 + 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); + VirtSharedMemory *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, 0); + 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