2 * Copyright 2014 Advanced Micro Devices, Inc.
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:
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.
21 * The above copyright notice and this permission notice (including the
22 * next paragraph) shall be included in all copies or substantial portions
34 * For coherent userptr handling registers an MMU notifier to inform the driver
35 * about updates on the page tables of a process.
37 * When somebody tries to invalidate the page tables we block the update until
38 * all operations on the pages in question are completed, then those pages are
39 * marked as accessed and also dirty if it wasn't a read only access.
41 * New command submissions using the userptrs in question are delayed until all
42 * page table invalidation are completed and we once more see a coherent process
46 #include <linux/firmware.h>
47 #include <linux/module.h>
48 #include <linux/mmu_notifier.h>
49 #include <linux/interval_tree.h>
54 #include "amdgpu_amdkfd.h"
59 * @adev: amdgpu device pointer
60 * @mm: process address space
61 * @mn: MMU notifier structur
62 * @work: destruction work item
63 * @node: hash table node to find structure by adev and mn
64 * @lock: rw semaphore protecting the notifier nodes
65 * @objects: interval tree containing amdgpu_mn_nodes
66 * @read_lock: mutex for recursive locking of @lock
67 * @recursion: depth of recursion
69 * Data for each amdgpu device and process address space.
72 /* constant after initialisation */
73 struct amdgpu_device *adev;
75 struct mmu_notifier mn;
76 enum amdgpu_mn_type type;
78 /* only used on destruction */
79 struct work_struct work;
81 /* protected by adev->mn_lock */
82 struct hlist_node node;
84 /* objects protected by lock */
85 struct rw_semaphore lock;
86 struct rb_root_cached objects;
87 struct mutex read_lock;
92 * struct amdgpu_mn_node
94 * @it: interval node defining start-last of the affected address range
95 * @bos: list of all BOs in the affected address range
97 * Manages all BOs which are affected of a certain range of address space.
99 struct amdgpu_mn_node {
100 struct interval_tree_node it;
101 struct list_head bos;
105 * amdgpu_mn_destroy - destroy the MMU notifier
107 * @work: previously sheduled work item
109 * Lazy destroys the notifier from a work item
111 static void amdgpu_mn_destroy(struct work_struct *work)
113 struct amdgpu_mn *amn = container_of(work, struct amdgpu_mn, work);
114 struct amdgpu_device *adev = amn->adev;
115 struct amdgpu_mn_node *node, *next_node;
116 struct amdgpu_bo *bo, *next_bo;
118 mutex_lock(&adev->mn_lock);
119 down_write(&amn->lock);
120 hash_del(&amn->node);
121 rbtree_postorder_for_each_entry_safe(node, next_node,
122 &amn->objects.rb_root, it.rb) {
123 list_for_each_entry_safe(bo, next_bo, &node->bos, mn_list) {
125 list_del_init(&bo->mn_list);
129 up_write(&amn->lock);
130 mutex_unlock(&adev->mn_lock);
131 mmu_notifier_unregister_no_release(&amn->mn, amn->mm);
136 * amdgpu_mn_release - callback to notify about mm destruction
139 * @mm: the mm this callback is about
141 * Shedule a work item to lazy destroy our notifier.
143 static void amdgpu_mn_release(struct mmu_notifier *mn,
144 struct mm_struct *mm)
146 struct amdgpu_mn *amn = container_of(mn, struct amdgpu_mn, mn);
148 INIT_WORK(&amn->work, amdgpu_mn_destroy);
149 schedule_work(&amn->work);
154 * amdgpu_mn_lock - take the write side lock for this notifier
158 void amdgpu_mn_lock(struct amdgpu_mn *mn)
161 down_write(&mn->lock);
165 * amdgpu_mn_unlock - drop the write side lock for this notifier
169 void amdgpu_mn_unlock(struct amdgpu_mn *mn)
176 * amdgpu_mn_read_lock - take the read side lock for this notifier
180 static void amdgpu_mn_read_lock(struct amdgpu_mn *amn)
182 mutex_lock(&amn->read_lock);
183 if (atomic_inc_return(&amn->recursion) == 1)
184 down_read_non_owner(&amn->lock);
185 mutex_unlock(&amn->read_lock);
189 * amdgpu_mn_read_unlock - drop the read side lock for this notifier
193 static void amdgpu_mn_read_unlock(struct amdgpu_mn *amn)
195 if (atomic_dec_return(&amn->recursion) == 0)
196 up_read_non_owner(&amn->lock);
200 * amdgpu_mn_invalidate_node - unmap all BOs of a node
202 * @node: the node with the BOs to unmap
203 * @start: start of address range affected
204 * @end: end of address range affected
206 * Block for operations on BOs to finish and mark pages as accessed and
209 static void amdgpu_mn_invalidate_node(struct amdgpu_mn_node *node,
213 struct amdgpu_bo *bo;
216 list_for_each_entry(bo, &node->bos, mn_list) {
218 if (!amdgpu_ttm_tt_affect_userptr(bo->tbo.ttm, start, end))
221 r = reservation_object_wait_timeout_rcu(bo->tbo.resv,
222 true, false, MAX_SCHEDULE_TIMEOUT);
224 DRM_ERROR("(%ld) failed to wait for user bo\n", r);
226 amdgpu_ttm_tt_mark_user_pages(bo->tbo.ttm);
231 * amdgpu_mn_invalidate_range_start_gfx - callback to notify about mm change
234 * @mm: the mm this callback is about
235 * @start: start of updated range
236 * @end: end of updated range
238 * Block for operations on BOs to finish and mark pages as accessed and
241 static void amdgpu_mn_invalidate_range_start_gfx(struct mmu_notifier *mn,
242 struct mm_struct *mm,
246 struct amdgpu_mn *amn = container_of(mn, struct amdgpu_mn, mn);
247 struct interval_tree_node *it;
249 /* notification is exclusive, but interval is inclusive */
252 amdgpu_mn_read_lock(amn);
254 it = interval_tree_iter_first(&amn->objects, start, end);
256 struct amdgpu_mn_node *node;
258 node = container_of(it, struct amdgpu_mn_node, it);
259 it = interval_tree_iter_next(it, start, end);
261 amdgpu_mn_invalidate_node(node, start, end);
266 * amdgpu_mn_invalidate_range_start_hsa - callback to notify about mm change
269 * @mn: the mm this callback is about
270 * @start: start of updated range
271 * @end: end of updated range
273 * We temporarily evict all BOs between start and end. This
274 * necessitates evicting all user-mode queues of the process. The BOs
275 * are restorted in amdgpu_mn_invalidate_range_end_hsa.
277 static void amdgpu_mn_invalidate_range_start_hsa(struct mmu_notifier *mn,
278 struct mm_struct *mm,
282 struct amdgpu_mn *amn = container_of(mn, struct amdgpu_mn, mn);
283 struct interval_tree_node *it;
285 /* notification is exclusive, but interval is inclusive */
288 amdgpu_mn_read_lock(amn);
290 it = interval_tree_iter_first(&amn->objects, start, end);
292 struct amdgpu_mn_node *node;
293 struct amdgpu_bo *bo;
295 node = container_of(it, struct amdgpu_mn_node, it);
296 it = interval_tree_iter_next(it, start, end);
298 list_for_each_entry(bo, &node->bos, mn_list) {
299 struct kgd_mem *mem = bo->kfd_bo;
301 if (amdgpu_ttm_tt_affect_userptr(bo->tbo.ttm,
303 amdgpu_amdkfd_evict_userptr(mem, mm);
309 * amdgpu_mn_invalidate_range_end - callback to notify about mm change
312 * @mm: the mm this callback is about
313 * @start: start of updated range
314 * @end: end of updated range
316 * Release the lock again to allow new command submissions.
318 static void amdgpu_mn_invalidate_range_end(struct mmu_notifier *mn,
319 struct mm_struct *mm,
323 struct amdgpu_mn *amn = container_of(mn, struct amdgpu_mn, mn);
325 amdgpu_mn_read_unlock(amn);
328 static const struct mmu_notifier_ops amdgpu_mn_ops[] = {
329 [AMDGPU_MN_TYPE_GFX] = {
330 .release = amdgpu_mn_release,
331 .invalidate_range_start = amdgpu_mn_invalidate_range_start_gfx,
332 .invalidate_range_end = amdgpu_mn_invalidate_range_end,
334 [AMDGPU_MN_TYPE_HSA] = {
335 .release = amdgpu_mn_release,
336 .invalidate_range_start = amdgpu_mn_invalidate_range_start_hsa,
337 .invalidate_range_end = amdgpu_mn_invalidate_range_end,
341 /* Low bits of any reasonable mm pointer will be unused due to struct
342 * alignment. Use these bits to make a unique key from the mm pointer
345 #define AMDGPU_MN_KEY(mm, type) ((unsigned long)(mm) + (type))
348 * amdgpu_mn_get - create notifier context
350 * @adev: amdgpu device pointer
351 * @type: type of MMU notifier context
353 * Creates a notifier context for current->mm.
355 struct amdgpu_mn *amdgpu_mn_get(struct amdgpu_device *adev,
356 enum amdgpu_mn_type type)
358 struct mm_struct *mm = current->mm;
359 struct amdgpu_mn *amn;
360 unsigned long key = AMDGPU_MN_KEY(mm, type);
363 mutex_lock(&adev->mn_lock);
364 if (down_write_killable(&mm->mmap_sem)) {
365 mutex_unlock(&adev->mn_lock);
366 return ERR_PTR(-EINTR);
369 hash_for_each_possible(adev->mn_hash, amn, node, key)
370 if (AMDGPU_MN_KEY(amn->mm, amn->type) == key)
373 amn = kzalloc(sizeof(*amn), GFP_KERNEL);
375 amn = ERR_PTR(-ENOMEM);
381 init_rwsem(&amn->lock);
383 amn->mn.ops = &amdgpu_mn_ops[type];
384 amn->objects = RB_ROOT_CACHED;
385 mutex_init(&amn->read_lock);
386 atomic_set(&amn->recursion, 0);
388 r = __mmu_notifier_register(&amn->mn, mm);
392 hash_add(adev->mn_hash, &amn->node, AMDGPU_MN_KEY(mm, type));
395 up_write(&mm->mmap_sem);
396 mutex_unlock(&adev->mn_lock);
401 up_write(&mm->mmap_sem);
402 mutex_unlock(&adev->mn_lock);
409 * amdgpu_mn_register - register a BO for notifier updates
411 * @bo: amdgpu buffer object
412 * @addr: userptr addr we should monitor
414 * Registers an MMU notifier for the given BO at the specified address.
415 * Returns 0 on success, -ERRNO if anything goes wrong.
417 int amdgpu_mn_register(struct amdgpu_bo *bo, unsigned long addr)
419 unsigned long end = addr + amdgpu_bo_size(bo) - 1;
420 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
421 enum amdgpu_mn_type type =
422 bo->kfd_bo ? AMDGPU_MN_TYPE_HSA : AMDGPU_MN_TYPE_GFX;
423 struct amdgpu_mn *amn;
424 struct amdgpu_mn_node *node = NULL, *new_node;
425 struct list_head bos;
426 struct interval_tree_node *it;
428 amn = amdgpu_mn_get(adev, type);
432 new_node = kmalloc(sizeof(*new_node), GFP_KERNEL);
436 INIT_LIST_HEAD(&bos);
438 down_write(&amn->lock);
440 while ((it = interval_tree_iter_first(&amn->objects, addr, end))) {
442 node = container_of(it, struct amdgpu_mn_node, it);
443 interval_tree_remove(&node->it, &amn->objects);
444 addr = min(it->start, addr);
445 end = max(it->last, end);
446 list_splice(&node->bos, &bos);
456 node->it.start = addr;
458 INIT_LIST_HEAD(&node->bos);
459 list_splice(&bos, &node->bos);
460 list_add(&bo->mn_list, &node->bos);
462 interval_tree_insert(&node->it, &amn->objects);
464 up_write(&amn->lock);
470 * amdgpu_mn_unregister - unregister a BO for notifier updates
472 * @bo: amdgpu buffer object
474 * Remove any registration of MMU notifier updates from the buffer object.
476 void amdgpu_mn_unregister(struct amdgpu_bo *bo)
478 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
479 struct amdgpu_mn *amn;
480 struct list_head *head;
482 mutex_lock(&adev->mn_lock);
486 mutex_unlock(&adev->mn_lock);
490 down_write(&amn->lock);
492 /* save the next list entry for later */
493 head = bo->mn_list.next;
496 list_del_init(&bo->mn_list);
498 if (list_empty(head)) {
499 struct amdgpu_mn_node *node;
501 node = container_of(head, struct amdgpu_mn_node, bos);
502 interval_tree_remove(&node->it, &amn->objects);
506 up_write(&amn->lock);
507 mutex_unlock(&adev->mn_lock);