spinlock.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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,
  111. unsigned int ctx)
  112. {
  113. assert (ctx < ARRAY_SIZE (cpu_data->qnodes));
  114. return (&cpu_data->qnodes[ctx]);
  115. }
  116. static uint32_t
  117. spinlock_qid_build (unsigned int ctx, unsigned int cpu)
  118. {
  119. assert (ctx <= SPINLOCK_QID_CTX_MASK);
  120. assert (cpu <= SPINLOCK_QID_CPU_MASK);
  121. return ((cpu << SPINLOCK_QID_CPU_SHIFT) | (ctx << SPINLOCK_QID_CTX_SHIFT));
  122. }
  123. static unsigned int
  124. spinlock_qid_ctx (uint32_t qid)
  125. {
  126. return ((qid >> SPINLOCK_QID_CTX_SHIFT) & SPINLOCK_QID_CTX_MASK);
  127. }
  128. static unsigned int
  129. spinlock_qid_cpu (uint32_t qid)
  130. {
  131. return ((qid >> SPINLOCK_QID_CPU_SHIFT) & SPINLOCK_QID_CPU_MASK);
  132. }
  133. void
  134. spinlock_init (struct spinlock *lock)
  135. {
  136. lock->value = SPINLOCK_UNLOCKED;
  137. #ifdef SPINLOCK_TRACK_OWNER
  138. lock->owner = NULL;
  139. #endif
  140. }
  141. static void
  142. spinlock_qnode_init (struct spinlock_qnode *qnode)
  143. {
  144. qnode->next = NULL;
  145. }
  146. static struct spinlock_qnode*
  147. spinlock_qnode_wait_next (const struct spinlock_qnode *qnode)
  148. {
  149. while (1)
  150. {
  151. _Auto next = atomic_load_acq (&qnode->next);
  152. if (next)
  153. return (next);
  154. cpu_pause ();
  155. }
  156. }
  157. static void
  158. spinlock_qnode_set_next (struct spinlock_qnode *qnode, 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 (1)
  172. {
  173. if (!atomic_load_acq (&qnode->locked))
  174. break;
  175. cpu_pause ();
  176. }
  177. }
  178. static void
  179. spinlock_qnode_clear_locked (struct spinlock_qnode *qnode)
  180. {
  181. atomic_store_rel (&qnode->locked, 0);
  182. }
  183. static void
  184. spinlock_get_local_qnode (struct spinlock_qnode **qnode, uint32_t *qid)
  185. {
  186. _Auto cpu_data = cpu_local_ptr (spinlock_cpu_data);
  187. unsigned int ctx = thread_interrupted () ?
  188. SPINLOCK_CTX_INTR : SPINLOCK_CTX_THREAD;
  189. *qnode = spinlock_cpu_data_get_qnode (cpu_data, ctx);
  190. *qid = spinlock_qid_build (ctx, cpu_id ());
  191. }
  192. static uint32_t
  193. spinlock_enqueue (struct spinlock *lock, uint32_t qid)
  194. {
  195. uint32_t next = (qid << SPINLOCK_QID_SHIFT) | SPINLOCK_CONTENDED;
  196. while (1)
  197. {
  198. uint32_t old_value = atomic_load_rlx (&lock->value);
  199. uint32_t new_value = next | (old_value & SPINLOCK_LOCKED);
  200. uint32_t prev = atomic_cas_rel (&lock->value, old_value, new_value);
  201. if (prev == old_value)
  202. return (prev);
  203. cpu_pause ();
  204. }
  205. }
  206. static struct spinlock_qnode*
  207. spinlock_get_remote_qnode (uint32_t qid)
  208. {
  209. // This fence synchronizes with queueing.
  210. atomic_fence_acq ();
  211. uint32_t ctx = spinlock_qid_ctx (qid),
  212. cpu = spinlock_qid_cpu (qid);
  213. _Auto cpu_data = percpu_ptr (spinlock_cpu_data, cpu);
  214. return (spinlock_cpu_data_get_qnode (cpu_data, ctx));
  215. }
  216. static void
  217. spinlock_set_locked (struct spinlock *lock)
  218. {
  219. atomic_or_rlx (&lock->value, SPINLOCK_LOCKED);
  220. }
  221. static void
  222. spinlock_wait_locked (const struct spinlock *lock)
  223. {
  224. while (1)
  225. {
  226. if (!(atomic_load_acq (&lock->value) & SPINLOCK_LOCKED))
  227. break;
  228. cpu_pause ();
  229. }
  230. }
  231. static int
  232. spinlock_downgrade (struct spinlock *lock, uint32_t qid)
  233. {
  234. uint32_t value = (qid << SPINLOCK_QID_SHIFT) | SPINLOCK_CONTENDED,
  235. prev = atomic_cas_rlx (&lock->value, value, SPINLOCK_LOCKED);
  236. assert (prev & SPINLOCK_CONTENDED);
  237. return (prev != value ? EBUSY : 0);
  238. }
  239. void
  240. spinlock_lock_slow (struct spinlock *lock)
  241. {
  242. uint32_t qid;
  243. struct spinlock_qnode *qnode;
  244. spinlock_get_local_qnode (&qnode, &qid);
  245. spinlock_qnode_init (qnode);
  246. uint32_t prev = spinlock_enqueue (lock, qid);
  247. if (prev & SPINLOCK_CONTENDED)
  248. {
  249. _Auto prev_qn = spinlock_get_remote_qnode (prev >> SPINLOCK_QID_SHIFT);
  250. spinlock_qnode_set_locked (qnode);
  251. spinlock_qnode_set_next (prev_qn, qnode);
  252. spinlock_qnode_wait_locked (qnode);
  253. }
  254. /*
  255. * If uncontended, the previous lock value could be used to check whether
  256. * the lock bit was also cleared, but this wait operation also enforces
  257. * acquire ordering.
  258. */
  259. spinlock_wait_locked (lock);
  260. spinlock_own (lock);
  261. int error = spinlock_downgrade (lock, qid);
  262. if (! error)
  263. return;
  264. spinlock_set_locked (lock);
  265. _Auto next_qnode = spinlock_qnode_wait_next (qnode);
  266. spinlock_qnode_clear_locked (next_qnode);
  267. }
  268. static int __init
  269. spinlock_setup (void)
  270. {
  271. return (0);
  272. }
  273. INIT_OP_DEFINE (spinlock_setup,
  274. INIT_OP_DEP (thread_setup_booter, true));