Skip to contentSkip to conversation

Use kfd_lock/unlock_pdd helpers

PATCH In review Daniel Phillips 2 participants 5 messages Aug 25, 2022 – Aug 29, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover Use kfd_lock/unlock_pdd helpers

Patch 1 adds kfd_lock_pdd_by_id and patch 2 adds kfd_unlock_pdd helpers, broken out this way to reduce noise in the first patch, which is a bit tricky because of modifying a lot of error paths. Patch 2 is trivial.

Daniel Phillips (2):
drm/amdgpu: Use kfd_lock_pdd_by_id helper in more places drm/amdgpu: Use kfd_unlock_pdd helper

 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 130 +++++++++--------------
 1 file changed, 48 insertions(+), 82 deletions(-)
Signature · 1 line
2.35.1
maintainer
#1 lore raw

Do you mind squashing the two patches. It would make them easier to review because it makes it easier to see that the same functions are using both.

Thanks,
  Felix

Quoted 11 lines · Daniel Phillips

Patch 1 adds kfd_lock_pdd_by_id and patch 2 adds kfd_unlock_pdd helpers, broken out this way to reduce noise in the first patch, which is a bit tricky because of modifying a lot of error paths. Patch 2 is trivial.

Daniel Phillips (2):
drm/amdgpu: Use kfd_lock_pdd_by_id helper in more places drm/amdgpu: Use kfd_unlock_pdd helper

  drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 130 +++++++++--------------
  1 file changed, 48 insertions(+), 82 deletions(-)
#2 lore raw
Quoted 1 line · Felix Kuehling

Do you mind squashing the two patches. It would make them easier to review because it makes it easier to see that the same functions are using both.

Will do.

Regards,
Daniel

#3 lore raw

PATCH 1/2 drm/amdgpu: Use kfd_lock_pdd_by_id helper in more places

In reply to Daniel Phillips

Convert most of the "mutex_lock; kfd_process_device_data_by_id" occurrences in kfd_chardev to use the kfd_lock_pdd_by_id. These will now consistently log debug output if the lookup fails. Sites where kfd_process_device_data_by_id is used without locking are not converted for now.

---
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 96 ++++++++----------------
 1 file changed, 32 insertions(+), 64 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +32 -64
index 2b3d8bc8f0aa..bb5528c55b73 100644
@@ -75,6 +75,7 @@ static inline struct kfd_process_device *kfd_lock_pdd_by_id(struct kfd_process *
 	if (pdd)
 		return pdd;
 
+	pr_debug("Could not find gpu id 0x%x\n", gpu_id);
 	mutex_unlock(&p->mutex);
 	return NULL;
 }
