[PATCH 1/4] sched/core: Decouple the core scheduling unit from cpu_smt_mask
From: Ahmed Abdelraoof
Date: Thu Dec 03 2026 - 17:13:44 EST
From: Anselm Busse <abusse@xxxxxxxxxx>
Core scheduling uses cpu_smt_mask() to identify which CPUs (SMT threads)
belong to the same core scheduling equivalence group. That said, SMT
siblings are not the only CPUs that share microarchitectural state.
Cores behind a shared L2 cache can observe each other through it much
as SMT siblings can through the core.
Add sched_core_isolation_mask() as the single definition of that unit
and use it for every core scheduling user of cpu_smt_mask(). The helper
returns cpu_smt_mask() for now, so there is no functional change.
Signed-off-by: Anselm Busse <abusse@xxxxxxxxxx>
Co-developed-by: Ahmed Abdelraoof <araoof@xxxxxxxxxx>
Signed-off-by: Ahmed Abdelraoof <araoof@xxxxxxxxxx>
---
kernel/sched/core.c | 33 ++++++++++++++++++---------------
kernel/sched/sched.h | 18 ++++++++++++++----
2 files changed, 32 insertions(+), 19 deletions(-)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index ea89471d4743..cdd944bb274d 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -433,11 +433,11 @@ static void sched_core_lock(int cpu, unsigned long *flags)
__context_unsafe(/* acquires multiple */)
__acquires(&runqueues.__lock) /* overapproximation */
{
- const struct cpumask *smt_mask = cpu_smt_mask(cpu);
+ const struct cpumask *isolation_mask = sched_core_isolation_mask(cpu);
int t, i = 0;
local_irq_save(*flags);
- for_each_cpu(t, smt_mask)
+ for_each_cpu(t, isolation_mask)
raw_spin_lock_nested(&cpu_rq(t)->__lock, i++);
}
@@ -445,10 +445,10 @@ static void sched_core_unlock(int cpu, unsigned long *flags)
__context_unsafe(/* releases multiple */)
__releases(&runqueues.__lock) /* overapproximation */
{
- const struct cpumask *smt_mask = cpu_smt_mask(cpu);
+ const struct cpumask *isolation_mask = sched_core_isolation_mask(cpu);
int t;
- for_each_cpu(t, smt_mask)
+ for_each_cpu(t, isolation_mask)
raw_spin_unlock(&cpu_rq(t)->__lock);
local_irq_restore(*flags);
}
@@ -465,7 +465,7 @@ static void __sched_core_flip(bool enabled)
*/
cpumask_copy(&sched_core_mask, cpu_online_mask);
for_each_cpu(cpu, &sched_core_mask) {
- const struct cpumask *smt_mask = cpu_smt_mask(cpu);
+ const struct cpumask *isolation_mask = sched_core_isolation_mask(cpu);
sched_core_lock(cpu, &flags);
@@ -480,14 +480,17 @@ static void __sched_core_flip(bool enabled)
sched_core_lock(cpu, &flags);
}
- for_each_cpu(t, smt_mask)
+ for_each_cpu(t, isolation_mask)
cpu_rq(t)->core_enabled = enabled;
cpu_rq(cpu)->core->core_forceidle_start = 0;
sched_core_unlock(cpu, &flags);
- cpumask_andnot(&sched_core_mask, &sched_core_mask, smt_mask);
+ /*
+ * Skip already toggled cores
+ */
+ cpumask_andnot(&sched_core_mask, &sched_core_mask, isolation_mask);
}
/*
@@ -6700,7 +6703,7 @@ DEFINE_LOCK_GUARD_1(core_lock, int,
static void sched_core_cpu_starting(unsigned int cpu)
{
- const struct cpumask *smt_mask = cpu_smt_mask(cpu);
+ const struct cpumask *isolation_mask = sched_core_isolation_mask(cpu);
struct rq *rq = cpu_rq(cpu), *core_rq = NULL;
int t;
@@ -6709,11 +6712,11 @@ static void sched_core_cpu_starting(unsigned int cpu)
WARN_ON_ONCE(rq->core != rq);
/* if we're the first, we'll be our own leader */
- if (cpumask_weight(smt_mask) == 1)
+ if (cpumask_weight(isolation_mask) == 1)
return;
/* find the leader */
- for_each_cpu(t, smt_mask) {
+ for_each_cpu(t, isolation_mask) {
if (t == cpu)
continue;
rq = cpu_rq(t);
@@ -6727,7 +6730,7 @@ static void sched_core_cpu_starting(unsigned int cpu)
return;
/* install and validate core_rq */
- for_each_cpu(t, smt_mask) {
+ for_each_cpu(t, isolation_mask) {
rq = cpu_rq(t);
if (t == cpu)
@@ -6739,14 +6742,14 @@ static void sched_core_cpu_starting(unsigned int cpu)
static void sched_core_cpu_deactivate(unsigned int cpu)
{
- const struct cpumask *smt_mask = cpu_smt_mask(cpu);
+ const struct cpumask *isolation_mask = sched_core_isolation_mask(cpu);
struct rq *rq = cpu_rq(cpu), *core_rq = NULL;
int t;
guard(core_lock)(&cpu);
/* if we're the last man standing, nothing to do */
- if (cpumask_weight(smt_mask) == 1) {
+ if (cpumask_weight(isolation_mask) == 1) {
WARN_ON_ONCE(rq->core != rq);
return;
}
@@ -6756,7 +6759,7 @@ static void sched_core_cpu_deactivate(unsigned int cpu)
return;
/* find a new leader */
- for_each_cpu(t, smt_mask) {
+ for_each_cpu(t, isolation_mask) {
if (t == cpu)
continue;
core_rq = cpu_rq(t);
@@ -6789,7 +6792,7 @@ static void sched_core_cpu_deactivate(unsigned int cpu)
core_rq->core_forceidle_start = 0;
/* install new leader */
- for_each_cpu(t, smt_mask) {
+ for_each_cpu(t, isolation_mask) {
rq = cpu_rq(t);
rq->core = core_rq;
}
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index 32f76551a0c5..948f70af6fa6 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -1565,7 +1565,7 @@ static inline raw_spinlock_t *__rq_lockp(struct rq *rq)
}
/*
- * A CPU coming online is set in cpu_smt_mask() before
+ * A CPU coming online is set in sched_core_isolation_mask() before
* sched_core_cpu_starting() links its rq->core to the core leader.
* Until then it does not share the core-wide lock, so it must not be
* touched as part of the core.
@@ -1576,11 +1576,11 @@ static inline bool sched_core_sibling(struct rq *rq, struct rq *rq_i)
}
#define for_each_core_cpu(i, rq) \
- for_each_cpu(i, cpu_smt_mask(rq->cpu)) \
+ for_each_cpu(i, sched_core_isolation_mask(rq->cpu)) \
if (sched_core_sibling(rq, cpu_rq(i)))
#define for_each_core_cpu_wrap(i, rq) \
- for_each_cpu_wrap(i, cpu_smt_mask(rq->cpu), rq->cpu) \
+ for_each_cpu_wrap(i, sched_core_isolation_mask(rq->cpu), rq->cpu) \
if (sched_core_sibling(rq, cpu_rq(i)))
extern bool
@@ -1588,6 +1588,16 @@ cfs_prio_less(const struct task_struct *a, const struct task_struct *b, bool fi)
extern void task_vruntime_update(struct rq *rq, struct task_struct *p, bool in_fi);
+/*
+ * The core scheduling isolation unit: the set of CPUs that share one runqueue
+ * lock and that are constrained to run tasks carrying a single cookie at any
+ * instant.
+ */
+static inline const struct cpumask *sched_core_isolation_mask(int cpu)
+{
+ return cpu_smt_mask(cpu);
+}
+
/*
* Helpers to check if the CPU's core cookie matches with the task's cookie
* when core scheduling is enabled.
@@ -1615,7 +1625,7 @@ static inline bool sched_core_cookie_match(struct rq *rq, struct task_struct *p)
if (rq->core->core_cookie == p->core_cookie)
return true;
- for_each_cpu(cpu, cpu_smt_mask(cpu_of(rq))) {
+ for_each_cpu(cpu, sched_core_isolation_mask(cpu_of(rq))) {
if (!available_idle_cpu(cpu)) {
idle_core = false;
break;
--
2.47.3