Linux Perf Users
 help / color / mirror / Atom feed
From: Vasant Hegde <vasant.hegde@amd.com>
To: <linux-perf-users@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>, <peterz@infradead.org>,
	<mingo@redhat.com>, <acme@kernel.org>, <namhyung@kernel.org>,
	<mark.rutland@arm.com>, <alexander.shishkin@linux.intel.com>,
	<jolsa@kernel.org>, <irogers@google.com>,
	<adrian.hunter@intel.com>, <james.clark@linaro.org>
Cc: <x86@kernel.org>, <dave.hansen@linux.intel.com>,
	<tglx@kernel.org>, <bp@alien8.de>, <hpa@zytor.com>,
	<iommu@lists.linux.dev>, <joro@8bytes.org>,
	<suravee.suthikulpanit@amd.com>, <sandipan.das@amd.com>,
	<ctshao@google.com>, Vasant Hegde <vasant.hegde@amd.com>,
	Jia-yi Chen <jychen@google.com>
Subject: [PATCH] x86/events/amd/iommu: Count IOMMU events by counter delta
Date: Thu, 3 Dec 2026 10:57:25 +0000	[thread overview]
Message-ID: <20261203105725.315334-1-vasant.hegde@amd.com> (raw)

perf_iommu_read() adds the raw 48-bit counter into event->count. The
register is not cleared on read, and it starts at zero only when the
event is started, so a later read adds those counts again. perf stat -I
reads the event more than once.

Remember the last value in hw.prev_count and add only the delta,
including a 48-bit wrap. On PERF_EF_RELOAD, prev_count is set to the
zero written to the counter. Otherwise it is read from the counter at
start.

Fixes: e10de314287c ("x86/events/amd/iommu: Fix invalid Perf result due to IOMMU PMC power-gating")
Cc: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
Reported-by: Jia-yi Chen <jychen@google.com>
Tested-by: Jia-yi Chen <jychen@google.com>
Signed-off-by: Vasant Hegde <vasant.hegde@amd.com>
---
 arch/x86/events/amd/iommu.c | 33 +++++++++++++++++++--------------
 1 file changed, 19 insertions(+), 14 deletions(-)

diff --git a/arch/x86/events/amd/iommu.c b/arch/x86/events/amd/iommu.c
index b4ed454a0636..2c9d8e8713c5 100644
--- a/arch/x86/events/amd/iommu.c
+++ b/arch/x86/events/amd/iommu.c
@@ -317,6 +317,8 @@ static void perf_iommu_disable_event(struct perf_event *event)
 static void perf_iommu_start(struct perf_event *event, int flags)
 {
 	struct hw_perf_event *hwc = &event->hw;
+	struct amd_iommu *iommu = perf_event_2_iommu(event);
+	u64 count = 0;
 
 	if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED)))
 		return;
@@ -332,38 +334,41 @@ static void perf_iommu_start(struct perf_event *event, int flags)
 	perf_iommu_enable_event(event);
 
 	if (flags & PERF_EF_RELOAD) {
-		u64 count = 0;
-		struct amd_iommu *iommu = perf_event_2_iommu(event);
-
 		/*
-		 * Since the IOMMU PMU only support counting mode,
-		 * the counter always start with value zero.
+		 * Counting mode only. Start from zero so the following
+		 * reads add only what this event collects.
 		 */
 		amd_iommu_pc_set_reg(iommu, hwc->iommu_bank, hwc->iommu_cntr,
 				     IOMMU_PC_COUNTER_REG, &count);
+	} else if (amd_iommu_pc_get_reg(iommu, hwc->iommu_bank, hwc->iommu_cntr,
+					IOMMU_PC_COUNTER_REG, &count)) {
+		count = 0;
 	}
 
+	local64_set(&hwc->prev_count, count & GENMASK_ULL(47, 0));
+
 	perf_event_update_userpage(event);
 }
 
 static void perf_iommu_read(struct perf_event *event)
 {
-	u64 count;
 	struct hw_perf_event *hwc = &event->hw;
 	struct amd_iommu *iommu = perf_event_2_iommu(event);
+	u64 prev, new, delta;
 
+again:
+	prev = local64_read(&hwc->prev_count);
 	if (amd_iommu_pc_get_reg(iommu, hwc->iommu_bank, hwc->iommu_cntr,
-				 IOMMU_PC_COUNTER_REG, &count))
+				 IOMMU_PC_COUNTER_REG, &new))
 		return;
 
-	/* IOMMU pc counter register is only 48 bits */
-	count &= GENMASK_ULL(47, 0);
+	/* The counter is 48 bits and wraps. */
+	new &= GENMASK_ULL(47, 0);
+	if (local64_cmpxchg(&hwc->prev_count, prev, new) != prev)
+		goto again;
 
-	/*
-	 * Since the counter always start with value zero,
-	 * simply just accumulate the count for the event.
-	 */
-	local64_add(count, &event->count);
+	delta = (new - prev) & GENMASK_ULL(47, 0);
+	local64_add(delta, &event->count);
 }
 
 static void perf_iommu_stop(struct perf_event *event, int flags)
-- 
2.31.1


             reply	other threads:[~2026-12-03 10:58 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 10:57 Vasant Hegde [this message]
2026-12-03 11:20 ` [PATCH] x86/events/amd/iommu: Count IOMMU events by counter delta sashiko-bot

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=20261203105725.315334-1-vasant.hegde@amd.com \
    --to=vasant.hegde@amd.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=bp@alien8.de \
    --cc=ctshao@google.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=iommu@lists.linux.dev \
    --cc=irogers@google.com \
    --cc=james.clark@linaro.org \
    --cc=jolsa@kernel.org \
    --cc=joro@8bytes.org \
    --cc=jychen@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=sandipan.das@amd.com \
    --cc=suravee.suthikulpanit@amd.com \
    --cc=tglx@kernel.org \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox