All of lore.kernel.org
 help / color / mirror / Atom feed
From: Daniel Phillips <daniel.phillips@amd.com>
To: <amd-gfx@lists.freedesktop.org>
Cc: Daniel Phillips <daniel.phillips@amd.com>, felix.kuehling@amd.com
Subject: [PATCH 1/1] drm/amdgpu: Use kfd_lock_pdd_by_id helper in more places
Date: Mon, 22 Aug 2022 08:30:03 -0700	[thread overview]
Message-ID: <20220822153003.6349-1-daniel.phillips@amd.com> (raw)

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.

Signed-off-by: Daniel Phillips <daniel.phillips@amd.com>
---
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 96 ++++++++----------------
 1 file changed, 32 insertions(+), 64 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
index 2b3d8bc8f0aa..bb5528c55b73 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c
@@ -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);
 }
 
-- 
2.35.1


             reply	other threads:[~2022-08-22 15:30 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-22 15:30 Daniel Phillips [this message]
2022-08-22 15:53 ` [PATCH 1/1] drm/amdgpu: Use kfd_lock_pdd_by_id helper in more places Deucher, Alexander
2022-08-22 18:00 ` Felix Kuehling

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220822153003.6349-1-daniel.phillips@amd.com \
    --to=daniel.phillips@amd.com \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=felix.kuehling@amd.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.