KVM: selftests: PMU fixes for GNR/SRF/CWF
Sean Christopherson
maintainerv4 cover KVM: selftests: PMU fixes for GNR/SRF/CWF
Fix KVM PMU selftests errors encountered on Granite Rapids (GNR), Sierra Forest (SRF) and Clearwater Forest (CWF).
The cover letter from v2 has gory details, as do the patches.
v4:
- Fix an unavailable_mask goof. [Dapeng]
- Fix a bitmask goof (missing BIT_ULL()). [Dapeng]
v3:
- https://lore.kernel.org/all/[email protected]/
- Make PMU errata available to all tests by default.
- Redo testing of "unavailable PMU events" to drastically reduce the number of testcases.
v2:
- https://lore.kernel.org/all/[email protected]
- Add error fix for vmx_pmu_caps_test on GNR/SRF (patch 2/5).
- Opportunistically fix a typo (patch 1/5).
v1: https://lore.kernel.org/all/[email protected]
Dapeng Mi (2):
KVM: selftests: Add timing_info bit support in vmx_pmu_caps_test
KVM: selftests: Validate more arch-events in pmu_counters_test
Sean Christopherson (2):
KVM: selftests: Track unavailable_mask for PMU events as 32-bit value
KVM: selftests: Reduce number of "unavailable PMU events" combos
tested
dongsheng (1):
KVM: selftests: Handle Intel Atom errata that leads to PMU event
overcount
tools/testing/selftests/kvm/include/x86/pmu.h | 26 ++++++++ .../selftests/kvm/include/x86/processor.h | 7 ++- tools/testing/selftests/kvm/lib/x86/pmu.c | 49 +++++++++++++++ .../testing/selftests/kvm/lib/x86/processor.c | 4 ++ .../selftests/kvm/x86/pmu_counters_test.c | 63 +++++++++++++------ .../selftests/kvm/x86/pmu_event_filter_test.c | 4 +- .../selftests/kvm/x86/vmx_pmu_caps_test.c | 3 +- 7 files changed, 135 insertions(+), 21 deletions(-)
base-commit: c8fbf7ceb2ae3f64b0c377c8c21f6df577a13eb4
Signature · 1 line
2.51.0.470.ga7dc726c21-goog
Sean Christopherson
maintainerPATCH v4 1/5 KVM: selftests: Add timing_info bit support in vmx_pmu_caps_test
From: Dapeng Mi <[email protected]>
A new bit PERF_CAPABILITIES[17] called "PEBS_TIMING_INFO" bit is added to indicated if PEBS supports to record timing information in a new "Retried Latency" field.
Since KVM requires user can only set host consistent PEBS capabilities, otherwise the PERF_CAPABILITIES setting would fail, add pebs_timing_info into the "immutable_caps" to block host inconsistent PEBS configuration and cause errors.
Opportunistically drop the anythread_deprecated bit. It isn't and likely never was a PERF_CAPABILITIES flag, the test's definition snuck in when the union was copy+pasted from the kernel's definition.
--- tools/testing/selftests/kvm/x86/vmx_pmu_caps_test.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/x86/vmx_pmu_caps_test.c +2 -1
@@ -29,7 +29,7 @@ static union perf_capabilities { u64 pebs_baseline:1; u64 perf_metrics:1; u64 pebs_output_pt_available:1; - u64 anythread_deprecated:1; + u64 pebs_timing_info:1; };u64 capabilities; } host_cap;
@@ -44,6 +44,7 @@ static const union perf_capabilities immutable_caps = { .pebs_arch_reg = 1, .pebs_format = -1, .pebs_baseline = 1, + .pebs_timing_info = 1, };static const union perf_capabilities format_caps = {
Signature · 1 line
2.51.0.470.ga7dc726c21-goog
Sean Christopherson
maintainerPATCH v4 2/5 KVM: selftests: Track unavailable_mask for PMU events as 32-bit value
In reply to Sean ChristophersonTrack the mask of "unavailable" PMU events as a 32-bit value. While bits 31:9 are currently reserved, silently truncating those bits is unnecessary and asking for missed coverage. To avoid running afoul of the sanity check in vcpu_set_cpuid_property(), explicitly adjust the mask based on the non-reserved bits as reported by KVM's supported CPUID.
Opportunistically update the "all ones" testcase to pass -1u instead of 0xff.
--- tools/testing/selftests/kvm/x86/pmu_counters_test.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/x86/pmu_counters_test.c +5 -2
@@ -311,7 +311,7 @@ static void guest_test_arch_events(void) }static void test_arch_events (uint8_t pmu_version,uint64_t perf_capabilities, -uint8_t length,uint8_t unavailable_mask) +uint8_t length,uint32_t unavailable_mask) {struct kvm_vcpu *vcpu;struct kvm_vm *vm;
@@ -320,6 +320,9 @@ static void test_arch_events(uint8_t pmu_version, uint64_t perf_capabilities,if (!pmu_version)return ; + unavailable_mask &=GENMASK (X86_PROPERTY_PMU_EVENTS_MASK .hi_bit, +X86_PROPERTY_PMU_EVENTS_MASK .lo_bit); + vm =pmu_vm_create_with_one_vcpu (&vcpu, guest_test_arch_events, pmu_version, perf_capabilities);
@@ -630,7 +633,7 @@ static void test_intel_counters(void)*/ for (j =0 ; j <=NR_INTEL_ARCH_EVENTS +1 ; j++) {test_arch_events (v, perf_caps[i], j,0 ); -test_arch_events (v, perf_caps[i], j,0xff ); +test_arch_events (v, perf_caps[i], j,-1u );for (k =0 ; k <NR_INTEL_ARCH_EVENTS ; k++)test_arch_events (v, perf_caps[i], j,BIT (k));
Signature · 1 line
2.51.0.470.ga7dc726c21-goog
Re: [PATCH v4 2/5] KVM: selftests: Track unavailable_mask for PMU events as 32-bit value
Quoted 38 lines · Sean Christopherson
Track the mask of "unavailable" PMU events as a 32-bit value. While bits 31:9 are currently reserved, silently truncating those bits is unnecessary and asking for missed coverage. To avoid running afoul of the sanity check in
vcpu_set_cpuid_property(), explicitly adjust the mask based on the non-reserved bits as reported by KVM's supported CPUID.Opportunistically update the "all ones" testcase to pass -1u instead of
0xff.Signed-off-by Sean Christopherson <[email protected]>--- tools/testing/selftests/kvm/x86/pmu_counters_test.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)tools/testing/selftests/kvm/x86/pmu_counters_test.c +4
-1@@ -311,7 +311,7 @@ static void guest_test_arch_events(void) }static void test_arch_events (uint8_t pmu_version,uint64_t perf_capabilities, -uint8_t length,uint8_t unavailable_mask) +uint8_t length,uint32_t unavailable_mask) {struct kvm_vcpu *vcpu;struct kvm_vm *vm;@@ -320,6 +320,9 @@ static void test_arch_events(uint8_t pmu_version, uint64_t perf_capabilities,if (!pmu_version)return ; + unavailable_mask &=GENMASK (X86_PROPERTY_PMU_EVENTS_MASK .hi_bit, +X86_PROPERTY_PMU_EVENTS_MASK .lo_bit); + vm =pmu_vm_create_with_one_vcpu (&vcpu, guest_test_arch_events, pmu_version, perf_capabilities);
tools/testing/selftests/kvm/x86/pmu_counters_test.c static void test_intel_counters(void) · line 639
Quoted 8 lines
tools/testing/selftests/kvm/x86/pmu_counters_test.c +1
-1@@ -630,7 +633,7 @@ static void test_intel_counters(void)*/ for (j =0 ; j <=NR_INTEL_ARCH_EVENTS +1 ; j++) {test_arch_events (v, perf_caps[i], j,0 ); -test_arch_events (v, perf_caps[i], j,0xff ); +test_arch_events (v, perf_caps[i], j,-1u );for (k =0 ; k <NR_INTEL_ARCH_EVENTS ; k++)test_arch_events (v, perf_caps[i], j,BIT (k));
Sean Christopherson
maintainerPATCH v4 3/5 KVM: selftests: Reduce number of "unavailable PMU events" combos tested
In reply to Sean ChristophersonReduce the number of combinations of unavailable PMU events masks that are testing by the PMU counters test. In reality, testing every possible combination isn't all that interesting, and certainly not worth the tens of seconds (or worse, minutes) of runtime. Fully testing the N^2 space will be especially problematic in the near future, as 5! new arch events are on their way.
Use alternating bit patterns (and 0 and -1u) in the hopes that _if_ there is ever a KVM bug, it's not something horribly convoluted that shows up only with a super specific pattern/value.
--- .../selftests/kvm/x86/pmu_counters_test.c | 38 +++++++++++-------- 1 file changed, 23 insertions(+), 15 deletions(-)
tools/testing/selftests/kvm/x86/pmu_counters_test.c +23 -15
@@ -577,6 +577,26 @@ static void test_intel_counters(void) PMU_CAP_FW_WRITES, }; +/* +* To keep the total runtime reasonable, test only a handful of select, +* semi-arbitrary values for the mask of unavailable PMU events. Test +* 0 (all events available) and all ones (no events available) as well +* as alternating bit sequencues, e.g. to detect if KVM is checking the +* wrong bit(s). +*/ +const uint32_t unavailable_masks[] = { +0x0 , +0xffffffffu , +0xaaaaaaaau , +0x55555555u , +0xf0f0f0f0u , +0x0f0f0f0fu , +0xa0a0a0a0u , +0x0a0a0a0au , +0x50505050u , +0x05050505u , + }; +/* * Test up to PMU v5, which is the current maximum version defined by * Intel, i.e. is the last version that is guaranteed to be backwards
@@ -614,16 +634,7 @@ static void test_intel_counters(void)pr_info ("Testing arch events, PMU version %u, perf_caps = %lx\n" , v, perf_caps[i]); -/* -* To keep the total runtime reasonable, test every -* possible non-zero, non-reserved bitmap combination -* only with the native PMU version and the full bit -* vector length. -*/ -if (v == pmu_version) { -for (k =1 ; k < (BIT (NR_INTEL_ARCH_EVENTS ) -1 ); k++) -test_arch_events (v, perf_caps[i],NR_INTEL_ARCH_EVENTS , k); - } +/* * Test single bits for all PMU version and lengths up * the number of events +1 (to verify KVM doesn't do
@@ -632,11 +643,8 @@ static void test_intel_counters(void)* ones i.e. all events being available and unavailable. */ for (j =0 ; j <=NR_INTEL_ARCH_EVENTS +1 ; j++) { -test_arch_events (v, perf_caps[i], j,0 ); -test_arch_events (v, perf_caps[i], j,-1u ); - -for (k =0 ; k <NR_INTEL_ARCH_EVENTS ; k++) -test_arch_events (v, perf_caps[i], j,BIT (k)); +for (k =1 ; k <ARRAY_SIZE (unavailable_masks); k++) +test_arch_events (v, perf_caps[i], j, unavailable_masks[k]); } pr_info("Testing GP counters, PMU version %u, perf_caps = %lx\n",
Signature · 1 line
2.51.0.470.ga7dc726c21-goog
Sean Christopherson
maintainerPATCH v4 4/5 KVM: selftests: Validate more arch-events in pmu_counters_test
In reply to Sean ChristophersonFrom: Dapeng Mi <[email protected]>
Add support for 5 new architectural events (4 topdown level 1 metrics events and LBR inserts event) that will first show up in Intel's Clearwater Forest CPUs. Detailed info about the new events can be found in SDM section 21.2.7 "Pre-defined Architectural Performance Events".
--- tools/testing/selftests/kvm/include/x86/pmu.h | 10 ++++++++++ tools/testing/selftests/kvm/include/x86/processor.h | 7 ++++++- tools/testing/selftests/kvm/lib/x86/pmu.c | 5 +++++ tools/testing/selftests/kvm/x86/pmu_counters_test.c | 8 ++++++++ 4 files changed, 29 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/include/x86/pmu.h +10 -0
@@ -61,6 +61,11 @@#define INTEL_ARCH_BRANCHES_RETIRED RAW_EVENT(0xc4, 0x00)#define INTEL_ARCH_BRANCHES_MISPREDICTED RAW_EVENT(0xc5, 0x00)#define INTEL_ARCH_TOPDOWN_SLOTS RAW_EVENT(0xa4, 0x01) +#define INTEL_ARCH_TOPDOWN_BE_BOUND RAW_EVENT(0xa4, 0x02) +#define INTEL_ARCH_TOPDOWN_BAD_SPEC RAW_EVENT(0x73, 0x00) +#define INTEL_ARCH_TOPDOWN_FE_BOUND RAW_EVENT(0x9c, 0x01) +#define INTEL_ARCH_TOPDOWN_RETIRING RAW_EVENT(0xc2, 0x02) +#define INTEL_ARCH_LBR_INSERTS RAW_EVENT(0xe4, 0x01)#define AMD_ZEN_CORE_CYCLES RAW_EVENT(0x76, 0x00)#define AMD_ZEN_INSTRUCTIONS_RETIRED RAW_EVENT(0xc0, 0x00)
@@ -80,6 +85,11 @@ enum intel_pmu_architectural_events {INTEL_ARCH_BRANCHES_RETIRED_INDEX ,INTEL_ARCH_BRANCHES_MISPREDICTED_INDEX ,INTEL_ARCH_TOPDOWN_SLOTS_INDEX , +INTEL_ARCH_TOPDOWN_BE_BOUND_INDEX , +INTEL_ARCH_TOPDOWN_BAD_SPEC_INDEX , +INTEL_ARCH_TOPDOWN_FE_BOUND_INDEX , +INTEL_ARCH_TOPDOWN_RETIRING_INDEX , +INTEL_ARCH_LBR_INSERTS_INDEX , NR_INTEL_ARCH_EVENTS, };
tools/testing/selftests/kvm/include/x86/processor.h +6 -1
@@ -265,7 +265,7 @@ struct kvm_x86_cpu_property {#define X86_PROPERTY_PMU_NR_GP_COUNTERS KVM_X86_CPU_PROPERTY(0xa, 0, EAX, 8, 15)#define X86_PROPERTY_PMU_GP_COUNTERS_BIT_WIDTH KVM_X86_CPU_PROPERTY(0xa, 0, EAX, 16, 23)#define X86_PROPERTY_PMU_EBX_BIT_VECTOR_LENGTH KVM_X86_CPU_PROPERTY(0xa, 0, EAX, 24, 31) -#define X86_PROPERTY_PMU_EVENTS_MASK KVM_X86_CPU_PROPERTY(0xa, 0, EBX, 0, 7) +#define X86_PROPERTY_PMU_EVENTS_MASK KVM_X86_CPU_PROPERTY(0xa, 0, EBX, 0, 12)#define X86_PROPERTY_PMU_FIXED_COUNTERS_BITMASK KVM_X86_CPU_PROPERTY(0xa, 0, ECX, 0, 31)#define X86_PROPERTY_PMU_NR_FIXED_COUNTERS KVM_X86_CPU_PROPERTY(0xa, 0, EDX, 0, 4)#define X86_PROPERTY_PMU_FIXED_COUNTERS_BIT_WIDTH KVM_X86_CPU_PROPERTY(0xa, 0, EDX, 5, 12)
@@ -332,6 +332,11 @@ struct kvm_x86_pmu_feature {#define X86_PMU_FEATURE_BRANCH_INSNS_RETIRED KVM_X86_PMU_FEATURE(EBX, 5)#define X86_PMU_FEATURE_BRANCHES_MISPREDICTED KVM_X86_PMU_FEATURE(EBX, 6)#define X86_PMU_FEATURE_TOPDOWN_SLOTS KVM_X86_PMU_FEATURE(EBX, 7) +#define X86_PMU_FEATURE_TOPDOWN_BE_BOUND KVM_X86_PMU_FEATURE(EBX, 8) +#define X86_PMU_FEATURE_TOPDOWN_BAD_SPEC KVM_X86_PMU_FEATURE(EBX, 9) +#define X86_PMU_FEATURE_TOPDOWN_FE_BOUND KVM_X86_PMU_FEATURE(EBX, 10) +#define X86_PMU_FEATURE_TOPDOWN_RETIRING KVM_X86_PMU_FEATURE(EBX, 11) +#define X86_PMU_FEATURE_LBR_INSERTS KVM_X86_PMU_FEATURE(EBX, 12)#define X86_PMU_FEATURE_INSNS_RETIRED_FIXED KVM_X86_PMU_FEATURE(ECX, 0)#define X86_PMU_FEATURE_CPU_CYCLES_FIXED KVM_X86_PMU_FEATURE(ECX, 1)
tools/testing/selftests/kvm/lib/x86/pmu.c +5 -0
@@ -19,6 +19,11 @@ const uint64_t intel_pmu_arch_events[] = {INTEL_ARCH_BRANCHES_RETIRED ,INTEL_ARCH_BRANCHES_MISPREDICTED ,INTEL_ARCH_TOPDOWN_SLOTS , +INTEL_ARCH_TOPDOWN_BE_BOUND , +INTEL_ARCH_TOPDOWN_BAD_SPEC , +INTEL_ARCH_TOPDOWN_FE_BOUND , +INTEL_ARCH_TOPDOWN_RETIRING , +INTEL_ARCH_LBR_INSERTS , };kvm_static_assert (ARRAY_SIZE (intel_pmu_arch_events) ==NR_INTEL_ARCH_EVENTS );
tools/testing/selftests/kvm/x86/pmu_counters_test.c +8 -0
@@ -75,6 +75,11 @@ static struct kvm_intel_pmu_event intel_event_to_feature(uint8_t idx) [INTEL_ARCH_BRANCHES_RETIRED_INDEX] = { X86_PMU_FEATURE_BRANCH_INSNS_RETIRED, X86_PMU_FEATURE_NULL }, [INTEL_ARCH_BRANCHES_MISPREDICTED_INDEX] = { X86_PMU_FEATURE_BRANCHES_MISPREDICTED, X86_PMU_FEATURE_NULL }, [INTEL_ARCH_TOPDOWN_SLOTS_INDEX] = { X86_PMU_FEATURE_TOPDOWN_SLOTS, X86_PMU_FEATURE_TOPDOWN_SLOTS_FIXED }, + [INTEL_ARCH_TOPDOWN_BE_BOUND_INDEX] = { X86_PMU_FEATURE_TOPDOWN_BE_BOUND, X86_PMU_FEATURE_NULL }, + [INTEL_ARCH_TOPDOWN_BAD_SPEC_INDEX] = { X86_PMU_FEATURE_TOPDOWN_BAD_SPEC, X86_PMU_FEATURE_NULL }, + [INTEL_ARCH_TOPDOWN_FE_BOUND_INDEX] = { X86_PMU_FEATURE_TOPDOWN_FE_BOUND, X86_PMU_FEATURE_NULL }, + [INTEL_ARCH_TOPDOWN_RETIRING_INDEX] = { X86_PMU_FEATURE_TOPDOWN_RETIRING, X86_PMU_FEATURE_NULL }, + [INTEL_ARCH_LBR_INSERTS_INDEX] = { X86_PMU_FEATURE_LBR_INSERTS, X86_PMU_FEATURE_NULL }, };kvm_static_assert (ARRAY_SIZE (__intel_event_to_feature) ==NR_INTEL_ARCH_EVENTS );
@@ -171,9 +176,12 @@ static void guest_assert_event_count(uint8_t idx, uint32_t pmc, uint32_t pmc_msr fallthrough;case INTEL_ARCH_CPU_CYCLES_INDEX :case INTEL_ARCH_REFERENCE_CYCLES_INDEX : +case INTEL_ARCH_TOPDOWN_BE_BOUND_INDEX : +case INTEL_ARCH_TOPDOWN_FE_BOUND_INDEX :GUEST_ASSERT_NE (count,0 );break ;case INTEL_ARCH_TOPDOWN_SLOTS_INDEX : +case INTEL_ARCH_TOPDOWN_RETIRING_INDEX :__GUEST_ASSERT (count >=NUM_INSNS_RETIRED ,"Expected top-down slots >= %u, got count = %lu" ,NUM_INSNS_RETIRED , count);
Signature · 1 line
2.51.0.470.ga7dc726c21-goog
Sean Christopherson
maintainerPATCH v4 5/5 KVM: selftests: Handle Intel Atom errata that leads to PMU event overcount
In reply to Sean ChristophersonFrom: dongsheng <[email protected]>
Add a PMU errata framework and use it to relax precise event counts on Atom platforms that overcount "Instruction Retired" and "Branch Instruction Retired" events, as the overcount issues on VM-Exit/VM-Entry are impossible to prevent from userspace, e.g. the test can't prevent host IRQs.
Setup errata during early initialization and automatically sync the mask to VMs so that tests can check for errata without having to manually manage host=>guest variables.
For Intel Atom CPUs, the PMU events "Instruction Retired" or "Branch Instruction Retired" may be overcounted for some certain instructions, like FAR CALL/JMP, RETF, IRET, VMENTRY/VMEXIT/VMPTRLD and complex SGX/SMX/CSTATE instructions/flows.
The detailed information can be found in the errata (section SRF7):
https://edc.intel.com/content/www/us/en/design/products-and-solutions/processors-and-chipsets/sierra-forest/xeon-6700-series-processor-with-e-cores-specification-update/errata-details/
For the Atom platforms before Sierra Forest (including Sierra Forest), Both 2 events "Instruction Retired" and "Branch Instruction Retired" would be overcounted on these certain instructions, but for Clearwater Forest only "Instruction Retired" event is overcounted on these instructions.
--- tools/testing/selftests/kvm/include/x86/pmu.h | 16 +++++++ tools/testing/selftests/kvm/lib/x86/pmu.c | 44 +++++++++++++++++++ .../testing/selftests/kvm/lib/x86/processor.c | 4 ++ .../selftests/kvm/x86/pmu_counters_test.c | 12 ++++- .../selftests/kvm/x86/pmu_event_filter_test.c | 4 +- 5 files changed, 77 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/include/x86/pmu.h +16 -0
@@ -5,8 +5,11 @@#ifndef SELFTEST_KVM_PMU_H #define SELFTEST_KVM_PMU_H +#include <stdbool.h> #include <stdint.h> +#include <linux/bits.h> +#define KVM_PMU_EVENT_FILTER_MAX_EVENTS 300/*
@@ -104,4 +107,17 @@ enum amd_pmu_zen_events {extern const uint64_t intel_pmu_arch_events[];extern const uint64_t amd_pmu_zen_events[]; +enum pmu_errata { +INSTRUCTIONS_RETIRED_OVERCOUNT , +BRANCHES_RETIRED_OVERCOUNT , +}; +extern uint64_t pmu_errata_mask; + +void kvm_init_pmu_errata (void ); + +static inline bool this_pmu_has_errata (enum pmu_errata errata) +{ +return pmu_errata_mask &BIT_ULL (errata); +} +#endif /* SELFTEST_KVM_PMU_H */
tools/testing/selftests/kvm/lib/x86/pmu.c +44 -0
53 lines not on this page · raw message
tools/testing/selftests/kvm/lib/x86/processor.c +4 -0
@@ -6,6 +6,7 @@#include "linux/bitmap.h" #include "test_util.h" #include "kvm_util.h" +#include "pmu.h" #include "processor.h" #include "sev.h"
@@ -638,6 +639,7 @@ void kvm_arch_vm_post_create(struct kvm_vm *vm)sync_global_to_guest (vm, host_cpu_is_intel);sync_global_to_guest (vm, host_cpu_is_amd);sync_global_to_guest (vm, is_forced_emulation_enabled); +sync_global_to_guest (vm, pmu_errata_mask);if (is_sev_vm (vm)) {struct kvm_sev_init init = {0 };
@@ -1269,6 +1271,8 @@ void kvm_selftest_arch_init(void) host_cpu_is_intel =this_cpu_is_intel (); host_cpu_is_amd =this_cpu_is_amd (); is_forced_emulation_enabled =kvm_is_forced_emulation_enabled (); + +kvm_init_pmu_errata (); }bool sys_clocksource_is_based_on_tsc (void )
tools/testing/selftests/kvm/x86/pmu_counters_test.c +10 -2
@@ -163,10 +163,18 @@ static void guest_assert_event_count(uint8_t idx, uint32_t pmc, uint32_t pmc_msrswitch (idx) {case INTEL_ARCH_INSTRUCTIONS_RETIRED_INDEX : -GUEST_ASSERT_EQ (count,NUM_INSNS_RETIRED ); +/* Relax precise count check due to VM-EXIT/VM-ENTRY overcount issue */ +if (this_pmu_has_errata (INSTRUCTIONS_RETIRED_OVERCOUNT )) +GUEST_ASSERT (count >=NUM_INSNS_RETIRED ); +else +GUEST_ASSERT_EQ (count,NUM_INSNS_RETIRED );break ;case INTEL_ARCH_BRANCHES_RETIRED_INDEX : -GUEST_ASSERT_EQ (count,NUM_BRANCH_INSNS_RETIRED ); +/* Relax precise count check due to VM-EXIT/VM-ENTRY overcount issue */ +if (this_pmu_has_errata (BRANCHES_RETIRED_OVERCOUNT )) +GUEST_ASSERT (count >=NUM_BRANCH_INSNS_RETIRED ); +else +GUEST_ASSERT_EQ (count,NUM_BRANCH_INSNS_RETIRED );break ;case INTEL_ARCH_LLC_REFERENCES_INDEX :case INTEL_ARCH_LLC_MISSES_INDEX :
tools/testing/selftests/kvm/x86/pmu_event_filter_test.c +3 -1
@@ -214,8 +214,10 @@ static void remove_event(struct __kvm_pmu_event_filter *f, uint64_t event) do { \ uint64_t br = pmc_results.branches_retired; \ uint64_t ir = pmc_results.instructions_retired; \ + bool br_matched = this_pmu_has_errata(BRANCHES_RETIRED_OVERCOUNT) ? \ + br >= NUM_BRANCHES : br == NUM_BRANCHES; \ \ - if (br && br != NUM_BRANCHES) \ + if (br && !br_matched) \ pr_info("%s: Branch instructions retired = %lu (expected %u)\n", \ __func__, br, NUM_BRANCHES); \ TEST_ASSERT(br, "%s: Branch instructions retired = %lu (expected > 0)", \
Signature · 1 line
2.51.0.470.ga7dc726c21-goog
Re: [PATCH v4 5/5] KVM: selftests: Handle Intel Atom errata that leads to PMU event overcount
Quoted 191 lines · Sean Christopherson
From: dongsheng <[email protected]>
Add a PMU errata framework and use it to relax precise event counts on Atom platforms that overcount "Instruction Retired" and "Branch Instruction Retired" events, as the overcount issues on VM-Exit/VM-Entry are impossible to prevent from userspace, e.g. the test can't prevent host IRQs.
Setup errata during early initialization and automatically sync the mask to VMs so that tests can check for errata without having to manually manage host=>guest variables.
For Intel Atom CPUs, the PMU events "Instruction Retired" or "Branch Instruction Retired" may be overcounted for some certain instructions, like FAR CALL/JMP, RETF, IRET, VMENTRY/VMEXIT/VMPTRLD and complex SGX/SMX/CSTATE instructions/flows.
The detailed information can be found in the errata (section SRF7):
https://edc.intel.com/content/www/us/en/design/products-and-solutions/processors-and-chipsets/sierra-forest/xeon-6700-series-processor-with-e-cores-specification-update/errata-details/For the Atom platforms before Sierra Forest (including Sierra Forest), Both 2 events "Instruction Retired" and "Branch Instruction Retired" would be overcounted on these certain instructions, but for Clearwater Forest only "Instruction Retired" event is overcounted on these instructions.
Signed-off-by dongsheng <[email protected]>Co-developed-by Dapeng Mi <[email protected]>Signed-off-by Dapeng Mi <[email protected]>Tested-by Yi Lai <[email protected]>Co-developed-by Sean Christopherson <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- tools/testing/selftests/kvm/include/x86/pmu.h | 16 +++++++ tools/testing/selftests/kvm/lib/x86/pmu.c | 44 +++++++++++++++++++ .../testing/selftests/kvm/lib/x86/processor.c | 4 ++ .../selftests/kvm/x86/pmu_counters_test.c | 12 ++++- .../selftests/kvm/x86/pmu_event_filter_test.c | 4 +- 5 files changed, 77 insertions(+), 3 deletions(-)tools/testing/selftests/kvm/include/x86/pmu.h +16
-0@@ -5,8 +5,11 @@#ifndef SELFTEST_KVM_PMU_H #define SELFTEST_KVM_PMU_H +#include <stdbool.h> #include <stdint.h> +#include <linux/bits.h> +#define KVM_PMU_EVENT_FILTER_MAX_EVENTS 300/* @@ -104,4 +107,17 @@ enum amd_pmu_zen_events {extern const uint64_t intel_pmu_arch_events[];extern const uint64_t amd_pmu_zen_events[]; +enum pmu_errata { +INSTRUCTIONS_RETIRED_OVERCOUNT , +BRANCHES_RETIRED_OVERCOUNT , +}; +extern uint64_t pmu_errata_mask; + +void kvm_init_pmu_errata (void ); + +static inline bool this_pmu_has_errata (enum pmu_errata errata) +{ +return pmu_errata_mask &BIT_ULL (errata); +} +#endif /* SELFTEST_KVM_PMU_H */ tools/testing/selftests/kvm/lib/x86/pmu.c +44
-0@@ -8,6 +8,7 @@#include <linux/kernel.h> #include "kvm_util.h" +#include "processor.h" #include "pmu.h" const uint64_t intel_pmu_arch_events[] = {@@ -34,3 +35,46 @@ const uint64_t amd_pmu_zen_events[] = {AMD_ZEN_BRANCHES_MISPREDICTED , };kvm_static_assert (ARRAY_SIZE (amd_pmu_zen_events) ==NR_AMD_ZEN_EVENTS ); + +/* +* For Intel Atom CPUs, the PMU events "Instruction Retired" or +* "Branch Instruction Retired" may be overcounted for some certain +* instructions, like FAR CALL/JMP, RETF, IRET, VMENTRY/VMEXIT/VMPTRLD +* and complex SGX/SMX/CSTATE instructions/flows. +* +* The detailed information can be found in the errata (section SRF7): +* https://edc.intel.com/content/www/us/en/design/products-and-solutions/processors-and-chipsets/sierra-forest/xeon-6700-series-processor-with-e-cores-specification-update/errata-details/ +* +* For the Atom platforms before Sierra Forest (including Sierra Forest), +* Both 2 events "Instruction Retired" and "Branch Instruction Retired" would +* be overcounted on these certain instructions, but for Clearwater Forest +* only "Instruction Retired" event is overcounted on these instructions. +*/ +static uint64_t get_pmu_errata (void ) +{ +if (!this_cpu_is_intel ()) +return 0 ; + +if (this_cpu_family () !=0x6 ) +return 0 ; + +switch (this_cpu_model ()) { +case 0xDD :/* Clearwater Forest */ +return BIT_ULL (INSTRUCTIONS_RETIRED_OVERCOUNT ); +case 0xAF :/* Sierra Forest */ +case 0x4D :/* Avaton, Rangely */ +case 0x5F :/* Denverton */ +case 0x86 :/* Jacobsville */ +return BIT_ULL (INSTRUCTIONS_RETIRED_OVERCOUNT ) | +BIT_ULL (BRANCHES_RETIRED_OVERCOUNT ); +default : +return 0 ; + } +} + +uint64_t pmu_errata_mask; + +void kvm_init_pmu_errata (void ) +{ + pmu_errata_mask =get_pmu_errata (); +}tools/testing/selftests/kvm/lib/x86/processor.c +4
-0@@ -6,6 +6,7 @@#include "linux/bitmap.h" #include "test_util.h" #include "kvm_util.h" +#include "pmu.h" #include "processor.h" #include "sev.h" @@ -638,6 +639,7 @@ void kvm_arch_vm_post_create(struct kvm_vm *vm)sync_global_to_guest (vm, host_cpu_is_intel);sync_global_to_guest (vm, host_cpu_is_amd);sync_global_to_guest (vm, is_forced_emulation_enabled); +sync_global_to_guest (vm, pmu_errata_mask);if (is_sev_vm (vm)) {struct kvm_sev_init init = {0 };@@ -1269,6 +1271,8 @@ void kvm_selftest_arch_init(void) host_cpu_is_intel =this_cpu_is_intel (); host_cpu_is_amd =this_cpu_is_amd (); is_forced_emulation_enabled =kvm_is_forced_emulation_enabled (); + +kvm_init_pmu_errata (); }bool sys_clocksource_is_based_on_tsc (void )tools/testing/selftests/kvm/x86/pmu_counters_test.c +10
-2@@ -163,10 +163,18 @@ static void guest_assert_event_count(uint8_t idx, uint32_t pmc, uint32_t pmc_msrswitch (idx) {case INTEL_ARCH_INSTRUCTIONS_RETIRED_INDEX : -GUEST_ASSERT_EQ (count,NUM_INSNS_RETIRED ); +/* Relax precise count check due to VM-EXIT/VM-ENTRY overcount issue */ +if (this_pmu_has_errata (INSTRUCTIONS_RETIRED_OVERCOUNT )) +GUEST_ASSERT (count >=NUM_INSNS_RETIRED ); +else +GUEST_ASSERT_EQ (count,NUM_INSNS_RETIRED );break ;case INTEL_ARCH_BRANCHES_RETIRED_INDEX : -GUEST_ASSERT_EQ (count,NUM_BRANCH_INSNS_RETIRED ); +/* Relax precise count check due to VM-EXIT/VM-ENTRY overcount issue */ +if (this_pmu_has_errata (BRANCHES_RETIRED_OVERCOUNT )) +GUEST_ASSERT (count >=NUM_BRANCH_INSNS_RETIRED ); +else +GUEST_ASSERT_EQ (count,NUM_BRANCH_INSNS_RETIRED );break ;case INTEL_ARCH_LLC_REFERENCES_INDEX :case INTEL_ARCH_LLC_MISSES_INDEX :tools/testing/selftests/kvm/x86/pmu_event_filter_test.c +0
-0@@ -214,8 +214,10 @@ static void remove_event(struct __kvm_pmu_event_filter *f, uint64_t event) do { \
tools/testing/selftests/kvm/x86/pmu_event_filter_test.c static void remove_event(struct __kvm_pmu_event_filter *f, uint64_t event) · line 223
Quoted 10 lines
tools/testing/selftests/kvm/x86/pmu_event_filter_test.c +3
-1uint64_t br = pmc_results.branches_retired; \uint64_t ir = pmc_results.instructions_retired; \ +bool br_matched =this_pmu_has_errata (BRANCHES_RETIRED_OVERCOUNT ) ? \ + br >=NUM_BRANCHES : br ==NUM_BRANCHES ; \ \ -if (br && br !=NUM_BRANCHES ) \ +if (br && !br_matched) \pr_info ("%s: Branch instructions retired = %lu (expected %u)\n" , \ __func__, br,NUM_BRANCHES ); \ TEST_ASSERT(br, "%s: Branch instructions retired = %lu (expected > 0)", \
Quoted 45 lines · Sean Christopherson
Fix KVM PMU selftests errors encountered on Granite Rapids (GNR), Sierra Forest (SRF) and Clearwater Forest (CWF).
The cover letter from v2 has gory details, as do the patches.
v4:
- Fix anunavailable_maskgoof. [Dapeng]
- Fix a bitmask goof (missingBIT_ULL()). [Dapeng]v3:
- https://lore.kernel.org/all/[email protected]/
- Make PMU errata available to all tests by default.
- Redo testing of "unavailable PMU events" to drastically reduce the number of testcases.v2:
- https://lore.kernel.org/all/[email protected]
- Add error fix forvmx_pmu_caps_teston GNR/SRF (patch 2/5).
- Opportunistically fix a typo (patch 1/5).v1: https://lore.kernel.org/all/[email protected]
Dapeng Mi (2):
KVM: selftests: Addtiming_infobit support invmx_pmu_caps_test
KVM: selftests: Validate more arch-events inpmu_counters_testSean Christopherson (2):
KVM: selftests: Trackunavailable_maskfor PMU events as 32-bit value
KVM: selftests: Reduce number of "unavailable PMU events" combos
testeddongsheng (1):
KVM: selftests: Handle Intel Atom errata that leads to PMU event
overcounttools/testing/selftests/kvm/include/x86/pmu.h | 26 ++++++++ .../selftests/kvm/include/x86/processor.h | 7 ++- tools/testing/selftests/kvm/lib/x86/pmu.c | 49 +++++++++++++++ .../testing/selftests/kvm/lib/x86/processor.c | 4 ++ .../selftests/kvm/x86/pmu_counters_test.c | 63 +++++++++++++------ .../selftests/kvm/x86/pmu_event_filter_test.c | 4 +- .../selftests/kvm/x86/vmx_pmu_caps_test.c | 3 +- 7 files changed, 135 insertions(+), 21 deletions(-)
Quoted 1 line
base-commit:
c8fbf7ceb2ae3f64b0c377c8c21f6df577a13eb4
Test pmu_counters_test/pmu_event_filter_test/vmx_pmu_caps_test on SPR/GNR/SRF/CWF platforms, no issue is found. Thanks.
Sean Christopherson
maintainerQuoted 10 lines · Sean Christopherson
Fix KVM PMU selftests errors encountered on Granite Rapids (GNR), Sierra Forest (SRF) and Clearwater Forest (CWF).
The cover letter from v2 has gory details, as do the patches.
v4:
- Fix anunavailable_maskgoof. [Dapeng]
- Fix a bitmask goof (missingBIT_ULL()). [Dapeng][...]
Applied to kvm-x86 selftests, thanks!
[1/5] KVM: selftests: Add timing_info bit support in vmx_pmu_caps_test
https://github.com/kvm-x86/linux/commit/210b09fa428c [2/5] KVM: selftests: Track unavailable_mask for PMU events as 32-bit value https://github.com/kvm-x86/linux/commit/571fc2833ed0 [3/5] KVM: selftests: Reduce number of "unavailable PMU events" combos tested https://github.com/kvm-x86/linux/commit/1fcd3053aa1a [4/5] KVM: selftests: Validate more arch-events in pmu_counters_test https://github.com/kvm-x86/linux/commit/2922b5958865 [5/5] KVM: selftests: Handle Intel Atom errata that leads to PMU event overcount https://github.com/kvm-x86/linux/commit/c435978e4ffe