* [PATCH sched_ext/for-7.4-fixes v2 1/2] sched_ext: Don't enable or disable a task being forked on class changes
2026-12-05 3:04 [PATCH sched_ext/for-7.4-fixes v2 0/2] sched_ext: Fix two races between fork and scx_post_fork() Hang Zhengyang
@ 2026-12-05 3:04 ` Hang Zhengyang
2026-12-05 3:04 ` [PATCH sched_ext/for-7.4-fixes v2 2/2] sched_ext: Don't enable a task twice when it migrates during fork Hang Zhengyang
2026-12-05 7:01 ` [PATCH sched_ext/for-7.4-fixes v2 0/2] sched_ext: Fix two races between fork and scx_post_fork() Tejun Heo
2 siblings, 0 replies; 4+ messages in thread
From: Hang Zhengyang @ 2026-12-05 3:04 UTC (permalink / raw)
To: Tejun Heo, David Vernet, Andrea Righi, Changwoo Min, Ingo Molnar,
Peter Zijlstra, Juri Lelli, Vincent Guittot, Dietmar Eggemann,
Steven Rostedt, Ben Segall, Mel Gorman, Valentin Schneider,
K Prateek Nayak
Cc: Cheng-Yang Chou, Barret Rhoden, Josh Don, Hao Luo, sched-ext,
linux-kernel, stable
A new task is visible by PID before scx_post_fork() runs and stays in
SCX_TASK_INIT until then. If another task moves it to or off the ext
class with sched_setscheduler() in that window, switching_to_scx()
enables it or switched_from_scx() disables it, and scx_post_fork() then
makes its own transition. ops.enable() runs twice, or ops.disable() runs
for a task that was never enabled, and the state check warns:
[ 8.241338] sched_ext: Invalid task state transition 0x4000000 -> 0x8000000 for scxfk[266]
Leave such a task to scx_post_fork() in both callbacks, and make
scx_post_fork() set READY and check the class under the rq lock that
the callbacks hold.
Fixes: f0e1a0643a59 ("sched_ext: Implement BPF extensible scheduler class")
Cc: stable@vger.kernel.org # v6.12+
Signed-off-by: Hang Zhengyang <hang.zhengyang1010@gmail.com>
---
kernel/sched/ext/ext.c | 48 +++++++++++++++++++++++++++++-------------
1 file changed, 33 insertions(+), 15 deletions(-)
diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
index 3bfeb35b002b..4750b73c2e59 100644
--- a/kernel/sched/ext/ext.c
+++ b/kernel/sched/ext/ext.c
@@ -4963,21 +4963,23 @@ int scx_fork(struct task_struct *p, struct kernel_clone_args *kargs)
void scx_post_fork(struct task_struct *p)
{
if (scx_init_task_enabled) {
- scx_set_task_state(p, SCX_TASK_READY);
+ struct rq_flags rf;
+ struct rq *rq;
/*
- * Enable the task immediately if it's running on sched_ext.
- * Otherwise, it'll be enabled in switching_to_scx() if and
- * when it's ever configured to run with a SCHED_EXT policy.
+ * Enable @p now if it is on the ext class. Otherwise,
+ * switching_to_scx() enables it if it ever switches over.
+ *
+ * A class switch before this point leaves enabling @p to us.
+ * Set %READY and test the class under the rq lock it holds, so
+ * that it can't run between the two and enable @p twice or not
+ * at all.
*/
- if (p->sched_class == &ext_sched_class) {
- struct rq_flags rf;
- struct rq *rq;
-
- rq = task_rq_lock(p, &rf);
+ rq = task_rq_lock(p, &rf);
+ scx_set_task_state(p, SCX_TASK_READY);
+ if (p->sched_class == &ext_sched_class)
scx_enable_task(scx_task_sched(p), p);
- task_rq_unlock(rq, p, &rf);
- }
+ task_rq_unlock(rq, p, &rf);
}
scoped_guard(raw_spinlock_irq, &scx_tasks_lock) {
@@ -5114,7 +5116,15 @@ static void switching_to_scx(struct rq *rq, struct task_struct *p)
if (task_dead_and_done(p))
return;
- scx_enable_task(sch, p);
+ /*
+ * A task being forked is %INIT until scx_post_fork(), which enables it
+ * if it is on the ext class by then. Enabling it here as well would
+ * call ops.enable() twice. Root enable and re-homing take other tasks
+ * through %INIT without dropping the rq lock, so no other task can be
+ * %INIT here.
+ */
+ if (scx_get_task_state(p) != SCX_TASK_INIT)
+ scx_enable_task(sch, p);
/*
* set_cpus_allowed_scx() is not called while @p is associated with a
@@ -5127,6 +5137,8 @@ static void switching_to_scx(struct rq *rq, struct task_struct *p)
static void switched_from_scx(struct rq *rq, struct task_struct *p)
{
+ u32 state;
+
/*
* A class change of the running task: sched_change_begin() put @p with
* no successor, which leaves rq->scx.sched set.
@@ -5144,10 +5156,16 @@ static void switched_from_scx(struct rq *rq, struct task_struct *p)
* scx_disable_task() would WARN on the non-%ENABLED state and trigger a
* NONE -> READY validation failure.
*
- * Either way, reset @p after ops.disable() has seen the final values. A
- * %NONE @p can still be a rescuee under the parent.
+ * %INIT means @p is being forked and hasn't been enabled.
+ * scx_disable_task() would WARN and call ops.disable() without a
+ * matching ops.enable(). scx_post_fork() will find @p off the ext class
+ * and leave it %READY.
+ *
+ * In all cases, reset @p after ops.disable() has seen the final values.
+ * A %NONE @p can still be a rescuee under the parent.
*/
- if (scx_get_task_state(p) != SCX_TASK_NONE)
+ state = scx_get_task_state(p);
+ if (state != SCX_TASK_NONE && state != SCX_TASK_INIT)
scx_disable_task(scx_task_sched(p), p);
scx_reset_task_fields(rq, p);
}
--
2.53.0
^ permalink raw reply related [flat|nested] 4+ messages in thread* [PATCH sched_ext/for-7.4-fixes v2 2/2] sched_ext: Don't enable a task twice when it migrates during fork
2026-12-05 3:04 [PATCH sched_ext/for-7.4-fixes v2 0/2] sched_ext: Fix two races between fork and scx_post_fork() Hang Zhengyang
2026-12-05 3:04 ` [PATCH sched_ext/for-7.4-fixes v2 1/2] sched_ext: Don't enable or disable a task being forked on class changes Hang Zhengyang
@ 2026-12-05 3:04 ` Hang Zhengyang
2026-12-05 7:01 ` [PATCH sched_ext/for-7.4-fixes v2 0/2] sched_ext: Fix two races between fork and scx_post_fork() Tejun Heo
2 siblings, 0 replies; 4+ messages in thread
From: Hang Zhengyang @ 2026-12-05 3:04 UTC (permalink / raw)
To: Tejun Heo, David Vernet, Andrea Righi, Changwoo Min, Ingo Molnar,
Peter Zijlstra, Juri Lelli, Vincent Guittot, Dietmar Eggemann,
Steven Rostedt, Ben Segall, Mel Gorman, Valentin Schneider,
K Prateek Nayak
Cc: Cheng-Yang Chou, Barret Rhoden, Josh Don, Hao Luo, sched-ext,
linux-kernel
A cgroup.procs write can migrate a new task between cgroup_post_fork()
and scx_post_fork(), while it is still in SCX_TASK_INIT. If the new
cgroup belongs to another scheduler, scx_rehome_task() sets the task
READY there and, on the ext class, enables it. scx_post_fork() then does
the same again, so ops.enable() runs twice, or the state check warns
about READY -> READY.
Leave such a task at INIT in scx_rehome_task() and let scx_post_fork()
finish it under the rq lock, as the previous patch made it do.
Fixes: bf9dee58ab56 ("sched_ext: Re-home tasks on cgroup migration")
Signed-off-by: Hang Zhengyang <hang.zhengyang1010@gmail.com>
---
kernel/sched/ext/ext.c | 8 ++++----
kernel/sched/ext/sub.c | 27 ++++++++++++++++++++-------
2 files changed, 24 insertions(+), 11 deletions(-)
diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
index 4750b73c2e59..2fed78e6928e 100644
--- a/kernel/sched/ext/ext.c
+++ b/kernel/sched/ext/ext.c
@@ -4970,10 +4970,10 @@ void scx_post_fork(struct task_struct *p)
* Enable @p now if it is on the ext class. Otherwise,
* switching_to_scx() enables it if it ever switches over.
*
- * A class switch before this point leaves enabling @p to us.
- * Set %READY and test the class under the rq lock it holds, so
- * that it can't run between the two and enable @p twice or not
- * at all.
+ * Only we move @p from %INIT to %READY. Until then, a class
+ * switch or a re-home leaves enabling @p to us. Set %READY and
+ * test the class under the rq lock they hold, so that neither
+ * can run between the two and enable @p twice or not at all.
*/
rq = task_rq_lock(p, &rf);
scx_set_task_state(p, SCX_TASK_READY);
diff --git a/kernel/sched/ext/sub.c b/kernel/sched/ext/sub.c
index 7c069a289766..ac4743e5f610 100644
--- a/kernel/sched/ext/sub.c
+++ b/kernel/sched/ext/sub.c
@@ -1638,16 +1638,27 @@ void drain_descendants(struct scx_sched *sch)
* @to: sched taking over @p, @p's init on it already complete
* @p: task to re-home
*
- * Exit @p from its current sched and switch it over to @to, overriding the
- * state to %SCX_TASK_READY to account for the already completed init. A task
- * on a non-ext class, possible under an %SCX_OPS_SWITCH_PARTIAL root, stays
- * %READY and is enabled by switching_to_scx() if it switches over.
+ * Exit @p from its current sched and switch it over to @to, normally overriding
+ * the state to %SCX_TASK_READY to account for the already completed init. A
+ * task on a non-ext class, possible under an %SCX_OPS_SWITCH_PARTIAL root,
+ * stays %READY and is enabled by switching_to_scx() if it switches over.
+ *
+ * The exception is a task still being forked, which a cgroup migration can
+ * reach between cgroup_post_fork() and scx_post_fork(), at %SCX_TASK_INIT.
+ * scx_post_fork() makes @p %READY on @to and enables it if it is on the ext
+ * class, so leave @p at %INIT. Making @p %READY here too would enable it twice
+ * or trigger a READY -> READY validation failure.
*/
static void scx_rehome_task(struct scx_sched *to, struct task_struct *p)
{
+ u32 state;
+
lockdep_assert_held(&p->pi_lock);
lockdep_assert_rq_held(task_rq(p));
+ /* the exit and re-init below overwrite it, read it first */
+ state = scx_get_task_state(p);
+
scx_prepare_task_sched_change(p);
scoped_guard (sched_change, p,
DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK) {
@@ -1655,9 +1666,11 @@ static void scx_rehome_task(struct scx_sched *to, struct task_struct *p)
scx_set_task_state(p, SCX_TASK_INIT_BEGIN);
scx_set_task_state(p, SCX_TASK_INIT);
scx_set_task_sched(p, to);
- scx_set_task_state(p, SCX_TASK_READY);
- if (p->sched_class == &ext_sched_class)
- scx_enable_task(to, p);
+ if (state != SCX_TASK_INIT) {
+ scx_set_task_state(p, SCX_TASK_READY);
+ if (p->sched_class == &ext_sched_class)
+ scx_enable_task(to, p);
+ }
}
}
--
2.53.0
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH sched_ext/for-7.4-fixes v2 0/2] sched_ext: Fix two races between fork and scx_post_fork()
2026-12-05 3:04 [PATCH sched_ext/for-7.4-fixes v2 0/2] sched_ext: Fix two races between fork and scx_post_fork() Hang Zhengyang
2026-12-05 3:04 ` [PATCH sched_ext/for-7.4-fixes v2 1/2] sched_ext: Don't enable or disable a task being forked on class changes Hang Zhengyang
2026-12-05 3:04 ` [PATCH sched_ext/for-7.4-fixes v2 2/2] sched_ext: Don't enable a task twice when it migrates during fork Hang Zhengyang
@ 2026-12-05 7:01 ` Tejun Heo
2 siblings, 0 replies; 4+ messages in thread
From: Tejun Heo @ 2026-12-05 7:01 UTC (permalink / raw)
To: Hang Zhengyang
Cc: David Vernet, Andrea Righi, Changwoo Min, Ingo Molnar,
Peter Zijlstra, Juri Lelli, Vincent Guittot, Dietmar Eggemann,
Steven Rostedt, Ben Segall, Mel Gorman, Valentin Schneider,
K Prateek Nayak, Cheng-Yang Chou, Barret Rhoden, Josh Don,
Hao Luo, sched-ext, linux-kernel
> Hang Zhengyang (2):
> sched_ext: Don't enable or disable a task being forked on class
> changes
> sched_ext: Don't enable a task twice when it migrates during fork
Applied 1-2 to sched_ext/for-7.4-fixes with the following tag added to 2/2:
Cc: stable@vger.kernel.org # v7.3+
Thanks.
--
tejun
^ permalink raw reply [flat|nested] 4+ messages in thread