From nobody Wed Nov 25 13:40:53 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.170 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795560507; b=DvgSgpI57PZFzH79zPdnywcYoHThuvYfIfQyRaGhQRXuVgRQ4noJtLC8ic4szSMCKqLT oJKSgNSWGsNgwyENamTx0unAA1KsenoZg4z7CXpANdYcs37sdRI3Dcl6dUewaBp/rImQj z9eJXQ7NyA1iXJSBpPyomJKEZ4eHmY845938Z8R6/DQP5yHsPfAnymlL4nDA6QFBmAqbB gptlRsg4wiAL03bmAXcyQ4Bw5Ehoh+BBYhPM598Gi2ky9AVz+6I2BY1L9j9eNnsBUOmYi ha0PJ7899NfjjvkFj6x3MT8CXwP7Y8dWwwImbR+aRcblM46/VI5JU+Hv2qoSq3fc//g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795560507; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=F7jWMMdIFqjVqKv+xCOiXr2uZGgG/WpfoM2zFX2xncU=; b=YJsheApn1Cld4jniFWJtDTVXQqwelwNuapOK36sfZTvpk5l1MpDFWI0IhlXUD/g+tpAd eVHwjlgKY6zbDRIhIx8i79JlL+V7CeLjPSViLdOsA5pNr5cIaJD2NwclP3Gmi1x3MgnnR kNvQ9rgBvUIjY3TcCNTg39YnzkYfRySxxxqKqYni+c7MfvIXN/uSrB4dI6lgvIsCh8kGD QyaSKWTyTDxbWFhUK+9skyLOcdybNfFFpjHpmPrgxkt1JW7NZY38mVHiIS/RQ58qj2PIL GsO/37yEoGraW7frrzYS2Iu//TFTQRz2PlEghUf7duP0x/ZnfEmD+SXN0kjzKSYCNIA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=eoj2RoUW; arc=none smtp.remote-ip=91.218.175.170 Received: from mta0.migadu.com (out-170.mta0.migadu.com [91.218.175.170]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DC693606A32E for ; Tue, 24 Nov 2026 22:48:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org DC693606A32E Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=eoj2RoUW X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=YYbBWFWIDkawZ2+cDvlI2yRTmkX5WPOSODkS2in79sg=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795560504; v=1; x=1796165304; b=eoj2RoUWK8Fhu3UiZf4QW20yRZttesNqQi9/wv71rr459CjnnP+iZ5B6vlXtsBAeiLuF6LD2 pl2Tacw4G7usLeuMAJTEgfk7j8spUlE/WD9db2U614mp63qto+7Gtm91a85Xtv55Env+tt6FrHT 0J9Z9FySj2SGV+3cmc0W5FhM= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 2cfd0229251cf346; Tue, 24 Nov 2026 22:48:24 +0000 X-Mizu-Trace-ID: 2cfd0229251cf346 X-Migadu-Flow: FLOW_OUT From: Ihor Solodrai To: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Eduard Zingerman , Kumar Kartikeya Dwivedi Cc: Alan Maguire , Emil Tsalapatis , Jiri Olsa , Song Liu , Tejun Heo , Yonghong Song , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, sched-ext@lists.linux.dev Subject: [PATCH bpf-next v3 1/6] libbpf: Separate interfaces shared with in-tree tools Date: Tue, 24 Nov 2026 15:48:06 -0700 Message-ID: <20261124224811.16134-2-ihor.solodrai@linux.dev> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261124224811.16134-1-ihor.solodrai@linux.dev> References: <20261124224811.16134-1-ihor.solodrai@linux.dev> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" resolve_btfids and bpftool link libbpf statically and use some of its internal helpers from libbpf_internal.h. That header also defines logging macros and poisons kernel integer typedefs, so resolve_btfids wraps the SHA-256 helper in a separate source file. Move the declarations these tools use to libbpf_tools.h and include it instead of libbpf_internal.h. The header is for in-tree tools and stays uninstalled. No shared-library symbols change. No functional change. Signed-off-by: Ihor Solodrai --- tools/bpf/bpftool/Makefile | 6 +- tools/bpf/bpftool/common.c | 2 +- tools/bpf/bpftool/gen.c | 3 +- tools/bpf/bpftool/prog.c | 2 +- tools/bpf/bpftool/xlated_dumper.c | 2 +- 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 | 162 +---------------------- tools/lib/bpf/libbpf_tools.h | 184 ++++++++++++++++++++++++++ 10 files changed, 199 insertions(+), 186 deletions(-) create mode 100644 tools/lib/bpf/libbpf_tools.h diff --git a/tools/bpf/bpftool/Makefile b/tools/bpf/bpftool/Makefile index 49dabf2eded0..ded4a9e79728 100644 --- a/tools/bpf/bpftool/Makefile +++ b/tools/bpf/bpftool/Makefile @@ -28,10 +28,10 @@ LIBBPF_BOOTSTRAP_INCLUDE :=3D $(LIBBPF_BOOTSTRAP_DESTDI= R)include LIBBPF_BOOTSTRAP_HDRS_DIR :=3D $(LIBBPF_BOOTSTRAP_INCLUDE)/bpf LIBBPF_BOOTSTRAP :=3D $(LIBBPF_BOOTSTRAP_OUTPUT)libbpf.a =20 -# We need to copy hashmap.h, nlattr.h, relo_core.h and libbpf_internal.h +# We need to copy hashmap.h, nlattr.h, relo_core.h and libbpf_tools.h # which are not otherwise exported by libbpf, but still required by bpftoo= l. -LIBBPF_INTERNAL_HDRS :=3D $(addprefix $(LIBBPF_HDRS_DIR)/,hashmap.h nlattr= .h relo_core.h libbpf_internal.h) -LIBBPF_BOOTSTRAP_INTERNAL_HDRS :=3D $(addprefix $(LIBBPF_BOOTSTRAP_HDRS_DI= R)/,hashmap.h relo_core.h libbpf_internal.h) +LIBBPF_INTERNAL_HDRS :=3D $(addprefix $(LIBBPF_HDRS_DIR)/,hashmap.h nlattr= .h relo_core.h libbpf_tools.h) +LIBBPF_BOOTSTRAP_INTERNAL_HDRS :=3D $(addprefix $(LIBBPF_BOOTSTRAP_HDRS_DI= R)/,hashmap.h relo_core.h libbpf_tools.h) =20 $(LIBBPF_OUTPUT) $(BOOTSTRAP_OUTPUT) $(LIBBPF_BOOTSTRAP_OUTPUT) $(LIBBPF_H= DRS_DIR) $(LIBBPF_BOOTSTRAP_HDRS_DIR): $(QUIET_MKDIR)mkdir -p $@ diff --git a/tools/bpf/bpftool/common.c b/tools/bpf/bpftool/common.c index ca5fa2f0bf9b..1f2099c17252 100644 --- a/tools/bpf/bpftool/common.c +++ b/tools/bpf/bpftool/common.c @@ -31,7 +31,7 @@ #include #include #include /* libbpf_num_possible_cpus */ -#include +#include #include #include =20 diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c index ee680f7c2116..dc0adeacf9a8 100644 --- a/tools/bpf/bpftool/gen.c +++ b/tools/bpf/bpftool/gen.c @@ -15,7 +15,8 @@ #include #include #include -#include +#include +#include #include #include #include diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c index 15efc6eebffb..a7b2ea9fbd80 100644 --- a/tools/bpf/bpftool/prog.c +++ b/tools/bpf/bpftool/prog.c @@ -29,7 +29,7 @@ #include #include #include -#include +#include #include =20 #include "cfg.h" diff --git a/tools/bpf/bpftool/xlated_dumper.c b/tools/bpf/bpftool/xlated_d= umper.c index 5579173a61e3..fc83785c0ae3 100644 --- a/tools/bpf/bpftool/xlated_dumper.c +++ b/tools/bpf/bpftool/xlated_dumper.c @@ -10,7 +10,7 @@ #include #include #include -#include +#include =20 #include "disasm.h" #include "json_writer.h" diff --git a/tools/bpf/resolve_btfids/btf_colors.c b/tools/bpf/resolve_btfi= ds/btf_colors.c index 5d307d70e7f2..7374aa2f8963 100644 --- a/tools/bpf/resolve_btfids/btf_colors.c +++ b/tools/bpf/resolve_btfids/btf_colors.c @@ -1,18 +1,9 @@ // SPDX-License-Identifier: GPL-2.0-only -#include +#include +#include +#include #include "btf_colors.h" =20 -/* - * Functions in this file mainly exist to refer to functions from libbpf_i= nternal.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 typ= e. diff --git a/tools/bpf/resolve_btfids/btf_colors.h b/tools/bpf/resolve_btfi= ds/btf_colors.h index 56a85a242d6b..9cea757cb356 100644 --- a/tools/bpf/resolve_btfids/btf_colors.h +++ b/tools/bpf/resolve_btfids/btf_colors.h @@ -11,10 +11,6 @@ enum btf_color { BTF_COLOR_SHARED =3D BTF_COLOR_MAIN | BTF_COLOR_LOC, }; =20 -#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); =20 diff --git a/tools/bpf/resolve_btfids/main.c b/tools/bpf/resolve_btfids/mai= n.c index 0ac895222a14..0e193c3c73aa 100644 --- a/tools/bpf/resolve_btfids/main.c +++ b/tools/bpf/resolve_btfids/main.c @@ -93,12 +93,14 @@ #include #include #include +#include #include "btf_colors.h" #include "fmodret.h" #include =20 #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__" =20 #define BTF_STRUCT "struct" @@ -1205,8 +1207,7 @@ static int patch_btf_link(struct object *obj, const s= truct 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 =3D (u8 *)data->d_buf + module_name_len + BTF_SHA256_DIGEST_LENGTH; if (obj->efile.encoding =3D=3D ELFDATA2LSB) { size[0] =3D raw_btf_size; diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_interna= l.h index e90b5336bac0..8e6f15e2f1bf 100644 --- a/tools/lib/bpf/libbpf_internal.h +++ b/tools/lib/bpf/libbpf_internal.h @@ -18,6 +18,7 @@ #include #include #include +#include "libbpf_tools.h" #include "relo_core.h" =20 /* Android's libc doesn't support AT_EACCESS in faccessat() implementation @@ -94,12 +95,6 @@ #define BTF_TYPE_TYPE_TAG_ENC(value, type) \ BTF_TYPE_ENC(value, BTF_INFO_ENC(BTF_KIND_TYPE_TAG, 0, 0), type) =20 -#ifndef likely -#define likely(x) __builtin_expect(!!(x), 1) -#endif -#ifndef unlikely -#define unlikely(x) __builtin_expect(!!(x), 0) -#endif #ifndef min # define min(x, y) ((x) < (y) ? (x) : (y)) #endif @@ -184,10 +179,6 @@ const char *libbpf_errstr(int err); =20 #define errstr(err) libbpf_errstr(err) =20 -#ifndef __has_builtin -#define __has_builtin(x) 0 -#endif - struct bpf_link { int (*detach)(struct bpf_link *link); void (*dealloc)(struct bpf_link *link); @@ -196,30 +187,6 @@ struct bpf_link { bool disconnected; }; =20 -/* - * Re-implement glibc's reallocarray() for libbpf internal-only use. - * reallocarray(), unfortunately, is not available in all versions of glib= c, - * so requires extra feature detection and using reallocarray() stub from - * and COMPAT_NEED_REALLOCARRAY. All this complicates - * build of libbpf unnecessarily and is just a maintenance burden. Instead, - * it's trivial to implement libbpf-specific internal version and use it - * throughout libbpf. - */ -static inline void *libbpf_reallocarray(void *ptr, size_t nmemb, size_t si= ze) -{ - size_t total; - -#if __has_builtin(__builtin_mul_overflow) - if (unlikely(__builtin_mul_overflow(nmemb, size, &total))) - return NULL; -#else - if (size =3D=3D 0 || nmemb > ULONG_MAX / size) - return NULL; - total =3D nmemb * size; -#endif - return realloc(ptr, total); -} - /* Copy up to sz - 1 bytes from zero-terminated src string and ensure that= dst * is zero-terminated string no matter what (unless sz =3D=3D 0, in which = case * it's a no-op). It's conceptually close to FreeBSD's strlcpy(), but diff= ers @@ -245,9 +212,6 @@ struct btf; struct btf_type; =20 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_m= ap, __u32 **str_map); @@ -431,7 +395,6 @@ int probe_memcg_account(int token_fd); int bump_rlimit_memlock(void); =20 int parse_cpu_mask_str(const char *s, bool **mask, int *mask_sz); -int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz); int libbpf__load_raw_btf(const char *raw_types, size_t types_len, const char *str_sec, size_t str_len, int token_fd); @@ -448,92 +411,6 @@ struct btf *btf_get_from_fd(int btf_fd, struct btf *ba= se_btf); void btf_get_kernel_prefix_kind(enum bpf_attach_type attach_type, const char **prefix, int *kind); =20 -struct btf_ext_info { - /* - * info points to the individual info section (e.g. func_info and - * line_info) from the .BTF.ext. It does not include the __u32 rec_size. - */ - void *info; - __u32 rec_size; - __u32 len; - /* optional (maintained internally by libbpf) mapping between .BTF.ext - * section and corresponding ELF section. This is used to join - * information like CO-RE relocation records with corresponding BPF - * programs defined in ELF sections - */ - __u32 *sec_idxs; - int sec_cnt; -}; - -#define for_each_btf_ext_sec(seg, sec) \ - for (sec =3D (seg)->info; \ - (void *)sec < (seg)->info + (seg)->len; \ - sec =3D (void *)sec + sizeof(struct btf_ext_info_sec) + \ - (seg)->rec_size * sec->num_info) - -#define for_each_btf_ext_rec(seg, sec, i, rec) \ - for (i =3D 0, rec =3D (void *)&(sec)->data; \ - i < (sec)->num_info; \ - i++, rec =3D (void *)rec + (seg)->rec_size) - -/* - * The .BTF.ext ELF section layout defined as - * struct btf_ext_header - * func_info subsection - * - * The func_info subsection layout: - * record size for struct bpf_func_info in the func_info subsection - * struct btf_ext_info_sec for section #1 - * a list of bpf_func_info records for section #1 - * where struct bpf_func_info mimics one in include/uapi/linux/bpf.h - * but may not be identical - * struct btf_ext_info_sec for section #2 - * a list of bpf_func_info records for section #2 - * ...... - * - * Note that the bpf_func_info record size in .BTF.ext may not - * be the same as the one defined in include/uapi/linux/bpf.h. - * The loader should ensure that record_size meets minimum - * requirement and pass the record as is to the kernel. The - * kernel will handle the func_info properly based on its contents. - */ -struct btf_ext_header { - __u16 magic; - __u8 version; - __u8 flags; - __u32 hdr_len; - - /* All offsets are in bytes relative to the end of this header */ - __u32 func_info_off; - __u32 func_info_len; - __u32 line_info_off; - __u32 line_info_len; - - /* optional part of .BTF.ext header */ - __u32 core_relo_off; - __u32 core_relo_len; -}; - -struct btf_ext { - union { - struct btf_ext_header *hdr; - void *data; - }; - void *data_swapped; - bool swapped_endian; - struct btf_ext_info func_info; - struct btf_ext_info line_info; - struct btf_ext_info core_relo_info; - __u32 data_size; -}; - -struct btf_ext_info_sec { - __u32 sec_name_off; - __u32 num_info; - /* Followed by num_info * record_size number of bytes */ - __u8 data[]; -}; - /* The minimum bpf_func_info checked by the loader */ struct bpf_func_info_min { __u32 insn_off; @@ -574,31 +451,6 @@ static inline void bpf_core_relo_bswap(struct bpf_core= _relo *i) i->kind =3D bswap_32(i->kind); } =20 -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, enu= m 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); @@ -721,15 +573,6 @@ static inline int reuse_fd(int fixed_fd, int tmp_fd) return err; } =20 -/* The following two functions are exposed to bpftool */ -int bpf_core_add_cands(struct bpf_core_cand *local_cand, - size_t local_essent_len, - const struct btf *targ_btf, - const char *targ_btf_name, - int targ_start_id, - struct bpf_core_cand_list *cands); -void bpf_core_free_cands(struct bpf_core_cand_list *cands); - struct usdt_manager *usdt_manager_new(struct bpf_object *obj); void usdt_manager_free(struct usdt_manager *man); struct bpf_link * usdt_manager_attach_usdt(struct usdt_manager *man, @@ -772,8 +615,5 @@ int elf_resolve_pattern_offsets(const char *binary_path= , const char *pattern, =20 int probe_fd(int fd); =20 -#define SHA256_DIGEST_LENGTH 32 - -void libbpf_sha256(const void *data, size_t len, __u8 out[SHA256_DIGEST_LE= NGTH]); int probe_sys_bpf_ext(void); #endif /* __LIBBPF_LIBBPF_INTERNAL_H */ diff --git a/tools/lib/bpf/libbpf_tools.h b/tools/lib/bpf/libbpf_tools.h new file mode 100644 index 000000000000..5c8d6d0d2fdd --- /dev/null +++ b/tools/lib/bpf/libbpf_tools.h @@ -0,0 +1,184 @@ +/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ +#ifndef __LIBBPF_TOOLS_H +#define __LIBBPF_TOOLS_H + +#include +#include +#include +#include +#include + +/* Internal interfaces for kernel tools that link libbpf statically. */ + +#ifndef likely +#define likely(x) __builtin_expect(!!(x), 1) +#endif +#ifndef unlikely +#define unlikely(x) __builtin_expect(!!(x), 0) +#endif + +#ifndef __has_builtin +#define __has_builtin(x) 0 +#endif + +/* + * Re-implement glibc's reallocarray() for libbpf internal-only use. + * reallocarray(), unfortunately, is not available in all versions of glib= c, + * so requires extra feature detection and using reallocarray() stub from + * and COMPAT_NEED_REALLOCARRAY. All this complicates + * build of libbpf unnecessarily and is just a maintenance burden. Instead, + * it's trivial to implement libbpf-specific internal version and use it + * throughout libbpf. + */ +static inline void *libbpf_reallocarray(void *ptr, size_t nmemb, size_t si= ze) +{ + size_t total; + +#if __has_builtin(__builtin_mul_overflow) + if (unlikely(__builtin_mul_overflow(nmemb, size, &total))) + return NULL; +#else + if (size =3D=3D 0 || nmemb > ULONG_MAX / size) + return NULL; + total =3D nmemb * size; +#endif + return realloc(ptr, total); +} + +struct btf; +struct btf_header; +struct btf_type; +struct bpf_core_cand; +struct bpf_core_cand_list; + +const struct btf_header *btf_header(const struct btf *btf); +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); + +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); + +struct btf_ext_info { + /* + * info points to the individual info section (e.g. func_info and + * line_info) from the .BTF.ext. It does not include the __u32 rec_size. + */ + void *info; + __u32 rec_size; + __u32 len; + /* optional (maintained internally by libbpf) mapping between .BTF.ext + * section and corresponding ELF section. This is used to join + * information like CO-RE relocation records with corresponding BPF + * programs defined in ELF sections + */ + __u32 *sec_idxs; + int sec_cnt; +}; + +#define for_each_btf_ext_sec(seg, sec) \ + for (sec =3D (seg)->info; \ + (void *)sec < (seg)->info + (seg)->len; \ + sec =3D (void *)sec + sizeof(struct btf_ext_info_sec) + \ + (seg)->rec_size * sec->num_info) + +#define for_each_btf_ext_rec(seg, sec, i, rec) \ + for (i =3D 0, rec =3D (void *)&(sec)->data; \ + i < (sec)->num_info; \ + i++, rec =3D (void *)rec + (seg)->rec_size) + +/* + * The .BTF.ext ELF section layout defined as + * struct btf_ext_header + * func_info subsection + * + * The func_info subsection layout: + * record size for struct bpf_func_info in the func_info subsection + * struct btf_ext_info_sec for section #1 + * a list of bpf_func_info records for section #1 + * where struct bpf_func_info mimics one in include/uapi/linux/bpf.h + * but may not be identical + * struct btf_ext_info_sec for section #2 + * a list of bpf_func_info records for section #2 + * ...... + * + * Note that the bpf_func_info record size in .BTF.ext may not + * be the same as the one defined in include/uapi/linux/bpf.h. + * The loader should ensure that record_size meets minimum + * requirement and pass the record as is to the kernel. The + * kernel will handle the func_info properly based on its contents. + */ +struct btf_ext_header { + __u16 magic; + __u8 version; + __u8 flags; + __u32 hdr_len; + + /* All offsets are in bytes relative to the end of this header */ + __u32 func_info_off; + __u32 func_info_len; + __u32 line_info_off; + __u32 line_info_len; + + /* optional part of .BTF.ext header */ + __u32 core_relo_off; + __u32 core_relo_len; +}; + +struct btf_ext { + union { + struct btf_ext_header *hdr; + void *data; + }; + void *data_swapped; + bool swapped_endian; + struct btf_ext_info func_info; + struct btf_ext_info line_info; + struct btf_ext_info core_relo_info; + __u32 data_size; +}; + +struct btf_ext_info_sec { + __u32 sec_name_off; + __u32 num_info; + /* Followed by num_info * record_size number of bytes */ + __u8 data[]; +}; + +int bpf_core_add_cands(struct bpf_core_cand *local_cand, + size_t local_essent_len, + const struct btf *targ_btf, + const char *targ_btf_name, + int targ_start_id, + struct bpf_core_cand_list *cands); +void bpf_core_free_cands(struct bpf_core_cand_list *cands); + +int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz); + +#define SHA256_DIGEST_LENGTH 32 + +void libbpf_sha256(const void *data, size_t len, __u8 out[SHA256_DIGEST_LE= NGTH]); + +#endif /* __LIBBPF_TOOLS_H */ --=20 2.56.0 From nobody Wed Nov 25 13:40:53 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.174 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795560512; b=LhyNTpW7b345xzdqRHAP2OfKLbrHf6/r1XIj8DBKlO6Wz+9NAew0qCxkFaGS556CbkWp Dp7US3vUUlYK/zngmvMoWWngSepT9HdlxQeXoCFZfYYj6OnIVqxIcSK1Hx9o34ycnElur UDgpZJWAJOMjMX9cAkW9FO2cVFEToOcRduWRotbBF+Xdk2gCskA4zfUnIumlYn9iRrqND X4TaLr74FpQoIxZ8VoFaJCvk94RxMqkmHkXv9w2ERodaLgj48rUBl60gDKXRNoEwI2/ak QukKfQuxZcGiGo+YUzSRc2M9SN1AIHP0TXv6XwCa8pw1oKGuM+AZrekaS5RMAceLAaA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795560512; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=tlZEx+2S2uPqz2EO7/zieEPpzImioLfgNzaugPckSXQ=; b=aj9ioq0j/SH7M/7oWDnOl5JW9fNC8iPYOfc5jNBlkzHXSWjhSF5gmp0fblYs7shmipA5 S/C+F/7kkol3G3fskeY4y681KEaRo5M5bPYD+qKozh6z7pVLiTmMDCLtEEMkTCkJTxS7P rnR8vlSa6CMT5SrkAfFT17I1g4FSIkUYQrMqRtV/YL6CdqbztvQnLDvo5p939S+z/S2t9 79weUdYIQQMjEPxPKwbX8XJZZhcgyzoxdhLWzNXd0V0iTpjpKvji7LaJf31Q0x+cbGJED 54apzuLuTzIDJxMbRCiQQp+wyIMW5bOATMaln5HT5ux1bEI5vWw4GjjGUFZVxFNTAiw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=rUUjyR3s; arc=none smtp.remote-ip=91.218.175.174 Received: from mta0.migadu.com (out-174.mta0.migadu.com [91.218.175.174]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BDD6E606A32A for ; Tue, 24 Nov 2026 22:48:30 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org BDD6E606A32A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=rUUjyR3s X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=aJzHhHVUtWVj1dZaUwICiOaSOIUvJToK7uVIiDZ6vnY=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795560508; v=1; x=1796165308; b=rUUjyR3s3NoQ6RXMghaTQde7FVnMyZGO779rTJar5o7p6hHk+Ir36ZBhP+2tOm9pqzugelVX t13M0anYuWjZY7xI56w8nQ38Nv/Jvr2SgT9KYMA2xUUMgvzy0zehWVPB+3LDV2X53QLf5P6Q+aP /mlGQqKqnd8emCYWIqlO69aQ= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 6e625f7673d8bf4c; Tue, 24 Nov 2026 22:48:28 +0000 X-Mizu-Trace-ID: 6e625f7673d8bf4c X-Migadu-Flow: FLOW_OUT From: Ihor Solodrai To: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Eduard Zingerman , Kumar Kartikeya Dwivedi Cc: Alan Maguire , Emil Tsalapatis , Jiri Olsa , Song Liu , Tejun Heo , Yonghong Song , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, sched-ext@lists.linux.dev Subject: [PATCH bpf-next v3 2/6] sched_ext: Prefer ___v2 kfunc names in compat.bpf.h Date: Tue, 24 Nov 2026 15:48:07 -0700 Message-ID: <20261124224811.16134-3-ihor.solodrai@linux.dev> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261124224811.16134-1-ihor.solodrai@linux.dev> References: <20261124224811.16134-1-ihor.solodrai@linux.dev> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" compat.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. __COMPAT_scx_bpf_reenqueue_local_from_anywhere() checks both names for the same reason. Signed-off-by: Ihor Solodrai --- tools/sched_ext/include/scx/compat.bpf.h | 48 +++++++++++++++++------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/tools/sched_ext/include/scx/compat.bpf.h b/tools/sched_ext/inc= lude/scx/compat.bpf.h index 254882bd8ce6..fcbf7908defd 100644 --- a/tools/sched_ext/include/scx/compat.bpf.h +++ b/tools/sched_ext/include/scx/compat.bpf.h @@ -29,8 +29,10 @@ struct cgroup *scx_bpf_task_cgroup___new(struct task_str= uct *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 __we= ak; 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, u= 64 slice) __ksym __weak; @@ -45,11 +47,13 @@ bool scx_bpf_dispatch_from_dsq___old(struct bpf_iter_sc= x_dsq *it__iter, struct t bool scx_bpf_dispatch_vtime_from_dsq___old(struct bpf_iter_scx_dsq *it__it= er, struct task_struct *p, u64 dsq_id, u64 enq_flags) __ksym __weak; =20 #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))))) =20 #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 d= sq_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 __SUFF= IX on - * kernel side. The entire suffix can be dropped later. + * Older libbpf can resolve scx_bpf_dsq_insert___v2() only through the ext= ra + * ___compat suffix. * * v6.13: scx_bpf_dsq_insert() renaming is also handled here. See the bloc= k 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, u= 64 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 slic= e, u64 enq_flags) __ksym __weak; @@ -443,7 +448,9 @@ void scx_bpf_dispatch___compat(struct task_struct *p, u= 64 dsq_id, u64 slice, u64 static inline bool scx_bpf_dsq_insert(struct task_struct *p, u64 dsq_id, u64 slice, u64 enq_f= lags) { - 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,23 +513,32 @@ static inline bool __COMPAT_has_dsq_reenq_task(void) =20 /* * 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 ___ prefi= xes 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 res= olve + * the v2 variant only through the extra ___compat suffix. Drop the wrappe= r 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; =20 static inline bool __COMPAT_scx_bpf_reenqueue_local_from_anywhere(void) { - return bpf_ksym_exists(scx_bpf_reenqueue_local___v2___compat); + bool v2 =3D bpf_ksym_exists(scx_bpf_reenqueue_local___v2); + + /* + * Without the barrier, clang folds || into a bitwise OR of the two ksym + * addresses, which the verifier rejects. + */ + barrier_var(v2); + return v2 || bpf_ksym_exists(scx_bpf_reenqueue_local___v2___compat); } =20 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 +557,10 @@ static inline int scx_bpf_reenqueue_local_from_anywher= e(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; --=20 2.56.0 From nobody Wed Nov 25 13:40:53 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.180 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795560515; b=LOb0EhBtxNeqAZF5Yg2QoEeg6PrNdZg7EttGaNZkQdhUW9LnNTmzCgzVzkD8RbirMGlQ FQBHWEcRauO3y0n/iGjWGOWBP6TCtxFmyGqi5RG+vw2LC38pWIvw5zlG31sq2a97EuOXj C2AWeCX//cBQBLrpJnpA4fRTwlD5QlgT6udAIbZgn9sKJQDYStho1fr/Nsb2cgNXfyyu4 66j12efSv2iRgVwXR2RsyZE/pb0D+7tE4+XmMF/8YIymV7l0RpJGVYEpc2dAMEnlY5A3J b+AOy55NwxP6ePdMMgsmtQ2mD+s6YiB5ej5mwSqtildCfbDa9wdmNgT+tPlwtY5bnxA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795560515; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=PYamhRQOUAMH4IpUvyw7uO6fx6sGOnTA1LN/Gj4PzBE=; b=rZ0hmmoKLiwI7/T8iMrHJMCFyyVjtWWGyvttQeOKo457nZmic9Gujxp7LWiNRQ81eqsN fFlXBnpMtXNJsGXOuFqL+UKSw6CyphmkkHgY/iVRbaYQNLirfU86aRg/N7SWnrPL09N7d UnWGGZC9cooIbQsNG3BjtI0ISVt1RDoi1p5PNGRdFEHv0prE60ulR/nHUjqvP5KGKbowP h6Nd9t98JL5VTzD/UENBqHK8hT4jCNMmxOOEzksEH0vHtfllU+iK0xBmOJ0varnw0UkJk X29pEEpzQzDDrYvlZZ+0a8DZDUN0TTyxVE0KNyaqHuPB4E/MIbZA4OVQJlirKZWxQ0A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=fYPB+wp1; arc=none smtp.remote-ip=91.218.175.180 Received: from mta0.migadu.com (out-180.mta0.migadu.com [91.218.175.180]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 522C2606A32D for ; Tue, 24 Nov 2026 22:48:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 522C2606A32D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=fYPB+wp1 X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=VZkDnUf7VacUM36nrmY/9dr6ql3Gg9+A7nyy5as6cwY=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795560512; v=1; x=1796165312; b=fYPB+wp1MomSblCJgxR+GY0HwhoRNrALCNStFFA5ytFU5L5TQZdzkbTRLmIXFW7+cnFFxYTK bLO6P/SoCQ0X27ia9uMGAaLW2tq2PHDI/pK1c977mIbkkgiKbNprxQ5lDXq5edJFdhr5x6UY55M ieILtGPw1JKEb6RZoWu0JRg4= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 4160e48562f50284; Tue, 24 Nov 2026 22:48:32 +0000 X-Mizu-Trace-ID: 4160e48562f50284 X-Migadu-Flow: FLOW_OUT From: Ihor Solodrai To: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Eduard Zingerman , Kumar Kartikeya Dwivedi Cc: Alan Maguire , Emil Tsalapatis , Jiri Olsa , Song Liu , Tejun Heo , Yonghong Song , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, sched-ext@lists.linux.dev Subject: [PATCH bpf-next v3 3/6] libbpf: Resolve kfunc variants by prototype Date: Tue, 24 Nov 2026 15:48:08 -0700 Message-ID: <20261124224811.16134-4-ihor.solodrai@linux.dev> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261124224811.16134-1-ihor.solodrai@linux.dev> References: <20261124224811.16134-1-ihor.solodrai@linux.dev> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" libbpf 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 Signed-off-by: Ihor Solodrai --- tools/lib/bpf/btf.c | 99 +++++++++++++++++++++++++++- tools/lib/bpf/libbpf.c | 112 ++++++++++++++++++++++---------- tools/lib/bpf/libbpf_internal.h | 2 + tools/lib/bpf/libbpf_tools.h | 2 + 4 files changed, 178 insertions(+), 37 deletions(-) diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c index a4bf01403a43..71cba0b9bbfe 100644 --- a/tools/lib/bpf/btf.c +++ b/tools/lib/bpf/btf.c @@ -1140,7 +1140,7 @@ static void btf_check_sorted(struct btf *btf) } =20 static __s32 btf_find_type_by_name_bsearch(const struct btf *btf, const ch= ar *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 stru= ct btf *btf, const char *na m =3D l + (r - l) / 2; t =3D btf_type_by_id(btf, m); tname =3D btf__str_by_offset(btf, t->name_off); - if (strcmp(tname, name) >=3D 0) { + if (strncmp(tname, name, name_len) >=3D 0) { if (l =3D=3D r) return r; r =3D m; @@ -1164,6 +1164,99 @@ static __s32 btf_find_type_by_name_bsearch(const str= uct btf *btf, const char *na return btf__type_cnt(btf); } =20 +/* + * 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 ID= s. + */ +void btf_name_prefix_range(const struct btf *btf, const char *name, size_t= name_len, + __u32 *start, __u32 *end) +{ + __u32 i, n =3D btf__type_cnt(btf); + + if (btf->named_start_id <=3D 0) { + *start =3D btf->start_id; + *end =3D n; + return; + } + *start =3D btf_find_type_by_name_bsearch(btf, name, name_len, btf->named_= start_id); + for (i =3D *start; i < n; i++) { + const struct btf_type *t =3D btf_type_by_id(btf, i); + const char *tname =3D btf__str_by_offset(btf, t->name_off); + + if (strncmp(tname, name, name_len) !=3D 0) + break; + } + *end =3D 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 =3D skip_mods_and_typedefs(a, a_id, NULL); + const struct btf_type *bt =3D skip_mods_and_typedefs(b, b_id, NULL); + __u16 kind; + int i, ret; + + if (!at || !bt || !depth) + return -EINVAL; + + kind =3D kfunc_type_kind(at); + if (kind !=3D kfunc_type_kind(bt)) + return 0; + + switch (kind) { + case BTF_KIND_STRUCT: + case BTF_KIND_UNION: { + const char *an =3D btf__name_by_offset(a, at->name_off); + const char *bn =3D btf__name_by_offset(b, bt->name_off); + size_t name_len =3D bpf_core_essential_name_len(an); + + return name_len =3D=3D bpf_core_essential_name_len(bn) && + strncmp(an, bn, name_len) =3D=3D 0; + } + case BTF_KIND_UNKN: + case BTF_KIND_ENUM: + return 1; + case BTF_KIND_INT: + return btf_int_offset(at) =3D=3D 0 && btf_int_offset(bt) =3D=3D 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) !=3D btf_vlen(bt)) + return 0; + for (i =3D 0; i < btf_vlen(at); i++) { + ret =3D kfunc_type_match(a, btf_params(at)[i].type, + b, btf_params(bt)[i].type, depth - 1); + if (ret <=3D 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, =20 if (btf->named_start_id > 0 && type_name[0]) { start_id =3D max(start_id, btf->named_start_id); - id =3D btf_find_type_by_name_bsearch(btf, type_name, start_id); + id =3D btf_find_type_by_name_bsearch(btf, type_name, strlen(type_name), = start_id); for (; id < nr_types; id++) { t =3D btf__type_by_id(btf, id); tname =3D btf__str_by_offset(btf, t->name_off); diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 1efc0dd37b2e..dadf446daa0b 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -641,7 +641,6 @@ struct extern_desc { int btf_id; int sec_btf_id; char *name; - char *essent_name; bool is_set; bool is_weak; union { @@ -4446,7 +4445,6 @@ static int bpf_object__collect_externs(struct bpf_obj= ect *obj) struct extern_desc *ext; int i, n, off, dummy_var_btf_id; const char *ext_name, *sec_name; - size_t ext_essent_len; Elf_Scn *scn; Elf64_Shdr *sh; =20 @@ -4498,14 +4496,6 @@ static int bpf_object__collect_externs(struct bpf_ob= ject *obj) ext->sym_idx =3D i; ext->is_weak =3D ELF64_ST_BIND(sym->st_info) =3D=3D STB_WEAK; =20 - ext_essent_len =3D bpf_core_essential_name_len(ext->name); - ext->essent_name =3D NULL; - if (ext_essent_len !=3D strlen(ext->name)) { - ext->essent_name =3D strndup(ext->name, ext_essent_len); - if (!ext->essent_name) - return -ENOMEM; - } - ext->sec_btf_id =3D find_extern_sec_btf_id(obj->btf, ext->btf_id); if (ext->sec_btf_id <=3D 0) { pr_warn("failed to find BTF for extern '%s' [%d] section: %d\n", @@ -9513,41 +9503,96 @@ static int bpf_object__resolve_ksym_var_btf_id(stru= ct bpf_object *obj, return 0; } =20 +/* t is a FUNC whose essential name is the first name_len bytes of name */ +static bool btf_is_kfunc_candidate(const struct btf *btf, const struct btf= _type *t, + const char *name, size_t name_len) +{ + const char *tname; + + if (!btf_is_func(t)) + return false; + tname =3D btf__name_by_offset(btf, t->name_off); + if (strncmp(tname, name, name_len) !=3D 0) + return false; + return bpf_core_essential_name_len(tname) =3D=3D name_len; +} + +/* + * 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. + */ +static int find_kfunc_btf_id(struct bpf_object *obj, const struct extern_d= esc *ext, + struct btf **res_btf, struct module_btf **res_mod_btf) +{ + size_t name_len =3D bpf_core_essential_name_len(ext->name); + int kfunc_id =3D 0, nr_cands =3D 0; + size_t i; + + for (i =3D 0; i <=3D obj->btf_module_cnt; i++) { + struct module_btf *mod =3D i ? &obj->btf_modules[i - 1] : NULL; + struct btf *btf =3D mod ? mod->btf : obj->btf_vmlinux; + __u32 id, end; + + btf_name_prefix_range(btf, ext->name, name_len, &id, &end); + for (; id < end; id++) { + const struct btf_type *t =3D btf__type_by_id(btf, id); + + if (!btf_is_kfunc_candidate(btf, t, ext->name, name_len)) + continue; + nr_cands++; + if (btf_kfunc_proto_match(obj->btf, ext->ksym.type_id, btf, t->type) <= =3D 0) + continue; + if (kfunc_id) + return -ENOTUNIQ; + kfunc_id =3D id; + *res_btf =3D btf; + *res_mod_btf =3D mod; + } + } + if (!nr_cands) + return -ESRCH; + if (!kfunc_id) + return -EINVAL; + return kfunc_id; +} + static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object *obj, struct extern_desc *ext) { - int local_func_proto_id, kfunc_proto_id, kfunc_id; struct module_btf *mod_btf =3D NULL; const struct btf_type *kern_func; struct btf *kern_btf =3D NULL; - int ret; + int kfunc_id, ret; =20 - local_func_proto_id =3D ext->ksym.type_id; + ret =3D load_module_btfs(obj); + if (ret) + return ret; =20 - kfunc_id =3D find_ksym_btf_id(obj, ext->essent_name ?: ext->name, BTF_KIN= D_FUNC, &kern_btf, - &mod_btf); - if (kfunc_id < 0) { - if (kfunc_id =3D=3D -ESRCH && ext->is_weak) + kfunc_id =3D find_kfunc_btf_id(obj, ext, &kern_btf, &mod_btf); + if (kfunc_id =3D=3D -ESRCH) { + if (ext->is_weak) return 0; pr_warn("extern (func ksym) '%s': not found in kernel or module BTFs\n", ext->name); return kfunc_id; } - - kern_func =3D btf__type_by_id(kern_btf, kfunc_id); - kfunc_proto_id =3D kern_func->type; - - ret =3D bpf_core_types_are_compat(obj->btf, local_func_proto_id, - kern_btf, kfunc_proto_id); - if (ret <=3D 0) { + if (kfunc_id =3D=3D -EINVAL) { if (ext->is_weak) return 0; - - pr_warn("extern (func ksym) '%s': func_proto [%d] incompatible with %s [= %d]\n", - ext->name, local_func_proto_id, - mod_btf ? mod_btf->name : "vmlinux", kfunc_proto_id); - return -EINVAL; + pr_warn("extern (func ksym) '%s': no compatible prototype in kernel or m= odule BTFs\n", + ext->name); + return kfunc_id; } + /* an error even for a weak kfunc: ambiguity isn't absence */ + if (kfunc_id =3D=3D -ENOTUNIQ) { + pr_warn("extern (func ksym) '%s': more than one compatible prototype in = kernel or module BTFs\n", + ext->name); + return kfunc_id; + } + kern_func =3D btf__type_by_id(kern_btf, kfunc_id); =20 /* set index for module BTF fd in fd_array, if unset */ if (mod_btf && !mod_btf->fd_array_idx) { @@ -9579,8 +9624,9 @@ static int bpf_object__resolve_ksym_func_btf_id(struc= t bpf_object *obj, * {kernel_btf_id, kernel_btf_obj_fd} -> fixup ld_imm64. */ ext->ksym.kernel_btf_obj_fd =3D mod_btf ? mod_btf->fd : 0; - pr_debug("extern (func ksym) '%s': resolved to %s [%d]\n", - ext->name, mod_btf ? mod_btf->name : "vmlinux", kfunc_id); + pr_debug("extern (func ksym) '%s': resolved to %s '%s' [%d]\n", + ext->name, mod_btf ? mod_btf->name : "vmlinux", + btf__name_by_offset(kern_btf, kern_func->name_off), kfunc_id); =20 return 0; } @@ -10441,10 +10487,8 @@ void bpf_object__close(struct bpf_object *obj) zfree(&obj->btf_custom_path); zfree(&obj->kconfig); =20 - for (i =3D 0; i < obj->nr_extern; i++) { + for (i =3D 0; i < obj->nr_extern; i++) zfree(&obj->externs[i].name); - zfree(&obj->externs[i].essent_name); - } =20 zfree(&obj->externs); obj->nr_extern =3D 0; diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_interna= l.h index 8e6f15e2f1bf..389c48548222 100644 --- a/tools/lib/bpf/libbpf_internal.h +++ b/tools/lib/bpf/libbpf_internal.h @@ -215,6 +215,8 @@ struct btf_type *btf_type_by_id(const struct btf *btf, = __u32 type_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_m= ap, __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_ty= pe *t); =20 static inline enum btf_func_linkage btf_func_linkage(const struct btf_type= *t) diff --git a/tools/lib/bpf/libbpf_tools.h b/tools/lib/bpf/libbpf_tools.h index 5c8d6d0d2fdd..cd9137376d44 100644 --- a/tools/lib/bpf/libbpf_tools.h +++ b/tools/lib/bpf/libbpf_tools.h @@ -52,6 +52,8 @@ struct bpf_core_cand; struct bpf_core_cand_list; =20 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); 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); =20 --=20 2.56.0 From nobody Wed Nov 25 13:40:53 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.71 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795560521; b=i7RlbG6bZHt0UkzrhVMO9Yo47jjV0WeDcgzjrFEFpS3ppNRN/aZeaC3Ax8NJUxLhTi/3 tUJdAMyPbZ9XzGfvBldXc5Y9KR3XL2gJ7CJ0X0+P+fPfx/faZqV4y8iaByF54OxrI2v/W 0+t4NqnErw32VuH/vx/v84U2p0/xC3Jt5IUO5JlfM3Fw5m+DzWRGDfxfCVuEd+VQRIbrA ShEkhLhVIs1l2hLO2Y0loq08K9Ynv4xkA7uOPPzeTajWe8mXSS/zDvIPlDGG5WhsOzNRL ln2gLfPqSQwsQRmb/0eqyrFo3zcgcrqXrHHcT8rl1FmhaR3qOZvjW0z6taHPJUTTpZw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795560521; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=eiFf1/ZX0x4TPZURmW0qi8uagLmw49tN1yU5fbQU1KI=; b=VJtB9vUdFsccQIi91ay6Y9EKYElpcQJFB2vwvlN1XTgsiqvb2FTmxwD8tNLFhM7wCHGT 9IdA60evMi2apSBLPGQhWOE3dr+UGcD5Q19dNvRVQMbRTktbvtLTo9/ausYXmtmIENbkG dZe+LQlySEYdJ0ohjeshpuI7fxWD2Jqyf9YpAjkoln2tVm7bk4shfGRL3nuAbmDYOW1eT 45XLq4bzrl+tC7JniYtVEXDKvXyzBFCDlA0B+VOGoWzHxriaWtBrQ95k2n/uKGXSDNeY/ RcFBP5aTna/fGfPjCyCfajZ/laC95/YIfKJJZHoGhS8uP79AWhSoxGkjoIc0gDDBkAg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b="AvX/+e2o"; arc=none smtp.remote-ip=95.215.58.71 Received: from mta1.migadu.com (out-71.mta1.migadu.com [95.215.58.71]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5B0CF606A321 for ; Tue, 24 Nov 2026 22:48:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5B0CF606A321 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=AvX/+e2o X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=wGzuJ2tsy16VrGrlGhasL3A14wXGG8Z8FdNekGk0n2c=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795560518; v=1; x=1796165318; b=AvX/+e2oUV+kbAjv0sQvq8Z4GrR5yLZSgXf8sU6uxC/w4S08ilBAOCuEZlimvyLrcmqnajnF EUnFfio2+my3xDx9ST7m2TTfprDG55Z25//S2Urk+IXNK5YIgkAoOxTDX65PwiPOqqbIOQShg7J TU24QcrzLbOr3QD3aModDUis= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 8276b2be94724bc7; Tue, 24 Nov 2026 22:48:37 +0000 X-Mizu-Trace-ID: 8276b2be94724bc7 X-Migadu-Flow: FLOW_OUT From: Ihor Solodrai To: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Eduard Zingerman , Kumar Kartikeya Dwivedi Cc: Alan Maguire , Emil Tsalapatis , Jiri Olsa , Song Liu , Tejun Heo , Yonghong Song , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, sched-ext@lists.linux.dev Subject: [PATCH bpf-next v3 4/6] selftests/bpf: Test kfunc variant selection by prototype Date: Tue, 24 Nov 2026 15:48:09 -0700 Message-ID: <20261124224811.16134-5-ihor.solodrai@linux.dev> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261124224811.16134-1-ihor.solodrai@linux.dev> References: <20261124224811.16134-1-ihor.solodrai@linux.dev> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Register 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. Signed-off-by: Ihor Solodrai --- .../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 diff --git a/tools/testing/selftests/bpf/prog_tests/verifier.c b/tools/test= ing/selftests/bpf/prog_tests/verifier.c index 816ab3cfafc7..157267f90177 100644 --- a/tools/testing/selftests/bpf/prog_tests/verifier.c +++ b/tools/testing/selftests/bpf/prog_tests/verifier.c @@ -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" @@ -225,6 +226,8 @@ void test_arena_kfunc_jit(void) { RUN_TES= TS(arena_kfunc_jit); } =20 void test_cb_refs(void) { RUN_TESTS(cb_refs); } =20 +void test_kfunc_variants(void) { RUN_TESTS(kfunc_variants);= } + void test_verifier_aggregate_arg(void) { RUN_TESTS(verifier_aggrega= te_arg); } void test_verifier_aggregate_ret(void) { RUN_TESTS(verifier_aggrega= te_ret); } void test_verifier_alu_range(void) { RUN(verifier_alu_range); } diff --git a/tools/testing/selftests/bpf/progs/kfunc_variants.c b/tools/tes= ting/selftests/bpf/progs/kfunc_variants.c new file mode 100644 index 000000000000..a9c393ea9d82 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/kfunc_variants.c @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */ + +#include +#include +#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") =3D "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); +} diff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c b/tools/t= esting/selftests/bpf/test_kmods/bpf_testmod.c index 1fbc0061cc24..764877cdce6e 100644 --- a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +++ b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c @@ -1928,6 +1928,16 @@ __bpf_kfunc void bpf_kfunc_trigger_ctx_check(void) irq_work_queue(&ctx_check_irq); } =20 +__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_IMPLICI= T_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) =20 static int bpf_testmod_ops_init(struct btf *btf) diff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h b/t= ools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h index 0a7df1798603..ce579eede88d 100644 --- a/tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h +++ b/tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h @@ -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; =20 +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 */ --=20 2.56.0 From nobody Wed Nov 25 13:40:53 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.78 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795560525; b=oS62C9HFgbR0UZuOArTQcISF8IjKA64ICfKSlikiMx2UlPX6Rxx5J4DM7S3j+r0Y6D0F kSm239W6e2bqSAvmZoA+m8FRAFX0/xDjJYDXRdv2jFsw+lyh7bN+dz52IYVzpst6usM7R SB8wiM1dLHL110EzMluJmubCm/dBi/ZZDwvu6IojS0ndWZQSCfRhpQH6q/VgGrtS7MxZW +DZu8Y1mDXKc2XMQLyUn59yn24pRW3396dGMkskEcszI6mHVMcnT9xK7mgywUKMLRCX4N ngBHJ8Uku9FB0hmQcoAB0RvDSJfInd4abjJLin7KWCQ412c/klg/bsHmQ6xaII2QcMg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795560525; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=6wjOzGU8wFSb9+4j9IB20HEtOP5PcwiSTLoefqj+xPI=; b=zpFp5LKGlh5ayZBc6VcgPMX2J0H+CNWryjWo9TWHaoZ/FIZ0SmpS9dcmTnAC9JzdB9ym nAU6mbcIZ3oHBue6nW5tB+61H9AlFd8OBOyRyBt/hSySsbCoglBDoucLPzI72IMlJzAHj 2YI6d/4C1RaWTrNlkdToI2wL1e9qqyGF9PQvmokvFCK8g3OnXWk1q9w2fJGBLdBQNEW7L yn6jwovzumc9tC7b70P1ZFtze4jEq0QWPF02SvbUzPRooGVw5cBUHPwPsHLCTb1D0PmP5 VvKEzPhpCnUAWxdHHQ60RPAFcoCwA0smzYVaNAwtOgUHtfLaeg9maZeApLoT+CqxFGA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b="EqC01J0/"; arc=none smtp.remote-ip=95.215.58.78 Received: from mta1.migadu.com (out-78.mta1.migadu.com [95.215.58.78]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 93DE1606A331 for ; Tue, 24 Nov 2026 22:48:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 93DE1606A331 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=EqC01J0/ X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=BFyhtG+w37cAmuVAdcKU6EMG/hj7GNHCLQPRWjUEC8I=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795560522; v=1; x=1796165322; b=EqC01J0/RoEYjdEXCBT6mzhATyZVS+ipoJEP7fq/AGdK2T72XHOVtgTa4sONqYZSD2utFRjg iOxB9goQCCJ3bNhHOKFN37O6QrC5wqYWJgJp/msmgYHaPhLbObBxVKneT4rlVjP37NoijfINB92 m6AG8RFh7WGk7sQXAlvY3uGw= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 376c3601e76205b5; Tue, 24 Nov 2026 22:48:42 +0000 X-Mizu-Trace-ID: 376c3601e76205b5 X-Migadu-Flow: FLOW_OUT From: Ihor Solodrai To: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Eduard Zingerman , Kumar Kartikeya Dwivedi Cc: Alan Maguire , Emil Tsalapatis , Jiri Olsa , Song Liu , Tejun Heo , Yonghong Song , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, sched-ext@lists.linux.dev Subject: [PATCH bpf-next v3 5/6] resolve_btfids: Reject ambiguous kfunc variants Date: Tue, 24 Nov 2026 15:48:10 -0700 Message-ID: <20261124224811.16134-6-ihor.solodrai@linux.dev> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261124224811.16134-1-ihor.solodrai@linux.dev> References: <20261124224811.16134-1-ihor.solodrai@linux.dev> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" libbpf 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. Signed-off-by: Ihor Solodrai Acked-by: Eduard Zingerman --- tools/bpf/resolve_btfids/main.c | 69 +++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/tools/bpf/resolve_btfids/main.c b/tools/bpf/resolve_btfids/mai= n.c index 0e193c3c73aa..e21ac6843873 100644 --- a/tools/bpf/resolve_btfids/main.c +++ b/tools/bpf/resolve_btfids/main.c @@ -94,6 +94,7 @@ #include #include #include +#include #include "btf_colors.h" #include "fmodret.h" #include @@ -1918,6 +1919,70 @@ static int sort_btf_by_name(struct btf *btf) return err; } =20 +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 =3D lhs, *b =3D rhs; + int ret =3D strncmp(a->name, b->name, min(a->essent_len, b->essent_len)); + + return ret ?: (a->essent_len > b->essent_len) - (a->essent_len < b->essen= t_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 =3D btf__base_btf(btf); + struct kfunc_variant *kfuncs =3D NULL; + u32 cnt =3D 0, cap =3D 0; + int err =3D 0; + + for (u32 id =3D base ? btf__type_cnt(base) : 1; id < btf__type_cnt(btf); = id++) { + const struct btf_type *t =3D btf__type_by_id(btf, id); + const char *name; + + if (!btf_is_decl_tag(t) || btf_decl_tag(t)->component_idx !=3D -1) + continue; + if (strcmp(btf__name_by_offset(btf, t->name_off), DECL_TAG_KFUNC) !=3D 0) + continue; + + err =3D ensure_mem(&kfuncs, &cap, cnt + 1); + if (err) + goto out; + name =3D btf__name_by_offset(btf, btf__type_by_id(btf, t->type)->name_of= f); + kfuncs[cnt++] =3D (struct kfunc_variant){ + .name =3D name, + .essent_len =3D bpf_core_essential_name_len(name), + .id =3D t->type, + }; + } + qsort(kfuncs, cnt, sizeof(*kfuncs), cmp_kfunc_variants); + + for (u32 i =3D 0; i < cnt; i++) { + const struct btf_type *a =3D btf__type_by_id(btf, kfuncs[i].id); + + for (u32 j =3D i + 1; j < cnt; j++) { + const struct btf_type *b; + + if (cmp_kfunc_variants(&kfuncs[i], &kfuncs[j]) !=3D 0) + break; + b =3D btf__type_by_id(btf, kfuncs[j].id); + if (btf_kfunc_proto_match(btf, a->type, btf, b->type) <=3D 0) + continue; + pr_err("ERROR: resolve_btfids: kfuncs %s and %s have matching prototype= s\n", + kfuncs[i].name, kfuncs[j].name); + err =3D -EINVAL; + } + } +out: + free(kfuncs); + return err; +} + static int finalize_btf(struct object *obj) { struct btf *base_btf =3D obj->base_btf, *btf =3D obj->btf; @@ -1929,6 +1994,10 @@ static int finalize_btf(struct object *obj) return err; } =20 + err =3D check_kfunc_variants(obj->btf); + if (err) + return err; + if (obj->base_btf && obj->distill_base) { err =3D btf__distill_base(obj->btf, &base_btf, &btf); if (err) { --=20 2.56.0 From nobody Wed Nov 25 13:40:53 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.194 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795560529; b=fl7KKUly0pTrnd8zy5J1RDkkasmUzj0yPZrOBrjIJ1A0rJLfj4ygDM0AhJ2ir4y5kQGl bGjy3WpD5HSDvCmEdI0Zu1KTdktHAg4uwnB3C3gGcW9UUH1dEbJ1t09R+dDlFjkanFaqG AQhqTGc8zMQenjssOGvYdz+dYG9kB9jNSiwbALe5el0GbYCcgXTEatNfvsMBxceodl50o hJStWYgX7P7Zn9phKHgZEoOs+zByOhQwZug3msa1ggOqtzOl7kh/lswUfAfz+7ZGt1wwK 7uOfbkhn1W6z8tEqoMV742augIYaiWELv4ez3A4aCEr+g6qR+kll4qzvwstd/pLKcnQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795560529; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=mWnH3KwWgrqBhOLVLwR1FQPdX8lFVp3mEV3Ku0UuUhw=; b=Yyc/kq+CsjYAFsXkd9rL8JR8hABta7JLsZM+/f0eCY4idrrBmQ/7iI18BwhmSajyu8GG 7dx75tLc+6H+rnwb3ZRp8Y1tP8PZdNZmJK0oF8GaxinzMUWJFDZmw0Q6TssC6xKXkd+mc t92LO7b5eASWuoaqZca/XmB1ytU+1dW4ywWlotXKj9VfjEM3ozuHsMOiZ9y5UcplNJXDN sTxdlHC1W8HZSjJUfyv9qvXDYsA+Ut/VDt344nvD+nv7vAItpaAHRS7H8Cfo6ccRCWn/A 2nhG+6/8eXncbTp96eMUGqh7VKxH+Ycj4jr787RdRcfiPdEzgy3GvG8VDh5Y0dqatLw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=MJAlAeXR; arc=none smtp.remote-ip=91.218.175.194 Received: from mta0.migadu.com (out-194.mta0.migadu.com [91.218.175.194]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C2B9C606A323 for ; Tue, 24 Nov 2026 22:48:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C2B9C606A323 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=MJAlAeXR X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=EcqbZB1OxH0qu5490o9LdjMRCZshiMRYEZO7x3iyBtw=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795560526; v=1; x=1796165326; b=MJAlAeXRC3icCHtwqzspF7eXR56XBUsedL73wpkFhXF9IHaL2UMYjMSHaclaBDWsJ6hVeamP ILjOCUhjYfhCaB3SPC3MMuk2/B9kI84/pfzwLRc+Y2JzHdIEE/KuMwnkUdwxdtxHerx2XcZcSMe tErq6+O1zLle4sZ00/tBar4w= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 6b2124b402350240; Tue, 24 Nov 2026 22:48:45 +0000 X-Mizu-Trace-ID: 6b2124b402350240 X-Migadu-Flow: FLOW_OUT From: Ihor Solodrai To: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Eduard Zingerman , Kumar Kartikeya Dwivedi Cc: Alan Maguire , Emil Tsalapatis , Jiri Olsa , Song Liu , Tejun Heo , Yonghong Song , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, sched-ext@lists.linux.dev Subject: [PATCH bpf-next v3 6/6] docs/bpf: Describe how to change a kfunc's prototype Date: Tue, 24 Nov 2026 15:48:11 -0700 Message-ID: <20261124224811.16134-7-ihor.solodrai@linux.dev> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261124224811.16134-1-ihor.solodrai@linux.dev> References: <20261124224811.16134-1-ihor.solodrai@linux.dev> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add 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. Signed-off-by: Ihor Solodrai --- Documentation/bpf/kfuncs.rst | 99 ++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/Documentation/bpf/kfuncs.rst b/Documentation/bpf/kfuncs.rst index 335dbb833bfe..caaa69731339 100644 --- a/Documentation/bpf/kfuncs.rst +++ b/Documentation/bpf/kfuncs.rst @@ -800,6 +800,105 @@ 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. =20 +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 do= es +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 +``___v1`` (``___v2`` for the next change, and so on) for a deprecati= on +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`` when resolving ``bpf_f= oo``. +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 mus= t not +match, not even for aliases: libbpf can't tell them apart and fails to loa= d a +program that declares either one, even as ``__weak``. For example, this wi= dens +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 prot= otypes + +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 lo= ading +the program. + +Give the new prototype a name outside the family, not ``___*``, 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. + +Older libbpf resolves ``bpf_foo___v1`` only from a declaration with a stac= ked +suffix, such as ``bpf_foo___v1___compat``, and light skeletons only from +``bpf_foo___v1`` itself. Don't rename a ``___vN`` kfunc while progra= ms +declaring it that way are in use. libbpf 1.9 and later bind by prototype, = so a +rename doesn't affect them. + 4. Core kfuncs =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 --=20 2.56.0