Re: [PATCH v3 08/13] riscv: extend execmem_params for generated code allocations

2023-09-23 Thread Mike Rapoport
On Fri, Sep 22, 2023 at 12:37:07PM +0200, Alexandre Ghiti wrote:
> Hi Mike,
> 
> On 18/09/2023 09:29, Mike Rapoport wrote:
> > From: "Mike Rapoport (IBM)" 
> > 
> > The memory allocations for kprobes and BPF on RISC-V are not placed in
> > the modules area and these custom allocations are implemented with
> > overrides of alloc_insn_page() and  bpf_jit_alloc_exec().
> > 
> > Slightly reorder execmem_params initialization to support both 32 and 64
> > bit variants, define EXECMEM_KPROBES and EXECMEM_BPF ranges in
> > riscv::execmem_params and drop overrides of alloc_insn_page() and
> > bpf_jit_alloc_exec().
> > 
> > Signed-off-by: Mike Rapoport (IBM) 
> > ---
> >   arch/riscv/kernel/module.c | 21 -
> >   arch/riscv/kernel/probes/kprobes.c | 10 --
> >   arch/riscv/net/bpf_jit_core.c  | 13 -
> >   3 files changed, 20 insertions(+), 24 deletions(-)
> > 
> > diff --git a/arch/riscv/kernel/module.c b/arch/riscv/kernel/module.c
> > index 343a0edfb6dd..31505ecb5c72 100644
> > --- a/arch/riscv/kernel/module.c
> > +++ b/arch/riscv/kernel/module.c
> > @@ -436,20 +436,39 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char 
> > *strtab,
> > return 0;
> >   }
> > -#if defined(CONFIG_MMU) && defined(CONFIG_64BIT)
> > +#ifdef CONFIG_MMU
> >   static struct execmem_params execmem_params __ro_after_init = {
> > .ranges = {
> > [EXECMEM_DEFAULT] = {
> > .pgprot = PAGE_KERNEL,
> > .alignment = 1,
> > },
> > +   [EXECMEM_KPROBES] = {
> > +   .pgprot = PAGE_KERNEL_READ_EXEC,
> > +   .alignment = 1,
> > +   },
> > +   [EXECMEM_BPF] = {
> > +   .pgprot = PAGE_KERNEL,
> > +   .alignment = 1,
> 
> 
> Not entirely sure it is the same alignment (sorry did not go through the
> entire series), but if it is, the alignment above ^ is not the same that is
> requested by our current bpf_jit_alloc_exec() implementation which is
> PAGE_SIZE.
 
This literally translates vmalloc() in alloc_insn_page() to a set of
parameters, so "1" comes from there. And using alignment of 1 with
vmalloc() implicitly sets it to PAGE_SIZE.

> > +   },
> > },
> >   };
> >   struct execmem_params __init *execmem_arch_params(void)
> >   {
> > +#ifdef CONFIG_64BIT
> > execmem_params.ranges[EXECMEM_DEFAULT].start = MODULES_VADDR;
> > execmem_params.ranges[EXECMEM_DEFAULT].end = MODULES_END;
> > +#else
> > +   execmem_params.ranges[EXECMEM_DEFAULT].start = VMALLOC_START;
> > +   execmem_params.ranges[EXECMEM_DEFAULT].end = VMALLOC_END;
> > +#endif
> > +
> > +   execmem_params.ranges[EXECMEM_KPROBES].start = VMALLOC_START;
> > +   execmem_params.ranges[EXECMEM_KPROBES].end = VMALLOC_END;
> > +
> > +   execmem_params.ranges[EXECMEM_BPF].start = BPF_JIT_REGION_START;
> > +   execmem_params.ranges[EXECMEM_BPF].end = BPF_JIT_REGION_END;
> > return _params;
> >   }
> > diff --git a/arch/riscv/kernel/probes/kprobes.c 
> > b/arch/riscv/kernel/probes/kprobes.c
> > index 2f08c14a933d..e64f2f3064eb 100644
> > --- a/arch/riscv/kernel/probes/kprobes.c
> > +++ b/arch/riscv/kernel/probes/kprobes.c
> > @@ -104,16 +104,6 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p)
> > return 0;
> >   }
> > -#ifdef CONFIG_MMU
> > -void *alloc_insn_page(void)
> > -{
> > -   return  __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START, VMALLOC_END,
> > -GFP_KERNEL, PAGE_KERNEL_READ_EXEC,
> > -VM_FLUSH_RESET_PERMS, NUMA_NO_NODE,
> > -__builtin_return_address(0));
> > -}
> > -#endif
> > -
> >   /* install breakpoint in text */
> >   void __kprobes arch_arm_kprobe(struct kprobe *p)
> >   {
> > diff --git a/arch/riscv/net/bpf_jit_core.c b/arch/riscv/net/bpf_jit_core.c
> > index 7b70ccb7fec3..c8a758f0882b 100644
> > --- a/arch/riscv/net/bpf_jit_core.c
> > +++ b/arch/riscv/net/bpf_jit_core.c
> > @@ -218,19 +218,6 @@ u64 bpf_jit_alloc_exec_limit(void)
> > return BPF_JIT_REGION_SIZE;
> >   }
> > -void *bpf_jit_alloc_exec(unsigned long size)
> > -{
> > -   return __vmalloc_node_range(size, PAGE_SIZE, BPF_JIT_REGION_START,
> > -   BPF_JIT_REGION_END, GFP_KERNEL,
> > -   PAGE_KERNEL, 0, NUMA_NO_NODE,
> > -   __builtin_return_address(0));
> > -}
> > -
> > -void bpf_jit_free_exec(void *addr)
> > -{
> > -   return vfree(addr);
> > -}
> > -
> >   void *bpf_arch_text_copy(void *dst, void *src, size_t len)
> >   {
> > int ret;
> 
> 
> Otherwise, you can add:
> 
> Reviewed-by: Alexandre Ghiti 
> 
> Thanks,
> 
> Alex
> 
> 

-- 
Sincerely yours,
Mike.



Re: [PATCH v3 08/13] riscv: extend execmem_params for generated code allocations

2023-09-22 Thread Alexandre Ghiti

Hi Mike,

On 18/09/2023 09:29, Mike Rapoport wrote:

From: "Mike Rapoport (IBM)" 

The memory allocations for kprobes and BPF on RISC-V are not placed in
the modules area and these custom allocations are implemented with
overrides of alloc_insn_page() and  bpf_jit_alloc_exec().

Slightly reorder execmem_params initialization to support both 32 and 64
bit variants, define EXECMEM_KPROBES and EXECMEM_BPF ranges in
riscv::execmem_params and drop overrides of alloc_insn_page() and
bpf_jit_alloc_exec().

Signed-off-by: Mike Rapoport (IBM) 
---
  arch/riscv/kernel/module.c | 21 -
  arch/riscv/kernel/probes/kprobes.c | 10 --
  arch/riscv/net/bpf_jit_core.c  | 13 -
  3 files changed, 20 insertions(+), 24 deletions(-)

diff --git a/arch/riscv/kernel/module.c b/arch/riscv/kernel/module.c
index 343a0edfb6dd..31505ecb5c72 100644
--- a/arch/riscv/kernel/module.c
+++ b/arch/riscv/kernel/module.c
@@ -436,20 +436,39 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char 
*strtab,
return 0;
  }
  
