This patch can fix the bug. I tried kvm-unit-tests vmx with your patch ten times on Ice lake and Cooper lake and the failure didn't happen. Thanks Jim! -----Original Message----- From: Jim Mattson <jmattson@xxxxxxxxxx> Sent: Wednesday, June 29, 2022 10:57 AM To: kvm@xxxxxxxxxxxxxxx; pbonzini@xxxxxxxxxx; Yang, Lixiao <lixiao.yang@xxxxxxxxx>; nadav.amit@xxxxxxxxx Cc: Jim Mattson <jmattson@xxxxxxxxxx> Subject: [kvm-unit-tests PATCH] x86: VMX: Fix the VMX-preemption timer expiration test When the VMX-preemption timer fires between the test for "vmx_get_test_stage() == 0" and the subsequent rdtsc instruction, the final VM-entry to finish the guest will inadvertently update vmx_preemption_timer_expiry_finish. Move the code to finish the guest until after the calculations involving vmx_preemption_timer_expiry_finish are done, so that it doesn't matter if vmx_preemption_timer_expiry_finish is clobbered. Signed-off-by: Jim Mattson <jmattson@xxxxxxxxxx> Fixes: b49a1a6d4e23 ("x86: VMX: Add a VMX-preemption timer expiration test") --- x86/vmx_tests.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/x86/vmx_tests.c b/x86/vmx_tests.c index 4d581e7085ea..8a1393668d93 100644 --- a/x86/vmx_tests.c +++ b/x86/vmx_tests.c @@ -9194,16 +9194,16 @@ static void vmx_preemption_timer_expiry_test(void) reason = (u32)vmcs_read(EXI_REASON); TEST_ASSERT(reason == VMX_PREEMPT); - vmcs_clear_bits(PIN_CONTROLS, PIN_PREEMPT); - vmx_set_test_stage(1); - enter_guest(); - tsc_deadline = ((vmx_preemption_timer_expiry_start >> misc.pt_bit) << misc.pt_bit) + (preemption_timer_value << misc.pt_bit); report(vmx_preemption_timer_expiry_finish < tsc_deadline, "Last stored guest TSC (%lu) < TSC deadline (%lu)", vmx_preemption_timer_expiry_finish, tsc_deadline); + + vmcs_clear_bits(PIN_CONTROLS, PIN_PREEMPT); + vmx_set_test_stage(1); + enter_guest(); } static void vmx_db_test_guest(void) -- 2.37.0.rc0.161.g10f37bed90-goog