Skip to content

Commit 62838fa

Browse files
committed
KVM: selftests: Relax assertion on HLT exits if CPU supports Idle HLT
If the CPU supports Idle HLT, which elides HLT VM-Exits if the vCPU has an unmasked pending IRQ or NMI, relax the xAPIC IPI test's assertion on the number of HLT exits to only require that the number of exits is less than or equal to the number of HLT instructions that were executed. I.e. don't fail the test if Idle HLT does what it's supposed to do. Note, unfortunately there's no way to determine if *KVM* supports Idle HLT, as this_cpu_has() checks raw CPU support, and kvm_cpu_has() checks what can be exposed to L1, i.e. the latter would check if KVM supports nested Idle HLT. But, since the assert is purely bonus coverage, checking for CPU support is good enough. Cc: Manali Shukla <Manali.Shukla@amd.com> Tested-by: Manali Shukla <Manali.Shukla@amd.com> Link: https://lore.kernel.org/r/20250226231809.3183093-1-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 75418e2 commit 62838fa

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

tools/testing/selftests/kvm/include/x86/processor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ struct kvm_x86_cpu_feature {
197197
#define X86_FEATURE_PAUSEFILTER KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 10)
198198
#define X86_FEATURE_PFTHRESHOLD KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 12)
199199
#define X86_FEATURE_VGIF KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 16)
200+
#define X86_FEATURE_IDLE_HLT KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 30)
200201
#define X86_FEATURE_SEV KVM_X86_CPU_FEATURE(0x8000001F, 0, EAX, 1)
201202
#define X86_FEATURE_SEV_ES KVM_X86_CPU_FEATURE(0x8000001F, 0, EAX, 3)
202203

tools/testing/selftests/kvm/x86/xapic_ipi_test.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,18 @@ int main(int argc, char *argv[])
465465
cancel_join_vcpu_thread(threads[0], params[0].vcpu);
466466
cancel_join_vcpu_thread(threads[1], params[1].vcpu);
467467

468-
TEST_ASSERT_EQ(data->hlt_count, vcpu_get_stat(params[0].vcpu, halt_exits));
468+
/*
469+
* If the host support Idle HLT, i.e. KVM *might* be using Idle HLT,
470+
* then the number of HLT exits may be less than the number of HLTs
471+
* that were executed, as Idle HLT elides the exit if the vCPU has an
472+
* unmasked, pending IRQ (or NMI).
473+
*/
474+
if (this_cpu_has(X86_FEATURE_IDLE_HLT))
475+
TEST_ASSERT(data->hlt_count >= vcpu_get_stat(params[0].vcpu, halt_exits),
476+
"HLT insns = %lu, HLT exits = %lu",
477+
data->hlt_count, vcpu_get_stat(params[0].vcpu, halt_exits));
478+
else
479+
TEST_ASSERT_EQ(data->hlt_count, vcpu_get_stat(params[0].vcpu, halt_exits));
469480

470481
fprintf(stderr,
471482
"Test successful after running for %d seconds.\n"

0 commit comments

Comments
 (0)