Fix some clean up during userq create/destroy
Subsystems (2)
v5 cover Fix some clean up during userq create/destroy
Fix the missing clean up during userq create and destroy. Also introduce the change which uses array for list for userq vas
Sunil Khatri (8):
drm/amdgpu/userq: Fix doorbell object cleanup of queue drm/amdgpu/userq: Fix the mutex_init cleanup for fence_drv_lock drm/amdgpu: simplify return value in amdgpu_userq_get_doorbell_index drm/amdgpu/userq: clean up wptr_obj along with mqd_destroy drm/amdgpu/userq: add amdgpu_bo_unpin when amdgpu_ttm_alloc_gart fails drm/amdgpu/userq: reserve root bo without interruption drm/amdgpu/userq: make sure queue is valid in the hang_detect_work drm/amdgpu/userq: user array to store userq vas
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 86 +++++++++++----------- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 8 +- drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 4 +- 3 files changed, 48 insertions(+), 50 deletions(-)
Signature · 1 line
2.34.1
PATCH v5 1/8 drm/amdgpu/userq: Fix doorbell object cleanup of queue
Unpin and unref the door bell obj if queue creation fails before initialization is complete.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +8 -3
@@ -787,7 +787,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) r = uq_funcs->mqd_create (queue, &args->in);if (r) {drm_file_err (uq_mgr->file,"Failed to create Queue\n" ); - goto clean_mapping; + goto clean_doorbell_bo; }/* Update VM owner at userq submit-time for page-fault attribution. */
@@ -808,7 +808,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)if (r) {drm_file_err (uq_mgr->file,"Failed to map Queue\n" );mutex_unlock (&uq_mgr->userq_mutex); - goto clean_doorbell; + goto erase_doorbell; } }
@@ -831,10 +831,15 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) args->out.queue_id = qid;return 0 ; -clean_doorbell: +erase_doorbell:xa_erase_irq (&adev->userq_doorbell_xa, index); clean_mqd: uq_funcs->mqd_destroy (queue); +clean_doorbell_bo: +amdgpu_bo_reserve (queue->db_obj.obj, true); +amdgpu_bo_unpin (queue->db_obj.obj); +amdgpu_bo_unreserve (queue->db_obj.obj); +amdgpu_bo_unref (&queue->db_obj.obj); clean_mapping:amdgpu_bo_reserve (fpriv->vm.root.bo, true);amdgpu_userq_buffer_vas_list_cleanup (adev, queue);
Signature · 1 line
2.34.1
PATCH v5 2/8 drm/amdgpu/userq: Fix the mutex_init cleanup for fence_drv_lock
In reply to Sunil Khatrimutex fence_drv_lock is destroyed in amdgpu_userq_fence_driver_free also in one of the jump condition mutex_destroy is also called leading to double mutex_destroy.
So rearranging the code so amdgpu_userq_fence_driver_free takes care of the clean up along with mutex_destroy.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -3
@@ -748,12 +748,12 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)INIT_DELAYED_WORK (&queue->hang_detect_work, amdgpu_userq_hang_detect_work); -mutex_init (&queue->fence_drv_lock); -xa_init_flags (&queue->fence_drv_xa,XA_FLAGS_ALLOC ); r =amdgpu_userq_fence_driver_alloc (adev, &queue->fence_drv);if (r) goto free_queue; +xa_init_flags (&queue->fence_drv_xa,XA_FLAGS_ALLOC ); +mutex_init (&queue->fence_drv_lock);/* Make sure the queue can actually run with those virtual addresses. */ r =amdgpu_bo_reserve (fpriv->vm.root.bo, false);if (r)
@@ -844,7 +844,6 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)amdgpu_bo_reserve (fpriv->vm.root.bo, true);amdgpu_userq_buffer_vas_list_cleanup (adev, queue);amdgpu_bo_unreserve (fpriv->vm.root.bo); -mutex_destroy (&queue->fence_drv_lock); free_fence_drv:amdgpu_userq_fence_driver_free (queue); free_queue:
Signature · 1 line
2.34.1
PATCH v5 3/8 drm/amdgpu: simplify return value in amdgpu_userq_get_doorbell_index
In reply to Sunil Khatriamdgpu_userq_get_doorbell_index returns a uint64 type index as well as a int type failure values. Simplifying this and using a int type return value and getting the index in input pointer of type uint64 type.
Also since it's used at once place making it static would be better.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 19 ++++++++++--------- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 4 ---- 2 files changed, 10 insertions(+), 13 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +10 -9
@@ -536,12 +536,13 @@ void amdgpu_userq_destroy_object(struct amdgpu_userq_mgr *uq_mgr,amdgpu_bo_unref (&userq_obj->obj); } -uint64_t +static int amdgpu_userq_get_doorbell_index (struct amdgpu_userq_mgr *uq_mgr,struct amdgpu_db_info *db_info, -struct drm_file *filp) +struct drm_file *filp, +uint64_t *index) { -uint64_t index; +uint64_t doorbell_index;struct drm_gem_object *gobj;struct amdgpu_userq_obj *db_obj = db_info->db_obj;int r, db_size;
@@ -588,12 +589,13 @@ amdgpu_userq_get_doorbell_index(struct amdgpu_userq_mgr *uq_mgr, goto unpin_bo; } - index =amdgpu_doorbell_index_on_bar (uq_mgr->adev, db_obj->obj, + doorbell_index =amdgpu_doorbell_index_on_bar (uq_mgr->adev, db_obj->obj, db_info->doorbell_offset, db_size);drm_dbg_driver (adev_to_drm (uq_mgr->adev), -"[Usermode queues] doorbell index=%lld\n" , index); +"[Usermode queues] doorbell index=%lld\n" , doorbell_index);amdgpu_bo_unreserve (db_obj->obj); -return index; + *index = doorbell_index; +return 0 ; unpin_bo:amdgpu_bo_unpin (db_obj->obj);
@@ -776,10 +778,9 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) db_info.doorbell_handle = queue->doorbell_handle; db_info.db_obj = &queue->db_obj; db_info.doorbell_offset = args->in.doorbell_offset; - index =amdgpu_userq_get_doorbell_index (uq_mgr, &db_info, filp); -if (index == (uint64_t )-EINVAL ) { + r =amdgpu_userq_get_doorbell_index (uq_mgr, &db_info, filp, &index); +if (r) {drm_file_err (uq_mgr->file,"Failed to get doorbell for queue\n" ); - r = -EINVAL ; goto clean_mapping; }
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +0 -4
@@ -163,10 +163,6 @@ void amdgpu_userq_evict(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); -uint64_t amdgpu_userq_get_doorbell_index (struct amdgpu_userq_mgr *uq_mgr, -struct amdgpu_db_info *db_info, -struct drm_file *filp); -u32 amdgpu_userq_get_supported_ip_mask (struct amdgpu_device *adev);bool amdgpu_userq_enabled (struct drm_device *dev);
Signature · 1 line
2.34.1
PATCH v5 4/8 drm/amdgpu/userq: clean up wptr_obj along with mqd_destroy
In reply to Sunil KhatriDuring queue creation failure, when we clean up mqd via mqd_destroy we arent doing the wptr_obj cleanup and hence adding that clean up.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
@@ -835,6 +835,10 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) erase_doorbell:xa_erase_irq (&adev->userq_doorbell_xa, index); clean_mqd: +amdgpu_bo_reserve (queue->wptr_obj.obj, true); +amdgpu_bo_unpin (queue->wptr_obj.obj); +amdgpu_bo_unreserve (queue->wptr_obj.obj); +amdgpu_bo_unref (&queue->wptr_obj.obj); uq_funcs->mqd_destroy (queue); clean_doorbell_bo:amdgpu_bo_reserve (queue->db_obj.obj, true);
Signature · 1 line
2.34.1
PATCH v5 5/8 drm/amdgpu/userq: add amdgpu_bo_unpin when amdgpu_ttm_alloc_gart fails
In reply to Sunil KhatriUnping the wptr_obj->obj when amdgpu_ttm_alloc_gart fails.
--- drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +3 -1
@@ -81,7 +81,7 @@ mes_userq_create_wptr_mapping(struct amdgpu_device *adev, ret =amdgpu_ttm_alloc_gart (&wptr_obj->obj->tbo);if (ret) {DRM_ERROR ("Failed to bind bo to GART. ret %d\n" , ret); - goto fail_map; + goto fail_alloc_gart; } queue->wptr_obj.gpu_addr =amdgpu_bo_gpu_offset (wptr_obj->obj);
@@ -89,6 +89,8 @@ mes_userq_create_wptr_mapping(struct amdgpu_device *adev,drm_exec_fini (&exec);return 0 ; +fail_alloc_gart: +amdgpu_bo_unpin (wptr_obj->obj); fail_map:amdgpu_bo_unref (&wptr_obj->obj); fail_lock:
Signature · 1 line
2.34.1
PATCH v5 6/8 drm/amdgpu/userq: reserve root bo without interruption
In reply to Sunil KhatriFix the code to make it an uninterruptible reservation for root bo.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -5
@@ -620,11 +620,7 @@ amdgpu_userq_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_que/* Cancel any pending hang detection work and cleanup */ cancel_delayed_work_sync (&queue->hang_detect_work); - r =amdgpu_bo_reserve (vm->root.bo, false); -if (r) { -drm_file_err (uq_mgr->file,"Failed to reserve root bo during userqueue destroy\n" ); -return r; - } +amdgpu_bo_reserve (vm->root.bo, true);amdgpu_userq_buffer_vas_list_cleanup (adev, queue);amdgpu_bo_unreserve (vm->root.bo);
Signature · 1 line
2.34.1
PATCH v5 7/8 drm/amdgpu/userq: make sure queue is valid in the hang_detect_work
In reply to Sunil KhatriThread 1: Running amdgpu_userq_destroy which eventually remove the queue from door bell and set userq_mgr = NULL.
Thread2: An interrupt might have scheduled the hang_detect_work which still need userq_mgr to be valid but could get an NULL ptrs.
To fix that make sure we cancel the hang_detect_work again before setting userq_mgr to NULL.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -1
@@ -427,7 +427,6 @@ static void amdgpu_userq_cleanup(struct amdgpu_usermode_queue *queue)xa_erase_irq (&adev->userq_doorbell_xa, queue->doorbell_index);amdgpu_userq_fence_driver_free (queue); queue->fence_drv =NULL ; - queue->userq_mgr =NULL ;list_del (&queue->userq_va_list);up_read (&adev->reset_domain->sem);
@@ -635,6 +634,10 @@ amdgpu_userq_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_queamdgpu_userq_cleanup (queue);mutex_unlock (&uq_mgr->userq_mutex); +/* This is case an interrupt was fired and a hang detection work is pending */ +cancel_delayed_work_sync (&queue->hang_detect_work); + queue->userq_mgr =NULL ; +amdgpu_bo_reserve (queue->db_obj.obj, true);amdgpu_bo_unpin (queue->db_obj.obj);amdgpu_bo_unreserve (queue->db_obj.obj);
Signature · 1 line
2.34.1
PATCH v5 8/8 drm/amdgpu/userq: user array to store userq vas
In reply to Sunil KhatriAdd per queue array to store userq vas and keep
size to accommodate vas of all types of queues
i.e gfx, compute and sdma.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 36 ++++++++--------------- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h | 4 ++- 2 files changed, 16 insertions(+), 24 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +13 -23
@@ -218,18 +218,11 @@ void amdgpu_userq_process_fence_irq(struct amdgpu_device *adev, u32 doorbell)static int amdgpu_userq_buffer_va_list_add (struct amdgpu_usermode_queue *queue,struct amdgpu_bo_va_mapping *va_map,u64 addr) { -struct amdgpu_userq_va_cursor *va_cursor; -struct userq_va_list ; - - va_cursor =kzalloc (sizeof (*va_cursor),GFP_KERNEL ); -if (!va_cursor) +if (queue->userq_va_count >=ARRAY_SIZE (queue->userq_va))return -ENOMEM ; -INIT_LIST_HEAD (&va_cursor->list); - va_cursor->gpu_addr = addr; + queue->userq_va[queue->userq_va_count++] = addr; va_map->bo_va->userq_va_mapped = true; -list_add (&va_cursor->list, &queue->userq_va_list); -return 0 ; }
@@ -284,14 +277,13 @@ static bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm *vm, u64 addr)static bool amdgpu_userq_buffer_vas_mapped (struct amdgpu_usermode_queue *queue) { -struct amdgpu_userq_va_cursor *va_cursor, *tmp; -int r =0 ; +int i, r =0 ; -list_for_each_entry_safe (va_cursor, tmp, &queue->userq_va_list, list) { - r +=amdgpu_userq_buffer_va_mapped (queue->vm, va_cursor->gpu_addr); +for (i =0 ; i < queue->userq_va_count; i++) { + r +=amdgpu_userq_buffer_va_mapped (queue->vm, queue->userq_va[i]);dev_dbg (queue->userq_mgr->adev->dev,"validate the userq mapping:%p va:%llx r:%d\n" , - queue, va_cursor->gpu_addr, r); + queue, queue->userq_va[i], r); }if (r !=0 )
@@ -303,19 +295,19 @@ static bool amdgpu_userq_buffer_vas_mapped(struct amdgpu_usermode_queue *queue)static void amdgpu_userq_buffer_vas_list_cleanup (struct amdgpu_device *adev,struct amdgpu_usermode_queue *queue) { -struct amdgpu_userq_va_cursor *va_cursor, *tmp;struct amdgpu_bo_va_mapping *mapping; +int i;/* Caller must hold vm->root.bo reservation */ dma_resv_assert_held (queue->vm->root.bo->tbo.base.resv); -list_for_each_entry_safe (va_cursor, tmp, &queue->userq_va_list, list) { - mapping =amdgpu_vm_bo_lookup_mapping (queue->vm, va_cursor->gpu_addr); -if (mapping) +for (i =0 ; i < queue->userq_va_count; i++) { + mapping =amdgpu_vm_bo_lookup_mapping (queue->vm, queue->userq_va[i]); +if (mapping) { + mapping->bo_va->userq_va_mapped = false;dev_dbg (adev->dev,"delete the userq:%p va:%llx\n" , - queue, va_cursor->gpu_addr); -list_del (&va_cursor->list); -kfree (va_cursor); + queue, queue->userq_va[i]); + } } }
@@ -427,7 +419,6 @@ static void amdgpu_userq_cleanup(struct amdgpu_usermode_queue *queue)xa_erase_irq (&adev->userq_doorbell_xa, queue->doorbell_index);amdgpu_userq_fence_driver_free (queue); queue->fence_drv =NULL ; -list_del (&queue->userq_va_list);up_read (&adev->reset_domain->sem); }
@@ -740,7 +731,6 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) }kref_init (&queue->refcount); -INIT_LIST_HEAD (&queue->userq_va_list); queue->doorbell_handle = args->in.doorbell_handle; queue->queue_type = args->in.ip_type; queue->vm = &fpriv->vm;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +3 -1
@@ -93,7 +93,9 @@ struct amdgpu_usermode_queue {struct delayed_work hang_detect_work;struct kref refcount; -struct list_head userq_va_list; +/* User to store core bo's va addresses */ +u64 userq_va[5 ]; +int userq_va_count; };struct amdgpu_userq_funcs {
Signature · 1 line
2.34.1