Skip to content

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

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

satchlj wants to merge 3 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

Claude Opus 5.5 on @satchlj's behalf.

Update after review (2026-11-24):

Stacked on #2481 (fork/exec placement), which is stacked on #2480. Review the last three commits:

  1. sched: Make sysctl_sched_features a u64. The new features exceed 32 bits (__SCHED_FEAT_NR == 33 with HAVE_RT_PUSH_IPI); widening to u64 with 1ULL shifts and static_assert(__SCHED_FEAT_NR <= 64) keeps both 32-bit and 64-bit SMP builds clean.
  2. sched/fair: Spread memory-bound tasks across LLCs (MEM_SPREAD). Classifies tasks from a pinned per-CPU L1D-read-miss counter and spreads only memory-bound tasks across LLCs. Off by default.
  3. sched/fair: Set up the MEM_SPREAD counters at boot and across CPU hotplug. Sets up the counters lazily when enabled at boot (if (mem_spread_wanted())) and tracks CPU hotplug.

The commit messages carry the design and the thresholds. Without a PMU that counts L1D read misses, nothing is classified.

It depends on the LLC topology, like #2481: on T6050 it needs the CPU cache DT (#2574).

Evidence (J775c, Mac Studio M5 Max, earlier private lobata kernels with the cache DT and a PMU cache map)

  • MEM_SPREAD, toggled in the same boot (2026-11-06): STREAM OpenMP 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: about 79.1k at 6 threads, and 188.6k vs 187.5k at 18.
  • With it on by default (2026-11-06): triad with 2–7 threads gave 252 / 354 / 357 / 359 / 384 / 385 GB/s. macOS on the same machine (2026-11-05) gave 248 / 319 / 342 / – / 350.
  • Caveats:
    • The CPU-hotplug path has not been exercised on hardware yet.
    • One 16-thread sample dropped from 362 to 319 GB/s; a 3-round repeat showed it was noise.
    • The feature holds one general-purpose PMU counter per CPU while it is on.

Testing

Assisted-by: LLM (Anthropic:Claude Opus 5.5, Google:Gemini 4 Neon)

@RyanTheTide

Copy link
Copy Markdown

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

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

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

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.

Assisted-by: LLM (Gemini 4 Neon)

@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

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.

Assisted-by: LLM (Gemini 4 Neon)

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

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.

Assisted-by: LLM (Gemini 4 Neon)

@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

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.)

Assisted-by: LLM (Gemini 4 Neon)

@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants