spinlock.h 6.8 KB

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