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=1796345494; b=sIyDy/1dQ5MPa0M41FkhEQOMRloN1256p3SfCNzYBZnlEYkehru117AEd5U1wNce7ekv 0nmyWL/m2TsXmz4BMrS+qA6f++F663avLJYOf7bML7pw5VBeoMgigMjy5QRDWMF/cdz2d 8zJVIK3ZRS59wCglL+8J3ah8QTNDfxatqD24D63iGNwpUj2I+6nMEmuqn/L9rHQOpVNB1 yrAOLGVvnTfq7Y8qZgzRssnEi050h84q+TUX4MKRJ+G9sUL2ursL94f/OWaUlvtzqugPC 3eHdwkI5fCclkev4TUrnKq6fSGYYejxRmGtIB7F4IT95Ly3sQG5+XQ46/3/RBDbKjzw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796345494; 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=UmK7Uau10m8hCW2khFmCjkUFFJIl3XVSqp0nJ98bJKQ=; b=rObk6XvrorcCEBNnKysdBzg6+f9pNi9iblUW2mOj0BNtUVlkoaRSnUdRyfmJ5bfcV9Lp o5FYsaWDJp+VfahsWppGK2CKD00p3NcaLlINNeDi0MSWnmduppMSNARBjhbFyXhDofnjw pUReMl15tn/Cenk9TMIuOeQU5HCPlgT3ZlPtiWj6JiAGy40tyXovagGTeHQ/Wni2xHzH/ cEM+wVeahTadcIZVnhs9PaE5Dmv6nlx+KSxJW5m27MG6viZpypNqvwwPk5+VHrHeEvKuC yemKxfyn3OckfAVzY16+EGLsJTPNUJKi8myr5BoUR0YsDQzlZay6JliscZkVtAJSp/w== 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=nL5JXv0S; 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 523D9606A322 for ; Fri, 4 Dec 2026 00:51:34 +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=nL5JXv0S Received: by smtp.kernel.org (Postfix) with ESMTPSA id D88501F00897; Fri, 4 Dec 2026 00:51:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796345494; bh=UmK7Uau10m8hCW2khFmCjkUFFJIl3XVSqp0nJ98bJKQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=nL5JXv0SOOb6vuSXwpbcHiAGhR4lHv6n5sCh2WzpTF1KlF9k9z1o6mT2SQ9vSzA6S TykR2dOHm+k+dJBX7NquP/FDhPQrwrgHJHvd05gbWqOiSJgli/EzZvHfT5obTGduf6 t9gxYzIpRFqOEx+HmzZiVC/X1HJFxqCmaipyqIdhl4Dr1HWL76xYh6HsZNmYimXhXS upTz02ixwsXdRY7qQC0wPiVWdocj2EiVCBFavRF5Fv6WYSrwn8t5JdeesATobDj2YE nZRwsnXPhPrXlb6ydXvGhTwy7iqG29EiKzfv1T0qgyq8zNHmjZ5m9/veNj5Xguau2e FWyu/K9MFNNPg== 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.6.y] drm/sched: Ensure stopped entity cannot be re-added to the run-queue Date: Thu, 3 Dec 2026 19:51:31 -0500 Message-ID: <20261204005046.1540895-1-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <2026120219-wackiness-tug-1d0d@gregkh> References: <2026120219-wackiness-tug-1d0d@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: Adapted the stopped-entity guard into drm_sched_rq_update_fifo() in sched_main.c, since the target function doesn't exist here, and added an unlock of rq_lock on the early return. ] Signed-off-by: Sasha Levin --- drivers/gpu/drm/scheduler/sched_main.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/drivers/gpu/drm/scheduler/sched_main.c b/drivers/gpu/drm/scheduler/sched_main.c index a18693303c221..8c0fb5fb2bcce 100644 --- a/drivers/gpu/drm/scheduler/sched_main.c +++ b/drivers/gpu/drm/scheduler/sched_main.c @@ -103,6 +103,16 @@ void drm_sched_rq_update_fifo(struct drm_sched_entity *entity, ktime_t ts) * other to update the rb tree structure. */ spin_lock(&entity->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) { + spin_unlock(&entity->rq_lock); + return; + } + spin_lock(&entity->rq->lock); drm_sched_rq_remove_fifo_locked(entity); -- 2.53.0