Skip to content

Commit 59cb3ac

Browse files
committed
KVM: selftests: Update x86's KVM PV test to match KVM's disabling exits behavior
Rework x86's KVM PV features test to align with KVM's new, fixed behavior of not allowing userspace to disable HLT-exiting after vCPUs have been created. Rework the core testcase to disable HLT-exiting before creating a vCPU, and opportunistically modify keep the paired VM+vCPU creation to verify that KVM rejects KVM_CAP_X86_DISABLE_EXITS as expected. Reviewed-by: Maxim Levitsky <mlevitsk@redhat.com> Reviewed-by: Binbin Wu <binbin.wu@linux.intel.com> Reviewed-by: Xiaoyao Li <xiaoyao.li@intel.com> Link: https://lore.kernel.org/r/20241128013424.4096668-18-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 7b2658c commit 59cb3ac

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ static void test_pv_unhalt(void)
139139
struct kvm_vm *vm;
140140
struct kvm_cpuid_entry2 *ent;
141141
u32 kvm_sig_old;
142+
int r;
142143

143144
if (!(kvm_check_cap(KVM_CAP_X86_DISABLE_EXITS) & KVM_X86_DISABLE_EXITS_HLT))
144145
return;
@@ -152,19 +153,45 @@ static void test_pv_unhalt(void)
152153
TEST_ASSERT(vcpu_cpuid_has(vcpu, X86_FEATURE_KVM_PV_UNHALT),
153154
"Enabling X86_FEATURE_KVM_PV_UNHALT had no effect");
154155

155-
/* Make sure KVM clears vcpu->arch.kvm_cpuid */
156+
/* Verify KVM disallows disabling exits after vCPU creation. */
157+
r = __vm_enable_cap(vm, KVM_CAP_X86_DISABLE_EXITS, KVM_X86_DISABLE_EXITS_HLT);
158+
TEST_ASSERT(r && errno == EINVAL,
159+
"Disabling exits after vCPU creation didn't fail as expected");
160+
161+
kvm_vm_free(vm);
162+
163+
/* Verify that KVM clear PV_UNHALT from guest CPUID. */
164+
vm = vm_create(1);
165+
vm_enable_cap(vm, KVM_CAP_X86_DISABLE_EXITS, KVM_X86_DISABLE_EXITS_HLT);
166+
167+
vcpu = vm_vcpu_add(vm, 0, NULL);
168+
TEST_ASSERT(!vcpu_cpuid_has(vcpu, X86_FEATURE_KVM_PV_UNHALT),
169+
"vCPU created with PV_UNHALT set by default");
170+
171+
vcpu_set_cpuid_feature(vcpu, X86_FEATURE_KVM_PV_UNHALT);
172+
TEST_ASSERT(!vcpu_cpuid_has(vcpu, X86_FEATURE_KVM_PV_UNHALT),
173+
"PV_UNHALT set in guest CPUID when HLT-exiting is disabled");
174+
175+
/*
176+
* Clobber the KVM PV signature and verify KVM does NOT clear PV_UNHALT
177+
* when KVM PV is not present, and DOES clear PV_UNHALT when switching
178+
* back to the correct signature..
179+
*/
156180
ent = vcpu_get_cpuid_entry(vcpu, KVM_CPUID_SIGNATURE);
157181
kvm_sig_old = ent->ebx;
158182
ent->ebx = 0xdeadbeef;
159183
vcpu_set_cpuid(vcpu);
160184

161-
vm_enable_cap(vm, KVM_CAP_X86_DISABLE_EXITS, KVM_X86_DISABLE_EXITS_HLT);
185+
vcpu_set_cpuid_feature(vcpu, X86_FEATURE_KVM_PV_UNHALT);
186+
TEST_ASSERT(vcpu_cpuid_has(vcpu, X86_FEATURE_KVM_PV_UNHALT),
187+
"PV_UNHALT cleared when using bogus KVM PV signature");
188+
162189
ent = vcpu_get_cpuid_entry(vcpu, KVM_CPUID_SIGNATURE);
163190
ent->ebx = kvm_sig_old;
164191
vcpu_set_cpuid(vcpu);
165192

166193
TEST_ASSERT(!vcpu_cpuid_has(vcpu, X86_FEATURE_KVM_PV_UNHALT),
167-
"KVM_FEATURE_PV_UNHALT is set with KVM_CAP_X86_DISABLE_EXITS");
194+
"PV_UNHALT set in guest CPUID when HLT-exiting is disabled");
168195

169196
/* FIXME: actually test KVM_FEATURE_PV_UNHALT feature */
170197

0 commit comments

Comments
 (0)