kfd_process.c 10.0 KB

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