Skip to content

Commit b55e7ad

Browse files
committed
KVM: selftests: Expand PMU counters test to verify LLC events
Expand the PMU counters test to verify that LLC references and misses have non-zero counts when the code being executed while the LLC event(s) is active is evicted via CFLUSH{,OPT}. Note, CLFLUSH{,OPT} requires a fence of some kind to ensure the cache lines are flushed before execution continues. Use MFENCE for simplicity (performance is not a concern). Suggested-by: Jim Mattson <[email protected]> Reviewed-by: Dapeng Mi <[email protected]> Tested-by: Dapeng Mi <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sean Christopherson <[email protected]>
1 parent 787071f commit b55e7ad

File tree

1 file changed

+40
-19
lines changed

1 file changed

+40
-19
lines changed

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

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
/*
1515
* Number of "extra" instructions that will be counted, i.e. the number of
1616
* instructions that are needed to set up the loop and then disabled the
17-
* counter. 2 MOV, 2 XOR, 1 WRMSR.
17+
* counter. 1 CLFLUSH/CLFLUSHOPT/NOP, 1 MFENCE, 2 MOV, 2 XOR, 1 WRMSR.
1818
*/
19-
#define NUM_EXTRA_INSNS 5
19+
#define NUM_EXTRA_INSNS 7
2020
#define NUM_INSNS_RETIRED (NUM_BRANCHES + NUM_EXTRA_INSNS)
2121

2222
static uint8_t kvm_pmu_version;
@@ -107,6 +107,12 @@ static void guest_assert_event_count(uint8_t idx,
107107
case INTEL_ARCH_BRANCHES_RETIRED_INDEX:
108108
GUEST_ASSERT_EQ(count, NUM_BRANCHES);
109109
break;
110+
case INTEL_ARCH_LLC_REFERENCES_INDEX:
111+
case INTEL_ARCH_LLC_MISSES_INDEX:
112+
if (!this_cpu_has(X86_FEATURE_CLFLUSHOPT) &&
113+
!this_cpu_has(X86_FEATURE_CLFLUSH))
114+
break;
115+
fallthrough;
110116
case INTEL_ARCH_CPU_CYCLES_INDEX:
111117
case INTEL_ARCH_REFERENCE_CYCLES_INDEX:
112118
GUEST_ASSERT_NE(count, 0);
@@ -123,29 +129,44 @@ static void guest_assert_event_count(uint8_t idx,
123129
GUEST_ASSERT_EQ(_rdpmc(pmc), 0xdead);
124130
}
125131

132+
/*
133+
* Enable and disable the PMC in a monolithic asm blob to ensure that the
134+
* compiler can't insert _any_ code into the measured sequence. Note, ECX
135+
* doesn't need to be clobbered as the input value, @pmc_msr, is restored
136+
* before the end of the sequence.
137+
*
138+
* If CLFUSH{,OPT} is supported, flush the cacheline containing (at least) the
139+
* start of the loop to force LLC references and misses, i.e. to allow testing
140+
* that those events actually count.
141+
*/
142+
#define GUEST_MEASURE_EVENT(_msr, _value, clflush) \
143+
do { \
144+
__asm__ __volatile__("wrmsr\n\t" \
145+
clflush "\n\t" \
146+
"mfence\n\t" \
147+
"1: mov $" __stringify(NUM_BRANCHES) ", %%ecx\n\t" \
148+
"loop .\n\t" \
149+
"mov %%edi, %%ecx\n\t" \
150+
"xor %%eax, %%eax\n\t" \
151+
"xor %%edx, %%edx\n\t" \
152+
"wrmsr\n\t" \
153+
:: "a"((uint32_t)_value), "d"(_value >> 32), \
154+
"c"(_msr), "D"(_msr) \
155+
); \
156+
} while (0)
157+
126158
static void __guest_test_arch_event(uint8_t idx, struct kvm_x86_pmu_feature event,
127159
uint32_t pmc, uint32_t pmc_msr,
128160
uint32_t ctrl_msr, uint64_t ctrl_msr_value)
129161
{
130162
wrmsr(pmc_msr, 0);
131163

132-
/*
133-
* Enable and disable the PMC in a monolithic asm blob to ensure that
134-
* the compiler can't insert _any_ code into the measured sequence.
135-
* Note, ECX doesn't need to be clobbered as the input value, @pmc_msr,
136-
* is restored before the end of the sequence.
137-
*/
138-
__asm__ __volatile__("wrmsr\n\t"
139-
"mov $" __stringify(NUM_BRANCHES) ", %%ecx\n\t"
140-
"loop .\n\t"
141-
"mov %%edi, %%ecx\n\t"
142-
"xor %%eax, %%eax\n\t"
143-
"xor %%edx, %%edx\n\t"
144-
"wrmsr\n\t"
145-
:: "a"((uint32_t)ctrl_msr_value),
146-
"d"(ctrl_msr_value >> 32),
147-
"c"(ctrl_msr), "D"(ctrl_msr)
148-
);
164+
if (this_cpu_has(X86_FEATURE_CLFLUSHOPT))
165+
GUEST_MEASURE_EVENT(ctrl_msr, ctrl_msr_value, "clflushopt 1f");
166+
else if (this_cpu_has(X86_FEATURE_CLFLUSH))
167+
GUEST_MEASURE_EVENT(ctrl_msr, ctrl_msr_value, "clflush 1f");
168+
else
169+
GUEST_MEASURE_EVENT(ctrl_msr, ctrl_msr_value, "nop");
149170

150171
guest_assert_event_count(idx, event, pmc, pmc_msr);
151172
}

0 commit comments

Comments
 (0)