spinlock.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. static __always_inline int arch_spin_value_unlocked(arch_spinlock_t lock)
  55. {
  56. return lock.tickets.head == lock.tickets.tail;
  57. }
  58. /*
  59. * Ticket locks are conceptually two parts, one indicating the current head of
  60. * the queue, and the other indicating the current tail. The lock is acquired
  61. * by atomically noting the tail and incrementing it by one (thus adding
  62. * ourself to the queue and noting our position), then waiting until the head
  63. * becomes equal to the the initial value of the tail.
  64. *
  65. * We use an xadd covering *both* parts of the lock, to increment the tail and
  66. * also load the position of the head, which takes care of memory ordering
  67. * issues and should be optimal for the uncontended case. Note the tail must be
  68. * in the high part, because a wide xadd increment of the low part would carry
  69. * up and contaminate the high part.
  70. */
  71. static __always_inline void arch_spin_lock(arch_spinlock_t *lock)
  72. {
  73. register struct __raw_tickets inc = { .tail = TICKET_LOCK_INC };
  74. inc = xadd(&lock->tickets, inc);
  75. if (likely(inc.head == inc.tail))
  76. goto out;
  77. inc.tail &= ~TICKET_SLOWPATH_FLAG;
  78. for (;;) {
  79. unsigned count = SPIN_THRESHOLD;
  80. do {
  81. if (ACCESS_ONCE(lock->tickets.head) == inc.tail)
  82. goto out;
  83. cpu_relax();
  84. } while (--count);
  85. __ticket_lock_spinning(lock, inc.tail);
  86. }
  87. out: barrier(); /* make sure nothing creeps before the lock is taken */
  88. }
  89. static __always_inline int arch_spin_trylock(arch_spinlock_t *lock)
  90. {
  91. arch_spinlock_t old, new;
  92. old.tickets = ACCESS_ONCE(lock->tickets);
  93. if (old.tickets.head != (old.tickets.tail & ~TICKET_SLOWPATH_FLAG))
  94. return 0;
  95. new.head_tail = old.head_tail + (TICKET_LOCK_INC << TICKET_SHIFT);
  96. /* cmpxchg is a full barrier, so nothing can move before it */
  97. return cmpxchg(&lock->head_tail, old.head_tail, new.head_tail) == old.head_tail;
  98. }
  99. static inline void __ticket_unlock_slowpath(arch_spinlock_t *lock,
  100. arch_spinlock_t old)
  101. {
  102. arch_spinlock_t new;
  103. BUILD_BUG_ON(((__ticket_t)NR_CPUS) != NR_CPUS);
  104. /* Perform the unlock on the "before" copy */
  105. old.tickets.head += TICKET_LOCK_INC;
  106. /* Clear the slowpath flag */
  107. new.head_tail = old.head_tail & ~(TICKET_SLOWPATH_FLAG << TICKET_SHIFT);
  108. /*
  109. * If the lock is uncontended, clear the flag - use cmpxchg in
  110. * case it changes behind our back though.
  111. */
  112. if (new.tickets.head != new.tickets.tail ||
  113. cmpxchg(&lock->head_tail, old.head_tail,
  114. new.head_tail) != old.head_tail) {
  115. /*
  116. * Lock still has someone queued for it, so wake up an
  117. * appropriate waiter.
  118. */
  119. __ticket_unlock_kick(lock, old.tickets.head);
  120. }
  121. }
  122. static __always_inline void arch_spin_unlock(arch_spinlock_t *lock)
  123. {
  124. if (TICKET_SLOWPATH_FLAG &&
  125. static_key_false(&paravirt_ticketlocks_enabled)) {
  126. arch_spinlock_t prev;
  127. prev = *lock;
  128. add_smp(&lock->tickets.head, TICKET_LOCK_INC);
  129. /* add_smp() is a full mb() */
  130. if (unlikely(lock->tickets.tail & TICKET_SLOWPATH_FLAG))
  131. __ticket_unlock_slowpath(lock, prev);
  132. } else
  133. __add(&lock->tickets.head, TICKET_LOCK_INC, UNLOCK_LOCK_PREFIX);
  134. }
  135. static inline int arch_spin_is_locked(arch_spinlock_t *lock)
  136. {
  137. struct __raw_tickets tmp = ACCESS_ONCE(lock->tickets);
  138. return tmp.tail != tmp.head;
  139. }
  140. static inline int arch_spin_is_contended(arch_spinlock_t *lock)
  141. {
  142. struct __raw_tickets tmp = ACCESS_ONCE(lock->tickets);
  143. return (__ticket_t)(tmp.tail - tmp.head) > TICKET_LOCK_INC;
  144. }
  145. #define arch_spin_is_contended arch_spin_is_contended
  146. static __always_inline void arch_spin_lock_flags(arch_spinlock_t *lock,
  147. unsigned long flags)
  148. {
  149. arch_spin_lock(lock);
  150. }
  151. static inline void arch_spin_unlock_wait(arch_spinlock_t *lock)
  152. {
  153. while (arch_spin_is_locked(lock))
  154. cpu_relax();
  155. }
  156. /*
  157. * Read-write spinlocks, allowing multiple readers
  158. * but only one writer.
  159. *
  160. * NOTE! it is quite common to have readers in interrupts
  161. * but no interrupt writers. For those circumstances we
  162. * can "mix" irq-safe locks - any writer needs to get a
  163. * irq-safe write-lock, but readers can get non-irqsafe
  164. * read-locks.
  165. *
  166. * On x86, we implement read-write locks as a 32-bit counter
  167. * with the high bit (sign) being the "contended" bit.
  168. */
  169. /**
  170. * read_can_lock - would read_trylock() succeed?
  171. * @lock: the rwlock in question.
  172. */
  173. static inline int arch_read_can_lock(arch_rwlock_t *lock)
  174. {
  175. return lock->lock > 0;
  176. }
  177. /**
  178. * write_can_lock - would write_trylock() succeed?
  179. * @lock: the rwlock in question.
  180. */
  181. static inline int arch_write_can_lock(arch_rwlock_t *lock)
  182. {
  183. return lock->write == WRITE_LOCK_CMP;
  184. }
  185. static inline void arch_read_lock(arch_rwlock_t *rw)
  186. {
  187. asm volatile(LOCK_PREFIX READ_LOCK_SIZE(dec) " (%0)\n\t"
  188. "jns 1f\n"
  189. "call __read_lock_failed\n\t"
  190. "1:\n"
  191. ::LOCK_PTR_REG (rw) : "memory");
  192. }
  193. static inline void arch_write_lock(arch_rwlock_t *rw)
  194. {
  195. asm volatile(LOCK_PREFIX WRITE_LOCK_SUB(%1) "(%0)\n\t"
  196. "jz 1f\n"
  197. "call __write_lock_failed\n\t"
  198. "1:\n"
  199. ::LOCK_PTR_REG (&rw->write), "i" (RW_LOCK_BIAS)
  200. : "memory");
  201. }
  202. static inline int arch_read_trylock(arch_rwlock_t *lock)
  203. {
  204. READ_LOCK_ATOMIC(t) *count = (READ_LOCK_ATOMIC(t) *)lock;
  205. if (READ_LOCK_ATOMIC(dec_return)(count) >= 0)
  206. return 1;
  207. READ_LOCK_ATOMIC(inc)(count);
  208. return 0;
  209. }
  210. static inline int arch_write_trylock(arch_rwlock_t *lock)
  211. {
  212. atomic_t *count = (atomic_t *)&lock->write;
  213. if (atomic_sub_and_test(WRITE_LOCK_CMP, count))
  214. return 1;
  215. atomic_add(WRITE_LOCK_CMP, count);
  216. return 0;
  217. }
  218. static inline void arch_read_unlock(arch_rwlock_t *rw)
  219. {
  220. asm volatile(LOCK_PREFIX READ_LOCK_SIZE(inc) " %0"
  221. :"+m" (rw->lock) : : "memory");
  222. }
  223. static inline void arch_write_unlock(arch_rwlock_t *rw)
  224. {
  225. asm volatile(LOCK_PREFIX WRITE_LOCK_ADD(%1) "%0"
  226. : "+m" (rw->write) : "i" (RW_LOCK_BIAS) : "memory");
  227. }
  228. #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
  229. #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
  230. #undef READ_LOCK_SIZE
  231. #undef READ_LOCK_ATOMIC
  232. #undef WRITE_LOCK_ADD
  233. #undef WRITE_LOCK_SUB
  234. #undef WRITE_LOCK_CMP
  235. #define arch_spin_relax(lock) cpu_relax()
  236. #define arch_read_relax(lock) cpu_relax()
  237. #define arch_write_relax(lock) cpu_relax()
  238. #endif /* _ASM_X86_SPINLOCK_H */