amdgpu_mn.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. /**
  31. * DOC: MMU Notifier
  32. *
  33. * For coherent userptr handling registers an MMU notifier to inform the driver
  34. * about updates on the page tables of a process.
  35. *
  36. * When somebody tries to invalidate the page tables we block the update until
  37. * all operations on the pages in question are completed, then those pages are
  38. * marked as accessed and also dirty if it wasn't a read only access.
  39. *
  40. * New command submissions using the userptrs in question are delayed until all
  41. * page table invalidation are completed and we once more see a coherent process
  42. * address space.
  43. */
  44. #include <linux/firmware.h>
  45. #include <linux/module.h>
  46. #include <linux/mmu_notifier.h>
  47. #include <linux/interval_tree.h>
  48. #include <drm/drmP.h>
  49. #include <drm/drm.h>
  50. #include "amdgpu.h"
  51. #include "amdgpu_amdkfd.h"
  52. /**
  53. * struct amdgpu_mn
  54. *
  55. * @adev: amdgpu device pointer
  56. * @mm: process address space
  57. * @mn: MMU notifier structure
  58. * @type: type of MMU notifier
  59. * @work: destruction work item
  60. * @node: hash table node to find structure by adev and mn
  61. * @lock: rw semaphore protecting the notifier nodes
  62. * @objects: interval tree containing amdgpu_mn_nodes
  63. * @read_lock: mutex for recursive locking of @lock
  64. * @recursion: depth of recursion
  65. *
  66. * Data for each amdgpu device and process address space.
  67. */
  68. struct amdgpu_mn {
  69. /* constant after initialisation */
  70. struct amdgpu_device *adev;
  71. struct mm_struct *mm;
  72. struct mmu_notifier mn;
  73. enum amdgpu_mn_type type;
  74. /* only used on destruction */
  75. struct work_struct work;
  76. /* protected by adev->mn_lock */
  77. struct hlist_node node;
  78. /* objects protected by lock */
  79. struct rw_semaphore lock;
  80. struct rb_root_cached objects;
  81. struct mutex read_lock;
  82. atomic_t recursion;
  83. };
  84. /**
  85. * struct amdgpu_mn_node
  86. *
  87. * @it: interval node defining start-last of the affected address range
  88. * @bos: list of all BOs in the affected address range
  89. *
  90. * Manages all BOs which are affected of a certain range of address space.
  91. */
  92. struct amdgpu_mn_node {
  93. struct interval_tree_node it;
  94. struct list_head bos;
  95. };
  96. /**
  97. * amdgpu_mn_destroy - destroy the MMU notifier
  98. *
  99. * @work: previously sheduled work item
  100. *
  101. * Lazy destroys the notifier from a work item
  102. */
  103. static void amdgpu_mn_destroy(struct work_struct *work)
  104. {
  105. struct amdgpu_mn *amn = container_of(work, struct amdgpu_mn, work);
  106. struct amdgpu_device *adev = amn->adev;
  107. struct amdgpu_mn_node *node, *next_node;
  108. struct amdgpu_bo *bo, *next_bo;
  109. mutex_lock(&adev->mn_lock);
  110. down_write(&amn->lock);
  111. hash_del(&amn->node);
  112. rbtree_postorder_for_each_entry_safe(node, next_node,
  113. &amn->objects.rb_root, it.rb) {
  114. list_for_each_entry_safe(bo, next_bo, &node->bos, mn_list) {
  115. bo->mn = NULL;
  116. list_del_init(&bo->mn_list);
  117. }
  118. kfree(node);
  119. }
  120. up_write(&amn->lock);
  121. mutex_unlock(&adev->mn_lock);
  122. mmu_notifier_unregister_no_release(&amn->mn, amn->mm);
  123. kfree(amn);
  124. }
  125. /**
  126. * amdgpu_mn_release - callback to notify about mm destruction
  127. *
  128. * @mn: our notifier
  129. * @mm: the mm this callback is about
  130. *
  131. * Shedule a work item to lazy destroy our notifier.
  132. */
  133. static void amdgpu_mn_release(struct mmu_notifier *mn,
  134. struct mm_struct *mm)
  135. {
  136. struct amdgpu_mn *amn = container_of(mn, struct amdgpu_mn, mn);
  137. INIT_WORK(&amn->work, amdgpu_mn_destroy);
  138. schedule_work(&amn->work);
  139. }
  140. /**
  141. * amdgpu_mn_lock - take the write side lock for this notifier
  142. *
  143. * @mn: our notifier
  144. */
  145. void amdgpu_mn_lock(struct amdgpu_mn *mn)
  146. {
  147. if (mn)
  148. down_write(&mn->lock);
  149. }
  150. /**
  151. * amdgpu_mn_unlock - drop the write side lock for this notifier
  152. *
  153. * @mn: our notifier
  154. */
  155. void amdgpu_mn_unlock(struct amdgpu_mn *mn)
  156. {
  157. if (mn)
  158. up_write(&mn->lock);
  159. }
  160. /**
  161. * amdgpu_mn_read_lock - take the read side lock for this notifier
  162. *
  163. * @amn: our notifier
  164. */
  165. static void amdgpu_mn_read_lock(struct amdgpu_mn *amn)
  166. {
  167. mutex_lock(&amn->read_lock);
  168. if (atomic_inc_return(&amn->recursion) == 1)
  169. down_read_non_owner(&amn->lock);
  170. mutex_unlock(&amn->read_lock);
  171. }
  172. /**
  173. * amdgpu_mn_read_unlock - drop the read side lock for this notifier
  174. *
  175. * @amn: our notifier
  176. */
  177. static void amdgpu_mn_read_unlock(struct amdgpu_mn *amn)
  178. {
  179. if (atomic_dec_return(&amn->recursion) == 0)
  180. up_read_non_owner(&amn->lock);
  181. }
  182. /**
  183. * amdgpu_mn_invalidate_node - unmap all BOs of a node
  184. *
  185. * @node: the node with the BOs to unmap
  186. * @start: start of address range affected
  187. * @end: end of address range affected
  188. *
  189. * Block for operations on BOs to finish and mark pages as accessed and
  190. * potentially dirty.
  191. */
  192. static void amdgpu_mn_invalidate_node(struct amdgpu_mn_node *node,
  193. unsigned long start,
  194. unsigned long end)
  195. {
  196. struct amdgpu_bo *bo;
  197. long r;
  198. list_for_each_entry(bo, &node->bos, mn_list) {
  199. if (!amdgpu_ttm_tt_affect_userptr(bo->tbo.ttm, start, end))
  200. continue;
  201. r = reservation_object_wait_timeout_rcu(bo->tbo.resv,
  202. true, false, MAX_SCHEDULE_TIMEOUT);
  203. if (r <= 0)
  204. DRM_ERROR("(%ld) failed to wait for user bo\n", r);
  205. amdgpu_ttm_tt_mark_user_pages(bo->tbo.ttm);
  206. }
  207. }
  208. /**
  209. * amdgpu_mn_invalidate_range_start_gfx - callback to notify about mm change
  210. *
  211. * @mn: our notifier
  212. * @mm: the mm this callback is about
  213. * @start: start of updated range
  214. * @end: end of updated range
  215. *
  216. * Block for operations on BOs to finish and mark pages as accessed and
  217. * potentially dirty.
  218. */
  219. static void amdgpu_mn_invalidate_range_start_gfx(struct mmu_notifier *mn,
  220. struct mm_struct *mm,
  221. unsigned long start,
  222. unsigned long end)
  223. {
  224. struct amdgpu_mn *amn = container_of(mn, struct amdgpu_mn, mn);
  225. struct interval_tree_node *it;
  226. /* notification is exclusive, but interval is inclusive */
  227. end -= 1;
  228. amdgpu_mn_read_lock(amn);
  229. it = interval_tree_iter_first(&amn->objects, start, end);
  230. while (it) {
  231. struct amdgpu_mn_node *node;
  232. node = container_of(it, struct amdgpu_mn_node, it);
  233. it = interval_tree_iter_next(it, start, end);
  234. amdgpu_mn_invalidate_node(node, start, end);
  235. }
  236. }
  237. /**
  238. * amdgpu_mn_invalidate_range_start_hsa - callback to notify about mm change
  239. *
  240. * @mn: our notifier
  241. * @mm: the mm this callback is about
  242. * @start: start of updated range
  243. * @end: end of updated range
  244. *
  245. * We temporarily evict all BOs between start and end. This
  246. * necessitates evicting all user-mode queues of the process. The BOs
  247. * are restorted in amdgpu_mn_invalidate_range_end_hsa.
  248. */
  249. static void amdgpu_mn_invalidate_range_start_hsa(struct mmu_notifier *mn,
  250. struct mm_struct *mm,
  251. unsigned long start,
  252. unsigned long end)
  253. {
  254. struct amdgpu_mn *amn = container_of(mn, struct amdgpu_mn, mn);
  255. struct interval_tree_node *it;
  256. /* notification is exclusive, but interval is inclusive */
  257. end -= 1;
  258. amdgpu_mn_read_lock(amn);
  259. it = interval_tree_iter_first(&amn->objects, start, end);
  260. while (it) {
  261. struct amdgpu_mn_node *node;
  262. struct amdgpu_bo *bo;
  263. node = container_of(it, struct amdgpu_mn_node, it);
  264. it = interval_tree_iter_next(it, start, end);
  265. list_for_each_entry(bo, &node->bos, mn_list) {
  266. struct kgd_mem *mem = bo->kfd_bo;
  267. if (amdgpu_ttm_tt_affect_userptr(bo->tbo.ttm,
  268. start, end))
  269. amdgpu_amdkfd_evict_userptr(mem, mm);
  270. }
  271. }
  272. }
  273. /**
  274. * amdgpu_mn_invalidate_range_end - callback to notify about mm change
  275. *
  276. * @mn: our notifier
  277. * @mm: the mm this callback is about
  278. * @start: start of updated range
  279. * @end: end of updated range
  280. *
  281. * Release the lock again to allow new command submissions.
  282. */
  283. static void amdgpu_mn_invalidate_range_end(struct mmu_notifier *mn,
  284. struct mm_struct *mm,
  285. unsigned long start,
  286. unsigned long end)
  287. {
  288. struct amdgpu_mn *amn = container_of(mn, struct amdgpu_mn, mn);
  289. amdgpu_mn_read_unlock(amn);
  290. }
  291. static const struct mmu_notifier_ops amdgpu_mn_ops[] = {
  292. [AMDGPU_MN_TYPE_GFX] = {
  293. .release = amdgpu_mn_release,
  294. .invalidate_range_start = amdgpu_mn_invalidate_range_start_gfx,
  295. .invalidate_range_end = amdgpu_mn_invalidate_range_end,
  296. },
  297. [AMDGPU_MN_TYPE_HSA] = {
  298. .release = amdgpu_mn_release,
  299. .invalidate_range_start = amdgpu_mn_invalidate_range_start_hsa,
  300. .invalidate_range_end = amdgpu_mn_invalidate_range_end,
  301. },
  302. };
  303. /* Low bits of any reasonable mm pointer will be unused due to struct
  304. * alignment. Use these bits to make a unique key from the mm pointer
  305. * and notifier type.
  306. */
  307. #define AMDGPU_MN_KEY(mm, type) ((unsigned long)(mm) + (type))
  308. /**
  309. * amdgpu_mn_get - create notifier context
  310. *
  311. * @adev: amdgpu device pointer
  312. * @type: type of MMU notifier context
  313. *
  314. * Creates a notifier context for current->mm.
  315. */
  316. struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev,
  317. enum amdgpu_mn_type type)
  318. {
  319. struct mm_struct *mm = current->mm;
  320. struct amdgpu_mn *amn;
  321. unsigned long key = AMDGPU_MN_KEY(mm, type);
  322. int r;
  323. mutex_lock(&adev->mn_lock);
  324. if (down_write_killable(&mm->mmap_sem)) {
  325. mutex_unlock(&adev->mn_lock);
  326. return ERR_PTR(-EINTR);
  327. }
  328. hash_for_each_possible(adev->mn_hash, amn, node, key)
  329. if (AMDGPU_MN_KEY(amn->mm, amn->type) == key)
  330. goto release_locks;
  331. amn = kzalloc(sizeof(*amn), GFP_KERNEL);
  332. if (!amn) {
  333. amn = ERR_PTR(-ENOMEM);
  334. goto release_locks;
  335. }
  336. amn->adev = adev;
  337. amn->mm = mm;
  338. init_rwsem(&amn->lock);
  339. amn->type = type;
  340. amn->mn.ops = &amdgpu_mn_ops[type];
  341. amn->objects = RB_ROOT_CACHED;
  342. mutex_init(&amn->read_lock);
  343. atomic_set(&amn->recursion, 0);
  344. r = __mmu_notifier_register(&amn->mn, mm);
  345. if (r)
  346. goto free_amn;
  347. hash_add(adev->mn_hash, &amn->node, AMDGPU_MN_KEY(mm, type));
  348. release_locks:
  349. up_write(&mm->mmap_sem);
  350. mutex_unlock(&adev->mn_lock);
  351. return amn;
  352. free_amn:
  353. up_write(&mm->mmap_sem);
  354. mutex_unlock(&adev->mn_lock);
  355. kfree(amn);
  356. return ERR_PTR(r);
  357. }
  358. /**
  359. * amdgpu_mn_register - register a BO for notifier updates
  360. *
  361. * @bo: amdgpu buffer object
  362. * @addr: userptr addr we should monitor
  363. *
  364. * Registers an MMU notifier for the given BO at the specified address.
  365. * Returns 0 on success, -ERRNO if anything goes wrong.
  366. */
  367. int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr)
  368. {
  369. unsigned long end = addr + amdgpu_bo_size(bo) - 1;
  370. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  371. enum amdgpu_mn_type type =
  372. bo->kfd_bo ? AMDGPU_MN_TYPE_HSA : AMDGPU_MN_TYPE_GFX;
  373. struct amdgpu_mn *amn;
  374. struct amdgpu_mn_node *node = NULL, *new_node;
  375. struct list_head bos;
  376. struct interval_tree_node *it;
  377. amn = amdgpu_mn_get(adev, type);
  378. if (IS_ERR(amn))
  379. return PTR_ERR(amn);
  380. new_node = kmalloc(sizeof(*new_node), GFP_KERNEL);
  381. if (!new_node)
  382. return -ENOMEM;
  383. INIT_LIST_HEAD(&bos);
  384. down_write(&amn->lock);
  385. while ((it = interval_tree_iter_first(&amn->objects, addr, end))) {
  386. kfree(node);
  387. node = container_of(it, struct amdgpu_mn_node, it);
  388. interval_tree_remove(&node->it, &amn->objects);
  389. addr = min(it->start, addr);
  390. end = max(it->last, end);
  391. list_splice(&node->bos, &bos);
  392. }
  393. if (!node)
  394. node = new_node;
  395. else
  396. kfree(new_node);
  397. bo->mn = amn;
  398. node->it.start = addr;
  399. node->it.last = end;
  400. INIT_LIST_HEAD(&node->bos);
  401. list_splice(&bos, &node->bos);
  402. list_add(&bo->mn_list, &node->bos);
  403. interval_tree_insert(&node->it, &amn->objects);
  404. up_write(&amn->lock);
  405. return 0;
  406. }
  407. /**
  408. * amdgpu_mn_unregister - unregister a BO for notifier updates
  409. *
  410. * @bo: amdgpu buffer object
  411. *
  412. * Remove any registration of MMU notifier updates from the buffer object.
  413. */
  414. void amdgpu_mn_unregister(struct amdgpu_bo *bo)
  415. {
  416. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  417. struct amdgpu_mn *amn;
  418. struct list_head *head;
  419. mutex_lock(&adev->mn_lock);
  420. amn = bo->mn;
  421. if (amn == NULL) {
  422. mutex_unlock(&adev->mn_lock);
  423. return;
  424. }
  425. down_write(&amn->lock);
  426. /* save the next list entry for later */
  427. head = bo->mn_list.next;
  428. bo->mn = NULL;
  429. list_del_init(&bo->mn_list);
  430. if (list_empty(head)) {
  431. struct amdgpu_mn_node *node;
  432. node = container_of(head, struct amdgpu_mn_node, bos);
  433. interval_tree_remove(&node->it, &amn->objects);
  434. kfree(node);
  435. }
  436. up_write(&amn->lock);
  437. mutex_unlock(&adev->mn_lock);
  438. }