Skip to contentSkip to conversation

Never take root bo reservation locks with

PATCH Superseded Sunil Khatri 3 participants 14 messages Apr 9

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Never take root bo reservation locks with

v: Fixes the locks issue in all the places where userq_mutex is taken and then vm->root.bo reservation lock is next.

Sunil Khatri (4):
drm/amdgpu/userq: dont look root bo with userq_mutex held drm/amdgpu/userq: create_mqd does not need userq_mutex drm/amdgpu/userq: caller to take reserv lock for
amdgpu_userq_buffer_vas_list_cleanup
drm/amdgpu/userq: hold reservation lock in caller of
amdgpu_userq_input_va_validate

 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  | 77 +++++++++++++---------
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 23 +++++++
 2 files changed, 69 insertions(+), 31 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/4 drm/amdgpu/userq: dont look root bo with userq_mutex held

Donot hold reservation lock for root bo if userq_mutex is already held in the call flow this cause a lock issue with ttm_bo_delayed_delete.

Its better to lock the vm->root.bo first and then go ahead with userq_mutex so userq_mutex threads dont get stuck until the reservation lock is held.

In this case it helps in the function amdgpu_userq_buffer_vas_mapped for each queue during restore_all.

---
 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
index 9f7a08a6b018..cf8c8dfde721 100644
@@ -270,15 +270,13 @@ static bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm *vm, u64 addr)
 	struct amdgpu_bo_va_mapping *mapping;
 	bool r;
 
-	if (amdgpu_bo_reserve(vm->root.bo, false))
-		return false;
+	dma_resv_assert_held(vm->root.bo->tbo.base.resv);
 
 	mapping = amdgpu_vm_bo_lookup_mapping(vm, addr);
 	if (!IS_ERR_OR_NULL(mapping) && atomic_read(&mapping->bo_va->userq_va_mapped))
 		r = true;
 	else
 		r = false;
-	amdgpu_bo_unreserve(vm->root.bo);
 
 	return r;
 }
@@ -991,10 +989,16 @@ int amdgpu_userq_ioctl(struct drm_device *dev, void *data,
 static int
 amdgpu_userq_restore_all(struct amdgpu_userq_mgr *uq_mgr)
 {
+	struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr);
+	struct amdgpu_vm *vm = &fpriv->vm;
 	struct amdgpu_usermode_queue *queue;
 	unsigned long queue_id;
 	int ret = 0, r;
 
+
+	if (amdgpu_bo_reserve(vm->root.bo, false))
+		return false;
+
 	mutex_lock(&uq_mgr->userq_mutex);
 	/* Resume all the queues for this process */
 	xa_for_each(&uq_mgr->userq_xa, queue_id, queue) {
@@ -1012,6 +1016,7 @@ amdgpu_userq_restore_all(struct amdgpu_userq_mgr *uq_mgr)
 
 	}
 	mutex_unlock(&uq_mgr->userq_mutex);
+	amdgpu_bo_unreserve(vm->root.bo);
 
 	if (ret)
 		drm_file_err(uq_mgr->file,
Signature · 1 line
2.34.1
maintainer Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH v1 1/4] drm/amdgpu/userq: dont look root bo with userq_mutex held

Quoted 4 lines · Sunil Khatri

Donot hold reservation lock for root bo if userq_mutex is already held in the call flow this cause a lock issue with ttm_bo_delayed_delete.

Quoted 8 lines

Its better to lock the vm->root.bo first and then go ahead with userq_mutex so userq_mutex threads dont get stuck until the reservation lock is held.

In this case it helps in the function amdgpu_userq_buffer_vas_mapped for each queue during restore_all.

Quoted 50 lines
---
 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
index 9f7a08a6b018..cf8c8dfde721 100644
@@ -270,15 +270,13 @@ static bool amdgpu_userq_buffer_va_mapped(struct amdgpu_vm *vm, u64 addr)
 	struct amdgpu_bo_va_mapping *mapping;
 	bool r;
 
-	if (amdgpu_bo_reserve(vm->root.bo, false))
-		return false;
+	dma_resv_assert_held(vm->root.bo->tbo.base.resv);
 
 	mapping = amdgpu_vm_bo_lookup_mapping(vm, addr);
 	if (!IS_ERR_OR_NULL(mapping) && atomic_read(&mapping->bo_va->userq_va_mapped))
 		r = true;
 	else
 		r = false;
-	amdgpu_bo_unreserve(vm->root.bo);
 
 	return r;
 }
@@ -991,10 +989,16 @@ int amdgpu_userq_ioctl(struct drm_device *dev, void *data,
 static int
 amdgpu_userq_restore_all(struct amdgpu_userq_mgr *uq_mgr)
 {
+	struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr);
+	struct amdgpu_vm *vm = &fpriv->vm;
 	struct amdgpu_usermode_queue *queue;
 	unsigned long queue_id;
 	int ret = 0, r;
 
+
+	if (amdgpu_bo_reserve(vm->root.bo, false))
+		return false;
+
 	mutex_lock(&uq_mgr->userq_mutex);
 	/* Resume all the queues for this process */
 	xa_for_each(&uq_mgr->userq_xa, queue_id, queue) {
@@ -1012,6 +1016,7 @@ amdgpu_userq_restore_all(struct amdgpu_userq_mgr *uq_mgr)
 
 	}
 	mutex_unlock(&uq_mgr->userq_mutex);
+	amdgpu_bo_unreserve(vm->root.bo);
 
 	if (ret)
 		drm_file_err(uq_mgr->file,
#3 lore raw

PATCH 2/4 drm/amdgpu/userq: create_mqd does not need userq_mutex

In reply to Sunil Khatri

Reshuffle the code to run create_mqd outside the mutex. code here is mostly setting up software structure init before actually registering the userqueue in the xa and to the driver.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +7 -9
index cf8c8dfde721..2408f888c4d9 100644
@@ -793,14 +793,14 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 		goto clean_mapping;
 	}
 
-	amdgpu_userq_ensure_ev_fence(&fpriv->userq_mgr, &fpriv->evf_mgr);
-
 	r = uq_funcs->mqd_create(queue, &args->in);
 	if (r) {
 		drm_file_err(uq_mgr->file, "Failed to create Queue\n");
 		goto clean_fence_driver;
 	}
 
+	amdgpu_userq_ensure_ev_fence(&fpriv->userq_mgr, &fpriv->evf_mgr);
+
 	/* don't map the queue if scheduling is halted */
 	if (adev->userq_halt_for_enforce_isolation &&
 	    ((queue->queue_type == AMDGPU_HW_IP_GFX) ||
@@ -812,7 +812,6 @@ 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");
-			down_read(&adev->reset_domain->sem);
 			goto clean_mqd;
 		}
 	}
@@ -828,9 +827,8 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 	if (r) {
 		if (!skip_map_queue)
 			amdgpu_userq_unmap_helper(queue);
-
 		r = -ENOMEM;
-		goto clean_mqd;
+		goto clean_reset_domain;
 	}
 
 	r = xa_err(xa_store_irq(&adev->userq_doorbell_xa, index, queue, GFP_KERNEL));
@@ -838,8 +836,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 		xa_erase(&uq_mgr->userq_xa, qid);
 		if (!skip_map_queue)
 			amdgpu_userq_unmap_helper(queue);
-
-		goto clean_mqd;
+		goto clean_reset_domain;
 	}
 	up_read(&adev->reset_domain->sem);
 
