bpf: Introduce kprobe multi wrapper attach
Subsystems (7)
Jiri Olsa
reviewerv2 cover bpf: Introduce kprobe multi wrapper attach
hi,
adding support to attach both entry and return bpf program on single kprobe multi link. The first RFC patchset is in [0].
Having entry together with return probe for given function is common use case for tetragon, bpftrace and most likely for others.
At the moment if we want both entry and return probe to execute bpf program we need to create two (entry and return probe) links. The link for return probe creates extra entry probe to setup the return probe. The extra entry probe execution could be omitted if we had a way to use just single link for both entry and exit probe.
In addition it's possible to control the execution of the return probe with the return value of the entry bpf program. If the entry program returns 0 the return probe is installed and executed, otherwise it's skip.
v2 changes:
- adding 'kprobe.wrapper' program that is called both for entry and exit probe [Andrii]
- I kept the interface that adds new flag in attr.link_create.kprobe_multi.flags, because I don't see it breaking backward compatibility and it's much simpler than new attach type, I tried to discuss this in [1], but I'm ok to change that if it turns out to be a problem
thanks,
jirka
[0] https://lore.kernel.org/bpf/[email protected]/
[1] https://lore.kernel.org/bpf/ZdhmKQ1_vpCJTS_U@krava/
---
Jiri Olsa (4):
bpf: Add support for kprobe multi wrapper attach
bpf: Add bpf_kprobe_multi_is_return kfunc
libbpf: Add support for kprobe multi wrapper attach selftests/bpf: Add kprobe multi wrapper test
include/uapi/linux/bpf.h | 3 ++- kernel/bpf/btf.c | 3 +++ kernel/trace/bpf_trace.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++------- tools/include/uapi/linux/bpf.h | 3 ++- tools/lib/bpf/libbpf.c | 38 ++++++++++++++++++++++++++++--- tools/lib/bpf/libbpf.h | 4 +++- tools/testing/selftests/bpf/bpf_kfuncs.h | 2 ++ tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c | 49 ++++++++++++++++++++++++++++++++++++++++ tools/testing/selftests/bpf/progs/kprobe_multi_wrapper.c | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 259 insertions(+), 14 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/kprobe_multi_wrapper.c
Jiri Olsa
reviewerRFC PATCH v2 1/4 bpf: Add support for kprobe multi wrapper attach
Adding support to attach bpf program for entry and return probe of the same function. This is common usecase and at the moment it requires to create two kprobe multi links.
Adding new attr.link_create.kprobe_multi.flags value that instructs kernel to attach link program to both entry and exit probe.
It's possible to control execution of the bpf program on return probe simply by returning zero or non zero from the entry bpf program execution to execute or not respectively the bpf program on return probe.
--- include/uapi/linux/bpf.h | 3 ++- kernel/trace/bpf_trace.c | 24 ++++++++++++++++++------ tools/include/uapi/linux/bpf.h | 3 ++- 3 files changed, 22 insertions(+), 8 deletions(-)
include/uapi/linux/bpf.h +2 -1
@@ -1247,7 +1247,8 @@ enum bpf_perf_event_type {* BPF_TRACE_KPROBE_MULTI attach type to create return probe. */ enum { -BPF_F_KPROBE_MULTI_RETURN = (1U <<0 ) +BPF_F_KPROBE_MULTI_RETURN = (1U <<0 ), +BPF_F_KPROBE_MULTI_WRAPPER = (1U <<1 ), };/* link_create.uprobe_multi.flags used in LINK_CREATE command for
kernel/trace/bpf_trace.c +18 -6
@@ -2587,6 +2587,7 @@ struct bpf_kprobe_multi_link {u32 mods_cnt;struct module **mods;u32 flags; +bool is_wrapper; };struct bpf_kprobe_multi_run_ctx {
@@ -2826,10 +2827,11 @@ kprobe_multi_link_handler(struct fprobe *fp, unsigned long fentry_ip, void *data) {struct bpf_kprobe_multi_link *link; +int err; link = container_of(fp, struct bpf_kprobe_multi_link, fp); -kprobe_multi_link_prog_run (link,get_entry_ip (fentry_ip), regs); -return 0 ; + err =kprobe_multi_link_prog_run (link,get_entry_ip (fentry_ip), regs); +return link->is_wrapper ? err :0 ; } static void
@@ -2967,6 +2969,7 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prvoid __user *uaddrs;u64 *cookies =NULL ;void __user *usyms; +bool is_wrapper;int err;/* no support for 32bit archs yet */
@@ -2977,9 +2980,12 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prreturn -EINVAL ; flags = attr->link_create.kprobe_multi.flags; -if (flags & ~BPF_F_KPROBE_MULTI_RETURN ) +if (flags & ~(BPF_F_KPROBE_MULTI_RETURN | +BPF_F_KPROBE_MULTI_WRAPPER ))return -EINVAL ; + is_wrapper = flags &BPF_F_KPROBE_MULTI_WRAPPER ; + uaddrs =u64_to_user_ptr (attr->link_create.kprobe_multi.addrs); usyms =u64_to_user_ptr (attr->link_create.kprobe_multi.syms);if (!!uaddrs == !!usyms)
@@ -3054,15 +3060,21 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prif (err) goto error; -if (flags &BPF_F_KPROBE_MULTI_RETURN ) - link->fp.exit_handler = kprobe_multi_link_exit_handler; -else +if (is_wrapper) { link->fp.entry_handler = kprobe_multi_link_handler; + link->fp.exit_handler = kprobe_multi_link_exit_handler; + }else { +if (flags &BPF_F_KPROBE_MULTI_RETURN ) + link->fp.exit_handler = kprobe_multi_link_exit_handler; +else + link->fp.entry_handler = kprobe_multi_link_handler; + } link->addrs = addrs; link->cookies = cookies; link->cnt = cnt; link->flags = flags; + link->is_wrapper = is_wrapper;if (cookies) {/*
tools/include/uapi/linux/bpf.h +2 -1
@@ -1247,7 +1247,8 @@ enum bpf_perf_event_type {* BPF_TRACE_KPROBE_MULTI attach type to create return probe. */ enum { -BPF_F_KPROBE_MULTI_RETURN = (1U <<0 ) +BPF_F_KPROBE_MULTI_RETURN = (1U <<0 ), +BPF_F_KPROBE_MULTI_WRAPPER = (1U <<1 ), };/* link_create.uprobe_multi.flags used in LINK_CREATE command for
Signature · 1 line
2.43.2
Re: [PATCH RFCv2 bpf-next 1/4] bpf: Add support for kprobe multi wrapper attach
Quoted 39 lines · Jiri Olsa
Adding support to attach bpf program for entry and return probe of the same function. This is common usecase and at the moment it requires to create two kprobe multi links.
Adding new attr.link_create.kprobe_multi.flags value that instructs kernel to attach link program to both entry and exit probe.
It's possible to control execution of the bpf program on return probe simply by returning zero or non zero from the entry bpf program execution to execute or not respectively the bpf program on return probe.
Signed-off-by Jiri Olsa <[email protected]>--- include/uapi/linux/bpf.h | 3 ++- kernel/trace/bpf_trace.c | 24 ++++++++++++++++++------ tools/include/uapi/linux/bpf.h | 3 ++- 3 files changed, 22 insertions(+), 8 deletions(-)include/uapi/linux/bpf.h +2
-1@@ -1247,7 +1247,8 @@ enum bpf_perf_event_type {* BPF_TRACE_KPROBE_MULTI attach type to create return probe. */ enum { -BPF_F_KPROBE_MULTI_RETURN = (1U <<0 ) +BPF_F_KPROBE_MULTI_RETURN = (1U <<0 ), +BPF_F_KPROBE_MULTI_WRAPPER = (1U <<1 ), };/* link_create.uprobe_multi.flags used in LINK_CREATE command for kernel/trace/bpf_trace.c +0
-0
kernel/trace/bpf_trace.c struct bpf_kprobe_multi_link { · line 2590
Quoted 4 lines
kernel/trace/bpf_trace.c +1
-0@@ -2587,6 +2587,7 @@ struct bpf_kprobe_multi_link {u32 mods_cnt;struct module **mods;u32 flags; +bool is_wrapper;
flags should be sufficient for this, why storing redundant bool field?
Quoted 26 lines
kernel/trace/bpf_trace.c +4
-2};struct bpf_kprobe_multi_run_ctx {@@ -2826,10 +2827,11 @@ kprobe_multi_link_handler(struct fprobe *fp, unsigned long fentry_ip, void *data) {struct bpf_kprobe_multi_link *link; +int err; link = container_of(fp, struct bpf_kprobe_multi_link, fp); -kprobe_multi_link_prog_run (link,get_entry_ip (fentry_ip), regs); -return 0 ; + err =kprobe_multi_link_prog_run (link,get_entry_ip (fentry_ip), regs); +return link->is_wrapper ? err :0 ; } static void@@ -2967,6 +2969,7 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prvoid __user *uaddrs;u64 *cookies =NULL ;void __user *usyms; +bool is_wrapper;int err;/* no support for 32bit archs yet */
kernel/trace/bpf_trace.c int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr · line 2984
Quoted 6 lines
kernel/trace/bpf_trace.c +2
-1@@ -2977,9 +2980,12 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prreturn -EINVAL ; flags = attr->link_create.kprobe_multi.flags; -if (flags & ~BPF_F_KPROBE_MULTI_RETURN ) +if (flags & ~(BPF_F_KPROBE_MULTI_RETURN | +BPF_F_KPROBE_MULTI_WRAPPER ))
nit: spaces around | are missing, also keep on a single line?
Quoted 14 lines
kernel/trace/bpf_trace.c +2
-3return -EINVAL ; + is_wrapper = flags &BPF_F_KPROBE_MULTI_WRAPPER ; + uaddrs =u64_to_user_ptr (attr->link_create.kprobe_multi.addrs); usyms =u64_to_user_ptr (attr->link_create.kprobe_multi.syms);if (!!uaddrs == !!usyms)@@ -3054,15 +3060,21 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prif (err) goto error; -if (flags &BPF_F_KPROBE_MULTI_RETURN ) - link->fp.exit_handler = kprobe_multi_link_exit_handler; -else
kernel/trace/bpf_trace.c int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr · line 3072
Quoted 10 lines
kernel/trace/bpf_trace.c +8
-0+if (is_wrapper) { link->fp.entry_handler = kprobe_multi_link_handler; + link->fp.exit_handler = kprobe_multi_link_exit_handler; + }else { +if (flags &BPF_F_KPROBE_MULTI_RETURN ) + link->fp.exit_handler = kprobe_multi_link_exit_handler; +else + link->fp.entry_handler = kprobe_multi_link_handler; + }
how about:
if (!(flags & BPF_F_KPROBE_MULTI_RETURN))
link->fp.entry_handler = kprobe_multi_link_handler;
if (flags & (BPF_F_KPROBE_MULTI_RETURN | BPF_F_KPROBE_MULTI_WRAPPER))
link->fp.exit_handler = kprobe_multi_link_exit_handler;Quoted 25 lines
kernel/trace/bpf_trace.c +1
-0link->addrs = addrs; link->cookies = cookies; link->cnt = cnt; link->flags = flags; + link->is_wrapper = is_wrapper;if (cookies) {/* tools/include/uapi/linux/bpf.h +2
-2@@ -1247,7 +1247,8 @@ enum bpf_perf_event_type {* BPF_TRACE_KPROBE_MULTI attach type to create return probe. */ enum { -BPF_F_KPROBE_MULTI_RETURN = (1U <<0 ) +BPF_F_KPROBE_MULTI_RETURN = (1U <<0 ), +BPF_F_KPROBE_MULTI_WRAPPER = (1U <<1 ), };/* link_create.uprobe_multi.flags used in LINK_CREATE command for -- 2.43.2
Re: [PATCH RFCv2 bpf-next 1/4] bpf: Add support for kprobe multi wrapper attach
Quoted 34 lines · Andrii Nakryiko
Quoted 33 lines · Jiri Olsa
Adding support to attach bpf program for entry and return probe of the same function. This is common usecase and at the moment it requires to create two kprobe multi links.
Adding new attr.link_create.kprobe_multi.flags value that instructs kernel to attach link program to both entry and exit probe.
It's possible to control execution of the bpf program on return probe simply by returning zero or non zero from the entry bpf program execution to execute or not respectively the bpf program on return probe.
Signed-off-by Jiri Olsa <[email protected]>--- include/uapi/linux/bpf.h | 3 ++- kernel/trace/bpf_trace.c | 24 ++++++++++++++++++------ tools/include/uapi/linux/bpf.h | 3 ++- 3 files changed, 22 insertions(+), 8 deletions(-)include/uapi/linux/bpf.h +2
-1@@ -1247,7 +1247,8 @@ enum bpf_perf_event_type {* BPF_TRACE_KPROBE_MULTI attach type to create return probe. */ enum { -BPF_F_KPROBE_MULTI_RETURN = (1U <<0 ) +BPF_F_KPROBE_MULTI_RETURN = (1U <<0 ), +BPF_F_KPROBE_MULTI_WRAPPER = (1U <<1 ), };
Quoted 12 lines
kernel/trace/bpf_trace.c struct bpf_kprobe_multi_link { · line 2590
Quoted 10 lines
include/uapi/linux/bpf.h +0
-0/* link_create.uprobe_multi.flags used in LINK_CREATE command for kernel/trace/bpf_trace.c +1
-0@@ -2587,6 +2587,7 @@ struct bpf_kprobe_multi_link {u32 mods_cnt;struct module **mods;u32 flags; +bool is_wrapper;flags should be sufficient for this, why storing redundant bool field?
true
Quoted 29 lines
Quoted 29 lines
kernel/trace/bpf_trace.c +4
-2};struct bpf_kprobe_multi_run_ctx {@@ -2826,10 +2827,11 @@ kprobe_multi_link_handler(struct fprobe *fp, unsigned long fentry_ip, void *data) {struct bpf_kprobe_multi_link *link; +int err; link = container_of(fp, struct bpf_kprobe_multi_link, fp); -kprobe_multi_link_prog_run (link,get_entry_ip (fentry_ip), regs); -return 0 ; + err =kprobe_multi_link_prog_run (link,get_entry_ip (fentry_ip), regs); +return link->is_wrapper ? err :0 ; } static void@@ -2967,6 +2969,7 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prvoid __user *uaddrs;u64 *cookies =NULL ;void __user *usyms; +bool is_wrapper;int err;/* no support for 32bit archs yet */ @@ -2977,9 +2980,12 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prreturn -EINVAL ;
Quoted 6 lines
kernel/trace/bpf_trace.c int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr · line 2984
Quoted 4 lines
kernel/trace/bpf_trace.c +2
-1flags = attr->link_create.kprobe_multi.flags; -if (flags & ~BPF_F_KPROBE_MULTI_RETURN ) +if (flags & ~(BPF_F_KPROBE_MULTI_RETURN | +BPF_F_KPROBE_MULTI_WRAPPER ))nit: spaces around | are missing, also keep on a single line?
ok
Quoted 28 lines
Quoted 15 lines
kernel/trace/bpf_trace.c +2
-3return -EINVAL ; + is_wrapper = flags &BPF_F_KPROBE_MULTI_WRAPPER ; + uaddrs =u64_to_user_ptr (attr->link_create.kprobe_multi.addrs); usyms =u64_to_user_ptr (attr->link_create.kprobe_multi.syms);if (!!uaddrs == !!usyms)@@ -3054,15 +3060,21 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prif (err) goto error; -if (flags &BPF_F_KPROBE_MULTI_RETURN ) - link->fp.exit_handler = kprobe_multi_link_exit_handler; -else kernel/trace/bpf_trace.c int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr · line 3072
Quoted 10 lines
kernel/trace/bpf_trace.c +8
-0+if (is_wrapper) { link->fp.entry_handler = kprobe_multi_link_handler; + link->fp.exit_handler = kprobe_multi_link_exit_handler; + }else { +if (flags &BPF_F_KPROBE_MULTI_RETURN ) + link->fp.exit_handler = kprobe_multi_link_exit_handler; +else + link->fp.entry_handler = kprobe_multi_link_handler; + }how about:
Quoted 4 lines
if (!(flags &
BPF_F_KPROBE_MULTI_RETURN))link->fp.entry_handler = kprobe_multi_link_handler; if (flags & (BPF_F_KPROBE_MULTI_RETURN | BPF_F_KPROBE_MULTI_WRAPPER)) link->fp.exit_handler = kprobe_multi_link_exit_handler;
I have another changes on top of that, which add more handlers, so I guess I wanted to keep it more explicit, but your suggestion is simpler, will change
thanks,
jirka
Quoted 27 lines
Quoted 27 lines
kernel/trace/bpf_trace.c +1
-0link->addrs = addrs; link->cookies = cookies; link->cnt = cnt; link->flags = flags; + link->is_wrapper = is_wrapper;if (cookies) {/* tools/include/uapi/linux/bpf.h +2
-2@@ -1247,7 +1247,8 @@ enum bpf_perf_event_type {* BPF_TRACE_KPROBE_MULTI attach type to create return probe. */ enum { -BPF_F_KPROBE_MULTI_RETURN = (1U <<0 ) +BPF_F_KPROBE_MULTI_RETURN = (1U <<0 ), +BPF_F_KPROBE_MULTI_WRAPPER = (1U <<1 ), };/* link_create.uprobe_multi.flags used in LINK_CREATE command for -- 2.43.2
Jiri Olsa
reviewerRFC PATCH v2 2/4 bpf: Add bpf_kprobe_multi_is_return kfunc
In reply to Jiri OlsaAdding bpf_kprobe_multi_is_return kfunc that returns true if the bpf program is executed from the exit probe of the kprobe multi link attached in wrapper mode. It returns false otherwise.
Adding new kprobe hook for kprobe program type.
--- kernel/bpf/btf.c | 3 +++ kernel/trace/bpf_trace.c | 49 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 49 insertions(+), 3 deletions(-)
kernel/bpf/btf.c +3 -0
@@ -218,6 +218,7 @@ enum btf_kfunc_hook {BTF_KFUNC_HOOK_SOCKET_FILTER ,BTF_KFUNC_HOOK_LWT ,BTF_KFUNC_HOOK_NETFILTER , +BTF_KFUNC_HOOK_KPROBE , BTF_KFUNC_HOOK_MAX, };
@@ -8112,6 +8113,8 @@ static int bpf_prog_type_to_kfunc_hook(enum bpf_prog_type prog_type)return BTF_KFUNC_HOOK_LWT ;case BPF_PROG_TYPE_NETFILTER :return BTF_KFUNC_HOOK_NETFILTER ; +case BPF_PROG_TYPE_KPROBE : +return BTF_KFUNC_HOOK_KPROBE ;default :return BTF_KFUNC_HOOK_MAX ; }
kernel/trace/bpf_trace.c +46 -3
83 lines not on this page · raw message
Signature · 1 line
2.43.2
Re: [PATCH RFCv2 bpf-next 2/4] bpf: Add bpf_kprobe_multi_is_return kfunc
Quoted 80 lines · Jiri Olsa
Adding
bpf_kprobe_multi_is_returnkfunc that returns true if the bpf program is executed from the exit probe of the kprobe multi link attached in wrapper mode. It returns false otherwise.Adding new kprobe hook for kprobe program type.
Signed-off-by Jiri Olsa <[email protected]>--- kernel/bpf/btf.c | 3 +++ kernel/trace/bpf_trace.c | 49 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 49 insertions(+), 3 deletions(-)kernel/bpf/btf.c +3
-0@@ -218,6 +218,7 @@ enum btf_kfunc_hook {BTF_KFUNC_HOOK_SOCKET_FILTER ,BTF_KFUNC_HOOK_LWT ,BTF_KFUNC_HOOK_NETFILTER , +BTF_KFUNC_HOOK_KPROBE , BTF_KFUNC_HOOK_MAX, };@@ -8112,6 +8113,8 @@ static int bpf_prog_type_to_kfunc_hook(enum bpf_prog_type prog_type)return BTF_KFUNC_HOOK_LWT ;case BPF_PROG_TYPE_NETFILTER :return BTF_KFUNC_HOOK_NETFILTER ; +case BPF_PROG_TYPE_KPROBE : +return BTF_KFUNC_HOOK_KPROBE ;default :return BTF_KFUNC_HOOK_MAX ; }kernel/trace/bpf_trace.c +6
-3@@ -2594,6 +2594,7 @@ struct bpf_kprobe_multi_run_ctx {struct bpf_run_ctx run_ctx;struct bpf_kprobe_multi_link *link;unsigned long entry_ip; +bool is_return; };struct user_syms {@@ -2793,11 +2794,13 @@ static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx)static int kprobe_multi_link_prog_run (struct bpf_kprobe_multi_link *link, -unsigned long entry_ip,struct pt_regs *regs) +unsigned long entry_ip,struct pt_regs *regs, +bool is_return) {struct bpf_kprobe_multi_run_ctx run_ctx = { .link = link, .entry_ip = entry_ip, + .is_return = is_return, };struct bpf_run_ctx *old_run_ctx;int err;@@ -2830,7 +2833,7 @@ kprobe_multi_link_handler(struct fprobe *fp, unsigned long fentry_ip,int err; link = container_of(fp, struct bpf_kprobe_multi_link, fp); - err =kprobe_multi_link_prog_run (link,get_entry_ip (fentry_ip), regs); + err =kprobe_multi_link_prog_run (link,get_entry_ip (fentry_ip), regs, false);return link->is_wrapper ? err :0 ; }@@ -2842,7 +2845,7 @@ kprobe_multi_link_exit_handler(struct fprobe *fp, unsigned long fentry_ip,struct bpf_kprobe_multi_link *link; link = container_of(fp, struct bpf_kprobe_multi_link, fp); -kprobe_multi_link_prog_run (link,get_entry_ip (fentry_ip), regs); +kprobe_multi_link_prog_run (link,get_entry_ip (fentry_ip), regs, true); }static int symbols_cmp_r (const void *a,const void *b,const void *priv)
kernel/trace/bpf_trace.c int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr · line 3120
Quoted 7 lines
kernel/trace/bpf_trace.c +4
-0@@ -3111,6 +3114,46 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prkvfree (cookies);return err; } + +__bpf_kfunc_start_defs (); + +__bpf_kfunc bool bpf_kprobe_multi_is_return(void)
and for uprobes we'll have bpf_uprobe_multi_is_return?...
BTW, have you tried implementing a "session cookie" idea?
Quoted 42 lines
kernel/trace/bpf_trace.c +36
-1+{ +struct bpf_kprobe_multi_run_ctx *run_ctx; + + run_ctx = container_of(current->bpf_ctx, struct bpf_kprobe_multi_run_ctx, run_ctx); +return run_ctx->is_return; +} + +__bpf_kfunc_end_defs (); + +BTF_KFUNCS_START (kprobe_multi_kfunc_set_ids) +BTF_ID_FLAGS (func ,bpf_kprobe_multi_is_return ) +BTF_KFUNCS_END (kprobe_multi_kfunc_set_ids) + +static int bpf_kprobe_multi_filter(const struct bpf_prog *prog, u32 kfunc_id) +{ +if (!btf_id_set8_contains (&kprobe_multi_kfunc_set_ids, kfunc_id)) +return 0 ; + +if (prog->expected_attach_type !=BPF_TRACE_KPROBE_MULTI ) +return -EACCES ; + +return 0 ; +} + +static const struct btf_kfunc_id_set bpf_kprobe_multi_kfunc_set = { + .owner =THIS_MODULE , + .set = &kprobe_multi_kfunc_set_ids, + .filter = bpf_kprobe_multi_filter, +}; + +static int __init bpf_kprobe_multi_kfuncs_init (void ) +{ +return register_btf_kfunc_id_set (BPF_PROG_TYPE_KPROBE , &bpf_kprobe_multi_kfunc_set); +} + +late_initcall (bpf_kprobe_multi_kfuncs_init);#else /* !CONFIG_FPROBE */ int bpf_kprobe_multi_link_attach (const union bpf_attr *attr,struct bpf_prog *prog) { --2.43.2
Re: [PATCH RFCv2 bpf-next 2/4] bpf: Add bpf_kprobe_multi_is_return kfunc
On Wed, Feb 28, 2024 at 05:23:45PM -0800, Andrii Nakryiko wrote:
SNIP
Quoted 30 lines
Quoted 30 lines
static int
kprobe_multi_link_prog_run(structbpf_kprobe_multi_link*link,kernel/trace/bpf_trace.c +5
-3- unsigned long entry_ip, struct pt_regs *regs) + unsigned long entry_ip, struct pt_regs *regs, + bool is_return) { struct bpf_kprobe_multi_run_ctx run_ctx = { .link = link, .entry_ip = entry_ip, + .is_return = is_return, };struct bpf_run_ctx *old_run_ctx;int err;@@ -2830,7 +2833,7 @@ kprobe_multi_link_handler(struct fprobe *fp, unsigned long fentry_ip,int err; link = container_of(fp, struct bpf_kprobe_multi_link, fp); - err =kprobe_multi_link_prog_run (link,get_entry_ip (fentry_ip), regs); + err =kprobe_multi_link_prog_run (link,get_entry_ip (fentry_ip), regs, false);return link->is_wrapper ? err :0 ; }@@ -2842,7 +2845,7 @@ kprobe_multi_link_exit_handler(struct fprobe *fp, unsigned long fentry_ip,struct bpf_kprobe_multi_link *link; link = container_of(fp, struct bpf_kprobe_multi_link, fp); -kprobe_multi_link_prog_run (link,get_entry_ip (fentry_ip), regs); +kprobe_multi_link_prog_run (link,get_entry_ip (fentry_ip), regs, true); }
Quoted 11 lines
kernel/trace/bpf_trace.c int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr · line 3120
Quoted 9 lines
kernel/trace/bpf_trace.c +4
-0static int symbols_cmp_r (const void *a,const void *b,const void *priv)@@ -3111,6 +3114,46 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prkvfree (cookies);return err; } + +__bpf_kfunc_start_defs (); + +__bpf_kfunc bool bpf_kprobe_multi_is_return(void)and for uprobes we'll have
bpf_uprobe_multi_is_return?...
yes, but now I'm thinking maybe we could also have 'session' api and have single 'bpf_session_is_return' because both kprobe and uprobe are KPROBE program type.. and align it together with other session kfuncs:
bpf_session_is_return
bpf_session_set_cookie
bpf_session_get_cookie
Quoted 2 lines
BTW, have you tried implementing a "session cookie" idea?
yep, with a little fix [0] it's working on top of Masami's 'fprobe over fgraph' changes, you can check last 2 patches in [1] .. I did not do this on top of the current fprobe/rethook kernel code, because it seems it's about to go away
I still need to implement that on top of uprobes and I will send rfc, so we can see all of it and discuss the interface
jirka
[0] https://lore.kernel.org/bpf/ZdyKaRiI-PnG80Q0@krava/
[1] https://git.kernel.org/pub/scm/linux/kernel/git/jolsa/perf.git/log/?h=bpf/session_data
Quoted 44 lines
Quoted 44 lines
kernel/trace/bpf_trace.c +36
-1+{ +struct bpf_kprobe_multi_run_ctx *run_ctx; + + run_ctx = container_of(current->bpf_ctx, struct bpf_kprobe_multi_run_ctx, run_ctx); +return run_ctx->is_return; +} + +__bpf_kfunc_end_defs (); + +BTF_KFUNCS_START (kprobe_multi_kfunc_set_ids) +BTF_ID_FLAGS (func ,bpf_kprobe_multi_is_return ) +BTF_KFUNCS_END (kprobe_multi_kfunc_set_ids) + +static int bpf_kprobe_multi_filter(const struct bpf_prog *prog, u32 kfunc_id) +{ +if (!btf_id_set8_contains (&kprobe_multi_kfunc_set_ids, kfunc_id)) +return 0 ; + +if (prog->expected_attach_type !=BPF_TRACE_KPROBE_MULTI ) +return -EACCES ; + +return 0 ; +} + +static const struct btf_kfunc_id_set bpf_kprobe_multi_kfunc_set = { + .owner =THIS_MODULE , + .set = &kprobe_multi_kfunc_set_ids, + .filter = bpf_kprobe_multi_filter, +}; + +static int __init bpf_kprobe_multi_kfuncs_init (void ) +{ +return register_btf_kfunc_id_set (BPF_PROG_TYPE_KPROBE , &bpf_kprobe_multi_kfunc_set); +} + +late_initcall (bpf_kprobe_multi_kfuncs_init);#else /* !CONFIG_FPROBE */ int bpf_kprobe_multi_link_attach (const union bpf_attr *attr,struct bpf_prog *prog) { --2.43.2
Re: [PATCH RFCv2 bpf-next 2/4] bpf: Add bpf_kprobe_multi_is_return kfunc
Quoted 47 lines · Jiri Olsa
On Wed, Feb 28, 2024 at 05:23:45PM -0800, Andrii Nakryiko wrote:
SNIP
Quoted 30 lines
Quoted 30 lines
static int
kprobe_multi_link_prog_run(structbpf_kprobe_multi_link*link,kernel/trace/bpf_trace.c +5
-3- unsigned long entry_ip, struct pt_regs *regs) + unsigned long entry_ip, struct pt_regs *regs, + bool is_return) { struct bpf_kprobe_multi_run_ctx run_ctx = { .link = link, .entry_ip = entry_ip, + .is_return = is_return, };struct bpf_run_ctx *old_run_ctx;int err;@@ -2830,7 +2833,7 @@ kprobe_multi_link_handler(struct fprobe *fp, unsigned long fentry_ip,int err; link = container_of(fp, struct bpf_kprobe_multi_link, fp); - err =kprobe_multi_link_prog_run (link,get_entry_ip (fentry_ip), regs); + err =kprobe_multi_link_prog_run (link,get_entry_ip (fentry_ip), regs, false);return link->is_wrapper ? err :0 ; }@@ -2842,7 +2845,7 @@ kprobe_multi_link_exit_handler(struct fprobe *fp, unsigned long fentry_ip,struct bpf_kprobe_multi_link *link; link = container_of(fp, struct bpf_kprobe_multi_link, fp); -kprobe_multi_link_prog_run (link,get_entry_ip (fentry_ip), regs); +kprobe_multi_link_prog_run (link,get_entry_ip (fentry_ip), regs, true); }Quoted 11 lines
kernel/trace/bpf_trace.c int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr · line 3120
Quoted 9 lines
kernel/trace/bpf_trace.c +4
-0static int symbols_cmp_r (const void *a,const void *b,const void *priv)@@ -3111,6 +3114,46 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prkvfree (cookies);return err; } + +__bpf_kfunc_start_defs (); + +__bpf_kfunc bool bpf_kprobe_multi_is_return(void)and for uprobes we'll have
bpf_uprobe_multi_is_return?...
Quoted 9 lines
yes, but now I'm thinking maybe we could also have 'session' api and have single '
bpf_session_is_return' because both kprobe and uprobe are KPROBE program type.. and align it together with other session kfuncs:
bpf_session_is_return
bpf_session_set_cookie
bpf_session_get_cookie
We can do that. But I was thinking more of a
u64 *bpf_session_cookie()
which would return a read/write pointer that BPF program can manipulate. Instead of doing two calls (get_cookie + set_cookie), it would be one call. Is there any benefit to having separate set/get cookie calls?
Quoted 6 lines
Quoted 2 lines
BTW, have you tried implementing a "session cookie" idea?
yep, with a little fix [0] it's working on top of Masami's 'fprobe over fgraph' changes, you can check last 2 patches in [1] .. I did not do this on top of the current fprobe/rethook kernel code, because it seems it's about to go away
do you know what is the timeline for fprobe over fgraph work to be finished?
Quoted 4 lines
I still need to implement that on top of uprobes and I will send rfc, so we can see all of it and discuss the interface
great, yeah, I think the session cookie idea should go in at the same time, if possible, so that we can assume it is supported for new [ku]probe.wrapper programs.
Quoted 50 lines
jirka
[0] https://lore.kernel.org/bpf/ZdyKaRiI-PnG80Q0@krava/
[1] https://git.kernel.org/pub/scm/linux/kernel/git/jolsa/perf.git/log/?h=bpf/session_dataQuoted 44 lines
Quoted 44 lines
kernel/trace/bpf_trace.c +36
-1+{ +struct bpf_kprobe_multi_run_ctx *run_ctx; + + run_ctx = container_of(current->bpf_ctx, struct bpf_kprobe_multi_run_ctx, run_ctx); +return run_ctx->is_return; +} + +__bpf_kfunc_end_defs (); + +BTF_KFUNCS_START (kprobe_multi_kfunc_set_ids) +BTF_ID_FLAGS (func ,bpf_kprobe_multi_is_return ) +BTF_KFUNCS_END (kprobe_multi_kfunc_set_ids) + +static int bpf_kprobe_multi_filter(const struct bpf_prog *prog, u32 kfunc_id) +{ +if (!btf_id_set8_contains (&kprobe_multi_kfunc_set_ids, kfunc_id)) +return 0 ; + +if (prog->expected_attach_type !=BPF_TRACE_KPROBE_MULTI ) +return -EACCES ; + +return 0 ; +} + +static const struct btf_kfunc_id_set bpf_kprobe_multi_kfunc_set = { + .owner =THIS_MODULE , + .set = &kprobe_multi_kfunc_set_ids, + .filter = bpf_kprobe_multi_filter, +}; + +static int __init bpf_kprobe_multi_kfuncs_init (void ) +{ +return register_btf_kfunc_id_set (BPF_PROG_TYPE_KPROBE , &bpf_kprobe_multi_kfunc_set); +} + +late_initcall (bpf_kprobe_multi_kfuncs_init);#else /* !CONFIG_FPROBE */ int bpf_kprobe_multi_link_attach (const union bpf_attr *attr,struct bpf_prog *prog) { --2.43.2
Re: [PATCH RFCv2 bpf-next 2/4] bpf: Add bpf_kprobe_multi_is_return kfunc
On Fri, Mar 01, 2024 at 10:01:16AM -0800, Andrii Nakryiko wrote:
sNIP
Quoted 14 lines
Quoted 4 lines
Quoted 3 lines
Quoted 1 line
+__bpf_kfunc bool
bpf_kprobe_multi_is_return(void)and for uprobes we'll have
bpf_uprobe_multi_is_return?...Quoted 9 lines
yes, but now I'm thinking maybe we could also have 'session' api and have single '
bpf_session_is_return' because both kprobe and uprobe are KPROBE program type.. and align it together with other session kfuncs:
bpf_session_is_return
bpf_session_set_cookie
bpf_session_get_cookie
Quoted 8 lines
We can do that. But I was thinking more of a
u64
*bpf_session_cookie()which would return a read/write pointer that BPF program can manipulate. Instead of doing two calls (
get_cookie+set_cookie), it would be one call. Is there any benefit to having separate set/get cookie calls?
ok, that would be easier, will check on that
Quoted 9 lines
Quoted 7 lines
Quoted 3 lines
BTW, have you tried implementing a "session cookie" idea?
yep, with a little fix [0] it's working on top of Masami's 'fprobe over fgraph' changes, you can check last 2 patches in [1] .. I did not do this on top of the current fprobe/rethook kernel code, because it seems it's about to go away
do you know what is the timeline for fprobe over fgraph work to be finished?
good question ;-) Masami, any idea?
fwiw there's new version needed for [1] fix
[1] https://lore.kernel.org/bpf/ZdyKaRiI-PnG80Q0@krava/
Quoted 9 lines
Quoted 5 lines
I still need to implement that on top of uprobes and I will send rfc, so we can see all of it and discuss the interface
great, yeah, I think the session cookie idea should go in at the same time, if possible, so that we can assume it is supported for new [ku]probe.wrapper programs.
makes sense, even though with new kfuncs detection stuff, it will be easy to find out
jirka
Jiri Olsa
reviewerRFC PATCH v2 3/4 libbpf: Add support for kprobe multi wrapper attach
In reply to Jiri OlsaAdding support for specify wrapper mode in bpf_kprobe_multi_opts struct object and new bpf program loader section:
SEC("kprobe.wrapper/bpf_fentry_test*")
to load program as kprobe multi wrapper.
--- tools/lib/bpf/libbpf.c | 38 +++++++++++++++++++++++++++++++++++--- tools/lib/bpf/libbpf.h | 4 +++- 2 files changed, 38 insertions(+), 4 deletions(-)
tools/lib/bpf/libbpf.c +35 -3
@@ -8944,6 +8944,7 @@ static int attach_tp(const struct bpf_program *prog, long cookie, struct bpf_linstatic int attach_raw_tp (const struct bpf_program *prog,long cookie,struct bpf_link **link);static int attach_trace (const struct bpf_program *prog,long cookie,struct bpf_link **link);static int attach_kprobe_multi (const struct bpf_program *prog,long cookie,struct bpf_link **link); +static int attach_kprobe_wrapper (const struct bpf_program *prog,long cookie,struct bpf_link **link);static int attach_uprobe_multi (const struct bpf_program *prog,long cookie,struct bpf_link **link);static int attach_lsm (const struct bpf_program *prog,long cookie,struct bpf_link **link);static int attach_iter (const struct bpf_program *prog,long cookie,struct bpf_link **link);
@@ -8960,6 +8961,7 @@ static const struct bpf_sec_def section_defs[] = {SEC_DEF ("uretprobe.s+" ,KPROBE ,0 ,SEC_SLEEPABLE , attach_uprobe),SEC_DEF ("kprobe.multi+" ,KPROBE ,BPF_TRACE_KPROBE_MULTI ,SEC_NONE , attach_kprobe_multi),SEC_DEF ("kretprobe.multi+" ,KPROBE ,BPF_TRACE_KPROBE_MULTI ,SEC_NONE , attach_kprobe_multi), +SEC_DEF ("kprobe.wrapper+" ,KPROBE ,BPF_TRACE_KPROBE_MULTI ,SEC_NONE , attach_kprobe_wrapper),SEC_DEF ("uprobe.multi+" ,KPROBE ,BPF_TRACE_UPROBE_MULTI ,SEC_NONE , attach_uprobe_multi),SEC_DEF ("uretprobe.multi+" ,KPROBE ,BPF_TRACE_UPROBE_MULTI ,SEC_NONE , attach_uprobe_multi), SEC_DEF("uprobe.multi.s+", KPROBE, BPF_TRACE_UPROBE_MULTI, SEC_SLEEPABLE, attach_uprobe_multi),
@@ -11034,7 +11036,7 @@ bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,int err, link_fd, prog_fd;const __u64 *cookies;const char **syms; -bool retprobe; +__u32 flags =0 ;size_t cnt;if (!OPTS_VALID (opts, bpf_kprobe_multi_opts))
@@ -11065,13 +11067,16 @@ bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog, cnt = res.cnt; } - retprobe =OPTS_GET (opts, retprobe, false); +if (OPTS_GET (opts, retprobe, false)) + flags |=BPF_F_KPROBE_MULTI_RETURN ; +if (OPTS_GET (opts, wrapper, false)) + flags |=BPF_F_KPROBE_MULTI_WRAPPER ; lopts.kprobe_multi.syms = syms; lopts.kprobe_multi.addrs = addrs; lopts.kprobe_multi.cookies = cookies; lopts.kprobe_multi.cnt = cnt; - lopts.kprobe_multi.flags = retprobe ?BPF_F_KPROBE_MULTI_RETURN :0 ; + lopts.kprobe_multi.flags = flags; link =calloc (1 ,sizeof (*link));if (!link) {
@@ -11187,6 +11192,33 @@ static int attach_kprobe_multi(const struct bpf_program *prog, long cookie, strureturn libbpf_get_error (*link); } +static int attach_kprobe_wrapper (const struct bpf_program *prog,long cookie,struct bpf_link **link) +{ +LIBBPF_OPTS (bpf_kprobe_multi_opts, opts, + .wrapper = true, + ); +const char *spec; +char *pattern; +int n; + + *link =NULL ; + +/* no auto-attach for SEC("kprobe.wrapper") */ +if (strcmp (prog->sec_name,"kprobe.wrapper" ) ==0 ) +return 0 ; + + spec = prog->sec_name +sizeof ("kprobe.wrapper/" ) -1 ; + n =sscanf (spec,"%m[a-zA-Z0-9_.*?]" , &pattern); +if (n <1 ) { +pr_warn ("kprobe wrapper pattern is invalid: %s\n" , pattern); +return -EINVAL ; + } + + *link =bpf_program__attach_kprobe_multi_opts (prog, pattern, &opts); +free (pattern); +return libbpf_get_error (*link); +} +static int attach_uprobe_multi (const struct bpf_program *prog,long cookie,struct bpf_link **link) {char *probe_type =NULL , *binary_path =NULL , *func_name =NULL ;
tools/lib/bpf/libbpf.h +3 -1
@@ -539,10 +539,12 @@ struct bpf_kprobe_multi_opts {size_t cnt;/* create return kprobes */ bool retprobe; +/* create wrapper kprobes */ +bool wrapper; size_t :0; }; -#define bpf_kprobe_multi_opts__last_field retprobe +#define bpf_kprobe_multi_opts__last_field wrapper LIBBPF_API struct bpf_link * bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,
Signature · 1 line
2.43.2
Re: [PATCH RFCv2 bpf-next 3/4] libbpf: Add support for kprobe multi wrapper attach
Quoted 44 lines · Jiri Olsa
Adding support for specify wrapper mode in
bpf_kprobe_multi_optsstruct object and new bpf program loader section:
SEC("kprobe.wrapper/bpf_fentry_test*")to load program as kprobe multi wrapper.
Signed-off-by Jiri Olsa <[email protected]>--- tools/lib/bpf/libbpf.c | 38 +++++++++++++++++++++++++++++++++++--- tools/lib/bpf/libbpf.h | 4 +++- 2 files changed, 38 insertions(+), 4 deletions(-)tools/lib/bpf/libbpf.c +3
-1@@ -8944,6 +8944,7 @@ static int attach_tp(const struct bpf_program *prog, long cookie, struct bpf_linstatic int attach_raw_tp (const struct bpf_program *prog,long cookie,struct bpf_link **link);static int attach_trace (const struct bpf_program *prog,long cookie,struct bpf_link **link);static int attach_kprobe_multi (const struct bpf_program *prog,long cookie,struct bpf_link **link); +static int attach_kprobe_wrapper (const struct bpf_program *prog,long cookie,struct bpf_link **link);static int attach_uprobe_multi (const struct bpf_program *prog,long cookie,struct bpf_link **link);static int attach_lsm (const struct bpf_program *prog,long cookie,struct bpf_link **link);static int attach_iter (const struct bpf_program *prog,long cookie,struct bpf_link **link);@@ -8960,6 +8961,7 @@ static const struct bpf_sec_def section_defs[] = {SEC_DEF ("uretprobe.s+" ,KPROBE ,0 ,SEC_SLEEPABLE , attach_uprobe),SEC_DEF ("kprobe.multi+" ,KPROBE ,BPF_TRACE_KPROBE_MULTI ,SEC_NONE , attach_kprobe_multi),SEC_DEF ("kretprobe.multi+" ,KPROBE ,BPF_TRACE_KPROBE_MULTI ,SEC_NONE , attach_kprobe_multi), +SEC_DEF ("kprobe.wrapper+" ,KPROBE ,BPF_TRACE_KPROBE_MULTI ,SEC_NONE , attach_kprobe_wrapper),SEC_DEF ("uprobe.multi+" ,KPROBE ,BPF_TRACE_UPROBE_MULTI ,SEC_NONE , attach_uprobe_multi),SEC_DEF ("uretprobe.multi+" ,KPROBE ,BPF_TRACE_UPROBE_MULTI ,SEC_NONE , attach_uprobe_multi), SEC_DEF("uprobe.multi.s+", KPROBE, BPF_TRACE_UPROBE_MULTI, SEC_SLEEPABLE, attach_uprobe_multi),@@ -11034,7 +11036,7 @@ bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,int err, link_fd, prog_fd;const __u64 *cookies;const char **syms; -bool retprobe; +__u32 flags =0 ;size_t cnt;if (!OPTS_VALID (opts, bpf_kprobe_multi_opts))
tools/lib/bpf/libbpf.c bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog, · line 11073
Quoted 8 lines
tools/lib/bpf/libbpf.c +4
-1@@ -11065,13 +11067,16 @@ bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog, cnt = res.cnt; } - retprobe =OPTS_GET (opts, retprobe, false); +if (OPTS_GET (opts, retprobe, false)) + flags |=BPF_F_KPROBE_MULTI_RETURN ; +if (OPTS_GET (opts, wrapper, false)) + flags |=BPF_F_KPROBE_MULTI_WRAPPER ;
probably error out if both retprobe and wrapper are set?
Quoted 11 lines
tools/lib/bpf/libbpf.c +1
-1lopts.kprobe_multi.syms = syms; lopts.kprobe_multi.addrs = addrs; lopts.kprobe_multi.cookies = cookies; lopts.kprobe_multi.cnt = cnt; - lopts.kprobe_multi.flags = retprobe ?BPF_F_KPROBE_MULTI_RETURN :0 ; + lopts.kprobe_multi.flags = flags; link =calloc (1 ,sizeof (*link));if (!link) {
tools/lib/bpf/libbpf.c static int attach_kprobe_multi(const struct bpf_program *prog, long cookie, stru · line 11199
Quoted 8 lines
tools/lib/bpf/libbpf.c +5
-0@@ -11187,6 +11192,33 @@ static int attach_kprobe_multi(const struct bpf_program *prog, long cookie, strureturn libbpf_get_error (*link); } +static int attach_kprobe_wrapper (const struct bpf_program *prog,long cookie,struct bpf_link **link) +{ +LIBBPF_OPTS (bpf_kprobe_multi_opts, opts, + .wrapper = true, + );
nit: keep on a single line?
Quoted 9 lines
tools/lib/bpf/libbpf.c +9
-0+const char *spec; +char *pattern; +int n; + + *link =NULL ; + +/* no auto-attach for SEC("kprobe.wrapper") */ +if (strcmp (prog->sec_name,"kprobe.wrapper" ) ==0 ) +return 0 ;
tools/lib/bpf/libbpf.c static int attach_kprobe_multi(const struct bpf_program *prog, long cookie, stru · line 11218
Quoted 10 lines
tools/lib/bpf/libbpf.c +10
-0+ + spec = prog->sec_name +sizeof ("kprobe.wrapper/" ) -1 ; + n =sscanf (spec,"%m[a-zA-Z0-9_.*?]" , &pattern); +if (n <1 ) { +pr_warn ("kprobe wrapper pattern is invalid: %s\n" , pattern); +return -EINVAL ; + } + + *link =bpf_program__attach_kprobe_multi_opts (prog, pattern, &opts); +free (pattern);
is it guaranteed that free() won't clobber errno? or should we record it right after attach call (and stop using libbpf_get_error())?
Quoted 27 lines
tools/lib/bpf/libbpf.c +3
-0+return libbpf_get_error (*link); +} +static int attach_uprobe_multi (const struct bpf_program *prog,long cookie,struct bpf_link **link) {char *probe_type =NULL , *binary_path =NULL , *func_name =NULL ;tools/lib/bpf/libbpf.h +3
-2@@ -539,10 +539,12 @@ struct bpf_kprobe_multi_opts {size_t cnt;/* create return kprobes */ bool retprobe; +/* create wrapper kprobes */ +bool wrapper; size_t :0; }; -#define bpf_kprobe_multi_opts__last_field retprobe +#define bpf_kprobe_multi_opts__last_field wrapper LIBBPF_API struct bpf_link * bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog, --2.43.2
Re: [PATCH RFCv2 bpf-next 3/4] libbpf: Add support for kprobe multi wrapper attach
Quoted 48 lines · Andrii Nakryiko
Quoted 47 lines · Jiri Olsa
Adding support for specify wrapper mode in
bpf_kprobe_multi_optsstruct object and new bpf program loader section:
SEC("kprobe.wrapper/bpf_fentry_test*")to load program as kprobe multi wrapper.
Signed-off-by Jiri Olsa <[email protected]>--- tools/lib/bpf/libbpf.c | 38 +++++++++++++++++++++++++++++++++++--- tools/lib/bpf/libbpf.h | 4 +++- 2 files changed, 38 insertions(+), 4 deletions(-)tools/lib/bpf/libbpf.c +3
-1@@ -8944,6 +8944,7 @@ static int attach_tp(const struct bpf_program *prog, long cookie, struct bpf_linstatic int attach_raw_tp (const struct bpf_program *prog,long cookie,struct bpf_link **link);static int attach_trace (const struct bpf_program *prog,long cookie,struct bpf_link **link);static int attach_kprobe_multi (const struct bpf_program *prog,long cookie,struct bpf_link **link); +static int attach_kprobe_wrapper (const struct bpf_program *prog,long cookie,struct bpf_link **link);static int attach_uprobe_multi (const struct bpf_program *prog,long cookie,struct bpf_link **link);static int attach_lsm (const struct bpf_program *prog,long cookie,struct bpf_link **link);static int attach_iter (const struct bpf_program *prog,long cookie,struct bpf_link **link);@@ -8960,6 +8961,7 @@ static const struct bpf_sec_def section_defs[] = {SEC_DEF ("uretprobe.s+" ,KPROBE ,0 ,SEC_SLEEPABLE , attach_uprobe),SEC_DEF ("kprobe.multi+" ,KPROBE ,BPF_TRACE_KPROBE_MULTI ,SEC_NONE , attach_kprobe_multi),SEC_DEF ("kretprobe.multi+" ,KPROBE ,BPF_TRACE_KPROBE_MULTI ,SEC_NONE , attach_kprobe_multi), +SEC_DEF ("kprobe.wrapper+" ,KPROBE ,BPF_TRACE_KPROBE_MULTI ,SEC_NONE , attach_kprobe_wrapper),SEC_DEF ("uprobe.multi+" ,KPROBE ,BPF_TRACE_UPROBE_MULTI ,SEC_NONE , attach_uprobe_multi),SEC_DEF ("uretprobe.multi+" ,KPROBE ,BPF_TRACE_UPROBE_MULTI ,SEC_NONE , attach_uprobe_multi), SEC_DEF("uprobe.multi.s+", KPROBE, BPF_TRACE_UPROBE_MULTI, SEC_SLEEPABLE, attach_uprobe_multi),@@ -11034,7 +11036,7 @@ bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,int err, link_fd, prog_fd;const __u64 *cookies;const char **syms; -bool retprobe; +__u32 flags =0 ;size_t cnt;if (!OPTS_VALID (opts, bpf_kprobe_multi_opts))@@ -11065,13 +11067,16 @@ bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog, cnt = res.cnt; }
Quoted 7 lines
tools/lib/bpf/libbpf.c bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog, · line 11073
Quoted 5 lines
tools/lib/bpf/libbpf.c +4
-1- retprobe =OPTS_GET (opts, retprobe, false); +if (OPTS_GET (opts, retprobe, false)) + flags |=BPF_F_KPROBE_MULTI_RETURN ; +if (OPTS_GET (opts, wrapper, false)) + flags |=BPF_F_KPROBE_MULTI_WRAPPER ;probably error out if both retprobe and wrapper are set?
ok
Quoted 15 lines
Quoted 15 lines
tools/lib/bpf/libbpf.c +1
-1lopts.kprobe_multi.syms = syms; lopts.kprobe_multi.addrs = addrs; lopts.kprobe_multi.cookies = cookies; lopts.kprobe_multi.cnt = cnt; - lopts.kprobe_multi.flags = retprobe ?BPF_F_KPROBE_MULTI_RETURN :0 ; + lopts.kprobe_multi.flags = flags; link =calloc (1 ,sizeof (*link));if (!link) {@@ -11187,6 +11192,33 @@ static int attach_kprobe_multi(const struct bpf_program *prog, long cookie, strureturn libbpf_get_error (*link); }
Quoted 7 lines
tools/lib/bpf/libbpf.c static int attach_kprobe_multi(const struct bpf_program *prog, long cookie, stru · line 11199
Quoted 5 lines
tools/lib/bpf/libbpf.c +5
-0+static int attach_kprobe_wrapper (const struct bpf_program *prog,long cookie,struct bpf_link **link) +{ +LIBBPF_OPTS (bpf_kprobe_multi_opts, opts, + .wrapper = true, + );nit: keep on a single line?
ok
Quoted 15 lines
Quoted 15 lines
tools/lib/bpf/libbpf.c +14
-0+const char *spec; +char *pattern; +int n; + + *link =NULL ; + +/* no auto-attach for SEC("kprobe.wrapper") */ +if (strcmp (prog->sec_name,"kprobe.wrapper" ) ==0 ) +return 0 ; + + spec = prog->sec_name +sizeof ("kprobe.wrapper/" ) -1 ; + n =sscanf (spec,"%m[a-zA-Z0-9_.*?]" , &pattern); +if (n <1 ) { +pr_warn ("kprobe wrapper pattern is invalid: %s\n" , pattern);
Quoted 8 lines
tools/lib/bpf/libbpf.c static int attach_kprobe_multi(const struct bpf_program *prog, long cookie, stru · line 11218
Quoted 5 lines
tools/lib/bpf/libbpf.c +5
-0+return -EINVAL ; + } + + *link =bpf_program__attach_kprobe_multi_opts (prog, pattern, &opts); +free (pattern);is it guaranteed that
free()won't clobber errno? or should we record it right after attach call (and stop usinglibbpf_get_error())?
hum, I copy&pasted from the attach_kprobe_multi, so did not think of that ;-)
anyway man page says:
The free() function returns no value, and preserves errno.
so we're good
thanks,
jirka
Quoted 29 lines
Quoted 29 lines
tools/lib/bpf/libbpf.c +3
-0+return libbpf_get_error (*link); +} +static int attach_uprobe_multi (const struct bpf_program *prog,long cookie,struct bpf_link **link) {char *probe_type =NULL , *binary_path =NULL , *func_name =NULL ;tools/lib/bpf/libbpf.h +3
-2@@ -539,10 +539,12 @@ struct bpf_kprobe_multi_opts {size_t cnt;/* create return kprobes */ bool retprobe; +/* create wrapper kprobes */ +bool wrapper; size_t :0; }; -#define bpf_kprobe_multi_opts__last_field retprobe +#define bpf_kprobe_multi_opts__last_field wrapper LIBBPF_API struct bpf_link * bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog, --2.43.2
Jiri Olsa
reviewerRFC PATCH v2 4/4 selftests/bpf: Add kprobe multi wrapper test
In reply to Jiri OlsaAdding kprobe multi wrapper test and also testing the entry program return value controls execution of the return probe program.
--- tools/testing/selftests/bpf/bpf_kfuncs.h | 2 + .../bpf/prog_tests/kprobe_multi_test.c | 49 +++++++++ .../bpf/progs/kprobe_multi_wrapper.c | 100 ++++++++++++++++++ 3 files changed, 151 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/kprobe_multi_wrapper.c
tools/testing/selftests/bpf/bpf_kfuncs.h +2 -0
@@ -75,4 +75,6 @@ extern void bpf_key_put(struct bpf_key *key) __ksym; extern int bpf_verify_pkcs7_signature(struct bpf_dynptr *data_ptr, struct bpf_dynptr *sig_ptr, struct bpf_key *trusted_keyring) __ksym; + +extern bool bpf_kprobe_multi_is_return(void) __ksym;#endif
tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c +49 -0
65 lines not on this page · raw message
tools/testing/selftests/bpf/progs/kprobe_multi_wrapper.c new file +100 -0
100 lines not on this page · raw message
Signature · 1 line
2.43.2