amdgpu_amdkfd_gpuvm.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128
  1. /*
  2. * Copyright 2014-2018 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #define pr_fmt(fmt) "kfd2kgd: " fmt
  23. #include <linux/list.h>
  24. #include <linux/pagemap.h>
  25. #include <linux/sched/mm.h>
  26. #include <drm/drmP.h>
  27. #include "amdgpu_object.h"
  28. #include "amdgpu_vm.h"
  29. #include "amdgpu_amdkfd.h"
  30. /* Special VM and GART address alignment needed for VI pre-Fiji due to
  31. * a HW bug.
  32. */
  33. #define VI_BO_SIZE_ALIGN (0x8000)
  34. /* BO flag to indicate a KFD userptr BO */
  35. #define AMDGPU_AMDKFD_USERPTR_BO (1ULL << 63)
  36. /* Userptr restore delay, just long enough to allow consecutive VM
  37. * changes to accumulate
  38. */
  39. #define AMDGPU_USERPTR_RESTORE_DELAY_MS 1
  40. /* Impose limit on how much memory KFD can use */
  41. static struct {
  42. uint64_t max_system_mem_limit;
  43. uint64_t max_userptr_mem_limit;
  44. int64_t system_mem_used;
  45. int64_t userptr_mem_used;
  46. spinlock_t mem_limit_lock;
  47. } kfd_mem_limit;
  48. /* Struct used for amdgpu_amdkfd_bo_validate */
  49. struct amdgpu_vm_parser {
  50. uint32_t domain;
  51. bool wait;
  52. };
  53. static const char * const domain_bit_to_string[] = {
  54. "CPU",
  55. "GTT",
  56. "VRAM",
  57. "GDS",
  58. "GWS",
  59. "OA"
  60. };
  61. #define domain_string(domain) domain_bit_to_string[ffs(domain)-1]
  62. static void amdgpu_amdkfd_restore_userptr_worker(struct work_struct *work);
  63. static inline struct amdgpu_device *get_amdgpu_device(struct kgd_dev *kgd)
  64. {
  65. return (struct amdgpu_device *)kgd;
  66. }
  67. static bool check_if_add_bo_to_vm(struct amdgpu_vm *avm,
  68. struct kgd_mem *mem)
  69. {
  70. struct kfd_bo_va_list *entry;
  71. list_for_each_entry(entry, &mem->bo_va_list, bo_list)
  72. if (entry->bo_va->base.vm == avm)
  73. return false;
  74. return true;
  75. }
  76. /* Set memory usage limits. Current, limits are
  77. * System (kernel) memory - 3/8th System RAM
  78. * Userptr memory - 3/4th System RAM
  79. */
  80. void amdgpu_amdkfd_gpuvm_init_mem_limits(void)
  81. {
  82. struct sysinfo si;
  83. uint64_t mem;
  84. si_meminfo(&si);
  85. mem = si.totalram - si.totalhigh;
  86. mem *= si.mem_unit;
  87. spin_lock_init(&kfd_mem_limit.mem_limit_lock);
  88. kfd_mem_limit.max_system_mem_limit = (mem >> 1) - (mem >> 3);
  89. kfd_mem_limit.max_userptr_mem_limit = mem - (mem >> 2);
  90. pr_debug("Kernel memory limit %lluM, userptr limit %lluM\n",
  91. (kfd_mem_limit.max_system_mem_limit >> 20),
  92. (kfd_mem_limit.max_userptr_mem_limit >> 20));
  93. }
  94. static int amdgpu_amdkfd_reserve_system_mem_limit(struct amdgpu_device *adev,
  95. uint64_t size, u32 domain)
  96. {
  97. size_t acc_size;
  98. int ret = 0;
  99. acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
  100. sizeof(struct amdgpu_bo));
  101. spin_lock(&kfd_mem_limit.mem_limit_lock);
  102. if (domain == AMDGPU_GEM_DOMAIN_GTT) {
  103. if (kfd_mem_limit.system_mem_used + (acc_size + size) >
  104. kfd_mem_limit.max_system_mem_limit) {
  105. ret = -ENOMEM;
  106. goto err_no_mem;
  107. }
  108. kfd_mem_limit.system_mem_used += (acc_size + size);
  109. } else if (domain == AMDGPU_GEM_DOMAIN_CPU) {
  110. if ((kfd_mem_limit.system_mem_used + acc_size >
  111. kfd_mem_limit.max_system_mem_limit) ||
  112. (kfd_mem_limit.userptr_mem_used + (size + acc_size) >
  113. kfd_mem_limit.max_userptr_mem_limit)) {
  114. ret = -ENOMEM;
  115. goto err_no_mem;
  116. }
  117. kfd_mem_limit.system_mem_used += acc_size;
  118. kfd_mem_limit.userptr_mem_used += size;
  119. }
  120. err_no_mem:
  121. spin_unlock(&kfd_mem_limit.mem_limit_lock);
  122. return ret;
  123. }
  124. static void unreserve_system_mem_limit(struct amdgpu_device *adev,
  125. uint64_t size, u32 domain)
  126. {
  127. size_t acc_size;
  128. acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
  129. sizeof(struct amdgpu_bo));
  130. spin_lock(&kfd_mem_limit.mem_limit_lock);
  131. if (domain == AMDGPU_GEM_DOMAIN_GTT) {
  132. kfd_mem_limit.system_mem_used -= (acc_size + size);
  133. } else if (domain == AMDGPU_GEM_DOMAIN_CPU) {
  134. kfd_mem_limit.system_mem_used -= acc_size;
  135. kfd_mem_limit.userptr_mem_used -= size;
  136. }
  137. WARN_ONCE(kfd_mem_limit.system_mem_used < 0,
  138. "kfd system memory accounting unbalanced");
  139. WARN_ONCE(kfd_mem_limit.userptr_mem_used < 0,
  140. "kfd userptr memory accounting unbalanced");
  141. spin_unlock(&kfd_mem_limit.mem_limit_lock);
  142. }
  143. void amdgpu_amdkfd_unreserve_system_memory_limit(struct amdgpu_bo *bo)
  144. {
  145. spin_lock(&kfd_mem_limit.mem_limit_lock);
  146. if (bo->flags & AMDGPU_AMDKFD_USERPTR_BO) {
  147. kfd_mem_limit.system_mem_used -= bo->tbo.acc_size;
  148. kfd_mem_limit.userptr_mem_used -= amdgpu_bo_size(bo);
  149. } else if (bo->preferred_domains == AMDGPU_GEM_DOMAIN_GTT) {
  150. kfd_mem_limit.system_mem_used -=
  151. (bo->tbo.acc_size + amdgpu_bo_size(bo));
  152. }
  153. WARN_ONCE(kfd_mem_limit.system_mem_used < 0,
  154. "kfd system memory accounting unbalanced");
  155. WARN_ONCE(kfd_mem_limit.userptr_mem_used < 0,
  156. "kfd userptr memory accounting unbalanced");
  157. spin_unlock(&kfd_mem_limit.mem_limit_lock);
  158. }
  159. /* amdgpu_amdkfd_remove_eviction_fence - Removes eviction fence(s) from BO's
  160. * reservation object.
  161. *
  162. * @bo: [IN] Remove eviction fence(s) from this BO
  163. * @ef: [IN] If ef is specified, then this eviction fence is removed if it
  164. * is present in the shared list.
  165. * @ef_list: [OUT] Returns list of eviction fences. These fences are removed
  166. * from BO's reservation object shared list.
  167. * @ef_count: [OUT] Number of fences in ef_list.
  168. *
  169. * NOTE: If called with ef_list, then amdgpu_amdkfd_add_eviction_fence must be
  170. * called to restore the eviction fences and to avoid memory leak. This is
  171. * useful for shared BOs.
  172. * NOTE: Must be called with BO reserved i.e. bo->tbo.resv->lock held.
  173. */
  174. static int amdgpu_amdkfd_remove_eviction_fence(struct amdgpu_bo *bo,
  175. struct amdgpu_amdkfd_fence *ef,
  176. struct amdgpu_amdkfd_fence ***ef_list,
  177. unsigned int *ef_count)
  178. {
  179. struct reservation_object *resv = bo->tbo.resv;
  180. struct reservation_object_list *old, *new;
  181. unsigned int i, j, k;
  182. if (!ef && !ef_list)
  183. return -EINVAL;
  184. if (ef_list) {
  185. *ef_list = NULL;
  186. *ef_count = 0;
  187. }
  188. old = reservation_object_get_list(resv);
  189. if (!old)
  190. return 0;
  191. new = kmalloc(offsetof(typeof(*new), shared[old->shared_max]),
  192. GFP_KERNEL);
  193. if (!new)
  194. return -ENOMEM;
  195. /* Go through all the shared fences in the resevation object and sort
  196. * the interesting ones to the end of the list.
  197. */
  198. for (i = 0, j = old->shared_count, k = 0; i < old->shared_count; ++i) {
  199. struct dma_fence *f;
  200. f = rcu_dereference_protected(old->shared[i],
  201. reservation_object_held(resv));
  202. if ((ef && f->context == ef->base.context) ||
  203. (!ef && to_amdgpu_amdkfd_fence(f)))
  204. RCU_INIT_POINTER(new->shared[--j], f);
  205. else
  206. RCU_INIT_POINTER(new->shared[k++], f);
  207. }
  208. new->shared_max = old->shared_max;
  209. new->shared_count = k;
  210. if (!ef) {
  211. unsigned int count = old->shared_count - j;
  212. /* Alloc memory for count number of eviction fence pointers.
  213. * Fill the ef_list array and ef_count
  214. */
  215. *ef_list = kcalloc(count, sizeof(**ef_list), GFP_KERNEL);
  216. *ef_count = count;
  217. if (!*ef_list) {
  218. kfree(new);
  219. return -ENOMEM;
  220. }
  221. }
  222. /* Install the new fence list, seqcount provides the barriers */
  223. preempt_disable();
  224. write_seqcount_begin(&resv->seq);
  225. RCU_INIT_POINTER(resv->fence, new);
  226. write_seqcount_end(&resv->seq);
  227. preempt_enable();
  228. /* Drop the references to the removed fences or move them to ef_list */
  229. for (i = j, k = 0; i < old->shared_count; ++i) {
  230. struct dma_fence *f;
  231. f = rcu_dereference_protected(new->shared[i],
  232. reservation_object_held(resv));
  233. if (!ef)
  234. (*ef_list)[k++] = to_amdgpu_amdkfd_fence(f);
  235. else
  236. dma_fence_put(f);
  237. }
  238. kfree_rcu(old, rcu);
  239. return 0;
  240. }
  241. /* amdgpu_amdkfd_add_eviction_fence - Adds eviction fence(s) back into BO's
  242. * reservation object.
  243. *
  244. * @bo: [IN] Add eviction fences to this BO
  245. * @ef_list: [IN] List of eviction fences to be added
  246. * @ef_count: [IN] Number of fences in ef_list.
  247. *
  248. * NOTE: Must call amdgpu_amdkfd_remove_eviction_fence before calling this
  249. * function.
  250. */
  251. static void amdgpu_amdkfd_add_eviction_fence(struct amdgpu_bo *bo,
  252. struct amdgpu_amdkfd_fence **ef_list,
  253. unsigned int ef_count)
  254. {
  255. int i;
  256. if (!ef_list || !ef_count)
  257. return;
  258. for (i = 0; i < ef_count; i++) {
  259. amdgpu_bo_fence(bo, &ef_list[i]->base, true);
  260. /* Re-adding the fence takes an additional reference. Drop that
  261. * reference.
  262. */
  263. dma_fence_put(&ef_list[i]->base);
  264. }
  265. kfree(ef_list);
  266. }
  267. static int amdgpu_amdkfd_bo_validate(struct amdgpu_bo *bo, uint32_t domain,
  268. bool wait)
  269. {
  270. struct ttm_operation_ctx ctx = { false, false };
  271. int ret;
  272. if (WARN(amdgpu_ttm_tt_get_usermm(bo->tbo.ttm),
  273. "Called with userptr BO"))
  274. return -EINVAL;
  275. amdgpu_bo_placement_from_domain(bo, domain);
  276. ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
  277. if (ret)
  278. goto validate_fail;
  279. if (wait) {
  280. struct amdgpu_amdkfd_fence **ef_list;
  281. unsigned int ef_count;
  282. ret = amdgpu_amdkfd_remove_eviction_fence(bo, NULL, &ef_list,
  283. &ef_count);
  284. if (ret)
  285. goto validate_fail;
  286. ttm_bo_wait(&bo->tbo, false, false);
  287. amdgpu_amdkfd_add_eviction_fence(bo, ef_list, ef_count);
  288. }
  289. validate_fail:
  290. return ret;
  291. }
  292. static int amdgpu_amdkfd_validate(void *param, struct amdgpu_bo *bo)
  293. {
  294. struct amdgpu_vm_parser *p = param;
  295. return amdgpu_amdkfd_bo_validate(bo, p->domain, p->wait);
  296. }
  297. /* vm_validate_pt_pd_bos - Validate page table and directory BOs
  298. *
  299. * Page directories are not updated here because huge page handling
  300. * during page table updates can invalidate page directory entries
  301. * again. Page directories are only updated after updating page
  302. * tables.
  303. */
  304. static int vm_validate_pt_pd_bos(struct amdgpu_vm *vm)
  305. {
  306. struct amdgpu_bo *pd = vm->root.base.bo;
  307. struct amdgpu_device *adev = amdgpu_ttm_adev(pd->tbo.bdev);
  308. struct amdgpu_vm_parser param;
  309. int ret;
  310. param.domain = AMDGPU_GEM_DOMAIN_VRAM;
  311. param.wait = false;
  312. ret = amdgpu_vm_validate_pt_bos(adev, vm, amdgpu_amdkfd_validate,
  313. &param);
  314. if (ret) {
  315. pr_err("amdgpu: failed to validate PT BOs\n");
  316. return ret;
  317. }
  318. ret = amdgpu_amdkfd_validate(&param, pd);
  319. if (ret) {
  320. pr_err("amdgpu: failed to validate PD\n");
  321. return ret;
  322. }
  323. vm->pd_phys_addr = amdgpu_gmc_pd_addr(vm->root.base.bo);
  324. if (vm->use_cpu_for_update) {
  325. ret = amdgpu_bo_kmap(pd, NULL);
  326. if (ret) {
  327. pr_err("amdgpu: failed to kmap PD, ret=%d\n", ret);
  328. return ret;
  329. }
  330. }
  331. return 0;
  332. }
  333. static int sync_vm_fence(struct amdgpu_device *adev, struct amdgpu_sync *sync,
  334. struct dma_fence *f)
  335. {
  336. int ret = amdgpu_sync_fence(adev, sync, f, false);
  337. /* Sync objects can't handle multiple GPUs (contexts) updating
  338. * sync->last_vm_update. Fortunately we don't need it for
  339. * KFD's purposes, so we can just drop that fence.
  340. */
  341. if (sync->last_vm_update) {
  342. dma_fence_put(sync->last_vm_update);
  343. sync->last_vm_update = NULL;
  344. }
  345. return ret;
  346. }
  347. static int vm_update_pds(struct amdgpu_vm *vm, struct amdgpu_sync *sync)
  348. {
  349. struct amdgpu_bo *pd = vm->root.base.bo;
  350. struct amdgpu_device *adev = amdgpu_ttm_adev(pd->tbo.bdev);
  351. int ret;
  352. ret = amdgpu_vm_update_directories(adev, vm);
  353. if (ret)
  354. return ret;
  355. return sync_vm_fence(adev, sync, vm->last_update);
  356. }
  357. /* add_bo_to_vm - Add a BO to a VM
  358. *
  359. * Everything that needs to bo done only once when a BO is first added
  360. * to a VM. It can later be mapped and unmapped many times without
  361. * repeating these steps.
  362. *
  363. * 1. Allocate and initialize BO VA entry data structure
  364. * 2. Add BO to the VM
  365. * 3. Determine ASIC-specific PTE flags
  366. * 4. Alloc page tables and directories if needed
  367. * 4a. Validate new page tables and directories
  368. */
  369. static int add_bo_to_vm(struct amdgpu_device *adev, struct kgd_mem *mem,
  370. struct amdgpu_vm *vm, bool is_aql,
  371. struct kfd_bo_va_list **p_bo_va_entry)
  372. {
  373. int ret;
  374. struct kfd_bo_va_list *bo_va_entry;
  375. struct amdgpu_bo *pd = vm->root.base.bo;
  376. struct amdgpu_bo *bo = mem->bo;
  377. uint64_t va = mem->va;
  378. struct list_head *list_bo_va = &mem->bo_va_list;
  379. unsigned long bo_size = bo->tbo.mem.size;
  380. if (!va) {
  381. pr_err("Invalid VA when adding BO to VM\n");
  382. return -EINVAL;
  383. }
  384. if (is_aql)
  385. va += bo_size;
  386. bo_va_entry = kzalloc(sizeof(*bo_va_entry), GFP_KERNEL);
  387. if (!bo_va_entry)
  388. return -ENOMEM;
  389. pr_debug("\t add VA 0x%llx - 0x%llx to vm %p\n", va,
  390. va + bo_size, vm);
  391. /* Add BO to VM internal data structures*/
  392. bo_va_entry->bo_va = amdgpu_vm_bo_add(adev, vm, bo);
  393. if (!bo_va_entry->bo_va) {
  394. ret = -EINVAL;
  395. pr_err("Failed to add BO object to VM. ret == %d\n",
  396. ret);
  397. goto err_vmadd;
  398. }
  399. bo_va_entry->va = va;
  400. bo_va_entry->pte_flags = amdgpu_gmc_get_pte_flags(adev,
  401. mem->mapping_flags);
  402. bo_va_entry->kgd_dev = (void *)adev;
  403. list_add(&bo_va_entry->bo_list, list_bo_va);
  404. if (p_bo_va_entry)
  405. *p_bo_va_entry = bo_va_entry;
  406. /* Allocate new page tables if needed and validate
  407. * them. Clearing of new page tables and validate need to wait
  408. * on move fences. We don't want that to trigger the eviction
  409. * fence, so remove it temporarily.
  410. */
  411. amdgpu_amdkfd_remove_eviction_fence(pd,
  412. vm->process_info->eviction_fence,
  413. NULL, NULL);
  414. ret = amdgpu_vm_alloc_pts(adev, vm, va, amdgpu_bo_size(bo));
  415. if (ret) {
  416. pr_err("Failed to allocate pts, err=%d\n", ret);
  417. goto err_alloc_pts;
  418. }
  419. ret = vm_validate_pt_pd_bos(vm);
  420. if (ret) {
  421. pr_err("validate_pt_pd_bos() failed\n");
  422. goto err_alloc_pts;
  423. }
  424. /* Add the eviction fence back */
  425. amdgpu_bo_fence(pd, &vm->process_info->eviction_fence->base, true);
  426. return 0;
  427. err_alloc_pts:
  428. amdgpu_bo_fence(pd, &vm->process_info->eviction_fence->base, true);
  429. amdgpu_vm_bo_rmv(adev, bo_va_entry->bo_va);
  430. list_del(&bo_va_entry->bo_list);
  431. err_vmadd:
  432. kfree(bo_va_entry);
  433. return ret;
  434. }
  435. static void remove_bo_from_vm(struct amdgpu_device *adev,
  436. struct kfd_bo_va_list *entry, unsigned long size)
  437. {
  438. pr_debug("\t remove VA 0x%llx - 0x%llx in entry %p\n",
  439. entry->va,
  440. entry->va + size, entry);
  441. amdgpu_vm_bo_rmv(adev, entry->bo_va);
  442. list_del(&entry->bo_list);
  443. kfree(entry);
  444. }
  445. static void add_kgd_mem_to_kfd_bo_list(struct kgd_mem *mem,
  446. struct amdkfd_process_info *process_info,
  447. bool userptr)
  448. {
  449. struct ttm_validate_buffer *entry = &mem->validate_list;
  450. struct amdgpu_bo *bo = mem->bo;
  451. INIT_LIST_HEAD(&entry->head);
  452. entry->shared = true;
  453. entry->bo = &bo->tbo;
  454. mutex_lock(&process_info->lock);
  455. if (userptr)
  456. list_add_tail(&entry->head, &process_info->userptr_valid_list);
  457. else
  458. list_add_tail(&entry->head, &process_info->kfd_bo_list);
  459. mutex_unlock(&process_info->lock);
  460. }
  461. /* Initializes user pages. It registers the MMU notifier and validates
  462. * the userptr BO in the GTT domain.
  463. *
  464. * The BO must already be on the userptr_valid_list. Otherwise an
  465. * eviction and restore may happen that leaves the new BO unmapped
  466. * with the user mode queues running.
  467. *
  468. * Takes the process_info->lock to protect against concurrent restore
  469. * workers.
  470. *
  471. * Returns 0 for success, negative errno for errors.
  472. */
  473. static int init_user_pages(struct kgd_mem *mem, struct mm_struct *mm,
  474. uint64_t user_addr)
  475. {
  476. struct amdkfd_process_info *process_info = mem->process_info;
  477. struct amdgpu_bo *bo = mem->bo;
  478. struct ttm_operation_ctx ctx = { true, false };
  479. int ret = 0;
  480. mutex_lock(&process_info->lock);
  481. ret = amdgpu_ttm_tt_set_userptr(bo->tbo.ttm, user_addr, 0);
  482. if (ret) {
  483. pr_err("%s: Failed to set userptr: %d\n", __func__, ret);
  484. goto out;
  485. }
  486. ret = amdgpu_mn_register(bo, user_addr);
  487. if (ret) {
  488. pr_err("%s: Failed to register MMU notifier: %d\n",
  489. __func__, ret);
  490. goto out;
  491. }
  492. /* If no restore worker is running concurrently, user_pages
  493. * should not be allocated
  494. */
  495. WARN(mem->user_pages, "Leaking user_pages array");
  496. mem->user_pages = kvmalloc_array(bo->tbo.ttm->num_pages,
  497. sizeof(struct page *),
  498. GFP_KERNEL | __GFP_ZERO);
  499. if (!mem->user_pages) {
  500. pr_err("%s: Failed to allocate pages array\n", __func__);
  501. ret = -ENOMEM;
  502. goto unregister_out;
  503. }
  504. ret = amdgpu_ttm_tt_get_user_pages(bo->tbo.ttm, mem->user_pages);
  505. if (ret) {
  506. pr_err("%s: Failed to get user pages: %d\n", __func__, ret);
  507. goto free_out;
  508. }
  509. amdgpu_ttm_tt_set_user_pages(bo->tbo.ttm, mem->user_pages);
  510. ret = amdgpu_bo_reserve(bo, true);
  511. if (ret) {
  512. pr_err("%s: Failed to reserve BO\n", __func__);
  513. goto release_out;
  514. }
  515. amdgpu_bo_placement_from_domain(bo, mem->domain);
  516. ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
  517. if (ret)
  518. pr_err("%s: failed to validate BO\n", __func__);
  519. amdgpu_bo_unreserve(bo);
  520. release_out:
  521. if (ret)
  522. release_pages(mem->user_pages, bo->tbo.ttm->num_pages);
  523. free_out:
  524. kvfree(mem->user_pages);
  525. mem->user_pages = NULL;
  526. unregister_out:
  527. if (ret)
  528. amdgpu_mn_unregister(bo);
  529. out:
  530. mutex_unlock(&process_info->lock);
  531. return ret;
  532. }
  533. /* Reserving a BO and its page table BOs must happen atomically to
  534. * avoid deadlocks. Some operations update multiple VMs at once. Track
  535. * all the reservation info in a context structure. Optionally a sync
  536. * object can track VM updates.
  537. */
  538. struct bo_vm_reservation_context {
  539. struct amdgpu_bo_list_entry kfd_bo; /* BO list entry for the KFD BO */
  540. unsigned int n_vms; /* Number of VMs reserved */
  541. struct amdgpu_bo_list_entry *vm_pd; /* Array of VM BO list entries */
  542. struct ww_acquire_ctx ticket; /* Reservation ticket */
  543. struct list_head list, duplicates; /* BO lists */
  544. struct amdgpu_sync *sync; /* Pointer to sync object */
  545. bool reserved; /* Whether BOs are reserved */
  546. };
  547. enum bo_vm_match {
  548. BO_VM_NOT_MAPPED = 0, /* Match VMs where a BO is not mapped */
  549. BO_VM_MAPPED, /* Match VMs where a BO is mapped */
  550. BO_VM_ALL, /* Match all VMs a BO was added to */
  551. };
  552. /**
  553. * reserve_bo_and_vm - reserve a BO and a VM unconditionally.
  554. * @mem: KFD BO structure.
  555. * @vm: the VM to reserve.
  556. * @ctx: the struct that will be used in unreserve_bo_and_vms().
  557. */
  558. static int reserve_bo_and_vm(struct kgd_mem *mem,
  559. struct amdgpu_vm *vm,
  560. struct bo_vm_reservation_context *ctx)
  561. {
  562. struct amdgpu_bo *bo = mem->bo;
  563. int ret;
  564. WARN_ON(!vm);
  565. ctx->reserved = false;
  566. ctx->n_vms = 1;
  567. ctx->sync = &mem->sync;
  568. INIT_LIST_HEAD(&ctx->list);
  569. INIT_LIST_HEAD(&ctx->duplicates);
  570. ctx->vm_pd = kcalloc(ctx->n_vms, sizeof(*ctx->vm_pd), GFP_KERNEL);
  571. if (!ctx->vm_pd)
  572. return -ENOMEM;
  573. ctx->kfd_bo.priority = 0;
  574. ctx->kfd_bo.tv.bo = &bo->tbo;
  575. ctx->kfd_bo.tv.shared = true;
  576. ctx->kfd_bo.user_pages = NULL;
  577. list_add(&ctx->kfd_bo.tv.head, &ctx->list);
  578. amdgpu_vm_get_pd_bo(vm, &ctx->list, &ctx->vm_pd[0]);
  579. ret = ttm_eu_reserve_buffers(&ctx->ticket, &ctx->list,
  580. false, &ctx->duplicates);
  581. if (!ret)
  582. ctx->reserved = true;
  583. else {
  584. pr_err("Failed to reserve buffers in ttm\n");
  585. kfree(ctx->vm_pd);
  586. ctx->vm_pd = NULL;
  587. }
  588. return ret;
  589. }
  590. /**
  591. * reserve_bo_and_cond_vms - reserve a BO and some VMs conditionally
  592. * @mem: KFD BO structure.
  593. * @vm: the VM to reserve. If NULL, then all VMs associated with the BO
  594. * is used. Otherwise, a single VM associated with the BO.
  595. * @map_type: the mapping status that will be used to filter the VMs.
  596. * @ctx: the struct that will be used in unreserve_bo_and_vms().
  597. *
  598. * Returns 0 for success, negative for failure.
  599. */
  600. static int reserve_bo_and_cond_vms(struct kgd_mem *mem,
  601. struct amdgpu_vm *vm, enum bo_vm_match map_type,
  602. struct bo_vm_reservation_context *ctx)
  603. {
  604. struct amdgpu_bo *bo = mem->bo;
  605. struct kfd_bo_va_list *entry;
  606. unsigned int i;
  607. int ret;
  608. ctx->reserved = false;
  609. ctx->n_vms = 0;
  610. ctx->vm_pd = NULL;
  611. ctx->sync = &mem->sync;
  612. INIT_LIST_HEAD(&ctx->list);
  613. INIT_LIST_HEAD(&ctx->duplicates);
  614. list_for_each_entry(entry, &mem->bo_va_list, bo_list) {
  615. if ((vm && vm != entry->bo_va->base.vm) ||
  616. (entry->is_mapped != map_type
  617. && map_type != BO_VM_ALL))
  618. continue;
  619. ctx->n_vms++;
  620. }
  621. if (ctx->n_vms != 0) {
  622. ctx->vm_pd = kcalloc(ctx->n_vms, sizeof(*ctx->vm_pd),
  623. GFP_KERNEL);
  624. if (!ctx->vm_pd)
  625. return -ENOMEM;
  626. }
  627. ctx->kfd_bo.priority = 0;
  628. ctx->kfd_bo.tv.bo = &bo->tbo;
  629. ctx->kfd_bo.tv.shared = true;
  630. ctx->kfd_bo.user_pages = NULL;
  631. list_add(&ctx->kfd_bo.tv.head, &ctx->list);
  632. i = 0;
  633. list_for_each_entry(entry, &mem->bo_va_list, bo_list) {
  634. if ((vm && vm != entry->bo_va->base.vm) ||
  635. (entry->is_mapped != map_type
  636. && map_type != BO_VM_ALL))
  637. continue;
  638. amdgpu_vm_get_pd_bo(entry->bo_va->base.vm, &ctx->list,
  639. &ctx->vm_pd[i]);
  640. i++;
  641. }
  642. ret = ttm_eu_reserve_buffers(&ctx->ticket, &ctx->list,
  643. false, &ctx->duplicates);
  644. if (!ret)
  645. ctx->reserved = true;
  646. else
  647. pr_err("Failed to reserve buffers in ttm.\n");
  648. if (ret) {
  649. kfree(ctx->vm_pd);
  650. ctx->vm_pd = NULL;
  651. }
  652. return ret;
  653. }
  654. /**
  655. * unreserve_bo_and_vms - Unreserve BO and VMs from a reservation context
  656. * @ctx: Reservation context to unreserve
  657. * @wait: Optionally wait for a sync object representing pending VM updates
  658. * @intr: Whether the wait is interruptible
  659. *
  660. * Also frees any resources allocated in
  661. * reserve_bo_and_(cond_)vm(s). Returns the status from
  662. * amdgpu_sync_wait.
  663. */
  664. static int unreserve_bo_and_vms(struct bo_vm_reservation_context *ctx,
  665. bool wait, bool intr)
  666. {
  667. int ret = 0;
  668. if (wait)
  669. ret = amdgpu_sync_wait(ctx->sync, intr);
  670. if (ctx->reserved)
  671. ttm_eu_backoff_reservation(&ctx->ticket, &ctx->list);
  672. kfree(ctx->vm_pd);
  673. ctx->sync = NULL;
  674. ctx->reserved = false;
  675. ctx->vm_pd = NULL;
  676. return ret;
  677. }
  678. static int unmap_bo_from_gpuvm(struct amdgpu_device *adev,
  679. struct kfd_bo_va_list *entry,
  680. struct amdgpu_sync *sync)
  681. {
  682. struct amdgpu_bo_va *bo_va = entry->bo_va;
  683. struct amdgpu_vm *vm = bo_va->base.vm;
  684. struct amdgpu_bo *pd = vm->root.base.bo;
  685. /* Remove eviction fence from PD (and thereby from PTs too as
  686. * they share the resv. object). Otherwise during PT update
  687. * job (see amdgpu_vm_bo_update_mapping), eviction fence would
  688. * get added to job->sync object and job execution would
  689. * trigger the eviction fence.
  690. */
  691. amdgpu_amdkfd_remove_eviction_fence(pd,
  692. vm->process_info->eviction_fence,
  693. NULL, NULL);
  694. amdgpu_vm_bo_unmap(adev, bo_va, entry->va);
  695. amdgpu_vm_clear_freed(adev, vm, &bo_va->last_pt_update);
  696. /* Add the eviction fence back */
  697. amdgpu_bo_fence(pd, &vm->process_info->eviction_fence->base, true);
  698. sync_vm_fence(adev, sync, bo_va->last_pt_update);
  699. return 0;
  700. }
  701. static int update_gpuvm_pte(struct amdgpu_device *adev,
  702. struct kfd_bo_va_list *entry,
  703. struct amdgpu_sync *sync)
  704. {
  705. int ret;
  706. struct amdgpu_vm *vm;
  707. struct amdgpu_bo_va *bo_va;
  708. struct amdgpu_bo *bo;
  709. bo_va = entry->bo_va;
  710. vm = bo_va->base.vm;
  711. bo = bo_va->base.bo;
  712. /* Update the page tables */
  713. ret = amdgpu_vm_bo_update(adev, bo_va, false);
  714. if (ret) {
  715. pr_err("amdgpu_vm_bo_update failed\n");
  716. return ret;
  717. }
  718. return sync_vm_fence(adev, sync, bo_va->last_pt_update);
  719. }
  720. static int map_bo_to_gpuvm(struct amdgpu_device *adev,
  721. struct kfd_bo_va_list *entry, struct amdgpu_sync *sync,
  722. bool no_update_pte)
  723. {
  724. int ret;
  725. /* Set virtual address for the allocation */
  726. ret = amdgpu_vm_bo_map(adev, entry->bo_va, entry->va, 0,
  727. amdgpu_bo_size(entry->bo_va->base.bo),
  728. entry->pte_flags);
  729. if (ret) {
  730. pr_err("Failed to map VA 0x%llx in vm. ret %d\n",
  731. entry->va, ret);
  732. return ret;
  733. }
  734. if (no_update_pte)
  735. return 0;
  736. ret = update_gpuvm_pte(adev, entry, sync);
  737. if (ret) {
  738. pr_err("update_gpuvm_pte() failed\n");
  739. goto update_gpuvm_pte_failed;
  740. }
  741. return 0;
  742. update_gpuvm_pte_failed:
  743. unmap_bo_from_gpuvm(adev, entry, sync);
  744. return ret;
  745. }
  746. static int process_validate_vms(struct amdkfd_process_info *process_info)
  747. {
  748. struct amdgpu_vm *peer_vm;
  749. int ret;
  750. list_for_each_entry(peer_vm, &process_info->vm_list_head,
  751. vm_list_node) {
  752. ret = vm_validate_pt_pd_bos(peer_vm);
  753. if (ret)
  754. return ret;
  755. }
  756. return 0;
  757. }
  758. static int process_update_pds(struct amdkfd_process_info *process_info,
  759. struct amdgpu_sync *sync)
  760. {
  761. struct amdgpu_vm *peer_vm;
  762. int ret;
  763. list_for_each_entry(peer_vm, &process_info->vm_list_head,
  764. vm_list_node) {
  765. ret = vm_update_pds(peer_vm, sync);
  766. if (ret)
  767. return ret;
  768. }
  769. return 0;
  770. }
  771. static int init_kfd_vm(struct amdgpu_vm *vm, void **process_info,
  772. struct dma_fence **ef)
  773. {
  774. struct amdkfd_process_info *info = NULL;
  775. int ret;
  776. if (!*process_info) {
  777. info = kzalloc(sizeof(*info), GFP_KERNEL);
  778. if (!info)
  779. return -ENOMEM;
  780. mutex_init(&info->lock);
  781. INIT_LIST_HEAD(&info->vm_list_head);
  782. INIT_LIST_HEAD(&info->kfd_bo_list);
  783. INIT_LIST_HEAD(&info->userptr_valid_list);
  784. INIT_LIST_HEAD(&info->userptr_inval_list);
  785. info->eviction_fence =
  786. amdgpu_amdkfd_fence_create(dma_fence_context_alloc(1),
  787. current->mm);
  788. if (!info->eviction_fence) {
  789. pr_err("Failed to create eviction fence\n");
  790. ret = -ENOMEM;
  791. goto create_evict_fence_fail;
  792. }
  793. info->pid = get_task_pid(current->group_leader, PIDTYPE_PID);
  794. atomic_set(&info->evicted_bos, 0);
  795. INIT_DELAYED_WORK(&info->restore_userptr_work,
  796. amdgpu_amdkfd_restore_userptr_worker);
  797. *process_info = info;
  798. *ef = dma_fence_get(&info->eviction_fence->base);
  799. }
  800. vm->process_info = *process_info;
  801. /* Validate page directory and attach eviction fence */
  802. ret = amdgpu_bo_reserve(vm->root.base.bo, true);
  803. if (ret)
  804. goto reserve_pd_fail;
  805. ret = vm_validate_pt_pd_bos(vm);
  806. if (ret) {
  807. pr_err("validate_pt_pd_bos() failed\n");
  808. goto validate_pd_fail;
  809. }
  810. ret = ttm_bo_wait(&vm->root.base.bo->tbo, false, false);
  811. if (ret)
  812. goto wait_pd_fail;
  813. amdgpu_bo_fence(vm->root.base.bo,
  814. &vm->process_info->eviction_fence->base, true);
  815. amdgpu_bo_unreserve(vm->root.base.bo);
  816. /* Update process info */
  817. mutex_lock(&vm->process_info->lock);
  818. list_add_tail(&vm->vm_list_node,
  819. &(vm->process_info->vm_list_head));
  820. vm->process_info->n_vms++;
  821. mutex_unlock(&vm->process_info->lock);
  822. return 0;
  823. wait_pd_fail:
  824. validate_pd_fail:
  825. amdgpu_bo_unreserve(vm->root.base.bo);
  826. reserve_pd_fail:
  827. vm->process_info = NULL;
  828. if (info) {
  829. /* Two fence references: one in info and one in *ef */
  830. dma_fence_put(&info->eviction_fence->base);
  831. dma_fence_put(*ef);
  832. *ef = NULL;
  833. *process_info = NULL;
  834. put_pid(info->pid);
  835. create_evict_fence_fail:
  836. mutex_destroy(&info->lock);
  837. kfree(info);
  838. }
  839. return ret;
  840. }
  841. int amdgpu_amdkfd_gpuvm_create_process_vm(struct kgd_dev *kgd, unsigned int pasid,
  842. void **vm, void **process_info,
  843. struct dma_fence **ef)
  844. {
  845. struct amdgpu_device *adev = get_amdgpu_device(kgd);
  846. struct amdgpu_vm *new_vm;
  847. int ret;
  848. new_vm = kzalloc(sizeof(*new_vm), GFP_KERNEL);
  849. if (!new_vm)
  850. return -ENOMEM;
  851. /* Initialize AMDGPU part of the VM */
  852. ret = amdgpu_vm_init(adev, new_vm, AMDGPU_VM_CONTEXT_COMPUTE, pasid);
  853. if (ret) {
  854. pr_err("Failed init vm ret %d\n", ret);
  855. goto amdgpu_vm_init_fail;
  856. }
  857. /* Initialize KFD part of the VM and process info */
  858. ret = init_kfd_vm(new_vm, process_info, ef);
  859. if (ret)
  860. goto init_kfd_vm_fail;
  861. *vm = (void *) new_vm;
  862. return 0;
  863. init_kfd_vm_fail:
  864. amdgpu_vm_fini(adev, new_vm);
  865. amdgpu_vm_init_fail:
  866. kfree(new_vm);
  867. return ret;
  868. }
  869. int amdgpu_amdkfd_gpuvm_acquire_process_vm(struct kgd_dev *kgd,
  870. struct file *filp, unsigned int pasid,
  871. void **vm, void **process_info,
  872. struct dma_fence **ef)
  873. {
  874. struct amdgpu_device *adev = get_amdgpu_device(kgd);
  875. struct drm_file *drm_priv = filp->private_data;
  876. struct amdgpu_fpriv *drv_priv = drm_priv->driver_priv;
  877. struct amdgpu_vm *avm = &drv_priv->vm;
  878. int ret;
  879. /* Already a compute VM? */
  880. if (avm->process_info)
  881. return -EINVAL;
  882. /* Convert VM into a compute VM */
  883. ret = amdgpu_vm_make_compute(adev, avm, pasid);
  884. if (ret)
  885. return ret;
  886. /* Initialize KFD part of the VM and process info */
  887. ret = init_kfd_vm(avm, process_info, ef);
  888. if (ret)
  889. return ret;
  890. *vm = (void *)avm;
  891. return 0;
  892. }
  893. void amdgpu_amdkfd_gpuvm_destroy_cb(struct amdgpu_device *adev,
  894. struct amdgpu_vm *vm)
  895. {
  896. struct amdkfd_process_info *process_info = vm->process_info;
  897. struct amdgpu_bo *pd = vm->root.base.bo;
  898. if (!process_info)
  899. return;
  900. /* Release eviction fence from PD */
  901. amdgpu_bo_reserve(pd, false);
  902. amdgpu_bo_fence(pd, NULL, false);
  903. amdgpu_bo_unreserve(pd);
  904. /* Update process info */
  905. mutex_lock(&process_info->lock);
  906. process_info->n_vms--;
  907. list_del(&vm->vm_list_node);
  908. mutex_unlock(&process_info->lock);
  909. /* Release per-process resources when last compute VM is destroyed */
  910. if (!process_info->n_vms) {
  911. WARN_ON(!list_empty(&process_info->kfd_bo_list));
  912. WARN_ON(!list_empty(&process_info->userptr_valid_list));
  913. WARN_ON(!list_empty(&process_info->userptr_inval_list));
  914. dma_fence_put(&process_info->eviction_fence->base);
  915. cancel_delayed_work_sync(&process_info->restore_userptr_work);
  916. put_pid(process_info->pid);
  917. mutex_destroy(&process_info->lock);
  918. kfree(process_info);
  919. }
  920. }
  921. void amdgpu_amdkfd_gpuvm_destroy_process_vm(struct kgd_dev *kgd, void *vm)
  922. {
  923. struct amdgpu_device *adev = get_amdgpu_device(kgd);
  924. struct amdgpu_vm *avm = (struct amdgpu_vm *)vm;
  925. if (WARN_ON(!kgd || !vm))
  926. return;
  927. pr_debug("Destroying process vm %p\n", vm);
  928. /* Release the VM context */
  929. amdgpu_vm_fini(adev, avm);
  930. kfree(vm);
  931. }
  932. void amdgpu_amdkfd_gpuvm_release_process_vm(struct kgd_dev *kgd, void *vm)
  933. {
  934. struct amdgpu_device *adev = get_amdgpu_device(kgd);
  935. struct amdgpu_vm *avm = (struct amdgpu_vm *)vm;
  936. if (WARN_ON(!kgd || !vm))
  937. return;
  938. pr_debug("Releasing process vm %p\n", vm);
  939. /* The original pasid of amdgpu vm has already been
  940. * released during making a amdgpu vm to a compute vm
  941. * The current pasid is managed by kfd and will be
  942. * released on kfd process destroy. Set amdgpu pasid
  943. * to 0 to avoid duplicate release.
  944. */
  945. amdgpu_vm_release_compute(adev, avm);
  946. }
  947. uint32_t amdgpu_amdkfd_gpuvm_get_process_page_dir(void *vm)
  948. {
  949. struct amdgpu_vm *avm = (struct amdgpu_vm *)vm;
  950. return avm->pd_phys_addr >> AMDGPU_GPU_PAGE_SHIFT;
  951. }
  952. int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(
  953. struct kgd_dev *kgd, uint64_t va, uint64_t size,
  954. void *vm, struct kgd_mem **mem,
  955. uint64_t *offset, uint32_t flags)
  956. {
  957. struct amdgpu_device *adev = get_amdgpu_device(kgd);
  958. struct amdgpu_vm *avm = (struct amdgpu_vm *)vm;
  959. uint64_t user_addr = 0;
  960. struct amdgpu_bo *bo;
  961. struct amdgpu_bo_param bp;
  962. int byte_align;
  963. u32 domain, alloc_domain;
  964. u64 alloc_flags;
  965. uint32_t mapping_flags;
  966. int ret;
  967. /*
  968. * Check on which domain to allocate BO
  969. */
  970. if (flags & ALLOC_MEM_FLAGS_VRAM) {
  971. domain = alloc_domain = AMDGPU_GEM_DOMAIN_VRAM;
  972. alloc_flags = AMDGPU_GEM_CREATE_VRAM_CLEARED;
  973. alloc_flags |= (flags & ALLOC_MEM_FLAGS_PUBLIC) ?
  974. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED :
  975. AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
  976. } else if (flags & ALLOC_MEM_FLAGS_GTT) {
  977. domain = alloc_domain = AMDGPU_GEM_DOMAIN_GTT;
  978. alloc_flags = 0;
  979. } else if (flags & ALLOC_MEM_FLAGS_USERPTR) {
  980. domain = AMDGPU_GEM_DOMAIN_GTT;
  981. alloc_domain = AMDGPU_GEM_DOMAIN_CPU;
  982. alloc_flags = 0;
  983. if (!offset || !*offset)
  984. return -EINVAL;
  985. user_addr = *offset;
  986. } else {
  987. return -EINVAL;
  988. }
  989. *mem = kzalloc(sizeof(struct kgd_mem), GFP_KERNEL);
  990. if (!*mem)
  991. return -ENOMEM;
  992. INIT_LIST_HEAD(&(*mem)->bo_va_list);
  993. mutex_init(&(*mem)->lock);
  994. (*mem)->aql_queue = !!(flags & ALLOC_MEM_FLAGS_AQL_QUEUE_MEM);
  995. /* Workaround for AQL queue wraparound bug. Map the same
  996. * memory twice. That means we only actually allocate half
  997. * the memory.
  998. */
  999. if ((*mem)->aql_queue)
  1000. size = size >> 1;
  1001. /* Workaround for TLB bug on older VI chips */
  1002. byte_align = (adev->family == AMDGPU_FAMILY_VI &&
  1003. adev->asic_type != CHIP_FIJI &&
  1004. adev->asic_type != CHIP_POLARIS10 &&
  1005. adev->asic_type != CHIP_POLARIS11) ?
  1006. VI_BO_SIZE_ALIGN : 1;
  1007. mapping_flags = AMDGPU_VM_PAGE_READABLE;
  1008. if (flags & ALLOC_MEM_FLAGS_WRITABLE)
  1009. mapping_flags |= AMDGPU_VM_PAGE_WRITEABLE;
  1010. if (flags & ALLOC_MEM_FLAGS_EXECUTABLE)
  1011. mapping_flags |= AMDGPU_VM_PAGE_EXECUTABLE;
  1012. if (flags & ALLOC_MEM_FLAGS_COHERENT)
  1013. mapping_flags |= AMDGPU_VM_MTYPE_UC;
  1014. else
  1015. mapping_flags |= AMDGPU_VM_MTYPE_NC;
  1016. (*mem)->mapping_flags = mapping_flags;
  1017. amdgpu_sync_create(&(*mem)->sync);
  1018. ret = amdgpu_amdkfd_reserve_system_mem_limit(adev, size, alloc_domain);
  1019. if (ret) {
  1020. pr_debug("Insufficient system memory\n");
  1021. goto err_reserve_system_mem;
  1022. }
  1023. pr_debug("\tcreate BO VA 0x%llx size 0x%llx domain %s\n",
  1024. va, size, domain_string(alloc_domain));
  1025. memset(&bp, 0, sizeof(bp));
  1026. bp.size = size;
  1027. bp.byte_align = byte_align;
  1028. bp.domain = alloc_domain;
  1029. bp.flags = alloc_flags;
  1030. bp.type = ttm_bo_type_device;
  1031. bp.resv = NULL;
  1032. ret = amdgpu_bo_create(adev, &bp, &bo);
  1033. if (ret) {
  1034. pr_debug("Failed to create BO on domain %s. ret %d\n",
  1035. domain_string(alloc_domain), ret);
  1036. goto err_bo_create;
  1037. }
  1038. bo->kfd_bo = *mem;
  1039. (*mem)->bo = bo;
  1040. if (user_addr)
  1041. bo->flags |= AMDGPU_AMDKFD_USERPTR_BO;
  1042. (*mem)->va = va;
  1043. (*mem)->domain = domain;
  1044. (*mem)->mapped_to_gpu_memory = 0;
  1045. (*mem)->process_info = avm->process_info;
  1046. add_kgd_mem_to_kfd_bo_list(*mem, avm->process_info, user_addr);
  1047. if (user_addr) {
  1048. ret = init_user_pages(*mem, current->mm, user_addr);
  1049. if (ret) {
  1050. mutex_lock(&avm->process_info->lock);
  1051. list_del(&(*mem)->validate_list.head);
  1052. mutex_unlock(&avm->process_info->lock);
  1053. goto allocate_init_user_pages_failed;
  1054. }
  1055. }
  1056. if (offset)
  1057. *offset = amdgpu_bo_mmap_offset(bo);
  1058. return 0;
  1059. allocate_init_user_pages_failed:
  1060. amdgpu_bo_unref(&bo);
  1061. /* Don't unreserve system mem limit twice */
  1062. goto err_reserve_system_mem;
  1063. err_bo_create:
  1064. unreserve_system_mem_limit(adev, size, alloc_domain);
  1065. err_reserve_system_mem:
  1066. mutex_destroy(&(*mem)->lock);
  1067. kfree(*mem);
  1068. return ret;
  1069. }
  1070. int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
  1071. struct kgd_dev *kgd, struct kgd_mem *mem)
  1072. {
  1073. struct amdkfd_process_info *process_info = mem->process_info;
  1074. unsigned long bo_size = mem->bo->tbo.mem.size;
  1075. struct kfd_bo_va_list *entry, *tmp;
  1076. struct bo_vm_reservation_context ctx;
  1077. struct ttm_validate_buffer *bo_list_entry;
  1078. int ret;
  1079. mutex_lock(&mem->lock);
  1080. if (mem->mapped_to_gpu_memory > 0) {
  1081. pr_debug("BO VA 0x%llx size 0x%lx is still mapped.\n",
  1082. mem->va, bo_size);
  1083. mutex_unlock(&mem->lock);
  1084. return -EBUSY;
  1085. }
  1086. mutex_unlock(&mem->lock);
  1087. /* lock is not needed after this, since mem is unused and will
  1088. * be freed anyway
  1089. */
  1090. /* No more MMU notifiers */
  1091. amdgpu_mn_unregister(mem->bo);
  1092. /* Make sure restore workers don't access the BO any more */
  1093. bo_list_entry = &mem->validate_list;
  1094. mutex_lock(&process_info->lock);
  1095. list_del(&bo_list_entry->head);
  1096. mutex_unlock(&process_info->lock);
  1097. /* Free user pages if necessary */
  1098. if (mem->user_pages) {
  1099. pr_debug("%s: Freeing user_pages array\n", __func__);
  1100. if (mem->user_pages[0])
  1101. release_pages(mem->user_pages,
  1102. mem->bo->tbo.ttm->num_pages);
  1103. kvfree(mem->user_pages);
  1104. }
  1105. ret = reserve_bo_and_cond_vms(mem, NULL, BO_VM_ALL, &ctx);
  1106. if (unlikely(ret))
  1107. return ret;
  1108. /* The eviction fence should be removed by the last unmap.
  1109. * TODO: Log an error condition if the bo still has the eviction fence
  1110. * attached
  1111. */
  1112. amdgpu_amdkfd_remove_eviction_fence(mem->bo,
  1113. process_info->eviction_fence,
  1114. NULL, NULL);
  1115. pr_debug("Release VA 0x%llx - 0x%llx\n", mem->va,
  1116. mem->va + bo_size * (1 + mem->aql_queue));
  1117. /* Remove from VM internal data structures */
  1118. list_for_each_entry_safe(entry, tmp, &mem->bo_va_list, bo_list)
  1119. remove_bo_from_vm((struct amdgpu_device *)entry->kgd_dev,
  1120. entry, bo_size);
  1121. ret = unreserve_bo_and_vms(&ctx, false, false);
  1122. /* Free the sync object */
  1123. amdgpu_sync_free(&mem->sync);
  1124. /* Free the BO*/
  1125. amdgpu_bo_unref(&mem->bo);
  1126. mutex_destroy(&mem->lock);
  1127. kfree(mem);
  1128. return ret;
  1129. }
  1130. int amdgpu_amdkfd_gpuvm_map_memory_to_gpu(
  1131. struct kgd_dev *kgd, struct kgd_mem *mem, void *vm)
  1132. {
  1133. struct amdgpu_device *adev = get_amdgpu_device(kgd);
  1134. struct amdgpu_vm *avm = (struct amdgpu_vm *)vm;
  1135. int ret;
  1136. struct amdgpu_bo *bo;
  1137. uint32_t domain;
  1138. struct kfd_bo_va_list *entry;
  1139. struct bo_vm_reservation_context ctx;
  1140. struct kfd_bo_va_list *bo_va_entry = NULL;
  1141. struct kfd_bo_va_list *bo_va_entry_aql = NULL;
  1142. unsigned long bo_size;
  1143. bool is_invalid_userptr = false;
  1144. bo = mem->bo;
  1145. if (!bo) {
  1146. pr_err("Invalid BO when mapping memory to GPU\n");
  1147. return -EINVAL;
  1148. }
  1149. /* Make sure restore is not running concurrently. Since we
  1150. * don't map invalid userptr BOs, we rely on the next restore
  1151. * worker to do the mapping
  1152. */
  1153. mutex_lock(&mem->process_info->lock);
  1154. /* Lock mmap-sem. If we find an invalid userptr BO, we can be
  1155. * sure that the MMU notifier is no longer running
  1156. * concurrently and the queues are actually stopped
  1157. */
  1158. if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm)) {
  1159. down_write(&current->mm->mmap_sem);
  1160. is_invalid_userptr = atomic_read(&mem->invalid);
  1161. up_write(&current->mm->mmap_sem);
  1162. }
  1163. mutex_lock(&mem->lock);
  1164. domain = mem->domain;
  1165. bo_size = bo->tbo.mem.size;
  1166. pr_debug("Map VA 0x%llx - 0x%llx to vm %p domain %s\n",
  1167. mem->va,
  1168. mem->va + bo_size * (1 + mem->aql_queue),
  1169. vm, domain_string(domain));
  1170. ret = reserve_bo_and_vm(mem, vm, &ctx);
  1171. if (unlikely(ret))
  1172. goto out;
  1173. /* Userptr can be marked as "not invalid", but not actually be
  1174. * validated yet (still in the system domain). In that case
  1175. * the queues are still stopped and we can leave mapping for
  1176. * the next restore worker
  1177. */
  1178. if (bo->tbo.mem.mem_type == TTM_PL_SYSTEM)
  1179. is_invalid_userptr = true;
  1180. if (check_if_add_bo_to_vm(avm, mem)) {
  1181. ret = add_bo_to_vm(adev, mem, avm, false,
  1182. &bo_va_entry);
  1183. if (ret)
  1184. goto add_bo_to_vm_failed;
  1185. if (mem->aql_queue) {
  1186. ret = add_bo_to_vm(adev, mem, avm,
  1187. true, &bo_va_entry_aql);
  1188. if (ret)
  1189. goto add_bo_to_vm_failed_aql;
  1190. }
  1191. } else {
  1192. ret = vm_validate_pt_pd_bos(avm);
  1193. if (unlikely(ret))
  1194. goto add_bo_to_vm_failed;
  1195. }
  1196. if (mem->mapped_to_gpu_memory == 0 &&
  1197. !amdgpu_ttm_tt_get_usermm(bo->tbo.ttm)) {
  1198. /* Validate BO only once. The eviction fence gets added to BO
  1199. * the first time it is mapped. Validate will wait for all
  1200. * background evictions to complete.
  1201. */
  1202. ret = amdgpu_amdkfd_bo_validate(bo, domain, true);
  1203. if (ret) {
  1204. pr_debug("Validate failed\n");
  1205. goto map_bo_to_gpuvm_failed;
  1206. }
  1207. }
  1208. list_for_each_entry(entry, &mem->bo_va_list, bo_list) {
  1209. if (entry->bo_va->base.vm == vm && !entry->is_mapped) {
  1210. pr_debug("\t map VA 0x%llx - 0x%llx in entry %p\n",
  1211. entry->va, entry->va + bo_size,
  1212. entry);
  1213. ret = map_bo_to_gpuvm(adev, entry, ctx.sync,
  1214. is_invalid_userptr);
  1215. if (ret) {
  1216. pr_err("Failed to map radeon bo to gpuvm\n");
  1217. goto map_bo_to_gpuvm_failed;
  1218. }
  1219. ret = vm_update_pds(vm, ctx.sync);
  1220. if (ret) {
  1221. pr_err("Failed to update page directories\n");
  1222. goto map_bo_to_gpuvm_failed;
  1223. }
  1224. entry->is_mapped = true;
  1225. mem->mapped_to_gpu_memory++;
  1226. pr_debug("\t INC mapping count %d\n",
  1227. mem->mapped_to_gpu_memory);
  1228. }
  1229. }
  1230. if (!amdgpu_ttm_tt_get_usermm(bo->tbo.ttm) && !bo->pin_count)
  1231. amdgpu_bo_fence(bo,
  1232. &avm->process_info->eviction_fence->base,
  1233. true);
  1234. ret = unreserve_bo_and_vms(&ctx, false, false);
  1235. goto out;
  1236. map_bo_to_gpuvm_failed:
  1237. if (bo_va_entry_aql)
  1238. remove_bo_from_vm(adev, bo_va_entry_aql, bo_size);
  1239. add_bo_to_vm_failed_aql:
  1240. if (bo_va_entry)
  1241. remove_bo_from_vm(adev, bo_va_entry, bo_size);
  1242. add_bo_to_vm_failed:
  1243. unreserve_bo_and_vms(&ctx, false, false);
  1244. out:
  1245. mutex_unlock(&mem->process_info->lock);
  1246. mutex_unlock(&mem->lock);
  1247. return ret;
  1248. }
  1249. int amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
  1250. struct kgd_dev *kgd, struct kgd_mem *mem, void *vm)
  1251. {
  1252. struct amdgpu_device *adev = get_amdgpu_device(kgd);
  1253. struct amdkfd_process_info *process_info =
  1254. ((struct amdgpu_vm *)vm)->process_info;
  1255. unsigned long bo_size = mem->bo->tbo.mem.size;
  1256. struct kfd_bo_va_list *entry;
  1257. struct bo_vm_reservation_context ctx;
  1258. int ret;
  1259. mutex_lock(&mem->lock);
  1260. ret = reserve_bo_and_cond_vms(mem, vm, BO_VM_MAPPED, &ctx);
  1261. if (unlikely(ret))
  1262. goto out;
  1263. /* If no VMs were reserved, it means the BO wasn't actually mapped */
  1264. if (ctx.n_vms == 0) {
  1265. ret = -EINVAL;
  1266. goto unreserve_out;
  1267. }
  1268. ret = vm_validate_pt_pd_bos((struct amdgpu_vm *)vm);
  1269. if (unlikely(ret))
  1270. goto unreserve_out;
  1271. pr_debug("Unmap VA 0x%llx - 0x%llx from vm %p\n",
  1272. mem->va,
  1273. mem->va + bo_size * (1 + mem->aql_queue),
  1274. vm);
  1275. list_for_each_entry(entry, &mem->bo_va_list, bo_list) {
  1276. if (entry->bo_va->base.vm == vm && entry->is_mapped) {
  1277. pr_debug("\t unmap VA 0x%llx - 0x%llx from entry %p\n",
  1278. entry->va,
  1279. entry->va + bo_size,
  1280. entry);
  1281. ret = unmap_bo_from_gpuvm(adev, entry, ctx.sync);
  1282. if (ret == 0) {
  1283. entry->is_mapped = false;
  1284. } else {
  1285. pr_err("failed to unmap VA 0x%llx\n",
  1286. mem->va);
  1287. goto unreserve_out;
  1288. }
  1289. mem->mapped_to_gpu_memory--;
  1290. pr_debug("\t DEC mapping count %d\n",
  1291. mem->mapped_to_gpu_memory);
  1292. }
  1293. }
  1294. /* If BO is unmapped from all VMs, unfence it. It can be evicted if
  1295. * required.
  1296. */
  1297. if (mem->mapped_to_gpu_memory == 0 &&
  1298. !amdgpu_ttm_tt_get_usermm(mem->bo->tbo.ttm) && !mem->bo->pin_count)
  1299. amdgpu_amdkfd_remove_eviction_fence(mem->bo,
  1300. process_info->eviction_fence,
  1301. NULL, NULL);
  1302. unreserve_out:
  1303. unreserve_bo_and_vms(&ctx, false, false);
  1304. out:
  1305. mutex_unlock(&mem->lock);
  1306. return ret;
  1307. }
  1308. int amdgpu_amdkfd_gpuvm_sync_memory(
  1309. struct kgd_dev *kgd, struct kgd_mem *mem, bool intr)
  1310. {
  1311. struct amdgpu_sync sync;
  1312. int ret;
  1313. amdgpu_sync_create(&sync);
  1314. mutex_lock(&mem->lock);
  1315. amdgpu_sync_clone(&mem->sync, &sync);
  1316. mutex_unlock(&mem->lock);
  1317. ret = amdgpu_sync_wait(&sync, intr);
  1318. amdgpu_sync_free(&sync);
  1319. return ret;
  1320. }
  1321. int amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(struct kgd_dev *kgd,
  1322. struct kgd_mem *mem, void **kptr, uint64_t *size)
  1323. {
  1324. int ret;
  1325. struct amdgpu_bo *bo = mem->bo;
  1326. if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm)) {
  1327. pr_err("userptr can't be mapped to kernel\n");
  1328. return -EINVAL;
  1329. }
  1330. /* delete kgd_mem from kfd_bo_list to avoid re-validating
  1331. * this BO in BO's restoring after eviction.
  1332. */
  1333. mutex_lock(&mem->process_info->lock);
  1334. ret = amdgpu_bo_reserve(bo, true);
  1335. if (ret) {
  1336. pr_err("Failed to reserve bo. ret %d\n", ret);
  1337. goto bo_reserve_failed;
  1338. }
  1339. ret = amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT);
  1340. if (ret) {
  1341. pr_err("Failed to pin bo. ret %d\n", ret);
  1342. goto pin_failed;
  1343. }
  1344. ret = amdgpu_bo_kmap(bo, kptr);
  1345. if (ret) {
  1346. pr_err("Failed to map bo to kernel. ret %d\n", ret);
  1347. goto kmap_failed;
  1348. }
  1349. amdgpu_amdkfd_remove_eviction_fence(
  1350. bo, mem->process_info->eviction_fence, NULL, NULL);
  1351. list_del_init(&mem->validate_list.head);
  1352. if (size)
  1353. *size = amdgpu_bo_size(bo);
  1354. amdgpu_bo_unreserve(bo);
  1355. mutex_unlock(&mem->process_info->lock);
  1356. return 0;
  1357. kmap_failed:
  1358. amdgpu_bo_unpin(bo);
  1359. pin_failed:
  1360. amdgpu_bo_unreserve(bo);
  1361. bo_reserve_failed:
  1362. mutex_unlock(&mem->process_info->lock);
  1363. return ret;
  1364. }
  1365. int amdgpu_amdkfd_gpuvm_get_vm_fault_info(struct kgd_dev *kgd,
  1366. struct kfd_vm_fault_info *mem)
  1367. {
  1368. struct amdgpu_device *adev;
  1369. adev = (struct amdgpu_device *)kgd;
  1370. if (atomic_read(&adev->gmc.vm_fault_info_updated) == 1) {
  1371. *mem = *adev->gmc.vm_fault_info;
  1372. mb();
  1373. atomic_set(&adev->gmc.vm_fault_info_updated, 0);
  1374. }
  1375. return 0;
  1376. }
  1377. /* Evict a userptr BO by stopping the queues if necessary
  1378. *
  1379. * Runs in MMU notifier, may be in RECLAIM_FS context. This means it
  1380. * cannot do any memory allocations, and cannot take any locks that
  1381. * are held elsewhere while allocating memory. Therefore this is as
  1382. * simple as possible, using atomic counters.
  1383. *
  1384. * It doesn't do anything to the BO itself. The real work happens in
  1385. * restore, where we get updated page addresses. This function only
  1386. * ensures that GPU access to the BO is stopped.
  1387. */
  1388. int amdgpu_amdkfd_evict_userptr(struct kgd_mem *mem,
  1389. struct mm_struct *mm)
  1390. {
  1391. struct amdkfd_process_info *process_info = mem->process_info;
  1392. int invalid, evicted_bos;
  1393. int r = 0;
  1394. invalid = atomic_inc_return(&mem->invalid);
  1395. evicted_bos = atomic_inc_return(&process_info->evicted_bos);
  1396. if (evicted_bos == 1) {
  1397. /* First eviction, stop the queues */
  1398. r = kgd2kfd->quiesce_mm(mm);
  1399. if (r)
  1400. pr_err("Failed to quiesce KFD\n");
  1401. schedule_delayed_work(&process_info->restore_userptr_work,
  1402. msecs_to_jiffies(AMDGPU_USERPTR_RESTORE_DELAY_MS));
  1403. }
  1404. return r;
  1405. }
  1406. /* Update invalid userptr BOs
  1407. *
  1408. * Moves invalidated (evicted) userptr BOs from userptr_valid_list to
  1409. * userptr_inval_list and updates user pages for all BOs that have
  1410. * been invalidated since their last update.
  1411. */
  1412. static int update_invalid_user_pages(struct amdkfd_process_info *process_info,
  1413. struct mm_struct *mm)
  1414. {
  1415. struct kgd_mem *mem, *tmp_mem;
  1416. struct amdgpu_bo *bo;
  1417. struct ttm_operation_ctx ctx = { false, false };
  1418. int invalid, ret;
  1419. /* Move all invalidated BOs to the userptr_inval_list and
  1420. * release their user pages by migration to the CPU domain
  1421. */
  1422. list_for_each_entry_safe(mem, tmp_mem,
  1423. &process_info->userptr_valid_list,
  1424. validate_list.head) {
  1425. if (!atomic_read(&mem->invalid))
  1426. continue; /* BO is still valid */
  1427. bo = mem->bo;
  1428. if (amdgpu_bo_reserve(bo, true))
  1429. return -EAGAIN;
  1430. amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_CPU);
  1431. ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
  1432. amdgpu_bo_unreserve(bo);
  1433. if (ret) {
  1434. pr_err("%s: Failed to invalidate userptr BO\n",
  1435. __func__);
  1436. return -EAGAIN;
  1437. }
  1438. list_move_tail(&mem->validate_list.head,
  1439. &process_info->userptr_inval_list);
  1440. }
  1441. if (list_empty(&process_info->userptr_inval_list))
  1442. return 0; /* All evicted userptr BOs were freed */
  1443. /* Go through userptr_inval_list and update any invalid user_pages */
  1444. list_for_each_entry(mem, &process_info->userptr_inval_list,
  1445. validate_list.head) {
  1446. invalid = atomic_read(&mem->invalid);
  1447. if (!invalid)
  1448. /* BO hasn't been invalidated since the last
  1449. * revalidation attempt. Keep its BO list.
  1450. */
  1451. continue;
  1452. bo = mem->bo;
  1453. if (!mem->user_pages) {
  1454. mem->user_pages =
  1455. kvmalloc_array(bo->tbo.ttm->num_pages,
  1456. sizeof(struct page *),
  1457. GFP_KERNEL | __GFP_ZERO);
  1458. if (!mem->user_pages) {
  1459. pr_err("%s: Failed to allocate pages array\n",
  1460. __func__);
  1461. return -ENOMEM;
  1462. }
  1463. } else if (mem->user_pages[0]) {
  1464. release_pages(mem->user_pages, bo->tbo.ttm->num_pages);
  1465. }
  1466. /* Get updated user pages */
  1467. ret = amdgpu_ttm_tt_get_user_pages(bo->tbo.ttm,
  1468. mem->user_pages);
  1469. if (ret) {
  1470. mem->user_pages[0] = NULL;
  1471. pr_info("%s: Failed to get user pages: %d\n",
  1472. __func__, ret);
  1473. /* Pretend it succeeded. It will fail later
  1474. * with a VM fault if the GPU tries to access
  1475. * it. Better than hanging indefinitely with
  1476. * stalled user mode queues.
  1477. */
  1478. }
  1479. /* Mark the BO as valid unless it was invalidated
  1480. * again concurrently
  1481. */
  1482. if (atomic_cmpxchg(&mem->invalid, invalid, 0) != invalid)
  1483. return -EAGAIN;
  1484. }
  1485. return 0;
  1486. }
  1487. /* Validate invalid userptr BOs
  1488. *
  1489. * Validates BOs on the userptr_inval_list, and moves them back to the
  1490. * userptr_valid_list. Also updates GPUVM page tables with new page
  1491. * addresses and waits for the page table updates to complete.
  1492. */
  1493. static int validate_invalid_user_pages(struct amdkfd_process_info *process_info)
  1494. {
  1495. struct amdgpu_bo_list_entry *pd_bo_list_entries;
  1496. struct list_head resv_list, duplicates;
  1497. struct ww_acquire_ctx ticket;
  1498. struct amdgpu_sync sync;
  1499. struct amdgpu_vm *peer_vm;
  1500. struct kgd_mem *mem, *tmp_mem;
  1501. struct amdgpu_bo *bo;
  1502. struct ttm_operation_ctx ctx = { false, false };
  1503. int i, ret;
  1504. pd_bo_list_entries = kcalloc(process_info->n_vms,
  1505. sizeof(struct amdgpu_bo_list_entry),
  1506. GFP_KERNEL);
  1507. if (!pd_bo_list_entries) {
  1508. pr_err("%s: Failed to allocate PD BO list entries\n", __func__);
  1509. return -ENOMEM;
  1510. }
  1511. INIT_LIST_HEAD(&resv_list);
  1512. INIT_LIST_HEAD(&duplicates);
  1513. /* Get all the page directory BOs that need to be reserved */
  1514. i = 0;
  1515. list_for_each_entry(peer_vm, &process_info->vm_list_head,
  1516. vm_list_node)
  1517. amdgpu_vm_get_pd_bo(peer_vm, &resv_list,
  1518. &pd_bo_list_entries[i++]);
  1519. /* Add the userptr_inval_list entries to resv_list */
  1520. list_for_each_entry(mem, &process_info->userptr_inval_list,
  1521. validate_list.head) {
  1522. list_add_tail(&mem->resv_list.head, &resv_list);
  1523. mem->resv_list.bo = mem->validate_list.bo;
  1524. mem->resv_list.shared = mem->validate_list.shared;
  1525. }
  1526. /* Reserve all BOs and page tables for validation */
  1527. ret = ttm_eu_reserve_buffers(&ticket, &resv_list, false, &duplicates);
  1528. WARN(!list_empty(&duplicates), "Duplicates should be empty");
  1529. if (ret)
  1530. goto out;
  1531. amdgpu_sync_create(&sync);
  1532. /* Avoid triggering eviction fences when unmapping invalid
  1533. * userptr BOs (waits for all fences, doesn't use
  1534. * FENCE_OWNER_VM)
  1535. */
  1536. list_for_each_entry(peer_vm, &process_info->vm_list_head,
  1537. vm_list_node)
  1538. amdgpu_amdkfd_remove_eviction_fence(peer_vm->root.base.bo,
  1539. process_info->eviction_fence,
  1540. NULL, NULL);
  1541. ret = process_validate_vms(process_info);
  1542. if (ret)
  1543. goto unreserve_out;
  1544. /* Validate BOs and update GPUVM page tables */
  1545. list_for_each_entry_safe(mem, tmp_mem,
  1546. &process_info->userptr_inval_list,
  1547. validate_list.head) {
  1548. struct kfd_bo_va_list *bo_va_entry;
  1549. bo = mem->bo;
  1550. /* Copy pages array and validate the BO if we got user pages */
  1551. if (mem->user_pages[0]) {
  1552. amdgpu_ttm_tt_set_user_pages(bo->tbo.ttm,
  1553. mem->user_pages);
  1554. amdgpu_bo_placement_from_domain(bo, mem->domain);
  1555. ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
  1556. if (ret) {
  1557. pr_err("%s: failed to validate BO\n", __func__);
  1558. goto unreserve_out;
  1559. }
  1560. }
  1561. /* Validate succeeded, now the BO owns the pages, free
  1562. * our copy of the pointer array. Put this BO back on
  1563. * the userptr_valid_list. If we need to revalidate
  1564. * it, we need to start from scratch.
  1565. */
  1566. kvfree(mem->user_pages);
  1567. mem->user_pages = NULL;
  1568. list_move_tail(&mem->validate_list.head,
  1569. &process_info->userptr_valid_list);
  1570. /* Update mapping. If the BO was not validated
  1571. * (because we couldn't get user pages), this will
  1572. * clear the page table entries, which will result in
  1573. * VM faults if the GPU tries to access the invalid
  1574. * memory.
  1575. */
  1576. list_for_each_entry(bo_va_entry, &mem->bo_va_list, bo_list) {
  1577. if (!bo_va_entry->is_mapped)
  1578. continue;
  1579. ret = update_gpuvm_pte((struct amdgpu_device *)
  1580. bo_va_entry->kgd_dev,
  1581. bo_va_entry, &sync);
  1582. if (ret) {
  1583. pr_err("%s: update PTE failed\n", __func__);
  1584. /* make sure this gets validated again */
  1585. atomic_inc(&mem->invalid);
  1586. goto unreserve_out;
  1587. }
  1588. }
  1589. }
  1590. /* Update page directories */
  1591. ret = process_update_pds(process_info, &sync);
  1592. unreserve_out:
  1593. list_for_each_entry(peer_vm, &process_info->vm_list_head,
  1594. vm_list_node)
  1595. amdgpu_bo_fence(peer_vm->root.base.bo,
  1596. &process_info->eviction_fence->base, true);
  1597. ttm_eu_backoff_reservation(&ticket, &resv_list);
  1598. amdgpu_sync_wait(&sync, false);
  1599. amdgpu_sync_free(&sync);
  1600. out:
  1601. kfree(pd_bo_list_entries);
  1602. return ret;
  1603. }
  1604. /* Worker callback to restore evicted userptr BOs
  1605. *
  1606. * Tries to update and validate all userptr BOs. If successful and no
  1607. * concurrent evictions happened, the queues are restarted. Otherwise,
  1608. * reschedule for another attempt later.
  1609. */
  1610. static void amdgpu_amdkfd_restore_userptr_worker(struct work_struct *work)
  1611. {
  1612. struct delayed_work *dwork = to_delayed_work(work);
  1613. struct amdkfd_process_info *process_info =
  1614. container_of(dwork, struct amdkfd_process_info,
  1615. restore_userptr_work);
  1616. struct task_struct *usertask;
  1617. struct mm_struct *mm;
  1618. int evicted_bos;
  1619. evicted_bos = atomic_read(&process_info->evicted_bos);
  1620. if (!evicted_bos)
  1621. return;
  1622. /* Reference task and mm in case of concurrent process termination */
  1623. usertask = get_pid_task(process_info->pid, PIDTYPE_PID);
  1624. if (!usertask)
  1625. return;
  1626. mm = get_task_mm(usertask);
  1627. if (!mm) {
  1628. put_task_struct(usertask);
  1629. return;
  1630. }
  1631. mutex_lock(&process_info->lock);
  1632. if (update_invalid_user_pages(process_info, mm))
  1633. goto unlock_out;
  1634. /* userptr_inval_list can be empty if all evicted userptr BOs
  1635. * have been freed. In that case there is nothing to validate
  1636. * and we can just restart the queues.
  1637. */
  1638. if (!list_empty(&process_info->userptr_inval_list)) {
  1639. if (atomic_read(&process_info->evicted_bos) != evicted_bos)
  1640. goto unlock_out; /* Concurrent eviction, try again */
  1641. if (validate_invalid_user_pages(process_info))
  1642. goto unlock_out;
  1643. }
  1644. /* Final check for concurrent evicton and atomic update. If
  1645. * another eviction happens after successful update, it will
  1646. * be a first eviction that calls quiesce_mm. The eviction
  1647. * reference counting inside KFD will handle this case.
  1648. */
  1649. if (atomic_cmpxchg(&process_info->evicted_bos, evicted_bos, 0) !=
  1650. evicted_bos)
  1651. goto unlock_out;
  1652. evicted_bos = 0;
  1653. if (kgd2kfd->resume_mm(mm)) {
  1654. pr_err("%s: Failed to resume KFD\n", __func__);
  1655. /* No recovery from this failure. Probably the CP is
  1656. * hanging. No point trying again.
  1657. */
  1658. }
  1659. unlock_out:
  1660. mutex_unlock(&process_info->lock);
  1661. mmput(mm);
  1662. put_task_struct(usertask);
  1663. /* If validation failed, reschedule another attempt */
  1664. if (evicted_bos)
  1665. schedule_delayed_work(&process_info->restore_userptr_work,
  1666. msecs_to_jiffies(AMDGPU_USERPTR_RESTORE_DELAY_MS));
  1667. }
  1668. /** amdgpu_amdkfd_gpuvm_restore_process_bos - Restore all BOs for the given
  1669. * KFD process identified by process_info
  1670. *
  1671. * @process_info: amdkfd_process_info of the KFD process
  1672. *
  1673. * After memory eviction, restore thread calls this function. The function
  1674. * should be called when the Process is still valid. BO restore involves -
  1675. *
  1676. * 1. Release old eviction fence and create new one
  1677. * 2. Get two copies of PD BO list from all the VMs. Keep one copy as pd_list.
  1678. * 3 Use the second PD list and kfd_bo_list to create a list (ctx.list) of
  1679. * BOs that need to be reserved.
  1680. * 4. Reserve all the BOs
  1681. * 5. Validate of PD and PT BOs.
  1682. * 6. Validate all KFD BOs using kfd_bo_list and Map them and add new fence
  1683. * 7. Add fence to all PD and PT BOs.
  1684. * 8. Unreserve all BOs
  1685. */
  1686. int amdgpu_amdkfd_gpuvm_restore_process_bos(void *info, struct dma_fence **ef)
  1687. {
  1688. struct amdgpu_bo_list_entry *pd_bo_list;
  1689. struct amdkfd_process_info *process_info = info;
  1690. struct amdgpu_vm *peer_vm;
  1691. struct kgd_mem *mem;
  1692. struct bo_vm_reservation_context ctx;
  1693. struct amdgpu_amdkfd_fence *new_fence;
  1694. int ret = 0, i;
  1695. struct list_head duplicate_save;
  1696. struct amdgpu_sync sync_obj;
  1697. INIT_LIST_HEAD(&duplicate_save);
  1698. INIT_LIST_HEAD(&ctx.list);
  1699. INIT_LIST_HEAD(&ctx.duplicates);
  1700. pd_bo_list = kcalloc(process_info->n_vms,
  1701. sizeof(struct amdgpu_bo_list_entry),
  1702. GFP_KERNEL);
  1703. if (!pd_bo_list)
  1704. return -ENOMEM;
  1705. i = 0;
  1706. mutex_lock(&process_info->lock);
  1707. list_for_each_entry(peer_vm, &process_info->vm_list_head,
  1708. vm_list_node)
  1709. amdgpu_vm_get_pd_bo(peer_vm, &ctx.list, &pd_bo_list[i++]);
  1710. /* Reserve all BOs and page tables/directory. Add all BOs from
  1711. * kfd_bo_list to ctx.list
  1712. */
  1713. list_for_each_entry(mem, &process_info->kfd_bo_list,
  1714. validate_list.head) {
  1715. list_add_tail(&mem->resv_list.head, &ctx.list);
  1716. mem->resv_list.bo = mem->validate_list.bo;
  1717. mem->resv_list.shared = mem->validate_list.shared;
  1718. }
  1719. ret = ttm_eu_reserve_buffers(&ctx.ticket, &ctx.list,
  1720. false, &duplicate_save);
  1721. if (ret) {
  1722. pr_debug("Memory eviction: TTM Reserve Failed. Try again\n");
  1723. goto ttm_reserve_fail;
  1724. }
  1725. amdgpu_sync_create(&sync_obj);
  1726. /* Validate PDs and PTs */
  1727. ret = process_validate_vms(process_info);
  1728. if (ret)
  1729. goto validate_map_fail;
  1730. /* Wait for PD/PTs validate to finish */
  1731. /* FIXME: I think this isn't needed */
  1732. list_for_each_entry(peer_vm, &process_info->vm_list_head,
  1733. vm_list_node) {
  1734. struct amdgpu_bo *bo = peer_vm->root.base.bo;
  1735. ttm_bo_wait(&bo->tbo, false, false);
  1736. }
  1737. /* Validate BOs and map them to GPUVM (update VM page tables). */
  1738. list_for_each_entry(mem, &process_info->kfd_bo_list,
  1739. validate_list.head) {
  1740. struct amdgpu_bo *bo = mem->bo;
  1741. uint32_t domain = mem->domain;
  1742. struct kfd_bo_va_list *bo_va_entry;
  1743. ret = amdgpu_amdkfd_bo_validate(bo, domain, false);
  1744. if (ret) {
  1745. pr_debug("Memory eviction: Validate BOs failed. Try again\n");
  1746. goto validate_map_fail;
  1747. }
  1748. list_for_each_entry(bo_va_entry, &mem->bo_va_list,
  1749. bo_list) {
  1750. ret = update_gpuvm_pte((struct amdgpu_device *)
  1751. bo_va_entry->kgd_dev,
  1752. bo_va_entry,
  1753. &sync_obj);
  1754. if (ret) {
  1755. pr_debug("Memory eviction: update PTE failed. Try again\n");
  1756. goto validate_map_fail;
  1757. }
  1758. }
  1759. }
  1760. /* Update page directories */
  1761. ret = process_update_pds(process_info, &sync_obj);
  1762. if (ret) {
  1763. pr_debug("Memory eviction: update PDs failed. Try again\n");
  1764. goto validate_map_fail;
  1765. }
  1766. amdgpu_sync_wait(&sync_obj, false);
  1767. /* Release old eviction fence and create new one, because fence only
  1768. * goes from unsignaled to signaled, fence cannot be reused.
  1769. * Use context and mm from the old fence.
  1770. */
  1771. new_fence = amdgpu_amdkfd_fence_create(
  1772. process_info->eviction_fence->base.context,
  1773. process_info->eviction_fence->mm);
  1774. if (!new_fence) {
  1775. pr_err("Failed to create eviction fence\n");
  1776. ret = -ENOMEM;
  1777. goto validate_map_fail;
  1778. }
  1779. dma_fence_put(&process_info->eviction_fence->base);
  1780. process_info->eviction_fence = new_fence;
  1781. *ef = dma_fence_get(&new_fence->base);
  1782. /* Wait for validate to finish and attach new eviction fence */
  1783. list_for_each_entry(mem, &process_info->kfd_bo_list,
  1784. validate_list.head)
  1785. ttm_bo_wait(&mem->bo->tbo, false, false);
  1786. list_for_each_entry(mem, &process_info->kfd_bo_list,
  1787. validate_list.head)
  1788. amdgpu_bo_fence(mem->bo,
  1789. &process_info->eviction_fence->base, true);
  1790. /* Attach eviction fence to PD / PT BOs */
  1791. list_for_each_entry(peer_vm, &process_info->vm_list_head,
  1792. vm_list_node) {
  1793. struct amdgpu_bo *bo = peer_vm->root.base.bo;
  1794. amdgpu_bo_fence(bo, &process_info->eviction_fence->base, true);
  1795. }
  1796. validate_map_fail:
  1797. ttm_eu_backoff_reservation(&ctx.ticket, &ctx.list);
  1798. amdgpu_sync_free(&sync_obj);
  1799. ttm_reserve_fail:
  1800. mutex_unlock(&process_info->lock);
  1801. kfree(pd_bo_list);
  1802. return ret;
  1803. }