Skip to content

sched/fair: Treat a task that runs without sleeping on a smaller CPU as misfit - #2020

Draft
IronsideXXVI wants to merge 2 commits into
aurora-silicon:mainfrom
IronsideXXVI:j604-misfit-runtime
Draft

IronsideXXVI wants to merge 2 commits into
aurora-silicon:mainfrom
IronsideXXVI:j604-misfit-runtime

Conversation

@IronsideXXVI

@IronsideXXVI IronsideXXVI commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

What: A fair task that runs for MISFIT_SLICES (4) base slices without sleeping or migrating now counts as misfit on a CPU that has a bigger CPU in its affinity. The misfit rotation work then moves it to an idle bigger CPU, at once when one looks idle. There is no switch for this: the idle move works whether MISFIT_ROTATE is set or not, and clearing MISFIT_ROTATE turns off only the swap. Draft: per the review, this belongs on LKML with the sched maintainers, and this PR will be closed with a link once it is posted there.

Tested: On the J604 (M4, 4 performance + 6 efficiency cores), this head's two commits (bdc8ff7, same patch-id) on our kernel against the same kernel with kernel/sched at the merge base, one boot each: no gain to show and no clear loss. 10-thread copy 102.4-103.3 against 102.1-103.2 GB/s (the base is already above macOS's 97.2), 0 xruns on both, package power 0.92-0.99 of the base except video at 1.06, where the base's own two windows differ by 17 %. One guard benchmark was 0.9 % below the base. checkpatch --strict is clean on both commits, and kernel/sched/ builds with W=1 and no warnings with clang and gcc at both commits and on the base.

Risk: two-way · every Mac with mixed core sizes (all Apple Silicon). It is on for any fair task whose affinity includes a bigger CPU.

Not yet: Audio wake-to-run latency (not captured in this run), more power windows than 2 per row, and the same rows on at least one other asymmetric Mac. Then the LKML posting.

Full description

Draft. Two commits on top of this tree's misfit handling (MISFIT_ROTATE, 2d24fbc and e0bb4f5). Per the review, a version of this that is on by default belongs on LKML with the sched maintainers. This draft stays here until it is posted there, and I will close it with a link then.

What it does.

  • First commit: a fair task that runs for MISFIT_SLICES (4) base slices without sleeping or migrating counts as misfit on a CPU that has a bigger CPU in its affinity. Today a task counts as misfit only once its PELT utilization no longer fits the CPU, which for a CPU-bound thread on an M4 E-core takes about 46 ms at the top frequency. It is on by default on asymmetric-capacity systems, with no feature bit and no debugfs word. The uclamp_max, SCHED_IDLE and kthread exclusions stay.
  • Second commit: the misfit rotation work moves such a task to an idle bigger CPU. The tick queues the work at once when a bigger CPU looks idle, and otherwise after MISFIT_SLICES as before. The work picks the destination under misfit_rotate_mutex from misfit_rotate_last_cpu, the same start as a swap. A synchronous stopper moves that task by name, through can_migrate_task() and detach_task(), the way migrate_swap() names its tasks. It moves nothing when the task has slept, moved or left the fair class since the work looked, or when the target is no longer idle.
  • The idle move does not depend on MISFIT_ROTATE. Clearing MISFIT_ROTATE turns off the swap only.

Measured on this head (J604, 2026-11-24). Side A: our kernel with kernel/sched and include/linux/sched.h at this PR's merge base. Side B: A plus this PR's two commits (same patch-id as bdc8ff7), built with one test-only knob that reads MISFIT_SLICES from a sysctl; it stayed at 4, which is this head's policy. Same config and command line, one boot per side, 2 windows per row.

  • Bandwidth: 10-thread copy A 102.2 / 102.1 / 103.2, B 103.1 / 103.3 / 102.4 GB/s. The base no longer shows the 66-78 GB/s the first version was measured against, so there is no gap here for this change to close.
  • Guards (three matrix benchmarks, 3 runs each): 1-thread A 148.8-149.5, B 149.9-151.1; the second equal; the third A 18.98-19.18, B 18.82-19.00, so B's mean is 0.9 % below A's and below A's spread.
  • Package power, B / A: video 1.059, browser with JS bursts 0.938, idle desktop with a periodic task 0.922, audio 0.947-0.991. A's two video windows differ by 17 % (5048.6 and 5916.8), so the video number is inside A's own noise. The backlight was at 184 on A and 246 on B, which counts against B.
  • Audio at PipeWire quantum 64 and 128: 0 xruns on both sides. The wake-to-run latency was not captured.
  • Ping-pong (a task moved E to P and back): B at most 1.88 per second at slices 4, A at most 5.00.
  • With 2 windows per row and one boot per side, these runs cannot separate B from A on power.

