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)
commit577b85ec41b5560a81a4fb09d1ec7760bcacc49f
tree34f3e1ac3e1d4fcd6a95de2cdebee1794fc82526
parentf8735bf1fb208bc1b6b1ca818413c9e41944e813
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 <gilbsgilbs@gmail.com>
Signed-off-by: Gilbert Gilb's <gilbsgilbert@gmail.com>
arch/arch-riscv64.h