spinlock.c 9.6 KB

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