@@ -311,14 +312,9 @@ static int kfd_ioctl_create_queue(struct file *filep, struct kfd_process *p,
 
 	pr_debug("Looking for gpu id 0x%x\n", args->gpu_id);
 
-	mutex_lock(&p->mutex);
-
-	pdd = kfd_process_device_data_by_id(p, args->gpu_id);
-	if (!pdd) {
-		pr_debug("Could not find gpu id 0x%x\n", args->gpu_id);
-		err = -EINVAL;
-		goto err_pdd;
-	}
+	pdd = kfd_lock_pdd_by_id(p, args->gpu_id);
+	if (!pdd)
+		return -EINVAL;
 	dev = pdd->dev;
 
 	pdd = kfd_bind_process_to_device(dev, p);
@@ -405,7 +401,6 @@ static int kfd_ioctl_create_queue(struct file *filep, struct kfd_process *p,
 		amdgpu_amdkfd_free_gtt_mem(dev->adev, wptr_bo);
 err_wptr_map_gart:
 err_bind_process:
-err_pdd:
 	mutex_unlock(&p->mutex);
 	return err;
 }
@@ -566,13 +561,9 @@ static int kfd_ioctl_set_memory_policy(struct file *filep,
 		return -EINVAL;
 	}
 
-	mutex_lock(&p->mutex);
-	pdd = kfd_process_device_data_by_id(p, args->gpu_id);
-	if (!pdd) {
-		pr_debug("Could not find gpu id 0x%x\n", args->gpu_id);
-		err = -EINVAL;
-		goto err_pdd;
-	}
+	pdd = kfd_lock_pdd_by_id(p, args->gpu_id);
+	if (!pdd)
+		return -EINVAL;
 
 	pdd = kfd_bind_process_to_device(pdd->dev, p);
 	if (IS_ERR(pdd)) {
@@ -596,7 +587,6 @@ static int kfd_ioctl_set_memory_policy(struct file *filep,
 		err = -EINVAL;
 
 out:
-err_pdd:
 	mutex_unlock(&p->mutex);
 
 	return err;
@@ -609,13 +599,9 @@ static int kfd_ioctl_set_trap_handler(struct file *filep,
 	int err = 0;
 	struct kfd_process_device *pdd;
 
-	mutex_lock(&p->mutex);
-
-	pdd = kfd_process_device_data_by_id(p, args->gpu_id);
-	if (!pdd) {
-		err = -EINVAL;
-		goto err_pdd;
-	}
+	pdd = kfd_lock_pdd_by_id(p, args->gpu_id);
+	if (!pdd)
+		return -EINVAL;
 
 	pdd = kfd_bind_process_to_device(pdd->dev, p);
 	if (IS_ERR(pdd)) {
@@ -626,7 +612,6 @@ static int kfd_ioctl_set_trap_handler(struct file *filep,
 	kfd_process_set_trap_handler(&pdd->qpd, args->tba_addr, args->tma_addr);
 
 out:
-err_pdd:
 	mutex_unlock(&p->mutex);
 
 	return err;
@@ -663,13 +648,12 @@ static int kfd_ioctl_get_clock_counters(struct file *filep,
 	struct kfd_ioctl_get_clock_counters_args *args = data;
 	struct kfd_process_device *pdd;
 
-	mutex_lock(&p->mutex);
-	pdd = kfd_process_device_data_by_id(p, args->gpu_id);
-	mutex_unlock(&p->mutex);
-	if (pdd)
+	pdd = kfd_lock_pdd_by_id(p, args->gpu_id);
+	if (pdd) {
+		mutex_unlock(&p->mutex);
 		/* Reading GPU clock counter from KGD */
 		args->gpu_clock_counter = amdgpu_amdkfd_get_gpu_clock_counter(pdd->dev->adev);
-	else
+	} else
 		/* Node without GPU resource */
 		args->gpu_clock_counter = 0;
 
@@ -886,12 +870,9 @@ static int kfd_ioctl_set_scratch_backing_va(struct file *filep,
 	struct kfd_dev *dev;
 	long err;
 
-	mutex_lock(&p->mutex);
-	pdd = kfd_process_device_data_by_id(p, args->gpu_id);
-	if (!pdd) {
-		err = -EINVAL;
-		goto err_pdd;
-	}
+	pdd = kfd_lock_pdd_by_id(p, args->gpu_id);
+	if (!pdd)
+		return -EINVAL;
 	dev = pdd->dev;
 
 	pdd = kfd_bind_process_to_device(dev, p);
@@ -912,7 +893,6 @@ static int kfd_ioctl_set_scratch_backing_va(struct file *filep,
 	return 0;
 
 bind_process_to_device_fail:
-err_pdd:
 	mutex_unlock(&p->mutex);
 	return err;
 }
@@ -973,12 +953,9 @@ static int kfd_ioctl_acquire_vm(struct file *filep, struct kfd_process *p,
 	if (!drm_file)
 		return -EINVAL;
 
-	mutex_lock(&p->mutex);
-	pdd = kfd_process_device_data_by_id(p, args->gpu_id);
-	if (!pdd) {
-		ret = -EINVAL;
-		goto err_pdd;
-	}
+	pdd = kfd_lock_pdd_by_id(p, args->gpu_id);
+	if (!pdd)
+		return -EINVAL;
 
 	if (pdd->drm_file) {
 		ret = pdd->drm_file == drm_file ? 0 : -EBUSY;
@@ -995,7 +972,6 @@ static int kfd_ioctl_acquire_vm(struct file *filep, struct kfd_process *p,
 	return 0;
 
 err_unlock:
-err_pdd:
 err_drm_file:
 	mutex_unlock(&p->mutex);
 	fput(drm_file);
@@ -1063,12 +1039,9 @@ static int kfd_ioctl_alloc_memory_of_gpu(struct file *filep,
 	}
 	mutex_unlock(&p->svms.lock);
 #endif
-	mutex_lock(&p->mutex);
-	pdd = kfd_process_device_data_by_id(p, args->gpu_id);
-	if (!pdd) {
-		err = -EINVAL;
-		goto err_pdd;
-	}
+	pdd = kfd_lock_pdd_by_id(p, args->gpu_id);
+	if (!pdd)
+		return EINVAL;
 
 	dev = pdd->dev;
 
@@ -1140,7 +1113,6 @@ static int kfd_ioctl_alloc_memory_of_gpu(struct file *filep,
 	amdgpu_amdkfd_gpuvm_free_memory_of_gpu(dev->adev, (struct kgd_mem *)mem,
 					       pdd->drm_priv, NULL);
 err_unlock:
-err_pdd:
 err_large_bar:
 	mutex_unlock(&p->mutex);
 	return err;
@@ -1231,8 +1203,7 @@ static int kfd_ioctl_map_memory_to_gpu(struct file *filep,
 		goto copy_from_user_failed;
 	}
 
-	mutex_lock(&p->mutex);
-	pdd = kfd_process_device_data_by_id(p, GET_GPU_ID(args->handle));
+	pdd = kfd_lock_pdd_by_id(p, GET_GPU_ID(args->handle));
 	if (!pdd) {
 		err = -EINVAL;
 		goto get_process_device_data_failed;
@@ -1304,12 +1275,12 @@ static int kfd_ioctl_map_memory_to_gpu(struct file *filep,
 
 	return err;
 
-get_process_device_data_failed:
 bind_process_to_device_failed:
 get_mem_obj_from_handle_failed:
 map_memory_to_gpu_failed:
 	mutex_unlock(&p->mutex);
 copy_from_user_failed:
+get_process_device_data_failed:
 sync_memory_failed:
 	kfree(devices_arr);
 
@@ -1347,8 +1318,7 @@ static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
 		goto copy_from_user_failed;
 	}
 
-	mutex_lock(&p->mutex);
-	pdd = kfd_process_device_data_by_id(p, GET_GPU_ID(args->handle));
+	pdd = kfd_lock_pdd_by_id(p, GET_GPU_ID(args->handle));
 	if (!pdd) {
 		err = -EINVAL;
 		goto bind_process_to_device_failed;
@@ -1398,10 +1368,10 @@ static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
 
 	return 0;
 
-bind_process_to_device_failed:
 get_mem_obj_from_handle_failed:
 unmap_memory_from_gpu_failed:
 	mutex_unlock(&p->mutex);
+bind_process_to_device_failed:
 copy_from_user_failed:
 sync_memory_failed:
 	kfree(devices_arr);
@@ -1517,11 +1487,10 @@ static int kfd_ioctl_import_dmabuf(struct file *filep,
 	if (IS_ERR(dmabuf))
 		return PTR_ERR(dmabuf);
 
-	mutex_lock(&p->mutex);
-	pdd = kfd_process_device_data_by_id(p, args->gpu_id);
+	pdd = kfd_lock_pdd_by_id(p, args->gpu_id);
 	if (!pdd) {
 		r = -EINVAL;
-		goto err_unlock;
+		goto err_pdd;
 	}
 
 	pdd = kfd_bind_process_to_device(pdd->dev, p);
@@ -1555,6 +1524,7 @@ static int kfd_ioctl_import_dmabuf(struct file *filep,
 					       pdd->drm_priv, NULL);
 err_unlock:
 	mutex_unlock(&p->mutex);
+err_pdd:
 	dma_buf_put(dmabuf);
 	return r;
 }
@@ -1566,13 +1536,11 @@ static int kfd_ioctl_smi_events(struct file *filep,
 	struct kfd_ioctl_smi_events_args *args = data;
 	struct kfd_process_device *pdd;
 
-	mutex_lock(&p->mutex);
-
-	pdd = kfd_process_device_data_by_id(p, args->gpuid);
-	mutex_unlock(&p->mutex);
+	pdd = kfd_lock_pdd_by_id(p, args->gpuid);
 	if (!pdd)
 		return -EINVAL;
 
+	mutex_unlock(&p->mutex);
 	return kfd_smi_event_open(pdd->dev, &args->anon_fd);
 }
 
Signature · 1 line
2.35.1
#4 lore raw

PATCH 2/2 drm/amdgpu: Use kfd_unlock_pdd helper

In reply to Daniel Phillips

Trivially changes kfd_lock_pdd_by_id ... mutex_unlock to use the kfd_unlock_pdd helper.

---
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 38 +++++++++++-------------
 1 file changed, 18 insertions(+), 20 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +18 -20
index bb5528c55b73..cf5bfd928b69 100644
@@ -381,7 +381,7 @@ static int kfd_ioctl_create_queue(struct file *filep, struct kfd_process *p,
 		 */
 		args->doorbell_offset |= doorbell_offset_in_process;
 
-	mutex_unlock(&p->mutex);
+	kfd_unlock_pdd(pdd);
 
 	pr_debug("Queue id %d was created successfully\n", args->queue_id);
 
@@ -401,7 +401,7 @@ static int kfd_ioctl_create_queue(struct file *filep, struct kfd_process *p,
 		amdgpu_amdkfd_free_gtt_mem(dev->adev, wptr_bo);
 err_wptr_map_gart:
 err_bind_process:
-	mutex_unlock(&p->mutex);
+	kfd_unlock_pdd(pdd);
 	return err;
 }
 
@@ -587,8 +587,7 @@ static int kfd_ioctl_set_memory_policy(struct file *filep,
 		err = -EINVAL;
 
 out:
-	mutex_unlock(&p->mutex);
-
+	kfd_unlock_pdd(pdd);
 	return err;
 }
 
@@ -612,8 +611,7 @@ static int kfd_ioctl_set_trap_handler(struct file *filep,
 	kfd_process_set_trap_handler(&pdd->qpd, args->tba_addr, args->tma_addr);
 
 out:
-	mutex_unlock(&p->mutex);
-
+	kfd_unlock_pdd(pdd);
 	return err;
 }
 
@@ -650,7 +648,7 @@ static int kfd_ioctl_get_clock_counters(struct file *filep,
 
 	pdd = kfd_lock_pdd_by_id(p, args->gpu_id);
 	if (pdd) {
-		mutex_unlock(&p->mutex);
+		kfd_unlock_pdd(pdd);
 		/* Reading GPU clock counter from KGD */
 		args->gpu_clock_counter = amdgpu_amdkfd_get_gpu_clock_counter(pdd->dev->adev);
 	} else
@@ -883,7 +881,7 @@ static int kfd_ioctl_set_scratch_backing_va(struct file *filep,
 
 	pdd->qpd.sh_hidden_private_base = args->va_addr;
 
-	mutex_unlock(&p->mutex);
+	kfd_unlock_pdd(pdd);
 
 	if (dev->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS &&
 	    pdd->qpd.vmid != 0 && dev->kfd2kgd->set_scratch_backing_va)
@@ -893,7 +891,7 @@ static int kfd_ioctl_set_scratch_backing_va(struct file *filep,
 	return 0;
 
 bind_process_to_device_fail:
-	mutex_unlock(&p->mutex);
+	kfd_unlock_pdd(pdd);
 	return err;
 }
 
@@ -967,13 +965,13 @@ static int kfd_ioctl_acquire_vm(struct file *filep, struct kfd_process *p,
 		goto err_unlock;
 
 	/* On success, the PDD keeps the drm_file reference */
-	mutex_unlock(&p->mutex);
+	kfd_unlock_pdd(pdd);
 
 	return 0;
 
 err_unlock:
 err_drm_file:
-	mutex_unlock(&p->mutex);
+	kfd_unlock_pdd(pdd);
 	fput(drm_file);
 	return ret;
 }
@@ -1095,7 +1093,7 @@ static int kfd_ioctl_alloc_memory_of_gpu(struct file *filep,
 	if (flags & KFD_IOC_ALLOC_MEM_FLAGS_VRAM)
 		WRITE_ONCE(pdd->vram_usage, pdd->vram_usage + args->size);
 
-	mutex_unlock(&p->mutex);
+	kfd_unlock_pdd(pdd);
 
 	args->handle = MAKE_HANDLE(args->gpu_id, idr_handle);
 	args->mmap_offset = offset;
@@ -1114,7 +1112,7 @@ static int kfd_ioctl_alloc_memory_of_gpu(struct file *filep,
 					       pdd->drm_priv, NULL);
 err_unlock:
 err_large_bar:
-	mutex_unlock(&p->mutex);
+	kfd_unlock_pdd(pdd);
 	return err;
 }
 
@@ -1256,7 +1254,7 @@ static int kfd_ioctl_map_memory_to_gpu(struct file *filep,
 		args->n_success = i+1;
 	}
 
-	mutex_unlock(&p->mutex);
+	kfd_unlock_pdd(pdd);
 
 	err = amdgpu_amdkfd_gpuvm_sync_memory(dev->adev, (struct kgd_mem *) mem, true);
 	if (err) {
@@ -1278,7 +1276,7 @@ static int kfd_ioctl_map_memory_to_gpu(struct file *filep,
 bind_process_to_device_failed:
 get_mem_obj_from_handle_failed:
 map_memory_to_gpu_failed:
-	mutex_unlock(&p->mutex);
+	kfd_unlock_pdd(pdd);
 copy_from_user_failed:
 get_process_device_data_failed:
 sync_memory_failed:
@@ -1346,7 +1344,7 @@ static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
 		}
 		args->n_success = i+1;
 	}
-	mutex_unlock(&p->mutex);
+	kfd_unlock_pdd(pdd);
 
 	if (kfd_flush_tlb_after_unmap(pdd->dev)) {
 		err = amdgpu_amdkfd_gpuvm_sync_memory(pdd->dev->adev,
@@ -1370,7 +1368,7 @@ static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
 
 get_mem_obj_from_handle_failed:
 unmap_memory_from_gpu_failed:
-	mutex_unlock(&p->mutex);
+	kfd_unlock_pdd(pdd);
 bind_process_to_device_failed:
 copy_from_user_failed:
 sync_memory_failed:
@@ -1512,7 +1510,7 @@ static int kfd_ioctl_import_dmabuf(struct file *filep,
 		goto err_free;
 	}
 
-	mutex_unlock(&p->mutex);
+	kfd_unlock_pdd(pdd);
 	dma_buf_put(dmabuf);
 
 	args->handle = MAKE_HANDLE(args->gpu_id, idr_handle);
@@ -1523,7 +1521,7 @@ static int kfd_ioctl_import_dmabuf(struct file *filep,
 	amdgpu_amdkfd_gpuvm_free_memory_of_gpu(pdd->dev->adev, (struct kgd_mem *)mem,
 					       pdd->drm_priv, NULL);
 err_unlock:
-	mutex_unlock(&p->mutex);
+	kfd_unlock_pdd(pdd);
 err_pdd:
 	dma_buf_put(dmabuf);
 	return r;
@@ -1540,7 +1538,7 @@ static int kfd_ioctl_smi_events(struct file *filep,
 	if (!pdd)
 		return -EINVAL;
 
-	mutex_unlock(&p->mutex);
+	kfd_unlock_pdd(pdd);
 	return kfd_smi_event_open(pdd->dev, &args->anon_fd);
 }
 
Signature · 1 line
2.35.1