* [PATCH 0/2] KVM: selftests: Fix PMU counters test flakiness
@ 2024-06-28 0:55 Sean Christopherson
2024-06-28 0:55 ` [PATCH 1/2] KVM: selftests: Rework macros in PMU counters test to prep for multi-insn loop Sean Christopherson
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Sean Christopherson @ 2024-06-28 0:55 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini; +Cc: kvm, linux-kernel, Maxim Levitsky
v2 of Maxim's patch to fix the flakiness with the PMU counters test do to
a single CLFLUSH not guaranteeing an LLC miss.
v2:
- Add a prep patch to tweak the macros in preparation for moving CLFUSH{,OPT}
into the loop.
- Keep the MFENCE (because paranoia doesn't hurt in this case).
v1: https://lore.kernel.org/all/20240621204305.1730677-1-mlevitsk@redhat.com
Maxim Levitsky (1):
KVM: selftests: Increase robustness of LLC cache misses in PMU
counters test
Sean Christopherson (1):
KVM: selftests: Rework macros in PMU counters test to prep for
multi-insn loop
.../selftests/kvm/x86_64/pmu_counters_test.c | 44 ++++++++++++-------
1 file changed, 29 insertions(+), 15 deletions(-)
base-commit: c81b138d5075c6f5ba3419ac1d2a2e7047719c14
--
2.45.2.803.g4e1b14247a-goog
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 1/2] KVM: selftests: Rework macros in PMU counters test to prep for multi-insn loop
2024-06-28 0:55 [PATCH 0/2] KVM: selftests: Fix PMU counters test flakiness Sean Christopherson
@ 2024-06-28 0:55 ` Sean Christopherson
2024-06-28 0:55 ` [PATCH 2/2] KVM: selftests: Increase robustness of LLC cache misses in PMU counters test Sean Christopherson
2024-06-28 22:55 ` [PATCH 0/2] KVM: selftests: Fix PMU counters test flakiness Sean Christopherson
2 siblings, 0 replies; 4+ messages in thread
From: Sean Christopherson @ 2024-06-28 0:55 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini; +Cc: kvm, linux-kernel, Maxim Levitsky
Tweak the macros in the PMU counters test to prepare for moving the
CLFLUSH+MFENCE instructions into the loop body, to fix an issue where
a single CLFUSH doesn't guarantee an LLC miss.
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
.../selftests/kvm/x86_64/pmu_counters_test.c | 28 +++++++++++++------
1 file changed, 19 insertions(+), 9 deletions(-)
diff --git a/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c b/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c
index 96446134c00b..bb40d7c0f83e 100644
--- a/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c
+++ b/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c
@@ -7,15 +7,25 @@
#include "pmu.h"
#include "processor.h"
-/* Number of LOOP instructions for the guest measurement payload. */
-#define NUM_BRANCHES 10
+/* Number of iterations of the loop for the guest measurement payload. */
+#define NUM_LOOPS 10
+
+/* Each iteration of the loop retires one branch instruction. */
+#define NUM_BRANCH_INSNS_RETIRED (NUM_LOOPS)
+
+/* Number of instructions in each loop. */
+#define NUM_INSNS_PER_LOOP 1
+
/*
* Number of "extra" instructions that will be counted, i.e. the number of
- * instructions that are needed to set up the loop and then disabled the
+ * instructions that are needed to set up the loop and then disable the
* counter. 1 CLFLUSH/CLFLUSHOPT/NOP, 1 MFENCE, 2 MOV, 2 XOR, 1 WRMSR.
*/
-#define NUM_EXTRA_INSNS 7
-#define NUM_INSNS_RETIRED (NUM_BRANCHES + NUM_EXTRA_INSNS)
+#define NUM_EXTRA_INSNS 7
+
+/* Total number of instructions retired within the measured section. */
+#define NUM_INSNS_RETIRED (NUM_LOOPS * NUM_INSNS_PER_LOOP + NUM_EXTRA_INSNS)
+
static uint8_t kvm_pmu_version;
static bool kvm_has_perf_caps;
@@ -100,7 +110,7 @@ static void guest_assert_event_count(uint8_t idx,
GUEST_ASSERT_EQ(count, NUM_INSNS_RETIRED);
break;
case INTEL_ARCH_BRANCHES_RETIRED_INDEX:
- GUEST_ASSERT_EQ(count, NUM_BRANCHES);
+ GUEST_ASSERT_EQ(count, NUM_BRANCH_INSNS_RETIRED);
break;
case INTEL_ARCH_LLC_REFERENCES_INDEX:
case INTEL_ARCH_LLC_MISSES_INDEX:
@@ -120,7 +130,7 @@ static void guest_assert_event_count(uint8_t idx,
}
sanity_checks:
- __asm__ __volatile__("loop ." : "+c"((int){NUM_BRANCHES}));
+ __asm__ __volatile__("loop ." : "+c"((int){NUM_LOOPS}));
GUEST_ASSERT_EQ(_rdpmc(pmc), count);
wrmsr(pmc_msr, 0xdead);
@@ -147,7 +157,7 @@ do { \
__asm__ __volatile__("wrmsr\n\t" \
clflush "\n\t" \
"mfence\n\t" \
- "1: mov $" __stringify(NUM_BRANCHES) ", %%ecx\n\t" \
+ "1: mov $" __stringify(NUM_LOOPS) ", %%ecx\n\t" \
FEP "loop .\n\t" \
FEP "mov %%edi, %%ecx\n\t" \
FEP "xor %%eax, %%eax\n\t" \
@@ -500,7 +510,7 @@ static void guest_test_fixed_counters(void)
wrmsr(MSR_CORE_PERF_FIXED_CTR0 + i, 0);
wrmsr(MSR_CORE_PERF_FIXED_CTR_CTRL, FIXED_PMC_CTRL(i, FIXED_PMC_KERNEL));
wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, FIXED_PMC_GLOBAL_CTRL_ENABLE(i));
- __asm__ __volatile__("loop ." : "+c"((int){NUM_BRANCHES}));
+ __asm__ __volatile__("loop ." : "+c"((int){NUM_LOOPS}));
wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, 0);
val = rdmsr(MSR_CORE_PERF_FIXED_CTR0 + i);
--
2.45.2.803.g4e1b14247a-goog
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 2/2] KVM: selftests: Increase robustness of LLC cache misses in PMU counters test
2024-06-28 0:55 [PATCH 0/2] KVM: selftests: Fix PMU counters test flakiness Sean Christopherson
2024-06-28 0:55 ` [PATCH 1/2] KVM: selftests: Rework macros in PMU counters test to prep for multi-insn loop Sean Christopherson
@ 2024-06-28 0:55 ` Sean Christopherson
2024-06-28 22:55 ` [PATCH 0/2] KVM: selftests: Fix PMU counters test flakiness Sean Christopherson
2 siblings, 0 replies; 4+ messages in thread
From: Sean Christopherson @ 2024-06-28 0:55 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini; +Cc: kvm, linux-kernel, Maxim Levitsky
From: Maxim Levitsky <mlevitsk@redhat.com>
Currently the PMU counters test does a single CLFLUSH{,OPT} on the loop's
code, but due to speculative execution this might not cause LLC misses
within the measured section.
Instead of doing a single flush before the loop, do a cache flush on each
iteration of the loop to confuse the prediction and ensure that at least
one cache miss occurs within the measured section.
Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com>
[sean: keep MFENCE, massage changelog]
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
.../selftests/kvm/x86_64/pmu_counters_test.c | 24 +++++++++++--------
1 file changed, 14 insertions(+), 10 deletions(-)
diff --git a/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c b/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c
index bb40d7c0f83e..698cb36989db 100644
--- a/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c
+++ b/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c
@@ -13,15 +13,18 @@
/* Each iteration of the loop retires one branch instruction. */
#define NUM_BRANCH_INSNS_RETIRED (NUM_LOOPS)
-/* Number of instructions in each loop. */
-#define NUM_INSNS_PER_LOOP 1
+/*
+ * Number of instructions in each loop. 1 CLFLUSH/CLFLUSHOPT/NOP, 1 MFENCE,
+ * 1 LOOP.
+ */
+#define NUM_INSNS_PER_LOOP 3
/*
* Number of "extra" instructions that will be counted, i.e. the number of
* instructions that are needed to set up the loop and then disable the
- * counter. 1 CLFLUSH/CLFLUSHOPT/NOP, 1 MFENCE, 2 MOV, 2 XOR, 1 WRMSR.
+ * counter. 2 MOV, 2 XOR, 1 WRMSR.
*/
-#define NUM_EXTRA_INSNS 7
+#define NUM_EXTRA_INSNS 5
/* Total number of instructions retired within the measured section. */
#define NUM_INSNS_RETIRED (NUM_LOOPS * NUM_INSNS_PER_LOOP + NUM_EXTRA_INSNS)
@@ -144,8 +147,8 @@ static void guest_assert_event_count(uint8_t idx,
* before the end of the sequence.
*
* If CLFUSH{,OPT} is supported, flush the cacheline containing (at least) the
- * start of the loop to force LLC references and misses, i.e. to allow testing
- * that those events actually count.
+ * CLFUSH{,OPT} instruction 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
@@ -155,10 +158,11 @@ static void guest_assert_event_count(uint8_t idx,
#define GUEST_MEASURE_EVENT(_msr, _value, clflush, FEP) \
do { \
__asm__ __volatile__("wrmsr\n\t" \
+ " mov $" __stringify(NUM_LOOPS) ", %%ecx\n\t" \
+ "1:\n\t" \
clflush "\n\t" \
"mfence\n\t" \
- "1: mov $" __stringify(NUM_LOOPS) ", %%ecx\n\t" \
- FEP "loop .\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" \
@@ -173,9 +177,9 @@ do { \
wrmsr(pmc_msr, 0); \
\
if (this_cpu_has(X86_FEATURE_CLFLUSHOPT)) \
- GUEST_MEASURE_EVENT(_ctrl_msr, _value, "clflushopt 1f", FEP); \
+ GUEST_MEASURE_EVENT(_ctrl_msr, _value, "clflushopt .", FEP); \
else if (this_cpu_has(X86_FEATURE_CLFLUSH)) \
- GUEST_MEASURE_EVENT(_ctrl_msr, _value, "clflush 1f", FEP); \
+ GUEST_MEASURE_EVENT(_ctrl_msr, _value, "clflush .", FEP); \
else \
GUEST_MEASURE_EVENT(_ctrl_msr, _value, "nop", FEP); \
\
--
2.45.2.803.g4e1b14247a-goog
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH 0/2] KVM: selftests: Fix PMU counters test flakiness
2024-06-28 0:55 [PATCH 0/2] KVM: selftests: Fix PMU counters test flakiness Sean Christopherson
2024-06-28 0:55 ` [PATCH 1/2] KVM: selftests: Rework macros in PMU counters test to prep for multi-insn loop Sean Christopherson
2024-06-28 0:55 ` [PATCH 2/2] KVM: selftests: Increase robustness of LLC cache misses in PMU counters test Sean Christopherson
@ 2024-06-28 22:55 ` Sean Christopherson
2 siblings, 0 replies; 4+ messages in thread
From: Sean Christopherson @ 2024-06-28 22:55 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini; +Cc: kvm, linux-kernel, Maxim Levitsky
On Thu, 27 Jun 2024 17:55:55 -0700, Sean Christopherson wrote:
> v2 of Maxim's patch to fix the flakiness with the PMU counters test do to
> a single CLFLUSH not guaranteeing an LLC miss.
>
> v2:
> - Add a prep patch to tweak the macros in preparation for moving CLFUSH{,OPT}
> into the loop.
> - Keep the MFENCE (because paranoia doesn't hurt in this case).
>
> [...]
Applied to kvm-x86 selftests, thanks!
[1/2] KVM: selftests: Rework macros in PMU counters test to prep for multi-insn loop
https://github.com/kvm-x86/linux/commit/5bb9af07d37c
[2/2] KVM: selftests: Increase robustness of LLC cache misses in PMU counters test
https://github.com/kvm-x86/linux/commit/4669de42aa6c
--
https://github.com/kvm-x86/linux/tree/next
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2024-06-28 22:56 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-28 0:55 [PATCH 0/2] KVM: selftests: Fix PMU counters test flakiness Sean Christopherson
2024-06-28 0:55 ` [PATCH 1/2] KVM: selftests: Rework macros in PMU counters test to prep for multi-insn loop Sean Christopherson
2024-06-28 0:55 ` [PATCH 2/2] KVM: selftests: Increase robustness of LLC cache misses in PMU counters test Sean Christopherson
2024-06-28 22:55 ` [PATCH 0/2] KVM: selftests: Fix PMU counters test flakiness Sean Christopherson
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®