Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 5: Locking and Interprocess Communication


The source code of Linux is readily available, and there’s no need to fill page after page with material
that you cannot only easily inspect yourself on your computer, but that is additionally subject to perma-
nent change in many details. So it is much better to equip you with a solid understanding of theconcepts
that are naturally much less likely to change rapidly. Nevertheless, this chapter will provide you with
everything necessary to understand how protection against concurrency is implemented in specific sub-
systems, and together with the explanations about the design and working of these, you will be well
equipped to dive into the source code, read, and modify it.

5.2.1 Atomic Operations on Integers


The kernel defines theatomic_tdata type (in<asm-arch/atomic.h>) as the basis for atomic operations
with integer counters. In the view of the kernel, these are operations that are performed as if they con-
sisted of a single assembly language instruction. A short example, in which a counter is incremented
by 1, is sufficient to demonstrate the need for operations of this kind. On the assembly language level,
incrementation is usually performed in three steps:


  1. The counter value is copied from memory into a processor register.

  2. The value is incremented.

  3. The register data are written back to memory.


Problems may occur if this operation is performed on a second processor at the same time. Both proces-
sors read the value in memory simultaneously (e.g., 4), increment it to 5, and write the new value back to
memory. However, the correct value in memory should be 6 because the counter was incremented twice.

All processors supported by the kernel provide means of performing operations of this kind atomically.
In general, special lock instructions are used to prevent the other processors in the system from working
until the current processor has completed the next action. Equivalent mechanisms that produce the same
effect may also be used.^2

To enable platform-independent kernel parts to useatomic operations, the architecture-specific code
must provide the operations listed in Table 5-1 that manipulate variables of typeatomic_t.Because,
on some systems, these operations are much slower than normal C operations, they should not be used
unless really necessary.

As an understanding of operation implementation presupposes a deep knowledge of the assembler
facilities of the individual CPUs, I do not deal with this topic here (each processor architecture provides
special functions to implement operations).

It is not possible to mix classic and atomic operations. The operations listed in
Table 5-1 donotfunction with normal data types such asintorlong,and
conversely standard operators such as++do not work withatomic_tvariables.

It should also be noted that atomic variables may be initialized only with the help of theATOMIC_INIT
macro. Because the atomic data types are ultimately implemented with normal C types, the kernel
encapsulates standard variables in a structure that can no longer be processed with normal operators
such as++.

(^2) The required instruction is actually calledlockon IA-32 systems.

Free download pdf