Synchronization Primitives
Chapter 1 introduced the concepts of race condition and critical region for processes. The same definitions apply to kernel control paths. In this chapter, a race condition can occur when the outcome of a computation depends on how two or more interleaved kernel control paths are nested. A critical region is any section of code that must be completely executed by any kernel control path that enters it before another kernel control path can enter it.
We now examine how kernel control paths can be interleaved while avoiding race conditions among shared data. Table 5-1 lists the synchronization techniques used by the Linux kernel. The "Applies to" column indicates whether the synchronization technique applies to all CPUs in the system or to a single CPU. For instance, local interrupts 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).
|
Technique |
Description |
Scope |
|
Atomic operation |
Atomic read-modify-write instruction to a counter |
All CPUs |
|
Avoid instruction re-ordering |
Local CPU | |
|
Lock with busy wait |
All CPUs | |
|
Semaphore |
Lock with blocking wait (sleep) |
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 |
|
Global interrupt disabling |
Forbid interrupt and softirq handling on all CPUs |
All CPUs |
Let's now briefly discuss each synchronization technique. In the later section Section 5.4, we show how these synchronization techniques can be combined to effectively protect kernel data structures.
Continue reading here: Atomic Operations
Was this article helpful?