// SPDX-License-Identifier: GPL-2.0 OR MIT /* * Copyright 2020-2021 Advanced Micro Devices, Inc. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE.
*/
/* Long enough to ensure no retry fault comes after svm range is restored and * page table is updated.
*/ #define AMDGPU_SVM_RANGE_RETRY_FAULT_PENDING (2UL * NSEC_PER_MSEC) #if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) #define dynamic_svm_range_dump(svms) \
_dynamic_func_call_no_desc("svm_range_dump", svm_range_debug_dump, svms) #else #define dynamic_svm_range_dump(svms) \ do { if (0) svm_range_debug_dump(svms); } while (0) #endif
/* Giant svm range split into smaller ranges based on this, it is decided using * minimum of all dGPU/APU 1/32 VRAM size, between 2MB to 1GB and alignment to * power of 2MB.
*/ static uint64_t max_svm_range_pages;
/** * svm_range_unlink - unlink svm_range from lists and interval tree * @prange: svm range structure to be removed * * Remove the svm_range from the svms and svm_bo lists and the svms * interval tree. * * Context: The caller must hold svms->lock
*/ staticvoid svm_range_unlink(struct svm_range *prange)
{
pr_debug("svms 0x%p prange 0x%p [0x%lx 0x%lx]\n", prange->svms,
prange, prange->start, prange->last);
if (prange->svm_bo) {
spin_lock(&prange->svm_bo->list_lock);
list_del(&prange->svm_bo_list);
spin_unlock(&prange->svm_bo->list_lock);
}
mmu_interval_notifier_insert_locked(&prange->notifier, mm,
prange->start << PAGE_SHIFT,
prange->npages << PAGE_SHIFT,
&svm_range_mn_ops);
}
/** * svm_range_add_to_svms - add svm range to svms * @prange: svm range structure to be added * * Add the svm range to svms interval tree and link list * * Context: The caller must hold svms->lock
*/ staticvoid svm_range_add_to_svms(struct svm_range *prange)
{
pr_debug("svms 0x%p prange 0x%p [0x%lx 0x%lx]\n", prange->svms,
prange, prange->start, prange->last);
spin_lock(&svm_bo->list_lock); while (!list_empty(&svm_bo->range_list)) { struct svm_range *prange =
list_first_entry(&svm_bo->range_list, struct svm_range, svm_bo_list); /* list_del_init tells a concurrent svm_range_vram_node_new when * it's safe to reuse the svm_bo pointer and svm_bo_list head.
*/
list_del_init(&prange->svm_bo_list);
spin_unlock(&svm_bo->list_lock);
pr_debug("svms 0x%p [0x%lx 0x%lx]\n", prange->svms,
prange->start, prange->last);
mutex_lock(&prange->lock);
prange->svm_bo = NULL; /* prange should not hold vram page now */
WARN_ONCE(prange->actual_loc, "prange should not hold vram page");
mutex_unlock(&prange->lock);
mm = svm_bo->eviction_fence->mm; /* * The forked child process takes svm_bo device pages ref, svm_bo could be * released after parent process is gone.
*/
p = kfd_lookup_process_by_mm(mm); if (p) {
pdd = kfd_get_process_device_data(svm_bo->node, p); if (pdd)
atomic64_sub(amdgpu_bo_size(svm_bo->bo), &pdd->vram_usage);
kfd_unref_process(p);
}
mmput(mm);
}
if (!dma_fence_is_signaled(&svm_bo->eviction_fence->base)) /* We're not in the eviction worker. Signal the fence. */
dma_fence_signal(&svm_bo->eviction_fence->base);
dma_fence_put(&svm_bo->eviction_fence->base);
amdgpu_bo_unref(&svm_bo->bo);
kfree(svm_bo);
}
staticvoid svm_range_bo_unref(struct svm_range_bo *svm_bo)
{ if (svm_bo)
kref_put(&svm_bo->kref, svm_range_bo_release);
}
staticbool
svm_range_validate_svm_bo(struct kfd_node *node, struct svm_range *prange)
{
mutex_lock(&prange->lock); if (!prange->svm_bo) {
mutex_unlock(&prange->lock); returnfalse;
} if (prange->ttm_res) { /* We still have a reference, all is well */
mutex_unlock(&prange->lock); returntrue;
} if (svm_bo_ref_unless_zero(prange->svm_bo)) { /* * Migrate from GPU to GPU, remove range from source svm_bo->node * range list, and return false to allocate svm_bo from destination * node.
*/ if (prange->svm_bo->node != node) {
mutex_unlock(&prange->lock);
svm_range_bo_unref(prange->svm_bo); returnfalse;
} if (READ_ONCE(prange->svm_bo->evicting)) { struct dma_fence *f; struct svm_range_bo *svm_bo; /* The BO is getting evicted, * we need to get a new one
*/
mutex_unlock(&prange->lock);
svm_bo = prange->svm_bo;
f = dma_fence_get(&svm_bo->eviction_fence->base);
svm_range_bo_unref(prange->svm_bo); /* wait for the fence to avoid long spin-loop * at list_empty_careful
*/
dma_fence_wait(f, false);
dma_fence_put(f);
} else { /* The BO was still around and we got * a new reference to it
*/
mutex_unlock(&prange->lock);
pr_debug("reuse old bo svms 0x%p [0x%lx 0x%lx]\n",
prange->svms, prange->start, prange->last);
/* We need a new svm_bo. Spin-loop to wait for concurrent * svm_range_bo_release to finish removing this range from * its range list and set prange->svm_bo to null. After this, * it is safe to reuse the svm_bo pointer and svm_bo_list head.
*/ while (!list_empty_careful(&prange->svm_bo_list) || prange->svm_bo)
cond_resched();
p = container_of(prange->svms, struct kfd_process, svms);
pdd = kfd_process_device_data_by_id(p, gpu_id); if (!pdd) {
pr_debug("failed to get kfd process device by id 0x%x\n", gpu_id); return NULL;
}
for (i = 0; i < nattr; i++) {
uint32_t val = attrs[i].value; int gpuidx = MAX_GPU_INSTANCE;
switch (attrs[i].type) { case KFD_IOCTL_SVM_ATTR_PREFERRED_LOC: if (val != KFD_IOCTL_SVM_LOCATION_SYSMEM &&
val != KFD_IOCTL_SVM_LOCATION_UNDEFINED)
gpuidx = kfd_process_gpuidx_from_gpuid(p, val); break; case KFD_IOCTL_SVM_ATTR_PREFETCH_LOC: if (val != KFD_IOCTL_SVM_LOCATION_SYSMEM)
gpuidx = kfd_process_gpuidx_from_gpuid(p, val); break; case KFD_IOCTL_SVM_ATTR_ACCESS: case KFD_IOCTL_SVM_ATTR_ACCESS_IN_PLACE: case KFD_IOCTL_SVM_ATTR_NO_ACCESS:
gpuidx = kfd_process_gpuidx_from_gpuid(p, val); break; case KFD_IOCTL_SVM_ATTR_SET_FLAGS: break; case KFD_IOCTL_SVM_ATTR_CLR_FLAGS: break; case KFD_IOCTL_SVM_ATTR_GRANULARITY: break; default:
pr_debug("unknown attr type 0x%x\n", attrs[i].type); return -EINVAL;
}
for (i = 0; i < nattr; i++) { switch (attrs[i].type) { case KFD_IOCTL_SVM_ATTR_PREFERRED_LOC:
prange->preferred_loc = attrs[i].value; break; case KFD_IOCTL_SVM_ATTR_PREFETCH_LOC:
prange->prefetch_loc = attrs[i].value; break; case KFD_IOCTL_SVM_ATTR_ACCESS: case KFD_IOCTL_SVM_ATTR_ACCESS_IN_PLACE: case KFD_IOCTL_SVM_ATTR_NO_ACCESS: if (!p->xnack_enabled)
*update_mapping = true;
for (i = 0; i < nattr; i++) { switch (attrs[i].type) { case KFD_IOCTL_SVM_ATTR_PREFERRED_LOC: if (prange->preferred_loc != attrs[i].value) returnfalse; break; case KFD_IOCTL_SVM_ATTR_PREFETCH_LOC: /* Prefetch should always trigger a migration even * if the value of the attribute didn't change.
*/ returnfalse; case KFD_IOCTL_SVM_ATTR_ACCESS: case KFD_IOCTL_SVM_ATTR_ACCESS_IN_PLACE: case KFD_IOCTL_SVM_ATTR_NO_ACCESS:
gpuidx = kfd_process_gpuidx_from_gpuid(p,
attrs[i].value); if (attrs[i].type == KFD_IOCTL_SVM_ATTR_NO_ACCESS) { if (test_bit(gpuidx, prange->bitmap_access) ||
test_bit(gpuidx, prange->bitmap_aip)) returnfalse;
} elseif (attrs[i].type == KFD_IOCTL_SVM_ATTR_ACCESS) { if (!test_bit(gpuidx, prange->bitmap_access)) returnfalse;
} else { if (!test_bit(gpuidx, prange->bitmap_aip)) returnfalse;
} break; case KFD_IOCTL_SVM_ATTR_SET_FLAGS: if ((prange->flags & attrs[i].value) != attrs[i].value) returnfalse; break; case KFD_IOCTL_SVM_ATTR_CLR_FLAGS: if ((prange->flags & attrs[i].value) != 0) returnfalse; break; case KFD_IOCTL_SVM_ATTR_GRANULARITY: if (prange->granularity != attrs[i].value) returnfalse; break; default:
WARN_ONCE(1, "svm_range_check_attrs wasn't called?");
}
}
returntrue;
}
/** * svm_range_debug_dump - print all range information from svms * @svms: svm range list header * * debug output svm range start, end, prefetch location from svms * interval tree and link list * * Context: The caller must hold svms->lock
*/ staticvoid svm_range_debug_dump(struct svm_range_list *svms)
{ struct interval_tree_node *node; struct svm_range *prange;
*vram_pages = 0; for (i = 0; i < num_elements; i++) {
dma_addr_t *temp;
temp = (dma_addr_t *)dst + i;
*temp = *((dma_addr_t *)src + i); if (*temp&SVM_RANGE_VRAM_DOMAIN)
(*vram_pages)++;
}
return (void *)dst;
}
staticint
svm_range_copy_dma_addrs(struct svm_range *dst, struct svm_range *src)
{ int i;
for (i = 0; i < MAX_GPU_INSTANCE; i++) { if (!src->dma_addr[i]) continue;
dst->dma_addr[i] = svm_range_copy_array(src->dma_addr[i], sizeof(*src->dma_addr[i]), src->npages, 0, NULL); if (!dst->dma_addr[i]) return -ENOMEM;
}
/** * svm_range_split_adjust - split range and adjust * * @new: new range * @old: the old range * @start: the old range adjust to start address in pages * @last: the old range adjust to last address in pages * * Copy system memory dma_addr or vram ttm_res in old range to new * range from new_start up to size new->npages, the remaining old range is from * start to last * * Return: * 0 - OK, -ENOMEM - out of memory
*/ staticint
svm_range_split_adjust(struct svm_range *new, struct svm_range *old,
uint64_t start, uint64_t last)
{ int r;
pr_debug("svms 0x%p new 0x%lx old [0x%lx 0x%lx] => [0x%llx 0x%llx]\n",
new->svms, new->start, old->start, old->last, start, last);
if (new->start < old->start ||
new->last > old->last) {
WARN_ONCE(1, "invalid new range start or last\n"); return -EINVAL;
}
r = svm_range_split_pages(new, old, start, last); if (r) return r;
if (old->actual_loc && old->ttm_res) {
r = svm_range_split_nodes(new, old, start, last); if (r) return r;
}
/** * svm_range_split - split a range in 2 ranges * * @prange: the svm range to split * @start: the remaining range start address in pages * @last: the remaining range last address in pages * @new: the result new range generated * * Two cases only: * case 1: if start == prange->start * prange ==> prange[start, last] * new range [last + 1, prange->last] * * case 2: if last == prange->last * prange ==> prange[start, last] * new range [prange->start, start - 1] * * Return: * 0 - OK, -ENOMEM - out of memory, -EINVAL - invalid start, last
*/ staticint
svm_range_split(struct svm_range *prange, uint64_t start, uint64_t last, struct svm_range **new)
{
uint64_t old_start = prange->start;
uint64_t old_last = prange->last; struct svm_range_list *svms; int r = 0;
for (i = offset; i < offset + npages; i++) {
last_domain = dma_addr[i] & SVM_RANGE_VRAM_DOMAIN;
dma_addr[i] &= ~SVM_RANGE_VRAM_DOMAIN;
/* Collect all pages in the same address range and memory domain * that can be mapped with a single call to update mapping.
*/ if (i < offset + npages - 1 &&
last_domain == (dma_addr[i + 1] & SVM_RANGE_VRAM_DOMAIN)) continue;
pr_debug("Mapping range [0x%lx 0x%llx] on domain: %s\n",
last_start, prange->start + i, last_domain ? "GPU" : "CPU");
pte_flags = svm_range_get_pte_flags(pdd->dev, prange, last_domain); if (readonly)
pte_flags &= ~AMDGPU_PTE_WRITEABLE;
/* For dGPU mode, we use same vm_manager to allocate VRAM for * different memory partition based on fpfn/lpfn, we should use * same vm_manager.vram_base_offset regardless memory partition.
*/
r = amdgpu_vm_update_range(adev, vm, false, false, flush_tlb, true,
NULL, last_start, prange->start + i,
pte_flags,
(last_start - prange->start) << PAGE_SHIFT,
bo_adev ? bo_adev->vm_manager.vram_base_offset : 0,
NULL, dma_addr, &vm->last_update);
pdd = kfd_process_device_from_gpuidx(p, gpuidx); if (!pdd) return NULL;
return SVM_ADEV_PGMAP_OWNER(pdd->dev->adev);
}
/* * Validation+GPU mapping with concurrent invalidation (MMU notifiers) * * To prevent concurrent destruction or change of range attributes, the * svm_read_lock must be held. The caller must not hold the svm_write_lock * because that would block concurrent evictions and lead to deadlocks. To * serialize concurrent migrations or validations of the same range, the * prange->migrate_mutex must be held. * * For VRAM ranges, the SVM BO must be allocated and valid (protected by its * eviction fence. * * The following sequence ensures race-free validation and GPU mapping: * * 1. Reserve page table (and SVM BO if range is in VRAM) * 2. hmm_range_fault to get page addresses (if system memory) * 3. DMA-map pages (if system memory) * 4-a. Take notifier lock * 4-b. Check that pages still valid (mmu_interval_read_retry) * 4-c. Check that the range was not split or otherwise invalidated * 4-d. Update GPU page table * 4.e. Release notifier lock * 5. Release page table (and SVM BO) reservation
*/ staticint svm_range_validate_and_map(struct mm_struct *mm, unsignedlong map_start, unsignedlong map_last, struct svm_range *prange, int32_t gpuidx, bool intr, bool wait, bool flush_tlb)
{ struct svm_validate_context *ctx; unsignedlong start, end, addr; struct kfd_process *p; void *owner;
int32_t idx; int r = 0;
/* If prefetch range to GPU, or GPU retry fault migrate range to * GPU, which has ACCESS attribute to the range, create mapping * on that GPU.
*/ if (prange->actual_loc) {
gpuidx = kfd_process_gpuidx_from_gpuid(ctx->process,
prange->actual_loc); if (gpuidx < 0) {
WARN_ONCE(1, "failed get device by id 0x%x\n",
prange->actual_loc);
r = -EINVAL; goto free_ctx;
} if (test_bit(gpuidx, prange->bitmap_access))
bitmap_set(ctx->bitmap, gpuidx, 1);
}
/* * If prange is already mapped or with always mapped flag, * update mapping on GPUs with ACCESS attribute
*/ if (bitmap_empty(ctx->bitmap, MAX_GPU_INSTANCE)) { if (prange->mapped_to_gpu ||
prange->flags & KFD_IOCTL_SVM_FLAG_GPU_ALWAYS_MAPPED)
bitmap_copy(ctx->bitmap, prange->bitmap_access, MAX_GPU_INSTANCE);
}
} else {
bitmap_or(ctx->bitmap, prange->bitmap_access,
prange->bitmap_aip, MAX_GPU_INSTANCE);
}
if (bitmap_empty(ctx->bitmap, MAX_GPU_INSTANCE)) {
r = 0; goto free_ctx;
}
if (prange->actual_loc && !prange->ttm_res) { /* This should never happen. actual_loc gets set by * svm_migrate_ram_to_vram after allocating a BO.
*/
WARN_ONCE(1, "VRAM BO missing during validation\n");
r = -EINVAL; goto free_ctx;
}
r = svm_range_reserve_bos(ctx, intr); if (r) goto free_ctx;
next = min(vma->vm_end, end);
npages = (next - addr) >> PAGE_SHIFT; /* HMM requires at least READ permissions. If provided with PROT_NONE, * unmap the memory. If it's not already mapped, this is a no-op * If PROT_WRITE is provided without READ, warn first then unmap
*/ if (!(vma->vm_flags & VM_READ)) { unsignedlong e, s;
svm_range_lock(prange); if (vma->vm_flags & VM_WRITE)
pr_debug("VM_WRITE without VM_READ is not supported");
s = max(start, prange->start);
e = min(end, prange->last); if (e >= s)
r = svm_range_unmap_from_gpus(prange, s, e,
KFD_SVM_UNMAP_TRIGGER_UNMAP_FROM_CPU);
svm_range_unlock(prange); /* If unmap returns non-zero, we'll bail on the next for loop * iteration, so just leave r and continue
*/
addr = next; continue;
}
WRITE_ONCE(p->svms.faulting_task, current);
r = amdgpu_hmm_range_get_pages(&prange->notifier, addr, npages,
readonly, owner, NULL,
&hmm_range);
WRITE_ONCE(p->svms.faulting_task, NULL); if (r)
pr_debug("failed %d to get svm range pages\n", r);
} else {
r = -EFAULT;
}
if (!r) {
offset = (addr >> PAGE_SHIFT) - prange->start;
r = svm_range_dma_map(prange, ctx->bitmap, offset, npages,
hmm_range->hmm_pfns); if (r)
pr_debug("failed %d to dma map range\n", r);
}
svm_range_lock(prange);
/* Free backing memory of hmm_range if it was initialized * Overrride return value to TRY AGAIN only if prior returns * were successful
*/ if (hmm_range && amdgpu_hmm_range_get_pages_done(hmm_range) && !r) {
pr_debug("hmm update the range, need validate again\n");
r = -EAGAIN;
}
if (!r && !list_empty(&prange->child_list)) {
pr_debug("range split by unmap in parallel, validate again\n");
r = -EAGAIN;
}
p = container_of(svms, struct kfd_process, svms);
process_info = p->kgd_process_info;
/* Keep mm reference when svm_range_validate_and_map ranges */
mm = get_task_mm(p->lead_thread); if (!mm) {
pr_debug("svms 0x%p process mm gone\n", svms); return;
}
/* * If range is migrating, wait for migration is done.
*/
mutex_lock(&prange->migrate_mutex);
r = svm_range_validate_and_map(mm, prange->start, prange->last, prange,
MAX_GPU_INSTANCE, false, true, false); if (r)
pr_debug("failed %d to map 0x%lx to gpus\n", r,
prange->start);
mutex_unlock(&prange->migrate_mutex); if (r) goto out_reschedule;
if (atomic_cmpxchg(&prange->invalid, invalid, 0) != invalid) goto out_reschedule;
}
if (atomic_cmpxchg(&svms->evicted_ranges, evicted_ranges, 0) !=
evicted_ranges) goto out_reschedule;
evicted_ranges = 0;
r = kgd2kfd_resume_mm(mm); if (r) { /* No recovery from this failure. Probably the CP is * hanging. No point trying again.
*/
pr_debug("failed %d to resume KFD\n", r);
}
/* If validation failed, reschedule another attempt */ if (evicted_ranges) {
pr_debug("reschedule to restore svm range\n");
queue_delayed_work(system_freezable_wq, &svms->restore_work,
msecs_to_jiffies(AMDGPU_SVM_RANGE_RESTORE_DELAY_MS));
/** * svm_range_evict - evict svm range * @prange: svm range structure * @mm: current process mm_struct * @start: starting process queue number * @last: last process queue number * @event: mmu notifier event when range is evicted or migrated * * Stop all queues of the process to ensure GPU doesn't access the memory, then * return to let CPU evict the buffer and proceed CPU pagetable update. * * Don't need use lock to sync cpu pagetable invalidation with GPU execution. * If invalidation happens while restore work is running, restore work will * restart to ensure to get the latest CPU pages mapping to GPU, then start * the queues.
*/ staticint
svm_range_evict(struct svm_range *prange, struct mm_struct *mm, unsignedlong start, unsignedlong last, enum mmu_notifier_event event)
{ struct svm_range_list *svms = prange->svms; struct svm_range *pchild; struct kfd_process *p; int r = 0;
/** * svm_range_add - add svm range and handle overlap * @p: the range add to this process svms * @start: page size aligned * @size: page size aligned * @nattr: number of attributes * @attrs: array of attributes * @update_list: output, the ranges need validate and update GPU mapping * @insert_list: output, the ranges need insert to svms * @remove_list: output, the ranges are replaced and need remove from svms * @remap_list: output, remap unaligned svm ranges * * Check if the virtual address range has overlap with any existing ranges, * split partly overlapping ranges and add new ranges in the gaps. All changes * should be applied to the range_list and interval tree transactionally. If * any range split or allocation fails, the entire update fails. Therefore any * existing overlapping svm_ranges are cloned and the original svm_ranges left * unchanged. * * If the transaction succeeds, the caller can update and insert clones and * new ranges, then free the originals. * * Otherwise the caller can free the clones and new ranges, while the old * svm_ranges remain unchanged. * * Context: Process context, caller must hold svms->lock * * Return: * 0 - OK, otherwise error code
*/ staticint
svm_range_add(struct kfd_process *p, uint64_t start, uint64_t size,
uint32_t nattr, struct kfd_ioctl_svm_attribute *attrs, struct list_head *update_list, struct list_head *insert_list, struct list_head *remove_list, struct list_head *remap_list)
{ unsignedlong last = start + size - 1UL; struct svm_range_list *svms = &p->svms; struct interval_tree_node *node; struct svm_range *prange; struct svm_range *tmp; struct list_head new_list; int r = 0;
if (svm_range_is_same_attrs(p, prange, nattr, attrs) &&
prange->mapped_to_gpu) { /* nothing to do */
} elseif (node->start < start || node->last > last) { /* node intersects the update range and its attributes * will change. Clone and split it, apply updates only * to the overlapping part
*/ struct svm_range *old = prange;
prange = svm_range_clone(old); if (!prange) {
r = -ENOMEM; goto out;
}
if (node->start < start) {
pr_debug("change old range start\n");
r = svm_range_split_head(prange, start,
insert_list, remap_list); if (r) goto out;
} if (node->last > last) {
pr_debug("change old range last\n");
r = svm_range_split_tail(prange, last,
insert_list, remap_list); if (r) goto out;
}
} else { /* The node is contained within start..last, * just update it
*/
list_add(&prange->update_list, update_list);
}
/* insert a new node if needed */ if (node->start > start) {
r = svm_range_split_new(svms, start, node->start - 1,
READ_ONCE(max_svm_range_pages),
&new_list, update_list); if (r) goto out;
}
node = next;
start = next_start;
}
/* add a final range at the end if needed */ if (start <= last)
r = svm_range_split_new(svms, start, last,
READ_ONCE(max_svm_range_pages),
&new_list, update_list);
pr_debug("prange 0x%p [0x%lx 0x%lx] op %d\n", prange,
prange->start, prange->last, prange->work_item.op);
mm = prange->work_item.mm;
mmap_write_lock(mm);
/* Remove from deferred_list must be inside mmap write lock, for * two race cases: * 1. unmap_from_cpu may change work_item.op and add the range * to deferred_list again, cause use after free bug. * 2. svm_range_list_lock_and_flush_work may hold mmap write * lock and continue because deferred_list is empty, but * deferred_list work is actually waiting for mmap lock.
*/
spin_lock(&svms->deferred_list_lock);
list_del_init(&prange->deferred_list);
spin_unlock(&svms->deferred_list_lock);
mutex_lock(&svms->lock);
mutex_lock(&prange->migrate_mutex); while (!list_empty(&prange->child_list)) { struct svm_range *pchild;
/* Pairs with mmget in svm_range_add_list_work. If dropping the * last mm refcount, schedule release work to avoid circular locking
*/
mmput_async(mm);
void
svm_range_add_list_work(struct svm_range_list *svms, struct svm_range *prange, struct mm_struct *mm, enum svm_work_list_ops op)
{
spin_lock(&svms->deferred_list_lock); /* if prange is on the deferred list */ if (!list_empty(&prange->deferred_list)) {
pr_debug("update exist prange 0x%p work op %d\n", prange, op);
WARN_ONCE(prange->work_item.mm != mm, "unmatch mm\n"); if (op != SVM_OP_NULL &&
prange->work_item.op != SVM_OP_UNMAP_RANGE)
prange->work_item.op = op;
} else { /* Pairs with mmput in deferred_list_work. * If process is exiting and mm is gone, don't update mmu notifier.
*/ if (mmget_not_zero(mm)) {
prange->work_item.mm = mm;
prange->work_item.op = op;
list_add_tail(&prange->deferred_list,
&prange->svms->deferred_range_list);
pr_debug("add prange 0x%p [0x%lx 0x%lx] to work list op %d\n",
prange, prange->start, prange->last, op);
}
}
spin_unlock(&svms->deferred_list_lock);
}
/* calculate time stamps that are used to decide which page faults need be * dropped or handled before unmap pages from gpu vm
*/
for_each_set_bit(i, svms->bitmap_supported, p->n_pdds) { struct kfd_process_device *pdd; struct amdgpu_device *adev; struct amdgpu_ih_ring *ih;
uint32_t checkpoint_wptr;
pdd = p->pdds[i]; if (!pdd) continue;
adev = pdd->dev->adev;
/* Check and drain ih1 ring if cam not available */ if (adev->irq.ih1.ring_size) {
ih = &adev->irq.ih1;
checkpoint_wptr = amdgpu_ih_get_wptr(adev, ih); if (ih->rptr != checkpoint_wptr) {
svms->checkpoint_ts[i] =
amdgpu_ih_decode_iv_ts(adev, ih, checkpoint_wptr, -1); continue;
}
}
/* check if dev->irq.ih_soft is not empty */
ih = &adev->irq.ih_soft;
checkpoint_wptr = amdgpu_ih_get_wptr(adev, ih); if (ih->rptr != checkpoint_wptr)
svms->checkpoint_ts[i] = amdgpu_ih_decode_iv_ts(adev, ih, checkpoint_wptr, -1);
}
unmap_parent = start <= prange->start && last >= prange->last;
list_for_each_entry(pchild, &prange->child_list, child_list) {
mutex_lock_nested(&pchild->lock, 1);
s = max(start, pchild->start);
l = min(last, pchild->last); if (l >= s)
svm_range_unmap_from_gpus(pchild, s, l, trigger);
svm_range_unmap_split(prange, pchild, start, last);
mutex_unlock(&pchild->lock);
}
s = max(start, prange->start);
l = min(last, prange->last); if (l >= s)
svm_range_unmap_from_gpus(prange, s, l, trigger);
svm_range_unmap_split(prange, prange, start, last);
if (unmap_parent)
svm_range_add_list_work(svms, prange, mm, SVM_OP_UNMAP_RANGE); else
svm_range_add_list_work(svms, prange, mm,
SVM_OP_UPDATE_RANGE_NOTIFIER);
schedule_deferred_list_work(svms);
kfd_unref_process(p);
}
/** * svm_range_cpu_invalidate_pagetables - interval notifier callback * @mni: mmu_interval_notifier struct * @range: mmu_notifier_range struct * @cur_seq: value to pass to mmu_interval_set_seq() * * If event is MMU_NOTIFY_UNMAP, this is from CPU unmap range, otherwise, it * is from migration, or CPU page invalidation callback. * * For unmap event, unmap range from GPUs, remove prange from svms in a delayed * work thread, and split prange if only part of prange is unmapped. * * For invalidation event, if GPU retry fault is not enabled, evict the queues, * then schedule svm_range_restore_work to update GPU mapping and resume queues. * If GPU retry fault is enabled, unmap the svm range from GPU, retry fault will * update GPU mapping to recover. * * Context: mmap lock, notifier_invalidate_start lock are held * for invalidate event, prange lock is held if this is from migration
*/ staticbool
svm_range_cpu_invalidate_pagetables(struct mmu_interval_notifier *mni, conststruct mmu_notifier_range *range, unsignedlong cur_seq)
{ struct svm_range *prange; unsignedlong start; unsignedlong last;
if (range->event == MMU_NOTIFY_RELEASE) returntrue;
switch (range->event) { case MMU_NOTIFY_UNMAP:
svm_range_unmap_from_cpu(mni->mm, prange, start, last); break; default:
svm_range_evict(prange, mni->mm, start, last, range->event); break;
}
svm_range_unlock(prange);
returntrue;
}
/** * svm_range_from_addr - find svm range from fault address * @svms: svm range list header * @addr: address to search range interval tree, in pages * @parent: parent range if range is on child list * * Context: The caller must hold svms->lock * * Return: the svm_range found or NULL
*/ struct svm_range *
svm_range_from_addr(struct svm_range_list *svms, unsignedlong addr, struct svm_range **parent)
{ struct interval_tree_node *node; struct svm_range *prange; struct svm_range *pchild;
node = interval_tree_iter_first(&svms->objects, addr, addr); if (!node) return NULL;
/* svm_range_best_restore_location - decide the best fault restore location * @prange: svm range structure * @adev: the GPU on which vm fault happened * * This is only called when xnack is on, to decide the best location to restore * the range mapping after GPU vm fault. Caller uses the best location to do * migration if actual loc is not best location, then update GPU page table * mapping to the best location. * * If the preferred loc is accessible by faulting GPU, use preferred loc. * If vm fault gpu idx is on range ACCESSIBLE bitmap, best_loc is vm fault gpu * If vm fault gpu idx is on range ACCESSIBLE_IN_PLACE bitmap, then * if range actual loc is cpu, best_loc is cpu * if vm fault gpu is on xgmi same hive of range actual loc gpu, best_loc is * range actual loc. * Otherwise, GPU no access, best_loc is -1. * * Return: * -1 means vm fault GPU no access * 0 for CPU or GPU id
*/ static int32_t
svm_range_best_restore_location(struct svm_range *prange, struct kfd_node *node,
int32_t *gpuidx)
{ struct kfd_node *bo_node, *preferred_node; struct kfd_process *p;
uint32_t gpuid; int r;
p = container_of(prange->svms, struct kfd_process, svms);
r = kfd_process_gpuid_from_node(p, node, &gpuid, gpuidx); if (r < 0) {
pr_debug("failed to get gpuid from kgd\n"); return -1;
}
if (node->adev->apu_prefer_gtt) return 0;
if (prange->preferred_loc == gpuid ||
prange->preferred_loc == KFD_IOCTL_SVM_LOCATION_SYSMEM) { return prange->preferred_loc;
} elseif (prange->preferred_loc != KFD_IOCTL_SVM_LOCATION_UNDEFINED) {
preferred_node = svm_range_get_node_by_id(prange, prange->preferred_loc); if (preferred_node && svm_nodes_in_same_hive(node, preferred_node)) return prange->preferred_loc; /* fall through */
}
if (test_bit(*gpuidx, prange->bitmap_access)) return gpuid;
if (test_bit(*gpuidx, prange->bitmap_aip)) { if (!prange->actual_loc) return 0;
/* First range that starts after the fault address */
node = interval_tree_iter_first(&p->svms.objects, addr + 1, ULONG_MAX); if (node) {
end_limit = min(end_limit, node->start); /* Last range that ends before the fault address */
rb_node = rb_prev(&node->rb);
} else { /* Last range must end before addr because * there was no range after addr
*/
rb_node = rb_last(&p->svms.objects.rb_root);
} if (rb_node) {
node = container_of(rb_node, struct interval_tree_node, rb); if (node->last >= addr) {
WARN(1, "Overlap with prev node and page fault addr\n"); return -EFAULT;
}
start_limit = max(start_limit, node->last + 1);
}
if (svm_range_get_range_boundaries(p, addr, &start, &last,
&is_heap_stack)) return NULL;
r = svm_range_check_vm(p, start, last, &bo_s, &bo_l); if (r != -EADDRINUSE)
r = svm_range_check_vm_userptr(p, start, last, &bo_s, &bo_l);
if (r == -EADDRINUSE) { if (addr >= bo_s && addr <= bo_l) return NULL;
/* Create one page svm range if 2MB range overlapping */
start = addr;
last = addr;
}
prange = svm_range_new(&p->svms, start, last, true); if (!prange) {
pr_debug("Failed to create prange in address [0x%llx]\n", addr); return NULL;
} if (kfd_process_gpuid_from_node(p, node, &gpuid, &gpuidx)) {
pr_debug("failed to get gpuid from kgd\n");
svm_range_free(prange, true); return NULL;
}
if (is_heap_stack)
prange->preferred_loc = KFD_IOCTL_SVM_LOCATION_SYSMEM;
/* svm_range_skip_recover - decide if prange can be recovered * @prange: svm range structure * * GPU vm retry fault handle skip recover the range for cases: * 1. prange is on deferred list to be removed after unmap, it is stale fault, * deferred list work will drain the stale fault before free the prange. * 2. prange is on deferred list to add interval notifier after split, or * 3. prange is child range, it is split from parent prange, recover later * after interval notifier is added. * * Return: true to skip recover, false to recover
*/ staticbool svm_range_skip_recover(struct svm_range *prange)
{ struct svm_range_list *svms = prange->svms;
spin_lock(&svms->deferred_list_lock); if (list_empty(&prange->deferred_list) &&
list_empty(&prange->child_list)) {
spin_unlock(&svms->deferred_list_lock); returnfalse;
}
spin_unlock(&svms->deferred_list_lock);
/* fault is on different page of same range * or fault is skipped to recover later * or fault is on invalid virtual address
*/ if (gpuidx == MAX_GPU_INSTANCE) {
uint32_t gpuid; int r;
r = kfd_process_gpuid_from_node(p, node, &gpuid, &gpuidx); if (r < 0) return;
}
/* fault is recovered * or fault cannot recover because GPU no access on the range
*/
pdd = kfd_process_device_from_gpuidx(p, gpuidx); if (pdd)
WRITE_ONCE(pdd->faults, pdd->faults + 1);
}
if (atomic_read(&svms->drain_pagefaults)) {
pr_debug("page fault handling disabled, drop fault 0x%llx\n", addr);
r = 0; goto out;
}
node = kfd_node_by_irq_ids(adev, node_id, vmid); if (!node) {
pr_debug("kfd node does not exist node_id: %d, vmid: %d\n", node_id,
vmid);
r = -EFAULT; goto out;
}
if (kfd_process_gpuid_from_node(p, node, &gpuid, &gpuidx)) {
pr_debug("failed to get gpuid/gpuidex for node_id: %d\n", node_id);
r = -EFAULT; goto out;
}
if (!p->xnack_enabled) {
pr_debug("XNACK not enabled for pasid 0x%x\n", pasid);
r = -EFAULT; goto out;
}
/* p->lead_thread is available as kfd_process_wq_release flush the work * before releasing task ref.
*/
mm = get_task_mm(p->lead_thread); if (!mm) {
pr_debug("svms 0x%p failed to get mm\n", svms);
r = 0; goto out;
}
/* check if this page fault time stamp is before svms->checkpoint_ts */ if (svms->checkpoint_ts[gpuidx] != 0) { if (amdgpu_ih_ts_after_or_equal(ts, svms->checkpoint_ts[gpuidx])) {
pr_debug("draining retry fault, drop fault 0x%llx\n", addr); if (write_locked)
mmap_write_downgrade(mm);
r = -EAGAIN; goto out_unlock_svms;
} else { /* ts is after svms->checkpoint_ts now, reset svms->checkpoint_ts * to zero to avoid following ts wrap around give wrong comparing
*/
svms->checkpoint_ts[gpuidx] = 0;
}
}
prange = svm_range_from_addr(svms, addr, NULL); if (!prange) {
pr_debug("failed to find prange svms 0x%p address [0x%llx]\n",
svms, addr); if (!write_locked) { /* Need the write lock to create new range with MMU notifier. * Also flush pending deferred work to make sure the interval * tree is up to date before we add a new range
*/
mutex_unlock(&svms->lock);
mmap_read_unlock(mm);
mmap_write_lock(mm);
write_locked = true; goto retry_write_locked;
}
prange = svm_range_create_unregistered_range(node, p, mm, addr); if (!prange) {
pr_debug("failed to create unregistered range svms 0x%p address [0x%llx]\n",
svms, addr);
mmap_write_downgrade(mm);
r = -EFAULT; goto out_unlock_svms;
}
} if (write_locked)
mmap_write_downgrade(mm);
mutex_lock(&prange->migrate_mutex);
if (svm_range_skip_recover(prange)) {
amdgpu_gmc_filter_faults_remove(node->adev, addr, pasid);
r = 0; goto out_unlock_range;
}
/* skip duplicate vm fault on different pages of same range */ if (ktime_before(timestamp, ktime_add_ns(prange->validate_timestamp,
AMDGPU_SVM_RANGE_RETRY_FAULT_PENDING))) {
pr_debug("svms 0x%p [0x%lx %lx] already restored\n",
svms, prange->start, prange->last);
r = 0; goto out_unlock_range;
}
/* __do_munmap removed VMA, return success as we are handling stale * retry fault.
*/
vma = vma_lookup(mm, addr << PAGE_SHIFT); if (!vma) {
pr_debug("address 0x%llx VMA is removed\n", addr);
r = 0; goto out_unlock_range;
}
if (!svm_fault_allowed(vma, write_fault)) {
pr_debug("fault addr 0x%llx no %s permission\n", addr,
write_fault ? "write" : "read");
r = -EPERM; goto out_unlock_range;
}
best_loc = svm_range_best_restore_location(prange, node, &gpuidx); if (best_loc == -1) {
pr_debug("svms %p failed get best restore loc [0x%lx 0x%lx]\n",
svms, prange->start, prange->last);
r = -EACCES; goto out_unlock_range;
}
pr_debug("svms %p [0x%lx 0x%lx] best restore 0x%x, actual loc 0x%x\n",
svms, prange->start, prange->last, best_loc,
prange->actual_loc);
/* Align migration range start and size to granularity size */
size = 1UL << prange->granularity;
start = max_t(unsignedlong, ALIGN_DOWN(addr, size), prange->start);
last = min_t(unsignedlong, ALIGN(addr + 1, size) - 1, prange->last); if (prange->actual_loc != 0 || best_loc != 0) { if (best_loc) {
r = svm_migrate_to_vram(prange, best_loc, start, last,
mm, KFD_MIGRATE_TRIGGER_PAGEFAULT_GPU); if (r) {
pr_debug("svm_migrate_to_vram failed (%d) at %llx, falling back to system memory\n",
r, addr); /* Fallback to system memory if migration to * VRAM failed
*/ if (prange->actual_loc && prange->actual_loc != best_loc)
r = svm_migrate_vram_to_ram(prange, mm, start, last,
KFD_MIGRATE_TRIGGER_PAGEFAULT_GPU, NULL); else
r = 0;
}
} else {
r = svm_migrate_vram_to_ram(prange, mm, start, last,
KFD_MIGRATE_TRIGGER_PAGEFAULT_GPU, NULL);
} if (r) {
pr_debug("failed %d to migrate svms %p [0x%lx 0x%lx]\n",
r, svms, start, last); goto out_migrate_fail;
} else {
migration = true;
}
}
r = svm_range_validate_and_map(mm, start, last, prange, gpuidx, false, false, false); if (r)
pr_debug("failed %d to map svms 0x%p [0x%lx 0x%lx] to gpus\n",
r, svms, start, last);
size = (prange->last - prange->start + 1) << PAGE_SHIFT; if (xnack_enabled) {
amdgpu_amdkfd_unreserve_mem_limit(NULL, size,
KFD_IOC_ALLOC_MEM_FLAGS_USERPTR, 0);
} else {
r = amdgpu_amdkfd_reserve_mem_limit(NULL, size,
KFD_IOC_ALLOC_MEM_FLAGS_USERPTR, 0); if (r) goto out_unlock;
reserved_size += size;
}
out_unlock:
svm_range_unlock(prange); if (r) break;
}
if (r)
amdgpu_amdkfd_unreserve_mem_limit(NULL, reserved_size,
KFD_IOC_ALLOC_MEM_FLAGS_USERPTR, 0); else /* Change xnack mode must be inside svms lock, to avoid race with * svm_range_deferred_list_work unreserve memory in parallel.
*/
p->xnack_enabled = xnack_enabled;
/* Ensure list work is finished before process is destroyed */
flush_work(&p->svms.deferred_list_work);
/* * Ensure no retry fault comes in afterwards, as page fault handler will * not find kfd process and take mm lock to recover fault. * stop kfd page fault handing, then wait pending page faults got drained
*/
atomic_set(&p->svms.drain_pagefaults, 1);
svm_range_drain_retry_fault(&p->svms);
for (i = 0; i < p->n_pdds; i++) if (KFD_IS_SVM_API_SUPPORTED(p->pdds[i]->dev->adev))
bitmap_set(svms->bitmap_supported, i, 1);
/* Value of default granularity cannot exceed 0x1B, the * number of pages supported by a 4-level paging table
*/
svms->default_granularity = min_t(u8, amdgpu_svm_default_granularity, 0x1B);
pr_debug("Default SVM Granularity to use: %d\n", svms->default_granularity);
return 0;
}
/** * svm_range_check_vm - check if virtual address range mapped already * @p: current kfd_process * @start: range start address, in pages * @last: range last address, in pages * @bo_s: mapping start address in pages if address range already mapped * @bo_l: mapping last address in pages if address range already mapped * * The purpose is to avoid virtual address ranges already allocated by * kfd_ioctl_alloc_memory_of_gpu ioctl. * It looks for each pdd in the kfd_process. * * Context: Process context * * Return 0 - OK, if the range is not mapped. * Otherwise error code: * -EADDRINUSE - if address is mapped already by kfd_ioctl_alloc_memory_of_gpu * -ERESTARTSYS - A wait for the buffer to become unreserved was interrupted by * a signal. Release all buffer reservations and return to user-space.
*/ staticint
svm_range_check_vm(struct kfd_process *p, uint64_t start, uint64_t last,
uint64_t *bo_s, uint64_t *bo_l)
{ struct amdgpu_bo_va_mapping *mapping; struct interval_tree_node *node;
uint32_t i; int r;
for (i = 0; i < p->n_pdds; i++) { struct amdgpu_vm *vm;
if (!p->pdds[i]->drm_priv) continue;
vm = drm_priv_to_vm(p->pdds[i]->drm_priv);
r = amdgpu_bo_reserve(vm->root.bo, false); if (r) return r;
/** * svm_range_best_prefetch_location - decide the best prefetch location * @prange: svm range structure * * For xnack off: * If range map to single GPU, the best prefetch location is prefetch_loc, which * can be CPU or GPU. * * If range is ACCESS or ACCESS_IN_PLACE by mGPUs, only if mGPU connection on * XGMI same hive, the best prefetch location is prefetch_loc GPU, othervise * the best prefetch location is always CPU, because GPU can not have coherent * mapping VRAM of other GPUs even with large-BAR PCIe connection. * * For xnack on: * If range is not ACCESS_IN_PLACE by mGPUs, the best prefetch location is * prefetch_loc, other GPU access will generate vm fault and trigger migration. * * If range is ACCESS_IN_PLACE by mGPUs, only if mGPU connection on XGMI same * hive, the best prefetch location is prefetch_loc GPU, otherwise the best * prefetch location is always CPU. * * Context: Process context * * Return: * 0 for CPU or GPU id
*/ static uint32_t
svm_range_best_prefetch_location(struct svm_range *prange)
{
DECLARE_BITMAP(bitmap, MAX_GPU_INSTANCE);
uint32_t best_loc = prange->prefetch_loc; struct kfd_process_device *pdd; struct kfd_node *bo_node; struct kfd_process *p;
uint32_t gpuidx;
p = container_of(prange->svms, struct kfd_process, svms);
if (!best_loc || best_loc == KFD_IOCTL_SVM_LOCATION_UNDEFINED) goto out;
bo_node = svm_range_get_node_by_id(prange, best_loc); if (!bo_node) {
WARN_ONCE(1, "failed to get valid kfd node at id%x\n", best_loc);
best_loc = 0; goto out;
}
if (bo_node->adev->apu_prefer_gtt) {
best_loc = 0; goto out;
}
if (p->xnack_enabled)
bitmap_copy(bitmap, prange->bitmap_aip, MAX_GPU_INSTANCE); else
bitmap_or(bitmap, prange->bitmap_access, prange->bitmap_aip,
MAX_GPU_INSTANCE);
for_each_set_bit(gpuidx, bitmap, MAX_GPU_INSTANCE) {
pdd = kfd_process_device_from_gpuidx(p, gpuidx); if (!pdd) {
pr_debug("failed to get device by idx 0x%x\n", gpuidx); continue;
}
if (pdd->dev->adev == bo_node->adev) continue;
if (!svm_nodes_in_same_hive(pdd->dev, bo_node)) {
best_loc = 0; break;
}
}
out:
pr_debug("xnack %d svms 0x%p [0x%lx 0x%lx] best loc 0x%x\n",
p->xnack_enabled, &p->svms, prange->start, prange->last,
best_loc);
return best_loc;
}
/* svm_range_trigger_migration - start page migration if prefetch loc changed * @mm: current process mm_struct * @prange: svm range structure * @migrated: output, true if migration is triggered * * If range perfetch_loc is GPU, actual loc is cpu 0, then migrate the range * from ram to vram. * If range prefetch_loc is cpu 0, actual loc is GPU, then migrate the range * from vram to ram. * * If GPU vm fault retry is not enabled, migration interact with MMU notifier * and restore work: * 1. migrate_vma_setup invalidate pages, MMU notifier callback svm_range_evict * stops all queues, schedule restore work * 2. svm_range_restore_work wait for migration is done by * a. svm_range_validate_vram takes prange->migrate_mutex * b. svm_range_validate_ram HMM get pages wait for CPU fault handle returns * 3. restore work update mappings of GPU, resume all queues. * * Context: Process context * * Return: * 0 - OK, otherwise - error code of migration
*/ staticint
svm_range_trigger_migration(struct mm_struct *mm, struct svm_range *prange, bool *migrated)
{
uint32_t best_loc; int r = 0;
/* when best_loc is a gpu node and same as prange->actual_loc * we still need do migration as prange->actual_loc !=0 does * not mean all pages in prange are vram. hmm migrate will pick * up right pages during migration.
*/ if ((best_loc == KFD_IOCTL_SVM_LOCATION_UNDEFINED) ||
(best_loc == 0 && prange->actual_loc == 0)) return 0;
if (!best_loc) {
r = svm_migrate_vram_to_ram(prange, mm, prange->start, prange->last,
KFD_MIGRATE_TRIGGER_PREFETCH, NULL);
*migrated = !r; return r;
}
r = svm_migrate_to_vram(prange, best_loc, prange->start, prange->last,
mm, KFD_MIGRATE_TRIGGER_PREFETCH);
*migrated = !r;
return 0;
}
int svm_range_schedule_evict_svm_bo(struct amdgpu_amdkfd_fence *fence)
{ /* Dereferencing fence->svm_bo is safe here because the fence hasn't * signaled yet and we're under the protection of the fence->lock. * After the fence is signaled in svm_range_bo_release, we cannot get * here any more. * * Reference is dropped in svm_range_evict_svm_bo_worker.
*/ if (svm_bo_ref_unless_zero(fence->svm_bo)) {
WRITE_ONCE(fence->svm_bo->evicting, 1);
schedule_work(&fence->svm_bo->eviction_work);
}
return 0;
}
staticvoid svm_range_evict_svm_bo_worker(struct work_struct *work)
{ struct svm_range_bo *svm_bo; struct mm_struct *mm; int r = 0;
mutex_lock(&prange->migrate_mutex); do { /* migrate all vram pages in this prange to sys ram * after that prange->actual_loc should be zero
*/
r = svm_migrate_vram_to_ram(prange, mm,
prange->start, prange->last,
KFD_MIGRATE_TRIGGER_TTM_EVICTION, NULL);
} while (!r && prange->actual_loc && --retries);
if (!r && prange->actual_loc)
pr_info_once("Migration failed during eviction");
if (!prange->actual_loc) {
mutex_lock(&prange->lock);
prange->svm_bo = NULL;
mutex_unlock(&prange->lock);
}
mutex_unlock(&prange->migrate_mutex);
/* This is the last reference to svm_bo, after svm_range_vram_node_free * has been called in svm_migrate_vram_to_ram
*/
WARN_ONCE(!r && kref_read(&svm_bo->kref) != 1, "This was not the last reference\n");
svm_range_bo_unref(svm_bo);
}
r = svm_range_check_attr(p, nattr, attrs); if (r) return r;
svms = &p->svms;
mutex_lock(&process_info->lock);
svm_range_list_lock_and_flush_work(svms, mm);
r = svm_range_is_valid(p, start, size); if (r) {
pr_debug("invalid range r=%d\n", r);
mmap_write_unlock(mm); goto out;
}
mutex_lock(&svms->lock);
/* Add new range and split existing ranges as needed */
r = svm_range_add(p, start, size, nattr, attrs, &update_list,
&insert_list, &remove_list, &remap_list); if (r) {
mutex_unlock(&svms->lock);
mmap_write_unlock(mm); goto out;
} /* Apply changes as a transaction */
list_for_each_entry_safe(prange, next, &insert_list, list) {
svm_range_add_to_svms(prange);
svm_range_add_notifier_locked(mm, prange);
}
list_for_each_entry(prange, &update_list, update_list) {
svm_range_apply_attrs(p, prange, nattr, attrs, &update_mapping); /* TODO: unmap ranges from GPU that lost access */
}
list_for_each_entry_safe(prange, next, &remove_list, update_list) {
pr_debug("unlink old 0x%p prange 0x%p [0x%lx 0x%lx]\n",
prange->svms, prange, prange->start,
prange->last);
svm_range_unlink(prange);
svm_range_remove_notifier(prange);
svm_range_free(prange, false);
}
mmap_write_downgrade(mm); /* Trigger migrations and revalidate and map to GPUs as needed. If * this fails we may be left with partially completed actions. There * is no clean way of rolling back to the previous state in such a * case because the rollback wouldn't be guaranteed to work either.
*/
list_for_each_entry(prange, &update_list, update_list) { bool migrated;
mutex_lock(&prange->migrate_mutex);
r = svm_range_trigger_migration(mm, prange, &migrated); if (r) goto out_unlock_range;
if (migrated && (!p->xnack_enabled ||
(prange->flags & KFD_IOCTL_SVM_FLAG_GPU_ALWAYS_MAPPED)) &&
prange->mapped_to_gpu) {
pr_debug("restore_work will update mappings of GPUs\n");
mutex_unlock(&prange->migrate_mutex); continue;
}
if (!migrated && !update_mapping) {
mutex_unlock(&prange->migrate_mutex); continue;
}
/* Flush pending deferred work to avoid racing with deferred actions from * previous memory map changes (e.g. munmap). Concurrent memory map changes * can still race with get_attr because we don't hold the mmap lock. But that * would be a race condition in the application anyway, and undefined * behaviour is acceptable in that case.
*/
flush_work(&p->svms.deferred_list_work);
mmap_read_lock(mm);
r = svm_range_is_valid(p, start, size);
mmap_read_unlock(mm); if (r) {
pr_debug("invalid range r=%d\n", r); return r;
}
for (i = 0; i < nattr; i++) { switch (attrs[i].type) { case KFD_IOCTL_SVM_ATTR_PREFERRED_LOC:
get_preferred_loc = true; break; case KFD_IOCTL_SVM_ATTR_PREFETCH_LOC:
get_prefetch_loc = true; break; case KFD_IOCTL_SVM_ATTR_ACCESS:
get_accessible = true; break; case KFD_IOCTL_SVM_ATTR_SET_FLAGS: case KFD_IOCTL_SVM_ATTR_CLR_FLAGS:
get_flags = true; break; case KFD_IOCTL_SVM_ATTR_GRANULARITY:
get_granularity = true; break; case KFD_IOCTL_SVM_ATTR_ACCESS_IN_PLACE: case KFD_IOCTL_SVM_ATTR_NO_ACCESS:
fallthrough; default:
pr_debug("get invalid attr type 0x%x\n", attrs[i].type); return -EINVAL;
}
}
i = j = 0;
list_for_each_entry(criu_svm_md, &svms->criu_svm_metadata_list, list) {
pr_debug("criu_svm_md[%d]\n\tstart: 0x%llx size: 0x%llx (npages)\n",
i, criu_svm_md->data.start_addr, criu_svm_md->data.size);
for (j = 0; j < num_attrs; j++) {
pr_debug("\ncriu_svm_md[%d]->attrs[%d].type : 0x%x\ncriu_svm_md[%d]->attrs[%d].value : 0x%x\n",
i, j, criu_svm_md->data.attrs[j].type,
i, j, criu_svm_md->data.attrs[j].value); switch (criu_svm_md->data.attrs[j].type) { /* During Checkpoint operation, the query for * KFD_IOCTL_SVM_ATTR_PREFETCH_LOC attribute might * return KFD_IOCTL_SVM_LOCATION_UNDEFINED if they were * not used by the range which was checkpointed. Care * must be taken to not restore with an invalid value * otherwise the gpuidx value will be invalid and * set_attr would eventually fail so just replace those * with another dummy attribute such as * KFD_IOCTL_SVM_ATTR_SET_FLAGS.
*/ case KFD_IOCTL_SVM_ATTR_PREFETCH_LOC: if (criu_svm_md->data.attrs[j].value ==
KFD_IOCTL_SVM_LOCATION_UNDEFINED) {
criu_svm_md->data.attrs[j].type =
KFD_IOCTL_SVM_ATTR_SET_FLAGS;
criu_svm_md->data.attrs[j].value = 0;
} break; case KFD_IOCTL_SVM_ATTR_SET_FLAGS:
set_flags = criu_svm_md->data.attrs[j].value; break; default: break;
}
}
/* CLR_FLAGS is not available via get_attr during checkpoint but * it needs to be inserted before restoring the ranges so * allocate extra space for it before calling set_attr
*/
set_attr_size = sizeof(struct kfd_ioctl_svm_attribute) *
(num_attrs + 1);
set_attr_new = krealloc(set_attr, set_attr_size,
GFP_KERNEL); if (!set_attr_new) {
ret = -ENOMEM; gotoexit;
}
set_attr = set_attr_new;
ret = svm_range_set_attr(p, mm, criu_svm_md->data.start_addr,
criu_svm_md->data.size, num_attrs + 1,
set_attr); if (ret) {
pr_err("CRIU: failed to set range attributes\n"); gotoexit;
}
int kfd_criu_restore_svm(struct kfd_process *p,
uint8_t __user *user_priv_ptr,
uint64_t *priv_data_offset,
uint64_t max_priv_data_size)
{
uint64_t svm_priv_data_size, svm_object_md_size, svm_attrs_size; int nattr_common = 4, nattr_accessibility = 1; struct criu_svm_metadata *criu_svm_md = NULL; struct svm_range_list *svms = &p->svms;
uint32_t num_devices; int ret = 0;
num_devices = p->n_pdds; /* Handle one SVM range object at a time, also the number of gpus are * assumed to be same on the restore node, checking must be done while * evaluating the topology earlier
*/
criu_svm_md = kzalloc(svm_object_md_size, GFP_KERNEL); if (!criu_svm_md) {
pr_err("failed to allocate memory to store svm metadata\n"); return -ENOMEM;
} if (*priv_data_offset + svm_priv_data_size > max_priv_data_size) {
ret = -EINVAL; gotoexit;
}
ret = copy_from_user(&criu_svm_md->data, user_priv_ptr + *priv_data_offset,
svm_priv_data_size); if (ret) {
ret = -EFAULT; gotoexit;
}
*priv_data_offset += svm_priv_data_size;
*num_svm_ranges = count; /* Only the accessbility attributes need to be queried for all the gpus * individually, remaining ones are spanned across the entire process * regardless of the various gpu nodes. Of the remaining attributes, * KFD_IOCTL_SVM_ATTR_CLR_FLAGS need not be saved. * * KFD_IOCTL_SVM_ATTR_PREFERRED_LOC * KFD_IOCTL_SVM_ATTR_PREFETCH_LOC * KFD_IOCTL_SVM_ATTR_SET_FLAGS * KFD_IOCTL_SVM_ATTR_GRANULARITY * * ** ACCESSBILITY ATTRIBUTES ** * (Considered as one, type is altered during query, value is gpuid) * KFD_IOCTL_SVM_ATTR_ACCESS * KFD_IOCTL_SVM_ATTR_ACCESS_IN_PLACE * KFD_IOCTL_SVM_ATTR_NO_ACCESS
*/ if (*num_svm_ranges > 0) {
common_attr_size = sizeof(struct kfd_ioctl_svm_attribute) *
nattr_common;
accessibility_size = sizeof(struct kfd_ioctl_svm_attribute) *
nattr_accessibility * num_devices;
ret = svm_range_get_attr(p, mm, svm_priv->start_addr,
svm_priv->size,
(nattr_common + num_devices),
svm_priv->attrs); if (ret) {
pr_err("CRIU: failed to obtain range attributes\n"); goto exit_priv;
}
if (copy_to_user(user_priv_data + *priv_data_offset, svm_priv,
svm_priv_data_size)) {
pr_err("Failed to copy svm priv to user\n");
ret = -EFAULT; goto exit_priv;
}
switch (op) { case KFD_IOCTL_SVM_OP_SET_ATTR:
r = svm_range_set_attr(p, mm, start, size, nattrs, attrs); break; case KFD_IOCTL_SVM_OP_GET_ATTR:
r = svm_range_get_attr(p, mm, start, size, nattrs, attrs); break; default:
r = -EINVAL; break;
}
return r;
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.67 Sekunden
(vorverarbeitet am 2026-04-29)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.