[PATCH sched_ext/for-7.4-fixes 2/2] sched_ext: Don't enable a task twice when it migrates during fork

From: Hang Zhengyang

Date: Thu Dec 03 2026 - 20:28:13 EST


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@xxxxxxxxx>
---
kernel/sched/ext/ext.c | 2 +-
kernel/sched/ext/sub.c | 26 +++++++++++++++++++-------
2 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
index 95149321b18f..ce05a0a10585 100644
--- a/kernel/sched/ext/ext.c
+++ b/kernel/sched/ext/ext.c
@@ -4962,7 +4962,7 @@ void scx_post_fork(struct task_struct *p)
struct rq_flags rf;
struct rq *rq;

- /* synchronize against switching_to_scx() and switched_from_scx() */
+ /* synchronize against the class switches and scx_rehome_task() */
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..83e76acd0add 100644
--- a/kernel/sched/ext/sub.c
+++ b/kernel/sched/ext/sub.c
@@ -1638,16 +1638,26 @@ 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. Unless @p is still
+ * being forked, override 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.
+ *
+ * A cgroup migration can reach @p after cgroup_post_fork() and before
+ * scx_post_fork(). Such @p is still %SCX_TASK_INIT. Leave it at %INIT on @to
+ * and let scx_post_fork() make it %READY and enable it. scx_post_fork() does
+ * that under the rq lock, which is held here too.
*/
static void scx_rehome_task(struct scx_sched *to, struct task_struct *p)
{
+ bool forking;
+
lockdep_assert_held(&p->pi_lock);
lockdep_assert_rq_held(task_rq(p));

+ forking = scx_get_task_state(p) == SCX_TASK_INIT;
+
scx_prepare_task_sched_change(p);
scoped_guard (sched_change, p,
DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK) {
@@ -1655,9 +1665,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 (!forking) {
+ scx_set_task_state(p, SCX_TASK_READY);
+ if (p->sched_class == &ext_sched_class)
+ scx_enable_task(to, p);
+ }
}
}

--
2.53.0