From nobody Tue Dec 16 23:43:32 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9C68D1F3BAB for ; Thu, 24 Apr 2025 09:56:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745488563; cv=none; b=G47j8PaEd3ciNV0152m3imFnGucdJZXi9kfrfWlttGTvXtbbJIGnrLprduRFXrdXsNR1B3u4jPCuAiCcQ9YO4SlUeS2cQgLmLJ971f7ftrNOWxWY4c/O3r3NIOvCZutaLhiz1kqJ2Rzpyn8YSCA0NHLeJTMBkFMuJPb2iBugLLo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745488563; c=relaxed/simple; bh=rpW5JFhdFXiSSz8ekySoqWMfhA5/SQ3rnqjUf4YRGGY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SFveoJy4mmNF5t0slFhn0VeFQNdDivFY4UFDSxlJWlk4iTiFyBQ+5YguHUcw4/u0LZtmJD3KLk9h/hxZlBFmtyb5K7UEQnIZR+yazaStdfmLD6mzWxH77JjVsNicYqbPML5hBYQGLbsPJX1ahA6zY/alzk/QxSOnJH9vdzuotfA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=K1vl9gmQ; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="K1vl9gmQ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A5749C4CEEA; Thu, 24 Apr 2025 09:55:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1745488563; bh=rpW5JFhdFXiSSz8ekySoqWMfhA5/SQ3rnqjUf4YRGGY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=K1vl9gmQyTzRf7o2oERSfo6iU4W77OYw1L3xnMrDb+I0y7dTXFA2dj6kz+hCRayp3 hyu/fNBZ/O7n5Xrh15PDPatnkZJA1hkmdrDxsvo72AyO3LL2zjrq0RUtyjSTBZBn5F UFdQrzEaUQV4NjM0UNtclfq1D5Sje+BOzhKLfJMi5JjF6Wq41Um6w8ly1VGjK2IAYk Inwe+uhJzuKd48LkIS5879CVHNbZI4yhP8nVwOGzvrmTz0gSeUEbu8Y/p/5hoak2L+ BhxApf4yyas6qgmIUtfiR7nUyy0AopK8p0QkVJ36IFFWdxJhzSbjSKO0ePSCMmHVUq fn8jrn650Ryaw== From: Philipp Stanner To: Lyude Paul , Danilo Krummrich , David Airlie , Simona Vetter , Matthew Brost , Philipp Stanner , =?UTF-8?q?Christian=20K=C3=B6nig?= , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , Tvrtko Ursulin Cc: dri-devel@lists.freedesktop.org, nouveau@lists.freedesktop.org, linux-kernel@vger.kernel.org, Philipp Stanner Subject: [PATCH v2 1/6] drm/sched: Fix teardown leaks with waitqueue Date: Thu, 24 Apr 2025 11:55:31 +0200 Message-ID: <20250424095535.26119-3-phasta@kernel.org> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250424095535.26119-2-phasta@kernel.org> References: <20250424095535.26119-2-phasta@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Philipp Stanner The GPU scheduler currently does not ensure that its pending_list is empty before performing various other teardown tasks in drm_sched_fini(). If there are still jobs in the pending_list, this is problematic because after scheduler teardown, no one will call backend_ops.free_job() anymore. This would, consequently, result in memory leaks. One way to solve this is to implement a waitqueue that drm_sched_fini() blocks on until the pending_list has become empty. Add a waitqueue to struct drm_gpu_scheduler. Wake up waiters once the pending_list becomes empty. Wait in drm_sched_fini() for that to happen. Signed-off-by: Philipp Stanner --- drivers/gpu/drm/scheduler/sched_main.c | 72 ++++++++++++++++++++------ include/drm/gpu_scheduler.h | 6 +++ 2 files changed, 63 insertions(+), 15 deletions(-) diff --git a/drivers/gpu/drm/scheduler/sched_main.c b/drivers/gpu/drm/sched= uler/sched_main.c index 829579c41c6b..ea82e69a72a8 100644 --- a/drivers/gpu/drm/scheduler/sched_main.c +++ b/drivers/gpu/drm/scheduler/sched_main.c @@ -367,7 +367,7 @@ static void drm_sched_run_job_queue(struct drm_gpu_sche= duler *sched) */ static void __drm_sched_run_free_queue(struct drm_gpu_scheduler *sched) { - if (!READ_ONCE(sched->pause_submit)) + if (!READ_ONCE(sched->pause_free)) queue_work(sched->submit_wq, &sched->work_free_job); } =20 @@ -556,6 +556,7 @@ static void drm_sched_job_timedout(struct work_struct *= work) * is parked at which point it's safe. */ list_del_init(&job->list); + spin_unlock(&sched->job_list_lock); =20 status =3D job->sched->ops->timedout_job(job); @@ -1119,6 +1120,12 @@ drm_sched_get_finished_job(struct drm_gpu_scheduler = *sched) /* remove job from pending_list */ list_del_init(&job->list); =20 + /* + * Inform tasks blocking in drm_sched_fini() that it's now safe to proce= ed. + */ + if (list_empty(&sched->pending_list)) + wake_up(&sched->pending_list_waitque); + /* cancel this job's TO timer */ cancel_delayed_work(&sched->work_tdr); /* make the scheduled timestamp more accurate */ @@ -1324,6 +1331,7 @@ int drm_sched_init(struct drm_gpu_scheduler *sched, c= onst struct drm_sched_init_ init_waitqueue_head(&sched->job_scheduled); INIT_LIST_HEAD(&sched->pending_list); spin_lock_init(&sched->job_list_lock); + init_waitqueue_head(&sched->pending_list_waitque); 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); @@ -1331,6 +1339,7 @@ int drm_sched_init(struct drm_gpu_scheduler *sched, c= onst struct drm_sched_init_ atomic_set(&sched->_score, 0); atomic64_set(&sched->job_id_count, 0); sched->pause_submit =3D false; + sched->pause_free =3D false; =20 sched->ready =3D true; return 0; @@ -1348,6 +1357,39 @@ int drm_sched_init(struct drm_gpu_scheduler *sched, = const struct drm_sched_init_ } EXPORT_SYMBOL(drm_sched_init); =20 +/** + * drm_sched_submission_and_timeout_stop - stop everything except for free= _job() + * @sched: scheduler instance + * + * Only needed to cleanly tear down the scheduler in drm_sched_fini(). + */ +static inline void +drm_sched_submission_and_timeout_stop(struct drm_gpu_scheduler *sched) +{ + WRITE_ONCE(sched->pause_submit, true); + cancel_work_sync(&sched->work_run_job); + cancel_delayed_work_sync(&sched->work_tdr); +} + +static inline void +drm_sched_free_stop(struct drm_gpu_scheduler *sched) +{ + WRITE_ONCE(sched->pause_free, true); + cancel_work_sync(&sched->work_free_job); +} + +static inline bool +drm_sched_no_jobs_pending(struct drm_gpu_scheduler *sched) +{ + bool empty; + + spin_lock(&sched->job_list_lock); + empty =3D list_empty(&sched->pending_list); + spin_unlock(&sched->job_list_lock); + + return empty; +} + /** * drm_sched_fini - Destroy a gpu scheduler * @@ -1355,26 +1397,24 @@ EXPORT_SYMBOL(drm_sched_init); * * Tears down and cleans up the scheduler. * - * This stops submission of new jobs to the hardware through - * drm_sched_backend_ops.run_job(). Consequently, drm_sched_backend_ops.fr= ee_job() - * will not be called for all jobs still in drm_gpu_scheduler.pending_list. - * There is no solution for this currently. Thus, it is up to the driver t= o make - * sure that: - * - * a) drm_sched_fini() is only called after for all submitted jobs - * drm_sched_backend_ops.free_job() has been called or that - * b) the jobs for which drm_sched_backend_ops.free_job() has not been ca= lled - * after drm_sched_fini() ran are freed manually. - * - * FIXME: Take care of the above problem and prevent this function from le= aking - * the jobs in drm_gpu_scheduler.pending_list under any circumstances. + * Note that this function blocks until all the fences returned by + * &struct drm_sched_backend_ops.run_job have been signalled. */ void drm_sched_fini(struct drm_gpu_scheduler *sched) { struct drm_sched_entity *s_entity; int i; =20 - drm_sched_wqueue_stop(sched); + /* + * Jobs that have neither been scheduled or which have timed out are + * gone by now, but jobs that have been submitted through + * backend_ops.run_job() and have not yet terminated are still pending. + * + * Wait for the pending_list to become empty to avoid leaking those jobs. + */ + drm_sched_submission_and_timeout_stop(sched); + wait_event(sched->pending_list_waitque, drm_sched_no_jobs_pending(sched)); + drm_sched_free_stop(sched); =20 for (i =3D DRM_SCHED_PRIORITY_KERNEL; i < sched->num_rqs; i++) { struct drm_sched_rq *rq =3D sched->sched_rq[i]; @@ -1471,6 +1511,7 @@ EXPORT_SYMBOL(drm_sched_wqueue_ready); void drm_sched_wqueue_stop(struct drm_gpu_scheduler *sched) { WRITE_ONCE(sched->pause_submit, true); + WRITE_ONCE(sched->pause_free, true); cancel_work_sync(&sched->work_run_job); cancel_work_sync(&sched->work_free_job); } @@ -1488,6 +1529,7 @@ EXPORT_SYMBOL(drm_sched_wqueue_stop); void drm_sched_wqueue_start(struct drm_gpu_scheduler *sched) { WRITE_ONCE(sched->pause_submit, false); + WRITE_ONCE(sched->pause_free, false); queue_work(sched->submit_wq, &sched->work_run_job); queue_work(sched->submit_wq, &sched->work_free_job); } diff --git a/include/drm/gpu_scheduler.h b/include/drm/gpu_scheduler.h index 1a7e377d4cbb..d0b1f416b4d9 100644 --- a/include/drm/gpu_scheduler.h +++ b/include/drm/gpu_scheduler.h @@ -29,6 +29,7 @@ #include #include #include +#include =20 #define MAX_WAIT_SCHED_ENTITY_Q_EMPTY msecs_to_jiffies(1000) =20 @@ -533,6 +534,8 @@ struct drm_sched_backend_ops { * timeout interval is over. * @pending_list: the list of jobs which are currently in the job queue. * @job_list_lock: lock to protect the pending_list. + * @pending_list_waitque: a waitqueue for drm_sched_fini() to block on unt= il all + * pending jobs have been finished. * @hang_limit: once the hangs by a job crosses this limit then it is mark= ed * guilty and it will no longer be considered for scheduling. * @score: score to help loadbalancer pick a idle sched @@ -540,6 +543,7 @@ struct drm_sched_backend_ops { * @ready: marks if the underlying HW is ready to work * @free_guilty: A hit to time out handler to free the guilty job. * @pause_submit: pause queuing of @work_run_job on @submit_wq + * @pause_free: pause queuing of @work_free_job on @submit_wq * @own_submit_wq: scheduler owns allocation of @submit_wq * @dev: system &struct device * @@ -562,12 +566,14 @@ struct drm_gpu_scheduler { struct delayed_work work_tdr; struct list_head pending_list; spinlock_t job_list_lock; + wait_queue_head_t pending_list_waitque; int hang_limit; atomic_t *score; atomic_t _score; bool ready; bool free_guilty; bool pause_submit; + bool pause_free; bool own_submit_wq; struct device *dev; }; --=20 2.48.1 From nobody Tue Dec 16 23:43:32 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 91B3721FF45 for ; Thu, 24 Apr 2025 09:56:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745488567; cv=none; b=tPPEIakGv/UABwodMTju3/rkjttKzTxUBq4NAZs/yoPOuMiLbwDPSc9Waa6yUrGBlO+/cXarhpIKkz6AJPl2KBWrzWUOrTSGcoNsNjR5NP6EOMtXloqSE9ST+Hh/CwkwP5S5V0r5PFRy/ygzDyLXuLWKuvJ0zHeScsyDQGGPgxA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745488567; c=relaxed/simple; bh=riFR0MXBDay78EEbu4sXTWErrIzJCWihr4JqE0kKrtI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=P/Mv7kwFPXFbWSdqtgv4hXsa+GO1f9u2wDLEGskhNOuxIgud+EoVFJ1bHdC4V+/pMjmG2FZRW11tI/9bVKvCgzXC0OZ5t1Shmkl8jn6xAJlXebiuxU96YrFhFqK8i9xcDQUCceSlXxkGvDEv8J2DknQKNltjm2brvyhU3YiEz+0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DFiHTdhr; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="DFiHTdhr" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A29C5C4CEEC; Thu, 24 Apr 2025 09:56:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1745488567; bh=riFR0MXBDay78EEbu4sXTWErrIzJCWihr4JqE0kKrtI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DFiHTdhrmgSaHGJOrJQT80r7WMLWqPcvQQ0mwqGv7Ns0y5S4Hnggyr1owPS43ONpo IgyPpOEH7wPLsPMqGXBClKMo/HjXD3sLXPGvQ3cH+jqBC6abqfAI6tSeaADz485uXT GRxk0HGSGfLsvUW4rHvdbHyhIPbTFMx38T6KO5gQ+3l/cOXtoDznly0pQqQeJsb5Qd sbDDWDQxveQ9xCfUdwqNmS+ZwK+msWe7d1FJluh8OVbKPhyojhWiom9+vczmpGkLf2 SL68wk2O0FbMi60Hx/5F6pJQiFAopwo1WkPb/7AUGy6VRr6dorJU+pHO8MdGUx2MT+ MziqZiVrztqRg== From: Philipp Stanner To: Lyude Paul , Danilo Krummrich , David Airlie , Simona Vetter , Matthew Brost , Philipp Stanner , =?UTF-8?q?Christian=20K=C3=B6nig?= , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , Tvrtko Ursulin Cc: dri-devel@lists.freedesktop.org, nouveau@lists.freedesktop.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH v2 2/6] drm/sched: Prevent teardown waitque from blocking too long Date: Thu, 24 Apr 2025 11:55:32 +0200 Message-ID: <20250424095535.26119-4-phasta@kernel.org> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250424095535.26119-2-phasta@kernel.org> References: <20250424095535.26119-2-phasta@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The waitqueue that ensures that drm_sched_fini() blocks until the pending_list has become empty could theoretically cause that function to block for a very long time. That, ultimately, could block userspace procesess and prevent them from being killable through SIGKILL. When a driver calls drm_sched_fini(), it is safe to assume that all still pending jobs are not needed anymore anyways. Thus, they can be cancelled and thereby it can be ensured that drm_sched_fini() will return relatively quickly. Implement a new helper to stop all work items / submission except for the drm_sched_backend_ops.run_job(). Implement a driver callback, kill_fence_context(), that instructs the driver to kill the fence context associated with this scheduler, thereby causing all pending hardware fences to be signalled. Call those new routines in drm_sched_fini() and ensure backwards compatibility if the new callback is not implemented. Suggested-by: Danilo Krummrich Signed-off-by: Philipp Stanner --- drivers/gpu/drm/scheduler/sched_main.c | 47 +++++++++++++++++--------- include/drm/gpu_scheduler.h | 11 ++++++ 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/drivers/gpu/drm/scheduler/sched_main.c b/drivers/gpu/drm/sched= uler/sched_main.c index ea82e69a72a8..c2ad6c70bfb6 100644 --- a/drivers/gpu/drm/scheduler/sched_main.c +++ b/drivers/gpu/drm/scheduler/sched_main.c @@ -1390,31 +1390,46 @@ drm_sched_no_jobs_pending(struct drm_gpu_scheduler = *sched) return empty; } =20 +/** + * drm_sched_cancel_jobs_and_wait - trigger freeing of all pending jobs + * @sched: scheduler instance + * + * Must only be called if &struct drm_sched_backend_ops.kill_fence_context= is + * implemented. + * + * Instructs the driver to kill the fence context associated with this sch= eduler, + * thereby signalling all pending fences. This, in turn, will trigger + * &struct drm_sched_backend_ops.free_job to be called for all pending job= s. + * The function then blocks until all pending jobs have been freed. + */ +static inline void +drm_sched_cancel_jobs_and_wait(struct drm_gpu_scheduler *sched) +{ + sched->ops->kill_fence_context(sched); + wait_event(sched->pending_list_waitque, drm_sched_no_jobs_pending(sched)); +} + /** * drm_sched_fini - Destroy a gpu scheduler * * @sched: scheduler instance * - * Tears down and cleans up the scheduler. - * - * Note that this function blocks until all the fences returned by - * &struct drm_sched_backend_ops.run_job have been signalled. + * Tears down and cleans up the scheduler. Might leak memory if + * &struct drm_sched_backend_ops.kill_fence_context is not implemented. */ void drm_sched_fini(struct drm_gpu_scheduler *sched) { struct drm_sched_entity *s_entity; int i; =20 - /* - * Jobs that have neither been scheduled or which have timed out are - * gone by now, but jobs that have been submitted through - * backend_ops.run_job() and have not yet terminated are still pending. - * - * Wait for the pending_list to become empty to avoid leaking those jobs. - */ - drm_sched_submission_and_timeout_stop(sched); - wait_event(sched->pending_list_waitque, drm_sched_no_jobs_pending(sched)); - drm_sched_free_stop(sched); + if (sched->ops->kill_fence_context) { + drm_sched_submission_and_timeout_stop(sched); + drm_sched_cancel_jobs_and_wait(sched); + drm_sched_free_stop(sched); + } else { + /* We're in "legacy free-mode" and ignore potential mem leaks */ + drm_sched_wqueue_stop(sched); + } =20 for (i =3D DRM_SCHED_PRIORITY_KERNEL; i < sched->num_rqs; i++) { struct drm_sched_rq *rq =3D sched->sched_rq[i]; @@ -1502,7 +1517,7 @@ bool drm_sched_wqueue_ready(struct drm_gpu_scheduler = *sched) EXPORT_SYMBOL(drm_sched_wqueue_ready); =20 /** - * drm_sched_wqueue_stop - stop scheduler submission + * drm_sched_wqueue_stop - stop scheduler submission and freeing * @sched: scheduler instance * * Stops the scheduler from pulling new jobs from entities. It also stops @@ -1518,7 +1533,7 @@ void drm_sched_wqueue_stop(struct drm_gpu_scheduler *= sched) EXPORT_SYMBOL(drm_sched_wqueue_stop); =20 /** - * drm_sched_wqueue_start - start scheduler submission + * drm_sched_wqueue_start - start scheduler submission and freeing * @sched: scheduler instance * * Restarts the scheduler after drm_sched_wqueue_stop() has stopped it. diff --git a/include/drm/gpu_scheduler.h b/include/drm/gpu_scheduler.h index d0b1f416b4d9..8630b4a26f10 100644 --- a/include/drm/gpu_scheduler.h +++ b/include/drm/gpu_scheduler.h @@ -509,6 +509,17 @@ struct drm_sched_backend_ops { * and it's time to clean it up. */ void (*free_job)(struct drm_sched_job *sched_job); + + /** + * @kill_fence_context: kill the fence context belonging to this scheduler + * + * Needed to cleanly tear the scheduler down in drm_sched_fini(). This + * callback will cause all hardware fences to be signalled by the driver, + * which, ultimately, ensures that all jobs get freed before teardown. + * + * This callback is optional, but it is highly recommended to implement i= t. + */ + void (*kill_fence_context)(struct drm_gpu_scheduler *sched); }; =20 /** --=20 2.48.1 From nobody Tue Dec 16 23:43:32 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7E804221261 for ; Thu, 24 Apr 2025 09:56:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745488571; cv=none; b=mJKJNG0IG9A7jyHf2sGWubNn30vieci+ib4H+wNYBSxRPjghU2/FbqqBaI85VlR1rrX1dwgDgFs7NT9FcqeJftlcwwKnsxxslqjYLgnNeRlEiMJRiXfoJTZkULVUfZ+GyC3iUF9fRjUroXInQ48NIN72cOidzqmaRit+V5FKGps= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745488571; c=relaxed/simple; bh=KwTxUWB8eoY1enwOmL1Rz7TnjVwHUrW7NjL5EDBhkRE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Rswld3AA6q6FbpwyTwmp58aoMHOv0hDnucxQbWliSCUTUhtp6YAG3YEaHKApMMz++pH0G+guz9KER1pleD40Qy759XCrjzVSdh9boCgc6H18cAA3Uf7Et+oA3x5Jx9C642TZciFe/vzEqqk20PSKtrFd7VwVmwCHupyeNEkBYUA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=KEqN75eq; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="KEqN75eq" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A239EC4CEEB; Thu, 24 Apr 2025 09:56:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1745488571; bh=KwTxUWB8eoY1enwOmL1Rz7TnjVwHUrW7NjL5EDBhkRE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KEqN75eqyoVVKaygRATIKVaTzIYqoNMjVzyvM+liC+gY5iTMuVg9bK6KI/4CLxIxL 1XQiTOUQocxaJLcGFUZIa9c7PfQJXaB4brw2ZXtH62uKWsiMTXrSs/a6RhRgrZVrTL OVdtXZABxPyNw4QCqjTHnTkTztBNk7VobZAjIvGeBLTDclf8nywqFCyP58FC87RnPy HXOt/+FL/ukQUKD6T4JTD4oqA2h/E31zqN+az/YBYmbLGJdm9ZUfra3+KA7Cjh+HCH MMmx6c9PnIdm1FF8LLmHAYfZiKfaGkJWJIxOs2uA4DkWHhRDFZbOKBLj5jAeo25SBp vW3wInHd7NWgA== From: Philipp Stanner To: Lyude Paul , Danilo Krummrich , David Airlie , Simona Vetter , Matthew Brost , Philipp Stanner , =?UTF-8?q?Christian=20K=C3=B6nig?= , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , Tvrtko Ursulin Cc: dri-devel@lists.freedesktop.org, nouveau@lists.freedesktop.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 3/6] drm/sched: Warn if pending list is not empty Date: Thu, 24 Apr 2025 11:55:33 +0200 Message-ID: <20250424095535.26119-5-phasta@kernel.org> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250424095535.26119-2-phasta@kernel.org> References: <20250424095535.26119-2-phasta@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" drm_sched_fini() can leak jobs under certain circumstances. Warn if that happens. Signed-off-by: Philipp Stanner --- drivers/gpu/drm/scheduler/sched_main.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/gpu/drm/scheduler/sched_main.c b/drivers/gpu/drm/sched= uler/sched_main.c index c2ad6c70bfb6..0c56b85c574f 100644 --- a/drivers/gpu/drm/scheduler/sched_main.c +++ b/drivers/gpu/drm/scheduler/sched_main.c @@ -1457,6 +1457,9 @@ void drm_sched_fini(struct drm_gpu_scheduler *sched) sched->ready =3D false; kfree(sched->sched_rq); sched->sched_rq =3D NULL; + + if (!list_empty(&sched->pending_list)) + dev_err(sched->dev, "Tearing down scheduler while jobs are pending!\n"); } EXPORT_SYMBOL(drm_sched_fini); =20 --=20 2.48.1 From nobody Tue Dec 16 23:43:32 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8296022170B for ; Thu, 24 Apr 2025 09:56:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745488576; cv=none; b=ksujgsnbs8dyOss8JbTzhZiQxF+N56q5LaYe1ZdCf7Jat0PJ21lXlGU+5eIGw3s1MmNr5AErLgjsZ9gLIgHXN9+K1GyQbgVtRPICXQr2EzO3jZQfTV4kM8b+Wo8OjNAtSBHyvfZO0RamAokrhH9RdWTfIsVLjf8tLUO85A9TvHc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745488576; c=relaxed/simple; bh=jWhUXx8C0pjgulz9f55tvHNeys7srTOZ3/RnhWeUk5Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Y5Kw5a4FMBlGcOw7e5PGOqnear6H/eQKaS85jxJ5BZIVOSTlOjMZCUEGF6YeYRsqrbEzMSsDn+qQTKqDwuUbfGm/f8vts1Z2KXLDhAtwo6kqTZLJUGWXehb+t05nA1gSy5O2UoR2GVVjTdwmLKHWAA9P1i+9vQ/pvJXAo3IJpfc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=QOQeDJpI; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="QOQeDJpI" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6EAA4C4CEE3; Thu, 24 Apr 2025 09:56:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1745488574; bh=jWhUXx8C0pjgulz9f55tvHNeys7srTOZ3/RnhWeUk5Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QOQeDJpI7lVlNB7hZwnGy70cpZqHqlUdfqbtzK8ADj/sGvDaewdztJqkAWOgy6ywy wYHBRer1MGX57f8S86pEx7QO3dyhXTbEked6yBwrb6R1I13FBQ52XaIjtwz0qtVpE5 15vkiSRiUzs+nu3PS/W/T20Rx6EkWy/El71vyuYasgnUYg7eYMHif0uzrwZt0+viRh R6qKx8LlEYfw0Piqb+f9zfcsNbQBcGNIfz7QADF8BZPgPA+kppm9+1paqE094YQn2+ dM1C/INg0wMa6boocd2507yFk5KBrE+nI+pmX2PF/nmV3OXOlYOFDv8gf8kDCCkZZ6 TV1FG4C949Ozg== From: Philipp Stanner To: Lyude Paul , Danilo Krummrich , David Airlie , Simona Vetter , Matthew Brost , Philipp Stanner , =?UTF-8?q?Christian=20K=C3=B6nig?= , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , Tvrtko Ursulin Cc: dri-devel@lists.freedesktop.org, nouveau@lists.freedesktop.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 4/6] drm/nouveau: Add new callback for scheduler teardown Date: Thu, 24 Apr 2025 11:55:34 +0200 Message-ID: <20250424095535.26119-6-phasta@kernel.org> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250424095535.26119-2-phasta@kernel.org> References: <20250424095535.26119-2-phasta@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" There is a new callback for always tearing the scheduler down in a leak-free, deadlock-free manner. Port Nouveau as its first user by providing the scheduler with a callback that ensures the fence context gets killed in drm_sched_fini(). Signed-off-by: Philipp Stanner --- drivers/gpu/drm/nouveau/nouveau_abi16.c | 4 ++-- drivers/gpu/drm/nouveau/nouveau_drm.c | 2 +- drivers/gpu/drm/nouveau/nouveau_sched.c | 19 +++++++++++++++++-- drivers/gpu/drm/nouveau/nouveau_sched.h | 3 +++ 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/drivers/gpu/drm/nouveau/nouveau_abi16.c b/drivers/gpu/drm/nouv= eau/nouveau_abi16.c index 2a0617e5fe2a..53b8a85a8adb 100644 --- a/drivers/gpu/drm/nouveau/nouveau_abi16.c +++ b/drivers/gpu/drm/nouveau/nouveau_abi16.c @@ -415,8 +415,8 @@ nouveau_abi16_ioctl_channel_alloc(ABI16_IOCTL_ARGS) * The client lock is already acquired by nouveau_abi16_get(). */ if (nouveau_cli_uvmm(cli)) { - ret =3D nouveau_sched_create(&chan->sched, drm, drm->sched_wq, - chan->chan->dma.ib_max); + ret =3D nouveau_sched_create(&chan->sched, drm, chan->chan, + drm->sched_wq, chan->chan->dma.ib_max); if (ret) goto done; } diff --git a/drivers/gpu/drm/nouveau/nouveau_drm.c b/drivers/gpu/drm/nouvea= u/nouveau_drm.c index c69139701056..2a2b319dca5f 100644 --- a/drivers/gpu/drm/nouveau/nouveau_drm.c +++ b/drivers/gpu/drm/nouveau/nouveau_drm.c @@ -313,7 +313,7 @@ nouveau_cli_init(struct nouveau_drm *drm, const char *s= name, * locks which indirectly or directly are held for allocations * elsewhere. */ - ret =3D nouveau_sched_create(&cli->sched, drm, NULL, 1); + ret =3D nouveau_sched_create(&cli->sched, drm, NULL, NULL, 1); if (ret) goto done; =20 diff --git a/drivers/gpu/drm/nouveau/nouveau_sched.c b/drivers/gpu/drm/nouv= eau/nouveau_sched.c index d326e55d2d24..3659ac78bb3e 100644 --- a/drivers/gpu/drm/nouveau/nouveau_sched.c +++ b/drivers/gpu/drm/nouveau/nouveau_sched.c @@ -11,6 +11,7 @@ #include "nouveau_exec.h" #include "nouveau_abi16.h" #include "nouveau_sched.h" +#include "nouveau_chan.h" =20 #define NOUVEAU_SCHED_JOB_TIMEOUT_MS 10000 =20 @@ -392,10 +393,22 @@ nouveau_sched_free_job(struct drm_sched_job *sched_jo= b) nouveau_job_fini(job); } =20 +static void +nouveau_sched_fence_context_kill(struct drm_gpu_scheduler *sched) +{ + struct nouveau_sched *nsched; + + nsched =3D container_of(sched, struct nouveau_sched, base); + + if (nsched->chan) + nouveau_channel_kill(nsched->chan); +} + static const struct drm_sched_backend_ops nouveau_sched_ops =3D { .run_job =3D nouveau_sched_run_job, .timedout_job =3D nouveau_sched_timedout_job, .free_job =3D nouveau_sched_free_job, + .kill_fence_context =3D nouveau_sched_fence_context_kill, }; =20 static int @@ -461,7 +474,8 @@ nouveau_sched_init(struct nouveau_sched *sched, struct = nouveau_drm *drm, =20 int nouveau_sched_create(struct nouveau_sched **psched, struct nouveau_drm *dr= m, - struct workqueue_struct *wq, u32 credit_limit) + struct nouveau_channel *chan, struct workqueue_struct *wq, + u32 credit_limit) { struct nouveau_sched *sched; int ret; @@ -470,6 +484,8 @@ nouveau_sched_create(struct nouveau_sched **psched, str= uct nouveau_drm *drm, if (!sched) return -ENOMEM; =20 + sched->chan =3D chan; + ret =3D nouveau_sched_init(sched, drm, wq, credit_limit); if (ret) { kfree(sched); @@ -481,7 +497,6 @@ nouveau_sched_create(struct nouveau_sched **psched, str= uct nouveau_drm *drm, return 0; } =20 - static void nouveau_sched_fini(struct nouveau_sched *sched) { diff --git a/drivers/gpu/drm/nouveau/nouveau_sched.h b/drivers/gpu/drm/nouv= eau/nouveau_sched.h index 20cd1da8db73..e6e2016a3569 100644 --- a/drivers/gpu/drm/nouveau/nouveau_sched.h +++ b/drivers/gpu/drm/nouveau/nouveau_sched.h @@ -9,6 +9,7 @@ #include =20 #include "nouveau_drv.h" +#include "nouveau_chan.h" =20 #define to_nouveau_job(sched_job) \ container_of((sched_job), struct nouveau_job, base) @@ -101,6 +102,7 @@ struct nouveau_sched { struct drm_sched_entity entity; struct workqueue_struct *wq; struct mutex mutex; + struct nouveau_channel *chan; =20 struct { struct { @@ -112,6 +114,7 @@ struct nouveau_sched { }; =20 int nouveau_sched_create(struct nouveau_sched **psched, struct nouveau_drm= *drm, + struct nouveau_channel *chan, struct workqueue_struct *wq, u32 credit_limit); void nouveau_sched_destroy(struct nouveau_sched **psched); =20 --=20 2.48.1 From nobody Tue Dec 16 23:43:32 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 143281FF7C5 for ; Thu, 24 Apr 2025 09:56:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745488581; cv=none; b=Z0l8jw+Dc06aiiHS9TIfaCBEJPplvKc1AMvBDR69JxMpjqZCeUZiD/bzGgt5ioKP3+ITP1WyNMGiIAv12ktEK3nhp92io3ARzkoIWxW6pEnDzyMB44kplcdhQV6t1BCj/x7nts60OXKv31FKUjYaD1yL5rPyUfGzM7Lkh9Yl4DA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745488581; c=relaxed/simple; bh=P28JeNDdfIsHs0Yhi2or+iZJOlxhsSMOqlD2e6764qc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Xp1geFgMt2g9JKIaxX/EpMFUIwJdoiCvFN/WGo7JMppDkRpgAHpGaaGTCC9JbvnZQkjOAUEnjRzUXCzI2bnUjXwwFThwo7Mr13cVNUW8jYdHt5EyOeLf5i6eSFZQjktopv0ayz1yN/dkZ1lvIpbJuTkKJAmAlGXmWKYPlJDnOp0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=VmCYyOkX; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="VmCYyOkX" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3B119C4CEEE; Thu, 24 Apr 2025 09:56:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1745488578; bh=P28JeNDdfIsHs0Yhi2or+iZJOlxhsSMOqlD2e6764qc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VmCYyOkXDb9BTTFQYQ09JnGdXSChMtGo7+dFoeXkGx16HUmP1AcbZXmHnVOZZ+MpQ Hk00VDKKRZwdeDLoXxLh4gZ6Z0sirAxfnSmGsUTOltia8oGt7Al91tvWR3yLhdyoPB bPWCuvIG4z78YOI+CwJySmoCAg5V24K0MqqZVoZLTHXCj1mn3XOhXhiznX50JlWeNf 8kOiDXP5yjNiEQUpYrd1TEQCANq+JE2+T7KmpR+3+l6JudlsHTJ+0TlXYowWQImibT 3T7mjHlyLQ9zIEydPZqiPZCQ2hz9q6aLn9H0ulifxyBx5MRf3kLPrCusP2+/SUUYjK LgkQEt0LeFRlw== From: Philipp Stanner To: Lyude Paul , Danilo Krummrich , David Airlie , Simona Vetter , Matthew Brost , Philipp Stanner , =?UTF-8?q?Christian=20K=C3=B6nig?= , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , Tvrtko Ursulin Cc: dri-devel@lists.freedesktop.org, nouveau@lists.freedesktop.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 5/6] drm/nouveau: Remove waitque for sched teardown Date: Thu, 24 Apr 2025 11:55:35 +0200 Message-ID: <20250424095535.26119-7-phasta@kernel.org> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250424095535.26119-2-phasta@kernel.org> References: <20250424095535.26119-2-phasta@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" struct nouveau_sched contains a waitque needed to prevent drm_sched_fini() from being called while there are still jobs pending. Doing so so far would have caused memory leaks. With the new memleak-free mode of operation switched on in drm_sched_fini() by providing the callback nouveau_sched_fence_context_kill() the waitque is not necessary anymore. Remove the waitque. Signed-off-by: Philipp Stanner --- drivers/gpu/drm/nouveau/nouveau_sched.c | 20 +++++++------------- drivers/gpu/drm/nouveau/nouveau_sched.h | 9 +++------ drivers/gpu/drm/nouveau/nouveau_uvmm.c | 8 ++++---- 3 files changed, 14 insertions(+), 23 deletions(-) diff --git a/drivers/gpu/drm/nouveau/nouveau_sched.c b/drivers/gpu/drm/nouv= eau/nouveau_sched.c index 3659ac78bb3e..d9ac76198616 100644 --- a/drivers/gpu/drm/nouveau/nouveau_sched.c +++ b/drivers/gpu/drm/nouveau/nouveau_sched.c @@ -121,11 +121,9 @@ nouveau_job_done(struct nouveau_job *job) { struct nouveau_sched *sched =3D job->sched; =20 - spin_lock(&sched->job.list.lock); + spin_lock(&sched->job_list.lock); list_del(&job->entry); - spin_unlock(&sched->job.list.lock); - - wake_up(&sched->job.wq); + spin_unlock(&sched->job_list.lock); } =20 void @@ -306,9 +304,9 @@ nouveau_job_submit(struct nouveau_job *job) } =20 /* Submit was successful; add the job to the schedulers job list. */ - spin_lock(&sched->job.list.lock); - list_add(&job->entry, &sched->job.list.head); - spin_unlock(&sched->job.list.lock); + spin_lock(&sched->job_list.lock); + list_add(&job->entry, &sched->job_list.head); + spin_unlock(&sched->job_list.lock); =20 drm_sched_job_arm(&job->base); job->done_fence =3D dma_fence_get(&job->base.s_fence->finished); @@ -458,9 +456,8 @@ nouveau_sched_init(struct nouveau_sched *sched, struct = nouveau_drm *drm, goto fail_sched; =20 mutex_init(&sched->mutex); - spin_lock_init(&sched->job.list.lock); - INIT_LIST_HEAD(&sched->job.list.head); - init_waitqueue_head(&sched->job.wq); + spin_lock_init(&sched->job_list.lock); + INIT_LIST_HEAD(&sched->job_list.head); =20 return 0; =20 @@ -503,9 +500,6 @@ nouveau_sched_fini(struct nouveau_sched *sched) struct drm_gpu_scheduler *drm_sched =3D &sched->base; struct drm_sched_entity *entity =3D &sched->entity; =20 - rmb(); /* for list_empty to work without lock */ - wait_event(sched->job.wq, list_empty(&sched->job.list.head)); - drm_sched_entity_fini(entity); drm_sched_fini(drm_sched); =20 diff --git a/drivers/gpu/drm/nouveau/nouveau_sched.h b/drivers/gpu/drm/nouv= eau/nouveau_sched.h index e6e2016a3569..339a14563fbb 100644 --- a/drivers/gpu/drm/nouveau/nouveau_sched.h +++ b/drivers/gpu/drm/nouveau/nouveau_sched.h @@ -105,12 +105,9 @@ struct nouveau_sched { struct nouveau_channel *chan; =20 struct { - struct { - struct list_head head; - spinlock_t lock; - } list; - struct wait_queue_head wq; - } job; + struct list_head head; + spinlock_t lock; + } job_list; }; =20 int nouveau_sched_create(struct nouveau_sched **psched, struct nouveau_drm= *drm, diff --git a/drivers/gpu/drm/nouveau/nouveau_uvmm.c b/drivers/gpu/drm/nouve= au/nouveau_uvmm.c index 48f105239f42..ddfc46bc1b3e 100644 --- a/drivers/gpu/drm/nouveau/nouveau_uvmm.c +++ b/drivers/gpu/drm/nouveau/nouveau_uvmm.c @@ -1019,8 +1019,8 @@ bind_validate_map_sparse(struct nouveau_job *job, u64= addr, u64 range) u64 end =3D addr + range; =20 again: - spin_lock(&sched->job.list.lock); - list_for_each_entry(__job, &sched->job.list.head, entry) { + spin_lock(&sched->job_list.lock); + list_for_each_entry(__job, &sched->job_list.head, entry) { struct nouveau_uvmm_bind_job *bind_job =3D to_uvmm_bind_job(__job); =20 list_for_each_op(op, &bind_job->ops) { @@ -1030,7 +1030,7 @@ bind_validate_map_sparse(struct nouveau_job *job, u64= addr, u64 range) =20 if (!(end <=3D op_addr || addr >=3D op_end)) { nouveau_uvmm_bind_job_get(bind_job); - spin_unlock(&sched->job.list.lock); + spin_unlock(&sched->job_list.lock); wait_for_completion(&bind_job->complete); nouveau_uvmm_bind_job_put(bind_job); goto again; @@ -1038,7 +1038,7 @@ bind_validate_map_sparse(struct nouveau_job *job, u64= addr, u64 range) } } } - spin_unlock(&sched->job.list.lock); + spin_unlock(&sched->job_list.lock); } =20 static int --=20 2.48.1 From nobody Tue Dec 16 23:43:32 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CD399F510 for ; Thu, 24 Apr 2025 09:56:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745488582; cv=none; b=FZTl4EmNhzXhsRo9AZu4L6E4EkTSiGEFVXYMawYIXgbcjxOv46Us50t0iKXKQiT6Vom1nRG7JyOFLw2rFrML6c0J3TPSiT2puaSsVohmKYdSHRx5fUS0QkYi+AG443MmiHj5QQsg4zynXWqlGXfL1cDnNF/lh3iYgbX00if1B4A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745488582; c=relaxed/simple; bh=uDucjiIXIOMk0SOi1/h4I9CU9DMx2BoeK9EwYcIl2RM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Cv/WnQKbGiW1bUwDgo/Mwp7v/pCftVrEZurgXBu79e2Z1Jp4bWpHiYmFE/cmdgNz2te64AvbJJtnasD+JuX+iROHvgPOX4LZ3gW7N9AkVK4CYHVnPVb4u5h4EYU8YZJcLxMzNIC7B9nREcwelQRkRuJDQ9lC6WE4jWNqKrXnYzs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DN2jpb84; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="DN2jpb84" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 064FEC4CEE3; Thu, 24 Apr 2025 09:56:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1745488582; bh=uDucjiIXIOMk0SOi1/h4I9CU9DMx2BoeK9EwYcIl2RM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DN2jpb848NX1mc1DlP4cENi4/aCFYjMEzfMDn1yADdNEPdNZzIl0ydEC44VOiWyt3 UprMnYdrorHMepPujLGTXPkhYBIebs8nqrZ1hQ5rzd1H2iFQyFn3+57vVCYScj3tDF FsgpPKWd449+d4YAmDZOZNkJ//6C+OtXZL/ILWxk6DbSh4BBywxLWD6NyehM+Ou6X7 f9R0/bfaY/DRvdVsIJX1NSl3q4XscsN1OCsVew8bTL3Q51+wxDOzPaUCM6jM26bxCT 5Aa35NnVmwb1tXE4Di/viPPHiPIOAbRxDjJFV5YCNySdhrJvMJtcm33VqB4sB1IVZB 0l6N+Dgf5LDCA== From: Philipp Stanner To: Lyude Paul , Danilo Krummrich , David Airlie , Simona Vetter , Matthew Brost , Philipp Stanner , =?UTF-8?q?Christian=20K=C3=B6nig?= , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , Tvrtko Ursulin Cc: dri-devel@lists.freedesktop.org, nouveau@lists.freedesktop.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 6/6] drm/sched: Port unit tests to new cleanup design Date: Thu, 24 Apr 2025 11:55:36 +0200 Message-ID: <20250424095535.26119-8-phasta@kernel.org> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250424095535.26119-2-phasta@kernel.org> References: <20250424095535.26119-2-phasta@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The unit tests so far took care manually of avoiding memory leaks that might have occurred when calling drm_sched_fini(). The scheduler now takes care by itself of avoiding memory leaks if the driver provides the callback drm_sched_backend_ops.kill_fence_context(). Implement that callback for the unit tests. Remove the manual cleanup code. Signed-off-by: Philipp Stanner --- .../gpu/drm/scheduler/tests/mock_scheduler.c | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/drivers/gpu/drm/scheduler/tests/mock_scheduler.c b/drivers/gpu= /drm/scheduler/tests/mock_scheduler.c index f999c8859cf7..a72d26ca8262 100644 --- a/drivers/gpu/drm/scheduler/tests/mock_scheduler.c +++ b/drivers/gpu/drm/scheduler/tests/mock_scheduler.c @@ -228,10 +228,30 @@ static void mock_sched_free_job(struct drm_sched_job = *sched_job) /* Mock job itself is freed by the kunit framework. */ } =20 +static void mock_sched_fence_context_kill(struct drm_gpu_scheduler *gpu_sc= hed) +{ + struct drm_mock_scheduler *sched =3D drm_sched_to_mock_sched(gpu_sched); + struct drm_mock_sched_job *job; + unsigned long flags; + + spin_lock_irqsave(&sched->lock, flags); + list_for_each_entry(job, &sched->job_list, link) { + spin_lock(&job->lock); + if (!dma_fence_is_signaled_locked(&job->hw_fence)) { + dma_fence_set_error(&job->hw_fence, -ECANCELED); + dma_fence_signal_locked(&job->hw_fence); + } + complete(&job->done); + spin_unlock(&job->lock); + } + spin_unlock_irqrestore(&sched->lock, flags); +} + static const struct drm_sched_backend_ops drm_mock_scheduler_ops =3D { .run_job =3D mock_sched_run_job, .timedout_job =3D mock_sched_timedout_job, - .free_job =3D mock_sched_free_job + .free_job =3D mock_sched_free_job, + .kill_fence_context =3D mock_sched_fence_context_kill, }; =20 /** @@ -300,18 +320,6 @@ void drm_mock_sched_fini(struct drm_mock_scheduler *sc= hed) drm_mock_sched_job_complete(job); spin_unlock_irqrestore(&sched->lock, flags); =20 - /* - * Free completed jobs and jobs not yet processed by the DRM scheduler - * free worker. - */ - spin_lock_irqsave(&sched->lock, flags); - list_for_each_entry_safe(job, next, &sched->done_list, link) - list_move_tail(&job->link, &list); - spin_unlock_irqrestore(&sched->lock, flags); - - list_for_each_entry_safe(job, next, &list, link) - mock_sched_free_job(&job->base); - drm_sched_fini(&sched->base); } =20 --=20 2.48.1