From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=148.251.105.195 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796030669; b=JKfb8wJTOVTjdA/GVJ+Gta2+QX9elJ73DFqBJNedYaTbEeStBZY/HuQK26cotjVgHz3h tZg6YLOMgRfNhyPYDFmZmyJj5OW2HkZESIdoDN3zoODgqOZwtFGq1f403HCtz0+Clarbp tLu2FJYYxS+j6VRs+YfJYAHPeuqaIHTg09tVuzSeUgi5+V6v7QjcxiVs73/Hln5s4A4yY IRScQSltaq4Hwi7h9juTGCbhlnO0jnJN+lsqMYMC+igYqcGSfUrnNsCwmOf5EWZRS9peP t6HUvNBr64foiuuhVcAh/Rdl41HPZl4x2zLB0iZi90gvnGz+uSdgiUHHy/Ikwpw3fHQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796030669; h=DMARC-Filter:DKIM-Signature:Received:Date:From:To:Cc:Subject: Message-ID:In-Reply-To:References:Organization:X-Mailer:MIME-Version: Content-Type:Content-Transfer-Encoding; bh=CC6OG2JUGE9FfGT2ftbvkZwCgijXRvUsATlzGjAbHio=; b=NS2wbBFFc2fwIvo2VtCgh4c5CWUIypTqRtjRE8DezxHjP1RvKgkU7QvEb7PCwtFTn7ZU ctRHZv3lwPICaSJuRnyjJurNPy9lEBfQQHDr6vW80zGA/T+r0osp6JKOfcJRcf11I0rGX pEa6SgPnSRhH5EERrIuKUugGePo3XbB2oWky3V89vhtZkxKuQz0EfIS5bkbBtJNdaKYZl BS9nAe/6Zgym44QniO2NzXfe26UKjhlbKkjzgspuxsSn4/F68t5KL4ayWdAK9g1COs2aV Md8mNEJnVje2vuX3vuS9Bz4YfOggSwfv8BiYRlgzemvldWKC3NrLOJwIZ33Qv7Nt/mg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass header.d=collabora.com header.i=@collabora.com header.a=rsa-sha256 header.s=mail header.b=gH26lpJh; arc=none smtp.remote-ip=148.251.105.195 Received: from bali.collaboradmins.com (bali.collaboradmins.com [148.251.105.195]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 947BB606A31C; Mon, 30 Nov 2026 09:24:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 947BB606A31C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=collabora.com header.i=@collabora.com header.a=rsa-sha256 header.s=mail header.b=gH26lpJh DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1796030666; bh=QGiukGp7ThVyXf+A2yhpqHTSb37rpmp2nZ8+fv0q1wk=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=gH26lpJh7Yb2c1md6yN0/vmXQy5tvlGAxS0lkbof59amI6GGkIRB02DVgByNx2Vut jDZ+oWD8KXGJ7O0oO1+ljt+cZyvAoblhZnA9TcMU4Ir01gjehRzLk3eVQhCEbMDKBU IwIU916u3qmVbHfqvDST2Yk8h6sowSYZ/CJ5kpJlev+i77b9S2gZcCGPmkxfipHfGK TQt6qaOQu477PH4m4DYVZwG+A1Y/GmFE3O0iBB+95vUX0WZ9/Rq+bkhOUbCryAHDvj CdJIj0KqPzbMxl4oXskMS+TITku1AqAP8tdk22Ks4yJuP+HTt1A6eGrHO/tN4JYS9P Wyg04jxxGLKUA== Received: from fedora1.home (unknown [100.64.0.11]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange secp256r1 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: bbrezillon) by bali.collaboradmins.com (Postfix) with ESMTPSA id D312F17E0883; Mon, 30 Nov 2026 10:24:25 +0100 (CET) Date: Mon, 30 Nov 2026 10:24:18 +0100 From: Boris Brezillon To: Nicolas Frattaroli Cc: Matthew Brost , Danilo Krummrich , Philipp Stanner , Christian =?UTF-8?B?S8O2bmln?= , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Luben Tuikov , 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 Message-ID: <20261130102418.68ed62fd@fedora1.home> In-Reply-To: <20261128-drm-sched-fini-race-v1-1-9803ee8aa8b4@collabora.com> References: <20261128-drm-sched-fini-race-v1-1-9803ee8aa8b4@collabora.com> Organization: Collabora X-Mailer: Claws Mail 4.4.0 (GTK 3.24.52; x86_64-redhat-linux-gnu) X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Hi Nicolas, On Sat, 28 Nov 2026 09:27:07 +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. > > 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 > --- > 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