Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

drm/sched: Fix deadlock in drm_sched_entity_kill_jobs_cb

The Mesa issue referenced below pointed out a possible deadlock:

[ 1231.611031] Possible interrupt unsafe locking scenario:

[ 1231.611033] CPU0 CPU1
[ 1231.611034] ---- ----
[ 1231.611035] lock(&xa->xa_lock#17);
[ 1231.611038] local_irq_disable();
[ 1231.611039] lock(&fence->lock);
[ 1231.611041] lock(&xa->xa_lock#17);
[ 1231.611044] <Interrupt>
[ 1231.611045] lock(&fence->lock);
[ 1231.611047]
*** DEADLOCK ***

In this example, CPU0 would be any function accessing job->dependencies
through the xa_* functions that don't disable interrupts (eg:
drm_sched_job_add_dependency(), drm_sched_entity_kill_jobs_cb()).

CPU1 is executing drm_sched_entity_kill_jobs_cb() as a fence signalling
callback so in an interrupt context. It will deadlock when trying to
grab the xa_lock which is already held by CPU0.

Replacing all xa_* usage by their xa_*_irq counterparts would fix
this issue, but Christian pointed out another issue: dma_fence_signal
takes fence.lock and so does dma_fence_add_callback.

dma_fence_signal() // locks f1.lock
-> drm_sched_entity_kill_jobs_cb()
-> foreach dependencies
-> dma_fence_add_callback() // locks f2.lock

This will deadlock if f1 and f2 share the same spinlock.

To fix both issues, the code iterating on dependencies and re-arming them
is moved out to drm_sched_entity_kill_jobs_work().

Cc: stable@vger.kernel.org # v6.2+
Fixes: 2fdb8a8f07c2 ("drm/scheduler: rework entity flush, kill and fini")
Link: https://gitlab.freedesktop.org/mesa/mesa/-/issues/13908
Reported-by: Mikhail Gavrilov <mikhail.v.gavrilov@gmail.com>
Suggested-by: Christian König <christian.koenig@amd.com>
Reviewed-by: Christian König <christian.koenig@amd.com>
Signed-off-by: Pierre-Eric Pelloux-Prayer <pierre-eric.pelloux-prayer@amd.com>
[phasta: commit message nits]
Signed-off-by: Philipp Stanner <phasta@kernel.org>
Link: https://patch.msgid.link/20251104095358.15092-1-pierre-eric.pelloux-prayer@amd.com

authored by

Pierre-Eric Pelloux-Prayer and committed by
Philipp Stanner
487df8b6 b4cd8f94

+19 -15
+19 -15
drivers/gpu/drm/scheduler/sched_entity.c
··· 173 173 } 174 174 EXPORT_SYMBOL(drm_sched_entity_error); 175 175 176 + static void drm_sched_entity_kill_jobs_cb(struct dma_fence *f, 177 + struct dma_fence_cb *cb); 178 + 176 179 static void drm_sched_entity_kill_jobs_work(struct work_struct *wrk) 177 180 { 178 181 struct drm_sched_job *job = container_of(wrk, typeof(*job), work); 179 - 180 - drm_sched_fence_scheduled(job->s_fence, NULL); 181 - drm_sched_fence_finished(job->s_fence, -ESRCH); 182 - WARN_ON(job->s_fence->parent); 183 - job->sched->ops->free_job(job); 184 - } 185 - 186 - /* Signal the scheduler finished fence when the entity in question is killed. */ 187 - static void drm_sched_entity_kill_jobs_cb(struct dma_fence *f, 188 - struct dma_fence_cb *cb) 189 - { 190 - struct drm_sched_job *job = container_of(cb, struct drm_sched_job, 191 - finish_cb); 182 + struct dma_fence *f; 192 183 unsigned long index; 193 - 194 - dma_fence_put(f); 195 184 196 185 /* Wait for all dependencies to avoid data corruptions */ 197 186 xa_for_each(&job->dependencies, index, f) { ··· 208 219 209 220 dma_fence_put(f); 210 221 } 222 + 223 + drm_sched_fence_scheduled(job->s_fence, NULL); 224 + drm_sched_fence_finished(job->s_fence, -ESRCH); 225 + WARN_ON(job->s_fence->parent); 226 + job->sched->ops->free_job(job); 227 + } 228 + 229 + /* Signal the scheduler finished fence when the entity in question is killed. */ 230 + static void drm_sched_entity_kill_jobs_cb(struct dma_fence *f, 231 + struct dma_fence_cb *cb) 232 + { 233 + struct drm_sched_job *job = container_of(cb, struct drm_sched_job, 234 + finish_cb); 235 + 236 + dma_fence_put(f); 211 237 212 238 INIT_WORK(&job->work, drm_sched_entity_kill_jobs_work); 213 239 schedule_work(&job->work);