spinlock.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. #include <asm/qspinlock.h>
  20. static void xen_qlock_kick(int cpu)
  21. {
  22. int irq = per_cpu(lock_kicker_irq, cpu);
  23. /* Don't kick if the target's kicker interrupt is not initialized. */
  24. if (irq == -1)
  25. return;
  26. xen_send_IPI_one(cpu, XEN_SPIN_UNLOCK_VECTOR);
  27. }
  28. /*
  29. * Halt the current CPU & release it back to the host
  30. */
  31. static void xen_qlock_wait(u8 *byte, u8 val)
  32. {
  33. int irq = __this_cpu_read(lock_kicker_irq);
  34. /* If kicker interrupts not initialized yet, just spin */
  35. if (irq == -1)
  36. return;
  37. /* clear pending */
  38. xen_clear_irq_pending(irq);
  39. barrier();
  40. /*
  41. * We check the byte value after clearing pending IRQ to make sure
  42. * that we won't miss a wakeup event because of the clearing.
  43. *
  44. * The sync_clear_bit() call in xen_clear_irq_pending() is atomic.
  45. * So it is effectively a memory barrier for x86.
  46. */
  47. if (READ_ONCE(*byte) != val)
  48. return;
  49. /*
  50. * If an interrupt happens here, it will leave the wakeup irq
  51. * pending, which will cause xen_poll_irq() to return
  52. * immediately.
  53. */
  54. /* Block until irq becomes pending (or perhaps a spurious wakeup) */
  55. xen_poll_irq(irq);
  56. }
  57. static irqreturn_t dummy_handler(int irq, void *dev_id)
  58. {
  59. BUG();
  60. return IRQ_HANDLED;
  61. }
  62. void xen_init_lock_cpu(int cpu)
  63. {
  64. int irq;
  65. char *name;
  66. if (!xen_pvspin)
  67. return;
  68. WARN(per_cpu(lock_kicker_irq, cpu) >= 0, "spinlock on CPU%d exists on IRQ%d!\n",
  69. cpu, per_cpu(lock_kicker_irq, cpu));
  70. name = kasprintf(GFP_KERNEL, "spinlock%d", cpu);
  71. irq = bind_ipi_to_irqhandler(XEN_SPIN_UNLOCK_VECTOR,
  72. cpu,
  73. dummy_handler,
  74. IRQF_PERCPU|IRQF_NOBALANCING,
  75. name,
  76. NULL);
  77. if (irq >= 0) {
  78. disable_irq(irq); /* make sure it's never delivered */
  79. per_cpu(lock_kicker_irq, cpu) = irq;
  80. per_cpu(irq_name, cpu) = name;
  81. }
  82. printk("cpu %d spinlock event irq %d\n", cpu, irq);
  83. }
  84. void xen_uninit_lock_cpu(int cpu)
  85. {
  86. if (!xen_pvspin)
  87. return;
  88. unbind_from_irqhandler(per_cpu(lock_kicker_irq, cpu), NULL);
  89. per_cpu(lock_kicker_irq, cpu) = -1;
  90. kfree(per_cpu(irq_name, cpu));
  91. per_cpu(irq_name, cpu) = NULL;
  92. }
  93. /*
  94. * Our init of PV spinlocks is split in two init functions due to us
  95. * using paravirt patching and jump labels patching and having to do
  96. * all of this before SMP code is invoked.
  97. *
  98. * The paravirt patching needs to be done _before_ the alternative asm code
  99. * is started, otherwise we would not patch the core kernel code.
  100. */
  101. void __init xen_init_spinlocks(void)
  102. {
  103. if (!xen_pvspin) {
  104. printk(KERN_DEBUG "xen: PV spinlocks disabled\n");
  105. return;
  106. }
  107. printk(KERN_DEBUG "xen: PV spinlocks enabled\n");
  108. __pv_init_lock_hash();
  109. pv_lock_ops.queued_spin_lock_slowpath = __pv_queued_spin_lock_slowpath;
  110. pv_lock_ops.queued_spin_unlock = PV_CALLEE_SAVE(__pv_queued_spin_unlock);
  111. pv_lock_ops.wait = xen_qlock_wait;
  112. pv_lock_ops.kick = xen_qlock_kick;
  113. }
  114. /*
  115. * While the jump_label init code needs to happend _after_ the jump labels are
  116. * enabled and before SMP is started. Hence we use pre-SMP initcall level
  117. * init. We cannot do it in xen_init_spinlocks as that is done before
  118. * jump labels are activated.
  119. */
  120. static __init int xen_init_spinlocks_jump(void)
  121. {
  122. if (!xen_pvspin)
  123. return 0;
  124. if (!xen_domain())
  125. return 0;
  126. static_key_slow_inc(&paravirt_ticketlocks_enabled);
  127. return 0;
  128. }
  129. early_initcall(xen_init_spinlocks_jump);
  130. static __init int xen_parse_nopvspin(char *arg)
  131. {
  132. xen_pvspin = false;
  133. return 0;
  134. }
  135. early_param("xen_nopvspin", xen_parse_nopvspin);