From nobody Wed Nov 25 13:32:53 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.162 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795221261; b=QVBQzJN5TWMRVYJXiqFY0pCXDNLt+1BeFcaMo+P7GRg8NfqdsewdHvAFKNTOpmx70+F9 uV5rUR1OaCEtB2eLtB8nJHmh6HIwrycqejYNjBiUhd98sxDae8hSx6Cu79KF815UL5FsJ m8Q8dJuYm0N1Dr+XHkM4C/tx2ztSRICXFyHZUmB7iDxNmA2P72zdTDCd5xpMPjwdTuHH6 cDn6o8Y3Wuxd5MTPIwx65crgqkA42mrDjgZT68zJh+ed3ybCjvsx22DRyd+FfL8yL32lc UENpceqa63L7Pkx/ylW0OKJguy2GAHdzyreOtoP8skDgzC6pkWBtnQtGL7WMHjpRBaQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795221261; 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=/D+Zhad86wjuATsbT8ohg/50USn4bj0NsQs4iN//PK8=; b=Eyql8VdDG8LikE0Nwyt9/acDnSm6cGEZZaFDujdenym9+dlY7KuKn1hxZ+hejzI++Gn+ Xfn1Glfk6JO6CmBV4b9mWY4WJM2AFSbX6AnT/tJQD+TX/uRc3nbi2enPhb1qkfddhgEt0 9DOgZ3vaWM9Ip/JTnCGOTt8bhWnqkT3/CbxKb40WwzWRC4QFK/02A4ssesV44VPjDv7kv p2kqd2jNT7BzY4yQt8f07uWgP+VuEnKoym91tOTJGJdYK075VacGDNTOd/2LAe7rFUYty cvCMqRxth5RzJKhZAfShun7Tzpl7th7kkbptITFrqDS9PzNUs/nHvXrT0YS2LNnB5uA== 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=UbBL8BrQ; arc=none smtp.remote-ip=91.218.175.162 Received: from mta0.migadu.com (out-162.mta0.migadu.com [91.218.175.162]) (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 937C9606A6AD for ; Sat, 21 Nov 2026 00:34:20 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 937C9606A6AD 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=UbBL8BrQ X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=vUBLLFu7exKFaLVeWVP1e19lJwUEn1zEA0S7uIyAxXw=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795221258; v=1; x=1795826058; b=UbBL8BrQ8Ro/aOrRc0+d7EsvcSEmxoG3Y7vq9beiNk45QOMQuq/GcoWiR2Efu3VzkjnFmCGj g2xTQ3yz4m5Opv9En7o5s79m6gblE9MA1XFub6xNFnc8OAgvmAaKLN2AjMJs6wTBTh4tgJyOreG z52wuIXVqHlIHHZHE5gp+hEc= X-Envelope-To: linux-kernel@vger.kernel.org Received: by mta11.migadu.com with ESMTPS id edff4338751dc96c; Sat, 21 Nov 2026 00:34:18 +0000 X-Mizu-Trace-ID: edff4338751dc96c 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 v2 1/6] libbpf: Separate interfaces shared with in-tree tools Date: Fri, 20 Nov 2026 17:33:58 -0700 Message-ID: <20261121003403.2928928-2-ihor.solodrai@linux.dev> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261121003403.2928928-1-ihor.solodrai@linux.dev> References: <20261121003403.2928928-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 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. Signed-off-by: Ihor Solodrai --- 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 diff --git a/tools/bpf/bpftool/Makefile b/tools/bpf/bpftool/Makefile index 49dabf2eded0..7e73e12af5b6 100644 --- a/tools/bpf/bpftool/Makefile +++ b/tools/bpf/bpftool/Makefile @@ -28,10 +28,11 @@ 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 -# 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) +# Stage private headers that bpftool uses and libbpf does not install. +LIBBPF_COMMON_INTERNAL_HDRS :=3D hashmap.h relo_core.h libbpf_internal.h l= ibbpf_tools.h +LIBBPF_INTERNAL_HDRS :=3D $(addprefix $(LIBBPF_HDRS_DIR)/,$(LIBBPF_COMMON_= INTERNAL_HDRS) nlattr.h) +LIBBPF_BOOTSTRAP_INTERNAL_HDRS :=3D $(addprefix $(LIBBPF_BOOTSTRAP_HDRS_DI= R)/,\ + $(LIBBPF_COMMON_INTERNAL_HDRS)) =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/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..0ab15b5589d6 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 @@ -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_m= ap, __u32 **str_map); @@ -574,31 +574,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); @@ -772,8 +747,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..c590ea4312f2 --- /dev/null +++ b/tools/lib/bpf/libbpf_tools.h @@ -0,0 +1,46 @@ +/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ +#ifndef __LIBBPF_TOOLS_H +#define __LIBBPF_TOOLS_H + +#include +#include + +/* Internal interfaces for kernel tools that link libbpf statically. */ + +struct btf; +struct btf_header; +struct btf_type; + +const struct btf_header *btf_header(const struct btf *btf); + +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); + +#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.1 From nobody Wed Nov 25 13:32:53 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.167 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795221270; b=A5VzoGErr6ThfZZWGUIQJ5SRkxRqqTN2kLQepyp+RV/RFYhs3zmgj7PuPiBSO1pSt5je tcPpZaWKOSD2GybtveXxUj4Dst/b1FhrwRuiYeJwWKE9Vm68jYTO0jpct0QD+lE+3IsQS /XvhvdVNW3r8455mACCLG/ifPp9beDe0D8MJ+UbNI0HwCjBpjqUhY0qeAKAKwWf8VtA6/ ZvIOASBH3KHv78tcCoeWT9pST1UFDlaGwXQdglzFyV3o4Y/eb6aUlJxBpUThro66UZaXK 6v/BcAYJTXC6l2O2bomKfCsfzENHr9a50hKmYm7x1Q064RponA9KHdQs/xqhR43yY3A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795221270; 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=uBtkiDsj+it2yuD+PNBgJrBOad1TOpfb5WOVADUa3UM=; b=Vbkx2bIT3mEa5GzzBl4kpwhdzoNKvTEWTD/bJUP5zo6iWUce7xjK40Y3yS0S8CAR3HzH aF/j5KAx7hi2WSAIRyxnTJOfcPgTnwU/1j9D0lvMyS0d22ond3q7uA7uJpS6hJxRLLDFB 7cIpJ0SFS19WrZRgX4AdLoqR/52OMTHsVFrVCvICTw2hxn6/nZ5226e4dy/7loUaq1S48 j58tZPLE6Mss08SnT5BGWUHXPL+5LEfNyL4ljpAlDRZU9QIiaJlOIh7OdtkIGM3eSrTdn rUtcNMbbB9dfEL7f+1aKEWOrsvfyBsKzaMm4hrEH+n85MUIrKIe4dZWZlRsS3jMpFaQ== 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=dsWCaO0H; arc=none smtp.remote-ip=91.218.175.167 Received: from mta0.migadu.com (out-167.mta0.migadu.com [91.218.175.167]) (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 A662360A2606 for ; Sat, 21 Nov 2026 00:34:24 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A662360A2606 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=dsWCaO0H X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=6xxqSekLAxGWLipL5wNFTRl3FhuvQzQBfNSr9PL/vKE=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795221263; v=1; x=1795826063; b=dsWCaO0HWVcVuW3Qs7Xbva1dEf/pZ0fPPFrRm1EXweP9sDiRSrpE9ZtTL8DtSlqZhhlorcfC Ag+HX2eun6lVMMUvPkQZYg/n/X17+d/IeAkwPoKUxsZyJmNd5CDIeCRm3yPrtsPc82bTOW1zi0n WRoCLopFvL35DiPaOpFDywTg= X-Envelope-To: linux-kernel@vger.kernel.org Received: by mta11.migadu.com with ESMTPS id 49c793bd95345343; Sat, 21 Nov 2026 00:34:22 +0000 X-Mizu-Trace-ID: 49c793bd95345343 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 v2 2/6] sched_ext: Prefer ___v2 kfunc names in compat.bpf.h Date: Fri, 20 Nov 2026 17:33:59 -0700 Message-ID: <20261121003403.2928928-3-ihor.solodrai@linux.dev> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261121003403.2928928-1-ihor.solodrai@linux.dev> References: <20261121003403.2928928-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. Signed-off-by: Ihor Solodrai --- tools/sched_ext/include/scx/compat.bpf.h | 39 ++++++++++++++++-------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/tools/sched_ext/include/scx/compat.bpf.h b/tools/sched_ext/inc= lude/scx/compat.bpf.h index 254882bd8ce6..16cf255e4221 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,12 +513,12 @@ 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) @@ -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_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.1 From nobody Wed Nov 25 13:32: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=1795221275; b=S8TorDl35U3g6b/zg+ZxkRwQ30VZmIfC+H6JqT+UxV/5dXmyWWlQPpF4c2n+wbLQBnOn VXb4XBmV6Xy7I+UFSpm1XMoSlIMMMdZFiipYyyD3hj4w/kg/JHWBxfff20MTBJzkCvAGk uNzY99/Md+BcjKnuLyhZR7bF2+Bz3YhlqVum7cMnU2Zo0Y93U6eY9/5QAKyytHfP8+9OF nWnk+mT0N5O/Jr+sKxxdu8hv+QIMrJbGWxNLN18BlOYfNdAAtzMW5BtU+yKw0GT+4AMhv NKcuZaq4IUHw6koeJZfr0mrJ9qeJkoUrVlmtvM+vBZzPDmPC6nbR3QiPLuMz+O9hifg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795221275; 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=UakQ/lVZu6ZklDKD0lITWhsDfWqXnOk3rxLWD0AR4iI=; b=R1pYK+GK58S0xBKYvuZyvMxx9v8U1gFB7OQ2+l2gjsgPY5aG7ml3+8ZBDw4fPvMQPAib bSuMnsjQDPJaClnapuVx1+jybcHtC/f2VkoqFX+JVdiRxVeHzqC3mopGLSwHzWXgTxGZn z6+UN9vXm3pI+oMVJqSU+L7daeIDZSdUcCRuNGf4MBjWaiys0vY/YFoUtPiSAesx2YrKj 1QsCcZyiTNUMDeZmf1GccYQ6LkKz+IqavWGKePpuPh0wzL5SkBEnDWCLN+p4NmKwFiBv1 sniGd2MSQkQHq0QXm4TIvtsnRVAdBb0SnuCxD9EBiSbOwCV6+D+cpkH8TozRVr7wvzg== 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=ga2cAh0G; 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 B865D606A33D for ; Sat, 21 Nov 2026 00:34:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B865D606A33D 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=ga2cAh0G X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=K9SJ+mNptETAOYUhXMxL24aCdmOEQZ4Pqm8uQvEQ9z4=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795221270; v=1; x=1795826070; b=ga2cAh0GDKz5Zpizb4kGBnzH/EKQ4G8e0oQpJOpQLR4OHhUe8GdbI1tAI7J27DcbuE61whdw ZbSkJrjWCQC1F5EMdaQFOzMN5dFPx+sWETkE+kANKuN+InycO13Yh1EYpUbqk+oEIc8ljD/wbDe aY/4HOZmS2s1cmG3NB4EnrM8= X-Envelope-To: linux-kernel@vger.kernel.org Received: by mta11.migadu.com with ESMTPS id 2523e64529c72b34; Sat, 21 Nov 2026 00:34:30 +0000 X-Mizu-Trace-ID: 2523e64529c72b34 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 v2 3/6] libbpf: Resolve kfunc variants by prototype Date: Fri, 20 Nov 2026 17:34:00 -0700 Message-ID: <20261121003403.2928928-4-ihor.solodrai@linux.dev> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261121003403.2928928-1-ihor.solodrai@linux.dev> References: <20261121003403.2928928-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 | 117 ++++++++++++++++++++++---------- tools/lib/bpf/libbpf_internal.h | 2 + tools/lib/bpf/libbpf_tools.h | 2 + 4 files changed, 182 insertions(+), 38 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..9439dc2816ea 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,99 @@ 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; + * - another negative error on failure. + */ +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, err; + size_t i; + + err =3D load_module_btfs(obj); + if (err) + return err; + + 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; - - local_func_proto_id =3D ext->ksym.type_id; + int kfunc_id, 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; + } + if (kfunc_id < 0) + 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 +9627,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 +10490,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 0ab15b5589d6..c3dadbfbe43d 100644 --- a/tools/lib/bpf/libbpf_internal.h +++ b/tools/lib/bpf/libbpf_internal.h @@ -251,6 +251,8 @@ const struct btf_type *skip_mods_and_typedefs(const str= uct 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_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 c590ea4312f2..6068146eabd2 100644 --- a/tools/lib/bpf/libbpf_tools.h +++ b/tools/lib/bpf/libbpf_tools.h @@ -12,6 +12,8 @@ struct btf_header; struct btf_type; =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); =20 enum btf_field_iter_kind { BTF_FIELD_ITER_IDS, --=20 2.56.1 From nobody Wed Nov 25 13:32:53 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.171 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795221277; b=GcWLvwMHBIh4hX61RdSgqg33xk713sJvGzvRiSUKKhFytHQrWeN83BoBXMuvJOmKIzfg FKbwUZJ474MVtb4IfllMUwOV39mZErbnrs8bMsz7qRSFRa016nyuE/6E7HdINTpOK8aOF CAMJvF8XuIkDvUfY/kkDBrvW40swqPdXljF365CLlfa+fvjp+yLL9oXECuIgBe/O/yjZ9 HWSvgKkwnH+mMVXW6Cq02cIRULRJiJBk4keEtnmDmnvMhsxdszcnt2Pmu/qYn7Ocho1dQ X8x8fd+YcEamKvTyKn5ZTf95dMbMX5wWpNw0K/kDFTL140vrKoZ964y5HpxuHcVuErA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795221277; 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=y3cU+YzuK5fSdgfjLmYUz1+UbwA40Jui0DNzbOCRe4A=; b=q5WhgkQEDuz5ER/o+WqXrI3LmLKReqFM0weheCMIGsRu4aktKkgq50iektrvb5EAAeWj OXu02icfkPTsrNYNi6oMCB/cpMDBVS6uKQ8eRo9ZKdgi+8i5GbTm7GRb6VJgbQoL/4Njv VvLtqJQE19tgeBMFpGxW0zIg+ye6SE2iBCX0XeFuO6RFVyYTPUGkD1Q96M0K4Ia+gHBU1 esLRcR+K1cvUzQ9ej6Q1+gWy5mfhdf2cyTMg58wzrlOoySlFBKzJyt2/tUO0ycOLDeqDW eclaRZfR31x/P5EdReQwMxm5tcmFz8Ye9Qa+iicvILoku511fcD91rmk5xWfvCm58Tw== 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=QAcC69+H; arc=none smtp.remote-ip=95.215.58.171 Received: from mta1.migadu.com (out-171.mta1.migadu.com [95.215.58.171]) (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 3F474606A6BD for ; Sat, 21 Nov 2026 00:34:36 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3F474606A6BD 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=QAcC69+H X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=UtKDaPphJFgbgeOsAny5vFb+2JgRSJB+5tieS7dfq5E=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795221274; v=1; x=1795826074; b=QAcC69+Hd8YGDXCNJoMJLk1pv4TRiyCMZiqyFEbsNh8kD/RZOUxC/y6IEEzuxQnlpMhcoPrn 9IBvgWHiuWzqfWA/FP0FGhatHEpXF01mK4yB4LFw8ZU9YSfjb1qrTUkR+4V6GDKDVPxRKIlt4ry NKVp8DRr/eEFLjJSfXuGCHC8= X-Envelope-To: linux-kernel@vger.kernel.org Received: by mta11.migadu.com with ESMTPS id 8bdd36f73602adcd; Sat, 21 Nov 2026 00:34:34 +0000 X-Mizu-Trace-ID: 8bdd36f73602adcd 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 v2 4/6] selftests/bpf: Test kfunc variant selection by prototype Date: Fri, 20 Nov 2026 17:34:01 -0700 Message-ID: <20261121003403.2928928-5-ihor.solodrai@linux.dev> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261121003403.2928928-1-ihor.solodrai@linux.dev> References: <20261121003403.2928928-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 57ff2578a991..3684670791a8 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" @@ -220,6 +221,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.1 From nobody Wed Nov 25 13:32:53 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.181 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795221285; b=JowT+qAESaw0OVsXoqs8UD70ogGAZpvfyhoZzICRsA5ezXg1e/edxClHW2yw0yzokoYp 2omzzttwhFLaXcM6dNSzUUwCbMmvsnefEqxxzEQyxONu6se5Gy3GeIIWqqdsRomr1IjKK Nl88aUJQamzwLJrCPElJ7hvy8XwWjXUPwGmpNMy7uSuJxy+JAPEPBT04B+9XldMxRHwFW /p2/IUbbsOaPReIjNjIv8STlu2fj8UzN5BEUfGvA7PQI/5Ny0/8Myd5V6Sg5fJwbKLekA TH9viPwZqVQZ/LVbXR2QZL/74IxhDpW6O1Ofubfko6GeQfSYz8/3HNu9mFYiRKlMk2Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795221285; 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=16uHI/wiILJAgxZZpw+Ll3nL/DkkAuBU3LPRe4/AFK0=; b=3wiBMf+xpD/WnNSNIuvCvQChq8MI7UEpJsMiWiYrXh8TY7PhtsntxEDY5d3zwl72PBCp irdCs/a1kiJUU082g6bEY0QzwrdxgWLkD1RalYQ70SCRecDti+5e4x7olYZekxC+oWP66 vyEYkZDaSsq9cKCh0MhXEz6hgzje/7b41wt6D6UtB+D3JHDQoFGk2g+XV3QxExIQTt+8k Rk8L0spPe3WYe3rsxsnjT0EapJVWgQo7O0vVaxf6NpO8S3eQaxNuRMfo7OFoszEXa8UmS javJ9MCpxCYWgQHfkcdEEIme0wE0dwu4Ixwfvi57ZyVBh7R98pm+EaXnhOkjyjHDnVQ== 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=fl3MurGo; arc=none smtp.remote-ip=95.215.58.181 Received: from mta1.migadu.com (out-181.mta1.migadu.com [95.215.58.181]) (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 50CCD606A338 for ; Sat, 21 Nov 2026 00:34:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 50CCD606A338 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=fl3MurGo X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=chSHStAxYYQbOaPVRpfqdX9lguVoKe6kfnjSXgmTuSc=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795221282; v=1; x=1795826082; b=fl3MurGoH7lHrmHdOMDOKMsubUBXWP+pz9sD0PKTGgoj4GrzFKNVVFldsMyhMu8hj+JVnaRK hIvgNK0lHHiyudGrfkgLEAyplOJSfGv6zpPm2vvuPvelj/qtQ8Z57yEs0JFr5ft99nogRcX/3zt NEhbOerSu6RBh9C5vNL9bzj8= X-Envelope-To: linux-kernel@vger.kernel.org Received: by mta11.migadu.com with ESMTPS id f63e0e71d92a25d7; Sat, 21 Nov 2026 00:34:42 +0000 X-Mizu-Trace-ID: f63e0e71d92a25d7 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 v2 5/6] resolve_btfids: Reject ambiguous kfunc variants Date: Fri, 20 Nov 2026 17:34:02 -0700 Message-ID: <20261121003403.2928928-6-ihor.solodrai@linux.dev> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261121003403.2928928-1-ihor.solodrai@linux.dev> References: <20261121003403.2928928-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.1 From nobody Wed Nov 25 13:32:53 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.186 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795221292; b=Cqlz5hEo8h8LhBZKsdQKL7kDZm1P6GcefhrgypgR7w5wbvgNaQ9DLVrOyi4KpKMmAom8 R1eln1QoxxS8Fa2qWn9Y3HxhMTLZWk6RH6gE/vsb88xbshFLFLqBV3VPrBmljiFneoZC6 dKu/ZIxlcYJHEcopjapVv+8n4KoVt/GRHLsJQG5Wp1hg/ddTwbfLbpiMbNmUw3ySvt79/ hkXUVoW110RmpIKz+e8n5buwRrUe55rK6Q3z9kS19v282SFFTyeNYs9yLs62CIRL0SfuV f8p5QXIkhcGfQhHT6+wQxB2ASvSWo9gV35+nuDYj8AUHQRWmUlyYinGrV6+MjuuMy4w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795221292; 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=IJxvQwmayN5aQvdb4D+gCDBM0pWPSVbeJfqswUb7Wok=; b=y0R2lJ3g0dTJLda3/nfdF1eZogtKWfdITAQooOozLXHLt3Q8fQi5QjtNrtf8kSviLksa YN2x1UGNJv3QTXrCsBs7JfGWelY/yrNapBr/Dn0hXfdzkx2bHIgDquxWVwBIk4jNrMVfj exuigPe8sHe3qMKq1YSAjlVjNubdrwIfo533UFKwpDjmZikNccf24ecaekRifLYM4D+2z rjSh0Ot0+uKuIqDwN3YziTYN4II4E50ydcROzQ7QD1SOblJtRBXfzMDO1fNS2B8A4Dgu+ nVl2Pm61YZS3FixvMk69XkQL80LdC2WYceoCC968+GwpDq7xX+APo7cd96UOogE1Ryw== 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=t++J3V0s; arc=none smtp.remote-ip=95.215.58.186 Received: from mta1.migadu.com (out-186.mta1.migadu.com [95.215.58.186]) (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 322D0606A68C for ; Sat, 21 Nov 2026 00:34:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 322D0606A68C 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=t++J3V0s X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=lxpd3jvcjo6xVOgK2t+dAOJAF/GseY3ufo1PEnDz03A=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795221287; v=1; x=1795826087; b=t++J3V0skR6gynoHIH/0m0VsGC5pIUnFYhlVYaNriusrNqBoKBzR8wjw9m/LSMzWc1WjG/7B m4fZkLWzTowf5++WFHo8vEHeEx0aSaEJKcaiMWs5rZ7icUtVoG0iHdz8vo8BR4ocK44yAMOgDDa bEXQ5sXhHG6is3K4kZYTb1b8= X-Envelope-To: linux-kernel@vger.kernel.org Received: by mta11.migadu.com with ESMTPS id 4f055eea9cb761d8; Sat, 21 Nov 2026 00:34:47 +0000 X-Mizu-Trace-ID: 4f055eea9cb761d8 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 v2 6/6] docs/bpf: Describe how to change a kfunc's prototype Date: Fri, 20 Nov 2026 17:34:03 -0700 Message-ID: <20261121003403.2928928-7-ihor.solodrai@linux.dev> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261121003403.2928928-1-ihor.solodrai@linux.dev> References: <20261121003403.2928928-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 | 96 ++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/Documentation/bpf/kfuncs.rst b/Documentation/bpf/kfuncs.rst index c7da603cab77..4d60a13ecbe3 100644 --- a/Documentation/bpf/kfuncs.rst +++ b/Documentation/bpf/kfuncs.rst @@ -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. =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``. 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. + +Don't rename a ``___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 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 --=20 2.56.1