KVM: nVMX: Expire the emulated preemption timer immediately if its deadline has passed
PATCH 1/1 KVM: nVMX: Expire the emulated preemption timer immediately if its deadline has passed
vmx_calc_preemption_timer_value() returns the unclamped u64 "preemption_timer_deadline - l1_scaled_tsc". The deadline is reused, not recomputed from vmcs12, on the two entries to L2 that are not VMLAUNCH/VMRESUME: RSM back into L2 (has_preemption_timer_deadline survives the SMI) and KVM_SET_NESTED_STATE with KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE. If the deadline has already passed by then, the subtraction wraps to ~2^64 and vmx_start_preemption_timer() arms the hrtimer ~2^64 / virtual_tsc_khz ns in the future (about two hours at 2.6 GHz) instead of synthesizing the immediate preemption-timer VM-Exit. L2 then keeps running without the exit L1 asked for until some other event forces a VM-Exit.
Reachable by:
- an SMI taken in L2 whose SMM handler runs longer than the remaining timer (SDM Vol. 3C 27.5.1: the timer-induced VM exit occurs immediately after RSM if the timer expired during SMI, in SMM, or during RSM);
- a guest WRMSR to IA32_TSC / IA32_TSC_ADJUST from SMM that moves L1's TSC past the deadline (the timer counts only TSC increments, so a TSC write must not affect it at all);
- KVM_SET_NESTED_STATE with a restored deadline that has already passed, e.g. a live migration whose blackout spans the expiry.
The original deadline code, 850448f35aaf ("KVM: nVMX: Fix VMX preemption timer migration"), returned 0 in this case; the guard was dropped by the nested.c hunk of the follow-up commit.
---
Observed inside a disposable nested L1 (KVM as both L0 and the L1 under test) on mainline and on 6.1.74, exercising all three routes. With a deadline set just in the past, the L2 preemption-timer hrtimer is armed about 7064 s out (2^64 / 2611200 kHz) instead of causing the immediate exit, and L2 runs for the whole observation window with no exit; the controls (no deadline, future deadline, SMI with a short handler) exit on time. No warning, BUG or KASAN on either kernel.
arch/x86/kvm/vmx/nested.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
arch/x86/kvm/vmx/nested.c +12 -0
@@ -2291,6 +2291,18 @@ vmcs12->vmx_preemption_timer_value + l1_scaled_tsc; vmx->nested.has_preemption_timer_deadline = true; } + +/* +* The deadline can already be in the past when L2 is (re)entered +* without a VMLAUNCH/VMRESUME, i.e. on RSM or KVM_SET_NESTED_STATE. +* Expire the timer immediately in that case (the SDM requires the +* timer-induced VM-Exit to occur immediately after RSM if the timer +* expired during SMI, in SMM, or during RSM) instead of letting the +* subtraction wrap to a ~2^64 timeout. +*/ +if (vmx->nested.preemption_timer_deadline <= l1_scaled_tsc) +return 0 ; +return vmx->nested.preemption_timer_deadline - l1_scaled_tsc; }
base-commit: cbe54fc3462b3949ab27b9ae81fb9ddb509040c7