KVM: VMX: Cap VMX preemption timer to work around Intel erratum
PATCH v3 1/1 KVM: VMX: Cap VMX preemption timer to work around Intel erratum
Due to a widespread Intel erratum (e.g. EMR158), programming the VMX-preemption timer with certain large values may cause the timer to expire earlier than expected. The recommended workaround is to cap the VMX-preemption timer value to strictly less than 2^25 * CPUID.15H:EBX[31:0] / CPUID.15H:EAX[31:0].
Calculate preemption_timer_limit during hardware setup based on CPUID 15H when available, and return -ERANGE in vmx_set_hv_timer() if the shifted delta_tsc reaches or exceeds preemption_timer_limit.
Use preemption_timer_limit - 1 instead of 0xffffffff when soft-disabling the timer and when checking the timer frequency limit in hardware setup.
---
v3:
* Use preemption_timer_limit - 1 instead of 0xffffffff when soft-disabling the timer and when checking the timer frequency limit in hardware setup [Chao]
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
arch/x86/kvm/vmx/vmx.c | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx/vmx.c +28 -5
@@ -151,6 +151,7 @@ module_param(dump_invalid_vmcs, bool, 0644);/* Guest_tsc -> host_tsc conversion requires 64-bit division. */ static int __read_mostly cpu_preemption_timer_multi;static bool __read_mostly enable_preemption_timer =1 ; +static u64 __ro_after_init preemption_timer_limit;#ifdef CONFIG_X86_64 module_param_named (preemption_timer, enable_preemption_timer, bool,S_IRUGO );#endif
@@ -7412,7 +7413,7 @@ static void vmx_update_hv_timer(struct kvm_vcpu *vcpu, bool force_immediate_exitvmcs_write32 (VMX_PREEMPTION_TIMER_VALUE , delta_tsc); vmx->loaded_vmcs->hv_timer_soft_disabled = false; }else if (!vmx->loaded_vmcs->hv_timer_soft_disabled) { -vmcs_write32 (VMX_PREEMPTION_TIMER_VALUE ,-1 ); +vmcs_write32 (VMX_PREEMPTION_TIMER_VALUE , preemption_timer_limit -1 ); vmx->loaded_vmcs->hv_timer_soft_disabled = true; } }
@@ -8338,12 +8339,12 @@ int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc,return -ERANGE ;/* -* If the delta tsc can't fit in the 32 bit after the multi shift, -* we can't use the preemption timer. +* If the delta tsc exceeds the preemption timer limit after the +* multi shift, we can't use the preemption timer. * It's possible that it fits on later vmentries, but checking * on every vmentry is costly so we just use an hrtimer. */ -if (delta_tsc >> (cpu_preemption_timer_multi +32 )) +if ((delta_tsc >> cpu_preemption_timer_multi) >= preemption_timer_limit)return -ERANGE ; vmx->hv_deadline_tsc = tscl + delta_tsc;
@@ -8585,6 +8586,26 @@ static void __init vmx_setup_me_spte_mask(void)kvm_mmu_set_me_spte_mask (0 , me_mask); } +/* +* Workaround for a widespread Intel erratum (e.g. EMR158) where the +* VMX-preemption timer may expire earlier than expected when programmed +* with large values. The workaround is to cap the timer value to strictly +* less than 2^25 * CPUID.15H:EBX / CPUID.15H:EAX. +*/ +static __init u64 calc_preemption_timer_limit (void ) +{ +u32 eax, ebx, ecx, edx; + +if (cpuid_eax (0 ) <0x15 ) +return 1ULL <<32 ; + +cpuid (0x15 , &eax, &ebx, &ecx, &edx); +if (!eax || !ebx) +return 1ULL <<32 ; + +return min_t (u64,1ULL <<32 ,div_u64 ((u64 )ebx <<25 , eax)); +} +__init int vmx_hardware_setup (void ) {unsigned long host_bndcfgs;
@@ -8726,6 +8747,8 @@ __init int vmx_hardware_setup(void) cpu_preemption_timer_multi =vmx_misc_preemption_timer_rate (vmcs_config.misc); + preemption_timer_limit =calc_preemption_timer_limit (); +if (tsc_khz) use_timer_freq = (u64 )tsc_khz *1000 ; use_timer_freq >>= cpu_preemption_timer_multi;
@@ -8735,7 +8758,7 @@ __init int vmx_hardware_setup(void)* value. Don't use the timer if it might cause spurious exits * at a rate faster than 0.1 Hz (of uninterrupted guest time). */ -if (use_timer_freq >0xffffffffu /10 ) +if (use_timer_freq > (preemption_timer_limit -1 ) /10 ) enable_preemption_timer = false; }
Signature · 1 line
2.55.0.229.g6434b31f56-goog
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] If `calc_preemption_timer_limit()` returns 0, `preemption_timer_limit - 1` underflows to…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] If calc_preemption_timer_limit() returns 0, preemption_timer_limit - 1 underflows to ULLONG_MAX, bypassing the frequency safety check and writing 0xFFFFFFFF to the preemption timer, which actively triggers the erratum. --
--- Patch [1]: [PATCH v3] KVM: VMX: Cap VMX preemption timer to work around Intel erratum --- commit a2b4522c8049c537b2f6064441cc75a032adcb65
Author: Jim Mattson <[email protected]>
KVM: VMX: Cap VMX preemption timer to work around Intel erratum
This commit implements a workaround for an Intel erratum where large VMX-preemption timer values cause early expiration. It introduces a calculated limit based on CPUID.15H to cap the timer value and soft-disable it.
Quoted 4 lines
arch/x86/kvm/vmx/vmx.c +0
-0
[ ... ]
arch/x86/kvm/vmx/vmx.c static void vmx_update_hv_timer(struct kvm_vcpu *vcpu, bool force_immediate_exit · line 7296
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +1
-1@@ -7292,7 +7293,7 @@ static void vmx_update_hv_timer(struct kvm_vcpu *vcpu, bool force_immediate_exitvmcs_write32 (VMX_PREEMPTION_TIMER_VALUE , delta_tsc); vmx->loaded_vmcs->hv_timer_soft_disabled = false; }else if (!vmx->loaded_vmcs->hv_timer_soft_disabled) { -vmcs_write32 (VMX_PREEMPTION_TIMER_VALUE ,-1 ); +vmcs_write32 (VMX_PREEMPTION_TIMER_VALUE , preemption_timer_limit -1 );
[Severity: Medium]
If preemption_timer_limit evaluates to 0, does this subtraction underflow and result in writing 0xFFFFFFFF (after u32 truncation) to the timer? Writing this value seems to be exactly what triggers the erratum we are trying to avoid.
Quoted 3 lines
arch/x86/kvm/vmx/vmx.c +0
-0vmx->loaded_vmcs->hv_timer_soft_disabled = true; } }
[ ... ]
Quoted 12 lines
arch/x86/kvm/vmx/vmx.c +8
-0@@ -8489,6 +8490,26 @@ static void __init vmx_setup_me_spte_mask(void)kvm_mmu_set_me_spte_mask (0 , me_mask); } +/* +* Workaround for a widespread Intel erratum (e.g. EMR158) where the +* VMX-preemption timer may expire earlier than expected when programmed +* with large values. The workaround is to cap the timer value to strictly +* less than 2^25 * CPUID.15H:EBX / CPUID.15H:EAX. +*/ +static __init u64 calc_preemption_timer_limit (void ) +{
arch/x86/kvm/vmx/vmx.c static void __init vmx_setup_me_spte_mask(void) · line 8510
Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +10
-0+u32 eax, ebx, ecx, edx; + +if (cpuid_eax (0 ) <0x15 ) +return 1ULL <<32 ; + +cpuid (0x15 , &eax, &ebx, &ecx, &edx); +if (!eax || !ebx) +return 1ULL <<32 ; + +return min_t (u64,1ULL <<32 ,div_u64 ((u64 )ebx <<25 , eax));
[Severity: Medium]
Could this division result in 0 if a CPU or an L0 hypervisor provides unusual CPUID.15H values where eax > (ebx << 25)? If calc_preemption_timer_limit() returns 0, it would cause the underflow mentioned above.
Quoted 5 lines
arch/x86/kvm/vmx/vmx.c +2
-0+} +__init int vmx_hardware_setup (void ) {unsigned long host_bndcfgs;
[ ... ]
arch/x86/kvm/vmx/vmx.c __init int vmx_hardware_setup(void) · line 8665
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +1
-1@@ -8639,7 +8662,7 @@ __init int vmx_hardware_setup(void)* value. Don't use the timer if it might cause spurious exits * at a rate faster than 0.1 Hz (of uninterrupted guest time). */ -if (use_timer_freq >0xffffffffu /10 ) +if (use_timer_freq > (preemption_timer_limit -1 ) /10 )
[Severity: Medium]
In the event that preemption_timer_limit is 0, this subtraction will underflow to ULLONG_MAX. Does this unintentionally bypass the safety check and leave enable_preemption_timer set to true?
Quoted 3 lines
arch/x86/kvm/vmx/vmx.c +0
-0enable_preemption_timer = false; }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1