diff --git a/include/linux/sched.h b/include/linux/sched.h index a6f978d4f3fe9..167cad301cc07 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 1000 active cycles, moving average */ + unsigned short mem_rate; u64 exec_start; u64 sum_exec_runtime; diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 2b30a9901e0c2..b0d0575a624a7 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..6d024c272dcfe 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); } @@ -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 95eb408358bb6..d77d269e92a25 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -52,6 +52,9 @@ #include #include #include +#include +#include +#include #include @@ -6337,6 +6340,366 @@ 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 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) 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 = 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; + 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); + 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 || !max_khz || 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); + 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; + 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; +} + +/* 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) && 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); + 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(cpus_read_lock)(); + + 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; + } + + /* + * 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_cpuslocked(&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); +} + +/* + * 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()) { + 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; +} + +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; +} + +#ifdef CONFIG_CPU_FREQ +static int mem_spread_cpufreq_notify(struct notifier_block *nb, + unsigned long val, void *data) +{ + 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 + * follow CPUs that come and go from then on. + */ +static int __init sched_mem_spread_init(void) +{ + 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 (sched_feat(MEM_SPREAD) || READ_ONCE(mem_spread_sample)) + 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); + 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 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)); + } + 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 +6707,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 +9573,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 +9620,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 +11153,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 +11728,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 +12215,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 +12238,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 +12765,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 +12786,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 +13508,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 +13738,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 +13805,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 +13931,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 +13963,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 +14177,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 +14209,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 +14221,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 +14530,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 +14764,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 +15036,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 +15131,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 +16169,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 a6def9a19c001..e361a4b886906 100644 --- a/kernel/sched/features.h +++ b/kernel/sched/features.h @@ -108,6 +108,17 @@ SCHED_FEAT(WAKE_SHORT_LOCAL, true) */ 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 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, true) + /* * 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 78640a29b0fbc..f3a5702fd3b94 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,13 +2472,22 @@ 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 */ 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;