Skip to contentSkip to conversation

drm/amdkfd: reserve the BO before validating it

PATCH Superseded Lang Yu 2 participants 7 messages Jan 22, 2024 – Jan 30, 2024

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Lang Yu

author
#0 lore raw

PATCH v2 1/1 drm/amdkfd: reserve the BO before validating it

Fixes 410f08516e0f ("drm/amdkfd: Move dma unmapping after TLB flush")

v2:
Avoid unmapping attachment twice when ERESTARTSYS.

[   41.708711] WARNING: CPU: 0 PID: 1463 at drivers/gpu/drm/ttm/ttm_bo.c:846 ttm_bo_validate+0x146/0x1b0 [ttm]
[   41.708989] Call Trace:
[   41.708992]  <TASK>
[   41.708996]  ? show_regs+0x6c/0x80
[   41.709000]  ? ttm_bo_validate+0x146/0x1b0 [ttm]
[   41.709008]  ? __warn+0x93/0x190
[   41.709014]  ? ttm_bo_validate+0x146/0x1b0 [ttm]
[   41.709024]  ? report_bug+0x1f9/0x210
[   41.709035]  ? handle_bug+0x46/0x80
[   41.709041]  ? exc_invalid_op+0x1d/0x80
[   41.709048]  ? asm_exc_invalid_op+0x1f/0x30
[   41.709057]  ? amdgpu_amdkfd_gpuvm_dmaunmap_mem+0x2c/0x80 [amdgpu]
[   41.709185]  ? ttm_bo_validate+0x146/0x1b0 [ttm]
[   41.709197]  ? amdgpu_amdkfd_gpuvm_dmaunmap_mem+0x2c/0x80 [amdgpu]
[   41.709337]  ? srso_alias_return_thunk+0x5/0x7f
[   41.709346]  kfd_mem_dmaunmap_attachment+0x9e/0x1e0 [amdgpu]
[   41.709467]  amdgpu_amdkfd_gpuvm_dmaunmap_mem+0x56/0x80 [amdgpu]
[   41.709586]  kfd_ioctl_unmap_memory_from_gpu+0x1b7/0x300 [amdgpu]
[   41.709710]  kfd_ioctl+0x1ec/0x650 [amdgpu]
[   41.709822]  ? __pfx_kfd_ioctl_unmap_memory_from_gpu+0x10/0x10 [amdgpu]
[   41.709945]  ? srso_alias_return_thunk+0x5/0x7f
[   41.709949]  ? tomoyo_file_ioctl+0x20/0x30
[   41.709959]  __x64_sys_ioctl+0x9c/0xd0
[   41.709967]  do_syscall_64+0x3f/0x90
[   41.709973]  entry_SYSCALL_64_after_hwframe+0x6e/0xd8
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h    |  2 +-
 .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  | 28 +++++++++++++++++--
 drivers/gpu/drm/amd/amdkfd/kfd_chardev.c      |  4 ++-
 3 files changed, 29 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +1 -1
index 584a0cea5572..41854417e487 100644
@@ -311,7 +311,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, void *drm_priv);
+int amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem, void *drm_priv);
 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 +25 -3
index 6f3a4cb2a9ef..7a050d46fa4d 100644
@@ -2088,21 +2088,43 @@ int amdgpu_amdkfd_gpuvm_map_memory_to_gpu(
 	return ret;
 }
 
-void amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem, void *drm_priv)
+int amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem, void *drm_priv)
 {
 	struct kfd_mem_attachment *entry;
 	struct amdgpu_vm *vm;
+	bool reserved = false;
+	int ret = 0;
 
 	vm = drm_priv_to_vm(drm_priv);
 
 	mutex_lock(&mem->lock);
 
 	list_for_each_entry(entry, &mem->attachments, list) {
-		if (entry->bo_va->base.vm == vm)
-			kfd_mem_dmaunmap_attachment(mem, entry);
+		if (entry->bo_va->base.vm != vm)
+			continue;
+		if (entry->type == KFD_MEM_ATT_SHARED ||
+		    entry->type == KFD_MEM_ATT_DMABUF)
+			continue;
+		if (!entry->bo_va->base.bo->tbo.ttm->sg)
+			continue;
+
+		if (!reserved) {
+			ret = amdgpu_bo_reserve(mem->bo, true);
+			if (ret)
+				goto out;
+			reserved = true;
+		}
+
+		kfd_mem_dmaunmap_attachment(mem, entry);
 	}
 
+	if (reserved)
+		amdgpu_bo_unreserve(mem->bo);
+
+out:
 	mutex_unlock(&mem->lock);
+
+	return ret;
 }
 
 int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +3 -1
index ce4c52ec34d8..80e90fdef291 100644
@@ -1442,7 +1442,9 @@ static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
 			kfd_flush_tlb(peer_pdd, TLB_FLUSH_HEAVYWEIGHT);
 
 		/* Remove dma mapping after tlb flush to avoid IO_PAGE_FAULT */
-		amdgpu_amdkfd_gpuvm_dmaunmap_mem(mem, peer_pdd->drm_priv);
+		err = amdgpu_amdkfd_gpuvm_dmaunmap_mem(mem, peer_pdd->drm_priv);
+		if (err)
+			goto sync_memory_failed;
 	}
 
 	mutex_unlock(&p->mutex);
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 71 lines · Lang Yu
Fixes 410f08516e0f ("drm/amdkfd: Move dma unmapping after TLB flush")

v2:
Avoid unmapping attachment twice when ERESTARTSYS.

[   41.708711] WARNING: CPU: 0 PID: 1463 at drivers/gpu/drm/ttm/ttm_bo.c:846 ttm_bo_validate+0x146/0x1b0 [ttm]
[   41.708989] Call Trace:
[   41.708992]  <TASK>
[   41.708996]  ? show_regs+0x6c/0x80
[   41.709000]  ? ttm_bo_validate+0x146/0x1b0 [ttm]
[   41.709008]  ? __warn+0x93/0x190
[   41.709014]  ? ttm_bo_validate+0x146/0x1b0 [ttm]
[   41.709024]  ? report_bug+0x1f9/0x210
[   41.709035]  ? handle_bug+0x46/0x80
[   41.709041]  ? exc_invalid_op+0x1d/0x80
[   41.709048]  ? asm_exc_invalid_op+0x1f/0x30
[   41.709057]  ? amdgpu_amdkfd_gpuvm_dmaunmap_mem+0x2c/0x80 [amdgpu]
[   41.709185]  ? ttm_bo_validate+0x146/0x1b0 [ttm]
[   41.709197]  ? amdgpu_amdkfd_gpuvm_dmaunmap_mem+0x2c/0x80 [amdgpu]
[   41.709337]  ? srso_alias_return_thunk+0x5/0x7f
[   41.709346]  kfd_mem_dmaunmap_attachment+0x9e/0x1e0 [amdgpu]
[   41.709467]  amdgpu_amdkfd_gpuvm_dmaunmap_mem+0x56/0x80 [amdgpu]
[   41.709586]  kfd_ioctl_unmap_memory_from_gpu+0x1b7/0x300 [amdgpu]
[   41.709710]  kfd_ioctl+0x1ec/0x650 [amdgpu]
[   41.709822]  ? __pfx_kfd_ioctl_unmap_memory_from_gpu+0x10/0x10 [amdgpu]
[   41.709945]  ? srso_alias_return_thunk+0x5/0x7f
[   41.709949]  ? tomoyo_file_ioctl+0x20/0x30
[   41.709959]  __x64_sys_ioctl+0x9c/0xd0
[   41.709967]  do_syscall_64+0x3f/0x90
[   41.709973]  entry_SYSCALL_64_after_hwframe+0x6e/0xd8
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h    |  2 +-
  .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  | 28 +++++++++++++++++--
  drivers/gpu/drm/amd/amdkfd/kfd_chardev.c      |  4 ++-
  3 files changed, 29 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +1 -1
