spinlock.c 3.6 KB

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