The Completely Fair Scheduler (CFS) is the Linux kernel process scheduler that divides CPU time among runnable tasks in proportion to their weights, approximating an ideal processor that runs every task simultaneously at an equal share. In Linux, it served as the default scheduler for normal (SCHED_OTHER) processes from kernel 2.6.23 in 2007 until kernel 6.6 in 2023, when the EEVDF scheduler replaced its core selection logic.
CFS was written by Ingo Molnar to replace the O(1) scheduler, whose heuristics for detecting interactive tasks were difficult to tune and easy to fool. Instead of fixed time slices and priority arrays, CFS tracks how much CPU time each task has received and always runs the task that is furthest behind its fair share.
How CFS tracks fairness with virtual runtime
CFS measures fairness through virtual runtime (vruntime), a per-task counter of weighted CPU time in nanoseconds. When a task runs, its vruntime grows by the real time consumed multiplied by the ratio of the default weight (1024) to the task’s own weight. A heavily weighted task therefore accumulates vruntime slowly and receives more real CPU time before it is preempted.
Runnable tasks are kept in a red-black tree ordered by vruntime. The leftmost node, which is cached for constant-time access, holds the task with the smallest vruntime, and that task runs next. Insertion and removal cost O(log n), which keeps scheduling overhead predictable even with thousands of runnable threads.
A newly woken or newly created task does not start at zero. CFS places it near the run queue’s min_vruntime, a monotonically increasing floor, so that a task that slept for an hour cannot monopolize the CPU when it wakes. A small sleeper credit still lets interactive tasks run promptly after waking.
The following table summarizes the core data structures and values that drive the algorithm.
| Element | Role | Typical value or form |
|---|---|---|
| vruntime | Weighted CPU time consumed by a task | Nanoseconds, scaled by weight |
| Red-black tree | Orders runnable tasks by vruntime | O(log n) insert and remove |
| min_vruntime | Floor used to place waking tasks | Monotonically increasing per queue |
| Load weight | Share of CPU derived from nice value | 1024 at nice 0 |
| Scheduling entity | Unit that CFS schedules | A thread or a group of threads |
Nice values, weights and time slices
In CFS, the nice value of a process maps to a load weight through a fixed table in the kernel. Each nice step changes the weight by a factor of about 1.25, which translates into roughly a 10 percent change in CPU share relative to a competing task at the neighboring level. The range runs from nice -20, the highest weight, to nice 19, the lowest.
CFS does not use fixed time slices. It computes a targeted scheduling latency, the period in which every runnable task should run once, and divides it among tasks by weight. When too many tasks are runnable, the period stretches so that each task still receives at least the minimum granularity, which prevents excessive context switching.
The table shows representative weights from the kernel table and the resulting share against a single nice 0 competitor.
| Nice value | Load weight | Share against one nice 0 task |
|---|---|---|
| -20 | 88761 | About 99 percent |
| -5 | 3121 | About 75 percent |
| 0 | 1024 | 50 percent |
| 5 | 335 | About 25 percent |
| 19 | 15 | About 1.4 percent |
Default tunables such as sched_latency_ns of 6 ms and sched_min_granularity_ns of 0.75 ms scale with the logarithm of the CPU count. Recent kernels moved these knobs from sysctl to debugfs, which signals that they are diagnostic settings rather than supported tuning interfaces.
Group scheduling and multiprocessor balancing
Group scheduling extends fairness from individual threads to collections of threads. With CONFIG_FAIR_GROUP_SCHED enabled, each cgroup receives its own scheduling entity and run queue, and CPU time is divided first between groups and then within each group. The cgroup v2 attribute cpu.weight, with a default of 100, sets a group’s relative share.
The autogroup feature applies the same idea automatically to terminal sessions, so that a parallel compile in one shell does not starve an interactive desktop session. Systemd achieves a similar effect by placing services and user sessions in separate cgroups.
On multiprocessor systems, every CPU has its own CFS run queue. Load balancing periodically moves tasks between queues along scheduling domains that mirror the hardware topology: hyperthread siblings, cores sharing a cache, and NUMA nodes. Balancing within a cache domain is cheap and frequent, while migration across NUMA nodes is conservative because it destroys cache and memory locality.
The kernel also offers bandwidth control. Through cpu.max in cgroup v2, an administrator sets a quota per period, such as 50 ms per 100 ms, which hard-caps a group at half a CPU regardless of idle capacity.
CFS compared with other Linux schedulers
CFS is one of several scheduling classes that the kernel consults in strict priority order. Real-time and deadline classes always run before CFS tasks, so a misbehaving real-time thread can starve the entire fair class. The comparison below places CFS among its predecessor, its successor and its sibling classes.
| Scheduler | Selection rule | Typical use |
|---|---|---|
| O(1) scheduler | Priority arrays with interactivity heuristics | Kernels 2.6.0 to 2.6.22 |
| CFS | Smallest vruntime runs next | Normal tasks, 2.6.23 to 6.5 |
| EEVDF | Earliest eligible virtual deadline | Normal tasks from 6.6 |
| SCHED_FIFO and SCHED_RR | Fixed real-time priority 1 to 99 | Low-latency control loops |
| SCHED_DEADLINE | Earliest deadline first with runtime budget | Periodic tasks with guarantees |
The EEVDF scheduler keeps the vruntime and weight machinery of CFS but adds lag and virtual deadlines, which give latency-sensitive tasks a principled way to run sooner without receiving more total CPU time. Much of the documentation and many tuning guides still describe CFS, so understanding it remains essential for reading kernel behavior on older systems.
Limitations and common misconceptions
CFS guarantees proportional fairness, not low latency. Its heuristics for wakeup preemption, such as the wakeup granularity, were added to favor interactive tasks and grew complex over time, which was a central motivation for EEVDF.
- Fair does not mean equal: tasks with different nice values or cgroup weights deliberately receive unequal shares.
- Weights only matter under contention: an idle system gives a nice 19 task a full CPU.
- Per-thread fairness can surprise: without group scheduling, a program with 50 threads receives far more CPU than one with a single thread.
- Bandwidth throttling can add latency: a tight cpu.max quota can pause a multithreaded service for the rest of a period.
The table lists frequent symptoms that administrators attribute to CFS, together with their usual cause and remedy.
| Symptom | Likely cause | Typical fix |
|---|---|---|
| Desktop stutters during builds | Build threads compete without grouping | Enable autogroup or renice the build |
| Container latency spikes | CFS quota throttling in short periods | Raise cpu.max quota or remove the cap |
| Nice has no visible effect | CPUs are not saturated | Measure under real contention |
| Fair tasks freeze entirely | Runaway real-time thread | Set RT throttling or fix the thread |
Put simply
The Completely Fair Scheduler is the part of Linux that decides which program gets the processor next. It keeps a running tally of how much processor time every program has used, and it always hands the processor to the one that has had the least so far.
Think of a teacher with one microphone in a busy classroom. The teacher notes how long each student has spoken and passes the microphone to whoever has spoken least. Students marked as more important get a bigger allowance, but everyone eventually gets a turn.
In practice
A system administrator runs a Linux build server with 16 cores that also hosts a small web dashboard used by the development team. During nightly compiles, developers complain that the dashboard responds slowly. Inspection with top shows 64 compiler threads at nice 0 competing with four web server threads, so under CFS the dashboard receives only a small fraction of the CPU.
The administrator places the compiler jobs in a systemd slice with cpu.weight set to 20 and leaves the web service at the default of 100. Because CFS now divides time between groups before threads, the web service receives about five times the share of the build group whenever both are busy. Dashboard response times return to normal, while builds still use every idle cycle and finish only a few minutes later.