blob: 0befa20ce96e21d3d50b635965d70fcfb75cfe1d [file] [log] [blame]
Peter Zijlstrafb0527b2014-01-29 12:51:42 +01001#include <linux/percpu.h>
Peter Zijlstrafb0527b2014-01-29 12:51:42 +01002#include <linux/sched.h>
Davidlohr Buesod84b6722015-01-06 11:45:07 -08003#include <linux/osq_lock.h>
Prateek Sood1d0f4402017-06-16 10:32:47 +05304#include <linux/sched/rt.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
25static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val)
26{
27 int cpu_nr = encoded_cpu_val - 1;
28
29 return per_cpu_ptr(&osq_node, cpu_nr);
30}
31
32/*
Peter Zijlstrafb0527b2014-01-29 12:51:42 +010033 * Get a stable @node->next pointer, either for unlock() or unqueue() purposes.
34 * Can return NULL in case we were the last queued and we updated @lock instead.
35 */
Jason Low046a6192014-07-14 10:27:48 -070036static inline struct optimistic_spin_node *
Jason Low90631822014-07-14 10:27:49 -070037osq_wait_next(struct optimistic_spin_queue *lock,
Jason Low046a6192014-07-14 10:27:48 -070038 struct optimistic_spin_node *node,
39 struct optimistic_spin_node *prev)
Peter Zijlstrafb0527b2014-01-29 12:51:42 +010040{
Jason Low046a6192014-07-14 10:27:48 -070041 struct optimistic_spin_node *next = NULL;
Jason Low90631822014-07-14 10:27:49 -070042 int curr = encode_cpu(smp_processor_id());
43 int old;
44
45 /*
46 * If there is a prev node in queue, then the 'old' value will be
47 * the prev node's CPU #, else it's set to OSQ_UNLOCKED_VAL since if
48 * we're currently last in queue, then the queue will then become empty.
49 */
50 old = prev ? prev->cpu : OSQ_UNLOCKED_VAL;
Peter Zijlstrafb0527b2014-01-29 12:51:42 +010051
52 for (;;) {
Jason Low90631822014-07-14 10:27:49 -070053 if (atomic_read(&lock->tail) == curr &&
Davidlohr Buesoc55a6ff2015-09-14 00:37:24 -070054 atomic_cmpxchg_acquire(&lock->tail, curr, old) == curr) {
Peter Zijlstrafb0527b2014-01-29 12:51:42 +010055 /*
56 * We were the last queued, we moved @lock back. @prev
57 * will now observe @lock and will complete its
58 * unlock()/unqueue().
59 */
60 break;
61 }
62
63 /*
64 * We must xchg() the @node->next value, because if we were to
65 * leave it in, a concurrent unlock()/unqueue() from
66 * @node->next might complete Step-A and think its @prev is
67 * still valid.
68 *
69 * If the concurrent unlock()/unqueue() wins the race, we'll
70 * wait for either @lock to point to us, through its Step-B, or
71 * wait for a new @node->next from its Step-C.
72 */
73 if (node->next) {
74 next = xchg(&node->next, NULL);
75 if (next)
76 break;
77 }
78
Davidlohr Bueso3a6bfbc2014-06-29 15:09:33 -070079 cpu_relax_lowlatency();
Peter Zijlstrafb0527b2014-01-29 12:51:42 +010080 }
81
82 return next;
83}
84
Jason Low90631822014-07-14 10:27:49 -070085bool osq_lock(struct optimistic_spin_queue *lock)
Peter Zijlstrafb0527b2014-01-29 12:51:42 +010086{
Jason Low046a6192014-07-14 10:27:48 -070087 struct optimistic_spin_node *node = this_cpu_ptr(&osq_node);
88 struct optimistic_spin_node *prev, *next;
Prateek Sood1d0f4402017-06-16 10:32:47 +053089 struct task_struct *task = current;
Jason Low90631822014-07-14 10:27:49 -070090 int curr = encode_cpu(smp_processor_id());
91 int old;
Peter Zijlstrafb0527b2014-01-29 12:51:42 +010092
93 node->locked = 0;
94 node->next = NULL;
Jason Low90631822014-07-14 10:27:49 -070095 node->cpu = curr;
Peter Zijlstrafb0527b2014-01-29 12:51:42 +010096
Davidlohr Buesoc55a6ff2015-09-14 00:37:24 -070097 /*
Will Deaconb4b29f92015-12-11 17:46:41 +000098 * We need both ACQUIRE (pairs with corresponding RELEASE in
99 * unlock() uncontended, or fastpath) and RELEASE (to publish
100 * the node fields we just initialised) semantics when updating
101 * the lock tail.
Davidlohr Buesoc55a6ff2015-09-14 00:37:24 -0700102 */
Will Deaconb4b29f92015-12-11 17:46:41 +0000103 old = atomic_xchg(&lock->tail, curr);
Jason Low90631822014-07-14 10:27:49 -0700104 if (old == OSQ_UNLOCKED_VAL)
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100105 return true;
106
Jason Low90631822014-07-14 10:27:49 -0700107 prev = decode_cpu(old);
108 node->prev = prev;
Prateek Sood3c25a9d2017-07-14 19:17:56 +0530109
110 /*
111 * osq_lock() unqueue
112 *
113 * node->prev = prev osq_wait_next()
114 * WMB MB
115 * prev->next = node next->prev = prev // unqueue-C
116 *
117 * Here 'node->prev' and 'next->prev' are the same variable and we need
118 * to ensure these stores happen in-order to avoid corrupting the list.
119 */
120 smp_wmb();
121
Davidlohr Bueso4d3199e2015-02-22 19:31:41 -0800122 WRITE_ONCE(prev->next, node);
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100123
124 /*
125 * Normally @prev is untouchable after the above store; because at that
126 * moment unlock can proceed and wipe the node element from stack.
127 *
128 * However, since our nodes are static per-cpu storage, we're
129 * guaranteed their existence -- this allows us to apply
130 * cmpxchg in an attempt to undo our queueing.
131 */
132
Davidlohr Bueso4d3199e2015-02-22 19:31:41 -0800133 while (!READ_ONCE(node->locked)) {
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100134 /*
135 * If we need to reschedule bail... so we can block.
Prateek Sood1d0f4402017-06-16 10:32:47 +0530136 * If a task spins on owner on a CPU after acquiring
137 * osq_lock while a RT task spins on another CPU to
138 * acquire osq_lock, it will starve the owner from
139 * completing if owner is to be scheduled on the same CPU.
140 * It will be a live lock.
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100141 */
Prateek Sood1d0f4402017-06-16 10:32:47 +0530142 if (need_resched() || rt_task(task))
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100143 goto unqueue;
144
Davidlohr Bueso3a6bfbc2014-06-29 15:09:33 -0700145 cpu_relax_lowlatency();
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100146 }
147 return true;
148
149unqueue:
150 /*
151 * Step - A -- stabilize @prev
152 *
153 * Undo our @prev->next assignment; this will make @prev's
154 * unlock()/unqueue() wait for a next pointer since @lock points to us
155 * (or later).
156 */
157
158 for (;;) {
159 if (prev->next == node &&
160 cmpxchg(&prev->next, node, NULL) == node)
161 break;
162
163 /*
164 * We can only fail the cmpxchg() racing against an unlock(),
165 * in which case we should observe @node->locked becomming
166 * true.
167 */
168 if (smp_load_acquire(&node->locked))
169 return true;
170
Davidlohr Bueso3a6bfbc2014-06-29 15:09:33 -0700171 cpu_relax_lowlatency();
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100172
173 /*
174 * Or we race against a concurrent unqueue()'s step-B, in which
175 * case its step-C will write us a new @node->prev pointer.
176 */
Davidlohr Bueso4d3199e2015-02-22 19:31:41 -0800177 prev = READ_ONCE(node->prev);
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100178 }
179
180 /*
181 * Step - B -- stabilize @next
182 *
183 * Similar to unlock(), wait for @node->next or move @lock from @node
184 * back to @prev.
185 */
186
187 next = osq_wait_next(lock, node, prev);
188 if (!next)
189 return false;
190
191 /*
192 * Step - C -- unlink
193 *
194 * @prev is stable because its still waiting for a new @prev->next
195 * pointer, @next is stable because our @node->next pointer is NULL and
196 * it will wait in Step-A.
197 */
198
Davidlohr Bueso4d3199e2015-02-22 19:31:41 -0800199 WRITE_ONCE(next->prev, prev);
200 WRITE_ONCE(prev->next, next);
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100201
202 return false;
203}
204
Jason Low90631822014-07-14 10:27:49 -0700205void osq_unlock(struct optimistic_spin_queue *lock)
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100206{
Jason Low33ecd202014-07-14 10:27:51 -0700207 struct optimistic_spin_node *node, *next;
Jason Low90631822014-07-14 10:27:49 -0700208 int curr = encode_cpu(smp_processor_id());
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100209
210 /*
211 * Fast path for the uncontended case.
212 */
Davidlohr Buesoc55a6ff2015-09-14 00:37:24 -0700213 if (likely(atomic_cmpxchg_release(&lock->tail, curr,
214 OSQ_UNLOCKED_VAL) == curr))
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100215 return;
216
217 /*
218 * Second most likely case.
219 */
Jason Low33ecd202014-07-14 10:27:51 -0700220 node = this_cpu_ptr(&osq_node);
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100221 next = xchg(&node->next, NULL);
222 if (next) {
Davidlohr Bueso4d3199e2015-02-22 19:31:41 -0800223 WRITE_ONCE(next->locked, 1);
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100224 return;
225 }
226
227 next = osq_wait_next(lock, node, NULL);
228 if (next)
Davidlohr Bueso4d3199e2015-02-22 19:31:41 -0800229 WRITE_ONCE(next->locked, 1);
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100230}