@@ -851,12 +848,13 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 	mutex_unlock(&uq_mgr->userq_mutex);
 	return 0;
 
+clean_reset_domain:
+	up_read(&adev->reset_domain->sem);
 clean_mqd:
+	mutex_unlock(&uq_mgr->userq_mutex);
 	uq_funcs->mqd_destroy(queue);
-	up_read(&adev->reset_domain->sem);
 clean_fence_driver:
 	amdgpu_userq_fence_driver_free(queue);
-	mutex_unlock(&uq_mgr->userq_mutex);
 clean_mapping:
 	amdgpu_userq_buffer_vas_list_cleanup(adev, queue);
 	kfree(queue);
Signature · 1 line
2.34.1
maintainer
#4 lore raw

Re: [PATCH v1 2/4] drm/amdgpu/userq: create_mqd does not need userq_mutex

Quoted 18 lines · Sunil Khatri

Reshuffle the code to run create_mqd outside the mutex. code here is mostly setting up software structure init before actually registering the userqueue in the xa and to the driver.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index cf8c8dfde721..2408f888c4d9 100644
@@ -793,14 +793,14 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 		goto clean_mapping;
 	}
 

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) · line 803

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -2
-	amdgpu_userq_ensure_ev_fence(&fpriv->userq_mgr, &fpriv->evf_mgr);
-
 	r = uq_funcs->mqd_create(queue, &args->in);
 	if (r) {
 		drm_file_err(uq_mgr->file, "Failed to create Queue\n");
 		goto clean_fence_driver;
 	}
 
+	amdgpu_userq_ensure_ev_fence(&fpriv->userq_mgr, &fpriv->evf_mgr);
+

Mhm while this might work it looks a bit questionable.

What exactly is uq_funcs->mqd_create() doing? I though it would only be initializing fields.

Regards,
Christian.

Quoted 48 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -7
 	/* don't map the queue if scheduling is halted */
 	if (adev->userq_halt_for_enforce_isolation &&
 	    ((queue->queue_type == AMDGPU_HW_IP_GFX) ||
@@ -812,7 +812,6 @@ 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");
-			down_read(&adev->reset_domain->sem);
 			goto clean_mqd;
 		}
 	}
@@ -828,9 +827,8 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 	if (r) {
 		if (!skip_map_queue)
 			amdgpu_userq_unmap_helper(queue);
-
 		r = -ENOMEM;
-		goto clean_mqd;
+		goto clean_reset_domain;
 	}
 
 	r = xa_err(xa_store_irq(&adev->userq_doorbell_xa, index, queue, GFP_KERNEL));
@@ -838,8 +836,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 		xa_erase(&uq_mgr->userq_xa, qid);
 		if (!skip_map_queue)
 			amdgpu_userq_unmap_helper(queue);
-
-		goto clean_mqd;
+		goto clean_reset_domain;
 	}
 	up_read(&adev->reset_domain->sem);
 
@@ -851,12 +848,13 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 	mutex_unlock(&uq_mgr->userq_mutex);
 	return 0;
 
+clean_reset_domain:
+	up_read(&adev->reset_domain->sem);
 clean_mqd:
+	mutex_unlock(&uq_mgr->userq_mutex);
 	uq_funcs->mqd_destroy(queue);
-	up_read(&adev->reset_domain->sem);
 clean_fence_driver:
 	amdgpu_userq_fence_driver_free(queue);
-	mutex_unlock(&uq_mgr->userq_mutex);
 clean_mapping:
 	amdgpu_userq_buffer_vas_list_cleanup(adev, queue);
 	kfree(queue);
#5 lore raw

Re: [PATCH v1 2/4] drm/amdgpu/userq: create_mqd does not need userq_mutex

Quoted 27 lines · Christian König
Quoted 26 lines · Sunil Khatri

Reshuffle the code to run create_mqd outside the mutex. code here is mostly setting up software structure init before actually registering the userqueue in the xa and to the driver.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 16 +++++++---------
  1 file changed, 7 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -2
index cf8c8dfde721..2408f888c4d9 100644
@@ -793,14 +793,14 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
  		goto clean_mapping;
  	}
 
-	amdgpu_userq_ensure_ev_fence(&fpriv->userq_mgr, &fpriv->evf_mgr);
-
  	r = uq_funcs->mqd_create(queue, &args->in);
  	if (r) {
  		drm_file_err(uq_mgr->file, "Failed to create Queue\n");
  		goto clean_fence_driver;
  	}
 
Quoted 5 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) · line 803

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -0
+	amdgpu_userq_ensure_ev_fence(&fpriv->userq_mgr, &fpriv->evf_mgr);
+

Mhm while this might work it looks a bit questionable.

What exactly is uq_funcs->mqd_create() doing? I though it would only be initializing fields.

Things done here
a. Allocate objects needed for creating queue, validated the mappings of various parameters of the queue. b. Call mqd_init for various IPs: like below, which set various parameters for mqd and but still not call any fw functions.
   1. gfx_v12_0_gfx_mqd_init
   2. gfx_v12_0_compute_mqd_init

Irrespective of that, we could make this mqd_init function with userq_mutex. I think it is safe.

Regards
Sunil Khatri

Quoted 52 lines

Regards,
Christian.

