1 // SPDX-License-Identifier: GPL-2.0
4 * Hyper-V specific spinlock code.
6 * Copyright (C) 2018, Intel, Inc.
11 #define pr_fmt(fmt) "Hyper-V: " fmt
13 #include <linux/spinlock.h>
15 #include <asm/mshyperv.h>
16 #include <asm/paravirt.h>
19 static bool __initdata hv_pvspin = true;
21 static void hv_qlock_kick(int cpu)
23 apic->send_IPI(cpu, X86_PLATFORM_IPI_VECTOR);
26 static void hv_qlock_wait(u8 *byte, u8 val)
34 * Reading HV_X64_MSR_GUEST_IDLE MSR tells the hypervisor that the
35 * vCPU can be put into 'idle' state. This 'idle' state is
36 * terminated by an IPI, usually from hv_qlock_kick(), even if
37 * interrupts are disabled on the vCPU.
39 * To prevent a race against the unlock path it is required to
40 * disable interrupts before accessing the HV_X64_MSR_GUEST_IDLE
41 * MSR. Otherwise, if the IPI from hv_qlock_kick() arrives between
42 * the lock value check and the rdmsrl() then the vCPU might be put
43 * into 'idle' state by the hypervisor and kept in that state for
44 * an unspecified amount of time.
46 local_irq_save(flags);
48 * Only issue the rdmsrl() when the lock state has not changed.
50 if (READ_ONCE(*byte) == val) {
51 unsigned long msr_val;
53 rdmsrl(HV_X64_MSR_GUEST_IDLE, msr_val);
57 local_irq_restore(flags);
61 * Hyper-V does not support this so far.
63 __visible bool hv_vcpu_is_preempted(int vcpu)
67 PV_CALLEE_SAVE_REGS_THUNK(hv_vcpu_is_preempted);
69 void __init hv_init_spinlocks(void)
71 if (!hv_pvspin || !apic ||
72 !(ms_hyperv.hints & HV_X64_CLUSTER_IPI_RECOMMENDED) ||
73 !(ms_hyperv.features & HV_MSR_GUEST_IDLE_AVAILABLE)) {
74 pr_info("PV spinlocks disabled\n");
77 pr_info("PV spinlocks enabled\n");
79 __pv_init_lock_hash();
80 pv_ops.lock.queued_spin_lock_slowpath = __pv_queued_spin_lock_slowpath;
81 pv_ops.lock.queued_spin_unlock = PV_CALLEE_SAVE(__pv_queued_spin_unlock);
82 pv_ops.lock.wait = hv_qlock_wait;
83 pv_ops.lock.kick = hv_qlock_kick;
84 pv_ops.lock.vcpu_is_preempted = PV_CALLEE_SAVE(hv_vcpu_is_preempted);
87 static __init int hv_parse_nopvspin(char *arg)
92 early_param("hv_nopvspin", hv_parse_nopvspin);