KVM: VMX: Cap VMX preemption timer to work around Intel errata
PATCH v2 1/1 KVM: VMX: Cap VMX preemption timer to work around Intel errata
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.
---
v2:
- Use div_u64() instead of 64-bit division '/' operator to fix link-time error (__udivdi3) on 32-bit x86 builds. [Sashiko]
v1: https://lore.kernel.org/all/[email protected]/
arch/x86/kvm/vmx/vmx.c | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-)
arch/x86/kvm/vmx/vmx.c +26 -3
@@ -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 __read_mostly preemption_timer_limit =1ULL <<32 ;#ifdef CONFIG_X86_64 module_param_named (preemption_timer, enable_preemption_timer, bool,S_IRUGO );#endif
@@ -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;
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: - [Low] The statically declared variable `preemption_timer_limit` is written to unconditionally but only…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The statically declared variable preemption_timer_limit is written to unconditionally but only read in a conditionally compiled function, causing a -Wunused-but-set-variable compiler warning on 32-bit x86 builds. --
--- Patch [1]: [PATCH v2] KVM: VMX: Cap VMX preemption timer to work around Intel errata --- commit b70aaa03571b4fcfb574b0df3d82309fea9e8407
Author: Jim Mattson <[email protected]>
KVM: VMX: Cap VMX preemption timer to work around Intel errata
This commit works around an Intel erratum where the VMX-preemption timer may expire earlier than expected. It limits the timer value based on CPUID leaf 15H during hardware setup and returns -ERANGE if the shifted delta_tsc reaches or exceeds this limit.
arch/x86/kvm/vmx/vmx.c module_param(dump_invalid_vmcs, bool, 0644); · line 146
Quoted 9 lines
arch/x86/kvm/vmx/vmx.c +1
-0@@ -143,6 +143,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 __read_mostly preemption_timer_limit =1ULL <<32 ;
[Severity: Low]
Since preemption_timer_limit is read only in vmx_set_hv_timer() which is conditionally compiled, does this unconditional declaration and later assignment in vmx_hardware_setup() result in a -Wunused-but-set-variable compiler warning on 32-bit x86 builds?
[ ... ]
Quoted 14 lines
arch/x86/kvm/vmx/vmx.c +3
-3@@ -8241,12 +8242,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 ;
[ ... ]
Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +2
-0@@ -8630,6 +8651,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 ;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 30 lines · Jim Mattson
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_limitduring hardware setup based on CPUID 15H when available, and return-ERANGEinvmx_set_hv_timer()if the shifteddelta_tscreaches or exceedspreemption_timer_limit.Reported-by Sean Christopherson <[email protected]>Suggested-by Chao Gao <[email protected]>Assisted-by Gemini:Gemini-NextSigned-off-by Jim Mattson <[email protected]>---
v2:
- Usediv_u64()instead of 64-bit division '/' operator to fix link-time error (__udivdi3) on 32-bit x86 builds. [Sashiko]v1: https://lore.kernel.org/all/[email protected]/
arch/x86/kvm/vmx/vmx.c| 29 ++++++++++++++++++++++++++---1 file changed, 26 insertions(+), 3 deletions(-)arch/x86/kvm/vmx/vmx.c +0
-0
arch/x86/kvm/vmx/vmx.c module_param(dump_invalid_vmcs, bool, 0644); · line 154
Quoted 4 lines
arch/x86/kvm/vmx/vmx.c +1
-0@@ -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 __read_mostly preemption_timer_limit =1ULL <<32 ;
This is set once at init, so __ro_after_init may be better and no need to initialize it at declaration.
The rest looks good,
btw, KVM is using 0xffffffff as preemption timer's max value in a few places (the use_timer_freq sanity check in vmx_hardware_setup() and the soft-disable path in vmx_update_hv_timer()). I am not sure if those should use preemption_timer_limit - 1.
Quoted 42 lines · Chao Gao
Quoted 30 lines · Jim Mattson
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_limitduring hardware setup based on CPUID 15H when available, and return-ERANGEinvmx_set_hv_timer()if the shifteddelta_tscreaches or exceedspreemption_timer_limit.Reported-by Sean Christopherson <[email protected]>Suggested-by Chao Gao <[email protected]>Assisted-by Gemini:Gemini-NextSigned-off-by Jim Mattson <[email protected]>---
v2:
- Usediv_u64()instead of 64-bit division '/' operator to fix link-time error (__udivdi3) on 32-bit x86 builds. [Sashiko]v1: https://lore.kernel.org/all/[email protected]/
arch/x86/kvm/vmx/vmx.c| 29 ++++++++++++++++++++++++++---1 file changed, 26 insertions(+), 3 deletions(-)arch/x86/kvm/vmx/vmx.c +0
-0arch/x86/kvm/vmx/vmx.c module_param(dump_invalid_vmcs, bool, 0644); · line 154
Quoted 4 lines
arch/x86/kvm/vmx/vmx.c +1
-0@@ -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 __read_mostly preemption_timer_limit =1ULL <<32 ;This is set once at init, so
__ro_after_initmay be better and no need to initialize it at declaration.The rest looks good,
Quoted 7 lines
Reviewed-by Chao Gao <[email protected]>btw, KVM is using
0xffffffffas preemption timer's max value in a few places (theuse_timer_freqsanity check invmx_hardware_setup()and the soft-disable path invmx_update_hv_timer()). I am not sure if those should usepreemption_timer_limit- 1.
Thanks! I'll address both points in V3.
Do we want to keep the hv_timer 64-bit only? AFAICT, this was only
done out of convenience.
See https://lore.kernel.org/kvm/[email protected]/.
I think something like this should work for 32- and 64- bit kernels (untested):
/* (a << shift) / divisor, return 1 if overflow otherwise 0 */ static inline int u64_shl_div_u64(u64 a, unsigned int shift,
u64 divisor, u64 *result)
{
- u64 low = a << shift, high = a >> (64 - shift);
+ u64 high = shift ? (a >> (64 - shift)) : 0;
- /* To avoid the overflow on divq */
- if (high >= divisor)
+ if (high >= divisor || !divisor)
return 1;- /* Low hold the result, high hold rem which is discarded */
- asm("divq %2\n\t" : "=a" (low), "=d" (high) :
- "rm" (divisor), "0" (low), "1" (high));
- *result = low;
-
+ *result = mul_u64_u64_div_u64(a, 1ULL << shift, divisor);
return 0;}
But I don't want to open up a can of worms.
Sean? Thoughts?
Sean Christopherson
maintainerQuoted 2 lines · Jim Mattson
On Tue, Jul 21, 2026 at 4:47 AM Chao Gao <[email protected]> wrote:
Do we want to keep thehv_timer64-bit only?
Hmm, yes. I don't want to open up a can of worms either.
Quoted 26 lines
AFAICT, this was only done out of convenience.
See https://lore.kernel.org/kvm/[email protected]/.I think something like this should work for 32- and 64- bit kernels (untested):
/* (a << shift) / divisor, return 1 if overflow otherwise 0 */ static inline int u64_shl_div_u64(u64 a, unsigned int shift,
u64 divisor, u64 *result){
(quoted diff) +3
-8- u64 low = a << shift, high = a >> (64 - shift); + u64 high = shift ? (a >> (64 - shift)) : 0; - /* To avoid the overflow on divq */ - if (high >= divisor) + if (high >= divisor || !divisor) return 1; - /* Low hold the result, high hold rem which is discarded */ - asm("divq %2\n\t" : "=a" (low), "=d" (high) : - "rm" (divisor), "0" (low), "1" (high)); - *result = low; - + *result = mul_u64_u64_div_u64(a, 1ULL << shift, divisor); return 0; }
Quoted 3 lines
But I don't want to open up a can of worms.
Sean? Thoughts?
I vote to go in the opposite direction, and double down on the code being 64-bit only. If we do that, then the annoying 32-bit issues Sashiko keeps pointing out should naturally go away.
Compile-tested only...
---
From: Sean Christopherson <[email protected]>
Date: Wed, 22 Jul 2026 00:30:46 +0000
Subject: [PATCH] KVM: VMX: Bury all of the VMX preemption timer code under
CONFIG_X86_64=y
Double down on using the VMX preemption timer only for 64-bit kernels, and bury the setup and runtime adjustment code, and all global variables, under CONFIG_X86_64=y. This will allow addressing various Intel erratum without running afoul of unused-but-set-variable and __udivdi3() warnings/errors on 32-bit kernels.
No functional change intended.
--- arch/x86/kvm/vmx/vmx.c | 122 +++++++++++++++++++++++------------------ 1 file changed, 69 insertions(+), 53 deletions(-)
arch/x86/kvm/vmx/vmx.c +69 -53
@@ -150,10 +150,12 @@ module_param(dump_invalid_vmcs, bool, 0644);#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL/* Guest_tsc -> host_tsc conversion requires 64-bit division. */ +#ifdef CONFIG_X86_64 static int __read_mostly cpu_preemption_timer_multi;static bool __read_mostly enable_preemption_timer =1 ; -#ifdef CONFIG_X86_64 module_param_named (preemption_timer, enable_preemption_timer, bool,S_IRUGO ); +#else +#define enable_preemption_timer false#endif extern bool __read_mostly allow_smaller_maxphyaddr;
@@ -7408,32 +7410,6 @@ static void vmx_refresh_guest_perf_global_control(struct kvm_vcpu *vcpu) pmu->global_ctrl =vmcs_read64 (GUEST_IA32_PERF_GLOBAL_CTRL ); } -static void vmx_update_hv_timer (struct kvm_vcpu *vcpu,bool force_immediate_exit) -{ -struct vcpu_vmx *vmx =to_vmx (vcpu); -u64 tscl; -u32 delta_tsc; - -if (force_immediate_exit) { -vmcs_write32 (VMX_PREEMPTION_TIMER_VALUE ,0 ); - vmx->loaded_vmcs->hv_timer_soft_disabled = false; - }else if (vmx->hv_deadline_tsc !=-1 ) { - tscl =rdtsc (); -if (vmx->hv_deadline_tsc > tscl) -/* set_hv_timer ensures the delta fits in 32-bits */ - delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >> - cpu_preemption_timer_multi); -else - delta_tsc =0 ; - -vmcs_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 ); - vmx->loaded_vmcs->hv_timer_soft_disabled = true; - } -} - void noinstr vmx_update_host_rsp(struct vcpu_vmx *vmx, unsigned long host_rsp) {if (unlikely (host_rsp != vmx->loaded_vmcs->host_state.rsp)) {
@@ -7519,6 +7495,8 @@ static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu,guest_state_exit_irqoff (); } +static void vmx_update_hv_timer (struct kvm_vcpu *vcpu,bool force_immediate_exit); +fastpath_t vmx_vcpu_run (struct kvm_vcpu *vcpu,u64 run_flags) {bool force_immediate_exit = run_flags &KVM_RUN_FORCE_IMMEDIATE_EXIT ;
@@ -8328,6 +8306,36 @@ static inline int u64_shl_div_u64(u64 a, unsigned int shift,return 0 ; } +static __init void vmx_setup_preemption_timer (void ) +{ +if (!cpu_has_vmx_preemption_timer ()) + enable_preemption_timer = false; + +if (enable_preemption_timer) { +u64 use_timer_freq =5000ULL *1000 *1000 ; + + cpu_preemption_timer_multi = +vmx_misc_preemption_timer_rate (vmcs_config.misc); + +if (tsc_khz) + use_timer_freq = (u64 )tsc_khz *1000 ; + use_timer_freq >>= cpu_preemption_timer_multi; + +/* +* KVM "disables" the preemption timer by setting it to its max +* 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 ) + enable_preemption_timer = false; + } + +if (!enable_preemption_timer) { + vt_x86_ops.set_hv_timer =NULL ; + vt_x86_ops.cancel_hv_timer =NULL ; + } +} +int vmx_set_hv_timer (struct kvm_vcpu *vcpu,u64 guest_deadline_tsc,bool *expired) {
@@ -8372,6 +8380,39 @@ void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu) {to_vmx (vcpu)->hv_deadline_tsc =-1 ; } + +static void vmx_update_hv_timer (struct kvm_vcpu *vcpu,bool force_immediate_exit) +{ +struct vcpu_vmx *vmx =to_vmx (vcpu); +u64 tscl; +u32 delta_tsc; + +if (force_immediate_exit) { +vmcs_write32 (VMX_PREEMPTION_TIMER_VALUE ,0 ); + vmx->loaded_vmcs->hv_timer_soft_disabled = false; + }else if (vmx->hv_deadline_tsc !=-1 ) { + tscl =rdtsc (); +if (vmx->hv_deadline_tsc > tscl) +/* set_hv_timer ensures the delta fits in 32-bits */ + delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >> + cpu_preemption_timer_multi); +else + delta_tsc =0 ; + +vmcs_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 ); + vmx->loaded_vmcs->hv_timer_soft_disabled = true; + } +} +#else +static __init void vmx_setup_preemption_timer (void ) { } + +static void vmx_update_hv_timer (struct kvm_vcpu *vcpu,bool force_immediate_exit) +{ +BUILD_BUG_ON (1 ); +}#endif void vmx_update_cpu_dirty_logging (struct kvm_vcpu *vcpu)
@@ -8734,32 +8775,7 @@ __init int vmx_hardware_setup(void)if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml ()) enable_pml =0 ; -if (!cpu_has_vmx_preemption_timer ()) - enable_preemption_timer = false; - -if (enable_preemption_timer) { -u64 use_timer_freq =5000ULL *1000 *1000 ; - - cpu_preemption_timer_multi = -vmx_misc_preemption_timer_rate (vmcs_config.misc); - -if (tsc_khz) - use_timer_freq = (u64 )tsc_khz *1000 ; - use_timer_freq >>= cpu_preemption_timer_multi; - -/* -* KVM "disables" the preemption timer by setting it to its max -* 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 ) - enable_preemption_timer = false; - } - -if (!enable_preemption_timer) { - vt_x86_ops.set_hv_timer =NULL ; - vt_x86_ops.cancel_hv_timer =NULL ; - } +vmx_setup_preemption_timer (); kvm_caps.supported_mce_cap |=MCG_LMCE_P ; kvm_caps.supported_mce_cap |=MCG_CMCI_P ;
base-commit: 3aec122bdcaf6f8c4ecfc7f6adf6773015ced676 --
Quoted 3 lines · Sean Christopherson
I vote to go in the opposite direction, and double down on the code being 64-bit only. If we do that, then the annoying 32-bit issues Sashiko keeps pointing out should naturally go away.
Oh, bummer. I thought by "the code," you meant KVM!