Quoted 48 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -7
  	/* don't map the queue if scheduling is halted */
  	if (adev->userq_halt_for_enforce_isolation &&
  	    ((queue->queue_type == AMDGPU_HW_IP_GFX) ||
@@ -812,7 +812,6 @@ 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");
-			down_read(&adev->reset_domain->sem);
  			goto clean_mqd;
  		}
  	}
@@ -828,9 +827,8 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
  	if (r) {
  		if (!skip_map_queue)
  			amdgpu_userq_unmap_helper(queue);
-
  		r = -ENOMEM;
-		goto clean_mqd;
+		goto clean_reset_domain;
  	}
 
  	r = xa_err(xa_store_irq(&adev->userq_doorbell_xa, index, queue, GFP_KERNEL));
@@ -838,8 +836,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
  		xa_erase(&uq_mgr->userq_xa, qid);
  		if (!skip_map_queue)
  			amdgpu_userq_unmap_helper(queue);
-
-		goto clean_mqd;
+		goto clean_reset_domain;
  	}
  	up_read(&adev->reset_domain->sem);
 
@@ -851,12 +848,13 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
  	mutex_unlock(&uq_mgr->userq_mutex);
  	return 0;
 
+clean_reset_domain:
+	up_read(&adev->reset_domain->sem);
  clean_mqd:
+	mutex_unlock(&uq_mgr->userq_mutex);
  	uq_funcs->mqd_destroy(queue);
-	up_read(&adev->reset_domain->sem);
  clean_fence_driver:
  	amdgpu_userq_fence_driver_free(queue);
-	mutex_unlock(&uq_mgr->userq_mutex);
  clean_mapping:
  	amdgpu_userq_buffer_vas_list_cleanup(adev, queue);
  	kfree(queue);
maintainer
#6 lore raw

Re: [PATCH v1 2/4] drm/amdgpu/userq: create_mqd does not need userq_mutex

Quoted 31 lines · Sunil
Quoted 29 lines · Christian König
Quoted 16 lines · Sunil Khatri

Reshuffle the code to run create_mqd outside the mutex. code here is mostly setting up software structure init before actually registering the userqueue in the xa and to the driver.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 16 +++++++---------

  1 file changed, 7 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index cf8c8dfde721..2408f888c4d9 100644
@@ -793,14 +793,14 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
           goto clean_mapping;

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) · line 802

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -1
       }
   -    amdgpu_userq_ensure_ev_fence(&fpriv->userq_mgr, &fpriv->evf_mgr);
-
       r = uq_funcs->mqd_create(queue, &args->in);
       if (r) {
           drm_file_err(uq_mgr->file, "Failed to create Queue\n");
           goto clean_fence_driver;
       }
   +    amdgpu_userq_ensure_ev_fence(&fpriv->userq_mgr, &fpriv->evf_mgr);
+

Mhm while this might work it looks a bit questionable.

Quoted 8 lines
Quoted 1 line

What exactly is uq_funcs->mqd_create() doing? I though it would only be initializing fields.

Things done here
a. Allocate objects needed for creating queue, validated the mappings of various parameters of the queue. b. Call mqd_init for various IPs: like below, which set various parameters for mqd and but still not call any fw functions.
   1. gfx_v12_0_gfx_mqd_init
   2. gfx_v12_0_compute_mqd_init

Irrespective of that, we could make this mqd_init function with userq_mutex. I think it is safe.

When it allocates memory it is not safe to call this function while holding the userq_mutex lock. All memory allocations must be done outside of that lock, with the only exception being the resume path for the eviction fence.

So as long as the function doesn't talk to the FW the patch is Reviewed-by: Christian König <[email protected]>.

Regards,
Christian.

Quoted 52 lines

Regards
Sunil Khatri

Quoted 49 lines

Regards,
Christian.

Quoted 45 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
       /* don't map the queue if scheduling is halted */

      if (adev->userq_halt_for_enforce_isolation && ((queue->queue_type == AMDGPU_HW_IP_GFX) ||

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -5
@@ -812,7 +812,6 @@ 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");
-            down_read(&adev->reset_domain->sem);
               goto clean_mqd;
           }
       }
@@ -828,9 +827,8 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
       if (r) {
           if (!skip_map_queue)
               amdgpu_userq_unmap_helper(queue);
-
           r = -ENOMEM;
-        goto clean_mqd;
+        goto clean_reset_domain;
       }
         r = xa_err(xa_store_irq(&adev->userq_doorbell_xa, index, queue, GFP_KERNEL));
@@ -838,8 +836,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
           xa_erase(&uq_mgr->userq_xa, qid);
           if (!skip_map_queue)
               amdgpu_userq_unmap_helper(queue);
-
-        goto clean_mqd;
+        goto clean_reset_domain;
       }
       up_read(&adev->reset_domain->sem);
   @@ -851,12 +848,13 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)

      mutex_unlock(&uq_mgr->userq_mutex); return 0;
  +clean_reset_domain:
+    up_read(&adev->reset_domain->sem); clean_mqd:
+    mutex_unlock(&uq_mgr->userq_mutex); uq_funcs->mqd_destroy(queue);
-    up_read(&adev->reset_domain->sem); clean_fence_driver:
      amdgpu_userq_fence_driver_free(queue);
-    mutex_unlock(&uq_mgr->userq_mutex); clean_mapping:
      amdgpu_userq_buffer_vas_list_cleanup(adev, queue); kfree(queue);

#7 lore raw

PATCH 3/4 drm/amdgpu/userq: caller to take reserv lock for amdgpu_userq_buffer_vas_list_cleanup

In reply to Sunil Khatri

Remove the reservation lock for vm from amdgpu_userq_buffer_vas_list_cleanup and caller should make sure it's taken before locking userq_mutex.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 34 ++++++++++++++---------
 1 file changed, 21 insertions(+), 13 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +21 -13
