xref: /linux-6.15/kernel/locking/qspinlock.c (revision ac08f68f)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2a33fda35SWaiman Long /*
3a33fda35SWaiman Long  * Queued spinlock
4a33fda35SWaiman Long  *
5a33fda35SWaiman Long  * (C) Copyright 2013-2015 Hewlett-Packard Development Company, L.P.
681d3dc9aSWaiman Long  * (C) Copyright 2013-2014,2018 Red Hat, Inc.
7a33fda35SWaiman Long  * (C) Copyright 2015 Intel Corp.
864d816cbSWaiman Long  * (C) Copyright 2015 Hewlett-Packard Enterprise Development LP
9a33fda35SWaiman Long  *
1081d3dc9aSWaiman Long  * Authors: Waiman Long <[email protected]>
11a33fda35SWaiman Long  *          Peter Zijlstra <[email protected]>
12a33fda35SWaiman Long  */
13a23db284SWaiman Long 
14a23db284SWaiman Long #ifndef _GEN_PV_LOCK_SLOWPATH
15a23db284SWaiman Long 
16a33fda35SWaiman Long #include <linux/smp.h>
17a33fda35SWaiman Long #include <linux/bug.h>
18a33fda35SWaiman Long #include <linux/cpumask.h>
19a33fda35SWaiman Long #include <linux/percpu.h>
20a33fda35SWaiman Long #include <linux/hardirq.h>
21a33fda35SWaiman Long #include <linux/mutex.h>
225671360fSStafford Horne #include <linux/prefetch.h>
2369f9cae9SPeter Zijlstra (Intel) #include <asm/byteorder.h>
24a33fda35SWaiman Long #include <asm/qspinlock.h>
25ee042be1SNamhyung Kim #include <trace/events/lock.h>
26a33fda35SWaiman Long 
27a33fda35SWaiman Long /*
28*ac08f68fSKumar Kartikeya Dwivedi  * Include queued spinlock definitions and statistics code
2981d3dc9aSWaiman Long  */
30*ac08f68fSKumar Kartikeya Dwivedi #include "qspinlock.h"
3181d3dc9aSWaiman Long #include "qspinlock_stat.h"
3281d3dc9aSWaiman Long 
3381d3dc9aSWaiman Long /*
34a33fda35SWaiman Long  * The basic principle of a queue-based spinlock can best be understood
35a33fda35SWaiman Long  * by studying a classic queue-based spinlock implementation called the
3657097124SWaiman Long  * MCS lock. A copy of the original MCS lock paper ("Algorithms for Scalable
3757097124SWaiman Long  * Synchronization on Shared-Memory Multiprocessors by Mellor-Crummey and
3857097124SWaiman Long  * Scott") is available at
39a33fda35SWaiman Long  *
4057097124SWaiman Long  * https://bugzilla.kernel.org/show_bug.cgi?id=206115
41a33fda35SWaiman Long  *
4257097124SWaiman Long  * This queued spinlock implementation is based on the MCS lock, however to
4357097124SWaiman Long  * make it fit the 4 bytes we assume spinlock_t to be, and preserve its
4457097124SWaiman Long  * existing API, we must modify it somehow.
45a33fda35SWaiman Long  *
46a33fda35SWaiman Long  * In particular; where the traditional MCS lock consists of a tail pointer
47a33fda35SWaiman Long  * (8 bytes) and needs the next pointer (another 8 bytes) of its own node to
48a33fda35SWaiman Long  * unlock the next pending (next->locked), we compress both these: {tail,
49a33fda35SWaiman Long  * next->locked} into a single u32 value.
50a33fda35SWaiman Long  *
51a33fda35SWaiman Long  * Since a spinlock disables recursion of its own context and there is a limit
52a33fda35SWaiman Long  * to the contexts that can nest; namely: task, softirq, hardirq, nmi. As there
53a33fda35SWaiman Long  * are at most 4 nesting levels, it can be encoded by a 2-bit number. Now
54a33fda35SWaiman Long  * we can encode the tail by combining the 2-bit nesting level with the cpu
55a33fda35SWaiman Long  * number. With one byte for the lock value and 3 bytes for the tail, only a
56a33fda35SWaiman Long  * 32-bit word is now needed. Even though we only need 1 bit for the lock,
57a33fda35SWaiman Long  * we extend it to a full byte to achieve better performance for architectures
58a33fda35SWaiman Long  * that support atomic byte write.
59a33fda35SWaiman Long  *
60a33fda35SWaiman Long  * We also change the first spinner to spin on the lock bit instead of its
61a33fda35SWaiman Long  * node; whereby avoiding the need to carry a node from lock to unlock, and
62a33fda35SWaiman Long  * preserving existing lock API. This also makes the unlock code simpler and
63a33fda35SWaiman Long  * faster.
6469f9cae9SPeter Zijlstra (Intel)  *
6569f9cae9SPeter Zijlstra (Intel)  * N.B. The current implementation only supports architectures that allow
6669f9cae9SPeter Zijlstra (Intel)  *      atomic operations on smaller 8-bit and 16-bit data types.
6769f9cae9SPeter Zijlstra (Intel)  *
68a33fda35SWaiman Long  */
69a33fda35SWaiman Long 
70a33fda35SWaiman Long #include "mcs_spinlock.h"
716512276dSWill Deacon 
726512276dSWill Deacon /*
73a33fda35SWaiman Long  * Per-CPU queue node structures; we can never have more than 4 nested
74a33fda35SWaiman Long  * contexts: task, softirq, hardirq, nmi.
75a33fda35SWaiman Long  *
76a33fda35SWaiman Long  * Exactly fits one 64-byte cacheline on a 64-bit architecture.
77a23db284SWaiman Long  *
78a23db284SWaiman Long  * PV doubles the storage and uses the second cacheline for PV state.
79a33fda35SWaiman Long  */
80*ac08f68fSKumar Kartikeya Dwivedi static DEFINE_PER_CPU_ALIGNED(struct qnode, qnodes[_Q_MAX_NODES]);
81a23db284SWaiman Long 
82a23db284SWaiman Long /*
83a23db284SWaiman Long  * Generate the native code for queued_spin_unlock_slowpath(); provide NOPs for
84a23db284SWaiman Long  * all the PV callbacks.
85a23db284SWaiman Long  */
86a23db284SWaiman Long 
__pv_init_node(struct mcs_spinlock * node)87a23db284SWaiman Long static __always_inline void __pv_init_node(struct mcs_spinlock *node) { }
__pv_wait_node(struct mcs_spinlock * node,struct mcs_spinlock * prev)88cd0272faSWaiman Long static __always_inline void __pv_wait_node(struct mcs_spinlock *node,
89cd0272faSWaiman Long 					   struct mcs_spinlock *prev) { }
__pv_kick_node(struct qspinlock * lock,struct mcs_spinlock * node)9075d22702SWaiman Long static __always_inline void __pv_kick_node(struct qspinlock *lock,
9175d22702SWaiman Long 					   struct mcs_spinlock *node) { }
__pv_wait_head_or_lock(struct qspinlock * lock,struct mcs_spinlock * node)921c4941fdSWaiman Long static __always_inline u32  __pv_wait_head_or_lock(struct qspinlock *lock,
931c4941fdSWaiman Long 						   struct mcs_spinlock *node)
941c4941fdSWaiman Long 						   { return 0; }
95a23db284SWaiman Long 
96a23db284SWaiman Long #define pv_enabled()		false
97a23db284SWaiman Long 
98a23db284SWaiman Long #define pv_init_node		__pv_init_node
99a23db284SWaiman Long #define pv_wait_node		__pv_wait_node
100a23db284SWaiman Long #define pv_kick_node		__pv_kick_node
1011c4941fdSWaiman Long #define pv_wait_head_or_lock	__pv_wait_head_or_lock
102a23db284SWaiman Long 
103a23db284SWaiman Long #ifdef CONFIG_PARAVIRT_SPINLOCKS
104a23db284SWaiman Long #define queued_spin_lock_slowpath	native_queued_spin_lock_slowpath
105a23db284SWaiman Long #endif
106a23db284SWaiman Long 
107a23db284SWaiman Long #endif /* _GEN_PV_LOCK_SLOWPATH */
108a23db284SWaiman Long 
1092c83e8e9SWaiman Long /**
110a33fda35SWaiman Long  * queued_spin_lock_slowpath - acquire the queued spinlock
111a33fda35SWaiman Long  * @lock: Pointer to queued spinlock structure
112a33fda35SWaiman Long  * @val: Current value of the queued spinlock 32-bit word
113a33fda35SWaiman Long  *
114c1fb159dSPeter Zijlstra (Intel)  * (queue tail, pending bit, lock value)
115a33fda35SWaiman Long  *
116a33fda35SWaiman Long  *              fast     :    slow                                  :    unlock
117a33fda35SWaiman Long  *                       :                                          :
118c1fb159dSPeter Zijlstra (Intel)  * uncontended  (0,0,0) -:--> (0,0,1) ------------------------------:--> (*,*,0)
119c1fb159dSPeter Zijlstra (Intel)  *                       :       | ^--------.------.             /  :
120c1fb159dSPeter Zijlstra (Intel)  *                       :       v           \      \            |  :
121c1fb159dSPeter Zijlstra (Intel)  * pending               :    (0,1,1) +--> (0,1,0)   \           |  :
122c1fb159dSPeter Zijlstra (Intel)  *                       :       | ^--'              |           |  :
123c1fb159dSPeter Zijlstra (Intel)  *                       :       v                   |           |  :
124c1fb159dSPeter Zijlstra (Intel)  * uncontended           :    (n,x,y) +--> (n,0,0) --'           |  :
125a33fda35SWaiman Long  *   queue               :       | ^--'                          |  :
126a33fda35SWaiman Long  *                       :       v                               |  :
127c1fb159dSPeter Zijlstra (Intel)  * contended             :    (*,x,y) +--> (*,0,0) ---> (*,0,1) -'  :
128a33fda35SWaiman Long  *   queue               :         ^--'                             :
129a33fda35SWaiman Long  */
queued_spin_lock_slowpath(struct qspinlock * lock,u32 val)130501f7f69SNamhyung Kim void __lockfunc queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)
131a33fda35SWaiman Long {
132a33fda35SWaiman Long 	struct mcs_spinlock *prev, *next, *node;
13359fb586bSWill Deacon 	u32 old, tail;
134a33fda35SWaiman Long 	int idx;
135a33fda35SWaiman Long 
136a33fda35SWaiman Long 	BUILD_BUG_ON(CONFIG_NR_CPUS >= (1U << _Q_TAIL_CPU_BITS));
137a33fda35SWaiman Long 
138a23db284SWaiman Long 	if (pv_enabled())
13981d3dc9aSWaiman Long 		goto pv_queue;
140a23db284SWaiman Long 
14143b3f028SPeter Zijlstra 	if (virt_spin_lock(lock))
1422aa79af6SPeter Zijlstra (Intel) 		return;
1432aa79af6SPeter Zijlstra (Intel) 
144c1fb159dSPeter Zijlstra (Intel) 	/*
1456512276dSWill Deacon 	 * Wait for in-progress pending->locked hand-overs with a bounded
1466512276dSWill Deacon 	 * number of spins so that we guarantee forward progress.
147c1fb159dSPeter Zijlstra (Intel) 	 *
148c1fb159dSPeter Zijlstra (Intel) 	 * 0,1,0 -> 0,0,1
149c1fb159dSPeter Zijlstra (Intel) 	 */
150c1fb159dSPeter Zijlstra (Intel) 	if (val == _Q_PENDING_VAL) {
1516512276dSWill Deacon 		int cnt = _Q_PENDING_LOOPS;
1526512276dSWill Deacon 		val = atomic_cond_read_relaxed(&lock->val,
1536512276dSWill Deacon 					       (VAL != _Q_PENDING_VAL) || !cnt--);
154c1fb159dSPeter Zijlstra (Intel) 	}
155c1fb159dSPeter Zijlstra (Intel) 
156c1fb159dSPeter Zijlstra (Intel) 	/*
157c1fb159dSPeter Zijlstra (Intel) 	 * If we observe any contention; queue.
158c1fb159dSPeter Zijlstra (Intel) 	 */
159c1fb159dSPeter Zijlstra (Intel) 	if (val & ~_Q_LOCKED_MASK)
160c1fb159dSPeter Zijlstra (Intel) 		goto queue;
161c1fb159dSPeter Zijlstra (Intel) 
16264d816cbSWaiman Long 	/*
16359fb586bSWill Deacon 	 * trylock || pending
16459fb586bSWill Deacon 	 *
165756b1df4SPeter Zijlstra 	 * 0,0,* -> 0,1,* -> 0,0,1 pending, trylock
16664d816cbSWaiman Long 	 */
1677aa54be2SPeter Zijlstra 	val = queued_fetch_set_pending_acquire(lock);
168756b1df4SPeter Zijlstra 
16953bf57faSPeter Zijlstra 	/*
170756b1df4SPeter Zijlstra 	 * If we observe contention, there is a concurrent locker.
171756b1df4SPeter Zijlstra 	 *
172756b1df4SPeter Zijlstra 	 * Undo and queue; our setting of PENDING might have made the
173756b1df4SPeter Zijlstra 	 * n,0,0 -> 0,0,0 transition fail and it will now be waiting
174756b1df4SPeter Zijlstra 	 * on @next to become !NULL.
17553bf57faSPeter Zijlstra 	 */
17653bf57faSPeter Zijlstra 	if (unlikely(val & ~_Q_LOCKED_MASK)) {
177756b1df4SPeter Zijlstra 
178756b1df4SPeter Zijlstra 		/* Undo PENDING if we set it. */
17953bf57faSPeter Zijlstra 		if (!(val & _Q_PENDING_MASK))
18053bf57faSPeter Zijlstra 			clear_pending(lock);
181756b1df4SPeter Zijlstra 
18253bf57faSPeter Zijlstra 		goto queue;
18353bf57faSPeter Zijlstra 	}
18453bf57faSPeter Zijlstra 
185c1fb159dSPeter Zijlstra (Intel) 	/*
18659fb586bSWill Deacon 	 * We're pending, wait for the owner to go away.
187c1fb159dSPeter Zijlstra (Intel) 	 *
1884282494aSGuo Ren 	 * 0,1,1 -> *,1,0
18969f9cae9SPeter Zijlstra (Intel) 	 *
19069f9cae9SPeter Zijlstra (Intel) 	 * this wait loop must be a load-acquire such that we match the
19169f9cae9SPeter Zijlstra (Intel) 	 * store-release that clears the locked bit and create lock
19259fb586bSWill Deacon 	 * sequentiality; this is because not all
19359fb586bSWill Deacon 	 * clear_pending_set_locked() implementations imply full
19459fb586bSWill Deacon 	 * barriers.
195c1fb159dSPeter Zijlstra (Intel) 	 */
19653bf57faSPeter Zijlstra 	if (val & _Q_LOCKED_MASK)
1974282494aSGuo Ren 		smp_cond_load_acquire(&lock->locked, !VAL);
198c1fb159dSPeter Zijlstra (Intel) 
199c1fb159dSPeter Zijlstra (Intel) 	/*
200c1fb159dSPeter Zijlstra (Intel) 	 * take ownership and clear the pending bit.
201c1fb159dSPeter Zijlstra (Intel) 	 *
20253bf57faSPeter Zijlstra 	 * 0,1,0 -> 0,0,1
203c1fb159dSPeter Zijlstra (Intel) 	 */
2046403bd7dSWaiman Long 	clear_pending_set_locked(lock);
205ad53fa10SWaiman Long 	lockevent_inc(lock_pending);
206c1fb159dSPeter Zijlstra (Intel) 	return;
207c1fb159dSPeter Zijlstra (Intel) 
208c1fb159dSPeter Zijlstra (Intel) 	/*
209c1fb159dSPeter Zijlstra (Intel) 	 * End of pending bit optimistic spinning and beginning of MCS
210c1fb159dSPeter Zijlstra (Intel) 	 * queuing.
211c1fb159dSPeter Zijlstra (Intel) 	 */
212c1fb159dSPeter Zijlstra (Intel) queue:
213ad53fa10SWaiman Long 	lockevent_inc(lock_slowpath);
21481d3dc9aSWaiman Long pv_queue:
2150fa809caSWaiman Long 	node = this_cpu_ptr(&qnodes[0].mcs);
216a33fda35SWaiman Long 	idx = node->count++;
217a33fda35SWaiman Long 	tail = encode_tail(smp_processor_id(), idx);
218a33fda35SWaiman Long 
219ee042be1SNamhyung Kim 	trace_contention_begin(lock, LCB_F_SPIN);
220ee042be1SNamhyung Kim 
221d682b596SWaiman Long 	/*
222d682b596SWaiman Long 	 * 4 nodes are allocated based on the assumption that there will
223d682b596SWaiman Long 	 * not be nested NMIs taking spinlocks. That may not be true in
224d682b596SWaiman Long 	 * some architectures even though the chance of needing more than
225d682b596SWaiman Long 	 * 4 nodes will still be extremely unlikely. When that happens,
226d682b596SWaiman Long 	 * we fall back to spinning on the lock directly without using
227d682b596SWaiman Long 	 * any MCS node. This is not the most elegant solution, but is
228d682b596SWaiman Long 	 * simple enough.
229d682b596SWaiman Long 	 */
230*ac08f68fSKumar Kartikeya Dwivedi 	if (unlikely(idx >= _Q_MAX_NODES)) {
231ad53fa10SWaiman Long 		lockevent_inc(lock_no_node);
232d682b596SWaiman Long 		while (!queued_spin_trylock(lock))
233d682b596SWaiman Long 			cpu_relax();
234d682b596SWaiman Long 		goto release;
235d682b596SWaiman Long 	}
236d682b596SWaiman Long 
2370fa809caSWaiman Long 	node = grab_mcs_node(node, idx);
23811dc1322SWill Deacon 
23911dc1322SWill Deacon 	/*
2401222109aSWaiman Long 	 * Keep counts of non-zero index values:
2411222109aSWaiman Long 	 */
242ad53fa10SWaiman Long 	lockevent_cond_inc(lock_use_node2 + idx - 1, idx);
2431222109aSWaiman Long 
2441222109aSWaiman Long 	/*
24511dc1322SWill Deacon 	 * Ensure that we increment the head node->count before initialising
24611dc1322SWill Deacon 	 * the actual node. If the compiler is kind enough to reorder these
24711dc1322SWill Deacon 	 * stores, then an IRQ could overwrite our assignments.
24811dc1322SWill Deacon 	 */
24911dc1322SWill Deacon 	barrier();
25011dc1322SWill Deacon 
251a33fda35SWaiman Long 	node->locked = 0;
252a33fda35SWaiman Long 	node->next = NULL;
253a23db284SWaiman Long 	pv_init_node(node);
254a33fda35SWaiman Long 
255a33fda35SWaiman Long 	/*
2566403bd7dSWaiman Long 	 * We touched a (possibly) cold cacheline in the per-cpu queue node;
2576403bd7dSWaiman Long 	 * attempt the trylock once more in the hope someone let go while we
2586403bd7dSWaiman Long 	 * weren't watching.
2596403bd7dSWaiman Long 	 */
2606403bd7dSWaiman Long 	if (queued_spin_trylock(lock))
2616403bd7dSWaiman Long 		goto release;
2626403bd7dSWaiman Long 
2636403bd7dSWaiman Long 	/*
2649d4646d1SWill Deacon 	 * Ensure that the initialisation of @node is complete before we
2659d4646d1SWill Deacon 	 * publish the updated tail via xchg_tail() and potentially link
2669d4646d1SWill Deacon 	 * @node into the waitqueue via WRITE_ONCE(prev->next, node) below.
2679d4646d1SWill Deacon 	 */
2689d4646d1SWill Deacon 	smp_wmb();
2699d4646d1SWill Deacon 
2709d4646d1SWill Deacon 	/*
2719d4646d1SWill Deacon 	 * Publish the updated tail.
272c1fb159dSPeter Zijlstra (Intel) 	 * We have already touched the queueing cacheline; don't bother with
273c1fb159dSPeter Zijlstra (Intel) 	 * pending stuff.
274c1fb159dSPeter Zijlstra (Intel) 	 *
2756403bd7dSWaiman Long 	 * p,*,* -> n,*,*
276a33fda35SWaiman Long 	 */
2776403bd7dSWaiman Long 	old = xchg_tail(lock, tail);
278aa68744fSWaiman Long 	next = NULL;
279a33fda35SWaiman Long 
280a33fda35SWaiman Long 	/*
281a33fda35SWaiman Long 	 * if there was a previous node; link it and wait until reaching the
282a33fda35SWaiman Long 	 * head of the waitqueue.
283a33fda35SWaiman Long 	 */
2846403bd7dSWaiman Long 	if (old & _Q_TAIL_MASK) {
285*ac08f68fSKumar Kartikeya Dwivedi 		prev = decode_tail(old, qnodes);
2868d53fa19SPeter Zijlstra 
2879d4646d1SWill Deacon 		/* Link @node into the waitqueue. */
2889d4646d1SWill Deacon 		WRITE_ONCE(prev->next, node);
289a33fda35SWaiman Long 
290cd0272faSWaiman Long 		pv_wait_node(node, prev);
291a33fda35SWaiman Long 		arch_mcs_spin_lock_contended(&node->locked);
29281b55986SWaiman Long 
29381b55986SWaiman Long 		/*
29481b55986SWaiman Long 		 * While waiting for the MCS lock, the next pointer may have
29581b55986SWaiman Long 		 * been set by another lock waiter. We optimistically load
29681b55986SWaiman Long 		 * the next pointer & prefetch the cacheline for writing
29781b55986SWaiman Long 		 * to reduce latency in the upcoming MCS unlock operation.
29881b55986SWaiman Long 		 */
29981b55986SWaiman Long 		next = READ_ONCE(node->next);
30081b55986SWaiman Long 		if (next)
30181b55986SWaiman Long 			prefetchw(next);
302a33fda35SWaiman Long 	}
303a33fda35SWaiman Long 
304a33fda35SWaiman Long 	/*
305c1fb159dSPeter Zijlstra (Intel) 	 * we're at the head of the waitqueue, wait for the owner & pending to
306c1fb159dSPeter Zijlstra (Intel) 	 * go away.
307a33fda35SWaiman Long 	 *
308c1fb159dSPeter Zijlstra (Intel) 	 * *,x,y -> *,0,0
3092c83e8e9SWaiman Long 	 *
3102c83e8e9SWaiman Long 	 * this wait loop must use a load-acquire such that we match the
3112c83e8e9SWaiman Long 	 * store-release that clears the locked bit and create lock
3122c83e8e9SWaiman Long 	 * sequentiality; this is because the set_locked() function below
3132c83e8e9SWaiman Long 	 * does not imply a full barrier.
3142c83e8e9SWaiman Long 	 *
3151c4941fdSWaiman Long 	 * The PV pv_wait_head_or_lock function, if active, will acquire
3161c4941fdSWaiman Long 	 * the lock and return a non-zero value. So we have to skip the
317f9c811faSWill Deacon 	 * atomic_cond_read_acquire() call. As the next PV queue head hasn't
318f9c811faSWill Deacon 	 * been designated yet, there is no way for the locked value to become
3191c4941fdSWaiman Long 	 * _Q_SLOW_VAL. So both the set_locked() and the
3201c4941fdSWaiman Long 	 * atomic_cmpxchg_relaxed() calls will be safe.
3211c4941fdSWaiman Long 	 *
3221c4941fdSWaiman Long 	 * If PV isn't active, 0 will be returned instead.
3231c4941fdSWaiman Long 	 *
324a33fda35SWaiman Long 	 */
3251c4941fdSWaiman Long 	if ((val = pv_wait_head_or_lock(lock, node)))
3261c4941fdSWaiman Long 		goto locked;
3271c4941fdSWaiman Long 
328f9c811faSWill Deacon 	val = atomic_cond_read_acquire(&lock->val, !(VAL & _Q_LOCKED_PENDING_MASK));
329a33fda35SWaiman Long 
3301c4941fdSWaiman Long locked:
331a33fda35SWaiman Long 	/*
332a33fda35SWaiman Long 	 * claim the lock:
333a33fda35SWaiman Long 	 *
334c1fb159dSPeter Zijlstra (Intel) 	 * n,0,0 -> 0,0,1 : lock, uncontended
33559fb586bSWill Deacon 	 * *,*,0 -> *,*,1 : lock, contended
3362c83e8e9SWaiman Long 	 *
33759fb586bSWill Deacon 	 * If the queue head is the only one in the queue (lock value == tail)
33859fb586bSWill Deacon 	 * and nobody is pending, clear the tail code and grab the lock.
33959fb586bSWill Deacon 	 * Otherwise, we only need to grab the lock.
340a33fda35SWaiman Long 	 */
341c61da58dSWill Deacon 
34264d816cbSWaiman Long 	/*
343756b1df4SPeter Zijlstra 	 * In the PV case we might already have _Q_LOCKED_VAL set, because
344756b1df4SPeter Zijlstra 	 * of lock stealing; therefore we must also allow:
345ae75d908SWill Deacon 	 *
346756b1df4SPeter Zijlstra 	 * n,0,1 -> 0,0,1
347756b1df4SPeter Zijlstra 	 *
348756b1df4SPeter Zijlstra 	 * Note: at this point: (val & _Q_PENDING_MASK) == 0, because of the
349756b1df4SPeter Zijlstra 	 *       above wait condition, therefore any concurrent setting of
350756b1df4SPeter Zijlstra 	 *       PENDING will make the uncontended transition fail.
35164d816cbSWaiman Long 	 */
352756b1df4SPeter Zijlstra 	if ((val & _Q_TAIL_MASK) == tail) {
353756b1df4SPeter Zijlstra 		if (atomic_try_cmpxchg_relaxed(&lock->val, &val, _Q_LOCKED_VAL))
3542c83e8e9SWaiman Long 			goto release; /* No contention */
355756b1df4SPeter Zijlstra 	}
356a33fda35SWaiman Long 
357756b1df4SPeter Zijlstra 	/*
358756b1df4SPeter Zijlstra 	 * Either somebody is queued behind us or _Q_PENDING_VAL got set
359756b1df4SPeter Zijlstra 	 * which will then detect the remaining tail and queue behind us
360756b1df4SPeter Zijlstra 	 * ensuring we'll see a @next.
361756b1df4SPeter Zijlstra 	 */
362c61da58dSWill Deacon 	set_locked(lock);
363c61da58dSWill Deacon 
364a33fda35SWaiman Long 	/*
365aa68744fSWaiman Long 	 * contended path; wait for next if not observed yet, release.
366a33fda35SWaiman Long 	 */
367c131a198SWill Deacon 	if (!next)
368c131a198SWill Deacon 		next = smp_cond_load_relaxed(&node->next, (VAL));
369a33fda35SWaiman Long 
370a33fda35SWaiman Long 	arch_mcs_spin_unlock_contended(&next->locked);
37175d22702SWaiman Long 	pv_kick_node(lock, next);
372a33fda35SWaiman Long 
373a33fda35SWaiman Long release:
374ee042be1SNamhyung Kim 	trace_contention_end(lock, 0);
375ee042be1SNamhyung Kim 
376a33fda35SWaiman Long 	/*
377a33fda35SWaiman Long 	 * release the node
378a33fda35SWaiman Long 	 */
3790fa809caSWaiman Long 	__this_cpu_dec(qnodes[0].mcs.count);
380a33fda35SWaiman Long }
381a33fda35SWaiman Long EXPORT_SYMBOL(queued_spin_lock_slowpath);
382a23db284SWaiman Long 
383a23db284SWaiman Long /*
384a23db284SWaiman Long  * Generate the paravirt code for queued_spin_unlock_slowpath().
385a23db284SWaiman Long  */
386a23db284SWaiman Long #if !defined(_GEN_PV_LOCK_SLOWPATH) && defined(CONFIG_PARAVIRT_SPINLOCKS)
387a23db284SWaiman Long #define _GEN_PV_LOCK_SLOWPATH
388a23db284SWaiman Long 
389a23db284SWaiman Long #undef  pv_enabled
390a23db284SWaiman Long #define pv_enabled()	true
391a23db284SWaiman Long 
392a23db284SWaiman Long #undef pv_init_node
393a23db284SWaiman Long #undef pv_wait_node
394a23db284SWaiman Long #undef pv_kick_node
3951c4941fdSWaiman Long #undef pv_wait_head_or_lock
396a23db284SWaiman Long 
397a23db284SWaiman Long #undef  queued_spin_lock_slowpath
398a23db284SWaiman Long #define queued_spin_lock_slowpath	__pv_queued_spin_lock_slowpath
399a23db284SWaiman Long 
400a23db284SWaiman Long #include "qspinlock_paravirt.h"
401a23db284SWaiman Long #include "qspinlock.c"
402a23db284SWaiman Long 
4039fe6a8c5SJuergen Gross bool nopvspin;
parse_nopvspin(char * arg)40405eee619SZhenzhong Duan static __init int parse_nopvspin(char *arg)
40505eee619SZhenzhong Duan {
40605eee619SZhenzhong Duan 	nopvspin = true;
40705eee619SZhenzhong Duan 	return 0;
40805eee619SZhenzhong Duan }
40905eee619SZhenzhong Duan early_param("nopvspin", parse_nopvspin);
410a23db284SWaiman Long #endif
411