riscv64: get clock from `rdtime` instead of `rdcycle`
authorGilbert Gilb's <gilbsgilbert@gmail.com>
Sun, 22 Oct 2023 17:06:45 +0000 (19:06 +0200)
committerGilbert Gilb's <gilbsgilbert@gmail.com>
Sun, 22 Oct 2023 19:12:03 +0000 (21:12 +0200)
`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 <gilbsgilbs@gmail.com>
Signed-off-by: Gilbert Gilb's <gilbsgilbert@gmail.com>
arch/arch-riscv64.h

index 9b8fd001d7af9d8d2094059ce9e478aaef5647f1..8ac33fa31c15ac982fb041818667a7ce8ebaefa9 100644 (file)
@@ -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