qspinlock.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. #include <linux/bug.h>
  3. #include <linux/compiler.h>
  4. #include <linux/export.h>
  5. #include <linux/percpu.h>
  6. #include <linux/processor.h>
  7. #include <linux/smp.h>
  8. #include <linux/topology.h>
  9. #include <linux/sched/clock.h>
  10. #include <asm/qspinlock.h>
  11. #include <asm/paravirt.h>
  12. #define MAX_NODES 4
  13. struct qnode {
  14. struct qnode *next;
  15. struct qspinlock *lock;
  16. int cpu;
  17. u8 sleepy; /* 1 if the previous vCPU was preempted or
  18. * if the previous node was sleepy */
  19. u8 locked; /* 1 if lock acquired */
  20. };
  21. struct qnodes {
  22. int count;
  23. struct qnode nodes[MAX_NODES];
  24. };
  25. /* Tuning parameters */
  26. static int steal_spins __read_mostly = (1 << 5);
  27. static int remote_steal_spins __read_mostly = (1 << 2);
  28. #if _Q_SPIN_TRY_LOCK_STEAL == 1
  29. static const bool maybe_stealers = true;
  30. #else
  31. static bool maybe_stealers __read_mostly = true;
  32. #endif
  33. static int head_spins __read_mostly = (1 << 8);
  34. static bool pv_yield_owner __read_mostly = true;
  35. static bool pv_yield_allow_steal __read_mostly = false;
  36. static bool pv_spin_on_preempted_owner __read_mostly = false;
  37. static bool pv_sleepy_lock __read_mostly = true;
  38. static bool pv_sleepy_lock_sticky __read_mostly = false;
  39. static u64 pv_sleepy_lock_interval_ns __read_mostly = 0;
  40. static int pv_sleepy_lock_factor __read_mostly = 256;
  41. static bool pv_yield_prev __read_mostly = true;
  42. static bool pv_yield_sleepy_owner __read_mostly = true;
  43. static bool pv_prod_head __read_mostly = false;
  44. static DEFINE_PER_CPU_ALIGNED(struct qnodes, qnodes);
  45. static DEFINE_PER_CPU_ALIGNED(u64, sleepy_lock_seen_clock);
  46. #if _Q_SPIN_SPEC_BARRIER == 1
  47. #define spec_barrier() do { asm volatile("ori 31,31,0" ::: "memory"); } while (0)
  48. #else
  49. #define spec_barrier() do { } while (0)
  50. #endif
  51. static __always_inline bool recently_sleepy(void)
  52. {
  53. /* pv_sleepy_lock is true when this is called */
  54. if (pv_sleepy_lock_interval_ns) {
  55. u64 seen = this_cpu_read(sleepy_lock_seen_clock);
  56. if (seen) {
  57. u64 delta = sched_clock() - seen;
  58. if (delta < pv_sleepy_lock_interval_ns)
  59. return true;
  60. this_cpu_write(sleepy_lock_seen_clock, 0);
  61. }
  62. }
  63. return false;
  64. }
  65. static __always_inline int get_steal_spins(bool paravirt, bool sleepy)
  66. {
  67. if (paravirt && sleepy)
  68. return steal_spins * pv_sleepy_lock_factor;
  69. else
  70. return steal_spins;
  71. }
  72. static __always_inline int get_remote_steal_spins(bool paravirt, bool sleepy)
  73. {
  74. if (paravirt && sleepy)
  75. return remote_steal_spins * pv_sleepy_lock_factor;
  76. else
  77. return remote_steal_spins;
  78. }
  79. static __always_inline int get_head_spins(bool paravirt, bool sleepy)
  80. {
  81. if (paravirt && sleepy)
  82. return head_spins * pv_sleepy_lock_factor;
  83. else
  84. return head_spins;
  85. }
  86. static inline u32 encode_tail_cpu(int cpu)
  87. {
  88. return (cpu + 1) << _Q_TAIL_CPU_OFFSET;
  89. }
  90. static inline int decode_tail_cpu(u32 val)
  91. {
  92. return (val >> _Q_TAIL_CPU_OFFSET) - 1;
  93. }
  94. static inline int get_owner_cpu(u32 val)
  95. {
  96. return (val & _Q_OWNER_CPU_MASK) >> _Q_OWNER_CPU_OFFSET;
  97. }
  98. /*
  99. * Try to acquire the lock if it was not already locked. If the tail matches
  100. * mytail then clear it, otherwise leave it unchnaged. Return previous value.
  101. *
  102. * This is used by the head of the queue to acquire the lock and clean up
  103. * its tail if it was the last one queued.
  104. */
  105. static __always_inline u32 trylock_clean_tail(struct qspinlock *lock, u32 tail)
  106. {
  107. u32 newval = queued_spin_encode_locked_val();
  108. u32 prev, tmp;
  109. asm volatile(
  110. "1: lwarx %0,0,%2,%7 # trylock_clean_tail \n"
  111. /* This test is necessary if there could be stealers */
  112. " andi. %1,%0,%5 \n"
  113. " bne 3f \n"
  114. /* Test whether the lock tail == mytail */
  115. " and %1,%0,%6 \n"
  116. " cmpw 0,%1,%3 \n"
  117. /* Merge the new locked value */
  118. " or %1,%1,%4 \n"
  119. " bne 2f \n"
  120. /* If the lock tail matched, then clear it, otherwise leave it. */
  121. " andc %1,%1,%6 \n"
  122. "2: stwcx. %1,0,%2 \n"
  123. " bne- 1b \n"
  124. "\t" PPC_ACQUIRE_BARRIER " \n"
  125. "3: \n"
  126. : "=&r" (prev), "=&r" (tmp)
  127. : "r" (&lock->val), "r"(tail), "r" (newval),
  128. "i" (_Q_LOCKED_VAL),
  129. "r" (_Q_TAIL_CPU_MASK),
  130. "i" (_Q_SPIN_EH_HINT)
  131. : "cr0", "memory");
  132. return prev;
  133. }
  134. /*
  135. * Publish our tail, replacing previous tail. Return previous value.
  136. *
  137. * This provides a release barrier for publishing node, this pairs with the
  138. * acquire barrier in get_tail_qnode() when the next CPU finds this tail
  139. * value.
  140. */
  141. static __always_inline u32 publish_tail_cpu(struct qspinlock *lock, u32 tail)
  142. {
  143. u32 prev, tmp;
  144. kcsan_release();
  145. asm volatile(
  146. "\t" PPC_RELEASE_BARRIER " \n"
  147. "1: lwarx %0,0,%2 # publish_tail_cpu \n"
  148. " andc %1,%0,%4 \n"
  149. " or %1,%1,%3 \n"
  150. " stwcx. %1,0,%2 \n"
  151. " bne- 1b \n"
  152. : "=&r" (prev), "=&r"(tmp)
  153. : "r" (&lock->val), "r" (tail), "r"(_Q_TAIL_CPU_MASK)
  154. : "cr0", "memory");
  155. return prev;
  156. }
  157. static __always_inline u32 set_mustq(struct qspinlock *lock)
  158. {
  159. u32 prev;
  160. asm volatile(
  161. "1: lwarx %0,0,%1 # set_mustq \n"
  162. " or %0,%0,%2 \n"
  163. " stwcx. %0,0,%1 \n"
  164. " bne- 1b \n"
  165. : "=&r" (prev)
  166. : "r" (&lock->val), "r" (_Q_MUST_Q_VAL)
  167. : "cr0", "memory");
  168. return prev;
  169. }
  170. static __always_inline u32 clear_mustq(struct qspinlock *lock)
  171. {
  172. u32 prev;
  173. asm volatile(
  174. "1: lwarx %0,0,%1 # clear_mustq \n"
  175. " andc %0,%0,%2 \n"
  176. " stwcx. %0,0,%1 \n"
  177. " bne- 1b \n"
  178. : "=&r" (prev)
  179. : "r" (&lock->val), "r" (_Q_MUST_Q_VAL)
  180. : "cr0", "memory");
  181. return prev;
  182. }
  183. static __always_inline bool try_set_sleepy(struct qspinlock *lock, u32 old)
  184. {
  185. u32 prev;
  186. u32 new = old | _Q_SLEEPY_VAL;
  187. BUG_ON(!(old & _Q_LOCKED_VAL));
  188. BUG_ON(old & _Q_SLEEPY_VAL);
  189. asm volatile(
  190. "1: lwarx %0,0,%1 # try_set_sleepy \n"
  191. " cmpw 0,%0,%2 \n"
  192. " bne- 2f \n"
  193. " stwcx. %3,0,%1 \n"
  194. " bne- 1b \n"
  195. "2: \n"
  196. : "=&r" (prev)
  197. : "r" (&lock->val), "r"(old), "r" (new)
  198. : "cr0", "memory");
  199. return likely(prev == old);
  200. }
  201. static __always_inline void seen_sleepy_owner(struct qspinlock *lock, u32 val)
  202. {
  203. if (pv_sleepy_lock) {
  204. if (pv_sleepy_lock_interval_ns)
  205. this_cpu_write(sleepy_lock_seen_clock, sched_clock());
  206. if (!(val & _Q_SLEEPY_VAL))
  207. try_set_sleepy(lock, val);
  208. }
  209. }
  210. static __always_inline void seen_sleepy_lock(void)
  211. {
  212. if (pv_sleepy_lock && pv_sleepy_lock_interval_ns)
  213. this_cpu_write(sleepy_lock_seen_clock, sched_clock());
  214. }
  215. static __always_inline void seen_sleepy_node(void)
  216. {
  217. if (pv_sleepy_lock) {
  218. if (pv_sleepy_lock_interval_ns)
  219. this_cpu_write(sleepy_lock_seen_clock, sched_clock());
  220. /* Don't set sleepy because we likely have a stale val */
  221. }
  222. }
  223. static struct qnode *get_tail_qnode(struct qspinlock *lock, int prev_cpu)
  224. {
  225. struct qnodes *qnodesp = per_cpu_ptr(&qnodes, prev_cpu);
  226. int idx;
  227. /*
  228. * After publishing the new tail and finding a previous tail in the
  229. * previous val (which is the control dependency), this barrier
  230. * orders the release barrier in publish_tail_cpu performed by the
  231. * last CPU, with subsequently looking at its qnode structures
  232. * after the barrier.
  233. */
  234. smp_acquire__after_ctrl_dep();
  235. for (idx = 0; idx < MAX_NODES; idx++) {
  236. struct qnode *qnode = &qnodesp->nodes[idx];
  237. if (qnode->lock == lock)
  238. return qnode;
  239. }
  240. BUG();
  241. }
  242. /* Called inside spin_begin(). Returns whether or not the vCPU was preempted. */
  243. static __always_inline bool __yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt, bool mustq)
  244. {
  245. int owner;
  246. u32 yield_count;
  247. bool preempted = false;
  248. BUG_ON(!(val & _Q_LOCKED_VAL));
  249. if (!paravirt)
  250. goto relax;
  251. if (!pv_yield_owner)
  252. goto relax;
  253. owner = get_owner_cpu(val);
  254. yield_count = yield_count_of(owner);
  255. if ((yield_count & 1) == 0)
  256. goto relax; /* owner vcpu is running */
  257. spin_end();
  258. seen_sleepy_owner(lock, val);
  259. preempted = true;
  260. /*
  261. * Read the lock word after sampling the yield count. On the other side
  262. * there may a wmb because the yield count update is done by the
  263. * hypervisor preemption and the value update by the OS, however this
  264. * ordering might reduce the chance of out of order accesses and
  265. * improve the heuristic.
  266. */
  267. smp_rmb();
  268. if (READ_ONCE(lock->val) == val) {
  269. if (mustq)
  270. clear_mustq(lock);
  271. yield_to_preempted(owner, yield_count);
  272. if (mustq)
  273. set_mustq(lock);
  274. spin_begin();
  275. /* Don't relax if we yielded. Maybe we should? */
  276. return preempted;
  277. }
  278. spin_begin();
  279. relax:
  280. spin_cpu_relax();
  281. return preempted;
  282. }
  283. /* Called inside spin_begin(). Returns whether or not the vCPU was preempted. */
  284. static __always_inline bool yield_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt)
  285. {
  286. return __yield_to_locked_owner(lock, val, paravirt, false);
  287. }
  288. /* Called inside spin_begin(). Returns whether or not the vCPU was preempted. */
  289. static __always_inline bool yield_head_to_locked_owner(struct qspinlock *lock, u32 val, bool paravirt)
  290. {
  291. bool mustq = false;
  292. if ((val & _Q_MUST_Q_VAL) && pv_yield_allow_steal)
  293. mustq = true;
  294. return __yield_to_locked_owner(lock, val, paravirt, mustq);
  295. }
  296. static __always_inline void propagate_sleepy(struct qnode *node, u32 val, bool paravirt)
  297. {
  298. struct qnode *next;
  299. int owner;
  300. if (!paravirt)
  301. return;
  302. if (!pv_yield_sleepy_owner)
  303. return;
  304. next = READ_ONCE(node->next);
  305. if (!next)
  306. return;
  307. if (next->sleepy)
  308. return;
  309. owner = get_owner_cpu(val);
  310. if (vcpu_is_preempted(owner))
  311. next->sleepy = 1;
  312. }
  313. /* Called inside spin_begin() */
  314. static __always_inline bool yield_to_prev(struct qspinlock *lock, struct qnode *node, int prev_cpu, bool paravirt)
  315. {
  316. u32 yield_count;
  317. bool preempted = false;
  318. if (!paravirt)
  319. goto relax;
  320. if (!pv_yield_sleepy_owner)
  321. goto yield_prev;
  322. /*
  323. * If the previous waiter was preempted it might not be able to
  324. * propagate sleepy to us, so check the lock in that case too.
  325. */
  326. if (node->sleepy || vcpu_is_preempted(prev_cpu)) {
  327. u32 val = READ_ONCE(lock->val);
  328. if (val & _Q_LOCKED_VAL) {
  329. if (node->next && !node->next->sleepy) {
  330. /*
  331. * Propagate sleepy to next waiter. Only if
  332. * owner is preempted, which allows the queue
  333. * to become "non-sleepy" if vCPU preemption
  334. * ceases to occur, even if the lock remains
  335. * highly contended.
  336. */
  337. if (vcpu_is_preempted(get_owner_cpu(val)))
  338. node->next->sleepy = 1;
  339. }
  340. preempted = yield_to_locked_owner(lock, val, paravirt);
  341. if (preempted)
  342. return preempted;
  343. }
  344. node->sleepy = false;
  345. }
  346. yield_prev:
  347. if (!pv_yield_prev)
  348. goto relax;
  349. yield_count = yield_count_of(prev_cpu);
  350. if ((yield_count & 1) == 0)
  351. goto relax; /* owner vcpu is running */
  352. spin_end();
  353. preempted = true;
  354. seen_sleepy_node();
  355. smp_rmb(); /* See __yield_to_locked_owner comment */
  356. if (!READ_ONCE(node->locked)) {
  357. yield_to_preempted(prev_cpu, yield_count);
  358. spin_begin();
  359. return preempted;
  360. }
  361. spin_begin();
  362. relax:
  363. spin_cpu_relax();
  364. return preempted;
  365. }
  366. static __always_inline bool steal_break(u32 val, int iters, bool paravirt, bool sleepy)
  367. {
  368. if (iters >= get_steal_spins(paravirt, sleepy))
  369. return true;
  370. if (IS_ENABLED(CONFIG_NUMA) &&
  371. (iters >= get_remote_steal_spins(paravirt, sleepy))) {
  372. int cpu = get_owner_cpu(val);
  373. if (numa_node_id() != cpu_to_node(cpu))
  374. return true;
  375. }
  376. return false;
  377. }
  378. static __always_inline bool try_to_steal_lock(struct qspinlock *lock, bool paravirt)
  379. {
  380. bool seen_preempted = false;
  381. bool sleepy = false;
  382. int iters = 0;
  383. u32 val;
  384. if (!steal_spins) {
  385. /* XXX: should spin_on_preempted_owner do anything here? */
  386. return false;
  387. }
  388. /* Attempt to steal the lock */
  389. spin_begin();
  390. do {
  391. bool preempted = false;
  392. val = READ_ONCE(lock->val);
  393. if (val & _Q_MUST_Q_VAL)
  394. break;
  395. spec_barrier();
  396. if (unlikely(!(val & _Q_LOCKED_VAL))) {
  397. spin_end();
  398. if (__queued_spin_trylock_steal(lock))
  399. return true;
  400. spin_begin();
  401. } else {
  402. preempted = yield_to_locked_owner(lock, val, paravirt);
  403. }
  404. if (paravirt && pv_sleepy_lock) {
  405. if (!sleepy) {
  406. if (val & _Q_SLEEPY_VAL) {
  407. seen_sleepy_lock();
  408. sleepy = true;
  409. } else if (recently_sleepy()) {
  410. sleepy = true;
  411. }
  412. }
  413. if (pv_sleepy_lock_sticky && seen_preempted &&
  414. !(val & _Q_SLEEPY_VAL)) {
  415. if (try_set_sleepy(lock, val))
  416. val |= _Q_SLEEPY_VAL;
  417. }
  418. }
  419. if (preempted) {
  420. seen_preempted = true;
  421. sleepy = true;
  422. if (!pv_spin_on_preempted_owner)
  423. iters++;
  424. /*
  425. * pv_spin_on_preempted_owner don't increase iters
  426. * while the owner is preempted -- we won't interfere
  427. * with it by definition. This could introduce some
  428. * latency issue if we continually observe preempted
  429. * owners, but hopefully that's a rare corner case of
  430. * a badly oversubscribed system.
  431. */
  432. } else {
  433. iters++;
  434. }
  435. } while (!steal_break(val, iters, paravirt, sleepy));
  436. spin_end();
  437. return false;
  438. }
  439. static __always_inline void queued_spin_lock_mcs_queue(struct qspinlock *lock, bool paravirt)
  440. {
  441. struct qnodes *qnodesp;
  442. struct qnode *next, *node;
  443. u32 val, old, tail;
  444. bool seen_preempted = false;
  445. bool sleepy = false;
  446. bool mustq = false;
  447. int idx;
  448. int iters = 0;
  449. BUILD_BUG_ON(CONFIG_NR_CPUS >= (1U << _Q_TAIL_CPU_BITS));
  450. qnodesp = this_cpu_ptr(&qnodes);
  451. if (unlikely(qnodesp->count >= MAX_NODES)) {
  452. spec_barrier();
  453. while (!queued_spin_trylock(lock))
  454. cpu_relax();
  455. return;
  456. }
  457. idx = qnodesp->count++;
  458. /*
  459. * Ensure that we increment the head node->count before initialising
  460. * the actual node. If the compiler is kind enough to reorder these
  461. * stores, then an IRQ could overwrite our assignments.
  462. */
  463. barrier();
  464. node = &qnodesp->nodes[idx];
  465. node->next = NULL;
  466. node->lock = lock;
  467. node->cpu = smp_processor_id();
  468. node->sleepy = 0;
  469. node->locked = 0;
  470. tail = encode_tail_cpu(node->cpu);
  471. /*
  472. * Assign all attributes of a node before it can be published.
  473. * Issues an lwsync, serving as a release barrier, as well as a
  474. * compiler barrier.
  475. */
  476. old = publish_tail_cpu(lock, tail);
  477. /*
  478. * If there was a previous node; link it and wait until reaching the
  479. * head of the waitqueue.
  480. */
  481. if (old & _Q_TAIL_CPU_MASK) {
  482. int prev_cpu = decode_tail_cpu(old);
  483. struct qnode *prev = get_tail_qnode(lock, prev_cpu);
  484. /* Link @node into the waitqueue. */
  485. WRITE_ONCE(prev->next, node);
  486. /* Wait for mcs node lock to be released */
  487. spin_begin();
  488. while (!READ_ONCE(node->locked)) {
  489. spec_barrier();
  490. if (yield_to_prev(lock, node, prev_cpu, paravirt))
  491. seen_preempted = true;
  492. }
  493. spec_barrier();
  494. spin_end();
  495. smp_rmb(); /* acquire barrier for the mcs lock */
  496. /*
  497. * Generic qspinlocks have this prefetch here, but it seems
  498. * like it could cause additional line transitions because
  499. * the waiter will keep loading from it.
  500. */
  501. if (_Q_SPIN_PREFETCH_NEXT) {
  502. next = READ_ONCE(node->next);
  503. if (next)
  504. prefetchw(next);
  505. }
  506. }
  507. /* We're at the head of the waitqueue, wait for the lock. */
  508. again:
  509. spin_begin();
  510. for (;;) {
  511. bool preempted;
  512. val = READ_ONCE(lock->val);
  513. if (!(val & _Q_LOCKED_VAL))
  514. break;
  515. spec_barrier();
  516. if (paravirt && pv_sleepy_lock && maybe_stealers) {
  517. if (!sleepy) {
  518. if (val & _Q_SLEEPY_VAL) {
  519. seen_sleepy_lock();
  520. sleepy = true;
  521. } else if (recently_sleepy()) {
  522. sleepy = true;
  523. }
  524. }
  525. if (pv_sleepy_lock_sticky && seen_preempted &&
  526. !(val & _Q_SLEEPY_VAL)) {
  527. if (try_set_sleepy(lock, val))
  528. val |= _Q_SLEEPY_VAL;
  529. }
  530. }
  531. propagate_sleepy(node, val, paravirt);
  532. preempted = yield_head_to_locked_owner(lock, val, paravirt);
  533. if (!maybe_stealers)
  534. continue;
  535. if (preempted)
  536. seen_preempted = true;
  537. if (paravirt && preempted) {
  538. sleepy = true;
  539. if (!pv_spin_on_preempted_owner)
  540. iters++;
  541. } else {
  542. iters++;
  543. }
  544. if (!mustq && iters >= get_head_spins(paravirt, sleepy)) {
  545. mustq = true;
  546. set_mustq(lock);
  547. val |= _Q_MUST_Q_VAL;
  548. }
  549. }
  550. spec_barrier();
  551. spin_end();
  552. /* If we're the last queued, must clean up the tail. */
  553. old = trylock_clean_tail(lock, tail);
  554. if (unlikely(old & _Q_LOCKED_VAL)) {
  555. BUG_ON(!maybe_stealers);
  556. goto again; /* Can only be true if maybe_stealers. */
  557. }
  558. if ((old & _Q_TAIL_CPU_MASK) == tail)
  559. goto release; /* We were the tail, no next. */
  560. /* There is a next, must wait for node->next != NULL (MCS protocol) */
  561. next = READ_ONCE(node->next);
  562. if (!next) {
  563. spin_begin();
  564. while (!(next = READ_ONCE(node->next)))
  565. cpu_relax();
  566. spin_end();
  567. }
  568. spec_barrier();
  569. /*
  570. * Unlock the next mcs waiter node. Release barrier is not required
  571. * here because the acquirer is only accessing the lock word, and
  572. * the acquire barrier we took the lock with orders that update vs
  573. * this store to locked. The corresponding barrier is the smp_rmb()
  574. * acquire barrier for mcs lock, above.
  575. */
  576. if (paravirt && pv_prod_head) {
  577. int next_cpu = next->cpu;
  578. WRITE_ONCE(next->locked, 1);
  579. if (_Q_SPIN_MISO)
  580. asm volatile("miso" ::: "memory");
  581. if (vcpu_is_preempted(next_cpu))
  582. prod_cpu(next_cpu);
  583. } else {
  584. WRITE_ONCE(next->locked, 1);
  585. if (_Q_SPIN_MISO)
  586. asm volatile("miso" ::: "memory");
  587. }
  588. release:
  589. /*
  590. * Clear the lock before releasing the node, as another CPU might see stale
  591. * values if an interrupt occurs after we increment qnodesp->count
  592. * but before node->lock is initialized. The barrier ensures that
  593. * there are no further stores to the node after it has been released.
  594. */
  595. node->lock = NULL;
  596. barrier();
  597. qnodesp->count--;
  598. }
  599. void queued_spin_lock_slowpath(struct qspinlock *lock)
  600. {
  601. /*
  602. * This looks funny, but it induces the compiler to inline both
  603. * sides of the branch rather than share code as when the condition
  604. * is passed as the paravirt argument to the functions.
  605. */
  606. if (IS_ENABLED(CONFIG_PARAVIRT_SPINLOCKS) && is_shared_processor()) {
  607. if (try_to_steal_lock(lock, true)) {
  608. spec_barrier();
  609. return;
  610. }
  611. queued_spin_lock_mcs_queue(lock, true);
  612. } else {
  613. if (try_to_steal_lock(lock, false)) {
  614. spec_barrier();
  615. return;
  616. }
  617. queued_spin_lock_mcs_queue(lock, false);
  618. }
  619. }
  620. EXPORT_SYMBOL(queued_spin_lock_slowpath);
  621. #ifdef CONFIG_PARAVIRT_SPINLOCKS
  622. void pv_spinlocks_init(void)
  623. {
  624. }
  625. #endif
  626. #include <linux/debugfs.h>
  627. static int steal_spins_set(void *data, u64 val)
  628. {
  629. #if _Q_SPIN_TRY_LOCK_STEAL == 1
  630. /* MAYBE_STEAL remains true */
  631. steal_spins = val;
  632. #else
  633. static DEFINE_MUTEX(lock);
  634. /*
  635. * The lock slow path has a !maybe_stealers case that can assume
  636. * the head of queue will not see concurrent waiters. That waiter
  637. * is unsafe in the presence of stealers, so must keep them away
  638. * from one another.
  639. */
  640. mutex_lock(&lock);
  641. if (val && !steal_spins) {
  642. maybe_stealers = true;
  643. /* wait for queue head waiter to go away */
  644. synchronize_rcu();
  645. steal_spins = val;
  646. } else if (!val && steal_spins) {
  647. steal_spins = val;
  648. /* wait for all possible stealers to go away */
  649. synchronize_rcu();
  650. maybe_stealers = false;
  651. } else {
  652. steal_spins = val;
  653. }
  654. mutex_unlock(&lock);
  655. #endif
  656. return 0;
  657. }
  658. static int steal_spins_get(void *data, u64 *val)
  659. {
  660. *val = steal_spins;
  661. return 0;
  662. }
  663. DEFINE_SIMPLE_ATTRIBUTE(fops_steal_spins, steal_spins_get, steal_spins_set, "%llu\n");
  664. static int remote_steal_spins_set(void *data, u64 val)
  665. {
  666. remote_steal_spins = val;
  667. return 0;
  668. }
  669. static int remote_steal_spins_get(void *data, u64 *val)
  670. {
  671. *val = remote_steal_spins;
  672. return 0;
  673. }
  674. DEFINE_SIMPLE_ATTRIBUTE(fops_remote_steal_spins, remote_steal_spins_get, remote_steal_spins_set, "%llu\n");
  675. static int head_spins_set(void *data, u64 val)
  676. {
  677. head_spins = val;
  678. return 0;
  679. }
  680. static int head_spins_get(void *data, u64 *val)
  681. {
  682. *val = head_spins;
  683. return 0;
  684. }
  685. DEFINE_SIMPLE_ATTRIBUTE(fops_head_spins, head_spins_get, head_spins_set, "%llu\n");
  686. static int pv_yield_owner_set(void *data, u64 val)
  687. {
  688. pv_yield_owner = !!val;
  689. return 0;
  690. }
  691. static int pv_yield_owner_get(void *data, u64 *val)
  692. {
  693. *val = pv_yield_owner;
  694. return 0;
  695. }
  696. DEFINE_SIMPLE_ATTRIBUTE(fops_pv_yield_owner, pv_yield_owner_get, pv_yield_owner_set, "%llu\n");
  697. static int pv_yield_allow_steal_set(void *data, u64 val)
  698. {
  699. pv_yield_allow_steal = !!val;
  700. return 0;
  701. }
  702. static int pv_yield_allow_steal_get(void *data, u64 *val)
  703. {
  704. *val = pv_yield_allow_steal;
  705. return 0;
  706. }
  707. DEFINE_SIMPLE_ATTRIBUTE(fops_pv_yield_allow_steal, pv_yield_allow_steal_get, pv_yield_allow_steal_set, "%llu\n");
  708. static int pv_spin_on_preempted_owner_set(void *data, u64 val)
  709. {
  710. pv_spin_on_preempted_owner = !!val;
  711. return 0;
  712. }
  713. static int pv_spin_on_preempted_owner_get(void *data, u64 *val)
  714. {
  715. *val = pv_spin_on_preempted_owner;
  716. return 0;
  717. }
  718. DEFINE_SIMPLE_ATTRIBUTE(fops_pv_spin_on_preempted_owner, pv_spin_on_preempted_owner_get, pv_spin_on_preempted_owner_set, "%llu\n");
  719. static int pv_sleepy_lock_set(void *data, u64 val)
  720. {
  721. pv_sleepy_lock = !!val;
  722. return 0;
  723. }
  724. static int pv_sleepy_lock_get(void *data, u64 *val)
  725. {
  726. *val = pv_sleepy_lock;
  727. return 0;
  728. }
  729. DEFINE_SIMPLE_ATTRIBUTE(fops_pv_sleepy_lock, pv_sleepy_lock_get, pv_sleepy_lock_set, "%llu\n");
  730. static int pv_sleepy_lock_sticky_set(void *data, u64 val)
  731. {
  732. pv_sleepy_lock_sticky = !!val;
  733. return 0;
  734. }
  735. static int pv_sleepy_lock_sticky_get(void *data, u64 *val)
  736. {
  737. *val = pv_sleepy_lock_sticky;
  738. return 0;
  739. }
  740. DEFINE_SIMPLE_ATTRIBUTE(fops_pv_sleepy_lock_sticky, pv_sleepy_lock_sticky_get, pv_sleepy_lock_sticky_set, "%llu\n");
  741. static int pv_sleepy_lock_interval_ns_set(void *data, u64 val)
  742. {
  743. pv_sleepy_lock_interval_ns = val;
  744. return 0;
  745. }
  746. static int pv_sleepy_lock_interval_ns_get(void *data, u64 *val)
  747. {
  748. *val = pv_sleepy_lock_interval_ns;
  749. return 0;
  750. }
  751. DEFINE_SIMPLE_ATTRIBUTE(fops_pv_sleepy_lock_interval_ns, pv_sleepy_lock_interval_ns_get, pv_sleepy_lock_interval_ns_set, "%llu\n");
  752. static int pv_sleepy_lock_factor_set(void *data, u64 val)
  753. {
  754. pv_sleepy_lock_factor = val;
  755. return 0;
  756. }
  757. static int pv_sleepy_lock_factor_get(void *data, u64 *val)
  758. {
  759. *val = pv_sleepy_lock_factor;
  760. return 0;
  761. }
  762. DEFINE_SIMPLE_ATTRIBUTE(fops_pv_sleepy_lock_factor, pv_sleepy_lock_factor_get, pv_sleepy_lock_factor_set, "%llu\n");
  763. static int pv_yield_prev_set(void *data, u64 val)
  764. {
  765. pv_yield_prev = !!val;
  766. return 0;
  767. }
  768. static int pv_yield_prev_get(void *data, u64 *val)
  769. {
  770. *val = pv_yield_prev;
  771. return 0;
  772. }
  773. DEFINE_SIMPLE_ATTRIBUTE(fops_pv_yield_prev, pv_yield_prev_get, pv_yield_prev_set, "%llu\n");
  774. static int pv_yield_sleepy_owner_set(void *data, u64 val)
  775. {
  776. pv_yield_sleepy_owner = !!val;
  777. return 0;
  778. }
  779. static int pv_yield_sleepy_owner_get(void *data, u64 *val)
  780. {
  781. *val = pv_yield_sleepy_owner;
  782. return 0;
  783. }
  784. DEFINE_SIMPLE_ATTRIBUTE(fops_pv_yield_sleepy_owner, pv_yield_sleepy_owner_get, pv_yield_sleepy_owner_set, "%llu\n");
  785. static int pv_prod_head_set(void *data, u64 val)
  786. {
  787. pv_prod_head = !!val;
  788. return 0;
  789. }
  790. static int pv_prod_head_get(void *data, u64 *val)
  791. {
  792. *val = pv_prod_head;
  793. return 0;
  794. }
  795. DEFINE_SIMPLE_ATTRIBUTE(fops_pv_prod_head, pv_prod_head_get, pv_prod_head_set, "%llu\n");
  796. static __init int spinlock_debugfs_init(void)
  797. {
  798. debugfs_create_file("qspl_steal_spins", 0600, arch_debugfs_dir, NULL, &fops_steal_spins);
  799. debugfs_create_file("qspl_remote_steal_spins", 0600, arch_debugfs_dir, NULL, &fops_remote_steal_spins);
  800. debugfs_create_file("qspl_head_spins", 0600, arch_debugfs_dir, NULL, &fops_head_spins);
  801. if (is_shared_processor()) {
  802. debugfs_create_file("qspl_pv_yield_owner", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_owner);
  803. debugfs_create_file("qspl_pv_yield_allow_steal", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_allow_steal);
  804. debugfs_create_file("qspl_pv_spin_on_preempted_owner", 0600, arch_debugfs_dir, NULL, &fops_pv_spin_on_preempted_owner);
  805. debugfs_create_file("qspl_pv_sleepy_lock", 0600, arch_debugfs_dir, NULL, &fops_pv_sleepy_lock);
  806. debugfs_create_file("qspl_pv_sleepy_lock_sticky", 0600, arch_debugfs_dir, NULL, &fops_pv_sleepy_lock_sticky);
  807. debugfs_create_file("qspl_pv_sleepy_lock_interval_ns", 0600, arch_debugfs_dir, NULL, &fops_pv_sleepy_lock_interval_ns);
  808. debugfs_create_file("qspl_pv_sleepy_lock_factor", 0600, arch_debugfs_dir, NULL, &fops_pv_sleepy_lock_factor);
  809. debugfs_create_file("qspl_pv_yield_prev", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_prev);
  810. debugfs_create_file("qspl_pv_yield_sleepy_owner", 0600, arch_debugfs_dir, NULL, &fops_pv_yield_sleepy_owner);
  811. debugfs_create_file("qspl_pv_prod_head", 0600, arch_debugfs_dir, NULL, &fops_pv_prod_head);
  812. }
  813. return 0;
  814. }
  815. device_initcall(spinlock_debugfs_init);