[PATCH V2] riscv: asid: Fixup stale TLB entry cause application crash

Andrew Jones ajones at ventanamicro.com
Thu Nov 10 01:22:42 PST 2022


On Thu, Nov 10, 2022 at 09:51:03AM +0800, Guo Ren wrote:
> On Wed, Nov 9, 2022 at 5:45 PM Andrew Jones <ajones at ventanamicro.com> wrote:
> >
> > On Wed, Nov 09, 2022 at 12:40:56AM -0500, guoren at kernel.org wrote:
> > >
> > > -     cpumask_clear_cpu(cpu, mm_cpumask(prev));
> > > +#ifdef CONFIG_MMU
> > > +     if (!static_branch_unlikely(&use_asid_allocator))
> > > +#endif
> >
> > That's not very pretty. Can't we just do the following, instead?
> >
> > diff --git a/arch/riscv/mm/context.c b/arch/riscv/mm/context.c
> > index 7acbfbd14557..ace419761e31 100644
> > --- a/arch/riscv/mm/context.c
> > +++ b/arch/riscv/mm/context.c
> > @@ -16,10 +16,11 @@
> >  #include <asm/cacheflush.h>
> >  #include <asm/mmu_context.h>
> >
> > -#ifdef CONFIG_MMU
> >
> >  DEFINE_STATIC_KEY_FALSE(use_asid_allocator);
> Define use_asid_allocator in nommu part? How about:

Yeah, I was thinking it'll just always be a false static branch
in the nommu case, but I like your proposal below better.

Thanks,
drew

> 
> diff --git a/arch/riscv/mm/context.c b/arch/riscv/mm/context.c
> index 7acbfbd14557..ed3f8de7ef97 100644
> --- a/arch/riscv/mm/context.c
> +++ b/arch/riscv/mm/context.c
> @@ -205,12 +205,16 @@ static void set_mm_noasid(struct mm_struct *mm)
>         local_flush_tlb_all();
>  }
> 
> -static inline void set_mm(struct mm_struct *mm, unsigned int cpu)
> +static inline void set_mm(struct mm_struct *prev,
> +                         struct mm_struct *next, unsigned int cpu)
>  {
> -       if (static_branch_unlikely(&use_asid_allocator))
> -               set_mm_asid(mm, cpu);
> -       else
> -               set_mm_noasid(mm);
> +       cpumask_set_cpu(cpu, mm_cpumask(next));
> +       if (static_branch_unlikely(&use_asid_allocator)) {
> +               set_mm_asid(next, cpu);
> +       } else {
> +               cpumask_clear_cpu(cpu, mm_cpumask(prev));
> +               set_mm_noasid(next);
> +       }
>  }
> 
>  static int __init asids_init(void)
> @@ -264,7 +268,8 @@ static int __init asids_init(void)
>  }
>  early_initcall(asids_init);
>  #else
> -static inline void set_mm(struct mm_struct *mm, unsigned int cpu)
> +static inline void set_mm(struct mm_struct *prev,
> +                         struct mm_struct *next, unsigned int cpu)
>  {
>         /* Nothing to do here when there is no MMU */
>  }
> @@ -317,10 +322,7 @@ void switch_mm(struct mm_struct *prev, struct
> mm_struct *next,
>          */
>         cpu = smp_processor_id();
> 
> -       cpumask_clear_cpu(cpu, mm_cpumask(prev));
> -       cpumask_set_cpu(cpu, mm_cpumask(next));
> -
> -       set_mm(next, cpu);
> +       set_mm(prev, next, cpu);
> 
>         flush_icache_deferred(next, cpu);
> 
> >
> > +#ifdef CONFIG_MMU
> > +
> >  static unsigned long asid_bits;
> >  static unsigned long num_asids;
> >  static unsigned long asid_mask;
> >
> >
> > Thanks,
> > drew
> >
> > > +             cpumask_clear_cpu(cpu, mm_cpumask(prev));
> > > +
> > >       cpumask_set_cpu(cpu, mm_cpumask(next));
> > >
> > >       set_mm(next, cpu);
> > > --
> > > 2.36.1
> > >
> > >
> > > _______________________________________________
> > > linux-riscv mailing list
> > > linux-riscv at lists.infradead.org
> > > http://lists.infradead.org/mailman/listinfo/linux-riscv
> 
> 
> 
> -- 
> Best Regards
>  Guo Ren



More information about the linux-riscv mailing list