Skip to content

Commit

Permalink
RISC-V: Check clint_time_val before use
Browse files Browse the repository at this point in the history
The NoMMU kernel is broken for QEMU virt machine from Linux-5.9-rc6
because clint_time_val is used even before CLINT driver is probed
at following places:
1. rand_initialize() calls get_cycles() which in-turn uses
   clint_time_val
2. boot_init_stack_canary() calls get_cycles() which in-turn
   uses clint_time_val

The issue#1 (above) is fixed by providing custom random_get_entropy()
for RISC-V NoMMU kernel. For issue#2 (above), we remove dependency of
boot_init_stack_canary() on get_cycles() and this is aligned with the
boot_init_stack_canary() implementations of ARM, ARM64 and MIPS kernel.

Fixes: d5be89a ("RISC-V: Resurrect the MMIO timer implementation for M-mode systems")
Signed-off-by: Anup Patel <anup.patel@wdc.com>
Tested-by: Damien Le Moal <damien.lemoal@wdc.com>
Signed-off-by: Palmer Dabbelt <palmerdabbelt@google.com>
  • Loading branch information
Anup Patel authored and Palmer Dabbelt committed Sep 30, 2020
1 parent c14decf commit aa98876
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
4 changes: 0 additions & 4 deletions arch/riscv/include/asm/stackprotector.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include <linux/random.h>
#include <linux/version.h>
#include <asm/timex.h>

extern unsigned long __stack_chk_guard;

Expand All @@ -18,12 +17,9 @@ extern unsigned long __stack_chk_guard;
static __always_inline void boot_init_stack_canary(void)
{
unsigned long canary;
unsigned long tsc;

/* Try to get a semi random initial value. */
get_random_bytes(&canary, sizeof(canary));
tsc = get_cycles();
canary += tsc + (tsc << BITS_PER_LONG/2);
canary ^= LINUX_VERSION_CODE;
canary &= CANARY_MASK;

Expand Down
13 changes: 13 additions & 0 deletions arch/riscv/include/asm/timex.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ static inline u32 get_cycles_hi(void)
#define get_cycles_hi get_cycles_hi
#endif /* CONFIG_64BIT */

/*
* Much like MIPS, we may not have a viable counter to use at an early point
* in the boot process. Unfortunately we don't have a fallback, so instead
* we just return 0.
*/
static inline unsigned long random_get_entropy(void)
{
if (unlikely(clint_time_val == NULL))
return 0;
return get_cycles();
}
#define random_get_entropy() random_get_entropy()

#else /* CONFIG_RISCV_M_MODE */

static inline cycles_t get_cycles(void)
Expand Down

0 comments on commit aa98876

Please sign in to comment.