amdgpu_amdkfd_gpuvm.c 56 KB

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