spinlock.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #ifndef _ASM_X86_SPINLOCK_H
  2. #define _ASM_X86_SPINLOCK_H
  3. #include <linux/jump_label.h>
  4. #include <linux/atomic.h>
  5. #include <asm/page.h>
  6. #include <asm/processor.h>
  7. #include <linux/compiler.h>
  8. #include <asm/paravirt.h>
  9. #include <asm/bitops.h>
  10. /*
  11. * Your basic SMP spinlocks, allowing only a single CPU anywhere
  12. *
  13. * Simple spin lock operations. There are two variants, one clears IRQ's
  14. * on the local processor, one does not.
  15. *
  16. * These are fair FIFO ticket locks, which support up to 2^16 CPUs.
  17. *
  18. * (the type definitions are in asm/spinlock_types.h)
  19. */
  20. #ifdef CONFIG_X86_32
  21. # define LOCK_PTR_REG "a"
  22. #else
  23. # define LOCK_PTR_REG "D"
  24. #endif
  25. #if defined(CONFIG_X86_32) && \
  26. (defined(CONFIG_X86_OOSTORE) || defined(CONFIG_X86_PPRO_FENCE))
  27. /*
  28. * On PPro SMP or if we are using OOSTORE, we use a locked operation to unlock
  29. * (PPro errata 66, 92)
  30. */
  31. # define UNLOCK_LOCK_PREFIX LOCK_PREFIX
  32. #else
  33. # define UNLOCK_LOCK_PREFIX
  34. #endif
  35. /* How long a lock should spin before we consider blocking */
  36. #define SPIN_THRESHOLD (1 << 15)
  37. extern struct static_key paravirt_ticketlocks_enabled;
  38. static __always_inline bool static_key_false(struct static_key *key);
  39. #ifdef CONFIG_PARAVIRT_SPINLOCKS
  40. static inline void __ticket_enter_slowpath(arch_spinlock_t *lock)
  41. {
  42. set_bit(0, (volatile unsigned long *)&lock->tickets.tail);
  43. }
  44. #else /* !CONFIG_PARAVIRT_SPINLOCKS */
  45. static __always_inline void __ticket_lock_spinning(arch_spinlock_t *lock,
  46. __ticket_t ticket)
  47. {
  48. }
  49. static inline void __ticket_unlock_kick(arch_spinlock_t *lock,
  50. __ticket_t ticket)
  51. {
  52. }
  53. #endif /* CONFIG_PARAVIRT_SPINLOCKS */
  54. /*
  55. * Ticket locks are conceptually two parts, one indicating the current head of
  56. * the queue, and the other indicating the current tail. The lock is acquired
  57. * by atomically noting the tail and incrementing it by one (thus adding
  58. * ourself to the queue and noting our position), then waiting until the head
  59. * becomes equal to the the initial value of the tail.
  60. *
  61. * We use an xadd covering *both* parts of the lock, to increment the tail and
  62. * also load the position of the head, which takes care of memory ordering
  63. * issues and should be optimal for the uncontended case. Note the tail must be
  64. * in the high part, because a wide xadd increment of the low part would carry
  65. * up and contaminate the high part.
  66. */
  67. static __always_inline void arch_spin_lock(arch_spinlock_t *lock)
  68. {
  69. register struct __raw_tickets inc = { .tail = TICKET_LOCK_INC };
  70. inc = xadd(&lock->tickets, inc);
  71. if (likely(inc.head == inc.tail))
  72. goto out;
  73. inc.tail &= ~TICKET_SLOWPATH_FLAG;
  74. for (;;) {
  75. unsigned count = SPIN_THRESHOLD;
  76. do {
  77. if (ACCESS_ONCE(lock->tickets.head) == inc.tail)
  78. goto out;
  79. cpu_relax();
  80. } while (--count);
  81. __ticket_lock_spinning(lock, inc.tail);
  82. }
  83. out: barrier(); /* make sure nothing creeps before the lock is taken */
  84. }
  85. static __always_inline int arch_spin_trylock(arch_spinlock_t *lock)
  86. {
  87. arch_spinlock_t old, new;
  88. old.tickets = ACCESS_ONCE(lock->tickets);
  89. if (old.tickets.head != (old.tickets.tail & ~TICKET_SLOWPATH_FLAG))
  90. return 0;
  91. new.head_tail = old.head_tail + (TICKET_LOCK_INC << TICKET_SHIFT);
  92. /* cmpxchg is a full barrier, so nothing can move before it */
  93. return cmpxchg(&lock->head_tail, old.head_tail, new.head_tail) == old.head_tail;
  94. }
  95. static inline void __ticket_unlock_slowpath(arch_spinlock_t *lock,
  96. arch_spinlock_t old)
  97. {
  98. arch_spinlock_t new;
  99. BUILD_BUG_ON(((__ticket_t)NR_CPUS) != NR_CPUS);
  100. /* Perform the unlock on the "before" copy */
  101. old.tickets.head += TICKET_LOCK_INC;
  102. /* Clear the slowpath flag */
  103. new.head_tail = old.head_tail & ~(TICKET_SLOWPATH_FLAG << TICKET_SHIFT);
  104. /*
  105. * If the lock is uncontended, clear the flag - use cmpxchg in
  106. * case it changes behind our back though.
  107. */
  108. if (new.tickets.head != new.tickets.tail ||
  109. cmpxchg(&lock->head_tail, old.head_tail,
  110. new.head_tail) != old.head_tail) {
  111. /*
  112. * Lock still has someone queued for it, so wake up an
  113. * appropriate waiter.
  114. */
  115. __ticket_unlock_kick(lock, old.tickets.head);
  116. }
  117. }
  118. static __always_inline void arch_spin_unlock(arch_spinlock_t *lock)
  119. {
  120. if (TICKET_SLOWPATH_FLAG &&
  121. static_key_false(&paravirt_ticketlocks_enabled)) {
  122. arch_spinlock_t prev;
  123. prev = *lock;
  124. add_smp(&lock->tickets.head, TICKET_LOCK_INC);
  125. /* add_smp() is a full mb() */
  126. if (unlikely(lock->tickets.tail & TICKET_SLOWPATH_FLAG))
  127. __ticket_unlock_slowpath(lock, prev);
  128. } else
  129. __add(&lock->tickets.head, TICKET_LOCK_INC, UNLOCK_LOCK_PREFIX);
  130. }
  131. static inline int arch_spin_is_locked(arch_spinlock_t *lock)
  132. {
  133. struct __raw_tickets tmp = ACCESS_ONCE(lock->tickets);
  134. return tmp.tail != tmp.head;
  135. }
  136. static inline int arch_spin_is_contended(arch_spinlock_t *lock)
  137. {
  138. struct __raw_tickets tmp = ACCESS_ONCE(lock->tickets);
  139. return (__ticket_t)(tmp.tail - tmp.head) > TICKET_LOCK_INC;
  140. }
  141. #define arch_spin_is_contended arch_spin_is_contended
  142. static __always_inline void arch_spin_lock_flags(arch_spinlock_t *lock,
  143. unsigned long flags)
  144. {
  145. arch_spin_lock(lock);
  146. }
  147. static inline void arch_spin_unlock_wait(arch_spinlock_t *lock)
  148. {
  149. while (arch_spin_is_locked(lock))
  150. cpu_relax();
  151. }
  152. /*
  153. * Read-write spinlocks, allowing multiple readers
  154. * but only one writer.
  155. *
  156. * NOTE! it is quite common to have readers in interrupts
  157. * but no interrupt writers. For those circumstances we
  158. * can "mix" irq-safe locks - any writer needs to get a
  159. * irq-safe write-lock, but readers can get non-irqsafe
  160. * read-locks.
  161. *
  162. * On x86, we implement read-write locks as a 32-bit counter
  163. * with the high bit (sign) being the "contended" bit.
  164. */
  165. /**
  166. * read_can_lock - would read_trylock() succeed?
  167. * @lock: the rwlock in question.
  168. */
  169. static inline int arch_read_can_lock(arch_rwlock_t *lock)
  170. {
  171. return lock->lock > 0;
  172. }
  173. /**
  174. * write_can_lock - would write_trylock() succeed?
  175. * @lock: the rwlock in question.
  176. */
  177. static inline int arch_write_can_lock(arch_rwlock_t *lock)
  178. {
  179. return lock->write == WRITE_LOCK_CMP;
  180. }
  181. static inline void arch_read_lock(arch_rwlock_t *rw)
  182. {
  183. asm volatile(LOCK_PREFIX READ_LOCK_SIZE(dec) " (%0)\n\t"
  184. "jns 1f\n"
  185. "call __read_lock_failed\n\t"
  186. "1:\n"
  187. ::LOCK_PTR_REG (rw) : "memory");
  188. }
  189. static inline void arch_write_lock(arch_rwlock_t *rw)
  190. {
  191. asm volatile(LOCK_PREFIX WRITE_LOCK_SUB(%1) "(%0)\n\t"
  192. "jz 1f\n"
  193. "call __write_lock_failed\n\t"
  194. "1:\n"
  195. ::LOCK_PTR_REG (&rw->write), "i" (RW_LOCK_BIAS)
  196. : "memory");
  197. }
  198. static inline int arch_read_trylock(arch_rwlock_t *lock)
  199. {
  200. READ_LOCK_ATOMIC(t) *count = (READ_LOCK_ATOMIC(t) *)lock;
  201. if (READ_LOCK_ATOMIC(dec_return)(count) >= 0)
  202. return 1;
  203. READ_LOCK_ATOMIC(inc)(count);
  204. return 0;
  205. }
  206. static inline int arch_write_trylock(arch_rwlock_t *lock)
  207. {
  208. atomic_t *count = (atomic_t *)&lock->write;
  209. if (atomic_sub_and_test(WRITE_LOCK_CMP, count))
  210. return 1;
  211. atomic_add(WRITE_LOCK_CMP, count);
  212. return 0;
  213. }
  214. static inline void arch_read_unlock(arch_rwlock_t *rw)
  215. {
  216. asm volatile(LOCK_PREFIX READ_LOCK_SIZE(inc) " %0"
  217. :"+m" (rw->lock) : : "memory");
  218. }
  219. static inline void arch_write_unlock(arch_rwlock_t *rw)
  220. {
  221. asm volatile(LOCK_PREFIX WRITE_LOCK_ADD(%1) "%0"
  222. : "+m" (rw->write) : "i" (RW_LOCK_BIAS) : "memory");
  223. }
  224. #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
  225. #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
  226. #undef READ_LOCK_SIZE
  227. #undef READ_LOCK_ATOMIC
  228. #undef WRITE_LOCK_ADD
  229. #undef WRITE_LOCK_SUB
  230. #undef WRITE_LOCK_CMP
  231. #define arch_spin_relax(lock) cpu_relax()
  232. #define arch_read_relax(lock) cpu_relax()
  233. #define arch_write_relax(lock) cpu_relax()
  234. /* The {read|write|spin}_lock() on x86 are full memory barriers. */
  235. static inline void smp_mb__after_lock(void) { }
  236. #define ARCH_HAS_SMP_MB_AFTER_LOCK
  237. #endif /* _ASM_X86_SPINLOCK_H */