From: Andrea Righi <arighi@nvidia.com>
To: Tejun Heo <tj@kernel.org>, David Vernet <void@manifault.com>,
Changwoo Min <changwoo@igalia.com>
Cc: Kuba Piecuch <jpiecuch@google.com>,
sched-ext@lists.linux.dev, linux-kernel@vger.kernel.org
Subject: [PATCH 1/2] sched_ext: Initialize idle masks before ops.init()
Date: Fri, 31 Jul 2026 10:59:48 +0200 [thread overview]
Message-ID: <20260731090334.2911948-2-arighi@nvidia.com> (raw)
In-Reply-To: <20260731090334.2911948-1-arighi@nvidia.com>
The built-in idle masks are reset with all online CPUs marked idle, but
idle state tracking starts only after the scheduler is fully enabled.
As a result, ops.init() can observe busy CPUs as idle, and those CPUs
remain incorrectly advertised until their next idle transition.
Enable idle tracking before ops.init() and refresh every online CPU under
its rq lock. Once a CPU is refreshed, later transitions keep its state
accurate. Keep ops.update_idle() notifications disabled until the
scheduler is fully enabled.
Suggested-by: Kuba Piecuch <jpiecuch@google.com>
Signed-off-by: Andrea Righi <arighi@nvidia.com>
---
kernel/sched/ext/ext.h | 4 +++-
kernel/sched/ext/idle.c | 38 ++++++++++++++++++++++++++++++++++++--
2 files changed, 39 insertions(+), 3 deletions(-)
diff --git a/kernel/sched/ext/ext.h b/kernel/sched/ext/ext.h
index 0b7fc46aee08c..495e7023c9ee1 100644
--- a/kernel/sched/ext/ext.h
+++ b/kernel/sched/ext/ext.h
@@ -59,11 +59,13 @@ static inline void init_sched_ext_class(void) {}
#endif /* CONFIG_SCHED_CLASS_EXT */
#ifdef CONFIG_SCHED_CLASS_EXT
+DECLARE_STATIC_KEY_FALSE(scx_idle_tracking_enabled);
+
void __scx_update_idle(struct rq *rq, bool idle, bool do_notify);
static inline void scx_update_idle(struct rq *rq, bool idle, bool do_notify)
{
- if (scx_enabled())
+ if (static_branch_unlikely(&scx_idle_tracking_enabled))
__scx_update_idle(rq, idle, do_notify);
}
#else
diff --git a/kernel/sched/ext/idle.c b/kernel/sched/ext/idle.c
index 3e9d6a44bf431..6d81bb7c43966 100644
--- a/kernel/sched/ext/idle.c
+++ b/kernel/sched/ext/idle.c
@@ -14,6 +14,9 @@
#include "idle.h"
#include "sub.h"
+/* Enable/disable idle state tracking */
+DEFINE_STATIC_KEY_FALSE(scx_idle_tracking_enabled);
+
/* Enable/disable built-in idle CPU selection policy */
static DEFINE_STATIC_KEY_FALSE(scx_builtin_idle_enabled);
@@ -810,6 +813,15 @@ void __scx_update_idle(struct rq *rq, bool idle, bool do_notify)
if (static_branch_likely(&scx_builtin_idle_enabled))
update_builtin_idle(cpu, idle);
+ /*
+ * Idle tracking starts before the scheduler is enabled so that the
+ * built-in idle masks are accurate when ops.init() runs. Suppress
+ * ops.update_idle() notifications until the scheduler is fully
+ * enabled.
+ */
+ if (!scx_enabled())
+ return;
+
/*
* ops.update_idle() fires on real idle transitions, indicated by
* @do_notify and managed by put_prev_task_idle()/set_next_task_idle().
@@ -838,8 +850,8 @@ static void reset_idle_masks(struct sched_ext_ops *ops)
int node;
/*
- * Consider all online cpus idle. Should converge to the actual state
- * quickly.
+ * Seed all online CPUs as idle. refresh_idle_masks() below corrects
+ * their state before ops.init() runs.
*/
if (!(ops->flags & SCX_OPS_BUILTIN_IDLE_PER_NODE)) {
cpumask_copy(idle_cpumask(NUMA_NO_NODE)->cpu, cpu_online_mask);
@@ -855,6 +867,23 @@ static void reset_idle_masks(struct sched_ext_ops *ops)
}
}
+static void refresh_idle_masks(void)
+{
+ int cpu;
+
+ /*
+ * Idle tracking is already enabled and the online CPU set is stable.
+ * Once a CPU is refreshed under its rq lock, subsequent transitions
+ * keep its state up to date.
+ */
+ for_each_online_cpu(cpu) {
+ struct rq *rq = cpu_rq(cpu);
+
+ scoped_guard(rq_lock_irqsave, rq)
+ update_builtin_idle(cpu, rq->curr == rq->idle);
+ }
+}
+
void scx_idle_enable(struct sched_ext_ops *ops)
{
if (!ops->update_idle || (ops->flags & SCX_OPS_KEEP_BUILTIN_IDLE))
@@ -868,10 +897,15 @@ void scx_idle_enable(struct sched_ext_ops *ops)
static_branch_disable_cpuslocked(&scx_builtin_idle_per_node);
reset_idle_masks(ops);
+ static_branch_enable_cpuslocked(&scx_idle_tracking_enabled);
+
+ if (static_branch_likely(&scx_builtin_idle_enabled))
+ refresh_idle_masks();
}
void scx_idle_disable(void)
{
+ static_branch_disable(&scx_idle_tracking_enabled);
static_branch_disable(&scx_builtin_idle_enabled);
static_branch_disable(&scx_builtin_idle_per_node);
}
--
2.55.0
next prev parent reply other threads:[~2026-07-31 9:04 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-31 8:59 [PATCHSET v2 sched_ext/for-7.3] sched_ext: Fix idle CPU state initialization and validation Andrea Righi
2026-07-31 8:59 ` Andrea Righi [this message]
2026-07-31 10:47 ` [PATCH 1/2] sched_ext: Initialize idle masks before ops.init() Kuba Piecuch
2026-07-31 15:01 ` Andrea Righi
2026-07-31 8:59 ` [PATCH 2/2] selftests/sched_ext: Make allowed_cpus idle validation race-free Andrea Righi
2026-07-31 11:12 ` Kuba Piecuch
2026-07-31 15:26 ` Andrea Righi
2026-08-01 10:35 ` Kuba Piecuch
2026-07-31 18:23 [PATCHSET v3 sched_ext/for-7.3] sched_ext: Fix idle CPU state initialization and validation Andrea Righi
2026-07-31 18:23 ` [PATCH 1/2] sched_ext: Initialize idle masks before ops.init() Andrea Righi
2026-08-02 19:19 ` Tejun Heo
2026-08-03 5:46 ` Andrea Righi
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=20260731090334.2911948-2-arighi@nvidia.com \
--to=arighi@nvidia.com \
--cc=changwoo@igalia.com \
--cc=jpiecuch@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=sched-ext@lists.linux.dev \
--cc=tj@kernel.org \
--cc=void@manifault.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®