From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796383006; b=AZYCY8MdQMsh+pKL7pPFH4BZ5qZy+7cJAc5DMEl2T49N9fjGbZP1oCc0Hx7Bn6oFfNu4 21BUbr/oj8Yb2laHRFobFNrP6mpyXrvi6Y+ZGzb8cEhVy9eITL7TvR1HtvUycgfxB3/qS Q0/l91yUIp6quJ3IY6oTGk8h51es7Nt30Y/3xaADhQxkqrRAS6liGLM5vQNccjYtAX0rQ JXa0rnY3xd0o8NDo8DModfONG6UG98HQIZ2pWNSuCpvqXoKrbbkUWIjDOW8l4R0tD6ujp ZxrKLAUIZFUrScb5eiH21vpiw6xjw7krcm+ntqbqiPlBO9364EbZfn9ZnGLsA9A9zUw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796383006; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Type:Content-Transfer-Encoding; bh=PCyaAonPsuDISqi0cUNjlLZ6pWax4SdeDqDUpXGbQsc=; b=MGy3fhbpfssZF4wR55zcuo7OSgUm6s7spJ9/NE9+zgYe4suzWflDK9I5JaW34kqnjdx9 1LrEfGB8NIdOnHgnP58P5AsOTnKy/ulPHx2IITnntFnUjAqS9fB++xzmpEiffmonfx0UU AjzYVOYp5856vETDEDjrEM5/ynudc3pBwt7smJ5J5b+PDTcSyDouhJJx5Xvq0rQFnPRUg fpFtzXoR/N25OoE8EC4p/yVsSKa/lC0mwQft5g48y7pgkLnB+XNWs2ag24YIg1CKQ+PEP XgVphmdsIjKxYKjXN+wSmjpqVMbE7Jhol+fv8gPeDBvhBMFIc5aikuvPeHCJC6hos6g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=feiYtOdb; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (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 4AC0E606A322; Fri, 4 Dec 2026 11:16:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=feiYtOdb Received: by smtp.kernel.org (Postfix) with ESMTPSA id B20121F000C2; Fri, 4 Dec 2026 11:16:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796383006; bh=PCyaAonPsuDISqi0cUNjlLZ6pWax4SdeDqDUpXGbQsc=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=feiYtOdb8Ui4YhGWcfXRLmAplpVEamPu8leoosiKtThxvorCnfKaCyot6UDlYVXYD +PdA8Rk04RjCLrs4KKgKYxKyCEoQ7ZFDfgeIaDtFvQbm4C+yfVJMLgdWMueWAoMB/Y N0uI5BB141W1BOCGyDrDbImfpnuAX28rpx0axht8= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Tvrtko Ursulin , Luben Tuikov , =?UTF-8?q?Christian=20K=C3=B6nig?= , Andrey Grodzovsky , Matthew Brost , Danilo Krummrich , Philipp Stanner , David Airlie , Simona Vetter , Sasha Levin Subject: [PATCH 6.18 688/703] drm/sched: Ensure stopped entity cannot be re-added to the run-queue Date: Fri, 4 Dec 2026 19:09:47 +0900 Message-ID: <20261204100646.135764435@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100627.522266145@linuxfoundation.org> References: <20261204100627.522266145@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore 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=UTF-8 Content-Transfer-Encoding: 8bit 6.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Tvrtko Ursulin [ Upstream commit 7e2645d5c05ac03f240685fe76a2c04733d36a1a ] When entities are torn down in drm_sched_entity_kill() a multi-step process is undertaken to synchronize with a drm_sched_run_job_work() consuming jobs from the same entity. First the entity is marked as stopped and removed from the run-queue, with both the entity and run-queue lock correctly taken. The process then waits for the entity idle via the completion after which point it assumes it is the only accessor to the entity in question. What can happen however in drm_sched_run_job_work() if there are at least two outstanding runnable jobs on an entity, is that drm_sched_entity_pop_job() can re-add it to the run-queue due the unconditial add to the rbtree via drm_sched_rq_update_fifo_locked(). What is required is that the teardown starts after the run job has selected the entity and that it gets to it's completion waiting stage before the run job goes to dequeue a job, and for the run job to be able to de-queue a job before teardown has consumed too many. Run job will then see there is another job pending (after the one about to be de-queued) and it will re-add the entity to the run-queue. Run job will then signal the completion and entity teardown will proceed incorrectly thinking it's removal of the entity was final. End result is a pointer to an entity about to be freed dangling forever in the rbtree. drm_sched_run_job_work() drm_sched_entity_kill() drm_sched_select_entity stops and removes entity wait_for_completion drm_sched_entity_pop_job -> re-adds to run-queue due more jobs complete_all drm_sched_entity_queue_pop ... fini entity ... drm_sched_run_job_queue drm_sched_select_entity - UAF This race existed ever since the FIFO policy was first added. Lets mirror the guard from drm_sched_rq_add_entity() in drm_sched_rq_pop_entity() and close this gap. Signed-off-by: Tvrtko Ursulin References: https://sashiko.dev/#/patchset/20261020154123.33631-1-tvrtko.ursulin@igalia.com?part=7 Fixes: 08fb97de03aa ("drm/sched: Add FIFO sched policy to run queue") Cc: Luben Tuikov Cc: Christian König Cc: Andrey Grodzovsky Cc: Matthew Brost Cc: Danilo Krummrich Cc: Philipp Stanner Cc: David Airlie Cc: Simona Vetter Cc: stable@vger.kernel.org # v6.2+ Signed-off-by: Philipp Stanner Link: https://patch.msgid.link/20261109160743.17020-1-tvrtko.ursulin@igalia.com [ sashal: Moved the entity->stopped guard into drm_sched_entity_pop_job() because drm_sched_rq_pop_entity() and sched_rq.c don't exist here, wrapping the rq lock and FIFO update in an if. ] Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- drivers/gpu/drm/scheduler/sched_entity.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) --- a/drivers/gpu/drm/scheduler/sched_entity.c +++ b/drivers/gpu/drm/scheduler/sched_entity.c @@ -504,11 +504,18 @@ struct drm_sched_job *drm_sched_entity_p struct drm_sched_rq *rq; spin_lock(&entity->lock); - rq = entity->rq; - spin_lock(&rq->lock); - drm_sched_rq_update_fifo_locked(entity, rq, - next->submit_ts); - spin_unlock(&rq->lock); + /* + * If entity is getting torn down via + * drm_sched_entity_kill() we need to ensure it does + * not get re-added to the tree behind it's back. + */ + if (!entity->stopped) { + rq = entity->rq; + spin_lock(&rq->lock); + drm_sched_rq_update_fifo_locked(entity, rq, + next->submit_ts); + spin_unlock(&rq->lock); + } spin_unlock(&entity->lock); } }