osq_lock.c 5.6 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. while (!READ_ONCE(node->locked)) {
  119. /*
  120. * If we need to reschedule bail... so we can block.
  121. * Use vcpu_is_preempted() to avoid waiting for a preempted
  122. * lock holder:
  123. */
  124. if (need_resched() || vcpu_is_preempted(node_cpu(node->prev)))
  125. goto unqueue;
  126. cpu_relax();
  127. }
  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. if (prev->next == node &&
  139. cmpxchg(&prev->next, node, NULL) == node)
  140. break;
  141. /*
  142. * We can only fail the cmpxchg() racing against an unlock(),
  143. * in which case we should observe @node->locked becomming
  144. * true.
  145. */
  146. if (smp_load_acquire(&node->locked))
  147. return true;
  148. cpu_relax();
  149. /*
  150. * Or we race against a concurrent unqueue()'s step-B, in which
  151. * case its step-C will write us a new @node->prev pointer.
  152. */
  153. prev = READ_ONCE(node->prev);
  154. }
  155. /*
  156. * Step - B -- stabilize @next
  157. *
  158. * Similar to unlock(), wait for @node->next or move @lock from @node
  159. * back to @prev.
  160. */
  161. next = osq_wait_next(lock, node, prev);
  162. if (!next)
  163. return false;
  164. /*
  165. * Step - C -- unlink
  166. *
  167. * @prev is stable because its still waiting for a new @prev->next
  168. * pointer, @next is stable because our @node->next pointer is NULL and
  169. * it will wait in Step-A.
  170. */
  171. WRITE_ONCE(next->prev, prev);
  172. WRITE_ONCE(prev->next, next);
  173. return false;
  174. }
  175. void osq_unlock(struct optimistic_spin_queue *lock)
  176. {
  177. struct optimistic_spin_node *node, *next;
  178. int curr = encode_cpu(smp_processor_id());
  179. /*
  180. * Fast path for the uncontended case.
  181. */
  182. if (likely(atomic_cmpxchg_release(&lock->tail, curr,
  183. OSQ_UNLOCKED_VAL) == curr))
  184. return;
  185. /*
  186. * Second most likely case.
  187. */
  188. node = this_cpu_ptr(&osq_node);
  189. next = xchg(&node->next, NULL);
  190. if (next) {
  191. WRITE_ONCE(next->locked, 1);
  192. return;
  193. }
  194. next = osq_wait_next(lock, node, NULL);
  195. if (next)
  196. WRITE_ONCE(next->locked, 1);
  197. }