Skip to content

Commit 8907d51

Browse files
author
Fox Snowpatch
committed
1 parent 5c2c0ba commit 8907d51

4 files changed

Lines changed: 117 additions & 11 deletions

File tree

arch/powerpc/net/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@
33
# Arch-specific network modules
44
#
55
obj-$(CONFIG_BPF_JIT) += bpf_jit_comp.o bpf_jit_comp$(BITS).o
6+
7+
ifdef CONFIG_PPC64
8+
+obj-$(CONFIG_BPF_JIT) += bpf_timed_may_goto.o
9+
endif

arch/powerpc/net/bpf_jit_comp.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,11 @@ bool bpf_jit_supports_subprog_tailcalls(void)
537537
return IS_ENABLED(CONFIG_PPC64);
538538
}
539539

540+
bool bpf_jit_supports_timed_may_goto(void)
541+
{
542+
return IS_ENABLED(CONFIG_PPC64);
543+
}
544+
540545
bool bpf_jit_supports_kfunc_call(void)
541546
{
542547
return IS_ENABLED(CONFIG_PPC64);

arch/powerpc/net/bpf_jit_comp64.c

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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 = func_addr - kernel_toc_addr();
457+
458+
if (reladdr > 0x7FFFFFFF || reladdr < -(0x80000000L)) {
459+
pr_err("eBPF: address of %ps out of range of kernel_toc.\n", (void *)func_addr);
460+
return -ERANGE;
461+
}
462+
463+
EMIT(PPC_RAW_ADDIS(reg, _R2, PPC_HA(reladdr)));
464+
EMIT(PPC_RAW_ADDI(reg, reg, PPC_LO(reladdr)));
465+
EMIT(PPC_RAW_MTCTR(reg));
466+
EMIT(PPC_RAW_BCTRL());
467+
468+
return 0;
469+
}
470+
454471
int bpf_jit_emit_func_call_rel(u32 *image, u32 *fimage, struct codegen_context *ctx, u64 func)
455472
{
456473
unsigned long func_addr = func ? ppc_function_entry((void *)func) : 0;
457-
long reladdr;
474+
long __maybe_unused 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) {
@@ -507,16 +525,9 @@ int bpf_jit_emit_func_call_rel(u32 *image, u32 *fimage, struct codegen_context *
507525
EMIT(PPC_RAW_BCTRL());
508526
#else
509527
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());
528+
ret = bpf_jit_emit_func_call(image, ctx, func_addr, _R12);
529+
if (ret)
530+
return ret;
520531
} else {
521532
if (IS_ENABLED(CONFIG_PPC64_ELF_ABI_V1)) {
522533
/* func points to the function descriptor */
@@ -1755,6 +1766,35 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code
17551766
if (ret < 0)
17561767
return ret;
17571768

1769+
/*
1770+
* Call to arch_bpf_timed_may_goto() is emitted by the
1771+
* verifier and called with custom calling convention with
1772+
* first argument and return value in BPF_REG_AX (_R12).
1773+
*
1774+
* The generic helper or bpf function call emission path
1775+
* may use the same scratch register as BPF_REG_AX to
1776+
* materialize the target address. This would clobber AX
1777+
* and break timed may_goto semantics.
1778+
*
1779+
* Emit a minimal indirect call sequence here using a temp
1780+
* register and skip the normal post-call return-value move.
1781+
*/
1782+
1783+
if (func_addr == (u64)arch_bpf_timed_may_goto) {
1784+
ret = 0;
1785+
if (!IS_ENABLED(CONFIG_PPC_KERNEL_PCREL))
1786+
ret = bpf_jit_emit_func_call(image, ctx, func_addr,
1787+
tmp1_reg);
1788+
1789+
if (ret || IS_ENABLED(CONFIG_PPC_KERNEL_PCREL)) {
1790+
PPC_LI_ADDR(tmp1_reg, func_addr);
1791+
EMIT(PPC_RAW_MTCTR(tmp1_reg));
1792+
EMIT(PPC_RAW_BCTRL());
1793+
}
1794+
1795+
break;
1796+
}
1797+
17581798
/* Take care of powerpc ABI requirements before kfunc call */
17591799
if (insn[i].src_reg == BPF_PSEUDO_KFUNC_CALL) {
17601800
if (prepare_for_kfunc_call(fp, image, ctx, &insn[i]))
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/* Copyright (c) 2025 IBM Corporation, Saket Kumar Bhaskar <skb99@linux.ibm.com> */
3+
4+
#include <linux/linkage.h>
5+
#include <asm/ppc_asm.h>
6+
7+
/*
8+
* arch_bpf_timed_may_goto() trampoline for powerpc64
9+
*
10+
* Custom BPF convention (verifier/JIT):
11+
* - input: stack offset in BPF_REG_AX (r12)
12+
* - output: updated count in BPF_REG_AX (r12)
13+
*
14+
* Call bpf_check_timed_may_goto(ptr) with normal powerpc64 ABI:
15+
* - r3 = ptr, return in r3
16+
*
17+
* Preserve BPF regs R0-R5 (mapping: r8, r3-r7).
18+
*/
19+
20+
SYM_FUNC_START(arch_bpf_timed_may_goto)
21+
/* Prologue: save LR, allocate frame */
22+
mflr r0
23+
std r0, 16(r1)
24+
stdu r1, -112(r1)
25+
26+
/* Save BPF registers R0 - R5 (r8, r3-r7) */
27+
std r3, 32(r1)
28+
std r4, 40(r1)
29+
std r5, 48(r1)
30+
std r6, 56(r1)
31+
std r7, 64(r1)
32+
std r8, 72(r1)
33+
34+
/*
35+
* r3 = BPF_REG_FP + BPF_REG_AX
36+
* BPF_REG_FP is r31; BPF_REG_AX is r12 (stack offset in bytes).
37+
*/
38+
add r3, r31, r12
39+
bl bpf_check_timed_may_goto
40+
41+
/* Put return value back into AX */
42+
mr r12, r3
43+
44+
/* Restore BPF registers R0 - R5 (r8, r3-r7) */
45+
ld r3, 32(r1)
46+
ld r4, 40(r1)
47+
ld r5, 48(r1)
48+
ld r6, 56(r1)
49+
ld r7, 64(r1)
50+
ld r8, 72(r1)
51+
52+
/* Epilogue: pop frame, restore LR, return */
53+
addi r1, r1, 112
54+
ld r0, 16(r1)
55+
mtlr r0
56+
blr
57+
SYM_FUNC_END(arch_bpf_timed_may_goto)

0 commit comments

Comments
 (0)