virtio-gpu: Add userptr support for compute workloads
Subsystems (4)
v7 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 strip them from the CREATE_BLOB wire command
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.
V7 changes:
- Mask guest-only DRM flags out of CREATE_BLOB wire blob_flags
- Clear userptr->pages after pin failure to avoid double-free
- DMA-map userptr SG only when virtio_gpu_use_dma_api() is required
- Use DMA_TO_DEVICE for USERPTR_RDONLY
- Reject USERPTR unless blob_mem is VIRTGPU_BLOB_MEM_GUEST
- Note that CAPSET_ROCM uses ID 8 because ID 7 is taken by VIRCL
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 | 37 +++ drivers/gpu/drm/virtio/virtgpu_ioctl.c | 25 +- drivers/gpu/drm/virtio/virtgpu_object.c | 10 + drivers/gpu/drm/virtio/virtgpu_userptr.c | 335 +++++++++++++++++++++++ drivers/gpu/drm/virtio/virtgpu_vq.c | 6 +- include/uapi/drm/virtgpu_drm.h | 9 + include/uapi/linux/virtio_gpu.h | 1 + 8 files changed, 422 insertions(+), 4 deletions(-) create mode 100644 drivers/gpu/drm/virtio/virtgpu_userptr.c
base-commit: 766bfba0f3bae329f99b42dcabc3ef11fa368f0b
Signature · 1 line
2.34.1
PATCH v7 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.
ID 7 is already used by VIRCL in the virgl/virtio-gpu stack, so ROCm uses ID 8. A matching virtio-gpu specification change is in progress to register this assignment.
--- 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 v7 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] virtio: speculative UAPI capability set ID assignment --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] virtio: speculative UAPI capability set ID assignment
--
commit 45781e0bfdae22c4f82ae57badc273b48904b478
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 support ROCm compute workloads in virtualized environments using ID 8. The commit message notes that a matching virtio-gpu specification change is currently in progress.
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]
Is it safe to speculatively assign a UAPI capability set ID while the virtio specification change is still in progress?
If the virtio technical committee assigns ID 8 to a different capability set or assigns a different ID to ROCm, the Linux kernel UAPI will permanently diverge from the official OASIS standard, which could break interoperability with spec-compliant hypervisors.
Should this UAPI addition wait until the specification assignment is formally accepted?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v7 2/4 drm/virtgpu api: add blob userptr resource
In reply to Honglei HuangAdd 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, so mask them out in virtio_gpu_cmd_resource_create_blob().
--- drivers/gpu/drm/virtio/virtgpu_vq.c | 6 +++++- include/uapi/drm/virtgpu_drm.h | 9 +++++++++ 2 files changed, 14 insertions(+), 1 deletion(-)
drivers/gpu/drm/virtio/virtgpu_vq.c +5 -1
@@ -1494,7 +1494,11 @@ virtio_gpu_cmd_resource_create_blob(struct virtio_gpu_device *vgdev, cmd_p->hdr.ctx_id =cpu_to_le32 (params->ctx_id); cmd_p->resource_id =cpu_to_le32 (bo->hw_res_handle); cmd_p->blob_mem =cpu_to_le32 (params->blob_mem); - cmd_p->blob_flags =cpu_to_le32 (params->blob_flags); +/* Guest-only flags stay off the wire. */ + cmd_p->blob_flags =cpu_to_le32 (params->blob_flags & + (VIRTIO_GPU_BLOB_FLAG_USE_MAPPABLE | +VIRTIO_GPU_BLOB_FLAG_USE_SHAREABLE | +VIRTIO_GPU_BLOB_FLAG_USE_CROSS_DEVICE )); cmd_p->blob_id =cpu_to_le64 (params->blob_id); cmd_p->size =cpu_to_le64 (params->size); cmd_p->nr_entries =cpu_to_le32 (nents);
include/uapi/drm/virtgpu_drm.h +9 -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,12 @@ 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. +*/ +__u64 userptr; };#define VIRTGPU_CONTEXT_PARAM_CAPSET_ID 0x0001
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v7 2/4] drm/virtgpu api: add blob userptr resource
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines
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-onlyVIRTGPU_BLOB_FLAG_USE_USERPTRandVIRTGPU_BLOB_FLAG_USERPTR_RDONLYflags
- Add a userptr field todrm_virtgpu_resource_create_blob
Quoted 5 lines
These flags select guest pin behavior in the ioctl. They are not part of the virtio
CREATE_BLOBwire ABI, so mask them out invirtio_gpu_cmd_resource_create_blob().Signed-off-by Honglei Huang <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH v7 3/4 drm/virtio: implement userptr support for zero-copy memory access
In reply to Honglei HuangAdd 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 only when virtio_gpu_use_dma_api() is required; use DMA_TO_DEVICE for USERPTR_RDONLY
- Keep pages pinned until RESOURCE_UNREF is queued; drop them from cleanup_object() on the unref response or on create failure
- Clear userptr->pages on pin failure to avoid double-free on cleanup
- 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 | 37 +++ drivers/gpu/drm/virtio/virtgpu_object.c | 10 + drivers/gpu/drm/virtio/virtgpu_userptr.c | 335 +++++++++++++++++++++++ 4 files changed, 384 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 +37 -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,42 @@ 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; +enum dma_data_direction dma_dir; +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 +593,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 +335 -0
335 lines not on this page · raw message
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v7 3/4] drm/virtio: implement userptr support for zero-copy memory access
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Critical] mm: silent data loss due to missing dirty tracking on unpin - [Critical] virtgpu: privilege…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Critical] mm: silent data loss due to missing dirty tracking on unpin
- [Critical] virtgpu: privilege escalation via read-write PRIME export of read-only userptrs
- [High] virtgpu: missing DMA sync for userptr objects causes stale reads
--
commit 419406d5baa93528ec030b7b95cc7d5ada190751
Author: Honglei Huang <[email protected]>
drm/virtio: implement userptr support for zero-copy memory access
This commit implements userptr support for virtio-gpu by adding a new virtio_gpu_object_userptr type. It pins user pages via pin_user_pages_fast() and maps them via the DMA API when required.
Quoted 6 lines
drivers/gpu/drm/virtio/virtgpu_userptr.c new file +0
-0@@ -0,0 +1,335 @@
[ ... ]
drivers/gpu/drm/virtio/virtgpu_userptr.c line 43
Quoted 7 lines
drivers/gpu/drm/virtio/virtgpu_userptr.c new file +7
-0+static const struct drm_gem_object_funcs virtio_gpu_userptr_funcs = { + .open = virtio_gpu_gem_object_open, + .close = virtio_gpu_gem_object_close, + .free = virtio_gpu_userptr_free, + .export = virtgpu_gem_prime_export, + .get_sg_table = virtio_gpu_userptr_get_sg_table, +};
[Severity: Critical]
Is it safe to expose these objects to the PRIME export subsystem without verifying their read-only status?
If a userptr object is created with VIRTGPU_BLOB_FLAG_USERPTR_RDONLY to pin read-only memory, userspace could potentially export it via PRIME with O_RDWR permissions and import it into another device that performs DMA writes. Could this allow hardware to bypass CPU page table protections and overwrite read-only system memory or files?
[ ... ]
Quoted 3 lines
drivers/gpu/drm/virtio/virtgpu_userptr.c new file +3
-0+static void +virtio_gpu_userptr_put_pages (struct virtio_gpu_object_userptr *userptr) +{
[ ... ]
drivers/gpu/drm/virtio/virtgpu_userptr.c line 120
Quoted 5 lines
drivers/gpu/drm/virtio/virtgpu_userptr.c new file +5
-0+if (userptr->pages) { +unpin_user_pages (userptr->pages, userptr->npages); +kvfree (userptr->pages); + userptr->pages =NULL ; + }
[Severity: Critical]
Are we missing dirty tracking when releasing these pages back to the OS?
If a userptr object is created without the read-only flag, pinned with FOLL_WRITE, written to by the device, and then destroyed, we call unpin_user_pages() here without explicitly marking the pages as dirty. If the pages are file-backed or swapped, could the kernel memory management subsystem silently discard the data written by the host or device?
[ ... ]
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 314
Quoted 13 lines
drivers/gpu/drm/virtio/virtgpu_userptr.c new file +13
-0+/* +* Match shmem blobs: only DMA-map when the virtio DMA API is in +* use. Mapping unconditionally can create SWIOTLB bounce buffers +* that get copied back over guest pages on unmap even though the +* host was given sg_phys() addresses. +*/ +if (virtio_gpu_use_dma_api (vgdev->vdev)) { +enum dma_data_direction dir = + (userptr->flags &VIRTGPU_BLOB_FLAG_USERPTR_RDONLY ) ? +DMA_TO_DEVICE :DMA_BIDIRECTIONAL ; + + ret =dma_map_sgtable (drm_dev_dma_dev (vgdev->ddev), sgt, + dir,0 );
[Severity: High]
How does this DMA-mapped memory get synchronized to avoid stale reads?
When mapping with DMA_BIDIRECTIONAL and the guest modifies the userptr memory, the host might read stale data from bounce buffers unless a DMA sync occurs. However, in virtio_gpu_cmd_transfer_to_host_3d() and 2d transfers, the sync check looks like this:
if (virtio_gpu_is_shmem(bo) && use_dma_api) dma_sync_sgtable_for_device(vgdev->vdev->dev.parent, bo->base.sgt, DMA_TO_DEVICE);
Since virtio_gpu_is_shmem() evaluates to false for userptr objects, they appear to be excluded from this synchronization. Will this skip cause the host to read stale data during transfers?
Quoted 6 lines
drivers/gpu/drm/virtio/virtgpu_userptr.c new file +6
-0+if (ret) + goto err_cleanup; + + userptr->dma_dir = dir; + userptr->dma_mapped = true; + }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH v7 4/4 drm/virtio: wire blob ioctl creation to userptr objects
In reply to Honglei HuangIntegrate 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
- Reject USERPTR unless blob_mem is VIRTGPU_BLOB_MEM_GUEST
- 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 | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-)
drivers/gpu/drm/virtio/virtgpu_ioctl.c +23 -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;
@@ -472,6 +485,11 @@ static int verify_blob(struct virtio_gpu_device *vgdev,return -EINVAL ; } +/* Userptr is only defined for pure guest backing. */ +if ((rc_blob->blob_flags &VIRTGPU_BLOB_FLAG_USE_USERPTR ) && + rc_blob->blob_mem !=VIRTGPU_BLOB_MEM_GUEST ) +return -EINVAL ; +if (*host3d_blob) {if (!vgdev->has_virgl_3d)return -EINVAL ;
@@ -495,6 +513,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 +557,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 v7 4/4] drm/virtio: wire blob ioctl creation to userptr objects
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Critical] drm/virtio: PRIME export of RDONLY userptr allows writable DMA mapping --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] drm/virtio: PRIME export of RDONLY userptr allows writable DMA mapping --
commit 5d368a790c7066062061e9164c64d6e00b653f96
Author: Honglei Huang <[email protected]>
Subject: drm/virtio: wire blob ioctl creation to userptr objects
This commit integrates userptr support into the virtio GPU blob resource creation ioctl. It adds userptr flags to the allowed mask, handles userptr verification, and calls virtio_gpu_userptr_create() when requested.
drivers/gpu/drm/virtio/virtgpu_ioctl.c line 41
Quoted 12 lines
drivers/gpu/drm/virtio/virtgpu_ioctl.c +3
-1@@ -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)
[Severity: Critical]
By exposing VIRTGPU_BLOB_FLAG_USERPTR_RDONLY to userspace, does this allow an unprivileged local user to bypass write protection on read-only pages via PRIME export?
Looking at virtio_gpu_userptr_funcs in drivers/gpu/drm/virtio/virtgpu_userptr.c, .export is exposed for userptr objects:
drivers/gpu/drm/virtio/virtgpu_userptr.c:
static const struct drm_gem_object_funcs virtio_gpu_userptr_funcs = {
.open = virtio_gpu_gem_object_open,
.close = virtio_gpu_gem_object_close,
.free = virtio_gpu_userptr_free,
.export = virtgpu_gem_prime_export,
.get_sg_table = virtio_gpu_userptr_get_sg_table,
};
And in virtio_gpu_userptr_get_pages() within the same file, it only pins with FOLL_WRITE if read-only is not set:
drivers/gpu/drm/virtio/virtgpu_userptr.c:virtio_gpu_userptr_get_pages()
if (!(userptr->flags & VIRTGPU_BLOB_FLAG_USERPTR_RDONLY)) flag |= FOLL_WRITE;
If a user maps a read-only file into memory, creates a virtgpu blob resource using VIRTGPU_BLOB_FLAG_USE_USERPTR and VIRTGPU_BLOB_FLAG_USERPTR_RDONLY, and then exports it via DRM_IOCTL_PRIME_HANDLE_TO_FD while requesting DRM_RDWR, does this allow the user to import this dma-buf into another device and perform DMA writes to it?
This appears to circumvent the read-only pinning constraints when the resulting sg_table is shared via PRIME and modified by an unaware importer, potentially leading to arbitrary code execution or local privilege escalation.
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4