How the Linux kernel works
medium 40 minThe kernel is the one program that runs with full privilege.
See it run
Watch requests cross into the kernel: a system call, an interrupt, a page fault. Then follow tasks, memory, files and packets.
Operations
A system call
cat calls read(). The CPU switches to kernel mode, the VFS finds the page, the bytes are copied back.
Step 1 of 5
In user mode
cat runs its own code with limited rights. It cannot touch the disk or kernel memory.
Step 1: In user mode
n = read(fd, buf, 131072) # C library wrapper# syscall instruction: CPU enters kernel modefile = current.files[fd] # the open file objectpage = page_cache(file, 0) # or read from disk and sleepcopy_to_user(buf, page) # bytes cross the boundaryreturn n # back to user mode- Costs
- one mode switch, a copy
- Look with
- strace, perf trace
- On a cache miss
- sleep in D for the disk
A program can only enter at the fixed entry point. That is what keeps the kernel safe.
In the real world
The same how the linux kernel works doing real jobs. Pick one and step through it.
Asha runs cat /etc/hostname on a busy host. It takes two seconds. Where did the time go?
Step 1 of 3
The question
| Suspect | Check | Result | |
|---|---|---|---|
| CPU | run queue | vmstat r | |
| Disk | page cache miss | vmstat b, iostat | |
| Lock | lock wait | off-CPU profile |
Three suspects: waiting for a CPU, for the disk, or for a lock.
Step 1: The question
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Treating the kernel as a process
majorThere is no kernel process to restart or kill. Kernel code runs on behalf of a process, an interrupt, or a kernel thread.
Wrongkill <kernel pid> # there is noneRightps -eo pid,ppid,cmd | awk '$2 == 2' # kernel threadsExpecting kill -9 to always work
criticalA task in D acts on signals only when it returns to user mode. A zombie is already dead.
Wrongwhile true; do kill -9 812; doneRightps -o stat,wchan -p 812cat /proc/812/stackReading load average as CPU use
majorLinux load counts tasks in R and D. High load with idle CPU means waiting for I/O.
Wrongload 40 > 8 cores: add CPUsRightvmstat 1 # compare r and bCalling buff/cache wasted memory
minorThe page cache is reclaimable and makes reads fast. Dropping it slows the box down.
Wrongecho 3 > /proc/sys/vm/drop_caches # in productionRightfree -m # read the available columnRunning strace on a hot production process
majorstrace stops the target on every system call, so it can slow it a lot. Prefer counting tools or a short window.
Wrongstrace -f -p $(pidof api) # for an hourRightperf stat -e context-switches,page-faults -p PID sleep 10
Interview tips
How to explain it
- One privileged program owns the hardware.
- Three ways in: ask, ring, fault.
- Each subsystem owns one resource.
- Each subsystem has one command to watch it.
Memory trick
- Knock, ring, trip.
- D is for disk and don't disturb.
- Front shelf first, basement on a miss.
Always say
- Signals act on return to user mode.
- Load counts R and D.
- fork is copy-on-write.
- Containers share one kernel.
Numbers to know
- Nice -20 to 19.
- Huge pages on x86: 2M and 1G.
- cpu.max default: max 100000.
- Soft lockup: over 20 s in the kernel.