powerpc64/bpf: Support tailcalls with subprogs & BPF exceptions
Subsystems (4)
v3 cover powerpc64/bpf: Support tailcalls with subprogs & BPF exceptions
From: Abhishek Dubey <[email protected]>
This patch series enables support for two BPF JIT features on powerpc64. The first three patches target support for tail calls with subprogram combinations. The first patch supports realignment of tail_call_cnt offset in stack frame. Implementation details are provided in the commit messages.
The last three patches add support for BPF exceptions. An architecture-specific stack walker is implemented to assist with stack walk during exceptions.
All selftests related to tailcalls and exceptions are passing:
# ./test_progs -t tailcalls #442/1 tailcalls/tailcall_1:OK #442/2 tailcalls/tailcall_2:OK #442/3 tailcalls/tailcall_3:OK #442/4 tailcalls/tailcall_4:OK #442/5 tailcalls/tailcall_5:OK #442/6 tailcalls/tailcall_6:OK #442/7 tailcalls/tailcall_bpf2bpf_1:OK #442/8 tailcalls/tailcall_bpf2bpf_2:OK #442/9 tailcalls/tailcall_bpf2bpf_3:OK #442/10 tailcalls/tailcall_bpf2bpf_4:OK #442/11 tailcalls/tailcall_bpf2bpf_5:OK #442/12 tailcalls/tailcall_bpf2bpf_6:OK #442/13 tailcalls/tailcall_bpf2bpf_fentry:OK #442/14 tailcalls/tailcall_bpf2bpf_fexit:OK #442/15 tailcalls/tailcall_bpf2bpf_fentry_fexit:OK #442/16 tailcalls/tailcall_bpf2bpf_fentry_entry:OK #442/17 tailcalls/tailcall_poke:OK #442/18 tailcalls/tailcall_bpf2bpf_hierarchy_1:OK #442/19 tailcalls/tailcall_bpf2bpf_hierarchy_fentry:OK #442/20 tailcalls/tailcall_bpf2bpf_hierarchy_fexit:OK #442/21 tailcalls/tailcall_bpf2bpf_hierarchy_fentry_fexit:OK #442/22 tailcalls/tailcall_bpf2bpf_hierarchy_fentry_entry:OK #442/23 tailcalls/tailcall_bpf2bpf_hierarchy_2:OK #442/24 tailcalls/tailcall_bpf2bpf_hierarchy_3:OK #442/25 tailcalls/tailcall_freplace:OK #442/26 tailcalls/tailcall_bpf2bpf_freplace:OK #442/27 tailcalls/tailcall_failure:OK #442/28 tailcalls/reject_tail_call_spin_lock:OK #442/29 tailcalls/reject_tail_call_rcu_lock:OK #442/30 tailcalls/reject_tail_call_preempt_lock:OK #442/31 tailcalls/reject_tail_call_ref:OK #442 tailcalls:OK Summary: 1/31 PASSED, 0 SKIPPED, 0 FAILED
# ./test_progs -t exceptions #105/1 exceptions/exception_throw_always_1:OK #105/2 exceptions/exception_throw_always_2:OK #105/3 exceptions/exception_throw_unwind_1:OK #105/4 exceptions/exception_throw_unwind_2:OK #105/5 exceptions/exception_throw_default:OK #105/6 exceptions/exception_throw_default_value:OK #105/7 exceptions/exception_tail_call:OK #105/8 exceptions/exception_ext:OK #105/9 exceptions/exception_ext_mod_cb_runtime:OK #105/10 exceptions/exception_throw_subprog:OK #105/11 exceptions/exception_assert_nz_gfunc:OK #105/12 exceptions/exception_assert_zero_gfunc:OK #105/13 exceptions/exception_assert_neg_gfunc:OK #105/14 exceptions/exception_assert_pos_gfunc:OK #105/15 exceptions/exception_assert_negeq_gfunc:OK #105/16 exceptions/exception_assert_poseq_gfunc:OK #105/17 exceptions/exception_assert_nz_gfunc_with:OK #105/18 exceptions/exception_assert_zero_gfunc_with:OK #105/19 exceptions/exception_assert_neg_gfunc_with:OK #105/20 exceptions/exception_assert_pos_gfunc_with:OK #105/21 exceptions/exception_assert_negeq_gfunc_with:OK #105/22 exceptions/exception_assert_poseq_gfunc_with:OK #105/23 exceptions/exception_bad_assert_nz_gfunc:OK #105/24 exceptions/exception_bad_assert_zero_gfunc:OK #105/25 exceptions/exception_bad_assert_neg_gfunc:OK #105/26 exceptions/exception_bad_assert_pos_gfunc:OK #105/27 exceptions/exception_bad_assert_negeq_gfunc:OK #105/28 exceptions/exception_bad_assert_poseq_gfunc:OK #105/29 exceptions/exception_bad_assert_nz_gfunc_with:OK #105/30 exceptions/exception_bad_assert_zero_gfunc_with:OK #105/31 exceptions/exception_bad_assert_neg_gfunc_with:OK #105/32 exceptions/exception_bad_assert_pos_gfunc_with:OK #105/33 exceptions/exception_bad_assert_negeq_gfunc_with:OK #105/34 exceptions/exception_bad_assert_poseq_gfunc_with:OK #105/35 exceptions/exception_assert_range:OK #105/36 exceptions/exception_assert_range_with:OK #105/37 exceptions/exception_bad_assert_range:OK #105/38 exceptions/exception_bad_assert_range_with:OK #105/39 exceptions/non-throwing fentry -> exception_cb:OK #105/40 exceptions/throwing fentry -> exception_cb:OK #105/41 exceptions/non-throwing fexit -> exception_cb:OK #105/42 exceptions/throwing fexit -> exception_cb:OK #105/43 exceptions/throwing extension (with custom cb) -> exception_cb:OK #105/44 exceptions/throwing extension -> global func in exception_cb:OK #105/45 exceptions/exception_ext_mod_cb_runtime:OK #105/46 exceptions/throwing extension (with custom cb) -> global func in exception_cb:OK #105/47 exceptions/exception_ext:OK #105/48 exceptions/non-throwing fentry -> non-throwing subprog:OK #105/49 exceptions/throwing fentry -> non-throwing subprog:OK #105/50 exceptions/non-throwing fentry -> throwing subprog:OK #105/51 exceptions/throwing fentry -> throwing subprog:OK #105/52 exceptions/non-throwing fexit -> non-throwing subprog:OK #105/53 exceptions/throwing fexit -> non-throwing subprog:OK #105/54 exceptions/non-throwing fexit -> throwing subprog:OK #105/55 exceptions/throwing fexit -> throwing subprog:OK #105/56 exceptions/non-throwing fmod_ret -> non-throwing subprog:OK #105/57 exceptions/non-throwing fmod_ret -> non-throwing global subprog:OK #105/58 exceptions/non-throwing extension -> non-throwing subprog:OK #105/59 exceptions/non-throwing extension -> throwing subprog:OK #105/60 exceptions/non-throwing extension -> non-throwing subprog:OK #105/61 exceptions/non-throwing extension -> throwing global subprog:OK #105/62 exceptions/throwing extension -> throwing global subprog:OK #105/63 exceptions/throwing extension -> non-throwing global subprog:OK #105/64 exceptions/non-throwing extension -> main subprog:OK #105/65 exceptions/throwing extension -> main subprog:OK #105/66 exceptions/reject_exception_cb_type_1:OK #105/67 exceptions/reject_exception_cb_type_2:OK #105/68 exceptions/reject_exception_cb_type_3:OK #105/69 exceptions/reject_exception_cb_type_4:OK #105/70 exceptions/reject_async_callback_throw:OK #105/71 exceptions/reject_with_lock:OK #105/72 exceptions/reject_subprog_with_lock:OK #105/73 exceptions/reject_with_rcu_read_lock:OK #105/74 exceptions/reject_subprog_with_rcu_read_lock:OK #105/75 exceptions/reject_with_rbtree_add_throw:OK #105/76 exceptions/reject_with_reference:OK #105/77 exceptions/reject_with_cb_reference:OK #105/78 exceptions/reject_with_cb:OK #105/79 exceptions/reject_with_subprog_reference:OK #105/80 exceptions/reject_throwing_exception_cb:OK #105/81 exceptions/reject_exception_cb_call_global_func:OK #105/82 exceptions/reject_exception_cb_call_static_func:OK #105/83 exceptions/reject_multiple_exception_cb:OK #105/84 exceptions/reject_exception_throw_cb:OK #105/85 exceptions/reject_exception_throw_cb_diff:OK #105/86 exceptions/reject_set_exception_cb_bad_ret1:OK #105/87 exceptions/reject_set_exception_cb_bad_ret2:OK #105/88 exceptions/check_assert_eq_int_min:OK #105/89 exceptions/check_assert_eq_int_max:OK #105/90 exceptions/check_assert_eq_zero:OK #105/91 exceptions/check_assert_eq_llong_min:OK #105/92 exceptions/check_assert_eq_llong_max:OK #105/93 exceptions/check_assert_lt_pos:OK #105/94 exceptions/check_assert_lt_zero:OK #105/95 exceptions/check_assert_lt_neg:OK #105/96 exceptions/check_assert_le_pos:OK #105/97 exceptions/check_assert_le_zero:OK #105/98 exceptions/check_assert_le_neg:OK #105/99 exceptions/check_assert_gt_pos:OK #105/100 exceptions/check_assert_gt_zero:OK #105/101 exceptions/check_assert_gt_neg:OK #105/102 exceptions/check_assert_ge_pos:OK #105/103 exceptions/check_assert_ge_zero:OK #105/104 exceptions/check_assert_ge_neg:OK #105/105 exceptions/check_assert_range_s64:OK #105/106 exceptions/check_assert_range_u64:OK #105/107 exceptions/check_assert_single_range_s64:OK #105/108 exceptions/check_assert_single_range_u64:OK #105/109 exceptions/check_assert_generic:OK #105/110 exceptions/check_assert_with_return:OK #105 exceptions:OK Summary: 1/110 PASSED, 0 SKIPPED, 0 FAILED
[v1]: https://lore.kernel.org/all/[email protected]/
[v2]: https://lore.kernel.org/all/[email protected]/
Changes v1->v2:
Move tail_call_cnt to offset 0 in stack frame Remove trampoline NVR remapping-patch3/6[v1]
Changes v2->v3:
Added PPC_BCC_CONST_SHORT for short jumps of constant offset Optimize tailcall allocation for BPF_TRAMP_F_CALL_ORIG flag New helper for stack size calculation during exceptions Prologue JIT optimizations during non exception prog case
Abhishek Dubey (6):
powerpc64/bpf : Moving tail_call_cnt to bottom of frame powerpc64/bpf: Support tailcalls with subprogs powerpc64/bpf: Avoid tailcall restore from trampoline powerpc64/bpf: Add arch_bpf_stack_walk() for BPF JIT powerpc64/bpf: Support exceptions powerpc64/bpf: Additional NVR handling for bpf_throw
arch/powerpc/net/bpf_jit.h | 16 ++ arch/powerpc/net/bpf_jit_comp.c | 90 ++++++++-- arch/powerpc/net/bpf_jit_comp64.c | 262 ++++++++++++++++++++++++------ 3 files changed, 303 insertions(+), 65 deletions(-)
Signature · 1 line
2.48.1
PATCH v3 1/6 powerpc64/bpf : Moving tail_call_cnt to bottom of frame
From: Abhishek Dubey <[email protected]>
In the conventional stack frame, the position of tail_call_cnt is after the NVR save area (BPF_PPC_STACK_SAVE). Whereas, the offset of tail_call_cnt in the trampoline frame is after the stack alignment padding. BPF JIT logic could become complex when dealing with frame-sensitive offset calculation of tail_call_cnt. Having the same offset in both frames is the desired objective.
The trampoline frame does not have a BPF_PPC_STACK_SAVE area. Introducing it leads to under-utilization of extra memory meant only for the offset alignment of tail_call_cnt. Another challenge is the variable alignment padding sitting at the bottom of the trampoline frame, which requires additional handling to compute tail_call_cnt offset.
This patch addresses the above issues by moving tail_call_cnt to the bottom of the stack frame at offset 0 for both types of frames. This saves additional bytes required by BPF_PPC_STACK_SAVE in trampoline frame, and a common offset computation for tail_call_cnt serves both frames.
The changes in this patch are required by the second patch in the series, where the 'reference to tail_call_info' of the main frame is copied into the trampoline frame from the previous frame.
--- arch/powerpc/net/bpf_jit.h | 1 + arch/powerpc/net/bpf_jit_comp.c | 13 +++++++++++-- arch/powerpc/net/bpf_jit_comp64.c | 31 ++++++++++++++++++++----------- 3 files changed, 32 insertions(+), 13 deletions(-)
arch/powerpc/net/bpf_jit.h +1 -0
@@ -24,6 +24,7 @@#define SZL sizeof(unsigned long)#define BPF_INSN_SAFETY 64 +#define BPF_PPC_TAILCALL 8#define PLANT_INSTR (d, idx, instr) \ do { if (d) { (d)[idx] = instr; } idx++; } while (0)
arch/powerpc/net/bpf_jit_comp.c +11 -2
@@ -604,8 +604,8 @@ static void bpf_trampoline_setup_tail_call_cnt(u32 *image, struct codegen_contex int func_frame_offset, int r4_off) { if (IS_ENABLED(CONFIG_PPC64)) { - /* See bpf_jit_stack_tailcallcnt() */ - int tailcallcnt_offset = 7 * 8; + /* See Generated stack layout */ + int tailcallcnt_offset = BPF_PPC_TAILCALL;EMIT (PPC_RAW_LL (_R3, _R1, func_frame_offset - tailcallcnt_offset));EMIT (PPC_RAW_STL (_R3, _R1, -tailcallcnt_offset));
@@ -714,6 +714,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im* LR save area [ r0 save (64-bit) ] | header * [ r0 save (32-bit) ] | * dummy frame for unwind [ back chain 1 ] -- +* [ tail_call_cnt ] optional - 64-bit powerpc * [ padding ] align stack frame * r4_off [ r4 (tailcallcnt) ] optional - 32-bit powerpc * alt_lr_off [ real lr (ool stub)] optional - actual lr
@@ -795,6 +796,14 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im } } +/* +* Save tailcall count pointer at the same offset on the +* stack where subprogs expect it +*/ +if ((flags &BPF_TRAMP_F_CALL_ORIG ) && + (flags &BPF_TRAMP_F_TAIL_CALL_CTX )) + bpf_frame_size +=BPF_PPC_TAILCALL ; +/* Padding to align stack frame, if any */ bpf_frame_size =round_up (bpf_frame_size,SZL *2 );
arch/powerpc/net/bpf_jit_comp64.c +20 -11
@@ -20,13 +20,15 @@#include "bpf_jit.h" /* -* Stack layout: +* Stack layout with frame: +* Layout when setting up our own stack frame. +* Note: r1 at bottom, component offsets positive wrt r1. * Ensure the top half (upto local_tmp_var) stays consistent * with our redzone usage. * * [ prev sp ] <------------- -* [ nv gpr save area ] 6*8 | * [ tail_call_cnt ] 8 | +* [ nv gpr save area ] 6*8 | * [ local_tmp_var ] 24 | * fp (r31) --> [ ebpf stack space ] upto 512 | * [ frame header ] 32/112 |
@@ -36,10 +38,12 @@/* for gpr non volatile registers BPG_REG_6 to 10 */ #define BPF_PPC_STACK_SAVE (6*8)/* for bpf JIT code internal usage */ -#define BPF_PPC_STACK_LOCALS 32 +#define BPF_PPC_STACK_LOCALS 24/* stack frame excluding BPF stack, ensure this is quadword aligned */ #define BPF_PPC_STACKFRAME (STACK_FRAME_MIN_SIZE + \ - BPF_PPC_STACK_LOCALS + BPF_PPC_STACK_SAVE) + BPF_PPC_STACK_LOCALS + \ + BPF_PPC_STACK_SAVE + \ + BPF_PPC_TAILCALL)/* BPF register usage */ #define TMP_REG_1 (MAX_BPF_JIT_REG + 0)
@@ -87,27 +91,32 @@ static inline bool bpf_has_stack_frame(struct codegen_context *ctx) }/* +* Stack layout with redzone: * When not setting up our own stackframe, the redzone (288 bytes) usage is: +* Note: r1 from prev frame. Component offset negative wrt r1. * * [ prev sp ] <------------- * [ ... ] | * sp (r1) ---> [ stack pointer ] -------------- -* [ nv gpr save area ] 6*8 * [ tail_call_cnt ] 8 +* [ nv gpr save area ] 6*8 * [ local_tmp_var ] 24 * [ unused red zone ] 224 */ static int bpf_jit_stack_local (struct codegen_context *ctx) { -if (bpf_has_stack_frame (ctx)) +if (bpf_has_stack_frame (ctx)) { +/* Stack layout with frame */ return STACK_FRAME_MIN_SIZE + ctx->stack_size; -else -return -(BPF_PPC_STACK_SAVE +32 ); + }else { +/* Stack layout with redzone */ +return -(BPF_PPC_TAILCALL +BPF_PPC_STACK_SAVE +BPF_PPC_STACK_LOCALS ); + } }static int bpf_jit_stack_tailcallcnt (struct codegen_context *ctx) { -return bpf_jit_stack_local (ctx) +24 ; +return bpf_jit_stack_local (ctx) +BPF_PPC_STACK_LOCALS +BPF_PPC_STACK_SAVE ; }static int bpf_jit_stack_offsetof (struct codegen_context *ctx,int reg)
@@ -115,7 +124,7 @@ static int bpf_jit_stack_offsetof(struct codegen_context *ctx, int reg)if (reg >=BPF_PPC_NVR_MIN && reg <32 )return (bpf_has_stack_frame (ctx) ? (BPF_PPC_STACKFRAME + ctx->stack_size) :0 ) - - (8 * (32 - reg)); + - (8 * (32 - reg)) -BPF_PPC_TAILCALL ;pr_err ("BPF JIT is asking about unknown registers" );BUG ();
@@ -145,7 +154,7 @@ void bpf_jit_build_prologue(u32 *image, struct codegen_context *ctx)if (ctx->seen &SEEN_TAILCALL ) {EMIT (PPC_RAW_LI (bpf_to_ppc (TMP_REG_1 ),0 ));/* this goes in the redzone */ -EMIT (PPC_RAW_STD (bpf_to_ppc (TMP_REG_1 ), _R1, -(BPF_PPC_STACK_SAVE +8 ))); +EMIT (PPC_RAW_STD (bpf_to_ppc (TMP_REG_1 ), _R1, -(BPF_PPC_TAILCALL ))); }else {EMIT (PPC_RAW_NOP ());EMIT (PPC_RAW_NOP ());
Signature · 1 line
2.48.1
[email protected]
AI reviewRe: [PATCH v3 1/6] powerpc64/bpf : Moving tail_call_cnt to bottom of frame
Should bpf_trampoline_restore_tail_call_cnt() also be updated to use BPF_PPC_TAILCALL? That function still has:
Quoted 9 lines
arch/powerpc/net/bpf_jit.h +1
-0@@ -24,6 +24,7 @@#define SZL sizeof(unsigned long)#define BPF_INSN_SAFETY 64 +#define BPF_PPC_TAILCALL 8
[ ... ]
arch/powerpc/net/bpf_jit_comp.c static void bpf_trampoline_setup_tail_call_cnt(u32 *image, struct codegen_contex · line 608
Quoted 12 lines
arch/powerpc/net/bpf_jit_comp.c +2
-2@@ -604,8 +604,8 @@ static void bpf_trampoline_setup_tail_call_cnt(u32 *image, struct codegen_contex int func_frame_offset, int r4_off) { if (IS_ENABLED(CONFIG_PPC64)) { -/* See bpf_jit_stack_tailcallcnt() */ -int tailcallcnt_offset =7 *8 ; +/* See Generated stack layout */ +int tailcallcnt_offset =BPF_PPC_TAILCALL ;
Should bpf_trampoline_restore_tail_call_cnt() also be updated to use BPF_PPC_TAILCALL? That function still has:
int tailcallcnt_offset = 7 * 8;
The setup function stores tail_call_cnt at offset -8 (BPF_PPC_TAILCALL), but the restore function reads from offset -56 (7 * 8). This mismatch would cause the restore to read from and write to incorrect stack locations when BPF_TRAMP_F_TAIL_CALL_CTX is set.
I note that commit b18ae1c3dadf ("powerpc64/bpf: Avoid tailcall restore from trampoline") later in this series removes the PPC64 path from bpf_trampoline_restore_tail_call_cnt(), which would resolve this. However, intermediate commits between this patch and that fix could have broken tail call counting in trampolines if bisected to.
[ ... ]
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/21257691486
PATCH v3 2/6 powerpc64/bpf: Support tailcalls with subprogs
In reply to [email protected]From: Abhishek Dubey <[email protected]>
Enabling tailcalls with subprog combinations by referencing method. The actual tailcall count is always maintained in the tail_call_info variable present in the frame of main function (also called entry function). The tail_call_info field in the stack frame of subprogs contains reference to the tail_call_info field in the stack frame of main BPF program.
Dynamic resolution interprets the tail_call_info either as value or reference depending on the context of active frame while tailcall is invoked.
--- arch/powerpc/net/bpf_jit.h | 13 ++++++ arch/powerpc/net/bpf_jit_comp.c | 59 +++++++++++++++++++++++---- arch/powerpc/net/bpf_jit_comp64.c | 68 +++++++++++++++++++++++-------- 3 files changed, 117 insertions(+), 23 deletions(-)
arch/powerpc/net/bpf_jit.h +13 -0
@@ -52,6 +52,13 @@EMIT (PPC_INST_BRANCH_COND | (((cond) &0x3ff ) <<16 ) | (offset &0xfffc )); \ }while (0 ) +/* When constant jump offset is known prior */ +#define PPC_BCC_CONST_SHORT (cond, offset) \ + do { \ + BUILD_BUG_ON(offset < -0x8000 || offset > 0x7fff || (offset & 0x3)); \ + EMIT(PPC_INST_BRANCH_COND | (((cond) & 0x3ff) << 16) | (offset & 0xfffc)); \ + } while (0) +/* * Sign-extended 32-bit immediate load *
@@ -73,6 +80,10 @@ } } while (0)#ifdef CONFIG_PPC64 + +/* for gpr non volatile registers BPG_REG_6 to 10 */ +#define BPF_PPC_STACK_SAVE (6 * 8) +/* If dummy pass (!image), account for maximum possible instructions */ #define PPC_LI64 (d, i) do { \ if (!image) \
@@ -167,6 +178,7 @@ struct codegen_context {unsigned int alt_exit_addr;u64 arena_vm_start;u64 user_vm_start; +bool is_subprog; };#define bpf_to_ppc (r) (ctx->b2p[r])
@@ -206,6 +218,7 @@ int bpf_add_extable_entry(struct bpf_prog *fp, u32 *image, u32 *fimage, int pass struct codegen_context *ctx, int insn_idx, int jmp_off, int dst_reg, u32 code); +int bpf_jit_stack_tailcallinfo_offset (struct codegen_context *ctx);#endif #endif
arch/powerpc/net/bpf_jit_comp.c +52 -7
93 lines not on this page · raw message
arch/powerpc/net/bpf_jit_comp64.c +52 -16
118 lines not on this page · raw message
Signature · 1 line
2.48.1
PATCH v3 3/6 powerpc64/bpf: Avoid tailcall restore from trampoline
In reply to [email protected]From: Abhishek Dubey <[email protected]>
Back propagation of tailcall count is no longer needed for powerpc64 due to use of reference, which updates the tailcall count in the tail_call_info field in the frame of the main program only.
Back propagation is still required for 32-bit powerpc.
--- arch/powerpc/net/bpf_jit_comp.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-)
arch/powerpc/net/bpf_jit_comp.c +5 -10
@@ -662,16 +662,11 @@ static void bpf_trampoline_setup_tail_call_info(u32 *image, struct codegen_contestatic void bpf_trampoline_restore_tail_call_cnt (u32 *image,struct codegen_context *ctx,int func_frame_offset,int r4_off) { -if (IS_ENABLED (CONFIG_PPC64 )) { -/* See bpf_jit_stack_tailcallcnt() */ -int tailcallcnt_offset =7 *8 ; - -EMIT (PPC_RAW_LL (_R3, _R1, -tailcallcnt_offset)); -EMIT (PPC_RAW_STL (_R3, _R1, func_frame_offset - tailcallcnt_offset)); - }else { -/* See bpf_jit_stack_offsetof() and BPF_PPC_TC */ -EMIT (PPC_RAW_STL (_R4, _R1, r4_off)); - } +/* +* Restore tailcall for 32-bit ppc. +* See bpf_jit_stack_offsetof() and BPF_PPC_TC +*/ +EMIT (PPC_RAW_STL (_R4, _R1, r4_off)); } static void bpf_trampoline_save_args(u32 *image, struct codegen_context *ctx, int func_frame_offset,
Signature · 1 line
2.48.1
[email protected]
AI reviewRe: [PATCH v3 3/6] powerpc64/bpf: Avoid tailcall restore from trampoline
After this change, on PPC64, the function unconditionally emits EMIT(PPC_RAW_STL(_R4, _R1, r4_off)). However, r4_off is only assigned a value in __arch_prepare_bpf_trampoline() when…
Quoted 14 lines
arch/powerpc/net/bpf_jit_comp.c +0
-6@@ -662,16 +662,11 @@ static void bpf_trampoline_setup_tail_call_info(u32 *image, struct codegen_contestatic void bpf_trampoline_restore_tail_call_cnt (u32 *image,struct codegen_context *ctx,int func_frame_offset,int r4_off) { -if (IS_ENABLED (CONFIG_PPC64 )) { -/* See bpf_jit_stack_tailcallcnt() */ -int tailcallcnt_offset =7 *8 ; - -EMIT (PPC_RAW_LL (_R3, _R1, -tailcallcnt_offset)); -EMIT (PPC_RAW_STL (_R3, _R1, func_frame_offset - tailcallcnt_offset));
arch/powerpc/net/bpf_jit_comp.c static void bpf_trampoline_setup_tail_call_info(u32 *image, struct codegen_conte · line 670
Quoted 10 lines
arch/powerpc/net/bpf_jit_comp.c +5
-4- }else { -/* See bpf_jit_stack_offsetof() and BPF_PPC_TC */ -EMIT (PPC_RAW_STL (_R4, _R1, r4_off)); - } +/* +* Restore tailcall for 32-bit ppc. +* See bpf_jit_stack_offsetof() and BPF_PPC_TC +*/ +EMIT (PPC_RAW_STL (_R4, _R1, r4_off)); }
After this change, on PPC64, the function unconditionally emits EMIT(PPC_RAW_STL(_R4, _R1, r4_off)). However, r4_off is only assigned a value in __arch_prepare_bpf_trampoline() when IS_ENABLED(CONFIG_PPC32), and is initialized to 0 otherwise. Since __arch_prepare_bpf_trampoline() returns early with -EOPNOTSUPP for PPC32, only PPC64 executes this code, meaning r4_off will always be 0.
On PPC64, PPC_RAW_STL maps to PPC_RAW_STD. Will this emit a store of R4 to offset 0 from R1, which is the stack back chain location? The comment says "Restore tailcall for 32-bit ppc" but the code runs unconditionally on all architectures.
Looking at bpf_trampoline_setup_tail_call_info() in the same file, it properly handles the two architectures using if (IS_ENABLED(CONFIG_PPC64)). Should this restore function have a similar guard, or return early for PPC64 since the back propagation is no longer needed?
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/21257691486
PATCH v3 4/6 powerpc64/bpf: Add arch_bpf_stack_walk() for BPF JIT
In reply to [email protected]From: Abhishek Dubey <[email protected]>
This function is used by bpf_throw() to unwind the stack until frame of exception-boundary during BPF exception handling.
This function is necessary to support BPF exceptions on PowerPC.
--- arch/powerpc/net/bpf_jit_comp64.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+)
arch/powerpc/net/bpf_jit_comp64.c +28 -0
@@ -247,6 +247,34 @@ void bpf_jit_build_epilogue(u32 *image, struct codegen_context *ctx)bpf_jit_build_fentry_stubs (image, ctx); } +void arch_bpf_stack_walk (bool (*consume_fn)(void *,u64 ,u64 ,u64 ),void *cookie) +{ +// callback processing always in current context +unsigned long fp =current_stack_frame (); + +for (;;) { +unsigned long *frame = (unsigned long *) fp; +unsigned long ip; + +if (!validate_sp (fp, current)) +return ; + + ip = frame[STACK_FRAME_LR_SAVE ]; +if (!ip) +break ; + +/* +* consume_fn common code expects stack pointer(sp) in third +* argument. There is no sp in ppc64, rather pass frame +* pointer. +*/ +if (ip && !consume_fn (cookie, ip, fp, fp)) +break ; + + fp = frame[0 ]; + } +} +int bpf_jit_emit_func_call_rel (u32 *image,u32 *fimage,struct codegen_context *ctx,u64 func) {unsigned long func_addr = func ?ppc_function_entry ((void *)func) :0 ;
Signature · 1 line
2.48.1
PATCH v3 5/6 powerpc64/bpf: Support exceptions
In reply to [email protected]From: Abhishek Dubey <[email protected]>
The modified prologue/epilogue generation code now enables exception-callback to use the stack frame of the program marked as exception boundary, where callee saved registers are stored.
As per ppc64 ABIv2 documentation[1], r14-r31 are callee saved registers. BPF programs on ppc64 already saves r26-r31 registers. Saving the remaining set of callee saved registers(r14-r25) is handled in the next patch.
[1] https://ftp.rtems.org/pub/rtems/people/sebh/ABI64BitOpenPOWERv1.1_16July2015_pub.pdf
--- arch/powerpc/net/bpf_jit.h | 2 ++ arch/powerpc/net/bpf_jit_comp.c | 7 ++++ arch/powerpc/net/bpf_jit_comp64.c | 58 +++++++++++++++++++++---------- 3 files changed, 48 insertions(+), 19 deletions(-)
arch/powerpc/net/bpf_jit.h +2 -0
@@ -179,6 +179,8 @@ struct codegen_context {u64 arena_vm_start;u64 user_vm_start;bool is_subprog; +bool exception_boundary; +bool exception_cb; };#define bpf_to_ppc (r) (ctx->b2p[r])
arch/powerpc/net/bpf_jit_comp.c +7 -0
@@ -207,6 +207,8 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp) cgctx.arena_vm_start =bpf_arena_get_kern_vm_start (fp->aux->arena); cgctx.user_vm_start =bpf_arena_get_user_vm_start (fp->aux->arena); cgctx.is_subprog =bpf_is_subprog (fp); + cgctx.exception_boundary = fp->aux->exception_boundary; + cgctx.exception_cb = fp->aux->exception_cb;/* Scouting faux-generate pass 0 */ if (bpf_jit_build_body (fp,NULL ,NULL , &cgctx, addrs,0 , false)) {
@@ -436,6 +438,11 @@ void bpf_jit_free(struct bpf_prog *fp)bpf_prog_unlock_free (fp); } +bool bpf_jit_supports_exceptions (void ) +{ +return IS_ENABLED (CONFIG_PPC64 ); +} +bool bpf_jit_supports_subprog_tailcalls (void ) {return IS_ENABLED (CONFIG_PPC64 );
arch/powerpc/net/bpf_jit_comp64.c +39 -19
102 lines not on this page · raw message
Signature · 1 line
2.48.1
PATCH v3 6/6 powerpc64/bpf: Additional NVR handling for bpf_throw
In reply to [email protected]From: Abhishek Dubey <[email protected]>
The bpf_throw() function never returns, if it has clobbered any callee-saved register, those will remain clobbered. The prologue must take care of saving all callee-saved registers in the frame of exception boundary program. Later these additional non volatile registers R14-R25 along with other NVRs are restored back in the epilogue of exception callback.
To achieve above objective, the frame size is determined dynamically to accommodate additional non volatile registers in exception boundary's frame. For non-exception boundary program, the frame size remains optimal. The additional instructions to save & restore r14-r25 registers are emitted only during exception boundary and exception callback program respectively.
--- arch/powerpc/net/bpf_jit_comp64.c | 87 ++++++++++++++++++++++++++++--- 1 file changed, 80 insertions(+), 7 deletions(-)
arch/powerpc/net/bpf_jit_comp64.c +80 -7
@@ -32,21 +32,37 @@* * [ prev sp ] <------------- * [ tail_call_info ] 8 | -* [ nv gpr save area ] 6*8 | +* [ nv gpr save area ] 6*8 + (12*8) | * [ local_tmp_var ] 24 | * fp (r31) --> [ ebpf stack space ] upto 512 | * [ frame header ] 32/112 | * sp (r1) ---> [ stack pointer ] -------------- +* +* Additional (12*8) in 'nv gpr save area' only in case of +* exception boundary. */ /* for bpf JIT code internal usage */ #define BPF_PPC_STACK_LOCALS 24 +/* +* for additional non volatile registers(r14-r25) to be saved +* at exception boundary +*/ +#define BPF_PPC_EXC_STACK_SAVE (12*8) +/* stack frame excluding BPF stack, ensure this is quadword aligned */ #define BPF_PPC_STACKFRAME (STACK_FRAME_MIN_SIZE + \ BPF_PPC_STACK_LOCALS + \ BPF_PPC_STACK_SAVE + \ BPF_PPC_TAILCALL) +/* +* same as BPF_PPC_STACKFRAME with save area for additional +* non volatile registers saved at exception boundary. +* This is quad-word aligned. +*/ +#define BPF_PPC_EXC_STACKFRAME (BPF_PPC_STACKFRAME + BPF_PPC_EXC_STACK_SAVE) +/* BPF register usage */ #define TMP_REG_1 (MAX_BPF_JIT_REG + 0)#define TMP_REG_2 (MAX_BPF_JIT_REG + 1)
@@ -103,9 +119,12 @@ static inline bool bpf_has_stack_frame(struct codegen_context *ctx)* [ ... ] | * sp (r1) ---> [ stack pointer ] -------------- * [ tail_call_info ] 8 -* [ nv gpr save area ] 6*8 +* [ nv gpr save area ] 6*8 + (12*8) * [ local_tmp_var ] 24 * [ unused red zone ] 224 +* +* Additional (12*8) in 'nv gpr save area' only in case of +* exception boundary. */ static int bpf_jit_stack_local (struct codegen_context *ctx) {
@@ -114,7 +133,12 @@ static int bpf_jit_stack_local(struct codegen_context *ctx)return STACK_FRAME_MIN_SIZE + ctx->stack_size; }else {/* Stack layout with redzone */ -return -(BPF_PPC_TAILCALL +BPF_PPC_STACK_SAVE +BPF_PPC_STACK_LOCALS ); +return -(BPF_PPC_TAILCALL + +BPF_PPC_STACK_SAVE + +(ctx->exception_boundary || ctx->exception_cb ? +BPF_PPC_EXC_STACK_SAVE :0 ) + +BPF_PPC_STACK_LOCALS + ); } }
@@ -125,9 +149,19 @@ int bpf_jit_stack_tailcallinfo_offset(struct codegen_context *ctx)static int bpf_jit_stack_offsetof (struct codegen_context *ctx,int reg) { -if (reg >=BPF_PPC_NVR_MIN && reg <32 ) +int min_valid_nvreg =BPF_PPC_NVR_MIN ; +/* Default frame size for all cases except exception boundary */ +int frame_nvr_size =BPF_PPC_STACKFRAME ; + +/* Consider all nv regs for handling exceptions */ +if (ctx->exception_boundary || ctx->exception_cb) { + min_valid_nvreg = _R14; + frame_nvr_size =BPF_PPC_EXC_STACKFRAME ; + } + +if (reg >= min_valid_nvreg && reg <32 )return (bpf_has_stack_frame (ctx) ? - (BPF_PPC_STACKFRAME + ctx->stack_size) :0 ) + (frame_nvr_size + ctx->stack_size) :0 ) - (8 * (32 - reg)) -BPF_PPC_TAILCALL ;pr_err ("BPF JIT is asking about unknown registers" );
@@ -138,6 +172,17 @@ void bpf_jit_realloc_regs(struct codegen_context *ctx) { } +/* +* For exception boundary & exception_cb progs: +* return increased size to accommodate additional NVRs. +*/ +static int bpf_jit_stack_size (struct codegen_context *ctx) +{ +return ctx->exception_boundary || ctx->exception_cb ? +BPF_PPC_EXC_STACKFRAME : +BPF_PPC_STACKFRAME ; +} +void bpf_jit_build_prologue (u32 *image,struct codegen_context *ctx) {int i;
@@ -198,7 +243,19 @@ void bpf_jit_build_prologue(u32 *image, struct codegen_context *ctx)EMIT (PPC_RAW_STD (_R0, _R1,PPC_LR_STKOFF )); } -EMIT (PPC_RAW_STDU (_R1, _R1, -(BPF_PPC_STACKFRAME + ctx->stack_size))); +EMIT (PPC_RAW_STDU (_R1, _R1, + -(bpf_jit_stack_size (ctx) + ctx->stack_size))); + } + +/* +* Program acting as exception boundary pushes R14..R25 in addition to +* BPF callee-saved non volatile registers. Exception callback uses +* the boundary program's stack frame, recover additionally saved +* registers in epilogue of exception callback. +*/ +if (ctx->exception_boundary) { +for (i = _R14; i <= _R25; i++) +EMIT (PPC_RAW_STD (i, _R1,bpf_jit_stack_offsetof (ctx, i))); }if (!ctx->exception_cb) {
@@ -245,9 +302,25 @@ static void bpf_jit_emit_common_epilogue(u32 *image, struct codegen_context *ctxEMIT (PPC_RAW_LD (bpf_to_ppc (ARENA_VM_START ), _R1,bpf_jit_stack_offsetof (ctx,bpf_to_ppc (ARENA_VM_START )))); +if (ctx->exception_cb) { +/* +* Recover additionally saved non volatile registers from stack +* frame of exception boundary program. +*/ +for (i = _R14; i <= _R25; i++) +EMIT (PPC_RAW_LD (i, _R1,bpf_jit_stack_offsetof (ctx, i))); + +/* +* LR value from boundary-frame is received as second parameter +* in exception callback. +*/ +EMIT (PPC_RAW_MTLR (_R4)); + } +/* Tear down our stack frame */ if (bpf_has_stack_frame (ctx)) { -EMIT (PPC_RAW_ADDI (_R1, _R1,BPF_PPC_STACKFRAME + ctx->stack_size)); +EMIT (PPC_RAW_ADDI (_R1, _R1,bpf_jit_stack_size (ctx) + ctx->stack_size)); +if (ctx->seen &SEEN_FUNC || ctx->exception_cb) {EMIT (PPC_RAW_LD (_R0, _R1,PPC_LR_STKOFF ));EMIT (PPC_RAW_MTLR (_R0));
Signature · 1 line
2.48.1
[email protected]
AI reviewRe: [PATCH v3 6/6] powerpc64/bpf: Additional NVR handling for bpf_throw
This sets LR from R4 (the boundary's LR passed as second parameter).
Quoted 4 lines
arch/powerpc/net/bpf_jit_comp64.c +0
-0
[ ... ]
Quoted 7 lines
arch/powerpc/net/bpf_jit_comp64.c +3
-0@@ -245,9 +302,25 @@ static void bpf_jit_emit_common_epilogue(u32 *image, struct codegen_context *ctxEMIT (PPC_RAW_LD (bpf_to_ppc (ARENA_VM_START ), _R1,bpf_jit_stack_offsetof (ctx,bpf_to_ppc (ARENA_VM_START )))); +if (ctx->exception_cb) { +/* +* Recover additionally saved non volatile registers from stack
arch/powerpc/net/bpf_jit_comp64.c static void bpf_jit_emit_common_epilogue(u32 *image, struct codegen_context *ctx · line 317
Quoted 10 lines
arch/powerpc/net/bpf_jit_comp64.c +10
-0+* frame of exception boundary program. +*/ +for (i = _R14; i <= _R25; i++) +EMIT (PPC_RAW_LD (i, _R1,bpf_jit_stack_offsetof (ctx, i))); + +/* +* LR value from boundary-frame is received as second parameter +* in exception callback. +*/ +EMIT (PPC_RAW_MTLR (_R4));
^^^^^^^^^^^^^^^^^^^
This sets LR from R4 (the boundary's LR passed as second parameter).
arch/powerpc/net/bpf_jit_comp64.c static void bpf_jit_emit_common_epilogue(u32 *image, struct codegen_context *ctx · line 324
Quoted 8 lines
arch/powerpc/net/bpf_jit_comp64.c +4
-1+ } +/* Tear down our stack frame */ if (bpf_has_stack_frame (ctx)) { -EMIT (PPC_RAW_ADDI (_R1, _R1,BPF_PPC_STACKFRAME + ctx->stack_size)); +EMIT (PPC_RAW_ADDI (_R1, _R1,bpf_jit_stack_size (ctx) + ctx->stack_size)); +if (ctx->seen &SEEN_FUNC || ctx->exception_cb) {
^^^^^^^^^^^^^^^^^^^^^
arch/powerpc/net/bpf_jit_comp64.c static void bpf_jit_emit_common_epilogue(u32 *image, struct codegen_context *ctx · line 326
Quoted 2 lines
arch/powerpc/net/bpf_jit_comp64.c +0
-0EMIT (PPC_RAW_LD (_R0, _R1,PPC_LR_STKOFF ));EMIT (PPC_RAW_MTLR (_R0));
^^^^^^^^^^^^^^^^^^^
For exception_cb programs, bpf_has_stack_frame() returns true (the function explicitly checks ctx->exception_cb). The condition on the inner if statement also evaluates true because of "|| ctx->exception_cb".
This means for exception_cb programs, LR is first set correctly from R4, but then immediately overwritten by loading from the stack and issuing a second MTLR.
The comment above the EMIT(PPC_RAW_MTLR(_R4)) states that LR is received as the second parameter, suggesting R4 is the authoritative source. If exception_cb and exception_boundary have different stack_depth values, the stack-loaded LR value after the ADDI may point to an incorrect location.
Should the inner condition exclude exception_cb, perhaps something like:
if (ctx->seen & SEEN_FUNC && !ctx->exception_cb) {Or alternatively, should the EMIT(PPC_RAW_MTLR(_R4)) be moved after the stack teardown code to ensure it takes precedence?
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/21257691486