powerpc64/bpf: fix tail calls for PCREL addressing
Subsystems (4)
Hari Bathini
maintainerPATCH v4 1/2 powerpc64/bpf: fix tail calls for PCREL addressing
With PCREL addressing, there is no kernel TOC. So, it is not setup in prologue when PCREL addressing is used. But the number of instructions to skip on a tail call was not adjusted accordingly. That resulted in not so obvious failures while using tailcalls. 'tailcalls' selftest crashed the system with the below call trace:
bpf_test_run+0xe8/0x3cc (unreliable) bpf_prog_test_run_skb+0x348/0x778 __sys_bpf+0xb04/0x2b00 sys_bpf+0x28/0x38 system_call_exception+0x168/0x340 system_call_vectored_common+0x15c/0x2ec
Also, as bpf programs are always module addresses and a bpf helper in general is a core kernel text address, using PC relative addressing often fails with "out of range of pcrel address" error. Switch to using kernel base for relative addressing to handle this better.
---
* Changes in v4:
- Fix out of range errors by switching to kernelbase instead of PC for relative addressing.
* Changes in v3:
- New patch to fix tailcall issues with PCREL addressing.
arch/powerpc/net/bpf_jit_comp64.c | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-)
arch/powerpc/net/bpf_jit_comp64.c +16 -14
@@ -202,7 +202,8 @@ void bpf_jit_build_epilogue(u32 *image, struct codegen_context *ctx)EMIT (PPC_RAW_BLR ()); } -static int bpf_jit_emit_func_call_hlp (u32 *image,struct codegen_context *ctx,u64 func) +static int +bpf_jit_emit_func_call_hlp (u32 *image,u32 *fimage,struct codegen_context *ctx,u64 func) {unsigned long func_addr = func ?ppc_function_entry ((void *)func) :0 ;long reladdr;
@@ -211,19 +212,20 @@ static int bpf_jit_emit_func_call_hlp(u32 *image, struct codegen_context *ctx, ureturn -EINVAL ;if (IS_ENABLED (CONFIG_PPC_KERNEL_PCREL )) { - reladdr = func_addr -CTX_NIA (ctx); + reladdr = func_addr - local_paca->kernelbase;if (reladdr >= (long )SZ_8G || reladdr < -(long )SZ_8G ) { -pr_err ("eBPF: address of %ps out of range of pcrel address.\n" , - (void *)func); +pr_err ("eBPF: address of %ps out of range of 34-bit relative address.\n" , + (void *)func);return -ERANGE ; } -/* pla r12,addr */ -EMIT (PPC_PREFIX_MLS |__PPC_PRFX_R (1 ) |IMM_H18 (reladdr)); -EMIT (PPC_INST_PADDI |___PPC_RT (_R12) |IMM_L (reladdr)); -EMIT (PPC_RAW_MTCTR (_R12)); -EMIT (PPC_RAW_BCTR ()); - +EMIT (PPC_RAW_LD (_R12, _R13, offsetof(struct paca_struct , kernelbase))); +/* Align for subsequent prefix instruction */ +if (!IS_ALIGNED ((unsigned long )fimage +CTX_NIA (ctx),8 )) +EMIT (PPC_RAW_NOP ()); +/* paddi r12,r12,addr */ +EMIT (PPC_PREFIX_MLS |__PPC_PRFX_R (0 ) |IMM_H18 (reladdr)); +EMIT (PPC_INST_PADDI |___PPC_RT (_R12) |___PPC_RA (_R12) |IMM_L (reladdr)); }else { reladdr = func_addr -kernel_toc_addr ();if (reladdr >0x7FFFFFFF || reladdr < -(0x80000000L )) {
@@ -233,9 +235,9 @@ static int bpf_jit_emit_func_call_hlp(u32 *image, struct codegen_context *ctx, uEMIT (PPC_RAW_ADDIS (_R12, _R2,PPC_HA (reladdr)));EMIT (PPC_RAW_ADDI (_R12, _R12,PPC_LO (reladdr))); -EMIT (PPC_RAW_MTCTR (_R12)); -EMIT (PPC_RAW_BCTRL ()); } +EMIT (PPC_RAW_MTCTR (_R12)); +EMIT (PPC_RAW_BCTRL ());return 0 ; }
@@ -285,7 +287,7 @@ static int bpf_jit_emit_tail_call(u32 *image, struct codegen_context *ctx, u32 oint b2p_index =bpf_to_ppc (BPF_REG_3 );int bpf_tailcall_prologue_size =8 ; -if (IS_ENABLED (CONFIG_PPC64_ELF_ABI_V2 )) +if (!IS_ENABLED (CONFIG_PPC_KERNEL_PCREL ) &&IS_ENABLED (CONFIG_PPC64_ELF_ABI_V2 )) bpf_tailcall_prologue_size +=4 ;/* skip past the toc load */ /*
@@ -993,7 +995,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct codereturn ret;if (func_addr_fixed) - ret =bpf_jit_emit_func_call_hlp (image, ctx, func_addr); + ret =bpf_jit_emit_func_call_hlp (image, fimage, ctx, func_addr);else ret =bpf_jit_emit_func_call_rel (image, fimage, ctx, func_addr);
Signature · 1 line
2.44.0
Hari Bathini
maintainerPATCH v4 2/2 powerpc/bpf: enable kfunc call
Currently, bpf jit code on powerpc assumes all the bpf functions and helpers to be part of core kernel text. This is false for kfunc case, as function addresses may not be part of core kernel text area. So, add support for addresses that are not within core kernel text area too, to enable kfunc support. Emit instructions based on whether the function address is within core kernel text address or not, to retain optimized instruction sequence where possible.
In case of PCREL, as a bpf function that is not within core kernel text area is likely to go out of range with relative addressing on kernel base, use PC relative addressing. If that goes out of range, load the full address with PPC_LI64().
With addresses that are not within core kernel text area supported, override bpf_jit_supports_kfunc_call() to enable kfunc support. Also, override bpf_jit_supports_far_kfunc_call() to enable 64-bit pointers, as an address offset can be more than 32-bit long on PPC64.
---
* Changes in v4:
- Use either kernelbase or PC for relative addressing. Also, fallback to PPC_LI64(), if both are out of range.
- Update r2 with kernel TOC for elfv1 too as elfv1 also uses the optimization sequence, that expects r2 to be kernel TOC, when function address is within core kernel text.
* Changes in v3:
- Retained optimized instruction sequence when function address is a core kernel address as suggested by Naveen.
- Used unoptimized instruction sequence for PCREL addressing to avoid out of range errors for core kernel function addresses.
- Folded patch that adds support for kfunc calls with patch that enables/advertises this support as suggested by Naveen.
arch/powerpc/net/bpf_jit_comp.c | 10 +++++ arch/powerpc/net/bpf_jit_comp64.c | 61 ++++++++++++++++++++++++++----- 2 files changed, 61 insertions(+), 10 deletions(-)
arch/powerpc/net/bpf_jit_comp.c +10 -0
@@ -359,3 +359,13 @@ void bpf_jit_free(struct bpf_prog *fp)bpf_prog_unlock_free (fp); } + +bool bpf_jit_supports_kfunc_call (void ) +{ +return true; +} + +bool bpf_jit_supports_far_kfunc_call (void ) +{ +return IS_ENABLED (CONFIG_PPC64 ); +}
arch/powerpc/net/bpf_jit_comp64.c +51 -10
83 lines not on this page · raw message
Signature · 1 line
2.44.0
Re: [PATCH v4 2/2] powerpc/bpf: enable kfunc call
Quoted 64 lines · Hari Bathini
Currently, bpf jit code on powerpc assumes all the bpf functions and helpers to be part of core kernel text. This is false for kfunc case, as function addresses may not be part of core kernel text area. So, add support for addresses that are not within core kernel text area too, to enable kfunc support. Emit instructions based on whether the function address is within core kernel text address or not, to retain optimized instruction sequence where possible.
In case of PCREL, as a bpf function that is not within core kernel text area is likely to go out of range with relative addressing on kernel base, use PC relative addressing. If that goes out of range, load the full address with
PPC_LI64().With addresses that are not within core kernel text area supported, override
bpf_jit_supports_kfunc_call()to enable kfunc support. Also, overridebpf_jit_supports_far_kfunc_call()to enable 64-bit pointers, as an address offset can be more than 32-bit long on PPC64.Signed-off-by Hari Bathini <[email protected]>---
* Changes in v4:
- Use either kernelbase or PC for relative addressing. Also, fallback toPPC_LI64(), if both are out of range.
- Update r2 with kernel TOC for elfv1 too as elfv1 also uses the optimization sequence, that expects r2 to be kernel TOC, when function address is within core kernel text.* Changes in v3:
- Retained optimized instruction sequence when function address is a core kernel address as suggested by Naveen.
- Used unoptimized instruction sequence for PCREL addressing to avoid out of range errors for core kernel function addresses.
- Folded patch that adds support for kfunc calls with patch that enables/advertises this support as suggested by Naveen.arch/powerpc/net/bpf_jit_comp.c | 10 +++++ arch/powerpc/net/bpf_jit_comp64.c | 61 ++++++++++++++++++++++++++----- 2 files changed, 61 insertions(+), 10 deletions(-)arch/powerpc/net/bpf_jit_comp.c +10
-0@@ -359,3 +359,13 @@ void bpf_jit_free(struct bpf_prog *fp)bpf_prog_unlock_free (fp); } + +bool bpf_jit_supports_kfunc_call (void ) +{ +return true; +} + +bool bpf_jit_supports_far_kfunc_call (void ) +{ +return IS_ENABLED (CONFIG_PPC64 ); +}arch/powerpc/net/bpf_jit_comp64.c +0
-0
arch/powerpc/net/bpf_jit_comp64.c bpf_jit_emit_func_call_hlp(u32 *image, u32 *fimage, struct codegen_context *ctx, · line 214
Quoted 10 lines
arch/powerpc/net/bpf_jit_comp64.c +2
-3@@ -208,17 +208,13 @@ bpf_jit_emit_func_call_hlp(u32 *image, u32 *fimage, struct codegen_context *ctx,unsigned long func_addr = func ?ppc_function_entry ((void *)func) :0 ;long reladdr; -if (WARN_ON_ONCE (!core_kernel_text (func_addr))) +if (WARN_ON_ONCE (!kernel_text_address (func_addr)))return -EINVAL ; -if (IS_ENABLED (CONFIG_PPC_KERNEL_PCREL )) { - reladdr = func_addr - local_paca->kernelbase; +#ifdef CONFIG_PPC_KERNEL_PCREL
Would be good to retain use of IS_ENABLED().
- Naveen
Quoted 62 lines · Hari Bathini
With PCREL addressing, there is no kernel TOC. So, it is not setup in prologue when PCREL addressing is used. But the number of instructions to skip on a tail call was not adjusted accordingly. That resulted in not so obvious failures while using tailcalls. 'tailcalls' selftest crashed the system with the below call trace:
bpf_test_run+0xe8/0x3cc (unreliable) bpf_prog_test_run_skb+0x348/0x778 __sys_bpf+0xb04/0x2b00 sys_bpf+0x28/0x38 system_call_exception+0x168/0x340 system_call_vectored_common+0x15c/0x2ecAlso, as bpf programs are always module addresses and a bpf helper in general is a core kernel text address, using PC relative addressing often fails with "out of range of pcrel address" error. Switch to using kernel base for relative addressing to handle this better.
Fixes 7e3a68be42e1 ("powerpc/64: vmlinux support building with PCREL addresing")Signed-off-by Hari Bathini <[email protected]>---
* Changes in v4:
- Fix out of range errors by switching to kernelbase instead of PC for relative addressing.* Changes in v3:
- New patch to fix tailcall issues with PCREL addressing.arch/powerpc/net/bpf_jit_comp64.c | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-)arch/powerpc/net/bpf_jit_comp64.c +5
-4@@ -202,7 +202,8 @@ void bpf_jit_build_epilogue(u32 *image, struct codegen_context *ctx)EMIT (PPC_RAW_BLR ()); } -static int bpf_jit_emit_func_call_hlp (u32 *image,struct codegen_context *ctx,u64 func) +static int +bpf_jit_emit_func_call_hlp (u32 *image,u32 *fimage,struct codegen_context *ctx,u64 func) {unsigned long func_addr = func ?ppc_function_entry ((void *)func) :0 ;long reladdr;@@ -211,19 +212,20 @@ static int bpf_jit_emit_func_call_hlp(u32 *image, struct codegen_context *ctx, ureturn -EINVAL ;if (IS_ENABLED (CONFIG_PPC_KERNEL_PCREL )) { - reladdr = func_addr -CTX_NIA (ctx); + reladdr = func_addr - local_paca->kernelbase;if (reladdr >= (long )SZ_8G || reladdr < -(long )SZ_8G ) { -pr_err ("eBPF: address of %ps out of range of pcrel address.\n" , - (void *)func); +pr_err ("eBPF: address of %ps out of range of 34-bit relative address.\n" , + (void *)func);return -ERANGE ; }
arch/powerpc/net/bpf_jit_comp64.c static int bpf_jit_emit_func_call_hlp(u32 *image, struct codegen_context *ctx, u · line 225
Quoted 10 lines
arch/powerpc/net/bpf_jit_comp64.c +4
-6-/* pla r12,addr */ -EMIT (PPC_PREFIX_MLS |__PPC_PRFX_R (1 ) |IMM_H18 (reladdr)); -EMIT (PPC_INST_PADDI |___PPC_RT (_R12) |IMM_L (reladdr)); -EMIT (PPC_RAW_MTCTR (_R12)); -EMIT (PPC_RAW_BCTR ()); - +EMIT (PPC_RAW_LD (_R12, _R13, offsetof(struct paca_struct , kernelbase))); +/* Align for subsequent prefix instruction */ +if (!IS_ALIGNED ((unsigned long )fimage +CTX_NIA (ctx),8 )) +EMIT (PPC_RAW_NOP ());
We don't need the prefix instruction to be aligned to a doubleword boundary - it just shouldn't cross a 64-byte boundary. Since we know the exact address of the instruction here, we should be able to check for that case.
Quoted 7 lines
arch/powerpc/net/bpf_jit_comp64.c +3
-0+/* paddi r12,r12,addr */ +EMIT (PPC_PREFIX_MLS |__PPC_PRFX_R (0 ) |IMM_H18 (reladdr)); +EMIT (PPC_INST_PADDI |___PPC_RT (_R12) |___PPC_RA (_R12) |IMM_L (reladdr)); }else { reladdr = func_addr -kernel_toc_addr ();if (reladdr >0x7FFFFFFF || reladdr < -(0x80000000L )) {
arch/powerpc/net/bpf_jit_comp64.c static int bpf_jit_emit_func_call_hlp(u32 *image, struct codegen_context *ctx, u · line 240
Quoted 8 lines
arch/powerpc/net/bpf_jit_comp64.c +2
-2@@ -233,9 +235,9 @@ static int bpf_jit_emit_func_call_hlp(u32 *image, struct codegen_context *ctx, uEMIT (PPC_RAW_ADDIS (_R12, _R2,PPC_HA (reladdr)));EMIT (PPC_RAW_ADDI (_R12, _R12,PPC_LO (reladdr))); -EMIT (PPC_RAW_MTCTR (_R12)); -EMIT (PPC_RAW_BCTRL ()); } +EMIT (PPC_RAW_MTCTR (_R12)); +EMIT (PPC_RAW_BCTRL ());
This change shouldn't be necessary since these instructions are moved back into the conditional in the next patch.
Other than those minor comments:
- Naveen
Michael Ellerman
AppliedQuoted 6 lines · Hari Bathini
With PCREL addressing, there is no kernel TOC. So, it is not setup in prologue when PCREL addressing is used. But the number of instructions to skip on a tail call was not adjusted accordingly. That resulted in not so obvious failures while using tailcalls. 'tailcalls' selftest crashed the system with the below call trace:
Quoted 8 lines
bpf_test_run+0xe8/0x3cc (unreliable) bpf_prog_test_run_skb+0x348/0x778 __sys_bpf+0xb04/0x2b00 sys_bpf+0x28/0x38 system_call_exception+0x168/0x340 system_call_vectored_common+0x15c/0x2ec[...]
Applied to powerpc/next.
[1/2] powerpc64/bpf: fix tail calls for PCREL addressing
https://git.kernel.org/powerpc/c/2ecfe59cd7de1f202e9af2516a61fbbf93d0bd4d [2/2] powerpc/bpf: enable kfunc call https://git.kernel.org/powerpc/c/61688a82e047a4166436bf2665716cc070572ffa
cheers