vhost-user: Add SHMEM_MAP/UNMAP requests
v2 cover vhost-user: Add SHMEM_MAP/UNMAP requests
Hi all,
v1->v2:
- Corrected typos and clarifications from first review
- Added SHMEM_CONFIG frontend request to query VIRTIO shared memory regions from backends
- vhost-user-device to use SHMEM_CONFIG to request and initialise regions
- Added MEM_READ/WRITE backend requests in case address translation fails accessing VIRTIO Shared Memory Regions with MMAPs
This is an update of my attempt to have backends support dynamic fd mapping into VIRTIO Shared Memory Regions. After the first review I have added more commits and new messages to the vhost-user protocol.
However, I still have some doubts as to how will this work, specially regarding the MEM_READ and MEM_WRITE commands. Thus, I am still looking for feedback, to ensure that I am going in the right direction with the implementation.
The usecase for this patch is, e.g., to support vhost-user-gpu RESOURCE_BLOB operations, or DAX Window request for virtio-fs. In general, any operation where a backend need to request the frontend to mmap an fd into a VIRTIO Shared Memory Region, so that the guest can then access it.
After receiving the SHMEM_MAP/UNMAP request, the frontend will perform the mmap with the instructed parameters (i.e., shmid, shm_offset, fd_offset, fd, lenght).
As there are already a couple devices that could benefit of such a feature, and more could require it in the future, the goal is to make the implementation generic.
To that end, the VIRTIO Shared Memory Region list is declared in the VirtIODevice struct.
This patch also includes:SHMEM_CONFIG frontend request that is specifically meant to allow generic vhost-user-device frontend to be able to query VIRTIO Shared Memory settings from the backend (as this device is generic and agnostic of the actual backend configuration).
Finally, MEM_READ/WRITE backend requests are added to deal with a potential issue when having any backend sharing a descriptor that references a mapping to another backend. The first backend will not be able to see these mappings. So these requests are a fallback for vhost-user memory translation fails.
Albert Esteve (5):
vhost-user: Add VIRTIO Shared Memory map request
vhost_user: Add frontend command for shmem config
vhost-user-dev: Add cache BAR
vhost_user: Add MEM_READ/WRITE backend requests
vhost_user: Implement mem_read/mem_write handlers
docs/interop/vhost-user.rst | 58 ++++++ hw/virtio/vhost-user-base.c | 39 +++- hw/virtio/vhost-user-device-pci.c | 37 +++- hw/virtio/vhost-user.c | 221 ++++++++++++++++++++++ hw/virtio/virtio.c | 12 ++ include/hw/virtio/vhost-backend.h | 6 + include/hw/virtio/vhost-user.h | 1 + include/hw/virtio/virtio.h | 5 + subprojects/libvhost-user/libvhost-user.c | 149 +++++++++++++++ subprojects/libvhost-user/libvhost-user.h | 91 +++++++++ 10 files changed, 614 insertions(+), 5 deletions(-)
Signature · 1 line
2.45.2
RFC PATCH v2 1/5 vhost-user: Add VIRTIO Shared Memory map request
Add SHMEM_MAP/UNMAP requests to vhost-user to handle VIRTIO Shared Memory mappings.
This request allows backends to dynamically map fds into a VIRTIO Shared Memory Region indentified by its shmid. Then, the fd memory is advertised to the driver as a base addres + offset, so it can be read/written (depending on the mmap flags requested) while its valid.
The backend can munmap the memory range in a given VIRTIO Shared Memory Region (again, identified by its shmid), to free it. Upon receiving this message, the front-end must mmap the regions with PROT_NONE to reserve the virtual memory space.
The device model needs to create MemoryRegion instances for the VIRTIO Shared Memory Regions and add them to the VirtIODevice instance.
--- docs/interop/vhost-user.rst | 27 +++++ hw/virtio/vhost-user.c | 122 ++++++++++++++++++++++ hw/virtio/virtio.c | 12 +++ include/hw/virtio/virtio.h | 5 + subprojects/libvhost-user/libvhost-user.c | 65 ++++++++++++ subprojects/libvhost-user/libvhost-user.h | 53 ++++++++++ 6 files changed, 284 insertions(+)
docs/interop/vhost-user.rst +27 -0
@@ -1859,6 +1859,33 @@ 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 + + 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``. A reply is generated indicating whether mapping + succeeded. + + Mapping over an already existing map is not allowed and request shall fail. + Therefore, the memory range in the request must correspond with a valid, + free region of the VIRTIO Shared Memory Region. + +``VHOST_USER_BACKEND_SHMEM_UNMAP`` + :id: 10 + :equivalent ioctl: N/A + :request payload: ``struct VhostUserMMap`` + :reply payload: N/A + + This message can be submitted by the backends so that the front-end un-mmap + a given range (``offset``, ``len``) in the VIRTIO Shared Memory Region with + the requested ``shmid``. + A reply is generated indicating whether unmapping succeeded. + .. _reply_ack: VHOST_USER_PROTOCOL_F_REPLY_ACK
hw/virtio/vhost-user.c +122 -0
152 lines not on this page · raw message
hw/virtio/virtio.c +12 -0
@@ -2856,6 +2856,16 @@ int virtio_save(VirtIODevice *vdev, QEMUFile *f)return vmstate_save_state (f, &vmstate_virtio, vdev,NULL ); } +MemoryRegion *virtio_new_shmem_region (VirtIODevice *vdev) +{ +MemoryRegion *mr =g_new0 (MemoryRegion,1 ); + ++vdev->n_shmem_regions; + vdev->shmem_list =g_renew (MemoryRegion, vdev->shmem_list, + vdev->n_shmem_regions); + vdev->shmem_list[vdev->n_shmem_regions -1 ] = *mr; +return mr; +} +/* 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)
@@ -3264,6 +3274,8 @@ void virtio_init(VirtIODevice *vdev, uint16_t device_id, size_t config_size) virtio_vmstate_change, vdev); vdev->device_endian =virtio_default_endian (); vdev->use_guest_notifier_mask = true; + vdev->shmem_list =NULL ; + vdev->n_shmem_regions =0 ; }/*
include/hw/virtio/virtio.h +5 -0
@@ -165,6 +165,9 @@ struct VirtIODevice*/ EventNotifier config_notifier;bool device_iotlb_enabled; +/* Shared memory region for vhost-user mappings. */ +MemoryRegion *shmem_list; +int n_shmem_regions; };struct VirtioDeviceClass {
@@ -280,6 +283,8 @@ void virtio_notify(VirtIODevice *vdev, VirtQueue *vq);int virtio_save (VirtIODevice *vdev,QEMUFile *f); +MemoryRegion *virtio_new_shmem_region (VirtIODevice *vdev); +extern const VMStateInfo virtio_vmstate_info;#define VMSTATE_VIRTIO_DEVICE \
subprojects/libvhost-user/libvhost-user.c +65 -0
71 lines not on this page · raw message
subprojects/libvhost-user/libvhost-user.h +53 -0
77 lines not on this page · raw message
Signature · 1 line
2.45.2
Re: [RFC PATCH v2 1/5] vhost-user: Add VIRTIO Shared Memory map request
Quoted 53 lines · Albert Esteve
Add SHMEM_MAP/UNMAP requests to vhost-user to handle VIRTIO Shared Memory mappings.
This request allows backends to dynamically map fds into a VIRTIO Shared Memory Region indentified by its
shmid. Then, the fd memory is advertised to the driver as a base addres + offset, so it can be read/written (depending on the mmap flags requested) while its valid.The backend can munmap the memory range in a given VIRTIO Shared Memory Region (again, identified by its
shmid), to free it. Upon receiving this message, the front-end must mmap the regions withPROT_NONEto reserve the virtual memory space.The device model needs to create MemoryRegion instances for the VIRTIO Shared Memory Regions and add them to the
VirtIODeviceinstance.Signed-off-by Albert Esteve <[email protected]>--- docs/interop/vhost-user.rst | 27 +++++ hw/virtio/vhost-user.c | 122 ++++++++++++++++++++++ hw/virtio/virtio.c | 12 +++ include/hw/virtio/virtio.h | 5 + subprojects/libvhost-user/libvhost-user.c | 65 ++++++++++++ subprojects/libvhost-user/libvhost-user.h | 53 ++++++++++ 6 files changed, 284 insertions(+)docs/interop/vhost-user.rst +14
-0@@ -1859,6 +1859,33 @@ 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 + + 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``. A reply is generated indicating whether mapping + succeeded. + + Mapping over an already existing map is not allowed and request shall fail. + Therefore, the memory range in the request must correspond with a valid,
docs/interop/vhost-user.rst is sent by the front-end. · line 1885
Quoted 10 lines
docs/interop/vhost-user.rst +10
-0+ free region of the VIRTIO Shared Memory Region. + +``VHOST_USER_BACKEND_SHMEM_UNMAP`` + :id: 10 + :equivalent ioctl: N/A + :request payload: ``struct VhostUserMMap`` + :reply payload: N/A + + This message can be submitted by the backends so that the front-end un-mmap + a given range (``offset``, ``len``) in the VIRTIO Shared Memory Region with
s/offset/shm_offset/
docs/interop/vhost-user.rst is sent by the front-end. · line 1886
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ the requested ``shmid``.
Please clarify that <offset, len> must correspond to the entirety of a valid mapped region.
By the way, the VIRTIO 1.3 gives the following behavior for the virtiofs DAX Window:
When a FUSE_SETUPMAPPING request perfectly overlaps a previous mapping, the previous mapping is replaced. When a mapping partially overlaps a previous mapping, the previous mapping is split into one or two smaller mappings. When a mapping is partially unmapped it is also split into one or two smaller mappings.
Establishing new mappings or splitting existing mappings consumes resources. If the device runs out of resources the FUSE_SETUPMAPPING request fails until resources are available again following FUSE_REMOVEMAPPING.
I think SETUPMAPPING/REMOVMAPPING can be implemented using SHMEM_MAP/UNMAP. SHMEM_MAP/UNMAP do not allow atomically replacing partial ranges, but as far as I know that's not necessary for virtiofs in practice.
It's worth mentioning that mappings consume resources and that SHMEM_MAP can fail when there are no resources available. The process-wide limit is vm.max_map_count on Linux although a vhost-user frontend may reduce it further to control vhost-user resource usage.
Quoted 84 lines
docs/interop/vhost-user.rst +2
-0+ A reply is generated indicating whether unmapping succeeded. + .. _reply_ack: VHOST_USER_PROTOCOL_F_REPLY_ACKhw/virtio/vhost-user.c +50
-0@@ -115,6 +115,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;@@ -192,6 +194,24 @@ typedef struct VhostUserShared {unsigned char uuid[16 ]; } VhostUserShared; +/* For the flags field of VhostUserMMap */ +#define VHOST_USER_FLAG_MAP_R (1u << 0) +#define VHOST_USER_FLAG_MAP_W (1u << 1) + +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_* */ +uint64_t flags; +}VhostUserMMap ; + typedef struct { VhostUserRequest request;@@ -224,6 +244,7 @@ typedef union {VhostUserInflight inflight;VhostUserShared object;VhostUserTransferDeviceState transfer_state; +VhostUserMMap mmap; } VhostUserPayload; typedef struct VhostUserMsg {@@ -1748,6 +1769,100 @@ vhost_user_backend_handle_shared_object_lookup(struct vhost_user *u,return 0 ; } +static int +vhost_user_backend_handle_shmem_map (struct vhost_dev *dev, +VhostUserMMap *vu_mmap, +int fd) +{ +void *addr =0 ; +MemoryRegion *mr =NULL ; + +if (fd <0 ) { +error_report ("Bad fd for map" ); +return -EBADF ; + } + +if (!dev->vdev->shmem_list || + dev->vdev->n_shmem_regions <= vu_mmap->shmid) { +error_report ("Device only has %d VIRTIO Shared Memory Regions. " +"Requested ID: %d" , + dev->vdev->n_shmem_regions, vu_mmap->shmid); +return -EFAULT ; + } + + mr = &dev->vdev->shmem_list[vu_mmap->shmid]; + +if (!mr) { +error_report ("VIRTIO Shared Memory Region at " +"ID %d unitialized" , vu_mmap->shmid); +return -EFAULT ; + } +
hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_lookup(struct vhost_user *u, · line 1810
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) > mr->size) { +error_report ("Bad offset/len for mmap %" PRIx64"+%" PRIx64, + vu_mmap->shm_offset, vu_mmap->len); +return -EFAULT ; + } + +void *shmem_ptr =memory_region_get_ram_ptr (mr); + + addr = mmap(shmem_ptr + vu_mmap->shm_offset, vu_mmap->len,
Missing check for overlap between range [shm_offset, shm_offset + len) and existing mappings.
Quoted 35 lines
hw/virtio/vhost-user.c +35
-0+ ((vu_mmap->flags &VHOST_USER_FLAG_MAP_R ) ?PROT_READ :0 ) | + ((vu_mmap->flags &VHOST_USER_FLAG_MAP_W ) ?PROT_WRITE :0 ), + MAP_SHARED | MAP_FIXED, fd, vu_mmap->fd_offset); + +if (addr ==MAP_FAILED ) { +error_report ("Failed to mmap mem fd" ); +return -EFAULT ; + } + +return 0 ; +} + +static int +vhost_user_backend_handle_shmem_unmap (struct vhost_dev *dev, +VhostUserMMap *vu_mmap) +{ +void *addr =0 ; +MemoryRegion *mr =NULL ; + +if (!dev->vdev->shmem_list || + dev->vdev->n_shmem_regions <= vu_mmap->shmid) { +error_report ("Device only has %d VIRTIO Shared Memory Regions. " +"Requested ID: %d" , + dev->vdev->n_shmem_regions, vu_mmap->shmid); +return -EFAULT ; + } + + mr = &dev->vdev->shmem_list[vu_mmap->shmid]; + +if (!mr) { +error_report ("VIRTIO Shared Memory Region at " +"ID %d unitialized" , vu_mmap->shmid); +return -EFAULT ; + } +
hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_lookup(struct vhost_user *u, · line 1855
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) > mr->size) { +error_report ("Bad offset/len for mmap %" PRIx64"+%" PRIx64, + vu_mmap->shm_offset, vu_mmap->len); +return -EFAULT ; + } + +void *shmem_ptr =memory_region_get_ram_ptr (mr); + + addr = mmap(shmem_ptr + vu_mmap->shm_offset, vu_mmap->len,
Missing check for existing mapping with exact range [shm_offset, len) match.
Quoted 32 lines
hw/virtio/vhost-user.c +17
-0+ PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0); + +if (addr ==MAP_FAILED ) { +error_report ("Failed to unmap memory" ); +return -EFAULT ; + } + +return 0 ; +} +static void close_backend_channel (struct vhost_user *u) {g_source_destroy (u->backend_src);@@ -1816,6 +1931,13 @@ static gboolean backend_read(QIOChannel *ioc, GIOCondition condition, ret =vhost_user_backend_handle_shared_object_lookup (dev->opaque, ioc, &hdr, &payload);break ; +case VHOST_USER_BACKEND_SHMEM_MAP : + ret =vhost_user_backend_handle_shmem_map (dev, &payload.mmap, + fd ? fd[0 ] :-1 ); +break ; +case VHOST_USER_BACKEND_SHMEM_UNMAP : + ret =vhost_user_backend_handle_shmem_unmap (dev, &payload.mmap); +break ;default :error_report ("Received unexpected msg type: %d." , hdr.request); ret = -EINVAL ;hw/virtio/virtio.c +0
-0
hw/virtio/virtio.c int virtio_save(VirtIODevice *vdev, QEMUFile *f) · line 2864
Quoted 9 lines
hw/virtio/virtio.c +6
-0@@ -2856,6 +2856,16 @@ int virtio_save(VirtIODevice *vdev, QEMUFile *f)return vmstate_save_state (f, &vmstate_virtio, vdev,NULL ); } +MemoryRegion *virtio_new_shmem_region (VirtIODevice *vdev) +{ +MemoryRegion *mr =g_new0 (MemoryRegion,1 ); + ++vdev->n_shmem_regions; + vdev->shmem_list =g_renew (MemoryRegion, vdev->shmem_list, + vdev->n_shmem_regions);
Where is shmem_list freed?
The name "list" is misleading since this is an array, not a list.
hw/virtio/virtio.c int virtio_save(VirtIODevice *vdev, QEMUFile *f) · line 2867
Quoted 3 lines
hw/virtio/virtio.c +3
-0+ vdev->shmem_list[vdev->n_shmem_regions -1 ] = *mr; +return mr; +}
This looks weird. The contents of mr are copied into shmem_list[] and then the pointer to mr is returned? Did you mean for the field's type to be MemoryRegion **shmem_list and then vdev->shmem_list[...] = mr would stash the pointer?
Quoted 80 lines
hw/virtio/virtio.c +3
-0+/* 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)@@ -3264,6 +3274,8 @@ void virtio_init(VirtIODevice *vdev, uint16_t device_id, size_t config_size) virtio_vmstate_change, vdev); vdev->device_endian =virtio_default_endian (); vdev->use_guest_notifier_mask = true; + vdev->shmem_list =NULL ; + vdev->n_shmem_regions =0 ; }/* include/hw/virtio/virtio.h +5
-0@@ -165,6 +165,9 @@ struct VirtIODevice*/ EventNotifier config_notifier;bool device_iotlb_enabled; +/* Shared memory region for vhost-user mappings. */ +MemoryRegion *shmem_list; +int n_shmem_regions; };struct VirtioDeviceClass {@@ -280,6 +283,8 @@ void virtio_notify(VirtIODevice *vdev, VirtQueue *vq);int virtio_save (VirtIODevice *vdev,QEMUFile *f); +MemoryRegion *virtio_new_shmem_region (VirtIODevice *vdev); +extern const VMStateInfo virtio_vmstate_info;#define VMSTATE_VIRTIO_DEVICE \subprojects/libvhost-user/libvhost-user.c +36
-0@@ -1586,6 +1586,71 @@ 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) +{ +bool result = false; +VhostUserMsg msg_reply; +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, + }, + }; + +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 fd_offset, +uint64_t shm_offset,uint64_t len)
subprojects/libvhost-user/libvhost-user.c vu_rm_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN]) · line 1634
Quoted 10 lines
subprojects/libvhost-user/libvhost-user.c +10
-0+{ +bool result = false; +VhostUserMsg msg_reply; +VhostUserMsg vmsg = { + .request =VHOST_USER_BACKEND_SHMEM_UNMAP , + .size =sizeof (vmsg.payload.mmap), + .flags =VHOST_USER_VERSION , + .payload.mmap = { + .shmid = shmid, + .fd_offset = fd_offset,
What is the meaning of this field? I expected it to be set to 0.
Quoted 110 lines
subprojects/libvhost-user/libvhost-user.c +19
-0+ .shm_offset = shm_offset, + .len = len, + }, + }; + +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 +53
-0@@ -127,6 +127,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 +188,24 @@ typedef struct VhostUserShared {unsigned char uuid[UUID_LEN ]; } VhostUserShared; +/* For the flags field of VhostUserMMap */ +#define VHOST_USER_FLAG_MAP_R (1u << 0) +#define VHOST_USER_FLAG_MAP_W (1u << 1) + +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_* */ +uint64_t flags; +}VhostUserMMap ; +#if defined(_WIN32) && (defined(__x86_64__) || defined(__i386__))# define VU_PACKED __attribute__((gcc_struct, packed))#else @@ -214,6 +234,7 @@ typedef struct VhostUserMsg {VhostUserVringArea area;VhostUserInflight inflight;VhostUserShared object; +VhostUserMMap mmap; } payload;int fds[VHOST_MEMORY_BASELINE_NREGIONS ];@@ -597,6 +618,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 +* +* 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); + +/** +* 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 +* +* 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 fd_offset, +uint64_t shm_offset,uint64_t len); +/** * vu_queue_set_notification: * @dev: a VuDev context Signature · 1 line
2.45.2
Re: [RFC PATCH v2 1/5] vhost-user: Add VIRTIO Shared Memory map request
Quoted 195 lines · Stefan Hajnoczi
Quoted 36 lines · Albert Esteve
Add SHMEM_MAP/UNMAP requests to vhost-user to handle VIRTIO Shared Memory mappings.
This request allows backends to dynamically map fds into a VIRTIO Shared Memory Region indentified by its
shmid. Then, the fd memory is advertised to the driver as a base addres + offset, so it can be read/written (depending on the mmap flags requested) while its valid.The backend can munmap the memory range in a given VIRTIO Shared Memory Region (again, identified by its
shmid), to free it. Upon receiving this message, the front-end must mmap the regions withPROT_NONEto reserve the virtual memory space.The device model needs to create MemoryRegion instances for the VIRTIO Shared Memory Regions and add them to the
VirtIODeviceinstance.Signed-off-by Albert Esteve <[email protected]>--- docs/interop/vhost-user.rst | 27 +++++ hw/virtio/vhost-user.c | 122 ++++++++++++++++++++++ hw/virtio/virtio.c | 12 +++ include/hw/virtio/virtio.h | 5 + subprojects/libvhost-user/libvhost-user.c | 65 ++++++++++++ subprojects/libvhost-user/libvhost-user.h | 53 ++++++++++ 6 files changed, 284 insertions(+)docs/interop/vhost-user.rst +0
-0docs/interop/vhost-user.rst is sent by the front-end. · line 1859
Quoted 1 line
docs/interop/vhost-user.rst +0
-0@@ -1859,6 +1859,33 @@ is sent by the front-end. when the operation is successful, or non-zero otherwise. Note that ifthe
docs/interop/vhost-user.rst is sent by the front-end.
Quoted 9 lines
docs/interop/vhost-user.rst +7
-0operation 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 + + This message can be submitted by the backends to advertise a newmapping
docs/interop/vhost-user.rst is sent by the front-end. · line 1869
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ to be made in a given VIRTIO Shared Memory Region. Upon receiving themessage,
docs/interop/vhost-user.rst is sent by the front-end. · line 1870
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ The front-end will mmap the given fd into the VIRTIO Shared MemoryRegion
docs/interop/vhost-user.rst is sent by the front-end. · line 1871
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ with the requested ``shmid``. A reply is generated indicating whethermapping
docs/interop/vhost-user.rst is sent by the front-end.
Quoted 3 lines
docs/interop/vhost-user.rst +3
-0+ succeeded. + + Mapping over an already existing map is not allowed and request shallfail.
docs/interop/vhost-user.rst is sent by the front-end. · line 1875
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ Therefore, the memory range in the request must correspond with avalid,
docs/interop/vhost-user.rst is sent by the front-end.
Quoted 9 lines
docs/interop/vhost-user.rst +9
-0+ free region of the VIRTIO Shared Memory Region. + +``VHOST_USER_BACKEND_SHMEM_UNMAP`` + :id: 10 + :equivalent ioctl: N/A + :request payload: ``struct VhostUserMMap`` + :reply payload: N/A + + This message can be submitted by the backends so that the front-endun-mmap
docs/interop/vhost-user.rst is sent by the front-end. · line 1885
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ a given range (``offset``, ``len``) in the VIRTIO Shared MemoryRegion with
s/offset/shm_offset/
docs/interop/vhost-user.rst is sent by the front-end. · line 1886
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ the requested ``shmid``.Please clarify that <offset, len> must correspond to the entirety of a valid mapped region.
By the way, the VIRTIO 1.3 gives the following behavior for the virtiofs DAX Window:
When a
FUSE_SETUPMAPPINGrequest perfectly overlaps a previous mapping, the previous mapping is replaced. When a mapping partially overlaps a previous mapping, the previous mapping is split into one or two smaller mappings. When a mapping is partially unmapped it is also split into one or two smaller mappings.Establishing new mappings or splitting existing mappings consumes resources. If the device runs out of resources the
FUSE_SETUPMAPPINGrequest fails until resources are available again followingFUSE_REMOVEMAPPING.I think SETUPMAPPING/REMOVMAPPING can be implemented using SHMEM_MAP/UNMAP. SHMEM_MAP/UNMAP do not allow atomically replacing partial ranges, but as far as I know that's not necessary for virtiofs in practice.
It's worth mentioning that mappings consume resources and that
SHMEM_MAPcan fail when there are no resources available. The process-wide limit is vm.max_map_count on Linux although a vhost-user frontend may reduce it further to control vhost-user resource usage.hw/virtio/vhost-user.c
Quoted 52 lines
docs/interop/vhost-user.rst +2
-0+ A reply is generated indicating whether unmapping succeeded. + .. _reply_ack: VHOST_USER_PROTOCOL_F_REPLY_ACKhw/virtio/vhost-user.c +21
-0@@ -115,6 +115,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;@@ -192,6 +194,24 @@ typedef struct VhostUserShared {unsigned char uuid[16 ]; } VhostUserShared; +/* For the flags field of VhostUserMMap */ +#define VHOST_USER_FLAG_MAP_R (1u << 0) +#define VHOST_USER_FLAG_MAP_W (1u << 1) + +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_* */ +uint64_t flags; +}VhostUserMMap ; + typedef struct { VhostUserRequest request;@@ -224,6 +244,7 @@ typedef union {VhostUserInflight inflight;VhostUserShared object;VhostUserTransferDeviceState transfer_state; +VhostUserMMap mmap; } VhostUserPayload; typedef struct VhostUserMsg {@@ -1748,6 +1769,100 @@vhost_user_backend_handle_shared_object_lookup(struct
vhost_user*u,Quoted 37 lines
hw/virtio/vhost-user.c +34
-0return 0 ; } +static int +vhost_user_backend_handle_shmem_map (struct vhost_dev *dev, +VhostUserMMap *vu_mmap, +int fd) +{ +void *addr =0 ; +MemoryRegion *mr =NULL ; + +if (fd <0 ) { +error_report ("Bad fd for map" ); +return -EBADF ; + } + +if (!dev->vdev->shmem_list || + dev->vdev->n_shmem_regions <= vu_mmap->shmid) { +error_report ("Device only has %d VIRTIO Shared Memory Regions. " +"Requested ID: %d" , + dev->vdev->n_shmem_regions, vu_mmap->shmid); +return -EFAULT ; + } + + mr = &dev->vdev->shmem_list[vu_mmap->shmid]; + +if (!mr) { +error_report ("VIRTIO Shared Memory Region at " +"ID %d unitialized" , vu_mmap->shmid); +return -EFAULT ; + } + +if ((vu_mmap->shm_offset + vu_mmap->len) < vu_mmap->len || + (vu_mmap->shm_offset + vu_mmap->len) > mr->size) { +error_report ("Bad offset/len for mmap %" PRIx64"+%" PRIx64, + vu_mmap->shm_offset, vu_mmap->len); +return -EFAULT ;
Quoted 9 lines
hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_lookup(struct vhost_user *u, · line 1810
Quoted 5 lines
hw/virtio/vhost-user.c +5
-0+ } + +void *shmem_ptr =memory_region_get_ram_ptr (mr); + + addr = mmap(shmem_ptr + vu_mmap->shm_offset, vu_mmap->len,Missing check for overlap between range [
shm_offset,shm_offset+ len) and existing mappings.
Not sure how to do this check. Specifically, I am not sure how previous ranges are stored within the MemoryRegion. Is looping through mr->subregions a valid option?
Quoted 322 lines
Quoted 36 lines
hw/virtio/vhost-user.c +35
-0+ ((vu_mmap->flags &VHOST_USER_FLAG_MAP_R ) ?PROT_READ :0 ) | + ((vu_mmap->flags &VHOST_USER_FLAG_MAP_W ) ?PROT_WRITE :0 ), + MAP_SHARED | MAP_FIXED, fd, vu_mmap->fd_offset); + +if (addr ==MAP_FAILED ) { +error_report ("Failed to mmap mem fd" ); +return -EFAULT ; + } + +return 0 ; +} + +static int +vhost_user_backend_handle_shmem_unmap (struct vhost_dev *dev, +VhostUserMMap *vu_mmap) +{ +void *addr =0 ; +MemoryRegion *mr =NULL ; + +if (!dev->vdev->shmem_list || + dev->vdev->n_shmem_regions <= vu_mmap->shmid) { +error_report ("Device only has %d VIRTIO Shared Memory Regions. " +"Requested ID: %d" , + dev->vdev->n_shmem_regions, vu_mmap->shmid); +return -EFAULT ; + } + + mr = &dev->vdev->shmem_list[vu_mmap->shmid]; + +if (!mr) { +error_report ("VIRTIO Shared Memory Region at " +"ID %d unitialized" , vu_mmap->shmid); +return -EFAULT ; + } +hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_lookup(struct vhost_user *u, · line 1855
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) > mr->size) { +error_report ("Bad offset/len for mmap %" PRIx64"+%" PRIx64, + vu_mmap->shm_offset, vu_mmap->len); +return -EFAULT ; + } + +void *shmem_ptr =memory_region_get_ram_ptr (mr); + + addr = mmap(shmem_ptr + vu_mmap->shm_offset, vu_mmap->len,Missing check for existing mapping with exact range [
shm_offset, len) match.hw/virtio/vhost-user.c line 1856
Quoted 1 line
hw/virtio/vhost-user.c +1
-0+ PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1,0);
hw/virtio/vhost-user.c static gboolean backend_read(QIOChannel *ioc,
Quoted 13 lines
hw/virtio/vhost-user.c +9
-0+ +if (addr ==MAP_FAILED ) { +error_report ("Failed to unmap memory" ); +return -EFAULT ; + } + +return 0 ; +} +static void close_backend_channel (struct vhost_user *u) {g_source_destroy (u->backend_src);@@ -1816,6 +1931,13 @@ static gboolean backend_read(QIOChannel *ioc,GIOCondition condition,
hw/virtio/vhost-user.c static gboolean backend_read(QIOChannel *ioc, · line 1931
Quoted 1 line
hw/virtio/vhost-user.c +0
-0ret =vhost_user_backend_handle_shared_object_lookup(dev->opaque, ioc,
hw/virtio/vhost-user.c static gboolean backend_read(QIOChannel *ioc, · line 1932
Quoted 1 line
hw/virtio/vhost-user.c +0
-0&hdr,&payload);
Quoted 16 lines
hw/virtio/vhost-user.c +7
-0break ; +case VHOST_USER_BACKEND_SHMEM_MAP : + ret =vhost_user_backend_handle_shmem_map (dev, &payload.mmap, + fd ? fd[0 ] :-1 ); +break ; +case VHOST_USER_BACKEND_SHMEM_UNMAP : + ret =vhost_user_backend_handle_shmem_unmap (dev, &payload.mmap); +break ;default :error_report ("Received unexpected msg type: %d." , hdr.request); ret = -EINVAL ;hw/virtio/virtio.c +0
-0hw/virtio/virtio.c int virtio_save(VirtIODevice *vdev, QEMUFile *f) · line 2864
Quoted 9 lines
hw/virtio/virtio.c +6
-0@@ -2856,6 +2856,16 @@ int virtio_save(VirtIODevice *vdev, QEMUFile *f)return vmstate_save_state (f, &vmstate_virtio, vdev,NULL ); } +MemoryRegion *virtio_new_shmem_region (VirtIODevice *vdev) +{ +MemoryRegion *mr =g_new0 (MemoryRegion,1 ); + ++vdev->n_shmem_regions; + vdev->shmem_list =g_renew (MemoryRegion, vdev->shmem_list, + vdev->n_shmem_regions);Where is
shmem_listfreed?The name "list" is misleading since this is an array, not a list.
hw/virtio/virtio.c int virtio_save(VirtIODevice *vdev, QEMUFile *f) · line 2867
Quoted 3 lines
hw/virtio/virtio.c +3
-0+ vdev->shmem_list[vdev->n_shmem_regions -1 ] = *mr; +return mr; +}This looks weird. The contents of mr are copied into shmem_list[] and then the pointer to mr is returned? Did you mean for the field's type to be MemoryRegion **shmem_list and then vdev->shmem_list[...] = mr would stash the pointer?
hw/virtio/virtio.c int virtio_save(VirtIODevice *vdev, QEMUFile *f)
Quoted 4 lines
hw/virtio/virtio.c +1
-0+/* 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)
hw/virtio/virtio.c void virtio_init(VirtIODevice *vdev, uint16_t
Quoted 1 line
hw/virtio/virtio.c +0
-0@@ -3264,6 +3274,8 @@ void virtio_init(VirtIODevice *vdev, uint16_t
device_id,size_tconfig_size)include/hw/virtio/virtio.h void virtio_notify(VirtIODevice *vdev, VirtQueue
Quoted 23 lines
hw/virtio/virtio.c +2
-0virtio_vmstate_change, vdev); vdev->device_endian =virtio_default_endian (); vdev->use_guest_notifier_mask = true; + vdev->shmem_list =NULL ; + vdev->n_shmem_regions =0 ; }/* include/hw/virtio/virtio.h +3
-0@@ -165,6 +165,9 @@ struct VirtIODevice*/ EventNotifier config_notifier;bool device_iotlb_enabled; +/* Shared memory region for vhost-user mappings. */ +MemoryRegion *shmem_list; +int n_shmem_regions; };struct VirtioDeviceClass {@@ -280,6 +283,8 @@ void virtio_notify(VirtIODevice *vdev, VirtQueue*vq);
Review
Quoted 9 lines
include/hw/virtio/virtio.h +2
-0int virtio_save (VirtIODevice *vdev,QEMUFile *f); +MemoryRegion *virtio_new_shmem_region (VirtIODevice *vdev); +extern const VMStateInfo virtio_vmstate_info;#define VMSTATE_VIRTIO_DEVICE \(quoted diff) +0
-0
b/subprojects/libvhost-user/libvhost-user.csubprojects/libvhost-user/libvhost-user.c vu_rm_shared_object(VuDev *dev, unsigned char
Quoted 4 lines
index
a879149fef..28556d183a100644subprojects/libvhost-user/libvhost-user.c +0
-0@@ -1586,6 +1586,71 @@ vu_rm_shared_object(VuDev *dev, unsigned charuuid[UUID_LEN])
Quoted 39 lines
subprojects/libvhost-user/libvhost-user.c +36
-0return 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) +{ +bool result = false; +VhostUserMsg msg_reply; +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, + }, + }; + +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 fd_offset, +uint64_t shm_offset,uint64_t len)subprojects/libvhost-user/libvhost-user.c vu_rm_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN]) · line 1634
Quoted 10 lines
subprojects/libvhost-user/libvhost-user.c +10
-0+{ +bool result = false; +VhostUserMsg msg_reply; +VhostUserMsg vmsg = { + .request =VHOST_USER_BACKEND_SHMEM_UNMAP , + .size =sizeof (vmsg.payload.mmap), + .flags =VHOST_USER_VERSION , + .payload.mmap = { + .shmid = shmid, + .fd_offset = fd_offset,What is the meaning of this field? I expected it to be set to 0.
Review
Quoted 23 lines
subprojects/libvhost-user/libvhost-user.c +19
-0+ .shm_offset = shm_offset, + .len = len, + }, + }; + +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) {(quoted diff) +0
-0
b/subprojects/libvhost-user/libvhost-user.hsubprojects/libvhost-user/libvhost-user.h bool vu_add_shared_object(VuDev *dev, unsigned char
Quoted 46 lines
index
deb40e77b3..7f6c22cc1a100644subprojects/libvhost-user/libvhost-user.h +21
-0@@ -127,6 +127,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 +188,24 @@ typedef struct VhostUserShared {unsigned char uuid[UUID_LEN ]; } VhostUserShared; +/* For the flags field of VhostUserMMap */ +#define VHOST_USER_FLAG_MAP_R (1u << 0) +#define VHOST_USER_FLAG_MAP_W (1u << 1) + +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_* */ +uint64_t flags; +}VhostUserMMap ; +#if defined(_WIN32) && (defined(__x86_64__) || defined(__i386__))# define VU_PACKED __attribute__((gcc_struct, packed))#else @@ -214,6 +234,7 @@ typedef struct VhostUserMsg {VhostUserVringArea area;VhostUserInflight inflight;VhostUserShared object; +VhostUserMMap mmap; } payload;int fds[VHOST_MEMORY_BASELINE_NREGIONS ];@@ -597,6 +618,38 @@ bool vu_add_shared_object(VuDev *dev, unsigned charuuid[UUID_LEN]);
subprojects/libvhost-user/libvhost-user.h bool vu_add_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN]);
Quoted 13 lines
subprojects/libvhost-user/libvhost-user.h +10
-0*/ 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 +* +* Advertises a new mapping to be made in a given VIRTIO Shared Memory Region.
Quoted 5 lines
subprojects/libvhost-user/libvhost-user.h +5
-0+* +* 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);subprojects/libvhost-user/libvhost-user.h bool vu_add_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN]);
Quoted 10 lines
subprojects/libvhost-user/libvhost-user.h +10
-0+ +/** +* 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 +* +* The front-end un-mmaps a given range in the VIRTIO Shared Memory Region
Quoted 13 lines
subprojects/libvhost-user/libvhost-user.h +7
-1+* with the requested `shmid`. +* +* Returns: TRUE on success, FALSE on failure. +*/ +bool vu_shmem_unmap (VuDev *dev,uint8_t shmid,uint64_t fd_offset, +uint64_t shm_offset,uint64_t len); +/** * vu_queue_set_notification: * @dev: a VuDev context -- 2.45.2
Re: [RFC PATCH v2 1/5] vhost-user: Add VIRTIO Shared Memory map request
Quoted 211 lines · Albert Esteve
Quoted 196 lines · Stefan Hajnoczi
Quoted 36 lines · Albert Esteve
Add SHMEM_MAP/UNMAP requests to vhost-user to handle VIRTIO Shared Memory mappings.
This request allows backends to dynamically map fds into a VIRTIO Shared Memory Region indentified by its
shmid. Then, the fd memory is advertised to the driver as a base addres + offset, so it can be read/written (depending on the mmap flags requested) while its valid.The backend can munmap the memory range in a given VIRTIO Shared Memory Region (again, identified by its
shmid), to free it. Upon receiving this message, the front-end must mmap the regions withPROT_NONEto reserve the virtual memory space.The device model needs to create MemoryRegion instances for the VIRTIO Shared Memory Regions and add them to the
VirtIODeviceinstance.Signed-off-by Albert Esteve <[email protected]>--- docs/interop/vhost-user.rst | 27 +++++ hw/virtio/vhost-user.c | 122 ++++++++++++++++++++++ hw/virtio/virtio.c | 12 +++ include/hw/virtio/virtio.h | 5 + subprojects/libvhost-user/libvhost-user.c | 65 ++++++++++++ subprojects/libvhost-user/libvhost-user.h | 53 ++++++++++ 6 files changed, 284 insertions(+)docs/interop/vhost-user.rst +0
-0docs/interop/vhost-user.rst is sent by the front-end. · line 1859
Quoted 1 line
docs/interop/vhost-user.rst +0
-0@@ -1859,6 +1859,33 @@ is sent by the front-end. when the operation is successful, or non-zero otherwise. Note thatif the
docs/interop/vhost-user.rst is sent by the front-end.
Quoted 9 lines
docs/interop/vhost-user.rst +7
-0operation 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 + + This message can be submitted by the backends to advertise a newmapping
docs/interop/vhost-user.rst is sent by the front-end. · line 1869
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ to be made in a given VIRTIO Shared Memory Region. Upon receivingthe message,
docs/interop/vhost-user.rst is sent by the front-end. · line 1870
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ The front-end will mmap the given fd into the VIRTIO Shared MemoryRegion
docs/interop/vhost-user.rst is sent by the front-end. · line 1871
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ with the requested ``shmid``. A reply is generated indicatingwhether mapping
docs/interop/vhost-user.rst is sent by the front-end.
Quoted 3 lines
docs/interop/vhost-user.rst +3
-0+ succeeded. + + Mapping over an already existing map is not allowed and requestshall fail.
docs/interop/vhost-user.rst is sent by the front-end. · line 1875
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ Therefore, the memory range in the request must correspond with avalid,
docs/interop/vhost-user.rst is sent by the front-end.
Quoted 9 lines
docs/interop/vhost-user.rst +9
-0+ free region of the VIRTIO Shared Memory Region. + +``VHOST_USER_BACKEND_SHMEM_UNMAP`` + :id: 10 + :equivalent ioctl: N/A + :request payload: ``struct VhostUserMMap`` + :reply payload: N/A + + This message can be submitted by the backends so that the front-endun-mmap
docs/interop/vhost-user.rst is sent by the front-end. · line 1885
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ a given range (``offset``, ``len``) in the VIRTIO Shared MemoryRegion with
s/offset/shm_offset/
docs/interop/vhost-user.rst is sent by the front-end. · line 1886
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ the requested ``shmid``.Please clarify that <offset, len> must correspond to the entirety of a valid mapped region.
By the way, the VIRTIO 1.3 gives the following behavior for the virtiofs DAX Window:
When a
FUSE_SETUPMAPPINGrequest perfectly overlaps a previous mapping, the previous mapping is replaced. When a mapping partially overlaps a previous mapping, the previous mapping is split into one or two smaller mappings. When a mapping is partially unmapped it is also split into one or two smaller mappings.Establishing new mappings or splitting existing mappings consumes resources. If the device runs out of resources the
FUSE_SETUPMAPPINGrequest fails until resources are available again followingFUSE_REMOVEMAPPING.I think SETUPMAPPING/REMOVMAPPING can be implemented using SHMEM_MAP/UNMAP. SHMEM_MAP/UNMAP do not allow atomically replacing partial ranges, but as far as I know that's not necessary for virtiofs in practice.
It's worth mentioning that mappings consume resources and that
SHMEM_MAPcan fail when there are no resources available. The process-wide limit is vm.max_map_count on Linux although a vhost-user frontend may reduce it further to control vhost-user resource usage.hw/virtio/vhost-user.c
Quoted 52 lines
docs/interop/vhost-user.rst +2
-0+ A reply is generated indicating whether unmapping succeeded. + .. _reply_ack: VHOST_USER_PROTOCOL_F_REPLY_ACKhw/virtio/vhost-user.c +21
-0@@ -115,6 +115,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;@@ -192,6 +194,24 @@ typedef struct VhostUserShared {unsigned char uuid[16 ]; } VhostUserShared; +/* For the flags field of VhostUserMMap */ +#define VHOST_USER_FLAG_MAP_R (1u << 0) +#define VHOST_USER_FLAG_MAP_W (1u << 1) + +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_* */ +uint64_t flags; +}VhostUserMMap ; + typedef struct { VhostUserRequest request;@@ -224,6 +244,7 @@ typedef union {VhostUserInflight inflight;VhostUserShared object;VhostUserTransferDeviceState transfer_state; +VhostUserMMap mmap; } VhostUserPayload; typedef struct VhostUserMsg {@@ -1748,6 +1769,100 @@vhost_user_backend_handle_shared_object_lookup(struct
vhost_user*u,Quoted 9 lines
hw/virtio/vhost-user.c +6
-0return 0 ; } +static int +vhost_user_backend_handle_shmem_map (struct vhost_dev *dev, +VhostUserMMap *vu_mmap, +int fd) +{ +void *addr =0 ;hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_lookup(struct vhost_user *u,
Quoted 10 lines
hw/virtio/vhost-user.c +10
-0+MemoryRegion *mr =NULL ; + +if (fd <0 ) { +error_report ("Bad fd for map" ); +return -EBADF ; + } + +if (!dev->vdev->shmem_list || + dev->vdev->n_shmem_regions <= vu_mmap->shmid) { + error_report("Device only has %d VIRTIO Shared Memory Regions."
Quoted 18 lines
hw/virtio/vhost-user.c +18
-0+"Requested ID: %d" , + dev->vdev->n_shmem_regions, vu_mmap->shmid); +return -EFAULT ; + } + + mr = &dev->vdev->shmem_list[vu_mmap->shmid]; + +if (!mr) { +error_report ("VIRTIO Shared Memory Region at " +"ID %d unitialized" , vu_mmap->shmid); +return -EFAULT ; + } + +if ((vu_mmap->shm_offset + vu_mmap->len) < vu_mmap->len || + (vu_mmap->shm_offset + vu_mmap->len) > mr->size) { +error_report ("Bad offset/len for mmap %" PRIx64"+%" PRIx64, + vu_mmap->shm_offset, vu_mmap->len); +return -EFAULT ;Quoted 9 lines
hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_lookup(struct vhost_user *u, · line 1810
Quoted 5 lines
hw/virtio/vhost-user.c +5
-0+ } + +void *shmem_ptr =memory_region_get_ram_ptr (mr); + + addr = mmap(shmem_ptr + vu_mmap->shm_offset, vu_mmap->len,Missing check for overlap between range [
shm_offset,shm_offset+ len) and existing mappings.
Quoted 5 lines
Not sure how to do this check. Specifically, I am not sure how previous ranges are stored within the MemoryRegion. Is looping through
mr->subregions
a valid option?
Maybe something like this would do?
```
if (memory_region_find(mr, vu_mmap->shm_offset, vu_mmap->len).mr) {
error_report("Requested memory (%" PRIx64 "+%" PRIx64 " overalps "
"with previously mapped memory",
vu_mmap->shm_offset, vu_mmap->len);
return -EFAULT;
}
```Quoted 329 lines
Quoted 328 lines
Quoted 15 lines
hw/virtio/vhost-user.c +12
-0+ ((vu_mmap->flags &VHOST_USER_FLAG_MAP_R ) ?PROT_READ :0 ) | + ((vu_mmap->flags &VHOST_USER_FLAG_MAP_W ) ?PROT_WRITE :0 ), + MAP_SHARED | MAP_FIXED, fd, vu_mmap->fd_offset); + +if (addr ==MAP_FAILED ) { +error_report ("Failed to mmap mem fd" ); +return -EFAULT ; + } + +return 0 ; +} +hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_lookup(struct vhost_user *u,
Quoted 10 lines
hw/virtio/vhost-user.c +10
-0+static int +vhost_user_backend_handle_shmem_unmap (struct vhost_dev *dev, +VhostUserMMap *vu_mmap) +{ +void *addr =0 ; +MemoryRegion *mr =NULL ; + +if (!dev->vdev->shmem_list || + dev->vdev->n_shmem_regions <= vu_mmap->shmid) { + error_report("Device only has %d VIRTIO Shared Memory Regions."
Quoted 13 lines
hw/virtio/vhost-user.c +13
-0+"Requested ID: %d" , + dev->vdev->n_shmem_regions, vu_mmap->shmid); +return -EFAULT ; + } + + mr = &dev->vdev->shmem_list[vu_mmap->shmid]; + +if (!mr) { +error_report ("VIRTIO Shared Memory Region at " +"ID %d unitialized" , vu_mmap->shmid); +return -EFAULT ; + } +hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_lookup(struct vhost_user *u, · line 1810
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) > mr->size) { +error_report ("Bad offset/len for mmap %" PRIx64"+%" PRIx64, + vu_mmap->shm_offset, vu_mmap->len); +return -EFAULT ; + } + +void *shmem_ptr =memory_region_get_ram_ptr (mr); + + addr = mmap(shmem_ptr + vu_mmap->shm_offset, vu_mmap->len,Missing check for existing mapping with exact range [
shm_offset, len) match.hw/virtio/vhost-user.c line 1856
Quoted 1 line
hw/virtio/vhost-user.c +1
-0+ PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,-1, 0);
hw/virtio/vhost-user.c static gboolean backend_read(QIOChannel *ioc,
Quoted 13 lines
hw/virtio/vhost-user.c +9
-0+ +if (addr ==MAP_FAILED ) { +error_report ("Failed to unmap memory" ); +return -EFAULT ; + } + +return 0 ; +} +static void close_backend_channel (struct vhost_user *u) {g_source_destroy (u->backend_src);@@ -1816,6 +1931,13 @@ static gboolean backend_read(QIOChannel *ioc,GIOCondition condition,
hw/virtio/vhost-user.c static gboolean backend_read(QIOChannel *ioc, · line 1931
Quoted 1 line
hw/virtio/vhost-user.c +0
-0ret =vhost_user_backend_handle_shared_object_lookup(dev->opaque, ioc,
hw/virtio/vhost-user.c static gboolean backend_read(QIOChannel *ioc, · line 1932
Quoted 1 line
hw/virtio/vhost-user.c +0
-0&hdr,&payload);
hw/virtio/vhost-user.c static gboolean backend_read(QIOChannel *ioc, GIOCondition condition,
Quoted 7 lines
hw/virtio/vhost-user.c +6
-0break ; +case VHOST_USER_BACKEND_SHMEM_MAP : + ret =vhost_user_backend_handle_shmem_map (dev, &payload.mmap, + fd ? fd[0 ] :-1 ); +break ; +case VHOST_USER_BACKEND_SHMEM_UNMAP : + ret = vhost_user_backend_handle_shmem_unmap(dev,&payload.mmap);
Quoted 9 lines
hw/virtio/vhost-user.c +1
-0+break ;default :error_report ("Received unexpected msg type: %d." , hdr.request); ret = -EINVAL ;hw/virtio/virtio.c +0
-0hw/virtio/virtio.c int virtio_save(VirtIODevice *vdev, QEMUFile *f) · line 2864
Quoted 9 lines
hw/virtio/virtio.c +6
-0@@ -2856,6 +2856,16 @@ int virtio_save(VirtIODevice *vdev, QEMUFile *f)return vmstate_save_state (f, &vmstate_virtio, vdev,NULL ); } +MemoryRegion *virtio_new_shmem_region (VirtIODevice *vdev) +{ +MemoryRegion *mr =g_new0 (MemoryRegion,1 ); + ++vdev->n_shmem_regions; + vdev->shmem_list =g_renew (MemoryRegion, vdev->shmem_list, + vdev->n_shmem_regions);Where is
shmem_listfreed?The name "list" is misleading since this is an array, not a list.
hw/virtio/virtio.c int virtio_save(VirtIODevice *vdev, QEMUFile *f) · line 2867
Quoted 3 lines
hw/virtio/virtio.c +3
-0+ vdev->shmem_list[vdev->n_shmem_regions -1 ] = *mr; +return mr; +}This looks weird. The contents of mr are copied into shmem_list[] and then the pointer to mr is returned? Did you mean for the field's type to be MemoryRegion **shmem_list and then vdev->shmem_list[...] = mr would stash the pointer?
hw/virtio/virtio.c int virtio_save(VirtIODevice *vdev, QEMUFile *f)
Quoted 4 lines
hw/virtio/virtio.c +1
-0+/* 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)
hw/virtio/virtio.c void virtio_init(VirtIODevice *vdev, uint16_t
Quoted 1 line
hw/virtio/virtio.c +0
-0@@ -3264,6 +3274,8 @@ void virtio_init(VirtIODevice *vdev, uint16_t
device_id,size_tconfig_size)include/hw/virtio/virtio.h void virtio_notify(VirtIODevice *vdev, VirtQueue
Quoted 23 lines
hw/virtio/virtio.c +2
-0virtio_vmstate_change, vdev); vdev->device_endian =virtio_default_endian (); vdev->use_guest_notifier_mask = true; + vdev->shmem_list =NULL ; + vdev->n_shmem_regions =0 ; }/* include/hw/virtio/virtio.h +3
-0@@ -165,6 +165,9 @@ struct VirtIODevice*/ EventNotifier config_notifier;bool device_iotlb_enabled; +/* Shared memory region for vhost-user mappings. */ +MemoryRegion *shmem_list; +int n_shmem_regions; };struct VirtioDeviceClass {@@ -280,6 +283,8 @@ void virtio_notify(VirtIODevice *vdev, VirtQueue*vq);
Review
Quoted 9 lines
include/hw/virtio/virtio.h +2
-0int virtio_save (VirtIODevice *vdev,QEMUFile *f); +MemoryRegion *virtio_new_shmem_region (VirtIODevice *vdev); +extern const VMStateInfo virtio_vmstate_info;#define VMSTATE_VIRTIO_DEVICE \(quoted diff) +0
-0
b/subprojects/libvhost-user/libvhost-user.csubprojects/libvhost-user/libvhost-user.c vu_rm_shared_object(VuDev *dev, unsigned char
Quoted 4 lines
index
a879149fef..28556d183a100644subprojects/libvhost-user/libvhost-user.c +0
-0@@ -1586,6 +1586,71 @@ vu_rm_shared_object(VuDev *dev, unsigned charuuid[UUID_LEN])
Quoted 13 lines
subprojects/libvhost-user/libvhost-user.c +10
-0return 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) +{ +bool result = false; +VhostUserMsg msg_reply; +VhostUserMsg vmsg = { + .request =VHOST_USER_BACKEND_SHMEM_MAP , + .size =sizeof (vmsg.payload.mmap), + .flags =VHOST_USER_VERSION ,subprojects/libvhost-user/libvhost-user.c vu_rm_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN])
Quoted 10 lines
subprojects/libvhost-user/libvhost-user.c +10
-0+ .payload.mmap = { + .shmid = shmid, + .fd_offset = fd_offset, + .shm_offset = shm_offset, + .len = len, + .flags = flags, + }, + }; + +if (vu_has_protocol_feature (dev,VHOST_USER_PROTOCOL_F_REPLY_ACK )){
Quoted 16 lines
subprojects/libvhost-user/libvhost-user.c +16
-0+ 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 fd_offset, +uint64_t shm_offset,uint64_t len)subprojects/libvhost-user/libvhost-user.c vu_rm_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN]) · line 1634
Quoted 10 lines
subprojects/libvhost-user/libvhost-user.c +10
-0+{ +bool result = false; +VhostUserMsg msg_reply; +VhostUserMsg vmsg = { + .request =VHOST_USER_BACKEND_SHMEM_UNMAP , + .size =sizeof (vmsg.payload.mmap), + .flags =VHOST_USER_VERSION , + .payload.mmap = { + .shmid = shmid, + .fd_offset = fd_offset,What is the meaning of this field? I expected it to be set to 0.
subprojects/libvhost-user/libvhost-user.c vu_rm_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN])
Quoted 6 lines
subprojects/libvhost-user/libvhost-user.c +6
-0+ .shm_offset = shm_offset, + .len = len, + }, + }; + +if (vu_has_protocol_feature (dev,VHOST_USER_PROTOCOL_F_REPLY_ACK )){
Review
Quoted 17 lines
subprojects/libvhost-user/libvhost-user.c +13
-0+ 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) {(quoted diff) +0
-0
b/subprojects/libvhost-user/libvhost-user.hsubprojects/libvhost-user/libvhost-user.h bool vu_add_shared_object(VuDev *dev, unsigned
Quoted 46 lines
index
deb40e77b3..7f6c22cc1a100644subprojects/libvhost-user/libvhost-user.h +21
-0@@ -127,6 +127,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 +188,24 @@ typedef struct VhostUserShared {unsigned char uuid[UUID_LEN ]; } VhostUserShared; +/* For the flags field of VhostUserMMap */ +#define VHOST_USER_FLAG_MAP_R (1u << 0) +#define VHOST_USER_FLAG_MAP_W (1u << 1) + +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_* */ +uint64_t flags; +}VhostUserMMap ; +#if defined(_WIN32) && (defined(__x86_64__) || defined(__i386__))# define VU_PACKED __attribute__((gcc_struct, packed))#else @@ -214,6 +234,7 @@ typedef struct VhostUserMsg {VhostUserVringArea area;VhostUserInflight inflight;VhostUserShared object; +VhostUserMMap mmap; } payload;int fds[VHOST_MEMORY_BASELINE_NREGIONS ];@@ -597,6 +618,38 @@ bool vu_add_shared_object(VuDev *dev, unsignedchar uuid[UUID_LEN]);
subprojects/libvhost-user/libvhost-user.h bool vu_add_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN]);
Quoted 13 lines
subprojects/libvhost-user/libvhost-user.h +10
-0*/ 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 +* +* Advertises a new mapping to be made in a given VIRTIO Shared Memory Region.
Quoted 5 lines
subprojects/libvhost-user/libvhost-user.h +5
-0+* +* 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);subprojects/libvhost-user/libvhost-user.h bool vu_add_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN]);
Quoted 10 lines
subprojects/libvhost-user/libvhost-user.h +10
-0+ +/** +* 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 +* +* The front-end un-mmaps a given range in the VIRTIO Shared Memory Region
Quoted 13 lines
subprojects/libvhost-user/libvhost-user.h +7
-1+* with the requested `shmid`. +* +* Returns: TRUE on success, FALSE on failure. +*/ +bool vu_shmem_unmap (VuDev *dev,uint8_t shmid,uint64_t fd_offset, +uint64_t shm_offset,uint64_t len); +/** * vu_queue_set_notification: * @dev: a VuDev context -- 2.45.2
Re: [RFC PATCH v2 1/5] vhost-user: Add VIRTIO Shared Memory map request
Quoted 220 lines · Albert Esteve
Quoted 211 lines · Albert Esteve
Quoted 196 lines · Stefan Hajnoczi
Quoted 36 lines · Albert Esteve
Add SHMEM_MAP/UNMAP requests to vhost-user to handle VIRTIO Shared Memory mappings.
This request allows backends to dynamically map fds into a VIRTIO Shared Memory Region indentified by its
shmid. Then, the fd memory is advertised to the driver as a base addres + offset, so it can be read/written (depending on the mmap flags requested) while its valid.The backend can munmap the memory range in a given VIRTIO Shared Memory Region (again, identified by its
shmid), to free it. Upon receiving this message, the front-end must mmap the regions withPROT_NONEto reserve the virtual memory space.The device model needs to create MemoryRegion instances for the VIRTIO Shared Memory Regions and add them to the
VirtIODeviceinstance.Signed-off-by Albert Esteve <[email protected]>--- docs/interop/vhost-user.rst | 27 +++++ hw/virtio/vhost-user.c | 122 ++++++++++++++++++++++ hw/virtio/virtio.c | 12 +++ include/hw/virtio/virtio.h | 5 + subprojects/libvhost-user/libvhost-user.c | 65 ++++++++++++ subprojects/libvhost-user/libvhost-user.h | 53 ++++++++++ 6 files changed, 284 insertions(+)docs/interop/vhost-user.rst +0
-0docs/interop/vhost-user.rst is sent by the front-end. · line 1859
Quoted 1 line
docs/interop/vhost-user.rst +0
-0@@ -1859,6 +1859,33 @@ is sent by the front-end. when the operation is successful, or non-zero otherwise. Note thatif the
docs/interop/vhost-user.rst is sent by the front-end.
Quoted 9 lines
docs/interop/vhost-user.rst +7
-0operation 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 + + This message can be submitted by the backends to advertise a newmapping
docs/interop/vhost-user.rst is sent by the front-end. · line 1869
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ to be made in a given VIRTIO Shared Memory Region. Upon receivingthe message,
docs/interop/vhost-user.rst is sent by the front-end. · line 1870
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ The front-end will mmap the given fd into the VIRTIO Shared MemoryRegion
docs/interop/vhost-user.rst is sent by the front-end. · line 1871
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ with the requested ``shmid``. A reply is generated indicatingwhether mapping
docs/interop/vhost-user.rst is sent by the front-end.
Quoted 3 lines
docs/interop/vhost-user.rst +3
-0+ succeeded. + + Mapping over an already existing map is not allowed and requestshall fail.
docs/interop/vhost-user.rst is sent by the front-end. · line 1875
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ Therefore, the memory range in the request must correspond with avalid,
docs/interop/vhost-user.rst is sent by the front-end.
Quoted 9 lines
docs/interop/vhost-user.rst +9
-0+ free region of the VIRTIO Shared Memory Region. + +``VHOST_USER_BACKEND_SHMEM_UNMAP`` + :id: 10 + :equivalent ioctl: N/A + :request payload: ``struct VhostUserMMap`` + :reply payload: N/A + + This message can be submitted by the backends so that the front-endun-mmap
docs/interop/vhost-user.rst is sent by the front-end. · line 1885
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ a given range (``offset``, ``len``) in the VIRTIO Shared MemoryRegion with
s/offset/shm_offset/
docs/interop/vhost-user.rst is sent by the front-end. · line 1886
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ the requested ``shmid``.Please clarify that <offset, len> must correspond to the entirety of a valid mapped region.
By the way, the VIRTIO 1.3 gives the following behavior for the virtiofs DAX Window:
When a
FUSE_SETUPMAPPINGrequest perfectly overlaps a previous mapping, the previous mapping is replaced. When a mapping partially overlaps a previous mapping, the previous mapping is split into one or two smaller mappings. When a mapping is partially unmapped it is also split into one or two smaller mappings.Establishing new mappings or splitting existing mappings consumes resources. If the device runs out of resources the
FUSE_SETUPMAPPINGrequest fails until resources are available again followingFUSE_REMOVEMAPPING.I think SETUPMAPPING/REMOVMAPPING can be implemented using SHMEM_MAP/UNMAP. SHMEM_MAP/UNMAP do not allow atomically replacing partial ranges, but as far as I know that's not necessary for virtiofs in practice.
It's worth mentioning that mappings consume resources and that
SHMEM_MAPcan fail when there are no resources available. The process-wide limit is vm.max_map_count on Linux although a vhost-user frontend may reduce it further to control vhost-user resource usage.hw/virtio/vhost-user.c
Quoted 52 lines
docs/interop/vhost-user.rst +2
-0+ A reply is generated indicating whether unmapping succeeded. + .. _reply_ack: VHOST_USER_PROTOCOL_F_REPLY_ACKhw/virtio/vhost-user.c +21
-0@@ -115,6 +115,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;@@ -192,6 +194,24 @@ typedef struct VhostUserShared {unsigned char uuid[16 ]; } VhostUserShared; +/* For the flags field of VhostUserMMap */ +#define VHOST_USER_FLAG_MAP_R (1u << 0) +#define VHOST_USER_FLAG_MAP_W (1u << 1) + +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_* */ +uint64_t flags; +}VhostUserMMap ; + typedef struct { VhostUserRequest request;@@ -224,6 +244,7 @@ typedef union {VhostUserInflight inflight;VhostUserShared object;VhostUserTransferDeviceState transfer_state; +VhostUserMMap mmap; } VhostUserPayload; typedef struct VhostUserMsg {@@ -1748,6 +1769,100 @@vhost_user_backend_handle_shared_object_lookup(struct
vhost_user*u,Quoted 9 lines
hw/virtio/vhost-user.c +6
-0return 0 ; } +static int +vhost_user_backend_handle_shmem_map (struct vhost_dev *dev, +VhostUserMMap *vu_mmap, +int fd) +{ +void *addr =0 ;hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_lookup(struct vhost_user *u,
Quoted 10 lines
hw/virtio/vhost-user.c +10
-0+MemoryRegion *mr =NULL ; + +if (fd <0 ) { +error_report ("Bad fd for map" ); +return -EBADF ; + } + +if (!dev->vdev->shmem_list || + dev->vdev->n_shmem_regions <= vu_mmap->shmid) { + error_report("Device only has %d VIRTIO Shared Memory Regions."
Quoted 18 lines
hw/virtio/vhost-user.c +18
-0+"Requested ID: %d" , + dev->vdev->n_shmem_regions, vu_mmap->shmid); +return -EFAULT ; + } + + mr = &dev->vdev->shmem_list[vu_mmap->shmid]; + +if (!mr) { +error_report ("VIRTIO Shared Memory Region at " +"ID %d unitialized" , vu_mmap->shmid); +return -EFAULT ; + } + +if ((vu_mmap->shm_offset + vu_mmap->len) < vu_mmap->len || + (vu_mmap->shm_offset + vu_mmap->len) > mr->size) { +error_report ("Bad offset/len for mmap %" PRIx64"+%" PRIx64, + vu_mmap->shm_offset, vu_mmap->len); +return -EFAULT ;Quoted 9 lines
hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_lookup(struct vhost_user *u, · line 1810
Quoted 5 lines
hw/virtio/vhost-user.c +5
-0+ } + +void *shmem_ptr =memory_region_get_ram_ptr (mr); + + addr = mmap(shmem_ptr + vu_mmap->shm_offset, vu_mmap->len,Missing check for overlap between range [
shm_offset,shm_offset+ len) and existing mappings.Quoted 5 lines
Not sure how to do this check. Specifically, I am not sure how previous ranges are stored within the MemoryRegion. Is looping through
mr->subregions
a valid option?Maybe something like this would do?
Quoted 8 lines
```
if (memory_region_find(mr, vu_mmap->shm_offset, vu_mmap->len).mr) {
error_report("Requested memory (%" PRIx64 "+%" PRIx64 " overalps "
"with previously mapped memory",
vu_mmap->shm_offset, vu_mmap->len);
return -EFAULT;
}
```I don't think that works because the QEMU MemoryRegion covers the entire range, some of which contains mappings and some of which is empty. It would be necessary to track mappings that have been made.
I'm not aware of a security implication if the overlap check is missing, so I guess it may be okay to skip it and rely on the vhost-user back-end author to honor the spec. I'm not totally against that because it's faster and less code, but it feels a bit iffy to not enforce the input validation that the spec requires.
Maintain a list of mappings so this check can be performed?
Quoted 330 lines
Quoted 330 lines
Quoted 329 lines
Quoted 16 lines
hw/virtio/vhost-user.c +12
-0+ ((vu_mmap->flags &VHOST_USER_FLAG_MAP_R ) ?PROT_READ :0 ) | + ((vu_mmap->flags &VHOST_USER_FLAG_MAP_W ) ?PROT_WRITE :0 ), + MAP_SHARED | MAP_FIXED, fd, vu_mmap->fd_offset); + +if (addr ==MAP_FAILED ) { +error_report ("Failed to mmap mem fd" ); +return -EFAULT ; + } + +return 0 ; +} +hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_lookup(struct vhost_user *u,
Quoted 10 lines
hw/virtio/vhost-user.c +10
-0+static int +vhost_user_backend_handle_shmem_unmap (struct vhost_dev *dev, +VhostUserMMap *vu_mmap) +{ +void *addr =0 ; +MemoryRegion *mr =NULL ; + +if (!dev->vdev->shmem_list || + dev->vdev->n_shmem_regions <= vu_mmap->shmid) { + error_report("Device only has %d VIRTIO Shared Memory Regions."
Quoted 13 lines
hw/virtio/vhost-user.c +13
-0+"Requested ID: %d" , + dev->vdev->n_shmem_regions, vu_mmap->shmid); +return -EFAULT ; + } + + mr = &dev->vdev->shmem_list[vu_mmap->shmid]; + +if (!mr) { +error_report ("VIRTIO Shared Memory Region at " +"ID %d unitialized" , vu_mmap->shmid); +return -EFAULT ; + } +hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_lookup(struct vhost_user *u, · line 1810
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) > mr->size) { +error_report ("Bad offset/len for mmap %" PRIx64"+%" PRIx64, + vu_mmap->shm_offset, vu_mmap->len); +return -EFAULT ; + } + +void *shmem_ptr =memory_region_get_ram_ptr (mr); + + addr = mmap(shmem_ptr + vu_mmap->shm_offset, vu_mmap->len,Missing check for existing mapping with exact range [
shm_offset, len) match.hw/virtio/vhost-user.c line 1856
Quoted 1 line
hw/virtio/vhost-user.c +1
-0+ PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,-1, 0);
hw/virtio/vhost-user.c static gboolean backend_read(QIOChannel *ioc,
Quoted 13 lines
hw/virtio/vhost-user.c +9
-0+ +if (addr ==MAP_FAILED ) { +error_report ("Failed to unmap memory" ); +return -EFAULT ; + } + +return 0 ; +} +static void close_backend_channel (struct vhost_user *u) {g_source_destroy (u->backend_src);@@ -1816,6 +1931,13 @@ static gboolean backend_read(QIOChannel *ioc,GIOCondition condition,
hw/virtio/vhost-user.c static gboolean backend_read(QIOChannel *ioc, · line 1931
Quoted 1 line
hw/virtio/vhost-user.c +0
-0ret =vhost_user_backend_handle_shared_object_lookup(dev->opaque, ioc,
hw/virtio/vhost-user.c static gboolean backend_read(QIOChannel *ioc, · line 1932
Quoted 1 line
hw/virtio/vhost-user.c +0
-0&hdr,&payload);
hw/virtio/vhost-user.c static gboolean backend_read(QIOChannel *ioc, GIOCondition condition,
Quoted 7 lines
hw/virtio/vhost-user.c +6
-0break ; +case VHOST_USER_BACKEND_SHMEM_MAP : + ret =vhost_user_backend_handle_shmem_map (dev, &payload.mmap, + fd ? fd[0 ] :-1 ); +break ; +case VHOST_USER_BACKEND_SHMEM_UNMAP : + ret = vhost_user_backend_handle_shmem_unmap(dev,&payload.mmap);
Quoted 9 lines
hw/virtio/vhost-user.c +1
-0+break ;default :error_report ("Received unexpected msg type: %d." , hdr.request); ret = -EINVAL ;hw/virtio/virtio.c +0
-0hw/virtio/virtio.c int virtio_save(VirtIODevice *vdev, QEMUFile *f) · line 2864
Quoted 9 lines
hw/virtio/virtio.c +6
-0@@ -2856,6 +2856,16 @@ int virtio_save(VirtIODevice *vdev, QEMUFile *f)return vmstate_save_state (f, &vmstate_virtio, vdev,NULL ); } +MemoryRegion *virtio_new_shmem_region (VirtIODevice *vdev) +{ +MemoryRegion *mr =g_new0 (MemoryRegion,1 ); + ++vdev->n_shmem_regions; + vdev->shmem_list =g_renew (MemoryRegion, vdev->shmem_list, + vdev->n_shmem_regions);Where is
shmem_listfreed?The name "list" is misleading since this is an array, not a list.
hw/virtio/virtio.c int virtio_save(VirtIODevice *vdev, QEMUFile *f) · line 2867
Quoted 3 lines
hw/virtio/virtio.c +3
-0+ vdev->shmem_list[vdev->n_shmem_regions -1 ] = *mr; +return mr; +}This looks weird. The contents of mr are copied into shmem_list[] and then the pointer to mr is returned? Did you mean for the field's type to be MemoryRegion **shmem_list and then vdev->shmem_list[...] = mr would stash the pointer?
hw/virtio/virtio.c int virtio_save(VirtIODevice *vdev, QEMUFile *f)
Quoted 4 lines
hw/virtio/virtio.c +1
-0+/* 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)
hw/virtio/virtio.c void virtio_init(VirtIODevice *vdev, uint16_t
Quoted 1 line
hw/virtio/virtio.c +0
-0@@ -3264,6 +3274,8 @@ void virtio_init(VirtIODevice *vdev, uint16_t
device_id,size_tconfig_size)include/hw/virtio/virtio.h void virtio_notify(VirtIODevice *vdev, VirtQueue
Quoted 23 lines
hw/virtio/virtio.c +2
-0virtio_vmstate_change, vdev); vdev->device_endian =virtio_default_endian (); vdev->use_guest_notifier_mask = true; + vdev->shmem_list =NULL ; + vdev->n_shmem_regions =0 ; }/* include/hw/virtio/virtio.h +3
-0@@ -165,6 +165,9 @@ struct VirtIODevice*/ EventNotifier config_notifier;bool device_iotlb_enabled; +/* Shared memory region for vhost-user mappings. */ +MemoryRegion *shmem_list; +int n_shmem_regions; };struct VirtioDeviceClass {@@ -280,6 +283,8 @@ void virtio_notify(VirtIODevice *vdev, VirtQueue*vq);
Review
Quoted 9 lines
include/hw/virtio/virtio.h +2
-0int virtio_save (VirtIODevice *vdev,QEMUFile *f); +MemoryRegion *virtio_new_shmem_region (VirtIODevice *vdev); +extern const VMStateInfo virtio_vmstate_info;#define VMSTATE_VIRTIO_DEVICE \(quoted diff) +0
-0
b/subprojects/libvhost-user/libvhost-user.csubprojects/libvhost-user/libvhost-user.c vu_rm_shared_object(VuDev *dev, unsigned char
Quoted 4 lines
index
a879149fef..28556d183a100644subprojects/libvhost-user/libvhost-user.c +0
-0@@ -1586,6 +1586,71 @@ vu_rm_shared_object(VuDev *dev, unsigned charuuid[UUID_LEN])
Quoted 13 lines
subprojects/libvhost-user/libvhost-user.c +10
-0return 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) +{ +bool result = false; +VhostUserMsg msg_reply; +VhostUserMsg vmsg = { + .request =VHOST_USER_BACKEND_SHMEM_MAP , + .size =sizeof (vmsg.payload.mmap), + .flags =VHOST_USER_VERSION ,subprojects/libvhost-user/libvhost-user.c vu_rm_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN])
Quoted 10 lines
subprojects/libvhost-user/libvhost-user.c +10
-0+ .payload.mmap = { + .shmid = shmid, + .fd_offset = fd_offset, + .shm_offset = shm_offset, + .len = len, + .flags = flags, + }, + }; + +if (vu_has_protocol_feature (dev,VHOST_USER_PROTOCOL_F_REPLY_ACK )){
Quoted 16 lines
subprojects/libvhost-user/libvhost-user.c +16
-0+ 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 fd_offset, +uint64_t shm_offset,uint64_t len)subprojects/libvhost-user/libvhost-user.c vu_rm_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN]) · line 1634
Quoted 10 lines
subprojects/libvhost-user/libvhost-user.c +10
-0+{ +bool result = false; +VhostUserMsg msg_reply; +VhostUserMsg vmsg = { + .request =VHOST_USER_BACKEND_SHMEM_UNMAP , + .size =sizeof (vmsg.payload.mmap), + .flags =VHOST_USER_VERSION , + .payload.mmap = { + .shmid = shmid, + .fd_offset = fd_offset,What is the meaning of this field? I expected it to be set to 0.
subprojects/libvhost-user/libvhost-user.c vu_rm_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN])
Quoted 6 lines
subprojects/libvhost-user/libvhost-user.c +6
-0+ .shm_offset = shm_offset, + .len = len, + }, + }; + +if (vu_has_protocol_feature (dev,VHOST_USER_PROTOCOL_F_REPLY_ACK )){
Review
Quoted 17 lines
subprojects/libvhost-user/libvhost-user.c +13
-0+ 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) {(quoted diff) +0
-0
b/subprojects/libvhost-user/libvhost-user.hsubprojects/libvhost-user/libvhost-user.h bool vu_add_shared_object(VuDev *dev, unsigned
Quoted 46 lines
index
deb40e77b3..7f6c22cc1a100644subprojects/libvhost-user/libvhost-user.h +21
-0@@ -127,6 +127,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 +188,24 @@ typedef struct VhostUserShared {unsigned char uuid[UUID_LEN ]; } VhostUserShared; +/* For the flags field of VhostUserMMap */ +#define VHOST_USER_FLAG_MAP_R (1u << 0) +#define VHOST_USER_FLAG_MAP_W (1u << 1) + +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_* */ +uint64_t flags; +}VhostUserMMap ; +#if defined(_WIN32) && (defined(__x86_64__) || defined(__i386__))# define VU_PACKED __attribute__((gcc_struct, packed))#else @@ -214,6 +234,7 @@ typedef struct VhostUserMsg {VhostUserVringArea area;VhostUserInflight inflight;VhostUserShared object; +VhostUserMMap mmap; } payload;int fds[VHOST_MEMORY_BASELINE_NREGIONS ];@@ -597,6 +618,38 @@ bool vu_add_shared_object(VuDev *dev, unsignedchar uuid[UUID_LEN]);
subprojects/libvhost-user/libvhost-user.h bool vu_add_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN]);
Quoted 13 lines
subprojects/libvhost-user/libvhost-user.h +10
-0*/ 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 +* +* Advertises a new mapping to be made in a given VIRTIO Shared Memory Region.
Quoted 5 lines
subprojects/libvhost-user/libvhost-user.h +5
-0+* +* 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);subprojects/libvhost-user/libvhost-user.h bool vu_add_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN]);
Quoted 10 lines
subprojects/libvhost-user/libvhost-user.h +10
-0+ +/** +* 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 +* +* The front-end un-mmaps a given range in the VIRTIO Shared Memory Region
Quoted 13 lines
subprojects/libvhost-user/libvhost-user.h +7
-1+* with the requested `shmid`. +* +* Returns: TRUE on success, FALSE on failure. +*/ +bool vu_shmem_unmap (VuDev *dev,uint8_t shmid,uint64_t fd_offset, +uint64_t shm_offset,uint64_t len); +/** * vu_queue_set_notification: * @dev: a VuDev context -- 2.45.2
Re: [RFC PATCH v2 1/5] vhost-user: Add VIRTIO Shared Memory map request
Quoted 247 lines · Stefan Hajnoczi
Quoted 1 line · Albert Esteve
On Tue, Sep 3, 2024 at 11:54 AM Albert Esteve <[email protected]>
Quoted 24 lines · wrote:
Quoted 24 lines
Quoted 18 lines · Stefan Hajnoczi
Quoted 17 lines · Albert Esteve
Add SHMEM_MAP/UNMAP requests to vhost-user to handle VIRTIO Shared Memory mappings.
This request allows backends to dynamically map fds into a VIRTIO Shared Memory Region indentified by its
shmid. Then, the fd memory is advertised to the driver as a base addres + offset, so it can be read/written (depending on the mmap flags requested) while its valid.The backend can munmap the memory range in a given VIRTIO Shared Memory Region (again, identified by its
shmid), to free it. Upon receiving this message, the front-end must mmap the regions withPROT_NONEto reserve the virtual memory space.Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
The device model needs to create MemoryRegion instances for the VIRTIO Shared Memory Regions and add them to the
VirtIODeviceinstance.Signed-off-by Albert Esteve <[email protected]>--- docs/interop/vhost-user.rst | 27 +++++ hw/virtio/vhost-user.c | 122++++++++++++++++++++++
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Review
Quoted 7 lines
hw/virtio/virtio.c | 12 +++ include/hw/virtio/virtio.h | 5 + subprojects/libvhost-user/libvhost-user.c | 65 ++++++++++++ subprojects/libvhost-user/libvhost-user.h | 53 ++++++++++ 6 files changed, 284 insertions(+)(quoted diff) +0
-0
b/docs/interop/vhost-user.rstQuoted 5 lines
Quoted 5 lines
Quoted 5 lines
docs/interop/vhost-user.rst is sent by the front-end. · line 1859
Quoted 5 lines
index
d8419fd2f1..d52ba719d5100644docs/interop/vhost-user.rst +0
-0@@ -1859,6 +1859,33 @@ is sent by the front-end. when the operation is successful, or non-zero otherwise. Notethat
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
if the
Quoted 9 lines
docs/interop/vhost-user.rst +7
-0operation 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 + + This message can be submitted by the backends to advertise a newQuoted 4 lines
Quoted 4 lines
Quoted 4 lines
mapping
docs/interop/vhost-user.rst is sent by the front-end. · line 1869
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ to be made in a given VIRTIO Shared Memory Region. Upon receivingthe message,
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ The front-end will mmap the given fd into the VIRTIO SharedMemory
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Region
docs/interop/vhost-user.rst is sent by the front-end. · line 1871
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ with the requested ``shmid``. A reply is generated indicatingwhether mapping
docs/interop/vhost-user.rst is sent by the front-end.
Quoted 3 lines
docs/interop/vhost-user.rst +3
-0+ succeeded. + + Mapping over an already existing map is not allowed and requestshall fail.
docs/interop/vhost-user.rst is sent by the front-end. · line 1875
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ Therefore, the memory range in the request must correspond with avalid,
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
docs/interop/vhost-user.rst is sent by the front-end.
Quoted 9 lines
docs/interop/vhost-user.rst +9
-0+ free region of the VIRTIO Shared Memory Region. + +``VHOST_USER_BACKEND_SHMEM_UNMAP`` + :id: 10 + :equivalent ioctl: N/A + :request payload: ``struct VhostUserMMap`` + :reply payload: N/A + + This message can be submitted by the backends so that thefront-end
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
un-mmap
docs/interop/vhost-user.rst is sent by the front-end. · line 1885
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ a given range (``offset``, ``len``) in the VIRTIO Shared MemoryRegion with
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
s/offset/shm_offset/
docs/interop/vhost-user.rst is sent by the front-end. · line 1886
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ the requested ``shmid``.Please clarify that <offset, len> must correspond to the entirety of a valid mapped region.
By the way, the VIRTIO 1.3 gives the following behavior for the
virtiofs
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
DAX Window:
When a
FUSE_SETUPMAPPINGrequest perfectly overlaps a previous mapping, the previous mapping is replaced. When a mapping partially overlaps a previous mapping, the previous mapping is split into oneor
Quoted 1 line
Quoted 1 line
Quoted 1 line
two smaller mappings. When a mapping is partially unmapped it is
also
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
split into one or two smaller mappings.
Establishing new mappings or splitting existing mappings consumes resources. If the device runs out of resources the
FUSE_SETUPMAPPINGrequest fails until resources are available again followingFUSE_REMOVEMAPPING.Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
I think SETUPMAPPING/REMOVMAPPING can be implemented using SHMEM_MAP/UNMAP. SHMEM_MAP/UNMAP do not allow atomically replacing partial ranges, but as far as I know that's not necessary for virtiofs in practice.
It's worth mentioning that mappings consume resources and that
SHMEM_MAPQuoted 60 lines
Quoted 60 lines
Quoted 60 lines
can fail when there are no resources available. The process-wide limit is vm.max_map_count on Linux although a vhost-user frontend may reduce it further to control vhost-user resource usage.
hw/virtio/vhost-user.c
Quoted 52 lines
docs/interop/vhost-user.rst +2
-0+ A reply is generated indicating whether unmapping succeeded. + .. _reply_ack: VHOST_USER_PROTOCOL_F_REPLY_ACKhw/virtio/vhost-user.c +21
-0@@ -115,6 +115,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;@@ -192,6 +194,24 @@ typedef struct VhostUserShared {unsigned char uuid[16 ]; } VhostUserShared; +/* For the flags field of VhostUserMMap */ +#define VHOST_USER_FLAG_MAP_R (1u << 0) +#define VHOST_USER_FLAG_MAP_W (1u << 1) + +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_* */ +uint64_t flags; +}VhostUserMMap ; + typedef struct { VhostUserRequest request;@@ -224,6 +244,7 @@ typedef union {VhostUserInflight inflight;VhostUserShared object;VhostUserTransferDeviceState transfer_state; +VhostUserMMap mmap; } VhostUserPayload; typedef struct VhostUserMsg {@@ -1748,6 +1769,100 @@vhost_user_backend_handle_shared_object_lookup(struct
vhost_user*u,Quoted 3 lines
hw/virtio/vhost-user.c +0
-0return 0 ; }Quoted 16 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
hw/virtio/vhost-user.c +8
-0+static int +vhost_user_backend_handle_shmem_map (struct vhost_dev *dev, +VhostUserMMap *vu_mmap, +int fd) +{ +void *addr =0 ; +MemoryRegion *mr =NULL ; +Quoted 8 lines
Quoted 8 lines
hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_lookup(struct vhost_user *u,
Quoted 8 lines
hw/virtio/vhost-user.c +8
-0+if (fd <0 ) { +error_report ("Bad fd for map" ); +return -EBADF ; + } + +if (!dev->vdev->shmem_list || + dev->vdev->n_shmem_regions <= vu_mmap->shmid) { + error_report("Device only has %d VIRTIO Shared MemoryRegions.
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
"
Quoted 6 lines
hw/virtio/vhost-user.c +6
-0+"Requested ID: %d" , + dev->vdev->n_shmem_regions, vu_mmap->shmid); +return -EFAULT ; + } + + mr = &dev->vdev->shmem_list[vu_mmap->shmid];hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_lookup(struct vhost_user *u,
Quoted 10 lines
hw/virtio/vhost-user.c +10
-0+ +if (!mr) { +error_report ("VIRTIO Shared Memory Region at " +"ID %d unitialized" , vu_mmap->shmid); +return -EFAULT ; + } + +if ((vu_mmap->shm_offset + vu_mmap->len) < vu_mmap->len || + (vu_mmap->shm_offset + vu_mmap->len) > mr->size) { + error_report("Bad offset/len for mmap %" PRIx64 "+%"PRIx64,
Quoted 18 lines
Quoted 12 lines
Quoted 11 lines
hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_lookup(struct vhost_user *u, · line 1810
Quoted 7 lines
hw/virtio/vhost-user.c +7
-0+ vu_mmap->shm_offset, vu_mmap->len); +return -EFAULT ; + } + +void *shmem_ptr =memory_region_get_ram_ptr (mr); + + addr = mmap(shmem_ptr + vu_mmap->shm_offset, vu_mmap->len,Missing check for overlap between range [
shm_offset,shm_offset+ len) and existing mappings.Quoted 5 lines
Not sure how to do this check. Specifically, I am not sure how previous ranges are stored within the MemoryRegion. Is looping through
mr->subregions
a valid option?Quoted 4 lines
Maybe something like this would do?
```
if (memory_region_find(mr, vu_mmap->shm_offset, vu_mmap->len).mr) {
error_report("Requested memory (%" PRIx64 "+%" PRIx64 " overalps"
Quoted 5 lines
"with previously mapped memory", vu_mmap->shm_offset, vu_mmap->len); return -EFAULT; }```
I don't think that works because the QEMU MemoryRegion covers the entire range, some of which contains mappings and some of which is empty. It would be necessary to track mappings that have been made.
Quoted 9 lines
I'm not aware of a security implication if the overlap check is missing, so I guess it may be okay to skip it and rely on the vhost-user back-end author to honor the spec. I'm not totally against that because it's faster and less code, but it feels a bit iffy to not enforce the input validation that the spec requires.
Maintain a list of mappings so this check can be performed?
Ok, I prefer to aim for the better solution and see where that takes us. So I will add a mapped_regions list or something like that to the MemoryRegion struct in a new commit, so that it can be reviewed independently. With the infrastructure's code in the patch we can decide if it is worth to have it.
Thank you!
Quoted 344 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
hw/virtio/vhost-user.c line 1811
Quoted 5 lines
hw/virtio/vhost-user.c +1
-0+ ((vu_mmap->flags &VHOST_USER_FLAG_MAP_R ) ?PROT_READ :0 )|
Quoted 1 line
Quoted 1 line
Quoted 1 line
hw/virtio/vhost-user.c line 1812
Quoted 1 line
hw/virtio/vhost-user.c +1
-0+ ((vu_mmap->flags & VHOST_USER_FLAG_MAP_W) ? PROT_WRITE :0),
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
hw/virtio/vhost-user.c +12
-0+ MAP_SHARED | MAP_FIXED, fd, vu_mmap->fd_offset); + +if (addr ==MAP_FAILED ) { +error_report ("Failed to mmap mem fd" ); +return -EFAULT ; + } + +return 0 ; +} + +static int +vhost_user_backend_handle_shmem_unmap(struct vhost_dev *dev,Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_lookup(struct vhost_user *u,
Quoted 8 lines
hw/virtio/vhost-user.c +8
-0+ VhostUserMMap *vu_mmap) +{ +void *addr =0 ; +MemoryRegion *mr =NULL ; + +if (!dev->vdev->shmem_list || + dev->vdev->n_shmem_regions <= vu_mmap->shmid) { + error_report("Device only has %d VIRTIO Shared MemoryRegions.
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
"
Quoted 6 lines
hw/virtio/vhost-user.c +6
-0+"Requested ID: %d" , + dev->vdev->n_shmem_regions, vu_mmap->shmid); +return -EFAULT ; + } + + mr = &dev->vdev->shmem_list[vu_mmap->shmid];hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_lookup(struct vhost_user *u,
Quoted 10 lines
hw/virtio/vhost-user.c +10
-0+ +if (!mr) { +error_report ("VIRTIO Shared Memory Region at " +"ID %d unitialized" , vu_mmap->shmid); +return -EFAULT ; + } + +if ((vu_mmap->shm_offset + vu_mmap->len) < vu_mmap->len || + (vu_mmap->shm_offset + vu_mmap->len) > mr->size) { + error_report("Bad offset/len for mmap %" PRIx64 "+%"PRIx64,
Quoted 27 lines
Quoted 27 lines
Quoted 27 lines
hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_lookup(struct vhost_user *u, · line 1810
Quoted 7 lines
hw/virtio/vhost-user.c +7
-0+ vu_mmap->shm_offset, vu_mmap->len); +return -EFAULT ; + } + +void *shmem_ptr =memory_region_get_ram_ptr (mr); + + addr = mmap(shmem_ptr + vu_mmap->shm_offset, vu_mmap->len,Missing check for existing mapping with exact range [
shm_offset, len) match.hw/virtio/vhost-user.c line 1856
Quoted 1 line
hw/virtio/vhost-user.c +1
-0+ PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,-1, 0);
hw/virtio/vhost-user.c static gboolean backend_read(QIOChannel *ioc,
Quoted 13 lines
hw/virtio/vhost-user.c +9
-0+ +if (addr ==MAP_FAILED ) { +error_report ("Failed to unmap memory" ); +return -EFAULT ; + } + +return 0 ; +} +static void close_backend_channel (struct vhost_user *u) {g_source_destroy (u->backend_src);@@ -1816,6 +1931,13 @@ static gboolean backend_read(QIOChannel *ioc,GIOCondition condition,
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
hw/virtio/vhost-user.c static gboolean backend_read(QIOChannel *ioc, · line 1931
Quoted 1 line
hw/virtio/vhost-user.c +0
-0ret =vhost_user_backend_handle_shared_object_lookup(dev->opaque, ioc,
hw/virtio/vhost-user.c static gboolean backend_read(QIOChannel *ioc, · line 1932
Quoted 1 line
hw/virtio/vhost-user.c +0
-0&hdr,&payload);
Quoted 3 lines
hw/virtio/vhost-user.c +2
-0break ; +case VHOST_USER_BACKEND_SHMEM_MAP : + ret = vhost_user_backend_handle_shmem_map(dev,&payload.mmap,
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
hw/virtio/vhost-user.c static gboolean backend_read(QIOChannel *ioc, GIOCondition condition,
Quoted 4 lines
hw/virtio/vhost-user.c +4
-0+ fd ? fd[0] : -1); +break ; +case VHOST_USER_BACKEND_SHMEM_UNMAP : + ret = vhost_user_backend_handle_shmem_unmap(dev,&payload.mmap);
Quoted 3 lines
hw/virtio/vhost-user.c +1
-0+break ;default : error_report("Received unexpected msg type: %d.",hdr.request);
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
hw/virtio/virtio.c int virtio_save(VirtIODevice *vdev, QEMUFile
Quoted 6 lines
hw/virtio/vhost-user.c +0
-0ret = -EINVAL ;hw/virtio/virtio.c +0
-0@@ -2856,6 +2856,16 @@ int virtio_save(VirtIODevice *vdev, QEMUFile*f)
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
hw/virtio/virtio.c int virtio_save(VirtIODevice *vdev, QEMUFile *f) · line 2864
Quoted 9 lines
hw/virtio/virtio.c +6
-0return vmstate_save_state (f, &vmstate_virtio, vdev,NULL ); } +MemoryRegion *virtio_new_shmem_region (VirtIODevice *vdev) +{ +MemoryRegion *mr =g_new0 (MemoryRegion,1 ); + ++vdev->n_shmem_regions; + vdev->shmem_list =g_renew (MemoryRegion, vdev->shmem_list, + vdev->n_shmem_regions);Where is
shmem_listfreed?Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
The name "list" is misleading since this is an array, not a list.
hw/virtio/virtio.c int virtio_save(VirtIODevice *vdev, QEMUFile *f) · line 2867
Quoted 3 lines
hw/virtio/virtio.c +3
-0+ vdev->shmem_list[vdev->n_shmem_regions -1 ] = *mr; +return mr; +}This looks weird. The contents of mr are copied into shmem_list[] and then the pointer to mr is returned? Did you mean for the field's type
to
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
be MemoryRegion **shmem_list and then vdev->shmem_list[...] = mr would stash the pointer?
Quoted 3 lines
hw/virtio/virtio.c +1
-0+/* A wrapper for use as a VMState .put function */ static int virtio_device_put(QEMUFile *f, void *opaque, size_tsize,
Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
hw/virtio/virtio.c int virtio_save(VirtIODevice *vdev, QEMUFile · line 2871
Quoted 1 line
hw/virtio/virtio.c +0
-0const VMStateField *field, JSONWriter*vmdesc)
hw/virtio/virtio.c void virtio_init(VirtIODevice *vdev, uint16_t
Quoted 1 line
hw/virtio/virtio.c +0
-0@@ -3264,6 +3274,8 @@ void virtio_init(VirtIODevice *vdev, uint16_t
device_id,size_tconfig_size)include/hw/virtio/virtio.h void virtio_notify(VirtIODevice *vdev, VirtQueue
Quoted 23 lines
hw/virtio/virtio.c +2
-0virtio_vmstate_change, vdev); vdev->device_endian =virtio_default_endian (); vdev->use_guest_notifier_mask = true; + vdev->shmem_list =NULL ; + vdev->n_shmem_regions =0 ; }/* include/hw/virtio/virtio.h +3
-0@@ -165,6 +165,9 @@ struct VirtIODevice*/ EventNotifier config_notifier;bool device_iotlb_enabled; +/* Shared memory region for vhost-user mappings. */ +MemoryRegion *shmem_list; +int n_shmem_regions; };struct VirtioDeviceClass {@@ -280,6 +283,8 @@ void virtio_notify(VirtIODevice *vdev, VirtQueue*vq);
Review
Quoted 9 lines
include/hw/virtio/virtio.h +2
-0int virtio_save (VirtIODevice *vdev,QEMUFile *f); +MemoryRegion *virtio_new_shmem_region (VirtIODevice *vdev); +extern const VMStateInfo virtio_vmstate_info;#define VMSTATE_VIRTIO_DEVICE \(quoted diff) +0
-0
b/subprojects/libvhost-user/libvhost-user.csubprojects/libvhost-user/libvhost-user.c vu_rm_shared_object(VuDev *dev, unsigned char
Quoted 4 lines
index
a879149fef..28556d183a100644subprojects/libvhost-user/libvhost-user.c +0
-0@@ -1586,6 +1586,71 @@ vu_rm_shared_object(VuDev *dev, unsigned charuuid[UUID_LEN])
Quoted 15 lines
subprojects/libvhost-user/libvhost-user.c +12
-0return 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) +{ +bool result = false; +VhostUserMsg msg_reply; +VhostUserMsg vmsg = { + .request =VHOST_USER_BACKEND_SHMEM_MAP , + .size =sizeof (vmsg.payload.mmap), + .flags =VHOST_USER_VERSION , + .payload.mmap = { + .shmid = shmid,Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
subprojects/libvhost-user/libvhost-user.c vu_rm_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN])
Quoted 8 lines
subprojects/libvhost-user/libvhost-user.c +8
-0+ .fd_offset = fd_offset, + .shm_offset = shm_offset, + .len = len, + .flags = flags, + }, + }; + + if (vu_has_protocol_feature(dev,
VHOST_USER_PROTOCOL_F_REPLY_ACK))Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
{
Quoted 16 lines
subprojects/libvhost-user/libvhost-user.c +16
-0+ 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 fd_offset, +uint64_t shm_offset,uint64_t len)subprojects/libvhost-user/libvhost-user.c vu_rm_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN]) · line 1634
Quoted 10 lines
subprojects/libvhost-user/libvhost-user.c +10
-0+{ +bool result = false; +VhostUserMsg msg_reply; +VhostUserMsg vmsg = { + .request =VHOST_USER_BACKEND_SHMEM_UNMAP , + .size =sizeof (vmsg.payload.mmap), + .flags =VHOST_USER_VERSION , + .payload.mmap = { + .shmid = shmid, + .fd_offset = fd_offset,What is the meaning of this field? I expected it to be set to 0.
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
subprojects/libvhost-user/libvhost-user.c vu_rm_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN])
Quoted 6 lines
subprojects/libvhost-user/libvhost-user.c +6
-0+ .shm_offset = shm_offset, + .len = len, + }, + }; + + if (vu_has_protocol_feature(dev,
VHOST_USER_PROTOCOL_F_REPLY_ACK))Quoted 69 lines
Quoted 69 lines
Quoted 69 lines
{
Review
Quoted 17 lines
subprojects/libvhost-user/libvhost-user.c +13
-0+ 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) {(quoted diff) +0
-0
b/subprojects/libvhost-user/libvhost-user.hsubprojects/libvhost-user/libvhost-user.h bool vu_add_shared_object(VuDev *dev, unsigned
Quoted 46 lines
index
deb40e77b3..7f6c22cc1a100644subprojects/libvhost-user/libvhost-user.h +21
-0@@ -127,6 +127,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 +188,24 @@ typedef struct VhostUserShared {unsigned char uuid[UUID_LEN ]; } VhostUserShared; +/* For the flags field of VhostUserMMap */ +#define VHOST_USER_FLAG_MAP_R (1u << 0) +#define VHOST_USER_FLAG_MAP_W (1u << 1) + +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_* */ +uint64_t flags; +}VhostUserMMap ; +#if defined(_WIN32) && (defined(__x86_64__) || defined(__i386__))# define VU_PACKED __attribute__((gcc_struct, packed))#else @@ -214,6 +234,7 @@ typedef struct VhostUserMsg {VhostUserVringArea area;VhostUserInflight inflight;VhostUserShared object; +VhostUserMMap mmap; } payload;int fds[VHOST_MEMORY_BASELINE_NREGIONS ];@@ -597,6 +618,38 @@ bool vu_add_shared_object(VuDev *dev, unsignedchar uuid[UUID_LEN]);
Quoted 3 lines
subprojects/libvhost-user/libvhost-user.h +0
-0*/ bool vu_rm_shared_object (VuDev *dev,unsigned char uuid[UUID_LEN ]);Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
subprojects/libvhost-user/libvhost-user.h bool vu_add_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN]);
Quoted 10 lines
subprojects/libvhost-user/libvhost-user.h +10
-0+/** +* 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 +* +* Advertises a new mapping to be made in a given VIRTIO Shared Memory
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Region.
Quoted 5 lines
subprojects/libvhost-user/libvhost-user.h +5
-0+* +* 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_tflags);
Quoted 26 lines
Quoted 26 lines
Quoted 25 lines
subprojects/libvhost-user/libvhost-user.h bool vu_add_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN]);
Quoted 10 lines
subprojects/libvhost-user/libvhost-user.h +10
-0+ +/** +* 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 +* +* The front-end un-mmaps a given range in the VIRTIO Shared Memory Region
Quoted 13 lines
subprojects/libvhost-user/libvhost-user.h +7
-1+* with the requested `shmid`. +* +* Returns: TRUE on success, FALSE on failure. +*/ +bool vu_shmem_unmap (VuDev *dev,uint8_t shmid,uint64_t fd_offset, +uint64_t shm_offset,uint64_t len); +/** * vu_queue_set_notification: * @dev: a VuDev context -- 2.45.2
Re: [RFC PATCH v2 1/5] vhost-user: Add VIRTIO Shared Memory map request
Quoted 242 lines · Albert Esteve
Quoted 241 lines · Stefan Hajnoczi
Quoted 220 lines · Albert Esteve
Quoted 211 lines · Albert Esteve
Quoted 196 lines · Stefan Hajnoczi
Quoted 36 lines · Albert Esteve
Add SHMEM_MAP/UNMAP requests to vhost-user to handle VIRTIO Shared Memory mappings.
This request allows backends to dynamically map fds into a VIRTIO Shared Memory Region indentified by its
shmid. Then, the fd memory is advertised to the driver as a base addres + offset, so it can be read/written (depending on the mmap flags requested) while its valid.The backend can munmap the memory range in a given VIRTIO Shared Memory Region (again, identified by its
shmid), to free it. Upon receiving this message, the front-end must mmap the regions withPROT_NONEto reserve the virtual memory space.The device model needs to create MemoryRegion instances for the VIRTIO Shared Memory Regions and add them to the
VirtIODeviceinstance.Signed-off-by Albert Esteve <[email protected]>--- docs/interop/vhost-user.rst | 27 +++++ hw/virtio/vhost-user.c | 122 ++++++++++++++++++++++ hw/virtio/virtio.c | 12 +++ include/hw/virtio/virtio.h | 5 + subprojects/libvhost-user/libvhost-user.c | 65 ++++++++++++ subprojects/libvhost-user/libvhost-user.h | 53 ++++++++++ 6 files changed, 284 insertions(+)docs/interop/vhost-user.rst +0
-0docs/interop/vhost-user.rst is sent by the front-end. · line 1859
Quoted 1 line
docs/interop/vhost-user.rst +0
-0@@ -1859,6 +1859,33 @@ is sent by the front-end. when the operation is successful, or non-zero otherwise. Note thatif the
docs/interop/vhost-user.rst is sent by the front-end.
Quoted 9 lines
docs/interop/vhost-user.rst +7
-0operation 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 + + This message can be submitted by the backends to advertise a newmapping
docs/interop/vhost-user.rst is sent by the front-end. · line 1869
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ to be made in a given VIRTIO Shared Memory Region. Upon receivingthe message,
docs/interop/vhost-user.rst is sent by the front-end. · line 1870
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ The front-end will mmap the given fd into the VIRTIO Shared MemoryRegion
docs/interop/vhost-user.rst is sent by the front-end. · line 1871
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ with the requested ``shmid``. A reply is generated indicatingwhether mapping
docs/interop/vhost-user.rst is sent by the front-end.
Quoted 3 lines
docs/interop/vhost-user.rst +3
-0+ succeeded. + + Mapping over an already existing map is not allowed and requestshall fail.
docs/interop/vhost-user.rst is sent by the front-end. · line 1875
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ Therefore, the memory range in the request must correspond with avalid,
docs/interop/vhost-user.rst is sent by the front-end.
Quoted 9 lines
docs/interop/vhost-user.rst +9
-0+ free region of the VIRTIO Shared Memory Region. + +``VHOST_USER_BACKEND_SHMEM_UNMAP`` + :id: 10 + :equivalent ioctl: N/A + :request payload: ``struct VhostUserMMap`` + :reply payload: N/A + + This message can be submitted by the backends so that the front-endun-mmap
docs/interop/vhost-user.rst is sent by the front-end. · line 1885
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ a given range (``offset``, ``len``) in the VIRTIO Shared MemoryRegion with
s/offset/shm_offset/
docs/interop/vhost-user.rst is sent by the front-end. · line 1886
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ the requested ``shmid``.Please clarify that <offset, len> must correspond to the entirety of a valid mapped region.
By the way, the VIRTIO 1.3 gives the following behavior for the virtiofs DAX Window:
When a
FUSE_SETUPMAPPINGrequest perfectly overlaps a previous mapping, the previous mapping is replaced. When a mapping partially overlaps a previous mapping, the previous mapping is split into one or two smaller mappings. When a mapping is partially unmapped it is also split into one or two smaller mappings.Establishing new mappings or splitting existing mappings consumes resources. If the device runs out of resources the
FUSE_SETUPMAPPINGrequest fails until resources are available again followingFUSE_REMOVEMAPPING.I think SETUPMAPPING/REMOVMAPPING can be implemented using SHMEM_MAP/UNMAP. SHMEM_MAP/UNMAP do not allow atomically replacing partial ranges, but as far as I know that's not necessary for virtiofs in practice.
It's worth mentioning that mappings consume resources and that
SHMEM_MAPcan fail when there are no resources available. The process-wide limit is vm.max_map_count on Linux although a vhost-user frontend may reduce it further to control vhost-user resource usage.hw/virtio/vhost-user.c
Quoted 52 lines
docs/interop/vhost-user.rst +2
-0+ A reply is generated indicating whether unmapping succeeded. + .. _reply_ack: VHOST_USER_PROTOCOL_F_REPLY_ACKhw/virtio/vhost-user.c +21
-0@@ -115,6 +115,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;@@ -192,6 +194,24 @@ typedef struct VhostUserShared {unsigned char uuid[16 ]; } VhostUserShared; +/* For the flags field of VhostUserMMap */ +#define VHOST_USER_FLAG_MAP_R (1u << 0) +#define VHOST_USER_FLAG_MAP_W (1u << 1) + +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_* */ +uint64_t flags; +}VhostUserMMap ; + typedef struct { VhostUserRequest request;@@ -224,6 +244,7 @@ typedef union {VhostUserInflight inflight;VhostUserShared object;VhostUserTransferDeviceState transfer_state; +VhostUserMMap mmap; } VhostUserPayload; typedef struct VhostUserMsg {@@ -1748,6 +1769,100 @@vhost_user_backend_handle_shared_object_lookup(struct
vhost_user*u,Quoted 9 lines
hw/virtio/vhost-user.c +6
-0return 0 ; } +static int +vhost_user_backend_handle_shmem_map (struct vhost_dev *dev, +VhostUserMMap *vu_mmap, +int fd) +{ +void *addr =0 ;hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_lookup(struct vhost_user *u,
Quoted 10 lines
hw/virtio/vhost-user.c +10
-0+MemoryRegion *mr =NULL ; + +if (fd <0 ) { +error_report ("Bad fd for map" ); +return -EBADF ; + } + +if (!dev->vdev->shmem_list || + dev->vdev->n_shmem_regions <= vu_mmap->shmid) { + error_report("Device only has %d VIRTIO Shared Memory Regions."
Quoted 18 lines
hw/virtio/vhost-user.c +18
-0+"Requested ID: %d" , + dev->vdev->n_shmem_regions, vu_mmap->shmid); +return -EFAULT ; + } + + mr = &dev->vdev->shmem_list[vu_mmap->shmid]; + +if (!mr) { +error_report ("VIRTIO Shared Memory Region at " +"ID %d unitialized" , vu_mmap->shmid); +return -EFAULT ; + } + +if ((vu_mmap->shm_offset + vu_mmap->len) < vu_mmap->len || + (vu_mmap->shm_offset + vu_mmap->len) > mr->size) { +error_report ("Bad offset/len for mmap %" PRIx64"+%" PRIx64, + vu_mmap->shm_offset, vu_mmap->len); +return -EFAULT ;Quoted 9 lines
hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_lookup(struct vhost_user *u, · line 1810
Quoted 5 lines
hw/virtio/vhost-user.c +5
-0+ } + +void *shmem_ptr =memory_region_get_ram_ptr (mr); + + addr = mmap(shmem_ptr + vu_mmap->shm_offset, vu_mmap->len,Missing check for overlap between range [
shm_offset,shm_offset+ len) and existing mappings.Quoted 5 lines
Not sure how to do this check. Specifically, I am not sure how previous ranges are stored within the MemoryRegion. Is looping through
mr->subregions
a valid option?Maybe something like this would do?
Quoted 8 lines
``` if (memory_region_find(mr, vu_mmap->shm_offset, vu_mmap->len).mr) { error_report("Requested memory (%" PRIx64 "+%" PRIx64 " overalps " "with previously mapped memory", vu_mmap->shm_offset, vu_mmap->len); return -EFAULT; } ```I don't think that works because the QEMU MemoryRegion covers the entire range, some of which contains mappings and some of which is empty. It would be necessary to track mappings that have been made.
I'm not aware of a security implication if the overlap check is missing, so I guess it may be okay to skip it and rely on the vhost-user back-end author to honor the spec. I'm not totally against that because it's faster and less code, but it feels a bit iffy to not enforce the input validation that the spec requires.
Quoted 8 lines
Quoted 2 lines
Maintain a list of mappings so this check can be performed?
Ok, I prefer to aim for the better solution and see where that takes us. So I will add a
mapped_regionslist or something like that to the MemoryRegion struct in a new commit, so that it can be reviewed independently. With the infrastructure's code in the patch we can decide if it is worth to have it.
Great. MemoryRegion is a core struct that's not related to vhost-user. I don't think anything else needs a mappings list. Maybe add the mappings list to shmem_list[] elements instead so that each VIRTIO Shared Memory Region has a mappings list? Each element could be a struct with a MemoryRegion field and a mappings list.
Stefan
Quoted 334 lines
Thank you!
Quoted 331 lines
Quoted 331 lines
Quoted 331 lines
Quoted 330 lines
Quoted 17 lines
hw/virtio/vhost-user.c +12
-0+ ((vu_mmap->flags &VHOST_USER_FLAG_MAP_R ) ?PROT_READ :0 ) | + ((vu_mmap->flags &VHOST_USER_FLAG_MAP_W ) ?PROT_WRITE :0 ), + MAP_SHARED | MAP_FIXED, fd, vu_mmap->fd_offset); + +if (addr ==MAP_FAILED ) { +error_report ("Failed to mmap mem fd" ); +return -EFAULT ; + } + +return 0 ; +} +hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_lookup(struct vhost_user *u,
Quoted 10 lines
hw/virtio/vhost-user.c +10
-0+static int +vhost_user_backend_handle_shmem_unmap (struct vhost_dev *dev, +VhostUserMMap *vu_mmap) +{ +void *addr =0 ; +MemoryRegion *mr =NULL ; + +if (!dev->vdev->shmem_list || + dev->vdev->n_shmem_regions <= vu_mmap->shmid) { + error_report("Device only has %d VIRTIO Shared Memory Regions."
Quoted 13 lines
hw/virtio/vhost-user.c +13
-0+"Requested ID: %d" , + dev->vdev->n_shmem_regions, vu_mmap->shmid); +return -EFAULT ; + } + + mr = &dev->vdev->shmem_list[vu_mmap->shmid]; + +if (!mr) { +error_report ("VIRTIO Shared Memory Region at " +"ID %d unitialized" , vu_mmap->shmid); +return -EFAULT ; + } +hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_lookup(struct vhost_user *u, · line 1810
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) > mr->size) { +error_report ("Bad offset/len for mmap %" PRIx64"+%" PRIx64, + vu_mmap->shm_offset, vu_mmap->len); +return -EFAULT ; + } + +void *shmem_ptr =memory_region_get_ram_ptr (mr); + + addr = mmap(shmem_ptr + vu_mmap->shm_offset, vu_mmap->len,Missing check for existing mapping with exact range [
shm_offset, len) match.hw/virtio/vhost-user.c line 1856
Quoted 1 line
hw/virtio/vhost-user.c +1
-0+ PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,-1, 0);
hw/virtio/vhost-user.c static gboolean backend_read(QIOChannel *ioc,
Quoted 13 lines
hw/virtio/vhost-user.c +9
-0+ +if (addr ==MAP_FAILED ) { +error_report ("Failed to unmap memory" ); +return -EFAULT ; + } + +return 0 ; +} +static void close_backend_channel (struct vhost_user *u) {g_source_destroy (u->backend_src);@@ -1816,6 +1931,13 @@ static gboolean backend_read(QIOChannel *ioc,GIOCondition condition,
hw/virtio/vhost-user.c static gboolean backend_read(QIOChannel *ioc, · line 1931
Quoted 1 line
hw/virtio/vhost-user.c +0
-0ret =vhost_user_backend_handle_shared_object_lookup(dev->opaque, ioc,
hw/virtio/vhost-user.c static gboolean backend_read(QIOChannel *ioc, · line 1932
Quoted 1 line
hw/virtio/vhost-user.c +0
-0&hdr,&payload);
hw/virtio/vhost-user.c static gboolean backend_read(QIOChannel *ioc, GIOCondition condition,
Quoted 7 lines
hw/virtio/vhost-user.c +6
-0break ; +case VHOST_USER_BACKEND_SHMEM_MAP : + ret =vhost_user_backend_handle_shmem_map (dev, &payload.mmap, + fd ? fd[0 ] :-1 ); +break ; +case VHOST_USER_BACKEND_SHMEM_UNMAP : + ret = vhost_user_backend_handle_shmem_unmap(dev,&payload.mmap);
Quoted 9 lines
hw/virtio/vhost-user.c +1
-0+break ;default :error_report ("Received unexpected msg type: %d." , hdr.request); ret = -EINVAL ;hw/virtio/virtio.c +0
-0hw/virtio/virtio.c int virtio_save(VirtIODevice *vdev, QEMUFile *f) · line 2864
Quoted 9 lines
hw/virtio/virtio.c +6
-0@@ -2856,6 +2856,16 @@ int virtio_save(VirtIODevice *vdev, QEMUFile *f)return vmstate_save_state (f, &vmstate_virtio, vdev,NULL ); } +MemoryRegion *virtio_new_shmem_region (VirtIODevice *vdev) +{ +MemoryRegion *mr =g_new0 (MemoryRegion,1 ); + ++vdev->n_shmem_regions; + vdev->shmem_list =g_renew (MemoryRegion, vdev->shmem_list, + vdev->n_shmem_regions);Where is
shmem_listfreed?The name "list" is misleading since this is an array, not a list.
hw/virtio/virtio.c int virtio_save(VirtIODevice *vdev, QEMUFile *f) · line 2867
Quoted 3 lines
hw/virtio/virtio.c +3
-0+ vdev->shmem_list[vdev->n_shmem_regions -1 ] = *mr; +return mr; +}This looks weird. The contents of mr are copied into shmem_list[] and then the pointer to mr is returned? Did you mean for the field's type to be MemoryRegion **shmem_list and then vdev->shmem_list[...] = mr would stash the pointer?
hw/virtio/virtio.c int virtio_save(VirtIODevice *vdev, QEMUFile *f)
Quoted 4 lines
hw/virtio/virtio.c +1
-0+/* 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)
hw/virtio/virtio.c void virtio_init(VirtIODevice *vdev, uint16_t
Quoted 1 line
hw/virtio/virtio.c +0
-0@@ -3264,6 +3274,8 @@ void virtio_init(VirtIODevice *vdev, uint16_t
device_id,size_tconfig_size)include/hw/virtio/virtio.h void virtio_notify(VirtIODevice *vdev, VirtQueue
Quoted 23 lines
hw/virtio/virtio.c +2
-0virtio_vmstate_change, vdev); vdev->device_endian =virtio_default_endian (); vdev->use_guest_notifier_mask = true; + vdev->shmem_list =NULL ; + vdev->n_shmem_regions =0 ; }/* include/hw/virtio/virtio.h +3
-0@@ -165,6 +165,9 @@ struct VirtIODevice*/ EventNotifier config_notifier;bool device_iotlb_enabled; +/* Shared memory region for vhost-user mappings. */ +MemoryRegion *shmem_list; +int n_shmem_regions; };struct VirtioDeviceClass {@@ -280,6 +283,8 @@ void virtio_notify(VirtIODevice *vdev, VirtQueue*vq);
Review
Quoted 9 lines
include/hw/virtio/virtio.h +2
-0int virtio_save (VirtIODevice *vdev,QEMUFile *f); +MemoryRegion *virtio_new_shmem_region (VirtIODevice *vdev); +extern const VMStateInfo virtio_vmstate_info;#define VMSTATE_VIRTIO_DEVICE \(quoted diff) +0
-0
b/subprojects/libvhost-user/libvhost-user.csubprojects/libvhost-user/libvhost-user.c vu_rm_shared_object(VuDev *dev, unsigned char
Quoted 4 lines
index
a879149fef..28556d183a100644subprojects/libvhost-user/libvhost-user.c +0
-0@@ -1586,6 +1586,71 @@ vu_rm_shared_object(VuDev *dev, unsigned charuuid[UUID_LEN])
Quoted 13 lines
subprojects/libvhost-user/libvhost-user.c +10
-0return 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) +{ +bool result = false; +VhostUserMsg msg_reply; +VhostUserMsg vmsg = { + .request =VHOST_USER_BACKEND_SHMEM_MAP , + .size =sizeof (vmsg.payload.mmap), + .flags =VHOST_USER_VERSION ,subprojects/libvhost-user/libvhost-user.c vu_rm_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN])
Quoted 10 lines
subprojects/libvhost-user/libvhost-user.c +10
-0+ .payload.mmap = { + .shmid = shmid, + .fd_offset = fd_offset, + .shm_offset = shm_offset, + .len = len, + .flags = flags, + }, + }; + +if (vu_has_protocol_feature (dev,VHOST_USER_PROTOCOL_F_REPLY_ACK )){
Quoted 16 lines
subprojects/libvhost-user/libvhost-user.c +16
-0+ 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 fd_offset, +uint64_t shm_offset,uint64_t len)subprojects/libvhost-user/libvhost-user.c vu_rm_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN]) · line 1634
Quoted 10 lines
subprojects/libvhost-user/libvhost-user.c +10
-0+{ +bool result = false; +VhostUserMsg msg_reply; +VhostUserMsg vmsg = { + .request =VHOST_USER_BACKEND_SHMEM_UNMAP , + .size =sizeof (vmsg.payload.mmap), + .flags =VHOST_USER_VERSION , + .payload.mmap = { + .shmid = shmid, + .fd_offset = fd_offset,What is the meaning of this field? I expected it to be set to 0.
subprojects/libvhost-user/libvhost-user.c vu_rm_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN])
Quoted 6 lines
subprojects/libvhost-user/libvhost-user.c +6
-0+ .shm_offset = shm_offset, + .len = len, + }, + }; + +if (vu_has_protocol_feature (dev,VHOST_USER_PROTOCOL_F_REPLY_ACK )){
Review
Quoted 17 lines
subprojects/libvhost-user/libvhost-user.c +13
-0+ 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) {(quoted diff) +0
-0
b/subprojects/libvhost-user/libvhost-user.hsubprojects/libvhost-user/libvhost-user.h bool vu_add_shared_object(VuDev *dev, unsigned
Quoted 46 lines
index
deb40e77b3..7f6c22cc1a100644subprojects/libvhost-user/libvhost-user.h +21
-0@@ -127,6 +127,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 +188,24 @@ typedef struct VhostUserShared {unsigned char uuid[UUID_LEN ]; } VhostUserShared; +/* For the flags field of VhostUserMMap */ +#define VHOST_USER_FLAG_MAP_R (1u << 0) +#define VHOST_USER_FLAG_MAP_W (1u << 1) + +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_* */ +uint64_t flags; +}VhostUserMMap ; +#if defined(_WIN32) && (defined(__x86_64__) || defined(__i386__))# define VU_PACKED __attribute__((gcc_struct, packed))#else @@ -214,6 +234,7 @@ typedef struct VhostUserMsg {VhostUserVringArea area;VhostUserInflight inflight;VhostUserShared object; +VhostUserMMap mmap; } payload;int fds[VHOST_MEMORY_BASELINE_NREGIONS ];@@ -597,6 +618,38 @@ bool vu_add_shared_object(VuDev *dev, unsignedchar uuid[UUID_LEN]);
subprojects/libvhost-user/libvhost-user.h bool vu_add_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN]);
Quoted 13 lines
subprojects/libvhost-user/libvhost-user.h +10
-0*/ 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 +* +* Advertises a new mapping to be made in a given VIRTIO Shared Memory Region.
Quoted 5 lines
subprojects/libvhost-user/libvhost-user.h +5
-0+* +* 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);subprojects/libvhost-user/libvhost-user.h bool vu_add_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN]);
Quoted 10 lines
subprojects/libvhost-user/libvhost-user.h +10
-0+ +/** +* 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 +* +* The front-end un-mmaps a given range in the VIRTIO Shared Memory Region
Quoted 13 lines
subprojects/libvhost-user/libvhost-user.h +7
-1+* with the requested `shmid`. +* +* Returns: TRUE on success, FALSE on failure. +*/ +bool vu_shmem_unmap (VuDev *dev,uint8_t shmid,uint64_t fd_offset, +uint64_t shm_offset,uint64_t len); +/** * vu_queue_set_notification: * @dev: a VuDev context -- 2.45.2
Re: [RFC PATCH v2 1/5] vhost-user: Add VIRTIO Shared Memory map request
In reply to Stefan HajnocziQuoted 401 lines · Stefan Hajnoczi
Quoted 36 lines · Albert Esteve
Add SHMEM_MAP/UNMAP requests to vhost-user to handle VIRTIO Shared Memory mappings.
This request allows backends to dynamically map fds into a VIRTIO Shared Memory Region indentified by its
shmid. Then, the fd memory is advertised to the driver as a base addres + offset, so it can be read/written (depending on the mmap flags requested) while its valid.The backend can munmap the memory range in a given VIRTIO Shared Memory Region (again, identified by its
shmid), to free it. Upon receiving this message, the front-end must mmap the regions withPROT_NONEto reserve the virtual memory space.The device model needs to create MemoryRegion instances for the VIRTIO Shared Memory Regions and add them to the
VirtIODeviceinstance.Signed-off-by Albert Esteve <[email protected]>--- docs/interop/vhost-user.rst | 27 +++++ hw/virtio/vhost-user.c | 122 ++++++++++++++++++++++ hw/virtio/virtio.c | 12 +++ include/hw/virtio/virtio.h | 5 + subprojects/libvhost-user/libvhost-user.c | 65 ++++++++++++ subprojects/libvhost-user/libvhost-user.h | 53 ++++++++++ 6 files changed, 284 insertions(+)docs/interop/vhost-user.rst +0
-0docs/interop/vhost-user.rst is sent by the front-end. · line 1859
Quoted 1 line
docs/interop/vhost-user.rst +0
-0@@ -1859,6 +1859,33 @@ is sent by the front-end. when the operation is successful, or non-zero otherwise. Note that ifthe
docs/interop/vhost-user.rst is sent by the front-end.
Quoted 9 lines
docs/interop/vhost-user.rst +7
-0operation 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 + + This message can be submitted by the backends to advertise a newmapping
docs/interop/vhost-user.rst is sent by the front-end. · line 1869
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ to be made in a given VIRTIO Shared Memory Region. Upon receiving themessage,
docs/interop/vhost-user.rst is sent by the front-end. · line 1870
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ The front-end will mmap the given fd into the VIRTIO Shared MemoryRegion
docs/interop/vhost-user.rst is sent by the front-end. · line 1871
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ with the requested ``shmid``. A reply is generated indicating whethermapping
docs/interop/vhost-user.rst is sent by the front-end.
Quoted 3 lines
docs/interop/vhost-user.rst +3
-0+ succeeded. + + Mapping over an already existing map is not allowed and request shallfail.
docs/interop/vhost-user.rst is sent by the front-end. · line 1875
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ Therefore, the memory range in the request must correspond with avalid,
docs/interop/vhost-user.rst is sent by the front-end.
Quoted 9 lines
docs/interop/vhost-user.rst +9
-0+ free region of the VIRTIO Shared Memory Region. + +``VHOST_USER_BACKEND_SHMEM_UNMAP`` + :id: 10 + :equivalent ioctl: N/A + :request payload: ``struct VhostUserMMap`` + :reply payload: N/A + + This message can be submitted by the backends so that the front-endun-mmap
docs/interop/vhost-user.rst is sent by the front-end. · line 1885
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ a given range (``offset``, ``len``) in the VIRTIO Shared MemoryRegion with
s/offset/shm_offset/
docs/interop/vhost-user.rst is sent by the front-end. · line 1886
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ the requested ``shmid``.Please clarify that <offset, len> must correspond to the entirety of a valid mapped region.
By the way, the VIRTIO 1.3 gives the following behavior for the virtiofs DAX Window:
When a
FUSE_SETUPMAPPINGrequest perfectly overlaps a previous mapping, the previous mapping is replaced. When a mapping partially overlaps a previous mapping, the previous mapping is split into one or two smaller mappings. When a mapping is partially unmapped it is also split into one or two smaller mappings.Establishing new mappings or splitting existing mappings consumes resources. If the device runs out of resources the
FUSE_SETUPMAPPINGrequest fails until resources are available again followingFUSE_REMOVEMAPPING.I think SETUPMAPPING/REMOVMAPPING can be implemented using SHMEM_MAP/UNMAP. SHMEM_MAP/UNMAP do not allow atomically replacing partial ranges, but as far as I know that's not necessary for virtiofs in practice.
It's worth mentioning that mappings consume resources and that
SHMEM_MAPcan fail when there are no resources available. The process-wide limit is vm.max_map_count on Linux although a vhost-user frontend may reduce it further to control vhost-user resource usage.hw/virtio/vhost-user.c
Quoted 52 lines
docs/interop/vhost-user.rst +2
-0+ A reply is generated indicating whether unmapping succeeded. + .. _reply_ack: VHOST_USER_PROTOCOL_F_REPLY_ACKhw/virtio/vhost-user.c +21
-0@@ -115,6 +115,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;@@ -192,6 +194,24 @@ typedef struct VhostUserShared {unsigned char uuid[16 ]; } VhostUserShared; +/* For the flags field of VhostUserMMap */ +#define VHOST_USER_FLAG_MAP_R (1u << 0) +#define VHOST_USER_FLAG_MAP_W (1u << 1) + +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_* */ +uint64_t flags; +}VhostUserMMap ; + typedef struct { VhostUserRequest request;@@ -224,6 +244,7 @@ typedef union {VhostUserInflight inflight;VhostUserShared object;VhostUserTransferDeviceState transfer_state; +VhostUserMMap mmap; } VhostUserPayload; typedef struct VhostUserMsg {@@ -1748,6 +1769,100 @@vhost_user_backend_handle_shared_object_lookup(struct
vhost_user*u,Quoted 32 lines
hw/virtio/vhost-user.c +29
-0return 0 ; } +static int +vhost_user_backend_handle_shmem_map (struct vhost_dev *dev, +VhostUserMMap *vu_mmap, +int fd) +{ +void *addr =0 ; +MemoryRegion *mr =NULL ; + +if (fd <0 ) { +error_report ("Bad fd for map" ); +return -EBADF ; + } + +if (!dev->vdev->shmem_list || + dev->vdev->n_shmem_regions <= vu_mmap->shmid) { +error_report ("Device only has %d VIRTIO Shared Memory Regions. " +"Requested ID: %d" , + dev->vdev->n_shmem_regions, vu_mmap->shmid); +return -EFAULT ; + } + + mr = &dev->vdev->shmem_list[vu_mmap->shmid]; + +if (!mr) { +error_report ("VIRTIO Shared Memory Region at " +"ID %d unitialized" , vu_mmap->shmid); +return -EFAULT ; + } +hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_lookup(struct vhost_user *u, · line 1810
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) > mr->size) { +error_report ("Bad offset/len for mmap %" PRIx64"+%" PRIx64, + vu_mmap->shm_offset, vu_mmap->len); +return -EFAULT ; + } + +void *shmem_ptr =memory_region_get_ram_ptr (mr); + + addr = mmap(shmem_ptr + vu_mmap->shm_offset, vu_mmap->len,Missing check for overlap between range [
shm_offset,shm_offset+ len) and existing mappings.Quoted 35 lines
hw/virtio/vhost-user.c +35
-0+ ((vu_mmap->flags &VHOST_USER_FLAG_MAP_R ) ?PROT_READ :0 ) | + ((vu_mmap->flags &VHOST_USER_FLAG_MAP_W ) ?PROT_WRITE :0 ), + MAP_SHARED | MAP_FIXED, fd, vu_mmap->fd_offset); + +if (addr ==MAP_FAILED ) { +error_report ("Failed to mmap mem fd" ); +return -EFAULT ; + } + +return 0 ; +} + +static int +vhost_user_backend_handle_shmem_unmap (struct vhost_dev *dev, +VhostUserMMap *vu_mmap) +{ +void *addr =0 ; +MemoryRegion *mr =NULL ; + +if (!dev->vdev->shmem_list || + dev->vdev->n_shmem_regions <= vu_mmap->shmid) { +error_report ("Device only has %d VIRTIO Shared Memory Regions. " +"Requested ID: %d" , + dev->vdev->n_shmem_regions, vu_mmap->shmid); +return -EFAULT ; + } + + mr = &dev->vdev->shmem_list[vu_mmap->shmid]; + +if (!mr) { +error_report ("VIRTIO Shared Memory Region at " +"ID %d unitialized" , vu_mmap->shmid); +return -EFAULT ; + } +hw/virtio/vhost-user.c vhost_user_backend_handle_shared_object_lookup(struct vhost_user *u, · line 1855
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) > mr->size) { +error_report ("Bad offset/len for mmap %" PRIx64"+%" PRIx64, + vu_mmap->shm_offset, vu_mmap->len); +return -EFAULT ; + } + +void *shmem_ptr =memory_region_get_ram_ptr (mr); + + addr = mmap(shmem_ptr + vu_mmap->shm_offset, vu_mmap->len,Missing check for existing mapping with exact range [
shm_offset, len) match.hw/virtio/vhost-user.c line 1856
Quoted 1 line
hw/virtio/vhost-user.c +1
-0+ PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1,0);
hw/virtio/vhost-user.c static gboolean backend_read(QIOChannel *ioc,
Quoted 13 lines
hw/virtio/vhost-user.c +9
-0+ +if (addr ==MAP_FAILED ) { +error_report ("Failed to unmap memory" ); +return -EFAULT ; + } + +return 0 ; +} +static void close_backend_channel (struct vhost_user *u) {g_source_destroy (u->backend_src);@@ -1816,6 +1931,13 @@ static gboolean backend_read(QIOChannel *ioc,GIOCondition condition,
hw/virtio/vhost-user.c static gboolean backend_read(QIOChannel *ioc, · line 1931
Quoted 1 line
hw/virtio/vhost-user.c +0
-0ret =vhost_user_backend_handle_shared_object_lookup(dev->opaque, ioc,
hw/virtio/vhost-user.c static gboolean backend_read(QIOChannel *ioc, · line 1932
Quoted 1 line
hw/virtio/vhost-user.c +0
-0&hdr,&payload);
Quoted 16 lines
hw/virtio/vhost-user.c +7
-0break ; +case VHOST_USER_BACKEND_SHMEM_MAP : + ret =vhost_user_backend_handle_shmem_map (dev, &payload.mmap, + fd ? fd[0 ] :-1 ); +break ; +case VHOST_USER_BACKEND_SHMEM_UNMAP : + ret =vhost_user_backend_handle_shmem_unmap (dev, &payload.mmap); +break ;default :error_report ("Received unexpected msg type: %d." , hdr.request); ret = -EINVAL ;hw/virtio/virtio.c +0
-0hw/virtio/virtio.c int virtio_save(VirtIODevice *vdev, QEMUFile *f) · line 2864
Quoted 9 lines
hw/virtio/virtio.c +6
-0@@ -2856,6 +2856,16 @@ int virtio_save(VirtIODevice *vdev, QEMUFile *f)return vmstate_save_state (f, &vmstate_virtio, vdev,NULL ); } +MemoryRegion *virtio_new_shmem_region (VirtIODevice *vdev) +{ +MemoryRegion *mr =g_new0 (MemoryRegion,1 ); + ++vdev->n_shmem_regions; + vdev->shmem_list =g_renew (MemoryRegion, vdev->shmem_list, + vdev->n_shmem_regions);Where is
shmem_listfreed?The name "list" is misleading since this is an array, not a list.
hw/virtio/virtio.c int virtio_save(VirtIODevice *vdev, QEMUFile *f) · line 2867
Quoted 3 lines
hw/virtio/virtio.c +3
-0+ vdev->shmem_list[vdev->n_shmem_regions -1 ] = *mr; +return mr; +}This looks weird. The contents of mr are copied into shmem_list[] and then the pointer to mr is returned? Did you mean for the field's type to be MemoryRegion **shmem_list and then vdev->shmem_list[...] = mr would stash the pointer?
hw/virtio/virtio.c int virtio_save(VirtIODevice *vdev, QEMUFile *f)
Quoted 4 lines
hw/virtio/virtio.c +1
-0+/* 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)
hw/virtio/virtio.c void virtio_init(VirtIODevice *vdev, uint16_t
Quoted 1 line
hw/virtio/virtio.c +0
-0@@ -3264,6 +3274,8 @@ void virtio_init(VirtIODevice *vdev, uint16_t
device_id,size_tconfig_size)include/hw/virtio/virtio.h void virtio_notify(VirtIODevice *vdev, VirtQueue
Quoted 23 lines
hw/virtio/virtio.c +2
-0virtio_vmstate_change, vdev); vdev->device_endian =virtio_default_endian (); vdev->use_guest_notifier_mask = true; + vdev->shmem_list =NULL ; + vdev->n_shmem_regions =0 ; }/* include/hw/virtio/virtio.h +3
-0@@ -165,6 +165,9 @@ struct VirtIODevice*/ EventNotifier config_notifier;bool device_iotlb_enabled; +/* Shared memory region for vhost-user mappings. */ +MemoryRegion *shmem_list; +int n_shmem_regions; };struct VirtioDeviceClass {@@ -280,6 +283,8 @@ void virtio_notify(VirtIODevice *vdev, VirtQueue*vq);
Review
Quoted 9 lines
include/hw/virtio/virtio.h +2
-0int virtio_save (VirtIODevice *vdev,QEMUFile *f); +MemoryRegion *virtio_new_shmem_region (VirtIODevice *vdev); +extern const VMStateInfo virtio_vmstate_info;#define VMSTATE_VIRTIO_DEVICE \(quoted diff) +0
-0
b/subprojects/libvhost-user/libvhost-user.csubprojects/libvhost-user/libvhost-user.c vu_rm_shared_object(VuDev *dev, unsigned char
Quoted 4 lines
index
a879149fef..28556d183a100644subprojects/libvhost-user/libvhost-user.c +0
-0@@ -1586,6 +1586,71 @@ vu_rm_shared_object(VuDev *dev, unsigned charuuid[UUID_LEN])
Quoted 43 lines
subprojects/libvhost-user/libvhost-user.c +40
-0return 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) +{ +bool result = false; +VhostUserMsg msg_reply; +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, + }, + }; + +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 fd_offset, +uint64_t shm_offset,uint64_t len) +{ +bool result = false; +VhostUserMsg msg_reply; +VhostUserMsg vmsg = {
Quoted 9 lines
subprojects/libvhost-user/libvhost-user.c vu_rm_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN]) · line 1634
Quoted 6 lines
subprojects/libvhost-user/libvhost-user.c +6
-0+ .request = VHOST_USER_BACKEND_SHMEM_UNMAP, + .size = sizeof(vmsg.payload.mmap), + .flags = VHOST_USER_VERSION, + .payload.mmap = { + .shmid = shmid, + .fd_offset = fd_offset,What is the meaning of this field? I expected it to be set to 0.
Probably true. I just kept it generic for backends to decide what to set it to,
without considering the real use.
I will remove the parameter and set it to 0 in the request.
Quoted 116 lines
Review
Quoted 24 lines
subprojects/libvhost-user/libvhost-user.c +19
-0+ .shm_offset = shm_offset, + .len = len, + }, + }; + +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) {(quoted diff) +0
-0
b/subprojects/libvhost-user/libvhost-user.hsubprojects/libvhost-user/libvhost-user.h bool vu_add_shared_object(VuDev *dev, unsigned char
Quoted 46 lines
index
deb40e77b3..7f6c22cc1a100644subprojects/libvhost-user/libvhost-user.h +21
-0@@ -127,6 +127,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 +188,24 @@ typedef struct VhostUserShared {unsigned char uuid[UUID_LEN ]; } VhostUserShared; +/* For the flags field of VhostUserMMap */ +#define VHOST_USER_FLAG_MAP_R (1u << 0) +#define VHOST_USER_FLAG_MAP_W (1u << 1) + +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_* */ +uint64_t flags; +}VhostUserMMap ; +#if defined(_WIN32) && (defined(__x86_64__) || defined(__i386__))# define VU_PACKED __attribute__((gcc_struct, packed))#else @@ -214,6 +234,7 @@ typedef struct VhostUserMsg {VhostUserVringArea area;VhostUserInflight inflight;VhostUserShared object; +VhostUserMMap mmap; } payload;int fds[VHOST_MEMORY_BASELINE_NREGIONS ];@@ -597,6 +618,38 @@ bool vu_add_shared_object(VuDev *dev, unsigned charuuid[UUID_LEN]);
subprojects/libvhost-user/libvhost-user.h bool vu_add_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN]);
Quoted 13 lines
subprojects/libvhost-user/libvhost-user.h +10
-0*/ 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 +* +* Advertises a new mapping to be made in a given VIRTIO Shared Memory Region.
Quoted 5 lines
subprojects/libvhost-user/libvhost-user.h +5
-0+* +* 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);subprojects/libvhost-user/libvhost-user.h bool vu_add_shared_object(VuDev *dev, unsigned char uuid[UUID_LEN]);
Quoted 10 lines
subprojects/libvhost-user/libvhost-user.h +10
-0+ +/** +* 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 +* +* The front-end un-mmaps a given range in the VIRTIO Shared Memory Region
Quoted 13 lines
subprojects/libvhost-user/libvhost-user.h +7
-1+* with the requested `shmid`. +* +* Returns: TRUE on success, FALSE on failure. +*/ +bool vu_shmem_unmap (VuDev *dev,uint8_t shmid,uint64_t fd_offset, +uint64_t shm_offset,uint64_t len); +/** * vu_queue_set_notification: * @dev: a VuDev context -- 2.45.2
RFC PATCH v2 2/5 vhost_user: Add frontend command for shmem config
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.
--- docs/interop/vhost-user.rst | 31 +++++++++++++++++++++++ hw/virtio/vhost-user.c | 42 +++++++++++++++++++++++++++++++ include/hw/virtio/vhost-backend.h | 6 +++++ include/hw/virtio/vhost-user.h | 1 + 4 files changed, 80 insertions(+)
docs/interop/vhost-user.rst +31 -0
@@ -348,6 +348,19 @@ Device state transfer parameters In the future, additional phases might be added e.g. to allow iterative migration while the device is running. +VIRTIO Shared Memory Region configuration +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + ++-------------+---------+------------+----+------------+ +| num regions | padding | mem size 0 | .. | mem size 7 | ++-------------+---------+------------+----+------------+ + +:num regions: a 32-bit number of regions + +:padding: 32-bit + +:mem size: 64-bit size of VIRTIO Shared Memory Region + C structure -----------
@@ -369,6 +382,10 @@ In QEMU the vhost-user message is implemented with the following struct: VhostUserConfig config; VhostUserVringArea area; VhostUserInflight inflight; + VhostUserShared object; + VhostUserTransferDeviceState transfer_state; + VhostUserMMap mmap; + VhostUserShMemConfig shmem; }; } QEMU_PACKED VhostUserMsg;
@@ -1051,6 +1068,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 -----------------------
@@ -1725,6 +1743,19 @@ 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. 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 index + of the array. + Back-end message types ----------------------
hw/virtio/vhost-user.c +42 -0
70 lines not on this page · raw message
include/hw/virtio/vhost-backend.h +6 -0
@@ -159,6 +159,11 @@ 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); +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;
@@ -214,6 +219,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 };
Signature · 1 line
2.45.2
Re: [RFC PATCH v2 2/5] vhost_user: Add frontend command for shmem config
Quoted 22 lines · Albert Esteve
The frontend can use this command to retrieve VIRTIO Shared Memory Regions configuration from the backend. The response contains the number of shared memory regions, their size, and shmid.
This is useful when the frontend is unaware of specific backend type and configuration, for example, in the
vhost-user-devicecase.Signed-off-by Albert Esteve <[email protected]>--- docs/interop/vhost-user.rst | 31 +++++++++++++++++++++++ hw/virtio/vhost-user.c | 42 +++++++++++++++++++++++++++++++ include/hw/virtio/vhost-backend.h | 6 +++++ include/hw/virtio/vhost-user.h | 1 + 4 files changed, 80 insertions(+)docs/interop/vhost-user.rst +0
-0
docs/interop/vhost-user.rst Device state transfer parameters · line 356
Quoted 9 lines
docs/interop/vhost-user.rst +6
-0@@ -348,6 +348,19 @@ Device state transfer parameters In the future, additional phases might be added e.g. to allow iterative migration while the device is running. +VIRTIO Shared Memory Region configuration +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + ++-------------+---------+------------+----+------------+ +| num regions | padding | mem size 0 | .. | mem size 7 | ++-------------+---------+------------+----+------------+
8 regions may not be enough. The max according to the VIRTIO spec is 256 because virtio-pci uses an 8-bit cap.id field for the shmid. I think the maximum number should be 256 here.
(I haven't checked the QEMU vhost-user code to see whether it's reasonable to hardcode to 256 or some logic if needed to dynamically size the buffer depending on the "num regions" field.)
Quoted 11 lines
docs/interop/vhost-user.rst +7
-0+ +:num regions: a 32-bit number of regions + +:padding: 32-bit + +:mem size: 64-bit size of VIRTIO Shared Memory Region + C structure -----------
docs/interop/vhost-user.rst In QEMU the vhost-user message is implemented with the following struct: · line 387
Quoted 6 lines
docs/interop/vhost-user.rst +3
-0@@ -369,6 +382,10 @@ In QEMU the vhost-user message is implemented with the following struct: VhostUserConfig config; VhostUserVringArea area; VhostUserInflight inflight; + VhostUserShared object; + VhostUserTransferDeviceState transfer_state; + VhostUserMMap mmap;
Why are these added by this patch? Please add them in the same patch where they are introduced.
Quoted 18 lines
docs/interop/vhost-user.rst +4
-0+ VhostUserShMemConfig shmem; }; } QEMU_PACKED VhostUserMsg;@@ -1051,6 +1068,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 -----------------------@@ -1725,6 +1743,19 @@ 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
docs/interop/vhost-user.rst Front-end message types · line 1757
Quoted 10 lines
docs/interop/vhost-user.rst +10
-0+ :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. 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 index + of the array.
Is the information returned by SHMEM_CONFIG valid and unchanged for the entire lifetime of the vhost-user connection?
I think the answer is yes because the enumeration that virtio-pci and virtio-mmio transports support is basically a one-time operation at driver startup and it is static (Shared Memory Regions do not appear or go away at runtime). Please be explicit how VHOST_USER_GET_SHMEM_CONFIG is intended to be used.
Quoted 58 lines
docs/interop/vhost-user.rst +1
-0+ Back-end message types ----------------------hw/virtio/vhost-user.c +25
-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;@@ -138,6 +139,12 @@ typedef struct VhostUserMemRegMsg {VhostUserMemoryRegion region; } VhostUserMemRegMsg; +typedef struct VhostUserShMemConfig { +uint32_t nregions; +uint32_t padding; +uint64_t memory_sizes[VHOST_MEMORY_BASELINE_NREGIONS ]; +}VhostUserShMemConfig ; + typedef struct VhostUserLog { uint64_t mmap_size; uint64_t mmap_offset;@@ -245,6 +252,7 @@ typedef union {VhostUserShared object;VhostUserTransferDeviceState transfer_state;VhostUserMMap mmap; +VhostUserShMemConfig shmem; } VhostUserPayload; typedef struct VhostUserMsg {@@ -3136,6 +3144,39 @@ 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 )) { +return 0 ; + } + + ret =vhost_user_write (dev, &msg,NULL ,0 );
hw/virtio/vhost-user.c static int vhost_user_check_device_state(struct vhost_dev *dev, Error **errp) · line 3173
Quoted 10 lines
hw/virtio/vhost-user.c +10
-0+if (ret <0 ) { +return ret; + } + + ret =vhost_user_read (dev, &msg); +if (ret <0 ) { +return ret; + } + + *nregions = msg.payload.shmem.nregions;
Missing input validation from the untrusted vhost-user backend. nregions may be out of range.
Quoted 54 lines
hw/virtio/vhost-user.c +7
-0+memcpy (memory_sizes, + &msg.payload.shmem.memory_sizes, +sizeof (uint64_t ) *VHOST_MEMORY_BASELINE_NREGIONS ); +return 0 ; +} +const VhostOps user_ops = { .backend_type =VHOST_BACKEND_TYPE_USER , .vhost_backend_init = vhost_user_backend_init,@@ -3174,4 +3215,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, };include/hw/virtio/vhost-backend.h +6
-0@@ -159,6 +159,11 @@ 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); +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;@@ -214,6 +219,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 };Signature · 1 line
2.45.2
Re: [RFC PATCH v2 2/5] vhost_user: Add frontend command for shmem config
Quoted 26 lines · Stefan Hajnoczi
Quoted 25 lines · Albert Esteve
The frontend can use this command to retrieve VIRTIO Shared Memory Regions configuration from the backend. The response contains the number of shared memory regions, their size, and shmid.
This is useful when the frontend is unaware of specific backend type and configuration, for example, in the
vhost-user-devicecase.Signed-off-by Albert Esteve <[email protected]>--- docs/interop/vhost-user.rst | 31 +++++++++++++++++++++++ hw/virtio/vhost-user.c | 42 +++++++++++++++++++++++++++++++ include/hw/virtio/vhost-backend.h | 6 +++++ include/hw/virtio/vhost-user.h | 1 + 4 files changed, 80 insertions(+)docs/interop/vhost-user.rst +0
-0@@ -348,6 +348,19 @@ Device state transfer parameters In the future, additional phases might be added e.g. to allow iterative migration while the device is running.
Quoted 11 lines
docs/interop/vhost-user.rst Device state transfer parameters · line 356
Quoted 6 lines
docs/interop/vhost-user.rst +6
-0+VIRTIO Shared Memory Region configuration +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + ++-------------+---------+------------+----+------------+ +| num regions | padding | mem size 0 | .. | mem size 7 | ++-------------+---------+------------+----+------------+8 regions may not be enough. The max according to the VIRTIO spec is 256 because virtio-pci uses an 8-bit cap.id field for the shmid. I think the maximum number should be 256 here.
Ok, I'll set it to 255, as it starts at 0.
Quoted 24 lines
(I haven't checked the QEMU vhost-user code to see whether it's reasonable to hardcode to 256 or some logic if needed to dynamically size the buffer depending on the "num regions" field.)
docs/interop/vhost-user.rst In QEMU the vhost-user message is implemented with
Quoted 11 lines
docs/interop/vhost-user.rst +7
-0+ +:num regions: a 32-bit number of regions + +:padding: 32-bit + +:mem size: 64-bit size of VIRTIO Shared Memory Region + C structure -----------@@ -369,6 +382,10 @@ In QEMU the vhost-user message is implemented withthe following struct:
docs/interop/vhost-user.rst In QEMU the vhost-user message is implemented with the following struct: · line 387
Quoted 6 lines
docs/interop/vhost-user.rst +3
-0VhostUserConfig config; VhostUserVringArea area; VhostUserInflight inflight; + VhostUserShared object; + VhostUserTransferDeviceState transfer_state; + VhostUserMMap mmap;
Quoted 2 lines
Why are these added by this patch? Please add them in the same patch where they are introduced.
For object and transfer_state that's a ship that already sailed. In order to still align the excerpt with the actual code, I will split them into their own commit to avoid confusion.
Quoted 33 lines
Quoted 14 lines
docs/interop/vhost-user.rst +2
-0+ VhostUserShMemConfig shmem; }; } QEMU_PACKED VhostUserMsg;@@ -1051,6 +1068,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 -----------------------@@ -1725,6 +1743,19 @@ Front-end message types Using this function requires prior negotiation of thedocs/interop/vhost-user.rst Front-end message types · line 1753
Quoted 10 lines
docs/interop/vhost-user.rst +8
-0``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 thefront-end
docs/interop/vhost-user.rst Front-end message types · line 1754
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ to gather the VIRTIO Shared Memory Region configuration. Back-endwill respond
docs/interop/vhost-user.rst Front-end message types · line 1755
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ with the number of VIRTIO Shared Memory Regions it requires, and eachshared memory
docs/interop/vhost-user.rst Front-end message types · line 1756
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ region size in an array. The shared memory IDs are represented by theindex
docs/interop/vhost-user.rst Front-end message types · line 1757
Quoted 1 line
docs/interop/vhost-user.rst +1
-0+ of the array.
Quoted 9 lines
Is the information returned by
SHMEM_CONFIGvalid and unchanged for the entire lifetime of the vhost-user connection?I think the answer is yes because the enumeration that virtio-pci and virtio-mmio transports support is basically a one-time operation at driver startup and it is static (Shared Memory Regions do not appear or go away at runtime). Please be explicit how
VHOST_USER_GET_SHMEM_CONFIGis intended to be used.
Yes, I will be explicit.
Quoted 133 lines
hw/virtio/vhost-user.c static int vhost_user_check_device_state(struct
Quoted 39 lines
docs/interop/vhost-user.rst +1
-0+ Back-end message types ----------------------hw/virtio/vhost-user.c +8
-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;@@ -138,6 +139,12 @@ typedef struct VhostUserMemRegMsg {VhostUserMemoryRegion region; } VhostUserMemRegMsg; +typedef struct VhostUserShMemConfig { +uint32_t nregions; +uint32_t padding; +uint64_t memory_sizes[VHOST_MEMORY_BASELINE_NREGIONS ]; +}VhostUserShMemConfig ; + typedef struct VhostUserLog { uint64_t mmap_size; uint64_t mmap_offset;@@ -245,6 +252,7 @@ typedef union {VhostUserShared object;VhostUserTransferDeviceState transfer_state;VhostUserMMap mmap; +VhostUserShMemConfig shmem; } VhostUserPayload; typedef struct VhostUserMsg {@@ -3136,6 +3144,39 @@ static int vhost_user_check_device_state(struct
vhost_dev*dev, Error **errp)Quoted 20 lines
hw/virtio/vhost-user.c +17
-0return 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 )) { +return 0 ; + } + + ret =vhost_user_write (dev, &msg,NULL ,0 );hw/virtio/vhost-user.c static int vhost_user_check_device_state(struct vhost_dev *dev, Error **errp) · line 3173
Quoted 10 lines
hw/virtio/vhost-user.c +10
-0+if (ret <0 ) { +return ret; + } + + ret =vhost_user_read (dev, &msg); +if (ret <0 ) { +return ret; + } + + *nregions = msg.payload.shmem.nregions;Missing input validation from the untrusted vhost-user backend. nregions may be out of range.
Review
Quoted 16 lines
hw/virtio/vhost-user.c +7
-0+memcpy (memory_sizes, + &msg.payload.shmem.memory_sizes, +sizeof (uint64_t ) *VHOST_MEMORY_BASELINE_NREGIONS ); +return 0 ; +} +const VhostOps user_ops = { .backend_type =VHOST_BACKEND_TYPE_USER , .vhost_backend_init = vhost_user_backend_init,@@ -3174,4 +3215,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, };(quoted diff) +0
-0
b/include/hw/virtio/vhost-backend.hinclude/hw/virtio/vhost-backend.h typedef int (*vhost_set_device_state_fd_op)(struct
Quoted 4 lines
index
70c2e8ffee..f9c2955420100644include/hw/virtio/vhost-backend.h +0
-0@@ -159,6 +159,11 @@ typedef int (*vhost_set_device_state_fd_op)(struct
vhost_dev*dev,include/hw/virtio/vhost-backend.h typedef int (*vhost_set_device_state_fd_op)(struct vhost_dev *dev,
Quoted 3 lines
include/hw/virtio/vhost-backend.h +0
-0int *reply_fd, Error **errp); typedef int (*vhost_check_device_state_op)(struct vhost_dev *dev, Error**errp);
Review
Quoted 17 lines
include/hw/virtio/vhost-backend.h +6
-0+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;@@ -214,6 +219,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,(quoted diff) +0
-0
b/include/hw/virtio/vhost-user.hQuoted 14 lines
index
d7c09ffd34..e1b587a908100644include/hw/virtio/vhost-user.h +1
-1@@ -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 }; --2.45.2
Re: [RFC PATCH v2 2/5] vhost_user: Add frontend command for shmem config
In reply to Albert EsteveQuoted 66 lines · Albert Esteve
The frontend can use this command to retrieve VIRTIO Shared Memory Regions configuration from the backend. The response contains the number of shared memory regions, their size, and shmid.
This is useful when the frontend is unaware of specific backend type and configuration, for example, in the
vhost-user-devicecase.Signed-off-by Albert Esteve <[email protected]>--- docs/interop/vhost-user.rst | 31 +++++++++++++++++++++++ hw/virtio/vhost-user.c | 42 +++++++++++++++++++++++++++++++ include/hw/virtio/vhost-backend.h | 6 +++++ include/hw/virtio/vhost-user.h | 1 + 4 files changed, 80 insertions(+)docs/interop/vhost-user.rst +20
-0@@ -348,6 +348,19 @@ Device state transfer parameters In the future, additional phases might be added e.g. to allow iterative migration while the device is running. +VIRTIO Shared Memory Region configuration +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + ++-------------+---------+------------+----+------------+ +| num regions | padding | mem size 0 | .. | mem size 7 | ++-------------+---------+------------+----+------------+ + +:num regions: a 32-bit number of regions + +:padding: 32-bit + +:mem size: 64-bit size of VIRTIO Shared Memory Region + C structure -----------@@ -369,6 +382,10 @@ In QEMU the vhost-user message is implemented with the following struct: VhostUserConfig config; VhostUserVringArea area; VhostUserInflight inflight; + VhostUserShared object; + VhostUserTransferDeviceState transfer_state; + VhostUserMMap mmap; + VhostUserShMemConfig shmem; }; } QEMU_PACKED VhostUserMsg;@@ -1051,6 +1068,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 -----------------------@@ -1725,6 +1743,19 @@ 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
docs/interop/vhost-user.rst Front-end message types · line 1757
Quoted 10 lines
docs/interop/vhost-user.rst +10
-0+ :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. 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 index + of the array.
Please add:
- 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.
RFC PATCH v2 3/5 vhost-user-dev: Add cache BAR
In reply to Albert EsteveAdd a cache BAR in the vhost-user-device
into which files can be directly mapped.
The number, shmid, and size of the VIRTIO Shared Memory subregions is retrieved through a get_shmem_config message sent by the vhost-user-base module
on the realize step, after virtio_init().
By default, if VHOST_USER_PROTOCOL_F_SHMEM
feature is not supported by the backend,
there is no cache.
--- hw/virtio/vhost-user-base.c | 39 +++++++++++++++++++++++++++++-- hw/virtio/vhost-user-device-pci.c | 37 ++++++++++++++++++++++++++--- 2 files changed, 71 insertions(+), 5 deletions(-)
hw/virtio/vhost-user-base.c +37 -2
@@ -268,7 +268,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[8 ]; +void *cache_ptr; +int i, ret, nregions;if (!vub->chardev.chr) {error_setg (errp,"vhost-user-base: missing chardev" );
@@ -311,7 +313,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));
@@ -328,6 +330,39 @@ static void vub_device_realize(DeviceState *dev, Error **errp)do_vhost_user_cleanup (vdev, vub); } + ret = vub->vhost_dev.vhost_ops->vhost_get_shmem_config (&vub->vhost_dev, + &nregions, + memory_sizes, + errp); + +if (ret <0 ) { +do_vhost_user_cleanup (vdev, vub); + } + +for (i =0 ; i < nregions; i++) { +if (memory_sizes[i]) { +if (!is_power_of_2 (memory_sizes[i]) || + memory_sizes[i] <qemu_real_host_page_size ()) { +error_setg (errp,"Shared memory %d size must be a power of 2 " +"no smaller than the page size" , i); +return ; + } + + cache_ptr =mmap (NULL , memory_sizes[i],PROT_READ , +MAP_ANONYMOUS |MAP_PRIVATE ,-1 ,0 ); +if (cache_ptr ==MAP_FAILED ) { +error_setg (errp,"Unable to mmap blank cache: %s" , +strerror (errno)); +return ; + } + +virtio_new_shmem_region (vdev); +memory_region_init_ram_ptr (&vdev->shmem_list[i], +OBJECT (vdev),"vub-shm-" + i, + memory_sizes[i], cache_ptr); + } + } +qemu_chr_fe_set_handlers (&vub->chardev,NULL ,NULL , vub_event,NULL , dev,NULL , true); }
hw/virtio/vhost-user-device-pci.c +34 -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_CACHE_BAR 2 +struct VHostUserDevicePCI {VirtIOPCIProxy parent_obj;VHostUserBase vub; +MemoryRegion cachebar; };#define TYPE_VHOST_USER_DEVICE_PCI "vhost-user-device-pci-base"
@@ -25,10 +29,37 @@ OBJECT_DECLARE_SIMPLE_TYPE(VHostUserDevicePCI, VHOST_USER_DEVICE_PCI)static void vhost_user_device_pci_realize (VirtIOPCIProxy *vpci_dev,Error **errp) {VHostUserDevicePCI *dev =VHOST_USER_DEVICE_PCI (vpci_dev); -DeviceState *vdev =DEVICE (&dev->vub); - +DeviceState *dev_state =DEVICE (&dev->vub); +VirtIODevice *vdev =VIRTIO_DEVICE (dev_state); +uint64_t offset =0 , cache_size =0 ; +int i; + vpci_dev->nvectors =1 ; -qdev_realize (vdev,BUS (&vpci_dev->bus), errp); +qdev_realize (dev_state,BUS (&vpci_dev->bus), errp); + +for (i =0 ; i < vdev->n_shmem_regions; i++) { +if (vdev->shmem_list[i].size >UINT64_MAX - cache_size) { +error_setg (errp,"Total shared memory required overflow" ); +return ; + } + cache_size = cache_size + vdev->shmem_list[i].size; + } +if (cache_size) { +memory_region_init (&dev->cachebar,OBJECT (vpci_dev), +"vhost-device-pci-cachebar" , cache_size); +for (i =0 ; i < vdev->n_shmem_regions; i++) { +memory_region_add_subregion (&dev->cachebar, offset, + &vdev->shmem_list[i]); +virtio_pci_add_shm_cap (vpci_dev,VIRTIO_DEVICE_PCI_CACHE_BAR , + offset, vdev->shmem_list[i].size, i); + offset = offset + vdev->shmem_list[i].size; + } +pci_register_bar (&vpci_dev->pci_dev,VIRTIO_DEVICE_PCI_CACHE_BAR , +PCI_BASE_ADDRESS_SPACE_MEMORY | +PCI_BASE_ADDRESS_MEM_PREFETCH | +PCI_BASE_ADDRESS_MEM_TYPE_64 , + &dev->cachebar); + } }static void vhost_user_device_pci_class_init (ObjectClass *klass,void *data)
Signature · 1 line
2.45.2
Re: [RFC PATCH v2 3/5] vhost-user-dev: Add cache BAR
Quoted 8 lines · Albert Esteve
Add a cache BAR in the vhost-user-device
into which files can be directly mapped.The number, shmid, and size of the VIRTIO Shared Memory subregions is retrieved through a
get_shmem_configmessage sent by the vhost-user-base module
on the realize step, aftervirtio_init().
Quoted 5 lines
By default, if
VHOST_USER_PROTOCOL_F_SHMEM
feature is not supported by the backend,
there is no cache.Signed-off-by Albert Esteve <[email protected]>
Michael: Please review vhost_user_device_pci_realize() below regarding virtio-pci BAR layout. Thanks!
Quoted 36 lines
--- hw/virtio/vhost-user-base.c | 39 +++++++++++++++++++++++++++++-- hw/virtio/vhost-user-device-pci.c | 37 ++++++++++++++++++++++++++--- 2 files changed, 71 insertions(+), 5 deletions(-)hw/virtio/vhost-user-base.c +7
-2@@ -268,7 +268,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[8 ]; +void *cache_ptr; +int i, ret, nregions;if (!vub->chardev.chr) {error_setg (errp,"vhost-user-base: missing chardev" );@@ -311,7 +313,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));@@ -328,6 +330,39 @@ static void vub_device_realize(DeviceState *dev, Error **errp)do_vhost_user_cleanup (vdev, vub); } + ret = vub->vhost_dev.vhost_ops->vhost_get_shmem_config(&vub->vhost_dev, + &nregions, + memory_sizes,
hw/virtio/vhost-user-base.c static void vub_device_realize(DeviceState *dev, Error **errp) · line 345
Quoted 10 lines
hw/virtio/vhost-user-base.c +10
-0+ errp); + +if (ret <0 ) { +do_vhost_user_cleanup (vdev, vub); + } + +for (i =0 ; i < nregions; i++) { +if (memory_sizes[i]) { +if (!is_power_of_2 (memory_sizes[i]) || + memory_sizes[i] <qemu_real_host_page_size ()) {
Or just if (memory_sizes[i] % qemu_real_host_page_size() != 0)?
hw/virtio/vhost-user-base.c static void vub_device_realize(DeviceState *dev, Error **errp) · line 351
Quoted 6 lines
hw/virtio/vhost-user-base.c +6
-0+error_setg (errp,"Shared memory %d size must be a power of 2 " +"no smaller than the page size" , i); +return ; + } + + cache_ptr = mmap(NULL, memory_sizes[i], PROT_READ,
Should this be PROT_NONE like invhost_user_backend_handle_shmem_unmap()?
hw/virtio/vhost-user-base.c static void vub_device_realize(DeviceState *dev, Error **errp) · line 355
Quoted 4 lines
hw/virtio/vhost-user-base.c +4
-0+ MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); +if (cache_ptr ==MAP_FAILED ) { +error_setg (errp,"Unable to mmap blank cache: %s" , +strerror (errno));
error_setg_errno() can be used here.
Quoted 80 lines
hw/virtio/vhost-user-base.c +10
-0+return ; + } + +virtio_new_shmem_region (vdev); +memory_region_init_ram_ptr (&vdev->shmem_list[i], +OBJECT (vdev),"vub-shm-" + i, + memory_sizes[i], cache_ptr); + } + } +qemu_chr_fe_set_handlers (&vub->chardev,NULL ,NULL , vub_event,NULL , dev,NULL , true); }hw/virtio/vhost-user-device-pci.c +34
-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_CACHE_BAR 2 +struct VHostUserDevicePCI {VirtIOPCIProxy parent_obj;VHostUserBase vub; +MemoryRegion cachebar; };#define TYPE_VHOST_USER_DEVICE_PCI "vhost-user-device-pci-base"@@ -25,10 +29,37 @@ OBJECT_DECLARE_SIMPLE_TYPE(VHostUserDevicePCI, VHOST_USER_DEVICE_PCI)static void vhost_user_device_pci_realize (VirtIOPCIProxy *vpci_dev,Error **errp) {VHostUserDevicePCI *dev =VHOST_USER_DEVICE_PCI (vpci_dev); -DeviceState *vdev =DEVICE (&dev->vub); - +DeviceState *dev_state =DEVICE (&dev->vub); +VirtIODevice *vdev =VIRTIO_DEVICE (dev_state); +uint64_t offset =0 , cache_size =0 ; +int i; + vpci_dev->nvectors =1 ; -qdev_realize (vdev,BUS (&vpci_dev->bus), errp); +qdev_realize (dev_state,BUS (&vpci_dev->bus), errp); + +for (i =0 ; i < vdev->n_shmem_regions; i++) { +if (vdev->shmem_list[i].size >UINT64_MAX - cache_size) { +error_setg (errp,"Total shared memory required overflow" ); +return ; + } + cache_size = cache_size + vdev->shmem_list[i].size; + } +if (cache_size) { +memory_region_init (&dev->cachebar,OBJECT (vpci_dev), +"vhost-device-pci-cachebar" , cache_size); +for (i =0 ; i < vdev->n_shmem_regions; i++) { +memory_region_add_subregion (&dev->cachebar, offset, + &vdev->shmem_list[i]); +virtio_pci_add_shm_cap (vpci_dev,VIRTIO_DEVICE_PCI_CACHE_BAR , + offset, vdev->shmem_list[i].size, i); + offset = offset + vdev->shmem_list[i].size; + } +pci_register_bar (&vpci_dev->pci_dev,VIRTIO_DEVICE_PCI_CACHE_BAR , +PCI_BASE_ADDRESS_SPACE_MEMORY | +PCI_BASE_ADDRESS_MEM_PREFETCH | +PCI_BASE_ADDRESS_MEM_TYPE_64 , + &dev->cachebar); + } }static void vhost_user_device_pci_class_init (ObjectClass *klass,void *data)Signature · 1 line
2.45.2
Re: [RFC PATCH v2 3/5] vhost-user-dev: Add cache BAR
Quoted 56 lines · Stefan Hajnoczi
Quoted 8 lines · Albert Esteve
Add a cache BAR in the vhost-user-device
into which files can be directly mapped.The number, shmid, and size of the VIRTIO Shared Memory subregions is retrieved through a
get_shmem_configmessage sent by the vhost-user-base module
on the realize step, aftervirtio_init().Quoted 5 lines
By default, if
VHOST_USER_PROTOCOL_F_SHMEM
feature is not supported by the backend,
there is no cache.Signed-off-by Albert Esteve <[email protected]>Michael: Please review
vhost_user_device_pci_realize()below regarding virtio-pci BAR layout. Thanks!hw/virtio/vhost-user-base.c static void vub_device_realize(DeviceState *dev,
Quoted 10 lines
--- hw/virtio/vhost-user-base.c | 39 +++++++++++++++++++++++++++++-- hw/virtio/vhost-user-device-pci.c | 37 ++++++++++++++++++++++++++--- 2 files changed, 71 insertions(+), 5 deletions(-)hw/virtio/vhost-user-base.c +0
-0@@ -268,7 +268,9 @@ static void vub_device_realize(DeviceState *dev,Error **errp)
hw/virtio/vhost-user-base.c static void vub_device_realize(DeviceState *dev,
Quoted 11 lines
hw/virtio/vhost-user-base.c +3
-1{VirtIODevice *vdev =VIRTIO_DEVICE (dev);VHostUserBase *vub =VHOST_USER_BASE (dev); -int ret; +uint64_t memory_sizes[8 ]; +void *cache_ptr; +int i, ret, nregions;if (!vub->chardev.chr) {error_setg (errp,"vhost-user-base: missing chardev" );@@ -311,7 +313,7 @@ static void vub_device_realize(DeviceState *dev,Error **errp)
hw/virtio/vhost-user-base.c static void vub_device_realize(DeviceState *dev,
Quoted 9 lines
hw/virtio/vhost-user-base.c +1
-1/* 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));@@ -328,6 +330,39 @@ static void vub_device_realize(DeviceState *dev,Error **errp)
hw/virtio/vhost-user-base.c static void vub_device_realize(DeviceState *dev, Error **errp)
Quoted 4 lines
hw/virtio/vhost-user-base.c +1
-0do_vhost_user_cleanup (vdev, vub); } + ret =vub->vhost_dev.vhost_ops->vhost_get_shmem_config(&vub->vhost_dev,
Quoted 14 lines
hw/virtio/vhost-user-base.c static void vub_device_realize(DeviceState *dev, Error **errp) · line 345
Quoted 12 lines
hw/virtio/vhost-user-base.c +12
-0+ &nregions, + memory_sizes, + errp); + +if (ret <0 ) { +do_vhost_user_cleanup (vdev, vub); + } + +for (i =0 ; i < nregions; i++) { +if (memory_sizes[i]) { +if (!is_power_of_2 (memory_sizes[i]) || + memory_sizes[i] <qemu_real_host_page_size ()) {Or just if (memory_sizes[i] %
qemu_real_host_page_size()!= 0)?
I like both options. The original is more explicit, your proposal is more concise.
I will change it.
Quoted 12 lines
hw/virtio/vhost-user-base.c static void vub_device_realize(DeviceState *dev, · line 346
Quoted 2 lines
hw/virtio/vhost-user-base.c +1
-0+ error_setg(errp, "Shared memory %d size must be a powerof 2 "
hw/virtio/vhost-user-base.c static void vub_device_realize(DeviceState *dev, Error **errp) · line 351
Quoted 5 lines
hw/virtio/vhost-user-base.c +5
-0+ "no smaller than the page size", i); +return ; + } + + cache_ptr = mmap(NULL, memory_sizes[i], PROT_READ,Should this be
PROT_NONElike invhost_user_backend_handle_shmem_unmap()?
Since this is supposed to be blank memory, I think you may be right. But I am not completely certain. I'll change it and check if everything works as expected on my side.
Quoted 95 lines
hw/virtio/vhost-user-base.c static void vub_device_realize(DeviceState *dev, Error **errp) · line 355
Quoted 5 lines
hw/virtio/vhost-user-base.c +4
-0+ MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); +if (cache_ptr ==MAP_FAILED ) { +error_setg (errp,"Unable to mmap blank cache: %s" , +strerror (errno));
error_setg_errno()can be used here.Review
Quoted 14 lines
hw/virtio/vhost-user-base.c +10
-0+return ; + } + +virtio_new_shmem_region (vdev); +memory_region_init_ram_ptr (&vdev->shmem_list[i], +OBJECT (vdev),"vub-shm-" + i, + memory_sizes[i], cache_ptr); + } + } +qemu_chr_fe_set_handlers (&vub->chardev,NULL ,NULL , vub_event,NULL , dev,NULL , true); }(quoted diff) +0
-0
b/hw/virtio/vhost-user-device-pci.chw/virtio/vhost-user-device-pci.c OBJECT_DECLARE_SIMPLE_TYPE(VHostUserDevicePCI,
Quoted 23 lines
index
efaf55d3dd..314bacfb7a100644hw/virtio/vhost-user-device-pci.c +4
-0@@ -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_CACHE_BAR 2 +struct VHostUserDevicePCI {VirtIOPCIProxy parent_obj;VHostUserBase vub; +MemoryRegion cachebar; };#define TYPE_VHOST_USER_DEVICE_PCI "vhost-user-device-pci-base"@@ -25,10 +29,37 @@ OBJECT_DECLARE_SIMPLE_TYPE(VHostUserDevicePCI,
VHOST_USER_DEVICE_PCI)hw/virtio/vhost-user-device-pci.c OBJECT_DECLARE_SIMPLE_TYPE(VHostUserDevicePCI, · line 29
Quoted 1 line
hw/virtio/vhost-user-device-pci.c +0
-0static void vhost_user_device_pci_realize(VirtIOPCIProxy *vpci_dev,Error **errp)
Quoted 17 lines
hw/virtio/vhost-user-device-pci.c +11
-3{VHostUserDevicePCI *dev =VHOST_USER_DEVICE_PCI (vpci_dev); -DeviceState *vdev =DEVICE (&dev->vub); - +DeviceState *dev_state =DEVICE (&dev->vub); +VirtIODevice *vdev =VIRTIO_DEVICE (dev_state); +uint64_t offset =0 , cache_size =0 ; +int i; + vpci_dev->nvectors =1 ; -qdev_realize (vdev,BUS (&vpci_dev->bus), errp); +qdev_realize (dev_state,BUS (&vpci_dev->bus), errp); + +for (i =0 ; i < vdev->n_shmem_regions; i++) { +if (vdev->shmem_list[i].size >UINT64_MAX - cache_size) { +error_setg (errp,"Total shared memory required overflow" ); +return ;hw/virtio/vhost-user-device-pci.c OBJECT_DECLARE_SIMPLE_TYPE(VHostUserDevicePCI, VHOST_USER_DEVICE_PCI)
Quoted 10 lines
hw/virtio/vhost-user-device-pci.c +10
-0+ } + cache_size = cache_size + vdev->shmem_list[i].size; + } +if (cache_size) { +memory_region_init (&dev->cachebar,OBJECT (vpci_dev), +"vhost-device-pci-cachebar" , cache_size); +for (i =0 ; i < vdev->n_shmem_regions; i++) { +memory_region_add_subregion (&dev->cachebar, offset, + &vdev->shmem_list[i]); + virtio_pci_add_shm_cap(vpci_dev,
VIRTIO_DEVICE_PCI_CACHE_BAR,hw/virtio/vhost-user-device-pci.c OBJECT_DECLARE_SIMPLE_TYPE(VHostUserDevicePCI, VHOST_USER_DEVICE_PCI)
Quoted 4 lines
hw/virtio/vhost-user-device-pci.c +4
-0+ offset, vdev->shmem_list[i].size, i); + offset = offset + vdev->shmem_list[i].size; + } + pci_register_bar(&vpci_dev->pci_dev,
VIRTIO_DEVICE_PCI_CACHE_BAR,hw/virtio/vhost-user-device-pci.c OBJECT_DECLARE_SIMPLE_TYPE(VHostUserDevicePCI, VHOST_USER_DEVICE_PCI)
Quoted 8 lines
hw/virtio/vhost-user-device-pci.c +5
-0+PCI_BASE_ADDRESS_SPACE_MEMORY | +PCI_BASE_ADDRESS_MEM_PREFETCH | +PCI_BASE_ADDRESS_MEM_TYPE_64 , + &dev->cachebar); + } } static void vhost_user_device_pci_class_init(ObjectClass *klass, void*data)
Quoted 3 lines
--
2.45.2
RFC PATCH v2 4/5 vhost_user: Add MEM_READ/WRITE backend requests
In reply to Albert EsteveWith SHMEM_MAP messages, sharing descriptors between devices will cause that these devices do not see the mappings, and fail to access these memory regions.
To solve this, introduce MEM_READ/WRITE requests that will get triggered as a fallback when vhost-user memory translation fails.
--- hw/virtio/vhost-user.c | 31 +++++++++ subprojects/libvhost-user/libvhost-user.c | 84 +++++++++++++++++++++++ subprojects/libvhost-user/libvhost-user.h | 38 ++++++++++ 3 files changed, 153 insertions(+)
hw/virtio/vhost-user.c +31 -0
@@ -118,6 +118,8 @@ typedef enum VhostUserBackendRequest {VHOST_USER_BACKEND_SHARED_OBJECT_LOOKUP =8 ,VHOST_USER_BACKEND_SHMEM_MAP =9 ,VHOST_USER_BACKEND_SHMEM_UNMAP =10 , +VHOST_USER_BACKEND_MEM_READ =11 , +VHOST_USER_BACKEND_MEM_WRITE =12 , VHOST_USER_BACKEND_MAX } VhostUserBackendRequest;
@@ -145,6 +147,12 @@ typedef struct VhostUserShMemConfig {uint64_t memory_sizes[VHOST_MEMORY_BASELINE_NREGIONS ]; } VhostUserShMemConfig; +typedef struct VhostUserMemRWMsg { +uint64_t guest_address; +uint32_t size; +uint8_t data[]; +}VhostUserMemRWMsg ; + typedef struct VhostUserLog { uint64_t mmap_size; uint64_t mmap_offset;
@@ -253,6 +261,7 @@ typedef union {VhostUserTransferDeviceState transfer_state;VhostUserMMap mmap;VhostUserShMemConfig shmem; +VhostUserMemRWMsg mem_rw; } VhostUserPayload; typedef struct VhostUserMsg {
@@ -1871,6 +1880,22 @@ vhost_user_backend_handle_shmem_unmap(struct vhost_dev *dev,return 0 ; } +static int +vhost_user_backend_handle_mem_read (struct vhost_dev *dev, +VhostUserMemRWMsg *mem_rw) +{ +/* TODO */ +return -EPERM ; +} + +static int +vhost_user_backend_handle_mem_write (struct vhost_dev *dev, +VhostUserMemRWMsg *mem_rw) +{ +/* TODO */ +return -EPERM ; +} +static void close_backend_channel (struct vhost_user *u) {g_source_destroy (u->backend_src);
@@ -1946,6 +1971,12 @@ static gboolean backend_read(QIOChannel *ioc, GIOCondition condition,case VHOST_USER_BACKEND_SHMEM_UNMAP : ret =vhost_user_backend_handle_shmem_unmap (dev, &payload.mmap);break ; +case VHOST_USER_BACKEND_MEM_READ : + ret =vhost_user_backend_handle_mem_read (dev, &payload.mem_rw); +break ; +case VHOST_USER_BACKEND_MEM_WRITE : + ret =vhost_user_backend_handle_mem_write (dev, &payload.mem_rw); +break ;default :error_report ("Received unexpected msg type: %d." , hdr.request); ret = -EINVAL ;
subprojects/libvhost-user/libvhost-user.c +84 -0
90 lines not on this page · raw message
subprojects/libvhost-user/libvhost-user.h +38 -0
@@ -129,6 +129,8 @@ typedef enum VhostUserBackendRequest {VHOST_USER_BACKEND_SHARED_OBJECT_LOOKUP =8 ,VHOST_USER_BACKEND_SHMEM_MAP =9 ,VHOST_USER_BACKEND_SHMEM_UNMAP =10 , +VHOST_USER_BACKEND_MEM_READ =11 , +VHOST_USER_BACKEND_MEM_WRITE =12 , VHOST_USER_BACKEND_MAX } VhostUserBackendRequest;
@@ -152,6 +154,12 @@ typedef struct VhostUserMemRegMsg {VhostUserMemoryRegion region; } VhostUserMemRegMsg; +typedef struct VhostUserMemRWMsg { +uint64_t guest_address; +uint32_t size; +uint8_t data[]; +}VhostUserMemRWMsg ; + typedef struct VhostUserLog { uint64_t mmap_size; uint64_t mmap_offset;
@@ -235,6 +243,7 @@ typedef struct VhostUserMsg {VhostUserInflight inflight;VhostUserShared object;VhostUserMMap mmap; +VhostUserMemRWMsg mem_rw; } payload;int fds[VHOST_MEMORY_BASELINE_NREGIONS ];
@@ -650,6 +659,35 @@ bool vu_shmem_map(VuDev *dev, uint8_t shmid, uint64_t fd_offset,bool vu_shmem_unmap (VuDev *dev,uint8_t shmid,uint64_t fd_offset,uint64_t shm_offset,uint64_t len); +/** +* vu_send_mem_read: +* @dev: a VuDev context +* @guest_addr: guest physical address to read +* @size: number of bytes to read +* @data: head of an unitialized bytes array +* +* Reads `size` bytes of `guest_addr` in the frontend and stores +* them in `data`. +* +* Returns: TRUE on success, FALSE on failure. +*/ +bool vu_send_mem_read (VuDev *dev,uint64_t guest_addr,uint32_t size, +uint8_t *data); + +/** +* vu_send_mem_write: +* @dev: a VuDev context +* @guest_addr: guest physical address to write +* @size: number of bytes to write +* @data: head of an array with `size` bytes to write +* +* Writes `size` bytes from `data` into `guest_addr` in the frontend. +* +* Returns: TRUE on success, FALSE on failure. +*/ +bool vu_send_mem_write (VuDev *dev,uint64_t guest_addr,uint32_t size, +uint8_t *data); +/** * vu_queue_set_notification: * @dev: a VuDev context
Signature · 1 line
2.45.2
Re: [RFC PATCH v2 4/5] vhost_user: Add MEM_READ/WRITE backend requests
Quoted 29 lines · Albert Esteve
With
SHMEM_MAPmessages, sharing descriptors between devices will cause that these devices do not see the mappings, and fail to access these memory regions.To solve this, introduce MEM_READ/WRITE requests that will get triggered as a fallback when vhost-user memory translation fails.
Signed-off-by Albert Esteve <[email protected]>--- hw/virtio/vhost-user.c | 31 +++++++++ subprojects/libvhost-user/libvhost-user.c | 84 +++++++++++++++++++++++ subprojects/libvhost-user/libvhost-user.h | 38 ++++++++++ 3 files changed, 153 insertions(+)hw/virtio/vhost-user.c +2
-0@@ -118,6 +118,8 @@ typedef enum VhostUserBackendRequest {VHOST_USER_BACKEND_SHARED_OBJECT_LOOKUP =8 ,VHOST_USER_BACKEND_SHMEM_MAP =9 ,VHOST_USER_BACKEND_SHMEM_UNMAP =10 , +VHOST_USER_BACKEND_MEM_READ =11 , +VHOST_USER_BACKEND_MEM_WRITE =12 , VHOST_USER_BACKEND_MAX } VhostUserBackendRequest;
hw/virtio/vhost-user.c typedef struct VhostUserShMemConfig { · line 153
Quoted 7 lines
hw/virtio/vhost-user.c +4
-0@@ -145,6 +147,12 @@ typedef struct VhostUserShMemConfig {uint64_t memory_sizes[VHOST_MEMORY_BASELINE_NREGIONS ]; } VhostUserShMemConfig; +typedef struct VhostUserMemRWMsg { + uint64_t guest_address; + uint32_t size; + uint8_t data[];
I don't think flexible array members work in VhostUserMsg payload structs in its current form. It would be necessary to move the VhostUserMsg.payload field to the end of the VhostUserMsg and then heap-allocate VhostUserMsg with the additional size required for VhostUserMemRWMsg.data[].
Right now this patch is calling memcpy() on memory beyond VhostUserMsg.payload because the VhostUserMsg struct does not have size bytes of extra space and the payload field is in the middle of the struct where flexible array members cannot be used.
Quoted 22 lines
hw/virtio/vhost-user.c +8
-0+} VhostUserMemRWMsg; + typedef struct VhostUserLog { uint64_t mmap_size; uint64_t mmap_offset;@@ -253,6 +261,7 @@ typedef union {VhostUserTransferDeviceState transfer_state;VhostUserMMap mmap;VhostUserShMemConfig shmem; +VhostUserMemRWMsg mem_rw; } VhostUserPayload; typedef struct VhostUserMsg {@@ -1871,6 +1880,22 @@ vhost_user_backend_handle_shmem_unmap(struct vhost_dev *dev,return 0 ; } +static int +vhost_user_backend_handle_mem_read (struct vhost_dev *dev, +VhostUserMemRWMsg *mem_rw) +{ +/* TODO */
hw/virtio/vhost-user.c vhost_user_backend_handle_shmem_unmap(struct vhost_dev *dev, · line 1897
Quoted 10 lines
hw/virtio/vhost-user.c +10
-0+return -EPERM ; +} + +static int +vhost_user_backend_handle_mem_write (struct vhost_dev *dev, +VhostUserMemRWMsg *mem_rw) +{ +/* TODO */ +return -EPERM ; +}
Reading/writing guest memory can be done via
address_space_read/write(vdev->dma_as, ...).
Quoted 55 lines
hw/virtio/vhost-user.c +7
-0+static void close_backend_channel (struct vhost_user *u) {g_source_destroy (u->backend_src);@@ -1946,6 +1971,12 @@ static gboolean backend_read(QIOChannel *ioc, GIOCondition condition,case VHOST_USER_BACKEND_SHMEM_UNMAP : ret =vhost_user_backend_handle_shmem_unmap (dev, &payload.mmap);break ; +case VHOST_USER_BACKEND_MEM_READ : + ret =vhost_user_backend_handle_mem_read (dev, &payload.mem_rw); +break ; +case VHOST_USER_BACKEND_MEM_WRITE : + ret =vhost_user_backend_handle_mem_write (dev, &payload.mem_rw); +break ;default :error_report ("Received unexpected msg type: %d." , hdr.request); ret = -EINVAL ;subprojects/libvhost-user/libvhost-user.c +30
-0@@ -1651,6 +1651,90 @@ vu_shmem_unmap(VuDev *dev, uint8_t shmid, uint64_t fd_offset,return vu_process_message_reply (dev, &vmsg); } +bool +vu_send_mem_read (VuDev *dev,uint64_t guest_addr,uint32_t size, +uint8_t *data) +{ +VhostUserMsg msg_reply; +VhostUserMsg msg = { + .request =VHOST_USER_BACKEND_MEM_READ , + .size =sizeof (msg.payload.mem_rw), + .flags =VHOST_USER_VERSION |VHOST_USER_NEED_REPLY_MASK , + .payload = { + .mem_rw = { + .guest_address = guest_addr, + .size = size, + } + } + }; + +pthread_mutex_lock (&dev->backend_mutex); +if (!vu_message_write (dev, dev->backend_fd, &msg)) { + goto out_err; + } + +if (!vu_message_read_default (dev, dev->backend_fd, &msg_reply)) { + goto out_err; + } + +if (msg_reply.request != msg.request) { +DPRINT ("Received unexpected msg type. Expected %d, received %d" , + msg.request, msg_reply.request); + goto out_err;
subprojects/libvhost-user/libvhost-user.c vu_shmem_unmap(VuDev *dev, uint8_t shmid, uint64_t fd_offset, · line 1693
Quoted 10 lines
subprojects/libvhost-user/libvhost-user.c +10
-0+ } + +if (msg_reply.payload.mem_rw.size != size) { +DPRINT ("Received unexpected number of bytes in the response. " +"Expected %d, received %d" , + size, msg_reply.payload.mem_rw.size); + goto out_err; + } + + data =malloc (msg_reply.payload.mem_rw.size);
The caller passed in size and data so the caller has provided the buffer. malloc() is not necessary here.
subprojects/libvhost-user/libvhost-user.c vu_shmem_unmap(VuDev *dev, uint8_t shmid, uint64_t fd_offset, · line 1699
Quoted 6 lines
subprojects/libvhost-user/libvhost-user.c +6
-0+if (!data) { +DPRINT ("Failed to malloc read memory data" ); + goto out_err; + } + +memcpy (data, msg_reply.payload.mem_rw.data, size);
It should be possible to avoid memcpy() here by receiving directly into the caller's buffer. If you don't want to look into this, please leave a TODO comment.
Quoted 14 lines
subprojects/libvhost-user/libvhost-user.c +14
-0+pthread_mutex_unlock (&dev->backend_mutex); +return true; + +out_err: +pthread_mutex_unlock (&dev->backend_mutex); +return false; +} + +bool +vu_send_mem_write (VuDev *dev,uint64_t guest_addr,uint32_t size, +uint8_t *data) +{ +VhostUserMsg msg = { + .request =VHOST_USER_BACKEND_MEM_WRITE ,
subprojects/libvhost-user/libvhost-user.c vu_shmem_unmap(VuDev *dev, uint8_t shmid, uint64_t fd_offset, · line 1723
Quoted 10 lines
subprojects/libvhost-user/libvhost-user.c +10
-0+ .size = sizeof(msg.payload.mem_rw), + .flags = VHOST_USER_VERSION, + .payload = { + .mem_rw = { + .guest_address = guest_addr, + .size = size, + } + } + }; +memcpy (msg.payload.mem_rw.data, data, size);
This memcpy() can be eliminated too. It's worth a code comment in case someone looks at optimizing this in the future.
Quoted 90 lines
subprojects/libvhost-user/libvhost-user.c +14
-0+ +if (vu_has_protocol_feature (dev,VHOST_USER_PROTOCOL_F_REPLY_ACK )) { + msg.flags |=VHOST_USER_NEED_REPLY_MASK ; + } + +if (!vu_message_write (dev, dev->backend_fd, &msg)) { +pthread_mutex_unlock (&dev->backend_mutex); +return false; + } + +/* Also unlocks the backend_mutex */ +return vu_process_message_reply (dev, &msg); +} +static bool vu_set_vring_call_exec (VuDev *dev,VhostUserMsg *vmsg) {subprojects/libvhost-user/libvhost-user.h +38
-0@@ -129,6 +129,8 @@ typedef enum VhostUserBackendRequest {VHOST_USER_BACKEND_SHARED_OBJECT_LOOKUP =8 ,VHOST_USER_BACKEND_SHMEM_MAP =9 ,VHOST_USER_BACKEND_SHMEM_UNMAP =10 , +VHOST_USER_BACKEND_MEM_READ =11 , +VHOST_USER_BACKEND_MEM_WRITE =12 , VHOST_USER_BACKEND_MAX } VhostUserBackendRequest;@@ -152,6 +154,12 @@ typedef struct VhostUserMemRegMsg {VhostUserMemoryRegion region; } VhostUserMemRegMsg; +typedef struct VhostUserMemRWMsg { +uint64_t guest_address; +uint32_t size; +uint8_t data[]; +}VhostUserMemRWMsg ; + typedef struct VhostUserLog { uint64_t mmap_size; uint64_t mmap_offset;@@ -235,6 +243,7 @@ typedef struct VhostUserMsg {VhostUserInflight inflight;VhostUserShared object;VhostUserMMap mmap; +VhostUserMemRWMsg mem_rw; } payload;int fds[VHOST_MEMORY_BASELINE_NREGIONS ];@@ -650,6 +659,35 @@ bool vu_shmem_map(VuDev *dev, uint8_t shmid, uint64_t fd_offset,bool vu_shmem_unmap (VuDev *dev,uint8_t shmid,uint64_t fd_offset,uint64_t shm_offset,uint64_t len); +/** +* vu_send_mem_read: +* @dev: a VuDev context +* @guest_addr: guest physical address to read +* @size: number of bytes to read +* @data: head of an unitialized bytes array +* +* Reads `size` bytes of `guest_addr` in the frontend and stores +* them in `data`. +* +* Returns: TRUE on success, FALSE on failure. +*/ +bool vu_send_mem_read (VuDev *dev,uint64_t guest_addr,uint32_t size, +uint8_t *data); + +/** +* vu_send_mem_write: +* @dev: a VuDev context +* @guest_addr: guest physical address to write +* @size: number of bytes to write +* @data: head of an array with `size` bytes to write +* +* Writes `size` bytes from `data` into `guest_addr` in the frontend. +* +* Returns: TRUE on success, FALSE on failure. +*/ +bool vu_send_mem_write (VuDev *dev,uint64_t guest_addr,uint32_t size, +uint8_t *data); +/** * vu_queue_set_notification: * @dev: a VuDev context Signature · 1 line
2.45.2
RFC PATCH v2 5/5 vhost_user: Implement mem_read/mem_write handlers
In reply to Albert EsteveImplement function handlers for memory read and write operations.
--- hw/virtio/vhost-user.c | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-)
hw/virtio/vhost-user.c +30 -4
@@ -1884,16 +1884,42 @@ static int vhost_user_backend_handle_mem_read(struct vhost_dev *dev, VhostUserMemRWMsg *mem_rw) { - /* TODO */ - return -EPERM; + ram_addr_t offset; +int fd; +MemoryRegion *mr; + + mr =vhost_user_get_mr_data (mem_rw->guest_address, &offset, &fd); + +if (!mr) { +error_report ("Failed to get memory region with address %" PRIx64, + mem_rw->guest_address); +return -EFAULT ; + } + +memcpy (mem_rw->data,memory_region_get_ram_ptr (mr) + offset, mem_rw->size); + +return 0 ; }static int vhost_user_backend_handle_mem_write (struct vhost_dev *dev,VhostUserMemRWMsg *mem_rw) { -/* TODO */ -return -EPERM ; +ram_addr_t offset; +int fd; +MemoryRegion *mr; + + mr =vhost_user_get_mr_data (mem_rw->guest_address, &offset, &fd); + +if (!mr) { +error_report ("Failed to get memory region with address %" PRIx64, + mem_rw->guest_address); +return -EFAULT ; + } + +memcpy (memory_region_get_ram_ptr (mr) + offset, mem_rw->data, mem_rw->size); + +return 0 ; }static void close_backend_channel (struct vhost_user *u)
Signature · 1 line
2.45.2
Re: [RFC PATCH v2 5/5] vhost_user: Implement mem_read/mem_write handlers
Quoted 21 lines · Albert Esteve
Implement function handlers for memory read and write operations.
Signed-off-by Albert Esteve <[email protected]>--- hw/virtio/vhost-user.c | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-)hw/virtio/vhost-user.c +3
-2@@ -1884,16 +1884,42 @@ static int vhost_user_backend_handle_mem_read(struct vhost_dev *dev, VhostUserMemRWMsg *mem_rw) { - /* TODO */ - return -EPERM; + ram_addr_t offset; +int fd; +MemoryRegion *mr;
hw/virtio/vhost-user.c static int · line 1899
Quoted 10 lines
hw/virtio/vhost-user.c +10
-0+ + mr =vhost_user_get_mr_data (mem_rw->guest_address, &offset, &fd); + +if (!mr) { +error_report ("Failed to get memory region with address %" PRIx64, + mem_rw->guest_address); +return -EFAULT ; + } + +memcpy (mem_rw->data,memory_region_get_ram_ptr (mr) + offset, mem_rw->size);
Don't try to write this from scratch. Use address_space_read/write(). It supports corner cases like crossing MemoryRegions.
Quoted 31 lines
hw/virtio/vhost-user.c +17
-2+ +return 0 ; }static int vhost_user_backend_handle_mem_write (struct vhost_dev *dev,VhostUserMemRWMsg *mem_rw) { -/* TODO */ -return -EPERM ; +ram_addr_t offset; +int fd; +MemoryRegion *mr; + + mr =vhost_user_get_mr_data (mem_rw->guest_address, &offset, &fd); + +if (!mr) { +error_report ("Failed to get memory region with address %" PRIx64, + mem_rw->guest_address); +return -EFAULT ; + } + +memcpy (memory_region_get_ram_ptr (mr) + offset, mem_rw->data, mem_rw->size); + +return 0 ; }static void close_backend_channel (struct vhost_user *u)Signature · 1 line
2.45.2
Re: [RFC PATCH v2 5/5] vhost_user: Implement mem_read/mem_write handlers
Quoted 33 lines · Stefan Hajnoczi
Quoted 16 lines · Albert Esteve
Implement function handlers for memory read and write operations.
Signed-off-by Albert Esteve <[email protected]>--- hw/virtio/vhost-user.c | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-)hw/virtio/vhost-user.c +0
-0@@ -1884,16 +1884,42 @@ static int vhost_user_backend_handle_mem_read(struct vhost_dev *dev, VhostUserMemRWMsg *mem_rw) {hw/virtio/vhost-user.c static int · line 1894
Quoted 10 lines
hw/virtio/vhost-user.c +8
-2-/* TODO */ -return -EPERM ; +ram_addr_t offset; +int fd; +MemoryRegion *mr; + + mr =vhost_user_get_mr_data (mem_rw->guest_address, &offset, &fd); + +if (!mr) { + error_report("Failed to get memory region with address %"PRIx64,
hw/virtio/vhost-user.c static int
Quoted 5 lines
hw/virtio/vhost-user.c +5
-0+ mem_rw->guest_address); +return -EFAULT ; + } + + memcpy(mem_rw->data, memory_region_get_ram_ptr(mr) + offset,
Quoted 5 lines
mem_rw->size);Don't try to write this from scratch. Use address_space_read/write(). It supports corner cases like crossing MemoryRegions.
I am having issues getting the address space from the vhost_dev struct to feed
address_spave_read/write() function with the first parameter. But I found mr->ops.
Would something like this perhaps be enough?
```
mr->ops->read_with_attrs(mr->opaque, mem_rw->guest_address,
&mem_rw->data, mem_rw->size,
MEMTXATTRS_UNSPECIFIED);
```Quoted 35 lines
Quoted 9 lines
hw/virtio/vhost-user.c +2
-0+ +return 0 ; }static int vhost_user_backend_handle_mem_write (struct vhost_dev *dev,VhostUserMemRWMsg *mem_rw) {hw/virtio/vhost-user.c static int
Quoted 10 lines
hw/virtio/vhost-user.c +8
-2-/* TODO */ -return -EPERM ; +ram_addr_t offset; +int fd; +MemoryRegion *mr; + + mr =vhost_user_get_mr_data (mem_rw->guest_address, &offset, &fd); + +if (!mr) { + error_report("Failed to get memory region with address %"PRIx64,
hw/virtio/vhost-user.c static int
Quoted 5 lines
hw/virtio/vhost-user.c +5
-0+ mem_rw->guest_address); +return -EFAULT ; + } + + memcpy(memory_region_get_ram_ptr(mr) + offset, mem_rw->data,
mem_rw->size);Quoted 8 lines
hw/virtio/vhost-user.c +2
-1+ +return 0 ; }static void close_backend_channel (struct vhost_user *u) --2.45.2
Re: [RFC PATCH v2 5/5] vhost_user: Implement mem_read/mem_write handlers
Quoted 48 lines · Albert Esteve
Quoted 33 lines · Stefan Hajnoczi
Quoted 16 lines · Albert Esteve
Implement function handlers for memory read and write operations.
Signed-off-by Albert Esteve <[email protected]>--- hw/virtio/vhost-user.c | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-)hw/virtio/vhost-user.c +0
-0@@ -1884,16 +1884,42 @@ static int vhost_user_backend_handle_mem_read(struct vhost_dev *dev, VhostUserMemRWMsg *mem_rw) {hw/virtio/vhost-user.c static int · line 1894
Quoted 10 lines
hw/virtio/vhost-user.c +8
-2-/* TODO */ -return -EPERM ; +ram_addr_t offset; +int fd; +MemoryRegion *mr; + + mr =vhost_user_get_mr_data (mem_rw->guest_address, &offset, &fd); + +if (!mr) { + error_report("Failed to get memory region with address %"PRIx64,
hw/virtio/vhost-user.c static int
Quoted 5 lines
hw/virtio/vhost-user.c +5
-0+ mem_rw->guest_address); +return -EFAULT ; + } + + memcpy(mem_rw->data, memory_region_get_ram_ptr(mr) + offset,Quoted 5 lines
mem_rw->size);Don't try to write this from scratch. Use address_space_read/write(). It supports corner cases like crossing MemoryRegions.
I am having issues getting the address space from the
vhost_devstruct to feed
address_spave_read/write() function with the first parameter. But I foundmr->ops.
Would something like this perhaps be enough?
Quoted 5 lines
```
mr->ops->read_with_attrs(mr->opaque, mem_rw->guest_address,
&mem_rw->data, mem_rw->size,
MEMTXATTRS_UNSPECIFIED);
```You can use dev->vdev->dma_as to get the AddressSpace for address_space_read/write():
struct vhost_dev {
VirtIODevice *vdev;
struct VirtIODevice
{
...
AddressSpace *dma_as;Quoted 37 lines
Quoted 37 lines
Quoted 11 lines
hw/virtio/vhost-user.c +2
-0+ +return 0 ; }static int vhost_user_backend_handle_mem_write (struct vhost_dev *dev,VhostUserMemRWMsg *mem_rw) {hw/virtio/vhost-user.c static int
Quoted 10 lines
hw/virtio/vhost-user.c +8
-2-/* TODO */ -return -EPERM ; +ram_addr_t offset; +int fd; +MemoryRegion *mr; + + mr =vhost_user_get_mr_data (mem_rw->guest_address, &offset, &fd); + +if (!mr) { + error_report("Failed to get memory region with address %"PRIx64,
hw/virtio/vhost-user.c static int
Quoted 5 lines
hw/virtio/vhost-user.c +5
-0+ mem_rw->guest_address); +return -EFAULT ; + } + + memcpy(memory_region_get_ram_ptr(mr) + offset, mem_rw->data,
mem_rw->size);Quoted 8 lines
hw/virtio/vhost-user.c +2
-1+ +return 0 ; }static void close_backend_channel (struct vhost_user *u) --2.45.2
Re: [RFC PATCH v2 5/5] vhost_user: Implement mem_read/mem_write handlers
Quoted 51 lines · Stefan Hajnoczi
Quoted 12 lines · Albert Esteve
Quoted 9 lines · Stefan Hajnoczi
Quoted 8 lines · Albert Esteve
Implement function handlers for memory read and write operations.
Signed-off-by Albert Esteve <[email protected]>--- hw/virtio/vhost-user.c | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-)Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
hw/virtio/vhost-user.c +0
-0@@ -1884,16 +1884,42 @@ static int vhost_user_backend_handle_mem_read(struct vhost_dev *dev, VhostUserMemRWMsg *mem_rw)Quoted 8 lines
hw/virtio/vhost-user.c static int
Quoted 8 lines
hw/virtio/vhost-user.c +5
-2{ -/* TODO */ -return -EPERM ; +ram_addr_t offset; +int fd; +MemoryRegion *mr; + + mr = vhost_user_get_mr_data(mem_rw->guest_address, &offset,&fd);
Quoted 4 lines
Quoted 4 lines
hw/virtio/vhost-user.c static int
Quoted 3 lines
hw/virtio/vhost-user.c +3
-0+ +if (!mr) { + error_report("Failed to get memory region with address %"PRIx64,
Quoted 8 lines
Quoted 8 lines
hw/virtio/vhost-user.c static int
Quoted 5 lines
hw/virtio/vhost-user.c +5
-0+ mem_rw->guest_address); +return -EFAULT ; + } + + memcpy(mem_rw->data, memory_region_get_ram_ptr(mr) + offset,
mem_rw->size);Don't try to write this from scratch. Use address_space_read/write().
It
Quoted 9 lines
Quoted 2 lines
supports corner cases like crossing MemoryRegions.
I am having issues getting the address space from the
vhost_devstruct to feed
address_spave_read/write() function with the first parameter. But I foundmr->ops.
Would something like this perhaps be enough?
Quoted 9 lines
Quoted 5 lines
``` mr->ops->read_with_attrs(mr->opaque, mem_rw->guest_address, &mem_rw->data, mem_rw->size, MEMTXATTRS_UNSPECIFIED); ```You can use
dev->vdev->dma_asto get the AddressSpace for address_space_read/write():
Oof, I see, thanks!
I still struggle a bit with the structs relationships...
Quoted 48 lines
struct vhost_dev { VirtIODevice *vdev; struct VirtIODevice { ... AddressSpace *dma_as;Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
hw/virtio/vhost-user.c +2
-0+ +return 0 ; }Quoted 11 lines
Quoted 11 lines
hw/virtio/vhost-user.c static int
Quoted 11 lines
hw/virtio/vhost-user.c +5
-2static int vhost_user_backend_handle_mem_write (struct vhost_dev *dev,VhostUserMemRWMsg *mem_rw) { -/* TODO */ -return -EPERM ; +ram_addr_t offset; +int fd; +MemoryRegion *mr; + + mr = vhost_user_get_mr_data(mem_rw->guest_address, &offset,&fd);
Quoted 19 lines
Quoted 19 lines
hw/virtio/vhost-user.c static int
Quoted 3 lines
hw/virtio/vhost-user.c +3
-0+ +if (!mr) { + error_report("Failed to get memory region with address %"PRIx64,
hw/virtio/vhost-user.c static int
Quoted 5 lines
hw/virtio/vhost-user.c +5
-0+ mem_rw->guest_address); +return -EFAULT ; + } + + memcpy(memory_region_get_ram_ptr(mr) + offset, mem_rw->data,
mem_rw->size);Quoted 8 lines
hw/virtio/vhost-user.c +2
-1+ +return 0 ; }static void close_backend_channel (struct vhost_user *u) --2.45.2
Quoted 14 lines · Albert Esteve
Hi all,
v1->v2:
- Corrected typos and clarifications from first review
- AddedSHMEM_CONFIGfrontend request to query VIRTIO shared memory regions from backends
- vhost-user-device to useSHMEM_CONFIGto request and initialise regions
- Added MEM_READ/WRITE backend requests in case address translation fails accessing VIRTIO Shared Memory Regions with MMAPs
Hi Albert,
I will be offline next week. I've posted comments.
I think the hard part will be adjusting vhost-user backend code to make use of MEM_READ/MEM_WRITE when address translation fails. Ideally every guest memory access (including vring accesses) should fall back to MEM_READ/MEM_WRITE.
A good test for MEM_READ/MEM_WRITE is to completely skip setting the memory table from the frontend and fall back for every guest memory access. If the vhost-user backend still works without a memory table then you know MEM_READ/MEM_WRITE is working too.
The vhost-user spec should probably contain a comment explaining that MEM_READ/MEM_WRITE may be necessary when other device backends use SHMEM_MAP due to the incomplete memory table that prevents translating those memory addresses. In other words, if the guest has a device that uses SHMEM_MAP, then all other vhost-user devices should support MEM_READ/MEM_WRITE in order to ensure that DMA works with Shared Memory Regions.
Stefan
Quoted 74 lines
This is an update of my attempt to have backends support dynamic fd mapping into VIRTIO Shared Memory Regions. After the first review I have added more commits and new messages to the vhost-user protocol.
However, I still have some doubts as to how will this work, specially regarding theMEM_READandMEM_WRITEcommands. Thus, I am still looking for feedback, to ensure that I am going in the right direction with the implementation.The usecase for this patch is, e.g., to support vhost-user-gpu
RESOURCE_BLOBoperations, or DAX Window request for virtio-fs. In general, any operation where a backend need to request the frontend to mmap an fd into a VIRTIO Shared Memory Region, so that the guest can then access it.After receiving the SHMEM_MAP/UNMAP request, the frontend will perform the mmap with the instructed parameters (i.e., shmid,
shm_offset,fd_offset, fd, lenght).As there are already a couple devices that could benefit of such a feature, and more could require it in the future, the goal is to make the implementation generic.
To that end, the VIRTIO Shared Memory Region list is declared in the
VirtIODevicestruct.This patch also includes:
SHMEM_CONFIGfrontend request that is specifically meant to allow generic vhost-user-device frontend to be able to query VIRTIO Shared Memory settings from the backend (as this device is generic and agnostic of the actual backend configuration).Finally, MEM_READ/WRITE backend requests are added to deal with a potential issue when having any backend sharing a descriptor that references a mapping to another backend. The first backend will not be able to see these mappings. So these requests are a fallback for vhost-user memory translation fails.
Albert Esteve (5):
vhost-user: Add VIRTIO Shared Memory map request
vhost_user: Add frontend command for shmem config
vhost-user-dev: Add cache BAR
vhost_user: Add MEM_READ/WRITE backend requests
vhost_user: Implement mem_read/mem_write handlersdocs/interop/vhost-user.rst | 58 ++++++ hw/virtio/vhost-user-base.c | 39 +++- hw/virtio/vhost-user-device-pci.c | 37 +++- hw/virtio/vhost-user.c | 221 ++++++++++++++++++++++ hw/virtio/virtio.c | 12 ++ include/hw/virtio/vhost-backend.h | 6 + include/hw/virtio/vhost-user.h | 1 + include/hw/virtio/virtio.h | 5 + subprojects/libvhost-user/libvhost-user.c | 149 +++++++++++++++ subprojects/libvhost-user/libvhost-user.h | 91 +++++++++ 10 files changed, 614 insertions(+), 5 deletions(-)Signature · 1 line
2.45.2
Adding David Stevens, who implemented SHMEM_MAP and SHMEM_UNMAP in crosvm a couple of years ago.
David, I'd be particularly interested for your thoughts on the MEM_READ and MEM_WRITE commands, since as far as I know crosvm doesn't implement anything like that. The discussion leading to those being added starts here:
https://lore.kernel.org/qemu-devel/[email protected]/
It would be great if this could be standardised between QEMU and crosvm (and therefore have a clearer path toward being implemented in other VMMs)!
Quoted 87 lines · Albert Esteve
Hi all,
v1->v2:
- Corrected typos and clarifications from first review
- AddedSHMEM_CONFIGfrontend request to query VIRTIO shared memory regions from backends
- vhost-user-device to useSHMEM_CONFIGto request and initialise regions
- Added MEM_READ/WRITE backend requests in case address translation fails accessing VIRTIO Shared Memory Regions with MMAPsThis is an update of my attempt to have backends support dynamic fd mapping into VIRTIO Shared Memory Regions. After the first review I have added more commits and new messages to the vhost-user protocol.
However, I still have some doubts as to how will this work, specially regarding theMEM_READandMEM_WRITEcommands. Thus, I am still looking for feedback, to ensure that I am going in the right direction with the implementation.The usecase for this patch is, e.g., to support vhost-user-gpu
RESOURCE_BLOBoperations, or DAX Window request for virtio-fs. In general, any operation where a backend need to request the frontend to mmap an fd into a VIRTIO Shared Memory Region, so that the guest can then access it.After receiving the SHMEM_MAP/UNMAP request, the frontend will perform the mmap with the instructed parameters (i.e., shmid,
shm_offset,fd_offset, fd, lenght).As there are already a couple devices that could benefit of such a feature, and more could require it in the future, the goal is to make the implementation generic.
To that end, the VIRTIO Shared Memory Region list is declared in the
VirtIODevicestruct.This patch also includes:
SHMEM_CONFIGfrontend request that is specifically meant to allow generic vhost-user-device frontend to be able to query VIRTIO Shared Memory settings from the backend (as this device is generic and agnostic of the actual backend configuration).Finally, MEM_READ/WRITE backend requests are added to deal with a potential issue when having any backend sharing a descriptor that references a mapping to another backend. The first backend will not be able to see these mappings. So these requests are a fallback for vhost-user memory translation fails.
Albert Esteve (5):
vhost-user: Add VIRTIO Shared Memory map request
vhost_user: Add frontend command for shmem config
vhost-user-dev: Add cache BAR
vhost_user: Add MEM_READ/WRITE backend requests
vhost_user: Implement mem_read/mem_write handlersdocs/interop/vhost-user.rst | 58 ++++++ hw/virtio/vhost-user-base.c | 39 +++- hw/virtio/vhost-user-device-pci.c | 37 +++- hw/virtio/vhost-user.c | 221 ++++++++++++++++++++++ hw/virtio/virtio.c | 12 ++ include/hw/virtio/vhost-backend.h | 6 + include/hw/virtio/vhost-user.h | 1 + include/hw/virtio/virtio.h | 5 + subprojects/libvhost-user/libvhost-user.c | 149 +++++++++++++++ subprojects/libvhost-user/libvhost-user.h | 91 +++++++++ 10 files changed, 614 insertions(+), 5 deletions(-)Signature · 1 line
2.45.2
Quoted 9 lines · Alyssa Ross
Adding David Stevens, who implemented
SHMEM_MAPandSHMEM_UNMAPin crosvm a couple of years ago.David, I'd be particularly interested for your thoughts on the
MEM_READandMEM_WRITEcommands, since as far as I know crosvm doesn't implement anything like that. The discussion leading to those being added starts here:
Quoted 4 lines
https://lore.kernel.org/qemu-devel/[email protected]/
It would be great if this could be standardised between QEMU and crosvm (and therefore have a clearer path toward being implemented in other VMMs)!
Setting aside vhost-user for a moment, the DAX example given by Stefan won't work in crosvm today.
Is universal access to virtio shared memory regions actually mandated by the virtio spec? Copying from virtiofs DAX to virtiofs sharing seems reasonable enough, but what about virtio-pmem to virtio-blk? What about screenshotting a framebuffer in virtio-gpu shared memory to virtio-scsi? I guess with some plumbing in the VMM, it's solvable in a virtualized environment. But what about when you have real hardware that speaks virtio involved? That's outside my wheelhouse, but it doesn't seem like that would be easy to solve.
For what it's worth, my interpretation of the target scenario:
Quoted 3 lines
Other backends don't see these mappings. If the guest submits a vring descriptor referencing a mapping to another backend, then that backend won't be able to access this memory
is that it's omitting how the implementation is reconciled with section 2.10.1 of v1.3 of the virtio spec, which states that:
Quoted 4 lines
References into shared memory regions are represented as offsets from the beginning of the region instead of absolute memory addresses. Offsets are used both for references between structures stored within shared memory and for requests placed in virtqueues that refer to shared memory.
My interpretation of that statement is that putting raw guest physical addresses corresponding to virtio shared memory regions into a vring is a driver spec violation.
-David
Quoted 18 lines · David Stevens
Quoted 9 lines · Alyssa Ross
Adding David Stevens, who implemented
SHMEM_MAPandSHMEM_UNMAPin crosvm a couple of years ago.David, I'd be particularly interested for your thoughts on the
MEM_READandMEM_WRITEcommands, since as far as I know crosvm doesn't implement anything like that. The discussion leading to those being added starts here:Quoted 4 lines
https://lore.kernel.org/qemu-devel/[email protected]/
It would be great if this could be standardised between QEMU and crosvm (and therefore have a clearer path toward being implemented in other VMMs)!
Setting aside vhost-user for a moment, the DAX example given by Stefan won't work in crosvm today.
Quoted 8 lines
Is universal access to virtio shared memory regions actually mandated by the virtio spec? Copying from virtiofs DAX to virtiofs sharing seems reasonable enough, but what about virtio-pmem to virtio-blk? What about screenshotting a framebuffer in virtio-gpu shared memory to virtio-scsi? I guess with some plumbing in the VMM, it's solvable in a virtualized environment. But what about when you have real hardware that speaks virtio involved? That's outside my wheelhouse, but it doesn't seem like that would be easy to solve.
Yes, it can work for physical devices if allowed by host configuration. E.g. VFIO supports that I think. Don't think VDPA does.
Quoted 14 lines
For what it's worth, my interpretation of the target scenario:
Quoted 3 lines
Other backends don't see these mappings. If the guest submits a vring descriptor referencing a mapping to another backend, then that backend won't be able to access this memory
is that it's omitting how the implementation is reconciled with section 2.10.1 of v1.3 of the virtio spec, which states that:
Quoted 4 lines
References into shared memory regions are represented as offsets from the beginning of the region instead of absolute memory addresses. Offsets are used both for references between structures stored within shared memory and for requests placed in virtqueues that refer to shared memory.
Quoted 5 lines
My interpretation of that statement is that putting raw guest physical addresses corresponding to virtio shared memory regions into a vring is a driver spec violation.
-David
This really applies within device I think. Should be clarified ...
Signature · 1 line
MST
Quoted 29 lines · Michael S. Tsirkin
Quoted 18 lines · David Stevens
Quoted 9 lines · Alyssa Ross
Adding David Stevens, who implemented
SHMEM_MAPandSHMEM_UNMAPin crosvm a couple of years ago.David, I'd be particularly interested for your thoughts on the
MEM_READandMEM_WRITEcommands, since as far as I know crosvm doesn't implement anything like that. The discussion leading to those being added starts here:Quoted 4 lines
https://lore.kernel.org/qemu-devel/[email protected]/
It would be great if this could be standardised between QEMU and crosvm (and therefore have a clearer path toward being implemented in other VMMs)!
Setting aside vhost-user for a moment, the DAX example given by Stefan won't work in crosvm today.
Quoted 8 lines
Is universal access to virtio shared memory regions actually mandated by the virtio spec? Copying from virtiofs DAX to virtiofs sharing seems reasonable enough, but what about virtio-pmem to virtio-blk? What about screenshotting a framebuffer in virtio-gpu shared memory to virtio-scsi? I guess with some plumbing in the VMM, it's solvable in a virtualized environment. But what about when you have real hardware that speaks virtio involved? That's outside my wheelhouse, but it doesn't seem like that would be easy to solve.
Quoted 3 lines
Yes, it can work for physical devices if allowed by host configuration. E.g. VFIO supports that I think. Don't think VDPA does.
I guess you meant iommufd support here?
Thanks
Quoted 29 lines · Michael S. Tsirkin
Quoted 18 lines · David Stevens
Quoted 9 lines · Alyssa Ross
Adding David Stevens, who implemented
SHMEM_MAPandSHMEM_UNMAPin crosvm a couple of years ago.David, I'd be particularly interested for your thoughts on the
MEM_READandMEM_WRITEcommands, since as far as I know crosvm doesn't implement anything like that. The discussion leading to those being added starts here:Quoted 4 lines
https://lore.kernel.org/qemu-devel/[email protected]/
It would be great if this could be standardised between QEMU and crosvm (and therefore have a clearer path toward being implemented in other VMMs)!
Setting aside vhost-user for a moment, the DAX example given by Stefan won't work in crosvm today.
Quoted 8 lines
Is universal access to virtio shared memory regions actually mandated by the virtio spec? Copying from virtiofs DAX to virtiofs sharing seems reasonable enough, but what about virtio-pmem to virtio-blk? What about screenshotting a framebuffer in virtio-gpu shared memory to virtio-scsi? I guess with some plumbing in the VMM, it's solvable in a virtualized environment. But what about when you have real hardware that speaks virtio involved? That's outside my wheelhouse, but it doesn't seem like that would be easy to solve.
Quoted 2 lines
Yes, it can work for physical devices if allowed by host configuration. E.g. VFIO supports that I think. Don't think VDPA does.
I'm sure it can work, but that sounds more like a SHOULD (MAY?), rather than a MUST.
Quoted 14 lines
Quoted 14 lines
For what it's worth, my interpretation of the target scenario:
Quoted 3 lines
Other backends don't see these mappings. If the guest submits a vring descriptor referencing a mapping to another backend, then that backend won't be able to access this memory
is that it's omitting how the implementation is reconciled with section 2.10.1 of v1.3 of the virtio spec, which states that:
Quoted 4 lines
References into shared memory regions are represented as offsets from the beginning of the region instead of absolute memory addresses. Offsets are used both for references between structures stored within shared memory and for requests placed in virtqueues that refer to shared memory.
Quoted 7 lines
Quoted 5 lines
My interpretation of that statement is that putting raw guest physical addresses corresponding to virtio shared memory regions into a vring is a driver spec violation.
-David
This really applies within device I think. Should be clarified ...
You mean that a virtio device can use absolute memory addresses for other devices' shared memory regions, but it can't use absolute memory addresses for its own shared memory regions? That's a rather strange requirement. Or is the statement simply giving an addressing strategy that device type specifications are free to ignore?
-David
Hello all,
Sorry, I have been a bit disconnected from this thread as I was on vacations and then had to switch tasks for a while.
I will try to go through all comments and address them for the first non-RFC drop of this patch series.
But I was discussing with some colleagues on this. So turns out rust-vmm's vhost-user-gpu will potentially use
this soon, and a rust-vmm/vhost patch have been already posted:
https://github.com/rust-vmm/vhost/pull/251.
So I think it may make sense to:
1. Split the vhost-user documentation patch once settled. Since it is taken as the official spec,
having it upstreamed independently of the implementation will benefit
other projects to
work/integrate their own code.
2. Split READ_/WRITE_MEM messages from SHMEM_MAP/_UNMAP patches. If I remember correctly, this addresses a virtio-fs specific issue,
that will not
impact either virtio-gpu nor virtio-media, or any other. So it may make
sense
to separate them so that one does not stall the other. I will try to
have both
integrated in the mid term.
WDYT?
BR,
Albert.
Quoted 76 lines · David Stevens
Quoted 8 lines · Michael S. Tsirkin
Quoted 6 lines · David Stevens
Quoted 5 lines · Alyssa Ross
Adding David Stevens, who implemented
SHMEM_MAPandSHMEM_UNMAPin crosvm a couple of years ago.David, I'd be particularly interested for your thoughts on the
MEM_READQuoted 1 line
Quoted 1 line
Quoted 1 line
and
MEM_WRITEcommands, since as far as I know crosvm doesn'timplement
Quoted 1 line
Quoted 1 line
Quoted 1 line
anything like that. The discussion leading to those being added
starts
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
here:
https://lore.kernel.org/qemu-devel/[email protected]/
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
It would be great if this could be standardised between QEMU and
crosvm
Quoted 1 line
Quoted 1 line
Quoted 1 line
(and therefore have a clearer path toward being implemented in other
VMMs)!
Quoted 13 lines
Quoted 4 lines
Setting aside vhost-user for a moment, the DAX example given by Stefan won't work in crosvm today.
Quoted 8 lines
Is universal access to virtio shared memory regions actually mandated by the virtio spec? Copying from virtiofs DAX to virtiofs sharing seems reasonable enough, but what about virtio-pmem to virtio-blk? What about screenshotting a framebuffer in virtio-gpu shared memory to virtio-scsi? I guess with some plumbing in the VMM, it's solvable in a virtualized environment. But what about when you have real hardware that speaks virtio involved? That's outside my wheelhouse, but it doesn't seem like that would be easy to solve.
Quoted 2 lines
Yes, it can work for physical devices if allowed by host configuration. E.g. VFIO supports that I think. Don't think VDPA does.
I'm sure it can work, but that sounds more like a SHOULD (MAY?), rather than a MUST.
Quoted 4 lines
Quoted 4 lines
For what it's worth, my interpretation of the target scenario:
Quoted 2 lines
Other backends don't see these mappings. If the guest submits a vring descriptor referencing a mapping to another backend, then that
backend
Quoted 7 lines
Quoted 7 lines
Quoted 1 line
won't be able to access this memory
is that it's omitting how the implementation is reconciled with section 2.10.1 of v1.3 of the virtio spec, which states that:
Quoted 2 lines
References into shared memory regions are represented as offsets from the beginning of the region instead of absolute memory addresses.
Offsets
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
are used both for references between structures stored within shared memory and for requests placed in virtqueues that refer to shared
memory.
Quoted 8 lines
Quoted 6 lines
My interpretation of that statement is that putting raw guest physical addresses corresponding to virtio shared memory regions into a vring is a driver spec violation.
-David
This really applies within device I think. Should be clarified ...
You mean that a virtio device can use absolute memory addresses for other devices' shared memory regions, but it can't use absolute memory addresses for its own shared memory regions? That's a rather strange requirement. Or is the statement simply giving an addressing strategy that device type specifications are free to ignore?
-David
Quoted 8 lines · Albert Esteve
Hello all,
Sorry, I have been a bit disconnected from this thread as I was on vacations and then had to switch tasks for a while.
I will try to go through all comments and address them for the first non-RFC drop of this patch series.
Quoted 14 lines
But I was discussing with some colleagues on this. So turns out rust-vmm's vhost-user-gpu will potentially use
this soon, and a rust-vmm/vhost patch have been already posted:
https://github.com/rust-vmm/vhost/pull/251.
So I think it may make sense to:
1. Split the vhost-user documentation patch once settled. Since it is taken as the official spec,
having it upstreamed independently of the implementation will benefit
other projects to
work/integrate their own code.
2. Split READ_/WRITE_MEM messages from SHMEM_MAP/_UNMAP patches. If I remember correctly, this addresses a virtio-fs specific issue,
that will not
impact either virtio-gpu nor virtio-media, or any other.
This is an architectural issue that arises from exposing VIRTIO Shared Memory Regions in vhost-user. It was first seen with Linux virtiofs but it could happen with other devices and/or guest operating systems.
Any VIRTIO Shared Memory Region that can be mmapped into Linux userspace may trigger this issue. Userspace may write(2) to an O_DIRECT file with the mmap as the source. The vhost-user-blk device will not be able to access the source device's VIRTIO Shared Memory Region and will fail.
Quoted 5 lines
So it may make
sense
to separate them so that one does not stall the other. I will try to
have both
integrated in the mid term.
If READ_/WRITE_MEM is a pain to implement (I think it is in the vhost-user back-end, even though I've been a proponent of it), then another way to deal with this issue is to specify that upon receiving MAP/UNMAP messages, the vhost-user front-end must update the vhost-user memory tables of all other vhost-user devices. That way vhost-user devices will be able to access VIRTIO Shared Memory Regions mapped by other devices.
Implementing this in QEMU should be much easier than implementing READ_/WRITE_MEM support in device back-ends.
This will be slow and scale poorly but performance is only a problem for devices that frequently MAP/UNMAP like virtiofs. Will virtio-gpu and virtio-media use MAP/UNMAP often at runtime? They might be able to get away with this simple solution.
I'd be happy with that. If someone wants to make virtiofs DAX faster, they can implement READ/WRITE_MEM or another solution later, but let's at least make things correct from the start.
Stefan
Quoted 84 lines
WDYT?
BR,
Albert.Quoted 76 lines · David Stevens
Quoted 8 lines · Michael S. Tsirkin
Quoted 6 lines · David Stevens
Quoted 5 lines · Alyssa Ross
Adding David Stevens, who implemented
SHMEM_MAPandSHMEM_UNMAPin crosvm a couple of years ago.David, I'd be particularly interested for your thoughts on the
MEM_READQuoted 1 line
Quoted 1 line
Quoted 1 line
and
MEM_WRITEcommands, since as far as I know crosvm doesn'timplement
Quoted 1 line
Quoted 1 line
Quoted 1 line
anything like that. The discussion leading to those being added
starts
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
here:
https://lore.kernel.org/qemu-devel/[email protected]/
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
It would be great if this could be standardised between QEMU and
crosvm
Quoted 1 line
Quoted 1 line
Quoted 1 line
(and therefore have a clearer path toward being implemented in other
VMMs)!
Quoted 13 lines
Quoted 4 lines
Setting aside vhost-user for a moment, the DAX example given by Stefan won't work in crosvm today.
Quoted 8 lines
Is universal access to virtio shared memory regions actually mandated by the virtio spec? Copying from virtiofs DAX to virtiofs sharing seems reasonable enough, but what about virtio-pmem to virtio-blk? What about screenshotting a framebuffer in virtio-gpu shared memory to virtio-scsi? I guess with some plumbing in the VMM, it's solvable in a virtualized environment. But what about when you have real hardware that speaks virtio involved? That's outside my wheelhouse, but it doesn't seem like that would be easy to solve.
Quoted 2 lines
Yes, it can work for physical devices if allowed by host configuration. E.g. VFIO supports that I think. Don't think VDPA does.
I'm sure it can work, but that sounds more like a SHOULD (MAY?), rather than a MUST.
Quoted 4 lines
Quoted 4 lines
For what it's worth, my interpretation of the target scenario:
Quoted 2 lines
Other backends don't see these mappings. If the guest submits a vring descriptor referencing a mapping to another backend, then that
backend
Quoted 7 lines
Quoted 7 lines
Quoted 1 line
won't be able to access this memory
is that it's omitting how the implementation is reconciled with section 2.10.1 of v1.3 of the virtio spec, which states that:
Quoted 2 lines
References into shared memory regions are represented as offsets from the beginning of the region instead of absolute memory addresses.
Offsets
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
are used both for references between structures stored within shared memory and for requests placed in virtqueues that refer to shared
memory.
Quoted 8 lines
Quoted 6 lines
My interpretation of that statement is that putting raw guest physical addresses corresponding to virtio shared memory regions into a vring is a driver spec violation.
-David
This really applies within device I think. Should be clarified ...
You mean that a virtio device can use absolute memory addresses for other devices' shared memory regions, but it can't use absolute memory addresses for its own shared memory regions? That's a rather strange requirement. Or is the statement simply giving an addressing strategy that device type specifications are free to ignore?
-David
Quoted 52 lines · Stefan Hajnoczi
Quoted 9 lines · Albert Esteve
Hello all,
Sorry, I have been a bit disconnected from this thread as I was on vacations and then had to switch tasks for a while.
I will try to go through all comments and address them for the first non-RFC drop of this patch series.
But I was discussing with some colleagues on this. So turns out
rust-vmm's
Quoted 5 lines
vhost-user-gpu will potentially use
this soon, and a rust-vmm/vhost patch have been already posted:
https://github.com/rust-vmm/vhost/pull/251.
So I think it may make sense to:
1. Split the vhost-user documentation patch once settled. Since it istaken
Quoted 8 lines
as the official spec,
having it upstreamed independently of the implementation will benefit
other projects to
work/integrate their own code.
2. Split READ_/WRITE_MEM messages from SHMEM_MAP/_UNMAP patches. If I remember correctly, this addresses a virtio-fs specific issue,
that will not
impact either virtio-gpu nor virtio-media, or any other.This is an architectural issue that arises from exposing VIRTIO Shared Memory Regions in vhost-user. It was first seen with Linux virtiofs but it could happen with other devices and/or guest operating systems.
Any VIRTIO Shared Memory Region that can be mmapped into Linux userspace may trigger this issue. Userspace may write(2) to an
O_DIRECTfile with the mmap as the source. The vhost-user-blk device will not be able to access the source device's VIRTIO Shared Memory Region and will fail.Quoted 5 lines
So it may make
sense
to separate them so that one does not stall the other. I will try to
have both
integrated in the mid term.If READ_/WRITE_MEM is a pain to implement (I think it is in the vhost-user back-end, even though I've been a proponent of it), then another way to deal with this issue is to specify that upon receiving MAP/UNMAP messages, the vhost-user front-end must update the vhost-user memory tables of all other vhost-user devices. That way vhost-user devices will be able to access VIRTIO Shared Memory Regions mapped by other devices.
Implementing this in QEMU should be much easier than implementing READ_/WRITE_MEM support in device back-ends.
Quoted 9 lines
This will be slow and scale poorly but performance is only a problem for devices that frequently MAP/UNMAP like virtiofs. Will virtio-gpu and virtio-media use MAP/UNMAP often at runtime? They might be able to get away with this simple solution.
I'd be happy with that. If someone wants to make virtiofs DAX faster, they can implement READ/WRITE_MEM or another solution later, but let's at least make things correct from the start.
I agree. I want it to be correct first. If you agree on splitting the spec bits from this
patch I'm already happy. I suggested splitting READ_/WRITE_MEM messages because I thought that it was a virtiofs-specific issue.
The alternative that you proposed is interesting. I'll take it into account. But I
feel I prefer to go for the better solution, and if I get too entangled, then switch
to the easier implementation.
I think we could do this in 2 patches:
1. Split the documentation bits for SHMEM_MAP/_UNMAP. The implementation for these messages will go into the second patch.
2. The implementation patch: keep going for the time being with READ_/WRITE_MEM support. And the documentation for that is kept it within this patch. This way if we switch to the frontend updating vhost-user memory table, we weren't set in any specific solution if patch 1 has been already merged.
BR,
Albert.
Quoted 103 lines
Stefan
Quoted 7 lines
WDYT?
BR,
Albert.On Tue, Jul 16, 2024 at 3:21 AM David Stevens <[email protected]>
Quoted 2 lines · wrote:
Quoted 2 lines
On Fri, Jul 12, 2024 at 2:47 PM Michael S. Tsirkin <[email protected]>
Quoted 5 lines · wrote:
Quoted 5 lines
Quoted 5 lines · David Stevens
Quoted 3 lines · Alyssa Ross
Quoted 2 lines
Adding David Stevens, who implemented
SHMEM_MAPandSHMEM_UNMAPin
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
crosvm a couple of years ago.
David, I'd be particularly interested for your thoughts on the
MEM_READQuoted 8 lines
Quoted 8 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
and
MEM_WRITEcommands, since as far as I know crosvm doesn'timplement
Quoted 1 line
Quoted 1 line
Quoted 1 line
anything like that. The discussion leading to those being added
starts
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
here:
https://lore.kernel.org/qemu-devel/[email protected]/
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
It would be great if this could be standardised between QEMU and
crosvm
Quoted 1 line
Quoted 1 line
Quoted 1 line
(and therefore have a clearer path toward being implemented in
other
Quoted 3 lines
Quoted 3 lines
VMMs)!
Quoted 2 lines
Quoted 2 lines
Setting aside vhost-user for a moment, the DAX example given by
Stefan
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
won't work in crosvm today.
Is universal access to virtio shared memory regions actually
mandated
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
by the virtio spec? Copying from virtiofs DAX to virtiofs sharing seems reasonable enough, but what about virtio-pmem to virtio-blk? What about screenshotting a framebuffer in virtio-gpu shared
memory to
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
virtio-scsi? I guess with some plumbing in the VMM, it's solvable
in a
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
virtualized environment. But what about when you have real hardware that speaks virtio involved? That's outside my wheelhouse, but it doesn't seem like that would be easy to solve.
Yes, it can work for physical devices if allowed by host
configuration.
Quoted 8 lines
Quoted 8 lines
Quoted 1 line
E.g. VFIO supports that I think. Don't think VDPA does.
I'm sure it can work, but that sounds more like a SHOULD (MAY?), rather than a MUST.
Quoted 3 lines
Quoted 3 lines
For what it's worth, my interpretation of the target scenario:
Quoted 1 line
Other backends don't see these mappings. If the guest submits a
vring
Quoted 8 lines
Quoted 8 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
descriptor referencing a mapping to another backend, then that
backend
Quoted 6 lines
Quoted 6 lines
Quoted 1 line
won't be able to access this memory
is that it's omitting how the implementation is reconciled with section 2.10.1 of v1.3 of the virtio spec, which states that:
Quoted 1 line
References into shared memory regions are represented as offsets
from
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
the beginning of the region instead of absolute memory addresses.
Offsets
Quoted 1 line
Quoted 1 line
Quoted 1 line
are used both for references between structures stored within
shared
Quoted 4 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
memory and for requests placed in virtqueues that refer to shared
memory.
Quoted 2 lines
Quoted 2 lines
My interpretation of that statement is that putting raw guest
physical
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
addresses corresponding to virtio shared memory regions into a
vring
Quoted 15 lines
Quoted 15 lines
Quoted 5 lines
Quoted 3 lines
is a driver spec violation.
-David
This really applies within device I think. Should be clarified ...
You mean that a virtio device can use absolute memory addresses for other devices' shared memory regions, but it can't use absolute memory addresses for its own shared memory regions? That's a rather strange requirement. Or is the statement simply giving an addressing strategy that device type specifications are free to ignore?
-David
Quoted 62 lines · Albert Esteve
Quoted 51 lines · Stefan Hajnoczi
Quoted 8 lines · Albert Esteve
Hello all,
Sorry, I have been a bit disconnected from this thread as I was on vacations and then had to switch tasks for a while.
I will try to go through all comments and address them for the first non-RFC drop of this patch series.
Quoted 14 lines
But I was discussing with some colleagues on this. So turns out rust-vmm's vhost-user-gpu will potentially use
this soon, and a rust-vmm/vhost patch have been already posted:
https://github.com/rust-vmm/vhost/pull/251.
So I think it may make sense to:
1. Split the vhost-user documentation patch once settled. Since it is taken as the official spec,
having it upstreamed independently of the implementation will benefit
other projects to
work/integrate their own code.
2. Split READ_/WRITE_MEM messages from SHMEM_MAP/_UNMAP patches. If I remember correctly, this addresses a virtio-fs specific issue,
that will not
impact either virtio-gpu nor virtio-media, or any other.This is an architectural issue that arises from exposing VIRTIO Shared Memory Regions in vhost-user. It was first seen with Linux virtiofs but it could happen with other devices and/or guest operating systems.
Any VIRTIO Shared Memory Region that can be mmapped into Linux userspace may trigger this issue. Userspace may write(2) to an
O_DIRECTfile with the mmap as the source. The vhost-user-blk device will not be able to access the source device's VIRTIO Shared Memory Region and will fail.Quoted 5 lines
So it may make
sense
to separate them so that one does not stall the other. I will try to
have both
integrated in the mid term.If READ_/WRITE_MEM is a pain to implement (I think it is in the vhost-user back-end, even though I've been a proponent of it), then another way to deal with this issue is to specify that upon receiving MAP/UNMAP messages, the vhost-user front-end must update the vhost-user memory tables of all other vhost-user devices. That way vhost-user devices will be able to access VIRTIO Shared Memory Regions mapped by other devices.
Implementing this in QEMU should be much easier than implementing READ_/WRITE_MEM support in device back-ends.
Quoted 8 lines
This will be slow and scale poorly but performance is only a problem for devices that frequently MAP/UNMAP like virtiofs. Will virtio-gpu and virtio-media use MAP/UNMAP often at runtime? They might be able to get away with this simple solution.
I'd be happy with that. If someone wants to make virtiofs DAX faster, they can implement READ/WRITE_MEM or another solution later, but let's at least make things correct from the start.
Quoted 7 lines
I agree. I want it to be correct first. If you agree on splitting the spec bits from this patch I'm already happy. I suggested splitting READ_/WRITE_MEM messages because I thought that it was a virtiofs-specific issue.
The alternative that you proposed is interesting. I'll take it into account. But I feel I prefer to go for the better solution, and if I get too entangled, then switch to the easier implementation.
Great. The difficult part to implementing READ_/WRITE_MEM messages is modifying libvhost-user and rust-vmm's vhost crate to send the new messages when address translation fails. This needs to cover all memory accesses (including vring struct accesses). That code may be a few levels down in the call stack and assume it can always load/store directly from mmapped memory.
Quoted 9 lines
I think we could do this in 2 patches:
1. Split the documentation bits for SHMEM_MAP/_UNMAP. The implementation for these messages will go into the second patch.
2. The implementation patch: keep going for the time being with READ_/WRITE_MEM support. And the documentation for that is kept it within this patch. This way if we switch to the frontend updating vhost-user memory table, we weren't set in any specific solution if patch 1 has been already merged.
I'm happy as long as the vhost-user spec patch that introduces MAP/UNMAP also covers a solution for the memory access problem (either READ_/WRITE_MEM or propagating mappings to all vhost-user back-ends).
Stefan
Quoted 92 lines
BR,
Albert.Quoted 88 lines
Stefan
Quoted 84 lines
WDYT?
BR,
Albert.Quoted 76 lines · David Stevens
Quoted 8 lines · Michael S. Tsirkin
Quoted 6 lines · David Stevens
Quoted 5 lines · Alyssa Ross
Adding David Stevens, who implemented
SHMEM_MAPandSHMEM_UNMAPin crosvm a couple of years ago.David, I'd be particularly interested for your thoughts on the
MEM_READQuoted 1 line
Quoted 1 line
Quoted 1 line
and
MEM_WRITEcommands, since as far as I know crosvm doesn'timplement
Quoted 1 line
Quoted 1 line
Quoted 1 line
anything like that. The discussion leading to those being added
starts
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
here:
https://lore.kernel.org/qemu-devel/[email protected]/
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
It would be great if this could be standardised between QEMU and
crosvm
Quoted 1 line
Quoted 1 line
Quoted 1 line
(and therefore have a clearer path toward being implemented in other
VMMs)!
Quoted 13 lines
Quoted 4 lines
Setting aside vhost-user for a moment, the DAX example given by Stefan won't work in crosvm today.
Quoted 8 lines
Is universal access to virtio shared memory regions actually mandated by the virtio spec? Copying from virtiofs DAX to virtiofs sharing seems reasonable enough, but what about virtio-pmem to virtio-blk? What about screenshotting a framebuffer in virtio-gpu shared memory to virtio-scsi? I guess with some plumbing in the VMM, it's solvable in a virtualized environment. But what about when you have real hardware that speaks virtio involved? That's outside my wheelhouse, but it doesn't seem like that would be easy to solve.
Quoted 2 lines
Yes, it can work for physical devices if allowed by host configuration. E.g. VFIO supports that I think. Don't think VDPA does.
I'm sure it can work, but that sounds more like a SHOULD (MAY?), rather than a MUST.
Quoted 4 lines
Quoted 4 lines
For what it's worth, my interpretation of the target scenario:
Quoted 2 lines
Other backends don't see these mappings. If the guest submits a vring descriptor referencing a mapping to another backend, then that
backend
Quoted 7 lines
Quoted 7 lines
Quoted 1 line
won't be able to access this memory
is that it's omitting how the implementation is reconciled with section 2.10.1 of v1.3 of the virtio spec, which states that:
Quoted 2 lines
References into shared memory regions are represented as offsets from the beginning of the region instead of absolute memory addresses.
Offsets
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
are used both for references between structures stored within shared memory and for requests placed in virtqueues that refer to shared
memory.
Quoted 8 lines
Quoted 6 lines
My interpretation of that statement is that putting raw guest physical addresses corresponding to virtio shared memory regions into a vring is a driver spec violation.
-David
This really applies within device I think. Should be clarified ...
You mean that a virtio device can use absolute memory addresses for other devices' shared memory regions, but it can't use absolute memory addresses for its own shared memory regions? That's a rather strange requirement. Or is the statement simply giving an addressing strategy that device type specifications are free to ignore?
-David
Quoted 56 lines · David Stevens
Quoted 29 lines · Michael S. Tsirkin
Quoted 18 lines · David Stevens
Quoted 9 lines · Alyssa Ross
Adding David Stevens, who implemented
SHMEM_MAPandSHMEM_UNMAPin crosvm a couple of years ago.David, I'd be particularly interested for your thoughts on the
MEM_READandMEM_WRITEcommands, since as far as I know crosvm doesn't implement anything like that. The discussion leading to those being added starts here:Quoted 4 lines
https://lore.kernel.org/qemu-devel/[email protected]/
It would be great if this could be standardised between QEMU and crosvm (and therefore have a clearer path toward being implemented in other VMMs)!
Setting aside vhost-user for a moment, the DAX example given by Stefan won't work in crosvm today.
Quoted 8 lines
Is universal access to virtio shared memory regions actually mandated by the virtio spec? Copying from virtiofs DAX to virtiofs sharing seems reasonable enough, but what about virtio-pmem to virtio-blk? What about screenshotting a framebuffer in virtio-gpu shared memory to virtio-scsi? I guess with some plumbing in the VMM, it's solvable in a virtualized environment. But what about when you have real hardware that speaks virtio involved? That's outside my wheelhouse, but it doesn't seem like that would be easy to solve.
Quoted 2 lines
Yes, it can work for physical devices if allowed by host configuration. E.g. VFIO supports that I think. Don't think VDPA does.
I'm sure it can work, but that sounds more like a SHOULD (MAY?), rather than a MUST.
Quoted 20 lines
Quoted 19 lines
For what it's worth, my interpretation of the target scenario:
Quoted 3 lines
Other backends don't see these mappings. If the guest submits a vring descriptor referencing a mapping to another backend, then that backend won't be able to access this memory
is that it's omitting how the implementation is reconciled with section 2.10.1 of v1.3 of the virtio spec, which states that:
Quoted 4 lines
References into shared memory regions are represented as offsets from the beginning of the region instead of absolute memory addresses. Offsets are used both for references between structures stored within shared memory and for requests placed in virtqueues that refer to shared memory.
My interpretation of that statement is that putting raw guest physical addresses corresponding to virtio shared memory regions into a vring is a driver spec violation.
-David
Quoted 7 lines
Quoted 1 line
This really applies within device I think. Should be clarified ...
You mean that a virtio device can use absolute memory addresses for other devices' shared memory regions, but it can't use absolute memory addresses for its own shared memory regions? That's a rather strange requirement. Or is the statement simply giving an addressing strategy that device type specifications are free to ignore?
My recollection of the intent behind the quoted section is:
1. Structures in shared memory that point to shared memory must used relative offsets instead of absolute physical addresses.
2. Virtqueue requests that refer to shared memory (e.g. map this page from virtiofs file to this location in shared memory) must use relative offsets instead of absolute physical addresses.
In other words, shared memory must be relocatable. Don't assume Shared Memory Regions have an absolute guest physical address. This makes device implementations independent of the guest physical memory layout and might also help when Shared Memory Regions are exposed to guest user-space where the guest physical memory layout isn't known.
Stefan
Quoted 74 lines · Stefan Hajnoczi
Quoted 56 lines · David Stevens
Quoted 29 lines · Michael S. Tsirkin
Quoted 18 lines · David Stevens
Quoted 9 lines · Alyssa Ross
Adding David Stevens, who implemented
SHMEM_MAPandSHMEM_UNMAPin crosvm a couple of years ago.David, I'd be particularly interested for your thoughts on the
MEM_READandMEM_WRITEcommands, since as far as I know crosvm doesn't implement anything like that. The discussion leading to those being added starts here:Quoted 4 lines
https://lore.kernel.org/qemu-devel/[email protected]/
It would be great if this could be standardised between QEMU and crosvm (and therefore have a clearer path toward being implemented in other VMMs)!
Setting aside vhost-user for a moment, the DAX example given by Stefan won't work in crosvm today.
Quoted 8 lines
Is universal access to virtio shared memory regions actually mandated by the virtio spec? Copying from virtiofs DAX to virtiofs sharing seems reasonable enough, but what about virtio-pmem to virtio-blk? What about screenshotting a framebuffer in virtio-gpu shared memory to virtio-scsi? I guess with some plumbing in the VMM, it's solvable in a virtualized environment. But what about when you have real hardware that speaks virtio involved? That's outside my wheelhouse, but it doesn't seem like that would be easy to solve.
Quoted 2 lines
Yes, it can work for physical devices if allowed by host configuration. E.g. VFIO supports that I think. Don't think VDPA does.
I'm sure it can work, but that sounds more like a SHOULD (MAY?), rather than a MUST.
Quoted 20 lines
Quoted 19 lines
For what it's worth, my interpretation of the target scenario:
Quoted 3 lines
Other backends don't see these mappings. If the guest submits a vring descriptor referencing a mapping to another backend, then that backend won't be able to access this memory
is that it's omitting how the implementation is reconciled with section 2.10.1 of v1.3 of the virtio spec, which states that:
Quoted 4 lines
References into shared memory regions are represented as offsets from the beginning of the region instead of absolute memory addresses. Offsets are used both for references between structures stored within shared memory and for requests placed in virtqueues that refer to shared memory.
My interpretation of that statement is that putting raw guest physical addresses corresponding to virtio shared memory regions into a vring is a driver spec violation.
-David
Quoted 7 lines
Quoted 1 line
This really applies within device I think. Should be clarified ...
You mean that a virtio device can use absolute memory addresses for other devices' shared memory regions, but it can't use absolute memory addresses for its own shared memory regions? That's a rather strange requirement. Or is the statement simply giving an addressing strategy that device type specifications are free to ignore?
My recollection of the intent behind the quoted section is:
1. Structures in shared memory that point to shared memory must used relative offsets instead of absolute physical addresses.
2. Virtqueue requests that refer to shared memory (e.g. map this page from virtiofs file to this location in shared memory) must use relative offsets instead of absolute physical addresses.
Quoted 5 lines
In other words, shared memory must be relocatable. Don't assume Shared Memory Regions have an absolute guest physical address. This makes device implementations independent of the guest physical memory layout and might also help when Shared Memory Regions are exposed to guest user-space where the guest physical memory layout isn't known.
Doesn't this discussion contradict the necessity of point 1? If I'm understanding things correctly, it is valid for virtio device A to refer to a structure in virtio device B's shared memory region by absolute guest physical address. At least there is nothing in the spec about resolving shmid values among different virtio devices, so absolute guest physical addresses is the only way this sharing can be done. And if it's valid for a pointer to a structure in a shared memory region to exist, it's not clear to me why you can't have pointers within a shared memory region.
It definitely makes sense that setting up a mapping should be done with offsets. But unless a shared memory region can be dynamically reallocated at runtime, then it doesn't seem necessary to ban pointers within a shared memory region.
-David
Quoted 82 lines · David Stevens
Quoted 74 lines · Stefan Hajnoczi
Quoted 56 lines · David Stevens
Quoted 29 lines · Michael S. Tsirkin
Quoted 18 lines · David Stevens
Quoted 9 lines · Alyssa Ross
Adding David Stevens, who implemented
SHMEM_MAPandSHMEM_UNMAPin crosvm a couple of years ago.David, I'd be particularly interested for your thoughts on the
MEM_READandMEM_WRITEcommands, since as far as I know crosvm doesn't implement anything like that. The discussion leading to those being added starts here:Quoted 4 lines
https://lore.kernel.org/qemu-devel/[email protected]/
It would be great if this could be standardised between QEMU and crosvm (and therefore have a clearer path toward being implemented in other VMMs)!
Setting aside vhost-user for a moment, the DAX example given by Stefan won't work in crosvm today.
Quoted 8 lines
Is universal access to virtio shared memory regions actually mandated by the virtio spec? Copying from virtiofs DAX to virtiofs sharing seems reasonable enough, but what about virtio-pmem to virtio-blk? What about screenshotting a framebuffer in virtio-gpu shared memory to virtio-scsi? I guess with some plumbing in the VMM, it's solvable in a virtualized environment. But what about when you have real hardware that speaks virtio involved? That's outside my wheelhouse, but it doesn't seem like that would be easy to solve.
Quoted 2 lines
Yes, it can work for physical devices if allowed by host configuration. E.g. VFIO supports that I think. Don't think VDPA does.
I'm sure it can work, but that sounds more like a SHOULD (MAY?), rather than a MUST.
Quoted 20 lines
Quoted 19 lines
For what it's worth, my interpretation of the target scenario:
Quoted 3 lines
Other backends don't see these mappings. If the guest submits a vring descriptor referencing a mapping to another backend, then that backend won't be able to access this memory
is that it's omitting how the implementation is reconciled with section 2.10.1 of v1.3 of the virtio spec, which states that:
Quoted 4 lines
References into shared memory regions are represented as offsets from the beginning of the region instead of absolute memory addresses. Offsets are used both for references between structures stored within shared memory and for requests placed in virtqueues that refer to shared memory.
My interpretation of that statement is that putting raw guest physical addresses corresponding to virtio shared memory regions into a vring is a driver spec violation.
-David
Quoted 7 lines
Quoted 1 line
This really applies within device I think. Should be clarified ...
You mean that a virtio device can use absolute memory addresses for other devices' shared memory regions, but it can't use absolute memory addresses for its own shared memory regions? That's a rather strange requirement. Or is the statement simply giving an addressing strategy that device type specifications are free to ignore?
My recollection of the intent behind the quoted section is:
1. Structures in shared memory that point to shared memory must used relative offsets instead of absolute physical addresses.
2. Virtqueue requests that refer to shared memory (e.g. map this page from virtiofs file to this location in shared memory) must use relative offsets instead of absolute physical addresses.Quoted 5 lines
In other words, shared memory must be relocatable. Don't assume Shared Memory Regions have an absolute guest physical address. This makes device implementations independent of the guest physical memory layout and might also help when Shared Memory Regions are exposed to guest user-space where the guest physical memory layout isn't known.
Quoted 9 lines
Doesn't this discussion contradict the necessity of point 1? If I'm understanding things correctly, it is valid for virtio device A to refer to a structure in virtio device B's shared memory region by absolute guest physical address. At least there is nothing in the spec about resolving shmid values among different virtio devices, so absolute guest physical addresses is the only way this sharing can be done. And if it's valid for a pointer to a structure in a shared memory region to exist, it's not clear to me why you can't have pointers within a shared memory region.
The reason is that VIRTIO has a layered design where the transport and vring layout deal with bus addresses but device types generally do not (except for specific exceptions like memory ballooning, etc).
A device's virtqueue requests do not contain addresses (e.g. struct virtio_net_hdr). The virtqueue interface hides the details of memory organization and access. In theory a transport could be implemented over a medium that doesn't even offer shared memory (I think people have played with remote VIRTIO over TCP) and this is possible because device types don't know how virtqueue elements are represented.
This same design constraint extends to VIRTIO Shared Memory Regions because a VIRTIO Shared Memory Region's contents are defined by the device type, just like virtqueue requests.. I mentioned that it avoids address translation in device type implementations and also makes it easy to expose VIRTIO Shared Memory Regions to guest userspace.
(Similarly, putting addresses into the VIRTIO Configuration Space is also problematic because it exposes details of memory to the device type. They should be hidden by the VIRTIO transport.)
That explains the intention within the VIRTIO world. The question you raised was why you're allowed to then pass the address of a VIRTIO Shared Memory Region to another device instead of passing a <shmid, offset> pair. The answer is because DMA is beyond the scope of the VIRTIO spec. If the architecture allows you to expose a buffer that happens to be located in a VIRTIO Shared Memory Region to another device, then it's possible to pass that address. The other device may not even be a VIRTIO device. It just performs a DMA transaction to read/write that memory. This is happening at another layer and it's a valid thing to do.
So the answer is that in terms of designing VIRTIO device types, VIRTIO Shared Memory Region structure layouts or virtqueue request structs referring to VIRTIO Shared Memory Regions must not use addresses. But you may be able to pass the address of a VIRTIO Shared Memory Region to another device for DMA. They don't conflict because they are at different levels.
Quoted 4 lines
It definitely makes sense that setting up a mapping should be done with offsets. But unless a shared memory region can be dynamically reallocated at runtime, then it doesn't seem necessary to ban pointers within a shared memory region.
On a PCI device the BAR containing the VIRTIO Shared Memory Region can be remapped at runtime, so the shared memory region can move.
The reason is the same as for why device types don't use address inside virtqueue request structures: the details of memory are hidden by the VIRTIO transport and the device type doesn't deal in addresses.
Also, it makes mmapping a VIRTIO Shared Memory Region difficult in guest userspace because now some kind of address translation mechanism is necessary (i.e. IOMMU) so that the userspace application (which doesn't know about physical memory addresses) and the device implementation can translate memory. Just using offsets avoids this problem.
Stefan