spinlock.c 3.6 KB

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