amdgpu_amdkfd_gpuvm.c 57 KB

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