dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / kernel / locking / osq_lock.c
blob8d7047ecef4e16ed561ef48dd83b788ce29d981d
1 #include <linux/percpu.h>
2 #include <linux/sched.h>
3 #include <linux/osq_lock.h>
5 /*
6 * An MCS like lock especially tailored for optimistic spinning for sleeping
7 * lock implementations (mutex, rwsem, etc).
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.
13 static DEFINE_PER_CPU_SHARED_ALIGNED(struct optimistic_spin_node, osq_node);
16 * We use the value 0 to represent "no CPU", thus the encoded value
17 * will be the CPU number incremented by 1.
19 static inline int encode_cpu(int cpu_nr)
21 return cpu_nr + 1;
24 static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val)
26 int cpu_nr = encoded_cpu_val - 1;
28 return per_cpu_ptr(&osq_node, cpu_nr);
32 * 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.
35 static inline struct optimistic_spin_node *
36 osq_wait_next(struct optimistic_spin_queue *lock,
37 struct optimistic_spin_node *node,
38 struct optimistic_spin_node *prev)
40 struct optimistic_spin_node *next = NULL;
41 int curr = encode_cpu(smp_processor_id());
42 int old;
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.
49 old = prev ? prev->cpu : OSQ_UNLOCKED_VAL;
51 for (;;) {
52 if (atomic_read(&lock->tail) == curr &&
53 atomic_cmpxchg_acquire(&lock->tail, curr, old) == curr) {
55 * We were the last queued, we moved @lock back. @prev
56 * will now observe @lock and will complete its
57 * unlock()/unqueue().
59 break;
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.
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.
72 if (node->next) {
73 next = xchg(&node->next, NULL);
74 if (next)
75 break;
78 cpu_relax_lowlatency();
81 return next;
84 bool osq_lock(struct optimistic_spin_queue *lock)
86 struct optimistic_spin_node *node = this_cpu_ptr(&osq_node);
87 struct optimistic_spin_node *prev, *next;
88 int curr = encode_cpu(smp_processor_id());
89 int old;
91 node->locked = 0;
92 node->next = NULL;
93 node->cpu = curr;
96 * 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.
101 old = atomic_xchg(&lock->tail, curr);
102 if (old == OSQ_UNLOCKED_VAL)
103 return true;
105 prev = decode_cpu(old);
106 node->prev = prev;
109 * osq_lock() unqueue
111 * node->prev = prev osq_wait_next()
112 * WMB MB
113 * prev->next = node next->prev = prev // unqueue-C
115 * Here 'node->prev' and 'next->prev' are the same variable and we need
116 * to ensure these stores happen in-order to avoid corrupting the list.
118 smp_wmb();
120 WRITE_ONCE(prev->next, node);
123 * Normally @prev is untouchable after the above store; because at that
124 * moment unlock can proceed and wipe the node element from stack.
126 * However, since our nodes are static per-cpu storage, we're
127 * guaranteed their existence -- this allows us to apply
128 * cmpxchg in an attempt to undo our queueing.
131 while (!READ_ONCE(node->locked)) {
133 * If we need to reschedule bail... so we can block.
135 if (need_resched())
136 goto unqueue;
138 cpu_relax_lowlatency();
140 return true;
142 unqueue:
144 * Step - A -- stabilize @prev
146 * Undo our @prev->next assignment; this will make @prev's
147 * unlock()/unqueue() wait for a next pointer since @lock points to us
148 * (or later).
151 for (;;) {
152 if (prev->next == node &&
153 cmpxchg(&prev->next, node, NULL) == node)
154 break;
157 * We can only fail the cmpxchg() racing against an unlock(),
158 * in which case we should observe @node->locked becomming
159 * true.
161 if (smp_load_acquire(&node->locked))
162 return true;
164 cpu_relax_lowlatency();
167 * Or we race against a concurrent unqueue()'s step-B, in which
168 * case its step-C will write us a new @node->prev pointer.
170 prev = READ_ONCE(node->prev);
174 * Step - B -- stabilize @next
176 * Similar to unlock(), wait for @node->next or move @lock from @node
177 * back to @prev.
180 next = osq_wait_next(lock, node, prev);
181 if (!next)
182 return false;
185 * Step - C -- unlink
187 * @prev is stable because its still waiting for a new @prev->next
188 * pointer, @next is stable because our @node->next pointer is NULL and
189 * it will wait in Step-A.
192 WRITE_ONCE(next->prev, prev);
193 WRITE_ONCE(prev->next, next);
195 return false;
198 void osq_unlock(struct optimistic_spin_queue *lock)
200 struct optimistic_spin_node *node, *next;
201 int curr = encode_cpu(smp_processor_id());
204 * Fast path for the uncontended case.
206 if (likely(atomic_cmpxchg_release(&lock->tail, curr,
207 OSQ_UNLOCKED_VAL) == curr))
208 return;
211 * Second most likely case.
213 node = this_cpu_ptr(&osq_node);
214 next = xchg(&node->next, NULL);
215 if (next) {
216 WRITE_ONCE(next->locked, 1);
217 return;
220 next = osq_wait_next(lock, node, NULL);
221 if (next)
222 WRITE_ONCE(next->locked, 1);