osq_lock.c 5.1 KB

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