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=1796394895; b=4zFPUsNyAtt/rNmkHW8XRd2pHOv7Fj8rZpp1Xqeb0tWxirerT6vXSERqjFRlg9AarAIi FhFocFCrW/igRlrA7IxX8or+/1bExJRp6T1x3KT/OMJRWYSttFWy0XnuHeJGuzovK3Go/ h0fpwFn2L34ml27SVpUs8F44kHR0LJdeWyP9nTM9knEjVRdNahnqyVjGPZVoS7+2jsGTD sIedP4+/Nyneb3REm/3l/Yp5HsFTxAnozFxQNY1DEO+01NbIt4QJ9Xq7lEDRDCEOaoQgv Vpq3eZS5gGaW7H733DhU+pTxi+sPkbmRZE4PwbiRfUA6JnGVp+LMGIB14CDs2d7udxg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796394895; h=Received:DKIM-Signature:Received:Received:X-ME-Sender: X-ME-Proxy-Cause:X-ME-Proxy:Feedback-ID:Received:X-Mailer: MIME-Version:X-ThreadId:Date:From:To:Cc:Message-Id:Subject: Content-Type:Content-Transfer-Encoding; bh=1xoYOq6ROA+bxH1+Pzc95HmMuoy4BZDRRWxX9H88FZE=; b=s4DHoYhe0aMzxXffqqzxtDzqELkCVP2b5u4xzSQCskrgFAV+/B/+ZJp/kD/d2m1GEMim 8K/0nMwHixor8IvMdX7e7BA0oe+HQphc3CV6gnhjLjhxzdJbno3jmG6BLjMHk1vc/0HfH /qhKj+bOCgt1aSUBpmuP+x7N0xtSyZXklFxBVEFfzsuA1eqTPDJgUigPASg/U4dffRgyE z1jbjT3+wAtlecCEOJYK/K1xRSU0nMmkcM9DgBOCpiUBPyC8tUj/KcOMdn4XtE5QpHEJv /bmrVte86ihq/T5R1ididC+ILkFvJmBiAvTvWfTXjhCbO8N5fO4EZ8tFbQsLXN3E2Ww== 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=iFhVyaSB; 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 BA154606A32C; Fri, 4 Dec 2026 14:34:55 +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=iFhVyaSB Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4E12F1F000C2; Fri, 4 Dec 2026 14:34:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796394895; bh=1xoYOq6ROA+bxH1+Pzc95HmMuoy4BZDRRWxX9H88FZE=; h=Date:From:To:Cc:Subject; b=iFhVyaSBwWfu9I3h5TqxaJGvc2RVXCmFHrRlseYuzn86AtjnzioRZHgpY6GcDUXKN /IctY8puZD9Qb19pOSUGuqHoMnpCuMJiJHo1sYApUn9OncjzBQaD+7VldXGN7zwgxo abbJXa5JeJzD6HZFwmyF4OEp8dN7dtTtFCSgOoP6JoFMvZHj/gxzABhL/apx+i6Zn8 ywRijE/NdmSNi84dxuagGDjTZERCPzGdK8CPA05Ugshb2YKst2zSzah+HwjkImbfQX XRyJQzNVdxNs7GwZa3jNOP2O6UooyTcI2xZn3pcJY1lzrfQUkTw1W65t0hFm8HpIiM M/JZY0xDSvqXw== Received: from stl-compute-04.internal (stl-compute-04.internal [10.204.2.64]) by mailfauth.stl.internal (Postfix) with ESMTP id B6B68780043; Fri, 4 Dec 2026 09:34:54 -0500 (EST) Received: from stl-imap-13 ([10.204.2.104]) by stl-compute-04.internal (MEProxy); Fri, 04 Dec 2026 09:34:54 -0500 X-ME-Sender: X-ME-Proxy-Cause: dmFkZTGU+j8kO7p8+syuXHFPGUu/tBkuuxcAWgJhD4aLlXqaxTinlOjZHUH8Gr6cZUje0Q OihL5tIz9lj1scWoK28sPur+XYkX01uq0h4BNYTig8RPXYcX1b2RTkfbQ5Qu2RRTPBqkSP cRcBM79+07YxAuRFxmPEOA8aMucS+yxDwbKTalT95/ei0ryivl3k/Sg7eOXj7ODJ/xCay6 GvOZqEz+Su8YHFchk0I5f6YKRFbZwA3tNGR8gViJf5D+E2alFEneIxaoKdrylQIYyFSmuQ J2HC4HNsyZ9BdfhWhvNs3kDUh66BDZWyy/qccBPDt65MZzEgvtTzisv48gthqNNnqwkukY ViPstHF+qCAZU65aLwHiwszsi2/buv9earQpVrUgBHx/tChOTADx09hANCx+OwBtiZ9NeG w2Yr2PHuUp2tPhASQp+iA1HpcOgDUQWvaTjSw+kpLiKMLDDjZLUrH1RECj9qqfwo7oS+t0 EDQRQ7ZHGqKmv2IyPaD9onZbZ9sSQOWl/TV7WEoKV+SgPM+3AMVc53x41k2hGIQYZCfYuw EBg/qswEnzhUNN/HTU+YovGDowXtblyFneU8aztjy2QwhYWKJAkvUfJTUS9e9D6Nhsy9+D Z4LaNYgcEdTsc/tn+Y6gk3yF+xs8yKxgEyhCfSY8TOBpynecFa7z98IQBa8Q X-ME-Proxy: Feedback-ID: iccae4b50:Fastmail Received: by mailuser.stl.internal (Postfix, from userid 501) id 767A5F6008B; Fri, 4 Dec 2026 09:34:54 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface 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 X-ThreadId: AlyfSAddWJyo Date: Fri, 04 Dec 2026 09:34:34 -0500 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 Message-Id: <20261204091840.14930-1-mason@kernel.org> Subject: [PATCH v2 sched_ext/for-7.4-fixes] sched_ext: Don't skip NONE tasks in sched_ext_dead() Content-Type: text/plain Content-Transfer-Encoding: 7bit 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) sched_ext_dead() also reads the state without the rq lock, so it can race with scx_rehome_task(), which transiently goes through NONE: scx_sub_disable(sub) sched_ext_dead(p) ==================== ================= task_rq_lock(p) scx_rehome_task(parent, p) scx_disable_and_exit_task() state = NONE state is NONE, skip p->scx.sched = parent The fix used here is to drop the skip, so that every dead task is taken off its sched and marked SCX_TASK_DEAD. The fast path for SCX_DISABLED is from Andrea Righi. 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. I had claude reproduce both paths with KASAN. The punted task hits the bug in 10 of 10 runs, and the race hits it in 8 of 3,168 tries. Neither does with this patch, and the sched_ext selftests pass. BUG: KASAN: slab-use-after-free in scx_vexit+0xe9/0x100 scx_vexit+0xe9/0x100 __scx_exit+0x9f/0xd0 scx_bpf_select_cpu_and+0x23e/0x2b0 bpf_prog_test_run_syscall+0x46a/0xa40 Freed by task 73: kfree+0x212/0x600 scx_sched_free_rcu_work+0xa60/0x1020 v1->v2: - Skip the rq lock while no BPF scheduler is loaded (Andrea) - Describe the race with scx_rehome_task() (Andrea) - Update the comments to match v1: https://lore.kernel.org/all/20261203202815.32456-1-mason@kernel.org/ kernel/sched/ext/ext.c | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c index efa1d01d03afa..e199bc54d6241 100644 --- a/kernel/sched/ext/ext.c +++ b/kernel/sched/ext/ext.c @@ -1014,8 +1014,9 @@ struct task_struct *scx_task_iter_next_locked(struct scx_task_iter *iter) * cgroup_task_dead() removes the dead tasks from cset->tasks * after sched_ext_dead() and cgroup iteration may see tasks * which already finished sched_ext_dead(). %SCX_TASK_DEAD is - * set by sched_ext_dead() under @p's rq lock. Test it to - * avoid visiting tasks which are already dead from SCX POV. + * set by sched_ext_dead() under @p's rq lock, or under + * scx_tasks_lock while disabled. Test it to avoid visiting + * tasks which are already dead from SCX POV. */ if (scx_get_task_state(p) == SCX_TASK_DEAD) { __scx_task_iter_rq_unlock(iter); @@ -4973,6 +4974,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() @@ -4985,6 +4989,16 @@ void sched_ext_dead(struct task_struct *p) rhashtable_remove_fast(&scx_tid_hash, &p->scx.tid_hash_node, scx_tid_hash_params); + + /* + * If disabled, @p is %NONE with no sched and, being off + * scx_tasks, can't be picked up by a racing root enable. Skip + * the rq lock. + */ + if (scx_enable_state() == SCX_DISABLED) { + scx_set_task_state(p, SCX_TASK_DEAD); + return; + } } /* @@ -4992,24 +5006,17 @@ 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(). * * %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