selftests: kvm/x86: test if it checks all the bits in the LBR_FMT bit-field
PATCH 1/1 selftests: kvm/x86: test if it checks all the bits in the LBR_FMT bit-field
From: Like Xu <[email protected]>
User space only enable guest LBR feature when the exactly supported LBR format value is initialized to the MSR_IA32_PERF_CAPABILITIES. The input is also invalid if only partially supported bits are set.
Note for PEBS feature, the PEBS_FORMAT bit field is the primary concern, thus if the PEBS_FORMAT input is empty, the other bits check about PEBS (like PEBS_TRAP or ARCH_REG) will be ignored.
--- tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c +8 -1
@@ -13,6 +13,7 @@#define _GNU_SOURCE/* for program_invocation_short_name */ #include <sys/ioctl.h> +#include <linux/bitmap.h> #include "kvm_util.h" #include "vmx.h"
@@ -56,7 +57,7 @@ int main(int argc, char *argv[])const struct kvm_cpuid_entry2 *entry_a_0;struct kvm_vm *vm;struct kvm_vcpu *vcpu; -int ret; +int ret, bit;union cpuid10_eax eax;union perf_capabilities host_cap;
@@ -97,6 +98,12 @@ int main(int argc, char *argv[]) ret =_vcpu_set_msr (vcpu,MSR_IA32_PERF_CAPABILITIES ,0x30 );TEST_ASSERT (ret ==0 ,"Bad PERF_CAPABILITIES didn't fail." ); +/* testcase 4, reject LBR_FMT if only partially supported bits are set */ +for_each_set_bit (bit, (unsigned long *)&host_cap.capabilities,6 ) { + ret =_vcpu_set_msr (vcpu,MSR_IA32_PERF_CAPABILITIES ,BIT_ULL (bit)); +TEST_ASSERT (ret ==0 ,"Bad PERF_CAPABILITIES didn't fail." ); + } +printf ("Completed perf capability tests.\n" );kvm_vm_free (vm); }
Signature · 1 line
2.37.1
Sean Christopherson
reviewerQuoted 37 lines · Like Xu
From: Like Xu <[email protected]>
User space only enable guest LBR feature when the exactly supported LBR format value is initialized to the
MSR_IA32_PERF_CAPABILITIES. The input is also invalid if only partially supported bits are set.Note for PEBS feature, the
PEBS_FORMATbit field is the primary concern, thus if thePEBS_FORMATinput is empty, the other bits check about PEBS (likePEBS_TRAPorARCH_REG) will be ignored.Signed-off-by Like Xu <[email protected]>--- tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c +2
-1@@ -13,6 +13,7 @@#define _GNU_SOURCE/* for program_invocation_short_name */ #include <sys/ioctl.h> +#include <linux/bitmap.h> #include "kvm_util.h" #include "vmx.h" @@ -56,7 +57,7 @@ int main(int argc, char *argv[])const struct kvm_cpuid_entry2 *entry_a_0;struct kvm_vm *vm;struct kvm_vcpu *vcpu; -int ret; +int ret, bit;union cpuid10_eax eax;union perf_capabilities host_cap;
tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c int main(int argc, char *argv[]) · line 105
Quoted 8 lines
tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c +5
-0@@ -97,6 +98,12 @@ int main(int argc, char *argv[]) ret =_vcpu_set_msr (vcpu,MSR_IA32_PERF_CAPABILITIES ,0x30 );TEST_ASSERT (ret ==0 ,"Bad PERF_CAPABILITIES didn't fail." ); +/* testcase 4, reject LBR_FMT if only partially supported bits are set */ +for_each_set_bit (bit, (unsigned long *)&host_cap.capabilities,6 ) { + ret =_vcpu_set_msr (vcpu,MSR_IA32_PERF_CAPABILITIES ,BIT_ULL (bit)); +TEST_ASSERT (ret ==0 ,"Bad PERF_CAPABILITIES didn't fail." ); + }
Rather than add another testcase and target only a subset of possible values, what about replacing (fixing IMO) testcase #3 with fully exhaustive approach?
---
From: Sean Christopherson <[email protected]>
Date: Thu, 4 Aug 2022 12:18:15 -0700
Subject: [PATCH] KVM: selftests: Test all possible "invalid"
PERF_CAPABILITIES.LBR_FMT vals
Test all possible input values to verify that KVM rejects all values except the exact host value. Due to the LBR format affecting the core functionality of LBRs, KVM can't emulate "other" formats, so even though there are a variety of legal values, KVM should reject anything but an exact host match.
--- .../selftests/kvm/x86_64/vmx_pmu_caps_test.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)
tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c +12 -5
@@ -59,6 +59,7 @@ int main(int argc, char *argv[])int ret;union cpuid10_eax eax;union perf_capabilities host_cap; +uint64_t val; host_cap.capabilities =kvm_get_feature_msr (MSR_IA32_PERF_CAPABILITIES ); host_cap.capabilities &= (PMU_CAP_FW_WRITES |PMU_CAP_LBR_FMT );
@@ -91,11 +92,17 @@ int main(int argc, char *argv[])vcpu_set_msr (vcpu,MSR_IA32_PERF_CAPABILITIES , host_cap.lbr_format);ASSERT_EQ (vcpu_get_msr (vcpu,MSR_IA32_PERF_CAPABILITIES ), (u64 )host_cap.lbr_format); -/* testcase 3, check invalid LBR format is rejected */ -/* Note, on Arch LBR capable platforms, LBR_FMT in perf capability msr is 0x3f, -* to avoid the failure, use a true invalid format 0x30 for the test. */ - ret =_vcpu_set_msr (vcpu,MSR_IA32_PERF_CAPABILITIES ,0x30 ); -TEST_ASSERT (ret ==0 ,"Bad PERF_CAPABILITIES didn't fail." ); +/* +* Testcase 3, check that an "invalid" LBR format is rejected. Only an +* exact match of the host's format (and 0/disabled) is allowed. +*/ +for (val =1 ; val <=PMU_CAP_LBR_FMT ; val++) { +if (val == (host_cap.capabilities &PMU_CAP_LBR_FMT )) +continue ; + + ret =_vcpu_set_msr (vcpu,MSR_IA32_PERF_CAPABILITIES , val); +TEST_ASSERT (!ret,"Bad LBR FMT = 0x%lx didn't fail" , val); + }printf ("Completed perf capability tests.\n" );kvm_vm_free (vm);
base-commit: 93472b79715378a2386598d6632c654a2223267b --
Quoted 41 lines · Sean Christopherson
Quoted 40 lines · Like Xu
From: Like Xu <[email protected]>
User space only enable guest LBR feature when the exactly supported LBR format value is initialized to the
MSR_IA32_PERF_CAPABILITIES. The input is also invalid if only partially supported bits are set.Note for PEBS feature, the
PEBS_FORMATbit field is the primary concern, thus if thePEBS_FORMATinput is empty, the other bits check about PEBS (likePEBS_TRAPorARCH_REG) will be ignored.Signed-off-by Like Xu <[email protected]>--- tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c +2
-1@@ -13,6 +13,7 @@#define _GNU_SOURCE/* for program_invocation_short_name */ #include <sys/ioctl.h> +#include <linux/bitmap.h> #include "kvm_util.h" #include "vmx.h" @@ -56,7 +57,7 @@ int main(int argc, char *argv[])const struct kvm_cpuid_entry2 *entry_a_0;struct kvm_vm *vm;struct kvm_vcpu *vcpu; -int ret; +int ret, bit;union cpuid10_eax eax;union perf_capabilities host_cap;@@ -97,6 +98,12 @@ int main(int argc, char *argv[]) ret =_vcpu_set_msr (vcpu,MSR_IA32_PERF_CAPABILITIES ,0x30 );TEST_ASSERT (ret ==0 ,"Bad PERF_CAPABILITIES didn't fail." );
Quoted 8 lines
tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c int main(int argc, char *argv[]) · line 105
Quoted 5 lines
tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c +5
-0+/* testcase 4, reject LBR_FMT if only partially supported bits are set */ +for_each_set_bit (bit, (unsigned long *)&host_cap.capabilities,6 ) { + ret =_vcpu_set_msr (vcpu,MSR_IA32_PERF_CAPABILITIES ,BIT_ULL (bit)); +TEST_ASSERT (ret ==0 ,"Bad PERF_CAPABILITIES didn't fail." ); + }Rather than add another testcase and target only a subset of possible values, what about replacing (fixing IMO) testcase #3 with fully exhaustive approach?
Yeah, much better.
Quoted 36 lines
---
From: Sean Christopherson <[email protected]>
Date: Thu, 4 Aug 2022 12:18:15 -0700
Subject: [PATCH] KVM: selftests: Test all possible "invalid"
PERF_CAPABILITIES.LBR_FMT valsTest all possible input values to verify that KVM rejects all values except the exact host value. Due to the LBR format affecting the core functionality of LBRs, KVM can't emulate "other" formats, so even though there are a variety of legal values, KVM should reject anything but an exact host match.
Suggested-by Like Xu <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- .../selftests/kvm/x86_64/vmx_pmu_caps_test.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c +1
-1@@ -59,6 +59,7 @@ int main(int argc, char *argv[])int ret;union cpuid10_eax eax;union perf_capabilities host_cap; +uint64_t val; host_cap.capabilities =kvm_get_feature_msr (MSR_IA32_PERF_CAPABILITIES ); host_cap.capabilities &= (PMU_CAP_FW_WRITES |PMU_CAP_LBR_FMT );@@ -91,11 +92,17 @@ int main(int argc, char *argv[])vcpu_set_msr (vcpu,MSR_IA32_PERF_CAPABILITIES , host_cap.lbr_format);ASSERT_EQ (vcpu_get_msr (vcpu,MSR_IA32_PERF_CAPABILITIES ), (u64 )host_cap.lbr_format); -/* testcase 3, check invalid LBR format is rejected */
tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c int main(int argc, char *argv[]) · line 100
Quoted 10 lines
tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c +6
-4-/* Note, on Arch LBR capable platforms, LBR_FMT in perf capability msr is 0x3f, -* to avoid the failure, use a true invalid format 0x30 for the test. */ - ret =_vcpu_set_msr (vcpu,MSR_IA32_PERF_CAPABILITIES ,0x30 ); -TEST_ASSERT (ret ==0 ,"Bad PERF_CAPABILITIES didn't fail." ); +/* +* Testcase 3, check that an "invalid" LBR format is rejected. Only an +* exact match of the host's format (and 0/disabled) is allowed. +*/ +for (val =1 ; val <=PMU_CAP_LBR_FMT ; val++) { +if (val == (host_cap.capabilities &PMU_CAP_LBR_FMT ))
Emm,
if (val == host_cap.lbr_format)
to save a minor cost of logical operation.
Quoted 12 lines
tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c +5
-0+continue ; + + ret =_vcpu_set_msr (vcpu,MSR_IA32_PERF_CAPABILITIES , val); +TEST_ASSERT (!ret,"Bad LBR FMT = 0x%lx didn't fail" , val); + }printf ("Completed perf capability tests.\n" );kvm_vm_free (vm);base-commit:
93472b79715378a2386598d6632c654a2223267b--