x86/ticketlock: Fix spin_unlock_wait() livelock
[firefly-linux-kernel-4.4.55.git] / arch / x86 / include / asm / spinlock.h
1 #ifndef _ASM_X86_SPINLOCK_H
2 #define _ASM_X86_SPINLOCK_H
3
4 #include <linux/jump_label.h>
5 #include <linux/atomic.h>
6 #include <asm/page.h>
7 #include <asm/processor.h>
8 #include <linux/compiler.h>
9 #include <asm/paravirt.h>
10 #include <asm/bitops.h>
11
12 /*
13  * Your basic SMP spinlocks, allowing only a single CPU anywhere
14  *
15  * Simple spin lock operations.  There are two variants, one clears IRQ's
16  * on the local processor, one does not.
17  *
18  * These are fair FIFO ticket locks, which support up to 2^16 CPUs.
19  *
20  * (the type definitions are in asm/spinlock_types.h)
21  */
22
23 #ifdef CONFIG_X86_32
24 # define LOCK_PTR_REG "a"
25 #else
26 # define LOCK_PTR_REG "D"
27 #endif
28
29 #if defined(CONFIG_X86_32) && \
30         (defined(CONFIG_X86_OOSTORE) || defined(CONFIG_X86_PPRO_FENCE))
31 /*
32  * On PPro SMP or if we are using OOSTORE, we use a locked operation to unlock
33  * (PPro errata 66, 92)
34  */
35 # define UNLOCK_LOCK_PREFIX LOCK_PREFIX
36 #else
37 # define UNLOCK_LOCK_PREFIX
38 #endif
39
40 /* How long a lock should spin before we consider blocking */
41 #define SPIN_THRESHOLD  (1 << 15)
42
43 extern struct static_key paravirt_ticketlocks_enabled;
44 static __always_inline bool static_key_false(struct static_key *key);
45
46 #ifdef CONFIG_PARAVIRT_SPINLOCKS
47
48 static inline void __ticket_enter_slowpath(arch_spinlock_t *lock)
49 {
50         set_bit(0, (volatile unsigned long *)&lock->tickets.tail);
51 }
52
53 #else  /* !CONFIG_PARAVIRT_SPINLOCKS */
54 static __always_inline void __ticket_lock_spinning(arch_spinlock_t *lock,
55                                                         __ticket_t ticket)
56 {
57 }
58 static inline void __ticket_unlock_kick(arch_spinlock_t *lock,
59                                                         __ticket_t ticket)
60 {
61 }
62
63 #endif /* CONFIG_PARAVIRT_SPINLOCKS */
64
65 static __always_inline int arch_spin_value_unlocked(arch_spinlock_t lock)
66 {
67         return lock.tickets.head == lock.tickets.tail;
68 }
69
70 /*
71  * Ticket locks are conceptually two parts, one indicating the current head of
72  * the queue, and the other indicating the current tail. The lock is acquired
73  * by atomically noting the tail and incrementing it by one (thus adding
74  * ourself to the queue and noting our position), then waiting until the head
75  * becomes equal to the the initial value of the tail.
76  *
77  * We use an xadd covering *both* parts of the lock, to increment the tail and
78  * also load the position of the head, which takes care of memory ordering
79  * issues and should be optimal for the uncontended case. Note the tail must be
80  * in the high part, because a wide xadd increment of the low part would carry
81  * up and contaminate the high part.
82  */
83 static __always_inline void arch_spin_lock(arch_spinlock_t *lock)
84 {
85         register struct __raw_tickets inc = { .tail = TICKET_LOCK_INC };
86
87         inc = xadd(&lock->tickets, inc);
88         if (likely(inc.head == inc.tail))
89                 goto out;
90
91         inc.tail &= ~TICKET_SLOWPATH_FLAG;
92         for (;;) {
93                 unsigned count = SPIN_THRESHOLD;
94
95                 do {
96                         if (ACCESS_ONCE(lock->tickets.head) == inc.tail)
97                                 goto out;
98                         cpu_relax();
99                 } while (--count);
100                 __ticket_lock_spinning(lock, inc.tail);
101         }
102 out:    barrier();      /* make sure nothing creeps before the lock is taken */
103 }
104
105 static __always_inline int arch_spin_trylock(arch_spinlock_t *lock)
106 {
107         arch_spinlock_t old, new;
108
109         old.tickets = ACCESS_ONCE(lock->tickets);
110         if (old.tickets.head != (old.tickets.tail & ~TICKET_SLOWPATH_FLAG))
111                 return 0;
112
113         new.head_tail = old.head_tail + (TICKET_LOCK_INC << TICKET_SHIFT);
114
115         /* cmpxchg is a full barrier, so nothing can move before it */
116         return cmpxchg(&lock->head_tail, old.head_tail, new.head_tail) == old.head_tail;
117 }
118
119 static inline void __ticket_unlock_slowpath(arch_spinlock_t *lock,
120                                             arch_spinlock_t old)
121 {
122         arch_spinlock_t new;
123
124         BUILD_BUG_ON(((__ticket_t)NR_CPUS) != NR_CPUS);
125
126         /* Perform the unlock on the "before" copy */
127         old.tickets.head += TICKET_LOCK_INC;
128
129         /* Clear the slowpath flag */
130         new.head_tail = old.head_tail & ~(TICKET_SLOWPATH_FLAG << TICKET_SHIFT);
131
132         /*
133          * If the lock is uncontended, clear the flag - use cmpxchg in
134          * case it changes behind our back though.
135          */
136         if (new.tickets.head != new.tickets.tail ||
137             cmpxchg(&lock->head_tail, old.head_tail,
138                                         new.head_tail) != old.head_tail) {
139                 /*
140                  * Lock still has someone queued for it, so wake up an
141                  * appropriate waiter.
142                  */
143                 __ticket_unlock_kick(lock, old.tickets.head);
144         }
145 }
146
147 static __always_inline void arch_spin_unlock(arch_spinlock_t *lock)
148 {
149         if (TICKET_SLOWPATH_FLAG &&
150             static_key_false(&paravirt_ticketlocks_enabled)) {
151                 arch_spinlock_t prev;
152
153                 prev = *lock;
154                 add_smp(&lock->tickets.head, TICKET_LOCK_INC);
155
156                 /* add_smp() is a full mb() */
157
158                 if (unlikely(lock->tickets.tail & TICKET_SLOWPATH_FLAG))
159                         __ticket_unlock_slowpath(lock, prev);
160         } else
161                 __add(&lock->tickets.head, TICKET_LOCK_INC, UNLOCK_LOCK_PREFIX);
162 }
163
164 static inline int arch_spin_is_locked(arch_spinlock_t *lock)
165 {
166         struct __raw_tickets tmp = ACCESS_ONCE(lock->tickets);
167
168         return tmp.tail != tmp.head;
169 }
170
171 static inline int arch_spin_is_contended(arch_spinlock_t *lock)
172 {
173         struct __raw_tickets tmp = ACCESS_ONCE(lock->tickets);
174
175         return (__ticket_t)(tmp.tail - tmp.head) > TICKET_LOCK_INC;
176 }
177 #define arch_spin_is_contended  arch_spin_is_contended
178
179 static __always_inline void arch_spin_lock_flags(arch_spinlock_t *lock,
180                                                   unsigned long flags)
181 {
182         arch_spin_lock(lock);
183 }
184
185 static inline void arch_spin_unlock_wait(arch_spinlock_t *lock)
186 {
187         __ticket_t head = ACCESS_ONCE(lock->tickets.head);
188
189         for (;;) {
190                 struct __raw_tickets tmp = ACCESS_ONCE(lock->tickets);
191                 /*
192                  * We need to check "unlocked" in a loop, tmp.head == head
193                  * can be false positive because of overflow.
194                  */
195                 if (tmp.head == (tmp.tail & ~TICKET_SLOWPATH_FLAG) ||
196                     tmp.head != head)
197                         break;
198
199                 cpu_relax();
200         }
201 }
202
203 /*
204  * Read-write spinlocks, allowing multiple readers
205  * but only one writer.
206  *
207  * NOTE! it is quite common to have readers in interrupts
208  * but no interrupt writers. For those circumstances we
209  * can "mix" irq-safe locks - any writer needs to get a
210  * irq-safe write-lock, but readers can get non-irqsafe
211  * read-locks.
212  *
213  * On x86, we implement read-write locks as a 32-bit counter
214  * with the high bit (sign) being the "contended" bit.
215  */
216
217 /**
218  * read_can_lock - would read_trylock() succeed?
219  * @lock: the rwlock in question.
220  */
221 static inline int arch_read_can_lock(arch_rwlock_t *lock)
222 {
223         return lock->lock > 0;
224 }
225
226 /**
227  * write_can_lock - would write_trylock() succeed?
228  * @lock: the rwlock in question.
229  */
230 static inline int arch_write_can_lock(arch_rwlock_t *lock)
231 {
232         return lock->write == WRITE_LOCK_CMP;
233 }
234
235 static inline void arch_read_lock(arch_rwlock_t *rw)
236 {
237         asm volatile(LOCK_PREFIX READ_LOCK_SIZE(dec) " (%0)\n\t"
238                      "jns 1f\n"
239                      "call __read_lock_failed\n\t"
240                      "1:\n"
241                      ::LOCK_PTR_REG (rw) : "memory");
242 }
243
244 static inline void arch_write_lock(arch_rwlock_t *rw)
245 {
246         asm volatile(LOCK_PREFIX WRITE_LOCK_SUB(%1) "(%0)\n\t"
247                      "jz 1f\n"
248                      "call __write_lock_failed\n\t"
249                      "1:\n"
250                      ::LOCK_PTR_REG (&rw->write), "i" (RW_LOCK_BIAS)
251                      : "memory");
252 }
253
254 static inline int arch_read_trylock(arch_rwlock_t *lock)
255 {
256         READ_LOCK_ATOMIC(t) *count = (READ_LOCK_ATOMIC(t) *)lock;
257
258         if (READ_LOCK_ATOMIC(dec_return)(count) >= 0)
259                 return 1;
260         READ_LOCK_ATOMIC(inc)(count);
261         return 0;
262 }
263
264 static inline int arch_write_trylock(arch_rwlock_t *lock)
265 {
266         atomic_t *count = (atomic_t *)&lock->write;
267
268         if (atomic_sub_and_test(WRITE_LOCK_CMP, count))
269                 return 1;
270         atomic_add(WRITE_LOCK_CMP, count);
271         return 0;
272 }
273
274 static inline void arch_read_unlock(arch_rwlock_t *rw)
275 {
276         asm volatile(LOCK_PREFIX READ_LOCK_SIZE(inc) " %0"
277                      :"+m" (rw->lock) : : "memory");
278 }
279
280 static inline void arch_write_unlock(arch_rwlock_t *rw)
281 {
282         asm volatile(LOCK_PREFIX WRITE_LOCK_ADD(%1) "%0"
283                      : "+m" (rw->write) : "i" (RW_LOCK_BIAS) : "memory");
284 }
285
286 #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
287 #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
288
289 #undef READ_LOCK_SIZE
290 #undef READ_LOCK_ATOMIC
291 #undef WRITE_LOCK_ADD
292 #undef WRITE_LOCK_SUB
293 #undef WRITE_LOCK_CMP
294
295 #define arch_spin_relax(lock)   cpu_relax()
296 #define arch_read_relax(lock)   cpu_relax()
297 #define arch_write_relax(lock)  cpu_relax()
298
299 #endif /* _ASM_X86_SPINLOCK_H */