A Deep Dive into Linux System Implementation from Hardware to User Space
Introduction
- Why Look Inside the Kernel
- What This Book Covers and How to Read It
- Anchor Version, Architecture and Source References
- Prerequisites
- How to Use the Code Examples
- A Note on Accuracy and Scope
- Acknowledgments
Chapter 1: The Kernel and Its World
- The Kernel as a Resource Manager
- Hardware Abstraction and the System Call Interface
- Monolithic vs Microkernel: Design Choices in Linux
- Navigating the Kernel Source Tree
- Building and Booting a Custom Kernel
- The Version History That Matters
Chapter 2: CPU Privilege Levels and Hardware Boundaries
- Ring 0 and Ring 3 on x86-64
- The x86-64 Protected Mode Transition
- The Memory Management Unit and Page Tables
- ARM64 Exception Levels and Virtualization Extensions
- Context Registers, Control Registers and MSR Space
- How Hardware Enforces Isolation
Chapter 3: Interrupts, Exceptions and Traps
- Hardware Interrupts and the APIC
- The Interrupt Descriptor Table and IDT Handlers
- Exceptions: Page Faults, General Protection Faults and More
- System Calls as Software-Generated Traps
- The Interrupt Handler Lifecycle and Context
- Interrupt Latency, Disabling and Masking
Chapter 4: Concurrency Primitives and Synchronization
- Atomic Operations and the CPU Memory Model
- Compiler Barriers vs CPU Barriers
- Spinlocks: When and Why They Work
- Mutexes, Semaphores and Sleeping Lockes
- Read-Copy-Update: Lock-Free Kernel Concurrency
- Wait Queues and Event Signaling
Chapter 5: The Task Struct and Process Lifecycle
- Anatomy of the task_struct
- Fork, Clone and vfork: Copying and Sharing
- execve: Loading a New Program Image
- Process States: Running, Sleeping, Stopped, Zombie
- Signal Delivery and Handling
- PID Namespaces and Container Isolation
Chapter 6: ELF Loading and Dynamic Linking
- ELF File Format: Headers, Sections and Segments
- The execve System Call Path
- Program Headers and Segment Mapping
- Dynamic Linking and the ELF Interpreter
- Symbol Resolution and Relocations
- Building a Minimal ELF Loader
Chapter 7: Threads, Futexes and POSIX Threading
- Threads as Lightweight Processes
- The clone System Call and Shared Resources
- Futexes: Fast Userspace Mutexes
- How pthreads Maps to Kernel Primitives
- Thread Pools and Work Queues in the Kernel
- Detached Threads and Cleanup
Chapter 8: Process Scheduling and Context Switching
- The Scheduling Class Framework
- The Completely Fair Scheduler and CFS Bands
- Load Balancing Across CPUs and NUMA Nodes
- Context Switching: The Complete Path
- Real-Time Schedulers: SCHED_FIFO and SCHED_RR
- Scheduler Tracing with ftrace and perf
Chapter 9: Physical Memory and Page Allocation
- Boot-Time Memory Discovery and Zone Setup
- The Buddy System for Page Allocation
- The Slab and Slub Allocators
- NUMA-Aware Allocation and Policy
- Memory Cgroups and Per-Cgroup Limits
- Memory Reclaim and the OOM Killer
Chapter 10: Virtual Memory and Address Translation
- User Virtual Address Space Layout on x86-64
- Four-Level Page Tables and CR3 Switching
- Page Table Entry Formats and Flags
- VM Areas and the mm_struct
- The Kernel Virtual Address Space and KASAN
Chapter 11: Page Faults, Copy-on-Write and File Mapping
- The Page Fault Handler Path
- Demand Paging and Lazy Allocation
- Copy-on-Write in fork and mmap
- mmap: Mapping Files into Address Space
- Shared Memory and shm_unlink
- Transparent Huge Pages
Chapter 12: The Page Cache and Memory Reclaim
- The Page Cache Architecture
- Page Fault to Cache: The Read Path
- Dirty Pages and Writeback
- The Reclaim Scanner and LRU Lists
- Swap and Swap Cache
- OOM Handling and Process Selection
Chapter 13: The Virtual Filesystem Layer
- The VFS Abstraction and Its Interfaces
- Inodes, Superblocks and Filesystem Mounts
- The Dentry Cache and Path Resolution
- File Descriptors and the fdtable
- File Operations and the file Struct
- Tracing VFS Operations with ftrace
Chapter 14: Ext4 and Modern Filesystems
- Ext4 Block Groups and Metadata
- Extents and Allocation Strategies
- Journaling: Write-Ahead Logging in ext4
- Directory Hashing and htree Indexing
- XFS Allocation Groups and Extents
- Btrfs Copy-on-Write and Subvolumes
Chapter 15: Block I/O and the I/O Scheduler
- The Block Layer Architecture
- Bio Structures and Block Request Paths
- Request Queues and I/O Completion
- I/O Schedulers: mq-deadline, bfq, none
- Direct I/O and Bypassing the Page Cache
- NVMe and Modern Block Device Drivers
Chapter 16: Device Drivers and DMA
- Driver Model: Devices, Buses and Drivers
- Memory-Mapped I/O and I/O Ports
- DMA Controllers and Scatter-Gather Lists
- Interrupt Handling in Drivers
- Power Management and Runtime PM
- Writing a Minimal Character Device Driver
Chapter 17: Networking Stack Architecture
- The Netdev Subsystem and Packet Reception
- NAPI and Interrupt Coalescing
- Protocol Demultiplexing and the Socket Layer
- Routing Table Lookup and FIB
- The Network Namespace Isolation Model
- Traffic Control and Qdisc Queuing
Chapter 18: TCP/IP Implementation Internals
- The TCP Control Block and Socket Memory
- Connection Establishment and the Three-Way Handshake
- Congestion Control Algorithms in Linux
- Retransmission and Fast Recovery
- TCP Window Scaling and Buffer Autotuning
- TCP States and the Timeout Machinery
Chapter 19: Sockets, Epoll and I/O Multiplexing
- The Socket Abstraction and Protocol Families
- The Socket Buffer and Data Copy Paths
- epoll vs poll vs select: Internal Mechanisms
- Epoll Ready Lists and Edge-Triggered Mode
- Zero-Copy Networking and sendfile
- Socket Options and Tuning
Chapter 20: Capabilities, Credentials and LSM
- Traditional Unix Permissions and UID/GID
- Capabilities: Breaking All-or-Nothing Root
- The Credential Struct and Subjective/Objective Credentials
- The LSM Framework and Hook Points
- SELinux Policy and Mandatory Access Control
- AppArmor Profiles and Confinement
Chapter 21: Namespaces and Process Isolation
- The Namespace Framework Architecture
- PID Namespaces and Container PIDs
- Mount Namespaces and Rootfs Isolation
- Network Namespaces and Virtual Interfaces
- User Namespaces and UID Mapping
- Combining Namespaces for Container Isolation
Chapter 22: Control Groups and Resource Limiting
- cgroups v1 to v2: Design Evolution
- The cgroup Subsystem Hierarchy
- CPU Controller and Scheduling Weights
- Memory Controller: Limits, Accounting and OOM
- I/O Controller and Block Throttling
- PIDs Controller and Container Limits
Chapter 23: Kernel Modules and Dynamic Loading
- The Module Struct and Symbol Tables
- insmod and modprobe: Loading Path
- Symbol Resolution and Export Macros
- Module Versioning and KABI
- Hot-Pluggable Drivers and uevent
- Writing a Hello World Kernel Module
Chapter 24: Tracing, Observability and eBPF
- ftrace and the Function Graph Tracer
- Kernel Tracepoints and Static Probes
- perf Events and Hardware Counters
- kprobes and Dynamic Tracing
- eBPF: Safe Kernel Extension
- eBPF Maps, Probes and Observability Use Cases
Chapter 25: Performance Analysis and Debugging
- Profiling User Space and Kernel with perf
- Lock Contention and Spinlock Analysis
- Page Fault Analysis and TLB Shootdowns
- Memory Leak Detection with kmemleak and kasan
- Kernel Panics, Oops and Crash Dump Analysis
- Bisection and Debugging with bisect