Linux Perf Users
 help / color / mirror / Atom feed
* [PATCH] x86/events/amd/iommu: Count IOMMU events by counter delta
@ 2026-12-03 10:57 Vasant Hegde
  2026-12-03 11:20 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Vasant Hegde @ 2026-12-03 10:57 UTC (permalink / raw)
  To: linux-perf-users, linux-kernel, peterz, mingo, acme, namhyung,
	mark.rutland, alexander.shishkin, jolsa, irogers, adrian.hunter,
	james.clark
  Cc: x86, dave.hansen, tglx, bp, hpa, iommu, joro,
	suravee.suthikulpanit, sandipan.das, ctshao, Vasant Hegde,
	Jia-yi Chen

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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] x86/events/amd/iommu: Count IOMMU events by counter delta
  2026-12-03 10:57 [PATCH] x86/events/amd/iommu: Count IOMMU events by counter delta Vasant Hegde
@ 2026-12-03 11:20 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-03 11:20 UTC (permalink / raw)
  To: Vasant Hegde, linux-perf-users

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] x86/events/amd/iommu: NMI perf_iommu_read() race corrupts
  event->count
- [Medium] x86/events/amd/iommu: perf_iommu_read() reads stopped
  power-gated counter
--

commit 011ccb2680cbd25efd4676fcebb8ce75bb56795a
Author: Vasant Hegde <vasant.hegde@amd.com>

x86/events/amd/iommu: Count IOMMU events by counter delta

This patch tracks the last observed AMD IOMMU counter value in
hw.prev_count and accumulates the 48-bit wraparound delta in
perf_iommu_read(), rather than adding the raw counter value on every read.

> diff --git a/arch/x86/events/amd/iommu.c b/arch/x86/events/amd/iommu.c
> index f332c7089bd59..5235abeea75f5 100644
> --- a/arch/x86/events/amd/iommu.c
> +++ b/arch/x86/events/amd/iommu.c

[ ... ]

> @@ -294,38 +296,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);
>  }

[Severity: Medium]
Can an NMI calling perf_iommu_read() between zeroing the hardware counter
and updating hwc->prev_count corrupt event->count?

event_sched_in() publishes event->oncpu before calling perf_iommu_add() ->
perf_iommu_start().  When a multiplexed event is rescheduled,
hwc->prev_count still holds the non-zero value from its previous slice,
and a BPF NMI program on the same CPU can call bpf_perf_event_read() ->
perf_event_read_local() -> perf_iommu_read():

event_sched_in()
  WRITE_ONCE(event->oncpu, smp_processor_id())
  perf_iommu_add() -> perf_iommu_start(..., PERF_EF_RELOAD)
    amd_iommu_pc_set_reg(..., IOMMU_PC_COUNTER_REG, 0)
    <NMI>
      bpf_perf_event_read() -> perf_event_read_local()
        perf_iommu_read()
          prev = local64_read(&hwc->prev_count)  /* stale P */
          amd_iommu_pc_get_reg(...) -> new = 0
          delta = (0 - P) & GENMASK_ULL(47, 0)   <-- adds ~2^48
    local64_set(&hwc->prev_count, 0)

In the else branch, an NMI read between amd_iommu_pc_get_reg() and
local64_set(&hwc->prev_count, ...) has its updated prev_count overwritten
backwards, causing double-counting.  Similarly, perf_iommu_stop()
power-gates the counter via perf_iommu_disable_event() before setting
PERF_HES_STOPPED while event->oncpu is still set, opening the same ~2^48
wraparound window.

Checking READ_ONCE(hwc->state) & PERF_HES_STOPPED in perf_iommu_read(),
clearing PERF_HES_STOPPED only after updating hwc->prev_count in
perf_iommu_start(), and setting PERF_HES_STOPPED before
perf_iommu_disable_event() in perf_iommu_stop() (with compiler barriers)
would close these windows.

>  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);
>  }

[Severity: Medium]
Should perf_iommu_read() return early when PERF_HES_STOPPED is set?

If an amd_iommu event is grouped with a sampling software event and the
group is throttled, perf_event_throttle_group() -> perf_event_throttle()
calls perf_iommu_stop(event, 0) while leaving event->state set to
PERF_EVENT_STATE_ACTIVE.  perf_iommu_stop() leaves the last non-zero
count in hwc->prev_count and disables the counter, which power-gates it
so subsequent reads return 0.

Because event->state remains PERF_EVENT_STATE_ACTIVE, a later
perf_pmu_read() (for example from a userspace read() syscall) still
invokes perf_iommu_read(), which reads 0 against the non-zero prev_count
and adds 2^48 - prev to event->count.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203105725.315334-1-vasant.hegde@amd.com?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-03 11:20 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 10:57 [PATCH] x86/events/amd/iommu: Count IOMMU events by counter delta Vasant Hegde
2026-12-03 11:20 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox