qspinlock.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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. /*
  227. * queued_spin_lock_slowpath() can (load-)ACQUIRE the lock before
  228. * issuing an _unordered_ store to set _Q_LOCKED_VAL.
  229. *
  230. * This means that the store can be delayed, but no later than the
  231. * store-release from the unlock. This means that simply observing
  232. * _Q_LOCKED_VAL is not sufficient to determine if the lock is acquired.
  233. *
  234. * There are two paths that can issue the unordered store:
  235. *
  236. * (1) clear_pending_set_locked(): *,1,0 -> *,0,1
  237. *
  238. * (2) set_locked(): t,0,0 -> t,0,1 ; t != 0
  239. * atomic_cmpxchg_relaxed(): t,0,0 -> 0,0,1
  240. *
  241. * However, in both cases we have other !0 state we've set before to queue
  242. * ourseves:
  243. *
  244. * For (1) we have the atomic_cmpxchg_acquire() that set _Q_PENDING_VAL, our
  245. * load is constrained by that ACQUIRE to not pass before that, and thus must
  246. * observe the store.
  247. *
  248. * For (2) we have a more intersting scenario. We enqueue ourselves using
  249. * xchg_tail(), which ends up being a RELEASE. This in itself is not
  250. * sufficient, however that is followed by an smp_cond_acquire() on the same
  251. * word, giving a RELEASE->ACQUIRE ordering. This again constrains our load and
  252. * guarantees we must observe that store.
  253. *
  254. * Therefore both cases have other !0 state that is observable before the
  255. * unordered locked byte store comes through. This means we can use that to
  256. * wait for the lock store, and then wait for an unlock.
  257. */
  258. #ifndef queued_spin_unlock_wait
  259. void queued_spin_unlock_wait(struct qspinlock *lock)
  260. {
  261. u32 val;
  262. for (;;) {
  263. val = atomic_read(&lock->val);
  264. if (!val) /* not locked, we're done */
  265. goto done;
  266. if (val & _Q_LOCKED_MASK) /* locked, go wait for unlock */
  267. break;
  268. /* not locked, but pending, wait until we observe the lock */
  269. cpu_relax();
  270. }
  271. /* any unlock is good */
  272. while (atomic_read(&lock->val) & _Q_LOCKED_MASK)
  273. cpu_relax();
  274. done:
  275. smp_rmb(); /* CTRL + RMB -> ACQUIRE */
  276. }
  277. EXPORT_SYMBOL(queued_spin_unlock_wait);
  278. #endif
  279. #endif /* _GEN_PV_LOCK_SLOWPATH */
  280. /**
  281. * queued_spin_lock_slowpath - acquire the queued spinlock
  282. * @lock: Pointer to queued spinlock structure
  283. * @val: Current value of the queued spinlock 32-bit word
  284. *
  285. * (queue tail, pending bit, lock value)
  286. *
  287. * fast : slow : unlock
  288. * : :
  289. * uncontended (0,0,0) -:--> (0,0,1) ------------------------------:--> (*,*,0)
  290. * : | ^--------.------. / :
  291. * : v \ \ | :
  292. * pending : (0,1,1) +--> (0,1,0) \ | :
  293. * : | ^--' | | :
  294. * : v | | :
  295. * uncontended : (n,x,y) +--> (n,0,0) --' | :
  296. * queue : | ^--' | :
  297. * : v | :
  298. * contended : (*,x,y) +--> (*,0,0) ---> (*,0,1) -' :
  299. * queue : ^--' :
  300. */
  301. void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)
  302. {
  303. struct mcs_spinlock *prev, *next, *node;
  304. u32 new, old, tail;
  305. int idx;
  306. BUILD_BUG_ON(CONFIG_NR_CPUS >= (1U << _Q_TAIL_CPU_BITS));
  307. if (pv_enabled())
  308. goto queue;
  309. if (virt_spin_lock(lock))
  310. return;
  311. /*
  312. * wait for in-progress pending->locked hand-overs
  313. *
  314. * 0,1,0 -> 0,0,1
  315. */
  316. if (val == _Q_PENDING_VAL) {
  317. while ((val = atomic_read(&lock->val)) == _Q_PENDING_VAL)
  318. cpu_relax();
  319. }
  320. /*
  321. * trylock || pending
  322. *
  323. * 0,0,0 -> 0,0,1 ; trylock
  324. * 0,0,1 -> 0,1,1 ; pending
  325. */
  326. for (;;) {
  327. /*
  328. * If we observe any contention; queue.
  329. */
  330. if (val & ~_Q_LOCKED_MASK)
  331. goto queue;
  332. new = _Q_LOCKED_VAL;
  333. if (val == new)
  334. new |= _Q_PENDING_VAL;
  335. old = atomic_cmpxchg(&lock->val, val, new);
  336. if (old == val)
  337. break;
  338. val = old;
  339. }
  340. /*
  341. * we won the trylock
  342. */
  343. if (new == _Q_LOCKED_VAL)
  344. return;
  345. /*
  346. * we're pending, wait for the owner to go away.
  347. *
  348. * *,1,1 -> *,1,0
  349. *
  350. * this wait loop must be a load-acquire such that we match the
  351. * store-release that clears the locked bit and create lock
  352. * sequentiality; this is because not all clear_pending_set_locked()
  353. * implementations imply full barriers.
  354. */
  355. while ((val = smp_load_acquire(&lock->val.counter)) & _Q_LOCKED_MASK)
  356. cpu_relax();
  357. /*
  358. * take ownership and clear the pending bit.
  359. *
  360. * *,1,0 -> *,0,1
  361. */
  362. clear_pending_set_locked(lock);
  363. return;
  364. /*
  365. * End of pending bit optimistic spinning and beginning of MCS
  366. * queuing.
  367. */
  368. queue:
  369. node = this_cpu_ptr(&mcs_nodes[0]);
  370. idx = node->count++;
  371. tail = encode_tail(smp_processor_id(), idx);
  372. node += idx;
  373. /*
  374. * Ensure that we increment the head node->count before initialising
  375. * the actual node. If the compiler is kind enough to reorder these
  376. * stores, then an IRQ could overwrite our assignments.
  377. */
  378. barrier();
  379. node->locked = 0;
  380. node->next = NULL;
  381. pv_init_node(node);
  382. /*
  383. * We touched a (possibly) cold cacheline in the per-cpu queue node;
  384. * attempt the trylock once more in the hope someone let go while we
  385. * weren't watching.
  386. */
  387. if (queued_spin_trylock(lock))
  388. goto release;
  389. /*
  390. * We have already touched the queueing cacheline; don't bother with
  391. * pending stuff.
  392. *
  393. * p,*,* -> n,*,*
  394. */
  395. old = xchg_tail(lock, tail);
  396. /*
  397. * if there was a previous node; link it and wait until reaching the
  398. * head of the waitqueue.
  399. */
  400. if (old & _Q_TAIL_MASK) {
  401. prev = decode_tail(old);
  402. WRITE_ONCE(prev->next, node);
  403. pv_wait_node(node);
  404. arch_mcs_spin_lock_contended(&node->locked);
  405. }
  406. /*
  407. * we're at the head of the waitqueue, wait for the owner & pending to
  408. * go away.
  409. *
  410. * *,x,y -> *,0,0
  411. *
  412. * this wait loop must use a load-acquire such that we match the
  413. * store-release that clears the locked bit and create lock
  414. * sequentiality; this is because the set_locked() function below
  415. * does not imply a full barrier.
  416. *
  417. */
  418. pv_wait_head(lock, node);
  419. while ((val = smp_load_acquire(&lock->val.counter)) & _Q_LOCKED_PENDING_MASK)
  420. cpu_relax();
  421. /*
  422. * claim the lock:
  423. *
  424. * n,0,0 -> 0,0,1 : lock, uncontended
  425. * *,0,0 -> *,0,1 : lock, contended
  426. *
  427. * If the queue head is the only one in the queue (lock value == tail),
  428. * clear the tail code and grab the lock. Otherwise, we only need
  429. * to grab the lock.
  430. */
  431. for (;;) {
  432. if (val != tail) {
  433. set_locked(lock);
  434. break;
  435. }
  436. old = atomic_cmpxchg(&lock->val, val, _Q_LOCKED_VAL);
  437. if (old == val)
  438. goto release; /* No contention */
  439. val = old;
  440. }
  441. /*
  442. * contended path; wait for next, release.
  443. */
  444. while (!(next = READ_ONCE(node->next)))
  445. cpu_relax();
  446. arch_mcs_spin_unlock_contended(&next->locked);
  447. pv_kick_node(lock, next);
  448. release:
  449. /*
  450. * release the node
  451. */
  452. this_cpu_dec(mcs_nodes[0].count);
  453. }
  454. EXPORT_SYMBOL(queued_spin_lock_slowpath);
  455. /*
  456. * Generate the paravirt code for queued_spin_unlock_slowpath().
  457. */
  458. #if !defined(_GEN_PV_LOCK_SLOWPATH) && defined(CONFIG_PARAVIRT_SPINLOCKS)
  459. #define _GEN_PV_LOCK_SLOWPATH
  460. #undef pv_enabled
  461. #define pv_enabled() true
  462. #undef pv_init_node
  463. #undef pv_wait_node
  464. #undef pv_kick_node
  465. #undef pv_wait_head
  466. #undef queued_spin_lock_slowpath
  467. #define queued_spin_lock_slowpath __pv_queued_spin_lock_slowpath
  468. #include "qspinlock_paravirt.h"
  469. #include "qspinlock.c"
  470. #endif