Skip to contentSkip to conversation

x86: VMX: Fix the VMX-preemption timer expiration test

PATCH In review Jim Mattson 3 participants 4 messages Jun 29, 2022 – Jul 7, 2022 KVM

lore
Share & tools
lore
#0 lore raw

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.

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 -4
index 4d581e7085ea..8a1393668d93 100644
@@ -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
#1 lore raw

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.

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 -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

author Tested-by Lixiao Yang
#2 lore raw
Quoted 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 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.

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

#3 lore raw
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 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.

Quoted 6 lines
Quoted 3 lines

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.

Thank you, Jim.

Sorry that I don’t have the time to test the patch right now, but I trust you and Lixiao. :)