index 2408f888c4d9..1b81ce49d408 100644
@@ -312,25 +312,21 @@ static int amdgpu_userq_buffer_vas_list_cleanup(struct amdgpu_device *adev,
 {
 	struct amdgpu_userq_va_cursor *va_cursor, *tmp;
 	struct amdgpu_bo_va_mapping *mapping;
-	int r;
 
-	r = amdgpu_bo_reserve(queue->vm->root.bo, false);
-	if (r)
-		return r;
+	/* 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) {
-			r = -EINVAL;
-			goto err;
+			return -EINVAL;
 		}
 		dev_dbg(adev->dev, "delete the userq:%p va:%llx\n",
 			queue, va_cursor->gpu_addr);
 		amdgpu_userq_buffer_va_list_del(mapping, va_cursor);
 	}
-err:
-	amdgpu_bo_unreserve(queue->vm->root.bo);
-	return r;
+
+	return 0;
 }
 
 static int amdgpu_userq_preempt_helper(struct amdgpu_usermode_queue *queue)
@@ -444,8 +440,6 @@ static void amdgpu_userq_cleanup(struct amdgpu_usermode_queue *queue)
 	/* Wait for mode-1 reset to complete */
 	down_read(&adev->reset_domain->sem);
 
-	/* Drop the userq reference. */
-	amdgpu_userq_buffer_vas_list_cleanup(adev, queue);
 	uq_funcs->mqd_destroy(queue);
 	/* Use interrupt-safe locking since IRQ handlers may access these XArrays */
 	xa_erase_irq(&adev->userq_doorbell_xa, queue->doorbell_index);
@@ -626,6 +620,9 @@ static int
 amdgpu_userq_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_queue *queue)
 {
 	struct amdgpu_device *adev = uq_mgr->adev;
+	struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr);
+	struct amdgpu_vm *vm = &fpriv->vm;
+
 	int r = 0;
 
 	cancel_delayed_work_sync(&uq_mgr->resume_work);
@@ -633,6 +630,14 @@ 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_userq_buffer_vas_list_cleanup(adev, queue);
+	amdgpu_bo_unreserve(vm->root.bo);
+
 	mutex_lock(&uq_mgr->userq_mutex);
 	queue->hang_detect_fence = NULL;
 	amdgpu_userq_wait_for_last_fence(queue);
@@ -664,7 +669,6 @@ amdgpu_userq_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_que
 	}
 	amdgpu_userq_cleanup(queue);
 	mutex_unlock(&uq_mgr->userq_mutex);
-
 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
 
 	return r;
@@ -856,7 +860,11 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 clean_fence_driver:
 	amdgpu_userq_fence_driver_free(queue);
 clean_mapping:
-	amdgpu_userq_buffer_vas_list_cleanup(adev, queue);
+	if (!amdgpu_bo_reserve(fpriv->vm.root.bo, false)) {
+		amdgpu_userq_buffer_vas_list_cleanup(adev, queue);
+		amdgpu_bo_unreserve(fpriv->vm.root.bo);
+	}
+
 	kfree(queue);
 	return r;
 }
Signature · 1 line
2.34.1
maintainer
#8 lore raw

Re: [PATCH v1 3/4] drm/amdgpu/userq: caller to take reserv lock for amdgpu_userq_buffer_vas_list_cleanup

Quoted 86 lines · Sunil Khatri

Remove the reservation lock for vm from amdgpu_userq_buffer_vas_list_cleanup and caller should make sure it's taken before locking userq_mutex.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 34 ++++++++++++++---------
 1 file changed, 21 insertions(+), 13 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +16 -12
index 2408f888c4d9..1b81ce49d408 100644
@@ -312,25 +312,21 @@ static int amdgpu_userq_buffer_vas_list_cleanup(struct amdgpu_device *adev,
 {
 	struct amdgpu_userq_va_cursor *va_cursor, *tmp;
 	struct amdgpu_bo_va_mapping *mapping;
-	int r;
 
-	r = amdgpu_bo_reserve(queue->vm->root.bo, false);
-	if (r)
-		return r;
+	/* 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) {
-			r = -EINVAL;
-			goto err;
+			return -EINVAL;
 		}
 		dev_dbg(adev->dev, "delete the userq:%p va:%llx\n",
 			queue, va_cursor->gpu_addr);
 		amdgpu_userq_buffer_va_list_del(mapping, va_cursor);
 	}
-err:
-	amdgpu_bo_unreserve(queue->vm->root.bo);
-	return r;
+
+	return 0;
 }
 
 static int amdgpu_userq_preempt_helper(struct amdgpu_usermode_queue *queue)
@@ -444,8 +440,6 @@ static void amdgpu_userq_cleanup(struct amdgpu_usermode_queue *queue)
 	/* Wait for mode-1 reset to complete */
 	down_read(&adev->reset_domain->sem);
 
-	/* Drop the userq reference. */
-	amdgpu_userq_buffer_vas_list_cleanup(adev, queue);
 	uq_funcs->mqd_destroy(queue);
 	/* Use interrupt-safe locking since IRQ handlers may access these XArrays */
 	xa_erase_irq(&adev->userq_doorbell_xa, queue->doorbell_index);
@@ -626,6 +620,9 @@ static int
 amdgpu_userq_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_queue *queue)
 {
 	struct amdgpu_device *adev = uq_mgr->adev;
+	struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr);
+	struct amdgpu_vm *vm = &fpriv->vm;
+
 	int r = 0;
 
 	cancel_delayed_work_sync(&uq_mgr->resume_work);
@@ -633,6 +630,14 @@ 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_userq_buffer_vas_list_cleanup(adev, queue);
+	amdgpu_bo_unreserve(vm->root.bo);
+
 	mutex_lock(&uq_mgr->userq_mutex);
 	queue->hang_detect_fence = NULL;
 	amdgpu_userq_wait_for_last_fence(queue);
@@ -664,7 +669,6 @@ amdgpu_userq_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_que
 	}
 	amdgpu_userq_cleanup(queue);
 	mutex_unlock(&uq_mgr->userq_mutex);
-
 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
 
 	return r;

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) · line 863

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -1
@@ -856,7 +860,11 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 clean_fence_driver:
 	amdgpu_userq_fence_driver_free(queue);
 clean_mapping:
-	amdgpu_userq_buffer_vas_list_cleanup(adev, queue);
+	if (!amdgpu_bo_reserve(fpriv->vm.root.bo, false)) {

The second parameter should be true here, and then you actually don't need to check the return value.

Regards,
Christian.

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
+		amdgpu_userq_buffer_vas_list_cleanup(adev, queue);
+		amdgpu_bo_unreserve(fpriv->vm.root.bo);
+	}
+
 	kfree(queue);
 	return r;
 }
#9 lore raw

Re: [PATCH v1 3/4] drm/amdgpu/userq: caller to take reserv lock for amdgpu_userq_buffer_vas_list_cleanup

Quoted 86 lines · Christian König
Quoted 84 lines · Sunil Khatri

