Skip to contentSkip to conversation

drm/amdkfd: drop process lock in kunmap signal BO

PATCH New Yongqiang Sun 1 participant 1 message Dec 3

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

PATCH 2/2 drm/amdkfd: drop process lock in kunmap signal BO

kfd_process_kunmap_signal_bo() is only called from kfd_process_wq_release(), after the last reference to the process has been dropped, so no other task can reach the process. The caller already states that no process locking is needed. Holding p->mutex here means a task that died while holding it would block the release worker forever, so remove the lock.

kfd_process_device_free_bos() ignored failures from amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu() and amdgpu_amdkfd_gpuvm_free_memory_of_gpu(). Log those failures so a BO that cannot be freed is visible. The handle is still removed as before. A failed free leaves the GPU VA mapping in the VM, so this change does not fix the stale VM pointer that can follow.

---
 drivers/gpu/drm/amd/amdkfd/kfd_process.c | 28 +++++++++++++++---------
 1 file changed, 18 insertions(+), 10 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_process.c +18 -10
index 0a76ae0768074..aabefc092441e 100644
@@ -1114,6 +1114,7 @@ static void kfd_process_device_free_bos(struct kfd_process_device *pdd)
 	void *mem;
 	int id;
 	int i;
+	int ret;
 
 	/*
 	 * Remove all handles from idr and release appropriate
@@ -1126,12 +1127,23 @@ static void kfd_process_device_free_bos(struct kfd_process_device *pdd)
 
 			if (!peer_pdd->drm_priv)
 				continue;
-			amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
+			ret = amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
 				peer_pdd->dev->adev, mem, peer_pdd->drm_priv);
+			if (ret)
+				pr_warn("pid %d: unmap handle %d from GPU 0x%x failed: %d\n",
+					p->lead_thread->pid, id, peer_pdd->dev->id, ret);
 		}
 
-		amdgpu_amdkfd_gpuvm_free_memory_of_gpu(pdd->dev->adev, mem,
-						       pdd->drm_priv, NULL);
+		/*
+		 * On failure the BO is not freed and its GPU VA mapping stays
+		 * in the VM. The handle is removed anyway, so the BO can no
+		 * longer be reached from this process.
+		 */
+		ret = amdgpu_amdkfd_gpuvm_free_memory_of_gpu(pdd->dev->adev, mem,
+							     pdd->drm_priv, NULL);
+		if (ret)
+			pr_warn("pid %d: free handle %d on GPU 0x%x failed: %d, BO not freed\n",
+				p->lead_thread->pid, id, pdd->dev->id, ret);
 		kfd_process_device_remove_obj_handle(pdd, id);
 	}
 }
@@ -1146,25 +1158,21 @@ static void kfd_process_kunmap_signal_bo(struct kfd_process *p)
 	struct kfd_node *kdev;
 	void *mem;
 
+	/* Called from kfd_process_wq_release(), no process lock needed */
 	kdev = kfd_device_by_id(GET_GPU_ID(p->signal_handle));
 	if (!kdev)
 		return;
 
-	mutex_lock(&p->mutex);
-
 	pdd = kfd_get_process_device_data(kdev, p);
 	if (!pdd)
-		goto out;
+		return;
 
 	mem = kfd_process_device_translate_handle(
 		pdd, GET_IDR_HANDLE(p->signal_handle));
 	if (!mem)
-		goto out;
+		return;
 
 	amdgpu_amdkfd_gpuvm_unmap_bo_from_kernel(mem);
-
-out:
-	mutex_unlock(&p->mutex);
 }
 
 static void kfd_process_free_outstanding_kfd_bos(struct kfd_process *p)
Signature · 1 line
2.43.0