mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Luka Absandze <absandze@amazon.de>
To: <peterz@infradead.org>, <mingo@redhat.com>, <acme@kernel.org>,
	<namhyung@kernel.org>, <tglx@kernel.org>, <bp@alien8.de>,
	<dave.hansen@linux.intel.com>, <x86@kernel.org>, <yaoz@meta.com>,
	<dwmw2@infradead.org>
Cc: <kim.phillips@amd.com>, <kvm@vger.kernel.org>,
	<linux-perf-users@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>,
	"Luka Absandze" <absandze@amazon.de>
Subject: [PATCH] perf/x86/amd: Reject AMD Merge event as a countable event
Date: Wed, 16 Sep 2026 12:33:15 +0000	[thread overview]
Message-ID: <20260916123315.89042-1-absandze@amazon.de> (raw)

AMD event select 0xFFF (Merge) is the hardware control encoding used by
Large Increment per Cycle (LIPC) pairs.  When written into an odd
counter's PerfCtl it turns that counter into the upper 16 bits of a
64-bit merged accumulator; any subsequent arming write (-(2^47-1)) is
silently truncated to its low 16 bits, while hwc->prev_count retains the
full value.

This is particularly problematic with a KVM guest, which will program
the Merge encoding into its own odd vPMC as a normal part of scheduling
a LIPC event.  KVM intercepts that write and faithfully forwards it to
the host as a raw event carrying the Merge encoding, where it is
accepted as an ordinary countable event. Once a counter's PerfCtl holds
the Merge encoding, every later user of that counter gets a first delta
inflated by 2^47

Reject Merge (AMD_MERGE_EVENT) in amd_core_hw_config() so it cannot be
submitted as a user-visible event.  The pair mechanism programs it
itself via x86_pmu.perf_ctr_pair_en; nothing else should do so.

Fixes: 5738891229a2 ("perf/x86/amd: Add support for Large Increment per Cycle Events")
Reported-by: Yao Zhou <yaoz@meta.com>
Signed-off-by: Luka Absandze <absandze@amazon.de>
---
 arch/x86/events/amd/core.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/arch/x86/events/amd/core.c b/arch/x86/events/amd/core.c
index 49b6b8fce566..cb655c751e5b 100644
--- a/arch/x86/events/amd/core.c
+++ b/arch/x86/events/amd/core.c
@@ -380,8 +380,12 @@ static int amd_core_hw_config(struct perf_event *event)
 	else if (event->attr.exclude_guest)
 		event->hw.config |= AMD64_EVENTSEL_HOSTONLY;
 
-	if ((x86_pmu.flags & PMU_FL_PAIR) && amd_is_pair_event_code(&event->hw))
-		event->hw.flags |= PERF_X86_EVENT_PAIR;
+	if (x86_pmu.flags & PMU_FL_PAIR) {
+		if ((event->hw.config & AMD64_EVENTSEL_EVENT) == AMD_MERGE_EVENT)
+			return -EINVAL;
+		if (amd_is_pair_event_code(&event->hw))
+			event->hw.flags |= PERF_X86_EVENT_PAIR;
+	}
 
 	if (has_branch_stack(event))
 		return static_call(amd_pmu_branch_hw_config)(event);
-- 
2.47.3


             reply	other threads:[~2026-09-16 12:34 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-16 12:33 Luka Absandze [this message]
2026-09-16 15:04 ` Borislav Petkov

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=20260916123315.89042-1-absandze@amazon.de \
    --to=absandze@amazon.de \
    --cc=acme@kernel.org \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=dwmw2@infradead.org \
    --cc=kim.phillips@amd.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=tglx@kernel.org \
    --cc=x86@kernel.org \
    --cc=yaoz@meta.com \
    /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®