checkpatch --strict is clean on both commits, and kernel/sched/ builds with W=1 and no warnings with clang and gcc at both commits and on the base.

Older numbers, not for this head. The first version of this PR (an off-by-default MISFIT_RUNTIME switch and a stopper push from the tick, both now removed) was measured on the J604 on an older base. With no placement hint, 10-thread copy went from 68.2-70.5 to 97.4-97.6 GB/s (macOS on the same machine: 97.2), and the share of that work done on performance cores from 40 % to 89 %. Those runs measured that design on that base, not this one.

Still to measure on this head, against the merge base, same kernel config and default command line: audio wake-to-run latency at small buffer sizes; more power windows per row (video, browser, idle desktop); 1-thread and 10-thread bandwidth again on a second boot per side; and no new kernel errors. On the J604 and on at least one other asymmetric Mac (M1/M2 4+4, or an M-Pro). I will post the numbers here as they come in.

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

Thanks for the measurements. 89 % of the 10-thread copy work landing on P-cores instead of 40 %, and bandwidth matching macOS with no placement hint, is a real result. Because this changes CFS on every asymmetric-capacity system, I've held it to a higher bar, and it isn't ready to merge yet.

  1. Off by default is an A/B switch. As posted, merged code would carry MISFIT_RUNTIME = false plus a debugfs tunable: a knob choosing between old and new behaviour, which we don't merge any more. It has to be on by default, and justified as the default for asym-cpucap systems, or stay out of the tree.

  2. The evidence has to cover that default. So far it's a memory-bandwidth microbenchmark plus one CPU benchmark run with the P cluster capped. Before this is the default on a laptop, we'd need:

    • battery or package power under bursty everyday loads: video playback, a browser workload with JS bursts longer than 8 ms, an idle desktop with periodic work;
    • latency-sensitive loads, such as audio at small buffer sizes;
    • a check for ping-pong, where wakeup placement keeps putting a task back on an E-core that it then leaves again 8 ms later.
  3. The 8 ms default has no stated source. Where does it come from: the PELT ramp on these cores, a macOS observation, a sweep? A value that steers placement on every asymmetric system needs one.

  4. The tick push skips the rotation path's serialisation. e0bb4f5 added misfit_rotate_mutex and misfit_rotate_last_cpu to serialise destination choice. misfit_push_idle() instead always picks the first idle CPU of the highest capacity. Several E-cores ticking close together will therefore aim at the same idle P-core until one move lands. __active_load_balance_cpu_stop() only checks that the target is active, not that it is still idle.

    To your question: yes, I'd rather the push go through the rotation work. It already finds an idle bigger CPU (that's the idle = true case, which returns -EAGAIN today). Moving the task there, under the same mutex and the same wrap start, keeps one destination policy.

  5. This belongs upstream. It's generic fair-class policy, not Apple hardware, and upstream has been working on the same problem (a push mechanism for misfit tasks under EAS). A version that's on by default should go to LKML and the sched maintainers rather than be carried here, where every rebase onto a new kernel has to bring it along.

The rest looked right to me:

  • the push runs from sched_balance_trigger() outside the rq lock;
  • it follows the active-balance pattern: active_balance set under the lock, stop_one_cpu_nowait() with preemption off;
  • the uclamp_max, SCHED_IDLE and kthread exclusions are sensible;
  • with the feature off, the existing paths are unchanged.

@IronsideXXVI

Copy link
Copy Markdown
Contributor Author

@DjDeveloperr thanks, that's a fair bar and I agree with point 5: this is generic fair-class policy and it should go to LKML and the sched maintainers, not be carried here.

