Support mapping virtio-gpu virgl hostmem blobs using MAP_FIXED API
v8 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:
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 (4):
virtio-gpu: Remove superfluous memory_region_set_enabled()
virtio-gpu: Validate hostmem mapping offset
virtio-gpu: Destroy virgl resources on virtio-gpu reset
virtio-gpu: Support mapping hostmem blobs with map_fixed
hw/display/virtio-gpu-gl.c | 58 +++++++--- hw/display/virtio-gpu-virgl.c | 186 ++++++++++++++++++++++++++++----- include/hw/virtio/virtio-gpu.h | 9 +- 3 files changed, 211 insertions(+), 42 deletions(-)
Signature · 1 line
2.52.0
RFC PATCH v8 1/4 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 v8 2/4 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 v8 3/4 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 | 115 +++++++++++++++++++++++++++------ include/hw/virtio/virtio-gpu.h | 6 +- 3 files changed, 101 insertions(+), 38 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 +94 -21
156 lines not on this page · raw message
include/hw/virtio/virtio-gpu.h +5 -1
@@ -389,9 +389,13 @@ 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_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
Re: [RFC PATCH v8 3/4] virtio-gpu: Destroy virgl resources on virtio-gpu reset
Quoted 225 lines · Dmitry Osipenko
Properly destroy virgl resources on virtio-gpu reset to not leak resources on a hot reboot of a VM.
Suggested-by Akihiko Odaki <[email protected]>Signed-off-by Dmitry Osipenko <[email protected]>--- hw/display/virtio-gpu-gl.c | 18 +----- hw/display/virtio-gpu-virgl.c | 115 +++++++++++++++++++++++++++------ include/hw/virtio/virtio-gpu.h | 6 +- 3 files changed, 101 insertions(+), 38 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 +94
-21@@ -80,10 +80,41 @@ to_hostmem_region(MemoryRegion *mr) return container_of(mr, struct virtio_gpu_virgl_hostmem_region, mr); } +bool virtio_gpu_virgl_update_render_state (VirtIOGPU *g) +{ +VirtIOGPUGL *gl =VIRTIO_GPU_GL (g); + +switch (gl->renderer_state) { +case RS_RESET : +if (virtio_gpu_virgl_reset (g)) { +return false; + } +/* fallthrough */ +case RS_START : +if (virtio_gpu_virgl_init (g)) { + gl->renderer_state =RS_INIT_FAILED ; +return false; + } + + gl->renderer_state =RS_INITED ; +break ; +case RS_INIT_FAILED : +return false; +case RS_INITED : +break ; + } + +return true; +} +static void virtio_gpu_virgl_resume_cmdq_bh (void *opaque) {VirtIOGPU *g = opaque; +if (!virtio_gpu_virgl_update_render_state (g)) { +return ; + } +virtio_gpu_process_cmdq (g); }@@ -304,14 +335,46 @@ static void virgl_cmd_create_resource_3d(VirtIOGPU *g,virgl_renderer_resource_create (&args,NULL ,0 ); } +static int +virtio_gpu_virgl_resource_unref (VirtIOGPU *g, +struct virtio_gpu_virgl_resource *res, +bool *suspended) +{ +struct iovec *res_iovs =NULL ; +int num_iovs =0 ; +#if VIRGL_VERSION_MAJOR >=1 +int ret; + + ret =virtio_gpu_virgl_unmap_resource_blob (g, res, suspended); +if (ret) { +return ret; + } +if (*suspended) { +return 0 ; + } +#endif + +virgl_renderer_resource_detach_iov (res->base.resource_id, + &res_iovs, + &num_iovs); +if (res_iovs !=NULL && num_iovs !=0 ) { +virtio_gpu_cleanup_mapping_iov (g, res_iovs, num_iovs); + } +virgl_renderer_resource_unref (res->base.resource_id); + +QTAILQ_REMOVE (&g->reslist, &res->base, next); + +g_free (res); + +return 0 ; +} +static void virgl_cmd_resource_unref (VirtIOGPU *g,struct virtio_gpu_ctrl_command *cmd,bool *cmd_suspended) {struct virtio_gpu_resource_unref unref;struct virtio_gpu_virgl_resource *res; -struct iovec *res_iovs =NULL ; -int num_iovs =0 ;VIRTIO_GPU_FILL_CMD (unref);trace_virtio_gpu_cmd_res_unref (unref.resource_id);@@ -324,27 +387,21 @@ static void virgl_cmd_resource_unref(VirtIOGPU *g,return ; } -#if VIRGL_VERSION_MAJOR >=1 -if (virtio_gpu_virgl_unmap_resource_blob (g, res, cmd_suspended)) { - cmd->error =VIRTIO_GPU_RESP_ERR_UNSPEC ; -return ; - } -if (*cmd_suspended) { -return ; - } -#endif +virtio_gpu_virgl_resource_unref (g, res, cmd_suspended); +} -virgl_renderer_resource_detach_iov (unref.resource_id, - &res_iovs, - &num_iovs); -if (res_iovs !=NULL && num_iovs !=0 ) { -virtio_gpu_cleanup_mapping_iov (g, res_iovs, num_iovs); - } -virgl_renderer_resource_unref (unref.resource_id); +void virtio_gpu_virgl_resource_destroy (VirtIOGPU *g, +struct virtio_gpu_simple_resource *base, +Error **errp) +{ +struct virtio_gpu_virgl_resource *res; +bool suspended = false; -QTAILQ_REMOVE (&g->reslist, &res->base, next); + res = container_of(base, struct virtio_gpu_virgl_resource, base); -g_free (res); +if (virtio_gpu_virgl_resource_unref (g, res, &suspended)) { +error_setg (errp,"failed to destroy virgl resource" ); + } } static void virgl_cmd_context_create(VirtIOGPU *g,@@ -1273,11 +1330,27 @@ void virtio_gpu_virgl_reset_scanout(VirtIOGPU *g) } } -void virtio_gpu_virgl_reset (VirtIOGPU *g) +int virtio_gpu_virgl_reset (VirtIOGPU *g) { +struct virtio_gpu_simple_resource *res, *tmp; + +/* +* Virgl blob resource unmapping can be suspended and +* deferred on unref, ensure that destruction is completed. +*/ + QTAILQ_FOREACH_SAFE(res, &g->reslist, next, tmp) { +virtio_gpu_virgl_resource_destroy (g, res,NULL ); + } + +if (!QTAILQ_EMPTY (&g->reslist)) { +return -EBUSY ; + } +virgl_renderer_reset ();virtio_gpu_virgl_reset_async_fences (g); + +return 0 ; }int virtio_gpu_virgl_init (VirtIOGPU *g)include/hw/virtio/virtio-gpu.h +0
-0@@ -389,9 +389,13 @@ void virtio_gpu_virgl_process_cmd(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd);void virtio_gpu_virgl_fence_poll (VirtIOGPU *g);
include/hw/virtio/virtio-gpu.h void virtio_gpu_virgl_process_cmd(VirtIOGPU *g, · line 399
Quoted 10 lines
include/hw/virtio/virtio-gpu.h +5
-1void virtio_gpu_virgl_reset_scanout (VirtIOGPU *g); -void virtio_gpu_virgl_reset (VirtIOGPU *g); +int 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);
virtio_gpu_virgl_update_render_state() and virtio_gpu_virgl_reset() return values basically for the same purpose, but the values are different, and I find it confusing. Personally I prefer both to have bool.
Quoted 2 lines
include/hw/virtio/virtio-gpu.h +0
-0#endif
Re: [RFC PATCH v8 3/4] virtio-gpu: Destroy virgl resources on virtio-gpu reset
Quoted 85 lines · Akihiko Odaki
Quoted 84 lines · Dmitry Osipenko
Properly destroy virgl resources on virtio-gpu reset to not leak resources on a hot reboot of a VM.
Suggested-by Akihiko Odaki <[email protected]>Signed-off-by Dmitry Osipenko <[email protected]>---
hw/display/virtio-gpu-gl.c | 18 +----- hw/display/virtio-gpu-virgl.c | 115 +++++++++++++++++++++++++++------include/hw/virtio/virtio-gpu.h| 6 +-3 files changed, 101 insertions(+), 38 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 +26
-0@@ -80,10 +80,41 @@ to_hostmem_region(MemoryRegion *mr) return container_of(mr, struct virtio_gpu_virgl_hostmem_region, mr); } +bool virtio_gpu_virgl_update_render_state (VirtIOGPU *g) +{ + VirtIOGPUGL *gl = VIRTIO_GPU_GL(g); + + switch (gl->renderer_state) { + case RS_RESET: + if (virtio_gpu_virgl_reset(g)) { + return false; + } + /* fallthrough */ + case RS_START: + if (virtio_gpu_virgl_init(g)) { + gl->renderer_state = RS_INIT_FAILED; + return false; + } + + gl->renderer_state = RS_INITED; + break; + case RS_INIT_FAILED: + return false; + case RS_INITED: + break; + } + + return true; +}
Quoted 8 lines
hw/display/virtio-gpu-virgl.c to_hostmem_region(MemoryRegion *mr) · line 117
Quoted 8 lines
hw/display/virtio-gpu-virgl.c +5
-0+ static void virtio_gpu_virgl_resume_cmdq_bh(void *opaque) { VirtIOGPU *g = opaque; + if (!virtio_gpu_virgl_update_render_state(g)) { + return; + } +
This logic is still faulty. virtio_gpu_virgl_unmap_resource_blob() assumes the memory region is still parented when !vmr->finish_unmapping, but it may no longer be true. The function can be called for a memory region even while its finish_unmapping stays false due to another memory region freed earlier.
To fix this issue, I suggest converting finish_unmapping to an enum that represents three states:
1. The memory region is still parented.
2. The memory region is unparented but
virtio_gpu_virgl_hostmem_region_free() has not been called yet.
3. virtio_gpu_virgl_hostmem_region_free() has been called.
Note that the distinction between state 1 and 2 is not present with finish_unmapping and a new addition.
Quoted 134 lines
Quoted 119 lines
hw/display/virtio-gpu-virgl.c +63
-21virtio_gpu_process_cmdq(g); }@@ -304,14 +335,46 @@ static voidvirgl_cmd_create_resource_3d (VirtIOGPU *g, virgl_renderer_resource_create(&args, NULL, 0); } +static int +virtio_gpu_virgl_resource_unref(VirtIOGPU *g, + struct virtio_gpu_virgl_resource *res, + bool *suspended) +{ + struct iovec *res_iovs = NULL; + int num_iovs = 0; +#if VIRGL_VERSION_MAJOR >=1 + int ret; + + ret = virtio_gpu_virgl_unmap_resource_blob(g, res, suspended); + if (ret) { + return ret; + } + if (*suspended) { + return 0; + } +#endif + + virgl_renderer_resource_detach_iov(res->base.resource_id, + &res_iovs, + &num_iovs); + if (res_iovs != NULL && num_iovs != 0) { + virtio_gpu_cleanup_mapping_iov(g, res_iovs, num_iovs); + } + virgl_renderer_resource_unref(res->base.resource_id); + + QTAILQ_REMOVE(&g->reslist, &res->base, next); + + g_free(res); + + return 0; +} + static void virgl_cmd_resource_unref(VirtIOGPU *g, struct virtio_gpu_ctrl_command *cmd, bool *cmd_suspended) { struct virtio_gpu_resource_unref unref; struct virtio_gpu_virgl_resource *res; - struct iovec *res_iovs = NULL; - int num_iovs = 0; VIRTIO_GPU_FILL_CMD(unref); trace_virtio_gpu_cmd_res_unref(unref.resource_id);@@ -324,27 +387,21 @@ static void virgl_cmd_resource_unref(VirtIOGPU *g, return; } -#if VIRGL_VERSION_MAJOR >=1 - if (virtio_gpu_virgl_unmap_resource_blob(g, res, cmd_suspended)) { - cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC; - return; - } - if (*cmd_suspended) { - return; - } -#endif + virtio_gpu_virgl_resource_unref(g, res, cmd_suspended); +} - virgl_renderer_resource_detach_iov(unref.resource_id, - &res_iovs, - &num_iovs); - if (res_iovs != NULL && num_iovs != 0) { - virtio_gpu_cleanup_mapping_iov(g, res_iovs, num_iovs); - } - virgl_renderer_resource_unref(unref.resource_id); +void virtio_gpu_virgl_resource_destroy (VirtIOGPU *g, + structvirtio_gpu_simple_resource *base, + Error **errp) +{ + struct virtio_gpu_virgl_resource *res; + bool suspended = false; - QTAILQ_REMOVE(&g->reslist, &res->base, next); + res = container_of(base, struct virtio_gpu_virgl_resource, base); - g_free(res); + if (virtio_gpu_virgl_resource_unref(g, res, &suspended)) { + error_setg(errp, "failed to destroy virgl resource"); + } } static void virgl_cmd_context_create(VirtIOGPU *g,@@ -1273,11 +1330,27 @@ void virtio_gpu_virgl_reset_scanout(VirtIOGPU *g) } } -void virtio_gpu_virgl_reset (VirtIOGPU *g) +int virtio_gpu_virgl_reset (VirtIOGPU *g) { + struct virtio_gpu_simple_resource *res, *tmp; + + /* + * Virgl blob resource unmapping can be suspended and + * deferred on unref, ensure that destruction is completed. + */ + QTAILQ_FOREACH_SAFE(res, &g->reslist, next, tmp) { + virtio_gpu_virgl_resource_destroy(g, res, NULL); + } + + if (!QTAILQ_EMPTY(&g->reslist)) { + return -EBUSY; + } +>> virgl_renderer_reset(); virtio_gpu_virgl_reset_async_fences(g); + + return 0; } int virtio_gpu_virgl_init(VirtIOGPU *g)include/hw/virtio/virtio-gpu.h +0
-0@@ -389,9 +389,13 @@ 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);include/hw/virtio/virtio-gpu.h void virtio_gpu_virgl_process_cmd(VirtIOGPU *g, · line 400
Quoted 10 lines
include/hw/virtio/virtio-gpu.h +5
-1-void virtio_gpu_virgl_reset (VirtIOGPU *g); +int 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, + structvirtio_gpu_simple_resource *res, + Error **errp); +bool virtio_gpu_virgl_update_render_state (VirtIOGPU *g);
virtio_gpu_virgl_update_render_state()andvirtio_gpu_virgl_reset()return values basically for the same purpose, but the values are different, and I find it confusing. Personally I prefer both to have bool.Quoted 1 line
include/hw/virtio/virtio-gpu.h +0
-0#endif
Re: [RFC PATCH v8 3/4] virtio-gpu: Destroy virgl resources on virtio-gpu reset
Hello Akihiko,
Quoted 48 lines · Akihiko Odaki
Quoted 26 lines
Quoted 26 lines
hw/display/virtio-gpu-virgl.c +26
-0+bool virtio_gpu_virgl_update_render_state (VirtIOGPU *g) +{ + VirtIOGPUGL *gl = VIRTIO_GPU_GL(g); + + switch (gl->renderer_state) { + case RS_RESET: + if (virtio_gpu_virgl_reset(g)) { + return false; + } + /* fallthrough */ + case RS_START: + if (virtio_gpu_virgl_init(g)) { + gl->renderer_state = RS_INIT_FAILED; + return false; + } + + gl->renderer_state = RS_INITED; + break; + case RS_INIT_FAILED: + return false; + case RS_INITED: + break; + } + + return true; +}Quoted 8 lines
Quoted 8 lines
+
static voidvirtio_gpu_virgl_resume_cmdq_bh(void *opaque){
VirtIOGPU *g = opaque;
+ if (!virtio_gpu_virgl_update_render_state(g)) { + return;
+ }
+This logic is still faulty.
virtio_gpu_virgl_unmap_resource_blob()assumes the memory region is still parented when !vmr->finish_unmapping, but it may no longer be true. The function can be called for a memory region even while itsfinish_unmappingstays false due to another memory region freed earlier.To fix this issue, I suggest converting
finish_unmappingto an enum that represents three states:
1. The memory region is still parented.
2. The memory region is unparented butvirtio_gpu_virgl_hostmem_region_free()has not been called yet.
3.virtio_gpu_virgl_hostmem_region_free()has been called.
Quoted 2 lines
Note that the distinction between state 1 and 2 is not present with
finish_unmappingand a new addition.
Very good catch, will fix in v9. Thanks for the review!
Signature · 2 lines
Best regards, Dmitry
RFC PATCH v8 4/4 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