All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ravi Bangoria <ravi.bangoria@amd.com>
To: <like.xu.linux@gmail.com>, <jmattson@google.com>,
	<eranian@google.com>, <peterz@infradead.org>
Cc: <ravi.bangoria@amd.com>, <santosh.shukla@amd.com>,
	<pbonzini@redhat.com>, <seanjc@google.com>,
	<wanpengli@tencent.com>, <vkuznets@redhat.com>, <joro@8bytes.org>,
	<mingo@redhat.com>, <alexander.shishkin@linux.intel.com>,
	<tglx@linutronix.de>, <bp@alien8.de>,
	<dave.hansen@linux.intel.com>, <hpa@zytor.com>,
	<kvm@vger.kernel.org>, <x86@kernel.org>,
	<linux-perf-users@vger.kernel.org>, <ananth.narayan@amd.com>,
	<kim.phillips@amd.com>
Subject: [PATCH v3] perf/amd: Implement erratum #1292 workaround for F19h M00-0Fh
Date: Thu, 3 Feb 2022 15:28:41 +0530	[thread overview]
Message-ID: <20220203095841.7937-1-ravi.bangoria@amd.com> (raw)
In-Reply-To: <fe53507b-9732-b47e-32e0-647a9bfc8a80@amd.com>

Perf counter may overcount for a list of Retire Based Events. Implement
workaround for Zen3 Family 19 Model 00-0F processors as suggested in
Revision Guide[1]:

  To count the non-FP affected PMC events correctly:
    o Use Core::X86::Msr::PERF_CTL2 to count the events, and
    o Program Core::X86::Msr::PERF_CTL2[43] to 1b, and
    o Program Core::X86::Msr::PERF_CTL2[20] to 0b.

Note that the specified workaround applies only to counting events and
not to sampling events. Thus sampling event will continue functioning
as is.

Although the issue exists on all previous Zen revisions, the workaround
is different and thus not included in this patch.

This patch needs Like's patch[2] to make it work on kvm guest.

[1] https://bugzilla.kernel.org/attachment.cgi?id=298241
[2] https://lore.kernel.org/lkml/20220117055703.52020-1-likexu@tencent.com

Signed-off-by: Ravi Bangoria <ravi.bangoria@amd.com>
---
v2: https://lore.kernel.org/r/20220202105158.7072-1-ravi.bangoria@amd.com
v2->v3:
  - Use EVENT_CONSTRAINT_RANGE() for continuous event codes.

 arch/x86/events/amd/core.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/arch/x86/events/amd/core.c b/arch/x86/events/amd/core.c
index 9687a8aef01c..124ec15851bc 100644
--- a/arch/x86/events/amd/core.c
+++ b/arch/x86/events/amd/core.c
@@ -874,6 +874,17 @@ amd_get_event_constraints_f15h(struct cpu_hw_events *cpuc, int idx,
 	}
 }
 
+/* Overcounting of Retire Based Events Erratum */
+static struct event_constraint retire_event_constraints[] __read_mostly = {
+	EVENT_CONSTRAINT_RANGE(0xC0, 0xC5, 0x4, AMD64_EVENTSEL_EVENT),
+	EVENT_CONSTRAINT_RANGE(0xC8, 0xCA, 0x4, AMD64_EVENTSEL_EVENT),
+	EVENT_CONSTRAINT(0xCC, 0x4, AMD64_EVENTSEL_EVENT),
+	EVENT_CONSTRAINT(0xD1, 0x4, AMD64_EVENTSEL_EVENT),
+	EVENT_CONSTRAINT(0x1000000C7, 0x4, AMD64_EVENTSEL_EVENT),
+	EVENT_CONSTRAINT(0x1000000D0, 0x4, AMD64_EVENTSEL_EVENT),
+	EVENT_CONSTRAINT_END
+};
+
 static struct event_constraint pair_constraint;
 
 static struct event_constraint *
@@ -881,10 +892,30 @@ amd_get_event_constraints_f17h(struct cpu_hw_events *cpuc, int idx,
 			       struct perf_event *event)
 {
 	struct hw_perf_event *hwc = &event->hw;
+	struct event_constraint *c;
 
 	if (amd_is_pair_event_code(hwc))
 		return &pair_constraint;
 
+	/*
+	 * Although 'Overcounting of Retire Based Events' erratum exists
+	 * for older generation cpus, workaround to set bit 43 works only
+	 * for Family 19h Model 00-0Fh as per the Revision Guide.
+	 */
+	if (boot_cpu_data.x86 == 0x19 && boot_cpu_data.x86_model <= 0xf) {
+		if (is_sampling_event(event))
+			goto out;
+
+		for_each_event_constraint(c, retire_event_constraints) {
+			if (constraint_match(c, event->hw.config)) {
+				event->hw.config |= (1ULL << 43);
+				event->hw.config &= ~(1ULL << 20);
+				return c;
+			}
+		}
+	}
+
+out:
 	return &unconstrained;
 }
 
-- 
2.27.0


  reply	other threads:[~2022-02-03  9:59 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-17  5:57 [PATCH] KVM: x86/pmu: Clear reserved bit PERF_CTL2[43] for AMD erratum 1292 Like Xu
2022-02-02  4:28 ` [PATCH] perf/amd: Implement errata #1292 workaround for F19h M00-0Fh Ravi Bangoria
2022-02-02  5:27   ` Stephane Eranian
2022-02-02  6:02     ` Ravi Bangoria
2022-02-02  6:16       ` Stephane Eranian
2022-02-02  6:32         ` Ravi Bangoria
2022-02-02 10:51           ` [PATCH v2] perf/amd: Implement erratum " Ravi Bangoria
2022-02-02 14:36             ` Peter Zijlstra
2022-02-02 15:32               ` Ravi Bangoria
2022-02-03  9:58                 ` Ravi Bangoria [this message]
2022-02-09 12:51                   ` [PATCH v3] " Peter Zijlstra
2022-02-10  4:05                     ` Ravi Bangoria
2022-02-10  8:46                       ` Peter Zijlstra
2022-02-03  4:09             ` [PATCH v2] " Jim Mattson
2022-02-03  5:18               ` Ravi Bangoria
2022-02-03 17:55                 ` Jim Mattson
2022-02-04  9:32                   ` Ravi Bangoria
2022-02-04 13:01                     ` Jim Mattson
2022-02-09 10:18                       ` Like Xu
2022-02-09 21:40                         ` Jim Mattson
2022-02-10  4:06                           ` Ravi Bangoria
2022-02-10 13:56                             ` Like Xu

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=20220203095841.7937-1-ravi.bangoria@amd.com \
    --to=ravi.bangoria@amd.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=ananth.narayan@amd.com \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=eranian@google.com \
    --cc=hpa@zytor.com \
    --cc=jmattson@google.com \
    --cc=joro@8bytes.org \
    --cc=kim.phillips@amd.com \
    --cc=kvm@vger.kernel.org \
    --cc=like.xu.linux@gmail.com \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peterz@infradead.org \
    --cc=santosh.shukla@amd.com \
    --cc=seanjc@google.com \
    --cc=tglx@linutronix.de \
    --cc=vkuznets@redhat.com \
    --cc=wanpengli@tencent.com \
    --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.