osq_lock.c 5.0 KB

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