Locking
Kernel code can run concurrently on multiple CPUs and in different execution contexts. Locking primitives coordinate access to shared state. The appropriate primitive depends on the context, whether waiting may sleep, and the guarantees the protected operation requires.
Start with Lock types and their rules, which explains sleeping, spinning, and CPU-local locks, their nesting rules, and how their behaviour changes under PREEMPT_RT.
Locking primitives #
- Spinlocks and mutexes.
- RT-mutexes and priority inheritance, with a detailed implementation guide.
- Wound/wait mutexes for acquiring multiple locks when their order is not fixed.
- Sequence counters and sequential locks.
- Per-CPU reader/writer semaphores.
- Hardware spinlocks.
Execution context and userspace coordination #
Locking and preemption describes protection for per-CPU data, processor state, and critical sections.
The futex articles cover priority inheritance, requeueing, robust futexes, and the robust futex ABI.
Validation and diagnostics #
- Lock dependency validation explains lockdep and its annotations.
- Lock statistics covers contention and hold-time measurements.
- Lock torture tests describes the locking stress-test module.