What Makes System Calls Expensive: A Linux Internals Deep Dive
15 hours ago
- #Linux
- #Performance
- #System Calls
- System calls in Linux are expensive due to both direct and indirect overheads.
- Direct overhead includes the kernel entry/exit path, which involves switching page tables, stacks, and saving/restoring registers.
- Indirect overhead comes from microarchitectural side-effects like pipeline draining, branch predictor state loss, and speculative execution mitigations.
- The vDSO (virtual dynamic shared object) provides a user-space shortcut for certain system calls to avoid kernel entry overhead.
- Benchmarks show that vDSO-based calls (e.g., clock_gettime) are significantly faster than traditional system calls.
- System calls disrupt CPU optimizations such as instruction pipelining and branch prediction, leading to performance degradation.
- Mitigations against speculative execution attacks (e.g., Spectre, Retbleed) add overhead by clearing branch history and return stack buffers.
- Practical strategies to reduce system calls include using vDSO, caching values, batching I/O operations (e.g., io_uring), and leveraging eBPF for kernel-side processing.
- The true cost of system calls extends beyond kernel execution time, impacting CPU state recovery and optimization rebuilding.