Skip to content

Commit 3727d6e

Browse files
hbathinimaddy-kerneldev
authored andcommitted
powerpc64/bpf: use consistent tailcall offset in trampoline
Ideally, the offset used to load the tail call info field and to find the pass by reference address for tail call field should be the same. But while setting up the tail call info in the trampoline, this was not followed. This can be misleading and can lead to unpredictable results if and when bpf_has_stack_frame() ends up returning true for trampoline frame. Since commit 15513be ("powerpc64/bpf: Moving tail_call_cnt to bottom of frame") and commit 2ed2d8f ("powerpc64/bpf: Support tailcalls with subprogs") ensured tail call field is at the bottom of the stack frame for BPF programs as well as BPF trampoline, avoid relying on bpf_jit_stack_tailcallinfo_offset() and bpf_has_stack_frame() for trampoline frame and always calculate tail call field offset with reference to older frame. Fixes: 2ed2d8f ("powerpc64/bpf: Support tailcalls with subprogs") Signed-off-by: Hari Bathini <[email protected]> Signed-off-by: Madhavan Srinivasan <[email protected]> Link: https://patch.msgid.link/[email protected]
1 parent 1578202 commit 3727d6e

3 files changed

Lines changed: 9 additions & 13 deletions

File tree

arch/powerpc/net/bpf_jit.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,6 @@
8181

8282
#ifdef CONFIG_PPC64
8383

84-
/* for gpr non volatile registers BPG_REG_6 to 10 */
85-
#define BPF_PPC_STACK_SAVE (6 * 8)
86-
8784
/* If dummy pass (!image), account for maximum possible instructions */
8885
#define PPC_LI64(d, i) do { \
8986
if (!image) \
@@ -219,8 +216,6 @@ int bpf_jit_emit_exit_insn(u32 *image, struct codegen_context *ctx, int tmp_reg,
219216
int bpf_add_extable_entry(struct bpf_prog *fp, u32 *image, u32 *fimage, int pass,
220217
struct codegen_context *ctx, int insn_idx,
221218
int jmp_off, int dst_reg, u32 code);
222-
223-
int bpf_jit_stack_tailcallinfo_offset(struct codegen_context *ctx);
224219
#endif
225220

226221
#endif

arch/powerpc/net/bpf_jit_comp.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -642,38 +642,36 @@ static void bpf_trampoline_setup_tail_call_info(u32 *image, struct codegen_conte
642642
int bpf_dummy_frame_size, int r4_off)
643643
{
644644
if (IS_ENABLED(CONFIG_PPC64)) {
645-
/* See Generated stack layout */
646-
int tailcallinfo_offset = BPF_PPC_TAILCALL;
647-
648645
/*
649646
* func_frame_offset = ...(1)
650647
* bpf_dummy_frame_size + trampoline_frame_size
651648
*/
652649
EMIT(PPC_RAW_LD(_R4, _R1, func_frame_offset));
653-
EMIT(PPC_RAW_LD(_R3, _R4, -tailcallinfo_offset));
650+
/* Refer to trampoline's Generated stack layout */
651+
EMIT(PPC_RAW_LD(_R3, _R4, -BPF_PPC_TAILCALL));
654652

655653
/*
656654
* Setting the tail_call_info in trampoline's frame
657655
* depending on if previous frame had value or reference.
658656
*/
659657
EMIT(PPC_RAW_CMPLWI(_R3, MAX_TAIL_CALL_CNT));
660658
PPC_BCC_CONST_SHORT(COND_GT, 8);
661-
EMIT(PPC_RAW_ADDI(_R3, _R4, bpf_jit_stack_tailcallinfo_offset(ctx)));
659+
EMIT(PPC_RAW_ADDI(_R3, _R4, -BPF_PPC_TAILCALL));
662660
/*
663661
* From ...(1) above:
664662
* trampoline_frame_bottom = ...(2)
665663
* func_frame_offset - bpf_dummy_frame_size
666664
*
667665
* Using ...(2) derived above:
668666
* trampoline_tail_call_info_offset = ...(3)
669-
* trampoline_frame_bottom - tailcallinfo_offset
667+
* trampoline_frame_bottom - BPF_PPC_TAILCALL
670668
*
671669
* From ...(3):
672670
* Use trampoline_tail_call_info_offset to write reference of main's
673671
* tail_call_info in trampoline frame.
674672
*/
675673
EMIT(PPC_RAW_STL(_R3, _R1, (func_frame_offset - bpf_dummy_frame_size)
676-
- tailcallinfo_offset));
674+
- BPF_PPC_TAILCALL));
677675
} else {
678676
/* See bpf_jit_stack_offsetof() and BPF_PPC_TC */
679677
EMIT(PPC_RAW_LL(_R4, _R1, r4_off));

arch/powerpc/net/bpf_jit_comp64.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
* exception boundary.
4343
*/
4444

45+
/* BPF non-volatile registers save area size */
46+
#define BPF_PPC_STACK_SAVE (6 * 8)
47+
4548
/* for bpf JIT code internal usage */
4649
#define BPF_PPC_STACK_LOCALS 24
4750
/*
@@ -148,7 +151,7 @@ static int bpf_jit_stack_local(struct codegen_context *ctx)
148151
}
149152
}
150153

151-
int bpf_jit_stack_tailcallinfo_offset(struct codegen_context *ctx)
154+
static int bpf_jit_stack_tailcallinfo_offset(struct codegen_context *ctx)
152155
{
153156
return bpf_jit_stack_local(ctx) + BPF_PPC_STACK_LOCALS + BPF_PPC_STACK_SAVE;
154157
}

0 commit comments

Comments
 (0)