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.
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.
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.
18 * Authors: Waiman Long <waiman.long@hp.com>
19 * Peter Zijlstra <peterz@infradead.org>
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>
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
35 * http://www.cise.ufl.edu/tr/DOC/REP-1992-71.pdf
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.
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.
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.
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
61 #include "mcs_spinlock.h"
64 * Per-CPU queue node structures; we can never have more than 4 nested
65 * contexts: task, softirq, hardirq, nmi.
67 * Exactly fits one 64-byte cacheline on a 64-bit architecture.
69 static DEFINE_PER_CPU_ALIGNED(struct mcs_spinlock
, mcs_nodes
[4]);
72 * We must be able to distinguish between no-tail and the tail at 0:0,
73 * therefore increment the cpu number by one.
76 static inline u32
encode_tail(int cpu
, int idx
)
80 #ifdef CONFIG_DEBUG_SPINLOCK
83 tail
= (cpu
+ 1) << _Q_TAIL_CPU_OFFSET
;
84 tail
|= idx
<< _Q_TAIL_IDX_OFFSET
; /* assume < 4 */
89 static inline struct mcs_spinlock
*decode_tail(u32 tail
)
91 int cpu
= (tail
>> _Q_TAIL_CPU_OFFSET
) - 1;
92 int idx
= (tail
& _Q_TAIL_IDX_MASK
) >> _Q_TAIL_IDX_OFFSET
;
94 return per_cpu_ptr(&mcs_nodes
[idx
], cpu
);
98 * queued_spin_lock_slowpath - acquire the queued spinlock
99 * @lock: Pointer to queued spinlock structure
100 * @val: Current value of the queued spinlock 32-bit word
102 * (queue tail, lock value)
104 * fast : slow : unlock
106 * uncontended (0,0) --:--> (0,1) --------------------------------:--> (*,0)
109 * uncontended : (n,x) --+--> (n,0) | :
112 * contended : (*,x) --+--> (*,0) -----> (*,1) ---' :
116 void queued_spin_lock_slowpath(struct qspinlock
*lock
, u32 val
)
118 struct mcs_spinlock
*prev
, *next
, *node
;
122 BUILD_BUG_ON(CONFIG_NR_CPUS
>= (1U << _Q_TAIL_CPU_BITS
));
124 node
= this_cpu_ptr(&mcs_nodes
[0]);
126 tail
= encode_tail(smp_processor_id(), idx
);
133 * trylock || xchg(lock, node)
135 * 0,0 -> 0,1 ; no tail, not locked -> no tail, locked.
136 * p,x -> n,x ; tail was p -> tail is n; preserving locked.
141 new = tail
| (val
& _Q_LOCKED_MASK
);
143 old
= atomic_cmpxchg(&lock
->val
, val
, new);
151 * we won the trylock; forget about queueing.
153 if (new == _Q_LOCKED_VAL
)
157 * if there was a previous node; link it and wait until reaching the
158 * head of the waitqueue.
160 if (old
& ~_Q_LOCKED_MASK
) {
161 prev
= decode_tail(old
);
162 WRITE_ONCE(prev
->next
, node
);
164 arch_mcs_spin_lock_contended(&node
->locked
);
168 * we're at the head of the waitqueue, wait for the owner to go away.
172 while ((val
= atomic_read(&lock
->val
)) & _Q_LOCKED_MASK
)
178 * n,0 -> 0,1 : lock, uncontended
179 * *,0 -> *,1 : lock, contended
186 old
= atomic_cmpxchg(&lock
->val
, val
, new);
194 * contended path; wait for next, release.
196 if (new != _Q_LOCKED_VAL
) {
197 while (!(next
= READ_ONCE(node
->next
)))
200 arch_mcs_spin_unlock_contended(&next
->locked
);
207 this_cpu_dec(mcs_nodes
[0].count
);
209 EXPORT_SYMBOL(queued_spin_lock_slowpath
);