spinlock.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 = ACCESS_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. #define TIMEOUT (1 << 10)
  70. static inline void add_stats(enum xen_contention_stat var, u32 val)
  71. {
  72. }
  73. static inline u64 spin_time_start(void)
  74. {
  75. return 0;
  76. }
  77. static inline void spin_time_accum_blocked(u64 start)
  78. {
  79. }
  80. #endif /* CONFIG_XEN_DEBUG_FS */
  81. /*
  82. * Size struct xen_spinlock so it's the same as arch_spinlock_t.
  83. */
  84. #if NR_CPUS < 256
  85. typedef u8 xen_spinners_t;
  86. # define inc_spinners(xl) \
  87. asm(LOCK_PREFIX " incb %0" : "+m" ((xl)->spinners) : : "memory");
  88. # define dec_spinners(xl) \
  89. asm(LOCK_PREFIX " decb %0" : "+m" ((xl)->spinners) : : "memory");
  90. #else
  91. typedef u16 xen_spinners_t;
  92. # define inc_spinners(xl) \
  93. asm(LOCK_PREFIX " incw %0" : "+m" ((xl)->spinners) : : "memory");
  94. # define dec_spinners(xl) \
  95. asm(LOCK_PREFIX " decw %0" : "+m" ((xl)->spinners) : : "memory");
  96. #endif
  97. struct xen_lock_waiting {
  98. struct arch_spinlock *lock;
  99. __ticket_t want;
  100. };
  101. static DEFINE_PER_CPU(int, lock_kicker_irq) = -1;
  102. static DEFINE_PER_CPU(char *, irq_name);
  103. static DEFINE_PER_CPU(struct xen_lock_waiting, lock_waiting);
  104. static cpumask_t waiting_cpus;
  105. static void xen_lock_spinning(struct arch_spinlock *lock, __ticket_t want)
  106. {
  107. int irq = __this_cpu_read(lock_kicker_irq);
  108. struct xen_lock_waiting *w = &__get_cpu_var(lock_waiting);
  109. int cpu = smp_processor_id();
  110. u64 start;
  111. unsigned long flags;
  112. /* If kicker interrupts not initialized yet, just spin */
  113. if (irq == -1)
  114. return;
  115. start = spin_time_start();
  116. /*
  117. * Make sure an interrupt handler can't upset things in a
  118. * partially setup state.
  119. */
  120. local_irq_save(flags);
  121. w->want = want;
  122. smp_wmb();
  123. w->lock = lock;
  124. /* This uses set_bit, which atomic and therefore a barrier */
  125. cpumask_set_cpu(cpu, &waiting_cpus);
  126. add_stats(TAKEN_SLOW, 1);
  127. /* clear pending */
  128. xen_clear_irq_pending(irq);
  129. /* Only check lock once pending cleared */
  130. barrier();
  131. /* check again make sure it didn't become free while
  132. we weren't looking */
  133. if (ACCESS_ONCE(lock->tickets.head) == want) {
  134. add_stats(TAKEN_SLOW_PICKUP, 1);
  135. goto out;
  136. }
  137. /* Block until irq becomes pending (or perhaps a spurious wakeup) */
  138. xen_poll_irq(irq);
  139. add_stats(TAKEN_SLOW_SPURIOUS, !xen_test_irq_pending(irq));
  140. kstat_incr_irqs_this_cpu(irq, irq_to_desc(irq));
  141. out:
  142. cpumask_clear_cpu(cpu, &waiting_cpus);
  143. w->lock = NULL;
  144. local_irq_restore(flags);
  145. spin_time_accum_blocked(start);
  146. }
  147. static void xen_unlock_kick(struct arch_spinlock *lock, __ticket_t next)
  148. {
  149. int cpu;
  150. add_stats(RELEASED_SLOW, 1);
  151. for_each_cpu(cpu, &waiting_cpus) {
  152. const struct xen_lock_waiting *w = &per_cpu(lock_waiting, cpu);
  153. if (w->lock == lock && w->want == next) {
  154. add_stats(RELEASED_SLOW_KICKED, 1);
  155. xen_send_IPI_one(cpu, XEN_SPIN_UNLOCK_VECTOR);
  156. break;
  157. }
  158. }
  159. }
  160. static irqreturn_t dummy_handler(int irq, void *dev_id)
  161. {
  162. BUG();
  163. return IRQ_HANDLED;
  164. }
  165. void xen_init_lock_cpu(int cpu)
  166. {
  167. int irq;
  168. char *name;
  169. WARN(per_cpu(lock_kicker_irq, cpu) >= 0, "spinlock on CPU%d exists on IRQ%d!\n",
  170. cpu, per_cpu(lock_kicker_irq, cpu));
  171. /*
  172. * See git commit f10cd522c5fbfec9ae3cc01967868c9c2401ed23
  173. * (xen: disable PV spinlocks on HVM)
  174. */
  175. if (xen_hvm_domain())
  176. return;
  177. name = kasprintf(GFP_KERNEL, "spinlock%d", cpu);
  178. irq = bind_ipi_to_irqhandler(XEN_SPIN_UNLOCK_VECTOR,
  179. cpu,
  180. dummy_handler,
  181. IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING,
  182. name,
  183. NULL);
  184. if (irq >= 0) {
  185. disable_irq(irq); /* make sure it's never delivered */
  186. per_cpu(lock_kicker_irq, cpu) = irq;
  187. per_cpu(irq_name, cpu) = name;
  188. }
  189. printk("cpu %d spinlock event irq %d\n", cpu, irq);
  190. }
  191. void xen_uninit_lock_cpu(int cpu)
  192. {
  193. /*
  194. * See git commit f10cd522c5fbfec9ae3cc01967868c9c2401ed23
  195. * (xen: disable PV spinlocks on HVM)
  196. */
  197. if (xen_hvm_domain())
  198. return;
  199. unbind_from_irqhandler(per_cpu(lock_kicker_irq, cpu), NULL);
  200. per_cpu(lock_kicker_irq, cpu) = -1;
  201. kfree(per_cpu(irq_name, cpu));
  202. per_cpu(irq_name, cpu) = NULL;
  203. }
  204. static bool xen_pvspin __initdata = true;
  205. void __init xen_init_spinlocks(void)
  206. {
  207. /*
  208. * See git commit f10cd522c5fbfec9ae3cc01967868c9c2401ed23
  209. * (xen: disable PV spinlocks on HVM)
  210. */
  211. if (xen_hvm_domain())
  212. return;
  213. if (!xen_pvspin) {
  214. printk(KERN_DEBUG "xen: PV spinlocks disabled\n");
  215. return;
  216. }
  217. pv_lock_ops.lock_spinning = xen_lock_spinning;
  218. pv_lock_ops.unlock_kick = xen_unlock_kick;
  219. }
  220. static __init int xen_parse_nopvspin(char *arg)
  221. {
  222. xen_pvspin = false;
  223. return 0;
  224. }
  225. early_param("xen_nopvspin", xen_parse_nopvspin);
  226. #ifdef CONFIG_XEN_DEBUG_FS
  227. static struct dentry *d_spin_debug;
  228. static int __init xen_spinlock_debugfs(void)
  229. {
  230. struct dentry *d_xen = xen_init_debugfs();
  231. if (d_xen == NULL)
  232. return -ENOMEM;
  233. d_spin_debug = debugfs_create_dir("spinlocks", d_xen);
  234. debugfs_create_u8("zero_stats", 0644, d_spin_debug, &zero_stats);
  235. debugfs_create_u32("taken_slow", 0444, d_spin_debug,
  236. &spinlock_stats.contention_stats[TAKEN_SLOW]);
  237. debugfs_create_u32("taken_slow_pickup", 0444, d_spin_debug,
  238. &spinlock_stats.contention_stats[TAKEN_SLOW_PICKUP]);
  239. debugfs_create_u32("taken_slow_spurious", 0444, d_spin_debug,
  240. &spinlock_stats.contention_stats[TAKEN_SLOW_SPURIOUS]);
  241. debugfs_create_u32("released_slow", 0444, d_spin_debug,
  242. &spinlock_stats.contention_stats[RELEASED_SLOW]);
  243. debugfs_create_u32("released_slow_kicked", 0444, d_spin_debug,
  244. &spinlock_stats.contention_stats[RELEASED_SLOW_KICKED]);
  245. debugfs_create_u64("time_blocked", 0444, d_spin_debug,
  246. &spinlock_stats.time_blocked);
  247. debugfs_create_u32_array("histo_blocked", 0444, d_spin_debug,
  248. spinlock_stats.histo_spin_blocked, HISTO_BUCKETS + 1);
  249. return 0;
  250. }
  251. fs_initcall(xen_spinlock_debugfs);
  252. #endif /* CONFIG_XEN_DEBUG_FS */