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).
Table 5-1. Various types of synchronization techniques used by the kernel
Technique |
Description |
Scope |
---|---|---|
Atomic operation |
Atomic read-modify-write instruction to a counter |
All CPUs |
Memory barrier |
Avoid instruction re-ordering |
Local CPU |
Spin lock |
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 ... |
Get Understanding the Linux Kernel, Second 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.