spinlock.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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) && (defined(CONFIG_X86_PPRO_FENCE))
  26. /*
  27. * On PPro SMP, we use a locked operation to unlock
  28. * (PPro errata 66, 92)
  29. */
  30. # define UNLOCK_LOCK_PREFIX LOCK_PREFIX
  31. #else
  32. # define UNLOCK_LOCK_PREFIX
  33. #endif
  34. /* How long a lock should spin before we consider blocking */
  35. #define SPIN_THRESHOLD (1 << 15)
  36. extern struct static_key paravirt_ticketlocks_enabled;
  37. static __always_inline bool static_key_false(struct static_key *key);
  38. #ifdef CONFIG_PARAVIRT_SPINLOCKS
  39. static inline void __ticket_enter_slowpath(arch_spinlock_t *lock)
  40. {
  41. set_bit(0, (volatile unsigned long *)&lock->tickets.head);
  42. }
  43. #else /* !CONFIG_PARAVIRT_SPINLOCKS */
  44. static __always_inline void __ticket_lock_spinning(arch_spinlock_t *lock,
  45. __ticket_t ticket)
  46. {
  47. }
  48. static inline void __ticket_unlock_kick(arch_spinlock_t *lock,
  49. __ticket_t ticket)
  50. {
  51. }
  52. #endif /* CONFIG_PARAVIRT_SPINLOCKS */
  53. static inline int __tickets_equal(__ticket_t one, __ticket_t two)
  54. {
  55. return !((one ^ two) & ~TICKET_SLOWPATH_FLAG);
  56. }
  57. static inline void __ticket_check_and_clear_slowpath(arch_spinlock_t *lock,
  58. __ticket_t head)
  59. {
  60. if (head & TICKET_SLOWPATH_FLAG) {
  61. arch_spinlock_t old, new;
  62. old.tickets.head = head;
  63. new.tickets.head = head & ~TICKET_SLOWPATH_FLAG;
  64. old.tickets.tail = new.tickets.head + TICKET_LOCK_INC;
  65. new.tickets.tail = old.tickets.tail;
  66. /* try to clear slowpath flag when there are no contenders */
  67. cmpxchg(&lock->head_tail, old.head_tail, new.head_tail);
  68. }
  69. }
  70. static __always_inline int arch_spin_value_unlocked(arch_spinlock_t lock)
  71. {
  72. return __tickets_equal(lock.tickets.head, lock.tickets.tail);
  73. }
  74. /*
  75. * Ticket locks are conceptually two parts, one indicating the current head of
  76. * the queue, and the other indicating the current tail. The lock is acquired
  77. * by atomically noting the tail and incrementing it by one (thus adding
  78. * ourself to the queue and noting our position), then waiting until the head
  79. * becomes equal to the the initial value of the tail.
  80. *
  81. * We use an xadd covering *both* parts of the lock, to increment the tail and
  82. * also load the position of the head, which takes care of memory ordering
  83. * issues and should be optimal for the uncontended case. Note the tail must be
  84. * in the high part, because a wide xadd increment of the low part would carry
  85. * up and contaminate the high part.
  86. */
  87. static __always_inline void arch_spin_lock(arch_spinlock_t *lock)
  88. {
  89. register struct __raw_tickets inc = { .tail = TICKET_LOCK_INC };
  90. inc = xadd(&lock->tickets, inc);
  91. if (likely(inc.head == inc.tail))
  92. goto out;
  93. for (;;) {
  94. unsigned count = SPIN_THRESHOLD;
  95. do {
  96. inc.head = READ_ONCE(lock->tickets.head);
  97. if (__tickets_equal(inc.head, inc.tail))
  98. goto clear_slowpath;
  99. cpu_relax();
  100. } while (--count);
  101. __ticket_lock_spinning(lock, inc.tail);
  102. }
  103. clear_slowpath:
  104. __ticket_check_and_clear_slowpath(lock, inc.head);
  105. out:
  106. barrier(); /* make sure nothing creeps before the lock is taken */
  107. }
  108. static __always_inline int arch_spin_trylock(arch_spinlock_t *lock)
  109. {
  110. arch_spinlock_t old, new;
  111. old.tickets = READ_ONCE(lock->tickets);
  112. if (!__tickets_equal(old.tickets.head, old.tickets.tail))
  113. return 0;
  114. new.head_tail = old.head_tail + (TICKET_LOCK_INC << TICKET_SHIFT);
  115. new.head_tail &= ~TICKET_SLOWPATH_FLAG;
  116. /* cmpxchg is a full barrier, so nothing can move before it */
  117. return cmpxchg(&lock->head_tail, old.head_tail, new.head_tail) == old.head_tail;
  118. }
  119. static __always_inline void arch_spin_unlock(arch_spinlock_t *lock)
  120. {
  121. if (TICKET_SLOWPATH_FLAG &&
  122. static_key_false(&paravirt_ticketlocks_enabled)) {
  123. __ticket_t head;
  124. BUILD_BUG_ON(((__ticket_t)NR_CPUS) != NR_CPUS);
  125. head = xadd(&lock->tickets.head, TICKET_LOCK_INC);
  126. if (unlikely(head & TICKET_SLOWPATH_FLAG)) {
  127. head &= ~TICKET_SLOWPATH_FLAG;
  128. __ticket_unlock_kick(lock, (head + TICKET_LOCK_INC));
  129. }
  130. } else
  131. __add(&lock->tickets.head, TICKET_LOCK_INC, UNLOCK_LOCK_PREFIX);
  132. }
  133. static inline int arch_spin_is_locked(arch_spinlock_t *lock)
  134. {
  135. struct __raw_tickets tmp = READ_ONCE(lock->tickets);
  136. return !__tickets_equal(tmp.tail, tmp.head);
  137. }
  138. static inline int arch_spin_is_contended(arch_spinlock_t *lock)
  139. {
  140. struct __raw_tickets tmp = READ_ONCE(lock->tickets);
  141. tmp.head &= ~TICKET_SLOWPATH_FLAG;
  142. return (tmp.tail - tmp.head) > TICKET_LOCK_INC;
  143. }
  144. #define arch_spin_is_contended arch_spin_is_contended
  145. static __always_inline void arch_spin_lock_flags(arch_spinlock_t *lock,
  146. unsigned long flags)
  147. {
  148. arch_spin_lock(lock);
  149. }
  150. static inline void arch_spin_unlock_wait(arch_spinlock_t *lock)
  151. {
  152. __ticket_t head = READ_ONCE(lock->tickets.head);
  153. for (;;) {
  154. struct __raw_tickets tmp = READ_ONCE(lock->tickets);
  155. /*
  156. * We need to check "unlocked" in a loop, tmp.head == head
  157. * can be false positive because of overflow.
  158. */
  159. if (__tickets_equal(tmp.head, tmp.tail) ||
  160. !__tickets_equal(tmp.head, head))
  161. break;
  162. cpu_relax();
  163. }
  164. }
  165. /*
  166. * Read-write spinlocks, allowing multiple readers
  167. * but only one writer.
  168. *
  169. * NOTE! it is quite common to have readers in interrupts
  170. * but no interrupt writers. For those circumstances we
  171. * can "mix" irq-safe locks - any writer needs to get a
  172. * irq-safe write-lock, but readers can get non-irqsafe
  173. * read-locks.
  174. *
  175. * On x86, we implement read-write locks using the generic qrwlock with
  176. * x86 specific optimization.
  177. */
  178. #include <asm/qrwlock.h>
  179. #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
  180. #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
  181. #define arch_spin_relax(lock) cpu_relax()
  182. #define arch_read_relax(lock) cpu_relax()
  183. #define arch_write_relax(lock) cpu_relax()
  184. #endif /* _ASM_X86_SPINLOCK_H */