Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH] drm/sched: Sanitise wqueue_start/wqueue_stop/is_stopped interleavings
@ 2026-11-28  8:27 Nicolas Frattaroli
  2026-11-30  9:24 ` Boris Brezillon
  0 siblings, 1 reply; 2+ messages in thread
From: Nicolas Frattaroli @ 2026-11-28  8:27 UTC (permalink / raw)
  To: Boris Brezillon, Matthew Brost, Danilo Krummrich, Philipp Stanner,
	Christian König, Maarten Lankhorst, Maxime Ripard,
	Thomas Zimmermann, David Airlie, Simona Vetter, Luben Tuikov
  Cc: dri-devel, linux-kernel, kernel, stable, Nicolas Frattaroli

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@vger.kernel.org
Signed-off-by: Nicolas Frattaroli <nicolas.frattaroli@collabora.com>
---
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@collabora.com>


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] drm/sched: Sanitise wqueue_start/wqueue_stop/is_stopped interleavings
  2026-11-28  8:27 [PATCH] drm/sched: Sanitise wqueue_start/wqueue_stop/is_stopped interleavings Nicolas Frattaroli
@ 2026-11-30  9:24 ` Boris Brezillon
  0 siblings, 0 replies; 2+ messages in thread
From: Boris Brezillon @ 2026-11-30  9:24 UTC (permalink / raw)
  To: Nicolas Frattaroli
  Cc: Matthew Brost, Danilo Krummrich, Philipp Stanner,
	Christian König, Maarten Lankhorst, Maxime Ripard,
	Thomas Zimmermann, David Airlie, Simona Vetter, Luben Tuikov,
	dri-devel, linux-kernel, kernel, stable

Hi Nicolas,

On Sat, 28 Nov 2026 09:27:07 +0100
Nicolas Frattaroli <nicolas.frattaroli@collabora.com> wrote:

> 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@vger.kernel.org
> Signed-off-by: Nicolas Frattaroli <nicolas.frattaroli@collabora.com>
> ---
> 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);
> +

	/* Queue is already stopped, bail out so we don't mess up with
	 * the disable_cnt on the work items.
	 */
	if (sched->pause_submit)
		return;

See below for more details as to why I think it'd be preferable to do
that here.

> +	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);

This feels clunky given the newly introduced lock is taken in the
stop/start helpers. I get that drm_sched_is_stopped() is the reason you
have to enforce ordering with smp_store_release(), but mixing a lock
for write accesses and lockless mechanism for read accesses feels odd.

> +}
> +
>  /**
>   * 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);
> +

Similarly, I'd add:

	if (!sched->pause_submit)
		return;

> +	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);

This is all super fragile IMHO, because it relies on the fact the state
of the scheduler doesn't change between the moment this helper is called
and the moment the caller decides to do something with the result.
Acquiring the newly introduced pause_lock doesn't help unless we turn
drm_sched_is_stopped() into drm_sched_is_stopped_locked() and force the
caller to check the pause_submit state and do the associated action
with the pause_lock held, but that's likely to cause nasty lock
ordering issues because drivers probably have their own locking to
enforce serialization of pause/unpause sequences of the component
embedding the drm_gpu_scheduler being paused.

My gut feeling is that we should find a way to kill
drm_sched_is_stopped() altogether and try to find a replacement for all
the current users. As things stand, all users in the core gpu scheduler
code are gone. That leaves us with Xe and AMD. The
drm_sched_is_stopped_locked() calls in AMD could go away if we move
the is_stopped check in drm_sched_wqueue_{stop,start}_locked() and bail
out when the queue is already stopped/started. Those in
xe_gpu_scheduler.c can go away if we use the same disable_work_sync()
mechanism for the work_process_msg work. This would leave the one in
xe_guc_submit.c, which I'm sure how to addres. Christian, Marteen,
what's your take on that?

>  }
>  EXPORT_SYMBOL(drm_sched_is_stopped);

Regards,

Boris

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-30  9:24 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-28  8:27 [PATCH] drm/sched: Sanitise wqueue_start/wqueue_stop/is_stopped interleavings Nicolas Frattaroli
2026-11-30  9:24 ` Boris Brezillon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox