Kernel KVM virtualization development
 help / color / mirror / Atom feed
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 3/3] x86/pmu: Check emulated instruction/branch count difference on AMD
Date: Thu, 26 Nov 2026 00:48:31 +0000	[thread overview]
Message-ID: <20261126004831.2712453-4-yosry@kernel.org> (raw)
In-Reply-To: <20261126004831.2712453-1-yosry@kernel.org>

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


      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 ` [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 [this message]

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-4-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