vhost-user: Add SHMEM_MAP/UNMAP requests
v12 cover vhost-user: Add SHMEM_MAP/UNMAP requests
Hi all,
v11->v12
- Refined VIRTIO Shared Memory Region configuration mem size spec description
- Removed unused name in vhost-user-base's vub_device_realize to avoid build error
- Initialize shmem in vhost_user_backend_handle_shmem_unmap to avoid compiler being confused
- Added stub for memory_region_init_ram_from_fd in memory.h for windows builds
- Use memory_region_size for shmem_region handlers instead of accessing size member directly in shmem mrsv10->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
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 | 104 +++++++++ hw/virtio/vhost-user-base.c | 46 +++- hw/virtio/vhost-user-test-device-pci.c | 38 ++- hw/virtio/vhost-user.c | 269 ++++++++++++++++++++++ hw/virtio/virtio-qmp.c | 3 + hw/virtio/virtio.c | 201 ++++++++++++++++ include/hw/virtio/vhost-backend.h | 10 + include/hw/virtio/vhost-user.h | 1 + include/hw/virtio/virtio.h | 137 +++++++++++ include/system/memory.h | 13 ++ subprojects/libvhost-user/libvhost-user.c | 70 ++++++ subprojects/libvhost-user/libvhost-user.h | 54 +++++ 12 files changed, 941 insertions(+), 5 deletions(-)
Signature · 1 line
2.52.0
PATCH v12 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 | 201 ++++++++++++++++ include/hw/virtio/virtio.h | 135 +++++++++++ include/system/memory.h | 13 ++ subprojects/libvhost-user/libvhost-user.c | 70 ++++++ subprojects/libvhost-user/libvhost-user.h | 54 +++++ 6 files changed, 742 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 =NULL ; +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 && shmem && 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 +201 -0
245 lines not on this page · raw message
include/hw/virtio/virtio.h +135 -0
153 lines not on this page · raw message
include/system/memory.h +13 -0
@@ -1518,6 +1518,19 @@ bool memory_region_init_ram_from_fd(MemoryRegion *mr,int fd, ram_addr_t offset, Error **errp); +#else +static inline bool memory_region_init_ram_from_fd (MemoryRegion *mr, +Object *owner, +const char *name, +uint64_t size, +uint32_t ram_flags, +int fd, +ram_addr_t offset, +Error **errp) +{ +error_setg (errp,"memory_region_init_ram_from_fd is not supported on this platform" ); +return false; +}#endif /**
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.52.0