Skip to content

Commit 9e5d275

Browse files
fxkamdalexdeucher
authored andcommitted
drm/amdgpu: Move kfd_mem_attach outside reservation
This is needed to avoid deadlocks with DMA buf import in the next patch. Also move PT/PD validation out of kfd_mem_attach, that way the caller can bo this unconditionally. Signed-off-by: Felix Kuehling <[email protected]> Acked-by: Oak Zeng <[email protected]> Acked-by: Ramesh Errabolu <[email protected]> Signed-off-by: Alex Deucher <[email protected]>
1 parent b72ed8a commit 9e5d275

File tree

1 file changed

+44
-31
lines changed

1 file changed

+44
-31
lines changed

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

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,34 @@ kfd_mem_dmaunmap_attachment(struct kgd_mem *mem,
582582
}
583583
}
584584

585+
static int
586+
kfd_mem_attach_userptr(struct amdgpu_device *adev, struct kgd_mem *mem,
587+
struct amdgpu_bo **bo)
588+
{
589+
unsigned long bo_size = mem->bo->tbo.base.size;
590+
struct drm_gem_object *gobj;
591+
int ret;
592+
593+
ret = amdgpu_bo_reserve(mem->bo, false);
594+
if (ret)
595+
return ret;
596+
597+
ret = amdgpu_gem_object_create(adev, bo_size, 1,
598+
AMDGPU_GEM_DOMAIN_CPU,
599+
0, ttm_bo_type_sg,
600+
mem->bo->tbo.base.resv,
601+
&gobj);
602+
if (ret)
603+
return ret;
604+
605+
amdgpu_bo_unreserve(mem->bo);
606+
607+
*bo = gem_to_amdgpu_bo(gobj);
608+
(*bo)->parent = amdgpu_bo_ref(mem->bo);
609+
610+
return 0;
611+
}
612+
585613
/* kfd_mem_attach - Add a BO to a VM
586614
*
587615
* Everything that needs to bo done only once when a BO is first added
@@ -603,7 +631,6 @@ static int kfd_mem_attach(struct amdgpu_device *adev, struct kgd_mem *mem,
603631
uint64_t va = mem->va;
604632
struct kfd_mem_attachment *attachment[2] = {NULL, NULL};
605633
struct amdgpu_bo *bo[2] = {NULL, NULL};
606-
struct drm_gem_object *gobj;
607634
int i, ret;
608635

609636
if (!va) {
@@ -637,15 +664,9 @@ static int kfd_mem_attach(struct amdgpu_device *adev, struct kgd_mem *mem,
637664
} else if (amdgpu_ttm_tt_get_usermm(mem->bo->tbo.ttm)) {
638665
/* Create an SG BO to DMA-map userptrs on other GPUs */
639666
attachment[i]->type = KFD_MEM_ATT_USERPTR;
640-
ret = amdgpu_gem_object_create(adev, bo_size, 1,
641-
AMDGPU_GEM_DOMAIN_CPU,
642-
0, ttm_bo_type_sg,
643-
mem->bo->tbo.base.resv,
644-
&gobj);
667+
ret = kfd_mem_attach_userptr(adev, mem, &bo[i]);
645668
if (ret)
646669
goto unwind;
647-
bo[i] = gem_to_amdgpu_bo(gobj);
648-
bo[i]->parent = amdgpu_bo_ref(mem->bo);
649670
} else {
650671
/* FIXME: Need to DMA-map other BO types */
651672
attachment[i]->type = KFD_MEM_ATT_SHARED;
@@ -670,13 +691,6 @@ static int kfd_mem_attach(struct amdgpu_device *adev, struct kgd_mem *mem,
670691
va += bo_size;
671692
}
672693

673-
/* Allocate validate page tables if needed */
674-
ret = vm_validate_pt_pd_bos(vm);
675-
if (unlikely(ret)) {
676-
pr_err("validate_pt_pd_bos() failed\n");
677-
goto unwind;
678-
}
679-
680694
return 0;
681695

682696
unwind:
@@ -1483,12 +1497,12 @@ int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
14831497
pr_debug("Release VA 0x%llx - 0x%llx\n", mem->va,
14841498
mem->va + bo_size * (1 + mem->aql_queue));
14851499

1500+
ret = unreserve_bo_and_vms(&ctx, false, false);
1501+
14861502
/* Remove from VM internal data structures */
14871503
list_for_each_entry_safe(entry, tmp, &mem->attachments, list)
14881504
kfd_mem_detach(entry);
14891505

1490-
ret = unreserve_bo_and_vms(&ctx, false, false);
1491-
14921506
/* Free the sync object */
14931507
amdgpu_sync_free(&mem->sync);
14941508

@@ -1565,6 +1579,12 @@ int amdgpu_amdkfd_gpuvm_map_memory_to_gpu(
15651579
mem->va + bo_size * (1 + mem->aql_queue),
15661580
avm, domain_string(domain));
15671581

1582+
if (!kfd_mem_is_attached(avm, mem)) {
1583+
ret = kfd_mem_attach(adev, mem, avm, mem->aql_queue);
1584+
if (ret)
1585+
goto out;
1586+
}
1587+
15681588
ret = reserve_bo_and_vm(mem, avm, &ctx);
15691589
if (unlikely(ret))
15701590
goto out;
@@ -1578,15 +1598,9 @@ int amdgpu_amdkfd_gpuvm_map_memory_to_gpu(
15781598
bo->tbo.mem.mem_type == TTM_PL_SYSTEM)
15791599
is_invalid_userptr = true;
15801600

1581-
if (!kfd_mem_is_attached(avm, mem)) {
1582-
ret = kfd_mem_attach(adev, mem, avm, mem->aql_queue);
1583-
if (ret)
1584-
goto attach_failed;
1585-
} else {
1586-
ret = vm_validate_pt_pd_bos(avm);
1587-
if (unlikely(ret))
1588-
goto attach_failed;
1589-
}
1601+
ret = vm_validate_pt_pd_bos(avm);
1602+
if (unlikely(ret))
1603+
goto out_unreserve;
15901604

15911605
if (mem->mapped_to_gpu_memory == 0 &&
15921606
!amdgpu_ttm_tt_get_usermm(bo->tbo.ttm)) {
@@ -1597,7 +1611,7 @@ int amdgpu_amdkfd_gpuvm_map_memory_to_gpu(
15971611
ret = amdgpu_amdkfd_bo_validate(bo, domain, true);
15981612
if (ret) {
15991613
pr_debug("Validate failed\n");
1600-
goto map_bo_to_gpuvm_failed;
1614+
goto out_unreserve;
16011615
}
16021616
}
16031617

@@ -1612,13 +1626,13 @@ int amdgpu_amdkfd_gpuvm_map_memory_to_gpu(
16121626
is_invalid_userptr);
16131627
if (ret) {
16141628
pr_err("Failed to map bo to gpuvm\n");
1615-
goto map_bo_to_gpuvm_failed;
1629+
goto out_unreserve;
16161630
}
16171631

16181632
ret = vm_update_pds(avm, ctx.sync);
16191633
if (ret) {
16201634
pr_err("Failed to update page directories\n");
1621-
goto map_bo_to_gpuvm_failed;
1635+
goto out_unreserve;
16221636
}
16231637

16241638
entry->is_mapped = true;
@@ -1635,8 +1649,7 @@ int amdgpu_amdkfd_gpuvm_map_memory_to_gpu(
16351649

16361650
goto out;
16371651

1638-
map_bo_to_gpuvm_failed:
1639-
attach_failed:
1652+
out_unreserve:
16401653
unreserve_bo_and_vms(&ctx, false, false);
16411654
out:
16421655
mutex_unlock(&mem->process_info->lock);

0 commit comments

Comments
 (0)