spinlock.h 6.4 KB

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