spinlock.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * Split spinlock implementation out into its own file, so it can be
  3. * compiled in a FTRACE-compatible way.
  4. */
  5. #include <linux/kernel_stat.h>
  6. #include <linux/spinlock.h>
  7. #include <linux/debugfs.h>
  8. #include <linux/log2.h>
  9. #include <linux/gfp.h>
  10. #include <linux/slab.h>
  11. #include <linux/atomic.h>
  12. #include <asm/paravirt.h>
  13. #include <xen/interface/xen.h>
  14. #include <xen/events.h>
  15. #include "xen-ops.h"
  16. #include "debugfs.h"
  17. static DEFINE_PER_CPU(int, lock_kicker_irq) = -1;
  18. static DEFINE_PER_CPU(char *, irq_name);
  19. static DEFINE_PER_CPU(atomic_t, xen_qlock_wait_nest);
  20. static bool xen_pvspin = true;
  21. #ifdef CONFIG_QUEUED_SPINLOCKS
  22. #include <asm/qspinlock.h>
  23. static void xen_qlock_kick(int cpu)
  24. {
  25. int irq = per_cpu(lock_kicker_irq, cpu);
  26. /* Don't kick if the target's kicker interrupt is not initialized. */
  27. if (irq == -1)
  28. return;
  29. xen_send_IPI_one(cpu, XEN_SPIN_UNLOCK_VECTOR);
  30. }
  31. /*
  32. * Halt the current CPU & release it back to the host
  33. */
  34. static void xen_qlock_wait(u8 *byte, u8 val)
  35. {
  36. int irq = __this_cpu_read(lock_kicker_irq);
  37. atomic_t *nest_cnt = this_cpu_ptr(&xen_qlock_wait_nest);
  38. /* If kicker interrupts not initialized yet, just spin */
  39. if (irq == -1 || in_nmi())
  40. return;
  41. /* Detect reentry. */
  42. atomic_inc(nest_cnt);
  43. /* If irq pending already and no nested call clear it. */
  44. if (atomic_read(nest_cnt) == 1 && xen_test_irq_pending(irq)) {
  45. xen_clear_irq_pending(irq);
  46. } else if (READ_ONCE(*byte) == val) {
  47. /* Block until irq becomes pending (or a spurious wakeup) */
  48. xen_poll_irq(irq);
  49. }
  50. atomic_dec(nest_cnt);
  51. }
  52. #else /* CONFIG_QUEUED_SPINLOCKS */
  53. enum xen_contention_stat {
  54. TAKEN_SLOW,
  55. TAKEN_SLOW_PICKUP,
  56. TAKEN_SLOW_SPURIOUS,
  57. RELEASED_SLOW,
  58. RELEASED_SLOW_KICKED,
  59. NR_CONTENTION_STATS
  60. };
  61. #ifdef CONFIG_XEN_DEBUG_FS
  62. #define HISTO_BUCKETS 30
  63. static struct xen_spinlock_stats
  64. {
  65. u32 contention_stats[NR_CONTENTION_STATS];
  66. u32 histo_spin_blocked[HISTO_BUCKETS+1];
  67. u64 time_blocked;
  68. } spinlock_stats;
  69. static u8 zero_stats;
  70. static inline void check_zero(void)
  71. {
  72. u8 ret;
  73. u8 old = READ_ONCE(zero_stats);
  74. if (unlikely(old)) {
  75. ret = cmpxchg(&zero_stats, old, 0);
  76. /* This ensures only one fellow resets the stat */
  77. if (ret == old)
  78. memset(&spinlock_stats, 0, sizeof(spinlock_stats));
  79. }
  80. }
  81. static inline void add_stats(enum xen_contention_stat var, u32 val)
  82. {
  83. check_zero();
  84. spinlock_stats.contention_stats[var] += val;
  85. }
  86. static inline u64 spin_time_start(void)
  87. {
  88. return xen_clocksource_read();
  89. }
  90. static void __spin_time_accum(u64 delta, u32 *array)
  91. {
  92. unsigned index = ilog2(delta);
  93. check_zero();
  94. if (index < HISTO_BUCKETS)
  95. array[index]++;
  96. else
  97. array[HISTO_BUCKETS]++;
  98. }
  99. static inline void spin_time_accum_blocked(u64 start)
  100. {
  101. u32 delta = xen_clocksource_read() - start;
  102. __spin_time_accum(delta, spinlock_stats.histo_spin_blocked);
  103. spinlock_stats.time_blocked += delta;
  104. }
  105. #else /* !CONFIG_XEN_DEBUG_FS */
  106. static inline void add_stats(enum xen_contention_stat var, u32 val)
  107. {
  108. }
  109. static inline u64 spin_time_start(void)
  110. {
  111. return 0;
  112. }
  113. static inline void spin_time_accum_blocked(u64 start)
  114. {
  115. }
  116. #endif /* CONFIG_XEN_DEBUG_FS */
  117. struct xen_lock_waiting {
  118. struct arch_spinlock *lock;
  119. __ticket_t want;
  120. };
  121. static DEFINE_PER_CPU(struct xen_lock_waiting, lock_waiting);
  122. static cpumask_t waiting_cpus;
  123. __visible void xen_lock_spinning(struct arch_spinlock *lock, __ticket_t want)
  124. {
  125. int irq = __this_cpu_read(lock_kicker_irq);
  126. struct xen_lock_waiting *w = this_cpu_ptr(&lock_waiting);
  127. int cpu = smp_processor_id();
  128. u64 start;
  129. __ticket_t head;
  130. unsigned long flags;
  131. /* If kicker interrupts not initialized yet, just spin */
  132. if (irq == -1)
  133. return;
  134. start = spin_time_start();
  135. /*
  136. * Make sure an interrupt handler can't upset things in a
  137. * partially setup state.
  138. */
  139. local_irq_save(flags);
  140. /*
  141. * We don't really care if we're overwriting some other
  142. * (lock,want) pair, as that would mean that we're currently
  143. * in an interrupt context, and the outer context had
  144. * interrupts enabled. That has already kicked the VCPU out
  145. * of xen_poll_irq(), so it will just return spuriously and
  146. * retry with newly setup (lock,want).
  147. *
  148. * The ordering protocol on this is that the "lock" pointer
  149. * may only be set non-NULL if the "want" ticket is correct.
  150. * If we're updating "want", we must first clear "lock".
  151. */
  152. w->lock = NULL;
  153. smp_wmb();
  154. w->want = want;
  155. smp_wmb();
  156. w->lock = lock;
  157. /* This uses set_bit, which atomic and therefore a barrier */
  158. cpumask_set_cpu(cpu, &waiting_cpus);
  159. add_stats(TAKEN_SLOW, 1);
  160. /* clear pending */
  161. xen_clear_irq_pending(irq);
  162. /* Only check lock once pending cleared */
  163. barrier();
  164. /*
  165. * Mark entry to slowpath before doing the pickup test to make
  166. * sure we don't deadlock with an unlocker.
  167. */
  168. __ticket_enter_slowpath(lock);
  169. /* make sure enter_slowpath, which is atomic does not cross the read */
  170. smp_mb__after_atomic();
  171. /*
  172. * check again make sure it didn't become free while
  173. * we weren't looking
  174. */
  175. head = READ_ONCE(lock->tickets.head);
  176. if (__tickets_equal(head, want)) {
  177. add_stats(TAKEN_SLOW_PICKUP, 1);
  178. goto out;
  179. }
  180. /* Allow interrupts while blocked */
  181. local_irq_restore(flags);
  182. /*
  183. * If an interrupt happens here, it will leave the wakeup irq
  184. * pending, which will cause xen_poll_irq() to return
  185. * immediately.
  186. */
  187. /* Block until irq becomes pending (or perhaps a spurious wakeup) */
  188. xen_poll_irq(irq);
  189. add_stats(TAKEN_SLOW_SPURIOUS, !xen_test_irq_pending(irq));
  190. local_irq_save(flags);
  191. kstat_incr_irq_this_cpu(irq);
  192. out:
  193. cpumask_clear_cpu(cpu, &waiting_cpus);
  194. w->lock = NULL;
  195. local_irq_restore(flags);
  196. spin_time_accum_blocked(start);
  197. }
  198. PV_CALLEE_SAVE_REGS_THUNK(xen_lock_spinning);
  199. static void xen_unlock_kick(struct arch_spinlock *lock, __ticket_t next)
  200. {
  201. int cpu;
  202. add_stats(RELEASED_SLOW, 1);
  203. for_each_cpu(cpu, &waiting_cpus) {
  204. const struct xen_lock_waiting *w = &per_cpu(lock_waiting, cpu);
  205. /* Make sure we read lock before want */
  206. if (READ_ONCE(w->lock) == lock &&
  207. READ_ONCE(w->want) == next) {
  208. add_stats(RELEASED_SLOW_KICKED, 1);
  209. xen_send_IPI_one(cpu, XEN_SPIN_UNLOCK_VECTOR);
  210. break;
  211. }
  212. }
  213. }
  214. #endif /* CONFIG_QUEUED_SPINLOCKS */
  215. static irqreturn_t dummy_handler(int irq, void *dev_id)
  216. {
  217. BUG();
  218. return IRQ_HANDLED;
  219. }
  220. void xen_init_lock_cpu(int cpu)
  221. {
  222. int irq;
  223. char *name;
  224. if (!xen_pvspin)
  225. return;
  226. WARN(per_cpu(lock_kicker_irq, cpu) >= 0, "spinlock on CPU%d exists on IRQ%d!\n",
  227. cpu, per_cpu(lock_kicker_irq, cpu));
  228. name = kasprintf(GFP_KERNEL, "spinlock%d", cpu);
  229. irq = bind_ipi_to_irqhandler(XEN_SPIN_UNLOCK_VECTOR,
  230. cpu,
  231. dummy_handler,
  232. IRQF_PERCPU|IRQF_NOBALANCING,
  233. name,
  234. NULL);
  235. if (irq >= 0) {
  236. disable_irq(irq); /* make sure it's never delivered */
  237. per_cpu(lock_kicker_irq, cpu) = irq;
  238. per_cpu(irq_name, cpu) = name;
  239. }
  240. printk("cpu %d spinlock event irq %d\n", cpu, irq);
  241. }
  242. void xen_uninit_lock_cpu(int cpu)
  243. {
  244. if (!xen_pvspin)
  245. return;
  246. unbind_from_irqhandler(per_cpu(lock_kicker_irq, cpu), NULL);
  247. per_cpu(lock_kicker_irq, cpu) = -1;
  248. kfree(per_cpu(irq_name, cpu));
  249. per_cpu(irq_name, cpu) = NULL;
  250. }
  251. /*
  252. * Our init of PV spinlocks is split in two init functions due to us
  253. * using paravirt patching and jump labels patching and having to do
  254. * all of this before SMP code is invoked.
  255. *
  256. * The paravirt patching needs to be done _before_ the alternative asm code
  257. * is started, otherwise we would not patch the core kernel code.
  258. */
  259. void __init xen_init_spinlocks(void)
  260. {
  261. if (!xen_pvspin) {
  262. printk(KERN_DEBUG "xen: PV spinlocks disabled\n");
  263. return;
  264. }
  265. printk(KERN_DEBUG "xen: PV spinlocks enabled\n");
  266. #ifdef CONFIG_QUEUED_SPINLOCKS
  267. __pv_init_lock_hash();
  268. pv_lock_ops.queued_spin_lock_slowpath = __pv_queued_spin_lock_slowpath;
  269. pv_lock_ops.queued_spin_unlock = PV_CALLEE_SAVE(__pv_queued_spin_unlock);
  270. pv_lock_ops.wait = xen_qlock_wait;
  271. pv_lock_ops.kick = xen_qlock_kick;
  272. #else
  273. pv_lock_ops.lock_spinning = PV_CALLEE_SAVE(xen_lock_spinning);
  274. pv_lock_ops.unlock_kick = xen_unlock_kick;
  275. #endif
  276. }
  277. /*
  278. * While the jump_label init code needs to happend _after_ the jump labels are
  279. * enabled and before SMP is started. Hence we use pre-SMP initcall level
  280. * init. We cannot do it in xen_init_spinlocks as that is done before
  281. * jump labels are activated.
  282. */
  283. static __init int xen_init_spinlocks_jump(void)
  284. {
  285. if (!xen_pvspin)
  286. return 0;
  287. if (!xen_domain())
  288. return 0;
  289. static_key_slow_inc(&paravirt_ticketlocks_enabled);
  290. return 0;
  291. }
  292. early_initcall(xen_init_spinlocks_jump);
  293. static __init int xen_parse_nopvspin(char *arg)
  294. {
  295. xen_pvspin = false;
  296. return 0;
  297. }
  298. early_param("xen_nopvspin", xen_parse_nopvspin);
  299. #if defined(CONFIG_XEN_DEBUG_FS) && !defined(CONFIG_QUEUED_SPINLOCKS)
  300. static struct dentry *d_spin_debug;
  301. static int __init xen_spinlock_debugfs(void)
  302. {
  303. struct dentry *d_xen = xen_init_debugfs();
  304. if (d_xen == NULL)
  305. return -ENOMEM;
  306. if (!xen_pvspin)
  307. return 0;
  308. d_spin_debug = debugfs_create_dir("spinlocks", d_xen);
  309. debugfs_create_u8("zero_stats", 0644, d_spin_debug, &zero_stats);
  310. debugfs_create_u32("taken_slow", 0444, d_spin_debug,
  311. &spinlock_stats.contention_stats[TAKEN_SLOW]);
  312. debugfs_create_u32("taken_slow_pickup", 0444, d_spin_debug,
  313. &spinlock_stats.contention_stats[TAKEN_SLOW_PICKUP]);
  314. debugfs_create_u32("taken_slow_spurious", 0444, d_spin_debug,
  315. &spinlock_stats.contention_stats[TAKEN_SLOW_SPURIOUS]);
  316. debugfs_create_u32("released_slow", 0444, d_spin_debug,
  317. &spinlock_stats.contention_stats[RELEASED_SLOW]);
  318. debugfs_create_u32("released_slow_kicked", 0444, d_spin_debug,
  319. &spinlock_stats.contention_stats[RELEASED_SLOW_KICKED]);
  320. debugfs_create_u64("time_blocked", 0444, d_spin_debug,
  321. &spinlock_stats.time_blocked);
  322. debugfs_create_u32_array("histo_blocked", 0444, d_spin_debug,
  323. spinlock_stats.histo_spin_blocked, HISTO_BUCKETS + 1);
  324. return 0;
  325. }
  326. fs_initcall(xen_spinlock_debugfs);
  327. #endif /* CONFIG_XEN_DEBUG_FS */