What is the Linux kernel scheduling (CFS)?

Answer

The Completely Fair Scheduler (CFS) is Linux's default CPU scheduler (since kernel 2.6.23), replacing the O(1) scheduler. CFS aims to give each runnable task a fair share of CPU time — as if we had an ideal multi-tasking processor. Core concept — virtual runtime (vruntime): CFS tracks how much CPU time each task has consumed via a "virtual runtime" counter. The task with the smallest vruntime gets to run next — it's the task that has received the least CPU time relative to its weight (priority). vruntime increases faster for tasks with lower priority (nice level). Red-black tree: CFS stores tasks in a red-black tree ordered by vruntime. The leftmost node (smallest vruntime) is always the next task to run — O(log n) to find, O(1) cached in min_vruntime. Weight (nice levels): each task has a weight based on its nice value (-20 to +19). Nice -20 (highest priority) = 9× more weight than nice 0. Weight determines how fast vruntime increases: high-priority tasks accumulate vruntime slowly (run longer before being preempted). Scheduling latency: the target period for all tasks to run at least once. Default: 6ms for ≤8 tasks; scales with task count. Minimum granularity: 0.75ms (prevents too-frequent context switches). Load balancing: CFS balances load across CPUs and NUMA nodes. Periodically migrates tasks to underloaded CPUs. Control groups (cgroups): CFS supports hierarchical scheduling via cgroups — limit CPU percentage per group (containers, user sessions). EEVDF (Earliest Eligible Virtual Deadline First): scheduled to replace CFS in Linux 6.6+ — more responsive, better for latency-sensitive workloads.