blob: 4ea2710b9d6c17952602c5a25f8d33607f7ce909 [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>
Peter Zijlstrafb0527b2014-01-29 12:51:42 +01004
5/*
6 * An MCS like lock especially tailored for optimistic spinning for sleeping
7 * lock implementations (mutex, rwsem, etc).
8 *
9 * Using a single mcs node per CPU is safe because sleeping locks should not be
10 * called from interrupt context and we have preemption disabled while
11 * spinning.
12 */
Jason Low046a6192014-07-14 10:27:48 -070013static DEFINE_PER_CPU_SHARED_ALIGNED(struct optimistic_spin_node, osq_node);
Peter Zijlstrafb0527b2014-01-29 12:51:42 +010014
15/*
Jason Low90631822014-07-14 10:27:49 -070016 * We use the value 0 to represent "no CPU", thus the encoded value
17 * will be the CPU number incremented by 1.
18 */
19static inline int encode_cpu(int cpu_nr)
20{
21 return cpu_nr + 1;
22}
23
24static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val)
25{
26 int cpu_nr = encoded_cpu_val - 1;
27
28 return per_cpu_ptr(&osq_node, cpu_nr);
29}
30
31/*
Peter Zijlstrafb0527b2014-01-29 12:51:42 +010032 * Get a stable @node->next pointer, either for unlock() or unqueue() purposes.
33 * Can return NULL in case we were the last queued and we updated @lock instead.
34 */
Jason Low046a6192014-07-14 10:27:48 -070035static inline struct optimistic_spin_node *
Jason Low90631822014-07-14 10:27:49 -070036osq_wait_next(struct optimistic_spin_queue *lock,
Jason Low046a6192014-07-14 10:27:48 -070037 struct optimistic_spin_node *node,
38 struct optimistic_spin_node *prev)
Peter Zijlstrafb0527b2014-01-29 12:51:42 +010039{
Jason Low046a6192014-07-14 10:27:48 -070040 struct optimistic_spin_node *next = NULL;
Jason Low90631822014-07-14 10:27:49 -070041 int curr = encode_cpu(smp_processor_id());
42 int old;
43
44 /*
45 * If there is a prev node in queue, then the 'old' value will be
46 * the prev node's CPU #, else it's set to OSQ_UNLOCKED_VAL since if
47 * we're currently last in queue, then the queue will then become empty.
48 */
49 old = prev ? prev->cpu : OSQ_UNLOCKED_VAL;
Peter Zijlstrafb0527b2014-01-29 12:51:42 +010050
51 for (;;) {
Jason Low90631822014-07-14 10:27:49 -070052 if (atomic_read(&lock->tail) == curr &&
Davidlohr Buesoc55a6ff2015-09-14 00:37:24 -070053 atomic_cmpxchg_acquire(&lock->tail, curr, old) == curr) {
Peter Zijlstrafb0527b2014-01-29 12:51:42 +010054 /*
55 * We were the last queued, we moved @lock back. @prev
56 * will now observe @lock and will complete its
57 * unlock()/unqueue().
58 */
59 break;
60 }
61
62 /*
63 * We must xchg() the @node->next value, because if we were to
64 * leave it in, a concurrent unlock()/unqueue() from
65 * @node->next might complete Step-A and think its @prev is
66 * still valid.
67 *
68 * If the concurrent unlock()/unqueue() wins the race, we'll
69 * wait for either @lock to point to us, through its Step-B, or
70 * wait for a new @node->next from its Step-C.
71 */
72 if (node->next) {
73 next = xchg(&node->next, NULL);
74 if (next)
75 break;
76 }
77
Christian Borntraegerf2f09a42016-10-25 11:03:14 +020078 cpu_relax();
Peter Zijlstrafb0527b2014-01-29 12:51:42 +010079 }
80
81 return next;
82}
83
Jason Low90631822014-07-14 10:27:49 -070084bool osq_lock(struct optimistic_spin_queue *lock)
Peter Zijlstrafb0527b2014-01-29 12:51:42 +010085{
Jason Low046a6192014-07-14 10:27:48 -070086 struct optimistic_spin_node *node = this_cpu_ptr(&osq_node);
87 struct optimistic_spin_node *prev, *next;
Jason Low90631822014-07-14 10:27:49 -070088 int curr = encode_cpu(smp_processor_id());
89 int old;
Peter Zijlstrafb0527b2014-01-29 12:51:42 +010090
91 node->locked = 0;
92 node->next = NULL;
Jason Low90631822014-07-14 10:27:49 -070093 node->cpu = curr;
Peter Zijlstrafb0527b2014-01-29 12:51:42 +010094
Davidlohr Buesoc55a6ff2015-09-14 00:37:24 -070095 /*
Will Deaconb4b29f92015-12-11 17:46:41 +000096 * We need both ACQUIRE (pairs with corresponding RELEASE in
97 * unlock() uncontended, or fastpath) and RELEASE (to publish
98 * the node fields we just initialised) semantics when updating
99 * the lock tail.
Davidlohr Buesoc55a6ff2015-09-14 00:37:24 -0700100 */
Will Deaconb4b29f92015-12-11 17:46:41 +0000101 old = atomic_xchg(&lock->tail, curr);
Jason Low90631822014-07-14 10:27:49 -0700102 if (old == OSQ_UNLOCKED_VAL)
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100103 return true;
104
Jason Low90631822014-07-14 10:27:49 -0700105 prev = decode_cpu(old);
106 node->prev = prev;
Davidlohr Bueso4d3199e2015-02-22 19:31:41 -0800107 WRITE_ONCE(prev->next, node);
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100108
109 /*
110 * Normally @prev is untouchable after the above store; because at that
111 * moment unlock can proceed and wipe the node element from stack.
112 *
113 * However, since our nodes are static per-cpu storage, we're
114 * guaranteed their existence -- this allows us to apply
115 * cmpxchg in an attempt to undo our queueing.
116 */
117
Davidlohr Bueso4d3199e2015-02-22 19:31:41 -0800118 while (!READ_ONCE(node->locked)) {
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100119 /*
120 * If we need to reschedule bail... so we can block.
121 */
122 if (need_resched())
123 goto unqueue;
124
Christian Borntraegerf2f09a42016-10-25 11:03:14 +0200125 cpu_relax();
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100126 }
127 return true;
128
129unqueue:
130 /*
131 * Step - A -- stabilize @prev
132 *
133 * Undo our @prev->next assignment; this will make @prev's
134 * unlock()/unqueue() wait for a next pointer since @lock points to us
135 * (or later).
136 */
137
138 for (;;) {
139 if (prev->next == node &&
140 cmpxchg(&prev->next, node, NULL) == node)
141 break;
142
143 /*
144 * We can only fail the cmpxchg() racing against an unlock(),
145 * in which case we should observe @node->locked becomming
146 * true.
147 */
148 if (smp_load_acquire(&node->locked))
149 return true;
150
Christian Borntraegerf2f09a42016-10-25 11:03:14 +0200151 cpu_relax();
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100152
153 /*
154 * Or we race against a concurrent unqueue()'s step-B, in which
155 * case its step-C will write us a new @node->prev pointer.
156 */
Davidlohr Bueso4d3199e2015-02-22 19:31:41 -0800157 prev = READ_ONCE(node->prev);
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100158 }
159
160 /*
161 * Step - B -- stabilize @next
162 *
163 * Similar to unlock(), wait for @node->next or move @lock from @node
164 * back to @prev.
165 */
166
167 next = osq_wait_next(lock, node, prev);
168 if (!next)
169 return false;
170
171 /*
172 * Step - C -- unlink
173 *
174 * @prev is stable because its still waiting for a new @prev->next
175 * pointer, @next is stable because our @node->next pointer is NULL and
176 * it will wait in Step-A.
177 */
178
Davidlohr Bueso4d3199e2015-02-22 19:31:41 -0800179 WRITE_ONCE(next->prev, prev);
180 WRITE_ONCE(prev->next, next);
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100181
182 return false;
183}
184
Jason Low90631822014-07-14 10:27:49 -0700185void osq_unlock(struct optimistic_spin_queue *lock)
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100186{
Jason Low33ecd202014-07-14 10:27:51 -0700187 struct optimistic_spin_node *node, *next;
Jason Low90631822014-07-14 10:27:49 -0700188 int curr = encode_cpu(smp_processor_id());
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100189
190 /*
191 * Fast path for the uncontended case.
192 */
Davidlohr Buesoc55a6ff2015-09-14 00:37:24 -0700193 if (likely(atomic_cmpxchg_release(&lock->tail, curr,
194 OSQ_UNLOCKED_VAL) == curr))
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100195 return;
196
197 /*
198 * Second most likely case.
199 */
Jason Low33ecd202014-07-14 10:27:51 -0700200 node = this_cpu_ptr(&osq_node);
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100201 next = xchg(&node->next, NULL);
202 if (next) {
Davidlohr Bueso4d3199e2015-02-22 19:31:41 -0800203 WRITE_ONCE(next->locked, 1);
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100204 return;
205 }
206
207 next = osq_wait_next(lock, node, NULL);
208 if (next)
Davidlohr Bueso4d3199e2015-02-22 19:31:41 -0800209 WRITE_ONCE(next->locked, 1);
Peter Zijlstrafb0527b2014-01-29 12:51:42 +0100210}