mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Hang Zhengyang <hang.zhengyang1010@gmail.com>
To: Tejun Heo <tj@kernel.org>, David Vernet <void@manifault.com>,
	Andrea Righi <arighi@nvidia.com>,
	Changwoo Min <changwoo@igalia.com>,
	Ingo Molnar <mingo@redhat.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Juri Lelli <juri.lelli@redhat.com>,
	Vincent Guittot <vincent.guittot@linaro.org>,
	Dietmar Eggemann <dietmar.eggemann@arm.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	Ben Segall <bsegall@google.com>, Mel Gorman <mgorman@suse.de>,
	Valentin Schneider <vschneid@redhat.com>,
	K Prateek Nayak <kprateek.nayak@amd.com>
Cc: Cheng-Yang Chou <yphbchou0911@gmail.com>,
	Barret Rhoden <brho@google.com>, Josh Don <joshdon@google.com>,
	Hao Luo <haoluo@google.com>,
	sched-ext@lists.linux.dev, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org
Subject: [PATCH sched_ext/for-7.4-fixes 1/2] sched_ext: Don't enable or disable a task being forked on class changes
Date: Fri,  4 Dec 2026 01:27:43 +0000	[thread overview]
Message-ID: <20261204012744.195571-2-hang.zhengyang1010@gmail.com> (raw)
In-Reply-To: <20261204012744.195571-1-hang.zhengyang1010@gmail.com>

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


  reply	other threads:[~2026-12-04  1:27 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204012744.195571-2-hang.zhengyang1010@gmail.com \
    --to=hang.zhengyang1010@gmail.com \
    --cc=arighi@nvidia.com \
    --cc=brho@google.com \
    --cc=bsegall@google.com \
    --cc=changwoo@igalia.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=haoluo@google.com \
    --cc=joshdon@google.com \
    --cc=juri.lelli@redhat.com \
    --cc=kprateek.nayak@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=sched-ext@lists.linux.dev \
    --cc=stable@vger.kernel.org \
    --cc=tj@kernel.org \
    --cc=vincent.guittot@linaro.org \
    --cc=void@manifault.com \
    --cc=vschneid@redhat.com \
    --cc=yphbchou0911@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®