What I'll do:

  • Mark this PR as a draft so it isn't merged here. I'll close it with a link once the series is on the list.
  • Rework the push to go through the rotation work, under misfit_rotate_mutex and the same wrap start, as you describe in point 4.
  • Make it on by default with no switch, and measure that default before posting: package power on video playback, a browser workload with JS bursts over 8 ms and an idle desktop with periodic work; audio at small buffer sizes; and a ping-pong count.
  • State where the 8 ms comes from, or replace it with a value I can derive, together with a sweep.

I'll post the numbers here as they come in.

@IronsideXXVI
IronsideXXVI marked this pull request as draft November 21, 2026 01:09
@IronsideXXVI

Copy link
Copy Markdown
Contributor Author

Pushed b475b38, two commits on top:

  • 19b4631: runtime misfit is on by default on asymmetric systems. The MISFIT_RUNTIME feature and the misfit_runtime_ns debugfs word are gone. The 8 ms is replaced by MISFIT_SLICES (4) base slices of execution without sleeping or migrating: four full EEVDF requests in a row, the same interval the rotation already waits before a swap. With 10 CPUs that is 11.2 ms. For comparison, PELT alone needs about 46 ms on the M4 E-cores (c = 0.48) at their top frequency before task_fits_cpu() fails. The commit message has the arithmetic.
  • b475b38: misfit_push_idle() is gone. The tick queues the rotation work at once when a bigger CPU looks idle, and otherwise after MISFIT_SLICES as before. The work picks the destination under misfit_rotate_mutex from misfit_rotate_last_cpu. It moves the task with a synchronous stop_one_cpu(), so the move completes before the next CPU chooses. The stopper (new LBF_MISFIT_IDLE) moves nothing unless the target is still idle.

checkpatch --strict is clean, and kernel/sched/ builds with W=1 and no warnings. Nothing is measured on this head yet. The power, audio and ping-pong runs you listed, plus a sweep of 2, 4, 6 and 8 slices, come next, old kernel against new on the same J604, and I'll post them here.

@RyanTheTide

Copy link
Copy Markdown
Contributor

Blockers: 1.

  1. kernel/sched/fair.c:15146-15175 (misfit_move_idle()) asks the stopper to move "one task" off src_rq, not p. Under the src_rq lock it checks only that p is still queued (15154). __active_load_balance_cpu_stop() then checks nr_running <= 1 (14156) and calls detach_one_task() (14187), which takes the first migratable task from the tail of cfs_tasks. The tick's nr_running == 1 check is older than the work. If another fair task woke on src_cpu in between, the stopper can push that task, which is not a misfit, to the idle bigger CPU. p stays on the small CPU, and the work reports -EAGAIN. The stopper's idle_cpu(target) recheck does not cover this. Move p explicitly: pass p to the stopper and run can_migrate_task()/detach_task() on p only, the way migrate_swap() names its tasks. Or at least make the stopper refuse to move anything when the detached task is not p.
  2. The history still adds and then removes the A/B switch. bccc6b6 and 9536d03 add MISFIT_RUNTIME (off), the misfit_runtime_ns debugfs word and misfit_push_idle(), and 9a16858 and 1d0415c remove them again. Every bisect point in between carries the knob and the unserialised push from the earlier review. Squash the series into the design that stays: runtime misfit after MISFIT_SLICES, and the idle move through the rotation work. The PR body also still describes the removed design ("off by default", sched_misfit_runtime_ns, a stopper push from the tick), and its table measures that design, not this head.

Nit: the idle move now runs only inside misfit_rotate_work(), which returns -ECANCELED when MISFIT_ROTATE is off (fair.c:15192-15194). task_misfit_runtime() still marks tasks as misfit in that case, so the runtime-misfit behaviour silently depends on a second feature bit. Either say so in the commit message, or keep the idle move outside that check.

What I checked and found sound: the uclamp_max, SCHED_IDLE and kthread exclusions in task_misfit_runtime(); the burst_runtime reset on fork, wakeup and migration; LBF_MISFIT_IDLE (0x80) does not collide with the other LBF flags; the stopper error path clears active_balance; and the per-CPU kworker that waits in stop_one_cpu() cannot itself be detached (kthread_is_per_cpu). CI host-tests passes, and the commits carry a human Signed-off-by with no agent trailers.

