Skip to content

Commit 60e3f5a

Browse files
nhaehnlegregkh
authored andcommitted
drm/ttm: fix use-after-free races in vm fault handling
commit 3089c1df10e2931b1d72d2ffa7d86431084c86b3 upstream. The vm fault handler relies on the fact that the VMA owns a reference to the BO. However, once mmap_sem is released, other tasks are free to destroy the VMA, which can lead to the BO being freed. Fix two code paths where that can happen, both related to vm fault retries. Found via a lock debugging warning which flagged &bo->wu_mutex as locked while being destroyed. Fixes: cbe12e7 ("drm/ttm: Allow vm fault retries") Signed-off-by: Nicolai Hähnle <nicolai.haehnle@amd.com> Reviewed-by: Christian König <christian.koenig@amd.com> Signed-off-by: Alex Deucher <alexander.deucher@amd.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 4edbdf5 commit 60e3f5a

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

drivers/gpu/drm/ttm/ttm_bo_vm.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,11 @@ static int ttm_bo_vm_fault_idle(struct ttm_buffer_object *bo,
6666
if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
6767
goto out_unlock;
6868

69+
ttm_bo_reference(bo);
6970
up_read(&vma->vm_mm->mmap_sem);
7071
(void) ttm_bo_wait(bo, false, true, false);
72+
ttm_bo_unreserve(bo);
73+
ttm_bo_unref(&bo);
7174
goto out_unlock;
7275
}
7376

@@ -114,8 +117,10 @@ static int ttm_bo_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
114117

115118
if (vmf->flags & FAULT_FLAG_ALLOW_RETRY) {
116119
if (!(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
120+
ttm_bo_reference(bo);
117121
up_read(&vma->vm_mm->mmap_sem);
118122
(void) ttm_bo_wait_unreserved(bo);
123+
ttm_bo_unref(&bo);
119124
}
120125

121126
return VM_FAULT_RETRY;
@@ -160,6 +165,13 @@ static int ttm_bo_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
160165
ret = ttm_bo_vm_fault_idle(bo, vma, vmf);
161166
if (unlikely(ret != 0)) {
162167
retval = ret;
168+
169+
if (retval == VM_FAULT_RETRY &&
170+
!(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
171+
/* The BO has already been unreserved. */
172+
return retval;
173+
}
174+
163175
goto out_unlock;
164176
}
165177

0 commit comments

Comments
 (0)