# Locking

_Peios Kernel / Internals / Locking_

> Find the locking primitives, usage rules, implementation details, and diagnostic tools used inside the kernel.

<!-- SPDX-License-Identifier: GPL-2.0 -->

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](/kernel/internals/locking/locktypes.md),
which explains sleeping, spinning, and CPU-local locks, their nesting rules,
and how their behaviour changes under PREEMPT_RT.

## Locking primitives

- [Spinlocks](/kernel/internals/locking/spinlocks.md) and [mutexes](/kernel/internals/locking/mutex-design.md).
- [RT-mutexes and priority inheritance](/kernel/internals/locking/rt-mutex.md), with a detailed [implementation guide](/kernel/internals/locking/rt-mutex-design.md).
- [Wound/wait mutexes](/kernel/internals/locking/ww-mutex-design.md) for acquiring multiple locks when their order is not fixed.
- [Sequence counters and sequential locks](/kernel/internals/locking/seqlock.md).
- [Per-CPU reader/writer semaphores](/kernel/internals/locking/percpu-rw-semaphore.md).
- [Hardware spinlocks](/kernel/internals/locking/hwspinlock.md).

## Execution context and userspace coordination

[Locking and preemption](/kernel/internals/locking/preempt-locking.md) describes
protection for per-CPU data, processor state, and critical sections.

The futex articles cover [priority inheritance](/kernel/internals/locking/pi-futex.md),
[requeueing](/kernel/internals/locking/futex-requeue-pi.md),
[robust futexes](/kernel/internals/locking/robust-futexes.md), and the
[robust futex ABI](/kernel/internals/locking/robust-futex-abi.md).

## Validation and diagnostics

- [Lock dependency validation](/kernel/internals/locking/lockdep-design.md) explains lockdep and its annotations.
- [Lock statistics](/kernel/internals/locking/lockstat.md) covers contention and hold-time measurements.
- [Lock torture tests](/kernel/internals/locking/locktorture.md) describes the locking stress-test module.
