Skip to content

Commit

Permalink
bpf: Add bpf_get_func_ip helper for kprobe programs
Browse files Browse the repository at this point in the history
Adding bpf_get_func_ip helper for BPF_PROG_TYPE_KPROBE programs,
so it's now possible to call bpf_get_func_ip from both kprobe and
kretprobe programs.

Taking the caller's address from 'struct kprobe::addr', which is
defined for both kprobe and kretprobe.

Signed-off-by: Jiri Olsa <[email protected]>
Signed-off-by: Alexei Starovoitov <[email protected]>
Reviewed-by: Masami Hiramatsu <[email protected]>
Link: https://lore.kernel.org/bpf/[email protected]
  • Loading branch information
Jiri Olsa authored and Alexei Starovoitov committed Jul 16, 2021
1 parent 9b99edc commit 9ffd9f3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion include/uapi/linux/bpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -4844,7 +4844,7 @@ union bpf_attr {
*
* u64 bpf_get_func_ip(void *ctx)
* Description
* Get address of the traced function (for tracing programs).
* Get address of the traced function (for tracing and kprobe programs).
* Return
* Address of the traced function.
*/
Expand Down
2 changes: 2 additions & 0 deletions kernel/bpf/verifier.c
Original file line number Diff line number Diff line change
Expand Up @@ -6175,6 +6175,8 @@ static int check_get_func_ip(struct bpf_verifier_env *env)
return -ENOTSUPP;
}
return 0;
} else if (type == BPF_PROG_TYPE_KPROBE) {
return 0;
}

verbose(env, "func %s#%d not supported for program type %d\n",
Expand Down
16 changes: 16 additions & 0 deletions kernel/trace/bpf_trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,20 @@ static const struct bpf_func_proto bpf_get_func_ip_proto_tracing = {
.arg1_type = ARG_PTR_TO_CTX,
};

BPF_CALL_1(bpf_get_func_ip_kprobe, struct pt_regs *, regs)
{
struct kprobe *kp = kprobe_running();

return kp ? (u64) kp->addr : 0;
}

static const struct bpf_func_proto bpf_get_func_ip_proto_kprobe = {
.func = bpf_get_func_ip_kprobe,
.gpl_only = true,
.ret_type = RET_INTEGER,
.arg1_type = ARG_PTR_TO_CTX,
};

const struct bpf_func_proto *
bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
{
Expand Down Expand Up @@ -1092,6 +1106,8 @@ kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
case BPF_FUNC_override_return:
return &bpf_override_return_proto;
#endif
case BPF_FUNC_get_func_ip:
return &bpf_get_func_ip_proto_kprobe;
default:
return bpf_tracing_func_proto(func_id, prog);
}
Expand Down
2 changes: 1 addition & 1 deletion tools/include/uapi/linux/bpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -4844,7 +4844,7 @@ union bpf_attr {
*
* u64 bpf_get_func_ip(void *ctx)
* Description
* Get address of the traced function (for tracing programs).
* Get address of the traced function (for tracing and kprobe programs).
* Return
* Address of the traced function.
*/
Expand Down

0 comments on commit 9ffd9f3

Please sign in to comment.