Skip to content

Commit ec3aae1

Browse files
fxkamdNeroReflex
authored andcommitted
drm/amdkfd: Fix lock dependency warning
[ Upstream commit 47bf0f8 ] ====================================================== WARNING: possible circular locking dependency detected 6.5.0-kfd-fkuehlin torvalds#276 Not tainted ------------------------------------------------------ kworker/8:2/2676 is trying to acquire lock: ffff9435aae95c88 ((work_completion)(&svm_bo->eviction_work)){+.+.}-{0:0}, at: __flush_work+0x52/0x550 but task is already holding lock: ffff9435cd8e1720 (&svms->lock){+.+.}-{3:3}, at: svm_range_deferred_list_work+0xe8/0x340 [amdgpu] which lock already depends on the new lock. the existing dependency chain (in reverse order) is: -> ChimeraOS#2 (&svms->lock){+.+.}-{3:3}: __mutex_lock+0x97/0xd30 kfd_ioctl_alloc_memory_of_gpu+0x6d/0x3c0 [amdgpu] kfd_ioctl+0x1b2/0x5d0 [amdgpu] __x64_sys_ioctl+0x86/0xc0 do_syscall_64+0x39/0x80 entry_SYSCALL_64_after_hwframe+0x63/0xcd -> ChimeraOS#1 (&mm->mmap_lock){++++}-{3:3}: down_read+0x42/0x160 svm_range_evict_svm_bo_worker+0x8b/0x340 [amdgpu] process_one_work+0x27a/0x540 worker_thread+0x53/0x3e0 kthread+0xeb/0x120 ret_from_fork+0x31/0x50 ret_from_fork_asm+0x11/0x20 -> #0 ((work_completion)(&svm_bo->eviction_work)){+.+.}-{0:0}: __lock_acquire+0x1426/0x2200 lock_acquire+0xc1/0x2b0 __flush_work+0x80/0x550 __cancel_work_timer+0x109/0x190 svm_range_bo_release+0xdc/0x1c0 [amdgpu] svm_range_free+0x175/0x180 [amdgpu] svm_range_deferred_list_work+0x15d/0x340 [amdgpu] process_one_work+0x27a/0x540 worker_thread+0x53/0x3e0 kthread+0xeb/0x120 ret_from_fork+0x31/0x50 ret_from_fork_asm+0x11/0x20 other info that might help us debug this: Chain exists of: (work_completion)(&svm_bo->eviction_work) --> &mm->mmap_lock --> &svms->lock Possible unsafe locking scenario: CPU0 CPU1 ---- ---- lock(&svms->lock); lock(&mm->mmap_lock); lock(&svms->lock); lock((work_completion)(&svm_bo->eviction_work)); I believe this cannot really lead to a deadlock in practice, because svm_range_evict_svm_bo_worker only takes the mmap_read_lock if the BO refcount is non-0. That means it's impossible that svm_range_bo_release is running concurrently. However, there is no good way to annotate this. To avoid the problem, take a BO reference in svm_range_schedule_evict_svm_bo instead of in the worker. That way it's impossible for a BO to get freed while eviction work is pending and the cancel_work_sync call in svm_range_bo_release can be eliminated. v2: Use svm_bo_ref_unless_zero and explained why that's safe. Also removed redundant checks that are already done in amdkfd_fence_enable_signaling. Signed-off-by: Felix Kuehling <felix.kuehling@amd.com> Reviewed-by: Philip Yang <philip.yang@amd.com> Signed-off-by: Alex Deucher <alexander.deucher@amd.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent fc92aba commit ec3aae1

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

drivers/gpu/drm/amd/amdkfd/kfd_svm.c

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -379,14 +379,9 @@ static void svm_range_bo_release(struct kref *kref)
379379
spin_lock(&svm_bo->list_lock);
380380
}
381381
spin_unlock(&svm_bo->list_lock);
382-
if (!dma_fence_is_signaled(&svm_bo->eviction_fence->base)) {
383-
/* We're not in the eviction worker.
384-
* Signal the fence and synchronize with any
385-
* pending eviction work.
386-
*/
382+
if (!dma_fence_is_signaled(&svm_bo->eviction_fence->base))
383+
/* We're not in the eviction worker. Signal the fence. */
387384
dma_fence_signal(&svm_bo->eviction_fence->base);
388-
cancel_work_sync(&svm_bo->eviction_work);
389-
}
390385
dma_fence_put(&svm_bo->eviction_fence->base);
391386
amdgpu_bo_unref(&svm_bo->bo);
392387
kfree(svm_bo);
@@ -3384,13 +3379,14 @@ svm_range_trigger_migration(struct mm_struct *mm, struct svm_range *prange,
33843379

33853380
int svm_range_schedule_evict_svm_bo(struct amdgpu_amdkfd_fence *fence)
33863381
{
3387-
if (!fence)
3388-
return -EINVAL;
3389-
3390-
if (dma_fence_is_signaled(&fence->base))
3391-
return 0;
3392-
3393-
if (fence->svm_bo) {
3382+
/* Dereferencing fence->svm_bo is safe here because the fence hasn't
3383+
* signaled yet and we're under the protection of the fence->lock.
3384+
* After the fence is signaled in svm_range_bo_release, we cannot get
3385+
* here any more.
3386+
*
3387+
* Reference is dropped in svm_range_evict_svm_bo_worker.
3388+
*/
3389+
if (svm_bo_ref_unless_zero(fence->svm_bo)) {
33943390
WRITE_ONCE(fence->svm_bo->evicting, 1);
33953391
schedule_work(&fence->svm_bo->eviction_work);
33963392
}
@@ -3405,8 +3401,6 @@ static void svm_range_evict_svm_bo_worker(struct work_struct *work)
34053401
int r = 0;
34063402

34073403
svm_bo = container_of(work, struct svm_range_bo, eviction_work);
3408-
if (!svm_bo_ref_unless_zero(svm_bo))
3409-
return; /* svm_bo was freed while eviction was pending */
34103404

34113405
if (mmget_not_zero(svm_bo->eviction_fence->mm)) {
34123406
mm = svm_bo->eviction_fence->mm;

0 commit comments

Comments
 (0)