Every lock covered in the previous six chapters is concerned with how a single lock can be acquired and released efficiently. Real systems, however, have a thornier class of problems: an operation that can only proceed safely while holding many locks at once. The canonical example is command submission in a GPU driver. A single submission often references dozens or hundreds of buffer objects, each with its own lock, and the driver must lock all of them before it can consistently validate, migrate, and map that memory. Rename in a file system and multi-VMA operations in memory management have similar needs.

As soon as you need several locks at once, you run into the most classic ghost of concurrent programming: the ABBA deadlock. Task 1 locks in the order “A then B”, task 2 locks in the order “B then A”; each holds one lock and waits for the one the other holds, and they are stuck forever. The textbook solution is a global lock order: everyone agrees to take locks in one fixed order (for example, by ascending memory address of the lock), and ABBA becomes impossible. In the GPU world that premise often does not hold. Which buffer objects get locked, and in what order, depends on the commands userspace submits; the driver cannot know in advance and cannot impose an order. When “the locking order cannot be determined up front”, it is time for this chapter’s ww_mutex (wound/wait mutex). Instead of demanding a lock order, it introduces a deadlock avoidance algorithm: every “locking transaction” is stamped with a timestamp, and whenever a cycle might form, the younger transaction backs off, guaranteeing that the system as a whole always makes forward progress.

ww_mutex is not a brand-new lock. It is built on top of the mutex from Chapter 5: the underlying mutual exclusion, sleeping, optimistic spinning, and handoff are all reused from mutex, and ww_mutex merely wraps a layer of “timestamp + back-off” deadlock avoidance logic around it. The source code for this chapter lives mainly in kernel/locking/ww_mutex.h (the core algorithm), include/linux/ww_mutex.h (data structures and API), kernel/locking/ww_rt_mutex.c, and kernel/locking/test-ww_mutex.c.

Note: The main line is x86-64 with a non-PREEMPT_RT configuration, where the base of ww_mutex, WW_MUTEX_BASE, is the mutex from Chapter 5. Under PREEMPT_RT the base becomes rt_mutex; the algorithm is the same and is not covered separately.