The mutex of Chapter 5 is the kernel’s mainstream sleeping lock, but it has an inherent limitation: whether a task reads or writes, only one task at a time may enter the critical section. Yet a great deal of real-world data is read-mostly: a process’s address space via mm->mmap_lock, assorted file system metadata, configuration tables in drivers. These structures are read most of the time and modified only occasionally. Protect them with a mutex and even read-only tasks must line up and run one after another, throwing away the chance that “reads could have run in parallel with reads”. The qrwlock of Chapter 3 solves reader-writer parallelism for spinning, but it does not sleep and suits only very short critical sections. When the read and write critical sections are long and may sleep, it is time for this chapter’s rwsem (reader-writer semaphore): it lets any number of readers hold the lock at once, while a writer holds it exclusively, and a task that fails to get the lock sleeps and yields the CPU, just as with a mutex.

First, a misconception that has tripped up almost everyone: the “sem” in rwsem stands for semaphore, which makes it easy to assume it is a reader-writer version of the counting semaphore. It is not. The modern rwsem shares no implementation lineage with kernel/locking/semaphore.c. Its real ancestry is this: rwsem is the reader-writer sibling of Chapter 5’s mutex. It reuses the whole mutex design: one atomic field with bit encoding, an owner field paired with optimistic spinning, Chapter 4’s osq_lock as the foundation, and handoff to prevent starvation.

Note: The main line is x86-64, 64-bit, non-PREEMPT_RT configurations. Under PREEMPT_RT, rwsem switches to an rt_mutex foundation and readers no longer truly run in parallel; that is outside the scope of this chapter.