virtio-gpu: Add userptr support for compute workloads
Subsystems (4)
v6 cover virtio-gpu: Add userptr support for compute workloads
Hello,
This series adds virtio-gpu userptr support for ROCm native compute contexts. The guest kernel pins an existing userspace mapping with FOLL_LONGTERM and exposes it to the host as ordinary CREATE_BLOB backing entries, avoiding a second shmem allocation and memcpy.
USE_USERPTR and USERPTR_RDONLY are guest-only DRM ioctl selectors. They are not added to virtio_gpu.h; the device does not need a new wire flag.
Patches overview:
1. Add VIRTIO_GPU_CAPSET_ROCM capability for compute workloads
2. Extend DRM UAPI with guest-only userptr flags and userptr field
3. Implement core userptr functionality with page management
4. Wire blob ioctl creation to userptr objects
Tests:
- Full OPENCL CTS tests passed on ROCm 5.7.0 in V2000 platform.
- Near 70% percentage of OPENCL CTS tests passed on ROCm 7.0 W7900 platform.
- most HIP catch tests passed on ROCm 7.0 W7900 platform.
- Some AI applications enabled on ROCm 7.0 W7900 platform.
- latest ROCm 7.14 and ROCm 10 testing is ongoing.
V6 changes:
- Rebase onto drm-misc-next
- Keep USE_USERPTR / USERPTR_RDONLY as guest-only DRM flags; drop the virtio wire-header patch (5 patches down to 4)
- Fix userptr lifetime, DMA mapping, memlock accounting, alignment checks, and PRIME SG export
- Updated corresponding cover letter and commit messages
V5 changes:
- Add VIRTIO_GPU_BLOB_FLAG_USERPTR_RDONLY definition to patch 2
- Dropped unused VIRTIO_GPU_F_RESOURCE_USERPTR feature bit in patch 2
- Included VIRTIO_GPU_BLOB_FLAG_USERPTR_RDONLY in VIRTGPU_BLOB_FLAG_USE_MASK in patch 5
- Add check for userptr feature in patch 5 before creating userptr blob resource
- Updated corresponding cover letter and commit messages
V4 changes:
- Renamed VIRTIO_GPU_CAPSET_HSAKMT to VIRTIO_GPU_CAPSET_ROCM
- Remove userptr feature probing cause it can reuse the guest blob resource code path, reduce patch count from 6 to 5
- Updated corresponding commit messages
- Consolidated userptr feature detection in final patch
- Update corresponding cover letter content
V3 changes:
- Split into focused patches for easier review
- Removed complex interval tree userptr management
- Simplified resource creation without deduplication
- Added VIRTGPU_PARAM_RESOURCE_USERPTR for feature detection
- Improved UAPI documentation and error handling
- Enhanced code quality with proper cleanup paths
- Removed MMU notifier dependencies for simplicity
- Fixed resource lifecycle management issues
V2: - Split add HSAKMT context and blob userptr resource to two patches.
- Remove MMU notifier related patches, cause use not moveable user space memory with MMU notifier is not a good idea.
- Remove HSAKMT context check when create context, let all the context support the userptr feature.
- Remove MMU notifier related content in cover letter.
- Add more comments for patch 6 in cover letter.
Previous version:
https://lore.kernel.org/dri-devel/[email protected]/
Honglei Huang (4):
drm/virtio-gpu: Add VIRTIO_GPU_CAPSET_ROCM capability drm/virtgpu api: add blob userptr resource
drm/virtio: implement userptr support for zero-copy memory access drm/virtio: wire blob ioctl creation to userptr objects
drivers/gpu/drm/virtio/Makefile | 3 +- drivers/gpu/drm/virtio/virtgpu_drv.h | 36 +++ drivers/gpu/drm/virtio/virtgpu_ioctl.c | 20 +- drivers/gpu/drm/virtio/virtgpu_object.c | 10 + drivers/gpu/drm/virtio/virtgpu_userptr.c | 323 +++++++++++++++++++++++ include/uapi/drm/virtgpu_drm.h | 10 + include/uapi/linux/virtio_gpu.h | 1 + 7 files changed, 400 insertions(+), 3 deletions(-) create mode 100644 drivers/gpu/drm/virtio/virtgpu_userptr.c
base-commit: 766bfba0f3bae329f99b42dcabc3ef11fa368f0b
Signature · 1 line
2.34.1
PATCH v6 1/4 drm/virtio-gpu: Add VIRTIO_GPU_CAPSET_ROCM capability
Add a new GPU capability set VIRTIO_GPU_CAPSET_ROCM to support ROCm compute workloads in virtualized environments.
ROCm (Radeon Open Compute) is AMD's open-source software platform for GPU compute and HPC workloads.
--- include/uapi/linux/virtio_gpu.h | 1 + 1 file changed, 1 insertion(+)
include/uapi/linux/virtio_gpu.h +1 -0
@@ -321,6 +321,7 @@ struct virtio_gpu_cmd_submit {#define VIRTIO_GPU_CAPSET_VENUS 4#define VIRTIO_GPU_CAPSET_CROSS_DOMAIN 5#define VIRTIO_GPU_CAPSET_DRM 6 +#define VIRTIO_GPU_CAPSET_ROCM 8/* VIRTIO_GPU_CMD_GET_CAPSET_INFO */ struct virtio_gpu_get_capset_info {
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v6 1/4] drm/virtio-gpu: Add VIRTIO_GPU_CAPSET_ROCM capability
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] uapi: VIRTIO_GPU_CAPSET_ROCM defines unstandardized capset ID 8 --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] uapi: VIRTIO_GPU_CAPSET_ROCM defines unstandardized capset ID 8 --
commit ddbf02d8a1557a4976371db4a94be88bc1f7052f
Author: Honglei Huang <[email protected]>
drm/virtio-gpu: Add VIRTIO_GPU_CAPSET_ROCM capability
This commit adds a new GPU capability set VIRTIO_GPU_CAPSET_ROCM to the virtio gpu UAPI header to support ROCm compute workloads in virtualized environments.
include/uapi/linux/virtio_gpu.h struct virtio_gpu_cmd_submit { · line 324
Quoted 9 lines
include/uapi/linux/virtio_gpu.h +1
-0@@ -321,6 +321,7 @@ struct virtio_gpu_cmd_submit {#define VIRTIO_GPU_CAPSET_VENUS 4#define VIRTIO_GPU_CAPSET_CROSS_DOMAIN 5#define VIRTIO_GPU_CAPSET_DRM 6 +#define VIRTIO_GPU_CAPSET_ROCM 8
[Severity: Medium]
Does allocating this ID without specification standardization risk permanent ABI collisions?
If the virtio standard later assigns ID 8 to another capability, this UAPI addition could cause a conflict. Furthermore, skipping ID 7 leaves an undocumented gap that might conflict with virtio standardization protocols. Has ID 8 been allocated in the upstream virtio specification?
Quoted 3 lines
include/uapi/linux/virtio_gpu.h +0
-0/* VIRTIO_GPU_CMD_GET_CAPSET_INFO */ struct virtio_gpu_get_capset_info {
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
RE: [PATCH v6 1/4] drm/virtio-gpu: Add VIRTIO_GPU_CAPSET_ROCM capability
AMD General
Capset ID 7 is already used by VIRCL in the virgl/virtio-gpu stack So ROCm use ID 8, and we have created a pr in spec to reserve the ID8
-----Original Message-----
From: [email protected] <[email protected]>
Sent: Thursday, September 17, 2026 6:34 PM
To: Huang, Honglei1 <[email protected]>
Cc: Michael S. Tsirkin <[email protected]>; [email protected]; Eugenio Perez <[email protected]>; [email protected]
Subject: Re: [PATCH v6 1/4] drm/virtio-gpu: Add VIRTIO_GPU_CAPSET_ROCM capability
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] uapi: VIRTIO_GPU_CAPSET_ROCM defines unstandardized capset ID 8 --
commit ddbf02d8a1557a4976371db4a94be88bc1f7052f
Author: Honglei Huang <[email protected]>
drm/virtio-gpu: Add VIRTIO_GPU_CAPSET_ROCM capability
This commit adds a new GPU capability set VIRTIO_GPU_CAPSET_ROCM to the virtio gpu UAPI header to support ROCm compute workloads in virtualized environments.
Quoted 9 lines
(quoted diff) +0
-0
b/include/uapi/linux/virtio_gpu.hindex4f530d90058c7..3d4dfadc9d789100644include/uapi/linux/virtio_gpu.h +0
-0@@ -321,6 +321,7 @@ struct virtio_gpu_cmd_submit { #define
VIRTIO_GPU_CAPSET_VENUS4 #defineVIRTIO_GPU_CAPSET_CROSS_DOMAIN5 #defineVIRTIO_GPU_CAPSET_DRM6
+#defineVIRTIO_GPU_CAPSET_ROCM8
[Severity: Medium]
Does allocating this ID without specification standardization risk permanent ABI collisions?
If the virtio standard later assigns ID 8 to another capability, this UAPI addition could cause a conflict. Furthermore, skipping ID 7 leaves an undocumented gap that might conflict with virtio standardization protocols. Has ID 8 been allocated in the upstream virtio specification?
Quoted 3 lines
/*
VIRTIO_GPU_CMD_GET_CAPSET_INFO*/
structvirtio_gpu_get_capset_info{
--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v6 2/4 drm/virtgpu api: add blob userptr resource
In reply to Honglei HuangFrom: Honglei Huang <[email protected]>
Add DRM ioctl flags and a userptr address field so userspace can request a blob backed by an existing process mapping.
UAPI changes:
- Add guest-only VIRTGPU_BLOB_FLAG_USE_USERPTR and VIRTGPU_BLOB_FLAG_USERPTR_RDONLY flags
- Add a userptr field to drm_virtgpu_resource_create_blob
These flags select guest pin behavior in the ioctl. They are not part of the virtio CREATE_BLOB wire ABI.
--- include/uapi/drm/virtgpu_drm.h | 10 ++++++++++ 1 file changed, 10 insertions(+)
include/uapi/drm/virtgpu_drm.h +10 -0
@@ -186,6 +186,9 @@ struct drm_virtgpu_resource_create_blob {#define VIRTGPU_BLOB_FLAG_USE_MAPPABLE 0x0001#define VIRTGPU_BLOB_FLAG_USE_SHAREABLE 0x0002#define VIRTGPU_BLOB_FLAG_USE_CROSS_DEVICE 0x0004 +/* Guest-only flags */ +#define VIRTGPU_BLOB_FLAG_USE_USERPTR 0x0008 +#define VIRTGPU_BLOB_FLAG_USERPTR_RDONLY 0x0010/* zero is invalid blob_mem */ __u32 blob_mem;__u32 blob_flags;
@@ -205,6 +208,13 @@ struct drm_virtgpu_resource_create_blob {#define DRM_VIRTGPU_BLOB_FLAG_HINT_DEFER_MAPPING 0x0001__u32 blob_hints;__u32 pad2; + +/* +* userptr: guest userspace memory address for VIRTGPU_BLOB_FLAG_USE_USERPTR. +* Must be 0 if VIRTGPU_BLOB_FLAG_USE_USERPTR is not set. +* The driver will pin the user pages and allow the host to access them. +*/ +__u64 userptr; };#define VIRTGPU_CONTEXT_PARAM_CAPSET_ID 0x0001
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v6 2/4] drm/virtgpu api: add blob userptr resource
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] virtio-gpu: guest-only blob flags leaked to host breaking wire ABI --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] virtio-gpu: guest-only blob flags leaked to host breaking wire ABI --
commit ce150cb1a68528e491d40b28c03376311a653c0c
Author: Honglei Huang <[email protected]>
drm/virtgpu api: add blob userptr resource
This commit adds DRM ioctl flags and a userptr field to allow userspace to request a blob backed by an existing process mapping. It introduces guest-only flags VIRTGPU_BLOB_FLAG_USE_USERPTR and VIRTGPU_BLOB_FLAG_USERPTR_RDONLY for guest pin behavior.
include/uapi/drm/virtgpu_drm.h struct drm_virtgpu_resource_create_blob { · line 191
Quoted 11 lines
include/uapi/drm/virtgpu_drm.h +3
-0@@ -186,6 +186,9 @@ struct drm_virtgpu_resource_create_blob {#define VIRTGPU_BLOB_FLAG_USE_MAPPABLE 0x0001#define VIRTGPU_BLOB_FLAG_USE_SHAREABLE 0x0002#define VIRTGPU_BLOB_FLAG_USE_CROSS_DEVICE 0x0004 +/* Guest-only flags */ +#define VIRTGPU_BLOB_FLAG_USE_USERPTR 0x0008 +#define VIRTGPU_BLOB_FLAG_USERPTR_RDONLY 0x0010
[Severity: High]
Do these guest-only flags leak to the host and break the virtio wire ABI?
When userspace calls the DRM_IOCTL_VIRTGPU_RESOURCE_CREATE_BLOB ioctl, verify_blob() assigns rc_blob->blob_flags directly to params->blob_flags. This struct is passed to virtio_gpu_userptr_create(), which calls virtio_gpu_cmd_resource_create_blob().
In virtio_gpu_cmd_resource_create_blob(), the flags are written directly to the command without masking:
drivers/gpu/drm/virtio/virtgpu_vq.c:virtio_gpu_cmd_resource_create_blob() {
...
cmd_p->blob_flags = cpu_to_le32(params->blob_flags);
...
}If these guest-only flags are not masked out before being sent to the host, will strict hosts reject the command with an error, making the userptr feature unusable?
I noticed this wire ABI violation appears to remain unfixed at the end of the patch series (commit 357076bf581c).
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
RE: [PATCH v6 2/4] drm/virtgpu api: add blob userptr resource
AMD General
This part need some input from maintainer, these flags do not seem to have effect on the QEMU side.
-----Original Message-----
From: [email protected] <[email protected]>
Sent: Thursday, September 17, 2026 6:35 PM
To: Huang, Honglei1 <[email protected]>
Cc: Michael S. Tsirkin <[email protected]>; [email protected]; [email protected]; Eugenio Perez <[email protected]>
Subject: Re: [PATCH v6 2/4] drm/virtgpu api: add blob userptr resource
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] virtio-gpu: guest-only blob flags leaked to host breaking wire ABI --
commit ce150cb1a68528e491d40b28c03376311a653c0c
Author: Honglei Huang <[email protected]>
drm/virtgpu api: add blob userptr resource
This commit adds DRM ioctl flags and a userptr field to allow userspace to request a blob backed by an existing process mapping. It introduces guest-only flags VIRTGPU_BLOB_FLAG_USE_USERPTR and VIRTGPU_BLOB_FLAG_USERPTR_RDONLY for guest pin behavior.
include/uapi/drm/virtgpu_drm.h struct drm_virtgpu_resource_create_blob { · line 191
Quoted 12 lines
(quoted diff) +0
-0
b/include/uapi/drm/virtgpu_drm.hindex95587e12aed57..6171b4e2300b1100644include/uapi/drm/virtgpu_drm.h +3
-0@@ -186,6 +186,9 @@ struct drm_virtgpu_resource_create_blob {#define VIRTGPU_BLOB_FLAG_USE_MAPPABLE 0x0001#define VIRTGPU_BLOB_FLAG_USE_SHAREABLE 0x0002#define VIRTGPU_BLOB_FLAG_USE_CROSS_DEVICE 0x0004 +/* Guest-only flags */ +#define VIRTGPU_BLOB_FLAG_USE_USERPTR 0x0008 +#define VIRTGPU_BLOB_FLAG_USERPTR_RDONLY 0x0010
[Severity: High]
Do these guest-only flags leak to the host and break the virtio wire ABI?
When userspace calls the DRM_IOCTL_VIRTGPU_RESOURCE_CREATE_BLOB ioctl,verify_blob() assigns rc_blob->blob_flags directly to params->blob_flags.
This struct is passed to virtio_gpu_userptr_create(), which calls virtio_gpu_cmd_resource_create_blob().
In virtio_gpu_cmd_resource_create_blob(), the flags are written directly to the command without masking:
drivers/gpu/drm/virtio/virtgpu_vq.c:virtio_gpu_cmd_resource_create_blob() {
...
cmd_p->blob_flags = cpu_to_le32(params->blob_flags);
...
}If these guest-only flags are not masked out before being sent to the host, will strict hosts reject the command with an error, making the userptr feature unusable?
I noticed this wire ABI violation appears to remain unfixed at the end of the patch series (commit 357076bf581c).
[ ... ]
--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH v6 3/4 drm/virtio: implement userptr support for zero-copy memory access
In reply to Honglei HuangFrom: Honglei Huang <[email protected]>
Add userptr blob objects so the guest kernel can pin an existing userspace mapping and advertise it as CREATE_BLOB backing entries.
- New virtio_gpu_object_userptr type for userptr resources
- Pin pages with pin_user_pages_fast() and FOLL_LONGTERM
- Charge FOLL_LONGTERM pins against RLIMIT_MEMLOCK
- DMA-map the scatterlist and pass DMA addresses when the virtio DMA API is required
- Keep pages pinned until RESOURCE_UNREF is queued; drop them from cleanup_object() on the unref response or on create failure
- Reject unaligned or overflowing userptr ranges at create time
- Return a fresh SG table from PRIME export
--- drivers/gpu/drm/virtio/Makefile | 3 +- drivers/gpu/drm/virtio/virtgpu_drv.h | 36 +++ drivers/gpu/drm/virtio/virtgpu_object.c | 10 + drivers/gpu/drm/virtio/virtgpu_userptr.c | 323 +++++++++++++++++++++++ 4 files changed, 371 insertions(+), 1 deletion(-) create mode 100644 drivers/gpu/drm/virtio/virtgpu_userptr.c
drivers/gpu/drm/virtio/Makefile +2 -1
@@ -6,6 +6,7 @@ virtio-gpu-y := virtgpu_drv.o virtgpu_kms.o virtgpu_gem.o virtgpu_vram.o \ virtgpu_display.o virtgpu_vq.o \ virtgpu_fence.o virtgpu_object.o virtgpu_debugfs.o virtgpu_plane.o \ - virtgpu_ioctl.o virtgpu_prime.o virtgpu_trace_points.o virtgpu_submit.o + virtgpu_ioctl.o virtgpu_prime.o virtgpu_trace_points.o virtgpu_submit.o \ + virtgpu_userptr.o obj-$(CONFIG_DRM_VIRTIO_GPU) += virtio-gpu.o
drivers/gpu/drm/virtio/virtgpu_drv.h +36 -0
@@ -105,6 +105,7 @@ struct virtio_gpu_object_params {uint32_t blob_flags;uint64_t blob_id;uint32_t blob_hints; +uint64_t userptr; };struct virtio_gpu_object {
@@ -138,12 +139,41 @@ struct virtio_gpu_object_vram {struct drm_mm_node vram_node; }; +struct virtio_gpu_object_userptr ; + +struct virtio_gpu_object_userptr_ops { +int (*get_pages)(struct virtio_gpu_object_userptr *userptr); +void (*put_pages)(struct virtio_gpu_object_userptr *userptr); +}; + +struct virtio_gpu_object_userptr { +struct virtio_gpu_object base; +const struct virtio_gpu_object_userptr_ops *ops; +/* Protects pages and sgt. */ +struct mutex lock; + +uint64_t start; +uint32_t npages; +uint32_t bo_handle; +uint32_t flags; + +struct virtio_gpu_device *vgdev; +struct drm_file *file; +struct page **pages; +struct sg_table *sgt; +bool dma_mapped; +struct mm_struct *mm; +}; +#define to_virtio_gpu_shmem (virtio_gpu_object) \ container_of((virtio_gpu_object), struct virtio_gpu_object_shmem, base)#define to_virtio_gpu_vram (virtio_gpu_object) \ container_of((virtio_gpu_object), struct virtio_gpu_object_vram, base) +#define to_virtio_gpu_userptr (virtio_gpu_object) \ + container_of((virtio_gpu_object), struct virtio_gpu_object_userptr, base) +struct virtio_gpu_object_array {struct ww_acquire_ctx ticket;struct list_head next;
@@ -562,4 +592,10 @@ void virtio_gpu_vram_map_deferred(struct virtio_gpu_object_vram *vram);int virtio_gpu_execbuffer_ioctl (struct drm_device *dev,void *data,struct drm_file *file); +/* virtgpu_userptr.c */ +int virtio_gpu_userptr_create (struct virtio_gpu_device *vgdev, +struct drm_file *file, +struct virtio_gpu_object_params *params, +struct virtio_gpu_object **bo_ptr); +bool virtio_gpu_is_userptr (struct virtio_gpu_object *bo);#endif
drivers/gpu/drm/virtio/virtgpu_object.c +10 -0
@@ -91,6 +91,16 @@ void virtio_gpu_cleanup_object(struct virtio_gpu_object *bo)drm_gem_free_mmap_offset (&vram->base.base.base);drm_gem_object_release (&vram->base.base.base);kfree (vram); + }else if (virtio_gpu_is_userptr (bo)) { +struct virtio_gpu_object_userptr *userptr = +to_virtio_gpu_userptr (bo); + +mutex_lock (&userptr->lock); + userptr->ops->put_pages (userptr); +mutex_unlock (&userptr->lock); +mutex_destroy (&userptr->lock); +drm_gem_object_release (&userptr->base.base.base); +kfree (userptr); }else {drm_gem_object_release (&bo->base.base);kfree (bo);
drivers/gpu/drm/virtio/virtgpu_userptr.c new file +323 -0
323 lines not on this page · raw message
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v6 3/4] drm/virtio: implement userptr support for zero-copy memory access
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Critical] drm/virtio: missing pointer clear in virtio_gpu_userptr_get_pages() - [Critical] drm/virtio…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] drm/virtio: missing pointer clear in virtio_gpu_userptr_get_pages()
- [Critical] drm/virtio: unconditional dma_map_sgtable() causes data corruption --
commit 6e836cf8f7dd384050e500d6dbf47a69c2767c26
Author: Honglei Huang <[email protected]>
Subject: drm/virtio: implement userptr support for zero-copy memory access
This commit adds userptr blob objects so the guest kernel can pin an existing userspace mapping and advertise it as CREATE_BLOB backing entries. It pins pages using pin_user_pages_fast() and maps the scatterlist for the device.
Quoted 5 lines
drivers/gpu/drm/virtio/virtgpu_userptr.c new file +0
-0@@ -0,0 +1,323 @@
[ ... ]
Quoted 3 lines
drivers/gpu/drm/virtio/virtgpu_userptr.c new file +3
-0+static int +virtio_gpu_userptr_get_pages (struct virtio_gpu_object_userptr *userptr) +{
[ ... ]
drivers/gpu/drm/virtio/virtgpu_userptr.c line 80
Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_userptr.c new file +10
-0+ ret =pin_user_pages_fast (userptr->start + pinned *PAGE_SIZE , + num_pages, flag, + userptr->pages + pinned); + +if (ret <0 ) { +if (pinned) +unpin_user_pages (userptr->pages, pinned); +kvfree (userptr->pages); +return ret; + }
[Severity: Critical]
Does this error path leave a dangling pointer? The userptr->pages array is freed here but not set to NULL.
If pin_user_pages_fast() fails, this bubbles up to virtio_gpu_userptr_create() which jumps to err_cleanup and calls virtio_gpu_cleanup_object(). That function ultimately invokes virtio_gpu_userptr_put_pages():
if (userptr->pages) {
unpin_user_pages(userptr->pages, userptr->npages);
kvfree(userptr->pages);
userptr->pages = NULL;
}Because userptr->pages wasn't cleared, won't this result in a use-after-free and double-free when the cleanup function executes?
[ ... ]
Quoted 7 lines
drivers/gpu/drm/virtio/virtgpu_userptr.c new file +7
-0+static int +virtio_gpu_userptr_get_entries (struct virtio_gpu_device *vgdev, +struct virtio_gpu_object_userptr *userptr, +struct virtio_gpu_mem_entry **ents, +unsigned int *nents) +{ +bool use_dma_api =virtio_gpu_use_dma_api (vgdev->vdev);
[ ... ]
Quoted 13 lines
drivers/gpu/drm/virtio/virtgpu_userptr.c new file +13
-0+if (use_dma_api) { +for_each_sgtable_dma_sg (userptr->sgt, sg, si) { + (*ents)[si].addr =cpu_to_le64 (sg_dma_address (sg)); + (*ents)[si].length =cpu_to_le32 (sg_dma_len (sg)); + (*ents)[si].padding =0 ; + } + }else { +for_each_sgtable_sg (userptr->sgt, sg, si) { + (*ents)[si].addr =cpu_to_le64 (sg_phys (sg)); + (*ents)[si].length =cpu_to_le32 (sg->length); + (*ents)[si].padding =0 ; + } + }
[ ... ]
Quoted 5 lines
drivers/gpu/drm/virtio/virtgpu_userptr.c new file +5
-0+int virtio_gpu_userptr_create (struct virtio_gpu_device *vgdev, +struct drm_file *file, +struct virtio_gpu_object_params *params, +struct virtio_gpu_object **bo_ptr) +{
[ ... ]
drivers/gpu/drm/virtio/virtgpu_userptr.c line 300
Quoted 9 lines
drivers/gpu/drm/virtio/virtgpu_userptr.c new file +9
-0+ sgt =drm_prime_pages_to_sg (vgdev->ddev, userptr->pages, + userptr->npages); +if (IS_ERR (sgt)) { + ret =PTR_ERR (sgt); + goto err_cleanup; + } + + ret =dma_map_sgtable (drm_dev_dma_dev (vgdev->ddev), sgt, +DMA_BIDIRECTIONAL ,0 );
[Severity: Critical]
Can this unconditional DMA mapping cause silent data corruption?
If the system uses SWIOTLB bounce buffers and the device is configured to bypass the DMA API, the dma_map_sgtable() call will allocate a bounce buffer.
Later in virtio_gpu_userptr_get_entries(), the code branches based on virtio_gpu_use_dma_api(), and if false it ignores the DMA address and passes the raw physical address (sg_phys) to the device.
Since the raw physical address is passed to the device when DMA is bypassed, the device will write directly to the original physical memory. However, during cleanup, dma_unmap_sgtable() with DMA_BIDIRECTIONAL will copy the unmodified bounce buffer back over the original memory. Will this overwrite and destroy the data written by the device?
Quoted 5 lines
drivers/gpu/drm/virtio/virtgpu_userptr.c new file +5
-0+if (ret) { +sg_free_table (sgt); +kfree (sgt); + goto err_cleanup; + }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
RE: [PATCH v6 3/4] drm/virtio: implement userptr support for zero-copy memory access
AMD General
Will fix it and waiting for maintainer's suggestions
-----Original Message-----
From: [email protected] <[email protected]>
Sent: Thursday, September 17, 2026 6:38 PM
To: Huang, Honglei1 <[email protected]>
Cc: [email protected]; [email protected]; Eugenio Perez <[email protected]>; Michael S. Tsirkin <[email protected]>
Subject: Re: [PATCH v6 3/4] drm/virtio: implement userptr support for zero-copy memory access
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] drm/virtio: missing pointer clear in virtio_gpu_userptr_get_pages()
- [Critical] drm/virtio: unconditional dma_map_sgtable() causes data corruption --
commit 6e836cf8f7dd384050e500d6dbf47a69c2767c26
Author: Honglei Huang <[email protected]>
Subject: drm/virtio: implement userptr support for zero-copy memory access
This commit adds userptr blob objects so the guest kernel can pin an existing userspace mapping and advertise it as CREATE_BLOB backing entries. It pins pages using pin_user_pages_fast() and maps the scatterlist for the device.
Quoted 6 lines
drivers/gpu/drm/virtio/virtgpu_userptr.c new file +0
-0@@ -0,0 +1,323 @@
[ ... ]
Quoted 3 lines
drivers/gpu/drm/virtio/virtgpu_userptr.c new file +3
-0+static int +virtio_gpu_userptr_get_pages (struct virtio_gpu_object_userptr +*userptr) {
[ ... ]
drivers/gpu/drm/virtio/virtgpu_userptr.c line 80
Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_userptr.c new file +10
-0+ ret =pin_user_pages_fast (userptr->start + pinned *PAGE_SIZE , + num_pages, flag, + userptr->pages + pinned); + +if (ret <0 ) { +if (pinned) +unpin_user_pages (userptr->pages, pinned); +kvfree (userptr->pages); +return ret; + }
[Severity: Critical]
Does this error path leave a dangling pointer? The userptr->pages array is freed here but not set to NULL.
If pin_user_pages_fast() fails, this bubbles up to virtio_gpu_userptr_create() which jumps to err_cleanup and calls virtio_gpu_cleanup_object(). That function ultimately invokes virtio_gpu_userptr_put_pages():
if (userptr->pages) {
unpin_user_pages(userptr->pages, userptr->npages);
kvfree(userptr->pages);
userptr->pages = NULL;
}Because userptr->pages wasn't cleared, won't this result in a use-after-free and double-free when the cleanup function executes?
[ ... ]
Quoted 7 lines
drivers/gpu/drm/virtio/virtgpu_userptr.c new file +7
-0+static int +virtio_gpu_userptr_get_entries (struct virtio_gpu_device *vgdev, +struct virtio_gpu_object_userptr *userptr, +struct virtio_gpu_mem_entry **ents, +unsigned int *nents) +{ +bool use_dma_api =virtio_gpu_use_dma_api (vgdev->vdev);
[ ... ]
Quoted 13 lines
drivers/gpu/drm/virtio/virtgpu_userptr.c new file +13
-0+if (use_dma_api) { +for_each_sgtable_dma_sg (userptr->sgt, sg, si) { + (*ents)[si].addr =cpu_to_le64 (sg_dma_address (sg)); + (*ents)[si].length =cpu_to_le32 (sg_dma_len (sg)); + (*ents)[si].padding =0 ; + } + }else { +for_each_sgtable_sg (userptr->sgt, sg, si) { + (*ents)[si].addr =cpu_to_le64 (sg_phys (sg)); + (*ents)[si].length =cpu_to_le32 (sg->length); + (*ents)[si].padding =0 ; + } + }
[ ... ]
Quoted 4 lines
drivers/gpu/drm/virtio/virtgpu_userptr.c new file +4
-0+int virtio_gpu_userptr_create (struct virtio_gpu_device *vgdev, +struct drm_file *file, +struct virtio_gpu_object_params *params, +struct virtio_gpu_object **bo_ptr) {
[ ... ]
drivers/gpu/drm/virtio/virtgpu_userptr.c line 300
Quoted 9 lines
drivers/gpu/drm/virtio/virtgpu_userptr.c new file +9
-0+ sgt =drm_prime_pages_to_sg (vgdev->ddev, userptr->pages, + userptr->npages); +if (IS_ERR (sgt)) { + ret =PTR_ERR (sgt); + goto err_cleanup; + } + + ret =dma_map_sgtable (drm_dev_dma_dev (vgdev->ddev), sgt, +DMA_BIDIRECTIONAL ,0 );
[Severity: Critical]
Can this unconditional DMA mapping cause silent data corruption?
If the system uses SWIOTLB bounce buffers and the device is configured to bypass the DMA API, the dma_map_sgtable() call will allocate a bounce buffer.
Later in virtio_gpu_userptr_get_entries(), the code branches based on virtio_gpu_use_dma_api(), and if false it ignores the DMA address and passes the raw physical address (sg_phys) to the device.
Since the raw physical address is passed to the device when DMA is bypassed, the device will write directly to the original physical memory. However, during cleanup, dma_unmap_sgtable() with DMA_BIDIRECTIONAL will copy the unmodified bounce buffer back over the original memory. Will this overwrite and destroy the data written by the device?
drivers/gpu/drm/virtio/virtgpu_userptr.c line 305
Quoted 5 lines
drivers/gpu/drm/virtio/virtgpu_userptr.c new file +5
-0+if (ret) { +sg_free_table (sgt); +kfree (sgt); + goto err_cleanup; + }
--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH v6 4/4 drm/virtio: wire blob ioctl creation to userptr objects
In reply to Honglei HuangFrom: Honglei Huang <[email protected]>
Integrate userptr into the blob resource creation ioctl.
- Include VIRTGPU_BLOB_FLAG_USE_USERPTR and VIRTGPU_BLOB_FLAG_USERPTR_RDONLY in VIRTGPU_BLOB_FLAG_USE_MASK
- Require userptr if and only if USE_USERPTR is set
- Pass the userspace address into the internal create params
- Call virtio_gpu_userptr_create() for userptr blob requests
--- drivers/gpu/drm/virtio/virtgpu_ioctl.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-)
drivers/gpu/drm/virtio/virtgpu_ioctl.c +18 -2
@@ -36,7 +36,9 @@#define VIRTGPU_BLOB_FLAG_USE_MASK (VIRTGPU_BLOB_FLAG_USE_MAPPABLE | \ VIRTGPU_BLOB_FLAG_USE_SHAREABLE | \ - VIRTGPU_BLOB_FLAG_USE_CROSS_DEVICE) + VIRTGPU_BLOB_FLAG_USE_CROSS_DEVICE | \ + VIRTGPU_BLOB_FLAG_USE_USERPTR | \ + VIRTGPU_BLOB_FLAG_USERPTR_RDONLY)/* Must be called with &virtio_gpu_fpriv.struct_mutex held. */ static void virtio_gpu_create_context_locked(struct virtio_gpu_device *vgdev,
@@ -458,6 +460,17 @@ static int verify_blob(struct virtio_gpu_device *vgdev,return -EINVAL ; } +if (rc_blob->blob_flags &VIRTGPU_BLOB_FLAG_USE_USERPTR ) { +if (!rc_blob->userptr) +return -EINVAL ; + }else { +if (rc_blob->userptr) +return -EINVAL ; + +if (rc_blob->blob_flags &VIRTGPU_BLOB_FLAG_USERPTR_RDONLY ) +return -EINVAL ; + } +switch (rc_blob->blob_mem) {case VIRTGPU_BLOB_MEM_GUEST : *guest_blob = true;
@@ -495,6 +508,7 @@ static int verify_blob(struct virtio_gpu_device *vgdev, params->blob = true; params->blob_flags = rc_blob->blob_flags; params->blob_hints = rc_blob->blob_hints; + params->userptr = rc_blob->userptr;if (vgdev->has_blob_alignment && !IS_ALIGNED (params->size, vgdev->blob_alignment))
@@ -538,8 +552,10 @@ static int virtio_gpu_resource_create_blob_ioctl(struct drm_device *dev, vfpriv->ctx_id, NULL, NULL); } -if (guest_blob) +if (guest_blob && !params.userptr) ret =virtio_gpu_object_create (vgdev, ¶ms, &bo,NULL ); +else if (guest_blob && params.userptr) + ret =virtio_gpu_userptr_create (vgdev, file, ¶ms, &bo);else if (!guest_blob && host3d_blob) ret =virtio_gpu_vram_create (vgdev, ¶ms, &bo);else
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v6 4/4] drm/virtio: wire blob ioctl creation to userptr objects
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Critical] drm/virtio: double-free and UAF in virtio_gpu_userptr_get_pages error path - [High]…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Critical] drm/virtio: double-free and UAF in virtio_gpu_userptr_get_pages error path
- [High] drm/virtio: read-only userptr memory mapped as DMA_BIDIRECTIONAL
- [Medium] drm/virtio: silent fallback to VRAM if userptr is combined with HOST3D --
commit 357076bf581ca76e53fbe71d8c16da04e304d01c
Author: Honglei Huang <[email protected]>
drm/virtio: wire blob ioctl creation to userptr objects
Integrate userptr into the blob resource creation ioctl.
Quoted 3 lines
drivers/gpu/drm/virtio/virtgpu_ioctl.c +0
-0
[ ... ]
Quoted 5 lines
drivers/gpu/drm/virtio/virtgpu_ioctl.c +1
-0@@ -458,6 +460,17 @@ static int verify_blob(struct virtio_gpu_device *vgdev,return -EINVAL ; } +if (rc_blob->blob_flags &VIRTGPU_BLOB_FLAG_USE_USERPTR ) {
drivers/gpu/drm/virtio/virtgpu_ioctl.c static int verify_blob(struct virtio_gpu_device *vgdev, · line 473
Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_ioctl.c +10
-0+if (!rc_blob->userptr) +return -EINVAL ; + }else { +if (rc_blob->userptr) +return -EINVAL ; + +if (rc_blob->blob_flags &VIRTGPU_BLOB_FLAG_USERPTR_RDONLY ) +return -EINVAL ; + } +
[Severity: Medium]
Will this silently fall back to VRAM if the user requests a userptr blob but sets the memory type to VIRTGPU_BLOB_MEM_HOST3D?
If guest_blob is false and host3d_blob is true, the userptr flag is validated here but it doesn't ensure a guest memory type was requested.
This could allow the execution to fall through into:
virtio_gpu_resource_create_blob_ioctl() {
...
else if (!guest_blob && host3d_blob)
ret = virtio_gpu_vram_create(vgdev, ¶ms, &bo);
...
}which would silently ignore params.userptr and allocate standard VRAM instead of returning -EINVAL.
Quoted 2 lines
drivers/gpu/drm/virtio/virtgpu_ioctl.c +0
-0switch (rc_blob->blob_mem) {case VIRTGPU_BLOB_MEM_GUEST :
[ ... ]
drivers/gpu/drm/virtio/virtgpu_ioctl.c static int virtio_gpu_resource_create_blob_ioctl(struct drm_device *dev, · line 558
Quoted 9 lines
drivers/gpu/drm/virtio/virtgpu_ioctl.c +3
-1@@ -538,8 +552,10 @@ static int virtio_gpu_resource_create_blob_ioctl(struct drm_device *dev, vfpriv->ctx_id, NULL, NULL); } -if (guest_blob) +if (guest_blob && !params.userptr) ret =virtio_gpu_object_create (vgdev, ¶ms, &bo,NULL ); +else if (guest_blob && params.userptr) + ret =virtio_gpu_userptr_create (vgdev, file, ¶ms, &bo);
[Severity: Critical]
Does the error path in virtio_gpu_userptr_get_pages() lead to a use-after-free and double-free when this is called?
If virtio_gpu_userptr_get_pages() enters the error path, it frees the pages array but leaves a dangling pointer:
virtio_gpu_userptr_get_pages() {
...
if (pinned)
unpin_user_pages(userptr->pages, pinned);
kvfree(userptr->pages);
return ret;
}The error returned to virtio_gpu_userptr_create() triggers a jump to err_cleanup and calls virtio_gpu_cleanup_object(), which eventually calls virtio_gpu_userptr_put_pages():
virtio_gpu_userptr_put_pages() {
if (userptr->pages) {
unpin_user_pages(userptr->pages, userptr->npages);
kvfree(userptr->pages);
userptr->pages = NULL;
}
}Because the pointer is not NULL, this would unpin and free the already freed array, potentially corrupting page refcounts and memory.
[Severity: High]
Can a read-only userptr be overwritten by the host device?
When VIRTGPU_BLOB_FLAG_USERPTR_RDONLY is specified, the userptr memory is pinned without FOLL_WRITE, allowing read-only memory to be pinned. However, the DMA mapping in virtio_gpu_userptr_create() appears to be hardcoded to bidirectional:
virtio_gpu_userptr_create() {
...
ret = dma_map_sgtable(drm_dev_dma_dev(vgdev->ddev), sgt,
DMA_BIDIRECTIONAL, 0);
...
}Because it is mapped bidirectionally, the IOMMU allows device writes to these physical pages. Can unprivileged userspace submit command buffers that instruct the host device to write into the blob, overwriting the read-only guest memory? Should this use DMA_TO_DEVICE when USERPTR_RDONLY is set?
Quoted 4 lines
drivers/gpu/drm/virtio/virtgpu_ioctl.c +2
-0+else if (!guest_blob && host3d_blob) + ret =virtio_gpu_vram_create (vgdev, ¶ms, &bo);else return -EINVAL ;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
RE: [PATCH v6 4/4] drm/virtio: wire blob ioctl creation to userptr objects
AMD General
Will fix it and waiting for maintainer's suggestions
-----Original Message-----
From: [email protected] <[email protected]>
Sent: Thursday, September 17, 2026 6:38 PM
To: Huang, Honglei1 <[email protected]>
Cc: Michael S. Tsirkin <[email protected]>; [email protected]; Eugenio Perez <[email protected]>; [email protected]
Subject: Re: [PATCH v6 4/4] drm/virtio: wire blob ioctl creation to userptr objects
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Critical] drm/virtio: double-free and UAF in virtio_gpu_userptr_get_pages error path
- [High] drm/virtio: read-only userptr memory mapped as DMA_BIDIRECTIONAL
- [Medium] drm/virtio: silent fallback to VRAM if userptr is combined with HOST3D --
commit 357076bf581ca76e53fbe71d8c16da04e304d01c
Author: Honglei Huang <[email protected]>
drm/virtio: wire blob ioctl creation to userptr objects
Integrate userptr into the blob resource creation ioctl.
Quoted 4 lines
drivers/gpu/drm/virtio/virtgpu_ioctl.c +0
-0
[ ... ]
Quoted 5 lines
drivers/gpu/drm/virtio/virtgpu_ioctl.c +1
-0@@ -458,6 +460,17 @@ static int verify_blob(struct virtio_gpu_device *vgdev,return -EINVAL ; } +if (rc_blob->blob_flags &VIRTGPU_BLOB_FLAG_USE_USERPTR ) {
drivers/gpu/drm/virtio/virtgpu_ioctl.c static int verify_blob(struct virtio_gpu_device *vgdev, · line 473
Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_ioctl.c +10
-0+if (!rc_blob->userptr) +return -EINVAL ; + }else { +if (rc_blob->userptr) +return -EINVAL ; + +if (rc_blob->blob_flags &VIRTGPU_BLOB_FLAG_USERPTR_RDONLY ) +return -EINVAL ; + } +
[Severity: Medium]
Will this silently fall back to VRAM if the user requests a userptr blob but sets the memory type to VIRTGPU_BLOB_MEM_HOST3D?
If guest_blob is false and host3d_blob is true, the userptr flag is validated here but it doesn't ensure a guest memory type was requested.
This could allow the execution to fall through into:
virtio_gpu_resource_create_blob_ioctl() {
...
else if (!guest_blob && host3d_blob)
ret = virtio_gpu_vram_create(vgdev, ¶ms, &bo);
...
}which would silently ignore params.userptr and allocate standard VRAM instead of returning -EINVAL.
Quoted 2 lines
drivers/gpu/drm/virtio/virtgpu_ioctl.c +0
-0switch (rc_blob->blob_mem) {case VIRTGPU_BLOB_MEM_GUEST :
[ ... ]
drivers/gpu/drm/virtio/virtgpu_ioctl.c static int virtio_gpu_resource_create_blob_ioctl(struct drm_device *dev, · line 558
Quoted 9 lines
drivers/gpu/drm/virtio/virtgpu_ioctl.c +3
-1@@ -538,8 +552,10 @@ static int virtio_gpu_resource_create_blob_ioctl(struct drm_device *dev, vfpriv->ctx_id, NULL, NULL); } -if (guest_blob) +if (guest_blob && !params.userptr) ret =virtio_gpu_object_create (vgdev, ¶ms, &bo,NULL ); +else if (guest_blob && params.userptr) + ret =virtio_gpu_userptr_create (vgdev, file, ¶ms, &bo);
[Severity: Critical]
Does the error path in virtio_gpu_userptr_get_pages() lead to a use-after-free and double-free when this is called?
If virtio_gpu_userptr_get_pages() enters the error path, it frees the pages array but leaves a dangling pointer:
virtio_gpu_userptr_get_pages() {
...
if (pinned)
unpin_user_pages(userptr->pages, pinned);
kvfree(userptr->pages);
return ret;
}The error returned to virtio_gpu_userptr_create() triggers a jump to err_cleanup and calls virtio_gpu_cleanup_object(), which eventually calls virtio_gpu_userptr_put_pages():
virtio_gpu_userptr_put_pages() {
if (userptr->pages) {
unpin_user_pages(userptr->pages, userptr->npages);
kvfree(userptr->pages);
userptr->pages = NULL;
}
}Because the pointer is not NULL, this would unpin and free the already freed array, potentially corrupting page refcounts and memory.
[Severity: High]
Can a read-only userptr be overwritten by the host device?
When VIRTGPU_BLOB_FLAG_USERPTR_RDONLY is specified, the userptr memory is pinned without FOLL_WRITE, allowing read-only memory to be pinned. However, the DMA mapping in virtio_gpu_userptr_create() appears to be hardcoded to bidirectional:
virtio_gpu_userptr_create() {
...
ret = dma_map_sgtable(drm_dev_dma_dev(vgdev->ddev), sgt,
DMA_BIDIRECTIONAL, 0);
...
}Because it is mapped bidirectionally, the IOMMU allows device writes to these physical pages. Can unprivileged userspace submit command buffers that instruct the host device to write into the blob, overwriting the read-only guest memory? Should this use DMA_TO_DEVICE when USERPTR_RDONLY is set?
drivers/gpu/drm/virtio/virtgpu_ioctl.c static int virtio_gpu_resource_create_blob_ioctl(struct drm_device *dev,
Quoted 4 lines
drivers/gpu/drm/virtio/virtgpu_ioctl.c +2
-0+else if (!guest_blob && host3d_blob) + ret =virtio_gpu_vram_create (vgdev, ¶ms, &bo);else return -EINVAL ;
--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4