← Interview Prep

Linux Kernel — Synchronization

Protecting shared data across SMP, preemption and interrupts: atomics, spinlocks (and the interrupt-deadlock trap / spin_lock_irqsave), mutexes/semaphores, RCU and grace periods, seqlocks, memory barriers, per-CPU data, and lockdep.

The kernel runs on many CPUs, is preemptible, and gets interrupted mid-work — so shared data needs protection. Picking the right primitive (and knowing which ones may sleep) is the whole game. Original, interview-focused notes.

The decision tree: is the critical section tiny and can it run in interrupt context? → spinlock. Might it sleep / is it longer? → mutex/semaphore. Read-mostly, hot? → RCU or a seqlock. Just a counter/flag? → atomics.

Why it's needed

Concurrency in the kernel comes from several directions at once: multiple CPUs (SMP), kernel preemption (a task can be preempted mid-critical-section), and interrupts (a handler can fire while you hold data). Any unprotected read-modify-write is a race.

The primitives

PrimitiveBlocks byCan sleep?Use for
Atomic opsLock-free (CPU instruction)Counters, flags (atomic_t, bit ops).
SpinlockBusy-wait (spin)NoShort sections; the only choice in interrupt/softirq context.
MutexSleepYesLonger sections in process context.
Semaphore / rwsemSleepYesCounting; reader/writer variants.
RCULock-free readersReaders: noRead-mostly data (routing tables, lists).
seqlockRetry on writerRare writers, frequent readers (e.g. time).

Spinlocks & the interrupt trap

RCU — read-mostly without locking readers

Read-Copy-Update lets readers proceed with no locks and no waiting. Writers make a copy, update it, then swap the pointer atomically; the old version is freed only after a grace period — once every pre-existing reader has finished (a "quiescent state"). Perfect for hot, read-mostly structures where locking readers would kill scalability. Readers just wrap access in rcu_read_lock() (which mostly disables preemption).

Memory ordering

CPUs and compilers reorder memory accesses, so on weakly-ordered hardware a lock's correctness needs memory barriers (smp_mb/rmb/wmb). The locking primitives embed the right barriers for you — you only reach for explicit barriers in lock-free code. Mention this to show you know locks aren't just mutual exclusion; they're also ordering.

Getting it right

Likely interview questions

Kernel Internals series. Related: Interrupts · Scheduling.