Skip to content

Commit 0e2e7c5

Browse files
fxkamdalexdeucher
authored andcommitted
drm/amdgpu: Attach eviction fence on alloc
Instead of attaching the eviction fence when a KFD BO is first mapped, attach it when it is allocated or imported. This in preparation to allow KFD BOs to be mapped using the render node API. Signed-off-by: Felix Kuehling <[email protected]> Acked-by: Christian König <[email protected]> Signed-off-by: Alex Deucher <[email protected]>
1 parent 5a104cb commit 0e2e7c5

File tree

1 file changed

+48
-31
lines changed

1 file changed

+48
-31
lines changed

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

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,32 @@ static int amdgpu_amdkfd_bo_validate(struct amdgpu_bo *bo, uint32_t domain,
425425
return ret;
426426
}
427427

428+
static int amdgpu_amdkfd_bo_validate_and_fence(struct amdgpu_bo *bo,
429+
uint32_t domain,
430+
struct dma_fence *fence)
431+
{
432+
int ret = amdgpu_bo_reserve(bo, false);
433+
434+
if (ret)
435+
return ret;
436+
437+
ret = amdgpu_amdkfd_bo_validate(bo, domain, true);
438+
if (ret)
439+
goto unreserve_out;
440+
441+
ret = dma_resv_reserve_fences(bo->tbo.base.resv, 1);
442+
if (ret)
443+
goto unreserve_out;
444+
445+
dma_resv_add_fence(bo->tbo.base.resv, fence,
446+
DMA_RESV_USAGE_BOOKKEEP);
447+
448+
unreserve_out:
449+
amdgpu_bo_unreserve(bo);
450+
451+
return ret;
452+
}
453+
428454
static int amdgpu_amdkfd_validate_vm_bo(void *_unused, struct amdgpu_bo *bo)
429455
{
430456
return amdgpu_amdkfd_bo_validate(bo, bo->allowed_domains, false);
@@ -1784,6 +1810,15 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(
17841810
}
17851811
bo->allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
17861812
bo->preferred_domains = AMDGPU_GEM_DOMAIN_GTT;
1813+
} else {
1814+
mutex_lock(&avm->process_info->lock);
1815+
if (avm->process_info->eviction_fence &&
1816+
!dma_fence_is_signaled(&avm->process_info->eviction_fence->base))
1817+
ret = amdgpu_amdkfd_bo_validate_and_fence(bo, domain,
1818+
&avm->process_info->eviction_fence->base);
1819+
mutex_unlock(&avm->process_info->lock);
1820+
if (ret)
1821+
goto err_validate_bo;
17871822
}
17881823

17891824
if (offset)
@@ -1793,6 +1828,7 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(
17931828

17941829
allocate_init_user_pages_failed:
17951830
err_pin_bo:
1831+
err_validate_bo:
17961832
remove_kgd_mem_from_kfd_bo_list(*mem, avm->process_info);
17971833
drm_vma_node_revoke(&gobj->vma_node, drm_priv);
17981834
err_node_allow:
@@ -1866,10 +1902,6 @@ int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
18661902
if (unlikely(ret))
18671903
return ret;
18681904

1869-
/* The eviction fence should be removed by the last unmap.
1870-
* TODO: Log an error condition if the bo still has the eviction fence
1871-
* attached
1872-
*/
18731905
amdgpu_amdkfd_remove_eviction_fence(mem->bo,
18741906
process_info->eviction_fence);
18751907
pr_debug("Release VA 0x%llx - 0x%llx\n", mem->va,
@@ -1998,19 +2030,6 @@ int amdgpu_amdkfd_gpuvm_map_memory_to_gpu(
19982030
if (unlikely(ret))
19992031
goto out_unreserve;
20002032

2001-
if (mem->mapped_to_gpu_memory == 0 &&
2002-
!amdgpu_ttm_tt_get_usermm(bo->tbo.ttm)) {
2003-
/* Validate BO only once. The eviction fence gets added to BO
2004-
* the first time it is mapped. Validate will wait for all
2005-
* background evictions to complete.
2006-
*/
2007-
ret = amdgpu_amdkfd_bo_validate(bo, domain, true);
2008-
if (ret) {
2009-
pr_debug("Validate failed\n");
2010-
goto out_unreserve;
2011-
}
2012-
}
2013-
20142033
list_for_each_entry(entry, &mem->attachments, list) {
20152034
if (entry->bo_va->base.vm != avm || entry->is_mapped)
20162035
continue;
@@ -2037,10 +2056,6 @@ int amdgpu_amdkfd_gpuvm_map_memory_to_gpu(
20372056
mem->mapped_to_gpu_memory);
20382057
}
20392058

2040-
if (!amdgpu_ttm_tt_get_usermm(bo->tbo.ttm) && !bo->tbo.pin_count)
2041-
dma_resv_add_fence(bo->tbo.base.resv,
2042-
&avm->process_info->eviction_fence->base,
2043-
DMA_RESV_USAGE_BOOKKEEP);
20442059
ret = unreserve_bo_and_vms(&ctx, false, false);
20452060

20462061
goto out;
@@ -2074,7 +2089,6 @@ int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
20742089
struct amdgpu_device *adev, struct kgd_mem *mem, void *drm_priv)
20752090
{
20762091
struct amdgpu_vm *avm = drm_priv_to_vm(drm_priv);
2077-
struct amdkfd_process_info *process_info = avm->process_info;
20782092
unsigned long bo_size = mem->bo->tbo.base.size;
20792093
struct kfd_mem_attachment *entry;
20802094
struct bo_vm_reservation_context ctx;
@@ -2115,15 +2129,6 @@ int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
21152129
mem->mapped_to_gpu_memory);
21162130
}
21172131

2118-
/* If BO is unmapped from all VMs, unfence it. It can be evicted if
2119-
* required.
2120-
*/
2121-
if (mem->mapped_to_gpu_memory == 0 &&
2122-
!amdgpu_ttm_tt_get_usermm(mem->bo->tbo.ttm) &&
2123-
!mem->bo->tbo.pin_count)
2124-
amdgpu_amdkfd_remove_eviction_fence(mem->bo,
2125-
process_info->eviction_fence);
2126-
21272132
unreserve_out:
21282133
unreserve_bo_and_vms(&ctx, false, false);
21292134
out:
@@ -2351,8 +2356,20 @@ int amdgpu_amdkfd_gpuvm_import_dmabuf(struct amdgpu_device *adev,
23512356
amdgpu_sync_create(&(*mem)->sync);
23522357
(*mem)->is_imported = true;
23532358

2359+
mutex_lock(&avm->process_info->lock);
2360+
if (avm->process_info->eviction_fence &&
2361+
!dma_fence_is_signaled(&avm->process_info->eviction_fence->base))
2362+
ret = amdgpu_amdkfd_bo_validate_and_fence(bo, (*mem)->domain,
2363+
&avm->process_info->eviction_fence->base);
2364+
mutex_unlock(&avm->process_info->lock);
2365+
if (ret)
2366+
goto err_remove_mem;
2367+
23542368
return 0;
23552369

2370+
err_remove_mem:
2371+
remove_kgd_mem_from_kfd_bo_list(*mem, avm->process_info);
2372+
drm_vma_node_revoke(&obj->vma_node, drm_priv);
23562373
err_free_mem:
23572374
kfree(*mem);
23582375
err_put_obj:

0 commit comments

Comments
 (0)