From: Chen Pei <cp0613@linux.alibaba.com>
To: acme@kernel.org, peterz@infradead.org, mingo@redhat.com,
namhyung@kernel.org
Cc: mark.rutland@arm.com, alexander.shishkin@linux.intel.com,
jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com,
james.clark@linaro.org, leo.yan@linux.dev,
john.g.garry@oracle.com, will@kernel.org, mike.leach@arm.com,
pjw@kernel.org, palmer@dabbelt.com, guoren@kernel.org,
linux-perf-users@vger.kernel.org,
linux-arm-kernel@lists.infradead.org,
linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org,
Anup Patel <anup@brainfault.org>
Subject: [PATCH v2 1/2] perf riscv: Implement rdtsc()
Date: Tue, 29 Sep 2026 11:41:32 +0800 [thread overview]
Message-ID: <20260929034133.2153-2-cp0613@linux.alibaba.com> (raw)
In-Reply-To: <20260929034133.2153-1-cp0613@linux.alibaba.com>
Perf has no riscv implementation of rdtsc(), so it links against the weak
stub in util/tsc.c and every caller reads a constant 0 that cannot be
told apart from a counter standing still.
Implement rdtsc() over the time CSR with rdtime. The time CSR is a TSC
equivalent on riscv: readable from user mode, constant frequency, and the
same counter riscv's arch_perf_update_userpage() builds the user page
conversion fields from, so the counter and perf time share a timebase.
Define it for RV64 only. Reading the counter whole on RV32 takes time and
timeh in a loop, and no RV32 user in tools/perf needs it that much, so
RV32 keeps the weak stub.
Reviewed-by: Anup Patel <anup@brainfault.org>
Signed-off-by: Chen Pei <cp0613@linux.alibaba.com>
---
Changes in v2:
- Define rdtsc() for RV64 only.
tools/perf/arch/riscv/util/Build | 1 +
tools/perf/arch/riscv/util/tsc.c | 27 +++++++++++++++++++++++++++
2 files changed, 28 insertions(+)
create mode 100644 tools/perf/arch/riscv/util/tsc.c
diff --git a/tools/perf/arch/riscv/util/Build b/tools/perf/arch/riscv/util/Build
index 2328fb9a30a3..52cd6a481de8 100644
--- a/tools/perf/arch/riscv/util/Build
+++ b/tools/perf/arch/riscv/util/Build
@@ -1 +1,2 @@
perf-util-y += header.o
+perf-util-y += tsc.o
diff --git a/tools/perf/arch/riscv/util/tsc.c b/tools/perf/arch/riscv/util/tsc.c
new file mode 100644
index 000000000000..3dd419ce9d2a
--- /dev/null
+++ b/tools/perf/arch/riscv/util/tsc.c
@@ -0,0 +1,27 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/types.h>
+
+#include "../../../util/tsc.h"
+
+/*
+ * RV32 is left to the weak stub: reading the counter whole there takes time
+ * and timeh in a loop, and no RV32 user in tools/perf needs it that much.
+ */
+#if defined(__riscv) && __riscv_xlen == 64
+
+u64 rdtsc(void)
+{
+ u64 val;
+
+ /*
+ * The time CSR ticks at a constant frequency and is the same counter
+ * the kernel feeds into the user page conversion fields through
+ * sched_clock(), so it is a TSC equivalent here.
+ */
+ asm volatile("rdtime %0" : "=r" (val));
+
+ return val;
+}
+
+#endif
--
2.50.1
next prev parent reply other threads:[~2026-09-29 3:41 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-29 3:41 [PATCH v2 0/2] " Chen Pei
2026-09-29 3:41 ` Chen Pei [this message]
2026-09-29 3:41 ` [PATCH v2 2/2] perf tsc: Declare TSC support from the build system Chen Pei
2026-09-29 9:49 ` Leo Yan
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=20260929034133.2153-2-cp0613@linux.alibaba.com \
--to=cp0613@linux.alibaba.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=anup@brainfault.org \
--cc=guoren@kernel.org \
--cc=irogers@google.com \
--cc=james.clark@linaro.org \
--cc=john.g.garry@oracle.com \
--cc=jolsa@kernel.org \
--cc=leo.yan@linux.dev \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=mark.rutland@arm.com \
--cc=mike.leach@arm.com \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=palmer@dabbelt.com \
--cc=peterz@infradead.org \
--cc=pjw@kernel.org \
--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®