Synchronization Primitives

We now examine how kernel control paths can be interleaved while avoiding race conditions among shared data. Table 5-2 lists the synchronization techniques used by the Linux kernel. The “Scope” column indicates whether the synchronization technique applies to all CPUs in the system or to a single CPU. For instance, local interrupt disabling applies to just one CPU (other CPUs in the system are not affected); conversely, an atomic operation affects all CPUs in the system (atomic operations on several CPUs cannot interleave while accessing the same data structure).

Table 5-2. Various types of synchronization techniques used by the kernel

Technique

Description

Scope

Per-CPU variables

Duplicate a data structure among the CPUs

All CPUs

Atomic operation

Atomic read-modify-write instruction to a counter

All CPUs

Memory barrier

Avoid instruction reordering

Local CPU or All CPUs

Spin lock

Lock with busy wait

All CPUs

Semaphore

Lock with blocking wait (sleep)

All CPUs

Seqlocks

Lock based on an access counter

All CPUs

Local interrupt disabling

Forbid interrupt handling on a single CPU

Local CPU

Local softirq disabling

Forbid deferrable function handling on a single CPU

Local CPU

Read-copy-update (RCU)

Lock-free access to shared data structures through pointers

All CPUs

Let’s now briefly discuss each synchronization technique. In the later section "Synchronizing Accesses to Kernel Data Structures,” we show how these synchronization techniques can be combined to effectively protect kernel data ...

Get Understanding the Linux Kernel, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.