drm/amdgpu: validate userq input args
Subsystems (2)
PATCH v5 1/9 drm/amdgpu: validate userq input args
This will help on validating the userq input args, and rejecting for the invalid userq request at the IOCTLs first place.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 81 +++++++++++++++------- drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 7 -- 2 files changed, 56 insertions(+), 32 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +56 -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,45 @@ 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_va ==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) { +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 +520,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 -7
@@ -215,13 +215,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 v5 2/9 drm/amdgpu: validate userq hw unmap status for destroying userq
Before destroying the userq buffer object, it requires validating the userq HW unmap status and ensuring the userq is unmapped from hardware. If the user HW unmap failed, then it needs to reset the queue for reusing.
--- 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 userq hw unmap error*/ +if (unlikely (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
PATCH v5 3/9 drm/amdgpu: rework the userq doorbell object destroy
In reply to Prike LiangThis patch aims to unify and destroy the userq doorbell objects at mes_userq_mqd_destroy(), and this change will also help with unpinning and destroying the userq doorbell objects for amdgpu_userq_mgr_fini() during releasing the drm files.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 1 - drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 4 ++++ 2 files changed, 4 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 userq hw unmap error*/ if (unlikely (r !=AMDGPU_USERQ_STATE_UNMAPPED )) {
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +4 -0
@@ -336,6 +336,10 @@ mes_userq_mqd_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_queue *queue) {amdgpu_userq_destroy_object (uq_mgr, &queue->fw_obj); +/*Validate the doorbell obj whether has unpinned by amdgpu_userq_destroy()*/ +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
Christian König
maintainerRe: [PATCH v5 3/9] drm/amdgpu: rework the userq doorbell object destroy
Quoted 29 lines · Prike Liang
This patch aims to unify and destroy the userq doorbell objects at
mes_userq_mqd_destroy(), and this change will also help with unpinning and destroying the userq doorbell objects foramdgpu_userq_mgr_fini()during releasing the drm files.Signed-off-by Prike Liang <[email protected]>Reviewed-by Alex Deucher <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 1 - drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 4 ++++ 2 files changed, 4 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 userq hw unmap error*/ if (unlikely (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 340
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +2
-0@@ -336,6 +336,10 @@ mes_userq_mqd_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_queue *queue) {amdgpu_userq_destroy_object (uq_mgr, &queue->fw_obj); +/*Validate the doorbell obj whether has unpinned by amdgpu_userq_destroy()*/ +if (queue->db_obj.obj->tbo.pin_count)
Clear NAK to touching the pin_count here. That is clearly broken!
Regards,
Christian.
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +2
-0+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); }
RE: [PATCH v5 3/9] drm/amdgpu: rework the userq doorbell object destroy
[Public]
Quoted 42 lines
-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Monday, July 7, 2025 5:28 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v5 3/9] drm/amdgpu: rework the userq doorbell object destroydrivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_mqd_destroy(struct amdgpu_userq_mgr
Quoted 31 lines · Prike Liang
This patch aims to unify and destroy the userq doorbell objects at
mes_userq_mqd_destroy(), and this change will also help with unpinning and destroying the userq doorbell objects foramdgpu_userq_mgr_fini()during releasing the drm files.Signed-off-by Prike Liang <[email protected]>Reviewed-by Alex Deucher <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 1 - drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 4 ++++ 2 files changed, 4 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 userq hw unmap error*/ if (unlikely(r != AMDGPU_USERQ_STATE_UNMAPPED)) { diff --git
a/drivers/gpu/drm/amd/amdgpu/mes_userqueue.cb/drivers/gpu/drm/amd/amdgpu/mes_userqueue.c
index1457fb49a794..dbacdfcb6f7b100644drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0
-0@@ -336,6 +336,10 @@ mes_userq_mqd_destroy(struct amdgpu_userq_mgr
*uq_mgr,
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_mqd_destroy(struct amdgpu_userq_mgr · line 338
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); +/*Validate the doorbell obj whether has unpinned by amdgpu_userq_destroy()*/
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_mqd_destroy(struct amdgpu_userq_mgr *uq_mgr, · line 340
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +1
-0+if (queue->db_obj.obj->tbo.pin_count)Clear NAK to touching the
pin_counthere. That is clearly broken!
Thank you. I will ensure the doorbell object is locked before unpinning the BOs and then consolidate and relocate the userq doorbell unpinning code to mes_userq_mqd_destroy().
Quoted 7 lines
Regards,
Christian.Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +2
-0+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); }
Christian König
maintainerRe: [PATCH v5 3/9] drm/amdgpu: rework the userq doorbell object destroy
Quoted 44 lines · Prike
[Public]
Quoted 42 lines
-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Monday, July 7, 2025 5:28 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v5 3/9] drm/amdgpu: rework the userq doorbell object destroydrivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_mqd_destroy(struct amdgpu_userq_mgr
Quoted 31 lines · Prike Liang
This patch aims to unify and destroy the userq doorbell objects at
mes_userq_mqd_destroy(), and this change will also help with unpinning and destroying the userq doorbell objects foramdgpu_userq_mgr_fini()during releasing the drm files.Signed-off-by Prike Liang <[email protected]>Reviewed-by Alex Deucher <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 1 - drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 4 ++++ 2 files changed, 4 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 userq hw unmap error*/ if (unlikely(r != AMDGPU_USERQ_STATE_UNMAPPED)) { diff --git
a/drivers/gpu/drm/amd/amdgpu/mes_userqueue.cb/drivers/gpu/drm/amd/amdgpu/mes_userqueue.c
index1457fb49a794..dbacdfcb6f7b100644drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0
-0@@ -336,6 +336,10 @@ mes_userq_mqd_destroy(struct amdgpu_userq_mgr
*uq_mgr,
Quoted 8 lines
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_mqd_destroy(struct amdgpu_userq_mgr · line 338
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); +/*Validate the doorbell obj whether has unpinned by amdgpu_userq_destroy()*/
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c mes_userq_mqd_destroy(struct amdgpu_userq_mgr *uq_mgr, · line 340
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +1
-0+if (queue->db_obj.obj->tbo.pin_count)Clear NAK to touching the
pin_counthere. That is clearly broken!Thank you. I will ensure the doorbell object is locked before unpinning the BOs and then consolidate and relocate the userq doorbell unpinning code to
mes_userq_mqd_destroy().
Thanks! And yes, that sounds like it should work.
Regards,
Christian.
Quoted 9 lines
Quoted 8 lines
Regards,
Christian.Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +2
-0+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); }
PATCH v5 4/9 drm/amdgpu: validate userq buffer virtual address and size
In reply to Prike LiangIt needs to validate the userq object virtual address to determin whether it is residented in a valid vm mapping.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 38 ++++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 2 ++ drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 25 ++++++++++++++ 3 files changed, 65 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +38 -0
@@ -44,6 +44,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 = (addr &AMDGPU_GMC_HOLE_MASK ) >>AMDGPU_GPU_PAGE_SHIFT ; + 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 resident 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 +421,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) {
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 +25 -0
@@ -206,6 +206,7 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr,struct amdgpu_mqd *mqd_hw_default = &adev->mqds[queue->queue_type];struct drm_amdgpu_userq_in *mqd_user = args_in;struct amdgpu_mqd_prop *userq_props; +struct amdgpu_gfx_shadow_info shadow_info;int r;/* Structure to initialize MQD for userqueue using generic MQD init function */
@@ -231,6 +232,8 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr, userq_props->doorbell_index = queue->doorbell_index; userq_props->fence_address = queue->fence_drv->gpu_addr; +if (adev->gfx.funcs->get_gfx_shadow_info) + adev->gfx.funcs->get_gfx_shadow_info (adev, &shadow_info, true);if (queue->queue_type ==AMDGPU_HW_IP_COMPUTE ) {struct drm_amdgpu_userq_mqd_compute_gfx11 *compute_mqd;
@@ -247,6 +250,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 ;
@@ -274,6 +284,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, + shadow_info.shadow_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;
@@ -291,6 +309,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, + shadow_info.csa_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
PATCH v5 5/9 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 | 172 ++++++++++++++++++++- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 14 ++ drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 4 + 3 files changed, 189 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +171 -1
@@ -74,6 +74,174 @@ 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 = (addr &AMDGPU_GMC_HOLE_MASK ) >>AMDGPU_GPU_PAGE_SHIFT ; + 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 +* shadow bo +* csa 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 = (addr &AMDGPU_GMC_HOLE_MASK ) >>AMDGPU_GPU_PAGE_SHIFT ; + +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) +{ + +switch (queue->queue_type) { +case AMDGPU_HW_IP_GFX : +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) || +amdgpu_userq_buffer_va_mapped (vm, queue->shadow_va) || +amdgpu_userq_buffer_va_mapped (vm, queue->csa_va)) +return true; +break ; +case AMDGPU_HW_IP_COMPUTE : +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) || +amdgpu_userq_buffer_va_mapped (vm, queue->eop_va)) +return true; +break ; +case AMDGPU_HW_IP_DMA : +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) || +amdgpu_userq_buffer_va_mapped (vm, queue->csa_va)) +return true; +break ; +default : +break ; + } + +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 = (addr &AMDGPU_GMC_HOLE_MASK ) >>AMDGPU_GPU_PAGE_SHIFT ; + 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; +/* +* TODO: It requires figuring out the root cause of userq va mapping +* reference imbalance issue. +*/ +/*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 ; +} + +static void amdgpu_userq_buffer_vas_get (struct amdgpu_vm *vm, +struct amdgpu_usermode_queue *queue) +{ + + +amdgpu_userq_buffer_va_get (vm, queue->queue_va); +amdgpu_userq_buffer_va_get (vm, queue->rptr_va); +amdgpu_userq_buffer_va_get (vm, queue->wptr_va); + +switch (queue->queue_type) { +case AMDGPU_HW_IP_GFX : +amdgpu_userq_buffer_va_get (vm, queue->shadow_va); +amdgpu_userq_buffer_va_get (vm, queue->csa_va); +break ; +case AMDGPU_HW_IP_COMPUTE : +amdgpu_userq_buffer_va_get (vm, queue->eop_va); +break ; +case AMDGPU_HW_IP_DMA : +amdgpu_userq_buffer_va_get (vm, queue->csa_va); +break ; +default : +break ; + } +} + +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); + +switch (queue->queue_type) { +case AMDGPU_HW_IP_GFX : +amdgpu_userq_buffer_va_put (vm, queue->shadow_va); +amdgpu_userq_buffer_va_put (vm, queue->csa_va); +break ; +case AMDGPU_HW_IP_COMPUTE : +amdgpu_userq_buffer_va_put (vm, queue->eop_va); +break ; +case AMDGPU_HW_IP_DMA : +amdgpu_userq_buffer_va_put (vm, queue->csa_va); +break ; +default : +break ; + } +return 0 ; +} +static int amdgpu_userq_unmap_helper (struct amdgpu_userq_mgr *uq_mgr,struct amdgpu_usermode_queue *queue)
@@ -440,6 +608,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;
@@ -470,7 +641,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 +14 -0
@@ -52,6 +52,13 @@ 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 eop_va; +uint64_t shadow_va; +uint64_t csa_va; +uint64_t flags;struct amdgpu_mqd_prop *userq_prop;struct amdgpu_userq_mgr *userq_mgr;
@@ -134,4 +141,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
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +4 -0
@@ -263,6 +263,7 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr, userq_props->hqd_active = false; userq_props->tmz_queue = mqd_user->flags &AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE ; + queue->eop_va = compute_mqd->eop_va;kfree (compute_mqd); }else if (queue->queue_type ==AMDGPU_HW_IP_GFX ) {struct drm_amdgpu_userq_mqd_gfx11 *mqd_gfx_v11;
@@ -284,6 +285,8 @@ 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 ; + queue->shadow_va = mqd_gfx_v11->shadow_va; + queue->csa_va = mqd_gfx_v11->csa_va;if (amdgpu_userq_input_va_validate (queue->vm, mqd_gfx_v11->shadow_va, shadow_info.shadow_size)) {
@@ -317,6 +320,7 @@ static int mes_userq_mqd_create(struct amdgpu_userq_mgr *uq_mgr, } userq_props->csa_addr = mqd_sdma_v11->csa_va; + queue->csa_va = mqd_sdma_v11->csa_va;kfree (mqd_sdma_v11); }
Signature · 1 line
2.34.1
PATCH v5 6/9 drm/amdgpu: track the userq bo va for its obj management
In reply to Prike LiangThe user queue object destroy requires ensuring its VA keeps mapping prior to the queue being destroyed. Otherwise, it seems a bug in the user space or VA freed wrongly, and the kernel driver should report an invalidated error to the user IOCLT request.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +15 -0
@@ -515,12 +515,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 mapped, +* 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 umapped here\n" ); + r = -EINVAL ; + } + r =amdgpu_userq_unmap_helper (uq_mgr, queue);/*TODO: It requires a reset for userq hw unmap error*/ if (unlikely (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);
@@ -641,6 +653,9 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) goto unlock; } +/* refer to the userq objects vm bo*/ +amdgpu_userq_buffer_vas_get (queue->vm, queue); + 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
PATCH v5 7/9 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 | 78 +++++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 3 + 2 files changed, 81 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +78 -0
@@ -1180,3 +1180,81 @@ 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) +{ + va = va <<AMDGPU_GPU_PAGE_SHIFT |AMDGPU_GMC_HOLE_END ; + +switch (queue->queue_type) { +case AMDGPU_HW_IP_GFX : +if (queue->queue_va == va || + queue->wptr_va == va || + queue->rptr_va == va || + queue->shadow_va == va || + queue->csa_va == va) +return true; +break ; +case AMDGPU_HW_IP_COMPUTE : +if (queue->queue_va == va || + queue->wptr_va == va || + queue->rptr_va == va || + queue->eop_va == va) +return true; +break ; +case AMDGPU_HW_IP_DMA : +if (queue->queue_va == va || + queue->wptr_va == va || + queue->rptr_va == va || + queue->csa_va == va) +return true; +break ; +default : +break ; + } + +return false; +} + +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 (&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 ,
@@ -148,4 +149,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
Christian König
maintainerRe: [PATCH v5 7/9] drm/amdgpu: add userq va unmap validated helper
Quoted 14 lines · Prike Liang
This helper can validate the userq whether can be
unmapped prior to the userq VA GEM unmap.Signed-off-by Prike Liang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 78 +++++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 3 + 2 files changed, 81 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-0
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev, · line 1187
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5
-0@@ -1180,3 +1180,81 @@ 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) +{ + va = va <<AMDGPU_GPU_PAGE_SHIFT |AMDGPU_GMC_HOLE_END ;
Still clear NAK to doing that here.
Quoted 23 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +23
-0+ +switch (queue->queue_type) { +case AMDGPU_HW_IP_GFX : +if (queue->queue_va == va || + queue->wptr_va == va || + queue->rptr_va == va || + queue->shadow_va == va || + queue->csa_va == va) +return true; +break ; +case AMDGPU_HW_IP_COMPUTE : +if (queue->queue_va == va || + queue->wptr_va == va || + queue->rptr_va == va || + queue->eop_va == va) +return true; +break ; +case AMDGPU_HW_IP_DMA : +if (queue->queue_va == va || + queue->wptr_va == va || + queue->rptr_va == va || + queue->csa_va == va) +return true;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev, · line 1220
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10
-0+break ; +default : +break ; + } + +return false; +} + +int amdgpu_userq_gem_va_unmap_validate (struct amdgpu_vm *vm, +uint64_t va)
I don't see an user of this function?
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev, · line 1225
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5
-0+{ +struct amdgpu_fpriv *fpriv =vm_to_fpriv (vm); +struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr; + +if (&fpriv->vm == vm) {
This check is complete nosense.
Quoted 40 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +35
-0+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 +0
-0
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h line 34
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +1
-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)
NAK to that, the VM should not be casted to fpriv.
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2
-0enum amdgpu_userq_state {AMDGPU_USERQ_STATE_UNMAPPED =0 ,@@ -148,4 +149,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
RE: [PATCH v5 7/9] drm/amdgpu: add userq va unmap validated helper
[Public]
Regards,
Prike
Quoted 33 lines
-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Monday, July 7, 2025 5:37 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v5 7/9] drm/amdgpu: add userq va unmap validated helperdrivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int
Quoted 16 lines · Prike Liang
This helper can validate the userq whether can be unmapped prior to the userq VA GEM unmap.
Signed-off-by Prike Liang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 78 +++++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 3 + 2 files changed, 81 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-0@@ -1180,3 +1180,81 @@ intamdgpu_userq_start_sched_for_enforce_isolation(struct
amdgpu_device*adev,drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-0mutex_unlock (&adev->userq_mutex);return ret; } + +static bool amdgpu_userq_gem_va_unmap_queue_retrieve(struct
amdgpu_usermode_queue*queue,
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev, · line 1187
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3
-0+ uint64_t va) +{ + va = va <<AMDGPU_GPU_PAGE_SHIFT |AMDGPU_GMC_HOLE_END ;Still clear NAK to doing that here.
It seems there's no existing VM helper for aligning the userspace VA into gem mapping range.
Without such alignment change, could you please give more detail how to covert the VA between the user input and GEM mapping range?
Quoted 109 lines
Quoted 23 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +23
-0+ +switch (queue->queue_type) { +case AMDGPU_HW_IP_GFX : +if (queue->queue_va == va || + queue->wptr_va == va || + queue->rptr_va == va || + queue->shadow_va == va || + queue->csa_va == va) +return true; +break ; +case AMDGPU_HW_IP_COMPUTE : +if (queue->queue_va == va || + queue->wptr_va == va || + queue->rptr_va == va || + queue->eop_va == va) +return true; +break ; +case AMDGPU_HW_IP_DMA : +if (queue->queue_va == va || + queue->wptr_va == va || + queue->rptr_va == va || + queue->csa_va == va) +return true;drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev, · line 1220
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10
-0+break ; +default : +break ; + } + +return false; +} + +int amdgpu_userq_gem_va_unmap_validate (struct amdgpu_vm *vm, +uint64_t va)I don't see an user of this function?
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev, · line 1225
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5
-0+{ +struct amdgpu_fpriv *fpriv =vm_to_fpriv (vm); +struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr; + +if (&fpriv->vm == vm) {This check is complete nosense.
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5
-0+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.*/
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5
-0+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)) {
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int · line 1236
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-0+ dev_dbg(uq_mgr->adev->dev, "queue(id:%d)not belond to vm:%p\n",
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +7
-0+ queue_id,vm); +continue ; + } + +if (f && !dma_fence_is_signaled (f)) { + + dev_warn(uq_mgr->adev->dev, "try to unmapthe busy queue(id:%d):%p under vm:%p\n",
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int · line 1245
Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-0+ queue_id, queue, vm); +/* Need to set a resonable state for avoiding reusing this queue*/
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int · line 1246
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-0+ queue->state =
AMDGPU_USERQ_STATE_HUNG;Quoted 22 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +14
-0+ 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 +0
-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,Quoted 3 lines
userq_mgr) #definework_to_uq_mgr(w, name)container_of(w, struct amdgpu_userq_mgr, name)
+#definevm_to_fpriv(v)container_of(v, struct amdgpu_fpriv, vm)NAK to that, the VM should not be casted to fpriv.
Quoted 10 lines
enum
amdgpu_userq_state{
AMDGPU_USERQ_STATE_UNMAPPED= 0,drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2
-0@@ -148,4 +149,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
Christian König
maintainerRe: [PATCH v5 7/9] drm/amdgpu: add userq va unmap validated helper
Quoted 38 lines · Prike
[Public]
Regards,
PrikeQuoted 33 lines
-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Monday, July 7, 2025 5:37 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v5 7/9] drm/amdgpu: add userq va unmap validated helperdrivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int
Quoted 16 lines · Prike Liang
This helper can validate the userq whether can be unmapped prior to the userq VA GEM unmap.
Signed-off-by Prike Liang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 78 +++++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 3 + 2 files changed, 81 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-0@@ -1180,3 +1180,81 @@ intamdgpu_userq_start_sched_for_enforce_isolation(struct
amdgpu_device*adev,drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-0mutex_unlock (&adev->userq_mutex);return ret; } + +static bool amdgpu_userq_gem_va_unmap_queue_retrieve(struct
amdgpu_usermode_queue*queue,
Quoted 7 lines
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev, · line 1187
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3
-0+ uint64_t va) +{ + va = va <<AMDGPU_GPU_PAGE_SHIFT |AMDGPU_GMC_HOLE_END ;Still clear NAK to doing that here.
It seems there's no existing VM helper for aligning the userspace VA into gem mapping range.
Without such alignment change, could you please give more detail how to covert the VA between the user input and GEM mapping range?
Ok sounds like I wasn't clear enough what the problem is: Don't convert the VA between the user input and the GEM mapping ranges!
Instead when the queue is resumed validate the VAs and make sure that all of them are mapped. See amdgpu_cs_find_mapping() for an example.
When any of them are not mapped correctly then don't resume the queue.
As second step we then need to figure out how to suspend the queue when any of those mappings change.
Regards,
Christian.
Quoted 111 lines
Quoted 110 lines
Quoted 24 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +23
-0+ +switch (queue->queue_type) { +case AMDGPU_HW_IP_GFX : +if (queue->queue_va == va || + queue->wptr_va == va || + queue->rptr_va == va || + queue->shadow_va == va || + queue->csa_va == va) +return true; +break ; +case AMDGPU_HW_IP_COMPUTE : +if (queue->queue_va == va || + queue->wptr_va == va || + queue->rptr_va == va || + queue->eop_va == va) +return true; +break ; +case AMDGPU_HW_IP_DMA : +if (queue->queue_va == va || + queue->wptr_va == va || + queue->rptr_va == va || + queue->csa_va == va) +return true;drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev, · line 1220
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10
-0+break ; +default : +break ; + } + +return false; +} + +int amdgpu_userq_gem_va_unmap_validate (struct amdgpu_vm *vm, +uint64_t va)I don't see an user of this function?
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev, · line 1225
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5
-0+{ +struct amdgpu_fpriv *fpriv =vm_to_fpriv (vm); +struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr; + +if (&fpriv->vm == vm) {This check is complete nosense.
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5
-0+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.*/
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5
-0+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)) {
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int · line 1236
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-0+ dev_dbg(uq_mgr->adev->dev, "queue(id:%d)not belond to vm:%p\n",
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +7
-0+ queue_id,vm); +continue ; + } + +if (f && !dma_fence_is_signaled (f)) { + + dev_warn(uq_mgr->adev->dev, "try to unmapthe busy queue(id:%d):%p under vm:%p\n",
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int · line 1245
Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-0+ queue_id, queue, vm); +/* Need to set a resonable state for avoiding reusing this queue*/
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int · line 1246
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-0+ queue->state =
AMDGPU_USERQ_STATE_HUNG;Quoted 22 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +14
-0+ 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 +0
-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,Quoted 3 lines
userq_mgr) #definework_to_uq_mgr(w, name)container_of(w, struct amdgpu_userq_mgr, name)
+#definevm_to_fpriv(v)container_of(v, struct amdgpu_fpriv, vm)NAK to that, the VM should not be casted to fpriv.
Quoted 10 lines
enum
amdgpu_userq_state{
AMDGPU_USERQ_STATE_UNMAPPED= 0,drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2
-0@@ -148,4 +149,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
RE: [PATCH v5 7/9] drm/amdgpu: add userq va unmap validated helper
[Public]
Regards,
Prike
Quoted 55 lines
-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Tuesday, July 8, 2025 5:18 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v5 7/9] drm/amdgpu: add userq va unmap validated helperQuoted 23 lines · Prike
[Public]
Regards,
PrikeQuoted 18 lines
-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Monday, July 7, 2025 5:37 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v5 7/9] drm/amdgpu: add userq va unmap validated helperQuoted 7 lines · Prike Liang
This helper can validate the userq whether can be unmapped prior to the userq VA GEM unmap.
Signed-off-by Prike Liang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 78+++++++++++++++++++++++
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h|
Quoted 17 lines
Quoted 17 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int
Quoted 9 lines
3 +
2 files changed, 81 insertions(+)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-0@@ -1180,3 +1180,81 @@ intamdgpu_userq_start_sched_for_enforce_isolation(struct
amdgpu_device*adev,drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-0mutex_unlock (&adev->userq_mutex);return ret; } + +static bool amdgpu_userq_gem_va_unmap_queue_retrieve(struct
amdgpu_usermode_queue*queue,Quoted 5 lines
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int · line 1186
Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-0+ uint64_t va) { + va = va <<AMDGPU_GPU_PAGE_SHIFT |AMDGPU_GMC_HOLE_END ;Still clear NAK to doing that here.
It seems there's no existing VM helper for aligning the userspace VA into gem
mapping range.
Quoted 8 lines
Quoted 1 line
Without such alignment change, could you please give more detail how to covert
the VA between the user input and GEM mapping range?
Ok sounds like I wasn't clear enough what the problem is: Don't convert the VA between the user input and the GEM mapping ranges!
Instead when the queue is resumed validate the VAs and make sure that all of them are mapped. See
amdgpu_cs_find_mapping()for an example.
Thank you for the explanation. Yes, in the patch #5 amdgpu_userq_buffer_va_mapped() is using the userq VA to find out its GEM mapping. But to use amdgpu_vm_bo_lookup_mapping() to find out the userq VA mapping, which requires masking and aligning the useq input VA to the GEM mapping range first.
Quoted 125 lines
When any of them are not mapped correctly then don't resume the queue.
As second step we then need to figure out how to suspend the queue when any of those mappings change.
Regards,
Christian.Quoted 117 lines
Quoted 116 lines
Quoted 24 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +23
-0+ +switch (queue->queue_type) { +case AMDGPU_HW_IP_GFX : +if (queue->queue_va == va || + queue->wptr_va == va || + queue->rptr_va == va || + queue->shadow_va == va || + queue->csa_va == va) +return true; +break ; +case AMDGPU_HW_IP_COMPUTE : +if (queue->queue_va == va || + queue->wptr_va == va || + queue->rptr_va == va || + queue->eop_va == va) +return true; +break ; +case AMDGPU_HW_IP_DMA : +if (queue->queue_va == va || + queue->wptr_va == va || + queue->rptr_va == va || + queue->csa_va == va) +return true;drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev, · line 1220
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10
-0+break ; +default : +break ; + } + +return false; +} + +int amdgpu_userq_gem_va_unmap_validate (struct amdgpu_vm *vm, +uint64_t va)I don't see an user of this function?
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev, · line 1225
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5
-0+{ +struct amdgpu_fpriv *fpriv =vm_to_fpriv (vm); +struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr; + +if (&fpriv->vm == vm) {This check is complete nosense.
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +6
-0+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.*/
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5
-0+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)) {
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int · line 1237
Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-0+ dev_dbg(uq_mgr->adev->dev, + "queue(id:%d)not belond to vm:%p\n",
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +8
-0+ queue_id,vm); +continue ; + } + +if (f && !dma_fence_is_signaled (f)) { + + dev_warn(uq_mgr->adev->dev, "try + to unmapthe busy queue(id:%d):%p under vm:%p\n",
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int · line 1248
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3
-0+ queue_id, queue, vm); +/* Need to set a resonable state +for avoiding reusing this queue*/
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c int · line 1249
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-0+ queue->state =
AMDGPU_USERQ_STATE_HUNG;Quoted 22 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +14
-0+ 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 +0
-0@@ -31,6 +31,7 @@#define to_ev_fence (f) container_of(f, struct amdgpu_eviction_fence,Quoted 5 lines
base) #define
uq_mgr_to_fpriv(u)container_of(u, structamdgpu_fpriv,userq_mgr) #definework_to_uq_mgr(w, name)container_of(w, struct amdgpu_userq_mgr, name)
+#definevm_to_fpriv(v)container_of(v, struct amdgpu_fpriv, vm)NAK to that, the VM should not be casted to fpriv.
Quoted 10 lines
enum amdgpu_userq_state { AMDGPU_USERQ_STATE_UNMAPPED = 0, @@ -148,4 +149,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);drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +2
-0+int amdgpu_userq_gem_va_unmap_validate (struct amdgpu_vm *vm, +uint64_t va);#endif
PATCH v5 8/9 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 | 9 +++++++++ 2 files changed, 22 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +13 -3
@@ -281,7 +281,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) {
@@ -290,10 +290,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
@@ -509,7 +513,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" ); +/* For the fence signal timeout case, it requires resetting the busy queue.*/ + r = -ETIMEDOUT ; + } + 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 +9 -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,14 @@ int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,return -ENOENT ; } +/* It's unlikely to happen that the mapping userq hasn't been idled +* during user requests GEM unmap IOCTL except for forcing the unmap +* 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
Christian König
maintainerRe: [PATCH v5 8/9] drm/amdgpu: validate userq activity status for GEM_VA unmap
Quoted 3 lines · Prike Liang
The 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.
Clear NAK to the whole approach.
We should never deny unmapping a VA because it is used by an userqueue. This can cause a rat tail of problems in userspace.
Instead we *must* suspend the userqueue when the VA is unmapped and deny resuming it.
I think we can do that by adjusting the usage of the eviction fence for the BOs used by the user queue.
Regards,
Christian.
Quoted 78 lines
Signed-off-by Prike Liang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 16 +++++++++++++--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 9 +++++++++ 2 files changed, 22 insertions(+), 3 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +13
-3@@ -281,7 +281,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) {@@ -290,10 +290,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@@ -509,7 +513,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" ); +/* For the fence signal timeout case, it requires resetting the busy queue.*/ + r = -ETIMEDOUT ; + } + 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 +9
-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,14 @@ int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,return -ENOENT ; } +/* It's unlikely to happen that the mapping userq hasn't been idled +* during user requests GEM unmap IOCTL except for forcing the unmap +* 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 ;
RE: [PATCH v5 8/9] drm/amdgpu: validate userq activity status for GEM_VA unmap
[Public]
Regards,
Prike
Quoted 15 lines
-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Monday, July 7, 2025 5:43 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v5 8/9] drm/amdgpu: validate userq activity status forGEM_VAunmapQuoted 3 lines · Prike Liang
The 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.
Clear NAK to the whole approach.
Quoted 8 lines
We should never deny unmapping a VA because it is used by an userqueue. This can cause a rat tail of problems in userspace.
Instead we *must* suspend the userqueue when the VA is unmapped and deny resuming it.
I think we can do that by adjusting the usage of the eviction fence for the BOs used by the user queue.
I'm not sure understand correctly, do we need a notifier for the userq VA unmap event
or at amdgpu_vm_bo_unmap() directly invokes a new function of canceling the userq restore work and marking the userq state as hang to avoid further using it. Or only do the userq VA unmap check at the userq restore work? If not, could you give more detail on this solution?
Quoted 92 lines
Regards,
Christian.drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_map_helper(struct amdgpu_userq_mgr
Quoted 13 lines
Signed-off-by Prike Liang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 16 +++++++++++++--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 9 +++++++++ 2 files changed, 22 insertions(+), 3 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-0@@ -281,7 +281,7 @@ amdgpu_userq_map_helper(struct amdgpu_userq_mgr
*uq_mgr,drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_map_helper(struct amdgpu_userq_mgr *uq_mgr,
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-1return r; } -static void +static int amdgpu_userq_wait_for_last_fence (struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_queue *queue) { @@ -290,10 +290,14 @@
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_wait_for_last_fence(struct amdgpu_userq_mgr *uq_mgr,
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-0amdgpu_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",
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_destroy(struct drm_file *filp, int
Quoted 10 lines
f->context, f->seqno);drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4
-0+return -ETIMEDOUT ; + } } + +return 0 ; } static void@@ -509,7 +513,13 @@ 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)
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3
-1mutex_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 busyuserq\n");
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_destroy(struct drm_file *filp, int · line 519
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-0+/* For the fence signal timeout case, it requires resetting the busy queue.*/
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_bo_unmap(struct amdgpu_device
Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3
-0+ r = -ETIMEDOUT ; + } + 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 +0
-0@@ -1929,6 +1929,7 @@ int amdgpu_vm_bo_unmap(struct amdgpu_device*adev,
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_bo_unmap(struct amdgpu_device
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1
-0struct 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,14 @@ int amdgpu_vm_bo_unmap(struct amdgpu_device*adev,
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +7
-0return -ENOENT ; } +/* It's unlikely to happen that the mapping userq hasn't been idled +* during user requests GEM unmap IOCTL except for forcing the unmap +* 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
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2
-0+user space\n"); +list_del (&mapping->list);amdgpu_vm_it_remove (mapping, &vm->va); mapping->bo_va =NULL ;
Christian König
maintainerRe: [PATCH v5 8/9] drm/amdgpu: validate userq activity status for GEM_VA unmap
Quoted 23 lines · Prike
[Public]
Regards,
PrikeQuoted 18 lines
-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Monday, July 7, 2025 5:43 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>
Subject: Re: [PATCH v5 8/9] drm/amdgpu: validate userq activity status forGEM_VAunmapQuoted 3 lines · Prike Liang
The 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.
Clear NAK to the whole approach.
We should never deny unmapping a VA because it is used by an userqueue. This can cause a rat tail of problems in userspace.
Quoted 8 lines
Quoted 5 lines
Instead we *must* suspend the userqueue when the VA is unmapped and deny resuming it.
I think we can do that by adjusting the usage of the eviction fence for the BOs used by the user queue.
I'm not sure understand correctly, do we need a notifier for the userq VA unmap event
or atamdgpu_vm_bo_unmap()directly invokes a new function of canceling the userq restore work and marking the userq state as hang to avoid further using it. Or only do the userq VA unmap check at the userq restore work? If not, could you give more detail on this solution?
I haven't fully flashed out the solution either.
What we need is a) that amdgpu_vm_bo_unmap() waits for the eviction fence of the user queues to signal which suspends the user queue and b) that the starting/resuming the user queue validates the VA addresses they use and makes sure that the eviction fence is publicated.
This way the queue never starts or resumes while the VAs are not valid.
Regards,
Christian.
Quoted 94 lines
Quoted 93 lines
Regards,
Christian.drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_map_helper(struct amdgpu_userq_mgr
Quoted 13 lines
Signed-off-by Prike Liang <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 16 +++++++++++++--- drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 9 +++++++++ 2 files changed, 22 insertions(+), 3 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-0@@ -281,7 +281,7 @@ amdgpu_userq_map_helper(struct amdgpu_userq_mgr
*uq_mgr,drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_map_helper(struct amdgpu_userq_mgr *uq_mgr,
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-1return r; } -static void +static int amdgpu_userq_wait_for_last_fence (struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_queue *queue) { @@ -290,10 +290,14 @@
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_wait_for_last_fence(struct amdgpu_userq_mgr *uq_mgr,
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-0amdgpu_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",
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_destroy(struct drm_file *filp, int
Quoted 10 lines
f->context, f->seqno);drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4
-0+return -ETIMEDOUT ; + } } + +return 0 ; } static void@@ -509,7 +513,13 @@ 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)
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3
-1mutex_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 busyuserq\n");
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_destroy(struct drm_file *filp, int · line 519
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-0+/* For the fence signal timeout case, it requires resetting the busy queue.*/
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_bo_unmap(struct amdgpu_device
Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3
-0+ r = -ETIMEDOUT ; + } + 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 +0
-0@@ -1929,6 +1929,7 @@ int amdgpu_vm_bo_unmap(struct amdgpu_device*adev,
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_bo_unmap(struct amdgpu_device
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1
-0struct 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,14 @@ int amdgpu_vm_bo_unmap(struct amdgpu_device*adev,
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +7
-0return -ENOENT ; } +/* It's unlikely to happen that the mapping userq hasn't been idled +* during user requests GEM unmap IOCTL except for forcing the unmap +* 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
Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2
-0+user space\n"); +list_del (&mapping->list);amdgpu_vm_it_remove (mapping, &vm->va); mapping->bo_va =NULL ;
PATCH v5 9/9 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
@@ -314,22 +314,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
@@ -121,8 +121,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