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
Subject: [PATCH sched_ext/for-7.4-fixes v2 2/2] sched_ext: Don't enable a task twice when it migrates during fork
Date: Sat,  5 Dec 2026 03:04:27 +0000	[thread overview]
Message-ID: <20261205030427.933980-3-hang.zhengyang1010@gmail.com> (raw)
In-Reply-To: <20261205030427.933980-1-hang.zhengyang1010@gmail.com>

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


  parent reply	other threads:[~2026-12-05  3:04 UTC|newest]

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

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=20261205030427.933980-3-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=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®