AlgoViz

How the Linux kernel works

medium 40 min

The 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

Kernelread()filepage 0on a misscatuser mode, RSystem call doorkernel entryVFSfd 3 to inodePage cachehostname: cachedDiskblock device

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

SuspectCheckResult
CPUrun queuevmstat r
Diskpage cache missvmstat b, iostat
Locklock waitoff-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

    major

    There is no kernel process to restart or kill. Kernel code runs on behalf of a process, an interrupt, or a kernel thread.

    Wrong
    kill <kernel pid>   # there is none
    Right
    ps -eo pid,ppid,cmd | awk '$2 == 2'   # kernel threads
  • Expecting kill -9 to always work

    critical

    A task in D acts on signals only when it returns to user mode. A zombie is already dead.

    Wrong
    while true; do kill -9 812; done
    Right
    ps -o stat,wchan -p 812cat /proc/812/stack
  • Reading load average as CPU use

    major

    Linux load counts tasks in R and D. High load with idle CPU means waiting for I/O.

    Wrong
    load 40 > 8 cores: add CPUs
    Right
    vmstat 1   # compare r and b
  • Calling buff/cache wasted memory

    minor

    The page cache is reclaimable and makes reads fast. Dropping it slows the box down.

    Wrong
    echo 3 > /proc/sys/vm/drop_caches   # in production
    Right
    free -m   # read the available column
  • Running strace on a hot production process

    major

    strace stops the target on every system call, so it can slow it a lot. Prefer counting tools or a short window.

    Wrong
    strace -f -p $(pidof api)   # for an hour
    Right
    perf 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.