Skip to content

Commit

Permalink
bpf: Add support to inline bpf_get_func_ip helper on x86
Browse files Browse the repository at this point in the history
Adding support to inline it on x86, because it's single
load instruction.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20220316122419.933957-6-jolsa@kernel.org
  • Loading branch information
Jiri Olsa authored and Alexei Starovoitov committed Mar 18, 2022
1 parent 42a5712 commit 97ee4d2
Showing 2 changed files with 21 additions and 1 deletion.
21 changes: 20 additions & 1 deletion kernel/bpf/verifier.c
Original file line number Diff line number Diff line change
@@ -13678,7 +13678,7 @@ static int do_misc_fixups(struct bpf_verifier_env *env)
continue;
}

/* Implement bpf_get_func_ip inline. */
/* Implement tracing bpf_get_func_ip inline. */
if (prog_type == BPF_PROG_TYPE_TRACING &&
insn->imm == BPF_FUNC_get_func_ip) {
/* Load IP address from ctx - 16 */
@@ -13693,6 +13693,25 @@ static int do_misc_fixups(struct bpf_verifier_env *env)
continue;
}

#ifdef CONFIG_X86
/* Implement kprobe_multi bpf_get_func_ip inline. */
if (prog_type == BPF_PROG_TYPE_KPROBE &&
eatype == BPF_TRACE_KPROBE_MULTI &&
insn->imm == BPF_FUNC_get_func_ip) {
/* Load IP address from ctx (struct pt_regs) ip */
insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1,
offsetof(struct pt_regs, ip));

new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, 1);
if (!new_prog)
return -ENOMEM;

env->prog = prog = new_prog;
insn = new_prog->insnsi + i + delta;
continue;
}
#endif

patch_call_imm:
fn = env->ops->get_func_proto(insn->imm, env->prog);
/* all functions that have prototype and verifier allowed
1 change: 1 addition & 0 deletions kernel/trace/bpf_trace.c
Original file line number Diff line number Diff line change
@@ -1039,6 +1039,7 @@ static const struct bpf_func_proto bpf_get_func_ip_proto_kprobe = {

BPF_CALL_1(bpf_get_func_ip_kprobe_multi, struct pt_regs *, regs)
{
/* This helper call is inlined by verifier on x86. */
return instruction_pointer(regs);
}

0 comments on commit 97ee4d2

Please sign in to comment.