mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Ilkka Koskinen <ilkka@os.amperecomputing.com>
To: ilkka@os.amperecomputing.com, will@kernel.org,
	mark.rutland@arm.com, robin.murphy@arm.com
Cc: linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org, patches@amperecomputing.com
Subject: [PATCH] perf/arm-cmn: Check all the DTCs when reading global counters
Date: Fri, 20 Jan 2023 18:10:45 -0800	[thread overview]
Message-ID: <20230121021045.47595-1-ilkka@os.amperecomputing.com> (raw)

Some events may be available on nodes, none of which belongs to DTC0.
When the driver reads the global counters, it stops as soon as it finds a
DTC that's not being used and, thus, ignores the rest. As the driver
doesn't read the paired global counters, overflowing local counters are
regarded as overflowing global counters (assuming the new local counter
value is smaller than the previous one) and therefore we can see values
around 2^64. Fix the issue by checking all the used DTCs.

The driver is still trying to find a counter that's available on all the
DTCs rather than doing per-DTC allocation of global counters. We may
need to change it in the future, if needed.

Signed-off-by: Ilkka Koskinen <ilkka@os.amperecomputing.com>
---
 drivers/perf/arm-cmn.c | 41 ++++++++++++++++++++++++++++++++---------
 1 file changed, 32 insertions(+), 9 deletions(-)

diff --git a/drivers/perf/arm-cmn.c b/drivers/perf/arm-cmn.c
index b80a9b74662b..c516f091a002 100644
--- a/drivers/perf/arm-cmn.c
+++ b/drivers/perf/arm-cmn.c
@@ -540,7 +540,7 @@ struct arm_cmn_hw_event {
 	struct arm_cmn_node *dn;
 	u64 dtm_idx[4];
 	unsigned int dtc_idx;
-	u8 dtcs_used;
+	unsigned long dtcs_used;
 	u8 num_dns;
 	u8 dtm_offset;
 	bool wide_sel;
@@ -550,6 +550,9 @@ struct arm_cmn_hw_event {
 #define for_each_hw_dn(hw, dn, i) \
 	for (i = 0, dn = hw->dn; i < hw->num_dns; i++, dn++)
 
+#define for_each_used_dtc(hw, cmn, i) \
+	for_each_set_bit(i, &(hw)->dtcs_used, (cmn)->num_dtcs)
+
 static struct arm_cmn_hw_event *to_cmn_hw(struct perf_event *event)
 {
 	BUILD_BUG_ON(sizeof(struct arm_cmn_hw_event) > offsetof(struct hw_perf_event, target));
@@ -1272,7 +1275,7 @@ static void arm_cmn_init_counter(struct perf_event *event)
 	unsigned int i, pmevcnt = CMN_DT_PMEVCNT(hw->dtc_idx);
 	u64 count;
 
-	for (i = 0; hw->dtcs_used & (1U << i); i++) {
+	for_each_used_dtc(hw, cmn, i) {
 		writel_relaxed(CMN_COUNTER_INIT, cmn->dtc[i].base + pmevcnt);
 		cmn->dtc[i].counters[hw->dtc_idx] = event;
 	}
@@ -1301,7 +1304,7 @@ static void arm_cmn_event_read(struct perf_event *event)
 	delta = new - prev;
 
 	local_irq_save(flags);
-	for (i = 0; hw->dtcs_used & (1U << i); i++) {
+	for_each_used_dtc(hw, cmn, i) {
 		new = arm_cmn_read_counter(cmn->dtc + i, hw->dtc_idx);
 		delta += new << 16;
 	}
@@ -1614,10 +1617,32 @@ static void arm_cmn_event_clear(struct arm_cmn *cmn, struct perf_event *event,
 	}
 	memset(hw->dtm_idx, 0, sizeof(hw->dtm_idx));
 
-	for (i = 0; hw->dtcs_used & (1U << i); i++)
+	for_each_used_dtc(hw, cmn, i)
 		cmn->dtc[i].counters[hw->dtc_idx] = NULL;
 }
 
+static int arm_cmn_get_global_counter(struct arm_cmn *cmn,
+				      struct arm_cmn_hw_event *hw)
+{
+	int dtc_idx, i;
+	bool available;
+
+	for (dtc_idx = 0; dtc_idx < CMN_DT_NUM_COUNTERS; dtc_idx++) {
+		available = true;
+		for_each_used_dtc(hw, cmn, i) {
+			if (cmn->dtc[i].counters[dtc_idx]) {
+				available = false;
+				break;
+			}
+		}
+
+		if (available)
+			return dtc_idx;
+	}
+
+	return -ENOSPC;
+}
+
 static int arm_cmn_event_add(struct perf_event *event, int flags)
 {
 	struct arm_cmn *cmn = to_cmn(event->pmu);
@@ -1642,11 +1667,9 @@ static int arm_cmn_event_add(struct perf_event *event, int flags)
 		return 0;
 	}
 
-	/* Grab a free global counter first... */
-	dtc_idx = 0;
-	while (dtc->counters[dtc_idx])
-		if (++dtc_idx == CMN_DT_NUM_COUNTERS)
-			return -ENOSPC;
+	dtc_idx = arm_cmn_get_global_counter(cmn, hw);
+	if (dtc_idx < 0)
+		return dtc_idx;
 
 	hw->dtc_idx = dtc_idx;
 
-- 
2.17.1


             reply	other threads:[~2023-01-21  2:11 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-21  2:10 Ilkka Koskinen [this message]
2023-01-23 18:28 ` Robin Murphy

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=20230121021045.47595-1-ilkka@os.amperecomputing.com \
    --to=ilkka@os.amperecomputing.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=patches@amperecomputing.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 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®