bpf: Add fprobe link
Subsystems (11)
cover bpf: Add fprobe link
hi,
this patchset adds new link type BPF_LINK_TYPE_FPROBE that attaches kprobe program through fprobe API [1] instroduced by Masami.
The fprobe API allows to attach probe on multiple functions at once very fast, because it works on top of ftrace. On the other hand this limits the probe point to the function entry or return.
With bpftrace support I see following attach speed:
# perf stat --null -r 5 ./src/bpftrace -e 'kprobe:x* { } i:ms:1 { exit(); } '
Attaching 2 probes...
Attaching 3342 functions
...1.4960 +- 0.0285 seconds time elapsed ( +- 1.91% )
Also available at:
https://git.kernel.org/pub/scm/linux/kernel/git/jolsa/perf.git
bpf/fprobe_link
thanks,
jirka
[1] https://lore.kernel.org/bpf/[email protected]/T/#t
---
Jiri Olsa (8):
bpf: Add support to attach kprobe program with fprobe
bpf: Add bpf_get_func_ip kprobe helper for fprobe link
bpf: Add bpf_cookie support to fprobe
libbpf: Add libbpf__kallsyms_parse function
libbpf: Add bpf_link_create support for multi kprobes
libbpf: Add bpf_program__attach_kprobe_opts for multi kprobes selftest/bpf: Add fprobe attach test
selftest/bpf: Add fprobe test for bpf_cookie values
include/linux/bpf.h | 2 + include/linux/bpf_types.h | 1 + include/uapi/linux/bpf.h | 14 +++++ kernel/bpf/syscall.c | 327 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- kernel/bpf/verifier.c | 19 +++++- kernel/trace/bpf_trace.c | 32 +++++++++- tools/include/uapi/linux/bpf.h | 14 +++++ tools/lib/bpf/bpf.c | 7 +++ tools/lib/bpf/bpf.h | 9 ++- tools/lib/bpf/libbpf.c | 198 +++++++++++++++++++++++++++++++++++++++++++++++++++-------- tools/lib/bpf/libbpf_internal.h | 5 ++ tools/testing/selftests/bpf/prog_tests/bpf_cookie.c | 73 ++++++++++++++++++++++ tools/testing/selftests/bpf/prog_tests/fprobe_test.c | 117 +++++++++++++++++++++++++++++++++++ tools/testing/selftests/bpf/progs/fprobe.c | 58 ++++++++++++++++++ tools/testing/selftests/bpf/progs/fprobe_bpf_cookie.c | 62 +++++++++++++++++++ 15 files changed, 902 insertions(+), 36 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/fprobe_test.c create mode 100644 tools/testing/selftests/bpf/progs/fprobe.c create mode 100644 tools/testing/selftests/bpf/progs/fprobe_bpf_cookie.c
PATCH 1/8 bpf: Add support to attach kprobe program with fprobe
Adding new link type BPF_LINK_TYPE_FPROBE that attaches kprobe program through fprobe API.
The fprobe API allows to attach probe on multiple functions at once very fast, because it works on top of ftrace. On the other hand this limits the probe point to the function entry or return.
The kprobe program gets the same pt_regs input ctx as when it's attached through the perf API.
Adding new attach type BPF_TRACE_FPROBE that enables such link for kprobe program.
User provides array of addresses or symbols with count to attach the kprobe program to. The new link_create uapi interface looks like:
struct {
__aligned_u64 syms;
__aligned_u64 addrs;
__u32 cnt;
__u32 flags;
} fprobe;The flags field allows single BPF_F_FPROBE_RETURN bit to create return fprobe.
--- include/linux/bpf_types.h | 1 + include/uapi/linux/bpf.h | 13 ++ kernel/bpf/syscall.c | 248 ++++++++++++++++++++++++++++++++- tools/include/uapi/linux/bpf.h | 13 ++ 4 files changed, 270 insertions(+), 5 deletions(-)
include/linux/bpf_types.h +1 -0
@@ -140,3 +140,4 @@ BPF_LINK_TYPE(BPF_LINK_TYPE_XDP, xdp)#ifdef CONFIG_PERF_EVENTS BPF_LINK_TYPE(BPF_LINK_TYPE_PERF_EVENT, perf)#endif +BPF_LINK_TYPE (BPF_LINK_TYPE_FPROBE , fprobe)
include/uapi/linux/bpf.h +13 -0
@@ -997,6 +997,7 @@ enum bpf_attach_type {BPF_SK_REUSEPORT_SELECT ,BPF_SK_REUSEPORT_SELECT_OR_MIGRATE ,BPF_PERF_EVENT , +BPF_TRACE_FPROBE , __MAX_BPF_ATTACH_TYPE };
@@ -1011,6 +1012,7 @@ enum bpf_link_type {BPF_LINK_TYPE_NETNS =5 ,BPF_LINK_TYPE_XDP =6 ,BPF_LINK_TYPE_PERF_EVENT =7 , +BPF_LINK_TYPE_FPROBE =8 , MAX_BPF_LINK_TYPE, };
@@ -1118,6 +1120,11 @@ enum bpf_link_type {*/ #define BPF_F_XDP_HAS_FRAGS (1U << 5) +/* link_create.fprobe.flags used in LINK_CREATE command for +* BPF_TRACE_FPROBE attach type to create return probe. +*/ +#define BPF_F_FPROBE_RETURN (1U << 0) +/* When BPF ldimm64's insn[0].src_reg != 0 then this can have * the following extensions: *
@@ -1472,6 +1479,12 @@ union bpf_attr {*/ __u64 bpf_cookie; } perf_event; +struct { +__aligned_u64 syms; +__aligned_u64 addrs; +__u32 cnt; +__u32 flags; + } fprobe; }; } link_create;
kernel/bpf/syscall.c +243 -5
290 lines not on this page · raw message
tools/include/uapi/linux/bpf.h +13 -0
@@ -997,6 +997,7 @@ enum bpf_attach_type {BPF_SK_REUSEPORT_SELECT ,BPF_SK_REUSEPORT_SELECT_OR_MIGRATE ,BPF_PERF_EVENT , +BPF_TRACE_FPROBE , __MAX_BPF_ATTACH_TYPE };
@@ -1011,6 +1012,7 @@ enum bpf_link_type {BPF_LINK_TYPE_NETNS =5 ,BPF_LINK_TYPE_XDP =6 ,BPF_LINK_TYPE_PERF_EVENT =7 , +BPF_LINK_TYPE_FPROBE =8 , MAX_BPF_LINK_TYPE, };
@@ -1118,6 +1120,11 @@ enum bpf_link_type {*/ #define BPF_F_XDP_HAS_FRAGS (1U << 5) +/* link_create.fprobe.flags used in LINK_CREATE command for +* BPF_TRACE_FPROBE attach type to create return probe. +*/ +#define BPF_F_FPROBE_RETURN (1U << 0) +/* When BPF ldimm64's insn[0].src_reg != 0 then this can have * the following extensions: *
@@ -1472,6 +1479,12 @@ union bpf_attr {*/ __u64 bpf_cookie; } perf_event; +struct { +__aligned_u64 syms; +__aligned_u64 addrs; +__u32 cnt; +__u32 flags; + } fprobe; }; } link_create;
Signature · 1 line
2.34.1
PATCH 2/8 bpf: Add bpf_get_func_ip kprobe helper for fprobe link
In reply to Jiri OlsaAdding support to call get_func_ip_fprobe helper from kprobe programs attached by fprobe link.
Also adding support to inline it, because it's single load instruction.
--- kernel/bpf/verifier.c | 19 ++++++++++++++++++- kernel/trace/bpf_trace.c | 16 +++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-)
kernel/bpf/verifier.c +18 -1
@@ -13625,7 +13625,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 */
@@ -13640,6 +13640,23 @@ static int do_misc_fixups(struct bpf_verifier_env *env)continue ; } +/* Implement kprobe/fprobe bpf_get_func_ip inline. */ +if (prog_type ==BPF_PROG_TYPE_KPROBE && + eatype ==BPF_TRACE_FPROBE && + 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 ; + } + patch_call_imm: fn = env->ops->get_func_proto (insn->imm, env->prog);/* all functions that have prototype and verifier allowed
kernel/trace/bpf_trace.c +15 -1
@@ -1036,6 +1036,19 @@ static const struct bpf_func_proto bpf_get_func_ip_proto_kprobe = { .arg1_type = ARG_PTR_TO_CTX, }; +BPF_CALL_1(bpf_get_func_ip_fprobe, struct pt_regs *, regs) +{ +/* This helper call is inlined by verifier. */ +return regs->ip; +} + +static const struct bpf_func_proto bpf_get_func_ip_proto_fprobe = { + .func = bpf_get_func_ip_fprobe, + .gpl_only = false, + .ret_type =RET_INTEGER , + .arg1_type =ARG_PTR_TO_CTX , +}; + BPF_CALL_1(bpf_get_attach_cookie_trace, void *, ctx) {struct bpf_trace_run_ctx *run_ctx;
@@ -1279,7 +1292,8 @@ kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)return &bpf_override_return_proto;#endif case BPF_FUNC_get_func_ip: -return &bpf_get_func_ip_proto_kprobe; +return prog->expected_attach_type ==BPF_TRACE_FPROBE ? + &bpf_get_func_ip_proto_fprobe : &bpf_get_func_ip_proto_kprobe;case BPF_FUNC_get_attach_cookie:return &bpf_get_attach_cookie_proto_trace;default :
Signature · 1 line
2.34.1
PATCH 3/8 bpf: Add bpf_cookie support to fprobe
In reply to Jiri OlsaAdding support to call bpf_get_attach_cookie helper from kprobe program attached by fprobe link.
The bpf_cookie is provided by array of u64 values, where each value is paired with provided function address with the same array index.
--- include/linux/bpf.h | 2 + include/uapi/linux/bpf.h | 1 + kernel/bpf/syscall.c | 83 +++++++++++++++++++++++++++++++++- kernel/trace/bpf_trace.c | 16 ++++++- tools/include/uapi/linux/bpf.h | 1 + 5 files changed, 100 insertions(+), 3 deletions(-)
include/linux/bpf.h +2 -0
@@ -1301,6 +1301,8 @@ static inline void bpf_reset_run_ctx(struct bpf_run_ctx *old_ctx)#endif } +u64 bpf_fprobe_cookie (struct bpf_run_ctx *ctx,u64 ip); +/* BPF program asks to bypass CAP_NET_BIND_SERVICE in bind. */ #define BPF_RET_BIND_NO_CAP_NET_BIND_SERVICE (1 << 0)/* BPF program asks to set CN on the packet. */
include/uapi/linux/bpf.h +1 -0
@@ -1484,6 +1484,7 @@ union bpf_attr {__aligned_u64 addrs;__u32 cnt;__u32 flags; +__aligned_u64 bpf_cookies; } fprobe; }; } link_create;
kernel/bpf/syscall.c +81 -2
159 lines not on this page · raw message
kernel/trace/bpf_trace.c +15 -1
@@ -1049,6 +1049,18 @@ static const struct bpf_func_proto bpf_get_func_ip_proto_fprobe = { .arg1_type = ARG_PTR_TO_CTX, }; +BPF_CALL_1(bpf_get_attach_cookie_fprobe, struct pt_regs *, regs) +{ +return bpf_fprobe_cookie (current->bpf_ctx, regs->ip); +} + +static const struct bpf_func_proto bpf_get_attach_cookie_proto_fprobe = { + .func = bpf_get_attach_cookie_fprobe, + .gpl_only = false, + .ret_type =RET_INTEGER , + .arg1_type =ARG_PTR_TO_CTX , +}; + BPF_CALL_1(bpf_get_attach_cookie_trace, void *, ctx) {struct bpf_trace_run_ctx *run_ctx;
@@ -1295,7 +1307,9 @@ kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)return prog->expected_attach_type ==BPF_TRACE_FPROBE ? &bpf_get_func_ip_proto_fprobe : &bpf_get_func_ip_proto_kprobe;case BPF_FUNC_get_attach_cookie: -return &bpf_get_attach_cookie_proto_trace; +return prog->expected_attach_type ==BPF_TRACE_FPROBE ? + &bpf_get_attach_cookie_proto_fprobe : + &bpf_get_attach_cookie_proto_trace;default :return bpf_tracing_func_proto (func_id, prog); }
tools/include/uapi/linux/bpf.h +1 -0
@@ -1484,6 +1484,7 @@ union bpf_attr {__aligned_u64 addrs;__u32 cnt;__u32 flags; +__aligned_u64 bpf_cookies; } fprobe; }; } link_create;
Signature · 1 line
2.34.1
PATCH 4/8 libbpf: Add libbpf__kallsyms_parse function
In reply to Jiri OlsaMove the kallsyms parsing in internal libbpf__kallsyms_parse function, so it can be used from other places.
It will be used in following changes.
--- tools/lib/bpf/libbpf.c | 62 ++++++++++++++++++++------------- tools/lib/bpf/libbpf_internal.h | 5 +++ 2 files changed, 43 insertions(+), 24 deletions(-)
tools/lib/bpf/libbpf.c +38 -24
@@ -7165,12 +7165,10 @@ static int bpf_object__sanitize_maps(struct bpf_object *obj)return 0 ; } -static int bpf_object__read_kallsyms_file (struct bpf_object *obj) +int libbpf__kallsyms_parse (void *arg,kallsyms_cb_t cb) {char sym_type, sym_name[500 ];unsigned long long sym_addr; -const struct btf_type *t; -struct extern_desc *ext;int ret, err =0 ;FILE *f;
@@ -7189,35 +7187,51 @@ static int bpf_object__read_kallsyms_file(struct bpf_object *obj)if (ret !=3 ) {pr_warn ("failed to read kallsyms entry: %d\n" , ret); err = -EINVAL ; - goto out; +break ; } - ext =find_extern_by_name (obj, sym_name); -if (!ext || ext->type !=EXT_KSYM ) -continue ; - - t =btf__type_by_id (obj->btf, ext->btf_id); -if (!btf_is_var (t)) -continue ; - -if (ext->is_set && ext->ksym.addr != sym_addr) { -pr_warn ("extern (ksym) '%s' resolution is ambiguous: 0x%llx or 0x%llx\n" , - sym_name, ext->ksym.addr, sym_addr); - err = -EINVAL ; - goto out; - } -if (!ext->is_set) { - ext->is_set = true; - ext->ksym.addr = sym_addr; -pr_debug ("extern (ksym) %s=0x%llx\n" , sym_name, sym_addr); - } + err =cb (arg, sym_addr, sym_type, sym_name); +if (err) +break ; } -out:fclose (f);return err; } +static int kallsyms_cb (void *arg,unsigned long long sym_addr, +char sym_type,const char *sym_name) +{ +struct bpf_object *obj = arg; +const struct btf_type *t; +struct extern_desc *ext; + + ext =find_extern_by_name (obj, sym_name); +if (!ext || ext->type !=EXT_KSYM ) +return 0 ; + + t =btf__type_by_id (obj->btf, ext->btf_id); +if (!btf_is_var (t)) +return 0 ; + +if (ext->is_set && ext->ksym.addr != sym_addr) { +pr_warn ("extern (ksym) '%s' resolution is ambiguous: 0x%llx or 0x%llx\n" , + sym_name, ext->ksym.addr, sym_addr); +return -EINVAL ; + } +if (!ext->is_set) { + ext->is_set = true; + ext->ksym.addr = sym_addr; +pr_debug ("extern (ksym) %s=0x%llx\n" , sym_name, sym_addr); + } +return 0 ; +} + +static int bpf_object__read_kallsyms_file (struct bpf_object *obj) +{ +return libbpf__kallsyms_parse (obj, kallsyms_cb); +} +static int find_ksym_btf_id (struct bpf_object *obj,const char *ksym_name,__u16 kind,struct btf **res_btf,struct module_btf **res_mod_btf)
tools/lib/bpf/libbpf_internal.h +5 -0
@@ -449,6 +449,11 @@ __s32 btf__find_by_name_kind_own(const struct btf *btf, const char *type_name,extern enum libbpf_strict_mode libbpf_mode; +typedef int (*kallsyms_cb_t )(void *arg,unsigned long long sym_addr, +char sym_type,const char *sym_name); + +int libbpf__kallsyms_parse (void *arg,kallsyms_cb_t cb); +/* handle direct returned errors */ static inline int libbpf_err (int ret) {
Signature · 1 line
2.34.1
PATCH 5/8 libbpf: Add bpf_link_create support for multi kprobes
In reply to Jiri OlsaAdding new kprobe struct in bpf_link_create_opts object to pass multi kprobe data to link_create attr API.
--- tools/lib/bpf/bpf.c | 7 +++++++ tools/lib/bpf/bpf.h | 9 ++++++++- 2 files changed, 15 insertions(+), 1 deletion(-)
tools/lib/bpf/bpf.c +7 -0
@@ -853,6 +853,13 @@ int bpf_link_create(int prog_fd, int target_fd,if (!OPTS_ZEROED (opts, perf_event))return libbpf_err (-EINVAL );break ; +case BPF_TRACE_FPROBE : + attr.link_create.fprobe.syms =OPTS_GET (opts, fprobe.syms,0 ); + attr.link_create.fprobe.addrs =OPTS_GET (opts, fprobe.addrs,0 ); + attr.link_create.fprobe.cnt =OPTS_GET (opts, fprobe.cnt,0 ); + attr.link_create.fprobe.flags =OPTS_GET (opts, fprobe.flags,0 ); + attr.link_create.fprobe.bpf_cookies =OPTS_GET (opts, fprobe.bpf_cookies,0 ); +break ;default :if (!OPTS_ZEROED (opts, flags))return libbpf_err (-EINVAL );
tools/lib/bpf/bpf.h +8 -1
@@ -413,10 +413,17 @@ struct bpf_link_create_opts {struct {__u64 bpf_cookie; } perf_event; +struct { +__u64 syms; +__u64 addrs; +__u32 cnt; +__u32 flags; +__u64 bpf_cookies; + } fprobe; }; size_t :0; }; -#define bpf_link_create_opts__last_field perf_event +#define bpf_link_create_opts__last_field fprobe.bpf_cookies LIBBPF_API int bpf_link_create(int prog_fd, int target_fd, enum bpf_attach_type attach_type,
Signature · 1 line
2.34.1
PATCH 6/8 libbpf: Add bpf_program__attach_kprobe_opts for multi kprobes
In reply to Jiri OlsaAdding support to bpf_program__attach_kprobe_opts to load kprobes to multiple functions.
If the kprobe program has BPF_TRACE_FPROBE as expected_attach_type it will use the new fprobe link to attach the program. In this case it will use 'func_name' as pattern for functions to attach.
Adding also support to use '*' wildcard in 'kprobe/kretprobe' section name by SEC macro, like:
SEC("kprobe/bpf_fentry_test*")
SEC("kretprobe/bpf_fentry_test*")
This will set kprobe's expected_attach_type to BPF_TRACE_FPROBE, and attach it to provided functions pattern.
--- tools/lib/bpf/libbpf.c | 136 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 133 insertions(+), 3 deletions(-)
tools/lib/bpf/libbpf.c +133 -3
@@ -8607,13 +8607,15 @@ static struct bpf_link *attach_trace(const struct bpf_program *prog, long cookiestatic struct bpf_link *attach_lsm (const struct bpf_program *prog,long cookie);static struct bpf_link *attach_iter (const struct bpf_program *prog,long cookie); +static int init_kprobe (struct bpf_program *prog,long cookie); +static const struct bpf_sec_def section_defs[] = {SEC_DEF ("socket" ,SOCKET_FILTER ,0 ,SEC_NONE |SEC_SLOPPY_PFX ),SEC_DEF ("sk_reuseport/migrate" ,SK_REUSEPORT ,BPF_SK_REUSEPORT_SELECT_OR_MIGRATE ,SEC_ATTACHABLE |SEC_SLOPPY_PFX ),SEC_DEF ("sk_reuseport" ,SK_REUSEPORT ,BPF_SK_REUSEPORT_SELECT ,SEC_ATTACHABLE |SEC_SLOPPY_PFX ), -SEC_DEF ("kprobe/" ,KPROBE ,0 ,SEC_NONE , attach_kprobe), + SEC_DEF("kprobe/", KPROBE, 0, SEC_NONE, attach_kprobe, .init_fn = init_kprobe),SEC_DEF ("uprobe/" ,KPROBE ,0 ,SEC_NONE ), -SEC_DEF ("kretprobe/" ,KPROBE ,0 ,SEC_NONE , attach_kprobe), + SEC_DEF("kretprobe/", KPROBE, 0, SEC_NONE, attach_kprobe, .init_fn = init_kprobe),SEC_DEF ("uretprobe/" ,KPROBE ,0 ,SEC_NONE ),SEC_DEF ("tc" ,SCHED_CLS ,0 ,SEC_NONE ),SEC_DEF ("classifier" ,SCHED_CLS ,0 ,SEC_NONE |SEC_SLOPPY_PFX |SEC_DEPRECATED ),
@@ -10031,6 +10033,123 @@ static int perf_event_kprobe_open_legacy(const char *probe_name, bool retprobe,return pfd; } +struct fprobe_resolve { +const char *name; +__u64 *addrs; +__u32 alloc; +__u32 cnt; +}; + +static bool glob_matches (const char *glob,const char *s) +{ +int n =strlen (glob); + +if (n ==1 && glob[0 ] =='*' ) +return true; + +if (glob[0 ] =='*' && glob[n -1 ] =='*' ) { +const char *subs; +/* substring match */ + +/* this is hacky, but we don't want to allocate +* for no good reason +*/ + ((char *)glob)[n -1 ] ='\0' ; + subs =strstr (s, glob +1 ); + ((char *)glob)[n -1 ] ='*' ; + +return subs !=NULL ; + }else if (glob[0 ] =='*' ) { +size_t nn =strlen (s); +/* suffix match */ + +/* too short for a given suffix */ +if (nn < n -1 ) +return false; +return strcmp (s + nn - (n -1 ), glob +1 ) ==0 ; + }else if (glob[n -1 ] =='*' ) { +/* prefix match */ +return strncmp (s, glob, n -1 ) ==0 ; + }else { +/* exact match */ +return strcmp (glob, s) ==0 ; + } +} + +static int resolve_fprobe_cb (void *arg,unsigned long long sym_addr, +char sym_type,const char *sym_name) +{ +struct fprobe_resolve *res = arg; +__u64 *p; + +if (!glob_matches (res->name, sym_name)) +return 0 ; + +if (res->cnt == res->alloc) { + res->alloc =max ((__u32 )16 , res->alloc *3 /2 ); + p =libbpf_reallocarray (res->addrs, res->alloc,sizeof (__u32)); +if (!p) +return -ENOMEM ; + res->addrs = p; + } + res->addrs[res->cnt++] = sym_addr; +return 0 ; +} + +static struct bpf_link * +attach_fprobe_opts (const struct bpf_program *prog, +const char *func_name, +const struct bpf_kprobe_opts *kopts) +{ +DECLARE_LIBBPF_OPTS (bpf_link_create_opts, opts); +struct fprobe_resolve res = { + .name = func_name, + }; +struct bpf_link *link =NULL ; +char errmsg[STRERR_BUFSIZE ]; +int err, link_fd, prog_fd; +bool retprobe; + + err =libbpf__kallsyms_parse (&res, resolve_fprobe_cb); +if (err) + goto error; +if (!res.cnt) { + err = -ENOENT ; + goto error; + } + + retprobe =OPTS_GET (kopts, retprobe, false); + + opts.fprobe.addrs = (__u64 ) res.addrs; + opts.fprobe.cnt = res.cnt; + opts.flags = retprobe ?BPF_F_FPROBE_RETURN :0 ; + + link =calloc (1 ,sizeof (*link)); +if (!link) { + err = -ENOMEM ; + goto error; + } + link->detach = &bpf_link__detach_fd; + + prog_fd =bpf_program__fd (prog); + link_fd =bpf_link_create (prog_fd,0 ,BPF_TRACE_FPROBE , &opts); +if (link_fd <0 ) { + err = -errno; +pr_warn ("prog '%s': failed to attach to %s: %s\n" , + prog->name, res.name, +libbpf_strerror_r (err, errmsg,sizeof (errmsg))); + goto error; + } + link->fd = link_fd; +free (res.addrs); +return link; + +error: +free (link); +free (res.addrs); +return libbpf_err_ptr (err); +} +struct bpf_link * bpf_program__attach_kprobe_opts(const struct bpf_program *prog, const char *func_name,
@@ -10047,6 +10166,9 @@ bpf_program__attach_kprobe_opts(const struct bpf_program *prog,if (!OPTS_VALID (opts, bpf_kprobe_opts))return libbpf_err_ptr (-EINVAL ); +if (prog->expected_attach_type ==BPF_TRACE_FPROBE ) +return attach_fprobe_opts (prog, func_name, opts); + retprobe =OPTS_GET (opts, retprobe, false); offset =OPTS_GET (opts, offset,0 ); pe_opts.bpf_cookie =OPTS_GET (opts, bpf_cookie,0 );
@@ -10112,6 +10234,14 @@ struct bpf_link *bpf_program__attach_kprobe(const struct bpf_program *prog,return bpf_program__attach_kprobe_opts (prog, func_name, &opts); } +static int init_kprobe (struct bpf_program *prog,long cookie) +{ +/* If we have wildcard, switch to fprobe link. */ +if (strchr (prog->sec_name,'*' )) +bpf_program__set_expected_attach_type (prog,BPF_TRACE_FPROBE ); +return 0 ; +} +static struct bpf_link *attach_kprobe (const struct bpf_program *prog,long cookie) {DECLARE_LIBBPF_OPTS (bpf_kprobe_opts, opts);
@@ -10127,7 +10257,7 @@ static struct bpf_link *attach_kprobe(const struct bpf_program *prog, long cookielse func_name = prog->sec_name +sizeof ("kprobe/" ) -1 ; - n =sscanf (func_name,"%m[a-zA-Z0-9_.]+%li" , &func, &offset); + n =sscanf (func_name,"%m[a-zA-Z0-9_.*]+%li" , &func, &offset);if (n <1 ) { err = -EINVAL ;pr_warn ("kprobe name is invalid: %s\n" , func_name);
Signature · 1 line
2.34.1
PATCH 7/8 selftest/bpf: Add fprobe attach test
In reply to Jiri OlsaAdding kprobe attach test that uses new fprobe interface to attach kprobe program to multiple functions.
The test is attaching to bpf_fentry_test* functions and uses single trampoline program bpf_prog_test_run to trigger bpf_fentry_test* functions.
--- .../selftests/bpf/prog_tests/fprobe_test.c | 117 ++++++++++++++++++ tools/testing/selftests/bpf/progs/fprobe.c | 58 +++++++++ 2 files changed, 175 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/fprobe_test.c create mode 100644 tools/testing/selftests/bpf/progs/fprobe.c
tools/testing/selftests/bpf/prog_tests/fprobe_test.c new file +117 -0
@@ -0,0 +1,117 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <test_progs.h> +#include "fprobe.skel.h" +#include "trace_helpers.h" + +static void test_skel_api (void ) +{ +struct fprobe *skel =NULL ; +__u32 duration =0 , retval; +int err, prog_fd; + + skel =fprobe__open_and_load (); +if (!ASSERT_OK_PTR (skel,"fprobe__open_and_load" )) + goto cleanup; + + err =fprobe__attach (skel); +if (!ASSERT_OK (err,"fprobe__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" ); + +ASSERT_EQ (skel->bss->test2_result,8 ,"test2_result" ); +ASSERT_EQ (skel->bss->test3_result,8 ,"test3_result" ); + +cleanup: +fprobe__destroy (skel); +} + +static void test_link_api (struct bpf_link_create_opts *opts) +{ +int err, prog_fd, link1_fd =-1 , link2_fd =-1 ; +struct fprobe *skel =NULL ; +__u32 duration =0 , retval; + + skel =fprobe__open_and_load (); +if (!ASSERT_OK_PTR (skel,"fentry_raw_skel_load" )) + goto cleanup; + + prog_fd =bpf_program__fd (skel->progs.test2); + link1_fd =bpf_link_create (prog_fd,0 ,BPF_TRACE_FPROBE , opts); +if (!ASSERT_GE (link1_fd,0 ,"link_fd" )) + goto cleanup; + + opts->fprobe.flags =BPF_F_FPROBE_RETURN ; + prog_fd =bpf_program__fd (skel->progs.test3); + link2_fd =bpf_link_create (prog_fd,0 ,BPF_TRACE_FPROBE , opts); +if (!ASSERT_GE (link2_fd,0 ,"link_fd" )) + goto cleanup; + + skel->bss->test2_result =0 ; + skel->bss->test3_result =0 ; + + 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" ); + +ASSERT_EQ (skel->bss->test2_result,8 ,"test2_result" ); +ASSERT_EQ (skel->bss->test3_result,8 ,"test3_result" ); + +cleanup: +if (link1_fd !=-1 ) +close (link1_fd); +if (link2_fd !=-1 ) +close (link2_fd); +fprobe__destroy (skel); +} + +static void test_link_api_addrs (void ) +{ +DECLARE_LIBBPF_OPTS (bpf_link_create_opts, opts); +__u64 addrs[8 ]; + +kallsyms_find ("bpf_fentry_test1" , &addrs[0 ]); +kallsyms_find ("bpf_fentry_test2" , &addrs[1 ]); +kallsyms_find ("bpf_fentry_test3" , &addrs[2 ]); +kallsyms_find ("bpf_fentry_test4" , &addrs[3 ]); +kallsyms_find ("bpf_fentry_test5" , &addrs[4 ]); +kallsyms_find ("bpf_fentry_test6" , &addrs[5 ]); +kallsyms_find ("bpf_fentry_test7" , &addrs[6 ]); +kallsyms_find ("bpf_fentry_test8" , &addrs[7 ]); + + opts.fprobe.addrs = (__u64 ) addrs; + opts.fprobe.cnt =8 ; +test_link_api (&opts); +} + +static void test_link_api_syms (void ) +{ +DECLARE_LIBBPF_OPTS (bpf_link_create_opts, opts); +const char *syms[8 ] = { +"bpf_fentry_test1" , +"bpf_fentry_test2" , +"bpf_fentry_test3" , +"bpf_fentry_test4" , +"bpf_fentry_test5" , +"bpf_fentry_test6" , +"bpf_fentry_test7" , +"bpf_fentry_test8" , + }; + + opts.fprobe.syms = (__u64 ) syms; + opts.fprobe.cnt =8 ; +test_link_api (&opts); +} + +void test_fprobe_test (void ) +{ +test_skel_api (); +test_link_api_syms (); +test_link_api_addrs (); +}
tools/testing/selftests/bpf/progs/fprobe.c new file +58 -0
58 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH 8/8 selftest/bpf: Add fprobe test for bpf_cookie values
In reply to Jiri OlsaAdding bpf_cookie test for kprobe attached by fprobe link.
--- .../selftests/bpf/prog_tests/bpf_cookie.c | 73 +++++++++++++++++++ .../selftests/bpf/progs/fprobe_bpf_cookie.c | 62 ++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/fprobe_bpf_cookie.c
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +73 -0
@@ -7,6 +7,7 @@#include <unistd.h> #include <test_progs.h> #include "test_bpf_cookie.skel.h" +#include "fprobe_bpf_cookie.skel.h" /* uprobe attach point */ static void trigger_func (void )
@@ -63,6 +64,76 @@ static void kprobe_subtest(struct test_bpf_cookie *skel)bpf_link__destroy (retlink2); } +static void fprobe_subtest (void ) +{ +DECLARE_LIBBPF_OPTS (bpf_link_create_opts, opts); +int err, prog_fd, link1_fd =-1 , link2_fd =-1 ; +struct fprobe_bpf_cookie *skel =NULL ; +__u32 duration =0 , retval; +__u64 addrs[8 ], cookies[8 ]; + + skel =fprobe_bpf_cookie__open_and_load (); +if (!ASSERT_OK_PTR (skel,"fentry_raw_skel_load" )) + goto cleanup; + +kallsyms_find ("bpf_fentry_test1" , &addrs[0 ]); +kallsyms_find ("bpf_fentry_test2" , &addrs[1 ]); +kallsyms_find ("bpf_fentry_test3" , &addrs[2 ]); +kallsyms_find ("bpf_fentry_test4" , &addrs[3 ]); +kallsyms_find ("bpf_fentry_test5" , &addrs[4 ]); +kallsyms_find ("bpf_fentry_test6" , &addrs[5 ]); +kallsyms_find ("bpf_fentry_test7" , &addrs[6 ]); +kallsyms_find ("bpf_fentry_test8" , &addrs[7 ]); + + cookies[0 ] =1 ; + cookies[1 ] =2 ; + cookies[2 ] =3 ; + cookies[3 ] =4 ; + cookies[4 ] =5 ; + cookies[5 ] =6 ; + cookies[6 ] =7 ; + cookies[7 ] =8 ; + + opts.fprobe.addrs = (__u64) &addrs; + opts.fprobe.cnt =8 ; + opts.fprobe.bpf_cookies = (__u64) &cookies; + prog_fd =bpf_program__fd (skel->progs.test2); + + link1_fd =bpf_link_create (prog_fd,0 ,BPF_TRACE_FPROBE , &opts); +if (!ASSERT_GE (link1_fd,0 ,"link1_fd" )) +return ; + + cookies[0 ] =8 ; + cookies[1 ] =7 ; + cookies[2 ] =6 ; + cookies[3 ] =5 ; + cookies[4 ] =4 ; + cookies[5 ] =3 ; + cookies[6 ] =2 ; + cookies[7 ] =1 ; + + opts.flags =BPF_F_FPROBE_RETURN ; + prog_fd =bpf_program__fd (skel->progs.test3); + + link2_fd =bpf_link_create (prog_fd,0 ,BPF_TRACE_FPROBE , &opts); +if (!ASSERT_GE (link2_fd,0 ,"link2_fd" )) + 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" ); + +ASSERT_EQ (skel->bss->test2_result,8 ,"test2_result" ); +ASSERT_EQ (skel->bss->test3_result,8 ,"test3_result" ); + +cleanup: +close (link1_fd); +close (link2_fd); +fprobe_bpf_cookie__destroy (skel); +} +static void uprobe_subtest (struct test_bpf_cookie *skel) {DECLARE_LIBBPF_OPTS (bpf_uprobe_opts, opts);
@@ -249,6 +320,8 @@ void test_bpf_cookie(void)if (test__start_subtest ("kprobe" ))kprobe_subtest (skel); +if (test__start_subtest ("rawkprobe" )) +fprobe_subtest ();if (test__start_subtest ("uprobe" ))uprobe_subtest (skel);if (test__start_subtest ("tracepoint" ))
tools/testing/selftests/bpf/progs/fprobe_bpf_cookie.c new file +62 -0
62 lines not on this page · raw message
Signature · 1 line
2.34.1
Quoted 4 lines · Jiri Olsa
hi,
this patchset adds new link typeBPF_LINK_TYPE_FPROBEthat attaches kprobe program through fprobe API [1] instroduced by Masami.
No new prog type please.
I thought I made my reasons clear earlier. It's a multi kprobe. Not a fprobe or any other name. The kernel internal names should not leak into uapi.
Quoted 11 lines · Alexei Starovoitov
Quoted 4 lines · Jiri Olsa
hi,
this patchset adds new link typeBPF_LINK_TYPE_FPROBEthat attaches kprobe program through fprobe API [1] instroduced by Masami.No new prog type please.
I thought I made my reasons clear earlier. It's a multi kprobe. Not a fprobe or any other name. The kernel internal names should not leak into uapi.
well it's not new prog type, it's new link type that allows to attach kprobe program to multiple functions
the original change used BPF_LINK_TYPE_RAW_KPROBE, which did not seem to fit anymore, so I moved to FPROBE, because that's what it is ;-)
but if you don't want new name in uapi we could make this more obvious with link name:
BPF_LINK_TYPE_MULTI_KPROBE
and bpf_attach_type:
BPF_TRACE_MULTI_KPROBE
jirka
Quoted 14 lines · Jiri Olsa
Quoted 11 lines · Alexei Starovoitov
Quoted 4 lines · Jiri Olsa
hi,
this patchset adds new link typeBPF_LINK_TYPE_FPROBEthat attaches kprobe program through fprobe API [1] instroduced by Masami.No new prog type please.
I thought I made my reasons clear earlier. It's a multi kprobe. Not a fprobe or any other name. The kernel internal names should not leak into uapi.
Quoted 6 lines
well it's not new prog type, it's new link type that allows to attach kprobe program to multiple functions
the original change used
BPF_LINK_TYPE_RAW_KPROBE, which did not seem to fit anymore, so I moved to FPROBE, because that's what it is ;-)
Now I don't like the fprobe name even more.
Why invent new names? It's an ftrace interface.
Quoted 6 lines
but if you don't want new name in uapi we could make this more obvious with link name:
BPF_LINK_TYPE_MULTI_KPROBEand
bpf_attach_type:
BPF_TRACE_MULTI_KPROBE
I'd rather get rid of fprobe name first.
Quoted 18 lines · Alexei Starovoitov
Quoted 17 lines · Jiri Olsa
Quoted 11 lines · Alexei Starovoitov
Quoted 4 lines · Jiri Olsa
hi,
this patchset adds new link typeBPF_LINK_TYPE_FPROBEthat attaches kprobe program through fprobe API [1] instroduced by Masami.No new prog type please.
I thought I made my reasons clear earlier. It's a multi kprobe. Not a fprobe or any other name. The kernel internal names should not leak into uapi.well it's not new prog type, it's new link type that allows to attach kprobe program to multiple functions
Quoted 6 lines
Quoted 3 lines
the original change used
BPF_LINK_TYPE_RAW_KPROBE, which did not seem to fit anymore, so I moved to FPROBE, because that's what it is ;-)Now I don't like the fprobe name even more.
Why invent new names? It's an ftrace interface.
how about ftrace_probe ?
Quoted 10 lines
Quoted 7 lines
but if you don't want new name in uapi we could make this more obvious with link name:
BPF_LINK_TYPE_MULTI_KPROBEand
bpf_attach_type:
BPF_TRACE_MULTI_KPROBEI'd rather get rid of fprobe name first.
Masami, any idea?
thanks,
jirka
Masami Hiramatsu
maintainerQuoted 19 lines · On Thu, 3 Feb 2022 16:06:36 +0100 Jiri Olsa <[email protected]> wrote:
Quoted 18 lines · Alexei Starovoitov
Quoted 17 lines · Jiri Olsa
Quoted 11 lines · Alexei Starovoitov
Quoted 4 lines · Jiri Olsa
hi,
this patchset adds new link typeBPF_LINK_TYPE_FPROBEthat attaches kprobe program through fprobe API [1] instroduced by Masami.No new prog type please.
I thought I made my reasons clear earlier. It's a multi kprobe. Not a fprobe or any other name. The kernel internal names should not leak into uapi.well it's not new prog type, it's new link type that allows to attach kprobe program to multiple functions
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
the original change used
BPF_LINK_TYPE_RAW_KPROBE, which did not seem to fit anymore, so I moved to FPROBE, because that's what it is ;-)Now I don't like the fprobe name even more.
Why invent new names? It's an ftrace interface.how about
ftrace_probe?
I thought What Alexei pointed was that don't expose the FPROBE name to user space. If so, I agree with that. We can continue to use KPROBE for user space. Using fprobe is just for kernel implementation.
It means that we may better to keep simple mind model (there are only static event or dynamic kprobe event).
Quoted 11 lines
Quoted 9 lines
Quoted 6 lines
but if you don't want new name in uapi we could make this more obvious with link name:
BPF_LINK_TYPE_MULTI_KPROBEand
bpf_attach_type:
BPF_TRACE_MULTI_KPROBEI'd rather get rid of fprobe name first.
Masami, any idea?
Can't we continue to use kprobe prog type for user interface and internally, if there are multiple kprobes or kretprobes required, switch to use fprobe?
Thank you,
Quoted 4 lines
thanks,
jirka
Signature · 1 line
Masami Hiramatsu <[email protected]>
Quoted 4 lines · Masami Hiramatsu
I thought What Alexei pointed was that don't expose the FPROBE name to user space. If so, I agree with that. We can continue to use KPROBE for user space. Using fprobe is just for kernel implementation.
Clearly that intent is not working.
The "fprobe" name is already leaking outside of the kernel internals. The module interface is being proposed.
You'd need to document it, etc.
I think it's only causing confusion to users.
The new name serves no additional purpose other than
being new and unheard of.
fprobe is kprobe on ftrace. That's it.
Just call it kprobe on ftrace in api and everywhere. Please?
Masami Hiramatsu
maintainerQuoted 7 lines · On Thu, 3 Feb 2022 17:34:54 -0800 Alexei Starovoitov…
Quoted 4 lines · Masami Hiramatsu
I thought What Alexei pointed was that don't expose the FPROBE name to user space. If so, I agree with that. We can continue to use KPROBE for user space. Using fprobe is just for kernel implementation.
Clearly that intent is not working.
Thanks for confirmation :-)
Quoted 2 lines
The "fprobe" name is already leaking outside of the kernel internals. The module interface is being proposed.
Yes, but that is only for making the example module.
It is easy for me to enclose it inside kernel. I'm preparing KUnit selftest code for next version. After integrated that, we don't need that example module anymore.
Quoted 1 line
You'd need to document it, etc.
Yes, I've added a document of the APIs for the series. :-)
Quoted 4 lines
I think it's only causing confusion to users. The new name serves no additional purpose other than being new and unheard of.
fprobe is kprobe on ftrace. That's it.
No, fprobe is NOT kprobe on ftrace, kprobe on ftrace is already implemented transparently.
Quoted 2 lines
Just call it kprobe on ftrace in api and everywhere. Please?
Hmm, no, I think that's the work for who provide user-interface, isn't it?. Inside kernel, IMHO, the interface named from the programing viewpoint, and from that viewpoint, fprobe and kprobe interface are similar but different.
I'm able to allow kprobe-event (of ftrace) to accept "func*" (yeah, that's actually good idea), but ftrace interface will not export as fprobe. Even if it internally uses fprobe, I don't call it fprobe. It's kprobes from the viewpoint of ftrace user. (Yeah, I think it should be called as "dynamic-probe-event-for-kernel" but historically, it is called as kprobe-event.)
Thank you,
Signature · 1 line
Masami Hiramatsu <[email protected]>
Quoted 26 lines · Masami Hiramatsu
Quoted 7 lines · On Thu, 3 Feb 2022 17:34:54 -0800 Alexei Starovoitov…
Quoted 4 lines · Masami Hiramatsu
I thought What Alexei pointed was that don't expose the FPROBE name to user space. If so, I agree with that. We can continue to use KPROBE for user space. Using fprobe is just for kernel implementation.
Clearly that intent is not working.
Thanks for confirmation :-)
Quoted 2 lines
The "fprobe" name is already leaking outside of the kernel internals. The module interface is being proposed.
Yes, but that is only for making the example module.
It is easy for me to enclose it inside kernel. I'm preparing KUnit selftest code for next version. After integrated that, we don't need that example module anymore.Quoted 1 line
You'd need to document it, etc.
Yes, I've added a document of the APIs for the series. :-)
Quoted 7 lines
Quoted 4 lines
I think it's only causing confusion to users. The new name serves no additional purpose other than being new and unheard of.
fprobe is kprobe on ftrace. That's it.No, fprobe is NOT kprobe on ftrace, kprobe on ftrace is already implemented transparently.
Not true.
fprobe is nothing but _explicit_ kprobe on ftrace. There was an implicit optimization for kprobe when ftrace could be used.
All this new interface is doing is making it explicit. So a new name is not warranted here.
Quoted 1 line
from that viewpoint, fprobe and kprobe interface are similar but different.
What is the difference?
I don't see it.
Steven Rostedt
maintainerQuoted 10 lines · On Thu, 3 Feb 2022 18:12:11 -0800 Alexei Starovoitov…
Quoted 2 lines
No, fprobe is NOT kprobe on ftrace, kprobe on ftrace is already implemented transparently.
Not true.
fprobe is nothing but _explicit_ kprobe on ftrace. There was an implicit optimization for kprobe when ftrace could be used.
All this new interface is doing is making it explicit. So a new name is not warranted here.
Quoted 4 lines
Quoted 1 line
from that viewpoint, fprobe and kprobe interface are similar but different.
What is the difference?
I don't see it.
IIUC, a kprobe on a function (or ftrace, aka fprobe) gives some extra abilities that a normal kprobe does not. Namely, "what is the function parameters?"
You can only reliably get the parameters at function entry. Hence, by having a probe that is unique to functions as supposed to the middle of a function, makes sense to me.
That is, the API can change. "Give me parameter X". That along with some BTF reading, could figure out how to get parameter X, and record that.
-- Steve
Quoted 23 lines · Steven Rostedt
Quoted 10 lines · On Thu, 3 Feb 2022 18:12:11 -0800 Alexei Starovoitov…
Quoted 2 lines
No, fprobe is NOT kprobe on ftrace, kprobe on ftrace is already implemented transparently.
Not true.
fprobe is nothing but _explicit_ kprobe on ftrace. There was an implicit optimization for kprobe when ftrace could be used.
All this new interface is doing is making it explicit. So a new name is not warranted here.Quoted 4 lines
Quoted 1 line
from that viewpoint, fprobe and kprobe interface are similar but different.
What is the difference?
I don't see it.IIUC, a kprobe on a function (or ftrace, aka fprobe) gives some extra abilities that a normal kprobe does not. Namely, "what is the function parameters?"
Quoted 6 lines
You can only reliably get the parameters at function entry. Hence, by having a probe that is unique to functions as supposed to the middle of a function, makes sense to me.
That is, the API can change. "Give me parameter X". That along with some BTF reading, could figure out how to get parameter X, and record that.
This is more or less a description of kprobe on ftrace :) The bpf+kprobe users were relying on that for a long time. See PT_REGS_PARM1() macros in bpf_tracing.h They're meaningful only with kprobe on ftrace. So, no, fprobe is not inventing anything new here.
No one is using kprobe in the middle of the function. It's too difficult to make anything useful out of it, so no one bothers.
When people say "kprobe" 99 out of 100 they mean kprobe on ftrace/fentry.
Masami Hiramatsu
maintainerQuoted 42 lines · On Thu, 3 Feb 2022 18:12:11 -0800 Alexei Starovoitov…
Quoted 26 lines · Masami Hiramatsu
Quoted 7 lines · On Thu, 3 Feb 2022 17:34:54 -0800 Alexei Starovoitov…
Quoted 4 lines · Masami Hiramatsu
I thought What Alexei pointed was that don't expose the FPROBE name to user space. If so, I agree with that. We can continue to use KPROBE for user space. Using fprobe is just for kernel implementation.
Clearly that intent is not working.
Thanks for confirmation :-)
Quoted 2 lines
The "fprobe" name is already leaking outside of the kernel internals. The module interface is being proposed.
Yes, but that is only for making the example module.
It is easy for me to enclose it inside kernel. I'm preparing KUnit selftest code for next version. After integrated that, we don't need that example module anymore.Quoted 1 line
You'd need to document it, etc.
Yes, I've added a document of the APIs for the series. :-)
Quoted 7 lines
Quoted 4 lines
I think it's only causing confusion to users. The new name serves no additional purpose other than being new and unheard of.
fprobe is kprobe on ftrace. That's it.No, fprobe is NOT kprobe on ftrace, kprobe on ftrace is already implemented transparently.
Not true.
fprobe is nothing but _explicit_ kprobe on ftrace. There was an implicit optimization for kprobe when ftrace could be used.
All this new interface is doing is making it explicit. So a new name is not warranted here.
Quoted 4 lines
Quoted 1 line
from that viewpoint, fprobe and kprobe interface are similar but different.
What is the difference?
I don't see it.
From the raw-kernel programer's viewpoint, here are the differences.
kprobes is focusing on probing just a single probe point, and it can probe everywhere including function body. With this charactoristics, user can made a callback logic which is specialized for a specific address.
typedef int (*kprobe_pre_handler_t) (struct kprobe *, struct pt_regs *);
On the other hand, fprobe focuses on the multiple function entry and exit. That is just a wrapper of ftrace. So callbacks will need to check the function IP and change their behavior according to the IP.
void (*entry_handler)(struct fprobe *fp, unsigned long entry_ip, struct pt_regs *regs);
void (*exit_handler)(struct fprobe *fp, unsigned long entry_ip, struct pt_regs *regs);This is why the fprobe handler gets @entry_ip for the handlers.
However, from viewpoint of the higher level users, those may look same because both interrupts the kernel execution and callback their program like BPF. BPF can select collect program according to the instruction_pointer of @regs in both case.
In that case, I think it is natual that the BPF layer hides those differences from user, by abstracting those as a generic "kprobe" which means an idea of the general kernel instrumentation.
Thank you,
Signature · 1 line
Masami Hiramatsu <[email protected]>
Masami Hiramatsu
maintainerQuoted 37 lines · On Thu, 3 Feb 2022 18:42:22 -0800 Alexei Starovoitov…
Quoted 23 lines · Steven Rostedt
Quoted 10 lines · On Thu, 3 Feb 2022 18:12:11 -0800 Alexei Starovoitov…
Quoted 2 lines
No, fprobe is NOT kprobe on ftrace, kprobe on ftrace is already implemented transparently.
Not true.
fprobe is nothing but _explicit_ kprobe on ftrace. There was an implicit optimization for kprobe when ftrace could be used.
All this new interface is doing is making it explicit. So a new name is not warranted here.Quoted 4 lines
Quoted 1 line
from that viewpoint, fprobe and kprobe interface are similar but different.
What is the difference?
I don't see it.IIUC, a kprobe on a function (or ftrace, aka fprobe) gives some extra abilities that a normal kprobe does not. Namely, "what is the function parameters?"
Quoted 6 lines
You can only reliably get the parameters at function entry. Hence, by having a probe that is unique to functions as supposed to the middle of a function, makes sense to me.
That is, the API can change. "Give me parameter X". That along with some BTF reading, could figure out how to get parameter X, and record that.
This is more or less a description of kprobe on ftrace :) The bpf+kprobe users were relying on that for a long time. See
PT_REGS_PARM1()macros inbpf_tracing.hThey're meaningful only with kprobe on ftrace. So, no, fprobe is not inventing anything new here.
Quoted 3 lines
No one is using kprobe in the middle of the function. It's too difficult to make anything useful out of it, so no one bothers.
Perf-probe makes it very easy, as easy as gdb does. :-)
Thank you,
Quoted 2 lines
When people say "kprobe" 99 out of 100 they mean kprobe on ftrace/fentry.
Signature · 1 line
Masami Hiramatsu <[email protected]>
Masami Hiramatsu
maintainerHi Alexei,
Quoted 28 lines · On Thu, 3 Feb 2022 18:42:22 -0800 Alexei Starovoitov…
Quoted 27 lines · Steven Rostedt
Quoted 10 lines · On Thu, 3 Feb 2022 18:12:11 -0800 Alexei Starovoitov…
Quoted 2 lines
No, fprobe is NOT kprobe on ftrace, kprobe on ftrace is already implemented transparently.
Not true.
fprobe is nothing but _explicit_ kprobe on ftrace. There was an implicit optimization for kprobe when ftrace could be used.
All this new interface is doing is making it explicit. So a new name is not warranted here.Quoted 4 lines
Quoted 1 line
from that viewpoint, fprobe and kprobe interface are similar but different.
What is the difference?
I don't see it.IIUC, a kprobe on a function (or ftrace, aka fprobe) gives some extra abilities that a normal kprobe does not. Namely, "what is the function parameters?"
You can only reliably get the parameters at function entry. Hence, by having a probe that is unique to functions as supposed to the middle of a function, makes sense to me.
Quoted 8 lines
Quoted 2 lines
That is, the API can change. "Give me parameter X". That along with some BTF reading, could figure out how to get parameter X, and record that.
This is more or less a description of kprobe on ftrace :) The bpf+kprobe users were relying on that for a long time. See
PT_REGS_PARM1()macros inbpf_tracing.hThey're meaningful only with kprobe on ftrace. So, no, fprobe is not inventing anything new here.
Hmm, you may be misleading why PT_REGS_PARAM1() macro works. You can use it even if CONFIG_FUNCITON_TRACER=n if your kernel is built with CONFIG_KPROBES=y. It is valid unless you put a probe out of function entry.
Quoted 5 lines
No one is using kprobe in the middle of the function. It's too difficult to make anything useful out of it, so no one bothers.
When people say "kprobe" 99 out of 100 they mean kprobe on ftrace/fentry.
I see. But the kprobe is kprobe. It is not designed to support multiple probe points. If I'm forced to say, I can rename the struct fprobe to struct multi_kprobe, but that doesn't change the essence. You may need to use both of kprobes and so-called multi_kprobe properly. (Someone need to do that.)
Thank you,
Signature · 1 line
Masami Hiramatsu <[email protected]>
Re: [PATCH 1/8] bpf: Add support to attach kprobe program with fprobe
In reply to Jiri OlsaQuoted 35 lines · Jiri Olsa
Adding new link type
BPF_LINK_TYPE_FPROBEthat attaches kprobe program through fprobe API.The fprobe API allows to attach probe on multiple functions at once very fast, because it works on top of ftrace. On the other hand this limits the probe point to the function entry or return.
The kprobe program gets the same
pt_regsinput ctx as when it's attached through the perf API.Adding new attach type
BPF_TRACE_FPROBEthat enables such link for kprobe program.User provides array of addresses or symbols with count to attach the kprobe program to. The new
link_createuapi interface looks like:struct { __aligned_u64 syms; __aligned_u64 addrs; __u32 cnt; __u32 flags; } fprobe;The flags field allows single
BPF_F_FPROBE_RETURNbit to create return fprobe.Signed-off-by Masami Hiramatsu <[email protected]>Signed-off-by Jiri Olsa <[email protected]>--- include/linux/bpf_types.h | 1 + include/uapi/linux/bpf.h | 13 ++ kernel/bpf/syscall.c | 248 ++++++++++++++++++++++++++++++++- tools/include/uapi/linux/bpf.h | 13 ++ 4 files changed, 270 insertions(+), 5 deletions(-)
[...]
Quoted 21 lines
kernel/bpf/syscall.c +20
-0+#ifdef CONFIG_FPROBE + +struct bpf_fprobe_link { +struct bpf_link link; +struct fprobe fp; +unsigned long *addrs; +}; + +static void bpf_fprobe_link_release (struct bpf_link *link) +{ +struct bpf_fprobe_link *fprobe_link; + + fprobe_link = container_of(link, struct bpf_fprobe_link, link); +unregister_fprobe (&fprobe_link->fp); +} + +static void bpf_fprobe_link_dealloc (struct bpf_link *link) +{ +struct bpf_fprobe_link *fprobe_link; +
kernel/bpf/syscall.c static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *pro · line 3055
Quoted 10 lines
kernel/bpf/syscall.c +10
-0+ fprobe_link = container_of(link, struct bpf_fprobe_link, link); +kfree (fprobe_link->addrs); +kfree (fprobe_link); +} + +static const struct bpf_link_ops bpf_fprobe_link_lops = { + .release = bpf_fprobe_link_release, + .dealloc = bpf_fprobe_link_dealloc, +}; +
should this whole new link implementation (including fprobe_link_prog_run() below) maybe live in kernel/trace/bpf_trace.c? Seems a bit more fitting than kernel/bpf/syscall.c
Quoted 33 lines
kernel/bpf/syscall.c +33
-0+static int fprobe_link_prog_run (struct bpf_fprobe_link *fprobe_link, +struct pt_regs *regs) +{ +int err; + +if (unlikely (__this_cpu_inc_return (bpf_prog_active) !=1 )) { + err =0 ; + goto out; + } + +rcu_read_lock (); +migrate_disable (); + err =bpf_prog_run (fprobe_link->link.prog, regs); +migrate_enable (); +rcu_read_unlock (); + + out: +__this_cpu_dec (bpf_prog_active); +return err; +} + +static void fprobe_link_entry_handler (struct fprobe *fp,unsigned long entry_ip, +struct pt_regs *regs) +{ +unsigned long saved_ip =instruction_pointer (regs); +struct bpf_fprobe_link *fprobe_link; + +/* +* Because fprobe's regs->ip is set to the next instruction of +* dynamic-ftrace insturction, correct entry ip must be set, so +* that the bpf program can access entry address via regs as same +* as kprobes. +*/
kernel/bpf/syscall.c static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *pro · line 3098
Quoted 10 lines
kernel/bpf/syscall.c +10
-0+instruction_pointer_set (regs, entry_ip); + + fprobe_link = container_of(fp, struct bpf_fprobe_link, fp); +fprobe_link_prog_run (fprobe_link, regs); + +instruction_pointer_set (regs, saved_ip); +} + +static void fprobe_link_exit_handler (struct fprobe *fp,unsigned long entry_ip, +struct pt_regs *regs)
isn't it identical to fprobe_lnk_entry_handler? Maybe use one callback for both entry and exit?
Quoted 13 lines
kernel/bpf/syscall.c +13
-0+{ +unsigned long saved_ip =instruction_pointer (regs); +struct bpf_fprobe_link *fprobe_link; + +instruction_pointer_set (regs, entry_ip); + + fprobe_link = container_of(fp, struct bpf_fprobe_link, fp); +fprobe_link_prog_run (fprobe_link, regs); + +instruction_pointer_set (regs, saved_ip); +} + +static int fprobe_resolve_syms(const void *usyms, u32 cnt,
kernel/bpf/syscall.c static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *pro · line 3121
Quoted 10 lines
kernel/bpf/syscall.c +10
-0+ unsigned long *addrs) +{ +unsigned long addr, size; +const char **syms; +int err = -ENOMEM ; +unsigned int i; +char *func; + + size = cnt *sizeof (*syms); + syms =kzalloc (size,GFP_KERNEL );
any reason not to use kvzalloc() here?
Quoted 3 lines
kernel/bpf/syscall.c +3
-0+if (!syms) +return -ENOMEM ; +
[...]
Quoted 4 lines
kernel/bpf/syscall.c +4
-0+ +static int bpf_fprobe_link_attach (const union bpf_attr *attr,struct bpf_prog *prog) +{ +struct bpf_fprobe_link *link =NULL ;
kernel/bpf/syscall.c static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *pro · line 3174
Quoted 10 lines
kernel/bpf/syscall.c +10
-0+struct bpf_link_primer link_primer; +unsigned long *addrs; +u32 flags, cnt, size; +void __user *uaddrs; +void __user *usyms; +int err; + +/* no support for 32bit archs yet */ +if (sizeof (u64) !=sizeof (void *)) +return -EINVAL ;
-EOPNOTSUPP?
kernel/bpf/syscall.c static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *pro · line 3186
Quoted 12 lines
kernel/bpf/syscall.c +12
-0+ +if (prog->expected_attach_type !=BPF_TRACE_FPROBE ) +return -EINVAL ; + + flags = attr->link_create.fprobe.flags; +if (flags & ~BPF_F_FPROBE_RETURN ) +return -EINVAL ; + + uaddrs =u64_to_user_ptr (attr->link_create.fprobe.addrs); + usyms =u64_to_user_ptr (attr->link_create.fprobe.syms); +if ((!uaddrs && !usyms) || (uaddrs && usyms)) +return -EINVAL ;
!!uaddrs == !!usyms ?
kernel/bpf/syscall.c static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *pro · line 3193
Quoted 7 lines
kernel/bpf/syscall.c +7
-0+ + cnt = attr->link_create.fprobe.cnt; +if (!cnt) +return -EINVAL ; + + size = cnt *sizeof (*addrs); + addrs =kzalloc (size,GFP_KERNEL );
same, why not kvzalloc? Also, aren't you overwriting each addrs entry anyway, so "z" is not necessary, right?
Quoted 3 lines
kernel/bpf/syscall.c +3
-0+if (!addrs) +return -ENOMEM ; +
[...]
Re: [PATCH 2/8] bpf: Add bpf_get_func_ip kprobe helper for fprobe link
In reply to Jiri OlsaQuoted 27 lines · Jiri Olsa
Adding support to call
get_func_ip_fprobehelper from kprobe programs attached by fprobe link.Also adding support to inline it, because it's single load instruction.
Signed-off-by Jiri Olsa <[email protected]>--- kernel/bpf/verifier.c | 19 ++++++++++++++++++- kernel/trace/bpf_trace.c | 16 +++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-)kernel/bpf/verifier.c +1
-1@@ -13625,7 +13625,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 */
kernel/bpf/verifier.c static int do_misc_fixups(struct bpf_verifier_env *env) · line 13649
Quoted 10 lines
kernel/bpf/verifier.c +7
-0@@ -13640,6 +13640,23 @@ static int do_misc_fixups(struct bpf_verifier_env *env)continue ; } +/* Implement kprobe/fprobe bpf_get_func_ip inline. */ +if (prog_type ==BPF_PROG_TYPE_KPROBE && + eatype ==BPF_TRACE_FPROBE && + 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));
Isn't this architecture-specific? I'm starting to dislike this inlining whole more and more. It's just a complication in verifier without clear real-world benefits. We are clearly prematurely optimizing here. In practice you'll just call bpf_get_func_ip() once and that's it. Function call overhead will be negligible compare to other *userful* work you'll be doing in your BPF program.
Quoted 50 lines
kernel/bpf/verifier.c +10
-0+ + 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 ; + } + patch_call_imm: fn = env->ops->get_func_proto (insn->imm, env->prog);/* all functions that have prototype and verifier allowed kernel/trace/bpf_trace.c +15
-2@@ -1036,6 +1036,19 @@ static const struct bpf_func_proto bpf_get_func_ip_proto_kprobe = { .arg1_type = ARG_PTR_TO_CTX, }; +BPF_CALL_1(bpf_get_func_ip_fprobe, struct pt_regs *, regs) +{ +/* This helper call is inlined by verifier. */ +return regs->ip; +} + +static const struct bpf_func_proto bpf_get_func_ip_proto_fprobe = { + .func = bpf_get_func_ip_fprobe, + .gpl_only = false, + .ret_type =RET_INTEGER , + .arg1_type =ARG_PTR_TO_CTX , +}; + BPF_CALL_1(bpf_get_attach_cookie_trace, void *, ctx) {struct bpf_trace_run_ctx *run_ctx;@@ -1279,7 +1292,8 @@ kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)return &bpf_override_return_proto;#endif case BPF_FUNC_get_func_ip: -return &bpf_get_func_ip_proto_kprobe; +return prog->expected_attach_type ==BPF_TRACE_FPROBE ? + &bpf_get_func_ip_proto_fprobe : &bpf_get_func_ip_proto_kprobe;case BPF_FUNC_get_attach_cookie:return &bpf_get_attach_cookie_proto_trace;default : --2.34.1
Re: [PATCH 3/8] bpf: Add bpf_cookie support to fprobe
In reply to Jiri OlsaQuoted 36 lines · Jiri Olsa
Adding support to call
bpf_get_attach_cookiehelper from kprobe program attached by fprobe link.The
bpf_cookieis provided by array of u64 values, where each value is paired with provided function address with the same array index.Suggested-by Andrii Nakryiko <[email protected]>Signed-off-by Jiri Olsa <[email protected]>--- include/linux/bpf.h | 2 + include/uapi/linux/bpf.h | 1 + kernel/bpf/syscall.c | 83 +++++++++++++++++++++++++++++++++- kernel/trace/bpf_trace.c | 16 ++++++- tools/include/uapi/linux/bpf.h | 1 + 5 files changed, 100 insertions(+), 3 deletions(-)include/linux/bpf.h +2
-0@@ -1301,6 +1301,8 @@ static inline void bpf_reset_run_ctx(struct bpf_run_ctx *old_ctx)#endif } +u64 bpf_fprobe_cookie (struct bpf_run_ctx *ctx,u64 ip); +/* BPF program asks to bypass CAP_NET_BIND_SERVICE in bind. */ #define BPF_RET_BIND_NO_CAP_NET_BIND_SERVICE (1 << 0)/* BPF program asks to set CN on the packet. */ include/uapi/linux/bpf.h +0
-0
include/uapi/linux/bpf.h union bpf_attr { · line 1487
Quoted 4 lines
include/uapi/linux/bpf.h +1
-0@@ -1484,6 +1484,7 @@ union bpf_attr {__aligned_u64 addrs;__u32 cnt;__u32 flags; +__aligned_u64 bpf_cookies;
maybe put it right after addrs, they are closely related and cnt describes all of syms/addrs/cookies.
Quoted 21 lines
include/uapi/linux/bpf.h +0
-0} fprobe; }; } link_create;kernel/bpf/syscall.c +3
-0@@ -33,6 +33,8 @@#include <linux/rcupdate_trace.h> #include <linux/memcontrol.h> #include <linux/fprobe.h> +#include <linux/bsearch.h> +#include <linux/sort.h> #define IS_FD_ARRAY (map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \ (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \@@ -3025,10 +3027,18 @@ static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *pro#ifdef CONFIG_FPROBE +struct bpf_fprobe_cookie {
kernel/bpf/syscall.c static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *pro · line 3040
Quoted 10 lines
kernel/bpf/syscall.c +6
-0+unsigned long addr; +u64 bpf_cookie; +}; +struct bpf_fprobe_link {struct bpf_link link;struct fprobe fp;unsigned long *addrs; +struct bpf_run_ctx run_ctx; +struct bpf_fprobe_cookie *bpf_cookies;
you already have all the addrs above, why keeping a second copy of each addrs in bpf_fprobe_cookie. Let's have two arrays: addrs (unsigned long) and cookies (u64) and make sure that they are sorted together. Then lookup addrs, calculate index, use that index to fetch cookie.
Seems like sort_r() provides exactly the interface you'd need to do this very easily. Having addrs separate from cookies also a bit advantageous in terms of TLB misses (if you need any more persuasion ;)
Quoted 25 lines
kernel/bpf/syscall.c +11
-0+u32 cnt; };static void bpf_fprobe_link_release (struct bpf_link *link)@@ -3045,6 +3055,7 @@ static void bpf_fprobe_link_dealloc(struct bpf_link *link) fprobe_link = container_of(link, struct bpf_fprobe_link, link);kfree (fprobe_link->addrs); +kfree (fprobe_link->bpf_cookies);kfree (fprobe_link); }@@ -3053,9 +3064,37 @@ static const struct bpf_link_ops bpf_fprobe_link_lops = { .dealloc = bpf_fprobe_link_dealloc, }; +static int bpf_fprobe_cookie_cmp (const void *_a,const void *_b) +{ +const struct bpf_fprobe_cookie *a = _a; +const struct bpf_fprobe_cookie *b = _b; + +if (a->addr == b->addr) +return 0 ; +return a->addr < b->addr ?-1 :1 ; +}
kernel/bpf/syscall.c static const struct bpf_link_ops bpf_fprobe_link_lops = { · line 3085
Quoted 10 lines
kernel/bpf/syscall.c +10
-0+ +u64 bpf_fprobe_cookie (struct bpf_run_ctx *ctx,u64 ip) +{ +struct bpf_fprobe_link *fprobe_link; +struct bpf_fprobe_cookie *val, key = { + .addr = (unsigned long ) ip, + }; + +if (!ctx) +return 0 ;
is it allowed to have ctx == NULL?
Quoted 47 lines
kernel/bpf/syscall.c +16
-1+ fprobe_link = container_of(ctx, struct bpf_fprobe_link, run_ctx); +if (!fprobe_link->bpf_cookies) +return 0 ; + val =bsearch (&key, fprobe_link->bpf_cookies, fprobe_link->cnt, +sizeof (key), bpf_fprobe_cookie_cmp); +return val ? val->bpf_cookie :0 ; +} +static int fprobe_link_prog_run (struct bpf_fprobe_link *fprobe_link,struct pt_regs *regs) { +struct bpf_run_ctx *old_run_ctx;int err;if (unlikely (__this_cpu_inc_return (bpf_prog_active) !=1 )) {@@ -3063,12 +3102,16 @@ static int fprobe_link_prog_run(struct bpf_fprobe_link *fprobe_link, goto out; } + old_run_ctx =bpf_set_run_ctx (&fprobe_link->run_ctx); +rcu_read_lock ();migrate_disable (); err =bpf_prog_run (fprobe_link->link.prog, regs);migrate_enable ();rcu_read_unlock (); +bpf_reset_run_ctx (old_run_ctx); + out:__this_cpu_dec (bpf_prog_active);return err;@@ -3161,10 +3204,12 @@ static int fprobe_resolve_syms(const void *usyms, u32 cnt,static int bpf_fprobe_link_attach (const union bpf_attr *attr,struct bpf_prog *prog) { +struct bpf_fprobe_cookie *bpf_cookies =NULL ;struct bpf_fprobe_link *link =NULL ;struct bpf_link_primer link_primer; +void __user *ubpf_cookies; +u32 flags, cnt, i, size;unsigned long *addrs; -u32 flags, cnt, size;void __user *uaddrs;void __user *usyms;int err;
kernel/bpf/syscall.c static int bpf_fprobe_link_attach(const union bpf_attr *attr, struct bpf_prog *p · line 3253
Quoted 4 lines
kernel/bpf/syscall.c +1
-0@@ -3205,6 +3250,37 @@ static int bpf_fprobe_link_attach(const union bpf_attr *attr, struct bpf_prog *p goto error; } + ubpf_cookies =u64_to_user_ptr (attr->link_create.fprobe.bpf_cookies);
nit: let's call all this "cookies", this bpf_ prefix feels a bit redundant (I know about perf_event.bpf_cookie, but still).
kernel/bpf/syscall.c static int bpf_fprobe_link_attach(const union bpf_attr *attr, struct bpf_prog *p · line 3258
Quoted 5 lines
kernel/bpf/syscall.c +5
-0+if (ubpf_cookies) { +u64 *tmp; + + err = -ENOMEM ; + tmp =kzalloc (size,GFP_KERNEL );
kvmalloc?
Quoted 9 lines
kernel/bpf/syscall.c +9
-0+if (!tmp) + goto error; + +if (copy_from_user (tmp, ubpf_cookies, size)) { +kfree (tmp); + err = -EFAULT ; + goto error; + } +
[...]
Re: [PATCH 8/8] selftest/bpf: Add fprobe test for bpf_cookie values
In reply to Jiri OlsaQuoted 47 lines · Jiri Olsa
Adding
bpf_cookietest for kprobe attached by fprobe link.Signed-off-by Jiri Olsa <[email protected]>--- .../selftests/bpf/prog_tests/bpf_cookie.c | 73 +++++++++++++++++++ .../selftests/bpf/progs/fprobe_bpf_cookie.c | 62 ++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/fprobe_bpf_cookie.ctools/testing/selftests/bpf/prog_tests/bpf_cookie.c +22
-0@@ -7,6 +7,7 @@#include <unistd.h> #include <test_progs.h> #include "test_bpf_cookie.skel.h" +#include "fprobe_bpf_cookie.skel.h" /* uprobe attach point */ static void trigger_func (void )@@ -63,6 +64,76 @@ static void kprobe_subtest(struct test_bpf_cookie *skel)bpf_link__destroy (retlink2); } +static void fprobe_subtest (void ) +{ +DECLARE_LIBBPF_OPTS (bpf_link_create_opts, opts); +int err, prog_fd, link1_fd =-1 , link2_fd =-1 ; +struct fprobe_bpf_cookie *skel =NULL ; +__u32 duration =0 , retval; +__u64 addrs[8 ], cookies[8 ]; + + skel =fprobe_bpf_cookie__open_and_load (); +if (!ASSERT_OK_PTR (skel,"fentry_raw_skel_load" )) + goto cleanup; + +kallsyms_find ("bpf_fentry_test1" , &addrs[0 ]); +kallsyms_find ("bpf_fentry_test2" , &addrs[1 ]); +kallsyms_find ("bpf_fentry_test3" , &addrs[2 ]); +kallsyms_find ("bpf_fentry_test4" , &addrs[3 ]); +kallsyms_find ("bpf_fentry_test5" , &addrs[4 ]); +kallsyms_find ("bpf_fentry_test6" , &addrs[5 ]); +kallsyms_find ("bpf_fentry_test7" , &addrs[6 ]); +kallsyms_find ("bpf_fentry_test8" , &addrs[7 ]); +
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c static void kprobe_subtest(struct test_bpf_cookie *skel) · line 97
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +10
-0+ cookies[0 ] =1 ; + cookies[1 ] =2 ; + cookies[2 ] =3 ; + cookies[3 ] =4 ; + cookies[4 ] =5 ; + cookies[5 ] =6 ; + cookies[6 ] =7 ; + cookies[7 ] =8 ; + + opts.fprobe.addrs = (__u64) &addrs;
we should have ptr_to_u64() for test_progs, but if not, let's either add it or it should be (__u64)(uintptr_t)&addrs. Otherwise we'll be getting compilation warnings on some architectures.
Quoted 43 lines
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +39
-0+ opts.fprobe.cnt =8 ; + opts.fprobe.bpf_cookies = (__u64) &cookies; + prog_fd =bpf_program__fd (skel->progs.test2); + + link1_fd =bpf_link_create (prog_fd,0 ,BPF_TRACE_FPROBE , &opts); +if (!ASSERT_GE (link1_fd,0 ,"link1_fd" )) +return ; + + cookies[0 ] =8 ; + cookies[1 ] =7 ; + cookies[2 ] =6 ; + cookies[3 ] =5 ; + cookies[4 ] =4 ; + cookies[5 ] =3 ; + cookies[6 ] =2 ; + cookies[7 ] =1 ; + + opts.flags =BPF_F_FPROBE_RETURN ; + prog_fd =bpf_program__fd (skel->progs.test3); + + link2_fd =bpf_link_create (prog_fd,0 ,BPF_TRACE_FPROBE , &opts); +if (!ASSERT_GE (link2_fd,0 ,"link2_fd" )) + 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" ); + +ASSERT_EQ (skel->bss->test2_result,8 ,"test2_result" ); +ASSERT_EQ (skel->bss->test3_result,8 ,"test3_result" ); + +cleanup: +close (link1_fd); +close (link2_fd); +fprobe_bpf_cookie__destroy (skel); +} +static void uprobe_subtest (struct test_bpf_cookie *skel) {DECLARE_LIBBPF_OPTS (bpf_uprobe_opts, opts);
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c void test_bpf_cookie(void) · line 323
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +1
-0@@ -249,6 +320,8 @@ void test_bpf_cookie(void)if (test__start_subtest ("kprobe" ))kprobe_subtest (skel); +if (test__start_subtest ("rawkprobe" ))
kprobe.multi?
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +1
-0+fprobe_subtest ();if (test__start_subtest ("uprobe" ))uprobe_subtest (skel);if (test__start_subtest ("tracepoint" ))
[...]
Re: [PATCH 4/8] libbpf: Add libbpf__kallsyms_parse function
In reply to Jiri OlsaQuoted 17 lines · Jiri Olsa
Move the kallsyms parsing in internal
libbpf__kallsyms_parsefunction, so it can be used from other places.It will be used in following changes.
Signed-off-by Jiri Olsa <[email protected]>--- tools/lib/bpf/libbpf.c | 62 ++++++++++++++++++++------------- tools/lib/bpf/libbpf_internal.h | 5 +++ 2 files changed, 43 insertions(+), 24 deletions(-)tools/lib/bpf/libbpf.c +0
-0
tools/lib/bpf/libbpf.c static int bpf_object__sanitize_maps(struct bpf_object *obj) · line 7168
Quoted 5 lines
tools/lib/bpf/libbpf.c +1
-1@@ -7165,12 +7165,10 @@ static int bpf_object__sanitize_maps(struct bpf_object *obj)return 0 ; } -static int bpf_object__read_kallsyms_file (struct bpf_object *obj) +int libbpf__kallsyms_parse (void *arg,kallsyms_cb_t cb)
please call it libbpf_kallsyms_parse(), internal APIs don't use "object oriented" double underscore separator
also this "arg" is normally called "ctx" in similar APIs in libbpf and is passed the last, can you please adjust all that for consistency?
Quoted 8 lines
tools/lib/bpf/libbpf.c +0
-2{char sym_type, sym_name[500 ];unsigned long long sym_addr; -const struct btf_type *t; -struct extern_desc *ext;int ret, err =0 ;FILE *f;
[...]
Re: [PATCH 6/8] libbpf: Add bpf_program__attach_kprobe_opts for multi kprobes
In reply to Jiri OlsaQuoted 46 lines · Jiri Olsa
Adding support to
bpf_program__attach_kprobe_optsto load kprobes to multiple functions.If the kprobe program has
BPF_TRACE_FPROBEasexpected_attach_typeit will use the new fprobe link to attach the program. In this case it will use 'func_name' as pattern for functions to attach.Adding also support to use '*' wildcard in 'kprobe/kretprobe' section name by SEC macro, like:
SEC("kprobe/bpf_fentry_test*")
SEC("kretprobe/bpf_fentry_test*")This will set kprobe's
expected_attach_typetoBPF_TRACE_FPROBE, and attach it to provided functions pattern.Signed-off-by Jiri Olsa <[email protected]>--- tools/lib/bpf/libbpf.c | 136 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 133 insertions(+), 3 deletions(-)tools/lib/bpf/libbpf.c +4
-2@@ -8607,13 +8607,15 @@ static struct bpf_link *attach_trace(const struct bpf_program *prog, long cookiestatic struct bpf_link *attach_lsm (const struct bpf_program *prog,long cookie);static struct bpf_link *attach_iter (const struct bpf_program *prog,long cookie); +static int init_kprobe (struct bpf_program *prog,long cookie); +static const struct bpf_sec_def section_defs[] = {SEC_DEF ("socket" ,SOCKET_FILTER ,0 ,SEC_NONE |SEC_SLOPPY_PFX ),SEC_DEF ("sk_reuseport/migrate" ,SK_REUSEPORT ,BPF_SK_REUSEPORT_SELECT_OR_MIGRATE ,SEC_ATTACHABLE |SEC_SLOPPY_PFX ),SEC_DEF ("sk_reuseport" ,SK_REUSEPORT ,BPF_SK_REUSEPORT_SELECT ,SEC_ATTACHABLE |SEC_SLOPPY_PFX ), -SEC_DEF ("kprobe/" ,KPROBE ,0 ,SEC_NONE , attach_kprobe), + SEC_DEF("kprobe/", KPROBE, 0, SEC_NONE, attach_kprobe, .init_fn = init_kprobe),SEC_DEF ("uprobe/" ,KPROBE ,0 ,SEC_NONE ), -SEC_DEF ("kretprobe/" ,KPROBE ,0 ,SEC_NONE , attach_kprobe), + SEC_DEF("kretprobe/", KPROBE, 0, SEC_NONE, attach_kprobe, .init_fn = init_kprobe),SEC_DEF ("uretprobe/" ,KPROBE ,0 ,SEC_NONE ),SEC_DEF ("tc" ,SCHED_CLS ,0 ,SEC_NONE ),SEC_DEF ("classifier" ,SCHED_CLS ,0 ,SEC_NONE |SEC_SLOPPY_PFX |SEC_DEPRECATED ),@@ -10031,6 +10033,123 @@ static int perf_event_kprobe_open_legacy(const char *probe_name, bool retprobe,return pfd;
tools/lib/bpf/libbpf.c static int perf_event_kprobe_open_legacy(const char *probe_name, bool retprobe, · line 10043
Quoted 10 lines
tools/lib/bpf/libbpf.c +8
-0} +struct fprobe_resolve { +const char *name; +__u64 *addrs; +__u32 alloc; +__u32 cnt; +}; + +static bool glob_matches (const char *glob,const char *s)
we've since added more generic glob_match() implementation (see test_progs.c), let's copy/paste that one (it's actually shorter and doesn't do hacky input args modification). Let's maybe also add '?' handling (it's trivial). Both original code in perf and the one in test_progs.c are GPL-2.0-only, so let's also get acks from original authors.
Quoted 41 lines
tools/lib/bpf/libbpf.c +41
-0+{ +int n =strlen (glob); + +if (n ==1 && glob[0 ] =='*' ) +return true; + +if (glob[0 ] =='*' && glob[n -1 ] =='*' ) { +const char *subs; +/* substring match */ + +/* this is hacky, but we don't want to allocate +* for no good reason +*/ + ((char *)glob)[n -1 ] ='\0' ; + subs =strstr (s, glob +1 ); + ((char *)glob)[n -1 ] ='*' ; + +return subs !=NULL ; + }else if (glob[0 ] =='*' ) { +size_t nn =strlen (s); +/* suffix match */ + +/* too short for a given suffix */ +if (nn < n -1 ) +return false; +return strcmp (s + nn - (n -1 ), glob +1 ) ==0 ; + }else if (glob[n -1 ] =='*' ) { +/* prefix match */ +return strncmp (s, glob, n -1 ) ==0 ; + }else { +/* exact match */ +return strcmp (glob, s) ==0 ; + } +} + +static int resolve_fprobe_cb (void *arg,unsigned long long sym_addr, +char sym_type,const char *sym_name) +{ +struct fprobe_resolve *res = arg; +__u64 *p; +
tools/lib/bpf/libbpf.c static int perf_event_kprobe_open_legacy(const char *probe_name, bool retprobe, · line 10094
Quoted 10 lines
tools/lib/bpf/libbpf.c +10
-0+if (!glob_matches (res->name, sym_name)) +return 0 ; + +if (res->cnt == res->alloc) { + res->alloc =max ((__u32 )16 , res->alloc *3 /2 ); + p =libbpf_reallocarray (res->addrs, res->alloc,sizeof (__u32)); +if (!p) +return -ENOMEM ; + res->addrs = p; + }
please use libbpf_ensure_mem() instead
tools/lib/bpf/libbpf.c static int perf_event_kprobe_open_legacy(const char *probe_name, bool retprobe, · line 10101
Quoted 7 lines
tools/lib/bpf/libbpf.c +7
-0+ res->addrs[res->cnt++] = sym_addr; +return 0 ; +} + +static struct bpf_link * +attach_fprobe_opts(const struct bpf_program *prog, + const char *func_name,
func_glob or func_pattern?
Quoted 12 lines
tools/lib/bpf/libbpf.c +12
-0+ const struct bpf_kprobe_opts *kopts) +{ +DECLARE_LIBBPF_OPTS (bpf_link_create_opts, opts); +struct fprobe_resolve res = { + .name = func_name, + }; +struct bpf_link *link =NULL ; +char errmsg[STRERR_BUFSIZE ]; +int err, link_fd, prog_fd; +bool retprobe; + + err =libbpf__kallsyms_parse (&res, resolve_fprobe_cb);
tools/lib/bpf/libbpf.c static int perf_event_kprobe_open_legacy(const char *probe_name, bool retprobe, · line 10123
Quoted 10 lines
tools/lib/bpf/libbpf.c +10
-0+if (err) + goto error; +if (!res.cnt) { + err = -ENOENT ; + goto error; + } + + retprobe =OPTS_GET (kopts, retprobe, false); + + opts.fprobe.addrs = (__u64 ) res.addrs;
ptr_to_u64()
Quoted 43 lines
tools/lib/bpf/libbpf.c +32
-0+ opts.fprobe.cnt = res.cnt; + opts.flags = retprobe ?BPF_F_FPROBE_RETURN :0 ; + + link =calloc (1 ,sizeof (*link)); +if (!link) { + err = -ENOMEM ; + goto error; + } + link->detach = &bpf_link__detach_fd; + + prog_fd =bpf_program__fd (prog); + link_fd =bpf_link_create (prog_fd,0 ,BPF_TRACE_FPROBE , &opts); +if (link_fd <0 ) { + err = -errno; +pr_warn ("prog '%s': failed to attach to %s: %s\n" , + prog->name, res.name, +libbpf_strerror_r (err, errmsg,sizeof (errmsg))); + goto error; + } + link->fd = link_fd; +free (res.addrs); +return link; + +error: +free (link); +free (res.addrs); +return libbpf_err_ptr (err); +} +struct bpf_link * bpf_program__attach_kprobe_opts(const struct bpf_program *prog, const char *func_name,@@ -10047,6 +10166,9 @@ bpf_program__attach_kprobe_opts(const struct bpf_program *prog,if (!OPTS_VALID (opts, bpf_kprobe_opts))return libbpf_err_ptr (-EINVAL ); +if (prog->expected_attach_type ==BPF_TRACE_FPROBE ) +return attach_fprobe_opts (prog, func_name, opts); + retprobe =OPTS_GET (opts, retprobe, false); offset =OPTS_GET (opts, offset,0 ); pe_opts.bpf_cookie =OPTS_GET (opts, bpf_cookie,0 );
tools/lib/bpf/libbpf.c struct bpf_link *bpf_program__attach_kprobe(const struct bpf_program *prog, · line 10240
Quoted 7 lines
tools/lib/bpf/libbpf.c +4
-0@@ -10112,6 +10234,14 @@ struct bpf_link *bpf_program__attach_kprobe(const struct bpf_program *prog,return bpf_program__attach_kprobe_opts (prog, func_name, &opts); } +static int init_kprobe (struct bpf_program *prog,long cookie) +{ +/* If we have wildcard, switch to fprobe link. */ +if (strchr (prog->sec_name,'*' ))
ugh... :( maybe let's have a separate SEC("kprobe.multi/<glob>") and same for kretprobe?
Quoted 19 lines
tools/lib/bpf/libbpf.c +5
-2+bpf_program__set_expected_attach_type (prog,BPF_TRACE_FPROBE ); +return 0 ; +} +static struct bpf_link *attach_kprobe (const struct bpf_program *prog,long cookie) {DECLARE_LIBBPF_OPTS (bpf_kprobe_opts, opts);@@ -10127,7 +10257,7 @@ static struct bpf_link *attach_kprobe(const struct bpf_program *prog, long cookielse func_name = prog->sec_name +sizeof ("kprobe/" ) -1 ; - n =sscanf (func_name,"%m[a-zA-Z0-9_.]+%li" , &func, &offset); + n =sscanf (func_name,"%m[a-zA-Z0-9_.*]+%li" , &func, &offset);if (n <1 ) { err = -EINVAL ;pr_warn ("kprobe name is invalid: %s\n" , func_name); --2.34.1
Re: [PATCH 2/8] bpf: Add bpf_get_func_ip kprobe helper for fprobe link
In reply to Andrii NakryikoQuoted 32 lines · Andrii Nakryiko
Quoted 30 lines · Jiri Olsa
Adding support to call
get_func_ip_fprobehelper from kprobe programs attached by fprobe link.Also adding support to inline it, because it's single load instruction.
Signed-off-by Jiri Olsa <[email protected]>--- kernel/bpf/verifier.c | 19 ++++++++++++++++++- kernel/trace/bpf_trace.c | 16 +++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-)kernel/bpf/verifier.c +1
-1@@ -13625,7 +13625,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 */ @@ -13640,6 +13640,23 @@ static int do_misc_fixups(struct bpf_verifier_env *env)continue ; }
Quoted 14 lines
kernel/bpf/verifier.c static int do_misc_fixups(struct bpf_verifier_env *env) · line 13649
Quoted 7 lines
kernel/bpf/verifier.c +7
-0+/* Implement kprobe/fprobe bpf_get_func_ip inline. */ +if (prog_type ==BPF_PROG_TYPE_KPROBE && + eatype ==BPF_TRACE_FPROBE && + 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));Isn't this architecture-specific? I'm starting to dislike this inlining whole more and more. It's just a complication in verifier without clear real-world benefits. We are clearly prematurely optimizing here. In practice you'll just call
bpf_get_func_ip()once and that's it. Function call overhead will be negligible compare to other *userful* work you'll be doing in your BPF program.
We should be doing inlining when we can.
Every bit of performance matters.
Re: [PATCH 1/8] bpf: Add support to attach kprobe program with fprobe
In reply to Andrii NakryikoQuoted 66 lines · Andrii Nakryiko
Quoted 35 lines · Jiri Olsa
Adding new link type
BPF_LINK_TYPE_FPROBEthat attaches kprobe program through fprobe API.The fprobe API allows to attach probe on multiple functions at once very fast, because it works on top of ftrace. On the other hand this limits the probe point to the function entry or return.
The kprobe program gets the same
pt_regsinput ctx as when it's attached through the perf API.Adding new attach type
BPF_TRACE_FPROBEthat enables such link for kprobe program.User provides array of addresses or symbols with count to attach the kprobe program to. The new
link_createuapi interface looks like:struct { __aligned_u64 syms; __aligned_u64 addrs; __u32 cnt; __u32 flags; } fprobe;The flags field allows single
BPF_F_FPROBE_RETURNbit to create return fprobe.Signed-off-by Masami Hiramatsu <[email protected]>Signed-off-by Jiri Olsa <[email protected]>--- include/linux/bpf_types.h | 1 + include/uapi/linux/bpf.h | 13 ++ kernel/bpf/syscall.c | 248 ++++++++++++++++++++++++++++++++- tools/include/uapi/linux/bpf.h | 13 ++ 4 files changed, 270 insertions(+), 5 deletions(-)[...]
Quoted 27 lines
kernel/bpf/syscall.c +26
-0+#ifdef CONFIG_FPROBE + +struct bpf_fprobe_link { +struct bpf_link link; +struct fprobe fp; +unsigned long *addrs; +}; + +static void bpf_fprobe_link_release (struct bpf_link *link) +{ +struct bpf_fprobe_link *fprobe_link; + + fprobe_link = container_of(link, struct bpf_fprobe_link, link); +unregister_fprobe (&fprobe_link->fp); +} + +static void bpf_fprobe_link_dealloc (struct bpf_link *link) +{ +struct bpf_fprobe_link *fprobe_link; + + fprobe_link = container_of(link, struct bpf_fprobe_link, link); +kfree (fprobe_link->addrs); +kfree (fprobe_link); +} + +static const struct bpf_link_ops bpf_fprobe_link_lops = {
Quoted 8 lines
kernel/bpf/syscall.c static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *pro · line 3055
Quoted 4 lines
kernel/bpf/syscall.c +4
-0+ .release = bpf_fprobe_link_release, + .dealloc = bpf_fprobe_link_dealloc, +}; +should this whole new link implementation (including
fprobe_link_prog_run()below) maybe live inkernel/trace/bpf_trace.c? Seems a bit more fitting thankernel/bpf/syscall.c
right, it's trace related
Quoted 39 lines
Quoted 39 lines
kernel/bpf/syscall.c +38
-0+static int fprobe_link_prog_run (struct bpf_fprobe_link *fprobe_link, +struct pt_regs *regs) +{ +int err; + +if (unlikely (__this_cpu_inc_return (bpf_prog_active) !=1 )) { + err =0 ; + goto out; + } + +rcu_read_lock (); +migrate_disable (); + err =bpf_prog_run (fprobe_link->link.prog, regs); +migrate_enable (); +rcu_read_unlock (); + + out: +__this_cpu_dec (bpf_prog_active); +return err; +} + +static void fprobe_link_entry_handler (struct fprobe *fp,unsigned long entry_ip, +struct pt_regs *regs) +{ +unsigned long saved_ip =instruction_pointer (regs); +struct bpf_fprobe_link *fprobe_link; + +/* +* Because fprobe's regs->ip is set to the next instruction of +* dynamic-ftrace insturction, correct entry ip must be set, so +* that the bpf program can access entry address via regs as same +* as kprobes. +*/ +instruction_pointer_set (regs, entry_ip); + + fprobe_link = container_of(fp, struct bpf_fprobe_link, fp); +fprobe_link_prog_run (fprobe_link, regs); +
Quoted 8 lines
kernel/bpf/syscall.c static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *pro · line 3098
Quoted 5 lines
kernel/bpf/syscall.c +5
-0+instruction_pointer_set (regs, saved_ip); +} + +static void fprobe_link_exit_handler (struct fprobe *fp,unsigned long entry_ip, +struct pt_regs *regs)isn't it identical to
fprobe_lnk_entry_handler? Maybe use one callback for both entry and exit?
heh, did not notice that :) yep, looks that way, will check
Quoted 18 lines
Quoted 18 lines
kernel/bpf/syscall.c +17
-0+{ +unsigned long saved_ip =instruction_pointer (regs); +struct bpf_fprobe_link *fprobe_link; + +instruction_pointer_set (regs, entry_ip); + + fprobe_link = container_of(fp, struct bpf_fprobe_link, fp); +fprobe_link_prog_run (fprobe_link, regs); + +instruction_pointer_set (regs, saved_ip); +} + +static int fprobe_resolve_syms (const void *usyms,u32 cnt, +unsigned long *addrs) +{ +unsigned long addr, size; +const char **syms;
Quoted 8 lines
kernel/bpf/syscall.c static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *pro · line 3121
Quoted 6 lines
kernel/bpf/syscall.c +6
-0+int err = -ENOMEM ; +unsigned int i; +char *func; + + size = cnt *sizeof (*syms); + syms =kzalloc (size,GFP_KERNEL );any reason not to use
kvzalloc()here?
probably just my ignorance ;-) will check
Quoted 7 lines
Quoted 4 lines
kernel/bpf/syscall.c +3
-0+if (!syms) +return -ENOMEM ; +[...]
Quoted 16 lines
Quoted 4 lines
kernel/bpf/syscall.c +4
-0+ +static int bpf_fprobe_link_attach (const union bpf_attr *attr,struct bpf_prog *prog) +{ +struct bpf_fprobe_link *link =NULL ;kernel/bpf/syscall.c static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *pro · line 3174
Quoted 10 lines
kernel/bpf/syscall.c +10
-0+struct bpf_link_primer link_primer; +unsigned long *addrs; +u32 flags, cnt, size; +void __user *uaddrs; +void __user *usyms; +int err; + +/* no support for 32bit archs yet */ +if (sizeof (u64) !=sizeof (void *)) +return -EINVAL ;
-EOPNOTSUPP?
ok
Quoted 15 lines
kernel/bpf/syscall.c static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *pro · line 3186
Quoted 13 lines
kernel/bpf/syscall.c +12
-0+ +if (prog->expected_attach_type !=BPF_TRACE_FPROBE ) +return -EINVAL ; + + flags = attr->link_create.fprobe.flags; +if (flags & ~BPF_F_FPROBE_RETURN ) +return -EINVAL ; + + uaddrs =u64_to_user_ptr (attr->link_create.fprobe.addrs); + usyms =u64_to_user_ptr (attr->link_create.fprobe.syms); +if ((!uaddrs && !usyms) || (uaddrs && usyms)) +return -EINVAL ;!!uaddrs == !!usyms ?
ah right, will change
Quoted 11 lines
kernel/bpf/syscall.c static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *pro · line 3193
Quoted 8 lines
kernel/bpf/syscall.c +7
-0+ + cnt = attr->link_create.fprobe.cnt; +if (!cnt) +return -EINVAL ; + + size = cnt *sizeof (*addrs); + addrs =kzalloc (size,GFP_KERNEL );same, why not kvzalloc? Also, aren't you overwriting each addrs entry anyway, so "z" is not necessary, right?
true, no need for zeroing
thanks,
jirka
Re: [PATCH 3/8] bpf: Add bpf_cookie support to fprobe
In reply to Andrii NakryikoQuoted 36 lines · Andrii Nakryiko
Quoted 35 lines · Jiri Olsa
Adding support to call
bpf_get_attach_cookiehelper from kprobe program attached by fprobe link.The
bpf_cookieis provided by array of u64 values, where each value is paired with provided function address with the same array index.Suggested-by Andrii Nakryiko <[email protected]>Signed-off-by Jiri Olsa <[email protected]>--- include/linux/bpf.h | 2 + include/uapi/linux/bpf.h | 1 + kernel/bpf/syscall.c | 83 +++++++++++++++++++++++++++++++++- kernel/trace/bpf_trace.c | 16 ++++++- tools/include/uapi/linux/bpf.h | 1 + 5 files changed, 100 insertions(+), 3 deletions(-)include/linux/bpf.h +2
-0@@ -1301,6 +1301,8 @@ static inline void bpf_reset_run_ctx(struct bpf_run_ctx *old_ctx)#endif } +u64 bpf_fprobe_cookie (struct bpf_run_ctx *ctx,u64 ip); +/* BPF program asks to bypass CAP_NET_BIND_SERVICE in bind. */ #define BPF_RET_BIND_NO_CAP_NET_BIND_SERVICE (1 << 0)/* BPF program asks to set CN on the packet. */ include/uapi/linux/bpf.h +0
-0
Quoted 8 lines
include/uapi/linux/bpf.h union bpf_attr { · line 1487
Quoted 5 lines
include/uapi/linux/bpf.h +1
-0@@ -1484,6 +1484,7 @@ union bpf_attr {__aligned_u64 addrs;__u32 cnt;__u32 flags; +__aligned_u64 bpf_cookies;maybe put it right after addrs, they are closely related and cnt describes all of syms/addrs/cookies.
ok
Quoted 39 lines
Quoted 22 lines
include/uapi/linux/bpf.h +0
-0} fprobe; }; } link_create;kernel/bpf/syscall.c +3
-0@@ -33,6 +33,8 @@#include <linux/rcupdate_trace.h> #include <linux/memcontrol.h> #include <linux/fprobe.h> +#include <linux/bsearch.h> +#include <linux/sort.h> #define IS_FD_ARRAY (map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \ (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \@@ -3025,10 +3027,18 @@ static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *pro#ifdef CONFIG_FPROBE +struct bpf_fprobe_cookie {kernel/bpf/syscall.c static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *pro · line 3040
Quoted 10 lines
kernel/bpf/syscall.c +6
-0+unsigned long addr; +u64 bpf_cookie; +}; +struct bpf_fprobe_link {struct bpf_link link;struct fprobe fp;unsigned long *addrs; +struct bpf_run_ctx run_ctx; +struct bpf_fprobe_cookie *bpf_cookies;you already have all the addrs above, why keeping a second copy of each addrs in
bpf_fprobe_cookie. Let's have two arrays: addrs (unsigned long) and cookies (u64) and make sure that they are sorted together. Then lookup addrs, calculate index, use that index to fetch cookie.
Quoted 4 lines
Seems like
sort_r()provides exactly the interface you'd need to do this very easily. Having addrs separate from cookies also a bit advantageous in terms of TLB misses (if you need any more persuasion ;)
no persuation needed, I actually tried that but it turned out sort_r is not ready yet ;-)
because you can't pass priv pointer to the swap callback, so we can't swap the other array.. I did a change to allow that, but it's not trivial and will need some bigger testing/review because the original sort calls sort_r, and of course there are many 'sort' users ;-)
Quoted 30 lines
Quoted 30 lines
kernel/bpf/syscall.c +15
-0+u32 cnt; };static void bpf_fprobe_link_release (struct bpf_link *link)@@ -3045,6 +3055,7 @@ static void bpf_fprobe_link_dealloc(struct bpf_link *link) fprobe_link = container_of(link, struct bpf_fprobe_link, link);kfree (fprobe_link->addrs); +kfree (fprobe_link->bpf_cookies);kfree (fprobe_link); }@@ -3053,9 +3064,37 @@ static const struct bpf_link_ops bpf_fprobe_link_lops = { .dealloc = bpf_fprobe_link_dealloc, }; +static int bpf_fprobe_cookie_cmp (const void *_a,const void *_b) +{ +const struct bpf_fprobe_cookie *a = _a; +const struct bpf_fprobe_cookie *b = _b; + +if (a->addr == b->addr) +return 0 ; +return a->addr < b->addr ?-1 :1 ; +} + +u64 bpf_fprobe_cookie (struct bpf_run_ctx *ctx,u64 ip) +{ +struct bpf_fprobe_link *fprobe_link;
Quoted 8 lines
kernel/bpf/syscall.c static const struct bpf_link_ops bpf_fprobe_link_lops = { · line 3085
Quoted 6 lines
kernel/bpf/syscall.c +6
-0+struct bpf_fprobe_cookie *val, key = { + .addr = (unsigned long ) ip, + }; + +if (!ctx) +return 0 ;is it allowed to have ctx == NULL?
nope, I was also thinking this is more 'WARN_ON[_ONCE]' check
Quoted 51 lines
Quoted 51 lines
kernel/bpf/syscall.c +16
-1+ fprobe_link = container_of(ctx, struct bpf_fprobe_link, run_ctx); +if (!fprobe_link->bpf_cookies) +return 0 ; + val =bsearch (&key, fprobe_link->bpf_cookies, fprobe_link->cnt, +sizeof (key), bpf_fprobe_cookie_cmp); +return val ? val->bpf_cookie :0 ; +} +static int fprobe_link_prog_run (struct bpf_fprobe_link *fprobe_link,struct pt_regs *regs) { +struct bpf_run_ctx *old_run_ctx;int err;if (unlikely (__this_cpu_inc_return (bpf_prog_active) !=1 )) {@@ -3063,12 +3102,16 @@ static int fprobe_link_prog_run(struct bpf_fprobe_link *fprobe_link, goto out; } + old_run_ctx =bpf_set_run_ctx (&fprobe_link->run_ctx); +rcu_read_lock ();migrate_disable (); err =bpf_prog_run (fprobe_link->link.prog, regs);migrate_enable ();rcu_read_unlock (); +bpf_reset_run_ctx (old_run_ctx); + out:__this_cpu_dec (bpf_prog_active);return err;@@ -3161,10 +3204,12 @@ static int fprobe_resolve_syms(const void *usyms, u32 cnt,static int bpf_fprobe_link_attach (const union bpf_attr *attr,struct bpf_prog *prog) { +struct bpf_fprobe_cookie *bpf_cookies =NULL ;struct bpf_fprobe_link *link =NULL ;struct bpf_link_primer link_primer; +void __user *ubpf_cookies; +u32 flags, cnt, i, size;unsigned long *addrs; -u32 flags, cnt, size;void __user *uaddrs;void __user *usyms;int err;@@ -3205,6 +3250,37 @@ static int bpf_fprobe_link_attach(const union bpf_attr *attr, struct bpf_prog *p goto error; }
Quoted 4 lines
kernel/bpf/syscall.c static int bpf_fprobe_link_attach(const union bpf_attr *attr, struct bpf_prog *p · line 3253
Quoted 1 line
kernel/bpf/syscall.c +1
-0+ ubpf_cookies =u64_to_user_ptr (attr->link_create.fprobe.bpf_cookies);nit: let's call all this "cookies", this
bpf_prefix feels a bit redundant (I know about perf_event.bpf_cookie, but still).
ok
Quoted 8 lines
kernel/bpf/syscall.c static int bpf_fprobe_link_attach(const union bpf_attr *attr, struct bpf_prog *p · line 3258
Quoted 6 lines
kernel/bpf/syscall.c +5
-0+if (ubpf_cookies) { +u64 *tmp; + + err = -ENOMEM ; + tmp =kzalloc (size,GFP_KERNEL );kvmalloc?
ok
thanks,
jirka
Re: [PATCH 4/8] libbpf: Add libbpf__kallsyms_parse function
In reply to Andrii NakryikoQuoted 21 lines · Andrii Nakryiko
Quoted 20 lines · Jiri Olsa
Move the kallsyms parsing in internal
libbpf__kallsyms_parsefunction, so it can be used from other places.It will be used in following changes.
Signed-off-by Jiri Olsa <[email protected]>--- tools/lib/bpf/libbpf.c | 62 ++++++++++++++++++++------------- tools/lib/bpf/libbpf_internal.h | 5 +++ 2 files changed, 43 insertions(+), 24 deletions(-)tools/lib/bpf/libbpf.c +0
-0@@ -7165,12 +7165,10 @@ static int bpf_object__sanitize_maps(struct bpf_object *obj)return 0 ; }
Quoted 8 lines
tools/lib/bpf/libbpf.c static int bpf_object__sanitize_maps(struct bpf_object *obj) · line 7168
Quoted 2 lines
tools/lib/bpf/libbpf.c +1
-1-static int bpf_object__read_kallsyms_file (struct bpf_object *obj) +int libbpf__kallsyms_parse (void *arg,kallsyms_cb_t cb)please call it
libbpf_kallsyms_parse(), internal APIs don't use "object oriented" double underscore separatoralso this "arg" is normally called "ctx" in similar APIs in libbpf and is passed the last, can you please adjust all that for consistency?
ok, thanks
jirka
Quoted 12 lines
Quoted 9 lines
tools/lib/bpf/libbpf.c +0
-2{char sym_type, sym_name[500 ];unsigned long long sym_addr; -const struct btf_type *t; -struct extern_desc *ext;int ret, err =0 ;FILE *f;[...]
Re: [PATCH 6/8] libbpf: Add bpf_program__attach_kprobe_opts for multi kprobes
In reply to Andrii NakryikoOn Mon, Feb 07, 2022 at 10:59:29AM -0800, Andrii Nakryiko wrote:
SNIP
Quoted 7 lines
Quoted 7 lines
tools/lib/bpf/libbpf.c +7
-0+struct fprobe_resolve { +const char *name; +__u64 *addrs; +__u32 alloc; +__u32 cnt; +}; +
Quoted 8 lines
Quoted 1 line
+static bool
glob_matches(const char *glob, const char *s)we've since added more generic
glob_match()implementation (seetest_progs.c), let's copy/paste that one (it's actually shorter and doesn't do hacky input args modification). Let's maybe also add '?' handling (it's trivial). Both original code in perf and the one intest_progs.care GPL-2.0-only, so let's also get acks from original authors.
ok, will check
Quoted 46 lines
Quoted 46 lines
tools/lib/bpf/libbpf.c +45
-0+{ +int n =strlen (glob); + +if (n ==1 && glob[0 ] =='*' ) +return true; + +if (glob[0 ] =='*' && glob[n -1 ] =='*' ) { +const char *subs; +/* substring match */ + +/* this is hacky, but we don't want to allocate +* for no good reason +*/ + ((char *)glob)[n -1 ] ='\0' ; + subs =strstr (s, glob +1 ); + ((char *)glob)[n -1 ] ='*' ; + +return subs !=NULL ; + }else if (glob[0 ] =='*' ) { +size_t nn =strlen (s); +/* suffix match */ + +/* too short for a given suffix */ +if (nn < n -1 ) +return false; +return strcmp (s + nn - (n -1 ), glob +1 ) ==0 ; + }else if (glob[n -1 ] =='*' ) { +/* prefix match */ +return strncmp (s, glob, n -1 ) ==0 ; + }else { +/* exact match */ +return strcmp (glob, s) ==0 ; + } +} + +static int resolve_fprobe_cb (void *arg,unsigned long long sym_addr, +char sym_type,const char *sym_name) +{ +struct fprobe_resolve *res = arg; +__u64 *p; + +if (!glob_matches (res->name, sym_name)) +return 0 ; + +if (res->cnt == res->alloc) {
Quoted 8 lines
tools/lib/bpf/libbpf.c static int perf_event_kprobe_open_legacy(const char *probe_name, bool retprobe, · line 10094
Quoted 6 lines
tools/lib/bpf/libbpf.c +6
-0+ res->alloc =max ((__u32 )16 , res->alloc *3 /2 ); + p =libbpf_reallocarray (res->addrs, res->alloc,sizeof (__u32)); +if (!p) +return -ENOMEM ; + res->addrs = p; + }please use
libbpf_ensure_mem()instead
ok
Quoted 11 lines
tools/lib/bpf/libbpf.c static int perf_event_kprobe_open_legacy(const char *probe_name, bool retprobe, · line 10101
Quoted 9 lines
tools/lib/bpf/libbpf.c +7
-0+ res->addrs[res->cnt++] = sym_addr; +return 0 ; +} + +static struct bpf_link * +attach_fprobe_opts(const struct bpf_program *prog, + const char *func_name,
func_globorfunc_pattern?
ok
Quoted 17 lines
Quoted 17 lines
tools/lib/bpf/libbpf.c +16
-0+ const struct bpf_kprobe_opts *kopts) +{ +DECLARE_LIBBPF_OPTS (bpf_link_create_opts, opts); +struct fprobe_resolve res = { + .name = func_name, + }; +struct bpf_link *link =NULL ; +char errmsg[STRERR_BUFSIZE ]; +int err, link_fd, prog_fd; +bool retprobe; + + err =libbpf__kallsyms_parse (&res, resolve_fprobe_cb); +if (err) + goto error; +if (!res.cnt) { + err = -ENOENT ;
Quoted 8 lines
tools/lib/bpf/libbpf.c static int perf_event_kprobe_open_legacy(const char *probe_name, bool retprobe, · line 10123
Quoted 6 lines
tools/lib/bpf/libbpf.c +6
-0+ goto error; + } + + retprobe =OPTS_GET (kopts, retprobe, false); + + opts.fprobe.addrs = (__u64 ) res.addrs;
ptr_to_u64()
ok
Quoted 47 lines
Quoted 47 lines
tools/lib/bpf/libbpf.c +32
-0+ opts.fprobe.cnt = res.cnt; + opts.flags = retprobe ?BPF_F_FPROBE_RETURN :0 ; + + link =calloc (1 ,sizeof (*link)); +if (!link) { + err = -ENOMEM ; + goto error; + } + link->detach = &bpf_link__detach_fd; + + prog_fd =bpf_program__fd (prog); + link_fd =bpf_link_create (prog_fd,0 ,BPF_TRACE_FPROBE , &opts); +if (link_fd <0 ) { + err = -errno; +pr_warn ("prog '%s': failed to attach to %s: %s\n" , + prog->name, res.name, +libbpf_strerror_r (err, errmsg,sizeof (errmsg))); + goto error; + } + link->fd = link_fd; +free (res.addrs); +return link; + +error: +free (link); +free (res.addrs); +return libbpf_err_ptr (err); +} +struct bpf_link * bpf_program__attach_kprobe_opts(const struct bpf_program *prog, const char *func_name,@@ -10047,6 +10166,9 @@ bpf_program__attach_kprobe_opts(const struct bpf_program *prog,if (!OPTS_VALID (opts, bpf_kprobe_opts))return libbpf_err_ptr (-EINVAL ); +if (prog->expected_attach_type ==BPF_TRACE_FPROBE ) +return attach_fprobe_opts (prog, func_name, opts); + retprobe =OPTS_GET (opts, retprobe, false); offset =OPTS_GET (opts, offset,0 ); pe_opts.bpf_cookie =OPTS_GET (opts, bpf_cookie,0 );@@ -10112,6 +10234,14 @@ struct bpf_link *bpf_program__attach_kprobe(const struct bpf_program *prog,return bpf_program__attach_kprobe_opts (prog, func_name, &opts); }
Quoted 7 lines
tools/lib/bpf/libbpf.c struct bpf_link *bpf_program__attach_kprobe(const struct bpf_program *prog, · line 10240
Quoted 4 lines
tools/lib/bpf/libbpf.c +4
-0+static int init_kprobe (struct bpf_program *prog,long cookie) +{ +/* If we have wildcard, switch to fprobe link. */ +if (strchr (prog->sec_name,'*' ))ugh... :( maybe let's have a separate
SEC("kprobe.multi/<glob>")and same for kretprobe?
I agree new SEC type is more clear ;-) ok
thanks,
jirka
Re: [PATCH 8/8] selftest/bpf: Add fprobe test for bpf_cookie values
In reply to Andrii NakryikoQuoted 54 lines · Andrii Nakryiko
Quoted 53 lines · Jiri Olsa
Adding
bpf_cookietest for kprobe attached by fprobe link.Signed-off-by Jiri Olsa <[email protected]>--- .../selftests/bpf/prog_tests/bpf_cookie.c | 73 +++++++++++++++++++ .../selftests/bpf/progs/fprobe_bpf_cookie.c | 62 ++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/fprobe_bpf_cookie.ctools/testing/selftests/bpf/prog_tests/bpf_cookie.c +28
-0@@ -7,6 +7,7 @@#include <unistd.h> #include <test_progs.h> #include "test_bpf_cookie.skel.h" +#include "fprobe_bpf_cookie.skel.h" /* uprobe attach point */ static void trigger_func (void )@@ -63,6 +64,76 @@ static void kprobe_subtest(struct test_bpf_cookie *skel)bpf_link__destroy (retlink2); } +static void fprobe_subtest (void ) +{ +DECLARE_LIBBPF_OPTS (bpf_link_create_opts, opts); +int err, prog_fd, link1_fd =-1 , link2_fd =-1 ; +struct fprobe_bpf_cookie *skel =NULL ; +__u32 duration =0 , retval; +__u64 addrs[8 ], cookies[8 ]; + + skel =fprobe_bpf_cookie__open_and_load (); +if (!ASSERT_OK_PTR (skel,"fentry_raw_skel_load" )) + goto cleanup; + +kallsyms_find ("bpf_fentry_test1" , &addrs[0 ]); +kallsyms_find ("bpf_fentry_test2" , &addrs[1 ]); +kallsyms_find ("bpf_fentry_test3" , &addrs[2 ]); +kallsyms_find ("bpf_fentry_test4" , &addrs[3 ]); +kallsyms_find ("bpf_fentry_test5" , &addrs[4 ]); +kallsyms_find ("bpf_fentry_test6" , &addrs[5 ]); +kallsyms_find ("bpf_fentry_test7" , &addrs[6 ]); +kallsyms_find ("bpf_fentry_test8" , &addrs[7 ]); + + cookies[0 ] =1 ; + cookies[1 ] =2 ; + cookies[2 ] =3 ; + cookies[3 ] =4 ; + cookies[4 ] =5 ; + cookies[5 ] =6 ;
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c static void kprobe_subtest(struct test_bpf_cookie *skel) · line 97
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +4
-0+ cookies[6 ] =7 ; + cookies[7 ] =8 ; + + opts.fprobe.addrs = (__u64) &addrs;we should have
ptr_to_u64()fortest_progs, but if not, let's either add it or it should be (__u64)(uintptr_t)&addrs. Otherwise we'll be getting compilation warnings on some architectures.
there's one in btf.c, bpf.c and libbpf.c ;-) so I guess it could go to bpf.h
Quoted 45 lines
Quoted 45 lines
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +39
-0+ opts.fprobe.cnt =8 ; + opts.fprobe.bpf_cookies = (__u64) &cookies; + prog_fd =bpf_program__fd (skel->progs.test2); + + link1_fd =bpf_link_create (prog_fd,0 ,BPF_TRACE_FPROBE , &opts); +if (!ASSERT_GE (link1_fd,0 ,"link1_fd" )) +return ; + + cookies[0 ] =8 ; + cookies[1 ] =7 ; + cookies[2 ] =6 ; + cookies[3 ] =5 ; + cookies[4 ] =4 ; + cookies[5 ] =3 ; + cookies[6 ] =2 ; + cookies[7 ] =1 ; + + opts.flags =BPF_F_FPROBE_RETURN ; + prog_fd =bpf_program__fd (skel->progs.test3); + + link2_fd =bpf_link_create (prog_fd,0 ,BPF_TRACE_FPROBE , &opts); +if (!ASSERT_GE (link2_fd,0 ,"link2_fd" )) + 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" ); + +ASSERT_EQ (skel->bss->test2_result,8 ,"test2_result" ); +ASSERT_EQ (skel->bss->test3_result,8 ,"test3_result" ); + +cleanup: +close (link1_fd); +close (link2_fd); +fprobe_bpf_cookie__destroy (skel); +} +static void uprobe_subtest (struct test_bpf_cookie *skel) {DECLARE_LIBBPF_OPTS (bpf_uprobe_opts, opts);@@ -249,6 +320,8 @@ void test_bpf_cookie(void)
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c void test_bpf_cookie(void) · line 323
Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +1
-0if (test__start_subtest ("kprobe" ))kprobe_subtest (skel); +if (test__start_subtest ("rawkprobe" ))kprobe.multi?
yes
thanks,
jirka
Re: [PATCH 8/8] selftest/bpf: Add fprobe test for bpf_cookie values
Quoted 65 lines · Jiri Olsa
Quoted 54 lines · Andrii Nakryiko
Quoted 53 lines · Jiri Olsa
Adding
bpf_cookietest for kprobe attached by fprobe link.Signed-off-by Jiri Olsa <[email protected]>--- .../selftests/bpf/prog_tests/bpf_cookie.c | 73 +++++++++++++++++++ .../selftests/bpf/progs/fprobe_bpf_cookie.c | 62 ++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/fprobe_bpf_cookie.ctools/testing/selftests/bpf/prog_tests/bpf_cookie.c +28
-0@@ -7,6 +7,7 @@#include <unistd.h> #include <test_progs.h> #include "test_bpf_cookie.skel.h" +#include "fprobe_bpf_cookie.skel.h" /* uprobe attach point */ static void trigger_func (void )@@ -63,6 +64,76 @@ static void kprobe_subtest(struct test_bpf_cookie *skel)bpf_link__destroy (retlink2); } +static void fprobe_subtest (void ) +{ +DECLARE_LIBBPF_OPTS (bpf_link_create_opts, opts); +int err, prog_fd, link1_fd =-1 , link2_fd =-1 ; +struct fprobe_bpf_cookie *skel =NULL ; +__u32 duration =0 , retval; +__u64 addrs[8 ], cookies[8 ]; + + skel =fprobe_bpf_cookie__open_and_load (); +if (!ASSERT_OK_PTR (skel,"fentry_raw_skel_load" )) + goto cleanup; + +kallsyms_find ("bpf_fentry_test1" , &addrs[0 ]); +kallsyms_find ("bpf_fentry_test2" , &addrs[1 ]); +kallsyms_find ("bpf_fentry_test3" , &addrs[2 ]); +kallsyms_find ("bpf_fentry_test4" , &addrs[3 ]); +kallsyms_find ("bpf_fentry_test5" , &addrs[4 ]); +kallsyms_find ("bpf_fentry_test6" , &addrs[5 ]); +kallsyms_find ("bpf_fentry_test7" , &addrs[6 ]); +kallsyms_find ("bpf_fentry_test8" , &addrs[7 ]); + + cookies[0 ] =1 ; + cookies[1 ] =2 ; + cookies[2 ] =3 ; + cookies[3 ] =4 ; + cookies[4 ] =5 ; + cookies[5 ] =6 ;Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c static void kprobe_subtest(struct test_bpf_cookie *skel) · line 97
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +4
-0+ cookies[6 ] =7 ; + cookies[7 ] =8 ; + + opts.fprobe.addrs = (__u64) &addrs;we should have
ptr_to_u64()fortest_progs, but if not, let's either add it or it should be (__u64)(uintptr_t)&addrs. Otherwise we'll be getting compilation warnings on some architectures.
Quoted 1 line
there's one in
btf.c,bpf.candlibbpf.c;-) so I guess it could go tobpf.h
No, it shouldn't, bpf.h is a public API header. Let's keep internal helpers internal. Just copy/paste.
Quoted 57 lines
Quoted 46 lines
Quoted 46 lines
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +39
-0+ opts.fprobe.cnt =8 ; + opts.fprobe.bpf_cookies = (__u64) &cookies; + prog_fd =bpf_program__fd (skel->progs.test2); + + link1_fd =bpf_link_create (prog_fd,0 ,BPF_TRACE_FPROBE , &opts); +if (!ASSERT_GE (link1_fd,0 ,"link1_fd" )) +return ; + + cookies[0 ] =8 ; + cookies[1 ] =7 ; + cookies[2 ] =6 ; + cookies[3 ] =5 ; + cookies[4 ] =4 ; + cookies[5 ] =3 ; + cookies[6 ] =2 ; + cookies[7 ] =1 ; + + opts.flags =BPF_F_FPROBE_RETURN ; + prog_fd =bpf_program__fd (skel->progs.test3); + + link2_fd =bpf_link_create (prog_fd,0 ,BPF_TRACE_FPROBE , &opts); +if (!ASSERT_GE (link2_fd,0 ,"link2_fd" )) + 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" ); + +ASSERT_EQ (skel->bss->test2_result,8 ,"test2_result" ); +ASSERT_EQ (skel->bss->test3_result,8 ,"test3_result" ); + +cleanup: +close (link1_fd); +close (link2_fd); +fprobe_bpf_cookie__destroy (skel); +} +static void uprobe_subtest (struct test_bpf_cookie *skel) {DECLARE_LIBBPF_OPTS (bpf_uprobe_opts, opts);@@ -249,6 +320,8 @@ void test_bpf_cookie(void)Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c void test_bpf_cookie(void) · line 323
Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c +1
-0if (test__start_subtest ("kprobe" ))kprobe_subtest (skel); +if (test__start_subtest ("rawkprobe" ))kprobe.multi?
yes
thanks,
jirka
Re: [PATCH 3/8] bpf: Add bpf_cookie support to fprobe
In reply to Jiri OlsaQuoted 93 lines · Jiri Olsa
Quoted 36 lines · Andrii Nakryiko
Quoted 35 lines · Jiri Olsa
Adding support to call
bpf_get_attach_cookiehelper from kprobe program attached by fprobe link.The
bpf_cookieis provided by array of u64 values, where each value is paired with provided function address with the same array index.Suggested-by Andrii Nakryiko <[email protected]>Signed-off-by Jiri Olsa <[email protected]>--- include/linux/bpf.h | 2 + include/uapi/linux/bpf.h | 1 + kernel/bpf/syscall.c | 83 +++++++++++++++++++++++++++++++++- kernel/trace/bpf_trace.c | 16 ++++++- tools/include/uapi/linux/bpf.h | 1 + 5 files changed, 100 insertions(+), 3 deletions(-)include/linux/bpf.h +2
-0@@ -1301,6 +1301,8 @@ static inline void bpf_reset_run_ctx(struct bpf_run_ctx *old_ctx)#endif } +u64 bpf_fprobe_cookie (struct bpf_run_ctx *ctx,u64 ip); +/* BPF program asks to bypass CAP_NET_BIND_SERVICE in bind. */ #define BPF_RET_BIND_NO_CAP_NET_BIND_SERVICE (1 << 0)/* BPF program asks to set CN on the packet. */ include/uapi/linux/bpf.h +0
-0Quoted 8 lines
include/uapi/linux/bpf.h union bpf_attr { · line 1487
Quoted 5 lines
include/uapi/linux/bpf.h +1
-0@@ -1484,6 +1484,7 @@ union bpf_attr {__aligned_u64 addrs;__u32 cnt;__u32 flags; +__aligned_u64 bpf_cookies;maybe put it right after addrs, they are closely related and cnt describes all of syms/addrs/cookies.
ok
Quoted 39 lines
Quoted 22 lines
include/uapi/linux/bpf.h +0
-0} fprobe; }; } link_create;kernel/bpf/syscall.c +3
-0@@ -33,6 +33,8 @@#include <linux/rcupdate_trace.h> #include <linux/memcontrol.h> #include <linux/fprobe.h> +#include <linux/bsearch.h> +#include <linux/sort.h> #define IS_FD_ARRAY (map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \ (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \@@ -3025,10 +3027,18 @@ static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *pro#ifdef CONFIG_FPROBE +struct bpf_fprobe_cookie {kernel/bpf/syscall.c static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *pro · line 3040
Quoted 10 lines
kernel/bpf/syscall.c +6
-0+unsigned long addr; +u64 bpf_cookie; +}; +struct bpf_fprobe_link {struct bpf_link link;struct fprobe fp;unsigned long *addrs; +struct bpf_run_ctx run_ctx; +struct bpf_fprobe_cookie *bpf_cookies;you already have all the addrs above, why keeping a second copy of each addrs in
bpf_fprobe_cookie. Let's have two arrays: addrs (unsigned long) and cookies (u64) and make sure that they are sorted together. Then lookup addrs, calculate index, use that index to fetch cookie.Quoted 4 lines
Seems like
sort_r()provides exactly the interface you'd need to do this very easily. Having addrs separate from cookies also a bit advantageous in terms of TLB misses (if you need any more persuasion ;)
Quoted 7 lines
no persuation needed, I actually tried that but it turned out
sort_ris not ready yet ;-)because you can't pass priv pointer to the swap callback, so we can't swap the other array.. I did a change to allow that, but it's not trivial and will need some bigger testing/review because the original sort calls
sort_r, and of course there are many 'sort' users ;-)
Big sigh... :( Did you do something similar to _CMP_WRAPPER? You don't need to change the interface of sort(), so it shouldn't require extensive code refactoring. You'll just need to adjust priv to be not just cmp_func, but cmp_func + swap_fun (need a small struct on the stack in sort, probably). Or you did something else?
Quoted 114 lines
Quoted 31 lines
Quoted 31 lines
kernel/bpf/syscall.c +15
-0+u32 cnt; };static void bpf_fprobe_link_release (struct bpf_link *link)@@ -3045,6 +3055,7 @@ static void bpf_fprobe_link_dealloc(struct bpf_link *link) fprobe_link = container_of(link, struct bpf_fprobe_link, link);kfree (fprobe_link->addrs); +kfree (fprobe_link->bpf_cookies);kfree (fprobe_link); }@@ -3053,9 +3064,37 @@ static const struct bpf_link_ops bpf_fprobe_link_lops = { .dealloc = bpf_fprobe_link_dealloc, }; +static int bpf_fprobe_cookie_cmp (const void *_a,const void *_b) +{ +const struct bpf_fprobe_cookie *a = _a; +const struct bpf_fprobe_cookie *b = _b; + +if (a->addr == b->addr) +return 0 ; +return a->addr < b->addr ?-1 :1 ; +} + +u64 bpf_fprobe_cookie (struct bpf_run_ctx *ctx,u64 ip) +{ +struct bpf_fprobe_link *fprobe_link;Quoted 8 lines
kernel/bpf/syscall.c static const struct bpf_link_ops bpf_fprobe_link_lops = { · line 3085
Quoted 6 lines
kernel/bpf/syscall.c +6
-0+struct bpf_fprobe_cookie *val, key = { + .addr = (unsigned long ) ip, + }; + +if (!ctx) +return 0 ;is it allowed to have ctx == NULL?
nope, I was also thinking this is more 'WARN_ON[_ONCE]' check
Quoted 51 lines
Quoted 51 lines
kernel/bpf/syscall.c +16
-1+ fprobe_link = container_of(ctx, struct bpf_fprobe_link, run_ctx); +if (!fprobe_link->bpf_cookies) +return 0 ; + val =bsearch (&key, fprobe_link->bpf_cookies, fprobe_link->cnt, +sizeof (key), bpf_fprobe_cookie_cmp); +return val ? val->bpf_cookie :0 ; +} +static int fprobe_link_prog_run (struct bpf_fprobe_link *fprobe_link,struct pt_regs *regs) { +struct bpf_run_ctx *old_run_ctx;int err;if (unlikely (__this_cpu_inc_return (bpf_prog_active) !=1 )) {@@ -3063,12 +3102,16 @@ static int fprobe_link_prog_run(struct bpf_fprobe_link *fprobe_link, goto out; } + old_run_ctx =bpf_set_run_ctx (&fprobe_link->run_ctx); +rcu_read_lock ();migrate_disable (); err =bpf_prog_run (fprobe_link->link.prog, regs);migrate_enable ();rcu_read_unlock (); +bpf_reset_run_ctx (old_run_ctx); + out:__this_cpu_dec (bpf_prog_active);return err;@@ -3161,10 +3204,12 @@ static int fprobe_resolve_syms(const void *usyms, u32 cnt,static int bpf_fprobe_link_attach (const union bpf_attr *attr,struct bpf_prog *prog) { +struct bpf_fprobe_cookie *bpf_cookies =NULL ;struct bpf_fprobe_link *link =NULL ;struct bpf_link_primer link_primer; +void __user *ubpf_cookies; +u32 flags, cnt, i, size;unsigned long *addrs; -u32 flags, cnt, size;void __user *uaddrs;void __user *usyms;int err;@@ -3205,6 +3250,37 @@ static int bpf_fprobe_link_attach(const union bpf_attr *attr, struct bpf_prog *p goto error; }Quoted 4 lines
kernel/bpf/syscall.c static int bpf_fprobe_link_attach(const union bpf_attr *attr, struct bpf_prog *p · line 3253
Quoted 1 line
kernel/bpf/syscall.c +1
-0+ ubpf_cookies =u64_to_user_ptr (attr->link_create.fprobe.bpf_cookies);nit: let's call all this "cookies", this
bpf_prefix feels a bit redundant (I know about perf_event.bpf_cookie, but still).ok
Quoted 8 lines
kernel/bpf/syscall.c static int bpf_fprobe_link_attach(const union bpf_attr *attr, struct bpf_prog *p · line 3258
Quoted 6 lines
kernel/bpf/syscall.c +5
-0+if (ubpf_cookies) { +u64 *tmp; + + err = -ENOMEM ; + tmp =kzalloc (size,GFP_KERNEL );kvmalloc?
ok
thanks,
jirka
Re: [PATCH 3/8] bpf: Add bpf_cookie support to fprobe
Quoted 102 lines · Andrii Nakryiko
Quoted 93 lines · Jiri Olsa
Quoted 36 lines · Andrii Nakryiko
Quoted 35 lines · Jiri Olsa
Adding support to call
bpf_get_attach_cookiehelper from kprobe program attached by fprobe link.The
bpf_cookieis provided by array of u64 values, where each value is paired with provided function address with the same array index.Suggested-by Andrii Nakryiko <[email protected]>Signed-off-by Jiri Olsa <[email protected]>--- include/linux/bpf.h | 2 + include/uapi/linux/bpf.h | 1 + kernel/bpf/syscall.c | 83 +++++++++++++++++++++++++++++++++- kernel/trace/bpf_trace.c | 16 ++++++- tools/include/uapi/linux/bpf.h | 1 + 5 files changed, 100 insertions(+), 3 deletions(-)include/linux/bpf.h +2
-0@@ -1301,6 +1301,8 @@ static inline void bpf_reset_run_ctx(struct bpf_run_ctx *old_ctx)#endif } +u64 bpf_fprobe_cookie (struct bpf_run_ctx *ctx,u64 ip); +/* BPF program asks to bypass CAP_NET_BIND_SERVICE in bind. */ #define BPF_RET_BIND_NO_CAP_NET_BIND_SERVICE (1 << 0)/* BPF program asks to set CN on the packet. */ include/uapi/linux/bpf.h +0
-0Quoted 8 lines
include/uapi/linux/bpf.h union bpf_attr { · line 1487
Quoted 5 lines
include/uapi/linux/bpf.h +1
-0@@ -1484,6 +1484,7 @@ union bpf_attr {__aligned_u64 addrs;__u32 cnt;__u32 flags; +__aligned_u64 bpf_cookies;maybe put it right after addrs, they are closely related and cnt describes all of syms/addrs/cookies.
ok
Quoted 39 lines
Quoted 22 lines
include/uapi/linux/bpf.h +0
-0} fprobe; }; } link_create;kernel/bpf/syscall.c +3
-0@@ -33,6 +33,8 @@#include <linux/rcupdate_trace.h> #include <linux/memcontrol.h> #include <linux/fprobe.h> +#include <linux/bsearch.h> +#include <linux/sort.h> #define IS_FD_ARRAY (map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \ (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \@@ -3025,10 +3027,18 @@ static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *pro#ifdef CONFIG_FPROBE +struct bpf_fprobe_cookie {kernel/bpf/syscall.c static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *pro · line 3040
Quoted 10 lines
kernel/bpf/syscall.c +6
-0+unsigned long addr; +u64 bpf_cookie; +}; +struct bpf_fprobe_link {struct bpf_link link;struct fprobe fp;unsigned long *addrs; +struct bpf_run_ctx run_ctx; +struct bpf_fprobe_cookie *bpf_cookies;you already have all the addrs above, why keeping a second copy of each addrs in
bpf_fprobe_cookie. Let's have two arrays: addrs (unsigned long) and cookies (u64) and make sure that they are sorted together. Then lookup addrs, calculate index, use that index to fetch cookie.Quoted 4 lines
Seems like
sort_r()provides exactly the interface you'd need to do this very easily. Having addrs separate from cookies also a bit advantageous in terms of TLB misses (if you need any more persuasion ;)Quoted 7 lines
no persuation needed, I actually tried that but it turned out
sort_ris not ready yet ;-)because you can't pass priv pointer to the swap callback, so we can't swap the other array.. I did a change to allow that, but it's not trivial and will need some bigger testing/review because the original sort calls
sort_r, and of course there are many 'sort' users ;-)
Quoted 5 lines
Big sigh... :( Did you do something similar to
_CMP_WRAPPER? You don't need to change the interface ofsort(), so it shouldn't require extensive code refactoring. You'll just need to adjust priv to be not justcmp_func, butcmp_func+swap_fun(need a small struct on the stack in sort, probably). Or you did something else?
I ended up with change below
jirka
--- include/linux/sort.h | 2 +- include/linux/types.h | 1 + lib/sort.c | 44 +++++++++++++++++++++++++++++++++---------- 3 files changed, 36 insertions(+), 11 deletions(-)
include/linux/sort.h +1 -1
@@ -6,7 +6,7 @@void sort_r (void *base,size_t num,size_t size,cmp_r_func_t cmp_func, -swap_func_t swap_func, +swap_r_func_t swap_func,const void *priv); void sort(void *base, size_t num, size_t size,
include/linux/types.h +1 -0
@@ -226,6 +226,7 @@ struct callback_head {typedef void (*rcu_callback_t )(struct rcu_head *head);typedef void (*call_rcu_func_t )(struct rcu_head *head,rcu_callback_t func); +typedef void (*swap_r_func_t )(void *a,void *b,int size,const void *priv);typedef void (*swap_func_t )(void *a,void *b,int size);typedef int (*cmp_r_func_t )(const void *a,const void *b,const void *priv);
lib/sort.c +34 -10
101 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH 3/8] bpf: Add bpf_cookie support to fprobe
Quoted 104 lines · Jiri Olsa
Quoted 102 lines · Andrii Nakryiko
Quoted 100 lines · Jiri Olsa
Quoted 36 lines · Andrii Nakryiko
Quoted 35 lines · Jiri Olsa
Adding support to call
bpf_get_attach_cookiehelper from kprobe program attached by fprobe link.The
bpf_cookieis provided by array of u64 values, where each value is paired with provided function address with the same array index.Suggested-by Andrii Nakryiko <[email protected]>Signed-off-by Jiri Olsa <[email protected]>--- include/linux/bpf.h | 2 + include/uapi/linux/bpf.h | 1 + kernel/bpf/syscall.c | 83 +++++++++++++++++++++++++++++++++- kernel/trace/bpf_trace.c | 16 ++++++- tools/include/uapi/linux/bpf.h | 1 + 5 files changed, 100 insertions(+), 3 deletions(-)include/linux/bpf.h +2
-0@@ -1301,6 +1301,8 @@ static inline void bpf_reset_run_ctx(struct bpf_run_ctx *old_ctx)#endif } +u64 bpf_fprobe_cookie (struct bpf_run_ctx *ctx,u64 ip); +/* BPF program asks to bypass CAP_NET_BIND_SERVICE in bind. */ #define BPF_RET_BIND_NO_CAP_NET_BIND_SERVICE (1 << 0)/* BPF program asks to set CN on the packet. */ include/uapi/linux/bpf.h +0
-0Quoted 8 lines
include/uapi/linux/bpf.h union bpf_attr { · line 1487
Quoted 5 lines
include/uapi/linux/bpf.h +1
-0@@ -1484,6 +1484,7 @@ union bpf_attr {__aligned_u64 addrs;__u32 cnt;__u32 flags; +__aligned_u64 bpf_cookies;maybe put it right after addrs, they are closely related and cnt describes all of syms/addrs/cookies.
ok
Quoted 39 lines
Quoted 22 lines
include/uapi/linux/bpf.h +0
-0} fprobe; }; } link_create;kernel/bpf/syscall.c +3
-0@@ -33,6 +33,8 @@#include <linux/rcupdate_trace.h> #include <linux/memcontrol.h> #include <linux/fprobe.h> +#include <linux/bsearch.h> +#include <linux/sort.h> #define IS_FD_ARRAY (map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \ (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \@@ -3025,10 +3027,18 @@ static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *pro#ifdef CONFIG_FPROBE +struct bpf_fprobe_cookie {kernel/bpf/syscall.c static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *pro · line 3040
Quoted 10 lines
kernel/bpf/syscall.c +6
-0+unsigned long addr; +u64 bpf_cookie; +}; +struct bpf_fprobe_link {struct bpf_link link;struct fprobe fp;unsigned long *addrs; +struct bpf_run_ctx run_ctx; +struct bpf_fprobe_cookie *bpf_cookies;you already have all the addrs above, why keeping a second copy of each addrs in
bpf_fprobe_cookie. Let's have two arrays: addrs (unsigned long) and cookies (u64) and make sure that they are sorted together. Then lookup addrs, calculate index, use that index to fetch cookie.Quoted 4 lines
Seems like
sort_r()provides exactly the interface you'd need to do this very easily. Having addrs separate from cookies also a bit advantageous in terms of TLB misses (if you need any more persuasion ;)no persuation needed, I actually tried that but it turned out
sort_ris not ready yet ;-)because you can't pass priv pointer to the swap callback, so we can't swap the other array.. I did a change to allow that, but it's not trivial and will need some bigger testing/review because the original sort calls
sort_r, and of course there are many 'sort' users ;-)
Quoted 8 lines
Quoted 5 lines
Big sigh... :( Did you do something similar to
_CMP_WRAPPER? You don't need to change the interface ofsort(), so it shouldn't require extensive code refactoring. You'll just need to adjust priv to be not justcmp_func, butcmp_func+swap_fun(need a small struct on the stack in sort, probably). Or you did something else?I ended up with change below
exactly what I had in mind
Quoted 53 lines
jirka
--- include/linux/sort.h | 2 +- include/linux/types.h | 1 + lib/sort.c | 44 +++++++++++++++++++++++++++++++++---------- 3 files changed, 36 insertions(+), 11 deletions(-)include/linux/sort.h +1
-1@@ -6,7 +6,7 @@void sort_r (void *base,size_t num,size_t size,cmp_r_func_t cmp_func, -swap_func_t swap_func, +swap_r_func_t swap_func,const void *priv); void sort(void *base, size_t num, size_t size,include/linux/types.h +1
-0@@ -226,6 +226,7 @@ struct callback_head {typedef void (*rcu_callback_t )(struct rcu_head *head);typedef void (*call_rcu_func_t )(struct rcu_head *head,rcu_callback_t func); +typedef void (*swap_r_func_t )(void *a,void *b,int size,const void *priv);typedef void (*swap_func_t )(void *a,void *b,int size);typedef int (*cmp_r_func_t )(const void *a,const void *b,const void *priv);lib/sort.c +8
-3@@ -122,16 +122,29 @@ static void swap_bytes(void *a, void *b, size_t n)* a pointer, but small integers make for the smallest compare * instructions. */ -#define SWAP_WORDS_64 (swap_func_t)0 -#define SWAP_WORDS_32 (swap_func_t)1 -#define SWAP_BYTES (swap_func_t)2 +#define SWAP_WORDS_64 (swap_r_func_t)0 +#define SWAP_WORDS_32 (swap_r_func_t)1 +#define SWAP_BYTES (swap_r_func_t)2 +#define SWAP_WRAPPER (swap_r_func_t)3 + +struct wrapper { +cmp_func_t cmp; +swap_func_t swap;
lib/sort.c static void swap_bytes(void *a, void *b, size_t n) · line 141
Quoted 10 lines
lib/sort.c +3
-1+};/* * The function pointer is last to make tail calls most efficient if the * compiler decides not to inline this function. */ -static void do_swap (void *a,void *b,size_t size,swap_func_t swap_func) +static void do_swap (void *a,void *b,size_t size,swap_r_func_t swap_func,const void *priv) { +const struct wrapper *w = priv;
I'd just move this under if
lib/sort.c static void swap_bytes(void *a, void *b, size_t n) · line 143
Quoted 2 lines
lib/sort.c +2
-0+ +if (swap_func ==SWAP_WRAPPER ) {
const struct wrapper *w = priv; here
Quoted 14 lines
lib/sort.c +5
-1+ w->swap (a, b, (int )size); +return ; + } +if (swap_func ==SWAP_WORDS_64 )swap_words_64 (a, b, size);else if (swap_func ==SWAP_WORDS_32 )@@ -139,15 +152,17 @@ static void do_swap(void *a, void *b, size_t size, swap_func_t swap_func)else if (swap_func ==SWAP_BYTES )swap_bytes (a, b, size);else -swap_func (a, b, (int )size); +swap_func (a, b, (int )size, priv); }
lib/sort.c static void do_swap(void *a, void *b, size_t size, swap_func_t swap_func) · line 165
Quoted 10 lines
lib/sort.c +3
-1#define _CMP_WRAPPER ((cmp_r_func_t)0L)static int do_cmp (const void *a,const void *b,cmp_r_func_t cmp,const void *priv) { +const struct wrapper *w = priv; +if (cmp == _CMP_WRAPPER) -return ((cmp_func_t)(priv))(a, b); +return w->cmp (a, b);
same here, or just stick to the previous style with
return ((const struct wrapper *)priv)->cmd(a, b);
Quoted 10 lines
lib/sort.c +1
-1return cmp (a, b, priv); }@@ -198,16 +213,20 @@ static size_t parent(size_t i, unsigned int lsbit, size_t size)*/ void sort_r (void *base,size_t num,size_t size,cmp_r_func_t cmp_func, -swap_func_t swap_func, +swap_r_func_t swap_func,const void *priv)
lib/sort.c static size_t parent(size_t i, unsigned int lsbit, size_t size) · line 227
Quoted 10 lines
lib/sort.c +2
-0{/* pre-scale counters for performance */ size_t n = num * size, a = (num/2 ) * size;const unsigned int lsbit = size & -size;/* Used to find parent */ +const struct wrapper *w = priv;if (!a)/* num < 2 || size == 0 */ return ; +if (swap_func ==SWAP_WRAPPER && !w->swap)
same here, I'd probably do the cast right here to keep this wrapper stuff as local as possible
Quoted 39 lines
lib/sort.c +10
-4+ swap_func =NULL ; +if (!swap_func) {if (is_aligned (base, size,8 )) swap_func =SWAP_WORDS_64 ;@@ -230,7 +249,7 @@ void sort_r(void *base, size_t num, size_t size,if (a)/* Building heap: sift down --a */ a -= size;else if (n -= size)/* Sorting: Extract root to --n */ -do_swap (base, base + n, size, swap_func); +do_swap (base, base + n, size, swap_func, priv);else /* Sort complete */ break ;@@ -257,7 +276,7 @@ void sort_r(void *base, size_t num, size_t size, c = b;/* Where "a" belongs */ while (b != a) {/* Shift it into place */ b =parent (b, lsbit, size); -do_swap (base + b, base + c, size, swap_func); +do_swap (base + b, base + c, size, swap_func, priv); } } }@@ -267,6 +286,11 @@ void sort(void *base, size_t num, size_t size, cmp_func_t cmp_func, swap_func_t swap_func) { - return sort_r(base, num, size, _CMP_WRAPPER, swap_func, cmp_func); +struct wrapper w = { + .cmp = cmp_func, + .swap = swap_func, + }; + +return sort_r (base, num, size, _CMP_WRAPPER,SWAP_WRAPPER , &w); }EXPORT_SYMBOL (sort); --2.34.1
Re: [PATCH 2/8] bpf: Add bpf_get_func_ip kprobe helper for fprobe link
In reply to Andrii NakryikoQuoted 31 lines · Andrii Nakryiko
Quoted 30 lines · Jiri Olsa
Adding support to call
get_func_ip_fprobehelper from kprobe programs attached by fprobe link.Also adding support to inline it, because it's single load instruction.
Signed-off-by Jiri Olsa <[email protected]>--- kernel/bpf/verifier.c | 19 ++++++++++++++++++- kernel/trace/bpf_trace.c | 16 +++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-)kernel/bpf/verifier.c +1
-1@@ -13625,7 +13625,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 */ @@ -13640,6 +13640,23 @@ static int do_misc_fixups(struct bpf_verifier_env *env)continue ; }
Quoted 9 lines
kernel/bpf/verifier.c static int do_misc_fixups(struct bpf_verifier_env *env) · line 13649
Quoted 7 lines
kernel/bpf/verifier.c +7
-0+/* Implement kprobe/fprobe bpf_get_func_ip inline. */ +if (prog_type ==BPF_PROG_TYPE_KPROBE && + eatype ==BPF_TRACE_FPROBE && + 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));Isn't this architecture-specific? I'm starting to dislike this
ugh, it is.. I'm not sure we want #ifdef CONFIG_X86 in here, or some arch_* specific function?
jirka
Quoted 57 lines
inlining whole more and more. It's just a complication in verifier without clear real-world benefits. We are clearly prematurely optimizing here. In practice you'll just call
bpf_get_func_ip()once and that's it. Function call overhead will be negligible compare to other *userful* work you'll be doing in your BPF program.Quoted 50 lines
kernel/bpf/verifier.c +10
-0+ + 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 ; + } + patch_call_imm: fn = env->ops->get_func_proto (insn->imm, env->prog);/* all functions that have prototype and verifier allowed kernel/trace/bpf_trace.c +15
-2@@ -1036,6 +1036,19 @@ static const struct bpf_func_proto bpf_get_func_ip_proto_kprobe = { .arg1_type = ARG_PTR_TO_CTX, }; +BPF_CALL_1(bpf_get_func_ip_fprobe, struct pt_regs *, regs) +{ +/* This helper call is inlined by verifier. */ +return regs->ip; +} + +static const struct bpf_func_proto bpf_get_func_ip_proto_fprobe = { + .func = bpf_get_func_ip_fprobe, + .gpl_only = false, + .ret_type =RET_INTEGER , + .arg1_type =ARG_PTR_TO_CTX , +}; + BPF_CALL_1(bpf_get_attach_cookie_trace, void *, ctx) {struct bpf_trace_run_ctx *run_ctx;@@ -1279,7 +1292,8 @@ kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)return &bpf_override_return_proto;#endif case BPF_FUNC_get_func_ip: -return &bpf_get_func_ip_proto_kprobe; +return prog->expected_attach_type ==BPF_TRACE_FPROBE ? + &bpf_get_func_ip_proto_fprobe : &bpf_get_func_ip_proto_kprobe;case BPF_FUNC_get_attach_cookie:return &bpf_get_attach_cookie_proto_trace;default : --2.34.1
Re: [PATCH 2/8] bpf: Add bpf_get_func_ip kprobe helper for fprobe link
Quoted 43 lines · Jiri Olsa
Quoted 31 lines · Andrii Nakryiko
Quoted 30 lines · Jiri Olsa
Adding support to call
get_func_ip_fprobehelper from kprobe programs attached by fprobe link.Also adding support to inline it, because it's single load instruction.
Signed-off-by Jiri Olsa <[email protected]>--- kernel/bpf/verifier.c | 19 ++++++++++++++++++- kernel/trace/bpf_trace.c | 16 +++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-)kernel/bpf/verifier.c +1
-1@@ -13625,7 +13625,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 */ @@ -13640,6 +13640,23 @@ static int do_misc_fixups(struct bpf_verifier_env *env)continue ; }Quoted 9 lines
kernel/bpf/verifier.c static int do_misc_fixups(struct bpf_verifier_env *env) · line 13649
Quoted 7 lines
kernel/bpf/verifier.c +7
-0+/* Implement kprobe/fprobe bpf_get_func_ip inline. */ +if (prog_type ==BPF_PROG_TYPE_KPROBE && + eatype ==BPF_TRACE_FPROBE && + 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));Isn't this architecture-specific? I'm starting to dislike this
Quoted 2 lines
ugh, it is.. I'm not sure we want #ifdef
CONFIG_X86in here, or some arch_* specific function?
So not inlining it isn't even considered? this function will be called once or at most a few times per BPF program invocation. Anyone calling it in a tight loop is going to use it very-very suboptimally (and even then useful program logic will dominate). There is no point in inlining it.
Quoted 60 lines
jirka
Quoted 57 lines
inlining whole more and more. It's just a complication in verifier without clear real-world benefits. We are clearly prematurely optimizing here. In practice you'll just call
bpf_get_func_ip()once and that's it. Function call overhead will be negligible compare to other *userful* work you'll be doing in your BPF program.Quoted 50 lines
kernel/bpf/verifier.c +10
-0+ + 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 ; + } + patch_call_imm: fn = env->ops->get_func_proto (insn->imm, env->prog);/* all functions that have prototype and verifier allowed kernel/trace/bpf_trace.c +15
-2@@ -1036,6 +1036,19 @@ static const struct bpf_func_proto bpf_get_func_ip_proto_kprobe = { .arg1_type = ARG_PTR_TO_CTX, }; +BPF_CALL_1(bpf_get_func_ip_fprobe, struct pt_regs *, regs) +{ +/* This helper call is inlined by verifier. */ +return regs->ip; +} + +static const struct bpf_func_proto bpf_get_func_ip_proto_fprobe = { + .func = bpf_get_func_ip_fprobe, + .gpl_only = false, + .ret_type =RET_INTEGER , + .arg1_type =ARG_PTR_TO_CTX , +}; + BPF_CALL_1(bpf_get_attach_cookie_trace, void *, ctx) {struct bpf_trace_run_ctx *run_ctx;@@ -1279,7 +1292,8 @@ kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)return &bpf_override_return_proto;#endif case BPF_FUNC_get_func_ip: -return &bpf_get_func_ip_proto_kprobe; +return prog->expected_attach_type ==BPF_TRACE_FPROBE ? + &bpf_get_func_ip_proto_fprobe : &bpf_get_func_ip_proto_kprobe;case BPF_FUNC_get_attach_cookie:return &bpf_get_attach_cookie_proto_trace;default : --2.34.1
Re: [PATCH 2/8] bpf: Add bpf_get_func_ip kprobe helper for fprobe link
Quoted 48 lines · Andrii Nakryiko
Quoted 43 lines · Jiri Olsa
Quoted 31 lines · Andrii Nakryiko
Quoted 30 lines · Jiri Olsa
Adding support to call
get_func_ip_fprobehelper from kprobe programs attached by fprobe link.Also adding support to inline it, because it's single load instruction.
Signed-off-by Jiri Olsa <[email protected]>--- kernel/bpf/verifier.c | 19 ++++++++++++++++++- kernel/trace/bpf_trace.c | 16 +++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-)kernel/bpf/verifier.c +1
-1@@ -13625,7 +13625,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 */ @@ -13640,6 +13640,23 @@ static int do_misc_fixups(struct bpf_verifier_env *env)continue ; }Quoted 9 lines
kernel/bpf/verifier.c static int do_misc_fixups(struct bpf_verifier_env *env) · line 13649
Quoted 7 lines
kernel/bpf/verifier.c +7
-0+/* Implement kprobe/fprobe bpf_get_func_ip inline. */ +if (prog_type ==BPF_PROG_TYPE_KPROBE && + eatype ==BPF_TRACE_FPROBE && + 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));Isn't this architecture-specific? I'm starting to dislike this
Quoted 2 lines
ugh, it is.. I'm not sure we want #ifdef
CONFIG_X86in here, or some arch_* specific function?
Quoted 5 lines
So not inlining it isn't even considered? this function will be called once or at most a few times per BPF program invocation. Anyone calling it in a tight loop is going to use it very-very suboptimally (and even then useful program logic will dominate). There is no point in inlining it.
I agree that given its usage pattern there won't be too much gain, on the other hand it's simple verifier code changing call/load/ret into simple load, so I thought why not.. also there are just few helpers we can inline so easily
but yea.. I can't think of any sane usage of this helper that inlining would matter for.. which doesn't mean there isn't one ;-)
jirka
Quoted 61 lines
Quoted 61 lines
jirka
Quoted 57 lines
inlining whole more and more. It's just a complication in verifier without clear real-world benefits. We are clearly prematurely optimizing here. In practice you'll just call
bpf_get_func_ip()once and that's it. Function call overhead will be negligible compare to other *userful* work you'll be doing in your BPF program.Quoted 50 lines
kernel/bpf/verifier.c +10
-0+ + 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 ; + } + patch_call_imm: fn = env->ops->get_func_proto (insn->imm, env->prog);/* all functions that have prototype and verifier allowed kernel/trace/bpf_trace.c +15
-2@@ -1036,6 +1036,19 @@ static const struct bpf_func_proto bpf_get_func_ip_proto_kprobe = { .arg1_type = ARG_PTR_TO_CTX, }; +BPF_CALL_1(bpf_get_func_ip_fprobe, struct pt_regs *, regs) +{ +/* This helper call is inlined by verifier. */ +return regs->ip; +} + +static const struct bpf_func_proto bpf_get_func_ip_proto_fprobe = { + .func = bpf_get_func_ip_fprobe, + .gpl_only = false, + .ret_type =RET_INTEGER , + .arg1_type =ARG_PTR_TO_CTX , +}; + BPF_CALL_1(bpf_get_attach_cookie_trace, void *, ctx) {struct bpf_trace_run_ctx *run_ctx;@@ -1279,7 +1292,8 @@ kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)return &bpf_override_return_proto;#endif case BPF_FUNC_get_func_ip: -return &bpf_get_func_ip_proto_kprobe; +return prog->expected_attach_type ==BPF_TRACE_FPROBE ? + &bpf_get_func_ip_proto_fprobe : &bpf_get_func_ip_proto_kprobe;case BPF_FUNC_get_attach_cookie:return &bpf_get_attach_cookie_proto_trace;default : --2.34.1
Quoted 53 lines · Masami Hiramatsu
Hi Alexei,
Quoted 28 lines · On Thu, 3 Feb 2022 18:42:22 -0800 Alexei Starovoitov…
Quoted 27 lines · Steven Rostedt
Quoted 10 lines · On Thu, 3 Feb 2022 18:12:11 -0800 Alexei Starovoitov…
Quoted 2 lines
No, fprobe is NOT kprobe on ftrace, kprobe on ftrace is already implemented transparently.
Not true.
fprobe is nothing but _explicit_ kprobe on ftrace. There was an implicit optimization for kprobe when ftrace could be used.
All this new interface is doing is making it explicit. So a new name is not warranted here.Quoted 4 lines
Quoted 1 line
from that viewpoint, fprobe and kprobe interface are similar but different.
What is the difference?
I don't see it.IIUC, a kprobe on a function (or ftrace, aka fprobe) gives some extra abilities that a normal kprobe does not. Namely, "what is the function parameters?"
You can only reliably get the parameters at function entry. Hence, by having a probe that is unique to functions as supposed to the middle of a function, makes sense to me.
Quoted 8 lines
Quoted 2 lines
That is, the API can change. "Give me parameter X". That along with some BTF reading, could figure out how to get parameter X, and record that.
This is more or less a description of kprobe on ftrace :) The bpf+kprobe users were relying on that for a long time. See
PT_REGS_PARM1()macros inbpf_tracing.hThey're meaningful only with kprobe on ftrace. So, no, fprobe is not inventing anything new here.Hmm, you may be misleading why
PT_REGS_PARAM1()macro works. You can use it even ifCONFIG_FUNCITON_TRACER=nif your kernel is built withCONFIG_KPROBES=y. It is valid unless you put a probe out of function entry.Quoted 5 lines
No one is using kprobe in the middle of the function. It's too difficult to make anything useful out of it, so no one bothers.
When people say "kprobe" 99 out of 100 they mean kprobe on ftrace/fentry.
Quoted 5 lines
I see. But the kprobe is kprobe. It is not designed to support multiple probe points. If I'm forced to say, I can rename the struct fprobe to struct
multi_kprobe, but that doesn't change the essence. You may need to use both of kprobes and so-calledmulti_kprobeproperly. (Someone need to do that.)
hi,
tying to kick things further ;-) I was thinking about bpf side of this and we could use following interface:
enum bpf_attach_type {
...
BPF_TRACE_KPROBE_MULTI
};
enum bpf_link_type {
...
BPF_LINK_TYPE_KPROBE_MULTI
};
union bpf_attr { struct {
...
struct {
__aligned_u64 syms;
__aligned_u64 addrs;
__aligned_u64 cookies;
__u32 cnt;
__u32 flags;
} kprobe_multi;
} link_create;}
because from bpf user POV it's new link for attaching multiple kprobes and I agree new 'fprobe' type name in here brings more confusion, using kprobe_multi is straightforward
thoguhts?
thanks,
jirka
Quoted 87 lines · Jiri Olsa
Quoted 53 lines · Masami Hiramatsu
Hi Alexei,
Quoted 28 lines · On Thu, 3 Feb 2022 18:42:22 -0800 Alexei Starovoitov…
Quoted 27 lines · Steven Rostedt
Quoted 10 lines · On Thu, 3 Feb 2022 18:12:11 -0800 Alexei Starovoitov…
Quoted 2 lines
No, fprobe is NOT kprobe on ftrace, kprobe on ftrace is already implemented transparently.
Not true.
fprobe is nothing but _explicit_ kprobe on ftrace. There was an implicit optimization for kprobe when ftrace could be used.
All this new interface is doing is making it explicit. So a new name is not warranted here.Quoted 4 lines
Quoted 1 line
from that viewpoint, fprobe and kprobe interface are similar but different.
What is the difference?
I don't see it.IIUC, a kprobe on a function (or ftrace, aka fprobe) gives some extra abilities that a normal kprobe does not. Namely, "what is the function parameters?"
You can only reliably get the parameters at function entry. Hence, by having a probe that is unique to functions as supposed to the middle of a function, makes sense to me.
Quoted 8 lines
Quoted 2 lines
That is, the API can change. "Give me parameter X". That along with some BTF reading, could figure out how to get parameter X, and record that.
This is more or less a description of kprobe on ftrace :) The bpf+kprobe users were relying on that for a long time. See
PT_REGS_PARM1()macros inbpf_tracing.hThey're meaningful only with kprobe on ftrace. So, no, fprobe is not inventing anything new here.Hmm, you may be misleading why
PT_REGS_PARAM1()macro works. You can use it even ifCONFIG_FUNCITON_TRACER=nif your kernel is built withCONFIG_KPROBES=y. It is valid unless you put a probe out of function entry.Quoted 5 lines
No one is using kprobe in the middle of the function. It's too difficult to make anything useful out of it, so no one bothers.
When people say "kprobe" 99 out of 100 they mean kprobe on ftrace/fentry.Quoted 5 lines
I see. But the kprobe is kprobe. It is not designed to support multiple probe points. If I'm forced to say, I can rename the struct fprobe to struct
multi_kprobe, but that doesn't change the essence. You may need to use both of kprobes and so-calledmulti_kprobeproperly. (Someone need to do that.)hi,
tying to kick things further ;-) I was thinking about bpf side of this and we could use following interface:enum bpf_attach_type { ... BPF_TRACE_KPROBE_MULTI }; enum bpf_link_type { ... BPF_LINK_TYPE_KPROBE_MULTI }; union bpf_attr {struct { ... struct { __aligned_u64 syms; __aligned_u64 addrs; __aligned_u64 cookies; __u32 cnt; __u32 flags; } kprobe_multi; } link_create;
Quoted 7 lines
}
because from bpf user POV it's new link for attaching multiple kprobes and I agree new 'fprobe' type name in here brings more confusion, using
kprobe_multiis straightforwardthoguhts?
I think this makes sense. We do need new type of link to store ip -> cookie mapping anyways.
Is there any chance to support this fast multi-attach for uprobe? If yes, we might want to reuse the same link for both (so should we name it more generically? on the other hand BPF program type for uprobe is BPF_PROG_TYPE_KPROBE anyway, so keeping it as "kprobe" also would be consistent with what we have today).
But yeah, the main question is whether there is something preventing us from supporting multi-attach uprobe as well? It would be really great for USDT use case.
Quoted 3 lines
thanks,
jirka
Masami Hiramatsu
maintainerQuoted 90 lines · On Wed, 16 Feb 2022 10:27:19 -0800 Andrii Nakryiko <[email protected]>…
Quoted 89 lines · Jiri Olsa
Quoted 53 lines · Masami Hiramatsu
Hi Alexei,
Quoted 28 lines · On Thu, 3 Feb 2022 18:42:22 -0800 Alexei Starovoitov…
Quoted 27 lines · Steven Rostedt
Quoted 10 lines · On Thu, 3 Feb 2022 18:12:11 -0800 Alexei Starovoitov…
Quoted 2 lines
No, fprobe is NOT kprobe on ftrace, kprobe on ftrace is already implemented transparently.
Not true.
fprobe is nothing but _explicit_ kprobe on ftrace. There was an implicit optimization for kprobe when ftrace could be used.
All this new interface is doing is making it explicit. So a new name is not warranted here.Quoted 4 lines
Quoted 1 line
from that viewpoint, fprobe and kprobe interface are similar but different.
What is the difference?
I don't see it.IIUC, a kprobe on a function (or ftrace, aka fprobe) gives some extra abilities that a normal kprobe does not. Namely, "what is the function parameters?"
You can only reliably get the parameters at function entry. Hence, by having a probe that is unique to functions as supposed to the middle of a function, makes sense to me.
Quoted 8 lines
Quoted 2 lines
That is, the API can change. "Give me parameter X". That along with some BTF reading, could figure out how to get parameter X, and record that.
This is more or less a description of kprobe on ftrace :) The bpf+kprobe users were relying on that for a long time. See
PT_REGS_PARM1()macros inbpf_tracing.hThey're meaningful only with kprobe on ftrace. So, no, fprobe is not inventing anything new here.Hmm, you may be misleading why
PT_REGS_PARAM1()macro works. You can use it even ifCONFIG_FUNCITON_TRACER=nif your kernel is built withCONFIG_KPROBES=y. It is valid unless you put a probe out of function entry.Quoted 5 lines
No one is using kprobe in the middle of the function. It's too difficult to make anything useful out of it, so no one bothers.
When people say "kprobe" 99 out of 100 they mean kprobe on ftrace/fentry.Quoted 5 lines
I see. But the kprobe is kprobe. It is not designed to support multiple probe points. If I'm forced to say, I can rename the struct fprobe to struct
multi_kprobe, but that doesn't change the essence. You may need to use both of kprobes and so-calledmulti_kprobeproperly. (Someone need to do that.)hi,
tying to kick things further ;-) I was thinking about bpf side of this and we could use following interface:enum bpf_attach_type { ... BPF_TRACE_KPROBE_MULTI }; enum bpf_link_type { ... BPF_LINK_TYPE_KPROBE_MULTI }; union bpf_attr {struct { ... struct { __aligned_u64 syms; __aligned_u64 addrs; __aligned_u64 cookies; __u32 cnt; __u32 flags; } kprobe_multi; } link_create;}
Quoted 8 lines
Quoted 5 lines
because from bpf user POV it's new link for attaching multiple kprobes and I agree new 'fprobe' type name in here brings more confusion, using
kprobe_multiis straightforwardthoguhts?
I think this makes sense. We do need new type of link to store ip -> cookie mapping anyways.
This looks good to me too.
Quoted 4 lines
Is there any chance to support this fast multi-attach for uprobe? If yes, we might want to reuse the same link for both (so should we name it more generically?
There is no interface to do that but also there is no limitation to expand uprobes. For the kprobes, there are some limitations for the function entry because it needs to share the space with ftrace. So I introduced fprobe for easier to use.
Quoted 3 lines
on the other hand BPF program type for uprobe is
BPF_PROG_TYPE_KPROBEanyway, so keeping it as "kprobe" also would be consistent with what we have today).
Hmm, I'm not sure why BPF made such design choice... (Uprobe needs the target program.)
Quoted 3 lines
But yeah, the main question is whether there is something preventing us from supporting multi-attach uprobe as well? It would be really great for USDT use case.
Ah, for the USDT, it will be useful. But since now we will have "user-event" which is faster than uprobes, we may be better to consider to use it.
I'm not so sure how uprobes probes the target process, but maybe it has to manage some memory pages and task related things. If we can split those task-related part from struct uprobe software-breakpoint part, it maybe easy to support multiple probe (one task-related part + multiple software-breakpoint parts.)
Thank you,
Signature · 1 line
Masami Hiramatsu <[email protected]>
Quoted 115 lines · Masami Hiramatsu
Quoted 90 lines · On Wed, 16 Feb 2022 10:27:19 -0800 Andrii Nakryiko <[email protected]>…
Quoted 89 lines · Jiri Olsa
Quoted 53 lines · Masami Hiramatsu
Hi Alexei,
Quoted 28 lines · On Thu, 3 Feb 2022 18:42:22 -0800 Alexei Starovoitov…
Quoted 27 lines · Steven Rostedt
Quoted 10 lines · On Thu, 3 Feb 2022 18:12:11 -0800 Alexei Starovoitov…
Quoted 2 lines
No, fprobe is NOT kprobe on ftrace, kprobe on ftrace is already implemented transparently.
Not true.
fprobe is nothing but _explicit_ kprobe on ftrace. There was an implicit optimization for kprobe when ftrace could be used.
All this new interface is doing is making it explicit. So a new name is not warranted here.Quoted 4 lines
Quoted 1 line
from that viewpoint, fprobe and kprobe interface are similar but different.
What is the difference?
I don't see it.IIUC, a kprobe on a function (or ftrace, aka fprobe) gives some extra abilities that a normal kprobe does not. Namely, "what is the function parameters?"
You can only reliably get the parameters at function entry. Hence, by having a probe that is unique to functions as supposed to the middle of a function, makes sense to me.
Quoted 8 lines
Quoted 2 lines
That is, the API can change. "Give me parameter X". That along with some BTF reading, could figure out how to get parameter X, and record that.
This is more or less a description of kprobe on ftrace :) The bpf+kprobe users were relying on that for a long time. See
PT_REGS_PARM1()macros inbpf_tracing.hThey're meaningful only with kprobe on ftrace. So, no, fprobe is not inventing anything new here.Hmm, you may be misleading why
PT_REGS_PARAM1()macro works. You can use it even ifCONFIG_FUNCITON_TRACER=nif your kernel is built withCONFIG_KPROBES=y. It is valid unless you put a probe out of function entry.Quoted 5 lines
No one is using kprobe in the middle of the function. It's too difficult to make anything useful out of it, so no one bothers.
When people say "kprobe" 99 out of 100 they mean kprobe on ftrace/fentry.Quoted 5 lines
I see. But the kprobe is kprobe. It is not designed to support multiple probe points. If I'm forced to say, I can rename the struct fprobe to struct
multi_kprobe, but that doesn't change the essence. You may need to use both of kprobes and so-calledmulti_kprobeproperly. (Someone need to do that.)hi,
tying to kick things further ;-) I was thinking about bpf side of this and we could use following interface:enum bpf_attach_type { ... BPF_TRACE_KPROBE_MULTI }; enum bpf_link_type { ... BPF_LINK_TYPE_KPROBE_MULTI }; union bpf_attr {struct { ... struct { __aligned_u64 syms; __aligned_u64 addrs; __aligned_u64 cookies; __u32 cnt; __u32 flags; } kprobe_multi; } link_create;}
Quoted 8 lines
Quoted 5 lines
because from bpf user POV it's new link for attaching multiple kprobes and I agree new 'fprobe' type name in here brings more confusion, using
kprobe_multiis straightforwardthoguhts?
I think this makes sense. We do need new type of link to store ip -> cookie mapping anyways.
This looks good to me too.
Quoted 4 lines
Is there any chance to support this fast multi-attach for uprobe? If yes, we might want to reuse the same link for both (so should we name it more generically?
There is no interface to do that but also there is no limitation to expand uprobes. For the kprobes, there are some limitations for the function entry because it needs to share the space with ftrace. So I introduced fprobe for easier to use.
Quoted 7 lines
Quoted 3 lines
on the other hand BPF program type for uprobe is
BPF_PROG_TYPE_KPROBEanyway, so keeping it as "kprobe" also would be consistent with what we have today).Hmm, I'm not sure why BPF made such design choice... (Uprobe needs the target program.)
We've been talking about sleepable uprobe programs, so we might need to add uprobe-specific program type, probably. But historically, from BPF point of view there was no difference between kprobe and uprobe programs (in terms of how they are run and what's available to them). From BPF point of view, it was just attaching BPF program to a perf_event.
Quoted 7 lines
Quoted 4 lines
But yeah, the main question is whether there is something preventing us from supporting multi-attach uprobe as well? It would be really great for USDT use case.
Ah, for the USDT, it will be useful. But since now we will have "user-event" which is faster than uprobes, we may be better to consider to use it.
Any pointers? I'm not sure what "user-event" refers to.
Quoted 11 lines
I'm not so sure how uprobes probes the target process, but maybe it has to manage some memory pages and task related things. If we can split those task-related part from struct uprobe software-breakpoint part, it maybe easy to support multiple probe (one task-related part + multiple software-breakpoint parts.)
Thank you,
--
Masami Hiramatsu <[email protected]>
Masami Hiramatsu
maintainerQuoted 17 lines · On Thu, 17 Feb 2022 14:01:30 -0800 Andrii Nakryiko <[email protected]>…
Quoted 9 lines
Quoted 3 lines
Is there any chance to support this fast multi-attach for uprobe? If yes, we might want to reuse the same link for both (so should we name it more generically?
There is no interface to do that but also there is no limitation to expand uprobes. For the kprobes, there are some limitations for the function entry because it needs to share the space with ftrace. So I introduced fprobe for easier to use.
Quoted 7 lines
Quoted 3 lines
on the other hand BPF program type for uprobe is
BPF_PROG_TYPE_KPROBEanyway, so keeping it as "kprobe" also would be consistent with what we have today).Hmm, I'm not sure why BPF made such design choice... (Uprobe needs the target program.)
Quoted 6 lines
We've been talking about sleepable uprobe programs, so we might need to add uprobe-specific program type, probably. But historically, from BPF point of view there was no difference between kprobe and uprobe programs (in terms of how they are run and what's available to them). From BPF point of view, it was just attaching BPF program to a
perf_event.
Got it, so that will reuse the uprobe_events in ftrace. But I think the uprobe requires a "path" to the attached binary, how is it specified?
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
But yeah, the main question is whether there is something preventing us from supporting multi-attach uprobe as well? It would be really great for USDT use case.
Ah, for the USDT, it will be useful. But since now we will have "user-event" which is faster than uprobes, we may be better to consider to use it.
Any pointers? I'm not sure what "user-event" refers to.
Here is the user-events series, which allows user program to define raw dynamic events and it can write raw event data directly from user space.
https://lore.kernel.org/all/[email protected]/
Thank you,
Signature · 1 line
Masami Hiramatsu <[email protected]>
Quoted 29 lines · Masami Hiramatsu
Quoted 17 lines · On Thu, 17 Feb 2022 14:01:30 -0800 Andrii Nakryiko <[email protected]>…
Quoted 9 lines
Quoted 3 lines
Is there any chance to support this fast multi-attach for uprobe? If yes, we might want to reuse the same link for both (so should we name it more generically?
There is no interface to do that but also there is no limitation to expand uprobes. For the kprobes, there are some limitations for the function entry because it needs to share the space with ftrace. So I introduced fprobe for easier to use.
Quoted 7 lines
Quoted 3 lines
on the other hand BPF program type for uprobe is
BPF_PROG_TYPE_KPROBEanyway, so keeping it as "kprobe" also would be consistent with what we have today).Hmm, I'm not sure why BPF made such design choice... (Uprobe needs the target program.)
Quoted 6 lines
We've been talking about sleepable uprobe programs, so we might need to add uprobe-specific program type, probably. But historically, from BPF point of view there was no difference between kprobe and uprobe programs (in terms of how they are run and what's available to them). From BPF point of view, it was just attaching BPF program to a
perf_event.
Quoted 3 lines
Got it, so that will reuse the
uprobe_eventsin ftrace. But I think the uprobe requires a "path" to the attached binary, how is it specified?
It's passed as a string to perf subsystem during perf_event_open() syscall.
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
But yeah, the main question is whether there is something preventing us from supporting multi-attach uprobe as well? It would be really great for USDT use case.
Ah, for the USDT, it will be useful. But since now we will have "user-event" which is faster than uprobes, we may be better to consider to use it.
Quoted 8 lines
Quoted 1 line
Any pointers? I'm not sure what "user-event" refers to.
Here is the user-events series, which allows user program to define raw dynamic events and it can write raw event data directly from user space.
Thanks for the link! I'll check it out.
Quoted 4 lines
Thank you,
--
Masami Hiramatsu <[email protected]>
Quoted 40 lines · Masami Hiramatsu
Quoted 17 lines · On Thu, 17 Feb 2022 14:01:30 -0800 Andrii Nakryiko <[email protected]>…
Quoted 9 lines
Quoted 3 lines
Is there any chance to support this fast multi-attach for uprobe? If yes, we might want to reuse the same link for both (so should we name it more generically?
There is no interface to do that but also there is no limitation to expand uprobes. For the kprobes, there are some limitations for the function entry because it needs to share the space with ftrace. So I introduced fprobe for easier to use.
Quoted 7 lines
Quoted 3 lines
on the other hand BPF program type for uprobe is
BPF_PROG_TYPE_KPROBEanyway, so keeping it as "kprobe" also would be consistent with what we have today).Hmm, I'm not sure why BPF made such design choice... (Uprobe needs the target program.)
Quoted 6 lines
We've been talking about sleepable uprobe programs, so we might need to add uprobe-specific program type, probably. But historically, from BPF point of view there was no difference between kprobe and uprobe programs (in terms of how they are run and what's available to them). From BPF point of view, it was just attaching BPF program to a
perf_event.Got it, so that will reuse the
uprobe_eventsin ftrace. But I think the uprobe requires a "path" to the attached binary, how is it specified?Quoted 7 lines
Quoted 6 lines
Quoted 3 lines
But yeah, the main question is whether there is something preventing us from supporting multi-attach uprobe as well? It would be really great for USDT use case.
Ah, for the USDT, it will be useful. But since now we will have "user-event" which is faster than uprobes, we may be better to consider to use it.
Quoted 7 lines
Quoted 1 line
Any pointers? I'm not sure what "user-event" refers to.
Here is the user-events series, which allows user program to define raw dynamic events and it can write raw event data directly from user space.
Is this a way for user space to inject user bytes into kernel events? What is the use case?
Masami Hiramatsu
maintainerQuoted 43 lines · On Fri, 18 Feb 2022 18:10:08 -0800 Alexei Starovoitov…
Quoted 42 lines · Masami Hiramatsu
Quoted 17 lines · On Thu, 17 Feb 2022 14:01:30 -0800 Andrii Nakryiko <[email protected]>…
Quoted 9 lines
Quoted 3 lines
Is there any chance to support this fast multi-attach for uprobe? If yes, we might want to reuse the same link for both (so should we name it more generically?
There is no interface to do that but also there is no limitation to expand uprobes. For the kprobes, there are some limitations for the function entry because it needs to share the space with ftrace. So I introduced fprobe for easier to use.
Quoted 7 lines
Quoted 3 lines
on the other hand BPF program type for uprobe is
BPF_PROG_TYPE_KPROBEanyway, so keeping it as "kprobe" also would be consistent with what we have today).Hmm, I'm not sure why BPF made such design choice... (Uprobe needs the target program.)
Quoted 6 lines
We've been talking about sleepable uprobe programs, so we might need to add uprobe-specific program type, probably. But historically, from BPF point of view there was no difference between kprobe and uprobe programs (in terms of how they are run and what's available to them). From BPF point of view, it was just attaching BPF program to a
perf_event.Got it, so that will reuse the
uprobe_eventsin ftrace. But I think the uprobe requires a "path" to the attached binary, how is it specified?Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
But yeah, the main question is whether there is something preventing us from supporting multi-attach uprobe as well? It would be really great for USDT use case.
Ah, for the USDT, it will be useful. But since now we will have "user-event" which is faster than uprobes, we may be better to consider to use it.
Any pointers? I'm not sure what "user-event" refers to.
Quoted 7 lines
Quoted 5 lines
Here is the user-events series, which allows user program to define raw dynamic events and it can write raw event data directly from user space.
Is this a way for user space to inject user bytes into kernel events?
Yes, it is.
Quoted 1 line
What is the use case?
This is like trace_marker but more ftrace/perf friendly version. The trace_marker can only send a user string, and the kernel can not parse it. Thus, the traced data will be shown in the trace buffer, but the event filter, event trigger, histogram etc didn't work with trace_marker.
On the other hand, the user-events allows user-space defines new events with various arguments with types, and the application can send the formatted raw data to the kernel. Thus the kernel can apply event filter, event trigger and histograms on those events as same as other kernel defined events.
This will be helpful for users to push their own data as events of ftrace and perf (and eBPF I think) so that they can use those tracing tools to analyze both of their events and kernel events. :-)
Thank you,
Signature · 1 line
Masami Hiramatsu <[email protected]>
On Wed, Feb 16, 2022 at 10:27:19AM -0800, Andrii Nakryiko wrote:
SNIP
Quoted 44 lines
Quoted 27 lines
hi,
tying to kick things further ;-) I was thinking about bpf side of this and we could use following interface:enum bpf_attach_type { ... BPF_TRACE_KPROBE_MULTI }; enum bpf_link_type { ... BPF_LINK_TYPE_KPROBE_MULTI }; union bpf_attr {struct { ... struct { __aligned_u64 syms; __aligned_u64 addrs; __aligned_u64 cookies; __u32 cnt; __u32 flags; } kprobe_multi; } link_create;Quoted 7 lines
}
because from bpf user POV it's new link for attaching multiple kprobes and I agree new 'fprobe' type name in here brings more confusion, using
kprobe_multiis straightforwardthoguhts?
I think this makes sense. We do need new type of link to store ip -> cookie mapping anyways.
Is there any chance to support this fast multi-attach for uprobe? If yes, we might want to reuse the same link for both (so should we name it more generically? on the other hand BPF program type for uprobe is
BPF_PROG_TYPE_KPROBEanyway, so keeping it as "kprobe" also would be consistent with what we have today).
Quoted 3 lines
But yeah, the main question is whether there is something preventing us from supporting multi-attach uprobe as well? It would be really great for USDT use case.
I need to check with uprobes, my understanding ends at perf/trace code calling uprobe_register ;-)
maybe I should first try if uprobes suffer the same performance issue
I'll send another version with above interface, because there's tons of other fixes, and by the time for next version we might have answer for the interface change
jirka