amdgpu_mn.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*
  2. * Copyright 2014 Advanced Micro Devices, Inc.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sub license, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  16. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  17. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  18. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  19. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. *
  21. * The above copyright notice and this permission notice (including the
  22. * next paragraph) shall be included in all copies or substantial portions
  23. * of the Software.
  24. *
  25. */
  26. /*
  27. * Authors:
  28. * Christian König <christian.koenig@amd.com>
  29. */
  30. #include <linux/firmware.h>
  31. #include <linux/module.h>
  32. #include <linux/mmu_notifier.h>
  33. #include <linux/interval_tree.h>
  34. #include <drm/drmP.h>
  35. #include <drm/drm.h>
  36. #include "amdgpu.h"
  37. #include "amdgpu_amdkfd.h"
  38. struct amdgpu_mn {
  39. /* constant after initialisation */
  40. struct amdgpu_device *adev;
  41. struct mm_struct *mm;
  42. struct mmu_notifier mn;
  43. enum amdgpu_mn_type type;
  44. /* only used on destruction */
  45. struct work_struct work;
  46. /* protected by adev->mn_lock */
  47. struct hlist_node node;
  48. /* objects protected by lock */
  49. struct rw_semaphore lock;
  50. struct rb_root_cached objects;
  51. struct mutex read_lock;
  52. atomic_t recursion;
  53. };
  54. struct amdgpu_mn_node {
  55. struct interval_tree_node it;
  56. struct list_head bos;
  57. };
  58. /**
  59. * amdgpu_mn_destroy - destroy the rmn
  60. *
  61. * @work: previously sheduled work item
  62. *
  63. * Lazy destroys the notifier from a work item
  64. */
  65. static void amdgpu_mn_destroy(struct work_struct *work)
  66. {
  67. struct amdgpu_mn *rmn = container_of(work, struct amdgpu_mn, work);
  68. struct amdgpu_device *adev = rmn->adev;
  69. struct amdgpu_mn_node *node, *next_node;
  70. struct amdgpu_bo *bo, *next_bo;
  71. mutex_lock(&adev->mn_lock);
  72. down_write(&rmn->lock);
  73. hash_del(&rmn->node);
  74. rbtree_postorder_for_each_entry_safe(node, next_node,
  75. &rmn->objects.rb_root, it.rb) {
  76. list_for_each_entry_safe(bo, next_bo, &node->bos, mn_list) {
  77. bo->mn = NULL;
  78. list_del_init(&bo->mn_list);
  79. }
  80. kfree(node);
  81. }
  82. up_write(&rmn->lock);
  83. mutex_unlock(&adev->mn_lock);
  84. mmu_notifier_unregister_no_release(&rmn->mn, rmn->mm);
  85. kfree(rmn);
  86. }
  87. /**
  88. * amdgpu_mn_release - callback to notify about mm destruction
  89. *
  90. * @mn: our notifier
  91. * @mn: the mm this callback is about
  92. *
  93. * Shedule a work item to lazy destroy our notifier.
  94. */
  95. static void amdgpu_mn_release(struct mmu_notifier *mn,
  96. struct mm_struct *mm)
  97. {
  98. struct amdgpu_mn *rmn = container_of(mn, struct amdgpu_mn, mn);
  99. INIT_WORK(&rmn->work, amdgpu_mn_destroy);
  100. schedule_work(&rmn->work);
  101. }
  102. /**
  103. * amdgpu_mn_lock - take the write side lock for this mn
  104. */
  105. void amdgpu_mn_lock(struct amdgpu_mn *mn)
  106. {
  107. if (mn)
  108. down_write(&mn->lock);
  109. }
  110. /**
  111. * amdgpu_mn_unlock - drop the write side lock for this mn
  112. */
  113. void amdgpu_mn_unlock(struct amdgpu_mn *mn)
  114. {
  115. if (mn)
  116. up_write(&mn->lock);
  117. }
  118. /**
  119. * amdgpu_mn_read_lock - take the rmn read lock
  120. *
  121. * @rmn: our notifier
  122. *
  123. * Take the rmn read side lock.
  124. */
  125. static void amdgpu_mn_read_lock(struct amdgpu_mn *rmn)
  126. {
  127. mutex_lock(&rmn->read_lock);
  128. if (atomic_inc_return(&rmn->recursion) == 1)
  129. down_read_non_owner(&rmn->lock);
  130. mutex_unlock(&rmn->read_lock);
  131. }
  132. /**
  133. * amdgpu_mn_read_unlock - drop the rmn read lock
  134. *
  135. * @rmn: our notifier
  136. *
  137. * Drop the rmn read side lock.
  138. */
  139. static void amdgpu_mn_read_unlock(struct amdgpu_mn *rmn)
  140. {
  141. if (atomic_dec_return(&rmn->recursion) == 0)
  142. up_read_non_owner(&rmn->lock);
  143. }
  144. /**
  145. * amdgpu_mn_invalidate_node - unmap all BOs of a node
  146. *
  147. * @node: the node with the BOs to unmap
  148. *
  149. * We block for all BOs and unmap them by move them
  150. * into system domain again.
  151. */
  152. static void amdgpu_mn_invalidate_node(struct amdgpu_mn_node *node,
  153. unsigned long start,
  154. unsigned long end)
  155. {
  156. struct amdgpu_bo *bo;
  157. long r;
  158. list_for_each_entry(bo, &node->bos, mn_list) {
  159. if (!amdgpu_ttm_tt_affect_userptr(bo->tbo.ttm, start, end))
  160. continue;
  161. r = reservation_object_wait_timeout_rcu(bo->tbo.resv,
  162. true, false, MAX_SCHEDULE_TIMEOUT);
  163. if (r <= 0)
  164. DRM_ERROR("(%ld) failed to wait for user bo\n", r);
  165. amdgpu_ttm_tt_mark_user_pages(bo->tbo.ttm);
  166. }
  167. }
  168. /**
  169. * amdgpu_mn_invalidate_range_start_gfx - callback to notify about mm change
  170. *
  171. * @mn: our notifier
  172. * @mn: the mm this callback is about
  173. * @start: start of updated range
  174. * @end: end of updated range
  175. *
  176. * We block for all BOs between start and end to be idle and
  177. * unmap them by move them into system domain again.
  178. */
  179. static void amdgpu_mn_invalidate_range_start_gfx(struct mmu_notifier *mn,
  180. struct mm_struct *mm,
  181. unsigned long start,
  182. unsigned long end)
  183. {
  184. struct amdgpu_mn *rmn = container_of(mn, struct amdgpu_mn, mn);
  185. struct interval_tree_node *it;
  186. /* notification is exclusive, but interval is inclusive */
  187. end -= 1;
  188. amdgpu_mn_read_lock(rmn);
  189. it = interval_tree_iter_first(&rmn->objects, start, end);
  190. while (it) {
  191. struct amdgpu_mn_node *node;
  192. node = container_of(it, struct amdgpu_mn_node, it);
  193. it = interval_tree_iter_next(it, start, end);
  194. amdgpu_mn_invalidate_node(node, start, end);
  195. }
  196. }
  197. /**
  198. * amdgpu_mn_invalidate_range_start_hsa - callback to notify about mm change
  199. *
  200. * @mn: our notifier
  201. * @mn: the mm this callback is about
  202. * @start: start of updated range
  203. * @end: end of updated range
  204. *
  205. * We temporarily evict all BOs between start and end. This
  206. * necessitates evicting all user-mode queues of the process. The BOs
  207. * are restorted in amdgpu_mn_invalidate_range_end_hsa.
  208. */
  209. static void amdgpu_mn_invalidate_range_start_hsa(struct mmu_notifier *mn,
  210. struct mm_struct *mm,
  211. unsigned long start,
  212. unsigned long end)
  213. {
  214. struct amdgpu_mn *rmn = container_of(mn, struct amdgpu_mn, mn);
  215. struct interval_tree_node *it;
  216. /* notification is exclusive, but interval is inclusive */
  217. end -= 1;
  218. amdgpu_mn_read_lock(rmn);
  219. it = interval_tree_iter_first(&rmn->objects, start, end);
  220. while (it) {
  221. struct amdgpu_mn_node *node;
  222. struct amdgpu_bo *bo;
  223. node = container_of(it, struct amdgpu_mn_node, it);
  224. it = interval_tree_iter_next(it, start, end);
  225. list_for_each_entry(bo, &node->bos, mn_list) {
  226. struct kgd_mem *mem = bo->kfd_bo;
  227. if (amdgpu_ttm_tt_affect_userptr(bo->tbo.ttm,
  228. start, end))
  229. amdgpu_amdkfd_evict_userptr(mem, mm);
  230. }
  231. }
  232. }
  233. /**
  234. * amdgpu_mn_invalidate_range_end - callback to notify about mm change
  235. *
  236. * @mn: our notifier
  237. * @mn: the mm this callback is about
  238. * @start: start of updated range
  239. * @end: end of updated range
  240. *
  241. * Release the lock again to allow new command submissions.
  242. */
  243. static void amdgpu_mn_invalidate_range_end(struct mmu_notifier *mn,
  244. struct mm_struct *mm,
  245. unsigned long start,
  246. unsigned long end)
  247. {
  248. struct amdgpu_mn *rmn = container_of(mn, struct amdgpu_mn, mn);
  249. amdgpu_mn_read_unlock(rmn);
  250. }
  251. static const struct mmu_notifier_ops amdgpu_mn_ops[] = {
  252. [AMDGPU_MN_TYPE_GFX] = {
  253. .release = amdgpu_mn_release,
  254. .invalidate_range_start = amdgpu_mn_invalidate_range_start_gfx,
  255. .invalidate_range_end = amdgpu_mn_invalidate_range_end,
  256. },
  257. [AMDGPU_MN_TYPE_HSA] = {
  258. .release = amdgpu_mn_release,
  259. .invalidate_range_start = amdgpu_mn_invalidate_range_start_hsa,
  260. .invalidate_range_end = amdgpu_mn_invalidate_range_end,
  261. },
  262. };
  263. /* Low bits of any reasonable mm pointer will be unused due to struct
  264. * alignment. Use these bits to make a unique key from the mm pointer
  265. * and notifier type.
  266. */
  267. #define AMDGPU_MN_KEY(mm, type) ((unsigned long)(mm) + (type))
  268. /**
  269. * amdgpu_mn_get - create notifier context
  270. *
  271. * @adev: amdgpu device pointer
  272. * @type: type of MMU notifier context
  273. *
  274. * Creates a notifier context for current->mm.
  275. */
  276. struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev,
  277. enum amdgpu_mn_type type)
  278. {
  279. struct mm_struct *mm = current->mm;
  280. struct amdgpu_mn *rmn;
  281. unsigned long key = AMDGPU_MN_KEY(mm, type);
  282. int r;
  283. mutex_lock(&adev->mn_lock);
  284. if (down_write_killable(&mm->mmap_sem)) {
  285. mutex_unlock(&adev->mn_lock);
  286. return ERR_PTR(-EINTR);
  287. }
  288. hash_for_each_possible(adev->mn_hash, rmn, node, key)
  289. if (AMDGPU_MN_KEY(rmn->mm, rmn->type) == key)
  290. goto release_locks;
  291. rmn = kzalloc(sizeof(*rmn), GFP_KERNEL);
  292. if (!rmn) {
  293. rmn = ERR_PTR(-ENOMEM);
  294. goto release_locks;
  295. }
  296. rmn->adev = adev;
  297. rmn->mm = mm;
  298. init_rwsem(&rmn->lock);
  299. rmn->type = type;
  300. rmn->mn.ops = &amdgpu_mn_ops[type];
  301. rmn->objects = RB_ROOT_CACHED;
  302. mutex_init(&rmn->read_lock);
  303. atomic_set(&rmn->recursion, 0);
  304. r = __mmu_notifier_register(&rmn->mn, mm);
  305. if (r)
  306. goto free_rmn;
  307. hash_add(adev->mn_hash, &rmn->node, AMDGPU_MN_KEY(mm, type));
  308. release_locks:
  309. up_write(&mm->mmap_sem);
  310. mutex_unlock(&adev->mn_lock);
  311. return rmn;
  312. free_rmn:
  313. up_write(&mm->mmap_sem);
  314. mutex_unlock(&adev->mn_lock);
  315. kfree(rmn);
  316. return ERR_PTR(r);
  317. }
  318. /**
  319. * amdgpu_mn_register - register a BO for notifier updates
  320. *
  321. * @bo: amdgpu buffer object
  322. * @addr: userptr addr we should monitor
  323. *
  324. * Registers an MMU notifier for the given BO at the specified address.
  325. * Returns 0 on success, -ERRNO if anything goes wrong.
  326. */
  327. int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr)
  328. {
  329. unsigned long end = addr + amdgpu_bo_size(bo) - 1;
  330. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  331. enum amdgpu_mn_type type =
  332. bo->kfd_bo ? AMDGPU_MN_TYPE_HSA : AMDGPU_MN_TYPE_GFX;
  333. struct amdgpu_mn *rmn;
  334. struct amdgpu_mn_node *node = NULL, *new_node;
  335. struct list_head bos;
  336. struct interval_tree_node *it;
  337. rmn = amdgpu_mn_get(adev, type);
  338. if (IS_ERR(rmn))
  339. return PTR_ERR(rmn);
  340. new_node = kmalloc(sizeof(*new_node), GFP_KERNEL);
  341. if (!new_node)
  342. return -ENOMEM;
  343. INIT_LIST_HEAD(&bos);
  344. down_write(&rmn->lock);
  345. while ((it = interval_tree_iter_first(&rmn->objects, addr, end))) {
  346. kfree(node);
  347. node = container_of(it, struct amdgpu_mn_node, it);
  348. interval_tree_remove(&node->it, &rmn->objects);
  349. addr = min(it->start, addr);
  350. end = max(it->last, end);
  351. list_splice(&node->bos, &bos);
  352. }
  353. if (!node)
  354. node = new_node;
  355. else
  356. kfree(new_node);
  357. bo->mn = rmn;
  358. node->it.start = addr;
  359. node->it.last = end;
  360. INIT_LIST_HEAD(&node->bos);
  361. list_splice(&bos, &node->bos);
  362. list_add(&bo->mn_list, &node->bos);
  363. interval_tree_insert(&node->it, &rmn->objects);
  364. up_write(&rmn->lock);
  365. return 0;
  366. }
  367. /**
  368. * amdgpu_mn_unregister - unregister a BO for notifier updates
  369. *
  370. * @bo: amdgpu buffer object
  371. *
  372. * Remove any registration of MMU notifier updates from the buffer object.
  373. */
  374. void amdgpu_mn_unregister(struct amdgpu_bo *bo)
  375. {
  376. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  377. struct amdgpu_mn *rmn;
  378. struct list_head *head;
  379. mutex_lock(&adev->mn_lock);
  380. rmn = bo->mn;
  381. if (rmn == NULL) {
  382. mutex_unlock(&adev->mn_lock);
  383. return;
  384. }
  385. down_write(&rmn->lock);
  386. /* save the next list entry for later */
  387. head = bo->mn_list.next;
  388. bo->mn = NULL;
  389. list_del_init(&bo->mn_list);
  390. if (list_empty(head)) {
  391. struct amdgpu_mn_node *node;
  392. node = container_of(head, struct amdgpu_mn_node, bos);
  393. interval_tree_remove(&node->it, &rmn->objects);
  394. kfree(node);
  395. }
  396. up_write(&rmn->lock);
  397. mutex_unlock(&adev->mn_lock);
  398. }