blob: bf156ded74b56006a76cc02b8917984117af8afd [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
Linus Torvaldsbc08b442013-09-02 12:12:15 -070065static __always_inline int arch_spin_value_unlocked(arch_spinlock_t lock)
66{
67 return lock.tickets.head == lock.tickets.tail;
68}
69
Nick Piggin3a556b22008-01-30 13:33:00 +010070/*
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.
Nick Piggin3a556b22008-01-30 13:33:00 +010082 */
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +053083static __always_inline void arch_spin_lock(arch_spinlock_t *lock)
Thomas Gleixner1075cf72008-01-30 13:30:34 +010084{
Jeremy Fitzhardinge4a1ed4c2013-08-09 19:51:56 +053085 register struct __raw_tickets inc = { .tail = TICKET_LOCK_INC };
Nick Piggin314cdbe2008-01-30 13:31:21 +010086
Jeremy Fitzhardinge29944882010-07-13 14:07:45 -070087 inc = xadd(&lock->tickets, inc);
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +053088 if (likely(inc.head == inc.tail))
89 goto out;
Jeremy Fitzhardingec576a3e2010-07-03 01:06:04 +010090
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +053091 inc.tail &= ~TICKET_SLOWPATH_FLAG;
Jeremy Fitzhardingec576a3e2010-07-03 01:06:04 +010092 for (;;) {
Jeremy Fitzhardinge545ac132013-08-09 19:51:49 +053093 unsigned count = SPIN_THRESHOLD;
94
95 do {
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +053096 if (ACCESS_ONCE(lock->tickets.head) == inc.tail)
Jeremy Fitzhardinge545ac132013-08-09 19:51:49 +053097 goto out;
98 cpu_relax();
Jeremy Fitzhardinge545ac132013-08-09 19:51:49 +053099 } while (--count);
100 __ticket_lock_spinning(lock, inc.tail);
Jeremy Fitzhardingec576a3e2010-07-03 01:06:04 +0100101 }
Jeremy Fitzhardinge545ac132013-08-09 19:51:49 +0530102out: barrier(); /* make sure nothing creeps before the lock is taken */
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100103}
104
Jeremy Fitzhardingeb798df02013-08-09 19:51:51 +0530105static __always_inline int arch_spin_trylock(arch_spinlock_t *lock)
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100106{
Jeremy Fitzhardinge229855d2010-07-13 15:14:26 -0700107 arch_spinlock_t old, new;
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100108
Jeremy Fitzhardinge229855d2010-07-13 15:14:26 -0700109 old.tickets = ACCESS_ONCE(lock->tickets);
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +0530110 if (old.tickets.head != (old.tickets.tail & ~TICKET_SLOWPATH_FLAG))
Jeremy Fitzhardinge229855d2010-07-13 15:14:26 -0700111 return 0;
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100112
Jeremy Fitzhardinge4a1ed4c2013-08-09 19:51:56 +0530113 new.head_tail = old.head_tail + (TICKET_LOCK_INC << TICKET_SHIFT);
Jeremy Fitzhardinge229855d2010-07-13 15:14:26 -0700114
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;
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100117}
118
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +0530119static inline void __ticket_unlock_slowpath(arch_spinlock_t *lock,
120 arch_spinlock_t old)
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100121{
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +0530122 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 }
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100145}
146
Jeremy Fitzhardingeb798df02013-08-09 19:51:51 +0530147static __always_inline void arch_spin_unlock(arch_spinlock_t *lock)
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100148{
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +0530149 if (TICKET_SLOWPATH_FLAG &&
150 static_key_false(&paravirt_ticketlocks_enabled)) {
151 arch_spinlock_t prev;
Jeremy Fitzhardinge545ac132013-08-09 19:51:49 +0530152
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +0530153 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);
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100162}
163
Jeremy Fitzhardingeb798df02013-08-09 19:51:51 +0530164static inline int arch_spin_is_locked(arch_spinlock_t *lock)
Jan Beulich08f5fcb2008-09-05 13:26:39 +0100165{
Jeremy Fitzhardinge84eb9502010-07-02 23:26:36 +0100166 struct __raw_tickets tmp = ACCESS_ONCE(lock->tickets);
Jan Beulich08f5fcb2008-09-05 13:26:39 +0100167
Jan Beulich7931d492012-02-03 15:06:26 +0000168 return tmp.tail != tmp.head;
Jan Beulich08f5fcb2008-09-05 13:26:39 +0100169}
170
Jeremy Fitzhardingeb798df02013-08-09 19:51:51 +0530171static inline int arch_spin_is_contended(arch_spinlock_t *lock)
Jan Beulich08f5fcb2008-09-05 13:26:39 +0100172{
Jeremy Fitzhardinge84eb9502010-07-02 23:26:36 +0100173 struct __raw_tickets tmp = ACCESS_ONCE(lock->tickets);
Jan Beulich08f5fcb2008-09-05 13:26:39 +0100174
Jeremy Fitzhardinge4a1ed4c2013-08-09 19:51:56 +0530175 return (__ticket_t)(tmp.tail - tmp.head) > TICKET_LOCK_INC;
Jeremy Fitzhardinge74d4aff2008-07-07 12:07:50 -0700176}
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100177#define arch_spin_is_contended arch_spin_is_contended
Jeremy Fitzhardinge74d4aff2008-07-07 12:07:50 -0700178
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100179static __always_inline void arch_spin_lock_flags(arch_spinlock_t *lock,
Jeremy Fitzhardinge63d3a752008-08-19 13:19:36 -0700180 unsigned long flags)
181{
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100182 arch_spin_lock(lock);
Jeremy Fitzhardinge63d3a752008-08-19 13:19:36 -0700183}
184
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100185static inline void arch_spin_unlock_wait(arch_spinlock_t *lock)
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100186{
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100187 while (arch_spin_is_locked(lock))
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100188 cpu_relax();
189}
190
191/*
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}
273
Thomas Gleixnere5931942009-12-03 20:08:46 +0100274#define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
275#define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
Robin Holtf5f7eac2009-04-02 16:59:46 -0700276
Jan Beulicha7500362011-07-19 13:00:45 +0100277#undef READ_LOCK_SIZE
278#undef READ_LOCK_ATOMIC
279#undef WRITE_LOCK_ADD
280#undef WRITE_LOCK_SUB
281#undef WRITE_LOCK_CMP
282
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100283#define arch_spin_relax(lock) cpu_relax()
284#define arch_read_relax(lock) cpu_relax()
285#define arch_write_relax(lock) cpu_relax()
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100286
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700287#endif /* _ASM_X86_SPINLOCK_H */