Linux Kernel Synchronization-Atomic Operations
Linux Kernel Synchronization — Atomic Operations
1. What Are Atomic Operations?
Atomic operations are instructions that execute:
-
Indivisibly
-
Without interruption
-
Without race conditions
This means:
✔ Operation completes fully or not at all
✔ No other CPU/thread can see partial results
✔ Cannot interleave with another operation
Example:
Instead of:
Atomic instruction performs:
So two threads incrementing a value cannot overwrite each other.
2. Why Atomic Operations Are Important
They form the foundation of kernel synchronization because:
-
Faster than locks
-
Lower overhead
-
Prevent race conditions on simple variables
-
Avoid complex locking for small updates
Common uses:
-
Counters
-
Reference counts
-
Flags
-
State tracking
3. Atomic Integer Operations
Linux provides atomic operations using a special datatype:
Defined as:
Why use atomic_t instead of int?
-
Ensures only atomic functions access the value
-
Prevents compiler optimizations that break correctness
-
Handles architecture differences
-
Guarantees proper memory access
4. Declaring Atomic Variables
5. Basic Atomic Integer Functions
Set and Read
Arithmetic Operations
6. Atomic Operations with Testing
Useful for decision making:
-
Decrements value
-
Returns TRUE if result is zero
Other examples:
-
atomic_add_return()→ returns new value -
atomic_add_negative()→ checks if result < 0 -
atomic_inc_and_test()→ checks if result == 0
7. Atomicity vs Ordering
Atomicity
Means:
✔ Operation happens completely
✔ No partial update visible
Example:
Value changes from 42 → 365
A read sees:
-
either 42
-
or 365
Never corrupted mixture.
Ordering (different concept)
Ordering ensures:
✔ Instructions execute in required sequence
Atomic operations guarantee atomicity only, not ordering.
Ordering requires:
-
Memory barriers (discussed later in chapter)
8. 64-bit Atomic Operations
Linux also provides:
Defined as:
Used when:
-
64-bit values needed
-
Architecture supports it
Functions are similar but prefixed:
For portability:
✔ Prefer atomic_t (32-bit)
✔ Use atomic64_t only when necessary
9. Atomic Bitwise Operations
Kernel also provides atomic operations on individual bits.
Defined in:
These operate on:
-
generic memory address
-
specific bit number
Common Bit Operations
Test-and-Modify Operations
These:
✔ Modify bit atomically
✔ Return previous value
10. Atomic vs Nonatomic Bit Operations
Linux provides nonatomic versions:
These:
-
Faster
-
No atomic guarantee
-
Used only when data already protected by lock
Why nonatomic versions exist?
Because:
-
Atomicity ensures intermediate states occur correctly
-
Without atomicity, operations may overlap and skip states
Example:
Expected sequence:
Without atomicity:
-
clear might happen
-
set might never actually be visible
This matters for:
-
hardware registers
-
synchronization flags
-
ordering-sensitive logic
11. Finding Bits
Kernel also provides:
Search memory for first set/unset bit.
Useful in:
-
CPU masks
-
resource maps
-
scheduling
12. Key Advantages of Atomic Operations
✔ Very fast
✔ No context switch
✔ No sleeping
✔ Less cache contention than locks
✔ Ideal for simple shared data
13. Key Limitations
✘ Only suitable for simple operations
✘ Cannot protect large critical sections
✘ Do not enforce ordering
✘ Cannot replace locks in complex synchronization
Comments
Post a Comment