kfd_process.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /*
  3. * Copyright 2014-2022 Advanced Micro Devices, Inc.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <linux/mutex.h>
  24. #include <linux/log2.h>
  25. #include <linux/sched.h>
  26. #include <linux/sched/mm.h>
  27. #include <linux/sched/task.h>
  28. #include <linux/mmu_context.h>
  29. #include <linux/slab.h>
  30. #include <linux/notifier.h>
  31. #include <linux/compat.h>
  32. #include <linux/mman.h>
  33. #include <linux/file.h>
  34. #include <linux/pm_runtime.h>
  35. #include "amdgpu_amdkfd.h"
  36. #include "amdgpu.h"
  37. #include "amdgpu_reset.h"
  38. struct mm_struct;
  39. #include "kfd_priv.h"
  40. #include "kfd_device_queue_manager.h"
  41. #include "kfd_svm.h"
  42. #include "kfd_smi_events.h"
  43. #include "kfd_debug.h"
  44. /*
  45. * List of struct kfd_process (field kfd_process).
  46. * Unique/indexed by mm_struct*
  47. */
  48. DEFINE_HASHTABLE(kfd_processes_table, KFD_PROCESS_TABLE_SIZE);
  49. DEFINE_MUTEX(kfd_processes_mutex);
  50. DEFINE_SRCU(kfd_processes_srcu);
  51. /* For process termination handling */
  52. static struct workqueue_struct *kfd_process_wq;
  53. /* Ordered, single-threaded workqueue for restoring evicted
  54. * processes. Restoring multiple processes concurrently under memory
  55. * pressure can lead to processes blocking each other from validating
  56. * their BOs and result in a live-lock situation where processes
  57. * remain evicted indefinitely.
  58. */
  59. static struct workqueue_struct *kfd_restore_wq;
  60. static struct kfd_process *find_process(const struct task_struct *thread,
  61. bool ref);
  62. static void kfd_process_ref_release(struct kref *ref);
  63. static struct kfd_process *create_process(const struct task_struct *thread);
  64. static void evict_process_worker(struct work_struct *work);
  65. static void restore_process_worker(struct work_struct *work);
  66. static void kfd_process_device_destroy_cwsr_dgpu(struct kfd_process_device *pdd);
  67. struct kfd_procfs_tree {
  68. struct kobject *kobj;
  69. };
  70. static struct kfd_procfs_tree procfs;
  71. /*
  72. * Structure for SDMA activity tracking
  73. */
  74. struct kfd_sdma_activity_handler_workarea {
  75. struct work_struct sdma_activity_work;
  76. struct kfd_process_device *pdd;
  77. uint64_t sdma_activity_counter;
  78. };
  79. struct temp_sdma_queue_list {
  80. uint64_t __user *rptr;
  81. uint64_t sdma_val;
  82. unsigned int queue_id;
  83. struct list_head list;
  84. };
  85. static void kfd_sdma_activity_worker(struct work_struct *work)
  86. {
  87. struct kfd_sdma_activity_handler_workarea *workarea;
  88. struct kfd_process_device *pdd;
  89. uint64_t val;
  90. struct mm_struct *mm;
  91. struct queue *q;
  92. struct qcm_process_device *qpd;
  93. struct device_queue_manager *dqm;
  94. int ret = 0;
  95. struct temp_sdma_queue_list sdma_q_list;
  96. struct temp_sdma_queue_list *sdma_q, *next;
  97. workarea = container_of(work, struct kfd_sdma_activity_handler_workarea,
  98. sdma_activity_work);
  99. pdd = workarea->pdd;
  100. if (!pdd)
  101. return;
  102. dqm = pdd->dev->dqm;
  103. qpd = &pdd->qpd;
  104. if (!dqm || !qpd)
  105. return;
  106. /*
  107. * Total SDMA activity is current SDMA activity + past SDMA activity
  108. * Past SDMA count is stored in pdd.
  109. * To get the current activity counters for all active SDMA queues,
  110. * we loop over all SDMA queues and get their counts from user-space.
  111. *
  112. * We cannot call get_user() with dqm_lock held as it can cause
  113. * a circular lock dependency situation. To read the SDMA stats,
  114. * we need to do the following:
  115. *
  116. * 1. Create a temporary list of SDMA queue nodes from the qpd->queues_list,
  117. * with dqm_lock/dqm_unlock().
  118. * 2. Call get_user() for each node in temporary list without dqm_lock.
  119. * Save the SDMA count for each node and also add the count to the total
  120. * SDMA count counter.
  121. * Its possible, during this step, a few SDMA queue nodes got deleted
  122. * from the qpd->queues_list.
  123. * 3. Do a second pass over qpd->queues_list to check if any nodes got deleted.
  124. * If any node got deleted, its SDMA count would be captured in the sdma
  125. * past activity counter. So subtract the SDMA counter stored in step 2
  126. * for this node from the total SDMA count.
  127. */
  128. INIT_LIST_HEAD(&sdma_q_list.list);
  129. /*
  130. * Create the temp list of all SDMA queues
  131. */
  132. dqm_lock(dqm);
  133. list_for_each_entry(q, &qpd->queues_list, list) {
  134. if ((q->properties.type != KFD_QUEUE_TYPE_SDMA) &&
  135. (q->properties.type != KFD_QUEUE_TYPE_SDMA_XGMI))
  136. continue;
  137. sdma_q = kzalloc(sizeof(struct temp_sdma_queue_list), GFP_KERNEL);
  138. if (!sdma_q) {
  139. dqm_unlock(dqm);
  140. goto cleanup;
  141. }
  142. INIT_LIST_HEAD(&sdma_q->list);
  143. sdma_q->rptr = (uint64_t __user *)q->properties.read_ptr;
  144. sdma_q->queue_id = q->properties.queue_id;
  145. list_add_tail(&sdma_q->list, &sdma_q_list.list);
  146. }
  147. /*
  148. * If the temp list is empty, then no SDMA queues nodes were found in
  149. * qpd->queues_list. Return the past activity count as the total sdma
  150. * count
  151. */
  152. if (list_empty(&sdma_q_list.list)) {
  153. workarea->sdma_activity_counter = pdd->sdma_past_activity_counter;
  154. dqm_unlock(dqm);
  155. return;
  156. }
  157. dqm_unlock(dqm);
  158. /*
  159. * Get the usage count for each SDMA queue in temp_list.
  160. */
  161. mm = get_task_mm(pdd->process->lead_thread);
  162. if (!mm)
  163. goto cleanup;
  164. kthread_use_mm(mm);
  165. list_for_each_entry(sdma_q, &sdma_q_list.list, list) {
  166. val = 0;
  167. ret = read_sdma_queue_counter(sdma_q->rptr, &val);
  168. if (ret) {
  169. pr_debug("Failed to read SDMA queue active counter for queue id: %d",
  170. sdma_q->queue_id);
  171. } else {
  172. sdma_q->sdma_val = val;
  173. workarea->sdma_activity_counter += val;
  174. }
  175. }
  176. kthread_unuse_mm(mm);
  177. mmput(mm);
  178. /*
  179. * Do a second iteration over qpd_queues_list to check if any SDMA
  180. * nodes got deleted while fetching SDMA counter.
  181. */
  182. dqm_lock(dqm);
  183. workarea->sdma_activity_counter += pdd->sdma_past_activity_counter;
  184. list_for_each_entry(q, &qpd->queues_list, list) {
  185. if (list_empty(&sdma_q_list.list))
  186. break;
  187. if ((q->properties.type != KFD_QUEUE_TYPE_SDMA) &&
  188. (q->properties.type != KFD_QUEUE_TYPE_SDMA_XGMI))
  189. continue;
  190. list_for_each_entry_safe(sdma_q, next, &sdma_q_list.list, list) {
  191. if (((uint64_t __user *)q->properties.read_ptr == sdma_q->rptr) &&
  192. (sdma_q->queue_id == q->properties.queue_id)) {
  193. list_del(&sdma_q->list);
  194. kfree(sdma_q);
  195. break;
  196. }
  197. }
  198. }
  199. dqm_unlock(dqm);
  200. /*
  201. * If temp list is not empty, it implies some queues got deleted
  202. * from qpd->queues_list during SDMA usage read. Subtract the SDMA
  203. * count for each node from the total SDMA count.
  204. */
  205. list_for_each_entry_safe(sdma_q, next, &sdma_q_list.list, list) {
  206. workarea->sdma_activity_counter -= sdma_q->sdma_val;
  207. list_del(&sdma_q->list);
  208. kfree(sdma_q);
  209. }
  210. return;
  211. cleanup:
  212. list_for_each_entry_safe(sdma_q, next, &sdma_q_list.list, list) {
  213. list_del(&sdma_q->list);
  214. kfree(sdma_q);
  215. }
  216. }
  217. /**
  218. * kfd_get_cu_occupancy - Collect number of waves in-flight on this device
  219. * by current process. Translates acquired wave count into number of compute units
  220. * that are occupied.
  221. *
  222. * @attr: Handle of attribute that allows reporting of wave count. The attribute
  223. * handle encapsulates GPU device it is associated with, thereby allowing collection
  224. * of waves in flight, etc
  225. * @buffer: Handle of user provided buffer updated with wave count
  226. *
  227. * Return: Number of bytes written to user buffer or an error value
  228. */
  229. static int kfd_get_cu_occupancy(struct attribute *attr, char *buffer)
  230. {
  231. int cu_cnt;
  232. int wave_cnt;
  233. int max_waves_per_cu;
  234. struct kfd_node *dev = NULL;
  235. struct kfd_process *proc = NULL;
  236. struct kfd_process_device *pdd = NULL;
  237. int i;
  238. struct kfd_cu_occupancy *cu_occupancy;
  239. u32 queue_format;
  240. pdd = container_of(attr, struct kfd_process_device, attr_cu_occupancy);
  241. dev = pdd->dev;
  242. if (dev->kfd2kgd->get_cu_occupancy == NULL)
  243. return -EINVAL;
  244. cu_cnt = 0;
  245. proc = pdd->process;
  246. if (pdd->qpd.queue_count == 0) {
  247. pr_debug("Gpu-Id: %d has no active queues for process %d\n",
  248. dev->id, proc->pasid);
  249. return snprintf(buffer, PAGE_SIZE, "%d\n", cu_cnt);
  250. }
  251. /* Collect wave count from device if it supports */
  252. wave_cnt = 0;
  253. max_waves_per_cu = 0;
  254. cu_occupancy = kcalloc(AMDGPU_MAX_QUEUES, sizeof(*cu_occupancy), GFP_KERNEL);
  255. if (!cu_occupancy)
  256. return -ENOMEM;
  257. /*
  258. * For GFX 9.4.3, fetch the CU occupancy from the first XCC in the partition.
  259. * For AQL queues, because of cooperative dispatch we multiply the wave count
  260. * by number of XCCs in the partition to get the total wave counts across all
  261. * XCCs in the partition.
  262. * For PM4 queues, there is no cooperative dispatch so wave_cnt stay as it is.
  263. */
  264. dev->kfd2kgd->get_cu_occupancy(dev->adev, cu_occupancy,
  265. &max_waves_per_cu, ffs(dev->xcc_mask) - 1);
  266. for (i = 0; i < AMDGPU_MAX_QUEUES; i++) {
  267. if (cu_occupancy[i].wave_cnt != 0 &&
  268. kfd_dqm_is_queue_in_process(dev->dqm, &pdd->qpd,
  269. cu_occupancy[i].doorbell_off,
  270. &queue_format)) {
  271. if (unlikely(queue_format == KFD_QUEUE_FORMAT_PM4))
  272. wave_cnt += cu_occupancy[i].wave_cnt;
  273. else
  274. wave_cnt += (NUM_XCC(dev->xcc_mask) *
  275. cu_occupancy[i].wave_cnt);
  276. }
  277. }
  278. /* Translate wave count to number of compute units */
  279. cu_cnt = (wave_cnt + (max_waves_per_cu - 1)) / max_waves_per_cu;
  280. kfree(cu_occupancy);
  281. return snprintf(buffer, PAGE_SIZE, "%d\n", cu_cnt);
  282. }
  283. static ssize_t kfd_procfs_show(struct kobject *kobj, struct attribute *attr,
  284. char *buffer)
  285. {
  286. if (strcmp(attr->name, "pasid") == 0) {
  287. struct kfd_process *p = container_of(attr, struct kfd_process,
  288. attr_pasid);
  289. return snprintf(buffer, PAGE_SIZE, "%d\n", p->pasid);
  290. } else if (strncmp(attr->name, "vram_", 5) == 0) {
  291. struct kfd_process_device *pdd = container_of(attr, struct kfd_process_device,
  292. attr_vram);
  293. return snprintf(buffer, PAGE_SIZE, "%llu\n", atomic64_read(&pdd->vram_usage));
  294. } else if (strncmp(attr->name, "sdma_", 5) == 0) {
  295. struct kfd_process_device *pdd = container_of(attr, struct kfd_process_device,
  296. attr_sdma);
  297. struct kfd_sdma_activity_handler_workarea sdma_activity_work_handler;
  298. INIT_WORK_ONSTACK(&sdma_activity_work_handler.sdma_activity_work,
  299. kfd_sdma_activity_worker);
  300. sdma_activity_work_handler.pdd = pdd;
  301. sdma_activity_work_handler.sdma_activity_counter = 0;
  302. schedule_work(&sdma_activity_work_handler.sdma_activity_work);
  303. flush_work(&sdma_activity_work_handler.sdma_activity_work);
  304. destroy_work_on_stack(&sdma_activity_work_handler.sdma_activity_work);
  305. return snprintf(buffer, PAGE_SIZE, "%llu\n",
  306. (sdma_activity_work_handler.sdma_activity_counter)/
  307. SDMA_ACTIVITY_DIVISOR);
  308. } else {
  309. pr_err("Invalid attribute");
  310. return -EINVAL;
  311. }
  312. return 0;
  313. }
  314. static void kfd_procfs_kobj_release(struct kobject *kobj)
  315. {
  316. kfree(kobj);
  317. }
  318. static const struct sysfs_ops kfd_procfs_ops = {
  319. .show = kfd_procfs_show,
  320. };
  321. static const struct kobj_type procfs_type = {
  322. .release = kfd_procfs_kobj_release,
  323. .sysfs_ops = &kfd_procfs_ops,
  324. };
  325. void kfd_procfs_init(void)
  326. {
  327. int ret = 0;
  328. procfs.kobj = kfd_alloc_struct(procfs.kobj);
  329. if (!procfs.kobj)
  330. return;
  331. ret = kobject_init_and_add(procfs.kobj, &procfs_type,
  332. &kfd_device->kobj, "proc");
  333. if (ret) {
  334. pr_warn("Could not create procfs proc folder");
  335. /* If we fail to create the procfs, clean up */
  336. kfd_procfs_shutdown();
  337. }
  338. }
  339. void kfd_procfs_shutdown(void)
  340. {
  341. if (procfs.kobj) {
  342. kobject_del(procfs.kobj);
  343. kobject_put(procfs.kobj);
  344. procfs.kobj = NULL;
  345. }
  346. }
  347. static ssize_t kfd_procfs_queue_show(struct kobject *kobj,
  348. struct attribute *attr, char *buffer)
  349. {
  350. struct queue *q = container_of(kobj, struct queue, kobj);
  351. if (!strcmp(attr->name, "size"))
  352. return snprintf(buffer, PAGE_SIZE, "%llu",
  353. q->properties.queue_size);
  354. else if (!strcmp(attr->name, "type"))
  355. return snprintf(buffer, PAGE_SIZE, "%d", q->properties.type);
  356. else if (!strcmp(attr->name, "gpuid"))
  357. return snprintf(buffer, PAGE_SIZE, "%u", q->device->id);
  358. else
  359. pr_err("Invalid attribute");
  360. return 0;
  361. }
  362. static ssize_t kfd_procfs_stats_show(struct kobject *kobj,
  363. struct attribute *attr, char *buffer)
  364. {
  365. if (strcmp(attr->name, "evicted_ms") == 0) {
  366. struct kfd_process_device *pdd = container_of(attr,
  367. struct kfd_process_device,
  368. attr_evict);
  369. uint64_t evict_jiffies;
  370. evict_jiffies = atomic64_read(&pdd->evict_duration_counter);
  371. return snprintf(buffer,
  372. PAGE_SIZE,
  373. "%llu\n",
  374. jiffies64_to_msecs(evict_jiffies));
  375. /* Sysfs handle that gets CU occupancy is per device */
  376. } else if (strcmp(attr->name, "cu_occupancy") == 0) {
  377. return kfd_get_cu_occupancy(attr, buffer);
  378. } else {
  379. pr_err("Invalid attribute");
  380. }
  381. return 0;
  382. }
  383. static ssize_t kfd_sysfs_counters_show(struct kobject *kobj,
  384. struct attribute *attr, char *buf)
  385. {
  386. struct kfd_process_device *pdd;
  387. if (!strcmp(attr->name, "faults")) {
  388. pdd = container_of(attr, struct kfd_process_device,
  389. attr_faults);
  390. return sysfs_emit(buf, "%llu\n", READ_ONCE(pdd->faults));
  391. }
  392. if (!strcmp(attr->name, "page_in")) {
  393. pdd = container_of(attr, struct kfd_process_device,
  394. attr_page_in);
  395. return sysfs_emit(buf, "%llu\n", READ_ONCE(pdd->page_in));
  396. }
  397. if (!strcmp(attr->name, "page_out")) {
  398. pdd = container_of(attr, struct kfd_process_device,
  399. attr_page_out);
  400. return sysfs_emit(buf, "%llu\n", READ_ONCE(pdd->page_out));
  401. }
  402. return 0;
  403. }
  404. static struct attribute attr_queue_size = {
  405. .name = "size",
  406. .mode = KFD_SYSFS_FILE_MODE
  407. };
  408. static struct attribute attr_queue_type = {
  409. .name = "type",
  410. .mode = KFD_SYSFS_FILE_MODE
  411. };
  412. static struct attribute attr_queue_gpuid = {
  413. .name = "gpuid",
  414. .mode = KFD_SYSFS_FILE_MODE
  415. };
  416. static struct attribute *procfs_queue_attrs[] = {
  417. &attr_queue_size,
  418. &attr_queue_type,
  419. &attr_queue_gpuid,
  420. NULL
  421. };
  422. ATTRIBUTE_GROUPS(procfs_queue);
  423. static const struct sysfs_ops procfs_queue_ops = {
  424. .show = kfd_procfs_queue_show,
  425. };
  426. static const struct kobj_type procfs_queue_type = {
  427. .sysfs_ops = &procfs_queue_ops,
  428. .default_groups = procfs_queue_groups,
  429. };
  430. static const struct sysfs_ops procfs_stats_ops = {
  431. .show = kfd_procfs_stats_show,
  432. };
  433. static const struct kobj_type procfs_stats_type = {
  434. .sysfs_ops = &procfs_stats_ops,
  435. .release = kfd_procfs_kobj_release,
  436. };
  437. static const struct sysfs_ops sysfs_counters_ops = {
  438. .show = kfd_sysfs_counters_show,
  439. };
  440. static const struct kobj_type sysfs_counters_type = {
  441. .sysfs_ops = &sysfs_counters_ops,
  442. .release = kfd_procfs_kobj_release,
  443. };
  444. int kfd_procfs_add_queue(struct queue *q)
  445. {
  446. struct kfd_process *proc;
  447. int ret;
  448. if (!q || !q->process)
  449. return -EINVAL;
  450. proc = q->process;
  451. /* Create proc/<pid>/queues/<queue id> folder */
  452. if (!proc->kobj_queues)
  453. return -EFAULT;
  454. ret = kobject_init_and_add(&q->kobj, &procfs_queue_type,
  455. proc->kobj_queues, "%u", q->properties.queue_id);
  456. if (ret < 0) {
  457. pr_warn("Creating proc/<pid>/queues/%u failed",
  458. q->properties.queue_id);
  459. kobject_put(&q->kobj);
  460. return ret;
  461. }
  462. return 0;
  463. }
  464. static void kfd_sysfs_create_file(struct kobject *kobj, struct attribute *attr,
  465. char *name)
  466. {
  467. int ret;
  468. if (!kobj || !attr || !name)
  469. return;
  470. attr->name = name;
  471. attr->mode = KFD_SYSFS_FILE_MODE;
  472. sysfs_attr_init(attr);
  473. ret = sysfs_create_file(kobj, attr);
  474. if (ret)
  475. pr_warn("Create sysfs %s/%s failed %d", kobj->name, name, ret);
  476. }
  477. static void kfd_procfs_add_sysfs_stats(struct kfd_process *p)
  478. {
  479. int ret;
  480. int i;
  481. char stats_dir_filename[MAX_SYSFS_FILENAME_LEN];
  482. if (!p || !p->kobj)
  483. return;
  484. /*
  485. * Create sysfs files for each GPU:
  486. * - proc/<pid>/stats_<gpuid>/
  487. * - proc/<pid>/stats_<gpuid>/evicted_ms
  488. * - proc/<pid>/stats_<gpuid>/cu_occupancy
  489. */
  490. for (i = 0; i < p->n_pdds; i++) {
  491. struct kfd_process_device *pdd = p->pdds[i];
  492. snprintf(stats_dir_filename, MAX_SYSFS_FILENAME_LEN,
  493. "stats_%u", pdd->dev->id);
  494. pdd->kobj_stats = kfd_alloc_struct(pdd->kobj_stats);
  495. if (!pdd->kobj_stats)
  496. return;
  497. ret = kobject_init_and_add(pdd->kobj_stats,
  498. &procfs_stats_type,
  499. p->kobj,
  500. stats_dir_filename);
  501. if (ret) {
  502. pr_warn("Creating KFD proc/stats_%s folder failed",
  503. stats_dir_filename);
  504. kobject_put(pdd->kobj_stats);
  505. pdd->kobj_stats = NULL;
  506. return;
  507. }
  508. kfd_sysfs_create_file(pdd->kobj_stats, &pdd->attr_evict,
  509. "evicted_ms");
  510. /* Add sysfs file to report compute unit occupancy */
  511. if (pdd->dev->kfd2kgd->get_cu_occupancy)
  512. kfd_sysfs_create_file(pdd->kobj_stats,
  513. &pdd->attr_cu_occupancy,
  514. "cu_occupancy");
  515. }
  516. }
  517. static void kfd_procfs_add_sysfs_counters(struct kfd_process *p)
  518. {
  519. int ret = 0;
  520. int i;
  521. char counters_dir_filename[MAX_SYSFS_FILENAME_LEN];
  522. if (!p || !p->kobj)
  523. return;
  524. /*
  525. * Create sysfs files for each GPU which supports SVM
  526. * - proc/<pid>/counters_<gpuid>/
  527. * - proc/<pid>/counters_<gpuid>/faults
  528. * - proc/<pid>/counters_<gpuid>/page_in
  529. * - proc/<pid>/counters_<gpuid>/page_out
  530. */
  531. for_each_set_bit(i, p->svms.bitmap_supported, p->n_pdds) {
  532. struct kfd_process_device *pdd = p->pdds[i];
  533. struct kobject *kobj_counters;
  534. snprintf(counters_dir_filename, MAX_SYSFS_FILENAME_LEN,
  535. "counters_%u", pdd->dev->id);
  536. kobj_counters = kfd_alloc_struct(kobj_counters);
  537. if (!kobj_counters)
  538. return;
  539. ret = kobject_init_and_add(kobj_counters, &sysfs_counters_type,
  540. p->kobj, counters_dir_filename);
  541. if (ret) {
  542. pr_warn("Creating KFD proc/%s folder failed",
  543. counters_dir_filename);
  544. kobject_put(kobj_counters);
  545. return;
  546. }
  547. pdd->kobj_counters = kobj_counters;
  548. kfd_sysfs_create_file(kobj_counters, &pdd->attr_faults,
  549. "faults");
  550. kfd_sysfs_create_file(kobj_counters, &pdd->attr_page_in,
  551. "page_in");
  552. kfd_sysfs_create_file(kobj_counters, &pdd->attr_page_out,
  553. "page_out");
  554. }
  555. }
  556. static void kfd_procfs_add_sysfs_files(struct kfd_process *p)
  557. {
  558. int i;
  559. if (!p || !p->kobj)
  560. return;
  561. /*
  562. * Create sysfs files for each GPU:
  563. * - proc/<pid>/vram_<gpuid>
  564. * - proc/<pid>/sdma_<gpuid>
  565. */
  566. for (i = 0; i < p->n_pdds; i++) {
  567. struct kfd_process_device *pdd = p->pdds[i];
  568. snprintf(pdd->vram_filename, MAX_SYSFS_FILENAME_LEN, "vram_%u",
  569. pdd->dev->id);
  570. kfd_sysfs_create_file(p->kobj, &pdd->attr_vram,
  571. pdd->vram_filename);
  572. snprintf(pdd->sdma_filename, MAX_SYSFS_FILENAME_LEN, "sdma_%u",
  573. pdd->dev->id);
  574. kfd_sysfs_create_file(p->kobj, &pdd->attr_sdma,
  575. pdd->sdma_filename);
  576. }
  577. }
  578. void kfd_procfs_del_queue(struct queue *q)
  579. {
  580. if (!q)
  581. return;
  582. kobject_del(&q->kobj);
  583. kobject_put(&q->kobj);
  584. }
  585. int kfd_process_create_wq(void)
  586. {
  587. if (!kfd_process_wq)
  588. kfd_process_wq = alloc_workqueue("kfd_process_wq", 0, 0);
  589. if (!kfd_restore_wq)
  590. kfd_restore_wq = alloc_ordered_workqueue("kfd_restore_wq",
  591. WQ_FREEZABLE);
  592. if (!kfd_process_wq || !kfd_restore_wq) {
  593. kfd_process_destroy_wq();
  594. return -ENOMEM;
  595. }
  596. return 0;
  597. }
  598. void kfd_process_destroy_wq(void)
  599. {
  600. if (kfd_process_wq) {
  601. destroy_workqueue(kfd_process_wq);
  602. kfd_process_wq = NULL;
  603. }
  604. if (kfd_restore_wq) {
  605. destroy_workqueue(kfd_restore_wq);
  606. kfd_restore_wq = NULL;
  607. }
  608. }
  609. static void kfd_process_free_gpuvm(struct kgd_mem *mem,
  610. struct kfd_process_device *pdd, void **kptr)
  611. {
  612. struct kfd_node *dev = pdd->dev;
  613. if (kptr && *kptr) {
  614. amdgpu_amdkfd_gpuvm_unmap_gtt_bo_from_kernel(mem);
  615. *kptr = NULL;
  616. }
  617. amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(dev->adev, mem, pdd->drm_priv);
  618. amdgpu_amdkfd_gpuvm_free_memory_of_gpu(dev->adev, mem, pdd->drm_priv,
  619. NULL);
  620. }
  621. /* kfd_process_alloc_gpuvm - Allocate GPU VM for the KFD process
  622. * This function should be only called right after the process
  623. * is created and when kfd_processes_mutex is still being held
  624. * to avoid concurrency. Because of that exclusiveness, we do
  625. * not need to take p->mutex.
  626. */
  627. static int kfd_process_alloc_gpuvm(struct kfd_process_device *pdd,
  628. uint64_t gpu_va, uint32_t size,
  629. uint32_t flags, struct kgd_mem **mem, void **kptr)
  630. {
  631. struct kfd_node *kdev = pdd->dev;
  632. int err;
  633. err = amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(kdev->adev, gpu_va, size,
  634. pdd->drm_priv, mem, NULL,
  635. flags, false);
  636. if (err)
  637. goto err_alloc_mem;
  638. err = amdgpu_amdkfd_gpuvm_map_memory_to_gpu(kdev->adev, *mem,
  639. pdd->drm_priv);
  640. if (err)
  641. goto err_map_mem;
  642. err = amdgpu_amdkfd_gpuvm_sync_memory(kdev->adev, *mem, true);
  643. if (err) {
  644. pr_debug("Sync memory failed, wait interrupted by user signal\n");
  645. goto sync_memory_failed;
  646. }
  647. if (kptr) {
  648. err = amdgpu_amdkfd_gpuvm_map_gtt_bo_to_kernel(
  649. (struct kgd_mem *)*mem, kptr, NULL);
  650. if (err) {
  651. pr_debug("Map GTT BO to kernel failed\n");
  652. goto sync_memory_failed;
  653. }
  654. }
  655. return err;
  656. sync_memory_failed:
  657. amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(kdev->adev, *mem, pdd->drm_priv);
  658. err_map_mem:
  659. amdgpu_amdkfd_gpuvm_free_memory_of_gpu(kdev->adev, *mem, pdd->drm_priv,
  660. NULL);
  661. err_alloc_mem:
  662. *mem = NULL;
  663. *kptr = NULL;
  664. return err;
  665. }
  666. /* kfd_process_device_reserve_ib_mem - Reserve memory inside the
  667. * process for IB usage The memory reserved is for KFD to submit
  668. * IB to AMDGPU from kernel. If the memory is reserved
  669. * successfully, ib_kaddr will have the CPU/kernel
  670. * address. Check ib_kaddr before accessing the memory.
  671. */
  672. static int kfd_process_device_reserve_ib_mem(struct kfd_process_device *pdd)
  673. {
  674. struct qcm_process_device *qpd = &pdd->qpd;
  675. uint32_t flags = KFD_IOC_ALLOC_MEM_FLAGS_GTT |
  676. KFD_IOC_ALLOC_MEM_FLAGS_NO_SUBSTITUTE |
  677. KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE |
  678. KFD_IOC_ALLOC_MEM_FLAGS_EXECUTABLE;
  679. struct kgd_mem *mem;
  680. void *kaddr;
  681. int ret;
  682. if (qpd->ib_kaddr || !qpd->ib_base)
  683. return 0;
  684. /* ib_base is only set for dGPU */
  685. ret = kfd_process_alloc_gpuvm(pdd, qpd->ib_base, PAGE_SIZE, flags,
  686. &mem, &kaddr);
  687. if (ret)
  688. return ret;
  689. qpd->ib_mem = mem;
  690. qpd->ib_kaddr = kaddr;
  691. return 0;
  692. }
  693. static void kfd_process_device_destroy_ib_mem(struct kfd_process_device *pdd)
  694. {
  695. struct qcm_process_device *qpd = &pdd->qpd;
  696. if (!qpd->ib_kaddr || !qpd->ib_base)
  697. return;
  698. kfd_process_free_gpuvm(qpd->ib_mem, pdd, &qpd->ib_kaddr);
  699. }
  700. struct kfd_process *kfd_create_process(struct task_struct *thread)
  701. {
  702. struct kfd_process *process;
  703. int ret;
  704. if (!(thread->mm && mmget_not_zero(thread->mm)))
  705. return ERR_PTR(-EINVAL);
  706. /* Only the pthreads threading model is supported. */
  707. if (thread->group_leader->mm != thread->mm) {
  708. mmput(thread->mm);
  709. return ERR_PTR(-EINVAL);
  710. }
  711. /* If the process just called exec(3), it is possible that the
  712. * cleanup of the kfd_process (following the release of the mm
  713. * of the old process image) is still in the cleanup work queue.
  714. * Make sure to drain any job before trying to recreate any
  715. * resource for this process.
  716. */
  717. flush_workqueue(kfd_process_wq);
  718. /*
  719. * take kfd processes mutex before starting of process creation
  720. * so there won't be a case where two threads of the same process
  721. * create two kfd_process structures
  722. */
  723. mutex_lock(&kfd_processes_mutex);
  724. if (kfd_is_locked()) {
  725. pr_debug("KFD is locked! Cannot create process");
  726. process = ERR_PTR(-EINVAL);
  727. goto out;
  728. }
  729. /* A prior open of /dev/kfd could have already created the process. */
  730. process = find_process(thread, false);
  731. if (process) {
  732. pr_debug("Process already found\n");
  733. } else {
  734. process = create_process(thread);
  735. if (IS_ERR(process))
  736. goto out;
  737. if (!procfs.kobj)
  738. goto out;
  739. process->kobj = kfd_alloc_struct(process->kobj);
  740. if (!process->kobj) {
  741. pr_warn("Creating procfs kobject failed");
  742. goto out;
  743. }
  744. ret = kobject_init_and_add(process->kobj, &procfs_type,
  745. procfs.kobj, "%d",
  746. (int)process->lead_thread->pid);
  747. if (ret) {
  748. pr_warn("Creating procfs pid directory failed");
  749. kobject_put(process->kobj);
  750. goto out;
  751. }
  752. kfd_sysfs_create_file(process->kobj, &process->attr_pasid,
  753. "pasid");
  754. process->kobj_queues = kobject_create_and_add("queues",
  755. process->kobj);
  756. if (!process->kobj_queues)
  757. pr_warn("Creating KFD proc/queues folder failed");
  758. kfd_procfs_add_sysfs_stats(process);
  759. kfd_procfs_add_sysfs_files(process);
  760. kfd_procfs_add_sysfs_counters(process);
  761. init_waitqueue_head(&process->wait_irq_drain);
  762. }
  763. out:
  764. if (!IS_ERR(process))
  765. kref_get(&process->ref);
  766. mutex_unlock(&kfd_processes_mutex);
  767. mmput(thread->mm);
  768. return process;
  769. }
  770. struct kfd_process *kfd_get_process(const struct task_struct *thread)
  771. {
  772. struct kfd_process *process;
  773. if (!thread->mm)
  774. return ERR_PTR(-EINVAL);
  775. /* Only the pthreads threading model is supported. */
  776. if (thread->group_leader->mm != thread->mm)
  777. return ERR_PTR(-EINVAL);
  778. process = find_process(thread, false);
  779. if (!process)
  780. return ERR_PTR(-EINVAL);
  781. return process;
  782. }
  783. static struct kfd_process *find_process_by_mm(const struct mm_struct *mm)
  784. {
  785. struct kfd_process *process;
  786. hash_for_each_possible_rcu(kfd_processes_table, process,
  787. kfd_processes, (uintptr_t)mm)
  788. if (process->mm == mm)
  789. return process;
  790. return NULL;
  791. }
  792. static struct kfd_process *find_process(const struct task_struct *thread,
  793. bool ref)
  794. {
  795. struct kfd_process *p;
  796. int idx;
  797. idx = srcu_read_lock(&kfd_processes_srcu);
  798. p = find_process_by_mm(thread->mm);
  799. if (p && ref)
  800. kref_get(&p->ref);
  801. srcu_read_unlock(&kfd_processes_srcu, idx);
  802. return p;
  803. }
  804. void kfd_unref_process(struct kfd_process *p)
  805. {
  806. kref_put(&p->ref, kfd_process_ref_release);
  807. }
  808. /* This increments the process->ref counter. */
  809. struct kfd_process *kfd_lookup_process_by_pid(struct pid *pid)
  810. {
  811. struct task_struct *task = NULL;
  812. struct kfd_process *p = NULL;
  813. if (!pid) {
  814. task = current;
  815. get_task_struct(task);
  816. } else {
  817. task = get_pid_task(pid, PIDTYPE_PID);
  818. }
  819. if (task) {
  820. p = find_process(task, true);
  821. put_task_struct(task);
  822. }
  823. return p;
  824. }
  825. static void kfd_process_device_free_bos(struct kfd_process_device *pdd)
  826. {
  827. struct kfd_process *p = pdd->process;
  828. void *mem;
  829. int id;
  830. int i;
  831. /*
  832. * Remove all handles from idr and release appropriate
  833. * local memory object
  834. */
  835. idr_for_each_entry(&pdd->alloc_idr, mem, id) {
  836. for (i = 0; i < p->n_pdds; i++) {
  837. struct kfd_process_device *peer_pdd = p->pdds[i];
  838. if (!peer_pdd->drm_priv)
  839. continue;
  840. amdgpu_amdkfd_gpuvm_unmap_memory_from_gpu(
  841. peer_pdd->dev->adev, mem, peer_pdd->drm_priv);
  842. }
  843. amdgpu_amdkfd_gpuvm_free_memory_of_gpu(pdd->dev->adev, mem,
  844. pdd->drm_priv, NULL);
  845. kfd_process_device_remove_obj_handle(pdd, id);
  846. }
  847. }
  848. /*
  849. * Just kunmap and unpin signal BO here. It will be freed in
  850. * kfd_process_free_outstanding_kfd_bos()
  851. */
  852. static void kfd_process_kunmap_signal_bo(struct kfd_process *p)
  853. {
  854. struct kfd_process_device *pdd;
  855. struct kfd_node *kdev;
  856. void *mem;
  857. kdev = kfd_device_by_id(GET_GPU_ID(p->signal_handle));
  858. if (!kdev)
  859. return;
  860. mutex_lock(&p->mutex);
  861. pdd = kfd_get_process_device_data(kdev, p);
  862. if (!pdd)
  863. goto out;
  864. mem = kfd_process_device_translate_handle(
  865. pdd, GET_IDR_HANDLE(p->signal_handle));
  866. if (!mem)
  867. goto out;
  868. amdgpu_amdkfd_gpuvm_unmap_gtt_bo_from_kernel(mem);
  869. out:
  870. mutex_unlock(&p->mutex);
  871. }
  872. static void kfd_process_free_outstanding_kfd_bos(struct kfd_process *p)
  873. {
  874. int i;
  875. for (i = 0; i < p->n_pdds; i++)
  876. kfd_process_device_free_bos(p->pdds[i]);
  877. }
  878. static void kfd_process_destroy_pdds(struct kfd_process *p)
  879. {
  880. int i;
  881. for (i = 0; i < p->n_pdds; i++) {
  882. struct kfd_process_device *pdd = p->pdds[i];
  883. pr_debug("Releasing pdd (topology id %d) for process (pasid 0x%x)\n",
  884. pdd->dev->id, p->pasid);
  885. kfd_process_device_destroy_cwsr_dgpu(pdd);
  886. kfd_process_device_destroy_ib_mem(pdd);
  887. if (pdd->drm_file) {
  888. amdgpu_amdkfd_gpuvm_release_process_vm(
  889. pdd->dev->adev, pdd->drm_priv);
  890. fput(pdd->drm_file);
  891. }
  892. if (pdd->qpd.cwsr_kaddr && !pdd->qpd.cwsr_base)
  893. free_pages((unsigned long)pdd->qpd.cwsr_kaddr,
  894. get_order(KFD_CWSR_TBA_TMA_SIZE));
  895. idr_destroy(&pdd->alloc_idr);
  896. kfd_free_process_doorbells(pdd->dev->kfd, pdd);
  897. if (pdd->dev->kfd->shared_resources.enable_mes &&
  898. pdd->proc_ctx_cpu_ptr)
  899. amdgpu_amdkfd_free_gtt_mem(pdd->dev->adev,
  900. &pdd->proc_ctx_bo);
  901. /*
  902. * before destroying pdd, make sure to report availability
  903. * for auto suspend
  904. */
  905. if (pdd->runtime_inuse) {
  906. pm_runtime_mark_last_busy(adev_to_drm(pdd->dev->adev)->dev);
  907. pm_runtime_put_autosuspend(adev_to_drm(pdd->dev->adev)->dev);
  908. pdd->runtime_inuse = false;
  909. }
  910. kfree(pdd);
  911. p->pdds[i] = NULL;
  912. }
  913. p->n_pdds = 0;
  914. }
  915. static void kfd_process_remove_sysfs(struct kfd_process *p)
  916. {
  917. struct kfd_process_device *pdd;
  918. int i;
  919. if (!p->kobj)
  920. return;
  921. sysfs_remove_file(p->kobj, &p->attr_pasid);
  922. kobject_del(p->kobj_queues);
  923. kobject_put(p->kobj_queues);
  924. p->kobj_queues = NULL;
  925. for (i = 0; i < p->n_pdds; i++) {
  926. pdd = p->pdds[i];
  927. sysfs_remove_file(p->kobj, &pdd->attr_vram);
  928. sysfs_remove_file(p->kobj, &pdd->attr_sdma);
  929. sysfs_remove_file(pdd->kobj_stats, &pdd->attr_evict);
  930. if (pdd->dev->kfd2kgd->get_cu_occupancy)
  931. sysfs_remove_file(pdd->kobj_stats,
  932. &pdd->attr_cu_occupancy);
  933. kobject_del(pdd->kobj_stats);
  934. kobject_put(pdd->kobj_stats);
  935. pdd->kobj_stats = NULL;
  936. }
  937. for_each_set_bit(i, p->svms.bitmap_supported, p->n_pdds) {
  938. pdd = p->pdds[i];
  939. sysfs_remove_file(pdd->kobj_counters, &pdd->attr_faults);
  940. sysfs_remove_file(pdd->kobj_counters, &pdd->attr_page_in);
  941. sysfs_remove_file(pdd->kobj_counters, &pdd->attr_page_out);
  942. kobject_del(pdd->kobj_counters);
  943. kobject_put(pdd->kobj_counters);
  944. pdd->kobj_counters = NULL;
  945. }
  946. kobject_del(p->kobj);
  947. kobject_put(p->kobj);
  948. p->kobj = NULL;
  949. }
  950. /*
  951. * If any GPU is ongoing reset, wait for reset complete.
  952. */
  953. static void kfd_process_wait_gpu_reset_complete(struct kfd_process *p)
  954. {
  955. int i;
  956. for (i = 0; i < p->n_pdds; i++)
  957. flush_workqueue(p->pdds[i]->dev->adev->reset_domain->wq);
  958. }
  959. /* No process locking is needed in this function, because the process
  960. * is not findable any more. We must assume that no other thread is
  961. * using it any more, otherwise we couldn't safely free the process
  962. * structure in the end.
  963. */
  964. static void kfd_process_wq_release(struct work_struct *work)
  965. {
  966. struct kfd_process *p = container_of(work, struct kfd_process,
  967. release_work);
  968. struct dma_fence *ef;
  969. kfd_process_dequeue_from_all_devices(p);
  970. pqm_uninit(&p->pqm);
  971. /*
  972. * If GPU in reset, user queues may still running, wait for reset complete.
  973. */
  974. kfd_process_wait_gpu_reset_complete(p);
  975. /* Signal the eviction fence after user mode queues are
  976. * destroyed. This allows any BOs to be freed without
  977. * triggering pointless evictions or waiting for fences.
  978. */
  979. synchronize_rcu();
  980. ef = rcu_access_pointer(p->ef);
  981. if (ef)
  982. dma_fence_signal(ef);
  983. kfd_process_remove_sysfs(p);
  984. kfd_process_kunmap_signal_bo(p);
  985. kfd_process_free_outstanding_kfd_bos(p);
  986. svm_range_list_fini(p);
  987. kfd_process_destroy_pdds(p);
  988. dma_fence_put(ef);
  989. kfd_event_free_process(p);
  990. kfd_pasid_free(p->pasid);
  991. mutex_destroy(&p->mutex);
  992. put_task_struct(p->lead_thread);
  993. kfree(p);
  994. }
  995. static void kfd_process_ref_release(struct kref *ref)
  996. {
  997. struct kfd_process *p = container_of(ref, struct kfd_process, ref);
  998. INIT_WORK(&p->release_work, kfd_process_wq_release);
  999. queue_work(kfd_process_wq, &p->release_work);
  1000. }
  1001. static struct mmu_notifier *kfd_process_alloc_notifier(struct mm_struct *mm)
  1002. {
  1003. int idx = srcu_read_lock(&kfd_processes_srcu);
  1004. struct kfd_process *p = find_process_by_mm(mm);
  1005. srcu_read_unlock(&kfd_processes_srcu, idx);
  1006. return p ? &p->mmu_notifier : ERR_PTR(-ESRCH);
  1007. }
  1008. static void kfd_process_free_notifier(struct mmu_notifier *mn)
  1009. {
  1010. kfd_unref_process(container_of(mn, struct kfd_process, mmu_notifier));
  1011. }
  1012. static void kfd_process_notifier_release_internal(struct kfd_process *p)
  1013. {
  1014. int i;
  1015. cancel_delayed_work_sync(&p->eviction_work);
  1016. cancel_delayed_work_sync(&p->restore_work);
  1017. for (i = 0; i < p->n_pdds; i++) {
  1018. struct kfd_process_device *pdd = p->pdds[i];
  1019. /* re-enable GFX OFF since runtime enable with ttmp setup disabled it. */
  1020. if (!kfd_dbg_is_rlc_restore_supported(pdd->dev) && p->runtime_info.ttmp_setup)
  1021. amdgpu_gfx_off_ctrl(pdd->dev->adev, true);
  1022. }
  1023. /* Indicate to other users that MM is no longer valid */
  1024. p->mm = NULL;
  1025. kfd_dbg_trap_disable(p);
  1026. if (atomic_read(&p->debugged_process_count) > 0) {
  1027. struct kfd_process *target;
  1028. unsigned int temp;
  1029. int idx = srcu_read_lock(&kfd_processes_srcu);
  1030. hash_for_each_rcu(kfd_processes_table, temp, target, kfd_processes) {
  1031. if (target->debugger_process && target->debugger_process == p) {
  1032. mutex_lock_nested(&target->mutex, 1);
  1033. kfd_dbg_trap_disable(target);
  1034. mutex_unlock(&target->mutex);
  1035. if (atomic_read(&p->debugged_process_count) == 0)
  1036. break;
  1037. }
  1038. }
  1039. srcu_read_unlock(&kfd_processes_srcu, idx);
  1040. }
  1041. mmu_notifier_put(&p->mmu_notifier);
  1042. }
  1043. static void kfd_process_notifier_release(struct mmu_notifier *mn,
  1044. struct mm_struct *mm)
  1045. {
  1046. struct kfd_process *p;
  1047. /*
  1048. * The kfd_process structure can not be free because the
  1049. * mmu_notifier srcu is read locked
  1050. */
  1051. p = container_of(mn, struct kfd_process, mmu_notifier);
  1052. if (WARN_ON(p->mm != mm))
  1053. return;
  1054. mutex_lock(&kfd_processes_mutex);
  1055. /*
  1056. * Do early return if table is empty.
  1057. *
  1058. * This could potentially happen if this function is called concurrently
  1059. * by mmu_notifier and by kfd_cleanup_pocesses.
  1060. *
  1061. */
  1062. if (hash_empty(kfd_processes_table)) {
  1063. mutex_unlock(&kfd_processes_mutex);
  1064. return;
  1065. }
  1066. hash_del_rcu(&p->kfd_processes);
  1067. mutex_unlock(&kfd_processes_mutex);
  1068. synchronize_srcu(&kfd_processes_srcu);
  1069. kfd_process_notifier_release_internal(p);
  1070. }
  1071. static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = {
  1072. .release = kfd_process_notifier_release,
  1073. .alloc_notifier = kfd_process_alloc_notifier,
  1074. .free_notifier = kfd_process_free_notifier,
  1075. };
  1076. /*
  1077. * This code handles the case when driver is being unloaded before all
  1078. * mm_struct are released. We need to safely free the kfd_process and
  1079. * avoid race conditions with mmu_notifier that might try to free them.
  1080. *
  1081. */
  1082. void kfd_cleanup_processes(void)
  1083. {
  1084. struct kfd_process *p;
  1085. struct hlist_node *p_temp;
  1086. unsigned int temp;
  1087. HLIST_HEAD(cleanup_list);
  1088. /*
  1089. * Move all remaining kfd_process from the process table to a
  1090. * temp list for processing. Once done, callback from mmu_notifier
  1091. * release will not see the kfd_process in the table and do early return,
  1092. * avoiding double free issues.
  1093. */
  1094. mutex_lock(&kfd_processes_mutex);
  1095. hash_for_each_safe(kfd_processes_table, temp, p_temp, p, kfd_processes) {
  1096. hash_del_rcu(&p->kfd_processes);
  1097. synchronize_srcu(&kfd_processes_srcu);
  1098. hlist_add_head(&p->kfd_processes, &cleanup_list);
  1099. }
  1100. mutex_unlock(&kfd_processes_mutex);
  1101. hlist_for_each_entry_safe(p, p_temp, &cleanup_list, kfd_processes)
  1102. kfd_process_notifier_release_internal(p);
  1103. /*
  1104. * Ensures that all outstanding free_notifier get called, triggering
  1105. * the release of the kfd_process struct.
  1106. */
  1107. mmu_notifier_synchronize();
  1108. }
  1109. int kfd_process_init_cwsr_apu(struct kfd_process *p, struct file *filep)
  1110. {
  1111. unsigned long offset;
  1112. int i;
  1113. if (p->has_cwsr)
  1114. return 0;
  1115. for (i = 0; i < p->n_pdds; i++) {
  1116. struct kfd_node *dev = p->pdds[i]->dev;
  1117. struct qcm_process_device *qpd = &p->pdds[i]->qpd;
  1118. if (!dev->kfd->cwsr_enabled || qpd->cwsr_kaddr || qpd->cwsr_base)
  1119. continue;
  1120. offset = KFD_MMAP_TYPE_RESERVED_MEM | KFD_MMAP_GPU_ID(dev->id);
  1121. qpd->tba_addr = (int64_t)vm_mmap(filep, 0,
  1122. KFD_CWSR_TBA_TMA_SIZE, PROT_READ | PROT_EXEC,
  1123. MAP_SHARED, offset);
  1124. if (IS_ERR_VALUE(qpd->tba_addr)) {
  1125. int err = qpd->tba_addr;
  1126. dev_err(dev->adev->dev,
  1127. "Failure to set tba address. error %d.\n", err);
  1128. qpd->tba_addr = 0;
  1129. qpd->cwsr_kaddr = NULL;
  1130. return err;
  1131. }
  1132. memcpy(qpd->cwsr_kaddr, dev->kfd->cwsr_isa, dev->kfd->cwsr_isa_size);
  1133. kfd_process_set_trap_debug_flag(qpd, p->debug_trap_enabled);
  1134. qpd->tma_addr = qpd->tba_addr + KFD_CWSR_TMA_OFFSET;
  1135. pr_debug("set tba :0x%llx, tma:0x%llx, cwsr_kaddr:%p for pqm.\n",
  1136. qpd->tba_addr, qpd->tma_addr, qpd->cwsr_kaddr);
  1137. }
  1138. p->has_cwsr = true;
  1139. return 0;
  1140. }
  1141. static int kfd_process_device_init_cwsr_dgpu(struct kfd_process_device *pdd)
  1142. {
  1143. struct kfd_node *dev = pdd->dev;
  1144. struct qcm_process_device *qpd = &pdd->qpd;
  1145. uint32_t flags = KFD_IOC_ALLOC_MEM_FLAGS_GTT
  1146. | KFD_IOC_ALLOC_MEM_FLAGS_NO_SUBSTITUTE
  1147. | KFD_IOC_ALLOC_MEM_FLAGS_EXECUTABLE;
  1148. struct kgd_mem *mem;
  1149. void *kaddr;
  1150. int ret;
  1151. if (!dev->kfd->cwsr_enabled || qpd->cwsr_kaddr || !qpd->cwsr_base)
  1152. return 0;
  1153. /* cwsr_base is only set for dGPU */
  1154. ret = kfd_process_alloc_gpuvm(pdd, qpd->cwsr_base,
  1155. KFD_CWSR_TBA_TMA_SIZE, flags, &mem, &kaddr);
  1156. if (ret)
  1157. return ret;
  1158. qpd->cwsr_mem = mem;
  1159. qpd->cwsr_kaddr = kaddr;
  1160. qpd->tba_addr = qpd->cwsr_base;
  1161. memcpy(qpd->cwsr_kaddr, dev->kfd->cwsr_isa, dev->kfd->cwsr_isa_size);
  1162. kfd_process_set_trap_debug_flag(&pdd->qpd,
  1163. pdd->process->debug_trap_enabled);
  1164. qpd->tma_addr = qpd->tba_addr + KFD_CWSR_TMA_OFFSET;
  1165. pr_debug("set tba :0x%llx, tma:0x%llx, cwsr_kaddr:%p for pqm.\n",
  1166. qpd->tba_addr, qpd->tma_addr, qpd->cwsr_kaddr);
  1167. return 0;
  1168. }
  1169. static void kfd_process_device_destroy_cwsr_dgpu(struct kfd_process_device *pdd)
  1170. {
  1171. struct kfd_node *dev = pdd->dev;
  1172. struct qcm_process_device *qpd = &pdd->qpd;
  1173. if (!dev->kfd->cwsr_enabled || !qpd->cwsr_kaddr || !qpd->cwsr_base)
  1174. return;
  1175. kfd_process_free_gpuvm(qpd->cwsr_mem, pdd, &qpd->cwsr_kaddr);
  1176. }
  1177. void kfd_process_set_trap_handler(struct qcm_process_device *qpd,
  1178. uint64_t tba_addr,
  1179. uint64_t tma_addr)
  1180. {
  1181. if (qpd->cwsr_kaddr) {
  1182. /* KFD trap handler is bound, record as second-level TBA/TMA
  1183. * in first-level TMA. First-level trap will jump to second.
  1184. */
  1185. uint64_t *tma =
  1186. (uint64_t *)(qpd->cwsr_kaddr + KFD_CWSR_TMA_OFFSET);
  1187. tma[0] = tba_addr;
  1188. tma[1] = tma_addr;
  1189. } else {
  1190. /* No trap handler bound, bind as first-level TBA/TMA. */
  1191. qpd->tba_addr = tba_addr;
  1192. qpd->tma_addr = tma_addr;
  1193. }
  1194. }
  1195. bool kfd_process_xnack_mode(struct kfd_process *p, bool supported)
  1196. {
  1197. int i;
  1198. /* On most GFXv9 GPUs, the retry mode in the SQ must match the
  1199. * boot time retry setting. Mixing processes with different
  1200. * XNACK/retry settings can hang the GPU.
  1201. *
  1202. * Different GPUs can have different noretry settings depending
  1203. * on HW bugs or limitations. We need to find at least one
  1204. * XNACK mode for this process that's compatible with all GPUs.
  1205. * Fortunately GPUs with retry enabled (noretry=0) can run code
  1206. * built for XNACK-off. On GFXv9 it may perform slower.
  1207. *
  1208. * Therefore applications built for XNACK-off can always be
  1209. * supported and will be our fallback if any GPU does not
  1210. * support retry.
  1211. */
  1212. for (i = 0; i < p->n_pdds; i++) {
  1213. struct kfd_node *dev = p->pdds[i]->dev;
  1214. /* Only consider GFXv9 and higher GPUs. Older GPUs don't
  1215. * support the SVM APIs and don't need to be considered
  1216. * for the XNACK mode selection.
  1217. */
  1218. if (!KFD_IS_SOC15(dev))
  1219. continue;
  1220. /* Aldebaran can always support XNACK because it can support
  1221. * per-process XNACK mode selection. But let the dev->noretry
  1222. * setting still influence the default XNACK mode.
  1223. */
  1224. if (supported && KFD_SUPPORT_XNACK_PER_PROCESS(dev)) {
  1225. if (!amdgpu_sriov_xnack_support(dev->kfd->adev)) {
  1226. pr_debug("SRIOV platform xnack not supported\n");
  1227. return false;
  1228. }
  1229. continue;
  1230. }
  1231. /* GFXv10 and later GPUs do not support shader preemption
  1232. * during page faults. This can lead to poor QoS for queue
  1233. * management and memory-manager-related preemptions or
  1234. * even deadlocks.
  1235. */
  1236. if (KFD_GC_VERSION(dev) >= IP_VERSION(10, 1, 1))
  1237. return false;
  1238. if (dev->kfd->noretry)
  1239. return false;
  1240. }
  1241. return true;
  1242. }
  1243. void kfd_process_set_trap_debug_flag(struct qcm_process_device *qpd,
  1244. bool enabled)
  1245. {
  1246. if (qpd->cwsr_kaddr) {
  1247. uint64_t *tma =
  1248. (uint64_t *)(qpd->cwsr_kaddr + KFD_CWSR_TMA_OFFSET);
  1249. tma[2] = enabled;
  1250. }
  1251. }
  1252. /*
  1253. * On return the kfd_process is fully operational and will be freed when the
  1254. * mm is released
  1255. */
  1256. static struct kfd_process *create_process(const struct task_struct *thread)
  1257. {
  1258. struct kfd_process *process;
  1259. struct mmu_notifier *mn;
  1260. int err = -ENOMEM;
  1261. process = kzalloc(sizeof(*process), GFP_KERNEL);
  1262. if (!process)
  1263. goto err_alloc_process;
  1264. kref_init(&process->ref);
  1265. mutex_init(&process->mutex);
  1266. process->mm = thread->mm;
  1267. process->lead_thread = thread->group_leader;
  1268. process->n_pdds = 0;
  1269. process->queues_paused = false;
  1270. INIT_DELAYED_WORK(&process->eviction_work, evict_process_worker);
  1271. INIT_DELAYED_WORK(&process->restore_work, restore_process_worker);
  1272. process->last_restore_timestamp = get_jiffies_64();
  1273. err = kfd_event_init_process(process);
  1274. if (err)
  1275. goto err_event_init;
  1276. process->is_32bit_user_mode = in_compat_syscall();
  1277. process->debug_trap_enabled = false;
  1278. process->debugger_process = NULL;
  1279. process->exception_enable_mask = 0;
  1280. atomic_set(&process->debugged_process_count, 0);
  1281. sema_init(&process->runtime_enable_sema, 0);
  1282. process->pasid = kfd_pasid_alloc();
  1283. if (process->pasid == 0) {
  1284. err = -ENOSPC;
  1285. goto err_alloc_pasid;
  1286. }
  1287. err = pqm_init(&process->pqm, process);
  1288. if (err != 0)
  1289. goto err_process_pqm_init;
  1290. /* init process apertures*/
  1291. err = kfd_init_apertures(process);
  1292. if (err != 0)
  1293. goto err_init_apertures;
  1294. /* Check XNACK support after PDDs are created in kfd_init_apertures */
  1295. process->xnack_enabled = kfd_process_xnack_mode(process, false);
  1296. err = svm_range_list_init(process);
  1297. if (err)
  1298. goto err_init_svm_range_list;
  1299. /* alloc_notifier needs to find the process in the hash table */
  1300. hash_add_rcu(kfd_processes_table, &process->kfd_processes,
  1301. (uintptr_t)process->mm);
  1302. /* Avoid free_notifier to start kfd_process_wq_release if
  1303. * mmu_notifier_get failed because of pending signal.
  1304. */
  1305. kref_get(&process->ref);
  1306. /* MMU notifier registration must be the last call that can fail
  1307. * because after this point we cannot unwind the process creation.
  1308. * After this point, mmu_notifier_put will trigger the cleanup by
  1309. * dropping the last process reference in the free_notifier.
  1310. */
  1311. mn = mmu_notifier_get(&kfd_process_mmu_notifier_ops, process->mm);
  1312. if (IS_ERR(mn)) {
  1313. err = PTR_ERR(mn);
  1314. goto err_register_notifier;
  1315. }
  1316. BUG_ON(mn != &process->mmu_notifier);
  1317. kfd_unref_process(process);
  1318. get_task_struct(process->lead_thread);
  1319. INIT_WORK(&process->debug_event_workarea, debug_event_write_work_handler);
  1320. return process;
  1321. err_register_notifier:
  1322. hash_del_rcu(&process->kfd_processes);
  1323. svm_range_list_fini(process);
  1324. err_init_svm_range_list:
  1325. kfd_process_free_outstanding_kfd_bos(process);
  1326. kfd_process_destroy_pdds(process);
  1327. err_init_apertures:
  1328. pqm_uninit(&process->pqm);
  1329. err_process_pqm_init:
  1330. kfd_pasid_free(process->pasid);
  1331. err_alloc_pasid:
  1332. kfd_event_free_process(process);
  1333. err_event_init:
  1334. mutex_destroy(&process->mutex);
  1335. kfree(process);
  1336. err_alloc_process:
  1337. return ERR_PTR(err);
  1338. }
  1339. struct kfd_process_device *kfd_get_process_device_data(struct kfd_node *dev,
  1340. struct kfd_process *p)
  1341. {
  1342. int i;
  1343. for (i = 0; i < p->n_pdds; i++)
  1344. if (p->pdds[i]->dev == dev)
  1345. return p->pdds[i];
  1346. return NULL;
  1347. }
  1348. struct kfd_process_device *kfd_create_process_device_data(struct kfd_node *dev,
  1349. struct kfd_process *p)
  1350. {
  1351. struct kfd_process_device *pdd = NULL;
  1352. if (WARN_ON_ONCE(p->n_pdds >= MAX_GPU_INSTANCE))
  1353. return NULL;
  1354. pdd = kzalloc(sizeof(*pdd), GFP_KERNEL);
  1355. if (!pdd)
  1356. return NULL;
  1357. pdd->dev = dev;
  1358. INIT_LIST_HEAD(&pdd->qpd.queues_list);
  1359. INIT_LIST_HEAD(&pdd->qpd.priv_queue_list);
  1360. pdd->qpd.dqm = dev->dqm;
  1361. pdd->qpd.pqm = &p->pqm;
  1362. pdd->qpd.evicted = 0;
  1363. pdd->qpd.mapped_gws_queue = false;
  1364. pdd->process = p;
  1365. pdd->bound = PDD_UNBOUND;
  1366. pdd->already_dequeued = false;
  1367. pdd->runtime_inuse = false;
  1368. atomic64_set(&pdd->vram_usage, 0);
  1369. pdd->sdma_past_activity_counter = 0;
  1370. pdd->user_gpu_id = dev->id;
  1371. atomic64_set(&pdd->evict_duration_counter, 0);
  1372. p->pdds[p->n_pdds++] = pdd;
  1373. if (kfd_dbg_is_per_vmid_supported(pdd->dev))
  1374. pdd->spi_dbg_override = pdd->dev->kfd2kgd->disable_debug_trap(
  1375. pdd->dev->adev,
  1376. false,
  1377. 0);
  1378. /* Init idr used for memory handle translation */
  1379. idr_init(&pdd->alloc_idr);
  1380. return pdd;
  1381. }
  1382. /**
  1383. * kfd_process_device_init_vm - Initialize a VM for a process-device
  1384. *
  1385. * @pdd: The process-device
  1386. * @drm_file: Optional pointer to a DRM file descriptor
  1387. *
  1388. * If @drm_file is specified, it will be used to acquire the VM from
  1389. * that file descriptor. If successful, the @pdd takes ownership of
  1390. * the file descriptor.
  1391. *
  1392. * If @drm_file is NULL, a new VM is created.
  1393. *
  1394. * Returns 0 on success, -errno on failure.
  1395. */
  1396. int kfd_process_device_init_vm(struct kfd_process_device *pdd,
  1397. struct file *drm_file)
  1398. {
  1399. struct amdgpu_fpriv *drv_priv;
  1400. struct amdgpu_vm *avm;
  1401. struct kfd_process *p;
  1402. struct dma_fence *ef;
  1403. struct kfd_node *dev;
  1404. int ret;
  1405. if (!drm_file)
  1406. return -EINVAL;
  1407. if (pdd->drm_priv)
  1408. return -EBUSY;
  1409. ret = amdgpu_file_to_fpriv(drm_file, &drv_priv);
  1410. if (ret)
  1411. return ret;
  1412. avm = &drv_priv->vm;
  1413. p = pdd->process;
  1414. dev = pdd->dev;
  1415. ret = amdgpu_amdkfd_gpuvm_acquire_process_vm(dev->adev, avm,
  1416. &p->kgd_process_info,
  1417. p->ef ? NULL : &ef);
  1418. if (ret) {
  1419. dev_err(dev->adev->dev, "Failed to create process VM object\n");
  1420. return ret;
  1421. }
  1422. if (!p->ef)
  1423. RCU_INIT_POINTER(p->ef, ef);
  1424. pdd->drm_priv = drm_file->private_data;
  1425. ret = kfd_process_device_reserve_ib_mem(pdd);
  1426. if (ret)
  1427. goto err_reserve_ib_mem;
  1428. ret = kfd_process_device_init_cwsr_dgpu(pdd);
  1429. if (ret)
  1430. goto err_init_cwsr;
  1431. ret = amdgpu_amdkfd_gpuvm_set_vm_pasid(dev->adev, avm, p->pasid);
  1432. if (ret)
  1433. goto err_set_pasid;
  1434. pdd->drm_file = drm_file;
  1435. return 0;
  1436. err_set_pasid:
  1437. kfd_process_device_destroy_cwsr_dgpu(pdd);
  1438. err_init_cwsr:
  1439. kfd_process_device_destroy_ib_mem(pdd);
  1440. err_reserve_ib_mem:
  1441. pdd->drm_priv = NULL;
  1442. amdgpu_amdkfd_gpuvm_destroy_cb(dev->adev, avm);
  1443. return ret;
  1444. }
  1445. /*
  1446. * Direct the IOMMU to bind the process (specifically the pasid->mm)
  1447. * to the device.
  1448. * Unbinding occurs when the process dies or the device is removed.
  1449. *
  1450. * Assumes that the process lock is held.
  1451. */
  1452. struct kfd_process_device *kfd_bind_process_to_device(struct kfd_node *dev,
  1453. struct kfd_process *p)
  1454. {
  1455. struct kfd_process_device *pdd;
  1456. int err;
  1457. pdd = kfd_get_process_device_data(dev, p);
  1458. if (!pdd) {
  1459. dev_err(dev->adev->dev, "Process device data doesn't exist\n");
  1460. return ERR_PTR(-ENOMEM);
  1461. }
  1462. if (!pdd->drm_priv)
  1463. return ERR_PTR(-ENODEV);
  1464. /*
  1465. * signal runtime-pm system to auto resume and prevent
  1466. * further runtime suspend once device pdd is created until
  1467. * pdd is destroyed.
  1468. */
  1469. if (!pdd->runtime_inuse) {
  1470. err = pm_runtime_get_sync(adev_to_drm(dev->adev)->dev);
  1471. if (err < 0) {
  1472. pm_runtime_put_autosuspend(adev_to_drm(dev->adev)->dev);
  1473. return ERR_PTR(err);
  1474. }
  1475. }
  1476. /*
  1477. * make sure that runtime_usage counter is incremented just once
  1478. * per pdd
  1479. */
  1480. pdd->runtime_inuse = true;
  1481. return pdd;
  1482. }
  1483. /* Create specific handle mapped to mem from process local memory idr
  1484. * Assumes that the process lock is held.
  1485. */
  1486. int kfd_process_device_create_obj_handle(struct kfd_process_device *pdd,
  1487. void *mem)
  1488. {
  1489. return idr_alloc(&pdd->alloc_idr, mem, 0, 0, GFP_KERNEL);
  1490. }
  1491. /* Translate specific handle from process local memory idr
  1492. * Assumes that the process lock is held.
  1493. */
  1494. void *kfd_process_device_translate_handle(struct kfd_process_device *pdd,
  1495. int handle)
  1496. {
  1497. if (handle < 0)
  1498. return NULL;
  1499. return idr_find(&pdd->alloc_idr, handle);
  1500. }
  1501. /* Remove specific handle from process local memory idr
  1502. * Assumes that the process lock is held.
  1503. */
  1504. void kfd_process_device_remove_obj_handle(struct kfd_process_device *pdd,
  1505. int handle)
  1506. {
  1507. if (handle >= 0)
  1508. idr_remove(&pdd->alloc_idr, handle);
  1509. }
  1510. /* This increments the process->ref counter. */
  1511. struct kfd_process *kfd_lookup_process_by_pasid(u32 pasid)
  1512. {
  1513. struct kfd_process *p, *ret_p = NULL;
  1514. unsigned int temp;
  1515. int idx = srcu_read_lock(&kfd_processes_srcu);
  1516. hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
  1517. if (p->pasid == pasid) {
  1518. kref_get(&p->ref);
  1519. ret_p = p;
  1520. break;
  1521. }
  1522. }
  1523. srcu_read_unlock(&kfd_processes_srcu, idx);
  1524. return ret_p;
  1525. }
  1526. /* This increments the process->ref counter. */
  1527. struct kfd_process *kfd_lookup_process_by_mm(const struct mm_struct *mm)
  1528. {
  1529. struct kfd_process *p;
  1530. int idx = srcu_read_lock(&kfd_processes_srcu);
  1531. p = find_process_by_mm(mm);
  1532. if (p)
  1533. kref_get(&p->ref);
  1534. srcu_read_unlock(&kfd_processes_srcu, idx);
  1535. return p;
  1536. }
  1537. /* kfd_process_evict_queues - Evict all user queues of a process
  1538. *
  1539. * Eviction is reference-counted per process-device. This means multiple
  1540. * evictions from different sources can be nested safely.
  1541. */
  1542. int kfd_process_evict_queues(struct kfd_process *p, uint32_t trigger)
  1543. {
  1544. int r = 0;
  1545. int i;
  1546. unsigned int n_evicted = 0;
  1547. for (i = 0; i < p->n_pdds; i++) {
  1548. struct kfd_process_device *pdd = p->pdds[i];
  1549. struct device *dev = pdd->dev->adev->dev;
  1550. kfd_smi_event_queue_eviction(pdd->dev, p->lead_thread->pid,
  1551. trigger);
  1552. r = pdd->dev->dqm->ops.evict_process_queues(pdd->dev->dqm,
  1553. &pdd->qpd);
  1554. /* evict return -EIO if HWS is hang or asic is resetting, in this case
  1555. * we would like to set all the queues to be in evicted state to prevent
  1556. * them been add back since they actually not be saved right now.
  1557. */
  1558. if (r && r != -EIO) {
  1559. dev_err(dev, "Failed to evict process queues\n");
  1560. goto fail;
  1561. }
  1562. n_evicted++;
  1563. pdd->dev->dqm->is_hws_hang = false;
  1564. }
  1565. return r;
  1566. fail:
  1567. /* To keep state consistent, roll back partial eviction by
  1568. * restoring queues
  1569. */
  1570. for (i = 0; i < p->n_pdds; i++) {
  1571. struct kfd_process_device *pdd = p->pdds[i];
  1572. if (n_evicted == 0)
  1573. break;
  1574. kfd_smi_event_queue_restore(pdd->dev, p->lead_thread->pid);
  1575. if (pdd->dev->dqm->ops.restore_process_queues(pdd->dev->dqm,
  1576. &pdd->qpd))
  1577. dev_err(pdd->dev->adev->dev,
  1578. "Failed to restore queues\n");
  1579. n_evicted--;
  1580. }
  1581. return r;
  1582. }
  1583. /* kfd_process_restore_queues - Restore all user queues of a process */
  1584. int kfd_process_restore_queues(struct kfd_process *p)
  1585. {
  1586. int r, ret = 0;
  1587. int i;
  1588. for (i = 0; i < p->n_pdds; i++) {
  1589. struct kfd_process_device *pdd = p->pdds[i];
  1590. struct device *dev = pdd->dev->adev->dev;
  1591. kfd_smi_event_queue_restore(pdd->dev, p->lead_thread->pid);
  1592. r = pdd->dev->dqm->ops.restore_process_queues(pdd->dev->dqm,
  1593. &pdd->qpd);
  1594. if (r) {
  1595. dev_err(dev, "Failed to restore process queues\n");
  1596. if (!ret)
  1597. ret = r;
  1598. }
  1599. }
  1600. return ret;
  1601. }
  1602. int kfd_process_gpuidx_from_gpuid(struct kfd_process *p, uint32_t gpu_id)
  1603. {
  1604. int i;
  1605. for (i = 0; i < p->n_pdds; i++)
  1606. if (p->pdds[i] && gpu_id == p->pdds[i]->user_gpu_id)
  1607. return i;
  1608. return -EINVAL;
  1609. }
  1610. int
  1611. kfd_process_gpuid_from_node(struct kfd_process *p, struct kfd_node *node,
  1612. uint32_t *gpuid, uint32_t *gpuidx)
  1613. {
  1614. int i;
  1615. for (i = 0; i < p->n_pdds; i++)
  1616. if (p->pdds[i] && p->pdds[i]->dev == node) {
  1617. *gpuid = p->pdds[i]->user_gpu_id;
  1618. *gpuidx = i;
  1619. return 0;
  1620. }
  1621. return -EINVAL;
  1622. }
  1623. static int signal_eviction_fence(struct kfd_process *p)
  1624. {
  1625. struct dma_fence *ef;
  1626. int ret;
  1627. rcu_read_lock();
  1628. ef = dma_fence_get_rcu_safe(&p->ef);
  1629. rcu_read_unlock();
  1630. if (!ef)
  1631. return -EINVAL;
  1632. ret = dma_fence_signal(ef);
  1633. dma_fence_put(ef);
  1634. return ret;
  1635. }
  1636. static void evict_process_worker(struct work_struct *work)
  1637. {
  1638. int ret;
  1639. struct kfd_process *p;
  1640. struct delayed_work *dwork;
  1641. dwork = to_delayed_work(work);
  1642. /* Process termination destroys this worker thread. So during the
  1643. * lifetime of this thread, kfd_process p will be valid
  1644. */
  1645. p = container_of(dwork, struct kfd_process, eviction_work);
  1646. pr_debug("Started evicting pasid 0x%x\n", p->pasid);
  1647. ret = kfd_process_evict_queues(p, KFD_QUEUE_EVICTION_TRIGGER_TTM);
  1648. if (!ret) {
  1649. /* If another thread already signaled the eviction fence,
  1650. * they are responsible stopping the queues and scheduling
  1651. * the restore work.
  1652. */
  1653. if (signal_eviction_fence(p) ||
  1654. mod_delayed_work(kfd_restore_wq, &p->restore_work,
  1655. msecs_to_jiffies(PROCESS_RESTORE_TIME_MS)))
  1656. kfd_process_restore_queues(p);
  1657. pr_debug("Finished evicting pasid 0x%x\n", p->pasid);
  1658. } else
  1659. pr_err("Failed to evict queues of pasid 0x%x\n", p->pasid);
  1660. }
  1661. static int restore_process_helper(struct kfd_process *p)
  1662. {
  1663. int ret = 0;
  1664. /* VMs may not have been acquired yet during debugging. */
  1665. if (p->kgd_process_info) {
  1666. ret = amdgpu_amdkfd_gpuvm_restore_process_bos(
  1667. p->kgd_process_info, &p->ef);
  1668. if (ret)
  1669. return ret;
  1670. }
  1671. ret = kfd_process_restore_queues(p);
  1672. if (!ret)
  1673. pr_debug("Finished restoring pasid 0x%x\n", p->pasid);
  1674. else
  1675. pr_err("Failed to restore queues of pasid 0x%x\n", p->pasid);
  1676. return ret;
  1677. }
  1678. static void restore_process_worker(struct work_struct *work)
  1679. {
  1680. struct delayed_work *dwork;
  1681. struct kfd_process *p;
  1682. int ret = 0;
  1683. dwork = to_delayed_work(work);
  1684. /* Process termination destroys this worker thread. So during the
  1685. * lifetime of this thread, kfd_process p will be valid
  1686. */
  1687. p = container_of(dwork, struct kfd_process, restore_work);
  1688. pr_debug("Started restoring pasid 0x%x\n", p->pasid);
  1689. /* Setting last_restore_timestamp before successful restoration.
  1690. * Otherwise this would have to be set by KGD (restore_process_bos)
  1691. * before KFD BOs are unreserved. If not, the process can be evicted
  1692. * again before the timestamp is set.
  1693. * If restore fails, the timestamp will be set again in the next
  1694. * attempt. This would mean that the minimum GPU quanta would be
  1695. * PROCESS_ACTIVE_TIME_MS - (time to execute the following two
  1696. * functions)
  1697. */
  1698. p->last_restore_timestamp = get_jiffies_64();
  1699. ret = restore_process_helper(p);
  1700. if (ret) {
  1701. pr_debug("Failed to restore BOs of pasid 0x%x, retry after %d ms\n",
  1702. p->pasid, PROCESS_BACK_OFF_TIME_MS);
  1703. if (mod_delayed_work(kfd_restore_wq, &p->restore_work,
  1704. msecs_to_jiffies(PROCESS_RESTORE_TIME_MS)))
  1705. kfd_process_restore_queues(p);
  1706. }
  1707. }
  1708. void kfd_suspend_all_processes(void)
  1709. {
  1710. struct kfd_process *p;
  1711. unsigned int temp;
  1712. int idx = srcu_read_lock(&kfd_processes_srcu);
  1713. WARN(debug_evictions, "Evicting all processes");
  1714. hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
  1715. if (kfd_process_evict_queues(p, KFD_QUEUE_EVICTION_TRIGGER_SUSPEND))
  1716. pr_err("Failed to suspend process 0x%x\n", p->pasid);
  1717. signal_eviction_fence(p);
  1718. }
  1719. srcu_read_unlock(&kfd_processes_srcu, idx);
  1720. }
  1721. int kfd_resume_all_processes(void)
  1722. {
  1723. struct kfd_process *p;
  1724. unsigned int temp;
  1725. int ret = 0, idx = srcu_read_lock(&kfd_processes_srcu);
  1726. hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
  1727. if (restore_process_helper(p)) {
  1728. pr_err("Restore process %d failed during resume\n",
  1729. p->pasid);
  1730. ret = -EFAULT;
  1731. }
  1732. }
  1733. srcu_read_unlock(&kfd_processes_srcu, idx);
  1734. return ret;
  1735. }
  1736. int kfd_reserved_mem_mmap(struct kfd_node *dev, struct kfd_process *process,
  1737. struct vm_area_struct *vma)
  1738. {
  1739. struct kfd_process_device *pdd;
  1740. struct qcm_process_device *qpd;
  1741. if ((vma->vm_end - vma->vm_start) != KFD_CWSR_TBA_TMA_SIZE) {
  1742. dev_err(dev->adev->dev, "Incorrect CWSR mapping size.\n");
  1743. return -EINVAL;
  1744. }
  1745. pdd = kfd_get_process_device_data(dev, process);
  1746. if (!pdd)
  1747. return -EINVAL;
  1748. qpd = &pdd->qpd;
  1749. qpd->cwsr_kaddr = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  1750. get_order(KFD_CWSR_TBA_TMA_SIZE));
  1751. if (!qpd->cwsr_kaddr) {
  1752. dev_err(dev->adev->dev,
  1753. "Error allocating per process CWSR buffer.\n");
  1754. return -ENOMEM;
  1755. }
  1756. vm_flags_set(vma, VM_IO | VM_DONTCOPY | VM_DONTEXPAND
  1757. | VM_NORESERVE | VM_DONTDUMP | VM_PFNMAP);
  1758. /* Mapping pages to user process */
  1759. return remap_pfn_range(vma, vma->vm_start,
  1760. PFN_DOWN(__pa(qpd->cwsr_kaddr)),
  1761. KFD_CWSR_TBA_TMA_SIZE, vma->vm_page_prot);
  1762. }
  1763. /* assumes caller holds process lock. */
  1764. int kfd_process_drain_interrupts(struct kfd_process_device *pdd)
  1765. {
  1766. uint32_t irq_drain_fence[8];
  1767. uint8_t node_id = 0;
  1768. int r = 0;
  1769. if (!KFD_IS_SOC15(pdd->dev))
  1770. return 0;
  1771. pdd->process->irq_drain_is_open = true;
  1772. memset(irq_drain_fence, 0, sizeof(irq_drain_fence));
  1773. irq_drain_fence[0] = (KFD_IRQ_FENCE_SOURCEID << 8) |
  1774. KFD_IRQ_FENCE_CLIENTID;
  1775. irq_drain_fence[3] = pdd->process->pasid;
  1776. /*
  1777. * For GFX 9.4.3, send the NodeId also in IH cookie DW[3]
  1778. */
  1779. if (KFD_GC_VERSION(pdd->dev->kfd) == IP_VERSION(9, 4, 3) ||
  1780. KFD_GC_VERSION(pdd->dev->kfd) == IP_VERSION(9, 4, 4)) {
  1781. node_id = ffs(pdd->dev->interrupt_bitmap) - 1;
  1782. irq_drain_fence[3] |= node_id << 16;
  1783. }
  1784. /* ensure stale irqs scheduled KFD interrupts and send drain fence. */
  1785. if (amdgpu_amdkfd_send_close_event_drain_irq(pdd->dev->adev,
  1786. irq_drain_fence)) {
  1787. pdd->process->irq_drain_is_open = false;
  1788. return 0;
  1789. }
  1790. r = wait_event_interruptible(pdd->process->wait_irq_drain,
  1791. !READ_ONCE(pdd->process->irq_drain_is_open));
  1792. if (r)
  1793. pdd->process->irq_drain_is_open = false;
  1794. return r;
  1795. }
  1796. void kfd_process_close_interrupt_drain(unsigned int pasid)
  1797. {
  1798. struct kfd_process *p;
  1799. p = kfd_lookup_process_by_pasid(pasid);
  1800. if (!p)
  1801. return;
  1802. WRITE_ONCE(p->irq_drain_is_open, false);
  1803. wake_up_all(&p->wait_irq_drain);
  1804. kfd_unref_process(p);
  1805. }
  1806. struct send_exception_work_handler_workarea {
  1807. struct work_struct work;
  1808. struct kfd_process *p;
  1809. unsigned int queue_id;
  1810. uint64_t error_reason;
  1811. };
  1812. static void send_exception_work_handler(struct work_struct *work)
  1813. {
  1814. struct send_exception_work_handler_workarea *workarea;
  1815. struct kfd_process *p;
  1816. struct queue *q;
  1817. struct mm_struct *mm;
  1818. struct kfd_context_save_area_header __user *csa_header;
  1819. uint64_t __user *err_payload_ptr;
  1820. uint64_t cur_err;
  1821. uint32_t ev_id;
  1822. workarea = container_of(work,
  1823. struct send_exception_work_handler_workarea,
  1824. work);
  1825. p = workarea->p;
  1826. mm = get_task_mm(p->lead_thread);
  1827. if (!mm)
  1828. return;
  1829. kthread_use_mm(mm);
  1830. q = pqm_get_user_queue(&p->pqm, workarea->queue_id);
  1831. if (!q)
  1832. goto out;
  1833. csa_header = (void __user *)q->properties.ctx_save_restore_area_address;
  1834. get_user(err_payload_ptr, (uint64_t __user **)&csa_header->err_payload_addr);
  1835. get_user(cur_err, err_payload_ptr);
  1836. cur_err |= workarea->error_reason;
  1837. put_user(cur_err, err_payload_ptr);
  1838. get_user(ev_id, &csa_header->err_event_id);
  1839. kfd_set_event(p, ev_id);
  1840. out:
  1841. kthread_unuse_mm(mm);
  1842. mmput(mm);
  1843. }
  1844. int kfd_send_exception_to_runtime(struct kfd_process *p,
  1845. unsigned int queue_id,
  1846. uint64_t error_reason)
  1847. {
  1848. struct send_exception_work_handler_workarea worker;
  1849. INIT_WORK_ONSTACK(&worker.work, send_exception_work_handler);
  1850. worker.p = p;
  1851. worker.queue_id = queue_id;
  1852. worker.error_reason = error_reason;
  1853. schedule_work(&worker.work);
  1854. flush_work(&worker.work);
  1855. destroy_work_on_stack(&worker.work);
  1856. return 0;
  1857. }
  1858. struct kfd_process_device *kfd_process_device_data_by_id(struct kfd_process *p, uint32_t gpu_id)
  1859. {
  1860. int i;
  1861. if (gpu_id) {
  1862. for (i = 0; i < p->n_pdds; i++) {
  1863. struct kfd_process_device *pdd = p->pdds[i];
  1864. if (pdd->user_gpu_id == gpu_id)
  1865. return pdd;
  1866. }
  1867. }
  1868. return NULL;
  1869. }
  1870. int kfd_process_get_user_gpu_id(struct kfd_process *p, uint32_t actual_gpu_id)
  1871. {
  1872. int i;
  1873. if (!actual_gpu_id)
  1874. return 0;
  1875. for (i = 0; i < p->n_pdds; i++) {
  1876. struct kfd_process_device *pdd = p->pdds[i];
  1877. if (pdd->dev->id == actual_gpu_id)
  1878. return pdd->user_gpu_id;
  1879. }
  1880. return -EINVAL;
  1881. }
  1882. #if defined(CONFIG_DEBUG_FS)
  1883. int kfd_debugfs_mqds_by_process(struct seq_file *m, void *data)
  1884. {
  1885. struct kfd_process *p;
  1886. unsigned int temp;
  1887. int r = 0;
  1888. int idx = srcu_read_lock(&kfd_processes_srcu);
  1889. hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
  1890. seq_printf(m, "Process %d PASID 0x%x:\n",
  1891. p->lead_thread->tgid, p->pasid);
  1892. mutex_lock(&p->mutex);
  1893. r = pqm_debugfs_mqds(m, &p->pqm);
  1894. mutex_unlock(&p->mutex);
  1895. if (r)
  1896. break;
  1897. }
  1898. srcu_read_unlock(&kfd_processes_srcu, idx);
  1899. return r;
  1900. }
  1901. #endif