Chapter 1 laid the foundation: atomic operations make a read-modify-write indivisible, and acquire/release keeps the critical section from leaking out. This chapter takes apart the first real lock: the spinlock. It is the most basic and most widely used mutual-exclusion primitive in the kernel. A CPU that cannot get the lock does not sleep; it spins in place, retrying again and again until the lock is released. Precisely because it never sleeps, it can be used in contexts where scheduling is impossible, such as interrupt handlers or code already holding other locks. But “spinning in place” is not simple on a multicore machine: a naive implementation makes every contender crowd onto the same cache line and drag each other down. The thread running through this chapter is how the kernel evolved from the most naive test-and-set all the way to the MCS queue of qspinlock, reducing “every CPU fighting over one cache line” to “each CPU spinning on its own local node”.
Note: x86-64 is the main thread, and it uses
qspinlockby default;ticket_spinlockis analyzed alongside it as a “simpler, but still cache-contended” point of comparison.