1 // SPDX-License-Identifier: GPL-2.0 OR MIT
3 * Copyright 2020-2021 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 "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
24 #include <linux/types.h>
25 #include <linux/sched/task.h>
26 #include <drm/ttm/ttm_tt.h>
27 #include <drm/drm_exec.h>
29 #include "amdgpu_sync.h"
30 #include "amdgpu_object.h"
31 #include "amdgpu_vm.h"
32 #include "amdgpu_hmm.h"
34 #include "amdgpu_xgmi.h"
37 #include "kfd_migrate.h"
38 #include "kfd_smi_events.h"
43 #define dev_fmt(fmt) "kfd_svm: %s: " fmt, __func__
45 #define AMDGPU_SVM_RANGE_RESTORE_DELAY_MS 1
47 /* Long enough to ensure no retry fault comes after svm range is restored and
48 * page table is updated.
50 #define AMDGPU_SVM_RANGE_RETRY_FAULT_PENDING (2UL * NSEC_PER_MSEC)
51 #define dynamic_svm_range_dump(svms) \
52 _dynamic_func_call_no_desc("svm_range_dump", svm_range_debug_dump, svms)
54 /* Giant svm range split into smaller ranges based on this, it is decided using
55 * minimum of all dGPU/APU 1/32 VRAM size, between 2MB to 1GB and alignment to
58 static uint64_t max_svm_range_pages;
60 struct criu_svm_metadata {
61 struct list_head list;
62 struct kfd_criu_svm_range_priv_data data;
65 static void svm_range_evict_svm_bo_worker(struct work_struct *work);
67 svm_range_cpu_invalidate_pagetables(struct mmu_interval_notifier *mni,
68 const struct mmu_notifier_range *range,
69 unsigned long cur_seq);
71 svm_range_check_vm(struct kfd_process *p, uint64_t start, uint64_t last,
72 uint64_t *bo_s, uint64_t *bo_l);
73 static const struct mmu_interval_notifier_ops svm_range_mn_ops = {
74 .invalidate = svm_range_cpu_invalidate_pagetables,
78 * svm_range_unlink - unlink svm_range from lists and interval tree
79 * @prange: svm range structure to be removed
81 * Remove the svm_range from the svms and svm_bo lists and the svms
84 * Context: The caller must hold svms->lock
86 static void svm_range_unlink(struct svm_range *prange)
88 pr_debug("svms 0x%p prange 0x%p [0x%lx 0x%lx]\n", prange->svms,
89 prange, prange->start, prange->last);
92 spin_lock(&prange->svm_bo->list_lock);
93 list_del(&prange->svm_bo_list);
94 spin_unlock(&prange->svm_bo->list_lock);
97 list_del(&prange->list);
98 if (prange->it_node.start != 0 && prange->it_node.last != 0)
99 interval_tree_remove(&prange->it_node, &prange->svms->objects);
103 svm_range_add_notifier_locked(struct mm_struct *mm, struct svm_range *prange)
105 pr_debug("svms 0x%p prange 0x%p [0x%lx 0x%lx]\n", prange->svms,
106 prange, prange->start, prange->last);
108 mmu_interval_notifier_insert_locked(&prange->notifier, mm,
109 prange->start << PAGE_SHIFT,
110 prange->npages << PAGE_SHIFT,
115 * svm_range_add_to_svms - add svm range to svms
116 * @prange: svm range structure to be added
118 * Add the svm range to svms interval tree and link list
120 * Context: The caller must hold svms->lock
122 static void svm_range_add_to_svms(struct svm_range *prange)
124 pr_debug("svms 0x%p prange 0x%p [0x%lx 0x%lx]\n", prange->svms,
125 prange, prange->start, prange->last);
127 list_move_tail(&prange->list, &prange->svms->list);
128 prange->it_node.start = prange->start;
129 prange->it_node.last = prange->last;
130 interval_tree_insert(&prange->it_node, &prange->svms->objects);
133 static void svm_range_remove_notifier(struct svm_range *prange)
135 pr_debug("remove notifier svms 0x%p prange 0x%p [0x%lx 0x%lx]\n",
136 prange->svms, prange,
137 prange->notifier.interval_tree.start >> PAGE_SHIFT,
138 prange->notifier.interval_tree.last >> PAGE_SHIFT);
140 if (prange->notifier.interval_tree.start != 0 &&
141 prange->notifier.interval_tree.last != 0)
142 mmu_interval_notifier_remove(&prange->notifier);
146 svm_is_valid_dma_mapping_addr(struct device *dev, dma_addr_t dma_addr)
148 return dma_addr && !dma_mapping_error(dev, dma_addr) &&
149 !(dma_addr & SVM_RANGE_VRAM_DOMAIN);
153 svm_range_dma_map_dev(struct amdgpu_device *adev, struct svm_range *prange,
154 unsigned long offset, unsigned long npages,
155 unsigned long *hmm_pfns, uint32_t gpuidx)
157 enum dma_data_direction dir = DMA_BIDIRECTIONAL;
158 dma_addr_t *addr = prange->dma_addr[gpuidx];
159 struct device *dev = adev->dev;
164 addr = kvcalloc(prange->npages, sizeof(*addr), GFP_KERNEL);
167 prange->dma_addr[gpuidx] = addr;
171 for (i = 0; i < npages; i++) {
172 if (svm_is_valid_dma_mapping_addr(dev, addr[i]))
173 dma_unmap_page(dev, addr[i], PAGE_SIZE, dir);
175 page = hmm_pfn_to_page(hmm_pfns[i]);
176 if (is_zone_device_page(page)) {
177 struct amdgpu_device *bo_adev = prange->svm_bo->node->adev;
179 addr[i] = (hmm_pfns[i] << PAGE_SHIFT) +
180 bo_adev->vm_manager.vram_base_offset -
181 bo_adev->kfd.pgmap.range.start;
182 addr[i] |= SVM_RANGE_VRAM_DOMAIN;
183 pr_debug_ratelimited("vram address: 0x%llx\n", addr[i]);
186 addr[i] = dma_map_page(dev, page, 0, PAGE_SIZE, dir);
187 r = dma_mapping_error(dev, addr[i]);
189 dev_err(dev, "failed %d dma_map_page\n", r);
192 pr_debug_ratelimited("dma mapping 0x%llx for page addr 0x%lx\n",
193 addr[i] >> PAGE_SHIFT, page_to_pfn(page));
199 svm_range_dma_map(struct svm_range *prange, unsigned long *bitmap,
200 unsigned long offset, unsigned long npages,
201 unsigned long *hmm_pfns)
203 struct kfd_process *p;
207 p = container_of(prange->svms, struct kfd_process, svms);
209 for_each_set_bit(gpuidx, bitmap, MAX_GPU_INSTANCE) {
210 struct kfd_process_device *pdd;
212 pr_debug("mapping to gpu idx 0x%x\n", gpuidx);
213 pdd = kfd_process_device_from_gpuidx(p, gpuidx);
215 pr_debug("failed to find device idx %d\n", gpuidx);
219 r = svm_range_dma_map_dev(pdd->dev->adev, prange, offset, npages,
228 void svm_range_dma_unmap(struct device *dev, dma_addr_t *dma_addr,
229 unsigned long offset, unsigned long npages)
231 enum dma_data_direction dir = DMA_BIDIRECTIONAL;
237 for (i = offset; i < offset + npages; i++) {
238 if (!svm_is_valid_dma_mapping_addr(dev, dma_addr[i]))
240 pr_debug_ratelimited("unmap 0x%llx\n", dma_addr[i] >> PAGE_SHIFT);
241 dma_unmap_page(dev, dma_addr[i], PAGE_SIZE, dir);
246 void svm_range_free_dma_mappings(struct svm_range *prange)
248 struct kfd_process_device *pdd;
249 dma_addr_t *dma_addr;
251 struct kfd_process *p;
254 p = container_of(prange->svms, struct kfd_process, svms);
256 for (gpuidx = 0; gpuidx < MAX_GPU_INSTANCE; gpuidx++) {
257 dma_addr = prange->dma_addr[gpuidx];
261 pdd = kfd_process_device_from_gpuidx(p, gpuidx);
263 pr_debug("failed to find device idx %d\n", gpuidx);
266 dev = &pdd->dev->adev->pdev->dev;
267 svm_range_dma_unmap(dev, dma_addr, 0, prange->npages);
269 prange->dma_addr[gpuidx] = NULL;
273 static void svm_range_free(struct svm_range *prange, bool update_mem_usage)
275 uint64_t size = (prange->last - prange->start + 1) << PAGE_SHIFT;
276 struct kfd_process *p = container_of(prange->svms, struct kfd_process, svms);
278 pr_debug("svms 0x%p prange 0x%p [0x%lx 0x%lx]\n", prange->svms, prange,
279 prange->start, prange->last);
281 svm_range_vram_node_free(prange);
282 svm_range_free_dma_mappings(prange);
284 if (update_mem_usage && !p->xnack_enabled) {
285 pr_debug("unreserve prange 0x%p size: 0x%llx\n", prange, size);
286 amdgpu_amdkfd_unreserve_mem_limit(NULL, size,
287 KFD_IOC_ALLOC_MEM_FLAGS_USERPTR, 0);
289 mutex_destroy(&prange->lock);
290 mutex_destroy(&prange->migrate_mutex);
295 svm_range_set_default_attributes(int32_t *location, int32_t *prefetch_loc,
296 uint8_t *granularity, uint32_t *flags)
298 *location = KFD_IOCTL_SVM_LOCATION_UNDEFINED;
299 *prefetch_loc = KFD_IOCTL_SVM_LOCATION_UNDEFINED;
302 KFD_IOCTL_SVM_FLAG_HOST_ACCESS | KFD_IOCTL_SVM_FLAG_COHERENT;
306 svm_range *svm_range_new(struct svm_range_list *svms, uint64_t start,
307 uint64_t last, bool update_mem_usage)
309 uint64_t size = last - start + 1;
310 struct svm_range *prange;
311 struct kfd_process *p;
313 prange = kzalloc(sizeof(*prange), GFP_KERNEL);
317 p = container_of(svms, struct kfd_process, svms);
318 if (!p->xnack_enabled && update_mem_usage &&
319 amdgpu_amdkfd_reserve_mem_limit(NULL, size << PAGE_SHIFT,
320 KFD_IOC_ALLOC_MEM_FLAGS_USERPTR, 0)) {
321 pr_info("SVM mapping failed, exceeds resident system memory limit\n");
325 prange->npages = size;
327 prange->start = start;
329 INIT_LIST_HEAD(&prange->list);
330 INIT_LIST_HEAD(&prange->update_list);
331 INIT_LIST_HEAD(&prange->svm_bo_list);
332 INIT_LIST_HEAD(&prange->deferred_list);
333 INIT_LIST_HEAD(&prange->child_list);
334 atomic_set(&prange->invalid, 0);
335 prange->validate_timestamp = 0;
336 mutex_init(&prange->migrate_mutex);
337 mutex_init(&prange->lock);
339 if (p->xnack_enabled)
340 bitmap_copy(prange->bitmap_access, svms->bitmap_supported,
343 svm_range_set_default_attributes(&prange->preferred_loc,
344 &prange->prefetch_loc,
345 &prange->granularity, &prange->flags);
347 pr_debug("svms 0x%p [0x%llx 0x%llx]\n", svms, start, last);
352 static bool svm_bo_ref_unless_zero(struct svm_range_bo *svm_bo)
354 if (!svm_bo || !kref_get_unless_zero(&svm_bo->kref))
360 static void svm_range_bo_release(struct kref *kref)
362 struct svm_range_bo *svm_bo;
364 svm_bo = container_of(kref, struct svm_range_bo, kref);
365 pr_debug("svm_bo 0x%p\n", svm_bo);
367 spin_lock(&svm_bo->list_lock);
368 while (!list_empty(&svm_bo->range_list)) {
369 struct svm_range *prange =
370 list_first_entry(&svm_bo->range_list,
371 struct svm_range, svm_bo_list);
372 /* list_del_init tells a concurrent svm_range_vram_node_new when
373 * it's safe to reuse the svm_bo pointer and svm_bo_list head.
375 list_del_init(&prange->svm_bo_list);
376 spin_unlock(&svm_bo->list_lock);
378 pr_debug("svms 0x%p [0x%lx 0x%lx]\n", prange->svms,
379 prange->start, prange->last);
380 mutex_lock(&prange->lock);
381 prange->svm_bo = NULL;
382 mutex_unlock(&prange->lock);
384 spin_lock(&svm_bo->list_lock);
386 spin_unlock(&svm_bo->list_lock);
387 if (!dma_fence_is_signaled(&svm_bo->eviction_fence->base)) {
388 /* We're not in the eviction worker.
389 * Signal the fence and synchronize with any
390 * pending eviction work.
392 dma_fence_signal(&svm_bo->eviction_fence->base);
393 cancel_work_sync(&svm_bo->eviction_work);
395 dma_fence_put(&svm_bo->eviction_fence->base);
396 amdgpu_bo_unref(&svm_bo->bo);
400 static void svm_range_bo_wq_release(struct work_struct *work)
402 struct svm_range_bo *svm_bo;
404 svm_bo = container_of(work, struct svm_range_bo, release_work);
405 svm_range_bo_release(&svm_bo->kref);
408 static void svm_range_bo_release_async(struct kref *kref)
410 struct svm_range_bo *svm_bo;
412 svm_bo = container_of(kref, struct svm_range_bo, kref);
413 pr_debug("svm_bo 0x%p\n", svm_bo);
414 INIT_WORK(&svm_bo->release_work, svm_range_bo_wq_release);
415 schedule_work(&svm_bo->release_work);
418 void svm_range_bo_unref_async(struct svm_range_bo *svm_bo)
420 kref_put(&svm_bo->kref, svm_range_bo_release_async);
423 static void svm_range_bo_unref(struct svm_range_bo *svm_bo)
426 kref_put(&svm_bo->kref, svm_range_bo_release);
430 svm_range_validate_svm_bo(struct kfd_node *node, struct svm_range *prange)
432 mutex_lock(&prange->lock);
433 if (!prange->svm_bo) {
434 mutex_unlock(&prange->lock);
437 if (prange->ttm_res) {
438 /* We still have a reference, all is well */
439 mutex_unlock(&prange->lock);
442 if (svm_bo_ref_unless_zero(prange->svm_bo)) {
444 * Migrate from GPU to GPU, remove range from source svm_bo->node
445 * range list, and return false to allocate svm_bo from destination
448 if (prange->svm_bo->node != node) {
449 mutex_unlock(&prange->lock);
451 spin_lock(&prange->svm_bo->list_lock);
452 list_del_init(&prange->svm_bo_list);
453 spin_unlock(&prange->svm_bo->list_lock);
455 svm_range_bo_unref(prange->svm_bo);
458 if (READ_ONCE(prange->svm_bo->evicting)) {
460 struct svm_range_bo *svm_bo;
461 /* The BO is getting evicted,
462 * we need to get a new one
464 mutex_unlock(&prange->lock);
465 svm_bo = prange->svm_bo;
466 f = dma_fence_get(&svm_bo->eviction_fence->base);
467 svm_range_bo_unref(prange->svm_bo);
468 /* wait for the fence to avoid long spin-loop
469 * at list_empty_careful
471 dma_fence_wait(f, false);
474 /* The BO was still around and we got
475 * a new reference to it
477 mutex_unlock(&prange->lock);
478 pr_debug("reuse old bo svms 0x%p [0x%lx 0x%lx]\n",
479 prange->svms, prange->start, prange->last);
481 prange->ttm_res = prange->svm_bo->bo->tbo.resource;
486 mutex_unlock(&prange->lock);
489 /* We need a new svm_bo. Spin-loop to wait for concurrent
490 * svm_range_bo_release to finish removing this range from
491 * its range list. After this, it is safe to reuse the
492 * svm_bo pointer and svm_bo_list head.
494 while (!list_empty_careful(&prange->svm_bo_list))
500 static struct svm_range_bo *svm_range_bo_new(void)
502 struct svm_range_bo *svm_bo;
504 svm_bo = kzalloc(sizeof(*svm_bo), GFP_KERNEL);
508 kref_init(&svm_bo->kref);
509 INIT_LIST_HEAD(&svm_bo->range_list);
510 spin_lock_init(&svm_bo->list_lock);
516 svm_range_vram_node_new(struct kfd_node *node, struct svm_range *prange,
519 struct amdgpu_bo_param bp;
520 struct svm_range_bo *svm_bo;
521 struct amdgpu_bo_user *ubo;
522 struct amdgpu_bo *bo;
523 struct kfd_process *p;
524 struct mm_struct *mm;
527 p = container_of(prange->svms, struct kfd_process, svms);
528 pr_debug("pasid: %x svms 0x%p [0x%lx 0x%lx]\n", p->pasid, prange->svms,
529 prange->start, prange->last);
531 if (svm_range_validate_svm_bo(node, prange))
534 svm_bo = svm_range_bo_new();
536 pr_debug("failed to alloc svm bo\n");
539 mm = get_task_mm(p->lead_thread);
541 pr_debug("failed to get mm\n");
546 svm_bo->eviction_fence =
547 amdgpu_amdkfd_fence_create(dma_fence_context_alloc(1),
551 INIT_WORK(&svm_bo->eviction_work, svm_range_evict_svm_bo_worker);
552 svm_bo->evicting = 0;
553 memset(&bp, 0, sizeof(bp));
554 bp.size = prange->npages * PAGE_SIZE;
555 bp.byte_align = PAGE_SIZE;
556 bp.domain = AMDGPU_GEM_DOMAIN_VRAM;
557 bp.flags = AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
558 bp.flags |= clear ? AMDGPU_GEM_CREATE_VRAM_CLEARED : 0;
559 bp.flags |= AMDGPU_GEM_CREATE_DISCARDABLE;
560 bp.type = ttm_bo_type_device;
563 bp.xcp_id_plus1 = node->xcp->id + 1;
565 r = amdgpu_bo_create_user(node->adev, &bp, &ubo);
567 pr_debug("failed %d to create bo\n", r);
568 goto create_bo_failed;
572 pr_debug("alloc bo at offset 0x%lx size 0x%lx on partition %d\n",
573 bo->tbo.resource->start << PAGE_SHIFT, bp.size,
574 bp.xcp_id_plus1 - 1);
576 r = amdgpu_bo_reserve(bo, true);
578 pr_debug("failed %d to reserve bo\n", r);
579 goto reserve_bo_failed;
583 r = amdgpu_bo_sync_wait(bo, AMDGPU_FENCE_OWNER_KFD, false);
585 pr_debug("failed %d to sync bo\n", r);
586 amdgpu_bo_unreserve(bo);
587 goto reserve_bo_failed;
591 r = dma_resv_reserve_fences(bo->tbo.base.resv, 1);
593 pr_debug("failed %d to reserve bo\n", r);
594 amdgpu_bo_unreserve(bo);
595 goto reserve_bo_failed;
597 amdgpu_bo_fence(bo, &svm_bo->eviction_fence->base, true);
599 amdgpu_bo_unreserve(bo);
602 prange->svm_bo = svm_bo;
603 prange->ttm_res = bo->tbo.resource;
606 spin_lock(&svm_bo->list_lock);
607 list_add(&prange->svm_bo_list, &svm_bo->range_list);
608 spin_unlock(&svm_bo->list_lock);
613 amdgpu_bo_unref(&bo);
615 dma_fence_put(&svm_bo->eviction_fence->base);
617 prange->ttm_res = NULL;
622 void svm_range_vram_node_free(struct svm_range *prange)
624 svm_range_bo_unref(prange->svm_bo);
625 prange->ttm_res = NULL;
629 svm_range_get_node_by_id(struct svm_range *prange, uint32_t gpu_id)
631 struct kfd_process *p;
632 struct kfd_process_device *pdd;
634 p = container_of(prange->svms, struct kfd_process, svms);
635 pdd = kfd_process_device_data_by_id(p, gpu_id);
637 pr_debug("failed to get kfd process device by id 0x%x\n", gpu_id);
644 struct kfd_process_device *
645 svm_range_get_pdd_by_node(struct svm_range *prange, struct kfd_node *node)
647 struct kfd_process *p;
649 p = container_of(prange->svms, struct kfd_process, svms);
651 return kfd_get_process_device_data(node, p);
654 static int svm_range_bo_validate(void *param, struct amdgpu_bo *bo)
656 struct ttm_operation_ctx ctx = { false, false };
658 amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_VRAM);
660 return ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
664 svm_range_check_attr(struct kfd_process *p,
665 uint32_t nattr, struct kfd_ioctl_svm_attribute *attrs)
669 for (i = 0; i < nattr; i++) {
670 uint32_t val = attrs[i].value;
671 int gpuidx = MAX_GPU_INSTANCE;
673 switch (attrs[i].type) {
674 case KFD_IOCTL_SVM_ATTR_PREFERRED_LOC:
675 if (val != KFD_IOCTL_SVM_LOCATION_SYSMEM &&
676 val != KFD_IOCTL_SVM_LOCATION_UNDEFINED)
677 gpuidx = kfd_process_gpuidx_from_gpuid(p, val);
679 case KFD_IOCTL_SVM_ATTR_PREFETCH_LOC:
680 if (val != KFD_IOCTL_SVM_LOCATION_SYSMEM)
681 gpuidx = kfd_process_gpuidx_from_gpuid(p, val);
683 case KFD_IOCTL_SVM_ATTR_ACCESS:
684 case KFD_IOCTL_SVM_ATTR_ACCESS_IN_PLACE:
685 case KFD_IOCTL_SVM_ATTR_NO_ACCESS:
686 gpuidx = kfd_process_gpuidx_from_gpuid(p, val);
688 case KFD_IOCTL_SVM_ATTR_SET_FLAGS:
690 case KFD_IOCTL_SVM_ATTR_CLR_FLAGS:
692 case KFD_IOCTL_SVM_ATTR_GRANULARITY:
695 pr_debug("unknown attr type 0x%x\n", attrs[i].type);
700 pr_debug("no GPU 0x%x found\n", val);
702 } else if (gpuidx < MAX_GPU_INSTANCE &&
703 !test_bit(gpuidx, p->svms.bitmap_supported)) {
704 pr_debug("GPU 0x%x not supported\n", val);
713 svm_range_apply_attrs(struct kfd_process *p, struct svm_range *prange,
714 uint32_t nattr, struct kfd_ioctl_svm_attribute *attrs,
715 bool *update_mapping)
720 for (i = 0; i < nattr; i++) {
721 switch (attrs[i].type) {
722 case KFD_IOCTL_SVM_ATTR_PREFERRED_LOC:
723 prange->preferred_loc = attrs[i].value;
725 case KFD_IOCTL_SVM_ATTR_PREFETCH_LOC:
726 prange->prefetch_loc = attrs[i].value;
728 case KFD_IOCTL_SVM_ATTR_ACCESS:
729 case KFD_IOCTL_SVM_ATTR_ACCESS_IN_PLACE:
730 case KFD_IOCTL_SVM_ATTR_NO_ACCESS:
731 if (!p->xnack_enabled)
732 *update_mapping = true;
734 gpuidx = kfd_process_gpuidx_from_gpuid(p,
736 if (attrs[i].type == KFD_IOCTL_SVM_ATTR_NO_ACCESS) {
737 bitmap_clear(prange->bitmap_access, gpuidx, 1);
738 bitmap_clear(prange->bitmap_aip, gpuidx, 1);
739 } else if (attrs[i].type == KFD_IOCTL_SVM_ATTR_ACCESS) {
740 bitmap_set(prange->bitmap_access, gpuidx, 1);
741 bitmap_clear(prange->bitmap_aip, gpuidx, 1);
743 bitmap_clear(prange->bitmap_access, gpuidx, 1);
744 bitmap_set(prange->bitmap_aip, gpuidx, 1);
747 case KFD_IOCTL_SVM_ATTR_SET_FLAGS:
748 *update_mapping = true;
749 prange->flags |= attrs[i].value;
751 case KFD_IOCTL_SVM_ATTR_CLR_FLAGS:
752 *update_mapping = true;
753 prange->flags &= ~attrs[i].value;
755 case KFD_IOCTL_SVM_ATTR_GRANULARITY:
756 prange->granularity = attrs[i].value;
759 WARN_ONCE(1, "svm_range_check_attrs wasn't called?");
765 svm_range_is_same_attrs(struct kfd_process *p, struct svm_range *prange,
766 uint32_t nattr, struct kfd_ioctl_svm_attribute *attrs)
771 for (i = 0; i < nattr; i++) {
772 switch (attrs[i].type) {
773 case KFD_IOCTL_SVM_ATTR_PREFERRED_LOC:
774 if (prange->preferred_loc != attrs[i].value)
777 case KFD_IOCTL_SVM_ATTR_PREFETCH_LOC:
778 /* Prefetch should always trigger a migration even
779 * if the value of the attribute didn't change.
782 case KFD_IOCTL_SVM_ATTR_ACCESS:
783 case KFD_IOCTL_SVM_ATTR_ACCESS_IN_PLACE:
784 case KFD_IOCTL_SVM_ATTR_NO_ACCESS:
785 gpuidx = kfd_process_gpuidx_from_gpuid(p,
787 if (attrs[i].type == KFD_IOCTL_SVM_ATTR_NO_ACCESS) {
788 if (test_bit(gpuidx, prange->bitmap_access) ||
789 test_bit(gpuidx, prange->bitmap_aip))
791 } else if (attrs[i].type == KFD_IOCTL_SVM_ATTR_ACCESS) {
792 if (!test_bit(gpuidx, prange->bitmap_access))
795 if (!test_bit(gpuidx, prange->bitmap_aip))
799 case KFD_IOCTL_SVM_ATTR_SET_FLAGS:
800 if ((prange->flags & attrs[i].value) != attrs[i].value)
803 case KFD_IOCTL_SVM_ATTR_CLR_FLAGS:
804 if ((prange->flags & attrs[i].value) != 0)
807 case KFD_IOCTL_SVM_ATTR_GRANULARITY:
808 if (prange->granularity != attrs[i].value)
812 WARN_ONCE(1, "svm_range_check_attrs wasn't called?");
816 return !prange->is_error_flag;
820 * svm_range_debug_dump - print all range information from svms
821 * @svms: svm range list header
823 * debug output svm range start, end, prefetch location from svms
824 * interval tree and link list
826 * Context: The caller must hold svms->lock
828 static void svm_range_debug_dump(struct svm_range_list *svms)
830 struct interval_tree_node *node;
831 struct svm_range *prange;
833 pr_debug("dump svms 0x%p list\n", svms);
834 pr_debug("range\tstart\tpage\tend\t\tlocation\n");
836 list_for_each_entry(prange, &svms->list, list) {
837 pr_debug("0x%p 0x%lx\t0x%llx\t0x%llx\t0x%x\n",
838 prange, prange->start, prange->npages,
839 prange->start + prange->npages - 1,
843 pr_debug("dump svms 0x%p interval tree\n", svms);
844 pr_debug("range\tstart\tpage\tend\t\tlocation\n");
845 node = interval_tree_iter_first(&svms->objects, 0, ~0ULL);
847 prange = container_of(node, struct svm_range, it_node);
848 pr_debug("0x%p 0x%lx\t0x%llx\t0x%llx\t0x%x\n",
849 prange, prange->start, prange->npages,
850 prange->start + prange->npages - 1,
852 node = interval_tree_iter_next(node, 0, ~0ULL);
857 svm_range_split_array(void *ppnew, void *ppold, size_t size,
858 uint64_t old_start, uint64_t old_n,
859 uint64_t new_start, uint64_t new_n)
861 unsigned char *new, *old, *pold;
866 pold = *(unsigned char **)ppold;
870 new = kvmalloc_array(new_n, size, GFP_KERNEL);
874 d = (new_start - old_start) * size;
875 memcpy(new, pold + d, new_n * size);
877 old = kvmalloc_array(old_n, size, GFP_KERNEL);
883 d = (new_start == old_start) ? new_n * size : 0;
884 memcpy(old, pold + d, old_n * size);
887 *(void **)ppold = old;
888 *(void **)ppnew = new;
894 svm_range_split_pages(struct svm_range *new, struct svm_range *old,
895 uint64_t start, uint64_t last)
897 uint64_t npages = last - start + 1;
900 for (i = 0; i < MAX_GPU_INSTANCE; i++) {
901 r = svm_range_split_array(&new->dma_addr[i], &old->dma_addr[i],
902 sizeof(*old->dma_addr[i]), old->start,
903 npages, new->start, new->npages);
912 svm_range_split_nodes(struct svm_range *new, struct svm_range *old,
913 uint64_t start, uint64_t last)
915 uint64_t npages = last - start + 1;
917 pr_debug("svms 0x%p new prange 0x%p start 0x%lx [0x%llx 0x%llx]\n",
918 new->svms, new, new->start, start, last);
920 if (new->start == old->start) {
921 new->offset = old->offset;
922 old->offset += new->npages;
924 new->offset = old->offset + npages;
927 new->svm_bo = svm_range_bo_ref(old->svm_bo);
928 new->ttm_res = old->ttm_res;
930 spin_lock(&new->svm_bo->list_lock);
931 list_add(&new->svm_bo_list, &new->svm_bo->range_list);
932 spin_unlock(&new->svm_bo->list_lock);
938 * svm_range_split_adjust - split range and adjust
941 * @old: the old range
942 * @start: the old range adjust to start address in pages
943 * @last: the old range adjust to last address in pages
945 * Copy system memory dma_addr or vram ttm_res in old range to new
946 * range from new_start up to size new->npages, the remaining old range is from
950 * 0 - OK, -ENOMEM - out of memory
953 svm_range_split_adjust(struct svm_range *new, struct svm_range *old,
954 uint64_t start, uint64_t last)
958 pr_debug("svms 0x%p new 0x%lx old [0x%lx 0x%lx] => [0x%llx 0x%llx]\n",
959 new->svms, new->start, old->start, old->last, start, last);
961 if (new->start < old->start ||
962 new->last > old->last) {
963 WARN_ONCE(1, "invalid new range start or last\n");
967 r = svm_range_split_pages(new, old, start, last);
971 if (old->actual_loc && old->ttm_res) {
972 r = svm_range_split_nodes(new, old, start, last);
977 old->npages = last - start + 1;
980 new->flags = old->flags;
981 new->preferred_loc = old->preferred_loc;
982 new->prefetch_loc = old->prefetch_loc;
983 new->actual_loc = old->actual_loc;
984 new->granularity = old->granularity;
985 new->mapped_to_gpu = old->mapped_to_gpu;
986 bitmap_copy(new->bitmap_access, old->bitmap_access, MAX_GPU_INSTANCE);
987 bitmap_copy(new->bitmap_aip, old->bitmap_aip, MAX_GPU_INSTANCE);
993 * svm_range_split - split a range in 2 ranges
995 * @prange: the svm range to split
996 * @start: the remaining range start address in pages
997 * @last: the remaining range last address in pages
998 * @new: the result new range generated
1001 * case 1: if start == prange->start
1002 * prange ==> prange[start, last]
1003 * new range [last + 1, prange->last]
1005 * case 2: if last == prange->last
1006 * prange ==> prange[start, last]
1007 * new range [prange->start, start - 1]
1010 * 0 - OK, -ENOMEM - out of memory, -EINVAL - invalid start, last
1013 svm_range_split(struct svm_range *prange, uint64_t start, uint64_t last,
1014 struct svm_range **new)
1016 uint64_t old_start = prange->start;
1017 uint64_t old_last = prange->last;
1018 struct svm_range_list *svms;
1021 pr_debug("svms 0x%p [0x%llx 0x%llx] to [0x%llx 0x%llx]\n", prange->svms,
1022 old_start, old_last, start, last);
1024 if (old_start != start && old_last != last)
1026 if (start < old_start || last > old_last)
1029 svms = prange->svms;
1030 if (old_start == start)
1031 *new = svm_range_new(svms, last + 1, old_last, false);
1033 *new = svm_range_new(svms, old_start, start - 1, false);
1037 r = svm_range_split_adjust(*new, prange, start, last);
1039 pr_debug("failed %d split [0x%llx 0x%llx] to [0x%llx 0x%llx]\n",
1040 r, old_start, old_last, start, last);
1041 svm_range_free(*new, false);
1049 svm_range_split_tail(struct svm_range *prange,
1050 uint64_t new_last, struct list_head *insert_list)
1052 struct svm_range *tail;
1053 int r = svm_range_split(prange, prange->start, new_last, &tail);
1056 list_add(&tail->list, insert_list);
1061 svm_range_split_head(struct svm_range *prange,
1062 uint64_t new_start, struct list_head *insert_list)
1064 struct svm_range *head;
1065 int r = svm_range_split(prange, new_start, prange->last, &head);
1068 list_add(&head->list, insert_list);
1073 svm_range_add_child(struct svm_range *prange, struct mm_struct *mm,
1074 struct svm_range *pchild, enum svm_work_list_ops op)
1076 pr_debug("add child 0x%p [0x%lx 0x%lx] to prange 0x%p child list %d\n",
1077 pchild, pchild->start, pchild->last, prange, op);
1079 pchild->work_item.mm = mm;
1080 pchild->work_item.op = op;
1081 list_add_tail(&pchild->child_list, &prange->child_list);
1085 * svm_range_split_by_granularity - collect ranges within granularity boundary
1087 * @p: the process with svms list
1089 * @addr: the vm fault address in pages, to split the prange
1090 * @parent: parent range if prange is from child list
1091 * @prange: prange to split
1093 * Trims @prange to be a single aligned block of prange->granularity if
1094 * possible. The head and tail are added to the child_list in @parent.
1096 * Context: caller must hold mmap_read_lock and prange->lock
1099 * 0 - OK, otherwise error code
1102 svm_range_split_by_granularity(struct kfd_process *p, struct mm_struct *mm,
1103 unsigned long addr, struct svm_range *parent,
1104 struct svm_range *prange)
1106 struct svm_range *head, *tail;
1107 unsigned long start, last, size;
1110 /* Align splited range start and size to granularity size, then a single
1111 * PTE will be used for whole range, this reduces the number of PTE
1112 * updated and the L1 TLB space used for translation.
1114 size = 1UL << prange->granularity;
1115 start = ALIGN_DOWN(addr, size);
1116 last = ALIGN(addr + 1, size) - 1;
1118 pr_debug("svms 0x%p split [0x%lx 0x%lx] to [0x%lx 0x%lx] size 0x%lx\n",
1119 prange->svms, prange->start, prange->last, start, last, size);
1121 if (start > prange->start) {
1122 r = svm_range_split(prange, start, prange->last, &head);
1125 svm_range_add_child(parent, mm, head, SVM_OP_ADD_RANGE);
1128 if (last < prange->last) {
1129 r = svm_range_split(prange, prange->start, last, &tail);
1132 svm_range_add_child(parent, mm, tail, SVM_OP_ADD_RANGE);
1135 /* xnack on, update mapping on GPUs with ACCESS_IN_PLACE */
1136 if (p->xnack_enabled && prange->work_item.op == SVM_OP_ADD_RANGE) {
1137 prange->work_item.op = SVM_OP_ADD_RANGE_AND_MAP;
1138 pr_debug("change prange 0x%p [0x%lx 0x%lx] op %d\n",
1139 prange, prange->start, prange->last,
1140 SVM_OP_ADD_RANGE_AND_MAP);
1145 svm_nodes_in_same_hive(struct kfd_node *node_a, struct kfd_node *node_b)
1147 return (node_a->adev == node_b->adev ||
1148 amdgpu_xgmi_same_hive(node_a->adev, node_b->adev));
1152 svm_range_get_pte_flags(struct kfd_node *node,
1153 struct svm_range *prange, int domain)
1155 struct kfd_node *bo_node;
1156 uint32_t flags = prange->flags;
1157 uint32_t mapping_flags = 0;
1159 bool snoop = (domain != SVM_RANGE_VRAM_DOMAIN);
1160 bool coherent = flags & KFD_IOCTL_SVM_FLAG_COHERENT;
1161 bool uncached = false; /*flags & KFD_IOCTL_SVM_FLAG_UNCACHED;*/
1162 unsigned int mtype_local;
1164 if (domain == SVM_RANGE_VRAM_DOMAIN)
1165 bo_node = prange->svm_bo->node;
1167 switch (node->adev->ip_versions[GC_HWIP][0]) {
1168 case IP_VERSION(9, 4, 1):
1169 if (domain == SVM_RANGE_VRAM_DOMAIN) {
1170 if (bo_node == node) {
1171 mapping_flags |= coherent ?
1172 AMDGPU_VM_MTYPE_CC : AMDGPU_VM_MTYPE_RW;
1174 mapping_flags |= coherent ?
1175 AMDGPU_VM_MTYPE_UC : AMDGPU_VM_MTYPE_NC;
1176 if (svm_nodes_in_same_hive(node, bo_node))
1180 mapping_flags |= coherent ?
1181 AMDGPU_VM_MTYPE_UC : AMDGPU_VM_MTYPE_NC;
1184 case IP_VERSION(9, 4, 2):
1185 if (domain == SVM_RANGE_VRAM_DOMAIN) {
1186 if (bo_node == node) {
1187 mapping_flags |= coherent ?
1188 AMDGPU_VM_MTYPE_CC : AMDGPU_VM_MTYPE_RW;
1189 if (node->adev->gmc.xgmi.connected_to_cpu)
1192 mapping_flags |= coherent ?
1193 AMDGPU_VM_MTYPE_UC : AMDGPU_VM_MTYPE_NC;
1194 if (svm_nodes_in_same_hive(node, bo_node))
1198 mapping_flags |= coherent ?
1199 AMDGPU_VM_MTYPE_UC : AMDGPU_VM_MTYPE_NC;
1202 case IP_VERSION(9, 4, 3):
1203 mtype_local = amdgpu_mtype_local == 1 ? AMDGPU_VM_MTYPE_NC :
1204 (amdgpu_mtype_local == 2 ? AMDGPU_VM_MTYPE_CC : AMDGPU_VM_MTYPE_RW);
1207 mapping_flags |= AMDGPU_VM_MTYPE_UC;
1208 } else if (domain == SVM_RANGE_VRAM_DOMAIN) {
1209 /* local HBM region close to partition */
1210 if (bo_node->adev == node->adev &&
1211 (!bo_node->xcp || !node->xcp || bo_node->xcp->mem_id == node->xcp->mem_id))
1212 mapping_flags |= mtype_local;
1213 /* local HBM region far from partition or remote XGMI GPU */
1214 else if (svm_nodes_in_same_hive(bo_node, node))
1215 mapping_flags |= AMDGPU_VM_MTYPE_NC;
1218 mapping_flags |= AMDGPU_VM_MTYPE_UC;
1219 /* system memory accessed by the APU */
1220 } else if (node->adev->flags & AMD_IS_APU) {
1221 /* On NUMA systems, locality is determined per-page
1222 * in amdgpu_gmc_override_vm_pte_flags
1224 if (num_possible_nodes() <= 1)
1225 mapping_flags |= mtype_local;
1227 mapping_flags |= AMDGPU_VM_MTYPE_NC;
1228 /* system memory accessed by the dGPU */
1230 mapping_flags |= AMDGPU_VM_MTYPE_UC;
1234 mapping_flags |= coherent ?
1235 AMDGPU_VM_MTYPE_UC : AMDGPU_VM_MTYPE_NC;
1238 mapping_flags |= AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_PAGE_WRITEABLE;
1240 if (flags & KFD_IOCTL_SVM_FLAG_GPU_RO)
1241 mapping_flags &= ~AMDGPU_VM_PAGE_WRITEABLE;
1242 if (flags & KFD_IOCTL_SVM_FLAG_GPU_EXEC)
1243 mapping_flags |= AMDGPU_VM_PAGE_EXECUTABLE;
1245 pte_flags = AMDGPU_PTE_VALID;
1246 pte_flags |= (domain == SVM_RANGE_VRAM_DOMAIN) ? 0 : AMDGPU_PTE_SYSTEM;
1247 pte_flags |= snoop ? AMDGPU_PTE_SNOOPED : 0;
1249 pte_flags |= amdgpu_gem_va_map_flags(node->adev, mapping_flags);
1254 svm_range_unmap_from_gpu(struct amdgpu_device *adev, struct amdgpu_vm *vm,
1255 uint64_t start, uint64_t last,
1256 struct dma_fence **fence)
1258 uint64_t init_pte_value = 0;
1260 pr_debug("[0x%llx 0x%llx]\n", start, last);
1262 return amdgpu_vm_update_range(adev, vm, false, true, true, NULL, start,
1263 last, init_pte_value, 0, 0, NULL, NULL,
1268 svm_range_unmap_from_gpus(struct svm_range *prange, unsigned long start,
1269 unsigned long last, uint32_t trigger)
1271 DECLARE_BITMAP(bitmap, MAX_GPU_INSTANCE);
1272 struct kfd_process_device *pdd;
1273 struct dma_fence *fence = NULL;
1274 struct kfd_process *p;
1278 if (!prange->mapped_to_gpu) {
1279 pr_debug("prange 0x%p [0x%lx 0x%lx] not mapped to GPU\n",
1280 prange, prange->start, prange->last);
1284 if (prange->start == start && prange->last == last) {
1285 pr_debug("unmap svms 0x%p prange 0x%p\n", prange->svms, prange);
1286 prange->mapped_to_gpu = false;
1289 bitmap_or(bitmap, prange->bitmap_access, prange->bitmap_aip,
1291 p = container_of(prange->svms, struct kfd_process, svms);
1293 for_each_set_bit(gpuidx, bitmap, MAX_GPU_INSTANCE) {
1294 pr_debug("unmap from gpu idx 0x%x\n", gpuidx);
1295 pdd = kfd_process_device_from_gpuidx(p, gpuidx);
1297 pr_debug("failed to find device idx %d\n", gpuidx);
1301 kfd_smi_event_unmap_from_gpu(pdd->dev, p->lead_thread->pid,
1302 start, last, trigger);
1304 r = svm_range_unmap_from_gpu(pdd->dev->adev,
1305 drm_priv_to_vm(pdd->drm_priv),
1306 start, last, &fence);
1311 r = dma_fence_wait(fence, false);
1312 dma_fence_put(fence);
1317 kfd_flush_tlb(pdd, TLB_FLUSH_HEAVYWEIGHT);
1324 svm_range_map_to_gpu(struct kfd_process_device *pdd, struct svm_range *prange,
1325 unsigned long offset, unsigned long npages, bool readonly,
1326 dma_addr_t *dma_addr, struct amdgpu_device *bo_adev,
1327 struct dma_fence **fence, bool flush_tlb)
1329 struct amdgpu_device *adev = pdd->dev->adev;
1330 struct amdgpu_vm *vm = drm_priv_to_vm(pdd->drm_priv);
1332 unsigned long last_start;
1337 last_start = prange->start + offset;
1339 pr_debug("svms 0x%p [0x%lx 0x%lx] readonly %d\n", prange->svms,
1340 last_start, last_start + npages - 1, readonly);
1342 for (i = offset; i < offset + npages; i++) {
1343 last_domain = dma_addr[i] & SVM_RANGE_VRAM_DOMAIN;
1344 dma_addr[i] &= ~SVM_RANGE_VRAM_DOMAIN;
1346 /* Collect all pages in the same address range and memory domain
1347 * that can be mapped with a single call to update mapping.
1349 if (i < offset + npages - 1 &&
1350 last_domain == (dma_addr[i + 1] & SVM_RANGE_VRAM_DOMAIN))
1353 pr_debug("Mapping range [0x%lx 0x%llx] on domain: %s\n",
1354 last_start, prange->start + i, last_domain ? "GPU" : "CPU");
1356 pte_flags = svm_range_get_pte_flags(pdd->dev, prange, last_domain);
1358 pte_flags &= ~AMDGPU_PTE_WRITEABLE;
1360 pr_debug("svms 0x%p map [0x%lx 0x%llx] vram %d PTE 0x%llx\n",
1361 prange->svms, last_start, prange->start + i,
1362 (last_domain == SVM_RANGE_VRAM_DOMAIN) ? 1 : 0,
1365 /* For dGPU mode, we use same vm_manager to allocate VRAM for
1366 * different memory partition based on fpfn/lpfn, we should use
1367 * same vm_manager.vram_base_offset regardless memory partition.
1369 r = amdgpu_vm_update_range(adev, vm, false, false, flush_tlb, NULL,
1370 last_start, prange->start + i,
1372 (last_start - prange->start) << PAGE_SHIFT,
1373 bo_adev ? bo_adev->vm_manager.vram_base_offset : 0,
1374 NULL, dma_addr, &vm->last_update);
1376 for (j = last_start - prange->start; j <= i; j++)
1377 dma_addr[j] |= last_domain;
1380 pr_debug("failed %d to map to gpu 0x%lx\n", r, prange->start);
1383 last_start = prange->start + i + 1;
1386 r = amdgpu_vm_update_pdes(adev, vm, false);
1388 pr_debug("failed %d to update directories 0x%lx\n", r,
1394 *fence = dma_fence_get(vm->last_update);
1401 svm_range_map_to_gpus(struct svm_range *prange, unsigned long offset,
1402 unsigned long npages, bool readonly,
1403 unsigned long *bitmap, bool wait, bool flush_tlb)
1405 struct kfd_process_device *pdd;
1406 struct amdgpu_device *bo_adev = NULL;
1407 struct kfd_process *p;
1408 struct dma_fence *fence = NULL;
1412 if (prange->svm_bo && prange->ttm_res)
1413 bo_adev = prange->svm_bo->node->adev;
1415 p = container_of(prange->svms, struct kfd_process, svms);
1416 for_each_set_bit(gpuidx, bitmap, MAX_GPU_INSTANCE) {
1417 pr_debug("mapping to gpu idx 0x%x\n", gpuidx);
1418 pdd = kfd_process_device_from_gpuidx(p, gpuidx);
1420 pr_debug("failed to find device idx %d\n", gpuidx);
1424 pdd = kfd_bind_process_to_device(pdd->dev, p);
1428 if (bo_adev && pdd->dev->adev != bo_adev &&
1429 !amdgpu_xgmi_same_hive(pdd->dev->adev, bo_adev)) {
1430 pr_debug("cannot map to device idx %d\n", gpuidx);
1434 r = svm_range_map_to_gpu(pdd, prange, offset, npages, readonly,
1435 prange->dma_addr[gpuidx],
1436 bo_adev, wait ? &fence : NULL,
1442 r = dma_fence_wait(fence, false);
1443 dma_fence_put(fence);
1446 pr_debug("failed %d to dma fence wait\n", r);
1451 kfd_flush_tlb(pdd, TLB_FLUSH_LEGACY);
1457 struct svm_validate_context {
1458 struct kfd_process *process;
1459 struct svm_range *prange;
1461 DECLARE_BITMAP(bitmap, MAX_GPU_INSTANCE);
1462 struct drm_exec exec;
1465 static int svm_range_reserve_bos(struct svm_validate_context *ctx, bool intr)
1467 struct kfd_process_device *pdd;
1468 struct amdgpu_vm *vm;
1472 drm_exec_init(&ctx->exec, intr ? DRM_EXEC_INTERRUPTIBLE_WAIT: 0);
1473 drm_exec_until_all_locked(&ctx->exec) {
1474 for_each_set_bit(gpuidx, ctx->bitmap, MAX_GPU_INSTANCE) {
1475 pdd = kfd_process_device_from_gpuidx(ctx->process, gpuidx);
1477 pr_debug("failed to find device idx %d\n", gpuidx);
1481 vm = drm_priv_to_vm(pdd->drm_priv);
1483 r = amdgpu_vm_lock_pd(vm, &ctx->exec, 2);
1484 drm_exec_retry_on_contention(&ctx->exec);
1486 pr_debug("failed %d to reserve bo\n", r);
1492 for_each_set_bit(gpuidx, ctx->bitmap, MAX_GPU_INSTANCE) {
1493 pdd = kfd_process_device_from_gpuidx(ctx->process, gpuidx);
1495 pr_debug("failed to find device idx %d\n", gpuidx);
1500 r = amdgpu_vm_validate_pt_bos(pdd->dev->adev,
1501 drm_priv_to_vm(pdd->drm_priv),
1502 svm_range_bo_validate, NULL);
1504 pr_debug("failed %d validate pt bos\n", r);
1512 drm_exec_fini(&ctx->exec);
1516 static void svm_range_unreserve_bos(struct svm_validate_context *ctx)
1518 drm_exec_fini(&ctx->exec);
1521 static void *kfd_svm_page_owner(struct kfd_process *p, int32_t gpuidx)
1523 struct kfd_process_device *pdd;
1525 pdd = kfd_process_device_from_gpuidx(p, gpuidx);
1529 return SVM_ADEV_PGMAP_OWNER(pdd->dev->adev);
1533 * Validation+GPU mapping with concurrent invalidation (MMU notifiers)
1535 * To prevent concurrent destruction or change of range attributes, the
1536 * svm_read_lock must be held. The caller must not hold the svm_write_lock
1537 * because that would block concurrent evictions and lead to deadlocks. To
1538 * serialize concurrent migrations or validations of the same range, the
1539 * prange->migrate_mutex must be held.
1541 * For VRAM ranges, the SVM BO must be allocated and valid (protected by its
1544 * The following sequence ensures race-free validation and GPU mapping:
1546 * 1. Reserve page table (and SVM BO if range is in VRAM)
1547 * 2. hmm_range_fault to get page addresses (if system memory)
1548 * 3. DMA-map pages (if system memory)
1549 * 4-a. Take notifier lock
1550 * 4-b. Check that pages still valid (mmu_interval_read_retry)
1551 * 4-c. Check that the range was not split or otherwise invalidated
1552 * 4-d. Update GPU page table
1553 * 4.e. Release notifier lock
1554 * 5. Release page table (and SVM BO) reservation
1556 static int svm_range_validate_and_map(struct mm_struct *mm,
1557 struct svm_range *prange, int32_t gpuidx,
1558 bool intr, bool wait, bool flush_tlb)
1560 struct svm_validate_context *ctx;
1561 unsigned long start, end, addr;
1562 struct kfd_process *p;
1567 ctx = kzalloc(sizeof(struct svm_validate_context), GFP_KERNEL);
1570 ctx->process = container_of(prange->svms, struct kfd_process, svms);
1571 ctx->prange = prange;
1574 if (gpuidx < MAX_GPU_INSTANCE) {
1575 bitmap_zero(ctx->bitmap, MAX_GPU_INSTANCE);
1576 bitmap_set(ctx->bitmap, gpuidx, 1);
1577 } else if (ctx->process->xnack_enabled) {
1578 bitmap_copy(ctx->bitmap, prange->bitmap_aip, MAX_GPU_INSTANCE);
1580 /* If prefetch range to GPU, or GPU retry fault migrate range to
1581 * GPU, which has ACCESS attribute to the range, create mapping
1584 if (prange->actual_loc) {
1585 gpuidx = kfd_process_gpuidx_from_gpuid(ctx->process,
1586 prange->actual_loc);
1588 WARN_ONCE(1, "failed get device by id 0x%x\n",
1589 prange->actual_loc);
1593 if (test_bit(gpuidx, prange->bitmap_access))
1594 bitmap_set(ctx->bitmap, gpuidx, 1);
1597 bitmap_or(ctx->bitmap, prange->bitmap_access,
1598 prange->bitmap_aip, MAX_GPU_INSTANCE);
1601 if (bitmap_empty(ctx->bitmap, MAX_GPU_INSTANCE)) {
1602 bitmap_copy(ctx->bitmap, prange->bitmap_access, MAX_GPU_INSTANCE);
1603 if (!prange->mapped_to_gpu ||
1604 bitmap_empty(ctx->bitmap, MAX_GPU_INSTANCE)) {
1610 if (prange->actual_loc && !prange->ttm_res) {
1611 /* This should never happen. actual_loc gets set by
1612 * svm_migrate_ram_to_vram after allocating a BO.
1614 WARN_ONCE(1, "VRAM BO missing during validation\n");
1619 svm_range_reserve_bos(ctx, intr);
1621 p = container_of(prange->svms, struct kfd_process, svms);
1622 owner = kfd_svm_page_owner(p, find_first_bit(ctx->bitmap,
1624 for_each_set_bit(idx, ctx->bitmap, MAX_GPU_INSTANCE) {
1625 if (kfd_svm_page_owner(p, idx) != owner) {
1631 start = prange->start << PAGE_SHIFT;
1632 end = (prange->last + 1) << PAGE_SHIFT;
1633 for (addr = start; addr < end && !r; ) {
1634 struct hmm_range *hmm_range;
1635 struct vm_area_struct *vma;
1637 unsigned long offset;
1638 unsigned long npages;
1641 vma = vma_lookup(mm, addr);
1646 readonly = !(vma->vm_flags & VM_WRITE);
1648 next = min(vma->vm_end, end);
1649 npages = (next - addr) >> PAGE_SHIFT;
1650 WRITE_ONCE(p->svms.faulting_task, current);
1651 r = amdgpu_hmm_range_get_pages(&prange->notifier, addr, npages,
1652 readonly, owner, NULL,
1654 WRITE_ONCE(p->svms.faulting_task, NULL);
1656 pr_debug("failed %d to get svm range pages\n", r);
1660 offset = (addr - start) >> PAGE_SHIFT;
1661 r = svm_range_dma_map(prange, ctx->bitmap, offset, npages,
1662 hmm_range->hmm_pfns);
1664 pr_debug("failed %d to dma map range\n", r);
1668 svm_range_lock(prange);
1669 if (amdgpu_hmm_range_get_pages_done(hmm_range)) {
1670 pr_debug("hmm update the range, need validate again\n");
1674 if (!list_empty(&prange->child_list)) {
1675 pr_debug("range split by unmap in parallel, validate again\n");
1680 r = svm_range_map_to_gpus(prange, offset, npages, readonly,
1681 ctx->bitmap, wait, flush_tlb);
1684 svm_range_unlock(prange);
1690 prange->validated_once = true;
1691 prange->mapped_to_gpu = true;
1695 svm_range_unreserve_bos(ctx);
1697 prange->is_error_flag = !!r;
1699 prange->validate_timestamp = ktime_get_boottime();
1708 * svm_range_list_lock_and_flush_work - flush pending deferred work
1710 * @svms: the svm range list
1711 * @mm: the mm structure
1713 * Context: Returns with mmap write lock held, pending deferred work flushed
1717 svm_range_list_lock_and_flush_work(struct svm_range_list *svms,
1718 struct mm_struct *mm)
1721 flush_work(&svms->deferred_list_work);
1722 mmap_write_lock(mm);
1724 if (list_empty(&svms->deferred_range_list))
1726 mmap_write_unlock(mm);
1727 pr_debug("retry flush\n");
1728 goto retry_flush_work;
1731 static void svm_range_restore_work(struct work_struct *work)
1733 struct delayed_work *dwork = to_delayed_work(work);
1734 struct amdkfd_process_info *process_info;
1735 struct svm_range_list *svms;
1736 struct svm_range *prange;
1737 struct kfd_process *p;
1738 struct mm_struct *mm;
1743 svms = container_of(dwork, struct svm_range_list, restore_work);
1744 evicted_ranges = atomic_read(&svms->evicted_ranges);
1745 if (!evicted_ranges)
1748 pr_debug("restore svm ranges\n");
1750 p = container_of(svms, struct kfd_process, svms);
1751 process_info = p->kgd_process_info;
1753 /* Keep mm reference when svm_range_validate_and_map ranges */
1754 mm = get_task_mm(p->lead_thread);
1756 pr_debug("svms 0x%p process mm gone\n", svms);
1760 mutex_lock(&process_info->lock);
1761 svm_range_list_lock_and_flush_work(svms, mm);
1762 mutex_lock(&svms->lock);
1764 evicted_ranges = atomic_read(&svms->evicted_ranges);
1766 list_for_each_entry(prange, &svms->list, list) {
1767 invalid = atomic_read(&prange->invalid);
1771 pr_debug("restoring svms 0x%p prange 0x%p [0x%lx %lx] inv %d\n",
1772 prange->svms, prange, prange->start, prange->last,
1776 * If range is migrating, wait for migration is done.
1778 mutex_lock(&prange->migrate_mutex);
1780 r = svm_range_validate_and_map(mm, prange, MAX_GPU_INSTANCE,
1781 false, true, false);
1783 pr_debug("failed %d to map 0x%lx to gpus\n", r,
1786 mutex_unlock(&prange->migrate_mutex);
1788 goto out_reschedule;
1790 if (atomic_cmpxchg(&prange->invalid, invalid, 0) != invalid)
1791 goto out_reschedule;
1794 if (atomic_cmpxchg(&svms->evicted_ranges, evicted_ranges, 0) !=
1796 goto out_reschedule;
1800 r = kgd2kfd_resume_mm(mm);
1802 /* No recovery from this failure. Probably the CP is
1803 * hanging. No point trying again.
1805 pr_debug("failed %d to resume KFD\n", r);
1808 pr_debug("restore svm ranges successfully\n");
1811 mutex_unlock(&svms->lock);
1812 mmap_write_unlock(mm);
1813 mutex_unlock(&process_info->lock);
1815 /* If validation failed, reschedule another attempt */
1816 if (evicted_ranges) {
1817 pr_debug("reschedule to restore svm range\n");
1818 schedule_delayed_work(&svms->restore_work,
1819 msecs_to_jiffies(AMDGPU_SVM_RANGE_RESTORE_DELAY_MS));
1821 kfd_smi_event_queue_restore_rescheduled(mm);
1827 * svm_range_evict - evict svm range
1828 * @prange: svm range structure
1829 * @mm: current process mm_struct
1830 * @start: starting process queue number
1831 * @last: last process queue number
1832 * @event: mmu notifier event when range is evicted or migrated
1834 * Stop all queues of the process to ensure GPU doesn't access the memory, then
1835 * return to let CPU evict the buffer and proceed CPU pagetable update.
1837 * Don't need use lock to sync cpu pagetable invalidation with GPU execution.
1838 * If invalidation happens while restore work is running, restore work will
1839 * restart to ensure to get the latest CPU pages mapping to GPU, then start
1843 svm_range_evict(struct svm_range *prange, struct mm_struct *mm,
1844 unsigned long start, unsigned long last,
1845 enum mmu_notifier_event event)
1847 struct svm_range_list *svms = prange->svms;
1848 struct svm_range *pchild;
1849 struct kfd_process *p;
1852 p = container_of(svms, struct kfd_process, svms);
1854 pr_debug("invalidate svms 0x%p prange [0x%lx 0x%lx] [0x%lx 0x%lx]\n",
1855 svms, prange->start, prange->last, start, last);
1857 if (!p->xnack_enabled ||
1858 (prange->flags & KFD_IOCTL_SVM_FLAG_GPU_ALWAYS_MAPPED)) {
1860 bool mapped = prange->mapped_to_gpu;
1862 list_for_each_entry(pchild, &prange->child_list, child_list) {
1863 if (!pchild->mapped_to_gpu)
1866 mutex_lock_nested(&pchild->lock, 1);
1867 if (pchild->start <= last && pchild->last >= start) {
1868 pr_debug("increment pchild invalid [0x%lx 0x%lx]\n",
1869 pchild->start, pchild->last);
1870 atomic_inc(&pchild->invalid);
1872 mutex_unlock(&pchild->lock);
1878 if (prange->start <= last && prange->last >= start)
1879 atomic_inc(&prange->invalid);
1881 evicted_ranges = atomic_inc_return(&svms->evicted_ranges);
1882 if (evicted_ranges != 1)
1885 pr_debug("evicting svms 0x%p range [0x%lx 0x%lx]\n",
1886 prange->svms, prange->start, prange->last);
1888 /* First eviction, stop the queues */
1889 r = kgd2kfd_quiesce_mm(mm, KFD_QUEUE_EVICTION_TRIGGER_SVM);
1891 pr_debug("failed to quiesce KFD\n");
1893 pr_debug("schedule to restore svm %p ranges\n", svms);
1894 schedule_delayed_work(&svms->restore_work,
1895 msecs_to_jiffies(AMDGPU_SVM_RANGE_RESTORE_DELAY_MS));
1900 if (event == MMU_NOTIFY_MIGRATE)
1901 trigger = KFD_SVM_UNMAP_TRIGGER_MMU_NOTIFY_MIGRATE;
1903 trigger = KFD_SVM_UNMAP_TRIGGER_MMU_NOTIFY;
1905 pr_debug("invalidate unmap svms 0x%p [0x%lx 0x%lx] from GPUs\n",
1906 prange->svms, start, last);
1907 list_for_each_entry(pchild, &prange->child_list, child_list) {
1908 mutex_lock_nested(&pchild->lock, 1);
1909 s = max(start, pchild->start);
1910 l = min(last, pchild->last);
1912 svm_range_unmap_from_gpus(pchild, s, l, trigger);
1913 mutex_unlock(&pchild->lock);
1915 s = max(start, prange->start);
1916 l = min(last, prange->last);
1918 svm_range_unmap_from_gpus(prange, s, l, trigger);
1924 static struct svm_range *svm_range_clone(struct svm_range *old)
1926 struct svm_range *new;
1928 new = svm_range_new(old->svms, old->start, old->last, false);
1933 new->ttm_res = old->ttm_res;
1934 new->offset = old->offset;
1935 new->svm_bo = svm_range_bo_ref(old->svm_bo);
1936 spin_lock(&new->svm_bo->list_lock);
1937 list_add(&new->svm_bo_list, &new->svm_bo->range_list);
1938 spin_unlock(&new->svm_bo->list_lock);
1940 new->flags = old->flags;
1941 new->preferred_loc = old->preferred_loc;
1942 new->prefetch_loc = old->prefetch_loc;
1943 new->actual_loc = old->actual_loc;
1944 new->granularity = old->granularity;
1945 new->mapped_to_gpu = old->mapped_to_gpu;
1946 bitmap_copy(new->bitmap_access, old->bitmap_access, MAX_GPU_INSTANCE);
1947 bitmap_copy(new->bitmap_aip, old->bitmap_aip, MAX_GPU_INSTANCE);
1952 void svm_range_set_max_pages(struct amdgpu_device *adev)
1955 uint64_t pages, _pages;
1956 uint64_t min_pages = 0;
1959 for (i = 0; i < adev->kfd.dev->num_nodes; i++) {
1960 if (adev->kfd.dev->nodes[i]->xcp)
1961 id = adev->kfd.dev->nodes[i]->xcp->id;
1964 pages = KFD_XCP_MEMORY_SIZE(adev, id) >> 17;
1965 pages = clamp(pages, 1ULL << 9, 1ULL << 18);
1966 pages = rounddown_pow_of_two(pages);
1967 min_pages = min_not_zero(min_pages, pages);
1971 max_pages = READ_ONCE(max_svm_range_pages);
1972 _pages = min_not_zero(max_pages, min_pages);
1973 } while (cmpxchg(&max_svm_range_pages, max_pages, _pages) != max_pages);
1977 svm_range_split_new(struct svm_range_list *svms, uint64_t start, uint64_t last,
1978 uint64_t max_pages, struct list_head *insert_list,
1979 struct list_head *update_list)
1981 struct svm_range *prange;
1984 pr_debug("max_svm_range_pages 0x%llx adding [0x%llx 0x%llx]\n",
1985 max_pages, start, last);
1987 while (last >= start) {
1988 l = min(last, ALIGN_DOWN(start + max_pages, max_pages) - 1);
1990 prange = svm_range_new(svms, start, l, true);
1993 list_add(&prange->list, insert_list);
1994 list_add(&prange->update_list, update_list);
2002 * svm_range_add - add svm range and handle overlap
2003 * @p: the range add to this process svms
2004 * @start: page size aligned
2005 * @size: page size aligned
2006 * @nattr: number of attributes
2007 * @attrs: array of attributes
2008 * @update_list: output, the ranges need validate and update GPU mapping
2009 * @insert_list: output, the ranges need insert to svms
2010 * @remove_list: output, the ranges are replaced and need remove from svms
2012 * Check if the virtual address range has overlap with any existing ranges,
2013 * split partly overlapping ranges and add new ranges in the gaps. All changes
2014 * should be applied to the range_list and interval tree transactionally. If
2015 * any range split or allocation fails, the entire update fails. Therefore any
2016 * existing overlapping svm_ranges are cloned and the original svm_ranges left
2019 * If the transaction succeeds, the caller can update and insert clones and
2020 * new ranges, then free the originals.
2022 * Otherwise the caller can free the clones and new ranges, while the old
2023 * svm_ranges remain unchanged.
2025 * Context: Process context, caller must hold svms->lock
2028 * 0 - OK, otherwise error code
2031 svm_range_add(struct kfd_process *p, uint64_t start, uint64_t size,
2032 uint32_t nattr, struct kfd_ioctl_svm_attribute *attrs,
2033 struct list_head *update_list, struct list_head *insert_list,
2034 struct list_head *remove_list)
2036 unsigned long last = start + size - 1UL;
2037 struct svm_range_list *svms = &p->svms;
2038 struct interval_tree_node *node;
2039 struct svm_range *prange;
2040 struct svm_range *tmp;
2041 struct list_head new_list;
2044 pr_debug("svms 0x%p [0x%llx 0x%lx]\n", &p->svms, start, last);
2046 INIT_LIST_HEAD(update_list);
2047 INIT_LIST_HEAD(insert_list);
2048 INIT_LIST_HEAD(remove_list);
2049 INIT_LIST_HEAD(&new_list);
2051 node = interval_tree_iter_first(&svms->objects, start, last);
2053 struct interval_tree_node *next;
2054 unsigned long next_start;
2056 pr_debug("found overlap node [0x%lx 0x%lx]\n", node->start,
2059 prange = container_of(node, struct svm_range, it_node);
2060 next = interval_tree_iter_next(node, start, last);
2061 next_start = min(node->last, last) + 1;
2063 if (svm_range_is_same_attrs(p, prange, nattr, attrs)) {
2065 } else if (node->start < start || node->last > last) {
2066 /* node intersects the update range and its attributes
2067 * will change. Clone and split it, apply updates only
2068 * to the overlapping part
2070 struct svm_range *old = prange;
2072 prange = svm_range_clone(old);
2078 list_add(&old->update_list, remove_list);
2079 list_add(&prange->list, insert_list);
2080 list_add(&prange->update_list, update_list);
2082 if (node->start < start) {
2083 pr_debug("change old range start\n");
2084 r = svm_range_split_head(prange, start,
2089 if (node->last > last) {
2090 pr_debug("change old range last\n");
2091 r = svm_range_split_tail(prange, last,
2097 /* The node is contained within start..last,
2100 list_add(&prange->update_list, update_list);
2103 /* insert a new node if needed */
2104 if (node->start > start) {
2105 r = svm_range_split_new(svms, start, node->start - 1,
2106 READ_ONCE(max_svm_range_pages),
2107 &new_list, update_list);
2116 /* add a final range at the end if needed */
2118 r = svm_range_split_new(svms, start, last,
2119 READ_ONCE(max_svm_range_pages),
2120 &new_list, update_list);
2124 list_for_each_entry_safe(prange, tmp, insert_list, list)
2125 svm_range_free(prange, false);
2126 list_for_each_entry_safe(prange, tmp, &new_list, list)
2127 svm_range_free(prange, true);
2129 list_splice(&new_list, insert_list);
2136 svm_range_update_notifier_and_interval_tree(struct mm_struct *mm,
2137 struct svm_range *prange)
2139 unsigned long start;
2142 start = prange->notifier.interval_tree.start >> PAGE_SHIFT;
2143 last = prange->notifier.interval_tree.last >> PAGE_SHIFT;
2145 if (prange->start == start && prange->last == last)
2148 pr_debug("up notifier 0x%p prange 0x%p [0x%lx 0x%lx] [0x%lx 0x%lx]\n",
2149 prange->svms, prange, start, last, prange->start,
2152 if (start != 0 && last != 0) {
2153 interval_tree_remove(&prange->it_node, &prange->svms->objects);
2154 svm_range_remove_notifier(prange);
2156 prange->it_node.start = prange->start;
2157 prange->it_node.last = prange->last;
2159 interval_tree_insert(&prange->it_node, &prange->svms->objects);
2160 svm_range_add_notifier_locked(mm, prange);
2164 svm_range_handle_list_op(struct svm_range_list *svms, struct svm_range *prange,
2165 struct mm_struct *mm)
2167 switch (prange->work_item.op) {
2169 pr_debug("NULL OP 0x%p prange 0x%p [0x%lx 0x%lx]\n",
2170 svms, prange, prange->start, prange->last);
2172 case SVM_OP_UNMAP_RANGE:
2173 pr_debug("remove 0x%p prange 0x%p [0x%lx 0x%lx]\n",
2174 svms, prange, prange->start, prange->last);
2175 svm_range_unlink(prange);
2176 svm_range_remove_notifier(prange);
2177 svm_range_free(prange, true);
2179 case SVM_OP_UPDATE_RANGE_NOTIFIER:
2180 pr_debug("update notifier 0x%p prange 0x%p [0x%lx 0x%lx]\n",
2181 svms, prange, prange->start, prange->last);
2182 svm_range_update_notifier_and_interval_tree(mm, prange);
2184 case SVM_OP_UPDATE_RANGE_NOTIFIER_AND_MAP:
2185 pr_debug("update and map 0x%p prange 0x%p [0x%lx 0x%lx]\n",
2186 svms, prange, prange->start, prange->last);
2187 svm_range_update_notifier_and_interval_tree(mm, prange);
2188 /* TODO: implement deferred validation and mapping */
2190 case SVM_OP_ADD_RANGE:
2191 pr_debug("add 0x%p prange 0x%p [0x%lx 0x%lx]\n", svms, prange,
2192 prange->start, prange->last);
2193 svm_range_add_to_svms(prange);
2194 svm_range_add_notifier_locked(mm, prange);
2196 case SVM_OP_ADD_RANGE_AND_MAP:
2197 pr_debug("add and map 0x%p prange 0x%p [0x%lx 0x%lx]\n", svms,
2198 prange, prange->start, prange->last);
2199 svm_range_add_to_svms(prange);
2200 svm_range_add_notifier_locked(mm, prange);
2201 /* TODO: implement deferred validation and mapping */
2204 WARN_ONCE(1, "Unknown prange 0x%p work op %d\n", prange,
2205 prange->work_item.op);
2209 static void svm_range_drain_retry_fault(struct svm_range_list *svms)
2211 struct kfd_process_device *pdd;
2212 struct kfd_process *p;
2216 p = container_of(svms, struct kfd_process, svms);
2219 drain = atomic_read(&svms->drain_pagefaults);
2223 for_each_set_bit(i, svms->bitmap_supported, p->n_pdds) {
2228 pr_debug("drain retry fault gpu %d svms %p\n", i, svms);
2230 amdgpu_ih_wait_on_checkpoint_process_ts(pdd->dev->adev,
2231 pdd->dev->adev->irq.retry_cam_enabled ?
2232 &pdd->dev->adev->irq.ih :
2233 &pdd->dev->adev->irq.ih1);
2235 if (pdd->dev->adev->irq.retry_cam_enabled)
2236 amdgpu_ih_wait_on_checkpoint_process_ts(pdd->dev->adev,
2237 &pdd->dev->adev->irq.ih_soft);
2240 pr_debug("drain retry fault gpu %d svms 0x%p done\n", i, svms);
2242 if (atomic_cmpxchg(&svms->drain_pagefaults, drain, 0) != drain)
2246 static void svm_range_deferred_list_work(struct work_struct *work)
2248 struct svm_range_list *svms;
2249 struct svm_range *prange;
2250 struct mm_struct *mm;
2252 svms = container_of(work, struct svm_range_list, deferred_list_work);
2253 pr_debug("enter svms 0x%p\n", svms);
2255 spin_lock(&svms->deferred_list_lock);
2256 while (!list_empty(&svms->deferred_range_list)) {
2257 prange = list_first_entry(&svms->deferred_range_list,
2258 struct svm_range, deferred_list);
2259 spin_unlock(&svms->deferred_list_lock);
2261 pr_debug("prange 0x%p [0x%lx 0x%lx] op %d\n", prange,
2262 prange->start, prange->last, prange->work_item.op);
2264 mm = prange->work_item.mm;
2266 mmap_write_lock(mm);
2268 /* Checking for the need to drain retry faults must be inside
2269 * mmap write lock to serialize with munmap notifiers.
2271 if (unlikely(atomic_read(&svms->drain_pagefaults))) {
2272 mmap_write_unlock(mm);
2273 svm_range_drain_retry_fault(svms);
2277 /* Remove from deferred_list must be inside mmap write lock, for
2279 * 1. unmap_from_cpu may change work_item.op and add the range
2280 * to deferred_list again, cause use after free bug.
2281 * 2. svm_range_list_lock_and_flush_work may hold mmap write
2282 * lock and continue because deferred_list is empty, but
2283 * deferred_list work is actually waiting for mmap lock.
2285 spin_lock(&svms->deferred_list_lock);
2286 list_del_init(&prange->deferred_list);
2287 spin_unlock(&svms->deferred_list_lock);
2289 mutex_lock(&svms->lock);
2290 mutex_lock(&prange->migrate_mutex);
2291 while (!list_empty(&prange->child_list)) {
2292 struct svm_range *pchild;
2294 pchild = list_first_entry(&prange->child_list,
2295 struct svm_range, child_list);
2296 pr_debug("child prange 0x%p op %d\n", pchild,
2297 pchild->work_item.op);
2298 list_del_init(&pchild->child_list);
2299 svm_range_handle_list_op(svms, pchild, mm);
2301 mutex_unlock(&prange->migrate_mutex);
2303 svm_range_handle_list_op(svms, prange, mm);
2304 mutex_unlock(&svms->lock);
2305 mmap_write_unlock(mm);
2307 /* Pairs with mmget in svm_range_add_list_work */
2310 spin_lock(&svms->deferred_list_lock);
2312 spin_unlock(&svms->deferred_list_lock);
2313 pr_debug("exit svms 0x%p\n", svms);
2317 svm_range_add_list_work(struct svm_range_list *svms, struct svm_range *prange,
2318 struct mm_struct *mm, enum svm_work_list_ops op)
2320 spin_lock(&svms->deferred_list_lock);
2321 /* if prange is on the deferred list */
2322 if (!list_empty(&prange->deferred_list)) {
2323 pr_debug("update exist prange 0x%p work op %d\n", prange, op);
2324 WARN_ONCE(prange->work_item.mm != mm, "unmatch mm\n");
2325 if (op != SVM_OP_NULL &&
2326 prange->work_item.op != SVM_OP_UNMAP_RANGE)
2327 prange->work_item.op = op;
2329 prange->work_item.op = op;
2331 /* Pairs with mmput in deferred_list_work */
2333 prange->work_item.mm = mm;
2334 list_add_tail(&prange->deferred_list,
2335 &prange->svms->deferred_range_list);
2336 pr_debug("add prange 0x%p [0x%lx 0x%lx] to work list op %d\n",
2337 prange, prange->start, prange->last, op);
2339 spin_unlock(&svms->deferred_list_lock);
2342 void schedule_deferred_list_work(struct svm_range_list *svms)
2344 spin_lock(&svms->deferred_list_lock);
2345 if (!list_empty(&svms->deferred_range_list))
2346 schedule_work(&svms->deferred_list_work);
2347 spin_unlock(&svms->deferred_list_lock);
2351 svm_range_unmap_split(struct mm_struct *mm, struct svm_range *parent,
2352 struct svm_range *prange, unsigned long start,
2355 struct svm_range *head;
2356 struct svm_range *tail;
2358 if (prange->work_item.op == SVM_OP_UNMAP_RANGE) {
2359 pr_debug("prange 0x%p [0x%lx 0x%lx] is already freed\n", prange,
2360 prange->start, prange->last);
2363 if (start > prange->last || last < prange->start)
2366 head = tail = prange;
2367 if (start > prange->start)
2368 svm_range_split(prange, prange->start, start - 1, &tail);
2369 if (last < tail->last)
2370 svm_range_split(tail, last + 1, tail->last, &head);
2372 if (head != prange && tail != prange) {
2373 svm_range_add_child(parent, mm, head, SVM_OP_UNMAP_RANGE);
2374 svm_range_add_child(parent, mm, tail, SVM_OP_ADD_RANGE);
2375 } else if (tail != prange) {
2376 svm_range_add_child(parent, mm, tail, SVM_OP_UNMAP_RANGE);
2377 } else if (head != prange) {
2378 svm_range_add_child(parent, mm, head, SVM_OP_UNMAP_RANGE);
2379 } else if (parent != prange) {
2380 prange->work_item.op = SVM_OP_UNMAP_RANGE;
2385 svm_range_unmap_from_cpu(struct mm_struct *mm, struct svm_range *prange,
2386 unsigned long start, unsigned long last)
2388 uint32_t trigger = KFD_SVM_UNMAP_TRIGGER_UNMAP_FROM_CPU;
2389 struct svm_range_list *svms;
2390 struct svm_range *pchild;
2391 struct kfd_process *p;
2395 p = kfd_lookup_process_by_mm(mm);
2400 pr_debug("svms 0x%p prange 0x%p [0x%lx 0x%lx] [0x%lx 0x%lx]\n", svms,
2401 prange, prange->start, prange->last, start, last);
2403 /* Make sure pending page faults are drained in the deferred worker
2404 * before the range is freed to avoid straggler interrupts on
2405 * unmapped memory causing "phantom faults".
2407 atomic_inc(&svms->drain_pagefaults);
2409 unmap_parent = start <= prange->start && last >= prange->last;
2411 list_for_each_entry(pchild, &prange->child_list, child_list) {
2412 mutex_lock_nested(&pchild->lock, 1);
2413 s = max(start, pchild->start);
2414 l = min(last, pchild->last);
2416 svm_range_unmap_from_gpus(pchild, s, l, trigger);
2417 svm_range_unmap_split(mm, prange, pchild, start, last);
2418 mutex_unlock(&pchild->lock);
2420 s = max(start, prange->start);
2421 l = min(last, prange->last);
2423 svm_range_unmap_from_gpus(prange, s, l, trigger);
2424 svm_range_unmap_split(mm, prange, prange, start, last);
2427 svm_range_add_list_work(svms, prange, mm, SVM_OP_UNMAP_RANGE);
2429 svm_range_add_list_work(svms, prange, mm,
2430 SVM_OP_UPDATE_RANGE_NOTIFIER);
2431 schedule_deferred_list_work(svms);
2433 kfd_unref_process(p);
2437 * svm_range_cpu_invalidate_pagetables - interval notifier callback
2438 * @mni: mmu_interval_notifier struct
2439 * @range: mmu_notifier_range struct
2440 * @cur_seq: value to pass to mmu_interval_set_seq()
2442 * If event is MMU_NOTIFY_UNMAP, this is from CPU unmap range, otherwise, it
2443 * is from migration, or CPU page invalidation callback.
2445 * For unmap event, unmap range from GPUs, remove prange from svms in a delayed
2446 * work thread, and split prange if only part of prange is unmapped.
2448 * For invalidation event, if GPU retry fault is not enabled, evict the queues,
2449 * then schedule svm_range_restore_work to update GPU mapping and resume queues.
2450 * If GPU retry fault is enabled, unmap the svm range from GPU, retry fault will
2451 * update GPU mapping to recover.
2453 * Context: mmap lock, notifier_invalidate_start lock are held
2454 * for invalidate event, prange lock is held if this is from migration
2457 svm_range_cpu_invalidate_pagetables(struct mmu_interval_notifier *mni,
2458 const struct mmu_notifier_range *range,
2459 unsigned long cur_seq)
2461 struct svm_range *prange;
2462 unsigned long start;
2465 if (range->event == MMU_NOTIFY_RELEASE)
2467 if (!mmget_not_zero(mni->mm))
2470 start = mni->interval_tree.start;
2471 last = mni->interval_tree.last;
2472 start = max(start, range->start) >> PAGE_SHIFT;
2473 last = min(last, range->end - 1) >> PAGE_SHIFT;
2474 pr_debug("[0x%lx 0x%lx] range[0x%lx 0x%lx] notifier[0x%lx 0x%lx] %d\n",
2475 start, last, range->start >> PAGE_SHIFT,
2476 (range->end - 1) >> PAGE_SHIFT,
2477 mni->interval_tree.start >> PAGE_SHIFT,
2478 mni->interval_tree.last >> PAGE_SHIFT, range->event);
2480 prange = container_of(mni, struct svm_range, notifier);
2482 svm_range_lock(prange);
2483 mmu_interval_set_seq(mni, cur_seq);
2485 switch (range->event) {
2486 case MMU_NOTIFY_UNMAP:
2487 svm_range_unmap_from_cpu(mni->mm, prange, start, last);
2490 svm_range_evict(prange, mni->mm, start, last, range->event);
2494 svm_range_unlock(prange);
2501 * svm_range_from_addr - find svm range from fault address
2502 * @svms: svm range list header
2503 * @addr: address to search range interval tree, in pages
2504 * @parent: parent range if range is on child list
2506 * Context: The caller must hold svms->lock
2508 * Return: the svm_range found or NULL
2511 svm_range_from_addr(struct svm_range_list *svms, unsigned long addr,
2512 struct svm_range **parent)
2514 struct interval_tree_node *node;
2515 struct svm_range *prange;
2516 struct svm_range *pchild;
2518 node = interval_tree_iter_first(&svms->objects, addr, addr);
2522 prange = container_of(node, struct svm_range, it_node);
2523 pr_debug("address 0x%lx prange [0x%lx 0x%lx] node [0x%lx 0x%lx]\n",
2524 addr, prange->start, prange->last, node->start, node->last);
2526 if (addr >= prange->start && addr <= prange->last) {
2531 list_for_each_entry(pchild, &prange->child_list, child_list)
2532 if (addr >= pchild->start && addr <= pchild->last) {
2533 pr_debug("found address 0x%lx pchild [0x%lx 0x%lx]\n",
2534 addr, pchild->start, pchild->last);
2543 /* svm_range_best_restore_location - decide the best fault restore location
2544 * @prange: svm range structure
2545 * @adev: the GPU on which vm fault happened
2547 * This is only called when xnack is on, to decide the best location to restore
2548 * the range mapping after GPU vm fault. Caller uses the best location to do
2549 * migration if actual loc is not best location, then update GPU page table
2550 * mapping to the best location.
2552 * If the preferred loc is accessible by faulting GPU, use preferred loc.
2553 * If vm fault gpu idx is on range ACCESSIBLE bitmap, best_loc is vm fault gpu
2554 * If vm fault gpu idx is on range ACCESSIBLE_IN_PLACE bitmap, then
2555 * if range actual loc is cpu, best_loc is cpu
2556 * if vm fault gpu is on xgmi same hive of range actual loc gpu, best_loc is
2558 * Otherwise, GPU no access, best_loc is -1.
2561 * -1 means vm fault GPU no access
2562 * 0 for CPU or GPU id
2565 svm_range_best_restore_location(struct svm_range *prange,
2566 struct kfd_node *node,
2569 struct kfd_node *bo_node, *preferred_node;
2570 struct kfd_process *p;
2574 p = container_of(prange->svms, struct kfd_process, svms);
2576 r = kfd_process_gpuid_from_node(p, node, &gpuid, gpuidx);
2578 pr_debug("failed to get gpuid from kgd\n");
2582 if (node->adev->gmc.is_app_apu)
2585 if (prange->preferred_loc == gpuid ||
2586 prange->preferred_loc == KFD_IOCTL_SVM_LOCATION_SYSMEM) {
2587 return prange->preferred_loc;
2588 } else if (prange->preferred_loc != KFD_IOCTL_SVM_LOCATION_UNDEFINED) {
2589 preferred_node = svm_range_get_node_by_id(prange, prange->preferred_loc);
2590 if (preferred_node && svm_nodes_in_same_hive(node, preferred_node))
2591 return prange->preferred_loc;
2595 if (test_bit(*gpuidx, prange->bitmap_access))
2598 if (test_bit(*gpuidx, prange->bitmap_aip)) {
2599 if (!prange->actual_loc)
2602 bo_node = svm_range_get_node_by_id(prange, prange->actual_loc);
2603 if (bo_node && svm_nodes_in_same_hive(node, bo_node))
2604 return prange->actual_loc;
2613 svm_range_get_range_boundaries(struct kfd_process *p, int64_t addr,
2614 unsigned long *start, unsigned long *last,
2615 bool *is_heap_stack)
2617 struct vm_area_struct *vma;
2618 struct interval_tree_node *node;
2619 unsigned long start_limit, end_limit;
2621 vma = vma_lookup(p->mm, addr << PAGE_SHIFT);
2623 pr_debug("VMA does not exist in address [0x%llx]\n", addr);
2627 *is_heap_stack = (vma->vm_start <= vma->vm_mm->brk &&
2628 vma->vm_end >= vma->vm_mm->start_brk) ||
2629 (vma->vm_start <= vma->vm_mm->start_stack &&
2630 vma->vm_end >= vma->vm_mm->start_stack);
2632 start_limit = max(vma->vm_start >> PAGE_SHIFT,
2633 (unsigned long)ALIGN_DOWN(addr, 2UL << 8));
2634 end_limit = min(vma->vm_end >> PAGE_SHIFT,
2635 (unsigned long)ALIGN(addr + 1, 2UL << 8));
2636 /* First range that starts after the fault address */
2637 node = interval_tree_iter_first(&p->svms.objects, addr + 1, ULONG_MAX);
2639 end_limit = min(end_limit, node->start);
2640 /* Last range that ends before the fault address */
2641 node = container_of(rb_prev(&node->rb),
2642 struct interval_tree_node, rb);
2644 /* Last range must end before addr because
2645 * there was no range after addr
2647 node = container_of(rb_last(&p->svms.objects.rb_root),
2648 struct interval_tree_node, rb);
2651 if (node->last >= addr) {
2652 WARN(1, "Overlap with prev node and page fault addr\n");
2655 start_limit = max(start_limit, node->last + 1);
2658 *start = start_limit;
2659 *last = end_limit - 1;
2661 pr_debug("vma [0x%lx 0x%lx] range [0x%lx 0x%lx] is_heap_stack %d\n",
2662 vma->vm_start >> PAGE_SHIFT, vma->vm_end >> PAGE_SHIFT,
2663 *start, *last, *is_heap_stack);
2669 svm_range_check_vm_userptr(struct kfd_process *p, uint64_t start, uint64_t last,
2670 uint64_t *bo_s, uint64_t *bo_l)
2672 struct amdgpu_bo_va_mapping *mapping;
2673 struct interval_tree_node *node;
2674 struct amdgpu_bo *bo = NULL;
2675 unsigned long userptr;
2679 for (i = 0; i < p->n_pdds; i++) {
2680 struct amdgpu_vm *vm;
2682 if (!p->pdds[i]->drm_priv)
2685 vm = drm_priv_to_vm(p->pdds[i]->drm_priv);
2686 r = amdgpu_bo_reserve(vm->root.bo, false);
2690 /* Check userptr by searching entire vm->va interval tree */
2691 node = interval_tree_iter_first(&vm->va, 0, ~0ULL);
2693 mapping = container_of((struct rb_node *)node,
2694 struct amdgpu_bo_va_mapping, rb);
2695 bo = mapping->bo_va->base.bo;
2697 if (!amdgpu_ttm_tt_affect_userptr(bo->tbo.ttm,
2698 start << PAGE_SHIFT,
2701 node = interval_tree_iter_next(node, 0, ~0ULL);
2705 pr_debug("[0x%llx 0x%llx] already userptr mapped\n",
2708 *bo_s = userptr >> PAGE_SHIFT;
2709 *bo_l = *bo_s + bo->tbo.ttm->num_pages - 1;
2711 amdgpu_bo_unreserve(vm->root.bo);
2714 amdgpu_bo_unreserve(vm->root.bo);
2720 svm_range *svm_range_create_unregistered_range(struct kfd_node *node,
2721 struct kfd_process *p,
2722 struct mm_struct *mm,
2725 struct svm_range *prange = NULL;
2726 unsigned long start, last;
2727 uint32_t gpuid, gpuidx;
2733 if (svm_range_get_range_boundaries(p, addr, &start, &last,
2737 r = svm_range_check_vm(p, start, last, &bo_s, &bo_l);
2738 if (r != -EADDRINUSE)
2739 r = svm_range_check_vm_userptr(p, start, last, &bo_s, &bo_l);
2741 if (r == -EADDRINUSE) {
2742 if (addr >= bo_s && addr <= bo_l)
2745 /* Create one page svm range if 2MB range overlapping */
2750 prange = svm_range_new(&p->svms, start, last, true);
2752 pr_debug("Failed to create prange in address [0x%llx]\n", addr);
2755 if (kfd_process_gpuid_from_node(p, node, &gpuid, &gpuidx)) {
2756 pr_debug("failed to get gpuid from kgd\n");
2757 svm_range_free(prange, true);
2762 prange->preferred_loc = KFD_IOCTL_SVM_LOCATION_SYSMEM;
2764 svm_range_add_to_svms(prange);
2765 svm_range_add_notifier_locked(mm, prange);
2770 /* svm_range_skip_recover - decide if prange can be recovered
2771 * @prange: svm range structure
2773 * GPU vm retry fault handle skip recover the range for cases:
2774 * 1. prange is on deferred list to be removed after unmap, it is stale fault,
2775 * deferred list work will drain the stale fault before free the prange.
2776 * 2. prange is on deferred list to add interval notifier after split, or
2777 * 3. prange is child range, it is split from parent prange, recover later
2778 * after interval notifier is added.
2780 * Return: true to skip recover, false to recover
2782 static bool svm_range_skip_recover(struct svm_range *prange)
2784 struct svm_range_list *svms = prange->svms;
2786 spin_lock(&svms->deferred_list_lock);
2787 if (list_empty(&prange->deferred_list) &&
2788 list_empty(&prange->child_list)) {
2789 spin_unlock(&svms->deferred_list_lock);
2792 spin_unlock(&svms->deferred_list_lock);
2794 if (prange->work_item.op == SVM_OP_UNMAP_RANGE) {
2795 pr_debug("svms 0x%p prange 0x%p [0x%lx 0x%lx] unmapped\n",
2796 svms, prange, prange->start, prange->last);
2799 if (prange->work_item.op == SVM_OP_ADD_RANGE_AND_MAP ||
2800 prange->work_item.op == SVM_OP_ADD_RANGE) {
2801 pr_debug("svms 0x%p prange 0x%p [0x%lx 0x%lx] not added yet\n",
2802 svms, prange, prange->start, prange->last);
2809 svm_range_count_fault(struct kfd_node *node, struct kfd_process *p,
2812 struct kfd_process_device *pdd;
2814 /* fault is on different page of same range
2815 * or fault is skipped to recover later
2816 * or fault is on invalid virtual address
2818 if (gpuidx == MAX_GPU_INSTANCE) {
2822 r = kfd_process_gpuid_from_node(p, node, &gpuid, &gpuidx);
2827 /* fault is recovered
2828 * or fault cannot recover because GPU no access on the range
2830 pdd = kfd_process_device_from_gpuidx(p, gpuidx);
2832 WRITE_ONCE(pdd->faults, pdd->faults + 1);
2836 svm_fault_allowed(struct vm_area_struct *vma, bool write_fault)
2838 unsigned long requested = VM_READ;
2841 requested |= VM_WRITE;
2843 pr_debug("requested 0x%lx, vma permission flags 0x%lx\n", requested,
2845 return (vma->vm_flags & requested) == requested;
2849 svm_range_restore_pages(struct amdgpu_device *adev, unsigned int pasid,
2850 uint32_t vmid, uint32_t node_id,
2851 uint64_t addr, bool write_fault)
2853 struct mm_struct *mm = NULL;
2854 struct svm_range_list *svms;
2855 struct svm_range *prange;
2856 struct kfd_process *p;
2857 ktime_t timestamp = ktime_get_boottime();
2858 struct kfd_node *node;
2860 int32_t gpuidx = MAX_GPU_INSTANCE;
2861 bool write_locked = false;
2862 struct vm_area_struct *vma;
2863 bool migration = false;
2866 if (!KFD_IS_SVM_API_SUPPORTED(adev)) {
2867 pr_debug("device does not support SVM\n");
2871 p = kfd_lookup_process_by_pasid(pasid);
2873 pr_debug("kfd process not founded pasid 0x%x\n", pasid);
2878 pr_debug("restoring svms 0x%p fault address 0x%llx\n", svms, addr);
2880 if (atomic_read(&svms->drain_pagefaults)) {
2881 pr_debug("draining retry fault, drop fault 0x%llx\n", addr);
2886 if (!p->xnack_enabled) {
2887 pr_debug("XNACK not enabled for pasid 0x%x\n", pasid);
2892 /* p->lead_thread is available as kfd_process_wq_release flush the work
2893 * before releasing task ref.
2895 mm = get_task_mm(p->lead_thread);
2897 pr_debug("svms 0x%p failed to get mm\n", svms);
2902 node = kfd_node_by_irq_ids(adev, node_id, vmid);
2904 pr_debug("kfd node does not exist node_id: %d, vmid: %d\n", node_id,
2911 mutex_lock(&svms->lock);
2912 prange = svm_range_from_addr(svms, addr, NULL);
2914 pr_debug("failed to find prange svms 0x%p address [0x%llx]\n",
2916 if (!write_locked) {
2917 /* Need the write lock to create new range with MMU notifier.
2918 * Also flush pending deferred work to make sure the interval
2919 * tree is up to date before we add a new range
2921 mutex_unlock(&svms->lock);
2922 mmap_read_unlock(mm);
2923 mmap_write_lock(mm);
2924 write_locked = true;
2925 goto retry_write_locked;
2927 prange = svm_range_create_unregistered_range(node, p, mm, addr);
2929 pr_debug("failed to create unregistered range svms 0x%p address [0x%llx]\n",
2931 mmap_write_downgrade(mm);
2933 goto out_unlock_svms;
2937 mmap_write_downgrade(mm);
2939 mutex_lock(&prange->migrate_mutex);
2941 if (svm_range_skip_recover(prange)) {
2942 amdgpu_gmc_filter_faults_remove(node->adev, addr, pasid);
2944 goto out_unlock_range;
2947 /* skip duplicate vm fault on different pages of same range */
2948 if (ktime_before(timestamp, ktime_add_ns(prange->validate_timestamp,
2949 AMDGPU_SVM_RANGE_RETRY_FAULT_PENDING))) {
2950 pr_debug("svms 0x%p [0x%lx %lx] already restored\n",
2951 svms, prange->start, prange->last);
2953 goto out_unlock_range;
2956 /* __do_munmap removed VMA, return success as we are handling stale
2959 vma = vma_lookup(mm, addr << PAGE_SHIFT);
2961 pr_debug("address 0x%llx VMA is removed\n", addr);
2963 goto out_unlock_range;
2966 if (!svm_fault_allowed(vma, write_fault)) {
2967 pr_debug("fault addr 0x%llx no %s permission\n", addr,
2968 write_fault ? "write" : "read");
2970 goto out_unlock_range;
2973 best_loc = svm_range_best_restore_location(prange, node, &gpuidx);
2974 if (best_loc == -1) {
2975 pr_debug("svms %p failed get best restore loc [0x%lx 0x%lx]\n",
2976 svms, prange->start, prange->last);
2978 goto out_unlock_range;
2981 pr_debug("svms %p [0x%lx 0x%lx] best restore 0x%x, actual loc 0x%x\n",
2982 svms, prange->start, prange->last, best_loc,
2983 prange->actual_loc);
2985 kfd_smi_event_page_fault_start(node, p->lead_thread->pid, addr,
2986 write_fault, timestamp);
2988 if (prange->actual_loc != best_loc) {
2991 r = svm_migrate_to_vram(prange, best_loc, mm,
2992 KFD_MIGRATE_TRIGGER_PAGEFAULT_GPU);
2994 pr_debug("svm_migrate_to_vram failed (%d) at %llx, falling back to system memory\n",
2996 /* Fallback to system memory if migration to
2999 if (prange->actual_loc)
3000 r = svm_migrate_vram_to_ram(prange, mm,
3001 KFD_MIGRATE_TRIGGER_PAGEFAULT_GPU,
3007 r = svm_migrate_vram_to_ram(prange, mm,
3008 KFD_MIGRATE_TRIGGER_PAGEFAULT_GPU,
3012 pr_debug("failed %d to migrate svms %p [0x%lx 0x%lx]\n",
3013 r, svms, prange->start, prange->last);
3014 goto out_unlock_range;
3018 r = svm_range_validate_and_map(mm, prange, gpuidx, false, false, false);
3020 pr_debug("failed %d to map svms 0x%p [0x%lx 0x%lx] to gpus\n",
3021 r, svms, prange->start, prange->last);
3023 kfd_smi_event_page_fault_end(node, p->lead_thread->pid, addr,
3027 mutex_unlock(&prange->migrate_mutex);
3029 mutex_unlock(&svms->lock);
3030 mmap_read_unlock(mm);
3032 svm_range_count_fault(node, p, gpuidx);
3036 kfd_unref_process(p);
3039 pr_debug("recover vm fault later\n");
3040 amdgpu_gmc_filter_faults_remove(node->adev, addr, pasid);
3047 svm_range_switch_xnack_reserve_mem(struct kfd_process *p, bool xnack_enabled)
3049 struct svm_range *prange, *pchild;
3050 uint64_t reserved_size = 0;
3054 pr_debug("switching xnack from %d to %d\n", p->xnack_enabled, xnack_enabled);
3056 mutex_lock(&p->svms.lock);
3058 list_for_each_entry(prange, &p->svms.list, list) {
3059 svm_range_lock(prange);
3060 list_for_each_entry(pchild, &prange->child_list, child_list) {
3061 size = (pchild->last - pchild->start + 1) << PAGE_SHIFT;
3062 if (xnack_enabled) {
3063 amdgpu_amdkfd_unreserve_mem_limit(NULL, size,
3064 KFD_IOC_ALLOC_MEM_FLAGS_USERPTR, 0);
3066 r = amdgpu_amdkfd_reserve_mem_limit(NULL, size,
3067 KFD_IOC_ALLOC_MEM_FLAGS_USERPTR, 0);
3070 reserved_size += size;
3074 size = (prange->last - prange->start + 1) << PAGE_SHIFT;
3075 if (xnack_enabled) {
3076 amdgpu_amdkfd_unreserve_mem_limit(NULL, size,
3077 KFD_IOC_ALLOC_MEM_FLAGS_USERPTR, 0);
3079 r = amdgpu_amdkfd_reserve_mem_limit(NULL, size,
3080 KFD_IOC_ALLOC_MEM_FLAGS_USERPTR, 0);
3083 reserved_size += size;
3086 svm_range_unlock(prange);
3092 amdgpu_amdkfd_unreserve_mem_limit(NULL, reserved_size,
3093 KFD_IOC_ALLOC_MEM_FLAGS_USERPTR, 0);
3095 /* Change xnack mode must be inside svms lock, to avoid race with
3096 * svm_range_deferred_list_work unreserve memory in parallel.
3098 p->xnack_enabled = xnack_enabled;
3100 mutex_unlock(&p->svms.lock);
3104 void svm_range_list_fini(struct kfd_process *p)
3106 struct svm_range *prange;
3107 struct svm_range *next;
3109 pr_debug("pasid 0x%x svms 0x%p\n", p->pasid, &p->svms);
3111 cancel_delayed_work_sync(&p->svms.restore_work);
3113 /* Ensure list work is finished before process is destroyed */
3114 flush_work(&p->svms.deferred_list_work);
3117 * Ensure no retry fault comes in afterwards, as page fault handler will
3118 * not find kfd process and take mm lock to recover fault.
3120 atomic_inc(&p->svms.drain_pagefaults);
3121 svm_range_drain_retry_fault(&p->svms);
3123 list_for_each_entry_safe(prange, next, &p->svms.list, list) {
3124 svm_range_unlink(prange);
3125 svm_range_remove_notifier(prange);
3126 svm_range_free(prange, true);
3129 mutex_destroy(&p->svms.lock);
3131 pr_debug("pasid 0x%x svms 0x%p done\n", p->pasid, &p->svms);
3134 int svm_range_list_init(struct kfd_process *p)
3136 struct svm_range_list *svms = &p->svms;
3139 svms->objects = RB_ROOT_CACHED;
3140 mutex_init(&svms->lock);
3141 INIT_LIST_HEAD(&svms->list);
3142 atomic_set(&svms->evicted_ranges, 0);
3143 atomic_set(&svms->drain_pagefaults, 0);
3144 INIT_DELAYED_WORK(&svms->restore_work, svm_range_restore_work);
3145 INIT_WORK(&svms->deferred_list_work, svm_range_deferred_list_work);
3146 INIT_LIST_HEAD(&svms->deferred_range_list);
3147 INIT_LIST_HEAD(&svms->criu_svm_metadata_list);
3148 spin_lock_init(&svms->deferred_list_lock);
3150 for (i = 0; i < p->n_pdds; i++)
3151 if (KFD_IS_SVM_API_SUPPORTED(p->pdds[i]->dev->adev))
3152 bitmap_set(svms->bitmap_supported, i, 1);
3158 * svm_range_check_vm - check if virtual address range mapped already
3159 * @p: current kfd_process
3160 * @start: range start address, in pages
3161 * @last: range last address, in pages
3162 * @bo_s: mapping start address in pages if address range already mapped
3163 * @bo_l: mapping last address in pages if address range already mapped
3165 * The purpose is to avoid virtual address ranges already allocated by
3166 * kfd_ioctl_alloc_memory_of_gpu ioctl.
3167 * It looks for each pdd in the kfd_process.
3169 * Context: Process context
3171 * Return 0 - OK, if the range is not mapped.
3172 * Otherwise error code:
3173 * -EADDRINUSE - if address is mapped already by kfd_ioctl_alloc_memory_of_gpu
3174 * -ERESTARTSYS - A wait for the buffer to become unreserved was interrupted by
3175 * a signal. Release all buffer reservations and return to user-space.
3178 svm_range_check_vm(struct kfd_process *p, uint64_t start, uint64_t last,
3179 uint64_t *bo_s, uint64_t *bo_l)
3181 struct amdgpu_bo_va_mapping *mapping;
3182 struct interval_tree_node *node;
3186 for (i = 0; i < p->n_pdds; i++) {
3187 struct amdgpu_vm *vm;
3189 if (!p->pdds[i]->drm_priv)
3192 vm = drm_priv_to_vm(p->pdds[i]->drm_priv);
3193 r = amdgpu_bo_reserve(vm->root.bo, false);
3197 node = interval_tree_iter_first(&vm->va, start, last);
3199 pr_debug("range [0x%llx 0x%llx] already TTM mapped\n",
3201 mapping = container_of((struct rb_node *)node,
3202 struct amdgpu_bo_va_mapping, rb);
3204 *bo_s = mapping->start;
3205 *bo_l = mapping->last;
3207 amdgpu_bo_unreserve(vm->root.bo);
3210 amdgpu_bo_unreserve(vm->root.bo);
3217 * svm_range_is_valid - check if virtual address range is valid
3218 * @p: current kfd_process
3219 * @start: range start address, in pages
3220 * @size: range size, in pages
3222 * Valid virtual address range means it belongs to one or more VMAs
3224 * Context: Process context
3227 * 0 - OK, otherwise error code
3230 svm_range_is_valid(struct kfd_process *p, uint64_t start, uint64_t size)
3232 const unsigned long device_vma = VM_IO | VM_PFNMAP | VM_MIXEDMAP;
3233 struct vm_area_struct *vma;
3235 unsigned long start_unchg = start;
3237 start <<= PAGE_SHIFT;
3238 end = start + (size << PAGE_SHIFT);
3240 vma = vma_lookup(p->mm, start);
3241 if (!vma || (vma->vm_flags & device_vma))
3243 start = min(end, vma->vm_end);
3244 } while (start < end);
3246 return svm_range_check_vm(p, start_unchg, (end - 1) >> PAGE_SHIFT, NULL,
3251 * svm_range_best_prefetch_location - decide the best prefetch location
3252 * @prange: svm range structure
3255 * If range map to single GPU, the best prefetch location is prefetch_loc, which
3256 * can be CPU or GPU.
3258 * If range is ACCESS or ACCESS_IN_PLACE by mGPUs, only if mGPU connection on
3259 * XGMI same hive, the best prefetch location is prefetch_loc GPU, othervise
3260 * the best prefetch location is always CPU, because GPU can not have coherent
3261 * mapping VRAM of other GPUs even with large-BAR PCIe connection.
3264 * If range is not ACCESS_IN_PLACE by mGPUs, the best prefetch location is
3265 * prefetch_loc, other GPU access will generate vm fault and trigger migration.
3267 * If range is ACCESS_IN_PLACE by mGPUs, only if mGPU connection on XGMI same
3268 * hive, the best prefetch location is prefetch_loc GPU, otherwise the best
3269 * prefetch location is always CPU.
3271 * Context: Process context
3274 * 0 for CPU or GPU id
3277 svm_range_best_prefetch_location(struct svm_range *prange)
3279 DECLARE_BITMAP(bitmap, MAX_GPU_INSTANCE);
3280 uint32_t best_loc = prange->prefetch_loc;
3281 struct kfd_process_device *pdd;
3282 struct kfd_node *bo_node;
3283 struct kfd_process *p;
3286 p = container_of(prange->svms, struct kfd_process, svms);
3288 if (!best_loc || best_loc == KFD_IOCTL_SVM_LOCATION_UNDEFINED)
3291 bo_node = svm_range_get_node_by_id(prange, best_loc);
3293 WARN_ONCE(1, "failed to get valid kfd node at id%x\n", best_loc);
3298 if (bo_node->adev->gmc.is_app_apu) {
3303 if (p->xnack_enabled)
3304 bitmap_copy(bitmap, prange->bitmap_aip, MAX_GPU_INSTANCE);
3306 bitmap_or(bitmap, prange->bitmap_access, prange->bitmap_aip,
3309 for_each_set_bit(gpuidx, bitmap, MAX_GPU_INSTANCE) {
3310 pdd = kfd_process_device_from_gpuidx(p, gpuidx);
3312 pr_debug("failed to get device by idx 0x%x\n", gpuidx);
3316 if (pdd->dev->adev == bo_node->adev)
3319 if (!svm_nodes_in_same_hive(pdd->dev, bo_node)) {
3326 pr_debug("xnack %d svms 0x%p [0x%lx 0x%lx] best loc 0x%x\n",
3327 p->xnack_enabled, &p->svms, prange->start, prange->last,
3333 /* svm_range_trigger_migration - start page migration if prefetch loc changed
3334 * @mm: current process mm_struct
3335 * @prange: svm range structure
3336 * @migrated: output, true if migration is triggered
3338 * If range perfetch_loc is GPU, actual loc is cpu 0, then migrate the range
3340 * If range prefetch_loc is cpu 0, actual loc is GPU, then migrate the range
3343 * If GPU vm fault retry is not enabled, migration interact with MMU notifier
3345 * 1. migrate_vma_setup invalidate pages, MMU notifier callback svm_range_evict
3346 * stops all queues, schedule restore work
3347 * 2. svm_range_restore_work wait for migration is done by
3348 * a. svm_range_validate_vram takes prange->migrate_mutex
3349 * b. svm_range_validate_ram HMM get pages wait for CPU fault handle returns
3350 * 3. restore work update mappings of GPU, resume all queues.
3352 * Context: Process context
3355 * 0 - OK, otherwise - error code of migration
3358 svm_range_trigger_migration(struct mm_struct *mm, struct svm_range *prange,
3365 best_loc = svm_range_best_prefetch_location(prange);
3367 if (best_loc == KFD_IOCTL_SVM_LOCATION_UNDEFINED ||
3368 best_loc == prange->actual_loc)
3372 r = svm_migrate_vram_to_ram(prange, mm,
3373 KFD_MIGRATE_TRIGGER_PREFETCH, NULL);
3378 r = svm_migrate_to_vram(prange, best_loc, mm, KFD_MIGRATE_TRIGGER_PREFETCH);
3384 int svm_range_schedule_evict_svm_bo(struct amdgpu_amdkfd_fence *fence)
3389 if (dma_fence_is_signaled(&fence->base))
3392 if (fence->svm_bo) {
3393 WRITE_ONCE(fence->svm_bo->evicting, 1);
3394 schedule_work(&fence->svm_bo->eviction_work);
3400 static void svm_range_evict_svm_bo_worker(struct work_struct *work)
3402 struct svm_range_bo *svm_bo;
3403 struct mm_struct *mm;
3406 svm_bo = container_of(work, struct svm_range_bo, eviction_work);
3407 if (!svm_bo_ref_unless_zero(svm_bo))
3408 return; /* svm_bo was freed while eviction was pending */
3410 if (mmget_not_zero(svm_bo->eviction_fence->mm)) {
3411 mm = svm_bo->eviction_fence->mm;
3413 svm_range_bo_unref(svm_bo);
3418 spin_lock(&svm_bo->list_lock);
3419 while (!list_empty(&svm_bo->range_list) && !r) {
3420 struct svm_range *prange =
3421 list_first_entry(&svm_bo->range_list,
3422 struct svm_range, svm_bo_list);
3425 list_del_init(&prange->svm_bo_list);
3426 spin_unlock(&svm_bo->list_lock);
3428 pr_debug("svms 0x%p [0x%lx 0x%lx]\n", prange->svms,
3429 prange->start, prange->last);
3431 mutex_lock(&prange->migrate_mutex);
3433 r = svm_migrate_vram_to_ram(prange, mm,
3434 KFD_MIGRATE_TRIGGER_TTM_EVICTION, NULL);
3435 } while (!r && prange->actual_loc && --retries);
3437 if (!r && prange->actual_loc)
3438 pr_info_once("Migration failed during eviction");
3440 if (!prange->actual_loc) {
3441 mutex_lock(&prange->lock);
3442 prange->svm_bo = NULL;
3443 mutex_unlock(&prange->lock);
3445 mutex_unlock(&prange->migrate_mutex);
3447 spin_lock(&svm_bo->list_lock);
3449 spin_unlock(&svm_bo->list_lock);
3450 mmap_read_unlock(mm);
3453 dma_fence_signal(&svm_bo->eviction_fence->base);
3455 /* This is the last reference to svm_bo, after svm_range_vram_node_free
3456 * has been called in svm_migrate_vram_to_ram
3458 WARN_ONCE(!r && kref_read(&svm_bo->kref) != 1, "This was not the last reference\n");
3459 svm_range_bo_unref(svm_bo);
3463 svm_range_set_attr(struct kfd_process *p, struct mm_struct *mm,
3464 uint64_t start, uint64_t size, uint32_t nattr,
3465 struct kfd_ioctl_svm_attribute *attrs)
3467 struct amdkfd_process_info *process_info = p->kgd_process_info;
3468 struct list_head update_list;
3469 struct list_head insert_list;
3470 struct list_head remove_list;
3471 struct svm_range_list *svms;
3472 struct svm_range *prange;
3473 struct svm_range *next;
3474 bool update_mapping = false;
3478 pr_debug("pasid 0x%x svms 0x%p [0x%llx 0x%llx] pages 0x%llx\n",
3479 p->pasid, &p->svms, start, start + size - 1, size);
3481 r = svm_range_check_attr(p, nattr, attrs);
3487 mutex_lock(&process_info->lock);
3489 svm_range_list_lock_and_flush_work(svms, mm);
3491 r = svm_range_is_valid(p, start, size);
3493 pr_debug("invalid range r=%d\n", r);
3494 mmap_write_unlock(mm);
3498 mutex_lock(&svms->lock);
3500 /* Add new range and split existing ranges as needed */
3501 r = svm_range_add(p, start, size, nattr, attrs, &update_list,
3502 &insert_list, &remove_list);
3504 mutex_unlock(&svms->lock);
3505 mmap_write_unlock(mm);
3508 /* Apply changes as a transaction */
3509 list_for_each_entry_safe(prange, next, &insert_list, list) {
3510 svm_range_add_to_svms(prange);
3511 svm_range_add_notifier_locked(mm, prange);
3513 list_for_each_entry(prange, &update_list, update_list) {
3514 svm_range_apply_attrs(p, prange, nattr, attrs, &update_mapping);
3515 /* TODO: unmap ranges from GPU that lost access */
3517 list_for_each_entry_safe(prange, next, &remove_list, update_list) {
3518 pr_debug("unlink old 0x%p prange 0x%p [0x%lx 0x%lx]\n",
3519 prange->svms, prange, prange->start,
3521 svm_range_unlink(prange);
3522 svm_range_remove_notifier(prange);
3523 svm_range_free(prange, false);
3526 mmap_write_downgrade(mm);
3527 /* Trigger migrations and revalidate and map to GPUs as needed. If
3528 * this fails we may be left with partially completed actions. There
3529 * is no clean way of rolling back to the previous state in such a
3530 * case because the rollback wouldn't be guaranteed to work either.
3532 list_for_each_entry(prange, &update_list, update_list) {
3535 mutex_lock(&prange->migrate_mutex);
3537 r = svm_range_trigger_migration(mm, prange, &migrated);
3539 goto out_unlock_range;
3541 if (migrated && (!p->xnack_enabled ||
3542 (prange->flags & KFD_IOCTL_SVM_FLAG_GPU_ALWAYS_MAPPED)) &&
3543 prange->mapped_to_gpu) {
3544 pr_debug("restore_work will update mappings of GPUs\n");
3545 mutex_unlock(&prange->migrate_mutex);
3549 if (!migrated && !update_mapping) {
3550 mutex_unlock(&prange->migrate_mutex);
3554 flush_tlb = !migrated && update_mapping && prange->mapped_to_gpu;
3556 r = svm_range_validate_and_map(mm, prange, MAX_GPU_INSTANCE,
3557 true, true, flush_tlb);
3559 pr_debug("failed %d to map svm range\n", r);
3562 mutex_unlock(&prange->migrate_mutex);
3567 dynamic_svm_range_dump(svms);
3569 mutex_unlock(&svms->lock);
3570 mmap_read_unlock(mm);
3572 mutex_unlock(&process_info->lock);
3574 pr_debug("pasid 0x%x svms 0x%p [0x%llx 0x%llx] done, r=%d\n", p->pasid,
3575 &p->svms, start, start + size - 1, r);
3581 svm_range_get_attr(struct kfd_process *p, struct mm_struct *mm,
3582 uint64_t start, uint64_t size, uint32_t nattr,
3583 struct kfd_ioctl_svm_attribute *attrs)
3585 DECLARE_BITMAP(bitmap_access, MAX_GPU_INSTANCE);
3586 DECLARE_BITMAP(bitmap_aip, MAX_GPU_INSTANCE);
3587 bool get_preferred_loc = false;
3588 bool get_prefetch_loc = false;
3589 bool get_granularity = false;
3590 bool get_accessible = false;
3591 bool get_flags = false;
3592 uint64_t last = start + size - 1UL;
3593 uint8_t granularity = 0xff;
3594 struct interval_tree_node *node;
3595 struct svm_range_list *svms;
3596 struct svm_range *prange;
3597 uint32_t prefetch_loc = KFD_IOCTL_SVM_LOCATION_UNDEFINED;
3598 uint32_t location = KFD_IOCTL_SVM_LOCATION_UNDEFINED;
3599 uint32_t flags_and = 0xffffffff;
3600 uint32_t flags_or = 0;
3605 pr_debug("svms 0x%p [0x%llx 0x%llx] nattr 0x%x\n", &p->svms, start,
3606 start + size - 1, nattr);
3608 /* Flush pending deferred work to avoid racing with deferred actions from
3609 * previous memory map changes (e.g. munmap). Concurrent memory map changes
3610 * can still race with get_attr because we don't hold the mmap lock. But that
3611 * would be a race condition in the application anyway, and undefined
3612 * behaviour is acceptable in that case.
3614 flush_work(&p->svms.deferred_list_work);
3617 r = svm_range_is_valid(p, start, size);
3618 mmap_read_unlock(mm);
3620 pr_debug("invalid range r=%d\n", r);
3624 for (i = 0; i < nattr; i++) {
3625 switch (attrs[i].type) {
3626 case KFD_IOCTL_SVM_ATTR_PREFERRED_LOC:
3627 get_preferred_loc = true;
3629 case KFD_IOCTL_SVM_ATTR_PREFETCH_LOC:
3630 get_prefetch_loc = true;
3632 case KFD_IOCTL_SVM_ATTR_ACCESS:
3633 get_accessible = true;
3635 case KFD_IOCTL_SVM_ATTR_SET_FLAGS:
3636 case KFD_IOCTL_SVM_ATTR_CLR_FLAGS:
3639 case KFD_IOCTL_SVM_ATTR_GRANULARITY:
3640 get_granularity = true;
3642 case KFD_IOCTL_SVM_ATTR_ACCESS_IN_PLACE:
3643 case KFD_IOCTL_SVM_ATTR_NO_ACCESS:
3646 pr_debug("get invalid attr type 0x%x\n", attrs[i].type);
3653 mutex_lock(&svms->lock);
3655 node = interval_tree_iter_first(&svms->objects, start, last);
3657 pr_debug("range attrs not found return default values\n");
3658 svm_range_set_default_attributes(&location, &prefetch_loc,
3659 &granularity, &flags_and);
3660 flags_or = flags_and;
3661 if (p->xnack_enabled)
3662 bitmap_copy(bitmap_access, svms->bitmap_supported,
3665 bitmap_zero(bitmap_access, MAX_GPU_INSTANCE);
3666 bitmap_zero(bitmap_aip, MAX_GPU_INSTANCE);
3669 bitmap_copy(bitmap_access, svms->bitmap_supported, MAX_GPU_INSTANCE);
3670 bitmap_copy(bitmap_aip, svms->bitmap_supported, MAX_GPU_INSTANCE);
3673 struct interval_tree_node *next;
3675 prange = container_of(node, struct svm_range, it_node);
3676 next = interval_tree_iter_next(node, start, last);
3678 if (get_preferred_loc) {
3679 if (prange->preferred_loc ==
3680 KFD_IOCTL_SVM_LOCATION_UNDEFINED ||
3681 (location != KFD_IOCTL_SVM_LOCATION_UNDEFINED &&
3682 location != prange->preferred_loc)) {
3683 location = KFD_IOCTL_SVM_LOCATION_UNDEFINED;
3684 get_preferred_loc = false;
3686 location = prange->preferred_loc;
3689 if (get_prefetch_loc) {
3690 if (prange->prefetch_loc ==
3691 KFD_IOCTL_SVM_LOCATION_UNDEFINED ||
3692 (prefetch_loc != KFD_IOCTL_SVM_LOCATION_UNDEFINED &&
3693 prefetch_loc != prange->prefetch_loc)) {
3694 prefetch_loc = KFD_IOCTL_SVM_LOCATION_UNDEFINED;
3695 get_prefetch_loc = false;
3697 prefetch_loc = prange->prefetch_loc;
3700 if (get_accessible) {
3701 bitmap_and(bitmap_access, bitmap_access,
3702 prange->bitmap_access, MAX_GPU_INSTANCE);
3703 bitmap_and(bitmap_aip, bitmap_aip,
3704 prange->bitmap_aip, MAX_GPU_INSTANCE);
3707 flags_and &= prange->flags;
3708 flags_or |= prange->flags;
3711 if (get_granularity && prange->granularity < granularity)
3712 granularity = prange->granularity;
3717 mutex_unlock(&svms->lock);
3719 for (i = 0; i < nattr; i++) {
3720 switch (attrs[i].type) {
3721 case KFD_IOCTL_SVM_ATTR_PREFERRED_LOC:
3722 attrs[i].value = location;
3724 case KFD_IOCTL_SVM_ATTR_PREFETCH_LOC:
3725 attrs[i].value = prefetch_loc;
3727 case KFD_IOCTL_SVM_ATTR_ACCESS:
3728 gpuidx = kfd_process_gpuidx_from_gpuid(p,
3731 pr_debug("invalid gpuid %x\n", attrs[i].value);
3734 if (test_bit(gpuidx, bitmap_access))
3735 attrs[i].type = KFD_IOCTL_SVM_ATTR_ACCESS;
3736 else if (test_bit(gpuidx, bitmap_aip))
3738 KFD_IOCTL_SVM_ATTR_ACCESS_IN_PLACE;
3740 attrs[i].type = KFD_IOCTL_SVM_ATTR_NO_ACCESS;
3742 case KFD_IOCTL_SVM_ATTR_SET_FLAGS:
3743 attrs[i].value = flags_and;
3745 case KFD_IOCTL_SVM_ATTR_CLR_FLAGS:
3746 attrs[i].value = ~flags_or;
3748 case KFD_IOCTL_SVM_ATTR_GRANULARITY:
3749 attrs[i].value = (uint32_t)granularity;
3757 int kfd_criu_resume_svm(struct kfd_process *p)
3759 struct kfd_ioctl_svm_attribute *set_attr_new, *set_attr = NULL;
3760 int nattr_common = 4, nattr_accessibility = 1;
3761 struct criu_svm_metadata *criu_svm_md = NULL;
3762 struct svm_range_list *svms = &p->svms;
3763 struct criu_svm_metadata *next = NULL;
3764 uint32_t set_flags = 0xffffffff;
3765 int i, j, num_attrs, ret = 0;
3766 uint64_t set_attr_size;
3767 struct mm_struct *mm;
3769 if (list_empty(&svms->criu_svm_metadata_list)) {
3770 pr_debug("No SVM data from CRIU restore stage 2\n");
3774 mm = get_task_mm(p->lead_thread);
3776 pr_err("failed to get mm for the target process\n");
3780 num_attrs = nattr_common + (nattr_accessibility * p->n_pdds);
3783 list_for_each_entry(criu_svm_md, &svms->criu_svm_metadata_list, list) {
3784 pr_debug("criu_svm_md[%d]\n\tstart: 0x%llx size: 0x%llx (npages)\n",
3785 i, criu_svm_md->data.start_addr, criu_svm_md->data.size);
3787 for (j = 0; j < num_attrs; j++) {
3788 pr_debug("\ncriu_svm_md[%d]->attrs[%d].type : 0x%x\ncriu_svm_md[%d]->attrs[%d].value : 0x%x\n",
3789 i, j, criu_svm_md->data.attrs[j].type,
3790 i, j, criu_svm_md->data.attrs[j].value);
3791 switch (criu_svm_md->data.attrs[j].type) {
3792 /* During Checkpoint operation, the query for
3793 * KFD_IOCTL_SVM_ATTR_PREFETCH_LOC attribute might
3794 * return KFD_IOCTL_SVM_LOCATION_UNDEFINED if they were
3795 * not used by the range which was checkpointed. Care
3796 * must be taken to not restore with an invalid value
3797 * otherwise the gpuidx value will be invalid and
3798 * set_attr would eventually fail so just replace those
3799 * with another dummy attribute such as
3800 * KFD_IOCTL_SVM_ATTR_SET_FLAGS.
3802 case KFD_IOCTL_SVM_ATTR_PREFETCH_LOC:
3803 if (criu_svm_md->data.attrs[j].value ==
3804 KFD_IOCTL_SVM_LOCATION_UNDEFINED) {
3805 criu_svm_md->data.attrs[j].type =
3806 KFD_IOCTL_SVM_ATTR_SET_FLAGS;
3807 criu_svm_md->data.attrs[j].value = 0;
3810 case KFD_IOCTL_SVM_ATTR_SET_FLAGS:
3811 set_flags = criu_svm_md->data.attrs[j].value;
3818 /* CLR_FLAGS is not available via get_attr during checkpoint but
3819 * it needs to be inserted before restoring the ranges so
3820 * allocate extra space for it before calling set_attr
3822 set_attr_size = sizeof(struct kfd_ioctl_svm_attribute) *
3824 set_attr_new = krealloc(set_attr, set_attr_size,
3826 if (!set_attr_new) {
3830 set_attr = set_attr_new;
3832 memcpy(set_attr, criu_svm_md->data.attrs, num_attrs *
3833 sizeof(struct kfd_ioctl_svm_attribute));
3834 set_attr[num_attrs].type = KFD_IOCTL_SVM_ATTR_CLR_FLAGS;
3835 set_attr[num_attrs].value = ~set_flags;
3837 ret = svm_range_set_attr(p, mm, criu_svm_md->data.start_addr,
3838 criu_svm_md->data.size, num_attrs + 1,
3841 pr_err("CRIU: failed to set range attributes\n");
3849 list_for_each_entry_safe(criu_svm_md, next, &svms->criu_svm_metadata_list, list) {
3850 pr_debug("freeing criu_svm_md[]\n\tstart: 0x%llx\n",
3851 criu_svm_md->data.start_addr);
3860 int kfd_criu_restore_svm(struct kfd_process *p,
3861 uint8_t __user *user_priv_ptr,
3862 uint64_t *priv_data_offset,
3863 uint64_t max_priv_data_size)
3865 uint64_t svm_priv_data_size, svm_object_md_size, svm_attrs_size;
3866 int nattr_common = 4, nattr_accessibility = 1;
3867 struct criu_svm_metadata *criu_svm_md = NULL;
3868 struct svm_range_list *svms = &p->svms;
3869 uint32_t num_devices;
3872 num_devices = p->n_pdds;
3873 /* Handle one SVM range object at a time, also the number of gpus are
3874 * assumed to be same on the restore node, checking must be done while
3875 * evaluating the topology earlier
3878 svm_attrs_size = sizeof(struct kfd_ioctl_svm_attribute) *
3879 (nattr_common + nattr_accessibility * num_devices);
3880 svm_object_md_size = sizeof(struct criu_svm_metadata) + svm_attrs_size;
3882 svm_priv_data_size = sizeof(struct kfd_criu_svm_range_priv_data) +
3885 criu_svm_md = kzalloc(svm_object_md_size, GFP_KERNEL);
3887 pr_err("failed to allocate memory to store svm metadata\n");
3890 if (*priv_data_offset + svm_priv_data_size > max_priv_data_size) {
3895 ret = copy_from_user(&criu_svm_md->data, user_priv_ptr + *priv_data_offset,
3896 svm_priv_data_size);
3901 *priv_data_offset += svm_priv_data_size;
3903 list_add_tail(&criu_svm_md->list, &svms->criu_svm_metadata_list);
3913 int svm_range_get_info(struct kfd_process *p, uint32_t *num_svm_ranges,
3914 uint64_t *svm_priv_data_size)
3916 uint64_t total_size, accessibility_size, common_attr_size;
3917 int nattr_common = 4, nattr_accessibility = 1;
3918 int num_devices = p->n_pdds;
3919 struct svm_range_list *svms;
3920 struct svm_range *prange;
3923 *svm_priv_data_size = 0;
3929 mutex_lock(&svms->lock);
3930 list_for_each_entry(prange, &svms->list, list) {
3931 pr_debug("prange: 0x%p start: 0x%lx\t npages: 0x%llx\t end: 0x%llx\n",
3932 prange, prange->start, prange->npages,
3933 prange->start + prange->npages - 1);
3936 mutex_unlock(&svms->lock);
3938 *num_svm_ranges = count;
3939 /* Only the accessbility attributes need to be queried for all the gpus
3940 * individually, remaining ones are spanned across the entire process
3941 * regardless of the various gpu nodes. Of the remaining attributes,
3942 * KFD_IOCTL_SVM_ATTR_CLR_FLAGS need not be saved.
3944 * KFD_IOCTL_SVM_ATTR_PREFERRED_LOC
3945 * KFD_IOCTL_SVM_ATTR_PREFETCH_LOC
3946 * KFD_IOCTL_SVM_ATTR_SET_FLAGS
3947 * KFD_IOCTL_SVM_ATTR_GRANULARITY
3949 * ** ACCESSBILITY ATTRIBUTES **
3950 * (Considered as one, type is altered during query, value is gpuid)
3951 * KFD_IOCTL_SVM_ATTR_ACCESS
3952 * KFD_IOCTL_SVM_ATTR_ACCESS_IN_PLACE
3953 * KFD_IOCTL_SVM_ATTR_NO_ACCESS
3955 if (*num_svm_ranges > 0) {
3956 common_attr_size = sizeof(struct kfd_ioctl_svm_attribute) *
3958 accessibility_size = sizeof(struct kfd_ioctl_svm_attribute) *
3959 nattr_accessibility * num_devices;
3961 total_size = sizeof(struct kfd_criu_svm_range_priv_data) +
3962 common_attr_size + accessibility_size;
3964 *svm_priv_data_size = *num_svm_ranges * total_size;
3967 pr_debug("num_svm_ranges %u total_priv_size %llu\n", *num_svm_ranges,
3968 *svm_priv_data_size);
3972 int kfd_criu_checkpoint_svm(struct kfd_process *p,
3973 uint8_t __user *user_priv_data,
3974 uint64_t *priv_data_offset)
3976 struct kfd_criu_svm_range_priv_data *svm_priv = NULL;
3977 struct kfd_ioctl_svm_attribute *query_attr = NULL;
3978 uint64_t svm_priv_data_size, query_attr_size = 0;
3979 int index, nattr_common = 4, ret = 0;
3980 struct svm_range_list *svms;
3981 int num_devices = p->n_pdds;
3982 struct svm_range *prange;
3983 struct mm_struct *mm;
3989 mm = get_task_mm(p->lead_thread);
3991 pr_err("failed to get mm for the target process\n");
3995 query_attr_size = sizeof(struct kfd_ioctl_svm_attribute) *
3996 (nattr_common + num_devices);
3998 query_attr = kzalloc(query_attr_size, GFP_KERNEL);
4004 query_attr[0].type = KFD_IOCTL_SVM_ATTR_PREFERRED_LOC;
4005 query_attr[1].type = KFD_IOCTL_SVM_ATTR_PREFETCH_LOC;
4006 query_attr[2].type = KFD_IOCTL_SVM_ATTR_SET_FLAGS;
4007 query_attr[3].type = KFD_IOCTL_SVM_ATTR_GRANULARITY;
4009 for (index = 0; index < num_devices; index++) {
4010 struct kfd_process_device *pdd = p->pdds[index];
4012 query_attr[index + nattr_common].type =
4013 KFD_IOCTL_SVM_ATTR_ACCESS;
4014 query_attr[index + nattr_common].value = pdd->user_gpu_id;
4017 svm_priv_data_size = sizeof(*svm_priv) + query_attr_size;
4019 svm_priv = kzalloc(svm_priv_data_size, GFP_KERNEL);
4026 list_for_each_entry(prange, &svms->list, list) {
4028 svm_priv->object_type = KFD_CRIU_OBJECT_TYPE_SVM_RANGE;
4029 svm_priv->start_addr = prange->start;
4030 svm_priv->size = prange->npages;
4031 memcpy(&svm_priv->attrs, query_attr, query_attr_size);
4032 pr_debug("CRIU: prange: 0x%p start: 0x%lx\t npages: 0x%llx end: 0x%llx\t size: 0x%llx\n",
4033 prange, prange->start, prange->npages,
4034 prange->start + prange->npages - 1,
4035 prange->npages * PAGE_SIZE);
4037 ret = svm_range_get_attr(p, mm, svm_priv->start_addr,
4039 (nattr_common + num_devices),
4042 pr_err("CRIU: failed to obtain range attributes\n");
4046 if (copy_to_user(user_priv_data + *priv_data_offset, svm_priv,
4047 svm_priv_data_size)) {
4048 pr_err("Failed to copy svm priv to user\n");
4053 *priv_data_offset += svm_priv_data_size;
4068 svm_ioctl(struct kfd_process *p, enum kfd_ioctl_svm_op op, uint64_t start,
4069 uint64_t size, uint32_t nattrs, struct kfd_ioctl_svm_attribute *attrs)
4071 struct mm_struct *mm = current->mm;
4074 start >>= PAGE_SHIFT;
4075 size >>= PAGE_SHIFT;
4078 case KFD_IOCTL_SVM_OP_SET_ATTR:
4079 r = svm_range_set_attr(p, mm, start, size, nattrs, attrs);
4081 case KFD_IOCTL_SVM_OP_GET_ATTR:
4082 r = svm_range_get_attr(p, mm, start, size, nattrs, attrs);