H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 1 | #ifndef _ASM_X86_SPINLOCK_H |
| 2 | #define _ASM_X86_SPINLOCK_H |
Glauber de Oliveira Costa | 2fed0c5 | 2008-01-30 13:30:33 +0100 | [diff] [blame] | 3 | |
Jeremy Fitzhardinge | 96f853e | 2013-08-09 19:51:58 +0530 | [diff] [blame] | 4 | #include <linux/jump_label.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 5 | #include <linux/atomic.h> |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 6 | #include <asm/page.h> |
| 7 | #include <asm/processor.h> |
Nick Piggin | 314cdbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 8 | #include <linux/compiler.h> |
Jeremy Fitzhardinge | 74d4aff | 2008-07-07 12:07:50 -0700 | [diff] [blame] | 9 | #include <asm/paravirt.h> |
Jeremy Fitzhardinge | 96f853e | 2013-08-09 19:51:58 +0530 | [diff] [blame] | 10 | #include <asm/bitops.h> |
| 11 | |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 12 | /* |
| 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 Weinberger | 83be4ff | 2012-08-14 14:47:37 -0700 | [diff] [blame] | 18 | * These are fair FIFO ticket locks, which support up to 2^16 CPUs. |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 19 | * |
| 20 | * (the type definitions are in asm/spinlock_types.h) |
| 21 | */ |
| 22 | |
Thomas Gleixner | 96a388d | 2007-10-11 11:20:03 +0200 | [diff] [blame] | 23 | #ifdef CONFIG_X86_32 |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 24 | # define LOCK_PTR_REG "a" |
Thomas Gleixner | 96a388d | 2007-10-11 11:20:03 +0200 | [diff] [blame] | 25 | #else |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 26 | # define LOCK_PTR_REG "D" |
Thomas Gleixner | 96a388d | 2007-10-11 11:20:03 +0200 | [diff] [blame] | 27 | #endif |
Glauber de Oliveira Costa | 2fed0c5 | 2008-01-30 13:30:33 +0100 | [diff] [blame] | 28 | |
Dave Jones | 09df7c4 | 2014-03-10 19:32:22 -0400 | [diff] [blame] | 29 | #if defined(CONFIG_X86_32) && (defined(CONFIG_X86_PPRO_FENCE)) |
Nick Piggin | 3a556b2 | 2008-01-30 13:33:00 +0100 | [diff] [blame] | 30 | /* |
Dave Jones | 09df7c4 | 2014-03-10 19:32:22 -0400 | [diff] [blame] | 31 | * On PPro SMP, we use a locked operation to unlock |
Nick Piggin | 3a556b2 | 2008-01-30 13:33:00 +0100 | [diff] [blame] | 32 | * (PPro errata 66, 92) |
| 33 | */ |
| 34 | # define UNLOCK_LOCK_PREFIX LOCK_PREFIX |
| 35 | #else |
| 36 | # define UNLOCK_LOCK_PREFIX |
Nick Piggin | 314cdbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 37 | #endif |
| 38 | |
Jeremy Fitzhardinge | 545ac13 | 2013-08-09 19:51:49 +0530 | [diff] [blame] | 39 | /* How long a lock should spin before we consider blocking */ |
| 40 | #define SPIN_THRESHOLD (1 << 15) |
| 41 | |
Jeremy Fitzhardinge | 96f853e | 2013-08-09 19:51:58 +0530 | [diff] [blame] | 42 | extern struct static_key paravirt_ticketlocks_enabled; |
| 43 | static __always_inline bool static_key_false(struct static_key *key); |
Jeremy Fitzhardinge | 545ac13 | 2013-08-09 19:51:49 +0530 | [diff] [blame] | 44 | |
Ingo Molnar | 62c7a1e | 2015-05-11 09:47:23 +0200 | [diff] [blame] | 45 | #ifdef CONFIG_QUEUED_SPINLOCKS |
Waiman Long | d73a339 | 2015-04-24 14:56:31 -0400 | [diff] [blame] | 46 | #include <asm/qspinlock.h> |
| 47 | #else |
| 48 | |
Jeremy Fitzhardinge | 96f853e | 2013-08-09 19:51:58 +0530 | [diff] [blame] | 49 | #ifdef CONFIG_PARAVIRT_SPINLOCKS |
| 50 | |
| 51 | static inline void __ticket_enter_slowpath(arch_spinlock_t *lock) |
| 52 | { |
Raghavendra K T | d6abfdb | 2015-02-06 16:44:11 +0530 | [diff] [blame] | 53 | set_bit(0, (volatile unsigned long *)&lock->tickets.head); |
Jeremy Fitzhardinge | 96f853e | 2013-08-09 19:51:58 +0530 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | #else /* !CONFIG_PARAVIRT_SPINLOCKS */ |
| 57 | static __always_inline void __ticket_lock_spinning(arch_spinlock_t *lock, |
| 58 | __ticket_t ticket) |
| 59 | { |
| 60 | } |
| 61 | static inline void __ticket_unlock_kick(arch_spinlock_t *lock, |
Jeremy Fitzhardinge | 545ac13 | 2013-08-09 19:51:49 +0530 | [diff] [blame] | 62 | __ticket_t ticket) |
| 63 | { |
| 64 | } |
| 65 | |
Jeremy Fitzhardinge | 96f853e | 2013-08-09 19:51:58 +0530 | [diff] [blame] | 66 | #endif /* CONFIG_PARAVIRT_SPINLOCKS */ |
Raghavendra K T | d6abfdb | 2015-02-06 16:44:11 +0530 | [diff] [blame] | 67 | static inline int __tickets_equal(__ticket_t one, __ticket_t two) |
| 68 | { |
| 69 | return !((one ^ two) & ~TICKET_SLOWPATH_FLAG); |
| 70 | } |
| 71 | |
| 72 | static inline void __ticket_check_and_clear_slowpath(arch_spinlock_t *lock, |
| 73 | __ticket_t head) |
| 74 | { |
| 75 | if (head & TICKET_SLOWPATH_FLAG) { |
| 76 | arch_spinlock_t old, new; |
| 77 | |
| 78 | old.tickets.head = head; |
| 79 | new.tickets.head = head & ~TICKET_SLOWPATH_FLAG; |
| 80 | old.tickets.tail = new.tickets.head + TICKET_LOCK_INC; |
| 81 | new.tickets.tail = old.tickets.tail; |
| 82 | |
| 83 | /* try to clear slowpath flag when there are no contenders */ |
| 84 | cmpxchg(&lock->head_tail, old.head_tail, new.head_tail); |
| 85 | } |
| 86 | } |
Jeremy Fitzhardinge | 545ac13 | 2013-08-09 19:51:49 +0530 | [diff] [blame] | 87 | |
Linus Torvalds | bc08b44 | 2013-09-02 12:12:15 -0700 | [diff] [blame] | 88 | static __always_inline int arch_spin_value_unlocked(arch_spinlock_t lock) |
| 89 | { |
Raghavendra K T | d6abfdb | 2015-02-06 16:44:11 +0530 | [diff] [blame] | 90 | return __tickets_equal(lock.tickets.head, lock.tickets.tail); |
Linus Torvalds | bc08b44 | 2013-09-02 12:12:15 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Nick Piggin | 3a556b2 | 2008-01-30 13:33:00 +0100 | [diff] [blame] | 93 | /* |
| 94 | * Ticket locks are conceptually two parts, one indicating the current head of |
| 95 | * the queue, and the other indicating the current tail. The lock is acquired |
| 96 | * by atomically noting the tail and incrementing it by one (thus adding |
| 97 | * ourself to the queue and noting our position), then waiting until the head |
| 98 | * becomes equal to the the initial value of the tail. |
| 99 | * |
| 100 | * We use an xadd covering *both* parts of the lock, to increment the tail and |
| 101 | * also load the position of the head, which takes care of memory ordering |
| 102 | * issues and should be optimal for the uncontended case. Note the tail must be |
| 103 | * in the high part, because a wide xadd increment of the low part would carry |
| 104 | * up and contaminate the high part. |
Nick Piggin | 3a556b2 | 2008-01-30 13:33:00 +0100 | [diff] [blame] | 105 | */ |
Jeremy Fitzhardinge | 96f853e | 2013-08-09 19:51:58 +0530 | [diff] [blame] | 106 | static __always_inline void arch_spin_lock(arch_spinlock_t *lock) |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 107 | { |
Jeremy Fitzhardinge | 4a1ed4c | 2013-08-09 19:51:56 +0530 | [diff] [blame] | 108 | register struct __raw_tickets inc = { .tail = TICKET_LOCK_INC }; |
Nick Piggin | 314cdbe | 2008-01-30 13:31:21 +0100 | [diff] [blame] | 109 | |
Jeremy Fitzhardinge | 2994488 | 2010-07-13 14:07:45 -0700 | [diff] [blame] | 110 | inc = xadd(&lock->tickets, inc); |
Jeremy Fitzhardinge | 96f853e | 2013-08-09 19:51:58 +0530 | [diff] [blame] | 111 | if (likely(inc.head == inc.tail)) |
| 112 | goto out; |
Jeremy Fitzhardinge | c576a3e | 2010-07-03 01:06:04 +0100 | [diff] [blame] | 113 | |
| 114 | for (;;) { |
Jeremy Fitzhardinge | 545ac13 | 2013-08-09 19:51:49 +0530 | [diff] [blame] | 115 | unsigned count = SPIN_THRESHOLD; |
| 116 | |
| 117 | do { |
Raghavendra K T | d6abfdb | 2015-02-06 16:44:11 +0530 | [diff] [blame] | 118 | inc.head = READ_ONCE(lock->tickets.head); |
| 119 | if (__tickets_equal(inc.head, inc.tail)) |
| 120 | goto clear_slowpath; |
Jeremy Fitzhardinge | 545ac13 | 2013-08-09 19:51:49 +0530 | [diff] [blame] | 121 | cpu_relax(); |
Jeremy Fitzhardinge | 545ac13 | 2013-08-09 19:51:49 +0530 | [diff] [blame] | 122 | } while (--count); |
| 123 | __ticket_lock_spinning(lock, inc.tail); |
Jeremy Fitzhardinge | c576a3e | 2010-07-03 01:06:04 +0100 | [diff] [blame] | 124 | } |
Raghavendra K T | d6abfdb | 2015-02-06 16:44:11 +0530 | [diff] [blame] | 125 | clear_slowpath: |
| 126 | __ticket_check_and_clear_slowpath(lock, inc.head); |
| 127 | out: |
| 128 | barrier(); /* make sure nothing creeps before the lock is taken */ |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 129 | } |
| 130 | |
Jeremy Fitzhardinge | b798df0 | 2013-08-09 19:51:51 +0530 | [diff] [blame] | 131 | static __always_inline int arch_spin_trylock(arch_spinlock_t *lock) |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 132 | { |
Jeremy Fitzhardinge | 229855d | 2010-07-13 15:14:26 -0700 | [diff] [blame] | 133 | arch_spinlock_t old, new; |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 134 | |
Christian Borntraeger | 4f9d138 | 2014-11-24 10:53:46 +0100 | [diff] [blame] | 135 | old.tickets = READ_ONCE(lock->tickets); |
Raghavendra K T | d6abfdb | 2015-02-06 16:44:11 +0530 | [diff] [blame] | 136 | if (!__tickets_equal(old.tickets.head, old.tickets.tail)) |
Jeremy Fitzhardinge | 229855d | 2010-07-13 15:14:26 -0700 | [diff] [blame] | 137 | return 0; |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 138 | |
Jeremy Fitzhardinge | 4a1ed4c | 2013-08-09 19:51:56 +0530 | [diff] [blame] | 139 | new.head_tail = old.head_tail + (TICKET_LOCK_INC << TICKET_SHIFT); |
Raghavendra K T | d6abfdb | 2015-02-06 16:44:11 +0530 | [diff] [blame] | 140 | new.head_tail &= ~TICKET_SLOWPATH_FLAG; |
Jeremy Fitzhardinge | 229855d | 2010-07-13 15:14:26 -0700 | [diff] [blame] | 141 | |
| 142 | /* cmpxchg is a full barrier, so nothing can move before it */ |
| 143 | return cmpxchg(&lock->head_tail, old.head_tail, new.head_tail) == old.head_tail; |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 144 | } |
| 145 | |
Jeremy Fitzhardinge | b798df0 | 2013-08-09 19:51:51 +0530 | [diff] [blame] | 146 | static __always_inline void arch_spin_unlock(arch_spinlock_t *lock) |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 147 | { |
Jeremy Fitzhardinge | 96f853e | 2013-08-09 19:51:58 +0530 | [diff] [blame] | 148 | if (TICKET_SLOWPATH_FLAG && |
Raghavendra K T | d6abfdb | 2015-02-06 16:44:11 +0530 | [diff] [blame] | 149 | static_key_false(¶virt_ticketlocks_enabled)) { |
| 150 | __ticket_t head; |
Jeremy Fitzhardinge | 545ac13 | 2013-08-09 19:51:49 +0530 | [diff] [blame] | 151 | |
Raghavendra K T | d6abfdb | 2015-02-06 16:44:11 +0530 | [diff] [blame] | 152 | BUILD_BUG_ON(((__ticket_t)NR_CPUS) != NR_CPUS); |
Jeremy Fitzhardinge | 96f853e | 2013-08-09 19:51:58 +0530 | [diff] [blame] | 153 | |
Raghavendra K T | d6abfdb | 2015-02-06 16:44:11 +0530 | [diff] [blame] | 154 | head = xadd(&lock->tickets.head, TICKET_LOCK_INC); |
Jeremy Fitzhardinge | 96f853e | 2013-08-09 19:51:58 +0530 | [diff] [blame] | 155 | |
Raghavendra K T | d6abfdb | 2015-02-06 16:44:11 +0530 | [diff] [blame] | 156 | if (unlikely(head & TICKET_SLOWPATH_FLAG)) { |
| 157 | head &= ~TICKET_SLOWPATH_FLAG; |
| 158 | __ticket_unlock_kick(lock, (head + TICKET_LOCK_INC)); |
| 159 | } |
Jeremy Fitzhardinge | 96f853e | 2013-08-09 19:51:58 +0530 | [diff] [blame] | 160 | } else |
| 161 | __add(&lock->tickets.head, TICKET_LOCK_INC, UNLOCK_LOCK_PREFIX); |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 162 | } |
| 163 | |
Jeremy Fitzhardinge | b798df0 | 2013-08-09 19:51:51 +0530 | [diff] [blame] | 164 | static inline int arch_spin_is_locked(arch_spinlock_t *lock) |
Jan Beulich | 08f5fcb | 2008-09-05 13:26:39 +0100 | [diff] [blame] | 165 | { |
Christian Borntraeger | 4f9d138 | 2014-11-24 10:53:46 +0100 | [diff] [blame] | 166 | struct __raw_tickets tmp = READ_ONCE(lock->tickets); |
Jan Beulich | 08f5fcb | 2008-09-05 13:26:39 +0100 | [diff] [blame] | 167 | |
Raghavendra K T | d6abfdb | 2015-02-06 16:44:11 +0530 | [diff] [blame] | 168 | return !__tickets_equal(tmp.tail, tmp.head); |
Jan Beulich | 08f5fcb | 2008-09-05 13:26:39 +0100 | [diff] [blame] | 169 | } |
| 170 | |
Jeremy Fitzhardinge | b798df0 | 2013-08-09 19:51:51 +0530 | [diff] [blame] | 171 | static inline int arch_spin_is_contended(arch_spinlock_t *lock) |
Jan Beulich | 08f5fcb | 2008-09-05 13:26:39 +0100 | [diff] [blame] | 172 | { |
Christian Borntraeger | 4f9d138 | 2014-11-24 10:53:46 +0100 | [diff] [blame] | 173 | struct __raw_tickets tmp = READ_ONCE(lock->tickets); |
Jan Beulich | 08f5fcb | 2008-09-05 13:26:39 +0100 | [diff] [blame] | 174 | |
Raghavendra K T | d6abfdb | 2015-02-06 16:44:11 +0530 | [diff] [blame] | 175 | tmp.head &= ~TICKET_SLOWPATH_FLAG; |
Tahsin Erdogan | e8a4a26 | 2015-05-04 21:15:31 -0700 | [diff] [blame] | 176 | return (__ticket_t)(tmp.tail - tmp.head) > TICKET_LOCK_INC; |
Jeremy Fitzhardinge | 74d4aff | 2008-07-07 12:07:50 -0700 | [diff] [blame] | 177 | } |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 178 | #define arch_spin_is_contended arch_spin_is_contended |
Jeremy Fitzhardinge | 74d4aff | 2008-07-07 12:07:50 -0700 | [diff] [blame] | 179 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 180 | static __always_inline void arch_spin_lock_flags(arch_spinlock_t *lock, |
Jeremy Fitzhardinge | 63d3a75 | 2008-08-19 13:19:36 -0700 | [diff] [blame] | 181 | unsigned long flags) |
| 182 | { |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 183 | arch_spin_lock(lock); |
Jeremy Fitzhardinge | 63d3a75 | 2008-08-19 13:19:36 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 186 | static inline void arch_spin_unlock_wait(arch_spinlock_t *lock) |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 187 | { |
Raghavendra K T | d6abfdb | 2015-02-06 16:44:11 +0530 | [diff] [blame] | 188 | __ticket_t head = READ_ONCE(lock->tickets.head); |
Oleg Nesterov | 78bff1c | 2014-12-01 22:34:17 +0100 | [diff] [blame] | 189 | |
| 190 | for (;;) { |
Raghavendra K T | d6abfdb | 2015-02-06 16:44:11 +0530 | [diff] [blame] | 191 | struct __raw_tickets tmp = READ_ONCE(lock->tickets); |
Oleg Nesterov | 78bff1c | 2014-12-01 22:34:17 +0100 | [diff] [blame] | 192 | /* |
| 193 | * We need to check "unlocked" in a loop, tmp.head == head |
| 194 | * can be false positive because of overflow. |
| 195 | */ |
Raghavendra K T | d6abfdb | 2015-02-06 16:44:11 +0530 | [diff] [blame] | 196 | if (__tickets_equal(tmp.head, tmp.tail) || |
| 197 | !__tickets_equal(tmp.head, head)) |
Oleg Nesterov | 78bff1c | 2014-12-01 22:34:17 +0100 | [diff] [blame] | 198 | break; |
| 199 | |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 200 | cpu_relax(); |
Oleg Nesterov | 78bff1c | 2014-12-01 22:34:17 +0100 | [diff] [blame] | 201 | } |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 202 | } |
Ingo Molnar | 62c7a1e | 2015-05-11 09:47:23 +0200 | [diff] [blame] | 203 | #endif /* CONFIG_QUEUED_SPINLOCKS */ |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 204 | |
| 205 | /* |
| 206 | * Read-write spinlocks, allowing multiple readers |
| 207 | * but only one writer. |
| 208 | * |
| 209 | * NOTE! it is quite common to have readers in interrupts |
| 210 | * but no interrupt writers. For those circumstances we |
| 211 | * can "mix" irq-safe locks - any writer needs to get a |
| 212 | * irq-safe write-lock, but readers can get non-irqsafe |
| 213 | * read-locks. |
| 214 | * |
Waiman Long | 2ff810a | 2014-08-14 13:27:30 -0400 | [diff] [blame] | 215 | * On x86, we implement read-write locks using the generic qrwlock with |
| 216 | * x86 specific optimization. |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 217 | */ |
| 218 | |
Waiman Long | bd01ec1 | 2014-02-03 13:18:57 +0100 | [diff] [blame] | 219 | #include <asm/qrwlock.h> |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 220 | |
Thomas Gleixner | e593194 | 2009-12-03 20:08:46 +0100 | [diff] [blame] | 221 | #define arch_read_lock_flags(lock, flags) arch_read_lock(lock) |
| 222 | #define arch_write_lock_flags(lock, flags) arch_write_lock(lock) |
Robin Holt | f5f7eac | 2009-04-02 16:59:46 -0700 | [diff] [blame] | 223 | |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 224 | #define arch_spin_relax(lock) cpu_relax() |
| 225 | #define arch_read_relax(lock) cpu_relax() |
| 226 | #define arch_write_relax(lock) cpu_relax() |
Thomas Gleixner | 1075cf7 | 2008-01-30 13:30:34 +0100 | [diff] [blame] | 227 | |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 228 | #endif /* _ASM_X86_SPINLOCK_H */ |