osq_lock.c 5.9 KB

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