From: Yosry Ahmed <yosry@kernel.org>
To: Sean Christopherson <seanjc@google.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
Jim Mattson <jmattson@google.com>,
kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
Yosry Ahmed <yosry@kernel.org>,
stable@vger.kernel.org
Subject: [PATCH 1/3] KVM: x86/pmu: Recalc PMC emulation when deferring counter reprogramming
Date: Wed, 25 Nov 2026 18:27:01 +0000 [thread overview]
Message-ID: <20261125182703.1694998-2-yosry@kernel.org> (raw)
In-Reply-To: <20261125182703.1694998-1-yosry@kernel.org>
In preparation for always deferring counter reprogramming, recalculate
PMC emulation for each counter first. On AMD, this would make sure that
mode changes (e.g. via VMRUN or toggling EFER.SVME) are immediately
propagated before instruction emulation completes, such that
instructions are counted in the mode they retire in as hardware does.
This gives kvm_pmu_request_counters_reprogram() similar treatment to
kvm_pmu_request_counter_reprogram(), as done in commit 5dfd498bad5f
("KVM: x86/pmu: Calculate set of to-be-emulated PMCs at time of
WRMSRs").
Now that kvm_pmu_request_counter_reprogram() is just a single-counter
version of kvm_pmu_request_counters_reprogram(), just call the latter
from the former.
No functional change intended, as all architectural mode-changing paths
on AMD do not currently defer counter reprogramming.
Cc: stable@vger.kernel.org
Signed-off-by: Yosry Ahmed <yosry@kernel.org>
---
arch/x86/kvm/pmu.h | 26 ++++++++++++++++----------
1 file changed, 16 insertions(+), 10 deletions(-)
diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h
index 090c9bbb74f48..8aa9db1569167 100644
--- a/arch/x86/kvm/pmu.h
+++ b/arch/x86/kvm/pmu.h
@@ -218,26 +218,27 @@ void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops);
void kvm_pmu_recalc_pmc_emulation(struct kvm_pmu *pmu, struct kvm_pmc *pmc);
void kvm_pmu_handle_event(struct kvm_vcpu *vcpu);
-static inline void kvm_pmu_request_counter_reprogram(struct kvm_pmc *pmc)
-{
- kvm_pmu_recalc_pmc_emulation(pmc_to_pmu(pmc), pmc);
-
- set_bit(pmc->idx, pmc_to_pmu(pmc)->reprogram_pmi);
- kvm_make_request(KVM_REQ_PMU, pmc->vcpu);
-}
-
static inline void __kvm_pmu_reprogram_counters(struct kvm_pmu *pmu,
u64 counters,
bool defer)
{
+ DECLARE_BITMAP(bitmap, X86_PMC_IDX_MAX);
+ struct kvm_pmc *pmc;
+ int bit;
+
if (!counters)
return;
atomic64_or(counters, &pmu->__reprogram_pmi);
- if (defer)
+ if (defer) {
+ BUILD_BUG_ON(sizeof(bitmap) != sizeof(counters));
+ bitmap_from_u64(bitmap, counters);
+ kvm_for_each_pmc(pmu, pmc, bit, bitmap)
+ kvm_pmu_recalc_pmc_emulation(pmu, pmc);
kvm_make_request(KVM_REQ_PMU, pmu_to_vcpu(pmu));
- else
+ } else {
kvm_pmu_handle_event(pmu_to_vcpu(pmu));
+ }
}
static inline void kvm_pmu_request_counters_reprogram(struct kvm_pmu *pmu,
@@ -246,6 +247,11 @@ static inline void kvm_pmu_request_counters_reprogram(struct kvm_pmu *pmu,
__kvm_pmu_reprogram_counters(pmu, counters, true);
}
+static inline void kvm_pmu_request_counter_reprogram(struct kvm_pmc *pmc)
+{
+ kvm_pmu_request_counters_reprogram(pmc_to_pmu(pmc), BIT_ULL(pmc->idx));
+}
+
/*
* Check if a PMC is enabled by comparing it against global_ctrl bits.
*
--
2.56.1.839.g22a8290db9-goog
next prev parent reply other threads:[~2026-11-25 18:27 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 18:27 [PATCH 0/3] KVM: x86/pmu: Always defer PMU reprogramming on nested transitions Yosry Ahmed
2026-11-25 18:27 ` Yosry Ahmed [this message]
2026-11-25 18:27 ` [PATCH 2/3] KVM: x86/pmu: Always defer PMU reprogramming on SVM " Yosry Ahmed
2026-11-25 18:27 ` [PATCH 3/3] KVM: selftests: Set EFER.SVME via KVM_SET_SREGS in svm_pmu_host_guest_test 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=20261125182703.1694998-2-yosry@kernel.org \
--to=yosry@kernel.org \
--cc=jmattson@google.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
--cc=stable@vger.kernel.org \
/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
all inboxes | Powered by JetHome®