Skip to contentSkip to conversation

Support mapping virtio-gpu virgl hostmem blobs using MAP_FIXED API

RFC PATCH Superseded Dmitry Osipenko 3 participants 19 messages Nov 25, 2025 – Dec 8, 2025

v4 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v4 cover Support mapping virtio-gpu virgl hostmem blobs using MAP_FIXED API

Virglrender got a new unstable 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:

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 (7):
virtio-gpu: Remove superfluous memory_region_set_enabled()
virtio-gpu: Validate hostmem mapping offset
virtio-gpu: Improve virgl_cmd_resource_map_blob() error handling
virtio-gpu: Make virtio_gpu_virgl_unmap_resource_blob() return -1 on
error
virtio-gpu: Destroy virgl resources on virtio-gpu reset
virtio-gpu: Make virtio_gpu_virgl_init() return -1 on error
virtio-gpu: Support mapping hostmem blobs with map_fixed

 hw/display/virtio-gpu-gl.c     |  49 +++++++++-
 hw/display/virtio-gpu-virgl.c  | 172 +++++++++++++++++++++++++++------
 include/hw/virtio/virtio-gpu.h |   8 +-
 3 files changed, 195 insertions(+), 34 deletions(-)
Signature · 1 line
2.51.1
#1 lore raw

RFC PATCH v4 1/7 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
index 80f71c0b66aa..a6860f63b563 100644
@@ -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.51.1
Reviewed-by on v4 1/7
#2 lore raw

Re: [RFC PATCH v4 1/7] virtio-gpu: Remove superfluous memory_region_set_enabled()

Quoted 7 lines · Dmitry Osipenko

There is no need to explicitly enable/disable memory region when it's added or deleted respectively. Remove superfluous set_enabled() calls for consistency.

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#3 lore raw

RFC PATCH v4 2/7 virtio-gpu: Validate hostmem mapping offset

In reply to Dmitry Osipenko

Check hostmem mapping boundaries originated from guest.

---
 hw/display/virtio-gpu-virgl.c | 8 ++++++++
 1 file changed, 8 insertions(+)
hw/display/virtio-gpu-virgl.c +8 -0
index a6860f63b563..2224f59cf5d7 100644
@@ -126,6 +126,14 @@ virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g,
         return -EOPNOTSUPP;
     }
 
+    if (offset + res->base.blob_size > b->conf.hostmem ||
+        offset + res->base.blob_size < offset) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: failed to map virgl resource: invalid offset\n",
+                      __func__);
+        return -EINVAL;
+    }
+
     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",
Signature · 1 line
2.51.1
#4 lore raw

Re: [RFC PATCH v4 2/7] virtio-gpu: Validate hostmem mapping offset

Quoted 13 lines · Dmitry Osipenko

Check hostmem mapping boundaries originated from guest.

---
 hw/display/virtio-gpu-virgl.c | 8 ++++++++
 1 file changed, 8 insertions(+)
hw/display/virtio-gpu-virgl.c +0 -0
index a6860f63b563..2224f59cf5d7 100644

hw/display/virtio-gpu-virgl.c virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g, · line 130

Quoted 5 lines
hw/display/virtio-gpu-virgl.c +2 -0
@@ -126,6 +126,14 @@ virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g,
         return -EOPNOTSUPP;
     }
 
+    if (offset + res->base.blob_size > b->conf.hostmem ||
+        offset + res->base.blob_size < offset) {

This second check seems weird. offset + blob_size could only every be smaller than offset if blob_size was negative. I feel we should have caught that earlier if it can happen.

Are we trying to catch an overflow here?

Quoted 9 lines
hw/display/virtio-gpu-virgl.c +6 -0
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: failed to map virgl resource: invalid offset\n",
+                      __func__);
+        return -EINVAL;
+    }
+
     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",
Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#5 lore raw

Re: [RFC PATCH v4 2/7] virtio-gpu: Validate hostmem mapping offset

Quoted 18 lines · Alex Bennée
Quoted 16 lines · Dmitry Osipenko

Check hostmem mapping boundaries originated from guest.

---
 hw/display/virtio-gpu-virgl.c | 8 ++++++++
 1 file changed, 8 insertions(+)
