Support kernel module function calls from eBPF
Subsystems (11)
Kumar Kartikeya Dwivedi
maintainerv4 cover Support kernel module function calls from eBPF
This set enables kernel module function calls, and also modifies verifier logic to permit invalid kernel function calls as long as they are pruned as part of dead code elimination. This is done to provide better runtime portability for BPF objects, which can conditionally disable parts of code that are pruned later by the verifier (e.g. const volatile vars, kconfig options). libbpf modifications are made along with kernel changes to support module function calls. The set includes gen_loader support for emitting kfunc relocations.
It also converts TCP congestion control objects to use the module kfunc support instead of relying on IS_BUILTIN ifdef.
Changelog:
----------
v3 -> v4
v3: https://lore.kernel.org/bpf/[email protected]
* Address comments from Alexei
* Drop MAX_BPF_STACK change, instead move map_fd and BTF fd to BPF array map and pass fd_array using BPF_PSEUDO_MAP_IDX_VALUE
* Address comments from Andrii
* Fix selftest to store to variable for observing function call instead of printk and polluting CI logs
* Drop use of raw_tp for testing, instead reuse classifier based prog_test_run
* Drop index + 1 based insn->off convention for kfunc module calls
* Expand selftests to cover more corner cases
* Misc cleanups
v2 -> v3
v2: https://lore.kernel.org/bpf/[email protected]
* Fix issues pointed out by Kernel Test Robot
* Fix find_kfunc_desc to also take offset into consideration when comparing
RFC v1 -> v2
v1: https://lore.kernel.org/bpf/[email protected]
* Address comments from Alexei
* Reuse fd_array instead of introducing kfunc_btf_fds array
* Take btf and module reference as needed, instead of preloading
* Add BTF_KIND_FUNC relocation support to gen_loader infrastructure
* Address comments from Andrii
* Drop hashmap in libbpf for finding index of existing BTF in fd_array
* Preserve invalid kfunc calls only when the symbol is weak
* Adjust verifier selftests
Kumar Kartikeya Dwivedi (11):
bpf: Introduce BPF support for kernel module function calls
bpf: Be conservative while processing invalid kfunc calls
bpf: btf: Introduce helpers for dynamic BTF set registration
tools: Allow specifying base BTF file in resolve_btfids
bpf: Enable TCP congestion control kfunc from modules
libbpf: Support kernel module function calls
libbpf: Resolve invalid weak kfunc calls with imm = 0, off = 0
libbpf: Update gen_loader to emit BTF_KIND_FUNC relocations
tools: bpftool: Add separate fd_array map support for light skeleton
libbpf: Fix skel_internal.h to set errno on loader retval < 0
bpf: selftests: Add selftests for module kfunc support
include/linux/bpf.h | 8 +- include/linux/bpf_verifier.h | 2 + include/linux/bpfptr.h | 1 + include/linux/btf.h | 37 +++ kernel/bpf/btf.c | 56 +++++ kernel/bpf/core.c | 4 + kernel/bpf/verifier.c | 220 ++++++++++++++--- net/bpf/test_run.c | 7 +- net/ipv4/bpf_tcp_ca.c | 36 +-- net/ipv4/tcp_bbr.c | 28 ++- net/ipv4/tcp_cubic.c | 26 +- net/ipv4/tcp_dctcp.c | 26 +- scripts/Makefile.modfinal | 1 + tools/bpf/bpftool/gen.c | 3 +- tools/bpf/bpftool/prog.c | 1 + tools/bpf/resolve_btfids/main.c | 19 +- tools/lib/bpf/bpf.c | 1 + tools/lib/bpf/bpf_gen_internal.h | 16 +- tools/lib/bpf/gen_loader.c | 222 +++++++++++++++--- tools/lib/bpf/libbpf.c | 83 +++++-- tools/lib/bpf/libbpf.h | 1 + tools/lib/bpf/libbpf_internal.h | 1 + tools/lib/bpf/skel_internal.h | 33 ++- tools/testing/selftests/bpf/Makefile | 5 +- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 26 +- .../selftests/bpf/prog_tests/ksyms_module.c | 52 ++-- .../bpf/prog_tests/ksyms_module_libbpf.c | 44 ++++ .../selftests/bpf/progs/test_ksyms_module.c | 41 +++- .../bpf/progs/test_ksyms_module_fail.c | 29 +++ .../progs/test_ksyms_module_fail_toomany.c | 19 ++ .../bpf/progs/test_ksyms_module_libbpf.c | 71 ++++++ .../bpf/progs/test_ksyms_module_util.h | 48 ++++ 32 files changed, 1014 insertions(+), 153 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/ksyms_module_libbpf.c create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_module_fail.c create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_module_fail_toomany.c create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_module_libbpf.c create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_module_util.h
Signature · 1 line
2.33.0
Kumar Kartikeya Dwivedi
maintainerPATCH v4 1/11 bpf: Introduce BPF support for kernel module function calls
This change adds support on the kernel side to allow for BPF programs to call kernel module functions. Userspace will prepare an array of module BTF fds that is passed in during BPF_PROG_LOAD using fd_array parameter. In the kernel, the module BTFs are placed in the auxilliary struct for bpf_prog, and loaded as needed.
The verifier then uses insn->off to index into the fd_array. insn->off 0 is reserved for vmlinux BTF (for backwards compat), so userspace must use an fd_array index > 0 for module kfunc support. kfunc_btf_tab is sorted based on offset in an array, and each offset corresponds to one descriptor, with a max limit up to 256 such module BTFs.
Another change is to check_kfunc_call callback, which now include a struct module * pointer, this is to be used in later patch such that the kfunc_id and module pointer are matched for dynamically registered BTF sets from loadable modules, so that same kfunc_id in two modules doesn't lead to check_kfunc_call succeeding. For the duration of the check_kfunc_call, the reference to struct module exists, as it returns the pointer stored in kfunc_btf_tab.
--- include/linux/bpf.h | 8 +- include/linux/bpf_verifier.h | 2 + kernel/bpf/core.c | 4 + kernel/bpf/verifier.c | 202 ++++++++++++++++++++++++++++++----- net/bpf/test_run.c | 2 +- net/ipv4/bpf_tcp_ca.c | 2 +- 6 files changed, 188 insertions(+), 32 deletions(-)
include/linux/bpf.h +5 -3
@@ -511,7 +511,7 @@ struct bpf_verifier_ops { const struct btf_type *t, int off, int size, enum bpf_access_type atype, u32 *next_btf_id); -bool (*check_kfunc_call)(u32 kfunc_btf_id); +bool (*check_kfunc_call)(u32 kfunc_btf_id,struct module *owner); };struct bpf_prog_offload_ops {
@@ -874,6 +874,7 @@ struct bpf_prog_aux {void *jit_data;/* JIT specific data. arch dependent */ struct bpf_jit_poke_descriptor *poke_tab;struct bpf_kfunc_desc_tab *kfunc_tab; +struct bpf_kfunc_btf_tab *kfunc_btf_tab;u32 size_poke_tab;struct bpf_ksym ksym;const struct bpf_prog_ops *ops;
@@ -1636,7 +1637,7 @@ int bpf_prog_test_run_raw_tp(struct bpf_prog *prog,int bpf_prog_test_run_sk_lookup (struct bpf_prog *prog,const union bpf_attr *kattr,union bpf_attr __user *uattr); -bool bpf_prog_test_check_kfunc_call (u32 kfunc_id); +bool bpf_prog_test_check_kfunc_call (u32 kfunc_id,struct module *owner);bool btf_ctx_access (int off,int size,enum bpf_access_type type,const struct bpf_prog *prog,struct bpf_insn_access_aux *info);
@@ -1857,7 +1858,8 @@ static inline int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog,return -ENOTSUPP ; } -static inline bool bpf_prog_test_check_kfunc_call (u32 kfunc_id) +static inline bool bpf_prog_test_check_kfunc_call (u32 kfunc_id, +struct module *owner) {return false; }
include/linux/bpf_verifier.h +2 -0
@@ -527,5 +527,7 @@ int bpf_check_attach_target(struct bpf_verifier_log *log,const struct bpf_prog *tgt_prog, u32 btf_id, struct bpf_attach_target_info *tgt_info); +void bpf_free_kfunc_btf_tab (struct bpf_kfunc_btf_tab *tab); +#endif /* _LINUX_BPF_VERIFIER_H */
kernel/bpf/core.c +4 -0
@@ -32,6 +32,7 @@#include <linux/perf_event.h> #include <linux/extable.h> #include <linux/log2.h> +#include <linux/bpf_verifier.h> #include <asm/barrier.h> #include <asm/unaligned.h>
@@ -2255,6 +2256,9 @@ static void bpf_prog_free_deferred(struct work_struct *work)int i; aux = container_of(work, struct bpf_prog_aux, work); +#ifdef CONFIG_BPF_SYSCALL +bpf_free_kfunc_btf_tab (aux->kfunc_btf_tab); +#endif bpf_free_used_maps (aux);bpf_free_used_btfs (aux);if (bpf_prog_is_dev_bound (aux))
kernel/bpf/verifier.c +175 -27
357 lines not on this page · raw message
net/bpf/test_run.c +1 -1
@@ -241,7 +241,7 @@ BTF_ID(func, bpf_kfunc_call_test2) BTF_ID(func, bpf_kfunc_call_test3)BTF_SET_END (test_sk_kfunc_ids) -bool bpf_prog_test_check_kfunc_call(u32 kfunc_id) +bool bpf_prog_test_check_kfunc_call(u32 kfunc_id, struct module *owner) {return btf_id_set_contains (&test_sk_kfunc_ids, kfunc_id); }
net/ipv4/bpf_tcp_ca.c +1 -1
@@ -255,7 +255,7 @@ BTF_ID(func, bbr_set_state)#endif /* CONFIG_X86 */ BTF_SET_END (bpf_tcp_ca_kfunc_ids) -static bool bpf_tcp_ca_check_kfunc_call(u32 kfunc_btf_id) +static bool bpf_tcp_ca_check_kfunc_call(u32 kfunc_btf_id, struct module *owner) {return btf_id_set_contains (&bpf_tcp_ca_kfunc_ids, kfunc_btf_id); }
Signature · 1 line
2.33.0
Kumar Kartikeya Dwivedi
maintainerPATCH v4 2/11 bpf: Be conservative while processing invalid kfunc calls
In reply to Kumar Kartikeya DwivediThis patch also modifies the BPF verifier to only return error for invalid kfunc calls specially marked by userspace (with insn->imm == 0, insn->off == 0) after the verifier has eliminated dead instructions. This can be handled in the fixup stage, and skip processing during add and check stages.
If such an invalid call is dropped, the fixup stage will not encounter insn->imm as 0, otherwise it bails out and returns an error.
This will be exposed as weak ksym support in libbpf in later patches.
--- kernel/bpf/verifier.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
kernel/bpf/verifier.c +18 -0
@@ -1820,6 +1820,15 @@ static int add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, s16 offset) prog_aux->kfunc_tab = tab; } +/* func_id == 0 is always invalid, but instead of returning an error, be +* conservative and wait until the code elimination pass before returning +* error, so that invalid calls that get pruned out can be in BPF programs +* loaded from userspace. It is also required that offset be untouched +* for such calls. +*/ +if (!func_id && !offset) +return 0 ; +if (!btf_tab && offset) { btf_tab =kzalloc (sizeof (*btf_tab),GFP_KERNEL );if (!btf_tab)
@@ -6630,6 +6639,10 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn)struct btf *desc_btf;int err; +/* skip for now, but return error when we find this in fixup_kfunc_call */ +if (!insn->imm) +return 0 ; + desc_btf =find_kfunc_desc_btf (env, insn->imm, insn->off, &btf_mod);if (IS_ERR (desc_btf))return PTR_ERR (desc_btf);
@@ -12766,6 +12779,11 @@ static int fixup_kfunc_call(struct bpf_verifier_env *env, {const struct bpf_kfunc_desc *desc; +if (!insn->imm) { +verbose (env,"invalid kernel function call not eliminated in verifier pass\n" ); +return -EINVAL ; + } +/* insn->imm has the btf func_id. Replace it with * an address (relative to __bpf_base_call). */
Signature · 1 line
2.33.0
Kumar Kartikeya Dwivedi
maintainerPATCH v4 3/11 bpf: btf: Introduce helpers for dynamic BTF set registration
In reply to Kumar Kartikeya DwivediThis adds helpers for registering btf_id_set from modules and the check_kfunc_call callback that can be used to look them up.
With in kernel sets, the way this is supposed to work is, in kernel callback looks up within the in-kernel kfunc whitelist, and then defers to the dynamic BTF set lookup if it doesn't find the BTF id. If there is no in-kernel BTF id set, this callback can be used directly.
Also fix includes for btf.h and bpfptr.h so that they can included in isolation. This is in preparation for their usage in tcp_bbr, tcp_cubic and tcp_dctcp modules in the next patch.
--- include/linux/bpfptr.h | 1 + include/linux/btf.h | 31 +++++++++++++++++++++++++ kernel/bpf/btf.c | 51 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+)
include/linux/bpfptr.h +1 -0
@@ -3,6 +3,7 @@#ifndef _LINUX_BPFPTR_H#define _LINUX_BPFPTR_H +#include <linux/mm.h> #include <linux/sockptr.h> typedef sockptr_t bpfptr_t ;
include/linux/btf.h +31 -0
@@ -5,6 +5,7 @@#define _LINUX_BTF_H 1#include <linux/types.h> +#include <linux/bpfptr.h> #include <uapi/linux/btf.h> #include <uapi/linux/bpf.h>
@@ -238,4 +239,34 @@ static inline const char *btf_name_by_offset(const struct btf *btf, }#endif +struct kfunc_btf_id_set { +struct list_head list; +struct btf_id_set *set; +struct module *owner; +}; + +struct kfunc_btf_id_list ; + +#ifdef CONFIG_DEBUG_INFO_BTF_MODULES +void register_kfunc_btf_id_set (struct kfunc_btf_id_list *l, +struct kfunc_btf_id_set *s); +void unregister_kfunc_btf_id_set (struct kfunc_btf_id_list *l, +struct kfunc_btf_id_set *s); +#else +static inline void register_kfunc_btf_id_set (struct kfunc_btf_id_list *l, +struct kfunc_btf_id_set *s) +{ +} +static inline void unregister_kfunc_btf_id_set (struct kfunc_btf_id_list *l, +struct kfunc_btf_id_set *s) +{ +} +#endif + +#define DECLARE_CHECK_KFUNC_CALLBACK (type) \ + bool __bpf_check_##type##_kfunc_call(u32 kfunc_id, struct module *owner) +#define DEFINE_KFUNC_BTF_ID_SET (set, name) \ + struct kfunc_btf_id_set name = { LIST_HEAD_INIT(name.list), (set), \ + THIS_MODULE } +#endif
kernel/bpf/btf.c +51 -0
54 lines not on this page · raw message
Signature · 1 line
2.33.0
Kumar Kartikeya Dwivedi
maintainerPATCH v4 4/11 tools: Allow specifying base BTF file in resolve_btfids
In reply to Kumar Kartikeya DwivediThis commits allows specifying the base BTF for resolving btf id lists/sets during link time in the resolve_btfids tool. The base BTF is set to NULL if no path is passed. This allows resolving BTF ids for module kernel objects.
--- tools/bpf/resolve_btfids/main.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-)
tools/bpf/resolve_btfids/main.c +17 -2
@@ -89,6 +89,7 @@ struct btf_id {struct object {const char *path;const char *btf; +const char *base_btf_path;struct {int fd;
@@ -477,16 +478,27 @@ static int symbols_resolve(struct object *obj)int nr_structs = obj->nr_structs;int nr_unions = obj->nr_unions;int nr_funcs = obj->nr_funcs; +struct btf *base_btf =NULL ;int err, type_id;struct btf *btf;__u32 nr_types; - btf =btf__parse (obj->btf ?: obj->path,NULL ); +if (obj->base_btf_path) { + base_btf =btf__parse (obj->base_btf_path,NULL ); + err =libbpf_get_error (base_btf); +if (err) { +pr_err ("FAILED: load base BTF from %s: %s\n" , + obj->base_btf_path,strerror (-err)); +return -1 ; + } + } + + btf =btf__parse_split (obj->btf ?: obj->path, base_btf); err =libbpf_get_error (btf);if (err) {pr_err ("FAILED: load BTF from %s: %s\n" , obj->btf ?: obj->path,strerror (-err)); -return -1 ; + goto out; } err =-1 ;
@@ -545,6 +557,7 @@ static int symbols_resolve(struct object *obj) err =0 ; out: +btf__free (base_btf);btf__free (btf);return err; }
@@ -697,6 +710,8 @@ int main(int argc, const char **argv) "BTF data"),OPT_BOOLEAN (0 ,"no-fail" , &no_fail,"do not fail if " BTF_IDS_SECTION " section is not found" ), +OPT_STRING ('s' ,"base-btf" , &obj.base_btf_path,"file" , +"path of file providing base BTF data" ), OPT_END() };int err =-1 ;
Signature · 1 line
2.33.0
Kumar Kartikeya Dwivedi
maintainerPATCH v4 5/11 bpf: Enable TCP congestion control kfunc from modules
In reply to Kumar Kartikeya DwivediThis commit moves BTF ID lookup into the newly added registration helper, in a way that the bbr, cubic, and dctcp implementation set up their sets in the bpf_tcp_ca kfunc_btf_set list, while the ones not dependent on modules are looked up from the wrapper function.
This lifts the restriction for them to be compiled as built in objects, and can be loaded as modules if required. Also modify Makefile.modfinal to resolve_btfids in TCP congestion control modules if the config option is set, using the base BTF support added in the previous commit.
See following commits for background on use of:
CONFIG_X86 ifdef:
569c484f9995 (bpf: Limit static tcp-cc functions in the .BTF_ids list to x86)
CONFIG_DYNAMIC_FTRACE ifdef:
7aae231ac93b (bpf: tcp: Limit calling some tcp cc functions to CONFIG_DYNAMIC_FTRACE)
[ resolve_btfids uses --no-fail because some crypto kernel modules
under arch/x86/crypto generated from ASM do not have the .BTF sections ]
--- include/linux/btf.h | 4 ++++ kernel/bpf/btf.c | 3 +++ net/ipv4/bpf_tcp_ca.c | 34 +++------------------------------- net/ipv4/tcp_bbr.c | 28 +++++++++++++++++++++++++++- net/ipv4/tcp_cubic.c | 26 +++++++++++++++++++++++++- net/ipv4/tcp_dctcp.c | 26 +++++++++++++++++++++++++- scripts/Makefile.modfinal | 1 + 7 files changed, 88 insertions(+), 34 deletions(-)
include/linux/btf.h +4 -0
@@ -269,4 +269,8 @@ static inline void unregister_kfunc_btf_id_set(struct kfunc_btf_id_list *l,struct kfunc_btf_id_set name = {LIST_HEAD_INIT (name.list), (set), \ THIS_MODULE } +extern struct kfunc_btf_id_list bpf_tcp_ca_kfunc_list; + +DECLARE_CHECK_KFUNC_CALLBACK (bpf_tcp_ca); +#endif
kernel/bpf/btf.c +3 -0
@@ -6394,3 +6394,6 @@ EXPORT_SYMBOL_GPL(unregister_kfunc_btf_id_set);mutex_unlock (&list_name.mutex); \return false; \ } + +DEFINE_KFUNC_BTF_ID_LIST (bpf_tcp_ca_kfunc_list); +DEFINE_CHECK_KFUNC_CALLBACK (bpf_tcp_ca, bpf_tcp_ca_kfunc_list);
net/ipv4/bpf_tcp_ca.c +3 -31
@@ -223,41 +223,13 @@ BTF_ID(func, tcp_reno_cong_avoid) BTF_ID(func, tcp_reno_undo_cwnd) BTF_ID(func, tcp_slow_start) BTF_ID(func, tcp_cong_avoid_ai) -#ifdef CONFIG_X86 -#ifdef CONFIG_DYNAMIC_FTRACE -#if IS_BUILTIN (CONFIG_TCP_CONG_CUBIC ) -BTF_ID(func, cubictcp_init) -BTF_ID(func, cubictcp_recalc_ssthresh) -BTF_ID(func, cubictcp_cong_avoid) -BTF_ID(func, cubictcp_state) -BTF_ID(func, cubictcp_cwnd_event) -BTF_ID(func, cubictcp_acked) -#endif -#if IS_BUILTIN (CONFIG_TCP_CONG_DCTCP ) -BTF_ID(func, dctcp_init) -BTF_ID(func, dctcp_update_alpha) -BTF_ID(func, dctcp_cwnd_event) -BTF_ID(func, dctcp_ssthresh) -BTF_ID(func, dctcp_cwnd_undo) -BTF_ID(func, dctcp_state) -#endif -#if IS_BUILTIN (CONFIG_TCP_CONG_BBR ) -BTF_ID(func, bbr_init) -BTF_ID(func, bbr_main) -BTF_ID(func, bbr_sndbuf_expand) -BTF_ID(func, bbr_undo_cwnd) -BTF_ID(func, bbr_cwnd_event) -BTF_ID(func, bbr_ssthresh) -BTF_ID(func, bbr_min_tso_segs) -BTF_ID(func, bbr_set_state) -#endif -#endif /* CONFIG_DYNAMIC_FTRACE */ -#endif /* CONFIG_X86 */ BTF_SET_END (bpf_tcp_ca_kfunc_ids) static bool bpf_tcp_ca_check_kfunc_call(u32 kfunc_btf_id, struct module *owner) { -return btf_id_set_contains (&bpf_tcp_ca_kfunc_ids, kfunc_btf_id); +if (btf_id_set_contains (&bpf_tcp_ca_kfunc_ids, kfunc_btf_id)) +return true; +return __bpf_check_bpf_tcp_ca_kfunc_call (kfunc_btf_id, owner); }static const struct bpf_verifier_ops bpf_tcp_ca_verifier_ops = {
net/ipv4/tcp_bbr.c +27 -1
@@ -56,6 +56,8 @@* otherwise TCP stack falls back to an internal pacing using one high * resolution timer per TCP socket and may use more resources. */ +#include <linux/btf.h> +#include <linux/btf_ids.h> #include <linux/module.h> #include <net/tcp.h> #include <linux/inet_diag.h>
@@ -1152,14 +1154,38 @@ static struct tcp_congestion_ops tcp_bbr_cong_ops __read_mostly = { .set_state = bbr_set_state, }; +BTF_SET_START(tcp_bbr_kfunc_ids) +#ifdef CONFIG_X86 +#ifdef CONFIG_DYNAMIC_FTRACE +BTF_ID(func, bbr_init) +BTF_ID(func, bbr_main) +BTF_ID(func, bbr_sndbuf_expand) +BTF_ID(func, bbr_undo_cwnd) +BTF_ID(func, bbr_cwnd_event) +BTF_ID(func, bbr_ssthresh) +BTF_ID(func, bbr_min_tso_segs) +BTF_ID(func, bbr_set_state) +#endif +#endif +BTF_SET_END (tcp_bbr_kfunc_ids) + +static DEFINE_KFUNC_BTF_ID_SET(&tcp_bbr_kfunc_ids, tcp_bbr_kfunc_btf_set); +static int __init bbr_register (void ) { +int ret; +BUILD_BUG_ON (sizeof (struct bbr ) >ICSK_CA_PRIV_SIZE ); -return tcp_register_congestion_control (&tcp_bbr_cong_ops); + ret =tcp_register_congestion_control (&tcp_bbr_cong_ops); +if (ret) +return ret; +register_kfunc_btf_id_set (&bpf_tcp_ca_kfunc_list, &tcp_bbr_kfunc_btf_set); +return 0 ; } static void __exit bbr_unregister(void) { +unregister_kfunc_btf_id_set (&bpf_tcp_ca_kfunc_list, &tcp_bbr_kfunc_btf_set);tcp_unregister_congestion_control (&tcp_bbr_cong_ops); }
net/ipv4/tcp_cubic.c +25 -1
@@ -25,6 +25,8 @@*/ #include <linux/mm.h> +#include <linux/btf.h> +#include <linux/btf_ids.h> #include <linux/module.h> #include <linux/math64.h> #include <net/tcp.h>
@@ -482,8 +484,25 @@ static struct tcp_congestion_ops cubictcp __read_mostly = { .name = "cubic", }; +BTF_SET_START(tcp_cubic_kfunc_ids) +#ifdef CONFIG_X86 +#ifdef CONFIG_DYNAMIC_FTRACE +BTF_ID(func, cubictcp_init) +BTF_ID(func, cubictcp_recalc_ssthresh) +BTF_ID(func, cubictcp_cong_avoid) +BTF_ID(func, cubictcp_state) +BTF_ID(func, cubictcp_cwnd_event) +BTF_ID(func, cubictcp_acked) +#endif +#endif +BTF_SET_END (tcp_cubic_kfunc_ids) + +static DEFINE_KFUNC_BTF_ID_SET(&tcp_cubic_kfunc_ids, tcp_cubic_kfunc_btf_set); +static int __init cubictcp_register (void ) { +int ret; +BUILD_BUG_ON (sizeof (struct bictcp ) >ICSK_CA_PRIV_SIZE );/* Precompute a bunch of the scaling factors that are used per-packet
@@ -514,11 +533,16 @@ static int __init cubictcp_register(void)/* divide by bic_scale and by constant Srtt (100ms) */ do_div (cube_factor, bic_scale *10 ); -return tcp_register_congestion_control (&cubictcp); + ret =tcp_register_congestion_control (&cubictcp); +if (ret) +return ret; +register_kfunc_btf_id_set (&bpf_tcp_ca_kfunc_list, &tcp_cubic_kfunc_btf_set); +return 0 ; } static void __exit cubictcp_unregister(void) { +unregister_kfunc_btf_id_set (&bpf_tcp_ca_kfunc_list, &tcp_cubic_kfunc_btf_set);tcp_unregister_congestion_control (&cubictcp); }
net/ipv4/tcp_dctcp.c +25 -1
@@ -36,6 +36,8 @@* Glenn Judd <[email protected]> */ +#include <linux/btf.h> +#include <linux/btf_ids.h> #include <linux/module.h> #include <linux/mm.h> #include <net/tcp.h>
@@ -236,14 +238,36 @@ static struct tcp_congestion_ops dctcp_reno __read_mostly = { .name = "dctcp-reno", }; +BTF_SET_START(tcp_dctcp_kfunc_ids) +#ifdef CONFIG_X86 +#ifdef CONFIG_DYNAMIC_FTRACE +BTF_ID(func, dctcp_init) +BTF_ID(func, dctcp_update_alpha) +BTF_ID(func, dctcp_cwnd_event) +BTF_ID(func, dctcp_ssthresh) +BTF_ID(func, dctcp_cwnd_undo) +BTF_ID(func, dctcp_state) +#endif +#endif +BTF_SET_END (tcp_dctcp_kfunc_ids) + +static DEFINE_KFUNC_BTF_ID_SET(&tcp_dctcp_kfunc_ids, tcp_dctcp_kfunc_btf_set); +static int __init dctcp_register (void ) { +int ret; +BUILD_BUG_ON (sizeof (struct dctcp ) >ICSK_CA_PRIV_SIZE ); -return tcp_register_congestion_control (&dctcp); + ret =tcp_register_congestion_control (&dctcp); +if (ret) +return ret; +register_kfunc_btf_id_set (&bpf_tcp_ca_kfunc_list, &tcp_dctcp_kfunc_btf_set); +return 0 ; } static void __exit dctcp_unregister(void) { +unregister_kfunc_btf_id_set (&bpf_tcp_ca_kfunc_list, &tcp_dctcp_kfunc_btf_set);tcp_unregister_congestion_control (&dctcp); }
scripts/Makefile.modfinal +1 -0
@@ -41,6 +41,7 @@ quiet_cmd_btf_ko = BTF [M] $@ cmd_btf_ko = \ if [ -f vmlinux ]; then \ LLVM_OBJCOPY="$(OBJCOPY)" $(PAHOLE) -J --btf_base vmlinux $@; \ + $(RESOLVE_BTFIDS) --no-fail -s vmlinux $@; \ else \ printf "Skipping BTF generation for %s due to unavailability of vmlinux\n" $@ 1>&2; \ fi;
Signature · 1 line
2.33.0
Re: [PATCH bpf-next v4 05/11] bpf: Enable TCP congestion control kfunc from modules
Quoted 33 lines · Kumar Kartikeya Dwivedi
This commit moves BTF ID lookup into the newly added registration helper, in a way that the bbr, cubic, and dctcp implementation set up their sets in the
bpf_tcp_cakfunc_btf_setlist, while the ones not dependent on modules are looked up from the wrapper function.This lifts the restriction for them to be compiled as built in objects, and can be loaded as modules if required. Also modify Makefile.modfinal to
resolve_btfidsin TCP congestion control modules if the config option is set, using the base BTF support added in the previous commit.See following commits for background on use of:
CONFIG_X86ifdef:
569c484f9995(bpf: Limit static tcp-cc functions in the .BTF_ids list to x86)
CONFIG_DYNAMIC_FTRACEifdef:
7aae231ac93b(bpf: tcp: Limit calling some tcp cc functions toCONFIG_DYNAMIC_FTRACE)[
resolve_btfidsuses --no-fail because some crypto kernel modules
underarch/x86/cryptogenerated from ASM do not have the .BTF sections ]Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- include/linux/btf.h | 4 ++++ kernel/bpf/btf.c | 3 +++ net/ipv4/bpf_tcp_ca.c | 34 +++------------------------------- net/ipv4/tcp_bbr.c | 28 +++++++++++++++++++++++++++- net/ipv4/tcp_cubic.c | 26 +++++++++++++++++++++++++- net/ipv4/tcp_dctcp.c | 26 +++++++++++++++++++++++++- scripts/Makefile.modfinal | 1 + 7 files changed, 88 insertions(+), 34 deletions(-)
[...]
scripts/Makefile.modfinal quiet_cmd_btf_ko = BTF [M] $@ · line 44
Quoted 9 lines
scripts/Makefile.modfinal +1
-0@@ -41,6 +41,7 @@ quiet_cmd_btf_ko = BTF [M] $@ cmd_btf_ko = \ if [ -f vmlinux ]; then \ LLVM_OBJCOPY="$(OBJCOPY)" $(PAHOLE) -J --btf_base vmlinux $@; \ + $(RESOLVE_BTFIDS) --no-fail -s vmlinux $@; \
I think I've asked that before, but I don't remember this being answered. Why is this --no-fail?
Quoted 6 lines
scripts/Makefile.modfinal +0
-1else \ printf "Skipping BTF generation for %s due to unavailability of vmlinux\n" $@ 1>&2; \ fi; --2.33.0
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v4 05/11] bpf: Enable TCP congestion control kfunc from modules
Quoted 38 lines · Andrii Nakryiko
Quoted 33 lines · Kumar Kartikeya Dwivedi
This commit moves BTF ID lookup into the newly added registration helper, in a way that the bbr, cubic, and dctcp implementation set up their sets in the
bpf_tcp_cakfunc_btf_setlist, while the ones not dependent on modules are looked up from the wrapper function.This lifts the restriction for them to be compiled as built in objects, and can be loaded as modules if required. Also modify Makefile.modfinal to
resolve_btfidsin TCP congestion control modules if the config option is set, using the base BTF support added in the previous commit.See following commits for background on use of:
CONFIG_X86ifdef:
569c484f9995(bpf: Limit static tcp-cc functions in the .BTF_ids list to x86)
CONFIG_DYNAMIC_FTRACEifdef:
7aae231ac93b(bpf: tcp: Limit calling some tcp cc functions toCONFIG_DYNAMIC_FTRACE)[
resolve_btfidsuses --no-fail because some crypto kernel modules
underarch/x86/cryptogenerated from ASM do not have the .BTF sections ]Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- include/linux/btf.h | 4 ++++ kernel/bpf/btf.c | 3 +++ net/ipv4/bpf_tcp_ca.c | 34 +++------------------------------- net/ipv4/tcp_bbr.c | 28 +++++++++++++++++++++++++++- net/ipv4/tcp_cubic.c | 26 +++++++++++++++++++++++++- net/ipv4/tcp_dctcp.c | 26 +++++++++++++++++++++++++- scripts/Makefile.modfinal | 1 + 7 files changed, 88 insertions(+), 34 deletions(-)[...]
Quoted 13 lines
scripts/Makefile.modfinal quiet_cmd_btf_ko = BTF [M] $@ · line 44
Quoted 9 lines
scripts/Makefile.modfinal +1
-0@@ -41,6 +41,7 @@ quiet_cmd_btf_ko = BTF [M] $@ cmd_btf_ko = \ if [ -f vmlinux ]; then \ LLVM_OBJCOPY="$(OBJCOPY)" $(PAHOLE) -J --btf_base vmlinux $@; \ + $(RESOLVE_BTFIDS) --no-fail -s vmlinux $@; \I think I've asked that before, but I don't remember this being answered. Why is this --no-fail?
Sorry, the first time, I missed that mail, and then it was too late so I decided to put the reason in the commit message above.
Quoted 2 lines
Quoted 2 lines
[
resolve_btfidsuses --no-fail because some crypto kernel modules
underarch/x86/cryptogenerated from ASM do not have the .BTF sections ]
I could add a mode that fails only when processing a .BTF section present in object fails, would that be better?
--
Kartikeya
Re: [PATCH bpf-next v4 05/11] bpf: Enable TCP congestion control kfunc from modules
Quoted 54 lines · Kumar Kartikeya Dwivedi
Quoted 38 lines · Andrii Nakryiko
Quoted 33 lines · Kumar Kartikeya Dwivedi
This commit moves BTF ID lookup into the newly added registration helper, in a way that the bbr, cubic, and dctcp implementation set up their sets in the
bpf_tcp_cakfunc_btf_setlist, while the ones not dependent on modules are looked up from the wrapper function.This lifts the restriction for them to be compiled as built in objects, and can be loaded as modules if required. Also modify Makefile.modfinal to
resolve_btfidsin TCP congestion control modules if the config option is set, using the base BTF support added in the previous commit.See following commits for background on use of:
CONFIG_X86ifdef:
569c484f9995(bpf: Limit static tcp-cc functions in the .BTF_ids list to x86)
CONFIG_DYNAMIC_FTRACEifdef:
7aae231ac93b(bpf: tcp: Limit calling some tcp cc functions toCONFIG_DYNAMIC_FTRACE)[
resolve_btfidsuses --no-fail because some crypto kernel modules
underarch/x86/cryptogenerated from ASM do not have the .BTF sections ]Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- include/linux/btf.h | 4 ++++ kernel/bpf/btf.c | 3 +++ net/ipv4/bpf_tcp_ca.c | 34 +++------------------------------- net/ipv4/tcp_bbr.c | 28 +++++++++++++++++++++++++++- net/ipv4/tcp_cubic.c | 26 +++++++++++++++++++++++++- net/ipv4/tcp_dctcp.c | 26 +++++++++++++++++++++++++- scripts/Makefile.modfinal | 1 + 7 files changed, 88 insertions(+), 34 deletions(-)[...]
Quoted 13 lines
scripts/Makefile.modfinal quiet_cmd_btf_ko = BTF [M] $@ · line 44
Quoted 9 lines
scripts/Makefile.modfinal +1
-0@@ -41,6 +41,7 @@ quiet_cmd_btf_ko = BTF [M] $@ cmd_btf_ko = \ if [ -f vmlinux ]; then \ LLVM_OBJCOPY="$(OBJCOPY)" $(PAHOLE) -J --btf_base vmlinux $@; \ + $(RESOLVE_BTFIDS) --no-fail -s vmlinux $@; \I think I've asked that before, but I don't remember this being answered. Why is this --no-fail?
Quoted 8 lines
Sorry, the first time, I missed that mail, and then it was too late so I decided to put the reason in the commit message above.
Quoted 2 lines
Quoted 2 lines
[
resolve_btfidsuses --no-fail because some crypto kernel modules
underarch/x86/cryptogenerated from ASM do not have the .BTF sections ]I could add a mode that fails only when processing a .BTF section present in object fails, would that be better?
Oh, missed [ ] part in the commit message. But yeah, it feels like it shouldn't be an error if the module legitimately doesn't have a .BTF section. Is it an error right now? cc Jiri, maybe that was intentional
Quoted 3 lines
--
Kartikeya
Kumar Kartikeya Dwivedi
maintainerPATCH v4 6/11 libbpf: Support kernel module function calls
In reply to Kumar Kartikeya DwivediThis patch adds libbpf support for kernel module function call support. The fd_array parameter is used during BPF program load is used to pass module BTFs referenced by the program. insn->off is set to index into this array, but starts from 1, because insn->off as 0 is reserved for btf_vmlinux.
We try to use existing insn->off for a module, since the kernel limits the maximum distinct module BTFs for kfuncs to 256, and also because index must never exceed the maximum allowed value that can fit in insn->off (INT16_MAX). In the future, if kernel interprets signed offset as unsigned for kfunc calls, this limit can be increased to UINT16_MAX.
--- tools/lib/bpf/bpf.c | 1 + tools/lib/bpf/libbpf.c | 58 +++++++++++++++++++++++++++++++-- tools/lib/bpf/libbpf_internal.h | 1 + 3 files changed, 57 insertions(+), 3 deletions(-)
tools/lib/bpf/bpf.c +1 -0
@@ -264,6 +264,7 @@ int libbpf__bpf_prog_load(const struct bpf_prog_load_params *load_attr) attr.line_info_rec_size = load_attr->line_info_rec_size; attr.line_info_cnt = load_attr->line_info_cnt; attr.line_info =ptr_to_u64 (load_attr->line_info); + attr.fd_array =ptr_to_u64 (load_attr->fd_array);if (load_attr->name) memcpy(attr.prog_name, load_attr->name,
tools/lib/bpf/libbpf.c +55 -3
94 lines not on this page · raw message
tools/lib/bpf/libbpf_internal.h +1 -0
@@ -291,6 +291,7 @@ struct bpf_prog_load_params {__u32 log_level;char *log_buf;size_t log_buf_sz; +int *fd_array; };int libbpf__bpf_prog_load (const struct bpf_prog_load_params *load_attr);
Signature · 1 line
2.33.0
Re: [PATCH bpf-next v4 06/11] libbpf: Support kernel module function calls
Quoted 37 lines · Kumar Kartikeya Dwivedi
This patch adds libbpf support for kernel module function call support. The
fd_arrayparameter is used during BPF program load is used to pass module BTFs referenced by the program.insn->offis set to index into this array, but starts from 1, becauseinsn->offas 0 is reserved forbtf_vmlinux.We try to use existing
insn->offfor a module, since the kernel limits the maximum distinct module BTFs for kfuncs to 256, and also because index must never exceed the maximum allowed value that can fit ininsn->off(INT16_MAX). In the future, if kernel interprets signed offset as unsigned for kfunc calls, this limit can be increased toUINT16_MAX.Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- tools/lib/bpf/bpf.c | 1 + tools/lib/bpf/libbpf.c | 58 +++++++++++++++++++++++++++++++-- tools/lib/bpf/libbpf_internal.h | 1 + 3 files changed, 57 insertions(+), 3 deletions(-)tools/lib/bpf/bpf.c +1
-0@@ -264,6 +264,7 @@ int libbpf__bpf_prog_load(const struct bpf_prog_load_params *load_attr) attr.line_info_rec_size = load_attr->line_info_rec_size; attr.line_info_cnt = load_attr->line_info_cnt; attr.line_info =ptr_to_u64 (load_attr->line_info); + attr.fd_array =ptr_to_u64 (load_attr->fd_array);if (load_attr->name) memcpy(attr.prog_name, load_attr->name,tools/lib/bpf/libbpf.c +0
-0
tools/lib/bpf/libbpf.c struct extern_desc { · line 424
Quoted 5 lines
tools/lib/bpf/libbpf.c +2
-0@@ -420,6 +420,12 @@ struct extern_desc {/* local btf_id of the ksym extern's type. */ __u32 type_id; +/* offset to be patched in for insn->off, +* this is 0 for btf_vmlinux, and index + 1
What does "index + 1" mean here? Seems like kernel code is using the offset as is, without any -1 compensation.
Quoted 41 lines
tools/lib/bpf/libbpf.c +10
-3+* for module BTF, where index is BTF index in +* obj->fd_array +*/ +__s16 offset; } ksym; }; };@@ -516,6 +522,10 @@ struct bpf_object {void *priv;bpf_object_clear_priv_t clear_priv; +int *fd_array; +size_t fd_cap_cnt; +int nr_fds; +char path[]; };#define obj_elf_valid (o) ((o)->efile.elf)@@ -5357,6 +5367,7 @@ bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog) ext = &obj->externs[relo->sym_off]; insn[0 ].src_reg =BPF_PSEUDO_KFUNC_CALL ; insn[0 ].imm = ext->ksym.kernel_btf_id; + insn[0 ].off = ext->ksym.offset;break ;case RELO_SUBPROG_ADDR :if (insn[0 ].src_reg !=BPF_PSEUDO_FUNC ) {@@ -6151,6 +6162,7 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt, } load_attr.log_level = prog->log_level; load_attr.prog_flags = prog->prog_flags; + load_attr.fd_array = prog->obj->fd_array;if (prog->obj->gen_loader) { bpf_gen__prog_load(prog->obj->gen_loader, &load_attr,@@ -6763,9 +6775,46 @@ static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object *obj, }if (kern_btf != obj->btf_vmlinux) { -pr_warn ("extern (func ksym) '%s': function in kernel module is not supported\n" , - ext->name); -return -ENOTSUP ;
tools/lib/bpf/libbpf.c static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object *obj, · line 6787
Quoted 10 lines
tools/lib/bpf/libbpf.c +10
-0+int index =-1 ; + +if (!obj->fd_array) { + obj->fd_array =calloc (8 ,sizeof (*obj->fd_array)); +if (!obj->fd_array) +return -ENOMEM ; + obj->fd_cap_cnt =8 ; +/* index = 0 is for vmlinux BTF, so skip it */ + obj->nr_fds =1 ; + }
this doesn't make sense, you use libbpf_ensure_mem() and shouldn't do anything like this, it's all taken care of already
tools/lib/bpf/libbpf.c static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object *obj, · line 6794
Quoted 7 lines
tools/lib/bpf/libbpf.c +7
-0+ +for (int i =0 ; i < obj->nr_fds; i++) { +if (obj->fd_array[i] == kern_btf_fd) { + index = i; +break ; + } + }
we can actually avoid all this. We already have a list of module BTFs in bpf_object (obj->btf_modules), where we remember their id, fd, etc. We can also remember their fd_arr_idx for quick lookup. Just teach find_ksym_btf_id() to optionally return struct module_btf * and use that to find/set idx. That seems cleaner and probably would be easier in the future as well.
tools/lib/bpf/libbpf.c static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object *obj, · line 6797
Quoted 3 lines
tools/lib/bpf/libbpf.c +3
-0+ +if (index ==-1 ) { +if (obj->nr_fds == obj->fd_cap_cnt) {
don't check, libbpf_ensure_mem() handles that
Quoted 5 lines
tools/lib/bpf/libbpf.c +5
-0+ ret =libbpf_ensure_mem ((void **)&obj->fd_array, + &obj->fd_cap_cnt,sizeof (int ), + obj->fd_cap_cnt +1 ); +if (ret) +return ret;
tools/lib/bpf/libbpf.c static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object *obj, · line 6812
Quoted 10 lines
tools/lib/bpf/libbpf.c +10
-0+ } + + index = obj->nr_fds; + obj->fd_array[obj->nr_fds++] = kern_btf_fd; + } + +if (index >INT16_MAX ) { +/* insn->off is s16 */ +pr_warn ("extern (func ksym) '%s': module btf fd index too big\n" , + ext->name);
can you log index value here as well? "module BTF FD index %d is too big\n"?
tools/lib/bpf/libbpf.c static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object *obj,
Quoted 7 lines
tools/lib/bpf/libbpf.c +5
-0+return -E2BIG ; + } + ext->ksym.offset = index; + }else { + ext->ksym.offset =0 ; }
I think it will be cleaner if you move the entire offset determination logic after all the other checks are performed and ext is mostly populated. That will also make the logic shorter and simpler because if you find kern_btf_fd match, you can exit early (or probably rather goto to report the match and exit). Otherwise
tools/lib/bpf/libbpf.c static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object *obj, · line 6820
Quoted 2 lines
tools/lib/bpf/libbpf.c +0
-0kern_func =btf__type_by_id (kern_btf, kfunc_id);
this is actually extremely wasteful for module BTFs. Let's add internal (at least for now) helper that will search only for "own" BTF types in the BTF, skipping types in base BTF. Something like btf_type_by_id_own()?
Quoted 25 lines
tools/lib/bpf/libbpf.c +3
-0@@ -6941,6 +6990,9 @@ int bpf_object__load_xattr(struct bpf_object_load_attr *attr) err =bpf_gen__finish (obj->gen_loader); } +/* clean up fd_array */ +zfree (&obj->fd_array); +/* clean up module BTFs */ for (i =0 ; i < obj->btf_module_cnt; i++) {close (obj->btf_modules[i].fd);tools/lib/bpf/libbpf_internal.h +1
-1@@ -291,6 +291,7 @@ struct bpf_prog_load_params {__u32 log_level;char *log_buf;size_t log_buf_sz; +int *fd_array; };int libbpf__bpf_prog_load (const struct bpf_prog_load_params *load_attr); --2.33.0
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v4 06/11] libbpf: Support kernel module function calls
Quoted 7 lines · Andrii Nakryiko
Quoted 4 lines · Kumar Kartikeya Dwivedi
[...]
tools/lib/bpf/libbpf.c +3
-0+return -E2BIG ; + } + ext->ksym.offset = index;
Quoted 8 lines
tools/lib/bpf/libbpf.c static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object *obj, · line 6818
Quoted 3 lines
tools/lib/bpf/libbpf.c +2
-0+ }else { + ext->ksym.offset =0 ; }I think it will be cleaner if you move the entire offset determination logic after all the other checks are performed and ext is mostly populated. That will also make the logic shorter and simpler because if ayou find
kern_btf_fdmatch, you can exit early (or probably rather
Ack to everything else (including the other mail), but...
Quoted 2 lines
goto to report the match and exit). Otherwise
This sentence got eaten up.
Quoted 8 lines
Quoted 2 lines
kern_func = btf__type_by_id(kern_btf, kfunc_id);this is actually extremely wasteful for module BTFs. Let's add internal (at least for now) helper that will search only for "own" BTF types in the BTF, skipping types in base BTF. Something like
btf_type_by_id_own()?
Just to make sure I am not misunderstanding: I don't see where this is wasteful. btf_type_by_id seems to not be searching anything, but just returns pointer in base BTF if kfunc_id < btf->start_id, otherwise in module BTF.
What am I missing? I guess the 'kern_btf' name was the source of confusion? If so, I'll rename it.
Thanks.
--
Kartikeya
Re: [PATCH bpf-next v4 06/11] libbpf: Support kernel module function calls
Quoted 18 lines · Kumar Kartikeya Dwivedi
Quoted 7 lines · Andrii Nakryiko
Quoted 4 lines · Kumar Kartikeya Dwivedi
[...]
tools/lib/bpf/libbpf.c +3
-0+return -E2BIG ; + } + ext->ksym.offset = index;Quoted 8 lines
tools/lib/bpf/libbpf.c static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object *obj, · line 6818
Quoted 3 lines
tools/lib/bpf/libbpf.c +2
-0+ }else { + ext->ksym.offset =0 ; }I think it will be cleaner if you move the entire offset determination logic after all the other checks are performed and ext is mostly populated. That will also make the logic shorter and simpler because if ayou find
kern_btf_fdmatch, you can exit early (or probably rather
Quoted 6 lines
Ack to everything else (including the other mail), but...
Quoted 2 lines
goto to report the match and exit). Otherwise
This sentence got eaten up.
No idea what I was going to say here, sorry... Sometimes Gmail UI glitches with undo/redo, maybe that's what happened here. Doesn't matter, ignore the "Otherwise" part.
Quoted 10 lines
Quoted 9 lines
Quoted 3 lines
kern_func = btf__type_by_id(kern_btf, kfunc_id);this is actually extremely wasteful for module BTFs. Let's add internal (at least for now) helper that will search only for "own" BTF types in the BTF, skipping types in base BTF. Something like
btf_type_by_id_own()?
Quoted 4 lines
Just to make sure I am not misunderstanding: I don't see where this is wasteful.
btf_type_by_idseems to not be searching anything, but just returns pointer in base BTF ifkfunc_id<btf->start_id, otherwise in module BTF.
Hm, sorry... Right sentiment and thought, but wrong piece of code to quote it on.
I had in mind the btf__find_by_name_kind() use in find_ksym_btf_id(). Once we start going over each module, we shouldn't be re-checking vmlinux BTF when doing btf__find_by_name_kind. It should only check the types that each module BTF adds on top of vmlinux BTF. That's what would be good to optimize, especially as more complicated BPF programs will start using more ksym vars and funcs.
Quoted 7 lines
What am I missing? I guess the '
kern_btf' name was the source of confusion? If so, I'll rename it.Thanks.
--
Kartikeya
Kumar Kartikeya Dwivedi
maintainerPATCH v4 7/11 libbpf: Resolve invalid weak kfunc calls with imm = 0, off = 0
In reply to Kumar Kartikeya DwivediPreserve these calls as it allows verifier to succeed in loading the program if they are determined to be unreachable after dead code elimination during program load. If not, the verifier will fail at runtime. This is done for ext->is_weak symbols similar to the case for variable ksyms.
--- tools/lib/bpf/libbpf.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-)
tools/lib/bpf/libbpf.c +10 -9
@@ -3413,11 +3413,6 @@ static int bpf_object__collect_externs(struct bpf_object *obj)return -ENOTSUP ; } }else if (strcmp (sec_name,KSYMS_SEC ) ==0 ) { -if (btf_is_func (t) && ext->is_weak) { -pr_warn ("extern weak function %s is unsupported\n" , - ext->name); -return -ENOTSUP ; - } ksym_sec = sec; ext->type =EXT_KSYM ; skip_mods_and_typedefs(obj->btf, t->type,
@@ -5366,8 +5361,12 @@ bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog)case RELO_EXTERN_FUNC : ext = &obj->externs[relo->sym_off]; insn[0 ].src_reg =BPF_PSEUDO_KFUNC_CALL ; - insn[0 ].imm = ext->ksym.kernel_btf_id; - insn[0 ].off = ext->ksym.offset; +if (ext->is_set) { + insn[0 ].imm = ext->ksym.kernel_btf_id; + insn[0 ].off = ext->ksym.offset; + }else {/* unresolved weak kfunc */ + insn[0 ].imm = insn[0 ].off =0 ; + }break ;case RELO_SUBPROG_ADDR :if (insn[0 ].src_reg !=BPF_PSEUDO_FUNC ) {
@@ -6768,8 +6767,10 @@ static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object *obj, kfunc_id =find_ksym_btf_id (obj, ext->name,BTF_KIND_FUNC , &kern_btf, &kern_btf_fd); -if (kfunc_id <0 ) { -pr_warn ("extern (func ksym) '%s': not found in kernel BTF\n" , +if (kfunc_id == -ESRCH && ext->is_weak) { +return 0 ; + }else if (kfunc_id <0 ) { +pr_warn ("extern (func ksym) '%s': not found in kernel or module BTFs\n" , ext->name);return kfunc_id; }
Signature · 1 line
2.33.0
Re: [PATCH bpf-next v4 07/11] libbpf: Resolve invalid weak kfunc calls with imm = 0, off = 0
Quoted 9 lines · Kumar Kartikeya Dwivedi
Preserve these calls as it allows verifier to succeed in loading the program if they are determined to be unreachable after dead code elimination during program load. If not, the verifier will fail at runtime. This is done for
ext->is_weaksymbols similar to the case for variable ksyms.Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>---
Looks good with few nits below
Quoted 20 lines
tools/lib/bpf/libbpf.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-)tools/lib/bpf/libbpf.c +0
-5@@ -3413,11 +3413,6 @@ static int bpf_object__collect_externs(struct bpf_object *obj)return -ENOTSUP ; } }else if (strcmp (sec_name,KSYMS_SEC ) ==0 ) { -if (btf_is_func (t) && ext->is_weak) { -pr_warn ("extern weak function %s is unsupported\n" , - ext->name); -return -ENOTSUP ; - } ksym_sec = sec; ext->type =EXT_KSYM ; skip_mods_and_typedefs(obj->btf, t->type,
tools/lib/bpf/libbpf.c bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog) · line 5368
Quoted 10 lines
tools/lib/bpf/libbpf.c +5
-2@@ -5366,8 +5361,12 @@ bpf_object__relocate_data(struct bpf_object *obj, struct bpf_program *prog)case RELO_EXTERN_FUNC : ext = &obj->externs[relo->sym_off]; insn[0 ].src_reg =BPF_PSEUDO_KFUNC_CALL ; - insn[0 ].imm = ext->ksym.kernel_btf_id; - insn[0 ].off = ext->ksym.offset; +if (ext->is_set) { + insn[0 ].imm = ext->ksym.kernel_btf_id; + insn[0 ].off = ext->ksym.offset; + }else {/* unresolved weak kfunc */ + insn[0 ].imm = insn[0 ].off =0 ;
it's a bit too easy to miss this, please write as two separate statements
tools/lib/bpf/libbpf.c static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object *obj, · line 6772
Quoted 13 lines
tools/lib/bpf/libbpf.c +4
-2+ }break ;case RELO_SUBPROG_ADDR :if (insn[0 ].src_reg !=BPF_PSEUDO_FUNC ) {@@ -6768,8 +6767,10 @@ static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object *obj, kfunc_id =find_ksym_btf_id (obj, ext->name,BTF_KIND_FUNC , &kern_btf, &kern_btf_fd); -if (kfunc_id <0 ) { - pr_warn("extern (func ksym) '%s': not found in kernel BTF\n", +if (kfunc_id == -ESRCH && ext->is_weak) { +return 0 ; + }else if (kfunc_id <0 ) {
nit: there is return above, no need for "else if", please drop that part. It would be nice if you could clean this up in bpf_object__resolve_ksym_var_btf_id() as well. Thanks!
Quoted 7 lines
tools/lib/bpf/libbpf.c +1
-1+pr_warn ("extern (func ksym) '%s': not found in kernel or module BTFs\n" , ext->name);return kfunc_id; } --2.33.0
Kumar Kartikeya Dwivedi
maintainerPATCH v4 8/11 libbpf: Update gen_loader to emit BTF_KIND_FUNC relocations
In reply to Kumar Kartikeya DwivediThis change updates the BPF syscall loader to relocate BTF_KIND_FUNC relocations, with support for weak kfunc relocations. The next commit adds bpftool supports to set up the fd_array_sz parameter for light skeleton.
A second map for keeping fds is used instead of adding fds to existing loader.map because of following reasons:
If reserving an area for map and BTF fds, we would waste the remaining of (MAX_USED_MAPS + MAX_KFUNC_DESCS) * sizeof(int), which in most cases will be unused by the program. Also, we must place some limit on the amount of map and BTF fds a program can possibly open.
If setting gen->fd_array to first map_fd offset, and then just finding the offset relative to this (for later BTF fds), such that they can be packed without wasting space, we run the risk of unnecessarily running out of valid offset for emit_relo stage (for kfuncs), because gen map creation and relocation stages are separated by other steps that can add lots of data (including bpf_object__populate_internal_map). It is also prone to break silently if features are added between map and BTF fd emits that possibly add more data (just ~128KB to break BTF fd, since insn->off allows for INT16_MAX (32767) * 4 bytes).
Both of these issues are compounded by the fact that data map is shared by all programs, so it is easy to end up with invalid offset for BTF fd.
--- tools/lib/bpf/bpf_gen_internal.h | 16 ++- tools/lib/bpf/gen_loader.c | 222 ++++++++++++++++++++++++++----- tools/lib/bpf/libbpf.c | 8 +- tools/lib/bpf/libbpf.h | 1 + tools/lib/bpf/skel_internal.h | 27 +++- 5 files changed, 230 insertions(+), 44 deletions(-)
tools/lib/bpf/bpf_gen_internal.h +15 -1
@@ -7,6 +7,16 @@ struct ksym_relo_desc {const char *name;int kind;int insn_idx; +bool is_weak; +}; + +/* maximum distinct calls */ +#define MAX_KFUNC_DESCS 256 + +struct kfunc_desc { +const char *name; +int ref; +int off; };struct bpf_gen {
@@ -23,7 +33,10 @@ struct bpf_gen {struct ksym_relo_desc *relos;int relo_cnt;char attach_target[128 ]; +struct kfunc_desc kdescs[MAX_KFUNC_DESCS ];int attach_kind; +__u32 nr_kfuncs; +int fd_array_sz; };void bpf_gen__init (struct bpf_gen *gen,int log_level);
@@ -36,6 +49,7 @@ void bpf_gen__prog_load(struct bpf_gen *gen, struct bpf_prog_load_params *load_avoid bpf_gen__map_update_elem (struct bpf_gen *gen,int map_idx,void *value,__u32 value_size);void bpf_gen__map_freeze (struct bpf_gen *gen,int map_idx);void bpf_gen__record_attach_target (struct bpf_gen *gen,const char *name,enum bpf_attach_type type); -void bpf_gen__record_extern (struct bpf_gen *gen,const char *name,int kind,int insn_idx); +void bpf_gen__record_extern (struct bpf_gen *gen,const char *name,bool is_weak,int kind, +int insn_idx);#endif
tools/lib/bpf/gen_loader.c +190 -32
355 lines not on this page · raw message
tools/lib/bpf/libbpf.c +4 -4
@@ -6264,12 +6264,12 @@ static int bpf_program__record_externs(struct bpf_program *prog) ext->name);return -ENOTSUP ; } -bpf_gen__record_extern (obj->gen_loader, ext->name,BTF_KIND_VAR , - relo->insn_idx); +bpf_gen__record_extern (obj->gen_loader, ext->name, ext->is_weak, +BTF_KIND_VAR , relo->insn_idx);break ;case RELO_EXTERN_FUNC : -bpf_gen__record_extern (obj->gen_loader, ext->name,BTF_KIND_FUNC , - relo->insn_idx); +bpf_gen__record_extern (obj->gen_loader, ext->name, ext->is_weak, +BTF_KIND_FUNC , relo->insn_idx);break ;default :continue ;
tools/lib/bpf/libbpf.h +1 -0
@@ -885,6 +885,7 @@ struct gen_loader_opts {const char *insns;__u32 data_sz;__u32 insns_sz; +__u32 fd_array_sz; };#define gen_loader_opts__last_field insns_sz
tools/lib/bpf/skel_internal.h +20 -7
@@ -44,6 +44,7 @@ struct bpf_load_and_run_opts {const void *insns;__u32 data_sz;__u32 insns_sz; +__u32 fd_array_sz;const char *errstr; };
@@ -62,31 +63,41 @@ static inline int skel_closenz(int fd)static inline int bpf_load_and_run (struct bpf_load_and_run_opts *opts) { -int map_fd =-1 , prog_fd =-1 , key =0 , err; +int map_fd[2 ] = {-1 ,-1 }, prog_fd =-1 , key =0 , err;union bpf_attr attr; - map_fd =bpf_create_map_name (BPF_MAP_TYPE_ARRAY ,"__loader.map" ,4 , + map_fd[0 ] =bpf_create_map_name (BPF_MAP_TYPE_ARRAY ,"__loader.map" ,4 , opts->data_sz,1 ,0 ); -if (map_fd <0 ) { +if (map_fd[0 ] <0 ) { opts->errstr ="failed to create loader map" ; err = -errno; goto out; } - err =bpf_map_update_elem (map_fd, &key, opts->data,0 ); + err =bpf_map_update_elem (map_fd[0 ], &key, opts->data,0 );if (err <0 ) { opts->errstr ="failed to update loader map" ; err = -errno; goto out; } +if (opts->fd_array_sz) { + map_fd[1 ] =bpf_create_map_name (BPF_MAP_TYPE_ARRAY ,"__loader.fd.map" ,4 , + opts->fd_array_sz,1 ,0 ); +if (map_fd[1 ] <0 ) { + opts->errstr ="failed to create loader fd map" ; + err = -errno; + goto out; + } + } +memset (&attr,0 ,sizeof (attr)); attr.prog_type =BPF_PROG_TYPE_SYSCALL ; attr.insns = (long ) opts->insns; attr.insn_cnt = opts->insns_sz /sizeof (struct bpf_insn ); attr.license = (long )"Dual BSD/GPL" ;memcpy (attr.prog_name,"__loader.prog" ,sizeof ("__loader.prog" )); - attr.fd_array = (long ) &map_fd; + attr.fd_array = (long ) map_fd; attr.log_level = opts->ctx->log_level; attr.log_size = opts->ctx->log_size; attr.log_buf = opts->ctx->log_buf;
@@ -113,8 +124,10 @@ static inline int bpf_load_and_run(struct bpf_load_and_run_opts *opts) } err =0 ; out: -if (map_fd >=0 ) -close (map_fd); +if (map_fd[0 ] >=0 ) +close (map_fd[0 ]); +if (map_fd[1 ] >=0 ) +close (map_fd[1 ]);if (prog_fd >=0 )close (prog_fd);return err;
Signature · 1 line
2.33.0
Re: [PATCH bpf-next v4 08/11] libbpf: Update gen_loader to emit BTF_KIND_FUNC relocations
Quoted 8 lines · Kumar Kartikeya Dwivedi
This change updates the BPF syscall loader to relocate
BTF_KIND_FUNCrelocations, with support for weak kfunc relocations. The next commit adds bpftool supports to set up thefd_array_szparameter for light skeleton.A second map for keeping fds is used instead of adding fds to existing loader.map because of following reasons:
but it complicates signing bpf progs a lot.
Quoted 4 lines
If reserving an area for map and BTF fds, we would waste the remaining of (
MAX_USED_MAPS+MAX_KFUNC_DESCS) * sizeof(int), which in most cases will be unused by the program. Also, we must place some limit on the amount of map and BTF fds a program can possibly open.
That is just (256 + 64)*4 bytes of data. Really not much. I wouldn't worry about reserving this space.
Quoted 9 lines
If setting
gen->fd_arrayto firstmap_fdoffset, and then just finding the offset relative to this (for later BTF fds), such that they can be packed without wasting space, we run the risk of unnecessarily running out of valid offset foremit_relostage (for kfuncs), because gen map creation and relocation stages are separated by other steps that can add lots of data (includingbpf_object__populate_internal_map). It is also prone to break silently if features are added between map and BTF fd emits that possibly add more data (just ~128KB to break BTF fd, sinceinsn->offallows forINT16_MAX(32767) * 4 bytes).
I don't follow this logic.
Quoted 2 lines
Both of these issues are compounded by the fact that data map is shared by all programs, so it is easy to end up with invalid offset for BTF fd.
I don't follow this either. There is only one map and one program. What sharing are you talking about?
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v4 08/11] libbpf: Update gen_loader to emit BTF_KIND_FUNC relocations
Quoted 8 lines · Alexei Starovoitov
Quoted 6 lines · Kumar Kartikeya Dwivedi
This change updates the BPF syscall loader to relocate
BTF_KIND_FUNCrelocations, with support for weak kfunc relocations. The next commit adds bpftool supports to set up thefd_array_szparameter for light skeleton.
Quoted 5 lines
Quoted 2 lines
A second map for keeping fds is used instead of adding fds to existing loader.map because of following reasons:
but it complicates signing bpf progs a lot.
Can you explain this in short? (Just want to understand why it would be problem).
Quoted 8 lines
Quoted 4 lines
If reserving an area for map and BTF fds, we would waste the remaining of (
MAX_USED_MAPS+MAX_KFUNC_DESCS) * sizeof(int), which in most cases will be unused by the program. Also, we must place some limit on the amount of map and BTF fds a program can possibly open.That is just (256 + 64)*4 bytes of data. Really not much. I wouldn't worry about reserving this space.
Ok, I'll probably go with this now, I didn't realise a separate fd would be prohibitive for the signing case, so I thought it would nice to lift the limiation on number of map_fds by packing fd_array fds in another map.
Quoted 10 lines
Quoted 9 lines
If setting
gen->fd_arrayto firstmap_fdoffset, and then just finding the offset relative to this (for later BTF fds), such that they can be packed without wasting space, we run the risk of unnecessarily running out of valid offset foremit_relostage (for kfuncs), because gen map creation and relocation stages are separated by other steps that can add lots of data (includingbpf_object__populate_internal_map). It is also prone to break silently if features are added between map and BTF fd emits that possibly add more data (just ~128KB to break BTF fd, sinceinsn->offallows forINT16_MAX(32767) * 4 bytes).
Quoted 7 lines
I don't follow this logic.
Quoted 2 lines
Both of these issues are compounded by the fact that data map is shared by all programs, so it is easy to end up with invalid offset for BTF fd.
I don't follow this either. There is only one map and one program. What sharing are you talking about?
What I saw was that the sequence of calls is like this:bpf_gen__map_createadd_data - from first emit we add map_fd, we also store gen->fd_array then libbpf would call bpf_object__populate_internal_map which calls bpf_gen__map_update_elem, which also does add_data (can be of arbitrary sizes).
emit_relos happens relatively at the end.
For each program in the object, this sequence can be repeated, such that the add_data that we do in emit_relos, relative offset from gen->fd_array offset can end up becoming big enough (as all programs in object add data to same map), while gen->fd_array comes from first map creation.
However reserving an area works ok (like with the stack). Thanks.
--
Kartikeya
Re: [PATCH bpf-next v4 08/11] libbpf: Update gen_loader to emit BTF_KIND_FUNC relocations
Quoted 10 lines · Kumar Kartikeya Dwivedi
Quoted 8 lines · Alexei Starovoitov
Quoted 6 lines · Kumar Kartikeya Dwivedi
This change updates the BPF syscall loader to relocate
BTF_KIND_FUNCrelocations, with support for weak kfunc relocations. The next commit adds bpftool supports to set up thefd_array_szparameter for light skeleton.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
A second map for keeping fds is used instead of adding fds to existing loader.map because of following reasons:
but it complicates signing bpf progs a lot.
Can you explain this in short? (Just want to understand why it would be problem).
The signing idea (and light skeleton too) rely on two matching blocks:
signed map and signed prog that operates on this map.
They have to match and be technically part of single logical signature that consists of two pieces.
The second map doesn't quite fit this model. Especially since it's an empty map and it is there for temporary use during execution of the loader prog. That fd_array_sz value would somehow need to be part of the signature. Adding a 3rd non-generic component to a signature has consequences to the whole signing process.
The loader prog could have created this temp map on its own without asking bpf_load_and_run() to do it and without exposing it into a signature.
Anyway the signed bpf progs may get solved differently with the latest John proposal, but that's a different discussion.
The light skeleton minimalizm is its main advantage. Keeping it two
pieces: one map and one prog is its main selling point.
Quoted 38 lines
Quoted 8 lines
Quoted 4 lines
If reserving an area for map and BTF fds, we would waste the remaining of (
MAX_USED_MAPS+MAX_KFUNC_DESCS) * sizeof(int), which in most cases will be unused by the program. Also, we must place some limit on the amount of map and BTF fds a program can possibly open.That is just (256 + 64)*4 bytes of data. Really not much. I wouldn't worry about reserving this space.
Ok, I'll probably go with this now, I didn't realise a separate fd would be prohibitive for the signing case, so I thought it would nice to lift the limiation on number of
map_fdsby packingfd_arrayfds in another map.Quoted 10 lines
Quoted 9 lines
If setting
gen->fd_arrayto firstmap_fdoffset, and then just finding the offset relative to this (for later BTF fds), such that they can be packed without wasting space, we run the risk of unnecessarily running out of valid offset foremit_relostage (for kfuncs), because gen map creation and relocation stages are separated by other steps that can add lots of data (includingbpf_object__populate_internal_map). It is also prone to break silently if features are added between map and BTF fd emits that possibly add more data (just ~128KB to break BTF fd, sinceinsn->offallows forINT16_MAX(32767) * 4 bytes).Quoted 7 lines
I don't follow this logic.
Quoted 2 lines
Both of these issues are compounded by the fact that data map is shared by all programs, so it is easy to end up with invalid offset for BTF fd.
I don't follow this either. There is only one map and one program. What sharing are you talking about?
What I saw was that the sequence of calls is like this:
bpf_gen__map_createadd_data- from first emit we addmap_fd, we also storegen->fd_arraythen libbpf would callbpf_object__populate_internal_mapwhich callsbpf_gen__map_update_elem, which also doesadd_data(can be of arbitrary sizes).
Quoted 5 lines
emit_reloshappens relatively at the end.
For each program in the object, this sequence can be repeated, such that theadd_datathat we do inemit_relos, relative offset fromgen->fd_arrayoffset can end up becoming big enough (as all programs in object add data to same map), whilegen->fd_arraycomes from first map creation.
You've meant to use fd_array as a very very sparse array
with giant gaps between valid map_fds and btf_fds. Now I see it :) Indeed in such a case there is a risk of running out of 16-bit in bpf_insn->off. Reserving (256 + 64)*4 in the beginning of the data map should solve it, right? The loader prog can create a 2nd auxiliary map on the fly,
but it seems easier and simpler to just reserve this space in one and only map.
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v4 08/11] libbpf: Update gen_loader to emit BTF_KIND_FUNC relocations
Quoted 82 lines · Alexei Starovoitov
Quoted 10 lines · Kumar Kartikeya Dwivedi
Quoted 8 lines · Alexei Starovoitov
Quoted 6 lines · Kumar Kartikeya Dwivedi
This change updates the BPF syscall loader to relocate
BTF_KIND_FUNCrelocations, with support for weak kfunc relocations. The next commit adds bpftool supports to set up thefd_array_szparameter for light skeleton.Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
A second map for keeping fds is used instead of adding fds to existing loader.map because of following reasons:
but it complicates signing bpf progs a lot.
Can you explain this in short? (Just want to understand why it would be problem).
The signing idea (and light skeleton too) rely on two matching blocks:
signed map and signed prog that operates on this map.
They have to match and be technically part of single logical signature that consists of two pieces.
The second map doesn't quite fit this model. Especially since it's an empty map and it is there for temporary use during execution of the loader prog. Thatfd_array_szvalue would somehow need to be part of the signature. Adding a 3rd non-generic component to a signature has consequences to the whole signing process.
The loader prog could have created this temp map on its own without askingbpf_load_and_run()to do it and without exposing it into a signature.
Anyway the signed bpf progs may get solved differently with the latest John proposal, but that's a different discussion.
The light skeleton minimalizm is its main advantage. Keeping it two
pieces: one map and one prog is its main selling point.Quoted 38 lines
Quoted 8 lines
Quoted 4 lines
If reserving an area for map and BTF fds, we would waste the remaining of (
MAX_USED_MAPS+MAX_KFUNC_DESCS) * sizeof(int), which in most cases will be unused by the program. Also, we must place some limit on the amount of map and BTF fds a program can possibly open.That is just (256 + 64)*4 bytes of data. Really not much. I wouldn't worry about reserving this space.
Ok, I'll probably go with this now, I didn't realise a separate fd would be prohibitive for the signing case, so I thought it would nice to lift the limiation on number of
map_fdsby packingfd_arrayfds in another map.Quoted 10 lines
Quoted 9 lines
If setting
gen->fd_arrayto firstmap_fdoffset, and then just finding the offset relative to this (for later BTF fds), such that they can be packed without wasting space, we run the risk of unnecessarily running out of valid offset foremit_relostage (for kfuncs), because gen map creation and relocation stages are separated by other steps that can add lots of data (includingbpf_object__populate_internal_map). It is also prone to break silently if features are added between map and BTF fd emits that possibly add more data (just ~128KB to break BTF fd, sinceinsn->offallows forINT16_MAX(32767) * 4 bytes).Quoted 7 lines
I don't follow this logic.
Quoted 2 lines
Both of these issues are compounded by the fact that data map is shared by all programs, so it is easy to end up with invalid offset for BTF fd.
I don't follow this either. There is only one map and one program. What sharing are you talking about?
What I saw was that the sequence of calls is like this:
bpf_gen__map_createadd_data- from first emit we addmap_fd, we also storegen->fd_arraythen libbpf would callbpf_object__populate_internal_mapwhich callsbpf_gen__map_update_elem, which also doesadd_data(can be of arbitrary sizes).Quoted 5 lines
emit_reloshappens relatively at the end.
For each program in the object, this sequence can be repeated, such that theadd_datathat we do inemit_relos, relative offset fromgen->fd_arrayoffset can end up becoming big enough (as all programs in object add data to same map), whilegen->fd_arraycomes from first map creation.
Quoted 6 lines
You've meant to use
fd_arrayas a very very sparse array
with giant gaps between validmap_fdsandbtf_fds. Now I see it :) Indeed in such a case there is a risk of running out of 16-bit inbpf_insn->off. Reserving (256 + 64)*4 in the beginning of the data map should solve it, right? The loader prog can create a 2nd auxiliary map on the fly,
but it seems easier and simpler to just reserve this space in one and only map.
Thanks for the explanation! It makes sense. I will fix this in the next spin.
--
Kartikeya
Kumar Kartikeya Dwivedi
maintainerPATCH v4 9/11 tools: bpftool: Add separate fd_array map support for light skeleton
In reply to Kumar Kartikeya DwivediThis prepares bpftool to pass fd_array_sz parameter, so that the map can be created by bpf_load_and_run function to hold map and BTF fds.
--- tools/bpf/bpftool/gen.c | 3 ++- tools/bpf/bpftool/prog.c | 1 + 2 files changed, 3 insertions(+), 1 deletion(-)
tools/bpf/bpftool/gen.c +2 -1
@@ -532,10 +532,11 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *hcodegen ("\ \n\ +opts.fd_array_sz = %d; \n\ opts.insns_sz = %d; \n\ opts.insns = (void *)\"\\ \n\ " , - opts.insns_sz); + opts.fd_array_sz, opts.insns_sz);print_hex (opts.insns, opts.insns_sz); codegen("\ \n\
tools/bpf/bpftool/prog.c +1 -0
@@ -1693,6 +1693,7 @@ static int try_loader(struct gen_loader_opts *gen) opts.data_sz = gen->data_sz; opts.insns = gen->insns; opts.insns_sz = gen->insns_sz; + opts.fd_array_sz = gen->fd_array_sz; fds_before =count_open_fds (); err =bpf_load_and_run (&opts); fd_delta =count_open_fds () - fds_before;
Signature · 1 line
2.33.0
Kumar Kartikeya Dwivedi
maintainerPATCH v4 10/11 libbpf: Fix skel_internal.h to set errno on loader retval < 0
In reply to Kumar Kartikeya DwivediWhen the loader indicates an internal error (result of a checked bpf system call), it returns the result in attr.test.retval. However, tests that rely on ASSERT_OK_PTR on NULL (returned from light skeleton) may miss that NULL denotes an error if errno is set to 0. This would result in skel pointer being NULL, while ASSERT_OK_PTR returning 1, leading to a SEGV on dereference of skel, because libbpf_get_error relies on the assumption that errno is always set in case of error for ptr == NULL.
In particular, this was observed for the ksyms_module test. When executed using ./test_progs -t ksyms, prior tests manipulated errno and the test didn't crash when it failed at ksyms_module load, while using ./test_progs -t ksyms_module crashed due to errno being untouched.
--- tools/lib/bpf/skel_internal.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
tools/lib/bpf/skel_internal.h +4 -2
@@ -116,10 +116,12 @@ static inline int bpf_load_and_run(struct bpf_load_and_run_opts *opts) err =skel_sys_bpf (BPF_PROG_RUN , &attr,sizeof (attr));if (err <0 || (int )attr.test.retval <0 ) { opts->errstr ="failed to execute loader prog" ; -if (err <0 ) +if (err <0 ) { err = -errno; -else + }else { err = (int )attr.test.retval; + errno = -err; + } goto out; } err =0 ;
Signature · 1 line
2.33.0
Kumar Kartikeya Dwivedi
maintainerPATCH v4 11/11 bpf: selftests: Add selftests for module kfunc support
In reply to Kumar Kartikeya DwivediThis adds selftests that tests the success and failure path for modules kfuncs (in presence of invalid kfunc calls) for both libbpf and gen_loader. It also adds a prog_test kfunc_btf_id_list so that we can add module BTF ID set from bpf_testmod.
--- include/linux/btf.h | 2 + kernel/bpf/btf.c | 2 + net/bpf/test_run.c | 5 +- tools/testing/selftests/bpf/Makefile | 5 +- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 26 ++++++- .../selftests/bpf/prog_tests/ksyms_module.c | 52 ++++++++++---- .../bpf/prog_tests/ksyms_module_libbpf.c | 44 ++++++++++++ .../selftests/bpf/progs/test_ksyms_module.c | 41 ++++++++--- .../bpf/progs/test_ksyms_module_fail.c | 29 ++++++++ .../progs/test_ksyms_module_fail_toomany.c | 19 +++++ .../bpf/progs/test_ksyms_module_libbpf.c | 71 +++++++++++++++++++ .../bpf/progs/test_ksyms_module_util.h | 48 +++++++++++++ 12 files changed, 317 insertions(+), 27 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/ksyms_module_libbpf.c create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_module_fail.c create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_module_fail_toomany.c create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_module_libbpf.c create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_module_util.h
include/linux/btf.h +2 -0
@@ -270,7 +270,9 @@ static inline void unregister_kfunc_btf_id_set(struct kfunc_btf_id_list *l, THIS_MODULE } extern struct kfunc_btf_id_list bpf_tcp_ca_kfunc_list; +extern struct kfunc_btf_id_list prog_test_kfunc_list;DECLARE_CHECK_KFUNC_CALLBACK (bpf_tcp_ca); +DECLARE_CHECK_KFUNC_CALLBACK (prog_test);#endif
kernel/bpf/btf.c +2 -0
@@ -6397,3 +6397,5 @@ EXPORT_SYMBOL_GPL(unregister_kfunc_btf_id_set);DEFINE_KFUNC_BTF_ID_LIST (bpf_tcp_ca_kfunc_list);DEFINE_CHECK_KFUNC_CALLBACK (bpf_tcp_ca, bpf_tcp_ca_kfunc_list); +DEFINE_KFUNC_BTF_ID_LIST (prog_test_kfunc_list); +DEFINE_CHECK_KFUNC_CALLBACK (prog_test, prog_test_kfunc_list);
net/bpf/test_run.c +4 -1
@@ -2,6 +2,7 @@/* Copyright (c) 2017 Facebook */ #include <linux/bpf.h> +#include <linux/btf.h> #include <linux/btf_ids.h> #include <linux/slab.h> #include <linux/vmalloc.h>
@@ -243,7 +244,9 @@ BTF_SET_END(test_sk_kfunc_ids)bool bpf_prog_test_check_kfunc_call (u32 kfunc_id,struct module *owner) { -return btf_id_set_contains (&test_sk_kfunc_ids, kfunc_id); +if (btf_id_set_contains (&test_sk_kfunc_ids, kfunc_id)) +return true; +return __bpf_check_prog_test_kfunc_call (kfunc_id, owner); } static void *bpf_test_init(const union bpf_attr *kattr, u32 size,
tools/testing/selftests/bpf/Makefile +3 -2
@@ -174,6 +174,7 @@ $(OUTPUT)/bpf_testmod.ko: $(VMLINUX_BTF) $(wildcard bpf_testmod/Makefile bpf_tes $(Q)$(RM) bpf_testmod/bpf_testmod.ko # force re-compilation $(Q)$(MAKE) $(submake_extras) -C bpf_testmod $(Q)cp bpf_testmod/bpf_testmod.ko $@ + $(Q)$(RESOLVE_BTFIDS) -s ../../../../vmlinux bpf_testmod.ko $(OUTPUT)/test_stub.o: test_stub.c $(BPFOBJ) $(call msg,CC,,$@)
@@ -315,8 +316,8 @@ LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h \ linked_vars.skel.h linked_maps.skel.h LSKELS := kfunc_call_test.c fentry_test.c fexit_test.c fexit_sleep.c \ - test_ksyms_module.c test_ringbuf.c atomics.c trace_printk.c \ - trace_vprintk.c + test_ksyms_module.c test_ksyms_module_fail.c test_ksyms_module_fail_toomany.c \ + test_ringbuf.c atomics.c trace_printk.c trace_vprintk.c SKEL_BLACKLIST += $$(LSKELS) test_static_linked.skel.h-deps := test_static_linked1.o test_static_linked2.o
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +25 -1
@@ -1,5 +1,7 @@// SPDX-License-Identifier: GPL-2.0 /* Copyright (c) 2020 Facebook */ +#include <linux/btf.h> +#include <linux/btf_ids.h> #include <linux/error-injection.h> #include <linux/init.h> #include <linux/module.h>
@@ -10,9 +12,17 @@#define CREATE_TRACE_POINTS #include "bpf_testmod-events.h" +#include "../progs/test_ksyms_module_util.h" +KFUNC_DEFINE_VALID_DISTINCT_256 ;DEFINE_PER_CPU (int, bpf_testmod_ksym_percpu) =123 ; +noinline void +bpf_testmod_test_mod_kfunc (int i) +{ + *(int *)this_cpu_ptr (&bpf_testmod_ksym_percpu) = i; +} + noinline int bpf_testmod_loop_test(int n) {int i, sum =0 ;
@@ -71,13 +81,27 @@ static struct bin_attribute bin_attr_bpf_testmod_file __ro_after_init = { .write = bpf_testmod_test_write, }; +BTF_SET_START(bpf_testmod_kfunc_ids) +BTF_ID(func, bpf_testmod_test_mod_kfunc) +KFUNC_BTF_ID_VALID_DISTINCT_256 +BTF_SET_END(bpf_testmod_kfunc_ids) + +static DEFINE_KFUNC_BTF_ID_SET(&bpf_testmod_kfunc_ids, bpf_testmod_kfunc_btf_set); + static int bpf_testmod_init(void) { -return sysfs_create_bin_file (kernel_kobj, &bin_attr_bpf_testmod_file); + int ret; + + ret = sysfs_create_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file); + if (ret) + return ret; + register_kfunc_btf_id_set(&prog_test_kfunc_list, &bpf_testmod_kfunc_btf_set); + return 0; } static void bpf_testmod_exit(void) { + unregister_kfunc_btf_id_set(&prog_test_kfunc_list, &bpf_testmod_kfunc_btf_set); return sysfs_remove_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file); }
tools/testing/selftests/bpf/prog_tests/ksyms_module.c +38 -14
68 lines not on this page · raw message
tools/testing/selftests/bpf/prog_tests/ksyms_module_libbpf.c new file +44 -0
44 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_ksyms_module.c +32 -9
54 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_ksyms_module_fail.c new file +29 -0
@@ -0,0 +1,29 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "vmlinux.h" + +#include <bpf/bpf_helpers.h> + +extern const int bpf_testmod_ksym_percpu __ksym; +extern void bpf_testmod_test_mod_kfunc(int i) __ksym; +extern void bpf_testmod_invalid_mod_kfunc(void) __ksym __weak; + +int out_bpf_testmod_ksym =0 ; +const volatile int x =0 ; + +SEC("classifier") +int load (struct __sk_buff *skb) +{ +/* This should be preserved by clang, but not DCE'd by verifier, +* hence fail loading +*/ +if (!x) { +bpf_testmod_invalid_mod_kfunc (); +return -1 ; + } +bpf_testmod_test_mod_kfunc (42 ); + out_bpf_testmod_ksym = *(int *)bpf_this_cpu_ptr (&bpf_testmod_ksym_percpu); +return 0 ; +} + +char LICENSE[] SEC("license") = "GPL";
tools/testing/selftests/bpf/progs/test_ksyms_module_fail_toomany.c new file +19 -0
@@ -0,0 +1,19 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "vmlinux.h" + +#include <bpf/bpf_helpers.h> +#include "test_ksyms_module_util.h" + +KFUNC_KSYM_DECLARE_VALID_DISTINCT_256 ; +extern void bpf_testmod_test_mod_kfunc(int i) __ksym; + +SEC("classifier") +int load (struct __sk_buff *skb) +{ +KFUNC_VALID_DISTINCT_256 ; +KFUNC_VALID_SAME_ONE ; +return 0 ; +} + +char LICENSE[] SEC("license") = "GPL";
tools/testing/selftests/bpf/progs/test_ksyms_module_libbpf.c new file +71 -0
71 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_ksyms_module_util.h new file +48 -0
48 lines not on this page · raw message
Signature · 1 line
2.33.0
Re: [PATCH bpf-next v4 11/11] bpf: selftests: Add selftests for module kfunc support
Quoted 27 lines · Kumar Kartikeya Dwivedi
This adds selftests that tests the success and failure path for modules kfuncs (in presence of invalid kfunc calls) for both libbpf and
gen_loader. It also adds aprog_testkfunc_btf_id_listso that we can add module BTF ID set frombpf_testmod.Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- include/linux/btf.h | 2 + kernel/bpf/btf.c | 2 + net/bpf/test_run.c | 5 +- tools/testing/selftests/bpf/Makefile | 5 +- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 26 ++++++- .../selftests/bpf/prog_tests/ksyms_module.c | 52 ++++++++++---- .../bpf/prog_tests/ksyms_module_libbpf.c | 44 ++++++++++++ .../selftests/bpf/progs/test_ksyms_module.c | 41 ++++++++--- .../bpf/progs/test_ksyms_module_fail.c | 29 ++++++++ .../progs/test_ksyms_module_fail_toomany.c | 19 +++++ .../bpf/progs/test_ksyms_module_libbpf.c | 71 +++++++++++++++++++ .../bpf/progs/test_ksyms_module_util.h | 48 +++++++++++++ 12 files changed, 317 insertions(+), 27 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/ksyms_module_libbpf.c create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_module_fail.c create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_module_fail_toomany.c create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_module_libbpf.c create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_module_util.h
[...]
Quoted 16 lines
net/bpf/test_run.c +3
-1@@ -243,7 +244,9 @@ BTF_SET_END(test_sk_kfunc_ids)bool bpf_prog_test_check_kfunc_call (u32 kfunc_id,struct module *owner) { -return btf_id_set_contains (&test_sk_kfunc_ids, kfunc_id); +if (btf_id_set_contains (&test_sk_kfunc_ids, kfunc_id)) +return true; +return __bpf_check_prog_test_kfunc_call (kfunc_id, owner); } static void *bpf_test_init(const union bpf_attr *kattr, u32 size,tools/testing/selftests/bpf/Makefile +0
-0
tools/testing/selftests/bpf/Makefile $(OUTPUT)/bpf_testmod.ko: $(VMLINUX_BTF) $(wildcard bpf_testmod/Makefile bpf_tes · line 177
Quoted 4 lines
tools/testing/selftests/bpf/Makefile +1
-0@@ -174,6 +174,7 @@ $(OUTPUT)/bpf_testmod.ko: $(VMLINUX_BTF) $(wildcard bpf_testmod/Makefile bpf_tes $(Q)$(RM) bpf_testmod/bpf_testmod.ko # force re-compilation $(Q)$(MAKE) $(submake_extras) -C bpf_testmod $(Q)cp bpf_testmod/bpf_testmod.ko $@ + $(Q)$(RESOLVE_BTFIDS) -s ../../../../vmlinux bpf_testmod.ko
$(VMLINUX_BTF) instead of "../../../../vmlinux", it will break
Quoted 13 lines
tools/testing/selftests/bpf/Makefile +2
-2$(OUTPUT)/test_stub.o: test_stub.c $(BPFOBJ) $(call msg,CC,,$@)@@ -315,8 +316,8 @@ LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h \ linked_vars.skel.h linked_maps.skel.h LSKELS := kfunc_call_test.c fentry_test.c fexit_test.c fexit_sleep.c \ - test_ksyms_module.c test_ringbuf.c atomics.c trace_printk.c \ - trace_vprintk.c + test_ksyms_module.c test_ksyms_module_fail.c test_ksyms_module_fail_toomany.c \ + test_ringbuf.c atomics.c trace_printk.c trace_vprintk.c SKEL_BLACKLIST += $$(LSKELS)
[...]
Quoted 43 lines
tools/testing/selftests/bpf/progs/test_ksyms_module_util.h new file +37
-0@@ -0,0 +1,48 @@ +// SPDX-License-Identifier: GPL-2.0 +#ifndef __KSYMS_MODULE_UTIL_H__ +#define __KSYMS_MODULE_UTIL_H__ + +#define __KFUNC_NR_EXP (Y ) \ +Y(0) Y(1) Y(2) Y(3) Y(4) Y(5) Y(6) Y(7) Y(8) Y(9) Y(10) Y(11) Y(12) \ +Y(13) Y(14) Y(15) Y(16) Y(17) Y(18) Y(19) Y(20) Y(21) Y(22) Y(23) \ +Y(24) Y(25) Y(26) Y(27) Y(28) Y(29) Y(30) Y(31) Y(32) Y(33) Y(34) \ +Y(35) Y(36) Y(37) Y(38) Y(39) Y(40) Y(41) Y(42) Y(43) Y(44) Y(45) \ +Y(46) Y(47) Y(48) Y(49) Y(50) Y(51) Y(52) Y(53) Y(54) Y(55) Y(56) \ +Y(57) Y(58) Y(59) Y(60) Y(61) Y(62) Y(63) Y(64) Y(65) Y(66) Y(67) \ +Y(68) Y(69) Y(70) Y(71) Y(72) Y(73) Y(74) Y(75) Y(76) Y(77) Y(78) \ +Y(79) Y(80) Y(81) Y(82) Y(83) Y(84) Y(85) Y(86) Y(87) Y(88) Y(89) \ +Y(90) Y(91) Y(92) Y(93) Y(94) Y(95) Y(96) Y(97) Y(98) Y(99) Y(100) \ +Y(101) Y(102) Y(103) Y(104) Y(105) Y(106) Y(107) Y(108) Y(109) Y(110) \ +Y(111) Y(112) Y(113) Y(114) Y(115) Y(116) Y(117) Y(118) Y(119) Y(120) \ +Y(121) Y(122) Y(123) Y(124) Y(125) Y(126) Y(127) Y(128) Y(129) Y(130) \ +Y(131) Y(132) Y(133) Y(134) Y(135) Y(136) Y(137) Y(138) Y(139) Y(140) \ +Y(141) Y(142) Y(143) Y(144) Y(145) Y(146) Y(147) Y(148) Y(149) Y(150) \ +Y(151) Y(152) Y(153) Y(154) Y(155) Y(156) Y(157) Y(158) Y(159) Y(160) \ +Y(161) Y(162) Y(163) Y(164) Y(165) Y(166) Y(167) Y(168) Y(169) Y(170) \ +Y(171) Y(172) Y(173) Y(174) Y(175) Y(176) Y(177) Y(178) Y(179) Y(180) \ +Y(181) Y(182) Y(183) Y(184) Y(185) Y(186) Y(187) Y(188) Y(189) Y(190) \ +Y(191) Y(192) Y(193) Y(194) Y(195) Y(196) Y(197) Y(198) Y(199) Y(200) \ +Y(201) Y(202) Y(203) Y(204) Y(205) Y(206) Y(207) Y(208) Y(209) Y(210) \ +Y(211) Y(212) Y(213) Y(214) Y(215) Y(216) Y(217) Y(218) Y(219) Y(220) \ +Y(221) Y(222) Y(223) Y(224) Y(225) Y(226) Y(227) Y(228) Y(229) Y(230) \ +Y(231) Y(232) Y(233) Y(234) Y(235) Y(236) Y(237) Y(238) Y(239) Y(240) \ +Y(241) Y(242) Y(243) Y(244) Y(245) Y(246) Y(247) Y(248) Y(249) Y(250) \ +Y(251) Y(252) Y(253) Y(254) Y(255) + +#define __KFUNC_A (nr) bpf_testmod_test_mod_kfunc_##nr(); +#define KFUNC_VALID_DISTINCT_256 __KFUNC_NR_EXP(__KFUNC_A) + +#define __KFUNC_B (nr) extern void bpf_testmod_test_mod_kfunc_##nr(void) __ksym; +#define KFUNC_KSYM_DECLARE_VALID_DISTINCT_256 __KFUNC_NR_EXP(__KFUNC_B) +
tools/testing/selftests/bpf/progs/test_ksyms_module_util.h line 47
Quoted 10 lines
tools/testing/selftests/bpf/progs/test_ksyms_module_util.h new file +10
-0+#define __KFUNC_C (nr) noinline void bpf_testmod_test_mod_kfunc_##nr(void) {}; +#define KFUNC_DEFINE_VALID_DISTINCT_256 __KFUNC_NR_EXP(__KFUNC_C) + +#define __KFUNC_D (nr) BTF_ID(func, bpf_testmod_test_mod_kfunc_##nr) +#define KFUNC_BTF_ID_VALID_DISTINCT_256 __KFUNC_NR_EXP(__KFUNC_D) + +#define __KFUNC_E (nr) bpf_testmod_test_mod_kfunc(nr); +#define KFUNC_VALID_SAME_ONE __KFUNC_E(0) +#define KFUNC_VALID_SAME_256 __KFUNC_NR_EXP(__KFUNC_E) +
This is pretty horrible... Wouldn't it be better to test limits like this using the test_verifier approach, where we can craft a *short* sequence of instructions that will test all these limits?...
Quoted 4 lines
tools/testing/selftests/bpf/progs/test_ksyms_module_util.h new file +1
-1+#endif --2.33.0
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v4 11/11] bpf: selftests: Add selftests for module kfunc support
Quoted 118 lines · Andrii Nakryiko
Quoted 27 lines · Kumar Kartikeya Dwivedi
This adds selftests that tests the success and failure path for modules kfuncs (in presence of invalid kfunc calls) for both libbpf and
gen_loader. It also adds aprog_testkfunc_btf_id_listso that we can add module BTF ID set frombpf_testmod.Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- include/linux/btf.h | 2 + kernel/bpf/btf.c | 2 + net/bpf/test_run.c | 5 +- tools/testing/selftests/bpf/Makefile | 5 +- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 26 ++++++- .../selftests/bpf/prog_tests/ksyms_module.c | 52 ++++++++++---- .../bpf/prog_tests/ksyms_module_libbpf.c | 44 ++++++++++++ .../selftests/bpf/progs/test_ksyms_module.c | 41 ++++++++--- .../bpf/progs/test_ksyms_module_fail.c | 29 ++++++++ .../progs/test_ksyms_module_fail_toomany.c | 19 +++++ .../bpf/progs/test_ksyms_module_libbpf.c | 71 +++++++++++++++++++ .../bpf/progs/test_ksyms_module_util.h | 48 +++++++++++++ 12 files changed, 317 insertions(+), 27 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/ksyms_module_libbpf.c create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_module_fail.c create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_module_fail_toomany.c create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_module_libbpf.c create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_module_util.h[...]
Quoted 16 lines
net/bpf/test_run.c +3
-1@@ -243,7 +244,9 @@ BTF_SET_END(test_sk_kfunc_ids)bool bpf_prog_test_check_kfunc_call (u32 kfunc_id,struct module *owner) { -return btf_id_set_contains (&test_sk_kfunc_ids, kfunc_id); +if (btf_id_set_contains (&test_sk_kfunc_ids, kfunc_id)) +return true; +return __bpf_check_prog_test_kfunc_call (kfunc_id, owner); } static void *bpf_test_init(const union bpf_attr *kattr, u32 size,tools/testing/selftests/bpf/Makefile +0
-0tools/testing/selftests/bpf/Makefile $(OUTPUT)/bpf_testmod.ko: $(VMLINUX_BTF) $(wildcard bpf_testmod/Makefile bpf_tes · line 177
Quoted 4 lines
tools/testing/selftests/bpf/Makefile +1
-0@@ -174,6 +174,7 @@ $(OUTPUT)/bpf_testmod.ko: $(VMLINUX_BTF) $(wildcard bpf_testmod/Makefile bpf_tes $(Q)$(RM) bpf_testmod/bpf_testmod.ko # force re-compilation $(Q)$(MAKE) $(submake_extras) -C bpf_testmod $(Q)cp bpf_testmod/bpf_testmod.ko $@ + $(Q)$(RESOLVE_BTFIDS) -s ../../../../vmlinux bpf_testmod.ko$(VMLINUX_BTF) instead of "../../../../vmlinux", it will break
Quoted 13 lines
tools/testing/selftests/bpf/Makefile +2
-2$(OUTPUT)/test_stub.o: test_stub.c $(BPFOBJ) $(call msg,CC,,$@)@@ -315,8 +316,8 @@ LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h \ linked_vars.skel.h linked_maps.skel.h LSKELS := kfunc_call_test.c fentry_test.c fexit_test.c fexit_sleep.c \ - test_ksyms_module.c test_ringbuf.c atomics.c trace_printk.c \ - trace_vprintk.c + test_ksyms_module.c test_ksyms_module_fail.c test_ksyms_module_fail_toomany.c \ + test_ringbuf.c atomics.c trace_printk.c trace_vprintk.c SKEL_BLACKLIST += $$(LSKELS)[...]
Quoted 47 lines
tools/testing/selftests/bpf/progs/test_ksyms_module_util.h new file +41
-0@@ -0,0 +1,48 @@ +// SPDX-License-Identifier: GPL-2.0 +#ifndef __KSYMS_MODULE_UTIL_H__ +#define __KSYMS_MODULE_UTIL_H__ + +#define __KFUNC_NR_EXP (Y ) \ +Y(0) Y(1) Y(2) Y(3) Y(4) Y(5) Y(6) Y(7) Y(8) Y(9) Y(10) Y(11) Y(12) \ +Y(13) Y(14) Y(15) Y(16) Y(17) Y(18) Y(19) Y(20) Y(21) Y(22) Y(23) \ +Y(24) Y(25) Y(26) Y(27) Y(28) Y(29) Y(30) Y(31) Y(32) Y(33) Y(34) \ +Y(35) Y(36) Y(37) Y(38) Y(39) Y(40) Y(41) Y(42) Y(43) Y(44) Y(45) \ +Y(46) Y(47) Y(48) Y(49) Y(50) Y(51) Y(52) Y(53) Y(54) Y(55) Y(56) \ +Y(57) Y(58) Y(59) Y(60) Y(61) Y(62) Y(63) Y(64) Y(65) Y(66) Y(67) \ +Y(68) Y(69) Y(70) Y(71) Y(72) Y(73) Y(74) Y(75) Y(76) Y(77) Y(78) \ +Y(79) Y(80) Y(81) Y(82) Y(83) Y(84) Y(85) Y(86) Y(87) Y(88) Y(89) \ +Y(90) Y(91) Y(92) Y(93) Y(94) Y(95) Y(96) Y(97) Y(98) Y(99) Y(100) \ +Y(101) Y(102) Y(103) Y(104) Y(105) Y(106) Y(107) Y(108) Y(109) Y(110) \ +Y(111) Y(112) Y(113) Y(114) Y(115) Y(116) Y(117) Y(118) Y(119) Y(120) \ +Y(121) Y(122) Y(123) Y(124) Y(125) Y(126) Y(127) Y(128) Y(129) Y(130) \ +Y(131) Y(132) Y(133) Y(134) Y(135) Y(136) Y(137) Y(138) Y(139) Y(140) \ +Y(141) Y(142) Y(143) Y(144) Y(145) Y(146) Y(147) Y(148) Y(149) Y(150) \ +Y(151) Y(152) Y(153) Y(154) Y(155) Y(156) Y(157) Y(158) Y(159) Y(160) \ +Y(161) Y(162) Y(163) Y(164) Y(165) Y(166) Y(167) Y(168) Y(169) Y(170) \ +Y(171) Y(172) Y(173) Y(174) Y(175) Y(176) Y(177) Y(178) Y(179) Y(180) \ +Y(181) Y(182) Y(183) Y(184) Y(185) Y(186) Y(187) Y(188) Y(189) Y(190) \ +Y(191) Y(192) Y(193) Y(194) Y(195) Y(196) Y(197) Y(198) Y(199) Y(200) \ +Y(201) Y(202) Y(203) Y(204) Y(205) Y(206) Y(207) Y(208) Y(209) Y(210) \ +Y(211) Y(212) Y(213) Y(214) Y(215) Y(216) Y(217) Y(218) Y(219) Y(220) \ +Y(221) Y(222) Y(223) Y(224) Y(225) Y(226) Y(227) Y(228) Y(229) Y(230) \ +Y(231) Y(232) Y(233) Y(234) Y(235) Y(236) Y(237) Y(238) Y(239) Y(240) \ +Y(241) Y(242) Y(243) Y(244) Y(245) Y(246) Y(247) Y(248) Y(249) Y(250) \ +Y(251) Y(252) Y(253) Y(254) Y(255) + +#define __KFUNC_A (nr) bpf_testmod_test_mod_kfunc_##nr(); +#define KFUNC_VALID_DISTINCT_256 __KFUNC_NR_EXP(__KFUNC_A) + +#define __KFUNC_B (nr) extern void bpf_testmod_test_mod_kfunc_##nr(void) __ksym; +#define KFUNC_KSYM_DECLARE_VALID_DISTINCT_256 __KFUNC_NR_EXP(__KFUNC_B) + +#define __KFUNC_C (nr) noinline void bpf_testmod_test_mod_kfunc_##nr(void) {}; +#define KFUNC_DEFINE_VALID_DISTINCT_256 __KFUNC_NR_EXP(__KFUNC_C) + +#define __KFUNC_D (nr) BTF_ID(func, bpf_testmod_test_mod_kfunc_##nr)
Quoted 8 lines
tools/testing/selftests/bpf/progs/test_ksyms_module_util.h line 47
Quoted 6 lines
tools/testing/selftests/bpf/progs/test_ksyms_module_util.h new file +6
-0+#define KFUNC_BTF_ID_VALID_DISTINCT_256 __KFUNC_NR_EXP(__KFUNC_D) + +#define __KFUNC_E (nr) bpf_testmod_test_mod_kfunc(nr); +#define KFUNC_VALID_SAME_ONE __KFUNC_E(0) +#define KFUNC_VALID_SAME_256 __KFUNC_NR_EXP(__KFUNC_E) +This is pretty horrible... Wouldn't it be better to test limits like
Yeah, I actually thought about this a bit yesterday, we could also do:
(untested)
#define X_0(x)
#define X_1(x) x X_0(x)
#define X_2(x) x X_1(x)
#define X_3(x) x X_2(x)
#define X_4(x) x X_3(x)
#define X_5(x) x X_4(x)
#define X_6(x) x X_5(x)
#define X_7(x) x X_6(x)
#define X_8(x) x X_7(x)
#define X_9(x) x X_8(x)
#define X_10(x) x X_9(x)
Then, for generating 256 items
X_2(X_10(X_10(foo))) X_5(X_10(foo)) X_6(foo)
... which looks much better.
Quoted 3 lines
this using the
test_verifierapproach, where we can craft a *short* sequence of instructions that will test all these limits?...
Hmm, good idea, I'd just need to fill in the BTF id dynamically at runtime, but that should be possible.
Though we still need to craft distinct calls (I am trying to test the limit where insn->off is different for each case). Since we try to reuse index in both gen_loader and libbpf, just generating same call 256 times would not be enough.
Let me know which one of the two you prefer.
Quoted 5 lines
Quoted 5 lines
tools/testing/selftests/bpf/progs/test_ksyms_module_util.h new file +1
-1+#endif --2.33.0
--
Kartikeya
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v4 11/11] bpf: selftests: Add selftests for module kfunc support
Quoted 143 lines · Kumar Kartikeya Dwivedi
Quoted 118 lines · Andrii Nakryiko
Quoted 27 lines · Kumar Kartikeya Dwivedi
This adds selftests that tests the success and failure path for modules kfuncs (in presence of invalid kfunc calls) for both libbpf and
gen_loader. It also adds aprog_testkfunc_btf_id_listso that we can add module BTF ID set frombpf_testmod.Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- include/linux/btf.h | 2 + kernel/bpf/btf.c | 2 + net/bpf/test_run.c | 5 +- tools/testing/selftests/bpf/Makefile | 5 +- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 26 ++++++- .../selftests/bpf/prog_tests/ksyms_module.c | 52 ++++++++++---- .../bpf/prog_tests/ksyms_module_libbpf.c | 44 ++++++++++++ .../selftests/bpf/progs/test_ksyms_module.c | 41 ++++++++--- .../bpf/progs/test_ksyms_module_fail.c | 29 ++++++++ .../progs/test_ksyms_module_fail_toomany.c | 19 +++++ .../bpf/progs/test_ksyms_module_libbpf.c | 71 +++++++++++++++++++ .../bpf/progs/test_ksyms_module_util.h | 48 +++++++++++++ 12 files changed, 317 insertions(+), 27 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/ksyms_module_libbpf.c create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_module_fail.c create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_module_fail_toomany.c create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_module_libbpf.c create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_module_util.h[...]
Quoted 16 lines
net/bpf/test_run.c +3
-1@@ -243,7 +244,9 @@ BTF_SET_END(test_sk_kfunc_ids)bool bpf_prog_test_check_kfunc_call (u32 kfunc_id,struct module *owner) { -return btf_id_set_contains (&test_sk_kfunc_ids, kfunc_id); +if (btf_id_set_contains (&test_sk_kfunc_ids, kfunc_id)) +return true; +return __bpf_check_prog_test_kfunc_call (kfunc_id, owner); } static void *bpf_test_init(const union bpf_attr *kattr, u32 size,tools/testing/selftests/bpf/Makefile +0
-0tools/testing/selftests/bpf/Makefile $(OUTPUT)/bpf_testmod.ko: $(VMLINUX_BTF) $(wildcard bpf_testmod/Makefile bpf_tes · line 177
Quoted 4 lines
tools/testing/selftests/bpf/Makefile +1
-0@@ -174,6 +174,7 @@ $(OUTPUT)/bpf_testmod.ko: $(VMLINUX_BTF) $(wildcard bpf_testmod/Makefile bpf_tes $(Q)$(RM) bpf_testmod/bpf_testmod.ko # force re-compilation $(Q)$(MAKE) $(submake_extras) -C bpf_testmod $(Q)cp bpf_testmod/bpf_testmod.ko $@ + $(Q)$(RESOLVE_BTFIDS) -s ../../../../vmlinux bpf_testmod.ko$(VMLINUX_BTF) instead of "../../../../vmlinux", it will break
Quoted 13 lines
tools/testing/selftests/bpf/Makefile +2
-2$(OUTPUT)/test_stub.o: test_stub.c $(BPFOBJ) $(call msg,CC,,$@)@@ -315,8 +316,8 @@ LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h \ linked_vars.skel.h linked_maps.skel.h LSKELS := kfunc_call_test.c fentry_test.c fexit_test.c fexit_sleep.c \ - test_ksyms_module.c test_ringbuf.c atomics.c trace_printk.c \ - trace_vprintk.c + test_ksyms_module.c test_ksyms_module_fail.c test_ksyms_module_fail_toomany.c \ + test_ringbuf.c atomics.c trace_printk.c trace_vprintk.c SKEL_BLACKLIST += $$(LSKELS)[...]
Quoted 47 lines
tools/testing/selftests/bpf/progs/test_ksyms_module_util.h new file +41
-0@@ -0,0 +1,48 @@ +// SPDX-License-Identifier: GPL-2.0 +#ifndef __KSYMS_MODULE_UTIL_H__ +#define __KSYMS_MODULE_UTIL_H__ + +#define __KFUNC_NR_EXP (Y ) \ +Y(0) Y(1) Y(2) Y(3) Y(4) Y(5) Y(6) Y(7) Y(8) Y(9) Y(10) Y(11) Y(12) \ +Y(13) Y(14) Y(15) Y(16) Y(17) Y(18) Y(19) Y(20) Y(21) Y(22) Y(23) \ +Y(24) Y(25) Y(26) Y(27) Y(28) Y(29) Y(30) Y(31) Y(32) Y(33) Y(34) \ +Y(35) Y(36) Y(37) Y(38) Y(39) Y(40) Y(41) Y(42) Y(43) Y(44) Y(45) \ +Y(46) Y(47) Y(48) Y(49) Y(50) Y(51) Y(52) Y(53) Y(54) Y(55) Y(56) \ +Y(57) Y(58) Y(59) Y(60) Y(61) Y(62) Y(63) Y(64) Y(65) Y(66) Y(67) \ +Y(68) Y(69) Y(70) Y(71) Y(72) Y(73) Y(74) Y(75) Y(76) Y(77) Y(78) \ +Y(79) Y(80) Y(81) Y(82) Y(83) Y(84) Y(85) Y(86) Y(87) Y(88) Y(89) \ +Y(90) Y(91) Y(92) Y(93) Y(94) Y(95) Y(96) Y(97) Y(98) Y(99) Y(100) \ +Y(101) Y(102) Y(103) Y(104) Y(105) Y(106) Y(107) Y(108) Y(109) Y(110) \ +Y(111) Y(112) Y(113) Y(114) Y(115) Y(116) Y(117) Y(118) Y(119) Y(120) \ +Y(121) Y(122) Y(123) Y(124) Y(125) Y(126) Y(127) Y(128) Y(129) Y(130) \ +Y(131) Y(132) Y(133) Y(134) Y(135) Y(136) Y(137) Y(138) Y(139) Y(140) \ +Y(141) Y(142) Y(143) Y(144) Y(145) Y(146) Y(147) Y(148) Y(149) Y(150) \ +Y(151) Y(152) Y(153) Y(154) Y(155) Y(156) Y(157) Y(158) Y(159) Y(160) \ +Y(161) Y(162) Y(163) Y(164) Y(165) Y(166) Y(167) Y(168) Y(169) Y(170) \ +Y(171) Y(172) Y(173) Y(174) Y(175) Y(176) Y(177) Y(178) Y(179) Y(180) \ +Y(181) Y(182) Y(183) Y(184) Y(185) Y(186) Y(187) Y(188) Y(189) Y(190) \ +Y(191) Y(192) Y(193) Y(194) Y(195) Y(196) Y(197) Y(198) Y(199) Y(200) \ +Y(201) Y(202) Y(203) Y(204) Y(205) Y(206) Y(207) Y(208) Y(209) Y(210) \ +Y(211) Y(212) Y(213) Y(214) Y(215) Y(216) Y(217) Y(218) Y(219) Y(220) \ +Y(221) Y(222) Y(223) Y(224) Y(225) Y(226) Y(227) Y(228) Y(229) Y(230) \ +Y(231) Y(232) Y(233) Y(234) Y(235) Y(236) Y(237) Y(238) Y(239) Y(240) \ +Y(241) Y(242) Y(243) Y(244) Y(245) Y(246) Y(247) Y(248) Y(249) Y(250) \ +Y(251) Y(252) Y(253) Y(254) Y(255) + +#define __KFUNC_A (nr) bpf_testmod_test_mod_kfunc_##nr(); +#define KFUNC_VALID_DISTINCT_256 __KFUNC_NR_EXP(__KFUNC_A) + +#define __KFUNC_B (nr) extern void bpf_testmod_test_mod_kfunc_##nr(void) __ksym; +#define KFUNC_KSYM_DECLARE_VALID_DISTINCT_256 __KFUNC_NR_EXP(__KFUNC_B) + +#define __KFUNC_C (nr) noinline void bpf_testmod_test_mod_kfunc_##nr(void) {}; +#define KFUNC_DEFINE_VALID_DISTINCT_256 __KFUNC_NR_EXP(__KFUNC_C) + +#define __KFUNC_D (nr) BTF_ID(func, bpf_testmod_test_mod_kfunc_##nr)Quoted 8 lines
tools/testing/selftests/bpf/progs/test_ksyms_module_util.h line 47
Quoted 6 lines
tools/testing/selftests/bpf/progs/test_ksyms_module_util.h new file +6
-0+#define KFUNC_BTF_ID_VALID_DISTINCT_256 __KFUNC_NR_EXP(__KFUNC_D) + +#define __KFUNC_E (nr) bpf_testmod_test_mod_kfunc(nr); +#define KFUNC_VALID_SAME_ONE __KFUNC_E(0) +#define KFUNC_VALID_SAME_256 __KFUNC_NR_EXP(__KFUNC_E) +This is pretty horrible... Wouldn't it be better to test limits like
Yeah, I actually thought about this a bit yesterday, we could also do:
(untested)#define
X_0(x)
#defineX_1(x)xX_0(x)
#defineX_2(x)xX_1(x)
#defineX_3(x)xX_2(x)
#defineX_4(x)xX_3(x)
#defineX_5(x)xX_4(x)
#defineX_6(x)xX_5(x)
#defineX_7(x)xX_6(x)
#defineX_8(x)xX_7(x)
#defineX_9(x)xX_8(x)
#defineX_10(x)xX_9(x)
Quoted 4 lines
Then, for generating 256 items
X_2(X_10(X_10(foo))) X_5(X_10(foo))
X_6(foo)
Hmm, one problem with this is I cannot find a good way to attach an incrementing token to paste to each expansion, so that they become foo0, foo1, ..., it only works well for the simple case.
Unless someone knows a trick to make that work somehow, I guess test_verifier is the only option.
Quoted 16 lines
... which looks much better.
Quoted 3 lines
this using the
test_verifierapproach, where we can craft a *short* sequence of instructions that will test all these limits?...Hmm, good idea, I'd just need to fill in the BTF id dynamically at runtime, but that should be possible.
Though we still need to craft distinct calls (I am trying to test the limit where
insn->offis different for each case). Since we try to reuse index in bothgen_loaderand libbpf, just generating same call 256 times would not be enough.Let me know which one of the two you prefer.
Quoted 1 line
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
tools/testing/selftests/bpf/progs/test_ksyms_module_util.h new file +1
-1+#endif --2.33.0
--
Kartikeya
--
Kartikeya
Re: [PATCH bpf-next v4 11/11] bpf: selftests: Add selftests for module kfunc support
In reply to Kumar Kartikeya DwivediQuoted 154 lines · Kumar Kartikeya Dwivedi
Quoted 118 lines · Andrii Nakryiko
Quoted 27 lines · Kumar Kartikeya Dwivedi
This adds selftests that tests the success and failure path for modules kfuncs (in presence of invalid kfunc calls) for both libbpf and
gen_loader. It also adds aprog_testkfunc_btf_id_listso that we can add module BTF ID set frombpf_testmod.Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- include/linux/btf.h | 2 + kernel/bpf/btf.c | 2 + net/bpf/test_run.c | 5 +- tools/testing/selftests/bpf/Makefile | 5 +- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 26 ++++++- .../selftests/bpf/prog_tests/ksyms_module.c | 52 ++++++++++---- .../bpf/prog_tests/ksyms_module_libbpf.c | 44 ++++++++++++ .../selftests/bpf/progs/test_ksyms_module.c | 41 ++++++++--- .../bpf/progs/test_ksyms_module_fail.c | 29 ++++++++ .../progs/test_ksyms_module_fail_toomany.c | 19 +++++ .../bpf/progs/test_ksyms_module_libbpf.c | 71 +++++++++++++++++++ .../bpf/progs/test_ksyms_module_util.h | 48 +++++++++++++ 12 files changed, 317 insertions(+), 27 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/ksyms_module_libbpf.c create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_module_fail.c create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_module_fail_toomany.c create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_module_libbpf.c create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_module_util.h[...]
Quoted 16 lines
net/bpf/test_run.c +3
-1@@ -243,7 +244,9 @@ BTF_SET_END(test_sk_kfunc_ids)bool bpf_prog_test_check_kfunc_call (u32 kfunc_id,struct module *owner) { -return btf_id_set_contains (&test_sk_kfunc_ids, kfunc_id); +if (btf_id_set_contains (&test_sk_kfunc_ids, kfunc_id)) +return true; +return __bpf_check_prog_test_kfunc_call (kfunc_id, owner); } static void *bpf_test_init(const union bpf_attr *kattr, u32 size,tools/testing/selftests/bpf/Makefile +0
-0tools/testing/selftests/bpf/Makefile $(OUTPUT)/bpf_testmod.ko: $(VMLINUX_BTF) $(wildcard bpf_testmod/Makefile bpf_tes · line 177
Quoted 4 lines
tools/testing/selftests/bpf/Makefile +1
-0@@ -174,6 +174,7 @@ $(OUTPUT)/bpf_testmod.ko: $(VMLINUX_BTF) $(wildcard bpf_testmod/Makefile bpf_tes $(Q)$(RM) bpf_testmod/bpf_testmod.ko # force re-compilation $(Q)$(MAKE) $(submake_extras) -C bpf_testmod $(Q)cp bpf_testmod/bpf_testmod.ko $@ + $(Q)$(RESOLVE_BTFIDS) -s ../../../../vmlinux bpf_testmod.ko$(VMLINUX_BTF) instead of "../../../../vmlinux", it will break
Quoted 13 lines
tools/testing/selftests/bpf/Makefile +2
-2$(OUTPUT)/test_stub.o: test_stub.c $(BPFOBJ) $(call msg,CC,,$@)@@ -315,8 +316,8 @@ LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h \ linked_vars.skel.h linked_maps.skel.h LSKELS := kfunc_call_test.c fentry_test.c fexit_test.c fexit_sleep.c \ - test_ksyms_module.c test_ringbuf.c atomics.c trace_printk.c \ - trace_vprintk.c + test_ksyms_module.c test_ksyms_module_fail.c test_ksyms_module_fail_toomany.c \ + test_ringbuf.c atomics.c trace_printk.c trace_vprintk.c SKEL_BLACKLIST += $$(LSKELS)[...]
Quoted 47 lines
tools/testing/selftests/bpf/progs/test_ksyms_module_util.h new file +41
-0@@ -0,0 +1,48 @@ +// SPDX-License-Identifier: GPL-2.0 +#ifndef __KSYMS_MODULE_UTIL_H__ +#define __KSYMS_MODULE_UTIL_H__ + +#define __KFUNC_NR_EXP (Y ) \ +Y(0) Y(1) Y(2) Y(3) Y(4) Y(5) Y(6) Y(7) Y(8) Y(9) Y(10) Y(11) Y(12) \ +Y(13) Y(14) Y(15) Y(16) Y(17) Y(18) Y(19) Y(20) Y(21) Y(22) Y(23) \ +Y(24) Y(25) Y(26) Y(27) Y(28) Y(29) Y(30) Y(31) Y(32) Y(33) Y(34) \ +Y(35) Y(36) Y(37) Y(38) Y(39) Y(40) Y(41) Y(42) Y(43) Y(44) Y(45) \ +Y(46) Y(47) Y(48) Y(49) Y(50) Y(51) Y(52) Y(53) Y(54) Y(55) Y(56) \ +Y(57) Y(58) Y(59) Y(60) Y(61) Y(62) Y(63) Y(64) Y(65) Y(66) Y(67) \ +Y(68) Y(69) Y(70) Y(71) Y(72) Y(73) Y(74) Y(75) Y(76) Y(77) Y(78) \ +Y(79) Y(80) Y(81) Y(82) Y(83) Y(84) Y(85) Y(86) Y(87) Y(88) Y(89) \ +Y(90) Y(91) Y(92) Y(93) Y(94) Y(95) Y(96) Y(97) Y(98) Y(99) Y(100) \ +Y(101) Y(102) Y(103) Y(104) Y(105) Y(106) Y(107) Y(108) Y(109) Y(110) \ +Y(111) Y(112) Y(113) Y(114) Y(115) Y(116) Y(117) Y(118) Y(119) Y(120) \ +Y(121) Y(122) Y(123) Y(124) Y(125) Y(126) Y(127) Y(128) Y(129) Y(130) \ +Y(131) Y(132) Y(133) Y(134) Y(135) Y(136) Y(137) Y(138) Y(139) Y(140) \ +Y(141) Y(142) Y(143) Y(144) Y(145) Y(146) Y(147) Y(148) Y(149) Y(150) \ +Y(151) Y(152) Y(153) Y(154) Y(155) Y(156) Y(157) Y(158) Y(159) Y(160) \ +Y(161) Y(162) Y(163) Y(164) Y(165) Y(166) Y(167) Y(168) Y(169) Y(170) \ +Y(171) Y(172) Y(173) Y(174) Y(175) Y(176) Y(177) Y(178) Y(179) Y(180) \ +Y(181) Y(182) Y(183) Y(184) Y(185) Y(186) Y(187) Y(188) Y(189) Y(190) \ +Y(191) Y(192) Y(193) Y(194) Y(195) Y(196) Y(197) Y(198) Y(199) Y(200) \ +Y(201) Y(202) Y(203) Y(204) Y(205) Y(206) Y(207) Y(208) Y(209) Y(210) \ +Y(211) Y(212) Y(213) Y(214) Y(215) Y(216) Y(217) Y(218) Y(219) Y(220) \ +Y(221) Y(222) Y(223) Y(224) Y(225) Y(226) Y(227) Y(228) Y(229) Y(230) \ +Y(231) Y(232) Y(233) Y(234) Y(235) Y(236) Y(237) Y(238) Y(239) Y(240) \ +Y(241) Y(242) Y(243) Y(244) Y(245) Y(246) Y(247) Y(248) Y(249) Y(250) \ +Y(251) Y(252) Y(253) Y(254) Y(255) + +#define __KFUNC_A (nr) bpf_testmod_test_mod_kfunc_##nr(); +#define KFUNC_VALID_DISTINCT_256 __KFUNC_NR_EXP(__KFUNC_A) + +#define __KFUNC_B (nr) extern void bpf_testmod_test_mod_kfunc_##nr(void) __ksym; +#define KFUNC_KSYM_DECLARE_VALID_DISTINCT_256 __KFUNC_NR_EXP(__KFUNC_B) + +#define __KFUNC_C (nr) noinline void bpf_testmod_test_mod_kfunc_##nr(void) {}; +#define KFUNC_DEFINE_VALID_DISTINCT_256 __KFUNC_NR_EXP(__KFUNC_C) + +#define __KFUNC_D (nr) BTF_ID(func, bpf_testmod_test_mod_kfunc_##nr)Quoted 8 lines
tools/testing/selftests/bpf/progs/test_ksyms_module_util.h line 47
Quoted 6 lines
tools/testing/selftests/bpf/progs/test_ksyms_module_util.h new file +6
-0+#define KFUNC_BTF_ID_VALID_DISTINCT_256 __KFUNC_NR_EXP(__KFUNC_D) + +#define __KFUNC_E (nr) bpf_testmod_test_mod_kfunc(nr); +#define KFUNC_VALID_SAME_ONE __KFUNC_E(0) +#define KFUNC_VALID_SAME_256 __KFUNC_NR_EXP(__KFUNC_E) +This is pretty horrible... Wouldn't it be better to test limits like
Yeah, I actually thought about this a bit yesterday, we could also do:
(untested)#define
X_0(x)
#defineX_1(x)xX_0(x)
#defineX_2(x)xX_1(x)
#defineX_3(x)xX_2(x)
#defineX_4(x)xX_3(x)
#defineX_5(x)xX_4(x)
#defineX_6(x)xX_5(x)
#defineX_7(x)xX_6(x)
#defineX_8(x)xX_7(x)
#defineX_9(x)xX_8(x)
#defineX_10(x)xX_9(x)Then, for generating 256 items
X_2(X_10(X_10(foo))) X_5(X_10(foo))
X_6(foo)... which looks much better.
Quoted 3 lines
this using the
test_verifierapproach, where we can craft a *short* sequence of instructions that will test all these limits?...
Quoted 6 lines
Hmm, good idea, I'd just need to fill in the BTF id dynamically at runtime, but that should be possible.
Though we still need to craft distinct calls (I am trying to test the limit where
insn->offis different for each case). Since we try to reuse index in bothgen_loaderand libbpf, just generating same call 256 times would not be enough.
You just need to generate one instruction with offset = 257 to test this. And separately one call with fd_array that has module BTF fd at fd_array[256] (to check that 256 is ok). Or am I missing something?
Quoted 11 lines
Let me know which one of the two you prefer.
Quoted 5 lines
Quoted 5 lines
tools/testing/selftests/bpf/progs/test_ksyms_module_util.h new file +1
-1+#endif --2.33.0
--
Kartikeya
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v4 11/11] bpf: selftests: Add selftests for module kfunc support
Quoted 4 lines · Andrii Nakryiko
Quoted 4 lines
[...]
Hmm, good idea, I'd just need to fill in the BTF id dynamically at runtime, but that should be possible.
Quoted 8 lines
Quoted 3 lines
Though we still need to craft distinct calls (I am trying to test the limit where
insn->offis different for each case). Since we try to reuse index in bothgen_loaderand libbpf, just generating same call 256 times would not be enough.You just need to generate one instruction with offset = 257 to test this. And separately one call with
fd_arraythat has module BTF fd at fd_array[256] (to check that 256 is ok). Or am I missing something?
That won't be enough, if I just pass insn->imm = id, insn->off = 257, it becomes first descriptor in kfunc_tab and kfunc_btf_tab. The total limit is 256, and they are kept in sorted order by based on id and off for the first, off for the second. So 256 different offs are needed (imm may be same actually), so that both fill up.
--
Kartikeya
Re: [PATCH bpf-next v4 11/11] bpf: selftests: Add selftests for module kfunc support
Quoted 15 lines · Kumar Kartikeya Dwivedi
Quoted 4 lines · Andrii Nakryiko
Quoted 4 lines
[...]
Hmm, good idea, I'd just need to fill in the BTF id dynamically at runtime, but that should be possible.Quoted 8 lines
Quoted 3 lines
Though we still need to craft distinct calls (I am trying to test the limit where
insn->offis different for each case). Since we try to reuse index in bothgen_loaderand libbpf, just generating same call 256 times would not be enough.You just need to generate one instruction with offset = 257 to test this. And separately one call with
fd_arraythat has module BTF fd at fd_array[256] (to check that 256 is ok). Or am I missing something?
Quoted 5 lines
That won't be enough, if I just pass
insn->imm= id,insn->off= 257, it becomes first descriptor inkfunc_tabandkfunc_btf_tab. The total limit is 256, and they are kept in sorted order by based on id and off for the first, off for the second. So 256 different offs are needed (imm may be same actually), so that both fill up.
Just to test the 256 limit? I don't think it's necessary. afaik there is no test that exercises the 64 map limit.
Quoted 9 lines · Kumar Kartikeya Dwivedi
This set enables kernel module function calls, and also modifies verifier logic to permit invalid kernel function calls as long as they are pruned as part of dead code elimination. This is done to provide better runtime portability for BPF objects, which can conditionally disable parts of code that are pruned later by the verifier (e.g. const volatile vars, kconfig options). libbpf modifications are made along with kernel changes to support module function calls. The set includes
gen_loadersupport for emitting kfunc relocations.
Quoted 6 lines
It also converts TCP congestion control objects to use the module kfunc support instead of relying on
IS_BUILTINifdef.Changelog:
----------
v3 -> v4
Please use vmtest.sh locally to test everything. That should help to avoid breaking our CI ([0]):
test_ksyms_module_libbpf:PASS:test_ksyms_module_libbpf__open 0 nsec test_ksyms_module_libbpf:PASS:bpf_program__set_autoload falseload_fail1 0 nsec
test_ksyms_module_libbpf:PASS:bpf_program__set_autoload falseload_fail2 0 nsec
libbpf: load bpf program failed: Invalid argument
libbpf: -- BEGIN DUMP LOG ---
libbpf:
kernel btf_id 81786 is not a function
processed 0 insns (limit 1000000) max_states_per_insn 0 total_states
0 peak_states 0 mark_read 0
libbpf: -- END LOG --
libbpf: failed to load program 'handler'
libbpf: failed to load object 'test_ksyms_module_libbpf'
libbpf: failed to load BPF skeleton 'test_ksyms_module_libbpf': -4007 test_ksyms_module_libbpf:FAIL:test_ksyms_module_libbpf__load
unexpected error: -4007 (errno 4007)
#66 ksyms_module_libbpf:FAIL
test_module_attach:PASS:skel_open 0 nsec test_module_attach:PASS:set_attach_target 0 nsec
libbpf: load bpf program failed: Invalid argument
libbpf: -- BEGIN DUMP LOG ---
libbpf:
attach_btf_id 81768 is invalid
processed 0 insns (limit 1000000) max_states_per_insn 0 total_states
0 peak_states 0 mark_read 0
libbpf: -- END LOG --
libbpf: failed to load program 'handle_tp_btf'
libbpf: failed to load object 'test_module_attach'
libbpf: failed to load BPF skeleton 'test_module_attach': -4007 test_module_attach:FAIL:skel_load failed to load skeleton #81 module_attach:FAIL
[0] https://github.com/kernel-patches/bpf/pull/1807/checks?check_run_id=3652765027
Quoted 86 lines
v3: https://lore.kernel.org/bpf/[email protected]
* Address comments from Alexei
* DropMAX_BPF_STACKchange, instead movemap_fdand BTF fd to BPF array map and passfd_arrayusingBPF_PSEUDO_MAP_IDX_VALUE
* Address comments from Andrii
* Fix selftest to store to variable for observing function call instead of printk and polluting CI logs
* Drop use ofraw_tpfor testing, instead reuse classifier basedprog_test_run
* Drop index + 1 basedinsn->offconvention for kfunc module calls
* Expand selftests to cover more corner cases
* Misc cleanupsv2 -> v3
v2: https://lore.kernel.org/bpf/[email protected]* Fix issues pointed out by Kernel Test Robot
* Fixfind_kfunc_descto also take offset into consideration when comparingRFC v1 -> v2
v1: https://lore.kernel.org/bpf/[email protected]* Address comments from Alexei
* Reusefd_arrayinstead of introducingkfunc_btf_fdsarray
* Take btf and module reference as needed, instead of preloading
* AddBTF_KIND_FUNCrelocation support togen_loaderinfrastructure
* Address comments from Andrii
* Drop hashmap in libbpf for finding index of existing BTF infd_array
* Preserve invalid kfunc calls only when the symbol is weak
* Adjust verifier selftestsKumar Kartikeya Dwivedi (11):
bpf: Introduce BPF support for kernel module function calls
bpf: Be conservative while processing invalid kfunc calls
bpf: btf: Introduce helpers for dynamic BTF set registration
tools: Allow specifying base BTF file inresolve_btfids
bpf: Enable TCP congestion control kfunc from modules
libbpf: Support kernel module function calls
libbpf: Resolve invalid weak kfunc calls with imm = 0, off = 0
libbpf: Updategen_loaderto emitBTF_KIND_FUNCrelocations
tools: bpftool: Add separatefd_arraymap support for light skeleton
libbpf: Fixskel_internal.hto set errno on loader retval < 0
bpf: selftests: Add selftests for module kfunc supportinclude/linux/bpf.h | 8 +- include/linux/bpf_verifier.h | 2 + include/linux/bpfptr.h | 1 + include/linux/btf.h | 37 +++ kernel/bpf/btf.c | 56 +++++ kernel/bpf/core.c | 4 + kernel/bpf/verifier.c | 220 ++++++++++++++--- net/bpf/test_run.c | 7 +- net/ipv4/bpf_tcp_ca.c | 36 +-- net/ipv4/tcp_bbr.c | 28 ++- net/ipv4/tcp_cubic.c | 26 +- net/ipv4/tcp_dctcp.c | 26 +- scripts/Makefile.modfinal | 1 + tools/bpf/bpftool/gen.c | 3 +- tools/bpf/bpftool/prog.c | 1 + tools/bpf/resolve_btfids/main.c | 19 +- tools/lib/bpf/bpf.c | 1 + tools/lib/bpf/bpf_gen_internal.h | 16 +- tools/lib/bpf/gen_loader.c | 222 +++++++++++++++--- tools/lib/bpf/libbpf.c | 83 +++++-- tools/lib/bpf/libbpf.h | 1 + tools/lib/bpf/libbpf_internal.h | 1 + tools/lib/bpf/skel_internal.h | 33 ++- tools/testing/selftests/bpf/Makefile | 5 +- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 26 +- .../selftests/bpf/prog_tests/ksyms_module.c | 52 ++-- .../bpf/prog_tests/ksyms_module_libbpf.c | 44 ++++ .../selftests/bpf/progs/test_ksyms_module.c | 41 +++- .../bpf/progs/test_ksyms_module_fail.c | 29 +++ .../progs/test_ksyms_module_fail_toomany.c | 19 ++ .../bpf/progs/test_ksyms_module_libbpf.c | 71 ++++++ .../bpf/progs/test_ksyms_module_util.h | 48 ++++ 32 files changed, 1014 insertions(+), 153 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/ksyms_module_libbpf.c create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_module_fail.c create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_module_fail_toomany.c create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_module_libbpf.c create mode 100644 tools/testing/selftests/bpf/progs/test_ksyms_module_util.h--
2.33.0
Kumar Kartikeya Dwivedi
maintainerQuoted 14 lines · Andrii Nakryiko
Quoted 12 lines · Kumar Kartikeya Dwivedi
This set enables kernel module function calls, and also modifies verifier logic to permit invalid kernel function calls as long as they are pruned as part of dead code elimination. This is done to provide better runtime portability for BPF objects, which can conditionally disable parts of code that are pruned later by the verifier (e.g. const volatile vars, kconfig options). libbpf modifications are made along with kernel changes to support module function calls. The set includes
gen_loadersupport for emitting kfunc relocations.It also converts TCP congestion control objects to use the module kfunc support instead of relying on
IS_BUILTINifdef.
Quoted 7 lines
Quoted 3 lines
Changelog:
----------
v3 -> v4Please use
vmtest.shlocally to test everything. That should help to avoid breaking our CI ([0]):
I'll look into it. There were some problems getting it to run on Arch and Fedora, so I instead tested in my own VM (GLIBC_2.33 not found).
Surprisingly, everything passed when I ran it before sending. e.g. I still get this for the two failing tests in CI.
[root@(none) bpf]# ./test_progs -t ksyms_module_libbpf #66 ksyms_module_libbpf:OK
Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED [root@(none) bpf]# ./test_progs -t module_attach #81 module_attach:OK
Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED
--
Kartikeya