Remove the reservation lock for vm from amdgpu_userq_buffer_vas_list_cleanup and caller should make sure it's taken before locking userq_mutex.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 34 ++++++++++++++---------
  1 file changed, 21 insertions(+), 13 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +16 -12
index 2408f888c4d9..1b81ce49d408 100644
@@ -312,25 +312,21 @@ static int amdgpu_userq_buffer_vas_list_cleanup(struct amdgpu_device *adev,
  {
  	struct amdgpu_userq_va_cursor *va_cursor, *tmp;
  	struct amdgpu_bo_va_mapping *mapping;
-	int r;
 
-	r = amdgpu_bo_reserve(queue->vm->root.bo, false);
-	if (r)
-		return r;
+	/* 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) {
-			r = -EINVAL;
-			goto err;
+			return -EINVAL;
  		}
  		dev_dbg(adev->dev, "delete the userq:%p va:%llx\n",
  			queue, va_cursor->gpu_addr);
  		amdgpu_userq_buffer_va_list_del(mapping, va_cursor);
  	}
-err:
-	amdgpu_bo_unreserve(queue->vm->root.bo);
-	return r;
+
+	return 0;
  }
 
  static int amdgpu_userq_preempt_helper(struct amdgpu_usermode_queue *queue)
@@ -444,8 +440,6 @@ static void amdgpu_userq_cleanup(struct amdgpu_usermode_queue *queue)
  	/* Wait for mode-1 reset to complete */
  	down_read(&adev->reset_domain->sem);
 
-	/* Drop the userq reference. */
-	amdgpu_userq_buffer_vas_list_cleanup(adev, queue);
  	uq_funcs->mqd_destroy(queue);
  	/* Use interrupt-safe locking since IRQ handlers may access these XArrays */
  	xa_erase_irq(&adev->userq_doorbell_xa, queue->doorbell_index);
@@ -626,6 +620,9 @@ static int
  amdgpu_userq_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_queue *queue)
  {
  	struct amdgpu_device *adev = uq_mgr->adev;
+	struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr);
+	struct amdgpu_vm *vm = &fpriv->vm;
+
  	int r = 0;
 
  	cancel_delayed_work_sync(&uq_mgr->resume_work);
@@ -633,6 +630,14 @@ 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_userq_buffer_vas_list_cleanup(adev, queue);
+	amdgpu_bo_unreserve(vm->root.bo);
+
  	mutex_lock(&uq_mgr->userq_mutex);
  	queue->hang_detect_fence = NULL;
  	amdgpu_userq_wait_for_last_fence(queue);
@@ -664,7 +669,6 @@ amdgpu_userq_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_que
  	}
  	amdgpu_userq_cleanup(queue);
  	mutex_unlock(&uq_mgr->userq_mutex);
-
  	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
 
Quoted 7 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) · line 863

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -1
  	return r;
@@ -856,7 +860,11 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
  clean_fence_driver:
  	amdgpu_userq_fence_driver_free(queue);
  clean_mapping:
-	amdgpu_userq_buffer_vas_list_cleanup(adev, queue);
+	if (!amdgpu_bo_reserve(fpriv->vm.root.bo, false)) {

The second parameter should be true here, and then you actually don't need to check the return value.

Sure Noted. Should i do that in other patches too in this series where i do amdgpu_bo_reserve -> validate -> unreserve. so change this false to true and go without checking return values.

Regards
Sunil Khatri

Quoted 11 lines

Regards,
Christian.

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
+		amdgpu_userq_buffer_vas_list_cleanup(adev, queue);
+		amdgpu_bo_unreserve(fpriv->vm.root.bo);
+	}
+
  	kfree(queue);
  	return r;
  }
maintainer
#10 lore raw

Re: [PATCH v1 3/4] drm/amdgpu/userq: caller to take reserv lock for amdgpu_userq_buffer_vas_list_cleanup

Quoted 96 lines · Sunil
Quoted 85 lines · Christian König
Quoted 84 lines · Sunil Khatri

Remove the reservation lock for vm from amdgpu_userq_buffer_vas_list_cleanup and caller should make sure it's taken before locking userq_mutex.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 34 ++++++++++++++---------
 1 file changed, 21 insertions(+), 13 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +16 -12
index 2408f888c4d9..1b81ce49d408 100644
@@ -312,25 +312,21 @@ static int amdgpu_userq_buffer_vas_list_cleanup(struct amdgpu_device *adev,
 {
 	struct amdgpu_userq_va_cursor *va_cursor, *tmp;
 	struct amdgpu_bo_va_mapping *mapping;
-	int r;
 
-	r = amdgpu_bo_reserve(queue->vm->root.bo, false);
-	if (r)
-		return r;
+	/* 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) {
-			r = -EINVAL;
-			goto err;
+			return -EINVAL;
 		}
 		dev_dbg(adev->dev, "delete the userq:%p va:%llx\n",
 			queue, va_cursor->gpu_addr);
 		amdgpu_userq_buffer_va_list_del(mapping, va_cursor);
 	}
-err:
-	amdgpu_bo_unreserve(queue->vm->root.bo);
-	return r;
+
+	return 0;
 }
 
 static int amdgpu_userq_preempt_helper(struct amdgpu_usermode_queue *queue)
@@ -444,8 +440,6 @@ static void amdgpu_userq_cleanup(struct amdgpu_usermode_queue *queue)
 	/* Wait for mode-1 reset to complete */
 	down_read(&adev->reset_domain->sem);
 
-	/* Drop the userq reference. */
-	amdgpu_userq_buffer_vas_list_cleanup(adev, queue);
 	uq_funcs->mqd_destroy(queue);
 	/* Use interrupt-safe locking since IRQ handlers may access these XArrays */
 	xa_erase_irq(&adev->userq_doorbell_xa, queue->doorbell_index);
@@ -626,6 +620,9 @@ static int
 amdgpu_userq_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_queue *queue)
 {
 	struct amdgpu_device *adev = uq_mgr->adev;
+	struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr);
+	struct amdgpu_vm *vm = &fpriv->vm;
+
 	int r = 0;
 
 	cancel_delayed_work_sync(&uq_mgr->resume_work);
@@ -633,6 +630,14 @@ 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_userq_buffer_vas_list_cleanup(adev, queue);
+	amdgpu_bo_unreserve(vm->root.bo);
+
 	mutex_lock(&uq_mgr->userq_mutex);
 	queue->hang_detect_fence = NULL;
 	amdgpu_userq_wait_for_last_fence(queue);
@@ -664,7 +669,6 @@ amdgpu_userq_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_que
 	}
 	amdgpu_userq_cleanup(queue);
 	mutex_unlock(&uq_mgr->userq_mutex);
