kfd_process.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. /*
  32. * Initial size for the array of queues.
  33. * The allocated size is doubled each time
  34. * it is exceeded up to MAX_PROCESS_QUEUES.
  35. */
  36. #define INITIAL_QUEUE_ARRAY_SIZE 16
  37. /*
  38. * List of struct kfd_process (field kfd_process).
  39. * Unique/indexed by mm_struct*
  40. */
  41. #define KFD_PROCESS_TABLE_SIZE 5 /* bits: 32 entries */
  42. static DEFINE_HASHTABLE(kfd_processes_table, KFD_PROCESS_TABLE_SIZE);
  43. static DEFINE_MUTEX(kfd_processes_mutex);
  44. DEFINE_STATIC_SRCU(kfd_processes_srcu);
  45. static struct workqueue_struct *kfd_process_wq;
  46. struct kfd_process_release_work {
  47. struct work_struct kfd_work;
  48. struct kfd_process *p;
  49. };
  50. static struct kfd_process *find_process(const struct task_struct *thread);
  51. static struct kfd_process *create_process(const struct task_struct *thread);
  52. void kfd_process_create_wq(void)
  53. {
  54. if (!kfd_process_wq)
  55. kfd_process_wq = create_workqueue("kfd_process_wq");
  56. }
  57. void kfd_process_destroy_wq(void)
  58. {
  59. if (kfd_process_wq) {
  60. flush_workqueue(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. amd_iommu_unbind_pasid(pdd->dev->pdev, p->pasid);
  136. list_del(&pdd->per_device_list);
  137. kfree(pdd);
  138. }
  139. kfd_pasid_free(p->pasid);
  140. mutex_unlock(&p->mutex);
  141. mutex_destroy(&p->mutex);
  142. kfree(p->queues);
  143. kfree(p);
  144. kfree((void *)work);
  145. }
  146. static void kfd_process_destroy_delayed(struct rcu_head *rcu)
  147. {
  148. struct kfd_process_release_work *work;
  149. struct kfd_process *p;
  150. BUG_ON(!kfd_process_wq);
  151. p = container_of(rcu, struct kfd_process, rcu);
  152. BUG_ON(atomic_read(&p->mm->mm_count) <= 0);
  153. mmdrop(p->mm);
  154. work = (struct kfd_process_release_work *)
  155. kmalloc(sizeof(struct kfd_process_release_work), GFP_ATOMIC);
  156. if (work) {
  157. INIT_WORK((struct work_struct *) work, kfd_process_wq_release);
  158. work->p = p;
  159. queue_work(kfd_process_wq, (struct work_struct *) work);
  160. }
  161. }
  162. static void kfd_process_notifier_release(struct mmu_notifier *mn,
  163. struct mm_struct *mm)
  164. {
  165. struct kfd_process *p;
  166. /*
  167. * The kfd_process structure can not be free because the
  168. * mmu_notifier srcu is read locked
  169. */
  170. p = container_of(mn, struct kfd_process, mmu_notifier);
  171. BUG_ON(p->mm != mm);
  172. mutex_lock(&kfd_processes_mutex);
  173. hash_del_rcu(&p->kfd_processes);
  174. mutex_unlock(&kfd_processes_mutex);
  175. synchronize_srcu(&kfd_processes_srcu);
  176. mutex_lock(&p->mutex);
  177. /* In case our notifier is called before IOMMU notifier */
  178. pqm_uninit(&p->pqm);
  179. mutex_unlock(&p->mutex);
  180. /*
  181. * Because we drop mm_count inside kfd_process_destroy_delayed
  182. * and because the mmu_notifier_unregister function also drop
  183. * mm_count we need to take an extra count here.
  184. */
  185. atomic_inc(&p->mm->mm_count);
  186. mmu_notifier_unregister_no_release(&p->mmu_notifier, p->mm);
  187. mmu_notifier_call_srcu(&p->rcu, &kfd_process_destroy_delayed);
  188. }
  189. static const struct mmu_notifier_ops kfd_process_mmu_notifier_ops = {
  190. .release = kfd_process_notifier_release,
  191. };
  192. static struct kfd_process *create_process(const struct task_struct *thread)
  193. {
  194. struct kfd_process *process;
  195. int err = -ENOMEM;
  196. process = kzalloc(sizeof(*process), GFP_KERNEL);
  197. if (!process)
  198. goto err_alloc_process;
  199. process->queues = kmalloc_array(INITIAL_QUEUE_ARRAY_SIZE,
  200. sizeof(process->queues[0]), GFP_KERNEL);
  201. if (!process->queues)
  202. goto err_alloc_queues;
  203. process->pasid = kfd_pasid_alloc();
  204. if (process->pasid == 0)
  205. goto err_alloc_pasid;
  206. mutex_init(&process->mutex);
  207. process->mm = thread->mm;
  208. /* register notifier */
  209. process->mmu_notifier.ops = &kfd_process_mmu_notifier_ops;
  210. err = __mmu_notifier_register(&process->mmu_notifier, process->mm);
  211. if (err)
  212. goto err_mmu_notifier;
  213. hash_add_rcu(kfd_processes_table, &process->kfd_processes,
  214. (uintptr_t)process->mm);
  215. process->lead_thread = thread->group_leader;
  216. process->queue_array_size = INITIAL_QUEUE_ARRAY_SIZE;
  217. INIT_LIST_HEAD(&process->per_device_data);
  218. err = pqm_init(&process->pqm, process);
  219. if (err != 0)
  220. goto err_process_pqm_init;
  221. /* init process apertures*/
  222. process->is_32bit_user_mode = is_compat_task();
  223. if (kfd_init_apertures(process) != 0)
  224. goto err_init_apretures;
  225. return process;
  226. err_init_apretures:
  227. pqm_uninit(&process->pqm);
  228. err_process_pqm_init:
  229. hash_del_rcu(&process->kfd_processes);
  230. synchronize_rcu();
  231. mmu_notifier_unregister_no_release(&process->mmu_notifier, process->mm);
  232. err_mmu_notifier:
  233. kfd_pasid_free(process->pasid);
  234. err_alloc_pasid:
  235. kfree(process->queues);
  236. err_alloc_queues:
  237. kfree(process);
  238. err_alloc_process:
  239. return ERR_PTR(err);
  240. }
  241. struct kfd_process_device *kfd_get_process_device_data(struct kfd_dev *dev,
  242. struct kfd_process *p)
  243. {
  244. struct kfd_process_device *pdd = NULL;
  245. list_for_each_entry(pdd, &p->per_device_data, per_device_list)
  246. if (pdd->dev == dev)
  247. break;
  248. return pdd;
  249. }
  250. struct kfd_process_device *kfd_create_process_device_data(struct kfd_dev *dev,
  251. struct kfd_process *p)
  252. {
  253. struct kfd_process_device *pdd = NULL;
  254. pdd = kzalloc(sizeof(*pdd), GFP_KERNEL);
  255. if (pdd != NULL) {
  256. pdd->dev = dev;
  257. INIT_LIST_HEAD(&pdd->qpd.queues_list);
  258. INIT_LIST_HEAD(&pdd->qpd.priv_queue_list);
  259. pdd->qpd.dqm = dev->dqm;
  260. list_add(&pdd->per_device_list, &p->per_device_data);
  261. }
  262. return pdd;
  263. }
  264. /*
  265. * Direct the IOMMU to bind the process (specifically the pasid->mm)
  266. * to the device.
  267. * Unbinding occurs when the process dies or the device is removed.
  268. *
  269. * Assumes that the process lock is held.
  270. */
  271. struct kfd_process_device *kfd_bind_process_to_device(struct kfd_dev *dev,
  272. struct kfd_process *p)
  273. {
  274. struct kfd_process_device *pdd;
  275. int err;
  276. pdd = kfd_get_process_device_data(dev, p);
  277. if (!pdd) {
  278. pr_err("Process device data doesn't exist\n");
  279. return ERR_PTR(-ENOMEM);
  280. }
  281. if (pdd->bound)
  282. return pdd;
  283. err = amd_iommu_bind_pasid(dev->pdev, p->pasid, p->lead_thread);
  284. if (err < 0)
  285. return ERR_PTR(err);
  286. pdd->bound = true;
  287. return pdd;
  288. }
  289. void kfd_unbind_process_from_device(struct kfd_dev *dev, unsigned int pasid)
  290. {
  291. struct kfd_process *p;
  292. struct kfd_process_device *pdd;
  293. int idx, i;
  294. BUG_ON(dev == NULL);
  295. idx = srcu_read_lock(&kfd_processes_srcu);
  296. hash_for_each_rcu(kfd_processes_table, i, p, kfd_processes)
  297. if (p->pasid == pasid)
  298. break;
  299. srcu_read_unlock(&kfd_processes_srcu, idx);
  300. BUG_ON(p->pasid != pasid);
  301. mutex_lock(&p->mutex);
  302. pqm_uninit(&p->pqm);
  303. pdd = kfd_get_process_device_data(dev, p);
  304. /*
  305. * Just mark pdd as unbound, because we still need it to call
  306. * amd_iommu_unbind_pasid() in when the process exits.
  307. * We don't call amd_iommu_unbind_pasid() here
  308. * because the IOMMU called us.
  309. */
  310. if (pdd)
  311. pdd->bound = false;
  312. mutex_unlock(&p->mutex);
  313. }
  314. struct kfd_process_device *kfd_get_first_process_device_data(struct kfd_process *p)
  315. {
  316. return list_first_entry(&p->per_device_data,
  317. struct kfd_process_device,
  318. per_device_list);
  319. }
  320. struct kfd_process_device *kfd_get_next_process_device_data(struct kfd_process *p,
  321. struct kfd_process_device *pdd)
  322. {
  323. if (list_is_last(&pdd->per_device_list, &p->per_device_data))
  324. return NULL;
  325. return list_next_entry(pdd, per_device_list);
  326. }
  327. bool kfd_has_process_device_data(struct kfd_process *p)
  328. {
  329. return !(list_empty(&p->per_device_data));
  330. }