mcs_spinlock.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include <linux/percpu.h>
  2. #include <linux/sched.h>
  3. #include "mcs_spinlock.h"
  4. #ifdef CONFIG_SMP
  5. /*
  6. * An MCS like lock especially tailored for optimistic spinning for sleeping
  7. * lock implementations (mutex, rwsem, etc).
  8. *
  9. * Using a single mcs node per CPU is safe because sleeping locks should not be
  10. * called from interrupt context and we have preemption disabled while
  11. * spinning.
  12. */
  13. static DEFINE_PER_CPU_SHARED_ALIGNED(struct optimistic_spin_node, osq_node);
  14. /*
  15. * We use the value 0 to represent "no CPU", thus the encoded value
  16. * will be the CPU number incremented by 1.
  17. */
  18. static inline int encode_cpu(int cpu_nr)
  19. {
  20. return cpu_nr + 1;
  21. }
  22. static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val)
  23. {
  24. int cpu_nr = encoded_cpu_val - 1;
  25. return per_cpu_ptr(&osq_node, cpu_nr);
  26. }
  27. /*
  28. * Get a stable @node->next pointer, either for unlock() or unqueue() purposes.
  29. * Can return NULL in case we were the last queued and we updated @lock instead.
  30. */
  31. static inline struct optimistic_spin_node *
  32. osq_wait_next(struct optimistic_spin_queue *lock,
  33. struct optimistic_spin_node *node,
  34. struct optimistic_spin_node *prev)
  35. {
  36. struct optimistic_spin_node *next = NULL;
  37. int curr = encode_cpu(smp_processor_id());
  38. int old;
  39. /*
  40. * If there is a prev node in queue, then the 'old' value will be
  41. * the prev node's CPU #, else it's set to OSQ_UNLOCKED_VAL since if
  42. * we're currently last in queue, then the queue will then become empty.
  43. */
  44. old = prev ? prev->cpu : OSQ_UNLOCKED_VAL;
  45. for (;;) {
  46. if (atomic_read(&lock->tail) == curr &&
  47. atomic_cmpxchg(&lock->tail, curr, old) == curr) {
  48. /*
  49. * We were the last queued, we moved @lock back. @prev
  50. * will now observe @lock and will complete its
  51. * unlock()/unqueue().
  52. */
  53. break;
  54. }
  55. /*
  56. * We must xchg() the @node->next value, because if we were to
  57. * leave it in, a concurrent unlock()/unqueue() from
  58. * @node->next might complete Step-A and think its @prev is
  59. * still valid.
  60. *
  61. * If the concurrent unlock()/unqueue() wins the race, we'll
  62. * wait for either @lock to point to us, through its Step-B, or
  63. * wait for a new @node->next from its Step-C.
  64. */
  65. if (node->next) {
  66. next = xchg(&node->next, NULL);
  67. if (next)
  68. break;
  69. }
  70. cpu_relax_lowlatency();
  71. }
  72. return next;
  73. }
  74. bool osq_lock(struct optimistic_spin_queue *lock)
  75. {
  76. struct optimistic_spin_node *node = this_cpu_ptr(&osq_node);
  77. struct optimistic_spin_node *prev, *next;
  78. int curr = encode_cpu(smp_processor_id());
  79. int old;
  80. node->locked = 0;
  81. node->next = NULL;
  82. node->cpu = curr;
  83. old = atomic_xchg(&lock->tail, curr);
  84. if (old == OSQ_UNLOCKED_VAL)
  85. return true;
  86. prev = decode_cpu(old);
  87. node->prev = prev;
  88. ACCESS_ONCE(prev->next) = node;
  89. /*
  90. * Normally @prev is untouchable after the above store; because at that
  91. * moment unlock can proceed and wipe the node element from stack.
  92. *
  93. * However, since our nodes are static per-cpu storage, we're
  94. * guaranteed their existence -- this allows us to apply
  95. * cmpxchg in an attempt to undo our queueing.
  96. */
  97. while (!smp_load_acquire(&node->locked)) {
  98. /*
  99. * If we need to reschedule bail... so we can block.
  100. */
  101. if (need_resched())
  102. goto unqueue;
  103. cpu_relax_lowlatency();
  104. }
  105. return true;
  106. unqueue:
  107. /*
  108. * Step - A -- stabilize @prev
  109. *
  110. * Undo our @prev->next assignment; this will make @prev's
  111. * unlock()/unqueue() wait for a next pointer since @lock points to us
  112. * (or later).
  113. */
  114. for (;;) {
  115. if (prev->next == node &&
  116. cmpxchg(&prev->next, node, NULL) == node)
  117. break;
  118. /*
  119. * We can only fail the cmpxchg() racing against an unlock(),
  120. * in which case we should observe @node->locked becomming
  121. * true.
  122. */
  123. if (smp_load_acquire(&node->locked))
  124. return true;
  125. cpu_relax_lowlatency();
  126. /*
  127. * Or we race against a concurrent unqueue()'s step-B, in which
  128. * case its step-C will write us a new @node->prev pointer.
  129. */
  130. prev = ACCESS_ONCE(node->prev);
  131. }
  132. /*
  133. * Step - B -- stabilize @next
  134. *
  135. * Similar to unlock(), wait for @node->next or move @lock from @node
  136. * back to @prev.
  137. */
  138. next = osq_wait_next(lock, node, prev);
  139. if (!next)
  140. return false;
  141. /*
  142. * Step - C -- unlink
  143. *
  144. * @prev is stable because its still waiting for a new @prev->next
  145. * pointer, @next is stable because our @node->next pointer is NULL and
  146. * it will wait in Step-A.
  147. */
  148. ACCESS_ONCE(next->prev) = prev;
  149. ACCESS_ONCE(prev->next) = next;
  150. return false;
  151. }
  152. void osq_unlock(struct optimistic_spin_queue *lock)
  153. {
  154. struct optimistic_spin_node *node, *next;
  155. int curr = encode_cpu(smp_processor_id());
  156. /*
  157. * Fast path for the uncontended case.
  158. */
  159. if (likely(atomic_cmpxchg(&lock->tail, curr, OSQ_UNLOCKED_VAL) == curr))
  160. return;
  161. /*
  162. * Second most likely case.
  163. */
  164. node = this_cpu_ptr(&osq_node);
  165. next = xchg(&node->next, NULL);
  166. if (next) {
  167. ACCESS_ONCE(next->locked) = 1;
  168. return;
  169. }
  170. next = osq_wait_next(lock, node, NULL);
  171. if (next)
  172. ACCESS_ONCE(next->locked) = 1;
  173. }
  174. #endif