spinlock.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. #ifndef _ASM_X86_SPINLOCK_H
  2. #define _ASM_X86_SPINLOCK_H
  3. #include <asm/atomic.h>
  4. #include <asm/rwlock.h>
  5. #include <asm/page.h>
  6. #include <asm/processor.h>
  7. #include <linux/compiler.h>
  8. #include <asm/paravirt.h>
  9. /*
  10. * Your basic SMP spinlocks, allowing only a single CPU anywhere
  11. *
  12. * Simple spin lock operations. There are two variants, one clears IRQ's
  13. * on the local processor, one does not.
  14. *
  15. * These are fair FIFO ticket locks, which are currently limited to 256
  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. # define REG_PTR_MODE "k"
  23. #else
  24. # define LOCK_PTR_REG "D"
  25. # define REG_PTR_MODE "q"
  26. #endif
  27. #if defined(CONFIG_X86_32) && \
  28. (defined(CONFIG_X86_OOSTORE) || defined(CONFIG_X86_PPRO_FENCE))
  29. /*
  30. * On PPro SMP or if we are using OOSTORE, we use a locked operation to unlock
  31. * (PPro errata 66, 92)
  32. */
  33. # define UNLOCK_LOCK_PREFIX LOCK_PREFIX
  34. #else
  35. # define UNLOCK_LOCK_PREFIX
  36. #endif
  37. /*
  38. * Ticket locks are conceptually two parts, one indicating the current head of
  39. * the queue, and the other indicating the current tail. The lock is acquired
  40. * by atomically noting the tail and incrementing it by one (thus adding
  41. * ourself to the queue and noting our position), then waiting until the head
  42. * becomes equal to the the initial value of the tail.
  43. *
  44. * We use an xadd covering *both* parts of the lock, to increment the tail and
  45. * also load the position of the head, which takes care of memory ordering
  46. * issues and should be optimal for the uncontended case. Note the tail must be
  47. * in the high part, because a wide xadd increment of the low part would carry
  48. * up and contaminate the high part.
  49. *
  50. * With fewer than 2^8 possible CPUs, we can use x86's partial registers to
  51. * save some instructions and make the code more elegant. There really isn't
  52. * much between them in performance though, especially as locks are out of line.
  53. */
  54. #if (NR_CPUS < 256)
  55. #define TICKET_SHIFT 8
  56. static __always_inline void __ticket_spin_lock(raw_spinlock_t *lock)
  57. {
  58. short inc = 0x0100;
  59. asm volatile (
  60. LOCK_PREFIX "xaddw %w0, %1\n"
  61. "1:\t"
  62. "cmpb %h0, %b0\n\t"
  63. "je 2f\n\t"
  64. "rep ; nop\n\t"
  65. "movb %1, %b0\n\t"
  66. /* don't need lfence here, because loads are in-order */
  67. "jmp 1b\n"
  68. "2:"
  69. : "+Q" (inc), "+m" (lock->slock)
  70. :
  71. : "memory", "cc");
  72. }
  73. static __always_inline int __ticket_spin_trylock(raw_spinlock_t *lock)
  74. {
  75. int tmp, new;
  76. asm volatile("movzwl %2, %0\n\t"
  77. "cmpb %h0,%b0\n\t"
  78. "leal 0x100(%" REG_PTR_MODE "0), %1\n\t"
  79. "jne 1f\n\t"
  80. LOCK_PREFIX "cmpxchgw %w1,%2\n\t"
  81. "1:"
  82. "sete %b1\n\t"
  83. "movzbl %b1,%0\n\t"
  84. : "=&a" (tmp), "=&q" (new), "+m" (lock->slock)
  85. :
  86. : "memory", "cc");
  87. return tmp;
  88. }
  89. static __always_inline void __ticket_spin_unlock(raw_spinlock_t *lock)
  90. {
  91. asm volatile(UNLOCK_LOCK_PREFIX "incb %0"
  92. : "+m" (lock->slock)
  93. :
  94. : "memory", "cc");
  95. }
  96. #else
  97. #define TICKET_SHIFT 16
  98. static __always_inline void __ticket_spin_lock(raw_spinlock_t *lock)
  99. {
  100. int inc = 0x00010000;
  101. int tmp;
  102. asm volatile(LOCK_PREFIX "xaddl %0, %1\n"
  103. "movzwl %w0, %2\n\t"
  104. "shrl $16, %0\n\t"
  105. "1:\t"
  106. "cmpl %0, %2\n\t"
  107. "je 2f\n\t"
  108. "rep ; nop\n\t"
  109. "movzwl %1, %2\n\t"
  110. /* don't need lfence here, because loads are in-order */
  111. "jmp 1b\n"
  112. "2:"
  113. : "+r" (inc), "+m" (lock->slock), "=&r" (tmp)
  114. :
  115. : "memory", "cc");
  116. }
  117. static __always_inline int __ticket_spin_trylock(raw_spinlock_t *lock)
  118. {
  119. int tmp;
  120. int new;
  121. asm volatile("movl %2,%0\n\t"
  122. "movl %0,%1\n\t"
  123. "roll $16, %0\n\t"
  124. "cmpl %0,%1\n\t"
  125. "leal 0x00010000(%" REG_PTR_MODE "0), %1\n\t"
  126. "jne 1f\n\t"
  127. LOCK_PREFIX "cmpxchgl %1,%2\n\t"
  128. "1:"
  129. "sete %b1\n\t"
  130. "movzbl %b1,%0\n\t"
  131. : "=&a" (tmp), "=&q" (new), "+m" (lock->slock)
  132. :
  133. : "memory", "cc");
  134. return tmp;
  135. }
  136. static __always_inline void __ticket_spin_unlock(raw_spinlock_t *lock)
  137. {
  138. asm volatile(UNLOCK_LOCK_PREFIX "incw %0"
  139. : "+m" (lock->slock)
  140. :
  141. : "memory", "cc");
  142. }
  143. #endif
  144. static inline int __ticket_spin_is_locked(raw_spinlock_t *lock)
  145. {
  146. int tmp = ACCESS_ONCE(lock->slock);
  147. return !!(((tmp >> TICKET_SHIFT) ^ tmp) & ((1 << TICKET_SHIFT) - 1));
  148. }
  149. static inline int __ticket_spin_is_contended(raw_spinlock_t *lock)
  150. {
  151. int tmp = ACCESS_ONCE(lock->slock);
  152. return (((tmp >> TICKET_SHIFT) - tmp) & ((1 << TICKET_SHIFT) - 1)) > 1;
  153. }
  154. #ifdef CONFIG_PARAVIRT
  155. /*
  156. * Define virtualization-friendly old-style lock byte lock, for use in
  157. * pv_lock_ops if desired.
  158. *
  159. * This differs from the pre-2.6.24 spinlock by always using xchgb
  160. * rather than decb to take the lock; this allows it to use a
  161. * zero-initialized lock structure. It also maintains a 1-byte
  162. * contention counter, so that we can implement
  163. * __byte_spin_is_contended.
  164. */
  165. struct __byte_spinlock {
  166. s8 lock;
  167. s8 spinners;
  168. };
  169. static inline int __byte_spin_is_locked(raw_spinlock_t *lock)
  170. {
  171. struct __byte_spinlock *bl = (struct __byte_spinlock *)lock;
  172. return bl->lock != 0;
  173. }
  174. static inline int __byte_spin_is_contended(raw_spinlock_t *lock)
  175. {
  176. struct __byte_spinlock *bl = (struct __byte_spinlock *)lock;
  177. return bl->spinners != 0;
  178. }
  179. static inline void __byte_spin_lock(raw_spinlock_t *lock)
  180. {
  181. struct __byte_spinlock *bl = (struct __byte_spinlock *)lock;
  182. s8 val = 1;
  183. asm("1: xchgb %1, %0\n"
  184. " test %1,%1\n"
  185. " jz 3f\n"
  186. " " LOCK_PREFIX "incb %2\n"
  187. "2: rep;nop\n"
  188. " cmpb $1, %0\n"
  189. " je 2b\n"
  190. " " LOCK_PREFIX "decb %2\n"
  191. " jmp 1b\n"
  192. "3:"
  193. : "+m" (bl->lock), "+q" (val), "+m" (bl->spinners): : "memory");
  194. }
  195. static inline int __byte_spin_trylock(raw_spinlock_t *lock)
  196. {
  197. struct __byte_spinlock *bl = (struct __byte_spinlock *)lock;
  198. u8 old = 1;
  199. asm("xchgb %1,%0"
  200. : "+m" (bl->lock), "+q" (old) : : "memory");
  201. return old == 0;
  202. }
  203. static inline void __byte_spin_unlock(raw_spinlock_t *lock)
  204. {
  205. struct __byte_spinlock *bl = (struct __byte_spinlock *)lock;
  206. smp_wmb();
  207. bl->lock = 0;
  208. }
  209. #else /* !CONFIG_PARAVIRT */
  210. static inline int __raw_spin_is_locked(raw_spinlock_t *lock)
  211. {
  212. return __ticket_spin_is_locked(lock);
  213. }
  214. static inline int __raw_spin_is_contended(raw_spinlock_t *lock)
  215. {
  216. return __ticket_spin_is_contended(lock);
  217. }
  218. static __always_inline void __raw_spin_lock(raw_spinlock_t *lock)
  219. {
  220. __ticket_spin_lock(lock);
  221. }
  222. static __always_inline int __raw_spin_trylock(raw_spinlock_t *lock)
  223. {
  224. return __ticket_spin_trylock(lock);
  225. }
  226. static __always_inline void __raw_spin_unlock(raw_spinlock_t *lock)
  227. {
  228. __ticket_spin_unlock(lock);
  229. }
  230. static __always_inline void __raw_spin_lock_flags(raw_spinlock_t *lock,
  231. unsigned long flags)
  232. {
  233. __raw_spin_lock(lock);
  234. }
  235. #endif /* CONFIG_PARAVIRT */
  236. static inline void __raw_spin_unlock_wait(raw_spinlock_t *lock)
  237. {
  238. while (__raw_spin_is_locked(lock))
  239. cpu_relax();
  240. }
  241. /*
  242. * Read-write spinlocks, allowing multiple readers
  243. * but only one writer.
  244. *
  245. * NOTE! it is quite common to have readers in interrupts
  246. * but no interrupt writers. For those circumstances we
  247. * can "mix" irq-safe locks - any writer needs to get a
  248. * irq-safe write-lock, but readers can get non-irqsafe
  249. * read-locks.
  250. *
  251. * On x86, we implement read-write locks as a 32-bit counter
  252. * with the high bit (sign) being the "contended" bit.
  253. */
  254. /**
  255. * read_can_lock - would read_trylock() succeed?
  256. * @lock: the rwlock in question.
  257. */
  258. static inline int __raw_read_can_lock(raw_rwlock_t *lock)
  259. {
  260. return (int)(lock)->lock > 0;
  261. }
  262. /**
  263. * write_can_lock - would write_trylock() succeed?
  264. * @lock: the rwlock in question.
  265. */
  266. static inline int __raw_write_can_lock(raw_rwlock_t *lock)
  267. {
  268. return (lock)->lock == RW_LOCK_BIAS;
  269. }
  270. static inline void __raw_read_lock(raw_rwlock_t *rw)
  271. {
  272. asm volatile(LOCK_PREFIX " subl $1,(%0)\n\t"
  273. "jns 1f\n"
  274. "call __read_lock_failed\n\t"
  275. "1:\n"
  276. ::LOCK_PTR_REG (rw) : "memory");
  277. }
  278. static inline void __raw_write_lock(raw_rwlock_t *rw)
  279. {
  280. asm volatile(LOCK_PREFIX " subl %1,(%0)\n\t"
  281. "jz 1f\n"
  282. "call __write_lock_failed\n\t"
  283. "1:\n"
  284. ::LOCK_PTR_REG (rw), "i" (RW_LOCK_BIAS) : "memory");
  285. }
  286. static inline int __raw_read_trylock(raw_rwlock_t *lock)
  287. {
  288. atomic_t *count = (atomic_t *)lock;
  289. atomic_dec(count);
  290. if (atomic_read(count) >= 0)
  291. return 1;
  292. atomic_inc(count);
  293. return 0;
  294. }
  295. static inline int __raw_write_trylock(raw_rwlock_t *lock)
  296. {
  297. atomic_t *count = (atomic_t *)lock;
  298. if (atomic_sub_and_test(RW_LOCK_BIAS, count))
  299. return 1;
  300. atomic_add(RW_LOCK_BIAS, count);
  301. return 0;
  302. }
  303. static inline void __raw_read_unlock(raw_rwlock_t *rw)
  304. {
  305. asm volatile(LOCK_PREFIX "incl %0" :"+m" (rw->lock) : : "memory");
  306. }
  307. static inline void __raw_write_unlock(raw_rwlock_t *rw)
  308. {
  309. asm volatile(LOCK_PREFIX "addl %1, %0"
  310. : "+m" (rw->lock) : "i" (RW_LOCK_BIAS) : "memory");
  311. }
  312. #define _raw_spin_relax(lock) cpu_relax()
  313. #define _raw_read_relax(lock) cpu_relax()
  314. #define _raw_write_relax(lock) cpu_relax()
  315. #endif /* _ASM_X86_SPINLOCK_H */