blob: af9c2ef6e9307e66fa9c1cfbdec08d04078f9a9b [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.
17 *
18 * Authors: Waiman Long <waiman.long@hp.com>
19 * Peter Zijlstra <peterz@infradead.org>
20 */
21#include <linux/smp.h>
22#include <linux/bug.h>
23#include <linux/cpumask.h>
24#include <linux/percpu.h>
25#include <linux/hardirq.h>
26#include <linux/mutex.h>
27#include <asm/qspinlock.h>
28
29/*
30 * The basic principle of a queue-based spinlock can best be understood
31 * by studying a classic queue-based spinlock implementation called the
32 * MCS lock. The paper below provides a good description for this kind
33 * of lock.
34 *
35 * http://www.cise.ufl.edu/tr/DOC/REP-1992-71.pdf
36 *
37 * This queued spinlock implementation is based on the MCS lock, however to make
38 * it fit the 4 bytes we assume spinlock_t to be, and preserve its existing
39 * API, we must modify it somehow.
40 *
41 * In particular; where the traditional MCS lock consists of a tail pointer
42 * (8 bytes) and needs the next pointer (another 8 bytes) of its own node to
43 * unlock the next pending (next->locked), we compress both these: {tail,
44 * next->locked} into a single u32 value.
45 *
46 * Since a spinlock disables recursion of its own context and there is a limit
47 * to the contexts that can nest; namely: task, softirq, hardirq, nmi. As there
48 * are at most 4 nesting levels, it can be encoded by a 2-bit number. Now
49 * we can encode the tail by combining the 2-bit nesting level with the cpu
50 * number. With one byte for the lock value and 3 bytes for the tail, only a
51 * 32-bit word is now needed. Even though we only need 1 bit for the lock,
52 * we extend it to a full byte to achieve better performance for architectures
53 * that support atomic byte write.
54 *
55 * We also change the first spinner to spin on the lock bit instead of its
56 * node; whereby avoiding the need to carry a node from lock to unlock, and
57 * preserving existing lock API. This also makes the unlock code simpler and
58 * faster.
59 */
60
61#include "mcs_spinlock.h"
62
63/*
64 * Per-CPU queue node structures; we can never have more than 4 nested
65 * contexts: task, softirq, hardirq, nmi.
66 *
67 * Exactly fits one 64-byte cacheline on a 64-bit architecture.
68 */
69static DEFINE_PER_CPU_ALIGNED(struct mcs_spinlock, mcs_nodes[4]);
70
71/*
72 * We must be able to distinguish between no-tail and the tail at 0:0,
73 * therefore increment the cpu number by one.
74 */
75
76static inline u32 encode_tail(int cpu, int idx)
77{
78 u32 tail;
79
80#ifdef CONFIG_DEBUG_SPINLOCK
81 BUG_ON(idx > 3);
82#endif
83 tail = (cpu + 1) << _Q_TAIL_CPU_OFFSET;
84 tail |= idx << _Q_TAIL_IDX_OFFSET; /* assume < 4 */
85
86 return tail;
87}
88
89static inline struct mcs_spinlock *decode_tail(u32 tail)
90{
91 int cpu = (tail >> _Q_TAIL_CPU_OFFSET) - 1;
92 int idx = (tail & _Q_TAIL_IDX_MASK) >> _Q_TAIL_IDX_OFFSET;
93
94 return per_cpu_ptr(&mcs_nodes[idx], cpu);
95}
96
Peter Zijlstra (Intel)c1fb1592015-04-24 14:56:32 -040097#define _Q_LOCKED_PENDING_MASK (_Q_LOCKED_MASK | _Q_PENDING_MASK)
98
Waiman Longa33fda32015-04-24 14:56:30 -040099/**
100 * queued_spin_lock_slowpath - acquire the queued spinlock
101 * @lock: Pointer to queued spinlock structure
102 * @val: Current value of the queued spinlock 32-bit word
103 *
Peter Zijlstra (Intel)c1fb1592015-04-24 14:56:32 -0400104 * (queue tail, pending bit, lock value)
Waiman Longa33fda32015-04-24 14:56:30 -0400105 *
Peter Zijlstra (Intel)c1fb1592015-04-24 14:56:32 -0400106 * fast : slow : unlock
107 * : :
108 * uncontended (0,0,0) -:--> (0,0,1) ------------------------------:--> (*,*,0)
109 * : | ^--------.------. / :
110 * : v \ \ | :
111 * pending : (0,1,1) +--> (0,1,0) \ | :
112 * : | ^--' | | :
113 * : v | | :
114 * uncontended : (n,x,y) +--> (n,0,0) --' | :
115 * queue : | ^--' | :
116 * : v | :
117 * contended : (*,x,y) +--> (*,0,0) ---> (*,0,1) -' :
118 * queue : ^--' :
Waiman Longa33fda32015-04-24 14:56:30 -0400119 */
120void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)
121{
122 struct mcs_spinlock *prev, *next, *node;
123 u32 new, old, tail;
124 int idx;
125
126 BUILD_BUG_ON(CONFIG_NR_CPUS >= (1U << _Q_TAIL_CPU_BITS));
127
Peter Zijlstra (Intel)c1fb1592015-04-24 14:56:32 -0400128 /*
129 * wait for in-progress pending->locked hand-overs
130 *
131 * 0,1,0 -> 0,0,1
132 */
133 if (val == _Q_PENDING_VAL) {
134 while ((val = atomic_read(&lock->val)) == _Q_PENDING_VAL)
135 cpu_relax();
136 }
137
138 /*
139 * trylock || pending
140 *
141 * 0,0,0 -> 0,0,1 ; trylock
142 * 0,0,1 -> 0,1,1 ; pending
143 */
144 for (;;) {
145 /*
146 * If we observe any contention; queue.
147 */
148 if (val & ~_Q_LOCKED_MASK)
149 goto queue;
150
151 new = _Q_LOCKED_VAL;
152 if (val == new)
153 new |= _Q_PENDING_VAL;
154
155 old = atomic_cmpxchg(&lock->val, val, new);
156 if (old == val)
157 break;
158
159 val = old;
160 }
161
162 /*
163 * we won the trylock
164 */
165 if (new == _Q_LOCKED_VAL)
166 return;
167
168 /*
169 * we're pending, wait for the owner to go away.
170 *
171 * *,1,1 -> *,1,0
172 */
173 while ((val = atomic_read(&lock->val)) & _Q_LOCKED_MASK)
174 cpu_relax();
175
176 /*
177 * take ownership and clear the pending bit.
178 *
179 * *,1,0 -> *,0,1
180 */
181 for (;;) {
182 new = (val & ~_Q_PENDING_MASK) | _Q_LOCKED_VAL;
183
184 old = atomic_cmpxchg(&lock->val, val, new);
185 if (old == val)
186 break;
187
188 val = old;
189 }
190 return;
191
192 /*
193 * End of pending bit optimistic spinning and beginning of MCS
194 * queuing.
195 */
196queue:
Waiman Longa33fda32015-04-24 14:56:30 -0400197 node = this_cpu_ptr(&mcs_nodes[0]);
198 idx = node->count++;
199 tail = encode_tail(smp_processor_id(), idx);
200
201 node += idx;
202 node->locked = 0;
203 node->next = NULL;
204
205 /*
Peter Zijlstra (Intel)c1fb1592015-04-24 14:56:32 -0400206 * We have already touched the queueing cacheline; don't bother with
207 * pending stuff.
208 *
Waiman Longa33fda32015-04-24 14:56:30 -0400209 * trylock || xchg(lock, node)
210 *
Peter Zijlstra (Intel)c1fb1592015-04-24 14:56:32 -0400211 * 0,0,0 -> 0,0,1 ; no tail, not locked -> no tail, locked.
212 * p,y,x -> n,y,x ; tail was p -> tail is n; preserving locked.
Waiman Longa33fda32015-04-24 14:56:30 -0400213 */
214 for (;;) {
215 new = _Q_LOCKED_VAL;
216 if (val)
Peter Zijlstra (Intel)c1fb1592015-04-24 14:56:32 -0400217 new = tail | (val & _Q_LOCKED_PENDING_MASK);
Waiman Longa33fda32015-04-24 14:56:30 -0400218
219 old = atomic_cmpxchg(&lock->val, val, new);
220 if (old == val)
221 break;
222
223 val = old;
224 }
225
226 /*
227 * we won the trylock; forget about queueing.
228 */
229 if (new == _Q_LOCKED_VAL)
230 goto release;
231
232 /*
233 * if there was a previous node; link it and wait until reaching the
234 * head of the waitqueue.
235 */
Peter Zijlstra (Intel)c1fb1592015-04-24 14:56:32 -0400236 if (old & ~_Q_LOCKED_PENDING_MASK) {
Waiman Longa33fda32015-04-24 14:56:30 -0400237 prev = decode_tail(old);
238 WRITE_ONCE(prev->next, node);
239
240 arch_mcs_spin_lock_contended(&node->locked);
241 }
242
243 /*
Peter Zijlstra (Intel)c1fb1592015-04-24 14:56:32 -0400244 * we're at the head of the waitqueue, wait for the owner & pending to
245 * go away.
Waiman Longa33fda32015-04-24 14:56:30 -0400246 *
Peter Zijlstra (Intel)c1fb1592015-04-24 14:56:32 -0400247 * *,x,y -> *,0,0
Waiman Longa33fda32015-04-24 14:56:30 -0400248 */
Peter Zijlstra (Intel)c1fb1592015-04-24 14:56:32 -0400249 while ((val = atomic_read(&lock->val)) & _Q_LOCKED_PENDING_MASK)
Waiman Longa33fda32015-04-24 14:56:30 -0400250 cpu_relax();
251
252 /*
253 * claim the lock:
254 *
Peter Zijlstra (Intel)c1fb1592015-04-24 14:56:32 -0400255 * n,0,0 -> 0,0,1 : lock, uncontended
256 * *,0,0 -> *,0,1 : lock, contended
Waiman Longa33fda32015-04-24 14:56:30 -0400257 */
258 for (;;) {
259 new = _Q_LOCKED_VAL;
260 if (val != tail)
261 new |= val;
262
263 old = atomic_cmpxchg(&lock->val, val, new);
264 if (old == val)
265 break;
266
267 val = old;
268 }
269
270 /*
271 * contended path; wait for next, release.
272 */
273 if (new != _Q_LOCKED_VAL) {
274 while (!(next = READ_ONCE(node->next)))
275 cpu_relax();
276
277 arch_mcs_spin_unlock_contended(&next->locked);
278 }
279
280release:
281 /*
282 * release the node
283 */
284 this_cpu_dec(mcs_nodes[0].count);
285}
286EXPORT_SYMBOL(queued_spin_lock_slowpath);