bpf, x86: Add bpf_get_func_ip helper
Subsystems (12)
v4 cover bpf, x86: Add bpf_get_func_ip helper
hi,
adding bpf_get_func_ip helper that returns IP address of the caller function for trampoline and krobe programs.
There're 2 specific implementation of the bpf_get_func_ip helper, one for trampoline progs and one for kprobe/kretprobe progs.
The trampoline helper call is replaced/inlined by verifier with simple move instruction. The kprobe/kretprobe is actual helper call that returns prepared caller address.
Also available at:
https://git.kernel.org/pub/scm/linux/kernel/git/jolsa/perf.git
bpf/get_func_ip
v4 changes:
- dropped jit/x86 check for get_func_ip tracing check [Alexei]
- added code to bpf_get_func_ip_tracing [Alexei]
and tested that it works without inlining [Alexei]
- changed has_get_func_ip to check_get_func_ip [Andrii]
- replaced test assert loop with explicit asserts [Andrii]
- adde bpf_program__attach_kprobe_opts function
and use it for offset setup [Andrii]
- used bpf_program__set_autoload(false) for test6 [Andrii]
- added Masami's ack
v3 changes:
- resend with Masami in cc and v3 in each patch subject
v2 changes:
- use kprobe_running to get kprobe instead of cpu var [Masami]
- added support to add kprobe on function+offset
and test for that [Alan]
thanks,
jirka
---
Alan Maguire (1):
libbpf: Allow specification of "kprobe/function+offset"
Jiri Olsa (7):
bpf, x86: Store caller's ip in trampoline stack
bpf: Enable BPF_TRAMP_F_IP_ARG for trampolines with call_get_func_ip
bpf: Add bpf_get_func_ip helper for tracing programs
bpf: Add bpf_get_func_ip helper for kprobe programs selftests/bpf: Add test for bpf_get_func_ip helper
libbpf: Add bpf_program__attach_kprobe_opts function selftests/bpf: Add test for bpf_get_func_ip in kprobe+offset probe
arch/x86/net/bpf_jit_comp.c | 19 +++++++++++++++++++ include/linux/bpf.h | 5 +++++ include/linux/filter.h | 3 ++- include/uapi/linux/bpf.h | 7 +++++++ kernel/bpf/trampoline.c | 12 +++++++++--- kernel/bpf/verifier.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ kernel/trace/bpf_trace.c | 31 +++++++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 7 +++++++ tools/lib/bpf/libbpf.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++---------- tools/testing/selftests/bpf/prog_tests/get_func_ip_test.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ tools/testing/selftests/bpf/progs/get_func_ip_test.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 11 files changed, 297 insertions(+), 14 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/get_func_ip_test.c create mode 100644 tools/testing/selftests/bpf/progs/get_func_ip_test.c
PATCH v4 1/8 bpf, x86: Store caller's ip in trampoline stack
Storing caller's ip in trampoline's stack. Trampoline programs can reach the IP in (ctx - 8) address, so there's no change in program's arguments interface.
The IP address is takes from [fp + 8], which is return address from the initial 'call fentry' call to trampoline.
This IP address will be returned via bpf_get_func_ip helper helper, which is added in following patches.
--- arch/x86/net/bpf_jit_comp.c | 19 +++++++++++++++++++ include/linux/bpf.h | 5 +++++ 2 files changed, 24 insertions(+)
arch/x86/net/bpf_jit_comp.c +19 -0
@@ -1951,6 +1951,9 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *iif (flags &BPF_TRAMP_F_CALL_ORIG ) stack_size +=8 ;/* room for return value of orig_call */ +if (flags &BPF_TRAMP_F_IP_ARG ) + stack_size +=8 ;/* room for IP address argument */ +if (flags &BPF_TRAMP_F_SKIP_FRAME )/* skip patched call instruction and point orig_call to actual * body of the kernel function.
@@ -1964,6 +1967,22 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *iEMIT4 (0x48 ,0x83 ,0xEC , stack_size);/* sub rsp, stack_size */ EMIT1 (0x53 );/* push rbx */ +if (flags &BPF_TRAMP_F_IP_ARG ) { +/* Store IP address of the traced function: +* mov rax, QWORD PTR [rbp + 8] +* sub rax, X86_PATCH_SIZE +* mov QWORD PTR [rbp - stack_size], rax +*/ +emit_ldx (&prog,BPF_DW ,BPF_REG_0 ,BPF_REG_FP ,8 ); +EMIT4 (0x48 ,0x83 ,0xe8 ,X86_PATCH_SIZE ); +emit_stx (&prog,BPF_DW ,BPF_REG_FP ,BPF_REG_0 , -stack_size); + +/* Continue with stack_size for regs storage, stack will +* be correctly restored with 'leave' instruction. +*/ + stack_size -=8 ; + } +save_regs (m, &prog, nr_args, stack_size);if (flags &BPF_TRAMP_F_CALL_ORIG ) {
include/linux/bpf.h +5 -0
@@ -554,6 +554,11 @@ struct btf_func_model {*/ #define BPF_TRAMP_F_SKIP_FRAME BIT(2) +/* Store IP address of the caller on the trampoline stack, +* so it's available for trampoline's programs. +*/ +#define BPF_TRAMP_F_IP_ARG BIT(3) +/* Each call __bpf_prog_enter + call bpf_func + call __bpf_prog_exit is ~50 * bytes on x86. Pick a number to fit into BPF_IMAGE_SIZE / 2 */
Signature · 1 line
2.31.1
PATCH v4 2/8 bpf: Enable BPF_TRAMP_F_IP_ARG for trampolines with call_get_func_ip
In reply to Jiri OlsaEnabling BPF_TRAMP_F_IP_ARG for trampolines that actually need it.
The BPF_TRAMP_F_IP_ARG adds extra 3 instructions to trampoline code and is used only by programs with bpf_get_func_ip helper, which is added in following patch and sets call_get_func_ip bit.
This patch ensures that BPF_TRAMP_F_IP_ARG flag is used only for trampolines that have programs with call_get_func_ip set.
--- include/linux/filter.h | 3 ++- kernel/bpf/trampoline.c | 12 +++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-)
include/linux/filter.h +2 -1
@@ -559,7 +559,8 @@ struct bpf_prog { kprobe_override:1, /* Do we override a kprobe? */ has_callchain_buf:1, /* callchain buffer allocated? */ enforce_expected_attach_type:1, /* Enforce expected_attach_type checking at attach time */ - call_get_stack:1; /* Do we call bpf_get_stack() or bpf_get_stackid() */ + call_get_stack:1, /* Do we call bpf_get_stack() or bpf_get_stackid() */ + call_get_func_ip:1; /* Do we call get_func_ip() */enum bpf_prog_type type;/* Type of BPF program */ enum bpf_attach_type expected_attach_type;/* For some prog types */ u32 len;/* Number of filter blocks */
kernel/bpf/trampoline.c +9 -3
@@ -172,7 +172,7 @@ static int register_fentry(struct bpf_trampoline *tr, void *new_addr) }static struct bpf_tramp_progs * -bpf_trampoline_get_progs (const struct bpf_trampoline *tr,int *total) +bpf_trampoline_get_progs (const struct bpf_trampoline *tr,int *total,bool *ip_arg) {const struct bpf_prog_aux *aux;struct bpf_tramp_progs *tprogs;
@@ -189,8 +189,10 @@ bpf_trampoline_get_progs(const struct bpf_trampoline *tr, int *total) *total += tr->progs_cnt[kind]; progs = tprogs[kind].progs; -hlist_for_each_entry (aux, &tr->progs_hlist[kind], tramp_hlist) +hlist_for_each_entry (aux, &tr->progs_hlist[kind], tramp_hlist) { + *ip_arg |= aux->prog->call_get_func_ip; *progs++ = aux->prog; + } }return tprogs; }
@@ -333,9 +335,10 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr)struct bpf_tramp_image *im;struct bpf_tramp_progs *tprogs;u32 flags =BPF_TRAMP_F_RESTORE_REGS ; +bool ip_arg = false;int err, total; - tprogs =bpf_trampoline_get_progs (tr, &total); + tprogs =bpf_trampoline_get_progs (tr, &total, &ip_arg);if (IS_ERR (tprogs))return PTR_ERR (tprogs);
@@ -357,6 +360,9 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr) tprogs[BPF_TRAMP_MODIFY_RETURN].nr_progs) flags =BPF_TRAMP_F_CALL_ORIG |BPF_TRAMP_F_SKIP_FRAME ; +if (ip_arg) + flags |=BPF_TRAMP_F_IP_ARG ; + err =arch_prepare_bpf_trampoline (im, im->image, im->image +PAGE_SIZE , &tr->func.model, flags, tprogs, tr->func.addr);
Signature · 1 line
2.31.1
PATCH v4 3/8 bpf: Add bpf_get_func_ip helper for tracing programs
In reply to Jiri OlsaAdding bpf_get_func_ip helper for BPF_PROG_TYPE_TRACING programs, specifically for all trampoline attach types.
The trampoline's caller IP address is stored in (ctx - 8) address. so there's no reason to actually call the helper, but rather fixup the call instruction and return [ctx - 8] value directly (suggested by Alexei).
--- include/uapi/linux/bpf.h | 7 ++++++ kernel/bpf/verifier.c | 43 ++++++++++++++++++++++++++++++++++ kernel/trace/bpf_trace.c | 15 ++++++++++++ tools/include/uapi/linux/bpf.h | 7 ++++++ 4 files changed, 72 insertions(+)
include/uapi/linux/bpf.h +7 -0
@@ -4777,6 +4777,12 @@ union bpf_attr {* Execute close syscall for given FD. * Return * A syscall result. +* +* u64 bpf_get_func_ip(void *ctx) +* Description +* Get address of the traced function (for tracing programs). +* Return +* Address of the traced function. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -4948,6 +4954,7 @@ union bpf_attr {FN (sys_bpf), \FN (btf_find_by_name_kind), \FN (sys_close), \ +FN (get_func_ip), \/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper
kernel/bpf/verifier.c +43 -0
67 lines not on this page · raw message
kernel/trace/bpf_trace.c +15 -0
@@ -948,6 +948,19 @@ const struct bpf_func_proto bpf_snprintf_btf_proto = { .arg5_type = ARG_ANYTHING, }; +BPF_CALL_1(bpf_get_func_ip_tracing, void *, ctx) +{ +/* This helper call is inlined by verifier. */ +return ((u64 *)ctx)[-1 ]; +} + +static const struct bpf_func_proto bpf_get_func_ip_proto_tracing = { + .func = bpf_get_func_ip_tracing, + .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) {
@@ -1058,6 +1071,8 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)return &bpf_for_each_map_elem_proto;case BPF_FUNC_snprintf:return &bpf_snprintf_proto; +case BPF_FUNC_get_func_ip: +return &bpf_get_func_ip_proto_tracing;default :return NULL ; }
tools/include/uapi/linux/bpf.h +7 -0
@@ -4780,6 +4780,12 @@ union bpf_attr {* Execute close syscall for given FD. * Return * A syscall result. +* +* u64 bpf_get_func_ip(void *ctx) +* Description +* Get address of the traced function (for tracing programs). +* Return +* Address of the traced function. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -4951,6 +4957,7 @@ union bpf_attr {FN (sys_bpf), \FN (btf_find_by_name_kind), \FN (sys_close), \ +FN (get_func_ip), \/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper
Signature · 1 line
2.31.1
Re: [PATCHv4 bpf-next 3/8] bpf: Add bpf_get_func_ip helper for tracing programs
Quoted 4 lines · Jiri Olsa
Adding
bpf_get_func_iphelper forBPF_PROG_TYPE_TRACINGprograms, specifically for all trampoline attach types.
Quoted 8 lines
The trampoline's caller IP address is stored in (ctx - 8) address. so there's no reason to actually call the helper, but rather fixup the call instruction and return [ctx - 8] value directly (suggested by Alexei).
[fixed has_get_func_ip wrong return type]Reported-by kernel test robot <[email protected]>Reported-by Dan Carpenter <[email protected]>
I removed these tags, since they don't correspond to any real commit in the git. Otherwise all patches would have been full of such things when patch series go through iterations. Also fixed a few typos here and there, manually rebased and applied.
Thanks!
Re: [PATCHv4 bpf-next 3/8] bpf: Add bpf_get_func_ip helper for tracing programs
Quoted 14 lines · Alexei Starovoitov
Quoted 4 lines · Jiri Olsa
Adding
bpf_get_func_iphelper forBPF_PROG_TYPE_TRACINGprograms, specifically for all trampoline attach types.Quoted 8 lines
The trampoline's caller IP address is stored in (ctx - 8) address. so there's no reason to actually call the helper, but rather fixup the call instruction and return [ctx - 8] value directly (suggested by Alexei).
[fixed has_get_func_ip wrong return type]Reported-by kernel test robot <[email protected]>Reported-by Dan Carpenter <[email protected]>
Quoted 6 lines
I removed these tags, since they don't correspond to any real commit in the git. Otherwise all patches would have been full of such things when patch series go through iterations. Also fixed a few typos here and there, manually rebased and applied.
Thanks!
thanks,
jirka
PATCH v4 4/8 bpf: Add bpf_get_func_ip helper for kprobe programs
In reply to Jiri OlsaAdding 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.
--- include/uapi/linux/bpf.h | 2 +- kernel/bpf/verifier.c | 2 ++ kernel/trace/bpf_trace.c | 16 ++++++++++++++++ tools/include/uapi/linux/bpf.h | 2 +- 4 files changed, 20 insertions(+), 2 deletions(-)
include/uapi/linux/bpf.h +1 -1
@@ -4780,7 +4780,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. */
kernel/bpf/verifier.c +2 -0
@@ -5969,6 +5969,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",
kernel/trace/bpf_trace.c +16 -0
@@ -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) {
@@ -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); }
tools/include/uapi/linux/bpf.h +1 -1
@@ -4783,7 +4783,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. */
Signature · 1 line
2.31.1
PATCH v4 5/8 selftests/bpf: Add test for bpf_get_func_ip helper
In reply to Jiri OlsaAdding test for bpf_get_func_ip helper for fentry, fexit, kprobe, kretprobe and fmod_ret programs.
--- .../bpf/prog_tests/get_func_ip_test.c | 39 ++++++++++++ .../selftests/bpf/progs/get_func_ip_test.c | 62 +++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/get_func_ip_test.c create mode 100644 tools/testing/selftests/bpf/progs/get_func_ip_test.c
tools/testing/selftests/bpf/prog_tests/get_func_ip_test.c new file +39 -0
@@ -0,0 +1,39 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <test_progs.h> +#include "get_func_ip_test.skel.h" + +void test_get_func_ip_test (void ) +{ +struct get_func_ip_test *skel =NULL ; +__u32 duration =0 , retval; +int err, prog_fd; + + skel =get_func_ip_test__open_and_load (); +if (!ASSERT_OK_PTR (skel,"get_func_ip_test__open_and_load" )) +return ; + + err =get_func_ip_test__attach (skel); +if (!ASSERT_OK (err,"get_func_ip_test__attach" )) + goto cleanup; + + prog_fd =bpf_program__fd (skel->progs.test1); + err =bpf_prog_test_run (prog_fd,1 ,NULL ,0 , +NULL ,NULL , &retval, &duration); +ASSERT_OK (err,"test_run" ); +ASSERT_EQ (retval,0 ,"test_run" ); + + prog_fd =bpf_program__fd (skel->progs.test5); + err =bpf_prog_test_run (prog_fd,1 ,NULL ,0 , +NULL ,NULL , &retval, &duration); + +ASSERT_OK (err,"test_run" ); + +ASSERT_EQ (skel->bss->test1_result,1 ,"test1_result" ); +ASSERT_EQ (skel->bss->test2_result,1 ,"test2_result" ); +ASSERT_EQ (skel->bss->test3_result,1 ,"test3_result" ); +ASSERT_EQ (skel->bss->test4_result,1 ,"test4_result" ); +ASSERT_EQ (skel->bss->test5_result,1 ,"test5_result" ); + +cleanup: +get_func_ip_test__destroy (skel); +}
tools/testing/selftests/bpf/progs/get_func_ip_test.c new file +62 -0
62 lines not on this page · raw message
Signature · 1 line
2.31.1
PATCH v4 6/8 libbpf: Add bpf_program__attach_kprobe_opts function
In reply to Jiri OlsaAdding bpf_program__attach_kprobe_opts that does the same as bpf_program__attach_kprobe, but takes opts argument.
Currently opts struct holds just retprobe bool, but we will add new field in following patch.
The function is not exported, so there's no need to add size to the struct bpf_program_attach_kprobe_opts for now.
--- tools/lib/bpf/libbpf.c | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-)
tools/lib/bpf/libbpf.c +25 -9
@@ -10346,19 +10346,24 @@ static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,return pfd; } -struct bpf_link *bpf_program__attach_kprobe (struct bpf_program *prog, -bool retprobe, -const char *func_name) +struct bpf_program_attach_kprobe_opts { +bool retprobe; +}; + +static struct bpf_link * +bpf_program__attach_kprobe_opts (struct bpf_program *prog, +const char *func_name, +struct bpf_program_attach_kprobe_opts *opts) {char errmsg[STRERR_BUFSIZE ];struct bpf_link *link;int pfd, err; - pfd =perf_event_open_probe (false/* uprobe */ , retprobe, func_name, + pfd =perf_event_open_probe (false/* uprobe */ , opts->retprobe, func_name,0 /* offset */ ,-1 /* pid */ );if (pfd <0 ) {pr_warn ("prog '%s': failed to create %s '%s' perf event: %s\n" , - prog->name, retprobe ?"kretprobe" :"kprobe" , func_name, + prog->name, opts->retprobe ?"kretprobe" :"kprobe" , func_name,libbpf_strerror_r (pfd, errmsg,sizeof (errmsg)));return libbpf_err_ptr (pfd); }
@@ -10367,23 +10372,34 @@ struct bpf_link *bpf_program__attach_kprobe(struct bpf_program *prog,if (err) {close (pfd);pr_warn ("prog '%s': failed to attach to %s '%s': %s\n" , - prog->name, retprobe ?"kretprobe" :"kprobe" , func_name, + prog->name, opts->retprobe ?"kretprobe" :"kprobe" , func_name,libbpf_strerror_r (err, errmsg,sizeof (errmsg)));return libbpf_err_ptr (err); }return link; } +struct bpf_link *bpf_program__attach_kprobe (struct bpf_program *prog, +bool retprobe, +const char *func_name) +{ +struct bpf_program_attach_kprobe_opts opts = { + .retprobe = retprobe, + }; + +return bpf_program__attach_kprobe_opts (prog, func_name, &opts); +} +static struct bpf_link *attach_kprobe (const struct bpf_sec_def *sec,struct bpf_program *prog) { +struct bpf_program_attach_kprobe_opts opts;const char *func_name; -bool retprobe; func_name = prog->sec_name + sec->len; - retprobe =strcmp (sec->sec,"kretprobe/" ) ==0 ; + opts.retprobe =strcmp (sec->sec,"kretprobe/" ) ==0 ; -return bpf_program__attach_kprobe (prog, retprobe, func_name); +return bpf_program__attach_kprobe_opts (prog, func_name, &opts); } struct bpf_link *bpf_program__attach_uprobe(struct bpf_program *prog,
Signature · 1 line
2.31.1
Re: [PATCHv4 bpf-next 6/8] libbpf: Add bpf_program__attach_kprobe_opts function
Quoted 9 lines · Jiri Olsa
Adding
bpf_program__attach_kprobe_optsthat does the same asbpf_program__attach_kprobe, but takes opts argument.Currently opts struct holds just retprobe bool, but we will add new field in following patch.
The function is not exported, so there's no need to add size to the struct
bpf_program_attach_kprobe_optsfor now.
Why not exported? Please use a proper _opts struct just like others (e.g., bpf_object_open_opts) and add is as a public API, it's a useful addition. We are going to have a similar structure for attach_uprobe, btw. Please send a follow up patch.
Quoted 11 lines
Signed-off-by Jiri Olsa <[email protected]>--- tools/lib/bpf/libbpf.c | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-)tools/lib/bpf/libbpf.c +0
-0
tools/lib/bpf/libbpf.c static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name, · line 10349
Quoted 7 lines
tools/lib/bpf/libbpf.c +1
-3@@ -10346,19 +10346,24 @@ static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,return pfd; } -struct bpf_link *bpf_program__attach_kprobe (struct bpf_program *prog, -bool retprobe, -const char *func_name) +struct bpf_program_attach_kprobe_opts {
when you make it part of libbpf API, let's call it something shorter, like bpf_kprobe_opts, maybe? And later we'll have bpf_uprobe_opts for uprobes. Short and unambiguous.
Quoted 12 lines
tools/lib/bpf/libbpf.c +7
-0+bool retprobe; +}; + +static struct bpf_link * +bpf_program__attach_kprobe_opts (struct bpf_program *prog, +const char *func_name, +struct bpf_program_attach_kprobe_opts *opts) {char errmsg[STRERR_BUFSIZE ];struct bpf_link *link;int pfd, err;
[...]
Re: [PATCHv4 bpf-next 6/8] libbpf: Add bpf_program__attach_kprobe_opts function
Quoted 8 lines · Andrii Nakryiko
Quoted 7 lines · Jiri Olsa
Adding
bpf_program__attach_kprobe_optsthat does the same asbpf_program__attach_kprobe, but takes opts argument.Currently opts struct holds just retprobe bool, but we will add new field in following patch.
Quoted 7 lines
Quoted 2 lines
The function is not exported, so there's no need to add size to the struct
bpf_program_attach_kprobe_optsfor now.Why not exported? Please use a proper
_optsstruct just like others (e.g.,bpf_object_open_opts) and add is as a public API, it's a useful addition. We are going to have a similar structure forattach_uprobe, btw. Please send a follow up patch.
there's no outside user.. ok
Quoted 15 lines
Quoted 15 lines
Signed-off-by Jiri Olsa <[email protected]>--- tools/lib/bpf/libbpf.c | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-)tools/lib/bpf/libbpf.c +0
-0@@ -10346,19 +10346,24 @@ static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,return pfd; }
Quoted 8 lines
tools/lib/bpf/libbpf.c static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name, · line 10349
Quoted 4 lines
tools/lib/bpf/libbpf.c +1
-3-struct bpf_link *bpf_program__attach_kprobe (struct bpf_program *prog, -bool retprobe, -const char *func_name) +struct bpf_program_attach_kprobe_opts {when you make it part of libbpf API, let's call it something shorter, like
bpf_kprobe_opts, maybe? And later we'll havebpf_uprobe_optsfor uprobes. Short and unambiguous.
ok
jirka
Quoted 16 lines
Quoted 13 lines
tools/lib/bpf/libbpf.c +7
-0+bool retprobe; +}; + +static struct bpf_link * +bpf_program__attach_kprobe_opts (struct bpf_program *prog, +const char *func_name, +struct bpf_program_attach_kprobe_opts *opts) {char errmsg[STRERR_BUFSIZE ];struct bpf_link *link;int pfd, err;[...]
Re: [PATCHv4 bpf-next 6/8] libbpf: Add bpf_program__attach_kprobe_opts function
Quoted 13 lines · Jiri Olsa
Quoted 11 lines · Andrii Nakryiko
Quoted 9 lines · Jiri Olsa
Adding
bpf_program__attach_kprobe_optsthat does the same asbpf_program__attach_kprobe, but takes opts argument.Currently opts struct holds just retprobe bool, but we will add new field in following patch.
The function is not exported, so there's no need to add size to the struct
bpf_program_attach_kprobe_optsfor now.
Quoted 6 lines
Quoted 4 lines
Why not exported? Please use a proper
_optsstruct just like others (e.g.,bpf_object_open_opts) and add is as a public API, it's a useful addition. We are going to have a similar structure forattach_uprobe, btw. Please send a follow up patch.there's no outside user.. ok
because there is no API :) I've seen people asking about the ability to attach to kprobe+offset in some PRs.
Quoted 46 lines
Quoted 16 lines
Quoted 16 lines
Signed-off-by Jiri Olsa <[email protected]>--- tools/lib/bpf/libbpf.c | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-)tools/lib/bpf/libbpf.c +0
-0@@ -10346,19 +10346,24 @@ static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,return pfd; }Quoted 8 lines
tools/lib/bpf/libbpf.c static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name, · line 10349
Quoted 4 lines
tools/lib/bpf/libbpf.c +1
-3-struct bpf_link *bpf_program__attach_kprobe (struct bpf_program *prog, -bool retprobe, -const char *func_name) +struct bpf_program_attach_kprobe_opts {when you make it part of libbpf API, let's call it something shorter, like
bpf_kprobe_opts, maybe? And later we'll havebpf_uprobe_optsfor uprobes. Short and unambiguous.ok
jirka
Quoted 16 lines
Quoted 13 lines
tools/lib/bpf/libbpf.c +7
-0+bool retprobe; +}; + +static struct bpf_link * +bpf_program__attach_kprobe_opts (struct bpf_program *prog, +const char *func_name, +struct bpf_program_attach_kprobe_opts *opts) {char errmsg[STRERR_BUFSIZE ];struct bpf_link *link;int pfd, err;[...]
PATCH v4 7/8 libbpf: Allow specification of "kprobe/function+offset"
In reply to Jiri OlsaFrom: Alan Maguire <[email protected]>
kprobes can be placed on most instructions in a function, not just entry, and ftrace and bpftrace support the function+offset notification for probe placement. Adding parsing of func_name into func+offset to bpf_program__attach_kprobe() allows the user to specify
SEC("kprobe/bpf_fentry_test5+0x6")
...for example, and the offset can be passed to perf_event_open_probe() to support kprobe attachment.
[jolsa: changed original code to use bpf_program__attach_kprobe_opts and use dynamic allocation in sscanf]
--- tools/lib/bpf/libbpf.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-)
tools/lib/bpf/libbpf.c +22 -2
@@ -10348,6 +10348,7 @@ static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,struct bpf_program_attach_kprobe_opts {bool retprobe; +unsigned long offset; }; static struct bpf_link*
@@ -10360,7 +10361,7 @@ bpf_program__attach_kprobe_opts(struct bpf_program *prog,int pfd, err; pfd =perf_event_open_probe (false/* uprobe */ , opts->retprobe, func_name, -0 /* offset */ ,-1 /* pid */ ); + opts->offset,-1 /* pid */ );if (pfd <0 ) { pr_warn("prog '%s': failed to create %s '%s' perf event: %s\n", prog->name, opts->retprobe ? "kretprobe" : "kprobe", func_name,
@@ -10394,12 +10395,31 @@ static struct bpf_link *attach_kprobe(const struct bpf_sec_def *sec, struct bpf_program *prog) {struct bpf_program_attach_kprobe_opts opts; +unsigned long offset =0 ; +struct bpf_link *link;const char *func_name; +char *func; +int n, err; func_name = prog->sec_name + sec->len; opts.retprobe =strcmp (sec->sec,"kretprobe/" ) ==0 ; -return bpf_program__attach_kprobe_opts (prog, func_name, &opts); + n =sscanf (func_name,"%m[a-zA-Z0-9_.]+%lx" , &func, &offset); +if (n <1 ) { + err = -EINVAL ; +pr_warn ("kprobe name is invalid: %s\n" , func_name); +return libbpf_err_ptr (err); + } +if (opts.retprobe && offset !=0 ) { + err = -EINVAL ; +pr_warn ("kretprobes do not support offset specification\n" ); +return libbpf_err_ptr (err); + } + + opts.offset = offset; + link =bpf_program__attach_kprobe_opts (prog, func, &opts); +free (func); +return link; } struct bpf_link *bpf_program__attach_uprobe(struct bpf_program *prog,
Signature · 1 line
2.31.1
Re: [PATCHv4 bpf-next 7/8] libbpf: Allow specification of "kprobe/function+offset"
Quoted 49 lines · Jiri Olsa
From: Alan Maguire <[email protected]>
kprobes can be placed on most instructions in a function, not just entry, and ftrace and bpftrace support the function+offset notification for probe placement. Adding parsing of
func_nameinto func+offset tobpf_program__attach_kprobe()allows the user to specify
SEC("kprobe/bpf_fentry_test5+0x6")...for example, and the offset can be passed to
perf_event_open_probe()to support kprobe attachment.[jolsa: changed original code to use
bpf_program__attach_kprobe_optsand use dynamic allocation in sscanf]Signed-off-by Alan Maguire <[email protected]>Signed-off-by Jiri Olsa <[email protected]>--- tools/lib/bpf/libbpf.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-)tools/lib/bpf/libbpf.c +3
-1@@ -10348,6 +10348,7 @@ static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,struct bpf_program_attach_kprobe_opts {bool retprobe; +unsigned long offset; }; static struct bpf_link*@@ -10360,7 +10361,7 @@ bpf_program__attach_kprobe_opts(struct bpf_program *prog,int pfd, err; pfd =perf_event_open_probe (false/* uprobe */ , opts->retprobe, func_name, -0 /* offset */ ,-1 /* pid */ ); + opts->offset,-1 /* pid */ );if (pfd <0 ) { pr_warn("prog '%s': failed to create %s '%s' perf event: %s\n", prog->name, opts->retprobe ? "kretprobe" : "kprobe", func_name,@@ -10394,12 +10395,31 @@ static struct bpf_link *attach_kprobe(const struct bpf_sec_def *sec, struct bpf_program *prog) {struct bpf_program_attach_kprobe_opts opts; +unsigned long offset =0 ;
tools/lib/bpf/libbpf.c static struct bpf_link *attach_kprobe(const struct bpf_sec_def *sec, · line 10407
Quoted 10 lines
tools/lib/bpf/libbpf.c +4
-1+struct bpf_link *link;const char *func_name; +char *func; +int n, err; func_name = prog->sec_name + sec->len; opts.retprobe =strcmp (sec->sec,"kretprobe/" ) ==0 ; -return bpf_program__attach_kprobe_opts (prog, func_name, &opts); + n =sscanf (func_name,"%m[a-zA-Z0-9_.]+%lx" , &func, &offset);
could have used %li here to support both +0xabc and +123 forms
tools/lib/bpf/libbpf.c static struct bpf_link *attach_kprobe(const struct bpf_sec_def *sec, · line 10414
Quoted 7 lines
tools/lib/bpf/libbpf.c +7
-0+if (n <1 ) { + err = -EINVAL ; +pr_warn ("kprobe name is invalid: %s\n" , func_name); +return libbpf_err_ptr (err); + } +if (opts.retprobe && offset !=0 ) { + err = -EINVAL ;
leaking func here
Quoted 14 lines
tools/lib/bpf/libbpf.c +8
-1+pr_warn ("kretprobes do not support offset specification\n" ); +return libbpf_err_ptr (err); + } + + opts.offset = offset; + link =bpf_program__attach_kprobe_opts (prog, func, &opts); +free (func); +return link; } struct bpf_link *bpf_program__attach_uprobe(struct bpf_program *prog, --2.31.1
Re: [PATCHv4 bpf-next 7/8] libbpf: Allow specification of "kprobe/function+offset"
Quoted 55 lines · Andrii Nakryiko
Quoted 54 lines · Jiri Olsa
From: Alan Maguire <[email protected]>
kprobes can be placed on most instructions in a function, not just entry, and ftrace and bpftrace support the function+offset notification for probe placement. Adding parsing of
func_nameinto func+offset tobpf_program__attach_kprobe()allows the user to specify
SEC("kprobe/bpf_fentry_test5+0x6")...for example, and the offset can be passed to
perf_event_open_probe()to support kprobe attachment.[jolsa: changed original code to use
bpf_program__attach_kprobe_optsand use dynamic allocation in sscanf]Signed-off-by Alan Maguire <[email protected]>Signed-off-by Jiri Olsa <[email protected]>--- tools/lib/bpf/libbpf.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-)tools/lib/bpf/libbpf.c +6
-1@@ -10348,6 +10348,7 @@ static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name,struct bpf_program_attach_kprobe_opts {bool retprobe; +unsigned long offset; }; static struct bpf_link*@@ -10360,7 +10361,7 @@ bpf_program__attach_kprobe_opts(struct bpf_program *prog,int pfd, err; pfd =perf_event_open_probe (false/* uprobe */ , opts->retprobe, func_name, -0 /* offset */ ,-1 /* pid */ ); + opts->offset,-1 /* pid */ );if (pfd <0 ) { pr_warn("prog '%s': failed to create %s '%s' perf event: %s\n", prog->name, opts->retprobe ? "kretprobe" : "kprobe", func_name,@@ -10394,12 +10395,31 @@ static struct bpf_link *attach_kprobe(const struct bpf_sec_def *sec, struct bpf_program *prog) {struct bpf_program_attach_kprobe_opts opts; +unsigned long offset =0 ; +struct bpf_link *link;const char *func_name; +char *func; +int n, err;
Quoted 7 lines
tools/lib/bpf/libbpf.c static struct bpf_link *attach_kprobe(const struct bpf_sec_def *sec, · line 10407
Quoted 5 lines
tools/lib/bpf/libbpf.c +1
-1func_name = prog->sec_name + sec->len; opts.retprobe =strcmp (sec->sec,"kretprobe/" ) ==0 ; -return bpf_program__attach_kprobe_opts (prog, func_name, &opts); + n =sscanf (func_name,"%m[a-zA-Z0-9_.]+%lx" , &func, &offset);could have used %li here to support both +0xabc and +123 forms
ok
Quoted 10 lines
tools/lib/bpf/libbpf.c static struct bpf_link *attach_kprobe(const struct bpf_sec_def *sec, · line 10414
Quoted 8 lines
tools/lib/bpf/libbpf.c +7
-0+if (n <1 ) { + err = -EINVAL ; +pr_warn ("kprobe name is invalid: %s\n" , func_name); +return libbpf_err_ptr (err); + } +if (opts.retprobe && offset !=0 ) { + err = -EINVAL ;leaking func here
ugh.. thanks
jirka
Quoted 17 lines
Quoted 16 lines
tools/lib/bpf/libbpf.c +8
-1+pr_warn ("kretprobes do not support offset specification\n" ); +return libbpf_err_ptr (err); + } + + opts.offset = offset; + link =bpf_program__attach_kprobe_opts (prog, func, &opts); +free (func); +return link; } struct bpf_link *bpf_program__attach_uprobe(struct bpf_program *prog, --2.31.1
PATCH v4 8/8 selftests/bpf: Add test for bpf_get_func_ip in kprobe+offset probe
In reply to Jiri OlsaAdding test for bpf_get_func_ip in kprobe+ofset probe. Because of the offset value it's arch specific, enabling the new test only for x86_64 architecture.
--- .../bpf/prog_tests/get_func_ip_test.c | 18 ++++++++++++++++-- .../selftests/bpf/progs/get_func_ip_test.c | 11 +++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/get_func_ip_test.c +16 -2
@@ -8,10 +8,21 @@ void test_get_func_ip_test(void)__u32 duration =0 , retval;int err, prog_fd; - skel =get_func_ip_test__open_and_load (); -if (!ASSERT_OK_PTR (skel,"get_func_ip_test__open_and_load" )) + skel =get_func_ip_test__open (); +if (!ASSERT_OK_PTR (skel,"get_func_ip_test__open" ))return ; +/* test6 is x86_64 specifc because of the instruction +* offset, disabling it for all other archs +*/ +#ifndef __x86_64__ +bpf_program__set_autoload (skel->progs.test6, false); +#endif + + err =get_func_ip_test__load (skel); +if (!ASSERT_OK (err,"get_func_ip_test__load" )) + goto cleanup; + err =get_func_ip_test__attach (skel);if (!ASSERT_OK (err,"get_func_ip_test__attach" )) goto cleanup;
@@ -33,6 +44,9 @@ void test_get_func_ip_test(void)ASSERT_EQ (skel->bss->test3_result,1 ,"test3_result" );ASSERT_EQ (skel->bss->test4_result,1 ,"test4_result" );ASSERT_EQ (skel->bss->test5_result,1 ,"test5_result" ); +#ifdef __x86_64__ +ASSERT_EQ (skel->bss->test6_result,1 ,"test6_result" ); +#endif cleanup:get_func_ip_test__destroy (skel);
tools/testing/selftests/bpf/progs/get_func_ip_test.c +11 -0
@@ -10,6 +10,7 @@ extern const void bpf_fentry_test2 __ksym; extern const void bpf_fentry_test3 __ksym; extern const void bpf_fentry_test4 __ksym; extern const void bpf_modify_return_test __ksym; +extern const void bpf_fentry_test6 __ksym;__u64 test1_result =0 ;SEC ("fentry/bpf_fentry_test1" )
@@ -60,3 +61,13 @@ int BPF_PROG(test5, int a, int *b, int ret) test5_result = (const void *) addr == &bpf_modify_return_test;return ret; } + +__u64 test6_result =0 ; +SEC("kprobe/bpf_fentry_test6+0x5") +int test6 (struct pt_regs *ctx) +{ +__u64 addr =bpf_get_func_ip (ctx); + + test6_result = (const void *) addr == &bpf_fentry_test6 +5 ; +return 0 ; +}
Signature · 1 line
2.31.1