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=1796344914; b=2iXXRcPn1Pm+oCM6QTzzNsN1+JU9XEPw9DC41mcLQiRvJDnbcoAYMp0L8eEcqpwIpbbK zzbDPNDLTZOuKdPtwFqn0H2F/AKrRk3MhiWpSyAii+LgBzGhnuKusJQv5g5AjU8+NGpLd ZdbhHqVAiZo3BkY0zo/er+8lM74jsE5KKxp83qHmaoyISpWf8JFbAChp8OYAIpOVqdxDc 6NOVul1qSq2GbPC20d3oQ8dVAi0bEK5KjRYfYBE7MnrWPt7mDSuSmCgVQE9FucGT8JaiE 59lkZYytYUJLx+rqhPTQCPceDvVaaFZJkL8e2ISWn5USa5hOy0HGZoU9j+dys60v4Wg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796344914; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Type: Content-Transfer-Encoding; bh=t2Kl82nl/oCaMS7tNWeq5Q7ozpLUZH75NiTHFher+nU=; b=ioNOU1K/WPLVo7bIdclj9Mqr9eE2AClMbcr4Fy6dcJlLmMqv/upHeudaRr2JzSSfe0li DzIdu1uDIMUDjg6k7xdQJDzjQCw9wAyayaUDmyiVSXJ3GEizHSd85RmASabduWygF1rsl J8gylZLQwUWf2xyopnjzrRawd10W/wjBZZq8PzT/GDyfckXaRk39XIJCBxPov6r3BioDU PF+RaGE03fw0ott1l3zlpQElKHjypn7mwA9peFEmwYcoXroNdULv3ZxP7WsX/96OoNgKl fTVUmo/JAPNu/YUcWq7ObQ2AHWi570u+PWIff7OP2rH4JctJDIYwYDYHBFJhjZ2gIxQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=NpBaksKR; 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 9E87E606A32C for ; Fri, 4 Dec 2026 00:41:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=NpBaksKR Received: by smtp.kernel.org (Postfix) with ESMTPSA id 10EFC1F000C2; Fri, 4 Dec 2026 00:41:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796344914; bh=t2Kl82nl/oCaMS7tNWeq5Q7ozpLUZH75NiTHFher+nU=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=NpBaksKR2ZwhGGuqmCwJpr0aSkSupmvWBKG0AiK1+B+RDQLTo3D6w/vvvD9DUns4L mR8OX5ZFDQFT1lng7u+rkcpmOcbqHERUTL3npCeyUrC6DjFlBDvJjKQW81//YXij7/ D8MfoTxQ2MXuSPCTSx/HTnnkmtwZJsh747o4eAVi3EvQSGzNxscQ/GVXoHythlau25 lwjlx4UQQmLJQNRqF++h4qAlGJmEx5DaONexpfUPfOLZgRv48TT0bLZRZmaUOVKczm 4XERJk/iRt/JfFQ3y37BdwrjtQUUU6nJ3Af51uhVUTnW/2NyG9yWoo11ZE9BB0fATa Dqa24KNYQhacA== From: Sasha Levin To: stable@vger.kernel.org Cc: 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.12.y] drm/sched: Ensure stopped entity cannot be re-added to the run-queue Date: Thu, 3 Dec 2026 19:41:51 -0500 Message-ID: <20261204004043.1502027-1-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <2026120219-mulled-sheep-f77e@gregkh> References: <2026120219-mulled-sheep-f77e@gregkh> 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 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 stopped-entity guard from drm_sched_rq_pop_entity() in sched_rq.c to the FIFO re-add in drm_sched_entity_pop_job() in sched_entity.c, because the former doesn't exist here. ] Signed-off-by: Sasha Levin --- drivers/gpu/drm/scheduler/sched_entity.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/scheduler/sched_entity.c b/drivers/gpu/drm/scheduler/sched_entity.c index 45f22452a295f..5830efcb4c732 100644 --- a/drivers/gpu/drm/scheduler/sched_entity.c +++ b/drivers/gpu/drm/scheduler/sched_entity.c @@ -512,8 +512,14 @@ struct drm_sched_job *drm_sched_entity_pop_job(struct drm_sched_entity *entity) next = to_drm_sched_job(spsc_queue_peek(&entity->job_queue)); if (next) { spin_lock(&entity->lock); - drm_sched_rq_update_fifo_locked(entity, - next->submit_ts); + /* + * 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) + drm_sched_rq_update_fifo_locked(entity, + next->submit_ts); spin_unlock(&entity->lock); } } -- 2.53.0