spinlock.h 6.2 KB
Newer Older
H
H. Peter Anvin 已提交
1 2
#ifndef _ASM_X86_SPINLOCK_H
#define _ASM_X86_SPINLOCK_H
3

4
#include <linux/jump_label.h>
A
Arun Sharma 已提交
5
#include <linux/atomic.h>
T
Thomas Gleixner 已提交
6 7
#include <asm/page.h>
#include <asm/processor.h>
N
Nick Piggin 已提交
8
#include <linux/compiler.h>
9
#include <asm/paravirt.h>
10 11
#include <asm/bitops.h>

T
Thomas Gleixner 已提交
12 13 14 15 16 17
/*
 * Your basic SMP spinlocks, allowing only a single CPU anywhere
 *
 * Simple spin lock operations.  There are two variants, one clears IRQ's
 * on the local processor, one does not.
 *
18
 * These are fair FIFO ticket locks, which support up to 2^16 CPUs.
T
Thomas Gleixner 已提交
19 20 21 22
 *
 * (the type definitions are in asm/spinlock_types.h)
 */

23
#ifdef CONFIG_X86_32
T
Thomas Gleixner 已提交
24
# define LOCK_PTR_REG "a"
25
#else
T
Thomas Gleixner 已提交
26 27 28
# define LOCK_PTR_REG "D"
#endif

D
Dave Jones 已提交
29
#if defined(CONFIG_X86_32) && (defined(CONFIG_X86_PPRO_FENCE))
N
Nick Piggin 已提交
30
/*
D
Dave Jones 已提交
31
 * On PPro SMP, we use a locked operation to unlock
N
Nick Piggin 已提交
32 33 34 35 36
 * (PPro errata 66, 92)
 */
# define UNLOCK_LOCK_PREFIX LOCK_PREFIX
#else
# define UNLOCK_LOCK_PREFIX
N
Nick Piggin 已提交
37 38
#endif

39 40 41
/* How long a lock should spin before we consider blocking */
#define SPIN_THRESHOLD	(1 << 15)

42 43
extern struct static_key paravirt_ticketlocks_enabled;
static __always_inline bool static_key_false(struct static_key *key);
44

45 46 47
#ifdef CONFIG_PARAVIRT_SPINLOCKS

static inline void __ticket_enter_slowpath(arch_spinlock_t *lock)
48
{
49
	set_bit(0, (volatile unsigned long *)&lock->tickets.head);
50 51
}

52 53 54
#else  /* !CONFIG_PARAVIRT_SPINLOCKS */
static __always_inline void __ticket_lock_spinning(arch_spinlock_t *lock,
							__ticket_t ticket)
55 56
{
}
57 58
static inline void __ticket_unlock_kick(arch_spinlock_t *lock,
							__ticket_t ticket)
59 60 61
{
}

62
#endif /* CONFIG_PARAVIRT_SPINLOCKS */
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
static inline int  __tickets_equal(__ticket_t one, __ticket_t two)
{
	return !((one ^ two) & ~TICKET_SLOWPATH_FLAG);
}

static inline void __ticket_check_and_clear_slowpath(arch_spinlock_t *lock,
							__ticket_t head)
{
	if (head & TICKET_SLOWPATH_FLAG) {
		arch_spinlock_t old, new;

		old.tickets.head = head;
		new.tickets.head = head & ~TICKET_SLOWPATH_FLAG;
		old.tickets.tail = new.tickets.head + TICKET_LOCK_INC;
		new.tickets.tail = old.tickets.tail;

		/* try to clear slowpath flag when there are no contenders */
		cmpxchg(&lock->head_tail, old.head_tail, new.head_tail);
	}
}
83

84 85
static __always_inline int arch_spin_value_unlocked(arch_spinlock_t lock)
{
86
	return __tickets_equal(lock.tickets.head, lock.tickets.tail);
87 88
}

N
Nick Piggin 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101
/*
 * Ticket locks are conceptually two parts, one indicating the current head of
 * the queue, and the other indicating the current tail. The lock is acquired
 * by atomically noting the tail and incrementing it by one (thus adding
 * ourself to the queue and noting our position), then waiting until the head
 * becomes equal to the the initial value of the tail.
 *
 * We use an xadd covering *both* parts of the lock, to increment the tail and
 * also load the position of the head, which takes care of memory ordering
 * issues and should be optimal for the uncontended case. Note the tail must be
 * in the high part, because a wide xadd increment of the low part would carry
 * up and contaminate the high part.
 */