index 584a0cea5572..41854417e487 100644
@@ -311,7 +311,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, void *drm_priv);
+int amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem, void *drm_priv);
  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 +3 -1
index 6f3a4cb2a9ef..7a050d46fa4d 100644
@@ -2088,21 +2088,43 @@ int amdgpu_amdkfd_gpuvm_map_memory_to_gpu(
  	return ret;
  }
 
-void amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem, void *drm_priv)
+int amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem, void *drm_priv)
  {
  	struct kfd_mem_attachment *entry;
  	struct amdgpu_vm *vm;
+	bool reserved = false;
+	int ret = 0;
 
  	vm = drm_priv_to_vm(drm_priv);
 
  	mutex_lock(&mem->lock);
 

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c int amdgpu_amdkfd_gpuvm_map_memory_to_gpu( · line 2109

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +7 -2
  	list_for_each_entry(entry, &mem->attachments, list) {
-		if (entry->bo_va->base.vm == vm)
-			kfd_mem_dmaunmap_attachment(mem, entry);
+		if (entry->bo_va->base.vm != vm)
+			continue;
+		if (entry->type == KFD_MEM_ATT_SHARED ||
+		    entry->type == KFD_MEM_ATT_DMABUF)
+			continue;
+		if (!entry->bo_va->base.bo->tbo.ttm->sg)
+			continue;

You're going to great lengths to avoid the reservation when it's not needed by kfd_mem_dmaunmap_attachment. However, this feels a bit fragile. Any changes in the kfd_mem_dmaunmap_* functions could break this.

Quoted 26 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +15 -0
+
+		if (!reserved) {
+			ret = amdgpu_bo_reserve(mem->bo, true);
+			if (ret)
+				goto out;
+			reserved = true;
+		}
+
+		kfd_mem_dmaunmap_attachment(mem, entry);
  	}
 
+	if (reserved)
+		amdgpu_bo_unreserve(mem->bo);
+
+out:
  	mutex_unlock(&mem->lock);
+
+	return ret;
  }
 
  int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0 -0
index ce4c52ec34d8..80e90fdef291 100644

drivers/gpu/drm/amd/amdkfd/kfd_chardev.c static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep, · line 1447

Quoted 7 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +3 -1
@@ -1442,7 +1442,9 @@ static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep,
  			kfd_flush_tlb(peer_pdd, TLB_FLUSH_HEAVYWEIGHT);
 
  		/* Remove dma mapping after tlb flush to avoid IO_PAGE_FAULT */
-		amdgpu_amdkfd_gpuvm_dmaunmap_mem(mem, peer_pdd->drm_priv);
+		err = amdgpu_amdkfd_gpuvm_dmaunmap_mem(mem, peer_pdd->drm_priv);
+		if (err)
+			goto sync_memory_failed;

This handles the case that the system call got interrupted. But you're not handling the restart correctly. When the ioctl is restarted, you don't know how many dmaunmaps are already done. So you'd need to make sure that repeating the dmaunmap is safe in all cases. Or what David tried earlier, find a way to track the unmapping so you only try to dmaunmap on GPUs where it's actually dmamapped.

Regards,
  Felix

Quoted 3 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0 -0
  	}
 
  	mutex_unlock(&p->mutex);
author
#2 lore raw

[AMD Official Use Only - General]

Quoted 146 lines

-----Original Message-----
From: Kuehling, Felix <[email protected]>
Sent: Thursday, January 25, 2024 5:41 AM
To: Yu, Lang <[email protected]>; [email protected]
Cc: Francis, David <[email protected]>
Subject: Re: [PATCH v2] drm/amdkfd: reserve the BO before validating it

Quoted 6 lines · Lang Yu
Fixes 410f08516e0f ("drm/amdkfd: Move dma unmapping after TLB flush")

v2:
Avoid unmapping attachment twice when ERESTARTSYS.

[   41.708711] WARNING: CPU: 0 PID: 1463 at

drivers/gpu/drm/ttm/ttm_bo.c:846 ttm_bo_validate+0x146/0x1b0 [ttm]

Quoted 11 lines
[   41.708989] Call Trace:
[   41.708992]  <TASK>
[   41.708996]  ? show_regs+0x6c/0x80
[   41.709000]  ? ttm_bo_validate+0x146/0x1b0 [ttm]
[   41.709008]  ? __warn+0x93/0x190
[   41.709014]  ? ttm_bo_validate+0x146/0x1b0 [ttm]
[   41.709024]  ? report_bug+0x1f9/0x210
[   41.709035]  ? handle_bug+0x46/0x80
[   41.709041]  ? exc_invalid_op+0x1d/0x80
[   41.709048]  ? asm_exc_invalid_op+0x1f/0x30
[   41.709057]  ? amdgpu_amdkfd_gpuvm_dmaunmap_mem+0x2c/0x80

[amdgpu]

Quoted 2 lines
[   41.709185]  ? ttm_bo_validate+0x146/0x1b0 [ttm]
[   41.709197]  ? amdgpu_amdkfd_gpuvm_dmaunmap_mem+0x2c/0x80

[amdgpu]

Quoted 3 lines
[   41.709337]  ? srso_alias_return_thunk+0x5/0x7f
[   41.709346]  kfd_mem_dmaunmap_attachment+0x9e/0x1e0 [amdgpu]
[   41.709467]  amdgpu_amdkfd_gpuvm_dmaunmap_mem+0x56/0x80

[amdgpu]

Quoted 3 lines
[   41.709586]  kfd_ioctl_unmap_memory_from_gpu+0x1b7/0x300 [amdgpu]
[   41.709710]  kfd_ioctl+0x1ec/0x650 [amdgpu]
[   41.709822]  ? __pfx_kfd_ioctl_unmap_memory_from_gpu+0x10/0x10

