Registering a userptr BO faults in its whole range while holding p->mutex (alloc ioctl) and process_info->lock (init_user_pages). For a 32 GiB hipHostMalloc this takes seconds, and other threads of the process block on both locks in their own alloc and map ioctls, stalling GPU submission on every stream.
Allocate the BO without p->mutex; it has no handle until the ioctl takes the lock again, so no other thread can reach it. In init_user_pages, fault in and validate the pages without process_info->lock, then add the BO to userptr_valid_list under process_info->lock and notifier_lock only if the range was not invalidated meanwhile. Otherwise return -EAGAIN, which user mode retries; an invalidation after that point is handled by the restore worker as before. Signed-off-by: William Palacek <[email protected]> --- .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 45 ++++++++++++------- drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 7 +++ 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c index 565ecd7e012a..01a34ee5f316 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c @@ -1062,12 +1062,12 @@ static void remove_kgd_mem_from_kfd_bo_list(struct kgd_mem *mem, /* Initializes user pages. It registers the MMU notifier and validates * the userptr BO in the GTT domain. * - * The BO must already be on the userptr_valid_list. Otherwise an - * eviction and restore may happen that leaves the new BO unmapped - * with the user mode queues running. - * - * Takes the process_info->lock to protect against concurrent restore - * workers. + * Pages are faulted in and validated without process_info->lock, so a + * large range does not block other threads of the process. The BO is + * added to the userptr_valid_list only if no invalidation happened since + * its pages were read; otherwise an eviction and restore could leave it + * stale with the user mode queues running. A lost race returns -EAGAIN + * and user mode retries the ioctl. * * Returns 0 for success, negative errno for errors. */ @@ -1080,19 +1080,17 @@ static int init_user_pages(struct kgd_mem *mem, uint64_t user_addr, struct amdgpu_hmm_range *range; int ret = 0; - mutex_lock(&process_info->lock); - ret = amdgpu_ttm_tt_set_userptr(&bo->tbo, user_addr, 0); if (ret) { pr_err("%s: Failed to set userptr: %d\n", __func__, ret); - goto out; + return ret; } ret = amdgpu_hmm_register(bo, user_addr); if (ret) { pr_err("%s: Failed to register MMU notifier: %d\n", __func__, ret); - goto out; + return ret; } if (criu_resume) { @@ -1102,10 +1100,10 @@ static int init_user_pages(struct kgd_mem *mem, uint64_t user_addr, * later stage when it is scheduled by another ioctl called by * CRIU master process for the target pid for restore. */ + add_kgd_mem_to_kfd_bo_list(mem, process_info, true); mutex_lock(&process_info->notifier_lock); mem->invalid++; mutex_unlock(&process_info->notifier_lock); - mutex_unlock(&process_info->lock); return 0; } @@ -1117,12 +1115,11 @@ static int init_user_pages(struct kgd_mem *mem, uint64_t user_addr, ret = amdgpu_ttm_tt_get_user_pages(bo, range); if (ret) { - amdgpu_hmm_range_free(range); if (ret == -EAGAIN) pr_debug("Failed to get user pages, try again\n"); else pr_err("%s: Failed to get user pages: %d\n", __func__, ret); - goto unregister_out; + goto release_out; } ret = amdgpu_bo_reserve(bo, true); @@ -1138,14 +1135,27 @@ static int init_user_pages(struct kgd_mem *mem, uint64_t user_addr, if (ret) pr_err("%s: failed to validate BO\n", __func__); amdgpu_bo_unreserve(bo); + if (ret) + goto release_out; + + mutex_lock(&process_info->lock); + mutex_lock(&process_info->notifier_lock); + if (amdgpu_hmm_range_valid(range)) { + /* Earlier invalidations completed before the pages were read */ + mem->invalid = 0; + list_add_tail(&mem->validate_list, + &process_info->userptr_valid_list); + } else { + ret = -EAGAIN; + } + mutex_unlock(&process_info->notifier_lock); + mutex_unlock(&process_info->lock); release_out: amdgpu_hmm_range_free(range); unregister_out: if (ret) amdgpu_hmm_unregister(bo); -out: - mutex_unlock(&process_info->lock); return ret; } @@ -1875,7 +1885,8 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu( (*mem)->mapped_to_gpu_memory = 0; (*mem)->process_info = avm->process_info; - add_kgd_mem_to_kfd_bo_list(*mem, avm->process_info, user_addr); + if (!user_addr) + add_kgd_mem_to_kfd_bo_list(*mem, avm->process_info, false); if (user_addr) { pr_debug("creating userptr BO for user_addr = %llx\n", user_addr); @@ -1907,10 +1918,10 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu( return 0; -allocate_init_user_pages_failed: err_pin_bo: err_validate_bo: remove_kgd_mem_from_kfd_bo_list(*mem, avm->process_info); +allocate_init_user_pages_failed: drm_gem_handle_delete(adev->kfd.client.file, (*mem)->gem_handle); err_gem_handle_create: drm_vma_node_revoke(&gobj->vma_node, drm_priv); diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c index 9886bbd28ea2..acd4dd0f2f06 100644 --- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c +++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c @@ -1228,10 +1228,17 @@ static int kfd_ioctl_alloc_memory_of_gpu(struct file *filep, } } + /* + * Not under p->mutex: a userptr BO faults in its whole range here. + * The BO has no handle yet, so nothing else can reach it, and pdd + * lives until process teardown. + */ + mutex_unlock(&p->mutex); err = amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu( dev->adev, args->va_addr, args->size, pdd->drm_priv, (struct kgd_mem **) &mem, &offset, flags, false); + mutex_lock(&p->mutex); if (err) goto err_unlock; -- 2.43.0
