perf riscv: Implement rdtsc()
Subsystems (3)
cover perf riscv: Implement rdtsc()
tools/perf falls back to the __weak rdtsc() stub in tools/perf/util/tsc.c on every architecture that does not override it, riscv included. The stub returns a constant 0, so anything that reaches for the counter gets a value that means nothing and cannot tell it apart from a counter that happens to be at 0.
The time CSR is a natural TSC equivalent on riscv: readable from user mode, constant frequency, and the same counter that riscv's arch_perf_update_userpage() feeds the user page conversion fields from, ever since commit 83c5e13b8cbb ("riscv: Prepare for user-space perf event mmap support"). This series registers that on the tools side.
The two patches are split so that the first stands on its own:
1. Implement rdtsc() over the time CSR for riscv.
2. Replace the compile-time TSC support check with a weak arch__rdtsc_supported() hook, so that support is declared next to the rdtsc() it describes rather than in the central list of compiler macros.
What this is worth beyond the subtest result:
1. The subtest is the only reader of those conversion fields riscv can run, so it guards arithmetic that has shipped unexercised; registering the counter at the same time leaves a future riscv auxtrace driver the reading and the conversion already in place.
2. The hook gives a caller something to ask the architecture before it uses the counter value, instead of each caller keeping its own copy of the macro list.
Tests:
======
- The "Convert perf time to TSC" test in a riscv64 QEMU virt guest with sscofpmf: the subtest skips before the series, passes after it.
- With the riscv implementation left out of the build, so that the weak defaults are in effect, the subtest still skips: the behaviour of an architecture without a counter is unchanged.
Chen Pei (2):
perf riscv: Implement rdtsc()
perf tsc: Replace the compile-time TSC support check with a weak hook
tools/perf/arch/arm64/util/tsc.c | 5 +++++ tools/perf/arch/riscv/util/Build | 1 + tools/perf/arch/riscv/util/tsc.c | 24 ++++++++++++++++++++++++ tools/perf/arch/x86/util/tsc.c | 5 +++++ tools/perf/tests/perf-time-to-tsc.c | 12 +----------- tools/perf/util/tsc.c | 9 +++++++++ tools/perf/util/tsc.h | 1 + 7 files changed, 46 insertions(+), 11 deletions(-) create mode 100644 tools/perf/arch/riscv/util/tsc.c
Signature · 1 line
2.50.1
PATCH 1/2 perf riscv: Implement rdtsc()
There is no riscv implementation of rdtsc(), so perf links against the weak stub in util/tsc.c and every caller reads a constant 0 that is indistinguishable from a real counter standing still.
Implement rdtsc() over the time CSR with rdtime. The time CSR is a TSC equivalent on riscv: it is readable from user mode, it ticks at a constant frequency, and it is the same counter that riscv's arch_perf_update_userpage() builds the user page conversion fields from, so the counter and perf time share a timebase.
--- tools/perf/arch/riscv/util/Build | 1 + tools/perf/arch/riscv/util/tsc.c | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 tools/perf/arch/riscv/util/tsc.c
tools/perf/arch/riscv/util/Build +1 -0
@@ -1 +1,2 @@ perf-util-y += header.o +perf-util-y += tsc.o
tools/perf/arch/riscv/util/tsc.c new file +19 -0
@@ -0,0 +1,19 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/types.h> + +#include "../../../util/tsc.h" + +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. +*/ + asmvolatile ("rdtime %0" :"=r" (val)); + +return val; +}
Signature · 1 line
2.50.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] perf riscv: uninitialized upper 32 bits in rdtsc() on RV32 --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] perf riscv: uninitialized upper 32 bits in rdtsc() on RV32
--
commit fa857133aeb89c2c952b09a7575fc1ec0de78663
Author: Chen Pei <[email protected]>
perf riscv: Implement rdtsc()
There is no riscv implementation of rdtsc(), so perf links against the weak stub in util/tsc.c and every caller reads a constant 0. This commit implements rdtsc() over the time CSR with rdtime, which is a TSC equivalent on riscv.
Quoted 6 lines
tools/perf/arch/riscv/util/tsc.c new file +0
-0@@ -0,0 +1,19 @@
[ ... ]
tools/perf/arch/riscv/util/tsc.c line 16
Quoted 10 lines
tools/perf/arch/riscv/util/tsc.c new file +10
-0+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. +*/ + asmvolatile ("rdtime %0" :"=r" (val));
[Severity: Medium]
On 32-bit RISC-V (RV32), does this leave the upper 32 bits of val uninitialized?
The rdtime instruction on RV32 is a pseudo-instruction that reads only the lower 32 bits of the time CSR. Because the target variable val is a 64-bit integer (u64), the compiler maps it to a register pair on RV32.
However, the inline assembly constraint "=r" only writes to the first 32-bit register, leaving the upper 32 bits completely uninitialized and containing garbage. This could corrupt perf timestamp calculations when running on an RV32 system.
Proper 64-bit CSR reading on RV32 requires a loop reading both the time and timeh CSRs.
Quoted 3 lines
tools/perf/arch/riscv/util/tsc.c new file +3
-0+ +return val; +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 5 lines · Chen Pei
There is no riscv implementation of
rdtsc(), so perf links against the weak stub inutil/tsc.cand every caller reads a constant 0 that is indistinguishable from a real counter standing still.
Quoted 7 lines
Implement
rdtsc()over the time CSR with rdtime. The time CSR is a TSC equivalent on riscv: it is readable from user mode, it ticks at a constant frequency, and it is the same counter that riscv'sarch_perf_update_userpage()builds the user page conversion fields from, so the counter and perf time share a timebase.Signed-off-by Chen Pei <[email protected]>
LGTM from RISC-V perspective.
Regards,
Anup
Quoted 42 lines
--- tools/perf/arch/riscv/util/Build | 1 + tools/perf/arch/riscv/util/tsc.c | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 tools/perf/arch/riscv/util/tsc.ctools/perf/arch/riscv/util/Build +1
-0@@ -1 +1,2 @@ perf-util-y += header.o +perf-util-y += tsc.otools/perf/arch/riscv/util/tsc.c new file +19
-1@@ -0,0 +1,19 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/types.h> + +#include "../../../util/tsc.h" + +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. +*/ + asmvolatile ("rdtime %0" :"=r" (val)); + +return val; +} --2.50.1
PATCH 2/2 perf tsc: Replace the compile-time TSC support check with a weak hook
In reply to Chen PeiThe "TSC support" subtest of the "Convert perf time to TSC" test decides whether the architecture is covered by matching compiler macros for x86_64, i386 and arm64. That list has to be edited for every new architecture, and it can drift apart from the set of architectures that really provide a counter: util/tsc.c already ships a weak rdtsc() which an architecture overrides with a strong definition when it has one.
Add a weak arch__rdtsc_supported() next to the weak rdtsc(), let the test query it instead of the macros, and have x86, arm64 and riscv override it in the file where they implement rdtsc(). Support is then declared next to the counter it describes, and a caller can tell a stub apart from a real counter without keeping its own copy of the macro list. This follows the pattern of the other optional per-architecture hooks in util/ such as get_cpuid_str().
x86 and arm64 keep the same behaviour, and riscv starts running the subtest because it gained rdtsc() in the previous patch.
--- tools/perf/arch/arm64/util/tsc.c | 5 +++++ tools/perf/arch/riscv/util/tsc.c | 5 +++++ tools/perf/arch/x86/util/tsc.c | 5 +++++ tools/perf/tests/perf-time-to-tsc.c | 12 +----------- tools/perf/util/tsc.c | 9 +++++++++ tools/perf/util/tsc.h | 1 + 6 files changed, 26 insertions(+), 11 deletions(-)
tools/perf/arch/arm64/util/tsc.c +5 -0
@@ -19,3 +19,8 @@ u64 rdtsc(void)return val; } + +bool arch__rdtsc_supported (void ) +{ +return true; +}
tools/perf/arch/riscv/util/tsc.c +5 -0
@@ -17,3 +17,8 @@ u64 rdtsc(void)return val; } + +bool arch__rdtsc_supported (void ) +{ +return true; +}
tools/perf/arch/x86/util/tsc.c +5 -0
@@ -17,6 +17,11 @@ u64 rdtsc(void)return low | ((u64 )high) <<32 ; } +bool arch__rdtsc_supported (void ) +{ +return true; +} +/* * Derive the TSC frequency in Hz from the /proc/cpuinfo, for example: * ...
tools/perf/tests/perf-time-to-tsc.c +1 -11
@@ -22,16 +22,6 @@#include "tests.h" #include "util/sample.h" -/* -* Except x86_64/i386 and Arm64, other archs don't support TSC in perf. Just -* enable the test for x86_64/i386 and Arm64 archs. -*/ -#if defined(__x86_64__) || defined(__i386__) || defined(__aarch64__) -#define TSC_IS_SUPPORTED 1 -#else -#define TSC_IS_SUPPORTED 0 -#endif -#define CHECK__ (x) { \ while ((x) < 0) { \ pr_debug(#x " failed!\n"); \
@@ -49,7 +39,7 @@static int test__tsc_is_supported (struct test_suite *test__maybe_unused ,int subtest__maybe_unused ) { -if (!TSC_IS_SUPPORTED ) { +if (!arch__rdtsc_supported ()) {pr_debug ("Test not supported on this architecture\n" );return TEST_SKIP ; }
tools/perf/util/tsc.c +9 -0
@@ -113,6 +113,15 @@ u64 __weak rdtsc(void)return 0 ; } +/* +* An architecture that implements rdtsc() with a real counter should override +* this, so that users of the counter can tell a stub apart from support. +*/ +bool __weak arch__rdtsc_supported (void ) +{ +return false; +} +size_t perf_event__fprintf_time_conv (union perf_event *event,FILE *fp) {struct perf_record_time_conv *tc = (struct perf_record_time_conv *)event;
tools/perf/util/tsc.h +1 -0
@@ -25,6 +25,7 @@ int perf_read_tsc_conversion(const struct perf_event_mmap_page *pc,u64 perf_time_to_tsc (u64 ns,struct perf_tsc_conversion *tc);u64 tsc_to_perf_time (u64 cyc,struct perf_tsc_conversion *tc);u64 rdtsc (void ); +bool arch__rdtsc_supported (void );u64 arch_get_tsc_freq (void );size_t perf_event__fprintf_time_conv (union perf_event *event,FILE *fp);
Signature · 1 line
2.50.1
[email protected]
AI reviewRe: [PATCH 2/2] perf tsc: Replace the compile-time TSC support check with a weak hook
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 15 lines
The "TSC support" subtest of the "Convert perf time to TSC" test decides whether the architecture is covered by matching compiler macros for
x86_64, i386 and arm64. That list has to be edited for every new architecture, and it can drift apart from the set of architectures that really provide a counter:util/tsc.calready ships a weakrdtsc()which an architecture overrides with a strong definition when it has one.Add a weak
arch__rdtsc_supported()next to the weakrdtsc(), let the test query it instead of the macros, and have x86, arm64 and riscv override it in the file where they implementrdtsc(). Support is then declared next to the counter it describes, and a caller can tell a stub apart from a real counter without keeping its own copy of the macro list. This follows the pattern of the other optional per-architecture hooks in util/ such asget_cpuid_str().
Quoted 4 lines
x86 and arm64 keep the same behaviour, and riscv starts running the subtest because it gained
rdtsc()in the previous patch.Signed-off-by Chen Pei <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH 2/2] perf tsc: Replace the compile-time TSC support check with a weak hook
In reply to Chen PeiQuoted 16 lines · Chen Pei
The "TSC support" subtest of the "Convert perf time to TSC" test decides whether the architecture is covered by matching compiler macros for
x86_64, i386 and arm64. That list has to be edited for every new architecture, and it can drift apart from the set of architectures that really provide a counter:util/tsc.calready ships a weakrdtsc()which an architecture overrides with a strong definition when it has one.Add a weak
arch__rdtsc_supported()next to the weakrdtsc(), let the test query it instead of the macros, and have x86, arm64 and riscv override it in the file where they implementrdtsc(). Support is then declared next to the counter it describes, and a caller can tell a stub apart from a real counter without keeping its own copy of the macro list. This follows the pattern of the other optional per-architecture hooks in util/ such asget_cpuid_str().
Quoted 4 lines
x86 and arm64 keep the same behaviour, and riscv starts running the subtest because it gained
rdtsc()in the previous patch.Signed-off-by Chen Pei <[email protected]>
LGTM from RISC-V perspective.
Regards,
Anup
Quoted 117 lines
--- tools/perf/arch/arm64/util/tsc.c | 5 +++++ tools/perf/arch/riscv/util/tsc.c | 5 +++++ tools/perf/arch/x86/util/tsc.c | 5 +++++ tools/perf/tests/perf-time-to-tsc.c | 12 +----------- tools/perf/util/tsc.c | 9 +++++++++ tools/perf/util/tsc.h | 1 + 6 files changed, 26 insertions(+), 11 deletions(-)tools/perf/arch/arm64/util/tsc.c +5
-0@@ -19,3 +19,8 @@ u64 rdtsc(void)return val; } + +bool arch__rdtsc_supported (void ) +{ +return true; +}tools/perf/arch/riscv/util/tsc.c +5
-0@@ -17,3 +17,8 @@ u64 rdtsc(void)return val; } + +bool arch__rdtsc_supported (void ) +{ +return true; +}tools/perf/arch/x86/util/tsc.c +5
-0@@ -17,6 +17,11 @@ u64 rdtsc(void)return low | ((u64 )high) <<32 ; } +bool arch__rdtsc_supported (void ) +{ +return true; +} +/* * Derive the TSC frequency in Hz from the /proc/cpuinfo, for example: * ... tools/perf/tests/perf-time-to-tsc.c +1
-11@@ -22,16 +22,6 @@#include "tests.h" #include "util/sample.h" -/* -* Except x86_64/i386 and Arm64, other archs don't support TSC in perf. Just -* enable the test for x86_64/i386 and Arm64 archs. -*/ -#if defined(__x86_64__) || defined(__i386__) || defined(__aarch64__) -#define TSC_IS_SUPPORTED 1 -#else -#define TSC_IS_SUPPORTED 0 -#endif -#define CHECK__ (x) { \ while ((x) < 0) { \ pr_debug(#x " failed!\n"); \@@ -49,7 +39,7 @@static int test__tsc_is_supported (struct test_suite *test__maybe_unused ,int subtest__maybe_unused ) { -if (!TSC_IS_SUPPORTED ) { +if (!arch__rdtsc_supported ()) {pr_debug ("Test not supported on this architecture\n" );return TEST_SKIP ; }tools/perf/util/tsc.c +9
-0@@ -113,6 +113,15 @@ u64 __weak rdtsc(void)return 0 ; } +/* +* An architecture that implements rdtsc() with a real counter should override +* this, so that users of the counter can tell a stub apart from support. +*/ +bool __weak arch__rdtsc_supported (void ) +{ +return false; +} +size_t perf_event__fprintf_time_conv (union perf_event *event,FILE *fp) {struct perf_record_time_conv *tc = (struct perf_record_time_conv *)event;tools/perf/util/tsc.h +1
-1@@ -25,6 +25,7 @@ int perf_read_tsc_conversion(const struct perf_event_mmap_page *pc,u64 perf_time_to_tsc (u64 ns,struct perf_tsc_conversion *tc);u64 tsc_to_perf_time (u64 cyc,struct perf_tsc_conversion *tc);u64 rdtsc (void ); +bool arch__rdtsc_supported (void );u64 arch_get_tsc_freq (void );size_t perf_event__fprintf_time_conv (union perf_event *event,FILE *fp); --2.50.1
Re: [PATCH 2/2] perf tsc: Replace the compile-time TSC support check with a weak hook
In reply to Chen PeiOn Mon, Sep 28, 2026 at 05:47:39PM +0800, Chen Pei wrote:
[...]
Quoted 3 lines
Add a weak
arch__rdtsc_supported()next to the weakrdtsc(), let the test query it instead of the macros, and have x86, arm64 and riscv override it in the file where they implementrdtsc().
The patch itself is fine for me.
Just a bit on refactoring. You could refer to the implementation of PERF_HAVE_JITDUMP in arch Makefile and "CFLAGS += -DHAVE_JITDUMP" and do similiar thing for rdtsc.
Seems to me, we can just use build system for configurations.
Thanks,
Leo
Re: [PATCH 2/2] perf tsc: Replace the compile-time TSC support check with a weak hook
Hi Leo,
Thanks a lot for the review.
Quoted 5 lines
Just a bit on refactoring. You could refer to the implementation of
PERF_HAVE_JITDUMPin archMakefileand "CFLAGS += -DHAVE_JITDUMP" and do similiar thing for rdtsc.Seems to me, we can just use build system for configurations.
Makes sense, I'll change it that way in v2, thanks for pointing at the existing pattern.
Best regards,
Pei
Re: [PATCH 2/2] perf tsc: Replace the compile-time TSC support check with a weak hook
In reply to Chen PeiQuoted 6 lines · On Mon, 28 Sep 2026 17:47:39 +0800 Chen Pei <[email protected]> wrote:
The "TSC support" subtest of the "Convert perf time to TSC" test decides whether the architecture is covered by matching compiler macros for
x86_64, i386 and arm64. That list has to be edited for every new architecture, and it can drift apart from the set of architectures that really provide a counter:util/tsc.calready ships a weakrdtsc()which an architecture overrides with a strong definition when it has one.
If you mark the prototype in the .h file 'weak' you don't need a default - instead check the symbol for being NULL.
Then you can do:
if (!rdtsc) { pr_debug("Test not supported on this architecture\n"); return TEST_SKIP;
}
David
Quoted 126 lines
Add a weak
arch__rdtsc_supported()next to the weakrdtsc(), let the test query it instead of the macros, and have x86, arm64 and riscv override it in the file where they implementrdtsc(). Support is then declared next to the counter it describes, and a caller can tell a stub apart from a real counter without keeping its own copy of the macro list. This follows the pattern of the other optional per-architecture hooks in util/ such asget_cpuid_str().x86 and arm64 keep the same behaviour, and riscv starts running the subtest because it gained
rdtsc()in the previous patch.Signed-off-by Chen Pei <[email protected]>--- tools/perf/arch/arm64/util/tsc.c | 5 +++++ tools/perf/arch/riscv/util/tsc.c | 5 +++++ tools/perf/arch/x86/util/tsc.c | 5 +++++ tools/perf/tests/perf-time-to-tsc.c | 12 +----------- tools/perf/util/tsc.c | 9 +++++++++ tools/perf/util/tsc.h | 1 + 6 files changed, 26 insertions(+), 11 deletions(-)tools/perf/arch/arm64/util/tsc.c +5
-0@@ -19,3 +19,8 @@ u64 rdtsc(void)return val; } + +bool arch__rdtsc_supported (void ) +{ +return true; +}tools/perf/arch/riscv/util/tsc.c +5
-0@@ -17,3 +17,8 @@ u64 rdtsc(void)return val; } + +bool arch__rdtsc_supported (void ) +{ +return true; +}tools/perf/arch/x86/util/tsc.c +5
-0@@ -17,6 +17,11 @@ u64 rdtsc(void)return low | ((u64 )high) <<32 ; } +bool arch__rdtsc_supported (void ) +{ +return true; +} +/* * Derive the TSC frequency in Hz from the /proc/cpuinfo, for example: * ... tools/perf/tests/perf-time-to-tsc.c +1
-11@@ -22,16 +22,6 @@#include "tests.h" #include "util/sample.h" -/* -* Except x86_64/i386 and Arm64, other archs don't support TSC in perf. Just -* enable the test for x86_64/i386 and Arm64 archs. -*/ -#if defined(__x86_64__) || defined(__i386__) || defined(__aarch64__) -#define TSC_IS_SUPPORTED 1 -#else -#define TSC_IS_SUPPORTED 0 -#endif -#define CHECK__ (x) { \ while ((x) < 0) { \ pr_debug(#x " failed!\n"); \@@ -49,7 +39,7 @@static int test__tsc_is_supported (struct test_suite *test__maybe_unused ,int subtest__maybe_unused ) { -if (!TSC_IS_SUPPORTED ) { +if (!arch__rdtsc_supported ()) {pr_debug ("Test not supported on this architecture\n" );return TEST_SKIP ; }tools/perf/util/tsc.c +9
-0@@ -113,6 +113,15 @@ u64 __weak rdtsc(void)return 0 ; } +/* +* An architecture that implements rdtsc() with a real counter should override +* this, so that users of the counter can tell a stub apart from support. +*/ +bool __weak arch__rdtsc_supported (void ) +{ +return false; +} +size_t perf_event__fprintf_time_conv (union perf_event *event,FILE *fp) {struct perf_record_time_conv *tc = (struct perf_record_time_conv *)event;tools/perf/util/tsc.h +1
-0@@ -25,6 +25,7 @@ int perf_read_tsc_conversion(const struct perf_event_mmap_page *pc,u64 perf_time_to_tsc (u64 ns,struct perf_tsc_conversion *tc);u64 tsc_to_perf_time (u64 cyc,struct perf_tsc_conversion *tc);u64 rdtsc (void ); +bool arch__rdtsc_supported (void );u64 arch_get_tsc_freq (void );size_t perf_event__fprintf_time_conv (union perf_event *event,FILE *fp);
Re: [PATCH 2/2] perf tsc: Replace the compile-time TSC support check with a weak hook
Hi David,
Thanks a lot for the review.
Quoted 8 lines
If you mark the prototype in the .h file "weak" you don't need a default
- instead check the symbol for being NULL.Then you can do:
if (!rdtsc) { pr_debug("Test not supported on this architecture\n"); return TEST_SKIP; }
I've just sent v2, which declares the support from the build system instead, following the existing PERF_HAVE_JITDUMP pattern, so the decision is made at compile time and there is no symbol left to test for NULL.
Keeping the __weak rdtsc() stub also means the callers that already call it unconditionally don't have to grow a NULL check.
Thanks again for the suggestion.
Best regards,
Pei