Skip to contentSkip to conversation

drm/amdkfd: Move dma unmapping after TLB flush

PATCH Superseded Philip Yang 2 participants 2 messages Sep 8, 2023

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/amdkfd: Move dma unmapping after TLB flush

Otherwise GPU may access the mapping to cause IOMMU IO_PAGE_FAULT.

Remove dma mapping before free the mem attachment, to fix potential dma mapping leaking if failed unmap from GPUs.

For queues restore path, because FW already flush TLB, it is safe to leave this unchanged.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h     |  1 +
 .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c   | 18 +++++++++++++++---
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c       |  4 ++++
 3 files changed, 20 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +1 -0
index 559f14cc0a99..9a1f8d09b1e9 100644
@@ -304,6 +304,7 @@ int amdgpu_amdkfd_gpuvm_map_memory_to_gpu(struct amdgpu_device *adev,
 					  struct kgd_mem *mem, void *drm_priv);
 int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
 		struct amdgpu_device *adev, struct kgd_mem *mem, void *drm_priv);
+void amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem);
 int amdgpu_amdkfd_gpuvm_sync_memory(
 		struct amdgpu_device *adev, struct kgd_mem *mem, bool intr);
 int amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(struct kgd_mem *mem,
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +15 -3
index b5b940485059..3c566050bed8 100644
@@ -1249,8 +1249,6 @@ static void unmap_bo_from_gpuvm(struct kgd_mem *mem,
 	amdgpu_vm_clear_freed(adev, vm, &bo_va->last_pt_update);
 
 	amdgpu_sync_fence(sync, bo_va->last_pt_update);
-
-	kfd_mem_dmaunmap_attachment(mem, entry);
 }
 
 static int update_gpuvm_pte(struct kgd_mem *mem,
@@ -1910,8 +1908,10 @@ int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
 		mem->va + bo_size * (1 + mem->aql_queue));
 
 	/* Remove from VM internal data structures */
-	list_for_each_entry_safe(entry, tmp, &mem->attachments, list)
+	list_for_each_entry_safe(entry, tmp, &mem->attachments, list) {
+		kfd_mem_dmaunmap_attachment(mem, entry);
 		kfd_mem_detach(entry);
+	}
 
 	ret = unreserve_bo_and_vms(&ctx, false, false);
 
@@ -2085,6 +2085,18 @@ int amdgpu_amdkfd_gpuvm_map_memory_to_gpu(
 	return ret;
 }
 
+void amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem)
+{
+	struct kfd_mem_attachment *entry;
+
+	mutex_lock(&mem->lock);
+
+	list_for_each_entry(entry, &mem->attachments, list)
+		kfd_mem_dmaunmap_attachment(mem, entry);
+
+	mutex_unlock(&mem->lock);
+}
+
 int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
 		struct amdgpu_device *adev, struct kgd_mem *mem, void *drm_priv)
 {
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +4 -0
index 08687ce0aa8b..fdcc55e324ea 100644
@@ -1443,6 +1443,10 @@ static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
 			kfd_flush_tlb(peer_pdd, TLB_FLUSH_HEAVYWEIGHT);
 		}
 	}
+
+	/* Remove IOMMU mapping after tlb flush to avoid IO_PAGE_FAULT race */
+	amdgpu_amdkfd_gpuvm_dmaunmap_mem(mem);
+
 	kfree(devices_arr);
 
 	return 0;
Signature · 1 line
2.35.1
maintainer
#1 lore raw
Quoted 32 lines · Philip Yang

Otherwise GPU may access the mapping to cause IOMMU IO_PAGE_FAULT.

Remove dma mapping before free the mem attachment, to fix potential dma mapping leaking if failed unmap from GPUs.

For queues restore path, because FW already flush TLB, it is safe to leave this unchanged.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h     |  1 +
  .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c   | 18 +++++++++++++++---
  drivers/gpu/drm/amd/amdkfd/kfd_chardev.c       |  4 ++++
  3 files changed, 20 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +1 -0
index 559f14cc0a99..9a1f8d09b1e9 100644
@@ -304,6 +304,7 @@ int amdgpu_amdkfd_gpuvm_map_memory_to_gpu(struct amdgpu_device *adev,
  					  struct kgd_mem *mem, void *drm_priv);
  int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
  		struct amdgpu_device *adev, struct kgd_mem *mem, void *drm_priv);
+void amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem);
  int amdgpu_amdkfd_gpuvm_sync_memory(
  		struct amdgpu_device *adev, struct kgd_mem *mem, bool intr);
  int amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(struct kgd_mem *mem,
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0 -0
index b5b940485059..3c566050bed8 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c static void unmap_bo_from_gpuvm(struct kgd_mem *mem, · line 1253

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0 -2
@@ -1249,8 +1249,6 @@ static void unmap_bo_from_gpuvm(struct kgd_mem *mem,
  	amdgpu_vm_clear_freed(adev, vm, &bo_va->last_pt_update);
 
  	amdgpu_sync_fence(sync, bo_va->last_pt_update);
-
-	kfd_mem_dmaunmap_attachment(mem, entry);

This probably won't work correctly when the same BO is unmapped and then mapped again to the same GPU.

Regards,
  Felix

Quoted 49 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +15 -1
  }
 
  static int update_gpuvm_pte(struct kgd_mem *mem,
@@ -1910,8 +1908,10 @@ int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
  		mem->va + bo_size * (1 + mem->aql_queue));
 
  	/* Remove from VM internal data structures */
-	list_for_each_entry_safe(entry, tmp, &mem->attachments, list)
+	list_for_each_entry_safe(entry, tmp, &mem->attachments, list) {
+		kfd_mem_dmaunmap_attachment(mem, entry);
  		kfd_mem_detach(entry);
+	}
 
  	ret = unreserve_bo_and_vms(&ctx, false, false);
 
@@ -2085,6 +2085,18 @@ int amdgpu_amdkfd_gpuvm_map_memory_to_gpu(
  	return ret;
  }
 
+void amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem)
+{
+	struct kfd_mem_attachment *entry;
+
+	mutex_lock(&mem->lock);
+
+	list_for_each_entry(entry, &mem->attachments, list)
+		kfd_mem_dmaunmap_attachment(mem, entry);
+
+	mutex_unlock(&mem->lock);
+}
+
  int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
  		struct amdgpu_device *adev, struct kgd_mem *mem, void *drm_priv)
  {
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +4 -0
index 08687ce0aa8b..fdcc55e324ea 100644
@@ -1443,6 +1443,10 @@ static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
  			kfd_flush_tlb(peer_pdd, TLB_FLUSH_HEAVYWEIGHT);
  		}
  	}
+
+	/* Remove IOMMU mapping after tlb flush to avoid IO_PAGE_FAULT race */
+	amdgpu_amdkfd_gpuvm_dmaunmap_mem(mem);
+
  	kfree(devices_arr);
 
  	return 0;