amdgpu_amdkfd_gpuvm.c 56 KB

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