-#if defined(CONFIG_MMU) && defined(CONFIG_64BIT)

+#ifdef CONFIG_MMU
  static struct execmem_params execmem_params __ro_after_init = {
.ranges = {
[EXECMEM_DEFAULT] = {
.pgprot = PAGE_KERNEL,
.alignment = 1,
},
+   [EXECMEM_KPROBES] = {
+   .pgprot = PAGE_KERNEL_READ_EXEC,
+   .alignment = 1,
+   },
+   [EXECMEM_BPF] = {
+   .pgprot = PAGE_KERNEL,
+   .alignment = 1,



Not entirely sure it is the same alignment (sorry did not go through the 
entire series), but if it is, the alignment above ^ is not the same that 
is requested by our current bpf_jit_alloc_exec() implementation which is 
PAGE_SIZE.




+   },
},
  };
  
  struct execmem_params __init *execmem_arch_params(void)

  {
+#ifdef CONFIG_64BIT
execmem_params.ranges[EXECMEM_DEFAULT].start = MODULES_VADDR;
execmem_params.ranges[EXECMEM_DEFAULT].end = MODULES_END;
+#else
+   execmem_params.ranges[EXECMEM_DEFAULT].start = VMALLOC_START;
+   execmem_params.ranges[EXECMEM_DEFAULT].end = VMALLOC_END;
+#endif
+
+   execmem_params.ranges[EXECMEM_KPROBES].start = VMALLOC_START;
+   execmem_params.ranges[EXECMEM_KPROBES].end = VMALLOC_END;
+
+   execmem_params.ranges[EXECMEM_BPF].start = BPF_JIT_REGION_START;
+   execmem_params.ranges[EXECMEM_BPF].end = BPF_JIT_REGION_END;
  
  	return _params;

  }
