How the Linux Scheduler Load Balancing Works: Domains, Groups, and Why Tasks Migrate Between CPUs
A thread that has been running on one CPU for a while does not stay there forever. When other CPUs sit idle or carry less work, the scheduler moves runnable tasks so that the machine uses its cores more evenly. That movement is load balancing. The policy that decides which task runs next on a single CPU is Completely Fair Scheduling; the mechanism that decides which tasks should move between CPUs is what this article covers.
Related reading: How Linux CFS Works, How the Linux Context Switch Works, and How NUMA Works.
The contract
Load balancing tries to keep the load of runnable tasks roughly equal across CPUs while minimizing the cost of migration. A task that has recently run on a CPU has warm caches and TLB entries. Moving it to a distant CPU discards that state and may cross a NUMA boundary, turning local memory accesses into remote ones. The scheduler therefore balances inside a hierarchy that reflects the machine’s topology: sibling hardware threads first, then cores that share a last-level cache, then the package, then NUMA nodes.
The result is not perfect global fairness at every instant. It is a continuous, approximate pull of work toward under-loaded CPUs, performed at intervals that grow as the domain covers more CPUs.
Scheduling domains and groups
Each CPU owns a chain of struct sched_domain structures. The base domain is the smallest (often the SMT siblings of a physical core). Its parent spans a larger set (the cores that share an LLC), and so on up to a domain that covers the whole machine. The parent pointer is NULL-terminated. Domain structures are per-CPU because they are updated without locks.
Inside a domain the CPUs are partitioned into struct sched_group objects arranged in a circular list. Balancing happens between groups, not between individual CPUs at every step. The load of a group is the sum of the loads of its member CPUs. Only when one group is sufficiently busier than another does the scheduler consider moving tasks across the group boundary.
Typical hierarchy on a modern machine:
- SMT domain: groups are the sibling threads of one physical core. Migration inside this domain is cheapest.
- MC (multi-core) domain: groups are individual cores that share a last-level cache.
- DIE / package domain: groups are the MC domains of one socket.
- NUMA domains: groups are entire nodes, with further levels if the machine has multi-hop NUMA distances.
Architectures can supply their own topology levels. The flags on each domain (SD_SHARE_CPUCAPACITY, SD_SHARE_PKG_RESOURCES, SD_NUMA, etc.) tell the balancing code how expensive a migration at that level is.
When balancing runs
Two main paths invoke the balancer.
Periodic balancing is triggered from the scheduler tick. sched_balance_trigger() (called from sched_tick()) raises SCHED_SOFTIRQ when the runqueue’s next-balance time has arrived. In softirq context sched_balance_domains() walks the domain chain from the base domain upward. For each domain it checks whether the rebalance interval has expired. If it has, it calls sched_balance_rq() (historically load_balance()) on that domain. Intervals are shorter for small domains and longer for large ones; a busy CPU balances less often than an idle one.
Newly-idle balancing occurs when a CPU is about to go idle and still has no runnable CFS tasks. idle_balance() (or the equivalent) attempts to pull work from other CPUs before the core sleeps. This path uses the SD_BALANCE_NEWIDLE flag and is more aggressive about filling an idle CPU.
There is also active balancing, performed by migration threads, that can push a currently running task off an overloaded CPU when ordinary pull-based balancing is insufficient.
How a balance pass works
Inside sched_balance_rq() the steps are:
should_we_balance()decides whether this CPU is responsible for balancing the domain (usually the first CPU of its group).find_busiest_group()walks the groups, computes an imbalance (using PELT load and utilization figures, capacity, and an imbalance percentage threshold), and returns the busiest group if the imbalance is large enough.find_busiest_queue()selects the busiest runqueue inside that group.detach_tasks()/move_tasks()walks the source runqueue’s CFS tasks, checkscan_migrate_task()(allowed CPUs, cache-hotness heuristics, running status), and detaches a limited number of tasks.- The detached tasks are attached to the destination runqueue.
The number of tasks examined and moved is bounded (sysctl_sched_nr_migrate and loop-break limits) so that the balance pass does not hold runqueue locks for too long. PELT (Per-Entity Load Tracking) supplies the decaying load and utilization numbers that make the imbalance calculation sensitive to recent behavior rather than instantaneous runnable counts.
Why migration is not free
A migrated task loses the cache lines and TLB entries it had on the old CPU. On a NUMA system it may also lose locality to its memory allocations. The hierarchical domains and the increasing rebalance intervals exist precisely to make cheap migrations frequent and expensive ones rare. Cache-hot tasks are preferentially left alone; the balancer prefers to move cold tasks when possible.
On hybrid cores (big.LITTLE or Intel hybrid) the capacity of each CPU is taken into account so that a high-utilization task is not left on a low-capacity core while a high-capacity core sits idle.
Misconceptions
“CFS already balances everything.” CFS maintains fairness on one runqueue. Load balancing is the separate multi-CPU mechanism that moves tasks between runqueues.
“The kernel pushes tasks from busy CPUs.” Ordinary balancing is pull-based: the under-loaded CPU pulls work. Push-style active balancing exists but is the exception.
“Tasks are free to move anywhere.” Affinity masks, NUMA policies, and the cost of crossing domain boundaries all constrain migration.
Takeaways
Load balancing distributes CFS tasks across CPUs by walking a hierarchy of scheduling domains that mirror the machine’s cache and NUMA topology. Balancing occurs between groups inside each domain, at intervals that grow with domain size. The common path is a periodic or newly-idle pull of tasks from a busier runqueue onto an under-loaded one, guided by PELT load figures and limited by migration cost heuristics. The goal is even utilization without thrashing caches or crossing expensive NUMA boundaries more often than necessary.
See also the kernel documentation at Documentation/scheduler/sched-domains.rst and the comments in kernel/sched/fair.c.