[amdgpu]

Quoted 10 lines
[   41.709945]  ? srso_alias_return_thunk+0x5/0x7f
[   41.709949]  ? tomoyo_file_ioctl+0x20/0x30
[   41.709959]  __x64_sys_ioctl+0x9c/0xd0
[   41.709967]  do_syscall_64+0x3f/0x90
[   41.709973]  entry_SYSCALL_64_after_hwframe+0x6e/0xd8
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h    |  2 +-
  .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  | 28

+++++++++++++++++--

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h int

Quoted 9 lines
  drivers/gpu/drm/amd/amdkfd/kfd_chardev.c      |  4 ++-
  3 files changed, 29 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +0 -0
index 584a0cea5572..41854417e487 100644
@@ -311,7 +311,7 @@ int

amdgpu_amdkfd_gpuvm_map_memory_to_gpu(struct amdgpu_device *adev,

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h int · line 311

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +0 -0
                                       struct kgd_mem *mem, void

*drm_priv);

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h int · line 313

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +0 -0
  int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
             struct amdgpu_device *adev, struct kgd_mem *mem, void

*drm_priv);

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c int

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +2 -1
-void amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem, void
 *drm_priv);
+int amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem, void
+*drm_priv);
  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 6f3a4cb2a9ef..7a050d46fa4d 100644
@@ -2088,21 +2088,43 @@ int

amdgpu_amdkfd_gpuvm_map_memory_to_gpu(

Quoted 17 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +4 -1
     return ret;
  }
 
-void amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem, void
 *drm_priv)
+int amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem, void
+*drm_priv)
  {
     struct kfd_mem_attachment *entry;
     struct amdgpu_vm *vm;
+    bool reserved = false;
+    int ret = 0;
 
     vm = drm_priv_to_vm(drm_priv);
 
     mutex_lock(&mem->lock);
 

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c int amdgpu_amdkfd_gpuvm_map_memory_to_gpu( · line 2109

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +7 -2
     list_for_each_entry(entry, &mem->attachments, list) {
-            if (entry->bo_va->base.vm == vm)
-                    kfd_mem_dmaunmap_attachment(mem, entry);
+            if (entry->bo_va->base.vm != vm)
+                    continue;
+            if (entry->type == KFD_MEM_ATT_SHARED ||
+                entry->type == KFD_MEM_ATT_DMABUF)
+                    continue;
+            if (!entry->bo_va->base.bo->tbo.ttm->sg)
+                    continue;

You're going to great lengths to avoid the reservation when it's not needed by kfd_mem_dmaunmap_attachment. However, this feels a bit fragile. Any changes in the kfd_mem_dmaunmap_* functions could break this.

drivers/gpu/drm/amd/amdkfd/kfd_chardev.c static int kfd_ioctl_unmap_memory_from_gpu(struct

Quoted 27 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +15 -0
+
+            if (!reserved) {
+                    ret = amdgpu_bo_reserve(mem->bo, true);
+                    if (ret)
+                            goto out;
+                    reserved = true;
+            }
+
+            kfd_mem_dmaunmap_attachment(mem, entry);
     }
 
+    if (reserved)
+            amdgpu_bo_unreserve(mem->bo);
+
+out:
     mutex_unlock(&mem->lock);
+
+    return ret;
  }
 
  int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0 -0
index ce4c52ec34d8..80e90fdef291 100644
@@ -1442,7 +1442,9 @@ static int kfd_ioctl_unmap_memory_from_gpu(struct

file *filep,

drivers/gpu/drm/amd/amdkfd/kfd_chardev.c static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep,

Quoted 3 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0 -0
                     kfd_flush_tlb(peer_pdd, TLB_FLUSH_HEAVYWEIGHT);
 
             /* Remove dma mapping after tlb flush to avoid IO_PAGE_FAULT

*/

drivers/gpu/drm/amd/amdkfd/kfd_chardev.c static int kfd_ioctl_unmap_memory_from_gpu(struct · line 1446

Quoted 3 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +1 -1
-            amdgpu_amdkfd_gpuvm_dmaunmap_mem(mem, peer_pdd-
 drm_priv);
+            err = amdgpu_amdkfd_gpuvm_dmaunmap_mem(mem,

peer_pdd->drm_priv);

Quoted 8 lines

drivers/gpu/drm/amd/amdkfd/kfd_chardev.c static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep, · line 1447

Quoted 2 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +2 -0
+            if (err)
+                    goto sync_memory_failed;

This handles the case that the system call got interrupted. But you're not handling the restart correctly. When the ioctl is restarted, you don't know how many dmaunmaps are already done. So you'd need to make sure that repeating the dmaunmap is safe in all cases. Or what David tried earlier, find a way to track the unmapping so you only try to dmaunmap on GPUs where it's actually dmamapped.

From previous discussion, no one likes add another variable to track the unmappings. So I'm avoiding adding another variable.

Actually, all memory attachments use sg_table, ttm->sg is NULL? can be used as an indicator to see whether an attachment is already unmapped. That already unmapped will not be repeated.

Regards,
Lang

Quoted 7 lines

Regards,
Felix

Quoted 3 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0 -0
     }
 
     mutex_unlock(&p->mutex);
maintainer
#3 lore raw
Quoted 154 lines · Lang

[AMD Official Use Only - General]

Quoted 144 lines

-----Original Message-----
From: Kuehling, Felix <[email protected]>
Sent: Thursday, January 25, 2024 5:41 AM
To: Yu, Lang <[email protected]>; [email protected]
Cc: Francis, David <[email protected]>
Subject: Re: [PATCH v2] drm/amdkfd: reserve the BO before validating it

Quoted 6 lines · Lang Yu
Fixes 410f08516e0f ("drm/amdkfd: Move dma unmapping after TLB flush")

v2:
Avoid unmapping attachment twice when ERESTARTSYS.

[   41.708711] WARNING: CPU: 0 PID: 1463 at

drivers/gpu/drm/ttm/ttm_bo.c:846 ttm_bo_validate+0x146/0x1b0 [ttm]

Quoted 11 lines
[   41.708989] Call Trace:
[   41.708992]  <TASK>
[   41.708996]  ? show_regs+0x6c/0x80
[   41.709000]  ? ttm_bo_validate+0x146/0x1b0 [ttm]
[   41.709008]  ? __warn+0x93/0x190
[   41.709014]  ? ttm_bo_validate+0x146/0x1b0 [ttm]
[   41.709024]  ? report_bug+0x1f9/0x210
[   41.709035]  ? handle_bug+0x46/0x80
[   41.709041]  ? exc_invalid_op+0x1d/0x80
[   41.709048]  ? asm_exc_invalid_op+0x1f/0x30
[   41.709057]  ? amdgpu_amdkfd_gpuvm_dmaunmap_mem+0x2c/0x80

[amdgpu]

Quoted 2 lines
[   41.709185]  ? ttm_bo_validate+0x146/0x1b0 [ttm]
[   41.709197]  ? amdgpu_amdkfd_gpuvm_dmaunmap_mem+0x2c/0x80

[amdgpu]

Quoted 3 lines
[   41.709337]  ? srso_alias_return_thunk+0x5/0x7f
[   41.709346]  kfd_mem_dmaunmap_attachment+0x9e/0x1e0 [amdgpu]
[   41.709467]  amdgpu_amdkfd_gpuvm_dmaunmap_mem+0x56/0x80

[amdgpu]

Quoted 3 lines
[   41.709586]  kfd_ioctl_unmap_memory_from_gpu+0x1b7/0x300 [amdgpu]
[   41.709710]  kfd_ioctl+0x1ec/0x650 [amdgpu]
[   41.709822]  ? __pfx_kfd_ioctl_unmap_memory_from_gpu+0x10/0x10

[amdgpu]

Quoted 10 lines
[   41.709945]  ? srso_alias_return_thunk+0x5/0x7f
[   41.709949]  ? tomoyo_file_ioctl+0x20/0x30
[   41.709959]  __x64_sys_ioctl+0x9c/0xd0
[   41.709967]  do_syscall_64+0x3f/0x90
[   41.709973]  entry_SYSCALL_64_after_hwframe+0x6e/0xd8
---
   drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h    |  2 +-
   .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  | 28

+++++++++++++++++--

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h int

Quoted 9 lines
   drivers/gpu/drm/amd/amdkfd/kfd_chardev.c      |  4 ++-
   3 files changed, 29 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +0 -0
index 584a0cea5572..41854417e487 100644
@@ -311,7 +311,7 @@ int

amdgpu_amdkfd_gpuvm_map_memory_to_gpu(struct amdgpu_device *adev,

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h int · line 311

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +0 -0
                                        struct kgd_mem *mem, void

*drm_priv);

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h int · line 313

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +0 -0
   int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
              struct amdgpu_device *adev, struct kgd_mem *mem, void

*drm_priv);

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c int

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +2 -1
-void amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem, void
 *drm_priv);
+int amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem, void
+*drm_priv);
   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 6f3a4cb2a9ef..7a050d46fa4d 100644
