Support mapping virtio-gpu virgl hostmem blobs using MAP_FIXED API
v10 cover Support mapping virtio-gpu virgl hostmem blobs using MAP_FIXED API
Virglrender got a new API that allows mapping host blobs at a given memory address using MAP_FIXED mmap flag [1]. Usage of this new API brings major performance and stability improvement for venus and drm native contexts, see commit message of the RFC patch for details.
Sending early to collect review feeback and have patch prepared by the time new version of libvirglrenderer will be released with the stabilized API.
[1] https://gitlab.freedesktop.org/virgl/virglrenderer/-/merge_requests/1374
Based-on: [email protected]
Changelog:
v10:- Changed mapping_state switch-case to use fallthrough as was suggested
by Akihiko Odaki.
v9: - Added patch converting finish_unmapping to mapping_state that was
suggested by Akihiko Odaki to address MR unmapping problem that may arise on resetting virtio-gpu.
- Changed virtio_gpu_virgl_reset() to return true/false. Suggested by Akihiko Odaki.
- Made virtio_gpu_virgl_reset() and virtio_gpu_virgl_init() static.
v8: - Added virtio_gpu_virgl_update_render_state() to virgl_resume_cmdq_bh(),
making sure that GPU is reset once cmd execution is resumed after suspencion. Suggested by Akihiko Odaki.
v7: - Changed virtio_gpu_virgl_reset() to keep virtio-gpu in reset state
when reset fails and renamed cmd_suspended -> suspended argument of resource_unref(), as was suggested by Akihiko Odaki.
v6: - Updated comment for virtio_gpu_virgl_reset(), removing part
telling that reset at "runtime" is unexpected and removing excessive error message about a failed reset. Requested by Akihiko Odaki.
v5: - Switched to use error_setg_errno() for qemu_ram_mmap() error
handling, as was suggested Akihiko Odaki.
- Added r-b from Alex Bennée to the first patch.
- Moved hostmem mapping offset validation to upper function as was suggested by Alex Bennée.
- Dropped all patches and changes that made funcs to return -1 since it was rejected by Alex Bennée. Refactoring can be done later on in a separate patchset.
- Extended clarifying comment of virtio_gpu_virgl_reset().
v4: - Addressed v3 review comments from Akihiko Odaki.
- Dropped patch making resource_unmap() error reported as a host failure instead of guest and added patch improving resource_map_blob() error reporting.
- Re-added CONFIG_WIN32 checks.
- Added clarifying comment to virtio_gpu_virgl_reset() RE unsupported context restoring.
v3: - Addressed v2 review comments from Akihiko Odaki.
- Droped check for CONFIG_WIN32. My current understanding that MAP_FIXED is supported by Cygwin.
- Added new patches resetting virgl resources, validating hostmem offset and improving error-handlings.
- Added r-b from Akihiko Odaki to the frist patch and t-b from Yiwei Zhang to the map_fixed patch.
v2: - Addressed v1 review comments from Akihiko Odaki
- Added patch that removes unnecessary memory_region_set_enabled(), suggested by Akihiko Odaki
Dmitry Osipenko (5):
virtio-gpu: Remove superfluous memory_region_set_enabled()
virtio-gpu: Validate hostmem mapping offset
virtio-gpu: Replace finish_unmapping with mapping_state
virtio-gpu: Destroy virgl resources on virtio-gpu reset
virtio-gpu: Support mapping hostmem blobs with map_fixed
hw/display/trace-events | 2 +- hw/display/virtio-gpu-gl.c | 58 ++++++--- hw/display/virtio-gpu-virgl.c | 216 ++++++++++++++++++++++++++++----- include/hw/virtio/virtio-gpu.h | 9 +- 4 files changed, 234 insertions(+), 51 deletions(-)
Signature · 1 line
2.52.0
RFC PATCH v10 1/5 virtio-gpu: Remove superfluous memory_region_set_enabled()
There is no need to explicitly enable/disable memory region when it's added or deleted respectively. Remove superfluous set_enabled() calls for consistency.
--- hw/display/virtio-gpu-virgl.c | 2 -- 1 file changed, 2 deletions(-)
hw/display/virtio-gpu-virgl.c +0 -2
@@ -139,7 +139,6 @@ virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g, mr = &vmr->mr;memory_region_init_ram_ptr (mr,OBJECT (mr),"blob" , size, data);memory_region_add_subregion (&b->hostmem, offset, mr); -memory_region_set_enabled (mr, true);/* * MR could outlive the resource if MR's reference is held outside of
@@ -201,7 +200,6 @@ virtio_gpu_virgl_unmap_resource_blob(VirtIOGPU *g, b->renderer_blocked++;/* memory region owns self res->mr object and frees it by itself */ -memory_region_set_enabled (mr, false);memory_region_del_subregion (&b->hostmem, mr);object_unparent (OBJECT (mr)); }
Signature · 1 line
2.52.0
RFC PATCH v10 2/5 virtio-gpu: Validate hostmem mapping offset
In reply to Dmitry OsipenkoCheck hostmem mapping boundaries originated from guest.
--- hw/display/virtio-gpu-virgl.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
hw/display/virtio-gpu-virgl.c +10 -0
@@ -767,6 +767,7 @@ static void virgl_cmd_resource_map_blob(VirtIOGPU *g,struct virtio_gpu_resource_map_blob mblob;struct virtio_gpu_virgl_resource *res;struct virtio_gpu_resp_map_info resp; +VirtIOGPUBase *b =VIRTIO_GPU_BASE (g);int ret;VIRTIO_GPU_FILL_CMD (mblob);
@@ -780,6 +781,15 @@ static void virgl_cmd_resource_map_blob(VirtIOGPU *g,return ; } +if (mblob.offset + res->base.blob_size > b->conf.hostmem || + mblob.offset + res->base.blob_size < mblob.offset) { +qemu_log_mask (LOG_GUEST_ERROR , +"%s: failed to map virgl resource: invalid offset\n" , + __func__); + cmd->error =VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER ; +return ; + } + ret =virtio_gpu_virgl_map_resource_blob (g, res, mblob.offset);if (ret) { cmd->error =VIRTIO_GPU_RESP_ERR_UNSPEC ;
Signature · 1 line
2.52.0
RFC PATCH v10 3/5 virtio-gpu: Replace finish_unmapping with mapping_state
In reply to Dmitry OsipenkoAllow virtio_gpu_virgl_unmap_resource_blob() to be invoked while async unmapping is in progress. Do it in preparation to improvement of virtio-gpu resetting that will require this change.
--- hw/display/trace-events | 2 +- hw/display/virtio-gpu-virgl.c | 28 ++++++++++++++++++++++------ 2 files changed, 23 insertions(+), 7 deletions(-)
hw/display/trace-events +1 -1
@@ -39,7 +39,7 @@ virtio_gpu_cmd_res_create_2d(uint32_t res, uint32_t fmt, uint32_t w, uint32_t h) virtio_gpu_cmd_res_create_3d(uint32_t res, uint32_t fmt, uint32_t w, uint32_t h, uint32_t d) "res 0x%x, fmt 0x%x, w %d, h %d, d %d" virtio_gpu_cmd_res_create_blob(uint32_t res, uint64_t size) "res 0x%x, size %" PRId64 virtio_gpu_cmd_res_map_blob(uint32_t res, void *vmr, void *mr) "res 0x%x, vmr %p, mr %p" -virtio_gpu_cmd_res_unmap_blob(uint32_t res, void *mr, bool finish_unmapping) "res 0x%x, mr %p, finish_unmapping %d" +virtio_gpu_cmd_res_unmap_blob(uint32_t res, void *mr, int mapping_state) "res 0x%x, mr %p, mapping_state %d" virtio_gpu_cmd_res_unref(uint32_t res) "res 0x%x" virtio_gpu_cmd_res_back_attach(uint32_t res) "res 0x%x" virtio_gpu_cmd_res_back_detach(uint32_t res) "res 0x%x"
hw/display/virtio-gpu-virgl.c +22 -6
@@ -68,10 +68,16 @@ virgl_get_egl_display(G_GNUC_UNUSED void *cookie)#endif #if VIRGL_VERSION_MAJOR >=1 +enum virtio_gpu_virgl_hostmem_region_mapping_state { +VIRTIO_GPU_MR_MAPPED , +VIRTIO_GPU_MR_UNMAP_STARTED , +VIRTIO_GPU_MR_UNMAP_COMPLETED , +}; +struct virtio_gpu_virgl_hostmem_region {MemoryRegion mr;struct VirtIOGPU *g; -bool finish_unmapping; +enum virtio_gpu_virgl_hostmem_region_mapping_state mapping_state; }; static struct virtio_gpu_virgl_hostmem_region *
@@ -95,7 +101,7 @@ static void virtio_gpu_virgl_hostmem_region_free(void *obj)VirtIOGPUGL *gl; vmr =to_hostmem_region (mr); - vmr->finish_unmapping = true; + vmr->mapping_state =VIRTIO_GPU_MR_UNMAP_COMPLETED ; b =VIRTIO_GPU_BASE (vmr->g); b->renderer_blocked--;
@@ -135,6 +141,7 @@ virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g, vmr = g_new0(struct virtio_gpu_virgl_hostmem_region, 1); vmr->g = g; + vmr->mapping_state =VIRTIO_GPU_MR_MAPPED ; mr = &vmr->mr;memory_region_init_ram_ptr (mr,OBJECT (mr),"blob" , size, data);
@@ -171,7 +178,8 @@ virtio_gpu_virgl_unmap_resource_blob(VirtIOGPU *g, vmr =to_hostmem_region (res->mr); -trace_virtio_gpu_cmd_res_unmap_blob (res->base.resource_id, mr, vmr->finish_unmapping); +trace_virtio_gpu_cmd_res_unmap_blob (res->base.resource_id, mr, + vmr->mapping_state);/* * Perform async unmapping in 3 steps:
@@ -182,7 +190,8 @@ virtio_gpu_virgl_unmap_resource_blob(VirtIOGPU *g,* asynchronously by virtio_gpu_virgl_hostmem_region_free(). * 3. Finish the unmapping with final virgl_renderer_resource_unmap(). */ -if (vmr->finish_unmapping) { +switch (vmr->mapping_state) { +case VIRTIO_GPU_MR_UNMAP_COMPLETED : res->mr =NULL ;g_free (vmr);
@@ -193,15 +202,22 @@ virtio_gpu_virgl_unmap_resource_blob(VirtIOGPU *g, __func__, strerror(-ret));return ret; } - }else { - *cmd_suspended = true; +break ; +case VIRTIO_GPU_MR_MAPPED :/* render will be unblocked once MR is freed */ b->renderer_blocked++; + vmr->mapping_state =VIRTIO_GPU_MR_UNMAP_STARTED ; +/* memory region owns self res->mr object and frees it by itself */ memory_region_del_subregion (&b->hostmem, mr);object_unparent (OBJECT (mr)); + +/* Fallthrough */ +case VIRTIO_GPU_MR_UNMAP_STARTED : + *cmd_suspended = true; +break ; }return 0 ;
Signature · 1 line
2.52.0
RFC PATCH v10 4/5 virtio-gpu: Destroy virgl resources on virtio-gpu reset
In reply to Dmitry OsipenkoProperly destroy virgl resources on virtio-gpu reset to not leak resources on a hot reboot of a VM.
--- hw/display/virtio-gpu-gl.c | 18 +---- hw/display/virtio-gpu-virgl.c | 117 ++++++++++++++++++++++++++------- include/hw/virtio/virtio-gpu.h | 6 +- 3 files changed, 101 insertions(+), 40 deletions(-)
hw/display/virtio-gpu-gl.c +2 -16
@@ -63,29 +63,14 @@ static void virtio_gpu_gl_flushed(VirtIOGPUBase *b)static void virtio_gpu_gl_handle_ctrl (VirtIODevice *vdev,VirtQueue *vq) {VirtIOGPU *g =VIRTIO_GPU (vdev); -VirtIOGPUGL *gl =VIRTIO_GPU_GL (vdev);struct virtio_gpu_ctrl_command *cmd;if (!virtio_queue_ready (vq)) {return ; } -switch (gl->renderer_state) { -case RS_RESET : -virtio_gpu_virgl_reset (g); -/* fallthrough */ -case RS_START : -if (virtio_gpu_virgl_init (g)) { - gl->renderer_state =RS_INIT_FAILED ; -return ; - } - - gl->renderer_state =RS_INITED ; -break ; -case RS_INIT_FAILED : +if (!virtio_gpu_virgl_update_render_state (g)) {return ; -case RS_INITED : -break ; } cmd =virtqueue_pop (vq,sizeof (struct virtio_gpu_ctrl_command ));
@@ -201,6 +186,7 @@ static void virtio_gpu_gl_class_init(ObjectClass *klass, const void *data) vgc->process_cmd = virtio_gpu_virgl_process_cmd; vgc->update_cursor_data = virtio_gpu_gl_update_cursor_data; + vgc->resource_destroy = virtio_gpu_virgl_resource_destroy; vdc->realize = virtio_gpu_gl_device_realize; vdc->unrealize = virtio_gpu_gl_device_unrealize; vdc->reset = virtio_gpu_gl_reset;
hw/display/virtio-gpu-virgl.c +95 -22
162 lines not on this page · raw message
include/hw/virtio/virtio-gpu.h +4 -2
@@ -389,9 +389,11 @@ void virtio_gpu_virgl_process_cmd(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd);void virtio_gpu_virgl_fence_poll (VirtIOGPU *g);void virtio_gpu_virgl_reset_scanout (VirtIOGPU *g); -void virtio_gpu_virgl_reset (VirtIOGPU *g); -int virtio_gpu_virgl_init (VirtIOGPU *g);GArray *virtio_gpu_virgl_get_capsets (VirtIOGPU *g);void virtio_gpu_virgl_reset_async_fences (VirtIOGPU *g); +void virtio_gpu_virgl_resource_destroy (VirtIOGPU *g, +struct virtio_gpu_simple_resource *res, +Error **errp); +bool virtio_gpu_virgl_update_render_state (VirtIOGPU *g);#endif
Signature · 1 line
2.52.0
RFC PATCH v10 5/5 virtio-gpu: Support mapping hostmem blobs with map_fixed
In reply to Dmitry OsipenkoSupport mapping virgl blobs to a fixed location of a hostmem memory region using new virglrenderer MAP_FIXED API.
This new feature closes multiple problems for virtio-gpu on QEMU:
- Having dedicated memory region for each mapped blob works notoriously slow due to QEMU's memory region software design built around RCU that isn't optimized for frequent removal of the regions
- KVM isn't optimized for a frequent slot changes too
- QEMU/KVM has a limit for a total number of created memory regions, crashing QEMU when limit is reached
This patch makes virtio-gpu-gl to pre-create a single anonymous memory region covering whole hostmem area to which blobs will be mapped using the MAP_FIXED API.
Not all virgl resources will support mapping at a fixed memory address. For them, we will continue to create individual nested memory sub-regions. In particular, vrend resources may not have MAP_FIXED capability.
Venus and DRM native contexts will largely benefit from the MAP_FIXED feature in terms of performance and stability improvement.
--- hw/display/virtio-gpu-gl.c | 40 ++++++++++++++++++++++- hw/display/virtio-gpu-virgl.c | 59 +++++++++++++++++++++++++++++++++- include/hw/virtio/virtio-gpu.h | 3 ++ 3 files changed, 100 insertions(+), 2 deletions(-)
hw/display/virtio-gpu-gl.c +39 -1
@@ -13,6 +13,7 @@#include "qemu/osdep.h" #include "qemu/iov.h" +#include "qemu/mmap-alloc.h" #include "qemu/module.h" #include "qemu/error-report.h" #include "qapi/error.h"
@@ -106,7 +107,12 @@ static void virtio_gpu_gl_reset(VirtIODevice *vdev)static void virtio_gpu_gl_device_realize (DeviceState *qdev,Error **errp) {ERRP_GUARD (); -VirtIOGPU *g =VIRTIO_GPU (qdev); +VirtIOGPUBase *b =VIRTIO_GPU_BASE (qdev); +VirtIOGPU *g =VIRTIO_GPU (b); +#if !defined(CONFIG_WIN32 ) +VirtIOGPUGL *gl =VIRTIO_GPU_GL (g); +void *map; +#endif #if HOST_BIG_ENDIAN error_setg (errp,"virgl is not supported on bigendian platforms" );
@@ -137,6 +143,27 @@ static void virtio_gpu_gl_device_realize(DeviceState *qdev, Error **errp)#endif virtio_gpu_device_realize (qdev, errp); +if (*errp) { +return ; + } + +#if !defined(CONFIG_WIN32 ) +if (virtio_gpu_hostmem_enabled (b->conf)) { + map =qemu_ram_mmap (-1 , b->conf.hostmem,qemu_real_host_page_size (), +0 ,0 ); +if (map ==MAP_FAILED ) { +error_setg_errno (errp, errno, +"virgl hostmem region could not be initialized" ); +return ; + } + + gl->hostmem_mmap = map; +memory_region_init_ram_ptr (&gl->hostmem_background,NULL , +"hostmem-background" , b->conf.hostmem, + gl->hostmem_mmap); +memory_region_add_subregion (&b->hostmem,0 , &gl->hostmem_background); + } +#endif }static const Property virtio_gpu_gl_properties[] = {
@@ -172,6 +199,17 @@ static void virtio_gpu_gl_device_unrealize(DeviceState *qdev) gl->renderer_state =RS_START ;g_array_unref (g->capset_ids); + +/* +* It is not guaranteed that the memory region will be finalized +* immediately with memory_region_del_subregion(), there can be +* a remaining reference to gl->hostmem_mmap. VirtIO-GPU is not +* hotpluggable, hence no need to worry about the leaked mapping. +* +* The memory_region_del_subregion(gl->hostmem_background) is unnecessary +* because b->hostmem and gl->hostmem_background belong to the same +* device and will be gone at the same time. +*/ }static void virtio_gpu_gl_class_init (ObjectClass *klass,const void *data)
hw/display/virtio-gpu-virgl.c +58 -1
92 lines not on this page · raw message
include/hw/virtio/virtio-gpu.h +3 -0
@@ -263,6 +263,9 @@ struct VirtIOGPUGL {QEMUBH *async_fence_bh; QSLIST_HEAD(, virtio_gpu_virgl_context_fence) async_fenceq; + +MemoryRegion hostmem_background; +void *hostmem_mmap; };struct VhostUserGPU {
Signature · 1 line
2.52.0
Re: [RFC PATCH v10 5/5] virtio-gpu: Support mapping hostmem blobs with map_fixed
Quoted 65 lines · Dmitry Osipenko
Support mapping virgl blobs to a fixed location of a hostmem memory region using new virglrenderer
MAP_FIXEDAPI.This new feature closes multiple problems for virtio-gpu on QEMU:
- Having dedicated memory region for each mapped blob works notoriously slow due to QEMU's memory region software design built around RCU that isn't optimized for frequent removal of the regions
- KVM isn't optimized for a frequent slot changes too
- QEMU/KVM has a limit for a total number of created memory regions, crashing QEMU when limit is reached
This patch makes virtio-gpu-gl to pre-create a single anonymous memory region covering whole hostmem area to which blobs will be mapped using the
MAP_FIXEDAPI.Not all virgl resources will support mapping at a fixed memory address. For them, we will continue to create individual nested memory sub-regions. In particular, vrend resources may not have
MAP_FIXEDcapability.Venus and DRM native contexts will largely benefit from the
MAP_FIXEDfeature in terms of performance and stability improvement.Tested-by Yiwei Zhang <[email protected]>Signed-off-by Dmitry Osipenko <[email protected]>--- hw/display/virtio-gpu-gl.c | 40 ++++++++++++++++++++++- hw/display/virtio-gpu-virgl.c | 59 +++++++++++++++++++++++++++++++++- include/hw/virtio/virtio-gpu.h | 3 ++ 3 files changed, 100 insertions(+), 2 deletions(-)hw/display/virtio-gpu-gl.c +9
-1@@ -13,6 +13,7 @@#include "qemu/osdep.h" #include "qemu/iov.h" +#include "qemu/mmap-alloc.h" #include "qemu/module.h" #include "qemu/error-report.h" #include "qapi/error.h" @@ -106,7 +107,12 @@ static void virtio_gpu_gl_reset(VirtIODevice *vdev)static void virtio_gpu_gl_device_realize (DeviceState *qdev,Error **errp) {ERRP_GUARD (); -VirtIOGPU *g =VIRTIO_GPU (qdev); +VirtIOGPUBase *b =VIRTIO_GPU_BASE (qdev); +VirtIOGPU *g =VIRTIO_GPU (b); +#if !defined(CONFIG_WIN32 ) +VirtIOGPUGL *gl =VIRTIO_GPU_GL (g); +void *map; +#endif #if HOST_BIG_ENDIAN error_setg (errp,"virgl is not supported on bigendian platforms" );@@ -137,6 +143,27 @@ static void virtio_gpu_gl_device_realize(DeviceState *qdev, Error **errp)#endif virtio_gpu_device_realize (qdev, errp); +if (*errp) { +return ;
hw/display/virtio-gpu-gl.c static void virtio_gpu_gl_device_realize(DeviceState *qdev, Error **errp) · line 157
Quoted 10 lines
hw/display/virtio-gpu-gl.c +10
-0+ } + +#if !defined(CONFIG_WIN32 ) +if (virtio_gpu_hostmem_enabled (b->conf)) { + map =qemu_ram_mmap (-1 , b->conf.hostmem,qemu_real_host_page_size (), +0 ,0 ); +if (map ==MAP_FAILED ) { +error_setg_errno (errp, errno, +"virgl hostmem region could not be initialized" ); +return ;
This "return" will leak bunch of memory allocated in virtio_gpu_device_realize(). Though the relization code is not carefully written to avoid memory leaks in case of errors, it is better to avoid adding another by keeping virtio_gpu_device_realize() at the end.
By the way, I think this is likely to be my last comment (unless I miss nothing more; hopefully it won't happen). This patch and "[RFC PATCH v10 2/5] virtio-gpu: Validate hostmem mapping offset" are only useful after the new release of virglrenderer is made, but the other patches are independelty useful, so I suggest submitting them without RFC.
Perhaps it may be even fine to submit the MAP_FIXED changes without RFC before virglrenderer 1.2.1 is released since the merge request is already accepted, but I leave that to you.
Regards,
Akihiko Odaki
Quoted 145 lines
hw/display/virtio-gpu-gl.c +20
-0+ } + + gl->hostmem_mmap = map; +memory_region_init_ram_ptr (&gl->hostmem_background,NULL , +"hostmem-background" , b->conf.hostmem, + gl->hostmem_mmap); +memory_region_add_subregion (&b->hostmem,0 , &gl->hostmem_background); + } +#endif }static const Property virtio_gpu_gl_properties[] = {@@ -172,6 +199,17 @@ static void virtio_gpu_gl_device_unrealize(DeviceState *qdev) gl->renderer_state =RS_START ;g_array_unref (g->capset_ids); + +/* +* It is not guaranteed that the memory region will be finalized +* immediately with memory_region_del_subregion(), there can be +* a remaining reference to gl->hostmem_mmap. VirtIO-GPU is not +* hotpluggable, hence no need to worry about the leaked mapping. +* +* The memory_region_del_subregion(gl->hostmem_background) is unnecessary +* because b->hostmem and gl->hostmem_background belong to the same +* device and will be gone at the same time. +*/ }static void virtio_gpu_gl_class_init (ObjectClass *klass,const void *data)hw/display/virtio-gpu-virgl.c +58
-1@@ -41,9 +41,13 @@ VIRGL_VERSION_MICRO >= (micro))#endif +#define VIRGL_HAS_MAP_FIXED \ + (VIRGL_CHECK_VERSION(1, 2, 1) && !IS_ENABLED(CONFIG_WIN32)) +struct virtio_gpu_virgl_resource {struct virtio_gpu_simple_resource base;MemoryRegion *mr; +void *map_fixed; }; static struct virtio_gpu_virgl_resource *@@ -126,6 +130,9 @@ virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g, {struct virtio_gpu_virgl_hostmem_region *vmr;VirtIOGPUBase *b =VIRTIO_GPU_BASE (g); +#if VIRGL_HAS_MAP_FIXED +VirtIOGPUGL *gl =VIRTIO_GPU_GL (g); +#endif MemoryRegion *mr;uint64_t size;void *data;@@ -136,6 +143,41 @@ virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g,return -EOPNOTSUPP ; } +#if VIRGL_HAS_MAP_FIXED +/* +* virgl_renderer_resource_map_fixed() allows to create multiple +* mappings of the same resource, while virgl_renderer_resource_map() +* not. Don't allow mapping same resource twice. +*/ +if (res->map_fixed || res->mr) { +qemu_log_mask (LOG_GUEST_ERROR , +"%s: failed to map(fixed) virgl resource: already mapped\n" , + __func__); +return -EBUSY ; + } + + ret =virgl_renderer_resource_map_fixed (res->base.resource_id, + gl->hostmem_mmap + offset); +switch (ret) { +case 0 : + res->map_fixed = gl->hostmem_mmap + offset; +return 0 ; + +case -EOPNOTSUPP : +/* +* MAP_FIXED is unsupported by this resource. +* Mapping falls back to a blob subregion method in that case. +*/ +break ; + +default : +qemu_log_mask (LOG_GUEST_ERROR , +"%s: failed to map(fixed) virgl resource: %s\n" , + __func__,strerror (-ret)); +return ret; + } +#endif + ret =virgl_renderer_resource_map (res->base.resource_id, &data, &size);if (ret) { qemu_log_mask(LOG_GUEST_ERROR, "%s: failed to map virgl resource: %s\n",@@ -149,7 +191,7 @@ virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g, mr = &vmr->mr;memory_region_init_ram_ptr (mr,OBJECT (mr),"blob" , size, data); -memory_region_add_subregion (&b->hostmem, offset, mr); +memory_region_add_subregion_overlap (&b->hostmem, offset, mr,1 );/* * MR could outlive the resource if MR's reference is held outside of @@ -176,6 +218,21 @@ virtio_gpu_virgl_unmap_resource_blob(VirtIOGPU *g,MemoryRegion *mr = res->mr;int ret; +#if VIRGL_HAS_MAP_FIXED +if (res->map_fixed) { +if (mmap (res->map_fixed, res->base.blob_size,PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_ANONYMOUS |MAP_FIXED , +-1 ,0 ) ==MAP_FAILED ) { + ret = -errno; +error_report ("%s: failed to unmap(fixed) virgl resource: %s" , + __func__,strerror (-ret)); +return ret; + } + + res->map_fixed =NULL ; + } +#endif +if (!mr) {return 0 ; }include/hw/virtio/virtio-gpu.h +3
-0@@ -263,6 +263,9 @@ struct VirtIOGPUGL {QEMUBH *async_fence_bh; QSLIST_HEAD(, virtio_gpu_virgl_context_fence) async_fenceq; + +MemoryRegion hostmem_background; +void *hostmem_mmap; };struct VhostUserGPU {
Re: [RFC PATCH v10 5/5] virtio-gpu: Support mapping hostmem blobs with map_fixed
Quoted 90 lines · Akihiko Odaki
Quoted 67 lines · Dmitry Osipenko
Support mapping virgl blobs to a fixed location of a hostmem memory region using new virglrenderer
MAP_FIXEDAPI.This new feature closes multiple problems for virtio-gpu on QEMU:
- Having dedicated memory region for each mapped blob works notoriously slow due to QEMU's memory region software design built around RCU that isn't optimized for frequent removal of the regions
- KVM isn't optimized for a frequent slot changes too
- QEMU/KVM has a limit for a total number of created memory regions, crashing QEMU when limit is reached
This patch makes virtio-gpu-gl to pre-create a single anonymous memory region covering whole hostmem area to which blobs will be mapped using the
MAP_FIXEDAPI.Not all virgl resources will support mapping at a fixed memory address. For
them, we will continue to create individual nested memory sub-regions. In particular, vrend resources may not haveMAP_FIXEDcapability.Venus and DRM native contexts will largely benefit from the
MAP_FIXEDfeature in terms of performance and stability improvement.Tested-by Yiwei Zhang <[email protected]>Signed-off-by Dmitry Osipenko <[email protected]>---
hw/display/virtio-gpu-gl.c | 40 ++++++++++++++++++++++- hw/display/virtio-gpu-virgl.c | 59 +++++++++++++++++++++++++++++++++-include/hw/virtio/virtio-gpu.h| 3 ++3 files changed, 100 insertions(+), 2 deletions(-)hw/display/virtio-gpu-gl.c +1
-0@@ -13,6 +13,7 @@ #include "qemu/osdep.h" #include "qemu/iov.h" +#include "qemu/mmap-alloc.h" #include "qemu/module.h" #include "qemu/error-report.h" #include "qapi/error.h"@@ -106,7 +107,12 @@ static void virtio_gpu_gl_reset(VirtIODevice *vdev)static void
virtio_gpu_gl_device_realize(DeviceState *qdev, Error **errp)
{
ERRP_GUARD();
- VirtIOGPU *g =VIRTIO_GPU(qdev);
+ VirtIOGPUBase *b =VIRTIO_GPU_BASE(qdev);
+ VirtIOGPU *g =VIRTIO_GPU(b);hw/display/virtio-gpu-gl.c +4
-0+#if !defined(CONFIG_WIN32 ) + VirtIOGPUGL *gl = VIRTIO_GPU_GL(g); + void *map; +#endif #if
HOST_BIG_ENDIAN
error_setg(errp, "virgl is not supported on bigendian platforms");hw/display/virtio-gpu-gl.c +4
-0@@ -137,6 +143,27 @@ static void virtio_gpu_gl_device_realize(DeviceState *qdev, Error **errp) #endif virtio_gpu_device_realize(qdev, errp); + if (*errp) { + return; + } +hw/display/virtio-gpu-gl.c static void · line 159
Quoted 10 lines
hw/display/virtio-gpu-gl.c +8
-0+#if !defined(CONFIG_WIN32 ) + if (virtio_gpu_hostmem_enabled(b->conf)) { + map = qemu_ram_mmap(-1, b->conf.hostmem,qemu_real_host_page_size (), + 0, 0); + if (map == MAP_FAILED) { + error_setg_errno(errp, errno, + "virgl hostmem region could not be initialized"); + return;This "return" will leak bunch of memory allocated in
virtio_gpu_device_realize(). Though the relization code is not carefully written to avoid memory leaks in case of errors, it is better to avoid adding another by keepingvirtio_gpu_device_realize()at the end.By the way, I think this is likely to be my last comment (unless I miss nothing more; hopefully it won't happen). This patch and "[RFC PATCH v10 2/5] virtio-gpu: Validate hostmem mapping offset" are only useful after the new release of virglrenderer is made, but the other patches are independelty useful, so I suggest submitting them without RFC.
Quoted 3 lines
Perhaps it may be even fine to submit the
MAP_FIXEDchanges without RFC before virglrenderer 1.2.1 is released since the merge request is already accepted, but I leave that to you.
Thanks, will move the code in v11. New virglrenderer should be released soon as Gert Wollny returned form vacation today. May wait for the release before sending v11 without RFC.
Signature · 2 lines
Best regards, Dmitry