102
static __always_inline void arch_spin_lock(arch_spinlock_t *lock)
T
Thomas Gleixner 已提交
103
{
104
	register struct __raw_tickets inc = { .tail = TICKET_LOCK_INC };
N
Nick Piggin 已提交
105

106
	inc = xadd(&lock->tickets, inc);
107 108
	if (likely(inc.head == inc.tail))
		goto out;
109 110

	for (;;) {
111 112 113
		unsigned count = SPIN_THRESHOLD;

		do {
114 115 116
			inc.head = READ_ONCE(lock->tickets.head);
			if (__tickets_equal(inc.head, inc.tail))
				goto clear_slowpath;
117 118 119
			cpu_relax();
		} while (--count);
		__ticket_lock_spinning(lock, inc.tail);
120
	}
121 122 123 124
clear_slowpath:
	__ticket_check_and_clear_slowpath(lock, inc.head);
out:
	barrier();	/* make sure nothing creeps before the lock is taken */
T
Thomas Gleixner 已提交
125
}
N
Nick Piggin 已提交
126

127
static __always_inline int arch_spin_trylock(arch_spinlock_t *lock)
T
Thomas Gleixner 已提交
128
{
129 130
	arch_spinlock_t old, new;

131
	old.tickets = READ_ONCE(lock->tickets);
132
	if (!__tickets_equal(old.tickets.head, old.tickets.tail))
133
		return 0;
N
Nick Piggin 已提交
134

135
	new.head_tail = old.head_tail + (TICKET_LOCK_INC << TICKET_SHIFT);
136
	new.head_tail &= ~TICKET_SLOWPATH_FLAG;
137 138 139

	/* cmpxchg is a full barrier, so nothing can move before it */
	return cmpxchg(&lock->head_tail, old.head_tail, new.head_tail) == old.head_tail;
T
Thomas Gleixner 已提交
140 141
}

142
static __always_inline void arch_spin_unlock(arch_spinlock_t *lock)
143
{
144
	if (TICKET_SLOWPATH_FLAG &&
145 146
		static_key_false(&paravirt_ticketlocks_enabled)) {
		__ticket_t head;
147

148
		BUILD_BUG_ON(((__ticket_t)NR_CPUS) != NR_CPUS);
149

150
		head = xadd(&lock->tickets.head, TICKET_LOCK_INC);
151

152 153 154 155
		if (unlikely(head & TICKET_SLOWPATH_FLAG)) {
			head &= ~TICKET_SLOWPATH_FLAG;
			__ticket_unlock_kick(lock, (head + TICKET_LOCK_INC));
		}
156 157
	} else
		__add(&lock->tickets.head, TICKET_LOCK_INC, UNLOCK_LOCK_PREFIX);
158
}
159

160
static inline int arch_spin_is_locked(arch_spinlock_t *lock)
161
{
162
	struct __raw_tickets tmp = READ_ONCE(lock->tickets);
163

164
	return !__tickets_equal(tmp.tail, tmp.head);
165 166
}

167
static inline int arch_spin_is_contended(arch_spinlock_t *lock)
168
{
169
	struct __raw_tickets tmp = READ_ONCE(lock->tickets);
170

171 172
	tmp.head &= ~TICKET_SLOWPATH_FLAG;
	return (tmp.tail - tmp.head) > TICKET_LOCK_INC;
173
}
174
#define arch_spin_is_contended	arch_spin_is_contended
175

176
static __always_inline void arch_spin_lock_flags(arch_spinlock_t *lock,
177 178
						  unsigned long flags)
{
179
	arch_spin_lock(lock);
180 181
}

182
static inline void arch_spin_unlock_wait(arch_spinlock_t *lock)
T
Thomas Gleixner 已提交
183
{
184
	__ticket_t head = READ_ONCE(lock->tickets.head);
185 186

	for (;;) {
187
		struct __raw_tickets tmp = READ_ONCE(lock->tickets);
188 189 190 191
		/*
		 * We need to check "unlocked" in a loop, tmp.head == head
		 * can be false positive because of overflow.
		 */
192 193
		if (__tickets_equal(tmp.head, tmp.tail) ||
				!__tickets_equal(tmp.head, head))
194 195
			break;

T
Thomas Gleixner 已提交
196
		cpu_relax();
197
	}
T
Thomas Gleixner 已提交
198 199 200 201 202 203 204 205 206 207 208 209
}

/*
 * Read-write spinlocks, allowing multiple readers
 * but only one writer.
 *
 * NOTE! it is quite common to have readers in interrupts
 * but no interrupt writers. For those circumstances we
 * can "mix" irq-safe locks - any writer needs to get a
 * irq-safe write-lock, but readers can get non-irqsafe
 * read-locks.
 *
210 211
 * On x86, we implement read-write locks using the generic qrwlock with
 * x86 specific optimization.
T
Thomas Gleixner 已提交
212 213
 */

214
#include <asm/qrwlock.h>
T
Thomas Gleixner 已提交
215

216 217
#define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
#define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
218

219 220 221
#define arch_spin_relax(lock)	cpu_relax()
#define arch_read_relax(lock)	cpu_relax()
#define arch_write_relax(lock)	cpu_relax()
T
Thomas Gleixner 已提交
222

H
H. Peter Anvin 已提交
223
#endif /* _ASM_X86_SPINLOCK_H */