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=1796329697; b=mbW5Wpwld1kuF3qtNBjZsVEx9GcEeH6Kq2ugscs3ICQKsWhwDDzDB+YSu2n0yVEkH7lq zFoTpsmo5tnOtDD6ohlvbr3BdW4Hi8GeCusrtAK0Rqh4i1DSPCJM5qjd/hSZiJSUwvUNp jWZ/udWGtOkn3xe9LgE/DGyOHHfaCIt+HmGKZxbJEFCUsCmdqNfinrU/ArVGisydplI7F tHkbL04cKjhA82PU+/3GzJRBEHMTBGzCmVCGRZdRhc8/a4EMA9AWknqIAI6Q4ELzmRQfU jho3kuuWEQoK+nSYwaWb0FXBKQWwtD1ZsXLwr4tdUoo/VLMoTEThM/O5SBq6fu7n8Hw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796329697; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: MIME-Version:Content-Transfer-Encoding; bh=JcPeSRTdB3S7WaPD4s89+FaZpbt/VIo0vMzyPPJ0QVM=; b=QCzJkZMYopSwVdtKvVYJFMhrbYG0mQuiEDNFOTMCw6PhheBd8/vrha3z9fddZzlcFqCv nVeVAxF4+8x3b05pY5+k4dOnCZIRYzfGAvQ+UzZnp3O+KDiIXwg9JPPcOTX2XHAtH+IXF DI4Ti++y8KSHmCdxSvDh4zZ0htq0MpzEEKqXXvu5NXxTaloc804peNJIVFknmRqtiL9OK v4nIXKxHl5bWeA1NZRQFauADYmfzcvQs1XwwzwPYY6D6x+xjgvE3mg1q1do5t8kIWhH43 7wQiXyUgJdqtY69RUZZ4rR3nsWjzLPuFLDSlVljuRSyDhASmHgLwdFg4RUmFKTD+NUA== 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=knQj3SkU; 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 D9455606A322; Thu, 3 Dec 2026 20:28:17 +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=knQj3SkU Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9A0581F000C2; Thu, 3 Dec 2026 20:28:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796329697; bh=JcPeSRTdB3S7WaPD4s89+FaZpbt/VIo0vMzyPPJ0QVM=; h=From:To:Cc:Subject:Date; b=knQj3SkUcO7+mpx5tlOAi+JbE4N5wz32nPMQhE+7xapT6sQWNStfwA5kYPOXQhfMG aFWHJp5O+cO9HIILNSGuO/yFjkU4xrlnb0b5MOA0Ee7LK1FQZlYApdijxcxt76FuLz jKM+Cvis8qQXLysPPsLHWeLLX4CdPLVWaK+qQ+rIzhhN6FIuCCtCsFTA1jO6ew70a+ I9FvnwIuPwXIxztVOdpQvmhlaIRP7lY22G85JgoTtpxxhFEAjmPiFlFsm7i5ENFVD5 NFRhLMEBKoRZ51I2DhRVokC1wS5rdcj7qvoyiMhyk+u0iTxZbryssVp5oK37VFM+W4 zz2tt7Pch4FbA== From: Chris Mason To: tj@kernel.org, void@manifault.com, arighi@nvidia.com, changwoo@igalia.com, sched-ext@lists.linux.dev Cc: linux-kernel@vger.kernel.org, mason@kernel.org Subject: [PATCH sched_ext/for-7.4-fixes] sched_ext: Don't skip NONE tasks in sched_ext_dead() Date: Thu, 3 Dec 2026 15:28:15 -0500 Message-ID: <20261203202815.32456-1-mason@kernel.org> X-Mailer: git-send-email 2.55.0 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit sched_ext_dead() leaves p->scx.sched set if the task was punted by scx_fail_parent(), which leads to a use-after-free of the parent sched. Tasks hold no reference on their sched, so p->scx.sched has to be cleared before the sched is freed. sched_ext_dead() doesn't do that for a task at SCX_TASK_NONE, and scx_punt_task() leaves a live task at NONE with p->scx.sched set: scx_sub_disable(sub) __scx_init_task(parent) fails scx_fail_parent() scx_punt_task(parent, p) NONE, p->scx.sched = parent sched_ext_dead(p) list_del_init() p is off scx_tasks state is NONE p->scx.sched stays scx_root_disable(parent) scx_task_iter_next_locked() doesn't visit p scx_sched_free_rcu_work() kfree(parent) scx_bpf_select_cpu_and(p) a new root has a sub-sched scx_error(p->scx.sched) The fix used here is to drop the skip, so that every dead task is taken off its sched and marked SCX_TASK_DEAD. Fixes: 337ec00b1d9c ("sched_ext: Implement cgroup sub-sched enabling and disabling") Signed-off-by: Chris Mason --- This is an AI generated fix, on top of v7.4-rc5. It makes every exiting task take its rq lock in sched_ext_dead(), even without a BPF scheduler loaded. I haven't benchmarked that. kernel/sched/ext/ext.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c index efa1d01d03afa..fd3d3d49be145 100644 --- a/kernel/sched/ext/ext.c +++ b/kernel/sched/ext/ext.c @@ -4973,6 +4973,9 @@ static bool task_dead_and_done(struct task_struct *p) void sched_ext_dead(struct task_struct *p) { + struct rq_flags rf; + struct rq *rq; + /* * By the time control reaches here, @p has %TASK_DEAD set, switched out * for the last time and then dropped the rq lock - task_dead_and_done() @@ -4992,24 +4995,19 @@ void sched_ext_dead(struct task_struct *p) * ENABLED transitions can't race us. Disable ops for @p. * * %SCX_TASK_DEAD synchronizes against cgroup task iteration - see - * scx_task_iter_next_locked(). NONE tasks need no marking: cgroup - * iteration is only used from sub-sched paths, which require root - * enabled. Root enable transitions every live task to at least READY. + * scx_task_iter_next_locked(). NONE tasks go through this too: the + * sub-sched paths change the state under @p's rq lock, and + * scx_punt_task() leaves a live task at NONE with its sched set. * * %INIT_BEGIN means ops.init_task() is running for @p. Don't call * into ops; transition to %DEAD so the post-init recheck unwinds * via scx_sub_init_cancel_task(). */ - if (scx_get_task_state(p) != SCX_TASK_NONE) { - struct rq_flags rf; - struct rq *rq; - - rq = task_rq_lock(p, &rf); - if (scx_get_task_state(p) != SCX_TASK_INIT_BEGIN) - scx_disable_and_exit_task(scx_task_sched(p), p); - scx_set_task_state(p, SCX_TASK_DEAD); - task_rq_unlock(rq, p, &rf); - } + rq = task_rq_lock(p, &rf); + if (scx_get_task_state(p) != SCX_TASK_INIT_BEGIN) + scx_disable_and_exit_task(scx_task_sched(p), p); + scx_set_task_state(p, SCX_TASK_DEAD); + task_rq_unlock(rq, p, &rf); } static void reweight_task_scx(struct rq *rq, struct task_struct *p, base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244 -- 2.55.0