blob: d68883dd133c28f8ab6b25ebe3b02ae647940726 [file] [log] [blame]
H. Peter Anvin1965aae2008-10-22 22:26:29 -07001#ifndef _ASM_X86_SPINLOCK_H
2#define _ASM_X86_SPINLOCK_H
Glauber de Oliveira Costa2fed0c52008-01-30 13:30:33 +01003
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +05304#include <linux/jump_label.h>
Arun Sharma600634972011-07-26 16:09:06 -07005#include <linux/atomic.h>
Thomas Gleixner1075cf72008-01-30 13:30:34 +01006#include <asm/page.h>
7#include <asm/processor.h>
Nick Piggin314cdbe2008-01-30 13:31:21 +01008#include <linux/compiler.h>
Jeremy Fitzhardinge74d4aff2008-07-07 12:07:50 -07009#include <asm/paravirt.h>
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +053010#include <asm/bitops.h>
11
Thomas Gleixner1075cf72008-01-30 13:30:34 +010012/*
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 *
Richard Weinberger83be4ff2012-08-14 14:47:37 -070018 * These are fair FIFO ticket locks, which support up to 2^16 CPUs.
Thomas Gleixner1075cf72008-01-30 13:30:34 +010019 *
20 * (the type definitions are in asm/spinlock_types.h)
21 */
22
Thomas Gleixner96a388d2007-10-11 11:20:03 +020023#ifdef CONFIG_X86_32
Thomas Gleixner1075cf72008-01-30 13:30:34 +010024# define LOCK_PTR_REG "a"
Thomas Gleixner96a388d2007-10-11 11:20:03 +020025#else
Thomas Gleixner1075cf72008-01-30 13:30:34 +010026# define LOCK_PTR_REG "D"
Thomas Gleixner96a388d2007-10-11 11:20:03 +020027#endif
Glauber de Oliveira Costa2fed0c52008-01-30 13:30:33 +010028
Nick Piggin3a556b22008-01-30 13:33:00 +010029#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
Nick Piggin314cdbe2008-01-30 13:31:21 +010038#endif
39
Jeremy Fitzhardinge545ac132013-08-09 19:51:49 +053040/* How long a lock should spin before we consider blocking */
41#define SPIN_THRESHOLD (1 << 15)
42
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +053043extern struct static_key paravirt_ticketlocks_enabled;
44static __always_inline bool static_key_false(struct static_key *key);
Jeremy Fitzhardinge545ac132013-08-09 19:51:49 +053045
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +053046#ifdef CONFIG_PARAVIRT_SPINLOCKS
47
48static 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 */
54static __always_inline void __ticket_lock_spinning(arch_spinlock_t *lock,
55 __ticket_t ticket)
56{
57}
58static inline void __ticket_unlock_kick(arch_spinlock_t *lock,
Jeremy Fitzhardinge545ac132013-08-09 19:51:49 +053059 __ticket_t ticket)
60{
61}
62
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +053063#endif /* CONFIG_PARAVIRT_SPINLOCKS */
Jeremy Fitzhardinge545ac132013-08-09 19:51:49 +053064
Nick Piggin3a556b22008-01-30 13:33:00 +010065/*
66 * Ticket locks are conceptually two parts, one indicating the current head of
67 * the queue, and the other indicating the current tail. The lock is acquired
68 * by atomically noting the tail and incrementing it by one (thus adding
69 * ourself to the queue and noting our position), then waiting until the head
70 * becomes equal to the the initial value of the tail.
71 *
72 * We use an xadd covering *both* parts of the lock, to increment the tail and
73 * also load the position of the head, which takes care of memory ordering
74 * issues and should be optimal for the uncontended case. Note the tail must be
75 * in the high part, because a wide xadd increment of the low part would carry
76 * up and contaminate the high part.
Nick Piggin3a556b22008-01-30 13:33:00 +010077 */
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +053078static __always_inline void arch_spin_lock(arch_spinlock_t *lock)
Thomas Gleixner1075cf72008-01-30 13:30:34 +010079{
Jeremy Fitzhardinge4a1ed4c2013-08-09 19:51:56 +053080 register struct __raw_tickets inc = { .tail = TICKET_LOCK_INC };
Nick Piggin314cdbe2008-01-30 13:31:21 +010081
Jeremy Fitzhardinge29944882010-07-13 14:07:45 -070082 inc = xadd(&lock->tickets, inc);
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +053083 if (likely(inc.head == inc.tail))
84 goto out;
Jeremy Fitzhardingec576a3e2010-07-03 01:06:04 +010085
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +053086 inc.tail &= ~TICKET_SLOWPATH_FLAG;
Jeremy Fitzhardingec576a3e2010-07-03 01:06:04 +010087 for (;;) {
Jeremy Fitzhardinge545ac132013-08-09 19:51:49 +053088 unsigned count = SPIN_THRESHOLD;
89
90 do {
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +053091 if (ACCESS_ONCE(lock->tickets.head) == inc.tail)
Jeremy Fitzhardinge545ac132013-08-09 19:51:49 +053092 goto out;
93 cpu_relax();
Jeremy Fitzhardinge545ac132013-08-09 19:51:49 +053094 } while (--count);
95 __ticket_lock_spinning(lock, inc.tail);
Jeremy Fitzhardingec576a3e2010-07-03 01:06:04 +010096 }
Jeremy Fitzhardinge545ac132013-08-09 19:51:49 +053097out: barrier(); /* make sure nothing creeps before the lock is taken */
Thomas Gleixner1075cf72008-01-30 13:30:34 +010098}
99
Jeremy Fitzhardingeb798df02013-08-09 19:51:51 +0530100static __always_inline int arch_spin_trylock(arch_spinlock_t *lock)
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100101{
Jeremy Fitzhardinge229855d2010-07-13 15:14:26 -0700102 arch_spinlock_t old, new;
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100103
Jeremy Fitzhardinge229855d2010-07-13 15:14:26 -0700104 old.tickets = ACCESS_ONCE(lock->tickets);
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +0530105 if (old.tickets.head != (old.tickets.tail & ~TICKET_SLOWPATH_FLAG))
Jeremy Fitzhardinge229855d2010-07-13 15:14:26 -0700106 return 0;
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100107
Jeremy Fitzhardinge4a1ed4c2013-08-09 19:51:56 +0530108 new.head_tail = old.head_tail + (TICKET_LOCK_INC << TICKET_SHIFT);
Jeremy Fitzhardinge229855d2010-07-13 15:14:26 -0700109
110 /* cmpxchg is a full barrier, so nothing can move before it */
111 return cmpxchg(&lock->head_tail, old.head_tail, new.head_tail) == old.head_tail;
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100112}
113
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +0530114static inline void __ticket_unlock_slowpath(arch_spinlock_t *lock,
115 arch_spinlock_t old)
116{
117 arch_spinlock_t new;
118
119 BUILD_BUG_ON(((__ticket_t)NR_CPUS) != NR_CPUS);
120
121 /* Perform the unlock on the "before" copy */
122 old.tickets.head += TICKET_LOCK_INC;
123
124 /* Clear the slowpath flag */
125 new.head_tail = old.head_tail & ~(TICKET_SLOWPATH_FLAG << TICKET_SHIFT);
126
127 /*
128 * If the lock is uncontended, clear the flag - use cmpxchg in
129 * case it changes behind our back though.
130 */
131 if (new.tickets.head != new.tickets.tail ||
132 cmpxchg(&lock->head_tail, old.head_tail,
133 new.head_tail) != old.head_tail) {
134 /*
135 * Lock still has someone queued for it, so wake up an
136 * appropriate waiter.
137 */
138 __ticket_unlock_kick(lock, old.tickets.head);
139 }
140}
141
Jeremy Fitzhardingeb798df02013-08-09 19:51:51 +0530142static __always_inline void arch_spin_unlock(arch_spinlock_t *lock)
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100143{
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +0530144 if (TICKET_SLOWPATH_FLAG &&
145 static_key_false(&paravirt_ticketlocks_enabled)) {
146 arch_spinlock_t prev;
Jeremy Fitzhardinge545ac132013-08-09 19:51:49 +0530147
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +0530148 prev = *lock;
149 add_smp(&lock->tickets.head, TICKET_LOCK_INC);
150
151 /* add_smp() is a full mb() */
152
153 if (unlikely(lock->tickets.tail & TICKET_SLOWPATH_FLAG))
154 __ticket_unlock_slowpath(lock, prev);
155 } else
156 __add(&lock->tickets.head, TICKET_LOCK_INC, UNLOCK_LOCK_PREFIX);
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100157}
158
Jeremy Fitzhardingeb798df02013-08-09 19:51:51 +0530159static inline int arch_spin_is_locked(arch_spinlock_t *lock)
Jan Beulich08f5fcb2008-09-05 13:26:39 +0100160{
Jeremy Fitzhardinge84eb9502010-07-02 23:26:36 +0100161 struct __raw_tickets tmp = ACCESS_ONCE(lock->tickets);
Jan Beulich08f5fcb2008-09-05 13:26:39 +0100162
Jan Beulich7931d492012-02-03 15:06:26 +0000163 return tmp.tail != tmp.head;
Jan Beulich08f5fcb2008-09-05 13:26:39 +0100164}
165
Jeremy Fitzhardingeb798df02013-08-09 19:51:51 +0530166static inline int arch_spin_is_contended(arch_spinlock_t *lock)
Jan Beulich08f5fcb2008-09-05 13:26:39 +0100167{
Jeremy Fitzhardinge84eb9502010-07-02 23:26:36 +0100168 struct __raw_tickets tmp = ACCESS_ONCE(lock->tickets);
Jan Beulich08f5fcb2008-09-05 13:26:39 +0100169
Jeremy Fitzhardinge4a1ed4c2013-08-09 19:51:56 +0530170 return (__ticket_t)(tmp.tail - tmp.head) > TICKET_LOCK_INC;
Jan Beulich08f5fcb2008-09-05 13:26:39 +0100171}
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100172#define arch_spin_is_contended arch_spin_is_contended
Jeremy Fitzhardinge74d4aff2008-07-07 12:07:50 -0700173
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100174static __always_inline void arch_spin_lock_flags(arch_spinlock_t *lock,
Jeremy Fitzhardinge63d3a752008-08-19 13:19:36 -0700175 unsigned long flags)
176{
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100177 arch_spin_lock(lock);
Jeremy Fitzhardinge63d3a752008-08-19 13:19:36 -0700178}
179
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100180static inline void arch_spin_unlock_wait(arch_spinlock_t *lock)
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100181{
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100182 while (arch_spin_is_locked(lock))
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100183 cpu_relax();
184}
185
186/*
187 * Read-write spinlocks, allowing multiple readers
188 * but only one writer.
189 *
190 * NOTE! it is quite common to have readers in interrupts
191 * but no interrupt writers. For those circumstances we
192 * can "mix" irq-safe locks - any writer needs to get a
193 * irq-safe write-lock, but readers can get non-irqsafe
194 * read-locks.
195 *
196 * On x86, we implement read-write locks as a 32-bit counter
197 * with the high bit (sign) being the "contended" bit.
198 */
199
Nick Piggin314cdbe2008-01-30 13:31:21 +0100200/**
201 * read_can_lock - would read_trylock() succeed?
202 * @lock: the rwlock in question.
203 */
Thomas Gleixnere5931942009-12-03 20:08:46 +0100204static inline int arch_read_can_lock(arch_rwlock_t *lock)
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100205{
Jan Beulicha7500362011-07-19 13:00:45 +0100206 return lock->lock > 0;
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100207}
208
Nick Piggin314cdbe2008-01-30 13:31:21 +0100209/**
210 * write_can_lock - would write_trylock() succeed?
211 * @lock: the rwlock in question.
212 */
Thomas Gleixnere5931942009-12-03 20:08:46 +0100213static inline int arch_write_can_lock(arch_rwlock_t *lock)
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100214{
Jan Beulicha7500362011-07-19 13:00:45 +0100215 return lock->write == WRITE_LOCK_CMP;
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100216}
217
Thomas Gleixnere5931942009-12-03 20:08:46 +0100218static inline void arch_read_lock(arch_rwlock_t *rw)
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100219{
Jan Beulicha7500362011-07-19 13:00:45 +0100220 asm volatile(LOCK_PREFIX READ_LOCK_SIZE(dec) " (%0)\n\t"
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100221 "jns 1f\n"
222 "call __read_lock_failed\n\t"
223 "1:\n"
224 ::LOCK_PTR_REG (rw) : "memory");
225}
226
Thomas Gleixnere5931942009-12-03 20:08:46 +0100227static inline void arch_write_lock(arch_rwlock_t *rw)
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100228{
Jan Beulicha7500362011-07-19 13:00:45 +0100229 asm volatile(LOCK_PREFIX WRITE_LOCK_SUB(%1) "(%0)\n\t"
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100230 "jz 1f\n"
231 "call __write_lock_failed\n\t"
232 "1:\n"
Jan Beulicha7500362011-07-19 13:00:45 +0100233 ::LOCK_PTR_REG (&rw->write), "i" (RW_LOCK_BIAS)
234 : "memory");
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100235}
236
Thomas Gleixnere5931942009-12-03 20:08:46 +0100237static inline int arch_read_trylock(arch_rwlock_t *lock)
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100238{
Jan Beulicha7500362011-07-19 13:00:45 +0100239 READ_LOCK_ATOMIC(t) *count = (READ_LOCK_ATOMIC(t) *)lock;
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100240
Jan Beulicha7500362011-07-19 13:00:45 +0100241 if (READ_LOCK_ATOMIC(dec_return)(count) >= 0)
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100242 return 1;
Jan Beulicha7500362011-07-19 13:00:45 +0100243 READ_LOCK_ATOMIC(inc)(count);
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100244 return 0;
245}
246
Thomas Gleixnere5931942009-12-03 20:08:46 +0100247static inline int arch_write_trylock(arch_rwlock_t *lock)
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100248{
Jan Beulicha7500362011-07-19 13:00:45 +0100249 atomic_t *count = (atomic_t *)&lock->write;
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100250
Jan Beulicha7500362011-07-19 13:00:45 +0100251 if (atomic_sub_and_test(WRITE_LOCK_CMP, count))
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100252 return 1;
Jan Beulicha7500362011-07-19 13:00:45 +0100253 atomic_add(WRITE_LOCK_CMP, count);
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100254 return 0;
255}
256
Thomas Gleixnere5931942009-12-03 20:08:46 +0100257static inline void arch_read_unlock(arch_rwlock_t *rw)
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100258{
Jan Beulicha7500362011-07-19 13:00:45 +0100259 asm volatile(LOCK_PREFIX READ_LOCK_SIZE(inc) " %0"
260 :"+m" (rw->lock) : : "memory");
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100261}
262
Thomas Gleixnere5931942009-12-03 20:08:46 +0100263static inline void arch_write_unlock(arch_rwlock_t *rw)
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100264{
Jan Beulicha7500362011-07-19 13:00:45 +0100265 asm volatile(LOCK_PREFIX WRITE_LOCK_ADD(%1) "%0"
266 : "+m" (rw->write) : "i" (RW_LOCK_BIAS) : "memory");
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100267}
268
Thomas Gleixnere5931942009-12-03 20:08:46 +0100269#define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
270#define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
Robin Holtf5f7eac2009-04-02 16:59:46 -0700271
Jan Beulicha7500362011-07-19 13:00:45 +0100272#undef READ_LOCK_SIZE
273#undef READ_LOCK_ATOMIC
274#undef WRITE_LOCK_ADD
275#undef WRITE_LOCK_SUB
276#undef WRITE_LOCK_CMP
277
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100278#define arch_spin_relax(lock) cpu_relax()
279#define arch_read_relax(lock) cpu_relax()
280#define arch_write_relax(lock) cpu_relax()
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100281
Jiri Olsaad462762009-07-08 12:10:31 +0000282/* The {read|write|spin}_lock() on x86 are full memory barriers. */
283static inline void smp_mb__after_lock(void) { }
284#define ARCH_HAS_SMP_MB_AFTER_LOCK
285
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700286#endif /* _ASM_X86_SPINLOCK_H */