[PATCH] drm/sched: Sanitise wqueue_start/wqueue_stop/is_stopped interleavings

From: Nicolas Frattaroli

Date: Sat Nov 28 2026 - 03:28:11 EST


drm_sched_wqueue_stop() will WRITE_ONCE 1 to the sched's pause_submit
member. This member is checked (with READ_ONCE) in every
drm_sched_run_job_queue() and drm_sched_run_free_queue() invocation,
which is how the two work items drm_sched_wqueue_stop() cancels would
usually get re-armed.

However, this is of little consequence on SMP systems, where the effect
of WRITE_ONCE is not guaranteed to be ordered with READ_ONCE calls on
other CPUs. As a consequence, during drm_sched_fini(), a work item may
become re-armed after its call to drm_sched_wqueue_stop(), and run into
a sched->sched_rq that is now NULL, dereferencing a NULL pointer.

To fix this, add a pause_lock mutex which prevents queue starts and
stops from interleaving. This lock also guarantees that the wqueue
remains stopped during the drm_sched_fini() teardown.

Additionally, use disable_work_sync during wqueue stopping to ensure
that no previously stopped work item can be re-queued during the process
of stopping the wqueue. work_tdr in drm_sched_fini() is also disabled
rather than cancelled, so that no stray work timeouts can trigger
anything of concern during teardown.

Finally, use the smp stores/loads to set and read the pause_submit
member, so that drm_sched_is_stopped() will observe the results of a
start or stop operation in a properly ordered fashion, so that drivers
which use this function don't need to worry about unexpected
interleavings.

The drm_sched_run_job_queue() and drm_sched_run_free_queue() functions
consequently no longer need the (racey) drm_sched_is_stopped() check, as
the work disabling achieves this in an atomic manner immune to TOCTOU
bugs.

Fixes: a6149f039369 ("drm/sched: Convert drm scheduler to use a work queue rather than kthread")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Nicolas Frattaroli <nicolas.frattaroli@xxxxxxxxxxxxx>
---
We've been told of a NULL pointer deref stack trace in
drm_sched_run_job_work() on systems running panthor during teardown of a
GPU context. The affected customer reported that by disabling the work
items in drm_sched_fini(), rather than cancelling them through
drm_sched_wqueue_stop(), the crash would go away. The reason was that a
job could persist beyond the wqueue_stop, since they can be re-armed by
one of the other work items being processed on a different CPU.

Upon looking at the code I realised that drm_sched_wqueue_stop() was
actually broken in several subtle ways that go beyond just changing
which function of the workqueue APIs should be used.

While I was tempted to get rid of drm_sched_is_stopped(), several
drivers do use it. I think it's a potential TOCTOU footgun unless the
driver itself makes sure that no start/stop is issued between the check
and the code that is subsequently run to act on its result. But now it's
at least sequentially consistent as well.
---
drivers/gpu/drm/scheduler/sched_main.c | 52 +++++++++++++++++++++++++++-------
include/drm/gpu_scheduler.h | 1 +
2 files changed, 42 insertions(+), 11 deletions(-)

diff --git a/drivers/gpu/drm/scheduler/sched_main.c b/drivers/gpu/drm/scheduler/sched_main.c
index 99252674e266..4662f024ad88 100644
--- a/drivers/gpu/drm/scheduler/sched_main.c
+++ b/drivers/gpu/drm/scheduler/sched_main.c
@@ -138,8 +138,7 @@ bool drm_sched_can_queue(struct drm_gpu_scheduler *sched,
*/
static void drm_sched_run_job_queue(struct drm_gpu_scheduler *sched)
{
- if (!drm_sched_is_stopped(sched))
- queue_work(sched->submit_wq, &sched->work_run_job);
+ queue_work(sched->submit_wq, &sched->work_run_job);
}

/**
@@ -148,8 +147,7 @@ static void drm_sched_run_job_queue(struct drm_gpu_scheduler *sched)
*/
static void drm_sched_run_free_queue(struct drm_gpu_scheduler *sched)
{
- if (!drm_sched_is_stopped(sched))
- queue_work(sched->submit_wq, &sched->work_free_job);
+ queue_work(sched->submit_wq, &sched->work_free_job);
}

/**
@@ -1153,6 +1151,7 @@ int drm_sched_init(struct drm_gpu_scheduler *sched, const struct drm_sched_init_
init_waitqueue_head(&sched->job_scheduled);
INIT_LIST_HEAD(&sched->pending_list);
spin_lock_init(&sched->job_list_lock);
+ mutex_init(&sched->pause_lock);
atomic_set(&sched->credit_count, 0);
INIT_DELAYED_WORK(&sched->work_tdr, drm_sched_job_timedout);
INIT_WORK(&sched->work_run_job, drm_sched_run_job_work);
@@ -1190,6 +1189,20 @@ static void drm_sched_cancel_remaining_jobs(struct drm_gpu_scheduler *sched)
}
}

+static void drm_sched_wqueue_stop_locked(struct drm_gpu_scheduler *sched)
+{
+ lockdep_assert_held(&sched->pause_lock);
+
+ disable_work_sync(&sched->work_run_job);
+ disable_work_sync(&sched->work_free_job);
+
+ /*
+ * Anything that observes pause_submit will also want to see the effects
+ * of any of the previous memory operations.
+ */
+ smp_store_release(&sched->pause_submit, true);
+}
+
/**
* drm_sched_fini - Destroy a gpu scheduler
*
@@ -1212,13 +1225,15 @@ void drm_sched_fini(struct drm_gpu_scheduler *sched)
{
int i;

- drm_sched_wqueue_stop(sched);
+ /* Guarantee queue is paused for the entirety of the teardown */
+ guard(mutex)(&sched->pause_lock);
+ drm_sched_wqueue_stop_locked(sched);

