vhost-user: Add SHMEM_MAP/UNMAP requests
v11 cover vhost-user: Add SHMEM_MAP/UNMAP requests
Hi all,
v10->v11
- Rebase to grab commit fde5930cc37175cfcd0f03a089e26f4458a52311 and explicitly unset reply_ack on the SHMEM_MAP/UNMAP msg.
- Mention mapping cleanup on device reset in the spec. v9->v10
- Fix transaction_commit invoked without transaction_begin on vhost_user_backend_handle_shmem_map() early errors
- Removed fd tracking on VirtioSharedMemoryMapping, it is handled by the RAMBlock
- Reject invalid BAR configurations when VIRTIO Shared Memory Regions are in use by vhost-user-test-devicev8->v9
- Fixed vhost-user new handlers to ensure that they always reply
- Made MMAP request flags field u64 everywhere
- Fixed double memory_region_del_subregion() on UNMAP
- Add mappings cleaning on virtio_reset()
- Some small typos and fixes
- Fixed virtio pci bar mapping for vhost-user-test-device v7->v8
- Unified VhostUserShmemObject and VirtioSharedMemoryMapping
- Refined shmem_obj lifecycle by transferring ownership
- Other small improvements
This patch series implements dynamic fd-backed memory mapping support for vhost-user backends, enabling backends to dynamically request memory mappings and unmappings during runtime through the new VHOST_USER_BACKEND_SHMEM_MAP/UNMAP protocol messages.
This feature benefits various VIRTIO devices that require dynamic shared memory management, including virtiofs (for DAX mappings), virtio-gpu (for resource sharing), and the recently standardized virtio-media.
The implementation introduces a QOM-based architecture for managing shared memory lifecycle:
- VirtioSharedMemoryMapping: an intermediate object that manages individual memory mappings by acting as generic container for regions declared in any vhost-user device type
- Dynamic Mapping: backends can request mappings via SHMEM_MAP messages, with the frontend creating MemoryRegions from the provided file descriptors and adding them as subregions
When a SHMEM_MAP request is received, the frontend:
1. Creates VirtioSharedMemoryMapping to manage the mapping lifecycle
2. Maps the provided fd with memory_region_init_ram_from_fd()
3. Creates a MemoryRegion backed by the mapped memory
4. Adds it as a subregion of the appropiate VIRTIO Shared Memory Region
The QOM reference counting ensures automatic cleanup when mappings are removed or the device is destroyed.
This patch also includes:
- VHOST_USER_GET_SHMEM_CONFIG: a new frontend request allowing generic vhost-user devices to query shared memory configuration from backends at device initialization, enabling the generic vhost-user-device frontend to work with any backend regardless of specific shared memory requirements.
The implementation has been tested with rust-vmm based backends.
Albert Esteve (7):
vhost-user: Add VirtIO Shared Memory map request vhost_user.rst: Align VhostUserMsg excerpt members vhost_user.rst: Add SHMEM_MAP/_UNMAP to spec
vhost_user: Add frontend get_shmem_config command vhost_user.rst: Add GET_SHMEM_CONFIG message
qmp: add shmem feature map
vhost-user-device: Add shared memory BAR
docs/interop/vhost-user.rst | 102 ++++++++ hw/virtio/vhost-user-base.c | 47 +++- hw/virtio/vhost-user-test-device-pci.c | 39 +++- hw/virtio/vhost-user.c | 269 ++++++++++++++++++++++ hw/virtio/virtio-qmp.c | 3 + hw/virtio/virtio.c | 199 ++++++++++++++++ include/hw/virtio/vhost-backend.h | 10 + include/hw/virtio/vhost-user.h | 1 + include/hw/virtio/virtio.h | 137 +++++++++++ subprojects/libvhost-user/libvhost-user.c | 70 ++++++ subprojects/libvhost-user/libvhost-user.h | 54 +++++ 11 files changed, 926 insertions(+), 5 deletions(-)
Signature · 1 line
2.49.0
PATCH v11 1/7 vhost-user: Add VirtIO Shared Memory map request
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.
--- hw/virtio/vhost-user.c | 269 ++++++++++++++++++++++ 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, 727 insertions(+)
hw/virtio/vhost-user.c +269 -0
@@ -104,6 +104,7 @@ typedef enum VhostUserRequest {VHOST_USER_GET_SHARED_OBJECT =41 ,VHOST_USER_SET_DEVICE_STATE_FD =42 ,VHOST_USER_CHECK_DEVICE_STATE =43 , +VHOST_USER_GET_SHMEM_CONFIG =44 , VHOST_USER_MAX } VhostUserRequest;
@@ -115,6 +116,8 @@ typedef enum VhostUserBackendRequest {VHOST_USER_BACKEND_SHARED_OBJECT_ADD =6 ,VHOST_USER_BACKEND_SHARED_OBJECT_REMOVE =7 ,VHOST_USER_BACKEND_SHARED_OBJECT_LOOKUP =8 , +VHOST_USER_BACKEND_SHMEM_MAP =9 , +VHOST_USER_BACKEND_SHMEM_UNMAP =10 , VHOST_USER_BACKEND_MAX } VhostUserBackendRequest;
@@ -136,6 +139,12 @@ typedef struct VhostUserMemRegMsg {VhostUserMemoryRegion region; } VhostUserMemRegMsg; +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; +/* 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;
@@ -224,6 +250,8 @@ typedef union {VhostUserInflight inflight;VhostUserShared object;VhostUserTransferDeviceState transfer_state; +VhostUserMMap mmap; +VhostUserShMemConfig shmem; } VhostUserPayload; typedef struct VhostUserMsg {
@@ -1771,6 +1799,196 @@ vhost_user_backend_handle_shared_object_lookup(struct vhost_user *u,return 0 ; } +/** +* 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 = &payload->mmap; +VirtioSharedMemoryMapping *existing; +Error *local_err =NULL ; +int ret =0 ; + +if (fd <0 ) { +error_report ("Bad fd for map" ); + ret = -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 = -EFAULT ; + goto send_reply; + } + + shmem =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 = -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 = -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 = -EFAULT ; + goto send_reply; + } + } + +memory_region_transaction_begin (); + +/* Create VirtioSharedMemoryMapping object */ +VirtioSharedMemoryMapping *mapping =virtio_shared_memory_mapping_new ( + 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 = -EFAULT ; + goto send_reply_commit; + } + +/* Add the mapping to the shared memory region */ +if (virtio_add_shmem_map (shmem, mapping) !=0 ) { +error_report ("Failed to add shared memory mapping" ); +object_unref (OBJECT (mapping)); + ret = -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 = !!ret; + hdr->size =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 = !!ret; + hdr->size =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 request +* @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. Removes +* the specified memory mapping from the VirtIO shared memory region. This +* automatically unreferences the associated VhostUserShmemObject, which may +* 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 =NULL ; +VhostUserMMap *vu_mmap = &payload->mmap; +Error *local_err =NULL ; +int ret =0 ; + +if (QSIMPLEQ_EMPTY (&dev->vdev->shmem_list)) { +error_report ("Device has no VIRTIO Shared Memory Regions. " +"Requested ID: %d" , vu_mmap->shmid); + ret = -EFAULT ; + goto send_reply; + } + + shmem =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 = -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 = -EFAULT ; + goto send_reply; + } + + mmap =virtio_find_shmem_map (shmem, vu_mmap->shm_offset, vu_mmap->len); +if (!mmap) { +error_report ("Shared memory mapping not found at offset %" PRIx64 +" with length %" PRIx64, + vu_mmap->shm_offset, vu_mmap->len); + ret = -EFAULT ; + goto send_reply; + } + +send_reply: +if (hdr->flags &VHOST_USER_NEED_REPLY_MASK ) { + payload->u64 = !!ret; + hdr->size =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);
@@ -1844,6 +2062,21 @@ static gboolean backend_read(QIOChannel *ioc, GIOCondition condition, ret =vhost_user_backend_handle_shared_object_lookup (dev->opaque, &payload.object);break ; +case VHOST_USER_BACKEND_SHMEM_MAP : +/* Handler manages its own response, check error and close connection */ + reply_ack = false; +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 connection */ + reply_ack = false; +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 = -EINVAL ;
@@ -3021,6 +3254,41 @@ static int vhost_user_check_device_state(struct vhost_dev *dev, Error **errp)return 0 ; } +static int vhost_user_get_shmem_config (struct vhost_dev *dev, +int *nregions, +uint64_t *memory_sizes, +Error **errp) +{ +int ret; +VhostUserMsg msg = { + .hdr.request =VHOST_USER_GET_SHMEM_CONFIG , + .hdr.flags =VHOST_USER_VERSION , + }; + +if (!virtio_has_feature (dev->protocol_features, +VHOST_USER_PROTOCOL_F_SHMEM )) { + *nregions =0 ; +return 0 ; + } + + ret =vhost_user_write (dev, &msg,NULL ,0 ); +if (ret <0 ) { +return ret; + } + + ret =vhost_user_read (dev, &msg); +if (ret <0 ) { +return ret; + } + +assert (msg.payload.shmem.nregions <=VIRTIO_MAX_SHMEM_REGIONS ); + *nregions = 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 = { .backend_type =VHOST_BACKEND_TYPE_USER , .vhost_backend_init = vhost_user_backend_init,
@@ -3059,4 +3327,5 @@ const VhostOps user_ops = { .vhost_supports_device_state = vhost_user_supports_device_state, .vhost_set_device_state_fd = vhost_user_set_device_state_fd, .vhost_check_device_state = vhost_user_check_device_state, + .vhost_get_shmem_config = vhost_user_get_shmem_config, };
hw/virtio/virtio.c +199 -0
243 lines not on this page · raw message
include/hw/virtio/virtio.h +135 -0
153 lines not on this page · raw message
subprojects/libvhost-user/libvhost-user.c +70 -0
76 lines not on this page · raw message
subprojects/libvhost-user/libvhost-user.h +54 -0
84 lines not on this page · raw message
Signature · 1 line
2.49.0
Re: [PATCH v11 1/7] vhost-user: Add VirtIO Shared Memory map request
Quoted 240 lines · Albert Esteve
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 theVHOST_USER_NEED_REPLY_MASKflag, 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_TABLEmessage before the reply.Reviewed-by Stefan Hajnoczi <[email protected]>Signed-off-by Albert Esteve <[email protected]>--- hw/virtio/vhost-user.c | 269 ++++++++++++++++++++++ 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, 727 insertions(+)hw/virtio/vhost-user.c +153
-0@@ -104,6 +104,7 @@ typedef enum VhostUserRequest {VHOST_USER_GET_SHARED_OBJECT =41 ,VHOST_USER_SET_DEVICE_STATE_FD =42 ,VHOST_USER_CHECK_DEVICE_STATE =43 , +VHOST_USER_GET_SHMEM_CONFIG =44 , VHOST_USER_MAX } VhostUserRequest;@@ -115,6 +116,8 @@ typedef enum VhostUserBackendRequest {VHOST_USER_BACKEND_SHARED_OBJECT_ADD =6 ,VHOST_USER_BACKEND_SHARED_OBJECT_REMOVE =7 ,VHOST_USER_BACKEND_SHARED_OBJECT_LOOKUP =8 , +VHOST_USER_BACKEND_SHMEM_MAP =9 , +VHOST_USER_BACKEND_SHMEM_UNMAP =10 , VHOST_USER_BACKEND_MAX } VhostUserBackendRequest;@@ -136,6 +139,12 @@ typedef struct VhostUserMemRegMsg {VhostUserMemoryRegion region; } VhostUserMemRegMsg; +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; +/* 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;@@ -224,6 +250,8 @@ typedef union {VhostUserInflight inflight;VhostUserShared object;VhostUserTransferDeviceState transfer_state; +VhostUserMMap mmap; +VhostUserShMemConfig shmem; } VhostUserPayload; typedef struct VhostUserMsg {@@ -1771,6 +1799,196 @@ vhost_user_backend_handle_shared_object_lookup(struct vhost_user *u,return 0 ; } +/** +* 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 = &payload->mmap; +VirtioSharedMemoryMapping *existing; +Error *local_err =NULL ; +int ret =0 ; + +if (fd <0 ) { +error_report ("Bad fd for map" ); + ret = -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 = -EFAULT ; + goto send_reply; + } + + shmem =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 = -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 = -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 = -EFAULT ; + goto send_reply; + } + } + +memory_region_transaction_begin (); + +/* Create VirtioSharedMemoryMapping object */ +VirtioSharedMemoryMapping *mapping =virtio_shared_memory_mapping_new ( + 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 = -EFAULT ; + goto send_reply_commit; + } + +/* Add the mapping to the shared memory region */ +if (virtio_add_shmem_map (shmem, mapping) !=0 ) { +error_report ("Failed to add shared memory mapping" ); +object_unref (OBJECT (mapping)); + ret = -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 = !!ret; + hdr->size =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 = !!ret; + hdr->size =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 request +* @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. Removes +* the specified memory mapping from the VirtIO shared memory region. This +* automatically unreferences the associated VhostUserShmemObject, which may +* trigger its finalization and cleanup (munmap, close fd) if no other +* references exist.
hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_lookup(struct vhost_user *u, · line 1936
Quoted 10 lines
hw/virtio/vhost-user.c +10
-0+* +* 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;
shmem uninitialized here
hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_lookup(struct vhost_user *u, · line 1937
Quoted 1 line
hw/virtio/vhost-user.c +1
-0+VirtioSharedMemoryMapping *mmap =NULL ;
but mmap is NULL here
hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_lookup(struct vhost_user *u, · line 1844
Quoted 12 lines
hw/virtio/vhost-user.c +12
-0+VhostUserMMap *vu_mmap = &payload->mmap; +Error *local_err =NULL ; +int ret =0 ; + +if (QSIMPLEQ_EMPTY (&dev->vdev->shmem_list)) { +error_report ("Device has no VIRTIO Shared Memory Regions. " +"Requested ID: %d" , vu_mmap->shmid); + ret = -EFAULT ; + goto send_reply; + } + + shmem =virtio_find_shmem_region (dev->vdev, vu_mmap->shmid);
initialized here
Quoted 6 lines
hw/virtio/vhost-user.c +6
-0+if (!shmem) { +error_report ("VIRTIO Shared Memory Region at " +"ID %d not found or uninitialized" , vu_mmap->shmid); + ret = -EFAULT ; + goto send_reply; + }
hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_lookup(struct vhost_user *u, · line 1965
Quoted 10 lines
hw/virtio/vhost-user.c +10
-0+ +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 = -EFAULT ; + goto send_reply; + } + + mmap =virtio_find_shmem_map (shmem, vu_mmap->shm_offset, vu_mmap->len);
before mmap
Quoted 9 lines
hw/virtio/vhost-user.c +9
-0+if (!mmap) { +error_report ("Shared memory mapping not found at offset %" PRIx64 +" with length %" PRIx64, + vu_mmap->shm_offset, vu_mmap->len); + ret = -EFAULT ; + goto send_reply; + } + +send_reply:
hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_lookup(struct vhost_user *u, · line 1984
Quoted 10 lines
hw/virtio/vhost-user.c +10
-0+if (hdr->flags &VHOST_USER_NEED_REPLY_MASK ) { + payload->u64 = !!ret; + hdr->size =sizeof (payload->u64); +if (!vhost_user_send_resp (ioc, hdr, payload, &local_err)) { +error_report_err (local_err); +return -EFAULT ; + } + } + +if (!ret && mmap) {
so there is no path on which mmap is !null but shmem is uninitialized
hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_lookup(struct vhost_user *u, · line 1986
Quoted 2 lines
hw/virtio/vhost-user.c +2
-0+/* Free the MemoryRegion only after reply */ +virtio_del_shmem_map (shmem, vu_mmap->shm_offset, vu_mmap->len);
however gcc is not clever enough to know it:
Quoted 667 lines
hw/virtio/vhost-user.c +56
-0+ } + +return 0 ; +} +static void close_backend_channel (struct vhost_user *u) {g_source_destroy (u->backend_src);@@ -1844,6 +2062,21 @@ static gboolean backend_read(QIOChannel *ioc, GIOCondition condition, ret =vhost_user_backend_handle_shared_object_lookup (dev->opaque, &payload.object);break ; +case VHOST_USER_BACKEND_SHMEM_MAP : +/* Handler manages its own response, check error and close connection */ + reply_ack = false; +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 connection */ + reply_ack = false; +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 = -EINVAL ;@@ -3021,6 +3254,41 @@ static int vhost_user_check_device_state(struct vhost_dev *dev, Error **errp)return 0 ; } +static int vhost_user_get_shmem_config (struct vhost_dev *dev, +int *nregions, +uint64_t *memory_sizes, +Error **errp) +{ +int ret; +VhostUserMsg msg = { + .hdr.request =VHOST_USER_GET_SHMEM_CONFIG , + .hdr.flags =VHOST_USER_VERSION , + }; + +if (!virtio_has_feature (dev->protocol_features, +VHOST_USER_PROTOCOL_F_SHMEM )) { + *nregions =0 ; +return 0 ; + } + + ret =vhost_user_write (dev, &msg,NULL ,0 ); +if (ret <0 ) { +return ret; + } + + ret =vhost_user_read (dev, &msg); +if (ret <0 ) { +return ret; + } + +assert (msg.payload.shmem.nregions <=VIRTIO_MAX_SHMEM_REGIONS ); + *nregions = 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 = { .backend_type =VHOST_BACKEND_TYPE_USER , .vhost_backend_init = vhost_user_backend_init,@@ -3059,4 +3327,5 @@ const VhostOps user_ops = { .vhost_supports_device_state = vhost_user_supports_device_state, .vhost_set_device_state_fd = vhost_user_set_device_state_fd, .vhost_check_device_state = vhost_user_check_device_state, + .vhost_get_shmem_config = vhost_user_get_shmem_config, };hw/virtio/virtio.c +199
-0@@ -3090,6 +3090,173 @@ int virtio_save(VirtIODevice *vdev, QEMUFile *f)return ret; } +VirtioSharedMemory *virtio_new_shmem_region (VirtIODevice *vdev,uint8_t shmid,uint64_t size) +{ +VirtioSharedMemory *elem; + g_autofree char *name = NULL; + + elem =g_new0 (VirtioSharedMemory,1 ); + elem->shmid = shmid; + +/* Initialize embedded MemoryRegion as container for shmem mappings */ + name =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 shmid) +{ +VirtioSharedMemory *shmem, *next; + QSIMPLEQ_FOREACH_SAFE(shmem, &vdev->shmem_list, entry, next) { +if (shmem->shmid == shmid) { +return shmem; + } + } +return NULL ; +} + +static void virtio_shared_memory_mapping_instance_init (Object *obj) +{ +VirtioSharedMemoryMapping *mapping =VIRTIO_SHARED_MEMORY_MAPPING (obj); + + mapping->shmid =0 ; + mapping->offset =0 ; + mapping->len =0 ; + mapping->mr =NULL ; +} + +static void virtio_shared_memory_mapping_instance_finalize (Object *obj) +{ +VirtioSharedMemoryMapping *mapping =VIRTIO_SHARED_MEMORY_MAPPING (obj); + +/* Clean up MemoryRegion if it exists */ +if (mapping->mr) { +/* Unparent the MemoryRegion to trigger cleanup */ +object_unparent (OBJECT (mapping->mr)); + mapping->mr =NULL ; + } +} + +VirtioSharedMemoryMapping *virtio_shared_memory_mapping_new (uint8_t shmid, +int fd, +uint64_t fd_offset, +uint64_t shm_offset, +uint64_t len, +bool allow_write) +{ +VirtioSharedMemoryMapping *mapping; +MemoryRegion *mr; +g_autoptr (GString) mr_name =g_string_new (NULL ); +uint32_t ram_flags; +Error *local_err =NULL ; + +if (len ==0 ) { +error_report ("Shared memory mapping size cannot be zero" ); +return NULL ; + } + + fd =dup (fd); +if (fd <0 ) { +error_report ("Failed to duplicate fd: %s" ,strerror (errno)); +return NULL ; + } + +/* Determine RAM flags */ + ram_flags =RAM_SHARED ; +if (!allow_write) { + ram_flags |=RAM_READONLY_FD ; + } + +/* Create the VirtioSharedMemoryMapping */ + mapping =VIRTIO_SHARED_MEMORY_MAPPING ( +object_new (TYPE_VIRTIO_SHARED_MEMORY_MAPPING )); + +/* Set up object properties */ + mapping->shmid = shmid; + mapping->offset = shm_offset; + mapping->len = len; + +/* Create MemoryRegion as a child of this object */ + mr =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 = 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 == offset && mapping->mr->size == size) { +return mapping; + } + } +return NULL ; +} + +void virtio_del_shmem_map (VirtioSharedMemory *shmem,hwaddr offset, +uint64_t size) +{ +VirtioSharedMemoryMapping *mapping =virtio_find_shmem_map (shmem, offset, size); +if (mapping ==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 *vmdesc)@@ -3216,6 +3383,7 @@ void virtio_reset(void *opaque) {VirtIODevice *vdev = opaque;VirtioDeviceClass *k =VIRTIO_DEVICE_GET_CLASS (vdev); +VirtioSharedMemory *shmem;uint64_t features[VIRTIO_FEATURES_NU64S ];int i;@@ -3255,6 +3423,14 @@ void virtio_reset(void *opaque)for (i =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 =QTAILQ_FIRST (&shmem->mmaps); +virtio_del_shmem_map (shmem, mapping->offset, mapping->mr->size); + } + } } static void virtio_device_check_notification_compatibility(VirtIODevice *vdev,@@ -3578,6 +3754,7 @@ void virtio_init(VirtIODevice *vdev, uint16_t device_id, size_t config_size) NULL, virtio_vmstate_change, vdev); vdev->device_endian =virtio_default_endian (); vdev->use_guest_notifier_mask = true; +QSIMPLEQ_INIT (&vdev->shmem_list); }/* @@ -4089,11 +4266,24 @@ static void virtio_device_free_virtqueues(VirtIODevice *vdev)static void virtio_device_instance_finalize (Object *obj) {VirtIODevice *vdev =VIRTIO_DEVICE (obj); +VirtioSharedMemory *shmem;virtio_device_free_virtqueues (vdev);g_free (vdev->config);g_free (vdev->vector_queues); +while (!QSIMPLEQ_EMPTY (&vdev->shmem_list)) { + shmem =QSIMPLEQ_FIRST (&vdev->shmem_list); +while (!QTAILQ_EMPTY (&shmem->mmaps)) { +VirtioSharedMemoryMapping *mapping =QTAILQ_FIRST (&shmem->mmaps); +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); + } }static const Property virtio_properties[] = {@@ -4459,9 +4649,18 @@ static const TypeInfo virtio_device_info = { .class_size = sizeof(VirtioDeviceClass), }; +static const TypeInfo virtio_shared_memory_mapping_info = { + .name =TYPE_VIRTIO_SHARED_MEMORY_MAPPING , + .parent =TYPE_OBJECT , + .instance_size =sizeof (VirtioSharedMemoryMapping), + .instance_init = virtio_shared_memory_mapping_instance_init, + .instance_finalize = 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); }type_init (virtio_register_types)include/hw/virtio/virtio.h +135
-0@@ -99,6 +99,45 @@ enum virtio_device_endian { VIRTIO_DEVICE_ENDIAN_BIG, }; +#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 +* @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; + +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; };struct VirtioDeviceClass {@@ -298,6 +339,100 @@ void virtio_notify(VirtIODevice *vdev, VirtQueue *vq);int virtio_save (VirtIODevice *vdev,QEMUFile *f); +/** +* 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 failure +*/ +VirtioSharedMemory *virtio_new_shmem_region (VirtIODevice *vdev,uint8_t shmid,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 shmid); + +/** +* virtio_shared_memory_mapping_new() - Create a new VirtioSharedMemoryMapping +* @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 mapping. +* The object will create a MemoryRegion using memory_region_init_ram_from_fd() +* as a child object. When the object is finalized, it will automatically +* clean up the MemoryRegion and close the file descriptor. +* +* Return: A new VirtioSharedMemoryMapping on success, NULL on error. +*/ +VirtioSharedMemoryMapping *virtio_shared_memory_mapping_new (uint8_t shmid, +int fd, +uint64_t fd_offset, +uint64_t shm_offset, +uint64_t len, +bool allow_write); + +/** +* 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 VirtioSharedMemoryMapping +* ownership is transferred to the shared memory region and will be automatically +* 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 VirtioSharedMemory +* 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;#define VMSTATE_VIRTIO_DEVICE \subprojects/libvhost-user/libvhost-user.c +70
-0@@ -1592,6 +1592,76 @@ vu_rm_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN])return vu_send_message (dev, &msg); } +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 = { + .request =VHOST_USER_BACKEND_SHMEM_MAP , + .size =sizeof (vmsg.payload.mmap), + .flags =VHOST_USER_VERSION , + .payload.mmap = { + .shmid = shmid, + .fd_offset = fd_offset, + .shm_offset = shm_offset, + .len = len, + .flags = flags, + }, + .fd_num =1 , + .fds[0 ] = 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 |=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 len) +{ +VhostUserMsg vmsg = { + .request =VHOST_USER_BACKEND_SHMEM_UNMAP , + .size =sizeof (vmsg.payload.mmap), + .flags =VHOST_USER_VERSION , + .payload.mmap = { + .shmid = shmid, + .fd_offset =0 , + .shm_offset = shm_offset, + .len = 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 |=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) {subprojects/libvhost-user/libvhost-user.h +54
-0@@ -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 =18 , +/* Feature 19 is reserved for VHOST_USER_PROTOCOL_F_DEVICE_STATE */ +VHOST_USER_PROTOCOL_F_SHMEM =20 , VHOST_USER_PROTOCOL_F_MAX };@@ -127,6 +129,8 @@ typedef enum VhostUserBackendRequest {VHOST_USER_BACKEND_SHARED_OBJECT_ADD =6 ,VHOST_USER_BACKEND_SHARED_OBJECT_REMOVE =7 ,VHOST_USER_BACKEND_SHARED_OBJECT_LOOKUP =8 , +VHOST_USER_BACKEND_SHMEM_MAP =9 , +VHOST_USER_BACKEND_SHMEM_UNMAP =10 , VHOST_USER_BACKEND_MAX } VhostUserBackendRequest;@@ -186,6 +190,23 @@ typedef struct VhostUserShared {unsigned char uuid[UUID_LEN ]; } VhostUserShared; +/* 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)) typedef struct VhostUserMsg {@@ -210,6 +231,7 @@ typedef struct VhostUserMsg {VhostUserVringArea area;VhostUserInflight inflight;VhostUserShared object; +VhostUserMMap mmap; } payload;int fds[VHOST_MEMORY_BASELINE_NREGIONS ];@@ -593,6 +615,38 @@ bool vu_add_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN]);*/ bool vu_rm_shared_object (VuDev *dev,unsigned char uuid[UUID_LEN ]); +/** +* 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 Region. +* +* 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 fd); + +/** +* 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 Signature · 1 line
2.49.0
Re: [PATCH v11 1/7] vhost-user: Add VirtIO Shared Memory map request
In reply to Albert EsteveQuoted 29 lines · Albert Esteve
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 theVHOST_USER_NEED_REPLY_MASKflag, explicit error checking ensures proper connection closure on failures, maintaining the expected error flow.
Quoted 7 lines
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_TABLEmessage before the reply.Reviewed-by Stefan Hajnoczi <[email protected]>Signed-off-by Albert Esteve <[email protected]>
breaks windows build:
https://gitlab.com/mstredhat/qemu/-/jobs/12974866673
./hw/virtio/virtio.c: In function 'virtio_shared_memory_mapping_new':../hw/virtio/virtio.c:3209:10: error: implicit declaration of function 'memory_region_init_ram_from_fd'; did you mean 'memory_region_init_ram_ptr'? [-Wimplicit-function-declaration]
3209 | if (!memory_region_init_ram_from_fd(mr, OBJECT(mapping), mr_name->str,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| memory_region_init_ram_ptr../hw/virtio/virtio.c:3209:10: error: nested extern declaration of 'memory_region_init_ram_from_fd' [-Werror=nested-externs]
cc1: all warnings being treated as errors
this needs stubs.
Quoted 931 lines
--- hw/virtio/vhost-user.c | 269 ++++++++++++++++++++++ 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, 727 insertions(+)hw/virtio/vhost-user.c +269
-0@@ -104,6 +104,7 @@ typedef enum VhostUserRequest {VHOST_USER_GET_SHARED_OBJECT =41 ,VHOST_USER_SET_DEVICE_STATE_FD =42 ,VHOST_USER_CHECK_DEVICE_STATE =43 , +VHOST_USER_GET_SHMEM_CONFIG =44 , VHOST_USER_MAX } VhostUserRequest;@@ -115,6 +116,8 @@ typedef enum VhostUserBackendRequest {VHOST_USER_BACKEND_SHARED_OBJECT_ADD =6 ,VHOST_USER_BACKEND_SHARED_OBJECT_REMOVE =7 ,VHOST_USER_BACKEND_SHARED_OBJECT_LOOKUP =8 , +VHOST_USER_BACKEND_SHMEM_MAP =9 , +VHOST_USER_BACKEND_SHMEM_UNMAP =10 , VHOST_USER_BACKEND_MAX } VhostUserBackendRequest;@@ -136,6 +139,12 @@ typedef struct VhostUserMemRegMsg {VhostUserMemoryRegion region; } VhostUserMemRegMsg; +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; +/* 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;@@ -224,6 +250,8 @@ typedef union {VhostUserInflight inflight;VhostUserShared object;VhostUserTransferDeviceState transfer_state; +VhostUserMMap mmap; +VhostUserShMemConfig shmem; } VhostUserPayload; typedef struct VhostUserMsg {@@ -1771,6 +1799,196 @@ vhost_user_backend_handle_shared_object_lookup(struct vhost_user *u,return 0 ; } +/** +* 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 = &payload->mmap; +VirtioSharedMemoryMapping *existing; +Error *local_err =NULL ; +int ret =0 ; + +if (fd <0 ) { +error_report ("Bad fd for map" ); + ret = -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 = -EFAULT ; + goto send_reply; + } + + shmem =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 = -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 = -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 = -EFAULT ; + goto send_reply; + } + } + +memory_region_transaction_begin (); + +/* Create VirtioSharedMemoryMapping object */ +VirtioSharedMemoryMapping *mapping =virtio_shared_memory_mapping_new ( + 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 = -EFAULT ; + goto send_reply_commit; + } + +/* Add the mapping to the shared memory region */ +if (virtio_add_shmem_map (shmem, mapping) !=0 ) { +error_report ("Failed to add shared memory mapping" ); +object_unref (OBJECT (mapping)); + ret = -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 = !!ret; + hdr->size =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 = !!ret; + hdr->size =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 request +* @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. Removes +* the specified memory mapping from the VirtIO shared memory region. This +* automatically unreferences the associated VhostUserShmemObject, which may +* 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 =NULL ; +VhostUserMMap *vu_mmap = &payload->mmap; +Error *local_err =NULL ; +int ret =0 ; + +if (QSIMPLEQ_EMPTY (&dev->vdev->shmem_list)) { +error_report ("Device has no VIRTIO Shared Memory Regions. " +"Requested ID: %d" , vu_mmap->shmid); + ret = -EFAULT ; + goto send_reply; + } + + shmem =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 = -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 = -EFAULT ; + goto send_reply; + } + + mmap =virtio_find_shmem_map (shmem, vu_mmap->shm_offset, vu_mmap->len); +if (!mmap) { +error_report ("Shared memory mapping not found at offset %" PRIx64 +" with length %" PRIx64, + vu_mmap->shm_offset, vu_mmap->len); + ret = -EFAULT ; + goto send_reply; + } + +send_reply: +if (hdr->flags &VHOST_USER_NEED_REPLY_MASK ) { + payload->u64 = !!ret; + hdr->size =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);@@ -1844,6 +2062,21 @@ static gboolean backend_read(QIOChannel *ioc, GIOCondition condition, ret =vhost_user_backend_handle_shared_object_lookup (dev->opaque, &payload.object);break ; +case VHOST_USER_BACKEND_SHMEM_MAP : +/* Handler manages its own response, check error and close connection */ + reply_ack = false; +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 connection */ + reply_ack = false; +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 = -EINVAL ;@@ -3021,6 +3254,41 @@ static int vhost_user_check_device_state(struct vhost_dev *dev, Error **errp)return 0 ; } +static int vhost_user_get_shmem_config (struct vhost_dev *dev, +int *nregions, +uint64_t *memory_sizes, +Error **errp) +{ +int ret; +VhostUserMsg msg = { + .hdr.request =VHOST_USER_GET_SHMEM_CONFIG , + .hdr.flags =VHOST_USER_VERSION , + }; + +if (!virtio_has_feature (dev->protocol_features, +VHOST_USER_PROTOCOL_F_SHMEM )) { + *nregions =0 ; +return 0 ; + } + + ret =vhost_user_write (dev, &msg,NULL ,0 ); +if (ret <0 ) { +return ret; + } + + ret =vhost_user_read (dev, &msg); +if (ret <0 ) { +return ret; + } + +assert (msg.payload.shmem.nregions <=VIRTIO_MAX_SHMEM_REGIONS ); + *nregions = 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 = { .backend_type =VHOST_BACKEND_TYPE_USER , .vhost_backend_init = vhost_user_backend_init,@@ -3059,4 +3327,5 @@ const VhostOps user_ops = { .vhost_supports_device_state = vhost_user_supports_device_state, .vhost_set_device_state_fd = vhost_user_set_device_state_fd, .vhost_check_device_state = vhost_user_check_device_state, + .vhost_get_shmem_config = vhost_user_get_shmem_config, };hw/virtio/virtio.c +199
-0@@ -3090,6 +3090,173 @@ int virtio_save(VirtIODevice *vdev, QEMUFile *f)return ret; } +VirtioSharedMemory *virtio_new_shmem_region (VirtIODevice *vdev,uint8_t shmid,uint64_t size) +{ +VirtioSharedMemory *elem; + g_autofree char *name = NULL; + + elem =g_new0 (VirtioSharedMemory,1 ); + elem->shmid = shmid; + +/* Initialize embedded MemoryRegion as container for shmem mappings */ + name =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 shmid) +{ +VirtioSharedMemory *shmem, *next; + QSIMPLEQ_FOREACH_SAFE(shmem, &vdev->shmem_list, entry, next) { +if (shmem->shmid == shmid) { +return shmem; + } + } +return NULL ; +} + +static void virtio_shared_memory_mapping_instance_init (Object *obj) +{ +VirtioSharedMemoryMapping *mapping =VIRTIO_SHARED_MEMORY_MAPPING (obj); + + mapping->shmid =0 ; + mapping->offset =0 ; + mapping->len =0 ; + mapping->mr =NULL ; +} + +static void virtio_shared_memory_mapping_instance_finalize (Object *obj) +{ +VirtioSharedMemoryMapping *mapping =VIRTIO_SHARED_MEMORY_MAPPING (obj); + +/* Clean up MemoryRegion if it exists */ +if (mapping->mr) { +/* Unparent the MemoryRegion to trigger cleanup */ +object_unparent (OBJECT (mapping->mr)); + mapping->mr =NULL ; + } +} + +VirtioSharedMemoryMapping *virtio_shared_memory_mapping_new (uint8_t shmid, +int fd, +uint64_t fd_offset, +uint64_t shm_offset, +uint64_t len, +bool allow_write) +{ +VirtioSharedMemoryMapping *mapping; +MemoryRegion *mr; +g_autoptr (GString) mr_name =g_string_new (NULL ); +uint32_t ram_flags; +Error *local_err =NULL ; + +if (len ==0 ) { +error_report ("Shared memory mapping size cannot be zero" ); +return NULL ; + } + + fd =dup (fd); +if (fd <0 ) { +error_report ("Failed to duplicate fd: %s" ,strerror (errno)); +return NULL ; + } + +/* Determine RAM flags */ + ram_flags =RAM_SHARED ; +if (!allow_write) { + ram_flags |=RAM_READONLY_FD ; + } + +/* Create the VirtioSharedMemoryMapping */ + mapping =VIRTIO_SHARED_MEMORY_MAPPING ( +object_new (TYPE_VIRTIO_SHARED_MEMORY_MAPPING )); + +/* Set up object properties */ + mapping->shmid = shmid; + mapping->offset = shm_offset; + mapping->len = len; + +/* Create MemoryRegion as a child of this object */ + mr =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 = 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 == offset && mapping->mr->size == size) { +return mapping; + } + } +return NULL ; +} + +void virtio_del_shmem_map (VirtioSharedMemory *shmem,hwaddr offset, +uint64_t size) +{ +VirtioSharedMemoryMapping *mapping =virtio_find_shmem_map (shmem, offset, size); +if (mapping ==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 *vmdesc)@@ -3216,6 +3383,7 @@ void virtio_reset(void *opaque) {VirtIODevice *vdev = opaque;VirtioDeviceClass *k =VIRTIO_DEVICE_GET_CLASS (vdev); +VirtioSharedMemory *shmem;uint64_t features[VIRTIO_FEATURES_NU64S ];int i;@@ -3255,6 +3423,14 @@ void virtio_reset(void *opaque)for (i =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 =QTAILQ_FIRST (&shmem->mmaps); +virtio_del_shmem_map (shmem, mapping->offset, mapping->mr->size); + } + } } static void virtio_device_check_notification_compatibility(VirtIODevice *vdev,@@ -3578,6 +3754,7 @@ void virtio_init(VirtIODevice *vdev, uint16_t device_id, size_t config_size) NULL, virtio_vmstate_change, vdev); vdev->device_endian =virtio_default_endian (); vdev->use_guest_notifier_mask = true; +QSIMPLEQ_INIT (&vdev->shmem_list); }/* @@ -4089,11 +4266,24 @@ static void virtio_device_free_virtqueues(VirtIODevice *vdev)static void virtio_device_instance_finalize (Object *obj) {VirtIODevice *vdev =VIRTIO_DEVICE (obj); +VirtioSharedMemory *shmem;virtio_device_free_virtqueues (vdev);g_free (vdev->config);g_free (vdev->vector_queues); +while (!QSIMPLEQ_EMPTY (&vdev->shmem_list)) { + shmem =QSIMPLEQ_FIRST (&vdev->shmem_list); +while (!QTAILQ_EMPTY (&shmem->mmaps)) { +VirtioSharedMemoryMapping *mapping =QTAILQ_FIRST (&shmem->mmaps); +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); + } }static const Property virtio_properties[] = {@@ -4459,9 +4649,18 @@ static const TypeInfo virtio_device_info = { .class_size = sizeof(VirtioDeviceClass), }; +static const TypeInfo virtio_shared_memory_mapping_info = { + .name =TYPE_VIRTIO_SHARED_MEMORY_MAPPING , + .parent =TYPE_OBJECT , + .instance_size =sizeof (VirtioSharedMemoryMapping), + .instance_init = virtio_shared_memory_mapping_instance_init, + .instance_finalize = 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); }type_init (virtio_register_types)include/hw/virtio/virtio.h +135
-0@@ -99,6 +99,45 @@ enum virtio_device_endian { VIRTIO_DEVICE_ENDIAN_BIG, }; +#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 +* @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; + +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; };struct VirtioDeviceClass {@@ -298,6 +339,100 @@ void virtio_notify(VirtIODevice *vdev, VirtQueue *vq);int virtio_save (VirtIODevice *vdev,QEMUFile *f); +/** +* 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 failure +*/ +VirtioSharedMemory *virtio_new_shmem_region (VirtIODevice *vdev,uint8_t shmid,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 shmid); + +/** +* virtio_shared_memory_mapping_new() - Create a new VirtioSharedMemoryMapping +* @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 mapping. +* The object will create a MemoryRegion using memory_region_init_ram_from_fd() +* as a child object. When the object is finalized, it will automatically +* clean up the MemoryRegion and close the file descriptor. +* +* Return: A new VirtioSharedMemoryMapping on success, NULL on error. +*/ +VirtioSharedMemoryMapping *virtio_shared_memory_mapping_new (uint8_t shmid, +int fd, +uint64_t fd_offset, +uint64_t shm_offset, +uint64_t len, +bool allow_write); + +/** +* 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 VirtioSharedMemoryMapping +* ownership is transferred to the shared memory region and will be automatically +* 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 VirtioSharedMemory +* 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;#define VMSTATE_VIRTIO_DEVICE \subprojects/libvhost-user/libvhost-user.c +70
-0@@ -1592,6 +1592,76 @@ vu_rm_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN])return vu_send_message (dev, &msg); } +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 = { + .request =VHOST_USER_BACKEND_SHMEM_MAP , + .size =sizeof (vmsg.payload.mmap), + .flags =VHOST_USER_VERSION , + .payload.mmap = { + .shmid = shmid, + .fd_offset = fd_offset, + .shm_offset = shm_offset, + .len = len, + .flags = flags, + }, + .fd_num =1 , + .fds[0 ] = 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 |=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 len) +{ +VhostUserMsg vmsg = { + .request =VHOST_USER_BACKEND_SHMEM_UNMAP , + .size =sizeof (vmsg.payload.mmap), + .flags =VHOST_USER_VERSION , + .payload.mmap = { + .shmid = shmid, + .fd_offset =0 , + .shm_offset = shm_offset, + .len = 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 |=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) {subprojects/libvhost-user/libvhost-user.h +54
-0@@ -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 =18 , +/* Feature 19 is reserved for VHOST_USER_PROTOCOL_F_DEVICE_STATE */ +VHOST_USER_PROTOCOL_F_SHMEM =20 , VHOST_USER_PROTOCOL_F_MAX };@@ -127,6 +129,8 @@ typedef enum VhostUserBackendRequest {VHOST_USER_BACKEND_SHARED_OBJECT_ADD =6 ,VHOST_USER_BACKEND_SHARED_OBJECT_REMOVE =7 ,VHOST_USER_BACKEND_SHARED_OBJECT_LOOKUP =8 , +VHOST_USER_BACKEND_SHMEM_MAP =9 , +VHOST_USER_BACKEND_SHMEM_UNMAP =10 , VHOST_USER_BACKEND_MAX } VhostUserBackendRequest;@@ -186,6 +190,23 @@ typedef struct VhostUserShared {unsigned char uuid[UUID_LEN ]; } VhostUserShared; +/* 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)) typedef struct VhostUserMsg {@@ -210,6 +231,7 @@ typedef struct VhostUserMsg {VhostUserVringArea area;VhostUserInflight inflight;VhostUserShared object; +VhostUserMMap mmap; } payload;int fds[VHOST_MEMORY_BASELINE_NREGIONS ];@@ -593,6 +615,38 @@ bool vu_add_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN]);*/ bool vu_rm_shared_object (VuDev *dev,unsigned char uuid[UUID_LEN ]); +/** +* 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 Region. +* +* 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 fd); + +/** +* 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 Signature · 1 line
2.49.0
Re: [PATCH v11 1/7] vhost-user: Add VirtIO Shared Memory map request
In reply to Albert EsteveQuoted 500 lines · Albert Esteve
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 theVHOST_USER_NEED_REPLY_MASKflag, 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_TABLEmessage before the reply.Reviewed-by Stefan Hajnoczi <[email protected]>Signed-off-by Albert Esteve <[email protected]>--- hw/virtio/vhost-user.c | 269 ++++++++++++++++++++++ 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, 727 insertions(+)hw/virtio/vhost-user.c +269
-0@@ -104,6 +104,7 @@ typedef enum VhostUserRequest {VHOST_USER_GET_SHARED_OBJECT =41 ,VHOST_USER_SET_DEVICE_STATE_FD =42 ,VHOST_USER_CHECK_DEVICE_STATE =43 , +VHOST_USER_GET_SHMEM_CONFIG =44 , VHOST_USER_MAX } VhostUserRequest;@@ -115,6 +116,8 @@ typedef enum VhostUserBackendRequest {VHOST_USER_BACKEND_SHARED_OBJECT_ADD =6 ,VHOST_USER_BACKEND_SHARED_OBJECT_REMOVE =7 ,VHOST_USER_BACKEND_SHARED_OBJECT_LOOKUP =8 , +VHOST_USER_BACKEND_SHMEM_MAP =9 , +VHOST_USER_BACKEND_SHMEM_UNMAP =10 , VHOST_USER_BACKEND_MAX } VhostUserBackendRequest;@@ -136,6 +139,12 @@ typedef struct VhostUserMemRegMsg {VhostUserMemoryRegion region; } VhostUserMemRegMsg; +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; +/* 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;@@ -224,6 +250,8 @@ typedef union {VhostUserInflight inflight;VhostUserShared object;VhostUserTransferDeviceState transfer_state; +VhostUserMMap mmap; +VhostUserShMemConfig shmem; } VhostUserPayload; typedef struct VhostUserMsg {@@ -1771,6 +1799,196 @@ vhost_user_backend_handle_shared_object_lookup(struct vhost_user *u,return 0 ; } +/** +* 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 = &payload->mmap; +VirtioSharedMemoryMapping *existing; +Error *local_err =NULL ; +int ret =0 ; + +if (fd <0 ) { +error_report ("Bad fd for map" ); + ret = -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 = -EFAULT ; + goto send_reply; + } + + shmem =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 = -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 = -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 = -EFAULT ; + goto send_reply; + } + } + +memory_region_transaction_begin (); + +/* Create VirtioSharedMemoryMapping object */ +VirtioSharedMemoryMapping *mapping =virtio_shared_memory_mapping_new ( + 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 = -EFAULT ; + goto send_reply_commit; + } + +/* Add the mapping to the shared memory region */ +if (virtio_add_shmem_map (shmem, mapping) !=0 ) { +error_report ("Failed to add shared memory mapping" ); +object_unref (OBJECT (mapping)); + ret = -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 = !!ret; + hdr->size =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 = !!ret; + hdr->size =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 request +* @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. Removes +* the specified memory mapping from the VirtIO shared memory region. This +* automatically unreferences the associated VhostUserShmemObject, which may +* 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 =NULL ; +VhostUserMMap *vu_mmap = &payload->mmap; +Error *local_err =NULL ; +int ret =0 ; + +if (QSIMPLEQ_EMPTY (&dev->vdev->shmem_list)) { +error_report ("Device has no VIRTIO Shared Memory Regions. " +"Requested ID: %d" , vu_mmap->shmid); + ret = -EFAULT ; + goto send_reply; + } + + shmem =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 = -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 = -EFAULT ; + goto send_reply; + } + + mmap =virtio_find_shmem_map (shmem, vu_mmap->shm_offset, vu_mmap->len); +if (!mmap) { +error_report ("Shared memory mapping not found at offset %" PRIx64 +" with length %" PRIx64, + vu_mmap->shm_offset, vu_mmap->len); + ret = -EFAULT ; + goto send_reply; + } + +send_reply: +if (hdr->flags &VHOST_USER_NEED_REPLY_MASK ) { + payload->u64 = !!ret; + hdr->size =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);@@ -1844,6 +2062,21 @@ static gboolean backend_read(QIOChannel *ioc, GIOCondition condition, ret =vhost_user_backend_handle_shared_object_lookup (dev->opaque, &payload.object);break ; +case VHOST_USER_BACKEND_SHMEM_MAP : +/* Handler manages its own response, check error and close connection */ + reply_ack = false; +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 connection */ + reply_ack = false; +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 = -EINVAL ;@@ -3021,6 +3254,41 @@ static int vhost_user_check_device_state(struct vhost_dev *dev, Error **errp)return 0 ; } +static int vhost_user_get_shmem_config (struct vhost_dev *dev, +int *nregions, +uint64_t *memory_sizes, +Error **errp) +{ +int ret; +VhostUserMsg msg = { + .hdr.request =VHOST_USER_GET_SHMEM_CONFIG , + .hdr.flags =VHOST_USER_VERSION , + }; + +if (!virtio_has_feature (dev->protocol_features, +VHOST_USER_PROTOCOL_F_SHMEM )) { + *nregions =0 ; +return 0 ; + } + + ret =vhost_user_write (dev, &msg,NULL ,0 ); +if (ret <0 ) { +return ret; + } + + ret =vhost_user_read (dev, &msg); +if (ret <0 ) { +return ret; + } + +assert (msg.payload.shmem.nregions <=VIRTIO_MAX_SHMEM_REGIONS ); + *nregions = 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 = { .backend_type =VHOST_BACKEND_TYPE_USER , .vhost_backend_init = vhost_user_backend_init,@@ -3059,4 +3327,5 @@ const VhostOps user_ops = { .vhost_supports_device_state = vhost_user_supports_device_state, .vhost_set_device_state_fd = vhost_user_set_device_state_fd, .vhost_check_device_state = vhost_user_check_device_state, + .vhost_get_shmem_config = vhost_user_get_shmem_config, };hw/virtio/virtio.c +114
-0@@ -3090,6 +3090,173 @@ int virtio_save(VirtIODevice *vdev, QEMUFile *f)return ret; } +VirtioSharedMemory *virtio_new_shmem_region (VirtIODevice *vdev,uint8_t shmid,uint64_t size) +{ +VirtioSharedMemory *elem; + g_autofree char *name = NULL; + + elem =g_new0 (VirtioSharedMemory,1 ); + elem->shmid = shmid; + +/* Initialize embedded MemoryRegion as container for shmem mappings */ + name =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 shmid) +{ +VirtioSharedMemory *shmem, *next; + QSIMPLEQ_FOREACH_SAFE(shmem, &vdev->shmem_list, entry, next) { +if (shmem->shmid == shmid) { +return shmem; + } + } +return NULL ; +} + +static void virtio_shared_memory_mapping_instance_init (Object *obj) +{ +VirtioSharedMemoryMapping *mapping =VIRTIO_SHARED_MEMORY_MAPPING (obj); + + mapping->shmid =0 ; + mapping->offset =0 ; + mapping->len =0 ; + mapping->mr =NULL ; +} + +static void virtio_shared_memory_mapping_instance_finalize (Object *obj) +{ +VirtioSharedMemoryMapping *mapping =VIRTIO_SHARED_MEMORY_MAPPING (obj); + +/* Clean up MemoryRegion if it exists */ +if (mapping->mr) { +/* Unparent the MemoryRegion to trigger cleanup */ +object_unparent (OBJECT (mapping->mr)); + mapping->mr =NULL ; + } +} + +VirtioSharedMemoryMapping *virtio_shared_memory_mapping_new (uint8_t shmid, +int fd, +uint64_t fd_offset, +uint64_t shm_offset, +uint64_t len, +bool allow_write) +{ +VirtioSharedMemoryMapping *mapping; +MemoryRegion *mr; +g_autoptr (GString) mr_name =g_string_new (NULL ); +uint32_t ram_flags; +Error *local_err =NULL ; + +if (len ==0 ) { +error_report ("Shared memory mapping size cannot be zero" ); +return NULL ; + } + + fd =dup (fd); +if (fd <0 ) { +error_report ("Failed to duplicate fd: %s" ,strerror (errno)); +return NULL ; + } + +/* Determine RAM flags */ + ram_flags =RAM_SHARED ; +if (!allow_write) { + ram_flags |=RAM_READONLY_FD ; + } + +/* Create the VirtioSharedMemoryMapping */ + mapping =VIRTIO_SHARED_MEMORY_MAPPING ( +object_new (TYPE_VIRTIO_SHARED_MEMORY_MAPPING )); + +/* Set up object properties */ + mapping->shmid = shmid; + mapping->offset = shm_offset; + mapping->len = len; + +/* Create MemoryRegion as a child of this object */ + mr =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 = mr; +return mapping; +} + +int virtio_add_shmem_map (VirtioSharedMemory *shmem, +VirtioSharedMemoryMapping *mapping) +{ +if (!mapping) { +error_report ("VirtioSharedMemoryMapping cannot be NULL" ); +return -1 ; + }
hw/virtio/virtio.c int virtio_save(VirtIODevice *vdev, QEMUFile *f) · line 3216
Quoted 10 lines
hw/virtio/virtio.c +10
-0+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 ; + }
this gives errors like:
../hw/virtio/virtio.c:3255:60: error: invalid operands to binary expression ('Int128' (aka 'struct Int128') and 'uint64_t' (aka 'unsigned long long'))
Quoted 457 lines
hw/virtio/virtio.c +75
-0+ +/* 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 == offset && mapping->mr->size == size) { +return mapping; + } + } +return NULL ; +} + +void virtio_del_shmem_map (VirtioSharedMemory *shmem,hwaddr offset, +uint64_t size) +{ +VirtioSharedMemoryMapping *mapping =virtio_find_shmem_map (shmem, offset, size); +if (mapping ==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 *vmdesc)@@ -3216,6 +3383,7 @@ void virtio_reset(void *opaque) {VirtIODevice *vdev = opaque;VirtioDeviceClass *k =VIRTIO_DEVICE_GET_CLASS (vdev); +VirtioSharedMemory *shmem;uint64_t features[VIRTIO_FEATURES_NU64S ];int i;@@ -3255,6 +3423,14 @@ void virtio_reset(void *opaque)for (i =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 =QTAILQ_FIRST (&shmem->mmaps); +virtio_del_shmem_map (shmem, mapping->offset, mapping->mr->size); + } + } } static void virtio_device_check_notification_compatibility(VirtIODevice *vdev,@@ -3578,6 +3754,7 @@ void virtio_init(VirtIODevice *vdev, uint16_t device_id, size_t config_size) NULL, virtio_vmstate_change, vdev); vdev->device_endian =virtio_default_endian (); vdev->use_guest_notifier_mask = true; +QSIMPLEQ_INIT (&vdev->shmem_list); }/* @@ -4089,11 +4266,24 @@ static void virtio_device_free_virtqueues(VirtIODevice *vdev)static void virtio_device_instance_finalize (Object *obj) {VirtIODevice *vdev =VIRTIO_DEVICE (obj); +VirtioSharedMemory *shmem;virtio_device_free_virtqueues (vdev);g_free (vdev->config);g_free (vdev->vector_queues); +while (!QSIMPLEQ_EMPTY (&vdev->shmem_list)) { + shmem =QSIMPLEQ_FIRST (&vdev->shmem_list); +while (!QTAILQ_EMPTY (&shmem->mmaps)) { +VirtioSharedMemoryMapping *mapping =QTAILQ_FIRST (&shmem->mmaps); +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); + } }static const Property virtio_properties[] = {@@ -4459,9 +4649,18 @@ static const TypeInfo virtio_device_info = { .class_size = sizeof(VirtioDeviceClass), }; +static const TypeInfo virtio_shared_memory_mapping_info = { + .name =TYPE_VIRTIO_SHARED_MEMORY_MAPPING , + .parent =TYPE_OBJECT , + .instance_size =sizeof (VirtioSharedMemoryMapping), + .instance_init = virtio_shared_memory_mapping_instance_init, + .instance_finalize = 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); }type_init (virtio_register_types)include/hw/virtio/virtio.h +135
-0@@ -99,6 +99,45 @@ enum virtio_device_endian { VIRTIO_DEVICE_ENDIAN_BIG, }; +#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 +* @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; + +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; };struct VirtioDeviceClass {@@ -298,6 +339,100 @@ void virtio_notify(VirtIODevice *vdev, VirtQueue *vq);int virtio_save (VirtIODevice *vdev,QEMUFile *f); +/** +* 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 failure +*/ +VirtioSharedMemory *virtio_new_shmem_region (VirtIODevice *vdev,uint8_t shmid,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 shmid); + +/** +* virtio_shared_memory_mapping_new() - Create a new VirtioSharedMemoryMapping +* @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 mapping. +* The object will create a MemoryRegion using memory_region_init_ram_from_fd() +* as a child object. When the object is finalized, it will automatically +* clean up the MemoryRegion and close the file descriptor. +* +* Return: A new VirtioSharedMemoryMapping on success, NULL on error. +*/ +VirtioSharedMemoryMapping *virtio_shared_memory_mapping_new (uint8_t shmid, +int fd, +uint64_t fd_offset, +uint64_t shm_offset, +uint64_t len, +bool allow_write); + +/** +* 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 VirtioSharedMemoryMapping +* ownership is transferred to the shared memory region and will be automatically +* 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 VirtioSharedMemory +* 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;#define VMSTATE_VIRTIO_DEVICE \subprojects/libvhost-user/libvhost-user.c +70
-0@@ -1592,6 +1592,76 @@ vu_rm_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN])return vu_send_message (dev, &msg); } +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 = { + .request =VHOST_USER_BACKEND_SHMEM_MAP , + .size =sizeof (vmsg.payload.mmap), + .flags =VHOST_USER_VERSION , + .payload.mmap = { + .shmid = shmid, + .fd_offset = fd_offset, + .shm_offset = shm_offset, + .len = len, + .flags = flags, + }, + .fd_num =1 , + .fds[0 ] = 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 |=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 len) +{ +VhostUserMsg vmsg = { + .request =VHOST_USER_BACKEND_SHMEM_UNMAP , + .size =sizeof (vmsg.payload.mmap), + .flags =VHOST_USER_VERSION , + .payload.mmap = { + .shmid = shmid, + .fd_offset =0 , + .shm_offset = shm_offset, + .len = 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 |=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) {subprojects/libvhost-user/libvhost-user.h +54
-0@@ -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 =18 , +/* Feature 19 is reserved for VHOST_USER_PROTOCOL_F_DEVICE_STATE */ +VHOST_USER_PROTOCOL_F_SHMEM =20 , VHOST_USER_PROTOCOL_F_MAX };@@ -127,6 +129,8 @@ typedef enum VhostUserBackendRequest {VHOST_USER_BACKEND_SHARED_OBJECT_ADD =6 ,VHOST_USER_BACKEND_SHARED_OBJECT_REMOVE =7 ,VHOST_USER_BACKEND_SHARED_OBJECT_LOOKUP =8 , +VHOST_USER_BACKEND_SHMEM_MAP =9 , +VHOST_USER_BACKEND_SHMEM_UNMAP =10 , VHOST_USER_BACKEND_MAX } VhostUserBackendRequest;@@ -186,6 +190,23 @@ typedef struct VhostUserShared {unsigned char uuid[UUID_LEN ]; } VhostUserShared; +/* 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)) typedef struct VhostUserMsg {@@ -210,6 +231,7 @@ typedef struct VhostUserMsg {VhostUserVringArea area;VhostUserInflight inflight;VhostUserShared object; +VhostUserMMap mmap; } payload;int fds[VHOST_MEMORY_BASELINE_NREGIONS ];@@ -593,6 +615,38 @@ bool vu_add_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN]);*/ bool vu_rm_shared_object (VuDev *dev,unsigned char uuid[UUID_LEN ]); +/** +* 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 Region. +* +* 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 fd); + +/** +* 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 Signature · 1 line
2.49.0
PATCH v11 2/7 vhost_user.rst: Align VhostUserMsg excerpt members
In reply to Albert EsteveAdd missing members to the VhostUserMsg excerpt in the vhost-user spec documentation.
--- docs/interop/vhost-user.rst | 4 ++++ 1 file changed, 4 insertions(+)
docs/interop/vhost-user.rst +4 -0
@@ -366,11 +366,15 @@ In QEMU the vhost-user message is implemented with the 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;
Signature · 1 line
2.49.0
PATCH v11 3/7 vhost_user.rst: Add SHMEM_MAP/_UNMAP to spec
In reply to Albert EsteveAdd SHMEM_MAP/_UNMAP request to the vhost-user
spec documentation.
--- docs/interop/vhost-user.rst | 59 +++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+)
docs/interop/vhost-user.rst +59 -0
@@ -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. +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 -----------
@@ -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;
@@ -1064,6 +1086,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 Front-end message types -----------------------
@@ -1872,6 +1895,42 @@ 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. +``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 Region. + 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 fail. + 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 resources + available. Lastly, mappings are automatically unmapped by the front-end + across device reset operation. + +``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 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: VHOST_USER_PROTOCOL_F_REPLY_ACK
Signature · 1 line
2.49.0
Re: [PATCH v11 3/7] vhost_user.rst: Add SHMEM_MAP/_UNMAP to spec
Quoted 67 lines · Albert Esteve
Add SHMEM_MAP/_UNMAP request to the vhost-user
spec documentation.Reviewed-by Stefan Hajnoczi <[email protected]>Reviewed-by Manos Pitsidianakis <[email protected]>Signed-off-by Albert Esteve <[email protected]>--- docs/interop/vhost-user.rst | 59 +++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+)docs/interop/vhost-user.rst +28
-0@@ -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. +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 -----------@@ -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;@@ -1064,6 +1086,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 Front-end message types -----------------------@@ -1872,6 +1895,42 @@ 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. +``VHOST_USER_BACKEND_SHMEM_MAP`` + :id: 9 + :equivalent ioctl: N/A + :request payload: fd and ``struct VhostUserMMap`` + :reply payload: N/A
docs/interop/vhost-user.rst is sent by the front-end. · line 1912
Quoted 10 lines
docs/interop/vhost-user.rst +10
-0+ + 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 Region. + 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.
Having now tried to implement this, I'm wondering whether replies should be mandatory, even without VHOST_USER_PROTOCOL_F_REPLY_ACK, like they are for some other messages. Without waiting for a reply, a backend doesn't know when it can tell the driver to start using the mapped memory, so I'm not sure there's ever a case in which a backend would not want to wait for a reply after sending VHOST_USER_BACKEND_SHMEM_MAP, even if it doesn't wait habitually wait for replies for other messages. (crosvm is like this — its backends don't negotiate VHOST_USER_PROTOCOL_F_REPLY_ACK, and their non-standard map/unmap requests had mandatory replies.)
Quoted 25 lines
docs/interop/vhost-user.rst +20
-0+ Mapping over an already existing map is not allowed and requests shall fail. + 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 resources + available. Lastly, mappings are automatically unmapped by the front-end + across device reset operation. + +``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 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: VHOST_USER_PROTOCOL_F_REPLY_ACKSignature · 1 line
2.49.0
Re: [PATCH v11 3/7] vhost_user.rst: Add SHMEM_MAP/_UNMAP to spec
Quoted 83 lines · Alyssa Ross
Quoted 67 lines · Albert Esteve
Add SHMEM_MAP/_UNMAP request to the vhost-user
spec documentation.Reviewed-by Stefan Hajnoczi <[email protected]>Reviewed-by Manos Pitsidianakis <[email protected]>Signed-off-by Albert Esteve <[email protected]>--- docs/interop/vhost-user.rst | 59 +++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+)docs/interop/vhost-user.rst +28
-0@@ -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. +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 -----------@@ -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;@@ -1064,6 +1086,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 Front-end message types -----------------------@@ -1872,6 +1895,42 @@ 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. +``VHOST_USER_BACKEND_SHMEM_MAP`` + :id: 9 + :equivalent ioctl: N/A + :request payload: fd and ``struct VhostUserMMap`` + :reply payload: N/Adocs/interop/vhost-user.rst is sent by the front-end. · line 1912
Quoted 10 lines
docs/interop/vhost-user.rst +10
-0+ + 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 Region. + 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.Having now tried to implement this, I'm wondering whether replies should be mandatory, even without
VHOST_USER_PROTOCOL_F_REPLY_ACK, like they
Quoted 8 lines
are for some other messages. Without waiting for a reply, a backend doesn't know when it can tell the driver to start using the mapped memory, so I'm not sure there's ever a case in which a backend would not want to wait for a reply after sending
VHOST_USER_BACKEND_SHMEM_MAP, even if it doesn't wait habitually wait for replies for other messages. (crosvm is like this — its backends don't negotiateVHOST_USER_PROTOCOL_F_REPLY_ACK, and their non-standard map/unmap requests had mandatory replies.)
What you say makes sense to me. Every test I run for this, I used the flag, because it was required to work correctly. Nonetheless, I assumed giving flexibility may be better in case there are backends that will not be accessing the mapped memory right away and prefer to save the response. Also, previous implementation in QEMU always checked VHOST_USER_PROTOCOL_F_REPLY_ACK before sending a response. Which made me think this was the norm.
I could relax that requirement in a v12, given this patch is still pending to merge (and update the implementation accordingly).
Quoted 26 lines
Quoted 26 lines
docs/interop/vhost-user.rst +20
-1+ Mapping over an already existing map is not allowed and requests shall fail. + 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 resources + available. Lastly, mappings are automatically unmapped by the front-end + across device reset operation. + +``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 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: VHOST_USER_PROTOCOL_F_REPLY_ACK --2.49.0
Re: [PATCH v11 3/7] vhost_user.rst: Add SHMEM_MAP/_UNMAP to spec
In reply to Alyssa RossQuoted 82 lines · Alyssa Ross
Quoted 67 lines · Albert Esteve
Add SHMEM_MAP/_UNMAP request to the vhost-user
spec documentation.Reviewed-by Stefan Hajnoczi <[email protected]>Reviewed-by Manos Pitsidianakis <[email protected]>Signed-off-by Albert Esteve <[email protected]>--- docs/interop/vhost-user.rst | 59 +++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+)docs/interop/vhost-user.rst +28
-0@@ -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. +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 -----------@@ -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;@@ -1064,6 +1086,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 Front-end message types -----------------------@@ -1872,6 +1895,42 @@ 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. +``VHOST_USER_BACKEND_SHMEM_MAP`` + :id: 9 + :equivalent ioctl: N/A + :request payload: fd and ``struct VhostUserMMap`` + :reply payload: N/Adocs/interop/vhost-user.rst is sent by the front-end. · line 1912
Quoted 10 lines
docs/interop/vhost-user.rst +10
-0+ + 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 Region. + 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.Having now tried to implement this, I'm wondering whether replies should be mandatory, even without
VHOST_USER_PROTOCOL_F_REPLY_ACK, like they
Quoted 8 lines
are for some other messages. Without waiting for a reply, a backend doesn't know when it can tell the driver to start using the mapped memory, so I'm not sure there's ever a case in which a backend would not want to wait for a reply after sending
VHOST_USER_BACKEND_SHMEM_MAP, even if it doesn't wait habitually wait for replies for other messages. (crosvm is like this — its backends don't negotiateVHOST_USER_PROTOCOL_F_REPLY_ACK, and their non-standard map/unmap requests had mandatory replies.)
the use-case would be multiple MAP request and a single ack at the end to confirm them all.
behaviour of sending ack without VHOST_USER_PROTOCOL_F_REPLY_ACK is legacy. let's just stick to the simple rule - if you want an ack set VHOST_USER_PROTOCOL_F_REPLY_ACK.
Quoted 25 lines
Quoted 25 lines
docs/interop/vhost-user.rst +20
-0+ Mapping over an already existing map is not allowed and requests shall fail. + 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 resources + available. Lastly, mappings are automatically unmapped by the front-end + across device reset operation. + +``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 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: VHOST_USER_PROTOCOL_F_REPLY_ACKSignature · 1 line
2.49.0
Re: [PATCH v11 3/7] vhost_user.rst: Add SHMEM_MAP/_UNMAP to spec
Quoted 92 lines · Michael S. Tsirkin
Quoted 82 lines · Alyssa Ross
Quoted 67 lines · Albert Esteve
Add SHMEM_MAP/_UNMAP request to the vhost-user
spec documentation.Reviewed-by Stefan Hajnoczi <[email protected]>Reviewed-by Manos Pitsidianakis <[email protected]>Signed-off-by Albert Esteve <[email protected]>--- docs/interop/vhost-user.rst | 59 +++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+)docs/interop/vhost-user.rst +28
-0@@ -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. +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 -----------@@ -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;@@ -1064,6 +1086,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 Front-end message types -----------------------@@ -1872,6 +1895,42 @@ 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. +``VHOST_USER_BACKEND_SHMEM_MAP`` + :id: 9 + :equivalent ioctl: N/A + :request payload: fd and ``struct VhostUserMMap`` + :reply payload: N/Adocs/interop/vhost-user.rst is sent by the front-end. · line 1912
Quoted 10 lines
docs/interop/vhost-user.rst +10
-0+ + 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 Region. + 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.Having now tried to implement this, I'm wondering whether replies should be mandatory, even without
VHOST_USER_PROTOCOL_F_REPLY_ACK, like theyQuoted 8 lines
are for some other messages. Without waiting for a reply, a backend doesn't know when it can tell the driver to start using the mapped memory, so I'm not sure there's ever a case in which a backend would not want to wait for a reply after sending
VHOST_USER_BACKEND_SHMEM_MAP, even if it doesn't wait habitually wait for replies for other messages. (crosvm is like this — its backends don't negotiateVHOST_USER_PROTOCOL_F_REPLY_ACK, and their non-standard map/unmap requests had mandatory replies.)
Quoted 6 lines
the use-case would be multiple MAP request and a single ack at the end to confirm them all.
behaviour of sending ack without
VHOST_USER_PROTOCOL_F_REPLY_ACKis legacy. let's just stick to the simple rule - if you want an ack setVHOST_USER_PROTOCOL_F_REPLY_ACK.
Okay, makes sense to me. I'll try to fix crosvm's implementation.
PATCH v11 4/7 vhost_user: Add frontend get_shmem_config command
In reply to Albert EsteveThe 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.
--- 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(+)
include/hw/virtio/vhost-backend.h +10 -0
@@ -163,6 +163,15 @@ typedef int (*vhost_set_device_state_fd_op)(struct vhost_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); + 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; int vhost_backend_update_device_iotlb(struct vhost_dev *dev,
include/hw/virtio/vhost-user.h +1 -0
@@ -32,6 +32,7 @@ enum VhostUserProtocolFeature {/* Feature 17 reserved for VHOST_USER_PROTOCOL_F_XEN_MMAP. */ VHOST_USER_PROTOCOL_F_SHARED_OBJECT =18 ,VHOST_USER_PROTOCOL_F_DEVICE_STATE =19 , +VHOST_USER_PROTOCOL_F_SHMEM =20 , VHOST_USER_PROTOCOL_F_MAX };
include/hw/virtio/virtio.h +2 -0
@@ -82,6 +82,8 @@ typedef struct VirtQueueElement#define VIRTIO_NO_VECTOR 0xffff +#define VIRTIO_MAX_SHMEM_REGIONS 256 +/* special index value used internally for config irqs */ #define VIRTIO_CONFIG_IRQ_IDX -1
Signature · 1 line
2.49.0
PATCH v11 5/7 vhost_user.rst: Add GET_SHMEM_CONFIG message
In reply to Albert EsteveAdd GET_SHMEM_CONFIG vhost-user frontend message to the spec documentation.
--- docs/interop/vhost-user.rst | 39 +++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+)
docs/interop/vhost-user.rst +39 -0
@@ -371,6 +371,20 @@ MMAP request - 0: Pages are mapped read-only - 1: Pages are mapped read-write +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 -----------
@@ -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;
@@ -1761,6 +1776,30 @@ Front-end message types Using this function requires prior negotiation of the ``VHOST_USER_PROTOCOL_F_DEVICE_STATE`` feature. +``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 will + 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 ----------------------
Signature · 1 line
2.49.0
Re: [PATCH v11 5/7] vhost_user.rst: Add GET_SHMEM_CONFIG message
Quoted 23 lines · Albert Esteve
Add
GET_SHMEM_CONFIGvhost-user frontend message to the spec documentation.Reviewed-by Alyssa Ross <[email protected]>Reviewed-by Stefan Hajnoczi <[email protected]>Signed-off-by Albert Esteve <[email protected]>--- docs/interop/vhost-user.rst | 39 +++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+)docs/interop/vhost-user.rst +4
-0@@ -371,6 +371,20 @@ MMAP request - 0: Pages are mapped read-only - 1: Pages are mapped read-write +VIRTIO Shared Memory Region configuration +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + ++-------------+---------+------------+----+--------------+
docs/interop/vhost-user.rst MMAP request · line 387
Quoted 10 lines
docs/interop/vhost-user.rst +10
-0+| 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 +
When implementing this for rust-vmm, the mem size came up a bit confusing. In the last patch (7/7) of this series, the implementation uses num regions as a count for the number of valid regions (thus accounting for gaps in the shmem region mapping). Thus, mem size has this confusing statement saying that it containers num regions fields. It should say it contains 256 fields (it is only sent once during initialization, so no need to save bytes here), with only num regions that are valid (i.e., greater than 0). Maybe it could even discard the num regions field, and send only the full array. Thoughts?
As much as I wanted this series merged, this deserves a clarification. So I can either send a new version of the series or split the last three patches into a different series. Hopefully it only requires one more version though.
Quoted 45 lines
docs/interop/vhost-user.rst +25
-1C structure -----------@@ -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;@@ -1761,6 +1776,30 @@ Front-end message types Using this function requires prior negotiation of the ``VHOST_USER_PROTOCOL_F_DEVICE_STATE`` feature. +``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 will + 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 ---------------------- --2.49.0
Re: [PATCH v11 5/7] vhost_user.rst: Add GET_SHMEM_CONFIG message
Quoted 36 lines · Albert Esteve
Quoted 23 lines · Albert Esteve
Add
GET_SHMEM_CONFIGvhost-user frontend message to the spec documentation.Reviewed-by Alyssa Ross <[email protected]>Reviewed-by Stefan Hajnoczi <[email protected]>Signed-off-by Albert Esteve <[email protected]>--- docs/interop/vhost-user.rst | 39 +++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+)docs/interop/vhost-user.rst +4
-0@@ -371,6 +371,20 @@ MMAP request - 0: Pages are mapped read-only - 1: Pages are mapped read-write +VIRTIO Shared Memory Region configuration +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + ++-------------+---------+------------+----+--------------+docs/interop/vhost-user.rst MMAP request · line 387
Quoted 10 lines
docs/interop/vhost-user.rst +10
-0+| 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 +
Quoted 8 lines
When implementing this for rust-vmm, the mem size came up a bit confusing. In the last patch (7/7) of this series, the implementation uses
num regionsas a count for the number of valid regions (thus accounting for gaps in the shmem region mapping). Thus,mem sizehas this confusing statement saying that it containersnum regionsfields. It should say it contains 256 fields (it is only sent once during initialization, so no need to save bytes here), with onlynum regionsthat are valid (i.e., greater than 0).
That was my understanding also. I agree a clarification would not do any harm :)
Quoted 59 lines
Maybe it could even
discard thenum regionsfield, and send only the full array. Thoughts?As much as I wanted this series merged, this deserves a clarification. So I can either send a new version of the series or split the last three patches into a different series. Hopefully it only requires one more version though.
Quoted 45 lines
docs/interop/vhost-user.rst +25
-1C structure -----------@@ -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;@@ -1761,6 +1776,30 @@ Front-end message types Using this function requires prior negotiation of the ``VHOST_USER_PROTOCOL_F_DEVICE_STATE`` feature. +``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 will + 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 ---------------------- --2.49.0
Re: [PATCH v11 5/7] vhost_user.rst: Add GET_SHMEM_CONFIG message
In reply to Albert EsteveQuoted 37 lines · Albert Esteve
Quoted 23 lines · Albert Esteve
Add
GET_SHMEM_CONFIGvhost-user frontend message to the spec documentation.Reviewed-by Alyssa Ross <[email protected]>Reviewed-by Stefan Hajnoczi <[email protected]>Signed-off-by Albert Esteve <[email protected]>--- docs/interop/vhost-user.rst | 39 +++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+)docs/interop/vhost-user.rst +4
-0@@ -371,6 +371,20 @@ MMAP request - 0: Pages are mapped read-only - 1: Pages are mapped read-write +VIRTIO Shared Memory Region configuration +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + ++-------------+---------+------------+----+--------------+docs/interop/vhost-user.rst MMAP request · line 387
Quoted 10 lines
docs/interop/vhost-user.rst +10
-0+| 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 +When implementing this for rust-vmm, the mem size came up a bit confusing. In the last patch (7/7) of this series, the implementation
Quoted 8 lines
uses
num regionsas a count for the number of valid regions (thus accounting for gaps in the shmem region mapping). Thus,mem sizehas this confusing statement saying that it containersnum regionsfields. It should say it contains 256 fields (it is only sent once during initialization, so no need to save bytes here), with onlynum regionsthat are valid (i.e., greater than 0). Maybe it could even discard thenum regionsfield, and send only the full array. Thoughts?
Let's discuss the exact wording here.
I'm not sure why would we need this padding sending unused fields though. Waste no, need not?
Quoted 51 lines
As much as I wanted this series merged, this deserves a clarification. So I can either send a new version of the series or split the last three patches into a different series. Hopefully it only requires one more version though.
Quoted 45 lines
docs/interop/vhost-user.rst +25
-1C structure -----------@@ -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;@@ -1761,6 +1776,30 @@ Front-end message types Using this function requires prior negotiation of the ``VHOST_USER_PROTOCOL_F_DEVICE_STATE`` feature. +``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 will + 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 ---------------------- --2.49.0
Re: [PATCH v11 5/7] vhost_user.rst: Add GET_SHMEM_CONFIG message
Quoted 48 lines · Michael S. Tsirkin
Quoted 37 lines · Albert Esteve
Quoted 23 lines · Albert Esteve
Add
GET_SHMEM_CONFIGvhost-user frontend message to the spec documentation.Reviewed-by Alyssa Ross <[email protected]>Reviewed-by Stefan Hajnoczi <[email protected]>Signed-off-by Albert Esteve <[email protected]>--- docs/interop/vhost-user.rst | 39 +++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+)docs/interop/vhost-user.rst +4
-0@@ -371,6 +371,20 @@ MMAP request - 0: Pages are mapped read-only - 1: Pages are mapped read-write +VIRTIO Shared Memory Region configuration +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + ++-------------+---------+------------+----+--------------+docs/interop/vhost-user.rst MMAP request · line 387
Quoted 10 lines
docs/interop/vhost-user.rst +10
-0+| 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 +When implementing this for rust-vmm, the mem size came up a bit confusing. In the last patch (7/7) of this series, the implementation
Quoted 8 lines
uses
num regionsas a count for the number of valid regions (thus accounting for gaps in the shmem region mapping). Thus,mem sizehas this confusing statement saying that it containersnum regionsfields. It should say it contains 256 fields (it is only sent once during initialization, so no need to save bytes here), with onlynum regionsthat are valid (i.e., greater than 0). Maybe it could even discard thenum regionsfield, and send only the full array. Thoughts?
Quoted 3 lines
Let's discuss the exact wording here.
I'm not sure why would we need this padding sending unused fields though. Waste no, need not?
What about something like this:
+:mem size: an array of 256 64-bit fields representing the size of each
+ VIRTIO Shared Memory Region. ``num regions`` specifies the + number of valid regions (non-zero size). The array index + corresponds to the shared memory ID (shmid).
If you are suggesting removing the num regions+padding field, then the description could be further simplified to:
+:mem size: an array of 256 64-bit fields representing the size of each
+ VIRTIO Shared Memory Region. The array index corresponds
+ to the shared memory ID (shmid).
Quoted 53 lines
Quoted 52 lines
As much as I wanted this series merged, this deserves a clarification. So I can either send a new version of the series or split the last three patches into a different series. Hopefully it only requires one more version though.
Quoted 45 lines
docs/interop/vhost-user.rst +25
-1C structure -----------@@ -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;@@ -1761,6 +1776,30 @@ Front-end message types Using this function requires prior negotiation of the ``VHOST_USER_PROTOCOL_F_DEVICE_STATE`` feature. +``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 will + 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 ---------------------- --2.49.0
Re: [PATCH v11 5/7] vhost_user.rst: Add GET_SHMEM_CONFIG message
Quoted 57 lines · Albert Esteve
Quoted 48 lines · Michael S. Tsirkin
Quoted 37 lines · Albert Esteve
Quoted 23 lines · Albert Esteve
Add
GET_SHMEM_CONFIGvhost-user frontend message to the spec documentation.Reviewed-by Alyssa Ross <[email protected]>Reviewed-by Stefan Hajnoczi <[email protected]>Signed-off-by Albert Esteve <[email protected]>--- docs/interop/vhost-user.rst | 39 +++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+)docs/interop/vhost-user.rst +4
-0@@ -371,6 +371,20 @@ MMAP request - 0: Pages are mapped read-only - 1: Pages are mapped read-write +VIRTIO Shared Memory Region configuration +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + ++-------------+---------+------------+----+--------------+docs/interop/vhost-user.rst MMAP request · line 387
Quoted 10 lines
docs/interop/vhost-user.rst +10
-0+| 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 +When implementing this for rust-vmm, the mem size came up a bit confusing. In the last patch (7/7) of this series, the implementation
Quoted 8 lines
uses
num regionsas a count for the number of valid regions (thus accounting for gaps in the shmem region mapping). Thus,mem sizehas this confusing statement saying that it containersnum regionsfields. It should say it contains 256 fields (it is only sent once during initialization, so no need to save bytes here), with onlynum regionsthat are valid (i.e., greater than 0). Maybe it could even discard thenum regionsfield, and send only the full array. Thoughts?Quoted 3 lines
Let's discuss the exact wording here.
I'm not sure why would we need this padding sending unused fields though. Waste no, need not?What about something like this:
+:mem size: an array of 256 64-bit fields representing the size of each
Review
Quoted 3 lines
(quoted diff) +3
-0+ VIRTIO Shared Memory Region. ``num regions`` specifies the + number of valid regions (non-zero size). The array index + corresponds to the shared memory ID (shmid).
Just to be clear, I'd favour this change, which only entails a minor clarification on these lines. The rest of the series, including implementation and structures, remain unchanged.
Quoted 64 lines
If you are suggesting removing the num regions+padding field, then the description could be further simplified to:
+:mem size: an array of 256 64-bit fields representing the size of each
(quoted diff) +2
-0+ VIRTIO Shared Memory Region. The array index corresponds + to the shared memory ID (shmid).Quoted 57 lines
Quoted 56 lines
As much as I wanted this series merged, this deserves a clarification. So I can either send a new version of the series or split the last three patches into a different series. Hopefully it only requires one more version though.
Quoted 45 lines
docs/interop/vhost-user.rst +25
-1C structure -----------@@ -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;@@ -1761,6 +1776,30 @@ Front-end message types Using this function requires prior negotiation of the ``VHOST_USER_PROTOCOL_F_DEVICE_STATE`` feature. +``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 will + 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 ---------------------- --2.49.0
PATCH v11 6/7 qmp: add shmem feature map
In reply to Albert EsteveAdd new vhost-user protocolVHOST_USER_PROTOCOL_F_SHMEM feature to
feature map.
--- hw/virtio/virtio-qmp.c | 3 +++ 1 file changed, 3 insertions(+)
hw/virtio/virtio-qmp.c +3 -0
@@ -127,6 +127,9 @@ static const qmp_virtio_feature_map_t vhost_user_protocol_map[] = {FEATURE_ENTRY (VHOST_USER_PROTOCOL_F_DEVICE_STATE , \"VHOST_USER_PROTOCOL_F_DEVICE_STATE: Backend device state transfer " "supported" ), +FEATURE_ENTRY (VHOST_USER_PROTOCOL_F_SHMEM , \ +"VHOST_USER_PROTOCOL_F_SHMEM: Backend shared memory mapping " +"supported" ), { -1, "" } };
Signature · 1 line
2.49.0
PATCH v11 7/7 vhost-user-device: Add shared memory BAR
In reply to Albert EsteveAdd 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.
--- hw/virtio/vhost-user-base.c | 47 ++++++++++++++++++++++++-- hw/virtio/vhost-user-test-device-pci.c | 39 +++++++++++++++++++-- 2 files changed, 81 insertions(+), 5 deletions(-)
hw/virtio/vhost-user-base.c +44 -3
@@ -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" static void vub_start (VirtIODevice *vdev) {
@@ -276,7 +277,9 @@ static void vub_device_realize(DeviceState *dev, Error **errp) {VirtIODevice *vdev =VIRTIO_DEVICE (dev);VHostUserBase *vub =VHOST_USER_BASE (dev); -int ret; +uint64_t memory_sizes[VIRTIO_MAX_SHMEM_REGIONS ]; + g_autofree char *name = NULL; +int i, ret, nregions, regions_processed =0 ;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)/* Allocate queues */ vub->vqs =g_ptr_array_sized_new (vub->num_vqs); -for (int i =0 ; i < vub->num_vqs; i++) { +for (i =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, Error **errp) VHOST_BACKEND_TYPE_USER, 0, errp);if (ret <0 ) { -do_vhost_user_cleanup (vdev, vub); + goto err; + } + + ret = vub->vhost_dev.vhost_ops->vhost_get_shmem_config (&vub->vhost_dev, + &nregions, + memory_sizes, + errp); + +if (ret <0 ) { + goto err; + } + +for (i =0 ; i <VIRTIO_MAX_SHMEM_REGIONS && regions_processed < nregions; i++) { +if (memory_sizes[i]) { + regions_processed++; +if (vub->vhost_dev.migration_blocker ==NULL ) { +error_setg (&vub->vhost_dev.migration_blocker, +"Migration disabled: devices with VIRTIO Shared Memory " +"Regions do not support migration yet." ); + ret =migrate_add_blocker_normal ( + &vub->vhost_dev.migration_blocker, + errp); + +if (ret <0 ) { + goto err; + } + } + +if (memory_sizes[i] %qemu_real_host_page_size () !=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]); + } }qemu_chr_fe_set_handlers (&vub->chardev,NULL ,NULL , vub_event,NULL , dev,NULL , true); +return ; +err: +do_vhost_user_cleanup (vdev, vub); }static void vub_device_unrealize (DeviceState *dev)
hw/virtio/vhost-user-test-device-pci.c +37 -2
@@ -8,14 +8,18 @@*/ #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" +#define VIRTIO_DEVICE_PCI_SHMEM_BAR 4 +struct VHostUserDevicePCI {VirtIOPCIProxy parent_obj;VHostUserBase vub; +MemoryRegion shmembar; };#define TYPE_VHOST_USER_TEST_DEVICE_PCI "vhost-user-test-device-pci-base"
@@ -25,10 +29,41 @@ OBJECT_DECLARE_SIMPLE_TYPE(VHostUserDevicePCI, VHOST_USER_TEST_DEVICE_PCI)static void vhost_user_device_pci_realize (VirtIOPCIProxy *vpci_dev,Error **errp) {VHostUserDevicePCI *dev =VHOST_USER_TEST_DEVICE_PCI (vpci_dev); -DeviceState *vdev =DEVICE (&dev->vub); +DeviceState *dev_state =DEVICE (&dev->vub); +VirtIODevice *vdev =VIRTIO_DEVICE (dev_state); +VirtioSharedMemory *shmem, *next; +uint64_t offset =0 , shmem_size =0 ; + vpci_dev->modern_mem_bar_idx =2 ; vpci_dev->nvectors =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 = 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 PCI 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 = 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); + } } static void vhost_user_device_pci_class_init(ObjectClass *klass,
Signature · 1 line
2.49.0
Re: [PATCH v11 7/7] vhost-user-device: Add shared memory BAR
Quoted 42 lines · Albert Esteve
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_CONFIGmessage sent by vhost-user-base during realization aftervirtio_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_SHMEMand has configured shared memory regions. This maintains backward compatibility with backends that do not support shared memory functionality.Reviewed-by Stefan Hajnoczi <[email protected]>Signed-off-by Albert Esteve <[email protected]>--- hw/virtio/vhost-user-base.c | 47 ++++++++++++++++++++++++-- hw/virtio/vhost-user-test-device-pci.c | 39 +++++++++++++++++++-- 2 files changed, 81 insertions(+), 5 deletions(-)hw/virtio/vhost-user-base.c +1
-0@@ -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" static void vub_start (VirtIODevice *vdev) {
hw/virtio/vhost-user-base.c static void vub_device_realize(DeviceState *dev, Error **errp) · line 281
Quoted 6 lines
hw/virtio/vhost-user-base.c +2
-1@@ -276,7 +277,9 @@ static void vub_device_realize(DeviceState *dev, Error **errp) {VirtIODevice *vdev =VIRTIO_DEVICE (dev);VHostUserBase *vub =VHOST_USER_BASE (dev); -int ret; +uint64_t memory_sizes[VIRTIO_MAX_SHMEM_REGIONS ]; + g_autofree char *name = NULL;
This add a local variable "name" ...
Quoted 53 lines
hw/virtio/vhost-user-base.c +37
-2+int i, ret, nregions, regions_processed =0 ;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)/* Allocate queues */ vub->vqs =g_ptr_array_sized_new (vub->num_vqs); -for (int i =0 ; i < vub->num_vqs; i++) { +for (i =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, Error **errp) VHOST_BACKEND_TYPE_USER, 0, errp);if (ret <0 ) { -do_vhost_user_cleanup (vdev, vub); + goto err; + } + + ret = vub->vhost_dev.vhost_ops->vhost_get_shmem_config (&vub->vhost_dev, + &nregions, + memory_sizes, + errp); + +if (ret <0 ) { + goto err; + } + +for (i =0 ; i <VIRTIO_MAX_SHMEM_REGIONS && regions_processed < nregions; i++) { +if (memory_sizes[i]) { + regions_processed++; +if (vub->vhost_dev.migration_blocker ==NULL ) { +error_setg (&vub->vhost_dev.migration_blocker, +"Migration disabled: devices with VIRTIO Shared Memory " +"Regions do not support migration yet." ); + ret =migrate_add_blocker_normal ( + &vub->vhost_dev.migration_blocker, + errp); + +if (ret <0 ) { + goto err; + } + } + +if (memory_sizes[i] %qemu_real_host_page_size () !=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]);
hw/virtio/vhost-user-base.c static void vub_device_realize(DeviceState *dev, Error **errp) · line 383
Quoted 10 lines
hw/virtio/vhost-user-base.c +4
-0+ } }qemu_chr_fe_set_handlers (&vub->chardev,NULL ,NULL , vub_event,NULL , dev,NULL , true); +return ; +err: +do_vhost_user_cleanup (vdev, vub); }
which is never used.
this results in a build error:
Quoted 70 lines
hw/virtio/vhost-user-base.c +0
-0static void vub_device_unrealize (DeviceState *dev)hw/virtio/vhost-user-test-device-pci.c +37
-2@@ -8,14 +8,18 @@*/ #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" +#define VIRTIO_DEVICE_PCI_SHMEM_BAR 4 +struct VHostUserDevicePCI {VirtIOPCIProxy parent_obj;VHostUserBase vub; +MemoryRegion shmembar; };#define TYPE_VHOST_USER_TEST_DEVICE_PCI "vhost-user-test-device-pci-base"@@ -25,10 +29,41 @@ OBJECT_DECLARE_SIMPLE_TYPE(VHostUserDevicePCI, VHOST_USER_TEST_DEVICE_PCI)static void vhost_user_device_pci_realize (VirtIOPCIProxy *vpci_dev,Error **errp) {VHostUserDevicePCI *dev =VHOST_USER_TEST_DEVICE_PCI (vpci_dev); -DeviceState *vdev =DEVICE (&dev->vub); +DeviceState *dev_state =DEVICE (&dev->vub); +VirtIODevice *vdev =VIRTIO_DEVICE (dev_state); +VirtioSharedMemory *shmem, *next; +uint64_t offset =0 , shmem_size =0 ; + vpci_dev->modern_mem_bar_idx =2 ; vpci_dev->nvectors =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 = 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 PCI 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 = 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); + } } static void vhost_user_device_pci_class_init(ObjectClass *klass,Signature · 1 line
2.49.0
Re: [PATCH v11 7/7] vhost-user-device: Add shared memory BAR
Quoted 119 lines · Michael S. Tsirkin
Quoted 42 lines · Albert Esteve
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_CONFIGmessage sent by vhost-user-base during realization aftervirtio_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_SHMEMand has configured shared memory regions. This maintains backward compatibility with backends that do not support shared memory functionality.Reviewed-by Stefan Hajnoczi <[email protected]>Signed-off-by Albert Esteve <[email protected]>--- hw/virtio/vhost-user-base.c | 47 ++++++++++++++++++++++++-- hw/virtio/vhost-user-test-device-pci.c | 39 +++++++++++++++++++-- 2 files changed, 81 insertions(+), 5 deletions(-)hw/virtio/vhost-user-base.c +1
-0@@ -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" static void vub_start (VirtIODevice *vdev) {hw/virtio/vhost-user-base.c static void vub_device_realize(DeviceState *dev, Error **errp) · line 281
Quoted 6 lines
hw/virtio/vhost-user-base.c +2
-1@@ -276,7 +277,9 @@ static void vub_device_realize(DeviceState *dev, Error **errp) {VirtIODevice *vdev =VIRTIO_DEVICE (dev);VHostUserBase *vub =VHOST_USER_BASE (dev); -int ret; +uint64_t memory_sizes[VIRTIO_MAX_SHMEM_REGIONS ]; + g_autofree char *name = NULL;This add a local variable "name" ...
Quoted 53 lines
hw/virtio/vhost-user-base.c +37
-2+int i, ret, nregions, regions_processed =0 ;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)/* Allocate queues */ vub->vqs =g_ptr_array_sized_new (vub->num_vqs); -for (int i =0 ; i < vub->num_vqs; i++) { +for (i =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, Error **errp) VHOST_BACKEND_TYPE_USER, 0, errp);if (ret <0 ) { -do_vhost_user_cleanup (vdev, vub); + goto err; + } + + ret = vub->vhost_dev.vhost_ops->vhost_get_shmem_config (&vub->vhost_dev, + &nregions, + memory_sizes, + errp); + +if (ret <0 ) { + goto err; + } + +for (i =0 ; i <VIRTIO_MAX_SHMEM_REGIONS && regions_processed < nregions; i++) { +if (memory_sizes[i]) { + regions_processed++; +if (vub->vhost_dev.migration_blocker ==NULL ) { +error_setg (&vub->vhost_dev.migration_blocker, +"Migration disabled: devices with VIRTIO Shared Memory " +"Regions do not support migration yet." ); + ret =migrate_add_blocker_normal ( + &vub->vhost_dev.migration_blocker, + errp); + +if (ret <0 ) { + goto err; + } + } + +if (memory_sizes[i] %qemu_real_host_page_size () !=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]);hw/virtio/vhost-user-base.c static void vub_device_realize(DeviceState *dev, Error **errp) · line 383
Quoted 10 lines
hw/virtio/vhost-user-base.c +4
-0+ } }qemu_chr_fe_set_handlers (&vub->chardev,NULL ,NULL , vub_event,NULL , dev,NULL , true); +return ; +err: +do_vhost_user_cleanup (vdev, vub); }which is never used.
Quoted 5 lines
this results in a build error:
Thanks. I will send a new version fixing the build errors you mentioned. I am not sure why I did not see them myself when building locally :/
Quoted 73 lines
Quoted 72 lines
hw/virtio/vhost-user-base.c +0
-0static void vub_device_unrealize (DeviceState *dev)hw/virtio/vhost-user-test-device-pci.c +37
-3@@ -8,14 +8,18 @@*/ #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" +#define VIRTIO_DEVICE_PCI_SHMEM_BAR 4 +struct VHostUserDevicePCI {VirtIOPCIProxy parent_obj;VHostUserBase vub; +MemoryRegion shmembar; };#define TYPE_VHOST_USER_TEST_DEVICE_PCI "vhost-user-test-device-pci-base"@@ -25,10 +29,41 @@ OBJECT_DECLARE_SIMPLE_TYPE(VHostUserDevicePCI, VHOST_USER_TEST_DEVICE_PCI)static void vhost_user_device_pci_realize (VirtIOPCIProxy *vpci_dev,Error **errp) {VHostUserDevicePCI *dev =VHOST_USER_TEST_DEVICE_PCI (vpci_dev); -DeviceState *vdev =DEVICE (&dev->vub); +DeviceState *dev_state =DEVICE (&dev->vub); +VirtIODevice *vdev =VIRTIO_DEVICE (dev_state); +VirtioSharedMemory *shmem, *next; +uint64_t offset =0 , shmem_size =0 ; + vpci_dev->modern_mem_bar_idx =2 ; vpci_dev->nvectors =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 = 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 PCI 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 = 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); + } } static void vhost_user_device_pci_class_init(ObjectClass *klass, --2.49.0
Quoted 54 lines · Albert Esteve
Hi all,
v10->v11
- Rebase to grab commitfde5930cc37175cfcd0f03a089e26f4458a52311and explicitly unsetreply_ackon the SHMEM_MAP/UNMAP msg.
- Mention mapping cleanup on device reset in the spec.v9->v10
- Fixtransaction_commitinvoked withouttransaction_beginonvhost_user_backend_handle_shmem_map()early errors
- Removed fd tracking on VirtioSharedMemoryMapping, it is handled by the RAMBlock
- Reject invalid BAR configurations when VIRTIO Shared Memory Regions are in use by vhost-user-test-devicev8->v9
- Fixed vhost-user new handlers to ensure that they always reply
- Made MMAP request flags field u64 everywhere
- Fixed doublememory_region_del_subregion()on UNMAP
- Add mappings cleaning onvirtio_reset()
- Some small typos and fixes
- Fixed virtio pci bar mapping for vhost-user-test-devicev7->v8
- Unified VhostUserShmemObject and VirtioSharedMemoryMapping
- Refinedshmem_objlifecycle by transferring ownership
- Other small improvementsThis patch series implements dynamic fd-backed memory mapping support for vhost-user backends, enabling backends to dynamically request memory mappings and unmappings during runtime through the new VHOST_USER_BACKEND_SHMEM_MAP/UNMAP protocol messages.
This feature benefits various VIRTIO devices that require dynamic shared memory management, including virtiofs (for DAX mappings), virtio-gpu (for resource sharing), and the recently standardized virtio-media.
The implementation introduces a QOM-based architecture for managing shared memory lifecycle:
- VirtioSharedMemoryMapping: an intermediate object that manages individual memory mappings by acting as generic container for regions declared in any vhost-user device type
- Dynamic Mapping: backends can request mappings viaSHMEM_MAPmessages, with the frontend creating MemoryRegions from the provided file descriptors and adding them as subregionsWhen a
SHMEM_MAPrequest is received, the frontend:
1. Creates VirtioSharedMemoryMapping to manage the mapping lifecycle
2. Maps the provided fd withmemory_region_init_ram_from_fd()
3. Creates a MemoryRegion backed by the mapped memory
4. Adds it as a subregion of the appropiate VIRTIO Shared Memory RegionThe QOM reference counting ensures automatic cleanup when mappings are removed or the device is destroyed.
Quoted 6 lines
This patch also includes:
-VHOST_USER_GET_SHMEM_CONFIG: a new frontend request allowing generic vhost-user devices to query shared memory configuration from backends at device initialization, enabling the generic vhost-user-device frontend to work with any backend regardless of specific shared memory requirements.
Looks good, we are in freeze so will merge after 10.2. If it's not too much trouble, pls ping me after the release to help make sure it's not forgotten.
Quoted 26 lines
The implementation has been tested with rust-vmm based backends.
Albert Esteve (7):
vhost-user: Add VirtIO Shared Memory map requestvhost_user.rst: Align VhostUserMsg excerpt membersvhost_user.rst: Add SHMEM_MAP/_UNMAP to spec
vhost_user: Add frontendget_shmem_configcommandvhost_user.rst: AddGET_SHMEM_CONFIGmessage
qmp: add shmem feature map
vhost-user-device: Add shared memory BARdocs/interop/vhost-user.rst | 102 ++++++++ hw/virtio/vhost-user-base.c | 47 +++- hw/virtio/vhost-user-test-device-pci.c | 39 +++- hw/virtio/vhost-user.c | 269 ++++++++++++++++++++++ hw/virtio/virtio-qmp.c | 3 + hw/virtio/virtio.c | 199 ++++++++++++++++ include/hw/virtio/vhost-backend.h | 10 + include/hw/virtio/vhost-user.h | 1 + include/hw/virtio/virtio.h | 137 +++++++++++ subprojects/libvhost-user/libvhost-user.c | 70 ++++++ subprojects/libvhost-user/libvhost-user.h | 54 +++++ 11 files changed, 926 insertions(+), 5 deletions(-)Signature · 1 line
2.49.0
Quoted 63 lines · Michael S. Tsirkin
Quoted 54 lines · Albert Esteve
Hi all,
v10->v11
- Rebase to grab commitfde5930cc37175cfcd0f03a089e26f4458a52311and explicitly unsetreply_ackon the SHMEM_MAP/UNMAP msg.
- Mention mapping cleanup on device reset in the spec.v9->v10
- Fixtransaction_commitinvoked withouttransaction_beginonvhost_user_backend_handle_shmem_map()early errors
- Removed fd tracking on VirtioSharedMemoryMapping, it is handled by the RAMBlock
- Reject invalid BAR configurations when VIRTIO Shared Memory Regions are in use by vhost-user-test-devicev8->v9
- Fixed vhost-user new handlers to ensure that they always reply
- Made MMAP request flags field u64 everywhere
- Fixed doublememory_region_del_subregion()on UNMAP
- Add mappings cleaning onvirtio_reset()
- Some small typos and fixes
- Fixed virtio pci bar mapping for vhost-user-test-devicev7->v8
- Unified VhostUserShmemObject and VirtioSharedMemoryMapping
- Refinedshmem_objlifecycle by transferring ownership
- Other small improvementsThis patch series implements dynamic fd-backed memory mapping support for vhost-user backends, enabling backends to dynamically request memory mappings and unmappings during runtime through the new VHOST_USER_BACKEND_SHMEM_MAP/UNMAP protocol messages.
This feature benefits various VIRTIO devices that require dynamic shared memory management, including virtiofs (for DAX mappings), virtio-gpu (for resource sharing), and the recently standardized virtio-media.
The implementation introduces a QOM-based architecture for managing shared memory lifecycle:
- VirtioSharedMemoryMapping: an intermediate object that manages individual memory mappings by acting as generic container for regions declared in any vhost-user device type
- Dynamic Mapping: backends can request mappings viaSHMEM_MAPmessages, with the frontend creating MemoryRegions from the provided file descriptors and adding them as subregionsWhen a
SHMEM_MAPrequest is received, the frontend:
1. Creates VirtioSharedMemoryMapping to manage the mapping lifecycle
2. Maps the provided fd withmemory_region_init_ram_from_fd()
3. Creates a MemoryRegion backed by the mapped memory
4. Adds it as a subregion of the appropiate VIRTIO Shared Memory RegionThe QOM reference counting ensures automatic cleanup when mappings are removed or the device is destroyed.
Quoted 6 lines
This patch also includes:
-VHOST_USER_GET_SHMEM_CONFIG: a new frontend request allowing generic vhost-user devices to query shared memory configuration from backends at device initialization, enabling the generic vhost-user-device frontend to work with any backend regardless of specific shared memory requirements.
Quoted 3 lines
Looks good, we are in freeze so will merge after 10.2. If it's not too much trouble, pls ping me after the release to help make sure it's not forgotten.
Release happened already iianm?
Sending the ping.
Quoted 29 lines
Quoted 28 lines
The implementation has been tested with rust-vmm based backends.
Albert Esteve (7):
vhost-user: Add VirtIO Shared Memory map requestvhost_user.rst: Align VhostUserMsg excerpt membersvhost_user.rst: Add SHMEM_MAP/_UNMAP to spec
vhost_user: Add frontendget_shmem_configcommandvhost_user.rst: AddGET_SHMEM_CONFIGmessage
qmp: add shmem feature map
vhost-user-device: Add shared memory BARdocs/interop/vhost-user.rst | 102 ++++++++ hw/virtio/vhost-user-base.c | 47 +++- hw/virtio/vhost-user-test-device-pci.c | 39 +++- hw/virtio/vhost-user.c | 269 ++++++++++++++++++++++ hw/virtio/virtio-qmp.c | 3 + hw/virtio/virtio.c | 199 ++++++++++++++++ include/hw/virtio/vhost-backend.h | 10 + include/hw/virtio/vhost-user.h | 1 + include/hw/virtio/virtio.h | 137 +++++++++++ subprojects/libvhost-user/libvhost-user.c | 70 ++++++ subprojects/libvhost-user/libvhost-user.h | 54 +++++ 11 files changed, 926 insertions(+), 5 deletions(-)--
2.49.0