1Generic Mutex Subsystem 2 3started by Ingo Molnar <mingo@redhat.com> 4updated by Davidlohr Bueso <davidlohr@hp.com> 5 6What are mutexes? 7----------------- 8 9In the Linux kernel, mutexes refer to a particular locking primitive 10that enforces serialization on shared memory systems, and not only to 11the generic term referring to 'mutual exclusion' found in academia 12or similar theoretical text books. Mutexes are sleeping locks which 13behave similarly to binary semaphores, and were introduced in 2006[1] 14as an alternative to these. This new data structure provided a number 15of advantages, including simpler interfaces, and at that time smaller 16code (see Disadvantages). 17 18[1] http://lwn.net/Articles/164802/ 19 20Implementation 21-------------- 22 23Mutexes are represented by 'struct mutex', defined in include/linux/mutex.h 24and implemented in kernel/locking/mutex.c. These locks use a three 25state atomic counter (->count) to represent the different possible 26transitions that can occur during the lifetime of a lock: 27 28 1: unlocked 29 0: locked, no waiters 30 negative: locked, with potential waiters 31 32In its most basic form it also includes a wait-queue and a spinlock 33that serializes access to it. CONFIG_SMP systems can also include 34a pointer to the lock task owner (->owner) as well as a spinner MCS 35lock (->osq), both described below in (ii). 36 37When acquiring a mutex, there are three possible paths that can be 38taken, depending on the state of the lock: 39 40(i) fastpath: tries to atomically acquire the lock by decrementing the 41 counter. If it was already taken by another task it goes to the next 42 possible path. This logic is architecture specific. On x86-64, the 43 locking fastpath is 2 instructions: 44 45 0000000000000e10 <mutex_lock>: 46 e21: f0 ff 0b lock decl (%rbx) 47 e24: 79 08 jns e2e <mutex_lock+0x1e> 48 49 the unlocking fastpath is equally tight: 50 51 0000000000000bc0 <mutex_unlock>: 52 bc8: f0 ff 07 lock incl (%rdi) 53 bcb: 7f 0a jg bd7 <mutex_unlock+0x17> 54 55 56(ii) midpath: aka optimistic spinning, tries to spin for acquisition 57 while the lock owner is running and there are no other tasks ready 58 to run that have higher priority (need_resched). The rationale is 59 that if the lock owner is running, it is likely to release the lock 60 soon. The mutex spinners are queued up using MCS lock so that only 61 one spinner can compete for the mutex. 62 63 The MCS lock (proposed by Mellor-Crummey and Scott) is a simple spinlock 64 with the desirable properties of being fair and with each cpu trying 65 to acquire the lock spinning on a local variable. It avoids expensive 66 cacheline bouncing that common test-and-set spinlock implementations 67 incur. An MCS-like lock is specially tailored for optimistic spinning 68 for sleeping lock implementation. An important feature of the customized 69 MCS lock is that it has the extra property that spinners are able to exit 70 the MCS spinlock queue when they need to reschedule. This further helps 71 avoid situations where MCS spinners that need to reschedule would continue 72 waiting to spin on mutex owner, only to go directly to slowpath upon 73 obtaining the MCS lock. 74 75 76(iii) slowpath: last resort, if the lock is still unable to be acquired, 77 the task is added to the wait-queue and sleeps until woken up by the 78 unlock path. Under normal circumstances it blocks as TASK_UNINTERRUPTIBLE. 79 80While formally kernel mutexes are sleepable locks, it is path (ii) that 81makes them more practically a hybrid type. By simply not interrupting a 82task and busy-waiting for a few cycles instead of immediately sleeping, 83the performance of this lock has been seen to significantly improve a 84number of workloads. Note that this technique is also used for rw-semaphores. 85 86Semantics 87--------- 88 89The mutex subsystem checks and enforces the following rules: 90 91 - Only one task can hold the mutex at a time. 92 - Only the owner can unlock the mutex. 93 - Multiple unlocks are not permitted. 94 - Recursive locking/unlocking is not permitted. 95 - A mutex must only be initialized via the API (see below). 96 - A task may not exit with a mutex held. 97 - Memory areas where held locks reside must not be freed. 98 - Held mutexes must not be reinitialized. 99 - Mutexes may not be used in hardware or software interrupt 100 contexts such as tasklets and timers. 101 102These semantics are fully enforced when CONFIG DEBUG_MUTEXES is enabled. 103In addition, the mutex debugging code also implements a number of other 104features that make lock debugging easier and faster: 105 106 - Uses symbolic names of mutexes, whenever they are printed 107 in debug output. 108 - Point-of-acquire tracking, symbolic lookup of function names, 109 list of all locks held in the system, printout of them. 110 - Owner tracking. 111 - Detects self-recursing locks and prints out all relevant info. 112 - Detects multi-task circular deadlocks and prints out all affected 113 locks and tasks (and only those tasks). 114 115 116Interfaces 117---------- 118Statically define the mutex: 119 DEFINE_MUTEX(name); 120 121Dynamically initialize the mutex: 122 mutex_init(mutex); 123 124Acquire the mutex, uninterruptible: 125 void mutex_lock(struct mutex *lock); 126 void mutex_lock_nested(struct mutex *lock, unsigned int subclass); 127 int mutex_trylock(struct mutex *lock); 128 129Acquire the mutex, interruptible: 130 int mutex_lock_interruptible_nested(struct mutex *lock, 131 unsigned int subclass); 132 int mutex_lock_interruptible(struct mutex *lock); 133 134Acquire the mutex, interruptible, if dec to 0: 135 int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock); 136 137Unlock the mutex: 138 void mutex_unlock(struct mutex *lock); 139 140Test if the mutex is taken: 141 int mutex_is_locked(struct mutex *lock); 142 143Disadvantages 144------------- 145 146Unlike its original design and purpose, 'struct mutex' is larger than 147most locks in the kernel. E.g: on x86-64 it is 40 bytes, almost twice 148as large as 'struct semaphore' (24 bytes) and tied, along with rwsems, 149for the largest lock in the kernel. Larger structure sizes mean more 150CPU cache and memory footprint. 151 152When to use mutexes 153------------------- 154 155Unless the strict semantics of mutexes are unsuitable and/or the critical 156region prevents the lock from being shared, always prefer them to any other 157locking primitive. 158