drm/amdgpu: validate userq input args
Subsystems (2)
PATCH v2 1/11 drm/amdgpu: validate userq input args
This will help on validating the userq input args, and rejecting for the invalidate userq request at the IOCTLs first place.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 80 +++++++++++++++------- drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 14 ++-- 2 files changed, 62 insertions(+), 32 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +55 -25
@@ -359,27 +359,10 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) (args->in.flags &AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK ) >>AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_SHIFT ; -/* Usermode queues are only supported for GFX IP as of now */ -if (args->in.ip_type !=AMDGPU_HW_IP_GFX && - args->in.ip_type !=AMDGPU_HW_IP_DMA && - args->in.ip_type !=AMDGPU_HW_IP_COMPUTE ) { -drm_file_err (uq_mgr->file,"Usermode queue doesn't support IP type %u\n" , - args->in.ip_type); -return -EINVAL ; - } - r =amdgpu_userq_priority_permit (filp, priority);if (r)return r; -if ((args->in.flags &AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE ) && - (args->in.ip_type !=AMDGPU_HW_IP_GFX ) && - (args->in.ip_type !=AMDGPU_HW_IP_COMPUTE ) && - !amdgpu_is_tmz (adev)) { -drm_file_err (uq_mgr->file,"Secure only supported on GFX/Compute queues\n" ); -return -EINVAL ; - } - r =pm_runtime_get_sync (adev_to_drm (adev)->dev);if (r <0 ) {drm_file_err (uq_mgr->file,"pm_runtime_get_sync() failed for userqueue create\n" );
@@ -485,22 +468,44 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)return r; } -int amdgpu_userq_ioctl (struct drm_device *dev,void *data, -struct drm_file *filp) +static int amdgpu_userq_input_args_validate (struct drm_device *dev, +union drm_amdgpu_userq *args, +struct drm_file *filp) { -union drm_amdgpu_userq *args = data; -int r; +struct amdgpu_device *adev =drm_to_adev (dev);switch (args->in.op) {case AMDGPU_USERQ_OP_CREATE :if (args->in.flags & ~(AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK |AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE ))return -EINVAL ; - r =amdgpu_userq_create (filp, args); -if (r) -drm_file_err (filp,"Failed to create usermode queue\n" ); -break ; +/* Usermode queues are only supported for GFX IP as of now */ +if (args->in.ip_type !=AMDGPU_HW_IP_GFX && + args->in.ip_type !=AMDGPU_HW_IP_DMA && + args->in.ip_type !=AMDGPU_HW_IP_COMPUTE ) { +drm_file_err (filp,"Usermode queue doesn't support IP type %u\n" , + args->in.ip_type); +return -EINVAL ; + } + +if ((args->in.flags &AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE ) && + (args->in.ip_type !=AMDGPU_HW_IP_GFX ) && + (args->in.ip_type !=AMDGPU_HW_IP_COMPUTE ) && + !amdgpu_is_tmz (adev)) { +drm_file_err (filp,"Secure only supported on GFX/Compute queues\n" ); +return -EINVAL ; + } +if (args->in.queue_va ==AMDGPU_BO_INVALID_OFFSET || + args->in.queue_size ==0 ) { +drm_file_err (filp,"invalidate userq queue va or size\n" ); +return -EINVAL ; + } +if (!args->in.wptr_va || !args->in.rptr_va) { +drm_file_err (filp,"invalidate userq queue rptr or wptr\n" ); +return -EINVAL ; + } +break ;case AMDGPU_USERQ_OP_FREE : if (args->in.ip_type || args->in.doorbell_handle ||
@@ -514,6 +519,31 @@ int amdgpu_userq_ioctl(struct drm_device *dev, void *data, args->in.mqd || args->in.mqd_size) return -EINVAL; +break ; +default : +return -EINVAL ; + } + +return 0 ; +} + +int amdgpu_userq_ioctl (struct drm_device *dev,void *data, +struct drm_file *filp) +{ +union drm_amdgpu_userq *args = data; +int r; + +if (amdgpu_userq_input_args_validate (dev, args, filp) <0 ) +return -EINVAL ; + +switch (args->in.op) { +case AMDGPU_USERQ_OP_CREATE : + r =amdgpu_userq_create (filp, args); +if (r) +drm_file_err (filp,"Failed to create usermode queue\n" ); +break ; + +case AMDGPU_USERQ_OP_FREE : r =amdgpu_userq_destroy (filp, args->in.queue_id);if (r)drm_file_err (filp,"Failed to destroy usermode queue\n" );
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +7 -7
@@ -208,6 +208,13 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,struct amdgpu_mqd_prop *userq_props;int r; +/* validate the user va at early time to avoid the time cost on building memory resource*/ +if (!mqd_user->wptr_va || !mqd_user->rptr_va || + !mqd_user->queue_va || mqd_user->queue_size ==0 ) { +DRM_ERROR ("Invalid MQD parameters for userqueue\n" ); +return -EINVAL ; + } +/* Structure to initialize MQD for userqueue using generic MQD init function */ userq_props =kzalloc (sizeof (struct amdgpu_mqd_prop ),GFP_KERNEL );if (!userq_props) {
@@ -215,13 +222,6 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,return -ENOMEM ; } -if (!mqd_user->wptr_va || !mqd_user->rptr_va || - !mqd_user->queue_va || mqd_user->queue_size ==0 ) { -DRM_ERROR ("Invalid MQD parameters for userqueue\n" ); - r = -EINVAL ; - goto free_props; - } - r =amdgpu_userq_create_object (uq_mgr, &queue->mqd, mqd_hw_default->mqd_size);if (r) {DRM_ERROR ("Failed to create MQD object for userqueue\n" );
Signature · 1 line
2.34.1
PATCH v2 2/11 drm/amdgpu: validate unmap status for destroying userq
Before destroying the userq buffer object, it requires validating the userq unmap status and ensuring the userq is unmapped from hardware.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 5 +++++ 1 file changed, 5 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -0
@@ -319,6 +319,11 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id) }amdgpu_bo_unref (&queue->db_obj.obj); r =amdgpu_userq_unmap_helper (uq_mgr, queue); +/*TODO: It requires a reset for unmap error*/ +if (r !=AMDGPU_USERQ_STATE_UNMAPPED ) { +drm_warn (adev_to_drm (uq_mgr->adev),"trying to destroy a HW mapping userq\n" ); + r = -ETIMEDOUT ; + }amdgpu_userq_cleanup (uq_mgr, queue, queue_id);mutex_unlock (&uq_mgr->userq_mutex);
Signature · 1 line
2.34.1
Re: [PATCH v2 02/11] drm/amdgpu: validate unmap status for destroying userq
Quoted 6 lines · Prike Liang
Before destroying the userq buffer object, it requires validating the userq unmap status and ensuring the userq is unmapped from hardware.
Signed-off-by Prike Liang <[email protected]>
Quoted 23 lines
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 5 +++++ 1 file changed, 5 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5
-1@@ -319,6 +319,11 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id) }amdgpu_bo_unref (&queue->db_obj.obj); r =amdgpu_userq_unmap_helper (uq_mgr, queue); +/*TODO: It requires a reset for unmap error*/ +if (r !=AMDGPU_USERQ_STATE_UNMAPPED ) { +drm_warn (adev_to_drm (uq_mgr->adev),"trying to destroy a HW mapping userq\n" ); + r = -ETIMEDOUT ; + }amdgpu_userq_cleanup (uq_mgr, queue, queue_id);mutex_unlock (&uq_mgr->userq_mutex); --2.34.1
PATCH v2 3/11 drm/amdgpu: unify the userq doorbell object destroy
In reply to Prike LiangUnify and refine the userq doorbell object destroy.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 1 - drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 3 +++ 2 files changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -1
@@ -317,7 +317,6 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id)amdgpu_bo_unpin (queue->db_obj.obj);amdgpu_bo_unreserve (queue->db_obj.obj); } -amdgpu_bo_unref (&queue->db_obj.obj); r =amdgpu_userq_unmap_helper (uq_mgr, queue);/*TODO: It requires a reset for unmap error*/ if (r !=AMDGPU_USERQ_STATE_UNMAPPED ) {
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +3 -0
@@ -343,6 +343,9 @@ mes_userq_mqd_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_queue *queue) {amdgpu_userq_destroy_object (uq_mgr, &queue->fw_obj); +if (queue->db_obj.obj->tbo.pin_count) +amdgpu_bo_unpin (queue->db_obj.obj); +amdgpu_userq_destroy_object (uq_mgr, &queue->db_obj);kfree (queue->userq_prop);amdgpu_userq_destroy_object (uq_mgr, &queue->mqd); }
Signature · 1 line
2.34.1
Re: [PATCH v2 03/11] drm/amdgpu: unify the userq doorbell object destroy
Quoted 26 lines · Prike Liang
Unify and refine the userq doorbell object destroy.
Signed-off-by Prike Liang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 1 - drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 3 +++ 2 files changed, 3 insertions(+), 1 deletion(-)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-1@@ -317,7 +317,6 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id)amdgpu_bo_unpin (queue->db_obj.obj);amdgpu_bo_unreserve (queue->db_obj.obj); } -amdgpu_bo_unref (&queue->db_obj.obj); r =amdgpu_userq_unmap_helper (uq_mgr, queue);/*TODO: It requires a reset for unmap error*/ if (r !=AMDGPU_USERQ_STATE_UNMAPPED ) {drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0
-0
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_mqd_destroy(struct amdgpu_userq_mgr *uq_mgr, · line 346
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +1
-0@@ -343,6 +343,9 @@ mes_userq_mqd_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_queue *queue) {amdgpu_userq_destroy_object (uq_mgr, &queue->fw_obj); +if (queue->db_obj.obj->tbo.pin_count)
I don't think you need to check the pin count. Other than that, looks good.
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +2
-1+amdgpu_bo_unpin (queue->db_obj.obj); +amdgpu_userq_destroy_object (uq_mgr, &queue->db_obj);kfree (queue->userq_prop);amdgpu_userq_destroy_object (uq_mgr, &queue->mqd); } --2.34.1
RE: [PATCH v2 03/11] drm/amdgpu: unify the userq doorbell object destroy
[Public]
Quoted 37 lines
From: Alex Deucher <[email protected]>
Sent: Tuesday, June 17, 2025 11:14 PM
To: Liang, Prike <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>
Subject: Re: [PATCH v2 03/11] drm/amdgpu: unify the userq doorbell object destroydrivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_mqd_destroy(struct amdgpu_userq_mgr
Quoted 28 lines · Prike Liang
Unify and refine the userq doorbell object destroy.
Signed-off-by Prike Liang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 1 - drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 3 +++ 2 files changed, 3 insertions(+), 1 deletion(-)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-1@@ -317,7 +317,6 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id)amdgpu_bo_unpin (queue->db_obj.obj);amdgpu_bo_unreserve (queue->db_obj.obj); } -amdgpu_bo_unref (&queue->db_obj.obj); r =amdgpu_userq_unmap_helper (uq_mgr, queue);/*TODO: It requires a reset for unmap error*/ if (r != AMDGPU_USERQ_STATE_UNMAPPED) { diff --git
a/drivers/gpu/drm/amd/amdgpu/mes_userqueue.cb/drivers/gpu/drm/amd/amdgpu/mes_userqueue.c
index39decc0b00f7..c6f7b613e684100644drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0
-0@@ -343,6 +343,9 @@ mes_userq_mqd_destroy(struct amdgpu_userq_mgr
*uq_mgr,
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_mqd_destroy(struct amdgpu_userq_mgr *uq_mgr, · line 346
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +1
-0struct amdgpu_usermode_queue *queue) {amdgpu_userq_destroy_object (uq_mgr, &queue->fw_obj); +if (queue->db_obj.obj->tbo.pin_count)I don't think you need to check the pin count. Other than that, looks good.
[Prike] This pin count check is used for the case of amdgpu_userq_cleanup() invoked by amdgpu_userq_destroy(). If we want to remove the pin count check here then might need to clean up the doorbell object unpin at amdgpu_userq_destroy() before, otherwise, the driver will complain the double unpin during destroying userq.
Quoted 10 lines
Reviewed-by Alex Deucher <[email protected]>Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +2
-1+amdgpu_bo_unpin (queue->db_obj.obj); +amdgpu_userq_destroy_object (uq_mgr, &queue->db_obj);kfree (queue->userq_prop);amdgpu_userq_destroy_object (uq_mgr, &queue->mqd); } --2.34.1
PATCH v2 4/11 drm/amdgpu: validate userq buffer virtual address and size
In reply to Prike LiangIt needs to validate the userq object virtual address whether it is validated in vm mapping.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 50 +++++++++++++++++++++- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 2 + drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 24 +++++++++++ 3 files changed, 74 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +48 -2
@@ -31,6 +31,8 @@#include "amdgpu_userq.h" #include "amdgpu_userq_fence.h" +#define amdgpu_userq_va_align (va) (va & AMDGPU_GMC_HOLE_MASK) >> AMDGPU_GPU_PAGE_SHIFT +u32 amdgpu_userq_get_supported_ip_mask (struct amdgpu_device *adev) {int i;
@@ -44,6 +46,36 @@ u32 amdgpu_userq_get_supported_ip_mask(struct amdgpu_device *adev)return userq_ip_mask; } +int amdgpu_userq_input_va_validate (struct amdgpu_vm *vm,u64 addr, +u64 expected_size) +{ +struct amdgpu_bo_va_mapping *va_map; +u64 user_addr; +u64 size; +int r; + + user_addr =amdgpu_userq_va_align (addr); + size = expected_size >>AMDGPU_GPU_PAGE_SHIFT ; + + r =amdgpu_bo_reserve (vm->root.bo, false); +if (r) +return r; + + va_map =amdgpu_vm_bo_lookup_mapping (vm, user_addr); +if (!va_map) + goto out_err; +/* Only validate the userq whether residen in the VM mapping range */ +if (user_addr >= va_map->start && + (size !=0 && user_addr + size -1 <= va_map->last)) { +amdgpu_bo_unreserve (vm->root.bo); +return 0 ; + } + +out_err: +amdgpu_bo_unreserve (vm->root.bo); +return -EINVAL ; +} +static int amdgpu_userq_unmap_helper (struct amdgpu_userq_mgr *uq_mgr,struct amdgpu_usermode_queue *queue)
@@ -391,6 +423,14 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) r = -EINVAL ; goto unlock; } +/* Validate the userq virtual address.*/ +if (amdgpu_userq_input_va_validate (&fpriv->vm, args->in.queue_va, args->in.queue_size) || +amdgpu_userq_input_va_validate (&fpriv->vm, args->in.rptr_va,PAGE_SIZE ) || +amdgpu_userq_input_va_validate (&fpriv->vm, args->in.wptr_va,PAGE_SIZE )) { +drm_file_err (uq_mgr->file,"Usermode queue input virt address is invalid\n" ); + r = -EINVAL ; + goto unlock; + } queue =kzalloc (sizeof (struct amdgpu_usermode_queue ),GFP_KERNEL );if (!queue) {
@@ -501,11 +541,17 @@ static int amdgpu_userq_input_args_validate(struct drm_device *dev, }if (args->in.queue_va ==AMDGPU_BO_INVALID_OFFSET || - args->in.queue_size ==0 ) { + args->in.queue_size ==0 || + !access_ok (u64_to_user_ptr (args->in.queue_va &AMDGPU_GMC_HOLE_MASK ), + args->in.queue_size)) {drm_file_err (filp,"invalidate userq queue va or size\n" );return -EINVAL ; } -if (!args->in.wptr_va || !args->in.rptr_va) { +if (!args->in.wptr_va || !args->in.rptr_va || + !access_ok (u64_to_user_ptr (args->in.wptr_va &AMDGPU_GMC_HOLE_MASK ), +sizeof (uint64_t )) || + !access_ok (u64_to_user_ptr (args->in.rptr_va &AMDGPU_GMC_HOLE_MASK ), +sizeof (uint64_t ))) {drm_file_err (filp,"invalidate userq queue rptr or wptr\n" );return -EINVAL ; }
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2 -0
@@ -132,4 +132,6 @@ int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,int amdgpu_userq_start_sched_for_enforce_isolation (struct amdgpu_device *adev,u32 idx); +int amdgpu_userq_input_va_validate (struct amdgpu_vm *vm,u64 addr, +u64 expected_size);#endif
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +24 -0
@@ -28,6 +28,8 @@#define AMDGPU_USERQ_PROC_CTX_SZ PAGE_SIZE#define AMDGPU_USERQ_GANG_CTX_SZ PAGE_SIZE +#define MQD_SHADOW_BASE_SIZE 73728 +#define MQD_FWWORKAREA_SIZE 484static int mes_userq_map_gtt_bo_to_gart (struct amdgpu_bo *bo)
@@ -254,6 +256,13 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr, goto free_mqd; } +if (amdgpu_userq_input_va_validate (queue->vm, compute_mqd->eop_va, +max_t (u32,PAGE_SIZE ,AMDGPU_GPU_PAGE_SIZE ))) { +drm_file_err (uq_mgr->file,"EOP VA is invalid\n" ); + r = -EINVAL ; + goto free_mqd; + } + userq_props->eop_gpu_addr = compute_mqd->eop_va; userq_props->hqd_pipe_priority =AMDGPU_GFX_PIPE_PRIO_NORMAL ; userq_props->hqd_queue_priority =AMDGPU_GFX_QUEUE_PRIORITY_MINIMUM ;
@@ -281,6 +290,14 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr, userq_props->csa_addr = mqd_gfx_v11->csa_va; userq_props->tmz_queue = mqd_user->flags &AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE ; + +if (amdgpu_userq_input_va_validate (queue->vm, mqd_gfx_v11->shadow_va, +MQD_SHADOW_BASE_SIZE )) { +drm_file_err (uq_mgr->file,"shadow VA is invalid\n" ); + r = -EINVAL ; + goto free_mqd; + } +kfree (mqd_gfx_v11); }else if (queue->queue_type ==AMDGPU_HW_IP_DMA ) {struct drm_amdgpu_userq_mqd_sdma_gfx11 *mqd_sdma_v11;
@@ -298,6 +315,13 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr, goto free_mqd; } +if (amdgpu_userq_input_va_validate (queue->vm, mqd_sdma_v11->csa_va, +MQD_FWWORKAREA_SIZE )) { +drm_file_err (uq_mgr->file,"CSA VA is invalid\n" ); + r = -EINVAL ; + goto free_mqd; + } + userq_props->csa_addr = mqd_sdma_v11->csa_va;kfree (mqd_sdma_v11); }
Signature · 1 line
2.34.1
Re: [PATCH v2 04/11] drm/amdgpu: validate userq buffer virtual address and size
Quoted 37 lines · Prike Liang
It needs to validate the userq object virtual address whether it is validated in vm mapping.
Signed-off-by Prike Liang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 50 +++++++++++++++++++++- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 2 + drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 24 +++++++++++ 3 files changed, 74 insertions(+), 2 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +11
-0@@ -31,6 +31,8 @@#include "amdgpu_userq.h" #include "amdgpu_userq_fence.h" +#define amdgpu_userq_va_align (va) (va & AMDGPU_GMC_HOLE_MASK) >> AMDGPU_GPU_PAGE_SHIFT +u32 amdgpu_userq_get_supported_ip_mask (struct amdgpu_device *adev) {int i;@@ -44,6 +46,36 @@ u32 amdgpu_userq_get_supported_ip_mask(struct amdgpu_device *adev)return userq_ip_mask; } +int amdgpu_userq_input_va_validate (struct amdgpu_vm *vm,u64 addr, +u64 expected_size) +{ +struct amdgpu_bo_va_mapping *va_map; +u64 user_addr; +u64 size; +int r; + + user_addr =amdgpu_userq_va_align (addr);
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c u32 amdgpu_userq_get_supported_ip_mask(struct amdgpu_device *adev) · line 67
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10
-0+ size = expected_size >>AMDGPU_GPU_PAGE_SHIFT ; + + r =amdgpu_bo_reserve (vm->root.bo, false); +if (r) +return r; + + va_map =amdgpu_vm_bo_lookup_mapping (vm, user_addr); +if (!va_map) + goto out_err; +/* Only validate the userq whether residen in the VM mapping range */
resident
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c u32 amdgpu_userq_get_supported_ip_mask(struct amdgpu_device *adev) · line 69
Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-0+if (user_addr >= va_map->start && + (size !=0 && user_addr + size -1 <= va_map->last)) {
indentation here looks off.
Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +9
-0+amdgpu_bo_unreserve (vm->root.bo); +return 0 ; + } + +out_err: +amdgpu_bo_unreserve (vm->root.bo); +return -EINVAL ; +} +static int amdgpu_userq_unmap_helper (struct amdgpu_userq_mgr *uq_mgr,struct amdgpu_usermode_queue *queue)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) · line 429
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4
-0@@ -391,6 +423,14 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) r = -EINVAL ; goto unlock; } +/* Validate the userq virtual address.*/ +if (amdgpu_userq_input_va_validate (&fpriv->vm, args->in.queue_va, args->in.queue_size) || +amdgpu_userq_input_va_validate (&fpriv->vm, args->in.rptr_va,PAGE_SIZE ) || +amdgpu_userq_input_va_validate (&fpriv->vm, args->in.wptr_va,PAGE_SIZE )) {
indentation here looks off.
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4
-0+drm_file_err (uq_mgr->file,"Usermode queue input virt address is invalid\n" ); + r = -EINVAL ; + goto unlock; + } queue =kzalloc (sizeof (struct amdgpu_usermode_queue ),GFP_KERNEL );if (!queue) {
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 546
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3
-1@@ -501,11 +541,17 @@ static int amdgpu_userq_input_args_validate(struct drm_device *dev, }if (args->in.queue_va ==AMDGPU_BO_INVALID_OFFSET || - args->in.queue_size ==0 ) { + args->in.queue_size ==0 || + !access_ok (u64_to_user_ptr (args->in.queue_va &AMDGPU_GMC_HOLE_MASK ), + args->in.queue_size)) {
indentation here looks off.
Also, the VAs are GPU virtual addresses not user virtual addresses so I don't think this check is valid.
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 554
Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5
-1drm_file_err (filp,"invalidate userq queue va or size\n" );return -EINVAL ; } -if (!args->in.wptr_va || !args->in.rptr_va) { +if (!args->in.wptr_va || !args->in.rptr_va || + !access_ok (u64_to_user_ptr (args->in.wptr_va &AMDGPU_GMC_HOLE_MASK ), +sizeof (uint64_t )) || + !access_ok (u64_to_user_ptr (args->in.rptr_va &AMDGPU_GMC_HOLE_MASK ), +sizeof (uint64_t ))) {
Same comment here.
Quoted 19 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-0drm_file_err (filp,"invalidate userq queue rptr or wptr\n" );return -EINVAL ; }drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2
-0@@ -132,4 +132,6 @@ int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,int amdgpu_userq_start_sched_for_enforce_isolation (struct amdgpu_device *adev,u32 idx); +int amdgpu_userq_input_va_validate (struct amdgpu_vm *vm,u64 addr, +u64 expected_size);#endif drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0
-0
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c line 32
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +2
-0@@ -28,6 +28,8 @@#define AMDGPU_USERQ_PROC_CTX_SZ PAGE_SIZE#define AMDGPU_USERQ_GANG_CTX_SZ PAGE_SIZE +#define MQD_SHADOW_BASE_SIZE 73728 +#define MQD_FWWORKAREA_SIZE 484
These might be different across IP versions. Might be better to query these from gfx? there is get_gfx_shadow_info in struct amdgpu_gfx_funcs.
Alex
Quoted 49 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +22
-1static int mes_userq_map_gtt_bo_to_gart (struct amdgpu_bo *bo)@@ -254,6 +256,13 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr, goto free_mqd; } +if (amdgpu_userq_input_va_validate (queue->vm, compute_mqd->eop_va, +max_t (u32,PAGE_SIZE ,AMDGPU_GPU_PAGE_SIZE ))) { +drm_file_err (uq_mgr->file,"EOP VA is invalid\n" ); + r = -EINVAL ; + goto free_mqd; + } + userq_props->eop_gpu_addr = compute_mqd->eop_va; userq_props->hqd_pipe_priority =AMDGPU_GFX_PIPE_PRIO_NORMAL ; userq_props->hqd_queue_priority =AMDGPU_GFX_QUEUE_PRIORITY_MINIMUM ;@@ -281,6 +290,14 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr, userq_props->csa_addr = mqd_gfx_v11->csa_va; userq_props->tmz_queue = mqd_user->flags &AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE ; + +if (amdgpu_userq_input_va_validate (queue->vm, mqd_gfx_v11->shadow_va, +MQD_SHADOW_BASE_SIZE )) { +drm_file_err (uq_mgr->file,"shadow VA is invalid\n" ); + r = -EINVAL ; + goto free_mqd; + } +kfree (mqd_gfx_v11); }else if (queue->queue_type ==AMDGPU_HW_IP_DMA ) {struct drm_amdgpu_userq_mqd_sdma_gfx11 *mqd_sdma_v11;@@ -298,6 +315,13 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr, goto free_mqd; } +if (amdgpu_userq_input_va_validate (queue->vm, mqd_sdma_v11->csa_va, +MQD_FWWORKAREA_SIZE )) { +drm_file_err (uq_mgr->file,"CSA VA is invalid\n" ); + r = -EINVAL ; + goto free_mqd; + } + userq_props->csa_addr = mqd_sdma_v11->csa_va;kfree (mqd_sdma_v11); } --2.34.1
RE: [PATCH v2 04/11] drm/amdgpu: validate userq buffer virtual address and size
[Public]
Regards,
Prike
Quoted 90 lines
-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Alex Deucher
Sent: Tuesday, June 17, 2025 11:25 PM
To: Liang, Prike <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>
Subject: Re: [PATCH v2 04/11] drm/amdgpu: validate userq buffer virtual address and sizedrivers/gpu/drm/amd/amdgpu/amdgpu_userq.c u32 amdgpu_userq_get_supported_ip_mask(struct
Quoted 26 lines · Prike Liang
It needs to validate the userq object virtual address whether it is validated in vm mapping.
Signed-off-by Prike Liang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 50 +++++++++++++++++++++- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 2 + drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 24 +++++++++++ 3 files changed, 74 insertions(+), 2 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3
-0@@ -31,6 +31,8 @@#include "amdgpu_userq.h" #include "amdgpu_userq_fence.h" +#define amdgpu_userq_va_align (va) (va & AMDGPU_GMC_HOLE_MASK) >> +AMDGPU_GPU_PAGE_SHIFT + u32 amdgpu_userq_get_supported_ip_mask(struct amdgpu_device *adev) {int i;@@ -44,6 +46,36 @@ u32 amdgpu_userq_get_supported_ip_mask(struct
amdgpu_device*adev)Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +9
-0return userq_ip_mask; } +int amdgpu_userq_input_va_validate (struct amdgpu_vm *vm,u64 addr, +u64 expected_size) { +struct amdgpu_bo_va_mapping *va_map; +u64 user_addr; +u64 size; +int r; + + user_addr =amdgpu_userq_va_align (addr); + size = expected_size >>AMDGPU_GPU_PAGE_SHIFT ;drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c u32 amdgpu_userq_get_supported_ip_mask(struct amdgpu_device *adev)
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10
-0+ + r =amdgpu_bo_reserve (vm->root.bo, false); +if (r) +return r; + + va_map =amdgpu_vm_bo_lookup_mapping (vm, user_addr); +if (!va_map) + goto out_err; +/* Only validate the userq whether residen in the VM mapping +range */ resident
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c u32 amdgpu_userq_get_supported_ip_mask(struct · line 70
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3
-0+if (user_addr >= va_map->start && + (size !=0 && user_addr + size -1 <= + va_map->last)) {indentation here looks off.
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c u32 amdgpu_userq_get_supported_ip_mask(struct amdgpu_device *adev)
Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +9
-1+amdgpu_bo_unreserve (vm->root.bo); +return 0 ; + } + +out_err: +amdgpu_bo_unreserve (vm->root.bo); +return -EINVAL ; +} +static int amdgpu_userq_unmap_helper (struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_queue *queue) @@ -391,6 +423,14 @@ amdgpu_userq_create(struct drm_file *filp, union
drm_amdgpu_userq*args)Quoted 7 lines
r = -EINVAL; goto unlock; }drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-0+/* Validate the userq virtual address.*/ + if (amdgpu_userq_input_va_validate(&fpriv->vm, args->in.queue_va, args-in.queue_size) ||
+ amdgpu_userq_input_va_validate(&fpriv->vm,args->in.rptr_va,
Quoted 5 lines
PAGE_SIZE) ||Quoted 2 lines
+ amdgpu_userq_input_va_validate(&fpriv->vm,
+args->in.wptr_va,PAGE_SIZE)) {indentation here looks off.
Note.
Quoted 16 lines
Quoted 1 line
+ drm_file_err(uq_mgr->file, "Usermode queue input virt address is
invalid\n");
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3
-0+ r = -EINVAL ; + goto unlock; + } queue =kzalloc (sizeof (struct amdgpu_usermode_queue ),GFP_KERNEL );if (!queue) {@@ -501,11 +541,17 @@ static int amdgpu_userq_input_args_validate(struct
drm_device*dev,drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev,
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-1}if (args->in.queue_va ==AMDGPU_BO_INVALID_OFFSET || - args->in.queue_size ==0 ) { + args->in.queue_size ==0 || + !access_ok(u64_to_user_ptr(args->in.queue_va &
Quoted 7 lines
AMDGPU_GMC_HOLE_MASK),drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 546
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-0+ args->in.queue_size)) {indentation here looks off.
Also, the VAs are GPU virtual addresses not user virtual addresses so I don't think this check is valid.
But the userq input IOCTL argument object VA is a cpu pointer and that is also a cpu visible memory, so here does it make sense to validate the cpu access through the access_ok()?
Quoted 37 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev,
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-1drm_file_err (filp,"invalidate userq queue va or size\n" );return -EINVAL ; } -if (!args->in.wptr_va || !args->in.rptr_va) { +if (!args->in.wptr_va || !args->in.rptr_va || + !access_ok(u64_to_user_ptr(args->in.wptr_va &
AMDGPU_GMC_HOLE_MASK),drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct · line 553
Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-0+ sizeof(uint64_t)) || + !access_ok(u64_to_user_ptr(args->in.rptr_va &
AMDGPU_GMC_HOLE_MASK),drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c static int amdgpu_userq_input_args_validate(struct drm_device *dev, · line 554
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-0+ sizeof(uint64_t))) {Same comment here.
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h int · line 132
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-0drm_file_err (filp,"invalidate userq queue rptr or wptr\n" );return -EINVAL ; }drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +0
-0@@ -132,4 +132,6 @@ int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,int amdgpu_userq_start_sched_for_enforce_isolation(struct
amdgpu_device*adev,Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2
-0u32 idx); +int amdgpu_userq_input_va_validate (struct amdgpu_vm *vm,u64 addr, +u64 expected_size);#endif drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0
-0@@ -28,6 +28,8 @@
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c line 32
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0
-0#define AMDGPU_USERQ_PROC_CTX_SZ PAGE_SIZE #define
AMDGPU_USERQ_GANG_CTX_SZPAGE_SIZEdrivers/gpu/drm/amd/amdgpu/mes_userqueue.c +2
-0+#define MQD_SHADOW_BASE_SIZE 73728 +#define MQD_FWWORKAREA_SIZE 484These might be different across IP versions. Might be better to query these from gfx? there is
get_gfx_shadow_infoin structamdgpu_gfx_funcs.
I thought use the adev->gfx.funcs->get_gfx_shadow_info () before, but since the size is a constant and same value between gfx11/gfx12, so I simplify it as a constant assignment. I will use the get information callback to adapt for the upcoming asics.
Quoted 60 lines
Alex
Quoted 10 lines
static int
mes_userq_map_gtt_bo_to_gart(struct amdgpu_bo *bo)@@ -254,6 +256,13
@@ static int mes_userq_mqd_create(structamdgpu_userq_mgr*uq_mgr,goto free_mqd; }+ if (amdgpu_userq_input_va_validate(queue->vm, compute_mqd-
eop_va,
+ max_t(u32,PAGE_SIZE,
AMDGPU_GPU_PAGE_SIZE))) {drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +5
-0+drm_file_err (uq_mgr->file,"EOP VA is invalid\n" ); + r = -EINVAL ; + goto free_mqd; + } + userq_props->eop_gpu_addr = compute_mqd->eop_va; userq_props->hqd_pipe_priority =
AMDGPU_GFX_PIPE_PRIO_NORMAL;drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_mqd_create(struct
Quoted 3 lines
userq_props->hqd_queue_priority =
AMDGPU_GFX_QUEUE_PRIORITY_MINIMUM;drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0
-0@@ -281,6 +290,14 @@ static int mes_userq_mqd_create(struct
amdgpu_userq_mgr*uq_mgr,drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_mqd_create(struct
Quoted 17 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +8
-0userq_props->csa_addr = mqd_gfx_v11->csa_va; userq_props->tmz_queue = mqd_user->flags &AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE ; + +if (amdgpu_userq_input_va_validate (queue->vm, mqd_gfx_v11- shadow_va, +MQD_SHADOW_BASE_SIZE )) { +drm_file_err (uq_mgr->file,"shadow VA is invalid\n" ); + r = -EINVAL ; + goto free_mqd; + } +kfree (mqd_gfx_v11);} else if (queue->queue_type == AMDGPU_HW_IP_DMA) { struct drm_amdgpu_userq_mqd_sdma_gfx11 *mqd_sdma_v11;drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0
-0@@ -298,6 +315,13 @@ static int mes_userq_mqd_create(struct
amdgpu_userq_mgr*uq_mgr,Quoted 17 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +7
-0goto free_mqd; } +if (amdgpu_userq_input_va_validate (queue->vm, mqd_sdma_v11- csa_va, +MQD_FWWORKAREA_SIZE )) { +drm_file_err (uq_mgr->file,"CSA VA is invalid\n" ); + r = -EINVAL ; + goto free_mqd; + } + userq_props->csa_addr = mqd_sdma_v11->csa_va;kfree (mqd_sdma_v11);}--
2.34.1
PATCH v2 5/11 drm/amdgpu: add userq object va track helpers
In reply to Prike LiangAdd the userq object virtual address get(),mapped() and put() helpers for tracking the userq obj va address usage.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 114 +++++++++++++++++++++- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 11 +++ 2 files changed, 124 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +113 -1
@@ -76,6 +76,116 @@ int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr,return -EINVAL ; } +int amdgpu_userq_buffer_va_get (struct amdgpu_vm *vm,u64 addr) +{ +struct amdgpu_bo_va_mapping *mapping; +u64 user_addr; +int r; + + user_addr =amdgpu_userq_va_align (addr); + r =amdgpu_bo_reserve (vm->root.bo, false); +if (r) +return r; + + mapping =amdgpu_vm_bo_lookup_mapping (vm, user_addr); +if (!mapping) + goto out_err; + +/* +* Need to unify the following userq va reference. +* mqd bo +* rptr bo +* wptr bo +* eop bo +* doorbell bo +*/ +/*amdgpu_bo_ref(mapping->bo_va->base.bo);*/ + mapping->bo_va->queue_refcount++; + +amdgpu_bo_unreserve (vm->root.bo); +return 0 ; + +out_err: +amdgpu_bo_unreserve (vm->root.bo); +return -EINVAL ; +} + +bool amdgpu_userq_buffer_va_mapped (struct amdgpu_vm *vm,u64 addr) +{ +struct amdgpu_bo_va_mapping *mapping; +u64 user_addr; +bool r; + + user_addr =amdgpu_userq_va_align (addr); + +if (amdgpu_bo_reserve (vm->root.bo, false)) +return false; + + mapping =amdgpu_vm_bo_lookup_mapping (vm, user_addr); +if (!IS_ERR_OR_NULL (mapping) && mapping->bo_va->queue_refcount >0 ) + r = true; +else + r = false; +amdgpu_bo_unreserve (vm->root.bo); + +return r; +} + +bool amdgpu_userq_buffer_vas_mapped (struct amdgpu_vm *vm, +struct amdgpu_usermode_queue *queue) +{ + +if (amdgpu_userq_buffer_va_mapped (vm, queue->queue_va) || +amdgpu_userq_buffer_va_mapped (vm, queue->rptr_va) || +amdgpu_userq_buffer_va_mapped (vm, queue->wptr_va)) +return true; +else +return false; +} + +int amdgpu_userq_buffer_va_put (struct amdgpu_vm *vm,u64 addr) +{ +struct amdgpu_bo_va_mapping *mapping; +u64 user_addr; +int r; + + user_addr =amdgpu_userq_va_align (addr); + r =amdgpu_bo_reserve (vm->root.bo, false); +if (r) +return r; + + mapping =amdgpu_vm_bo_lookup_mapping (vm, user_addr); +if (!mapping) + goto out_err; +/* +* As the GEM userq bo will be unmapped by amdgpu_vm_bo_unmap() which is +* invoked before destroying userq. So if the user wants to reference/dereference +* the userq vm bo, then ensure the userq vm bo is dereferenced before amdgpu_vm_bo_unmap() +* or unmap the userq bo VA at amdgpu_userq_destroy() directly; otherwise, that results in the +* userq va BOs being referred to unbalanced and causing the failure of amdgpu_vm_bo_unmap(). +*/ +/*amdgpu_bo_unref(&mapping->bo_va->base.bo);*/ + + mapping->bo_va->queue_refcount--; + +amdgpu_bo_unreserve (vm->root.bo); +return 0 ; + +out_err: +amdgpu_bo_unreserve (vm->root.bo); +return -EINVAL ; +} + +int amdgpu_userq_buffer_vas_put (struct amdgpu_vm *vm, +struct amdgpu_usermode_queue *queue) +{ +amdgpu_userq_buffer_va_put (vm, queue->queue_va); +amdgpu_userq_buffer_va_put (vm, queue->rptr_va); +amdgpu_userq_buffer_va_put (vm, queue->wptr_va); + +return 0 ; +} +static int amdgpu_userq_unmap_helper (struct amdgpu_userq_mgr *uq_mgr,struct amdgpu_usermode_queue *queue)
@@ -442,6 +552,9 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) queue->queue_type = args->in.ip_type; queue->vm = &fpriv->vm; queue->priority = priority; + queue->queue_va = args->in.queue_va; + queue->rptr_va = args->in.rptr_va; + queue->wptr_va = args->in.wptr_va; db_info.queue_type = queue->queue_type; db_info.doorbell_handle = queue->doorbell_handle;
@@ -472,7 +585,6 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) goto unlock; } - qid =idr_alloc (&uq_mgr->userq_idr, queue,1 ,AMDGPU_MAX_USERQ_COUNT ,GFP_KERNEL );if (qid <0 ) {drm_file_err (uq_mgr->file,"Failed to allocate a queue id\n" );
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +11 -0
@@ -52,6 +52,10 @@ struct amdgpu_usermode_queue {enum amdgpu_userq_state state;uint64_t doorbell_handle;uint64_t doorbell_index; +uint64_t queue_va; +uint64_t rptr_va; +uint64_t wptr_va; +uint64_t flags;struct amdgpu_mqd_prop *userq_prop;struct amdgpu_userq_mgr *userq_mgr;
@@ -134,4 +138,11 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,int amdgpu_userq_input_va_validate (struct amdgpu_vm *vm,u64 addr,u64 expected_size); +int amdgpu_userq_buffer_va_get (struct amdgpu_vm *vm,u64 addr); +bool amdgpu_userq_buffer_va_mapped (struct amdgpu_vm *vm,u64 addr); +bool amdgpu_userq_buffer_vas_mapped (struct amdgpu_vm *vm, +struct amdgpu_usermode_queue *queue); +int amdgpu_userq_buffer_va_put (struct amdgpu_vm *vm,u64 addr); +int amdgpu_userq_buffer_vas_put (struct amdgpu_vm *vm, +struct amdgpu_usermode_queue *queue);#endif
Signature · 1 line
2.34.1
Re: [PATCH v2 05/11] drm/amdgpu: add userq object va track helpers
Quoted 3 lines · Prike Liang
Add the userq object virtual address get(),mapped() and
put()helpers for tracking the userq obj va address usage.
Need to add the other metadata buffers to the tracking.
Quoted 93 lines
Signed-off-by Prike Liang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 114 +++++++++++++++++++++- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 11 +++ 2 files changed, 124 insertions(+), 1 deletion(-)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +78
-0@@ -76,6 +76,116 @@ int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr,return -EINVAL ; } +int amdgpu_userq_buffer_va_get (struct amdgpu_vm *vm,u64 addr) +{ +struct amdgpu_bo_va_mapping *mapping; +u64 user_addr; +int r; + + user_addr =amdgpu_userq_va_align (addr); + r =amdgpu_bo_reserve (vm->root.bo, false); +if (r) +return r; + + mapping =amdgpu_vm_bo_lookup_mapping (vm, user_addr); +if (!mapping) + goto out_err; + +/* +* Need to unify the following userq va reference. +* mqd bo +* rptr bo +* wptr bo +* eop bo +* doorbell bo +*/ +/*amdgpu_bo_ref(mapping->bo_va->base.bo);*/ + mapping->bo_va->queue_refcount++; + +amdgpu_bo_unreserve (vm->root.bo); +return 0 ; + +out_err: +amdgpu_bo_unreserve (vm->root.bo); +return -EINVAL ; +} + +bool amdgpu_userq_buffer_va_mapped (struct amdgpu_vm *vm,u64 addr) +{ +struct amdgpu_bo_va_mapping *mapping; +u64 user_addr; +bool r; + + user_addr =amdgpu_userq_va_align (addr); + +if (amdgpu_bo_reserve (vm->root.bo, false)) +return false; + + mapping =amdgpu_vm_bo_lookup_mapping (vm, user_addr); +if (!IS_ERR_OR_NULL (mapping) && mapping->bo_va->queue_refcount >0 ) + r = true; +else + r = false; +amdgpu_bo_unreserve (vm->root.bo); + +return r; +} + +bool amdgpu_userq_buffer_vas_mapped (struct amdgpu_vm *vm, +struct amdgpu_usermode_queue *queue) +{ + +if (amdgpu_userq_buffer_va_mapped (vm, queue->queue_va) || +amdgpu_userq_buffer_va_mapped (vm, queue->rptr_va) || +amdgpu_userq_buffer_va_mapped (vm, queue->wptr_va)) +return true; +else +return false; +} + +int amdgpu_userq_buffer_va_put (struct amdgpu_vm *vm,u64 addr) +{ +struct amdgpu_bo_va_mapping *mapping; +u64 user_addr; +int r; + + user_addr =amdgpu_userq_va_align (addr); + r =amdgpu_bo_reserve (vm->root.bo, false); +if (r) +return r; +
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr, · line 166
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10
-0+ mapping =amdgpu_vm_bo_lookup_mapping (vm, user_addr); +if (!mapping) + goto out_err; +/* +* As the GEM userq bo will be unmapped by amdgpu_vm_bo_unmap() which is +* invoked before destroying userq. So if the user wants to reference/dereference +* the userq vm bo, then ensure the userq vm bo is dereferenced before amdgpu_vm_bo_unmap() +* or unmap the userq bo VA at amdgpu_userq_destroy() directly; otherwise, that results in the +* userq va BOs being referred to unbalanced and causing the failure of amdgpu_vm_bo_unmap(). +*/
I don't follow this comment. Why wouldn't this be balanced?
Alex
Quoted 73 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +25
-1+/*amdgpu_bo_unref(&mapping->bo_va->base.bo);*/ + + mapping->bo_va->queue_refcount--; + +amdgpu_bo_unreserve (vm->root.bo); +return 0 ; + +out_err: +amdgpu_bo_unreserve (vm->root.bo); +return -EINVAL ; +} + +int amdgpu_userq_buffer_vas_put (struct amdgpu_vm *vm, +struct amdgpu_usermode_queue *queue) +{ +amdgpu_userq_buffer_va_put (vm, queue->queue_va); +amdgpu_userq_buffer_va_put (vm, queue->rptr_va); +amdgpu_userq_buffer_va_put (vm, queue->wptr_va); + +return 0 ; +} +static int amdgpu_userq_unmap_helper (struct amdgpu_userq_mgr *uq_mgr,struct amdgpu_usermode_queue *queue)@@ -442,6 +552,9 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) queue->queue_type = args->in.ip_type; queue->vm = &fpriv->vm; queue->priority = priority; + queue->queue_va = args->in.queue_va; + queue->rptr_va = args->in.rptr_va; + queue->wptr_va = args->in.wptr_va; db_info.queue_type = queue->queue_type; db_info.doorbell_handle = queue->doorbell_handle;@@ -472,7 +585,6 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) goto unlock; } - qid =idr_alloc (&uq_mgr->userq_idr, queue,1 ,AMDGPU_MAX_USERQ_COUNT ,GFP_KERNEL );if (qid <0 ) {drm_file_err (uq_mgr->file,"Failed to allocate a queue id\n" );drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +11
-1@@ -52,6 +52,10 @@ struct amdgpu_usermode_queue {enum amdgpu_userq_state state;uint64_t doorbell_handle;uint64_t doorbell_index; +uint64_t queue_va; +uint64_t rptr_va; +uint64_t wptr_va; +uint64_t flags;struct amdgpu_mqd_prop *userq_prop;struct amdgpu_userq_mgr *userq_mgr;@@ -134,4 +138,11 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,int amdgpu_userq_input_va_validate (struct amdgpu_vm *vm,u64 addr,u64 expected_size); +int amdgpu_userq_buffer_va_get (struct amdgpu_vm *vm,u64 addr); +bool amdgpu_userq_buffer_va_mapped (struct amdgpu_vm *vm,u64 addr); +bool amdgpu_userq_buffer_vas_mapped (struct amdgpu_vm *vm, +struct amdgpu_usermode_queue *queue); +int amdgpu_userq_buffer_va_put (struct amdgpu_vm *vm,u64 addr); +int amdgpu_userq_buffer_vas_put (struct amdgpu_vm *vm, +struct amdgpu_usermode_queue *queue);#endif --2.34.1
RE: [PATCH v2 05/11] drm/amdgpu: add userq object va track helpers
[Public]
Regards,
Prike
Quoted 8 lines
-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: Tuesday, June 17, 2025 11:35 PM
To: Liang, Prike <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>
Subject: Re: [PATCH v2 05/11] drm/amdgpu: add userq object va track helpers
Quoted 6 lines
Quoted 3 lines · Prike Liang
Add the userq object virtual address get(),mapped() and
put()helpers for tracking the userq obj va address usage.Need to add the other metadata buffers to the tracking.
Note.
Quoted 101 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_input_va_validate(struct amdgpu_vm
Quoted 14 lines
Signed-off-by Prike Liang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 114 +++++++++++++++++++++- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 11 +++ 2 files changed, 124 insertions(+), 1 deletion(-)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-0@@ -76,6 +76,116 @@ int amdgpu_userq_input_va_validate(struct amdgpu_vm*vm, u64 addr,
Quoted 72 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +69
-0return -EINVAL ; } +int amdgpu_userq_buffer_va_get (struct amdgpu_vm *vm,u64 addr) { +struct amdgpu_bo_va_mapping *mapping; +u64 user_addr; +int r; + + user_addr =amdgpu_userq_va_align (addr); + r =amdgpu_bo_reserve (vm->root.bo, false); +if (r) +return r; + + mapping =amdgpu_vm_bo_lookup_mapping (vm, user_addr); +if (!mapping) + goto out_err; + +/* +* Need to unify the following userq va reference. +* mqd bo +* rptr bo +* wptr bo +* eop bo +* doorbell bo +*/ +/*amdgpu_bo_ref(mapping->bo_va->base.bo);*/ + mapping->bo_va->queue_refcount++; + +amdgpu_bo_unreserve (vm->root.bo); +return 0 ; + +out_err: +amdgpu_bo_unreserve (vm->root.bo); +return -EINVAL ; +} + +bool amdgpu_userq_buffer_va_mapped (struct amdgpu_vm *vm,u64 addr) { +struct amdgpu_bo_va_mapping *mapping; +u64 user_addr; +bool r; + + user_addr =amdgpu_userq_va_align (addr); + +if (amdgpu_bo_reserve (vm->root.bo, false)) +return false; + + mapping =amdgpu_vm_bo_lookup_mapping (vm, user_addr); +if (!IS_ERR_OR_NULL (mapping) && mapping->bo_va->queue_refcount >0 ) + r = true; +else + r = false; +amdgpu_bo_unreserve (vm->root.bo); + +return r; +} + +bool amdgpu_userq_buffer_vas_mapped (struct amdgpu_vm *vm, +struct amdgpu_usermode_queue *queue) { + +if (amdgpu_userq_buffer_va_mapped (vm, queue->queue_va) || +amdgpu_userq_buffer_va_mapped (vm, queue->rptr_va) || +amdgpu_userq_buffer_va_mapped (vm, queue->wptr_va)) +return true; +else +return false; +} + +int amdgpu_userq_buffer_va_put (struct amdgpu_vm *vm,u64 addr) { +struct amdgpu_bo_va_mapping *mapping; +u64 user_addr; +int r; +drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr,
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10
-0+ user_addr =amdgpu_userq_va_align (addr); + r =amdgpu_bo_reserve (vm->root.bo, false); +if (r) +return r; + + mapping =amdgpu_vm_bo_lookup_mapping (vm, user_addr); +if (!mapping) + goto out_err; +/* +* As the GEM userq bo will be unmapped by amdgpu_vm_bo_unmap() which is
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_input_va_validate(struct amdgpu_vm · line 158
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-0+* invoked before destroying userq. So if the user wants to reference/dereference
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_input_va_validate(struct amdgpu_vm · line 159
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-0+* the userq vm bo, then ensure the userq vm bo is dereferenced before
Quoted 8 lines
amdgpu_vm_bo_unmap()drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_input_va_validate(struct amdgpu_vm · line 160
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-0+* or unmap the userq bo VA at amdgpu_userq_destroy() directly; otherwise, that results in the
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_input_va_validate(struct amdgpu_vm · line 161
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-0+* userq va BOs being referred to unbalanced and causing the failure of
amdgpu_vm_bo_unmap().drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_input_va_validate(struct amdgpu_vm · line 162
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-0+*/ I don't follow this comment. Why wouldn't this be balanced?
If we refer to the userq VM BOs and keep the usage count by amdgpu_bo_ref(mapping->bo_va->base.bo) at creating the userq IOCTL, this reference and usage counter will be kept until amdgpu_userq_destroy(), while the userq VA is unmapped at amdgpu_vm_bo_unmap(), which is ahead of amdgpu_userq_destroy(). So, when amdgpu_vm_bo_unmap() tries to unmap the userq VA mapping, it will result in an unmap error caused by the BO's reference.
Quoted 79 lines
Alex
Quoted 15 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +15
-0+/*amdgpu_bo_unref(&mapping->bo_va->base.bo);*/ + + mapping->bo_va->queue_refcount--; + +amdgpu_bo_unreserve (vm->root.bo); +return 0 ; + +out_err: +amdgpu_bo_unreserve (vm->root.bo); +return -EINVAL ; +} + +int amdgpu_userq_buffer_vas_put (struct amdgpu_vm *vm, +struct amdgpu_usermode_queue *queue) { +amdgpu_userq_buffer_va_put (vm, queue->queue_va);drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr,
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +6
-1+amdgpu_userq_buffer_va_put (vm, queue->rptr_va); +amdgpu_userq_buffer_va_put (vm, queue->wptr_va); + +return 0 ; +} +static int amdgpu_userq_unmap_helper (struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_queue *queue) @@ -442,6 +552,9 @@ amdgpu_userq_create(struct drm_file *filp, union
drm_amdgpu_userq*args)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
Quoted 10 lines
queue->queue_type = args->in.ip_type; queue->vm = &fpriv->vm; queue->priority = priority;drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4
-0+ queue->queue_va = args->in.queue_va; + queue->rptr_va = args->in.rptr_va; + queue->wptr_va = args->in.wptr_va; db_info.queue_type = queue->queue_type; db_info.doorbell_handle = queue->doorbell_handle; @@ -472,7 +585,6 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq*args)
Quoted 5 lines
goto unlock; }-
qid = idr_alloc(&uq_mgr->userq_idr, queue, 1,
AMDGPU_MAX_USERQ_COUNT,GFP_KERNEL);Quoted 34 lines
if (qid < 0) { drm_file_err(uq_mgr->file, "Failed to allocate a queueid\n"); diff --git
a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.hb/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h
index704935ca0c36..9543fbab7cc3100644drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +4
-0@@ -52,6 +52,10 @@ struct amdgpu_usermode_queue {enum amdgpu_userq_state state;uint64_t doorbell_handle;uint64_t doorbell_index; +uint64_t queue_va; +uint64_t rptr_va; +uint64_t wptr_va; +uint64_t flags;struct amdgpu_mqd_prop *userq_prop; struct amdgpu_userq_mgr *userq_mgr; @@ -134,4 +138,11 @@ intamdgpu_userq_start_sched_for_enforce_isolation(struct
amdgpu_device*adev,int amdgpu_userq_input_va_validate(struct
amdgpu_vm*vm, u64 addr,u64 expected_size);drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +7
-1+int amdgpu_userq_buffer_va_get (struct amdgpu_vm *vm,u64 addr);bool +amdgpu_userq_buffer_va_mapped (struct amdgpu_vm *vm,u64 addr);bool +amdgpu_userq_buffer_vas_mapped (struct amdgpu_vm *vm, +struct amdgpu_usermode_queue *queue);int +amdgpu_userq_buffer_va_put (struct amdgpu_vm *vm,u64 addr);int +amdgpu_userq_buffer_vas_put (struct amdgpu_vm *vm, +struct amdgpu_usermode_queue *queue);#endif --2.34.1
PATCH v2 6/11 drm/amdgpu: track the userq bo va for its obj management
In reply to Prike LiangThe user queue object destroy requires ensuring its VA has been unmapped prior to the queue being destroyed. Otherwise, the kernel driver should report an invalidated error to the user IOCLT request.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +17 -0
@@ -459,12 +459,24 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id)amdgpu_bo_unpin (queue->db_obj.obj);amdgpu_bo_unreserve (queue->db_obj.obj); } + +/* +* At this point the userq obj va should be unmapped, +* otherwise will return error to user. +*/ +if (amdgpu_userq_buffer_vas_mapped (&fpriv->vm, queue)) { +drm_warn (adev_to_drm (uq_mgr->adev),"the userq obj va shouldn't be mapped during destroying userq\n" ); + r= -EINVAL ; + } + r =amdgpu_userq_unmap_helper (uq_mgr, queue);/*TODO: It requires a reset for unmap error*/ if (r !=AMDGPU_USERQ_STATE_UNMAPPED ) {drm_warn (adev_to_drm (uq_mgr->adev),"trying to destroy a HW mapping userq\n" ); r = -ETIMEDOUT ; } + +amdgpu_userq_buffer_vas_put (&fpriv->vm, queue);amdgpu_userq_cleanup (uq_mgr, queue, queue_id);mutex_unlock (&uq_mgr->userq_mutex);
@@ -585,6 +597,11 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) goto unlock; } +/* refer to the userq objects vm bo*/ +amdgpu_userq_buffer_va_get (queue->vm, args->in.queue_va); +amdgpu_userq_buffer_va_get (queue->vm, args->in.rptr_va); +amdgpu_userq_buffer_va_get (queue->vm, args->in.wptr_va); + qid =idr_alloc (&uq_mgr->userq_idr, queue,1 ,AMDGPU_MAX_USERQ_COUNT ,GFP_KERNEL );if (qid <0 ) {drm_file_err (uq_mgr->file,"Failed to allocate a queue id\n" );
Signature · 1 line
2.34.1
Re: [PATCH v2 06/11] drm/amdgpu: track the userq bo va for its obj management
Quoted 16 lines · Prike Liang
The user queue object destroy requires ensuring its VA has been unmapped prior to the queue being destroyed. Otherwise, the kernel driver should report an invalidated error to the user IOCLT request.
Signed-off-by Prike Liang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-0
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_destroy(struct drm_file *filp, int queue_id) · line 466
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5
-0@@ -459,12 +459,24 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id)amdgpu_bo_unpin (queue->db_obj.obj);amdgpu_bo_unreserve (queue->db_obj.obj); } + +/* +* At this point the userq obj va should be unmapped, +* otherwise will return error to user. +*/
I think ideally the queue would remain mapped until after it's destroyed. I.e., on the userspace side:
destroy queue
unmap VAs
free BOs
Alex
Quoted 31 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +12
-1+if (amdgpu_userq_buffer_vas_mapped (&fpriv->vm, queue)) { +drm_warn (adev_to_drm (uq_mgr->adev),"the userq obj va shouldn't be mapped during destroying userq\n" ); + r= -EINVAL ; + } + r =amdgpu_userq_unmap_helper (uq_mgr, queue);/*TODO: It requires a reset for unmap error*/ if (r !=AMDGPU_USERQ_STATE_UNMAPPED ) {drm_warn (adev_to_drm (uq_mgr->adev),"trying to destroy a HW mapping userq\n" ); r = -ETIMEDOUT ; } + +amdgpu_userq_buffer_vas_put (&fpriv->vm, queue);amdgpu_userq_cleanup (uq_mgr, queue, queue_id);mutex_unlock (&uq_mgr->userq_mutex);@@ -585,6 +597,11 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) goto unlock; } +/* refer to the userq objects vm bo*/ +amdgpu_userq_buffer_va_get (queue->vm, args->in.queue_va); +amdgpu_userq_buffer_va_get (queue->vm, args->in.rptr_va); +amdgpu_userq_buffer_va_get (queue->vm, args->in.wptr_va); + qid =idr_alloc (&uq_mgr->userq_idr, queue,1 ,AMDGPU_MAX_USERQ_COUNT ,GFP_KERNEL );if (qid <0 ) {drm_file_err (uq_mgr->file,"Failed to allocate a queue id\n" ); --2.34.1
RE: [PATCH v2 06/11] drm/amdgpu: track the userq bo va for its obj management
[Public]
Regards,
Prike
Quoted 37 lines
-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: Tuesday, June 17, 2025 11:38 PM
To: Liang, Prike <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>
Subject: Re: [PATCH v2 06/11] drm/amdgpu: track the userq bo va for its obj managementdrivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_destroy(struct drm_file *filp, int
Quoted 17 lines · Prike Liang
The user queue object destroy requires ensuring its VA has been unmapped prior to the queue being destroyed.
Otherwise, the kernel driver should report an invalidated error to the user IOCLT request.Signed-off-by Prike Liang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-0@@ -459,12 +459,24 @@ amdgpu_userq_destroy(struct drm_file *filp, int
queue_id)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_destroy(struct drm_file *filp, int queue_id) · line 466
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5
-0amdgpu_bo_unpin (queue->db_obj.obj);amdgpu_bo_unreserve (queue->db_obj.obj); } + +/* +* At this point the userq obj va should be unmapped, +* otherwise will return error to user. +*/
Quoted 6 lines
I think ideally the queue would remain mapped until after it's destroyed. I.e., on the userspace side:
destroy queue
unmap VAs
free BOs
I checked and traced the amdgpu_vm_bo_unmap() and amdgpu_userq_destroy() call sequence, and that shows the userq VA seems always unmapped before destroying the userq. So now the free sequence like as following:
unmap VAs
destroy queue
free BOs
Quoted 38 lines
Alex
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_destroy(struct drm_file *filp, int · line 468
Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-0+if (amdgpu_userq_buffer_vas_mapped (&fpriv->vm, queue)) { + drm_warn(adev_to_drm(uq_mgr->adev), "the userq obj va shouldn't bemapped during destroying userq\n");
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_destroy(struct drm_file *filp, int queue_id)
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3
-0+ r= -EINVAL ; + } + r =amdgpu_userq_unmap_helper (uq_mgr, queue);/*TODO: It requires a reset for unmap error*/ if (r !=AMDGPU_USERQ_STATE_UNMAPPED ) { drm_warn(adev_to_drm(uq_mgr->adev), "trying to destroy a HWmapping userq\n");
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-0r = -ETIMEDOUT ; } + +amdgpu_userq_buffer_vas_put (&fpriv->vm, queue);amdgpu_userq_cleanup (uq_mgr, queue, queue_id);mutex_unlock (&uq_mgr->userq_mutex);@@ -585,6 +597,11 @@ amdgpu_userq_create(struct drm_file *filp, union
drm_amdgpu_userq*args)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5
-0goto unlock; } +/* refer to the userq objects vm bo*/ +amdgpu_userq_buffer_va_get (queue->vm, args->in.queue_va); +amdgpu_userq_buffer_va_get (queue->vm, args->in.rptr_va); +amdgpu_userq_buffer_va_get (queue->vm, args->in.wptr_va); + qid = idr_alloc(&uq_mgr->userq_idr, queue, 1,
AMDGPU_MAX_USERQ_COUNT,GFP_KERNEL);Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-0if (qid <0 ) { drm_file_err(uq_mgr->file, "Failed to allocate a queueid\n");
--
2.34.1
PATCH v2 7/11 drm/amdgpu: add user queue vm identifier
In reply to Prike LiangAdd a user queue vm identifier for each userqueue
kms opt accessing.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 4 ++++ drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
@@ -483,6 +483,8 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id)pm_runtime_mark_last_busy (adev_to_drm (adev)->dev);pm_runtime_put_autosuspend (adev_to_drm (adev)->dev); + fpriv->vm.is_userq_context = false; +return r; }
@@ -517,6 +519,8 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) (args->in.flags &AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK ) >>AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_SHIFT ; + fpriv->vm.is_userq_context = true; + r =amdgpu_userq_priority_permit (filp, priority);if (r)return r;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -1
@@ -2560,7 +2560,7 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,ttm_lru_bulk_move_init (&vm->lru_bulk_move); vm->is_compute_context = false; - + vm->is_userq_context = false; vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &AMDGPU_VM_USE_CPU_FOR_GFX );
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -1
@@ -422,7 +422,7 @@ struct amdgpu_vm {struct ttm_lru_bulk_move lru_bulk_move;/* Flag to indicate if VM is used for compute */ bool is_compute_context; - +bool is_userq_context;/* Memory partition number, -1 means any partition */ int8_t mem_id;
Signature · 1 line
2.34.1
PATCH v2 8/11 drm/amdgpu: add userq va unmap validated helper
In reply to Prike LiangThis helper can validate the userq whether can be
unmapped prior to the userq VA GEM unmap.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 55 +++++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 3 ++ 2 files changed, 58 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +55 -0
@@ -1135,3 +1135,58 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,mutex_unlock (&adev->userq_mutex);return ret; } + +static bool amdgpu_userq_gem_va_unmap_queue_retrieve (struct amdgpu_usermode_queue *queue, +uint64_t va) +{ +/*TODO: validate the user vas of EOP, shadow buffer, CSA buffer*/ +if (amdgpu_userq_va_align (queue->queue_va) != va && +amdgpu_userq_va_align (queue->wptr_va) != va && +amdgpu_userq_va_align (queue->rptr_va) != va) +return false; + +return true; +} + +int amdgpu_userq_gem_va_unmap_validate (struct amdgpu_vm *vm, +uint64_t va) +{ +struct amdgpu_fpriv *fpriv =vm_to_fpriv (vm); +struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr; + +if (vm->is_userq_context && &fpriv->vm == vm) { +struct amdgpu_usermode_queue *queue; +int queue_id, r =0 ; + +if (mutex_trylock (&uq_mgr->userq_mutex)) { +/* If here the userq bo is busy and needs to deactivate and prevent reusing it.*/ +idr_for_each_entry (&uq_mgr->userq_idr, queue, queue_id) { +struct dma_fence *f = queue->last_fence; + + +if (!amdgpu_userq_gem_va_unmap_queue_retrieve (queue, va)) { +dev_dbg (uq_mgr->adev->dev,"queue(id:%d) not belond to vm:%p\n" , + queue_id,vm); +continue ; + } + +if (f && !dma_fence_is_signaled (f)) { + +dev_warn (uq_mgr->adev->dev,"try to unmap the busy queue(id:%d):%p under vm:%p\n" , + queue_id, queue, vm); +/* Need to set a resonable state for avoiding reusing this queue*/ + queue->state =AMDGPU_USERQ_STATE_HUNG ; + r++; + } + } +mutex_unlock (&uq_mgr->userq_mutex); +return r; + }else { +/* do we need a try lock again before return*/ +return -EBUSY ; + } + + } + +return 0 ; +}
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +3 -0
@@ -31,6 +31,7 @@#define to_ev_fence (f) container_of(f, struct amdgpu_eviction_fence, base)#define uq_mgr_to_fpriv (u) container_of(u, struct amdgpu_fpriv, userq_mgr)#define work_to_uq_mgr (w, name) container_of(w, struct amdgpu_userq_mgr, name) +#define vm_to_fpriv (v) container_of(v, struct amdgpu_fpriv, vm)enum amdgpu_userq_state {AMDGPU_USERQ_STATE_UNMAPPED =0 ,
@@ -145,4 +146,6 @@ bool amdgpu_userq_buffer_vas_mapped(struct amdgpu_vm *vm,int amdgpu_userq_buffer_va_put (struct amdgpu_vm *vm,u64 addr);int amdgpu_userq_buffer_vas_put (struct amdgpu_vm *vm,struct amdgpu_usermode_queue *queue); +int amdgpu_userq_gem_va_unmap_validate (struct amdgpu_vm *vm, +uint64_t va);#endif
Signature · 1 line
2.34.1
PATCH v2 9/11 drm/amdgpu: validate userq activity status for GEM_VA unmap
In reply to Prike LiangThe userq VA unmap requires validating queue status before unamapping it, if user tries to unmap a busy userq by GEM VA IOCTL then the driver should report an error for this illegal usage.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 16 +++++++++++++--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 8 ++++++++ 2 files changed, 21 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +13 -3
@@ -225,7 +225,7 @@ amdgpu_userq_map_helper(struct amdgpu_userq_mgr *uq_mgr,return r; } -static void +static int amdgpu_userq_wait_for_last_fence (struct amdgpu_userq_mgr *uq_mgr,struct amdgpu_usermode_queue *queue) {
@@ -234,10 +234,14 @@ amdgpu_userq_wait_for_last_fence(struct amdgpu_userq_mgr *uq_mgr,if (f && !dma_fence_is_signaled (f)) { ret =dma_fence_wait_timeout (f, true,msecs_to_jiffies (100 )); -if (ret <=0 ) +if (ret <=0 ) {drm_file_err (uq_mgr->file,"Timed out waiting for fence=%llu:%llu\n" , f->context, f->seqno); +return -ETIMEDOUT ; + } } + +return 0 ; } static void
@@ -453,7 +457,13 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id)mutex_unlock (&uq_mgr->userq_mutex);return -EINVAL ; } -amdgpu_userq_wait_for_last_fence (uq_mgr, queue); + +if (amdgpu_userq_wait_for_last_fence (uq_mgr, queue)) { +drm_warn (adev_to_drm (uq_mgr->adev),"Don't destroy a busy userq\n" ); +mutex_unlock (&uq_mgr->userq_mutex); +return -EINVAL ; + } + r =amdgpu_bo_reserve (queue->db_obj.obj, true);if (!r) {amdgpu_bo_unpin (queue->db_obj.obj);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +8 -0
@@ -1929,6 +1929,7 @@ int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,struct amdgpu_bo_va_mapping *mapping;struct amdgpu_vm *vm = bo_va->base.vm;bool valid = true; +int r; saddr /=AMDGPU_GPU_PAGE_SIZE ;
@@ -1949,6 +1950,13 @@ int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,return -ENOENT ; } +/* It's unlike to happen that the mapping userq hasn't been idled +* during user unmap vm except the vm unmaped forcely from user space. +*/ + r =amdgpu_userq_gem_va_unmap_validate (vm, saddr); +if (unlikely (r && r != -EBUSY )) +dev_warn (adev->dev,"Here should be an improper unmap request from user space\n" ); +list_del (&mapping->list);amdgpu_vm_it_remove (mapping, &vm->va); mapping->bo_va =NULL ;
Signature · 1 line
2.34.1
RE: [PATCH v2 09/11] drm/amdgpu: validate userq activity status for GEM_VA unmap
[Public]
The patch#7 ~patch9 are drafting for validating the unmapped metadata whether it is idled. If the user force unmaps a busy userq, then a warning message will be reported here.
Hi, @Deucher, Alexander Could you please help review the patches 7-9 to see whether they can fulfill the userq VA unmap validation design?
Regards,
Prike
Quoted 99 lines
-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Prike Liang
Sent: Tuesday, June 17, 2025 4:02 PM
To: [email protected]
Cc: Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>; Liang, Prike <[email protected]>
Subject: [PATCH v2 09/11] drm/amdgpu: validate userq activity status forGEM_VAunmapThe userq VA unmap requires validating queue status before unamapping it, if user tries to unmap a busy userq by GEM VA IOCTL then the driver should report an error for this illegal usage.
Signed-off-by Prike Liang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 16 +++++++++++++--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 8 ++++++++ 2 files changed, 21 insertions(+), 3 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-1@@ -225,7 +225,7 @@ amdgpu_userq_map_helper(struct amdgpu_userq_mgr *uq_mgr, return r; } -static void +static int amdgpu_userq_wait_for_last_fence (struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_queue *queue) { @@ -234,10 +234,14 @@ amdgpu_userq_wait_for_last_fence(struct
amdgpu_userq_mgr*uq_mgr,if (f && !dma_fence_is_signaled(f)) { ret = dma_fence_wait_timeout(f, true, msecs_to_jiffies(100));drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-1-if (ret <=0 ) +if (ret <=0 ) { drm_file_err(uq_mgr->file, "Timed out waiting forfence=%llu:%llu\n",
f->context, f->seqno);drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +11
-1+return -ETIMEDOUT ; + } } + +return 0 ; } static void@@ -453,7 +457,13 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id)mutex_unlock (&uq_mgr->userq_mutex);return -EINVAL ; } -amdgpu_userq_wait_for_last_fence (uq_mgr, queue); + +if (amdgpu_userq_wait_for_last_fence (uq_mgr, queue)) { +drm_warn (adev_to_drm (uq_mgr->adev),"Don't destroy a busy userq\n"); +mutex_unlock (&uq_mgr->userq_mutex); +return -EINVAL ; + } + r =amdgpu_bo_reserve (queue->db_obj.obj, true);if (!r) {amdgpu_bo_unpin(queue->db_obj.obj);drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +7
-0@@ -1929,6 +1929,7 @@ int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,struct amdgpu_bo_va_mapping *mapping;struct amdgpu_vm *vm = bo_va->base.vm;bool valid = true; +int r; saddr /=AMDGPU_GPU_PAGE_SIZE ;@@ -1949,6 +1950,13 @@ int amdgpu_vm_bo_unmap(struct amdgpu_device *adev, return -ENOENT ; } +/* It's unlike to happen that the mapping userq hasn't been idled +* during user unmap vm except the vm unmaped forcely from user space. +*/ + r =amdgpu_userq_gem_va_unmap_validate (vm, saddr); +if (unlikely (r && r != -EBUSY )) + dev_warn(adev->dev, "Here should be an improper unmap requestfrom
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2
-1+user space\n"); +list_del (&mapping->list);amdgpu_vm_it_remove (mapping, &vm->va); mapping->bo_va =NULL ; --2.34.1
PATCH v2 10/11 drm/amdgpu: only bound the eviction fence to userq bo
In reply to Prike LiangIt doesn't need to attach the invalid eviction fence to non-userq's BOs.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +9 -7
@@ -294,12 +294,14 @@ static int amdgpu_gem_object_open(struct drm_gem_object *obj,else ++bo_va->ref_count; -/* attach gfx eviction fence */ - r =amdgpu_eviction_fence_attach (&fpriv->evf_mgr, abo); -if (r) { -DRM_DEBUG_DRIVER ("Failed to attach eviction fence to BO\n" ); -amdgpu_bo_unreserve (abo); -return r; +/* attach gfx eviction fence when userq created*/ +if (vm->is_userq_context) { + r =amdgpu_eviction_fence_attach (&fpriv->evf_mgr, abo); +if (r) { +DRM_DEBUG_DRIVER ("Failed to attach eviction fence to BO\n" ); +amdgpu_bo_unreserve (abo); +return r; + } }amdgpu_bo_unreserve (abo);
@@ -365,7 +367,7 @@ static void amdgpu_gem_object_close(struct drm_gem_object *obj, goto out_unlock; } -if (!amdgpu_vm_is_bo_always_valid (vm, bo)) +if (!amdgpu_vm_is_bo_always_valid (vm, bo) && vm->is_compute_context)amdgpu_eviction_fence_detach (&fpriv->evf_mgr, bo); bo_va =amdgpu_vm_bo_find (vm, bo);
Signature · 1 line
2.34.1
PATCH v2 11/11 drm/amdgpu: clean up the amdgpu_userq_active()
In reply to Prike LiangThis is no invocation for amdgpu_userq_active().
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 16 ---------------- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 2 -- 2 files changed, 18 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -16
@@ -258,22 +258,6 @@ amdgpu_userq_cleanup(struct amdgpu_userq_mgr *uq_mgr,kfree (queue); } -int -amdgpu_userq_active (struct amdgpu_userq_mgr *uq_mgr) -{ -struct amdgpu_usermode_queue *queue; -int queue_id; -int ret =0 ; - -mutex_lock (&uq_mgr->userq_mutex); -/* Resume all the queues for this process */ -idr_for_each_entry (&uq_mgr->userq_idr, queue, queue_id) - ret += queue->state ==AMDGPU_USERQ_STATE_MAPPED ; - -mutex_unlock (&uq_mgr->userq_mutex); -return ret; -} -static struct amdgpu_usermode_queue *amdgpu_userq_find (struct amdgpu_userq_mgr *uq_mgr,int qid) {
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +0 -2
@@ -118,8 +118,6 @@ void amdgpu_userq_destroy_object(struct amdgpu_userq_mgr *uq_mgr,void amdgpu_userq_evict (struct amdgpu_userq_mgr *uq_mgr,struct amdgpu_eviction_fence *ev_fence); -int amdgpu_userq_active (struct amdgpu_userq_mgr *uq_mgr); -void amdgpu_userq_ensure_ev_fence (struct amdgpu_userq_mgr *userq_mgr,struct amdgpu_eviction_fence_mgr *evf_mgr);
Signature · 1 line
2.34.1
Re: [PATCH v2 11/11] drm/amdgpu: clean up the amdgpu_userq_active()
Quoted 4 lines · Prike Liang
This is no invocation for
amdgpu_userq_active().Signed-off-by Prike Liang <[email protected]>
Quoted 48 lines
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 16 ---------------- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 2 -- 2 files changed, 18 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-16@@ -258,22 +258,6 @@ amdgpu_userq_cleanup(struct amdgpu_userq_mgr *uq_mgr,kfree (queue); } -int -amdgpu_userq_active (struct amdgpu_userq_mgr *uq_mgr) -{ -struct amdgpu_usermode_queue *queue; -int queue_id; -int ret =0 ; - -mutex_lock (&uq_mgr->userq_mutex); -/* Resume all the queues for this process */ -idr_for_each_entry (&uq_mgr->userq_idr, queue, queue_id) - ret += queue->state ==AMDGPU_USERQ_STATE_MAPPED ; - -mutex_unlock (&uq_mgr->userq_mutex); -return ret; -} -static struct amdgpu_usermode_queue *amdgpu_userq_find (struct amdgpu_userq_mgr *uq_mgr,int qid) {drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +0
-3@@ -118,8 +118,6 @@ void amdgpu_userq_destroy_object(struct amdgpu_userq_mgr *uq_mgr,void amdgpu_userq_evict (struct amdgpu_userq_mgr *uq_mgr,struct amdgpu_eviction_fence *ev_fence); -int amdgpu_userq_active (struct amdgpu_userq_mgr *uq_mgr); -void amdgpu_userq_ensure_ev_fence (struct amdgpu_userq_mgr *userq_mgr,struct amdgpu_eviction_fence_mgr *evf_mgr); --2.34.1
Quoted 74 lines · Prike Liang
This will help on validating the userq input args, and rejecting for the invalidate userq request at the IOCTLs first place.
Signed-off-by Prike Liang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 80 +++++++++++++++------- drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 14 ++-- 2 files changed, 62 insertions(+), 32 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +12
-25@@ -359,27 +359,10 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) (args->in.flags &AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK ) >>AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_SHIFT ; -/* Usermode queues are only supported for GFX IP as of now */ -if (args->in.ip_type !=AMDGPU_HW_IP_GFX && - args->in.ip_type !=AMDGPU_HW_IP_DMA && - args->in.ip_type !=AMDGPU_HW_IP_COMPUTE ) { -drm_file_err (uq_mgr->file,"Usermode queue doesn't support IP type %u\n" , - args->in.ip_type); -return -EINVAL ; - } - r =amdgpu_userq_priority_permit (filp, priority);if (r)return r; -if ((args->in.flags &AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE ) && - (args->in.ip_type !=AMDGPU_HW_IP_GFX ) && - (args->in.ip_type !=AMDGPU_HW_IP_COMPUTE ) && - !amdgpu_is_tmz (adev)) { -drm_file_err (uq_mgr->file,"Secure only supported on GFX/Compute queues\n" ); -return -EINVAL ; - } - r =pm_runtime_get_sync (adev_to_drm (adev)->dev);if (r <0 ) {drm_file_err (uq_mgr->file,"pm_runtime_get_sync() failed for userqueue create\n" );@@ -485,22 +468,44 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)return r; } -int amdgpu_userq_ioctl (struct drm_device *dev,void *data, -struct drm_file *filp) +static int amdgpu_userq_input_args_validate (struct drm_device *dev, +union drm_amdgpu_userq *args, +struct drm_file *filp) { -union drm_amdgpu_userq *args = data; -int r; +struct amdgpu_device *adev =drm_to_adev (dev);switch (args->in.op) {case AMDGPU_USERQ_OP_CREATE :if (args->in.flags & ~(AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK |AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE ))return -EINVAL ; - r =amdgpu_userq_create (filp, args); -if (r) -drm_file_err (filp,"Failed to create usermode queue\n" ); -break ; +/* Usermode queues are only supported for GFX IP as of now */ +if (args->in.ip_type !=AMDGPU_HW_IP_GFX && + args->in.ip_type !=AMDGPU_HW_IP_DMA && + args->in.ip_type !=AMDGPU_HW_IP_COMPUTE ) { +drm_file_err (filp,"Usermode queue doesn't support IP type %u\n" , + args->in.ip_type); +return -EINVAL ; + }
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) · line 499
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +9
-0+ +if ((args->in.flags &AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE ) && + (args->in.ip_type !=AMDGPU_HW_IP_GFX ) && + (args->in.ip_type !=AMDGPU_HW_IP_COMPUTE ) && + !amdgpu_is_tmz (adev)) { +drm_file_err (filp,"Secure only supported on GFX/Compute queues\n" ); +return -EINVAL ; + } + if (args->in.queue_va == AMDGPU_BO_INVALID_OFFSET ||
args->in.queue_va of 0 is also invalid.
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) · line 504
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5
-0+ args->in.queue_size == 0) { +drm_file_err (filp,"invalidate userq queue va or size\n" ); +return -EINVAL ; + } +if (!args->in.wptr_va || !args->in.rptr_va) {
AMDGPU_BO_INVALID_OFFSET is also invalid for these pointers.
Quoted 44 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +29
-0+drm_file_err (filp,"invalidate userq queue rptr or wptr\n" ); +return -EINVAL ; + } +break ;case AMDGPU_USERQ_OP_FREE : if (args->in.ip_type || args->in.doorbell_handle ||@@ -514,6 +519,31 @@ int amdgpu_userq_ioctl(struct drm_device *dev, void *data, args->in.mqd || args->in.mqd_size) return -EINVAL; +break ; +default : +return -EINVAL ; + } + +return 0 ; +} + +int amdgpu_userq_ioctl (struct drm_device *dev,void *data, +struct drm_file *filp) +{ +union drm_amdgpu_userq *args = data; +int r; + +if (amdgpu_userq_input_args_validate (dev, args, filp) <0 ) +return -EINVAL ; + +switch (args->in.op) { +case AMDGPU_USERQ_OP_CREATE : + r =amdgpu_userq_create (filp, args); +if (r) +drm_file_err (filp,"Failed to create usermode queue\n" ); +break ; + +case AMDGPU_USERQ_OP_FREE : r =amdgpu_userq_destroy (filp, args->in.queue_id);if (r)drm_file_err (filp,"Failed to destroy usermode queue\n" );drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0
-0
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr, · line 216
Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +6
-0@@ -208,6 +208,13 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,struct amdgpu_mqd_prop *userq_props;int r; +/* validate the user va at early time to avoid the time cost on building memory resource*/ +if (!mqd_user->wptr_va || !mqd_user->rptr_va || + !mqd_user->queue_va || mqd_user->queue_size ==0 ) { +DRM_ERROR ("Invalid MQD parameters for userqueue\n" ); +return -EINVAL ; + }
I think we can drop this as you already validate these in amdgpu_userq_input_args_validate().
Alex
Quoted 21 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +1
-8+/* Structure to initialize MQD for userqueue using generic MQD init function */ userq_props =kzalloc (sizeof (struct amdgpu_mqd_prop ),GFP_KERNEL );if (!userq_props) {@@ -215,13 +222,6 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,return -ENOMEM ; } -if (!mqd_user->wptr_va || !mqd_user->rptr_va || - !mqd_user->queue_va || mqd_user->queue_size ==0 ) { -DRM_ERROR ("Invalid MQD parameters for userqueue\n" ); - r = -EINVAL ; - goto free_props; - } - r =amdgpu_userq_create_object (uq_mgr, &queue->mqd, mqd_hw_default->mqd_size);if (r) {DRM_ERROR ("Failed to create MQD object for userqueue\n" ); --2.34.1
[Public]
Regards,
Prike
Quoted 99 lines
-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: Tuesday, June 17, 2025 11:06 PM
To: Liang, Prike <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>
Subject: Re: [PATCH v2 01/11] drm/amdgpu: validate userq input argsdrivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union
Quoted 17 lines · Prike Liang
This will help on validating the userq input args, and rejecting for the invalidate userq request at the IOCTLs first place.
Signed-off-by Prike Liang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 80 +++++++++++++++------- drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 14 ++-- 2 files changed, 62 insertions(+), 32 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-0@@ -359,27 +359,10 @@ amdgpu_userq_create(struct drm_file *filp, union
drm_amdgpu_userq*args)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union · line 359
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-0(args->in.flags &
AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK) >>drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-5AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_SHIFT ; -/* Usermode queues are only supported for GFX IP as of now */ -if (args->in.ip_type !=AMDGPU_HW_IP_GFX && - args->in.ip_type !=AMDGPU_HW_IP_DMA && - args->in.ip_type !=AMDGPU_HW_IP_COMPUTE ) { - drm_file_err(uq_mgr->file, "Usermode queue doesn't support IPtype %u\n",
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-5- args->in.ip_type); -return -EINVAL ; - } - r =amdgpu_userq_priority_permit (filp, priority);if (r)return r; - if ((args->in.flags &
AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE) &&drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-4- (args->in.ip_type != AMDGPU_HW_IP_GFX) && - (args->in.ip_type != AMDGPU_HW_IP_COMPUTE) && - !amdgpu_is_tmz(adev)) { - drm_file_err(uq_mgr->file, "Secure only supported on GFX/Computequeues\n");
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-3-return -EINVAL ; - } - r =pm_runtime_get_sync (adev_to_drm (adev)->dev);if (r <0 ) { drm_file_err(uq_mgr->file, "pm_runtime_get_sync()failed for userqueue create\n"); @@ -485,22 +468,44 @@
amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)Quoted 6 lines
return r;}
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-2-int amdgpu_userq_ioctl (struct drm_device *dev,void *data, -struct drm_file *filp) +static int amdgpu_userq_input_args_validate(struct drm_device *dev,drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3
-2+union drm_amdgpu_userq *args, + struct drm_file *filp) { -union drm_amdgpu_userq *args = data; -int r; + struct amdgpu_device *adev = drm_to_adev(dev);switch (args->in.op) {case AMDGPU_USERQ_OP_CREATE : if (args->in.flags &~(AMDGPU_USERQ_CREATE_FLAGS_QUEUE_PRIORITY_MASK |
Quoted 1 line
AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE))Quoted 5 lines
return -EINVAL;drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-4- r =amdgpu_userq_create (filp, args); -if (r) -drm_file_err (filp,"Failed to create usermode queue\n" ); -break ;drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10
-0+/* Usermode queues are only supported for GFX IP as of now */ +if (args->in.ip_type !=AMDGPU_HW_IP_GFX && + args->in.ip_type !=AMDGPU_HW_IP_DMA && + args->in.ip_type !=AMDGPU_HW_IP_COMPUTE ) { +drm_file_err (filp,"Usermode queue doesn't support IP type %u\n" , + args->in.ip_type); +return -EINVAL ; + } + + if ((args->in.flags &
AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE) &&drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4
-0+ (args->in.ip_type != AMDGPU_HW_IP_GFX) && + (args->in.ip_type != AMDGPU_HW_IP_COMPUTE) && + !amdgpu_is_tmz(adev)) { + drm_file_err(filp, "Secure only supported on GFX/Compute
Quoted 7 lines
queues\n");
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) · line 499
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3
-0+return -EINVAL ; + } + if (args->in.queue_va == AMDGPU_BO_INVALID_OFFSET ||
args->in.queue_vaof 0 is also invalid.
Note.
Quoted 58 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) · line 504
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5
-0+ args->in.queue_size == 0) { +drm_file_err (filp,"invalidate userq queue va or size\n" ); +return -EINVAL ; + } +if (!args->in.wptr_va || !args->in.rptr_va) {
AMDGPU_BO_INVALID_OFFSETis also invalid for these pointers.drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_mqd_create(struct
Quoted 44 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4
-0+drm_file_err (filp,"invalidate userq queue rptr or wptr\n" ); +return -EINVAL ; + } +break ;case AMDGPU_USERQ_OP_FREE : if (args->in.ip_type || args->in.doorbell_handle || @@ -514,6 +519,31 @@int amdgpu_userq_ioctl(struct
drm_device*dev, void *data,args->in.mqd || args->in.mqd_size) return -EINVAL;drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +24
-0+break ; +default : +return -EINVAL ; + } + +return 0 ; +} + +int amdgpu_userq_ioctl (struct drm_device *dev,void *data, +struct drm_file *filp) { +union drm_amdgpu_userq *args = data; +int r; + +if (amdgpu_userq_input_args_validate (dev, args, filp) <0 ) +return -EINVAL ; + +switch (args->in.op) { +case AMDGPU_USERQ_OP_CREATE : + r =amdgpu_userq_create (filp, args); +if (r) +drm_file_err (filp,"Failed to create usermode queue\n" ); +break ; + +case AMDGPU_USERQ_OP_FREE : r =amdgpu_userq_destroy (filp, args->in.queue_id);if (r) drm_file_err(filp, "Failed to destroy usermodequeue\n"); diff --git
a/drivers/gpu/drm/amd/amdgpu/mes_userqueue.cb/drivers/gpu/drm/amd/amdgpu/mes_userqueue.c
indexd6f50b13e2ba..39decc0b00f7100644drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0
-0@@ -208,6 +208,13 @@ static int mes_userq_mqd_create(struct
amdgpu_userq_mgr*uq_mgr,drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +1
-0struct amdgpu_mqd_prop *userq_props;int r; +/* validate the user va at early time to avoid the time cost on building memory resource*/
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr, · line 216
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +5
-0+if (!mqd_user->wptr_va || !mqd_user->rptr_va || + !mqd_user->queue_va || mqd_user->queue_size ==0 ) { +DRM_ERROR ("Invalid MQD parameters for userqueue\n" ); +return -EINVAL ; + }I think we can drop this as you already validate these in
amdgpu_userq_input_args_validate().
OK, will drop this. I was though you prefer to validate this at this placement base your v1 comment.
Quoted 26 lines
Alex
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_mqd_create(struct
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +1
-0+/* Structure to initialize MQD for userqueue using generic MQD init function */ userq_props =kzalloc (sizeof (struct amdgpu_mqd_prop ),GFP_KERNEL );if (!userq_props) {@@ -215,13 +222,6 @@ static int mes_userq_mqd_create(struct
amdgpu_userq_mgr*uq_mgr,Quoted 18 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0
-7return -ENOMEM ; } -if (!mqd_user->wptr_va || !mqd_user->rptr_va || - !mqd_user->queue_va || mqd_user->queue_size ==0 ) { -DRM_ERROR ("Invalid MQD parameters for userqueue\n" ); - r = -EINVAL ; - goto free_props; - } - r =amdgpu_userq_create_object (uq_mgr, &queue->mqd, mqd_hw_default- mqd_size);if (r) {DRM_ERROR("Failed to create MQD object for
userqueue\n");
--
2.34.1