@@ -2088,21 +2088,43 @@ int

amdgpu_amdkfd_gpuvm_map_memory_to_gpu(

Quoted 17 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +4 -1
      return ret;
   }
 
-void amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem, void
 *drm_priv)
+int amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem, void
+*drm_priv)
   {
      struct kfd_mem_attachment *entry;
      struct amdgpu_vm *vm;
+    bool reserved = false;
+    int ret = 0;
 
      vm = drm_priv_to_vm(drm_priv);
 
      mutex_lock(&mem->lock);
 

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c int amdgpu_amdkfd_gpuvm_map_memory_to_gpu( · line 2109

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +7 -2
      list_for_each_entry(entry, &mem->attachments, list) {
-            if (entry->bo_va->base.vm == vm)
-                    kfd_mem_dmaunmap_attachment(mem, entry);
+            if (entry->bo_va->base.vm != vm)
+                    continue;
+            if (entry->type == KFD_MEM_ATT_SHARED ||
+                entry->type == KFD_MEM_ATT_DMABUF)
+                    continue;
+            if (!entry->bo_va->base.bo->tbo.ttm->sg)
+                    continue;

You're going to great lengths to avoid the reservation when it's not needed by kfd_mem_dmaunmap_attachment. However, this feels a bit fragile. Any changes in the kfd_mem_dmaunmap_* functions could break this.

drivers/gpu/drm/amd/amdkfd/kfd_chardev.c static int kfd_ioctl_unmap_memory_from_gpu(struct

Quoted 27 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +15 -0
+
+            if (!reserved) {
+                    ret = amdgpu_bo_reserve(mem->bo, true);
+                    if (ret)
+                            goto out;
+                    reserved = true;
+            }
+
+            kfd_mem_dmaunmap_attachment(mem, entry);
      }
 
+    if (reserved)
+            amdgpu_bo_unreserve(mem->bo);
+
+out:
      mutex_unlock(&mem->lock);
+
+    return ret;
   }
 
   int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0 -0
index ce4c52ec34d8..80e90fdef291 100644
@@ -1442,7 +1442,9 @@ static int kfd_ioctl_unmap_memory_from_gpu(struct

file *filep,

drivers/gpu/drm/amd/amdkfd/kfd_chardev.c static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep,

Quoted 3 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0 -0
                      kfd_flush_tlb(peer_pdd, TLB_FLUSH_HEAVYWEIGHT);
 
              /* Remove dma mapping after tlb flush to avoid IO_PAGE_FAULT

*/

drivers/gpu/drm/amd/amdkfd/kfd_chardev.c static int kfd_ioctl_unmap_memory_from_gpu(struct · line 1446

Quoted 3 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +1 -1
-            amdgpu_amdkfd_gpuvm_dmaunmap_mem(mem, peer_pdd-
 drm_priv);
+            err = amdgpu_amdkfd_gpuvm_dmaunmap_mem(mem,
Quoted 8 lines

peer_pdd->drm_priv);

drivers/gpu/drm/amd/amdkfd/kfd_chardev.c static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep, · line 1447

Quoted 2 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +2 -0
+            if (err)
+                    goto sync_memory_failed;

This handles the case that the system call got interrupted. But you're not handling the restart correctly. When the ioctl is restarted, you don't know how many dmaunmaps are already done. So you'd need to make sure that repeating the dmaunmap is safe in all cases. Or what David tried earlier, find a way to track the unmapping so you only try to dmaunmap on GPUs where it's actually dmamapped.

Quoted 4 lines

From previous discussion, no one likes add another variable to track the unmappings. So I'm avoiding adding another variable.

Actually, all memory attachments use sg_table, ttm->sg is NULL? can be used as an indicator to see whether an attachment is already unmapped. That already unmapped will not be repeated.

I think that should work. I'd add the checks in kfd_mem_dmaunmap_userptr and kfd_mem_dmaunmap_sg_bo, where we also set ttm->sg to NULL. In fact, both those functions already have that check. So looks like it should handle the the restart correctly with your patch.

Regards,
  Felix

Quoted 11 lines

Regards,
Lang

Quoted 7 lines

Regards,
Felix

Quoted 3 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0 -0
      }
 
      mutex_unlock(&p->mutex);
author
#4 lore raw

[AMD Official Use Only - General]

Quoted 172 lines

-----Original Message-----
From: Kuehling, Felix <[email protected]>
Sent: Saturday, January 27, 2024 3:22 AM
To: Yu, Lang <[email protected]>; [email protected]
Cc: Francis, David <[email protected]>
Subject: Re: [PATCH v2] drm/amdkfd: reserve the BO before validating it

Quoted 159 lines · Lang

[AMD Official Use Only - General]

Quoted 157 lines

-----Original Message-----
From: Kuehling, Felix <[email protected]>
Sent: Thursday, January 25, 2024 5:41 AM
To: Yu, Lang <[email protected]>; [email protected]
Cc: Francis, David <[email protected]>
Subject: Re: [PATCH v2] drm/amdkfd: reserve the BO before validating it

Quoted 7 lines · Lang Yu
Fixes 410f08516e0f ("drm/amdkfd: Move dma unmapping after TLB

flush")

v2:
Avoid unmapping attachment twice when ERESTARTSYS.

[   41.708711] WARNING: CPU: 0 PID: 1463 at

drivers/gpu/drm/ttm/ttm_bo.c:846 ttm_bo_validate+0x146/0x1b0 [ttm]

Quoted 11 lines
[   41.708989] Call Trace:
[   41.708992]  <TASK>
[   41.708996]  ? show_regs+0x6c/0x80
[   41.709000]  ? ttm_bo_validate+0x146/0x1b0 [ttm]
[   41.709008]  ? __warn+0x93/0x190
[   41.709014]  ? ttm_bo_validate+0x146/0x1b0 [ttm]
[   41.709024]  ? report_bug+0x1f9/0x210
[   41.709035]  ? handle_bug+0x46/0x80
[   41.709041]  ? exc_invalid_op+0x1d/0x80
[   41.709048]  ? asm_exc_invalid_op+0x1f/0x30
[   41.709057]  ? amdgpu_amdkfd_gpuvm_dmaunmap_mem+0x2c/0x80

[amdgpu]

Quoted 2 lines
[   41.709185]  ? ttm_bo_validate+0x146/0x1b0 [ttm]
[   41.709197]  ? amdgpu_amdkfd_gpuvm_dmaunmap_mem+0x2c/0x80

[amdgpu]

Quoted 3 lines
[   41.709337]  ? srso_alias_return_thunk+0x5/0x7f
[   41.709346]  kfd_mem_dmaunmap_attachment+0x9e/0x1e0 [amdgpu]
[   41.709467]  amdgpu_amdkfd_gpuvm_dmaunmap_mem+0x56/0x80

[amdgpu]

Quoted 3 lines
[   41.709586]  kfd_ioctl_unmap_memory_from_gpu+0x1b7/0x300 [amdgpu]
[   41.709710]  kfd_ioctl+0x1ec/0x650 [amdgpu]
[   41.709822]  ? __pfx_kfd_ioctl_unmap_memory_from_gpu+0x10/0x10

[amdgpu]

Quoted 10 lines
[   41.709945]  ? srso_alias_return_thunk+0x5/0x7f
[   41.709949]  ? tomoyo_file_ioctl+0x20/0x30
[   41.709959]  __x64_sys_ioctl+0x9c/0xd0
[   41.709967]  do_syscall_64+0x3f/0x90
[   41.709973]  entry_SYSCALL_64_after_hwframe+0x6e/0xd8
---
   drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h    |  2 +-
   .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  | 28

+++++++++++++++++--

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h int

Quoted 9 lines
   drivers/gpu/drm/amd/amdkfd/kfd_chardev.c      |  4 ++-
   3 files changed, 29 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +0 -0
index 584a0cea5572..41854417e487 100644
@@ -311,7 +311,7 @@ int

amdgpu_amdkfd_gpuvm_map_memory_to_gpu(struct amdgpu_device *adev,

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h int · line 311

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +0 -0
                                        struct kgd_mem *mem, void

*drm_priv);

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h int · line 313

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +0 -0
   int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
              struct amdgpu_device *adev, struct kgd_mem *mem, void

*drm_priv);

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c int

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +2 -1
-void amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem, void
 *drm_priv);
+int amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem, void
+*drm_priv);
   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 6f3a4cb2a9ef..7a050d46fa4d 100644
@@ -2088,21 +2088,43 @@ int

amdgpu_amdkfd_gpuvm_map_memory_to_gpu(

Quoted 17 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +4 -1
      return ret;
   }
 
-void amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem, void
 *drm_priv)
