blob: 0ed478e10071dc43b7be2bee67301e302ec0ce5a [file] [log] [blame]
Waiman Longa33fda32015-04-24 14:56:30 -04001/*
2 * Queued spinlock
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * (C) Copyright 2013-2015 Hewlett-Packard Development Company, L.P.
15 * (C) Copyright 2013-2014 Red Hat, Inc.
16 * (C) Copyright 2015 Intel Corp.
Waiman Long64d816c2015-11-09 19:09:21 -050017 * (C) Copyright 2015 Hewlett-Packard Enterprise Development LP
Waiman Longa33fda32015-04-24 14:56:30 -040018 *
Waiman Long64d816c2015-11-09 19:09:21 -050019 * Authors: Waiman Long <waiman.long@hpe.com>
Waiman Longa33fda32015-04-24 14:56:30 -040020 * Peter Zijlstra <peterz@infradead.org>
21 */
Waiman Longa23db282015-04-24 14:56:37 -040022
23#ifndef _GEN_PV_LOCK_SLOWPATH
24
Waiman Longa33fda32015-04-24 14:56:30 -040025#include <linux/smp.h>
26#include <linux/bug.h>
27#include <linux/cpumask.h>
28#include <linux/percpu.h>
29#include <linux/hardirq.h>
30#include <linux/mutex.h>
Peter Zijlstra (Intel)69f9cae2015-04-24 14:56:34 -040031#include <asm/byteorder.h>
Waiman Longa33fda32015-04-24 14:56:30 -040032#include <asm/qspinlock.h>
33
34/*
35 * The basic principle of a queue-based spinlock can best be understood
36 * by studying a classic queue-based spinlock implementation called the
37 * MCS lock. The paper below provides a good description for this kind
38 * of lock.
39 *
40 * http://www.cise.ufl.edu/tr/DOC/REP-1992-71.pdf
41 *
42 * This queued spinlock implementation is based on the MCS lock, however to make
43 * it fit the 4 bytes we assume spinlock_t to be, and preserve its existing
44 * API, we must modify it somehow.
45 *
46 * In particular; where the traditional MCS lock consists of a tail pointer
47 * (8 bytes) and needs the next pointer (another 8 bytes) of its own node to
48 * unlock the next pending (next->locked), we compress both these: {tail,
49 * next->locked} into a single u32 value.
50 *
51 * Since a spinlock disables recursion of its own context and there is a limit
52 * to the contexts that can nest; namely: task, softirq, hardirq, nmi. As there
53 * are at most 4 nesting levels, it can be encoded by a 2-bit number. Now
54 * we can encode the tail by combining the 2-bit nesting level with the cpu
55 * number. With one byte for the lock value and 3 bytes for the tail, only a
56 * 32-bit word is now needed. Even though we only need 1 bit for the lock,
57 * we extend it to a full byte to achieve better performance for architectures
58 * that support atomic byte write.
59 *
60 * We also change the first spinner to spin on the lock bit instead of its
61 * node; whereby avoiding the need to carry a node from lock to unlock, and
62 * preserving existing lock API. This also makes the unlock code simpler and
63 * faster.
Peter Zijlstra (Intel)69f9cae2015-04-24 14:56:34 -040064 *
65 * N.B. The current implementation only supports architectures that allow
66 * atomic operations on smaller 8-bit and 16-bit data types.
67 *
Waiman Longa33fda32015-04-24 14:56:30 -040068 */
69
70#include "mcs_spinlock.h"
71
Waiman Longa23db282015-04-24 14:56:37 -040072#ifdef CONFIG_PARAVIRT_SPINLOCKS
73#define MAX_NODES 8
74#else
75#define MAX_NODES 4
76#endif
77
Waiman Longa33fda32015-04-24 14:56:30 -040078/*
Will Deacon8e5b3bc2018-12-18 23:10:42 +010079 * The pending bit spinning loop count.
80 * This heuristic is used to limit the number of lockword accesses
81 * made by atomic_cond_read_relaxed when waiting for the lock to
82 * transition out of the "== _Q_PENDING_VAL" state. We don't spin
83 * indefinitely because there's no guarantee that we'll make forward
84 * progress.
85 */
86#ifndef _Q_PENDING_LOOPS
87#define _Q_PENDING_LOOPS 1
88#endif
89
90/*
Waiman Longa33fda32015-04-24 14:56:30 -040091 * Per-CPU queue node structures; we can never have more than 4 nested
92 * contexts: task, softirq, hardirq, nmi.
93 *
94 * Exactly fits one 64-byte cacheline on a 64-bit architecture.
Waiman Longa23db282015-04-24 14:56:37 -040095 *
96 * PV doubles the storage and uses the second cacheline for PV state.
Waiman Longa33fda32015-04-24 14:56:30 -040097 */
Waiman Longa23db282015-04-24 14:56:37 -040098static DEFINE_PER_CPU_ALIGNED(struct mcs_spinlock, mcs_nodes[MAX_NODES]);
Waiman Longa33fda32015-04-24 14:56:30 -040099
100/*
101 * We must be able to distinguish between no-tail and the tail at 0:0,
102 * therefore increment the cpu number by one.
103 */
104
Peter Zijlstra8d53fa12016-06-08 09:12:30 +0200105static inline __pure u32 encode_tail(int cpu, int idx)
Waiman Longa33fda32015-04-24 14:56:30 -0400106{
107 u32 tail;
108
109#ifdef CONFIG_DEBUG_SPINLOCK
110 BUG_ON(idx > 3);
111#endif
112 tail = (cpu + 1) << _Q_TAIL_CPU_OFFSET;
113 tail |= idx << _Q_TAIL_IDX_OFFSET; /* assume < 4 */
114
115 return tail;
116}
117
Peter Zijlstra8d53fa12016-06-08 09:12:30 +0200118static inline __pure struct mcs_spinlock *decode_tail(u32 tail)
Waiman Longa33fda32015-04-24 14:56:30 -0400119{
120 int cpu = (tail >> _Q_TAIL_CPU_OFFSET) - 1;
121 int idx = (tail & _Q_TAIL_IDX_MASK) >> _Q_TAIL_IDX_OFFSET;
122
123 return per_cpu_ptr(&mcs_nodes[idx], cpu);
124}
125
Peter Zijlstra (Intel)c1fb1592015-04-24 14:56:32 -0400126#define _Q_LOCKED_PENDING_MASK (_Q_LOCKED_MASK | _Q_PENDING_MASK)
127
Waiman Long2c83e8e2015-04-24 14:56:35 -0400128#if _Q_PENDING_BITS == 8
Peter Zijlstra (Intel)69f9cae2015-04-24 14:56:34 -0400129/**
Will Deacon9b588432018-12-18 23:10:44 +0100130 * clear_pending - clear the pending bit.
131 * @lock: Pointer to queued spinlock structure
132 *
133 * *,1,* -> *,0,*
134 */
135static __always_inline void clear_pending(struct qspinlock *lock)
136{
137 WRITE_ONCE(lock->pending, 0);
138}
139
140/**
Peter Zijlstra (Intel)69f9cae2015-04-24 14:56:34 -0400141 * clear_pending_set_locked - take ownership and clear the pending bit.
142 * @lock: Pointer to queued spinlock structure
143 *
144 * *,1,0 -> *,0,1
145 *
146 * Lock stealing is not allowed if this function is used.
147 */
148static __always_inline void clear_pending_set_locked(struct qspinlock *lock)
149{
Will Deacon60668f32018-12-18 23:10:43 +0100150 WRITE_ONCE(lock->locked_pending, _Q_LOCKED_VAL);
Peter Zijlstra (Intel)69f9cae2015-04-24 14:56:34 -0400151}
152
153/*
154 * xchg_tail - Put in the new queue tail code word & retrieve previous one
155 * @lock : Pointer to queued spinlock structure
156 * @tail : The new queue tail code word
157 * Return: The previous queue tail code word
158 *
Paul E. McKenneyc3b6e792018-12-18 23:10:40 +0100159 * xchg(lock, tail), which heads an address dependency
Peter Zijlstra (Intel)69f9cae2015-04-24 14:56:34 -0400160 *
161 * p,*,* -> n,*,* ; prev = xchg(lock, node)
162 */
163static __always_inline u32 xchg_tail(struct qspinlock *lock, u32 tail)
164{
Waiman Long64d816c2015-11-09 19:09:21 -0500165 /*
166 * Use release semantics to make sure that the MCS node is properly
167 * initialized before changing the tail code.
168 */
Will Deacon60668f32018-12-18 23:10:43 +0100169 return (u32)xchg_release(&lock->tail,
Waiman Long64d816c2015-11-09 19:09:21 -0500170 tail >> _Q_TAIL_OFFSET) << _Q_TAIL_OFFSET;
Peter Zijlstra (Intel)69f9cae2015-04-24 14:56:34 -0400171}
172
173#else /* _Q_PENDING_BITS == 8 */
174
Waiman Longa33fda32015-04-24 14:56:30 -0400175/**
Will Deacon9b588432018-12-18 23:10:44 +0100176 * clear_pending - clear the pending bit.
177 * @lock: Pointer to queued spinlock structure
178 *
179 * *,1,* -> *,0,*
180 */
181static __always_inline void clear_pending(struct qspinlock *lock)
182{
183 atomic_andnot(_Q_PENDING_VAL, &lock->val);
184}
185
186/**
Waiman Long6403bd72015-04-24 14:56:33 -0400187 * clear_pending_set_locked - take ownership and clear the pending bit.
188 * @lock: Pointer to queued spinlock structure
189 *
190 * *,1,0 -> *,0,1
191 */
192static __always_inline void clear_pending_set_locked(struct qspinlock *lock)
193{
194 atomic_add(-_Q_PENDING_VAL + _Q_LOCKED_VAL, &lock->val);
195}
196
197/**
198 * xchg_tail - Put in the new queue tail code word & retrieve previous one
199 * @lock : Pointer to queued spinlock structure
200 * @tail : The new queue tail code word
201 * Return: The previous queue tail code word
202 *
203 * xchg(lock, tail)
204 *
205 * p,*,* -> n,*,* ; prev = xchg(lock, node)
206 */
207static __always_inline u32 xchg_tail(struct qspinlock *lock, u32 tail)
208{
209 u32 old, new, val = atomic_read(&lock->val);
210
211 for (;;) {
212 new = (val & _Q_LOCKED_PENDING_MASK) | tail;
Waiman Long64d816c2015-11-09 19:09:21 -0500213 /*
214 * Use release semantics to make sure that the MCS node is
215 * properly initialized before changing the tail code.
216 */
217 old = atomic_cmpxchg_release(&lock->val, val, new);
Waiman Long6403bd72015-04-24 14:56:33 -0400218 if (old == val)
219 break;
220
221 val = old;
222 }
223 return old;
224}
Peter Zijlstra (Intel)69f9cae2015-04-24 14:56:34 -0400225#endif /* _Q_PENDING_BITS == 8 */
Waiman Long6403bd72015-04-24 14:56:33 -0400226
227/**
Peter Zijlstra88ce30f2018-12-18 23:10:49 +0100228 * queued_fetch_set_pending_acquire - fetch the whole lock value and set pending
229 * @lock : Pointer to queued spinlock structure
230 * Return: The previous lock value
231 *
232 * *,*,* -> *,1,*
233 */
234#ifndef queued_fetch_set_pending_acquire
235static __always_inline u32 queued_fetch_set_pending_acquire(struct qspinlock *lock)
236{
237 return atomic_fetch_or_acquire(_Q_PENDING_VAL, &lock->val);
238}
239#endif
240
241/**
Waiman Long2c83e8e2015-04-24 14:56:35 -0400242 * set_locked - Set the lock bit and own the lock
243 * @lock: Pointer to queued spinlock structure
244 *
245 * *,*,0 -> *,0,1
246 */
247static __always_inline void set_locked(struct qspinlock *lock)
248{
Will Deacon60668f32018-12-18 23:10:43 +0100249 WRITE_ONCE(lock->locked, _Q_LOCKED_VAL);
Waiman Long2c83e8e2015-04-24 14:56:35 -0400250}
251
Waiman Longa23db282015-04-24 14:56:37 -0400252
253/*
254 * Generate the native code for queued_spin_unlock_slowpath(); provide NOPs for
255 * all the PV callbacks.
256 */
257
258static __always_inline void __pv_init_node(struct mcs_spinlock *node) { }
Waiman Longcd0272f2015-11-09 19:09:27 -0500259static __always_inline void __pv_wait_node(struct mcs_spinlock *node,
260 struct mcs_spinlock *prev) { }
Waiman Long75d22702015-07-11 16:36:52 -0400261static __always_inline void __pv_kick_node(struct qspinlock *lock,
262 struct mcs_spinlock *node) { }
Waiman Long1c4941f2015-11-10 16:18:56 -0500263static __always_inline u32 __pv_wait_head_or_lock(struct qspinlock *lock,
264 struct mcs_spinlock *node)
265 { return 0; }
Waiman Longa23db282015-04-24 14:56:37 -0400266
267#define pv_enabled() false
268
269#define pv_init_node __pv_init_node
270#define pv_wait_node __pv_wait_node
271#define pv_kick_node __pv_kick_node
Waiman Long1c4941f2015-11-10 16:18:56 -0500272#define pv_wait_head_or_lock __pv_wait_head_or_lock
Waiman Longa23db282015-04-24 14:56:37 -0400273
274#ifdef CONFIG_PARAVIRT_SPINLOCKS
275#define queued_spin_lock_slowpath native_queued_spin_lock_slowpath
276#endif
277
Peter Zijlstra2c610022016-06-08 10:19:51 +0200278/*
Peter Zijlstra055ce0f2016-06-08 10:36:53 +0200279 * Various notes on spin_is_locked() and spin_unlock_wait(), which are
280 * 'interesting' functions:
281 *
282 * PROBLEM: some architectures have an interesting issue with atomic ACQUIRE
283 * operations in that the ACQUIRE applies to the LOAD _not_ the STORE (ARM64,
284 * PPC). Also qspinlock has a similar issue per construction, the setting of
285 * the locked byte can be unordered acquiring the lock proper.
286 *
287 * This gets to be 'interesting' in the following cases, where the /should/s
288 * end up false because of this issue.
289 *
290 *
291 * CASE 1:
292 *
293 * So the spin_is_locked() correctness issue comes from something like:
294 *
295 * CPU0 CPU1
296 *
297 * global_lock(); local_lock(i)
298 * spin_lock(&G) spin_lock(&L[i])
299 * for (i) if (!spin_is_locked(&G)) {
300 * spin_unlock_wait(&L[i]); smp_acquire__after_ctrl_dep();
301 * return;
302 * }
303 * // deal with fail
304 *
305 * Where it is important CPU1 sees G locked or CPU0 sees L[i] locked such
306 * that there is exclusion between the two critical sections.
307 *
308 * The load from spin_is_locked(&G) /should/ be constrained by the ACQUIRE from
309 * spin_lock(&L[i]), and similarly the load(s) from spin_unlock_wait(&L[i])
310 * /should/ be constrained by the ACQUIRE from spin_lock(&G).
311 *
312 * Similarly, later stuff is constrained by the ACQUIRE from CTRL+RMB.
313 *
314 *
315 * CASE 2:
316 *
317 * For spin_unlock_wait() there is a second correctness issue, namely:
318 *
319 * CPU0 CPU1
320 *
321 * flag = set;
322 * smp_mb(); spin_lock(&l)
323 * spin_unlock_wait(&l); if (!flag)
324 * // add to lockless list
325 * spin_unlock(&l);
326 * // iterate lockless list
327 *
328 * Which wants to ensure that CPU1 will stop adding bits to the list and CPU0
329 * will observe the last entry on the list (if spin_unlock_wait() had ACQUIRE
330 * semantics etc..)
331 *
332 * Where flag /should/ be ordered against the locked store of l.
333 */
334
335/*
Peter Zijlstra2c610022016-06-08 10:19:51 +0200336 * queued_spin_lock_slowpath() can (load-)ACQUIRE the lock before
337 * issuing an _unordered_ store to set _Q_LOCKED_VAL.
338 *
339 * This means that the store can be delayed, but no later than the
340 * store-release from the unlock. This means that simply observing
341 * _Q_LOCKED_VAL is not sufficient to determine if the lock is acquired.
342 *
343 * There are two paths that can issue the unordered store:
344 *
345 * (1) clear_pending_set_locked(): *,1,0 -> *,0,1
346 *
347 * (2) set_locked(): t,0,0 -> t,0,1 ; t != 0
348 * atomic_cmpxchg_relaxed(): t,0,0 -> 0,0,1
349 *
350 * However, in both cases we have other !0 state we've set before to queue
351 * ourseves:
352 *
353 * For (1) we have the atomic_cmpxchg_acquire() that set _Q_PENDING_VAL, our
354 * load is constrained by that ACQUIRE to not pass before that, and thus must
355 * observe the store.
356 *
357 * For (2) we have a more intersting scenario. We enqueue ourselves using
358 * xchg_tail(), which ends up being a RELEASE. This in itself is not
359 * sufficient, however that is followed by an smp_cond_acquire() on the same
360 * word, giving a RELEASE->ACQUIRE ordering. This again constrains our load and
361 * guarantees we must observe that store.
362 *
363 * Therefore both cases have other !0 state that is observable before the
364 * unordered locked byte store comes through. This means we can use that to
365 * wait for the lock store, and then wait for an unlock.
366 */
367#ifndef queued_spin_unlock_wait
368void queued_spin_unlock_wait(struct qspinlock *lock)
369{
370 u32 val;
371
372 for (;;) {
373 val = atomic_read(&lock->val);
374
375 if (!val) /* not locked, we're done */
376 goto done;
377
378 if (val & _Q_LOCKED_MASK) /* locked, go wait for unlock */
379 break;
380
381 /* not locked, but pending, wait until we observe the lock */
382 cpu_relax();
383 }
384
385 /* any unlock is good */
386 while (atomic_read(&lock->val) & _Q_LOCKED_MASK)
387 cpu_relax();
388
389done:
Peter Zijlstra33ac2792016-05-24 13:17:12 +0200390 smp_acquire__after_ctrl_dep();
Peter Zijlstra2c610022016-06-08 10:19:51 +0200391}
392EXPORT_SYMBOL(queued_spin_unlock_wait);
393#endif
394
Waiman Longa23db282015-04-24 14:56:37 -0400395#endif /* _GEN_PV_LOCK_SLOWPATH */
396
Waiman Long2c83e8e2015-04-24 14:56:35 -0400397/**
Waiman Longa33fda32015-04-24 14:56:30 -0400398 * queued_spin_lock_slowpath - acquire the queued spinlock
399 * @lock: Pointer to queued spinlock structure
400 * @val: Current value of the queued spinlock 32-bit word
401 *
Peter Zijlstra (Intel)c1fb1592015-04-24 14:56:32 -0400402 * (queue tail, pending bit, lock value)
Waiman Longa33fda32015-04-24 14:56:30 -0400403 *
Peter Zijlstra (Intel)c1fb1592015-04-24 14:56:32 -0400404 * fast : slow : unlock
405 * : :
406 * uncontended (0,0,0) -:--> (0,0,1) ------------------------------:--> (*,*,0)
407 * : | ^--------.------. / :
408 * : v \ \ | :
409 * pending : (0,1,1) +--> (0,1,0) \ | :
410 * : | ^--' | | :
411 * : v | | :
412 * uncontended : (n,x,y) +--> (n,0,0) --' | :
413 * queue : | ^--' | :
414 * : v | :
415 * contended : (*,x,y) +--> (*,0,0) ---> (*,0,1) -' :
416 * queue : ^--' :
Waiman Longa33fda32015-04-24 14:56:30 -0400417 */
418void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)
419{
420 struct mcs_spinlock *prev, *next, *node;
Will Deacon9b588432018-12-18 23:10:44 +0100421 u32 old, tail;
Waiman Longa33fda32015-04-24 14:56:30 -0400422 int idx;
423
424 BUILD_BUG_ON(CONFIG_NR_CPUS >= (1U << _Q_TAIL_CPU_BITS));
425
Waiman Longa23db282015-04-24 14:56:37 -0400426 if (pv_enabled())
427 goto queue;
428
Peter Zijlstra43b3f022015-09-04 17:25:23 +0200429 if (virt_spin_lock(lock))
Peter Zijlstra (Intel)2aa79af2015-04-24 14:56:36 -0400430 return;
431
Peter Zijlstra (Intel)c1fb1592015-04-24 14:56:32 -0400432 /*
Will Deacon8e5b3bc2018-12-18 23:10:42 +0100433 * Wait for in-progress pending->locked hand-overs with a bounded
434 * number of spins so that we guarantee forward progress.
Peter Zijlstra (Intel)c1fb1592015-04-24 14:56:32 -0400435 *
436 * 0,1,0 -> 0,0,1
437 */
438 if (val == _Q_PENDING_VAL) {
Will Deacon8e5b3bc2018-12-18 23:10:42 +0100439 int cnt = _Q_PENDING_LOOPS;
440 val = smp_cond_load_acquire(&lock->val.counter,
441 (VAL != _Q_PENDING_VAL) || !cnt--);
Peter Zijlstra (Intel)c1fb1592015-04-24 14:56:32 -0400442 }
443
444 /*
Will Deacon9b588432018-12-18 23:10:44 +0100445 * If we observe any contention; queue.
446 */
447 if (val & ~_Q_LOCKED_MASK)
448 goto queue;
449
450 /*
Peter Zijlstra (Intel)c1fb1592015-04-24 14:56:32 -0400451 * trylock || pending
452 *
453 * 0,0,0 -> 0,0,1 ; trylock
454 * 0,0,1 -> 0,1,1 ; pending
455 */
Peter Zijlstra88ce30f2018-12-18 23:10:49 +0100456 val = queued_fetch_set_pending_acquire(lock);
457
Peter Zijlstraf650bdc2018-12-18 23:10:47 +0100458 /*
459 * If we observe any contention; undo and queue.
460 */
461 if (unlikely(val & ~_Q_LOCKED_MASK)) {
462 if (!(val & _Q_PENDING_MASK))
463 clear_pending(lock);
464 goto queue;
Peter Zijlstra (Intel)c1fb1592015-04-24 14:56:32 -0400465 }
466
467 /*
Peter Zijlstraf650bdc2018-12-18 23:10:47 +0100468 * We're pending, wait for the owner to go away.
469 *
470 * 0,1,1 -> 0,1,0
471 *
472 * this wait loop must be a load-acquire such that we match the
473 * store-release that clears the locked bit and create lock
474 * sequentiality; this is because not all
475 * clear_pending_set_locked() implementations imply full
476 * barriers.
Peter Zijlstra (Intel)c1fb1592015-04-24 14:56:32 -0400477 */
Peter Zijlstraf650bdc2018-12-18 23:10:47 +0100478 if (val & _Q_LOCKED_MASK)
479 smp_cond_load_acquire(&lock->val.counter, !(VAL & _Q_LOCKED_MASK));
480
481 /*
482 * take ownership and clear the pending bit.
483 *
484 * 0,1,0 -> 0,0,1
485 */
486 clear_pending_set_locked(lock);
487 return;
Peter Zijlstra (Intel)c1fb1592015-04-24 14:56:32 -0400488
489 /*
490 * End of pending bit optimistic spinning and beginning of MCS
491 * queuing.
492 */
493queue:
Waiman Longa33fda32015-04-24 14:56:30 -0400494 node = this_cpu_ptr(&mcs_nodes[0]);
495 idx = node->count++;
496 tail = encode_tail(smp_processor_id(), idx);
497
498 node += idx;
Will Deaconc8723ce2018-02-13 13:22:57 +0000499
500 /*
501 * Ensure that we increment the head node->count before initialising
502 * the actual node. If the compiler is kind enough to reorder these
503 * stores, then an IRQ could overwrite our assignments.
504 */
505 barrier();
506
Waiman Longa33fda32015-04-24 14:56:30 -0400507 node->locked = 0;
508 node->next = NULL;
Waiman Longa23db282015-04-24 14:56:37 -0400509 pv_init_node(node);
Waiman Longa33fda32015-04-24 14:56:30 -0400510
511 /*
Waiman Long6403bd72015-04-24 14:56:33 -0400512 * We touched a (possibly) cold cacheline in the per-cpu queue node;
513 * attempt the trylock once more in the hope someone let go while we
514 * weren't watching.
515 */
516 if (queued_spin_trylock(lock))
517 goto release;
518
519 /*
Peter Zijlstra (Intel)c1fb1592015-04-24 14:56:32 -0400520 * We have already touched the queueing cacheline; don't bother with
521 * pending stuff.
522 *
Waiman Long6403bd72015-04-24 14:56:33 -0400523 * p,*,* -> n,*,*
Peter Zijlstra8d53fa12016-06-08 09:12:30 +0200524 *
525 * RELEASE, such that the stores to @node must be complete.
Waiman Longa33fda32015-04-24 14:56:30 -0400526 */
Waiman Long6403bd72015-04-24 14:56:33 -0400527 old = xchg_tail(lock, tail);
Waiman Longaa687442015-11-09 19:09:23 -0500528 next = NULL;
Waiman Longa33fda32015-04-24 14:56:30 -0400529
530 /*
531 * if there was a previous node; link it and wait until reaching the
532 * head of the waitqueue.
533 */
Waiman Long6403bd72015-04-24 14:56:33 -0400534 if (old & _Q_TAIL_MASK) {
Waiman Longa33fda32015-04-24 14:56:30 -0400535 prev = decode_tail(old);
Peter Zijlstra8d53fa12016-06-08 09:12:30 +0200536
Will Deacon48c42d42018-12-18 23:10:41 +0100537 /*
538 * We must ensure that the stores to @node are observed before
539 * the write to prev->next. The address dependency from
540 * xchg_tail is not sufficient to ensure this because the read
541 * component of xchg_tail is unordered with respect to the
542 * initialisation of @node.
543 */
544 smp_store_release(&prev->next, node);
Waiman Longa33fda32015-04-24 14:56:30 -0400545
Waiman Longcd0272f2015-11-09 19:09:27 -0500546 pv_wait_node(node, prev);
Waiman Longa33fda32015-04-24 14:56:30 -0400547 arch_mcs_spin_lock_contended(&node->locked);
Waiman Long81b55982015-11-09 19:09:22 -0500548
549 /*
550 * While waiting for the MCS lock, the next pointer may have
551 * been set by another lock waiter. We optimistically load
552 * the next pointer & prefetch the cacheline for writing
553 * to reduce latency in the upcoming MCS unlock operation.
554 */
555 next = READ_ONCE(node->next);
556 if (next)
557 prefetchw(next);
Waiman Longa33fda32015-04-24 14:56:30 -0400558 }
559
560 /*
Peter Zijlstra (Intel)c1fb1592015-04-24 14:56:32 -0400561 * we're at the head of the waitqueue, wait for the owner & pending to
562 * go away.
Waiman Longa33fda32015-04-24 14:56:30 -0400563 *
Peter Zijlstra (Intel)c1fb1592015-04-24 14:56:32 -0400564 * *,x,y -> *,0,0
Waiman Long2c83e8e2015-04-24 14:56:35 -0400565 *
566 * this wait loop must use a load-acquire such that we match the
567 * store-release that clears the locked bit and create lock
568 * sequentiality; this is because the set_locked() function below
569 * does not imply a full barrier.
570 *
Waiman Long1c4941f2015-11-10 16:18:56 -0500571 * The PV pv_wait_head_or_lock function, if active, will acquire
572 * the lock and return a non-zero value. So we have to skip the
Peter Zijlstra1f03e8d2016-04-04 10:57:12 +0200573 * smp_cond_load_acquire() call. As the next PV queue head hasn't been
Waiman Long1c4941f2015-11-10 16:18:56 -0500574 * designated yet, there is no way for the locked value to become
575 * _Q_SLOW_VAL. So both the set_locked() and the
576 * atomic_cmpxchg_relaxed() calls will be safe.
577 *
578 * If PV isn't active, 0 will be returned instead.
579 *
Waiman Longa33fda32015-04-24 14:56:30 -0400580 */
Waiman Long1c4941f2015-11-10 16:18:56 -0500581 if ((val = pv_wait_head_or_lock(lock, node)))
582 goto locked;
583
Peter Zijlstra1f03e8d2016-04-04 10:57:12 +0200584 val = smp_cond_load_acquire(&lock->val.counter, !(VAL & _Q_LOCKED_PENDING_MASK));
Waiman Longa33fda32015-04-24 14:56:30 -0400585
Waiman Long1c4941f2015-11-10 16:18:56 -0500586locked:
Waiman Longa33fda32015-04-24 14:56:30 -0400587 /*
588 * claim the lock:
589 *
Peter Zijlstra (Intel)c1fb1592015-04-24 14:56:32 -0400590 * n,0,0 -> 0,0,1 : lock, uncontended
Will Deacon9b588432018-12-18 23:10:44 +0100591 * *,*,0 -> *,*,1 : lock, contended
Waiman Long2c83e8e2015-04-24 14:56:35 -0400592 *
Will Deacon9b588432018-12-18 23:10:44 +0100593 * If the queue head is the only one in the queue (lock value == tail)
594 * and nobody is pending, clear the tail code and grab the lock.
595 * Otherwise, we only need to grab the lock.
Waiman Longa33fda32015-04-24 14:56:30 -0400596 */
Will Deacon0952e8f2018-12-18 23:10:46 +0100597
598 /* In the PV case we might already have _Q_LOCKED_VAL set */
599 if ((val & _Q_TAIL_MASK) == tail) {
Waiman Long64d816c2015-11-09 19:09:21 -0500600 /*
Peter Zijlstra1f03e8d2016-04-04 10:57:12 +0200601 * The smp_cond_load_acquire() call above has provided the
Will Deacon0952e8f2018-12-18 23:10:46 +0100602 * necessary acquire semantics required for locking.
Waiman Long64d816c2015-11-09 19:09:21 -0500603 */
604 old = atomic_cmpxchg_relaxed(&lock->val, val, _Q_LOCKED_VAL);
Waiman Long2c83e8e2015-04-24 14:56:35 -0400605 if (old == val)
Will Deacon0952e8f2018-12-18 23:10:46 +0100606 goto release; /* No contention */
Waiman Longa33fda32015-04-24 14:56:30 -0400607 }
608
Will Deacon0952e8f2018-12-18 23:10:46 +0100609 /* Either somebody is queued behind us or _Q_PENDING_VAL is set */
610 set_locked(lock);
611
Waiman Longa33fda32015-04-24 14:56:30 -0400612 /*
Waiman Longaa687442015-11-09 19:09:23 -0500613 * contended path; wait for next if not observed yet, release.
Waiman Longa33fda32015-04-24 14:56:30 -0400614 */
Waiman Longaa687442015-11-09 19:09:23 -0500615 if (!next) {
616 while (!(next = READ_ONCE(node->next)))
617 cpu_relax();
618 }
Waiman Longa33fda32015-04-24 14:56:30 -0400619
Waiman Long2c83e8e2015-04-24 14:56:35 -0400620 arch_mcs_spin_unlock_contended(&next->locked);
Waiman Long75d22702015-07-11 16:36:52 -0400621 pv_kick_node(lock, next);
Waiman Longa33fda32015-04-24 14:56:30 -0400622
623release:
624 /*
625 * release the node
626 */
Pan Xinhui0dceeaf2016-06-14 14:37:27 +0800627 __this_cpu_dec(mcs_nodes[0].count);
Waiman Longa33fda32015-04-24 14:56:30 -0400628}
629EXPORT_SYMBOL(queued_spin_lock_slowpath);
Waiman Longa23db282015-04-24 14:56:37 -0400630
631/*
632 * Generate the paravirt code for queued_spin_unlock_slowpath().
633 */
634#if !defined(_GEN_PV_LOCK_SLOWPATH) && defined(CONFIG_PARAVIRT_SPINLOCKS)
635#define _GEN_PV_LOCK_SLOWPATH
636
637#undef pv_enabled
638#define pv_enabled() true
639
640#undef pv_init_node
641#undef pv_wait_node
642#undef pv_kick_node
Waiman Long1c4941f2015-11-10 16:18:56 -0500643#undef pv_wait_head_or_lock
Waiman Longa23db282015-04-24 14:56:37 -0400644
645#undef queued_spin_lock_slowpath
646#define queued_spin_lock_slowpath __pv_queued_spin_lock_slowpath
647
648#include "qspinlock_paravirt.h"
649#include "qspinlock.c"
650
651#endif