-
 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
 
Quoted 7 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args) · line 863

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -1
 	return r;
@@ -856,7 +860,11 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 clean_fence_driver:
 	amdgpu_userq_fence_driver_free(queue);
 clean_mapping:
-	amdgpu_userq_buffer_vas_list_cleanup(adev, queue);
+	if (!amdgpu_bo_reserve(fpriv->vm.root.bo, false)) {

The second parameter should be true here, and then you actually don't need to check the return value.

Quoted 1 line

Sure Noted. Should i do that in other patches too in this series where i do amdgpu_bo_reserve -> validate -> unreserve. so change this false to true and go without checking return values.

It depend where the call is. If you can savely return to userspace then the lock should be interruptible (e.g. parameter false).

If it is like this here in a cleanup/error path you can only do it non-interruptible and then also don't check the return value.

Regards,
Christian.

Quoted 13 lines

Regards
Sunil Khatri

Quoted 10 lines

Regards,
Christian.

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
+		amdgpu_userq_buffer_vas_list_cleanup(adev, queue);
+		amdgpu_bo_unreserve(fpriv->vm.root.bo);
+	}
+
 	kfree(queue);
 	return r;
 }
#11 lore raw

PATCH 4/4 drm/amdgpu/userq: hold reservation lock in caller of amdgpu_userq_input_va_validate

In reply to Sunil Khatri

Caller should hold the reservation lock for root.bo in func amdgpu_userq_input_va_validate.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  | 18 ++++++++++-------
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 23 ++++++++++++++++++++++
 2 files changed, 34 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +11 -7
index 1b81ce49d408..333bd84e7619 100644
@@ -239,13 +239,12 @@ int amdgpu_userq_input_va_validate(struct amdgpu_device *adev,
 	u64 size;
 	int r = 0;
 
+	/* Caller must hold vm->root.bo reservation */
+	dma_resv_assert_held(queue->vm->root.bo->tbo.base.resv);
+
 	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) {
 		r = -EINVAL;
@@ -255,13 +254,11 @@ int amdgpu_userq_input_va_validate(struct amdgpu_device *adev,
 	if (user_addr >= va_map->start  &&
 	    va_map->last - user_addr + 1 >= size) {
 		amdgpu_userq_buffer_va_list_add(queue, va_map, user_addr);
-		amdgpu_bo_unreserve(vm->root.bo);
 		return 0;
 	}
 
 	r = -EINVAL;
 out_err:
-	amdgpu_bo_unreserve(vm->root.bo);
 	return r;
 }
 
@@ -773,13 +770,20 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 	db_info.doorbell_offset = args->in.doorbell_offset;
 
 	queue->userq_mgr = uq_mgr;
+
 	/* Validate the userq virtual address.*/
+	r = amdgpu_bo_reserve(fpriv->vm.root.bo, false);
+	if (r)
+		goto free_queue;
+
 	if (amdgpu_userq_input_va_validate(adev, queue, args->in.queue_va, args->in.queue_size) ||
 	    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;
+		amdgpu_bo_unreserve(fpriv->vm.root.bo);
 		goto clean_mapping;
 	}
+	amdgpu_bo_unreserve(fpriv->vm.root.bo);
 
 	/* Convert relative doorbell offset into absolute doorbell index */
 	index = amdgpu_userq_get_doorbell_index(uq_mgr, &db_info, filp);
@@ -864,7 +868,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 		amdgpu_userq_buffer_vas_list_cleanup(adev, queue);
 		amdgpu_bo_unreserve(fpriv->vm.root.bo);
 	}
-
+free_queue:
 	kfree(queue);
 	return r;
 }
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +23 -0
index 527cf2f14691..f7e00169c0be 100644
@@ -322,12 +322,19 @@ static int mes_userq_mqd_create(struct amdgpu_usermode_queue *queue,
 			goto free_mqd;
 		}
 
+		r = amdgpu_bo_reserve(queue->vm->root.bo, false);
+		if (r) {
+			kfree(compute_mqd);
+			goto free_mqd;
+		}
 		r = amdgpu_userq_input_va_validate(adev, queue, compute_mqd->eop_va,
 						   2048);
 		if (r) {
+			amdgpu_bo_unreserve(queue->vm->root.bo);
 			kfree(compute_mqd);
 			goto free_mqd;
 		}
+		amdgpu_bo_unreserve(queue->vm->root.bo);
 
 		userq_props->eop_gpu_addr = compute_mqd->eop_va;
 		userq_props->hqd_pipe_priority = AMDGPU_GFX_PIPE_PRIO_NORMAL;
