spinlock.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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_i.h>
  68. #include <kern/spinlock_types.h>
  69. #include <kern/thread.h>
  70. #include <machine/cpu.h>
  71. #define SPINLOCK_CONTENDED 0x2
  72. #define SPINLOCK_LOCKED_BITS 1
  73. #define SPINLOCK_CONTENDED_BITS 1
  74. #define SPINLOCK_QID_SHIFT (SPINLOCK_CONTENDED_BITS \
  75. + SPINLOCK_LOCKED_BITS)
  76. #define SPINLOCK_QID_CTX_BITS 1
  77. #define SPINLOCK_QID_CTX_SHIFT 0
  78. #define SPINLOCK_QID_CTX_MASK ((1U << SPINLOCK_QID_CTX_BITS) - 1)
  79. #define SPINLOCK_QID_CPU_BITS 29
  80. #define SPINLOCK_QID_CPU_SHIFT (SPINLOCK_QID_CTX_SHIFT \
  81. + SPINLOCK_QID_CTX_BITS)
  82. #define SPINLOCK_QID_CPU_MASK ((1U << SPINLOCK_QID_CPU_BITS) - 1)
  83. #define SPINLOCK_BITS (SPINLOCK_QID_CPU_BITS \
  84. + SPINLOCK_QID_CTX_BITS \
  85. + SPINLOCK_CONTENDED_BITS \
  86. + SPINLOCK_LOCKED_BITS)
  87. #if CONFIG_MAX_CPUS > (1U << SPINLOCK_QID_CPU_BITS)
  88. #error "maximum number of supported processors too large"
  89. #endif
  90. static_assert(SPINLOCK_BITS <= (CHAR_BIT * sizeof(uint32_t)),
  91. "spinlock too large");
  92. struct spinlock_qnode {
  93. alignas(CPU_L1_SIZE) struct spinlock_qnode *next;
  94. int locked;
  95. };
  96. /* TODO NMI support */
  97. enum {
  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. struct spinlock_qnode qnodes[SPINLOCK_NR_CTXS];
  106. };
  107. static struct spinlock_cpu_data spinlock_cpu_data __percpu;
  108. static struct spinlock_qnode *
  109. spinlock_cpu_data_get_qnode(struct spinlock_cpu_data *cpu_data,
  110. unsigned int 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(unsigned int ctx, unsigned int 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 unsigned int
  123. spinlock_qid_ctx(uint32_t qid)
  124. {
  125. return (qid >> SPINLOCK_QID_CTX_SHIFT) & SPINLOCK_QID_CTX_MASK;
  126. }
  127. static unsigned int
  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 /* SPINLOCK_TRACK_OWNER */
  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. struct spinlock_qnode *next;
  149. for (;;) {
  150. next = atomic_load(&qnode->next, ATOMIC_ACQUIRE);
  151. if (next) {
  152. break;
  153. }
  154. cpu_pause();
  155. }
  156. return next;
  157. }
  158. static void
  159. spinlock_qnode_set_next(struct spinlock_qnode *qnode, struct spinlock_qnode *next)
  160. {
  161. assert(next);
  162. atomic_store(&qnode->next, next, ATOMIC_RELEASE);
  163. }
  164. static void
  165. spinlock_qnode_set_locked(struct spinlock_qnode *qnode)
  166. {
  167. qnode->locked = 1;
  168. }
  169. static void
  170. spinlock_qnode_wait_locked(const struct spinlock_qnode *qnode)
  171. {
  172. int locked;
  173. for (;;) {
  174. locked = atomic_load(&qnode->locked, ATOMIC_ACQUIRE);
  175. if (!locked) {
  176. break;
  177. }
  178. cpu_pause();
  179. }
  180. }
  181. static void
  182. spinlock_qnode_clear_locked(struct spinlock_qnode *qnode)
  183. {
  184. atomic_store(&qnode->locked, 0, ATOMIC_RELEASE);
  185. }
  186. static void
  187. spinlock_get_local_qnode(struct spinlock_qnode **qnode, uint32_t *qid)
  188. {
  189. struct spinlock_cpu_data *cpu_data;
  190. unsigned int ctx;
  191. cpu_data = cpu_local_ptr(spinlock_cpu_data);
  192. ctx = thread_interrupted() ? SPINLOCK_CTX_INTR : SPINLOCK_CTX_THREAD;
  193. *qnode = spinlock_cpu_data_get_qnode(cpu_data, ctx);
  194. *qid = spinlock_qid_build(ctx, cpu_id());
  195. }
  196. static uint32_t
  197. spinlock_enqueue(struct spinlock *lock, uint32_t qid)
  198. {
  199. uint32_t old_value, new_value, prev, next;
  200. next = (qid << SPINLOCK_QID_SHIFT) | SPINLOCK_CONTENDED;
  201. for (;;) {
  202. old_value = atomic_load(&lock->value, ATOMIC_RELAXED);
  203. new_value = next | (old_value & SPINLOCK_LOCKED);
  204. prev = atomic_cas(&lock->value, old_value, new_value, ATOMIC_RELEASE);
  205. if (prev == old_value) {
  206. break;
  207. }
  208. cpu_pause();
  209. }
  210. return prev;
  211. }
  212. static struct spinlock_qnode *
  213. spinlock_get_remote_qnode(uint32_t qid)
  214. {
  215. struct spinlock_cpu_data *cpu_data;
  216. unsigned int ctx, cpu;
  217. /* This fence synchronizes with queueing */
  218. atomic_fence(ATOMIC_ACQUIRE);
  219. ctx = spinlock_qid_ctx(qid);
  220. cpu = spinlock_qid_cpu(qid);
  221. cpu_data = percpu_ptr(spinlock_cpu_data, cpu);
  222. return spinlock_cpu_data_get_qnode(cpu_data, ctx);
  223. }
  224. static void
  225. spinlock_set_locked(struct spinlock *lock)
  226. {
  227. atomic_or(&lock->value, SPINLOCK_LOCKED, ATOMIC_RELAXED);
  228. }
  229. static void
  230. spinlock_wait_locked(const struct spinlock *lock)
  231. {
  232. uint32_t value;
  233. for (;;) {
  234. value = atomic_load(&lock->value, ATOMIC_ACQUIRE);
  235. if (!(value & SPINLOCK_LOCKED)) {
  236. break;
  237. }
  238. cpu_pause();
  239. }
  240. }
  241. static int
  242. spinlock_downgrade(struct spinlock *lock, uint32_t qid)
  243. {
  244. uint32_t value, prev;
  245. value = (qid << SPINLOCK_QID_SHIFT) | SPINLOCK_CONTENDED;
  246. prev = atomic_cas(&lock->value, value, SPINLOCK_LOCKED, ATOMIC_RELAXED);
  247. assert(prev & SPINLOCK_CONTENDED);
  248. if (prev != value) {
  249. return EBUSY;
  250. }
  251. return 0;
  252. }
  253. void
  254. spinlock_lock_slow(struct spinlock *lock)
  255. {
  256. struct spinlock_qnode *qnode, *prev_qnode, *next_qnode;
  257. uint32_t prev, qid;
  258. int error;
  259. spinlock_get_local_qnode(&qnode, &qid);
  260. spinlock_qnode_init(qnode);
  261. prev = spinlock_enqueue(lock, qid);
  262. if (prev & SPINLOCK_CONTENDED) {
  263. prev_qnode = spinlock_get_remote_qnode(prev >> SPINLOCK_QID_SHIFT);
  264. spinlock_qnode_set_locked(qnode);
  265. spinlock_qnode_set_next(prev_qnode, qnode);
  266. spinlock_qnode_wait_locked(qnode);
  267. }
  268. /*
  269. * If uncontended, the previous lock value could be used to check whether
  270. * the lock bit was also cleared, but this wait operation also enforces
  271. * acquire ordering.
  272. */
  273. spinlock_wait_locked(lock);
  274. spinlock_own(lock);
  275. error = spinlock_downgrade(lock, qid);
  276. if (!error) {
  277. return;
  278. }
  279. spinlock_set_locked(lock);
  280. next_qnode = spinlock_qnode_wait_next(qnode);
  281. spinlock_qnode_clear_locked(next_qnode);
  282. }
  283. static int __init
  284. spinlock_setup(void)
  285. {
  286. return 0;
  287. }
  288. INIT_OP_DEFINE(spinlock_setup,
  289. INIT_OP_DEP(thread_setup_booter, true));