Skip to content

Commit 8e2e296

Browse files
jpoimboeKernel Patches Daemon
authored andcommitted
bpf: Add bpf_has_frame_pointer()
Introduce a bpf_has_frame_pointer() helper that uwninders can call to determine whether a given instruction pointer is within the valid frame pointer region of a BPF JIT program or trampoline (i.e., after the prologue, before the epilogue). This will enable livepatch (with the ORC unwinder) to reliably unwind through BPF JIT frames. Signed-off-by: Josh Poimboeuf <[email protected]> Acked-by: Song Liu <[email protected]>
1 parent 52f5a27 commit 8e2e296

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

arch/x86/net/bpf_jit_comp.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,6 +1678,9 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image, u8 *rw_image
16781678
emit_prologue(&prog, image, stack_depth,
16791679
bpf_prog_was_classic(bpf_prog), tail_call_reachable,
16801680
bpf_is_subprog(bpf_prog), bpf_prog->aux->exception_cb);
1681+
1682+
bpf_prog->aux->ksym.fp_start = prog - temp;
1683+
16811684
/* Exception callback will clobber callee regs for its own use, and
16821685
* restore the original callee regs from main prog's stack frame.
16831686
*/
@@ -2736,6 +2739,8 @@ st: if (is_imm8(insn->off))
27362739
pop_r12(&prog);
27372740
}
27382741
EMIT1(0xC9); /* leave */
2742+
bpf_prog->aux->ksym.fp_end = prog - temp;
2743+
27392744
emit_return(&prog, image + addrs[i - 1] + (prog - temp));
27402745
break;
27412746

@@ -3325,6 +3330,8 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im
33253330
}
33263331
EMIT1(0x55); /* push rbp */
33273332
EMIT3(0x48, 0x89, 0xE5); /* mov rbp, rsp */
3333+
im->ksym.fp_start = prog - (u8 *)rw_image;
3334+
33283335
if (!is_imm8(stack_size)) {
33293336
/* sub rsp, stack_size */
33303337
EMIT3_off32(0x48, 0x81, 0xEC, stack_size);
@@ -3462,7 +3469,10 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *rw_im
34623469
emit_ldx(&prog, BPF_DW, BPF_REG_0, BPF_REG_FP, -8);
34633470

34643471
emit_ldx(&prog, BPF_DW, BPF_REG_6, BPF_REG_FP, -rbx_off);
3472+
34653473
EMIT1(0xC9); /* leave */
3474+
im->ksym.fp_end = prog - (u8 *)rw_image;
3475+
34663476
if (flags & BPF_TRAMP_F_SKIP_FRAME) {
34673477
/* skip our return address and return to parent */
34683478
EMIT4(0x48, 0x83, 0xC4, 8); /* add rsp, 8 */

include/linux/bpf.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,6 +1283,8 @@ struct bpf_ksym {
12831283
struct list_head lnode;
12841284
struct latch_tree_node tnode;
12851285
bool prog;
1286+
u32 fp_start;
1287+
u32 fp_end;
12861288
};
12871289

12881290
enum bpf_tramp_prog_type {
@@ -1511,6 +1513,7 @@ void bpf_image_ksym_add(struct bpf_ksym *ksym);
15111513
void bpf_image_ksym_del(struct bpf_ksym *ksym);
15121514
void bpf_ksym_add(struct bpf_ksym *ksym);
15131515
void bpf_ksym_del(struct bpf_ksym *ksym);
1516+
bool bpf_has_frame_pointer(unsigned long ip);
15141517
int bpf_jit_charge_modmem(u32 size);
15151518
void bpf_jit_uncharge_modmem(u32 size);
15161519
bool bpf_prog_has_trampoline(const struct bpf_prog *prog);

kernel/bpf/core.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,22 @@ struct bpf_prog *bpf_prog_ksym_find(unsigned long addr)
760760
NULL;
761761
}
762762

763+
bool bpf_has_frame_pointer(unsigned long ip)
764+
{
765+
struct bpf_ksym *ksym;
766+
unsigned long offset;
767+
768+
guard(rcu)();
769+
770+
ksym = bpf_ksym_find(ip);
771+
if (!ksym || !ksym->fp_start || !ksym->fp_end)
772+
return false;
773+
774+
offset = ip - ksym->start;
775+
776+
return offset >= ksym->fp_start && offset < ksym->fp_end;
777+
}
778+
763779
const struct exception_table_entry *search_bpf_extables(unsigned long addr)
764780
{
765781
const struct exception_table_entry *e = NULL;

0 commit comments

Comments
 (0)