Skip to contentSkip to conversation

drm/amdgpu: Make sure to reserve BOs before adding or removing

PATCH In review Kent Russell 3 participants 4 messages Nov 1, 2021

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

PATCH 1/1 drm/amdgpu: Make sure to reserve BOs before adding or removing

BOs need to be reserved before they are added or removed, so ensure that they are reserved during kfd_mem_attach and kfd_mem_detach

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +10 -3
index 61784bbfd7fd..8d30ccd70800 100644
@@ -798,14 +798,19 @@ static int kfd_mem_attach(struct amdgpu_device *adev, struct kgd_mem *mem,
 		}
 
 		/* Add BO to VM internal data structures */
+		ret = amdgpu_bo_reserve(bo[i], false);
+		if (ret) {
+			pr_debug("Unable to reserve BO during memory attach");
+			goto unwind;
+		}
 		attachment[i]->bo_va = amdgpu_vm_bo_add(adev, vm, bo[i]);
+		amdgpu_bo_unreserve(bo[i]);
 		if (unlikely(!attachment[i]->bo_va)) {
 			ret = -ENOMEM;
 			pr_err("Failed to add BO object to VM. ret == %d\n",
 			       ret);
 			goto unwind;
 		}
-
 		attachment[i]->va = va;
 		attachment[i]->pte_flags = get_pte_flags(adev, mem);
 		attachment[i]->adev = adev;
