KVM: nVMX: Stale IA32_TSC in VM-Exit MSR-store list for a halted L2
From: Xiao Cheng
Date: Thu Dec 03 2026 - 03:58:26 EST
Hi,
SUMMARY
L1 enters L2 in the HLT activity state, with a VMX-preemption timer
value of 0 and IA32_TSC in its VM-Exit MSR-store list. KVM synthesizes
the VM-Exit for the timer and stores an old TSC in the list: the value
that the previous hardware VM-Exit of L2 stored. The value should be the
TSC at the time of the VM-Exit, as RDMSR would read it. Any L1 kernel
that uses nested VMX with this combination of controls can see the wrong
value; no other privilege is needed.
Affected version: tested on mainline commit
693c2e04e3f1d4c59af236e1211b7b8cb165a790 (v7.4-rc5-48-g693c2e04e3f1),
which was also the head of torvalds/linux master when checked on
2026-12-02 11:33 UTC. The code that stores the stale value comes from
commit 662f1d1d19317e792ccfc53dee625c02dcefac58 ("KVM: nVMX: Add support
for capturing highest observable L2 TSC", first in v5.5-rc1). The
selftest below was run on 693c2e04e3f1, and it failed. It was also run
on kvm-x86 next at cbe54fc3462b3949ab27b9ae81fb9ddb509040c7, with and
without a fix that is not part of this mail. It failed on that kernel
without the fix. On
2026-12-03 at about 01:44 UTC, the head of torvalds/linux master was
d06ce6f1eb5e. It has 55 commits after 693c2e04e3f1, and none of them
changes a file under a path with kvm. Lines 1152 to 1154 of
arch/x86/kvm/vmx/nested.c there are the same as at 693c2e04e3f1. That
check was made by reading the source, and the selftest was not run on
that commit.
STEPS TO REPRODUCE
1. Check out 693c2e04e3f1d4c59af236e1211b7b8cb165a790. Build it with
CONFIG_KVM=y and CONFIG_KVM_INTEL=y. No module parameter was changed.
The test kernel also had CONFIG_KASAN_GENERIC, CONFIG_KASAN_INLINE,
CONFIG_UBSAN_BOUNDS and CONFIG_PROVE_LOCKING on. They printed
nothing, and the failure was not tried without them.
2. Boot that kernel on a CPU with VMX, with /dev/kvm available to root.
The run used a guest of QEMU 4.2.1 (-accel kvm -cpu host -smp 1
-m 6G), so KVM ran nested.
3. Save the selftest from the end of this mail as
tools/testing/selftests/kvm/x86/vmx_halted_l2_tsc_store_test.c. In
tools/testing/selftests/kvm/Makefile.kvm, add this line after the
line for x86/vmx_preemption_timer_test:
TEST_GEN_PROGS_x86 += x86/vmx_halted_l2_tsc_store_test
4. Build and run it as root on the host under test. It took between 2.1
and 4.9 seconds in the recorded runs:
make -j4 headers && make -C tools/testing/selftests/kvm -j4 \
TEST_GEN_PROGS_x86=x86/vmx_halted_l2_tsc_store_test
./tools/testing/selftests/kvm/x86/vmx_halted_l2_tsc_store_test
The build needs gcc, GNU make and the libuuid development files
(uuid-dev on Debian and Ubuntu, libuuid-devel on Fedora), because the
selftest Makefile builds a VFIO helper library that links -luuid.
"make headers" exports the tree's UAPI headers, which the selftest
Makefile then uses. No other flag or header path is needed.
The test is a KVM selftest of 117 lines. Its source is at the end of
this mail. It makes three runs of L2 with one IA32_TSC entry in the
VM-Exit MSR-store list. Run 1 is a control: L2 is active and the timer
expires while it runs. Run 2 enters L2 in the HLT activity state with
the timer value 0. Run 3 is a second control: L2 is active and the timer
value is 0. In each run, L1 reads TSC before VMLAUNCH or VMRESUME and
after the VM-Exit, and the host side checks that the stored value lies
between them.
The Linux 5.4 machine that ran QEMU does not support the VMCS field
HOST_IA32_PERF_GLOBAL_CTRL. So the run changed one helper in
tools/testing/selftests/kvm/lib/x86/vmx.c, in init_vmcs_guest_state(),
to write GUEST_IA32_PERF_GLOBAL_CTRL only if the VM-Entry controls load
IA32_PERF_GLOBAL_CTRL:
- __vmwrite(GUEST_IA32_PERF_GLOBAL_CTRL,
- vmread(HOST_IA32_PERF_GLOBAL_CTRL));
+ if (vmread(VM_ENTRY_CONTROLS) & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
+ __vmwrite(GUEST_IA32_PERF_GLOBAL_CTRL,
+ vmread(HOST_IA32_PERF_GLOBAL_CTRL));
A host that supports that field does not need this change. The kernel
under test was not modified. The test has not been run on bare metal.
EXPECTED RESULT
All three runs print that the stored TSC is between the TSC read before
the entry and the TSC read after the exit. The test exits with status 0.
Run 1 printed this line, which is the expected form for every run:
run 1 (active L2): before=215966818 stored=219913472 after=220415972:
stored TSC is in [before, after]
ACTUAL RESULT
Runs 1 and 3 are in range. In run 2, the stored value is exactly the
value that run 1 stored, and it is smaller than the value read before
run 2. The test fails with exit status 254. This is the output of one
run, and the same happened in 3 of 3 fresh boots:
run 1 (active L2): before=215966818 stored=219913472 after=220415972:
stored TSC is in [before, after]
run 2 (halted L2): before=229664324 stored=219913472 after=231756034:
stored TSC is NOT in [before, after]
run 3 (active L2, timer 0): before=232477668 stored=233206116
after=233582045: stored TSC is in [before, after]
==== Test Assertion Failure ====
x86/vmx_halted_l2_tsc_store_test.c:110: !failed
pid=72 tid=72 errno=4 - Interrupted system call
1 0x00000000004027e7: main at vmx_halted_l2_tsc_store_test.c:110
2 0x00007f8607b06082: ?? ??:0
3 0x0000000000402a0d: _start at ??:?
L1 received a stale TSC in the VM-Exit MSR-store list
SELFTEST_EXIT:254
In the output above, the line for run 2, where L2 entered in the HLT
activity state, has stored=219913472. That is the same value that run 1
stored, and it is below before=229664324, so the test reports "NOT in
[before, after]". The line for run 1, the control with an active L2 in
the same process, has stored=219913472 between before=215966818 and
after=220415972. The line for run 3, the control with an active L2 and
the timer value 0, has stored=233206116 between before=232477668 and
after=233582045.
Control: runs 1 and 3 of the same test, with an active L2, pass in all 3
boots. All runs use the same VMCS controls and the same store list. Only
the activity state and the timer value differ.
WHY THIS IS A BUG
The Intel SDM, Volume 3C, chapter "VM Exits", section "Saving MSRs",
says each entry of the VM-exit MSR-store area is processed "by storing
the value of the MSR indexed by bits 31:0 (as they would be read by
RDMSR) into bits 127:64". The section "VMX-Preemption Timer" in the
chapter "VMX Non-Root Operation" says that when the timer counts down to
zero "a VM exit occurs". The section "VMX-Preemption Timer" in the
chapter "VM Entries" says the timer can expire during VM entry, for
example when its value is zero, and then "a VM exit occurs". KVM's own
comment at arch/x86/kvm/vmx/nested.c:2733-2742 says the extra vmcs02 store
entry exists so that "KVM effectively reads TSC at the time of VM-Exit
from L2". A value from an earlier VM-Exit does not meet this.
ROOT CAUSE
arch/x86/kvm/vmx/nested.c:1152 at commit
693c2e04e3f1d4c59af236e1211b7b8cb165a790, in
nested_vmx_get_vmexit_msr_value():
if (msr_index == MSR_IA32_TSC && vmx->nested.tsc_autostore_slot >= 0) {
int slot = vmx->nested.tsc_autostore_slot;
u64 host_tsc = vmx->msr_autostore.val[slot].value;
*data = kvm_read_l1_tsc(vcpu, host_tsc);
return true;
}
prepare_vmcs02_rare() sets only the index of this entry
(nested.c:2745-2746). The hardware writes the value only when it
performs a VM-Exit from L2 with vmcs02. For a halted L2,
nested_vmx_run() does not enter vmcs02 (nested.c:3910, "return
kvm_emulate_halt_noskip(vcpu)"). Later, vmx_check_nested_events()
synthesizes the timer VM-Exit (nested.c:4426). prepare_vmcs12() then
calls nested_vmx_store_msr() (nested.c:4794), which calls
nested_vmx_get_vmexit_msr_value() (nested.c:1202). So the function
returns the TSC of the last hardware VM-Exit of L2.
The same stale slot is read for every VM-Exit that KVM synthesizes
before L2 has run on hardware since the nested VM-Enter. From reading
the code, this includes the WAIT-FOR-SIPI activity state and a restore
by KVM_SET_NESTED_STATE. The selftest shows only the HLT case. Run 3, an
active L2 with the timer value 0, stored an in-range value on the
kernel under test, without the fix.
SUGGESTED FIX
A fix has been written and tested in virtual machines. It will be posted
as a separate series, with this selftest as its regression test. This
mail does not carry it. The fix addresses the cause: the vmcs02 slot is
read when hardware has not written it since the nested VM-Enter. It adds
a flag, tsc_autostore_stale, to struct nested_vmx.
nested_vmx_enter_non_root_mode() sets the flag on every nested VM-Enter,
including the ones from KVM_SET_NESTED_STATE and RSM. vmx_vcpu_run()
clears it after a hardware VM-Exit from L2 that stored the list. While
the flag is set, nested_vmx_get_vmexit_msr_value() reads the current
host TSC with rdtsc() and converts it with kvm_read_l1_tsc(), in place
of the value in the vmcs02 slot. That value includes the time that L0
needs to emulate the VM-Exit. The other way to fix it is to stop using
the vmcs02 auto-store entry and always read the TSC in software. That
gives up the accuracy that commit 662f1d1d1931 added for hardware
VM-Exits, so the fix does not do it. Only the HLT case is tested. The
WAIT-FOR-SIPI activity state, KVM_SET_NESTED_STATE and RSM use the same
flag and were not tested. The fix was made against kvm-x86 next at
cbe54fc3462b3949ab27b9ae81fb9ddb509040c7, and it applies cleanly to
kvm.git master (bd1be6e0267d7a52425bd515c53e9a49bd4e2a12).
Tests that were run on kvm-x86 next at
cbe54fc3462b3949ab27b9ae81fb9ddb509040c7 with the fix described above,
on the same L0 and with the same helper change as above. The kernel had
CONFIG_KVM_INTEL=m, CONFIG_KVM_AMD=n, CONFIG_KVM_WERROR=y,
CONFIG_KVM_SMM=y, CONFIG_KVM_XEN=y, CONFIG_KASAN_GENERIC,
CONFIG_UBSAN_BOUNDS and CONFIG_PROVE_LOCKING. It printed no sanitizer or
lock report in any of these runs. The same kernel image and the same
selftest binary were used with two builds of kvm-intel.ko. With the
module built without the fix, the selftest failed in 3 of 3 fresh boots;
run 2 stored the value of run 1. With the fixed module, it passed in 3
of 3 fresh boots, and runs 1, 2 and 3 were in range. Run 3 is also in
range without the fix, so it is a control and not a second failure. One
boot of an HLT-state L2 with the timer value 100000 on the fixed module
was in range; it was not run without the fix. The 18 nested VMX
selftests of this tree, including vmx_preemption_timer_test, gave the
same result with and without the fix: 13 passed and 5 skipped, because
the L0 does not offer the feature. Of the 114 x86 KVM selftests that
this tree builds, the only difference was the new test (fail without the
fix, pass with it); 6 other failures and 3 timeouts were the same
without and with the fix. kvm-unit-tests: 12 of the 18 VMX cases of
x86/unittests.cfg were run, with the same result without and with the
fix: 8 passed, ept and ept-mbec skipped (QEMU in the L0 has no
vmx-mbec), vmx reported 245 unexpected results of 31401 in both, and
vmx_vmcs_shadow_test timed out after 240 s in both.
A build with CONFIG_KVM_AMD=m and CONFIG_KVM_WERROR=y compiled every
object under arch/x86/kvm without a warning. The link of kvm-amd.ko
failed at modpost on undefined SEV symbols in this configuration, and
the fix changes no SVM code. Not done: a run on bare metal, the 6
vmx_pf_* kvm-unit-tests cases, any SVM test, and a run that measures
the stored TSC when a halted L2 is woken by an event and runs on
hardware before its VM-Exit.
ENVIRONMENT
- Processor: Intel Xeon W-2225 (Cascade Lake). The host under test ran
as a guest of QEMU on a Linux 5.4.0-216 machine, so KVM ran with
nested virtualisation. The test was not run on bare metal.
- Kernel configuration: CONFIG_KVM=y, CONFIG_KVM_INTEL=y, no changed
module parameter. Debug options that were also on:
CONFIG_KASAN_GENERIC, CONFIG_KASAN_INLINE, CONFIG_UBSAN_BOUNDS,
CONFIG_PROVE_LOCKING.
- Kernel for the fix tests: kvm-x86 next at
cbe54fc3462b3949ab27b9ae81fb9ddb509040c7, with CONFIG_KVM_INTEL=m,
CONFIG_KVM_AMD=n and the options named under "Suggested fix".
- Compiler: gcc 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.2).
- Emulator that ran the host under test: QEMU 4.2.1 (Debian
1:4.2-3ubuntu6.30), -accel kvm -cpu host.
AI assistance: I found this problem with AI assistance (OpenAI Codex
and Claude Code reviewing the KVM source). The same tools wrote the
reproducer and drafted this report. I reproduced the failure by hand
and checked the report.
Thanks,
jumormt
SELFTEST: tools/testing/selftests/kvm/x86/vmx_halted_l2_tsc_store_test.c
// SPDX-License-Identifier: GPL-2.0-only
/*
* Test that a nested VM-Exit that KVM synthesizes while L2 is halted stores
* the current TSC in L1's VM-Exit MSR-store list.
*
* Run 1 (control): L2 is active and runs until the VMX-preemption timer
* expires. Run 2: L2 is entered in the HLT activity state with the timer
* at zero, so the VM-Exit is synthesized without a hardware VM-Entry.
* Run 3: L2 is active again with the timer at zero. In all three runs the
* stored TSC must lie between the RDTSC before VMLAUNCH or VMRESUME and the
* RDTSC after the VM-Exit.
*/
#include <stdio.h>
#include "test_util.h"
#include "kvm_util.h"
#include "processor.h"
#include "vmx.h"
#define GUEST_ACTIVITY_HLT 1
static struct vmx_msr_entry tsc_store = { .index = MSR_IA32_TSC };
static void l2_guest_code(void)
{
for (;;)
cpu_relax();
}
static void l1_guest_code(struct vmx_pages *vmx_pages, u64 store_gpa)
{
u64 before, after;
prepare_for_vmx_operation(vmx_pages);
load_vmcs(vmx_pages);
prepare_vmcs(vmx_pages, l2_guest_code);
vmwrite(PIN_BASED_VM_EXEC_CONTROL,
vmread(PIN_BASED_VM_EXEC_CONTROL) |
PIN_BASED_VMX_PREEMPTION_TIMER);
vmwrite(VM_EXIT_MSR_STORE_ADDR, store_gpa);
vmwrite(VM_EXIT_MSR_STORE_COUNT, 1);
/* Run 1: active L2, the timer expires while L2 runs. */
vmwrite(VMX_PREEMPTION_TIMER_VALUE, 100000);
before = rdtsc();
vmlaunch();
after = rdtsc();
GUEST_SYNC_ARGS(1, before, tsc_store.value, after,
vmread(VM_EXIT_REASON));
/* Run 2: halted L2, timer value 0, VM-Exit synthesized by KVM. */
vmwrite(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_HLT);
vmwrite(VMX_PREEMPTION_TIMER_VALUE, 0);
before = rdtsc();
vmresume();
after = rdtsc();
GUEST_SYNC_ARGS(2, before, tsc_store.value, after,
vmread(VM_EXIT_REASON));
/* Run 3: active L2 with timer value 0. */
vmwrite(GUEST_ACTIVITY_STATE, 0);
vmwrite(VMX_PREEMPTION_TIMER_VALUE, 0);
before = rdtsc();
vmresume();
after = rdtsc();
GUEST_SYNC_ARGS(3, before, tsc_store.value, after,
vmread(VM_EXIT_REASON));
GUEST_DONE();
}
int main(void)
{
struct kvm_vcpu *vcpu;
struct kvm_vm *vm;
struct ucall uc;
gva_t vmx_pages_gva;
bool failed = false;
TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX));
vm = vm_create_with_one_vcpu(&vcpu, l1_guest_code);
vcpu_alloc_vmx(vm, &vmx_pages_gva);
vcpu_args_set(vcpu, 2, vmx_pages_gva,
addr_gva2gpa(vm, (uintptr_t)&tsc_store));
for (;;) {
vcpu_run(vcpu);
TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
switch (get_ucall(vcpu, &uc)) {
case UCALL_ABORT:
REPORT_GUEST_ASSERT(uc);
case UCALL_SYNC: {
u64 before = uc.args[2], stored = uc.args[3];
u64 after = uc.args[4];
bool ok = stored >= before && stored <= after;
TEST_ASSERT_EQ(uc.args[5],
EXIT_REASON_PREEMPTION_TIMER);
printf("run %lu (%s): before=%lu stored=%lu
after=%lu: %s\n",
uc.args[1],
uc.args[1] == 1 ? "active L2" :
uc.args[1] == 2 ? "halted L2" : "active
L2, timer 0",
before, stored, after,
ok ? "stored TSC is in [before, after]" :
"stored TSC is NOT in [before, after]");
failed |= !ok;
break;
}
case UCALL_DONE:
kvm_vm_free(vm);
TEST_ASSERT(!failed,
"L1 received a stale TSC in the
VM-Exit MSR-store list");
return 0;
default:
TEST_FAIL("Unexpected ucall %lu", uc.cmd);
}
}
}