x86: VMX: Fix the VMX-preemption timer expiration test
PATCH 1/1 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.
--- x86/vmx_tests.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
x86/vmx_tests.c +4 -4
@@ -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 )
Signature · 1 line
2.37.0.rc0.161.g10f37bed90-goog
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 <[email protected]>
Sent: Wednesday, June 29, 2022 10:57 AM
To: [email protected]; [email protected]; Yang, Lixiao <[email protected]>; [email protected]
Cc: Jim Mattson <[email protected]>
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.
--- x86/vmx_tests.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
x86/vmx_tests.c +4 -4
@@ -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
Jim Mattson
Tested-by Lixiao YangQuoted 9 lines · Lixiao
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 <[email protected]>
Sent: Wednesday, June 29, 2022 10:57 AM
To: [email protected]; [email protected]; Yang, Lixiao <[email protected]>; [email protected]
Cc: Jim Mattson <[email protected]>
Subject: [kvm-unit-tests PATCH] x86: VMX: Fix the VMX-preemption timer expiration test
Quoted 6 lines
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 updatevmx_preemption_timer_expiry_finish.Move the code to finish the guest until after the calculations involving
vmx_preemption_timer_expiry_finishare done, so that it doesn't matter ifvmx_preemption_timer_expiry_finishis clobbered.Signed-off-by Jim Mattson <[email protected]>
Quoted 32 lines
Fixes b49a1a6d4e23 ("x86: VMX: Add a VMX-preemption timer expiration test")--- x86/vmx_tests.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)x86/vmx_tests.c +4
-5@@ -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
Quoted 12 lines · Jim Mattson
Quoted 11 lines · Lixiao
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 <[email protected]>
Sent: Wednesday, June 29, 2022 10:57 AM
To: [email protected]; [email protected]; Yang, Lixiao <[email protected]>; [email protected]
Cc: Jim Mattson <[email protected]>
Subject: [kvm-unit-tests PATCH] x86: VMX: Fix the VMX-preemption timer expiration testWhen 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 updatevmx_preemption_timer_expiry_finish.
Quoted 6 lines
Quoted 3 lines
Move the code to finish the guest until after the calculations involving
vmx_preemption_timer_expiry_finishare done, so that it doesn't matter ifvmx_preemption_timer_expiry_finishis clobbered.Signed-off-by Jim Mattson <[email protected]>Reported-by Nadav Amit <[email protected]>Tested-by Lixiao Yang <[email protected]>
Thank you, Jim.
Sorry that I don’t have the time to test the patch right now, but I trust you and Lixiao. :)