hw/display/virtio-gpu-virgl.c +0 -0
index a6860f63b563..2224f59cf5d7 100644
@@ -126,6 +126,14 @@ virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g,
         return -EOPNOTSUPP;
     }
 
Quoted 8 lines

hw/display/virtio-gpu-virgl.c virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g, · line 130

Quoted 2 lines
hw/display/virtio-gpu-virgl.c +2 -0
+    if (offset + res->base.blob_size > b->conf.hostmem ||
+        offset + res->base.blob_size < offset) {

This second check seems weird. offset + blob_size could only every be smaller than offset if blob_size was negative. I feel we should have caught that earlier if it can happen.

Are we trying to catch an overflow here?

The second check catches integer overflow for huge mblob.offset that is u64 coming from guest. This wasn't caught before, we missed validation of the offset value.

Signature · 2 lines
Best regards,
Dmitry
#6 lore raw

RFC PATCH v4 3/7 virtio-gpu: Improve virgl_cmd_resource_map_blob() error handling

In reply to Dmitry Osipenko

Make virgl_cmd_resource_map_blob() return -1 for errors originated from external virglrenderer library and respond to guest with most appropriate error message.

---
 hw/display/virtio-gpu-virgl.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
hw/display/virtio-gpu-virgl.c +11 -2
index 2224f59cf5d7..edcdad0af232 100644
@@ -138,7 +138,7 @@ virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g,
     if (ret) {
         qemu_log_mask(LOG_GUEST_ERROR, "%s: failed to map virgl resource: %s\n",
                       __func__, strerror(-ret));
-        return ret;
+        return -1;
     }
 
     vmr = g_new0(struct virtio_gpu_virgl_hostmem_region, 1);
@@ -789,7 +789,16 @@ static void virgl_cmd_resource_map_blob(VirtIOGPU *g,
     }
 
     ret = virtio_gpu_virgl_map_resource_blob(g, res, mblob.offset);
-    if (ret) {
+
+    switch (ret) {
+    case 0:
+        break;
+
+    case -EINVAL:
+        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
+        return;
+
+    default:
         cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
         return;
     }
Signature · 1 line
2.51.1
#7 lore raw

Re: [RFC PATCH v4 3/7] virtio-gpu: Improve virgl_cmd_resource_map_blob() error handling

Quoted 15 lines · Dmitry Osipenko

Make virgl_cmd_resource_map_blob() return -1 for errors originated from external virglrenderer library and respond to guest with most appropriate error message.

---
 hw/display/virtio-gpu-virgl.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
hw/display/virtio-gpu-virgl.c +0 -0
index 2224f59cf5d7..edcdad0af232 100644

hw/display/virtio-gpu-virgl.c virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g, · line 141

Quoted 5 lines
hw/display/virtio-gpu-virgl.c +1 -1
@@ -138,7 +138,7 @@ virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g,
     if (ret) {
         qemu_log_mask(LOG_GUEST_ERROR, "%s: failed to map virgl resource: %s\n",
                       __func__, strerror(-ret));
-        return ret;
+        return -1;

If we are using errno's lets use the defines rather than -1, should this be -EPERM?

Quoted 6 lines
hw/display/virtio-gpu-virgl.c +0 -0
     }
 
     vmr = g_new0(struct virtio_gpu_virgl_hostmem_region, 1);
@@ -789,7 +789,16 @@ static void virgl_cmd_resource_map_blob(VirtIOGPU *g,
     }
 

hw/display/virtio-gpu-virgl.c static void virgl_cmd_resource_map_blob(VirtIOGPU *g, · line 799

Quoted 10 lines
hw/display/virtio-gpu-virgl.c +8 -1
     ret = virtio_gpu_virgl_map_resource_blob(g, res, mblob.offset);
-    if (ret) {
+
+    switch (ret) {
+    case 0:
+        break;
+
+    case -EINVAL:
+        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
+        return;

which isn't what can come here. I see EOPNOTSUPP not being handled either.

Quoted 5 lines
hw/display/virtio-gpu-virgl.c +2 -0
+
+    default:
         cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
         return;
     }
Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#8 lore raw

Re: [RFC PATCH v4 3/7] virtio-gpu: Improve virgl_cmd_resource_map_blob() error handling

Quoted 14 lines · Alex Bennée

hw/display/virtio-gpu-virgl.c virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g, · line 141

Quoted 5 lines
     if (ret) {
         qemu_log_mask(LOG_GUEST_ERROR, "%s: failed to map virgl resource: %s\n",
                       __func__, strerror(-ret));
hw/display/virtio-gpu-virgl.c +1 -1
-        return ret;
+        return -1;

If we are using errno's lets use the defines rather than -1, should this be -EPERM?

Quoted 6 lines
     }
 
     vmr = g_new0(struct virtio_gpu_virgl_hostmem_region, 1);
(quoted diff) +0 -0
@@ -789,7 +789,16 @@ static void virgl_cmd_resource_map_blob(VirtIOGPU *g,
     }
 
Quoted 11 lines

hw/display/virtio-gpu-virgl.c static void virgl_cmd_resource_map_blob(VirtIOGPU *g, · line 799

Quoted 10 lines
hw/display/virtio-gpu-virgl.c +8 -1
     ret = virtio_gpu_virgl_map_resource_blob(g, res, mblob.offset);
-    if (ret) {
+
+    switch (ret) {
+    case 0:
+        break;
+
+    case -EINVAL:
+        cmd->error = VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER;
+        return;

which isn't what can come here. I see EOPNOTSUPP not being handled either.

When error comes from external virglrenderer code, we don't know exact reason it fails. Using -1 allows clear distinguish of external vs internal to QEMU errors.

EOPNOTSUPP and everything else that doesn't have a directly matching guest code falls into the default error handling returning VIRTIO_GPU_RESP_ERR_UNSPEC to guest.

Signature · 2 lines
Best regards,
Dmitry
#9 lore raw

RFC PATCH v4 4/7 virtio-gpu: Make virtio_gpu_virgl_unmap_resource_blob() return -1 on error

In reply to Dmitry Osipenko

Make virtio_gpu_virgl_unmap_resource_blob() return -1 for more consistency of error propagation style in the code, adhering to QEMU's devel/style recommendations and preparing code for further code changes utilizing this function.

---
 hw/display/virtio-gpu-virgl.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
hw/display/virtio-gpu-virgl.c +3 -3
index edcdad0af232..a7b14a312c83 100644
@@ -199,7 +199,7 @@ virtio_gpu_virgl_unmap_resource_blob(VirtIOGPU *g,
             qemu_log_mask(LOG_GUEST_ERROR,
                           "%s: failed to unmap virgl resource: %s\n",
                           __func__, strerror(-ret));
-            return ret;
+            return -1;
         }
     } else {
         *cmd_suspended = true;
@@ -333,7 +333,7 @@ static void virgl_cmd_resource_unref(VirtIOGPU *g,
     }
 
 #if VIRGL_VERSION_MAJOR >= 1
-    if (virtio_gpu_virgl_unmap_resource_blob(g, res, cmd_suspended)) {
+    if (virtio_gpu_virgl_unmap_resource_blob(g, res, cmd_suspended) < 0) {
         cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
         return;
     }
@@ -829,7 +829,7 @@ static void virgl_cmd_resource_unmap_blob(VirtIOGPU *g,
     }
 
     ret = virtio_gpu_virgl_unmap_resource_blob(g, res, cmd_suspended);
-    if (ret) {
+    if (ret < 0) {
         cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
         return;
     }
Signature · 1 line
2.51.1
#10 lore raw

Re: [RFC PATCH v4 4/7] virtio-gpu: Make virtio_gpu_virgl_unmap_resource_blob() return -1 on error

Quoted 4 lines · Dmitry Osipenko

Make virtio_gpu_virgl_unmap_resource_blob() return -1 for more consistency of error propagation style in the code, adhering to QEMU's devel/style recommendations and preparing code for further code changes utilizing this function.

I'm not so sure of that. If the function is a pass/fail then we tend to prefer using bools in newer code. If you need richer internal error reporting then start using your errno defines. If this is user visible (i.e. during configuration) then we can make more of Error* and friends.

Quoted 37 lines
---
 hw/display/virtio-gpu-virgl.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
hw/display/virtio-gpu-virgl.c +3 -3
index edcdad0af232..a7b14a312c83 100644
@@ -199,7 +199,7 @@ virtio_gpu_virgl_unmap_resource_blob(VirtIOGPU *g,
             qemu_log_mask(LOG_GUEST_ERROR,
                           "%s: failed to unmap virgl resource: %s\n",
                           __func__, strerror(-ret));
-            return ret;
+            return -1;
         }
     } else {
         *cmd_suspended = true;
@@ -333,7 +333,7 @@ static void virgl_cmd_resource_unref(VirtIOGPU *g,
     }
 
 #if VIRGL_VERSION_MAJOR >= 1
-    if (virtio_gpu_virgl_unmap_resource_blob(g, res, cmd_suspended)) {
+    if (virtio_gpu_virgl_unmap_resource_blob(g, res, cmd_suspended) < 0) {
         cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
         return;
     }
@@ -829,7 +829,7 @@ static void virgl_cmd_resource_unmap_blob(VirtIOGPU *g,
     }
 
     ret = virtio_gpu_virgl_unmap_resource_blob(g, res, cmd_suspended);
-    if (ret) {
+    if (ret < 0) {
         cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
         return;
     }
Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#11 lore raw

Re: [RFC PATCH v4 4/7] virtio-gpu: Make virtio_gpu_virgl_unmap_resource_blob() return -1 on error

Quoted 10 lines · Alex Bennée
Quoted 4 lines · Dmitry Osipenko

Make virtio_gpu_virgl_unmap_resource_blob() return -1 for more consistency of error propagation style in the code, adhering to QEMU's devel/style recommendations and preparing code for further code changes utilizing this function.

I'm not so sure of that. If the function is a pass/fail then we tend to prefer using bools in newer code. If you need richer internal error reporting then start using your errno defines. If this is user visible (i.e. during configuration) then we can make more of Error* and friends.

The current code is a mix of all variants. Will be good to stick with one.

Akihiko, what are your thoughts on the best option for the errors handling? Would you also prefer bools?

Signature · 2 lines
Best regards,
Dmitry
#12 lore raw

Re: [RFC PATCH v4 4/7] virtio-gpu: Make virtio_gpu_virgl_unmap_resource_blob() return -1 on error

Quoted 12 lines · Dmitry Osipenko
Quoted 10 lines · Alex Bennée
Quoted 4 lines · Dmitry Osipenko

Make virtio_gpu_virgl_unmap_resource_blob() return -1 for more consistency of error propagation style in the code, adhering to QEMU's devel/style recommendations and preparing code for further code changes utilizing this function.

I'm not so sure of that. If the function is a pass/fail then we tend to prefer using bools in newer code. If you need richer internal error reporting then start using your errno defines. If this is user visible (i.e. during configuration) then we can make more of Error* and friends.

Quoted 4 lines

The current code is a mix of all variants. Will be good to stick with one.

Akihiko, what are your thoughts on the best option for the errors handling? Would you also prefer bools?

Sorry, I missed this.

I'm for bools. It allows compilers to enforce having either of two values, whose semantics is defined in include/qapi/error.h "true on success / false on failure"; it says functions should also have "Error **errp" but I think the semantics of bool can be applied for those without the parameter.

If you are still not sure or there are disagreements, I think it is better to ask Markus Armbruster, who maintains the error reporting facility shared for the whole codebase.

Regards,
Akihiko Odaki

#13 lore raw

Re: [RFC PATCH v4 4/7] virtio-gpu: Make virtio_gpu_virgl_unmap_resource_blob() return -1 on error

Quoted 29 lines · Akihiko Odaki
Quoted 14 lines · Dmitry Osipenko
Quoted 8 lines · Alex Bennée
Quoted 6 lines · Dmitry Osipenko

Make virtio_gpu_virgl_unmap_resource_blob() return -1 for more consistency
of error propagation style in the code, adhering to QEMU's devel/style recommendations and preparing code for further code changes utilizing this
function.

Quoted 4 lines

I'm not so sure of that. If the function is a pass/fail then we tend to prefer using bools in newer code. If you need richer internal error reporting then start using your errno defines. If this is user visible (i.e. during configuration) then we can make more of Error* and friends.

Quoted 5 lines

The current code is a mix of all variants. Will be good to stick with one.

Akihiko, what are your thoughts on the best option for the errors handling? Would you also prefer bools?

Sorry, I missed this.

I'm for bools. It allows compilers to enforce having either of two values, whose semantics is defined in include/qapi/error.h "true on success / false on failure"; it says functions should also have "Error **errp" but I think the semantics of bool can be applied for those without the parameter.

Quoted 3 lines

If you are still not sure or there are disagreements, I think it is better to ask Markus Armbruster, who maintains the error reporting facility shared for the whole codebase.

Thanks for the reply. The bools are fine with me. I removed patches changing the error handling in a next version of this patchset as they are a bit controversial and not essential for this series.

Signature · 2 lines
Best regards,
Dmitry
#14 lore raw

RFC PATCH v4 5/7 virtio-gpu: Destroy virgl resources on virtio-gpu reset

In reply to Dmitry Osipenko

Properly destroy virgl resources on virtio-gpu reset to not leak resources on a hot reboot of a VM.

---
 hw/display/virtio-gpu-gl.c     |  6 ++-
 hw/display/virtio-gpu-virgl.c  | 83 +++++++++++++++++++++++++---------
 include/hw/virtio/virtio-gpu.h |  5 +-
 3 files changed, 71 insertions(+), 23 deletions(-)
hw/display/virtio-gpu-gl.c +5 -1
index b640900fc6f1..bca7d489c1e3 100644
@@ -72,7 +72,10 @@ static void virtio_gpu_gl_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
 
     switch (gl->renderer_state) {
     case RS_RESET:
-        virtio_gpu_virgl_reset(g);
+        if (virtio_gpu_virgl_reset(g) < 0) {
+            gl->renderer_state = RS_INIT_FAILED;
+            return;
+        }
         /* fallthrough */
     case RS_START:
         if (virtio_gpu_virgl_init(g)) {
@@ -201,6 +204,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 +62 -21

114 lines not on this page · raw message

include/hw/virtio/virtio-gpu.h +4 -1
index 718332284305..9e1473d1bb66 100644
@@ -389,9 +389,12 @@ 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);
 
 #endif
Signature · 1 line
2.51.1
#15 lore raw

RFC PATCH v4 6/7 virtio-gpu: Make virtio_gpu_virgl_init() return -1 on error

In reply to Dmitry Osipenko

Make virtio_gpu_virgl_init() return -1 on error to make it consistent with virtio_gpu_virgl_reset() in regards to error handling codding style, adhering to QEMU's devel/style recommendations.

---
 hw/display/virtio-gpu-gl.c    | 2 +-
 hw/display/virtio-gpu-virgl.c | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
hw/display/virtio-gpu-gl.c +1 -1
index bca7d489c1e3..d65da4863923 100644
@@ -78,7 +78,7 @@ static void virtio_gpu_gl_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
         }
         /* fallthrough */
     case RS_START:
-        if (virtio_gpu_virgl_init(g)) {
+        if (virtio_gpu_virgl_init(g) < 0) {
             gl->renderer_state = RS_INIT_FAILED;
             return;
         }
hw/display/virtio-gpu-virgl.c +2 -2
index 91951c3ffb0a..9b36b378c2fd 100644
@@ -1371,7 +1371,7 @@ int virtio_gpu_virgl_init(VirtIOGPU *g)
              * Async-fence was bugged in virglrenderer versions <= 1.1.1.
              */
             error_report("drm requires egl display and virglrenderer >= 1.2.0");
-            return -EINVAL;
+            return -1;
         }
     }
 #endif
@@ -1379,7 +1379,7 @@ int virtio_gpu_virgl_init(VirtIOGPU *g)
     ret = virgl_renderer_init(g, flags, &virtio_gpu_3d_cbs);
     if (ret != 0) {
         error_report("virgl could not be initialized: %d", ret);
-        return ret;
+        return -1;
     }
 
     gl->fence_poll = timer_new_ms(QEMU_CLOCK_VIRTUAL,
Signature · 1 line
2.51.1
#16 lore raw

Re: [RFC PATCH v4 6/7] virtio-gpu: Make virtio_gpu_virgl_init() return -1 on error

Quoted 3 lines · Dmitry Osipenko

Make virtio_gpu_virgl_init() return -1 on error to make it consistent with virtio_gpu_virgl_reset() in regards to error handling codding style, adhering to QEMU's devel/style recommendations.

See previous comment on other -1 returns.

Quoted 42 lines
---
 hw/display/virtio-gpu-gl.c    | 2 +-
 hw/display/virtio-gpu-virgl.c | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
hw/display/virtio-gpu-gl.c +1 -1
index bca7d489c1e3..d65da4863923 100644
@@ -78,7 +78,7 @@ static void virtio_gpu_gl_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
         }
         /* fallthrough */
     case RS_START:
-        if (virtio_gpu_virgl_init(g)) {
+        if (virtio_gpu_virgl_init(g) < 0) {
             gl->renderer_state = RS_INIT_FAILED;
             return;
         }
hw/display/virtio-gpu-virgl.c +2 -2
index 91951c3ffb0a..9b36b378c2fd 100644
@@ -1371,7 +1371,7 @@ int virtio_gpu_virgl_init(VirtIOGPU *g)
              * Async-fence was bugged in virglrenderer versions <= 1.1.1.
              */
             error_report("drm requires egl display and virglrenderer >= 1.2.0");
-            return -EINVAL;
+            return -1;
         }
     }
 #endif
@@ -1379,7 +1379,7 @@ int virtio_gpu_virgl_init(VirtIOGPU *g)
     ret = virgl_renderer_init(g, flags, &virtio_gpu_3d_cbs);
     if (ret != 0) {
         error_report("virgl could not be initialized: %d", ret);
-        return ret;
+        return -1;
     }
 
     gl->fence_poll = timer_new_ms(QEMU_CLOCK_VIRTUAL,
Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#17 lore raw

RFC PATCH v4 7/7 virtio-gpu: Support mapping hostmem blobs with map_fixed

In reply to Dmitry Osipenko

Support 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     | 41 +++++++++++++++++++++++-
 hw/display/virtio-gpu-virgl.c  | 58 +++++++++++++++++++++++++++++++++-
 include/hw/virtio/virtio-gpu.h |  3 ++
 3 files changed, 100 insertions(+), 2 deletions(-)
hw/display/virtio-gpu-gl.c +40 -1
index d65da4863923..8a7a33946085 100644
@@ -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"
@@ -124,7 +125,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");
@@ -155,6 +161,28 @@ 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(errp,
+                       "virgl hostmem region could not be initialized: %s",
+                       strerror(errno));
+            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[] = {
@@ -190,6 +218,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 +57 -1

91 lines not on this page · raw message

include/hw/virtio/virtio-gpu.h +3 -0
index 9e1473d1bb66..420c6e2a2515 100644
@@ -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.51.1
#18 lore raw

Re: [RFC PATCH v4 7/7] 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_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     | 41 +++++++++++++++++++++++-
  hw/display/virtio-gpu-virgl.c  | 58 +++++++++++++++++++++++++++++++++-
  include/hw/virtio/virtio-gpu.h |  3 ++
  3 files changed, 100 insertions(+), 2 deletions(-)
hw/display/virtio-gpu-gl.c +9 -1
index d65da4863923..8a7a33946085 100644
@@ -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"
@@ -124,7 +125,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");
@@ -155,6 +161,28 @@ 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 175

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(errp,
+                       "virgl hostmem region could not be initialized: %s",
+                       strerror(errno));

error_setg_errno() will make this a bit simpler.

Quoted 145 lines
hw/display/virtio-gpu-gl.c +21 -0
+            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[] = {
@@ -190,6 +218,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 +57 -1
index 9b36b378c2fd..987980e8a49d 100644
@@ -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 *
@@ -116,6 +120,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;
@@ -134,6 +141,41 @@ virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g,
          return -EINVAL;
      }
 
+#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 -1;
+    }
+#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",
@@ -146,7 +188,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
@@ -173,6 +215,20 @@ 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) {
+            error_report("%s: failed to unmap(fixed) virgl resource: %s",
+                          __func__, strerror(errno));
+            return -1;
+        }
+
+        res->map_fixed = NULL;
+    }
+#endif
+
      if (!mr) {
          return 0;
      }
include/hw/virtio/virtio-gpu.h +3 -0
index 9e1473d1bb66..420c6e2a2515 100644
@@ -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 {