Skip to contentSkip to conversation

Find bo_va before create it when map bo into compute VM

PATCH In review Xiaogang.Chen 3 participants 5 messages Oct 11, 2023 – Oct 12, 2023

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

PATCH 1/1 Find bo_va before create it when map bo into compute VM

From: Xiaogang Chen <[email protected]>

This is needed to correctly handle BOs imported into compute VM from gfx. Both kfd and gfx should use same bo_va when map the Bos into same VM, otherwise we may trigger kernel general protection when iterate mappings from bo_va.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +10 -2
index a15e59abe70a..c1ec93cc50ae 100644
@@ -832,6 +832,7 @@ static int kfd_mem_attach(struct amdgpu_device *adev, struct kgd_mem *mem,
 	uint64_t va = mem->va;
 	struct kfd_mem_attachment *attachment[2] = {NULL, NULL};
 	struct amdgpu_bo *bo[2] = {NULL, NULL};
+	struct amdgpu_bo_va *bo_va;
 	bool same_hive = false;
 	int i, ret;
 
@@ -919,7 +920,13 @@ static int kfd_mem_attach(struct amdgpu_device *adev, struct kgd_mem *mem,
 			pr_debug("Unable to reserve BO during memory attach");
 			goto unwind;
 		}
-		attachment[i]->bo_va = amdgpu_vm_bo_add(adev, vm, bo[i]);
+		bo_va = amdgpu_vm_bo_find(vm, bo[i]);
+		if (!bo_va)
+			bo_va = amdgpu_vm_bo_add(adev, vm, bo[i]);
+		else
+			++bo_va->ref_count;
+		attachment[i]->bo_va = bo_va;
+
 		amdgpu_bo_unreserve(bo[i]);
 		if (unlikely(!attachment[i]->bo_va)) {
 			ret = -ENOMEM;
@@ -943,7 +950,8 @@ static int kfd_mem_attach(struct amdgpu_device *adev, struct kgd_mem *mem,
 			continue;
 		if (attachment[i]->bo_va) {
 			amdgpu_bo_reserve(bo[i], true);
-			amdgpu_vm_bo_del(adev, attachment[i]->bo_va);
+			if (--attachment[i]->bo_va->ref_count == 0)
+				amdgpu_vm_bo_del(adev, attachment[i]->bo_va);
 			amdgpu_bo_unreserve(bo[i]);
 			list_del(&attachment[i]->list);
 		}
Signature · 1 line
2.25.1
maintainer
#1 lore raw

The subject line somehow got messed up. There should be an drm/amdgpu: or drm/amdkfd: prefix.

Regards,
Christian.

Am 12.10.23 um 01:36 schrieb Xiaogang.Chen:

Quoted 52 lines

From: Xiaogang Chen <[email protected]>

This is needed to correctly handle BOs imported into compute VM from gfx. Both kfd and gfx should use same bo_va when map the Bos into same VM, otherwise we may trigger kernel general protection when iterate mappings from bo_va.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 12 ++++++++++--
  1 file changed, 10 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +10 -2
index a15e59abe70a..c1ec93cc50ae 100644
@@ -832,6 +832,7 @@ static int kfd_mem_attach(struct amdgpu_device *adev, struct kgd_mem *mem,
  	uint64_t va = mem->va;
  	struct kfd_mem_attachment *attachment[2] = {NULL, NULL};
  	struct amdgpu_bo *bo[2] = {NULL, NULL};
+	struct amdgpu_bo_va *bo_va;
  	bool same_hive = false;
  	int i, ret;
 
@@ -919,7 +920,13 @@ static int kfd_mem_attach(struct amdgpu_device *adev, struct kgd_mem *mem,
  			pr_debug("Unable to reserve BO during memory attach");
  			goto unwind;
  		}
-		attachment[i]->bo_va = amdgpu_vm_bo_add(adev, vm, bo[i]);
+		bo_va = amdgpu_vm_bo_find(vm, bo[i]);
+		if (!bo_va)
+			bo_va = amdgpu_vm_bo_add(adev, vm, bo[i]);
+		else
+			++bo_va->ref_count;
+		attachment[i]->bo_va = bo_va;
+
  		amdgpu_bo_unreserve(bo[i]);
  		if (unlikely(!attachment[i]->bo_va)) {
  			ret = -ENOMEM;
@@ -943,7 +950,8 @@ static int kfd_mem_attach(struct amdgpu_device *adev, struct kgd_mem *mem,
  			continue;
  		if (attachment[i]->bo_va) {
  			amdgpu_bo_reserve(bo[i], true);
-			amdgpu_vm_bo_del(adev, attachment[i]->bo_va);
+			if (--attachment[i]->bo_va->ref_count == 0)
+				amdgpu_vm_bo_del(adev, attachment[i]->bo_va);
  			amdgpu_bo_unreserve(bo[i]);
  			list_del(&attachment[i]->list);
  		}
#2 lore raw
Quoted 1 line · Christian König

The subject line somehow got messed up. There should be an drm/amdgpu: or drm/amdkfd: prefix.

yes, will resend it.

Regards

Xiaogang.

Quoted 59 lines

Regards,
Christian.

Am 12.10.23 um 01:36 schrieb Xiaogang.Chen:

Quoted 53 lines

From: Xiaogang Chen <[email protected]>

This is needed to correctly handle BOs imported into compute VM from gfx. Both kfd and gfx should use same bo_va when map the Bos into same VM, otherwise we may trigger kernel general protection when iterate mappings from bo_va.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 12 ++++++++++--

  1 file changed, 10 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +1 -0
index a15e59abe70a..c1ec93cc50ae 100644
@@ -832,6 +832,7 @@ static int kfd_mem_attach(struct amdgpu_device 
 *adev, struct kgd_mem *mem,
       uint64_t va = mem->va;
       struct kfd_mem_attachment *attachment[2] = {NULL, NULL};
       struct amdgpu_bo *bo[2] = {NULL, NULL};
+    struct amdgpu_bo_va *bo_va;
       bool same_hive = false;
       int i, ret;

  @@ -919,7 +920,13 @@ static int kfd_mem_attach(struct amdgpu_device *adev, struct kgd_mem *mem, pr_debug("Unable to reserve BO during memory attach");
              goto unwind;
          }
-        attachment[i]->bo_va = amdgpu_vm_bo_add(adev, vm, bo[i]);
+        bo_va = amdgpu_vm_bo_find(vm, bo[i]);
+        if (!bo_va)
+            bo_va = amdgpu_vm_bo_add(adev, vm, bo[i]);
+        else
+            ++bo_va->ref_count;
+        attachment[i]->bo_va = bo_va;
+
          amdgpu_bo_unreserve(bo[i]);
          if (unlikely(!attachment[i]->bo_va)) {
              ret = -ENOMEM;

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +2 -1
@@ -943,7 +950,8 @@ static int kfd_mem_attach(struct amdgpu_device 
 *adev, struct kgd_mem *mem,
               continue;
           if (attachment[i]->bo_va) {
               amdgpu_bo_reserve(bo[i], true);
-            amdgpu_vm_bo_del(adev, attachment[i]->bo_va);
+            if (--attachment[i]->bo_va->ref_count == 0)
+                amdgpu_vm_bo_del(adev, attachment[i]->bo_va);
               amdgpu_bo_unreserve(bo[i]);
               list_del(&attachment[i]->list);

          }

maintainer
#3 lore raw
In reply to Xiaogang.Chen
Quoted 1 line · Xiaogang.Chen

From: Xiaogang Chen <[email protected]>

Since you are the author of this updated patch, you should also add your Signed-off-by below.

Quoted 10 lines

This is needed to correctly handle BOs imported into compute VM from gfx. Both kfd and gfx should use same bo_va when map the Bos into same VM, otherwise we may trigger kernel general protection when iterate mappings from bo_va.

You lost any mention of reference counting from the commit headline and description. I think that's still an important concept that should be mentioned. I'd change the headline to something like this:

drm/amdgpu: Correctly use bo_va->ref_count in compute VMs

Regards,
  Felix

Quoted 41 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 12 ++++++++++--
  1 file changed, 10 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +10 -2
index a15e59abe70a..c1ec93cc50ae 100644
@@ -832,6 +832,7 @@ static int kfd_mem_attach(struct amdgpu_device *adev, struct kgd_mem *mem,
  	uint64_t va = mem->va;
  	struct kfd_mem_attachment *attachment[2] = {NULL, NULL};
  	struct amdgpu_bo *bo[2] = {NULL, NULL};
+	struct amdgpu_bo_va *bo_va;
  	bool same_hive = false;
  	int i, ret;
 
@@ -919,7 +920,13 @@ static int kfd_mem_attach(struct amdgpu_device *adev, struct kgd_mem *mem,
  			pr_debug("Unable to reserve BO during memory attach");
  			goto unwind;
  		}
-		attachment[i]->bo_va = amdgpu_vm_bo_add(adev, vm, bo[i]);
+		bo_va = amdgpu_vm_bo_find(vm, bo[i]);
+		if (!bo_va)
+			bo_va = amdgpu_vm_bo_add(adev, vm, bo[i]);
+		else
+			++bo_va->ref_count;
+		attachment[i]->bo_va = bo_va;
+
  		amdgpu_bo_unreserve(bo[i]);
  		if (unlikely(!attachment[i]->bo_va)) {
  			ret = -ENOMEM;
@@ -943,7 +950,8 @@ static int kfd_mem_attach(struct amdgpu_device *adev, struct kgd_mem *mem,
  			continue;
  		if (attachment[i]->bo_va) {
  			amdgpu_bo_reserve(bo[i], true);
-			amdgpu_vm_bo_del(adev, attachment[i]->bo_va);
+			if (--attachment[i]->bo_va->ref_count == 0)
+				amdgpu_vm_bo_del(adev, attachment[i]->bo_va);
  			amdgpu_bo_unreserve(bo[i]);
  			list_del(&attachment[i]->list);
  		}
#4 lore raw
Quoted 4 lines · Felix Kuehling
Quoted 1 line · Xiaogang.Chen

From: Xiaogang Chen <[email protected]>

Since you are the author of this updated patch, you should also add your Signed-off-by below.

ok, thanks.

Quoted 13 lines
Quoted 7 lines

This is needed to correctly handle BOs imported into compute VM from gfx. Both kfd and gfx should use same bo_va when map the Bos into same VM, otherwise we may trigger kernel general protection when iterate mappings from bo_va.

Quoted 5 lines
Quoted 4 lines

You lost any mention of reference counting from the commit headline and description. I think that's still an important concept that should be mentioned. I'd change the headline to something like this:

drm/amdgpu: Correctly use bo_va->ref_count in compute VMs

ok, will update commit headline and message and resend.

Regards

Xiaogang

Quoted 46 lines

Regards,
  Felix

Quoted 42 lines

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 12 ++++++++++--

  1 file changed, 10 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +1 -0
index a15e59abe70a..c1ec93cc50ae 100644
@@ -832,6 +832,7 @@ static int kfd_mem_attach(struct amdgpu_device 
 *adev, struct kgd_mem *mem,
       uint64_t va = mem->va;
       struct kfd_mem_attachment *attachment[2] = {NULL, NULL};
       struct amdgpu_bo *bo[2] = {NULL, NULL};
+    struct amdgpu_bo_va *bo_va;
       bool same_hive = false;
       int i, ret;

  @@ -919,7 +920,13 @@ static int kfd_mem_attach(struct amdgpu_device *adev, struct kgd_mem *mem, pr_debug("Unable to reserve BO during memory attach");
              goto unwind;
          }
-        attachment[i]->bo_va = amdgpu_vm_bo_add(adev, vm, bo[i]);
+        bo_va = amdgpu_vm_bo_find(vm, bo[i]);
+        if (!bo_va)
+            bo_va = amdgpu_vm_bo_add(adev, vm, bo[i]);
+        else
+            ++bo_va->ref_count;
+        attachment[i]->bo_va = bo_va;
+
          amdgpu_bo_unreserve(bo[i]);
          if (unlikely(!attachment[i]->bo_va)) {
              ret = -ENOMEM;

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +2 -1
@@ -943,7 +950,8 @@ static int kfd_mem_attach(struct amdgpu_device 
 *adev, struct kgd_mem *mem,
               continue;
           if (attachment[i]->bo_va) {
               amdgpu_bo_reserve(bo[i], true);
-            amdgpu_vm_bo_del(adev, attachment[i]->bo_va);
+            if (--attachment[i]->bo_va->ref_count == 0)
+                amdgpu_vm_bo_del(adev, attachment[i]->bo_va);
               amdgpu_bo_unreserve(bo[i]);
               list_del(&attachment[i]->list);

          }