drm/amdgpu/userq: defer queue publication until create completes
Subsystems (2)
PATCH v4 1/3 drm/amdgpu/userq: defer queue publication until create completes
The userq create path publishes queues to global xarrays such as userq_doorbell_xa and userq_xa before creation was fully complete. Later on if create queue fails, teardown could free an already visible queue, opening a UAF race with concurrent queue walkers. Also calling amdgpu_userq_put in such cases complicates the cleanup.
Solution is to defer queue publication until create succeeds and no partially initialized queue is exposed.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 66 +++++++++++------------ 1 file changed, 33 insertions(+), 33 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +33 -33
@@ -758,7 +758,6 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)const struct amdgpu_userq_funcs *uq_funcs;struct amdgpu_usermode_queue *queue;struct amdgpu_db_info db_info; -char *queue_name;bool skip_map_queue;u32 qid;uint64_t index;
@@ -848,32 +847,6 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) goto unlock; } -/* drop this refcount during queue destroy */ -kref_init (&queue->refcount); - -/* Wait for mode-1 reset to complete */ -down_read (&adev->reset_domain->sem); - r =xa_err (xa_store_irq (&adev->userq_doorbell_xa, index, queue,GFP_KERNEL )); -if (r) { -kfree (queue); -up_read (&adev->reset_domain->sem); - goto unlock; - } - - r =xa_alloc (&uq_mgr->userq_xa, &qid, queue, -XA_LIMIT (1 ,AMDGPU_MAX_USERQ_COUNT ),GFP_KERNEL ); -if (r) { -drm_file_err (uq_mgr->file,"Failed to allocate a queue id\n" ); -amdgpu_userq_fence_driver_free (queue); -xa_erase_irq (&adev->userq_doorbell_xa, index); - uq_funcs->mqd_destroy (queue); -kfree (queue); - r = -ENOMEM ; -up_read (&adev->reset_domain->sem); - goto unlock; - } -up_read (&adev->reset_domain->sem); -/* don't map the queue if scheduling is halted */ if (adev->userq_halt_for_enforce_isolation && ((queue->queue_type == AMDGPU_HW_IP_GFX) ||
@@ -885,28 +858,55 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) r =amdgpu_userq_map_helper (queue);if (r) {drm_file_err (uq_mgr->file,"Failed to map Queue\n" ); -xa_erase_irq (&adev->userq_doorbell_xa, index); -xa_erase (&uq_mgr->userq_xa, qid); -amdgpu_userq_fence_driver_free (queue); uq_funcs->mqd_destroy (queue); +amdgpu_userq_fence_driver_free (queue);kfree (queue); goto unlock; } } - queue_name =kasprintf (GFP_KERNEL ,"queue-%d" , qid); -if (!queue_name) { +/* drop this refcount during queue destroy */ +kref_init (&queue->refcount); + +/* Wait for mode-1 reset to complete */ +down_read (&adev->reset_domain->sem); + r =xa_alloc (&uq_mgr->userq_xa, &qid, queue, +XA_LIMIT (1 ,AMDGPU_MAX_USERQ_COUNT ),GFP_KERNEL ); +if (r) { +if (!skip_map_queue) +amdgpu_userq_unmap_helper (queue); + + uq_funcs->mqd_destroy (queue); +amdgpu_userq_fence_driver_free (queue); +kfree (queue); r = -ENOMEM ; +up_read (&adev->reset_domain->sem); goto unlock; } + r =xa_err (xa_store_irq (&adev->userq_doorbell_xa, index, queue,GFP_KERNEL )); +if (r) { +xa_erase (&uq_mgr->userq_xa, qid); +if (!skip_map_queue) +amdgpu_userq_unmap_helper (queue); + + uq_funcs->mqd_destroy (queue); +amdgpu_userq_fence_driver_free (queue); +kfree (queue); +up_read (&adev->reset_domain->sem); + goto unlock; + } +up_read (&adev->reset_domain->sem); +#if defined(CONFIG_DEBUG_FS ) +char queue_name[32 ]; + +scnprintf (queue_name,sizeof (queue_name),"queue_%d" , qid);/* Queue dentry per client to hold MQD information */ queue->debugfs_queue =debugfs_create_dir (queue_name, filp->debugfs_client);debugfs_create_file ("mqd_info" ,0444 , queue->debugfs_queue, queue, &amdgpu_mqd_info_fops);#endif amdgpu_userq_init_hang_detect_work (queue); -kfree (queue_name); args->out.queue_id = qid;atomic_inc (&uq_mgr->userq_count[queue->queue_type]);
Signature · 1 line
2.34.1
PATCH v4 2/3 drm/amdgpu/userq: declutter the code with goto
Clean up the amdgpu_userq_create function clean up in failure condition using goto method. This avoid replication of cleanup for every failure conditon.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 40 ++++++++++------------- 1 file changed, 18 insertions(+), 22 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +18 -22
@@ -818,17 +818,15 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) amdgpu_userq_input_va_validate(adev, queue, args->in.rptr_va, AMDGPU_GPU_PAGE_SIZE) || amdgpu_userq_input_va_validate(adev, queue, args->in.wptr_va, AMDGPU_GPU_PAGE_SIZE)) { r = -EINVAL ; -kfree (queue); - goto unlock; + goto free_queue; }/* Convert relative doorbell offset into absolute doorbell index */ index =amdgpu_userq_get_doorbell_index (uq_mgr, &db_info, filp);if (index == (uint64_t )-EINVAL ) {drm_file_err (uq_mgr->file,"Failed to get doorbell for queue\n" ); -kfree (queue); r = -EINVAL ; - goto unlock; + goto free_queue; } queue->doorbell_index = index;
@@ -836,15 +834,13 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) r =amdgpu_userq_fence_driver_alloc (adev, queue);if (r) {drm_file_err (uq_mgr->file,"Failed to alloc fence driver\n" ); - goto unlock; + goto free_queue; } r = uq_funcs->mqd_create (queue, &args->in);if (r) {drm_file_err (uq_mgr->file,"Failed to create Queue\n" ); -amdgpu_userq_fence_driver_free (queue); -kfree (queue); - goto unlock; + goto clean_fence_driver; }/* don't map the queue if scheduling is halted */
@@ -858,10 +854,8 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) r =amdgpu_userq_map_helper (queue);if (r) {drm_file_err (uq_mgr->file,"Failed to map Queue\n" ); - uq_funcs->mqd_destroy (queue); -amdgpu_userq_fence_driver_free (queue); -kfree (queue); - goto unlock; +down_read (&adev->reset_domain->sem); + goto clean_mqd; } }
@@ -870,18 +864,15 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)/* Wait for mode-1 reset to complete */ down_read (&adev->reset_domain->sem); + r =xa_alloc (&uq_mgr->userq_xa, &qid, queue,XA_LIMIT (1 ,AMDGPU_MAX_USERQ_COUNT ),GFP_KERNEL );if (r) {if (!skip_map_queue)amdgpu_userq_unmap_helper (queue); - uq_funcs->mqd_destroy (queue); -amdgpu_userq_fence_driver_free (queue); -kfree (queue); r = -ENOMEM ; -up_read (&adev->reset_domain->sem); - goto unlock; + goto clean_mqd; } r =xa_err (xa_store_irq (&adev->userq_doorbell_xa, index, queue,GFP_KERNEL ));
@@ -890,11 +881,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)if (!skip_map_queue)amdgpu_userq_unmap_helper (queue); - uq_funcs->mqd_destroy (queue); -amdgpu_userq_fence_driver_free (queue); -kfree (queue); -up_read (&adev->reset_domain->sem); - goto unlock; + goto clean_mqd; }up_read (&adev->reset_domain->sem);
@@ -910,7 +897,16 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) args->out.queue_id = qid;atomic_inc (&uq_mgr->userq_count[queue->queue_type]); +mutex_unlock (&uq_mgr->userq_mutex); +return 0 ; +clean_mqd: + uq_funcs->mqd_destroy (queue); +up_read (&adev->reset_domain->sem); +clean_fence_driver: +amdgpu_userq_fence_driver_free (queue); +free_queue: +kfree (queue); unlock:mutex_unlock (&uq_mgr->userq_mutex);
Signature · 1 line
2.34.1
Re: [PATCH v4 2/3] drm/amdgpu/userq: declutter the code with goto
Quoted 5 lines · Sunil Khatri
Clean up the
amdgpu_userq_createfunction clean up in failure condition using goto method. This avoid replication of cleanup for every failure conditon.Signed-off-by Sunil Khatri <[email protected]>
Quoted 110 lines
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 40 ++++++++++------------- 1 file changed, 18 insertions(+), 22 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +18
-22@@ -818,17 +818,15 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) amdgpu_userq_input_va_validate(adev, queue, args->in.rptr_va, AMDGPU_GPU_PAGE_SIZE) || amdgpu_userq_input_va_validate(adev, queue, args->in.wptr_va, AMDGPU_GPU_PAGE_SIZE)) { r = -EINVAL ; -kfree (queue); - goto unlock; + goto free_queue; }/* Convert relative doorbell offset into absolute doorbell index */ index =amdgpu_userq_get_doorbell_index (uq_mgr, &db_info, filp);if (index == (uint64_t )-EINVAL ) {drm_file_err (uq_mgr->file,"Failed to get doorbell for queue\n" ); -kfree (queue); r = -EINVAL ; - goto unlock; + goto free_queue; } queue->doorbell_index = index;@@ -836,15 +834,13 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) r =amdgpu_userq_fence_driver_alloc (adev, queue);if (r) {drm_file_err (uq_mgr->file,"Failed to alloc fence driver\n" ); - goto unlock; + goto free_queue; } r = uq_funcs->mqd_create (queue, &args->in);if (r) {drm_file_err (uq_mgr->file,"Failed to create Queue\n" ); -amdgpu_userq_fence_driver_free (queue); -kfree (queue); - goto unlock; + goto clean_fence_driver; }/* don't map the queue if scheduling is halted */ @@ -858,10 +854,8 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) r =amdgpu_userq_map_helper (queue);if (r) {drm_file_err (uq_mgr->file,"Failed to map Queue\n" ); - uq_funcs->mqd_destroy (queue); -amdgpu_userq_fence_driver_free (queue); -kfree (queue); - goto unlock; +down_read (&adev->reset_domain->sem); + goto clean_mqd; } }@@ -870,18 +864,15 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)/* Wait for mode-1 reset to complete */ down_read (&adev->reset_domain->sem); + r =xa_alloc (&uq_mgr->userq_xa, &qid, queue,XA_LIMIT (1 ,AMDGPU_MAX_USERQ_COUNT ),GFP_KERNEL );if (r) {if (!skip_map_queue)amdgpu_userq_unmap_helper (queue); - uq_funcs->mqd_destroy (queue); -amdgpu_userq_fence_driver_free (queue); -kfree (queue); r = -ENOMEM ; -up_read (&adev->reset_domain->sem); - goto unlock; + goto clean_mqd; } r =xa_err (xa_store_irq (&adev->userq_doorbell_xa, index, queue,GFP_KERNEL ));@@ -890,11 +881,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)if (!skip_map_queue)amdgpu_userq_unmap_helper (queue); - uq_funcs->mqd_destroy (queue); -amdgpu_userq_fence_driver_free (queue); -kfree (queue); -up_read (&adev->reset_domain->sem); - goto unlock; + goto clean_mqd; }up_read (&adev->reset_domain->sem);@@ -910,7 +897,16 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) args->out.queue_id = qid;atomic_inc (&uq_mgr->userq_count[queue->queue_type]); +mutex_unlock (&uq_mgr->userq_mutex); +return 0 ; +clean_mqd: + uq_funcs->mqd_destroy (queue); +up_read (&adev->reset_domain->sem); +clean_fence_driver: +amdgpu_userq_fence_driver_free (queue); +free_queue: +kfree (queue); unlock:mutex_unlock (&uq_mgr->userq_mutex);
PATCH v4 3/3 drm/amdgpu: push userq debugfs function in amdgpu_debugfs files
In reply to Sunil KhatriDebugfs files for amdgpu are better to be handled in the dedicated amdgpu_debugfs.c/.h files.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c | 53 +++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.h | 4 ++ drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 47 +----------------- 3 files changed, 58 insertions(+), 46 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c +53 -0
@@ -39,6 +39,7 @@#include "amdgpu_reset.h" #include "amdgpu_psp_ta.h" +#include "amdgpu_userq.h" #if defined(CONFIG_DEBUG_FS )
@@ -2156,6 +2157,53 @@ static const struct file_operations amdgpu_pt_info_fops = { .release = single_release, }; +static int amdgpu_mqd_info_read (struct seq_file *m,void *unused) +{ +struct amdgpu_usermode_queue *queue = m->private; +struct amdgpu_bo *bo; +int r; + +if (!queue || !queue->mqd.obj) +return -EINVAL ; + + bo =amdgpu_bo_ref (queue->mqd.obj); + r =amdgpu_bo_reserve (bo, true); +if (r) { +amdgpu_bo_unref (&bo); +return -EINVAL ; + } + +seq_printf (m,"queue_type: %d\n" , queue->queue_type); +seq_printf (m,"mqd_gpu_address: 0x%llx\n" ,amdgpu_bo_gpu_offset (queue->mqd.obj)); + +amdgpu_bo_unreserve (bo); +amdgpu_bo_unref (&bo); + +return 0 ; +} + +static int amdgpu_mqd_info_open (struct inode *inode,struct file *file) +{ +return single_open (file, amdgpu_mqd_info_read, inode->i_private); +} + +static const struct file_operations amdgpu_mqd_info_fops = { + .owner =THIS_MODULE , + .open = amdgpu_mqd_info_open, + .read = seq_read, + .llseek = seq_lseek, + .release = single_release, +}; + +void amdgpu_debugfs_userq_init (struct drm_file *file,struct amdgpu_usermode_queue *queue,int qid) +{ +char queue_name[32 ]; + +scnprintf (queue_name,sizeof (queue_name),"queue_%d" , qid); + queue->debugfs_queue =debugfs_create_dir (queue_name, file->debugfs_client); +debugfs_create_file ("mqd_info" ,0444 , queue->debugfs_queue, queue, &amdgpu_mqd_info_fops); +} +void amdgpu_debugfs_vm_init (struct drm_file *file) { debugfs_create_file("vm_pagetable_info", 0444, file->debugfs_client, file,
@@ -2174,4 +2222,9 @@ int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)void amdgpu_debugfs_vm_init (struct drm_file *file) { } +void amdgpu_debugfs_userq_init (struct drm_file *file, +struct amdgpu_usermode_queue *queue, +int qid) +{ +}#endif
drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.h +4 -0
@@ -25,6 +25,7 @@/* * Debugfs */ +struct amdgpu_usermode_queue ;int amdgpu_debugfs_regs_init (struct amdgpu_device *adev);int amdgpu_debugfs_init (struct amdgpu_device *adev);
@@ -34,4 +35,7 @@ void amdgpu_debugfs_firmware_init(struct amdgpu_device *adev);void amdgpu_debugfs_gem_init (struct amdgpu_device *adev);void amdgpu_debugfs_mes_event_log_init (struct amdgpu_device *adev);void amdgpu_debugfs_vm_init (struct drm_file *file); +void amdgpu_debugfs_userq_init (struct drm_file *file, +struct amdgpu_usermode_queue *queue, +int qid);
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -46
59 lines not on this page · raw message
Signature · 1 line
2.34.1
Christian König
maintainerRe: [PATCH v4 3/3] drm/amdgpu: push userq debugfs function in amdgpu_debugfs files
Quoted 159 lines · Sunil Khatri
Debugfs files for amdgpu are better to be handled in the dedicated amdgpu_debugfs.c/.h files.
Signed-off-by Sunil Khatri <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c | 53 +++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.h | 4 ++ drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 47 +----------------- 3 files changed, 58 insertions(+), 46 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c +53
-0@@ -39,6 +39,7 @@#include "amdgpu_reset.h" #include "amdgpu_psp_ta.h" +#include "amdgpu_userq.h" #if defined(CONFIG_DEBUG_FS )@@ -2156,6 +2157,53 @@ static const struct file_operations amdgpu_pt_info_fops = { .release = single_release, }; +static int amdgpu_mqd_info_read (struct seq_file *m,void *unused) +{ +struct amdgpu_usermode_queue *queue = m->private; +struct amdgpu_bo *bo; +int r; + +if (!queue || !queue->mqd.obj) +return -EINVAL ; + + bo =amdgpu_bo_ref (queue->mqd.obj); + r =amdgpu_bo_reserve (bo, true); +if (r) { +amdgpu_bo_unref (&bo); +return -EINVAL ; + } + +seq_printf (m,"queue_type: %d\n" , queue->queue_type); +seq_printf (m,"mqd_gpu_address: 0x%llx\n" ,amdgpu_bo_gpu_offset (queue->mqd.obj)); + +amdgpu_bo_unreserve (bo); +amdgpu_bo_unref (&bo); + +return 0 ; +} + +static int amdgpu_mqd_info_open (struct inode *inode,struct file *file) +{ +return single_open (file, amdgpu_mqd_info_read, inode->i_private); +} + +static const struct file_operations amdgpu_mqd_info_fops = { + .owner =THIS_MODULE , + .open = amdgpu_mqd_info_open, + .read = seq_read, + .llseek = seq_lseek, + .release = single_release, +}; + +void amdgpu_debugfs_userq_init (struct drm_file *file,struct amdgpu_usermode_queue *queue,int qid) +{ +char queue_name[32 ]; + +scnprintf (queue_name,sizeof (queue_name),"queue_%d" , qid); + queue->debugfs_queue =debugfs_create_dir (queue_name, file->debugfs_client); +debugfs_create_file ("mqd_info" ,0444 , queue->debugfs_queue, queue, &amdgpu_mqd_info_fops); +} +void amdgpu_debugfs_vm_init (struct drm_file *file) { debugfs_create_file("vm_pagetable_info", 0444, file->debugfs_client, file,@@ -2174,4 +2222,9 @@ int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)void amdgpu_debugfs_vm_init (struct drm_file *file) { } +void amdgpu_debugfs_userq_init (struct drm_file *file, +struct amdgpu_usermode_queue *queue, +int qid) +{ +}#endif drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.h +4
-0@@ -25,6 +25,7 @@/* * Debugfs */ +struct amdgpu_usermode_queue ;int amdgpu_debugfs_regs_init (struct amdgpu_device *adev);int amdgpu_debugfs_init (struct amdgpu_device *adev);@@ -34,4 +35,7 @@ void amdgpu_debugfs_firmware_init(struct amdgpu_device *adev);void amdgpu_debugfs_gem_init (struct amdgpu_device *adev);void amdgpu_debugfs_mes_event_log_init (struct amdgpu_device *adev);void amdgpu_debugfs_vm_init (struct drm_file *file); +void amdgpu_debugfs_userq_init (struct drm_file *file, +struct amdgpu_usermode_queue *queue, +int qid);drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-40@@ -709,46 +709,6 @@ static int amdgpu_userq_priority_permit(struct drm_file *filp,return -EACCES ; } -#if defined(CONFIG_DEBUG_FS ) -static int amdgpu_mqd_info_read (struct seq_file *m,void *unused) -{ -struct amdgpu_usermode_queue *queue = m->private; -struct amdgpu_bo *bo; -int r; - -if (!queue || !queue->mqd.obj) -return -EINVAL ; - - bo =amdgpu_bo_ref (queue->mqd.obj); - r =amdgpu_bo_reserve (bo, true); -if (r) { -amdgpu_bo_unref (&bo); -return -EINVAL ; - } - -seq_printf (m,"queue_type: %d\n" , queue->queue_type); -seq_printf (m,"mqd_gpu_address: 0x%llx\n" ,amdgpu_bo_gpu_offset (queue->mqd.obj)); - -amdgpu_bo_unreserve (bo); -amdgpu_bo_unref (&bo); - -return 0 ; -} - -static int amdgpu_mqd_info_open (struct inode *inode,struct file *file) -{ -return single_open (file, amdgpu_mqd_info_read, inode->i_private); -} - -static const struct file_operations amdgpu_mqd_info_fops = { - .owner =THIS_MODULE , - .open = amdgpu_mqd_info_open, - .read = seq_read, - .llseek = seq_lseek, - .release = single_release, -}; -#endif -static int amdgpu_userq_create (struct drm_file *filp,union drm_amdgpu_userq *args) {@@ -886,12 +846,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)up_read (&adev->reset_domain->sem);
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) · line 850
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-6#if defined(CONFIG_DEBUG_FS ) -char queue_name[32 ]; - -scnprintf (queue_name,sizeof (queue_name),"queue_%d" , qid); -/* Queue dentry per client to hold MQD information */ - queue->debugfs_queue =debugfs_create_dir (queue_name, filp->debugfs_client); -debugfs_create_file ("mqd_info" ,0444 , queue->debugfs_queue, queue, &amdgpu_mqd_info_fops); +amdgpu_debugfs_userq_init (filp, queue, qid);#endif
That #if defined protection can now be dropped.
With that fixed Reviewed-by: Christian König <[email protected]>.
Regards,
Christian.
Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-0amdgpu_userq_init_hang_detect_work (queue);
Re: [PATCH v4 3/3] drm/amdgpu: push userq debugfs function in amdgpu_debugfs files
Quoted 161 lines · Christian König
Quoted 160 lines · Sunil Khatri
Debugfs files for amdgpu are better to be handled in the dedicated amdgpu_debugfs.c/.h files.
Signed-off-by Sunil Khatri <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c | 53 +++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.h | 4 ++ drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 47 +----------------- 3 files changed, 58 insertions(+), 46 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c +53
-0@@ -39,6 +39,7 @@#include "amdgpu_reset.h" #include "amdgpu_psp_ta.h" +#include "amdgpu_userq.h" #if defined(CONFIG_DEBUG_FS )@@ -2156,6 +2157,53 @@ static const struct file_operations amdgpu_pt_info_fops = { .release = single_release, }; +static int amdgpu_mqd_info_read (struct seq_file *m,void *unused) +{ +struct amdgpu_usermode_queue *queue = m->private; +struct amdgpu_bo *bo; +int r; + +if (!queue || !queue->mqd.obj) +return -EINVAL ; + + bo =amdgpu_bo_ref (queue->mqd.obj); + r =amdgpu_bo_reserve (bo, true); +if (r) { +amdgpu_bo_unref (&bo); +return -EINVAL ; + } + +seq_printf (m,"queue_type: %d\n" , queue->queue_type); +seq_printf (m,"mqd_gpu_address: 0x%llx\n" ,amdgpu_bo_gpu_offset (queue->mqd.obj)); + +amdgpu_bo_unreserve (bo); +amdgpu_bo_unref (&bo); + +return 0 ; +} + +static int amdgpu_mqd_info_open (struct inode *inode,struct file *file) +{ +return single_open (file, amdgpu_mqd_info_read, inode->i_private); +} + +static const struct file_operations amdgpu_mqd_info_fops = { + .owner =THIS_MODULE , + .open = amdgpu_mqd_info_open, + .read = seq_read, + .llseek = seq_lseek, + .release = single_release, +}; + +void amdgpu_debugfs_userq_init (struct drm_file *file,struct amdgpu_usermode_queue *queue,int qid) +{ +char queue_name[32 ]; + +scnprintf (queue_name,sizeof (queue_name),"queue_%d" , qid); + queue->debugfs_queue =debugfs_create_dir (queue_name, file->debugfs_client); +debugfs_create_file ("mqd_info" ,0444 , queue->debugfs_queue, queue, &amdgpu_mqd_info_fops); +} +void amdgpu_debugfs_vm_init (struct drm_file *file) { debugfs_create_file("vm_pagetable_info", 0444, file->debugfs_client, file,@@ -2174,4 +2222,9 @@ int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)void amdgpu_debugfs_vm_init (struct drm_file *file) { } +void amdgpu_debugfs_userq_init (struct drm_file *file, +struct amdgpu_usermode_queue *queue, +int qid) +{ +}#endif drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.h +4
-0@@ -25,6 +25,7 @@/* * Debugfs */ +struct amdgpu_usermode_queue ;int amdgpu_debugfs_regs_init (struct amdgpu_device *adev);int amdgpu_debugfs_init (struct amdgpu_device *adev);@@ -34,4 +35,7 @@ void amdgpu_debugfs_firmware_init(struct amdgpu_device *adev);void amdgpu_debugfs_gem_init (struct amdgpu_device *adev);void amdgpu_debugfs_mes_event_log_init (struct amdgpu_device *adev);void amdgpu_debugfs_vm_init (struct drm_file *file); +void amdgpu_debugfs_userq_init (struct drm_file *file, +struct amdgpu_usermode_queue *queue, +int qid);drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-40@@ -709,46 +709,6 @@ static int amdgpu_userq_priority_permit(struct drm_file *filp,return -EACCES ; } -#if defined(CONFIG_DEBUG_FS ) -static int amdgpu_mqd_info_read (struct seq_file *m,void *unused) -{ -struct amdgpu_usermode_queue *queue = m->private; -struct amdgpu_bo *bo; -int r; - -if (!queue || !queue->mqd.obj) -return -EINVAL ; - - bo =amdgpu_bo_ref (queue->mqd.obj); - r =amdgpu_bo_reserve (bo, true); -if (r) { -amdgpu_bo_unref (&bo); -return -EINVAL ; - } - -seq_printf (m,"queue_type: %d\n" , queue->queue_type); -seq_printf (m,"mqd_gpu_address: 0x%llx\n" ,amdgpu_bo_gpu_offset (queue->mqd.obj)); - -amdgpu_bo_unreserve (bo); -amdgpu_bo_unref (&bo); - -return 0 ; -} - -static int amdgpu_mqd_info_open (struct inode *inode,struct file *file) -{ -return single_open (file, amdgpu_mqd_info_read, inode->i_private); -} - -static const struct file_operations amdgpu_mqd_info_fops = { - .owner =THIS_MODULE , - .open = amdgpu_mqd_info_open, - .read = seq_read, - .llseek = seq_lseek, - .release = single_release, -}; -#endif -static int amdgpu_userq_create (struct drm_file *filp,union drm_amdgpu_userq *args) {@@ -886,12 +846,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)up_read (&adev->reset_domain->sem);
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) · line 850
Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1
-6#if defined(CONFIG_DEBUG_FS ) -char queue_name[32 ]; - -scnprintf (queue_name,sizeof (queue_name),"queue_%d" , qid); -/* Queue dentry per client to hold MQD information */ - queue->debugfs_queue =debugfs_create_dir (queue_name, filp->debugfs_client); -debugfs_create_file ("mqd_info" ,0444 , queue->debugfs_queue, queue, &amdgpu_mqd_info_fops); +amdgpu_debugfs_userq_init (filp, queue, qid);#endif That #if defined protection can now be dropped.
Ah, yes this ain't needed anymore. Will drop that check and push the code. Thanks for the review Christian.
Thanks
Sunil Khatri
Quoted 8 lines
With that fixed Reviewed-by: Christian König <[email protected]>.
Regards,
Christian.Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-0amdgpu_userq_init_hang_detect_work (queue);
Quoted 10 lines · Sunil Khatri
The userq create path publishes queues to global xarrays such as
userq_doorbell_xaanduserq_xabefore creation was fully complete. Later on if create queue fails, teardown could free an already visible queue, opening a UAF race with concurrent queue walkers. Also callingamdgpu_userq_putin such cases complicates the cleanup.Solution is to defer queue publication until create succeeds and no partially initialized queue is exposed.
Signed-off-by Sunil Khatri <[email protected]>
Quoted 111 lines
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 66 +++++++++++------------ 1 file changed, 33 insertions(+), 33 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +33
-33@@ -758,7 +758,6 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)const struct amdgpu_userq_funcs *uq_funcs;struct amdgpu_usermode_queue *queue;struct amdgpu_db_info db_info; -char *queue_name;bool skip_map_queue;u32 qid;uint64_t index;@@ -848,32 +847,6 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) goto unlock; } -/* drop this refcount during queue destroy */ -kref_init (&queue->refcount); - -/* Wait for mode-1 reset to complete */ -down_read (&adev->reset_domain->sem); - r =xa_err (xa_store_irq (&adev->userq_doorbell_xa, index, queue,GFP_KERNEL )); -if (r) { -kfree (queue); -up_read (&adev->reset_domain->sem); - goto unlock; - } - - r =xa_alloc (&uq_mgr->userq_xa, &qid, queue, -XA_LIMIT (1 ,AMDGPU_MAX_USERQ_COUNT ),GFP_KERNEL ); -if (r) { -drm_file_err (uq_mgr->file,"Failed to allocate a queue id\n" ); -amdgpu_userq_fence_driver_free (queue); -xa_erase_irq (&adev->userq_doorbell_xa, index); - uq_funcs->mqd_destroy (queue); -kfree (queue); - r = -ENOMEM ; -up_read (&adev->reset_domain->sem); - goto unlock; - } -up_read (&adev->reset_domain->sem); -/* don't map the queue if scheduling is halted */ if (adev->userq_halt_for_enforce_isolation && ((queue->queue_type == AMDGPU_HW_IP_GFX) ||@@ -885,28 +858,55 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) r =amdgpu_userq_map_helper (queue);if (r) {drm_file_err (uq_mgr->file,"Failed to map Queue\n" ); -xa_erase_irq (&adev->userq_doorbell_xa, index); -xa_erase (&uq_mgr->userq_xa, qid); -amdgpu_userq_fence_driver_free (queue); uq_funcs->mqd_destroy (queue); +amdgpu_userq_fence_driver_free (queue);kfree (queue); goto unlock; } } - queue_name =kasprintf (GFP_KERNEL ,"queue-%d" , qid); -if (!queue_name) { +/* drop this refcount during queue destroy */ +kref_init (&queue->refcount); + +/* Wait for mode-1 reset to complete */ +down_read (&adev->reset_domain->sem); + r =xa_alloc (&uq_mgr->userq_xa, &qid, queue, +XA_LIMIT (1 ,AMDGPU_MAX_USERQ_COUNT ),GFP_KERNEL ); +if (r) { +if (!skip_map_queue) +amdgpu_userq_unmap_helper (queue); + + uq_funcs->mqd_destroy (queue); +amdgpu_userq_fence_driver_free (queue); +kfree (queue); r = -ENOMEM ; +up_read (&adev->reset_domain->sem); goto unlock; } + r =xa_err (xa_store_irq (&adev->userq_doorbell_xa, index, queue,GFP_KERNEL )); +if (r) { +xa_erase (&uq_mgr->userq_xa, qid); +if (!skip_map_queue) +amdgpu_userq_unmap_helper (queue); + + uq_funcs->mqd_destroy (queue); +amdgpu_userq_fence_driver_free (queue); +kfree (queue); +up_read (&adev->reset_domain->sem); + goto unlock; + } +up_read (&adev->reset_domain->sem); +#if defined(CONFIG_DEBUG_FS ) +char queue_name[32 ]; + +scnprintf (queue_name,sizeof (queue_name),"queue_%d" , qid);/* Queue dentry per client to hold MQD information */ queue->debugfs_queue =debugfs_create_dir (queue_name, filp->debugfs_client);debugfs_create_file ("mqd_info" ,0444 , queue->debugfs_queue, queue, &amdgpu_mqd_info_fops);#endif amdgpu_userq_init_hang_detect_work (queue); -kfree (queue_name); args->out.queue_id = qid;atomic_inc (&uq_mgr->userq_count[queue->queue_type]);