+int amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem, void
+*drm_priv)
   {
      struct kfd_mem_attachment *entry;
      struct amdgpu_vm *vm;
+    bool reserved = false;
+    int ret = 0;
 
      vm = drm_priv_to_vm(drm_priv);
 
      mutex_lock(&mem->lock);
 

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c int amdgpu_amdkfd_gpuvm_map_memory_to_gpu( · line 2109

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +7 -2
      list_for_each_entry(entry, &mem->attachments, list) {
-            if (entry->bo_va->base.vm == vm)
-                    kfd_mem_dmaunmap_attachment(mem, entry);
+            if (entry->bo_va->base.vm != vm)
+                    continue;
+            if (entry->type == KFD_MEM_ATT_SHARED ||
+                entry->type == KFD_MEM_ATT_DMABUF)
+                    continue;
+            if (!entry->bo_va->base.bo->tbo.ttm->sg)
+                    continue;

You're going to great lengths to avoid the reservation when it's not needed by kfd_mem_dmaunmap_attachment. However, this feels a bit fragile. Any changes in the kfd_mem_dmaunmap_* functions could break this.

Quoted 27 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +15 -0
+
+            if (!reserved) {
+                    ret = amdgpu_bo_reserve(mem->bo, true);
+                    if (ret)
+                            goto out;
+                    reserved = true;
+            }
+
+            kfd_mem_dmaunmap_attachment(mem, entry);
      }
 
+    if (reserved)
+            amdgpu_bo_unreserve(mem->bo);
+
+out:
      mutex_unlock(&mem->lock);
+
+    return ret;
   }
 
   int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0 -0
index ce4c52ec34d8..80e90fdef291 100644
@@ -1442,7 +1442,9 @@ static int
Quoted 1 line

kfd_ioctl_unmap_memory_from_gpu(struct

file *filep,

Quoted 5 lines

kfd_flush_tlb(peer_pdd,
TLB_FLUSH_HEAVYWEIGHT);

              /* Remove dma mapping after tlb flush to avoid

IO_PAGE_FAULT

*/

Quoted 3 lines

- amdgpu_amdkfd_gpuvm_dmaunmap_mem(mem, peer_pdd-
drm_priv);
+ err = amdgpu_amdkfd_gpuvm_dmaunmap_mem(mem,

peer_pdd->drm_priv);

drivers/gpu/drm/amd/amdkfd/kfd_chardev.c static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep, · line 1447

Quoted 2 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +2 -0
+            if (err)
+                    goto sync_memory_failed;

This handles the case that the system call got interrupted. But you're not handling the restart correctly. When the ioctl is restarted, you don't know how many dmaunmaps are already done. So you'd need to make sure that repeating the dmaunmap is safe in all cases. Or what David tried earlier, find a way to track the unmapping so you

only try to dmaunmap on GPUs where it's actually dmamapped.

Quoted 1 line

From previous discussion, no one likes add another variable to track the

unmappings. So I'm avoiding adding another variable.

Quoted 1 line
Quoted 8 lines
Quoted 1 line

Actually, all memory attachments use sg_table, ttm->sg is NULL? can be used as

an indicator to see whether an attachment is already unmapped.

Quoted 1 line

That already unmapped will not be repeated.

I think that should work. I'd add the checks in kfd_mem_dmaunmap_userptr and kfd_mem_dmaunmap_sg_bo, where we also set ttm->sg to NULL. In fact, both those functions already have that check. So looks like it should handle the the restart correctly with your patch.

Yes, both kfd_mem_dmaunmap_userptr() and kfd_mem_dmaunmap_sg_bo() have NULL check for ttm->sg.

And dmabuf also have this check in amdgpu_ttm_backend_unbind(). So dmaunmap won't be repeated actually.

Then the benefits of handling ERESTARTSYS is avoiding amdgpu_bo_reserve().

What do you think? It's worth avoiding reservation in this case?

Regards,
Lang

Quoted 15 lines

Regards,
Felix

Quoted 11 lines

Regards,
Lang

Quoted 7 lines

Regards,
Felix

Quoted 3 lines
      }

      mutex_unlock(&p->mutex);
maintainer
#5 lore raw
Quoted 180 lines · Lang

[AMD Official Use Only - General]

Quoted 170 lines

-----Original Message-----
From: Kuehling, Felix <[email protected]>
Sent: Saturday, January 27, 2024 3:22 AM
To: Yu, Lang <[email protected]>; [email protected]
Cc: Francis, David <[email protected]>
Subject: Re: [PATCH v2] drm/amdkfd: reserve the BO before validating it

Quoted 159 lines · Lang

[AMD Official Use Only - General]

Quoted 157 lines

-----Original Message-----
From: Kuehling, Felix <[email protected]>
Sent: Thursday, January 25, 2024 5:41 AM
To: Yu, Lang <[email protected]>; [email protected]
Cc: Francis, David <[email protected]>
Subject: Re: [PATCH v2] drm/amdkfd: reserve the BO before validating it

Quoted 7 lines · Lang Yu
Fixes 410f08516e0f ("drm/amdkfd: Move dma unmapping after TLB

flush")

v2:
Avoid unmapping attachment twice when ERESTARTSYS.

[   41.708711] WARNING: CPU: 0 PID: 1463 at

drivers/gpu/drm/ttm/ttm_bo.c:846 ttm_bo_validate+0x146/0x1b0 [ttm]

Quoted 11 lines
[   41.708989] Call Trace:
[   41.708992]  <TASK>
[   41.708996]  ? show_regs+0x6c/0x80
[   41.709000]  ? ttm_bo_validate+0x146/0x1b0 [ttm]
[   41.709008]  ? __warn+0x93/0x190
[   41.709014]  ? ttm_bo_validate+0x146/0x1b0 [ttm]
[   41.709024]  ? report_bug+0x1f9/0x210
[   41.709035]  ? handle_bug+0x46/0x80
[   41.709041]  ? exc_invalid_op+0x1d/0x80
[   41.709048]  ? asm_exc_invalid_op+0x1f/0x30
[   41.709057]  ? amdgpu_amdkfd_gpuvm_dmaunmap_mem+0x2c/0x80

[amdgpu]

Quoted 2 lines
[   41.709185]  ? ttm_bo_validate+0x146/0x1b0 [ttm]
[   41.709197]  ? amdgpu_amdkfd_gpuvm_dmaunmap_mem+0x2c/0x80

[amdgpu]

Quoted 3 lines
[   41.709337]  ? srso_alias_return_thunk+0x5/0x7f
[   41.709346]  kfd_mem_dmaunmap_attachment+0x9e/0x1e0 [amdgpu]
[   41.709467]  amdgpu_amdkfd_gpuvm_dmaunmap_mem+0x56/0x80

[amdgpu]

Quoted 3 lines
[   41.709586]  kfd_ioctl_unmap_memory_from_gpu+0x1b7/0x300 [amdgpu]
[   41.709710]  kfd_ioctl+0x1ec/0x650 [amdgpu]
[   41.709822]  ? __pfx_kfd_ioctl_unmap_memory_from_gpu+0x10/0x10

[amdgpu]

Quoted 10 lines
[   41.709945]  ? srso_alias_return_thunk+0x5/0x7f
[   41.709949]  ? tomoyo_file_ioctl+0x20/0x30
[   41.709959]  __x64_sys_ioctl+0x9c/0xd0
[   41.709967]  do_syscall_64+0x3f/0x90
[   41.709973]  entry_SYSCALL_64_after_hwframe+0x6e/0xd8
---
    drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h    |  2 +-
    .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  | 28

+++++++++++++++++--

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h int

Quoted 9 lines
    drivers/gpu/drm/amd/amdkfd/kfd_chardev.c      |  4 ++-
    3 files changed, 29 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +0 -0
index 584a0cea5572..41854417e487 100644
@@ -311,7 +311,7 @@ int

amdgpu_amdkfd_gpuvm_map_memory_to_gpu(struct amdgpu_device *adev,

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h int · line 311

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +0 -0
                                         struct kgd_mem *mem, void

*drm_priv);

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h int · line 313

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +0 -0
    int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
               struct amdgpu_device *adev, struct kgd_mem *mem, void

*drm_priv);

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c int

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +2 -1
-void amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem, void
 *drm_priv);
