* [kvm-unit-tests PATCH 1/3] x86/pmu: Mask emulated instruction count deltas to the counter width
2026-11-26 0:48 [kvm-unit-tests PATCH 0/3] x86/pmu: Restore emulated instruction test coverage on AMD Yosry Ahmed
@ 2026-11-26 0:48 ` Yosry Ahmed
2026-11-26 0:48 ` [kvm-unit-tests PATCH 2/3] x86/pmu: Account for VMRUNs in emulated instruction checks on AMD Yosry Ahmed
2026-11-26 0:48 ` [kvm-unit-tests PATCH 3/3] x86/pmu: Check emulated instruction/branch count difference " Yosry Ahmed
2 siblings, 0 replies; 4+ messages in thread
From: Yosry Ahmed @ 2026-11-26 0:48 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini; +Cc: Jim Mattson, kvm, Yosry Ahmed
check_emulated_instr() presets each counter to -N, N being the expected
number of instructions or branches, and computes the number of counted
events as "count - start", where start is -N as a 64-bit value.
That only works if the counter wraps. The counter is only
gp_counter_width bits wide, so if it counts fewer than N events, it
reads back as a large positive value (2^width - N + delta), and the
64-bit subtraction yields 2^width + delta instead of delta. E.g. a
48-bit counter preset to -22 that counts only 10 events reads back as
0xfffffffffff4, and the difference is 0x100000000000a instead of 10.
The "==" checks still fail in this case, but the ">=" checks, used
without GLOBAL_CTRL and on CPUs with the instructions/branches retired
overcount errata (i.e. all AMD CPUs), pass even though the counter
didn't count the expected number of events.
Mask the differences to the counter width, so that a counter that didn't
count the expected number of events fails the ">=" checks.
Fixes: 20cf91472daa ("x86/pmu: Test PMU virtualization on emulated instructions")
Signed-off-by: Yosry Ahmed <yosry@kernel.org>
---
x86/pmu.c | 13 ++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)
diff --git a/x86/pmu.c b/x86/pmu.c
index a54eb3843621c..db4198c2b64ae 100644
--- a/x86/pmu.c
+++ b/x86/pmu.c
@@ -736,7 +736,7 @@ static void check_running_counter_wrmsr(void)
static void check_emulated_instr(void)
{
u32 eax, edx, ecx;
- uint64_t status, instr_start, brnch_start;
+ uint64_t status, instr_start, brnch_start, instr_delta, brnch_delta;
uint64_t gp_counter_width = (1ull << pmu.gp_counter_width) - 1;
unsigned int branch_idx = pmu.is_intel ?
INTEL_BRANCHES_IDX : AMD_BRANCHES_IDX;
@@ -782,17 +782,20 @@ static void check_emulated_instr(void)
__stop_event(&brnch_cnt);
__stop_event(&instr_cnt);
+ instr_delta = (instr_cnt.count - instr_start) & gp_counter_width;
+ brnch_delta = (brnch_cnt.count - brnch_start) & gp_counter_width;
+
// Check that the end count - start count is at least the expected
// number of instructions and branches.
if (has_perf_global_ctrl && !pmu.errata.instructions_retired_overcount)
- report(instr_cnt.count - instr_start == KVM_FEP_INSNS, "instruction count");
+ report(instr_delta == KVM_FEP_INSNS, "instruction count");
else
- report(instr_cnt.count - instr_start >= KVM_FEP_INSNS, "instruction count");
+ report(instr_delta >= KVM_FEP_INSNS, "instruction count");
if (has_perf_global_ctrl && !pmu.errata.branches_retired_overcount)
- report(brnch_cnt.count - brnch_start == KVM_FEP_BRANCHES, "branch count");
+ report(brnch_delta == KVM_FEP_BRANCHES, "branch count");
else
- report(brnch_cnt.count - brnch_start >= KVM_FEP_BRANCHES, "branch count");
+ report(brnch_delta >= KVM_FEP_BRANCHES, "branch count");
if (this_cpu_has_perf_global_status()) {
// Additionally check that those counters overflowed properly.
--
2.56.1.911.g883d8bcb20-goog
^ permalink raw reply related [flat|nested] 4+ messages in thread* [kvm-unit-tests PATCH 2/3] x86/pmu: Account for VMRUNs in emulated instruction checks on AMD
2026-11-26 0:48 [kvm-unit-tests PATCH 0/3] x86/pmu: Restore emulated instruction test coverage on AMD Yosry Ahmed
2026-11-26 0:48 ` [kvm-unit-tests PATCH 1/3] x86/pmu: Mask emulated instruction count deltas to the counter width Yosry Ahmed
@ 2026-11-26 0:48 ` Yosry Ahmed
2026-11-26 0:48 ` [kvm-unit-tests PATCH 3/3] x86/pmu: Check emulated instruction/branch count difference " Yosry Ahmed
2 siblings, 0 replies; 4+ messages in thread
From: Yosry Ahmed @ 2026-11-26 0:48 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini; +Cc: Jim Mattson, kvm, Yosry Ahmed
AMD CPUs count a successful VMRUN as a retired instruction and as a
retired branch in guest mode, i.e. every VM-Exit taken in the measured
window of check_emulated_instr() adds one to both counts, including the
10 VM-Exits for the 5 forced emulations and the 5 CPUIDs themselves.
E.g. on AMD Turin, the counts are 32 instructions (22 expected) and 15
branches (5 expected) with a mediated PMU, and 33/16 with KVM's
perf-based vPMU, which also intercepts the GLOBAL_CTRL write that
enables the counters. KVM's accounting of the emulated instructions is
exact, the excess is all VMRUNs.
Since commit 7999ff1d28b0 ("x86/pmu: Treat all AMD CPUs as having
instruction/branch overcount errata"), the test only checks that the
counts are at least the expected counts on AMD, which always passes: the
measured window has 12 native instructions and no native branches, so
the 10 VMRUNs alone yield 22 instructions and 10 branches, enough to
pass ">= 22" and ">= 5" and to overflow the counters from the -22/-5
presets, even if KVM didn't account for any of the emulated
instructions.
On AMD, add the 10 synchronous VMRUNs to the expected counts and to the
presets, so that the counters only reach the expected counts, and only
overflow, if KVM retired all of the emulated instructions and branches.
Fixes: 7999ff1d28b0 ("x86/pmu: Treat all AMD CPUs as having instruction/branch overcount errata")
Signed-off-by: Yosry Ahmed <yosry@kernel.org>
---
x86/pmu.c | 33 +++++++++++++++++++++++++++------
1 file changed, 27 insertions(+), 6 deletions(-)
diff --git a/x86/pmu.c b/x86/pmu.c
index db4198c2b64ae..06d4992daf710 100644
--- a/x86/pmu.c
+++ b/x86/pmu.c
@@ -70,6 +70,12 @@ do { \
#define KVM_FEP_INSNS 22
#define KVM_FEP_BRANCHES 5
+/*
+ * The number of VM-Exits, and thus VMRUNs, caused by the kvm_fep_asm() blob:
+ * one for each of the 5 forced emulations and 5 CPUIDs.
+ */
+#define KVM_FEP_VMRUNS 10
+
/*
* KVM_FEP is a magic prefix that forces emulation so
* 'KVM_FEP "jne label\n"' just counts as a single instruction.
@@ -755,6 +761,21 @@ static void check_emulated_instr(void)
.config = EVNTSEL_OS | EVNTSEL_USR | gp_events[instruction_idx].unit_sel,
};
const bool has_perf_global_ctrl = this_cpu_has_perf_global_ctrl();
+ uint64_t instr_exp = KVM_FEP_INSNS, brnch_exp = KVM_FEP_BRANCHES;
+
+ /*
+ * AMD CPUs count a successful VMRUN as a retired instruction and branch
+ * in guest mode, i.e. the VM-Exits for the forced emulations and CPUIDs
+ * add one to both counts each. Account for them in the expected counts
+ * and the presets, so that the counters reach the expected counts, and
+ * overflow, only if KVM retired all of the emulated instructions and
+ * branches. Asynchronous VM-Exits can add more, which the ">=" checks
+ * for the overcount errata tolerate.
+ */
+ if (!pmu.is_intel) {
+ instr_exp += KVM_FEP_VMRUNS;
+ brnch_exp += KVM_FEP_VMRUNS;
+ }
report_prefix_push("emulated instruction");
@@ -764,8 +785,8 @@ static void check_emulated_instr(void)
__start_event(&brnch_cnt, 0);
__start_event(&instr_cnt, 0);
- brnch_start = -KVM_FEP_BRANCHES;
- instr_start = -KVM_FEP_INSNS;
+ brnch_start = -brnch_exp;
+ instr_start = -instr_exp;
wrmsr(MSR_GP_COUNTERx(0), brnch_start & gp_counter_width);
wrmsr(MSR_GP_COUNTERx(1), instr_start & gp_counter_width);
@@ -788,14 +809,14 @@ static void check_emulated_instr(void)
// Check that the end count - start count is at least the expected
// number of instructions and branches.
if (has_perf_global_ctrl && !pmu.errata.instructions_retired_overcount)
- report(instr_delta == KVM_FEP_INSNS, "instruction count");
+ report(instr_delta == instr_exp, "instruction count");
else
- report(instr_delta >= KVM_FEP_INSNS, "instruction count");
+ report(instr_delta >= instr_exp, "instruction count");
if (has_perf_global_ctrl && !pmu.errata.branches_retired_overcount)
- report(brnch_delta == KVM_FEP_BRANCHES, "branch count");
+ report(brnch_delta == brnch_exp, "branch count");
else
- report(brnch_delta >= KVM_FEP_BRANCHES, "branch count");
+ report(brnch_delta >= brnch_exp, "branch count");
if (this_cpu_has_perf_global_status()) {
// Additionally check that those counters overflowed properly.
--
2.56.1.911.g883d8bcb20-goog
^ permalink raw reply related [flat|nested] 4+ messages in thread* [kvm-unit-tests PATCH 3/3] x86/pmu: Check emulated instruction/branch count difference on AMD
2026-11-26 0:48 [kvm-unit-tests PATCH 0/3] x86/pmu: Restore emulated instruction test coverage on AMD Yosry Ahmed
2026-11-26 0:48 ` [kvm-unit-tests PATCH 1/3] x86/pmu: Mask emulated instruction count deltas to the counter width Yosry Ahmed
2026-11-26 0:48 ` [kvm-unit-tests PATCH 2/3] x86/pmu: Account for VMRUNs in emulated instruction checks on AMD Yosry Ahmed
@ 2026-11-26 0:48 ` Yosry Ahmed
2 siblings, 0 replies; 4+ messages in thread
From: Yosry Ahmed @ 2026-11-26 0:48 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini; +Cc: Jim Mattson, kvm, Yosry Ahmed
On AMD, the emulated instruction test can only check that the
instructions and branches counts are at least the expected counts, as
asynchronous VM-Exits, e.g. due to host IRQs, add to both counts (AMD
counts a successful VMRUN as a retired instruction and branch in guest
mode). This doesn't catch KVM overcounting emulated instructions or
branches, e.g. if KVM retired an emulated non-branch instruction twice,
or counted it as a branch.
But as every VMRUN adds one to both counts, the difference between the
counts is precise. When GLOBAL_CTRL is supported, i.e. when the
counters are enabled and disabled by the same WRMSRs, check that the
difference between the counts is the expected one, to catch KVM over- or
undercounting emulated instructions or branches unevenly. It can't
catch KVM overcounting both events by the same amount, but the presets
and the ">=" checks already catch undercounting.
Only do this on AMD: on Intel without the overcount errata, the precise
checks already verify both counts, and with the errata the overcount
isn't guaranteed to be the same for both events (e.g. only instructions
are overcounted on some CPUs).
Signed-off-by: Yosry Ahmed <yosry@kernel.org>
---
x86/pmu.c | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/x86/pmu.c b/x86/pmu.c
index 06d4992daf710..3b35092465f09 100644
--- a/x86/pmu.c
+++ b/x86/pmu.c
@@ -818,6 +818,19 @@ static void check_emulated_instr(void)
else
report(brnch_delta >= brnch_exp, "branch count");
+ /*
+ * On AMD, every VMRUN adds one to both counts, i.e. the difference of
+ * the counts is precise regardless of asynchronous VM-Exits. Check it
+ * to catch KVM over- or undercounting emulated instructions or branches
+ * (unevenly), which the ">=" checks can't. Without GLOBAL_CTRL, the
+ * counters are enabled and disabled at different points, i.e. count
+ * different code. AMD only, as VMRUN is known to add exactly one to
+ * both counts.
+ */
+ if (has_perf_global_ctrl && !pmu.is_intel)
+ report(instr_delta - brnch_delta == instr_exp - brnch_exp,
+ "instruction - branch count");
+
if (this_cpu_has_perf_global_status()) {
// Additionally check that those counters overflowed properly.
status = rdmsr(pmu.msr_global_status);
--
2.56.1.911.g883d8bcb20-goog
^ permalink raw reply related [flat|nested] 4+ messages in thread