From 20a6354fc503aef6a312f10d214ed2be8a1d8de0 Mon Sep 17 00:00:00 2001 From: Satya Benson Date: Mon, 23 Nov 2026 21:30:06 -0500 Subject: [PATCH 1/4] sched: Make sysctl_sched_features a u64 Each scheduler feature is a bit of sysctl_sched_features, previously an unsigned int with 1UL shifts. With every feature this tree has enabled on SMP (including HAVE_RT_PUSH_IPI), the 32 bits are exhausted and a 33rd feature overflows both unsigned int and 32-bit unsigned long without warning. Widen sysctl_sched_features to u64, switch the feature bit shifts in core.c, sched.h, and debug.c to 1ULL, and assert at build time that __SCHED_FEAT_NR fits in 64 bits so both 32-bit and 64-bit SMP builds succeed. Signed-off-by: Satya Benson --- kernel/sched/core.c | 5 +++-- kernel/sched/debug.c | 6 +++--- kernel/sched/sched.h | 4 ++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index b0719d4853818..1ce62904dc979 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -167,11 +167,12 @@ __setup("sched_proxy_exec", setup_proxy_exec); * at compile time and compiler optimization based on features default. */ #define SCHED_FEAT(name, enabled) \ - (1UL << __SCHED_FEAT_##name) * enabled | -__read_mostly unsigned int sysctl_sched_features = + (1ULL << __SCHED_FEAT_##name) * enabled | +__read_mostly u64 sysctl_sched_features = #include "features.h" 0; #undef SCHED_FEAT +static_assert(__SCHED_FEAT_NR <= 64); /* * Print a warning if need_resched is set for the given duration (if diff --git a/kernel/sched/debug.c b/kernel/sched/debug.c index 72236db67983b..115596b80f935 100644 --- a/kernel/sched/debug.c +++ b/kernel/sched/debug.c @@ -62,7 +62,7 @@ static int sched_feat_show(struct seq_file *m, void *v) int i; for (i = 0; i < __SCHED_FEAT_NR; i++) { - if (!(sysctl_sched_features & (1UL << i))) + if (!(sysctl_sched_features & (1ULL << i))) seq_puts(m, "NO_"); seq_printf(m, "%s ", sched_feat_names[i]); } @@ -114,10 +114,10 @@ static int sched_feat_set(char *cmp) return i; if (neg) { - sysctl_sched_features &= ~(1UL << i); + sysctl_sched_features &= ~(1ULL << i); sched_feat_disable(i); } else { - sysctl_sched_features |= (1UL << i); + sysctl_sched_features |= (1ULL << i); sched_feat_enable(i); } diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index 9dd8c6e6f73b5..65c8bca2e7e55 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -2454,7 +2454,7 @@ enum { * To support run-time toggling of sched features, all the translation units * (but core.c) reference the sysctl_sched_features defined in core.c. */ -extern __read_mostly unsigned int sysctl_sched_features; +extern __read_mostly u64 sysctl_sched_features; #ifdef CONFIG_JUMP_LABEL @@ -2472,7 +2472,7 @@ extern struct static_key sched_feat_keys[__SCHED_FEAT_NR]; #else /* !CONFIG_JUMP_LABEL: */ -#define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x)) +#define sched_feat(x) (sysctl_sched_features & (1ULL << __SCHED_FEAT_##x)) #endif /* !CONFIG_JUMP_LABEL */ From c6238aacd116a8473d2fb3fe227051130b4d35c0 Mon Sep 17 00:00:00 2001 From: Satya Benson Date: Mon, 23 Nov 2026 21:30:06 -0500 Subject: [PATCH 2/4] sched/fair: Spread memory-bound tasks across LLCs (MEM_SPREAD) On an M5 Max (T6050) memory bandwidth is limited per cluster: about 190 GB/s for the six Everest cores and 140 GB/s for a six-core Sawtooth cluster, while one STREAM triad thread alone gets 130 GB/s. Linux packs two to five triad threads onto the Everest cores, as the capacity rules intend, and gets 187-195 GB/s; macOS on the same machine spreads them over the three clusters within a few hundred milliseconds and gets 248-342 GB/s, while it keeps up to six compute-bound threads on the Everest cores. ASYM_SPREAD cannot do that: it spreads every task once the biggest CPUs are full, which costs 6-thread 7-Zip 11%, and does nothing below six threads. Tell the two kinds of task apart from the CPU PMU. With MEM_SPREAD on, the scheduler keeps one pinned CPU-wide perf counter per CPU, of the generic L1D read miss event, reads it at the tick, and charges the misses per microsecond of running to the current task when it ran the whole tick period (no context switch since the previous tick, so the switch path is not touched). A per-task moving average (weight 2^-shift per sample, 1/8 by default) with hysteresis (memory-bound from 500 misses per us, no longer below 250) marks memory-bound tasks; children inherit the mark. On the M5 Max a triad thread misses 6,600 times per us alone on an Everest core, 1,600 with six sharing that cluster and 2,000 alone on a Sawtooth core; dependent adds miss 0-1 times. Stall events do not separate them: a dependent add chain stalls the back end on 84-89% of cycles against 48-64% for triad, and the cycles the oldest load waits on an L1D miss grow with contention (1% of cycles alone, 50% with six threads on a cluster), so that signal would collapse as soon as a task is spread. Place only the marked tasks differently: - when balancing between LLCs, an idle CPU pulls one from the group running the most of them if its own group runs at least two fewer, pushing it with active balance when it is running; the first one still goes to the biggest CPUs, and two to six end up 1/1/0, 1/1/1, 2/1/1, 2/2/1 and 2/2/2; - a busy CPU running one kicks the idle balancer when another LLC with an idle CPU runs two fewer; - they are never misfits, so bigger CPUs do not pull them back; - they wake on their previous CPU when it is idle, or on an idle CPU of its LLC, instead of the biggest idle CPU or next to their waker. Other tasks are placed as before. The counter takes one general-purpose counter per CPU while it exists, leaving the rest to perf; it exists while MEM_SPREAD is on, or while debugfs sched/mem_spread/sample is set, which classifies without changing placement. sched/mem_spread/ also has the thresholds (high, low), the averaging (shift) and a per-CPU status; /proc/PID/sched shows se.mem_rate and se.mem_bound. Without a PMU that counts L1D read misses nothing is classified. Off by default. Signed-off-by: Satya Benson --- include/linux/sched.h | 5 +- kernel/sched/debug.c | 7 + kernel/sched/fair.c | 407 +++++++++++++++++++++++++++++++++++++++- kernel/sched/features.h | 9 + kernel/sched/sched.h | 9 + 5 files changed, 435 insertions(+), 2 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index a6f978d4f3fe9..637a780f7ba88 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -584,7 +584,10 @@ struct sched_entity { unsigned char sched_delayed; unsigned char rel_deadline; unsigned char custom_slice; - /* hole */ + /* MEM_SPREAD: classified memory-bound, see mem_spread_tick() */ + unsigned char mem_bound; + /* MEM_SPREAD: L1D load misses per us of running, moving average */ + unsigned short mem_rate; u64 exec_start; u64 sum_exec_runtime; diff --git a/kernel/sched/debug.c b/kernel/sched/debug.c index 115596b80f935..6d024c272dcfe 100644 --- a/kernel/sched/debug.c +++ b/kernel/sched/debug.c @@ -152,6 +152,9 @@ sched_feat_write(struct file *filp, const char __user *ubuf, if (ret < 0) return ret; + /* MEM_SPREAD needs its PMU counters set up or released. */ + sched_mem_spread_update(); + *ppos += cnt; return cnt; @@ -723,6 +726,8 @@ static __init int sched_init_debug(void) debugfs_create_u32("base_slice_ns", 0644, debugfs_sched, &sysctl_sched_base_slice); + sched_mem_spread_debugfs(debugfs_sched); + debugfs_create_u32("latency_warn_ms", 0644, debugfs_sched, &sysctl_resched_latency_warn_ms); debugfs_create_u32("latency_warn_once", 0644, debugfs_sched, &sysctl_resched_latency_warn_once); @@ -1424,6 +1429,8 @@ void proc_sched_show_task(struct task_struct *p, struct pid_namespace *ns, nr_switches = p->nvcsw + p->nivcsw; P(se.nr_migrations); + P(se.mem_rate); + P(se.mem_bound); if (schedstat_enabled()) { u64 avg_atom, avg_per_cpu; diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 98dcd6d33d15d..c9959bafeb5a5 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -52,6 +52,8 @@ #include #include #include +#include +#include #include @@ -6337,6 +6339,237 @@ static inline int task_fits_cpu(struct task_struct *p, int cpu) return (util_fits_cpu(util, uclamp_min, uclamp_max, cpu) > 0); } +#ifdef CONFIG_PERF_EVENTS +/* + * MEM_SPREAD: telling memory-bound tasks from compute-bound ones. + * + * On Apple SoCs memory bandwidth is limited per cluster: on an M5 Max + * (T6050) about 190 GB/s for the six Everest cores and 140 GB/s for each + * six-core Sawtooth cluster, while one thread alone reaches 130 GB/s. Two + * to five STREAM triad threads therefore get 187-195 GB/s on Linux, which + * packs them onto the Everest cores as it should for compute, and 248-342 + * GB/s on macOS, which spreads them over the clusters within a few hundred + * milliseconds but keeps compute-bound threads on the Everest cores. + * + * The signal is the rate of L1D load misses per microsecond of running, + * from one pinned CPU-wide perf counter per CPU (the generic L1D read miss + * cache event; no counter, no classification). On an M5 Max a triad + * thread misses 6,600 times per us alone on an Everest core, 1,600 with + * six sharing that cluster and 2,000 alone on a Sawtooth core, dependent + * adds 0-1 times. Stall events cannot tell them apart (a dependent add + * chain stalls the back end more than triad does), and + * the stall on L1D misses grows with contention, so it would fall as soon + * as a task is spread and the classification would oscillate. The rate is + * sampled at the tick, attributed to the running task only when it ran + * the whole period (no context switch since the previous tick), averaged + * per task over samples with weight 2^-shift, and classified with + * hysteresis (memory-bound at high, no longer below low). Tasks inherit + * their parent's state at fork. The counters exist only while MEM_SPREAD + * is on or debugfs sched/mem_spread/sample is set; the sample setting + * classifies without changing placement. + */ +static DEFINE_STATIC_KEY_FALSE(sched_mem_sample); + +static unsigned int sysctl_mem_spread_high = 500; +static unsigned int sysctl_mem_spread_low = 250; +static unsigned int sysctl_mem_spread_shift = 3; +static bool mem_spread_sample; + +struct mem_spread_cpu { + struct perf_event *event; + /* Counter, curr's run time and switch count at the previous tick */ + struct perf_event *last_event; + u64 last_count; + u64 last_exec; + u64 last_switches; + unsigned long samples; +}; + +static DEFINE_PER_CPU(struct mem_spread_cpu, mem_spread_cpu); + +static inline bool mem_spread_enabled(void) +{ + return static_branch_unlikely(&sched_mem_sample) && sched_feat(MEM_SPREAD); +} + +static inline bool mem_spread_task(struct task_struct *p) +{ + return mem_spread_enabled() && READ_ONCE(p->se.mem_bound); +} + +/* Whether @rq runs a memory-bound task; RCU or @rq's lock held. */ +static inline bool rq_curr_mem_bound(struct rq *rq) +{ + struct task_struct *curr = rcu_dereference_all(rq->curr); + + return curr->sched_class == &fair_sched_class && READ_ONCE(curr->se.mem_bound); +} + +static void mem_spread_tick(struct rq *rq, struct task_struct *curr) +{ + struct mem_spread_cpu *ms = this_cpu_ptr(&mem_spread_cpu); + struct perf_event *event = READ_ONCE(ms->event); + struct sched_entity *se = &curr->se; + u64 count, exec = se->sum_exec_runtime; + + if (!event || perf_event_read_local(event, &count, NULL, NULL)) { + ms->last_event = NULL; + return; + } + + if (ms->last_event == event && ms->last_switches == rq->nr_switches && + exec > ms->last_exec + 100 * NSEC_PER_USEC) { + unsigned int shift = min(READ_ONCE(sysctl_mem_spread_shift), 8U); + u64 rate = div64_u64((count - ms->last_count) * NSEC_PER_USEC, + exec - ms->last_exec); + int avg = se->mem_rate; + + avg += ((int)min_t(u64, rate, U16_MAX) - avg) >> shift; + WRITE_ONCE(se->mem_rate, avg); + if (avg >= READ_ONCE(sysctl_mem_spread_high)) + WRITE_ONCE(se->mem_bound, 1); + else if (avg < READ_ONCE(sysctl_mem_spread_low)) + WRITE_ONCE(se->mem_bound, 0); + ms->samples++; + } + + ms->last_event = event; + ms->last_count = count; + ms->last_exec = exec; + ms->last_switches = rq->nr_switches; +} + +static struct perf_event *mem_spread_create_event(int cpu) +{ + struct perf_event_attr attr = { + .type = PERF_TYPE_HW_CACHE, + .size = sizeof(attr), + .config = PERF_COUNT_HW_CACHE_L1D | + (PERF_COUNT_HW_CACHE_OP_READ << 8) | + (PERF_COUNT_HW_CACHE_RESULT_MISS << 16), + .pinned = 1, + /* Required by PMUs that cannot count guests at EL1 */ + .exclude_guest = 1, + }; + struct perf_event *event; + + event = perf_event_create_kernel_counter(&attr, cpu, NULL, NULL, NULL); + if (IS_ERR(event)) { + /* PMUs that support no mode exclusion at all */ + attr.exclude_guest = 0; + event = perf_event_create_kernel_counter(&attr, cpu, NULL, NULL, NULL); + } + return event; +} + +static DEFINE_MUTEX(mem_spread_mutex); + +static void mem_spread_update_events(struct work_struct *work) +{ + int cpu, nr = 0; + + guard(mutex)(&mem_spread_mutex); + + if (sched_feat(MEM_SPREAD) || READ_ONCE(mem_spread_sample)) { + scoped_guard(cpus_read_lock) { + for_each_online_cpu(cpu) { + struct mem_spread_cpu *ms = per_cpu_ptr(&mem_spread_cpu, cpu); + struct perf_event *event = ms->event; + + if (!event) { + event = mem_spread_create_event(cpu); + if (IS_ERR(event)) + continue; + WRITE_ONCE(ms->event, event); + } + nr++; + } + if (nr) + static_branch_enable_cpuslocked(&sched_mem_sample); + } + if (!nr) + pr_info_once("sched: MEM_SPREAD: no L1D load miss counter, nothing classified\n"); + return; + } + + /* + * The tick reads the counters with interrupts disabled, so once no + * tick sees the key any more, none uses a counter either. + */ + static_branch_disable(&sched_mem_sample); + synchronize_rcu(); + for_each_possible_cpu(cpu) { + struct mem_spread_cpu *ms = per_cpu_ptr(&mem_spread_cpu, cpu); + + if (ms->event) + perf_event_release_kernel(ms->event); + WRITE_ONCE(ms->event, NULL); + ms->last_event = NULL; + } +} + +static DECLARE_WORK(mem_spread_work, mem_spread_update_events); + +/* Set up or release the counters after MEM_SPREAD or sample changed. */ +void sched_mem_spread_update(void) +{ + schedule_work(&mem_spread_work); +} + +static int mem_spread_sample_get(void *data, u64 *val) +{ + *val = READ_ONCE(mem_spread_sample); + return 0; +} + +static int mem_spread_sample_set(void *data, u64 val) +{ + WRITE_ONCE(mem_spread_sample, !!val); + sched_mem_spread_update(); + flush_work(&mem_spread_work); + return 0; +} + +DEFINE_DEBUGFS_ATTRIBUTE(mem_spread_sample_fops, mem_spread_sample_get, + mem_spread_sample_set, "%llu\n"); + +static int mem_spread_status_show(struct seq_file *m, void *v) +{ + int cpu; + + seq_printf(m, "classifying %d placing %d\n", + !!static_branch_unlikely(&sched_mem_sample), mem_spread_enabled()); + guard(rcu)(); + for_each_online_cpu(cpu) { + struct mem_spread_cpu *ms = per_cpu_ptr(&mem_spread_cpu, cpu); + struct task_struct *curr = rcu_dereference(cpu_rq(cpu)->curr); + + seq_printf(m, "cpu%d %s samples %lu curr %d %s rate %u bound %u\n", cpu, + READ_ONCE(ms->event) ? "counting" : "-", READ_ONCE(ms->samples), + task_pid_nr(curr), curr->comm, READ_ONCE(curr->se.mem_rate), + READ_ONCE(curr->se.mem_bound)); + } + return 0; +} + +DEFINE_SHOW_ATTRIBUTE(mem_spread_status); + +void sched_mem_spread_debugfs(struct dentry *parent) +{ + struct dentry *d = debugfs_create_dir("mem_spread", parent); + + debugfs_create_file("sample", 0644, d, NULL, &mem_spread_sample_fops); + debugfs_create_u32("high", 0644, d, &sysctl_mem_spread_high); + debugfs_create_u32("low", 0644, d, &sysctl_mem_spread_low); + debugfs_create_u32("shift", 0644, d, &sysctl_mem_spread_shift); + debugfs_create_file("status", 0444, d, NULL, &mem_spread_status_fops); +} +#else /* !CONFIG_PERF_EVENTS: */ +static inline bool mem_spread_enabled(void) { return false; } +static inline bool mem_spread_task(struct task_struct *p) { return false; } +static inline bool rq_curr_mem_bound(struct rq *rq) { return false; } +#endif /* !CONFIG_PERF_EVENTS */ + static inline void update_misfit_status(struct task_struct *p, struct rq *rq) { int cpu = cpu_of(rq); @@ -6344,6 +6577,11 @@ static inline void update_misfit_status(struct task_struct *p, struct rq *rq) if (!sched_asym_cpucap_active()) return; + if (p && mem_spread_task(p)) { + rq->misfit_task_load = 0; + return; + } + /* * Affinity allows us to go somewhere higher? Or are we on biggest * available CPU already? Or do we fit into this CPU ? @@ -9205,6 +9443,30 @@ static inline bool asym_fits_cpu(unsigned long util, /* * Try and locate an idle core/thread in the LLC cache domain. */ +/* + * MEM_SPREAD: wake a memory-bound task where the balancer spread it, on + * its previous CPU or another idle CPU of that LLC, rather than on the + * biggest idle CPU (asymmetric capacities) or next to its waker. + */ +static int mem_spread_select_idle(struct task_struct *p, int prev) +{ + struct sched_domain *sd; + int cpu; + + if (choose_idle_cpu(prev, p) && cpumask_test_cpu(prev, p->cpus_ptr)) + return prev; + + sd = rcu_dereference_all(per_cpu(sd_llc, prev)); + if (!sd) + return -1; + + for_each_cpu_and(cpu, sched_domain_span(sd), p->cpus_ptr) { + if (choose_idle_cpu(cpu, p)) + return cpu; + } + return -1; +} + static int select_idle_sibling(struct task_struct *p, int prev, int target) { bool has_idle_core = false; @@ -9228,6 +9490,12 @@ static int select_idle_sibling(struct task_struct *p, int prev, int target) */ lockdep_assert_irqs_disabled(); + if (mem_spread_task(p)) { + i = mem_spread_select_idle(p, prev); + if (i >= 0) + return i; + } + if (choose_idle_cpu(target, p) && asym_fits_cpu(task_util, util_min, util_max, target)) return target; @@ -10755,6 +11023,7 @@ enum migration_type { #define LBF_ACTIVE_LB 0x10 #define LBF_LLC_PINNED 0x20 #define LBF_ACTIVE_LB_LLC 0x40 +#define LBF_MEM 0x80 /* MEM_SPREAD: move a memory-bound task */ struct lb_env { struct sched_domain *sd; @@ -11329,6 +11598,9 @@ int can_migrate_task(struct task_struct *p, struct lb_env *env) /* Record that we found at least one task that could run on dst_cpu */ env->flags &= ~LBF_ALL_PINNED; + if ((env->flags & LBF_MEM) && !READ_ONCE(p->se.mem_bound)) + return 0; + if (task_on_cpu(env->src_rq, p) || task_current_donor(env->src_rq, p)) { schedstat_inc(p->stats.nr_failed_migrations_running); @@ -11813,6 +12085,7 @@ struct sg_lb_stats { unsigned int group_llc_balance; /* Tasks should be moved to preferred LLC */ unsigned long group_misfit_task_load; /* A CPU has a task too big for its capacity */ unsigned int group_overutilized; /* At least one CPU is overutilized in the group */ + unsigned int nr_mem_bound; /* MEM_SPREAD: CPUs running a memory-bound task */ #ifdef CONFIG_NUMA_BALANCING unsigned int nr_numa_running; unsigned int nr_preferred_running; @@ -11835,6 +12108,10 @@ struct sd_lb_stats { struct sg_lb_stats busiest_stat; /* Statistics of the busiest group */ struct sg_lb_stats local_stat; /* Statistics of the local group */ + + /* MEM_SPREAD: the group running the most memory-bound tasks */ + struct sched_group *mem_busiest; + unsigned int mem_busiest_nr; }; static inline void init_sd_lb_stats(struct sd_lb_stats *sds) @@ -12358,6 +12635,7 @@ static inline void update_sg_lb_stats(struct lb_env *env, { int i, nr_running, local_group, sd_flags = env->sd->flags; bool balancing_at_rd = !env->sd->parent; + bool mem = mem_spread_enabled() && !(sd_flags & SD_SHARE_LLC); memset(sgs, 0, sizeof(*sgs)); @@ -12378,6 +12656,9 @@ static inline void update_sg_lb_stats(struct lb_env *env, if (cpu_overutilized(i)) sgs->group_overutilized = 1; + if (mem && nr_running && rq_curr_mem_bound(rq)) + sgs->nr_mem_bound++; + #ifdef CONFIG_SCHED_CACHE if (sched_cache_enabled()) { struct sched_domain *sd_tmp; @@ -13097,6 +13378,11 @@ static inline void update_sd_lb_stats(struct lb_env *env, struct sd_lb_stats *sd update_sg_lb_stats(env, sds, sg, sgs, &sg_overloaded); + if (!local_group && sgs->nr_mem_bound > sds->mem_busiest_nr) { + sds->mem_busiest = sg; + sds->mem_busiest_nr = sgs->nr_mem_bound; + } + if (!local_group && update_sd_pick_busiest(env, sds, sg, sgs)) { sds->busiest = sg; sds->busiest_stat = *sgs; @@ -13322,6 +13608,28 @@ static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *s ) / SCHED_CAPACITY_SCALE; } +/* + * MEM_SPREAD: balancing between LLCs, an idle CPU pulls a memory-bound task + * from the group running the most of them when its own runs at least two + * fewer. Counts even out (on an M5 Max, two to six triad threads end up + * 1/1/0, 1/1/1, 2/1/1, 2/2/1 and 2/2/2 over the three clusters), and the + * first goes to the biggest CPUs as usual. + */ +static bool mem_spread_imbalance(struct lb_env *env, struct sd_lb_stats *sds) +{ + /* sched_balance_rq() may retry with the same env */ + env->flags &= ~LBF_MEM; + + if (!sds->mem_busiest || !env->idle || env->dst_rq->nr_running || + sds->mem_busiest_nr < sds->local_stat.nr_mem_bound + 2) + return false; + + env->flags |= LBF_MEM; + env->migration_type = migrate_task; + env->imbalance = 1; + return true; +} + /******* sched_balance_find_src_group() helpers end here *********************/ /* @@ -13367,6 +13675,9 @@ static struct sched_group *sched_balance_find_src_group(struct lb_env *env) */ update_sd_lb_stats(env, &sds); + if (mem_spread_imbalance(env, &sds)) + return sds.mem_busiest; + /* There is no busy sibling group to pull tasks from */ if (!sds.busiest) goto out_balanced; @@ -13490,6 +13801,24 @@ static struct sched_group *sched_balance_find_src_group(struct lb_env *env) return NULL; } +/* MEM_SPREAD: a CPU of @group running a memory-bound task, the busiest. */ +static struct rq *mem_spread_find_src_rq(struct lb_env *env, + struct sched_group *group) +{ + struct rq *busiest = NULL; + int i; + + for_each_cpu_and(i, sched_group_span(group), env->cpus) { + struct rq *rq = cpu_rq(i); + + if (!rq_curr_mem_bound(rq)) + continue; + if (!busiest || rq->cfs.h_nr_runnable > busiest->cfs.h_nr_runnable) + busiest = rq; + } + return busiest; +} + /* * sched_balance_find_src_rq - find the busiest runqueue among the CPUs in the group. */ @@ -13504,6 +13833,9 @@ static struct rq *sched_balance_find_src_rq(struct lb_env *env, int __maybe_unused dst_llc; int i; + if (env->flags & LBF_MEM) + return mem_spread_find_src_rq(env, group); + for_each_cpu_and(i, sched_group_span(group), env->cpus) { unsigned long capacity, load, util; unsigned int nr_running; @@ -13715,6 +14047,10 @@ static int need_active_balance(struct lb_env *env) if (alb_break_llc(env)) return 0; + /* A running memory-bound task is moved by pushing it. */ + if (env->flags & LBF_MEM) + return 1; + if (asym_active_balance(env)) return 1; @@ -13743,6 +14079,7 @@ static int need_active_balance(struct lb_env *env) static int active_load_balance_cpu_stop(void *data); static int active_load_balance_llc_cpu_stop(void *data); +static int active_load_balance_mem_cpu_stop(void *data); /* * migration_type is checked elsewhere to decide migration policy, so @@ -13754,6 +14091,9 @@ static inline cpu_stop_fn_t alb_stop_fn(struct lb_env *env) if (env->migration_type == migrate_llc_task) return active_load_balance_llc_cpu_stop; + if (env->flags & LBF_MEM) + return active_load_balance_mem_cpu_stop; + return active_load_balance_cpu_stop; } @@ -14060,7 +14400,7 @@ static int sched_balance_rq(int this_cpu, struct rq *this_rq, */ if (idle != CPU_NEWLY_IDLE && env.migration_type != migrate_misfit && - !(env.flags & LBF_LLC_PINNED)) + !(env.flags & (LBF_LLC_PINNED | LBF_MEM))) sd->nr_balance_failed++; if (!need_active_balance(&env)) @@ -14294,6 +14634,11 @@ static int active_load_balance_llc_cpu_stop(void *data) return __active_load_balance_cpu_stop(data, LBF_ACTIVE_LB_LLC); } +static int active_load_balance_mem_cpu_stop(void *data) +{ + return __active_load_balance_cpu_stop(data, LBF_MEM); +} + /* * Scale the max sched_balance_rq interval with the number of CPUs in the system. * This trades load-balance latency on larger machines for less cross talk. @@ -14561,6 +14906,56 @@ static void kick_ilb(unsigned int flags) * Current decision point for kicking the idle load balancer in the presence * of idle CPUs in the system. */ +#ifdef CONFIG_PERF_EVENTS +static unsigned int mem_spread_llc_count(int cpu) +{ + struct sched_domain *sd = rcu_dereference_all(per_cpu(sd_llc, cpu)); + unsigned int nr = 0; + int i; + + if (!sd) + return 0; + for_each_cpu(i, sched_domain_span(sd)) + nr += rq_curr_mem_bound(cpu_rq(i)); + return nr; +} + +/* + * MEM_SPREAD: kick an idle balance when @rq runs a memory-bound task and + * an idle CPU's LLC runs at least two fewer than @rq's. + */ +static bool mem_spread_kick(struct rq *rq) +{ + int seen[8], nr_seen = 0, cpu = cpu_of(rq), i, j; + unsigned int nr; + + if (!mem_spread_enabled() || !rq_curr_mem_bound(rq)) + return false; + + nr = mem_spread_llc_count(cpu); + if (nr < 2) + return false; + + for_each_cpu(i, nohz.idle_cpus_mask) { + int llc = per_cpu(sd_llc_id, i); + + if (cpus_share_cache(i, cpu)) + continue; + for (j = 0; j < nr_seen && seen[j] != llc; j++) + ; + if (j < nr_seen) + continue; + if (nr_seen < ARRAY_SIZE(seen)) + seen[nr_seen++] = llc; + if (mem_spread_llc_count(i) + 2 <= nr) + return true; + } + return false; +} +#else +static inline bool mem_spread_kick(struct rq *rq) { return false; } +#endif + static void nohz_balancer_kick(struct rq *rq) { unsigned long now = jiffies; @@ -14606,6 +15001,11 @@ static void nohz_balancer_kick(struct rq *rq) goto out; } + if (mem_spread_kick(rq)) { + flags = NOHZ_STATS_KICK | NOHZ_BALANCE_KICK; + goto out; + } + sd = rcu_dereference_all(rq->sd); if (sd) { /* @@ -15639,6 +16039,11 @@ static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued) task_tick_cache(rq, curr); +#ifdef CONFIG_PERF_EVENTS + if (static_branch_unlikely(&sched_mem_sample)) + mem_spread_tick(rq, curr); +#endif + update_misfit_status(curr, rq); check_update_overutilized_status(task_rq(curr)); diff --git a/kernel/sched/features.h b/kernel/sched/features.h index 71afc624b09b5..7e16868d0a83d 100644 --- a/kernel/sched/features.h +++ b/kernel/sched/features.h @@ -108,6 +108,15 @@ SCHED_FEAT(WAKE_SHORT_LOCAL, true) */ SCHED_FEAT(FORK_LOCAL, false) +/* + * Spread memory-bound tasks evenly across LLCs (on Apple SoCs, clusters, + * whose memory bandwidth is limited per cluster) instead of packing them + * onto the biggest CPUs; other tasks are placed as usual. Tasks are + * classified from a per-CPU PMU count of L1D load misses, see + * mem_spread_tick(). + */ +SCHED_FEAT(MEM_SPREAD, false) + /* * Issue a WARN when we do multiple update_rq_clock() calls * in a single rq->lock section. Default disabled because the diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index 65c8bca2e7e55..d46495c228e34 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -2479,6 +2479,15 @@ extern struct static_key sched_feat_keys[__SCHED_FEAT_NR]; extern struct static_key_false sched_numa_balancing; extern struct static_key_false sched_schedstats; +struct dentry; +#ifdef CONFIG_PERF_EVENTS +void sched_mem_spread_update(void); +void sched_mem_spread_debugfs(struct dentry *parent); +#else +static inline void sched_mem_spread_update(void) { } +static inline void sched_mem_spread_debugfs(struct dentry *parent) { } +#endif + static inline u64 global_rt_period(void) { return (u64)sysctl_sched_rt_period * NSEC_PER_USEC; From 78ebc6a42d1742f8770a755ec5688907d531cefc Mon Sep 17 00:00:00 2001 From: Satya Benson Date: Mon, 23 Nov 2026 12:05:07 -0500 Subject: [PATCH 3/4] sched/fair: Set up the MEM_SPREAD counters at boot and across CPU hotplug The counters were only set up when the feature was switched on in debugfs. Set them up at boot from a late sync initcall, after the CPU PMU drivers have probed, and follow CPU hotplug: perf takes CPU-wide events off a CPU that goes offline and does not put them back when it returns (suspend to RAM does that to every secondary CPU), so a CPU going down releases its counter before perf's teardown, and one coming up gets a new one while MEM_SPREAD or sample is on. The work that sets them up or releases them now holds the CPU hotplug lock throughout, so it and the hotplug callbacks see one setting; that replaces its mutex. The first setup logs how many CPUs count. MEM_SPREAD stays off by default. On the M5 Max (T6050) it brings STREAM triad with 2-7 OpenMP threads from 178-289 GB/s to 247-372 GB/s and the pthread triad with 2-6 threads to 249-383 GB/s, against macOS's 248-350, with compute staying on the Everest cores (7-Zip scores the same with 6 threads and with 18). But its thresholds (500 and 250 misses per microsecond, shift 3) were set from M5 Max cores alone, so a default would be chosen by where it was tried, not by a hardware property. It can be turned on at run time in debugfs sched/features; the boot-time setup and the hotplug handling take effect then, or when sched/mem_spread/sample is turned on. Signed-off-by: Satya Benson --- kernel/sched/fair.c | 112 +++++++++++++++++++++++++++++++--------- kernel/sched/features.h | 2 +- 2 files changed, 90 insertions(+), 24 deletions(-) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index c9959bafeb5a5..be8365e86c84c 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -6365,8 +6365,8 @@ static inline int task_fits_cpu(struct task_struct *p, int cpu) * per task over samples with weight 2^-shift, and classified with * hysteresis (memory-bound at high, no longer below low). Tasks inherit * their parent's state at fork. The counters exist only while MEM_SPREAD - * is on or debugfs sched/mem_spread/sample is set; the sample setting - * classifies without changing placement. + * (off by default) is on or debugfs sched/mem_spread/sample is set; the + * sample setting classifies without changing placement. */ static DEFINE_STATIC_KEY_FALSE(sched_mem_sample); @@ -6462,33 +6462,46 @@ static struct perf_event *mem_spread_create_event(int cpu) return event; } -static DEFINE_MUTEX(mem_spread_mutex); +static inline bool mem_spread_wanted(void) +{ + return sched_feat(MEM_SPREAD) || READ_ONCE(mem_spread_sample); +} + +/* Give @cpu its counter if it has none; CPU hotplug lock held. */ +static bool mem_spread_start_cpu(int cpu) +{ + struct mem_spread_cpu *ms = per_cpu_ptr(&mem_spread_cpu, cpu); + struct perf_event *event; + if (ms->event) + return true; + event = mem_spread_create_event(cpu); + if (IS_ERR(event)) + return false; + WRITE_ONCE(ms->event, event); + return true; +} + +/* + * Runs with the CPU hotplug lock held throughout, so the hotplug callbacks + * below, which run under it too, see either the old or the new setting. + */ static void mem_spread_update_events(struct work_struct *work) { int cpu, nr = 0; - guard(mutex)(&mem_spread_mutex); - - if (sched_feat(MEM_SPREAD) || READ_ONCE(mem_spread_sample)) { - scoped_guard(cpus_read_lock) { - for_each_online_cpu(cpu) { - struct mem_spread_cpu *ms = per_cpu_ptr(&mem_spread_cpu, cpu); - struct perf_event *event = ms->event; + guard(cpus_read_lock)(); - if (!event) { - event = mem_spread_create_event(cpu); - if (IS_ERR(event)) - continue; - WRITE_ONCE(ms->event, event); - } - nr++; - } - if (nr) - static_branch_enable_cpuslocked(&sched_mem_sample); - } - if (!nr) + if (mem_spread_wanted()) { + for_each_online_cpu(cpu) + nr += mem_spread_start_cpu(cpu); + if (nr) { + static_branch_enable_cpuslocked(&sched_mem_sample); + pr_info_once("sched: MEM_SPREAD: counting L1D load misses on %d CPUs\n", + nr); + } else { pr_info_once("sched: MEM_SPREAD: no L1D load miss counter, nothing classified\n"); + } return; } @@ -6496,7 +6509,7 @@ static void mem_spread_update_events(struct work_struct *work) * The tick reads the counters with interrupts disabled, so once no * tick sees the key any more, none uses a counter either. */ - static_branch_disable(&sched_mem_sample); + static_branch_disable_cpuslocked(&sched_mem_sample); synchronize_rcu(); for_each_possible_cpu(cpu) { struct mem_spread_cpu *ms = per_cpu_ptr(&mem_spread_cpu, cpu); @@ -6508,6 +6521,40 @@ static void mem_spread_update_events(struct work_struct *work) } } +/* + * CPU hotplug: perf takes CPU-wide events off a CPU that goes offline and + * does not put them back when it returns, so release the counter before + * perf's own teardown and create a new one when the CPU is back (suspend + * to RAM takes every secondary CPU down and up again). Both run on @cpu + * itself. + */ +static int mem_spread_cpu_online(unsigned int cpu) +{ + if (mem_spread_wanted() && mem_spread_start_cpu(cpu)) + static_branch_enable_cpuslocked(&sched_mem_sample); + return 0; +} + +static int mem_spread_cpu_offline(unsigned int cpu) +{ + struct mem_spread_cpu *ms = per_cpu_ptr(&mem_spread_cpu, cpu); + struct perf_event *event; + unsigned long flags; + + WARN_ON_ONCE(cpu != smp_processor_id()); + + /* Only this CPU's tick reads its counter, with interrupts disabled. */ + local_irq_save(flags); + event = ms->event; + WRITE_ONCE(ms->event, NULL); + ms->last_event = NULL; + local_irq_restore(flags); + + if (event) + perf_event_release_kernel(event); + return 0; +} + static DECLARE_WORK(mem_spread_work, mem_spread_update_events); /* Set up or release the counters after MEM_SPREAD or sample changed. */ @@ -6516,6 +6563,25 @@ void sched_mem_spread_update(void) schedule_work(&mem_spread_work); } +/* + * If MEM_SPREAD is on from boot, set the counters up once the CPU PMU drivers + * have probed (device initcalls, deferred probes in late initcalls), and + * follow CPUs that come and go from then on. + */ +static int __init sched_mem_spread_init(void) +{ + int ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "sched/mem_spread:online", + mem_spread_cpu_online, mem_spread_cpu_offline); + + if (ret < 0) + pr_warn("sched: MEM_SPREAD: no CPU hotplug state (%d), CPUs that go offline lose their counter\n", + ret); + if (mem_spread_wanted()) + sched_mem_spread_update(); + return 0; +} +late_initcall_sync(sched_mem_spread_init); + static int mem_spread_sample_get(void *data, u64 *val) { *val = READ_ONCE(mem_spread_sample); diff --git a/kernel/sched/features.h b/kernel/sched/features.h index 7e16868d0a83d..5424a69dae6a7 100644 --- a/kernel/sched/features.h +++ b/kernel/sched/features.h @@ -113,7 +113,7 @@ SCHED_FEAT(FORK_LOCAL, false) * whose memory bandwidth is limited per cluster) instead of packing them * onto the biggest CPUs; other tasks are placed as usual. Tasks are * classified from a per-CPU PMU count of L1D load misses, see - * mem_spread_tick(). + * mem_spread_tick(); without such a counter nothing changes. */ SCHED_FEAT(MEM_SPREAD, false) From c1013c5169080d97bc46f2aa70fb90a1820b7536 Mon Sep 17 00:00:00 2001 From: Satya Benson Date: Wed, 25 Nov 2026 02:12:23 -0500 Subject: [PATCH 4/4] sched/fair: normalize MEM_SPREAD by CPU clock rate and enable by default Previously MEM_SPREAD classified tasks using raw L1D load misses per microsecond with M5 Max-specific thresholds (500/250 misses/us) and stayed disabled by default. Raw misses per microsecond scales directly with clock frequency, so fixed thresholds do not carry cleanly across slower clusters, DVFS states, or other Apple Silicon generations. Normalize each tick's L1D load-miss count into misses per 1,000 estimated active cycles (MPKC) using each CPU's reported hardware maximum frequency (cpufreq_get_hw_max_freq(), also updated via CPUFREQ_CREATE_POLICY for modular cpufreq drivers) scaled by live frequency invariance (arch_scale_freq_capacity()). This removes clock and DVFS dependence with no extra PMU counter: streaming workloads sustain 140-840 MPKC even under cluster contention while cache-heavy compute (7-Zip) stays at 15-16 MPKC across both P and M cores, cleanly separated by the 100/50 MPKC hysteresis thresholds (1 miss per 10 cycles to classify, below 1 per 20 to clear). Gate default-on PMU counter allocation on multi-LLC topologies so single-LLC systems incur zero PMU counter overhead unless debugfs sample mode is explicitly enabled, expose per-CPU max_khz in mem_spread/status, and enable SCHED_FEAT(MEM_SPREAD, true) by default. Signed-off-by: Satya Benson --- include/linux/sched.h | 2 +- kernel/sched/fair.c | 130 ++++++++++++++++++++++++++++++---------- kernel/sched/features.h | 8 ++- 3 files changed, 103 insertions(+), 37 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index 637a780f7ba88..167cad301cc07 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -586,7 +586,7 @@ struct sched_entity { unsigned char custom_slice; /* MEM_SPREAD: classified memory-bound, see mem_spread_tick() */ unsigned char mem_bound; - /* MEM_SPREAD: L1D load misses per us of running, moving average */ + /* MEM_SPREAD: L1D load misses per 1000 active cycles, moving average */ unsigned short mem_rate; u64 exec_start; diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index a25dc9e1ddf2d..d77d269e92a25 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -52,6 +52,7 @@ #include #include #include +#include #include #include @@ -6351,32 +6352,39 @@ static inline int task_fits_cpu(struct task_struct *p, int cpu) * GB/s on macOS, which spreads them over the clusters within a few hundred * milliseconds but keeps compute-bound threads on the Everest cores. * - * The signal is the rate of L1D load misses per microsecond of running, + * The signal is L1D load misses per 1,000 estimated active cycles (MPKC), * from one pinned CPU-wide perf counter per CPU (the generic L1D read miss - * cache event; no counter, no classification). On an M5 Max a triad - * thread misses 6,600 times per us alone on an Everest core, 1,600 with - * six sharing that cluster and 2,000 alone on a Sawtooth core, dependent - * adds 0-1 times. Stall events cannot tell them apart (a dependent add - * chain stalls the back end more than triad does), and - * the stall on L1D misses grows with contention, so it would fall as soon - * as a task is spread and the classification would oscillate. The rate is - * sampled at the tick, attributed to the running task only when it ran - * the whole period (no context switch since the previous tick), averaged - * per task over samples with weight 2^-shift, and classified with - * hysteresis (memory-bound at high, no longer below low). Tasks inherit - * their parent's state at fork. The counters exist only while MEM_SPREAD - * (off by default) is on or debugfs sched/mem_spread/sample is set; the - * sample setting classifies without changing placement. + * cache event) divided by the cycles estimated from each CPU's reported + * hardware maximum frequency (cpufreq_get_hw_max_freq()) scaled by live + * frequency invariance (arch_scale_freq_capacity()). Normalizing by active + * cycles removes clock-rate and DVFS dependence across heterogeneous + * clusters and SoC generations without pinning a second PMU counter: + * streaming workloads sustain 140-840 MPKC even under cluster contention + * while cache-heavy compute (7-Zip) stays around 15-16 MPKC across both + * P and M cores, well below the 100/50 MPKC hysteresis thresholds (1 miss + * per 10 cycles to classify, below 1 per 20 cycles to clear). Stall events + * cannot tell them apart (a dependent add chain stalls the back end more + * than triad does), and the stall on L1D misses grows with contention, so + * it would fall as soon as a task is spread and oscillate. + * + * The rate is sampled at the tick, attributed to the running task only + * when it ran the whole period (no context switch since the previous tick), + * averaged per task over samples with weight 2^-shift, and classified with + * hysteresis. Tasks inherit their parent's state at fork. Counters are + * opened only on multi-LLC systems while MEM_SPREAD is on, or whenever + * debugfs sched/mem_spread/sample is set (which classifies without + * changing placement). */ static DEFINE_STATIC_KEY_FALSE(sched_mem_sample); -static unsigned int sysctl_mem_spread_high = 500; -static unsigned int sysctl_mem_spread_low = 250; +static unsigned int sysctl_mem_spread_high = 100; +static unsigned int sysctl_mem_spread_low = 50; static unsigned int sysctl_mem_spread_shift = 3; static bool mem_spread_sample; struct mem_spread_cpu { struct perf_event *event; + unsigned int max_freq_khz; /* Counter, curr's run time and switch count at the previous tick */ struct perf_event *last_event; u64 last_count; @@ -6409,10 +6417,11 @@ static void mem_spread_tick(struct rq *rq, struct task_struct *curr) { struct mem_spread_cpu *ms = this_cpu_ptr(&mem_spread_cpu); struct perf_event *event = READ_ONCE(ms->event); + unsigned int max_khz = READ_ONCE(ms->max_freq_khz); struct sched_entity *se = &curr->se; u64 count, exec = se->sum_exec_runtime; - if (!event || perf_event_read_local(event, &count, NULL, NULL)) { + if (!event || !max_khz || perf_event_read_local(event, &count, NULL, NULL)) { ms->last_event = NULL; return; } @@ -6420,8 +6429,13 @@ static void mem_spread_tick(struct rq *rq, struct task_struct *curr) if (ms->last_event == event && ms->last_switches == rq->nr_switches && exec > ms->last_exec + 100 * NSEC_PER_USEC) { unsigned int shift = min(READ_ONCE(sysctl_mem_spread_shift), 8U); - u64 rate = div64_u64((count - ms->last_count) * NSEC_PER_USEC, - exec - ms->last_exec); + unsigned long freq_khz = + (u64)max_khz * arch_scale_freq_capacity(cpu_of(rq)) >> + SCHED_CAPACITY_SHIFT; + u64 kcycles = mul_u64_u32_div(exec - ms->last_exec, + max_t(u32, freq_khz, 1U), + NSEC_PER_SEC); + u64 rate = kcycles ? div64_u64(count - ms->last_count, kcycles) : 0; int avg = se->mem_rate; avg += ((int)min_t(u64, rate, U16_MAX) - avg) >> shift; @@ -6462,17 +6476,36 @@ static struct perf_event *mem_spread_create_event(int cpu) return event; } +/* Whether online CPUs span more than one LLC; CPU hotplug lock held. */ +static bool mem_spread_multi_llc(void) +{ + int first = cpumask_first(cpu_online_mask); + int cpu; + + if (first >= nr_cpu_ids) + return false; + for_each_online_cpu(cpu) { + if (!cpus_share_cache(cpu, first)) + return true; + } + return false; +} + static inline bool mem_spread_wanted(void) { - return sched_feat(MEM_SPREAD) || READ_ONCE(mem_spread_sample); + return (sched_feat(MEM_SPREAD) && mem_spread_multi_llc()) || + READ_ONCE(mem_spread_sample); } /* Give @cpu its counter if it has none; CPU hotplug lock held. */ static bool mem_spread_start_cpu(int cpu) { struct mem_spread_cpu *ms = per_cpu_ptr(&mem_spread_cpu, cpu); + unsigned int max_khz = cpufreq_get_hw_max_freq(cpu); struct perf_event *event; + if (max_khz) + WRITE_ONCE(ms->max_freq_khz, max_khz); if (ms->event) return true; event = mem_spread_create_event(cpu); @@ -6521,6 +6554,14 @@ static void mem_spread_update_events(struct work_struct *work) } } +static DECLARE_WORK(mem_spread_work, mem_spread_update_events); + +/* Set up or release the counters after MEM_SPREAD or sample changed. */ +void sched_mem_spread_update(void) +{ + schedule_work(&mem_spread_work); +} + /* * CPU hotplug: perf takes CPU-wide events off a CPU that goes offline and * does not put them back when it returns, so release the counter before @@ -6530,8 +6571,12 @@ static void mem_spread_update_events(struct work_struct *work) */ static int mem_spread_cpu_online(unsigned int cpu) { - if (mem_spread_wanted() && mem_spread_start_cpu(cpu)) - static_branch_enable_cpuslocked(&sched_mem_sample); + if (mem_spread_wanted()) { + if (!static_branch_unlikely(&sched_mem_sample)) + schedule_work(&mem_spread_work); + else if (mem_spread_start_cpu(cpu)) + static_branch_enable_cpuslocked(&sched_mem_sample); + } return 0; } @@ -6555,14 +6600,27 @@ static int mem_spread_cpu_offline(unsigned int cpu) return 0; } -static DECLARE_WORK(mem_spread_work, mem_spread_update_events); - -/* Set up or release the counters after MEM_SPREAD or sample changed. */ -void sched_mem_spread_update(void) +#ifdef CONFIG_CPU_FREQ +static int mem_spread_cpufreq_notify(struct notifier_block *nb, + unsigned long val, void *data) { - schedule_work(&mem_spread_work); + struct cpufreq_policy *policy = data; + int cpu; + + if (val != CPUFREQ_CREATE_POLICY) + return NOTIFY_DONE; + + for_each_cpu(cpu, policy->related_cpus) + WRITE_ONCE(per_cpu_ptr(&mem_spread_cpu, cpu)->max_freq_khz, + policy->cpuinfo.max_freq); + return NOTIFY_DONE; } +static struct notifier_block mem_spread_cpufreq_nb = { + .notifier_call = mem_spread_cpufreq_notify, +}; +#endif + /* * If MEM_SPREAD is on from boot, set the counters up once the CPU PMU drivers * have probed (device initcalls, deferred probes in late initcalls), and @@ -6570,13 +6628,18 @@ void sched_mem_spread_update(void) */ static int __init sched_mem_spread_init(void) { - int ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "sched/mem_spread:online", - mem_spread_cpu_online, mem_spread_cpu_offline); + int ret; +#ifdef CONFIG_CPU_FREQ + cpufreq_register_notifier(&mem_spread_cpufreq_nb, + CPUFREQ_POLICY_NOTIFIER); +#endif + ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "sched/mem_spread:online", + mem_spread_cpu_online, mem_spread_cpu_offline); if (ret < 0) pr_warn("sched: MEM_SPREAD: no CPU hotplug state (%d), CPUs that go offline lose their counter\n", ret); - if (mem_spread_wanted()) + if (sched_feat(MEM_SPREAD) || READ_ONCE(mem_spread_sample)) sched_mem_spread_update(); return 0; } @@ -6610,8 +6673,9 @@ static int mem_spread_status_show(struct seq_file *m, void *v) struct mem_spread_cpu *ms = per_cpu_ptr(&mem_spread_cpu, cpu); struct task_struct *curr = rcu_dereference(cpu_rq(cpu)->curr); - seq_printf(m, "cpu%d %s samples %lu curr %d %s rate %u bound %u\n", cpu, - READ_ONCE(ms->event) ? "counting" : "-", READ_ONCE(ms->samples), + seq_printf(m, "cpu%d %s max_khz %u samples %lu curr %d %s rate %u bound %u\n", + cpu, READ_ONCE(ms->event) ? "counting" : "-", + READ_ONCE(ms->max_freq_khz), READ_ONCE(ms->samples), task_pid_nr(curr), curr->comm, READ_ONCE(curr->se.mem_rate), READ_ONCE(curr->se.mem_bound)); } diff --git a/kernel/sched/features.h b/kernel/sched/features.h index efb6d46fafbfd..e361a4b886906 100644 --- a/kernel/sched/features.h +++ b/kernel/sched/features.h @@ -112,10 +112,12 @@ SCHED_FEAT(FORK_LOCAL, true) * Spread memory-bound tasks evenly across LLCs (on Apple SoCs, clusters, * whose memory bandwidth is limited per cluster) instead of packing them * onto the biggest CPUs; other tasks are placed as usual. Tasks are - * classified from a per-CPU PMU count of L1D load misses, see - * mem_spread_tick(); without such a counter nothing changes. + * classified from a per-CPU PMU count of L1D load misses normalized per + * 1,000 estimated active cycles from each CPU's reported clock frequency, + * see mem_spread_tick(); on single-LLC systems or without a supported + * counter and clock rate nothing changes. */ -SCHED_FEAT(MEM_SPREAD, false) +SCHED_FEAT(MEM_SPREAD, true) /* * Issue a WARN when we do multiple update_rq_clock() calls