HW row: this head (after the fix), default command line, on the J604 and on at least one other asymmetric Mac class (M1/M2 4+4, or an M-Pro with 2 or 4 E-cores): the 10-thread and 1-thread bandwidth A/B against the merge base, plus the power, audio and ping-pong rows already requested in this thread; 0 AVC, 0 failed units, no new kernel errors.

Assisted-by: LLM (Claude:Opus 5.5)

…as misfit

A task becomes a misfit, and can be pulled up by an idle bigger CPU or
rotated by MISFIT_ROTATE, only once task_fits_cpu() fails, i.e. once its
PELT utilization exceeds 80 % of the smaller CPU's capacity. From zero
utilization, a task that runs without a break on a CPU of capacity c (in
1/1024) at its highest frequency gets there after
32 / c * log2(1 / (1 - 0.8 c)) ms. On the M4 (T8132) the E-cores have
c = 754 * 2892 MHz / (1024 * 4464 MHz) = 0.48, so that is about 46 ms at
their top frequency, and longer at lower ones. Work that lasts less than
that never moves.

On the M4 MacBook Pro (4 P-cores, 6 E-cores) a 10-thread memory
bandwidth test that splits 1 GiB into equal slices finishes the P-core
slices at 11 ms and the E-core slices at 32 ms, with 0-4 migrations:
66-78 GB/s, against 97.2 GB/s for the same source on macOS on the same
machine. A work queue over the same arrays reaches 95.8 GB/s, and
uclamp_min 1024 on the test's threads 103 GB/s, so the loss is
placement, not the memory system.

A fair task that has run for MISFIT_SLICES (4) base slices without
sleeping or migrating, on a CPU smaller than its maximum allowed
capacity, now counts as misfit as well. A task that has used four full
EEVDF requests in a row is CPU-bound by the scheduler's own measure. It
is the interval misfit rotation already waits before a swap, which now
uses the same constant, and it scales with the base slice and the CPU
count as the slice does. With 10 CPUs the base slice is 2.8 ms, so the
threshold is 11.2 ms. There is no feature bit and no tunable for it.

The existing misfit balancing then moves the task to a bigger CPU with
spare capacity, and MISFIT_ROTATE shares the bigger CPUs once they are
all busy. The checkpoint is reset on fork, wakeup and migration. Kernel
threads, SCHED_IDLE tasks and tasks whose uclamp_max is at or below the
smaller CPU's capacity are left alone. On a CPU of the task's maximum
allowed capacity, as on every CPU of a symmetric system, nothing
changes.

Signed-off-by: Dylan Ironside <[email protected]>
…tion work

When a misfit task finds an idle bigger CPU, misfit rotation returns
-EAGAIN and leaves the move to that CPU's next newly-idle or nohz idle
balance, which is skipped by chance (NI_RANDOM) or waits for the
domain's balance interval. A runtime misfit can sit on its small CPU for
that long while the bigger CPU idles.

Let the rotation work make the move:
- the tick queues the work at once when a bigger CPU that the task may
  use looks idle, and otherwise after MISFIT_SLICES of misfit execution
  as before;
- the work picks the destination under misfit_rotate_mutex from
  misfit_rotate_last_cpu, the same round-robin start as a swap, so
  smaller CPUs that tick close together never aim at the same idle CPU;
- it moves the task with a synchronous stop_one_cpu(), so the move
  completes before the next destination is chosen;
- the stopper is given the task and moves that task only, through
  can_migrate_task() and detach_task(), as migrate_swap() names its
  tasks. It moves nothing when the task has slept, moved or left the
  fair class since the work looked, or when the target is no longer
  idle. A task that woke on the smaller CPU in the meantime is never
  moved in its place;
- rq->active_balance is held over the move, so load balancing does not
  queue its own stopper on that CPU meanwhile.

The move does not depend on MISFIT_ROTATE: a task that task_is_misfit()
marks gets it whether the feature is set or not. Clearing MISFIT_ROTATE
turns off the swap only, and the work then reports -ECANCELED instead
of swapping.

