Skip to content

Commit 5bb9af0

Browse files
committed
KVM: selftests: Rework macros in PMU counters test to prep for multi-insn loop
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. Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sean Christopherson <[email protected]>
1 parent ea09ace commit 5bb9af0

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,25 @@
77
#include "pmu.h"
88
#include "processor.h"
99

10-
/* Number of LOOP instructions for the guest measurement payload. */
11-
#define NUM_BRANCHES 10
10+
/* Number of iterations of the loop for the guest measurement payload. */
11+
#define NUM_LOOPS 10
12+
13+
/* Each iteration of the loop retires one branch instruction. */
14+
#define NUM_BRANCH_INSNS_RETIRED (NUM_LOOPS)
15+
16+
/* Number of instructions in each loop. */
17+
#define NUM_INSNS_PER_LOOP 1
18+
1219
/*
1320
* Number of "extra" instructions that will be counted, i.e. the number of
14-
* instructions that are needed to set up the loop and then disabled the
21+
* instructions that are needed to set up the loop and then disable the
1522
* counter. 1 CLFLUSH/CLFLUSHOPT/NOP, 1 MFENCE, 2 MOV, 2 XOR, 1 WRMSR.
1623
*/
17-
#define NUM_EXTRA_INSNS 7
18-
#define NUM_INSNS_RETIRED (NUM_BRANCHES + NUM_EXTRA_INSNS)
24+
#define NUM_EXTRA_INSNS 7
25+
26+
/* Total number of instructions retired within the measured section. */
27+
#define NUM_INSNS_RETIRED (NUM_LOOPS * NUM_INSNS_PER_LOOP + NUM_EXTRA_INSNS)
28+
1929

2030
static uint8_t kvm_pmu_version;
2131
static bool kvm_has_perf_caps;
@@ -100,7 +110,7 @@ static void guest_assert_event_count(uint8_t idx,
100110
GUEST_ASSERT_EQ(count, NUM_INSNS_RETIRED);
101111
break;
102112
case INTEL_ARCH_BRANCHES_RETIRED_INDEX:
103-
GUEST_ASSERT_EQ(count, NUM_BRANCHES);
113+
GUEST_ASSERT_EQ(count, NUM_BRANCH_INSNS_RETIRED);
104114
break;
105115
case INTEL_ARCH_LLC_REFERENCES_INDEX:
106116
case INTEL_ARCH_LLC_MISSES_INDEX:
@@ -120,7 +130,7 @@ static void guest_assert_event_count(uint8_t idx,
120130
}
121131

122132
sanity_checks:
123-
__asm__ __volatile__("loop ." : "+c"((int){NUM_BRANCHES}));
133+
__asm__ __volatile__("loop ." : "+c"((int){NUM_LOOPS}));
124134
GUEST_ASSERT_EQ(_rdpmc(pmc), count);
125135

126136
wrmsr(pmc_msr, 0xdead);
@@ -147,7 +157,7 @@ do { \
147157
__asm__ __volatile__("wrmsr\n\t" \
148158
clflush "\n\t" \
149159
"mfence\n\t" \
150-
"1: mov $" __stringify(NUM_BRANCHES) ", %%ecx\n\t" \
160+
"1: mov $" __stringify(NUM_LOOPS) ", %%ecx\n\t" \
151161
FEP "loop .\n\t" \
152162
FEP "mov %%edi, %%ecx\n\t" \
153163
FEP "xor %%eax, %%eax\n\t" \
@@ -500,7 +510,7 @@ static void guest_test_fixed_counters(void)
500510
wrmsr(MSR_CORE_PERF_FIXED_CTR0 + i, 0);
501511
wrmsr(MSR_CORE_PERF_FIXED_CTR_CTRL, FIXED_PMC_CTRL(i, FIXED_PMC_KERNEL));
502512
wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, FIXED_PMC_GLOBAL_CTRL_ENABLE(i));
503-
__asm__ __volatile__("loop ." : "+c"((int){NUM_BRANCHES}));
513+
__asm__ __volatile__("loop ." : "+c"((int){NUM_LOOPS}));
504514
wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, 0);
505515
val = rdmsr(MSR_CORE_PERF_FIXED_CTR0 + i);
506516

0 commit comments

Comments
 (0)