spinlock.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. #if (NR_CPUS < 256)
  54. static __always_inline void __ticket_spin_lock(arch_spinlock_t *lock)
  55. {
  56. register union {
  57. struct __raw_tickets tickets;
  58. unsigned short slock;
  59. } inc = { .slock = 1 << TICKET_SHIFT };
  60. asm volatile (LOCK_PREFIX "xaddw %w0, %1\n"
  61. : "+Q" (inc), "+m" (lock->slock) : : "memory", "cc");
  62. for (;;) {
  63. if (inc.tickets.head == inc.tickets.tail)
  64. break;
  65. cpu_relax();
  66. inc.tickets.head = ACCESS_ONCE(lock->tickets.head);
  67. }
  68. barrier(); /* make sure nothing creeps before the lock is taken */
  69. }
  70. static __always_inline int __ticket_spin_trylock(arch_spinlock_t *lock)
  71. {
  72. unsigned int tmp, new;
  73. asm volatile("movzwl %2, %0\n\t"
  74. "cmpb %h0,%b0\n\t"
  75. "leal 0x100(%" REG_PTR_MODE "0), %1\n\t"
  76. "jne 1f\n\t"
  77. LOCK_PREFIX "cmpxchgw %w1,%2\n\t"
  78. "1:"
  79. "sete %b1\n\t"
  80. "movzbl %b1,%0\n\t"
  81. : "=&a" (tmp), "=&q" (new), "+m" (lock->slock)
  82. :
  83. : "memory", "cc");
  84. return tmp;
  85. }
  86. static __always_inline void __ticket_spin_unlock(arch_spinlock_t *lock)
  87. {
  88. asm volatile(UNLOCK_LOCK_PREFIX "incb %0"
  89. : "+m" (lock->slock)
  90. :
  91. : "memory", "cc");
  92. }
  93. #else
  94. static __always_inline void __ticket_spin_lock(arch_spinlock_t *lock)
  95. {
  96. unsigned inc = 1 << TICKET_SHIFT;
  97. __ticket_t tmp;
  98. asm volatile(LOCK_PREFIX "xaddl %0, %1\n\t"
  99. : "+r" (inc), "+m" (lock->slock)
  100. : : "memory", "cc");
  101. tmp = inc;
  102. inc >>= TICKET_SHIFT;
  103. for (;;) {
  104. if ((__ticket_t)inc == tmp)
  105. break;
  106. cpu_relax();
  107. tmp = ACCESS_ONCE(lock->tickets.head);
  108. }
  109. barrier(); /* make sure nothing creeps before the lock is taken */
  110. }
  111. static __always_inline int __ticket_spin_trylock(arch_spinlock_t *lock)
  112. {
  113. unsigned tmp;
  114. unsigned new;
  115. asm volatile("movl %2,%0\n\t"
  116. "movl %0,%1\n\t"
  117. "roll $16, %0\n\t"
  118. "cmpl %0,%1\n\t"
  119. "leal 0x00010000(%" REG_PTR_MODE "0), %1\n\t"
  120. "jne 1f\n\t"
  121. LOCK_PREFIX "cmpxchgl %1,%2\n\t"
  122. "1:"
  123. "sete %b1\n\t"
  124. "movzbl %b1,%0\n\t"
  125. : "=&a" (tmp), "=&q" (new), "+m" (lock->slock)
  126. :
  127. : "memory", "cc");
  128. return tmp;
  129. }
  130. static __always_inline void __ticket_spin_unlock(arch_spinlock_t *lock)
  131. {
  132. asm volatile(UNLOCK_LOCK_PREFIX "incw %0"
  133. : "+m" (lock->slock)
  134. :
  135. : "memory", "cc");
  136. }
  137. #endif
  138. static inline int __ticket_spin_is_locked(arch_spinlock_t *lock)
  139. {
  140. struct __raw_tickets tmp = ACCESS_ONCE(lock->tickets);
  141. return !!(tmp.tail ^ tmp.head);
  142. }
  143. static inline int __ticket_spin_is_contended(arch_spinlock_t *lock)
  144. {
  145. struct __raw_tickets tmp = ACCESS_ONCE(lock->tickets);
  146. return ((tmp.tail - tmp.head) & TICKET_MASK) > 1;
  147. }
  148. #ifndef CONFIG_PARAVIRT_SPINLOCKS
  149. static inline int arch_spin_is_locked(arch_spinlock_t *lock)
  150. {
  151. return __ticket_spin_is_locked(lock);
  152. }
  153. static inline int arch_spin_is_contended(arch_spinlock_t *lock)
  154. {
  155. return __ticket_spin_is_contended(lock);
  156. }
  157. #define arch_spin_is_contended arch_spin_is_contended
  158. static __always_inline void arch_spin_lock(arch_spinlock_t *lock)
  159. {
  160. __ticket_spin_lock(lock);
  161. }
  162. static __always_inline int arch_spin_trylock(arch_spinlock_t *lock)
  163. {
  164. return __ticket_spin_trylock(lock);
  165. }
  166. static __always_inline void arch_spin_unlock(arch_spinlock_t *lock)
  167. {
  168. __ticket_spin_unlock(lock);
  169. }
  170. static __always_inline void arch_spin_lock_flags(arch_spinlock_t *lock,
  171. unsigned long flags)
  172. {
  173. arch_spin_lock(lock);
  174. }
  175. #endif /* CONFIG_PARAVIRT_SPINLOCKS */
  176. static inline void arch_spin_unlock_wait(arch_spinlock_t *lock)
  177. {
  178. while (arch_spin_is_locked(lock))
  179. cpu_relax();
  180. }
  181. /*
  182. * Read-write spinlocks, allowing multiple readers
  183. * but only one writer.
  184. *
  185. * NOTE! it is quite common to have readers in interrupts
  186. * but no interrupt writers. For those circumstances we
  187. * can "mix" irq-safe locks - any writer needs to get a
  188. * irq-safe write-lock, but readers can get non-irqsafe
  189. * read-locks.
  190. *
  191. * On x86, we implement read-write locks as a 32-bit counter
  192. * with the high bit (sign) being the "contended" bit.
  193. */
  194. /**
  195. * read_can_lock - would read_trylock() succeed?
  196. * @lock: the rwlock in question.
  197. */
  198. static inline int arch_read_can_lock(arch_rwlock_t *lock)
  199. {
  200. return lock->lock > 0;
  201. }
  202. /**
  203. * write_can_lock - would write_trylock() succeed?
  204. * @lock: the rwlock in question.
  205. */
  206. static inline int arch_write_can_lock(arch_rwlock_t *lock)
  207. {
  208. return lock->write == WRITE_LOCK_CMP;
  209. }
  210. static inline void arch_read_lock(arch_rwlock_t *rw)
  211. {
  212. asm volatile(LOCK_PREFIX READ_LOCK_SIZE(dec) " (%0)\n\t"
  213. "jns 1f\n"
  214. "call __read_lock_failed\n\t"
  215. "1:\n"
  216. ::LOCK_PTR_REG (rw) : "memory");
  217. }
  218. static inline void arch_write_lock(arch_rwlock_t *rw)
  219. {
  220. asm volatile(LOCK_PREFIX WRITE_LOCK_SUB(%1) "(%0)\n\t"
  221. "jz 1f\n"
  222. "call __write_lock_failed\n\t"
  223. "1:\n"
  224. ::LOCK_PTR_REG (&rw->write), "i" (RW_LOCK_BIAS)
  225. : "memory");
  226. }
  227. static inline int arch_read_trylock(arch_rwlock_t *lock)
  228. {
  229. READ_LOCK_ATOMIC(t) *count = (READ_LOCK_ATOMIC(t) *)lock;
  230. if (READ_LOCK_ATOMIC(dec_return)(count) >= 0)
  231. return 1;
  232. READ_LOCK_ATOMIC(inc)(count);
  233. return 0;
  234. }
  235. static inline int arch_write_trylock(arch_rwlock_t *lock)
  236. {
  237. atomic_t *count = (atomic_t *)&lock->write;
  238. if (atomic_sub_and_test(WRITE_LOCK_CMP, count))
  239. return 1;
  240. atomic_add(WRITE_LOCK_CMP, count);
  241. return 0;
  242. }
  243. static inline void arch_read_unlock(arch_rwlock_t *rw)
  244. {
  245. asm volatile(LOCK_PREFIX READ_LOCK_SIZE(inc) " %0"
  246. :"+m" (rw->lock) : : "memory");
  247. }
  248. static inline void arch_write_unlock(arch_rwlock_t *rw)
  249. {
  250. asm volatile(LOCK_PREFIX WRITE_LOCK_ADD(%1) "%0"
  251. : "+m" (rw->write) : "i" (RW_LOCK_BIAS) : "memory");
  252. }
  253. #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
  254. #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
  255. #undef READ_LOCK_SIZE
  256. #undef READ_LOCK_ATOMIC
  257. #undef WRITE_LOCK_ADD
  258. #undef WRITE_LOCK_SUB
  259. #undef WRITE_LOCK_CMP
  260. #define arch_spin_relax(lock) cpu_relax()
  261. #define arch_read_relax(lock) cpu_relax()
  262. #define arch_write_relax(lock) cpu_relax()
  263. /* The {read|write|spin}_lock() on x86 are full memory barriers. */
  264. static inline void smp_mb__after_lock(void) { }
  265. #define ARCH_HAS_SMP_MB_AFTER_LOCK
  266. #endif /* _ASM_X86_SPINLOCK_H */