Synchronization and Locking
Relevant source files
The following files were used as context for generating this wiki page:
Synchronization and locking protect shared resources from concurrent access in a multiprocessor environment. The Linux kernel uses these mechanisms to ensure data integrity when multiple threads, hardware interrupts, or softirqs access data structures or hardware registers simultaneously.
Modern kernel development requires explicit attention to concurrency. Any resource accessed by more than one thread must be protected by a lock to prevent data corruption and race conditions. Sources: Documentation/process/4.Coding.rst:114-131
Core Locking Primitives
The kernel provides several primitives to manage concurrency, each suited for different execution contexts and performance requirements.
Spinlocks
Spinlocks provide mutual exclusion for short periods. A thread attempting to acquire a busy spinlock "spins" in a loop, consuming CPU cycles until the lock becomes available. You must not call routines that sleep while holding a spinlock. Sources: Documentation/kernel-hacking/hacking.rst:228-232
Mutexes
Mutexes allow a thread to sleep while waiting for a lock. They are used for longer critical sections where the wait time justifies the overhead of a context switch. You can only use mutexes in user context. Sources: Documentation/kernel-hacking/hacking.rst:243-247, kernel/locking/mutex.c
RCU (Read-Copy-Update)
RCU allows multiple readers to access data simultaneously with a single writer. It is highly efficient for read-mostly data structures as readers do not incur locking overhead. Sources: kernel/rcu/tree.c, Documentation/maintainer/maintainer-tip.rst:54-55
Execution Contexts and Locking Rules
Locking behavior depends on the CPU's current execution context. The hierarchy of contexts determines preemption rules.
| Context | Preemptible | Can Sleep | Valid current |
|---|---|---|---|
| User Context | Yes | Yes | Yes |
| Softirq / Tasklet | No | No | No |
| Hard IRQ | No | No | No |
Sources: Documentation/kernel-hacking/hacking.rst:31-77
Context Interaction Flow
The following diagram illustrates how different execution contexts interact and which can preempt others.
The hierarchy shows that Hardware Interrupts have the highest priority and cannot be preempted by lower contexts.
Sources: Documentation/kernel-hacking/hacking.rst:31-45
Atomic Operations
Atomic operations provide a way to modify variables without full locking overhead. These are guaranteed to be atomic across all platforms.
atomic_tOperations: Used for integers. Functions includeatomic_add(),atomic_inc(), andatomic_dec_and_test().- Bitwise Operations: Used on
unsigned long. Functions includeset_bit(),clear_bit(), andtest_and_set_bit().
Sources: Documentation/kernel-hacking/hacking.rst:537-555
Deadlock Prevention and Debugging
Deadlocks occur when two or more threads wait indefinitely for each other to release locks. The kernel provides tools to identify these scenarios.
Lockdep (Lock Validator)
Lockdep tracks the acquisition order of every lock in the system. It detects potential deadlock scenarios before they occur in a live system by ensuring consistent locking order across all code paths. Sources: Documentation/process/4.Coding.rst:200-213
Lockdep Usage in Documentation
Developers should use lockdep_assert_held() to document and verify locking requirements at runtime instead of relying solely on comments. Sources: Documentation/maintainer/maintainer-tip.rst:399-411
Deadlocks occur when threads request locks in conflicting orders.
Sources: Documentation/process/4.Coding.rst:205-210
Best Practices
- Avoid Premature Abstraction: Only use locking and abstraction levels required by the current implementation. Sources: Documentation/process/4.Coding.rst:64-70
- Confine
#ifdef: Keep conditional locking or synchronization code in header files rather than sprinkling it through C files. Sources: Documentation/process/4.Coding.rst:95-103 - Check Contexts: Use
in_interrupt()orin_softirq()to verify execution context before calling functions that might sleep. Sources: Documentation/kernel-hacking/hacking.rst:51-77 - Use Atomic Types: For simple counters, use
atomic_tinstead of protecting a standard integer with a spinlock. Sources: Documentation/kernel-hacking/hacking.rst:548-550
Synchronization and locking are fundamental to kernel stability. Proper use of spinlocks, mutexes, and RCU, combined with automated tools like Lockdep, prevents data corruption and system hangs in complex multiprocessor environments. Sources: Documentation/process/4.Coding.rst:114-131, Documentation/kernel-hacking/hacking.rst:222-226