From: Yosry Ahmed <yosry@kernel.org>
To: Sean Christopherson <seanjc@google.com>,
Paolo Bonzini <pbonzini@redhat.com>
Cc: Jim Mattson <jmattson@google.com>,
kvm@vger.kernel.org, Yosry Ahmed <yosry@kernel.org>
Subject: [kvm-unit-tests PATCH 2/3] x86/pmu: Account for VMRUNs in emulated instruction checks on AMD
Date: Thu, 26 Nov 2026 00:48:30 +0000 [thread overview]
Message-ID: <20261126004831.2712453-3-yosry@kernel.org> (raw)
In-Reply-To: <20261126004831.2712453-1-yosry@kernel.org>
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
next prev parent reply other threads:[~2026-11-26 0:48 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
2026-11-26 0:48 ` [kvm-unit-tests PATCH 3/3] x86/pmu: Check emulated instruction/branch count difference on AMD Yosry Ahmed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261126004831.2712453-3-yosry@kernel.org \
--to=yosry@kernel.org \
--cc=jmattson@google.com \
--cc=kvm@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox