[PATCH 1/3] virtio-gpu: Add support for VIRTIO_GPU_BLOB_FLAG_USE_USERPTR flag

Honglei Huang posted 3 patches 1 month ago
Maintainers: "Michael S. Tsirkin" <mst@redhat.com>, "Alex Bennée" <alex.bennee@linaro.org>, Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp>, Dmitry Osipenko <dmitry.osipenko@collabora.com>, Cornelia Huck <cohuck@redhat.com>, Paolo Bonzini <pbonzini@redhat.com>
There is a newer version of this series
[PATCH 1/3] virtio-gpu: Add support for VIRTIO_GPU_BLOB_FLAG_USE_USERPTR flag
Posted by Honglei Huang 1 month ago
Add support for the USE_USERPTR blob flag in virtio-gpu to enable
user pointer mapping for blob resources. This allows guest applications
to use user-allocated memory for GPU resources more efficiently.

Changes include:
- Add VIRTIO_GPU_BLOB_FLAG_USE_USERPTR flag definition
- Enhance blob resource creation to handle userptr flag properly
- Remove arbitrary nr_entries limit (16384) in mapping creation
- Add conditional handling for userptr vs regular blob mapping
- Support guest_blob_mapped parameter for virgl renderer

This enables more flexible memory management between guest and host
for GPU virtualization scenarios.

Signed-off-by: Honglei Huang <honghuan@amd.com>
---
 hw/display/virtio-gpu-virgl.c               | 21 +++++++++++++++------
 hw/display/virtio-gpu.c                     |  7 -------
 include/standard-headers/linux/virtio_gpu.h |  1 +
 3 files changed, 16 insertions(+), 13 deletions(-)

diff --git a/hw/display/virtio-gpu-virgl.c b/hw/display/virtio-gpu-virgl.c
index 07f6355ad6..9da64bf16f 100644
--- a/hw/display/virtio-gpu-virgl.c
+++ b/hw/display/virtio-gpu-virgl.c
@@ -702,12 +702,21 @@ static void virgl_cmd_resource_create_blob(VirtIOGPU *g,
     res->base.dmabuf_fd = -1;
 
     if (cblob.blob_mem != VIRTIO_GPU_BLOB_MEM_HOST3D) {
-        ret = virtio_gpu_create_mapping_iov(g, cblob.nr_entries, sizeof(cblob),
-                                            cmd, &res->base.addrs,
-                                            &res->base.iov, &res->base.iov_cnt);
-        if (!ret) {
-            cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
-            return;
+        if (cblob.blob_flags & VIRTIO_GPU_BLOB_FLAG_USE_USERPTR) {
+            ret = virtio_gpu_create_mapping_iov(g, cblob.nr_entries, sizeof(cblob), cmd, &res->base.addrs,
+                                                &res->base.iov, &res->base.iov_cnt);
+            if (ret != 0) {
+                cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
+                return;
+            }
+        } else {
+            ret = virtio_gpu_create_mapping_iov(g, cblob.nr_entries, sizeof(cblob),
+                                                cmd, &res->base.addrs,
+                                                &res->base.iov, &res->base.iov_cnt);
+            if (!ret) {
+                cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
+                return;
+            }
         }
     }
 
diff --git a/hw/display/virtio-gpu.c b/hw/display/virtio-gpu.c
index 43e88a4daf..956dc811fa 100644
--- a/hw/display/virtio-gpu.c
+++ b/hw/display/virtio-gpu.c
@@ -808,13 +808,6 @@ int virtio_gpu_create_mapping_iov(VirtIOGPU *g,
     size_t esize, s;
     int e, v;
 
-    if (nr_entries > 16384) {
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: nr_entries is too big (%d > 16384)\n",
-                      __func__, nr_entries);
-        return -1;
-    }
-
     esize = sizeof(*ents) * nr_entries;
     ents = g_malloc(esize);
     s = iov_to_buf(cmd->elem.out_sg, cmd->elem.out_num,
diff --git a/include/standard-headers/linux/virtio_gpu.h b/include/standard-headers/linux/virtio_gpu.h
index 00cd3f04af..b85e781a2d 100644
--- a/include/standard-headers/linux/virtio_gpu.h
+++ b/include/standard-headers/linux/virtio_gpu.h
@@ -405,6 +405,7 @@ struct virtio_gpu_resource_create_blob {
 #define VIRTIO_GPU_BLOB_FLAG_USE_MAPPABLE     0x0001
 #define VIRTIO_GPU_BLOB_FLAG_USE_SHAREABLE    0x0002
 #define VIRTIO_GPU_BLOB_FLAG_USE_CROSS_DEVICE 0x0004
+#define VIRTIO_GPU_BLOB_FLAG_USE_USERPTR      0x0008
 	/* zero is invalid blob mem */
 	uint32_t blob_mem;
 	uint32_t blob_flags;
-- 
2.34.1
Re: [PATCH 1/3] virtio-gpu: Add support for VIRTIO_GPU_BLOB_FLAG_USE_USERPTR flag
Posted by Akihiko Odaki 1 month ago
On 2025/11/12 16:54, Honglei Huang wrote:
> Add support for the USE_USERPTR blob flag in virtio-gpu to enable
> user pointer mapping for blob resources. This allows guest applications
> to use user-allocated memory for GPU resources more efficiently.
> 
> Changes include:
> - Add VIRTIO_GPU_BLOB_FLAG_USE_USERPTR flag definition
> - Enhance blob resource creation to handle userptr flag properly
> - Remove arbitrary nr_entries limit (16384) in mapping creation
> - Add conditional handling for userptr vs regular blob mapping
> - Support guest_blob_mapped parameter for virgl renderer
> 
> This enables more flexible memory management between guest and host
> for GPU virtualization scenarios.
> 
> Signed-off-by: Honglei Huang <honghuan@amd.com>
> ---
>   hw/display/virtio-gpu-virgl.c               | 21 +++++++++++++++------
>   hw/display/virtio-gpu.c                     |  7 -------
>   include/standard-headers/linux/virtio_gpu.h |  1 +
>   3 files changed, 16 insertions(+), 13 deletions(-)
> 
> diff --git a/hw/display/virtio-gpu-virgl.c b/hw/display/virtio-gpu-virgl.c
> index 07f6355ad6..9da64bf16f 100644
> --- a/hw/display/virtio-gpu-virgl.c
> +++ b/hw/display/virtio-gpu-virgl.c
> @@ -702,12 +702,21 @@ static void virgl_cmd_resource_create_blob(VirtIOGPU *g,
>       res->base.dmabuf_fd = -1;
>   
>       if (cblob.blob_mem != VIRTIO_GPU_BLOB_MEM_HOST3D) {
> -        ret = virtio_gpu_create_mapping_iov(g, cblob.nr_entries, sizeof(cblob),
> -                                            cmd, &res->base.addrs,
> -                                            &res->base.iov, &res->base.iov_cnt);
> -        if (!ret) {
> -            cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
> -            return;
> +        if (cblob.blob_flags & VIRTIO_GPU_BLOB_FLAG_USE_USERPTR) {
> +            ret = virtio_gpu_create_mapping_iov(g, cblob.nr_entries, sizeof(cblob), cmd, &res->base.addrs,
> +                                                &res->base.iov, &res->base.iov_cnt);
> +            if (ret != 0) {
> +                cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
> +                return;
> +            }
> +        } else {
> +            ret = virtio_gpu_create_mapping_iov(g, cblob.nr_entries, sizeof(cblob),
> +                                                cmd, &res->base.addrs,
> +                                                &res->base.iov, &res->base.iov_cnt);
> +            if (!ret) {

Why does this check !ret instead of ret != 0?

Regards,
Akihiko Odaki

> +                cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
> +                return;
> +            }
>           }
>       }
>   
> diff --git a/hw/display/virtio-gpu.c b/hw/display/virtio-gpu.c
> index 43e88a4daf..956dc811fa 100644
> --- a/hw/display/virtio-gpu.c
> +++ b/hw/display/virtio-gpu.c
> @@ -808,13 +808,6 @@ int virtio_gpu_create_mapping_iov(VirtIOGPU *g,
>       size_t esize, s;
>       int e, v;
>   
> -    if (nr_entries > 16384) {
> -        qemu_log_mask(LOG_GUEST_ERROR,
> -                      "%s: nr_entries is too big (%d > 16384)\n",
> -                      __func__, nr_entries);
> -        return -1;
> -    }
> -
>       esize = sizeof(*ents) * nr_entries;
>       ents = g_malloc(esize);
>       s = iov_to_buf(cmd->elem.out_sg, cmd->elem.out_num,
> diff --git a/include/standard-headers/linux/virtio_gpu.h b/include/standard-headers/linux/virtio_gpu.h
> index 00cd3f04af..b85e781a2d 100644
> --- a/include/standard-headers/linux/virtio_gpu.h
> +++ b/include/standard-headers/linux/virtio_gpu.h
> @@ -405,6 +405,7 @@ struct virtio_gpu_resource_create_blob {
>   #define VIRTIO_GPU_BLOB_FLAG_USE_MAPPABLE     0x0001
>   #define VIRTIO_GPU_BLOB_FLAG_USE_SHAREABLE    0x0002
>   #define VIRTIO_GPU_BLOB_FLAG_USE_CROSS_DEVICE 0x0004
> +#define VIRTIO_GPU_BLOB_FLAG_USE_USERPTR      0x0008
>   	/* zero is invalid blob mem */
>   	uint32_t blob_mem;
>   	uint32_t blob_flags;
Re: [PATCH 1/3] virtio-gpu: Add support for VIRTIO_GPU_BLOB_FLAG_USE_USERPTR flag
Posted by Honglei1.Huang@amd.com 1 month ago

On 2025/11/12 16:44, Akihiko Odaki wrote:
> On 2025/11/12 16:54, Honglei Huang wrote:
>> Add support for the USE_USERPTR blob flag in virtio-gpu to enable
>> user pointer mapping for blob resources. This allows guest applications
>> to use user-allocated memory for GPU resources more efficiently.
>>
>> Changes include:
>> - Add VIRTIO_GPU_BLOB_FLAG_USE_USERPTR flag definition
>> - Enhance blob resource creation to handle userptr flag properly
>> - Remove arbitrary nr_entries limit (16384) in mapping creation
>> - Add conditional handling for userptr vs regular blob mapping
>> - Support guest_blob_mapped parameter for virgl renderer
>>
>> This enables more flexible memory management between guest and host
>> for GPU virtualization scenarios.
>>
>> Signed-off-by: Honglei Huang <honghuan@amd.com>
>> ---
>>   hw/display/virtio-gpu-virgl.c               | 21 +++++++++++++++------
>>   hw/display/virtio-gpu.c                     |  7 -------
>>   include/standard-headers/linux/virtio_gpu.h |  1 +
>>   3 files changed, 16 insertions(+), 13 deletions(-)
>>
>> diff --git a/hw/display/virtio-gpu-virgl.c b/hw/display/virtio-gpu- 
>> virgl.c
>> index 07f6355ad6..9da64bf16f 100644
>> --- a/hw/display/virtio-gpu-virgl.c
>> +++ b/hw/display/virtio-gpu-virgl.c
>> @@ -702,12 +702,21 @@ static void 
>> virgl_cmd_resource_create_blob(VirtIOGPU *g,
>>       res->base.dmabuf_fd = -1;
>>       if (cblob.blob_mem != VIRTIO_GPU_BLOB_MEM_HOST3D) {
>> -        ret = virtio_gpu_create_mapping_iov(g, cblob.nr_entries, 
>> sizeof(cblob),
>> -                                            cmd, &res->base.addrs,
>> -                                            &res->base.iov, &res- 
>> >base.iov_cnt);
>> -        if (!ret) {
>> -            cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
>> -            return;
>> +        if (cblob.blob_flags & VIRTIO_GPU_BLOB_FLAG_USE_USERPTR) {
>> +            ret = virtio_gpu_create_mapping_iov(g, cblob.nr_entries, 
>> sizeof(cblob), cmd, &res->base.addrs,
>> +                                                &res->base.iov, &res- 
>> >base.iov_cnt);
>> +            if (ret != 0) {
>> +                cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
>> +                return;
>> +            }
>> +        } else {
>> +            ret = virtio_gpu_create_mapping_iov(g, cblob.nr_entries, 
>> sizeof(cblob),
>> +                                                cmd, &res->base.addrs,
>> +                                                &res->base.iov, &res- 
>> >base.iov_cnt);
>> +            if (!ret) {
> 
> Why does this check !ret instead of ret != 0?
> 
> Regards,
> Akihiko Odaki
> 
>> +                cmd->error = VIRTIO_GPU_RESP_ERR_UNSPEC;
>> +                return;
>> +            }
>>           }
>>       }
>> diff --git a/hw/display/virtio-gpu.c b/hw/display/virtio-gpu.c
>> index 43e88a4daf..956dc811fa 100644
>> --- a/hw/display/virtio-gpu.c
>> +++ b/hw/display/virtio-gpu.c
>> @@ -808,13 +808,6 @@ int virtio_gpu_create_mapping_iov(VirtIOGPU *g,
>>       size_t esize, s;
>>       int e, v;
>> -    if (nr_entries > 16384) {
>> -        qemu_log_mask(LOG_GUEST_ERROR,
>> -                      "%s: nr_entries is too big (%d > 16384)\n",
>> -                      __func__, nr_entries);
>> -        return -1;
>> -    }
>> -
>>       esize = sizeof(*ents) * nr_entries;
>>       ents = g_malloc(esize);
>>       s = iov_to_buf(cmd->elem.out_sg, cmd->elem.out_num,
>> diff --git a/include/standard-headers/linux/virtio_gpu.h b/include/ 
>> standard-headers/linux/virtio_gpu.h
>> index 00cd3f04af..b85e781a2d 100644
>> --- a/include/standard-headers/linux/virtio_gpu.h
>> +++ b/include/standard-headers/linux/virtio_gpu.h
>> @@ -405,6 +405,7 @@ struct virtio_gpu_resource_create_blob {
>>   #define VIRTIO_GPU_BLOB_FLAG_USE_MAPPABLE     0x0001
>>   #define VIRTIO_GPU_BLOB_FLAG_USE_SHAREABLE    0x0002
>>   #define VIRTIO_GPU_BLOB_FLAG_USE_CROSS_DEVICE 0x0004
>> +#define VIRTIO_GPU_BLOB_FLAG_USE_USERPTR      0x0008
>>       /* zero is invalid blob mem */
>>       uint32_t blob_mem;
>>       uint32_t blob_flags;
> 


Hi Akihiko,

Thank you for the review!
You're absolutely right. I need to maintain a consistent code style.
I'll fix this in v2 along with making both check consistent.

Regards,
Honglei