Re: [PATCH -fixes] riscv: patch: Flush the icache right after patching to avoid illegal insns

From: Alexandre Ghiti
Date: Mon Jul 15 2024 - 04:12:44 EST


Hi Geert,

On Tue, Jul 9, 2024 at 10:13 AM Geert Uytterhoeven <geert@xxxxxxxxxxxxxx> wrote:
>
> Hi Alexandre,
>
> On Mon, Jun 24, 2024 at 10:23 AM Alexandre Ghiti <alexghiti@xxxxxxxxxxxx> wrote:
> > We cannot delay the icache flush after patching some functions as we may
> > have patched a function that will get called before the icache flush.
> >
> > The only way to completely avoid such scenario is by flushing the icache
> > as soon as we patch a function. This will probably be costly as we don't
> > batch the icache maintenance anymore.
> >
> > Fixes: 6ca445d8af0e ("riscv: Fix early ftrace nop patching")
> > Reported-by: Conor Dooley <conor.dooley@xxxxxxxxxxxxx>
> > Closes: https://lore.kernel.org/linux-riscv/20240613-lubricant-breath-061192a9489a@wendy/
> > Signed-off-by: Alexandre Ghiti <alexghiti@xxxxxxxxxxxx>
>
> Thanks for your patch, which is now commit edf2d546bfd6f5c4 ("riscv:
> patch: Flush the icache right after patching to avoid illegal
> insns") in v6.10-rc6.
>
> > --- a/arch/riscv/kernel/patch.c
> > +++ b/arch/riscv/kernel/patch.c
> > @@ -189,9 +205,6 @@ int patch_text_set_nosync(void *addr, u8 c, size_t len)
> >
> > ret = patch_insn_set(tp, c, len);
> >
> > - if (!ret)
> > - flush_icache_range((uintptr_t)tp, (uintptr_t)tp + len);
> > -
> > return ret;
> > }
> > NOKPROBE_SYMBOL(patch_text_set_nosync);
>
> patch_text_set_nosync() is now identical to (static) patch_insn_set(),
> and the latter has no other callers.
>
> > @@ -224,9 +237,6 @@ int patch_text_nosync(void *addr, const void *insns, size_t len)
> >
> > ret = patch_insn_write(tp, insns, len);
> >
> > - if (!ret)
> > - flush_icache_range((uintptr_t) tp, (uintptr_t) tp + len);
> > -
> > return ret;
> > }
> > NOKPROBE_SYMBOL(patch_text_nosync);
>
> patch_text_nosync() is now identical to patch_insn_write(), and both
> functions are called from outside this file.
>

I'll do something about that, thanks.

Alex

> Gr{oetje,eeting}s,
>
> Geert
>
> --
> Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@xxxxxxxxxxxxxx
>
> In personal conversations with technical people, I call myself a hacker. But
> when I'm talking to journalists I just say "programmer" or something like that.
> -- Linus Torvalds