spinlock.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Copyright (c) 2017-2018 Richard Braun.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. *
  18. * This implementation is based on the paper "Algorithms for Scalable
  19. * Synchronization on Shared-Memory Multiprocessors" by John M. Mellor-Crummey
  20. * and Michael L. Scott, which describes MCS locks, among other algorithms.
  21. *
  22. * Here are additional issues this module solves that require modifications
  23. * to the original MCS algorithm :
  24. * - There must not be any limit on the number of spin locks a thread may
  25. * hold, and spinlocks must not need dynamic memory allocation.
  26. * - Unlocking a spin lock must be a non-blocking operation. Without
  27. * this requirement, a locking operation may be interrupted in the
  28. * middle of a hand-off sequence, preventing the unlock operation
  29. * from completing, potentially causing tricky deadlocks.
  30. * - Spin lock storage must not exceed 32 bits.
  31. *
  32. * In order to solve these issues, the lock owner is never part of the
  33. * lock queue. This makes it possible to use a qnode only during the lock
  34. * operation, not after. This means a single qnode per execution context
  35. * is required even when holding multiple spin locks simultaneously.
  36. *
  37. * In addition, instead of making the owner perform a hand-off sequence
  38. * to unblock the first waiter when unlocking, the latter directly spins
  39. * on the lock word, and is the one performing the hand-off sequence with
  40. * the second waiter. As a side effect, this also optimizes spinning for
  41. * the common case of a single waiter.
  42. *
  43. * When a lock is held, the lock bit is set, and when a lock is contended
  44. * the contended bit is set. When contended, the lock word also contains
  45. * a compressed reference to the last waiter. That reference is called a
  46. * QID (for qnode ID). It is structured into two parts :
  47. * - the execution context
  48. * - the CPU ID
  49. *
  50. * The QID is used to uniquely identify a statically allocated qnode.
  51. *
  52. * The lock operation must make sure that the lock value is restored
  53. * to SPINLOCK_LOCKED if there is no more contention, an operation
  54. * called downgrading.
  55. */
  56. #include <assert.h>
  57. #include <errno.h>
  58. #include <limits.h>
  59. #include <stdalign.h>
  60. #include <stddef.h>
  61. #include <stdint.h>
  62. #include <kern/atomic.h>
  63. #include <kern/init.h>
  64. #include <kern/macros.h>
  65. #include <kern/percpu.h>
  66. #include <kern/spinlock.h>
  67. #include <kern/spinlock_types.h>
  68. #include <kern/thread.h>
  69. #include <machine/cpu.h>
  70. #define SPINLOCK_CONTENDED 0x2
  71. #define SPINLOCK_LOCKED_BITS 1
  72. #define SPINLOCK_CONTENDED_BITS 1
  73. #define SPINLOCK_QID_SHIFT \
  74. (SPINLOCK_CONTENDED_BITS + SPINLOCK_LOCKED_BITS)
  75. #define SPINLOCK_QID_CTX_BITS 1
  76. #define SPINLOCK_QID_CTX_SHIFT 0
  77. #define SPINLOCK_QID_CTX_MASK ((1U << SPINLOCK_QID_CTX_BITS) - 1)
  78. #define SPINLOCK_QID_CPU_BITS 29
  79. #define SPINLOCK_QID_CPU_SHIFT \
  80. (SPINLOCK_QID_CTX_SHIFT + SPINLOCK_QID_CTX_BITS)
  81. #define SPINLOCK_QID_CPU_MASK ((1U << SPINLOCK_QID_CPU_BITS) - 1)
  82. #define SPINLOCK_BITS \
  83. (SPINLOCK_QID_CPU_BITS + SPINLOCK_QID_CTX_BITS + \
  84. SPINLOCK_CONTENDED_BITS + SPINLOCK_LOCKED_BITS)
  85. #if CONFIG_MAX_CPUS > (1U << SPINLOCK_QID_CPU_BITS)
  86. #error "maximum number of supported processors too large"
  87. #endif
  88. static_assert (SPINLOCK_BITS <= CHAR_BIT * sizeof (uint32_t),
  89. "spinlock too large");
  90. struct spinlock_qnode
  91. {
  92. __cacheline_aligned struct spinlock_qnode *next;
  93. int locked;
  94. };
  95. // TODO NMI support.
  96. enum
  97. {
  98. SPINLOCK_CTX_THREAD,
  99. SPINLOCK_CTX_INTR,
  100. SPINLOCK_NR_CTXS
  101. };
  102. static_assert (SPINLOCK_NR_CTXS <= (SPINLOCK_QID_CTX_MASK + 1),
  103. "maximum number of contexts too large");
  104. struct spinlock_cpu_data
  105. {
  106. struct spinlock_qnode qnodes[SPINLOCK_NR_CTXS];
  107. };
  108. static struct spinlock_cpu_data spinlock_cpu_data __percpu;
  109. static struct spinlock_qnode*
  110. spinlock_cpu_data_get_qnode (struct spinlock_cpu_data *cpu_data, uint32_t ctx)
  111. {
  112. assert (ctx < ARRAY_SIZE (cpu_data->qnodes));
  113. return (&cpu_data->qnodes[ctx]);
  114. }
  115. static uint32_t
  116. spinlock_qid_build (uint32_t ctx, uint32_t cpu)
  117. {
  118. assert (ctx <= SPINLOCK_QID_CTX_MASK);
  119. assert (cpu <= SPINLOCK_QID_CPU_MASK);
  120. return ((cpu << SPINLOCK_QID_CPU_SHIFT) | (ctx << SPINLOCK_QID_CTX_SHIFT));
  121. }
  122. static uint32_t
  123. spinlock_qid_ctx (uint32_t qid)
  124. {
  125. return ((qid >> SPINLOCK_QID_CTX_SHIFT) & SPINLOCK_QID_CTX_MASK);
  126. }
  127. static uint32_t
  128. spinlock_qid_cpu (uint32_t qid)
  129. {
  130. return ((qid >> SPINLOCK_QID_CPU_SHIFT) & SPINLOCK_QID_CPU_MASK);
  131. }
  132. void
  133. spinlock_init (struct spinlock *lock)
  134. {
  135. lock->value = SPINLOCK_UNLOCKED;
  136. #ifdef SPINLOCK_TRACK_OWNER
  137. lock->owner = NULL;
  138. #endif
  139. }
  140. static void
  141. spinlock_qnode_init (struct spinlock_qnode *qnode)
  142. {
  143. qnode->next = NULL;
  144. }
  145. static struct spinlock_qnode*
  146. spinlock_qnode_wait_next (const struct spinlock_qnode *qnode)
  147. {
  148. while (1)
  149. {
  150. _Auto next = atomic_load_acq (&qnode->next);
  151. if (next)
  152. return (next);
  153. cpu_pause ();
  154. }
  155. }
  156. static void
  157. spinlock_qnode_set_next (struct spinlock_qnode *qnode,
  158. struct spinlock_qnode *next)
  159. {
  160. assert (next);
  161. atomic_store_rel (&qnode->next, next);
  162. }
  163. static void
  164. spinlock_qnode_set_locked (struct spinlock_qnode *qnode)
  165. {
  166. qnode->locked = 1;
  167. }
  168. static void
  169. spinlock_qnode_wait_locked (const struct spinlock_qnode *qnode)
  170. {
  171. while (atomic_load_acq (&qnode->locked))
  172. cpu_pause ();
  173. }
  174. static void
  175. spinlock_qnode_clear_locked (struct spinlock_qnode *qnode)
  176. {
  177. atomic_store_rel (&qnode->locked, 0);
  178. }
  179. static void
  180. spinlock_get_local_qnode (struct spinlock_qnode **qnode, uint32_t *qid)
  181. {
  182. _Auto cpu_data = cpu_local_ptr (spinlock_cpu_data);
  183. uint32_t ctx = thread_interrupted () ?
  184. SPINLOCK_CTX_INTR : SPINLOCK_CTX_THREAD;
  185. *qnode = spinlock_cpu_data_get_qnode (cpu_data, ctx);
  186. *qid = spinlock_qid_build (ctx, cpu_id ());
  187. }
  188. static uint32_t
  189. spinlock_enqueue (struct spinlock *lock, uint32_t qid)
  190. {
  191. uint32_t next = (qid << SPINLOCK_QID_SHIFT) | SPINLOCK_CONTENDED;
  192. while (1)
  193. {
  194. uint32_t old_value = atomic_load_rlx (&lock->value);
  195. uint32_t new_value = next | (old_value & SPINLOCK_LOCKED);
  196. uint32_t prev = atomic_cas_rel (&lock->value, old_value, new_value);
  197. if (prev == old_value)
  198. return (prev);
  199. cpu_pause ();
  200. }
  201. }
  202. static struct spinlock_qnode*
  203. spinlock_get_remote_qnode (uint32_t qid)
  204. {
  205. // This fence synchronizes with queueing.
  206. atomic_fence_acq ();
  207. uint32_t ctx = spinlock_qid_ctx (qid),
  208. cpu = spinlock_qid_cpu (qid);
  209. _Auto cpu_data = percpu_ptr (spinlock_cpu_data, cpu);
  210. return (spinlock_cpu_data_get_qnode (cpu_data, ctx));
  211. }
  212. static void
  213. spinlock_set_locked (struct spinlock *lock)
  214. {
  215. atomic_or_rlx (&lock->value, SPINLOCK_LOCKED);
  216. }
  217. static void
  218. spinlock_wait_locked (const struct spinlock *lock)
  219. {
  220. while (atomic_load_acq (&lock->value) & SPINLOCK_LOCKED)
  221. cpu_pause ();
  222. }
  223. static int
  224. spinlock_downgrade (struct spinlock *lock, uint32_t qid)
  225. {
  226. uint32_t value = (qid << SPINLOCK_QID_SHIFT) | SPINLOCK_CONTENDED,
  227. prev = atomic_cas_rlx (&lock->value, value, SPINLOCK_LOCKED);
  228. assert (prev & SPINLOCK_CONTENDED);
  229. return (prev != value ? EBUSY : 0);
  230. }
  231. void
  232. spinlock_lock_slow (struct spinlock *lock)
  233. {
  234. uint32_t qid;
  235. struct spinlock_qnode *qnode;
  236. spinlock_get_local_qnode (&qnode, &qid);
  237. spinlock_qnode_init (qnode);
  238. uint32_t prev = spinlock_enqueue (lock, qid);
  239. if (prev & SPINLOCK_CONTENDED)
  240. {
  241. _Auto prev_qn = spinlock_get_remote_qnode (prev >> SPINLOCK_QID_SHIFT);
  242. spinlock_qnode_set_locked (qnode);
  243. spinlock_qnode_set_next (prev_qn, qnode);
  244. spinlock_qnode_wait_locked (qnode);
  245. }
  246. /*
  247. * If uncontended, the previous lock value could be used to check whether
  248. * the lock bit was also cleared, but this wait operation also enforces
  249. * acquire ordering.
  250. */
  251. spinlock_wait_locked (lock);
  252. spinlock_own (lock);
  253. int error = spinlock_downgrade (lock, qid);
  254. if (! error)
  255. return;
  256. spinlock_set_locked (lock);
  257. _Auto next_qnode = spinlock_qnode_wait_next (qnode);
  258. spinlock_qnode_clear_locked (next_qnode);
  259. }
  260. static int __init
  261. spinlock_setup (void)
  262. {
  263. return (0);
  264. }
  265. INIT_OP_DEFINE (spinlock_setup,
  266. INIT_OP_DEP (thread_setup_booter, true));