@@ -365,18 +372,27 @@ static int mes_userq_mqd_create(struct amdgpu_usermode_queue *queue,
 		userq_props->tmz_queue =
 			mqd_user->flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;
 
+		r = amdgpu_bo_reserve(queue->vm->root.bo, false);
+		if (r) {
+			kfree(mqd_gfx_v11);
+			goto free_mqd;
+		}
 		r = amdgpu_userq_input_va_validate(adev, queue, mqd_gfx_v11->shadow_va,
 						   shadow_info.shadow_size);
 		if (r) {
+			amdgpu_bo_unreserve(queue->vm->root.bo);
 			kfree(mqd_gfx_v11);
 			goto free_mqd;
 		}
+
 		r = amdgpu_userq_input_va_validate(adev, queue, mqd_gfx_v11->csa_va,
 						   shadow_info.csa_size);
 		if (r) {
+			amdgpu_bo_unreserve(queue->vm->root.bo);
 			kfree(mqd_gfx_v11);
 			goto free_mqd;
 		}
+		amdgpu_bo_unreserve(queue->vm->root.bo);
 
 		kfree(mqd_gfx_v11);
 	} else if (queue->queue_type == AMDGPU_HW_IP_DMA) {
@@ -394,12 +410,19 @@ static int mes_userq_mqd_create(struct amdgpu_usermode_queue *queue,
 			r = -ENOMEM;
 			goto free_mqd;
 		}
+		r = amdgpu_bo_reserve(queue->vm->root.bo, false);
+		if (r) {
+			kfree(mqd_sdma_v11);
+			goto free_mqd;
+		}
 		r = amdgpu_userq_input_va_validate(adev, queue, mqd_sdma_v11->csa_va,
 						   32);
 		if (r) {
+			amdgpu_bo_unreserve(queue->vm->root.bo);
 			kfree(mqd_sdma_v11);
 			goto free_mqd;
 		}
+		amdgpu_bo_unreserve(queue->vm->root.bo);
 
 		userq_props->csa_addr = mqd_sdma_v11->csa_va;
 		kfree(mqd_sdma_v11);
Signature · 1 line
2.34.1
maintainer
#12 lore raw

Re: [PATCH v1 4/4] drm/amdgpu/userq: hold reservation lock in caller of amdgpu_userq_input_va_validate

Quoted 85 lines · Sunil Khatri

Caller should hold the reservation lock for root.bo in func amdgpu_userq_input_va_validate.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  | 18 ++++++++++-------
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 23 ++++++++++++++++++++++
 2 files changed, 34 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +11 -7
index 1b81ce49d408..333bd84e7619 100644
@@ -239,13 +239,12 @@ int amdgpu_userq_input_va_validate(struct amdgpu_device *adev,
 	u64 size;
 	int r = 0;
 
+	/* Caller must hold vm->root.bo reservation */
+	dma_resv_assert_held(queue->vm->root.bo->tbo.base.resv);
+
 	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) {
 		r = -EINVAL;
@@ -255,13 +254,11 @@ int amdgpu_userq_input_va_validate(struct amdgpu_device *adev,
 	if (user_addr >= va_map->start  &&
 	    va_map->last - user_addr + 1 >= size) {
 		amdgpu_userq_buffer_va_list_add(queue, va_map, user_addr);
-		amdgpu_bo_unreserve(vm->root.bo);
 		return 0;
 	}
 
 	r = -EINVAL;
 out_err:
-	amdgpu_bo_unreserve(vm->root.bo);
 	return r;
 }
 
@@ -773,13 +770,20 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 	db_info.doorbell_offset = args->in.doorbell_offset;
 
 	queue->userq_mgr = uq_mgr;
+
 	/* Validate the userq virtual address.*/
+	r = amdgpu_bo_reserve(fpriv->vm.root.bo, false);
+	if (r)
+		goto free_queue;
+
 	if (amdgpu_userq_input_va_validate(adev, queue, args->in.queue_va, args->in.queue_size) ||
 	    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;
+		amdgpu_bo_unreserve(fpriv->vm.root.bo);
 		goto clean_mapping;
 	}
+	amdgpu_bo_unreserve(fpriv->vm.root.bo);
 
 	/* Convert relative doorbell offset into absolute doorbell index */
 	index = amdgpu_userq_get_doorbell_index(uq_mgr, &db_info, filp);
@@ -864,7 +868,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 		amdgpu_userq_buffer_vas_list_cleanup(adev, queue);
 		amdgpu_bo_unreserve(fpriv->vm.root.bo);
 	}
-
+free_queue:
 	kfree(queue);
 	return r;
 }
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +3 -0
index 527cf2f14691..f7e00169c0be 100644
@@ -322,12 +322,19 @@ static int mes_userq_mqd_create(struct amdgpu_usermode_queue *queue,
 			goto free_mqd;
 		}
 
