blob: 64b611782ef0856f1744611936f76d6e8de1bb57 [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{
Raghavendra K Td6abfdb2015-02-06 16:44:11 +053049 set_bit(0, (volatile unsigned long *)&lock->tickets.head);
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +053050}
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 */
Raghavendra K Td6abfdb2015-02-06 16:44:11 +053063static inline int __tickets_equal(__ticket_t one, __ticket_t two)
64{
65 return !((one ^ two) & ~TICKET_SLOWPATH_FLAG);
66}
67
68static inline void __ticket_check_and_clear_slowpath(arch_spinlock_t *lock,
69 __ticket_t head)
70{
71 if (head & TICKET_SLOWPATH_FLAG) {
72 arch_spinlock_t old, new;
73
74 old.tickets.head = head;
75 new.tickets.head = head & ~TICKET_SLOWPATH_FLAG;
76 old.tickets.tail = new.tickets.head + TICKET_LOCK_INC;
77 new.tickets.tail = old.tickets.tail;
78
79 /* try to clear slowpath flag when there are no contenders */
80 cmpxchg(&lock->head_tail, old.head_tail, new.head_tail);
81 }
82}
Jeremy Fitzhardinge545ac132013-08-09 19:51:49 +053083
Linus Torvaldsbc08b442013-09-02 12:12:15 -070084static __always_inline int arch_spin_value_unlocked(arch_spinlock_t lock)
85{
Raghavendra K Td6abfdb2015-02-06 16:44:11 +053086 return __tickets_equal(lock.tickets.head, lock.tickets.tail);
Linus Torvaldsbc08b442013-09-02 12:12:15 -070087}
88
Nick Piggin3a556b22008-01-30 13:33:00 +010089/*
90 * Ticket locks are conceptually two parts, one indicating the current head of
91 * the queue, and the other indicating the current tail. The lock is acquired
92 * by atomically noting the tail and incrementing it by one (thus adding
93 * ourself to the queue and noting our position), then waiting until the head
94 * becomes equal to the the initial value of the tail.
95 *
96 * We use an xadd covering *both* parts of the lock, to increment the tail and
97 * also load the position of the head, which takes care of memory ordering
98 * issues and should be optimal for the uncontended case. Note the tail must be
99 * in the high part, because a wide xadd increment of the low part would carry
100 * up and contaminate the high part.
Nick Piggin3a556b22008-01-30 13:33:00 +0100101 */
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +0530102static __always_inline void arch_spin_lock(arch_spinlock_t *lock)
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100103{
Jeremy Fitzhardinge4a1ed4c2013-08-09 19:51:56 +0530104 register struct __raw_tickets inc = { .tail = TICKET_LOCK_INC };
Nick Piggin314cdbe2008-01-30 13:31:21 +0100105
Jeremy Fitzhardinge29944882010-07-13 14:07:45 -0700106 inc = xadd(&lock->tickets, inc);
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +0530107 if (likely(inc.head == inc.tail))
108 goto out;
Jeremy Fitzhardingec576a3e2010-07-03 01:06:04 +0100109
110 for (;;) {
Jeremy Fitzhardinge545ac132013-08-09 19:51:49 +0530111 unsigned count = SPIN_THRESHOLD;
112
113 do {
Raghavendra K Td6abfdb2015-02-06 16:44:11 +0530114 inc.head = READ_ONCE(lock->tickets.head);
115 if (__tickets_equal(inc.head, inc.tail))
116 goto clear_slowpath;
Jeremy Fitzhardinge545ac132013-08-09 19:51:49 +0530117 cpu_relax();
Jeremy Fitzhardinge545ac132013-08-09 19:51:49 +0530118 } while (--count);
119 __ticket_lock_spinning(lock, inc.tail);
Jeremy Fitzhardingec576a3e2010-07-03 01:06:04 +0100120 }
Raghavendra K Td6abfdb2015-02-06 16:44:11 +0530121clear_slowpath:
122 __ticket_check_and_clear_slowpath(lock, inc.head);
123out:
124 barrier(); /* make sure nothing creeps before the lock is taken */
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100125}
126
Jeremy Fitzhardingeb798df02013-08-09 19:51:51 +0530127static __always_inline int arch_spin_trylock(arch_spinlock_t *lock)
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100128{
Jeremy Fitzhardinge229855d2010-07-13 15:14:26 -0700129 arch_spinlock_t old, new;
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100130
Christian Borntraeger4f9d1382014-11-24 10:53:46 +0100131 old.tickets = READ_ONCE(lock->tickets);
Raghavendra K Td6abfdb2015-02-06 16:44:11 +0530132 if (!__tickets_equal(old.tickets.head, old.tickets.tail))
Jeremy Fitzhardinge229855d2010-07-13 15:14:26 -0700133 return 0;
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100134
Jeremy Fitzhardinge4a1ed4c2013-08-09 19:51:56 +0530135 new.head_tail = old.head_tail + (TICKET_LOCK_INC << TICKET_SHIFT);
Raghavendra K Td6abfdb2015-02-06 16:44:11 +0530136 new.head_tail &= ~TICKET_SLOWPATH_FLAG;
Jeremy Fitzhardinge229855d2010-07-13 15:14:26 -0700137
138 /* cmpxchg is a full barrier, so nothing can move before it */
139 return cmpxchg(&lock->head_tail, old.head_tail, new.head_tail) == old.head_tail;
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100140}
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 &&
Raghavendra K Td6abfdb2015-02-06 16:44:11 +0530145 static_key_false(&paravirt_ticketlocks_enabled)) {
146 __ticket_t head;
Jeremy Fitzhardinge545ac132013-08-09 19:51:49 +0530147
Raghavendra K Td6abfdb2015-02-06 16:44:11 +0530148 BUILD_BUG_ON(((__ticket_t)NR_CPUS) != NR_CPUS);
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +0530149
Raghavendra K Td6abfdb2015-02-06 16:44:11 +0530150 head = xadd(&lock->tickets.head, TICKET_LOCK_INC);
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +0530151
Raghavendra K Td6abfdb2015-02-06 16:44:11 +0530152 if (unlikely(head & TICKET_SLOWPATH_FLAG)) {
153 head &= ~TICKET_SLOWPATH_FLAG;
154 __ticket_unlock_kick(lock, (head + TICKET_LOCK_INC));
155 }
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +0530156 } else
157 __add(&lock->tickets.head, TICKET_LOCK_INC, UNLOCK_LOCK_PREFIX);
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100158}
159
Jeremy Fitzhardingeb798df02013-08-09 19:51:51 +0530160static inline int arch_spin_is_locked(arch_spinlock_t *lock)
Jan Beulich08f5fcb2008-09-05 13:26:39 +0100161{
Christian Borntraeger4f9d1382014-11-24 10:53:46 +0100162 struct __raw_tickets tmp = READ_ONCE(lock->tickets);
Jan Beulich08f5fcb2008-09-05 13:26:39 +0100163
Raghavendra K Td6abfdb2015-02-06 16:44:11 +0530164 return !__tickets_equal(tmp.tail, tmp.head);
Jan Beulich08f5fcb2008-09-05 13:26:39 +0100165}
166
Jeremy Fitzhardingeb798df02013-08-09 19:51:51 +0530167static inline int arch_spin_is_contended(arch_spinlock_t *lock)
Jan Beulich08f5fcb2008-09-05 13:26:39 +0100168{
Christian Borntraeger4f9d1382014-11-24 10:53:46 +0100169 struct __raw_tickets tmp = READ_ONCE(lock->tickets);
Jan Beulich08f5fcb2008-09-05 13:26:39 +0100170
Raghavendra K Td6abfdb2015-02-06 16:44:11 +0530171 tmp.head &= ~TICKET_SLOWPATH_FLAG;
Tahsin Erdogane8a4a262015-05-04 21:15:31 -0700172 return (__ticket_t)(tmp.tail - tmp.head) > TICKET_LOCK_INC;
Jeremy Fitzhardinge74d4aff2008-07-07 12:07:50 -0700173}
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100174#define arch_spin_is_contended arch_spin_is_contended
Jeremy Fitzhardinge74d4aff2008-07-07 12:07:50 -0700175
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100176static __always_inline void arch_spin_lock_flags(arch_spinlock_t *lock,
Jeremy Fitzhardinge63d3a752008-08-19 13:19:36 -0700177 unsigned long flags)
178{
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100179 arch_spin_lock(lock);
Jeremy Fitzhardinge63d3a752008-08-19 13:19:36 -0700180}
181
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100182static inline void arch_spin_unlock_wait(arch_spinlock_t *lock)
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100183{
Raghavendra K Td6abfdb2015-02-06 16:44:11 +0530184 __ticket_t head = READ_ONCE(lock->tickets.head);
Oleg Nesterov78bff1c2014-12-01 22:34:17 +0100185
186 for (;;) {
Raghavendra K Td6abfdb2015-02-06 16:44:11 +0530187 struct __raw_tickets tmp = READ_ONCE(lock->tickets);
Oleg Nesterov78bff1c2014-12-01 22:34:17 +0100188 /*
189 * We need to check "unlocked" in a loop, tmp.head == head
190 * can be false positive because of overflow.
191 */
Raghavendra K Td6abfdb2015-02-06 16:44:11 +0530192 if (__tickets_equal(tmp.head, tmp.tail) ||
193 !__tickets_equal(tmp.head, head))
Oleg Nesterov78bff1c2014-12-01 22:34:17 +0100194 break;
195
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100196 cpu_relax();
Oleg Nesterov78bff1c2014-12-01 22:34:17 +0100197 }
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100198}
199
200/*
201 * Read-write spinlocks, allowing multiple readers
202 * but only one writer.
203 *
204 * NOTE! it is quite common to have readers in interrupts
205 * but no interrupt writers. For those circumstances we
206 * can "mix" irq-safe locks - any writer needs to get a
207 * irq-safe write-lock, but readers can get non-irqsafe
208 * read-locks.
209 *
Waiman Long2ff810a2014-08-14 13:27:30 -0400210 * On x86, we implement read-write locks using the generic qrwlock with
211 * x86 specific optimization.
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100212 */
213
Waiman Longbd01ec12014-02-03 13:18:57 +0100214#include <asm/qrwlock.h>
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100215
Thomas Gleixnere5931942009-12-03 20:08:46 +0100216#define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
217#define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
Robin Holtf5f7eac2009-04-02 16:59:46 -0700218
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100219#define arch_spin_relax(lock) cpu_relax()
220#define arch_read_relax(lock) cpu_relax()
221#define arch_write_relax(lock) cpu_relax()
Thomas Gleixner1075cf72008-01-30 13:30:34 +0100222
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700223#endif /* _ASM_X86_SPINLOCK_H */