blob: 54f1c8068c02340a4600049f843fd9f3067b51c5 [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
Dave Jones09df7c42014-03-10 19:32:22 -040029#if defined(CONFIG_X86_32) && (defined(CONFIG_X86_PPRO_FENCE))
Nick Piggin3a556b22008-01-30 13:33:00 +010030/*
Dave Jones09df7c42014-03-10 19:32:22 -040031 * On PPro SMP, we use a locked operation to unlock
Nick Piggin3a556b22008-01-30 13:33:00 +010032 * (PPro errata 66, 92)
33 */
34# define UNLOCK_LOCK_PREFIX LOCK_PREFIX
35#else
36# define UNLOCK_LOCK_PREFIX
Nick Piggin314cdbe2008-01-30 13:31:21 +010037#endif
38
Jeremy Fitzhardinge545ac132013-08-09 19:51:49 +053039/* How long a lock should spin before we consider blocking */
40#define SPIN_THRESHOLD (1 << 15)
41
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +053042extern struct static_key paravirt_ticketlocks_enabled;
43static __always_inline bool static_key_false(struct static_key *key);
Jeremy Fitzhardinge545ac132013-08-09 19:51:49 +053044
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +053045#ifdef CONFIG_PARAVIRT_SPINLOCKS
46
47static inline void __ticket_enter_slowpath(arch_spinlock_t *lock)
48{
49 set_bit(0, (volatile unsigned long *)&lock->tickets.tail);
50}
51
52#else /* !CONFIG_PARAVIRT_SPINLOCKS */
53static __always_inline void __ticket_lock_spinning(arch_spinlock_t *lock,
54 __ticket_t ticket)
55{
56}
57static inline void __ticket_unlock_kick(arch_spinlock_t *lock,
Jeremy Fitzhardinge545ac132013-08-09 19:51:49 +053058 __ticket_t ticket)
59{
60}
61
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +053062#endif /* CONFIG_PARAVIRT_SPINLOCKS */
Jeremy Fitzhardinge545ac132013-08-09 19:51:49 +053063
Linus Torvaldsbc08b442013-09-02 12:12:15 -070064static __always_inline int arch_spin_value_unlocked(arch_spinlock_t lock)
65{
66 return lock.tickets.head == lock.tickets.tail;
67}
68
Nick Piggin3a556b22008-01-30 13:33:00 +010069/*
70 * Ticket locks are conceptually two parts, one indicating the current head of
71 * the queue, and the other indicating the current tail. The lock is acquired
72 * by atomically noting the tail and incrementing it by one (thus adding
73 * ourself to the queue and noting our position), then waiting until the head
74 * becomes equal to the the initial value of the tail.
75 *
76 * We use an xadd covering *both* parts of the lock, to increment the tail and
77 * also load the position of the head, which takes care of memory ordering
78 * issues and should be optimal for the uncontended case. Note the tail must be
79 * in the high part, because a wide xadd increment of the low part would carry
80 * up and contaminate the high part.
Nick Piggin3a556b22008-01-30 13:33:00 +010081 */
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +053082static __always_inline void arch_spin_lock(arch_spinlock_t *lock)
Thomas Gleixner1075cf72008-01-30 13:30:34 +010083{
Jeremy Fitzhardinge4a1ed4c2013-08-09 19:51:56 +053084 register struct __raw_tickets inc = { .tail = TICKET_LOCK_INC };
Nick Piggin314cdbe2008-01-30 13:31:21 +010085
Jeremy Fitzhardinge29944882010-07-13 14:07:45 -070086 inc = xadd(&lock->tickets, inc);
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +053087 if (likely(inc.head == inc.tail))
88 goto out;
Jeremy Fitzhardingec576a3e2010-07-03 01:06:04 +010089
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +053090 inc.tail &= ~TICKET_SLOWPATH_FLAG;
Jeremy Fitzhardingec576a3e2010-07-03 01:06:04 +010091 for (;;) {
Jeremy Fitzhardinge545ac132013-08-09 19:51:49 +053092 unsigned count = SPIN_THRESHOLD;
93
94 do {
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +053095 if (ACCESS_ONCE(lock->tickets.head) == inc.tail)
Jeremy Fitzhardinge545ac132013-08-09 19:51:49 +053096 goto out;
97 cpu_relax();
Jeremy Fitzhardinge545ac132013-08-09 19:51:49 +053098 } while (--count);
99 __ticket_lock_spinning(lock, inc.tail);
Jeremy Fitzhardingec576a3e2010-07-03 01:06:04 +0100100 }
Jeremy Fitzhardinge545ac132013-08-09 19:51:49 +0530101out: barrier(); /* make sure nothing creeps before the lock is taken */
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100102}
103
Jeremy Fitzhardingeb798df02013-08-09 19:51:51 +0530104static __always_inline int arch_spin_trylock(arch_spinlock_t *lock)
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100105{
Jeremy Fitzhardinge229855d2010-07-13 15:14:26 -0700106 arch_spinlock_t old, new;
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100107
Jeremy Fitzhardinge229855d2010-07-13 15:14:26 -0700108 old.tickets = ACCESS_ONCE(lock->tickets);
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +0530109 if (old.tickets.head != (old.tickets.tail & ~TICKET_SLOWPATH_FLAG))
Jeremy Fitzhardinge229855d2010-07-13 15:14:26 -0700110 return 0;
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100111
Jeremy Fitzhardinge4a1ed4c2013-08-09 19:51:56 +0530112 new.head_tail = old.head_tail + (TICKET_LOCK_INC << TICKET_SHIFT);
Jeremy Fitzhardinge229855d2010-07-13 15:14:26 -0700113
114 /* cmpxchg is a full barrier, so nothing can move before it */
115 return cmpxchg(&lock->head_tail, old.head_tail, new.head_tail) == old.head_tail;
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100116}
117
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +0530118static inline void __ticket_unlock_slowpath(arch_spinlock_t *lock,
119 arch_spinlock_t old)
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100120{
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +0530121 arch_spinlock_t new;
122
123 BUILD_BUG_ON(((__ticket_t)NR_CPUS) != NR_CPUS);
124
125 /* Perform the unlock on the "before" copy */
126 old.tickets.head += TICKET_LOCK_INC;
127
128 /* Clear the slowpath flag */
129 new.head_tail = old.head_tail & ~(TICKET_SLOWPATH_FLAG << TICKET_SHIFT);
130
131 /*
132 * If the lock is uncontended, clear the flag - use cmpxchg in
133 * case it changes behind our back though.
134 */
135 if (new.tickets.head != new.tickets.tail ||
136 cmpxchg(&lock->head_tail, old.head_tail,
137 new.head_tail) != old.head_tail) {
138 /*
139 * Lock still has someone queued for it, so wake up an
140 * appropriate waiter.
141 */
142 __ticket_unlock_kick(lock, old.tickets.head);
143 }
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100144}
145
Jeremy Fitzhardingeb798df02013-08-09 19:51:51 +0530146static __always_inline void arch_spin_unlock(arch_spinlock_t *lock)
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100147{
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +0530148 if (TICKET_SLOWPATH_FLAG &&
149 static_key_false(&paravirt_ticketlocks_enabled)) {
150 arch_spinlock_t prev;
Jeremy Fitzhardinge545ac132013-08-09 19:51:49 +0530151
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +0530152 prev = *lock;
153 add_smp(&lock->tickets.head, TICKET_LOCK_INC);
154
155 /* add_smp() is a full mb() */
156
157 if (unlikely(lock->tickets.tail & TICKET_SLOWPATH_FLAG))
158 __ticket_unlock_slowpath(lock, prev);
159 } else
160 __add(&lock->tickets.head, TICKET_LOCK_INC, UNLOCK_LOCK_PREFIX);
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100161}
162
Jeremy Fitzhardingeb798df02013-08-09 19:51:51 +0530163static inline int arch_spin_is_locked(arch_spinlock_t *lock)
Jan Beulich08f5fcb2008-09-05 13:26:39 +0100164{
Jeremy Fitzhardinge84eb9502010-07-02 23:26:36 +0100165 struct __raw_tickets tmp = ACCESS_ONCE(lock->tickets);
Jan Beulich08f5fcb2008-09-05 13:26:39 +0100166
Jan Beulich7931d492012-02-03 15:06:26 +0000167 return tmp.tail != tmp.head;
Jan Beulich08f5fcb2008-09-05 13:26:39 +0100168}
169
Jeremy Fitzhardingeb798df02013-08-09 19:51:51 +0530170static inline int arch_spin_is_contended(arch_spinlock_t *lock)
Jan Beulich08f5fcb2008-09-05 13:26:39 +0100171{
Jeremy Fitzhardinge84eb9502010-07-02 23:26:36 +0100172 struct __raw_tickets tmp = ACCESS_ONCE(lock->tickets);
Jan Beulich08f5fcb2008-09-05 13:26:39 +0100173
Jeremy Fitzhardinge4a1ed4c2013-08-09 19:51:56 +0530174 return (__ticket_t)(tmp.tail - tmp.head) > TICKET_LOCK_INC;
Jeremy Fitzhardinge74d4aff2008-07-07 12:07:50 -0700175}
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100176#define arch_spin_is_contended arch_spin_is_contended
Jeremy Fitzhardinge74d4aff2008-07-07 12:07:50 -0700177
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100178static __always_inline void arch_spin_lock_flags(arch_spinlock_t *lock,
Jeremy Fitzhardinge63d3a752008-08-19 13:19:36 -0700179 unsigned long flags)
180{
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100181 arch_spin_lock(lock);
Jeremy Fitzhardinge63d3a752008-08-19 13:19:36 -0700182}
183
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100184static inline void arch_spin_unlock_wait(arch_spinlock_t *lock)
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100185{
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100186 while (arch_spin_is_locked(lock))
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100187 cpu_relax();
188}
189
Waiman Longbd01ec12014-02-03 13:18:57 +0100190#ifndef CONFIG_QUEUE_RWLOCK
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100191/*
192 * Read-write spinlocks, allowing multiple readers
193 * but only one writer.
194 *
195 * NOTE! it is quite common to have readers in interrupts
196 * but no interrupt writers. For those circumstances we
197 * can "mix" irq-safe locks - any writer needs to get a
198 * irq-safe write-lock, but readers can get non-irqsafe
199 * read-locks.
200 *
201 * On x86, we implement read-write locks as a 32-bit counter
202 * with the high bit (sign) being the "contended" bit.
203 */
204
Nick Piggin314cdbe2008-01-30 13:31:21 +0100205/**
206 * read_can_lock - would read_trylock() succeed?
207 * @lock: the rwlock in question.
208 */
Thomas Gleixnere5931942009-12-03 20:08:46 +0100209static inline int arch_read_can_lock(arch_rwlock_t *lock)
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100210{
Jan Beulicha7500362011-07-19 13:00:45 +0100211 return lock->lock > 0;
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100212}
213
Nick Piggin314cdbe2008-01-30 13:31:21 +0100214/**
215 * write_can_lock - would write_trylock() succeed?
216 * @lock: the rwlock in question.
217 */
Thomas Gleixnere5931942009-12-03 20:08:46 +0100218static inline int arch_write_can_lock(arch_rwlock_t *lock)
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100219{
Jan Beulicha7500362011-07-19 13:00:45 +0100220 return lock->write == WRITE_LOCK_CMP;
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100221}
222
Thomas Gleixnere5931942009-12-03 20:08:46 +0100223static inline void arch_read_lock(arch_rwlock_t *rw)
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100224{
Jan Beulicha7500362011-07-19 13:00:45 +0100225 asm volatile(LOCK_PREFIX READ_LOCK_SIZE(dec) " (%0)\n\t"
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100226 "jns 1f\n"
227 "call __read_lock_failed\n\t"
228 "1:\n"
229 ::LOCK_PTR_REG (rw) : "memory");
230}
231
Thomas Gleixnere5931942009-12-03 20:08:46 +0100232static inline void arch_write_lock(arch_rwlock_t *rw)
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100233{
Jan Beulicha7500362011-07-19 13:00:45 +0100234 asm volatile(LOCK_PREFIX WRITE_LOCK_SUB(%1) "(%0)\n\t"
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100235 "jz 1f\n"
236 "call __write_lock_failed\n\t"
237 "1:\n"
Jan Beulicha7500362011-07-19 13:00:45 +0100238 ::LOCK_PTR_REG (&rw->write), "i" (RW_LOCK_BIAS)
239 : "memory");
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100240}
241
Thomas Gleixnere5931942009-12-03 20:08:46 +0100242static inline int arch_read_trylock(arch_rwlock_t *lock)
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100243{
Jan Beulicha7500362011-07-19 13:00:45 +0100244 READ_LOCK_ATOMIC(t) *count = (READ_LOCK_ATOMIC(t) *)lock;
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100245
Jan Beulicha7500362011-07-19 13:00:45 +0100246 if (READ_LOCK_ATOMIC(dec_return)(count) >= 0)
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100247 return 1;
Jan Beulicha7500362011-07-19 13:00:45 +0100248 READ_LOCK_ATOMIC(inc)(count);
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100249 return 0;
250}
251
Thomas Gleixnere5931942009-12-03 20:08:46 +0100252static inline int arch_write_trylock(arch_rwlock_t *lock)
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100253{
Jan Beulicha7500362011-07-19 13:00:45 +0100254 atomic_t *count = (atomic_t *)&lock->write;
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100255
Jan Beulicha7500362011-07-19 13:00:45 +0100256 if (atomic_sub_and_test(WRITE_LOCK_CMP, count))
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100257 return 1;
Jan Beulicha7500362011-07-19 13:00:45 +0100258 atomic_add(WRITE_LOCK_CMP, count);
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100259 return 0;
260}
261
Thomas Gleixnere5931942009-12-03 20:08:46 +0100262static inline void arch_read_unlock(arch_rwlock_t *rw)
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100263{
Jan Beulicha7500362011-07-19 13:00:45 +0100264 asm volatile(LOCK_PREFIX READ_LOCK_SIZE(inc) " %0"
265 :"+m" (rw->lock) : : "memory");
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100266}
267
Thomas Gleixnere5931942009-12-03 20:08:46 +0100268static inline void arch_write_unlock(arch_rwlock_t *rw)
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100269{
Jan Beulicha7500362011-07-19 13:00:45 +0100270 asm volatile(LOCK_PREFIX WRITE_LOCK_ADD(%1) "%0"
271 : "+m" (rw->write) : "i" (RW_LOCK_BIAS) : "memory");
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100272}
Waiman Longbd01ec12014-02-03 13:18:57 +0100273#else
274#include <asm/qrwlock.h>
275#endif /* CONFIG_QUEUE_RWLOCK */
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100276
Thomas Gleixnere5931942009-12-03 20:08:46 +0100277#define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
278#define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
Robin Holtf5f7eac2009-04-02 16:59:46 -0700279
Jan Beulicha7500362011-07-19 13:00:45 +0100280#undef READ_LOCK_SIZE
281#undef READ_LOCK_ATOMIC
282#undef WRITE_LOCK_ADD
283#undef WRITE_LOCK_SUB
284#undef WRITE_LOCK_CMP
285
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100286#define arch_spin_relax(lock) cpu_relax()
287#define arch_read_relax(lock) cpu_relax()
288#define arch_write_relax(lock) cpu_relax()
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100289
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700290#endif /* _ASM_X86_SPINLOCK_H */