Linux Kernel Architecture
Understanding the architecture of the Linux kernel is the foundation for everything else in this course. This module covers how the kernel is organized, why certain design decisions were made, and how to navigate the massive codebase.Interview Frequency: Very High
Key Topics: Monolithic design, kernel source navigation, boot process, modules
Time to Master: 10-12 hours
Key Topics: Monolithic design, kernel source navigation, boot process, modules
Time to Master: 10-12 hours
| Performance | Direct function calls | IPC overhead for every operation | | Latency | Lower (no message passing) | Higher (context switches) | | Complexity | Harder to maintain safety | Cleaner separation | | Reliability | One bug can crash kernel | Components isolated | | Development | Faster prototyping | More engineering overhead |
Interview Insight: Linux uses a “modular monolithic” approach — monolithic core with loadable modules. This provides the performance of monolithic with some flexibility of microkernel.
The Famous Torvalds-Tanenbaum Debate
In 1992, Linus Torvalds and Andrew Tanenbaum debated kernel design:- Tanenbaum: Microkernels are the future; monolithic is obsolete
- Torvalds: Performance matters; Linux’s approach is pragmatic
Kernel Source Tree Organization
The Linux kernel source is massive (30+ million lines), but well-organized:Key Files to Know
| File | Purpose |
|---|---|
init/main.c | Kernel entry point (start_kernel()) |
arch/x86/entry/entry_64.S | System call entry point |
kernel/sched/core.c | Scheduler core |
mm/page_alloc.c | Page allocator (buddy system) |
mm/slab.c | Slab allocator |
fs/read_write.c | read/write system calls |
net/core/dev.c | Core networking |
Boot Process Deep Dive
Understanding how Linux boots is essential for systems engineers:start_kernel() - The Heart of Boot
Key Boot Parameters
| Parameter | Purpose | Example |
|---|---|---|
root= | Root filesystem device | root=/dev/sda1 |
init= | First user process | init=/bin/bash |
quiet | Suppress boot messages | quiet |
debug | Enable debug messages | debug |
nokaslr | Disable KASLR | nokaslr |
isolcpus= | Isolate CPUs from scheduler | isolcpus=2,3 |
nosmp | Disable SMP | nosmp |
Kernel Address Space Layout
On x86-64, the virtual address space is split between user and kernel:KASLR (Kernel Address Space Layout Randomization)
KASLR randomizes kernel addresses at boot for security:Loadable Kernel Modules
Modules allow extending the kernel without recompiling:Module Structure
Module Loading Process
Module Management Commands
Module Parameters
Kernel Threads
Kernel threads (kthreads) are processes that run entirely in kernel mode:Important Kernel Threads
Lab Exercises
Lab 1: Navigate Kernel Source
Lab 1: Navigate Kernel Source
Lab 2: Build and Load Module
Lab 2: Build and Load Module
Objective: Write, compile, and load a kernel module
Lab 3: Analyze Boot Process
Lab 3: Analyze Boot Process
Objective: Understand boot timing and initialization
Interview Questions
Q1: Why is Linux monolithic, and what are the trade-offs?
Q1: Why is Linux monolithic, and what are the trade-offs?
Answer:Linux chose monolithic design for performance:
- Direct function calls between subsystems (no IPC overhead)
- Single address space eliminates context switch on internal calls
- Simpler data sharing between components
- A bug in any component can crash the entire kernel
- Larger attack surface (all code runs privileged)
- More complex codebase to maintain
- Loadable modules for flexibility
- Namespaces and cgroups for isolation
- Seccomp for syscall filtering
- Strong code review process
Q2: Walk through what happens when you type 'ls' and press Enter
Q2: Walk through what happens when you type 'ls' and press Enter
Answer (kernel perspective):
- Shell process:
fork()→ creates child process (clone syscall)execve("/bin/ls")→ replaces process image
- execve processing:
- Kernel opens ELF binary
- Maps code/data sections into memory
- Sets up stack with arguments/environment
- Loads dynamic linker (ld.so)
- ls execution:
- Dynamic linker loads libc
- ls calls
opendir()→getdents64syscall - Kernel reads directory entries from filesystem
- ls calls
write()→ output to terminal
- Termination:
- ls calls
exit()→exit_groupsyscall - Kernel cleans up resources
- Parent shell’s
wait()returns
- ls calls
Q3: How do kernel modules differ from user-space shared libraries?
Q3: How do kernel modules differ from user-space shared libraries?
Q4: Explain KASLR and its security benefits
Q4: Explain KASLR and its security benefits
Answer:KASLR (Kernel Address Space Layout Randomization):
- Randomizes kernel base address at each boot
- Makes it harder to exploit memory corruption vulnerabilities
- Attacker can’t hardcode kernel addresses
- Random offset chosen during early boot
- All kernel symbols shifted by this offset
/proc/kallsymsshows randomized addresses
- Information leaks can reveal base address
- Side-channel attacks (Meltdown/Spectre) can bypass
- Doesn’t protect against local attackers with kernel memory access
- SMEP (Supervisor Mode Execution Prevention)
- SMAP (Supervisor Mode Access Prevention)
- Stack canaries
Key Takeaways
Architecture Choice
Linux’s monolithic design prioritizes performance while modules add flexibility
Source Organization
Understanding source tree layout is essential for kernel development and debugging
Boot Process
From firmware to init, each stage has specific responsibilities and debugging points
Module System
Modules extend kernel functionality at runtime without recompilation
Further Reading
- Kernel Newbies - Great for getting started
- LWN.net - In-depth kernel coverage
- kernel.org Documentation - Official docs
- Linux Kernel Development by Robert Love - Essential book
Next: System Call Interface →