Linux Perf Users
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Chen Pei" <cp0613@linux.alibaba.com>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH 1/2] perf riscv: Implement rdtsc()
Date: Mon, 28 Sep 2026 09:54:26 +0000	[thread overview]
Message-ID: <20260928095426.C035A1F000FF@smtp.kernel.org> (raw)
In-Reply-To: <20260928094739.2605-2-cp0613@linux.alibaba.com>

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 <cp0613@linux.alibaba.com>

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.

> diff --git a/tools/perf/arch/riscv/util/tsc.c b/tools/perf/arch/riscv/util/tsc.c
> new file mode 100644
> index 0000000000000..7cc4710ac38c3
> --- /dev/null
> +++ b/tools/perf/arch/riscv/util/tsc.c
> @@ -0,0 +1,19 @@
[ ... ]
> +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));

[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.

> +
> +	return val;
> +}

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260928094739.2605-1-cp0613@linux.alibaba.com?part=1

  reply	other threads:[~2026-09-28  9:54 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-28  9:47 [PATCH 0/2] perf riscv: Implement rdtsc() Chen Pei
2026-09-28  9:47 ` [PATCH 1/2] " Chen Pei
2026-09-28  9:54   ` sashiko-bot [this message]
2026-09-28 13:42   ` Anup Patel
2026-09-28  9:47 ` [PATCH 2/2] perf tsc: Replace the compile-time TSC support check with a weak hook Chen Pei
2026-09-28  9:55   ` sashiko-bot
2026-09-28 13:42   ` Anup Patel
2026-09-28 16:34   ` Leo Yan
2026-09-29  3:40     ` Chen Pei
2026-09-29  8:28   ` David Laight
2026-09-29  9:08     ` Chen Pei

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=20260928095426.C035A1F000FF@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=cp0613@linux.alibaba.com \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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