Skip to content

Commit d8f992e

Browse files
committed
KVM: selftests: Verify LBRs are disabled if vPMU is disabled
Verify that disabling the guest's vPMU via CPUID also disables LBRs. KVM has had at least one bug where LBRs would remain enabled even though the intent was to disable everything PMU related. Link: https://lore.kernel.org/r/20230311004618.920745-22-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 8b95b41 commit d8f992e

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,34 @@ static void test_immutable_perf_capabilities(union perf_capabilities host_cap)
204204
kvm_vm_free(vm);
205205
}
206206

207+
/*
208+
* Test that LBR MSRs are writable when LBRs are enabled, and then verify that
209+
* disabling the vPMU via CPUID also disables LBR support. Set bits 2:0 of
210+
* LBR_TOS as those bits are writable across all uarch implementations (arch
211+
* LBRs will need to poke a different MSR).
212+
*/
213+
static void test_lbr_perf_capabilities(union perf_capabilities host_cap)
214+
{
215+
struct kvm_vcpu *vcpu;
216+
struct kvm_vm *vm;
217+
int r;
218+
219+
if (!host_cap.lbr_format)
220+
return;
221+
222+
vm = vm_create_with_one_vcpu(&vcpu, NULL);
223+
224+
vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, host_cap.capabilities);
225+
vcpu_set_msr(vcpu, MSR_LBR_TOS, 7);
226+
227+
vcpu_clear_cpuid_entry(vcpu, X86_PROPERTY_PMU_VERSION.function);
228+
229+
r = _vcpu_set_msr(vcpu, MSR_LBR_TOS, 7);
230+
TEST_ASSERT(!r, "Writing LBR_TOS should fail after disabling vPMU");
231+
232+
kvm_vm_free(vm);
233+
}
234+
207235
int main(int argc, char *argv[])
208236
{
209237
union perf_capabilities host_cap;
@@ -222,4 +250,5 @@ int main(int argc, char *argv[])
222250
test_fungible_perf_capabilities(host_cap);
223251
test_immutable_perf_capabilities(host_cap);
224252
test_guest_wrmsr_perf_capabilities(host_cap);
253+
test_lbr_perf_capabilities(host_cap);
225254
}

0 commit comments

Comments
 (0)