The unwinder will release resources that a frame keeps on its BPF stack. The verifier gives their locations relative to the frame's R10. The stack walk reports the native frame pointer of a frame, and the position of R10 relative to it depends on the architecture and on the stack that the frame uses: - On x86-64, R10 is the native frame pointer. - On arm64, R10 is the stack pointer after the frame record, the tail call count pair and the callee-saved pairs are pushed. This is -16 - 16 * ceil(pushed / 2) below x29. Record the distance as frame_fp_off in the program's aux data. x86-64 leaves it 0. - A frame that uses a private stack keeps its BPF stack in the per-CPU private area, so its R10 is not derived from the frame pointer. The JIT knows that R10 is a fixed distance into the per-CPU private stack of the function. Record the distance as priv_frame_off. The arm64 JIT sets up R10 only for a program that uses it, but records the distances for every program. Add bpf_jit_supports_exception_cleanup(), which tells the verifier whether the JIT records these. The other JITs that support exceptions do not record them yet, and keep the rule that bpf_throw() must be reached with no resources held. Signed-off-by: Kumar Kartikeya Dwivedi --- arch/arm64/net/bpf_jit_comp.c | 16 ++++++++++++++++ arch/x86/net/bpf_jit_comp.c | 7 +++++++ include/linux/bpf.h | 4 ++++ include/linux/filter.h | 1 + kernel/bpf/core.c | 10 ++++++++++ 5 files changed, 38 insertions(+) diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c index c9f851bd5003..418bda24020c 100644 --- a/arch/arm64/net/bpf_jit_comp.c +++ b/arch/arm64/net/bpf_jit_comp.c @@ -1055,6 +1055,16 @@ static int build_prologue(struct jit_ctx *ctx, bool ebpf_from_cbpf) /* Stack must be multiples of 16B */ ctx->stack_size = round_up(prog->aux->stack_depth, 16); + /* + * Where the exception unwinder finds this frame's BPF stack: the + * private stack, or below the frame record, the tail call count pair + * and the pairs push_callee_regs() pushed. + */ + if (ctx->priv_sp_used) + prog->aux->priv_frame_off = PRIV_STACK_GUARD_SZ + ctx->stack_size; + else + prog->aux->frame_fp_off = -16 - 16 * DIV_ROUND_UP(ctx->nr_used_callee_reg, 2); + if (ctx->fp_used) { if (ctx->priv_sp_used) { /* Set up private stack pointer */ @@ -3904,6 +3914,12 @@ bool bpf_jit_supports_exceptions(void) return true; } +/* build_prologue() records where each frame keeps its BPF stack. */ +bool bpf_jit_supports_exception_cleanup(void) +{ + return true; +} + bool bpf_jit_supports_arena(void) { return true; diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c index 9b7466ca1a82..9170cbe3fa16 100644 --- a/arch/x86/net/bpf_jit_comp.c +++ b/arch/x86/net/bpf_jit_comp.c @@ -2425,6 +2425,7 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * priv_stack_ptr = bpf_prog->aux->priv_stack_ptr; if (priv_stack_ptr) { priv_frame_ptr = priv_stack_ptr + PRIV_STACK_GUARD_SZ + round_up(stack_depth, 8); + bpf_prog->aux->priv_frame_off = PRIV_STACK_GUARD_SZ + round_up(stack_depth, 8); stack_depth = 0; } @@ -5066,6 +5067,12 @@ bool bpf_jit_supports_exceptions(void) return IS_ENABLED(CONFIG_UNWINDER_ORC); } +/* On x86-64 R10 is the frame pointer; do_jit() records where a private stack has it. */ +bool bpf_jit_supports_exception_cleanup(void) +{ + return bpf_jit_supports_exceptions(); +} + bool bpf_jit_supports_private_stack(void) { return true; diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 7f2db4e9afe0..17b13b16899d 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -2010,6 +2010,10 @@ struct bpf_prog_aux { * by the JIT; consumed by the exception unwinder. */ s16 callee_reg_off[BPF_THROW_NR_CALLEE_SAVED]; + /* BPF frame pointer relative to the native frame pointer the stack walker reports */ + s16 frame_fp_off; + /* With a private stack, the frame's R10 is this far into this CPU's priv_stack_ptr. */ + u32 priv_frame_off; #ifdef CONFIG_SECURITY void *security; #endif diff --git a/include/linux/filter.h b/include/linux/filter.h index 61448be53f8d..08ad906f649f 100644 --- a/include/linux/filter.h +++ b/include/linux/filter.h @@ -1391,6 +1391,7 @@ bool bpf_jit_supports_stack_args(void); bool bpf_jit_supports_arena_args(void); bool bpf_jit_supports_far_kfunc_call(void); bool bpf_jit_supports_exceptions(void); +bool bpf_jit_supports_exception_cleanup(void); bool bpf_jit_supports_ptr_xchg(void); bool bpf_jit_supports_arena(void); bool bpf_jit_supports_insn(struct bpf_insn *insn, bool in_arena); diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index 7a25d1cb2c30..59478a08b247 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -3896,6 +3896,16 @@ bool __weak bpf_jit_supports_exceptions(void) return false; } +/* + * Does the JIT record where each frame keeps its BPF stack, see + * bpf_prog_aux::frame_fp_off, so that the unwinder can release what a frame + * holds when bpf_throw() discards it? + */ +bool __weak bpf_jit_supports_exception_cleanup(void) +{ + return false; +} + bool __weak bpf_jit_supports_private_stack(void) { return false; -- 2.53.0