bpf: Add tracing_multi link support for bpf progs
Subsystems (9)
cover bpf: Add tracing_multi link support for bpf progs
Similar to the tracing_multi link support for kernel functions [1], add support for bpf progs.
When attaching to bpf progs, it must attaches to the target by text poke way.
[1] https://lore.kernel.org/bpf/[email protected]/
Leon Hwang (13):
bpf: Initialize ftrace_managed in bpf_trampoline_get
bpf: Factor out update_fentry_multi helper
bpf: Drop unnecessary ftrace_location() in update_fentry_multi()
bpf: Add tracing_multi link support for bpf progs
libbpf: Add tracing_multi link support for bpf progs
bpf: Add tracing_multi link fdinfo support for bpf progs
bpf: Add tracing_multi link info support for bpf progs selftests/bpf: Add tracing_multi bpf prog attach test selftests/bpf: Add tracing_multi bpf prog attach failure tests selftests/bpf: Add tracing_multi bpf prog cookie test selftests/bpf: Add tracing_multi bpf prog rollback test selftests/bpf: Add tracing_multi bpf prog link info test selftests/bpf: Test tailcall with fentry.multi
include/linux/bpf.h | 3 + include/uapi/linux/bpf.h | 6 +- kernel/bpf/trampoline.c | 87 +++--- kernel/bpf/verifier.c | 22 +- kernel/trace/bpf_trace.c | 212 ++++++++++++--- tools/bpf/bpftool/link.c | 59 +++- tools/include/uapi/linux/bpf.h | 6 +- tools/lib/bpf/bpf.c | 1 + tools/lib/bpf/bpf.h | 2 + tools/lib/bpf/libbpf.c | 39 ++- tools/lib/bpf/libbpf.h | 4 +- tools/lib/bpf/libbpf_internal.h | 1 + .../selftests/bpf/prog_tests/fill_link_info.c | 170 ++++++++++-- .../selftests/bpf/prog_tests/tailcalls.c | 62 +++++ .../selftests/bpf/prog_tests/tracing_multi.c | 254 ++++++++++++++++++ .../selftests/bpf/progs/tracing_multi_bpf.c | 61 +++++ 16 files changed, 869 insertions(+), 120 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/tracing_multi_bpf.c
--
2.55.0
PATCH 1/13 bpf: Initialize ftrace_managed in bpf_trampoline_get
A trampoline managed by ftrace is determined by its target, that said ftrace_managed is already known when the trampoline is initialized. so, set 'ftrace_managed = tr->ip != 0' in bpf_trampoline_get() instead of relying on the first regular or tracing_multi attachment to set it.
This makes the backend selection independent of attachment order and keeps the registration callbacks focused on installing the trampoline.
--- kernel/bpf/trampoline.c | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-)
kernel/bpf/trampoline.c +2 -9
@@ -474,18 +474,11 @@ static int register_fentry(struct bpf_trampoline *tr, struct bpf_tramp_image *im void *data __maybe_unused) {void *new_addr = im->image; -void *ip = tr->func.addr; -unsigned long faddr;int ret; - faddr =ftrace_location ((unsigned long )ip); -if (faddr) { +if (tr->func.ftrace_managed) {if (!tr->fops)return -ENOTSUPP ; - tr->func.ftrace_managed = true; - } - -if (tr->func.ftrace_managed) { ret =direct_ops_add (tr, new_addr); }else { ret =bpf_trampoline_update_fentry (tr,0 ,NULL , new_addr);
@@ -1226,6 +1219,7 @@ struct bpf_trampoline *bpf_trampoline_get(u64 key,memcpy (&tr->func.model, &tgt_info->fmodel,sizeof (tgt_info->fmodel)); tr->func.addr = (void *)tgt_info->tgt_addr; + tr->func.ftrace_managed = tr->ip !=0 ; out:trampoline_unlock (tr);return tr;
@@ -1583,7 +1577,6 @@ static int register_fentry_multi(struct bpf_trampoline *tr, struct bpf_tramp_imaif (bpf_trampoline_use_jmp (tr->flags)) addr =ftrace_jmp_set (addr); - tr->func.ftrace_managed = true;ftrace_hash_add (data->reg, data->entry, ip, addr); tr->cur_image = im;return 0 ;
Signature · 1 line
2.55.0
PATCH 2/13 bpf: Factor out update_fentry_multi helper
In reply to Leon HwangThe tracing_multi register, unregister, and modify callbacks differ only in the trampoline image and ftrace hash they use. Move their common address setup, hash insertion, and current image update into update_fentry_multi().
No functional changes intended.
--- kernel/bpf/trampoline.c | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-)
kernel/bpf/trampoline.c +14 -20
@@ -1568,47 +1568,41 @@ static void ftrace_hash_add(struct ftrace_hash *hash, struct ftrace_func_entry *add_ftrace_hash_entry (hash, entry); } -static int register_fentry_multi (struct bpf_trampoline *tr,struct bpf_tramp_image *im,void *ptr) +static int update_fentry_multi (struct bpf_trampoline *tr,u32 orig_flags, +struct bpf_tramp_image *im,struct ftrace_hash *hash, +struct bpf_tracing_multi_data *data) { -unsigned long addr = (unsigned long ) im->image; +unsigned long addr = (unsigned long )(im ? im->image : tr->cur_image->image);unsigned long ip =ftrace_location (tr->ip); -struct bpf_tracing_multi_data *data = ptr;if (bpf_trampoline_use_jmp (tr->flags)) addr =ftrace_jmp_set (addr); -ftrace_hash_add (data->reg, data->entry, ip, addr); +ftrace_hash_add (hash, data->entry, ip, addr); tr->cur_image = im;return 0 ; } -static int unregister_fentry_multi (struct bpf_trampoline *tr,u32 orig_flags,void *ptr) +static int register_fentry_multi (struct bpf_trampoline *tr,struct bpf_tramp_image *im,void *ptr) { -unsigned long addr = (unsigned long ) tr->cur_image->image; -unsigned long ip =ftrace_location (tr->ip);struct bpf_tracing_multi_data *data = ptr; -if (bpf_trampoline_use_jmp (tr->flags)) - addr =ftrace_jmp_set (addr); +return update_fentry_multi (tr,0 , im, data->reg, data); +} -ftrace_hash_add (data->unreg, data->entry, ip, addr); - tr->cur_image =NULL ; -return 0 ; +static int unregister_fentry_multi (struct bpf_trampoline *tr,u32 orig_flags,void *ptr) +{ +struct bpf_tracing_multi_data *data = ptr; + +return update_fentry_multi (tr, orig_flags,NULL , data->unreg, data); }static int modify_fentry_multi (struct bpf_trampoline *tr,u32 orig_flags,struct bpf_tramp_image *im,bool lock_direct_mutex,void *ptr) { -unsigned long addr = (unsigned long ) im->image; -unsigned long ip =ftrace_location (tr->ip);struct bpf_tracing_multi_data *data = ptr; -if (bpf_trampoline_use_jmp (tr->flags)) - addr =ftrace_jmp_set (addr); - -ftrace_hash_add (data->modify, data->entry, ip, addr); - tr->cur_image = im; -return 0 ; +return update_fentry_multi (tr, orig_flags, im, data->modify, data); }static const struct bpf_trampoline_ops trampoline_multi_ops = {
Signature · 1 line
2.55.0
PATCH 3/13 bpf: Drop unnecessary ftrace_location() in update_fentry_multi()
In reply to Leon HwangSince 'tr->ip' is already a 'ftrace_location()'-ed IP in bpf_trampoline_lookup(), it is unnecessary to call ftrace_location() again in update_fentry_multi().
--- kernel/bpf/trampoline.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
kernel/bpf/trampoline.c +1 -2
@@ -1573,12 +1573,11 @@ static int update_fentry_multi(struct bpf_trampoline *tr, u32 orig_flags, struct bpf_tracing_multi_data *data) {unsigned long addr = (unsigned long )(im ? im->image : tr->cur_image->image); -unsigned long ip =ftrace_location (tr->ip);if (bpf_trampoline_use_jmp (tr->flags)) addr =ftrace_jmp_set (addr); -ftrace_hash_add (hash, data->entry, ip, addr); +ftrace_hash_add (hash, data->entry, tr->ip, addr); tr->cur_image = im;return 0 ; }
Signature · 1 line
2.55.0
PATCH 4/13 bpf: Add tracing_multi link support for bpf progs
In reply to Leon HwangSimilar to the tracing_multi link support for kernel functions, add support for bpf progs by reusing the 'ids' fields as BTF ids, and adding fds array to indicate the target bpf prog. Then, for each pair of fd+id, they will be the attach targets of tracing_multi link.
When attaching bpf prog, it must attaches to the target by text poke way.
--- include/linux/bpf.h | 3 + include/uapi/linux/bpf.h | 2 + kernel/bpf/trampoline.c | 45 ++++++++-- kernel/bpf/verifier.c | 22 ++--- kernel/trace/bpf_trace.c | 157 ++++++++++++++++++++++++++++----- tools/include/uapi/linux/bpf.h | 2 + 6 files changed, 189 insertions(+), 42 deletions(-)
include/linux/bpf.h +3 -0
@@ -1517,6 +1517,7 @@ void bpf_trampoline_put(struct bpf_trampoline *tr);int arch_prepare_bpf_dispatcher (void *image,void *buf,s64 *funcs,int num_funcs);int bpf_trampoline_multi_attach (struct bpf_prog *prog,u32 *ids, +u64 *keys,struct bpf_prog **progs,struct bpf_tracing_multi_link *link);int bpf_trampoline_multi_detach (struct bpf_prog *prog,struct bpf_tracing_multi_link *link);
@@ -1635,6 +1636,7 @@ static inline bool bpf_prog_has_trampoline(const struct bpf_prog *prog)return false; }static inline int bpf_trampoline_multi_attach (struct bpf_prog *prog,u32 *ids, +u64 *keys,struct bpf_prog **progs,struct bpf_tracing_multi_link *link) {return -ENOTSUPP ;
@@ -2002,6 +2004,7 @@ struct bpf_tracing_multi_link {struct bpf_tracing_multi_data data;u64 *cookies;struct bpf_tramp_node *fexits; +struct bpf_prog **progs;int nodes_cnt; struct bpf_tracing_multi_node nodes[] __counted_by(nodes_cnt); };
include/uapi/linux/bpf.h +2 -0
@@ -1888,6 +1888,8 @@ union bpf_attr {__aligned_u64 ids;__aligned_u64 cookies;__u32 cnt; + __u32 :32 ; +__aligned_u64 fds; } tracing_multi; }; } link_create;
kernel/bpf/trampoline.c +36 -9
84 lines not on this page · raw message
kernel/bpf/verifier.c +12 -10
@@ -19291,22 +19291,24 @@ int bpf_check_attach_target(struct bpf_verifier_log *log,return -EOPNOTSUPP ; } -/* -* *.multi programs don't need an address during program -* verification, we just take the module ref if needed. -*/ -if (is_tracing_multi_id (prog, btf_id)) { +if (tgt_prog) { +if (subprog ==0 ) + addr = (long ) tgt_prog->bpf_func; +else + addr = (long ) tgt_prog->aux->func[subprog]->bpf_func; + }else if (is_tracing_multi_id (prog, btf_id)) { +/* +* *.multi programs don't need an address during program +* verification, we just take the module ref if needed. +* *.multi programs don't have tgt_prog during program +* verification. +*/ if (btf_is_module (btf)) { mod =btf_try_get_module (btf);if (!mod)return -ENOENT ; } addr =0 ; - }else if (tgt_prog) { -if (subprog ==0 ) - addr = (long ) tgt_prog->bpf_func; -else - addr = (long ) tgt_prog->aux->func[subprog]->bpf_func; }else {if (btf_is_module (btf)) { mod =btf_try_get_module (btf);
kernel/trace/bpf_trace.c +134 -23
248 lines not on this page · raw message
tools/include/uapi/linux/bpf.h +2 -0
@@ -1888,6 +1888,8 @@ union bpf_attr {__aligned_u64 ids;__aligned_u64 cookies;__u32 cnt; + __u32 :32 ; +__aligned_u64 fds; } tracing_multi; }; } link_create;
Signature · 1 line
2.55.0
PATCH 5/13 libbpf: Add tracing_multi link support for bpf progs
In reply to Leon HwangBy design, a tracing_multi link does not support both kernel functions and bpf progs at the same time.
In libbpf, the bpf_tracing_multi_opts options provides two new fields for tracing bpf progs:
* 'fds': the fd array of the target bpf progs. One fd must be provided multiple times for multiple targets.
* 'funcs': the function name array of the target bpf progs.
The 'cnt' field indicates the size of both 'fds' and 'funcs' arrays.
The 'funcs' will be translated to 'ids' by 'libbpf_find_prog_btf_id()'.
At last, 'fds'+'ids' will be passed to kernel.
--- tools/lib/bpf/bpf.c | 1 + tools/lib/bpf/bpf.h | 2 ++ tools/lib/bpf/libbpf.c | 37 +++++++++++++++++++++++++++++++++---- tools/lib/bpf/libbpf.h | 4 +++- 4 files changed, 39 insertions(+), 5 deletions(-)
tools/lib/bpf/bpf.c +1 -0
@@ -852,6 +852,7 @@ int bpf_link_create(int prog_fd, int target_fd, attr.link_create.tracing_multi.ids =ptr_to_u64 (OPTS_GET (opts, tracing_multi.ids,0 )); attr.link_create.tracing_multi.cookies =ptr_to_u64 (OPTS_GET (opts, tracing_multi.cookies,0 )); attr.link_create.tracing_multi.cnt =OPTS_GET (opts, tracing_multi.cnt,0 ); + attr.link_create.tracing_multi.fds =ptr_to_u64 (OPTS_GET (opts, tracing_multi.fds,0 ));if (!OPTS_ZEROED (opts, tracing_multi))return libbpf_err (-EINVAL );break ;
tools/lib/bpf/bpf.h +2 -0
@@ -474,6 +474,8 @@ struct bpf_link_create_opts {const __u32 *ids;const __u64 *cookies;__u32 cnt; + __u32 :32 ; +const int *fds; } tracing_multi; }; size_t :0;
tools/lib/bpf/libbpf.c +33 -4
@@ -12687,10 +12687,12 @@ bpf_program__attach_tracing_multi(const struct bpf_program *prog, const char *pa {LIBBPF_OPTS (bpf_link_create_opts, lopts);int prog_fd, link_fd, err, cnt; +struct bpf_link *link =NULL ;__u32 *free_ids =NULL ; -struct bpf_link *link;const __u64 *cookies; +const char **funcs;const __u32 *ids; +const int *fds;if (!OPTS_VALID (opts, bpf_tracing_multi_opts))return libbpf_err_ptr (-EINVAL );
@@ -12705,12 +12707,22 @@ bpf_program__attach_tracing_multi(const struct bpf_program *prog, const char *pa cnt =OPTS_GET (opts, cnt,0 ); ids =OPTS_GET (opts, ids,NULL ); cookies =OPTS_GET (opts, cookies,NULL ); + fds =OPTS_GET (opts, fds,NULL ); + funcs =OPTS_GET (opts, funcs,NULL ); -if (!!ids != !!cnt) +if (ids && fds) +return libbpf_err_ptr (-EINVAL ); +if (!!fds != !!funcs) +return libbpf_err_ptr (-EINVAL ); +if (fds && !cnt)return libbpf_err_ptr (-EINVAL ); -if (pattern && (ids || cookies)) +if (ids && !cnt)return libbpf_err_ptr (-EINVAL ); -if (!pattern && !ids) +if (!fds && !ids && cnt) +return libbpf_err_ptr (-EINVAL ); +if (pattern && (fds || ids || cookies)) +return libbpf_err_ptr (-EINVAL ); +if (!pattern && !ids && !fds)return libbpf_err_ptr (-EINVAL );if (pattern) {
@@ -12720,11 +12732,28 @@ bpf_program__attach_tracing_multi(const struct bpf_program *prog, const char *paif (cnt ==0 )return libbpf_err_ptr (-EINVAL ); ids = (const __u32 *) free_ids; + }else if (fds) { +size_t cap =0 ; +int i; + + err =libbpf_ensure_mem ((void **) &free_ids, &cap,sizeof (*free_ids), cnt); +if (err) +return libbpf_err_ptr (err); + +for (i =0 ; i < cnt; i++) { + err =libbpf_find_prog_btf_id (funcs[i], fds[i], prog->obj->token_fd); +if (err <0 ) + goto error; + + free_ids[i] = err; + } + ids = (const __u32 *) free_ids; } lopts.tracing_multi.ids = ids; lopts.tracing_multi.cookies = cookies; lopts.tracing_multi.cnt = cnt; + lopts.tracing_multi.fds = fds; link =calloc (1 ,sizeof (*link));if (!link) {
tools/lib/bpf/libbpf.h +3 -1
@@ -732,10 +732,12 @@ struct bpf_tracing_multi_opts {const __u32 *ids;const __u64 *cookies;size_t cnt; +const int *fds; +const char **funcs; size_t :0; }; -#define bpf_tracing_multi_opts__last_field cnt +#define bpf_tracing_multi_opts__last_field funcs LIBBPF_API struct bpf_link * bpf_program__attach_tracing_multi(const struct bpf_program *prog, const char *pattern,
Signature · 1 line
2.55.0
PATCH 6/13 bpf: Add tracing_multi link fdinfo support for bpf progs
In reply to Leon HwangShow the target bpf prog info in the fdinfo.
$ cat /proc/self/fdinfo/13 pos: 0 flags: 02000000 mnt_id: 18 ino: 1045 link_type: tracing_multi link_id: 9 prog_tag: 9a7f9e3ab6f138ab prog_id: 25 attach_type: 61 cnt: 2 prog-id btf-id cookie func 21 4 16706819 target_1 22 5 16706819 target_2
--- kernel/trace/bpf_trace.c | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-)
kernel/trace/bpf_trace.c +25 -5
@@ -3759,26 +3759,46 @@ static int bpf_tracing_multi_link_fill_link_info(const struct bpf_link *link, }#ifdef CONFIG_PROC_FS +static const char *bpf_prog_func_name (struct bpf_prog *prog,u32 btf_id) +{ +const struct btf *btf = prog->aux->btf; +const struct btf_type *t; + + t =btf_type_by_id (btf, btf_id); +if (!t || !btf_type_is_func (t)) +return "" ; + +return btf_name_by_offset (btf, t->name_off); +} +static void bpf_tracing_multi_show_fdinfo (const struct bpf_link *link,struct seq_file *seq) {struct bpf_tracing_multi_link *tr_link = container_of(link, struct bpf_tracing_multi_link, link);bool has_cookies = !!tr_link->cookies; +bool has_progs = !!tr_link->progs;seq_printf (seq,"attach_type:\t%u\n" , tr_link->link.attach_type);seq_printf (seq,"cnt:\t%u\n" , tr_link->nodes_cnt); -seq_printf (seq,"%s\t %s\t %s\t %s\n" ,"obj-id" ,"btf-id" ,"cookie" ,"func" ); +seq_printf (seq,"%s\t %s\t %s\t %s\n" , has_progs ?"prog-id" :"obj-id" ,"btf-id" ,"cookie" , +"func" );for (int i =0 ; i < tr_link->nodes_cnt; i++) {struct bpf_tracing_multi_node *mnode = &tr_link->nodes[i]; +u64 cookie = has_cookies ? tr_link->cookies[i] :0 ;u32 btf_id, obj_id;bpf_trampoline_unpack_key (mnode->trampoline->key, &obj_id, &btf_id); -seq_printf (seq,"%u\t %u\t %llu\t %pS\n" , - obj_id, btf_id, - has_cookies ? tr_link->cookies[i] :0 , - (void *) mnode->trampoline->ip); + btf_id = has_progs ? (u32 ) mnode->trampoline->key : btf_id; +if (has_progs) +seq_printf (seq,"%u\t %u\t %llu\t %s\n" , + obj_id, btf_id, cookie, +bpf_prog_func_name (tr_link->progs[i], btf_id)); +else +seq_printf (seq,"%u\t %u\t %llu\t %pS\n" , + obj_id, btf_id, cookie, + (void *) mnode->trampoline->ip);cond_resched (); }
Signature · 1 line
2.55.0
PATCH 7/13 bpf: Add tracing_multi link info support for bpf progs
In reply to Leon HwangShow bpf prog info in the link info.
$ bpftool link 9: tracing_multi prog 25 attach_type trace_fsession_multi btf_obj_id 1 count 2 prog_id func_btf_id addr cookie func [module] 21 4 ffffffffc080064c 16706819 target_1 [bpf] 22 5 ffffffffc08006d4 16706819 target_2 [bpf] pids test_progs(98)
--- include/uapi/linux/bpf.h | 4 ++- kernel/trace/bpf_trace.c | 25 ++++++++++---- tools/bpf/bpftool/link.c | 59 +++++++++++++++++++++++++++------- tools/include/uapi/linux/bpf.h | 4 ++- 4 files changed, 71 insertions(+), 21 deletions(-)
include/uapi/linux/bpf.h +3 -1
@@ -6890,10 +6890,12 @@ struct bpf_link_info {__u32 attach_type;__u32 count;/* in/out: tracing_multi target count */ __u32 btf_obj_id; - __u32 :32 ; + __u32 tgt_progs:1; + __u32 :31 ;__aligned_u64 ids;__aligned_u64 addrs;__aligned_u64 cookies; +__aligned_u64 func_btf_ids; } tracing_multi;struct {__u32 type;/* enum bpf_perf_event_type */
kernel/trace/bpf_trace.c +18 -7
@@ -3707,15 +3707,16 @@ static void bpf_tracing_multi_link_dealloc(struct bpf_link *link)static int bpf_tracing_multi_link_fill_link_info (const struct bpf_link *link,struct bpf_link_info *info) { +u32 __user *fids =u64_to_user_ptr (info->tracing_multi.func_btf_ids);u64 __user *ucookies =u64_to_user_ptr (info->tracing_multi.cookies);u64 __user *uaddrs =u64_to_user_ptr (info->tracing_multi.addrs);u32 __user *uids =u64_to_user_ptr (info->tracing_multi.ids);struct bpf_tracing_multi_link *tr_link;u32 ucount = info->tracing_multi.count; -bool has_cookies, show_addrs; +bool has_cookies, show_addrs, has_progs;int err =0 ; -if ((uids || ucookies || uaddrs) && !ucount) +if ((uids || ucookies || uaddrs || fids) && !ucount)return -EINVAL ; tr_link = container_of(link, struct bpf_tracing_multi_link, link);
@@ -3723,9 +3724,12 @@ static int bpf_tracing_multi_link_fill_link_info(const struct bpf_link *link, info->tracing_multi.attach_type = tr_link->link.attach_type; info->tracing_multi.count = tr_link->nodes_cnt; info->tracing_multi.btf_obj_id =btf_obj_id (tr_link->link.prog->aux->attach_btf); + info->tracing_multi.tgt_progs = has_progs = !!tr_link->progs; -if (!uids && !ucookies && !uaddrs) +if (!uids && !ucookies && !uaddrs && !fids)return 0 ; +if (has_progs != !!fids) +return -EINVAL ;if (ucount < tr_link->nodes_cnt) err = -ENOSPC ;
@@ -3737,20 +3741,27 @@ static int bpf_tracing_multi_link_fill_link_info(const struct bpf_link *link,for (int i =0 ; i < ucount; i++) {struct bpf_tracing_multi_node *mnode = &tr_link->nodes[i]; +u32 prog_id, btf_id;u64 addr, cookie; -u32 id; -bpf_trampoline_unpack_key (mnode->trampoline->key,NULL , &id); +bpf_trampoline_unpack_key (mnode->trampoline->key, &prog_id, &btf_id); - addr = show_addrs ? mnode->trampoline->ip :0 ; +if (!show_addrs) + addr =0 ; +else if (has_progs) + addr = (u64 ) mnode->trampoline->func.addr; +else + addr = mnode->trampoline->ip; cookie = has_cookies ? tr_link->cookies[i] :0 ; -if (uids &&put_user (id, uids + i)) +if (uids &&put_user (has_progs ? prog_id : btf_id, uids + i))return -EFAULT ;if (uaddrs &&put_user (addr, uaddrs + i))return -EFAULT ;if (ucookies &&put_user (cookie, ucookies + i))return -EFAULT ; +if (has_progs &&put_user ((u32 ) mnode->trampoline->key, fids + i)) +return -EFAULT ;cond_resched (); }
tools/bpf/bpftool/link.c +47 -12
133 lines not on this page · raw message
tools/include/uapi/linux/bpf.h +3 -1
@@ -6890,10 +6890,12 @@ struct bpf_link_info {__u32 attach_type;__u32 count;/* in/out: tracing_multi target count */ __u32 btf_obj_id; - __u32 :32 ; + __u32 tgt_progs:1; + __u32 :31 ;__aligned_u64 ids;__aligned_u64 addrs;__aligned_u64 cookies; +__aligned_u64 func_btf_ids; } tracing_multi;struct {__u32 type;/* enum bpf_perf_event_type */
Signature · 1 line
2.55.0
PATCH 8/13 selftests/bpf: Add tracing_multi bpf prog attach test
In reply to Leon HwangAttach to two bpf progs with fentry.multi, fexit.multi, and fsession.multi progs.
--- .../selftests/bpf/prog_tests/tracing_multi.c | 70 +++++++++++++++++++ .../selftests/bpf/progs/tracing_multi_bpf.c | 52 ++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/tracing_multi_bpf.c
tools/testing/selftests/bpf/prog_tests/tracing_multi.c +70 -0
@@ -12,6 +12,7 @@#include "tracing_multi_verifier.skel.h" #include "tracing_multi_bench.skel.h" #include "tracing_multi_rollback.skel.h" +#include "tracing_multi_bpf.skel.h" #include "trace_helpers.h" static __u64 bpf_fentry_test_cookies[] = {
@@ -288,6 +289,73 @@ static void test_link_api_ids(bool test_cookies)free (ids); } +static int run_bpf_target (struct bpf_program *prog,__u32 retval,const char *name) +{ +LIBBPF_OPTS (bpf_test_run_opts, topts); +int err; + + err =bpf_prog_test_run_opts (bpf_program__fd (prog), &topts); +if (!ASSERT_OK (err, name)) +return err; + +if (!ASSERT_EQ (topts.retval, retval, name)) +return -EINVAL ; + +return 0 ; +} + +static void test_link_api_bpf_prog (void ) +{ +const char *funcs[] = {"target_1" ,"target_2" }; +LIBBPF_OPTS (bpf_tracing_multi_opts, opts); +struct tracing_multi_bpf *skel =NULL ; +int fds[ARRAY_SIZE (funcs)]; +struct bpf_link *link; + + skel =tracing_multi_bpf__open_and_load (); +if (!ASSERT_OK_PTR (skel,"tracing_multi_bpf__open_and_load" )) +return ; + + fds[0 ] =bpf_program__fd (skel->progs.target_1); + fds[1 ] =bpf_program__fd (skel->progs.target_2); + + opts.fds = fds; + opts.funcs = funcs; + opts.cnt =ARRAY_SIZE (fds); + + link =bpf_program__attach_tracing_multi (skel->progs.test_fentry,NULL , &opts); +if (!ASSERT_OK_PTR (link,"attach_fentry" )) + goto cleanup; + skel->links.test_fentry = link; + + link =bpf_program__attach_tracing_multi (skel->progs.test_fexit,NULL , &opts); +if (!ASSERT_OK_PTR (link,"attach_fexit" )) + goto cleanup; + skel->links.test_fexit = link; + + link =bpf_program__attach_tracing_multi (skel->progs.test_fsession,NULL , &opts); +if (!ASSERT_OK_PTR (link,"attach_fsession" )) + goto cleanup; + skel->links.test_fsession = link; + +if (run_bpf_target (skel->progs.target_1,1 ,"target_1" )) + goto cleanup; +if (run_bpf_target (skel->progs.target_2,2 ,"target_2" )) + goto cleanup; + +ASSERT_EQ (skel->bss->target_1_result,1 ,"target_1_result" ); +ASSERT_EQ (skel->bss->target_2_result,1 ,"target_2_result" ); +ASSERT_EQ (skel->bss->test_result_fentry,ARRAY_SIZE (funcs),"test_result_fentry" ); +ASSERT_EQ (skel->bss->test_result_fexit,ARRAY_SIZE (funcs),"test_result_fexit" ); +ASSERT_EQ (skel->bss->test_result_fsession_entry,ARRAY_SIZE (funcs), +"test_result_fsession_entry" ); +ASSERT_EQ (skel->bss->test_result_fsession_exit,ARRAY_SIZE (funcs), +"test_result_fsession_exit" ); + +cleanup: +tracing_multi_bpf__destroy (skel); +} +static void test_module_skel_api (void ) {struct tracing_multi_module *skel =NULL ;
@@ -1026,4 +1094,6 @@ void test_tracing_multi_test(void)RUN_TESTS (tracing_multi_verifier);if (test__start_subtest ("fentry_after_multi" ))test_fentry_after_multi (); +if (test__start_subtest ("link_api_bpf_prog" )) +test_link_api_bpf_prog (); }
tools/testing/selftests/bpf/progs/tracing_multi_bpf.c new file +52 -0
52 lines not on this page · raw message
Signature · 1 line
2.55.0
PATCH 9/13 selftests/bpf: Add tracing_multi bpf prog attach failure tests
In reply to Leon HwangTests against two libbpf failures and two kernel failures:
* libbpf: 'ids' + 'fds'.
* libbpf: 'pattern' + 'fds'.
* kernel: 'fds' + invalid 'ids'.
* kernel: duplicate targets via different fd for the same bpf prog + func.
--- .../selftests/bpf/prog_tests/tracing_multi.c | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+)
tools/testing/selftests/bpf/prog_tests/tracing_multi.c +100 -0
@@ -730,6 +730,104 @@ static void test_attach_api_fails(void)free (ids2); } +static void test_attach_api_bpf_prog_fails (void ) +{ +static const char *const kfuncs[] = { +"bpf_fentry_test1" , + }; +const char *bpf_funcs[] = {"target_1" }; +LIBBPF_OPTS (bpf_tracing_multi_opts, opts); +LIBBPF_OPTS (bpf_link_create_opts, lopts); +struct tracing_multi_bpf *skel =NULL ; +int fd, target_id, token_fd; +int fds[2 ], dup_fd =-1 ; +struct bpf_link *link; +__u32 target_ids[2 ]; +__u32 *ids =NULL ; +long err; + + skel =tracing_multi_bpf__open_and_load (); +if (!ASSERT_OK_PTR (skel,"tracing_multi_bpf__open_and_load" )) +return ; + + ids =get_ids (kfuncs,ARRAY_SIZE (kfuncs),NULL ); +if (!ASSERT_OK_PTR (ids,"get_ids" )) + goto cleanup; + + fds[0 ] =bpf_program__fd (skel->progs.target_1); + +/* fail#1 (libbpf) kernel ids and BPF prog fds */ + LIBBPF_OPTS_RESET(opts, + .ids = ids, + .fds = fds, + .funcs = bpf_funcs, + .cnt = ARRAY_SIZE(bpf_funcs), + ); + + link =bpf_program__attach_tracing_multi (skel->progs.test_fentry,NULL , &opts); + err =libbpf_get_error (link); +if (!ASSERT_EQ (err, -EINVAL ,"libbpf_ids_fds" )) { +if (!err) +bpf_link__destroy (link); + goto cleanup; + } + +/* fail#2 (libbpf) kernel pattern and BPF prog fds */ + LIBBPF_OPTS_RESET(opts, + .fds = fds, + .funcs = bpf_funcs, + .cnt = ARRAY_SIZE(bpf_funcs), + ); + + link =bpf_program__attach_tracing_multi (skel->progs.test_fentry, +"bpf_fentry_test*" , &opts); + err =libbpf_get_error (link); +if (!ASSERT_EQ (err, -EINVAL ,"libbpf_pattern_fds" )) { +if (!err) +bpf_link__destroy (link); + goto cleanup; + } + +/* fail#3 (kernel) kernel id with BPF prog fd */ + lopts.tracing_multi.ids = ids; + lopts.tracing_multi.fds = fds; + lopts.tracing_multi.cnt =ARRAY_SIZE (bpf_funcs); + fd =bpf_link_create (bpf_program__fd (skel->progs.test_fentry),0 , +BPF_TRACE_FENTRY_MULTI , &lopts); +ASSERT_EQ (fd, -EINVAL ,"kernel_id_bpf_fd" ); +if (fd >=0 ) +close (fd); + +/* fail#4 (kernel) duplicate target through different fds */ + token_fd =bpf_object__token_fd (skel->obj); + token_fd = token_fd <0 ?0 : token_fd; + target_id =libbpf_find_prog_btf_id ("target_1" , fds[0 ], token_fd); +if (!ASSERT_GT (target_id,0 ,"target_1_btf_id" )) + goto cleanup; + + dup_fd =dup (fds[0 ]); +if (!ASSERT_GE (dup_fd,0 ,"dup_target_fd" )) + goto cleanup; + + fds[1 ] = dup_fd; + target_ids[0 ] = target_id; + target_ids[1 ] = target_id; + lopts.tracing_multi.ids = target_ids; + lopts.tracing_multi.fds = fds; + lopts.tracing_multi.cnt =ARRAY_SIZE (fds); + fd =bpf_link_create (bpf_program__fd (skel->progs.test_fentry),0 , +BPF_TRACE_FENTRY_MULTI , &lopts); +ASSERT_EQ (fd, -EINVAL ,"duplicate_bpf_target" ); +if (fd >=0 ) +close (fd); + +cleanup: +if (dup_fd >=0 ) +close (dup_fd); +tracing_multi_bpf__destroy (skel); +free (ids); +} +void serial_test_tracing_multi_bench_attach (void ) {LIBBPF_OPTS (bpf_tracing_multi_opts, opts);
@@ -1096,4 +1194,6 @@ void test_tracing_multi_test(void)test_fentry_after_multi ();if (test__start_subtest ("link_api_bpf_prog" ))test_link_api_bpf_prog (); +if (test__start_subtest ("attach_api_bpf_prog_fails" )) +test_attach_api_bpf_prog_fails (); }
Signature · 1 line
2.55.0
PATCH 10/13 selftests/bpf: Add tracing_multi bpf prog cookie test
In reply to Leon HwangVerify that the cookie also works for bpf prog targets of tracing_multi link.
--- .../selftests/bpf/prog_tests/tracing_multi.c | 25 ++++++++++++++++++- .../selftests/bpf/progs/tracing_multi_bpf.c | 13 ++++++++-- 2 files changed, 35 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/prog_tests/tracing_multi.c +24 -1
@@ -304,9 +304,12 @@ static int run_bpf_target(struct bpf_program *prog, __u32 retval, const char *nareturn 0 ; } -static void test_link_api_bpf_prog (void ) +static void __test_link_api_bpf_prog (bool test_cookies) {const char *funcs[] = {"target_1" ,"target_2" }; +__u64 fentry_cookies[] = {0xfeed01 ,0xfeed01 }; +__u64 fexit_cookies[] = {0xfeed02 ,0xfeed02 }; +__u64 fsession_cookies[] = {0xfeed03 ,0xfeed03 };LIBBPF_OPTS (bpf_tracing_multi_opts, opts);struct tracing_multi_bpf *skel =NULL ;int fds[ARRAY_SIZE (funcs)];
@@ -319,20 +322,28 @@ static void test_link_api_bpf_prog(void) fds[0 ] =bpf_program__fd (skel->progs.target_1); fds[1 ] =bpf_program__fd (skel->progs.target_2); + skel->bss->test_cookies = test_cookies; + skel->bss->fentry_cookie = fentry_cookies[0 ]; + skel->bss->fexit_cookie = fexit_cookies[0 ]; + skel->bss->fsession_cookie = fsession_cookies[0 ]; + opts.fds = fds; opts.funcs = funcs; opts.cnt =ARRAY_SIZE (fds); + opts.cookies = test_cookies ? fentry_cookies :NULL ; link =bpf_program__attach_tracing_multi (skel->progs.test_fentry,NULL , &opts);if (!ASSERT_OK_PTR (link,"attach_fentry" )) goto cleanup; skel->links.test_fentry = link; + opts.cookies = test_cookies ? fexit_cookies :NULL ; link =bpf_program__attach_tracing_multi (skel->progs.test_fexit,NULL , &opts);if (!ASSERT_OK_PTR (link,"attach_fexit" )) goto cleanup; skel->links.test_fexit = link; + opts.cookies = test_cookies ? fsession_cookies :NULL ; link =bpf_program__attach_tracing_multi (skel->progs.test_fsession,NULL , &opts);if (!ASSERT_OK_PTR (link,"attach_fsession" )) goto cleanup;
@@ -356,6 +367,16 @@ static void test_link_api_bpf_prog(void)tracing_multi_bpf__destroy (skel); } +static void test_link_api_bpf_prog (void ) +{ +__test_link_api_bpf_prog (false); +} + +static void test_cookies_bpf_prog (void ) +{ +__test_link_api_bpf_prog (true); +} +static void test_module_skel_api (void ) {struct tracing_multi_module *skel =NULL ;
@@ -1196,4 +1217,6 @@ void test_tracing_multi_test(void)test_link_api_bpf_prog ();if (test__start_subtest ("attach_api_bpf_prog_fails" ))test_attach_api_bpf_prog_fails (); +if (test__start_subtest ("cookies_bpf_prog" )) +test_cookies_bpf_prog (); }
tools/testing/selftests/bpf/progs/tracing_multi_bpf.c +11 -2
@@ -5,6 +5,10 @@ char _license[] SEC("license") = "GPL"; +bool test_cookies; +__u64 fentry_cookie; +__u64 fexit_cookie; +__u64 fsession_cookie;__u64 test_result_fentry;__u64 test_result_fexit;__u64 test_result_fsession_entry;
@@ -29,20 +33,25 @@ int target_2(void *ctx) SEC("fentry.multi")int BPF_PROG (test_fentry ) { - test_result_fentry++; +if (!test_cookies ||bpf_get_attach_cookie (ctx) == fentry_cookie) + test_result_fentry++;return 0 ; } SEC("fexit.multi")int BPF_PROG (test_fexit ) { - test_result_fexit++; +if (!test_cookies ||bpf_get_attach_cookie (ctx) == fexit_cookie) + test_result_fexit++;return 0 ; } SEC("fsession.multi")int BPF_PROG (test_fsession ) { +if (test_cookies &&bpf_get_attach_cookie (ctx) != fsession_cookie) +return 0 ; +if (bpf_session_is_return (ctx)) test_result_fsession_exit++;else
Signature · 1 line
2.55.0
PATCH 11/13 selftests/bpf: Add tracing_multi bpf prog rollback test
In reply to Leon HwangWhen rollbacking, the prepared trampolines should be reverted.
In this test, the first bpf prog will be attached with the trampoline, and the second bpf prog won't be attached because the bpf prog has been attached by a fentry.multi prog, that causes the rollback procedure.
--- tools/lib/bpf/libbpf.c | 2 +- tools/lib/bpf/libbpf_internal.h | 1 + .../selftests/bpf/prog_tests/tracing_multi.c | 61 +++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +1 -1
@@ -10629,7 +10629,7 @@ int libbpf_find_vmlinux_btf_id(const char *name,return libbpf_err (err); } -static int libbpf_find_prog_btf_id (const char *name,__u32 attach_prog_fd,int token_fd) +int libbpf_find_prog_btf_id (const char *name,__u32 attach_prog_fd,int token_fd) {struct bpf_prog_info info;__u32 info_len =sizeof (info);
tools/lib/bpf/libbpf_internal.h +1 -0
@@ -436,6 +436,7 @@ int btf_load_into_kernel(struct btf *btf, char *log_buf, size_t log_sz, __u32 log_level, int token_fd);struct btf *btf_load_from_kernel (__u32 id,struct btf *base_btf,int token_fd); +int libbpf_find_prog_btf_id (const char *name,__u32 attach_prog_fd,int token_fd);struct btf *btf_get_from_fd (int btf_fd,struct btf *base_btf); void btf_get_kernel_prefix_kind(enum bpf_attach_type attach_type,
tools/testing/selftests/bpf/prog_tests/tracing_multi.c +61 -0
71 lines not on this page · raw message
Signature · 1 line
2.55.0
PATCH 12/13 selftests/bpf: Add tracing_multi bpf prog link info test
In reply to Leon HwangVerify the link info works well for bpf prog targets of tracing_multi link.
--- .../selftests/bpf/prog_tests/fill_link_info.c | 170 ++++++++++++++++-- 1 file changed, 152 insertions(+), 18 deletions(-)
tools/testing/selftests/bpf/prog_tests/fill_link_info.c +152 -18
@@ -3,11 +3,13 @@#include <string.h> #include <linux/bpf.h> +#include <linux/kallsyms.h> #include <linux/limits.h> #include <bpf/btf.h> #include <test_progs.h> #include "trace_helpers.h" #include "test_fill_link_info.skel.h" +#include "tracing_multi_bpf.skel.h" #include "bpf/libbpf_internal.h" #define TP_CAT "sched"
@@ -39,8 +41,19 @@ struct tmulti_target {__u64 addr;__u64 cookie;__u32 id; +__u32 func_btf_id; }; +static const char *tmulti_bpf_funcs[] = { +"target_1" , +"target_2" , +}; + +static __u64 tmulti_bpf_fentry_cookies[] = {0xfeed01 ,0xfeed01 }; +static __u64 tmulti_bpf_fexit_cookies[] = {0xfeed02 ,0xfeed02 }; +static __u64 tmulti_bpf_fsession_cookies[] = {0xfeed03 ,0xfeed03 }; +#define TRACING_MULTI_BPF_CNT ARRAY_SIZE(tmulti_bpf_funcs) +#define UPROBE_FILE "/proc/self/exe"static ssize_t uprobe_offset;/* uprobe attach point */
@@ -421,14 +434,28 @@ static int tmulti_target_cmp(const void *a, const void *b)return (ta->id > tb->id) - (ta->id < tb->id); } +static int setup_tmulti_btf_obj_id (const struct bpf_program *prog,__u32 *btf_obj_id) +{ +struct bpf_prog_info prog_info = {}; +__u32 len =sizeof (prog_info); +int err; + + err =bpf_prog_get_info_by_fd (bpf_program__fd (prog), &prog_info, &len); +if (!ASSERT_OK (err,"bpf_prog_get_info_by_fd" )) +return -1 ; +if (!ASSERT_GT (prog_info.attach_btf_obj_id,0 ,"attach_btf_obj_id" )) +return -1 ; + + *btf_obj_id = prog_info.attach_btf_obj_id; +return 0 ; +} +static int setup_tmulti_targets (const struct bpf_program *prog,struct tmulti_target *targets,__u32 *btf_obj_id) { -struct bpf_prog_info prog_info; -__u32 len =sizeof (prog_info);struct btf *btf; -int err, i; +int i;__s32 id; btf =btf__load_vmlinux_btf ();
@@ -446,13 +473,8 @@ static int setup_tmulti_targets(const struct bpf_program *prog, targets[i].id = id; } -memset (&prog_info,0 , len); - err =bpf_prog_get_info_by_fd (bpf_program__fd (prog), &prog_info, &len); -if (!ASSERT_OK (err,"bpf_prog_get_info_by_fd" )) - goto error; -if (!ASSERT_GT (prog_info.attach_btf_obj_id,0 ,"attach_btf_obj_id" )) +if (setup_tmulti_btf_obj_id (prog, btf_obj_id)) goto error; - *btf_obj_id = prog_info.attach_btf_obj_id;/* * The kernel tracing multi attach sorts ids. We sort as well,
@@ -467,13 +489,48 @@ static int setup_tmulti_targets(const struct bpf_program *prog,return -1 ; } +static int setup_tmulti_bpf_prog_targets (const struct bpf_program *prog,int token_fd, +const __u64 *cookies, +const struct bpf_program *const *target_progs, +struct tmulti_target *targets, +__u32 *btf_obj_id) +{ +struct bpf_prog_info prog_info = {}; +__u32 len =sizeof (prog_info), func_btf_id; +int err, i, prog_fd; + +for (i =0 ; i <TRACING_MULTI_BPF_CNT ; i++) { +memset (&prog_info,0 ,sizeof (prog_info)); + len =sizeof (prog_info); + prog_info.jited_ksyms =ptr_to_u64 (&targets[i].addr); + prog_info.nr_jited_ksyms =1 ; + prog_fd =bpf_program__fd (target_progs[i]); + err =bpf_prog_get_info_by_fd (prog_fd, &prog_info, &len); +if (!ASSERT_OK (err,"bpf_prog_get_info_by_fd" )) +return -1 ; + + func_btf_id =libbpf_find_prog_btf_id (tmulti_bpf_funcs[i], prog_fd, token_fd); +if (!ASSERT_GT (func_btf_id,0 ,"libbpf_find_prog_btf_id" )) +return -1 ; + + targets[i].cookie = cookies[i]; + targets[i].id = prog_info.id; + targets[i].func_btf_id = func_btf_id; + } + +return setup_tmulti_btf_obj_id (prog, btf_obj_id); +} +static int verify_tracing_multi_link_info (int fd,const struct bpf_program *prog,const struct tmulti_target *targets, -__u32 btf_obj_id,bool has_cookies) +__u32 btf_obj_id,__u32 count, +bool has_cookies,bool tgt_progs) { +#define TMULTI_CNT (TRACING_MULTI_CNT > TRACING_MULTI_BPF_CNT ? \ + TRACING_MULTI_CNT : TRACING_MULTI_BPF_CNT)enum bpf_attach_type attach_type =bpf_program__expected_attach_type (prog); -__u64 addrs[TRACING_MULTI_CNT ], cookies[TRACING_MULTI_CNT ]; -__u32 ids[TRACING_MULTI_CNT ]; +__u64 addrs[TMULTI_CNT ], cookies[TMULTI_CNT ]; +__u32 ids[TMULTI_CNT ], fids[TMULTI_CNT ];struct bpf_link_info info;__u32 len =sizeof (info);int err, i;
@@ -487,7 +544,9 @@ static int verify_tracing_multi_link_info(int fd, const struct bpf_program *progreturn -1 ;ASSERT_EQ (info.tracing_multi.attach_type, attach_type,"info.tracing_multi.attach_type" ); -ASSERT_EQ (info.tracing_multi.count,TRACING_MULTI_CNT ,"info.tracing_multi.count" ); +ASSERT_EQ (info.tracing_multi.count, count,"info.tracing_multi.count" ); +ASSERT_EQ ((int ) info.tracing_multi.tgt_progs, (int ) tgt_progs, +"info.tracing_multi.tgt_progs" );memset (ids,0 ,sizeof (ids));memset (cookies,0 ,sizeof (cookies));
@@ -496,7 +555,8 @@ static int verify_tracing_multi_link_info(int fd, const struct bpf_program *prog info.tracing_multi.ids =ptr_to_u64 (ids); info.tracing_multi.addrs =ptr_to_u64 (addrs); info.tracing_multi.cookies = has_cookies ?ptr_to_u64 (cookies) :0 ; - info.tracing_multi.count =TRACING_MULTI_CNT ; + info.tracing_multi.func_btf_ids = tgt_progs ?ptr_to_u64 (fids) :0 ; + info.tracing_multi.count = count; err =bpf_link_get_info_by_fd (fd, &info, &len);if (!ASSERT_OK (err,"bpf_link_get_info_by_fd" ))
@@ -506,14 +566,19 @@ static int verify_tracing_multi_link_info(int fd, const struct bpf_program *progreturn -1 ;ASSERT_EQ (info.tracing_multi.attach_type, attach_type,"info.tracing_multi.attach_type" ); -ASSERT_EQ (info.tracing_multi.count,TRACING_MULTI_CNT ,"info.tracing_multi.count" ); +ASSERT_EQ (info.tracing_multi.count, count,"info.tracing_multi.count" );ASSERT_EQ (info.tracing_multi.btf_obj_id, btf_obj_id,"tracing_multi.btf_obj_id" ); +ASSERT_EQ ((int ) info.tracing_multi.tgt_progs, (int ) tgt_progs, +"info.tracing_multi.tgt_progs" ); -for (i =0 ; i <TRACING_MULTI_CNT ; i++) { +for (i =0 ; i < count; i++) {ASSERT_EQ (ids[i], targets[i].id,"tracing_multi.ids" );ASSERT_EQ (cookies[i], has_cookies ? targets[i].cookie :0 ,"tracing_multi.cookies" ); -if (targets[i].addr) { +if (tgt_progs) { +ASSERT_EQ (addrs[i], targets[i].addr,"tracing_multi.addrs" ); +ASSERT_EQ (fids[i], targets[i].func_btf_id,"tracing_multi.func_btf_ids" ); + }else if (targets[i].addr) {struct ksym *ksym;if (!ASSERT_NEQ (addrs[i],0 ,"tracing_multi.addrs" ))
@@ -624,13 +689,78 @@ static void test_tracing_multi_fill_link_info(struct test_fill_link_info *skel,verify_tracing_multi_invalid_user_buffer (link_fd, targets); }else { err =verify_tracing_multi_link_info (link_fd, skel->progs.tmulti_run, - targets, btf_obj_id, has_cookies); + targets, btf_obj_id,TRACING_MULTI_CNT , + has_cookies, false);ASSERT_OK (err,"verify_tracing_multi_link_info" ); }bpf_link__destroy (link); } +static void test_tracing_multi_bpf_fill_link_info_one (struct tracing_multi_bpf *skel, +struct bpf_program *prog, +__u64 *cookies, +bool has_cookies) +{ +LIBBPF_OPTS (bpf_tracing_multi_opts, opts); +const struct bpf_program *target_progs[] = { + skel->progs.target_1, + skel->progs.target_2, + }; +struct tmulti_target targets[TRACING_MULTI_BPF_CNT ] = {}; +int fds[TRACING_MULTI_BPF_CNT ], token_fd, err, i; +struct bpf_link *link; +__u32 btf_obj_id; + + token_fd =bpf_object__token_fd (skel->obj); + token_fd = token_fd <0 ?0 : token_fd; + +if (setup_tmulti_bpf_prog_targets (prog, token_fd, cookies, target_progs, targets, + &btf_obj_id)) +return ; + +for (i =0 ; i <TRACING_MULTI_BPF_CNT ; i++) + fds[i] =bpf_program__fd (target_progs[i]); + + opts.fds = fds; + opts.funcs = tmulti_bpf_funcs; + opts.cnt =ARRAY_SIZE (fds); + opts.cookies = has_cookies ? cookies :NULL ; + + link =bpf_program__attach_tracing_multi (prog,NULL , &opts); +if (!ASSERT_OK_PTR (link,"bpf_program__attach_tracing_multi" )) +return ; + + err =verify_tracing_multi_link_info (bpf_link__fd (link), prog, targets, btf_obj_id, +TRACING_MULTI_BPF_CNT , has_cookies, true); +ASSERT_OK (err,"verify_tracing_multi_link_info" ); + +bpf_link__destroy (link); +} + +static void test_tracing_multi_bpf_fill_link_info (bool has_cookies) +{ +struct tracing_multi_bpf *skel; + +#ifndef __x86_64__ +test__skip (); +return ; +#endif + + skel =tracing_multi_bpf__open_and_load (); +if (!ASSERT_OK_PTR (skel,"tracing_multi_bpf__open_and_load" )) +return ; + +test_tracing_multi_bpf_fill_link_info_one (skel, skel->progs.test_fentry, + tmulti_bpf_fentry_cookies, has_cookies); +test_tracing_multi_bpf_fill_link_info_one (skel, skel->progs.test_fexit, + tmulti_bpf_fexit_cookies, has_cookies); +test_tracing_multi_bpf_fill_link_info_one (skel, skel->progs.test_fsession, + tmulti_bpf_fsession_cookies, has_cookies); + +tracing_multi_bpf__destroy (skel); +} +#define SEC (name) __attribute__((section(name), used)) static short uprobe_link_info_sema_1 SEC(".probes");
@@ -879,6 +1009,10 @@ void test_fill_link_info(void)test_tracing_multi_fill_link_info (skel, false, false);test_tracing_multi_fill_link_info (skel, true, false); } +if (test__start_subtest ("tracing_multi_bpf_link_info" )) { +test_tracing_multi_bpf_fill_link_info (false); +test_tracing_multi_bpf_fill_link_info (true); + }if (test__start_subtest ("tracing_multi_invalid_ubuff" ))test_tracing_multi_fill_link_info (skel, true, true);
Signature · 1 line
2.55.0
PATCH 13/13 selftests/bpf: Test tailcall with fentry.multi
In reply to Leon HwangPositively verify that a tail call from a BPF subprog works when the subprog is traced by fentry.multi.
--- .../selftests/bpf/prog_tests/tailcalls.c | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+)
tools/testing/selftests/bpf/prog_tests/tailcalls.c +62 -0
@@ -15,6 +15,7 @@#include "tailcall_callback.skel.h" #include "tailcall_bpf2bpf2.skel.h" #include "tailcall_bpf2bpf_fexit.skel.h" +#include "tracing_multi_bpf.skel.h" /* test_tailcall_1 checks basic functionality by patching multiple locations * in a single program for a single tail call slot with nop->jmp, jmp->nop
@@ -1953,6 +1954,65 @@ static void test_tailcall_bpf2bpf_fexit_links(void)tailcall_bpf2bpf2__destroy (skel_tc); } +/* +* Verify that a tail call from a BPF subprog works when the subprog is +* traced by fentry.multi. +*/ +static void test_tailcall_bpf2bpf_fentry_multi (void ) +{ +struct tracing_multi_bpf *skel_tracing =NULL ; +LIBBPF_OPTS (bpf_tracing_multi_opts, opts); +const char *funcs[] = {"subprog_tail" }; +struct tailcall_bpf2bpf2 *skel_tc; +struct bpf_link *link =NULL ; +int fds[ARRAY_SIZE (funcs)]; +char buff[128 ] = {}; +int err, key =0 ; +LIBBPF_OPTS (bpf_test_run_opts, topts, + .data_in = buff, + .data_size_in = sizeof(buff), + .repeat = 1, + ); + +#ifndef __x86_64__ +test__skip (); +return ; +#endif + + skel_tc =tailcall_bpf2bpf2__open_and_load (); +if (!ASSERT_OK_PTR (skel_tc,"tailcall_bpf2bpf2__open_and_load" )) +return ; + + skel_tracing =tracing_multi_bpf__open_and_load (); +if (!ASSERT_OK_PTR (skel_tracing,"tracing_multi_bpf__open_and_load" )) + goto out; + + fds[0 ] =bpf_program__fd (skel_tc->progs.classifier_0); + err =bpf_map_update_elem (bpf_map__fd (skel_tc->maps.jmp_table), &key, &fds[0 ],BPF_ANY ); +if (!ASSERT_OK (err,"bpf_map_update_elem" )) + goto out; + + opts.fds = fds; + opts.funcs = funcs; + opts.cnt =ARRAY_SIZE (fds); + link =bpf_program__attach_tracing_multi (skel_tracing->progs.test_fentry,NULL , &opts); +if (!ASSERT_OK_PTR (link,"bpf_program__attach_tracing_multi" )) + goto out; + + err =bpf_prog_test_run_opts (bpf_program__fd (skel_tc->progs.entry), &topts); +if (!ASSERT_OK (err,"bpf_prog_test_run_opts" )) + goto out; + +ASSERT_EQ (topts.retval,1 ,"retval" ); +ASSERT_EQ (skel_tc->bss->count,33 ,"count" ); +ASSERT_EQ (skel_tracing->bss->test_result_fentry,33 ,"fentry.multi count" ); + +out: +bpf_link__destroy (link); +tracing_multi_bpf__destroy (skel_tracing); +tailcall_bpf2bpf2__destroy (skel_tc); +} +void test_tailcalls (void ) {if (test__start_subtest ("tailcall_1" ))
@@ -2022,4 +2082,6 @@ void test_tailcalls(void)test_tailcall_callback ();if (test__start_subtest ("tailcall_bpf2bpf_fexit_links" ))test_tailcall_bpf2bpf_fexit_links (); +if (test__start_subtest ("tailcall_bpf2bpf_fentry_multi" )) +test_tailcall_bpf2bpf_fentry_multi (); }
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH bpf-next 02/13] bpf: Factor out update_fentry_multi helper
In reply to Leon HwangThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The `orig_flags` parameter in the new `update_fentry_multi` helper function is unused. --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The orig_flags parameter in the new update_fentry_multi helper function is unused. --
--- Patch [2]: [PATCH bpf-next 02/13] bpf: Factor out update_fentry_multi helper --- commit c90e2dad924e48bb1043b67addb36ec916a663d1
Author: Leon Hwang <[email protected]>
bpf: Factor out update_fentry_multi helper
This commit factors out common functionality from the register, unregister, and modify multi fentry callbacks into a new update_fentry_multi helper.
kernel/bpf/trampoline.c static void ftrace_hash_add(struct ftrace_hash *hash, struct ftrace_func_entry * · line 1574
Quoted 13 lines
kernel/bpf/trampoline.c +3
-1@@ -1568,47 +1568,41 @@ static void ftrace_hash_add(struct ftrace_hash *hash, struct ftrace_func_entry *add_ftrace_hash_entry (hash, entry); } -static int register_fentry_multi (struct bpf_trampoline *tr,struct bpf_tramp_image *im,void *ptr) +static int update_fentry_multi (struct bpf_trampoline *tr,u32 orig_flags, +struct bpf_tramp_image *im,struct ftrace_hash *hash, +struct bpf_tracing_multi_data *data) {
[Severity: Low]
Is it intended to keep the orig_flags parameter here? It appears to be completely unused in the body of this new helper function.
Quoted 4 lines
kernel/bpf/trampoline.c +1
-2-unsigned long addr = (unsigned long ) im->image; +unsigned long addr = (unsigned long )(im ? im->image : tr->cur_image->image);unsigned long ip =ftrace_location (tr->ip); -struct bpf_tracing_multi_data *data = ptr;
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewRe: [PATCH bpf-next 09/13] selftests/bpf: Add tracing_multi bpf prog attach failure tests
In reply to Leon HwangThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Build failure due to undefined symbol `libbpf_find_prog_btf_id`. --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Build failure due to undefined symbol libbpf_find_prog_btf_id.
--
--- Patch [9]: [PATCH bpf-next 09/13] selftests/bpf: Add tracing_multi bpf prog attach failure tests --- commit c3b104cf9a90f8ddf11f23d2944ad76004fb3150
Author: Leon Hwang <[email protected]>
selftests/bpf: Add tracing_multi bpf prog attach failure tests
This patch adds tests for tracing_multi BPF program attach failures, covering both libbpf and kernel failure modes.
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/tracing_multi.c +0
-0
[ ... ]
tools/testing/selftests/bpf/prog_tests/tracing_multi.c static void test_attach_api_fails(void) · line 804
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/tracing_multi.c +4
-0+/* fail#4 (kernel) duplicate target through different fds */ + token_fd =bpf_object__token_fd (skel->obj); + token_fd = token_fd <0 ?0 : token_fd; + target_id =libbpf_find_prog_btf_id ("target_1" , fds[0 ], token_fd);
[Severity: Low]
Does this code compile successfully? libbpf_find_prog_btf_id is an internal static function in libbpf, so calling it directly from selftests seems like it would cause an implicit function declaration and undefined reference linker error during the build.
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/tracing_multi.c +6
-0+if (!ASSERT_GT (target_id,0 ,"target_1_btf_id" )) + goto cleanup; + + dup_fd =dup (fds[0 ]); +if (!ASSERT_GE (dup_fd,0 ,"dup_target_fd" )) + goto cleanup;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
[email protected]
AI reviewRe: [PATCH bpf-next 07/13] bpf: Add tracing_multi link info support for bpf progs
In reply to Leon HwangThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] Out-of-bounds Read / Uninitialized Memory Leak in sym_name_trim_prefix() - [Medium] UAPI…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Out-of-bounds Read / Uninitialized Memory Leak in sym_name_trim_prefix()
- [Medium] UAPI Anti-Pattern: Usage of C bitfields in UAPI headers
--
--- Patch [7]: [PATCH bpf-next 07/13] bpf: Add tracing_multi link info support for bpf progs --- commit 65386c192a0d673092c95f3f31578ae7c4d19498
Author: Leon Hwang <[email protected]>
Subject: bpf: Add tracing_multi link info support for bpf progs
This commit adds tracing_multi link info support for BPF programs. It updates the bpf_link_info structure and extends bpftool to show function BTF IDs and program IDs.
include/uapi/linux/bpf.h struct bpf_link_info { · line 6894
Quoted 11 lines
include/uapi/linux/bpf.h +2
-1@@ -6890,10 +6890,12 @@ struct bpf_link_info {__u32 attach_type;__u32 count;/* in/out: tracing_multi target count */ __u32 btf_obj_id; - __u32 :32 ; + __u32 tgt_progs:1; + __u32 :31 ;
[Severity: Medium]
Could this cause endianness issues since C bitfield layout is implementation-defined?
The established Linux UAPI standard is to use a __u32 flags field with bitmask macros instead of C bitfields to ensure portability across architectures and cross-platform tools. (This also applies to the mirrored header in tools/include/uapi/linux/bpf.h).
[ ... ]
Quoted 4 lines
tools/bpf/bpftool/link.c +0
-0
[ ... ]
Quoted 6 lines
tools/bpf/bpftool/link.c +6
-0+#define BPF_PROG_PREFIX "bpf_prog_" +#define BPF_PROG_PREFIX_LEN (sizeof(BPF_PROG_PREFIX) - 1) + +static const char * +sym_name_trim_prefix (struct kernel_sym *sym,bool is_prog) +{
tools/bpf/bpftool/link.c show_uprobe_multi_json(struct bpf_link_info *info, json_writer_t *wtr) · line 440
Quoted 10 lines
tools/bpf/bpftool/link.c +10
-0+int prefix_len; + +if (!is_prog) +return sym->name; + +/* Ref kernel/bpf/core.c::bpf_prog_ksym_set_name() */ + prefix_len =BPF_PROG_PREFIX_LEN ; + prefix_len +=BPF_TAG_SIZE *2 ; + prefix_len +=1 ;/* skip the '_' */ +return sym->name + prefix_len;
[Severity: Medium]
Does this code read past the null terminator for unnamed BPF programs?
When processing an unnamed BPF program (e.g., exported in kallsyms as bpf_prog_9a7f9e3ab6f138ab), the sym->name buffer contains exactly 25 characters. Here, prefix_len is calculated as 26 (9 + 16 + 1).
This offset advances the pointer past the null terminator into uninitialized heap memory, which is then passed to the JSON string writer when calling jsonw_string_field() later in show_tracing_multi_json().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
[email protected]
AI reviewRe: [PATCH bpf-next 11/13] selftests/bpf: Add tracing_multi bpf prog rollback test
In reply to Leon HwangThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The `test_rollback_bpf_prog` test hardcodes executing `target_1` to verify rollback behavior…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The test_rollback_bpf_prog test hardcodes executing target_1 to verify rollback behavior, leading to spurious test failures if target_2 happens to have a lower file descriptor. --
--- Patch [11]: [PATCH bpf-next 11/13] selftests/bpf: Add tracing_multi bpf prog rollback test --- commit b065372f3722241a5632ef72df356c018b778825
Author: Leon Hwang <[email protected]>
selftests/bpf: Add tracing_multi bpf prog rollback test
This commit adds a test to verify that prepared trampolines are correctly reverted when a BPF program multi-attach fails and triggers a rollback.
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/tracing_multi.c +0
-0
[ ... ]
Quoted 1 line
tools/testing/selftests/bpf/prog_tests/tracing_multi.c +0
-0@@ -377,6 +377,65 @@ static void test_cookies_bpf_prog(void)
[ ... ]
Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/tracing_multi.c +3
-0+static void test_rollback_bpf_prog (void ) +{ +int token_fd, prog_fd, fd =-1 , pre_fd =-1 ;
[ ... ]
Quoted 1 line
tools/testing/selftests/bpf/prog_tests/tracing_multi.c +1
-0+ first = target_fds[0 ] < target_fds[1 ] ?0 :1 ;
[ ... ]
tools/testing/selftests/bpf/prog_tests/tracing_multi.c static void test_cookies_bpf_prog(void) · line 427
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/tracing_multi.c +5
-0+ fd =bpf_link_create (prog_fd,0 ,BPF_TRACE_FENTRY_MULTI , &lopts); +if (!ASSERT_EQ (fd, -EBUSY ,"bpf_link_create" )) + goto cleanup; + +if (run_bpf_target (skel->progs.target_1, first +1 ,"rollback_target" ))
[Severity: Medium]
Does this test accidentally hardcode the execution of target_1 while expecting a dynamic return value based on file descriptor ordering?
If the file descriptors are assigned such that target_fds[1] < target_fds[0], the variable first becomes 1. The test then executes target_1 but expects a return value of first + 1 (which is 2).
Since target_1 always returns 1, will this cause a spurious test failure?
Additionally, if first == 1, target_1 would still have a trampoline attached via pre_fd. This would cause test_result_fentry to increment and fail the subsequent ASSERT_EQ() check.
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/tracing_multi.c +4
-0+ goto cleanup; +ASSERT_EQ (skel->bss->test_result_fentry,0 ,"test_result_fentry" ); + +cleanup:
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
[email protected]
AI reviewRe: [PATCH bpf-next 05/13] libbpf: Add tracing_multi link support for bpf progs
In reply to Leon HwangThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Missing input validation in `bpf_program__attach_tracing_multi` leads to a potential…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Missing input validation in bpf_program__attach_tracing_multi leads to a potential segmentation fault when a user-provided array element is NULL. --
--- Patch [5]: [PATCH bpf-next 05/13] libbpf: Add tracing_multi link support for bpf progs --- commit bef5c103ea1916738efc32c272265795c04de90a
Author: Leon Hwang <[email protected]>
libbpf: Add tracing_multi link support for bpf progs
This commit introduces tracing_multi link support for bpf programs in libbpf. It adds fds and funcs fields to bpf_tracing_multi_opts to specify target programs, translating the function names to BTF IDs before passing them to the kernel.
Quoted 4 lines
tools/lib/bpf/libbpf.c +0
-0
[ ... ]
Quoted 4 lines
tools/lib/bpf/libbpf.c +0
-0@@ -12720,11 +12732,28 @@ bpf_program__attach_tracing_multi(const struct bpf_program *prog, const char *paif (cnt ==0 )return libbpf_err_ptr (-EINVAL ); ids = (const __u32 *) free_ids;
tools/lib/bpf/libbpf.c bpf_program__attach_tracing_multi(const struct bpf_program *prog, const char *pa · line 12744
Quoted 10 lines
tools/lib/bpf/libbpf.c +10
-0+ }else if (fds) { +size_t cap =0 ; +int i; + + err =libbpf_ensure_mem ((void **) &free_ids, &cap,sizeof (*free_ids), cnt); +if (err) +return libbpf_err_ptr (err); + +for (i =0 ; i < cnt; i++) { + err =libbpf_find_prog_btf_id (funcs[i], fds[i], prog->obj->token_fd);
[Severity: Medium]
Could funcs[i] be NULL here if the user-space application provided an invalid string pointer in the array?
If it is NULL, this pointer gets passed down through libbpf_find_prog_btf_id() and eventually reaches btf_find_by_name_kind(), which performs this check:
tools/lib/bpf/btf.c:btf_find_by_name_kind() {
...
if (kind == BTF_KIND_UNKN || strcmp(type_name, "void") == 0)
...
}Calling strcmp() with a NULL type_name will cause a segmentation fault in the application. Is it worth validating that funcs[i] is not NULL to safely return an error code instead of crashing?
Quoted 7 lines
tools/lib/bpf/libbpf.c +6
-0+if (err <0 ) + goto error; + + free_ids[i] = err; + } + ids = (const __u32 *) free_ids; }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
[email protected]
AI reviewRe: [PATCH bpf-next 12/13] selftests/bpf: Add tracing_multi bpf prog link info test
In reply to Leon HwangThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] Missing array sorting in user-space targets leads to flaky test - [Medium] Uninitialized stack…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Missing array sorting in user-space targets leads to flaky test
- [Medium] Uninitialized stack variable fids passed to kernel
--
--- Patch [12]: [PATCH bpf-next 12/13] selftests/bpf: Add tracing_multi bpf prog link info test --- commit 5ee3239754522a052ad0eb2cf3e2485319bff581
Author: Leon Hwang <[email protected]>
selftests/bpf: Add tracing_multi bpf prog link info test
Verify the link info works well for bpf prog targets of tracing_multi link.
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/fill_link_info.c +0
-0
[ ... ]
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/fill_link_info.c +6
-0@@ -467,13 +489,48 @@ static int setup_tmulti_targets(const struct bpf_program *prog,return -1 ; } +static int setup_tmulti_bpf_prog_targets (const struct bpf_program *prog,int token_fd, +const __u64 *cookies, +const struct bpf_program *const *target_progs, +struct tmulti_target *targets, +__u32 *btf_obj_id) +{
[ ... ]
tools/testing/selftests/bpf/prog_tests/fill_link_info.c static int setup_tmulti_targets(const struct bpf_program *prog, · line 522
Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/fill_link_info.c +7
-0+ targets[i].cookie = cookies[i]; + targets[i].id = prog_info.id; + targets[i].func_btf_id = func_btf_id; + } + +return setup_tmulti_btf_obj_id (prog, btf_obj_id); +}
[Severity: Medium]
Will this lead to flaky tests? The kernel sorts the tracing_multi targets by a computed key during attachment.
When returning link info, the arrays are returned in this sorted order, and verify_tracing_multi_link_info() asserts that they perfectly match the targets array at the exact same indexes.
However, unlike setup_tmulti_targets(), this function does not call qsort() on the targets array. If ID allocation wraps around or another process allocates IDs concurrently, this might fail non-deterministically.
Quoted 17 lines
tools/testing/selftests/bpf/prog_tests/fill_link_info.c +7
-3+static int verify_tracing_multi_link_info (int fd,const struct bpf_program *prog,const struct tmulti_target *targets, -__u32 btf_obj_id,bool has_cookies) +__u32 btf_obj_id,__u32 count, +bool has_cookies,bool tgt_progs) { +#define TMULTI_CNT (TRACING_MULTI_CNT > TRACING_MULTI_BPF_CNT ? \ + TRACING_MULTI_CNT : TRACING_MULTI_BPF_CNT)enum bpf_attach_type attach_type =bpf_program__expected_attach_type (prog); -__u64 addrs[TRACING_MULTI_CNT ], cookies[TRACING_MULTI_CNT ]; -__u32 ids[TRACING_MULTI_CNT ]; +__u64 addrs[TMULTI_CNT ], cookies[TMULTI_CNT ]; +__u32 ids[TMULTI_CNT ], fids[TMULTI_CNT ];struct bpf_link_info info;__u32 len =sizeof (info);int err, i;
[ ... ]
tools/testing/selftests/bpf/prog_tests/fill_link_info.c static int verify_tracing_multi_link_info(int fd, const struct bpf_program *prog · line 561
Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/fill_link_info.c +2
-1memset (ids,0 ,sizeof (ids));memset (cookies,0 ,sizeof (cookies));@@ -496,7 +555,8 @@ static int verify_tracing_multi_link_info(int fd, const struct bpf_program *prog info.tracing_multi.ids =ptr_to_u64 (ids); info.tracing_multi.addrs =ptr_to_u64 (addrs); info.tracing_multi.cookies = has_cookies ?ptr_to_u64 (cookies) :0 ; - info.tracing_multi.count =TRACING_MULTI_CNT ; + info.tracing_multi.func_btf_ids = tgt_progs ?ptr_to_u64 (fids) :0 ; + info.tracing_multi.count = count; err =bpf_link_get_info_by_fd (fd, &info, &len);
[Severity: Medium]
Does this code leave the fids array uninitialized?
The other stack arrays (ids, cookies, addrs) are explicitly cleared with memset() before being passed to the bpf_link_get_info_by_fd() syscall, but fids is not. This could potentially cause issues with uninitialized memory.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] A text poke failure during the detachment of a `tracing_multi` link from BPF programs causes a…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] A text poke failure during the detachment of a tracing_multi link from BPF programs causes a Use-After-Free of the trampoline executable image and a double free.
- [Medium] Trampoline flags are not rolled back when __bpf_trampoline_link_prog fails during bpf_trampoline_multi_attach, permanently corrupting the state of the shared trampoline. --
--- Patch [4]: [PATCH bpf-next 04/13] bpf: Add tracing_multi link support for bpf progs --- commit b93dc01dd11e0331789988b67d9503ac3b565c51
Author: Leon Hwang <[email protected]>
bpf: Add tracing_multi link support for bpf progs
Similar to the tracing_multi link support for kernel functions, add support for bpf progs by reusing the 'ids' fields as BTF ids, and adding fds array to indicate the target bpf prog. Then, for each pair of fd+id, they will be the attach targets of tracing_multi link.
Quoted 16 lines
kernel/bpf/trampoline.c +4
-3@@ -1572,12 +1572,23 @@ static int update_fentry_multi(struct bpf_trampoline *tr, u32 orig_flags, struct bpf_tramp_image *im, struct ftrace_hash *hash, struct bpf_tracing_multi_data *data) { - unsigned long addr = (unsigned long)(im ? im->image : tr->cur_image->image); + if (tr->func.ftrace_managed) { + unsigned long addr = (unsigned long)(im ? im->image : tr->cur_image->image); -if (bpf_trampoline_use_jmp (tr->flags)) - addr =ftrace_jmp_set (addr); +if (bpf_trampoline_use_jmp (tr->flags)) + addr =ftrace_jmp_set (addr);
kernel/bpf/trampoline.c static int update_fentry_multi(struct bpf_trampoline *tr, u32 orig_flags, · line 1589
Quoted 10 lines
kernel/bpf/trampoline.c +10
-0+ +ftrace_hash_add (hash, data->entry, tr->ip, addr); + }else { +void *old_addr = tr->cur_image ? tr->cur_image->image :NULL ; +void *new_addr = im ? im->image :NULL ; +int ret; + + ret =bpf_trampoline_update_fentry (tr, orig_flags, old_addr, new_addr); +if (ret) +return ret;
[Severity: High]
If bpf_trampoline_update_fentry() fails here, we return the error and bypass updating tr->cur_image. In bpf_trampoline_multi_detach(), if __bpf_trampoline_unlink_prog() fails, it logs a warning but continues and unconditionally calls bpf_trampoline_multi_attach_free().
Since tr->cur_image wasn't updated, does bpf_trampoline_multi_attach_free() end up freeing the currently active executable image, leading to a use-after-free and a double free on the next attach/detach?
Quoted 6 lines
kernel/bpf/trampoline.c +1
-1+ } -ftrace_hash_add (hash, data->entry, tr->ip, addr); tr->cur_image = im;return 0 ; }
[ ... ]
kernel/bpf/trampoline.c int bpf_trampoline_multi_attach(struct bpf_prog *prog, u32 *ids, · line 1720
Quoted 7 lines
kernel/bpf/trampoline.c +3
-0@@ -1691,6 +1715,9 @@ int bpf_trampoline_multi_attach(struct bpf_prog *prog, u32 *ids,for_each_mnode (mnode, link) {bpf_trampoline_multi_attach_init (mnode->trampoline); +if (progs && progs[i]->aux->tail_call_reachable) + mnode->trampoline->flags |=BPF_TRAMP_F_TAIL_CALL_CTX ; +
[Severity: Medium]
If __bpf_trampoline_link_prog() fails below, the error handler sets rollback_cnt = i and skips rolling back the current node i.
Does this leave the BPF_TRAMP_F_TAIL_CALL_CTX flag permanently set on the trampoline?
Quoted 3 lines
kernel/bpf/trampoline.c +0
-0data->entry = &mnode->entry; err =__bpf_trampoline_link_prog (&mnode->node, mnode->trampoline,NULL , &trampoline_multi_ops, data);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
[email protected]
AI reviewRe: [PATCH bpf-next 10/13] selftests/bpf: Add tracing_multi bpf prog cookie test
In reply to Leon HwangA subsystem pattern flags this as potentially concerning: every per-target cookie array holds the same value twice. The test on the BPF side compares against a single scalar global set from element…
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/tracing_multi.c +0
-0
tools/testing/selftests/bpf/prog_tests/tracing_multi.c static int run_bpf_target(struct bpf_program *prog, __u32 retval, const char *na · line 312
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/tracing_multi.c +4
-1@@ -304,9 +304,12 @@ static int run_bpf_target(struct bpf_program *prog, __u32 retval, const char *nareturn 0 ; } -static void test_link_api_bpf_prog (void ) +static void __test_link_api_bpf_prog (bool test_cookies) {const char *funcs[] = {"target_1" ,"target_2" }; +__u64 fentry_cookies[] = {0xfeed01 ,0xfeed01 }; +__u64 fexit_cookies[] = {0xfeed02 ,0xfeed02 }; +__u64 fsession_cookies[] = {0xfeed03 ,0xfeed03 };
A subsystem pattern flags this as potentially concerning: every per-target cookie array holds the same value twice. The test on the BPF side compares against a single scalar global set from element [0]:
skel->bss->fentry_cookie = fentry_cookies[0];
and in progs/tracing_multi_bpf.c:
if (!test_cookies || bpf_get_attach_cookie(ctx) == fentry_cookie)
test_result_fentry++;Since cookie[0] == cookie[1] for all three arrays, the test cannot distinguish a correct cookie-to-target mapping from a permuted one. The assertions (test_result_fentry == 2, etc.) succeed as long as some correct-looking cookie value is delivered on both invocations, but swapping the cookies of target_1 and target_2 in the kernel is invisible.
That matters because the kernel path this commit exercises is exactly a
permutation: bpf_tracing_multi_attach() calls check_dup_keys() which calls keys_swap_r() in kernel/trace/bpf_trace.c to reorder the cookies, ids, and progs arrays to follow the sorted trampoline keys:
id_a = ids + (key_a - keys);
if (data->progs) { ... swap(*prog_a, *prog_b); }
if (cookies) { cookie_a = cookies + (id_a - ids); ... swap(*cookie_a, *cookie_b); }An indexing mistake in that swap could desynchronise cookies from progs/ids, yet the test would still pass. Since fds[]/ids[] come from BPF prog FDs, the sort order is not the array order the caller supplied.
The neighbouring test for kernel-function targets in this same file already does this correctly: bpf_fentry_test_cookies[] uses ten distinct values deliberately out of order (8, 9, 7, 5, 4, 2, 3, 1, 10, 6) and progs/tracing_multi_check.c checks the cookie per-IP with something like:
err |= test_cookies ? cookie != 8 : 0;
under ip == &bpf_fentry_test1.
Can the test use distinct cookies and a bpf_get_func_ip()-based per-target expectation so it is sensitive to cookie mis-association?
Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/tracing_multi.c +0
-0LIBBPF_OPTS (bpf_tracing_multi_opts, opts);struct tracing_multi_bpf *skel =NULL ;int fds[ARRAY_SIZE (funcs)];
[ ... ]
tools/testing/selftests/bpf/prog_tests/tracing_multi.c static void test_link_api_bpf_prog(void) · line 378
Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/tracing_multi.c +9
-0@@ -356,6 +367,16 @@ static void test_link_api_bpf_prog(void)tracing_multi_bpf__destroy (skel); } +static void test_link_api_bpf_prog (void ) +{ +__test_link_api_bpf_prog (false); +} + +static void test_cookies_bpf_prog (void ) +{ +__test_link_api_bpf_prog (true); +}
This isn't a bug, but would calling __test_link_api_bpf_prog(false) and __test_link_api_bpf_prog(true) straight from the two subtest arms read more consistently? The neighbouring test_link_api_ids() is dispatched directly as test_link_api_ids(false) and test_link_api_ids(true) just a dozen lines down, without wrapper functions.
[ ... ]
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/31320427087
[email protected]
AI reviewRe: [PATCH bpf-next 06/13] bpf: Add tracing_multi link fdinfo support for bpf progs
In reply to Leon HwangThis isn't a bug, but since bpf_trampoline_unpack_key() only masks off bit 31, which is never set for prog targets, does this re-assignment change anything, or could the unpacked value be used…
Quoted 31 lines
kernel/trace/bpf_trace.c +13
-0@@ -3759,26 +3759,46 @@ static int bpf_tracing_multi_link_fill_link_info(const struct bpf_link *link, }#ifdef CONFIG_PROC_FS +static const char *bpf_prog_func_name (struct bpf_prog *prog,u32 btf_id) +{ +const struct btf *btf = prog->aux->btf; +const struct btf_type *t; + + t =btf_type_by_id (btf, btf_id); +if (!t || !btf_type_is_func (t)) +return "" ; + +return btf_name_by_offset (btf, t->name_off); +} +static void bpf_tracing_multi_show_fdinfo (const struct bpf_link *link,struct seq_file *seq) {struct bpf_tracing_multi_link *tr_link = container_of(link, struct bpf_tracing_multi_link, link);bool has_cookies = !!tr_link->cookies; +bool has_progs = !!tr_link->progs;seq_printf (seq,"attach_type:\t%u\n" , tr_link->link.attach_type);seq_printf (seq,"cnt:\t%u\n" , tr_link->nodes_cnt);
kernel/trace/bpf_trace.c static int bpf_tracing_multi_link_fill_link_info(const struct bpf_link *link, · line 3793
Quoted 10 lines
kernel/trace/bpf_trace.c +4
-1-seq_printf (seq,"%s\t %s\t %s\t %s\n" ,"obj-id" ,"btf-id" ,"cookie" ,"func" ); +seq_printf (seq,"%s\t %s\t %s\t %s\n" , has_progs ?"prog-id" :"obj-id" ,"btf-id" ,"cookie" , +"func" );for (int i =0 ; i < tr_link->nodes_cnt; i++) {struct bpf_tracing_multi_node *mnode = &tr_link->nodes[i]; +u64 cookie = has_cookies ? tr_link->cookies[i] :0 ;u32 btf_id, obj_id;bpf_trampoline_unpack_key (mnode->trampoline->key, &obj_id, &btf_id); + btf_id = has_progs ? (u32 ) mnode->trampoline->key : btf_id;
^^^^
This isn't a bug, but since bpf_trampoline_unpack_key() only masks off bit 31, which is never set for prog targets, does this re-assignment change anything, or could the unpacked value be used directly?
bpf_trampoline_unpack_key() already sets btf_id = key & 0x7FFFFFFF, and bit 31 is only set by the !tgt_prog branch of bpf_trampoline_compute_key() (the kernel-BTF case).
In the has_progs case the key was built as:
((u64)tgt_prog->aux->id << 32) | ids[i]
and ids[i] must be a valid BTF type id (<= BTF_MAX_TYPE, 0xfffff) or bpf_check_attach_target() would have rejected the attach, so (u32)key and the unpacked btf_id are always equal here.
kernel/trace/bpf_trace.c static int bpf_tracing_multi_link_fill_link_info(const struct bpf_link *link, · line 3804
Quoted 11 lines
kernel/trace/bpf_trace.c +8
-0+if (has_progs) +seq_printf (seq,"%u\t %u\t %llu\t %s\n" , + obj_id, btf_id, cookie, +bpf_prog_func_name (tr_link->progs[i], btf_id)); +else +seq_printf (seq,"%u\t %u\t %llu\t %pS\n" , + obj_id, btf_id, cookie, + (void *) mnode->trampoline->ip);cond_resched (); }
The commit message shows the sample fdinfo output but doesn't say much about the implementation or why this change is needed. In particular:
Quoted 16 lines
Show the target bpf prog info in the fdinfo.
$ cat /proc/self/fdinfo/13 pos: 0 flags: 02000000 mnt_id: 18 ino: 1045 link_type: tracing_multi link_id: 9 prog_tag: 9a7f9e3ab6f138ab prog_id: 25 attach_type: 61 cnt: 2 prog-id btf-id cookie func 21 4 16706819 target_1 22 5 16706819 target_2
This isn't a bug, but could the changelog say why the trampoline ip is not usable as the func column for prog targets (tr->ip is 0 for JIT'd prog addresses), rather than mostly showing the resulting output? It also doesn't mention that the first column header changes from "obj-id" to "prog-id" for prog-target links.
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/31320427087
On Sun, Aug 09, 2026 at 11:01:02PM +0800, Leon Hwang wrote:
SNIP
Quoted 17 lines
kernel/bpf/trampoline.c +5
-3@@ -1572,12 +1572,23 @@ static int update_fentry_multi(struct bpf_trampoline *tr, u32 orig_flags, struct bpf_tramp_image *im, struct ftrace_hash *hash, struct bpf_tracing_multi_data *data) { - unsigned long addr = (unsigned long)(im ? im->image : tr->cur_image->image); + if (tr->func.ftrace_managed) { + unsigned long addr = (unsigned long)(im ? im->image : tr->cur_image->image); -if (bpf_trampoline_use_jmp (tr->flags)) - addr =ftrace_jmp_set (addr); +if (bpf_trampoline_use_jmp (tr->flags)) + addr =ftrace_jmp_set (addr); +
kernel/bpf/trampoline.c static int update_fentry_multi(struct bpf_trampoline *tr, u32 orig_flags, · line 1590
Quoted 10 lines
kernel/bpf/trampoline.c +10
-0+ftrace_hash_add (hash, data->entry, tr->ip, addr); + }else { +void *old_addr = tr->cur_image ? tr->cur_image->image :NULL ; +void *new_addr = im ? im->image :NULL ; +int ret; + + ret =bpf_trampoline_update_fentry (tr, orig_flags, old_addr, new_addr); +if (ret) +return ret; + }
hum, so IIUC this sequentially attaches to target bpf program as it would with current API, so there's no attachent speedup, right?
Quoted 57 lines
kernel/bpf/trampoline.c +18
-6-ftrace_hash_add (hash, data->entry, tr->ip, addr); tr->cur_image = im;return 0 ; }@@ -1627,6 +1638,18 @@ static void bpf_trampoline_multi_attach_free(struct bpf_trampoline *tr)static void bpf_trampoline_multi_attach_rollback (struct bpf_trampoline *tr) { +if (!tr->func.ftrace_managed) { +void *failed_addr = tr->cur_image ? tr->cur_image->image :NULL ; +void *old_addr = tr->multi_attach.old_image ? + tr->multi_attach.old_image->image :NULL ; +u32 orig_flags = tr->flags; +int ret; + + tr->flags = tr->multi_attach.old_flags; + ret =bpf_trampoline_update_fentry (tr, orig_flags, failed_addr, old_addr); +WARN_ONCE (ret,"bpf_trampoline_update_fentry failed: %d\n" , ret); + } +if (tr->cur_image)bpf_tramp_image_put (tr->cur_image); tr->cur_image = tr->multi_attach.old_image;@@ -1643,6 +1666,7 @@ static void bpf_trampoline_multi_attach_rollback(struct bpf_trampoline *tr)for_each_mnode_cnt (mnode, link, link->nodes_cnt) int bpf_trampoline_multi_attach(struct bpf_prog *prog, u32 *ids, + u64 *keys, struct bpf_prog **progs, struct bpf_tracing_multi_link *link) { struct bpf_tracing_multi_data *data = &link->data;@@ -1651,18 +1675,18 @@ int bpf_trampoline_multi_attach(struct bpf_prog *prog, u32 *ids,struct bpf_tracing_multi_node *mnode;struct bpf_trampoline *tr;int i, err, rollback_cnt; -u64 key;for_each_mnode (mnode, link) { rollback_cnt = i; - err =bpf_check_attach_btf_id_multi (btf, prog, ids[i], &tgt_info); +if (progs) + err =bpf_check_attach_target (NULL , prog, progs[i], ids[i], &tgt_info); +else + err =bpf_check_attach_btf_id_multi (btf, prog, ids[i], &tgt_info);if (err) goto rollback_put; - key =bpf_trampoline_compute_key (NULL , btf, ids[i]); - - tr =bpf_trampoline_get (key, &tgt_info); + tr =bpf_trampoline_get (keys[i], &tgt_info);if (!tr) { err = -ENOMEM ; goto rollback_put;
kernel/bpf/trampoline.c int bpf_trampoline_multi_attach(struct bpf_prog *prog, u32 *ids, · line 1723
Quoted 9 lines
kernel/bpf/trampoline.c +3
-0@@ -1691,6 +1715,9 @@ int bpf_trampoline_multi_attach(struct bpf_prog *prog, u32 *ids,for_each_mnode (mnode, link) {bpf_trampoline_multi_attach_init (mnode->trampoline); +if (progs && progs[i]->aux->tail_call_reachable) + mnode->trampoline->flags |=BPF_TRAMP_F_TAIL_CALL_CTX ; + data->entry = &mnode->entry; err =__bpf_trampoline_link_prog (&mnode->node, mnode->trampoline,NULL , &trampoline_multi_ops, data);
IIUC for bpf_program targets the actuall attachment is happening in here, right?
the rest of the function logic won't execute, because there won't be any data in the reg/noreg/mod hashes..?
you seem to use the tracing_multi API to ease up attachment to multiple bpf programs and end up with just single link fd for all attachments
I think it'd be cleaner to have separate api or code paths for that
jirka
Quoted 26 lines · Jiri Olsa
On Sun, Aug 09, 2026 at 11:01:02PM +0800, Leon Hwang wrote:
SNIP
Quoted 22 lines
kernel/bpf/trampoline.c +10
-3@@ -1572,12 +1572,23 @@ static int update_fentry_multi(struct bpf_trampoline *tr, u32 orig_flags, struct bpf_tramp_image *im, struct ftrace_hash *hash, struct bpf_tracing_multi_data *data) { - unsigned long addr = (unsigned long)(im ? im->image : tr->cur_image->image); + if (tr->func.ftrace_managed) { + unsigned long addr = (unsigned long)(im ? im->image : tr->cur_image->image); -if (bpf_trampoline_use_jmp (tr->flags)) - addr =ftrace_jmp_set (addr); +if (bpf_trampoline_use_jmp (tr->flags)) + addr =ftrace_jmp_set (addr); + +ftrace_hash_add (hash, data->entry, tr->ip, addr); + } else { +void *old_addr = tr->cur_image ? tr->cur_image->image :NULL ; +void *new_addr = im ? im->image :NULL ; +int ret;
Quoted 8 lines
kernel/bpf/trampoline.c static int update_fentry_multi(struct bpf_trampoline *tr, u32 orig_flags, · line 1590
Quoted 5 lines
kernel/bpf/trampoline.c +5
-0+ + ret =bpf_trampoline_update_fentry (tr, orig_flags, old_addr, new_addr); +if (ret) +return ret; + }hum, so IIUC this sequentially attaches to target bpf program as it would with current API, so there's no attachent speedup, right?
Correct.
Quoted 61 lines
Quoted 61 lines
kernel/bpf/trampoline.c +18
-6-ftrace_hash_add (hash, data->entry, tr->ip, addr); tr->cur_image = im;return 0 ; }@@ -1627,6 +1638,18 @@ static void bpf_trampoline_multi_attach_free(struct bpf_trampoline *tr)static void bpf_trampoline_multi_attach_rollback (struct bpf_trampoline *tr) { +if (!tr->func.ftrace_managed) { +void *failed_addr = tr->cur_image ? tr->cur_image->image :NULL ; +void *old_addr = tr->multi_attach.old_image ? + tr->multi_attach.old_image->image :NULL ; +u32 orig_flags = tr->flags; +int ret; + + tr->flags = tr->multi_attach.old_flags; + ret =bpf_trampoline_update_fentry (tr, orig_flags, failed_addr, old_addr); +WARN_ONCE (ret,"bpf_trampoline_update_fentry failed: %d\n" , ret); + } +if (tr->cur_image)bpf_tramp_image_put (tr->cur_image); tr->cur_image = tr->multi_attach.old_image;@@ -1643,6 +1666,7 @@ static void bpf_trampoline_multi_attach_rollback(struct bpf_trampoline *tr)for_each_mnode_cnt (mnode, link, link->nodes_cnt) int bpf_trampoline_multi_attach(struct bpf_prog *prog, u32 *ids, + u64 *keys, struct bpf_prog **progs, struct bpf_tracing_multi_link *link) { struct bpf_tracing_multi_data *data = &link->data;@@ -1651,18 +1675,18 @@ int bpf_trampoline_multi_attach(struct bpf_prog *prog, u32 *ids,struct bpf_tracing_multi_node *mnode;struct bpf_trampoline *tr;int i, err, rollback_cnt; -u64 key;for_each_mnode (mnode, link) { rollback_cnt = i; - err =bpf_check_attach_btf_id_multi (btf, prog, ids[i], &tgt_info); +if (progs) + err =bpf_check_attach_target (NULL , prog, progs[i], ids[i], &tgt_info); +else + err =bpf_check_attach_btf_id_multi (btf, prog, ids[i], &tgt_info);if (err) goto rollback_put; - key =bpf_trampoline_compute_key (NULL , btf, ids[i]); - - tr =bpf_trampoline_get (key, &tgt_info); + tr =bpf_trampoline_get (keys[i], &tgt_info);if (!tr) { err = -ENOMEM ; goto rollback_put;@@ -1691,6 +1715,9 @@ int bpf_trampoline_multi_attach(struct bpf_prog *prog, u32 *ids,for_each_mnode (mnode, link) {bpf_trampoline_multi_attach_init (mnode->trampoline);
Quoted 9 lines
kernel/bpf/trampoline.c int bpf_trampoline_multi_attach(struct bpf_prog *prog, u32 *ids, · line 1723
Quoted 6 lines
kernel/bpf/trampoline.c +3
-0+if (progs && progs[i]->aux->tail_call_reachable) + mnode->trampoline->flags |=BPF_TRAMP_F_TAIL_CALL_CTX ; + data->entry = &mnode->entry; err =__bpf_trampoline_link_prog (&mnode->node, mnode->trampoline,NULL , &trampoline_multi_ops, data);IIUC for
bpf_programtargets the actuall attachment is happening in here, right?
Right.
Quoted 9 lines
the rest of the function logic won't execute, because there won't be any data in the reg/noreg/mod hashes..?
you seem to use the
tracing_multiAPI to ease up attachment to multiple bpf programs and end up with just single link fd for all attachmentsI think it'd be cleaner to have separate api or code paths for that
Makes sense.
I'd like to use the same UAPI, and re-implement it with another code path, mainly in trampoline.c. And, the re-implementation will get rid of HAVE_SINGLE_FTRACE_DIRECT_OPS for targeting bpf progs at the same time.
Then, I think I can speed up the attachment using text-poke in batch on x86_64.
Thanks,
Leon
Re: [PATCH bpf-next 02/13] bpf: Factor out update_fentry_multi helper
In reply to [email protected]Quoted 21 lines · On 9/8/26 23:14, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Theorig_flagsparameter in the newupdate_fentry_multihelper function is unused. ----- Patch [2]: [PATCH bpf-next 02/13] bpf: Factor out
update_fentry_multihelper --- commitc90e2dad924e48bb1043b67addb36ec916a663d1
Author: Leon Hwang <[email protected]>bpf: Factor out
update_fentry_multihelperThis commit factors out common functionality from the register, unregister, and modify multi fentry callbacks into a new
update_fentry_multihelper.Quoted 8 lines
kernel/bpf/trampoline.c +0
-0@@ -1568,47 +1568,41 @@ static void ftrace_hash_add(struct ftrace_hash *hash, struct ftrace_func_entry *add_ftrace_hash_entry (hash, entry); }
Quoted 10 lines
kernel/bpf/trampoline.c static void ftrace_hash_add(struct ftrace_hash *hash, struct ftrace_func_entry * · line 1574
Quoted 5 lines
kernel/bpf/trampoline.c +3
-1-static int register_fentry_multi (struct bpf_trampoline *tr,struct bpf_tramp_image *im,void *ptr) +static int update_fentry_multi (struct bpf_trampoline *tr,u32 orig_flags, +struct bpf_tramp_image *im,struct ftrace_hash *hash, +struct bpf_tracing_multi_data *data) {[Severity: Low]
Is it intended to keep theorig_flagsparameter here? It appears to be completely unused in the body of this new helper function.
Yes, it was intended.
However, will drop orig_flags when re-implementing tracing_multi link support for bpf progs with another code path.
Thanks,
Leon
Quoted 49 lines · On 9/8/26 23:33, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] A text poke failure during the detachment of atracing_multilink from BPF programs causes a Use-After-Free of the trampoline executable image and a double free.
- [Medium] Trampoline flags are not rolled back when__bpf_trampoline_link_progfails duringbpf_trampoline_multi_attach, permanently corrupting the state of the shared trampoline. ----- Patch [4]: [PATCH bpf-next 04/13] bpf: Add
tracing_multilink support for bpf progs --- commitb93dc01dd11e0331789988b67d9503ac3b565c51
Author: Leon Hwang <[email protected]>bpf: Add
tracing_multilink support for bpf progsSimilar to the
tracing_multilink support for kernel functions, add support for bpf progs by reusing the 'ids' fields as BTF ids, and adding fds array to indicate the target bpf prog. Then, for each pair of fd+id, they will be the attach targets oftracing_multilink.Quoted 16 lines
kernel/bpf/trampoline.c +4
-3@@ -1572,12 +1572,23 @@ static int update_fentry_multi(struct bpf_trampoline *tr, u32 orig_flags, struct bpf_tramp_image *im, struct ftrace_hash *hash, struct bpf_tracing_multi_data *data) { - unsigned long addr = (unsigned long)(im ? im->image : tr->cur_image->image); + if (tr->func.ftrace_managed) { + unsigned long addr = (unsigned long)(im ? im->image : tr->cur_image->image); -if (bpf_trampoline_use_jmp (tr->flags)) - addr =ftrace_jmp_set (addr); +if (bpf_trampoline_use_jmp (tr->flags)) + addr =ftrace_jmp_set (addr);kernel/bpf/trampoline.c static int update_fentry_multi(struct bpf_trampoline *tr, u32 orig_flags, · line 1589
Quoted 10 lines
kernel/bpf/trampoline.c +10
-0+ +ftrace_hash_add (hash, data->entry, tr->ip, addr); + }else { +void *old_addr = tr->cur_image ? tr->cur_image->image :NULL ; +void *new_addr = im ? im->image :NULL ; +int ret; + + ret =bpf_trampoline_update_fentry (tr, orig_flags, old_addr, new_addr); +if (ret) +return ret;[Severity: High]
Ifbpf_trampoline_update_fentry()fails here, we return the error and bypass updatingtr->cur_image. Inbpf_trampoline_multi_detach(), if__bpf_trampoline_unlink_prog()fails, it logs a warning but continues and unconditionally callsbpf_trampoline_multi_attach_free().
Quoted 4 lines
Since
tr->cur_imagewasn't updated, doesbpf_trampoline_multi_attach_free()end up freeing the currently active executable image, leading to a use-after-free and a double free on the next attach/detach?
The potential UAF issue will be fixed by Hui's patch [1].
Quoted 21 lines
Quoted 6 lines
kernel/bpf/trampoline.c +1
-1+ } -ftrace_hash_add (hash, data->entry, tr->ip, addr); tr->cur_image = im;return 0 ; }[ ... ]
kernel/bpf/trampoline.c int bpf_trampoline_multi_attach(struct bpf_prog *prog, u32 *ids, · line 1720
Quoted 7 lines
kernel/bpf/trampoline.c +3
-0@@ -1691,6 +1715,9 @@ int bpf_trampoline_multi_attach(struct bpf_prog *prog, u32 *ids,for_each_mnode (mnode, link) {bpf_trampoline_multi_attach_init (mnode->trampoline); +if (progs && progs[i]->aux->tail_call_reachable) + mnode->trampoline->flags |=BPF_TRAMP_F_TAIL_CALL_CTX ; +[Severity: Medium]
If__bpf_trampoline_link_prog()fails below, the error handler setsrollback_cnt= i and skips rolling back the current node i.
Quoted 3 lines
Does this leave the
BPF_TRAMP_F_TAIL_CALL_CTXflag permanently set on the trampoline?
False positive. If the target bpf prog is tail_call_reachable, the tr->flags must keep BPF_TRAMP_F_TAIL_CALL_CTX always.
Thanks,
Leon
Quoted 4 lines
Quoted 3 lines
kernel/bpf/trampoline.c +0
-0data->entry = &mnode->entry; err =__bpf_trampoline_link_prog (&mnode->node, mnode->trampoline,NULL , &trampoline_multi_ops, data);
Re: [PATCH bpf-next 05/13] libbpf: Add tracing_multi link support for bpf progs
In reply to [email protected]Quoted 48 lines · On 9/8/26 23:21, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Missing input validation inbpf_program__attach_tracing_multileads to a potential segmentation fault when a user-provided array element is NULL. ----- Patch [5]: [PATCH bpf-next 05/13] libbpf: Add
tracing_multilink support for bpf progs --- commitbef5c103ea1916738efc32c272265795c04de90a
Author: Leon Hwang <[email protected]>libbpf: Add
tracing_multilink support for bpf progsThis commit introduces
tracing_multilink support for bpf programs in libbpf. It adds fds and funcs fields tobpf_tracing_multi_optsto specify target programs, translating the function names to BTF IDs before passing them to the kernel.Quoted 4 lines
tools/lib/bpf/libbpf.c +0
-0[ ... ]
Quoted 4 lines
tools/lib/bpf/libbpf.c +0
-0@@ -12720,11 +12732,28 @@ bpf_program__attach_tracing_multi(const struct bpf_program *prog, const char *paif (cnt ==0 )return libbpf_err_ptr (-EINVAL ); ids = (const __u32 *) free_ids;tools/lib/bpf/libbpf.c bpf_program__attach_tracing_multi(const struct bpf_program *prog, const char *pa · line 12744
Quoted 10 lines
tools/lib/bpf/libbpf.c +10
-0+ }else if (fds) { +size_t cap =0 ; +int i; + + err =libbpf_ensure_mem ((void **) &free_ids, &cap,sizeof (*free_ids), cnt); +if (err) +return libbpf_err_ptr (err); + +for (i =0 ; i < cnt; i++) { + err =libbpf_find_prog_btf_id (funcs[i], fds[i], prog->obj->token_fd);[Severity: Medium]
Could funcs[i] be NULL here if the user-space application provided an invalid string pointer in the array?If it is NULL, this pointer gets passed down through
libbpf_find_prog_btf_id()and eventually reachesbtf_find_by_name_kind(), which performs this check:tools/lib/bpf/btf.c:btf_find_by_name_kind() { ... if (kind == BTF_KIND_UNKN || strcmp(type_name, "void") == 0) ... }
Quoted 4 lines
Calling
strcmp()with a NULLtype_namewill cause a segmentation fault in the application. Is it worth validating that funcs[i] is not NULL to safely return an error code instead of crashing?
Will check '!funcs[i]' before libbpf_find_prog_btf_id().
Thanks,
Leon
Quoted 8 lines
Quoted 7 lines
tools/lib/bpf/libbpf.c +6
-0+if (err <0 ) + goto error; + + free_ids[i] = err; + } + ids = (const __u32 *) free_ids; }
Re: [PATCH bpf-next 06/13] bpf: Add tracing_multi link fdinfo support for bpf progs
In reply to [email protected]Quoted 51 lines · On 10/8/26 00:20, [email protected] wrote:
Quoted 31 lines
kernel/trace/bpf_trace.c +13
-0@@ -3759,26 +3759,46 @@ static int bpf_tracing_multi_link_fill_link_info(const struct bpf_link *link, }#ifdef CONFIG_PROC_FS +static const char *bpf_prog_func_name (struct bpf_prog *prog,u32 btf_id) +{ +const struct btf *btf = prog->aux->btf; +const struct btf_type *t; + + t =btf_type_by_id (btf, btf_id); +if (!t || !btf_type_is_func (t)) +return "" ; + +return btf_name_by_offset (btf, t->name_off); +} +static void bpf_tracing_multi_show_fdinfo (const struct bpf_link *link,struct seq_file *seq) {struct bpf_tracing_multi_link *tr_link = container_of(link, struct bpf_tracing_multi_link, link);bool has_cookies = !!tr_link->cookies; +bool has_progs = !!tr_link->progs;seq_printf (seq,"attach_type:\t%u\n" , tr_link->link.attach_type);seq_printf (seq,"cnt:\t%u\n" , tr_link->nodes_cnt);kernel/trace/bpf_trace.c static int bpf_tracing_multi_link_fill_link_info(const struct bpf_link *link, · line 3793
Quoted 10 lines
kernel/trace/bpf_trace.c +4
-1-seq_printf (seq,"%s\t %s\t %s\t %s\n" ,"obj-id" ,"btf-id" ,"cookie" ,"func" ); +seq_printf (seq,"%s\t %s\t %s\t %s\n" , has_progs ?"prog-id" :"obj-id" ,"btf-id" ,"cookie" , +"func" );for (int i =0 ; i < tr_link->nodes_cnt; i++) {struct bpf_tracing_multi_node *mnode = &tr_link->nodes[i]; +u64 cookie = has_cookies ? tr_link->cookies[i] :0 ;u32 btf_id, obj_id;bpf_trampoline_unpack_key (mnode->trampoline->key, &obj_id, &btf_id); + btf_id = has_progs ? (u32 ) mnode->trampoline->key : btf_id;^^^^This isn't a bug, but since
bpf_trampoline_unpack_key()only masks off bit 31, which is never set for prog targets, does this re-assignment change anything, or could the unpacked value be used directly?
bpf_trampoline_unpack_key()already setsbtf_id= key &0x7FFFFFFF, and bit 31 is only set by the !tgt_prog branch ofbpf_trampoline_compute_key()(the kernel-BTF case).
Quoted 8 lines
In the
has_progscase the key was built as:((u64)tgt_prog->aux->id << 32) | ids[i]and ids[i] must be a valid BTF type id (<=
BTF_MAX_TYPE,0xfffff) orbpf_check_attach_target()would have rejected the attach, so (u32)key and the unpackedbtf_idare always equal here.
Good catch.
Will drop this line.
Quoted 32 lines
kernel/trace/bpf_trace.c static int bpf_tracing_multi_link_fill_link_info(const struct bpf_link *link, · line 3804
Quoted 11 lines
kernel/trace/bpf_trace.c +8
-0+if (has_progs) +seq_printf (seq,"%u\t %u\t %llu\t %s\n" , + obj_id, btf_id, cookie, +bpf_prog_func_name (tr_link->progs[i], btf_id)); +else +seq_printf (seq,"%u\t %u\t %llu\t %pS\n" , + obj_id, btf_id, cookie, + (void *) mnode->trampoline->ip);cond_resched (); }The commit message shows the sample fdinfo output but doesn't say much about the implementation or why this change is needed. In particular:
Quoted 16 lines
Show the target bpf prog info in the fdinfo.
$ cat /proc/self/fdinfo/13 pos: 0 flags: 02000000 mnt_id: 18 ino: 1045 link_type: tracing_multi link_id: 9 prog_tag: 9a7f9e3ab6f138ab prog_id: 25 attach_type: 61 cnt: 2 prog-id btf-id cookie func 21 4 16706819 target_1 22 5 16706819 target_2
Quoted 6 lines
This isn't a bug, but could the changelog say why the trampoline ip is not usable as the func column for prog targets (
tr->ipis 0 for JIT'd prog addresses), rather than mostly showing the resulting output? It also doesn't mention that the first column header changes from "obj-id" to "prog-id" for prog-target links.
Hmm, will describe the change in the commit msg.
Thanks,
Leon
Quoted 6 lines
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.mdCI run summary: https://github.com/kernel-patches/bpf/actions/runs/31320427087
Re: [PATCH bpf-next 07/13] bpf: Add tracing_multi link info support for bpf progs
In reply to [email protected]Quoted 30 lines · On 9/8/26 23:17, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Out-of-bounds Read / Uninitialized Memory Leak insym_name_trim_prefix()
- [Medium] UAPI Anti-Pattern: Usage of C bitfields in UAPI headers
----- Patch [7]: [PATCH bpf-next 07/13] bpf: Add
tracing_multilink info support for bpf progs --- commit65386c192a0d673092c95f3f31578ae7c4d19498
Author: Leon Hwang <[email protected]>
Subject: bpf: Addtracing_multilink info support for bpf progsThis commit adds
tracing_multilink info support for BPF programs. It updates thebpf_link_infostructure and extends bpftool to show function BTF IDs and program IDs.include/uapi/linux/bpf.h struct bpf_link_info { · line 6894
Quoted 11 lines
include/uapi/linux/bpf.h +2
-1@@ -6890,10 +6890,12 @@ struct bpf_link_info {__u32 attach_type;__u32 count;/* in/out: tracing_multi target count */ __u32 btf_obj_id; - __u32 :32 ; + __u32 tgt_progs:1; + __u32 :31 ;[Severity: Medium]
Could this cause endianness issues since C bitfield layout is implementation-defined?
Quoted 5 lines
The established Linux UAPI standard is to use a
__u32flags field with bitmask macros instead of C bitfields to ensure portability across architectures and cross-platform tools. (This also applies to the mirrored header intools/include/uapi/linux/bpf.h).
Correct.
Will use the bit mask style by changing '__u32 :32' to '__u32 flags' and adding BPF_F_TRACING_MULTI_TGT_PROGS.
Quoted 27 lines
[ ... ]
Quoted 4 lines
tools/bpf/bpftool/link.c +0
-0[ ... ]
Quoted 6 lines
tools/bpf/bpftool/link.c +6
-0+#define BPF_PROG_PREFIX "bpf_prog_" +#define BPF_PROG_PREFIX_LEN (sizeof(BPF_PROG_PREFIX) - 1) + +static const char * +sym_name_trim_prefix (struct kernel_sym *sym,bool is_prog) +{tools/bpf/bpftool/link.c show_uprobe_multi_json(struct bpf_link_info *info, json_writer_t *wtr) · line 440
Quoted 10 lines
tools/bpf/bpftool/link.c +10
-0+int prefix_len; + +if (!is_prog) +return sym->name; + +/* Ref kernel/bpf/core.c::bpf_prog_ksym_set_name() */ + prefix_len =BPF_PROG_PREFIX_LEN ; + prefix_len +=BPF_TAG_SIZE *2 ; + prefix_len +=1 ;/* skip the '_' */ +return sym->name + prefix_len;[Severity: Medium]
Does this code read past the null terminator for unnamed BPF programs?
Quoted 8 lines
When processing an unnamed BPF program (e.g., exported in kallsyms as
bpf_prog_9a7f9e3ab6f138ab), thesym->namebuffer contains exactly 25 characters. Here,prefix_lenis calculated as 26 (9 + 16 + 1).This offset advances the pointer past the null terminator into uninitialized heap memory, which is then passed to the JSON string writer when calling
jsonw_string_field()later inshow_tracing_multi_json().
Correct.
According to bpf_prog_ksym_set_name(), will check "sym->name[prefix_len] != '\0'" before "+= 1".
Thanks,
Leon
Re: [PATCH bpf-next 09/13] selftests/bpf: Add tracing_multi bpf prog attach failure tests
In reply to [email protected]Quoted 18 lines · On 9/8/26 23:17, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Build failure due to undefined symbollibbpf_find_prog_btf_id.
----- Patch [9]: [PATCH bpf-next 09/13] selftests/bpf: Add
tracing_multibpf prog attach failure tests --- commitc3b104cf9a90f8ddf11f23d2944ad76004fb3150
Author: Leon Hwang <[email protected]>selftests/bpf: Add
tracing_multibpf prog attach failure testsThis patch adds tests for
tracing_multiBPF program attach failures, covering both libbpf and kernel failure modes.Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/tracing_multi.c +0
-0[ ... ]
Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/tracing_multi.c static void test_attach_api_fails(void) · line 804
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/tracing_multi.c +4
-0+/* fail#4 (kernel) duplicate target through different fds */ + token_fd =bpf_object__token_fd (skel->obj); + token_fd = token_fd <0 ?0 : token_fd; + target_id =libbpf_find_prog_btf_id ("target_1" , fds[0 ], token_fd);[Severity: Low]
Does this code compile successfully?libbpf_find_prog_btf_idis an internal static function in libbpf, so calling it directly from selftests seems like it would cause an implicit function declaration and undefined reference linker error during the build.
Good catch.
libbpf_find_prog_btf_id() is exposed in the patch #11.
Will move the expose in this patch.
Thanks,
Leon
Quoted 7 lines
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/tracing_multi.c +6
-0+if (!ASSERT_GT (target_id,0 ,"target_1_btf_id" )) + goto cleanup; + + dup_fd =dup (fds[0 ]); +if (!ASSERT_GE (dup_fd,0 ,"dup_target_fd" )) + goto cleanup;
Re: [PATCH bpf-next 10/13] selftests/bpf: Add tracing_multi bpf prog cookie test
In reply to [email protected]Quoted 51 lines · On 10/8/26 00:20, [email protected] wrote:
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/tracing_multi.c +0
-0tools/testing/selftests/bpf/prog_tests/tracing_multi.c static int run_bpf_target(struct bpf_program *prog, __u32 retval, const char *na · line 312
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/tracing_multi.c +4
-1@@ -304,9 +304,12 @@ static int run_bpf_target(struct bpf_program *prog, __u32 retval, const char *nareturn 0 ; } -static void test_link_api_bpf_prog (void ) +static void __test_link_api_bpf_prog (bool test_cookies) {const char *funcs[] = {"target_1" ,"target_2" }; +__u64 fentry_cookies[] = {0xfeed01 ,0xfeed01 }; +__u64 fexit_cookies[] = {0xfeed02 ,0xfeed02 }; +__u64 fsession_cookies[] = {0xfeed03 ,0xfeed03 };A subsystem pattern flags this as potentially concerning: every per-target cookie array holds the same value twice. The test on the BPF side compares against a single scalar global set from element [0]:
skel->bss->fentry_cookie = fentry_cookies[0];and in
progs/tracing_multi_bpf.c:if (!test_cookies || bpf_get_attach_cookie(ctx) == fentry_cookie) test_result_fentry++;Since cookie[0] == cookie[1] for all three arrays, the test cannot distinguish a correct cookie-to-target mapping from a permuted one. The assertions (
test_result_fentry== 2, etc.) succeed as long as some correct-looking cookie value is delivered on both invocations, but swapping the cookies oftarget_1andtarget_2in the kernel is invisible.That matters because the kernel path this commit exercises is exactly a
permutation:bpf_tracing_multi_attach()callscheck_dup_keys()which callskeys_swap_r()inkernel/trace/bpf_trace.cto reorder the cookies, ids, and progs arrays to follow the sorted trampoline keys:id_a = ids + (key_a - keys); if (data->progs) { ... swap(*prog_a, *prog_b); } if (cookies) { cookie_a = cookies + (id_a - ids); ... swap(*cookie_a, *cookie_b); }An indexing mistake in that swap could desynchronise cookies from progs/ids, yet the test would still pass. Since fds[]/ids[] come from BPF prog FDs, the sort order is not the array order the caller supplied.
The neighbouring test for kernel-function targets in this same file already does this correctly: bpf_fentry_test_cookies[] uses ten distinct values deliberately out of order (8, 9, 7, 5, 4, 2, 3, 1, 10, 6) and
progs/tracing_multi_check.cchecks the cookie per-IP with something like:
Quoted 7 lines
err |= test_cookies ? cookie != 8 : 0;under
ip == &bpf_fentry_test1.Can the test use distinct cookies and a bpf_get_func_ip()-based per-target expectation so it is sensitive to cookie mis-association?
Will update the cookies with different values.
Quoted 10 lines
Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/tracing_multi.c +0
-0LIBBPF_OPTS (bpf_tracing_multi_opts, opts);struct tracing_multi_bpf *skel =NULL ;int fds[ARRAY_SIZE (funcs)];[ ... ]
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/tracing_multi.c +0
-0@@ -356,6 +367,16 @@ static void test_link_api_bpf_prog(void)tracing_multi_bpf__destroy (skel); }
Quoted 16 lines
tools/testing/selftests/bpf/prog_tests/tracing_multi.c static void test_link_api_bpf_prog(void) · line 378
Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/tracing_multi.c +9
-0+static void test_link_api_bpf_prog (void ) +{ +__test_link_api_bpf_prog (false); +} + +static void test_cookies_bpf_prog (void ) +{ +__test_link_api_bpf_prog (true); +}This isn't a bug, but would calling
__test_link_api_bpf_prog(false)and__test_link_api_bpf_prog(true)straight from the two subtest arms read more consistently? The neighbouringtest_link_api_ids()is dispatched directly astest_link_api_ids(false)andtest_link_api_ids(true)just a dozen lines down, without wrapper functions.
Okay, will drop these two wrappers.
Thanks,
Leon
Quoted 8 lines
[ ... ]
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.mdCI run summary: https://github.com/kernel-patches/bpf/actions/runs/31320427087
Re: [PATCH bpf-next 11/13] selftests/bpf: Add tracing_multi bpf prog rollback test
In reply to [email protected]Quoted 40 lines · On 9/8/26 23:21, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Thetest_rollback_bpf_progtest hardcodes executingtarget_1to verify rollback behavior, leading to spurious test failures iftarget_2happens to have a lower file descriptor. ----- Patch [11]: [PATCH bpf-next 11/13] selftests/bpf: Add
tracing_multibpf prog rollback test --- commitb065372f3722241a5632ef72df356c018b778825
Author: Leon Hwang <[email protected]>selftests/bpf: Add
tracing_multibpf prog rollback testThis commit adds a test to verify that prepared trampolines are correctly reverted when a BPF program multi-attach fails and triggers a rollback.
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/tracing_multi.c +0
-0[ ... ]
Quoted 1 line
tools/testing/selftests/bpf/prog_tests/tracing_multi.c +0
-0@@ -377,6 +377,65 @@ static void test_cookies_bpf_prog(void)[ ... ]
Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/tracing_multi.c +3
-0+static void test_rollback_bpf_prog (void ) +{ +int token_fd, prog_fd, fd =-1 , pre_fd =-1 ;[ ... ]
Quoted 1 line
tools/testing/selftests/bpf/prog_tests/tracing_multi.c +1
-0+ first = target_fds[0 ] < target_fds[1 ] ?0 :1 ;[ ... ]
tools/testing/selftests/bpf/prog_tests/tracing_multi.c static void test_cookies_bpf_prog(void) · line 427
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/tracing_multi.c +5
-0+ fd =bpf_link_create (prog_fd,0 ,BPF_TRACE_FENTRY_MULTI , &lopts); +if (!ASSERT_EQ (fd, -EBUSY ,"bpf_link_create" )) + goto cleanup; + +if (run_bpf_target (skel->progs.target_1, first +1 ,"rollback_target" ))[Severity: Medium]
Does this test accidentally hardcode the execution oftarget_1while expecting a dynamic return value based on file descriptor ordering?If the file descriptors are assigned such that target_fds[1] < target_fds[0], the variable first becomes 1. The test then executes
target_1but expects a return value of first + 1 (which is 2).
Quoted 6 lines
Since
target_1always returns 1, will this cause a spurious test failure?Additionally, if first == 1,
target_1would still have a trampoline attached viapre_fd. This would causetest_result_fentryto increment and fail the subsequentASSERT_EQ()check.
Will select the target prog according to 'first' value.
Thanks,
Leon
Quoted 6 lines
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/tracing_multi.c +4
-0+ goto cleanup; +ASSERT_EQ (skel->bss->test_result_fentry,0 ,"test_result_fentry" ); + +cleanup:[ ... ]
Re: [PATCH bpf-next 12/13] selftests/bpf: Add tracing_multi bpf prog link info test
In reply to [email protected]Quoted 41 lines · On 9/8/26 23:29, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Missing array sorting in user-space targets leads to flaky test
- [Medium] Uninitialized stack variablefidspassed to kernel
----- Patch [12]: [PATCH bpf-next 12/13] selftests/bpf: Add
tracing_multibpf prog link info test --- commit5ee3239754522a052ad0eb2cf3e2485319bff581
Author: Leon Hwang <[email protected]>selftests/bpf: Add
tracing_multibpf prog link info testVerify the link info works well for bpf prog targets of
tracing_multilink.Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/fill_link_info.c +0
-0[ ... ]
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/fill_link_info.c +6
-0@@ -467,13 +489,48 @@ static int setup_tmulti_targets(const struct bpf_program *prog,return -1 ; } +static int setup_tmulti_bpf_prog_targets (const struct bpf_program *prog,int token_fd, +const __u64 *cookies, +const struct bpf_program *const *target_progs, +struct tmulti_target *targets, +__u32 *btf_obj_id) +{[ ... ]
tools/testing/selftests/bpf/prog_tests/fill_link_info.c static int setup_tmulti_targets(const struct bpf_program *prog, · line 522
Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/fill_link_info.c +7
-0+ targets[i].cookie = cookies[i]; + targets[i].id = prog_info.id; + targets[i].func_btf_id = func_btf_id; + } + +return setup_tmulti_btf_obj_id (prog, btf_obj_id); +}[Severity: Medium]
Will this lead to flaky tests? The kernel sorts thetracing_multitargets by a computed key during attachment.
Quoted 8 lines
When returning link info, the arrays are returned in this sorted order, and
verify_tracing_multi_link_info()asserts that they perfectly match the targets array at the exact same indexes.However, unlike
setup_tmulti_targets(), this function does not callqsort()on the targets array. If ID allocation wraps around or another process allocates IDs concurrently, this might fail non-deterministically.
Will add qsort() here.
Quoted 28 lines
Quoted 17 lines
tools/testing/selftests/bpf/prog_tests/fill_link_info.c +7
-3+static int verify_tracing_multi_link_info (int fd,const struct bpf_program *prog,const struct tmulti_target *targets, -__u32 btf_obj_id,bool has_cookies) +__u32 btf_obj_id,__u32 count, +bool has_cookies,bool tgt_progs) { +#define TMULTI_CNT (TRACING_MULTI_CNT > TRACING_MULTI_BPF_CNT ? \ + TRACING_MULTI_CNT : TRACING_MULTI_BPF_CNT)enum bpf_attach_type attach_type =bpf_program__expected_attach_type (prog); -__u64 addrs[TRACING_MULTI_CNT ], cookies[TRACING_MULTI_CNT ]; -__u32 ids[TRACING_MULTI_CNT ]; +__u64 addrs[TMULTI_CNT ], cookies[TMULTI_CNT ]; +__u32 ids[TMULTI_CNT ], fids[TMULTI_CNT ];struct bpf_link_info info;__u32 len =sizeof (info);int err, i;[ ... ]
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/fill_link_info.c +2
-1memset (ids,0 ,sizeof (ids));memset (cookies,0 ,sizeof (cookies));@@ -496,7 +555,8 @@ static int verify_tracing_multi_link_info(int fd, const struct bpf_program *prog info.tracing_multi.ids =ptr_to_u64 (ids); info.tracing_multi.addrs =ptr_to_u64 (addrs); info.tracing_multi.cookies = has_cookies ?ptr_to_u64 (cookies) :0 ; - info.tracing_multi.count =TRACING_MULTI_CNT ; + info.tracing_multi.func_btf_ids = tgt_progs ?ptr_to_u64 (fids) :0 ; + info.tracing_multi.count = count;
Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/fill_link_info.c static int verify_tracing_multi_link_info(int fd, const struct bpf_program *prog · line 561
Quoted 1 line
tools/testing/selftests/bpf/prog_tests/fill_link_info.c +0
-0err =bpf_link_get_info_by_fd (fd, &info, &len);[Severity: Medium]
Does this code leave the fids array uninitialized?The other stack arrays (ids, cookies, addrs) are explicitly cleared with
memset()before being passed to thebpf_link_get_info_by_fd()syscall, but fids is not. This could potentially cause issues with uninitialized memory.
Will add 'memset(fids, 0, sizeof(fids))'.
Thanks,
Leon
Quoted 7 lines · Leon Hwang
Similar to the
tracing_multilink support for kernel functions [1], add support for bpf progs.When attaching to bpf progs, it must attaches to the target by text poke way.
Please spend a bit more human effort on justification for the change and explaining your use case. In what case you'll be attaching to a large amount of BPF programs such that attachment speed-up (if there is any) matters.
Please do your homework. Generating code with AI is easy, reviewing and making decisions about whether it's the right approach (and ultimately supporting it long term) is still a human process, so let's weigh that in.
Quoted 38 lines
[1] https://lore.kernel.org/bpf/[email protected]/
Leon Hwang (13):
bpf: Initializeftrace_managedinbpf_trampoline_get
bpf: Factor outupdate_fentry_multihelper
bpf: Drop unnecessaryftrace_location()inupdate_fentry_multi()
bpf: Addtracing_multilink support for bpf progs
libbpf: Addtracing_multilink support for bpf progs
bpf: Addtracing_multilink fdinfo support for bpf progs
bpf: Addtracing_multilink info support for bpf progs selftests/bpf: Addtracing_multibpf prog attach test selftests/bpf: Addtracing_multibpf prog attach failure tests selftests/bpf: Addtracing_multibpf prog cookie test selftests/bpf: Addtracing_multibpf prog rollback test selftests/bpf: Addtracing_multibpf prog link info test selftests/bpf: Test tailcall with fentry.multiinclude/linux/bpf.h | 3 + include/uapi/linux/bpf.h | 6 +- kernel/bpf/trampoline.c | 87 +++--- kernel/bpf/verifier.c | 22 +- kernel/trace/bpf_trace.c | 212 ++++++++++++--- tools/bpf/bpftool/link.c | 59 +++- tools/include/uapi/linux/bpf.h | 6 +- tools/lib/bpf/bpf.c | 1 + tools/lib/bpf/bpf.h | 2 + tools/lib/bpf/libbpf.c | 39 ++- tools/lib/bpf/libbpf.h | 4 +- tools/lib/bpf/libbpf_internal.h | 1 + .../selftests/bpf/prog_tests/fill_link_info.c | 170 ++++++++++-- .../selftests/bpf/prog_tests/tailcalls.c | 62 +++++ .../selftests/bpf/prog_tests/tracing_multi.c | 254 ++++++++++++++++++ .../selftests/bpf/progs/tracing_multi_bpf.c | 61 +++++ 16 files changed, 869 insertions(+), 120 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/tracing_multi_bpf.c--
2.55.0
Quoted 5 lines · Andrii Nakryiko
Quoted 4 lines · Leon Hwang
Similar to the
tracing_multilink support for kernel functions [1], add support for bpf progs.
Quoted 8 lines
Quoted 3 lines
When attaching to bpf progs, it must attaches to the target by text poke way.
Please spend a bit more human effort on justification for the change and explaining your use case. In what case you'll be attaching to a large amount of BPF programs such that attachment speed-up (if there is any) matters.
Will update the cover letter with justification and my use case.
Here's my use case:
I'm planning to enhance the function-graph feature of bpfsnoop [1]. It will trace all bpf progs, including their subprogs, to draw a full function call graph including bpf prog call sites.
The attachment could speed-up when there are hundreds of targets. For example, on high CPU core count servers, it probably runs 200 tc-bpf progs as Kubernetes CNI, and each tc-bpf prog might have 4 subprogs. Hence, there will be 1000 targets to trace for bpfsnoop's function-graph feature.
With tracing_multi link support for bpf progs, bpfsnoop will be able to attach to all targets quickly.
[1] https://github.com/bpfsnoop/bpfsnoop
Quoted 2 lines
Please do your homework. Generating code with AI is easy, reviewing
Sure, will do my homework.
Quoted 3 lines
and making decisions about whether it's the right approach (and ultimately supporting it long term) is still a human process, so let's weigh that in.
Will keep it in mind.
Thanks,
Leon
Quoted 39 lines
Quoted 39 lines
[1] https://lore.kernel.org/bpf/[email protected]/
Leon Hwang (13):
bpf: Initializeftrace_managedinbpf_trampoline_get
bpf: Factor outupdate_fentry_multihelper
bpf: Drop unnecessaryftrace_location()inupdate_fentry_multi()
bpf: Addtracing_multilink support for bpf progs
libbpf: Addtracing_multilink support for bpf progs
bpf: Addtracing_multilink fdinfo support for bpf progs
bpf: Addtracing_multilink info support for bpf progs selftests/bpf: Addtracing_multibpf prog attach test selftests/bpf: Addtracing_multibpf prog attach failure tests selftests/bpf: Addtracing_multibpf prog cookie test selftests/bpf: Addtracing_multibpf prog rollback test selftests/bpf: Addtracing_multibpf prog link info test selftests/bpf: Test tailcall with fentry.multiinclude/linux/bpf.h | 3 + include/uapi/linux/bpf.h | 6 +- kernel/bpf/trampoline.c | 87 +++--- kernel/bpf/verifier.c | 22 +- kernel/trace/bpf_trace.c | 212 ++++++++++++--- tools/bpf/bpftool/link.c | 59 +++- tools/include/uapi/linux/bpf.h | 6 +- tools/lib/bpf/bpf.c | 1 + tools/lib/bpf/bpf.h | 2 + tools/lib/bpf/libbpf.c | 39 ++- tools/lib/bpf/libbpf.h | 4 +- tools/lib/bpf/libbpf_internal.h | 1 + .../selftests/bpf/prog_tests/fill_link_info.c | 170 ++++++++++-- .../selftests/bpf/prog_tests/tailcalls.c | 62 +++++ .../selftests/bpf/prog_tests/tracing_multi.c | 254 ++++++++++++++++++ .../selftests/bpf/progs/tracing_multi_bpf.c | 61 +++++ 16 files changed, 869 insertions(+), 120 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/tracing_multi_bpf.c--
2.55.0
Quoted 16 lines · Leon Hwang
Quoted 5 lines · Andrii Nakryiko
Quoted 4 lines · Leon Hwang
Similar to the
tracing_multilink support for kernel functions [1], add support for bpf progs.Quoted 8 lines
Quoted 3 lines
When attaching to bpf progs, it must attaches to the target by text poke way.
Please spend a bit more human effort on justification for the change and explaining your use case. In what case you'll be attaching to a large amount of BPF programs such that attachment speed-up (if there is any) matters.
Quoted 8 lines
Will update the cover letter with justification and my use case.
Here's my use case:
I'm planning to enhance the function-graph feature of bpfsnoop [1]. It will trace all bpf progs, including their subprogs, to draw a full function call graph including bpf prog call sites.
fair enough, interesting use case, definitely outline that in the next revision (and provide before/after attach time as well for such use case, please)
Quoted 65 lines
The attachment could speed-up when there are hundreds of targets. For example, on high CPU core count servers, it probably runs 200 tc-bpf progs as Kubernetes CNI, and each tc-bpf prog might have 4 subprogs. Hence, there will be 1000 targets to trace for bpfsnoop's function-graph feature.
With
tracing_multilink support for bpf progs, bpfsnoop will be able to attach to all targets quickly.[1] https://github.com/bpfsnoop/bpfsnoop
Quoted 2 lines
Please do your homework. Generating code with AI is easy, reviewing
Sure, will do my homework.
Quoted 3 lines
and making decisions about whether it's the right approach (and ultimately supporting it long term) is still a human process, so let's weigh that in.
Will keep it in mind.
Thanks,
LeonQuoted 39 lines
Quoted 39 lines
[1] https://lore.kernel.org/bpf/[email protected]/
Leon Hwang (13):
bpf: Initializeftrace_managedinbpf_trampoline_get
bpf: Factor outupdate_fentry_multihelper
bpf: Drop unnecessaryftrace_location()inupdate_fentry_multi()
bpf: Addtracing_multilink support for bpf progs
libbpf: Addtracing_multilink support for bpf progs
bpf: Addtracing_multilink fdinfo support for bpf progs
bpf: Addtracing_multilink info support for bpf progs selftests/bpf: Addtracing_multibpf prog attach test selftests/bpf: Addtracing_multibpf prog attach failure tests selftests/bpf: Addtracing_multibpf prog cookie test selftests/bpf: Addtracing_multibpf prog rollback test selftests/bpf: Addtracing_multibpf prog link info test selftests/bpf: Test tailcall with fentry.multiinclude/linux/bpf.h | 3 + include/uapi/linux/bpf.h | 6 +- kernel/bpf/trampoline.c | 87 +++--- kernel/bpf/verifier.c | 22 +- kernel/trace/bpf_trace.c | 212 ++++++++++++--- tools/bpf/bpftool/link.c | 59 +++- tools/include/uapi/linux/bpf.h | 6 +- tools/lib/bpf/bpf.c | 1 + tools/lib/bpf/bpf.h | 2 + tools/lib/bpf/libbpf.c | 39 ++- tools/lib/bpf/libbpf.h | 4 +- tools/lib/bpf/libbpf_internal.h | 1 + .../selftests/bpf/prog_tests/fill_link_info.c | 170 ++++++++++-- .../selftests/bpf/prog_tests/tailcalls.c | 62 +++++ .../selftests/bpf/prog_tests/tracing_multi.c | 254 ++++++++++++++++++ .../selftests/bpf/progs/tracing_multi_bpf.c | 61 +++++ 16 files changed, 869 insertions(+), 120 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/tracing_multi_bpf.c--
2.55.0
Quoted 21 lines · Andrii Nakryiko
Quoted 20 lines · Leon Hwang
Quoted 5 lines · Andrii Nakryiko
Quoted 4 lines · Leon Hwang
Similar to the
tracing_multilink support for kernel functions [1], add support for bpf progs.Quoted 8 lines
Quoted 3 lines
When attaching to bpf progs, it must attaches to the target by text poke way.
Please spend a bit more human effort on justification for the change and explaining your use case. In what case you'll be attaching to a large amount of BPF programs such that attachment speed-up (if there is any) matters.
Will update the cover letter with justification and my use case.
Here's my use case:
Quoted 9 lines
Quoted 4 lines
I'm planning to enhance the function-graph feature of bpfsnoop [1]. It will trace all bpf progs, including their subprogs, to draw a full function call graph including bpf prog call sites.
fair enough, interesting use case, definitely outline that in the next revision (and provide before/after attach time as well for such use case, please)
Will include my use case.
Implemented the attachment micro-benchmark. Here's the result on an x86_64 16c16g VM:
./bench tracing-multi-attach-progs
Setting up benchmark 'tracing-multi-attach-progs'... tracing-multi-attach-progs: prepared 1000 identical BPF program targets tracing-multi-attach-progs: fentry created and attached 1000 programs/links in 679.324ms
tracing-multi-attach-progs: fentry.multi created one program and attached one 1000-target link in 470.042ms tracing-multi-attach-progs: fentry.multi creation/attachment speedup is 1.45x
The speedup 1.45x looks good. But attaching to 1000 progs via fentry costs less than 1 second, which is really faster than attaching to around 1000 kernel functions via fentry:
bpfsnoop -k '*:(struct sock *)sk' -m entry -D
Tracing 1096 tracees costs 11.597266736s
Thanks,
Leon
Quoted 47 lines · Leon Hwang
Quoted 21 lines · Andrii Nakryiko
Quoted 20 lines · Leon Hwang
Quoted 5 lines · Andrii Nakryiko
Quoted 4 lines · Leon Hwang
Similar to the
tracing_multilink support for kernel functions [1], add support for bpf progs.Quoted 8 lines
Quoted 3 lines
When attaching to bpf progs, it must attaches to the target by text poke way.
Please spend a bit more human effort on justification for the change and explaining your use case. In what case you'll be attaching to a large amount of BPF programs such that attachment speed-up (if there is any) matters.
Will update the cover letter with justification and my use case.
Here's my use case:
Quoted 9 lines
Quoted 4 lines
I'm planning to enhance the function-graph feature of bpfsnoop [1]. It will trace all bpf progs, including their subprogs, to draw a full function call graph including bpf prog call sites.
fair enough, interesting use case, definitely outline that in the next revision (and provide before/after attach time as well for such use case, please)
Will include my use case.
Implemented the attachment micro-benchmark. Here's the result on an
x86_6416c16g VM:./bench tracing-multi-attach-progs
Setting up benchmark 'tracing-multi-attach-progs'... tracing-multi-attach-progs: prepared 1000 identical BPF program targets tracing-multi-attach-progs: fentry created and attached 1000 programs/links in 679.324ms
tracing-multi-attach-progs: fentry.multi created one program and attached one 1000-target link in 470.042ms tracing-multi-attach-progs: fentry.multi creation/attachment speedup is 1.45x
Quoted 1 line
The speedup 1.45x looks good. But attaching to 1000 progs via fentry
I'd say "meh". If 500ms is ok, 700ms is not that much more noticeable.
Quoted 9 lines
costs less than 1 second, which is really faster than attaching to around 1000 kernel functions via fentry:
bpfsnoop -k '*:(struct sock *)sk' -m entry -D
Tracing 1096 tracees costs 11.597266736sThanks,
Leon
Quoted 48 lines · Andrii Nakryiko
Quoted 47 lines · Leon Hwang
Quoted 21 lines · Andrii Nakryiko
Quoted 20 lines · Leon Hwang
Quoted 5 lines · Andrii Nakryiko
Quoted 4 lines · Leon Hwang
Similar to the
tracing_multilink support for kernel functions [1], add support for bpf progs.Quoted 8 lines
Quoted 3 lines
When attaching to bpf progs, it must attaches to the target by text poke way.
Please spend a bit more human effort on justification for the change and explaining your use case. In what case you'll be attaching to a large amount of BPF programs such that attachment speed-up (if there is any) matters.
Will update the cover letter with justification and my use case.
Here's my use case:
Quoted 9 lines
Quoted 4 lines
I'm planning to enhance the function-graph feature of bpfsnoop [1]. It will trace all bpf progs, including their subprogs, to draw a full function call graph including bpf prog call sites.
fair enough, interesting use case, definitely outline that in the next revision (and provide before/after attach time as well for such use case, please)
Will include my use case.
Implemented the attachment micro-benchmark. Here's the result on an
x86_6416c16g VM:./bench tracing-multi-attach-progs
Setting up benchmark 'tracing-multi-attach-progs'... tracing-multi-attach-progs: prepared 1000 identical BPF program targets tracing-multi-attach-progs: fentry created and attached 1000 programs/links in 679.324ms
tracing-multi-attach-progs: fentry.multi created one program and attached one 1000-target link in 470.042ms tracing-multi-attach-progs: fentry.multi creation/attachment speedup is 1.45x
Quoted 4 lines
Quoted 1 line
The speedup 1.45x looks good. But attaching to 1000 progs via fentry
I'd say "meh". If 500ms is ok, 700ms is not that much more noticeable.
Besides the attachment speedup, this series provides a way to attach to multiple bpf progs via one link. The current revision reuses the ftrace-based attachment, which relies on CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS. In the next revision, it will get rid of CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS with its own attachment implementation. Hence, this series will be able to run on those arches that have trampoline support.
Thanks,
Leon
Quoted 50 lines · Leon Hwang
Quoted 48 lines · Andrii Nakryiko
Quoted 47 lines · Leon Hwang
Quoted 21 lines · Andrii Nakryiko
Quoted 20 lines · Leon Hwang
Quoted 5 lines · Andrii Nakryiko
Quoted 4 lines · Leon Hwang
Similar to the
tracing_multilink support for kernel functions [1], add support for bpf progs.Quoted 8 lines
Quoted 3 lines
When attaching to bpf progs, it must attaches to the target by text poke way.
Please spend a bit more human effort on justification for the change and explaining your use case. In what case you'll be attaching to a large amount of BPF programs such that attachment speed-up (if there is any) matters.
Will update the cover letter with justification and my use case.
Here's my use case:
Quoted 9 lines
Quoted 4 lines
I'm planning to enhance the function-graph feature of bpfsnoop [1]. It will trace all bpf progs, including their subprogs, to draw a full function call graph including bpf prog call sites.
fair enough, interesting use case, definitely outline that in the next revision (and provide before/after attach time as well for such use case, please)
Will include my use case.
Implemented the attachment micro-benchmark. Here's the result on an
x86_6416c16g VM:./bench tracing-multi-attach-progs
Setting up benchmark 'tracing-multi-attach-progs'... tracing-multi-attach-progs: prepared 1000 identical BPF program targets tracing-multi-attach-progs: fentry created and attached 1000 programs/links in 679.324ms
tracing-multi-attach-progs: fentry.multi created one program and attached one 1000-target link in 470.042ms tracing-multi-attach-progs: fentry.multi creation/attachment speedup is 1.45x
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
The speedup 1.45x looks good. But attaching to 1000 progs via fentry
I'd say "meh". If 500ms is ok, 700ms is not that much more noticeable.
Besides the attachment speedup, this series provides a way to attach to multiple bpf progs via one link. The current revision reuses the
that's mostly a convenience which is pretty trivial to abstract away in user space, not really a hard requirement for kernel to support this
Quoted 6 lines
ftrace-based attachment, which relies on
CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS. In the next revision, it will get rid ofCONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPSwith its own attachment implementation. Hence, this series will be able to run on those arches that have trampoline support.
sure, post a new version, but it's a bit less motivating to add extra complexity to the kernel if the only outcome is slight speed up and user space convenience of having single FD
let's look at new revision and see how this goes
Quoted 3 lines
Thanks,
Leon