spinlock.c 8.3 KB

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