How the Linux scheduler deals with many cores
Summary
On Hackaday, Maya Posch puts into context a blog's claim that the Linux scheduler has been accidentally limited to eight cores for fifteen years. In fact the kernel only limits the factor by which the length of time slices grows with the number of CPUs, and has done so deliberately since 2009. Linux still uses any number of cores; the limit only affects how finely it switches between tasks.
What the limit actually controls
The scheduler code in fair.c contains min_t(unsigned int, num_online_cpus(), 8). From this the kernel calculates a factor, logarithmic by default as 1 + ilog2(cpus), that scales the base time slice. More than eight CPUs do not increase this factor any further; it stays at no more than 4.
In the current kernel the base time slice is 0.70 ms times this factor, so at most 2.8 ms. The number of cores that tasks are spread across is not affected.
Why the limit makes sense
On the Linux kernel mailing list the limit was justified in November and December 2009: with few cores, frequent switches are needed for concurrency to remain noticeable. With many cores more runs in parallel anyway, and from about eight CPUs the cost of switching outweighs the gain.
The episode shows how hard good schedulers are. Since Linux 6.6, EEVDF has replaced the Completely Fair Scheduler as the default; the scaling over at most eight CPUs was kept.
Ideas
- The eight-core limit concerns the time slice, not the number of usable cores.
- By default the time slice grows logarithmically with the number of CPUs.
- Few cores need finer switching so the system feels responsive.
- From about eight CPUs the cost of task switching dominates.
- A look at the mailing list clarifies the intent behind old code.
- EEVDF replaced CFS, the scaling rule remained.
Insights
- Sensational headlines about kernel code deserve a check against the source.
- A fixed limit in code is not automatically a bug but often a documented trade-off.
- The reasoning behind a decision often lives on mailing lists, not in comments.
Facts
- fair.c limits the CPU count used for scaling with min_t(..., num_online_cpus(), 8).
- The default scaling is logarithmic: factor = 1 + ilog2(cpus).
- In the current kernel the base time slice is 0.70 ms before scaling.
- The scaling mode can be set via debugfs under sched/tunable_scaling.
References
- Hackaday: Linux Scheduler and More Cores
- Linux source: kernel/sched/fair.c
- LKML, November 2009: discussion of the limit (Web Archive)
Remarks
- The original post appeared in November 2023 on the blog “The HFT Guy”; the discussions on Hacker News and the Level1Techs forum quickly cleared things up.
Recommendations
- Check claims about kernel limits against the current source, not against headlines.
- Change scheduler parameters only with measurements of your own workload.
- For conspicuous kernel code, read the related discussion on the mailing list.
Found a mistake or have something to add? Get in touch.
Links to the original source and the Web Archive open in a new tab.