osq_lock.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. /*
  94. * osq_lock() unqueue
  95. *
  96. * node->prev = prev osq_wait_next()
  97. * WMB MB
  98. * prev->next = node next->prev = prev // unqueue-C
  99. *
  100. * Here 'node->prev' and 'next->prev' are the same variable and we need
  101. * to ensure these stores happen in-order to avoid corrupting the list.
  102. */
  103. smp_wmb();
  104. WRITE_ONCE(prev->next, node);
  105. /*
  106. * Normally @prev is untouchable after the above store; because at that
  107. * moment unlock can proceed and wipe the node element from stack.
  108. *
  109. * However, since our nodes are static per-cpu storage, we're
  110. * guaranteed their existence -- this allows us to apply
  111. * cmpxchg in an attempt to undo our queueing.
  112. */
  113. while (!READ_ONCE(node->locked)) {
  114. /*
  115. * If we need to reschedule bail... so we can block.
  116. */
  117. if (need_resched())
  118. goto unqueue;
  119. cpu_relax_lowlatency();
  120. }
  121. return true;
  122. unqueue:
  123. /*
  124. * Step - A -- stabilize @prev
  125. *
  126. * Undo our @prev->next assignment; this will make @prev's
  127. * unlock()/unqueue() wait for a next pointer since @lock points to us
  128. * (or later).
  129. */
  130. for (;;) {
  131. if (prev->next == node &&
  132. cmpxchg(&prev->next, node, NULL) == node)
  133. break;
  134. /*
  135. * We can only fail the cmpxchg() racing against an unlock(),
  136. * in which case we should observe @node->locked becomming
  137. * true.
  138. */
  139. if (smp_load_acquire(&node->locked))
  140. return true;
  141. cpu_relax_lowlatency();
  142. /*
  143. * Or we race against a concurrent unqueue()'s step-B, in which
  144. * case its step-C will write us a new @node->prev pointer.
  145. */
  146. prev = READ_ONCE(node->prev);
  147. }
  148. /*
  149. * Step - B -- stabilize @next
  150. *
  151. * Similar to unlock(), wait for @node->next or move @lock from @node
  152. * back to @prev.
  153. */
  154. next = osq_wait_next(lock, node, prev);
  155. if (!next)
  156. return false;
  157. /*
  158. * Step - C -- unlink
  159. *
  160. * @prev is stable because its still waiting for a new @prev->next
  161. * pointer, @next is stable because our @node->next pointer is NULL and
  162. * it will wait in Step-A.
  163. */
  164. WRITE_ONCE(next->prev, prev);
  165. WRITE_ONCE(prev->next, next);
  166. return false;
  167. }
  168. void osq_unlock(struct optimistic_spin_queue *lock)
  169. {
  170. struct optimistic_spin_node *node, *next;
  171. int curr = encode_cpu(smp_processor_id());
  172. /*
  173. * Fast path for the uncontended case.
  174. */
  175. if (likely(atomic_cmpxchg_release(&lock->tail, curr,
  176. OSQ_UNLOCKED_VAL) == curr))
  177. return;
  178. /*
  179. * Second most likely case.
  180. */
  181. node = this_cpu_ptr(&osq_node);
  182. next = xchg(&node->next, NULL);
  183. if (next) {
  184. WRITE_ONCE(next->locked, 1);
  185. return;
  186. }
  187. next = osq_wait_next(lock, node, NULL);
  188. if (next)
  189. WRITE_ONCE(next->locked, 1);
  190. }