libbpf: Support kernel-side kfunc variants
Subsystems (8)
Ihor Solodrai
reviewerv2 cover libbpf: Support kernel-side kfunc variants
A kfunc prototype change breaks BPF programs built against the old prototype. For example, 8fe4dc4f6456 ("bpf: change prototype of bpf_session_{cookie,is_return}") made this change in v7.0:
before: bool bpf_session_is_return(void); v7.0: bool bpf_session_is_return(void *ctx);
A program built before v7.0 declares bpf_session_is_return(void), and libbpf can't resolve it on v7.0. The kernel has no way to keep the old prototype available to such programs. sched_ext works around the same problem with ___v2___compat names in compat.bpf.h.
With this series, the kernel can keep the old prototype as a variant:
bool bpf_session_is_return(void *ctx); bool bpf_session_is_return___v1(void);
and libbpf binds each declaration to the function with a matching BTF prototype.
The new lookup changes what some existing declarations resolve to.
A replay of kfunc resolution against v6.18, v7.2 and bpf-next BTF covered 1302 BPF objects: 1174 sched_ext schedulers from Meta's internal builds, and 128 public objects from the scx repo, tools/sched_ext, the selftests and bpftrace. It also covered 16 objects declaring every kfunc from each x86 vmlinux.h in the scx repo's history, counted here as public. No strong declaration changes. These __weak declarations change, counted in objects per kernel:
public Meta
lost on v7.2 and bpf-next, no candidate:
scx_bpf_dsq_insert___v2___compat 36 136
scx_bpf_dsq_move_to_local___v2___compat 33 27
scx_bpf_reenqueue_local___v2___compat 9 44
lost on v7.2 and bpf-next, struct names differ:
bpf_io_uring_get_region(struct io_ring_ctx *) 1 0
bpf_io_uring_submit_sqes(struct io_ring_ctx *) 1 0 lost on bpf-next, struct names differ:
bpf_cpumask_populate(struct cpumask *) 8 88
lost on v6.18 and v7.2, struct names differ:
bpf_cpumask_populate(struct bpf_cpumask *) 4 0
gained on v7.2 and bpf-next:
scx_bpf_dsq_move_to_local___v2(u64, u64) 7 120
scx_bpf_dsq_insert___v2(...) 8 0
scx_bpf_reenqueue_local___v2(void) 8 0In-tree compat.bpf.h now tries the plain ___v2 names first. Objects that gain a ___v2 kfunc now call it instead of their fallback path.
---
v1->v2:
* Pick a kfunc by prototype among all funcs with the same essential name, without preferring the exact name (Andrii, Eduard).
* Compare struct and union names, ignoring ___ suffixes, instead of using bpf_core_types_are_compat(), so that variants can differ only in a pointee type (Eduard, Andrii).
* Drop the alias exception that kept ___v2___compat names working (Eduard). New patch 2 updates compat.bpf.h instead.
* resolve_btfids: check registered kfuncs within vmlinux or a module with libbpf's new matcher and report every matching pair. New patch #1 shares libbpf internals with it instead of wrapping them (Eduard).
* Call kernel-side ___ functions "variants". "Flavor" stays CO-RE's term for local suffixes.
* Rewrite the docs for the new rules.
v1: https://lore.kernel.org/bpf/[email protected]/
---
Ihor Solodrai (6):
libbpf: Separate interfaces shared with in-tree tools
sched_ext: Prefer ___v2 kfunc names in compat.bpf.h
libbpf: Resolve kfunc variants by prototype selftests/bpf: Test kfunc variant selection by prototype
resolve_btfids: Reject ambiguous kfunc variants docs/bpf: Describe how to change a kfunc's prototype
Documentation/bpf/kfuncs.rst | 96 ++++++++++++++ tools/bpf/bpftool/Makefile | 9 +- tools/bpf/resolve_btfids/btf_colors.c | 15 +-- tools/bpf/resolve_btfids/btf_colors.h | 4 - tools/bpf/resolve_btfids/main.c | 74 ++++++++++- tools/lib/bpf/btf.c | 99 ++++++++++++++- tools/lib/bpf/libbpf.c | 117 ++++++++++++------ tools/lib/bpf/libbpf_internal.h | 32 +---- tools/lib/bpf/libbpf_tools.h | 48 +++++++ tools/sched_ext/include/scx/compat.bpf.h | 39 ++++-- .../selftests/bpf/prog_tests/verifier.c | 3 + .../selftests/bpf/progs/kfunc_variants.c | 32 +++++ .../selftests/bpf/test_kmods/bpf_testmod.c | 12 ++ .../bpf/test_kmods/bpf_testmod_kfunc.h | 3 + 14 files changed, 481 insertions(+), 102 deletions(-) create mode 100644 tools/lib/bpf/libbpf_tools.h create mode 100644 tools/testing/selftests/bpf/progs/kfunc_variants.c
base-commit: 82cc2b4383e3edfa1026c8612fb4e90a9329ba11
Signature · 1 line
2.56.1
Ihor Solodrai
reviewerPATCH v2 1/6 libbpf: Separate interfaces shared with in-tree tools
resolve_btfids links libbpf statically and uses its BTF header accessor, field iterator and SHA-256 helper. libbpf_internal.h also defines logging macros and poisons kernel integer typedefs, so resolve_btfids wraps the hash helper in a separate source file.
Move the shared declarations to libbpf_tools.h and use them directly in resolve_btfids. The header is for in-tree tools and stays uninstalled. No shared-library symbols change.
No functional change.
--- tools/bpf/bpftool/Makefile | 9 +++--- tools/bpf/resolve_btfids/btf_colors.c | 15 ++------- tools/bpf/resolve_btfids/btf_colors.h | 4 --- tools/bpf/resolve_btfids/main.c | 5 +-- tools/lib/bpf/libbpf_internal.h | 30 +---------------- tools/lib/bpf/libbpf_tools.h | 46 +++++++++++++++++++++++++++ 6 files changed, 58 insertions(+), 51 deletions(-) create mode 100644 tools/lib/bpf/libbpf_tools.h
tools/bpf/bpftool/Makefile +5 -4
@@ -28,10 +28,11 @@ LIBBPF_BOOTSTRAP_INCLUDE := $(LIBBPF_BOOTSTRAP_DESTDIR)include LIBBPF_BOOTSTRAP_HDRS_DIR := $(LIBBPF_BOOTSTRAP_INCLUDE)/bpf LIBBPF_BOOTSTRAP := $(LIBBPF_BOOTSTRAP_OUTPUT)libbpf.a -# We need to copy hashmap.h, nlattr.h, relo_core.h and libbpf_internal.h -# which are not otherwise exported by libbpf, but still required by bpftool. -LIBBPF_INTERNAL_HDRS := $(addprefix $(LIBBPF_HDRS_DIR)/,hashmap.h nlattr.h relo_core.h libbpf_internal.h) -LIBBPF_BOOTSTRAP_INTERNAL_HDRS := $(addprefix $(LIBBPF_BOOTSTRAP_HDRS_DIR)/,hashmap.h relo_core.h libbpf_internal.h) +# Stage private headers that bpftool uses and libbpf does not install. +LIBBPF_COMMON_INTERNAL_HDRS := hashmap.h relo_core.h libbpf_internal.h libbpf_tools.h +LIBBPF_INTERNAL_HDRS := $(addprefix $(LIBBPF_HDRS_DIR)/,$(LIBBPF_COMMON_INTERNAL_HDRS) nlattr.h) +LIBBPF_BOOTSTRAP_INTERNAL_HDRS := $(addprefix $(LIBBPF_BOOTSTRAP_HDRS_DIR)/,\ + $(LIBBPF_COMMON_INTERNAL_HDRS)) $(LIBBPF_OUTPUT) $(BOOTSTRAP_OUTPUT) $(LIBBPF_BOOTSTRAP_OUTPUT) $(LIBBPF_HDRS_DIR) $(LIBBPF_BOOTSTRAP_HDRS_DIR): $(QUIET_MKDIR)mkdir -p $@
tools/bpf/resolve_btfids/btf_colors.c +3 -12
@@ -1,18 +1,9 @@// SPDX-License-Identifier: GPL-2.0-only -#include <bpf/libbpf_internal.h> +#include <errno.h> +#include <stdlib.h> +#include <bpf/libbpf_tools.h> #include "btf_colors.h" -/* -* Functions in this file mainly exist to refer to functions from libbpf_internal.h, -* which can't be included in main.c because of the u32 poison and pr_warn macro conflicts. -*/ - -void btf_sha256 (const void *data,size_t len, -__u8 out[BTF_SHA256_DIGEST_LENGTH ]) -{ -libbpf_sha256 (data, len, out); -} -/* * Marks `root_id` and local types reachable from it with `color`. * `colors` is indexed by source ID; worklist has room for every local type.
tools/bpf/resolve_btfids/btf_colors.h +0 -4
@@ -11,10 +11,6 @@ enum btf_color { BTF_COLOR_SHARED = BTF_COLOR_MAIN | BTF_COLOR_LOC, }; -#define BTF_SHA256_DIGEST_LENGTH 32 - -void btf_sha256 (const void *data,size_t len, -__u8 out[BTF_SHA256_DIGEST_LENGTH ]);void btf_mark_reachable (struct btf *btf,__u32 root,enum btf_color color,__u8 *colors,__u32 *worklist);
tools/bpf/resolve_btfids/main.c +3 -2
@@ -93,12 +93,14 @@#include <linux/limits.h> #include <bpf/btf.h> #include <bpf/libbpf.h> +#include <bpf/libbpf_tools.h> #include "btf_colors.h" #include "fmodret.h" #include <subcmd/parse-options.h> #define BTF_IDS_SECTION ".BTF_ids"#define BTF_LINK_MODULE_NAME_MAX 64 +#define BTF_SHA256_DIGEST_LENGTH SHA256_DIGEST_LENGTH#define BTF_ID_PREFIX "__BTF_ID__"#define BTF_STRUCT "struct"
@@ -1205,8 +1207,7 @@ static int patch_btf_link(struct object *obj, const struct btf_link *link) }memset (data->d_buf,0 , data->d_size);memcpy (data->d_buf, link->module,strlen (link->module) +1 ); -btf_sha256 (raw_btf_data, raw_btf_size, - (u8 *)data->d_buf + module_name_len); +libbpf_sha256 (raw_btf_data, raw_btf_size, (u8 *)data->d_buf + module_name_len); size = (u8 *)data->d_buf + module_name_len +BTF_SHA256_DIGEST_LENGTH ;if (obj->efile.encoding ==ELFDATA2LSB ) { size[0 ] = raw_btf_size;
tools/lib/bpf/libbpf_internal.h +1 -29
@@ -18,6 +18,7 @@#include <unistd.h> #include <sys/syscall.h> #include <libelf.h> +#include "libbpf_tools.h" #include "relo_core.h" /* Android's libc doesn't support AT_EACCESS in faccessat() implementation
@@ -247,7 +248,6 @@ struct btf_type;struct btf_type *btf_type_by_id (const struct btf *btf,__u32 type_id);const char *btf_kind_str (const struct btf_type *t);const struct btf_type *skip_mods_and_typedefs (const struct btf *btf,__u32 id,__u32 *res_id); -const struct btf_header *btf_header (const struct btf *btf);void btf_set_base_btf (struct btf *btf,const struct btf *base_btf);int btf_relocate (struct btf *btf,const struct btf *base_btf,__u32 **id_map,__u32 **str_map);
@@ -574,31 +574,6 @@ static inline void bpf_core_relo_bswap(struct bpf_core_relo *i) i->kind =bswap_32 (i->kind); } -enum btf_field_iter_kind { -BTF_FIELD_ITER_IDS , -BTF_FIELD_ITER_STRS , -}; - -struct btf_field_desc { -/* once-per-type offsets */ -int t_off_cnt, t_offs[2 ]; -/* member struct size, or zero, if no members */ -int m_sz; -/* repeated per-member offsets */ -int m_off_cnt, m_offs[2 ]; -}; - -struct btf_field_iter { -struct btf_field_desc desc; -void *p; -int m_idx; -int off_idx; -int vlen; -}; - -int btf_field_iter_init (struct btf_field_iter *it,struct btf_type *t,enum btf_field_iter_kind iter_kind); -__u32 *btf_field_iter_next (struct btf_field_iter *it); -typedef int (*type_id_visit_fn )(__u32 *type_id,void *ctx);typedef int (*str_off_visit_fn )(__u32 *str_off,void *ctx);int btf_ext_visit_type_ids (struct btf_ext *btf_ext,type_id_visit_fn visit,void *ctx);
@@ -772,8 +747,5 @@ int elf_resolve_pattern_offsets(const char *binary_path, const char *pattern,int probe_fd (int fd); -#define SHA256_DIGEST_LENGTH 32 - -void libbpf_sha256 (const void *data,size_t len,__u8 out[SHA256_DIGEST_LENGTH ]);int probe_sys_bpf_ext (void );#endif /* __LIBBPF_LIBBPF_INTERNAL_H */
tools/lib/bpf/libbpf_tools.h new file +46 -0
46 lines not on this page · raw message
Signature · 1 line
2.56.1
Ihor Solodrai
reviewerPATCH v2 2/6 sched_ext: Prefer ___v2 kfunc names in compat.bpf.h
In reply to Ihor Solodraicompat.bpf.h declares scx_bpf_dsq_insert___v2___compat and similar names. libbpf drops the last ___ suffix of a declaration and looks up the rest exactly, so these resolve the kernel's ___v2 kfuncs.
The next patch makes libbpf drop the last suffix of kernel function names too and select a variant by prototype. The ___v2___compat names then resolve nothing, and the wrappers silently fall back to the ___v1 kfuncs. For example, scx_bpf_dsq_move_to_local() drops enq_flags.
Try the ___v2 names first. libbpf with that patch resolves them by prototype. Older libbpf leaves them unresolved, because the unsuffixed kfunc has a different prototype, and still resolves the ___v2___compat names that follow.
--- tools/sched_ext/include/scx/compat.bpf.h | 39 ++++++++++++++++-------- 1 file changed, 26 insertions(+), 13 deletions(-)
tools/sched_ext/include/scx/compat.bpf.h +26 -13
@@ -29,8 +29,10 @@ struct cgroup *scx_bpf_task_cgroup___new(struct task_struct *p) __ksym __weak;* scx_bpf_dispatch_from_dsq() and friends were added during v6.12 by * 4c30f5ce4f7a ("sched_ext: Implement scx_bpf_dispatch[_vtime]_from_dsq()"). * -* v7.1: scx_bpf_dsq_move_to_local___v2() to add @enq_flags. +* v7.1: scx_bpf_dsq_move_to_local___v2() to add @enq_flags. Older libbpf can +* resolve it only through the extra ___compat suffix. */ +bool scx_bpf_dsq_move_to_local___v2(u64 dsq_id, u64 enq_flags) __ksym __weak; bool scx_bpf_dsq_move_to_local___v2___compat(u64 dsq_id, u64 enq_flags) __ksym __weak; bool scx_bpf_dsq_move_to_local___v1(u64 dsq_id) __ksym __weak; void scx_bpf_dsq_move_set_slice___new(struct bpf_iter_scx_dsq *it__iter, u64 slice) __ksym __weak;
@@ -45,11 +47,13 @@ bool scx_bpf_dispatch_from_dsq___old(struct bpf_iter_scx_dsq *it__iter, struct t bool scx_bpf_dispatch_vtime_from_dsq___old(struct bpf_iter_scx_dsq *it__iter, struct task_struct *p, u64 dsq_id, u64 enq_flags) __ksym __weak;#define scx_bpf_dsq_move_to_local (dsq_id, enq_flags) \ - (bpf_ksym_exists(scx_bpf_dsq_move_to_local___v2___compat) ? \ - scx_bpf_dsq_move_to_local___v2___compat((dsq_id), (enq_flags)) : \ - (bpf_ksym_exists(scx_bpf_dsq_move_to_local___v1) ? \ - scx_bpf_dsq_move_to_local___v1((dsq_id)) : \ - scx_bpf_consume___old((dsq_id)))) + (bpf_ksym_exists(scx_bpf_dsq_move_to_local___v2) ? \ + scx_bpf_dsq_move_to_local___v2((dsq_id), (enq_flags)) : \ + (bpf_ksym_exists(scx_bpf_dsq_move_to_local___v2___compat) ? \ + scx_bpf_dsq_move_to_local___v2___compat((dsq_id), (enq_flags)) : \ + (bpf_ksym_exists(scx_bpf_dsq_move_to_local___v1) ? \ + scx_bpf_dsq_move_to_local___v1((dsq_id)) : \ + scx_bpf_consume___old((dsq_id)))))#define scx_bpf_dsq_move_set_slice (it__iter, slice) \ (bpf_ksym_exists(scx_bpf_dsq_move_set_slice___new) ? \
@@ -430,12 +434,13 @@ scx_bpf_dsq_insert_vtime(struct task_struct *p, u64 dsq_id, u64 slice, u64 vtime/* * v6.19: scx_bpf_dsq_insert() now returns bool instead of void. Move * scx_bpf_dsq_insert() decl to common.bpf.h and drop compat helper after v6.22. -* The extra ___compat suffix is to work around libbpf not ignoring __SUFFIX on -* kernel side. The entire suffix can be dropped later. +* Older libbpf can resolve scx_bpf_dsq_insert___v2() only through the extra +* ___compat suffix. * * v6.13: scx_bpf_dsq_insert() renaming is also handled here. See the block on * dispatch renaming above for more details. */ +bool scx_bpf_dsq_insert___v2(struct task_struct *p, u64 dsq_id, u64 slice, u64 enq_flags) __ksym __weak; bool scx_bpf_dsq_insert___v2___compat(struct task_struct *p, u64 dsq_id, u64 slice, u64 enq_flags) __ksym __weak; void scx_bpf_dsq_insert___v1(struct task_struct *p, u64 dsq_id, u64 slice, u64 enq_flags) __ksym __weak; void scx_bpf_dispatch___compat(struct task_struct *p, u64 dsq_id, u64 slice, u64 enq_flags) __ksym __weak;
@@ -443,7 +448,9 @@ void scx_bpf_dispatch___compat(struct task_struct *p, u64 dsq_id, u64 slice, u64static inline bool scx_bpf_dsq_insert (struct task_struct *p,u64 dsq_id,u64 slice,u64 enq_flags) { -if (bpf_ksym_exists (scx_bpf_dsq_insert___v2___compat)) { +if (bpf_ksym_exists (scx_bpf_dsq_insert___v2)) { +return scx_bpf_dsq_insert___v2 (p, dsq_id, slice, enq_flags); + }else if (bpf_ksym_exists (scx_bpf_dsq_insert___v2___compat)) {return scx_bpf_dsq_insert___v2___compat (p, dsq_id, slice, enq_flags); }else if (bpf_ksym_exists (scx_bpf_dsq_insert___v1)) {scx_bpf_dsq_insert___v1 (p, dsq_id, slice, enq_flags);
@@ -506,12 +513,12 @@ static inline bool __COMPAT_has_dsq_reenq_task(void)/* * v6.19: The new void variant can be called from anywhere while the older v1 -* variant can only be called from ops.cpu_release(). The double ___ prefixes on -* the v2 variant need to be removed once libbpf is updated to ignore ___ prefix -* on kernel side. Drop the wrapper and move the decl to common.bpf.h after -* v6.22. +* variant can only be called from ops.cpu_release(). Older libbpf can resolve +* the v2 variant only through the extra ___compat suffix. Drop the wrapper and +* move the decl to common.bpf.h after v6.22. */ u32 scx_bpf_reenqueue_local___v1(void) __ksym __weak; +void scx_bpf_reenqueue_local___v2(void) __ksym __weak; void scx_bpf_reenqueue_local___v2___compat(void) __ksym __weak;static inline bool __COMPAT_scx_bpf_reenqueue_local_from_anywhere (void )
@@ -523,6 +530,8 @@ static inline void scx_bpf_reenqueue_local(void) {if (__COMPAT_has_generic_reenq ())scx_bpf_dsq_reenq___compat (SCX_DSQ_LOCAL ,0 ); +else if (bpf_ksym_exists (scx_bpf_reenqueue_local___v2)) +scx_bpf_reenqueue_local___v2 ();else if (__COMPAT_scx_bpf_reenqueue_local_from_anywhere ())scx_bpf_reenqueue_local___v2___compat ();else
@@ -541,6 +550,10 @@ static inline int scx_bpf_reenqueue_local_from_anywhere(void)scx_bpf_dsq_reenq___compat (SCX_DSQ_LOCAL ,0 );return 0 ; } +if (bpf_ksym_exists (scx_bpf_reenqueue_local___v2)) { +scx_bpf_reenqueue_local___v2 (); +return 0 ; + }if (__COMPAT_scx_bpf_reenqueue_local_from_anywhere ()) {scx_bpf_reenqueue_local___v2___compat ();return 0 ;
Signature · 1 line
2.56.1
Ihor Solodrai
reviewerPATCH v2 3/6 libbpf: Resolve kfunc variants by prototype
In reply to Ihor Solodrailibbpf resolves a kfunc declaration by exact name, after dropping its last ___ suffix, and only then checks the prototype. The name alone picks the kernel function, so the kernel can't serve both prototypes of a changed kfunc. For example, vmlinux.h declares:
bool scx_bpf_dsq_move_to_local(u64 dsq_id); bool scx_bpf_dsq_move_to_local___v2(u64 dsq_id, u64 enq_flags);
libbpf resolves the second to scx_bpf_dsq_move_to_local() and rejects its prototype. Keeping the old prototype as ___v1 and giving the new one the unsuffixed name would break old declarations the same way.
Select a kfunc in two steps instead:
1. Collect the functions in vmlinux and module BTFs with the same essential name as the declaration, as CO-RE does for types. The "bpf_kfunc" decl tag is ignored.
2. Resolve the candidate with a compatible prototype. If there is none, a __weak kfunc stays unresolved. More than one is an error, even for a __weak kfunc.
The prototype check is stricter than bpf_core_types_are_compat(). It also compares the names of structs, unions and forward declarations, ignoring ___ suffixes. Members aren't compared. Typedefs and qualifiers are skipped. For example:
typedef struct foo foo_t;
void kfunc(struct foo *arg); void kfunc(struct foo___local *arg); void kfunc(foo_t *arg);
are compatible, but not with:
void kfunc(struct bar *arg);
So a declaration with an outdated struct name no longer resolves. For example, commit 8740156ad33b ("bpf: Require a BPF cpumask for bpf_cpumask_populate()") changed:
before: int bpf_cpumask_populate(struct cpumask *, void *, size_t); after: int bpf_cpumask_populate(struct bpf_cpumask *, void *, size_t);
Each declaration now resolves only on a kernel with the same prototype. A stacked suffix, as in foo___v2___compat, no longer resolves foo___v2, because its essential name is foo___v2. compat.bpf.h now tries foo___v2 first.
Module BTFs are now loaded for every object with kfuncs: 0.5 and 1.4 ms for 8 and 32 small modules in a VM. Lookup cost per kfunc:
before (hit / miss) after sorted, 23 modules 0.14 / 0.65 us 0.8 us unsorted (v6.18) 0.1 / 0.5 ms 0.7 ms
--- tools/lib/bpf/btf.c | 99 ++++++++++++++++++++++++++- tools/lib/bpf/libbpf.c | 117 ++++++++++++++++++++++---------- tools/lib/bpf/libbpf_internal.h | 2 + tools/lib/bpf/libbpf_tools.h | 2 + 4 files changed, 182 insertions(+), 38 deletions(-)
tools/lib/bpf/btf.c +96 -3
@@ -1140,7 +1140,7 @@ static void btf_check_sorted(struct btf *btf) }static __s32 btf_find_type_by_name_bsearch (const struct btf *btf,const char *name, -__s32 start_id) +size_t name_len,__s32 start_id) {const struct btf_type *t;const char *tname;
@@ -1152,7 +1152,7 @@ static __s32 btf_find_type_by_name_bsearch(const struct btf *btf, const char *na m = l + (r - l) /2 ; t =btf_type_by_id (btf, m); tname =btf__str_by_offset (btf, t->name_off); -if (strcmp (tname, name) >=0 ) { +if (strncmp (tname, name, name_len) >=0 ) {if (l == r)return r; r = m;
@@ -1164,6 +1164,99 @@ static __s32 btf_find_type_by_name_bsearch(const struct btf *btf, const char *nareturn btf__type_cnt (btf); } +/* +* Set [*start, *end) to the range of own type IDs that may match the +* first name_len bytes of name. For unsorted BTF, include all own type IDs. +*/ +void btf_name_prefix_range (const struct btf *btf,const char *name,size_t name_len, +__u32 *start,__u32 *end) +{ +__u32 i, n =btf__type_cnt (btf); + +if (btf->named_start_id <=0 ) { + *start = btf->start_id; + *end = n; +return ; + } + *start =btf_find_type_by_name_bsearch (btf, name, name_len, btf->named_start_id); +for (i = *start; i < n; i++) { +const struct btf_type *t =btf_type_by_id (btf, i); +const char *tname =btf__str_by_offset (btf, t->name_off); + +if (strncmp (tname, name, name_len) !=0 ) +break ; + } + *end = i; +} + +static __u16 kfunc_type_kind (const struct btf_type *t) +{ +if (btf_is_fwd (t)) +return btf_kflag (t) ?BTF_KIND_UNION :BTF_KIND_STRUCT ; +if (btf_is_enum64 (t)) +return BTF_KIND_ENUM ; +return btf_kind (t); +} + +/* +* Return 1 if the types match, 0 if they don't, or -EINVAL if a type +* can't be looked up or the types nest deeper than depth. +*/ +static int kfunc_type_match (const struct btf *a,__u32 a_id, +const struct btf *b,__u32 b_id,int depth) +{ +const struct btf_type *at =skip_mods_and_typedefs (a, a_id,NULL ); +const struct btf_type *bt =skip_mods_and_typedefs (b, b_id,NULL ); +__u16 kind; +int i, ret; + +if (!at || !bt || !depth) +return -EINVAL ; + + kind =kfunc_type_kind (at); +if (kind !=kfunc_type_kind (bt)) +return 0 ; + +switch (kind) { +case BTF_KIND_STRUCT : +case BTF_KIND_UNION : { +const char *an =btf__name_by_offset (a, at->name_off); +const char *bn =btf__name_by_offset (b, bt->name_off); +size_t name_len =bpf_core_essential_name_len (an); + +return name_len ==bpf_core_essential_name_len (bn) && +strncmp (an, bn, name_len) ==0 ; + } +case BTF_KIND_UNKN : +case BTF_KIND_ENUM : +return 1 ; +case BTF_KIND_INT : +return btf_int_offset (at) ==0 &&btf_int_offset (bt) ==0 ; +case BTF_KIND_PTR : +return kfunc_type_match (a, at->type, b, bt->type, depth -1 ); +case BTF_KIND_ARRAY : +return kfunc_type_match (a,btf_array (at)->type, b,btf_array (bt)->type, depth -1 ); +case BTF_KIND_FUNC_PROTO : +if (btf_vlen (at) !=btf_vlen (bt)) +return 0 ; +for (i =0 ; i <btf_vlen (at); i++) { + ret =kfunc_type_match (a,btf_params (at)[i].type, + b,btf_params (bt)[i].type, depth -1 ); +if (ret <=0 ) +return ret; + } +return kfunc_type_match (a, at->type, b, bt->type, depth -1 ); +default : +return 0 ; + } +} + +int btf_kfunc_proto_match (const struct btf *a,__u32 a_id, +const struct btf *b,__u32 b_id) +{ +return kfunc_type_match (a, a_id, b, b_id,32 ); +} +static __s32 btf_find_by_name_kind (const struct btf *btf,int start_id,const char *type_name,__s32 kind) {
@@ -1185,7 +1278,7 @@ static __s32 btf_find_by_name_kind(const struct btf *btf, int start_id,if (btf->named_start_id >0 && type_name[0 ]) { start_id =max (start_id, btf->named_start_id); - id =btf_find_type_by_name_bsearch (btf, type_name, start_id); + id =btf_find_type_by_name_bsearch (btf, type_name,strlen (type_name), start_id);for (; id < nr_types; id++) { t =btf__type_by_id (btf, id); tname =btf__str_by_offset (btf, t->name_off);
tools/lib/bpf/libbpf.c +82 -35
169 lines not on this page · raw message
tools/lib/bpf/libbpf_internal.h +2 -0
@@ -251,6 +251,8 @@ const struct btf_type *skip_mods_and_typedefs(const struct btf *btf, __u32 id, _void btf_set_base_btf (struct btf *btf,const struct btf *base_btf);int btf_relocate (struct btf *btf,const struct btf *base_btf,__u32 **id_map,__u32 **str_map); +void btf_name_prefix_range (const struct btf *btf,const char *name,size_t name_len, +__u32 *start,__u32 *end);bool btf_type_is_traceable_func (const struct btf *btf,const struct btf_type *t);static inline enum btf_func_linkage btf_func_linkage (const struct btf_type *t)
tools/lib/bpf/libbpf_tools.h +2 -0
@@ -12,6 +12,8 @@ struct btf_header;struct btf_type ;const struct btf_header *btf_header (const struct btf *btf); +int btf_kfunc_proto_match (const struct btf *a,__u32 a_id, +const struct btf *b,__u32 b_id);enum btf_field_iter_kind {BTF_FIELD_ITER_IDS ,
Signature · 1 line
2.56.1
Ihor Solodrai
reviewerPATCH v2 4/6 selftests/bpf: Test kfunc variant selection by prototype
In reply to Ihor SolodraiRegister two kfunc variants in bpf_testmod that differ only in a struct pointee:
int bpf_kfunc_variant(struct task_struct *p); int bpf_kfunc_variant___v2(struct file *p);
Check that a declaration of each resolves to it, and that a __weak declaration with struct inode * stays unresolved.
--- .../selftests/bpf/prog_tests/verifier.c | 3 ++ .../selftests/bpf/progs/kfunc_variants.c | 32 +++++++++++++++++++ .../selftests/bpf/test_kmods/bpf_testmod.c | 12 +++++++ .../bpf/test_kmods/bpf_testmod_kfunc.h | 3 ++ 4 files changed, 50 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/kfunc_variants.c
tools/testing/selftests/bpf/prog_tests/verifier.c +3 -0
@@ -8,6 +8,7 @@#include "cap_helpers.h" #include "sysctl_helpers.h" #include "cb_refs.skel.h" +#include "kfunc_variants.skel.h" #include "verifier_aggregate_arg.skel.h" #include "verifier_aggregate_ret.skel.h" #include "verifier_alu_range.skel.h"
@@ -220,6 +221,8 @@ void test_arena_kfunc_jit(void) { RUN_TESTS(arena_kfunc_jit); }void test_cb_refs (void ) {RUN_TESTS (cb_refs); } +void test_kfunc_variants (void ) {RUN_TESTS (kfunc_variants); } +void test_verifier_aggregate_arg (void ) {RUN_TESTS (verifier_aggregate_arg); }void test_verifier_aggregate_ret (void ) {RUN_TESTS (verifier_aggregate_ret); }void test_verifier_alu_range (void ) {RUN (verifier_alu_range); }
tools/testing/selftests/bpf/progs/kfunc_variants.c new file +32 -0
@@ -0,0 +1,32 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */ + +#include <vmlinux.h> +#include <bpf/bpf_helpers.h> +#include "bpf_misc.h" +#include "../test_kmods/bpf_testmod_kfunc.h" + +extern int bpf_kfunc_variant___none(struct inode *p) __weak __ksym; + +char _license[] SEC("license") = "GPL"; + +SEC("syscall") +__retval(1) +int unsuffixed_name (void *ctx) +{ +return bpf_kfunc_variant (NULL ); +} + +SEC("syscall") +__retval(2) +int kernel_variant (void *ctx) +{ +return bpf_kfunc_variant___v2 (NULL ); +} + +SEC("syscall") +__retval(0) +int no_match (void *ctx) +{ +return bpf_ksym_exists (bpf_kfunc_variant___none); +}
tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +12 -0
@@ -1928,6 +1928,16 @@ __bpf_kfunc void bpf_kfunc_trigger_ctx_check(void)irq_work_queue (&ctx_check_irq); } +__bpf_kfunc int bpf_kfunc_variant(struct task_struct *p__nullable) +{ +return 1 ; +} + +__bpf_kfunc int bpf_kfunc_variant___v2(struct file *p__nullable) +{ +return 2 ; +} + BTF_KFUNCS_START(bpf_testmod_check_kfunc_ids) BTF_ID_FLAGS(func, bpf_testmod_test_mod_kfunc, KF_SPINLOCK_SAFE) BTF_ID_FLAGS(func, bpf_kfunc_call_test1)
@@ -2019,6 +2029,8 @@ BTF_ID_FLAGS(func, bpf_kfunc_implicit_arg, KF_IMPLICIT_ARGS) BTF_ID_FLAGS(func, bpf_kfunc_implicit_arg_legacy, KF_IMPLICIT_ARGS) BTF_ID_FLAGS(func, bpf_kfunc_implicit_arg_legacy_impl) BTF_ID_FLAGS(func, bpf_kfunc_trigger_ctx_check) +BTF_ID_FLAGS(func, bpf_kfunc_variant) +BTF_ID_FLAGS(func, bpf_kfunc_variant___v2)BTF_KFUNCS_END (bpf_testmod_check_kfunc_ids) static int bpf_testmod_ops_init(struct btf *btf)
tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h +3 -0
@@ -381,4 +381,7 @@ void bpf_testmod_test_hardirq_fn(void);void bpf_testmod_test_softirq_fn (void ); void bpf_kfunc_trigger_ctx_check(void) __ksym; +int bpf_kfunc_variant(struct task_struct *p__nullable) __ksym; +int bpf_kfunc_variant___v2(struct file *p__nullable) __ksym; +#endif /* _BPF_TESTMOD_KFUNC_H */
Signature · 1 line
2.56.1
Ihor Solodrai
reviewerPATCH v2 5/6 resolve_btfids: Reject ambiguous kfunc variants
In reply to Ihor Solodrailibbpf fails to load a program if a kfunc declaration matches more than one kfunc variant. For example, libbpf doesn't distinguish integer widths, so a declaration of either of these matches both:
int bpf_foo(u32 flags); int bpf_foo___v1(u64 flags);
Check the kfuncs registered in vmlinux or a module at build time, so that such a pair fails the build instead. Group them by essential name and compare prototypes with libbpf's matcher. Skip unregistered functions, which libbpf also considers, so that code unrelated to kfuncs can't fail the build. Variants split across modules or vmlinux are also caught only by libbpf.
--- tools/bpf/resolve_btfids/main.c | 69 +++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+)
tools/bpf/resolve_btfids/main.c +69 -0
@@ -94,6 +94,7 @@#include <bpf/btf.h> #include <bpf/libbpf.h> #include <bpf/libbpf_tools.h> +#include <bpf/relo_core.h> #include "btf_colors.h" #include "fmodret.h" #include <subcmd/parse-options.h>
@@ -1918,6 +1919,70 @@ static int sort_btf_by_name(struct btf *btf)return err; } +struct kfunc_variant { +const char *name; +size_t essent_len; +u32 id; +}; + +static int cmp_kfunc_variants (const void *lhs,const void *rhs) +{ +const struct kfunc_variant *a = lhs, *b = rhs; +int ret =strncmp (a->name, b->name,min (a->essent_len, b->essent_len)); + +return ret ?: (a->essent_len > b->essent_len) - (a->essent_len < b->essent_len); +} + +/* libbpf rejects a declaration that matches more than one kfunc variant */ +static int check_kfunc_variants (const struct btf *btf) +{ +const struct btf *base =btf__base_btf (btf); +struct kfunc_variant *kfuncs =NULL ; +u32 cnt =0 , cap =0 ; +int err =0 ; + +for (u32 id = base ?btf__type_cnt (base) :1 ; id <btf__type_cnt (btf); id++) { +const struct btf_type *t =btf__type_by_id (btf, id); +const char *name; + +if (!btf_is_decl_tag (t) ||btf_decl_tag (t)->component_idx !=-1 ) +continue ; +if (strcmp (btf__name_by_offset (btf, t->name_off),DECL_TAG_KFUNC ) !=0 ) +continue ; + + err =ensure_mem (&kfuncs, &cap, cnt +1 ); +if (err) + goto out; + name =btf__name_by_offset (btf,btf__type_by_id (btf, t->type)->name_off); + kfuncs[cnt++] = (struct kfunc_variant ){ + .name = name, + .essent_len =bpf_core_essential_name_len (name), + .id = t->type, + }; + } +qsort (kfuncs, cnt,sizeof (*kfuncs), cmp_kfunc_variants); + +for (u32 i =0 ; i < cnt; i++) { +const struct btf_type *a =btf__type_by_id (btf, kfuncs[i].id); + +for (u32 j = i +1 ; j < cnt; j++) { +const struct btf_type *b; + +if (cmp_kfunc_variants (&kfuncs[i], &kfuncs[j]) !=0 ) +break ; + b =btf__type_by_id (btf, kfuncs[j].id); +if (btf_kfunc_proto_match (btf, a->type, btf, b->type) <=0 ) +continue ; +pr_err ("ERROR: resolve_btfids: kfuncs %s and %s have matching prototypes\n" , + kfuncs[i].name, kfuncs[j].name); + err = -EINVAL ; + } + } +out: +free (kfuncs); +return err; +} +static int finalize_btf (struct object *obj) {struct btf *base_btf = obj->base_btf, *btf = obj->btf;
@@ -1929,6 +1994,10 @@ static int finalize_btf(struct object *obj)return err; } + err =check_kfunc_variants (obj->btf); +if (err) +return err; +if (obj->base_btf && obj->distill_base) { err =btf__distill_base (obj->btf, &base_btf, &btf);if (err) {
Signature · 1 line
2.56.1
Ihor Solodrai
reviewerPATCH v2 6/6 docs/bpf: Describe how to change a kfunc's prototype
In reply to Ihor SolodraiAdd a section on changing a kfunc's prototype to the kfunc lifecycle expectations. libbpf now binds a ___-suffixed kfunc by prototype and resolve_btfids rejects kfuncs that libbpf can't tell apart, so kernel developers can keep BPF programs built against an old prototype loading, provided they follow the rules this section describes.
--- Documentation/bpf/kfuncs.rst | 96 ++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+)
Documentation/bpf/kfuncs.rst +96 -0
@@ -800,6 +800,102 @@ procedure is followed for removal: 3. After the deprecation period the kfunc will be removed. At this point, BPF programs calling the kfunc will be rejected by the verifier. +3.2 Changing a kfunc's prototype +-------------------------------- + +libbpf drops the last ``___suffix`` from the name of a kfunc declared in a BPF +program and from the names of functions in kernel and module BTF, as it does +for CO-RE type names. Among the functions with the same remaining name, it +binds the one with a prototype that matches the declaration. Types are +compared kind by kind, and structs and unions by name only: + +.. code-block:: c + + typedef struct task_struct task_t; + + /* these match */ + int bpf_foo(struct task_struct *p, u32 flags); + int bpf_foo(const task_t *p, u64 flags); + int bpf_foo(struct task_struct___local *p, int flags); + + /* none of these match the ones above */ + int bpf_foo(struct file *p, u32 flags); + int bpf_foo(struct task_struct *p); + void bpf_foo(struct task_struct *p, u32 flags); + +If the old and new prototypes match, change the kfunc in place. Otherwise the +new prototype keeps the name, and the old one stays available as +``<name>___v1`` (``___v2`` for the next change, and so on) for a deprecation +period, usually as a thin wrapper registered with the same flags: + +.. code-block:: c + + __bpf_kfunc int bpf_foo(struct task_struct *p, u64 flags) + { + ... + } + + /* bpf_foo() before flags was added */ + __bpf_kfunc int bpf_foo___v1(struct task_struct *p) + { + return bpf_foo(p, 0); + } + + BTF_ID_FLAGS(func, bpf_foo, KF_RCU) + BTF_ID_FLAGS(func, bpf_foo___v1, KF_RCU) + +BPF programs built against either prototype then load: + +.. code-block:: c + + /* built against the old prototype: binds bpf_foo___v1 */ + extern int bpf_foo(struct task_struct *p) __ksym; + + /* built against the new prototype: binds bpf_foo */ + extern int bpf_foo(struct task_struct *p, u64 flags) __ksym; + + /* the suffix doesn't select a kfunc: both bind bpf_foo___v1 */ + extern int bpf_foo___v1(struct task_struct *p) __ksym; + extern int bpf_foo___local(struct task_struct *p) __ksym; + + /* a stacked suffix binds nothing */ + extern int bpf_foo___v1___local(struct task_struct *p) __ksym; + +Only libbpf 1.9 and later look for ``bpf_foo___v1``. Older libbpf and light +skeletons see only ``bpf_foo``, as after an in-place change. + +If two kfunc names differ only in the ``___`` suffix, their prototypes must not +match, not even for aliases: libbpf can't tell them apart and fails to load a +program that declares either one, even as ``__weak``. For example, this widens +an offset to 64 bits and keeps the old prototype as ``bpf_bar___v1``: + +.. code-block:: c + + __bpf_kfunc int bpf_bar(const struct bpf_dynptr *p, u64 offset); + + /* bpf_bar() before offset was widened */ + __bpf_kfunc int bpf_bar___v1(const struct bpf_dynptr *p, u32 offset); + +``resolve_btfids`` rejects it:: + + ERROR: resolve_btfids: kfuncs bpf_bar and bpf_bar___v1 have matching prototypes + +Widening keeps the prototype matching, so it is done in place instead: drop +``bpf_bar___v1``, and programs built against the old prototype bind +``bpf_bar``. ``resolve_btfids`` checks registered kfuncs within vmlinux or one +module. libbpf catches other matches, such as between two modules, when loading +the program. + +Give the new prototype a name outside the family, not ``<name>___*``, if: + +- programs loaded by older libbpf or light skeletons must keep working, or +- the prototypes match but the change breaks callers, e.g. two integer + parameters swap places. + +Don't rename a ``<name>___vN`` kfunc while older libbpf or light skeletons +still load programs that call it by that name. libbpf 1.9 and later bind by +prototype, so a rename doesn't affect them. + 4. Core kfuncs ==============
Signature · 1 line
2.56.1
[email protected]
AI reviewRe: [PATCH bpf-next v2 6/6] docs/bpf: Describe how to change a kfunc's prototype
TL;DR: Docs-only question: the kfuncs.rst paragraph warning against renaming a ___vN kfunc for older libbpf seems to contradict the statement that older libbpf can't see it; which case does it…
Quoted 1 line
docs/bpf: Describe how to change a kfunc's prototype
TL;DR: Docs-only question: the kfuncs.rst paragraph warning against renaming a ___vN kfunc for older libbpf seems to contradict the statement that older libbpf can't see it; which case does it protect?
Quoted 3 lines
Documentation/bpf/kfuncs.rst +0
-0
[ ... ]
Quoted 2 lines
Documentation/bpf/kfuncs.rst +2
-0+Only libbpf 1.9 and later look for ``bpf_foo___v1``. Older libbpf and light +skeletons see only ``bpf_foo``, as after an in-place change.
[ ... ]
Documentation/bpf/kfuncs.rst procedure is followed for removal: · line 897
Quoted 3 lines
Documentation/bpf/kfuncs.rst +3
-0+Don't rename a ``<name>___vN`` kfunc while older libbpf or light skeletons +still load programs that call it by that name. libbpf 1.9 and later bind by +prototype, so a rename doesn't affect them.
This paragraph seems to contradict the earlier statement that older libbpf and light skeletons see only bpf_foo.
Older libbpf strips the last ___ suffix from the declared name and looks up the exact remaining name, so it should never resolve a kernel function named <name>___vN, whatever name the program declares.
If so, can a rename of bpf_foo___v1 break programs loaded by older libbpf, given that they could not bind it in the first place?
Is there a case this paragraph is meant to protect, such as a program using a differently named declaration? If so, could it be described, so that maintainers know which renames are safe?
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/44845964556
[email protected]
AI reviewRe: [PATCH bpf-next v2 2/6] sched_ext: Prefer ___v2 kfunc names in compat.bpf.h
In reply to Ihor SolodraiTL;DR: __COMPAT_scx_bpf_reenqueue_local_from_anywhere() in compat.bpf.h still checks only the ___v2___compat name, so with the new libbpf it may return false on v6.19+ kernels, silently disabling…
Quoted 1 line
sched_ext: Prefer___v2kfunc names incompat.bpf.h
TL;DR: __COMPAT_scx_bpf_reenqueue_local_from_anywhere() in compat.bpf.h still checks only the ___v2___compat name, so with the new libbpf it may return false on v6.19+ kernels, silently disabling scx_qmap's sched_switch reenqueue.
Quoted 3 lines
tools/sched_ext/include/scx/compat.bpf.h +0
-0
[ ... ]
tools/sched_ext/include/scx/compat.bpf.h static inline bool __COMPAT_has_dsq_reenq_task(void)
Quoted 8 lines
u32
scx_bpf_reenqueue_local___v1(void)__ksym__weak;tools/sched_ext/include/scx/compat.bpf.h +1
-0+void scx_bpf_reenqueue_local___v2(void) __ksym __weak; void scx_bpf_reenqueue_local___v2___compat(void) __ksym __weak;static inline bool __COMPAT_scx_bpf_reenqueue_local_from_anywhere (void ) {return bpf_ksym_exists (scx_bpf_reenqueue_local___v2___compat); }
After the libbpf change described in the commit message, the ___v2___compat names resolve nothing. This predicate still tests only scx_bpf_reenqueue_local___v2___compat, so with that libbpf it would return false even on v6.19+ kernels where scx_bpf_reenqueue_local___v2() can be called from anywhere.
The in-tree wrappers scx_bpf_reenqueue_local() and scx_bpf_reenqueue_local_from_anywhere() check bpf_ksym_exists(scx_bpf_reenqueue_local___v2) before calling it, but __COMPAT_scx_bpf_reenqueue_local_from_anywhere() is exported from this header and schedulers use it directly. For example, scx_qmap does:
qmap_sched_switch():
if (!__COMPAT_scx_bpf_reenqueue_local_from_anywhere())
return 0;
qmap_cpu_release():
if (!__COMPAT_scx_bpf_reenqueue_local_from_anywhere())
scx_bpf_reenqueue_local();A scheduler built this way would silently turn off its sched_switch based reenqueue and fall back to ops.cpu_release().
Should this predicate also check scx_bpf_reenqueue_local___v2, so that it still returns true on v6.19+ kernels with the new libbpf? Note that the two ksym checks probably need to be in separate branches rather than an ||, since the comment in scx_bpf_reenqueue_local_from_anywhere() says clang can fold that into a bitwise OR of the ksym addresses.
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/44845964556
[email protected]
AI reviewRe: [PATCH bpf-next v2 3/6] libbpf: Resolve kfunc variants by prototype
In reply to Ihor SolodraiTL;DR: A module BTF load error in find_kfunc_btf_id() may be returned as -EINVAL and mistaken for "no compatible prototype", so a weak kfunc in vmlinux could be treated as missing and strong ones…
Quoted 1 line
libbpf: Resolve kfunc variants by prototype
TL;DR: A module BTF load error in find_kfunc_btf_id() may be returned as -EINVAL and mistaken for "no compatible prototype", so a weak kfunc in vmlinux could be treated as missing and strong ones get a misleading error.
Quoted 3 lines
tools/lib/bpf/libbpf.c +0
-0
[ ... ]
Quoted 8 lines
tools/lib/bpf/libbpf.c +8
-0+/* +* Search vmlinux and module BTFs. Return: +* - a BTF ID for a unique match; +* - -ESRCH if the family is absent; +* - -EINVAL if no candidate has a compatible prototype; +* - -ENOTUNIQ if more than one candidate has a compatible prototype; +* - another negative error on failure. +*/
tools/lib/bpf/libbpf.c static int bpf_object__resolve_ksym_var_btf_id(struct bpf_object *obj, · line 9537
Quoted 10 lines
tools/lib/bpf/libbpf.c +10
-0+static int find_kfunc_btf_id (struct bpf_object *obj,const struct extern_desc *ext, +struct btf **res_btf,struct module_btf **res_mod_btf) +{ +size_t name_len =bpf_core_essential_name_len (ext->name); +int kfunc_id =0 , nr_cands =0 , err; +size_t i; + + err =load_module_btfs (obj); +if (err) +return err;
Can this return -EINVAL for a reason other than a prototype mismatch?
load_module_btfs() calls btf_get_from_fd(), which goes through btf_new() and can return -EINVAL from btf_parse_hdr(), btf_parse_type_sec() or btf_sanity_check() when libbpf rejects a module's BTF. That error is returned here unchanged.
[ ... ]
tools/lib/bpf/libbpf.c static int bpf_object__resolve_ksym_var_btf_id(struct bpf_object *obj, · line 9589
Quoted 7 lines
tools/lib/bpf/libbpf.c +4
-0+if (kfunc_id == -EINVAL ) {if (ext->is_weak)return 0 ; +pr_warn ("extern (func ksym) '%s': no compatible prototype in kernel or module BTFs\n" , + ext->name); +return kfunc_id; }
bpf_object__resolve_ksym_func_btf_id() treats that -EINVAL as "no compatible prototype". A __weak kfunc is then left unresolved even if it exists in vmlinux, so bpf_ksym_exists() reports it as missing. A strong kfunc fails with the misleading "no compatible prototype" message.
Also, load_module_btfs() has already set obj->btf_modules_loaded = true by then, so later kfuncs and the CO-RE lookups in bpf_core_find_cands() would continue with a partial module list instead of failing the load.
Before this patch, a kfunc found in vmlinux never called load_module_btfs(). When the lookup did reach it, find_ksym_btf_id() returned the error as is, and only -ESRCH was treated as absence.
Other errors from load_module_btfs() (-ENOMEM, errors from the BTF syscalls) also return here without a message naming the extern, because the generic "if (kfunc_id < 0) return kfunc_id;" path prints nothing.
Would it make sense to call load_module_btfs() in the caller, or to keep its error apart from the -ESRCH/-EINVAL/-ENOTUNIQ results?
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/44845964556
Quentin Monnet
maintainerRe: [PATCH bpf-next v2 1/6] libbpf: Separate interfaces shared with in-tree tools
In reply to Ihor Solodrai2026-11-20 17:33 UTC-0700 ~ Ihor Solodrai <[email protected]>
Quoted 8 lines
resolve_btfidslinks libbpf statically and uses its BTF header accessor, field iterator and SHA-256 helper.libbpf_internal.halso defines logging macros and poisons kernel integer typedefs, soresolve_btfidswraps the hash helper in a separate source file.Move the shared declarations to
libbpf_tools.hand use them directly inresolve_btfids. The header is for in-tree tools and stays uninstalled. No shared-library symbols change.
Hi Ihor, the change in bpftool's Makefile looks OK and I understand the motivation for libbpf_tools.h for resolve_btfids. But I find it a bit confusing to move away from libbpf_internal.h only partially for bpftool, and to have a split dependency on both libbpf_internal.h and libbpf_tools.h. Have you considered moving the other objects that bpftool requires from libbpf_internal.h to libbpf_tools.h as well?
Thanks,
Quentin
Ihor Solodrai
reviewerRe: [PATCH bpf-next v2 1/6] libbpf: Separate interfaces shared with in-tree tools
Quoted 11 lines · Quentin Monnet
2026-11-20 17:33 UTC-0700 ~ Ihor Solodrai <[email protected]>
Quoted 8 lines
resolve_btfidslinks libbpf statically and uses its BTF header accessor, field iterator and SHA-256 helper.libbpf_internal.halso defines logging macros and poisons kernel integer typedefs, soresolve_btfidswraps the hash helper in a separate source file.Move the shared declarations to
libbpf_tools.hand use them directly inresolve_btfids. The header is for in-tree tools and stays uninstalled. No shared-library symbols change.
Quoted 6 lines
Hi Ihor, the change in bpftool's
Makefilelooks OK and I understand the motivation forlibbpf_tools.hforresolve_btfids. But I find it a bit confusing to move away fromlibbpf_internal.honly partially for bpftool, and to have a split dependency on bothlibbpf_internal.handlibbpf_tools.h. Have you considered moving the other objects that bpftool requires fromlibbpf_internal.htolibbpf_tools.has well?
Hi Quentin, thank you for taking a look.
I haven't considered that, and it seems like a good idea to me.
The way I thought about this is that in practice libbpf has at least three levels of (userspace, non-BPF) API:
- public API defined in the symbols map
- internal API, shared helpers and such
- API for the in-tree users, that doesn't make sense to make public
I felt like the last one needs a header to reduce the amount of copy-paste. I don't like "libbpf_tools.h", it gives me "10k loc utils file" vibes, but I couldn't come up with a better name. Open to suggestions.
I need to look into bots' feedback, and I expect there will be a v3, so I'll see what bpftool parts can be moved to the new header as well.
Thanks!
Quoted 3 lines
Thanks,
Quentin
Re: [PATCH bpf-next v2 1/6] libbpf: Separate interfaces shared with in-tree tools
Quoted 22 lines · Ihor Solodrai
Quoted 11 lines · Quentin Monnet
2026-11-20 17:33 UTC-0700 ~ Ihor Solodrai <[email protected]>
Quoted 8 lines
resolve_btfidslinks libbpf statically and uses its BTF header accessor, field iterator and SHA-256 helper.libbpf_internal.halso defines logging macros and poisons kernel integer typedefs, soresolve_btfidswraps the hash helper in a separate source file.Move the shared declarations to
libbpf_tools.hand use them directly inresolve_btfids. The header is for in-tree tools and stays uninstalled. No shared-library symbols change.Quoted 6 lines
Hi Ihor, the change in bpftool's
Makefilelooks OK and I understand the motivation forlibbpf_tools.hforresolve_btfids. But I find it a bit confusing to move away fromlibbpf_internal.honly partially for bpftool, and to have a split dependency on bothlibbpf_internal.handlibbpf_tools.h. Have you considered moving the other objects that bpftool requires fromlibbpf_internal.htolibbpf_tools.has well?Hi Quentin, thank you for taking a look.
Quoted 8 lines
I haven't considered that, and it seems like a good idea to me.
The way I thought about this is that in practice libbpf has at least three levels of (userspace, non-BPF) API:
- public API defined in the symbols map
- internal API, shared helpers and such
- API for the in-tree users, that doesn't make sense to make public
well, I don't think encouraging this with extra "libbpf_tools.h" is a good thing. I'd prefer to keep stuff like that in libbpf_internal.h (because it is meant to be internal to libbpf, with bpftool and resolve_btfids being a special case)
Do I understand correctly that the problem is mostly in poisoning? should we just guard those pragmas with #ifdef check and let resolve_btfids disable that, if desired?
Quoted 16 lines
I felt like the last one needs a header to reduce the amount of copy-paste. I don't like "
libbpf_tools.h", it gives me "10k loc utils file" vibes, but I couldn't come up with a better name. Open to suggestions.I need to look into bots' feedback, and I expect there will be a v3, so I'll see what bpftool parts can be moved to the new header as well.
Thanks!
Quoted 3 lines
Thanks,
Quentin
Ihor Solodrai
reviewerRe: [PATCH bpf-next v2 1/6] libbpf: Separate interfaces shared with in-tree tools
Quoted 23 lines · Andrii Nakryiko
Quoted 22 lines · Ihor Solodrai
Quoted 11 lines · Quentin Monnet
2026-11-20 17:33 UTC-0700 ~ Ihor Solodrai <[email protected]>
Quoted 8 lines
resolve_btfidslinks libbpf statically and uses its BTF header accessor, field iterator and SHA-256 helper.libbpf_internal.halso defines logging macros and poisons kernel integer typedefs, soresolve_btfidswraps the hash helper in a separate source file.Move the shared declarations to
libbpf_tools.hand use them directly inresolve_btfids. The header is for in-tree tools and stays uninstalled. No shared-library symbols change.Quoted 6 lines
Hi Ihor, the change in bpftool's
Makefilelooks OK and I understand the motivation forlibbpf_tools.hforresolve_btfids. But I find it a bit confusing to move away fromlibbpf_internal.honly partially for bpftool, and to have a split dependency on bothlibbpf_internal.handlibbpf_tools.h. Have you considered moving the other objects that bpftool requires fromlibbpf_internal.htolibbpf_tools.has well?Hi Quentin, thank you for taking a look.
Quoted 8 lines
Quoted 8 lines
I haven't considered that, and it seems like a good idea to me.
The way I thought about this is that in practice libbpf has at least three levels of (userspace, non-BPF) API:
- public API defined in the symbols map
- internal API, shared helpers and such
- API for the in-tree users, that doesn't make sense to make public
Hi, Andrii. Just saw your comment after posting.
Please see:
https://lore.kernel.org/bpf/[email protected]/
Quoted 9 lines
well, I don't think encouraging this with extra "
libbpf_tools.h" is a good thing. I'd prefer to keep stuff like that inlibbpf_internal.h(because it is meant to be internal to libbpf, with bpftool andresolve_btfidsbeing a special case)Do I understand correctly that the problem is mostly in poisoning? should we just guard those pragmas with #ifdef check and let
resolve_btfidsdisable that, if desired?
Yes, this appears to be the root cause so to speak. An #ifdef may help as well, yes.
Quoted 17 lines
Quoted 17 lines
I felt like the last one needs a header to reduce the amount of copy-paste. I don't like "
libbpf_tools.h", it gives me "10k loc utils file" vibes, but I couldn't come up with a better name. Open to suggestions.I need to look into bots' feedback, and I expect there will be a v3, so I'll see what bpftool parts can be moved to the new header as well.
Thanks!
Quoted 3 lines
Thanks,
Quentin