+int amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem, void
+*drm_priv);
    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 6f3a4cb2a9ef..7a050d46fa4d 100644
@@ -2088,21 +2088,43 @@ int

amdgpu_amdkfd_gpuvm_map_memory_to_gpu(

Quoted 17 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +4 -1
       return ret;
    }
 
-void amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem, void
 *drm_priv)
+int amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem, void
+*drm_priv)
    {
       struct kfd_mem_attachment *entry;
       struct amdgpu_vm *vm;
+    bool reserved = false;
+    int ret = 0;
 
       vm = drm_priv_to_vm(drm_priv);
 
       mutex_lock(&mem->lock);
 

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c int amdgpu_amdkfd_gpuvm_map_memory_to_gpu( · line 2109

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +7 -2
       list_for_each_entry(entry, &mem->attachments, list) {
-            if (entry->bo_va->base.vm == vm)
-                    kfd_mem_dmaunmap_attachment(mem, entry);
+            if (entry->bo_va->base.vm != vm)
+                    continue;
+            if (entry->type == KFD_MEM_ATT_SHARED ||
+                entry->type == KFD_MEM_ATT_DMABUF)
+                    continue;
+            if (!entry->bo_va->base.bo->tbo.ttm->sg)
+                    continue;

You're going to great lengths to avoid the reservation when it's not needed by kfd_mem_dmaunmap_attachment. However, this feels a bit fragile. Any changes in the kfd_mem_dmaunmap_* functions could break this.

Quoted 27 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +15 -0
+
+            if (!reserved) {
+                    ret = amdgpu_bo_reserve(mem->bo, true);
+                    if (ret)
+                            goto out;
+                    reserved = true;
+            }
+
+            kfd_mem_dmaunmap_attachment(mem, entry);
       }
 
+    if (reserved)
+            amdgpu_bo_unreserve(mem->bo);
+
+out:
       mutex_unlock(&mem->lock);
+
+    return ret;
    }
 
    int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0 -0
index ce4c52ec34d8..80e90fdef291 100644
@@ -1442,7 +1442,9 @@ static int
Quoted 1 line

kfd_ioctl_unmap_memory_from_gpu(struct

file *filep,

Quoted 5 lines

kfd_flush_tlb(peer_pdd,
TLB_FLUSH_HEAVYWEIGHT);

               /* Remove dma mapping after tlb flush to avoid

IO_PAGE_FAULT

*/

Quoted 3 lines

- amdgpu_amdkfd_gpuvm_dmaunmap_mem(mem, peer_pdd-
drm_priv);
+ err = amdgpu_amdkfd_gpuvm_dmaunmap_mem(mem,

peer_pdd->drm_priv);

drivers/gpu/drm/amd/amdkfd/kfd_chardev.c static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep, · line 1447

Quoted 2 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +2 -0
+            if (err)
+                    goto sync_memory_failed;

This handles the case that the system call got interrupted. But you're not handling the restart correctly. When the ioctl is restarted, you don't know how many dmaunmaps are already done. So you'd need to make sure that repeating the dmaunmap is safe in all cases. Or what David tried earlier, find a way to track the unmapping so you

only try to dmaunmap on GPUs where it's actually dmamapped.

Quoted 1 line

From previous discussion, no one likes add another variable to track the

Quoted 8 lines

unmappings. So I'm avoiding adding another variable.

Quoted 1 line

Actually, all memory attachments use sg_table, ttm->sg is NULL? can be used as

an indicator to see whether an attachment is already unmapped.

Quoted 1 line

That already unmapped will not be repeated.

I think that should work. I'd add the checks in kfd_mem_dmaunmap_userptr and kfd_mem_dmaunmap_sg_bo, where we also set ttm->sg to NULL. In fact, both those functions already have that check. So looks like it should handle the the restart correctly with your patch.

Quoted 7 lines

Yes, both kfd_mem_dmaunmap_userptr() and kfd_mem_dmaunmap_sg_bo() have NULL check for ttm->sg.

And dmabuf also have this check in amdgpu_ttm_backend_unbind(). So dmaunmap won't be repeated actually.

Then the benefits of handling ERESTARTSYS is avoiding amdgpu_bo_reserve().

What do you think? It's worth avoiding reservation in this case?

I don't think it's worth the trouble. In fact, to avoid race conditions, you probably should take the reservation anyway before looking at ttm->sg.

Regards,
  Felix

Quoted 18 lines

Regards,
Lang

Quoted 14 lines

Regards,
Felix

Quoted 10 lines

Regards,
Lang

Quoted 7 lines

Regards,
Felix

Quoted 3 lines
       }

       mutex_unlock(&p->mutex);
author
#6 lore raw

[Public]

Quoted 206 lines

-----Original Message-----
From: Kuehling, Felix <[email protected]>
Sent: Monday, January 29, 2024 10:58 PM
To: Yu, Lang <[email protected]>; [email protected]
Cc: Francis, David <[email protected]>
Subject: Re: [PATCH v2] drm/amdkfd: reserve the BO before validating it

Quoted 51 lines · Lang

[AMD Official Use Only - General]

Quoted 49 lines

-----Original Message-----
From: Kuehling, Felix <[email protected]>
Sent: Saturday, January 27, 2024 3:22 AM
To: Yu, Lang <[email protected]>; [email protected]
Cc: Francis, David <[email protected]>
Subject: Re: [PATCH v2] drm/amdkfd: reserve the BO before validating it

Quoted 39 lines · Lang

[AMD Official Use Only - General]

Quoted 37 lines

-----Original Message-----
From: Kuehling, Felix <[email protected]>
Sent: Thursday, January 25, 2024 5:41 AM
To: Yu, Lang <[email protected]>; [email protected]
Cc: Francis, David <[email protected]>
Subject: Re: [PATCH v2] drm/amdkfd: reserve the BO before validating it

Quoted 7 lines · Lang Yu
Fixes 410f08516e0f ("drm/amdkfd: Move dma unmapping after TLB

flush")

v2:
Avoid unmapping attachment twice when ERESTARTSYS.

[   41.708711] WARNING: CPU: 0 PID: 1463 at

drivers/gpu/drm/ttm/ttm_bo.c:846 ttm_bo_validate+0x146/0x1b0 [ttm]

Quoted 11 lines
[   41.708989] Call Trace:
[   41.708992]  <TASK>
[   41.708996]  ? show_regs+0x6c/0x80
[   41.709000]  ? ttm_bo_validate+0x146/0x1b0 [ttm]
[   41.709008]  ? __warn+0x93/0x190
[   41.709014]  ? ttm_bo_validate+0x146/0x1b0 [ttm]
[   41.709024]  ? report_bug+0x1f9/0x210
[   41.709035]  ? handle_bug+0x46/0x80
[   41.709041]  ? exc_invalid_op+0x1d/0x80
[   41.709048]  ? asm_exc_invalid_op+0x1f/0x30
[   41.709057]  ? amdgpu_amdkfd_gpuvm_dmaunmap_mem+0x2c/0x80

[amdgpu]

Quoted 2 lines
[   41.709185]  ? ttm_bo_validate+0x146/0x1b0 [ttm]
[   41.709197]  ? amdgpu_amdkfd_gpuvm_dmaunmap_mem+0x2c/0x80

[amdgpu]

Quoted 3 lines
[   41.709337]  ? srso_alias_return_thunk+0x5/0x7f
[   41.709346]  kfd_mem_dmaunmap_attachment+0x9e/0x1e0 [amdgpu]
[   41.709467]  amdgpu_amdkfd_gpuvm_dmaunmap_mem+0x56/0x80

[amdgpu]

Quoted 1 line
[   41.709586]  kfd_ioctl_unmap_memory_from_gpu+0x1b7/0x300

[amdgpu]

Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 2 lines
[   41.709710]  kfd_ioctl+0x1ec/0x650 [amdgpu]
[   41.709822]  ? __pfx_kfd_ioctl_unmap_memory_from_gpu+0x10/0x10

[amdgpu]

Quoted 10 lines
[   41.709945]  ? srso_alias_return_thunk+0x5/0x7f
[   41.709949]  ? tomoyo_file_ioctl+0x20/0x30
[   41.709959]  __x64_sys_ioctl+0x9c/0xd0
[   41.709967]  do_syscall_64+0x3f/0x90
[   41.709973]  entry_SYSCALL_64_after_hwframe+0x6e/0xd8
---
    drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h    |  2 +-
    .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  | 28

+++++++++++++++++--

Quoted 3 lines
    drivers/gpu/drm/amd/amdkfd/kfd_chardev.c      |  4 ++-
    3 files changed, 29 insertions(+), 5 deletions(-)
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h int

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +0 -0
index 584a0cea5572..41854417e487 100644
@@ -311,7 +311,7 @@ int

amdgpu_amdkfd_gpuvm_map_memory_to_gpu(struct amdgpu_device

*adev,

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h int · line 311

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +0 -0
                                         struct kgd_mem *mem, void

*drm_priv);

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +0 -0
    int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
               struct amdgpu_device *adev, struct kgd_mem *mem,

void

*drm_priv);

Quoted 1 line

-void amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem,

void

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

*drm_priv);
+int amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem,