/* Wakeup everyone stuck in drm_sched_entity_flush for this scheduler */
wake_up_all(&sched->job_scheduled);

/* Confirm no work left behind accessing device structures */
- cancel_delayed_work_sync(&sched->work_tdr);
+ disable_delayed_work_sync(&sched->work_tdr);

/* Avoid memory leaks if supported by the driver. */
if (sched->ops->cancel_job)
@@ -1303,9 +1318,9 @@ EXPORT_SYMBOL(drm_sched_wqueue_ready);
*/
void drm_sched_wqueue_stop(struct drm_gpu_scheduler *sched)
{
- WRITE_ONCE(sched->pause_submit, true);
- cancel_work_sync(&sched->work_run_job);
- cancel_work_sync(&sched->work_free_job);
+ guard(mutex)(&sched->pause_lock);
+
+ drm_sched_wqueue_stop_locked(sched);
}
EXPORT_SYMBOL(drm_sched_wqueue_stop);

@@ -1320,9 +1335,19 @@ EXPORT_SYMBOL(drm_sched_wqueue_stop);
*/
void drm_sched_wqueue_start(struct drm_gpu_scheduler *sched)
{
- WRITE_ONCE(sched->pause_submit, false);
+ guard(mutex)(&sched->pause_lock);
+
+ enable_work(&sched->work_run_job);
+ enable_work(&sched->work_free_job);
+
queue_work(sched->submit_wq, &sched->work_run_job);
queue_work(sched->submit_wq, &sched->work_free_job);
+
+ /*
+ * Anything that observes pause_submit will also want to see the effects
+ * of any of the previous memory operations.
+ */
+ smp_store_release(&sched->pause_submit, false);
}
EXPORT_SYMBOL(drm_sched_wqueue_start);

@@ -1334,7 +1359,12 @@ EXPORT_SYMBOL(drm_sched_wqueue_start);
*/
bool drm_sched_is_stopped(struct drm_gpu_scheduler *sched)
{
- return READ_ONCE(sched->pause_submit);
+ /*
+ * Ensure drm_sched_wqueue_{stop,start}() && drm_sched_is_stopped()
+ * gives a consistent view to any observer that ensures no concurrent
+ * starting or stopping operation is ongoing.
+ */
+ return smp_load_acquire(&sched->pause_submit);
}
EXPORT_SYMBOL(drm_sched_is_stopped);

diff --git a/include/drm/gpu_scheduler.h b/include/drm/gpu_scheduler.h
index 3b0978fa8cf0..259960fd584a 100644
--- a/include/drm/gpu_scheduler.h
+++ b/include/drm/gpu_scheduler.h
@@ -627,6 +627,7 @@ struct drm_gpu_scheduler {
bool free_guilty;
bool pause_submit;
bool own_submit_wq;
+ struct mutex pause_lock;
struct device *dev;
};


---
base-commit: bf57105b00e32d37b666c029c8c6b8708391da20
change-id: 20261123-drm-sched-fini-race-9c1d2a8bbb76

Best regards,
--
Nicolas Frattaroli <nicolas.frattaroli@xxxxxxxxxxxxx>