kfd_process.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /*
  2. * Copyright 2014 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. #include <linux/mutex.h>
  23. #include <linux/log2.h>
  24. #include <linux/sched.h>
  25. #include <linux/slab.h>
  26. #include <linux/amd-iommu.h>
  27. #include <linux/notifier.h>
  28. #include <linux/compat.h>
  29. struct mm_struct;
  30. #include "kfd_priv.h"
  31. #include "kfd_dbgmgr.h"
  32. /*
  33. * Initial size for the array of queues.
  34. * The allocated size is doubled each time
  35. * it is exceeded up to MAX_PROCESS_QUEUES.
  36. */
  37. #define INITIAL_QUEUE_ARRAY_SIZE 16
  38. /*
  39. * List of struct kfd_process (field kfd_process).
  40. * Unique/indexed by mm_struct*
  41. */
  42. #define KFD_PROCESS_TABLE_SIZE 5 /* bits: 32 entries */
  43. static DEFINE_HASHTABLE(kfd_processes_table, KFD_PROCESS_TABLE_SIZE);
  44. static DEFINE_MUTEX(kfd_processes_mutex);
  45. DEFINE_STATIC_SRCU(kfd_processes_srcu);
  46. static struct workqueue_struct *kfd_process_wq;
  47. struct kfd_process_release_work {
  48. struct work_struct kfd_work;
  49. struct kfd_process *p;
  50. };
  51. static struct kfd_process *find_process(const struct task_struct *thread);
  52. static struct kfd_process *create_process(const struct task_struct *thread);
  53. void kfd_process_create_wq(void)
  54. {
  55. if (!kfd_process_wq)
  56. kfd_process_wq = alloc_workqueue("kfd_process_wq", 0, 0);
  57. }
  58. void kfd_process_destroy_wq(void)
  59. {
  60. if (kfd_process_wq) {
  61. destroy_workqueue(kfd_process_wq);
  62. kfd_process_wq = NULL;
  63. }
  64. }
  65. struct kfd_process *kfd_create_process(const struct task_struct *thread)
  66. {
  67. struct kfd_process *process;
  68. BUG_ON(!kfd_process_wq);
  69. if (thread->mm == NULL)
  70. return ERR_PTR(-EINVAL);
  71. /* Only the pthreads threading model is supported. */
  72. if (thread->group_leader->mm != thread->mm)
  73. return ERR_PTR(-EINVAL);
  74. /* Take mmap_sem because we call __mmu_notifier_register inside */
  75. down_write(&thread->mm->mmap_sem);
  76. /*
  77. * take kfd processes mutex before starting of process creation
  78. * so there won't be a case where two threads of the same process
  79. * create two kfd_process structures
  80. */
  81. mutex_lock(&kfd_processes_mutex);
  82. /* A prior open of /dev/kfd could have already created the process. */
  83. process = find_process(thread);
  84. if (process)
  85. pr_debug("kfd: process already found\n");
  86. if (!process)
  87. process = create_process(thread);
  88. mutex_unlock(&kfd_processes_mutex);
  89. up_write(&thread->mm->mmap_sem);
  90. return process;
  91. }
  92. struct kfd_process *kfd_get_process(const struct task_struct *thread)
  93. {
  94. struct kfd_process *process;
  95. if (thread->mm == NULL)
  96. return ERR_PTR(-EINVAL);
  97. /* Only the pthreads threading model is supported. */
  98. if (thread->group_leader->mm != thread->mm)
  99. return ERR_PTR(-EINVAL);
  100. process = find_process(thread);
  101. return process;
  102. }
  103. static struct kfd_process *find_process_by_mm(const struct mm_struct *mm)
  104. {
  105. struct kfd_process *process;
  106. hash_for_each_possible_rcu(kfd_processes_table, process,
  107. kfd_processes, (uintptr_t)mm)
  108. if (process->mm == mm)
  109. return process;
  110. return NULL;
  111. }
  112. static struct kfd_process *find_process(const struct task_struct *thread)
  113. {
  114. struct kfd_process *p;
  115. int idx;
  116. idx = srcu_read_lock(&kfd_processes_srcu);
  117. p = find_process_by_mm(thread->mm);
  118. srcu_read_unlock(&kfd_processes_srcu, idx);
  119. return p;
  120. }
  121. static void kfd_process_wq_release(struct work_struct *work)
  122. {
  123. struct kfd_process_release_work *my_work;
  124. struct kfd_process_device *pdd, *temp;
  125. struct kfd_process *p;
  126. my_work = (struct kfd_process_release_work *) work;
  127. p = my_work->p;
  128. pr_debug("Releasing process (pasid %d) in workqueue\n",
  129. p->pasid);
  130. mutex_lock(&p->mutex);
  131. list_for_each_entry_safe(pdd, temp, &p->per_device_data,
  132. per_device_list) {
  133. pr_debug("Releasing pdd (topology id %d) for process (pasid %d) in workqueue\n",
  134. pdd->dev->id, p->pasid);
  135. if (pdd->reset_wavefronts)
  136. dbgdev_wave_reset_wavefronts(pdd->dev, p);
  137. amd_iommu_unbind_pasid(pdd->dev->pdev, p->pasid);
  138. list_del(&pdd->per_device_list);
  139. kfree(pdd);
  140. }
  141. kfd_event_free_process(p);
  142. kfd_pasid_free(p->pasid);
  143. mutex_unlock(&p->mutex);
  144. mutex_destroy(&p->mutex);
  145. kfree(p->queues);
  146. kfree(p);
  147. kfree(work);
  148. }
  149. static void kfd_process_destroy_delayed(struct rcu_head *rcu)
  150. {
  151. struct kfd_process_release_work *work;
  152. struct kfd_process *p;
  153. BUG_ON(!kfd_process_wq);
  154. p = container_of(rcu, struct kfd_process, rcu);
  155. BUG_ON(atomic_read(&p->mm->mm_count) <= 0);
  156. mmdrop(p->mm);
  157. work = kmalloc(sizeof(struct kfd_process_release_work), GFP_ATOMIC);
  158. if (work) {
  159. INIT_WORK((struct work_struct *) work, kfd_process_wq_release);
  160. work->p = p;
  161. queue_work(kfd_process_wq, (struct work_struct *) work);
  162. }
  163. }
  164. static void kfd_process_notifier_release(struct mmu_notifier *mn,
  165. struct mm_struct *mm)
  166. {
  167. struct kfd_process *p;
  168. struct kfd_process_device *pdd = NULL;
  169. /*
  170. * The kfd_process structure can not be free because the
  171. * mmu_notifier srcu is read locked
  172. */
  173. p = container_of(mn, struct kfd_process, mmu_notifier);
  174. BUG_ON(p->mm != mm);
  175. mutex_lock(&kfd_processes_mutex);
  176. hash_del_rcu(&p->kfd_processes);
  177. mutex_unlock(&kfd_processes_mutex);
  178. synchronize_srcu(&kfd_processes_srcu);
  179. mutex_lock(&p->mutex);
  180. /* In case our notifier is called before IOMMU notifier */
  181. pqm_uninit(&p->pqm);
  182. /* Iterate over all process device data structure and check
  183. * if we should delete debug managers and reset all wavefronts
  184. */
  185. list_for_each_entry(pdd, &p->per_device_data, per_device_list) {
  186. if ((pdd->dev->dbgmgr) &&
  187. (pdd->dev->dbgmgr->pasid == p->pasid))
  188. kfd_dbgmgr_destroy(pdd->dev->dbgmgr);
  189. if (pdd->reset_wavefronts) {
  190. pr_warn("amdkfd: Resetting all wave fronts\n");
  191. dbgdev_wave_reset_wavefronts(pdd->dev, p);
  192. pdd->reset_wavefronts = false;
  193. }
  194. }
  195. mutex_unlock(&p->mutex);
  196. /*
  197. * Because we drop mm_count inside kfd_process_destroy_delayed
  198. * and because the mmu_notifier_unregister function also drop
  199. * mm_count we need to take an extra count here.
  200. */
  201. atomic_inc(&p->mm->mm_count);
  202. mmu_notifier_unregister_no_release(&p->mmu_notifier, p->mm);
  203. mmu_notifier_call_srcu(&p->rcu, &kfd_process_destroy_delayed);
  204. }
  205. static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = {
  206. .release = kfd_process_notifier_release,
  207. };
  208. static struct kfd_process *create_process(const struct task_struct *thread)
  209. {
  210. struct kfd_process *process;
  211. int err = -ENOMEM;
  212. process = kzalloc(sizeof(*process), GFP_KERNEL);
  213. if (!process)
  214. goto err_alloc_process;
  215. process->queues = kmalloc_array(INITIAL_QUEUE_ARRAY_SIZE,
  216. sizeof(process->queues[0]), GFP_KERNEL);
  217. if (!process->queues)
  218. goto err_alloc_queues;
  219. process->pasid = kfd_pasid_alloc();
  220. if (process->pasid == 0)
  221. goto err_alloc_pasid;
  222. mutex_init(&process->mutex);
  223. process->mm = thread->mm;
  224. /* register notifier */
  225. process->mmu_notifier.ops = &kfd_process_mmu_notifier_ops;
  226. err = __mmu_notifier_register(&process->mmu_notifier, process->mm);
  227. if (err)
  228. goto err_mmu_notifier;
  229. hash_add_rcu(kfd_processes_table, &process->kfd_processes,
  230. (uintptr_t)process->mm);
  231. process->lead_thread = thread->group_leader;
  232. process->queue_array_size = INITIAL_QUEUE_ARRAY_SIZE;
  233. INIT_LIST_HEAD(&process->per_device_data);
  234. kfd_event_init_process(process);
  235. err = pqm_init(&process->pqm, process);
  236. if (err != 0)
  237. goto err_process_pqm_init;
  238. /* init process apertures*/
  239. process->is_32bit_user_mode = in_compat_syscall();
  240. if (kfd_init_apertures(process) != 0)
  241. goto err_init_apretures;
  242. return process;
  243. err_init_apretures:
  244. pqm_uninit(&process->pqm);
  245. err_process_pqm_init:
  246. hash_del_rcu(&process->kfd_processes);
  247. synchronize_rcu();
  248. mmu_notifier_unregister_no_release(&process->mmu_notifier, process->mm);
  249. err_mmu_notifier:
  250. mutex_destroy(&process->mutex);
  251. kfd_pasid_free(process->pasid);
  252. err_alloc_pasid:
  253. kfree(process->queues);
  254. err_alloc_queues:
  255. kfree(process);
  256. err_alloc_process:
  257. return ERR_PTR(err);
  258. }
  259. struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev,
  260. struct kfd_process *p)
  261. {
  262. struct kfd_process_device *pdd = NULL;
  263. list_for_each_entry(pdd, &p->per_device_data, per_device_list)
  264. if (pdd->dev == dev)
  265. break;
  266. return pdd;
  267. }
  268. struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev,
  269. struct kfd_process *p)
  270. {
  271. struct kfd_process_device *pdd = NULL;
  272. pdd = kzalloc(sizeof(*pdd), GFP_KERNEL);
  273. if (pdd != NULL) {
  274. pdd->dev = dev;
  275. INIT_LIST_HEAD(&pdd->qpd.queues_list);
  276. INIT_LIST_HEAD(&pdd->qpd.priv_queue_list);
  277. pdd->qpd.dqm = dev->dqm;
  278. pdd->reset_wavefronts = false;
  279. list_add(&pdd->per_device_list, &p->per_device_data);
  280. }
  281. return pdd;
  282. }
  283. /*
  284. * Direct the IOMMU to bind the process (specifically the pasid->mm)
  285. * to the device.
  286. * Unbinding occurs when the process dies or the device is removed.
  287. *
  288. * Assumes that the process lock is held.
  289. */
  290. struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev,
  291. struct kfd_process *p)
  292. {
  293. struct kfd_process_device *pdd;
  294. int err;
  295. pdd = kfd_get_process_device_data(dev, p);
  296. if (!pdd) {
  297. pr_err("Process device data doesn't exist\n");
  298. return ERR_PTR(-ENOMEM);
  299. }
  300. if (pdd->bound)
  301. return pdd;
  302. err = amd_iommu_bind_pasid(dev->pdev, p->pasid, p->lead_thread);
  303. if (err < 0)
  304. return ERR_PTR(err);
  305. pdd->bound = true;
  306. return pdd;
  307. }
  308. void kfd_unbind_process_from_device(struct kfd_dev *dev, unsigned int pasid)
  309. {
  310. struct kfd_process *p;
  311. struct kfd_process_device *pdd;
  312. BUG_ON(dev == NULL);
  313. /*
  314. * Look for the process that matches the pasid. If there is no such
  315. * process, we either released it in amdkfd's own notifier, or there
  316. * is a bug. Unfortunately, there is no way to tell...
  317. */
  318. p = kfd_lookup_process_by_pasid(pasid);
  319. if (!p)
  320. return;
  321. pr_debug("Unbinding process %d from IOMMU\n", pasid);
  322. if ((dev->dbgmgr) && (dev->dbgmgr->pasid == p->pasid))
  323. kfd_dbgmgr_destroy(dev->dbgmgr);
  324. pqm_uninit(&p->pqm);
  325. pdd = kfd_get_process_device_data(dev, p);
  326. if (!pdd) {
  327. mutex_unlock(&p->mutex);
  328. return;
  329. }
  330. if (pdd->reset_wavefronts) {
  331. dbgdev_wave_reset_wavefronts(pdd->dev, p);
  332. pdd->reset_wavefronts = false;
  333. }
  334. /*
  335. * Just mark pdd as unbound, because we still need it
  336. * to call amd_iommu_unbind_pasid() in when the
  337. * process exits.
  338. * We don't call amd_iommu_unbind_pasid() here
  339. * because the IOMMU called us.
  340. */
  341. pdd->bound = false;
  342. mutex_unlock(&p->mutex);
  343. }
  344. struct kfd_process_device *kfd_get_first_process_device_data(struct kfd_process *p)
  345. {
  346. return list_first_entry(&p->per_device_data,
  347. struct kfd_process_device,
  348. per_device_list);
  349. }
  350. struct kfd_process_device *kfd_get_next_process_device_data(struct kfd_process *p,
  351. struct kfd_process_device *pdd)
  352. {
  353. if (list_is_last(&pdd->per_device_list, &p->per_device_data))
  354. return NULL;
  355. return list_next_entry(pdd, per_device_list);
  356. }
  357. bool kfd_has_process_device_data(struct kfd_process *p)
  358. {
  359. return !(list_empty(&p->per_device_data));
  360. }
  361. /* This returns with process->mutex locked. */
  362. struct kfd_process *kfd_lookup_process_by_pasid(unsigned int pasid)
  363. {
  364. struct kfd_process *p;
  365. unsigned int temp;
  366. int idx = srcu_read_lock(&kfd_processes_srcu);
  367. hash_for_each_rcu(kfd_processes_table, temp, p, kfd_processes) {
  368. if (p->pasid == pasid) {
  369. mutex_lock(&p->mutex);
  370. break;
  371. }
  372. }
  373. srcu_read_unlock(&kfd_processes_srcu, idx);
  374. return p;
  375. }