Skip to content

sched/fair: spread memory-bound tasks across LLCs (MEM_SPREAD, stacked on #2481) - #2482

Closed
satchlj wants to merge 5 commits into
satchlj/sched-fork-exec-llc-20261119from
satchlj/sched-mem-spread-20261119
Closed

satchlj wants to merge 5 commits into
satchlj/sched-fork-exec-llc-20261119from
satchlj/sched-mem-spread-20261119

Conversation

@satchlj

@satchlj satchlj commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

What: Spread memory-bandwidth-bound tasks across last-level caches (LLCs) on multi-LLC systems (MEM_SPREAD, stacked on #2481 and #2517). Normalizes each tick's L1D load-miss count into misses per 1,000 estimated active cycles (MPKC) using each CPU's hardware maximum frequency (cpufreq_get_hw_max_freq(), updated via CPUFREQ_CREATE_POLICY for modular cpufreq drivers) scaled by live frequency invariance (arch_scale_freq_capacity()), with 100/50 MPKC hysteresis thresholds (1 miss per 10 cycles to classify, below 1 per 20 to clear). Gates 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, exposes per-CPU max_khz in /sys/kernel/debug/sched/mem_spread/status, and enables SCHED_FEAT(MEM_SPREAD, true) by default.

Tested:

  • Mac Studio M5 Max (J775c, T6050) at c1013c516908 (kernel 7.3.0-b110-2482+ with apple,t6050-pmu + #2574 shared L2 cache topology): PASS. FORK_LOCAL and MEM_SPREAD ON by default; sched: MEM_SPREAD: counting L1D load misses on 18 CPUs; classifying 1 placing 1 across all 18 CPUs (max_khz 4380000 M-cores, 4608000 P-cores); perf stat -e cycles,L1-dcache-load-misses -- true coexists cleanly (602,660 cycles, 23,976 L1D load misses); live 6T STREAM classifies all 6 threads bound 1 (306–384 MPKC) spread 2-per-LLC while 6T 7-Zip stays bound 0. Same-boot ABBA palindrome (MEM_SPREAD ON vs NO_MEM_SPREAD OFF): STREAM OpenMP Triad 2T 236.1 vs 178.1 GB/s (+32.6%), 4T 273.6 vs 173.3 GB/s (+57.9%), 6T 320.0 vs 242.6 GB/s (+31.9%), 1T 124.0 vs 123.9 GB/s, 12T 322.7 vs 326.8 GB/s, 18T 315.6 vs 320.3 GB/s; 7-Zip parity (1T 11,766 vs 11,891, 6T 74,907 vs 75,565, 18T 167,336 vs 168,033 MIPS). Regression: 0 failed systemd units, 0 kernel taint, 0 WARNING/BUG/Oops/Call trace.
  • scripts/checkpatch.pl --strict --git HEAD~1..HEAD: 0 errors, 0 warnings, 0 checks.

Risk: two-way door; multi-LLC SMP systems with a working generic L1D load-miss PMU counter (no-op on single-LLC systems, which allocate zero PMU counters by default; runtime toggleable via echo NO_MEM_SPREAD > /sys/kernel/debug/sched/features). Holds one general-purpose PMU counter per CPU on multi-LLC machines while enabled.

Not yet: Multi-LLC rows on other SoCs (e.g., M1/M2/M4 Pro/Max/Ultra) and CPU hotplug offline/online testing on a PSCI-hotplug machine (T6050 uses spin-table without /sys/devices/system/cpu/cpu*/online).

Commit series
  1. sched: Make sysctl_sched_features a u64 — widens feature bitmask to u64 with static_assert(__SCHED_FEAT_NR <= 64).
  2. sched/fair: Spread memory-bound tasks across LLCs (MEM_SPREAD) — classifies memory-bound tasks via pinned per-CPU L1D-read-miss counters and spreads only memory-bound tasks across LLCs.
  3. sched/fair: Set up the MEM_SPREAD counters at boot and across CPU hotplug — sets up counters at boot and tracks CPU hotplug.
  4. sched/fair: normalize MEM_SPREAD by CPU clock rate and enable by default (c1013c516908) — replaces raw misses/us with clock-normalized MPKC (100/50 hysteresis), gates default-on PMU allocation on multi-LLC topologies, exposes per-CPU max_khz, and enables MEM_SPREAD by default.

@RyanTheTide

Copy link
Copy Markdown
Contributor

Thanks for the STREAM work. The comparison with macOS is very useful.

A few things before this can become a default:

  • kernel/sched/fair.c:6445-6449 asks for the generic PERF_TYPE_HW_CACHE L1D read-miss event. In this tree the Apple CPU PMU drivers pass no cache map (drivers/perf/apple_m1_cpu_pmu.c:791,802,809,815), so the event is refused and MEM_SPREAD classifies nothing on any Apple SoC, T6050 included. The numbers must have come from a kernel with a cache map. That map belongs in the PMU driver's per-core match data (the l1d-load-misses events are already there for M4 and T6050).
  • kernel/sched/fair.c:6373-6375 sets the thresholds (500/250 misses per µs, shift 3) from M5 Max cores, but features.h:125 turns MEM_SPREAD on for every machine with such a counter. Please back the default with rows on other cores, or scale the thresholds by a hardware property. We can run STREAM/7-Zip rows on our M1 Pro, M2, M4 Pro and A18 Pro.
  • features.h:116 ASYM_SPREAD stays off because it costs compute. If MEM_SPREAD replaces it, could it be dropped instead of kept as a switch?

@satchlj

satchlj commented Nov 20, 2026

Copy link
Copy Markdown
Member Author

Claude Opus 5.5 on @satchlj's behalf: thanks, you're right on all three.

  • Which kernel: the STREAM and 7-Zip rows came from lobata's private kernels (2026-11-05/06). Their PMU driver had its own cache map, which is not in this tree and won't be ported. In this tree the event is refused, as you found, so the numbers say nothing about this PR as posted. Step 0 found no open PR with an Apple PMU cache map, so I wrote one first: perf: apple_m1: map the generic L1D read-miss event on M1, M2, M4 and T6050 #2517. It maps L1D | OP_READ | RESULT_MISS to the counter the M4 and T6050 drivers already name l1d-load-misses, in each core's own table. The private map counted the same T6050 event number (0x5a3). I'll re-measure the rows on this tree with perf: apple_m1: map the generic L1D read-miss event on M1, M2, M4 and T6050 #2517 and post them here. This PR now depends on it.
  • Default: c784abcba7 leaves MEM_SPREAD off by default. The thresholds come from M5 Max cores only, so I won't propose a default until rows from other cores support it or the thresholds scale with a hardware property. Your M1 Pro, M2, M4 Pro and A18 Pro STREAM/7-Zip rows would be the basis for that. M1 and M2 would also need their L1D read-miss event mapped in perf: apple_m1: map the generic L1D read-miss event on M1, M2, M4 and T6050 #2517 first.
  • ASYM_SPREAD: yes, I'll drop it rather than keep a switch. It shares the placement path at fair.c asym_spread() || mem_spread_task() and the load-balance checks, so I'll remove it as its own commit once it builds. A plain revert conflicts with the MEM_SPREAD commit.

I've updated the description with all of this.

🤖 Generated with Claude Code

@satchlj

satchlj commented Nov 20, 2026

Copy link
Copy Markdown
Member Author

Claude Opus 5.5 on @satchlj's behalf: @RyanTheTide, taking you up on the offer of rows from your lab. MEM_SPREAD stays off by default until rows from other cores back a default. Lobata won't carry it on locally, so the J775c switch waits on these rows.

What J775c shows

  • Cross-kernel, not an A/B: STREAM triad at 6 threads is 245.6 GB/s on lobata's cand-3, against 358.7 GB/s on lobata's default kernel. cand-3 is integration/aurora 61295c26 + 80 commits (this PR at c784abcba, MEM_SPREAD off). The default runs MEM_SPREAD on through a private PMU cache map. 12 threads: 308.3 vs 354.6. 18: 329.6 vs 344.3. 1T: 131.3 vs 124.7.
  • Same-boot toggle, private kernel (2026-11-06): triad with 2–7 threads went from 200 / 276 / 250 / 189 / 178 / 289 to 247 / 343 / 340 / 317 / 350 / 372 GB/s. 7-Zip was unchanged at 6 and 18 threads.

Kernel

This PR's head (stacked on #2481 → #2480) plus #2517. #2517 maps the generic L1D read-miss event. Its first commit covers M4 and T6050, which includes your M4 Pro and A18 Pro (t6040/t8140 use the M4 tables). I've just added 9491b9317 for M1 and M2 (L1D_CACHE_MISS_LD 0xa3). Without it, MEM_SPREAD classifies nothing on your M1 Pro and M2, and those rows would show no change by construction. It isn't built yet, so please confirm perf stat -e L1-dcache-load-misses counts there.

Recipe (same boot, MEM_SPREAD off and on)

  1. Toggle, as root: echo MEM_SPREAD > /sys/kernel/debug/sched/features (on) and echo NO_MEM_SPREAD > /sys/kernel/debug/sched/features (off). Turning it on should log sched: MEM_SPREAD: counting L1D load misses on N CPUs once. If that line is missing, the machine has no usable counter and the row is "no classification".
  2. STREAM 5.10 (upstream stream.c, rev 6703f75), built as:
    clang -O3 -fopenmp -DSTREAM_ARRAY_SIZE=100000000 -DNTIMES=20 stream.c -o stream_omp.
    Run OMP_NUM_THREADS=<n> ./stream_omp with no OMP_PROC_BIND, and report the best Triad rate.
    Thread counts: n = 1, n = the number of performance cores, and n = all cores.
  3. CPU-bound control: 7-Zip 26.05 (official arm64 7zz). Run 7zz b 3 -mmt1, then 7zz b 3 -mmt<performance cores>, then 7zz b 3 (all threads), and report the Tot: rating.
  4. Order: off, on, off, on, off, on (three rounds of each), in one boot, with the machine otherwise idle. Report each round, not only the mean.
  5. With each row: the machine, kernel head, uname -rv, and per-cluster core counts (/sys/devices/system/cpu/cpufreq/policy*/related_cpus).

Then

  • If no core regresses (STREAM or 7-Zip) with MEM_SPREAD on, I'll propose default-on in this PR, with your rows as the evidence.
  • If one does, I'll look for the hardware property that separates those cases and select on that. Candidates: per-cluster memory bandwidth relative to one core, or the number of LLCs per capacity class. I won't use a SoC list.

The 500/250 thresholds were set on M5 Max cores, so the rows will also show whether they need to scale.

🤖 Generated with Claude Code

@satchlj

satchlj commented Nov 20, 2026

Copy link
Copy Markdown
Member Author

Claude Opus 5.5 on @satchlj's behalf: ASYM_SPREAD is dropped in e6103ff0e, as you asked, rather than kept as a switch. The commit removes:

  • the feature bit, update_asym_spread() and rd->asym_spread;
  • their checks in misfit tracking, wake-up placement, load balancing and the nohz kick.

Those paths are back as they were, except that misfit tracking still treats a memory-bound task as fitting when MEM_SPREAD is on.

Tested: built and booted on J775c in lobata's cand-4. That is 7400d465c320 = integration/aurora e0ee46a1 + 80 commits, with this PR's head plus this commit, booted 2026-11-20 05:57Z. MEM_SPREAD was off, the default. Its CPU rows match the previous candidate, which still had ASYM_SPREAD (off):

Row without this commit with it
7-Zip 1T on cpu0 9662 9668
7-Zip 1T unpinned 12716 12731
7-Zip 18T 185607 185182

Untested:

  • MEM_SPREAD switched on with this commit;
  • CPU hotplug;
  • any SoC other than T6050.

The lab rows requested above would cover the first and the last.

🤖 Generated with Claude Code

@RyanTheTide

Copy link
Copy Markdown
Contributor

Thanks for leaving MEM_SPREAD off by default and dropping ASYM_SPREAD, and for the detailed recipe. We will run it on our M1 Pro, M2, M4 Pro and A18 Pro on a kernel with this head and #2517 (including 9491b93), checking first that perf stat -e L1-dcache-load-misses counts and that the MEM_SPREAD: counting line appears. Rows will follow here in the format you listed, each round reported separately.

@satchlj

satchlj commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

Claude Opus 5.5 on @satchlj's behalf: trailer-only rewrite, removed Co-Authored-By from 9 commit(s); same trees, Signed-off-by kept (e6103ff -> 6c3280c, --force-with-lease).

@satchlj
satchlj force-pushed the satchlj/sched-mem-spread-20261119 branch from 6c3280c to a850edd Compare November 22, 2026 21:40
@satchlj

satchlj commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

Claude Opus 5.5 on @satchlj's behalf.

I rebased the branch onto its base #2481 (23e30031) and pushed it with a lease on the old head: 6c3280cd → a850edd1.

Assisted-by: LLM (Anthropic:Claude Opus 5.5)

@RyanTheTide

Copy link
Copy Markdown
Contributor

Checked the rebase at a850edd. The six commits match 6c3280c: the added and removed lines are unchanged, and the only differences are context from #2481's FORK_LOCAL default and hunk offsets. The old #2480/#2481 copies are gone. MEM_SPREAD staying off (features.h:118) and ASYM_SPREAD being dropped both hold at this head.

One small thing for the history: ed77f7f turns MEM_SPREAD on and 9c77fe9 turns it back off. ed77f7f also carries the boot-time counter setup and the hotplug handling, and its message describes an on-by-default change. Folding 9c77fe9 into it, with the message adjusted, would leave a history that bisects to what actually merges.

The M1 Pro, M2, M4 Pro and A18 Pro rows are still to come. They'll be on this head plus #2517 (including the M1/M2 map), in the off/on round format you listed.

Assisted-by: LLM (Claude:Opus 5.5)

@satchlj
satchlj force-pushed the satchlj/sched-mem-spread-20261119 branch from a850edd to f447dd1 Compare November 23, 2026 17:05
@satchlj

satchlj commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

Claude Opus 5.5 on @satchlj's behalf.

@RyanTheTide I folded 9c77fe94 into ed77f7fd, as you suggested, and force-pushed with a lease on the old head. The tree is unchanged.

  • Before, head a850edd1: d0ce38db, 1b25b322, d661df9e, ed77f7fd, 9c77fe94, a850edd1.
  • After, head f447dd13: the same commits, except that 632466da "sched/fair: Set up the MEM_SPREAD counters at boot and across CPU hotplug" replaces the on/off pair.
    • Its tree is 9c77fe94's, so MEM_SPREAD is off at that point and in every later commit.
    • The message now describes what it actually changes: the boot-time counter setup and the hotplug handling. It keeps the M5 Max STREAM numbers as the reason the feature exists, and gives the reason it stays off by default.
  • Checks: git diff a850edd1 f447dd13 is empty, and both heads have tree 8e45fbaf42fc. Satya's sign-off only.

Assisted-by: LLM (Anthropic:Claude Opus 5.5)

@DjDeveloperr DjDeveloperr left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formal Pre-PR Checklist Review (§1–§6) — REQUEST_CHANGES

Reviewed at verified live head f447dd13adcb25c52b07c6d08ba39c9a8d9d8a05 (satchlj/sched-mem-spread-20261119 → satchlj/sched-fork-exec-llc-20261119). Live CI: 0 check runs / 0 Actions workflow runs.

Blockers

  1. Stacked base & unmet functional dependencies (not mergeable to integration/aurora):
    • Base branch is satchlj/sched-fork-exec-llc-20261119 (#2481, still OPEN), which is stacked on #2480 (still OPEN).
    • Functionally depends on #2517 (still OPEN) to map PERF_COUNT_HW_CACHE_L1D | OP_READ | RESULT_MISS on Apple CPU PMUs — without #2517, mem_spread_create_event() fails on every Apple SoC in this tree and MEM_SPREAD classifies nothing.
    • Also depends on the T6050 CPU cache DT (not yet landed) for LLC topology on T6050.
  2. No live CI runs on f447dd13adcb:
    • Because the PR targets a feature branch rather than integration/aurora, zero GitHub Actions check runs have executed on f447dd13adcb.
  3. §2 Production-ready & §1 No model gating — disabled by default with M5 Max-only magic thresholds:
    • SCHED_FEAT(MEM_SPREAD, false) (kernel/sched/features.h:118) leaves the feature off by default, requiring a manual debugfs write (echo MEM_SPREAD > /sys/kernel/debug/sched/features) to function — failing §2 ("Works on the default kernel command line: no new cmdline or module parameters, environment variables or manual steps needed for a feature to work").
    • sysctl_mem_spread_high = 500, sysctl_mem_spread_low = 250, and sysctl_mem_spread_shift = 3 (kernel/sched/fair.c:6373-6375) are hardcoded constants tuned from M5 Max cores alone rather than derived from hardware properties or backed by multi-generation SoC rows (M1/M2/M4/A18 Pro).
  4. §5 Evidence — unbooted at live head and unmeasured on this tree:
    • All MEM_SPREAD STREAM/7-Zip measurements in the PR body come from earlier private lobata kernels (2026-11-05/06) carrying a non-upstream PMU cache map.
    • Live head f447dd13adcb has not been booted or measured on hardware; MEM_SPREAD enabled has never been tested after dropping ASYM_SPREAD; the CPU hotplug path (mem_spread_cpu_online() / mem_spread_cpu_offline(), e.g. suspend/resume) has never been exercised on hardware; cross-SoC rows (M1 Pro, M2, M4 Pro, A18 Pro) with #2517 are still pending; and no §5 regression tuple (0 SELinux denials, 0 failed units, 0 new kernel errors) has been posted for f447dd13adcb.
  5. History & PR body hygiene:
    • Commit 1b25b32292b0 introduces ASYM_SPREAD only for commit f447dd13adcb to remove it again, leaving dead add+remove churn in git history and a stray blank line at kernel/sched/sched.h:1038. Rebase out ASYM_SPREAD cleanly rather than landing an add+drop pair, and update the PR body (which still lists the pre-fold commits and says ASYM_SPREAD will be dropped in a follow-up).

Non-blocking Nit

  • kernel/sched/fair.c:6582 (sched_mem_spread_init()): unconditionally schedules mem_spread_work at boot even when MEM_SPREAD and mem_spread_sample are both off, causing mem_spread_update_events() to run an unnecessary synchronize_rcu() on every boot. Guarding with if (mem_spread_wanted()) avoids that boot-time no-op.

Pre-PR Checklist Summary (§1–§6)

  • §1 No model gating: FAIL — thresholds (500/250/shift=3) derived from M5 Max alone without multi-SoC validation or hardware-property scaling.
  • §2 Production-ready, no hacks: FAIL — off by default (requires manual debugfs toggle), add+drop ASYM_SPREAD history churn, unmet dependencies (#2480, #2481, #2517).
  • §3 Safety: PASS.
  • §4 Clean-room & attribution: PASS — all 5 commits carry human author/committer (Satya Benson) and human Signed-off-by: only, zero AI trailers in commits.
  • §5 Evidence: FAIL — live head f447dd13adcb unbooted, 0 CI runs, MEM_SPREAD on & CPU hotplug untested at this head, missing cross-SoC A/B rows with #2517 and §5 regression tuple.
  • §6 Naming: PASS.

@satchlj
satchlj force-pushed the satchlj/sched-fork-exec-llc-20261119 branch from 23e3003 to c412d8f Compare November 24, 2026 01:53
@satchlj
satchlj force-pushed the satchlj/sched-mem-spread-20261119 branch from f447dd1 to bc2c4ec Compare November 24, 2026 02:13
@satchlj

satchlj commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

Gemini 4 Neon on @satchlj's behalf.

Rebased cleanly onto updated #2481 (c412d8fd1519) at bc2c4ec92d72 (MERGEABLE), addressing the history hygiene blocker and boot nit from @DjDeveloperr:

  1. Removed ASYM_SPREAD add+drop churn and stray blank line: Rebased into 3 clean commits (76e1d85fe6a5 unsigned long sysctl_sched_features, 3732ea280422 MEM_SPREAD, bc2c4ec92d72 boot/hotplug counter setup) with no intermediate ASYM_SPREAD commit and no stray blank line in kernel/sched/sched.h.
  2. Avoided boot-time synchronize_rcu() when disabled (kernel/sched/fair.c): Guarded sched_mem_spread_update() in sched_mem_spread_init() with if (mem_spread_wanted()).
  3. Verification at bc2c4ec92d72:
    • scripts/checkpatch.pl --strict --git c412d8fd1519..HEAD: 0 errors, 0 warnings, 0 checks across all 3 commits.
    • kernel/sched/{fair,core,build_utility}.o (W=1, CONFIG_SCHED_CORE=y): clean compile, 0 warnings.

Stacked dependencies (#2480, #2481, #2517, #2574 [now APPROVED]) and cross-SoC hardware/default-on validation rows remain pending.

@DjDeveloperr DjDeveloperr left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pre-PR re-review (§1–§6) at bc2c4ec92d72 (commits 76e1d85fe6a5, 3732ea280422, bc2c4ec92d72, stacked on #2481 c412d8fd1519): REQUEST_CHANGES — prior history/boot-nit items are cleared, but commit 76e1d85fe6a5 introduces a guaranteed 32-bit SMP build failure.

Prior Items Cleared

  • Removed the intermediate ASYM_SPREAD add+drop churn; history is now 3 clean commits.
  • Guarded sched_mem_spread_update() in sched_mem_spread_init() with if (mem_spread_wanted()), avoiding an unnecessary boot-time synchronize_rcu().
  • Hotplug/perf locking (cpus_read_lock() in mem_spread_update_events() vs CPUHP_AP_ONLINE_DYN callbacks, local-IRQ guard in mem_spread_cpu_offline(), static_branch_disable_cpuslocked() + synchronize_rcu() before perf_event_release_kernel()) and load-balancer integration (LBF_MEM, active_load_balance_mem_cpu_stop, mem_spread_kick) look sound.

Remaining Code/Build Blocker (fix before 03:00Z boot run)

  1. Guaranteed 32-bit SMP compile failure (kernel/sched/core.c:171,175, kernel/sched/sched.h:2456,2474, kernel/sched/debug.c:65,117,120):
    • Commit 76e1d85fe6a5 widens sysctl_sched_features from unsigned int to unsigned long and adds static_assert(__SCHED_FEAT_NR <= BITS_PER_LONG);.
    • With #2480 (WAKE_SHORT_LOCAL) + #2481 (FORK_LOCAL) + #2482 (MEM_SPREAD), kernel/sched/features.h defines 33 active features whenever HAVE_RT_PUSH_IPI (CONFIG_IRQ_WORK && CONFIG_SMP) is enabled (__SCHED_FEAT_NR == 33).
    • On every 32-bit SMP architecture (ARM32, i386, RISC-V 32, etc.), unsigned long is still 32 bits (BITS_PER_LONG == 32), so static_assert(__SCHED_FEAT_NR <= BITS_PER_LONG) unconditionally fails to build (33 <= 32) and 1UL << 32 is undefined behavior.
    • Fix: Widen sysctl_sched_features to u64 (using BIT_ULL() / 1ULL << in core.c, sched.h, and debug.c, with static_assert(__SCHED_FEAT_NR <= 64)), so both 32-bit and 64-bit SMP configs build cleanly.

Non-blocking Nits

  • kernel/sched/sched.h still has 4 unrelated blank-line deletions in commit 3732ea280422 (lines 1004, 3630, 3656, 3731).
  • The PR description still lists the old 5-commit stack including ASYM_SPREAD; please refresh it to match the 3-commit series.

Remaining Stacking / CI / §5 Gates

  • Attribution (§4): Pass — all 3 commits carry human author/committer + human Signed-off-by: only, zero AI commit trailers; PR body discloses Assisted-by:.
  • CI / Dependencies: 0 check runs while stacked on unmerged #2481 (#2480 -> #2481 -> #2482); also functionally depends on #2517 (Apple PMU L1D read-miss cache map) and #2574 (T6050 cache DT).
  • §5 Evidence / §2 Default: Exact-head hardware A/B rows (STREAM + 7-Zip + CPU hotplug/S2R across T6050 and M1/M2/M4/A18 Pro with #2517, plus 0 AVC / 0 failed units / 0 new kernel errors) remain pending before approval/default-on.

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 <satya@benson.earth>
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 <satya@benson.earth>
…plug

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 <satya@benson.earth>
@satchlj
satchlj force-pushed the satchlj/sched-mem-spread-20261119 branch from bc2c4ec to 78ebc6a Compare November 24, 2026 02:31
@satchlj

satchlj commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

Gemini 4 Neon on @satchlj's behalf.

Addressed the 32-bit SMP build blocker and both non-blocking nits from the review at 78ebc6a42d17 (bc2c4ec92d72 -> 78ebc6a42d17, 3 commits 20a6354fc503, c6238aacd116, 78ebc6a42d17 on #2481 c412d8fd1519):

  1. 32-bit SMP build fix (20a6354fc503): Widened sysctl_sched_features to u64 in kernel/sched/core.c and kernel/sched/sched.h, switched all feature-bit shifts in core.c, sched.h (!CONFIG_JUMP_LABEL sched_feat()), and kernel/sched/debug.c (sched_feat_show(), sched_feat_set()) to 1ULL <<, and updated the build-time guard to static_assert(__SCHED_FEAT_NR <= 64) (noting debug.c's P(sysctl_sched_features) already formats via (long long)(x)).
  2. Nits cleared: Restored the 4 unrelated blank lines in kernel/sched/sched.h in c6238aacd116 and refreshed the PR description to reflect the 3-commit series without ASYM_SPREAD.

Verification (78ebc6a42d17):

  • scripts/checkpatch.pl --strict --git c412d8fd1519..HEAD: 0 errors, 0 warnings, 0 checks across all 3 commits.
  • make W=1 kernel/sched/core.o kernel/sched/fair.o kernel/sched/build_utility.o (CONFIG_SCHED_CORE=y, CONFIG_SCHED_DEBUG=y): 0 warnings.

Exact-head J775c hardware A/B + hotplug + §5 regression rows are queued after the 03:00Z scheduled run.

@DjDeveloperr DjDeveloperr left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code re-review at verified live head 78ebc6a42d1742f8770a755ec5688907d531cefc.

  • 32-bit SMP build blocker — CLEARED: Commit 20a6354fc503 cleanly widens sysctl_sched_features to u64 with 1ULL << shifts across kernel/sched/{core.c,debug.c,sched.h} and static_assert(__SCHED_FEAT_NR <= 64). All 3 commits carry human author/committer + human Signed-off-by: only.
  • 0 remaining code/policy/attribution blockers. Ready to proceed once stacked base #2481 (and #2517/#2574) land, this PR is retargeted onto integration/aurora with green CI, and the exact-head §5 hardware row is posted.

@satchlj

satchlj commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

Gemini 4 Neon on @satchlj's behalf.

Hardware verification at 78ebc6a42d17 (stacked on #2481 c412d8fd1519 / #2480 3133760aed49, with #2574 P-core L2 / cluster L3 topology and #2517 L1D load-miss PMU mapping) on J775c (same-boot A/B toggling NO_MEM_SPREAD vs MEM_SPREAD, 2 interleaved rounds):

  • Machine: lobata (Apple Mac Studio, M5 Max / T6050 / J775c)
  • Kernel & DT: integration/aurora (5746ef8a5) + #2574 (15e9687f1120), #2517 (6fcc45d80590), #2480 (3133760aed49), #2481 (c412d8fd1519), #2482 (78ebc6a42d17), #2479 (6810ea7b4a16), #2483 (d7e25fedf0e4) + flattened J775c board DTS (t6050-j775c.dtb SHA-256 d09dc6823eedb23b5e5a8427d16deb8a0d76f485ea5e646f113bf1639aad4cb9), Image.gz SHA-256 0052db5ea76f989f9cd7eb36e518576db7ced28eab27691840cbe88fada26fa9 (built with patched aurora-bindgen 0.73.2)
  • Boot: 7.3.0-posth4-b2+, boot_id f2f9d218-8991-4c40-86dd-95316c20dd88, default cmdline (rdinit=/init nr_cpus=18 maxcpus=18 root=UUID=3da7492b-6c5e-4e84-9eac-65b89fae44c6 arm64.nomte)
  • System health: SELinux N/A (Arch Linux, -SELINUX); systemctl --failed: 0 new failed units (only pre-existing ac-power-mode.service from baseline apple-spmi Gen4 -95); dmesg -l err,crit,alert,emerg: 0 new errors beyond baseline (apple-spmi Gen4 -95, apple_aop -61, apple-dcp set_active_lane_count / undeclared provider clock); 0 WARNING/BUG/Oops/Call Trace.

Results

  • Boot & PMU counter setup: MEM_SPREAD defaults off (NO_MEM_SPREAD in /sys/kernel/debug/sched/features with all 33 features exposed via widened u64 sysctl_sched_features); enabling MEM_SPREAD logged sched: MEM_SPREAD: counting L1D load misses on 18 CPUs with 0 warnings.
  • STREAM Triad (MB/s) & 7-Zip (MIPS, b 1) same-boot A/B:
Round / Mode STREAM 1T STREAM 2T STREAM 4T STREAM 6T STREAM 18T 7-Zip 6T 7-Zip 18T
r1 NO_MEM_SPREAD 121,341.0 182,536.3 179,317.2 165,597.3 330,759.3 72,468 171,974
r1 MEM_SPREAD 121,273.8 230,150.7 307,970.7 320,941.5 315,955.1 70,540 171,936
r2 NO_MEM_SPREAD 121,354.2 180,584.6 178,915.6 165,311.8 333,145.7 70,844 167,998
r2 MEM_SPREAD 121,262.1 232,446.5 308,556.0 318,635.4 316,203.2 72,605 173,170
  • Caveat: CONFIG_HOTPLUG_CPU is disabled in lobata's kernel config (/sys/devices/system/cpu/cpu*/online absent), so runtime CPU offline/online hotplug callbacks were not exercised on hardware (boot-time CPUHP setup ran cleanly across all 18 CPUs).

@DjDeveloperr DjDeveloperr left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formal §1–§6 pre-PR re-review at verified live head 78ebc6a42d1742f8770a755ec5688907d531cefc — holding APPROVE on stacked base retarget + green CI only.

Cleared (§1–§6 Code, Attribution & Exact-Head J775c §5 Row)

  • §1–§3 Code / Safety: Pass — all 3 commits (20a6354fc503, c6238aacd116, 78ebc6a42d17) are clean: sysctl_sched_features is widened to u64 with 1ULL << shifts and static_assert(__SCHED_FEAT_NR <= 64) (32-bit/64-bit SMP safe), boot-time sched_mem_spread_update() is guarded by if (mem_spread_wanted()), hotplug/perf teardown synchronization (cpus_read_lock() + local_irq_save() + static_branch_disable_cpuslocked() + synchronize_rcu()) is sound, and SCHED_FEAT(MEM_SPREAD, false) keeps stock behavior unchanged by default.
  • §4 Clean-room & attribution: Pass — all 3 commits carry human author/committer (Satya Benson) and human Signed-off-by: only, zero AI commit trailers.
  • §5 Evidence (J775c): Exact-head 78ebc6a42d17 row (lobata, 7.3.0-posth4-b2+, with #2574 cache topology and #2517 PMU map, 2 interleaved rounds) verifies all 33 features exposed, MEM_SPREAD defaults off, enabling logs sched: MEM_SPREAD: counting L1D load misses on 18 CPUs cleanly, STREAM Triad jumps +27% (2T 181 -> 231 GB/s), +72% (4T 179 -> 308 GB/s), and +93% (6T 165 -> 320 GB/s), 7-Zip 6T/18T stays flat, and health checks show 0 new failed units, 0 new kernel errors beyond baseline, and 0 WARNING/BUG/Oops/Call Trace.
  • §6 Naming: Pass.

Remaining Merge Gate (Blocking APPROVE)

  1. Stacked base & 0 CI runs: Base branch is still satchlj/sched-fork-exec-llc-20261119 (#2481, stacked on #2480), so statusCheckRollup has 0 check runs. Once #2480 and #2481 merge, retarget #2482 onto integration/aurora so host-tests runs green — ready for immediate APPROVE once CI passes.

(Non-blocking notes for any future default-on follow-up: note the consistent -4.8% STREAM 18T delta across both rounds [330.8 / 333.1 GB/s off -> 316.0 / 316.2 GB/s on], runtime CPU offline/online hotplug callbacks remain untested on hardware because lobata builds with CONFIG_HOTPLUG_CPU=n, and cross-SoC M1/M2/M4/A18 Pro rows with #2517 + #2574 will be needed before proposing default-on.)

@github-actions github-actions Bot added changes-requested A reviewer requested changes (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Nov 24, 2026
@github-actions github-actions Bot added the needs-template PR body lacks Tested or Risk (bot) label Nov 24, 2026

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: FINISH, then merge at 78ebc6a42d17

Blockers:

  1. kernel/sched/features.h: SCHED_FEAT(MEM_SPREAD, false): the feature only runs after a manual debugfs write, and merged features have to work on a default boot. The PR body gives the reason: sysctl_mem_spread_high/_low/_shift (kernel/sched/fair.c) were set from M5 Max cores only. Fix: derive the thresholds from something the machine reports (a PMU or cache property), or back them with rows from other cores, then turn the feature on by default.
  2. Missing row: nothing has run on this tree yet. As the body says, the STREAM and 7-Zip numbers came from private kernels with a different PMU cache map. Fix: a J775c A/B at the head on a kernel with #2517, so the L1D read-miss event counts, and with the MEM_SPREAD: counting line in dmesg.

Checked: no Apple material, no chip gates; ASYM_SPREAD is gone and the history is three commits; the u64 feature mask has a static_assert. It is still stacked on #2481 and needs #2517 to classify anything. This replaces my earlier review: the history, body and CI points in it aren't blockers.

Evidence

Read 20a6354f, c6238aac and 78ebc6a4, and the 11-20 to 11-24 thread with Ryan. The lab rows Ryan offered (M1 Pro, M2, M4 Pro, A18 Pro, with #2517) are what back a default for item 1.

@DjDeveloperr
DjDeveloperr dismissed stale reviews from themself November 24, 2026 18:55

Superseded by the review at 78ebc6a

@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Merge readiness at c1013c51: not ready yet

Check Detail
CI at head no failing: pr-text / pr-text
Review ok approved by DjDeveloperr
Requested changes ok none
Hardware row at head ok in the PR body at c1013c51
Conflicts ok none
PR text ok Risk: two-way · multi-LLC SMP systems with a working generic L1D load-miss PMU counter (no-op on single-LLC systems, which allocate zero PMU counters by default; runtime toggleable via echo NOMEMSPREAD > /sys/kernel/debug/sched/features). Holds one general-purpose PMU counter per CPU on multi-LLC machines while enabled.

Areas: none matched in CODEOWNERS

Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels.

…d-mem-spread-20261119

Signed-off-by: Satya Benson <satya@benson.earth>
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 <satya@benson.earth>
@satchlj
satchlj requested review from a team as code owners November 25, 2026 08:02
@satchlj
satchlj requested review from Acelogic and maralcbr November 25, 2026 08:02
@satchlj

satchlj commented Nov 25, 2026

Copy link
Copy Markdown
Member Author

Updated satchlj/sched-mem-spread-20261119 to c1013c516908 (forward-merged #2481 3928d792a145 with --signoff, then added c1013c516908 normalizing MEM_SPREAD by CPU clock rate and enabling it by default).

What changed at c1013c516908

  • Replaced M5 Max-specific raw misses/us (500/250) with clock- and DVFS-normalized misses per 1,000 estimated active cycles (MPKC) using cpufreq_get_hw_max_freq() (also updated via CPUFREQ_CREATE_POLICY for modular cpufreq drivers) scaled by arch_scale_freq_capacity(), with 100/50 MPKC hysteresis thresholds.
  • Gated default-on PMU counter allocation on multi-LLC topologies so single-LLC systems allocate zero PMU counters unless debugfs sample mode is explicitly enabled.
  • Exposed per-CPU max_khz in /sys/kernel/debug/sched/mem_spread/status and enabled SCHED_FEAT(MEM_SPREAD, true) by default.
  • scripts/checkpatch.pl --strict --git HEAD~1..HEAD: 0 errors, 0 warnings, 0 checks.

Exact-head hardware verification — Mac Studio M5 Max (J775c, T6050) at c1013c516908 (PASS)

  • Boot & default state (7.3.0-b110-2482+ with apple,t6050-pmu + #2574 shared L2 cache topology):
    • hw perfevents: enabled with apple_t6050_pmu PMU driver, 10 (0,000003ff) counters available
    • sched: MEM_SPREAD: counting L1D load misses on 18 CPUs
    • /sys/kernel/debug/sched/features: FORK_LOCAL ON, MEM_SPREAD ON by default
    • /sys/kernel/debug/sched/mem_spread/status: classifying 1 placing 1, cpu0–cpu17 counting, max_khz 4380000 (M cores) / 4608000 (P cores)
    • perf stat -e cycles,L1-dcache-load-misses -- true: PASS (602,660 cycles:H, 23,976 L1-dcache-load-misses:H) alongside MEM_SPREAD
    • Regression: 0 failed systemd units, 0 kernel taint, 0 WARNING/BUG/Oops/Call trace
  • Live MPKC classification snapshot:
    • During 6T stream_omp: all 6 threads classified bound 1 at 306–384 MPKC (cpu0: 318, cpu7: 306, cpu8: 326, cpu12: 376, cpu13: 384, cpu15: 307), spread 2 threads per LLC cluster; background tasks remained bound 0 (cat: 14, QSGRenderThread: 8).
    • During 6T 7z: 7z threads stayed bound 0 (rate 0); compositor/shell stayed bound 0 (22–25 MPKC).
  • Same-boot interleaved ABBA palindrome (MEM_SPREAD ON vs NO_MEM_SPREAD OFF, 2 rounds each):
Benchmark Threads MEM_SPREAD ON (ABBA runs / mean) NO_MEM_SPREAD OFF (ABBA runs / mean) Delta
STREAM Triad (GB/s) 1T 123.9 / 124.0 (124.0) 123.9 / 123.9 (123.9) +0.1%
STREAM Triad (GB/s) 2T 235.5 / 236.7 (236.1) 178.7 / 177.5 (178.1) +32.6%
STREAM Triad (GB/s) 4T 302.1 / 245.1 (273.6) 173.2 / 173.3 (173.3) +57.9%
STREAM Triad (GB/s) 6T 319.9 / 320.1 (320.0) 247.8 / 237.4 (242.6) +31.9%
STREAM Triad (GB/s) 12T 323.7 / 321.7 (322.7) 330.8 / 322.8 (326.8) -1.3%
STREAM Triad (GB/s) 18T 315.5 / 315.7 (315.6) 310.4 / 330.3 (320.3) -1.5%
7-Zip 7z b 1 (Tot MIPS) 1T 11,664 / 11,868 (11,766) 11,877 / 11,904 (11,891) -1.0%
7-Zip 7z b 1 (Tot MIPS) 6T 74,674 / 75,139 (74,907) 75,514 / 75,615 (75,565) -0.9%
7-Zip 7z b 1 (Tot MIPS) 18T 166,906 / 167,766 (167,336) 169,130 / 166,936 (168,033) -0.4%

Ready for re-review @DjDeveloperr (cc @RyanTheTide).

@satchlj
satchlj requested a review from DjDeveloperr November 25, 2026 08:03

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed full 857-line diff at c1013c516908: widening sysctl_sched_features to u64 with static_assert(__SCHED_FEAT_NR <= 64), normalizing L1D load misses by estimated active cycles (MPKC via cpufreq_get_hw_max_freq() + CPUFREQ_CREATE_POLICY notifier and arch_scale_freq_capacity()), gating default-on PMU counter allocation on mem_spread_multi_llc(), and CPU hotplug / RCU teardown are all clean. Clearing the earlier changes-requested review; holding merge behind #2481 for a second-generation multi-LLC Mac exact-head row.

@github-actions github-actions Bot added ci-failing A check failed at the PR head (bot) door:two-way Revertible or not, from Merge risk (bot) and removed changes-requested A reviewer requested changes (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) needs-template PR body lacks Tested or Risk (bot) labels Nov 25, 2026
@RyanTheTide
RyanTheTide deleted the branch satchlj/sched-fork-exec-llc-20261119 November 27, 2026 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-failing A check failed at the PR head (bot) door:two-way Revertible or not, from Merge risk (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants