When entities are torn down in drm_sched_entity_kill() a multi-step
process is undertaken to synchronize with a drm_sched_run_job_work()
consuming jobs from the same entity.
First the entity is marked as stopped and removed from the run-queue,
with both the entity and run-queue lock correctly taken. The process then
waits for the entity idle via the completion after which point it assumes
it is the only accessor to the entity in question.
What can happen however in drm_sched_run_job_work() if there are at least
two outstanding runnable jobs on an entity, is that
drm_sched_entity_pop_job() can re-add it to the run-queue due the
unconditial add to the rbtree via drm_sched_rq_update_fifo_locked().
What is required is that the teardown starts after the run job has
selected the entity and that it gets to it's completion waiting stage
before the run job goes to dequeue a job, and for the run job to be able
to de-queue a job before teardown has consumed too many.
Run job will then see there is another job pending (after the one about
to be de-queued) and it will re-add the entity to the run-queue. Run job
will then signal the completion and entity teardown will proceed
incorrectly thinking it's removal of the entity was final.
End result is a pointer to an entity about to be freed dangling forever in
the rbtree.
drm_sched_run_job_work() drm_sched_entity_kill()
drm_sched_select_entity
stops and removes entity
wait_for_completion
drm_sched_entity_pop_job
-> re-adds to run-queue due more jobs
complete_all
drm_sched_entity_queue_pop
... fini entity ...
drm_sched_run_job_queue
drm_sched_select_entity - UAF
This race existed ever since the FIFO policy was first added.
Lets mirror the guard from drm_sched_rq_add_entity() in
drm_sched_rq_pop_entity() and close this gap.
Signed-off-by: Tvrtko Ursulin <[email protected]>
References:
https://sashiko.dev/#/patchset/[email protected]?part=7
Fixes: 08fb97de03aa ("drm/sched: Add FIFO sched policy to run queue")
Cc: Luben Tuikov <[email protected]>
Cc: Christian König <[email protected]>
Cc: Andrey Grodzovsky <[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: Danilo Krummrich <[email protected]>
Cc: Philipp Stanner <[email protected]>
Cc: David Airlie <[email protected]>
Cc: Simona Vetter <[email protected]>
Cc: <[email protected]> # v6.2+
---
drivers/gpu/drm/scheduler/sched_rq.c | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/drivers/gpu/drm/scheduler/sched_rq.c
b/drivers/gpu/drm/scheduler/sched_rq.c
index 23f46ec610e7..6f67a146d99c 100644
--- a/drivers/gpu/drm/scheduler/sched_rq.c
+++ b/drivers/gpu/drm/scheduler/sched_rq.c
@@ -345,6 +345,13 @@ void drm_sched_rq_pop_entity(struct drm_sched_entity
*entity)
lockdep_assert_held(&entity->lock);
+ /*
+ * If entity is getting torn down via drm_sched_entity_kill() we need
+ * to ensure it does not get re-added to the tree behind it's back.
+ */
+ if (entity->stopped)
+ return;
+
spin_lock(&rq->lock);
/*
--
2.56.0