diff --git a/arch/riscv/kernel/probes/kprobes.c 
b/arch/riscv/kernel/probes/kprobes.c
index 2f08c14a933d..e64f2f3064eb 100644
--- a/arch/riscv/kernel/probes/kprobes.c
+++ b/arch/riscv/kernel/probes/kprobes.c
@@ -104,16 +104,6 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p)
return 0;
  }
  
-#ifdef CONFIG_MMU

-void *alloc_insn_page(void)
-{
-   return  __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START, VMALLOC_END,
-GFP_KERNEL, PAGE_KERNEL_READ_EXEC,
-VM_FLUSH_RESET_PERMS, NUMA_NO_NODE,
-__builtin_return_address(0));
-}
-#endif
-
  /* install breakpoint in text */
  void __kprobes arch_arm_kprobe(struct kprobe *p)
  {
diff --git a/arch/riscv/net/bpf_jit_core.c b/arch/riscv/net/bpf_jit_core.c
index 7b70ccb7fec3..c8a758f0882b 100644
--- a/arch/riscv/net/bpf_jit_core.c
+++ b/arch/riscv/net/bpf_jit_core.c
@@ -218,19 +218,6 @@ u64 bpf_jit_alloc_exec_limit(void)
return BPF_JIT_REGION_SIZE;
  }
  
-void *bpf_jit_alloc_exec(unsigned long size)

