Re: [PATCH v4 3/6] KVM: x86/pmu: Disable counters based on Host-Only/Guest-Only bits in SVM
From: Sean Christopherson
Date: Mon Apr 27 2026 - 14:50:13 EST
On Fri, Apr 24, 2026, Yosry Ahmed wrote:
> On Mon, Apr 06, 2026 at 06:30:05PM -0700, Sean Christopherson wrote:
> > I would rather make a single call from kvm_pmu_handle_event(), and let the vendor
> > deal with mediated vs. legacy. I want to avoid mediated-specific ops as much as
> > possible, and I think kvm_x86_ops.reprogram_counters() would be easier to
> > understand overall.
>
> I think this doesn't apply anymore now that most nested transitions
> won't be handled through kvm_pmu_handle_event(). Also because we need
> kvm_mediated_pmu_refresh_event_filter() to still be called before
> re-evaluating H/G bits and EFER.SVME.
>
> I think leave this callback as-is and handle everything through
> reprogram_counter(). Export reprogram_counter() and rename it to
> kvm_pmu_reprogram_counter(), and end up with something like this:
>
> void __kvm_pmu_handle_nested_transition(struct kvm_vcpu *vcpu, bool defer)
> {
> struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
> DECLARE_BITMAP(bitmap, X86_PMC_IDX_MAX);
>
> if (bitmap_empty(pmu->reprogram_on_nested_transition, X86_PMC_IDX_MAX))
> return;
>
> bitmap_copy(bitmap, pmu->reprogram_pmi, X86_PMC_IDX_MAX);
> bitmap_zero(pmu->reprogram_on_nested_transition, X86_PMC_IDX_MAX);
>
> BUILD_BUG_ON(sizeof(pmu->reprogram_on_nested_transition) != sizeof(atomic64_t));
> if (defer) {
> atomic64_or(*(s64 *)pmu->reprogram_on_nested_transition,
> &vcpu_to_pmu(vcpu)->__reprogram_pmi);
> kvm_make_request(KVM_REQ_PMU, vcpu);
> return;
> }
>
> kvm_for_each_pmc(pmu, pmc, bit, bitmap)
> kvm_pmu_reprogram_counter(pmc);
> }
>
> void kvm_pmu_handle_nested_transition(struct kvm_vcpu *vcpu)
> {
> __kvm_pmu_handle_nested_transition(vcpu, false);
> }
>
> Actually, if that's desired, we can move this logic into SVM code now.
> We won't be calling kvm_pmu_handle_nested_transition() from inside
> enter_guest_mode() and leave_guest_mode() anyway so that we can only
> defer for the svm_leave_nested() path.
>
> So we can move:
> - kvm_pmu_handle_nested_transition() to
> svm_pmu_handle_nested_transition()
> - pmu->reprogram_on_nested_transition to
> svm->nested.reprogram_on_nested_transition
>
> Not sure if we want to keep SVM-specific logic in SVM code, or if we
> want to keep code generic as much as possible. I can see good arguments
> for both stances.
We can have our cake and eat it too. Add svm_pmu_handle_nested_transition(),
but then also rename and rework reprogram_counters() to support both deferred and
synchronous operation, e.g. something like so:
---
static inline void __kvm_pmu_reprogram_counters(struct kvm_pmu *pmu, u64 diff,
bool defer)
{
struct kvm_vcpu *vcpu = pmu_to_vcpu(pmu);
lockdep_assert_once(defer || kvm_get_running_vcpu() == vcpu);
if (!diff)
return;
atomic64_or(diff, &pmu->__reprogram_pmi);
if (defer)
kvm_make_request(KVM_REQ_PMU, vcpu);
else
kvm_pmu_handle_event(pmu_to_vcpu(pmu));
}
static inline void kvm_pmu_reprogram_counters(struct kvm_pmu *pmu, u64 diff)
{
__kvm_pmu_reprogram_counters(pmu, diff, true);
}
---
and then have SVM code pass in the reprogram_on_nested_transition or whatever.