All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jiangtian Feng <jiangtianfeng@linux.alibaba.com>
To: Will Deacon <will@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Besar Wicaksono <bwicaksono@nvidia.com>
Cc: Robin Murphy <robin.murphy@arm.com>,
	linux-arm-kernel@lists.infradead.org,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
	jiangtianfeng@linux.alibaba.com
Subject: [PATCH 2/2] perf/arm_cspmu: nvidia: Fix TGT address-filter slot lifecycle
Date: Fri, 27 Nov 2026 15:10:10 +0800	[thread overview]
Message-ID: <20261127071021.820833-3-jiangtianfeng@linux.alibaba.com> (raw)
In-Reply-To: <20261127071021.820833-1-jiangtianfeng@linux.alibaba.com>

The current lookup returns a free slot before checking later slots for
an identical active filter. After another filter is stopped, a new event
can therefore occupy a different slot from an existing identical filter.
Reset then searches only by base/mask and can release the wrong slot.

Reset also matches zeroed metadata from free slots and decrements their
refcount without checking occupancy. A zero base/mask filter can thus
underflow a free slot's refcount when another filter is stopped first.
Cycle events never allocate an address-filter slot, but their stop path
can still call reset and release another event's filter.

First match an active filter so identical filters share one slot. Only
allocation searches for a free slot afterward. Reset must match an active
slot, and cycle events return without resetting an address filter. This
keeps zero base/mask as valid filter data and does not change the shared
PMU callback ABI.

Qoder, an AI coding assistant, helped analyze the issue, develop the fix
and source-derived state model, and draft this description.

Fixes: 3dd73022306b ("perf/arm_cspmu: nvidia: Add Tegra410 PCIE-TGT PMU")
Signed-off-by: Jiangtian Feng <jiangtianfeng@linux.alibaba.com>
---
 drivers/perf/arm_cspmu/nvidia_cspmu.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/drivers/perf/arm_cspmu/nvidia_cspmu.c b/drivers/perf/arm_cspmu/nvidia_cspmu.c
index a0257dc3249d..1e9e52cbec09 100644
--- a/drivers/perf/arm_cspmu/nvidia_cspmu.c
+++ b/drivers/perf/arm_cspmu/nvidia_cspmu.c
@@ -589,7 +589,7 @@ static struct pcie_tgt_data *pcie_tgt_get_data(struct arm_cspmu *cspmu)
 	return ctx->data;
 }
 
-/* Find the first available address filter slot. */
+/* Find an active matching filter or a free slot for allocation. */
 static int pcie_tgt_find_addr_idx(struct arm_cspmu *cspmu, u64 base, u64 mask,
 	bool is_reset)
 {
@@ -597,11 +597,17 @@ static int pcie_tgt_find_addr_idx(struct arm_cspmu *cspmu, u64 base, u64 mask,
 	struct pcie_tgt_data *data = pcie_tgt_get_data(cspmu);
 
 	for (i = 0; i < NV_PCIE_TGT_ADDR_COUNT; i++) {
-		if (!is_reset && data->addr_filter[i].refcount == 0)
+		if (data->addr_filter[i].refcount > 0 &&
+		    data->addr_filter[i].base == base &&
+		    data->addr_filter[i].mask == mask)
 			return i;
+	}
+
+	if (is_reset)
+		return -ENODEV;
 
-		if (data->addr_filter[i].base == base &&
-			data->addr_filter[i].mask == mask)
+	for (i = 0; i < NV_PCIE_TGT_ADDR_COUNT; i++) {
+		if (data->addr_filter[i].refcount == 0)
 			return i;
 	}
 
@@ -748,6 +754,10 @@ static void pcie_tgt_pmu_reset_ev_filter(struct arm_cspmu *cspmu,
 	u64 base, mask;
 	int idx;
 
+	/* Cycle events never allocate an address-filter slot. */
+	if (event->hw.extra_reg.idx == cspmu->cycle_counter_logical_idx)
+		return;
+
 	addr_filter_en = pcie_tgt_pmu_addr_en(event);
 	if (!addr_filter_en)
 		return;
-- 
2.47.3


  parent reply	other threads:[~2026-11-27  7:10 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27  7:10 [PATCH 0/2] perf/arm_cspmu: nvidia: Fix TGT address-filter handling Jiangtian Feng
2026-11-27  7:10 ` [PATCH 1/2] perf/arm_cspmu: nvidia: Reject missing TGT address-filter resource Jiangtian Feng
2026-12-02  1:15   ` Besar Wicaksono
2026-11-27  7:10 ` Jiangtian Feng [this message]
2026-12-02  1:20   ` [PATCH 2/2] perf/arm_cspmu: nvidia: Fix TGT address-filter slot lifecycle Besar Wicaksono
2026-11-27  7:17 ` [PATCH 0/2] perf/arm_cspmu: nvidia: Fix TGT address-filter handling 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=20261127071021.820833-3-jiangtianfeng@linux.alibaba.com \
    --to=jiangtianfeng@linux.alibaba.com \
    --cc=bwicaksono@nvidia.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=robin.murphy@arm.com \
    --cc=will@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.