From: Boris Brezillon <boris.brezillon@collabora.com>
To: Nicolas Frattaroli <nicolas.frattaroli@collabora.com>
Cc: "Matthew Brost" <matthew.brost@intel.com>,
"Danilo Krummrich" <dakr@kernel.org>,
"Philipp Stanner" <phasta@kernel.org>,
"Christian König" <ckoenig.leichtzumerken@gmail.com>,
"Maarten Lankhorst" <maarten.lankhorst@linux.intel.com>,
"Maxime Ripard" <mripard@kernel.org>,
"Thomas Zimmermann" <tzimmermann@suse.de>,
"David Airlie" <airlied@gmail.com>,
"Simona Vetter" <simona@ffwll.ch>,
"Luben Tuikov" <luben.tuikov@amd.com>,
dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org,
kernel@collabora.com, stable@vger.kernel.org
Subject: Re: [PATCH] drm/sched: Sanitise wqueue_start/wqueue_stop/is_stopped interleavings
Date: Mon, 30 Nov 2026 10:24:18 +0100 [thread overview]
Message-ID: <20261130102418.68ed62fd@fedora1.home> (raw)
In-Reply-To: <20261128-drm-sched-fini-race-v1-1-9803ee8aa8b4@collabora.com>
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
prev parent reply other threads:[~2026-11-30 9:24 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
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 message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261130102418.68ed62fd@fedora1.home \
--to=boris.brezillon@collabora.com \
--cc=airlied@gmail.com \
--cc=ckoenig.leichtzumerken@gmail.com \
--cc=dakr@kernel.org \
--cc=dri-devel@lists.freedesktop.org \
--cc=kernel@collabora.com \
--cc=linux-kernel@vger.kernel.org \
--cc=luben.tuikov@amd.com \
--cc=maarten.lankhorst@linux.intel.com \
--cc=matthew.brost@intel.com \
--cc=mripard@kernel.org \
--cc=nicolas.frattaroli@collabora.com \
--cc=phasta@kernel.org \
--cc=simona@ffwll.ch \
--cc=stable@vger.kernel.org \
--cc=tzimmermann@suse.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox