From: Amirmohammad Eftekhar <amirmohammad.eftekhar@cispa.de>
To: <seanjc@google.com>, <pbonzini@redhat.com>
Cc: <kvm@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
<amirmohammad.eftekhar@cispa.de>,
<amirmohammadeftekhar@gmail.com>
Subject: [PATCH] KVM: nVMX: Expire the emulated preemption timer immediately if its deadline has passed
Date: Wed, 25 Nov 2026 14:49:42 +0100 [thread overview]
Message-ID: <20261125134942.60776-1-amirmohammad.eftekhar@cispa.de> (raw)
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.
Fixes: 8d7fbf01f9af ("KVM: selftests: VMX preemption timer migration test")
Signed-off-by: Amirmohammad Eftekhar <amirmohammad.eftekhar@cispa.de>
---
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(+)
diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
--- a/arch/x86/kvm/vmx/nested.c
+++ b/arch/x86/kvm/vmx/nested.c
@@ -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
reply other threads:[~2026-11-25 13:49 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261125134942.60776-1-amirmohammad.eftekhar@cispa.de \
--to=amirmohammad.eftekhar@cispa.de \
--cc=amirmohammadeftekhar@gmail.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®