Skip to content

Commit ffa88b0

Browse files
xiaogang-chen-amdalexdeucher
authored andcommitted
drm/amdgpu: Correctly use bo_va->ref_count in compute VMs
This is needed to correctly handle BOs imported into compute VM from gfx. Both kfd and gfx should use same bo_va and set bo_va->ref_count correctly when map the Bos into same VM, otherwise we may trigger kernel general protection when iterate mappings over bo_va's valids or invalids list. Signed-off-by: Felix Kuehling <[email protected]> Signed-off-by: Xiaogang Chen <[email protected]> Acked-by: Christian König <[email protected]> Reviewed-by: Ramesh Errabolu <[email protected]> Tested-by: Xiaogang Chen <[email protected]> Signed-off-by: Alex Deucher <[email protected]>
1 parent f20f3b0 commit ffa88b0

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,7 @@ static int kfd_mem_attach(struct amdgpu_device *adev, struct kgd_mem *mem,
834834
uint64_t va = mem->va;
835835
struct kfd_mem_attachment *attachment[2] = {NULL, NULL};
836836
struct amdgpu_bo *bo[2] = {NULL, NULL};
837+
struct amdgpu_bo_va *bo_va;
837838
bool same_hive = false;
838839
int i, ret;
839840

@@ -921,7 +922,12 @@ static int kfd_mem_attach(struct amdgpu_device *adev, struct kgd_mem *mem,
921922
pr_debug("Unable to reserve BO during memory attach");
922923
goto unwind;
923924
}
924-
attachment[i]->bo_va = amdgpu_vm_bo_add(adev, vm, bo[i]);
925+
bo_va = amdgpu_vm_bo_find(vm, bo[i]);
926+
if (!bo_va)
927+
bo_va = amdgpu_vm_bo_add(adev, vm, bo[i]);
928+
else
929+
++bo_va->ref_count;
930+
attachment[i]->bo_va = bo_va;
925931
amdgpu_bo_unreserve(bo[i]);
926932
if (unlikely(!attachment[i]->bo_va)) {
927933
ret = -ENOMEM;
@@ -945,7 +951,8 @@ static int kfd_mem_attach(struct amdgpu_device *adev, struct kgd_mem *mem,
945951
continue;
946952
if (attachment[i]->bo_va) {
947953
amdgpu_bo_reserve(bo[i], true);
948-
amdgpu_vm_bo_del(adev, attachment[i]->bo_va);
954+
if (--attachment[i]->bo_va->ref_count == 0)
955+
amdgpu_vm_bo_del(adev, attachment[i]->bo_va);
949956
amdgpu_bo_unreserve(bo[i]);
950957
list_del(&attachment[i]->list);
951958
}
@@ -962,7 +969,8 @@ static void kfd_mem_detach(struct kfd_mem_attachment *attachment)
962969

963970
pr_debug("\t remove VA 0x%llx in entry %p\n",
964971
attachment->va, attachment);
965-
amdgpu_vm_bo_del(attachment->adev, attachment->bo_va);
972+
if (--attachment->bo_va->ref_count == 0)
973+
amdgpu_vm_bo_del(attachment->adev, attachment->bo_va);
966974
drm_gem_object_put(&bo->tbo.base);
967975
list_del(&attachment->list);
968976
kfree(attachment);

0 commit comments

Comments
 (0)