amdgpu_amdkfd_gpuvm.c 57 KB

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