drm/amdgpu/userq: avoid uneccessary locking in amdgpu_userq_create
Subsystems (2)
PATCH 1/2 drm/amdgpu/userq: avoid uneccessary locking in amdgpu_userq_create
Reorganise code to avoid holding mutex userq_mutex while also trying to grab exec lock ww_mutex where its not needed for function amdgpu_userq_input_va_validate
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 33 +++++++++++------------ 1 file changed, 15 insertions(+), 18 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +15 -18
@@ -737,28 +737,17 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)return r; } -/* -* There could be a situation that we are creating a new queue while -* the other queues under this UQ_mgr are suspended. So if there is any -* resume work pending, wait for it to get done. -* -* This will also make sure we have a valid eviction fence ready to be used. -*/ -amdgpu_userq_ensure_ev_fence (&fpriv->userq_mgr, &fpriv->evf_mgr); - uq_funcs = adev->userq_funcs[args->in.ip_type];if (!uq_funcs) {drm_file_err (uq_mgr->file,"Usermode queue is not supported for this IP (%u)\n" , args->in.ip_type); - r = -EINVAL ; - goto unlock; +return -EINVAL ; } queue =kzalloc (sizeof (struct amdgpu_usermode_queue ),GFP_KERNEL );if (!queue) {drm_file_err (uq_mgr->file,"Failed to allocate memory for queue\n" ); - r = -ENOMEM ; - goto unlock; +return -ENOMEM ; }INIT_LIST_HEAD (&queue->userq_va_list);
@@ -781,12 +770,21 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) goto free_queue; } +/* +* There could be a situation that we are creating a new queue while +* the other queues under this UQ_mgr are suspended. So if there is any +* resume work pending, wait for it to get done. +* +* This will also make sure we have a valid eviction fence ready to be used. +*/ +amdgpu_userq_ensure_ev_fence (&fpriv->userq_mgr, &fpriv->evf_mgr); +/* 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" ); r = -EINVAL ; - goto free_queue; + goto unlock; } queue->doorbell_index = index;
@@ -794,7 +792,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) r =amdgpu_userq_fence_driver_alloc (adev, &queue->fence_drv);if (r) {drm_file_err (uq_mgr->file,"Failed to alloc fence driver\n" ); - goto free_queue; + goto unlock; } r = uq_funcs->mqd_create (queue, &args->in);
@@ -858,11 +856,10 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)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); - +free_queue: +kfree (queue);return r; }
Signature · 1 line
2.34.1
PATCH 2/2 drm/amdgpu/userq: clean the VA mapping list for failed queue creation
If the queue creation failed during mapping of the important VA's like queue_va, rptr_va and wptr_va. These needs to be cleaned as queue destroy will not be called for such queues as user never get call to creation failure.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3 -2
@@ -767,7 +767,7 @@ 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 ; - goto free_queue; + goto clean_mapping; }/*
@@ -858,7 +858,8 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)amdgpu_userq_fence_driver_free (queue); unlock:mutex_unlock (&uq_mgr->userq_mutex); -free_queue: +clean_mapping: +amdgpu_userq_buffer_vas_list_cleanup (adev, queue);kfree (queue);return r; }
Signature · 1 line
2.34.1
Christian König
maintainerQuoted 46 lines · Sunil Khatri
Reorganise code to avoid holding mutex
userq_mutexwhile also trying to grab exec lockww_mutexwhere its not needed for functionamdgpu_userq_input_va_validateSigned-off-by Sunil Khatri <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 33 +++++++++++------------ 1 file changed, 15 insertions(+), 18 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-13@@ -737,28 +737,17 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)return r; } -/* -* There could be a situation that we are creating a new queue while -* the other queues under this UQ_mgr are suspended. So if there is any -* resume work pending, wait for it to get done. -* -* This will also make sure we have a valid eviction fence ready to be used. -*/ -amdgpu_userq_ensure_ev_fence (&fpriv->userq_mgr, &fpriv->evf_mgr); - uq_funcs = adev->userq_funcs[args->in.ip_type];if (!uq_funcs) {drm_file_err (uq_mgr->file,"Usermode queue is not supported for this IP (%u)\n" , args->in.ip_type); - r = -EINVAL ; - goto unlock; +return -EINVAL ; } queue =kzalloc (sizeof (struct amdgpu_usermode_queue ),GFP_KERNEL );if (!queue) {drm_file_err (uq_mgr->file,"Failed to allocate memory for queue\n" ); - r = -ENOMEM ; - goto unlock; +return -ENOMEM ; }INIT_LIST_HEAD (&queue->userq_va_list);@@ -781,12 +770,21 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) goto free_queue;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) · line 780
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +8
-0} +/* +* There could be a situation that we are creating a new queue while +* the other queues under this UQ_mgr are suspended. So if there is any +* resume work pending, wait for it to get done. +* +* This will also make sure we have a valid eviction fence ready to be used. +*/ +amdgpu_userq_ensure_ev_fence (&fpriv->userq_mgr, &fpriv->evf_mgr);
Even that position is not correct. After grabbing the userq_mutex we can't allocate memory any more.
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) · line 792
Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-1+/* 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" ); r = -EINVAL ; - goto free_queue; + goto unlock; } queue->doorbell_index = index;@@ -794,7 +792,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) r =amdgpu_userq_fence_driver_alloc (adev, &queue->fence_drv);
So doing that here is also forbidden.
Regards,
Christian.
Quoted 21 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3
-4if (r) {drm_file_err (uq_mgr->file,"Failed to alloc fence driver\n" ); - goto free_queue; + goto unlock; } r = uq_funcs->mqd_create (queue, &args->in);@@ -858,11 +856,10 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)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); - +free_queue: +kfree (queue);return r; }
Quoted 69 lines · Christian König
Quoted 46 lines · Sunil Khatri
Reorganise code to avoid holding mutex
userq_mutexwhile also trying to grab exec lockww_mutexwhere its not needed for functionamdgpu_userq_input_va_validateSigned-off-by Sunil Khatri<[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 33 +++++++++++------------ 1 file changed, 15 insertions(+), 18 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-13@@ -737,28 +737,17 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)return r; } -/* -* There could be a situation that we are creating a new queue while -* the other queues under this UQ_mgr are suspended. So if there is any -* resume work pending, wait for it to get done. -* -* This will also make sure we have a valid eviction fence ready to be used. -*/ -amdgpu_userq_ensure_ev_fence (&fpriv->userq_mgr, &fpriv->evf_mgr); - uq_funcs = adev->userq_funcs[args->in.ip_type];if (!uq_funcs) {drm_file_err (uq_mgr->file,"Usermode queue is not supported for this IP (%u)\n" , args->in.ip_type); - r = -EINVAL ; - goto unlock; +return -EINVAL ; } queue =kzalloc (sizeof (struct amdgpu_usermode_queue ),GFP_KERNEL );if (!queue) {drm_file_err (uq_mgr->file,"Failed to allocate memory for queue\n" ); - r = -ENOMEM ; - goto unlock; +return -ENOMEM ; }INIT_LIST_HEAD (&queue->userq_va_list);@@ -781,12 +770,21 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) goto free_queue;drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) · line 780
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +8
-0} +/* +* There could be a situation that we are creating a new queue while +* the other queues under this UQ_mgr are suspended. So if there is any +* resume work pending, wait for it to get done. +* +* This will also make sure we have a valid eviction fence ready to be used. +*/ +amdgpu_userq_ensure_ev_fence (&fpriv->userq_mgr, &fpriv->evf_mgr);Even that position is not correct. After grabbing the
userq_mutexwe can't allocate memory any more.Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-1+/* 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" ); r = -EINVAL ; - goto free_queue; + goto unlock; }
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) · line 792
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-0queue->doorbell_index = index;@@ -794,7 +792,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) r =amdgpu_userq_fence_driver_alloc (adev, &queue->fence_drv);So doing that here is also forbidden.
If i am not wrong we could move this whole code including amdgpu_userq_fence_driver_alloc and above out of mutex. Does that sounds correct ? Regards Sunil khatri
Quoted 25 lines
Regards,
Christian.Quoted 21 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3
-4if (r) {drm_file_err (uq_mgr->file,"Failed to alloc fence driver\n" ); - goto free_queue; + goto unlock; } r = uq_funcs->mqd_create (queue, &args->in);@@ -858,11 +856,10 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)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); - +free_queue: +kfree (queue);return r; }
Christian König
maintainerQuoted 71 lines · Sunil
Quoted 69 lines · Christian König
Quoted 46 lines · Sunil Khatri
Reorganise code to avoid holding mutex
userq_mutexwhile also trying to grab exec lockww_mutexwhere its not needed for functionamdgpu_userq_input_va_validateSigned-off-by Sunil Khatri <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 33 +++++++++++------------ 1 file changed, 15 insertions(+), 18 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-13@@ -737,28 +737,17 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)return r; } -/* -* There could be a situation that we are creating a new queue while -* the other queues under this UQ_mgr are suspended. So if there is any -* resume work pending, wait for it to get done. -* -* This will also make sure we have a valid eviction fence ready to be used. -*/ -amdgpu_userq_ensure_ev_fence (&fpriv->userq_mgr, &fpriv->evf_mgr); - uq_funcs = adev->userq_funcs[args->in.ip_type];if (!uq_funcs) {drm_file_err (uq_mgr->file,"Usermode queue is not supported for this IP (%u)\n" , args->in.ip_type); - r = -EINVAL ; - goto unlock; +return -EINVAL ; } queue =kzalloc (sizeof (struct amdgpu_usermode_queue ),GFP_KERNEL );if (!queue) {drm_file_err (uq_mgr->file,"Failed to allocate memory for queue\n" ); - r = -ENOMEM ; - goto unlock; +return -ENOMEM ; }INIT_LIST_HEAD (&queue->userq_va_list);@@ -781,12 +770,21 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) goto free_queue;drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) · line 780
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +8
-0} +/* +* There could be a situation that we are creating a new queue while +* the other queues under this UQ_mgr are suspended. So if there is any +* resume work pending, wait for it to get done. +* +* This will also make sure we have a valid eviction fence ready to be used. +*/ +amdgpu_userq_ensure_ev_fence (&fpriv->userq_mgr, &fpriv->evf_mgr);Even that position is not correct. After grabbing the
userq_mutexwe can't allocate memory any more.Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2
-1+/* 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" ); r = -EINVAL ; - goto free_queue; + goto unlock; }
Quoted 5 lines
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) · line 792
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0
-0queue->doorbell_index = index;@@ -794,7 +792,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) r =amdgpu_userq_fence_driver_alloc (adev, &queue->fence_drv);So doing that here is also forbidden.
If i am not wrong we could move this whole code including amdgpu_userq_fence_driver_alloc and above out of mutex. Does that sounds correct ?
At least of hand, yes. We only need to hold the mutex when finally talking to the HW to enable the new queue.
Regards,
Christian.
Regards Sunil khatri
Quoted 24 lines
Quoted 24 lines
Regards,
Christian.Quoted 21 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +3
-4if (r) {drm_file_err (uq_mgr->file,"Failed to alloc fence driver\n" ); - goto free_queue; + goto unlock; } r = uq_funcs->mqd_create (queue, &args->in);@@ -858,11 +856,10 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)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); - +free_queue: +kfree (queue);return r; }