[PATCH] RISC-V: Check clint_time_val before use

Anup Patel Anup.Patel at wdc.com
Sat Sep 26 05:31:57 EDT 2020



> -----Original Message-----
> From: Damien Le Moal <Damien.LeMoal at wdc.com>
> Sent: 26 September 2020 14:55
> To: paul.walmsley at sifive.com; palmer at dabbelt.com;
> palmerdabbelt at google.com; Anup Patel <Anup.Patel at wdc.com>;
> aou at eecs.berkeley.edu
> Cc: anup at brainfault.org; linux-riscv at lists.infradead.org; Atish Patra
> <Atish.Patra at wdc.com>; Alistair Francis <Alistair.Francis at wdc.com>; linux-
> kernel at vger.kernel.org
> Subject: Re: [PATCH] RISC-V: Check clint_time_val before use
> 
> On Sat, 2020-09-26 at 12:57 +0530, Anup Patel wrote:
> > The NoMMU kernel is broken for QEMU virt machine from Linux-5.9-rc6
> > because the get_cycles() and friends are called very early from
> > rand_initialize() before CLINT driver is probed. To fix this, we
> > should check clint_time_val before use in get_cycles() and friends.
> >
> > Fixes: d5be89a8d118 ("RISC-V: Resurrect the MMIO timer implementation
> > for M-mode systems")
> > Signed-off-by: Anup Patel <anup.patel at wdc.com>
> > ---
> >  arch/riscv/include/asm/timex.h | 12 +++++++++---
> >  1 file changed, 9 insertions(+), 3 deletions(-)
> >
> > diff --git a/arch/riscv/include/asm/timex.h
> > b/arch/riscv/include/asm/timex.h index 7f659dda0032..52b42bb1602c
> > 100644
> > --- a/arch/riscv/include/asm/timex.h
> > +++ b/arch/riscv/include/asm/timex.h
> > @@ -17,18 +17,24 @@ typedef unsigned long cycles_t;  #ifdef
> > CONFIG_64BIT  static inline cycles_t get_cycles(void)  {
> > -	return readq_relaxed(clint_time_val);
> > +	if (clint_time_val)
> > +		return readq_relaxed(clint_time_val);
> > +	return 0;
> >  }
> >  #else /* !CONFIG_64BIT */
> >  static inline u32 get_cycles(void)
> >  {
> > -	return readl_relaxed(((u32 *)clint_time_val));
> > +	if (clint_time_val)
> > +		return readl_relaxed(((u32 *)clint_time_val));
> > +	return 0;
> >  }
> >  #define get_cycles get_cycles
> >
> >  static inline u32 get_cycles_hi(void)  {
> > -	return readl_relaxed(((u32 *)clint_time_val) + 1);
> > +	if (clint_time_val)
> > +		return readl_relaxed(((u32 *)clint_time_val) + 1);
> > +	return 0
> >  }
> >  #define get_cycles_hi get_cycles_hi
> >  #endif /* CONFIG_64BIT */
> 
> Applying this on top of rc6, I now get a hang on Kendryte boot...
> No problems without the patch on the other hand.

Not sure about the issue with Kendryte but I get a crash on
QEMU virt machine without this patch.

Regards,
Anup


More information about the linux-riscv mailing list