hv_spinlock.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 <[email protected]>
  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 flags;
  22. if (in_nmi())
  23. return;
  24. /*
  25. * Reading HV_X64_MSR_GUEST_IDLE MSR tells the hypervisor that the
  26. * vCPU can be put into 'idle' state. This 'idle' state is
  27. * terminated by an IPI, usually from hv_qlock_kick(), even if
  28. * interrupts are disabled on the vCPU.
  29. *
  30. * To prevent a race against the unlock path it is required to
  31. * disable interrupts before accessing the HV_X64_MSR_GUEST_IDLE
  32. * MSR. Otherwise, if the IPI from hv_qlock_kick() arrives between
  33. * the lock value check and the rdmsrl() then the vCPU might be put
  34. * into 'idle' state by the hypervisor and kept in that state for
  35. * an unspecified amount of time.
  36. */
  37. local_irq_save(flags);
  38. /*
  39. * Only issue the rdmsrl() when the lock state has not changed.
  40. */
  41. if (READ_ONCE(*byte) == val) {
  42. unsigned long msr_val;
  43. rdmsrl(HV_X64_MSR_GUEST_IDLE, msr_val);
  44. (void)msr_val;
  45. }
  46. local_irq_restore(flags);
  47. }
  48. /*
  49. * Hyper-V does not support this so far.
  50. */
  51. __visible bool hv_vcpu_is_preempted(int vcpu)
  52. {
  53. return false;
  54. }
  55. PV_CALLEE_SAVE_REGS_THUNK(hv_vcpu_is_preempted);
  56. void __init hv_init_spinlocks(void)
  57. {
  58. if (!hv_pvspin || !apic ||
  59. !(ms_hyperv.hints & HV_X64_CLUSTER_IPI_RECOMMENDED) ||
  60. !(ms_hyperv.features & HV_MSR_GUEST_IDLE_AVAILABLE)) {
  61. pr_info("PV spinlocks disabled\n");
  62. return;
  63. }
  64. pr_info("PV spinlocks enabled\n");
  65. __pv_init_lock_hash();
  66. pv_ops.lock.queued_spin_lock_slowpath = __pv_queued_spin_lock_slowpath;
  67. pv_ops.lock.queued_spin_unlock = PV_CALLEE_SAVE(__pv_queued_spin_unlock);
  68. pv_ops.lock.wait = hv_qlock_wait;
  69. pv_ops.lock.kick = hv_qlock_kick;
  70. pv_ops.lock.vcpu_is_preempted = PV_CALLEE_SAVE(hv_vcpu_is_preempted);
  71. }
  72. static __init int hv_parse_nopvspin(char *arg)
  73. {
  74. hv_pvspin = false;
  75. return 0;
  76. }
  77. early_param("hv_nopvspin", hv_parse_nopvspin);