drm/virtio: userptr blobs for native-context compute
Subsystems (4)
v10 cover drm/virtio: userptr blobs for native-context compute
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.
A non-zero userptr field selects that path. Probe VIRTGPU_PARAM_USERPTR before using the field so an older guest kernel rejects the request instead of silently creating a shmem blob.
See overall detail in virglrenderer pr [1]
Patches overview:
1. Add VIRTIO_GPU_CAPSET_ROCM capability for compute workloads
2. Extend DRM/virtio UAPI with userptr, PARAM_USERPTR, USE_READONLY, and F_BLOB_READONLY
3. Implement core userptr functionality with page management
4. Wire blob ioctl creation to userptr objects and probe the readonly feature
V10 changes:
- Require DMA_ATTR_REQUIRE_COHERENT on create and restore mappings.
- Use drm_gem_object_put() after UUID assignment or handle creation failure.
- Drop the new_pinned < 0 check.
- Validate the userptr address against the host's blob_alignment in verify_blob().
V9 changes:
- Marked series RFC pending a virtio-gpu spec update for F_BLOB_READONLY / BLOB_FLAG_USE_READONLY (Odaki)
- Moved VIRTIO_GPU_CAPSET_ROCM/VIRTGPU_DRM_CAPSET_ROCM from ID 8 to 10 to avoid the gfxstream/rutabaga_gfx 7-9 collision (Odaki)
- Added the missing VIRTGPU_DRM_CAPSET_ROCM alias (Odaki, sashiko-bot)
- Dropped the kernel/guest-internal wire-flag comment from the UAPI header and reworded "Guest VA" to "address" in the userptr field comment (Odaki)
- Fixed a BUG() on every virtio-gpu probe: VIRTIO_GPU_F_BLOB_READONLY was queried with virtio_has_feature() but never added to the driver's features[] array (Odaki)
- Removed the dead TRANSFER_TO_HOST userptr DMA-sync branches added to virtgpu_vq.c; verify_blob() already rejects VIRTGPU_BLOB_MEM_GUEST for that ioctl, so the branches were unreachable (Odaki)
- Documented the DMA mapping and bounce buffer behavior of virtio_gpu_userptr_restore() and dropped the zero-copy claim from the commit title and message (Odaki)
V8 changes:
- Drop guest-only USERPTR ioctl flags; a non-zero userptr field selects the path
- Add VIRTGPU_PARAM_USERPTR so older kernels do not silently ignore the new field
- Request GPU-readonly backing with virtio wire flag USE_READONLY
- Accept USE_READONLY and omit FOLL_WRITE only if the host advertised VIRTIO_GPU_F_BLOB_READONLY
- Restore userptr blobs after hibernation without the shmem path
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 readonly blobs
- Sync userptr SG for the device on TRANSFER_TO_HOST
- Mark writable pages dirty when unpinning
- Reject USERPTR unless blob_mem is VIRTGPU_BLOB_MEM_GUEST
- Disallow PRIME export of userptr objects
- 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
- 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.
[1]: https://gitlab.freedesktop.org/virgl/virglrenderer/-/merge_requests/1708
Honglei Huang (4):
drm/virtio: add VIRTIO_GPU_CAPSET_ROCM capability drm/virtio: add userptr blob resource to the UAPI drm/virtio: implement userptr resource support drm/virtio: wire blob ioctl creation to userptr objects
drivers/gpu/drm/virtio/Makefile | 3 +- drivers/gpu/drm/virtio/virtgpu_debugfs.c | 1 + drivers/gpu/drm/virtio/virtgpu_drv.c | 1 + drivers/gpu/drm/virtio/virtgpu_drv.h | 42 +++ drivers/gpu/drm/virtio/virtgpu_ioctl.c | 49 +++- drivers/gpu/drm/virtio/virtgpu_kms.c | 8 +- drivers/gpu/drm/virtio/virtgpu_object.c | 22 ++ drivers/gpu/drm/virtio/virtgpu_userptr.c | 350 +++++++++++++++++++++++ include/uapi/drm/virtgpu_drm.h | 11 + include/uapi/linux/virtio_gpu.h | 6 + 10 files changed, 480 insertions(+), 13 deletions(-) create mode 100644 drivers/gpu/drm/virtio/virtgpu_userptr.c
base-commit: 1ecab8b0bad393750c60cc3819ce9c256595463c
Signature · 1 line
2.34.1
RFC PATCH v10 1/4 drm/virtio: 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 the OpenCL capset (VIRCL), and gfxstream's rutabaga_gfx has already claimed IDs 7-9 ahead of any spec or kernel header update, so ROCm uses ID 10 to avoid a further collision. A matching virtio-gpu specification change is in progress to register this assignment.
Also add the matching VIRTGPU_DRM_CAPSET_ROCM alias in virtgpu_drm.h for consistency with the other capability sets exposed through DRM_IOCTL_VIRTGPU_GET_CAPS.
--- include/uapi/drm/virtgpu_drm.h | 1 + include/uapi/linux/virtio_gpu.h | 1 + 2 files changed, 2 insertions(+)
include/uapi/drm/virtgpu_drm.h +1 -0
@@ -170,6 +170,7 @@ struct drm_virtgpu_3d_wait {#define VIRTGPU_DRM_CAPSET_VENUS 4#define VIRTGPU_DRM_CAPSET_CROSS_DOMAIN 5#define VIRTGPU_DRM_CAPSET_DRM 6 +#define VIRTGPU_DRM_CAPSET_ROCM 10struct drm_virtgpu_get_caps {__u32 cap_set_id;__u32 cap_set_ver;
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 10/* VIRTIO_GPU_CMD_GET_CAPSET_INFO */ struct virtio_gpu_get_capset_info {
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [RFC PATCH v10 1/4] drm/virtio: add VIRTIO_GPU_CAPSET_ROCM capability
Sashiko has reviewed this patch and found no issues.
Quoted 12 lines
Add a new GPU capability set
VIRTIO_GPU_CAPSET_ROCMto 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 the OpenCL capset (VIRCL), and gfxstream's
rutabaga_gfxhas already claimed IDs 7-9 ahead of any spec or kernel header update, so ROCm uses ID 10 to avoid a further collision. A matching virtio-gpu specification change is in progress to register this assignment.
Quoted 5 lines
Also add the matching
VIRTGPU_DRM_CAPSET_ROCMalias invirtgpu_drm.hfor consistency with the other capability sets exposed throughDRM_IOCTL_VIRTGPU_GET_CAPS.Signed-off-by Honglei Huang <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Dmitry Osipenko
maintainerRe: [RFC PATCH v10 1/4] drm/virtio: add VIRTIO_GPU_CAPSET_ROCM capability
In reply to Honglei HuangQuoted 39 lines · Honglei Huang
Add a new GPU capability set
VIRTIO_GPU_CAPSET_ROCMto 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 the OpenCL capset (VIRCL), and gfxstream's
rutabaga_gfxhas already claimed IDs 7-9 ahead of any spec or kernel header update, so ROCm uses ID 10 to avoid a further collision. A matching virtio-gpu specification change is in progress to register this assignment.Also add the matching
VIRTGPU_DRM_CAPSET_ROCMalias invirtgpu_drm.hfor consistency with the other capability sets exposed throughDRM_IOCTL_VIRTGPU_GET_CAPS.Signed-off-by Honglei Huang <[email protected]>--- include/uapi/drm/virtgpu_drm.h | 1 + include/uapi/linux/virtio_gpu.h | 1 + 2 files changed, 2 insertions(+)include/uapi/drm/virtgpu_drm.h +1
-0@@ -170,6 +170,7 @@ struct drm_virtgpu_3d_wait {#define VIRTGPU_DRM_CAPSET_VENUS 4#define VIRTGPU_DRM_CAPSET_CROSS_DOMAIN 5#define VIRTGPU_DRM_CAPSET_DRM 6 +#define VIRTGPU_DRM_CAPSET_ROCM 10struct drm_virtgpu_get_caps {__u32 cap_set_id;__u32 cap_set_ver;include/uapi/linux/virtio_gpu.h +0
-0
include/uapi/linux/virtio_gpu.h struct virtio_gpu_cmd_submit { · line 327
Quoted 7 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 10/* VIRTIO_GPU_CMD_GET_CAPSET_INFO */ struct virtio_gpu_get_capset_info {
Could you please give a link to the spec change discussion? Please send it to virtio-comment ML if haven't been done yet.
Signature · 2 lines
Best regards, Dmitry
Re: [RFC PATCH v10 1/4] drm/virtio: add VIRTIO_GPU_CAPSET_ROCM capability
Quoted 40 lines · Dmitry Osipenko
Quoted 39 lines · Honglei Huang
Add a new GPU capability set
VIRTIO_GPU_CAPSET_ROCMto 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 the OpenCL capset (VIRCL), and gfxstream's
rutabaga_gfxhas already claimed IDs 7-9 ahead of any spec or kernel header update, so ROCm uses ID 10 to avoid a further collision. A matching virtio-gpu specification change is in progress to register this assignment.Also add the matching
VIRTGPU_DRM_CAPSET_ROCMalias invirtgpu_drm.hfor consistency with the other capability sets exposed throughDRM_IOCTL_VIRTGPU_GET_CAPS.Signed-off-by Honglei Huang <[email protected]>---
include/uapi/drm/virtgpu_drm.h | 1 +
include/uapi/linux/virtio_gpu.h| 1 +2 files changed, 2 insertions(+)include/uapi/drm/virtgpu_drm.h +1
-0@@ -170,6 +170,7 @@ struct drm_virtgpu_3d_wait { #define VIRTGPU_DRM_CAPSET_VENUS 4 #define VIRTGPU_DRM_CAPSET_CROSS_DOMAIN 5 #define VIRTGPU_DRM_CAPSET_DRM 6 +#define VIRTGPU_DRM_CAPSET_ROCM 10 struct drm_virtgpu_get_caps { __u32 cap_set_id; __u32 cap_set_ver;include/uapi/linux/virtio_gpu.h +0
-0@@ -321,6 +321,7 @@ struct virtio_gpu_cmd_submit {
Quoted 8 lines
include/uapi/linux/virtio_gpu.h struct virtio_gpu_cmd_submit { · line 327
Quoted 6 lines
include/uapi/linux/virtio_gpu.h +1
-0#define VIRTIO_GPU_CAPSET_VENUS 4 #define VIRTIO_GPU_CAPSET_CROSS_DOMAIN 5 #define VIRTIO_GPU_CAPSET_DRM 6 +#define VIRTIO_GPU_CAPSET_ROCM 10 /* VIRTIO_GPU_CMD_GET_CAPSET_INFO */ struct virtio_gpu_get_capset_info {Could you please give a link to the spec change discussion? Please send it to virtio-comment ML if haven't been done yet.
Will send the spec change and return to here.
Regards,
Honglei
Quoted 1 line
RFC PATCH v10 2/4 drm/virtio: add userptr blob resource to the UAPI
In reply to Honglei HuangAdd a userptr address field so userspace can request a blob backed by an existing process mapping. A non-zero userptr selects that path; probe VIRTGPU_PARAM_USERPTR before using the field.
GPU-readonly backing is requested with the virtio CREATE_BLOB wire flag VIRTIO_GPU_BLOB_FLAG_USE_READONLY (also VIRTGPU_BLOB_FLAG_USE_READONLY). The device MUST NOT write such a resource. The guest pins without FOLL_WRITE only when the device advertised VIRTIO_GPU_F_BLOB_READONLY (VIRTGPU_PARAM_BLOB_READONLY).
--- include/uapi/drm/virtgpu_drm.h | 10 ++++++++++ include/uapi/linux/virtio_gpu.h | 5 +++++ 2 files changed, 15 insertions(+)
include/uapi/drm/virtgpu_drm.h +10 -0
@@ -99,6 +99,8 @@ struct drm_virtgpu_execbuffer {#define VIRTGPU_PARAM_SUPPORTED_CAPSET_IDs 7/* Bitmask of supported capability set ids */ #define VIRTGPU_PARAM_EXPLICIT_DEBUG_NAME 8/* Ability to set debug name from userspace */ #define VIRTGPU_PARAM_BLOB_ALIGNMENT 9/* Device alignment requirements for blobs */ +#define VIRTGPU_PARAM_USERPTR 10/* CREATE_BLOB userptr field is supported */ +#define VIRTGPU_PARAM_BLOB_READONLY 11/* VIRTGPU_BLOB_FLAG_USE_READONLY is honored */ struct drm_virtgpu_getparam {__u64 param;
@@ -187,6 +189,7 @@ 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 +#define VIRTGPU_BLOB_FLAG_USE_READONLY 0x0008/* zero is invalid blob_mem */ __u32 blob_mem;__u32 blob_flags;
@@ -206,6 +209,13 @@ struct drm_virtgpu_resource_create_blob {#define DRM_VIRTGPU_BLOB_FLAG_HINT_DEFER_MAPPING 0x0001__u32 blob_hints;__u32 pad2; + +/* +* Non-zero selects the userptr path and is valid only with +* VIRTGPU_BLOB_MEM_GUEST. Must be 0 otherwise. Probe +* VIRTGPU_PARAM_USERPTR before using this field. +*/ +__u64 userptr; };#define VIRTGPU_CONTEXT_PARAM_CAPSET_ID 0x0001
include/uapi/linux/virtio_gpu.h +5 -0
@@ -72,6 +72,10 @@* must be aligned to that value. */ #define VIRTIO_GPU_F_BLOB_ALIGNMENT 5 +/* +* VIRTIO_GPU_BLOB_FLAG_USE_READONLY +*/ +#define VIRTIO_GPU_F_BLOB_READONLY 6enum virtio_gpu_ctrl_type {VIRTIO_GPU_UNDEFINED =0 ,
@@ -415,6 +419,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_READONLY 0x0008/* zero is invalid blob mem */ __le32 blob_mem;__le32 blob_flags;
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [RFC PATCH v10 2/4] drm/virtio: add userptr blob resource to the UAPI
Sashiko has reviewed this patch and found no issues.
Quoted 11 lines
Add a userptr address field so userspace can request a blob backed by an existing process mapping. A non-zero userptr selects that path; probe
VIRTGPU_PARAM_USERPTRbefore using the field.GPU-readonly backing is requested with the virtio
CREATE_BLOBwire flagVIRTIO_GPU_BLOB_FLAG_USE_READONLY(alsoVIRTGPU_BLOB_FLAG_USE_READONLY). The device MUST NOT write such a resource. The guest pins withoutFOLL_WRITEonly when the device advertisedVIRTIO_GPU_F_BLOB_READONLY(VIRTGPU_PARAM_BLOB_READONLY).Signed-off-by Honglei Huang <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [RFC PATCH v10 2/4] drm/virtio: add userptr blob resource to the UAPI
In reply to Honglei HuangQuoted 67 lines · Honglei Huang
Add a userptr address field so userspace can request a blob backed by an existing process mapping. A non-zero userptr selects that path; probe
VIRTGPU_PARAM_USERPTRbefore using the field.GPU-readonly backing is requested with the virtio
CREATE_BLOBwire flagVIRTIO_GPU_BLOB_FLAG_USE_READONLY(alsoVIRTGPU_BLOB_FLAG_USE_READONLY). The device MUST NOT write such a resource. The guest pins withoutFOLL_WRITEonly when the device advertisedVIRTIO_GPU_F_BLOB_READONLY(VIRTGPU_PARAM_BLOB_READONLY).Signed-off-by Honglei Huang <[email protected]>--- include/uapi/drm/virtgpu_drm.h | 10 ++++++++++ include/uapi/linux/virtio_gpu.h | 5 +++++ 2 files changed, 15 insertions(+)include/uapi/drm/virtgpu_drm.h +10
-0@@ -99,6 +99,8 @@ struct drm_virtgpu_execbuffer {#define VIRTGPU_PARAM_SUPPORTED_CAPSET_IDs 7/* Bitmask of supported capability set ids */ #define VIRTGPU_PARAM_EXPLICIT_DEBUG_NAME 8/* Ability to set debug name from userspace */ #define VIRTGPU_PARAM_BLOB_ALIGNMENT 9/* Device alignment requirements for blobs */ +#define VIRTGPU_PARAM_USERPTR 10/* CREATE_BLOB userptr field is supported */ +#define VIRTGPU_PARAM_BLOB_READONLY 11/* VIRTGPU_BLOB_FLAG_USE_READONLY is honored */ struct drm_virtgpu_getparam {__u64 param;@@ -187,6 +189,7 @@ 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 +#define VIRTGPU_BLOB_FLAG_USE_READONLY 0x0008/* zero is invalid blob_mem */ __u32 blob_mem;__u32 blob_flags;@@ -206,6 +209,13 @@ struct drm_virtgpu_resource_create_blob {#define DRM_VIRTGPU_BLOB_FLAG_HINT_DEFER_MAPPING 0x0001__u32 blob_hints;__u32 pad2; + +/* +* Non-zero selects the userptr path and is valid only with +* VIRTGPU_BLOB_MEM_GUEST. Must be 0 otherwise. Probe +* VIRTGPU_PARAM_USERPTR before using this field. +*/ +__u64 userptr; };#define VIRTGPU_CONTEXT_PARAM_CAPSET_ID 0x0001include/uapi/linux/virtio_gpu.h +4
-0@@ -72,6 +72,10 @@* must be aligned to that value. */ #define VIRTIO_GPU_F_BLOB_ALIGNMENT 5 +/* +* VIRTIO_GPU_BLOB_FLAG_USE_READONLY +*/ +#define VIRTIO_GPU_F_BLOB_READONLY 6enum virtio_gpu_ctrl_type {VIRTIO_GPU_UNDEFINED =0 ,
include/uapi/linux/virtio_gpu.h struct virtio_gpu_resource_create_blob { · line 425
Quoted 7 lines
include/uapi/linux/virtio_gpu.h +1
-0@@ -415,6 +419,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_READONLY 0x0008/* zero is invalid blob mem */ __le32 blob_mem;__le32 blob_flags;
Signature · 2 lines
Best regards, Dmitry
RFC PATCH v10 3/4 drm/virtio: implement userptr resource support
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
- Omit FOLL_WRITE when VIRTGPU_BLOB_FLAG_USE_READONLY is set
- 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 USE_READONLY blobs
- Mark writable pages dirty when unpinning
- 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
- Disallow PRIME export of userptr objects
- Save CREATE_BLOB params and restore userptr resources after hibernation without using the shmem restore path
Map with DMA_ATTR_REQUIRE_COHERENT at create time, on hibernation restore, and on unmap.
--- drivers/gpu/drm/virtio/Makefile | 3 +- drivers/gpu/drm/virtio/virtgpu_drv.h | 42 +++ drivers/gpu/drm/virtio/virtgpu_object.c | 22 ++ drivers/gpu/drm/virtio/virtgpu_userptr.c | 350 +++++++++++++++++++++++ 4 files changed, 416 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 +42 -0
70 lines not on this page · raw message
drivers/gpu/drm/virtio/virtgpu_object.c +22 -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);
@@ -316,6 +326,18 @@ int virtio_gpu_object_restore_all(struct virtio_gpu_device *vgdev)continue ; } +if (virtio_gpu_is_userptr (bo)) { + ret =virtio_gpu_userptr_restore (vgdev, bo, &ents, + &nents); +if (ret) +break ; + +virtio_gpu_cmd_resource_create_blob (vgdev, bo, + &bo->params, + ents, nents); +continue ; + } +if (bo->params.blob || bo->attached) { ret =virtio_gpu_object_shmem_init (vgdev, bo, &ents, &nents);
drivers/gpu/drm/virtio/virtgpu_userptr.c new file +350 -0
350 lines not on this page · raw message
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [RFC PATCH v10 3/4] drm/virtio: implement userptr resource support
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] drm/virtio: virtio_gpu_userptr_restore() skips DMA remap after failed map - [Low] drm/virtio…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] drm/virtio: virtio_gpu_userptr_restore() skips DMA remap after failed map
- [Low] drm/virtio: duplicated virtio_gpu_userptr_free() and dead userptr struct fields
--
commit a322ca4c583b559738cbbb3a5e62c08918a07dd8
Author: Honglei Huang <[email protected]>
drm/virtio: implement userptr resource support
This commit adds virtio_gpu_object_userptr so the guest kernel can pin an existing userspace mapping with FOLL_LONGTERM and pass its backing pages to the host via VIRTIO_GPU_CMD_RESOURCE_CREATE_BLOB, including hibernation restore support.
Quoted 4 lines
drivers/gpu/drm/virtio/virtgpu_drv.h +0
-0
[ ... ]
Quoted 30 lines
drivers/gpu/drm/virtio/virtgpu_drv.h +26
-0@@ -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, @sgt and @dma_mapped. */ +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; +};
[ ... ]
Quoted 6 lines
drivers/gpu/drm/virtio/virtgpu_userptr.c new file +0
-0@@ -0,0 +1,350 @@
[ ... ]
Quoted 8 lines
drivers/gpu/drm/virtio/virtgpu_userptr.c new file +8
-0+static void virtio_gpu_userptr_free (struct drm_gem_object *obj) +{ +struct virtio_gpu_object *bo =gem_to_virtio_gpu_obj (obj); +struct virtio_gpu_device *vgdev = obj->dev->dev_private; + +/* +* Pages stay pinned until RESOURCE_UNREF finishes. Its callback +* calls virtio_gpu_cleanup_object to drop them.
drivers/gpu/drm/virtio/virtgpu_userptr.c line 31
Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_userptr.c new file +10
-0+*/ +if (bo->created) { +virtio_gpu_remove_from_restore_list (bo); +virtio_gpu_cmd_unref_resource (vgdev, bo, false); +virtio_gpu_notify (vgdev); +return ; + } + +virtio_gpu_cleanup_object (bo); +}
[Severity: Low]
This isn't a bug, but virtio_gpu_userptr_free() is identical to virtio_gpu_free_object() in drivers/gpu/drm/virtio/virtgpu_object.c and could reuse it directly if exported.
In struct virtio_gpu_object_userptr, bo_handle is never used and file is only written in virtio_gpu_userptr_init() and never read (leaving an unreferenced struct drm_file pointer on an object that can outlive the file descriptor until RESOURCE_UNREF finishes). Also,
struct virtio_gpu_object_userptr_ops has only one static implementation, so virtio_gpu_userptr_get_pages() and virtio_gpu_userptr_put_pages() can be called directly without the ops vtable.
[ ... ]
Quoted 61 lines
drivers/gpu/drm/virtio/virtgpu_userptr.c new file +61
-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); +struct scatterlist *sg; +unsigned int count; +int si; + + count = use_dma_api ? userptr->sgt->nents : userptr->sgt->orig_nents; +if (!count) +return -EINVAL ; + + *ents =kvmalloc_array (count,sizeof (**ents),GFP_KERNEL ); +if (!*ents) +return -ENOMEM ; + +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 ; + } + } + + *nents = count; +return 0 ; +} + +int virtio_gpu_userptr_restore (struct virtio_gpu_device *vgdev, +struct virtio_gpu_object *bo, +struct virtio_gpu_mem_entry **ents, +unsigned int *nents) +{ +struct virtio_gpu_object_userptr *userptr =to_virtio_gpu_userptr (bo); +int ret; + +mutex_lock (&userptr->lock); +if (!userptr->sgt || !userptr->pages) { +mutex_unlock (&userptr->lock); +return -EINVAL ; + } + +if (userptr->dma_mapped) { +struct device *dev =drm_dev_dma_dev (vgdev->ddev); + +dma_unmap_sgtable (dev, userptr->sgt, userptr->dma_dir, +DMA_ATTR_REQUIRE_COHERENT ); + userptr->dma_mapped = false; + + ret =dma_map_sgtable (dev, userptr->sgt, userptr->dma_dir, +DMA_ATTR_REQUIRE_COHERENT ); +if (ret) {
drivers/gpu/drm/virtio/virtgpu_userptr.c line 200
Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_userptr.c new file +10
-0+mutex_unlock (&userptr->lock); +return ret; + } + userptr->dma_mapped = true; + } + + ret =virtio_gpu_userptr_get_entries (vgdev, userptr, ents, nents); +mutex_unlock (&userptr->lock); +return ret; +}
[Severity: Medium]
If dma_map_sgtable() fails in virtio_gpu_userptr_restore(), will a later restore attempt send unmapped DMA addresses to the host?
When dma_map_sgtable() fails after dma_unmap_sgtable(), userptr->dma_mapped is left false while userptr->sgt->nents remains non-zero and the object stays on vgdev->obj_restore_list. A subsequent virtio_gpu_userptr_restore() call then skips remapping because userptr->dma_mapped is false, but virtio_gpu_userptr_get_entries() checks virtio_gpu_use_dma_api(vgdev->vdev) and reads the unmapped sg_dma_address(sg) entries anyway.
Checking virtio_gpu_use_dma_api(vgdev->vdev) to decide whether dma_map_sgtable() is needed, and only checking userptr->dma_mapped before calling dma_unmap_sgtable(), would avoid this.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Re: [RFC PATCH v10 3/4] drm/virtio: implement userptr resource support
In reply to Honglei HuangQuoted 507 lines · Honglei Huang
Add userptr blob objects so the guest kernel can pin an existing userspace mapping and advertise it as
CREATE_BLOBbacking entries.- New
virtio_gpu_object_userptrtype for userptr resources
- Pin pages withpin_user_pages_fast()andFOLL_LONGTERM
- OmitFOLL_WRITEwhenVIRTGPU_BLOB_FLAG_USE_READONLYis set
- ChargeFOLL_LONGTERMpins againstRLIMIT_MEMLOCK
- DMA-map the scatterlist only whenvirtio_gpu_use_dma_api()is required; useDMA_TO_DEVICEforUSE_READONLYblobs
- Mark writable pages dirty when unpinning
- Keep pages pinned untilRESOURCE_UNREFis queued; drop them fromcleanup_object()on the unref response or on create failure
- Clearuserptr->pageson pin failure to avoid double-free on cleanup
- Reject unaligned or overflowing userptr ranges at create time
- Disallow PRIME export of userptr objects
- SaveCREATE_BLOBparams and restore userptr resources after hibernation without using the shmem restore pathMap with
DMA_ATTR_REQUIRE_COHERENTat create time, on hibernation restore, and on unmap.Signed-off-by Honglei Huang <[email protected]>--- drivers/gpu/drm/virtio/Makefile | 3 +- drivers/gpu/drm/virtio/virtgpu_drv.h | 42 +++ drivers/gpu/drm/virtio/virtgpu_object.c | 22 ++ drivers/gpu/drm/virtio/virtgpu_userptr.c | 350 +++++++++++++++++++++++ 4 files changed, 416 insertions(+), 1 deletion(-)create mode 100644
drivers/gpu/drm/virtio/virtgpu_userptr.cdrivers/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.odrivers/gpu/drm/virtio/virtgpu_drv.h +42
-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, @sgt and @dma_mapped. */ +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;@@ -284,6 +315,7 @@ struct virtio_gpu_device {bool has_host_visible;bool has_context_init;bool has_blob_alignment; +bool has_blob_readonly;bool hibernated;struct virtio_shm_region host_visible_region;struct drm_mm host_visible_mm;@@ -562,4 +594,14 @@ 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); +int virtio_gpu_userptr_restore (struct virtio_gpu_device *vgdev, +struct virtio_gpu_object *bo, +struct virtio_gpu_mem_entry **ents, +unsigned int *nents);#endif drivers/gpu/drm/virtio/virtgpu_object.c +22
-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);@@ -316,6 +326,18 @@ int virtio_gpu_object_restore_all(struct virtio_gpu_device *vgdev)continue ; } +if (virtio_gpu_is_userptr (bo)) { + ret =virtio_gpu_userptr_restore (vgdev, bo, &ents, + &nents); +if (ret) +break ; + +virtio_gpu_cmd_resource_create_blob (vgdev, bo, + &bo->params, + ents, nents); +continue ; + } +if (bo->params.blob || bo->attached) { ret =virtio_gpu_object_shmem_init (vgdev, bo, &ents, &nents);drivers/gpu/drm/virtio/virtgpu_userptr.c new file +340
-0@@ -0,0 +1,350 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <linux/dma-mapping.h> +#include <linux/limits.h> +#include <linux/mm.h> +#include <linux/overflow.h> +#include <linux/pid.h> +#include <linux/sched/mm.h> +#include <linux/sched/signal.h> +#include <linux/vmalloc.h> + +#include "virtgpu_drv.h" +#include <drm/drm_gem.h> + +static void virtio_gpu_userptr_free (struct drm_gem_object *obj) +{ +struct virtio_gpu_object *bo =gem_to_virtio_gpu_obj (obj); +struct virtio_gpu_device *vgdev = obj->dev->dev_private; + +/* +* Pages stay pinned until RESOURCE_UNREF finishes. Its callback +* calls virtio_gpu_cleanup_object to drop them. +*/ +if (bo->created) { +virtio_gpu_remove_from_restore_list (bo); +virtio_gpu_cmd_unref_resource (vgdev, bo, false); +virtio_gpu_notify (vgdev); +return ; + } + +virtio_gpu_cleanup_object (bo); +} + +static struct dma_buf * +virtio_gpu_userptr_prime_export (struct drm_gem_object *obj,int flags) +{ +return ERR_PTR (-EINVAL ); +} + +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 = virtio_gpu_userptr_prime_export, +}; + +bool virtio_gpu_is_userptr (struct virtio_gpu_object *bo) +{ +return bo->base.base.funcs == &virtio_gpu_userptr_funcs; +} + +static int +virtio_gpu_userptr_get_pages (struct virtio_gpu_object_userptr *userptr) +{ +unsigned int flag =FOLL_LONGTERM ; +unsigned int num_pages, pinned =0 ; +int ret =0 ; + +if (userptr->pages) +return 0 ; + + userptr->pages =kvmalloc_array (userptr->npages,sizeof (struct page *), +GFP_KERNEL ); +if (!userptr->pages) +return -ENOMEM ; + +if (!(userptr->flags &VIRTGPU_BLOB_FLAG_USE_READONLY )) + flag |=FOLL_WRITE ; + +do { + num_pages = userptr->npages - pinned; + + 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); + userptr->pages =NULL ; +return ret; + } + + pinned += ret; + + }while (pinned < userptr->npages); + +return 0 ; +} + +static void +virtio_gpu_userptr_unaccount (struct virtio_gpu_object_userptr *userptr) +{ +if (!userptr->mm) +return ; + +atomic64_sub (userptr->npages, &userptr->mm->pinned_vm); +mmdrop (userptr->mm); + userptr->mm =NULL ; +} + +static void +virtio_gpu_userptr_put_pages (struct virtio_gpu_object_userptr *userptr) +{ +struct drm_device *dev = userptr->base.base.base.dev; + +if (userptr->sgt) { +if (userptr->dma_mapped) +dma_unmap_sgtable (drm_dev_dma_dev (dev), userptr->sgt, + userptr->dma_dir, +DMA_ATTR_REQUIRE_COHERENT ); + userptr->dma_mapped = false; +sg_free_table (userptr->sgt); +kfree (userptr->sgt); + userptr->sgt =NULL ; + } + +if (userptr->pages) { +bool dirty = !(userptr->flags &VIRTGPU_BLOB_FLAG_USE_READONLY ); + +unpin_user_pages_dirty_lock (userptr->pages, userptr->npages, + dirty); +kvfree (userptr->pages); + userptr->pages =NULL ; + } + +virtio_gpu_userptr_unaccount (userptr); +} + +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); +struct scatterlist *sg; +unsigned int count; +int si; + + count = use_dma_api ? userptr->sgt->nents : userptr->sgt->orig_nents; +if (!count) +return -EINVAL ; + + *ents =kvmalloc_array (count,sizeof (**ents),GFP_KERNEL ); +if (!*ents) +return -ENOMEM ; + +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 ; + } + } + + *nents = count; +return 0 ; +} + +int virtio_gpu_userptr_restore (struct virtio_gpu_device *vgdev, +struct virtio_gpu_object *bo, +struct virtio_gpu_mem_entry **ents, +unsigned int *nents) +{ +struct virtio_gpu_object_userptr *userptr =to_virtio_gpu_userptr (bo); +int ret; + +mutex_lock (&userptr->lock); +if (!userptr->sgt || !userptr->pages) { +mutex_unlock (&userptr->lock); +return -EINVAL ; + } + +if (userptr->dma_mapped) { +struct device *dev =drm_dev_dma_dev (vgdev->ddev); + +dma_unmap_sgtable (dev, userptr->sgt, userptr->dma_dir, +DMA_ATTR_REQUIRE_COHERENT ); + userptr->dma_mapped = false; + + ret =dma_map_sgtable (dev, userptr->sgt, userptr->dma_dir, +DMA_ATTR_REQUIRE_COHERENT ); +if (ret) { +mutex_unlock (&userptr->lock); +return ret; + } + userptr->dma_mapped = true; + } + + ret =virtio_gpu_userptr_get_entries (vgdev, userptr, ents, nents); +mutex_unlock (&userptr->lock); +return ret; +} + +static int +virtio_gpu_userptr_init (struct drm_device *dev,struct drm_file *file, +struct virtio_gpu_object_userptr *userptr, +struct virtio_gpu_object_params *params, +const struct virtio_gpu_object_userptr_ops *ops) +{ +struct drm_gem_object *obj; +int ret; + + userptr->start = params->userptr; + userptr->npages = params->size >>PAGE_SHIFT ; + userptr->flags = params->blob_flags; + +mutex_init (&userptr->lock); + userptr->vgdev = dev->dev_private; + userptr->file = file; + userptr->ops = ops; + +/* +* Get the resource id before GEM init, so a failed call here +* just needs a plain kfree and no special id zero check in the +* shared resource_id_put helper. +*/ + ret =virtio_gpu_resource_id_get (userptr->vgdev, + &userptr->base.hw_res_handle); +if (ret) { +mutex_destroy (&userptr->lock); +return ret; + } + + obj = &userptr->base.base.base; + obj->funcs = &virtio_gpu_userptr_funcs; + +drm_gem_private_object_init (dev, obj, params->size); +INIT_LIST_HEAD (&userptr->base.restore_node); + +return 0 ; +} + +static const struct virtio_gpu_object_userptr_ops virtio_gpu_userptr_ops = { + .get_pages = virtio_gpu_userptr_get_pages, + .put_pages = virtio_gpu_userptr_put_pages, +}; + +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) +{ +struct virtio_gpu_object_userptr *userptr; +struct virtio_gpu_mem_entry *ents =NULL ; +struct sg_table *sgt; +struct mm_struct *mm; +unsigned long lock_limit; +unsigned long start; +unsigned long end; +s64 new_pinned; +unsigned int nents; +int ret; + + *bo_ptr =NULL ; + +if (!params->size || !IS_ALIGNED (params->size,PAGE_SIZE ) || + params->userptr != (unsigned long )params->userptr) +return -EINVAL ; + + start = params->userptr; +if (!IS_ALIGNED (start,PAGE_SIZE ) || +check_add_overflow (start, (unsigned long )params->size, &end)) +return -EINVAL ; + +if (!can_do_mlock ()) +return -EPERM ; + +if (params->size >>PAGE_SHIFT >INT_MAX ) +return -E2BIG ; + +if (!access_ok ((void __user *)start, params->size)) +return -EFAULT ; + + userptr =kzalloc_obj (*userptr); +if (!userptr) +return -ENOMEM ; + + ret =virtio_gpu_userptr_init (vgdev->ddev, file, userptr, params, + &virtio_gpu_userptr_ops); +if (ret) { +kfree (userptr); +return ret; + } + + mm = current->mm; +mmgrab (mm); + lock_limit =rlimit (RLIMIT_MEMLOCK ) >>PAGE_SHIFT ; + new_pinned =atomic64_add_return (userptr->npages, &mm->pinned_vm); +if (new_pinned > lock_limit && !capable (CAP_IPC_LOCK )) { +atomic64_sub (userptr->npages, &mm->pinned_vm); +mmdrop (mm); + ret = -ENOMEM ; + goto err_cleanup; + } + userptr->mm = mm; + +mutex_lock (&userptr->lock); + ret = userptr->ops->get_pages (userptr); +mutex_unlock (&userptr->lock); +if (ret) + goto err_cleanup; + + sgt =drm_prime_pages_to_sg (vgdev->ddev, userptr->pages, + userptr->npages); +if (IS_ERR (sgt)) { + ret =PTR_ERR (sgt); + goto err_cleanup; + } + + userptr->sgt = sgt; + +if (virtio_gpu_use_dma_api (vgdev->vdev)) { +enum dma_data_direction dir = + (userptr->flags &VIRTGPU_BLOB_FLAG_USE_READONLY ) ? +DMA_TO_DEVICE :DMA_BIDIRECTIONAL ; + + ret =dma_map_sgtable (drm_dev_dma_dev (vgdev->ddev), sgt, + dir,DMA_ATTR_REQUIRE_COHERENT ); +if (ret) + goto err_cleanup; + + userptr->dma_dir = dir; + userptr->dma_mapped = true; + } + + ret =virtio_gpu_userptr_get_entries (vgdev, userptr, &ents, &nents); +if (ret) + goto err_cleanup; + +virtio_gpu_cmd_resource_create_blob (vgdev, &userptr->base, params, ents, + nents); +
drivers/gpu/drm/virtio/virtgpu_userptr.c line 350
Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_userptr.c new file +10
-0+ userptr->base.params = *params; +virtio_gpu_add_object_to_restore_list (vgdev, &userptr->base); + + *bo_ptr = &userptr->base; +return 0 ; + +err_cleanup: +virtio_gpu_cleanup_object (&userptr->base); +return ret; +}
Signature · 2 lines
Best regards, Dmitry
RFC PATCH v10 4/4 drm/virtio: wire blob ioctl creation to userptr objects
In reply to Honglei HuangIntegrate userptr into the blob resource creation ioctl.
- A non-zero userptr selects virtio_gpu_userptr_create()
- Reject userptr unless blob_mem is VIRTGPU_BLOB_MEM_GUEST
- Reject VIRTGPU_BLOB_FLAG_USE_READONLY unless the device advertised VIRTIO_GPU_F_BLOB_READONLY
- Advertise VIRTGPU_PARAM_USERPTR and VIRTGPU_PARAM_BLOB_READONLY
- Add VIRTIO_GPU_F_BLOB_READONLY to the driver's features[] array
--- drivers/gpu/drm/virtio/virtgpu_debugfs.c | 1 + drivers/gpu/drm/virtio/virtgpu_drv.c | 1 + drivers/gpu/drm/virtio/virtgpu_ioctl.c | 49 +++++++++++++++++++----- drivers/gpu/drm/virtio/virtgpu_kms.c | 8 +++- 4 files changed, 47 insertions(+), 12 deletions(-)
drivers/gpu/drm/virtio/virtgpu_debugfs.c +1 -0
@@ -55,6 +55,7 @@ static int virtio_gpu_features(struct seq_file *m, void *data) vgdev->has_resource_assign_uuid);virtio_gpu_add_bool (m,"blob resources" , vgdev->has_resource_blob); +virtio_gpu_add_bool (m,"blob readonly" , vgdev->has_blob_readonly);virtio_gpu_add_bool (m,"context init" , vgdev->has_context_init);virtio_gpu_add_int (m,"cap sets" , vgdev->num_capsets);virtio_gpu_add_int (m,"scanouts" , vgdev->num_scanouts);
drivers/gpu/drm/virtio/virtgpu_drv.c +1 -0
@@ -182,6 +182,7 @@ static unsigned int features[] = {VIRTIO_GPU_F_RESOURCE_BLOB ,VIRTIO_GPU_F_CONTEXT_INIT ,VIRTIO_GPU_F_BLOB_ALIGNMENT , + VIRTIO_GPU_F_BLOB_READONLY, };#ifdef CONFIG_PM_SLEEP
drivers/gpu/drm/virtio/virtgpu_ioctl.c +39 -10
103 lines not on this page · raw message
drivers/gpu/drm/virtio/virtgpu_kms.c +6 -2
@@ -249,15 +249,19 @@ int virtio_gpu_init(struct virtio_device *vdev, struct drm_device *dev) vgdev->blob_alignment = blob_alignment; } +if (virtio_has_feature (vgdev->vdev,VIRTIO_GPU_F_BLOB_READONLY )) + vgdev->has_blob_readonly = true; +DRM_INFO ("features: %cvirgl %cedid %cresource_blob %chost_visible" , vgdev->has_virgl_3d ?'+' :'-' , vgdev->has_edid ?'+' :'-' , vgdev->has_resource_blob ?'+' :'-' , vgdev->has_host_visible ?'+' :'-' ); -DRM_INFO ("features: %ccontext_init %cblob_alignment\n" , +DRM_INFO ("features: %ccontext_init %cblob_alignment %cblob_readonly\n" , vgdev->has_context_init ?'+' :'-' , - vgdev->has_blob_alignment ?'+' :'-' ); + vgdev->has_blob_alignment ?'+' :'-' , + vgdev->has_blob_readonly ?'+' :'-' ); ret =virtio_gpu_find_vqs (vgdev);if (ret) {
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [RFC PATCH v10 4/4] drm/virtio: wire blob ioctl creation to userptr objects
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] drm/virtio: unnotified ctrlq strands blob on handle creation failure --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] drm/virtio: unnotified ctrlq strands blob on handle creation failure
--
commit f8b823e6be2332eb525d4e0d0576cd0869cd03e1
Author: Honglei Huang <[email protected]>
drm/virtio: wire blob ioctl creation to userptr objects
This commit integrates userptr creation and read-only blob support into the blob resource creation and getparam ioctls, and switches the error paths in virtio_gpu_resource_create_blob_ioctl() from drm_gem_object_release() to drm_gem_object_put().
Quoted 4 lines
drivers/gpu/drm/virtio/virtgpu_ioctl.c +0
-0
[ ... ]
Quoted 6 lines
drivers/gpu/drm/virtio/virtgpu_ioctl.c +1
-1@@ -557,14 +586,14 @@ static int virtio_gpu_resource_create_blob_ioctl(struct drm_device *dev,if (params.blob_flags &VIRTGPU_BLOB_FLAG_USE_CROSS_DEVICE ) { ret =virtio_gpu_resource_assign_uuid (vgdev, bo);if (ret) { -drm_gem_object_release (obj); +drm_gem_object_put (obj);
drivers/gpu/drm/virtio/virtgpu_ioctl.c static int virtio_gpu_resource_create_blob_ioctl(struct drm_device *dev, · line 598
Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_ioctl.c +1
-1return ret; } } ret =drm_gem_handle_create (file, obj, &handle);if (ret) { -drm_gem_object_release (obj); +drm_gem_object_put (obj);return ret; }
[Severity: Medium]
Can this leave obj stranded in vgdev->ctrlq when drm_gem_handle_create() fails after virtio_gpu_resource_assign_uuid() succeeds?
Neither the blob creation helpers nor virtio_gpu_resource_assign_uuid() call virtio_gpu_notify(vgdev); notification normally happens at the end of virtio_gpu_gem_object_open() inside drm_gem_handle_create(). However, virtio_gpu_resource_assign_uuid() takes an extra reference on obj via virtio_gpu_array_add_obj() for the queued command buffer:
drivers/gpu/drm/virtio/virtgpu_prime.c:virtio_gpu_resource_assign_uuid() {
objs = virtio_gpu_array_alloc(1);
if (!objs)
return -ENOMEM;
virtio_gpu_array_add_obj(objs, &bo->base.base);
return virtio_gpu_cmd_resource_assign_uuid(vgdev, objs);
}If drm_gem_handle_create() fails before virtio_gpu_gem_object_open() reaches virtio_gpu_notify(vgdev), drm_gem_object_put(obj) only drops obj->refcount from 2 to 1. Because the refcount does not reach 0, obj->funcs->free() is not called, leaving the queued commands un-kicked and keeping userptr pages pinned and charged to mm->pinned_vm until an unrelated command notifies ctrlq. Calling virtio_gpu_notify(vgdev) on the drm_gem_handle_create() failure path would ensure the queued command completes and drops its reference.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Dmitry Osipenko
maintainerRe: [RFC PATCH v10 4/4] drm/virtio: wire blob ioctl creation to userptr objects
In reply to Honglei HuangQuoted 7 lines · Honglei Huang
drivers/gpu/drm/virtio/virtgpu_ioctl.c +1
-1@@ -495,10 +516,15 @@ 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 &&
drivers/gpu/drm/virtio/virtgpu_ioctl.c static int verify_blob(struct virtio_gpu_device *vgdev, · line 528
Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_ioctl.c +7
-2- !IS_ALIGNED(params->size, vgdev->blob_alignment)) -return -EINVAL ; +if (vgdev->has_blob_alignment) { +if (!IS_ALIGNED (params->size, vgdev->blob_alignment)) +return -EINVAL ; + +if (!IS_ALIGNED (params->userptr, vgdev->blob_alignment)) +return -EINVAL ; + }
There was a bug reported RE the alignment check on [1], my proposal there is to align allocated size to host page size.
[1] https://lore.kernel.org/dri-devel/[email protected]/
In virtio_gpu_userptr_create() I see it rejects size that is not aligned to PAGE_SIZE, but for VRAM and shmem BOs the driver code implicitly aligns size to PAGE_SIZE. Could we do the same for userptr for consistency?
Signature · 2 lines
Best regards, Dmitry
Re: [RFC PATCH v10 4/4] drm/virtio: wire blob ioctl creation to userptr objects
Quoted 20 lines · Dmitry Osipenko
Quoted 6 lines · Honglei Huang
drivers/gpu/drm/virtio/virtgpu_ioctl.c +1
-0@@ -495,10 +516,15 @@ 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;drivers/gpu/drm/virtio/virtgpu_ioctl.c static int verify_blob(struct virtio_gpu_device · line 527
Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_ioctl.c +7
-3- if (vgdev->has_blob_alignment && - !IS_ALIGNED(params->size, vgdev->blob_alignment)) - return -EINVAL; + if (vgdev->has_blob_alignment) { + if (!IS_ALIGNED(params->size, vgdev->blob_alignment)) + return -EINVAL; + + if (!IS_ALIGNED(params->userptr, vgdev->blob_alignment)) + return -EINVAL; + }There was a bug reported RE the alignment check on [1], my proposal there is to align allocated size to host page size.
Quoted 3 lines
[1] https://lore.kernel.org/dri-devel/20261006154913.2689067-2- [email protected]/
In
virtio_gpu_userptr_create()I see it rejects size that is not aligned toPAGE_SIZE, but for VRAM and shmem BOs the driver code implicitly aligns size toPAGE_SIZE. Could we do the same for userptr for consistency?
Got it will change.
Regards,
Honglei
Quoted 1 line
Quoted 10 lines · Honglei Huang
This series adds virtio-gpu userptr support for ROCm native compute contexts. The guest kernel pins an existing userspace mapping with
FOLL_LONGTERMand exposes it to the host as ordinaryCREATE_BLOBbacking entries, avoiding a second shmem allocation and memcpy.A non-zero userptr field selects that path. Probe
VIRTGPU_PARAM_USERPTRbefore using the field so an older guest kernel rejects the request instead of silently creating a shmem blob.See overall detail in virglrenderer pr [1]
Hi all,
Any review comments on this series? includes code for the kernel, QEMU, and other components.
Open to any review.
Regards,
Honglei
Quoted 121 lines
Patches overview:
1. AddVIRTIO_GPU_CAPSET_ROCMcapability for compute workloads
2. Extend DRM/virtio UAPI with userptr,PARAM_USERPTR,USE_READONLY, andF_BLOB_READONLY
3. Implement core userptr functionality with page management
4. Wire blob ioctl creation to userptr objects and probe the readonly featureV10 changes:
- RequireDMA_ATTR_REQUIRE_COHERENTon create and restore mappings.
- Usedrm_gem_object_put()after UUID assignment or handle creation failure.
- Drop thenew_pinned< 0 check.
- Validate the userptr address against the host'sblob_alignmentinverify_blob().V9 changes:
- Marked series RFC pending a virtio-gpu spec update forF_BLOB_READONLY/BLOB_FLAG_USE_READONLY(Odaki)
- Moved VIRTIO_GPU_CAPSET_ROCM/VIRTGPU_DRM_CAPSET_ROCM from ID 8 to 10 to avoid the gfxstream/rutabaga_gfx 7-9 collision (Odaki)
- Added the missingVIRTGPU_DRM_CAPSET_ROCMalias (Odaki, sashiko-bot)
- Dropped thekernel/guest-internalwire-flag comment from the UAPI header and reworded "Guest VA" to "address" in the userptr field comment (Odaki)
- Fixed aBUG()on every virtio-gpu probe:VIRTIO_GPU_F_BLOB_READONLYwas queried withvirtio_has_feature()but never added to the driver's features[] array (Odaki)
- Removed the deadTRANSFER_TO_HOSTuserptr DMA-sync branches added tovirtgpu_vq.c;verify_blob()already rejectsVIRTGPU_BLOB_MEM_GUESTfor that ioctl, so the branches were unreachable (Odaki)
- Documented the DMA mapping and bounce buffer behavior ofvirtio_gpu_userptr_restore()and dropped the zero-copy claim from the commit title and message (Odaki)V8 changes:
- Drop guest-only USERPTR ioctl flags; a non-zero userptr field selects the path
- AddVIRTGPU_PARAM_USERPTRso older kernels do not silently ignore the new field
- Request GPU-readonly backing with virtio wire flagUSE_READONLY
- AcceptUSE_READONLYand omitFOLL_WRITEonly if the host advertisedVIRTIO_GPU_F_BLOB_READONLY
- Restore userptr blobs after hibernation without the shmem pathV7 changes:
- Mask guest-only DRM flags out ofCREATE_BLOBwireblob_flags
- Clearuserptr->pagesafter pin failure to avoid double-free
- DMA-map userptr SG only whenvirtio_gpu_use_dma_api()is required
- UseDMA_TO_DEVICEfor readonly blobs
- Sync userptr SG for the device onTRANSFER_TO_HOST
- Mark writable pages dirty when unpinning
- Reject USERPTR unlessblob_memisVIRTGPU_BLOB_MEM_GUEST
- Disallow PRIME export of userptr objects
- Note thatCAPSET_ROCMuses ID 8 because ID 7 is taken by VIRCLV6 changes:
- Rebase onto drm-misc-next
- KeepUSE_USERPTR/USERPTR_RDONLYas 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 messagesV5 changes:
- AddVIRTIO_GPU_BLOB_FLAG_USERPTR_RDONLYdefinition to patch 2
- Dropped unusedVIRTIO_GPU_F_RESOURCE_USERPTRfeature bit in patch 2
- IncludedVIRTIO_GPU_BLOB_FLAG_USERPTR_RDONLYinVIRTGPU_BLOB_FLAG_USE_MASKin patch 5
- Add check for userptr feature in patch 5 before creating userptr blob resource
- Updated corresponding cover letter and commit messagesV4 changes:
- RenamedVIRTIO_GPU_CAPSET_HSAKMTtoVIRTIO_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 contentV3 changes:
- Split into focused patches for easier review
- Removed complex interval tree userptr management
- Simplified resource creation without deduplication
- AddedVIRTGPU_PARAM_RESOURCE_USERPTRfor feature detection
- Improved UAPI documentation and error handling
- Removed MMU notifier dependencies for simplicity
- Fixed resource lifecycle management issuesV2: - 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.[1]: https://gitlab.freedesktop.org/virgl/virglrenderer/-/merge_requests/1708
Honglei Huang (4):
drm/virtio: addVIRTIO_GPU_CAPSET_ROCMcapability drm/virtio: add userptr blob resource to the UAPI drm/virtio: implement userptr resource support drm/virtio: wire blob ioctl creation to userptr objectsdrivers/gpu/drm/virtio/Makefile | 3 +- drivers/gpu/drm/virtio/virtgpu_debugfs.c | 1 + drivers/gpu/drm/virtio/virtgpu_drv.c | 1 + drivers/gpu/drm/virtio/virtgpu_drv.h | 42 +++ drivers/gpu/drm/virtio/virtgpu_ioctl.c | 49 +++- drivers/gpu/drm/virtio/virtgpu_kms.c | 8 +- drivers/gpu/drm/virtio/virtgpu_object.c | 22 ++ drivers/gpu/drm/virtio/virtgpu_userptr.c | 350 +++++++++++++++++++++++ include/uapi/drm/virtgpu_drm.h | 11 + include/uapi/linux/virtio_gpu.h | 6 + 10 files changed, 480 insertions(+), 13 deletions(-)create mode 100644
drivers/gpu/drm/virtio/virtgpu_userptr.cbase-commit:
1ecab8b0bad393750c60cc3819ce9c256595463c
Dmitry Osipenko
maintainerHi,
Quoted 12 lines · Honglei
Quoted 9 lines · Honglei Huang
This series adds virtio-gpu userptr support for ROCm native compute contexts. The guest kernel pins an existing userspace mapping with
FOLL_LONGTERMand exposes it to the host as ordinaryCREATE_BLOBbacking entries, avoiding a second shmem allocation and memcpy.A non-zero userptr field selects that path. Probe
VIRTGPU_PARAM_USERPTRbefore using the field so an older guest kernel rejects the request instead of silently creating a shmem blob.
Quoted 8 lines
Quoted 1 line
See overall detail in virglrenderer pr [1]
Hi all,
Any review comments on this series? includes code for the kernel, QEMU, and other components.
Open to any review.
I want to see ack on VIRTIO_GPU_FLAG_BLOB_READONLY_ENABLED and userptr patches from Akihiko before merging since he requested the RO enforcement feature and reviewed userpatch patch.
BTW, you may drop RFC, this is already v10. RFC usually comes before v1.
Signature · 2 lines
Best regards, Dmitry
Quoted 17 lines · Dmitry Osipenko
Hi,
Quoted 14 lines · Honglei
Quoted 10 lines · Honglei Huang
This series adds virtio-gpu userptr support for ROCm native compute contexts. The guest kernel pins an existing userspace mapping with
FOLL_LONGTERMand exposes it to the host as ordinaryCREATE_BLOBbacking entries, avoiding a second shmem allocation and memcpy.A non-zero userptr field selects that path. Probe
VIRTGPU_PARAM_USERPTRbefore using the field so an older guest kernel rejects the request instead of silently creating a shmem blob.See overall detail in virglrenderer pr [1]
Quoted 9 lines
Quoted 6 lines
Hi all,
Any review comments on this series? includes code for the kernel, QEMU, and other components.
Open to any review.
I want to see ack on
VIRTIO_GPU_FLAG_BLOB_READONLY_ENABLEDand userptr patches from Akihiko before merging since he requested the RO enforcement feature and reviewed userpatch patch.
Hi Dmitry,
Yes, totally agreed.
Hi Akihiko,
Your feedback has helped the readonly contract, DMA coherence requirements and resource lifetime handling across the virtio GPU stack. Really appreciate your contribution. I think your continued review will be important for future virtio-gpu
userptr changes, and I'd welcome your help in guiding its longer term
development.
Regards,
Honglei
Quoted 2 lines
BTW, you may drop RFC, this is already v10. RFC usually comes before v1.