void

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c int

Quoted 11 lines
+*drm_priv);
    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, diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c index 6f3a4cb2a9ef..7a050d46fa4d 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0 -0
@@ -2088,21 +2088,43 @@ int

amdgpu_amdkfd_gpuvm_map_memory_to_gpu(

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c int amdgpu_amdkfd_gpuvm_map_memory_to_gpu(

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0 -1
       return ret;
    }
 
-void amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem,

void

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c int · line 2092

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +1 -0
 *drm_priv)
+int amdgpu_amdkfd_gpuvm_dmaunmap_mem(struct kgd_mem *mem,

void

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +10 -2
+*drm_priv)
    {
       struct kfd_mem_attachment *entry;
       struct amdgpu_vm *vm;
+    bool reserved = false;
+    int ret = 0;
 
       vm = drm_priv_to_vm(drm_priv);
 
       mutex_lock(&mem->lock);
 
       list_for_each_entry(entry, &mem->attachments, list) {
-            if (entry->bo_va->base.vm == vm)
-                    kfd_mem_dmaunmap_attachment(mem, entry);
+            if (entry->bo_va->base.vm != vm)
+                    continue;
+            if (entry->type == KFD_MEM_ATT_SHARED ||
+                entry->type == KFD_MEM_ATT_DMABUF)
+                    continue;
+            if (!entry->bo_va->base.bo->tbo.ttm->sg)
+                    continue;
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

You're going to great lengths to avoid the reservation when it's not needed by kfd_mem_dmaunmap_attachment. However, this feels a bit fragile. Any changes in the kfd_mem_dmaunmap_* functions could break

this.

Quoted 51 lines
Quoted 51 lines
Quoted 40 lines
Quoted 40 lines
Quoted 28 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +15 -0
+
+            if (!reserved) {
+                    ret = amdgpu_bo_reserve(mem->bo, true);
+                    if (ret)
+                            goto out;
+                    reserved = true;
+            }
+
+            kfd_mem_dmaunmap_attachment(mem, entry);
       }
 
+    if (reserved)
+            amdgpu_bo_unreserve(mem->bo);
+
+out:
       mutex_unlock(&mem->lock);
+
+    return ret;
    }
 
    int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +0 -0