+		r = amdgpu_bo_reserve(queue->vm->root.bo, false);
+		if (r) {
+			kfree(compute_mqd);

drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_mqd_create(struct amdgpu_usermode_queue *queue, · line 337

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +4 -0
+			goto free_mqd;
+		}
 		r = amdgpu_userq_input_va_validate(adev, queue, compute_mqd->eop_va,
 						   2048);
 		if (r) {
+			amdgpu_bo_unreserve(queue->vm->root.bo);
 			kfree(compute_mqd);
 			goto free_mqd;
 		}
+		amdgpu_bo_unreserve(queue->vm->root.bo);

That can be done before the "if (r)", so that we don't need it inside the if any more.

drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_mqd_create(struct amdgpu_usermode_queue *queue, · line 375

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +1 -0
 		userq_props->eop_gpu_addr = compute_mqd->eop_va;
 		userq_props->hqd_pipe_priority = AMDGPU_GFX_PIPE_PRIO_NORMAL;
@@ -365,18 +372,27 @@ static int mes_userq_mqd_create(struct amdgpu_usermode_queue *queue,
 		userq_props->tmz_queue =
 			mqd_user->flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;
 
+		r = amdgpu_bo_reserve(queue->vm->root.bo, false);

Acquiring and releasing the VM lock multiple times is a really bad idea, but cleaning that up can come later.

Apart from the nit pick above the patch looks good to me.

Regards,
Christian.

Quoted 43 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +15 -0
+		if (r) {
+			kfree(mqd_gfx_v11);
+			goto free_mqd;
+		}
 		r = amdgpu_userq_input_va_validate(adev, queue, mqd_gfx_v11->shadow_va,
 						   shadow_info.shadow_size);
 		if (r) {
+			amdgpu_bo_unreserve(queue->vm->root.bo);
 			kfree(mqd_gfx_v11);
 			goto free_mqd;
 		}
+
 		r = amdgpu_userq_input_va_validate(adev, queue, mqd_gfx_v11->csa_va,
 						   shadow_info.csa_size);
 		if (r) {
+			amdgpu_bo_unreserve(queue->vm->root.bo);
 			kfree(mqd_gfx_v11);
 			goto free_mqd;
 		}
+		amdgpu_bo_unreserve(queue->vm->root.bo);
 
 		kfree(mqd_gfx_v11);
 	} else if (queue->queue_type == AMDGPU_HW_IP_DMA) {
@@ -394,12 +410,19 @@ static int mes_userq_mqd_create(struct amdgpu_usermode_queue *queue,
 			r = -ENOMEM;
 			goto free_mqd;
 		}
+		r = amdgpu_bo_reserve(queue->vm->root.bo, false);
+		if (r) {
+			kfree(mqd_sdma_v11);
+			goto free_mqd;
+		}
 		r = amdgpu_userq_input_va_validate(adev, queue, mqd_sdma_v11->csa_va,
 						   32);
 		if (r) {
+			amdgpu_bo_unreserve(queue->vm->root.bo);
 			kfree(mqd_sdma_v11);
 			goto free_mqd;
 		}
+		amdgpu_bo_unreserve(queue->vm->root.bo);
 
 		userq_props->csa_addr = mqd_sdma_v11->csa_va;
 		kfree(mqd_sdma_v11);
#13 lore raw

Re: [PATCH v1 4/4] drm/amdgpu/userq: hold reservation lock in caller of amdgpu_userq_input_va_validate

Quoted 83 lines · Christian König
Quoted 82 lines · Sunil Khatri

Caller should hold the reservation lock for root.bo in func amdgpu_userq_input_va_validate.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  | 18 ++++++++++-------
  drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 23 ++++++++++++++++++++++
  2 files changed, 34 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +11 -7
index 1b81ce49d408..333bd84e7619 100644
@@ -239,13 +239,12 @@ int amdgpu_userq_input_va_validate(struct amdgpu_device *adev,
  	u64 size;
  	int r = 0;
 
+	/* Caller must hold vm->root.bo reservation */
+	dma_resv_assert_held(queue->vm->root.bo->tbo.base.resv);
+
  	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) {
  		r = -EINVAL;
@@ -255,13 +254,11 @@ int amdgpu_userq_input_va_validate(struct amdgpu_device *adev,
  	if (user_addr >= va_map->start  &&
  	    va_map->last - user_addr + 1 >= size) {
  		amdgpu_userq_buffer_va_list_add(queue, va_map, user_addr);
-		amdgpu_bo_unreserve(vm->root.bo);
  		return 0;
  	}
 
  	r = -EINVAL;
  out_err:
-	amdgpu_bo_unreserve(vm->root.bo);
  	return r;
  }
 
@@ -773,13 +770,20 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
  	db_info.doorbell_offset = args->in.doorbell_offset;
 
  	queue->userq_mgr = uq_mgr;
+
  	/* Validate the userq virtual address.*/
+	r = amdgpu_bo_reserve(fpriv->vm.root.bo, false);
+	if (r)
+		goto free_queue;
+
  	if (amdgpu_userq_input_va_validate(adev, queue, args->in.queue_va, args->in.queue_size) ||
  	    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;
+		amdgpu_bo_unreserve(fpriv->vm.root.bo);
  		goto clean_mapping;
  	}
+	amdgpu_bo_unreserve(fpriv->vm.root.bo);
 
  	/* Convert relative doorbell offset into absolute doorbell index */
  	index = amdgpu_userq_get_doorbell_index(uq_mgr, &db_info, filp);
@@ -864,7 +868,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
  		amdgpu_userq_buffer_vas_list_cleanup(adev, queue);
  		amdgpu_bo_unreserve(fpriv->vm.root.bo);
  	}
-
+free_queue:
  	kfree(queue);
  	return r;
  }
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -0
index 527cf2f14691..f7e00169c0be 100644
@@ -322,12 +322,19 @@ static int mes_userq_mqd_create(struct amdgpu_usermode_queue *queue,
  			goto free_mqd;
  		}
 
Quoted 14 lines

drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_mqd_create(struct amdgpu_usermode_queue *queue, · line 337

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +7 -0
+		r = amdgpu_bo_reserve(queue->vm->root.bo, false);
+		if (r) {
+			kfree(compute_mqd);
+			goto free_mqd;
+		}
  		r = amdgpu_userq_input_va_validate(adev, queue, compute_mqd->eop_va,
  						   2048);
  		if (r) {
+			amdgpu_bo_unreserve(queue->vm->root.bo);
  			kfree(compute_mqd);
  			goto free_mqd;
  		}
+		amdgpu_bo_unreserve(queue->vm->root.bo);

That can be done before the "if (r)", so that we don't need it inside the if any more.

Sure Noted.

Quoted 8 lines
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +0 -0
  		userq_props->eop_gpu_addr = compute_mqd->eop_va;
  		userq_props->hqd_pipe_priority = AMDGPU_GFX_PIPE_PRIO_NORMAL;
@@ -365,18 +372,27 @@ static int mes_userq_mqd_create(struct amdgpu_usermode_queue *queue,
  		userq_props->tmz_queue =
  			mqd_user->flags & AMDGPU_USERQ_CREATE_FLAGS_QUEUE_SECURE;
 
Quoted 4 lines

drivers/gpu/drm/amd/amdgpu/mes_userqueue.c static int mes_userq_mqd_create(struct amdgpu_usermode_queue *queue, · line 375

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +1 -0
+		r = amdgpu_bo_reserve(queue->vm->root.bo, false);

Acquiring and releasing the VM lock multiple times is a really bad idea, but cleaning that up can come later.

Apart from the nit pick above the patch looks good to me.

Sure Noted. Let me send the v2 of all the patches.

regards
Sunil khatri

Quoted 47 lines

Regards,
Christian.

Quoted 43 lines
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +15 -0
+		if (r) {
+			kfree(mqd_gfx_v11);
+			goto free_mqd;
+		}
  		r = amdgpu_userq_input_va_validate(adev, queue, mqd_gfx_v11->shadow_va,
  						   shadow_info.shadow_size);
  		if (r) {
+			amdgpu_bo_unreserve(queue->vm->root.bo);
  			kfree(mqd_gfx_v11);
  			goto free_mqd;
  		}
+
  		r = amdgpu_userq_input_va_validate(adev, queue, mqd_gfx_v11->csa_va,
  						   shadow_info.csa_size);
  		if (r) {
+			amdgpu_bo_unreserve(queue->vm->root.bo);
  			kfree(mqd_gfx_v11);
  			goto free_mqd;
  		}
+		amdgpu_bo_unreserve(queue->vm->root.bo);
 
  		kfree(mqd_gfx_v11);
  	} else if (queue->queue_type == AMDGPU_HW_IP_DMA) {
@@ -394,12 +410,19 @@ static int mes_userq_mqd_create(struct amdgpu_usermode_queue *queue,
  			r = -ENOMEM;
  			goto free_mqd;
  		}
+		r = amdgpu_bo_reserve(queue->vm->root.bo, false);
+		if (r) {
+			kfree(mqd_sdma_v11);
+			goto free_mqd;
+		}
  		r = amdgpu_userq_input_va_validate(adev, queue, mqd_sdma_v11->csa_va,
  						   32);
  		if (r) {
+			amdgpu_bo_unreserve(queue->vm->root.bo);
  			kfree(mqd_sdma_v11);
  			goto free_mqd;
  		}
+		amdgpu_bo_unreserve(queue->vm->root.bo);
 
  		userq_props->csa_addr = mqd_sdma_v11->csa_va;
  		kfree(mqd_sdma_v11);