* [PATCH v2 sched_ext/for-7.4-fixes] sched_ext: Don't skip NONE tasks in sched_ext_dead()
@ 2026-12-04 14:34 Chris Mason
2026-12-04 15:37 ` Tejun Heo
0 siblings, 1 reply; 2+ messages in thread
From: Chris Mason @ 2026-12-04 14:34 UTC (permalink / raw)
To: tj, void, arighi, changwoo, sched-ext; +Cc: linux-kernel, mason
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 <mason@kernel.org>
---
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
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v2 sched_ext/for-7.4-fixes] sched_ext: Don't skip NONE tasks in sched_ext_dead()
2026-12-04 14:34 [PATCH v2 sched_ext/for-7.4-fixes] sched_ext: Don't skip NONE tasks in sched_ext_dead() Chris Mason
@ 2026-12-04 15:37 ` Tejun Heo
0 siblings, 0 replies; 2+ messages in thread
From: Tejun Heo @ 2026-12-04 15:37 UTC (permalink / raw)
To: Chris Mason; +Cc: void, arighi, changwoo, sched-ext, linux-kernel
Applied to sched_ext/for-7.4-fixes with the following changes:
- The rq locking in sched_ext_dead() converted to
scoped_guard(task_rq_lock).
- Cc: stable@vger.kernel.org # v7.1+ added.
--- a/kernel/sched/ext/ext.c
+++ b/kernel/sched/ext/ext.c
@@ -5032,9 +5032,6 @@ 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()
@@ -5070,11 +5067,11 @@ void sched_ext_dead(struct task_struct *p)
* into ops; transition to %DEAD so the post-init recheck unwinds
* via scx_sub_init_cancel_task().
*/
- 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);
+ scoped_guard(task_rq_lock, p) {
+ 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);
+ }
}
static void reweight_task_scx(struct rq *rq, struct task_struct *p,
Thanks.
--
tejun
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-04 15:37 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 14:34 [PATCH v2 sched_ext/for-7.4-fixes] sched_ext: Don't skip NONE tasks in sched_ext_dead() Chris Mason
2026-12-04 15:37 ` Tejun Heo
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox