From patchwork Mon Mar 24 18:57:25 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 14027711 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 1836FC36008 for ; Mon, 24 Mar 2025 18:59:35 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 525F910E336; Mon, 24 Mar 2025 18:59:32 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.b="CR0Mq8QL"; dkim-atps=neutral Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by gabe.freedesktop.org (Postfix) with ESMTPS id 189EE10E05E; Mon, 24 Mar 2025 18:59:31 +0000 (UTC) Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sea.source.kernel.org (Postfix) with ESMTP id 56E7444759; Mon, 24 Mar 2025 18:59:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 55A27C4CEED; Mon, 24 Mar 2025 18:59:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1742842770; bh=DNWUEy0AwswO/8irYrxLdpr2c6TuEDEyDdVtB7RXVL8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CR0Mq8QLqbfCYz+/LXSBKbKnfDEE36w5PrRJLTDBQ4ppYa3NS52Dn9DqqAk7tTzOg t0CUrvaLV4BiUVuQwASFQlGwB+hHVru8JeR5vu0Srq6w54sQLD7mBQ8G3D+X9QmCyl XL7XTxVLNMQU4W8wApu1qVt1fy6ZryiCwui5iykKYwRVCEKpUgFWQwCrWIoRPQbEh1 WF5S+UCxw2UNo5YGsYnSfeuMASvABFMc9FZZ9c1F8scwS3k1v2q4zsnnQukypP6+tm jwsEBqmF6VFjartvvNRK1S3mvnbn/hHa0UhM9ACJvxWwQqyRqDO0AxMCrC+/se+/w9 fTjCbqwssdnsg== From: Philipp Stanner To: Lyude Paul , Danilo Krummrich , David Airlie , Simona Vetter , Matthew Brost , Philipp Stanner , =?utf-8?q?Christian_K=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: [RFC PATCH 1/5] drm/sched: Fix teardown leaks with waitqueue Date: Mon, 24 Mar 2025 19:57:25 +0100 Message-ID: <20250324185728.45857-3-phasta@kernel.org> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250324185728.45857-2-phasta@kernel.org> References: <20250324185728.45857-2-phasta@kernel.org> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" 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 | 84 ++++++++++++++++++++------ include/drm/gpu_scheduler.h | 8 +++ 2 files changed, 75 insertions(+), 17 deletions(-) diff --git a/drivers/gpu/drm/scheduler/sched_main.c b/drivers/gpu/drm/scheduler/sched_main.c index 53e6aec37b46..c1ab48ef61cf 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_scheduler *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); } @@ -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); status = job->sched->ops->timedout_job(job); @@ -572,8 +573,10 @@ static void drm_sched_job_timedout(struct work_struct *work) spin_unlock(&sched->job_list_lock); } - if (status != DRM_GPU_SCHED_STAT_ENODEV) - drm_sched_start_timeout_unlocked(sched); + if (status != DRM_GPU_SCHED_STAT_ENODEV) { + if (!READ_ONCE(sched->cancel_timeout)) + drm_sched_start_timeout_unlocked(sched); + } } /** @@ -1109,12 +1112,22 @@ drm_sched_get_finished_job(struct drm_gpu_scheduler *sched) /* remove job from pending_list */ list_del_init(&job->list); + /* + * Inform tasks blocking in drm_sched_fini() that it's now safe to proceed. + */ + 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 */ next = list_first_entry_or_null(&sched->pending_list, typeof(*next), list); + // TODO RFC: above we wake up the waitque which relies on the fact + // that timeouts have been deactivated. The below should never + // reactivate them since the list was empty above. Still, should + // we document that? if (next) { if (test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &next->s_fence->scheduled.flags)) @@ -1314,6 +1327,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); + 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); @@ -1321,6 +1335,8 @@ int drm_sched_init(struct drm_gpu_scheduler *sched, const struct drm_sched_init_ atomic_set(&sched->_score, 0); atomic64_set(&sched->job_id_count, 0); sched->pause_submit = false; + sched->pause_free = false; + sched->cancel_timeout = false; sched->ready = true; return 0; @@ -1338,6 +1354,40 @@ int drm_sched_init(struct drm_gpu_scheduler *sched, const struct drm_sched_init_ } EXPORT_SYMBOL(drm_sched_init); +/** + * 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); + WRITE_ONCE(sched->cancel_timeout, 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 = list_empty(&sched->pending_list); + spin_unlock(&sched->job_list_lock); + + return empty; +} + /** * drm_sched_fini - Destroy a gpu scheduler * @@ -1345,26 +1395,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.free_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 to 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 called - * after drm_sched_fini() ran are freed manually. - * - * FIXME: Take care of the above problem and prevent this function from leaking - * 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; - 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); for (i = DRM_SCHED_PRIORITY_KERNEL; i < sched->num_rqs; i++) { struct drm_sched_rq *rq = sched->sched_rq[i]; @@ -1461,6 +1509,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); } @@ -1478,6 +1527,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..badcf9ea4501 100644 --- a/include/drm/gpu_scheduler.h +++ b/include/drm/gpu_scheduler.h @@ -29,6 +29,7 @@ #include #include #include +#include #define MAX_WAIT_SCHED_ENTITY_Q_EMPTY msecs_to_jiffies(1000) @@ -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 until all + * pending jobs have been finished. * @hang_limit: once the hangs by a job crosses this limit then it is marked * guilty and it will no longer be considered for scheduling. * @score: score to help loadbalancer pick a idle sched @@ -540,6 +543,8 @@ 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 + * @cancel_timeout: pause queuing of @work_tdr on @submit_wq * @own_submit_wq: scheduler owns allocation of @submit_wq * @dev: system &struct device * @@ -562,12 +567,15 @@ 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 cancel_timeout; bool own_submit_wq; struct device *dev; }; From patchwork Mon Mar 24 18:57:26 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 14027713 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D75A9C36002 for ; Mon, 24 Mar 2025 18:59:39 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 42A9F10E4B3; Mon, 24 Mar 2025 18:59:39 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.b="e182ef8e"; dkim-atps=neutral Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by gabe.freedesktop.org (Postfix) with ESMTPS id 0A98910E1C6; Mon, 24 Mar 2025 18:59:35 +0000 (UTC) Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sea.source.kernel.org (Postfix) with ESMTP id 9E22744777; Mon, 24 Mar 2025 18:59:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2B32CC4CEDD; Mon, 24 Mar 2025 18:59:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1742842774; bh=LNb2F2c8zejJK69+mIOoiRybsoWajBLMqCUkOY1Wo0Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=e182ef8ede+irYinDn6XTciN4Ex31PqrioQgd73OedFXW6CnCknDLmOJtTNynMg/e lTR0qjVFMhS0c4RyEJ/pw1KDZmmO7KVLwfUI6pPXyeGixfxYPvj6xpdMRJbV5lofJ0 seWPT6drG7wpXKqUQkiipOO6Qh4iTdvEe3HlkboCD9hRv922rGjMcFjg4xfW0L0VMu vKsuVcT7hzGJy1/Z6a5DIwBuGhRcPaQzXF62dCUwjPBueYBhSS7aJMh9oxhuVa6dmT bS8B+J/+aevKaDQpBcMhHYNO93SCgllyqcZsO2Hxn42rWx3fI5Ut+ZZ1Zmm1myFqbS 3hcrG/+F4jeyg== From: Philipp Stanner To: Lyude Paul , Danilo Krummrich , David Airlie , Simona Vetter , Matthew Brost , Philipp Stanner , =?utf-8?q?Christian_K=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: [RFC PATCH 2/5] drm/sched: Prevent teardown waitque from blocking too long Date: Mon, 24 Mar 2025 19:57:26 +0100 Message-ID: <20250324185728.45857-4-phasta@kernel.org> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250324185728.45857-2-phasta@kernel.org> References: <20250324185728.45857-2-phasta@kernel.org> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" 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/scheduler/sched_main.c index c1ab48ef61cf..87acbcb1a821 100644 --- a/drivers/gpu/drm/scheduler/sched_main.c +++ b/drivers/gpu/drm/scheduler/sched_main.c @@ -1388,31 +1388,46 @@ drm_sched_no_jobs_pending(struct drm_gpu_scheduler *sched) return empty; } +/** + * 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 scheduler, + * thereby signalling all pending fences. This, in turn, will trigger + * &struct drm_sched_backend_ops.free_job to be called for all pending jobs. + * 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; - /* - * 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); + } for (i = DRM_SCHED_PRIORITY_KERNEL; i < sched->num_rqs; i++) { struct drm_sched_rq *rq = sched->sched_rq[i]; @@ -1500,7 +1515,7 @@ bool drm_sched_wqueue_ready(struct drm_gpu_scheduler *sched) EXPORT_SYMBOL(drm_sched_wqueue_ready); /** - * 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 @@ -1516,7 +1531,7 @@ void drm_sched_wqueue_stop(struct drm_gpu_scheduler *sched) EXPORT_SYMBOL(drm_sched_wqueue_stop); /** - * 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 badcf9ea4501..e13eb7e4c93a 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 it. + */ + void (*kill_fence_context)(struct drm_gpu_scheduler *sched); }; /** From patchwork Mon Mar 24 18:57:27 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 14027715 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 962A2C36002 for ; Mon, 24 Mar 2025 18:59:46 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id F15AB10E4B9; Mon, 24 Mar 2025 18:59:45 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.b="L17Z8LtJ"; dkim-atps=neutral Received: from tor.source.kernel.org (tor.source.kernel.org [172.105.4.254]) by gabe.freedesktop.org (Postfix) with ESMTPS id BD13E10E4B7; Mon, 24 Mar 2025 18:59:39 +0000 (UTC) Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by tor.source.kernel.org (Postfix) with ESMTP id 07786615A4; Mon, 24 Mar 2025 18:59:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 58058C4CEEA; Mon, 24 Mar 2025 18:59:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1742842778; bh=xhoLbq5vLDpkF7vOhtPhJa9aRN77Uui3d6RuWBdXsI8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=L17Z8LtJUZIhDsROpLic9OKSKSKVsgxhl+h6ZaPjlrMntjZvf12luHuySfvdldIMb H9HIptsl/RAQq/iYWI1ASRh9cWHvJbTXgd9UdbZPehLyyH8mpR5gl0U6sSjpMZGM/m w4NkTMRsVgukL2Zb4ncQJZBs/SBT31o+6agUzejxxB8rAINQ+CeT9Gjj/wbF0AqkMV fdo778tK3SeJlaxLOZ8q70TLh//c9yBQv+mHXl/7iwppe0rVSfN5lRFcdOvI+bZEOI WPXjEg2U5EkAl2zXk9FWYiaVHL2ymoBRrQp6dsgWAamqK6WBP2VbkpM28uuaFGz8FA 0rFxiaBtXFpxA== From: Philipp Stanner To: Lyude Paul , Danilo Krummrich , David Airlie , Simona Vetter , Matthew Brost , Philipp Stanner , =?utf-8?q?Christian_K=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: [RFC PATCH 3/5] drm/sched: Warn if pending list is not empty Date: Mon, 24 Mar 2025 19:57:27 +0100 Message-ID: <20250324185728.45857-5-phasta@kernel.org> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250324185728.45857-2-phasta@kernel.org> References: <20250324185728.45857-2-phasta@kernel.org> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" 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 | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/gpu/drm/scheduler/sched_main.c b/drivers/gpu/drm/scheduler/sched_main.c index 87acbcb1a821..4f8cc208a231 100644 --- a/drivers/gpu/drm/scheduler/sched_main.c +++ b/drivers/gpu/drm/scheduler/sched_main.c @@ -1455,6 +1455,10 @@ void drm_sched_fini(struct drm_gpu_scheduler *sched) sched->ready = false; kfree(sched->sched_rq); sched->sched_rq = NULL; + + if (!list_empty(&sched->pending_list)) + dev_err(sched->dev, "%s: Tearing down scheduler while jobs are pending!\n", + __func__); } EXPORT_SYMBOL(drm_sched_fini); From patchwork Mon Mar 24 18:57:28 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 14027716 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 130D8C3600D for ; Mon, 24 Mar 2025 18:59:47 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 43F9B10E4BD; Mon, 24 Mar 2025 18:59:46 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.b="Q1/vRZKZ"; dkim-atps=neutral Received: from tor.source.kernel.org (tor.source.kernel.org [172.105.4.254]) by gabe.freedesktop.org (Postfix) with ESMTPS id 40D4310E4B7; Mon, 24 Mar 2025 18:59:43 +0000 (UTC) Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by tor.source.kernel.org (Postfix) with ESMTP id C6A2D6157F; Mon, 24 Mar 2025 18:59:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 24BB2C4CEDD; Mon, 24 Mar 2025 18:59:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1742842782; bh=NCVkBBLqtGEdnEQtLmXeFUs5qUUJkAaPVyk19HzOsBs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Q1/vRZKZ6irTIbINb2G8xvj5JvwxePGTa1IwANcWLuMQZSbp+VZk4sXBX2rpKtiHf jgueHIlr7xWMCCxp/L8Q4b2bR+ozy/Cx9atJwCnG2RknbUJ0UpRA/h5W64NWshr3gH B1stRIOEmM4x307ks17UFSNgJhkBwJluNve8dtevb2U28RMcT3/ljMsMYQeOI7iawV FGEpKtRUOAzBCQFT5LacXnrNmVn66zdnZtuo2rfau0sqoUgy4tfJXT0wywRYsS3sNq IMBMOYlNSPu0yYuGXR8m/a9uZ2CwTcwjeODNVpT2bFYm/HQ/DDSoQO5o9mTGQdXRpo oo4UcmTl7DLXw== From: Philipp Stanner To: Lyude Paul , Danilo Krummrich , David Airlie , Simona Vetter , Matthew Brost , Philipp Stanner , =?utf-8?q?Christian_K=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: [RFC PATCH 4/5] drm/nouveau: Add new callback for scheduler teardown Date: Mon, 24 Mar 2025 19:57:28 +0100 Message-ID: <20250324185728.45857-6-phasta@kernel.org> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250324185728.45857-2-phasta@kernel.org> References: <20250324185728.45857-2-phasta@kernel.org> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" 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/nouveau/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 = nouveau_sched_create(&chan->sched, drm, drm->sched_wq, - chan->chan->dma.ib_max); + ret = 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/nouveau/nouveau_drm.c index e154d08857c5..95d677f6eae9 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 *sname, * locks which indirectly or directly are held for allocations * elsewhere. */ - ret = nouveau_sched_create(&cli->sched, drm, NULL, 1); + ret = nouveau_sched_create(&cli->sched, drm, NULL, NULL, 1); if (ret) goto done; diff --git a/drivers/gpu/drm/nouveau/nouveau_sched.c b/drivers/gpu/drm/nouveau/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" #define NOUVEAU_SCHED_JOB_TIMEOUT_MS 10000 @@ -392,10 +393,22 @@ nouveau_sched_free_job(struct drm_sched_job *sched_job) nouveau_job_fini(job); } +static void +nouveau_sched_fence_context_kill(struct drm_gpu_scheduler *sched) +{ + struct nouveau_sched *nsched; + + nsched = 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 = { .run_job = nouveau_sched_run_job, .timedout_job = nouveau_sched_timedout_job, .free_job = nouveau_sched_free_job, + .kill_fence_context = nouveau_sched_fence_context_kill, }; static int @@ -461,7 +474,8 @@ nouveau_sched_init(struct nouveau_sched *sched, struct nouveau_drm *drm, int nouveau_sched_create(struct nouveau_sched **psched, struct nouveau_drm *drm, - 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, struct nouveau_drm *drm, if (!sched) return -ENOMEM; + sched->chan = chan; + ret = nouveau_sched_init(sched, drm, wq, credit_limit); if (ret) { kfree(sched); @@ -481,7 +497,6 @@ nouveau_sched_create(struct nouveau_sched **psched, struct nouveau_drm *drm, return 0; } - static void nouveau_sched_fini(struct nouveau_sched *sched) { diff --git a/drivers/gpu/drm/nouveau/nouveau_sched.h b/drivers/gpu/drm/nouveau/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 #include "nouveau_drv.h" +#include "nouveau_chan.h" #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; struct { struct { @@ -112,6 +114,7 @@ struct nouveau_sched { }; 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); From patchwork Mon Mar 24 18:57:29 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Stanner X-Patchwork-Id: 14027718 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id CE21BC36008 for ; Mon, 24 Mar 2025 18:59:53 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 3980010E4C4; Mon, 24 Mar 2025 18:59:53 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.b="hEI+S8ti"; dkim-atps=neutral Received: from tor.source.kernel.org (tor.source.kernel.org [172.105.4.254]) by gabe.freedesktop.org (Postfix) with ESMTPS id 1C89810E4C1; Mon, 24 Mar 2025 18:59:47 +0000 (UTC) Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by tor.source.kernel.org (Postfix) with ESMTP id 9C12B6158E; Mon, 24 Mar 2025 18:59:42 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E580EC4CEDD; Mon, 24 Mar 2025 18:59:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1742842786; bh=P28JeNDdfIsHs0Yhi2or+iZJOlxhsSMOqlD2e6764qc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hEI+S8tiXB6lfLuJVkI0sE26Hhm+jk4CtP8cYz87BVegLJIt0bT9M+kilzBThiz/G 0I2bW94NAHsXkC68F49mB7kWekK5GY7z4ejygaTPUUHReCk5hX33Z/uPhtENnLo4Pe UEZTPz1jqCC6X/bm06/8sTNQPbr4E6X/ac3uEYdfuS6mAbEaNRquBUCAsSio5ScyKb tQTCyqCBKWTrDanoXtuqvkYPL4wYUHJEGeRgJV7N8WNieaqmhCyfQl1RbDRWamVu+s bAozn0aavxXUo503y+LkELMhIjivJ07kptjOrxbJC/qDYBaYc1MSE4vsk4kMUVlf2/ ZxDQbaoiqxj6w== From: Philipp Stanner To: Lyude Paul , Danilo Krummrich , David Airlie , Simona Vetter , Matthew Brost , Philipp Stanner , =?utf-8?q?Christian_K=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: [RFC PATCH 5/5] drm/nouveau: Remove waitque for sched teardown Date: Mon, 24 Mar 2025 19:57:29 +0100 Message-ID: <20250324185728.45857-7-phasta@kernel.org> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250324185728.45857-2-phasta@kernel.org> References: <20250324185728.45857-2-phasta@kernel.org> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" 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/nouveau/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 = job->sched; - 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); } void @@ -306,9 +304,9 @@ nouveau_job_submit(struct nouveau_job *job) } /* 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); drm_sched_job_arm(&job->base); job->done_fence = 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; 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); return 0; @@ -503,9 +500,6 @@ nouveau_sched_fini(struct nouveau_sched *sched) struct drm_gpu_scheduler *drm_sched = &sched->base; struct drm_sched_entity *entity = &sched->entity; - 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); diff --git a/drivers/gpu/drm/nouveau/nouveau_sched.h b/drivers/gpu/drm/nouveau/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; 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; }; 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/nouveau/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 = addr + range; 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 = to_uvmm_bind_job(__job); list_for_each_op(op, &bind_job->ops) { @@ -1030,7 +1030,7 @@ bind_validate_map_sparse(struct nouveau_job *job, u64 addr, u64 range) if (!(end <= op_addr || addr >= 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); } static int