spinlock.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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.tail);
  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 __always_inline int arch_spin_value_unlocked(arch_spinlock_t lock)
  54. {
  55. return lock.tickets.head == lock.tickets.tail;
  56. }
  57. /*
  58. * Ticket locks are conceptually two parts, one indicating the current head of
  59. * the queue, and the other indicating the current tail. The lock is acquired
  60. * by atomically noting the tail and incrementing it by one (thus adding
  61. * ourself to the queue and noting our position), then waiting until the head
  62. * becomes equal to the the initial value of the tail.
  63. *
  64. * We use an xadd covering *both* parts of the lock, to increment the tail and
  65. * also load the position of the head, which takes care of memory ordering
  66. * issues and should be optimal for the uncontended case. Note the tail must be
  67. * in the high part, because a wide xadd increment of the low part would carry
  68. * up and contaminate the high part.
  69. */
  70. static __always_inline void arch_spin_lock(arch_spinlock_t *lock)
  71. {
  72. register struct __raw_tickets inc = { .tail = TICKET_LOCK_INC };
  73. inc = xadd(&lock->tickets, inc);
  74. if (likely(inc.head == inc.tail))
  75. goto out;
  76. inc.tail &= ~TICKET_SLOWPATH_FLAG;
  77. for (;;) {
  78. unsigned count = SPIN_THRESHOLD;
  79. do {
  80. if (ACCESS_ONCE(lock->tickets.head) == inc.tail)
  81. goto out;
  82. cpu_relax();
  83. } while (--count);
  84. __ticket_lock_spinning(lock, inc.tail);
  85. }
  86. out: barrier(); /* make sure nothing creeps before the lock is taken */
  87. }
  88. static __always_inline int arch_spin_trylock(arch_spinlock_t *lock)
  89. {
  90. arch_spinlock_t old, new;
  91. old.tickets = ACCESS_ONCE(lock->tickets);
  92. if (old.tickets.head != (old.tickets.tail & ~TICKET_SLOWPATH_FLAG))
  93. return 0;
  94. new.head_tail = old.head_tail + (TICKET_LOCK_INC << TICKET_SHIFT);
  95. /* cmpxchg is a full barrier, so nothing can move before it */
  96. return cmpxchg(&lock->head_tail, old.head_tail, new.head_tail) == old.head_tail;
  97. }
  98. static inline void __ticket_unlock_slowpath(arch_spinlock_t *lock,
  99. arch_spinlock_t old)
  100. {
  101. arch_spinlock_t new;
  102. BUILD_BUG_ON(((__ticket_t)NR_CPUS) != NR_CPUS);
  103. /* Perform the unlock on the "before" copy */
  104. old.tickets.head += TICKET_LOCK_INC;
  105. /* Clear the slowpath flag */
  106. new.head_tail = old.head_tail & ~(TICKET_SLOWPATH_FLAG << TICKET_SHIFT);
  107. /*
  108. * If the lock is uncontended, clear the flag - use cmpxchg in
  109. * case it changes behind our back though.
  110. */
  111. if (new.tickets.head != new.tickets.tail ||
  112. cmpxchg(&lock->head_tail, old.head_tail,
  113. new.head_tail) != old.head_tail) {
  114. /*
  115. * Lock still has someone queued for it, so wake up an
  116. * appropriate waiter.
  117. */
  118. __ticket_unlock_kick(lock, old.tickets.head);
  119. }
  120. }
  121. static __always_inline void arch_spin_unlock(arch_spinlock_t *lock)
  122. {
  123. if (TICKET_SLOWPATH_FLAG &&
  124. static_key_false(&paravirt_ticketlocks_enabled)) {
  125. arch_spinlock_t prev;
  126. prev = *lock;
  127. add_smp(&lock->tickets.head, TICKET_LOCK_INC);
  128. /* add_smp() is a full mb() */
  129. if (unlikely(lock->tickets.tail & TICKET_SLOWPATH_FLAG))
  130. __ticket_unlock_slowpath(lock, prev);
  131. } else
  132. __add(&lock->tickets.head, TICKET_LOCK_INC, UNLOCK_LOCK_PREFIX);
  133. }
  134. static inline int arch_spin_is_locked(arch_spinlock_t *lock)
  135. {
  136. struct __raw_tickets tmp = ACCESS_ONCE(lock->tickets);
  137. return tmp.tail != tmp.head;
  138. }
  139. static inline int arch_spin_is_contended(arch_spinlock_t *lock)
  140. {
  141. struct __raw_tickets tmp = ACCESS_ONCE(lock->tickets);
  142. return (__ticket_t)(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. while (arch_spin_is_locked(lock))
  153. cpu_relax();
  154. }
  155. /*
  156. * Read-write spinlocks, allowing multiple readers
  157. * but only one writer.
  158. *
  159. * NOTE! it is quite common to have readers in interrupts
  160. * but no interrupt writers. For those circumstances we
  161. * can "mix" irq-safe locks - any writer needs to get a
  162. * irq-safe write-lock, but readers can get non-irqsafe
  163. * read-locks.
  164. *
  165. * On x86, we implement read-write locks using the generic qrwlock with
  166. * x86 specific optimization.
  167. */
  168. #include <asm/qrwlock.h>
  169. #define arch_read_lock_flags(lock, flags) arch_read_lock(lock)
  170. #define arch_write_lock_flags(lock, flags) arch_write_lock(lock)
  171. #define arch_spin_relax(lock) cpu_relax()
  172. #define arch_read_relax(lock) cpu_relax()
  173. #define arch_write_relax(lock) cpu_relax()
  174. #endif /* _ASM_X86_SPINLOCK_H */