osq_lock.c 5.3 KB

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