Skip to contentSkip to conversation

drm/virtio: userptr blobs for native-context compute

PATCH New Honglei Huang 2 participants 8 messages Dec 2

Subsystems (4)
lore
Share & tools
lore
#1 lore raw

PATCH v11 1/4 drm/virtio: add VIRTIO_GPU_CAPSET_ROCM capability

In reply to Honglei Huang

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
index 95587e12aed..9b886e9772a 100644
@@ -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 +1 -0
index 4f530d90058..e22049835e5 100644
@@ -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
#0 lore raw

v11 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].
virtio spec change request in: [2].

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

V11 changes:
- Drop RFC (Dmitry) and rebase onto drm-misc-next 5ce32a0c94a8.
- Round blob sizes up to the host/guest page size [3] (Dmitry).
- Round userptr sizes up to PAGE_SIZE like shmem and VRAM (Dmitry).
- Remap userptr DMA on every hibernation restore (sashiko-bot).
- Reject userptr blobs with USE_CROSS_DEVICE (sashiko-bot).
- Keep Dmitry's Reviewed-by on patches 2-3.

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
[2]: https://lore.kernel.org/virtio-comment/[email protected]/
[3]: https://lore.kernel.org/dri-devel/[email protected]/

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   |  52 +++-
 drivers/gpu/drm/virtio/virtgpu_kms.c     |   8 +-
 drivers/gpu/drm/virtio/virtgpu_object.c  |  22 ++
 drivers/gpu/drm/virtio/virtgpu_userptr.c | 352 +++++++++++++++++++++++
 include/uapi/drm/virtgpu_drm.h           |  12 +
 include/uapi/linux/virtio_gpu.h          |   6 +
 10 files changed, 488 insertions(+), 11 deletions(-)
 create mode 100644 drivers/gpu/drm/virtio/virtgpu_userptr.c

base-commit: 5ce32a0c94a827ff44ee60898c28060714dcff75

Signature · 1 line
2.34.1
#2 lore raw

PATCH v11 2/4 drm/virtio: add userptr blob resource to the UAPI

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_USERPTR before using the field. Userptr blobs are valid only with VIRTGPU_BLOB_MEM_GUEST and cannot be combined with VIRTGPU_BLOB_FLAG_USE_CROSS_DEVICE.

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  | 11 +++++++++++
 include/uapi/linux/virtio_gpu.h |  5 +++++
 2 files changed, 16 insertions(+)
include/uapi/drm/virtgpu_drm.h +11 -0
index 9b886e9772a..612fc5d2ac3 100644
@@ -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,14 @@ 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 and without VIRTGPU_BLOB_FLAG_USE_CROSS_DEVICE.
+	 * 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
index e22049835e5..af953747585 100644
@@ -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       6
 
 enum 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
#3 lore raw

PATCH v11 3/4 drm/virtio: implement userptr resource support

In reply to Honglei Huang

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
- 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
- Round the size up to PAGE_SIZE like shmem and VRAM objects, and 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 the scatterlist on every restore attempt so a failed remap is retried

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 | 352 +++++++++++++++++++++++
 4 files changed, 418 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/virtio/virtgpu_userptr.c
drivers/gpu/drm/virtio/Makefile +2 -1
index d2e1788a822..fe7332a621a 100644
@@ -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
index 49899485be6..ab21494b1c3 100644
@@ -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 +352 -0

352 lines not on this page · raw message

Signature · 1 line
2.34.1
#5 lore raw

PATCH v11 4/4 drm/virtio: wire blob ioctl creation to userptr objects

In reply to Honglei Huang

Integrate 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, and reject it together with VIRTGPU_BLOB_FLAG_USE_CROSS_DEVICE
- Reject VIRTGPU_BLOB_FLAG_USE_READONLY unless the device advertised VIRTIO_GPU_F_BLOB_READONLY
- Round blob sizes up to the larger of the host and guest page sizes instead of rejecting sizes not aligned to blob_alignment, and require the userptr address to be aligned to blob_alignment
- 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   | 52 ++++++++++++++++++++----
 drivers/gpu/drm/virtio/virtgpu_kms.c     |  8 +++-
 4 files changed, 52 insertions(+), 10 deletions(-)
drivers/gpu/drm/virtio/virtgpu_debugfs.c +1 -0
index 3a68a16b58a..b8b9b405844 100644
@@ -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
index 52c174f9bff..a97ef7a595a 100644
@@ -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 +44 -8

94 lines not on this page · raw message

drivers/gpu/drm/virtio/virtgpu_kms.c +6 -2
index 1d4d3bf46a2..06c2bded49a 100644
@@ -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
#4 lore raw

Re: [PATCH v11 3/4] drm/virtio: implement userptr resource support

In reply to Honglei Huang
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] drm/virtio: duplicated virtio_gpu_userptr_free() and dead userptr struct fields --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] drm/virtio: duplicated virtio_gpu_userptr_free() and dead userptr struct fields
--

commit b8bdb9c42be0c294b8ab707b39984b6a80ab6db5
Author: Honglei Huang <[email protected]>

