qspinlock_paravirt.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. #ifndef _GEN_PV_LOCK_SLOWPATH
  2. #error "do not include this file"
  3. #endif
  4. #include <linux/hash.h>
  5. #include <linux/bootmem.h>
  6. #include <linux/debug_locks.h>
  7. /*
  8. * Implement paravirt qspinlocks; the general idea is to halt the vcpus instead
  9. * of spinning them.
  10. *
  11. * This relies on the architecture to provide two paravirt hypercalls:
  12. *
  13. * pv_wait(u8 *ptr, u8 val) -- suspends the vcpu if *ptr == val
  14. * pv_kick(cpu) -- wakes a suspended vcpu
  15. *
  16. * Using these we implement __pv_queued_spin_lock_slowpath() and
  17. * __pv_queued_spin_unlock() to replace native_queued_spin_lock_slowpath() and
  18. * native_queued_spin_unlock().
  19. */
  20. #define _Q_SLOW_VAL (3U << _Q_LOCKED_OFFSET)
  21. /*
  22. * Queue node uses: vcpu_running & vcpu_halted.
  23. * Queue head uses: vcpu_running & vcpu_hashed.
  24. */
  25. enum vcpu_state {
  26. vcpu_running = 0,
  27. vcpu_halted, /* Used only in pv_wait_node */
  28. vcpu_hashed, /* = pv_hash'ed + vcpu_halted */
  29. };
  30. struct pv_node {
  31. struct mcs_spinlock mcs;
  32. struct mcs_spinlock __res[3];
  33. int cpu;
  34. u8 state;
  35. };
  36. /*
  37. * Lock and MCS node addresses hash table for fast lookup
  38. *
  39. * Hashing is done on a per-cacheline basis to minimize the need to access
  40. * more than one cacheline.
  41. *
  42. * Dynamically allocate a hash table big enough to hold at least 4X the
  43. * number of possible cpus in the system. Allocation is done on page
  44. * granularity. So the minimum number of hash buckets should be at least
  45. * 256 (64-bit) or 512 (32-bit) to fully utilize a 4k page.
  46. *
  47. * Since we should not be holding locks from NMI context (very rare indeed) the
  48. * max load factor is 0.75, which is around the point where open addressing
  49. * breaks down.
  50. *
  51. */
  52. struct pv_hash_entry {
  53. struct qspinlock *lock;
  54. struct pv_node *node;
  55. };
  56. #define PV_HE_PER_LINE (SMP_CACHE_BYTES / sizeof(struct pv_hash_entry))
  57. #define PV_HE_MIN (PAGE_SIZE / sizeof(struct pv_hash_entry))
  58. static struct pv_hash_entry *pv_lock_hash;
  59. static unsigned int pv_lock_hash_bits __read_mostly;
  60. /*
  61. * Allocate memory for the PV qspinlock hash buckets
  62. *
  63. * This function should be called from the paravirt spinlock initialization
  64. * routine.
  65. */
  66. void __init __pv_init_lock_hash(void)
  67. {
  68. int pv_hash_size = ALIGN(4 * num_possible_cpus(), PV_HE_PER_LINE);
  69. if (pv_hash_size < PV_HE_MIN)
  70. pv_hash_size = PV_HE_MIN;
  71. /*
  72. * Allocate space from bootmem which should be page-size aligned
  73. * and hence cacheline aligned.
  74. */
  75. pv_lock_hash = alloc_large_system_hash("PV qspinlock",
  76. sizeof(struct pv_hash_entry),
  77. pv_hash_size, 0, HASH_EARLY,
  78. &pv_lock_hash_bits, NULL,
  79. pv_hash_size, pv_hash_size);
  80. }
  81. #define for_each_hash_entry(he, offset, hash) \
  82. for (hash &= ~(PV_HE_PER_LINE - 1), he = &pv_lock_hash[hash], offset = 0; \
  83. offset < (1 << pv_lock_hash_bits); \
  84. offset++, he = &pv_lock_hash[(hash + offset) & ((1 << pv_lock_hash_bits) - 1)])
  85. static struct qspinlock **pv_hash(struct qspinlock *lock, struct pv_node *node)
  86. {
  87. unsigned long offset, hash = hash_ptr(lock, pv_lock_hash_bits);
  88. struct pv_hash_entry *he;
  89. for_each_hash_entry(he, offset, hash) {
  90. if (!cmpxchg(&he->lock, NULL, lock)) {
  91. WRITE_ONCE(he->node, node);
  92. return &he->lock;
  93. }
  94. }
  95. /*
  96. * Hard assume there is a free entry for us.
  97. *
  98. * This is guaranteed by ensuring every blocked lock only ever consumes
  99. * a single entry, and since we only have 4 nesting levels per CPU
  100. * and allocated 4*nr_possible_cpus(), this must be so.
  101. *
  102. * The single entry is guaranteed by having the lock owner unhash
  103. * before it releases.
  104. */
  105. BUG();
  106. }
  107. static struct pv_node *pv_unhash(struct qspinlock *lock)
  108. {
  109. unsigned long offset, hash = hash_ptr(lock, pv_lock_hash_bits);
  110. struct pv_hash_entry *he;
  111. struct pv_node *node;
  112. for_each_hash_entry(he, offset, hash) {
  113. if (READ_ONCE(he->lock) == lock) {
  114. node = READ_ONCE(he->node);
  115. WRITE_ONCE(he->lock, NULL);
  116. return node;
  117. }
  118. }
  119. /*
  120. * Hard assume we'll find an entry.
  121. *
  122. * This guarantees a limited lookup time and is itself guaranteed by
  123. * having the lock owner do the unhash -- IFF the unlock sees the
  124. * SLOW flag, there MUST be a hash entry.
  125. */
  126. BUG();
  127. }
  128. /*
  129. * Initialize the PV part of the mcs_spinlock node.
  130. */
  131. static void pv_init_node(struct mcs_spinlock *node)
  132. {
  133. struct pv_node *pn = (struct pv_node *)node;
  134. BUILD_BUG_ON(sizeof(struct pv_node) > 5*sizeof(struct mcs_spinlock));
  135. pn->cpu = smp_processor_id();
  136. pn->state = vcpu_running;
  137. }
  138. /*
  139. * Wait for node->locked to become true, halt the vcpu after a short spin.
  140. * pv_kick_node() is used to set _Q_SLOW_VAL and fill in hash table on its
  141. * behalf.
  142. */
  143. static void pv_wait_node(struct mcs_spinlock *node)
  144. {
  145. struct pv_node *pn = (struct pv_node *)node;
  146. int loop;
  147. for (;;) {
  148. for (loop = SPIN_THRESHOLD; loop; loop--) {
  149. if (READ_ONCE(node->locked))
  150. return;
  151. cpu_relax();
  152. }
  153. /*
  154. * Order pn->state vs pn->locked thusly:
  155. *
  156. * [S] pn->state = vcpu_halted [S] next->locked = 1
  157. * MB MB
  158. * [L] pn->locked [RmW] pn->state = vcpu_hashed
  159. *
  160. * Matches the cmpxchg() from pv_kick_node().
  161. */
  162. smp_store_mb(pn->state, vcpu_halted);
  163. if (!READ_ONCE(node->locked))
  164. pv_wait(&pn->state, vcpu_halted);
  165. /*
  166. * If pv_kick_node() changed us to vcpu_hashed, retain that value
  167. * so that pv_wait_head() knows to not also try to hash this lock.
  168. */
  169. cmpxchg(&pn->state, vcpu_halted, vcpu_running);
  170. /*
  171. * If the locked flag is still not set after wakeup, it is a
  172. * spurious wakeup and the vCPU should wait again. However,
  173. * there is a pretty high overhead for CPU halting and kicking.
  174. * So it is better to spin for a while in the hope that the
  175. * MCS lock will be released soon.
  176. */
  177. }
  178. /*
  179. * By now our node->locked should be 1 and our caller will not actually
  180. * spin-wait for it. We do however rely on our caller to do a
  181. * load-acquire for us.
  182. */
  183. }
  184. /*
  185. * Called after setting next->locked = 1 when we're the lock owner.
  186. *
  187. * Instead of waking the waiters stuck in pv_wait_node() advance their state such
  188. * that they're waiting in pv_wait_head(), this avoids a wake/sleep cycle.
  189. */
  190. static void pv_kick_node(struct qspinlock *lock, struct mcs_spinlock *node)
  191. {
  192. struct pv_node *pn = (struct pv_node *)node;
  193. struct __qspinlock *l = (void *)lock;
  194. /*
  195. * If the vCPU is indeed halted, advance its state to match that of
  196. * pv_wait_node(). If OTOH this fails, the vCPU was running and will
  197. * observe its next->locked value and advance itself.
  198. *
  199. * Matches with smp_store_mb() and cmpxchg() in pv_wait_node()
  200. */
  201. if (cmpxchg(&pn->state, vcpu_halted, vcpu_hashed) != vcpu_halted)
  202. return;
  203. /*
  204. * Put the lock into the hash table and set the _Q_SLOW_VAL.
  205. *
  206. * As this is the same vCPU that will check the _Q_SLOW_VAL value and
  207. * the hash table later on at unlock time, no atomic instruction is
  208. * needed.
  209. */
  210. WRITE_ONCE(l->locked, _Q_SLOW_VAL);
  211. (void)pv_hash(lock, pn);
  212. }
  213. /*
  214. * Wait for l->locked to become clear; halt the vcpu after a short spin.
  215. * __pv_queued_spin_unlock() will wake us.
  216. */
  217. static void pv_wait_head(struct qspinlock *lock, struct mcs_spinlock *node)
  218. {
  219. struct pv_node *pn = (struct pv_node *)node;
  220. struct __qspinlock *l = (void *)lock;
  221. struct qspinlock **lp = NULL;
  222. int loop;
  223. /*
  224. * If pv_kick_node() already advanced our state, we don't need to
  225. * insert ourselves into the hash table anymore.
  226. */
  227. if (READ_ONCE(pn->state) == vcpu_hashed)
  228. lp = (struct qspinlock **)1;
  229. for (;;) {
  230. for (loop = SPIN_THRESHOLD; loop; loop--) {
  231. if (!READ_ONCE(l->locked))
  232. return;
  233. cpu_relax();
  234. }
  235. if (!lp) { /* ONCE */
  236. lp = pv_hash(lock, pn);
  237. /*
  238. * We must hash before setting _Q_SLOW_VAL, such that
  239. * when we observe _Q_SLOW_VAL in __pv_queued_spin_unlock()
  240. * we'll be sure to be able to observe our hash entry.
  241. *
  242. * [S] <hash> [Rmw] l->locked == _Q_SLOW_VAL
  243. * MB RMB
  244. * [RmW] l->locked = _Q_SLOW_VAL [L] <unhash>
  245. *
  246. * Matches the smp_rmb() in __pv_queued_spin_unlock().
  247. */
  248. if (!cmpxchg(&l->locked, _Q_LOCKED_VAL, _Q_SLOW_VAL)) {
  249. /*
  250. * The lock is free and _Q_SLOW_VAL has never
  251. * been set. Therefore we need to unhash before
  252. * getting the lock.
  253. */
  254. WRITE_ONCE(*lp, NULL);
  255. return;
  256. }
  257. }
  258. pv_wait(&l->locked, _Q_SLOW_VAL);
  259. /*
  260. * The unlocker should have freed the lock before kicking the
  261. * CPU. So if the lock is still not free, it is a spurious
  262. * wakeup and so the vCPU should wait again after spinning for
  263. * a while.
  264. */
  265. }
  266. /*
  267. * Lock is unlocked now; the caller will acquire it without waiting.
  268. * As with pv_wait_node() we rely on the caller to do a load-acquire
  269. * for us.
  270. */
  271. }
  272. /*
  273. * PV version of the unlock function to be used in stead of
  274. * queued_spin_unlock().
  275. */
  276. __visible void __pv_queued_spin_unlock(struct qspinlock *lock)
  277. {
  278. struct __qspinlock *l = (void *)lock;
  279. struct pv_node *node;
  280. u8 locked;
  281. /*
  282. * We must not unlock if SLOW, because in that case we must first
  283. * unhash. Otherwise it would be possible to have multiple @lock
  284. * entries, which would be BAD.
  285. */
  286. locked = cmpxchg(&l->locked, _Q_LOCKED_VAL, 0);
  287. if (likely(locked == _Q_LOCKED_VAL))
  288. return;
  289. if (unlikely(locked != _Q_SLOW_VAL)) {
  290. WARN(!debug_locks_silent,
  291. "pvqspinlock: lock 0x%lx has corrupted value 0x%x!\n",
  292. (unsigned long)lock, atomic_read(&lock->val));
  293. return;
  294. }
  295. /*
  296. * A failed cmpxchg doesn't provide any memory-ordering guarantees,
  297. * so we need a barrier to order the read of the node data in
  298. * pv_unhash *after* we've read the lock being _Q_SLOW_VAL.
  299. *
  300. * Matches the cmpxchg() in pv_wait_head() setting _Q_SLOW_VAL.
  301. */
  302. smp_rmb();
  303. /*
  304. * Since the above failed to release, this must be the SLOW path.
  305. * Therefore start by looking up the blocked node and unhashing it.
  306. */
  307. node = pv_unhash(lock);
  308. /*
  309. * Now that we have a reference to the (likely) blocked pv_node,
  310. * release the lock.
  311. */
  312. smp_store_release(&l->locked, 0);
  313. /*
  314. * At this point the memory pointed at by lock can be freed/reused,
  315. * however we can still use the pv_node to kick the CPU.
  316. * The other vCPU may not really be halted, but kicking an active
  317. * vCPU is harmless other than the additional latency in completing
  318. * the unlock.
  319. */
  320. pv_kick(node->cpu);
  321. }
  322. /*
  323. * Include the architecture specific callee-save thunk of the
  324. * __pv_queued_spin_unlock(). This thunk is put together with
  325. * __pv_queued_spin_unlock() near the top of the file to make sure
  326. * that the callee-save thunk and the real unlock function are close
  327. * to each other sharing consecutive instruction cachelines.
  328. */
  329. #include <asm/qspinlock_paravirt.h>