qspinlock.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * Queued spinlock
  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 2 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. * (C) Copyright 2013-2015 Hewlett-Packard Development Company, L.P.
  15. * (C) Copyright 2013-2014 Red Hat, Inc.
  16. * (C) Copyright 2015 Intel Corp.
  17. *
  18. * Authors: Waiman Long <waiman.long@hp.com>
  19. * Peter Zijlstra <peterz@infradead.org>
  20. */
  21. #ifndef _GEN_PV_LOCK_SLOWPATH
  22. #include <linux/smp.h>
  23. #include <linux/bug.h>
  24. #include <linux/cpumask.h>
  25. #include <linux/percpu.h>
  26. #include <linux/hardirq.h>
  27. #include <linux/mutex.h>
  28. #include <asm/byteorder.h>
  29. #include <asm/qspinlock.h>
  30. /*
  31. * The basic principle of a queue-based spinlock can best be understood
  32. * by studying a classic queue-based spinlock implementation called the
  33. * MCS lock. The paper below provides a good description for this kind
  34. * of lock.
  35. *
  36. * http://www.cise.ufl.edu/tr/DOC/REP-1992-71.pdf
  37. *
  38. * This queued spinlock implementation is based on the MCS lock, however to make
  39. * it fit the 4 bytes we assume spinlock_t to be, and preserve its existing
  40. * API, we must modify it somehow.
  41. *
  42. * In particular; where the traditional MCS lock consists of a tail pointer
  43. * (8 bytes) and needs the next pointer (another 8 bytes) of its own node to
  44. * unlock the next pending (next->locked), we compress both these: {tail,
  45. * next->locked} into a single u32 value.
  46. *
  47. * Since a spinlock disables recursion of its own context and there is a limit
  48. * to the contexts that can nest; namely: task, softirq, hardirq, nmi. As there
  49. * are at most 4 nesting levels, it can be encoded by a 2-bit number. Now
  50. * we can encode the tail by combining the 2-bit nesting level with the cpu
  51. * number. With one byte for the lock value and 3 bytes for the tail, only a
  52. * 32-bit word is now needed. Even though we only need 1 bit for the lock,
  53. * we extend it to a full byte to achieve better performance for architectures
  54. * that support atomic byte write.
  55. *
  56. * We also change the first spinner to spin on the lock bit instead of its
  57. * node; whereby avoiding the need to carry a node from lock to unlock, and
  58. * preserving existing lock API. This also makes the unlock code simpler and
  59. * faster.
  60. *
  61. * N.B. The current implementation only supports architectures that allow
  62. * atomic operations on smaller 8-bit and 16-bit data types.
  63. *
  64. */
  65. #include "mcs_spinlock.h"
  66. #ifdef CONFIG_PARAVIRT_SPINLOCKS
  67. #define MAX_NODES 8
  68. #else
  69. #define MAX_NODES 4
  70. #endif
  71. /*
  72. * Per-CPU queue node structures; we can never have more than 4 nested
  73. * contexts: task, softirq, hardirq, nmi.
  74. *
  75. * Exactly fits one 64-byte cacheline on a 64-bit architecture.
  76. *
  77. * PV doubles the storage and uses the second cacheline for PV state.
  78. */
  79. static DEFINE_PER_CPU_ALIGNED(struct mcs_spinlock, mcs_nodes[MAX_NODES]);
  80. /*
  81. * We must be able to distinguish between no-tail and the tail at 0:0,
  82. * therefore increment the cpu number by one.
  83. */
  84. static inline u32 encode_tail(int cpu, int idx)
  85. {
  86. u32 tail;
  87. #ifdef CONFIG_DEBUG_SPINLOCK
  88. BUG_ON(idx > 3);
  89. #endif
  90. tail = (cpu + 1) << _Q_TAIL_CPU_OFFSET;
  91. tail |= idx << _Q_TAIL_IDX_OFFSET; /* assume < 4 */
  92. return tail;
  93. }
  94. static inline struct mcs_spinlock *decode_tail(u32 tail)
  95. {
  96. int cpu = (tail >> _Q_TAIL_CPU_OFFSET) - 1;
  97. int idx = (tail & _Q_TAIL_IDX_MASK) >> _Q_TAIL_IDX_OFFSET;
  98. return per_cpu_ptr(&mcs_nodes[idx], cpu);
  99. }
  100. #define _Q_LOCKED_PENDING_MASK (_Q_LOCKED_MASK | _Q_PENDING_MASK)
  101. /*
  102. * By using the whole 2nd least significant byte for the pending bit, we
  103. * can allow better optimization of the lock acquisition for the pending
  104. * bit holder.
  105. *
  106. * This internal structure is also used by the set_locked function which
  107. * is not restricted to _Q_PENDING_BITS == 8.
  108. */
  109. struct __qspinlock {
  110. union {
  111. atomic_t val;
  112. #ifdef __LITTLE_ENDIAN
  113. struct {
  114. u8 locked;
  115. u8 pending;
  116. };
  117. struct {
  118. u16 locked_pending;
  119. u16 tail;
  120. };
  121. #else
  122. struct {
  123. u16 tail;
  124. u16 locked_pending;
  125. };
  126. struct {
  127. u8 reserved[2];
  128. u8 pending;
  129. u8 locked;
  130. };
  131. #endif
  132. };
  133. };
  134. #if _Q_PENDING_BITS == 8
  135. /**
  136. * clear_pending_set_locked - take ownership and clear the pending bit.
  137. * @lock: Pointer to queued spinlock structure
  138. *
  139. * *,1,0 -> *,0,1
  140. *
  141. * Lock stealing is not allowed if this function is used.
  142. */
  143. static __always_inline void clear_pending_set_locked(struct qspinlock *lock)
  144. {
  145. struct __qspinlock *l = (void *)lock;
  146. WRITE_ONCE(l->locked_pending, _Q_LOCKED_VAL);
  147. }
  148. /*
  149. * xchg_tail - Put in the new queue tail code word & retrieve previous one
  150. * @lock : Pointer to queued spinlock structure
  151. * @tail : The new queue tail code word
  152. * Return: The previous queue tail code word
  153. *
  154. * xchg(lock, tail)
  155. *
  156. * p,*,* -> n,*,* ; prev = xchg(lock, node)
  157. */
  158. static __always_inline u32 xchg_tail(struct qspinlock *lock, u32 tail)
  159. {
  160. struct __qspinlock *l = (void *)lock;
  161. return (u32)xchg(&l->tail, tail >> _Q_TAIL_OFFSET) << _Q_TAIL_OFFSET;
  162. }
  163. #else /* _Q_PENDING_BITS == 8 */
  164. /**
  165. * clear_pending_set_locked - take ownership and clear the pending bit.
  166. * @lock: Pointer to queued spinlock structure
  167. *
  168. * *,1,0 -> *,0,1
  169. */
  170. static __always_inline void clear_pending_set_locked(struct qspinlock *lock)
  171. {
  172. atomic_add(-_Q_PENDING_VAL + _Q_LOCKED_VAL, &lock->val);
  173. }
  174. /**
  175. * xchg_tail - Put in the new queue tail code word & retrieve previous one
  176. * @lock : Pointer to queued spinlock structure
  177. * @tail : The new queue tail code word
  178. * Return: The previous queue tail code word
  179. *
  180. * xchg(lock, tail)
  181. *
  182. * p,*,* -> n,*,* ; prev = xchg(lock, node)
  183. */
  184. static __always_inline u32 xchg_tail(struct qspinlock *lock, u32 tail)
  185. {
  186. u32 old, new, val = atomic_read(&lock->val);
  187. for (;;) {
  188. new = (val & _Q_LOCKED_PENDING_MASK) | tail;
  189. old = atomic_cmpxchg(&lock->val, val, new);
  190. if (old == val)
  191. break;
  192. val = old;
  193. }
  194. return old;
  195. }
  196. #endif /* _Q_PENDING_BITS == 8 */
  197. /**
  198. * set_locked - Set the lock bit and own the lock
  199. * @lock: Pointer to queued spinlock structure
  200. *
  201. * *,*,0 -> *,0,1
  202. */
  203. static __always_inline void set_locked(struct qspinlock *lock)
  204. {
  205. struct __qspinlock *l = (void *)lock;
  206. WRITE_ONCE(l->locked, _Q_LOCKED_VAL);
  207. }
  208. /*
  209. * Generate the native code for queued_spin_unlock_slowpath(); provide NOPs for
  210. * all the PV callbacks.
  211. */
  212. static __always_inline void __pv_init_node(struct mcs_spinlock *node) { }
  213. static __always_inline void __pv_wait_node(struct mcs_spinlock *node) { }
  214. static __always_inline void __pv_kick_node(struct qspinlock *lock,
  215. struct mcs_spinlock *node) { }
  216. static __always_inline void __pv_wait_head(struct qspinlock *lock,
  217. struct mcs_spinlock *node) { }
  218. #define pv_enabled() false
  219. #define pv_init_node __pv_init_node
  220. #define pv_wait_node __pv_wait_node
  221. #define pv_kick_node __pv_kick_node
  222. #define pv_wait_head __pv_wait_head
  223. #ifdef CONFIG_PARAVIRT_SPINLOCKS
  224. #define queued_spin_lock_slowpath native_queued_spin_lock_slowpath
  225. #endif
  226. #endif /* _GEN_PV_LOCK_SLOWPATH */
  227. /**
  228. * queued_spin_lock_slowpath - acquire the queued spinlock
  229. * @lock: Pointer to queued spinlock structure
  230. * @val: Current value of the queued spinlock 32-bit word
  231. *
  232. * (queue tail, pending bit, lock value)
  233. *
  234. * fast : slow : unlock
  235. * : :
  236. * uncontended (0,0,0) -:--> (0,0,1) ------------------------------:--> (*,*,0)
  237. * : | ^--------.------. / :
  238. * : v \ \ | :
  239. * pending : (0,1,1) +--> (0,1,0) \ | :
  240. * : | ^--' | | :
  241. * : v | | :
  242. * uncontended : (n,x,y) +--> (n,0,0) --' | :
  243. * queue : | ^--' | :
  244. * : v | :
  245. * contended : (*,x,y) +--> (*,0,0) ---> (*,0,1) -' :
  246. * queue : ^--' :
  247. */
  248. void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)
  249. {
  250. struct mcs_spinlock *prev, *next, *node;
  251. u32 new, old, tail;
  252. int idx;
  253. BUILD_BUG_ON(CONFIG_NR_CPUS >= (1U << _Q_TAIL_CPU_BITS));
  254. if (pv_enabled())
  255. goto queue;
  256. if (virt_spin_lock(lock))
  257. return;
  258. /*
  259. * wait for in-progress pending->locked hand-overs
  260. *
  261. * 0,1,0 -> 0,0,1
  262. */
  263. if (val == _Q_PENDING_VAL) {
  264. while ((val = atomic_read(&lock->val)) == _Q_PENDING_VAL)
  265. cpu_relax();
  266. }
  267. /*
  268. * trylock || pending
  269. *
  270. * 0,0,0 -> 0,0,1 ; trylock
  271. * 0,0,1 -> 0,1,1 ; pending
  272. */
  273. for (;;) {
  274. /*
  275. * If we observe any contention; queue.
  276. */
  277. if (val & ~_Q_LOCKED_MASK)
  278. goto queue;
  279. new = _Q_LOCKED_VAL;
  280. if (val == new)
  281. new |= _Q_PENDING_VAL;
  282. old = atomic_cmpxchg(&lock->val, val, new);
  283. if (old == val)
  284. break;
  285. val = old;
  286. }
  287. /*
  288. * we won the trylock
  289. */
  290. if (new == _Q_LOCKED_VAL)
  291. return;
  292. /*
  293. * we're pending, wait for the owner to go away.
  294. *
  295. * *,1,1 -> *,1,0
  296. *
  297. * this wait loop must be a load-acquire such that we match the
  298. * store-release that clears the locked bit and create lock
  299. * sequentiality; this is because not all clear_pending_set_locked()
  300. * implementations imply full barriers.
  301. */
  302. while ((val = smp_load_acquire(&lock->val.counter)) & _Q_LOCKED_MASK)
  303. cpu_relax();
  304. /*
  305. * take ownership and clear the pending bit.
  306. *
  307. * *,1,0 -> *,0,1
  308. */
  309. clear_pending_set_locked(lock);
  310. return;
  311. /*
  312. * End of pending bit optimistic spinning and beginning of MCS
  313. * queuing.
  314. */
  315. queue:
  316. node = this_cpu_ptr(&mcs_nodes[0]);
  317. idx = node->count++;
  318. tail = encode_tail(smp_processor_id(), idx);
  319. node += idx;
  320. node->locked = 0;
  321. node->next = NULL;
  322. pv_init_node(node);
  323. /*
  324. * We touched a (possibly) cold cacheline in the per-cpu queue node;
  325. * attempt the trylock once more in the hope someone let go while we
  326. * weren't watching.
  327. */
  328. if (queued_spin_trylock(lock))
  329. goto release;
  330. /*
  331. * We have already touched the queueing cacheline; don't bother with
  332. * pending stuff.
  333. *
  334. * p,*,* -> n,*,*
  335. */
  336. old = xchg_tail(lock, tail);
  337. /*
  338. * if there was a previous node; link it and wait until reaching the
  339. * head of the waitqueue.
  340. */
  341. if (old & _Q_TAIL_MASK) {
  342. prev = decode_tail(old);
  343. WRITE_ONCE(prev->next, node);
  344. pv_wait_node(node);
  345. arch_mcs_spin_lock_contended(&node->locked);
  346. }
  347. /*
  348. * we're at the head of the waitqueue, wait for the owner & pending to
  349. * go away.
  350. *
  351. * *,x,y -> *,0,0
  352. *
  353. * this wait loop must use a load-acquire such that we match the
  354. * store-release that clears the locked bit and create lock
  355. * sequentiality; this is because the set_locked() function below
  356. * does not imply a full barrier.
  357. *
  358. */
  359. pv_wait_head(lock, node);
  360. while ((val = smp_load_acquire(&lock->val.counter)) & _Q_LOCKED_PENDING_MASK)
  361. cpu_relax();
  362. /*
  363. * claim the lock:
  364. *
  365. * n,0,0 -> 0,0,1 : lock, uncontended
  366. * *,0,0 -> *,0,1 : lock, contended
  367. *
  368. * If the queue head is the only one in the queue (lock value == tail),
  369. * clear the tail code and grab the lock. Otherwise, we only need
  370. * to grab the lock.
  371. */
  372. for (;;) {
  373. if (val != tail) {
  374. set_locked(lock);
  375. break;
  376. }
  377. old = atomic_cmpxchg(&lock->val, val, _Q_LOCKED_VAL);
  378. if (old == val)
  379. goto release; /* No contention */
  380. val = old;
  381. }
  382. /*
  383. * contended path; wait for next, release.
  384. */
  385. while (!(next = READ_ONCE(node->next)))
  386. cpu_relax();
  387. arch_mcs_spin_unlock_contended(&next->locked);
  388. pv_kick_node(lock, next);
  389. release:
  390. /*
  391. * release the node
  392. */
  393. this_cpu_dec(mcs_nodes[0].count);
  394. }
  395. EXPORT_SYMBOL(queued_spin_lock_slowpath);
  396. /*
  397. * Generate the paravirt code for queued_spin_unlock_slowpath().
  398. */
  399. #if !defined(_GEN_PV_LOCK_SLOWPATH) && defined(CONFIG_PARAVIRT_SPINLOCKS)
  400. #define _GEN_PV_LOCK_SLOWPATH
  401. #undef pv_enabled
  402. #define pv_enabled() true
  403. #undef pv_init_node
  404. #undef pv_wait_node
  405. #undef pv_kick_node
  406. #undef pv_wait_head
  407. #undef queued_spin_lock_slowpath
  408. #define queued_spin_lock_slowpath __pv_queued_spin_lock_slowpath
  409. #include "qspinlock_paravirt.h"
  410. #include "qspinlock.c"
  411. #endif