There is one destination policy for both cases now.

Signed-off-by: Dylan Ironside <[email protected]>
@IronsideXXVI

Copy link
Copy Markdown
Contributor Author

@RyanTheTide thanks. Pushed bdc8ff7, which replaces the four commits with two.

  1. misfit_move_idle() now moves p by name. The work passes p, the source CPU and the target to its own stopper, misfit_move_cpu_stop(). Under the source rq lock it checks that both CPUs are still active, that the target is still idle, and that p is still queued on that rq in the fair class. It then runs can_migrate_task() and detach_task() on p only, and attach_one_task() on the target, the way migrate_swap() names its tasks. If any check fails it moves nothing and the work reports -EAGAIN. A task that woke on the small CPU in between is never moved in p's place. LBF_MISFIT_IDLE and the extra entry into __active_load_balance_cpu_stop() are gone, so that function is the same as on the base again.

  2. Squashed. e327dea adds runtime misfit after MISFIT_SLICES, on by default, with no feature bit and no debugfs word. bdc8ff7 adds the idle move through the rotation work. No bisect point carries the knob or the tick push now. I rewrote the PR body for this head. It says the old table measured the removed design and is not evidence for this one.

Nit: I kept the idle move outside the MISFIT_ROTATE check. With MISFIT_ROTATE cleared, the work still moves a misfit to an idle bigger CPU and returns -ECANCELED only where it would swap. The second commit message and the comment over the rotation code in fair.c say so.

What I ran: checkpatch --strict is clean on both commits. kernel/sched/ builds with W=1 and no warnings with clang and gcc at both commits, the same as on the base. The host tests show the same results as on the base; none of them cover kernel/sched.

What I have not run: nothing is measured on this head yet. The J604 run is set up: this head against the merge base, default command line, with the bandwidth, power, audio and ping-pong rows from this thread. I've also asked for the same rows on an M1/M2 or an M-Pro. I'll post the numbers here.

@RyanTheTide

Copy link
Copy Markdown
Contributor

This still applies cleanly to the current integration/aurora tip 110861f. The tip has the misfit rotation (MISFIT_ROTATE) but no runtime-based misfit. The 11-21 head bdc8ff7 moves p by name in its own stopper and squashes the series to two commits, which covers both points from the last review; it hasn't been re-reviewed yet. No measurements have been posted for this head. The power, audio-latency and ping-pong rows and the slice sweep are what's left before it can be judged, here or on the list.

Build check of 110861f plus this change (arm64, LLVM=1, Image, dtbs and every directory it touches, Fedora config plus asahi.config): builds, no warnings in the files it touches.

@IronsideXXVI IronsideXXVI changed the title sched/fair: Treat a task that runs without sleeping on a smaller CPU as misfit (MISFIT_RUNTIME, off by default) sched/fair: Treat a task that runs without sleeping on a smaller CPU as misfit 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 before any merge here. It's a draft for LKML, which is the right home for a fair-class policy change.

Blockers: none in the code. The points from my last review are fixed. Runtime misfit is on by default with no switch. MISFIT_SLICES names the 4 base slices the rotation code already used. The idle move now goes through the rotation work under misfit_rotate_mutex, and it moves p by name only while the target is still idle.

Rows before merging here: this changes CFS on every Mac with mixed cores, so it would need your "Not yet" items: audio wake-to-run latency, more power windows per row, and the same rows on a second asymmetric Mac from another generation.

Checked: thank you for re-measuring on the current base and saying plainly that it no longer shows a gain there. Ping-pong went down, to at most 1.88 per second against the base's 5.00.

@DjDeveloperr
DjDeveloperr dismissed their stale review November 26, 2026 06:47

Superseded by the review at bdc8ff7

@RyanTheTide

Copy link
Copy Markdown
Contributor

@IronsideXXVI checking open drafts today. To come out of draft, this still needs:

  • A test run at the current head on a Mac with the hardware it touches, then mark it ready for review.

If it's no longer needed, say so here and we'll close it. Otherwise it stays open as a draft until these are done.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants