From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796296825; b=vJdhA0cghXnKy4PzNuCQb2pG+Zik8upBrJrNyZg+nEWjIa+9TB37eGBVOuRx98MmYf0f XK8OIGeSOTEs9+jyBxGF0sWpZ1fTAxpT+/EKzdGwAR8t4gBDqZ5JcrYKgaO2L9IzmPABs 0fjvRFFy/mZXewVoZPXLOXIC3JItj/yYrdZH5Wmd7eVVCwnbbi39/vLq4T2TfnYTWJRjT 8HtfnSxp2zgZYwPATogcAvIAE5AhX6/gdMqgIz2HTSMLvH4gPdqsbDFNgCyqyVI2J+9Yz mxAVsDRe4CoSySq3lkQ8jaaZ+GoblYnlWlgr8w4kT/DLYkTU8Uad1QVuKqRlGRiWqtg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796296825; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=ivp/7W3j9CEjgpzBHKDE0a8yZNjQ5UOShNqFSIOwUvg=; b=GGNoUvlt5JbrtRTwJoYYhG4+1gVvyJOx0/oh7FhsmqtM6eI3ecMhMQQpSKgio5MQi8SJ Y3wuJAEiX9Y1Y4RHXagrdqkKLqKCqggzkhNCoUXmZVdu/jwyiNF4kE7ZySv3VkS0DWjTJ K9TLUi6pgTXi27/78gfnN2xBUJL/mfr6m1bY8g5YffFhEDa05o2mDRvKTD+NzmNVQJQvA zRfzn42PM3qQULoQXTLSy4xVi6VuYUEkydNbZhJTCrjEo0JdWAZw0BqirODEJLyAq7jPk He1R5cMHKv0ZDS8kwGaOxxSa59HeEmyPTkRHzoJ/UDHt72uIVttOEpzi8DuB0mfgyzw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=COwYpdB5; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F0872606A322 for ; Thu, 3 Dec 2026 11:20:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=COwYpdB5 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 88B591F000C1; Thu, 3 Dec 2026 11:20:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796296824; bh=ivp/7W3j9CEjgpzBHKDE0a8yZNjQ5UOShNqFSIOwUvg=; h=From:Subject:Reply-To:To:In-Reply-To:References:Date; b=COwYpdB57hlsRt8xvC5TuFnzOcjud7WNshP6u7DPP1snW90/O0wbuk9Ypx11+tRn0 tQy0JFBmCsfocX61yhMC4dNAqnygObJnT3IquqrxLX79Pa9GBz/Gxc2KVKUMnJKCYT vXWg9ndc26qjVnxylR0MCVS8MIUU6ORdjuQnmtNYNqJhxfSQyaq7X73EbxGHCgpuqx U3ZRQVNtuYjDLau0FzamjeemtatoOpgPqC6Qi4pSEhD6ZIqEc9A2cBR5OFpvmdgs4T K2w1hbMmCIHPb05bMO6CsksGp8VkAGMcqmKAJZtkmPXYsskljoJkKXLvO1EX4cOwGP 2RcOP6sbGhLeQ== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH] x86/events/amd/iommu: Count IOMMU events by counter delta Reply-To: sashiko-reviews@lists.linux.dev To: "Vasant Hegde" , linux-perf-users@vger.kernel.org In-Reply-To: <20261203105725.315334-1-vasant.hegde@amd.com> References: <20261203105725.315334-1-vasant.hegde@amd.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Thu, 03 Dec 2026 11:20:24 +0000 X-Mailing-List: linux-perf-users@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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 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 *eve= nt, int flags) > perf_iommu_enable_event(event); > =20 > if (flags & PERF_EF_RELOAD) { > - u64 count =3D 0; > - struct amd_iommu *iommu =3D 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 =3D 0; > } > =20 > + 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) bpf_perf_event_read() -> perf_event_read_local() perf_iommu_read() prev =3D local64_read(&hwc->prev_count) /* stale P */ amd_iommu_pc_get_reg(...) -> new =3D 0 delta =3D (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 =3D &event->hw; > struct amd_iommu *iommu =3D perf_event_2_iommu(event); > + u64 prev, new, delta; > =20 > +again: > + prev =3D 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; > =20 > - /* IOMMU pc counter register is only 48 bits */ > - count &=3D GENMASK_ULL(47, 0); > + /* The counter is 48 bits and wraps. */ > + new &=3D GENMASK_ULL(47, 0); > + if (local64_cmpxchg(&hwc->prev_count, prev, new) !=3D prev) > + goto again; > =20 > - /* > - * Since the counter always start with value zero, > - * simply just accumulate the count for the event. > - */ > - local64_add(count, &event->count); > + delta =3D (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. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261203105725.3153= 34-1-vasant.hegde@amd.com?part=3D1