From 577b85ec41b5560a81a4fb09d1ec7760bcacc49f Mon Sep 17 00:00:00 2001 From: Gilbert Gilb's Date: Sun, 22 Oct 2023 19:06:45 +0200 Subject: [PATCH] riscv64: get clock from `rdtime` instead of `rdcycle` `rdcycle` pseudo-instruction accesses the "cycle CSR" which holds the real count of CPU core clock cycles [1]. As this leaves room for side-channel attacks, access to this register from userland might be forbidden by the kernel, which results in a SIGILL [2]. Anyhow, it seems that the actual usage of the `get_cpu_clock` function in fio is about getting a wall-clock rather than the actual CPU core clock (for instance, x86 uses `rdtsc`), so this is technically a bug. The "time CSR" is the proper register to track time on riscv64. Also, the "time CSR" is more likely to be available from userspace and not cause a crash. [1] RISC-V ISA Section 10.1: https://github.com/riscv/riscv-isa-manual/releases/download/Ratified-IMAFDQC/riscv-spec-20191213.pdf [2] https://lore.kernel.org/all/YxIzgYP3MujXdqwj@aurel32.net/T/ Signed-off-by: N. Le Roux Signed-off-by: Gilbert Gilb's --- arch/arch-riscv64.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/arch-riscv64.h b/arch/arch-riscv64.h index 9b8fd001d7..8ac33fa31c 100644 --- a/arch/arch-riscv64.h +++ b/arch/arch-riscv64.h @@ -16,7 +16,7 @@ static inline unsigned long long get_cpu_clock(void) { unsigned long val; - asm volatile("rdcycle %0" : "=r"(val)); + asm volatile("rdtime %0" : "=r"(val)); return val; } #define ARCH_HAVE_CPU_CLOCK