Re: [PATCH bpf-next v2 2/3] riscv: implement a memset like function for text

Song Liu
2023-08-24T22:06:07Z

On Thu, Aug 24, 2023 at 6:31 AM Puranjay Mohan <puranjay12@gmail.com> wrote:
>
> The BPF JIT needs to write invalid instructions to RX regions of memory
> to invalidate removed BPF programs. This needs a function like memset()
> that can work with RX memory.
>
> Implement patch_text_set_nosync() which is similar to text_poke_set() of
> x86.
>
> Signed-off-by: Puranjay Mohan <puranjay12@gmail.com>
> ---
> arch/riscv/include/asm/patch.h | 1 +
> arch/riscv/kernel/patch.c | 74 ++++++++++++++++++++++++++++++++++
> 2 files changed, 75 insertions(+)
>
> diff --git a/arch/riscv/include/asm/patch.h b/arch/riscv/include/asm/patch.h
> index 63c98833d510..aa5c1830ea43 100644
> --- a/arch/riscv/include/asm/patch.h
> +++ b/arch/riscv/include/asm/patch.h
> @@ -7,6 +7,7 @@
> #define _ASM_RISCV_PATCH_H
>
> int patch_text_nosync(void *addr, const void *insns, size_t len);
> +int patch_text_set_nosync(void *addr, const int c, size_t len);
> int patch_text(void *addr, u32 *insns, int ninsns);
>
> extern int riscv_patch_in_stop_machine;
> diff --git a/arch/riscv/kernel/patch.c b/arch/riscv/kernel/patch.c
> index 465b2eebbc37..24d49999ac1a 100644
> --- a/arch/riscv/kernel/patch.c
> +++ b/arch/riscv/kernel/patch.c
> @@ -13,6 +13,7 @@
> #include <asm/fixmap.h>
> #include <asm/ftrace.h>
> #include <asm/patch.h>
> +#include <asm/string.h>
>
> struct patch_insn {
> void *addr;
> @@ -53,6 +54,34 @@ static void patch_unmap(int fixmap)
> }
> NOKPROBE_SYMBOL(patch_unmap);
>
> +static int __patch_insn_set(void *addr, const int c, size_t len)
> +{
> + void *waddr = addr;
> + bool across_pages = (((uintptr_t) addr & ~PAGE_MASK) + len) > PAGE_SIZE;
> + int ret;
> +
> + /*
> + * Only two pages can be mapped at a time for writing.
> + */
> + if (len > 2 * PAGE_SIZE)
> + return -EINVAL;

Same for this one. …