amdgpu_amdkfd_gpuvm.c 56 KB

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