KVM: Add virtualization support of split lock detection
Subsystems (5)
v9 cover KVM: Add virtualization support of split lock detection
This series aims to add the virtualization of split lock detection in KVM.
Due to the fact that split lock detection is tightly coupled with CPU model and CPU model is configurable by host VMM, we elect to use paravirt method to expose and enumerate it for guest.
Changes in v9
- rebase to v5.7-rc4
- Add one patch to rename TIF_SLD to TIF_SLD_DISABLED;
- Add one patch to remove bogus case in handle_guest_split_lock;
- Introduce flag X86_FEATURE_SPLIT_LOCK_DETECT_FATAL and thus drop sld_state;
- Use X86_FEATURE_SPLIT_LOCK_DETECT and X86_FEATURE_SPLIT_LOCK_DETECT_FATAL to determine the SLD state of host;
- Introduce split_lock_virt_switch() and two wrappers for KVM instead of sld_update_to();
- Use paravirt to expose and enumerate split lock detection for guest;
- Split lock detection can be exposed to guest when host is sld_fatal, even though host is SMT available.
Changes in v8:
https://lkml.kernel.org/r/[email protected]
- rebase to v5.7-rc1.
- basic enabling of split lock detection already merged.
- When host is sld_warn and nosmt, load guest's sld bit when in KVM context, i.e., between vmx_prepare_switch_to_guest() and before vmx_prepare_switch_to_host(), KVM uses guest sld setting.
Changes in v7:
https://lkml.kernel.org/r/[email protected]
- only pick patch 1 and patch 2, and hold all the left.
- Update SLD bit on each processor based on sld_state.
Changes in v6:
https://lkml.kernel.org/r/[email protected]
- Drop the sld_not_exist flag and use X86_FEATURE_SPLIT_LOCK_DETECT to check whether need to init split lock detection. [tglx]
- Use tglx's method to verify the existence of split lock detectoin.
- small optimization of sld_update_msr() that the default value of msr_test_ctrl_cache has split_lock_detect bit cleared.
- Drop the patch3 in v5 that introducing kvm_only option. [tglx]
- Rebase patch4-8 to kvm/queue.
- use the new kvm-cpu-cap to expose X86_FEATURE_CORE_CAPABILITIES in Patch 6.
Changes in v5:
https://lkml.kernel.org/r/[email protected]
- Use X86_FEATURE_SPLIT_LOCK_DETECT flag in kvm to ensure split lock detection is really supported.
- Add and export sld related helper functions in their related usecase kvm patches.
Xiaoyao Li (8):
x86/split_lock: Rename TIF_SLD to TIF_SLD_DISABLED x86/split_lock: Remove bogus case in handle_guest_split_lock() x86/split_lock: Introduce flag X86_FEATURE_SLD_FATAL and drop
sld_state
x86/split_lock: Introduce split_lock_virt_switch() and two wrappers x86/kvm: Introduce paravirt split lock detection enumeration
KVM: VMX: Enable MSR TEST_CTRL for guest
KVM: VMX: virtualize split lock detection x86/split_lock: Enable split lock detection initialization when
running as an guest on KVM
Documentation/virt/kvm/cpuid.rst | 29 +++++++---- arch/x86/include/asm/cpu.h | 35 ++++++++++++++ arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/thread_info.h | 6 +-- arch/x86/include/uapi/asm/kvm_para.h | 8 ++-- arch/x86/kernel/cpu/intel.c | 59 ++++++++++++++++------- arch/x86/kernel/kvm.c | 3 ++ arch/x86/kernel/process.c | 2 +- arch/x86/kvm/cpuid.c | 6 +++ arch/x86/kvm/vmx/vmx.c | 72 +++++++++++++++++++++++++--- arch/x86/kvm/vmx/vmx.h | 3 ++ arch/x86/kvm/x86.c | 6 ++- arch/x86/kvm/x86.h | 7 +++ 13 files changed, 196 insertions(+), 41 deletions(-)
Signature · 1 line
2.18.2
PATCH v9 1/8 x86/split_lock: Rename TIF_SLD to TIF_SLD_DISABLED
TIF_SLD can only be set if a user space thread hits split lock and sld_state == sld_warn. This flag is set to indicate SLD (split lock detection) is turned off for the thread, so rename it to TIF_SLD_DISABLED, which is pretty self explaining.
--- arch/x86/include/asm/thread_info.h | 6 +++--- arch/x86/kernel/cpu/intel.c | 6 +++--- arch/x86/kernel/process.c | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-)
arch/x86/include/asm/thread_info.h +3 -3
@@ -92,7 +92,7 @@ struct thread_info {#define TIF_NOCPUID 15/* CPUID is not accessible in userland */ #define TIF_NOTSC 16/* TSC is not accessible in userland */ #define TIF_IA32 17/* IA32 compatibility process */ -#define TIF_SLD 18/* Restore split lock detection on context switch */ +#define TIF_SLD_DISABLED 18/* split lock detection is turned off */ #define TIF_MEMDIE 20/* is terminating due to OOM killer */ #define TIF_POLLING_NRFLAG 21/* idle is polling for TIF_NEED_RESCHED */ #define TIF_IO_BITMAP 22/* uses I/O bitmap */
@@ -122,7 +122,7 @@ struct thread_info {#define _TIF_NOCPUID (1 << TIF_NOCPUID)#define _TIF_NOTSC (1 << TIF_NOTSC)#define _TIF_IA32 (1 << TIF_IA32) -#define _TIF_SLD (1 << TIF_SLD) +#define _TIF_SLD_DISABLED (1 << TIF_SLD_DISABLED)#define _TIF_POLLING_NRFLAG (1 << TIF_POLLING_NRFLAG)#define _TIF_IO_BITMAP (1 << TIF_IO_BITMAP)#define _TIF_FORCED_TF (1 << TIF_FORCED_TF)
@@ -141,7 +141,7 @@ struct thread_info {/* flags to check in __switch_to() */ #define _TIF_WORK_CTXSW_BASE \ (_TIF_NOCPUID | _TIF_NOTSC | _TIF_BLOCKSTEP | \ - _TIF_SSBD | _TIF_SPEC_FORCE_UPDATE | _TIF_SLD) + _TIF_SSBD | _TIF_SPEC_FORCE_UPDATE | _TIF_SLD_DISABLED)/* * Avoid calls to __switch_to_xtra() on UP as STIBP is not evaluated.
arch/x86/kernel/cpu/intel.c +3 -3
@@ -1074,11 +1074,11 @@ static void split_lock_warn(unsigned long ip)/* * Disable the split lock detection for this task so it can make -* progress and set TIF_SLD so the detection is re-enabled via +* progress and set TIF_SLD_DISABLED so the detection is re-enabled via * switch_to_sld() when the task is scheduled out. */ sld_update_msr (false); -set_tsk_thread_flag (current,TIF_SLD ); +set_tsk_thread_flag (current,TIF_SLD_DISABLED ); }bool handle_guest_split_lock (unsigned long ip)
@@ -1116,7 +1116,7 @@ bool handle_user_split_lock(struct pt_regs *regs, long error_code)*/ void switch_to_sld (unsigned long tifn) { -sld_update_msr (!(tifn & _TIF_SLD)); +sld_update_msr (!(tifn & _TIF_SLD_DISABLED)); }/*
arch/x86/kernel/process.c +1 -1
@@ -650,7 +650,7 @@ void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p)__speculation_ctrl_update (~tifn, tifn); } -if ((tifp ^ tifn) & _TIF_SLD) +if ((tifp ^ tifn) & _TIF_SLD_DISABLED)switch_to_sld (tifn); }
Signature · 1 line
2.18.2
PATCH v9 2/8 x86/split_lock: Remove bogus case in handle_guest_split_lock()
In reply to Xiaoyao LiThe bogus case can never happen, i.e., when sld_state == sld_off, guest won't trigger split lock #AC and of course no handle_guest_split_lock() will be called.
Beside, drop bogus case also makes future patch easier to remove sld_state if we reach the alignment that it must be sld_warn or sld_fatal when handle_guest_split_lock() is called.
---
The alternative would be to remove the "SLD enabled" check from KVM so that a truly unexpected/bogus #AC would generate a warn. It's not clear whether or not calling handle_guest_split_lock() iff SLD is enabled was intended in the long term.
--- arch/x86/kernel/cpu/intel.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
arch/x86/kernel/cpu/intel.c +2 -3
@@ -1088,9 +1088,8 @@ bool handle_guest_split_lock(unsigned long ip)return true; } -pr_warn_once ("#AC: %s/%d %s split_lock trap at address: 0x%lx\n" , - current->comm, current->pid, - sld_state == sld_fatal ?"fatal" :"bogus" , ip); +pr_warn_once ("#AC: %s/%d fatal split_lock trap at address: 0x%lx\n" , + current->comm, current->pid, ip); current->thread.error_code =0 ; current->thread.trap_nr =X86_TRAP_AC ;
Signature · 1 line
2.18.2
PATCH v9 3/8 x86/split_lock: Introduce flag X86_FEATURE_SLD_FATAL and drop sld_state
In reply to Xiaoyao LiIntroduce a synthetic feature flag X86_FEATURE_SLD_FATAL, which means kernel is in sld_fatal mode if set.
Now sld_state is not needed any more that the state of SLD can be inferred from X86_FEATURE_SPLIT_LOCK_DETECT and X86_FEATURE_SLD_FATAL.
--- arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/kernel/cpu/intel.c | 16 ++++++---------- 2 files changed, 7 insertions(+), 10 deletions(-)
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -286,6 +286,7 @@#define X86_FEATURE_FENCE_SWAPGS_USER (11*32+ 4)/* "" LFENCE in user entry SWAPGS path */ #define X86_FEATURE_FENCE_SWAPGS_KERNEL (11*32+ 5)/* "" LFENCE in kernel entry SWAPGS path */ #define X86_FEATURE_SPLIT_LOCK_DETECT (11*32+ 6)/* #AC for split lock */ +#define X86_FEATURE_SLD_FATAL (11*32+ 7)/* split lock detection in fatal mode */ /* Intel-defined CPU features, CPUID level 0x00000007:1 (EAX), word 12 */ #define X86_FEATURE_AVX512_BF16 (12*32+ 5)/* AVX512 BFLOAT16 instructions */
arch/x86/kernel/cpu/intel.c +6 -10
@@ -40,12 +40,6 @@ enum split_lock_detect_state { sld_fatal, }; -/* -* Default to sld_off because most systems do not support split lock detection -* split_lock_setup() will switch this to sld_warn on systems that support -* split lock detect, unless there is a command line override. -*/ -static enum split_lock_detect_state sld_state__ro_after_init = sld_off;static u64 msr_test_ctrl_cache__ro_after_init ;/*
@@ -1043,8 +1037,9 @@ static void __init split_lock_setup(void)return ; } - sld_state = state;setup_force_cpu_cap (X86_FEATURE_SPLIT_LOCK_DETECT ); +if (state == sld_fatal) +setup_force_cpu_cap (X86_FEATURE_SLD_FATAL ); }/*
@@ -1064,7 +1059,7 @@ static void sld_update_msr(bool on)static void split_lock_init (void ) { -split_lock_verify_msr (sld_state != sld_off); +split_lock_verify_msr (boot_cpu_has (X86_FEATURE_SPLIT_LOCK_DETECT )); }static void split_lock_warn (unsigned long ip)
@@ -1083,7 +1078,7 @@ static void split_lock_warn(unsigned long ip)bool handle_guest_split_lock (unsigned long ip) { -if (sld_state == sld_warn) { +if (!boot_cpu_has (X86_FEATURE_SLD_FATAL )) {split_lock_warn (ip);return true; }
@@ -1100,7 +1095,8 @@ EXPORT_SYMBOL_GPL(handle_guest_split_lock);bool handle_user_split_lock (struct pt_regs *regs,long error_code) { -if ((regs->flags &X86_EFLAGS_AC ) || sld_state == sld_fatal) +if ((regs->flags &X86_EFLAGS_AC ) || +boot_cpu_has (X86_FEATURE_SLD_FATAL ))return false;split_lock_warn (regs->ip);return true;
Signature · 1 line
2.18.2
Re: [PATCH v9 3/8] x86/split_lock: Introduce flag X86_FEATURE_SLD_FATAL and drop sld_state
Quoted 6 lines · Xiaoyao Li
Introduce a synthetic feature flag
X86_FEATURE_SLD_FATAL, which means kernel is insld_fatalmode if set.Now
sld_stateis not needed any more that the state of SLD can be inferred fromX86_FEATURE_SPLIT_LOCK_DETECTandX86_FEATURE_SLD_FATAL.
Is it too much to ask for Intel to actually allocate and define a CPUID bit that means "this CPU *always* sends #AC on a split lock"? This would be a pure documentation change, but it would make this architectural rather than something that each hypervisor needs to hack up.
Meanwhile, I don't see why adding a cpufeature flag is worthwhile to avoid a less bizarre global variable. There's no performance issue here, and the old code looked a lot more comprehensible than the new code.
Re: [PATCH v9 3/8] x86/split_lock: Introduce flag X86_FEATURE_SLD_FATAL and drop sld_state
Quoted 5 lines · Andy Lutomirski
Quoted 4 lines · Xiaoyao Li
Introduce a synthetic feature flag
X86_FEATURE_SLD_FATAL, which means kernel is insld_fatalmode if set.
Quoted 8 lines
Quoted 2 lines
Now
sld_stateis not needed any more that the state of SLD can be inferred fromX86_FEATURE_SPLIT_LOCK_DETECTandX86_FEATURE_SLD_FATAL.Is it too much to ask for Intel to actually allocate and define a CPUID bit that means "this CPU *always* sends #AC on a split lock"? This would be a pure documentation change, but it would make this architectural rather than something that each hypervisor needs to hack up.
The original plan was to request a bit in MSR_TEST_CTRL be documented as such. Then we discovered that defining IA32_CORE_CAPABILITIES enumeration as architectural was an SDM bug[*]. At that point, enumerating SLD to a KVM guest through a KVM CPUID leaf is the least awful option. Emulating the model specific behavior doesn't provide userspace with a sane way to disable SLD for a guest, and emulating IA32_CORE_CAPABILITIES behavior would be tantamount to emulating model specific behavior.
Once paravirt is required for basic SLD enumeration, tacking on the "fatal" indicator is a minor blip.
I agree that having to reinvent the wheel for every hypervisor is completely ridiculous, but it provides well defined and controllable behavior. We could try to get two CPUID bits defined in the SDM, but pushing through all the bureaucracy that gates SDM changes means we wouldn't have a resolution for at least multiple months, assuming the proposal was even accepted.
[*] https://lkml.kernel.org/r/[email protected]
Quoted 4 lines
Meanwhile, I don't see why adding a cpufeature flag is worthwhile to avoid a less bizarre global variable. There's no performance issue here, and the old code looked a lot more comprehensible than the new code.
The flag has two main advantages:
- Automatically available to modules, i.e. KVM.
- Visible to userspace in /proc/cpuinfo.
Making the global variable available to KVM is ugly because it either requires exporting the variable and the enums (which gets especially nasty because kvm_intel can be built with CONFIG_CPU_SUP_INTEL=n), or requires adding a dedicated is_sld_fatal() wrapper and thus more exported crud.
And IMO, the feature flag is the less bizarre option once it's "public" knowledge, e.g. more in line with features that enumerate both basic support and sub-features via CPUID bits.
PATCH v9 4/8 x86/split_lock: Introduce split_lock_virt_switch() and two wrappers
In reply to Xiaoyao LiIntroduce split_lock_virt_switch(), which is used for toggling split lock detection setting as well as updating TIF_SLD_DISABLED flag to make them consistent. Note, it can only be used in sld warn mode, i.e., X86_FEATURE_SPLIT_LOCK_DETECT && !X86_FEATURE_SLD_FATAL.
The FATAL check is handled by wrappers, split_lock_set_guest() and split_lock_restore_host(), that will be used by KVM when virtualizing split lock detection for guest in the future.
--- arch/x86/include/asm/cpu.h | 33 +++++++++++++++++++++++++++++++++ arch/x86/kernel/cpu/intel.c | 20 ++++++++++++++++++++ 2 files changed, 53 insertions(+)
arch/x86/include/asm/cpu.h +33 -0
@@ -45,6 +45,7 @@ extern void __init cpu_set_core_cap_bits(struct cpuinfo_x86 *c);extern void switch_to_sld (unsigned long tifn);extern bool handle_user_split_lock (struct pt_regs *regs,long error_code);extern bool handle_guest_split_lock (unsigned long ip); +extern bool split_lock_virt_switch (bool on);#else static inline void __init cpu_set_core_cap_bits (struct cpuinfo_x86 *c) {}static inline void switch_to_sld (unsigned long tifn) {}
@@ -57,5 +58,37 @@ static inline bool handle_guest_split_lock(unsigned long ip) {return false; } + +static inline bool split_lock_virt_switch (bool on) {return false; }#endif + +/** +* split_lock_set_guest - Set SLD state for a guest +* @guest_sld_on: If SLD is on in the guest +* +* returns: %true if SLD was enabled in the task +* +* Must be called when X86_FEATURE_SPLIT_LOCK_DETECT is available. +*/ +static inline bool split_lock_set_guest (bool guest_sld_on) +{ +if (static_cpu_has (X86_FEATURE_SLD_FATAL )) +return true; + +return split_lock_virt_switch (guest_sld_on); +} + +/** +* split_lock_restore_host - Restore host SLD state +* @host_sld_on: If SLD is on in the host +* +* Must be called when X86_FEATURE_SPLIT_LOCK_DETECT is available. +*/ +static inline void split_lock_restore_host (bool host_sld_on) +{ +if (static_cpu_has (X86_FEATURE_SLD_FATAL )) +return ; + +split_lock_virt_switch (host_sld_on); +}#endif /* _ASM_X86_CPU_H */
arch/x86/kernel/cpu/intel.c +20 -0
@@ -1062,6 +1062,26 @@ static void split_lock_init(void)split_lock_verify_msr (boot_cpu_has (X86_FEATURE_SPLIT_LOCK_DETECT )); } +/* +* It should never be called directly but should use split_lock_set_guest() +* and split_lock_restore_host() instead. +* +* The caller needs to be in preemption disabled context to ensure +* MSR state and TIF_SLD_DISABLED state consistent. +*/ +bool split_lock_virt_switch (bool on) +{ +bool was_on = !test_thread_flag (TIF_SLD_DISABLED ); + +if (on != was_on) { +sld_update_msr (on); +update_thread_flag (TIF_SLD_DISABLED , !on); + } + +return was_on; +} +EXPORT_SYMBOL_GPL (split_lock_virt_switch); +static void split_lock_warn (unsigned long ip) { pr_warn_ratelimited("#AC: %s/%d took a split_lock trap at address: 0x%lx\n",
Signature · 1 line
2.18.2
PATCH v9 5/8 x86/kvm: Introduce paravirt split lock detection enumeration
In reply to Xiaoyao LiIntroduce KVM_FEATURE_SPLIT_LOCK_DETECT, for which linux guest running on KVM can enumerate the avaliablility of feature split lock detection.
Introduce KVM_HINTS_SLD_FATAL, which tells whether host is sld_fatal mode, i.e., whether split lock detection is forced on for guest vcpu.
--- Documentation/virt/kvm/cpuid.rst | 29 ++++++++++++++++++++-------- arch/x86/include/uapi/asm/kvm_para.h | 8 +++++--- 2 files changed, 26 insertions(+), 11 deletions(-)
Documentation/virt/kvm/cpuid.rst +21 -8
@@ -86,6 +86,12 @@ KVM_FEATURE_PV_SCHED_YIELD 13 guest checks this feature bit before using paravirtualized sched yield. +KVM_FEATURE_SPLIT_LOCK_DETECT 14 guest checks this feature bit for + available of split lock detection. + + KVM doesn't support enumerating + split lock detection via CPU model + KVM_FEATURE_CLOCSOURCE_STABLE_BIT 24 host will warn if no guest-side per-cpu warps are expeced in kvmclock
@@ -97,11 +103,18 @@ KVM_FEATURE_CLOCSOURCE_STABLE_BIT 24 host will warn if no guest-side Where ``flag`` here is defined as below: -================== ============ ================================= -flag value meaning -================== ============ ================================= -KVM_HINTS_REALTIME 0 guest checks this feature bit to - determine that vCPUs are never - preempted for an unlimited time - allowing optimizations -================== ============ ================================= +================================ ============ ================================= +flag value meaning +================================ ============ ================================= +KVM_HINTS_REALTIME 0 guest checks this feature bit to + determine that vCPUs are never + preempted for an unlimited time + allowing optimizations + +KVM_HINTS_SLD_FATAL 1 set if split lock detection is + forced on in the host, in which + case KVM will kill the guest if it + generates a split lock #AC with + SLD disabled from guest's + perspective +================================ ============ =================================
arch/x86/include/uapi/asm/kvm_para.h +5 -3
@@ -31,14 +31,16 @@#define KVM_FEATURE_PV_SEND_IPI 11#define KVM_FEATURE_POLL_CONTROL 12#define KVM_FEATURE_PV_SCHED_YIELD 13 - -#define KVM_HINTS_REALTIME 0 - +#define KVM_FEATURE_SPLIT_LOCK_DETECT 14/* The last 8 bits are used to indicate how to interpret the flags field * in pvclock structure. If no bits are set, all flags are ignored. */ #define KVM_FEATURE_CLOCKSOURCE_STABLE_BIT 24 +/* KVM feature hints in CPUID.0x40000001.EDX */ +#define KVM_HINTS_REALTIME 0 +#define KVM_HINTS_SLD_FATAL 1 +#define MSR_KVM_WALL_CLOCK 0x11#define MSR_KVM_SYSTEM_TIME 0x12
Signature · 1 line
2.18.2
PATCH v9 6/8 KVM: VMX: Enable MSR TEST_CTRL for guest
In reply to Xiaoyao LiUnconditionally allow the guest to read and zero-write MSR TEST_CTRL.
This matches the fact that most Intel CPUs support MSR TEST_CTRL, and it also alleviates the effort to handle wrmsr/rdmsr when split lock detection is exposed to the guest in a future patch.
--- arch/x86/kvm/vmx/vmx.c | 8 ++++++++ 1 file changed, 8 insertions(+)
arch/x86/kvm/vmx/vmx.c +8 -0
@@ -1789,6 +1789,9 @@ static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)u32 index;switch (msr_info->index) { +case MSR_TEST_CTRL : + msr_info->data =0 ; +break ;#ifdef CONFIG_X86_64 case MSR_FS_BASE : msr_info->data =vmcs_readl (GUEST_FS_BASE );
@@ -1942,6 +1945,11 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)u32 index;switch (msr_index) { +case MSR_TEST_CTRL : +if (data) +return 1 ; + +break ;case MSR_EFER : ret =kvm_set_msr_common (vcpu, msr_info);break ;
Signature · 1 line
2.18.2
PATCH v9 7/8 KVM: VMX: virtualize split lock detection
In reply to Xiaoyao LiTEST_CTRL MSR is per-core scope, i.e., the sibling threads in the same physical core share the same MSR. This requires additional constraint when exposing it to guest.
1) When host SLD state is sld_off (no X86_FEATURE_SPLIT_LOCK_DETECT), feature split lock detection is unsupported/disabled. Cannot expose it to guest.
2) When host SLD state is sld_warn (has X86_FEATURE_SPLIT_LOCK_DETECT but no X86_FEATURE_SLD_FATAL), feature split lock detection can be exposed to guest only when nosmt due to the per-core scope.
In this case, guest's setting can be propagated into real hardware MSR. Further, to avoid the potiential MSR_TEST_CTRL.SLD toggling overhead during every vm-enter and vm-exit, it loads and keeps guest's SLD setting when in vcpu run loop and guest_state_loaded, i.e., betweer vmx_prepare_switch_to_guest() and vmx_prepare_switch_to_host().
3) when host SLD state is sld_fatal (has X86_FEATURE_SLD_FATAL), feature split lock detection can be exposed to guest regardless of SMT but KVM_HINTS_SLD_FATAL needs to be set.
In this case, guest can still set and clear MSR_TEST_CTRL.SLD bit, but the bit value never be propagated to real MSR. KVM always keeps SLD bit turned on for guest vcpu. The reason why not force guest MSR_CTRL.SLD bit to 1 is that guest needs to set this bit to 1 itself to tell KVM it's SLD-aware.
--- arch/x86/kvm/cpuid.c | 6 ++++ arch/x86/kvm/vmx/vmx.c | 68 ++++++++++++++++++++++++++++++++++++------ arch/x86/kvm/vmx/vmx.h | 3 ++ arch/x86/kvm/x86.c | 6 +++- arch/x86/kvm/x86.h | 7 +++++ 5 files changed, 80 insertions(+), 10 deletions(-)
arch/x86/kvm/cpuid.c +6 -0
@@ -717,9 +717,15 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)if (sched_info_on ()) entry->eax |= (1 <<KVM_FEATURE_STEAL_TIME ); +if (kvm_split_lock_detect_supported ()) + entry->eax |= (1 <<KVM_FEATURE_SPLIT_LOCK_DETECT ); + entry->ebx =0 ; entry->ecx =0 ; entry->edx =0 ; + +if (boot_cpu_has (X86_FEATURE_SLD_FATAL )) + entry->edx |= (1 <<KVM_HINTS_SLD_FATAL );break ;case 0x80000000 : entry->eax =min (entry->eax,0x8000001f );
arch/x86/kvm/vmx/vmx.c +59 -9
136 lines not on this page · raw message
arch/x86/kvm/vmx/vmx.h +3 -0
@@ -216,12 +216,15 @@ struct vcpu_vmx {int nmsrs;int save_nmsrs;bool guest_msrs_ready; +bool guest_has_sld; +bool host_sld_on;#ifdef CONFIG_X86_64 u64 msr_host_kernel_gs_base;u64 msr_guest_kernel_gs_base;#endif u64 spec_ctrl; +u64 msr_test_ctrl;u32 msr_ia32_umwait_control;u32 secondary_exec_control;
arch/x86/kvm/x86.c +5 -1
@@ -1189,7 +1189,7 @@ static const u32 msrs_to_save_all[] = {#endif MSR_IA32_TSC ,MSR_IA32_CR_PAT ,MSR_VM_HSAVE_PA ,MSR_IA32_FEAT_CTL ,MSR_IA32_BNDCFGS ,MSR_TSC_AUX , -MSR_IA32_SPEC_CTRL , +MSR_IA32_SPEC_CTRL ,MSR_TEST_CTRL ,MSR_IA32_RTIT_CTL ,MSR_IA32_RTIT_STATUS ,MSR_IA32_RTIT_CR3_MATCH ,MSR_IA32_RTIT_OUTPUT_BASE ,MSR_IA32_RTIT_OUTPUT_MASK , MSR_IA32_RTIT_ADDR0_A, MSR_IA32_RTIT_ADDR0_B,
@@ -5239,6 +5239,10 @@ static void kvm_init_msr_list(void)* to the guests in some cases. */ switch (msrs_to_save_all[i]) { +case MSR_TEST_CTRL : +if (!kvm_split_lock_detect_supported ()) +continue ; +break ;case MSR_IA32_BNDCFGS :if (!kvm_mpx_supported ())continue ;
arch/x86/kvm/x86.h +7 -0
@@ -355,6 +355,13 @@ static inline bool kvm_dr7_valid(u64 data)return !(data >>32 ); } +static inline bool kvm_split_lock_detect_supported (void ) +{ +return boot_cpu_has (X86_FEATURE_SPLIT_LOCK_DETECT ) && + (boot_cpu_has (X86_FEATURE_SLD_FATAL ) || + !cpu_smt_possible ()); +} +void kvm_load_guest_xsave_state (struct kvm_vcpu *vcpu);void kvm_load_host_xsave_state (struct kvm_vcpu *vcpu);u64 kvm_spec_ctrl_valid_bits (struct kvm_vcpu *vcpu);
Signature · 1 line
2.18.2
PATCH v9 8/8 x86/split_lock: Enable split lock detection initialization when running as an guest on KVM
In reply to Xiaoyao LiWhen running as guest, enumerating feature split lock detection through CPU model is not easy since CPU model is configurable by host VMM.
If running upon KVM, it can be enumerated through KVM_FEATURE_SPLIT_LOCK_DETECT, and if KVM_HINTS_SLD_FATAL is set, it needs to be set to sld_fatal mode.
--- arch/x86/include/asm/cpu.h | 2 ++ arch/x86/kernel/cpu/intel.c | 12 ++++++++++-- arch/x86/kernel/kvm.c | 3 +++ 3 files changed, 15 insertions(+), 2 deletions(-)
arch/x86/include/asm/cpu.h +2 -0
@@ -42,12 +42,14 @@ unsigned int x86_model(unsigned int sig);unsigned int x86_stepping (unsigned int sig);#ifdef CONFIG_CPU_SUP_INTEL extern void __init cpu_set_core_cap_bits (struct cpuinfo_x86 *c); +extern void __init split_lock_setup (bool fatal);extern void switch_to_sld (unsigned long tifn);extern bool handle_user_split_lock (struct pt_regs *regs,long error_code);extern bool handle_guest_split_lock (unsigned long ip);extern bool split_lock_virt_switch (bool on);#else static inline void __init cpu_set_core_cap_bits (struct cpuinfo_x86 *c) {} +static inline void __init split_lock_setup (bool fatal) {}static inline void switch_to_sld (unsigned long tifn) {}static inline bool handle_user_split_lock (struct pt_regs *regs,long error_code) {
arch/x86/kernel/cpu/intel.c +10 -2
@@ -996,12 +996,18 @@ static bool split_lock_verify_msr(bool on)return ctrl == tmp; } -static void __init split_lock_setup (void ) +void __init split_lock_setup (bool fatal) {enum split_lock_detect_state state = sld_warn;char arg[20 ];int i, ret; +if (fatal) { + state = sld_fatal; +pr_info ("forced on, sending SIGBUS on user-space split_locks\n" ); + goto set_cap; + } +if (!split_lock_verify_msr (false)) {pr_info ("MSR access failed: Disabled\n" );return ;
@@ -1037,6 +1043,7 @@ static void __init split_lock_setup(void)return ; } +set_cap:setup_force_cpu_cap (X86_FEATURE_SPLIT_LOCK_DETECT );if (state == sld_fatal)setup_force_cpu_cap (X86_FEATURE_SLD_FATAL );
@@ -1161,6 +1168,7 @@ void __init cpu_set_core_cap_bits(struct cpuinfo_x86 *c)const struct x86_cpu_id *m;u64 ia32_core_caps; +/* Note, paravirt support can enable SLD, e.g., see kvm_guest_init(). */ if (boot_cpu_has (X86_FEATURE_HYPERVISOR ))return ;
@@ -1182,5 +1190,5 @@ void __init cpu_set_core_cap_bits(struct cpuinfo_x86 *c)return ; } -split_lock_setup (); +split_lock_setup (false); }
arch/x86/kernel/kvm.c +3 -0
@@ -670,6 +670,9 @@ static void __init kvm_guest_init(void)* overcommitted. */ hardlockup_detector_disable (); + +if (kvm_para_has_feature (KVM_FEATURE_SPLIT_LOCK_DETECT )) +split_lock_setup (kvm_para_has_hint (KVM_HINTS_SLD_FATAL )); } static noinline uint32_t __kvm_cpuid_base(void)
Signature · 1 line
2.18.2
Re: [PATCH v9 8/8] x86/split_lock: Enable split lock detection initialization when running as an guest on KVM
Quoted 6 lines · Xiaoyao Li
When running as guest, enumerating feature split lock detection through CPU model is not easy since CPU model is configurable by host VMM.
If running upon KVM, it can be enumerated through
KVM_FEATURE_SPLIT_LOCK_DETECT,
This needs crystal clear documentation. What, exactly, is the host telling the guest if it sets this flag?
Quoted 2 lines
and if
KVM_HINTS_SLD_FATALis set, it
needs to be set tosld_fatalmode.
This needs much better docs. Do you mean:
"If KVM_HINTS_SLD_FATAL is set, then the guest will get #AC if it does a split-lock regardless of what is written to MSR_TEST_CTRL?"
Quoted 91 lines
Signed-off-by Xiaoyao Li <[email protected]>--- arch/x86/include/asm/cpu.h | 2 ++ arch/x86/kernel/cpu/intel.c | 12 ++++++++++-- arch/x86/kernel/kvm.c | 3 +++ 3 files changed, 15 insertions(+), 2 deletions(-)arch/x86/include/asm/cpu.h +2
-0@@ -42,12 +42,14 @@ unsigned int x86_model(unsigned int sig);unsigned int x86_stepping (unsigned int sig);#ifdef CONFIG_CPU_SUP_INTEL extern void __init cpu_set_core_cap_bits (struct cpuinfo_x86 *c); +extern void __init split_lock_setup (bool fatal);extern void switch_to_sld (unsigned long tifn);extern bool handle_user_split_lock (struct pt_regs *regs,long error_code);extern bool handle_guest_split_lock (unsigned long ip);extern bool split_lock_virt_switch (bool on);#else static inline void __init cpu_set_core_cap_bits (struct cpuinfo_x86 *c) {} +static inline void __init split_lock_setup (bool fatal) {}static inline void switch_to_sld (unsigned long tifn) {}static inline bool handle_user_split_lock (struct pt_regs *regs,long error_code) {arch/x86/kernel/cpu/intel.c +10
-2@@ -996,12 +996,18 @@ static bool split_lock_verify_msr(bool on)return ctrl == tmp; } -static void __init split_lock_setup (void ) +void __init split_lock_setup (bool fatal) {enum split_lock_detect_state state = sld_warn;char arg[20 ];int i, ret; +if (fatal) { + state = sld_fatal; +pr_info ("forced on, sending SIGBUS on user-space split_locks\n" ); + goto set_cap; + } +if (!split_lock_verify_msr (false)) {pr_info ("MSR access failed: Disabled\n" );return ;@@ -1037,6 +1043,7 @@ static void __init split_lock_setup(void)return ; } +set_cap:setup_force_cpu_cap (X86_FEATURE_SPLIT_LOCK_DETECT );if (state == sld_fatal)setup_force_cpu_cap (X86_FEATURE_SLD_FATAL );@@ -1161,6 +1168,7 @@ void __init cpu_set_core_cap_bits(struct cpuinfo_x86 *c)const struct x86_cpu_id *m;u64 ia32_core_caps; +/* Note, paravirt support can enable SLD, e.g., see kvm_guest_init(). */ if (boot_cpu_has (X86_FEATURE_HYPERVISOR ))return ;@@ -1182,5 +1190,5 @@ void __init cpu_set_core_cap_bits(struct cpuinfo_x86 *c)return ; } -split_lock_setup (); +split_lock_setup (false); }arch/x86/kernel/kvm.c +3
-1@@ -670,6 +670,9 @@ static void __init kvm_guest_init(void)* overcommitted. */ hardlockup_detector_disable (); + +if (kvm_para_has_feature (KVM_FEATURE_SPLIT_LOCK_DETECT )) +split_lock_setup (kvm_para_has_hint (KVM_HINTS_SLD_FATAL )); } static noinline uint32_t __kvm_cpuid_base(void) --2.18.2
Re: [PATCH v9 8/8] x86/split_lock: Enable split lock detection initialization when running as an guest on KVM
Quoted 11 lines · Andy Lutomirski
Quoted 6 lines · Xiaoyao Li
When running as guest, enumerating feature split lock detection through CPU model is not easy since CPU model is configurable by host VMM.
If running upon KVM, it can be enumerated through
KVM_FEATURE_SPLIT_LOCK_DETECT,This needs crystal clear documentation. What, exactly, is the host telling the guest if it sets this flag?
Quoted 9 lines
Quoted 2 lines
and if
KVM_HINTS_SLD_FATALis set, it
needs to be set tosld_fatalmode.This needs much better docs. Do you mean:
"If
KVM_HINTS_SLD_FATALis set, then the guest will get #AC if it does a split-lock regardless of what is written toMSR_TEST_CTRL?"
Hi Andy,
KVM_FEATURE_SPLIT_LOCK_DETECT, KVM_HINTS_SLD_FATAL and their docs are introduced in Patch 5. Do I still need to explain them in detail in this patch?
Quoted 6 lines · Xiaoyao Li
This series aims to add the virtualization of split lock detection in KVM.
Due to the fact that split lock detection is tightly coupled with CPU model and CPU model is configurable by host VMM, we elect to use paravirt method to expose and enumerate it for guest.
Thomas and Paolo,
Do you have time to have a look at this version?
Hi Thomas,
Quoted 11 lines · Xiaoyao Li
Quoted 6 lines · Xiaoyao Li
This series aims to add the virtualization of split lock detection in KVM.
Due to the fact that split lock detection is tightly coupled with CPU model and CPU model is configurable by host VMM, we elect to use paravirt method to expose and enumerate it for guest.
Thomas and Paolo,
Do you have time to have a look at this version?
Does this series have any chance to meet 5.8?
If not, do you plan to take a look at it after merge window?
Thanks,
-Xiaoyao
Ping for comments.
Quoted 80 lines · Xiaoyao Li
This series aims to add the virtualization of split lock detection in KVM.
Due to the fact that split lock detection is tightly coupled with CPU model and CPU model is configurable by host VMM, we elect to use paravirt method to expose and enumerate it for guest.
Changes in v9
- rebase to v5.7-rc4
- Add one patch to renameTIF_SLDtoTIF_SLD_DISABLED;
- Add one patch to remove bogus case inhandle_guest_split_lock;
- Introduce flagX86_FEATURE_SPLIT_LOCK_DETECT_FATALand thus dropsld_state; - Use X86_FEATURE_SPLIT_LOCK_DETECT and X86_FEATURE_SPLIT_LOCK_DETECT_FATAL to determine the SLD state of host; - Introduce split_lock_virt_switch() and two wrappers for KVM instead of sld_update_to();- Use paravirt to expose and enumerate split lock detection for guest;
- Split lock detection can be exposed to guest when host issld_fatal, even though host is SMT available.Changes in v8:
https://lkml.kernel.org/r/[email protected]
- rebase to v5.7-rc1.
- basic enabling of split lock detection already merged.
- When host issld_warnand nosmt, load guest's sld bit when in KVM context, i.e., betweenvmx_prepare_switch_to_guest()and beforevmx_prepare_switch_to_host(), KVM uses guest sld setting.Changes in v7:
https://lkml.kernel.org/r/[email protected]
- only pick patch 1 and patch 2, and hold all the left.
- Update SLD bit on each processor based onsld_state.Changes in v6:
https://lkml.kernel.org/r/[email protected]
- Drop thesld_not_existflag and useX86_FEATURE_SPLIT_LOCK_DETECTto check whether need to init split lock detection. [tglx]
- Use tglx's method to verify the existence of split lock detectoin.
- small optimization ofsld_update_msr()that the default value ofmsr_test_ctrl_cachehassplit_lock_detectbit cleared.
- Drop the patch3 in v5 that introducingkvm_onlyoption. [tglx]
- Rebase patch4-8 to kvm/queue.
- use the new kvm-cpu-cap to exposeX86_FEATURE_CORE_CAPABILITIESin Patch 6.Changes in v5:
https://lkml.kernel.org/r/[email protected]
- UseX86_FEATURE_SPLIT_LOCK_DETECTflag in kvm to ensure split lock detection is really supported.
- Add and export sld related helper functions in their related usecase kvm patches.Xiaoyao Li (8):
x86/split_lock: RenameTIF_SLDtoTIF_SLD_DISABLEDx86/split_lock: Remove bogus case inhandle_guest_split_lock()x86/split_lock: Introduce flagX86_FEATURE_SLD_FATALand drop
sld_state
x86/split_lock: Introducesplit_lock_virt_switch()and two wrappers x86/kvm: Introduce paravirt split lock detection enumeration
KVM: VMX: Enable MSRTEST_CTRLfor guest
KVM: VMX: virtualize split lock detection x86/split_lock: Enable split lock detection initialization when
running as an guest on KVMDocumentation/virt/kvm/cpuid.rst | 29 +++++++---- arch/x86/include/asm/cpu.h | 35 ++++++++++++++ arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/thread_info.h | 6 +-- arch/x86/include/uapi/asm/kvm_para.h | 8 ++-- arch/x86/kernel/cpu/intel.c | 59 ++++++++++++++++------- arch/x86/kernel/kvm.c | 3 ++ arch/x86/kernel/process.c | 2 +- arch/x86/kvm/cpuid.c | 6 +++ arch/x86/kvm/vmx/vmx.c | 72 +++++++++++++++++++++++++--- arch/x86/kvm/vmx/vmx.h | 3 ++ arch/x86/kvm/x86.c | 6 ++- arch/x86/kvm/x86.h | 7 +++ 13 files changed, 196 insertions(+), 41 deletions(-)