Skip to content

Commit e5e3eb2

Browse files
Tvrtko Ursulingregkh
authored andcommitted
drm/sched: Fix potential double free in drm_sched_job_add_resv_dependencies
commit 5801e65 upstream. When adding dependencies with drm_sched_job_add_dependency(), that function consumes the fence reference both on success and failure, so in the latter case the dma_fence_put() on the error path (xarray failed to expand) is a double free. Interestingly this bug appears to have been present ever since commit ebd5f74 ("drm/sched: Add dependency tracking"), since the code back then looked like this: drm_sched_job_add_implicit_dependencies(): ... for (i = 0; i < fence_count; i++) { ret = drm_sched_job_add_dependency(job, fences[i]); if (ret) break; } for (; i < fence_count; i++) dma_fence_put(fences[i]); Which means for the failing 'i' the dma_fence_put was already a double free. Possibly there were no users at that time, or the test cases were insufficient to hit it. The bug was then only noticed and fixed after commit 9c2ba26 ("drm/scheduler: use new iterator in drm_sched_job_add_implicit_dependencies v2") landed, with its fixup of commit 4eaf02d ("drm/scheduler: fix drm_sched_job_add_implicit_dependencies"). At that point it was a slightly different flavour of a double free, which commit 963d0b3 ("drm/scheduler: fix drm_sched_job_add_implicit_dependencies harder") noticed and attempted to fix. But it only moved the double free from happening inside the drm_sched_job_add_dependency(), when releasing the reference not yet obtained, to the caller, when releasing the reference already released by the former in the failure case. As such it is not easy to identify the right target for the fixes tag so lets keep it simple and just continue the chain. While fixing we also improve the comment and explain the reason for taking the reference and not dropping it. Signed-off-by: Tvrtko Ursulin <[email protected]> Fixes: 963d0b3 ("drm/scheduler: fix drm_sched_job_add_implicit_dependencies harder") Reported-by: Dan Carpenter <[email protected]> Closes: https://lore.kernel.org/dri-devel/[email protected]/ Cc: Christian König <[email protected]> Cc: Rob Clark <[email protected]> Cc: Daniel Vetter <[email protected]> Cc: Matthew Brost <[email protected]> Cc: Danilo Krummrich <[email protected]> Cc: Philipp Stanner <[email protected]> Cc: Christian König <[email protected]> Cc: [email protected] Cc: [email protected] # v5.16+ Signed-off-by: Philipp Stanner <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 8bc4a8d commit e5e3eb2

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

drivers/gpu/drm/scheduler/sched_main.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -952,13 +952,14 @@ int drm_sched_job_add_resv_dependencies(struct drm_sched_job *job,
952952
dma_resv_assert_held(resv);
953953

954954
dma_resv_for_each_fence(&cursor, resv, usage, fence) {
955-
/* Make sure to grab an additional ref on the added fence */
956-
dma_fence_get(fence);
957-
ret = drm_sched_job_add_dependency(job, fence);
958-
if (ret) {
959-
dma_fence_put(fence);
955+
/*
956+
* As drm_sched_job_add_dependency always consumes the fence
957+
* reference (even when it fails), and dma_resv_for_each_fence
958+
* is not obtaining one, we need to grab one before calling.
959+
*/
960+
ret = drm_sched_job_add_dependency(job, dma_fence_get(fence));
961+
if (ret)
960962
return ret;
961-
}
962963
}
963964
return 0;
964965
}

0 commit comments

Comments
 (0)