mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH sched_ext/for-7.4-fixes 0/2] sched_ext: Fix two races between fork and scx_post_fork()
@ 2026-12-04  1:27 Hang Zhengyang
  2026-12-04  1:27 ` [PATCH sched_ext/for-7.4-fixes 1/2] sched_ext: Don't enable or disable a task being forked on class changes Hang Zhengyang
  2026-12-04  1:27 ` [PATCH sched_ext/for-7.4-fixes 2/2] sched_ext: Don't enable a task twice when it migrates during fork Hang Zhengyang
  0 siblings, 2 replies; 3+ messages in thread
From: Hang Zhengyang @ 2026-12-04  1:27 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

Before scx_post_fork() runs, a new task can be reached by
sched_setscheduler() (patch 1, since v6.12) and by a cgroup migration to
another sub-scheduler (patch 2). Either can make ops.enable() run twice
or trip the task state check.

Tested on for-7.4-fixes with KASAN and lockdep. With a debug-only delay
before scx_post_fork(), all seven class change and migration cases warn
or enable tasks twice before and are clean after. Without the delay, 700
seconds of migrations enable 38 of 46133 tasks twice before and 0 of
44149 after. The sched_ext selftests pass.

Andrea Righi's pending "sched: Let switching_to() decide whether to keep
a retained proxy donor" makes switching_to_scx() return bool, so patch 1
will need a return value there.

I used Claude Code to find the issues, write the patches and run the
tests. I reviewed the patches.

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

 kernel/sched/ext/ext.c | 32 +++++++++++++++++++++-----------
 kernel/sched/ext/sub.c | 26 +++++++++++++++++++-------
 2 files changed, 40 insertions(+), 18 deletions(-)


base-commit: b2b011d3d9cd7e01308ba8fa5775dcd1f2f11f70
-- 
2.53.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH sched_ext/for-7.4-fixes 1/2] sched_ext: Don't enable or disable a task being forked on class changes
  2026-12-04  1:27 [PATCH sched_ext/for-7.4-fixes 0/2] sched_ext: Fix two races between fork and scx_post_fork() Hang Zhengyang
@ 2026-12-04  1:27 ` Hang Zhengyang
  2026-12-04  1:27 ` [PATCH sched_ext/for-7.4-fixes 2/2] sched_ext: Don't enable a task twice when it migrates during fork Hang Zhengyang
  1 sibling, 0 replies; 3+ messages in thread
From: Hang Zhengyang @ 2026-12-04  1:27 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 | 32 +++++++++++++++++++++-----------
 1 file changed, 21 insertions(+), 11 deletions(-)

diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
index 4139b90d37fb..95149321b18f 100644
--- a/kernel/sched/ext/ext.c
+++ b/kernel/sched/ext/ext.c
@@ -4959,6 +4959,11 @@ 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) {
+		struct rq_flags rf;
+		struct rq *rq;
+
+		/* synchronize against switching_to_scx() and switched_from_scx() */
+		rq = task_rq_lock(p, &rf);
 		scx_set_task_state(p, SCX_TASK_READY);
 
 		/*
@@ -4966,14 +4971,9 @@ void scx_post_fork(struct task_struct *p)
 		 * Otherwise, it'll be enabled in switching_to_scx() if and
 		 * when it's ever configured to run with a SCHED_EXT policy.
 		 */
-		if (p->sched_class == &ext_sched_class) {
-			struct rq_flags rf;
-			struct rq *rq;
-
-			rq = task_rq_lock(p, &rf);
+		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) {
@@ -5107,7 +5107,13 @@ static void switching_to_scx(struct rq *rq, struct task_struct *p)
 	if (task_dead_and_done(p))
 		return;
 
-	scx_enable_task(sch, p);
+	/*
+	 * Only a task being forked can be %INIT here. Enabling the root and
+	 * re-homing take a task from %INIT to %READY under the rq lock held
+	 * here. scx_post_fork() enables a task being forked.
+	 */
+	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
@@ -5137,10 +5143,14 @@ 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 yet.
+	 * scx_post_fork() leaves it %READY if it is no longer on the ext class.
+	 *
+	 * 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)
+	if (scx_get_task_state(p) != SCX_TASK_NONE &&
+	    scx_get_task_state(p) != SCX_TASK_INIT)
 		scx_disable_task(scx_task_sched(p), p);
 	scx_reset_task_fields(rq, p);
 }
-- 
2.53.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH sched_ext/for-7.4-fixes 2/2] sched_ext: Don't enable a task twice when it migrates during fork
  2026-12-04  1:27 [PATCH sched_ext/for-7.4-fixes 0/2] sched_ext: Fix two races between fork and scx_post_fork() Hang Zhengyang
  2026-12-04  1:27 ` [PATCH sched_ext/for-7.4-fixes 1/2] sched_ext: Don't enable or disable a task being forked on class changes Hang Zhengyang
@ 2026-12-04  1:27 ` Hang Zhengyang
  1 sibling, 0 replies; 3+ messages in thread
From: Hang Zhengyang @ 2026-12-04  1:27 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 |  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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-04  1:28 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-04  1:27 [PATCH sched_ext/for-7.4-fixes 0/2] sched_ext: Fix two races between fork and scx_post_fork() Hang Zhengyang
2026-12-04  1:27 ` [PATCH sched_ext/for-7.4-fixes 1/2] sched_ext: Don't enable or disable a task being forked on class changes Hang Zhengyang
2026-12-04  1:27 ` [PATCH sched_ext/for-7.4-fixes 2/2] sched_ext: Don't enable a task twice when it migrates during fork Hang Zhengyang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®