index ce4c52ec34d8..80e90fdef291 100644
@@ -1442,7 +1442,9 @@ static int
Quoted 1 line

kfd_ioctl_unmap_memory_from_gpu(struct

file *filep,

Quoted 5 lines

kfd_flush_tlb(peer_pdd,
TLB_FLUSH_HEAVYWEIGHT);

               /* Remove dma mapping after tlb flush to avoid

IO_PAGE_FAULT

*/

Quoted 3 lines

- amdgpu_amdkfd_gpuvm_dmaunmap_mem(mem, peer_pdd-
drm_priv);
+ err = amdgpu_amdkfd_gpuvm_dmaunmap_mem(mem,

peer_pdd->drm_priv);

Quoted 8 lines
Quoted 8 lines

drivers/gpu/drm/amd/amdkfd/kfd_chardev.c static int kfd_ioctl_unmap_memory_from_gpu(struct file *filep, · line 1447

Quoted 2 lines
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +2 -0
+            if (err)
+                    goto sync_memory_failed;

This handles the case that the system call got interrupted. But you're not handling the restart correctly. When the ioctl is restarted, you don't know how many dmaunmaps are already done. So you'd need to make sure that repeating the dmaunmap is safe in all cases. Or what David tried earlier, find a way to track the unmapping so you

only try to dmaunmap on GPUs where it's actually dmamapped.

Quoted 2 lines

From previous discussion, no one likes add another variable to
track the

Quoted 7 lines
Quoted 7 lines

unmappings. So I'm avoiding adding another variable.

Quoted 2 lines

Actually, all memory attachments use sg_table, ttm->sg is NULL? can be used as

an indicator to see whether an attachment is already unmapped.

Quoted 1 line

That already unmapped will not be repeated.

I think that should work. I'd add the checks in kfd_mem_dmaunmap_userptr and kfd_mem_dmaunmap_sg_bo, where we

also

Quoted 3 lines
Quoted 2 lines

set ttm->sg to NULL. In fact, both those functions already have that check. So looks like it should handle the the restart correctly with your patch.

Yes, both kfd_mem_dmaunmap_userptr() and kfd_mem_dmaunmap_sg_bo()

have NULL check for ttm->sg.

Quoted 2 lines

And dmabuf also have this check in amdgpu_ttm_backend_unbind(). So

Quoted 8 lines

dmaunmap won't be repeated actually.

Quoted 4 lines

Then the benefits of handling ERESTARTSYS is avoiding amdgpu_bo_reserve().

What do you think? It's worth avoiding reservation in this case?

I don't think it's worth the trouble. In fact, to avoid race conditions, you probably should take the reservation anyway before looking at ttm->sg.

Got it. Thanks.

Regards,
Lang

Quoted 22 lines

Regards,
Felix

Quoted 18 lines

Regards,
Lang

Quoted 14 lines

Regards,
Felix

Quoted 10 lines

Regards,
Lang

Quoted 7 lines

Regards,
Felix

Quoted 3 lines
       }

       mutex_unlock(&p->mutex);