Completely Fair Scheduler

Also known as: CFS

On this page 7

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.

ElementRoleTypical value or form
vruntimeWeighted CPU time consumed by a taskNanoseconds, scaled by weight
Red-black treeOrders runnable tasks by vruntimeO(log n) insert and remove
min_vruntimeFloor used to place waking tasksMonotonically increasing per queue
Load weightShare of CPU derived from nice value1024 at nice 0
Scheduling entityUnit that CFS schedulesA thread or a group of threads
Core elements of the CFS algorithm

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 valueLoad weightShare against one nice 0 task
-2088761About 99 percent
-53121About 75 percent
0102450 percent
5335About 25 percent
1915About 1.4 percent
Selected nice values and their CFS weights

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.

SchedulerSelection ruleTypical use
O(1) schedulerPriority arrays with interactivity heuristicsKernels 2.6.0 to 2.6.22
CFSSmallest vruntime runs nextNormal tasks, 2.6.23 to 6.5
EEVDFEarliest eligible virtual deadlineNormal tasks from 6.6
SCHED_FIFO and SCHED_RRFixed real-time priority 1 to 99Low-latency control loops
SCHED_DEADLINEEarliest deadline first with runtime budgetPeriodic tasks with guarantees
CFS and related Linux scheduling approaches

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.

SymptomLikely causeTypical fix
Desktop stutters during buildsBuild threads compete without groupingEnable autogroup or renice the build
Container latency spikesCFS quota throttling in short periodsRaise cpu.max quota or remove the cap
Nice has no visible effectCPUs are not saturatedMeasure under real contention
Fair tasks freeze entirelyRunaway real-time threadSet RT throttling or fix the thread
Common CFS symptoms and fixes

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.