@@ -425,6 +425,32 @@ static int amdgpu_amdkfd_bo_validate(struct amdgpu_bo *bo, uint32_t domain,
425
425
return ret ;
426
426
}
427
427
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
+
428
454
static int amdgpu_amdkfd_validate_vm_bo (void * _unused , struct amdgpu_bo * bo )
429
455
{
430
456
return amdgpu_amdkfd_bo_validate (bo , bo -> allowed_domains , false);
@@ -1784,6 +1810,15 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(
1784
1810
}
1785
1811
bo -> allowed_domains = AMDGPU_GEM_DOMAIN_GTT ;
1786
1812
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 ;
1787
1822
}
1788
1823
1789
1824
if (offset )
@@ -1793,6 +1828,7 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(
1793
1828
1794
1829
allocate_init_user_pages_failed :
1795
1830
err_pin_bo :
1831
+ err_validate_bo :
1796
1832
remove_kgd_mem_from_kfd_bo_list (* mem , avm -> process_info );
1797
1833
drm_vma_node_revoke (& gobj -> vma_node , drm_priv );
1798
1834
err_node_allow :
@@ -1866,10 +1902,6 @@ int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
1866
1902
if (unlikely (ret ))
1867
1903
return ret ;
1868
1904
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
- */
1873
1905
amdgpu_amdkfd_remove_eviction_fence (mem -> bo ,
1874
1906
process_info -> eviction_fence );
1875
1907
pr_debug ("Release VA 0x%llx - 0x%llx\n" , mem -> va ,
@@ -1998,19 +2030,6 @@ int amdgpu_amdkfd_gpuvm_map_memory_to_gpu(
1998
2030
if (unlikely (ret ))
1999
2031
goto out_unreserve ;
2000
2032
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
-
2014
2033
list_for_each_entry (entry , & mem -> attachments , list ) {
2015
2034
if (entry -> bo_va -> base .vm != avm || entry -> is_mapped )
2016
2035
continue ;
@@ -2037,10 +2056,6 @@ int amdgpu_amdkfd_gpuvm_map_memory_to_gpu(
2037
2056
mem -> mapped_to_gpu_memory );
2038
2057
}
2039
2058
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 );
2044
2059
ret = unreserve_bo_and_vms (& ctx , false, false);
2045
2060
2046
2061
goto out ;
@@ -2074,7 +2089,6 @@ int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
2074
2089
struct amdgpu_device * adev , struct kgd_mem * mem , void * drm_priv )
2075
2090
{
2076
2091
struct amdgpu_vm * avm = drm_priv_to_vm (drm_priv );
2077
- struct amdkfd_process_info * process_info = avm -> process_info ;
2078
2092
unsigned long bo_size = mem -> bo -> tbo .base .size ;
2079
2093
struct kfd_mem_attachment * entry ;
2080
2094
struct bo_vm_reservation_context ctx ;
@@ -2115,15 +2129,6 @@ int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
2115
2129
mem -> mapped_to_gpu_memory );
2116
2130
}
2117
2131
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
-
2127
2132
unreserve_out :
2128
2133
unreserve_bo_and_vms (& ctx , false, false);
2129
2134
out :
@@ -2351,8 +2356,20 @@ int amdgpu_amdkfd_gpuvm_import_dmabuf(struct amdgpu_device *adev,
2351
2356
amdgpu_sync_create (& (* mem )-> sync );
2352
2357
(* mem )-> is_imported = true;
2353
2358
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
+
2354
2368
return 0 ;
2355
2369
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 );
2356
2373
err_free_mem :
2357
2374
kfree (* mem );
2358
2375
err_put_obj :
0 commit comments