amdgpu_mn.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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 int amdgpu_mn_read_lock(struct amdgpu_mn *amn, bool blockable)
  166. {
  167. if (blockable)
  168. mutex_lock(&amn->read_lock);
  169. else if (!mutex_trylock(&amn->read_lock))
  170. return -EAGAIN;
  171. if (atomic_inc_return(&amn->recursion) == 1)
  172. down_read_non_owner(&amn->lock);
  173. mutex_unlock(&amn->read_lock);
  174. return 0;
  175. }
  176. /**
  177. * amdgpu_mn_read_unlock - drop the read side lock for this notifier
  178. *
  179. * @amn: our notifier
  180. */
  181. static void amdgpu_mn_read_unlock(struct amdgpu_mn *amn)
  182. {
  183. if (atomic_dec_return(&amn->recursion) == 0)
  184. up_read_non_owner(&amn->lock);
  185. }
  186. /**
  187. * amdgpu_mn_invalidate_node - unmap all BOs of a node
  188. *
  189. * @node: the node with the BOs to unmap
  190. * @start: start of address range affected
  191. * @end: end of address range affected
  192. *
  193. * Block for operations on BOs to finish and mark pages as accessed and
  194. * potentially dirty.
  195. */
  196. static void amdgpu_mn_invalidate_node(struct amdgpu_mn_node *node,
  197. unsigned long start,
  198. unsigned long end)
  199. {
  200. struct amdgpu_bo *bo;
  201. long r;
  202. list_for_each_entry(bo, &node->bos, mn_list) {
  203. if (!amdgpu_ttm_tt_affect_userptr(bo->tbo.ttm, start, end))
  204. continue;
  205. r = reservation_object_wait_timeout_rcu(bo->tbo.resv,
  206. true, false, MAX_SCHEDULE_TIMEOUT);
  207. if (r <= 0)
  208. DRM_ERROR("(%ld) failed to wait for user bo\n", r);
  209. amdgpu_ttm_tt_mark_user_pages(bo->tbo.ttm);
  210. }
  211. }
  212. /**
  213. * amdgpu_mn_invalidate_range_start_gfx - callback to notify about mm change
  214. *
  215. * @mn: our notifier
  216. * @mm: the mm this callback is about
  217. * @start: start of updated range
  218. * @end: end of updated range
  219. *
  220. * Block for operations on BOs to finish and mark pages as accessed and
  221. * potentially dirty.
  222. */
  223. static int amdgpu_mn_invalidate_range_start_gfx(struct mmu_notifier *mn,
  224. struct mm_struct *mm,
  225. unsigned long start,
  226. unsigned long end,
  227. bool blockable)
  228. {
  229. struct amdgpu_mn *amn = container_of(mn, struct amdgpu_mn, mn);
  230. struct interval_tree_node *it;
  231. /* notification is exclusive, but interval is inclusive */
  232. end -= 1;
  233. /* TODO we should be able to split locking for interval tree and
  234. * amdgpu_mn_invalidate_node
  235. */
  236. if (amdgpu_mn_read_lock(amn, blockable))
  237. return -EAGAIN;
  238. it = interval_tree_iter_first(&amn->objects, start, end);
  239. while (it) {
  240. struct amdgpu_mn_node *node;
  241. if (!blockable) {
  242. amdgpu_mn_read_unlock(amn);
  243. return -EAGAIN;
  244. }
  245. node = container_of(it, struct amdgpu_mn_node, it);
  246. it = interval_tree_iter_next(it, start, end);
  247. amdgpu_mn_invalidate_node(node, start, end);
  248. }
  249. return 0;
  250. }
  251. /**
  252. * amdgpu_mn_invalidate_range_start_hsa - callback to notify about mm change
  253. *
  254. * @mn: our notifier
  255. * @mm: the mm this callback is about
  256. * @start: start of updated range
  257. * @end: end of updated range
  258. *
  259. * We temporarily evict all BOs between start and end. This
  260. * necessitates evicting all user-mode queues of the process. The BOs
  261. * are restorted in amdgpu_mn_invalidate_range_end_hsa.
  262. */
  263. static int amdgpu_mn_invalidate_range_start_hsa(struct mmu_notifier *mn,
  264. struct mm_struct *mm,
  265. unsigned long start,
  266. unsigned long end,
  267. bool blockable)
  268. {
  269. struct amdgpu_mn *amn = container_of(mn, struct amdgpu_mn, mn);
  270. struct interval_tree_node *it;
  271. /* notification is exclusive, but interval is inclusive */
  272. end -= 1;
  273. if (amdgpu_mn_read_lock(amn, blockable))
  274. return -EAGAIN;
  275. it = interval_tree_iter_first(&amn->objects, start, end);
  276. while (it) {
  277. struct amdgpu_mn_node *node;
  278. struct amdgpu_bo *bo;
  279. if (!blockable) {
  280. amdgpu_mn_read_unlock(amn);
  281. return -EAGAIN;
  282. }
  283. node = container_of(it, struct amdgpu_mn_node, it);
  284. it = interval_tree_iter_next(it, start, end);
  285. list_for_each_entry(bo, &node->bos, mn_list) {
  286. struct kgd_mem *mem = bo->kfd_bo;
  287. if (amdgpu_ttm_tt_affect_userptr(bo->tbo.ttm,
  288. start, end))
  289. amdgpu_amdkfd_evict_userptr(mem, mm);
  290. }
  291. }
  292. return 0;
  293. }
  294. /**
  295. * amdgpu_mn_invalidate_range_end - callback to notify about mm change
  296. *
  297. * @mn: our notifier
  298. * @mm: the mm this callback is about
  299. * @start: start of updated range
  300. * @end: end of updated range
  301. *
  302. * Release the lock again to allow new command submissions.
  303. */
  304. static void amdgpu_mn_invalidate_range_end(struct mmu_notifier *mn,
  305. struct mm_struct *mm,
  306. unsigned long start,
  307. unsigned long end)
  308. {
  309. struct amdgpu_mn *amn = container_of(mn, struct amdgpu_mn, mn);
  310. amdgpu_mn_read_unlock(amn);
  311. }
  312. static const struct mmu_notifier_ops amdgpu_mn_ops[] = {
  313. [AMDGPU_MN_TYPE_GFX] = {
  314. .release = amdgpu_mn_release,
  315. .invalidate_range_start = amdgpu_mn_invalidate_range_start_gfx,
  316. .invalidate_range_end = amdgpu_mn_invalidate_range_end,
  317. },
  318. [AMDGPU_MN_TYPE_HSA] = {
  319. .release = amdgpu_mn_release,
  320. .invalidate_range_start = amdgpu_mn_invalidate_range_start_hsa,
  321. .invalidate_range_end = amdgpu_mn_invalidate_range_end,
  322. },
  323. };
  324. /* Low bits of any reasonable mm pointer will be unused due to struct
  325. * alignment. Use these bits to make a unique key from the mm pointer
  326. * and notifier type.
  327. */
  328. #define AMDGPU_MN_KEY(mm, type) ((unsigned long)(mm) + (type))
  329. /**
  330. * amdgpu_mn_get - create notifier context
  331. *
  332. * @adev: amdgpu device pointer
  333. * @type: type of MMU notifier context
  334. *
  335. * Creates a notifier context for current->mm.
  336. */
  337. struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev,
  338. enum amdgpu_mn_type type)
  339. {
  340. struct mm_struct *mm = current->mm;
  341. struct amdgpu_mn *amn;
  342. unsigned long key = AMDGPU_MN_KEY(mm, type);
  343. int r;
  344. mutex_lock(&adev->mn_lock);
  345. if (down_write_killable(&mm->mmap_sem)) {
  346. mutex_unlock(&adev->mn_lock);
  347. return ERR_PTR(-EINTR);
  348. }
  349. hash_for_each_possible(adev->mn_hash, amn, node, key)
  350. if (AMDGPU_MN_KEY(amn->mm, amn->type) == key)
  351. goto release_locks;
  352. amn = kzalloc(sizeof(*amn), GFP_KERNEL);
  353. if (!amn) {
  354. amn = ERR_PTR(-ENOMEM);
  355. goto release_locks;
  356. }
  357. amn->adev = adev;
  358. amn->mm = mm;
  359. init_rwsem(&amn->lock);
  360. amn->type = type;
  361. amn->mn.ops = &amdgpu_mn_ops[type];
  362. amn->objects = RB_ROOT_CACHED;
  363. mutex_init(&amn->read_lock);
  364. atomic_set(&amn->recursion, 0);
  365. r = __mmu_notifier_register(&amn->mn, mm);
  366. if (r)
  367. goto free_amn;
  368. hash_add(adev->mn_hash, &amn->node, AMDGPU_MN_KEY(mm, type));
  369. release_locks:
  370. up_write(&mm->mmap_sem);
  371. mutex_unlock(&adev->mn_lock);
  372. return amn;
  373. free_amn:
  374. up_write(&mm->mmap_sem);
  375. mutex_unlock(&adev->mn_lock);
  376. kfree(amn);
  377. return ERR_PTR(r);
  378. }
  379. /**
  380. * amdgpu_mn_register - register a BO for notifier updates
  381. *
  382. * @bo: amdgpu buffer object
  383. * @addr: userptr addr we should monitor
  384. *
  385. * Registers an MMU notifier for the given BO at the specified address.
  386. * Returns 0 on success, -ERRNO if anything goes wrong.
  387. */
  388. int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr)
  389. {
  390. unsigned long end = addr + amdgpu_bo_size(bo) - 1;
  391. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  392. enum amdgpu_mn_type type =
  393. bo->kfd_bo ? AMDGPU_MN_TYPE_HSA : AMDGPU_MN_TYPE_GFX;
  394. struct amdgpu_mn *amn;
  395. struct amdgpu_mn_node *node = NULL, *new_node;
  396. struct list_head bos;
  397. struct interval_tree_node *it;
  398. amn = amdgpu_mn_get(adev, type);
  399. if (IS_ERR(amn))
  400. return PTR_ERR(amn);
  401. new_node = kmalloc(sizeof(*new_node), GFP_KERNEL);
  402. if (!new_node)
  403. return -ENOMEM;
  404. INIT_LIST_HEAD(&bos);
  405. down_write(&amn->lock);
  406. while ((it = interval_tree_iter_first(&amn->objects, addr, end))) {
  407. kfree(node);
  408. node = container_of(it, struct amdgpu_mn_node, it);
  409. interval_tree_remove(&node->it, &amn->objects);
  410. addr = min(it->start, addr);
  411. end = max(it->last, end);
  412. list_splice(&node->bos, &bos);
  413. }
  414. if (!node)
  415. node = new_node;
  416. else
  417. kfree(new_node);
  418. bo->mn = amn;
  419. node->it.start = addr;
  420. node->it.last = end;
  421. INIT_LIST_HEAD(&node->bos);
  422. list_splice(&bos, &node->bos);
  423. list_add(&bo->mn_list, &node->bos);
  424. interval_tree_insert(&node->it, &amn->objects);
  425. up_write(&amn->lock);
  426. return 0;
  427. }
  428. /**
  429. * amdgpu_mn_unregister - unregister a BO for notifier updates
  430. *
  431. * @bo: amdgpu buffer object
  432. *
  433. * Remove any registration of MMU notifier updates from the buffer object.
  434. */
  435. void amdgpu_mn_unregister(struct amdgpu_bo *bo)
  436. {
  437. struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
  438. struct amdgpu_mn *amn;
  439. struct list_head *head;
  440. mutex_lock(&adev->mn_lock);
  441. amn = bo->mn;
  442. if (amn == NULL) {
  443. mutex_unlock(&adev->mn_lock);
  444. return;
  445. }
  446. down_write(&amn->lock);
  447. /* save the next list entry for later */
  448. head = bo->mn_list.next;
  449. bo->mn = NULL;
  450. list_del_init(&bo->mn_list);
  451. if (list_empty(head)) {
  452. struct amdgpu_mn_node *node;
  453. node = container_of(head, struct amdgpu_mn_node, bos);
  454. interval_tree_remove(&node->it, &amn->objects);
  455. kfree(node);
  456. }
  457. up_write(&amn->lock);
  458. mutex_unlock(&adev->mn_lock);
  459. }