// SPDX-License-Identifier: GPL-2.0 OR MIT /* * Copyright 2014-2022 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.
*/
/* * List of struct kfd_process (field kfd_process). * Unique/indexed by mm_struct*
*/
DEFINE_HASHTABLE(kfd_processes_table, KFD_PROCESS_TABLE_SIZE);
DEFINE_MUTEX(kfd_processes_mutex);
DEFINE_SRCU(kfd_processes_srcu);
/* For process termination handling */ staticstruct workqueue_struct *kfd_process_wq;
/* Ordered, single-threaded workqueue for restoring evicted * processes. Restoring multiple processes concurrently under memory * pressure can lead to processes blocking each other from validating * their BOs and result in a live-lock situation where processes * remain evicted indefinitely.
*/ staticstruct workqueue_struct *kfd_restore_wq;
pdd = workarea->pdd; if (!pdd) return;
dqm = pdd->dev->dqm;
qpd = &pdd->qpd; if (!dqm || !qpd) return; /* * Total SDMA activity is current SDMA activity + past SDMA activity * Past SDMA count is stored in pdd. * To get the current activity counters for all active SDMA queues, * we loop over all SDMA queues and get their counts from user-space. * * We cannot call get_user() with dqm_lock held as it can cause * a circular lock dependency situation. To read the SDMA stats, * we need to do the following: * * 1. Create a temporary list of SDMA queue nodes from the qpd->queues_list, * with dqm_lock/dqm_unlock(). * 2. Call get_user() for each node in temporary list without dqm_lock. * Save the SDMA count for each node and also add the count to the total * SDMA count counter. * Its possible, during this step, a few SDMA queue nodes got deleted * from the qpd->queues_list. * 3. Do a second pass over qpd->queues_list to check if any nodes got deleted. * If any node got deleted, its SDMA count would be captured in the sdma * past activity counter. So subtract the SDMA counter stored in step 2 * for this node from the total SDMA count.
*/
INIT_LIST_HEAD(&sdma_q_list.list);
/* * Create the temp list of all SDMA queues
*/
dqm_lock(dqm);
/* * If the temp list is empty, then no SDMA queues nodes were found in * qpd->queues_list. Return the past activity count as the total sdma * count
*/ if (list_empty(&sdma_q_list.list)) {
workarea->sdma_activity_counter = pdd->sdma_past_activity_counter;
dqm_unlock(dqm); return;
}
dqm_unlock(dqm);
/* * Get the usage count for each SDMA queue in temp_list.
*/
mm = get_task_mm(pdd->process->lead_thread); if (!mm) goto cleanup;
kthread_use_mm(mm);
list_for_each_entry(sdma_q, &sdma_q_list.list, list) {
val = 0;
ret = read_sdma_queue_counter(sdma_q->rptr, &val); if (ret) {
pr_debug("Failed to read SDMA queue active counter for queue id: %d",
sdma_q->queue_id);
} else {
sdma_q->sdma_val = val;
workarea->sdma_activity_counter += val;
}
}
kthread_unuse_mm(mm);
mmput(mm);
/* * Do a second iteration over qpd_queues_list to check if any SDMA * nodes got deleted while fetching SDMA counter.
*/
dqm_lock(dqm);
/* * If temp list is not empty, it implies some queues got deleted * from qpd->queues_list during SDMA usage read. Subtract the SDMA * count for each node from the total SDMA count.
*/
list_for_each_entry_safe(sdma_q, next, &sdma_q_list.list, list) {
workarea->sdma_activity_counter -= sdma_q->sdma_val;
list_del(&sdma_q->list);
kfree(sdma_q);
}
/** * kfd_get_cu_occupancy - Collect number of waves in-flight on this device * by current process. Translates acquired wave count into number of compute units * that are occupied. * * @attr: Handle of attribute that allows reporting of wave count. The attribute * handle encapsulates GPU device it is associated with, thereby allowing collection * of waves in flight, etc * @buffer: Handle of user provided buffer updated with wave count * * Return: Number of bytes written to user buffer or an error value
*/ staticint kfd_get_cu_occupancy(struct attribute *attr, char *buffer)
{ int cu_cnt; int wave_cnt; int max_waves_per_cu; struct kfd_node *dev = NULL; struct kfd_process *proc = NULL; struct kfd_process_device *pdd = NULL; int i; struct kfd_cu_occupancy *cu_occupancy;
u32 queue_format;
pdd = container_of(attr, struct kfd_process_device, attr_cu_occupancy);
dev = pdd->dev; if (dev->kfd2kgd->get_cu_occupancy == NULL) return -EINVAL;
cu_cnt = 0;
proc = pdd->process; if (pdd->qpd.queue_count == 0) {
pr_debug("Gpu-Id: %d has no active queues for process pid %d\n",
dev->id, (int)proc->lead_thread->pid); return snprintf(buffer, PAGE_SIZE, "%d\n", cu_cnt);
}
/* Collect wave count from device if it supports */
wave_cnt = 0;
max_waves_per_cu = 0;
cu_occupancy = kcalloc(AMDGPU_MAX_QUEUES, sizeof(*cu_occupancy), GFP_KERNEL); if (!cu_occupancy) return -ENOMEM;
/* * For GFX 9.4.3, fetch the CU occupancy from the first XCC in the partition. * For AQL queues, because of cooperative dispatch we multiply the wave count * by number of XCCs in the partition to get the total wave counts across all * XCCs in the partition. * For PM4 queues, there is no cooperative dispatch so wave_cnt stay as it is.
*/
dev->kfd2kgd->get_cu_occupancy(dev->adev, cu_occupancy,
&max_waves_per_cu, ffs(dev->xcc_mask) - 1);
for (i = 0; i < AMDGPU_MAX_QUEUES; i++) { if (cu_occupancy[i].wave_cnt != 0 &&
kfd_dqm_is_queue_in_process(dev->dqm, &pdd->qpd,
cu_occupancy[i].doorbell_off,
&queue_format)) { if (unlikely(queue_format == KFD_QUEUE_FORMAT_PM4))
wave_cnt += cu_occupancy[i].wave_cnt; else
wave_cnt += (NUM_XCC(dev->xcc_mask) *
cu_occupancy[i].wave_cnt);
}
}
/* Translate wave count to number of compute units */
cu_cnt = (wave_cnt + (max_waves_per_cu - 1)) / max_waves_per_cu;
kfree(cu_occupancy); return snprintf(buffer, PAGE_SIZE, "%d\n", cu_cnt);
}
procfs.kobj = kfd_alloc_struct(procfs.kobj); if (!procfs.kobj) return;
ret = kobject_init_and_add(procfs.kobj, &procfs_type,
&kfd_device->kobj, "proc"); if (ret) {
pr_warn("Could not create procfs proc folder"); /* If we fail to create the procfs, clean up */
kfd_procfs_shutdown();
}
}
/* kfd_process_alloc_gpuvm - Allocate GPU VM for the KFD process * This function should be only called right after the process * is created and when kfd_processes_mutex is still being held * to avoid concurrency. Because of that exclusiveness, we do * not need to take p->mutex.
*/ staticint kfd_process_alloc_gpuvm(struct kfd_process_device *pdd,
uint64_t gpu_va, uint32_t size,
uint32_t flags, struct kgd_mem **mem, void **kptr)
{ struct kfd_node *kdev = pdd->dev; int err;
/* kfd_process_device_reserve_ib_mem - Reserve memory inside the * process for IB usage The memory reserved is for KFD to submit * IB to AMDGPU from kernel. If the memory is reserved * successfully, ib_kaddr will have the CPU/kernel * address. Check ib_kaddr before accessing the memory.
*/ staticint kfd_process_device_reserve_ib_mem(struct kfd_process_device *pdd)
{ struct qcm_process_device *qpd = &pdd->qpd;
uint32_t flags = KFD_IOC_ALLOC_MEM_FLAGS_GTT |
KFD_IOC_ALLOC_MEM_FLAGS_NO_SUBSTITUTE |
KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE |
KFD_IOC_ALLOC_MEM_FLAGS_EXECUTABLE; struct kgd_mem *mem; void *kaddr; int ret;
if (qpd->ib_kaddr || !qpd->ib_base) return 0;
/* ib_base is only set for dGPU */
ret = kfd_process_alloc_gpuvm(pdd, qpd->ib_base, PAGE_SIZE, flags,
&mem, &kaddr); if (ret) return ret;
if (!(thread->mm && mmget_not_zero(thread->mm))) return ERR_PTR(-EINVAL);
/* Only the pthreads threading model is supported. */ if (thread->group_leader->mm != thread->mm) {
mmput(thread->mm); return ERR_PTR(-EINVAL);
}
/* If the process just called exec(3), it is possible that the * cleanup of the kfd_process (following the release of the mm * of the old process image) is still in the cleanup work queue. * Make sure to drain any job before trying to recreate any * resource for this process.
*/
flush_workqueue(kfd_process_wq);
/* * take kfd processes mutex before starting of process creation * so there won't be a case where two threads of the same process * create two kfd_process structures
*/
mutex_lock(&kfd_processes_mutex);
if (kfd_is_locked(NULL)) {
pr_debug("KFD is locked! Cannot create process");
process = ERR_PTR(-EINVAL); goto out;
}
/* A prior open of /dev/kfd could have already created the process. * find_process will increase process kref in this case
*/
process = find_process(thread, true); if (process) {
pr_debug("Process already found\n");
} else {
process = create_process(thread); if (IS_ERR(process)) goto out;
/* * Just kunmap and unpin signal BO here. It will be freed in * kfd_process_free_outstanding_kfd_bos()
*/ staticvoid kfd_process_kunmap_signal_bo(struct kfd_process *p)
{ struct kfd_process_device *pdd; struct kfd_node *kdev; void *mem;
kdev = kfd_device_by_id(GET_GPU_ID(p->signal_handle)); if (!kdev) return;
mutex_lock(&p->mutex);
pdd = kfd_get_process_device_data(kdev, p); if (!pdd) goto out;
mem = kfd_process_device_translate_handle(
pdd, GET_IDR_HANDLE(p->signal_handle)); if (!mem) goto out;
staticvoid kfd_process_free_outstanding_kfd_bos(struct kfd_process *p)
{ int i;
for (i = 0; i < p->n_pdds; i++)
kfd_process_device_free_bos(p->pdds[i]);
}
staticvoid kfd_process_destroy_pdds(struct kfd_process *p)
{ int i;
for (i = 0; i < p->n_pdds; i++) { struct kfd_process_device *pdd = p->pdds[i];
kfd_smi_event_process(pdd, false);
pr_debug("Releasing pdd (topology id %d, for pid %d)\n",
pdd->dev->id, p->lead_thread->pid);
kfd_process_device_destroy_cwsr_dgpu(pdd);
kfd_process_device_destroy_ib_mem(pdd);
if (pdd->drm_file)
fput(pdd->drm_file);
if (pdd->qpd.cwsr_kaddr && !pdd->qpd.cwsr_base)
free_pages((unsignedlong)pdd->qpd.cwsr_kaddr,
get_order(KFD_CWSR_TBA_TMA_SIZE));
idr_destroy(&pdd->alloc_idr);
kfd_free_process_doorbells(pdd->dev->kfd, pdd);
if (pdd->dev->kfd->shared_resources.enable_mes &&
pdd->proc_ctx_cpu_ptr)
amdgpu_amdkfd_free_gtt_mem(pdd->dev->adev,
&pdd->proc_ctx_bo); /* * before destroying pdd, make sure to report availability * for auto suspend
*/ if (pdd->runtime_inuse) {
pm_runtime_mark_last_busy(adev_to_drm(pdd->dev->adev)->dev);
pm_runtime_put_autosuspend(adev_to_drm(pdd->dev->adev)->dev);
pdd->runtime_inuse = false;
}
atomic_dec(&pdd->dev->kfd->kfd_processes_count);
kfree(pdd);
p->pdds[i] = NULL;
}
p->n_pdds = 0;
}
staticvoid kfd_process_remove_sysfs(struct kfd_process *p)
{ struct kfd_process_device *pdd; int i;
/* * If any GPU is ongoing reset, wait for reset complete.
*/ staticvoid kfd_process_wait_gpu_reset_complete(struct kfd_process *p)
{ int i;
for (i = 0; i < p->n_pdds; i++)
flush_workqueue(p->pdds[i]->dev->adev->reset_domain->wq);
}
/* No process locking is needed in this function, because the process * is not findable any more. We must assume that no other thread is * using it any more, otherwise we couldn't safely free the process * structure in the end.
*/ staticvoid kfd_process_wq_release(struct work_struct *work)
{ struct kfd_process *p = container_of(work, struct kfd_process,
release_work); struct dma_fence *ef;
/* * If GPU in reset, user queues may still running, wait for reset complete.
*/
kfd_process_wait_gpu_reset_complete(p);
/* Signal the eviction fence after user mode queues are * destroyed. This allows any BOs to be freed without * triggering pointless evictions or waiting for fences.
*/
synchronize_rcu();
ef = rcu_access_pointer(p->ef); if (ef)
dma_fence_signal(ef);
for (i = 0; i < p->n_pdds; i++) { struct kfd_process_device *pdd = p->pdds[i];
/* re-enable GFX OFF since runtime enable with ttmp setup disabled it. */ if (!kfd_dbg_is_rlc_restore_supported(pdd->dev) && p->runtime_info.ttmp_setup)
amdgpu_gfx_off_ctrl(pdd->dev->adev, true);
}
/* Indicate to other users that MM is no longer valid */
p->mm = NULL;
kfd_dbg_trap_disable(p);
if (atomic_read(&p->debugged_process_count) > 0) { struct kfd_process *target; unsignedint temp; int idx = srcu_read_lock(&kfd_processes_srcu);
/* * The kfd_process structure can not be free because the * mmu_notifier srcu is read locked
*/
p = container_of(mn, struct kfd_process, mmu_notifier); if (WARN_ON(p->mm != mm)) return;
mutex_lock(&kfd_processes_mutex); /* * Do early return if table is empty. * * This could potentially happen if this function is called concurrently * by mmu_notifier and by kfd_cleanup_pocesses. *
*/ if (hash_empty(kfd_processes_table)) {
mutex_unlock(&kfd_processes_mutex); return;
}
hash_del_rcu(&p->kfd_processes);
mutex_unlock(&kfd_processes_mutex);
synchronize_srcu(&kfd_processes_srcu);
/* * This code handles the case when driver is being unloaded before all * mm_struct are released. We need to safely free the kfd_process and * avoid race conditions with mmu_notifier that might try to free them. *
*/ void kfd_cleanup_processes(void)
{ struct kfd_process *p; struct hlist_node *p_temp; unsignedint temp;
HLIST_HEAD(cleanup_list);
/* * Move all remaining kfd_process from the process table to a * temp list for processing. Once done, callback from mmu_notifier * release will not see the kfd_process in the table and do early return, * avoiding double free issues.
*/
mutex_lock(&kfd_processes_mutex);
hash_for_each_safe(kfd_processes_table, temp, p_temp, p, kfd_processes) {
hash_del_rcu(&p->kfd_processes);
synchronize_srcu(&kfd_processes_srcu);
hlist_add_head(&p->kfd_processes, &cleanup_list);
}
mutex_unlock(&kfd_processes_mutex);
if (!dev->kfd->cwsr_enabled || qpd->cwsr_kaddr || !qpd->cwsr_base) return 0;
/* cwsr_base is only set for dGPU */
ret = kfd_process_alloc_gpuvm(pdd, qpd->cwsr_base,
KFD_CWSR_TBA_TMA_SIZE, flags, &mem, &kaddr); if (ret) return ret;
void kfd_process_set_trap_handler(struct qcm_process_device *qpd,
uint64_t tba_addr,
uint64_t tma_addr)
{ if (qpd->cwsr_kaddr) { /* KFD trap handler is bound, record as second-level TBA/TMA * in first-level TMA. First-level trap will jump to second.
*/
uint64_t *tma =
(uint64_t *)(qpd->cwsr_kaddr + KFD_CWSR_TMA_OFFSET);
tma[0] = tba_addr;
tma[1] = tma_addr;
} else { /* No trap handler bound, bind as first-level TBA/TMA. */
qpd->tba_addr = tba_addr;
qpd->tma_addr = tma_addr;
}
}
bool kfd_process_xnack_mode(struct kfd_process *p, bool supported)
{ int i;
/* On most GFXv9 GPUs, the retry mode in the SQ must match the * boot time retry setting. Mixing processes with different * XNACK/retry settings can hang the GPU. * * Different GPUs can have different noretry settings depending * on HW bugs or limitations. We need to find at least one * XNACK mode for this process that's compatible with all GPUs. * Fortunately GPUs with retry enabled (noretry=0) can run code * built for XNACK-off. On GFXv9 it may perform slower. * * Therefore applications built for XNACK-off can always be * supported and will be our fallback if any GPU does not * support retry.
*/ for (i = 0; i < p->n_pdds; i++) { struct kfd_node *dev = p->pdds[i]->dev;
/* Only consider GFXv9 and higher GPUs. Older GPUs don't * support the SVM APIs and don't need to be considered * for the XNACK mode selection.
*/ if (!KFD_IS_SOC15(dev)) continue; /* Aldebaran can always support XNACK because it can support * per-process XNACK mode selection. But let the dev->noretry * setting still influence the default XNACK mode.
*/ if (supported && KFD_SUPPORT_XNACK_PER_PROCESS(dev)) { if (!amdgpu_sriov_xnack_support(dev->kfd->adev)) {
pr_debug("SRIOV platform xnack not supported\n"); returnfalse;
} continue;
}
/* GFXv10 and later GPUs do not support shader preemption * during page faults. This can lead to poor QoS for queue * management and memory-manager-related preemptions or * even deadlocks.
*/ if (KFD_GC_VERSION(dev) >= IP_VERSION(10, 1, 1)) returnfalse;
/* * On return the kfd_process is fully operational and will be freed when the * mm is released
*/ staticstruct kfd_process *create_process(conststruct task_struct *thread)
{ struct kfd_process *process; struct mmu_notifier *mn; int err = -ENOMEM;
process = kzalloc(sizeof(*process), GFP_KERNEL); if (!process) goto err_alloc_process;
err = pqm_init(&process->pqm, process); if (err != 0) goto err_process_pqm_init;
/* init process apertures*/
err = kfd_init_apertures(process); if (err != 0) goto err_init_apertures;
/* Check XNACK support after PDDs are created in kfd_init_apertures */
process->xnack_enabled = kfd_process_xnack_mode(process, false);
err = svm_range_list_init(process); if (err) goto err_init_svm_range_list;
/* alloc_notifier needs to find the process in the hash table */
hash_add_rcu(kfd_processes_table, &process->kfd_processes,
(uintptr_t)process->mm);
/* Avoid free_notifier to start kfd_process_wq_release if * mmu_notifier_get failed because of pending signal.
*/
kref_get(&process->ref);
/* MMU notifier registration must be the last call that can fail * because after this point we cannot unwind the process creation. * After this point, mmu_notifier_put will trigger the cleanup by * dropping the last process reference in the free_notifier.
*/
mn = mmu_notifier_get(&kfd_process_mmu_notifier_ops, process->mm); if (IS_ERR(mn)) {
err = PTR_ERR(mn); goto err_register_notifier;
}
BUG_ON(mn != &process->mmu_notifier);
/* Init idr used for memory handle translation */
idr_init(&pdd->alloc_idr);
atomic_inc(&dev->kfd->kfd_processes_count);
return pdd;
}
/** * kfd_process_device_init_vm - Initialize a VM for a process-device * * @pdd: The process-device * @drm_file: Optional pointer to a DRM file descriptor * * If @drm_file is specified, it will be used to acquire the VM from * that file descriptor. If successful, the @pdd takes ownership of * the file descriptor. * * If @drm_file is NULL, a new VM is created. * * Returns 0 on success, -errno on failure.
*/ int kfd_process_device_init_vm(struct kfd_process_device *pdd, struct file *drm_file)
{ struct amdgpu_fpriv *drv_priv; struct amdgpu_vm *avm; struct kfd_process *p; struct dma_fence *ef; struct kfd_node *dev; int ret;
if (!drm_file) return -EINVAL;
if (pdd->drm_priv) return -EBUSY;
ret = amdgpu_file_to_fpriv(drm_file, &drv_priv); if (ret) return ret;
avm = &drv_priv->vm;
p = pdd->process;
dev = pdd->dev;
ret = amdgpu_amdkfd_gpuvm_acquire_process_vm(dev->adev, avm,
&p->kgd_process_info,
p->ef ? NULL : &ef); if (ret) {
dev_err(dev->adev->dev, "Failed to create process VM object\n"); return ret;
}
if (!p->ef)
RCU_INIT_POINTER(p->ef, ef);
pdd->drm_priv = drm_file->private_data;
ret = kfd_process_device_reserve_ib_mem(pdd); if (ret) goto err_reserve_ib_mem;
ret = kfd_process_device_init_cwsr_dgpu(pdd); if (ret) goto err_init_cwsr;
if (unlikely(!avm->pasid)) {
dev_warn(pdd->dev->adev->dev, "WARN: vm %p has no pasid associated",
avm);
ret = -EINVAL; goto err_get_pasid;
}
/* * Direct the IOMMU to bind the process (specifically the pasid->mm) * to the device. * Unbinding occurs when the process dies or the device is removed. * * Assumes that the process lock is held.
*/ struct kfd_process_device *kfd_bind_process_to_device(struct kfd_node *dev, struct kfd_process *p)
{ struct kfd_process_device *pdd; int err;
pdd = kfd_get_process_device_data(dev, p); if (!pdd) {
dev_err(dev->adev->dev, "Process device data doesn't exist\n"); return ERR_PTR(-ENOMEM);
}
if (!pdd->drm_priv) return ERR_PTR(-ENODEV);
/* * signal runtime-pm system to auto resume and prevent * further runtime suspend once device pdd is created until * pdd is destroyed.
*/ if (!pdd->runtime_inuse) {
err = pm_runtime_get_sync(adev_to_drm(dev->adev)->dev); if (err < 0) {
pm_runtime_put_autosuspend(adev_to_drm(dev->adev)->dev); return ERR_PTR(err);
}
}
/* * make sure that runtime_usage counter is incremented just once * per pdd
*/
pdd->runtime_inuse = true;
return pdd;
}
/* Create specific handle mapped to mem from process local memory idr * Assumes that the process lock is held.
*/ int kfd_process_device_create_obj_handle(struct kfd_process_device *pdd, void *mem)
{ return idr_alloc(&pdd->alloc_idr, mem, 0, 0, GFP_KERNEL);
}
/* Translate specific handle from process local memory idr * Assumes that the process lock is held.
*/ void *kfd_process_device_translate_handle(struct kfd_process_device *pdd, int handle)
{ if (handle < 0) return NULL;
return idr_find(&pdd->alloc_idr, handle);
}
/* Remove specific handle from process local memory idr * Assumes that the process lock is held.
*/ void kfd_process_device_remove_obj_handle(struct kfd_process_device *pdd, int handle)
{ if (handle >= 0)
idr_remove(&pdd->alloc_idr, handle);
}
/* This increments the process->ref counter. */ struct kfd_process *kfd_lookup_process_by_mm(conststruct mm_struct *mm)
{ struct kfd_process *p;
int idx = srcu_read_lock(&kfd_processes_srcu);
p = find_process_by_mm(mm); if (p)
kref_get(&p->ref);
srcu_read_unlock(&kfd_processes_srcu, idx);
return p;
}
/* kfd_process_evict_queues - Evict all user queues of a process * * Eviction is reference-counted per process-device. This means multiple * evictions from different sources can be nested safely.
*/ int kfd_process_evict_queues(struct kfd_process *p, uint32_t trigger)
{ int r = 0; int i; unsignedint n_evicted = 0;
for (i = 0; i < p->n_pdds; i++) { struct kfd_process_device *pdd = p->pdds[i]; struct device *dev = pdd->dev->adev->dev;
r = pdd->dev->dqm->ops.evict_process_queues(pdd->dev->dqm,
&pdd->qpd); /* evict return -EIO if HWS is hang or asic is resetting, in this case * we would like to set all the queues to be in evicted state to prevent * them been add back since they actually not be saved right now.
*/ if (r && r != -EIO) {
dev_err(dev, "Failed to evict process queues\n"); goto fail;
}
n_evicted++;
pdd->dev->dqm->is_hws_hang = false;
}
return r;
fail: /* To keep state consistent, roll back partial eviction by * restoring queues
*/ for (i = 0; i < p->n_pdds; i++) { struct kfd_process_device *pdd = p->pdds[i];
r = pdd->dev->dqm->ops.restore_process_queues(pdd->dev->dqm,
&pdd->qpd); if (r) {
dev_err(dev, "Failed to restore process queues\n"); if (!ret)
ret = r;
}
}
return ret;
}
int kfd_process_gpuidx_from_gpuid(struct kfd_process *p, uint32_t gpu_id)
{ int i;
for (i = 0; i < p->n_pdds; i++) if (p->pdds[i] && gpu_id == p->pdds[i]->user_gpu_id) return i; return -EINVAL;
}
int
kfd_process_gpuid_from_node(struct kfd_process *p, struct kfd_node *node,
uint32_t *gpuid, uint32_t *gpuidx)
{ int i;
for (i = 0; i < p->n_pdds; i++) if (p->pdds[i] && p->pdds[i]->dev == node) {
*gpuid = p->pdds[i]->user_gpu_id;
*gpuidx = i; return 0;
} return -EINVAL;
}
staticint signal_eviction_fence(struct kfd_process *p)
{ struct dma_fence *ef; int ret;
rcu_read_lock();
ef = dma_fence_get_rcu_safe(&p->ef);
rcu_read_unlock(); if (!ef) return -EINVAL;
/* Process termination destroys this worker thread. So during the * lifetime of this thread, kfd_process p will be valid
*/
p = container_of(dwork, struct kfd_process, eviction_work);
pr_debug("Started evicting process pid %d\n", p->lead_thread->pid);
ret = kfd_process_evict_queues(p, KFD_QUEUE_EVICTION_TRIGGER_TTM); if (!ret) { /* If another thread already signaled the eviction fence, * they are responsible stopping the queues and scheduling * the restore work.
*/ if (signal_eviction_fence(p) ||
mod_delayed_work(kfd_restore_wq, &p->restore_work,
msecs_to_jiffies(PROCESS_RESTORE_TIME_MS)))
kfd_process_restore_queues(p);
pr_debug("Finished evicting process pid %d\n", p->lead_thread->pid);
} else
pr_err("Failed to evict queues of process pid %d\n", p->lead_thread->pid);
}
staticint restore_process_helper(struct kfd_process *p)
{ int ret = 0;
/* VMs may not have been acquired yet during debugging. */ if (p->kgd_process_info) {
ret = amdgpu_amdkfd_gpuvm_restore_process_bos(
p->kgd_process_info, &p->ef); if (ret) return ret;
}
ret = kfd_process_restore_queues(p); if (!ret)
pr_debug("Finished restoring process pid %d\n",
p->lead_thread->pid); else
pr_err("Failed to restore queues of process pid %d\n",
p->lead_thread->pid);
return ret;
}
staticvoid restore_process_worker(struct work_struct *work)
{ struct delayed_work *dwork; struct kfd_process *p; int ret = 0;
dwork = to_delayed_work(work);
/* Process termination destroys this worker thread. So during the * lifetime of this thread, kfd_process p will be valid
*/
p = container_of(dwork, struct kfd_process, restore_work);
pr_debug("Started restoring process pasid %d\n", (int)p->lead_thread->pid);
/* Setting last_restore_timestamp before successful restoration. * Otherwise this would have to be set by KGD (restore_process_bos) * before KFD BOs are unreserved. If not, the process can be evicted * again before the timestamp is set. * If restore fails, the timestamp will be set again in the next * attempt. This would mean that the minimum GPU quanta would be * PROCESS_ACTIVE_TIME_MS - (time to execute the following two * functions)
*/
p->last_restore_timestamp = get_jiffies_64();
ret = restore_process_helper(p); if (ret) {
pr_debug("Failed to restore BOs of process pid %d, retry after %d ms\n",
p->lead_thread->pid, PROCESS_BACK_OFF_TIME_MS); if (mod_delayed_work(kfd_restore_wq, &p->restore_work,
msecs_to_jiffies(PROCESS_RESTORE_TIME_MS)))
kfd_process_restore_queues(p);
}
}
/* * For GFX 9.4.3/9.5.0, send the NodeId also in IH cookie DW[3]
*/ if (KFD_GC_VERSION(pdd->dev->kfd) == IP_VERSION(9, 4, 3) ||
KFD_GC_VERSION(pdd->dev->kfd) == IP_VERSION(9, 4, 4) ||
KFD_GC_VERSION(pdd->dev->kfd) == IP_VERSION(9, 5, 0)) {
node_id = ffs(pdd->dev->interrupt_bitmap) - 1;
irq_drain_fence[3] |= node_id << 16;
}
/* ensure stale irqs scheduled KFD interrupts and send drain fence. */ if (amdgpu_amdkfd_send_close_event_drain_irq(pdd->dev->adev,
irq_drain_fence)) {
pdd->process->irq_drain_is_open = false; return 0;
}
r = wait_event_interruptible(pdd->process->wait_irq_drain,
!READ_ONCE(pdd->process->irq_drain_is_open)); if (r)
pdd->process->irq_drain_is_open = false;
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.