@@ -451,10 +451,28 @@ void arch_bpf_stack_walk(bool (*consume_fn)(void *, u64, u64, u64), void *cookie
451451 }
452452}
453453
454+ static int bpf_jit_emit_func_call (u32 * image , struct codegen_context * ctx , u64 func_addr , int reg )
455+ {
456+ long reladdr ;
457+
458+ reladdr = func_addr - kernel_toc_addr ();
459+ if (reladdr > 0x7FFFFFFF || reladdr < - (0x80000000L )) {
460+ pr_err ("eBPF: address of %ps out of range of kernel_toc.\n" , (void * )func_addr );
461+ return - ERANGE ;
462+ }
463+
464+ EMIT (PPC_RAW_ADDIS (reg , _R2 , PPC_HA (reladdr )));
465+ EMIT (PPC_RAW_ADDI (reg , reg , PPC_LO (reladdr )));
466+ EMIT (PPC_RAW_MTCTR (reg ));
467+ EMIT (PPC_RAW_BCTRL ());
468+
469+ return 0 ;
470+ }
471+
454472int bpf_jit_emit_func_call_rel (u32 * image , u32 * fimage , struct codegen_context * ctx , u64 func )
455473{
456474 unsigned long func_addr = func ? ppc_function_entry ((void * )func ) : 0 ;
457- long reladdr ;
475+ int ret ;
458476
459477 /* bpf to bpf call, func is not known in the initial pass. Emit 5 nops as a placeholder */
460478 if (!func ) {
@@ -469,6 +487,7 @@ int bpf_jit_emit_func_call_rel(u32 *image, u32 *fimage, struct codegen_context *
469487 }
470488
471489#ifdef CONFIG_PPC_KERNEL_PCREL
490+ long reladdr ;
472491 reladdr = func_addr - local_paca -> kernelbase ;
473492
474493 /*
@@ -507,16 +526,9 @@ int bpf_jit_emit_func_call_rel(u32 *image, u32 *fimage, struct codegen_context *
507526 EMIT (PPC_RAW_BCTRL ());
508527#else
509528 if (core_kernel_text (func_addr )) {
510- reladdr = func_addr - kernel_toc_addr ();
511- if (reladdr > 0x7FFFFFFF || reladdr < - (0x80000000L )) {
512- pr_err ("eBPF: address of %ps out of range of kernel_toc.\n" , (void * )func );
513- return - ERANGE ;
514- }
515-
516- EMIT (PPC_RAW_ADDIS (_R12 , _R2 , PPC_HA (reladdr )));
517- EMIT (PPC_RAW_ADDI (_R12 , _R12 , PPC_LO (reladdr )));
518- EMIT (PPC_RAW_MTCTR (_R12 ));
519- EMIT (PPC_RAW_BCTRL ());
529+ ret = bpf_jit_emit_func_call (image , ctx , func_addr , _R12 );
530+ if (ret )
531+ return ret ;
520532 } else {
521533 if (IS_ENABLED (CONFIG_PPC64_ELF_ABI_V1 )) {
522534 /* func points to the function descriptor */
@@ -1755,6 +1767,31 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code
17551767 if (ret < 0 )
17561768 return ret ;
17571769
1770+ /*
1771+ * Call to arch_bpf_timed_may_goto() is emitted by the
1772+ * verifier and called with custom calling convention with
1773+ * first argument and return value in BPF_REG_AX (_R12).
1774+ *
1775+ * The generic helper or bpf function call emission path
1776+ * may use the same scratch register as BPF_REG_AX to
1777+ * materialize the target address. This would clobber AX
1778+ * and break timed may_goto semantics.
1779+ *
1780+ * Emit a minimal indirect call sequence here using a temp
1781+ * register and skip the normal post-call return-value move.
1782+ */
1783+
1784+ if (func_addr == (u64 )arch_bpf_timed_may_goto ) {
1785+ #ifdef CONFIG_PPC_KERNEL_PCREL
1786+ PPC_LI_ADDR (tmp1_reg , func_addr );
1787+ EMIT (PPC_RAW_MTCTR (tmp1_reg ));
1788+ EMIT (PPC_RAW_BCTRL ());
1789+ #else
1790+ bpf_jit_emit_func_call (image , ctx , func_addr , tmp1_reg );
1791+ #endif
1792+ break ;
1793+ }
1794+
17581795 /* Take care of powerpc ABI requirements before kfunc call */
17591796 if (insn [i ].src_reg == BPF_PSEUDO_KFUNC_CALL ) {
17601797 if (prepare_for_kfunc_call (fp , image , ctx , & insn [i ]))
0 commit comments