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:

read i increment i write i

Atomic instruction performs:

read + increment + write (single step)

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:

atomic_t

Defined as:

typedef struct { volatile int counter; } atomic_t;

Why use atomic_t instead of int?

  1. Ensures only atomic functions access the value

  2. Prevents compiler optimizations that break correctness

  3. Handles architecture differences

  4. Guarantees proper memory access


4. Declaring Atomic Variables

atomic_t v; atomic_t u = ATOMIC_INIT(0);

5. Basic Atomic Integer Functions

Set and Read

atomic_set(&v, 4); atomic_read(&v);

Arithmetic Operations

atomic_add(2, &v); atomic_sub(1, &v); atomic_inc(&v); atomic_dec(&v);

6. Atomic Operations with Testing

Useful for decision making:

atomic_dec_and_test(&v)
  • 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:

atomic64_t

Defined as:

typedef struct { volatile long counter; } atomic64_t;

Used when:

  • 64-bit values needed

  • Architecture supports it

Functions are similar but prefixed:

atomic64_read() atomic64_set() atomic64_add() atomic64_inc()

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:

<asm/bitops.h>

These operate on:

  • generic memory address

  • specific bit number


Common Bit Operations

set_bit(n, addr) // set bit clear_bit(n, addr) // clear bit change_bit(n, addr) // toggle bit test_bit(n, addr) // read bit

Test-and-Modify Operations

test_and_set_bit(n, addr) test_and_clear_bit(n, addr) test_and_change_bit(n, addr)

These:

✔ Modify bit atomically
✔ Return previous value


10. Atomic vs Nonatomic Bit Operations

Linux provides nonatomic versions:

__set_bit() __clear_bit()

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:

set bit → clear bit

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:

find_first_bit() find_first_zero_bit()

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

Popular posts from this blog

Operating Systems OS PCCST403 Semester 4 BTech KTU CS 2024 Scheme

Introduction to Operating System -Virtualization, Concurrency, and Persistence

Differences Between Linux and Classic UNIX Kernels