bpf: Attach kprobe multi link to inlined functions
Subsystems (15)
Jiri Olsa
reviewercover bpf: Attach kprobe multi link to inlined functions
hi,
the kprobe multi link attaches only the out-of-line copy of the function, so inlined instances are not traced and a function inlined in all its callers can't be attached at all.
This patchset adds BPF_F_KPROBE_MULTI_INLINE flag that attaches also the inlined instances of the functions in the syms array. The instances are resolved from inline BTF (CONFIG_DEBUG_INFO_BTF_INLINE) of vmlinux and modules and each gets its own kprobe.
Only instances inlined in traceable functions (with ftrace location) are attached, instances in notrace functions are likely not safe to probe. Those and the instances that kprobes refuse are skipped, or fail the attach with BPF_F_KPROBE_MULTI_INLINE_STRICT flag.
The program attached to inlined instance can use cookies, bpf_get_func_ip and bpf_get_func_arg[_cnt] helpers. The instances are visible in link info, fdinfo and bpftool.
The BPF_F_KPROBE_MULTI_INLINE flag can't be used with return probe, session program, addrs array or bpf_override_return. The support is x86_64 only for now.
- patches 1-5: x86 ptrace, kprobes, module and BTF preparation
- patches 6-16: kprobe multi inline attach
- patches 17-19: libbpf and bpftool support
- patches 20-30: selftests
The kprobe_multi_bench_attach test in KVM guest, 67295 vmlinux functions:
instances attach detach without inline - 0.324s 0.154s with inline 19947 6.443s 0.361s
The speed of the inline attach depends on several kprobe changes. Two of them are part of this series:
kprobes: Make the kprobe hash table size configurable
kprobes: Add enable_kprobes to enable multiple kprobes at once
and two were posted separately:
[1] jump_label/x86: Avoid decoding instructions for jump entry size
[2] x86/kprobes: Use ftrace_location_range in __recover_probed_insn
I think there's still space for kprobe changes that would make the attach faster, especially the follow up fixes for [3]. I'm sending this as RFC to get feedback on the interface, while I work on the kprobe attach speedup.
thanks,
jirka
[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/
[3] https://lore.kernel.org/bpf/[email protected]/
---
Jiri Olsa (31):
x86/ptrace: Add regs_get_dwarf_register function
kprobes: Make the kprobe hash table size configurable
kprobes: Add enable_kprobes to enable multiple kprobes at once
module: Add module_sect_addr function
bpf: Add bpf_get_btf_vmlinux_inline function
bpf: Use goto labels for error path in bpf_kprobe_multi_link_attach
bpf: Add support to attach inlined functions with kprobe multi link
bpf: Add cookie support for kprobe multi inlined functions
bpf: Add link info support for kprobe multi inlined functions
bpf: Add fdinfo support for kprobe multi inlined functions
bpf: Add btf_for_each_module_inline function
bpf: Change get_modules_for_addrs to use struct modules_array
tbpf: Add support to attach inlined functions in modules with kprobe multi link
bpf: Add BPF_F_KPROBE_MULTI_INLINE_STRICT flag for kprobe multi link
bpf: Add get_func_arg[_cnt] support for kprobe multi functions
bpf: Add get_func_arg[_cnt] support for kprobe multi inlined functions
bpf: Add log messages for kprobe multi inline attach
bpf: Add inlined functions for kprobe multi inline tests
libbpf: Add inline options to bpf_kprobe_multi_opts
libbpf: Print kernel log for failed kprobe multi attach
bpftool: Display inlined functions for kprobe multi link selftests/bpf: Add inlined functions to bpf_testmod selftests/bpf: Add kprobe multi inline attach test
selftests/bpf: Add kprobe multi inline mixed test
selftests/bpf: Add kprobe multi inline skip test
selftests/bpf: Add kprobe multi inline fails test
selftests/bpf: Add kprobe multi inline module test
selftests/bpf: Add kprobe multi inline strict test
selftests/bpf: Add kprobe multi inline args test
selftests/bpf: Add kprobe multi inline bench attach test selftests/bpf: Add kprobe multi inline link info test
arch/Kconfig | 23 ++ arch/x86/Kconfig | 1 + arch/x86/include/asm/ptrace.h | 8 + arch/x86/kernel/kprobes/core.c | 13 + arch/x86/kernel/ptrace.c | 42 +++ include/linux/bpf.h | 2 + include/linux/btf.h | 17 + include/linux/kprobes.h | 7 + include/linux/module.h | 9 + include/linux/ptrace.h | 10 + include/linux/trace_events.h | 7 +- include/uapi/linux/bpf.h | 29 +- kernel/bpf/btf.c | 157 ++++++++- kernel/bpf/syscall.c | 2 +- kernel/bpf/sysfs_btf.c | 2 +- kernel/bpf/verifier.c | 3 + kernel/kprobes.c | 92 ++++- kernel/module/sysfs.c | 20 ++ kernel/trace/bpf_trace.c | 921 ++++++++++++++++++++++++++++++++++++++++++++++--- net/bpf/test_run.c | 83 ++++- tools/bpf/bpftool/link.c | 348 ++++++++++++++++--- tools/include/uapi/linux/bpf.h | 29 +- tools/lib/bpf/libbpf.c | 29 +- tools/lib/bpf/libbpf.h | 6 +- tools/testing/selftests/bpf/prog_tests/fill_link_info.c | 251 ++++++++++++++ tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c | 963 ++++++++++++++++++++++++++++++++++++++++++++++++++++ tools/testing/selftests/bpf/progs/kprobe_multi_inline.c | 79 +++++ tools/testing/selftests/bpf/test_kmods/bpf_testmod.c | 28 ++ 28 files changed, 3058 insertions(+), 123 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/kprobe_multi_inline.c
Jiri Olsa
reviewerRFC PATCH 1/31 x86/ptrace: Add regs_get_dwarf_register function
Add regs_get_dwarf_register function that returns value of the register specified by its DWARF register number from pt_regs.
It will be used in following changes to read arguments of inlined functions, where the argument location is described in inline BTF with DWARF register numbers.
The x86_64 version maps the DWARF register numbers as defined by psABI, other archs get the generic version that returns -EOPNOTSUPP.
--- arch/x86/include/asm/ptrace.h | 8 +++++++ arch/x86/kernel/ptrace.c | 42 +++++++++++++++++++++++++++++++++++ include/linux/ptrace.h | 10 +++++++++ 3 files changed, 60 insertions(+)
arch/x86/include/asm/ptrace.h +8 -0
@@ -317,6 +317,14 @@ static __always_inline bool regs_irqs_disabled(struct pt_regs *regs)/* Query offset/name of register from its name/offset */ extern int regs_query_register_offset (const char *name);extern const char *regs_query_register_name (unsigned int offset); + +#ifdef CONFIG_X86_64 +/* Get value of the register specified by DWARF register number. */ +extern int regs_get_dwarf_register (struct pt_regs *regs,unsigned int regno, +unsigned long *value); +#define regs_get_dwarf_register regs_get_dwarf_register +#endif +#define MAX_REG_OFFSET (offsetof(struct pt_regs, ss))/**
arch/x86/kernel/ptrace.c +42 -0
48 lines not on this page · raw message
include/linux/ptrace.h +10 -0
@@ -398,6 +398,16 @@ static inline void user_single_step_report(struct pt_regs *regs)#define exception_ip (x) instruction_pointer(x)#endif +#ifndef regs_get_dwarf_register +/* Get value of the register specified by DWARF register number. */ +static inline int regs_get_dwarf_register (struct pt_regs *regs, +unsigned int regno, +unsigned long *value) +{ +return -EOPNOTSUPP ; +} +#endif +extern int task_current_syscall (struct task_struct *target,struct syscall_info *info);extern void sigaction_compat_abi (struct k_sigaction *act,struct k_sigaction *oact);
Signature · 1 line
2.56.1
Jiri Olsa
reviewerRFC PATCH 2/31 kprobes: Make the kprobe hash table size configurable
In reply to Jiri OlsaThe kprobe hash table has 64 buckets, so with 20000 probes registered each get_kprobe() call walks a chain of more than 300 entries on average.
On x86 the registration path calls get_kprobe() for each byte or decoded instruction it has to check around the new probe, and most of these lookups miss and walk the whole chain. It is also called on every probe hit.
This matters for attaching to inlined functions, where a few functions specified by name can end up with thousands of kprobes.
Add CONFIG_KPROBE_HASH_BITS with range 6-16. The default stays at 6 and is raised to 10 (1024 buckets, 8KB on 64-bit) with CONFIG_DEBUG_INFO_BTF_INLINE.
Time to register the probes with the kprobe_many_example sample module [1] in a KVM guest, mean of 5 runs:
bits buckets table memory 20000 probes 50000 probes
6 64 512 B 4.71 s 29.99 s
8 256 2 KB 2.05 s 9.90 s
10 1024 8 KB 1.39 s 4.63 s
12 4096 32 KB 1.30 s 3.64 s
14 16384 128 KB 1.28 s 3.41 s
16 65536 512 KB 1.28 s 3.38 sWith 10 bits the registration is 3.4 times faster for 20000 probes and 6.5 times faster for 50000 probes. Bigger tables help only with very high numbers of probes.
[1] https://lore.kernel.org/bpf/[email protected]/
--- arch/Kconfig | 17 +++++++++++++++++ kernel/kprobes.c | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-)
arch/Kconfig +17 -0
@@ -141,6 +141,23 @@ config KPROBES for kernel debugging, non-intrusive instrumentation and testing. If in doubt, say "N". +config KPROBE_HASH_BITS + int "Kprobe hash table size in bits" if EXPERT + depends on KPROBES + range 6 16 + default 10 if DEBUG_INFO_BTF_INLINE + default 6 + help + Size of the kprobe hash table as a power of two. The default + of 6 (64 buckets) is enough for a small number of probes. + + Attaching to inlined functions can register thousands of + kprobes at once, so the default is raised to 10 (1024 buckets, + 8KB on 64-bit) when the inline site information is available + (DEBUG_INFO_BTF_INLINE). + + If in doubt, keep the default. + config JUMP_LABEL bool "Optimize very unlikely/likely branches" depends on HAVE_ARCH_JUMP_LABEL
kernel/kprobes.c +1 -1
@@ -50,7 +50,7 @@#include <asm/errno.h> #include <linux/uaccess.h> -#define KPROBE_HASH_BITS 6 +#define KPROBE_HASH_BITS CONFIG_KPROBE_HASH_BITS#define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)#if !defined(CONFIG_OPTPROBES ) || !defined(CONFIG_SYSCTL )
Signature · 1 line
2.56.1
Jiri Olsa
reviewerRFC PATCH 3/31 kprobes: Add enable_kprobes to enable multiple kprobes at once
In reply to Jiri OlsaEnabling kprobe puts the breakpoint in place and waits for all the other CPUs to see it. On x86 that is an IPI to every CPU for each kprobe, which gets expensive with thousands of kprobes.
Adding enable_kprobes function that enables array of kprobes under single kprobe_mutex lock, installs their breakpoints without waiting for the other CPUs and syncs them just once at the end.
To support that adding two arch functions:
arch_arm_kprobe_nosync - installs the breakpoint without the sync arch_arm_kprobes_sync - syncs all the breakpoints installed so far
The default arch_arm_kprobe_nosync falls back to arch_arm_kprobe and the default arch_arm_kprobes_sync does nothing, so there's no change for other architectures. Adding the x86 version.
The ftrace based kprobes are armed through ftrace as before, there's no difference for them.
If any of the kprobes fails to get enabled, enable_kprobes returns the error and leaves the kprobes enabled so far as they are, the caller can find them with kprobe_disabled(). The sync is done also in this case.
Time to enable disabled kprobes with the kprobe_many_example sample module [1], enable_kprobe() in a loop against single enable_kprobes() call, on bare metal AMD Ryzen 7 PRO 8840HS (16 CPUs), median of 6 runs, CONFIG_KPROBE_HASH_BITS=16:
probes enable_kprobe enable_kprobes speedup
1000 12.1 ms 0.36 ms 34x
5000 55.3 ms 1.70 ms 33x
10000 102 ms 3.50 ms 29x
20000 187 ms 7.30 ms 26x
50000 354 ms 21.4 ms 17xThe speedup depends on the kprobe hash table size, because arming the kprobe does several get_kprobe() lookups. With CONFIG_KPROBE_HASH_BITS=10 it is 33x for 1000 kprobes, but only 2.7x for 20000 and 1.3x for 50000, where the hash chain walks take most of the time.
[1] https://lore.kernel.org/bpf/[email protected]/
--- arch/x86/kernel/kprobes/core.c | 13 +++++ include/linux/kprobes.h | 7 +++ kernel/kprobes.c | 90 +++++++++++++++++++++++++++++----- 3 files changed, 99 insertions(+), 11 deletions(-)
arch/x86/kernel/kprobes/core.c +13 -0
@@ -793,6 +793,19 @@ void arch_arm_kprobe(struct kprobe *p)perf_event_text_poke (p->addr, &p->opcode,1 , &int3,1 ); } +void arch_arm_kprobe_nosync (struct kprobe *p) +{ +u8 int3 =INT3_INSN_OPCODE ; + +text_poke (p->addr, &int3,1 ); +perf_event_text_poke (p->addr, &p->opcode,1 , &int3,1 ); +} + +void arch_arm_kprobes_sync (void ) +{ +smp_text_poke_sync_each_cpu (); +} +void arch_disarm_kprobe (struct kprobe *p) {u8 int3 =INT3_INSN_OPCODE ;
include/linux/kprobes.h +7 -0
@@ -272,6 +272,8 @@ extern struct kretprobe_blackpoint kretprobe_blacklist[];extern int arch_prepare_kprobe (struct kprobe *p);extern void arch_arm_kprobe (struct kprobe *p); +extern void arch_arm_kprobe_nosync (struct kprobe *p); +extern void arch_arm_kprobes_sync (void );extern void arch_disarm_kprobe (struct kprobe *p);extern int arch_init_kprobes (void );extern void kprobes_inc_nmissed_count (struct kprobe *p);
@@ -423,6 +425,7 @@ void kprobe_free_init_mem(void);int disable_kprobe (struct kprobe *kp);int enable_kprobe (struct kprobe *kp); +int enable_kprobes (struct kprobe **kps,int num);void dump_kprobe (struct kprobe *kp);
@@ -498,6 +501,10 @@ static inline int disable_kprobe(struct kprobe *kp) {return -EOPNOTSUPP ; } +static inline int enable_kprobes (struct kprobe **kps,int num) +{ +return -EOPNOTSUPP ; +}static inline int enable_kprobe (struct kprobe *kp) {return -EOPNOTSUPP ;
kernel/kprobes.c +79 -11
153 lines not on this page · raw message
Signature · 1 line
2.56.1
Jiri Olsa
reviewerRFC PATCH 4/31 module: Add module_sect_addr function
In reply to Jiri OlsaAdd module_sect_addr() that returns the runtime address of the loaded module section with given name, or 0 if there's no such section or the section attributes are not available (they need CONFIG_KALLSYMS and CONFIG_SYSFS).
The caller needs to make sure the module is live and hold a reference to it, which keeps the section attributes stable, so no other locking is needed.
It will be used to resolve addresses of inlined instances in modules, which are stored in inline BTF as section relative offsets.
--- include/linux/module.h | 9 +++++++++ kernel/module/sysfs.c | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+)
include/linux/module.h +9 -0
@@ -952,6 +952,15 @@ static inline bool module_sig_ok(struct module *module) }#endif /* CONFIG_MODULE_SIG */ +#if defined(CONFIG_MODULES ) && defined(CONFIG_KALLSYMS ) && defined(CONFIG_SYSFS ) +unsigned long module_sect_addr (const struct module *mod,const char *name); +#else +static inline unsigned long module_sect_addr (const struct module *mod,const char *name) +{ +return 0 ; +} +#endif +#if defined(CONFIG_MODULES ) && defined(CONFIG_KALLSYMS ) int module_kallsyms_on_each_symbol(const char *modname, int (*fn)(void *, const char *, unsigned long),
kernel/module/sysfs.c +20 -0
@@ -133,6 +133,26 @@ static void remove_sect_attrs(struct module *mod) } } +/* +* Returns address of the loaded section @name or 0 if there's no such +* section. The caller needs to make sure the module is live +* (MODULE_STATE_LIVE) and hold a reference to it, which keeps +* mod->sect_attrs stable; no other locking is needed. +*/ +unsigned long module_sect_addr (const struct module *mod,const char *name) +{ +const struct bin_attribute *const *bin_attr; + +if (!mod->sect_attrs) +return 0 ; + +for (bin_attr = mod->sect_attrs->grp.bin_attrs; *bin_attr; bin_attr++) { +if (!strcmp ((*bin_attr)->attr.name, name)) +return (unsigned long )(*bin_attr)->private; + } +return 0 ; +} +/* * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections. */
Signature · 1 line
2.56.1
Jiri Olsa
reviewerRFC PATCH 5/31 bpf: Add bpf_get_btf_vmlinux_inline function
In reply to Jiri OlsaAdd bpf_get_btf_vmlinux_inline() that returns parsed vmlinux inline BTF (vmlinux.inline), the split BTF on top of vmlinux BTF that describes inline sites of kernel functions.
The inline BTF is parsed on first use and stays cached, so there's no cost unless something asks for it. The data comes from the BTF inline section when CONFIG_DEBUG_INFO_BTF_INLINE is built in, otherwise from the lazily loaded data behind the vmlinux.inline sysfs attribute, exporting btf_sysfs_lazy_data() for that.
Make btf_parse_split() available also for CONFIG_DEBUG_INFO_BTF_INLINE and keep btf_is_module() false for vmlinux.inline BTF.
--- include/linux/bpf.h | 1 + include/linux/btf.h | 6 +++ kernel/bpf/btf.c | 93 +++++++++++++++++++++++++++++++++++++++++- kernel/bpf/sysfs_btf.c | 2 +- 4 files changed, 99 insertions(+), 3 deletions(-)
include/linux/bpf.h +1 -0
@@ -3346,6 +3346,7 @@ static inline s32 bpf_call_args_imm(s16 idx)#endif struct btf *bpf_get_btf_vmlinux (void ); +struct btf *bpf_get_btf_vmlinux_inline (void );/* Map specifics */ struct xdp_frame ;
include/linux/btf.h +6 -0
@@ -646,6 +646,7 @@ int btf_check_iter_arg(struct btf *btf, const struct btf_type *func, int arg_idxstruct bin_attribute *sysfs_btf_add (const char *name,void *data,size_t data_size,bool mmap,const char *lazy_module_name);bool sysfs_btf_update (struct bin_attribute *attr,void *data); +void *btf_sysfs_lazy_data (const struct bin_attribute *attr);void sysfs_btf_remove (struct bin_attribute *attr);#else static inline struct bin_attribute *
@@ -660,6 +661,11 @@ static inline bool sysfs_btf_update(struct bin_attribute *attr, void *data)return false; } +static inline void *btf_sysfs_lazy_data (const struct bin_attribute *attr) +{ +return NULL ; +} +static inline void sysfs_btf_remove (struct bin_attribute *attr) { }
kernel/bpf/btf.c +91 -2
117 lines not on this page · raw message
kernel/bpf/sysfs_btf.c +1 -1
@@ -74,7 +74,7 @@ struct btf_sysfs_entry {char *module_name; }; -static void *btf_sysfs_lazy_data (const struct bin_attribute *attr) +void *btf_sysfs_lazy_data (const struct bin_attribute *attr) { struct btf_sysfs_entry *entry = container_of(attr, struct btf_sysfs_entry, attr);void *data;
Signature · 1 line
2.56.1
Jiri Olsa
reviewerRFC PATCH 6/31 bpf: Use goto labels for error path in bpf_kprobe_multi_link_attach
In reply to Jiri OlsaThe error handling after the link is primed repeats the cleanup in each failing branch. Use goto labels instead, so it's easier to add another step that can fail in following changes.
There's no functional change intended.
--- kernel/trace/bpf_trace.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-)
kernel/trace/bpf_trace.c +10 -9
@@ -2943,21 +2943,22 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr } err =get_modules_for_addrs (&link->mods, addrs, cnt); -if (err <0 ) { -bpf_link_cleanup (&link_primer); -return err; - } +if (err <0 ) + goto error_cleanup; link->mods_cnt = err; err =register_fprobe_ips (&link->fp, addrs, cnt); -if (err) { -kprobe_multi_put_modules (link->mods, link->mods_cnt); -bpf_link_cleanup (&link_primer); -return err; - } +if (err) + goto error_modules;return bpf_link_settle (&link_primer); +error_modules: +kprobe_multi_put_modules (link->mods, link->mods_cnt); +error_cleanup: +bpf_link_cleanup (&link_primer); +return err; + error:kfree (link);kvfree (addrs);
Signature · 1 line
2.56.1
Jiri Olsa
reviewerRFC PATCH 7/31 bpf: Add support to attach inlined functions with kprobe multi link
In reply to Jiri OlsaThe kprobe multi link attaches functions through fprobe, so it gets only to the out-of-line copy of the function. The inlined instances are not traced, and a function that is inlined in all its callers can't be attached at all.
Adding BPF_F_KPROBE_MULTI_INLINE flag that makes the link attach also the inlined instances of the functions provided in the syms array.
The instances are resolved by function name from the vmlinux inline BTF (inline.text LOCSEC). Each of them gets its own kprobe and the kprobes are enabled all at once with enable_kprobes. Instances of two functions can share the same address, the program is executed for both.
Each function needs to have either entry or at least one inlined instance, otherwise the attach fails with -ESRCH.
Instances that kprobes refuse (jump label or static call site, bug table entry, NOKPROBE_SYMBOL or noinstr caller) are skipped, and so are instances inlined in functions without ftrace location (notrace), which are likely not safe to probe. With all the attachable functions in vmlinux that's 748 instances out of 20688. The attach fails if a function has no entry and all its instances are skipped.
The program attached to inlined instance gets pt_regs of the probed instruction, so the function arguments are not available in the argument registers. The bpf_get_func_ip helper returns the address of the inlined instance.
The flag can't be combined with BPF_F_KPROBE_MULTI_RETURN flag, session program, the addrs array or program that uses bpf_override_return.
The number of inlined instances is limited to MAX_KPROBE_MULTI_CNT, the attach fails with -E2BIG above that.
The support is x86_64 only for now, selected with the new HAVE_KPROBE_MULTI_INLINE config option. The attach with the flag fails with -EOPNOTSUPP on other architectures, on kernel without inline BTF and without CONFIG_KPROBES.
The support for cookies and for inlined instances in modules is added in following changes.
--- arch/Kconfig | 6 + arch/x86/Kconfig | 1 + include/uapi/linux/bpf.h | 8 +- kernel/trace/bpf_trace.c | 379 +++++++++++++++++++++++++++++++-- tools/include/uapi/linux/bpf.h | 8 +- 5 files changed, 384 insertions(+), 18 deletions(-)
arch/Kconfig +6 -0
@@ -308,6 +308,12 @@ config HAVE_OPTPROBES config HAVE_KPROBES_ON_FTRACE bool +config HAVE_KPROBE_MULTI_INLINE + bool + help + The architecture supports kprobe multi link attach to inlined + functions (BPF_F_KPROBE_MULTI_INLINE). + config ARCH_CORRECT_STACKTRACE_ON_KRETPROBE bool help
arch/x86/Kconfig +1 -0
@@ -262,6 +262,7 @@ config X86 select HAVE_KERNEL_ZSTD select HAVE_KPROBES select HAVE_KPROBES_ON_FTRACE + select HAVE_KPROBE_MULTI_INLINE if X86_64 select HAVE_FUNCTION_ERROR_INJECTION select HAVE_KRETPROBES select HAVE_RETHOOK
include/uapi/linux/bpf.h +6 -2
@@ -1365,10 +1365,14 @@ enum bpf_perf_event_type {#define BPF_F_TEST_REG_INVARIANTS (1U << 7)/* link_create.kprobe_multi.flags used in LINK_CREATE command for -* BPF_TRACE_KPROBE_MULTI attach type to create return probe. +* BPF_TRACE_KPROBE_MULTI attach type. */ enum { -BPF_F_KPROBE_MULTI_RETURN = (1U <<0 ) +/* Create return probe. */ +BPF_F_KPROBE_MULTI_RETURN = (1U <<0 ), + +/* Attach also to inlined function instances, skip those that fail. */ +BPF_F_KPROBE_MULTI_INLINE = (1U <<1 ), };/* link_create.uprobe_multi.flags used in LINK_CREATE command for
kernel/trace/bpf_trace.c +365 -14
500 lines not on this page · raw message
tools/include/uapi/linux/bpf.h +6 -2
@@ -1365,10 +1365,14 @@ enum bpf_perf_event_type {#define BPF_F_TEST_REG_INVARIANTS (1U << 7)/* link_create.kprobe_multi.flags used in LINK_CREATE command for -* BPF_TRACE_KPROBE_MULTI attach type to create return probe. +* BPF_TRACE_KPROBE_MULTI attach type. */ enum { -BPF_F_KPROBE_MULTI_RETURN = (1U <<0 ) +/* Create return probe. */ +BPF_F_KPROBE_MULTI_RETURN = (1U <<0 ), + +/* Attach also to inlined function instances, skip those that fail. */ +BPF_F_KPROBE_MULTI_INLINE = (1U <<1 ), };/* link_create.uprobe_multi.flags used in LINK_CREATE command for
Signature · 1 line
2.56.1
Jiri Olsa
reviewerRFC PATCH 8/31 bpf: Add cookie support for kprobe multi inlined functions
In reply to Jiri OlsaAdd support to use cookies together with BPF_F_KPROBE_MULTI_INLINE flag.
Each inlined instance carries the cookie configured for its function and the instance is passed to the program through the run context, so bpf_get_attach_cookie returns the cookie of the instance's function.
When instances of several functions share the same address (nested inlines), the handler of each kprobe is called separately, so each instance runs the program with its own cookie.
Functions without the entry are removed from the addrs array, so remove them from the cookies array as well to keep both arrays in sync.
--- kernel/trace/bpf_trace.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)
kernel/trace/bpf_trace.c +16 -4
@@ -2371,6 +2371,7 @@ struct bpf_kprobe_multi_link;struct inline_site {struct kprobe kp;struct bpf_kprobe_multi_link *link; +u64 cookie;u32 sym_idx;bool has_entry;bool traceable;
@@ -2402,6 +2403,7 @@ struct bpf_kprobe_multi_run_ctx {struct bpf_session_run_ctx session_ctx;struct bpf_kprobe_multi_link *link;unsigned long entry_ip; +struct inline_site *site; };struct user_syms {
@@ -2629,6 +2631,8 @@ static u64 bpf_kprobe_multi_cookie(struct bpf_run_ctx *ctx)return 0 ; run_ctx = container_of(current->bpf_ctx, struct bpf_kprobe_multi_run_ctx, session_ctx.run_ctx); +if (run_ctx->site) +return run_ctx->site->cookie; link = run_ctx->link;if (!link->cookies)return 0 ;
@@ -2652,6 +2656,7 @@ static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx) static __always_inline intkprobe_multi_link_prog_run (struct bpf_kprobe_multi_link *link, +struct inline_site *site,unsigned long entry_ip,struct ftrace_regs *fregs,struct pt_regs *regs,bool is_return,void *data) {
@@ -2662,6 +2667,7 @@ kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link *link, }, .link = link, .entry_ip = entry_ip, + .site = site, };struct bpf_run_ctx *old_run_ctx;int err;
@@ -2704,7 +2710,7 @@ kprobe_multi_link_handler(struct fprobe *fp, unsigned long fentry_ip,int err; link = container_of(fp, struct bpf_kprobe_multi_link, fp); - err =kprobe_multi_link_prog_run (link,ftrace_get_entry_ip (fentry_ip), + err =kprobe_multi_link_prog_run (link,NULL ,ftrace_get_entry_ip (fentry_ip), fregs,NULL , false, data);return is_kprobe_session (link->link.prog) ? err :0 ; }
@@ -2717,7 +2723,7 @@ kprobe_multi_link_exit_handler(struct fprobe *fp, unsigned long fentry_ip,struct bpf_kprobe_multi_link *link; link = container_of(fp, struct bpf_kprobe_multi_link, fp); -kprobe_multi_link_prog_run (link,ftrace_get_entry_ip (fentry_ip), +kprobe_multi_link_prog_run (link,NULL ,ftrace_get_entry_ip (fentry_ip), fregs,NULL , true, data); }
@@ -2727,7 +2733,7 @@ kprobe_multi_inline_handler(struct kprobe *kp, struct pt_regs *regs)struct inline_site *site; site = container_of(kp, struct inline_site, kp); -kprobe_multi_link_prog_run (site->link, (unsigned long )kp->addr, +kprobe_multi_link_prog_run (site->link, site, (unsigned long )kp->addr,NULL , regs, false,NULL );return 0 ; }
@@ -2885,6 +2891,7 @@ struct inline_lookup {/* Input: functions to look for, @syms is alphabetically sorted. */ const char **syms;unsigned long *addrs; +u64 *cookies;u32 syms_cnt;/* Output */
@@ -2988,6 +2995,7 @@ static int inline_lookup_symbols(struct inline_lookup *lookup)struct inline_sites *inlines = lookup->inlines;unsigned long *addrs = lookup->addrs;u32 i, j, k, n, cnt = lookup->syms_cnt; +u64 *cookies = lookup->cookies;struct inline_site *sites;struct btf *btf;int err;
@@ -3015,13 +3023,14 @@ static int inline_lookup_symbols(struct inline_lookup *lookup) sites[i].kp.pre_handler = kprobe_multi_inline_handler;/* Registered disabled and enabled all at once. */ sites[i].kp.flags =KPROBE_FLAG_DISABLED ; + sites[i].cookie = cookies ? cookies[sites[i].sym_idx] :0 ; sites[i].has_entry = !!addrs[sites[i].sym_idx]; sites[i].traceable =inline_site_traceable ((unsigned long )sites[i].kp.addr); }/* * Each symbol needs to have either entry or inlined instance, -* keep only symbols with entry in @addrs array. +* keep only symbols with entry in @addrs and @cookies arrays. * The instances are sorted by symbol name, so we walk them together. */ for (i =0 , j =0 , k =0 ; i < cnt; i++) {
@@ -3034,6 +3043,8 @@ static int inline_lookup_symbols(struct inline_lookup *lookup) goto error; } addrs[j] = addrs[i]; +if (cookies) + cookies[j] = cookies[i]; j++; }
@@ -3224,6 +3235,7 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr .syms = us.syms, .syms_cnt = cnt, .addrs = addrs, + .cookies = cookies, }; err =inline_lookup_symbols (&lookup);
Signature · 1 line
2.56.1
Jiri Olsa
reviewerRFC PATCH 9/31 bpf: Add link info support for kprobe multi inlined functions
In reply to Jiri OlsaThe kprobe multi link info shows just the functions attached with fprobe, the inlined instances are not visible.
Adding new fields to struct bpf_link_info for the attached inlined instances:
inline_addrs - addresses of the inlined instances
inline_cookies - cookies of the inlined instances
inline_ids - BTF ids of the inlined functions
inline_count - number of the inlined instances
inline_skipped - number of instances that could not be attached
The BTF id is needed to get the function name, because instances of two functions can share the same address.
The arrays have the same semantics as the addrs/cookies arrays, with inline_count as in/out value.
Also adding the missed count of the inlined instances to the missed value in the link info.
--- include/uapi/linux/bpf.h | 5 +++ kernel/trace/bpf_trace.c | 70 ++++++++++++++++++++++++++++++++-- tools/include/uapi/linux/bpf.h | 5 +++ 3 files changed, 76 insertions(+), 4 deletions(-)
include/uapi/linux/bpf.h +5 -0
@@ -6979,6 +6979,11 @@ struct bpf_link_info {__u32 flags;__u64 missed;__aligned_u64 cookies; +__aligned_u64 inline_addrs; +__aligned_u64 inline_cookies; +__aligned_u64 inline_ids; +__u32 inline_count;/* in/out: kprobe_multi inlined instances count */ +__u32 inline_skipped; } kprobe_multi;struct {__aligned_u64 path;
kernel/trace/bpf_trace.c +66 -4
108 lines not on this page · raw message
tools/include/uapi/linux/bpf.h +5 -0
@@ -6979,6 +6979,11 @@ struct bpf_link_info {__u32 flags;__u64 missed;__aligned_u64 cookies; +__aligned_u64 inline_addrs; +__aligned_u64 inline_cookies; +__aligned_u64 inline_ids; +__u32 inline_count;/* in/out: kprobe_multi inlined instances count */ +__u32 inline_skipped; } kprobe_multi;struct {__aligned_u64 path;
Signature · 1 line
2.56.1
Jiri Olsa
reviewerRFC PATCH 10/31 bpf: Add fdinfo support for kprobe multi inlined functions
In reply to Jiri OlsaDisplay the number of registered inlined instances in link's fdinfo as kprobe_inline_cnt and the number of skipped ones (the ones that failed to register) as kprobe_inline_skipped.
Add the missed count of the inlined instances kprobes to the missed value in the fdinfo.
--- kernel/trace/bpf_trace.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
kernel/trace/bpf_trace.c +5 -1
@@ -2629,9 +2629,13 @@ static void bpf_kprobe_multi_show_fdinfo(const struct bpf_link *link,seq_printf (seq,"kprobe_cnt:\t%u\n" +"kprobe_inline_cnt:\t%u\n" +"kprobe_inline_skipped:\t%u\n" "missed:\t%lu\n" , kmulti_link->cnt, - kmulti_link->fp.nmissed); + kmulti_link->inlines.cnt, + kmulti_link->inlines.sites_cnt - kmulti_link->inlines.cnt, + kmulti_link->fp.nmissed +inline_sites_missed (&kmulti_link->inlines));seq_printf (seq,"%s\t %s\n" ,"cookie" ,"func" );for (int i =0 ; i < kmulti_link->cnt; i++) {
Signature · 1 line
2.56.1
Jiri Olsa
reviewerRFC PATCH 11/31 bpf: Add btf_for_each_module_inline function
In reply to Jiri OlsaAdd btf_for_each_module_inline() that calls the provided callback for each live module with inline BTF, the split BTF on top of module BTF that describes inline sites in the module.
At the moment we keep just relocated inline BTF data for the module, which is exposed in sysfs. Parse it on first use and keep the parsed inline BTF cached until the module is gone, so there's no cost unless something asks for it.
The module reference is held just for the callback, so the callback needs to get its own reference to keep using the module.
--- include/linux/btf.h | 11 ++++++++ kernel/bpf/btf.c | 64 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+)
include/linux/btf.h +11 -0
@@ -256,6 +256,17 @@ bool btf_type_is_arena_ptr(const struct btf *btf, const struct btf_type *t);s32 btf_find_by_name_kind (const struct btf *btf,const char *name,u8 kind);s32 bpf_find_btf_id (const char *name,u32 kind,struct btf **btf_p);struct btf *btf_get_module_btf (const struct module *module); + +typedef int (*btf_module_inline_fn )(struct module *mod,const struct btf *btf,void *data); + +#if IS_ENABLED (CONFIG_DEBUG_INFO_BTF_MODULES ) &&IS_ENABLED (CONFIG_DEBUG_INFO_BTF_INLINE ) +int btf_for_each_module_inline (btf_module_inline_fn fn,void *data); +#else +static inline int btf_for_each_module_inline (btf_module_inline_fn fn,void *data) +{ +return 0 ; +} +#endif __u32 btf_relocate_id (const struct btf *btf,__u32 id);const struct btf_type *btf_type_skip_modifiers (const struct btf *btf,u32 id,u32 *res_id);
kernel/bpf/btf.c +64 -0
88 lines not on this page · raw message
Signature · 1 line
2.56.1
Jiri Olsa
reviewerRFC PATCH 12/31 bpf: Change get_modules_for_addrs to use struct modules_array
In reply to Jiri OlsaChange get_modules_for_addrs to add modules to struct modules_array object provided by the caller instead of using its own, and return just zero or error. On error all the modules in the array are released.
It will be used to store modules with inlined instances together with modules of the functions attached with fprobe in following change. There's no functional change intended.
--- kernel/trace/bpf_trace.c | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-)
kernel/trace/bpf_trace.c +18 -15
@@ -2872,9 +2872,12 @@ static bool has_module(struct modules_array *arr, struct module *mod)return false; } -static int get_modules_for_addrs (struct module ***mods,unsigned long *addrs,u32 addrs_cnt) +/* +* Add modules for @addrs to @arr. On error all the modules in @arr +* are released. +*/ +static int get_modules_for_addrs (struct modules_array *arr,unsigned long *addrs,u32 addrs_cnt) { -struct modules_array arr = {};u32 i, err =0 ;for (i =0 ; i < addrs_cnt; i++) {
@@ -2884,7 +2887,7 @@ static int get_modules_for_addrs(struct module ***mods, unsigned long *addrs, u3scoped_guard (rcu) { mod =__module_address (addrs[i]);/* Either no module or it's already stored */ -if (!mod ||has_module (&arr, mod)) { +if (!mod ||has_module (arr, mod)) { skip_add = true;break ;/* scoped_guard */ }
@@ -2895,23 +2898,19 @@ static int get_modules_for_addrs(struct module ***mods, unsigned long *addrs, u3continue ;if (err)break ; - err =add_module (&arr, mod); + err =add_module (arr, mod);if (err) {module_put (mod);break ; } } -/* We return either err < 0 in case of error, ... */ if (err) { -kprobe_multi_put_modules (arr.mods, arr.mods_cnt); -kfree (arr.mods); -return err; +kprobe_multi_put_modules (arr->mods, arr->mods_cnt); +kfree (arr->mods); +memset (arr,0 ,sizeof (*arr)); } - -/* or number of modules found if everything is ok. */ - *mods = arr.mods; -return arr.mods_cnt; +return err; }static int addrs_check_error_injection_list (unsigned long *addrs,u32 cnt)
@@ -3188,6 +3187,7 @@ static int inline_sites_register(struct bpf_kprobe_multi_link *link)int bpf_kprobe_multi_link_attach (const union bpf_attr *attr,struct bpf_prog *prog) {struct inline_sites inlines = {}; +struct modules_array mods = {};struct bpf_kprobe_multi_link *link =NULL ;struct bpf_link_primer link_primer;void __user *ucookies;
@@ -3359,10 +3359,11 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr link); } - err =get_modules_for_addrs (&link->mods, addrs, cnt); -if (err <0 ) + err =get_modules_for_addrs (&mods, addrs, cnt); +if (err) goto error_cleanup; - link->mods_cnt = err; + link->mods = mods.mods; + link->mods_cnt = mods.mods_cnt;if (inlines.sites_cnt) { err =inline_sites_register (link);
@@ -3388,6 +3389,8 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prreturn err; error: +kprobe_multi_put_modules (mods.mods, mods.mods_cnt); +kfree (mods.mods);kfree (link);kvfree (addrs);kvfree (cookies);
Signature · 1 line
2.56.1
Jiri Olsa
reviewerRFC PATCH 13/31 tbpf: Add support to attach inlined functions in modules with kprobe multi link
In reply to Jiri OlsaThe BPF_F_KPROBE_MULTI_INLINE flag attaches inlined instances found in vmlinux inline BTF. Add support to attach also inlined instances in kernel modules.
The module inline BTF has LOCSEC for each section with inlined instances, named as inline<section>, with offsets relative to the section start. Sections that are not in module text, like freed init sections, are skipped.
The function is matched by name in module BTF and its base vmlinux BTF, so kernel function inlined in module code is attached as well.
Modules with inlined instances are stored in the link, so they stay loaded as long as the link exists.
The link info inline_ids value of instance in module is the BTF id from the inline BTF of that module.
--- kernel/trace/bpf_trace.c | 70 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 5 deletions(-)
kernel/trace/bpf_trace.c +65 -5
@@ -2873,8 +2873,8 @@ static bool has_module(struct modules_array *arr, struct module *mod) }/* -* Add modules for @addrs to @arr. On error all the modules in @arr -* are released. +* Add modules for @addrs to @arr, which might already have modules +* with inlined instances. On error all the modules in @arr are released. */ static int get_modules_for_addrs (struct modules_array *arr,unsigned long *addrs,u32 addrs_cnt) {
@@ -2959,6 +2959,7 @@ struct inline_lookup {/* Output */ struct inline_sites *inlines; +struct modules_array *mods;u32 addrs_cnt;/* Lookup state */
@@ -3015,6 +3016,58 @@ static int inline_lookup_locsec(struct inline_lookup *lookup, const struct btf *return 0 ; } +#ifdef CONFIG_DEBUG_INFO_BTF_MODULES +static int inline_lookup_module (struct module *mod,const struct btf *btf,void *data) +{ +u32 id, nr_types =btf_nr_types (btf); +struct inline_lookup *lookup = data; +const struct btf_type *t; +u32 cnt = lookup->cnt; +unsigned long base; +const char *name; +int err; + +/* +* Each section with inlined instances has its own LOCSEC named +* as inline<section> with offsets relative to the section start. +*/ +for (id =btf_nr_types (btf_base_btf (btf)); id < nr_types; id++) { + t =btf_type_by_id (btf, id); +if (btf_kind (t) !=BTF_KIND_LOCSEC ) +continue ; + name =btf_name_by_offset (btf, t->name_off); +if (!name || !str_has_prefix (name,"inline" )) +continue ; +/* Skip sections that are not loaded or are freed already (init). */ + base =module_sect_addr (mod, name +strlen ("inline" )); +if (!base || !within_module_mem_type (base, mod,MOD_TEXT )) +continue ; + err =inline_lookup_locsec (lookup, btf, t, base); +if (err) +return err; + } + +if (lookup->cnt == cnt) +return 0 ; + +/* Keep the module we have inlined instances in. */ + err =add_module (lookup->mods, mod); +if (!err) +__module_get (mod); +return err; +} + +static int inline_lookup_modules (struct inline_lookup *lookup) +{ +return btf_for_each_module_inline (inline_lookup_module, lookup); +} +#else +static int inline_lookup_modules (struct inline_lookup *lookup) +{ +return 0 ; +} +#endif +/* * Sort instances by symbol name and address, so instances of each * symbol are next to each other, and get rid of duplicates.
@@ -3051,9 +3104,11 @@ static bool inline_site_traceable(unsigned long addr) }/* -* Find inlined instances for functions in @lookup->syms. Each function -* must have an entry address or at least one inlined instance, functions -* without an entry address are dropped from @lookup->addrs. +* Find inlined instances for functions in @lookup->syms in kernel and +* modules, modules with instances are stored in @lookup->mods with +* reference taken. Each function must have an entry address or at least +* one inlined instance, functions without an entry address are dropped +* from @lookup->addrs. */ static int inline_lookup_symbols (struct inline_lookup *lookup) {
@@ -3075,6 +3130,10 @@ static int inline_lookup_symbols(struct inline_lookup *lookup)if (id <0 )return -EOPNOTSUPP ; + err =inline_lookup_modules (lookup); +if (err) + goto error; + err =inline_lookup_locsec (lookup, btf,btf_type_by_id (btf, id), (unsigned long )_stext);if (err) goto error;
@@ -3298,6 +3357,7 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prif ((flags &BPF_F_KPROBE_MULTI_INLINE ) && (!err || err == -ESRCH )) {struct inline_lookup lookup = { .inlines = &inlines, + .mods = &mods, .syms = us.syms, .syms_cnt = cnt, .addrs = addrs,
Signature · 1 line
2.56.1
Jiri Olsa
reviewerRFC PATCH 14/31 bpf: Add BPF_F_KPROBE_MULTI_INLINE_STRICT flag for kprobe multi link
In reply to Jiri OlsaThe kprobe multi link with BPF_F_KPROBE_MULTI_INLINE flag skips inlined instances that fail to attach, so the link can end up attached just to some of the inlined instances of the function.
Add BPF_F_KPROBE_MULTI_INLINE_STRICT flag for users that need all the inlined instances attached. With the flag the attach fails on the first inlined instance that fails to register and returns its error.
The flag needs to be used together with BPF_F_KPROBE_MULTI_INLINE flag, the attach fails with -EINVAL otherwise.
--- include/uapi/linux/bpf.h | 3 +++ kernel/trace/bpf_trace.c | 17 +++++++++++++---- tools/include/uapi/linux/bpf.h | 3 +++ 3 files changed, 19 insertions(+), 4 deletions(-)
include/uapi/linux/bpf.h +3 -0
@@ -1373,6 +1373,9 @@ enum {/* Attach also to inlined function instances, skip those that fail. */ BPF_F_KPROBE_MULTI_INLINE = (1U <<1 ), + +/* With BPF_F_KPROBE_MULTI_INLINE, fail if inlined instance fails. */ + BPF_F_KPROBE_MULTI_INLINE_STRICT = (1U << 2), };/* link_create.uprobe_multi.flags used in LINK_CREATE command for
kernel/trace/bpf_trace.c +13 -4
@@ -3184,9 +3184,10 @@ static int inline_lookup_symbols(struct inline_lookup *lookup)/* * Register inlined instances of the @link and skip the ones that fail. -* Fail if function has no entry and none of its instances registered. +* Fail if function has no entry and none of its instances registered, +* with @strict fail on the first instance that fails to register. */ -static int inline_sites_register (struct bpf_kprobe_multi_link *link) +static int inline_sites_register (struct bpf_kprobe_multi_link *link,bool strict) {struct inline_sites *inlines = &link->inlines;struct inline_site *site, *first =NULL ;
@@ -3212,6 +3213,8 @@ static int inline_sites_register(struct bpf_kprobe_multi_link *link) err = site->traceable ?register_kprobe (&site->kp) : -EINVAL ;if (err == -ENOMEM ) goto error; +if (err && strict) + goto error;if (err) { first_err = first_err ?: err; }else {
@@ -3255,6 +3258,7 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prvoid __user *uaddrs;u64 *cookies =NULL ;void __user *usyms; +bool strict;int err;/* no support for 32bit archs yet */
@@ -3276,14 +3280,19 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prreturn -EINVAL ; flags = attr->link_create.kprobe_multi.flags; -if (flags & ~(BPF_F_KPROBE_MULTI_RETURN |BPF_F_KPROBE_MULTI_INLINE )) +if (flags & ~(BPF_F_KPROBE_MULTI_RETURN |BPF_F_KPROBE_MULTI_INLINE | +BPF_F_KPROBE_MULTI_INLINE_STRICT ))return -EINVAL ; + strict = flags &BPF_F_KPROBE_MULTI_INLINE_STRICT ; uaddrs =u64_to_user_ptr (attr->link_create.kprobe_multi.addrs); usyms =u64_to_user_ptr (attr->link_create.kprobe_multi.syms);if (!!uaddrs == !!usyms)return -EINVAL ; +if (strict && !(flags &BPF_F_KPROBE_MULTI_INLINE )) +return -EINVAL ; +if (flags &BPF_F_KPROBE_MULTI_INLINE ) {/* * Inlined instances are resolved from function names and
@@ -3426,7 +3435,7 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr link->mods_cnt = mods.mods_cnt;if (inlines.sites_cnt) { - err =inline_sites_register (link); + err =inline_sites_register (link, strict);if (err) goto error_modules; }
tools/include/uapi/linux/bpf.h +3 -0
@@ -1373,6 +1373,9 @@ enum {/* Attach also to inlined function instances, skip those that fail. */ BPF_F_KPROBE_MULTI_INLINE = (1U <<1 ), + +/* With BPF_F_KPROBE_MULTI_INLINE, fail if inlined instance fails. */ + BPF_F_KPROBE_MULTI_INLINE_STRICT = (1U << 2), };/* link_create.uprobe_multi.flags used in LINK_CREATE command for
Signature · 1 line
2.56.1
Jiri Olsa
reviewerRFC PATCH 15/31 bpf: Add get_func_arg[_cnt] support for kprobe multi functions
In reply to Jiri OlsaAdding support for bpf_get_func_arg and bpf_get_func_arg_cnt helpers in kprobe multi programs.
The number of arguments is read from function's BTF during the attach and the argument value is read from ftrace_regs. The BTF lookup is done only for programs that call the helpers.
The kprobe multi link does not need kernel BTF so far and that stays the same for programs that do not call the helpers. For programs that do, the number of arguments is zero for functions without BTF, the attach does not fail.
The arguments are not available in return probe and for inlined instances, the support for those is added in following change.
--- include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 10 ++ kernel/bpf/verifier.c | 3 + kernel/trace/bpf_trace.c | 179 +++++++++++++++++++++++++++++++-- tools/include/uapi/linux/bpf.h | 10 ++ 5 files changed, 192 insertions(+), 11 deletions(-)
include/linux/bpf.h +1 -0
@@ -2073,6 +2073,7 @@ struct bpf_prog { enforce_expected_attach_type:1, /* Enforce expected_attach_type checking at attach time */ call_get_stack:1, /* Do we call bpf_get_stack() or bpf_get_stackid() */ call_get_func_ip:1, /* Do we call get_func_ip() */ + call_get_func_arg:1, /* Do we call get_func_arg[_cnt]() */ call_session_cookie:1, /* Do we call bpf_session_cookie() */ tstamp_type_access:1, /* Accessed __sk_buff->tstamp_type */ sleepable:1; /* BPF program is sleepable */
include/uapi/linux/bpf.h +10 -0
@@ -5680,9 +5680,14 @@ union bpf_attr {* Get **n**-th argument register (zero based) of the traced function (for tracing programs) * returned in **value**. * +* For kprobe multi programs get **n**-th argument (zero based) of +* the traced function. +* * Return * 0 on success. * **-EINVAL** if n >= argument register count of traced function. +* **-EOPNOTSUPP** for kprobe multi programs executed in return +* probe or for inlined instances. * * long bpf_get_func_ret(void *ctx, u64 *value) * Description
@@ -5698,8 +5703,13 @@ union bpf_attr {* Get number of registers of the traced function (for tracing programs) where * function arguments are stored in these registers. * +* For kprobe multi programs get number of arguments of the traced +* function. +* * Return * The number of argument registers of the traced function. +* 0 for kprobe multi programs attached with +* **BPF_F_KPROBE_MULTI_RETURN** flag or for inlined instances. * * int bpf_get_retval(void) * Description
kernel/bpf/verifier.c +3 -0
@@ -13479,6 +13479,9 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn env->prog->call_get_func_ip = true; } +if (func_id == BPF_FUNC_get_func_arg || func_id == BPF_FUNC_get_func_arg_cnt) + env->prog->call_get_func_arg = true; +/* a call that only a speculative state reaches is dead code */ if (func_id == BPF_FUNC_override_return && !env->cur_state->speculative) env->prog->kprobe_override =1 ;
kernel/trace/bpf_trace.c +168 -11
293 lines not on this page · raw message
tools/include/uapi/linux/bpf.h +10 -0
@@ -5680,9 +5680,14 @@ union bpf_attr {* Get **n**-th argument register (zero based) of the traced function (for tracing programs) * returned in **value**. * +* For kprobe multi programs get **n**-th argument (zero based) of +* the traced function. +* * Return * 0 on success. * **-EINVAL** if n >= argument register count of traced function. +* **-EOPNOTSUPP** for kprobe multi programs executed in return +* probe or for inlined instances. * * long bpf_get_func_ret(void *ctx, u64 *value) * Description
@@ -5698,8 +5703,13 @@ union bpf_attr {* Get number of registers of the traced function (for tracing programs) where * function arguments are stored in these registers. * +* For kprobe multi programs get number of arguments of the traced +* function. +* * Return * The number of argument registers of the traced function. +* 0 for kprobe multi programs attached with +* **BPF_F_KPROBE_MULTI_RETURN** flag or for inlined instances. * * int bpf_get_retval(void) * Description
Signature · 1 line
2.56.1
Jiri Olsa
reviewerRFC PATCH 16/31 bpf: Add get_func_arg[_cnt] support for kprobe multi inlined functions
In reply to Jiri OlsaAdding bpf_get_func_arg and bpf_get_func_arg_cnt helpers support for inlined instances attached with BPF_F_KPROBE_MULTI_INLINE flag.
The program attached to inlined instance gets pt_regs of the probed instruction, where the arguments are not in the argument registers. Their locations are described in inline BTF, so store the location prototype of each instance and use it in the helpers.
The register value is read with regs_get_dwarf_register function, so the support is at the moment limited to x86_64.
The bpf_get_func_arg helper fails with:
-EINVAL for argument index out of the number of arguments -ENOENT for argument without location (likely optimized out) -EFAULT if the argument can't be read from its location
-EOPNOTSUPP for unsupported location
--- include/uapi/linux/bpf.h | 13 ++-- kernel/trace/bpf_trace.c | 132 ++++++++++++++++++++++++++++++--- tools/include/uapi/linux/bpf.h | 13 ++-- 3 files changed, 137 insertions(+), 21 deletions(-)
include/uapi/linux/bpf.h +8 -5
@@ -5681,13 +5681,16 @@ union bpf_attr {* returned in **value**. * * For kprobe multi programs get **n**-th argument (zero based) of -* the traced function. +* the traced function or of its inlined instance. * * Return * 0 on success. * **-EINVAL** if n >= argument register count of traced function. -* **-EOPNOTSUPP** for kprobe multi programs executed in return -* probe or for inlined instances. +* **-ENOENT** if the argument of inlined instance has no location. +* **-EFAULT** if the argument of inlined instance can't be read. +* **-EOPNOTSUPP** if the location of inlined instance argument is +* not supported or for kprobe multi programs executed in return +* probe. * * long bpf_get_func_ret(void *ctx, u64 *value) * Description
@@ -5704,12 +5707,12 @@ union bpf_attr {* function arguments are stored in these registers. * * For kprobe multi programs get number of arguments of the traced -* function. +* function or of its inlined instance. * * Return * The number of argument registers of the traced function. * 0 for kprobe multi programs attached with -* **BPF_F_KPROBE_MULTI_RETURN** flag or for inlined instances. +* **BPF_F_KPROBE_MULTI_RETURN** flag. * * int bpf_get_retval(void) * Description
kernel/trace/bpf_trace.c +121 -11
199 lines not on this page · raw message
tools/include/uapi/linux/bpf.h +8 -5
@@ -5681,13 +5681,16 @@ union bpf_attr {* returned in **value**. * * For kprobe multi programs get **n**-th argument (zero based) of -* the traced function. +* the traced function or of its inlined instance. * * Return * 0 on success. * **-EINVAL** if n >= argument register count of traced function. -* **-EOPNOTSUPP** for kprobe multi programs executed in return -* probe or for inlined instances. +* **-ENOENT** if the argument of inlined instance has no location. +* **-EFAULT** if the argument of inlined instance can't be read. +* **-EOPNOTSUPP** if the location of inlined instance argument is +* not supported or for kprobe multi programs executed in return +* probe. * * long bpf_get_func_ret(void *ctx, u64 *value) * Description
@@ -5704,12 +5707,12 @@ union bpf_attr {* function arguments are stored in these registers. * * For kprobe multi programs get number of arguments of the traced -* function. +* function or of its inlined instance. * * Return * The number of argument registers of the traced function. * 0 for kprobe multi programs attached with -* **BPF_F_KPROBE_MULTI_RETURN** flag or for inlined instances. +* **BPF_F_KPROBE_MULTI_RETURN** flag. * * int bpf_get_retval(void) * Description
Signature · 1 line
2.56.1
Jiri Olsa
reviewerRFC PATCH 17/31 bpf: Add log messages for kprobe multi inline attach
In reply to Jiri OlsaThe attach of kprobe multi link with BPF_F_KPROBE_MULTI_INLINE flag fails for several reasons with the same error.
Pass the link_create log to bpf_kprobe_multi_link_attach and use it to explain the failure, including the function and the inlined instance that can't be attached.
--- include/linux/trace_events.h | 7 +++- kernel/bpf/syscall.c | 2 +- kernel/trace/bpf_trace.c | 78 ++++++++++++++++++++++++++++-------- 3 files changed, 68 insertions(+), 19 deletions(-)
include/linux/trace_events.h +5 -2
@@ -15,6 +15,7 @@ struct array_buffer;struct tracer ;struct dentry ;struct bpf_prog ; +struct bpf_verifier_log ;union bpf_attr ;struct bpf_verifier_log ;
@@ -795,7 +796,8 @@ int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id, u32 *fd_type, const char **buf, u64 *probe_offset, u64 *probe_addr, unsigned long *missed); -int bpf_kprobe_multi_link_attach (const union bpf_attr *attr,struct bpf_prog *prog); +int bpf_kprobe_multi_link_attach (const union bpf_attr *attr,struct bpf_prog *prog, +struct bpf_verifier_log *log);int bpf_uprobe_multi_link_attach (const union bpf_attr *attr,struct bpf_prog *prog);int bpf_tracing_multi_attach (struct bpf_prog *prog,const union bpf_attr *attr,struct bpf_verifier_log *log);
@@ -847,7 +849,8 @@ static inline int bpf_get_perf_event_info(const struct perf_event *event,return -EOPNOTSUPP ; }static inline int -bpf_kprobe_multi_link_attach (const union bpf_attr *attr,struct bpf_prog *prog) +bpf_kprobe_multi_link_attach (const union bpf_attr *attr,struct bpf_prog *prog, +struct bpf_verifier_log *log) {return -EOPNOTSUPP ; }
kernel/bpf/syscall.c +1 -1
@@ -6017,7 +6017,7 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr, struct bpf_common_a ret =bpf_perf_link_attach (attr, prog); else if (attr->link_create.attach_type == BPF_TRACE_KPROBE_MULTI || attr->link_create.attach_type == BPF_TRACE_KPROBE_SESSION) - ret =bpf_kprobe_multi_link_attach (attr, prog); + ret =bpf_kprobe_multi_link_attach (attr, prog, log); else if (attr->link_create.attach_type == BPF_TRACE_UPROBE_MULTI || attr->link_create.attach_type == BPF_TRACE_UPROBE_SESSION) ret =bpf_uprobe_multi_link_attach (attr, prog);
kernel/trace/bpf_trace.c +62 -16
206 lines not on this page · raw message
Signature · 1 line
2.56.1
Jiri Olsa
reviewerRFC PATCH 18/31 bpf: Add inlined functions for kprobe multi inline tests
In reply to Jiri OlsaAdd always inlined functions and their callers for kprobe multi inline tests, the callers are triggered through the tracing test_run.
The bpf_kprobe_multi_inline_test4 caller is marked with NOKPROBE_SYMBOL, so inlined instances in it can't be attached.
--- net/bpf/test_run.c | 83 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 82 insertions(+), 1 deletion(-)
net/bpf/test_run.c +82 -1
@@ -6,6 +6,7 @@#include <linux/btf_ids.h> #include <linux/slab.h> #include <linux/init.h> +#include <linux/kprobes.h> #include <linux/vmalloc.h> #include <linux/etherdevice.h> #include <linux/filter.h>
@@ -558,6 +559,81 @@ noinline int bpf_fentry_test10(const void *a)return (long )a; } +/* +* Inlined functions for kprobe multi inline tests, the outer function +* starts with the inner one, so their inlined instances in +* bpf_kprobe_multi_inline_test1 share the address. +*/ +static __always_inline int bpf_kprobe_multi_inline_inner(int a) +{ +/* +* Keep an instruction in the inline site, there's no argument +* setup, so it's the first instruction of the outer function +* inline site as well. +*/ + asmvolatile ("nop" ); +return a +1 ; +} + +static __always_inline int bpf_kprobe_multi_inline_outer(int a) +{ +return bpf_kprobe_multi_inline_inner (a) +1 ; +} + +noinline int bpf_kprobe_multi_inline_test1(int a) +{ +return bpf_kprobe_multi_inline_outer (a); +} + +noinline int bpf_kprobe_multi_inline_test2(int a) +{ +return bpf_kprobe_multi_inline_inner (a); +} + +/* +* Inlined functions with instances that can't be attached, because +* kprobes are not allowed in bpf_kprobe_multi_inline_test4: +* +* bpf_kprobe_multi_inline_partial - one instance can be attached +* (test3) and one can't (test4) +* bpf_kprobe_multi_inline_nokprobe - no instance can be attached +*/ +static __always_inline int bpf_kprobe_multi_inline_partial(int a) +{ + asmvolatile ("nop" ); +return a +1 ; +} + +static __always_inline int bpf_kprobe_multi_inline_nokprobe(int a) +{ + asmvolatile ("nop" ); +return a +2 ; +} + +noinline int bpf_kprobe_multi_inline_test3(int a) +{ +return bpf_kprobe_multi_inline_partial (a); +} + +noinline int bpf_kprobe_multi_inline_test4(int a) +{ +return bpf_kprobe_multi_inline_partial (a) + +bpf_kprobe_multi_inline_nokprobe (a); +} +NOKPROBE_SYMBOL (bpf_kprobe_multi_inline_test4); + +/* Inlined function with arguments for kprobe multi inline args test. */ +static __always_inline long bpf_kprobe_multi_inline_args (int a,long b,void *c) +{ + asmvolatile ("nop" ); +return a + b + (long )c; +} + +noinline long bpf_kprobe_multi_inline_test5 (int a,long b,void *c) +{ +return bpf_kprobe_multi_inline_args (a, b, c); +} + noinline void bpf_fentry_test_sinfo(struct skb_shared_info *sinfo) { }
@@ -710,7 +786,12 @@ int bpf_prog_test_run_tracing(struct bpf_prog *prog,bpf_fentry_test7 ((struct bpf_fentry_test_t *)0 ) !=0 ||bpf_fentry_test8 (&arg) !=0 ||bpf_fentry_test9 (&retval) !=0 || - bpf_fentry_test10((void *)0) != 0) +bpf_fentry_test10 ((void *)0 ) !=0 || +bpf_kprobe_multi_inline_test1 (1 ) !=3 || +bpf_kprobe_multi_inline_test2 (1 ) !=2 || +bpf_kprobe_multi_inline_test3 (1 ) !=2 || +bpf_kprobe_multi_inline_test4 (1 ) !=5 || + bpf_kprobe_multi_inline_test5(1, 2, (void *)3) != 6) goto out;break ;case BPF_MODIFY_RETURN :
Signature · 1 line
2.56.1
Jiri Olsa
reviewerRFC PATCH 19/31 libbpf: Add inline options to bpf_kprobe_multi_opts
In reply to Jiri OlsaAdd inlined and inlined_strict options to struct bpf_kprobe_multi_opts to attach the link with BPF_F_KPROBE_MULTI_INLINE[_STRICT] flags.
The inlined instances are resolved by kernel from function names, so the options work only with syms array or with pattern without wildcards.
--- tools/lib/bpf/libbpf.c | 18 +++++++++++++++++- tools/lib/bpf/libbpf.h | 6 +++++- 2 files changed, 22 insertions(+), 2 deletions(-)
tools/lib/bpf/libbpf.c +17 -1
@@ -13159,7 +13159,7 @@ bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,struct bpf_link *link =NULL ;const unsigned long *addrs;int err, link_fd, prog_fd; -bool retprobe, session, unique_match; +bool retprobe, session, unique_match, inlined, inlined_strict;const __u64 *cookies;const char **syms;size_t cnt;
@@ -13179,6 +13179,8 @@ bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog, cnt =OPTS_GET (opts, cnt, false); cookies =OPTS_GET (opts, cookies, false); unique_match =OPTS_GET (opts, unique_match, false); + inlined =OPTS_GET (opts, inlined, false); + inlined_strict =OPTS_GET (opts, inlined_strict, false);if (!pattern && !addrs && !syms)return libbpf_err_ptr (-EINVAL );
@@ -13190,6 +13192,14 @@ bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,return libbpf_err_ptr (-EINVAL );if (addrs && syms)return libbpf_err_ptr (-EINVAL ); +/* +* Inlined instances are resolved by kernel from function names, +* so we can't use addresses or pattern resolved to addresses. +*/ +if (inlined && (addrs || unique_match || (pattern &&strpbrk (pattern,"*?" )))) +return libbpf_err_ptr (-EINVAL ); +if (inlined_strict && !inlined) +return libbpf_err_ptr (-EINVAL );/* * Exact function name (no wildcards) without unique_match:
@@ -13224,6 +13234,8 @@ bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,if (retprobe && session)return libbpf_err_ptr (-EINVAL ); +if (inlined && (retprobe || session)) +return libbpf_err_ptr (-EINVAL ); attach_type = session ?BPF_TRACE_KPROBE_SESSION :BPF_TRACE_KPROBE_MULTI ;
@@ -13232,6 +13244,10 @@ bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog, lopts.kprobe_multi.cookies = cookies; lopts.kprobe_multi.cnt = cnt; lopts.kprobe_multi.flags = retprobe ?BPF_F_KPROBE_MULTI_RETURN :0 ; +if (inlined) + lopts.kprobe_multi.flags |=BPF_F_KPROBE_MULTI_INLINE ; +if (inlined_strict) + lopts.kprobe_multi.flags |=BPF_F_KPROBE_MULTI_INLINE_STRICT ; link =calloc (1 ,sizeof (*link));if (!link) {
tools/lib/bpf/libbpf.h +5 -1
@@ -648,10 +648,14 @@ struct bpf_kprobe_multi_opts {bool session;/* enforce unique match */ bool unique_match; +/* attach also to inlined instances of functions */ +bool inlined; +/* fail if any inlined instance can't be attached, needs inlined */ +bool inlined_strict; size_t :0; }; -#define bpf_kprobe_multi_opts__last_field unique_match +#define bpf_kprobe_multi_opts__last_field inlined_strict LIBBPF_API struct bpf_link * bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,
Signature · 1 line
2.56.1
Jiri Olsa
reviewerRFC PATCH 20/31 libbpf: Print kernel log for failed kprobe multi attach
In reply to Jiri OlsaThe kernel explains the failure of kprobe multi link attach in the link_create log, but libbpf does not ask for it, so the user gets just the error code.
Pass the log buffer to bpf_link_create in bpf_program__attach_kprobe_multi_opts and print the kernel message when the attach fails, like:
libbpf: prog 'test': failed to attach: -ENOENT
libbpf: prog 'test': Function foo has no entry and no inlined instance.
The log is passed only if kernel supports the syscall common attributes, so there's no change for older kernels.
--- tools/lib/bpf/libbpf.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
tools/lib/bpf/libbpf.c +11 -0
@@ -13160,8 +13160,10 @@ bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,const unsigned long *addrs;int err, link_fd, prog_fd;bool retprobe, session, unique_match, inlined, inlined_strict; +LIBBPF_OPTS (bpf_log_opts, log_opts);const __u64 *cookies;const char **syms; +char log_buf[4096 ];size_t cnt;if (!OPTS_VALID (opts, bpf_kprobe_multi_opts))
@@ -13249,6 +13251,13 @@ bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,if (inlined_strict) lopts.kprobe_multi.flags |=BPF_F_KPROBE_MULTI_INLINE_STRICT ; +/* Get the reason of the attach failure from kernel if it's supported. */ + log_buf[0 ] ='\0' ; + log_opts.buf = log_buf; + log_opts.size =sizeof (log_buf); + log_opts.level =1 ; + lopts.log_opts = &log_opts; + link =calloc (1 ,sizeof (*link));if (!link) { err = -ENOMEM ;
@@ -13269,6 +13278,8 @@ bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog, err = -ENOENT ;pr_warn ("prog '%s': failed to attach: %s\n" , prog->name,errstr (err)); +if (log_buf[0 ]) +pr_warn ("prog '%s': %s\n" , prog->name, log_buf); goto error; } link->fd = link_fd;
Signature · 1 line
2.56.1
Jiri Olsa
reviewerRFC PATCH 21/31 bpftool: Display inlined functions for kprobe multi link
In reply to Jiri OlsaDisplay the inline flags and the attached inlined instances of the kprobe multi link.
For each inlined instance display its address, cookie, name of the inlined function and the function it is inlined in:
# bpftool link
3: kprobe_multi prog 10
kprobe.multi inline func_cnt 1 inline_cnt 2
addr cookie func [module]
ffffffff825e9110 5 bpf_fentry_test1
addr cookie func, inlined in [module]
ffffffff825eb4b9 6 bpf_kprobe_multi_inline_inner, bpf_kprobe_multi_inline_test1+0x9
ffffffff825eb4c9 6 bpf_kprobe_multi_inline_inner, bpf_kprobe_multi_inline_test2+0x9The function the instance is inlined in is the kallsyms symbol covering the instance address.
The name of the inlined function is resolved from the BTF type id returned in inline_ids, because several inlined functions can have their instance on the same address.
In json output the instances are displayed in "inlines" array, together with "inline", "inline_strict", "inline_cnt" and "inline_skipped" fields.
--- tools/bpf/bpftool/link.c | 348 ++++++++++++++++++++++++++++++++++----- 1 file changed, 303 insertions(+), 45 deletions(-)
tools/bpf/bpftool/link.c +303 -45
@@ -11,6 +11,7 @@#include <unistd.h> #include <bpf/bpf.h> +#include <bpf/btf.h> #include <bpf/hashmap.h> #include "json_writer.h"
@@ -254,6 +255,12 @@ struct addr_cookie {__u64 cookie; }; +struct addr_cookie_id { +__u64 addr; +__u64 cookie; +__u32 id; +}; +static int cmp_addr_cookie (const void *A ,const void *B ) {const struct addr_cookie *a =A , *b =B ;
@@ -263,6 +270,15 @@ static int cmp_addr_cookie(const void *A, const void *B)return a->addr < b->addr ?-1 :1 ; } +static int cmp_addr_cookie_id (const void *A ,const void *B ) +{ +const struct addr_cookie_id *a =A , *b =B ; + +if (a->addr == b->addr) +return 0 ; +return a->addr < b->addr ?-1 :1 ; +} +static struct addr_cookie *get_addr_cookie_array (__u64 *addrs,__u64 *cookies,__u32 count) {
@@ -282,6 +298,29 @@ get_addr_cookie_array(__u64 *addrs, __u64 *cookies, __u32 count)return data; } +static struct addr_cookie_id *get_addr_cookie_id_array (struct bpf_link_info *info) +{ +__u64 *cookies =u64_to_ptr (info->kprobe_multi.inline_cookies); +__u64 *addrs =u64_to_ptr (info->kprobe_multi.inline_addrs); +__u32 *ids =u64_to_ptr (info->kprobe_multi.inline_ids); +__u32 count = info->kprobe_multi.inline_count; +struct addr_cookie_id *data; +__u32 i; + + data =calloc (count,sizeof (data[0 ])); +if (!data) { +p_err ("mem alloc failed" ); +return NULL ; + } +for (i =0 ; i < count; i++) { + data[i].addr = addrs[i]; + data[i].cookie = cookies[i]; + data[i].id = ids[i]; + } +qsort (data, count,sizeof (data[0 ]), cmp_addr_cookie_id); +return data; +} +static bool is_x86_ibt_enabled (void ) {#if defined(__x86_64__)
@@ -322,33 +361,156 @@ symbol_matches_target(__u64 sym_addr, __u64 target_addr, bool is_ibt_enabled)return false; } +/* Inlined instances are placed inside functions, find the one covering @addr. */ +static struct kernel_sym *find_kernel_sym_by_range (__u64 addr) +{ +__u32 lo =0 , hi = dd.sym_count, mid; + +if (!addr) +return NULL ; + +while (lo < hi) { + mid = lo + (hi - lo) /2 ; +if (dd.sym_mapping[mid].address <= addr) + lo = mid +1 ; +else + hi = mid; + } +return lo ? &dd.sym_mapping[lo -1 ] :NULL ; +} + +/* +* Inline BTF used to get names of inlined functions. The vmlinux one is +* loaded once on first use, for modules we keep the last used one. +*/ +static struct { +struct btf *vmlinux; +struct btf *vmlinux_inline; +struct btf *mod; +struct btf *mod_inline; +char mod_name[MODULE_MAX_NAME ]; +bool vmlinux_loaded; +} inline_btf; + +static struct btf *parse_sysfs_btf (const char *name,const char *suffix,struct btf *base) +{ +char path[PATH_MAX ]; +struct btf *btf; + +snprintf (path,sizeof (path),"/sys/kernel/btf/%s%s" , name, suffix); + btf =btf__parse_split (path, base); +return IS_ERR_OR_NULL (btf) ?NULL : btf; +} + +/* Get inline BTF for vmlinux or for @module, NULL if it's not available. */ +static struct btf *get_inline_btf (const char *module) +{ +if (!inline_btf.vmlinux_loaded) { + inline_btf.vmlinux_loaded = true; + inline_btf.vmlinux =libbpf_find_kernel_btf (); +if (IS_ERR_OR_NULL (inline_btf.vmlinux)) + inline_btf.vmlinux =NULL ; +else + inline_btf.vmlinux_inline =parse_sysfs_btf ("vmlinux" ,".inline" , + inline_btf.vmlinux); + } +if (!inline_btf.vmlinux) +return NULL ; +if (module[0 ] =='\0' ) +return inline_btf.vmlinux_inline; + +if (strcmp (inline_btf.mod_name, module)) { +btf__free (inline_btf.mod_inline); +btf__free (inline_btf.mod); + inline_btf.mod_inline =NULL ; + +/* Module inline BTF is split BTF on top of the module BTF. */ +snprintf (inline_btf.mod_name,sizeof (inline_btf.mod_name),"%s" , module); + inline_btf.mod =parse_sysfs_btf (module,"" , inline_btf.vmlinux); +if (inline_btf.mod) + inline_btf.mod_inline =parse_sysfs_btf (module,".inline" , + inline_btf.mod); + } +return inline_btf.mod_inline; +} + +static void free_inline_btf (void ) +{ +/* The caller checks errno of the last link lookup, keep it. */ +int saved_errno = errno; + +btf__free (inline_btf.mod_inline); +btf__free (inline_btf.mod); +btf__free (inline_btf.vmlinux_inline); +btf__free (inline_btf.vmlinux); +memset (&inline_btf,0 ,sizeof (inline_btf)); + errno = saved_errno; +} + +/* +* Get name of the inlined function with BTF type @id, the id is from +* inline BTF of the object the inlined instance is located in, which +* we get from @sym. Returns NULL if the name is not available. +*/ +static const char *inline_func_name (const struct kernel_sym *sym,__u32 id) +{ +const struct btf_type *t; +const char *name; +struct btf *btf; + +if (!sym || !id) +return NULL ; + btf =get_inline_btf (sym->module); +if (!btf) +return NULL ; + t =btf__type_by_id (btf, id); +if (!t) +return NULL ; + name =btf__name_by_offset (btf, t->name_off); +return name && name[0 ] ? name :NULL ; +} +static void show_kprobe_multi_json (struct bpf_link_info *info,json_writer_t *wtr) { -struct addr_cookie *data; +__u32 inline_count = info->kprobe_multi.inline_count; +__u32 count = info->kprobe_multi.count; +struct addr_cookie_id *inlines =NULL ; +struct addr_cookie *data =NULL ; +struct kernel_sym *sym; +const char *name;__u32 i, j =0 ;bool is_ibt_enabled;jsonw_bool_field (json_wtr,"retprobe" , info->kprobe_multi.flags &BPF_F_KPROBE_MULTI_RETURN ); -jsonw_uint_field (json_wtr,"func_cnt" , info->kprobe_multi.count); +jsonw_bool_field (json_wtr,"inline" , + info->kprobe_multi.flags &BPF_F_KPROBE_MULTI_INLINE ); +jsonw_bool_field (json_wtr,"inline_strict" , + info->kprobe_multi.flags &BPF_F_KPROBE_MULTI_INLINE_STRICT ); +jsonw_uint_field (json_wtr,"func_cnt" , count);jsonw_uint_field (json_wtr,"missed" , info->kprobe_multi.missed); -jsonw_name (json_wtr,"funcs" ); -jsonw_start_array (json_wtr); - data =get_addr_cookie_array (u64_to_ptr (info->kprobe_multi.addrs), -u64_to_ptr (info->kprobe_multi.cookies), - info->kprobe_multi.count); -if (!data) -return ; + +if (count) + data =get_addr_cookie_array (u64_to_ptr (info->kprobe_multi.addrs), +u64_to_ptr (info->kprobe_multi.cookies), + count); +if (inline_count) + inlines =get_addr_cookie_id_array (info);/* Load it once for all. */ -if (!dd.sym_count) +if ((data || inlines) && !dd.sym_count)kernel_syms_load (&dd); -if (!dd.sym_count) - goto error; +/* The array is empty if we don't have its data or symbols. */ +if (!data || !dd.sym_count) + count =0 ; +if (!inlines || !dd.sym_count) + inline_count =0 ; is_ibt_enabled =is_x86_ibt_enabled (); -for (i =0 ; i < dd.sym_count; i++) { +jsonw_name (json_wtr,"funcs" ); +jsonw_start_array (json_wtr); +for (i =0 ; count && i < dd.sym_count; i++) {if (!symbol_matches_target (dd.sym_mapping[i].address, data[j].addr, is_ibt_enabled))continue ;
@@ -364,12 +526,50 @@ show_kprobe_multi_json(struct bpf_link_info *info, json_writer_t *wtr) }jsonw_uint_field (json_wtr,"cookie" , data[j].cookie);jsonw_end_object (json_wtr); -if (++j == info->kprobe_multi.count) +if (++j == count)break ; }jsonw_end_array (json_wtr); -error: + +jsonw_uint_field (json_wtr,"inline_cnt" , info->kprobe_multi.inline_count); +jsonw_uint_field (json_wtr,"inline_skipped" , info->kprobe_multi.inline_skipped); +jsonw_name (json_wtr,"inlines" ); +jsonw_start_array (json_wtr); +for (i =0 ; i < inline_count; i++) { + sym =find_kernel_sym_by_range (inlines[i].addr); + name =inline_func_name (sym, inlines[i].id); + +jsonw_start_object (json_wtr); +jsonw_uint_field (json_wtr,"addr" , (unsigned long )inlines[i].addr); +/* Name of the inlined function. */ +if (name) { +jsonw_string_field (json_wtr,"func" , name); + }else { +jsonw_name (json_wtr,"func" ); +jsonw_null (json_wtr); + } +/* The function it is inlined in. */ +if (sym) { +jsonw_string_field (json_wtr,"caller" , sym->name); +jsonw_uint_field (json_wtr,"offset" , inlines[i].addr - sym->address); + }else { +jsonw_name (json_wtr,"caller" ); +jsonw_null (json_wtr); +jsonw_uint_field (json_wtr,"offset" ,0 ); + } +/* Print null if it is vmlinux */ +if (!sym || sym->module[0 ] =='\0' ) { +jsonw_name (json_wtr,"module" ); +jsonw_null (json_wtr); + }else { +jsonw_string_field (json_wtr,"module" , sym->module); + } +jsonw_uint_field (json_wtr,"cookie" , inlines[i].cookie); +jsonw_end_object (json_wtr); + } +jsonw_end_array (json_wtr);free (data); +free (inlines); }static __u64 *u64_to_arr (__u64 val)
@@ -828,35 +1028,57 @@ void netfilter_dump_plain(const struct bpf_link_info *info)static void show_kprobe_multi_plain (struct bpf_link_info *info) { -struct addr_cookie *data; +__u32 inline_count = info->kprobe_multi.inline_count; +bool is_ibt_enabled, is_inline, is_inline_strict; +__u32 count = info->kprobe_multi.count; +struct addr_cookie_id *inlines =NULL ; +struct addr_cookie *data =NULL ; +struct kernel_sym *sym; +const char *name;__u32 i, j =0 ; -bool is_ibt_enabled; -if (!info->kprobe_multi.count) + is_inline_strict = info->kprobe_multi.flags &BPF_F_KPROBE_MULTI_INLINE_STRICT ; + is_inline = info->kprobe_multi.flags &BPF_F_KPROBE_MULTI_INLINE ; + +/* Link with inline flag can have only inlined instances attached. */ +if (!count && !is_inline)return ;if (info->kprobe_multi.flags &BPF_F_KPROBE_MULTI_RETURN )printf ("\n\tkretprobe.multi " );else printf ("\n\tkprobe.multi " ); -printf ("func_cnt %u " , info->kprobe_multi.count); +if (is_inline_strict) +printf ("inline_strict " ); +else if (is_inline) +printf ("inline " ); +printf ("func_cnt %u " , count); +if (is_inline) +printf ("inline_cnt %u " , inline_count); +if (info->kprobe_multi.inline_skipped) +printf ("inline_skipped %u " , info->kprobe_multi.inline_skipped);if (info->kprobe_multi.missed)printf ("missed %llu " , info->kprobe_multi.missed); - data =get_addr_cookie_array (u64_to_ptr (info->kprobe_multi.addrs), -u64_to_ptr (info->kprobe_multi.cookies), - info->kprobe_multi.count); -if (!data) -return ; +if (count) + data =get_addr_cookie_array (u64_to_ptr (info->kprobe_multi.addrs), +u64_to_ptr (info->kprobe_multi.cookies), + count); +if (inline_count) + inlines =get_addr_cookie_id_array (info);/* Load it once for all. */ -if (!dd.sym_count) +if ((data || inlines) && !dd.sym_count)kernel_syms_load (&dd); -if (!dd.sym_count) - goto error; +/* The array is empty if we don't have its data or symbols. */ +if (!data || !dd.sym_count) + count =0 ; +if (!inlines || !dd.sym_count) + inline_count =0 ; is_ibt_enabled =is_x86_ibt_enabled (); -printf ("\n\t%-16s %-16s %s" ,"addr" ,"cookie" ,"func [module]" ); -for (i =0 ; i < dd.sym_count; i++) { +if (count) +printf ("\n\t%-16s %-16s %s" ,"addr" ,"cookie" ,"func [module]" ); +for (i =0 ; count && i < dd.sym_count; i++) {if (!symbol_matches_target (dd.sym_mapping[i].address, data[j].addr, is_ibt_enabled))continue ;
@@ -867,11 +1089,30 @@ static void show_kprobe_multi_plain(struct bpf_link_info *info)else printf (" " ); -if (++j == info->kprobe_multi.count) +if (++j == count)break ; } -error: + +if (inline_count) +printf ("\n\t%-16s %-16s %s" ,"addr" ,"cookie" ,"func, inlined in [module]" ); +for (i =0 ; i < inline_count; i++) { + sym =find_kernel_sym_by_range (inlines[i].addr); + +printf ("\n\t%016lx %-16llx " , + (unsigned long )inlines[i].addr, inlines[i].cookie); +if (!sym) +continue ; + name =inline_func_name (sym, inlines[i].id); +if (name) +printf ("%s, " , name); +printf ("%s+0x%lx" , sym->name, + (unsigned long )inlines[i].addr - sym->address); +if (sym->module[0 ] !='\0' ) +printf (" [%s]" , sym->module); +printf (" " ); + }free (data); +free (inlines); }static void show_uprobe_multi_plain (struct bpf_link_info *info)
@@ -1140,7 +1381,8 @@ static int show_link_close_plain(int fd, struct bpf_link_info *info)static int do_show_link (int fd) {__u64 *ref_ctr_offsets =NULL , *offsets =NULL , *cookies =NULL ; -__u32 *ids =NULL ; +__u64 *inline_addrs =NULL , *inline_cookies =NULL ; +__u32 *ids =NULL , *inline_ids =NULL ;struct bpf_link_info info;__u32 len =sizeof (info);char path_buf[PATH_MAX ];
@@ -1172,24 +1414,36 @@ static int do_show_link(int fd) goto again; }if (info.type ==BPF_LINK_TYPE_KPROBE_MULTI && - !info.kprobe_multi.addrs) { + !info.kprobe_multi.addrs && !info.kprobe_multi.inline_addrs) { +__u32 inline_count = info.kprobe_multi.inline_count; + count = info.kprobe_multi.count;if (count) { addrs =calloc (count,sizeof (__u64)); -if (!addrs) { -p_err ("mem alloc failed" ); -close (fd); -return -ENOMEM ; - } - info.kprobe_multi.addrs =ptr_to_u64 (addrs); cookies =calloc (count,sizeof (__u64)); -if (!cookies) { -p_err ("mem alloc failed" ); -free (addrs); -close (fd); -return -ENOMEM ; - } + } +if (inline_count) { + inline_addrs =calloc (inline_count,sizeof (__u64)); + inline_cookies =calloc (inline_count,sizeof (__u64)); + inline_ids =calloc (inline_count,sizeof (__u32)); + } +if ((count && (!addrs || !cookies)) || + (inline_count && (!inline_addrs || !inline_cookies || !inline_ids))) { +p_err ("mem alloc failed" ); +free (inline_ids); +free (inline_cookies); +free (inline_addrs); +free (cookies); +free (addrs); +close (fd); +return -ENOMEM ; + } +if (count || inline_count) { + info.kprobe_multi.addrs =ptr_to_u64 (addrs); info.kprobe_multi.cookies =ptr_to_u64 (cookies); + info.kprobe_multi.inline_addrs =ptr_to_u64 (inline_addrs); + info.kprobe_multi.inline_cookies =ptr_to_u64 (inline_cookies); + info.kprobe_multi.inline_ids =ptr_to_u64 (inline_ids); goto again; } }
@@ -1282,6 +1536,9 @@ static int do_show_link(int fd)show_link_close_plain (fd, &info);free (ref_ctr_offsets); +free (inline_ids); +free (inline_cookies); +free (inline_addrs);free (cookies);free (offsets);free (addrs);
@@ -1353,6 +1610,7 @@ static int do_show(int argc, char **argv) out:if (dd.sym_count)kernel_syms_destroy (&dd); +free_inline_btf ();return errno ==ENOENT ?0 :-1 ; }
Signature · 1 line
2.56.1
Jiri Olsa
reviewerRFC PATCH 22/31 selftests/bpf: Add inlined functions to bpf_testmod
In reply to Jiri OlsaAdd always inlined functions and their callers to bpf_testmod module for kprobe multi inline tests, the callers are triggered through bpf_testmod_test_read:
bpf_testmod_kprobe_multi_inline_test - calls inline
bpf_testmod_kprobe_multi_inline_args_test - calls inline_args
--- .../selftests/bpf/test_kmods/bpf_testmod.c | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+)
tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +28 -0
@@ -166,6 +166,31 @@ bpf_testmod_test_union_arg_2(int a, union bpf_testmod_union_arg_2 b)return bpf_testmod_test_struct_arg_result; } +/* Inlined function for kprobe multi inline tests. */ +static __always_inline int bpf_testmod_kprobe_multi_inline(int a) +{ +/* Keep an instruction in the inline site. */ + asmvolatile ("nop" ); +return a +1 ; +} + +noinline int bpf_testmod_kprobe_multi_inline_test(int a) +{ +return bpf_testmod_kprobe_multi_inline (a); +} + +/* Inlined function with arguments for kprobe multi inline args test. */ +static __always_inline long bpf_testmod_kprobe_multi_inline_args (int a,long b,void *c) +{ + asmvolatile ("nop" ); +return a + b + (long )c; +} + +noinline long bpf_testmod_kprobe_multi_inline_args_test (int a,long b,void *c) +{ +return bpf_testmod_kprobe_multi_inline_args (a, b, c); +} + noinline intbpf_testmod_test_arg_ptr_to_struct (struct bpf_testmod_struct_arg_1 *a) { bpf_testmod_test_struct_arg_result = a->a;
@@ -828,6 +853,9 @@ bpf_testmod_test_read(struct file *file, struct kobject *kobj, (void )bpf_testmod_test_arg_ptr_to_struct (&struct_arg1_2); + (void )bpf_testmod_kprobe_multi_inline_test (1 ); + (void )bpf_testmod_kprobe_multi_inline_args_test (1 ,2 , (void *)3 ); +#ifdef __SIZEOF_INT128__ (void )bpf_testmod_test_int128_ret (i); (void )bpf_testmod_test_int128_arg ((__int128 )1 ,2 ,3 );
Signature · 1 line
2.56.1
Jiri Olsa
reviewerRFC PATCH 23/31 selftests/bpf: Add kprobe multi inline attach test
In reply to Jiri OlsaAdd inline_attach test that attaches inlined functions with BPF_F_KPROBE_MULTI_INLINE flag and checks each inlined instance is hit once with its function cookie, including instances sharing the same address, and that the probes are gone after detach.
--- .../bpf/prog_tests/kprobe_multi_test.c | 213 ++++++++++++++++++ .../selftests/bpf/progs/kprobe_multi_inline.c | 41 ++++ 2 files changed, 254 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/kprobe_multi_inline.c
tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c +213 -0
@@ -11,6 +11,7 @@#include "kprobe_multi_verifier.skel.h" #include "kprobe_write_ctx.skel.h" #include "kprobe_multi_sleepable.skel.h" +#include "kprobe_multi_inline.skel.h" #include "bpf/libbpf_internal.h" #include "bpf/hashmap.h"
@@ -536,6 +537,16 @@ static void test_kprobe_multi_bench_attach(bool kernel)free_kallsyms_local (ksyms); } +/* Attaching to inlined functions is supported only on x86_64. */ +static bool inline_attach_supported (void ) +{ +#ifdef __x86_64__ +return true; +#else +return false; +#endif +} +static void test_kprobe_multi_bench_attach_addr (bool kernel) {LIBBPF_OPTS (bpf_kprobe_multi_opts, opts);
@@ -703,6 +714,207 @@ static void test_attach_probe_dup_sym(void)kprobe_multi__destroy (skel); } +#define INLINE_MAX 8 + +/* +* Get addresses of inlined instances of @func from inline.text LOCSEC +* of @inline_btf, the offsets are relative to @base. Returns number of +* instances or -ENOENT if there's no such LOCSEC. +*/ +static int find_inline_sites (struct btf *inline_btf,unsigned long base,const char *func, +unsigned long *addrs,int max) +{ +const struct btf_type *t, *ft; +struct btf_loc *l; +int id, i, n, cnt =0 ; + + id =btf__find_by_name_kind (inline_btf,"inline.text" ,BTF_KIND_LOCSEC ); +if (id <0 ) +return -ENOENT ; + + t =btf__type_by_id (inline_btf, id); + n =btf_vlen (t); +for (i =0 , l =btf_locsec_locs (t); i < n; i++, l++) { + ft =btf__type_by_id (inline_btf, l->func); +if (!ft ||strcmp (btf__name_by_offset (inline_btf, ft->name_off), func)) +continue ; +if (!ASSERT_LT (cnt, max,"inline_sites_max" )) +return -EINVAL ; + addrs[cnt++] = base + l->offset; + } +return cnt; +} + +/* BTF used to find inlined instances, loaded once on first use. */ +static struct btf *vmlinux_btf; +static struct btf *vmlinux_inline_btf; + +static struct btf *get_vmlinux_btf (void ) +{ +if (!vmlinux_btf) + vmlinux_btf =btf__load_vmlinux_btf (); +return vmlinux_btf; +} + +/* +* Get split BTF from @path on top of @base, it's parsed just once and +* stored in @cache. Returns 0 on success or negative error. +*/ +static int get_split_btf (struct btf **cache,const char *path,struct btf *base) +{ +struct btf *btf; +int err; + +if (*cache) +return 0 ; + + btf =btf__parse_split (path, base); + err =libbpf_get_error (btf); +if (err) +return err; + *cache = btf; +return 0 ; +} + +static void free_inline_btf (void ) +{ +btf__free (vmlinux_inline_btf); +btf__free (vmlinux_btf); + vmlinux_inline_btf = vmlinux_btf =NULL ; +} + +/* +* Get addresses of inlined instances of @func from vmlinux inline BTF. +* Returns number of instances or -ENOENT if there's no inline BTF. +*/ +static int get_inline_sites (const char *func,unsigned long *addrs,int max) +{ +static unsigned long base; +int err; + +/* LOCSEC offsets are relative to the .text section base. */ +if (!base) + base =ksym_get_addr ("_stext" ); +if (!ASSERT_NEQ (base,0 ,"stext_addr" )) +return -EINVAL ; + +if (!ASSERT_OK_PTR (get_vmlinux_btf (),"vmlinux_btf" )) +return -EINVAL ; + + err =get_split_btf (&vmlinux_inline_btf,"/sys/kernel/btf/vmlinux.inline" , vmlinux_btf); +if (err) { +/* pahole may not have inline BTF feature support. */ +if (err != -ENOENT ) +ASSERT_OK (err,"parse_inline_btf" ); +return err; + } + +return find_inline_sites (vmlinux_inline_btf, base, func, addrs, max); +} + +static int inline_hits (struct kprobe_multi_inline *skel,unsigned long ip,__u64 cookie) +{ +int i, cnt =0 ; + +for (i =0 ; i < skel->bss->hits_cnt; i++) { +if (skel->bss->hits_ip[i] == ip && skel->bss->hits_cookie[i] == cookie) + cnt++; + } +return cnt; +} + +static void inline_trigger (struct kprobe_multi_inline *skel) +{ +LIBBPF_OPTS (bpf_test_run_opts, topts); +int err; + + skel->bss->hits_cnt =0 ; + err =bpf_prog_test_run_opts (bpf_program__fd (skel->progs.trigger), &topts); +ASSERT_OK (err,"test_run" ); +ASSERT_EQ (topts.retval,0 ,"test_run" ); +} + +/* Each inlined instance must be hit just once with its function cookie. */ +static void inline_check_sites (struct kprobe_multi_inline *skel,unsigned long *addrs, +int cnt,__u64 cookie) +{ +int i; + +for (i =0 ; i < cnt; i++) +ASSERT_EQ (inline_hits (skel, addrs[i], cookie),1 ,"inline_site_hit" ); +} + +static void test_inline_attach (void ) +{ +const char *syms[2 ] = {"bpf_kprobe_multi_inline_outer" , +"bpf_kprobe_multi_inline_inner" }; +unsigned long inner[INLINE_MAX ], outer[INLINE_MAX ]; +LIBBPF_OPTS (bpf_kprobe_multi_opts, opts); +struct kprobe_multi_inline *skel =NULL ; +__u64 cookies[2 ] = {2 ,1 }; +int inner_cnt, outer_cnt; + + inner_cnt =get_inline_sites (syms[1 ], inner,INLINE_MAX ); + outer_cnt =get_inline_sites (syms[0 ], outer,INLINE_MAX ); +if (inner_cnt == -ENOENT || outer_cnt == -ENOENT ) { +test__skip (); +return ; + } + +/* inner is inlined in both callers, outer just in the first one */ +if (!ASSERT_EQ (inner_cnt,2 ,"inner_cnt" ) || !ASSERT_EQ (outer_cnt,1 ,"outer_cnt" )) +return ; + + skel =kprobe_multi_inline__open_and_load (); +if (!ASSERT_OK_PTR (skel,"kprobe_multi_inline__open_and_load" )) +return ; + + skel->bss->pid =getpid (); + + opts.syms = syms; + opts.cookies = cookies; + opts.cnt =ARRAY_SIZE (syms); + opts.inlined = true; + + skel->links.test_kprobe_inline = +bpf_program__attach_kprobe_multi_opts (skel->progs.test_kprobe_inline,NULL , &opts); +if (!ASSERT_OK_PTR (skel->links.test_kprobe_inline,"attach_kprobe_multi_opts" )) + goto cleanup; + +inline_trigger (skel); + +/* +* The outer instance is usually on the same address as one of +* the inner instances, each of them needs to get its own cookie. +*/ +ASSERT_EQ (skel->bss->hits_cnt, inner_cnt + outer_cnt,"hits_cnt" ); +inline_check_sites (skel, inner, inner_cnt,1 ); +inline_check_sites (skel, outer, outer_cnt,2 ); + +/* Make sure the probes are gone after detach. */ +kprobe_multi_inline__detach (skel); + +inline_trigger (skel); +ASSERT_EQ (skel->bss->hits_cnt,0 ,"hits_cnt_detached" ); + +cleanup: +kprobe_multi_inline__destroy (skel); +} + +/* Run all the tests for attaching to inlined functions. */ +static void test_inline_subtests (void ) +{ +if (!inline_attach_supported ()) { +test__skip (); +return ; + } + +if (test__start_subtest ("inline_attach" )) +test_inline_attach (); + +free_inline_btf (); +} +void serial_test_kprobe_multi_bench_attach (void ) {if (test__start_subtest ("kernel" ))
@@ -748,5 +960,6 @@ void test_kprobe_multi_test(void)test_attach_write_ctx ();if (test__start_subtest ("dup_sym" ))test_attach_probe_dup_sym (); +test_inline_subtests ();RUN_TESTS (kprobe_multi_verifier); }
tools/testing/selftests/bpf/progs/kprobe_multi_inline.c new file +41 -0
41 lines not on this page · raw message
Signature · 1 line
2.56.1
Jiri Olsa
reviewerRFC PATCH 24/31 selftests/bpf: Add kprobe multi inline mixed test
In reply to Jiri OlsaAdd inline_mixed test that attaches function with entry probe and inlined function in single kprobe multi link, and checks that both the function entry and the inlined instances are hit once with their cookies.
--- .../bpf/prog_tests/kprobe_multi_test.c | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+)
tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c +50 -0
@@ -901,6 +901,54 @@ static void test_inline_attach(void)kprobe_multi_inline__destroy (skel); } +/* Attach function with entry probe and inlined function in single link. */ +static void test_inline_mixed (void ) +{ +const char *syms[2 ] = {"bpf_kprobe_multi_inline_inner" ,"bpf_fentry_test1" }; +LIBBPF_OPTS (bpf_kprobe_multi_opts, opts); +struct kprobe_multi_inline *skel =NULL ; +unsigned long inner[INLINE_MAX ], addr; +__u64 cookies[2 ] = {6 ,5 }; +int inner_cnt; + + inner_cnt =get_inline_sites (syms[0 ], inner,INLINE_MAX ); +if (inner_cnt == -ENOENT ) { +test__skip (); +return ; + } +if (!ASSERT_EQ (inner_cnt,2 ,"inner_cnt" )) +return ; + + addr =ksym_get_addr ("bpf_fentry_test1" ); +if (!ASSERT_NEQ (addr,0 ,"bpf_fentry_test1_addr" )) +return ; + + skel =kprobe_multi_inline__open_and_load (); +if (!ASSERT_OK_PTR (skel,"kprobe_multi_inline__open_and_load" )) +return ; + + skel->bss->pid =getpid (); + + opts.syms = syms; + opts.cookies = cookies; + opts.cnt =ARRAY_SIZE (syms); + opts.inlined = true; + + skel->links.test_kprobe_inline = +bpf_program__attach_kprobe_multi_opts (skel->progs.test_kprobe_inline,NULL , &opts); +if (!ASSERT_OK_PTR (skel->links.test_kprobe_inline,"attach_kprobe_multi_opts" )) + goto cleanup; + +inline_trigger (skel); + +ASSERT_EQ (skel->bss->hits_cnt, inner_cnt +1 ,"hits_cnt" ); +ASSERT_EQ (inline_hits (skel, addr,5 ),1 ,"entry_hit" ); +inline_check_sites (skel, inner, inner_cnt,6 ); + +cleanup: +kprobe_multi_inline__destroy (skel); +} +/* Run all the tests for attaching to inlined functions. */ static void test_inline_subtests (void ) {
@@ -911,6 +959,8 @@ static void test_inline_subtests(void)if (test__start_subtest ("inline_attach" ))test_inline_attach (); +if (test__start_subtest ("inline_mixed" )) +test_inline_mixed ();free_inline_btf (); }
Signature · 1 line
2.56.1
Jiri Olsa
reviewerRFC PATCH 25/31 selftests/bpf: Add kprobe multi inline skip test
In reply to Jiri OlsaAdd inline_skip test that attaches function with one inlined instance that can be attached and one that can't, and checks the second one is skipped.
Also check the attach fails for function that has no entry and its only inlined instance can't be attached.
--- .../bpf/prog_tests/kprobe_multi_test.c | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+)
tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c +102 -0
@@ -487,6 +487,33 @@ static void test_unique_match(void)kprobe_multi__destroy (skel); } +/* +* Get value of the @name counter from fdinfo of kprobe multi link. +* +* The same counters are available also in link info, but we want to +* have fdinfo tested as well and link info has its own test in +* fill_link_info.c. +*/ +static long get_fdinfo_cnt (int link_fd,const char *name) +{ +char path[64 ], buf[256 ], fmt[64 ]; +long cnt =-1 ; +FILE *f; + +snprintf (path,sizeof (path),"/proc/self/fdinfo/%d" , link_fd); + f =fopen (path,"r" ); +if (!ASSERT_OK_PTR (f,"open_fdinfo" )) +return -1 ; + +snprintf (fmt,sizeof (fmt),"%s:\t%%ld" , name); +while (fgets (buf,sizeof (buf), f)) { +if (sscanf (buf, fmt, &cnt) ==1 ) +break ; + } +fclose (f); +return cnt; +} +static void do_bench_test (struct kprobe_multi_empty *skel,struct bpf_kprobe_multi_opts *opts) {long attach_start_ns, attach_end_ns;
@@ -949,6 +976,79 @@ static void test_inline_mixed(void)kprobe_multi_inline__destroy (skel); } +/* Inlined instances that can't be attached are skipped. */ +static void test_inline_skip (void ) +{ +LIBBPF_OPTS (bpf_kprobe_multi_opts, opts); +const char *syms[1 ] = {"bpf_kprobe_multi_inline_partial" }; +const char *nosyms[1 ] = {"bpf_kprobe_multi_inline_nokprobe" }; +struct kprobe_multi_inline *skel =NULL ; +unsigned long sites[INLINE_MAX ]; +__u64 cookies[1 ] = {8 }; +int cnt, i, saved_error, link_fd; +struct ksym *ksym; +bool attached; + + cnt =get_inline_sites (syms[0 ], sites,INLINE_MAX ); +if (cnt == -ENOENT ) { +test__skip (); +return ; + } + +/* One instance in function that kprobes can attach and one that can't. */ +if (!ASSERT_EQ (cnt,2 ,"inline_sites_cnt" )) +return ; + + skel =kprobe_multi_inline__open_and_load (); +if (!ASSERT_OK_PTR (skel,"kprobe_multi_inline__open_and_load" )) +return ; + + skel->bss->pid =getpid (); + + opts.syms = syms; + opts.cookies = cookies; + opts.cnt =ARRAY_SIZE (syms); + opts.inlined = true; + + skel->links.test_kprobe_inline = +bpf_program__attach_kprobe_multi_opts (skel->progs.test_kprobe_inline,NULL , &opts); +if (!ASSERT_OK_PTR (skel->links.test_kprobe_inline,"attach_kprobe_multi_opts" )) + goto cleanup; + + link_fd =bpf_link__fd (skel->links.test_kprobe_inline); +ASSERT_EQ (get_fdinfo_cnt (link_fd,"kprobe_inline_cnt" ),1 ,"kprobe_inline_cnt" ); +ASSERT_EQ (get_fdinfo_cnt (link_fd,"kprobe_inline_skipped" ),1 ,"kprobe_inline_skipped" ); + +inline_trigger (skel); + +/* Only the instance outside of the nokprobe function is hit. */ +ASSERT_EQ (skel->bss->hits_cnt,1 ,"hits_cnt" ); + +for (i =0 ; i < cnt; i++) { + ksym =ksym_search (sites[i]); +if (!ASSERT_OK_PTR (ksym,"ksym_search" )) +continue ; + attached = !strcmp (ksym->name,"bpf_kprobe_multi_inline_test3" ); +ASSERT_EQ (inline_hits (skel, sites[i],8 ), attached ?1 :0 ,"inline_site_hit" ); + } + +kprobe_multi_inline__detach (skel); + +/* Function with no entry and all inlined instances skipped fails to attach. */ + opts.syms = nosyms; + opts.cookies =NULL ; + + skel->links.test_kprobe_inline = +bpf_program__attach_kprobe_multi_opts (skel->progs.test_kprobe_inline,NULL , &opts); + saved_error = -errno; +if (!ASSERT_ERR_PTR (skel->links.test_kprobe_inline,"attach_nokprobe" )) + goto cleanup; +ASSERT_EQ (saved_error, -EINVAL ,"attach_nokprobe_error" ); + +cleanup: +kprobe_multi_inline__destroy (skel); +} +/* Run all the tests for attaching to inlined functions. */ static void test_inline_subtests (void ) {
@@ -961,6 +1061,8 @@ static void test_inline_subtests(void)test_inline_attach ();if (test__start_subtest ("inline_mixed" ))test_inline_mixed (); +if (test__start_subtest ("inline_skip" )) +test_inline_skip ();free_inline_btf (); }
Signature · 1 line
2.56.1
Jiri Olsa
reviewerRFC PATCH 26/31 selftests/bpf: Add kprobe multi inline fails test
In reply to Jiri OlsaAdd inline_fails test for attach failures of kprobe multi link with BPF_F_KPROBE_MULTI_INLINE flag: used with addrs, with return probe, with session program, with wildcard pattern and for function that has nothing to attach.
Most of the setups are rejected by libbpf already, so they are tested also through bpf_link_create to check the kernel error and the message in the link_create log.
--- .../bpf/prog_tests/kprobe_multi_test.c | 168 ++++++++++++++++++ .../selftests/bpf/progs/kprobe_multi_inline.c | 6 + 2 files changed, 174 insertions(+)
tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c +168 -0
@@ -1049,6 +1049,172 @@ static void test_inline_skip(void)kprobe_multi_inline__destroy (skel); } +/* +* Make sure the link create fails with @error and kernel explains +* the failure with @msg in the log. +*/ +static bool inline_link_fails (int prog_fd,enum bpf_attach_type type, +struct bpf_link_create_opts *lopts, +int error,const char *msg) +{ +LIBBPF_OPTS (bpf_log_opts, log_opts); +char log_buf[4096 ] = {}; +int saved_error, link_fd; + + log_opts.buf = log_buf; + log_opts.size =sizeof (log_buf); + log_opts.level =1 ; + lopts->log_opts = &log_opts; + + link_fd =bpf_link_create (prog_fd,0 , type, lopts); + saved_error = -errno; +if (!ASSERT_LT (link_fd,0 ,"link_create" )) { +close (link_fd); +return false; + } +ASSERT_EQ (saved_error, error,"link_create_error" ); +ASSERT_HAS_SUBSTR (log_buf, msg,"link_create_log" ); +return true; +} + +/* +* Most of the invalid inline setups are rejected by libbpf before +* it gets to the kernel, so each setup is tested twice: +* +* - through bpf_program__attach_kprobe_multi_opts to test the libbpf check, +* - through bpf_link_create (inline_link_fails) to test the kernel check +* together with the failure message in the kernel log, which libbpf +* does not pass to the caller. +*/ +static void test_inline_fails (void ) +{ +LIBBPF_OPTS (bpf_kprobe_multi_opts, opts); +LIBBPF_OPTS (bpf_link_create_opts, lopts); +struct kprobe_multi_inline *skel =NULL ; +const char *syms[1 ] = {"bpf_kprobe_multi_inline_inner" }; +const char *nosyms[1 ] = {"bpf_kprobe_multi_inline_nonexistent" }; +const char *nokprobe[1 ] = {"bpf_kprobe_multi_inline_nokprobe" }; +unsigned long inner[INLINE_MAX ], addrs[1 ]; +int saved_error, prog_fd; + +if (get_inline_sites (syms[0 ], inner,INLINE_MAX ) == -ENOENT ) { +test__skip (); +return ; + } + + addrs[0 ] =ksym_get_addr ("bpf_fentry_test1" ); +if (!ASSERT_NEQ (addrs[0 ],0 ,"bpf_fentry_test1_addr" )) +return ; + + skel =kprobe_multi_inline__open_and_load (); +if (!ASSERT_OK_PTR (skel,"kprobe_multi_inline__open_and_load" )) +return ; + + prog_fd =bpf_program__fd (skel->progs.test_kprobe_inline); + +/* fail_1 - inlined with addrs */ + opts.addrs = addrs; + opts.cnt =1 ; + opts.inlined = true; + + skel->links.test_kprobe_inline = +bpf_program__attach_kprobe_multi_opts (skel->progs.test_kprobe_inline,NULL , &opts); + saved_error = -errno; +if (!ASSERT_ERR_PTR (skel->links.test_kprobe_inline,"fail_1" )) + goto cleanup; +ASSERT_EQ (saved_error, -EINVAL ,"fail_1_error" ); + + lopts.kprobe_multi.addrs = addrs; + lopts.kprobe_multi.cnt =1 ; + lopts.kprobe_multi.flags =BPF_F_KPROBE_MULTI_INLINE ; + +if (!inline_link_fails (prog_fd,BPF_TRACE_KPROBE_MULTI , &lopts, -EINVAL , +"Inlined functions need to be specified with kprobe_multi.syms." )) + goto cleanup; + +/* fail_2 - inlined with return probe */ + LIBBPF_OPTS_RESET(opts, + .syms = syms, + .cnt = 1, + .inlined = true, + .retprobe = true, + ); + + skel->links.test_kprobe_inline = +bpf_program__attach_kprobe_multi_opts (skel->progs.test_kprobe_inline,NULL , &opts); + saved_error = -errno; +if (!ASSERT_ERR_PTR (skel->links.test_kprobe_inline,"fail_2" )) + goto cleanup; +ASSERT_EQ (saved_error, -EINVAL ,"fail_2_error" ); + + LIBBPF_OPTS_RESET(lopts, + .kprobe_multi.syms = syms, + .kprobe_multi.cnt = 1, + .kprobe_multi.flags = BPF_F_KPROBE_MULTI_INLINE | BPF_F_KPROBE_MULTI_RETURN, + ); + +if (!inline_link_fails (prog_fd,BPF_TRACE_KPROBE_MULTI , &lopts, -EINVAL , +"Inlined functions can't be attached with return probe." )) + goto cleanup; + +/* fail_3 - inlined with session program */ + lopts.kprobe_multi.flags =BPF_F_KPROBE_MULTI_INLINE ; + +if (!inline_link_fails (bpf_program__fd (skel->progs.test_session_inline), +BPF_TRACE_KPROBE_SESSION , &lopts, -EINVAL , +"Inlined functions can't be attached with session or override " +"program." )) + goto cleanup; + +/* fail_4 - inlined with wildcard pattern */ + LIBBPF_OPTS_RESET(opts, + .inlined = true, + ); + + skel->links.test_kprobe_inline = +bpf_program__attach_kprobe_multi_opts (skel->progs.test_kprobe_inline, +"bpf_kprobe_multi_inline_*" , &opts); + saved_error = -errno; +if (!ASSERT_ERR_PTR (skel->links.test_kprobe_inline,"fail_4" )) + goto cleanup; +ASSERT_EQ (saved_error, -EINVAL ,"fail_4_error" ); + +/* fail_5 - function with no entry and no inlined instance */ + LIBBPF_OPTS_RESET(opts, + .syms = nosyms, + .cnt = 1, + .inlined = true, + ); + + skel->links.test_kprobe_inline = +bpf_program__attach_kprobe_multi_opts (skel->progs.test_kprobe_inline,NULL , &opts); + saved_error = -errno; +if (!ASSERT_ERR_PTR (skel->links.test_kprobe_inline,"fail_5" )) + goto cleanup; +ASSERT_EQ (saved_error, -ENOENT ,"fail_5_error" ); + + LIBBPF_OPTS_RESET(lopts, + .kprobe_multi.syms = nosyms, + .kprobe_multi.cnt = 1, + .kprobe_multi.flags = BPF_F_KPROBE_MULTI_INLINE, + ); + +if (!inline_link_fails (prog_fd,BPF_TRACE_KPROBE_MULTI , &lopts, -ESRCH , +"Function bpf_kprobe_multi_inline_nonexistent has no entry and " +"no inlined instance." )) + goto cleanup; + +/* fail_6 - function with no entry and no inlined instance that can be attached */ + lopts.kprobe_multi.syms = nokprobe; + +inline_link_fails (prog_fd,BPF_TRACE_KPROBE_MULTI , &lopts, -EINVAL , +"Function bpf_kprobe_multi_inline_nokprobe has no entry and none of its " +"inlined instances can be attached, bpf_kprobe_multi_inline_test4+" ); + +cleanup: +kprobe_multi_inline__destroy (skel); +} +/* Run all the tests for attaching to inlined functions. */ static void test_inline_subtests (void ) {
@@ -1063,6 +1229,8 @@ static void test_inline_subtests(void)test_inline_mixed ();if (test__start_subtest ("inline_skip" ))test_inline_skip (); +if (test__start_subtest ("inline_fails" )) +test_inline_fails ();free_inline_btf (); }
tools/testing/selftests/bpf/progs/kprobe_multi_inline.c +6 -0
@@ -39,3 +39,9 @@ int test_kprobe_inline(struct pt_regs *ctx) hits_cnt = idx +1 ;return 0 ; } + +SEC("kprobe.session") +int test_session_inline (struct pt_regs *ctx) +{ +return 0 ; +}
Signature · 1 line
2.56.1
Jiri Olsa
reviewerRFC PATCH 27/31 selftests/bpf: Add kprobe multi inline module test
In reply to Jiri OlsaAdd inline_module test that attaches function inlined in bpf_testmod module with BPF_F_KPROBE_MULTI_INLINE flag and checks its inlined instance is hit once with the function cookie and that the probe is gone after detach.
The expected address of the inlined instance is read from bpf_testmod inline BTF, the test is skipped if it's not available.
--- .../bpf/prog_tests/kprobe_multi_test.c | 115 +++++++++++++++++- 1 file changed, 114 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c +114 -1
@@ -775,6 +775,8 @@ static int find_inline_sites(struct btf *inline_btf, unsigned long base, const c/* BTF used to find inlined instances, loaded once on first use. */ static struct btf *vmlinux_btf;static struct btf *vmlinux_inline_btf; +static struct btf *testmod_btf; +static struct btf *testmod_inline_btf;static struct btf *get_vmlinux_btf (void ) {
@@ -805,9 +807,11 @@ static int get_split_btf(struct btf **cache, const char *path, struct btf *base)static void free_inline_btf (void ) { +btf__free (testmod_inline_btf); +btf__free (testmod_btf);btf__free (vmlinux_inline_btf);btf__free (vmlinux_btf); - vmlinux_inline_btf = vmlinux_btf =NULL ; + testmod_inline_btf = testmod_btf = vmlinux_inline_btf = vmlinux_btf =NULL ; }/*
@@ -839,6 +843,56 @@ static int get_inline_sites(const char *func, unsigned long *addrs, int max)return find_inline_sites (vmlinux_inline_btf, base, func, addrs, max); } +/* Get address of bpf_testmod .text section, zero if it's not available. */ +static unsigned long get_testmod_text_addr (void ) +{ +static unsigned long addr; +FILE *f; + +if (addr) +return addr; + + f =fopen ("/sys/module/bpf_testmod/sections/.text" ,"r" ); +if (!f) +return 0 ; +if (fscanf (f,"%lx" , &addr) !=1 ) + addr =0 ; +fclose (f); +return addr; +} + +/* +* Get addresses of inlined instances of @func from bpf_testmod inline BTF. +* Returns number of instances or -ENOENT if there's no inline BTF. +*/ +static int get_testmod_inline_sites (const char *func,unsigned long *addrs,int max) +{ +unsigned long base; +int err; + +/* LOCSEC offsets are relative to the module .text section base. */ + base =get_testmod_text_addr (); +if (!ASSERT_NEQ (base,0 ,"testmod_text_addr" )) +return -EINVAL ; + +if (!ASSERT_OK_PTR (get_vmlinux_btf (),"vmlinux_btf" )) +return -EINVAL ; + + err =get_split_btf (&testmod_btf,"/sys/kernel/btf/bpf_testmod" , vmlinux_btf); +if (!ASSERT_OK (err,"testmod_btf" )) +return -EINVAL ; + + err =get_split_btf (&testmod_inline_btf,"/sys/kernel/btf/bpf_testmod.inline" , testmod_btf); +if (err) { +/* pahole may not have inline BTF feature support. */ +if (err != -ENOENT ) +ASSERT_OK (err,"parse_testmod_inline_btf" ); +return err; + } + +return find_inline_sites (testmod_inline_btf, base, func, addrs, max); +} +static int inline_hits (struct kprobe_multi_inline *skel,unsigned long ip,__u64 cookie) {int i, cnt =0 ;
@@ -1049,6 +1103,63 @@ static void test_inline_skip(void)kprobe_multi_inline__destroy (skel); } +/* Attach inlined function in kernel module. */ +static void test_inline_module (void ) +{ +LIBBPF_OPTS (bpf_kprobe_multi_opts, opts); +const char *syms[1 ] = {"bpf_testmod_kprobe_multi_inline" }; +struct kprobe_multi_inline *skel =NULL ; +unsigned long sites[INLINE_MAX ]; +__u64 cookies[1 ] = {7 }; +struct ksym *ksym; +int cnt; + + cnt =get_testmod_inline_sites (syms[0 ], sites,INLINE_MAX ); +if (cnt == -ENOENT ) { +test__skip (); +return ; + } +if (!ASSERT_EQ (cnt,1 ,"sites_cnt" )) +return ; + + ksym =ksym_search (sites[0 ]); +if (!ASSERT_OK_PTR (ksym,"ksym_search" )) +return ; +ASSERT_STREQ (ksym->name,"bpf_testmod_kprobe_multi_inline_test" ,"inline_site_caller" ); + + skel =kprobe_multi_inline__open_and_load (); +if (!ASSERT_OK_PTR (skel,"kprobe_multi_inline__open_and_load" )) +return ; + + skel->bss->pid =getpid (); + + opts.syms = syms; + opts.cookies = cookies; + opts.cnt =ARRAY_SIZE (syms); + opts.inlined = true; + + skel->links.test_kprobe_inline = +bpf_program__attach_kprobe_multi_opts (skel->progs.test_kprobe_inline,NULL , &opts); +if (!ASSERT_OK_PTR (skel->links.test_kprobe_inline,"attach_kprobe_multi_opts" )) + goto cleanup; + + skel->bss->hits_cnt =0 ; +ASSERT_OK (trigger_module_test_read (1 ),"trigger_read" ); + +ASSERT_EQ (skel->bss->hits_cnt,1 ,"hits_cnt" ); +ASSERT_EQ (inline_hits (skel, sites[0 ],7 ),1 ,"inline_site_hit" ); + +/* Make sure the probe is gone after detach. */ +kprobe_multi_inline__detach (skel); + + skel->bss->hits_cnt =0 ; +ASSERT_OK (trigger_module_test_read (1 ),"trigger_read" ); +ASSERT_EQ (skel->bss->hits_cnt,0 ,"hits_cnt_detached" ); + +cleanup: +kprobe_multi_inline__destroy (skel); +} +/* * Make sure the link create fails with @error and kernel explains * the failure with @msg in the log.
@@ -1231,6 +1342,8 @@ static void test_inline_subtests(void)test_inline_skip ();if (test__start_subtest ("inline_fails" ))test_inline_fails (); +if (test__start_subtest ("inline_module" )) +test_inline_module ();free_inline_btf (); }
Signature · 1 line
2.56.1
Jiri Olsa
reviewerRFC PATCH 28/31 selftests/bpf: Add kprobe multi inline strict test
In reply to Jiri OlsaAdd inline_strict test for kprobe multi link withBPF_F_KPROBE_MULTI_INLINE_STRICT flag:
- the attach fails when the flag is used without BPF_F_KPROBE_MULTI_INLINE flag
- the attach fails for function with inlined instance that can't be attached, and the log names the function and the instance
- the attach succeeds for function with all the inlined instances attached and link's fdinfo shows no skipped instance
--- .../bpf/prog_tests/kprobe_multi_test.c | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+)
tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c +82 -0
@@ -1326,6 +1326,86 @@ static void test_inline_fails(void)kprobe_multi_inline__destroy (skel); } +/* With strict flag all the inlined instances need to be attached. */ +static void test_inline_strict (void ) +{ +LIBBPF_OPTS (bpf_kprobe_multi_opts, opts); +LIBBPF_OPTS (bpf_link_create_opts, lopts); +const char *partial[1 ] = {"bpf_kprobe_multi_inline_partial" }; +const char *syms[1 ] = {"bpf_kprobe_multi_inline_inner" }; +struct kprobe_multi_inline *skel =NULL ; +unsigned long sites[INLINE_MAX ]; +int cnt, saved_error, prog_fd, link_fd; + + cnt =get_inline_sites (syms[0 ], sites,INLINE_MAX ); +if (cnt == -ENOENT ) { +test__skip (); +return ; + } +if (!ASSERT_EQ (cnt,2 ,"inline_sites_cnt" )) +return ; + + skel =kprobe_multi_inline__open_and_load (); +if (!ASSERT_OK_PTR (skel,"kprobe_multi_inline__open_and_load" )) +return ; + + prog_fd =bpf_program__fd (skel->progs.test_kprobe_inline); + +/* strict without inlined */ + opts.syms = syms; + opts.cnt =1 ; + opts.inlined_strict = true; + + skel->links.test_kprobe_inline = +bpf_program__attach_kprobe_multi_opts (skel->progs.test_kprobe_inline,NULL , &opts); + saved_error = -errno; +if (!ASSERT_ERR_PTR (skel->links.test_kprobe_inline,"strict_no_inlined" )) + goto cleanup; +ASSERT_EQ (saved_error, -EINVAL ,"strict_no_inlined_error" ); + + lopts.kprobe_multi.syms = syms; + lopts.kprobe_multi.cnt =1 ; + lopts.kprobe_multi.flags =BPF_F_KPROBE_MULTI_INLINE_STRICT ; + +if (!inline_link_fails (prog_fd,BPF_TRACE_KPROBE_MULTI , &lopts, -EINVAL , +"Strict flag needs to be used with inline flag." )) + goto cleanup; + +/* function with inlined instance that can't be attached */ + opts.syms = partial; + opts.inlined = true; + + skel->links.test_kprobe_inline = +bpf_program__attach_kprobe_multi_opts (skel->progs.test_kprobe_inline,NULL , &opts); + saved_error = -errno; +if (!ASSERT_ERR_PTR (skel->links.test_kprobe_inline,"strict_partial" )) + goto cleanup; +ASSERT_EQ (saved_error, -EINVAL ,"strict_partial_error" ); + + lopts.kprobe_multi.syms = partial; + lopts.kprobe_multi.flags =BPF_F_KPROBE_MULTI_INLINE |BPF_F_KPROBE_MULTI_INLINE_STRICT ; + +if (!inline_link_fails (prog_fd,BPF_TRACE_KPROBE_MULTI , &lopts, -EINVAL , +"Function bpf_kprobe_multi_inline_partial has inlined instance " +"that can't be attached, bpf_kprobe_multi_inline_test4+" )) + goto cleanup; + +/* function with all the inlined instances attached */ + opts.syms = syms; + + skel->links.test_kprobe_inline = +bpf_program__attach_kprobe_multi_opts (skel->progs.test_kprobe_inline,NULL , &opts); +if (!ASSERT_OK_PTR (skel->links.test_kprobe_inline,"strict_attach" )) + goto cleanup; + + link_fd =bpf_link__fd (skel->links.test_kprobe_inline); +ASSERT_EQ (get_fdinfo_cnt (link_fd,"kprobe_inline_cnt" ), cnt,"kprobe_inline_cnt" ); +ASSERT_EQ (get_fdinfo_cnt (link_fd,"kprobe_inline_skipped" ),0 ,"kprobe_inline_skipped" ); + +cleanup: +kprobe_multi_inline__destroy (skel); +} +/* Run all the tests for attaching to inlined functions. */ static void test_inline_subtests (void ) {
@@ -1344,6 +1424,8 @@ static void test_inline_subtests(void)test_inline_fails ();if (test__start_subtest ("inline_module" ))test_inline_module (); +if (test__start_subtest ("inline_strict" )) +test_inline_strict ();free_inline_btf (); }
Signature · 1 line
2.56.1
Jiri Olsa
reviewerRFC PATCH 29/31 selftests/bpf: Add kprobe multi inline args test
In reply to Jiri OlsaAdd inline_args and inline_args_module tests for bpf_get_func_arg and bpf_get_func_arg_cnt helpers used in kprobe multi program, for inlined function in kernel and module and for function with entry probe.
--- .../bpf/prog_tests/kprobe_multi_test.c | 185 ++++++++++++++++++ .../selftests/bpf/progs/kprobe_multi_inline.c | 32 +++ 2 files changed, 217 insertions(+)
tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c +185 -0
@@ -1160,6 +1160,187 @@ static void test_inline_module(void)kprobe_multi_inline__destroy (skel); } +#define INLINE_ARGS_CNT 3 + +/* +* The probe on @ip must be hit just once and see @exp arguments, +* anything above that needs to fail. +*/ +static void inline_check_args (struct kprobe_multi_inline *skel,unsigned long ip, +const __u64 *exp) +{ +int i, idx =-1 ; + +if (!ASSERT_EQ (inline_hits (skel, ip,0 ),1 ,"args_hit" )) +return ; + +for (i =0 ; i < skel->bss->hits_cnt; i++) { +if (skel->bss->hits_ip[i] == ip) + idx = i; + } + +ASSERT_EQ (skel->bss->args_cnt[idx],INLINE_ARGS_CNT ,"args_cnt" ); + +for (i =0 ; i <INLINE_ARGS_CNT ; i++) { +ASSERT_OK (skel->bss->args_err[idx][i],"args_err" ); +/* The arguments have different sizes, check the lower part. */ +ASSERT_EQ ((__u32 )skel->bss->args_val[idx][i], (__u32 )exp[i],"args_val" ); + } + +ASSERT_EQ (skel->bss->args_err[idx][i], -EINVAL ,"args_err_last" ); +} + +/* +* Get arguments of inlined function and of function with entry probe +* in single link. The arguments of function with entry probe work also +* for link without inline flag, but not for return probe. +*/ +static void test_inline_args (void ) +{ +const char *syms[2 ] = {"bpf_kprobe_multi_inline_args" ,"bpf_fentry_test3" }; +LIBBPF_OPTS (bpf_kprobe_multi_opts, opts); +struct kprobe_multi_inline *skel =NULL ; +__u64 exp_entry[INLINE_ARGS_CNT ] = {4 ,5 ,6 }; +__u64 exp[INLINE_ARGS_CNT ] = {1 ,2 ,3 }; +unsigned long sites[INLINE_MAX ], addr; +int cnt, i; + + cnt =get_inline_sites (syms[0 ], sites,INLINE_MAX ); +if (cnt == -ENOENT ) { +test__skip (); +return ; + } +if (!ASSERT_EQ (cnt,1 ,"sites_cnt" )) +return ; + + addr =ksym_get_addr ("bpf_fentry_test3" ); +if (!ASSERT_NEQ (addr,0 ,"bpf_fentry_test3_addr" )) +return ; + + skel =kprobe_multi_inline__open_and_load (); +if (!ASSERT_OK_PTR (skel,"kprobe_multi_inline__open_and_load" )) +return ; + + skel->bss->pid =getpid (); + + opts.syms = syms; + opts.cnt =ARRAY_SIZE (syms); + opts.inlined = true; + + skel->links.test_kprobe_inline_args = +bpf_program__attach_kprobe_multi_opts (skel->progs.test_kprobe_inline_args, +NULL , &opts); +if (!ASSERT_OK_PTR (skel->links.test_kprobe_inline_args,"attach_kprobe_multi_opts" )) + goto cleanup; + +inline_trigger (skel); + +ASSERT_EQ (skel->bss->hits_cnt,2 ,"hits_cnt" ); +inline_check_args (skel, sites[0 ], exp); +inline_check_args (skel, addr, exp_entry); + +kprobe_multi_inline__detach (skel); + +/* Function with entry probe has arguments also without inline flag. */ + opts.syms = &syms[1 ]; + opts.cnt =1 ; + opts.inlined = false; + + skel->links.test_kprobe_inline_args = +bpf_program__attach_kprobe_multi_opts (skel->progs.test_kprobe_inline_args, +NULL , &opts); +if (!ASSERT_OK_PTR (skel->links.test_kprobe_inline_args,"attach_kprobe_multi_opts" )) + goto cleanup; + +inline_trigger (skel); + +ASSERT_EQ (skel->bss->hits_cnt,1 ,"hits_cnt" ); +inline_check_args (skel, addr, exp_entry); + +kprobe_multi_inline__detach (skel); + +/* The same for function specified by address. */ + opts.syms =NULL ; + opts.addrs = &addr; + + skel->links.test_kprobe_inline_args = +bpf_program__attach_kprobe_multi_opts (skel->progs.test_kprobe_inline_args, +NULL , &opts); +if (!ASSERT_OK_PTR (skel->links.test_kprobe_inline_args,"attach_kprobe_multi_opts" )) + goto cleanup; + +inline_trigger (skel); + +ASSERT_EQ (skel->bss->hits_cnt,1 ,"hits_cnt" ); +inline_check_args (skel, addr, exp_entry); + +kprobe_multi_inline__detach (skel); + +/* There are no arguments in return probe. */ + opts.retprobe = true; + + skel->links.test_kprobe_inline_args = +bpf_program__attach_kprobe_multi_opts (skel->progs.test_kprobe_inline_args, +NULL , &opts); +if (!ASSERT_OK_PTR (skel->links.test_kprobe_inline_args,"attach_kprobe_multi_opts" )) + goto cleanup; + +inline_trigger (skel); + +if (!ASSERT_EQ (skel->bss->hits_cnt,1 ,"hits_cnt" )) + goto cleanup; +ASSERT_EQ (skel->bss->args_cnt[0 ],0 ,"args_cnt" ); +for (i =0 ; i <INLINE_ARGS_CNT ; i++) +ASSERT_EQ (skel->bss->args_err[0 ][i], -EOPNOTSUPP ,"args_err" ); + +cleanup: +kprobe_multi_inline__destroy (skel); +} + +/* Get arguments of function inlined in module. */ +static void test_inline_args_module (void ) +{ +LIBBPF_OPTS (bpf_kprobe_multi_opts, opts); +const char *syms[1 ] = {"bpf_testmod_kprobe_multi_inline_args" }; +struct kprobe_multi_inline *skel =NULL ; +__u64 exp[INLINE_ARGS_CNT ] = {1 ,2 ,3 }; +unsigned long sites[INLINE_MAX ]; +int cnt; + + cnt =get_testmod_inline_sites (syms[0 ], sites,INLINE_MAX ); +if (cnt == -ENOENT ) { +test__skip (); +return ; + } +if (!ASSERT_EQ (cnt,1 ,"sites_cnt" )) +return ; + + skel =kprobe_multi_inline__open_and_load (); +if (!ASSERT_OK_PTR (skel,"kprobe_multi_inline__open_and_load" )) +return ; + + skel->bss->pid =getpid (); + + opts.syms = syms; + opts.cnt =ARRAY_SIZE (syms); + opts.inlined = true; + + skel->links.test_kprobe_inline_args = +bpf_program__attach_kprobe_multi_opts (skel->progs.test_kprobe_inline_args, +NULL , &opts); +if (!ASSERT_OK_PTR (skel->links.test_kprobe_inline_args,"attach_kprobe_multi_opts" )) + goto cleanup; + + skel->bss->hits_cnt =0 ; +ASSERT_OK (trigger_module_test_read (1 ),"trigger_read" ); + +ASSERT_EQ (skel->bss->hits_cnt,1 ,"hits_cnt" ); +inline_check_args (skel, sites[0 ], exp); + +cleanup: +kprobe_multi_inline__destroy (skel); +} +/* * Make sure the link create fails with @error and kernel explains * the failure with @msg in the log.
@@ -1426,6 +1607,10 @@ static void test_inline_subtests(void)test_inline_module ();if (test__start_subtest ("inline_strict" ))test_inline_strict (); +if (test__start_subtest ("inline_args" )) +test_inline_args (); +if (test__start_subtest ("inline_args_module" )) +test_inline_args_module ();free_inline_btf (); }
tools/testing/selftests/bpf/progs/kprobe_multi_inline.c +32 -0
@@ -6,6 +6,7 @@ char _license[] SEC("license") = "GPL";#define MAX_HITS 16 +#define MAX_ARGS 4int pid =0 ;
@@ -13,6 +14,10 @@ __u64 hits_ip[MAX_HITS];__u64 hits_cookie[MAX_HITS ];__u32 hits_cnt =0 ; +__u64 args_cnt[MAX_HITS ]; +__u64 args_val[MAX_HITS ][MAX_ARGS ]; +long args_err[MAX_HITS ][MAX_ARGS ]; +/* * No tests in here, just to trigger 'bpf_kprobe_multi_inline_test*' * through tracing test_run
@@ -40,6 +45,33 @@ int test_kprobe_inline(struct pt_regs *ctx)return 0 ; } +SEC("kprobe.multi") +int test_kprobe_inline_args (struct pt_regs *ctx) +{ +__u32 idx = hits_cnt; +__u64 val; +int i; + +if (bpf_get_current_pid_tgid () >>32 != pid) +return 0 ; + +if (idx >=MAX_HITS ) +return 0 ; + + hits_ip[idx] =bpf_get_func_ip (ctx); + hits_cookie[idx] =bpf_get_attach_cookie (ctx); + args_cnt[idx] =bpf_get_func_arg_cnt (ctx); + +for (i =0 ; i <MAX_ARGS ; i++) { + val =0 ; + args_err[idx][i] =bpf_get_func_arg (ctx, i, &val); + args_val[idx][i] = val; + } + + hits_cnt = idx +1 ; +return 0 ; +} + SEC("kprobe.session")int test_session_inline (struct pt_regs *ctx) {
Signature · 1 line
2.56.1
Jiri Olsa
reviewerRFC PATCH 30/31 selftests/bpf: Add kprobe multi inline bench attach test
In reply to Jiri OlsaAdd kernel_inline and modules_inline subtests to the kprobe_multi_bench_attach test that attach all attachable functions with BPF_F_KPROBE_MULTI_INLINE flag, so the link is attached also to all their inlined instances.
On top of the attach/detach times the test prints the number of attached and skipped inlined instances from the link's fdinfo.
--- .../bpf/prog_tests/kprobe_multi_test.c | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+)
tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c +50 -0
@@ -520,6 +520,7 @@ static void do_bench_test(struct kprobe_multi_empty *skel, struct bpf_kprobe_mullong detach_start_ns, detach_end_ns;double attach_delta, detach_delta;struct bpf_link *link =NULL ; +long inline_cnt =0 , inline_skipped =0 ; attach_start_ns =get_time_ns (); link = bpf_program__attach_kprobe_multi_opts(skel->progs.test_kprobe_empty,
@@ -529,6 +530,11 @@ static void do_bench_test(struct kprobe_multi_empty *skel, struct bpf_kprobe_mulif (!ASSERT_OK_PTR (link,"bpf_program__attach_kprobe_multi_opts" ))return ; +if (opts->inlined) { + inline_cnt =get_fdinfo_cnt (bpf_link__fd (link),"kprobe_inline_cnt" ); + inline_skipped =get_fdinfo_cnt (bpf_link__fd (link),"kprobe_inline_skipped" ); + } + detach_start_ns =get_time_ns ();bpf_link__destroy (link); detach_end_ns =get_time_ns ();
@@ -537,6 +543,10 @@ static void do_bench_test(struct kprobe_multi_empty *skel, struct bpf_kprobe_mul detach_delta = (detach_end_ns - detach_start_ns) /1000000000.0 ;printf ("%s: found %lu functions\n" , __func__, opts->cnt); +if (opts->inlined) { +printf ("%s: attached %ld inlined instances, skipped %ld\n" , __func__, + inline_cnt, inline_skipped); + }printf ("%s: attached in %7.3lfs\n" , __func__, attach_delta);printf ("%s: detached in %7.3lfs\n" , __func__, detach_delta); }
@@ -574,6 +584,42 @@ static bool inline_attach_supported(void)#endif } +/* Attach all attachable functions together with their inlined instances. */ +static void test_kprobe_multi_bench_attach_inline (bool kernel) +{ +LIBBPF_OPTS (bpf_kprobe_multi_opts, opts); +struct kprobe_multi_empty *skel =NULL ; +struct ksyms *ksyms =NULL ; + +if (!inline_attach_supported ()) { +test__skip (); +return ; + } + +/* pahole may not have inline BTF feature support. */ +if (access ("/sys/kernel/btf/vmlinux.inline" ,F_OK )) { +test__skip (); +return ; + } + +if (!ASSERT_OK (bpf_get_ksyms (&ksyms, kernel),"bpf_get_ksyms" )) +return ; + + skel =kprobe_multi_empty__open_and_load (); +if (!ASSERT_OK_PTR (skel,"kprobe_multi_empty__open_and_load" )) + goto cleanup; + + opts.syms = (const char **)ksyms->filtered_syms; + opts.cnt = ksyms->filtered_cnt; + opts.inlined = true; + +do_bench_test (skel, &opts); + +cleanup: +kprobe_multi_empty__destroy (skel); +free_kallsyms_local (ksyms); +} +static void test_kprobe_multi_bench_attach_addr (bool kernel) {LIBBPF_OPTS (bpf_kprobe_multi_opts, opts);
@@ -1625,6 +1671,10 @@ void serial_test_kprobe_multi_bench_attach(void)test_kprobe_multi_bench_attach_addr (true);if (test__start_subtest ("modules" ))test_kprobe_multi_bench_attach_addr (false); +if (test__start_subtest ("kernel_inline" )) +test_kprobe_multi_bench_attach_inline (true); +if (test__start_subtest ("modules_inline" )) +test_kprobe_multi_bench_attach_inline (false); }void test_kprobe_multi_test (void )
Signature · 1 line
2.56.1
Jiri Olsa
reviewerRFC PATCH 31/31 selftests/bpf: Add kprobe multi inline link info test
In reply to Jiri OlsaAdding fill_link_info tests for kprobe multi link with inlined instances attached:
- kprobe_multi_inline_link_info checks the addresses, cookies and BTF ids of the inlined instances and the number of attached and skipped instances
- kprobe_multi_inline_invalid_ubuff checks the errors returned for wrong setup of the inline link info fields
--- .../selftests/bpf/prog_tests/fill_link_info.c | 251 ++++++++++++++++++ 1 file changed, 251 insertions(+)
tools/testing/selftests/bpf/prog_tests/fill_link_info.c +251 -0
@@ -413,6 +413,253 @@ static void test_kprobe_multi_fill_link_info(struct test_fill_link_info *skel,bpf_link__destroy (link); } +/* +* The first function is attached on its entry, the rest through inlined +* instances: outer is inlined in one caller, inner in two and partial has +* one instance that is attached and one that can't be attached and is +* skipped. +*/ +static const char *kmulti_inline_syms[] = { +"bpf_fentry_test1" , +"bpf_kprobe_multi_inline_outer" , +"bpf_kprobe_multi_inline_inner" , +"bpf_kprobe_multi_inline_partial" , +}; +static __u64 kmulti_inline_cookies[] = {1 ,2 ,3 ,4 }; +static int kmulti_inline_cnts[] = {0 ,1 ,2 ,1 }; +#define KMULTI_INLINE_CNT 4 + +/* +* Check the inline.text LOCSEC of @btf has inlined instance of function +* with BTF id @id on address @addr. +*/ +static bool kmulti_inline_has_loc (const struct btf *btf,__u32 id,__u64 addr) +{ +/* LOCSEC offsets are relative to the .text section base. */ +__u64 base =ksym_get_addr ("_stext" ); +const struct btf_type *t; +struct btf_loc *loc; +int i, n, locsec; + + locsec =btf__find_by_name_kind (btf,"inline.text" ,BTF_KIND_LOCSEC ); +if (!ASSERT_GT (locsec,0 ,"inline_locsec" )) +return false; + + t =btf__type_by_id (btf, locsec); + n =btf_vlen (t); +for (i =0 , loc =btf_locsec_locs (t); i < n; i++, loc++) { +if (loc->func == id && base + loc->offset == addr) +return true; + } +return false; +} + +static int verify_kmulti_inline_link_info (int fd,const struct btf *btf) +{ +__u64 inline_addrs[KMULTI_INLINE_CNT ], inline_cookies[KMULTI_INLINE_CNT ]; +int cnts[ARRAY_SIZE (kmulti_inline_syms)] = {}, i, j, err; +__u32 inline_ids[KMULTI_INLINE_CNT ]; +const struct btf_type *t; +struct bpf_link_info info; +__u32 len =sizeof (info); +__u64 addr =0 , cookie =0 ; +struct ksym *ksym; +const char *name; + +memset (&info,0 ,sizeof (info)); + +again: + err =bpf_link_get_info_by_fd (fd, &info, &len); +if (!ASSERT_OK (err,"get_link_info" )) +return -1 ; + +if (!ASSERT_EQ (info.type,BPF_LINK_TYPE_KPROBE_MULTI ,"kmulti_type" )) +return -1 ; + +ASSERT_EQ (info.kprobe_multi.flags,BPF_F_KPROBE_MULTI_INLINE ,"kmulti_flags" ); + +/* Function with entry probe is not counted in inlined instances. */ +if (!ASSERT_EQ (info.kprobe_multi.count,1 ,"func_cnt" ) || + !ASSERT_EQ (info.kprobe_multi.inline_count,KMULTI_INLINE_CNT ,"inline_cnt" )) +return -1 ; + +/* Instances that could not be attached are counted as skipped. */ +ASSERT_EQ (info.kprobe_multi.inline_skipped,1 ,"inline_skipped" ); + +if (!info.kprobe_multi.addrs) { + info.kprobe_multi.addrs =ptr_to_u64 (&addr); + info.kprobe_multi.cookies =ptr_to_u64 (&cookie); + info.kprobe_multi.inline_addrs =ptr_to_u64 (inline_addrs); + info.kprobe_multi.inline_cookies =ptr_to_u64 (inline_cookies); + info.kprobe_multi.inline_ids =ptr_to_u64 (inline_ids); + goto again; + } + +ASSERT_EQ (addr,ksym_get_addr (kmulti_inline_syms[0 ]),"kmulti_addr" ); +ASSERT_EQ (cookie, kmulti_inline_cookies[0 ],"kmulti_cookie" ); + +for (i =0 ; i <KMULTI_INLINE_CNT ; i++) { +/* The id is BTF id of the inlined function .. */ + t =btf__type_by_id (btf, inline_ids[i]); +if (!ASSERT_OK_PTR (t,"inline_id" )) +continue ; + name =btf__name_by_offset (btf, t->name_off); + +/* .. the instance has cookie of that function .. */ +for (j =0 ; j <ARRAY_SIZE (kmulti_inline_syms); j++) { +if (!strcmp (name, kmulti_inline_syms[j])) +break ; + } +if (!ASSERT_LT (j,ARRAY_SIZE (kmulti_inline_syms),"inline_func" )) +continue ; +ASSERT_EQ (inline_cookies[i], kmulti_inline_cookies[j],"inline_cookie" ); + cnts[j]++; + +/* .. and its address is the one from inline BTF, inside the caller. */ +ASSERT_TRUE (kmulti_inline_has_loc (btf, inline_ids[i], inline_addrs[i]), +"inline_addr" ); + ksym =ksym_search (inline_addrs[i]); +if (!ASSERT_OK_PTR (ksym,"ksym_search" )) +continue ; +ASSERT_OK (strncmp (ksym->name,"bpf_kprobe_multi_inline_test" ,28 ),"inline_caller" ); + } + +for (j =0 ; j <ARRAY_SIZE (kmulti_inline_syms); j++) +ASSERT_EQ (cnts[j], kmulti_inline_cnts[j],"inline_func_cnt" ); +return 0 ; +} + +static void verify_kmulti_inline_invalid_user_buffer (int fd) +{ +__u64 addrs[KMULTI_INLINE_CNT +1 ], cookies[KMULTI_INLINE_CNT ]; +__u32 ids[KMULTI_INLINE_CNT ]; +struct bpf_link_info info; +__u32 len =sizeof (info); +int err, i; + +/* Wrong info setup (inline_addrs == NULL and inline_count != 0) -> EINVAL */ +memset (&info,0 ,sizeof (info)); + info.kprobe_multi.inline_count =KMULTI_INLINE_CNT ; + err =bpf_link_get_info_by_fd (fd, &info, &len); +ASSERT_EQ (err, -EINVAL ,"no_addr" ); + +/* Wrong info setup (inline_addrs != NULL and inline_count == 0) -> EINVAL */ +memset (&info,0 ,sizeof (info)); + info.kprobe_multi.inline_addrs =ptr_to_u64 (addrs); + err =bpf_link_get_info_by_fd (fd, &info, &len); +ASSERT_EQ (err, -EINVAL ,"no_cnt" ); + +/* Wrong info setup (inline_cookies != NULL and inline_count == 0) -> EINVAL */ +memset (&info,0 ,sizeof (info)); + info.kprobe_multi.inline_cookies =ptr_to_u64 (cookies); + err =bpf_link_get_info_by_fd (fd, &info, &len); +ASSERT_EQ (err, -EINVAL ,"cookies_no_cnt" ); + +/* Wrong info setup (inline_ids != NULL and inline_count == 0) -> EINVAL */ +memset (&info,0 ,sizeof (info)); + info.kprobe_multi.inline_ids =ptr_to_u64 (ids); + err =bpf_link_get_info_by_fd (fd, &info, &len); +ASSERT_EQ (err, -EINVAL ,"ids_no_cnt" ); + +/* Smaller than actual count provided -> ENOSPC */ +memset (&info,0 ,sizeof (info)); +memset (addrs,0 ,sizeof (addrs)); + info.kprobe_multi.inline_addrs =ptr_to_u64 (addrs); + info.kprobe_multi.inline_count =KMULTI_INLINE_CNT -1 ; + err =bpf_link_get_info_by_fd (fd, &info, &len); +ASSERT_EQ (err, -ENOSPC ,"smaller_cnt" ); +for (i =0 ; i <KMULTI_INLINE_CNT -1 ; i++) +ASSERT_NEQ (addrs[i],0 ,"inline_addrs" ); +/* check that the last entry is not populated */ +ASSERT_EQ (addrs[i],0 ,"inline_addrs" ); + +/* Bigger than actual count provided -> OK */ +memset (&info,0 ,sizeof (info)); +memset (addrs,0 ,sizeof (addrs)); + info.kprobe_multi.inline_addrs =ptr_to_u64 (addrs); + info.kprobe_multi.inline_count =KMULTI_INLINE_CNT +1 ; + err =bpf_link_get_info_by_fd (fd, &info, &len); +ASSERT_EQ (err,0 ,"bigger_cnt" ); +ASSERT_EQ (info.kprobe_multi.inline_count,KMULTI_INLINE_CNT ,"bigger_cnt_inline_cnt" ); +for (i =0 ; i <KMULTI_INLINE_CNT ; i++) +ASSERT_NEQ (addrs[i],0 ,"inline_addrs" ); +ASSERT_EQ (addrs[i],0 ,"inline_addrs" ); + +/* Invalid inline_addrs pointer -> EFAULT */ +memset (&info,0 ,sizeof (info)); + info.kprobe_multi.inline_addrs =0x1 ; + info.kprobe_multi.inline_count =KMULTI_INLINE_CNT ; + err =bpf_link_get_info_by_fd (fd, &info, &len); +ASSERT_EQ (err, -EFAULT ,"invalid_buff_addrs" ); + +/* Invalid inline_cookies pointer -> EFAULT */ +memset (&info,0 ,sizeof (info)); + info.kprobe_multi.inline_addrs =ptr_to_u64 (addrs); + info.kprobe_multi.inline_cookies =0x1 ; + info.kprobe_multi.inline_count =KMULTI_INLINE_CNT ; + err =bpf_link_get_info_by_fd (fd, &info, &len); +ASSERT_EQ (err, -EFAULT ,"invalid_buff_cookies" ); + +/* Invalid inline_ids pointer -> EFAULT */ +memset (&info,0 ,sizeof (info)); + info.kprobe_multi.inline_addrs =ptr_to_u64 (addrs); + info.kprobe_multi.inline_ids =0x1 ; + info.kprobe_multi.inline_count =KMULTI_INLINE_CNT ; + err =bpf_link_get_info_by_fd (fd, &info, &len); +ASSERT_EQ (err, -EFAULT ,"invalid_buff_ids" ); +} + +static void test_kprobe_multi_inline_fill_link_info (struct test_fill_link_info *skel, +bool invalid) +{ +LIBBPF_OPTS (bpf_kprobe_multi_opts, opts); +struct btf *vmlinux_btf, *inline_btf =NULL ; +struct bpf_link *link; +int link_fd, err; + +/* Attaching to inlined functions is supported only on x86_64. */ +#ifndef __x86_64__ +test__skip (); +return ; +#endif + + vmlinux_btf =btf__load_vmlinux_btf (); +if (!ASSERT_OK_PTR (vmlinux_btf,"vmlinux_btf" )) +return ; + + inline_btf =btf__parse_split ("/sys/kernel/btf/vmlinux.inline" , vmlinux_btf); + err =libbpf_get_error (inline_btf); +if (err) { + inline_btf =NULL ; +/* pahole may not have inline BTF feature support. */ +if (err == -ENOENT ) +test__skip (); +else +ASSERT_OK (err,"parse_inline_btf" ); + goto out; + } + + opts.syms = kmulti_inline_syms; + opts.cookies = kmulti_inline_cookies; + opts.cnt =ARRAY_SIZE (kmulti_inline_syms); + opts.inlined = true; + link =bpf_program__attach_kprobe_multi_opts (skel->progs.kmulti_run,NULL , &opts); +if (!ASSERT_OK_PTR (link,"attach_kprobe_multi_inline" )) + goto out; + + link_fd =bpf_link__fd (link); +if (!invalid) { + err =verify_kmulti_inline_link_info (link_fd, inline_btf); +ASSERT_OK (err,"verify_kmulti_inline_link_info" ); + }else { +verify_kmulti_inline_invalid_user_buffer (link_fd); + } +bpf_link__destroy (link); +out: +btf__free (inline_btf); +btf__free (vmlinux_btf); +} +static int tmulti_target_cmp (const void *a,const void *b) {const struct tmulti_target *ta = a;
@@ -874,6 +1121,10 @@ void test_fill_link_info(void) }if (test__start_subtest ("kprobe_multi_invalid_ubuff" ))test_kprobe_multi_fill_link_info (skel, true, true, true); +if (test__start_subtest ("kprobe_multi_inline_link_info" )) +test_kprobe_multi_inline_fill_link_info (skel, false); +if (test__start_subtest ("kprobe_multi_inline_invalid_ubuff" )) +test_kprobe_multi_inline_fill_link_info (skel, true);if (test__start_subtest ("tracing_multi_link_info" )) {test_tracing_multi_fill_link_info (skel, false, false);
Signature · 1 line
2.56.1
[email protected]
AI reviewRe: [PATCH RFC bpf-next 23/31] selftests/bpf: Add kprobe multi inline attach test
In reply to Jiri OlsaThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] selftests/bpf: premature test__skip() in test_inline_subtests() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] selftests/bpf: premature test__skip() in test_inline_subtests() --
commit 3b66f425ce4402b3110a2ac73ffbb4d519bd10dd
Author: Jiri Olsa <[email protected]>
selftests/bpf: Add kprobe multi inline attach test
This commit adds the inline_attach subtest to kprobe_multi_test to verify attaching to inlined functions with BPF_F_KPROBE_MULTI_INLINE, checking hit counts and function cookies before and after detach.
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c +0
-0
[ ... ]
Quoted 1 line
tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c +0
-0@@ -703,6 +714,207 @@ static void test_attach_probe_dup_sym(void)
[ ... ]
Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c +13
-0+/* Run all the tests for attaching to inlined functions. */ +static void test_inline_subtests (void ) +{ +if (!inline_attach_supported ()) { +test__skip (); +return ; + } + +if (test__start_subtest ("inline_attach" )) +test_inline_attach (); + +free_inline_btf (); +}
[ ... ]
tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c void test_kprobe_multi_test(void) · line 965
Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c +1
-0@@ -748,5 +960,6 @@ void test_kprobe_multi_test(void)test_attach_write_ctx ();if (test__start_subtest ("dup_sym" ))test_attach_probe_dup_sym (); +test_inline_subtests ();RUN_TESTS (kprobe_multi_verifier); }
[Severity: Low]
Does calling test__skip() before test__start_subtest("inline_attach") on non-x86_64 architectures mark the preceding dup_sym subtest as skipped?
test__start_subtest() only finalizes the previous subtest when the next subtest starts, so env.subtest_state still points to dup_sym when test_inline_subtests() runs, causing test__skip() to set dup_sym's skipped flag while never registering inline_attach. Checking inline_attach_supported() inside test_inline_attach() (after test__start_subtest("inline_attach")) would skip the intended subtest.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=23
[email protected]
AI reviewRe: [PATCH RFC bpf-next 18/31] bpf: Add inlined functions for kprobe multi inline tests
In reply to Jiri OlsaThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] bpf: raw asm nop in test_run.c breaks build on archs without nop mnemonic --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] bpf: raw asm nop in test_run.c breaks build on archs without nop mnemonic
--
commit 5d987286743a14e6df613a09586a18d8317f8a98
Author: Jiri Olsa <[email protected]>
bpf: Add inlined functions for kprobe multi inline tests
This commit adds always-inlined helper functions and their callers in net/bpf/test_run.c for kprobe multi inline testing via BPF tracing test_run.
Quoted 4 lines
net/bpf/test_run.c +0
-0
[ ... ]
Quoted 19 lines
net/bpf/test_run.c +15
-0@@ -558,6 +559,81 @@ noinline int bpf_fentry_test10(const void *a)return (long )a; } +/* +* Inlined functions for kprobe multi inline tests, the outer function +* starts with the inner one, so their inlined instances in +* bpf_kprobe_multi_inline_test1 share the address. +*/ +static __always_inline int bpf_kprobe_multi_inline_inner(int a) +{ +/* +* Keep an instruction in the inline site, there's no argument +* setup, so it's the first instruction of the outer function +* inline site as well. +*/ + asmvolatile ("nop" ); +return a +1 ; +}
[ ... ]
Quoted 11 lines
net/bpf/test_run.c +11
-0+static __always_inline int bpf_kprobe_multi_inline_partial(int a) +{ + asmvolatile ("nop" ); +return a +1 ; +} + +static __always_inline int bpf_kprobe_multi_inline_nokprobe(int a) +{ + asmvolatile ("nop" ); +return a +2 ; +}
[ ... ]
net/bpf/test_run.c noinline int bpf_fentry_test10(const void *a) · line 630
Quoted 6 lines
net/bpf/test_run.c +6
-0+/* Inlined function with arguments for kprobe multi inline args test. */ +static __always_inline long bpf_kprobe_multi_inline_args (int a,long b,void *c) +{ + asmvolatile ("nop" ); +return a + b + (long )c; +}
[Severity: Medium]
Will raw asm volatile("nop") break the build on architectures whose assembler does not have a bare "nop" mnemonic?
net/bpf/test_run.c is compiled on all architectures when CONFIG_BPF_SYSCALL=y, and OpenRISC uses "l.nop" and rejects bare "nop" as an unrecognized instruction. Using the nop() macro from <asm/barrier.h> keeps this portable across architectures.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=18
[email protected]
AI reviewRe: [PATCH RFC bpf-next 16/31] bpf: Add get_func_arg[_cnt] support for kprobe multi inlined functions
In reply to Jiri OlsaThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] bpf: incorrect BTF_LOC_PARAM_SIGNED handling in inline_site_arg() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] bpf: incorrect BTF_LOC_PARAM_SIGNED handling in inline_site_arg()
--
commit 0b0ce9f66acdc81e725db0ce2dff47e37a273076
Author: Jiri Olsa <[email protected]>
bpf: Add get_func_arg[_cnt] support for kprobe multi inlined functions
This commit adds bpf_get_func_arg() and bpf_get_func_arg_cnt() support for inlined function instances attached with BPF_F_KPROBE_MULTI_INLINE by storing each instance's inline BTF location prototype and reading argument values according to BTF_KIND_LOC_PARAM.
Quoted 4 lines
kernel/trace/bpf_trace.c +0
-0
[ ... ]
Quoted 11 lines
kernel/trace/bpf_trace.c +7
-0@@ -2733,6 +2736,109 @@ static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx)return run_ctx->entry_ip; } +/* +* Get @n-th argument of the inlined instance @site from its location +* described in inline BTF, @regs are registers of the probed instruction. +*/ +static int inline_site_arg (const struct inline_site *site,struct pt_regs *regs, +u32 n,u64 *value) +{
[ ... ]
Quoted 16 lines
kernel/trace/bpf_trace.c +16
-0+if (flags &BTF_LOC_PARAM_CONST ) { + val = p->values[0 ]; +if (vlen >1 ) + val |= (u64 )p->values[1 ] <<32 ; +if (flags &BTF_LOC_PARAM_ADDR ) { +/* The address is relative to the kernel image start. */ +if (!site->is_vmlinux) +return -EOPNOTSUPP ; + val += (unsigned long )_text; + }else if (flags &BTF_LOC_PARAM_SIGNED ) { +/* Sign extend the constant from the argument size. */ + val =sign_extend64 (val, t->size *8 -1 ); + } + *value = val; +return 0 ; + }
[ ... ]
kernel/trace/bpf_trace.c static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx) · line 2823
Quoted 10 lines
kernel/trace/bpf_trace.c +10
-0+/* +* The offset follows the register number, it's either signed +* 32-bit value or 64-bit value in {lo, hi} order. +*/ +if (flags &BTF_LOC_PARAM_OFFSET ) { +if (vlen >2 ) + val += (u64 )p->values[2 ] <<32 | p->values[1 ]; +else + val += (s32 )p->values[1 ]; + }
[Severity: Medium]
Does inline_site_arg() mishandle BTF_LOC_PARAM_SIGNED for 32-bit register offsets and signed address constants?
For BTF_LOC_PARAM_OFFSET with vlen == 2, btf_loc_param_check_meta() allows both signed and unsigned 1-word offsets, but p->values[1] is unconditionally cast to s32 without checking BTF_LOC_PARAM_SIGNED. An unsigned 32-bit offset in 0x80000000..0xffffffff will be sign-extended into a negative 64-bit offset, returning a corrupted argument value or dereferencing the wrong address when BTF_LOC_PARAM_DEREF is set.
Also, in the BTF_LOC_PARAM_CONST block above, btf_loc_param_check_meta() permits BTF_LOC_PARAM_CONST | BTF_LOC_PARAM_ADDR | BTF_LOC_PARAM_SIGNED, but the else-if branch skips sign extension before adding _text when BTF_LOC_PARAM_ADDR is set on a 32-bit value.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=16
[email protected]
AI reviewRe: [PATCH RFC bpf-next 21/31] bpftool: Display inlined functions for kprobe multi link
In reply to Jiri OlsaThank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] bpftool: broken binary search in find_kernel_sym_by_range() due to 32-bit sort - [Low] bpftool…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] bpftool: broken binary search in find_kernel_sym_by_range() due to 32-bit sort
- [Low] bpftool: memory leak in do_show_link() when second link info query fails
--
commit 40ced4c64011e2e50d8f3a0de0d6ac7287df545e
Author: Jiri Olsa <[email protected]>
bpftool: Display inlined functions for kprobe multi link
This commit updates bpftool to display inline flags and attached inlined function instances for kprobe_multi links in both plain and JSON output.
Quoted 4 lines
tools/bpf/bpftool/link.c +0
-0
[ ... ]
Quoted 11 lines
tools/bpf/bpftool/link.c +7
-0@@ -322,33 +361,156 @@ symbol_matches_target(__u64 sym_addr, __u64 target_addr, bool is_ibt_enabled)return false; } +/* Inlined instances are placed inside functions, find the one covering @addr. */ +static struct kernel_sym *find_kernel_sym_by_range (__u64 addr) +{ +__u32 lo =0 , hi = dd.sym_count, mid; + +if (!addr) +return NULL ;
tools/bpf/bpftool/link.c symbol_matches_target(__u64 sym_addr, __u64 target_addr, bool is_ibt_enabled) · line 380
Quoted 10 lines
tools/bpf/bpftool/link.c +10
-0+ +while (lo < hi) { + mid = lo + (hi - lo) /2 ; +if (dd.sym_mapping[mid].address <= addr) + lo = mid +1 ; +else + hi = mid; + } +return lo ? &dd.sym_mapping[lo -1 ] :NULL ; +}
[Severity: Medium]
Can find_kernel_sym_by_range() return a per-cpu symbol instead of the enclosing function because dd.sym_mapping is not sorted in 64-bit unsigned order?
kernel_syms_load() sorts dd.sym_mapping with kernel_syms_cmp(), which truncates the 64-bit unsigned long address difference to a 32-bit signed int:
tools/bpf/bpftool/xlated_dumper.c:kernel_syms_cmp() {
return ((struct kernel_sym *)sym_a)->address -
((struct kernel_sym *)sym_b)->address;
}On 64-bit SMP systems such as x86_64, per-cpu symbols in /proc/kallsyms have small positive offsets (e.g. 0x1000), while kernel and module text symbols sit at 0xffffffff80000000 and above, where bit 31 is set. Because truncating (per_cpu_addr - text_addr) to int yields a positive value, qsort() places the per-cpu symbols at the tail of dd.sym_mapping after text symbols. When find_kernel_sym_by_range() binary-searches dd.sym_mapping with 64-bit unsigned comparisons for an inlined address in a module or in the upper half of the table, mid probes into those trailing per-cpu entries, sees dd.sym_mapping[mid].address <= addr, and advances lo to the end of the array, reporting a bogus caller, offset, module, and inline BTF lookup.
[ ... ]
Quoted 40 lines
tools/bpf/bpftool/link.c +19
-13@@ -1172,24 +1414,36 @@ static int do_show_link(int fd) goto again; }if (info.type ==BPF_LINK_TYPE_KPROBE_MULTI && - !info.kprobe_multi.addrs) { + !info.kprobe_multi.addrs && !info.kprobe_multi.inline_addrs) { +__u32 inline_count = info.kprobe_multi.inline_count; + count = info.kprobe_multi.count;if (count) { addrs =calloc (count,sizeof (__u64)); -if (!addrs) { -p_err ("mem alloc failed" ); -close (fd); -return -ENOMEM ; - } - info.kprobe_multi.addrs =ptr_to_u64 (addrs); cookies =calloc (count,sizeof (__u64)); -if (!cookies) { -p_err ("mem alloc failed" ); -free (addrs); -close (fd); -return -ENOMEM ; - } + } +if (inline_count) { + inline_addrs =calloc (inline_count,sizeof (__u64)); + inline_cookies =calloc (inline_count,sizeof (__u64)); + inline_ids =calloc (inline_count,sizeof (__u32)); + } +if ((count && (!addrs || !cookies)) || + (inline_count && (!inline_addrs || !inline_cookies || !inline_ids))) { +p_err ("mem alloc failed" ); +free (inline_ids); +free (inline_cookies); +free (inline_addrs); +free (cookies); +free (addrs); +close (fd); +return -ENOMEM ;
tools/bpf/bpftool/link.c static int do_show_link(int fd) · line 1449
Quoted 10 lines
tools/bpf/bpftool/link.c +6
-0+ } +if (count || inline_count) { + info.kprobe_multi.addrs =ptr_to_u64 (addrs); info.kprobe_multi.cookies =ptr_to_u64 (cookies); + info.kprobe_multi.inline_addrs =ptr_to_u64 (inline_addrs); + info.kprobe_multi.inline_cookies =ptr_to_u64 (inline_cookies); + info.kprobe_multi.inline_ids =ptr_to_u64 (inline_ids); goto again; } }
[Severity: Low]
If the second bpf_link_get_info_by_fd() call after "goto again" fails, does do_show_link() return early without freeing inline_addrs, inline_cookies, inline_ids, addrs, and cookies?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=21
[email protected]
AI reviewRe: [PATCH RFC bpf-next 15/31] bpf: Add get_func_arg[_cnt] support for kprobe multi functions
In reply to Jiri OlsaThank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] bpf: missing call_get_func_arg propagation for tail calls and freplace - [Medium] bpf: unscoped…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] bpf: missing call_get_func_arg propagation for tail calls and freplace
- [Medium] bpf: unscoped bpf_find_btf_id() lookup in kprobe_multi_args_cnt()
--
commit 70cd82b71680ac2bf4eba159cfcb717936c5ada2
Author: Jiri Olsa <[email protected]>
bpf: Add get_func_arg[_cnt] support for kprobe multi functions
This commit adds bpf_get_func_arg() and bpf_get_func_arg_cnt() helper support for kprobe multi programs by looking up each attached function's argument count in BTF at attach time and reading argument values from ftrace_regs at runtime.
Quoted 5 lines
kernel/bpf/verifier.c +0
-0
kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · line 13487
Quoted 9 lines
kernel/bpf/verifier.c +3
-0@@ -13479,6 +13479,9 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn env->prog->call_get_func_ip = true; } +if (func_id == BPF_FUNC_get_func_arg || func_id == BPF_FUNC_get_func_arg_cnt) + env->prog->call_get_func_arg = true; +/* a call that only a speculative state reaches is dead code */ if (func_id == BPF_FUNC_override_return && !env->cur_state->speculative) env->prog->kprobe_override =1 ;
[Severity: Medium]
Should call_get_func_arg also be set when a program uses BPF_FUNC_tail_call (and handled for BPF_PROG_TYPE_EXT freplace attachments)?
In bpf_do_misc_fixups(), BPF_FUNC_tail_call conservatively sets prog->call_get_func_ip and prog->call_session_cookie on the root program, because a tail-called program can invoke those helpers at runtime:
kernel/bpf/fixups.c:bpf_do_misc_fixups() {
...
prog->cb_access = 1;
prog->call_get_func_ip = 1;
if (prog_type == BPF_PROG_TYPE_TRACING &&
(eatype == BPF_TRACE_FSESSION || eatype == BPF_TRACE_FSESSION_MULTI))
prog->call_session_cookie = 1;
...
}Without setting prog->call_get_func_arg there as well, attaching a root kprobe_multi program that tail-calls (or is extended via freplace by) a program calling bpf_get_func_arg() or bpf_get_func_arg_cnt() leaves link->args_cnt NULL in bpf_kprobe_multi_link_attach(), so those helpers fail with -EOPNOTSUPP or return 0 at runtime.
Quoted 4 lines
kernel/trace/bpf_trace.c +0
-0
[ ... ]
Quoted 36 lines
kernel/trace/bpf_trace.c +32
-0@@ -3068,6 +3157,49 @@ static int inline_lookup_modules(struct inline_lookup *lookup) }#endif +/* Get number of arguments of the function from its BTF, zero if there's none. */ +static u8 func_args_cnt (const char *name) +{ +const struct btf_type *t; +struct btf *btf; +u32 cnt =0 ; +s32 id; + + id =bpf_find_btf_id (name,BTF_KIND_FUNC , &btf); +if (id <0 ) +return 0 ; + t =btf_type_by_id (btf,btf_type_by_id (btf, id)->type); +if (btf_type_is_func_proto (t)) + cnt =btf_type_vlen (t); +/* Do not count variable arguments. */ +if (cnt && !btf_params (t)[cnt -1 ].type) + cnt--; +btf_put (btf); +return min (cnt,U8_MAX ); +} + +/* +* Get number of arguments for @cnt functions in @addrs, the function +* name is taken from @syms if it's provided, otherwise it's resolved +* from the function address. +*/ +static void kprobe_multi_args_cnt (u8 *args_cnt,const char **syms, +unsigned long *addrs,u32 cnt) +{ +char buf[KSYM_NAME_LEN ]; +const char *name; +u32 i;
kernel/trace/bpf_trace.c static int inline_lookup_modules(struct inline_lookup *lookup) · line 3201
Quoted 10 lines
kernel/trace/bpf_trace.c +10
-0+ +for (i =0 ; i < cnt; i++) { +/* Function that has only inlined instances. */ +if (!addrs[i]) +continue ; + name = syms ? syms[i] :kallsyms_lookup (addrs[i],NULL ,NULL ,NULL , buf); +if (name) + args_cnt[i] =func_args_cnt (name); + } +}
[Severity: Medium]
Can func_args_cnt() look up BTF by bare symbol name across all BTF objects instead of scoping the lookup to the BTF owning addrs[i]?
Because bpf_find_btf_id() searches vmlinux BTF first and then walks every module BTF in btf_idr, a traced module function that shares a name with a static function in vmlinux or an earlier module resolves against the wrong BTF_KIND_FUNC entry and records the wrong argument count in args_cnt[i].
In addition, for every attached address not present in vmlinux BTF (such as module symbols or compiler clones like .isra.0 and .constprop.0), bpf_find_btf_id() cycles spin_lock_bh(&btf_idr_lock) across all loaded module BTFs, and each btf_find_by_name_kind(mod_btf, ...) call recursively re-searches base_btf (vmlinux BTF) before checking the module BTF. Looking up addrs[i]'s owning module (or vmlinux when it is a core kernel address) and searching only that BTF object would avoid both the wrong-symbol match and the O(N * M) module BTF scan during attach.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=15
Re: [PATCH RFC bpf-next 05/31] bpf: Add bpf_get_btf_vmlinux_inline function
In reply to Jiri Olsakernel/bpf/btf.c static int __init btf_module_init(void) · line 9720
Quoted 3 lines · Jiri Olsa
kernel/bpf/btf.c +3
-0+ env->log.level =BPF_LOG_KERNEL ; + btf =btf_parse_split (env,"vmlinux.inline" , data, + data_size, vmlinux_btf);
btf_parse_split() does kvmemdup().
That's a 2nd copy of vmlinux.inline and it's never freed. It was 9.2 Mbytes in Alan's numbers.
The original stays in memory for both =y and =m. can we parse it in place like btf_parse_base() does ?
Re: [PATCH RFC bpf-next 07/31] bpf: Add support to attach inlined functions with kprobe multi link
In reply to Jiri Olsakernel/trace/bpf_trace.c static int addrs_check_error_injection_list(unsigned long *addrs, u32 cnt) · line 2978
Quoted 4 lines · Jiri Olsa
kernel/trace/bpf_trace.c +4
-0+if (!kallsyms_lookup_size_offset (addr, &size, &offset)) +return false; + addr -= offset; +return ftrace_location_range (addr, addr + size -1 );
gcc moves unlikely paths of foo() into foo.cold, which has no fentry call. An instance in foo.cold is skipped, though foo() is traceable. With the strict flag from patch 14 it fails the attach. within_notrace_func() handles this case since
commit c7411a1a126f ("tracing/kprobe: Check whether the non-suffixed symbol is notrace").
can we reuse it ?
Re: [PATCH RFC bpf-next 15/31] bpf: Add get_func_arg[_cnt] support for kprobe multi functions
In reply to Jiri Olsakernel/trace/bpf_trace.c static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx) · line 2774
Quoted 6 lines · Jiri Olsa
kernel/trace/bpf_trace.c +6
-0+ cnt =kprobe_multi_entry_args_cnt (run_ctx); +if (cnt <0 ) +return cnt; +if (n >= cnt) +return -EINVAL ; + *value =ftrace_regs_get_argument (run_ctx->fregs, n);
cnt is the number of args, but n selects a register.
They differ when a struct takes two registers. sockptr_t has 16 bytes:
sk_setsockopt(sk, level, optname, sockptr_t optval, optlen)
rdi rsi rdx rcx:r8 r9
n fentry entry, this patch inlined, patch 16 - ----------- ----------------- ----------------- 3 optval, rcx optval, rcx -EOPNOTSUPP 4 optval, r8 optval, r8 optlen 5 optlen -EINVAL -EINVAL
optlen can't be read at the entry.
The same prog gets a different arg for n == 4 at an inlined instance. Should n mean the same as in fentry?
Quoted 4 lines · Jiri Olsa
This patchset adds
BPF_F_KPROBE_MULTI_INLINEflag that attaches also the inlined instances of the functions in the syms array. The instances are resolved from inline BTF (CONFIG_DEBUG_INFO_BTF_INLINE) of vmlinux and modules and each gets its own kprobe.
I like the idea. kprobes behind the kprobe multi link is the piece that was missing.
But the lookup by name in the kernel is kinda heavy.
In Alan's RFC thread I leaned towards the kernel doing the whole thing. The reason: kprobe multi could not attach to arbitrary IPs. Patch 7 shows that it can. That changes the trade off.
Lets start with the mechanism only.
addrs[] takes any address that kprobes accept.
fprobe gets the ftrace locations. Every other address gets a kprobe. Such addrs fail with -ENOENT in fprobe_init() today, so no need for a flag.
User space reads vmlinux.inline from sysfs and passes the sites.
libbpf works this way already. It resolves "tcp_*" in user space and passes addrs. In this series wildcards can't use inline (patch 19). To match fully inlined functions libbpf has to read vmlinux.inline anyway.
this series addrs[]
----------------- ----------------------- -------------------------
name -> sites kernel, exact names user space, wildcards too
subset of sites no yes
vmlinux.inline 2nd copy, ~10 Mbytes, mmap in user space
never freed
cookie per function per site
args bpf_get_func_arg() cookie -> arg spec, like
usdt.bpf.h
arch x86-64 any with kprobes
link info 5 new fields as is
fdinfo 2 new lines as is, %pS
bpftool new table, BTF lookup print sym+off
new flags 2 0Your use case from Alan's v2 thread works too: trace foo() only where it is inlined in bar(). User space picks the sites.
Then there is no need for BTF parsing in the kernel, module_sect_addr(), regs_get_dwarf_register(), inline_* in bpf_link_info, STRICT, etc. Patches 2 and 3 stay.
If tracers show that the lookup has to be in the kernel we add it on top of addrs[]. uapi doesn't go the other way.
What the diff doesn't do:
It is all or none. imo that's the right default.
It doesn't say which address kprobes refuse. The link_create log can name it. A flag to skip such addresses can come later. link info shows what is attached.
It calls register_kprobes(). enable_kprobes() goes on top. Two functions inlined at one address share one cookie.
The kernel side is below. It compiles, I didn't attach anything with it:
kernel/trace/bpf_trace.c +115 -10
@@ -2360,6 +2360,16 @@ struct bpf_session_run_ctx { }; #ifdef CONFIG_FPROBE +struct bpf_kprobe_multi_link; + +/* For an address in link->addrs that is not an ftrace location. */ +struct bpf_kprobe_multi_kprobe { + struct kprobe kp; + struct bpf_kprobe_multi_link *link; + /* As in link->addrs. register_kprobe() can move kp.addr behind endbr. */ + unsigned long addr; +}; + struct bpf_kprobe_multi_link { struct bpf_link link; struct fprobe fp;
@@ -2368,6 +2378,9 @@ struct bpf_kprobe_multi_link {u32 cnt;u32 mods_cnt;struct module **mods; +struct bpf_kprobe_multi_kprobe *kprobes; +struct kprobe **kps; +u32 kps_cnt; };struct bpf_kprobe_multi_run_ctx {
@@ -2459,6 +2472,7 @@ static void bpf_kprobe_multi_link_release(struct bpf_link *link) kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link);/* Don't wait for RCU GP here. */ unregister_fprobe_async (&kmulti_link->fp); +unregister_kprobes (kmulti_link->kps, kmulti_link->kps_cnt);kprobe_multi_put_modules (kmulti_link->mods, kmulti_link->mods_cnt); }
@@ -2467,12 +2481,24 @@ static void bpf_kprobe_multi_link_dealloc(struct bpf_link *link)struct bpf_kprobe_multi_link *kmulti_link; kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link); +kvfree (kmulti_link->kprobes); +kvfree (kmulti_link->kps);kvfree (kmulti_link->addrs);kvfree (kmulti_link->cookies);kfree (kmulti_link->mods);kfree (kmulti_link); } +static unsigned long kprobe_multi_missed (const struct bpf_kprobe_multi_link *link) +{ +unsigned long missed = link->fp.nmissed; +u32 i; + +for (i =0 ; i < link->kps_cnt; i++) + missed += link->kprobes[i].kp.nmissed; +return missed; +} +static int bpf_kprobe_multi_link_fill_link_info (const struct bpf_link *link,struct bpf_link_info *info) {
@@ -2490,7 +2516,7 @@ static int bpf_kprobe_multi_link_fill_link_info(const struct bpf_link *link, kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link); info->kprobe_multi.count = kmulti_link->cnt; info->kprobe_multi.flags = kmulti_link->link.flags; - info->kprobe_multi.missed = kmulti_link->fp.nmissed; + info->kprobe_multi.missed =kprobe_multi_missed (kmulti_link);if (!uaddrs)return 0 ;
@@ -2537,7 +2563,7 @@ static void bpf_kprobe_multi_show_fdinfo(const struct bpf_link *link,"kprobe_cnt:\t%u\n" "missed:\t%lu\n" , kmulti_link->cnt, - kmulti_link->fp.nmissed); + kprobe_multi_missed(kmulti_link));seq_printf (seq,"%s\t %s\n" ,"cookie" ,"func" );for (int i =0 ; i < kmulti_link->cnt; i++) {
@@ -2621,7 +2647,7 @@ static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx) static __always_inline intkprobe_multi_link_prog_run (struct bpf_kprobe_multi_link *link,unsigned long entry_ip,struct ftrace_regs *fregs, -bool is_return,void *data) +struct pt_regs *regs,bool is_return,void *data) {struct bpf_kprobe_multi_run_ctx run_ctx = { .session_ctx = {
@@ -2632,11 +2658,10 @@ kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link *link, .entry_ip = entry_ip, };struct bpf_run_ctx *old_run_ctx; -struct pt_regs *regs;int err;/* -* graph tracer framework ensures we won't migrate, so there is no need +* fprobe and kprobe handlers don't migrate, so there is no need * to use migrate_disable for bpf_prog_run again. The check here just for * __this_cpu_inc_return. */
@@ -2649,11 +2674,14 @@ kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link *link, }rcu_read_lock (); - regs =ftrace_partial_regs (fregs,bpf_kprobe_multi_pt_regs_ptr ()); +/* fprobe has fregs, kprobe has regs */ +if (fregs) + regs =ftrace_partial_regs (fregs,bpf_kprobe_multi_pt_regs_ptr ()); old_run_ctx =bpf_set_run_ctx (&run_ctx.session_ctx.run_ctx); err =bpf_prog_run (link->link.prog, regs);bpf_reset_run_ctx (old_run_ctx); -ftrace_partial_regs_update (fregs,bpf_kprobe_multi_pt_regs_ptr ()); +if (fregs) +ftrace_partial_regs_update (fregs,bpf_kprobe_multi_pt_regs_ptr ());rcu_read_unlock (); out:
@@ -2671,7 +2699,7 @@ kprobe_multi_link_handler(struct fprobe *fp, unsigned long fentry_ip, link = container_of(fp, struct bpf_kprobe_multi_link, fp); err =kprobe_multi_link_prog_run (link,ftrace_get_entry_ip (fentry_ip), - fregs, false, data); + fregs,NULL , false, data);return is_kprobe_session (link->link.prog) ? err :0 ; }
@@ -2684,8 +2712,18 @@ kprobe_multi_link_exit_handler(struct fprobe *fp, unsigned long fentry_ip, link = container_of(fp, struct bpf_kprobe_multi_link, fp);kprobe_multi_link_prog_run (link,ftrace_get_entry_ip (fentry_ip), - fregs, true, data); + fregs,NULL , true, data); +} + +static int kprobe_multi_kprobe_handler (struct kprobe *kp,struct pt_regs *regs) +{ +struct bpf_kprobe_multi_kprobe *mkp; + + mkp = container_of(kp, struct bpf_kprobe_multi_kprobe, kp); +kprobe_multi_link_prog_run (mkp->link, mkp->addr,NULL , regs, false,NULL ); +return 0 ; } +NOKPROBE_SYMBOL (kprobe_multi_kprobe_handler);static int symbols_cmp_r (const void *a,const void *b,const void *priv) {
@@ -2804,6 +2842,73 @@ static int addrs_check_error_injection_list(unsigned long *addrs, u32 cnt)return 0 ; } +/* fprobe takes the ftrace locations. Every other address gets a kprobe. */ +static int kprobe_multi_register (struct bpf_kprobe_multi_link *link) +{ +u32 i, cnt = link->cnt, fp_cnt =0 , kp_cnt =0 ; +struct bpf_kprobe_multi_kprobe *mkp; +unsigned long *addrs; +int err =0 ; + + addrs =kvmalloc_objs (*addrs, cnt); +if (!addrs) +return -ENOMEM ; + +/* ftrace locations fill addrs from the start, the rest from the end. */ +for (i =0 ; i < cnt; i++) { +if (ftrace_location (link->addrs[i])) + addrs[fp_cnt++] = link->addrs[i]; +else + addrs[cnt - ++kp_cnt] = link->addrs[i]; + } + +if (kp_cnt) { +/* +* No return probe, session and override in the middle of +* a function. Fail the way fprobe_init() does today. +*/ +if (link->fp.exit_handler || link->link.prog->kprobe_override) { + err = -ENOENT ; + goto out; + } + +/* bpf_kprobe_multi_link_dealloc() frees both. */ + link->kprobes =kvcalloc (kp_cnt,sizeof (*link->kprobes),GFP_KERNEL ); + link->kps =kvmalloc_objs (*link->kps, kp_cnt); +if (!link->kprobes || !link->kps) { + err = -ENOMEM ; + goto out; + } + +for (i =0 ; i < kp_cnt; i++) { + mkp = &link->kprobes[i]; + mkp->link = link; + mkp->addr = addrs[fp_cnt + i]; + mkp->kp.addr = (kprobe_opcode_t *)mkp->addr; + mkp->kp.pre_handler = kprobe_multi_kprobe_handler; + link->kps[i] = &mkp->kp; + } + +/* All or none. */ + err =register_kprobes (link->kps, kp_cnt); +if (err) + goto out; + } + +if (fp_cnt) { + err =register_fprobe_ips (&link->fp, addrs, fp_cnt); +if (err) { +unregister_kprobes (link->kps, kp_cnt); + goto out; + } + } + + link->kps_cnt = kp_cnt; +out: +kvfree (addrs); +return err; +} +int bpf_kprobe_multi_link_attach (const union bpf_attr *attr,struct bpf_prog *prog) {struct bpf_kprobe_multi_link *link =NULL ;
@@ -2944,7 +3049,7 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr } link->mods_cnt = err; - err =register_fprobe_ips (&link->fp, addrs, cnt); + err =kprobe_multi_register (link);if (err) {kprobe_multi_put_modules (link->mods, link->mods_cnt);bpf_link_cleanup (&link_primer);
pw-bot: cr