From: Sean Christopherson <seanjc@google.com>
To: Yosry Ahmed <yosry@kernel.org>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
Jim Mattson <jmattson@google.com>,
Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Namhyung Kim <namhyung@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
kvm@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v5 07/13] KVM: x86/pmu: Disable counters based on Host-Only/Guest-Only bits in SVM
Date: Tue, 5 May 2026 11:11:02 -0700 [thread overview]
Message-ID: <afoytsu6LzzpeTfc@google.com> (raw)
In-Reply-To: <afTnlv_cAWFfouqk@google.com>
On Fri, May 01, 2026, Yosry Ahmed wrote:
> On Thu, Apr 30, 2026 at 08:34:59PM -0700, Yosry Ahmed wrote:
> > On Thu, Apr 30, 2026 at 4:24 PM Yosry Ahmed <yosry@kernel.org> wrote:
> > > > +static void amd_mediated_pmu_handle_host_guest_bits(struct kvm_vcpu *vcpu,
> > > > + struct kvm_pmc *pmc)
> > > > +{
> > > > + u64 host_guest_bits;
> > > > +
> > > > + if (!(pmc->eventsel & ARCH_PERFMON_EVENTSEL_ENABLE))
> > > > + return;
> > > > +
> > > > + /* Count all events if both bits are cleared */
> > > > + host_guest_bits = pmc->eventsel & AMD64_EVENTSEL_HOST_GUEST_MASK;
> > > > + if (!host_guest_bits)
> > > > + return;
> > > > +
> > > > + /*
> > > > + * If EFER.SVME is set, the counter is disabledd if only one of the bits
> > > > + * is set and it doesn't match the vCPU context. If EFER.SVME is
> > > > + * cleared, the counter is disable if any of the bits is set.
> > > > + */
> > > > + if (vcpu->arch.efer & EFER_SVME) {
> > > > + if (host_guest_bits == AMD64_EVENTSEL_HOST_GUEST_MASK)
> > > > + return;
> > > > +
> > > > + if (!!(host_guest_bits & AMD64_EVENTSEL_GUESTONLY) == is_guest_mode(vcpu))
> > > > + return;
> > > > + }
> > > > +
> > > > + pmc->eventsel_hw &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
> > >
> > > Sashiko raised a good point here. In the following patch, we reprogram
> > > the counters synchronously on nested transitions to update the
> > > counters' enablement before counting VMRUN or WRMSR(EFER.SVME).
> > > However, this updates pmc->eventsel_hw while
> > > kvm_pmu_recalc_pmc_emulation() checks pmc->eventsel to check if the
> > > counter is enabled.
> > >
> > > I think either pmc_is_locally_enabled() needs to check
> > > pmc->eventsel_hw or we need to update pmc->eventsel here.
Hmm. I don't think either of those is the correct approach. Unlike the MSR filter
case, the H/G stuff is architectural. I.e. KVM doesn't just need to disable the
counter in hardware, KVM _always_ needs to treat the counter as disabled.
So I think we actually want to handle this in pmc_is_locally_enabled(), because
the host/guest bits are "local" controls. One option would be to add the guest/host
masks as constants in kvm_pmu_ops, and bleed the logic into pmc_is_locally_enabled(),
e.g. to avoid the CALL+RET overhead. But if make the callback a "negative", then
we can make the static call OPTIONAL_RET0, which will turn the call into a glorified
nop for everything except AMD with a mediated PMU. E.g.
diff --git arch/x86/kvm/pmu.h arch/x86/kvm/pmu.h
index 0925246731cb..d8ce0938fcbe 100644
--- arch/x86/kvm/pmu.h
+++ arch/x86/kvm/pmu.h
@@ -190,7 +190,8 @@ static inline bool pmc_is_locally_enabled(struct kvm_pmc *pmc)
pmc->idx - KVM_FIXED_PMC_BASE_IDX) &
(INTEL_FIXED_0_KERNEL | INTEL_FIXED_0_USER);
- return pmc->eventsel & ARCH_PERFMON_EVENTSEL_ENABLE;
+ return (pmc->eventsel & ARCH_PERFMON_EVENTSEL_ENABLE) &&
+ !kvm_pmu_call(pmc_is_locally_disabled(pmc));
}
extern struct x86_pmu_capability kvm_pmu_cap;
next prev parent reply other threads:[~2026-05-05 18:11 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-30 20:27 [PATCH v5 00/13] KVM: x86/pmu: Add support for AMD Host-Only/Guest-Only bits Yosry Ahmed
2026-04-30 20:27 ` [PATCH v5 01/13] KVM: nSVM: Stop leaking single-stepping on VMRUN into L2 Yosry Ahmed
2026-04-30 20:27 ` [PATCH v5 02/13] KVM: nSVM: Bail early out of VMRUN emulation if advancing RIP fails Yosry Ahmed
2026-04-30 20:27 ` [PATCH v5 03/13] KVM: nSVM: Move VMRUN instruction retirement after entering guest mode Yosry Ahmed
2026-04-30 20:27 ` [PATCH v5 04/13] KVM: x86: Move enable_pmu/enable_mediated_pmu to pmu.h and pmu.c Yosry Ahmed
2026-04-30 20:27 ` [PATCH v5 05/13] KVM: x86/pmu: Rename reprogram_counters() to clarify usage Yosry Ahmed
2026-04-30 20:27 ` [PATCH v5 06/13] KVM: x86/pmu: Do a single atomic OR when reprogramming counters Yosry Ahmed
2026-04-30 20:27 ` [PATCH v5 07/13] KVM: x86/pmu: Disable counters based on Host-Only/Guest-Only bits in SVM Yosry Ahmed
2026-04-30 23:24 ` Yosry Ahmed
2026-05-01 3:34 ` Yosry Ahmed
2026-05-01 17:50 ` Yosry Ahmed
2026-05-05 18:11 ` Sean Christopherson [this message]
2026-05-05 18:23 ` Yosry Ahmed
2026-05-05 18:49 ` Sean Christopherson
2026-05-05 19:32 ` Yosry Ahmed
2026-05-05 19:58 ` Sean Christopherson
2026-05-05 20:24 ` Yosry Ahmed
2026-04-30 20:27 ` [PATCH v5 08/13] KVM: x86/pmu: Reprogram Host/Guest-Only counters on nested transitions Yosry Ahmed
2026-04-30 20:27 ` [PATCH v5 09/13] KVM: x86/pmu: Allow Host-Only/Guest-Only bits with nSVM and mediated PMU Yosry Ahmed
2026-04-30 20:27 ` [PATCH v5 10/13] KVM: selftests: Refactor allocating guest stack into a helper Yosry Ahmed
2026-04-30 20:27 ` [PATCH v5 11/13] KVM: selftests: Allocate a dedicated guest page for x86 L2 guest stack Yosry Ahmed
2026-04-30 20:27 ` [PATCH v5 12/13] KVM: selftests: Drop L1-provided stacks for L2 guests on x86 Yosry Ahmed
2026-04-30 20:27 ` [PATCH v5 13/13] KVM: selftests: Add svm_pmu_host_guest_test for Host-Only/Guest-Only bits Yosry Ahmed
2026-04-30 20:38 ` [PATCH v5 00/13] KVM: x86/pmu: Add support for 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=afoytsu6LzzpeTfc@google.com \
--to=seanjc@google.com \
--cc=acme@kernel.org \
--cc=alexander.shishkin@linux.intel.com \
--cc=jmattson@google.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=pbonzini@redhat.com \
--cc=peterz@infradead.org \
--cc=yosry@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.