-{
-   return __vmalloc_node_range(size, PAGE_SIZE, BPF_JIT_REGION_START,
-   BPF_JIT_REGION_END, GFP_KERNEL,
-   PAGE_KERNEL, 0, NUMA_NO_NODE,
-   __builtin_return_address(0));
-}
-
-void bpf_jit_free_exec(void *addr)
-{
-   return vfree(addr);
-}
-
  void *bpf_arch_text_copy(void *dst, void *src, size_t len)
  {
int ret;



Otherwise, you can add:

Reviewed-by: Alexandre Ghiti 

Thanks,

Alex




[PATCH v3 08/13] riscv: extend execmem_params for generated code allocations

2023-09-18 Thread Mike Rapoport
From: "Mike Rapoport (IBM)" 

The memory allocations for kprobes and BPF on RISC-V are not placed in
the modules area and these custom allocations are implemented with
overrides of alloc_insn_page() and  bpf_jit_alloc_exec().

Slightly reorder execmem_params initialization to support both 32 and 64
bit variants, define EXECMEM_KPROBES and EXECMEM_BPF ranges in
riscv::execmem_params and drop overrides of alloc_insn_page() and
bpf_jit_alloc_exec().

Signed-off-by: Mike Rapoport (IBM) 
---
 arch/riscv/kernel/module.c | 21 -
 arch/riscv/kernel/probes/kprobes.c | 10 --
 arch/riscv/net/bpf_jit_core.c  | 13 -
 3 files changed, 20 insertions(+), 24 deletions(-)

diff --git a/arch/riscv/kernel/module.c b/arch/riscv/kernel/module.c
index 343a0edfb6dd..31505ecb5c72 100644
--- a/arch/riscv/kernel/module.c
+++ b/arch/riscv/kernel/module.c
@@ -436,20 +436,39 @@ int apply_relocate_add(Elf_Shdr *sechdrs, const char 
*strtab,
return 0;
 }
 
-#if defined(CONFIG_MMU) && defined(CONFIG_64BIT)
+#ifdef CONFIG_MMU
 static struct execmem_params execmem_params __ro_after_init = {
.ranges = {
[EXECMEM_DEFAULT] = {
.pgprot = PAGE_KERNEL,
.alignment = 1,
},
+   [EXECMEM_KPROBES] = {
+   .pgprot = PAGE_KERNEL_READ_EXEC,
+   .alignment = 1,
+   },
+   [EXECMEM_BPF] = {
+   .pgprot = PAGE_KERNEL,
+   .alignment = 1,
+   },
},
 };
 
 struct execmem_params __init *execmem_arch_params(void)
 {
+#ifdef CONFIG_64BIT
execmem_params.ranges[EXECMEM_DEFAULT].start = MODULES_VADDR;
execmem_params.ranges[EXECMEM_DEFAULT].end = MODULES_END;
+#else
+   execmem_params.ranges[EXECMEM_DEFAULT].start = VMALLOC_START;
+   execmem_params.ranges[EXECMEM_DEFAULT].end = VMALLOC_END;
+#endif
+
+   execmem_params.ranges[EXECMEM_KPROBES].start = VMALLOC_START;
+   execmem_params.ranges[EXECMEM_KPROBES].end = VMALLOC_END;
+
+   execmem_params.ranges[EXECMEM_BPF].start = BPF_JIT_REGION_START;
+   execmem_params.ranges[EXECMEM_BPF].end = BPF_JIT_REGION_END;
 
return _params;
 }
diff --git a/arch/riscv/kernel/probes/kprobes.c 
b/arch/riscv/kernel/probes/kprobes.c
index 2f08c14a933d..e64f2f3064eb 100644
--- a/arch/riscv/kernel/probes/kprobes.c
+++ b/arch/riscv/kernel/probes/kprobes.c
@@ -104,16 +104,6 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p)
return 0;
 }
 
-#ifdef CONFIG_MMU
-void *alloc_insn_page(void)
-{
-   return  __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START, VMALLOC_END,
-GFP_KERNEL, PAGE_KERNEL_READ_EXEC,
-VM_FLUSH_RESET_PERMS, NUMA_NO_NODE,
-__builtin_return_address(0));
-}
-#endif
-
 /* install breakpoint in text */
 void __kprobes arch_arm_kprobe(struct kprobe *p)
 {
diff --git a/arch/riscv/net/bpf_jit_core.c b/arch/riscv/net/bpf_jit_core.c
index 7b70ccb7fec3..c8a758f0882b 100644
--- a/arch/riscv/net/bpf_jit_core.c
+++ b/arch/riscv/net/bpf_jit_core.c
@@ -218,19 +218,6 @@ u64 bpf_jit_alloc_exec_limit(void)
return BPF_JIT_REGION_SIZE;
 }
 
-void *bpf_jit_alloc_exec(unsigned long size)
-{
-   return __vmalloc_node_range(size, PAGE_SIZE, BPF_JIT_REGION_START,
-   BPF_JIT_REGION_END, GFP_KERNEL,
-   PAGE_KERNEL, 0, NUMA_NO_NODE,
-   __builtin_return_address(0));
-}
-
-void bpf_jit_free_exec(void *addr)
-{
-   return vfree(addr);
-}
-
 void *bpf_arch_text_copy(void *dst, void *src, size_t len)
 {
int ret;
-- 
2.39.2