Re: [PATCH v3 17/20] sched/core: Introduce default arch handling code for inc/dec preferred CPUs

From: Ilya Leoshkevich

Date: Mon Jun 08 2026 - 12:40:36 EST




On 5/14/26 17:22, Shrikanth Hegde wrote:
Define default handlers for high/low steal time. If arch has better
decision logic, may override the default implementation.

- If the steal time higher than threshold, reduce the number of preferred
CPUs by 1 core. The last core in the intersection of online and
preferred CPUs will be marked as non-preferred.
Ensure at least one core is left as preferred always.

- If the steal time lower than threshold, increase the number of preferred
CPUs by 1 core. First online core which is not in cpu_preferred_mask will
be marked as preferred.
If all cores are already set to preferred, bail out.

Increase/Decrease may need to modify the splicing across NUMA nodes. It is
being kept simple for now.

Signed-off-by: Shrikanth Hegde <sshegde@xxxxxxxxxxxxx>
---
include/linux/sched.h | 2 ++
kernel/sched/core.c | 58 +++++++++++++++++++++++++++++++++++++++++++
2 files changed, 60 insertions(+)

diff --git a/include/linux/sched.h b/include/linux/sched.h
index 738f17d63943..2afbcd70f0ac 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -2529,6 +2529,8 @@ struct steal_monitor_t {
};
extern struct steal_monitor_t steal_mon;
+void arch_dec_preferred_cpus(struct steal_monitor_t *sm, u64 steal_ratio);
+void arch_inc_preferred_cpus(struct steal_monitor_t *sm, u64 steal_ratio);
#endif
#endif
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index a3f65e9c7d30..195e3648b1b5 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -11368,6 +11368,64 @@ void sched_init_steal_monitor(void)
steal_mon.sampling_period_ms = 1000; /* once per second */
}
+/*
+ * Default implementation of decrementing the preferred CPUs based on steal
+ * time. This is simple logic and decrease the preferred CPUs by 1 core.
+ * It takes out the last core in the online & preferred.
+ *
+ * Ensure at least one housekeeping core is always kept as preferred
+ *
+ * Could be overwritten by arch specific handling.
+ */
+#ifndef arch_dec_preferred_cpus
+void arch_dec_preferred_cpus(struct steal_monitor_t *sm, u64 steal_ratio)
+{
+ int last_cpu, tmp_cpu;
+ int this_cpu = raw_smp_processor_id();
+
+ cpumask_and(sm->tmp_mask, cpu_online_mask, cpu_preferred_mask);
+ last_cpu = cpumask_last(sm->tmp_mask);
+
+ /*
+ * If the core belongs to the housekeeping CPUs, no action is
+ * taken. This leaves at least one core preferred always.
+ * This ensures at least some CPUs are available to run
+ */
+ if (cpumask_equal(cpu_smt_mask(last_cpu), cpu_smt_mask(this_cpu)))
+ return;
+
+ for_each_cpu_and(tmp_cpu, cpu_smt_mask(last_cpu), cpu_online_mask) {
+ set_cpu_preferred(tmp_cpu, false);
+ if (tick_nohz_full_cpu(tmp_cpu))
+ tick_nohz_dep_set_cpu(tmp_cpu, TICK_DEP_BIT_SCHED);

Should the opposite be done in arch_inc_preferred_cpus()?

If not, it would be good to write a comment explaining why; this
asymmetry catches the eye.

+ }
+}
+#endif
+
+/*
+ * Default implementation of incrementing preferred CPUs based on steal
+ * time. This is simple logic and increases the preferred CPUs by 1 core.
+ * It adds the first core in online & !preferred
+ *
+ * Nothing to do if online == preferred
+ *
+ * Could be overwritten by arch specific handling.
+ */
+#ifndef arch_inc_preferred_cpus
+void arch_inc_preferred_cpus(struct steal_monitor_t *sm, u64 steal_ratio)
+{
+ int first_cpu, tmp_cpu;
+
+ first_cpu = cpumask_first_andnot(cpu_online_mask, cpu_preferred_mask);
+ /* All CPUs are preferred. Nothing to increase further */
+ if (first_cpu >= nr_cpu_ids)
+ return;
+
+ for_each_cpu_and(tmp_cpu, cpu_smt_mask(first_cpu), cpu_online_mask)
+ set_cpu_preferred(tmp_cpu, true);
+}
+#endif
+
/* This is only a skeleton. Subsequent patches introduce more of it */
void sched_steal_detection_work(struct work_struct *work)
{