dri-devel Archive on lore.kernel.org
 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-28  8:42 ` sashiko-bot
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ 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] 6+ 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-28  8:42 ` sashiko-bot
  2026-11-30  9:24 ` Boris Brezillon
  2026-12-01  7:56 ` Philipp Stanner
  2 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-11-28  8:42 UTC (permalink / raw)
  To: Nicolas Frattaroli; +Cc: dri-devel

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] drm/sched: unbalanced work disable depth in wqueue stop and
  start
- [High] drm/sched: deadlock on pause_lock and work_tdr in
  drm_sched_fini()
- [Medium] drm/sched: lockdep inversion acquiring pause_lock in
  drm_sched_wqueue_start()
- [Low] drm/sched: missing kernel-doc entry for pause_lock in
  drm_gpu_scheduler
--

commit a76a12f06c945a6e5b2e2b77f19934547861ccb1
Author: Nicolas Frattaroli <nicolas.frattaroli@collabora.com>

drm/sched: Sanitise wqueue_start/wqueue_stop/is_stopped interleavings

This commit serializes GPU scheduler workqueue start and stop operations with
a mutex, switches from cancel_work_sync() to disable_work_sync() /
enable_work(), and orders pause_submit accesses with release/acquire barriers.

> diff --git a/drivers/gpu/drm/scheduler/sched_main.c b/drivers/gpu/drm/scheduler/sched_main.c
> index 99252674e2668..4662f024ad885 100644
> --- a/drivers/gpu/drm/scheduler/sched_main.c
> +++ b/drivers/gpu/drm/scheduler/sched_main.c
[ ... ]
> @@ -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);
> +}

[Severity: High]
Can unbalanced drm_sched_wqueue_stop() and drm_sched_wqueue_start() calls leave
work_run_job and work_free_job permanently disabled or underflow their disable
count?

Previously, drm_sched_wqueue_stop() and drm_sched_wqueue_start() were
idempotent, whereas disable_work_sync() and enable_work() increment and
decrement a reference-counted disable depth on each work item. Neither
drm_sched_wqueue_stop_locked() nor drm_sched_wqueue_start() checks
sched->pause_submit before changing the disable depth.

Several drivers do not pair stop and start 1:1. For example,
amdgpu_job_timedout() calls drm_sched_wqueue_stop(&ring->sched) before
attempting a per-ring reset; if amdgpu_ring_reset() fails and falls through to
amdgpu_device_gpu_recover() -> amdgpu_device_halt_activities(),
drm_sched_wqueue_stop(&ring->sched) is called a second time while
amdgpu_device_sched_resume() only calls drm_sched_wqueue_start(&ring->sched)
once. That leaves the work disable depth at 1 while pause_submit is set back
to false, wedging the scheduler ring. Conversely, if amdgpu_ring_reset()
succeeds via amdgpu_sdma_reset_engine(), the helper already restarts
ring->sched before amdgpu_job_timedout() calls drm_sched_wqueue_start() a
second time, triggering the disable count underflow warning in
work_offqd_enable().

Guarding disable_work_sync() and enable_work() on sched->pause_submit under
sched->pause_lock would keep stop and start idempotent.

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

[Severity: High]
Does holding sched->pause_lock across disable_delayed_work_sync(&sched->work_tdr)
cause an ABBA deadlock when a timeout work item is running?

drm_sched_job_timedout() runs on sched->work_tdr and invokes
sched->ops->timedout_job(), where drivers call drm_sched_stop() /
drm_sched_wqueue_stop() and drm_sched_start() / drm_sched_wqueue_start(). Both
now take sched->pause_lock:

CPU0                              CPU1 (work_tdr)
----                              ---------------
drm_sched_fini()                  drm_sched_job_timedout()
  mutex_lock(&sched->pause_lock)    ops->timedout_job()
  ...                                 drm_sched_stop()
  disable_delayed_work_sync(            drm_sched_wqueue_stop()
    &sched->work_tdr)                     mutex_lock(&sched->pause_lock)

 *** DEADLOCK ***

[ ... ]
> @@ -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);
>  }

[Severity: Medium]
Does taking sched->pause_lock in drm_sched_wqueue_start() introduce lockdep
inversions with dma_fence signalling and with work items running on
sched->submit_wq?

In drm_sched_wqueue_stop_locked() and drm_sched_fini(), sched->pause_lock is
held across disable_work_sync(&sched->work_run_job),
disable_work_sync(&sched->work_free_job), drm_sched_cancel_remaining_jobs(),
and destroy_workqueue(sched->submit_wq), chaining sched->pause_lock above the
submit_wq lockdep map and the free_job() / fs_reclaim paths.

In panfrost_reset(), drm_sched_start() -> drm_sched_wqueue_start() is called
inside a dma_fence_begin_signalling() critical section, which chains
dma_fence_signalling -> sched->pause_lock -> work_free_job / fs_reclaim. In
xe's disable_scheduling_deregister(), xe_sched_submission_start() ->
drm_sched_wqueue_start() runs from xe_sched_process_msg_work() on
sched->submit_wq, inverting the sched->pause_lock -> submit_wq order and
risking deadlock if a concurrent drm_sched_wqueue_stop() holds
sched->pause_lock while waiting for work queued behind work_process_msg on the
same ordered submit_wq.

> diff --git a/include/drm/gpu_scheduler.h b/include/drm/gpu_scheduler.h
> index 3b0978fa8cf08..259960fd584ae 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;
>  };

