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

drm/panfrost: Handle resetting on timeout better

Panfrost uses multiple schedulers (one for each slot, so 2 in reality),
and on a timeout has to stop all the schedulers to safely perform a
reset. However more than one scheduler can trigger a timeout at the same
time. This race condition results in jobs being freed while they are
still in use.

When stopping other slots use cancel_delayed_work_sync() to ensure that
any timeout started for that slot has completed. Also use
mutex_trylock() to obtain reset_lock. This means that only one thread
attempts the reset, the other threads will simply complete without doing
anything (the first thread will wait for this in the call to
cancel_delayed_work_sync()).

While we're here and since the function is already dependent on
sched_job not being NULL, let's remove the unnecessary checks.

Fixes: aa20236784ab ("drm/panfrost: Prevent concurrent resets")
Tested-by: Neil Armstrong <narmstrong@baylibre.com>
Signed-off-by: Steven Price <steven.price@arm.com>
Signed-off-by: Rob Herring <robh@kernel.org>
Link: https://patchwork.freedesktop.org/patch/msgid/20191009094456.9704-1-steven.price@arm.com

authored by

Steven Price and committed by
Rob Herring
de89212d 603e398a

+11 -5
+11 -5
drivers/gpu/drm/panfrost/panfrost_job.c
··· 381 381 job_read(pfdev, JS_TAIL_LO(js)), 382 382 sched_job); 383 383 384 - mutex_lock(&pfdev->reset_lock); 384 + if (!mutex_trylock(&pfdev->reset_lock)) 385 + return; 385 386 386 - for (i = 0; i < NUM_JOB_SLOTS; i++) 387 - drm_sched_stop(&pfdev->js->queue[i].sched, sched_job); 387 + for (i = 0; i < NUM_JOB_SLOTS; i++) { 388 + struct drm_gpu_scheduler *sched = &pfdev->js->queue[i].sched; 388 389 389 - if (sched_job) 390 - drm_sched_increase_karma(sched_job); 390 + drm_sched_stop(sched, sched_job); 391 + if (js != i) 392 + /* Ensure any timeouts on other slots have finished */ 393 + cancel_delayed_work_sync(&sched->work_tdr); 394 + } 395 + 396 + drm_sched_increase_karma(sched_job); 391 397 392 398 spin_lock_irqsave(&pfdev->js->job_lock, flags); 393 399 for (i = 0; i < NUM_JOB_SLOTS; i++) {