@@ -821,7 +826,9 @@ static int kfd_mem_attach(struct amdgpu_device *adev, struct kgd_mem *mem,
 		if (!attachment[i])
 			continue;
 		if (attachment[i]->bo_va) {
+			amdgpu_bo_reserve(bo[i], true);
 			amdgpu_vm_bo_rmv(adev, attachment[i]->bo_va);
+			amdgpu_bo_unreserve(bo[i]);
 			list_del(&attachment[i]->list);
 		}
 		if (bo[i])
@@ -1700,12 +1707,12 @@ int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
 	pr_debug("Release VA 0x%llx - 0x%llx\n", mem->va,
 		mem->va + bo_size * (1 + mem->aql_queue));
 
-	ret = unreserve_bo_and_vms(&ctx, false, false);
-
 	/* Remove from VM internal data structures */
 	list_for_each_entry_safe(entry, tmp, &mem->attachments, list)
 		kfd_mem_detach(entry);
 
+	ret = unreserve_bo_and_vms(&ctx, false, false);
+
 	/* Free the sync object */
 	amdgpu_sync_free(&mem->sync);
 
Signature · 1 line
2.25.1
maintainer Reviewed-by on v1 1/1
#1 lore raw

Am 2021-11-01 um 4:14 p.m. schrieb Kent Russell:

Quoted 4 lines

BOs need to be reserved before they are added or removed, so ensure that they are reserved during kfd_mem_attach and kfd_mem_detach

Quoted 54 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +10 -3
index 61784bbfd7fd..8d30ccd70800 100644
@@ -798,14 +798,19 @@ static int kfd_mem_attach(struct amdgpu_device *adev, struct kgd_mem *mem,
 		}
 
 		/* Add BO to VM internal data structures */
+		ret = amdgpu_bo_reserve(bo[i], false);
+		if (ret) {
+			pr_debug("Unable to reserve BO during memory attach");
+			goto unwind;
+		}
 		attachment[i]->bo_va = amdgpu_vm_bo_add(adev, vm, bo[i]);
+		amdgpu_bo_unreserve(bo[i]);
 		if (unlikely(!attachment[i]->bo_va)) {
 			ret = -ENOMEM;
 			pr_err("Failed to add BO object to VM. ret == %d\n",
 			       ret);
 			goto unwind;
 		}
-
 		attachment[i]->va = va;
 		attachment[i]->pte_flags = get_pte_flags(adev, mem);
 		attachment[i]->adev = adev;
@@ -821,7 +826,9 @@ static int kfd_mem_attach(struct amdgpu_device *adev, struct kgd_mem *mem,
 		if (!attachment[i])
 			continue;
 		if (attachment[i]->bo_va) {
+			amdgpu_bo_reserve(bo[i], true);
 			amdgpu_vm_bo_rmv(adev, attachment[i]->bo_va);
+			amdgpu_bo_unreserve(bo[i]);
 			list_del(&attachment[i]->list);
 		}
 		if (bo[i])
@@ -1700,12 +1707,12 @@ int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
 	pr_debug("Release VA 0x%llx - 0x%llx\n", mem->va,
 		mem->va + bo_size * (1 + mem->aql_queue));
 
-	ret = unreserve_bo_and_vms(&ctx, false, false);
-
 	/* Remove from VM internal data structures */
 	list_for_each_entry_safe(entry, tmp, &mem->attachments, list)
 		kfd_mem_detach(entry);
 
+	ret = unreserve_bo_and_vms(&ctx, false, false);
+
 	/* Free the sync object */
 	amdgpu_sync_free(&mem->sync);
 
Reviewed-by Felix Kuehling
#2 lore raw

Kent, the call to map has the same structure. Is it not possible to call kfd_mem_attach after BO's are reserved?

Regards,
Ramesh

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Felix Kuehling
Sent: Monday, November 1, 2021 3:18 PM
To: Russell, Kent <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu: Make sure to reserve BOs before adding or removing

[CAUTION: External Email]

Am 2021-11-01 um 4:14 p.m. schrieb Kent Russell:

Quoted 4 lines

BOs need to be reserved before they are added or removed, so ensure that they are reserved during kfd_mem_attach and kfd_mem_detach

Quoted 56 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +6 -1
index 61784bbfd7fd..8d30ccd70800 100644
@@ -798,14 +798,19 @@ static int kfd_mem_attach(struct amdgpu_device *adev, struct kgd_mem *mem,
              }
 
              /* Add BO to VM internal data structures */
+             ret = amdgpu_bo_reserve(bo[i], false);
+             if (ret) {
+                     pr_debug("Unable to reserve BO during memory attach");
+                     goto unwind;
+             }
              attachment[i]->bo_va = amdgpu_vm_bo_add(adev, vm, 
 bo[i]);
+             amdgpu_bo_unreserve(bo[i]);
              if (unlikely(!attachment[i]->bo_va)) {
                      ret = -ENOMEM;
                      pr_err("Failed to add BO object to VM. ret == %d\n",
                             ret);
                      goto unwind;
              }
-
              attachment[i]->va = va;
              attachment[i]->pte_flags = get_pte_flags(adev, mem);
              attachment[i]->adev = adev; @@ -821,7 +826,9 @@ static 
int kfd_mem_attach(struct amdgpu_device *adev, struct kgd_mem *mem,
              if (!attachment[i])
                      continue;
              if (attachment[i]->bo_va) {
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +4 -2
+                     amdgpu_bo_reserve(bo[i], true);
                      amdgpu_vm_bo_rmv(adev, attachment[i]->bo_va);
+                     amdgpu_bo_unreserve(bo[i]);
                      list_del(&attachment[i]->list);
              }
              if (bo[i])
@@ -1700,12 +1707,12 @@ int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
      pr_debug("Release VA 0x%llx - 0x%llx\n", mem->va,
              mem->va + bo_size * (1 + mem->aql_queue));
 
-     ret = unreserve_bo_and_vms(&ctx, false, false);
-
      /* Remove from VM internal data structures */
      list_for_each_entry_safe(entry, tmp, &mem->attachments, list)
              kfd_mem_detach(entry);
 
+     ret = unreserve_bo_and_vms(&ctx, false, false);
+
      /* Free the sync object */
      amdgpu_sync_free(&mem->sync);
 
#3 lore raw

[AMD Official Use Only]

Hi Ramesh,

kfd_mem_attach_dmabuf would deadlock if the BO is already reserved, which is obviously problematic.

Also, if it's AQL, we make 2 BOs, and would need to reserve each one of those during addition, which we can't easily do if we lock outside of attach.

Kent

Quoted 87 lines

-----Original Message-----
From: Errabolu, Ramesh <[email protected]>
Sent: Monday, November 1, 2021 4:43 PM
To: Kuehling, Felix <[email protected]>; Russell, Kent <[email protected]>; [email protected]
Subject: RE: [PATCH] drm/amdgpu: Make sure to reserve BOs before adding or removing

Kent, the call to map has the same structure. Is it not possible to call kfd_mem_attach after BO's are reserved?

Regards,
Ramesh

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Felix Kuehling
Sent: Monday, November 1, 2021 3:18 PM
To: Russell, Kent <[email protected]>; [email protected]
Subject: Re: [PATCH] drm/amdgpu: Make sure to reserve BOs before adding or removing

[CAUTION: External Email]

Am 2021-11-01 um 4:14 p.m. schrieb Kent Russell:

Quoted 4 lines

BOs need to be reserved before they are added or removed, so ensure that they are reserved during kfd_mem_attach and kfd_mem_detach

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c static int kfd_mem_attach(struct amdgpu_device *adev, struct

Quoted 10 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0 -0
index 61784bbfd7fd..8d30ccd70800 100644
@@ -798,14 +798,19 @@ static int kfd_mem_attach(struct amdgpu_device *adev, struct

kgd_mem *mem,

Quoted 46 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +6 -1
              }
 
              /* Add BO to VM internal data structures */
+             ret = amdgpu_bo_reserve(bo[i], false);
+             if (ret) {
+                     pr_debug("Unable to reserve BO during memory attach");
+                     goto unwind;
+             }
              attachment[i]->bo_va = amdgpu_vm_bo_add(adev, vm,
 bo[i]);
+             amdgpu_bo_unreserve(bo[i]);
              if (unlikely(!attachment[i]->bo_va)) {
                      ret = -ENOMEM;
                      pr_err("Failed to add BO object to VM. ret == %d\n",
                             ret);
                      goto unwind;
              }
-
              attachment[i]->va = va;
              attachment[i]->pte_flags = get_pte_flags(adev, mem);
              attachment[i]->adev = adev; @@ -821,7 +826,9 @@ static
int kfd_mem_attach(struct amdgpu_device *adev, struct kgd_mem *mem,
              if (!attachment[i])
                      continue;
              if (attachment[i]->bo_va) {
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +4 -2
+                     amdgpu_bo_reserve(bo[i], true);
                      amdgpu_vm_bo_rmv(adev, attachment[i]->bo_va);
+                     amdgpu_bo_unreserve(bo[i]);
                      list_del(&attachment[i]->list);
              }
              if (bo[i])
@@ -1700,12 +1707,12 @@ int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
      pr_debug("Release VA 0x%llx - 0x%llx\n", mem->va,
              mem->va + bo_size * (1 + mem->aql_queue));
 
-     ret = unreserve_bo_and_vms(&ctx, false, false);
-
      /* Remove from VM internal data structures */
      list_for_each_entry_safe(entry, tmp, &mem->attachments, list)
              kfd_mem_detach(entry);
 
+     ret = unreserve_bo_and_vms(&ctx, false, false);
+
      /* Free the sync object */
      amdgpu_sync_free(&mem->sync);