drm/virtio: implement userptr resource support

This commit introduces virtio_gpu_object_userptr to pin userspace memory pages and create blob resources backed by them, including hibernation restore support.

Quoted 4 lines
drivers/gpu/drm/virtio/virtgpu_drv.h +0 -0
index f3bbbe4468bf1..635122c68a29c 100644

[ ... ]

Quoted 30 lines
drivers/gpu/drm/virtio/virtgpu_drv.h +26 -0
@@ -140,12 +141,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
new file mode 100644
index 0000000000000..eddf29bad0094
@@ -0,0 +1,352 @@

[ ... ]

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, which could be exported and reused instead.

Also, in struct virtio_gpu_object_userptr, bo_handle is never used, file is assigned in virtio_gpu_userptr_init() (storing an unreferenced drm_file pointer on an object that can outlive the file descriptor across asynchronous RESOURCE_UNREF completion) and never read, and virtio_gpu_object_userptr_ops has only a single implementation so virtio_gpu_userptr_get_pages() and virtio_gpu_userptr_put_pages() can be called directly.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#6 lore raw

Re: [PATCH v11 4/4] drm/virtio: wire blob ioctl creation to userptr objects

In reply to Honglei Huang
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] drm/virtio: out-of-bounds user page pinning in verify_blob() on size ALIGN() It also noticed 1…

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] drm/virtio: out-of-bounds user page pinning in verify_blob() on size ALIGN()
It also noticed 1 related pre-existing issue this patch does not introduce:
- [Critical] drm/virtio: silent blob size truncation in verify_blob()
[1]
--

commit 47f959d0cfefd215bffd18078894e965cea40441
Author: Honglei Huang <[email protected]>

drm/virtio: wire blob ioctl creation to userptr objects

This commit wires the blob resource creation ioctl to userptr objects, adds support for the VIRTIO_GPU_F_BLOB_READONLY feature, and adjusts blob size and userptr alignment handling.

Quoted 4 lines
drivers/gpu/drm/virtio/virtgpu_ioctl.c +0 -0
index 81e70a12b3569..454d0e9a7d59d 100644

[ ... ]

drivers/gpu/drm/virtio/virtgpu_ioctl.c static int verify_blob(struct virtio_gpu_device *vgdev, · line 568

Quoted 5 lines
drivers/gpu/drm/virtio/virtgpu_ioctl.c +1 -0
@@ -538,10 +565,16 @@ 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;

[Severity: Critical]
This problem wasn't introduced by this patch, but does assigning the 64-bit rc_blob->size to params->size just above this [1] silently truncate blob sizes over 4 GB on 32-bit architectures?

params->size is an unsigned long, so on 32-bit systems the upper 32 bits of rc_blob->size are discarded before alignment checks, allocation, and host command submission, allocating a smaller buffer than userspace requested.

Quoted 4 lines
drivers/gpu/drm/virtio/virtgpu_ioctl.c +1 -3
-	if (vgdev->has_blob_alignment &&
-	    !IS_ALIGNED(params->size, vgdev->blob_alignment))
-		return -EINVAL;
+	if (vgdev->has_blob_alignment) {

drivers/gpu/drm/virtio/virtgpu_ioctl.c static int verify_blob(struct virtio_gpu_device *vgdev, · line 580

Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_ioctl.c +7 -0
+		params->size = ALIGN(params->size,
+				     max_t(uint32_t, vgdev->blob_alignment,
+					   PAGE_SIZE));
+
+		if (!IS_ALIGNED(params->userptr, vgdev->blob_alignment))
+			return -EINVAL;
+	}
 
 	return 0;
 }

[Severity: High]
When params->userptr is non-zero and vgdev->blob_alignment is larger than PAGE_SIZE, can rounding params->size up to vgdev->blob_alignment cause virtio_gpu_userptr_create() to pin guest pages past the end of the user-supplied buffer?

Unlike shmem or VRAM blobs that allocate new backing storage of params->size, virtio_gpu_userptr_init() sets userptr->npages = params->size >> PAGE_SHIFT and virtio_gpu_userptr_get_pages() pins that many pages from current->mm starting at userptr->start. If userspace passes a userptr buffer whose size is not a multiple of vgdev->blob_alignment, the extra pages past rc_blob->userptr + rc_blob->size either fail in pin_user_pages_fast() with -EFAULT or pin and export unrelated pages from an adjacent VMA to the host device.

[1] https://sashiko.dev/#/bug/linux-20577b57-1c85-44eb-8080-3e8c4e2bce2a

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#7 lore raw
In reply to Honglei Huang
Sashiko has finished reviewing this 4-patch series:

Sashiko has finished reviewing this 4-patch series:

- Patches 1 and 2: no issues found.
- Patch 3 ("drm/virtio: implement userptr resource support"): 1 potential issue (1 Low)
- Patch 4 ("drm/virtio: wire blob ioctl creation to userptr objects"): 1 potential issue (1 High) and 1 related pre-existing bug

Detailed reviews have been sent as separate replies to patches 3 and 4.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]