1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/percpu.h> 3 #include <linux/sched.h> 4 #include <linux/osq_lock.h> 5 6 /* 7 * An MCS like lock especially tailored for optimistic spinning for sleeping 8 * lock implementations (mutex, rwsem, etc). 9 * 10 * Using a single mcs node per CPU is safe because sleeping locks should not be 11 * called from interrupt context and we have preemption disabled while 12 * spinning. 13 */ 14 15 struct optimistic_spin_node { 16 struct optimistic_spin_node *next, *prev; 17 int locked; /* 1 if lock acquired */ 18 int cpu; /* encoded CPU # + 1 value */ 19 }; 20 21 static DEFINE_PER_CPU_SHARED_ALIGNED(struct optimistic_spin_node, osq_node); 22 23 /* 24 * We use the value 0 to represent "no CPU", thus the encoded value 25 * will be the CPU number incremented by 1. 26 */ 27 static inline int encode_cpu(int cpu_nr) 28 { 29 return cpu_nr + 1; 30 } 31 32 static inline int node_cpu(struct optimistic_spin_node *node) 33 { 34 return node->cpu - 1; 35 } 36 37 static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val) 38 { 39 int cpu_nr = encoded_cpu_val - 1; 40 41 return per_cpu_ptr(&osq_node, cpu_nr); 42 } 43 44 /* 45 * Get a stable @node->next pointer, either for unlock() or unqueue() purposes. 46 * Can return NULL in case we were the last queued and we updated @lock instead. 47 * 48 * If osq_lock() is being cancelled there must be a previous node 49 * and 'old_cpu' is its CPU #. 50 * For osq_unlock() there is never a previous node and old_cpu is 51 * set to OSQ_UNLOCKED_VAL. 52 */ 53 static inline struct optimistic_spin_node * 54 osq_wait_next(struct optimistic_spin_queue *lock, 55 struct optimistic_spin_node *node, 56 int old_cpu) 57 { 58 int curr = encode_cpu(smp_processor_id()); 59 60 for (;;) { 61 if (atomic_read(&lock->tail) == curr && 62 atomic_cmpxchg_release(&lock->tail, curr, old_cpu) == curr) { 63 /* 64 * We were the last queued, we moved @lock back. @prev 65 * will now observe @lock and will complete its 66 * unlock()/unqueue(). 67 */ 68 return NULL; 69 } 70 71 /* 72 * We must xchg() the @node->next value, because if we were to 73 * leave it in, a concurrent unlock()/unqueue() from 74 * @node->next might complete Step-A and think its @prev is 75 * still valid. 76 * 77 * If the concurrent unlock()/unqueue() wins the race, we'll 78 * wait for either @lock to point to us, through its Step-B, or 79 * wait for a new @node->next from its Step-C. 80 */ 81 if (node->next) { 82 struct optimistic_spin_node *next; 83 84 next = xchg(&node->next, NULL); 85 if (next) 86 return next; 87 } 88 89 cpu_relax(); 90 } 91 } 92 93 /* 94 * Set prev->next; this is the counterpart of osq_wait_next() in that 95 * by setting ->next the wait is terminated and progress is resumed. 96 */ 97 static inline void osq_link_next(struct optimistic_spin_node *prev, 98 struct optimistic_spin_node *next) 99 { 100 /* 101 * Suppose: 102 * 103 * tail 104 * | 105 * V 106 * CPU1 -> CPU2 -> CPU3: 107 * n: 2 n: 3 n: nil 108 * p: nil p: 1 p: 2 109 * 110 * And this is CPU2 doing the self-unqueue concurrent against CPU1s 111 * unlock()/unqueue(). Since 'prev->next == NULL' and CPU1 will be 112 * stuck in osq_wait_next() until the below store of 'prev->next'. 113 * 114 * CPU2 CPU1 115 * 116 * next->prev = prev; osq_wait_next() 117 * WMB MB 118 * prev->next = next; next->prev = prev 119 * 120 * Without the WMB it would be possible to have conflicting stores 121 * like: 122 * 123 * CPU2 CPU1 124 * 125 * prev->next = next // CPU1.n = 3 126 * next = osq_wait_next() // = 3 127 * next->prev = prev // CPU3.p = nil 128 * next->prev = prev // CPU3.p = 1 129 * 130 * Which would result in list corruption. 131 */ 132 smp_wmb(); 133 WRITE_ONCE(prev->next, next); 134 } 135 136 bool osq_lock(struct optimistic_spin_queue *lock) 137 { 138 struct optimistic_spin_node *node = this_cpu_ptr(&osq_node); 139 struct optimistic_spin_node *prev, *next; 140 int curr = encode_cpu(smp_processor_id()); 141 int old; 142 143 node->locked = 0; 144 node->next = NULL; 145 node->cpu = curr; 146 147 /* 148 * We need both ACQUIRE (pairs with corresponding RELEASE in 149 * unlock() uncontended, or fastpath) and RELEASE (to publish 150 * the node fields we just initialised) semantics when updating 151 * the lock tail. 152 */ 153 old = atomic_xchg(&lock->tail, curr); 154 if (old == OSQ_UNLOCKED_VAL) 155 return true; 156 157 prev = decode_cpu(old); 158 node->prev = prev; 159 osq_link_next(prev, node); 160 161 /* 162 * Normally @prev is untouchable after the above store; because at that 163 * moment unlock can proceed and wipe the node element from stack. 164 * 165 * However, since our nodes are static per-cpu storage, we're 166 * guaranteed their existence -- this allows us to apply 167 * cmpxchg in an attempt to undo our queueing. 168 */ 169 170 /* 171 * Wait to acquire the lock or cancellation. Note that need_resched() 172 * will come with an IPI, which will wake smp_cond_load_relaxed() if it 173 * is implemented with a monitor-wait. vcpu_is_preempted() relies on 174 * polling, be careful. 175 */ 176 if (smp_cond_load_relaxed(&node->locked, VAL || need_resched() || 177 vcpu_is_preempted(node_cpu(node->prev)))) 178 return true; 179 180 /* unqueue */ 181 /* 182 * Step - A -- stabilize @prev 183 * 184 * Undo our @prev->next assignment; this will make @prev's 185 * unlock()/unqueue() wait for a next pointer since @lock points to us 186 * (or later). 187 */ 188 189 for (;;) { 190 /* 191 * cpu_relax() below implies a compiler barrier which would 192 * prevent this comparison being optimized away. 193 */ 194 if (data_race(prev->next) == node && 195 cmpxchg(&prev->next, node, NULL) == node) 196 break; 197 198 /* 199 * We can only fail the cmpxchg() racing against an unlock(), 200 * in which case we should observe @node->locked becoming 201 * true. 202 */ 203 if (smp_load_acquire(&node->locked)) 204 return true; 205 206 cpu_relax(); 207 208 /* 209 * Or we race against a concurrent unqueue()'s step-B, in which 210 * case its step-C will write us a new @node->prev pointer. 211 */ 212 prev = READ_ONCE(node->prev); 213 } 214 215 /* 216 * Step - B -- stabilize @next 217 * 218 * Similar to unlock(), wait for @node->next or move @lock from @node 219 * back to @prev. 220 */ 221 222 next = osq_wait_next(lock, node, prev->cpu); 223 if (!next) 224 return false; 225 226 /* 227 * Step - C -- unlink 228 * 229 * @prev is stable because its still waiting for a new @prev->next 230 * pointer, @next is stable because our @node->next pointer is NULL and 231 * it will wait in Step-A. 232 */ 233 234 WRITE_ONCE(next->prev, prev); 235 osq_link_next(prev, next); 236 237 return false; 238 } 239 240 void osq_unlock(struct optimistic_spin_queue *lock) 241 { 242 struct optimistic_spin_node *node, *next; 243 int curr = encode_cpu(smp_processor_id()); 244 245 /* 246 * Fast path for the uncontended case. 247 */ 248 if (atomic_try_cmpxchg_release(&lock->tail, &curr, OSQ_UNLOCKED_VAL)) 249 return; 250 251 /* 252 * Second most likely case. 253 */ 254 node = this_cpu_ptr(&osq_node); 255 next = xchg(&node->next, NULL); 256 if (next) { 257 WRITE_ONCE(next->locked, 1); 258 return; 259 } 260 261 next = osq_wait_next(lock, node, OSQ_UNLOCKED_VAL); 262 if (next) 263 WRITE_ONCE(next->locked, 1); 264 } 265