hv_spinlock.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Hyper-V specific spinlock code.
  4. *
  5. * Copyright (C) 2018, Intel, Inc.
  6. *
  7. * Author : Yi Sun <yi.y.sun@intel.com>
  8. */
  9. #define pr_fmt(fmt) "Hyper-V: " fmt
  10. #include <linux/spinlock.h>
  11. #include <asm/mshyperv.h>
  12. #include <asm/paravirt.h>
  13. #include <asm/apic.h>
  14. static bool __initdata hv_pvspin = true;
  15. static void hv_qlock_kick(int cpu)
  16. {
  17. apic->send_IPI(cpu, X86_PLATFORM_IPI_VECTOR);
  18. }
  19. static void hv_qlock_wait(u8 *byte, u8 val)
  20. {
  21. unsigned long msr_val;
  22. unsigned long flags;
  23. if (in_nmi())
  24. return;
  25. /*
  26. * Reading HV_X64_MSR_GUEST_IDLE MSR tells the hypervisor that the
  27. * vCPU can be put into 'idle' state. This 'idle' state is
  28. * terminated by an IPI, usually from hv_qlock_kick(), even if
  29. * interrupts are disabled on the vCPU.
  30. *
  31. * To prevent a race against the unlock path it is required to
  32. * disable interrupts before accessing the HV_X64_MSR_GUEST_IDLE
  33. * MSR. Otherwise, if the IPI from hv_qlock_kick() arrives between
  34. * the lock value check and the rdmsrl() then the vCPU might be put
  35. * into 'idle' state by the hypervisor and kept in that state for
  36. * an unspecified amount of time.
  37. */
  38. local_irq_save(flags);
  39. /*
  40. * Only issue the rdmsrl() when the lock state has not changed.
  41. */
  42. if (READ_ONCE(*byte) == val)
  43. rdmsrl(HV_X64_MSR_GUEST_IDLE, msr_val);
  44. local_irq_restore(flags);
  45. }
  46. /*
  47. * Hyper-V does not support this so far.
  48. */
  49. bool hv_vcpu_is_preempted(int vcpu)
  50. {
  51. return false;
  52. }
  53. PV_CALLEE_SAVE_REGS_THUNK(hv_vcpu_is_preempted);
  54. void __init hv_init_spinlocks(void)
  55. {
  56. if (!hv_pvspin || !apic ||
  57. !(ms_hyperv.hints & HV_X64_CLUSTER_IPI_RECOMMENDED) ||
  58. !(ms_hyperv.features & HV_X64_MSR_GUEST_IDLE_AVAILABLE)) {
  59. pr_info("PV spinlocks disabled\n");
  60. return;
  61. }
  62. pr_info("PV spinlocks enabled\n");
  63. __pv_init_lock_hash();
  64. pv_ops.lock.queued_spin_lock_slowpath = __pv_queued_spin_lock_slowpath;
  65. pv_ops.lock.queued_spin_unlock = PV_CALLEE_SAVE(__pv_queued_spin_unlock);
  66. pv_ops.lock.wait = hv_qlock_wait;
  67. pv_ops.lock.kick = hv_qlock_kick;
  68. pv_ops.lock.vcpu_is_preempted = PV_CALLEE_SAVE(hv_vcpu_is_preempted);
  69. }
  70. static __init int hv_parse_nopvspin(char *arg)
  71. {
  72. hv_pvspin = false;
  73. return 0;
  74. }
  75. early_param("hv_nopvspin", hv_parse_nopvspin);