bpf: Add batch support for attaching trampolines
Subsystems (13)
v5 cover bpf: Add batch support for attaching trampolines
hi,
sending new version of batch attach support, previous post is in here [1].
As discussed in previous post adding this version is adding generic approach to attach program to multiple functions, meaning you are now able to attach multiple trampolines on top of each other with no limitations, which was not possible in previous version.
v5 changes:
- ftrace new direct batch api got merged, so it's not longer part of this patchset (thanks Steven!), adding just few small related fixes
- added code that allows attach trampolines on top of each other as per previous version comments
- fixed btf__find_by_glob_kind as per Andrii's comments
- added new selftest to check on the trampoline's splitting
- added in bpf_arg/bpf_ret_value helpers with tests
I'm not completely sure about the approach, because it was quite difficult to debug/finalize, but I can't think of anything better for now.. so sending this as RFC ;)
Also available at:
https://git.kernel.org/pub/scm/linux/kernel/git/jolsa/perf.git
bpf/batch
thanks,
jirka
[1] https://lore.kernel.org/bpf/[email protected]/
Cc: Steven Rostedt <[email protected]>
---
Jiri Olsa (29):
ftrace: Use direct_ops hash in unregister_ftrace_direct
ftrace: Add cleanup to unregister_ftrace_direct_multi
ftrace: Add ftrace_set_filter_ips function
bpf: Factor bpf_check_attach_target function
bpf: Add bpf_check_attach_model function
bpf: Add bpf_arg/bpf_ret_value helpers for tracing programs bpf, x64: Allow to use caller address from stack
bpf: Keep active attached trampoline in bpf_prog
bpf: Add support to load multi func tracing program
bpf: Add bpf_trampoline_id object
bpf: Add addr to bpf_trampoline_id object
bpf: Add struct bpf_tramp_node layer
bpf: Add bpf_tramp_attach layer for trampoline attachment
bpf: Add support to store multiple ids in bpf_tramp_id object
bpf: Add support to store multiple addrs in bpf_tramp_id object
bpf: Add bpf_tramp_id_single function
bpf: Resolve id in bpf_tramp_id_single
bpf: Add refcount_t to struct bpf_tramp_id
bpf: Add support to attach trampolines with multiple IDs
bpf: Add support for tracing multi link
libbpf: Add btf__find_by_glob_kind function
libbpf: Add support to link multi func tracing program selftests/bpf: Add bpf_arg/bpf_ret_value test selftests/bpf: Add fentry multi func test selftests/bpf: Add fexit multi func test selftests/bpf: Add fentry/fexit multi func test selftests/bpf: Add mixed multi func test selftests/bpf: Add ret_mod multi func test selftests/bpf: Add attach multi func test
arch/x86/net/bpf_jit_comp.c | 31 ++- include/linux/bpf.h | 76 +++++-- include/linux/bpf_verifier.h | 23 +-- include/linux/ftrace.h | 3 + include/uapi/linux/bpf.h | 26 +++ kernel/bpf/core.c | 4 +- kernel/bpf/syscall.c | 379 +++++++++++++++++++++++++++++++---- kernel/bpf/trampoline.c | 926 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------- kernel/bpf/verifier.c | 173 +++++++++++++--- kernel/kallsyms.c | 2 +- kernel/trace/bpf_trace.c | 38 +++- kernel/trace/ftrace.c | 61 +++++- tools/include/uapi/linux/bpf.h | 26 +++ tools/lib/bpf/bpf.c | 7 + tools/lib/bpf/bpf.h | 6 +- tools/lib/bpf/btf.c | 77 ++++++++ tools/lib/bpf/btf.h | 3 + tools/lib/bpf/libbpf.c | 66 +++++++ tools/testing/selftests/bpf/Makefile | 10 +- tools/testing/selftests/bpf/prog_tests/args_test.c | 34 ++++ tools/testing/selftests/bpf/prog_tests/modify_return.c | 114 ++++++++++- tools/testing/selftests/bpf/prog_tests/multi_attach_test.c | 176 +++++++++++++++++ tools/testing/selftests/bpf/prog_tests/multi_fentry_fexit_test.c | 32 +++ tools/testing/selftests/bpf/prog_tests/multi_fentry_test.c | 30 +++ tools/testing/selftests/bpf/prog_tests/multi_fexit_test.c | 31 +++ tools/testing/selftests/bpf/prog_tests/multi_mixed_test.c | 34 ++++ tools/testing/selftests/bpf/progs/args_test.c | 30 +++ tools/testing/selftests/bpf/progs/multi_attach.c | 105 ++++++++++ tools/testing/selftests/bpf/progs/multi_check.c | 86 ++++++++ tools/testing/selftests/bpf/progs/multi_fentry.c | 17 ++ tools/testing/selftests/bpf/progs/multi_fentry_fexit.c | 28 +++ tools/testing/selftests/bpf/progs/multi_fexit.c | 20 ++ tools/testing/selftests/bpf/progs/multi_mixed.c | 43 ++++ tools/testing/selftests/bpf/progs/multi_modify_return.c | 17 ++ 34 files changed, 2539 insertions(+), 195 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/args_test.c create mode 100644 tools/testing/selftests/bpf/prog_tests/multi_attach_test.c create mode 100644 tools/testing/selftests/bpf/prog_tests/multi_fentry_fexit_test.c create mode 100644 tools/testing/selftests/bpf/prog_tests/multi_fentry_test.c create mode 100644 tools/testing/selftests/bpf/prog_tests/multi_fexit_test.c create mode 100644 tools/testing/selftests/bpf/prog_tests/multi_mixed_test.c create mode 100644 tools/testing/selftests/bpf/progs/args_test.c create mode 100644 tools/testing/selftests/bpf/progs/multi_attach.c create mode 100644 tools/testing/selftests/bpf/progs/multi_check.c create mode 100644 tools/testing/selftests/bpf/progs/multi_fentry.c create mode 100644 tools/testing/selftests/bpf/progs/multi_fentry_fexit.c create mode 100644 tools/testing/selftests/bpf/progs/multi_fexit.c create mode 100644 tools/testing/selftests/bpf/progs/multi_mixed.c create mode 100644 tools/testing/selftests/bpf/progs/multi_modify_return.c
PATCH v5 1/29 ftrace: Use direct_ops hash in unregister_ftrace_direct
Now when we have *direct_multi interface the direct_functions hash is no longer owned just by direct_ops. It's also used by any other ftrace_ops passed to *direct_multi interface.
Thus to find out that we are unregistering the last function from direct_ops, we need to check directly direct_ops's hash.
--- kernel/trace/ftrace.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/trace/ftrace.c +3 -1
@@ -5217,6 +5217,7 @@ int unregister_ftrace_direct(unsigned long ip, unsigned long addr) {struct ftrace_direct_func *direct;struct ftrace_func_entry *entry; +struct ftrace_hash *hash;int ret = -ENODEV ;mutex_lock (&direct_mutex);
@@ -5225,7 +5226,8 @@ int unregister_ftrace_direct(unsigned long ip, unsigned long addr)if (!entry) goto out_unlock; -if (direct_functions->count ==1 ) + hash = direct_ops.func_hash->filter_hash; +if (hash->count ==1 )unregister_ftrace_function (&direct_ops); ret =ftrace_set_filter_ip (&direct_ops, ip,1 ,0 );
Signature · 1 line
2.31.1
PATCH v5 2/29 ftrace: Add cleanup to unregister_ftrace_direct_multi
In reply to Jiri OlsaAdding ops cleanup to unregister_ftrace_direct_multi, so it can be reused in another register call.
--- kernel/trace/ftrace.c | 4 ++++ 1 file changed, 4 insertions(+)
kernel/trace/ftrace.c +4 -0
@@ -5542,6 +5542,10 @@ int unregister_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr) err =unregister_ftrace_function (ops);remove_direct_functions_hash (hash, addr);mutex_unlock (&direct_mutex); + +/* cleanup for possible another register call */ + ops->func =NULL ; + ops->trampoline =0 ;return err; }EXPORT_SYMBOL_GPL (unregister_ftrace_direct_multi);
Signature · 1 line
2.31.1
PATCH v5 3/29 ftrace: Add ftrace_set_filter_ips function
In reply to Jiri OlsaAdding ftrace_set_filter_ips function to be able to set filter on multiple ip addresses at once.
With the *direct_multi interface we have cases where we need to initialize ftrace_ops object with thousands of functions, so having single function diving into ftrace_hash_move_and_update_ops with ftrace_lock is better.
The functions ips are passed as unsigned ong array with count.
--- include/linux/ftrace.h | 3 +++ kernel/trace/ftrace.c | 53 +++++++++++++++++++++++++++++++++++------- 2 files changed, 47 insertions(+), 9 deletions(-)
include/linux/ftrace.h +3 -0
@@ -512,6 +512,8 @@ struct dyn_ftrace {int ftrace_set_filter_ip (struct ftrace_ops *ops,unsigned long ip,int remove,int reset); +int ftrace_set_filter_ips (struct ftrace_ops *ops,unsigned long *ips, +unsigned int cnt,int remove,int reset);int ftrace_set_filter (struct ftrace_ops *ops,unsigned char *buf,int len,int reset); int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
@@ -802,6 +804,7 @@ static inline unsigned long ftrace_location(unsigned long ip)#define ftrace_regex_open (ops, flag, inod, file) ({ -ENODEV; })#define ftrace_set_early_filter (ops, buf, enable) do { } while (0)#define ftrace_set_filter_ip (ops, ip, remove, reset) ({ -ENODEV; }) +#define ftrace_set_filter_ips (ops, ips, cnt, remove, reset) ({ -ENODEV; })#define ftrace_set_filter (ops, buf, len, reset) ({ -ENODEV; })#define ftrace_set_notrace (ops, buf, len, reset) ({ -ENODEV; })#define ftrace_free_filter (ops) do { } while (0)
kernel/trace/ftrace.c +44 -9
95 lines not on this page · raw message
Signature · 1 line
2.31.1
PATCH v5 4/29 bpf: Factor bpf_check_attach_target function
In reply to Jiri OlsaSeparating the check itself from model distilling and address search into __bpf_check_attach_target function.
This way we can easily add function in following patch that gets only function model without the address search, while using the same code as bpf_check_attach_target.
--- kernel/bpf/verifier.c | 79 ++++++++++++++++++++++++++++++++----------- 1 file changed, 59 insertions(+), 20 deletions(-)
kernel/bpf/verifier.c +59 -20
@@ -13568,20 +13568,26 @@ static int check_non_sleepable_error_inject(u32 btf_id)return btf_id_set_contains (&btf_non_sleepable_error_inject, btf_id); } -int bpf_check_attach_target (struct bpf_verifier_log *log, -const struct bpf_prog *prog, -const struct bpf_prog *tgt_prog, -u32 btf_id, -struct bpf_attach_target_info *tgt_info) +struct attach_target { +const struct btf_type *t; +const char *tname; +int subprog; +struct btf *btf; +}; + +static int __bpf_check_attach_target (struct bpf_verifier_log *log, +const struct bpf_prog *prog, +const struct bpf_prog *tgt_prog, +u32 btf_id, +struct attach_target *target) {bool prog_extension = prog->type ==BPF_PROG_TYPE_EXT ;const char prefix[] ="btf_trace_" ; -int ret =0 , subprog =-1 , i; +int subprog =-1 , i;const struct btf_type *t;bool conservative = true;const char *tname;struct btf *btf; -long addr =0 ;if (!btf_id) {bpf_log (log,"Tracing programs must provide btf_id\n" );
@@ -13706,9 +13712,6 @@ int bpf_check_attach_target(struct bpf_verifier_log *log, t =btf_type_by_id (btf, t->type);if (!btf_type_is_func_proto (t))return -EINVAL ; - ret =btf_distill_func_proto (log, btf, t, tname, &tgt_info->fmodel); -if (ret) -return ret;break ;default :if (!prog_extension)
@@ -13737,22 +13740,57 @@ int bpf_check_attach_target(struct bpf_verifier_log *log,if (tgt_prog && conservative) t =NULL ; + } + + target->t = t; + target->tname = tname; + target->subprog = subprog; + target->btf = btf; +return 0 ; +} + +int bpf_check_attach_target (struct bpf_verifier_log *log, +const struct bpf_prog *prog, +const struct bpf_prog *tgt_prog, +u32 btf_id, +struct bpf_attach_target_info *tgt_info) +{ +struct attach_target target = { }; +long addr =0 ; +int ret; - ret =btf_distill_func_proto (log, btf, t, tname, &tgt_info->fmodel); + ret =__bpf_check_attach_target (log, prog, tgt_prog, btf_id, &target); +if (ret) +return ret; + +switch (prog->expected_attach_type) { +case BPF_TRACE_RAW_TP : +break ; +case BPF_TRACE_ITER : + ret =btf_distill_func_proto (log, target.btf, target.t, target.tname, &tgt_info->fmodel); +if (ret) +return ret; +break ; +default : +case BPF_MODIFY_RETURN : +case BPF_LSM_MAC : +case BPF_TRACE_FENTRY : +case BPF_TRACE_FEXIT : + ret =btf_distill_func_proto (log, target.btf, target.t, target.tname, &tgt_info->fmodel);if (ret <0 )return ret;if (tgt_prog) { -if (subprog ==0 ) +if (target.subprog ==0 ) addr = (long ) tgt_prog->bpf_func;else - addr = (long ) tgt_prog->aux->func[subprog]->bpf_func; + addr = (long ) tgt_prog->aux->func[target.subprog]->bpf_func; }else { - addr =kallsyms_lookup_name (tname); + addr =kallsyms_lookup_name (target.tname);if (!addr) {bpf_log (log,"The address of function %s cannot be found\n" , - tname); + target.tname);return -ENOENT ; } }
@@ -13779,7 +13817,7 @@ int bpf_check_attach_target(struct bpf_verifier_log *log,break ; }if (ret) { -bpf_log (log,"%s is not sleepable\n" , tname); +bpf_log (log,"%s is not sleepable\n" , target.tname);return ret; } }else if (prog->expected_attach_type ==BPF_MODIFY_RETURN ) {
@@ -13787,18 +13825,19 @@ int bpf_check_attach_target(struct bpf_verifier_log *log,bpf_log (log,"can't modify return codes of BPF programs\n" );return -EINVAL ; } - ret =check_attach_modify_return (addr, tname); + ret =check_attach_modify_return (addr, target.tname);if (ret) { -bpf_log (log,"%s() is not modifiable\n" , tname); +bpf_log (log,"%s() is not modifiable\n" , target.tname);return ret; } }break ; } + tgt_info->tgt_addr = addr; - tgt_info->tgt_name = tname; - tgt_info->tgt_type = t; + tgt_info->tgt_name = target.tname; + tgt_info->tgt_type = target.t;return 0 ; }
Signature · 1 line
2.31.1
PATCH v5 5/29 bpf: Add bpf_check_attach_model function
In reply to Jiri OlsaAdding bpf_check_attach_model function that returns model for function specified by btf_id. It will be used in following patches.
--- include/linux/bpf_verifier.h | 4 ++++ kernel/bpf/verifier.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+)
include/linux/bpf_verifier.h +4 -0
@@ -527,6 +527,10 @@ int bpf_check_attach_target(struct bpf_verifier_log *log,const struct bpf_prog *tgt_prog, u32 btf_id, struct bpf_attach_target_info *tgt_info); +int bpf_check_attach_model (const struct bpf_prog *prog, +const struct bpf_prog *tgt_prog, +u32 btf_id, +struct btf_func_model *fmodel);void bpf_free_kfunc_btf_tab (struct bpf_kfunc_btf_tab *tab);
kernel/bpf/verifier.c +29 -0
@@ -13749,6 +13749,35 @@ static int __bpf_check_attach_target(struct bpf_verifier_log *log,return 0 ; } +int bpf_check_attach_model (const struct bpf_prog *prog, +const struct bpf_prog *tgt_prog, +u32 btf_id, +struct btf_func_model *fmodel) +{ +struct attach_target target = { }; +int ret; + + ret =__bpf_check_attach_target (NULL , prog, tgt_prog, btf_id, &target); +if (ret) +return ret; + +switch (prog->expected_attach_type) { +case BPF_TRACE_RAW_TP : +break ; +case BPF_TRACE_ITER : + ret =btf_distill_func_proto (NULL , target.btf, target.t, target.tname, fmodel); +break ; +default : +case BPF_MODIFY_RETURN : +case BPF_LSM_MAC : +case BPF_TRACE_FENTRY : +case BPF_TRACE_FEXIT : + ret =btf_distill_func_proto (NULL , target.btf, target.t, target.tname, fmodel); + } + +return ret; +} + int bpf_check_attach_target(struct bpf_verifier_log *log, const struct bpf_prog *prog, const struct bpf_prog *tgt_prog,
Signature · 1 line
2.31.1
PATCH v5 6/29 bpf: Add bpf_arg/bpf_ret_value helpers for tracing programs
In reply to Jiri OlsaAdding bpf_arg/bpf_ret_value helpers for tracing programs that returns traced function arguments.
Get n-th argument of the traced function:
long bpf_arg(void *ctx, int n)
Get return value of the traced function:
long bpf_ret_value(void *ctx)
The trampoline now stores number of arguments on ctx-8 address, so it's easy to verify argument index and find return value argument.
Moving function ip address on the trampoline stack behind the number of functions arguments, so it's now stored on ctx-16 address.
Both helpers are inlined by verifier.
--- arch/x86/net/bpf_jit_comp.c | 18 +++++++++++--- include/uapi/linux/bpf.h | 14 +++++++++++ kernel/bpf/verifier.c | 45 ++++++++++++++++++++++++++++++++-- kernel/trace/bpf_trace.c | 38 +++++++++++++++++++++++++++- tools/include/uapi/linux/bpf.h | 14 +++++++++++ 5 files changed, 122 insertions(+), 7 deletions(-)
arch/x86/net/bpf_jit_comp.c +14 -4
@@ -1941,7 +1941,7 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *i void *orig_call) {int ret, i, nr_args = m->nr_args; -int stack_size = nr_args *8 ; +int stack_size = nr_args *8 +8 /* nr_args */ ;struct bpf_tramp_progs *fentry = &tprogs[BPF_TRAMP_FENTRY ];struct bpf_tramp_progs *fexit = &tprogs[BPF_TRAMP_FEXIT ];struct bpf_tramp_progs *fmod_ret = &tprogs[BPF_TRAMP_MODIFY_RETURN ];
@@ -1987,12 +1987,22 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *iEMIT4 (0x48 ,0x83 ,0xe8 ,X86_PATCH_SIZE );emit_stx (&prog,BPF_DW ,BPF_REG_FP ,BPF_REG_0 , -stack_size); -/* Continue with stack_size for regs storage, stack will -* be correctly restored with 'leave' instruction. -*/ +/* Continue with stack_size for 'nr_args' storage */ stack_size -=8 ; } +/* Store number of arguments of the traced function: +* mov rax, nr_args +* mov QWORD PTR [rbp - stack_size], rax +*/ +emit_mov_imm64 (&prog,BPF_REG_0 ,0 , (u32 ) nr_args); +emit_stx (&prog,BPF_DW ,BPF_REG_FP ,BPF_REG_0 , -stack_size); + +/* Continue with stack_size for regs storage, stack will +* be correctly restored with 'leave' instruction. +*/ + stack_size -=8 ; +save_regs (m, &prog, nr_args, stack_size);if (flags &BPF_TRAMP_F_CALL_ORIG ) {
include/uapi/linux/bpf.h +14 -0
@@ -4957,6 +4957,18 @@ union bpf_attr {* **-ENOENT** if *task->mm* is NULL, or no vma contains *addr*. * **-EBUSY** if failed to try lock mmap_lock. * **-EINVAL** for invalid **flags**. +* +* long bpf_arg(void *ctx, int n) +* Description +* Get n-th argument of the traced function (for tracing programs). +* Return +* Value of the argument. +* +* long bpf_ret_value(void *ctx) +* Description +* Get return value of the traced function (for tracing programs). +* Return +* Return value of the traced function. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -5140,6 +5152,8 @@ union bpf_attr {FN (skc_to_unix_sock), \FN (kallsyms_lookup_name), \FN (find_vma), \ +FN (arg), \ +FN (ret_value), \/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper
kernel/bpf/verifier.c +43 -2
54 lines not on this page · raw message
kernel/trace/bpf_trace.c +37 -1
@@ -1012,7 +1012,7 @@ const struct bpf_func_proto bpf_snprintf_btf_proto = { BPF_CALL_1(bpf_get_func_ip_tracing, void *, ctx) {/* This helper call is inlined by verifier. */ -return ((u64 *)ctx)[-1 ]; +return ((u64 *)ctx)[-2 ]; }static const struct bpf_func_proto bpf_get_func_ip_proto_tracing = {
@@ -1091,6 +1091,38 @@ static const struct bpf_func_proto bpf_get_branch_snapshot_proto = { .arg2_type = ARG_CONST_SIZE_OR_ZERO, }; +BPF_CALL_2(bpf_arg, void *, ctx, int, n) +{ +/* This helper call is inlined by verifier. */ +u64 nr_args = ((u64 *)ctx)[-1 ]; + +if ((u64 ) n >= nr_args) +return 0 ; +return ((u64 *)ctx)[n]; +} + +static const struct bpf_func_proto bpf_arg_proto = { + .func = bpf_arg, + .gpl_only = true, + .ret_type =RET_INTEGER , + .arg1_type =ARG_PTR_TO_CTX , + .arg1_type =ARG_ANYTHING , +}; + +BPF_CALL_1(bpf_ret_value, void *, ctx) +{ +/* This helper call is inlined by verifier. */ +u64 nr_args = ((u64 *)ctx)[-1 ]; + +return ((u64 *)ctx)[nr_args]; +} + +static const struct bpf_func_proto bpf_ret_value_proto = { + .func = bpf_ret_value, + .gpl_only = true, + .ret_type =RET_INTEGER , +}; +static const struct bpf_func_proto *bpf_tracing_func_proto (enum bpf_func_id func_id,const struct bpf_prog *prog) {
@@ -1212,6 +1244,10 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)return &bpf_find_vma_proto;case BPF_FUNC_trace_vprintk:return bpf_get_trace_vprintk_proto (); +case BPF_FUNC_arg: +return &bpf_arg_proto; +case BPF_FUNC_ret_value: +return &bpf_ret_value_proto;default :return bpf_base_func_proto (func_id); }
tools/include/uapi/linux/bpf.h +14 -0
@@ -4957,6 +4957,18 @@ union bpf_attr {* **-ENOENT** if *task->mm* is NULL, or no vma contains *addr*. * **-EBUSY** if failed to try lock mmap_lock. * **-EINVAL** for invalid **flags**. +* +* long bpf_arg(void *ctx, int n) +* Description +* Get n-th argument of the traced function (for tracing programs). +* Return +* Value of the argument. +* +* long bpf_ret_value(void *ctx) +* Description +* Get return value of the traced function (for tracing programs). +* Return +* Return value of the traced function. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -5140,6 +5152,8 @@ union bpf_attr {FN (skc_to_unix_sock), \FN (kallsyms_lookup_name), \FN (find_vma), \ +FN (arg), \ +FN (ret_value), \/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper
Signature · 1 line
2.31.1
PATCH v5 7/29 bpf, x64: Allow to use caller address from stack
In reply to Jiri OlsaCurrently we call the original function by using the absolute address given at the JIT generation. That's not usable when having trampoline attached to multiple functions. In this case we need to take the return address from the stack.
Adding support to retrieve the original function address from the stack by adding new BPF_TRAMP_F_ORIG_STACK flag for arch_prepare_bpf_trampoline function.
Basically we take the return address of the 'fentry' call:
function + 0: call fentry # stores 'function + 5' address on stack
function + 5: ...
The 'function + 5' address will be used as the address for the original function to call.
--- arch/x86/net/bpf_jit_comp.c | 13 +++++++++---- include/linux/bpf.h | 5 +++++ 2 files changed, 14 insertions(+), 4 deletions(-)
arch/x86/net/bpf_jit_comp.c +9 -4
@@ -2035,10 +2035,15 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *iif (flags &BPF_TRAMP_F_CALL_ORIG ) {restore_regs (m, &prog, nr_args, stack_size); -/* call original function */ -if (emit_call (&prog, orig_call, prog)) { - ret = -EINVAL ; - goto cleanup; +if (flags &BPF_TRAMP_F_ORIG_STACK ) { +emit_ldx (&prog,BPF_DW ,BPF_REG_0 ,BPF_REG_FP ,8 ); +EMIT2 (0xff ,0xd0 );/* call *rax */ + }else { +/* call original function */ +if (emit_call (&prog, orig_call, prog)) { + ret = -EINVAL ; + goto cleanup; + } }/* remember return value in a stack for bpf prog to access */ emit_stx (&prog,BPF_DW ,BPF_REG_FP ,BPF_REG_0 ,-8 );
include/linux/bpf.h +5 -0
@@ -594,6 +594,11 @@ struct btf_func_model {/* Return the return value of fentry prog. Only used by bpf_struct_ops. */ #define BPF_TRAMP_F_RET_FENTRY_RET BIT(4) +/* Get original function from stack instead of from provided direct address. +* Makes sense for fexit programs only. +*/ +#define BPF_TRAMP_F_ORIG_STACK BIT(5) +/* Each call __bpf_prog_enter + call bpf_func + call __bpf_prog_exit is ~50 * bytes on x86. Pick a number to fit into BPF_IMAGE_SIZE / 2 */
Signature · 1 line
2.31.1
PATCH v5 8/29 bpf: Keep active attached trampoline in bpf_prog
In reply to Jiri OlsaKeeping active attached trampoline in bpf_prog so it can be used in following changes to account for multiple functions attachments in program.
As EXT programs are not going to be supported in multiple functions attachment for now, I'm keeping them stored in link.
--- include/linux/bpf.h | 1 + kernel/bpf/syscall.c | 34 +++++++++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 5 deletions(-)
include/linux/bpf.h +1 -0
@@ -872,6 +872,7 @@ struct bpf_prog_aux {struct mutex dst_mutex;/* protects dst_* pointers below, *after* prog becomes visible */ struct bpf_prog *dst_prog;struct bpf_trampoline *dst_trampoline; +struct bpf_trampoline *trampoline;enum bpf_prog_type saved_dst_prog_type;enum bpf_attach_type saved_dst_attach_type;bool verifier_zext;/* Zero extensions has been inserted by verifier. */
kernel/bpf/syscall.c +29 -5
@@ -2623,15 +2623,28 @@ struct bpf_tracing_link {struct bpf_prog *tgt_prog; }; +static struct bpf_trampoline *link_trampoline (struct bpf_tracing_link *link) +{ +struct bpf_prog *prog = link->link.prog; + +if (prog->type ==BPF_PROG_TYPE_EXT ) +return link->trampoline; +else +return prog->aux->trampoline; +} +static void bpf_tracing_link_release (struct bpf_link *link) {struct bpf_tracing_link *tr_link = container_of(link, struct bpf_tracing_link, link); +struct bpf_trampoline *tr =link_trampoline (tr_link); +struct bpf_prog *prog = link->prog; -WARN_ON_ONCE (bpf_trampoline_unlink_prog (link->prog, - tr_link->trampoline)); +WARN_ON_ONCE (bpf_trampoline_unlink_prog (link->prog, tr)); -bpf_trampoline_put (tr_link->trampoline); +if (prog->type !=BPF_PROG_TYPE_EXT ) + prog->aux->trampoline =NULL ; +bpf_trampoline_put (tr);/* tgt_prog is NULL if target is a kernel function */ if (tr_link->tgt_prog)
@@ -2662,9 +2675,10 @@ static int bpf_tracing_link_fill_link_info(const struct bpf_link *link, {struct bpf_tracing_link *tr_link = container_of(link, struct bpf_tracing_link, link); +struct bpf_trampoline *tr =link_trampoline (tr_link); info->tracing.attach_type = tr_link->attach_type; -bpf_trampoline_unpack_key (tr_link->trampoline->key, +bpf_trampoline_unpack_key (tr->key, &info->tracing.target_obj_id, &info->tracing.target_btf_id);
@@ -2682,6 +2696,7 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog,int tgt_prog_fd, u32 btf_id) { + bool prog_extension = prog->type == BPF_PROG_TYPE_EXT;struct bpf_link_primer link_primer;struct bpf_prog *tgt_prog =NULL ;struct bpf_trampoline *tr =NULL ;
@@ -2748,6 +2763,11 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog,mutex_lock (&prog->aux->dst_mutex); +if (!prog_extension && prog->aux->trampoline) { + err = -EBUSY ; + goto out_unlock; + } +/* There are a few possible cases here: * * - if prog->aux->dst_trampoline is set, the program was just loaded
@@ -2824,7 +2844,11 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog, } link->tgt_prog = tgt_prog; - link->trampoline = tr; + +if (prog_extension) + link->trampoline = tr; +else + prog->aux->trampoline = tr;/* Always clear the trampoline and target prog from prog->aux to make * sure the original attach destination is not kept alive after a
Signature · 1 line
2.31.1
PATCH v5 9/29 bpf: Add support to load multi func tracing program
In reply to Jiri OlsaAdding support to load tracing program with new BPF_F_MULTI_FUNC flag, that allows the program to be loaded without specific function to be attached to.
Such program will be allowed to be attached to multiple functions in following patches.
--- include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 7 +++++++ kernel/bpf/syscall.c | 35 +++++++++++++++++++++++++++++----- kernel/bpf/verifier.c | 3 ++- tools/include/uapi/linux/bpf.h | 7 +++++++ 5 files changed, 47 insertions(+), 6 deletions(-)
include/linux/bpf.h +1 -0
@@ -881,6 +881,7 @@ struct bpf_prog_aux {bool func_proto_unreliable;bool sleepable;bool tail_call_reachable; +bool multi_func;struct hlist_node tramp_hlist;/* BTF_KIND_FUNC_PROTO for valid attach_btf_id */ const struct btf_type *attach_func_proto;
include/uapi/linux/bpf.h +7 -0
@@ -1111,6 +1111,13 @@ enum bpf_link_type {*/ #define BPF_F_SLEEPABLE (1U << 4) +/* If BPF_F_MULTI_FUNC is used in BPF_PROG_LOAD command, the verifier does +* not expect BTF ID for the program, instead it assumes it's function +* with 6 u64 arguments. No trampoline is created for the program. Such +* program can be attached to multiple functions. +*/ +#define BPF_F_MULTI_FUNC (1U << 5) +/* When BPF ldimm64's insn[0].src_reg != 0 then this can have * the following extensions: *
kernel/bpf/syscall.c +30 -5
@@ -31,6 +31,7 @@#include <linux/bpf-netns.h> #include <linux/rcupdate_trace.h> #include <linux/memcontrol.h> +#include <linux/btf_ids.h> #define IS_FD_ARRAY (map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \ (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
@@ -2040,7 +2041,8 @@ static int bpf_prog_load_check_attach(enum bpf_prog_type prog_type, enum bpf_attach_type expected_attach_type, struct btf *attach_btf, u32 btf_id, - struct bpf_prog *dst_prog) + struct bpf_prog *dst_prog, + bool multi_func) { if (btf_id) { if (btf_id > BTF_MAX_TYPE)
@@ -2060,6 +2062,14 @@ bpf_prog_load_check_attach(enum bpf_prog_type prog_type, } } +if (multi_func) { +if (prog_type !=BPF_PROG_TYPE_TRACING ) +return -EINVAL ; +if (!attach_btf || btf_id) +return -EINVAL ; +return 0 ; + } +if (attach_btf && (!btf_id || dst_prog))return -EINVAL ;
@@ -2183,6 +2193,16 @@ static bool is_perfmon_prog_type(enum bpf_prog_type prog_type) } } +#define DEFINE_BPF_MULTI_FUNC(args...) \ + extern int bpf_multi_func(args); \ + int __init bpf_multi_func(args) { return 0; } + +DEFINE_BPF_MULTI_FUNC(unsigned long a1, unsigned long a2, + unsigned long a3, unsigned long a4, + unsigned long a5, unsigned long a6) + +BTF_ID_LIST_SINGLE(bpf_multi_func_btf_id, func, bpf_multi_func) + /* last field in 'union bpf_attr' used by this command */ #define BPF_PROG_LOAD_LAST_FIELD fd_array
@@ -2193,6 +2213,7 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr)struct btf *attach_btf =NULL ;int err;char license[128 ]; +bool multi_func;bool is_gpl;if (CHECK_ATTR (BPF_PROG_LOAD ))
@@ -2202,7 +2223,8 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr)BPF_F_ANY_ALIGNMENT |BPF_F_TEST_STATE_FREQ |BPF_F_SLEEPABLE | - BPF_F_TEST_RND_HI32)) +BPF_F_TEST_RND_HI32 | + BPF_F_MULTI_FUNC)) return -EINVAL; if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
@@ -2233,6 +2255,8 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr)if (is_perfmon_prog_type (type) && !perfmon_capable ())return -EPERM ; + multi_func = attr->prog_flags &BPF_F_MULTI_FUNC ; +/* attach_prog_fd/attach_btf_obj_fd can specify fd of either bpf_prog * or btf, we need to check which one it is */
@@ -2251,7 +2275,7 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr)return -ENOTSUPP ; } } - }else if (attr->attach_btf_id) { + }else if (attr->attach_btf_id || multi_func) {/* fall back to vmlinux BTF, if BTF type ID is specified */ attach_btf =bpf_get_btf_vmlinux ();if (IS_ERR (attach_btf))
@@ -2264,7 +2288,7 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr)bpf_prog_load_fixup_attach_type (attr);if (bpf_prog_load_check_attach (type, attr->expected_attach_type, attach_btf, attr->attach_btf_id, - dst_prog)) { + dst_prog, multi_func)) {if (dst_prog)bpf_prog_put (dst_prog);if (attach_btf)
@@ -2284,10 +2308,11 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr) prog->expected_attach_type = attr->expected_attach_type; prog->aux->attach_btf = attach_btf; - prog->aux->attach_btf_id = attr->attach_btf_id; + prog->aux->attach_btf_id = multi_func ? bpf_multi_func_btf_id[0 ] : attr->attach_btf_id; prog->aux->dst_prog = dst_prog; prog->aux->offload_requested = !!attr->prog_ifindex; prog->aux->sleepable = attr->prog_flags &BPF_F_SLEEPABLE ; + prog->aux->multi_func = multi_func; err =security_bpf_prog_alloc (prog->aux);if (err)
kernel/bpf/verifier.c +2 -1
@@ -13983,7 +13983,8 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)if (!bpf_iter_prog_supported (prog))return -EINVAL ;return 0 ; - } + }else if (prog->aux->multi_func) +return prog->type ==BPF_PROG_TYPE_TRACING ?0 : -EINVAL ;if (prog->type ==BPF_PROG_TYPE_LSM ) { ret =bpf_lsm_verify_prog (&env->log, prog);
tools/include/uapi/linux/bpf.h +7 -0
@@ -1111,6 +1111,13 @@ enum bpf_link_type {*/ #define BPF_F_SLEEPABLE (1U << 4) +/* If BPF_F_MULTI_FUNC is used in BPF_PROG_LOAD command, the verifier does +* not expect BTF ID for the program, instead it assumes it's function +* with 6 u64 arguments. No trampoline is created for the program. Such +* program can be attached to multiple functions. +*/ +#define BPF_F_MULTI_FUNC (1U << 5) +/* When BPF ldimm64's insn[0].src_reg != 0 then this can have * the following extensions: *
Signature · 1 line
2.31.1
PATCH v5 10/29 bpf: Add bpf_trampoline_id object
In reply to Jiri OlsaReplacing the bpf_trampoline's key with struct bpf_tramp_id object, that currently holds only obj_id/btf_id, so same data as key.
Having the key in the struct will allow us to add more ids (functions) to single trampoline in following patches.
No functional change is intended.
--- include/linux/bpf.h | 18 +++++++++-- include/linux/bpf_verifier.h | 19 ------------ kernel/bpf/syscall.c | 30 +++++++++++++----- kernel/bpf/trampoline.c | 59 +++++++++++++++++++++++++++++++----- kernel/bpf/verifier.c | 14 ++++++--- 5 files changed, 99 insertions(+), 41 deletions(-)
include/linux/bpf.h +15 -3
@@ -671,13 +671,18 @@ struct bpf_tramp_image { }; }; +struct bpf_tramp_id { +u32 obj_id; +u32 btf_id; +}; +struct bpf_trampoline {/* hlist for trampoline_table */ struct hlist_node hlist;/* serializes access to fields of this trampoline */ struct mutex mutex;refcount_t refcnt; -u64 key; +struct bpf_tramp_id *id;struct {struct btf_func_model model;void *addr;
@@ -732,9 +737,16 @@ static __always_inline __nocfi unsigned int bpf_dispatcher_nop_func(return bpf_func (ctx, insnsi); }#ifdef CONFIG_BPF_JIT +struct bpf_tramp_id *bpf_tramp_id_alloc (void ); +void bpf_tramp_id_free (struct bpf_tramp_id *id); +bool bpf_tramp_id_is_empty (struct bpf_tramp_id *id); +int bpf_tramp_id_is_equal (struct bpf_tramp_id *a,struct bpf_tramp_id *b); +void bpf_tramp_id_init (struct bpf_tramp_id *id, +const struct bpf_prog *tgt_prog, +struct btf *btf,u32 btf_id);int bpf_trampoline_link_prog (struct bpf_prog *prog,struct bpf_trampoline *tr);int bpf_trampoline_unlink_prog (struct bpf_prog *prog,struct bpf_trampoline *tr); -struct bpf_trampoline *bpf_trampoline_get (u64 key, +struct bpf_trampoline *bpf_trampoline_get (struct bpf_tramp_id *id,struct bpf_attach_target_info *tgt_info);void bpf_trampoline_put (struct bpf_trampoline *tr);#define BPF_DISPATCHER_INIT (_name) { \
@@ -792,7 +804,7 @@ static inline int bpf_trampoline_unlink_prog(struct bpf_prog *prog, {return -ENOTSUPP ; } -static inline struct bpf_trampoline *bpf_trampoline_get (u64 key, +static inline struct bpf_trampoline *bpf_trampoline_get (struct bpf_tramp_id *id,struct bpf_attach_target_info *tgt_info) {return ERR_PTR (-EOPNOTSUPP );
include/linux/bpf_verifier.h +0 -19
@@ -503,25 +503,6 @@ int check_ctx_reg(struct bpf_verifier_env *env,int check_mem_reg (struct bpf_verifier_env *env,struct bpf_reg_state *reg,u32 regno,u32 mem_size); -/* this lives here instead of in bpf.h because it needs to dereference tgt_prog */ -static inline u64 bpf_trampoline_compute_key (const struct bpf_prog *tgt_prog, -struct btf *btf,u32 btf_id) -{ -if (tgt_prog) -return ((u64 )tgt_prog->aux->id <<32 ) | btf_id; -else -return ((u64 )btf_obj_id (btf) <<32 ) |0x80000000 | btf_id; -} - -/* unpack the IDs from the key as constructed above */ -static inline void bpf_trampoline_unpack_key (u64 key,u32 *obj_id,u32 *btf_id) -{ -if (obj_id) - *obj_id = key >>32 ; -if (btf_id) - *btf_id = key &0x7FFFFFFF ; -} - int bpf_check_attach_target(struct bpf_verifier_log *log, const struct bpf_prog *prog, const struct bpf_prog *tgt_prog,
kernel/bpf/syscall.c +22 -8
@@ -2703,9 +2703,8 @@ static int bpf_tracing_link_fill_link_info(const struct bpf_link *link,struct bpf_trampoline *tr =link_trampoline (tr_link); info->tracing.attach_type = tr_link->attach_type; -bpf_trampoline_unpack_key (tr->key, - &info->tracing.target_obj_id, - &info->tracing.target_btf_id); + info->tracing.target_obj_id = tr->id->obj_id; + info->tracing.target_btf_id = tr->id->btf_id;return 0 ; }
@@ -2726,7 +2725,7 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog,struct bpf_prog *tgt_prog =NULL ;struct bpf_trampoline *tr =NULL ;struct bpf_tracing_link *link; -u64 key =0 ; +struct bpf_tramp_id *id =NULL ;int err;switch (prog->type) {
@@ -2767,6 +2766,12 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog, goto out_put_prog; } + id =bpf_tramp_id_alloc (); +if (!id) { + err = -ENOMEM ; + goto out_put_prog; + } + tgt_prog =bpf_prog_get (tgt_prog_fd);if (IS_ERR (tgt_prog)) { err =PTR_ERR (tgt_prog);
@@ -2774,7 +2779,7 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog, goto out_put_prog; } - key =bpf_trampoline_compute_key (tgt_prog,NULL , btf_id); +bpf_tramp_id_init (id, tgt_prog,NULL , btf_id); } link =kzalloc (sizeof (*link),GFP_USER );
@@ -2823,12 +2828,20 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog, err = -EINVAL ; goto out_unlock; } + + id =bpf_tramp_id_alloc (); +if (!id) { + err = -ENOMEM ; + goto out_unlock; + } + btf_id = prog->aux->attach_btf_id; - key =bpf_trampoline_compute_key (NULL , prog->aux->attach_btf, btf_id); +bpf_tramp_id_init (id,NULL , prog->aux->attach_btf, btf_id); }if (!prog->aux->dst_trampoline || - (key && key != prog->aux->dst_trampoline->key)) { + (!bpf_tramp_id_is_empty (id) && +bpf_tramp_id_is_equal (id, prog->aux->dst_trampoline->id))) {/* If there is no saved target, or the specified target is * different from the destination specified at load time, we * need a new trampoline and a check for compatibility
@@ -2840,7 +2853,7 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog,if (err) goto out_unlock; - tr =bpf_trampoline_get (key, &tgt_info); + tr =bpf_trampoline_get (id, &tgt_info);if (!tr) { err = -ENOMEM ; goto out_unlock;
@@ -2900,6 +2913,7 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog, out_put_prog:if (tgt_prog_fd && tgt_prog)bpf_prog_put (tgt_prog); +bpf_tramp_id_free (id);return err; }
kernel/bpf/trampoline.c +52 -7
118 lines not on this page · raw message
kernel/bpf/verifier.c +10 -4
@@ -13929,8 +13929,8 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)struct bpf_attach_target_info tgt_info = {};u32 btf_id = prog->aux->attach_btf_id;struct bpf_trampoline *tr; +struct bpf_tramp_id *id;int ret; -u64 key;if (prog->type ==BPF_PROG_TYPE_SYSCALL ) {if (prog->aux->sleepable)
@@ -13995,11 +13995,17 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)return -EINVAL ; } - key =bpf_trampoline_compute_key (tgt_prog, prog->aux->attach_btf, btf_id); - tr =bpf_trampoline_get (key, &tgt_info); -if (!tr) + id =bpf_tramp_id_alloc (); +if (!id)return -ENOMEM ; +bpf_tramp_id_init (id, tgt_prog, prog->aux->attach_btf, btf_id); + tr =bpf_trampoline_get (id, &tgt_info); +if (!tr) { +bpf_tramp_id_free (id); +return -ENOMEM ; + } + prog->aux->dst_trampoline = tr;return 0 ; }
Signature · 1 line
2.31.1
PATCH v5 11/29 bpf: Add addr to bpf_trampoline_id object
In reply to Jiri OlsaAdding addr to bpf_trampoline_id object so it's not associated directly with trampoline directly. This will help us to easily support multiple ids/addresses support for trampolines coming in following changes.
--- include/linux/bpf.h | 4 ++-- kernel/bpf/trampoline.c | 18 +++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-)
include/linux/bpf.h +2 -2
@@ -674,6 +674,7 @@ struct bpf_tramp_image {struct bpf_tramp_id {u32 obj_id;u32 btf_id; +void *addr; };struct bpf_trampoline {
@@ -685,11 +686,10 @@ struct bpf_trampoline {struct bpf_tramp_id *id;struct {struct btf_func_model model; -void *addr;bool ftrace_managed; } func;/* if !NULL this is BPF_PROG_TYPE_EXT program that extends another BPF -* program by replacing one of its functions. func.addr is the address +* program by replacing one of its functions. id->addr is the address * of the function it replaced. */ struct bpf_prog *extension_prog;
kernel/bpf/trampoline.c +9 -9
@@ -136,7 +136,7 @@ static int bpf_trampoline_module_get(struct bpf_trampoline *tr)int err =0 ;preempt_disable (); - mod =__module_text_address ((unsigned long ) tr->func.addr); + mod =__module_text_address ((unsigned long ) tr->id->addr);if (mod && !try_module_get (mod)) err = -ENOENT ;preempt_enable ();
@@ -164,7 +164,7 @@ static int is_ftrace_location(void *ip)static int unregister_fentry (struct bpf_trampoline *tr,void *old_addr) { -void *ip = tr->func.addr; +void *ip = tr->id->addr;int ret;if (tr->func.ftrace_managed)
@@ -179,7 +179,7 @@ static int unregister_fentry(struct bpf_trampoline *tr, void *old_addr)static int modify_fentry (struct bpf_trampoline *tr,void *old_addr,void *new_addr) { -void *ip = tr->func.addr; +void *ip = tr->id->addr;int ret;if (tr->func.ftrace_managed)
@@ -192,7 +192,7 @@ static int modify_fentry(struct bpf_trampoline *tr, void *old_addr, void *new_ad/* first time registering */ static int register_fentry (struct bpf_trampoline *tr,void *new_addr) { -void *ip = tr->func.addr; +void *ip = tr->id->addr;int ret; ret =is_ftrace_location (ip);
@@ -410,7 +410,7 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr) err =arch_prepare_bpf_trampoline (im, im->image, im->image +PAGE_SIZE , &tr->func.model, flags, tprogs, - tr->func.addr); + tr->id->addr);if (err <0 ) goto out;
@@ -478,7 +478,7 @@ int bpf_trampoline_link_prog(struct bpf_prog *prog, struct bpf_trampoline *tr) goto out; } tr->extension_prog = prog; - err =bpf_arch_text_poke (tr->func.addr,BPF_MOD_JUMP ,NULL , + err =bpf_arch_text_poke (tr->id->addr,BPF_MOD_JUMP ,NULL , prog->bpf_func); goto out; }
@@ -513,7 +513,7 @@ int bpf_trampoline_unlink_prog(struct bpf_prog *prog, struct bpf_trampoline *tr)mutex_lock (&tr->mutex);if (kind ==BPF_TRAMP_REPLACE ) {WARN_ON_ONCE (!tr->extension_prog); - err =bpf_arch_text_poke (tr->func.addr,BPF_MOD_JUMP , + err =bpf_arch_text_poke (tr->id->addr,BPF_MOD_JUMP , tr->extension_prog->bpf_func,NULL ); tr->extension_prog =NULL ; goto out;
@@ -536,11 +536,11 @@ struct bpf_trampoline *bpf_trampoline_get(struct bpf_tramp_id *id,return NULL ;mutex_lock (&tr->mutex); -if (tr->func.addr) +if (tr->id->addr) goto out;memcpy (&tr->func.model, &tgt_info->fmodel,sizeof (tgt_info->fmodel)); - tr->func.addr = (void *)tgt_info->tgt_addr; + tr->id->addr = (void *)tgt_info->tgt_addr; out:mutex_unlock (&tr->mutex);return tr;
Signature · 1 line
2.31.1
PATCH v5 12/29 bpf: Add struct bpf_tramp_node layer
In reply to Jiri OlsaCurrently each trampoline holds a list of programs that are attached to it. With multi func attach support we need a way for a single program to be connected to multiple trampolines.
Adding struct bpf_tramp_node object that holds bpf_prog pointer, so it can be resolved directly. We can now have multiple struct bpf_tramp_node being attached to different trampolines pointing to single bpf_prog.
--- include/linux/bpf.h | 15 ++++++++++----- kernel/bpf/core.c | 1 + kernel/bpf/syscall.c | 4 ++-- kernel/bpf/trampoline.c | 22 ++++++++++++---------- 4 files changed, 25 insertions(+), 17 deletions(-)
include/linux/bpf.h +10 -5
@@ -677,6 +677,11 @@ struct bpf_tramp_id {void *addr; }; +struct bpf_tramp_node { +struct hlist_node hlist_tramp; +struct bpf_prog *prog; +}; +struct bpf_trampoline {/* hlist for trampoline_table */ struct hlist_node hlist;
@@ -744,8 +749,8 @@ int bpf_tramp_id_is_equal(struct bpf_tramp_id *a, struct bpf_tramp_id *b);void bpf_tramp_id_init (struct bpf_tramp_id *id,const struct bpf_prog *tgt_prog,struct btf *btf,u32 btf_id); -int bpf_trampoline_link_prog (struct bpf_prog *prog,struct bpf_trampoline *tr); -int bpf_trampoline_unlink_prog (struct bpf_prog *prog,struct bpf_trampoline *tr); +int bpf_trampoline_link_prog (struct bpf_tramp_node *node,struct bpf_trampoline *tr); +int bpf_trampoline_unlink_prog (struct bpf_tramp_node *node,struct bpf_trampoline *tr);struct bpf_trampoline *bpf_trampoline_get (struct bpf_tramp_id *id,struct bpf_attach_target_info *tgt_info);void bpf_trampoline_put (struct bpf_trampoline *tr);
@@ -794,12 +799,12 @@ void bpf_ksym_del(struct bpf_ksym *ksym);int bpf_jit_charge_modmem (u32 pages);void bpf_jit_uncharge_modmem (u32 pages);#else -static inline int bpf_trampoline_link_prog (struct bpf_prog *prog, +static inline int bpf_trampoline_link_prog (struct bpf_tramp_node *node,struct bpf_trampoline *tr) {return -ENOTSUPP ; } -static inline int bpf_trampoline_unlink_prog (struct bpf_prog *prog, +static inline int bpf_trampoline_unlink_prog (struct bpf_tramp_node *node,struct bpf_trampoline *tr) {return -ENOTSUPP ;
@@ -894,7 +899,7 @@ struct bpf_prog_aux {bool sleepable;bool tail_call_reachable;bool multi_func; -struct hlist_node tramp_hlist; +struct bpf_tramp_node tramp_node;/* BTF_KIND_FUNC_PROTO for valid attach_btf_id */ const struct btf_type *attach_func_proto;/* function name for valid attach_btf_id */
kernel/bpf/core.c +1 -0
@@ -105,6 +105,7 @@ struct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flag fp->aux = aux; fp->aux->prog = fp; fp->jit_requested =ebpf_jit_enabled (); + fp->aux->tramp_node.prog = fp;INIT_LIST_HEAD_RCU (&fp->aux->ksym.lnode);mutex_init (&fp->aux->used_maps_mutex);
kernel/bpf/syscall.c +2 -2
@@ -2665,7 +2665,7 @@ static void bpf_tracing_link_release(struct bpf_link *link)struct bpf_trampoline *tr =link_trampoline (tr_link);struct bpf_prog *prog = link->prog; -WARN_ON_ONCE (bpf_trampoline_unlink_prog (link->prog, tr)); +WARN_ON_ONCE (bpf_trampoline_unlink_prog (&link->prog->aux->tramp_node, tr));if (prog->type !=BPF_PROG_TYPE_EXT ) prog->aux->trampoline =NULL ;
@@ -2874,7 +2874,7 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog,if (err) goto out_unlock; - err =bpf_trampoline_link_prog (prog, tr); + err =bpf_trampoline_link_prog (&prog->aux->tramp_node, tr);if (err) {bpf_link_cleanup (&link_primer); link =NULL ;
kernel/bpf/trampoline.c +12 -10
@@ -216,8 +216,8 @@ static int register_fentry(struct bpf_trampoline *tr, void *new_addr)static struct bpf_tramp_progs *bpf_trampoline_get_progs (const struct bpf_trampoline *tr,int *total,bool *ip_arg) { -const struct bpf_prog_aux *aux;struct bpf_tramp_progs *tprogs; +struct bpf_tramp_node *node;struct bpf_prog **progs;int kind;
@@ -231,9 +231,9 @@ bpf_trampoline_get_progs(const struct bpf_trampoline *tr, int *total, bool *ip_a *total += tr->progs_cnt[kind]; progs = tprogs[kind].progs; -hlist_for_each_entry (aux, &tr->progs_hlist[kind], tramp_hlist) { - *ip_arg |= aux->prog->call_get_func_ip; - *progs++ = aux->prog; +hlist_for_each_entry (node, &tr->progs_hlist[kind], hlist_tramp) { + *ip_arg |= node->prog->call_get_func_ip; + *progs++ = node->prog; } }return tprogs;
@@ -455,8 +455,9 @@ static enum bpf_tramp_prog_type bpf_attach_type_to_tramp(struct bpf_prog *prog) } } -int bpf_trampoline_link_prog (struct bpf_prog *prog,struct bpf_trampoline *tr) +int bpf_trampoline_link_prog (struct bpf_tramp_node *node,struct bpf_trampoline *tr) { +struct bpf_prog *prog = node->prog;enum bpf_tramp_prog_type kind;int err =0 ;int cnt;
@@ -486,16 +487,16 @@ int bpf_trampoline_link_prog(struct bpf_prog *prog, struct bpf_trampoline *tr) err = -E2BIG ; goto out; } -if (!hlist_unhashed (&prog->aux->tramp_hlist)) { +if (!hlist_unhashed (&node->hlist_tramp)) {/* prog already linked */ err = -EBUSY ; goto out; } -hlist_add_head (&prog->aux->tramp_hlist, &tr->progs_hlist[kind]); +hlist_add_head (&node->hlist_tramp, &tr->progs_hlist[kind]); tr->progs_cnt[kind]++; err =bpf_trampoline_update (tr);if (err) { -hlist_del_init (&prog->aux->tramp_hlist); +hlist_del_init (&node->hlist_tramp); tr->progs_cnt[kind]--; } out:
@@ -504,8 +505,9 @@ int bpf_trampoline_link_prog(struct bpf_prog *prog, struct bpf_trampoline *tr) }/* bpf_trampoline_unlink_prog() should never fail. */ -int bpf_trampoline_unlink_prog (struct bpf_prog *prog,struct bpf_trampoline *tr) +int bpf_trampoline_unlink_prog (struct bpf_tramp_node *node,struct bpf_trampoline *tr) { +struct bpf_prog *prog = node->prog;enum bpf_tramp_prog_type kind;int err;
@@ -518,7 +520,7 @@ int bpf_trampoline_unlink_prog(struct bpf_prog *prog, struct bpf_trampoline *tr) tr->extension_prog =NULL ; goto out; } -hlist_del_init (&prog->aux->tramp_hlist); +hlist_del_init (&node->hlist_tramp); tr->progs_cnt[kind]--; err =bpf_trampoline_update (tr); out:
Signature · 1 line
2.31.1
PATCH v5 13/29 bpf: Add bpf_tramp_attach layer for trampoline attachment
In reply to Jiri OlsaAdding bpf_tramp_attach layer for trampoline attachment to have extra layer on top of the trampoline. The reason is that in following changes we will add multiple trampolines for single program and we need entity to hold them.
The api in nutshell:
- each bpf_prog holds 'bpf_tramp_attach' object, which holds
list of 'struct bpf_tramp_node' objects:
struct bpf_tramp_attach {
struct bpf_tramp_id *id;
struct hlist_head nodes;
};
This allow us to hold multiple trampolines for each program. - bpf_tramp_attach returns 'bpf_tramp_attach' object that finds trampoline for given 'id' and adds it to the attach object, no actuall program attachment is done, just trampoline allocation
- bpf_tramp_attach_link does the actual attachment of the program to trampoline
- bpf_tramp_attach_unlink unlinks all the trampolines present in the attach object
- bpf_tramp_detach frees all the trampolines in attach object
Currently there'll be only single node added in attach object.
Following patches add support for multiple id trampolines, and uses multiple nodes in attach object to hold trampoline for given program.
--- include/linux/bpf.h | 21 +++++-- kernel/bpf/core.c | 5 +- kernel/bpf/syscall.c | 61 ++++++++++---------- kernel/bpf/trampoline.c | 122 ++++++++++++++++++++++++++++++++-------- kernel/bpf/verifier.c | 12 ++-- 5 files changed, 156 insertions(+), 65 deletions(-)
include/linux/bpf.h +16 -5
@@ -679,7 +679,14 @@ struct bpf_tramp_id {struct bpf_tramp_node {struct hlist_node hlist_tramp; +struct hlist_node hlist_attach;struct bpf_prog *prog; +struct bpf_trampoline *tr; +}; + +struct bpf_tramp_attach { +struct bpf_tramp_id *id; +struct hlist_head nodes; };struct bpf_trampoline {
@@ -751,9 +758,14 @@ void bpf_tramp_id_init(struct bpf_tramp_id *id, struct btf *btf, u32 btf_id);int bpf_trampoline_link_prog (struct bpf_tramp_node *node,struct bpf_trampoline *tr);int bpf_trampoline_unlink_prog (struct bpf_tramp_node *node,struct bpf_trampoline *tr); -struct bpf_trampoline *bpf_trampoline_get (struct bpf_tramp_id *id, -struct bpf_attach_target_info *tgt_info);void bpf_trampoline_put (struct bpf_trampoline *tr); + +struct bpf_tramp_attach *bpf_tramp_attach (struct bpf_tramp_id *id, +struct bpf_prog *tgt_prog, +struct bpf_prog *prog); +void bpf_tramp_detach (struct bpf_tramp_attach *attach); +int bpf_tramp_attach_link (struct bpf_tramp_attach *attach); +int bpf_tramp_attach_unlink (struct bpf_tramp_attach *attach);#define BPF_DISPATCHER_INIT (_name) { \ .mutex = __MUTEX_INITIALIZER(_name.mutex), \ .func = &_name##_func, \
@@ -888,8 +900,8 @@ struct bpf_prog_aux {const struct bpf_ctx_arg_aux *ctx_arg_info;struct mutex dst_mutex;/* protects dst_* pointers below, *after* prog becomes visible */ struct bpf_prog *dst_prog; -struct bpf_trampoline *dst_trampoline; -struct bpf_trampoline *trampoline; +struct bpf_tramp_attach *dst_attach; +struct bpf_tramp_attach *attach;enum bpf_prog_type saved_dst_prog_type;enum bpf_attach_type saved_dst_attach_type;bool verifier_zext;/* Zero extensions has been inserted by verifier. */
@@ -899,7 +911,6 @@ struct bpf_prog_aux {bool sleepable;bool tail_call_reachable;bool multi_func; -struct bpf_tramp_node tramp_node;/* BTF_KIND_FUNC_PROTO for valid attach_btf_id */ const struct btf_type *attach_func_proto;/* function name for valid attach_btf_id */
kernel/bpf/core.c +2 -3
@@ -105,7 +105,6 @@ struct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flag fp->aux = aux; fp->aux->prog = fp; fp->jit_requested =ebpf_jit_enabled (); - fp->aux->tramp_node.prog = fp;INIT_LIST_HEAD_RCU (&fp->aux->ksym.lnode);mutex_init (&fp->aux->used_maps_mutex);
@@ -2284,8 +2283,8 @@ static void bpf_prog_free_deferred(struct work_struct *work)if (aux->prog->has_callchain_buf)put_callchain_buffers ();#endif -if (aux->dst_trampoline) -bpf_trampoline_put (aux->dst_trampoline); +if (aux->dst_attach) +bpf_tramp_detach (aux->dst_attach);for (i =0 ; i < aux->func_cnt; i++) {/* We can just unlink the subprog poke descriptor table as * it was originally linked to the main program and is also
kernel/bpf/syscall.c +31 -30
161 lines not on this page · raw message
kernel/bpf/trampoline.c +100 -22
151 lines not on this page · raw message
kernel/bpf/verifier.c +7 -5
@@ -13928,7 +13928,7 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)struct bpf_prog *tgt_prog = prog->aux->dst_prog;struct bpf_attach_target_info tgt_info = {};u32 btf_id = prog->aux->attach_btf_id; -struct bpf_trampoline *tr; +struct bpf_tramp_attach *attach;struct bpf_tramp_id *id;int ret;
@@ -14000,13 +14000,15 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)return -ENOMEM ;bpf_tramp_id_init (id, tgt_prog, prog->aux->attach_btf, btf_id); - tr =bpf_trampoline_get (id, &tgt_info); -if (!tr) { + id->addr = (void *) tgt_info.tgt_addr; + + attach =bpf_tramp_attach (id, tgt_prog, prog); +if (IS_ERR (attach)) {bpf_tramp_id_free (id); -return -ENOMEM ; +return PTR_ERR (attach); } - prog->aux->dst_trampoline = tr; + prog->aux->dst_attach = attach;return 0 ; }
Signature · 1 line
2.31.1
PATCH v5 15/29 bpf: Add support to store multiple addrs in bpf_tramp_id object
In reply to Jiri OlsaAdding support to store multiple addrs in bpf_tramp_id object, to provide address values for id values stored in the object.
The id->addr[idx] returns address value for id->id[idx] id.
--- include/linux/bpf.h | 2 +- kernel/bpf/syscall.c | 2 +- kernel/bpf/trampoline.c | 20 ++++++++++++-------- kernel/bpf/verifier.c | 2 +- 4 files changed, 15 insertions(+), 11 deletions(-)
include/linux/bpf.h +1 -1
@@ -676,7 +676,7 @@ struct bpf_tramp_id {u32 cnt;u32 obj_id;u32 *id; -void *addr; +void **addr; };struct bpf_tramp_node {
kernel/bpf/syscall.c +1 -1
@@ -2853,7 +2853,7 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog,if (err) goto out_unlock; - id->addr = (void *) tgt_info.tgt_addr; + id->addr[0 ] = (void *) tgt_info.tgt_addr; attach =bpf_tramp_attach (id, tgt_prog, prog);if (IS_ERR (attach)) {
kernel/bpf/trampoline.c +12 -8
@@ -92,7 +92,10 @@ struct bpf_tramp_id *bpf_tramp_id_alloc(u32 max) id =kzalloc (sizeof (*id),GFP_KERNEL );if (id) { id->id =kzalloc (sizeof (u32) * max,GFP_KERNEL ); -if (!id->id) { + id->addr =kzalloc (sizeof (*id->addr) * max,GFP_KERNEL ); +if (!id->id || !id->addr) { +kfree (id->id); +kfree (id->addr);kfree (id);return NULL ; }
@@ -117,6 +120,7 @@ void bpf_tramp_id_free(struct bpf_tramp_id *id) {if (!id)return ; +kfree (id->addr);kfree (id->id);kfree (id); }
@@ -159,7 +163,7 @@ static int bpf_trampoline_module_get(struct bpf_trampoline *tr)int err =0 ;preempt_disable (); - mod =__module_text_address ((unsigned long ) tr->id->addr); + mod =__module_text_address ((unsigned long ) tr->id->addr[0 ]);if (mod && !try_module_get (mod)) err = -ENOENT ;preempt_enable ();
@@ -187,7 +191,7 @@ static int is_ftrace_location(void *ip)static int unregister_fentry (struct bpf_trampoline *tr,void *old_addr) { -void *ip = tr->id->addr; +void *ip = tr->id->addr[0 ];int ret;if (tr->func.ftrace_managed)
@@ -202,7 +206,7 @@ static int unregister_fentry(struct bpf_trampoline *tr, void *old_addr)static int modify_fentry (struct bpf_trampoline *tr,void *old_addr,void *new_addr) { -void *ip = tr->id->addr; +void *ip = tr->id->addr[0 ];int ret;if (tr->func.ftrace_managed)
@@ -215,7 +219,7 @@ static int modify_fentry(struct bpf_trampoline *tr, void *old_addr, void *new_ad/* first time registering */ static int register_fentry (struct bpf_trampoline *tr,void *new_addr) { -void *ip = tr->id->addr; +void *ip = tr->id->addr[0 ];int ret; ret =is_ftrace_location (ip);
@@ -434,7 +438,7 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr) err =arch_prepare_bpf_trampoline (im, im->image, im->image +PAGE_SIZE , &tr->func.model, flags, tprogs, - tr->id->addr); + tr->id->addr[0 ]);if (err <0 ) goto out;
@@ -503,7 +507,7 @@ int bpf_trampoline_link_prog(struct bpf_tramp_node *node, struct bpf_trampoline goto out; } tr->extension_prog = prog; - err =bpf_arch_text_poke (tr->id->addr,BPF_MOD_JUMP ,NULL , + err =bpf_arch_text_poke (tr->id->addr[0 ],BPF_MOD_JUMP ,NULL , prog->bpf_func); goto out; }
@@ -539,7 +543,7 @@ int bpf_trampoline_unlink_prog(struct bpf_tramp_node *node, struct bpf_trampolinmutex_lock (&tr->mutex);if (kind ==BPF_TRAMP_REPLACE ) {WARN_ON_ONCE (!tr->extension_prog); - err =bpf_arch_text_poke (tr->id->addr,BPF_MOD_JUMP , + err =bpf_arch_text_poke (tr->id->addr[0 ],BPF_MOD_JUMP , tr->extension_prog->bpf_func,NULL ); tr->extension_prog =NULL ; goto out;
kernel/bpf/verifier.c +1 -1
@@ -14000,7 +14000,7 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)return -ENOMEM ;bpf_tramp_id_init (id, tgt_prog, prog->aux->attach_btf, btf_id); - id->addr = (void *) tgt_info.tgt_addr; + id->addr[0 ] = (void *) tgt_info.tgt_addr; attach =bpf_tramp_attach (id, tgt_prog, prog);if (IS_ERR (attach)) {
Signature · 1 line
2.31.1
PATCH v5 14/29 bpf: Add support to store multiple ids in bpf_tramp_id object
In reply to Jiri OlsaAdding support to store multiple ids in bpf_tramp_id object, to have id for trampolines with multiple functions assigned.
Extra array of u32 values is allocated within bpf_tramp_id object allocation.
--- include/linux/bpf.h | 6 ++++-- kernel/bpf/syscall.c | 6 +++--- kernel/bpf/trampoline.c | 39 +++++++++++++++++++++++++++++++-------- kernel/bpf/verifier.c | 2 +- 4 files changed, 39 insertions(+), 14 deletions(-)
include/linux/bpf.h +4 -2
@@ -672,8 +672,10 @@ struct bpf_tramp_image { };struct bpf_tramp_id { +u32 max; +u32 cnt;u32 obj_id; -u32 btf_id; +u32 *id;void *addr; };
@@ -749,7 +751,7 @@ static __always_inline __nocfi unsigned int bpf_dispatcher_nop_func(return bpf_func (ctx, insnsi); }#ifdef CONFIG_BPF_JIT -struct bpf_tramp_id *bpf_tramp_id_alloc (void ); +struct bpf_tramp_id *bpf_tramp_id_alloc (u32 cnt);void bpf_tramp_id_free (struct bpf_tramp_id *id);bool bpf_tramp_id_is_empty (struct bpf_tramp_id *id);int bpf_tramp_id_is_equal (struct bpf_tramp_id *a,struct bpf_tramp_id *b);
kernel/bpf/syscall.c +3 -3
@@ -2704,7 +2704,7 @@ static int bpf_tracing_link_fill_link_info(const struct bpf_link *link, info->tracing.attach_type = tr_link->attach_type; info->tracing.target_obj_id = attach->id->obj_id; - info->tracing.target_btf_id = attach->id->btf_id; + info->tracing.target_btf_id = attach->id->id[0 ];return 0 ; }
@@ -2766,7 +2766,7 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog, goto out_put_prog; } - id =bpf_tramp_id_alloc (); + id =bpf_tramp_id_alloc (1 );if (!id) { err = -ENOMEM ; goto out_put_prog;
@@ -2829,7 +2829,7 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog, goto out_unlock; } - id =bpf_tramp_id_alloc (); + id =bpf_tramp_id_alloc (1 );if (!id) { err = -ENOMEM ; goto out_unlock;
kernel/bpf/trampoline.c +31 -8
@@ -60,27 +60,45 @@ void bpf_image_ksym_del(struct bpf_ksym *ksym) PAGE_SIZE, true, ksym->name); } +static bool bpf_tramp_id_is_multi (struct bpf_tramp_id *id) +{ +return id->cnt >1 ; +} +static u64 bpf_tramp_id_key (struct bpf_tramp_id *id) { -return ((u64 ) id->obj_id <<32 ) | id->btf_id; +if (bpf_tramp_id_is_multi (id)) +return (u64) &id; +else +return ((u64 ) id->obj_id <<32 ) | id->id[0 ]; }bool bpf_tramp_id_is_empty (struct bpf_tramp_id *id) { -return !id || (!id->obj_id && !id->btf_id); +return !id || id->cnt ==0 ; }int bpf_tramp_id_is_equal (struct bpf_tramp_id *a,struct bpf_tramp_id *b) { -return !memcmp (a, b,sizeof (*a)); +return a->obj_id == b->obj_id && a->cnt == b->cnt && + !memcmp (a->id, b->id, a->cnt *sizeof (*a->id)); } -struct bpf_tramp_id *bpf_tramp_id_alloc (void ) +struct bpf_tramp_id *bpf_tramp_id_alloc (u32 max) {struct bpf_tramp_id *id; -return kzalloc (sizeof (*id),GFP_KERNEL ); + id =kzalloc (sizeof (*id),GFP_KERNEL ); +if (id) { + id->id =kzalloc (sizeof (u32) * max,GFP_KERNEL ); +if (!id->id) { +kfree (id); +return NULL ; + } + id->max = max; + } +return id; } void bpf_tramp_id_init(struct bpf_tramp_id *id,
@@ -91,11 +109,15 @@ void bpf_tramp_id_init(struct bpf_tramp_id *id, id->obj_id = tgt_prog->aux->id; else id->obj_id = btf_obj_id(btf); - id->btf_id = btf_id; + id->id[0 ] = btf_id; + id->cnt =1 ; }void bpf_tramp_id_free (struct bpf_tramp_id *id) { +if (!id) +return ; +kfree (id->id);kfree (id); }
@@ -362,7 +384,8 @@ bpf_tramp_image_alloc(struct bpf_tramp_id *id, u32 idx) ksym = &im->ksym;INIT_LIST_HEAD_RCU (&ksym->lnode); key =bpf_tramp_id_key (id); -snprintf (ksym->name,KSYM_NAME_LEN ,"bpf_trampoline_%llu_%u" , key, idx); +snprintf (ksym->name,KSYM_NAME_LEN ,"bpf_trampoline_%llu_%u%s" , key, idx, +bpf_tramp_id_is_multi (id) ?"_multi" :"" );bpf_image_ksym_add (image, ksym);return im;
@@ -597,7 +620,7 @@ struct bpf_tramp_attach *bpf_tramp_attach(struct bpf_tramp_id *id,if (!node) goto out; - err =bpf_check_attach_model (prog, tgt_prog, id->btf_id, &tr->func.model); + err =bpf_check_attach_model (prog, tgt_prog, id->id[0 ], &tr->func.model);if (err) goto out;
kernel/bpf/verifier.c +1 -1
@@ -13995,7 +13995,7 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)return -EINVAL ; } - id =bpf_tramp_id_alloc (); + id =bpf_tramp_id_alloc (1 );if (!id)return -ENOMEM ;
Signature · 1 line
2.31.1
PATCH v5 16/29 bpf: Add bpf_tramp_id_single function
In reply to Jiri OlsaAdding bpf_tramp_id_single function as interface to create trampoline with single ID and grouping together the trampoline allocation with init that is used on several places and save us few lines.
--- include/linux/bpf.h | 5 ++--- kernel/bpf/syscall.c | 18 +++++++----------- kernel/bpf/trampoline.c | 11 ++++++++--- kernel/bpf/verifier.c | 3 +-- 4 files changed, 18 insertions(+), 19 deletions(-)
include/linux/bpf.h +2 -3
@@ -755,9 +755,8 @@ struct bpf_tramp_id *bpf_tramp_id_alloc(u32 cnt);void bpf_tramp_id_free (struct bpf_tramp_id *id);bool bpf_tramp_id_is_empty (struct bpf_tramp_id *id);int bpf_tramp_id_is_equal (struct bpf_tramp_id *a,struct bpf_tramp_id *b); -void bpf_tramp_id_init (struct bpf_tramp_id *id, -const struct bpf_prog *tgt_prog, -struct btf *btf,u32 btf_id); +struct bpf_tramp_id *bpf_tramp_id_single (const struct bpf_prog *tgt_prog, +struct btf *btf,u32 btf_id);int bpf_trampoline_link_prog (struct bpf_tramp_node *node,struct bpf_trampoline *tr);int bpf_trampoline_unlink_prog (struct bpf_tramp_node *node,struct bpf_trampoline *tr);void bpf_trampoline_put (struct bpf_trampoline *tr);
kernel/bpf/syscall.c +7 -11
@@ -2766,12 +2766,6 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog, goto out_put_prog; } - id =bpf_tramp_id_alloc (1 ); -if (!id) { - err = -ENOMEM ; - goto out_put_prog; - } - tgt_prog =bpf_prog_get (tgt_prog_fd);if (IS_ERR (tgt_prog)) { err =PTR_ERR (tgt_prog);
@@ -2779,7 +2773,11 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog, goto out_put_prog; } -bpf_tramp_id_init (id, tgt_prog,NULL , btf_id); + id =bpf_tramp_id_single (tgt_prog,NULL , btf_id); +if (!id) { + err = -ENOMEM ; + goto out_put_prog; + } } link =kzalloc (sizeof (*link),GFP_USER );
@@ -2829,14 +2827,12 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog, goto out_unlock; } - id =bpf_tramp_id_alloc (1 ); + btf_id = prog->aux->attach_btf_id; + id =bpf_tramp_id_single (NULL , prog->aux->attach_btf, btf_id);if (!id) { err = -ENOMEM ; goto out_unlock; } - - btf_id = prog->aux->attach_btf_id; -bpf_tramp_id_init (id,NULL , prog->aux->attach_btf, btf_id); } if (!prog->aux->dst_attach ||
kernel/bpf/trampoline.c +8 -3
@@ -104,16 +104,21 @@ struct bpf_tramp_id *bpf_tramp_id_alloc(u32 max)return id; } -void bpf_tramp_id_init (struct bpf_tramp_id *id, -const struct bpf_prog *tgt_prog, -struct btf *btf,u32 btf_id) +struct bpf_tramp_id *bpf_tramp_id_single (const struct bpf_prog *tgt_prog, +struct btf *btf,u32 btf_id) { +struct bpf_tramp_id *id; + + id =bpf_tramp_id_alloc (1 ); +if (!id) +return NULL ;if (tgt_prog) id->obj_id = tgt_prog->aux->id;else id->obj_id =btf_obj_id (btf); id->id[0 ] = btf_id; id->cnt =1 ; +return id; }void bpf_tramp_id_free (struct bpf_tramp_id *id)
kernel/bpf/verifier.c +1 -2
@@ -13995,11 +13995,10 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)return -EINVAL ; } - id =bpf_tramp_id_alloc (1 ); + id =bpf_tramp_id_single (NULL , prog->aux->attach_btf, btf_id);if (!id)return -ENOMEM ; -bpf_tramp_id_init (id, tgt_prog, prog->aux->attach_btf, btf_id); id->addr[0 ] = (void *) tgt_info.tgt_addr; attach =bpf_tramp_attach (id, tgt_prog, prog);
Signature · 1 line
2.31.1
PATCH v5 17/29 bpf: Resolve id in bpf_tramp_id_single
In reply to Jiri OlsaMoving the id resolving in the bpf_tramp_id_single function so it's centralized together with the trampoline's allocation and init.
--- include/linux/bpf.h | 3 ++- kernel/bpf/syscall.c | 21 ++++++--------------- kernel/bpf/trampoline.c | 18 +++++++++++++++--- kernel/bpf/verifier.c | 4 +--- 4 files changed, 24 insertions(+), 22 deletions(-)
include/linux/bpf.h +2 -1
@@ -756,7 +756,8 @@ void bpf_tramp_id_free(struct bpf_tramp_id *id);bool bpf_tramp_id_is_empty (struct bpf_tramp_id *id);int bpf_tramp_id_is_equal (struct bpf_tramp_id *a,struct bpf_tramp_id *b);struct bpf_tramp_id *bpf_tramp_id_single (const struct bpf_prog *tgt_prog, -struct btf *btf,u32 btf_id); +struct bpf_prog *prog,u32 btf_id, +struct bpf_attach_target_info *tgt_info);int bpf_trampoline_link_prog (struct bpf_tramp_node *node,struct bpf_trampoline *tr);int bpf_trampoline_unlink_prog (struct bpf_tramp_node *node,struct bpf_trampoline *tr);void bpf_trampoline_put (struct bpf_trampoline *tr);
kernel/bpf/syscall.c +6 -15
@@ -2773,9 +2773,9 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog, goto out_put_prog; } - id =bpf_tramp_id_single (tgt_prog,NULL , btf_id); -if (!id) { - err = -ENOMEM ; + id =bpf_tramp_id_single (tgt_prog, prog, btf_id,NULL ); +if (IS_ERR (id)) { + err =PTR_ERR (id); goto out_put_prog; } }
@@ -2828,9 +2828,9 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog, } btf_id = prog->aux->attach_btf_id; - id =bpf_tramp_id_single (NULL , prog->aux->attach_btf, btf_id); -if (!id) { - err = -ENOMEM ; + id =bpf_tramp_id_single (NULL , prog, btf_id,NULL ); +if (IS_ERR (id)) { + err =PTR_ERR (id); goto out_unlock; } }
@@ -2842,15 +2842,6 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog,* different from the destination specified at load time, we * need a new trampoline and a check for compatibility */ -struct bpf_attach_target_info tgt_info = {}; - - err =bpf_check_attach_target (NULL , prog, tgt_prog, btf_id, - &tgt_info); -if (err) - goto out_unlock; - - id->addr[0 ] = (void *) tgt_info.tgt_addr; - attach =bpf_tramp_attach (id, tgt_prog, prog);if (IS_ERR (attach)) { err =PTR_ERR (attach);
kernel/bpf/trampoline.c +15 -3
@@ -105,18 +105,30 @@ struct bpf_tramp_id *bpf_tramp_id_alloc(u32 max) }struct bpf_tramp_id *bpf_tramp_id_single (const struct bpf_prog *tgt_prog, -struct btf *btf,u32 btf_id) +struct bpf_prog *prog,u32 btf_id, +struct bpf_attach_target_info *tgt_info) {struct bpf_tramp_id *id; +if (!tgt_info) { +struct bpf_attach_target_info __tgt_info = {}; +int err; + + tgt_info = &__tgt_info; + err =bpf_check_attach_target (NULL , prog, tgt_prog, btf_id, + tgt_info); +if (err) +return ERR_PTR (err); + } id =bpf_tramp_id_alloc (1 );if (!id) -return NULL ; +return ERR_PTR (-ENOMEM );if (tgt_prog) id->obj_id = tgt_prog->aux->id;else - id->obj_id =btf_obj_id (btf); + id->obj_id =btf_obj_id (prog->aux->attach_btf); id->id[0 ] = btf_id; + id->addr[0 ] = (void *) tgt_info->tgt_addr; id->cnt =1 ;return id; }
kernel/bpf/verifier.c +1 -3
@@ -13995,12 +13995,10 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)return -EINVAL ; } - id =bpf_tramp_id_single (NULL , prog->aux->attach_btf, btf_id); + id =bpf_tramp_id_single (tgt_prog, prog, btf_id, &tgt_info);if (!id)return -ENOMEM ; - id->addr[0 ] = (void *) tgt_info.tgt_addr; - attach =bpf_tramp_attach (id, tgt_prog, prog);if (IS_ERR (attach)) {bpf_tramp_id_free (id);
Signature · 1 line
2.31.1
PATCH v5 18/29 bpf: Add refcount_t to struct bpf_tramp_id
In reply to Jiri OlsaAdding refcount_t to struct bpf_tramp_id so we can track its allocation and safely use one object on more places in following changes.
--- include/linux/bpf.h | 3 +++ kernel/bpf/syscall.c | 2 +- kernel/bpf/trampoline.c | 16 +++++++++++++--- kernel/bpf/verifier.c | 2 +- 4 files changed, 18 insertions(+), 5 deletions(-)
include/linux/bpf.h +3 -0
@@ -23,6 +23,7 @@#include <linux/slab.h> #include <linux/percpu-refcount.h> #include <linux/bpfptr.h> +#include <linux/refcount.h> struct bpf_verifier_env ;struct bpf_verifier_log ;
@@ -677,6 +678,7 @@ struct bpf_tramp_id {u32 obj_id;u32 *id;void **addr; +refcount_t refcnt; };struct bpf_tramp_node {
@@ -753,6 +755,7 @@ static __always_inline __nocfi unsigned int bpf_dispatcher_nop_func(#ifdef CONFIG_BPF_JIT struct bpf_tramp_id *bpf_tramp_id_alloc (u32 cnt);void bpf_tramp_id_free (struct bpf_tramp_id *id); +void bpf_tramp_id_put (struct bpf_tramp_id *id);bool bpf_tramp_id_is_empty (struct bpf_tramp_id *id);int bpf_tramp_id_is_equal (struct bpf_tramp_id *a,struct bpf_tramp_id *b); struct bpf_tramp_id *bpf_tramp_id_single(const struct bpf_prog *tgt_prog,
kernel/bpf/syscall.c +1 -1
@@ -2901,7 +2901,7 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog, out_put_prog:if (tgt_prog_fd && tgt_prog)bpf_prog_put (tgt_prog); -bpf_tramp_id_free (id); +bpf_tramp_id_put (id);return err; }
kernel/bpf/trampoline.c +13 -3
@@ -100,6 +100,7 @@ struct bpf_tramp_id *bpf_tramp_id_alloc(u32 max)return NULL ; } id->max = max; +refcount_set (&id->refcnt,1 ); }return id; }
@@ -133,10 +134,18 @@ struct bpf_tramp_id *bpf_tramp_id_single(const struct bpf_prog *tgt_prog,return id; } -void bpf_tramp_id_free (struct bpf_tramp_id *id) +static struct bpf_tramp_id *bpf_tramp_id_get (struct bpf_tramp_id *id) +{ +refcount_inc (&id->refcnt); +return id; +} + +void bpf_tramp_id_put (struct bpf_tramp_id *id) {if (!id)return ; +if (!refcount_dec_and_test (&id->refcnt)) +return ;kfree (id->addr);kfree (id->id);kfree (id);
@@ -162,7 +171,7 @@ static struct bpf_trampoline *bpf_trampoline_get(struct bpf_tramp_id *id)if (!tr) goto out; - tr->id = id; + tr->id =bpf_tramp_id_get (id);INIT_HLIST_NODE (&tr->hlist);hlist_add_head (&tr->hlist, head);refcount_set (&tr->refcnt,1 );
@@ -592,6 +601,7 @@ void bpf_trampoline_put(struct bpf_trampoline *tr)* multiple rcu callbacks. */ hlist_del (&tr->hlist); +bpf_tramp_id_put (tr->id);kfree (tr); out:mutex_unlock (&trampoline_mutex);
@@ -663,7 +673,7 @@ void bpf_tramp_detach(struct bpf_tramp_attach *attach)hlist_for_each_entry_safe (node, n, &attach->nodes, hlist_attach)node_free (node); -bpf_tramp_id_free (attach->id); +bpf_tramp_id_put (attach->id);kfree (attach); }
kernel/bpf/verifier.c +1 -1
@@ -14001,7 +14001,7 @@ static int check_attach_btf_id(struct bpf_verifier_env *env) attach =bpf_tramp_attach (id, tgt_prog, prog);if (IS_ERR (attach)) { -bpf_tramp_id_free (id); +bpf_tramp_id_put (id);return PTR_ERR (attach); }
Signature · 1 line
2.31.1
PATCH v5 19/29 bpf: Add support to attach trampolines with multiple IDs
In reply to Jiri OlsaAdding support to attach trampolines with multiple IDs.
This patch adds support to bpf_tramp_attach function to attach given program to bpf_tramp_id object that holds multiple BTF function IDs.
The process of attaching in bpf_tramp_attach is as follows:
- IDs in bpf_tramp_id object are sorted out to several new bpf_tramp_id objects based on number of arguments of each ID - so we end up with up to 6 bpf_tramp_id objects, that we will create or find suitable trampoline for
- separating function IDs that have same number of arguments save us troubles of handling different argument counts within one trampoline
- now for each such bpf_tramp_id object we do following:
* search existing trampolines to find match or intersection
* if there's full match on IDs, we add program to existing trampoline and we are done
* if there's intersection with existing trampoline, we split it and add new program to the common part, the rest of the IDs are attached to new trampoline
- we keep trampoline_table as place holder for all trampolines, (while the has works only for single ID trampolines) so in case there is no multi-id trampoline defined, we still use the fast hash trampoline lookup
The bpf_tramp_attach assumes ID array is coming in sorted so it's possible to run bsearch on it to do all the needed searches.
The splitting of the trampoline use the fact that we carry 'bpf_tramp_attach' object for each bpf_program, so when we split trampoline that the program is attached to, we just add new 'bpf_tramp_node' object to the program's attach 'nodes'. This way we keep track of all program's trampolines and it will be properly detached when the program goes away.
The splitting of the trampoline is done with following steps:
- lock the trampoline
- unregister trampoline
- alloc the duplicate, which means that for all attached programs of the original trampoline we create new bpf_tramp_node objects and add them to these programs' attach objects
- then we assign new IDs (common and the rest) to both (original and the duplicated) trampolines
- register both trampolines
- unlock the original trampoline
This patch only adds bpf_tramp_attach support to attach multiple ID bpf_tramp_id object. The actual user interface for that comes in following patch.
Now when each call to bpf_tramp_attach can change any program's attach object, we need to take trampoline_mutex in both bpf_tramp_attach_link and bpf_tramp_attach_unlink functions. Perhaps we could add new lock to bpf_tramp_attach object to get rid of single lock for all.
--- include/linux/bpf.h | 12 +- kernel/bpf/trampoline.c | 717 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 674 insertions(+), 55 deletions(-)
include/linux/bpf.h +10 -2
@@ -24,6 +24,13 @@#include <linux/percpu-refcount.h> #include <linux/bpfptr.h> #include <linux/refcount.h> +#ifdef CONFIG_FUNCTION_TRACER +#ifndef CC_USING_FENTRY +#define CC_USING_FENTRY +#endif +#endif +#include <linux/ftrace.h> +#include <linux/types.h> struct bpf_verifier_env ;struct bpf_verifier_log ;
@@ -703,6 +710,9 @@ struct bpf_trampoline {struct {struct btf_func_model model;bool ftrace_managed; +#ifdef CONFIG_FUNCTION_TRACER +struct ftrace_ops ops; +#endif } func;/* if !NULL this is BPF_PROG_TYPE_EXT program that extends another BPF * program by replacing one of its functions. id->addr is the address
@@ -763,7 +773,6 @@ struct bpf_tramp_id *bpf_tramp_id_single(const struct bpf_prog *tgt_prog, struct bpf_attach_target_info *tgt_info);int bpf_trampoline_link_prog (struct bpf_tramp_node *node,struct bpf_trampoline *tr);int bpf_trampoline_unlink_prog (struct bpf_tramp_node *node,struct bpf_trampoline *tr); -void bpf_trampoline_put (struct bpf_trampoline *tr); struct bpf_tramp_attach *bpf_tramp_attach(struct bpf_tramp_id *id, struct bpf_prog *tgt_prog,
@@ -831,7 +840,6 @@ static inline struct bpf_trampoline *bpf_trampoline_get(struct bpf_tramp_id *id, {return ERR_PTR (-EOPNOTSUPP ); } -static inline void bpf_trampoline_put (struct bpf_trampoline *tr) {}#define DEFINE_BPF_DISPATCHER (name)#define DECLARE_BPF_DISPATCHER (name)#define BPF_DISPATCHER_FUNC (name) bpf_dispatcher_nop_func
kernel/bpf/trampoline.c +664 -53
876 lines not on this page · raw message
Signature · 1 line
2.31.1
PATCH v5 20/29 bpf: Add support for tracing multi link
In reply to Jiri OlsaAdding new link to allow to attach program to multiple function BTF IDs.
New fields are added to bpf_attr::link_create to pass array of BTF IDs:
struct {
__aligned_u64 btf_ids; /* addresses to attach */
__u32 btf_ids_cnt; /* addresses count */
} multi;The new link code will load these IDs into bpf_tramp_id and resolve their ips.
The resolve itself is done as per Andrii's suggestion:
- lookup all names for given IDs
- store and sort them by name
- go through all kallsyms symbols and use bsearch to find it in provided names
- if name is found, store the address for the name
- resort the names array based on ID
If there are multi symbols of the same name the first one will be used to resolve the address.
The new link will pass them to the bpf_tramp_attach that does all the work of attaching.
--- include/uapi/linux/bpf.h | 5 + kernel/bpf/syscall.c | 252 +++++++++++++++++++++++++++++++++ kernel/kallsyms.c | 2 +- tools/include/uapi/linux/bpf.h | 5 + 4 files changed, 263 insertions(+), 1 deletion(-)
include/uapi/linux/bpf.h +5 -0
@@ -1009,6 +1009,7 @@ enum bpf_link_type {BPF_LINK_TYPE_NETNS =5 ,BPF_LINK_TYPE_XDP =6 ,BPF_LINK_TYPE_PERF_EVENT =7 , +BPF_LINK_TYPE_TRACING_MULTI =8 , MAX_BPF_LINK_TYPE, };
@@ -1470,6 +1471,10 @@ union bpf_attr {*/ __u64 bpf_cookie; } perf_event; +struct { +__aligned_u64 btf_ids;/* addresses to attach */ +__u32 btf_ids_cnt;/* addresses count */ + } multi; }; } link_create;
kernel/bpf/syscall.c +252 -0
276 lines not on this page · raw message
kernel/kallsyms.c +1 -1
@@ -224,7 +224,7 @@ unsigned long kallsyms_lookup_name(const char *name)return module_kallsyms_lookup_name (name); } -#ifdef CONFIG_LIVEPATCH +#if defined(CONFIG_LIVEPATCH ) || defined(CONFIG_BPF )/* * Iterate over all symbols in vmlinux. For symbols from modules use * module_kallsyms_on_each_symbol instead.
tools/include/uapi/linux/bpf.h +5 -0
@@ -1009,6 +1009,7 @@ enum bpf_link_type {BPF_LINK_TYPE_NETNS =5 ,BPF_LINK_TYPE_XDP =6 ,BPF_LINK_TYPE_PERF_EVENT =7 , +BPF_LINK_TYPE_TRACING_MULTI =8 , MAX_BPF_LINK_TYPE, };
@@ -1470,6 +1471,10 @@ union bpf_attr {*/ __u64 bpf_cookie; } perf_event; +struct { +__aligned_u64 btf_ids;/* addresses to attach */ +__u32 btf_ids_cnt;/* addresses count */ + } multi; }; } link_create;
Signature · 1 line
2.31.1
PATCH v5 21/29 libbpf: Add btf__find_by_glob_kind function
In reply to Jiri OlsaAdding btf__find_by_glob_kind function that returns array of BTF ids that match given kind and allow/deny patterns.
int btf__find_by_glob_kind(const struct btf *btf, __u32 kind,
const char *allow_pattern, const char *deny_pattern, __u32 **__ids);
The __ids array is allocated and needs to be manually freed.
At the moment the supported pattern is '*' at the beginning or the end of the pattern.
Kindly borrowed from retsnoop.
--- tools/lib/bpf/btf.c | 77 +++++++++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/btf.h | 3 ++ 2 files changed, 80 insertions(+)
tools/lib/bpf/btf.c +77 -0
@@ -749,6 +749,83 @@ __s32 btf__find_by_name_kind(const struct btf *btf, const char *type_name,return btf_find_by_name_kind (btf,1 , type_name, kind); } +/* 'borrowed' from retsnoop */ +static bool glob_matches (const char *glob,const char *s) +{ +int n =strlen (glob); + +if (n ==1 && glob[0 ] =='*' ) +return true; + +if (glob[0 ] =='*' && glob[n -1 ] =='*' ) { +const char *subs; +/* substring match */ + +/* this is hacky, but we don't want to allocate for no good reason */ + ((char *)glob)[n -1 ] ='\0' ; + subs =strstr (s, glob +1 ); + ((char *)glob)[n -1 ] ='*' ; + +return subs !=NULL ; + }else if (glob[0 ] =='*' ) { +size_t nn =strlen (s); +/* suffix match */ + +/* too short for a given suffix */ +if (nn < n -1 ) +return false; + +return strcmp (s + nn - (n -1 ), glob +1 ) ==0 ; + }else if (glob[n -1 ] =='*' ) { +/* prefix match */ +return strncmp (s, glob, n -1 ) ==0 ; + }else { +/* exact match */ +return strcmp (glob, s) ==0 ; + } +} + +int btf__find_by_glob_kind (const struct btf *btf,__u32 kind, +const char *allow_pattern,const char *deny_pattern, +__u32 **__ids) +{ +__u32 i, nr_types =btf__get_nr_types (btf); +int cnt =0 , alloc =0 ; +__u32 *ids =NULL ; + +for (i =1 ; i <= nr_types; i++) { +const struct btf_type *t =btf__type_by_id (btf, i); +const char *name; +__u32 *p; + +if (btf_kind (t) != kind) +continue ; + name =btf__name_by_offset (btf, t->name_off); +if (!name) +continue ; + +if (deny_pattern &&glob_matches (deny_pattern, name)) +continue ; +if (allow_pattern && !glob_matches (allow_pattern, name)) +continue ; + +if (cnt == alloc) { + alloc =max (16 , alloc *3 /2 ); + p =libbpf_reallocarray (ids, alloc,sizeof (__u32)); +if (!p) { +free (ids); +return -ENOMEM ; + } + ids = p; + } + ids[cnt] = i; + cnt++; + } + + *__ids = ids; +return cnt; +} +static bool btf_is_modifiable (const struct btf *btf) {return (void *)btf->hdr != btf->raw_data;
tools/lib/bpf/btf.h +3 -0
@@ -572,6 +572,9 @@ static inline struct btf_decl_tag *btf_decl_tag(const struct btf_type *t)return (struct btf_decl_tag *)(t +1 ); } +int btf__find_by_glob_kind (const struct btf *btf,__u32 kind, +const char *allow_pattern,const char *deny_pattern, +__u32 **__ids); #ifdef __cplusplus }/* extern "C" */ #endif
Signature · 1 line
2.31.1
PATCH v5 22/29 libbpf: Add support to link multi func tracing program
In reply to Jiri OlsaAdding support to link multi func tracing program
through link_create interface.
Adding special types for multi func programs:
fentry.multi
fexit.multi
so you can define multi func programs like:
SEC("fentry.multi/bpf_fentry_test*")
int BPF_PROG(test1, __u64 a, __u64 b, __u64 c, __u64 d, __u64 e, __u64 f)
that defines test1 to be attached to bpf_fentry_test* functions. The test1 program is loaded with BPF_F_MULTI_FUNC flag.
If functions are not specified the program needs to be attached manually.
Adding new btf_ids/btf_ids_cnt fields to bpf_link_create_opts, that define functions to attach the program to.
--- tools/lib/bpf/bpf.c | 7 +++++ tools/lib/bpf/bpf.h | 6 +++- tools/lib/bpf/libbpf.c | 66 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-)
tools/lib/bpf/bpf.c +7 -0
@@ -784,6 +784,13 @@ int bpf_link_create(int prog_fd, int target_fd,if (!OPTS_ZEROED (opts, perf_event))return libbpf_err (-EINVAL );break ; +case BPF_TRACE_FENTRY : +case BPF_TRACE_FEXIT : + attr.link_create.multi.btf_ids = (__u64 )OPTS_GET (opts, multi.btf_ids,0 ); + attr.link_create.multi.btf_ids_cnt =OPTS_GET (opts, multi.btf_ids_cnt,0 ); +if (!OPTS_ZEROED (opts, multi)) +return libbpf_err (-EINVAL ); +break ;default :if (!OPTS_ZEROED (opts, flags))return libbpf_err (-EINVAL );
tools/lib/bpf/bpf.h +5 -1
@@ -249,10 +249,14 @@ struct bpf_link_create_opts {struct {__u64 bpf_cookie; } perf_event; +struct { +__u32 *btf_ids; +__u32 btf_ids_cnt; + } multi; }; size_t :0; }; -#define bpf_link_create_opts__last_field perf_event +#define bpf_link_create_opts__last_field multi LIBBPF_API int bpf_link_create(int prog_fd, int target_fd, enum bpf_attach_type attach_type,
tools/lib/bpf/libbpf.c +66 -0
102 lines not on this page · raw message
Signature · 1 line
2.31.1
PATCH v5 23/29 selftests/bpf: Add bpf_arg/bpf_ret_value test
In reply to Jiri OlsaAdding tests for bpf_arg/bpf_ret_value helpers on
both fentry and fexit programs.
--- .../selftests/bpf/prog_tests/args_test.c | 34 +++++++++++++++++++ tools/testing/selftests/bpf/progs/args_test.c | 30 ++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/args_test.c create mode 100644 tools/testing/selftests/bpf/progs/args_test.c
tools/testing/selftests/bpf/prog_tests/args_test.c new file +34 -0
@@ -0,0 +1,34 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <test_progs.h> +#include "args_test.skel.h" + +void test_args_test (void ) +{ +struct args_test *skel =NULL ; +__u32 duration =0 , retval; +int err, prog_fd; + + skel =args_test__open (); +if (!ASSERT_OK_PTR (skel,"args_test__open" )) +return ; + + err =args_test__load (skel); +if (!ASSERT_OK (err,"args_test__load" )) + goto cleanup; + + err =args_test__attach (skel); +if (!ASSERT_OK (err,"args_test__attach" )) + goto cleanup; + + prog_fd =bpf_program__fd (skel->progs.test1); + err =bpf_prog_test_run (prog_fd,1 ,NULL ,0 , +NULL ,NULL , &retval, &duration); +ASSERT_OK (err,"test_run" ); +ASSERT_EQ (retval,0 ,"test_run" ); + +ASSERT_EQ (skel->bss->test1_result,1 ,"test1_result" ); +ASSERT_EQ (skel->bss->test2_result,1 ,"test2_result" ); + +cleanup: +args_test__destroy (skel); +}
tools/testing/selftests/bpf/progs/args_test.c new file +30 -0
@@ -0,0 +1,30 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <linux/bpf.h> +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> + +char _license[] SEC("license") = "GPL"; + +__u64 test1_result =0 ; +SEC("fentry/bpf_fentry_test1") +int BPF_PROG (test1 ) +{ +__u64 a =bpf_arg (ctx,0 ); +__u64 x =bpf_arg (ctx,1 ); + + test1_result = (int ) a ==1 && x ==0 ; +return 0 ; +} + +__u64 test2_result =0 ; +SEC("fexit/bpf_fentry_test2") +int BPF_PROG (test2 ) +{ +__u64 ret =bpf_ret_value (ctx); +__u64 a =bpf_arg (ctx,0 ); +__u64 b =bpf_arg (ctx,1 ); +__u64 x =bpf_arg (ctx,2 ); + + test2_result = (int ) a ==2 && b ==3 && ret ==5 && x ==0 ; +return 0 ; +}
Signature · 1 line
2.31.1
PATCH v5 24/29 selftests/bpf: Add fentry multi func test
In reply to Jiri OlsaAdding selftest for fentry multi func test that attaches to bpf_fentry_test* functions and checks argument values based on the processed function.
We need to cast to real arguments types in multi_arg_check, because the checked value can be shorter than u64.
--- tools/testing/selftests/bpf/Makefile | 4 +- .../bpf/prog_tests/multi_fentry_test.c | 30 +++++++++ .../testing/selftests/bpf/progs/multi_check.c | 63 +++++++++++++++++++ .../selftests/bpf/progs/multi_fentry.c | 17 +++++ 4 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/multi_fentry_test.c create mode 100644 tools/testing/selftests/bpf/progs/multi_check.c create mode 100644 tools/testing/selftests/bpf/progs/multi_fentry.c
tools/testing/selftests/bpf/Makefile +3 -1
@@ -322,7 +322,8 @@ endef SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h \ - linked_vars.skel.h linked_maps.skel.h + linked_vars.skel.h linked_maps.skel.h \ + multi_fentry_test.skel.h LSKELS := kfunc_call_test.c fentry_test.c fexit_test.c fexit_sleep.c \ test_ringbuf.c atomics.c trace_printk.c trace_vprintk.c
@@ -334,6 +335,7 @@ test_static_linked.skel.h-deps := test_static_linked1.o test_static_linked2.o linked_funcs.skel.h-deps := linked_funcs1.o linked_funcs2.o linked_vars.skel.h-deps := linked_vars1.o linked_vars2.o linked_maps.skel.h-deps := linked_maps1.o linked_maps2.o +multi_fentry_test.skel.h-deps := multi_fentry.o multi_check.o LINKED_BPF_SRCS := $(patsubst %.o,%.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps)))
tools/testing/selftests/bpf/prog_tests/multi_fentry_test.c new file +30 -0
@@ -0,0 +1,30 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <test_progs.h> +#include "multi_fentry_test.skel.h" +#include "trace_helpers.h" + +void test_multi_fentry_test (void ) +{ +struct multi_fentry_test *skel =NULL ; +__u32 duration =0 , retval; +int err, prog_fd; + + skel =multi_fentry_test__open_and_load (); +if (!ASSERT_OK_PTR (skel,"fentry_multi_skel_load" )) + goto cleanup; + + err =multi_fentry_test__attach (skel); +if (!ASSERT_OK (err,"fentry_attach" )) + goto cleanup; + + prog_fd =bpf_program__fd (skel->progs.test); + err =bpf_prog_test_run (prog_fd,1 ,NULL ,0 , +NULL ,NULL , &retval, &duration); +ASSERT_OK (err,"test_run" ); +ASSERT_EQ (retval,0 ,"test_run" ); + +ASSERT_EQ (skel->bss->test_result,8 ,"test_result" ); + +cleanup: +multi_fentry_test__destroy (skel); +}
tools/testing/selftests/bpf/progs/multi_check.c new file +63 -0
63 lines not on this page · raw message
tools/testing/selftests/bpf/progs/multi_fentry.c new file +17 -0
@@ -0,0 +1,17 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <linux/bpf.h> +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> + +char _license[] SEC("license") = "GPL"; + +__u64 test_result =0 ; + +__hidden extern void multi_arg_check(__u64 *ctx, __u64 *test_result); + +SEC("fentry.multi/bpf_fentry_test*") +int BPF_PROG (test ,__u64 a,__u64 b,__u64 c,__u64 d,__u64 e,__u64 f) +{ +multi_arg_check (ctx, &test_result); +return 0 ; +}
Signature · 1 line
2.31.1
PATCH v5 25/29 selftests/bpf: Add fexit multi func test
In reply to Jiri OlsaAdding selftest for fexit multi func test that attaches to bpf_fentry_test* functions and checks argument values based on the processed function.
--- tools/testing/selftests/bpf/Makefile | 3 +- .../bpf/prog_tests/multi_fexit_test.c | 31 +++++++++++++++++++ .../testing/selftests/bpf/progs/multi_check.c | 23 ++++++++++++++ .../testing/selftests/bpf/progs/multi_fexit.c | 20 ++++++++++++ 4 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/multi_fexit_test.c create mode 100644 tools/testing/selftests/bpf/progs/multi_fexit.c
tools/testing/selftests/bpf/Makefile +2 -1
@@ -323,7 +323,7 @@ SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h \ linked_vars.skel.h linked_maps.skel.h \ - multi_fentry_test.skel.h + multi_fentry_test.skel.h multi_fexit_test.skel.h LSKELS := kfunc_call_test.c fentry_test.c fexit_test.c fexit_sleep.c \ test_ringbuf.c atomics.c trace_printk.c trace_vprintk.c
@@ -336,6 +336,7 @@ linked_funcs.skel.h-deps := linked_funcs1.o linked_funcs2.o linked_vars.skel.h-deps := linked_vars1.o linked_vars2.o linked_maps.skel.h-deps := linked_maps1.o linked_maps2.o multi_fentry_test.skel.h-deps := multi_fentry.o multi_check.o +multi_fexit_test.skel.h-deps := multi_fexit.o multi_check.o LINKED_BPF_SRCS := $(patsubst %.o,%.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps)))
tools/testing/selftests/bpf/prog_tests/multi_fexit_test.c new file +31 -0
@@ -0,0 +1,31 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <test_progs.h> +#include "multi_fexit_test.skel.h" +#include "trace_helpers.h" + +void test_multi_fexit_test (void ) +{ +struct multi_fexit_test *skel =NULL ; +__u32 duration =0 , retval; +int err, prog_fd; + + skel =multi_fexit_test__open_and_load (); +if (!ASSERT_OK_PTR (skel,"fexit_multi_skel_load" )) + goto cleanup; + + err =multi_fexit_test__attach (skel); +if (!ASSERT_OK (err,"fexit_attach" )) + goto cleanup; + + prog_fd =bpf_program__fd (skel->progs.test); + err =bpf_prog_test_run (prog_fd,1 ,NULL ,0 , +NULL ,NULL , &retval, &duration); +ASSERT_OK (err,"test_run" ); +ASSERT_EQ (retval,0 ,"test_run" ); + +ASSERT_EQ (skel->bss->test_arg_result,8 ,"fexit_multi_arg_result" ); +ASSERT_EQ (skel->bss->test_ret_result,8 ,"fexit_multi_ret_result" ); + +cleanup: +multi_fexit_test__destroy (skel); +}
tools/testing/selftests/bpf/progs/multi_check.c +23 -0
@@ -61,3 +61,26 @@ void multi_arg_check(__u64 *ctx, __u64 *test_result) *test_result +=1 ; } } + +void multi_ret_check (void *ctx,__u64 *test_result) +{ +void *ip = (void *)bpf_get_func_ip (ctx); +int ret = (int )bpf_ret_value (ctx); + +if (ip == &bpf_fentry_test1) + *test_result += ret ==2 ; +else if (ip == &bpf_fentry_test2) + *test_result += ret ==5 ; +else if (ip == &bpf_fentry_test3) + *test_result += ret ==15 ; +else if (ip == &bpf_fentry_test4) + *test_result += ret ==34 ; +else if (ip == &bpf_fentry_test5) + *test_result += ret ==65 ; +else if (ip == &bpf_fentry_test6) + *test_result += ret ==111 ; +else if (ip == &bpf_fentry_test7) + *test_result += ret ==0 ; +else if (ip == &bpf_fentry_test8) + *test_result += ret ==0 ; +}
tools/testing/selftests/bpf/progs/multi_fexit.c new file +20 -0
@@ -0,0 +1,20 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <linux/bpf.h> +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> + +char _license[] SEC("license") = "GPL"; + +__hidden extern void multi_arg_check(__u64 *ctx, __u64 *test_result); +__hidden extern void multi_ret_check(void *ctx, __u64 *test_result); + +__u64 test_arg_result =0 ; +__u64 test_ret_result =0 ; + +SEC("fexit.multi/bpf_fentry_test*") +int BPF_PROG (test ) +{ +multi_arg_check (ctx, &test_arg_result); +multi_ret_check (ctx, &test_ret_result); +return 0 ; +}
Signature · 1 line
2.31.1
PATCH v5 26/29 selftests/bpf: Add fentry/fexit multi func test
In reply to Jiri OlsaAdding selftest for fentry/fexit multi func tests that attaches to bpf_fentry_test* functions and checks argument values based on the processed function.
--- tools/testing/selftests/bpf/Makefile | 4 ++- .../bpf/prog_tests/multi_fentry_fexit_test.c | 32 +++++++++++++++++++ .../selftests/bpf/progs/multi_fentry_fexit.c | 28 ++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/multi_fentry_fexit_test.c create mode 100644 tools/testing/selftests/bpf/progs/multi_fentry_fexit.c
tools/testing/selftests/bpf/Makefile +3 -1
@@ -323,7 +323,8 @@ SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h \ linked_vars.skel.h linked_maps.skel.h \ - multi_fentry_test.skel.h multi_fexit_test.skel.h + multi_fentry_test.skel.h multi_fexit_test.skel.h \ + multi_fentry_fexit_test.skel.h LSKELS := kfunc_call_test.c fentry_test.c fexit_test.c fexit_sleep.c \ test_ringbuf.c atomics.c trace_printk.c trace_vprintk.c
@@ -337,6 +338,7 @@ linked_vars.skel.h-deps := linked_vars1.o linked_vars2.o linked_maps.skel.h-deps := linked_maps1.o linked_maps2.o multi_fentry_test.skel.h-deps := multi_fentry.o multi_check.o multi_fexit_test.skel.h-deps := multi_fexit.o multi_check.o +multi_fentry_fexit_test.skel.h-deps := multi_fentry_fexit.o multi_check.o LINKED_BPF_SRCS := $(patsubst %.o,%.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps)))
tools/testing/selftests/bpf/prog_tests/multi_fentry_fexit_test.c new file +32 -0
@@ -0,0 +1,32 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <test_progs.h> +#include "multi_fentry_fexit_test.skel.h" + +void test_multi_fentry_fexit_test (void ) +{ +DECLARE_LIBBPF_OPTS (bpf_link_update_opts, link_upd_opts); +struct multi_fentry_fexit_test *skel =NULL ; +__u32 duration =0 , retval; +int err, prog_fd; + + skel =multi_fentry_fexit_test__open_and_load (); +if (!ASSERT_OK_PTR (skel,"fentry_multi_skel_load" )) + goto cleanup; + + err =multi_fentry_fexit_test__attach (skel); +if (!ASSERT_OK (err,"fentry_fexit_attach" )) + goto cleanup; + + prog_fd =bpf_program__fd (skel->progs.test2); + err =bpf_prog_test_run (prog_fd,1 ,NULL ,0 , +NULL ,NULL , &retval, &duration); +ASSERT_OK (err,"test_run" ); +ASSERT_EQ (retval,0 ,"test_run" ); + +ASSERT_EQ (skel->bss->test1_arg_result,8 ,"test1_arg_result" ); +ASSERT_EQ (skel->bss->test2_arg_result,8 ,"test2_arg_result" ); +ASSERT_EQ (skel->bss->test2_ret_result,8 ,"test2_ret_result" ); + +cleanup: +multi_fentry_fexit_test__destroy (skel); +}
tools/testing/selftests/bpf/progs/multi_fentry_fexit.c new file +28 -0
@@ -0,0 +1,28 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <linux/bpf.h> +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> + +char _license[] SEC("license") = "GPL"; + +__u64 test1_arg_result =0 ; +__u64 test2_arg_result =0 ; +__u64 test2_ret_result =0 ; + +__hidden extern void multi_arg_check(__u64 *ctx, __u64 *test_result); +__hidden extern void multi_ret_check(void *ctx, __u64 *test_result); + +SEC("fentry.multi/bpf_fentry_test*") +int BPF_PROG (test1 ,__u64 a,__u64 b,__u64 c,__u64 d,__u64 e,__u64 f) +{ +multi_arg_check (ctx, &test1_arg_result); +return 0 ; +} + +SEC("fexit.multi/bpf_fentry_test*") +int BPF_PROG (test2 ,__u64 a,__u64 b,__u64 c,__u64 d,__u64 e,__u64 f,int ret) +{ +multi_arg_check (ctx, &test2_arg_result); +multi_ret_check (ctx, &test2_ret_result); +return 0 ; +}
Signature · 1 line
2.31.1
PATCH v5 27/29 selftests/bpf: Add mixed multi func test
In reply to Jiri OlsaAdding selftest for fentry/fexit multi func tests that attaches to bpf_fentry_test* functions, where some of them have already attached trampoline.
--- tools/testing/selftests/bpf/Makefile | 3 +- .../bpf/prog_tests/multi_mixed_test.c | 34 +++++++++++++++ .../testing/selftests/bpf/progs/multi_mixed.c | 43 +++++++++++++++++++ 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/multi_mixed_test.c create mode 100644 tools/testing/selftests/bpf/progs/multi_mixed.c
tools/testing/selftests/bpf/Makefile +2 -1
@@ -324,7 +324,7 @@ SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h \ linked_vars.skel.h linked_maps.skel.h \ multi_fentry_test.skel.h multi_fexit_test.skel.h \ - multi_fentry_fexit_test.skel.h + multi_fentry_fexit_test.skel.h multi_mixed_test.skel.h LSKELS := kfunc_call_test.c fentry_test.c fexit_test.c fexit_sleep.c \ test_ringbuf.c atomics.c trace_printk.c trace_vprintk.c
@@ -339,6 +339,7 @@ linked_maps.skel.h-deps := linked_maps1.o linked_maps2.o multi_fentry_test.skel.h-deps := multi_fentry.o multi_check.o multi_fexit_test.skel.h-deps := multi_fexit.o multi_check.o multi_fentry_fexit_test.skel.h-deps := multi_fentry_fexit.o multi_check.o +multi_mixed_test.skel.h-deps := multi_mixed.o multi_check.o LINKED_BPF_SRCS := $(patsubst %.o,%.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps)))
tools/testing/selftests/bpf/prog_tests/multi_mixed_test.c new file +34 -0
@@ -0,0 +1,34 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <test_progs.h> +#include "multi_mixed_test.skel.h" + +void test_multi_mixed_test (void ) +{ +DECLARE_LIBBPF_OPTS (bpf_link_update_opts, link_upd_opts); +struct multi_mixed_test *skel =NULL ; +__u32 duration =0 , retval; +int err, prog_fd; + + skel =multi_mixed_test__open_and_load (); +if (!ASSERT_OK_PTR (skel,"fentry_multi_skel_load" )) + goto cleanup; + + err =multi_mixed_test__attach (skel); +if (!ASSERT_OK (err,"fentry_attach" )) + goto cleanup; + + prog_fd =bpf_program__fd (skel->progs.test1); + err =bpf_prog_test_run (prog_fd,1 ,NULL ,0 , +NULL ,NULL , &retval, &duration); +ASSERT_OK (err,"test_run" ); +ASSERT_EQ (retval,0 ,"test_run" ); + +ASSERT_EQ (skel->bss->test1_result,1 ,"test1_result" ); +ASSERT_EQ (skel->bss->test2_result,1 ,"test2_result" ); +ASSERT_EQ (skel->bss->test3_arg_result,8 ,"test3_arg_result" ); +ASSERT_EQ (skel->bss->test4_arg_result,8 ,"test4_arg_result" ); +ASSERT_EQ (skel->bss->test4_ret_result,8 ,"test4_ret_result" ); + +cleanup: +multi_mixed_test__destroy (skel); +}
tools/testing/selftests/bpf/progs/multi_mixed.c new file +43 -0
43 lines not on this page · raw message
Signature · 1 line
2.31.1
PATCH v5 28/29 selftests/bpf: Add ret_mod multi func test
In reply to Jiri OlsaAdding extra test to existing modify_return test to test this with multi func program attached on top of the modify return program.
Because the supported wildcards do not allow us to match both bpf_fentry_test* and bpf_modify_return_test, adding extra code to look it up in kernel's BTF.
--- .../selftests/bpf/prog_tests/modify_return.c | 114 +++++++++++++++++- .../selftests/bpf/progs/multi_modify_return.c | 17 +++ 2 files changed, 128 insertions(+), 3 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/multi_modify_return.c
tools/testing/selftests/bpf/prog_tests/modify_return.c +111 -3
@@ -5,13 +5,100 @@*/ #include <test_progs.h> +#include <bpf/btf.h> #include "modify_return.skel.h" +#include "multi_modify_return.skel.h" #define LOWER (x) ((x) & 0xffff)#define UPPER (x) ((x) >> 16) -static void run_test (__u32 input_retval,__u16 want_side_effect,__s16 want_ret) +struct multi_data { +struct multi_modify_return *skel; +int link_fentry; +int link_fexit; +__u32 btf_ids[9 ]; +}; + +static int multi_btf_ids (struct multi_data *md) +{ +__u32 i, nr_types, ids_cnt; +struct btf *btf; + + btf =btf__load_vmlinux_btf (); +if (!ASSERT_OK_PTR (btf,"btf__load_vmlinux_btf" )) +return -1 ; + + nr_types =btf__get_nr_types (btf); + +for (i =1 ; i <= nr_types; i++) { +const struct btf_type *t =btf__type_by_id (btf, i); +const char *name; +bool match; + +if (!btf_is_func (t)) +continue ; + + name =btf__name_by_offset (btf, t->name_off); +if (!name) +continue ; + match =strncmp (name,"bpf_modify_return_test" , +sizeof ("bpf_modify_return_test" ) -1 ) ==0 ; + match |=strncmp (name,"bpf_fentry_test" , +sizeof ("bpf_fentry_test" ) -1 ) ==0 ; +if (!match) +continue ; + + md->btf_ids[ids_cnt] = i; + ids_cnt++; + } + +btf__free (btf); +return ASSERT_EQ (ids_cnt,9 ,"multi_btf_ids" ) ?0 :-1 ; +} + +static int multi_attach (struct multi_data *md) +{ +DECLARE_LIBBPF_OPTS (bpf_link_create_opts, opts); +int prog_fd; + + md->skel =multi_modify_return__open_and_load (); +if (!ASSERT_OK_PTR (md->skel,"multi_attach_check__load" )) +return -1 ; + + opts.multi.btf_ids = md->btf_ids; + opts.multi.btf_ids_cnt =9 ; + + prog_fd =bpf_program__fd (md->skel->progs.test1); + + md->link_fentry =bpf_link_create (prog_fd,0 ,BPF_TRACE_FENTRY , &opts); +if (!ASSERT_GE (md->link_fentry,0 ,"bpf_link_create" )) + goto cleanup; + + prog_fd =bpf_program__fd (md->skel->progs.test2); + + md->link_fexit =bpf_link_create (prog_fd,0 ,BPF_TRACE_FEXIT , &opts); +if (!ASSERT_GE (md->link_fexit,0 ,"bpf_link_create" )) + goto cleanup_close; + +return 0 ; + +cleanup_close: +close (md->link_fentry); +cleanup: +multi_modify_return__destroy (md->skel); +return -1 ; +} + +static void multi_detach (struct multi_data *md) +{ +close (md->link_fentry); +close (md->link_fexit); +multi_modify_return__destroy (md->skel); +} + +static void run_test (__u32 input_retval,__u16 want_side_effect,__s16 want_ret, +struct multi_data *md) {struct modify_return *skel =NULL ;int err, prog_fd;
@@ -27,6 +114,9 @@ static void run_test(__u32 input_retval, __u16 want_side_effect, __s16 want_ret)if (CHECK (err,"modify_return" ,"attach failed: %d\n" , err)) goto cleanup; +if (md && !ASSERT_OK (multi_attach (md),"multi_attach" )) + goto cleanup; + skel->bss->input_retval = input_retval; prog_fd =bpf_program__fd (skel->progs.fmod_ret_test); err = bpf_prog_test_run(prog_fd, 1, NULL, 0, NULL, 0,
@@ -49,6 +139,8 @@ static void run_test(__u32 input_retval, __u16 want_side_effect, __s16 want_ret)CHECK (skel->bss->fmod_ret_result !=1 ,"modify_return" ,"fmod_ret failed\n" ); +if (md) +multi_detach (md); cleanup:modify_return__destroy (skel); }
@@ -56,11 +148,27 @@ static void run_test(__u32 input_retval, __u16 want_side_effect, __s16 want_ret)/* TODO: conflict with get_func_ip_test */ void serial_test_modify_return (void ) { +struct multi_data data = {}; + +run_test (0 /* input_retval */ , +1 /* want_side_effect */ , +4 /* want_ret */ , +NULL /* no multi func test */ ); +run_test (-EINVAL /* input_retval */ , +0 /* want_side_effect */ , + -EINVAL /* want_ret */ , +NULL /* no multi func test */ ); + +if (!ASSERT_OK (multi_btf_ids (&data),"multi_attach" )) +return ; +run_test (0 /* input_retval */ ,1 /* want_side_effect */ , -4 /* want_ret */ ); +4 /* want_ret */ , + &data);run_test (-EINVAL /* input_retval */ ,0 /* want_side_effect */ , - -EINVAL /* want_ret */ ); + -EINVAL /* want_ret */ , + &data); }
tools/testing/selftests/bpf/progs/multi_modify_return.c new file +17 -0
@@ -0,0 +1,17 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <linux/bpf.h> +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> + +char _license[] SEC("license") = "GPL"; +SEC("fentry.multi/bpf_fentry_test*") +int BPF_PROG (test1 ,__u64 a,__u64 b,__u64 c,__u64 d,__u64 e,__u64 f) +{ +return 0 ; +} + +SEC("fexit.multi/bpf_fentry_test*") +int BPF_PROG (test2 ,__u64 a,__u64 b,__u64 c,__u64 d,__u64 e,__u64 f,int ret) +{ +return 0 ; +}
Signature · 1 line
2.31.1
PATCH v5 29/29 selftests/bpf: Add attach multi func test
In reply to Jiri OlsaAdding test code to check on trampolines spliting.
The tests attached various bpf_fetry_* functions in a way so there's always non trivial IDs intersection, that leads to trampoline splitting in kenrel code.
--- tools/testing/selftests/bpf/Makefile | 4 +- .../bpf/prog_tests/multi_attach_test.c | 176 ++++++++++++++++++ .../selftests/bpf/progs/multi_attach.c | 105 +++++++++++ 3 files changed, 284 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/multi_attach_test.c create mode 100644 tools/testing/selftests/bpf/progs/multi_attach.c
tools/testing/selftests/bpf/Makefile +3 -1
@@ -324,7 +324,8 @@ SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h \ linked_vars.skel.h linked_maps.skel.h \ multi_fentry_test.skel.h multi_fexit_test.skel.h \ - multi_fentry_fexit_test.skel.h multi_mixed_test.skel.h + multi_fentry_fexit_test.skel.h multi_mixed_test.skel.h \ + multi_attach_test.skel.h LSKELS := kfunc_call_test.c fentry_test.c fexit_test.c fexit_sleep.c \ test_ringbuf.c atomics.c trace_printk.c trace_vprintk.c
@@ -340,6 +341,7 @@ multi_fentry_test.skel.h-deps := multi_fentry.o multi_check.o multi_fexit_test.skel.h-deps := multi_fexit.o multi_check.o multi_fentry_fexit_test.skel.h-deps := multi_fentry_fexit.o multi_check.o multi_mixed_test.skel.h-deps := multi_mixed.o multi_check.o +multi_attach_test.skel.h-deps := multi_attach.o multi_check.o LINKED_BPF_SRCS := $(patsubst %.o,%.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps)))
tools/testing/selftests/bpf/prog_tests/multi_attach_test.c new file +176 -0
176 lines not on this page · raw message
tools/testing/selftests/bpf/progs/multi_attach.c new file +105 -0
105 lines not on this page · raw message
Signature · 1 line
2.31.1
Re: [PATCH bpf-next 09/29] bpf: Add support to load multi func tracing program
In reply to Jiri Olsakernel/bpf/syscall.c static bool is_perfmon_prog_type(enum bpf_prog_type prog_type) · line 2202
Quoted 4 lines · Jiri Olsa
kernel/bpf/syscall.c +4
-0+ +DEFINE_BPF_MULTI_FUNC(unsigned long a1, unsigned long a2, + unsigned long a3, unsigned long a4, + unsigned long a5, unsigned long a6)
This is probably a bit too x86 specific. May be make add all 12 args? Or other places would need to be tweaked?
Quoted 1 line
+BTF_ID_LIST_SINGLE(bpf_multi_func_btf_id, func,
bpf_multi_func)
...
kernel/bpf/syscall.c static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr) · line 2311
Quoted 2 lines
kernel/bpf/syscall.c +1
-1- prog->aux->attach_btf_id = attr->attach_btf_id; + prog->aux->attach_btf_id = multi_func ? bpf_multi_func_btf_id[0 ] : attr->attach_btf_id;
Just ignoring that was passed in uattr?
Maybe instead of ignoring dopr BPF_F_MULTI_FUNC and make libbpf
point to that btf_id instead?
Then multi or not can be checked with if (attr->attach_btf_id == bpf_multi_func_btf_id[0]).
Re: [PATCH bpf-next 07/29] bpf, x64: Allow to use caller address from stack
In reply to Jiri OlsaQuoted 28 lines · Jiri Olsa
Currently we call the original function by using the absolute address given at the JIT generation. That's not usable when having trampoline attached to multiple functions. In this case we need to take the return address from the stack.
Adding support to retrieve the original function address from the stack by adding new
BPF_TRAMP_F_ORIG_STACKflag forarch_prepare_bpf_trampolinefunction.Basically we take the return address of the 'fentry' call:
function + 0: call fentry # stores 'function + 5' address on stack
function + 5: ...The 'function + 5' address will be used as the address for the original function to call.
Signed-off-by Jiri Olsa <[email protected]>--- arch/x86/net/bpf_jit_comp.c | 13 +++++++++---- include/linux/bpf.h | 5 +++++ 2 files changed, 14 insertions(+), 4 deletions(-)arch/x86/net/bpf_jit_comp.c +0
-0
arch/x86/net/bpf_jit_comp.c int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *i · line 2040
Quoted 10 lines
arch/x86/net/bpf_jit_comp.c +3
-4@@ -2035,10 +2035,15 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *iif (flags &BPF_TRAMP_F_CALL_ORIG ) {restore_regs (m, &prog, nr_args, stack_size); -/* call original function */ -if (emit_call (&prog, orig_call, prog)) { - ret = -EINVAL ; - goto cleanup; +if (flags &BPF_TRAMP_F_ORIG_STACK ) { +emit_ldx (&prog,BPF_DW ,BPF_REG_0 ,BPF_REG_FP ,8 ); +EMIT2 (0xff ,0xd0 );/* call *rax */
Either return an eror if repoline is on
or use emit_indirect_jump().
Re: [PATCH bpf-next 07/29] bpf, x64: Allow to use caller address from stack
Quoted 32 lines · Alexei Starovoitov
Quoted 31 lines · Jiri Olsa
Currently we call the original function by using the absolute address given at the JIT generation. That's not usable when having trampoline attached to multiple functions. In this case we need to take the return address from the stack.
Adding support to retrieve the original function address from the stack by adding new
BPF_TRAMP_F_ORIG_STACKflag forarch_prepare_bpf_trampolinefunction.Basically we take the return address of the 'fentry' call:
function + 0: call fentry # stores 'function + 5' address on stack
function + 5: ...The 'function + 5' address will be used as the address for the original function to call.
Signed-off-by Jiri Olsa <[email protected]>--- arch/x86/net/bpf_jit_comp.c | 13 +++++++++---- include/linux/bpf.h | 5 +++++ 2 files changed, 14 insertions(+), 4 deletions(-)arch/x86/net/bpf_jit_comp.c +0
-0@@ -2035,10 +2035,15 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *iif (flags &BPF_TRAMP_F_CALL_ORIG ) {restore_regs (m, &prog, nr_args, stack_size);
Quoted 11 lines
arch/x86/net/bpf_jit_comp.c int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *i · line 2040
Quoted 7 lines
arch/x86/net/bpf_jit_comp.c +3
-4-/* call original function */ -if (emit_call (&prog, orig_call, prog)) { - ret = -EINVAL ; - goto cleanup; +if (flags &BPF_TRAMP_F_ORIG_STACK ) { +emit_ldx (&prog,BPF_DW ,BPF_REG_0 ,BPF_REG_FP ,8 ); +EMIT2 (0xff ,0xd0 );/* call *rax */ Either return an eror if repoline is on
or useemit_indirect_jump().
ok, will check
thanks,
jirka
Re: [PATCH bpf-next 09/29] bpf: Add support to load multi func tracing program
In reply to Alexei StarovoitovQuoted 8 lines · Alexei Starovoitov
kernel/bpf/syscall.c static bool is_perfmon_prog_type(enum bpf_prog_type prog_type) · line 2202
Quoted 4 lines · Jiri Olsa
kernel/bpf/syscall.c +4
-0+ +DEFINE_BPF_MULTI_FUNC(unsigned long a1, unsigned long a2, + unsigned long a3, unsigned long a4, + unsigned long a5, unsigned long a6)This is probably a bit too x86 specific. May be make add all 12 args? Or other places would need to be tweaked?
I think si, I'll check
Quoted 11 lines
Quoted 2 lines
+BTF_ID_LIST_SINGLE(bpf_multi_func_btf_id, func,
bpf_multi_func)...
kernel/bpf/syscall.c static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr) · line 2311
Quoted 2 lines
kernel/bpf/syscall.c +1
-1- prog->aux->attach_btf_id = attr->attach_btf_id; + prog->aux->attach_btf_id = multi_func ? bpf_multi_func_btf_id[0 ] : attr->attach_btf_id;Just ignoring that was passed in uattr?
Maybe instead of ignoring doprBPF_F_MULTI_FUNCand make libbpf
point to thatbtf_idinstead?
Then multi or not can be checked with if (attr->attach_btf_id== bpf_multi_func_btf_id[0]).
nice idea, it might fit better than the flag
thanks,
jirka
Re: [PATCH bpf-next 06/29] bpf: Add bpf_arg/bpf_ret_value helpers for tracing programs
In reply to Jiri OlsaQuoted 14 lines · Jiri Olsa
Adding bpf_arg/bpf_ret_value helpers for tracing programs that returns traced function arguments.
Get n-th argument of the traced function:
longbpf_arg(void *ctx, int n)Get return value of the traced function:
longbpf_ret_value(void *ctx)The trampoline now stores number of arguments on ctx-8 address, so it's easy to verify argument index and find return value argument.
Quoted 8 lines
Moving function ip address on the trampoline stack behind the number of functions arguments, so it's now stored on ctx-16 address.
Both helpers are inlined by verifier.
Signed-off-by Jiri Olsa <[email protected]>---
It would be great to land these changes separate from your huge patch set. There are some upcoming BPF trampoline related changes that will touch this (to add BPF cookie support for fentry/fexit progs), so would be nice to minimize the interdependencies. So maybe post this patch separately (probably after holidays ;) ).
Quoted 12 lines
arch/x86/net/bpf_jit_comp.c | 18 +++++++++++--- include/uapi/linux/bpf.h | 14 +++++++++++ kernel/bpf/verifier.c | 45 ++++++++++++++++++++++++++++++++-- kernel/trace/bpf_trace.c | 38 +++++++++++++++++++++++++++- tools/include/uapi/linux/bpf.h | 14 +++++++++++ 5 files changed, 122 insertions(+), 7 deletions(-)arch/x86/net/bpf_jit_comp.c +0
-0
arch/x86/net/bpf_jit_comp.c int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *i · line 1944
Quoted 5 lines
arch/x86/net/bpf_jit_comp.c +1
-1@@ -1941,7 +1941,7 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *i void *orig_call) {int ret, i, nr_args = m->nr_args; -int stack_size = nr_args *8 ; +int stack_size = nr_args *8 +8 /* nr_args */ ;
this /* nr_args */ next to 8 is super confusing, would be better to expand the comment; might be a good idea to have some sort of a description of possible stack layouts (e.g., fexit has some extra stuff on the stack, I think, but it's impossible to remember and need to recover that knowledge from the assembly code, basically).
arch/x86/net/bpf_jit_comp.c int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *i · line 1990
Quoted 11 lines
arch/x86/net/bpf_jit_comp.c +1
-3struct bpf_tramp_progs *fentry = &tprogs[BPF_TRAMP_FENTRY ];struct bpf_tramp_progs *fexit = &tprogs[BPF_TRAMP_FEXIT ];struct bpf_tramp_progs *fmod_ret = &tprogs[BPF_TRAMP_MODIFY_RETURN ];@@ -1987,12 +1987,22 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *iEMIT4 (0x48 ,0x83 ,0xe8 ,X86_PATCH_SIZE );emit_stx (&prog,BPF_DW ,BPF_REG_FP ,BPF_REG_0 , -stack_size); -/* Continue with stack_size for regs storage, stack will -* be correctly restored with 'leave' instruction. -*/ +/* Continue with stack_size for 'nr_args' storage */
same, I don't think this comment really helps, just confuses some more
Quoted 4 lines
arch/x86/net/bpf_jit_comp.c +1
-0stack_size -=8 ; } +/* Store number of arguments of the traced function:
arch/x86/net/bpf_jit_comp.c int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *i · line 2004
Quoted 10 lines
arch/x86/net/bpf_jit_comp.c +10
-0+* mov rax, nr_args +* mov QWORD PTR [rbp - stack_size], rax +*/ +emit_mov_imm64 (&prog,BPF_REG_0 ,0 , (u32 ) nr_args); +emit_stx (&prog,BPF_DW ,BPF_REG_FP ,BPF_REG_0 , -stack_size); + +/* Continue with stack_size for regs storage, stack will +* be correctly restored with 'leave' instruction. +*/ + stack_size -=8 ;
I think "stack_size" as a name outlived itself and it just makes everything harder to understand. It's used more like a stack offset (relative to rsp or rbp) for different things. Would it make code worse if we had few offset variables instead (or rather in addition, we still need to calculate a full stack_size; it's just it's constant re-adjustment is what's hard to keep track of), like regs_off, ret_ip_off, arg_cnt_off, etc?
Quoted 9 lines
arch/x86/net/bpf_jit_comp.c +1
-0+save_regs (m, &prog, nr_args, stack_size);if (flags &BPF_TRAMP_F_CALL_ORIG ) {include/uapi/linux/bpf.h +0
-0
include/uapi/linux/bpf.h union bpf_attr { · line 4961
Quoted 5 lines
include/uapi/linux/bpf.h +2
-0@@ -4957,6 +4957,18 @@ union bpf_attr {* **-ENOENT** if *task->mm* is NULL, or no vma contains *addr*. * **-EBUSY** if failed to try lock mmap_lock. * **-EINVAL** for invalid **flags**. +* +* long bpf_arg(void *ctx, int n)
__u32 n ?
include/uapi/linux/bpf.h union bpf_attr { · line 4965
Quoted 4 lines
include/uapi/linux/bpf.h +4
-0+* Description +* Get n-th argument of the traced function (for tracing programs). +* Return +* Value of the argument.
What about errors? those need to be documented.
include/uapi/linux/bpf.h union bpf_attr { · line 4971
Quoted 6 lines
include/uapi/linux/bpf.h +6
-0+* +* long bpf_ret_value(void *ctx) +* Description +* Get return value of the traced function (for tracing programs). +* Return +* Return value of the traced function.
Same, errors not documented. Also would be good to document what happens when ret_value is requested in the context where there is no ret value (e.g., fentry)
include/uapi/linux/bpf.h union bpf_attr { · line 5156
Quoted 9 lines
include/uapi/linux/bpf.h +2
-0*/ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \@@ -5140,6 +5152,8 @@ union bpf_attr {FN (skc_to_unix_sock), \FN (kallsyms_lookup_name), \FN (find_vma), \ +FN (arg), \ + FN(ret_value), \
We already have bpf_get_func_ip, so why not continue a tradition and call these bpf_get_func_arg() and bpf_get_func_ret(). Nice, short, clean, consistent.
BTW, a wild thought. Wouldn't it be cool to have these functions work with kprobe/kretprobe as well? Do you think it's possible?
Quoted 21 lines
include/uapi/linux/bpf.h +0
-0/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper kernel/bpf/verifier.c +10
-0@@ -13246,11 +13246,52 @@ static int do_misc_fixups(struct bpf_verifier_env *env)continue ; } +/* Implement bpf_arg inline. */ +if (prog_type ==BPF_PROG_TYPE_TRACING && + insn->imm == BPF_FUNC_arg) { +/* Load nr_args from ctx - 8 */ + insn_buf[0 ] =BPF_LDX_MEM (BPF_DW ,BPF_REG_0 ,BPF_REG_1 ,-8 ); + insn_buf[1 ] =BPF_JMP32_REG (BPF_JGE ,BPF_REG_2 ,BPF_REG_0 ,4 ); + insn_buf[2 ] =BPF_ALU64_IMM (BPF_MUL ,BPF_REG_2 ,8 ); + insn_buf[3 ] =BPF_ALU64_REG (BPF_ADD ,BPF_REG_2 ,BPF_REG_1 ); + insn_buf[4 ] =BPF_LDX_MEM (BPF_DW ,BPF_REG_0 ,BPF_REG_2 ,0 ); + insn_buf[5 ] =BPF_JMP_A (1 );
kernel/bpf/verifier.c static int do_misc_fixups(struct bpf_verifier_env *env) · line 13268
Quoted 10 lines
kernel/bpf/verifier.c +10
-0+ insn_buf[6 ] =BPF_MOV64_IMM (BPF_REG_0 ,0 ); + + new_prog =bpf_patch_insn_data (env, i + delta, insn_buf,7 ); +if (!new_prog) +return -ENOMEM ; + + delta +=6 ; + env->prog = prog = new_prog; + insn = new_prog->insnsi + i + delta; +continue ;
nit: this whole sequence of steps and calculations seems like something that might be abstracted and hidden behind a macro or helper func? Not related to your change, though. But wouldn't it be easier to understand if it was just written as:
PATCH_INSNS(
BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8);
BPF_JMP32_REG(BPF_JGE, BPF_REG_2, BPF_REG_0, 4);
BPF_ALU64_IMM(BPF_MUL, BPF_REG_2, 8);
BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_1);
BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_2, 0);
BPF_JMP_A(1);
BPF_MOV64_IMM(BPF_REG_0, 0));continue;
?
Quoted 45 lines
kernel/bpf/verifier.c +23
-2+ } + +/* Implement bpf_ret_value inline. */ +if (prog_type ==BPF_PROG_TYPE_TRACING && + insn->imm == BPF_FUNC_ret_value) { +/* Load nr_args from ctx - 8 */ + insn_buf[0 ] =BPF_LDX_MEM (BPF_DW ,BPF_REG_2 ,BPF_REG_1 ,-8 ); + insn_buf[1 ] =BPF_ALU64_IMM (BPF_MUL ,BPF_REG_2 ,8 ); + insn_buf[2 ] =BPF_ALU64_REG (BPF_ADD ,BPF_REG_2 ,BPF_REG_1 ); + insn_buf[3 ] =BPF_LDX_MEM (BPF_DW ,BPF_REG_0 ,BPF_REG_2 ,0 ); + + new_prog =bpf_patch_insn_data (env, i + delta, insn_buf,4 ); +if (!new_prog) +return -ENOMEM ; + + delta +=3 ; + env->prog = prog = new_prog; + insn = new_prog->insnsi + i + delta; +continue ; + } +/* Implement bpf_get_func_ip inline. */ if (prog_type ==BPF_PROG_TYPE_TRACING && insn->imm == BPF_FUNC_get_func_ip) { -/* Load IP address from ctx - 8 */ - insn_buf[0 ] =BPF_LDX_MEM (BPF_DW ,BPF_REG_0 ,BPF_REG_1 ,-8 ); +/* Load IP address from ctx - 16 */ + insn_buf[0 ] =BPF_LDX_MEM (BPF_DW ,BPF_REG_0 ,BPF_REG_1 ,-16 ); new_prog =bpf_patch_insn_data (env, i + delta, insn_buf,1 );if (!new_prog)kernel/trace/bpf_trace.c +1
-1@@ -1012,7 +1012,7 @@ const struct bpf_func_proto bpf_snprintf_btf_proto = { BPF_CALL_1(bpf_get_func_ip_tracing, void *, ctx) {/* This helper call is inlined by verifier. */ -return ((u64 *)ctx)[-1 ]; +return ((u64 *)ctx)[-2 ]; }static const struct bpf_func_proto bpf_get_func_ip_proto_tracing = {
kernel/trace/bpf_trace.c static const struct bpf_func_proto bpf_get_branch_snapshot_proto = { · line 1100
Quoted 10 lines
kernel/trace/bpf_trace.c +7
-0@@ -1091,6 +1091,38 @@ static const struct bpf_func_proto bpf_get_branch_snapshot_proto = { .arg2_type = ARG_CONST_SIZE_OR_ZERO, }; +BPF_CALL_2(bpf_arg, void *, ctx, int, n) +{ +/* This helper call is inlined by verifier. */ +u64 nr_args = ((u64 *)ctx)[-1 ]; + +if ((u64 ) n >= nr_args) +return 0 ;
We'll need bpf_get_func_arg_cnt() helper as well to be able to know the actual number of arguments traced function has. It's impossible to know whether the argument is zero or there is no argument, otherwise.
Quoted 7 lines
kernel/trace/bpf_trace.c +7
-0+return ((u64 *)ctx)[n]; +} + +static const struct bpf_func_proto bpf_arg_proto = { + .func = bpf_arg, + .gpl_only = true, + .ret_type =RET_INTEGER ,
kernel/trace/bpf_trace.c static const struct bpf_func_proto bpf_get_branch_snapshot_proto = { · line 1117
Quoted 10 lines
kernel/trace/bpf_trace.c +10
-0+ .arg1_type = ARG_PTR_TO_CTX, + .arg1_type = ARG_ANYTHING, +}; + +BPF_CALL_1(bpf_ret_value, void *, ctx) +{ +/* This helper call is inlined by verifier. */ +u64 nr_args = ((u64 *)ctx)[-1 ]; + +return ((u64 *)ctx)[nr_args];
we should return 0 for fentry or disable this helper for anything but fexit? It's going to return garbage otherwise.
Quoted 8 lines
kernel/trace/bpf_trace.c +8
-0+} + +static const struct bpf_func_proto bpf_ret_value_proto = { + .func = bpf_ret_value, + .gpl_only = true, + .ret_type =RET_INTEGER , +}; +
[...]
Re: [PATCH bpf-next 08/29] bpf: Keep active attached trampoline in bpf_prog
In reply to Jiri OlsaQuoted 7 lines · Jiri Olsa
Keeping active attached trampoline in
bpf_progso it can be used in following changes to account for multiple functions attachments in program.As EXT programs are not going to be supported in multiple functions attachment for now, I'm keeping them stored in link.
can the same EXT program be attached twice? If not, why can't you just use the same prog->aux->trampoline instead of the if/else everywhere?
Quoted 7 lines
Signed-off-by Jiri Olsa <[email protected]>--- include/linux/bpf.h | 1 + kernel/bpf/syscall.c | 34 +++++++++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 5 deletions(-)
Re: [PATCH bpf-next 09/29] bpf: Add support to load multi func tracing program
In reply to Jiri OlsaQuoted 19 lines · Jiri Olsa
Quoted 8 lines · Alexei Starovoitov
kernel/bpf/syscall.c static bool is_perfmon_prog_type(enum bpf_prog_type prog_type) · line 2202
Quoted 4 lines · Jiri Olsa
kernel/bpf/syscall.c +4
-0+ +DEFINE_BPF_MULTI_FUNC(unsigned long a1, unsigned long a2, + unsigned long a3, unsigned long a4, + unsigned long a5, unsigned long a6)This is probably a bit too x86 specific. May be make add all 12 args? Or other places would need to be tweaked?
I think si, I'll check
Quoted 6 lines
Quoted 2 lines
+BTF_ID_LIST_SINGLE(bpf_multi_func_btf_id, func,
bpf_multi_func)...
Quoted 2 lines
kernel/bpf/syscall.c +1
-1- prog->aux->attach_btf_id = attr->attach_btf_id; + prog->aux->attach_btf_id = multi_func ? bpf_multi_func_btf_id[0 ] : attr->attach_btf_id;
Quoted 7 lines
Quoted 5 lines
Just ignoring that was passed in uattr?
Maybe instead of ignoring doprBPF_F_MULTI_FUNCand make libbpf
point to thatbtf_idinstead?
Then multi or not can be checked with if (attr->attach_btf_id== bpf_multi_func_btf_id[0]).nice idea, it might fit better than the flag
Instead of a flag we can also use a different expected_attach_type (FENTRY vs FENTRY_MULTI, etc). As for attach_btf_id, why can't we just enforce it as 0?
Quoted 4 lines
thanks,
jirka
Re: [PATCH bpf-next 06/29] bpf: Add bpf_arg/bpf_ret_value helpers for tracing programs
In reply to Andrii NakryikoQuoted 26 lines · Andrii Nakryiko
Quoted 10 lines
kernel/bpf/verifier.c +10
-0+/* Implement bpf_arg inline. */ +if (prog_type ==BPF_PROG_TYPE_TRACING && + insn->imm == BPF_FUNC_arg) { +/* Load nr_args from ctx - 8 */ + insn_buf[0 ] =BPF_LDX_MEM (BPF_DW ,BPF_REG_0 ,BPF_REG_1 ,-8 ); + insn_buf[1 ] =BPF_JMP32_REG (BPF_JGE ,BPF_REG_2 ,BPF_REG_0 ,4 ); + insn_buf[2 ] =BPF_ALU64_IMM (BPF_MUL ,BPF_REG_2 ,8 ); + insn_buf[3 ] =BPF_ALU64_REG (BPF_ADD ,BPF_REG_2 ,BPF_REG_1 ); + insn_buf[4 ] =BPF_LDX_MEM (BPF_DW ,BPF_REG_0 ,BPF_REG_2 ,0 ); + insn_buf[5 ] =BPF_JMP_A (1 );kernel/bpf/verifier.c static int do_misc_fixups(struct bpf_verifier_env *env) · line 13268
Quoted 10 lines
kernel/bpf/verifier.c +10
-0+ insn_buf[6 ] =BPF_MOV64_IMM (BPF_REG_0 ,0 ); + + new_prog =bpf_patch_insn_data (env, i + delta, insn_buf,7 ); +if (!new_prog) +return -ENOMEM ; + + delta +=6 ; + env->prog = prog = new_prog; + insn = new_prog->insnsi + i + delta; +continue ;nit: this whole sequence of steps and calculations seems like something that might be abstracted and hidden behind a macro or helper func? Not related to your change, though. But wouldn't it be easier to understand if it was just written as:
Quoted 8 lines
PATCH_INSNS(
BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); BPF_JMP32_REG(BPF_JGE, BPF_REG_2, BPF_REG_0, 4); BPF_ALU64_IMM(BPF_MUL, BPF_REG_2, 8); BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_1); BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_2, 0); BPF_JMP_A(1); BPF_MOV64_IMM(BPF_REG_0, 0));
Daniel and myself tried to do similar macro magic in the past, but it suffers unnecessary stack increase and extra copies. So eventually we got rid of it.
I suggest staying with Jiri's approach.
Independent from anything else...
Just noticed BPF_MUL in the above...
Please use BPF_LSH instead. JITs don't optimize such things. It's a job of gcc/llvm to do so. JITs assume that all normal optimizations were done by the compiler.
Re: [PATCH bpf-next 08/29] bpf: Keep active attached trampoline in bpf_prog
In reply to Andrii NakryikoQuoted 11 lines · Andrii Nakryiko
Quoted 7 lines · Jiri Olsa
Keeping active attached trampoline in
bpf_progso it can be used in following changes to account for multiple functions attachments in program.As EXT programs are not going to be supported in multiple functions attachment for now, I'm keeping them stored in link.
can the same EXT program be attached twice? If not, why can't you just use the same
prog->aux->trampolineinstead of the if/else everywhere?
I recall that was my initial change, but it was clashing with fentry/fexit programs because extensions are special
I'll re-check and try to make this generic
jirka
Quoted 9 lines
Quoted 8 lines
Signed-off-by Jiri Olsa <[email protected]>--- include/linux/bpf.h | 1 + kernel/bpf/syscall.c | 34 +++++++++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 5 deletions(-)
Re: [PATCH bpf-next 09/29] bpf: Add support to load multi func tracing program
In reply to Andrii NakryikoQuoted 26 lines · Andrii Nakryiko
Quoted 25 lines · Jiri Olsa
Quoted 8 lines · Alexei Starovoitov
kernel/bpf/syscall.c static bool is_perfmon_prog_type(enum bpf_prog_type prog_type) · line 2202
Quoted 4 lines · Jiri Olsa
kernel/bpf/syscall.c +4
-0+ +DEFINE_BPF_MULTI_FUNC(unsigned long a1, unsigned long a2, + unsigned long a3, unsigned long a4, + unsigned long a5, unsigned long a6)This is probably a bit too x86 specific. May be make add all 12 args? Or other places would need to be tweaked?
I think si, I'll check
Quoted 11 lines
Quoted 2 lines
+BTF_ID_LIST_SINGLE(bpf_multi_func_btf_id, func,
bpf_multi_func)...
kernel/bpf/syscall.c static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr) · line 2311
Quoted 2 lines
kernel/bpf/syscall.c +1
-1- prog->aux->attach_btf_id = attr->attach_btf_id; + prog->aux->attach_btf_id = multi_func ? bpf_multi_func_btf_id[0 ] : attr->attach_btf_id;Just ignoring that was passed in uattr?
Maybe instead of ignoring doprBPF_F_MULTI_FUNCand make libbpf
point to thatbtf_idinstead?
Then multi or not can be checked with if (attr->attach_btf_id== bpf_multi_func_btf_id[0]).
Quoted 4 lines
Quoted 1 line
nice idea, it might fit better than the flag
Instead of a flag we can also use a different
expected_attach_type(FENTRY vsFENTRY_MULTI, etc).
right, you already asked for that - https://lore.kernel.org/bpf/YS9k26rRcUJVS%2Fvx@krava/
I still think it'd mean more code while this way we just use current fentry/fexit code paths with few special handling for multi programs
Quoted 2 lines
As for
attach_btf_id, why can't we just
enforce it as 0?
there's prog->aux->attach_func_proto that needs to be set based on attach_btf_id, and is checked later in btf_ctx_access
jirka
Quoted 6 lines
Quoted 5 lines
thanks,
jirka
Re: [PATCH bpf-next 06/29] bpf: Add bpf_arg/bpf_ret_value helpers for tracing programs
In reply to Andrii NakryikoQuoted 21 lines · Andrii Nakryiko
Quoted 20 lines · Jiri Olsa
Adding bpf_arg/bpf_ret_value helpers for tracing programs that returns traced function arguments.
Get n-th argument of the traced function:
longbpf_arg(void *ctx, int n)Get return value of the traced function:
longbpf_ret_value(void *ctx)The trampoline now stores number of arguments on ctx-8 address, so it's easy to verify argument index and find return value argument.
Moving function ip address on the trampoline stack behind the number of functions arguments, so it's now stored on ctx-16 address.
Both helpers are inlined by verifier.
Quoted 8 lines
Quoted 2 lines
Signed-off-by Jiri Olsa <[email protected]>---
It would be great to land these changes separate from your huge patch set. There are some upcoming BPF trampoline related changes that will touch this (to add BPF cookie support for fentry/fexit progs), so would be nice to minimize the interdependencies. So maybe post this patch separately (probably after holidays ;) ).
ok
Quoted 32 lines
Quoted 13 lines
arch/x86/net/bpf_jit_comp.c | 18 +++++++++++--- include/uapi/linux/bpf.h | 14 +++++++++++ kernel/bpf/verifier.c | 45 ++++++++++++++++++++++++++++++++-- kernel/trace/bpf_trace.c | 38 +++++++++++++++++++++++++++- tools/include/uapi/linux/bpf.h | 14 +++++++++++ 5 files changed, 122 insertions(+), 7 deletions(-)arch/x86/net/bpf_jit_comp.c +0
-0arch/x86/net/bpf_jit_comp.c int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *i · line 1944
Quoted 5 lines
arch/x86/net/bpf_jit_comp.c +1
-1@@ -1941,7 +1941,7 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *i void *orig_call) {int ret, i, nr_args = m->nr_args; -int stack_size = nr_args *8 ; +int stack_size = nr_args *8 +8 /* nr_args */ ;this /*
nr_args*/ next to 8 is super confusing, would be better to expand the comment; might be a good idea to have some sort of a description of possible stack layouts (e.g., fexit has some extra stuff on the stack, I think, but it's impossible to remember and need to recover that knowledge from the assembly code, basically).Quoted 7 lines
arch/x86/net/bpf_jit_comp.c +0
-0struct bpf_tramp_progs *fentry = &tprogs[BPF_TRAMP_FENTRY ];struct bpf_tramp_progs *fexit = &tprogs[BPF_TRAMP_FEXIT ];struct bpf_tramp_progs *fmod_ret = &tprogs[BPF_TRAMP_MODIFY_RETURN ];@@ -1987,12 +1987,22 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *iEMIT4 (0x48 ,0x83 ,0xe8 ,X86_PATCH_SIZE );emit_stx (&prog,BPF_DW ,BPF_REG_FP ,BPF_REG_0 , -stack_size);
Quoted 6 lines
arch/x86/net/bpf_jit_comp.c int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *i · line 1990
Quoted 4 lines
arch/x86/net/bpf_jit_comp.c +1
-3-/* Continue with stack_size for regs storage, stack will -* be correctly restored with 'leave' instruction. -*/ +/* Continue with stack_size for 'nr_args' storage */ same, I don't think this comment really helps, just confuses some more
ok, I'll put some more comments with list of possible the stack layouts
Quoted 16 lines
Quoted 5 lines
arch/x86/net/bpf_jit_comp.c +1
-0stack_size -=8 ; } +/* Store number of arguments of the traced function: arch/x86/net/bpf_jit_comp.c int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *i · line 2004
Quoted 10 lines
arch/x86/net/bpf_jit_comp.c +10
-0+* mov rax, nr_args +* mov QWORD PTR [rbp - stack_size], rax +*/ +emit_mov_imm64 (&prog,BPF_REG_0 ,0 , (u32 ) nr_args); +emit_stx (&prog,BPF_DW ,BPF_REG_FP ,BPF_REG_0 , -stack_size); + +/* Continue with stack_size for regs storage, stack will +* be correctly restored with 'leave' instruction. +*/ + stack_size -=8 ;
Quoted 7 lines
I think "
stack_size" as a name outlived itself and it just makes everything harder to understand. It's used more like a stack offset (relative to rsp or rbp) for different things. Would it make code worse if we had few offset variables instead (or rather in addition, we still need to calculate a fullstack_size; it's just it's constant re-adjustment is what's hard to keep track of), likeregs_off,ret_ip_off,arg_cnt_off, etc?
let's see, I'll try that
Quoted 4 lines
Quoted 4 lines
arch/x86/net/bpf_jit_comp.c +1
-0+save_regs (m, &prog, nr_args, stack_size);
Quoted 13 lines
include/uapi/linux/bpf.h union bpf_attr { · line 4961
Quoted 11 lines
arch/x86/net/bpf_jit_comp.c +0
-0if (flags &BPF_TRAMP_F_CALL_ORIG ) {include/uapi/linux/bpf.h +2
-0@@ -4957,6 +4957,18 @@ union bpf_attr {* **-ENOENT** if *task->mm* is NULL, or no vma contains *addr*. * **-EBUSY** if failed to try lock mmap_lock. * **-EINVAL** for invalid **flags**. +* +* long bpf_arg(void *ctx, int n)
__u32n ?
ok
Quoted 7 lines
include/uapi/linux/bpf.h union bpf_attr { · line 4965
Quoted 5 lines
include/uapi/linux/bpf.h +4
-0+* Description +* Get n-th argument of the traced function (for tracing programs). +* Return +* Value of the argument. What about errors? those need to be documented.
ok
Quoted 11 lines
include/uapi/linux/bpf.h union bpf_attr { · line 4971
Quoted 7 lines
include/uapi/linux/bpf.h +6
-0+* +* long bpf_ret_value(void *ctx) +* Description +* Get return value of the traced function (for tracing programs). +* Return +* Return value of the traced function. Same, errors not documented. Also would be good to document what happens when
ret_valueis requested in the context where there is no ret value (e.g., fentry)
ugh, that's not handled at the moment.. should we fail when we see bpf_ret_value helper call in fentry program?
Quoted 14 lines
include/uapi/linux/bpf.h union bpf_attr { · line 5156
Quoted 10 lines
include/uapi/linux/bpf.h +2
-0*/ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \@@ -5140,6 +5152,8 @@ union bpf_attr {FN (skc_to_unix_sock), \FN (kallsyms_lookup_name), \FN (find_vma), \ +FN (arg), \ + FN(ret_value), \We already have
bpf_get_func_ip, so why not continue a tradition and call thesebpf_get_func_arg()andbpf_get_func_ret(). Nice, short, clean, consistent.
ok
Quoted 3 lines
BTW, a wild thought. Wouldn't it be cool to have these functions work with kprobe/kretprobe as well? Do you think it's possible?
right, bpf_get_func_ip already works for kprobes
struct kprobe could have the btf_func_model of the traced function, so in case we trace function directly on the entry point we could read arguments registers based on the btf_func_model
I'll check with Massami
Quoted 39 lines
Quoted 22 lines
include/uapi/linux/bpf.h +0
-0/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper kernel/bpf/verifier.c +10
-0@@ -13246,11 +13246,52 @@ static int do_misc_fixups(struct bpf_verifier_env *env)continue ; } +/* Implement bpf_arg inline. */ +if (prog_type ==BPF_PROG_TYPE_TRACING && + insn->imm == BPF_FUNC_arg) { +/* Load nr_args from ctx - 8 */ + insn_buf[0 ] =BPF_LDX_MEM (BPF_DW ,BPF_REG_0 ,BPF_REG_1 ,-8 ); + insn_buf[1 ] =BPF_JMP32_REG (BPF_JGE ,BPF_REG_2 ,BPF_REG_0 ,4 ); + insn_buf[2 ] =BPF_ALU64_IMM (BPF_MUL ,BPF_REG_2 ,8 ); + insn_buf[3 ] =BPF_ALU64_REG (BPF_ADD ,BPF_REG_2 ,BPF_REG_1 ); + insn_buf[4 ] =BPF_LDX_MEM (BPF_DW ,BPF_REG_0 ,BPF_REG_2 ,0 ); + insn_buf[5 ] =BPF_JMP_A (1 );kernel/bpf/verifier.c static int do_misc_fixups(struct bpf_verifier_env *env) · line 13268
Quoted 10 lines
kernel/bpf/verifier.c +10
-0+ insn_buf[6 ] =BPF_MOV64_IMM (BPF_REG_0 ,0 ); + + new_prog =bpf_patch_insn_data (env, i + delta, insn_buf,7 ); +if (!new_prog) +return -ENOMEM ; + + delta +=6 ; + env->prog = prog = new_prog; + insn = new_prog->insnsi + i + delta; +continue ;nit: this whole sequence of steps and calculations seems like something that might be abstracted and hidden behind a macro or helper func? Not related to your change, though. But wouldn't it be easier to understand if it was just written as:
PATCH_INSNS(
Quoted 8 lines
BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); BPF_JMP32_REG(BPF_JGE, BPF_REG_2, BPF_REG_0, 4); BPF_ALU64_IMM(BPF_MUL, BPF_REG_2, 8); BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_1); BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_2, 0); BPF_JMP_A(1); BPF_MOV64_IMM(BPF_REG_0, 0));continue;
yep, looks better ;-) I'll check
Quoted 52 lines
?
Quoted 48 lines
kernel/bpf/verifier.c +23
-2+ } + +/* Implement bpf_ret_value inline. */ +if (prog_type ==BPF_PROG_TYPE_TRACING && + insn->imm == BPF_FUNC_ret_value) { +/* Load nr_args from ctx - 8 */ + insn_buf[0 ] =BPF_LDX_MEM (BPF_DW ,BPF_REG_2 ,BPF_REG_1 ,-8 ); + insn_buf[1 ] =BPF_ALU64_IMM (BPF_MUL ,BPF_REG_2 ,8 ); + insn_buf[2 ] =BPF_ALU64_REG (BPF_ADD ,BPF_REG_2 ,BPF_REG_1 ); + insn_buf[3 ] =BPF_LDX_MEM (BPF_DW ,BPF_REG_0 ,BPF_REG_2 ,0 ); + + new_prog =bpf_patch_insn_data (env, i + delta, insn_buf,4 ); +if (!new_prog) +return -ENOMEM ; + + delta +=3 ; + env->prog = prog = new_prog; + insn = new_prog->insnsi + i + delta; +continue ; + } +/* Implement bpf_get_func_ip inline. */ if (prog_type ==BPF_PROG_TYPE_TRACING && insn->imm == BPF_FUNC_get_func_ip) { -/* Load IP address from ctx - 8 */ - insn_buf[0 ] =BPF_LDX_MEM (BPF_DW ,BPF_REG_0 ,BPF_REG_1 ,-8 ); +/* Load IP address from ctx - 16 */ + insn_buf[0 ] =BPF_LDX_MEM (BPF_DW ,BPF_REG_0 ,BPF_REG_1 ,-16 ); new_prog =bpf_patch_insn_data (env, i + delta, insn_buf,1 );if (!new_prog)kernel/trace/bpf_trace.c +1
-1@@ -1012,7 +1012,7 @@ const struct bpf_func_proto bpf_snprintf_btf_proto = { BPF_CALL_1(bpf_get_func_ip_tracing, void *, ctx) {/* This helper call is inlined by verifier. */ -return ((u64 *)ctx)[-1 ]; +return ((u64 *)ctx)[-2 ]; }static const struct bpf_func_proto bpf_get_func_ip_proto_tracing = {@@ -1091,6 +1091,38 @@ static const struct bpf_func_proto bpf_get_branch_snapshot_proto = { .arg2_type = ARG_CONST_SIZE_OR_ZERO, };
Quoted 11 lines
kernel/trace/bpf_trace.c static const struct bpf_func_proto bpf_get_branch_snapshot_proto = { · line 1100
Quoted 7 lines
kernel/trace/bpf_trace.c +7
-0+BPF_CALL_2(bpf_arg, void *, ctx, int, n) +{ +/* This helper call is inlined by verifier. */ +u64 nr_args = ((u64 *)ctx)[-1 ]; + +if ((u64 ) n >= nr_args) +return 0 ;We'll need
bpf_get_func_arg_cnt()helper as well to be able to know the actual number of arguments traced function has. It's impossible to know whether the argument is zero or there is no argument, otherwise.
my idea was that the program will call those helpers based on get_func_ip with proper argument indexes
but with bpf_get_func_arg_cnt we could make a simple program that would just print function with all its arguments easily, ok ;-)
Quoted 13 lines
Quoted 13 lines
kernel/trace/bpf_trace.c +12
-0+return ((u64 *)ctx)[n]; +} + +static const struct bpf_func_proto bpf_arg_proto = { + .func = bpf_arg, + .gpl_only = true, + .ret_type =RET_INTEGER , + .arg1_type =ARG_PTR_TO_CTX , + .arg1_type =ARG_ANYTHING , +}; + +BPF_CALL_1(bpf_ret_value, void *, ctx)
Quoted 8 lines
kernel/trace/bpf_trace.c static const struct bpf_func_proto bpf_get_branch_snapshot_proto = { · line 1117
Quoted 5 lines
kernel/trace/bpf_trace.c +5
-0+{ +/* This helper call is inlined by verifier. */ +u64 nr_args = ((u64 *)ctx)[-1 ]; + +return ((u64 *)ctx)[nr_args];we should return 0 for fentry or disable this helper for anything but fexit? It's going to return garbage otherwise.
disabling seems like right choice to me
thanks,
jirka
Re: [PATCH bpf-next 06/29] bpf: Add bpf_arg/bpf_ret_value helpers for tracing programs
In reply to Alexei StarovoitovQuoted 42 lines · Alexei Starovoitov
Quoted 26 lines · Andrii Nakryiko
Quoted 10 lines
kernel/bpf/verifier.c +10
-0+/* Implement bpf_arg inline. */ +if (prog_type ==BPF_PROG_TYPE_TRACING && + insn->imm == BPF_FUNC_arg) { +/* Load nr_args from ctx - 8 */ + insn_buf[0 ] =BPF_LDX_MEM (BPF_DW ,BPF_REG_0 ,BPF_REG_1 ,-8 ); + insn_buf[1 ] =BPF_JMP32_REG (BPF_JGE ,BPF_REG_2 ,BPF_REG_0 ,4 ); + insn_buf[2 ] =BPF_ALU64_IMM (BPF_MUL ,BPF_REG_2 ,8 ); + insn_buf[3 ] =BPF_ALU64_REG (BPF_ADD ,BPF_REG_2 ,BPF_REG_1 ); + insn_buf[4 ] =BPF_LDX_MEM (BPF_DW ,BPF_REG_0 ,BPF_REG_2 ,0 ); + insn_buf[5 ] =BPF_JMP_A (1 );kernel/bpf/verifier.c static int do_misc_fixups(struct bpf_verifier_env *env) · line 13268
Quoted 10 lines
kernel/bpf/verifier.c +10
-0+ insn_buf[6 ] =BPF_MOV64_IMM (BPF_REG_0 ,0 ); + + new_prog =bpf_patch_insn_data (env, i + delta, insn_buf,7 ); +if (!new_prog) +return -ENOMEM ; + + delta +=6 ; + env->prog = prog = new_prog; + insn = new_prog->insnsi + i + delta; +continue ;nit: this whole sequence of steps and calculations seems like something that might be abstracted and hidden behind a macro or helper func? Not related to your change, though. But wouldn't it be easier to understand if it was just written as:
Quoted 8 lines
PATCH_INSNS(
BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); BPF_JMP32_REG(BPF_JGE, BPF_REG_2, BPF_REG_0, 4); BPF_ALU64_IMM(BPF_MUL, BPF_REG_2, 8); BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_1); BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_2, 0); BPF_JMP_A(1); BPF_MOV64_IMM(BPF_REG_0, 0));Daniel and myself tried to do similar macro magic in the past, but it suffers unnecessary stack increase and extra copies. So eventually we got rid of it.
I suggest staying with Jiri's approach.
Quoted 6 lines
Independent from anything else...
Just noticedBPF_MULin the above...
Please useBPF_LSHinstead. JITs don't optimize such things. It's a job of gcc/llvm to do so. JITs assume that all normal optimizations were done by the compiler.
will change
thanks,
jirka
Re: [PATCH bpf-next 06/29] bpf: Add bpf_arg/bpf_ret_value helpers for tracing programs
In reply to Jiri OlsaQuoted 139 lines · Jiri Olsa
Quoted 21 lines · Andrii Nakryiko
Quoted 20 lines · Jiri Olsa
Adding bpf_arg/bpf_ret_value helpers for tracing programs that returns traced function arguments.
Get n-th argument of the traced function:
longbpf_arg(void *ctx, int n)Get return value of the traced function:
longbpf_ret_value(void *ctx)The trampoline now stores number of arguments on ctx-8 address, so it's easy to verify argument index and find return value argument.
Moving function ip address on the trampoline stack behind the number of functions arguments, so it's now stored on ctx-16 address.
Both helpers are inlined by verifier.
Quoted 8 lines
Quoted 2 lines
Signed-off-by Jiri Olsa <[email protected]>---
It would be great to land these changes separate from your huge patch set. There are some upcoming BPF trampoline related changes that will touch this (to add BPF cookie support for fentry/fexit progs), so would be nice to minimize the interdependencies. So maybe post this patch separately (probably after holidays ;) ).
ok
Quoted 32 lines
Quoted 13 lines
arch/x86/net/bpf_jit_comp.c | 18 +++++++++++--- include/uapi/linux/bpf.h | 14 +++++++++++ kernel/bpf/verifier.c | 45 ++++++++++++++++++++++++++++++++-- kernel/trace/bpf_trace.c | 38 +++++++++++++++++++++++++++- tools/include/uapi/linux/bpf.h | 14 +++++++++++ 5 files changed, 122 insertions(+), 7 deletions(-)arch/x86/net/bpf_jit_comp.c +0
-0arch/x86/net/bpf_jit_comp.c int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *i · line 1944
Quoted 5 lines
arch/x86/net/bpf_jit_comp.c +1
-1@@ -1941,7 +1941,7 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *i void *orig_call) {int ret, i, nr_args = m->nr_args; -int stack_size = nr_args *8 ; +int stack_size = nr_args *8 +8 /* nr_args */ ;this /*
nr_args*/ next to 8 is super confusing, would be better to expand the comment; might be a good idea to have some sort of a description of possible stack layouts (e.g., fexit has some extra stuff on the stack, I think, but it's impossible to remember and need to recover that knowledge from the assembly code, basically).Quoted 7 lines
arch/x86/net/bpf_jit_comp.c +0
-0struct bpf_tramp_progs *fentry = &tprogs[BPF_TRAMP_FENTRY ];struct bpf_tramp_progs *fexit = &tprogs[BPF_TRAMP_FEXIT ];struct bpf_tramp_progs *fmod_ret = &tprogs[BPF_TRAMP_MODIFY_RETURN ];@@ -1987,12 +1987,22 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *iEMIT4 (0x48 ,0x83 ,0xe8 ,X86_PATCH_SIZE );emit_stx (&prog,BPF_DW ,BPF_REG_FP ,BPF_REG_0 , -stack_size);Quoted 6 lines
arch/x86/net/bpf_jit_comp.c int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *i · line 1990
Quoted 4 lines
arch/x86/net/bpf_jit_comp.c +1
-3-/* Continue with stack_size for regs storage, stack will -* be correctly restored with 'leave' instruction. -*/ +/* Continue with stack_size for 'nr_args' storage */ same, I don't think this comment really helps, just confuses some more
ok, I'll put some more comments with list of possible the stack layouts
Quoted 16 lines
Quoted 5 lines
arch/x86/net/bpf_jit_comp.c +1
-0stack_size -=8 ; } +/* Store number of arguments of the traced function: arch/x86/net/bpf_jit_comp.c int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *i · line 2004
Quoted 10 lines
arch/x86/net/bpf_jit_comp.c +10
-0+* mov rax, nr_args +* mov QWORD PTR [rbp - stack_size], rax +*/ +emit_mov_imm64 (&prog,BPF_REG_0 ,0 , (u32 ) nr_args); +emit_stx (&prog,BPF_DW ,BPF_REG_FP ,BPF_REG_0 , -stack_size); + +/* Continue with stack_size for regs storage, stack will +* be correctly restored with 'leave' instruction. +*/ + stack_size -=8 ;Quoted 7 lines
I think "
stack_size" as a name outlived itself and it just makes everything harder to understand. It's used more like a stack offset (relative to rsp or rbp) for different things. Would it make code worse if we had few offset variables instead (or rather in addition, we still need to calculate a fullstack_size; it's just it's constant re-adjustment is what's hard to keep track of), likeregs_off,ret_ip_off,arg_cnt_off, etc?let's see, I'll try that
Quoted 4 lines
Quoted 4 lines
arch/x86/net/bpf_jit_comp.c +1
-0+save_regs (m, &prog, nr_args, stack_size);Quoted 13 lines
include/uapi/linux/bpf.h union bpf_attr { · line 4961
Quoted 11 lines
arch/x86/net/bpf_jit_comp.c +0
-0if (flags &BPF_TRAMP_F_CALL_ORIG ) {include/uapi/linux/bpf.h +2
-0@@ -4957,6 +4957,18 @@ union bpf_attr {* **-ENOENT** if *task->mm* is NULL, or no vma contains *addr*. * **-EBUSY** if failed to try lock mmap_lock. * **-EINVAL** for invalid **flags**. +* +* long bpf_arg(void *ctx, int n)
__u32n ?ok
Quoted 7 lines
include/uapi/linux/bpf.h union bpf_attr { · line 4965
Quoted 5 lines
include/uapi/linux/bpf.h +4
-0+* Description +* Get n-th argument of the traced function (for tracing programs). +* Return +* Value of the argument. What about errors? those need to be documented.
ok
Quoted 8 lines
Quoted 7 lines
include/uapi/linux/bpf.h +6
-0+* +* long bpf_ret_value(void *ctx) +* Description +* Get return value of the traced function (for tracing programs). +* Return +* Return value of the traced function.
Quoted 6 lines
Quoted 3 lines
Same, errors not documented. Also would be good to document what happens when
ret_valueis requested in the context where there is no ret value (e.g., fentry)ugh, that's not handled at the moment.. should we fail when we see
bpf_ret_valuehelper call in fentry program?
Well, two options, really. Either return zero or detect at verification time and fail verifications. I find myself leaning towards less restrictions at verification time, so I'd probably go with runtime check and zero. This allows to have the same BPF subprogram that can be called both from fentry/fexit with a proper if() guard to not do anything with the result of bpf_ret_value (as one example).
Quoted 22 lines
Quoted 15 lines
include/uapi/linux/bpf.h union bpf_attr { · line 5156
Quoted 11 lines
include/uapi/linux/bpf.h +2
-0*/ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \@@ -5140,6 +5152,8 @@ union bpf_attr {FN (skc_to_unix_sock), \FN (kallsyms_lookup_name), \FN (find_vma), \ +FN (arg), \ + FN(ret_value), \We already have
bpf_get_func_ip, so why not continue a tradition and call thesebpf_get_func_arg()andbpf_get_func_ret(). Nice, short, clean, consistent.ok
Quoted 3 lines
BTW, a wild thought. Wouldn't it be cool to have these functions work with kprobe/kretprobe as well? Do you think it's possible?
Quoted 7 lines
right,
bpf_get_func_ipalready works for kprobesstruct kprobe could have the
btf_func_modelof the traced function, so in case we trace function directly on the entry point we could read arguments registers based on thebtf_func_modelI'll check with Massami
Hm... I'd actually try to keep kprobe BTF-free. We have fentry for cases where BTF is present and the function is simple enough (like <=6 args, etc). Kprobe is an escape hatch mechanism when all the BTF fanciness just gets in the way (retsnoop being a primary example from my side). What I meant here was that bpf_get_arg(int n) would read correct fields from pt_regs that map to first N arguments passed in the registers. What we currently have with PT_REGS_PARM macros in bpf_tracing.h, but with a proper unified BPF helper.
Quoted 49 lines
Quoted 40 lines
Quoted 23 lines
include/uapi/linux/bpf.h +0
-0/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper kernel/bpf/verifier.c +10
-0@@ -13246,11 +13246,52 @@ static int do_misc_fixups(struct bpf_verifier_env *env)continue ; } +/* Implement bpf_arg inline. */ +if (prog_type ==BPF_PROG_TYPE_TRACING && + insn->imm == BPF_FUNC_arg) { +/* Load nr_args from ctx - 8 */ + insn_buf[0 ] =BPF_LDX_MEM (BPF_DW ,BPF_REG_0 ,BPF_REG_1 ,-8 ); + insn_buf[1 ] =BPF_JMP32_REG (BPF_JGE ,BPF_REG_2 ,BPF_REG_0 ,4 ); + insn_buf[2 ] =BPF_ALU64_IMM (BPF_MUL ,BPF_REG_2 ,8 ); + insn_buf[3 ] =BPF_ALU64_REG (BPF_ADD ,BPF_REG_2 ,BPF_REG_1 ); + insn_buf[4 ] =BPF_LDX_MEM (BPF_DW ,BPF_REG_0 ,BPF_REG_2 ,0 ); + insn_buf[5 ] =BPF_JMP_A (1 );kernel/bpf/verifier.c static int do_misc_fixups(struct bpf_verifier_env *env) · line 13268
Quoted 10 lines
kernel/bpf/verifier.c +10
-0+ insn_buf[6 ] =BPF_MOV64_IMM (BPF_REG_0 ,0 ); + + new_prog =bpf_patch_insn_data (env, i + delta, insn_buf,7 ); +if (!new_prog) +return -ENOMEM ; + + delta +=6 ; + env->prog = prog = new_prog; + insn = new_prog->insnsi + i + delta; +continue ;nit: this whole sequence of steps and calculations seems like something that might be abstracted and hidden behind a macro or helper func? Not related to your change, though. But wouldn't it be easier to understand if it was just written as:
PATCH_INSNS(
Quoted 8 lines
BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); BPF_JMP32_REG(BPF_JGE, BPF_REG_2, BPF_REG_0, 4); BPF_ALU64_IMM(BPF_MUL, BPF_REG_2, 8); BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_1); BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_2, 0); BPF_JMP_A(1); BPF_MOV64_IMM(BPF_REG_0, 0));continue;
Quoted 1 line
yep, looks better ;-) I'll check
as Alexei mentioned, might not be possible, but if variadic implementation turns out to be not too ugly, I think it might work. Macro can assume that insn_buf and all the other variables are there, so there shouldn't be any increase in stack size use, I think.
But this is just an item on a wishlist, so don't overstress about that.
Quoted 65 lines
Quoted 53 lines
?
Quoted 48 lines
kernel/bpf/verifier.c +23
-2+ } + +/* Implement bpf_ret_value inline. */ +if (prog_type ==BPF_PROG_TYPE_TRACING && + insn->imm == BPF_FUNC_ret_value) { +/* Load nr_args from ctx - 8 */ + insn_buf[0 ] =BPF_LDX_MEM (BPF_DW ,BPF_REG_2 ,BPF_REG_1 ,-8 ); + insn_buf[1 ] =BPF_ALU64_IMM (BPF_MUL ,BPF_REG_2 ,8 ); + insn_buf[2 ] =BPF_ALU64_REG (BPF_ADD ,BPF_REG_2 ,BPF_REG_1 ); + insn_buf[3 ] =BPF_LDX_MEM (BPF_DW ,BPF_REG_0 ,BPF_REG_2 ,0 ); + + new_prog =bpf_patch_insn_data (env, i + delta, insn_buf,4 ); +if (!new_prog) +return -ENOMEM ; + + delta +=3 ; + env->prog = prog = new_prog; + insn = new_prog->insnsi + i + delta; +continue ; + } +/* Implement bpf_get_func_ip inline. */ if (prog_type ==BPF_PROG_TYPE_TRACING && insn->imm == BPF_FUNC_get_func_ip) { -/* Load IP address from ctx - 8 */ - insn_buf[0 ] =BPF_LDX_MEM (BPF_DW ,BPF_REG_0 ,BPF_REG_1 ,-8 ); +/* Load IP address from ctx - 16 */ + insn_buf[0 ] =BPF_LDX_MEM (BPF_DW ,BPF_REG_0 ,BPF_REG_1 ,-16 ); new_prog =bpf_patch_insn_data (env, i + delta, insn_buf,1 );if (!new_prog)kernel/trace/bpf_trace.c +1
-1@@ -1012,7 +1012,7 @@ const struct bpf_func_proto bpf_snprintf_btf_proto = { BPF_CALL_1(bpf_get_func_ip_tracing, void *, ctx) {/* This helper call is inlined by verifier. */ -return ((u64 *)ctx)[-1 ]; +return ((u64 *)ctx)[-2 ]; }static const struct bpf_func_proto bpf_get_func_ip_proto_tracing = {@@ -1091,6 +1091,38 @@ static const struct bpf_func_proto bpf_get_branch_snapshot_proto = { .arg2_type = ARG_CONST_SIZE_OR_ZERO, };Quoted 11 lines
kernel/trace/bpf_trace.c static const struct bpf_func_proto bpf_get_branch_snapshot_proto = { · line 1100
Quoted 7 lines
kernel/trace/bpf_trace.c +7
-0+BPF_CALL_2(bpf_arg, void *, ctx, int, n) +{ +/* This helper call is inlined by verifier. */ +u64 nr_args = ((u64 *)ctx)[-1 ]; + +if ((u64 ) n >= nr_args) +return 0 ;We'll need
bpf_get_func_arg_cnt()helper as well to be able to know the actual number of arguments traced function has. It's impossible to know whether the argument is zero or there is no argument, otherwise.
Quoted 2 lines
my idea was that the program will call those helpers based on
get_func_ipwith proper argument indexes
see my comments on multi-attach kprobes, get_func_ip() is nice, but BPF cookies are often much better. So I wouldn't design everything with the assumption that user always has to use hashmap + get_func_ip().
Quoted 3 lines
but with
bpf_get_func_arg_cntwe could make a simple program that would just print function with all its arguments easily, ok ;-)
right, and many other more complicated functions that don't have to do runtime ip lookups ;)
Quoted 23 lines
Quoted 14 lines
Quoted 14 lines
kernel/trace/bpf_trace.c +12
-0+return ((u64 *)ctx)[n]; +} + +static const struct bpf_func_proto bpf_arg_proto = { + .func = bpf_arg, + .gpl_only = true, + .ret_type =RET_INTEGER , + .arg1_type =ARG_PTR_TO_CTX , + .arg1_type =ARG_ANYTHING , +}; + +BPF_CALL_1(bpf_ret_value, void *, ctx)Quoted 8 lines
kernel/trace/bpf_trace.c static const struct bpf_func_proto bpf_get_branch_snapshot_proto = { · line 1117
Quoted 5 lines
kernel/trace/bpf_trace.c +5
-0+{ +/* This helper call is inlined by verifier. */ +u64 nr_args = ((u64 *)ctx)[-1 ]; + +return ((u64 *)ctx)[nr_args];we should return 0 for fentry or disable this helper for anything but fexit? It's going to return garbage otherwise.
Quoted 2 lines
disabling seems like right choice to me
well, see above. I think we should prefer statically disabling something if it's harmful to enable otherwise, but for more flexibility and less headache with "proving to BPF verifier", I lean more and more towards runtime checks, if they are safe and not overly expensive or complicated.
Quoted 3 lines
thanks,
jirka
Re: [PATCH bpf-next 09/29] bpf: Add support to load multi func tracing program
In reply to Jiri OlsaQuoted 30 lines · Jiri Olsa
Quoted 28 lines · Andrii Nakryiko
Quoted 26 lines · Jiri Olsa
Quoted 8 lines · Alexei Starovoitov
kernel/bpf/syscall.c static bool is_perfmon_prog_type(enum bpf_prog_type prog_type) · line 2202
Quoted 4 lines · Jiri Olsa
kernel/bpf/syscall.c +4
-0+ +DEFINE_BPF_MULTI_FUNC(unsigned long a1, unsigned long a2, + unsigned long a3, unsigned long a4, + unsigned long a5, unsigned long a6)This is probably a bit too x86 specific. May be make add all 12 args? Or other places would need to be tweaked?
I think si, I'll check
Quoted 11 lines
Quoted 2 lines
+BTF_ID_LIST_SINGLE(bpf_multi_func_btf_id, func,
bpf_multi_func)...
kernel/bpf/syscall.c static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr) · line 2311
Quoted 2 lines
kernel/bpf/syscall.c +1
-1- prog->aux->attach_btf_id = attr->attach_btf_id; + prog->aux->attach_btf_id = multi_func ? bpf_multi_func_btf_id[0 ] : attr->attach_btf_id;Just ignoring that was passed in uattr?
Maybe instead of ignoring doprBPF_F_MULTI_FUNCand make libbpf
point to thatbtf_idinstead?
Then multi or not can be checked with if (attr->attach_btf_id== bpf_multi_func_btf_id[0]).nice idea, it might fit better than the flag
Quoted 9 lines
Quoted 2 lines
Instead of a flag we can also use a different
expected_attach_type(FENTRY vsFENTRY_MULTI, etc).right, you already asked for that - https://lore.kernel.org/bpf/YS9k26rRcUJVS%2Fvx@krava/
I still think it'd mean more code while this way we just use current fentry/fexit code paths with few special handling for multi programs
I don't see how it makes much difference for kernel implementation. Checking expected_attach_type vs checking prog_flags is about the same amount of code. The big advantage of new expected_attach_type (or prog_type) is that it will be very obvious in all sorts of diagnostics tooling (think bpftool prog show output, etc). prog_flags are almost invisible and it will be the last thing that users will think about. I'd try to minimize the usage of prog_flags overall.
Quoted 5 lines
Quoted 2 lines
As for
attach_btf_id, why can't we just
enforce it as 0?there's
prog->aux->attach_func_protothat needs to be set based onattach_btf_id, and is checked later inbtf_ctx_access
right:
if (attach_btf_id == 0)
prog->aux->attach_func_proto =
&special_func_model_or_proto_or_whatever_that_does_not_have_to_be_known_to_libbpf_and_outside_world_ever;
;) let's keep implementation details as internal implementation details, instead of dumping all that to UAPI
Quoted 10 lines
jirka
Quoted 6 lines
Quoted 5 lines
thanks,
jirka
Re: [PATCH bpf-next 06/29] bpf: Add bpf_arg/bpf_ret_value helpers for tracing programs
In reply to Andrii NakryikoQuoted 9 lines · Andrii Nakryiko
Hm... I'd actually try to keep kprobe BTF-free. We have fentry for cases where BTF is present and the function is simple enough (like <=6 args, etc). Kprobe is an escape hatch mechanism when all the BTF fanciness just gets in the way (retsnoop being a primary example from my side). What I meant here was that
bpf_get_arg(int n)would read correct fields frompt_regsthat map to first N arguments passed in the registers. What we currently have withPT_REGS_PARMmacros inbpf_tracing.h, but with a proper unified BPF helper.
and these macros are arch specific.
which means that it won't be a trivial patch to add bpf_get_arg() support for kprobes.
Plenty of things to consider. Like should it return an error at run-time or verification time when a particular arch is not supported. Or argument 6 might be available on one arch, but not on the other. 32-bit CPU regs vs 64-bit regs of BPF, etc.
I wouldn't attempt to mix this work with current patches.
Re: [PATCH bpf-next 06/29] bpf: Add bpf_arg/bpf_ret_value helpers for tracing programs
Quoted 13 lines · Alexei Starovoitov
Quoted 9 lines · Andrii Nakryiko
Hm... I'd actually try to keep kprobe BTF-free. We have fentry for cases where BTF is present and the function is simple enough (like <=6 args, etc). Kprobe is an escape hatch mechanism when all the BTF fanciness just gets in the way (retsnoop being a primary example from my side). What I meant here was that
bpf_get_arg(int n)would read correct fields frompt_regsthat map to first N arguments passed in the registers. What we currently have withPT_REGS_PARMmacros inbpf_tracing.h, but with a proper unified BPF helper.
Quoted 3 lines
and these macros are arch specific.
which means that it won't be a trivial patch to addbpf_get_arg()support for kprobes.
no one suggested it would be trivial :) things worth doing are usually non-trivial, as can be evidenced by Jiri's patch set
Quoted 2 lines
Plenty of things to consider. Like should it return an error at run-time or verification time when a particular arch is not supported.
See my other replies to Jiri, I'm more and more convinced that dynamic is the way to go for things like this, where the safety of the kernel or BPF program are not compromised.
But you emphasized an important point, that it's probably good to allow users to distinguish errors from reading actual value 0. There are and will be situations where argument isn't available or some combination of conditions are not supported. So I think, while it's a bit more verbose, these forms are generally better:
int bpf_get_func_arg(int n, u64 *value);
int bpf_get_func_ret(u64 *value);
WDYT?
Quoted 3 lines
Or argument 6 might be available on one arch, but not on the other. 32-bit CPU regs vs 64-bit regs of BPF, etc.
I wouldn't attempt to mix this work with current patches.
Oh, I didn't suggest doing it as part of this already huge and complicated set. But I think it's good to think a bit ahead and design the helper API appropriately, at the very least.
And again, I think bpf_get_func_arg/bpf_get_func_ret deserve their own patch set where we can discuss all this independently from multi-attach.
Re: [PATCH bpf-next 06/29] bpf: Add bpf_arg/bpf_ret_value helpers for tracing programs
Quoted 11 lines · Andrii Nakryiko
But you emphasized an important point, that it's probably good to allow users to distinguish errors from reading actual value 0. There are and will be situations where argument isn't available or some combination of conditions are not supported. So I think, while it's a bit more verbose, these forms are generally better:
int
bpf_get_func_arg(int n, u64 *value);
intbpf_get_func_ret(u64 *value);WDYT?
Makes sense to me.
The verifier will be able to inline it just fine.
Two extra insns only compared to direct return.
Re: [PATCH bpf-next 06/29] bpf: Add bpf_arg/bpf_ret_value helpers for tracing programs
In reply to Andrii NakryikoQuoted 35 lines · Andrii Nakryiko
Quoted 13 lines · Alexei Starovoitov
Quoted 9 lines · Andrii Nakryiko
Hm... I'd actually try to keep kprobe BTF-free. We have fentry for cases where BTF is present and the function is simple enough (like <=6 args, etc). Kprobe is an escape hatch mechanism when all the BTF fanciness just gets in the way (retsnoop being a primary example from my side). What I meant here was that
bpf_get_arg(int n)would read correct fields frompt_regsthat map to first N arguments passed in the registers. What we currently have withPT_REGS_PARMmacros inbpf_tracing.h, but with a proper unified BPF helper.Quoted 3 lines
and these macros are arch specific.
which means that it won't be a trivial patch to addbpf_get_arg()support for kprobes.no one suggested it would be trivial :) things worth doing are usually non-trivial, as can be evidenced by Jiri's patch set
Quoted 2 lines
Plenty of things to consider. Like should it return an error at run-time or verification time when a particular arch is not supported.
See my other replies to Jiri, I'm more and more convinced that dynamic is the way to go for things like this, where the safety of the kernel or BPF program are not compromised.
But you emphasized an important point, that it's probably good to allow users to distinguish errors from reading actual value 0. There are and will be situations where argument isn't available or some combination of conditions are not supported. So I think, while it's a bit more verbose, these forms are generally better:
Quoted 4 lines
int
bpf_get_func_arg(int n, u64 *value);
intbpf_get_func_ret(u64 *value);WDYT?
ok, good preparation for kprobe code quirks described by Alexei
Quoted 5 lines
Quoted 4 lines
Or argument 6 might be available on one arch, but not on the other. 32-bit CPU regs vs 64-bit regs of BPF, etc.
I wouldn't attempt to mix this work with current patches.
Quoted 8 lines
Oh, I didn't suggest doing it as part of this already huge and complicated set. But I think it's good to think a bit ahead and design the helper API appropriately, at the very least.
And again, I think bpf_get_func_arg/bpf_get_func_ret deserve their own patch set where we can discuss all this independently from multi-attach.
good ;-) thanks,
jirka
Re: [PATCH bpf-next 09/29] bpf: Add support to load multi func tracing program
In reply to Andrii NakryikoQuoted 41 lines · Andrii Nakryiko
Quoted 30 lines · Jiri Olsa
Quoted 28 lines · Andrii Nakryiko
Quoted 26 lines · Jiri Olsa
Quoted 8 lines · Alexei Starovoitov
kernel/bpf/syscall.c static bool is_perfmon_prog_type(enum bpf_prog_type prog_type) · line 2202
Quoted 4 lines · Jiri Olsa
kernel/bpf/syscall.c +4
-0+ +DEFINE_BPF_MULTI_FUNC(unsigned long a1, unsigned long a2, + unsigned long a3, unsigned long a4, + unsigned long a5, unsigned long a6)This is probably a bit too x86 specific. May be make add all 12 args? Or other places would need to be tweaked?
I think si, I'll check
Quoted 11 lines
Quoted 2 lines
+BTF_ID_LIST_SINGLE(bpf_multi_func_btf_id, func,
bpf_multi_func)...
kernel/bpf/syscall.c static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr) · line 2311
Quoted 2 lines
kernel/bpf/syscall.c +1
-1- prog->aux->attach_btf_id = attr->attach_btf_id; + prog->aux->attach_btf_id = multi_func ? bpf_multi_func_btf_id[0 ] : attr->attach_btf_id;Just ignoring that was passed in uattr?
Maybe instead of ignoring doprBPF_F_MULTI_FUNCand make libbpf
point to thatbtf_idinstead?
Then multi or not can be checked with if (attr->attach_btf_id== bpf_multi_func_btf_id[0]).nice idea, it might fit better than the flag
Quoted 9 lines
Quoted 2 lines
Instead of a flag we can also use a different
expected_attach_type(FENTRY vsFENTRY_MULTI, etc).right, you already asked for that - https://lore.kernel.org/bpf/YS9k26rRcUJVS%2Fvx@krava/
I still think it'd mean more code while this way we just use current fentry/fexit code paths with few special handling for multi programs
Quoted 7 lines
I don't see how it makes much difference for kernel implementation. Checking
expected_attach_typevs checkingprog_flagsis about the same amount of code. The big advantage of newexpected_attach_type(orprog_type) is that it will be very obvious in all sorts of diagnostics tooling (think bpftool prog show output, etc).prog_flagsare almost invisible and it will be the last thing that users will think about. I'd try to minimize the usage ofprog_flagsoverall.
ok, I'll check on that.. I recall adding this new type in many expected_attach_type switches, which made me think the new flag will be easier
Quoted 7 lines
Quoted 6 lines
Quoted 3 lines
As for
attach_btf_id, why can't we just
enforce it as 0?there's
prog->aux->attach_func_protothat needs to be set based onattach_btf_id, and is checked later inbtf_ctx_access
Quoted 8 lines
right:
if (
attach_btf_id== 0)prog->aux->attach_func_proto =
&special_func_model_or_proto_or_whatever_that_does_not_have_to_be_known_to_libbpf_and_outside_world_ever;;) let's keep implementation details as internal implementation details, instead of dumping all that to UAPI
ok, we can do that ;-)
thanks,
jirka