[PATCH v10 17/18] KVM: selftests: Move and generalize PMU guest measurement helpers

From: Zide Chen

Date: Tue Nov 24 2026 - 16:55:06 EST


Move GUEST_MEASURE_EVENT() from pmu_counters_test.c into
include/x86/pmu.h so the measurement assembly sequence can be shared
across PMU selftests (e.g. Topdown PERF_METRICS).

Introduce GUEST_RUN_PAYLOAD() to execute the loop while selecting the
supported cache-flush instruction (CLFLUSHOPT, CLFLUSH, or NOP).

No functional change intended.

Signed-off-by: Zide Chen <zide.chen@xxxxxxxxx>
---
v10: new patch.
---
tools/testing/selftests/kvm/include/x86/pmu.h | 54 +++++++++++++++++++
.../selftests/kvm/x86/pmu_counters_test.c | 53 +-----------------
2 files changed, 55 insertions(+), 52 deletions(-)

diff --git a/tools/testing/selftests/kvm/include/x86/pmu.h b/tools/testing/selftests/kvm/include/x86/pmu.h
index 608ed83d7c6a..c06ac78db01f 100644
--- a/tools/testing/selftests/kvm/include/x86/pmu.h
+++ b/tools/testing/selftests/kvm/include/x86/pmu.h
@@ -127,4 +127,58 @@ static inline bool this_pmu_has_errata(enum pmu_errata errata)
return pmu_errata_mask & BIT_ULL(errata);
}

+/*
+ * Enable and disable the PMC in a monolithic asm blob to ensure that the
+ * compiler can't insert _any_ code into the measured sequence.
+ *
+ * If CLFLUSH{,OPT} is supported, flush and reload a dummy cacheline on each
+ * loop iteration to force LLC references and misses, i.e. to allow testing
+ * that those events actually count.
+ *
+ * If forced emulation is enabled (and specified), force emulation on a subset
+ * of the measured code to verify that KVM correctly emulates instructions and
+ * branches retired events in conjunction with hardware also counting said
+ * events.
+ */
+#define GUEST_MEASURE_EVENT(_msr, _value, clflush, FEP, _loops) \
+do { \
+ u32 _a = (u32)(_value), _d = (u32)((_value) >> 32); \
+ u32 _c = (_msr); \
+ u32 _dummy = 0; \
+ \
+ __asm__ __volatile__("wrmsr\n\t" \
+ "mov %[loops], %%ecx\n\t" \
+ "1:\n\t" \
+ FEP "enter $0, $0\n\t" \
+ clflush "\n\t" \
+ "mfence\n\t" \
+ "mov (%[ptr]), %%eax\n\t" \
+ FEP "leave\n\t" \
+ FEP "loop 1b\n\t" \
+ FEP "mov %%edi, %%ecx\n\t" \
+ FEP "xor %%eax, %%eax\n\t" \
+ FEP "xor %%edx, %%edx\n\t" \
+ "wrmsr\n\t" \
+ : "+a"(_a), "+d"(_d), "+c"(_c) \
+ : "D"(_msr), [ptr]"S"(&_dummy), \
+ [loops]"i"(_loops) \
+ : "memory", "cc" \
+ ); \
+} while (0)
+
+#define GUEST_RUN_PAYLOAD(_ctrl_msr, _value, FEP, _loops) \
+do { \
+ if (this_cpu_has(X86_FEATURE_CLFLUSHOPT)) \
+ GUEST_MEASURE_EVENT(_ctrl_msr, _value, \
+ "clflushopt (%[ptr])", \
+ FEP, _loops); \
+ else if (this_cpu_has(X86_FEATURE_CLFLUSH)) \
+ GUEST_MEASURE_EVENT(_ctrl_msr, _value, \
+ "clflush (%[ptr])", \
+ FEP, _loops); \
+ else \
+ GUEST_MEASURE_EVENT(_ctrl_msr, _value, "nop", \
+ FEP, _loops); \
+} while (0)
+
#endif /* SELFTEST_KVM_PMU_H */
diff --git a/tools/testing/selftests/kvm/x86/pmu_counters_test.c b/tools/testing/selftests/kvm/x86/pmu_counters_test.c
index 5ae7f2d15d04..4836745bef7f 100644
--- a/tools/testing/selftests/kvm/x86/pmu_counters_test.c
+++ b/tools/testing/selftests/kvm/x86/pmu_counters_test.c
@@ -219,61 +219,10 @@ static void guest_assert_event_count(u8 idx, u32 pmc, u32 pmc_msr)
GUEST_ASSERT_EQ(_rdpmc(pmc), 0xdead);
}

-/*
- * Enable and disable the PMC in a monolithic asm blob to ensure that the
- * compiler can't insert _any_ code into the measured sequence.
- *
- * If CLFLUSH{,OPT} is supported, flush and reload a dummy cacheline on each
- * loop iteration to force LLC references and misses, i.e. to allow testing
- * that those events actually count.
- *
- * If forced emulation is enabled (and specified), force emulation on a subset
- * of the measured code to verify that KVM correctly emulates instructions and
- * branches retired events in conjunction with hardware also counting said
- * events.
- */
-#define GUEST_MEASURE_EVENT(_msr, _value, clflush, FEP, _loops) \
-do { \
- u32 _a = (u32)(_value), _d = (u32)((_value) >> 32); \
- u32 _c = (_msr); \
- u32 _dummy = 0; \
- \
- __asm__ __volatile__("wrmsr\n\t" \
- "mov %[loops], %%ecx\n\t" \
- "1:\n\t" \
- FEP "enter $0, $0\n\t" \
- clflush "\n\t" \
- "mfence\n\t" \
- "mov (%[ptr]), %%eax\n\t" \
- FEP "leave\n\t" \
- FEP "loop 1b\n\t" \
- FEP "mov %%edi, %%ecx\n\t" \
- FEP "xor %%eax, %%eax\n\t" \
- FEP "xor %%edx, %%edx\n\t" \
- "wrmsr\n\t" \
- : "+a"(_a), "+d"(_d), "+c"(_c) \
- : "D"(_msr), [ptr]"S"(&_dummy), \
- [loops]"i"(_loops) \
- : "memory", "cc" \
- ); \
-} while (0)
-
#define GUEST_TEST_EVENT(_idx, _pmc, _pmc_msr, _ctrl_msr, _value, FEP) \
do { \
wrmsr(_pmc_msr, 0); \
- \
- if (this_cpu_has(X86_FEATURE_CLFLUSHOPT)) \
- GUEST_MEASURE_EVENT(_ctrl_msr, _value, \
- "clflushopt (%[ptr])", \
- FEP, NUM_LOOPS); \
- else if (this_cpu_has(X86_FEATURE_CLFLUSH)) \
- GUEST_MEASURE_EVENT(_ctrl_msr, _value, \
- "clflush (%[ptr])", \
- FEP, NUM_LOOPS); \
- else \
- GUEST_MEASURE_EVENT(_ctrl_msr, _value, "nop", \
- FEP, NUM_LOOPS); \
- \
+ GUEST_RUN_PAYLOAD(_ctrl_msr, _value, FEP, NUM_LOOPS); \
guest_assert_event_count(_idx, _pmc, _pmc_msr); \
} while (0)

--
2.56.1