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=1796344348; b=VI2fCzeckZJUyVFdNWLAn6rhQgGlNRbY5YJ5pVS4kFjW0FTAnCwec+NQo1IEai3sJyo6 EfFgf30onH5xbiU2FDhOJZ/+HIpvtra8m6yOAbYv649A+VvMl0UIhzynxy91084QFq7Pj GB9mfLYau/3LLWfCYB+eXdUhZGHEOG575hXNX4tAVAatTsj64gxJ/YkQNj34Af3dUg9d/ /jGdDRvGDddCO3m2qf457WXxuiYhGxIAcAukcJQ1ol6pGHJyuo3ShSHkOWCK3COn66z6Q kEct9pR5O+wq0LzxGzBYw4+c13jZ0RzvhfIXbl1q0DINYrwmIGQ6aTjB3o9U1HgZwfQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796344348; 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=rbw/TlYqQxg1ROIdvrnCqNrdhMzMOzQQybim/SjsLhw=; b=yzFRDtpUA6lIha6XUc3HGNket6YhbdGfxMZKSGkY4U55Fufwhi+3ePmZOyFDRcep8Yx1 2lAVBcdmmkiMNq3atmHa0/KE2ou8ULz7QSQT17Mzqeq7hZWBtMKsUVKH7iKcRxJBq+FZj GZwpCEGkOsuU3cTdQuNJHot4Egyjok1uV88l5rANVuTRsD0sFh8uJzFMPpapMzhw5rCDj kyTJXopeKOWljeSqbd9q62InZhj7FHnJnKXbYsPyx44d+MqzquAFyoVAhOEpzEyJkPqIK 9f9hCyrtlEA0KX2AWk1dRJNOFNVY8jr2aZHJeEFDYt+H9rtNukVbcFWXGN5VuX31CaQ== 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=BQfDl9dQ; 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 308AE606A322 for ; Fri, 4 Dec 2026 00:32:27 +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=BQfDl9dQ Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0F6D01F000C2; Fri, 4 Dec 2026 00:32:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796344347; bh=rbw/TlYqQxg1ROIdvrnCqNrdhMzMOzQQybim/SjsLhw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=BQfDl9dQ4Z6UJb8oi/1dAZL8vjKoKXXIcG1qAnLesWTUV0eVghjvpaCfgjqKqW81C SozovBNZiWzNHuguvwtlQaKeyfCOFyDJL3lgZFIlk8F/FxloWvudwJPI7c+RQXKTbw c7V0xvXSGEj2VifKXwAb5xgZA3YgtjAIa3zattToMw8ffK5TfCxfxV3XFQHpMDv3Cn 4hZuQFpCZg+/zyeLlB+HagZ0mxS9wpdRQwPXSjZJxEiG7cGFDbk6CDW3APLaSGKixg l2hLOw47TxXiPrYdzsbTbsZ3yfBiw0Gyu413DXVXzEwoi+kyjjp6JwEqK80WpERfnm GbpbK9TgD2Nww== 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.18.y] drm/sched: Ensure stopped entity cannot be re-added to the run-queue Date: Thu, 3 Dec 2026 19:32:24 -0500 Message-ID: <20261204003135.1471506-1-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <2026120218-politely-chain-ca03@gregkh> References: <2026120218-politely-chain-ca03@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 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 --- drivers/gpu/drm/scheduler/sched_entity.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/drivers/gpu/drm/scheduler/sched_entity.c b/drivers/gpu/drm/scheduler/sched_entity.c index fe174a4857be7..ca7c010697678 100644 --- 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_pop_job(struct drm_sched_entity *entity) 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); } } -- 2.53.0