[Severity: Low]
struct drm_gpu_scheduler has a kernel-doc comment block describing all of its
members, so adding pause_lock without a matching @pause_lock entry will
trigger a kernel-doc warning.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261128-drm-sched-fini-race-v1-1-9803ee8aa8b4@collabora.com?part=1

^ permalink raw reply	[flat|nested] 6+ 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-28  8:42 ` sashiko-bot
@ 2026-11-30  9:24 ` Boris Brezillon
  2026-11-30 16:31   ` Philipp Stanner
  2026-12-01  7:56 ` Philipp Stanner
  2 siblings, 1 reply; 6+ 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] 6+ messages in thread

* Re: [PATCH] drm/sched: Sanitise wqueue_start/wqueue_stop/is_stopped interleavings
  2026-11-30  9:24 ` Boris Brezillon
@ 2026-11-30 16:31   ` Philipp Stanner
  0 siblings, 0 replies; 6+ messages in thread
From: Philipp Stanner @ 2026-11-30 16:31 UTC (permalink / raw)
  To: Boris Brezillon, 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

On Mon, 2026-11-30 at 10:24 +0100, Boris Brezillon wrote:
> Hi Nicolas,
> 

[…]

> 
> 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.

+1

> >  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.

+1

> 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

The "caller" is the right keyword here.

> 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.

This existing is_stopped() mechanism seems to be yet another relic
where the gpu_scheduler is used a synchronisation mechanism and a
storage vessel for knowledge that lies in the driver.

The driver stops the scheduler, so it knows that it has stopped it. Why
does it need to check at another place with drm_sched_is_stopped()?

I agree that this function shouldn't exist, and the state for which
function it has called on which object should be stored in driver data.
A first glance at the xe code looks as if there is synchronisation with
a mutex in place there, which is likely the reason why the explosion
has not been observed there.

amdgpu also seems to hold a mutex, except for in
amdgpu_multi_ring_reset_helper_begin().

@Christian, any info on that?


P.

^ permalink raw reply	[flat|nested] 6+ 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-28  8:42 ` sashiko-bot
  2026-11-30  9:24 ` Boris Brezillon
@ 2026-12-01  7:56 ` Philipp Stanner
  2026-12-01 13:18   ` Nicolas Frattaroli
  2 siblings, 1 reply; 6+ messages in thread
From: Philipp Stanner @ 2026-12-01  7:56 UTC (permalink / raw)
  To: Nicolas Frattaroli, 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

On Sat, 2026-11-28 at 09:27 +0100, Nicolas Frattaroli 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.


I just realize that we have recently solved basically the same issue
for drm::JobQueue.

https://lore.kernel.org/rust-for-linux/20261124122951.878199-11-phasta@kernel.org/


We need to park and unpark the queue. We do that for scheduling-
reasons, but the addressed issue is basically the same since we also
use it to halt the run_worker in our JobQueue deconstructor.

The mechanism is based on an atomic bool + srcu to wait for everyone to
have seen the flag. Only then do we tear down. The flag is checked in
the worker, so no matter who might kick the worker when for what
reason, it will never access freed resources.

Maybe you can draw inspiration from that and see whether a mechanism
like that could address the observed problem in drm_sched?


Thx
P.

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

* Re: [PATCH] drm/sched: Sanitise wqueue_start/wqueue_stop/is_stopped interleavings
  2026-12-01  7:56 ` Philipp Stanner
@ 2026-12-01 13:18   ` Nicolas Frattaroli
  0 siblings, 0 replies; 6+ messages in thread
From: Nicolas Frattaroli @ 2026-12-01 13:18 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,
	phasta
  Cc: dri-devel, linux-kernel, kernel, stable

On Tuesday, 1 December 2026 08:56:35 Central European Standard Time Philipp Stanner wrote:
> On Sat, 2026-11-28 at 09:27 +0100, Nicolas Frattaroli 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.
> 
> 
> I just realize that we have recently solved basically the same issue
> for drm::JobQueue.
> 
> https://lore.kernel.org/rust-for-linux/20261124122951.878199-11-phasta@kernel.org/
> 
> 
> We need to park and unpark the queue. We do that for scheduling-
> reasons, but the addressed issue is basically the same since we also
> use it to halt the run_worker in our JobQueue deconstructor.
> 
> The mechanism is based on an atomic bool + srcu to wait for everyone to
> have seen the flag. Only then do we tear down. The flag is checked in
> the worker, so no matter who might kick the worker when for what
> reason, it will never access freed resources.
> 
> Maybe you can draw inspiration from that and see whether a mechanism
> like that could address the observed problem in drm_sched?

Will do. My current solution also has a deadlock according to both
sashiko and casual reading of the code, but funnily enough I could
not yet trigger a lockdep splat by running both code paths. Basically
work_tdr can and will start/stop the drm_sched as well, so synchronously
disabling it while holding the pause_lock in drm_sched_fini is a bad idea.

In practice, lockdep has somehow not complained, despite me confirming with
ftrace that both the timedout function ran and the drm_sched_fini function
ran. I guess because in none of those executions, the timedout thread
synchronised with drm_sched_fini? Who knows.

Kind regards,
Nicolas Frattaroli

> 
> 
> Thx
> P.
> 





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

end of thread, other threads:[~2026-12-01 13:19 UTC | newest]

Thread overview: 6+ 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-28  8:42 ` sashiko-bot
2026-11-30  9:24 ` Boris Brezillon
2026-11-30 16:31   ` Philipp Stanner
2026-12-01  7:56 ` Philipp Stanner
2026-12-01 13:18   ` Nicolas Frattaroli

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