From: Sean Christopherson <seanjc@google.com>
To: Jim Mattson <jmattson@google.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
Dave Hansen <dave.hansen@linux.intel.com>,
x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
Peter Zijlstra <peterz@infradead.org>,
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>,
Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
Adrian Hunter <adrian.hunter@intel.com>,
James Clark <james.clark@linaro.org>,
Shuah Khan <shuah@kernel.org>,
kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-perf-users@vger.kernel.org,
linux-kselftest@vger.kernel.org
Subject: Re: [PATCH 2/6] KVM: x86/pmu: Disable HG_ONLY events as appropriate for current vCPU state
Date: Thu, 22 Jan 2026 15:51:43 -0800 [thread overview]
Message-ID: <aXK4DzweZLy9O8n1@google.com> (raw)
In-Reply-To: <CALMp9eRGj3rNeNN82H12f=XO5iXi5s2ri2K71CnjoVp9eKzz1w@mail.gmail.com>
On Thu, Jan 22, 2026, Jim Mattson wrote:
> On Thu, Jan 22, 2026 at 8:33 AM Sean Christopherson <seanjc@google.com> wrote:
> > On Wed, Jan 21, 2026, Jim Mattson wrote:
> > > Introduce amd_pmu_dormant_hg_event(), which determines whether an AMD PMC
> > > should be dormant (i.e. not count) based on the guest's Host-Only and
> > > Guest-Only event selector bits and the current vCPU state.
> > >
> > > Update amd_pmu_set_eventsel_hw() to clear the event selector's enable bit
> > > when the event is dormant.
> > >
> > > Signed-off-by: Jim Mattson <jmattson@google.com>
> > > ---
> > > arch/x86/include/asm/perf_event.h | 2 ++
> > > arch/x86/kvm/svm/pmu.c | 23 +++++++++++++++++++++++
> > > 2 files changed, 25 insertions(+)
> > >
> > > diff --git a/arch/x86/include/asm/perf_event.h b/arch/x86/include/asm/perf_event.h
> > > index 0d9af4135e0a..7649d79d91a6 100644
> > > --- a/arch/x86/include/asm/perf_event.h
> > > +++ b/arch/x86/include/asm/perf_event.h
> > > @@ -58,6 +58,8 @@
> > > #define AMD64_EVENTSEL_INT_CORE_ENABLE (1ULL << 36)
> > > #define AMD64_EVENTSEL_GUESTONLY (1ULL << 40)
> > > #define AMD64_EVENTSEL_HOSTONLY (1ULL << 41)
> > > +#define AMD64_EVENTSEL_HG_ONLY \
> >
> > I would strongly prefer to avoid the HG acronym, as it's not immediately obvious
> > that it's HOST_GUEST, and avoiding long lines even with the full HOST_GUEST is
> > pretty easy.
>
> In this instance, I'm happy to make the suggested change, but I think
> your overall distaste for HG_ONLY is unwarranted.
> These bits are documented in the APM as:
>
> > HG_ONLY (Host/Guest Only)—Bits 41:40, read/write
Ugh, stupid APM. That makes me hate it a little less, but still, ugh.
> > Maybe amd_pmc_is_active() or amd_pmc_counts_in_current_mode()?
>
> I think amd_pmc_is_active() is a much stronger statement, implying
> that both enable bits are also set.
Ooh, good point.
> Similarly, amd_pmc_counts_in_current_mode() sounds like it looks at
> OS/USR bits as well.
Yeah, I didn't like that collision either. :-/
> I'll see if I can think of a better name that isn't misleading. I
> actually went with this polarity because of the naming problem. But, I
> agree that the reverse polarity is marginally better.
>
> > > +{
> > > + u64 hg_only = pmc->eventsel & AMD64_EVENTSEL_HG_ONLY;
> > > + struct kvm_vcpu *vcpu = pmc->vcpu;
> > > +
> > > + if (hg_only == 0)
> >
> > !hg_only
>
> Now, you're just being petty. But, okay.
Eh, that's a very standard kernel style thing.
> > In the spirit of avoiding the "hg" acronym, what if we do something like this?
> >
> > const u64 HOST_GUEST_MASK = AMD64_EVENTSEL_HOST_GUEST_MASK;
>
> Ugh. No. You can't both prefer the longer name and yet avoid it like
> the plague. If you need to introduce a shorter alias, the longer name
> is a bad choice.
IMO, there's a big difference between a global macro that may be read in a variety
of contexts, and a variable that's scoped to a function and consumed within a few
lines of its definition.
That said, I'm definitely open to other ways to write this code that don't require
a local const, it's HG_ONLY that I really dislike.
> > struct kvm_vcpu *vcpu = pmc->vcpu;
> > u64 eventsel = pmc->eventsel;
> >
> > /*
> > * PMCs count in both host and guest if neither {HOST,GUEST}_ONLY flags
> > * are set, or if both flags are set.
> > */
> > if (!(eventsel & HOST_GUEST_MASK) ||
> > ((eventsel & HOST_GUEST_MASK) == HOST_GUEST_MASK))
> > return true;
> >
> > /* {HOST,GUEST}_ONLY bits are ignored when SVME is clear. */
> > if (!(vcpu->arch.efer & EFER_SVME))
> > return true;
> >
> > return !!(eventsel & AMD64_EVENTSEL_GUESTONLY) == is_guest_mode(vcpu);
> >
> > > + /* Not an HG_ONLY event */
> >
> > Please don't put comments inside single-line if-statements. 99% of the time
> > it's easy to put the comment outside of the if-statement, and doing so encourages
> > a more verbose comment and avoids a "does this if-statement need curly-braces"
> > debate.
>
> There is no debate. A comment is not a statement. But, okay.
LOL, dollars to donuts says I can find someone to debate you on the "correct"
style. :-D
next prev parent reply other threads:[~2026-01-22 23:51 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-21 22:53 [PATCH 0/6] KVM: x86/pmu: Add support for AMD HG_ONLY bits Jim Mattson
2026-01-21 22:53 ` [PATCH 1/6] KVM: x86/pmu: Introduce amd_pmu_set_eventsel_hw() Jim Mattson
2026-01-22 16:04 ` Sean Christopherson
2026-01-22 21:57 ` Jim Mattson
2026-01-21 22:54 ` [PATCH 2/6] KVM: x86/pmu: Disable HG_ONLY events as appropriate for current vCPU state Jim Mattson
2026-01-22 16:33 ` Sean Christopherson
2026-01-22 22:47 ` Jim Mattson
2026-01-22 23:51 ` Sean Christopherson [this message]
2026-01-21 22:54 ` [PATCH 3/6] KVM: x86/pmu: Track enabled AMD PMCs with Host-Only xor Guest-Only bits set Jim Mattson
2026-01-22 16:49 ` Sean Christopherson
2026-01-24 1:09 ` Jim Mattson
2026-01-21 22:54 ` [PATCH 4/6] KVM: x86/pmu: [De]activate HG_ONLY PMCs at SVME changes and nested transitions Jim Mattson
2026-01-22 16:55 ` Sean Christopherson
2026-01-28 23:43 ` Jim Mattson
2026-01-29 22:34 ` Sean Christopherson
2026-01-21 22:54 ` [PATCH 5/6] KVM: x86/pmu: Allow HG_ONLY bits with nSVM and mediated PMU Jim Mattson
2026-01-22 16:56 ` Sean Christopherson
2026-01-21 22:54 ` [PATCH 6/6] KVM: selftests: x86: Add svm_pmu_hg_test for HG_ONLY bits Jim Mattson
2026-01-22 17:12 ` Sean Christopherson
2026-01-28 23:47 ` Jim Mattson
2026-01-22 18:56 ` kernel test robot
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=aXK4DzweZLy9O8n1@google.com \
--to=seanjc@google.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=hpa@zytor.com \
--cc=irogers@google.com \
--cc=james.clark@linaro.org \
--cc=jmattson@google.com \
--cc=jolsa@kernel.org \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-perf-users@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=shuah@kernel.org \
--cc=tglx@linutronix.de \
--cc=x86@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.