Skip to content

Commit 3ad18c1

Browse files
committed
drm/scheduler: Clean up jobs when the scheduler is torn down.
drm_sched_fini() currently leaves any pending jobs dangling, which causes segfaults and other badness when job completion fences are signaled after the scheduler is torn down. Explicitly detach all jobs from their completion callbacks and free them. This makes it possible to write a sensible safe abstraction for drm_sched, without having to externally duplicate the tracking of in-flight jobs. This shouldn't regress any existing drivers, since calling drm_sched_fini() with any pending jobs is broken and this change should be a no-op if there are no pending jobs. Signed-off-by: Asahi Lina <[email protected]>
1 parent 2c8cad7 commit 3ad18c1

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

drivers/gpu/drm/scheduler/sched_main.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,8 +1336,33 @@ EXPORT_SYMBOL(drm_sched_init);
13361336
void drm_sched_fini(struct drm_gpu_scheduler *sched)
13371337
{
13381338
struct drm_sched_entity *s_entity;
1339+
struct drm_sched_job *s_job, *tmp;
13391340
int i;
13401341

1342+
/*
1343+
* Stop the scheduler, detaching all jobs from their hardware callbacks
1344+
* and cleaning up complete jobs.
1345+
*/
1346+
drm_sched_stop(sched, NULL);
1347+
1348+
/*
1349+
* Iterate through the pending job list and free all jobs.
1350+
* This assumes the driver has either guaranteed jobs are already stopped, or that
1351+
* otherwise it is responsible for keeping any necessary data structures for
1352+
* in-progress jobs alive even when the free_job() callback is called early (e.g. by
1353+
* putting them in its own queue or doing its own refcounting).
1354+
*/
1355+
list_for_each_entry_safe(s_job, tmp, &sched->pending_list, list) {
1356+
spin_lock(&sched->job_list_lock);
1357+
list_del_init(&s_job->list);
1358+
spin_unlock(&sched->job_list_lock);
1359+
1360+
drm_sched_fence_finished(s_job->s_fence, -ESRCH);
1361+
1362+
WARN_ON(s_job->s_fence->parent);
1363+
sched->ops->free_job(s_job);
1364+
}
1365+
13411366
drm_sched_wqueue_stop(sched);
13421367

13431368
for (i = DRM_SCHED_PRIORITY_KERNEL; i < sched->num_rqs; i++) {

0 commit comments

Comments
 (0)