blob: d5610ad52b92b55b98b6a35f8700c5df940e8f81 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Peter Zijlstrafb0527b2014-01-29 12:51:42 +01002#include <linux/percpu.h>
Peter Zijlstrafb0527b2014-01-29 12:51:42 +01003#include <linux/sched.h>
Davidlohr Buesod84b6722015-01-06 11:45:07 -08004#include <linux/osq_lock.h>
Peter Zijlstrafb0527b2014-01-29 12:51:42 +01005
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 */
Jason Low046a6192014-07-14 10:27:48 -070014static DEFINE_PER_CPU_SHARED_ALIGNED(struct optimistic_spin_node, osq_node);
Peter Zijlstrafb0527b2014-01-29 12:51:42 +010015
16/*
Jason Low90631822014-07-14 10:27:49 -070017 * We use the value 0 to represent "no CPU", thus the encoded value
18 * will be the CPU number incremented by 1.
19 */
20static inline int encode_cpu(int cpu_nr)
21{
22 return cpu_nr + 1;
23}
24
Pan Xinhui5aff60a2016-11-02 05:08:29 -040025static inline int node_cpu(struct optimistic_spin_node *node)
26{
27 return node->cpu - 1;
28}
29
Jason Low90631822014-07-14 10:27:49 -070030static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val)
31{
32 int cpu_nr = encoded_cpu_val - 1;
33
34 return per_cpu_ptr(&osq_node, cpu_nr);
35}
36
37/*
Peter Zijlstrafb0527b2014-01-29 12:51:42 +010038 * Get a stable @node->next pointer, either for unlock() or unqueue() purposes.
39 * Can return NULL in case we were the last queued and we updated @lock instead.
40 */
Jason Low046a6192014-07-14 10:27:48 -070041static inline struct optimistic_spin_node *
Jason Low90631822014-07-14 10:27:49 -070042osq_wait_next(struct optimistic_spin_queue *lock,
Jason Low046a6192014-07-14 10:27:48 -070043 struct optimistic_spin_node *node,
44 struct optimistic_spin_node *prev)
Peter Zijlstrafb0527b2014-01-29 12:51:42 +010045{
Jason Low046a6192014-07-14 10:27:48 -070046 struct optimistic_spin_node *next = NULL;
Jason Low90631822014-07-14 10:27:49 -070047 int curr = encode_cpu(smp_processor_id());
48 int old;
49
50 /*
51 * If there is a prev node in queue, then the 'old' value will be
52 * the prev node's CPU #, else it's set to OSQ_UNLOCKED_VAL since if
53 * we're currently last in queue, then the queue will then become empty.
54 */
55 old = prev ? prev->cpu : OSQ_UNLOCKED_VAL;
Peter Zijlstrafb0527b2014-01-29 12:51:42 +010056
57 for (;;) {
Jason Low90631822014-07-14 10:27:49 -070058 if (atomic_read(&lock->tail) == curr &&
Davidlohr Buesoc55a6ff2015-09-14 00:37:24 -070059 atomic_cmpxchg_acquire(&lock->tail, curr, old) == curr) {
Peter Zijlstrafb0527b2014-01-29 12:51:42 +010060 /*
61 * We were the last queued, we moved @lock back. @prev
62 * will now observe @lock and will complete its
63 * unlock()/unqueue().
64 */
65 break;
66 }
67
68 /*
69 * We must xchg() the @node->next value, because if we were to
70 * leave it in, a concurrent unlock()/unqueue() from
71 * @node->next might complete Step-A and think its @prev is
72 * still valid.
73 *
74 * If the concurrent unlock()/unqueue() wins the race, we'll
75 * wait for either @lock to point to us, through its Step-B, or
76 * wait for a new @node->next from its Step-C.
77 */
78 if (node->next) {
79 next = xchg(&node->next, NULL);
80 if (next)
81 break;
82 }
83
Christian Borntraegerf2f09a42016-10-25 11:03:14 +020084 cpu_relax();
Peter Zijlstrafb0527b2014-01-29 12:51:42 +010085 }
86
87 return next;
88}
89
Jason Low90631822014-07-14 10:27:49 -070090bool osq_lock(struct optimistic_spin_queue *lock)
Peter Zijlstrafb0527b2014-01-29 12:51:42 +010091{
Jason Low046a6192014-07-14 10:27:48 -070092 struct optimistic_spin_node *node = this_cpu_ptr(&osq_node);
93 struct optimistic_spin_node *prev, *next;
Jason Low90631822014-07-14 10:27:49 -070094 int curr = encode_cpu(smp_processor_id());
95 int old;
Peter Zijlstrafb0527b2014-01-29 12:51:42 +010096
97 node->locked = 0;
98 node->next = NULL;
Jason Low90631822014-07-14 10:27:49 -070099 node->cpu = curr;
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100100
Davidlohr Buesoc55a6ff2015-09-14 00:37:24 -0700101 /*
Will Deaconb4b29f92015-12-11 17:46:41 +0000102 * We need both ACQUIRE (pairs with corresponding RELEASE in
103 * unlock() uncontended, or fastpath) and RELEASE (to publish
104 * the node fields we just initialised) semantics when updating
105 * the lock tail.
Davidlohr Buesoc55a6ff2015-09-14 00:37:24 -0700106 */
Will Deaconb4b29f92015-12-11 17:46:41 +0000107 old = atomic_xchg(&lock->tail, curr);
Jason Low90631822014-07-14 10:27:49 -0700108 if (old == OSQ_UNLOCKED_VAL)
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100109 return true;
110
Jason Low90631822014-07-14 10:27:49 -0700111 prev = decode_cpu(old);
112 node->prev = prev;
Prateek Sood50972fe2017-07-14 19:17:56 +0530113
114 /*
115 * osq_lock() unqueue
116 *
117 * node->prev = prev osq_wait_next()
118 * WMB MB
119 * prev->next = node next->prev = prev // unqueue-C
120 *
121 * Here 'node->prev' and 'next->prev' are the same variable and we need
122 * to ensure these stores happen in-order to avoid corrupting the list.
123 */
124 smp_wmb();
125
Davidlohr Bueso4d3199e2015-02-22 19:31:41 -0800126 WRITE_ONCE(prev->next, node);
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100127
128 /*
129 * Normally @prev is untouchable after the above store; because at that
130 * moment unlock can proceed and wipe the node element from stack.
131 *
132 * However, since our nodes are static per-cpu storage, we're
133 * guaranteed their existence -- this allows us to apply
134 * cmpxchg in an attempt to undo our queueing.
135 */
136
Waiman Longf5bfdc82020-01-13 10:07:35 -0500137 /*
Ingo Molnare2db7592021-03-22 02:35:05 +0100138 * Wait to acquire the lock or cancellation. Note that need_resched()
Waiman Longf5bfdc82020-01-13 10:07:35 -0500139 * will come with an IPI, which will wake smp_cond_load_relaxed() if it
140 * is implemented with a monitor-wait. vcpu_is_preempted() relies on
141 * polling, be careful.
142 */
143 if (smp_cond_load_relaxed(&node->locked, VAL || need_resched() ||
144 vcpu_is_preempted(node_cpu(node->prev))))
145 return true;
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100146
Waiman Longf5bfdc82020-01-13 10:07:35 -0500147 /* unqueue */
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100148 /*
149 * Step - A -- stabilize @prev
150 *
151 * Undo our @prev->next assignment; this will make @prev's
152 * unlock()/unqueue() wait for a next pointer since @lock points to us
153 * (or later).
154 */
155
156 for (;;) {
Qian Cai33190b62020-02-11 08:54:15 -0500157 /*
158 * cpu_relax() below implies a compiler barrier which would
159 * prevent this comparison being optimized away.
160 */
161 if (data_race(prev->next) == node &&
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100162 cmpxchg(&prev->next, node, NULL) == node)
163 break;
164
165 /*
166 * We can only fail the cmpxchg() racing against an unlock(),
Ingo Molnare2db7592021-03-22 02:35:05 +0100167 * in which case we should observe @node->locked becoming
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100168 * true.
169 */
170 if (smp_load_acquire(&node->locked))
171 return true;
172
Christian Borntraegerf2f09a42016-10-25 11:03:14 +0200173 cpu_relax();
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100174
175 /*
176 * Or we race against a concurrent unqueue()'s step-B, in which
177 * case its step-C will write us a new @node->prev pointer.
178 */
Davidlohr Bueso4d3199e2015-02-22 19:31:41 -0800179 prev = READ_ONCE(node->prev);
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100180 }
181
182 /*
183 * Step - B -- stabilize @next
184 *
185 * Similar to unlock(), wait for @node->next or move @lock from @node
186 * back to @prev.
187 */
188
189 next = osq_wait_next(lock, node, prev);
190 if (!next)
191 return false;
192
193 /*
194 * Step - C -- unlink
195 *
196 * @prev is stable because its still waiting for a new @prev->next
197 * pointer, @next is stable because our @node->next pointer is NULL and
198 * it will wait in Step-A.
199 */
200
Davidlohr Bueso4d3199e2015-02-22 19:31:41 -0800201 WRITE_ONCE(next->prev, prev);
202 WRITE_ONCE(prev->next, next);
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100203
204 return false;
205}
206
Jason Low90631822014-07-14 10:27:49 -0700207void osq_unlock(struct optimistic_spin_queue *lock)
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100208{
Jason Low33ecd202014-07-14 10:27:51 -0700209 struct optimistic_spin_node *node, *next;
Jason Low90631822014-07-14 10:27:49 -0700210 int curr = encode_cpu(smp_processor_id());
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100211
212 /*
213 * Fast path for the uncontended case.
214 */
Davidlohr Buesoc55a6ff2015-09-14 00:37:24 -0700215 if (likely(atomic_cmpxchg_release(&lock->tail, curr,
216 OSQ_UNLOCKED_VAL) == curr))
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100217 return;
218
219 /*
220 * Second most likely case.
221 */
Jason Low33ecd202014-07-14 10:27:51 -0700222 node = this_cpu_ptr(&osq_node);
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100223 next = xchg(&node->next, NULL);
224 if (next) {
Davidlohr Bueso4d3199e2015-02-22 19:31:41 -0800225 WRITE_ONCE(next->locked, 1);
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100226 return;
227 }
228
229 next = osq_wait_next(lock, node, NULL);
230 if (next)
Davidlohr Bueso4d3199e2015-02-22 19:31:41 -0800231 WRITE_ONCE(next->locked, 1);
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100232}