The locks in the previous three chapters share one trait: they are user-facing locks. You call spin_lock, I call read_lock, and whoever gets the lock enters the critical section. The osq_lock in this chapter (optimistic spin queue lock) is the exception. Ordinary code almost never calls it directly; it lives deep inside mutex (Chapter 5) and rwsem (Chapter 6), serving as the common foundation for their optimistic spinning. To understand why the sleeping locks of the next two chapters “spin for a while first even though they could sleep”, you first have to understand the queue lock sitting underneath them. Its source is a single file, kernel/locking/osq_lock.c, yet it distills two key ideas: the “everyone spins locally” of the MCS queue from Chapter 2’s qspinlock, and a capability qspinlock does not have, introduced in this chapter: spinning that can be cancelled midway.

Note: x86-64 is the main thread of discussion. osq_lock shares its origins with the MCS lock in Chapter 2, so it is worth reading alongside Section 2.7. Its direct users are mutex/rwsem; Section 4.8 points out where it plugs into them.