osq_lock.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/percpu.h>
  3. #include <linux/sched.h>
  4. #include <linux/osq_lock.h>
  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 int node_cpu(struct optimistic_spin_node *node)
  23. {
  24. return node->cpu - 1;
  25. }
  26. static inline struct optimistic_spin_node *decode_cpu(int encoded_cpu_val)
  27. {
  28. int cpu_nr = encoded_cpu_val - 1;
  29. return per_cpu_ptr(&osq_node, cpu_nr);
  30. }
  31. /*
  32. * Get a stable @node->next pointer, either for unlock() or unqueue() purposes.
  33. * Can return NULL in case we were the last queued and we updated @lock instead.
  34. */
  35. static inline struct optimistic_spin_node *
  36. osq_wait_next(struct optimistic_spin_queue *lock,
  37. struct optimistic_spin_node *node,
  38. struct optimistic_spin_node *prev)
  39. {
  40. struct optimistic_spin_node *next = NULL;
  41. int curr = encode_cpu(smp_processor_id());
  42. int old;
  43. /*
  44. * If there is a prev node in queue, then the 'old' value will be
  45. * the prev node's CPU #, else it's set to OSQ_UNLOCKED_VAL since if
  46. * we're currently last in queue, then the queue will then become empty.
  47. */
  48. old = prev ? prev->cpu : OSQ_UNLOCKED_VAL;
  49. for (;;) {
  50. if (atomic_read(&lock->tail) == curr &&
  51. atomic_cmpxchg_acquire(&lock->tail, curr, old) == curr) {
  52. /*
  53. * We were the last queued, we moved @lock back. @prev
  54. * will now observe @lock and will complete its
  55. * unlock()/unqueue().
  56. */
  57. break;
  58. }
  59. /*
  60. * We must xchg() the @node->next value, because if we were to
  61. * leave it in, a concurrent unlock()/unqueue() from
  62. * @node->next might complete Step-A and think its @prev is
  63. * still valid.
  64. *
  65. * If the concurrent unlock()/unqueue() wins the race, we'll
  66. * wait for either @lock to point to us, through its Step-B, or
  67. * wait for a new @node->next from its Step-C.
  68. */
  69. if (node->next) {
  70. next = xchg(&node->next, NULL);
  71. if (next)
  72. break;
  73. }
  74. cpu_relax();
  75. }
  76. return next;
  77. }
  78. bool osq_lock(struct optimistic_spin_queue *lock)
  79. {
  80. struct optimistic_spin_node *node = this_cpu_ptr(&osq_node);
  81. struct optimistic_spin_node *prev, *next;
  82. int curr = encode_cpu(smp_processor_id());
  83. int old;
  84. node->locked = 0;
  85. node->next = NULL;
  86. node->cpu = curr;
  87. /*
  88. * We need both ACQUIRE (pairs with corresponding RELEASE in
  89. * unlock() uncontended, or fastpath) and RELEASE (to publish
  90. * the node fields we just initialised) semantics when updating
  91. * the lock tail.
  92. */
  93. old = atomic_xchg(&lock->tail, curr);
  94. if (old == OSQ_UNLOCKED_VAL)
  95. return true;
  96. prev = decode_cpu(old);
  97. node->prev = prev;
  98. /*
  99. * osq_lock() unqueue
  100. *
  101. * node->prev = prev osq_wait_next()
  102. * WMB MB
  103. * prev->next = node next->prev = prev // unqueue-C
  104. *
  105. * Here 'node->prev' and 'next->prev' are the same variable and we need
  106. * to ensure these stores happen in-order to avoid corrupting the list.
  107. */
  108. smp_wmb();
  109. WRITE_ONCE(prev->next, node);
  110. /*
  111. * Normally @prev is untouchable after the above store; because at that
  112. * moment unlock can proceed and wipe the node element from stack.
  113. *
  114. * However, since our nodes are static per-cpu storage, we're
  115. * guaranteed their existence -- this allows us to apply
  116. * cmpxchg in an attempt to undo our queueing.
  117. */
  118. /*
  119. * Wait to acquire the lock or cancelation. Note that need_resched()
  120. * will come with an IPI, which will wake smp_cond_load_relaxed() if it
  121. * is implemented with a monitor-wait. vcpu_is_preempted() relies on
  122. * polling, be careful.
  123. */
  124. if (smp_cond_load_relaxed(&node->locked, VAL || need_resched() ||
  125. vcpu_is_preempted(node_cpu(node->prev))))
  126. return true;
  127. /* unqueue */
  128. /*
  129. * Step - A -- stabilize @prev
  130. *
  131. * Undo our @prev->next assignment; this will make @prev's
  132. * unlock()/unqueue() wait for a next pointer since @lock points to us
  133. * (or later).
  134. */
  135. for (;;) {
  136. /*
  137. * cpu_relax() below implies a compiler barrier which would
  138. * prevent this comparison being optimized away.
  139. */
  140. if (data_race(prev->next) == node &&
  141. cmpxchg(&prev->next, node, NULL) == node)
  142. break;
  143. /*
  144. * We can only fail the cmpxchg() racing against an unlock(),
  145. * in which case we should observe @node->locked becomming
  146. * true.
  147. */
  148. if (smp_load_acquire(&node->locked))
  149. return true;
  150. cpu_relax();
  151. /*
  152. * Or we race against a concurrent unqueue()'s step-B, in which
  153. * case its step-C will write us a new @node->prev pointer.
  154. */
  155. prev = READ_ONCE(node->prev);
  156. }
  157. /*
  158. * Step - B -- stabilize @next
  159. *
  160. * Similar to unlock(), wait for @node->next or move @lock from @node
  161. * back to @prev.
  162. */
  163. next = osq_wait_next(lock, node, prev);
  164. if (!next)
  165. return false;
  166. /*
  167. * Step - C -- unlink
  168. *
  169. * @prev is stable because its still waiting for a new @prev->next
  170. * pointer, @next is stable because our @node->next pointer is NULL and
  171. * it will wait in Step-A.
  172. */
  173. WRITE_ONCE(next->prev, prev);
  174. WRITE_ONCE(prev->next, next);
  175. return false;
  176. }
  177. void osq_unlock(struct optimistic_spin_queue *lock)
  178. {
  179. struct optimistic_spin_node *node, *next;
  180. int curr = encode_cpu(smp_processor_id());
  181. /*
  182. * Fast path for the uncontended case.
  183. */
  184. if (likely(atomic_cmpxchg_release(&lock->tail, curr,
  185. OSQ_UNLOCKED_VAL) == curr))
  186. return;
  187. /*
  188. * Second most likely case.
  189. */
  190. node = this_cpu_ptr(&osq_node);
  191. next = xchg(&node->next, NULL);
  192. if (next) {
  193. WRITE_ONCE(next->locked, 1);
  194. return;
  195. }
  196. next = osq_wait_next(lock, node, NULL);
  197. if (next)
  198. WRITE_ONCE(next->locked, 1);
  199. }