Skip to contentSkip to conversation

drm/amdkfd: cleanup device pointer dereference chains

PATCH In review Yunxiang Li 2 participants 4 messages Dec 3, 2024 – Dec 4, 2024

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

PATCH v3 1/2 drm/amdkfd: cleanup device pointer dereference chains

Pull out some duplicated dereference chains into variables, and in some cases grab struct device pointer directly from amdgpu_device instead of via drm_device.

---
v3: no change

 drivers/gpu/drm/amd/amdkfd/kfd_process.c | 29 ++++++++++++------------
 1 file changed, 15 insertions(+), 14 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_process.c +15 -14
index 87cd52cf4ee99..555a892fcf963 100644
@@ -1055,6 +1055,7 @@ static void kfd_process_destroy_pdds(struct kfd_process *p)
 
 	for (i = 0; i < p->n_pdds; i++) {
 		struct kfd_process_device *pdd = p->pdds[i];
+		struct amdgpu_device *adev = pdd->dev->adev;
 
 		pr_debug("Releasing pdd (topology id %d) for process (pasid 0x%x)\n",
 				pdd->dev->id, p->pasid);
@@ -1063,8 +1064,8 @@ static void kfd_process_destroy_pdds(struct kfd_process *p)
 		kfd_process_device_destroy_ib_mem(pdd);
 
 		if (pdd->drm_file) {
-			amdgpu_amdkfd_gpuvm_release_process_vm(
-					pdd->dev->adev, pdd->drm_priv);
+			amdgpu_amdkfd_gpuvm_release_process_vm(adev,
+							       pdd->drm_priv);
 			fput(pdd->drm_file);
 		}
 
@@ -1077,15 +1078,14 @@ static void kfd_process_destroy_pdds(struct kfd_process *p)
 		kfd_free_process_doorbells(pdd->dev->kfd, pdd);
 
 		if (pdd->dev->kfd->shared_resources.enable_mes)
-			amdgpu_amdkfd_free_gtt_mem(pdd->dev->adev,
-						   &pdd->proc_ctx_bo);
+			amdgpu_amdkfd_free_gtt_mem(adev, &pdd->proc_ctx_bo);
 		/*
 		 * before destroying pdd, make sure to report availability
 		 * for auto suspend
 		 */
 		if (pdd->runtime_inuse) {
-			pm_runtime_mark_last_busy(adev_to_drm(pdd->dev->adev)->dev);
-			pm_runtime_put_autosuspend(adev_to_drm(pdd->dev->adev)->dev);
+			pm_runtime_mark_last_busy(adev->dev);
+			pm_runtime_put_autosuspend(adev->dev);
 			pdd->runtime_inuse = false;
 		}
 
@@ -1608,6 +1608,8 @@ struct kfd_process_device *kfd_create_process_device_data(struct kfd_node *dev,
 							struct kfd_process *p)
 {
 	struct kfd_process_device *pdd = NULL;
+	struct amdgpu_device *adev = dev->adev;
+	struct device *bdev = adev->dev;
 	int retval = 0;
 
 	if (WARN_ON_ONCE(p->n_pdds >= MAX_GPU_INSTANCE))
@@ -1633,14 +1635,14 @@ struct kfd_process_device *kfd_create_process_device_data(struct kfd_node *dev,
 	atomic64_set(&pdd->evict_duration_counter, 0);
 
 	if (dev->kfd->shared_resources.enable_mes) {
-		retval = amdgpu_amdkfd_alloc_gtt_mem(dev->adev,
+		retval = amdgpu_amdkfd_alloc_gtt_mem(adev,
 						AMDGPU_MES_PROC_CTX_SIZE,
 						&pdd->proc_ctx_bo,
 						&pdd->proc_ctx_gpu_addr,
 						&pdd->proc_ctx_cpu_ptr,
 						false);
 		if (retval) {
-			dev_err(dev->adev->dev,
+			dev_err(bdev,
 				"failed to allocate process context bo\n");
 			goto err_free_pdd;
 		}
@@ -1649,10 +1651,8 @@ struct kfd_process_device *kfd_create_process_device_data(struct kfd_node *dev,
 
 	p->pdds[p->n_pdds++] = pdd;
 	if (kfd_dbg_is_per_vmid_supported(pdd->dev))
-		pdd->spi_dbg_override = pdd->dev->kfd2kgd->disable_debug_trap(
-							pdd->dev->adev,
-							false,
-							0);
+		pdd->spi_dbg_override =
+			pdd->dev->kfd2kgd->disable_debug_trap(adev, false, 0);
 
 	/* Init idr used for memory handle translation */
 	idr_init(&pdd->alloc_idr);
@@ -1752,11 +1752,12 @@ struct kfd_process_device *kfd_bind_process_to_device(struct kfd_node *dev,
 							struct kfd_process *p)
 {
 	struct kfd_process_device *pdd;
+	struct device *bdev = dev->adev->dev;
 	int err;
 
 	pdd = kfd_get_process_device_data(dev, p);
 	if (!pdd) {
-		dev_err(dev->adev->dev, "Process device data doesn't exist\n");
+		dev_err(bdev, "Process device data doesn't exist\n");
 		return ERR_PTR(-ENOMEM);
 	}
 
@@ -1769,7 +1770,7 @@ struct kfd_process_device *kfd_bind_process_to_device(struct kfd_node *dev,
 	 * pdd is destroyed.
 	 */
 	if (!pdd->runtime_inuse) {
-		err = pm_runtime_get_sync(adev_to_drm(dev->adev)->dev);
+		err = pm_runtime_get_sync(bdev);
 		if (err < 0) {
 			pm_runtime_put_autosuspend(adev_to_drm(dev->adev)->dev);
 			return ERR_PTR(err);
Signature · 1 line
2.34.1
#1 lore raw

PATCH v3 2/2 drm/amdkfd: pause autosuspend when creating pdd

When using MES creating a pdd will require talking to the GPU to setup the relevant context. The code here forgot to wake up the GPU in case it was in suspend, this causes KVM to EFAULT for passthrough GPU for example. This issue can be masked if the GPU was woken up by other things (e.g. opening the KMS node) first and have not yet gone to sleep.

Fixes cc009e613de6 ("drm/amdkfd: Add KFD support for soc21 v3")

---
v3: remove the cleanup in kfd_bind_process_to_device and document why this issue doesn't always happen

 drivers/gpu/drm/amd/amdkfd/kfd_process.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_process.c +7 -0
index 555a892fcf963..c81c020af75d1 100644
@@ -1635,12 +1635,19 @@ struct kfd_process_device *kfd_create_process_device_data(struct kfd_node *dev,
 	atomic64_set(&pdd->evict_duration_counter, 0);
 
 	if (dev->kfd->shared_resources.enable_mes) {
+		retval = pm_runtime_resume_and_get(bdev);
+		if (retval < 0) {
+			pr_err("failed to stop autosuspend\n");
+			goto err_free_pdd;
+		}
 		retval = amdgpu_amdkfd_alloc_gtt_mem(adev,
 						AMDGPU_MES_PROC_CTX_SIZE,
 						&pdd->proc_ctx_bo,
 						&pdd->proc_ctx_gpu_addr,
 						&pdd->proc_ctx_cpu_ptr,
 						false);
+		pm_runtime_mark_last_busy(bdev);
+		pm_runtime_put_autosuspend(bdev);
 		if (retval) {
 			dev_err(bdev,
 				"failed to allocate process context bo\n");
Signature · 1 line
2.34.1
maintainer
#2 lore raw

Re: [PATCH v3 2/2] drm/amdkfd: pause autosuspend when creating pdd

Quoted 24 lines · Yunxiang Li

When using MES creating a pdd will require talking to the GPU to setup the relevant context. The code here forgot to wake up the GPU in case it was in suspend, this causes KVM to EFAULT for passthrough GPU for example. This issue can be masked if the GPU was woken up by other things (e.g. opening the KMS node) first and have not yet gone to sleep.

Fixes cc009e613de6 ("drm/amdkfd: Add KFD support for soc21 v3")

---
v3: remove the cleanup in kfd_bind_process_to_device and document why this issue doesn't always happen

  drivers/gpu/drm/amd/amdkfd/kfd_process.c | 7 +++++++
  1 file changed, 7 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_process.c +1 -0
index 555a892fcf963..c81c020af75d1 100644
@@ -1635,12 +1635,19 @@ struct kfd_process_device *kfd_create_process_device_data(struct kfd_node *dev,
  	atomic64_set(&pdd->evict_duration_counter, 0);
 
  	if (dev->kfd->shared_resources.enable_mes) {
+		retval = pm_runtime_resume_and_get(bdev);

drivers/gpu/drm/amd/amdkfd/kfd_process.c struct kfd_process_device *kfd_create_process_device_data(struct kfd_node *dev, · line 1648

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +4 -0
+		if (retval < 0) {
+			pr_err("failed to stop autosuspend\n");
+			goto err_free_pdd;
+		}
  		retval = amdgpu_amdkfd_alloc_gtt_mem(adev,
  						AMDGPU_MES_PROC_CTX_SIZE,
  						&pdd->proc_ctx_bo,
  						&pdd->proc_ctx_gpu_addr,
  						&pdd->proc_ctx_cpu_ptr,
  						false);

As far as I can see from grepping the code, this BO is never used. It is allocated here and freed in kfd_process_destroy_pdds, and that's it.

I see a different proc_ctx_bo allocation in amdgpu_mes_create_process but I don't see that function being called anywhere. Either my grep-Fu is getting rusty, or there is some dead code and data structures surrounding MES here.

So unless I'm missing something, we can just remove this proc_ctx_bo completely.

Regards,
  Felix

Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +2 -0
+		pm_runtime_mark_last_busy(bdev);
+		pm_runtime_put_autosuspend(bdev);
  		if (retval) {
  			dev_err(bdev,
  				"failed to allocate process context bo\n");
maintainer
#3 lore raw

Re: [PATCH v3 2/2] drm/amdkfd: pause autosuspend when creating pdd

Quoted 39 lines · Felix Kuehling
Quoted 24 lines · Yunxiang Li

When using MES creating a pdd will require talking to the GPU to setup the relevant context. The code here forgot to wake up the GPU in case it was in suspend, this causes KVM to EFAULT for passthrough GPU for example. This issue can be masked if the GPU was woken up by other things (e.g. opening the KMS node) first and have not yet gone to sleep.

Fixes cc009e613de6 ("drm/amdkfd: Add KFD support for soc21 v3")

---
v3: remove the cleanup in kfd_bind_process_to_device and document why this issue doesn't always happen

  drivers/gpu/drm/amd/amdkfd/kfd_process.c | 7 +++++++

  1 file changed, 7 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_process.c +1 -0
index 555a892fcf963..c81c020af75d1 100644
@@ -1635,12 +1635,19 @@ struct kfd_process_device 
 *kfd_create_process_device_data(struct kfd_node *dev,
       atomic64_set(&pdd->evict_duration_counter, 0);
         if (dev->kfd->shared_resources.enable_mes) {
+        retval = pm_runtime_resume_and_get(bdev);

drivers/gpu/drm/amd/amdkfd/kfd_process.c struct kfd_process_device · line 1648

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +4 -0
+        if (retval < 0) {
+            pr_err("failed to stop autosuspend\n");
+            goto err_free_pdd;
+        }
           retval = amdgpu_amdkfd_alloc_gtt_mem(adev,
                           AMDGPU_MES_PROC_CTX_SIZE,
                           &pdd->proc_ctx_bo,
                           &pdd->proc_ctx_gpu_addr,
                           &pdd->proc_ctx_cpu_ptr,
                           false);

As far as I can see from grepping the code, this BO is never used. It is allocated here and freed in kfd_process_destroy_pdds, and that's it.

Quoted 3 lines

I see a different proc_ctx_bo allocation in amdgpu_mes_create_process but I don't see that function being called anywhere. Either my grep-Fu is getting rusty, or there is some dead code and data structures surrounding MES here.

So unless I'm missing something, we can just remove this proc_ctx_bo completely.

OK, I was missing that proc_ctx_gpu_addr is used in add_queue_mes.

One other suggestion would be to do the allocation of proc_ctx_bo in a lazy fashion when the first queue is created in a process. Then it would naturally happen after the GPU has been taken out of runtime-PM in kfd_bind_process_to_device. This would avoid unnecessarily waking up all GPUs in the system every time a KFD process is started.

Regards,
  Felix

Quoted 11 lines

Regards,
  Felix

Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_process.c +2 -0
+        pm_runtime_mark_last_busy(bdev);
+        pm_runtime_put_autosuspend(bdev);
           if (retval) {
               dev_err(bdev,
                   "failed to allocate process context bo\n");