mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Ihor Solodrai <ihor.solodrai@linux.dev>
To: Alexei Starovoitov <ast@kernel.org>,
	Andrii Nakryiko <andrii@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Kumar Kartikeya Dwivedi <memxor@gmail.com>
Cc: Alan Maguire <alan.maguire@oracle.com>,
	Emil Tsalapatis <emil@etsalapatis.com>,
	Jiri Olsa <jolsa@kernel.org>, Song Liu <song@kernel.org>,
	Tejun Heo <tj@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	bpf@vger.kernel.org, linux-kernel@vger.kernel.org,
	sched-ext@lists.linux.dev
Subject: [PATCH bpf-next v1 1/4] libbpf: Fall back to kernel ___ flavors when resolving kfuncs
Date: Tue, 17 Nov 2026 12:21:50 -0700	[thread overview]
Message-ID: <20261117192153.3384031-2-ihor.solodrai@linux.dev> (raw)
In-Reply-To: <20261117192153.3384031-1-ihor.solodrai@linux.dev>

When resolving a kfunc, libbpf strips the ___<flavor> suffix from its
name and binds the kfunc with the resulting name if its prototype is
compatible. So when a kfunc foo changes its prototype, the kernel can't
keep the old one available to existing BPF programs as foo___v1: they
declare foo, and libbpf never looks at foo___v1.

sched_ext worked around this by giving the new prototype the name
scx_bpf_dsq_insert___v2 and spelling it scx_bpf_dsq_insert___v2___compat
in BPF programs, so that the stripped name matches the kernel name
exactly. That leaks kernel naming into BPF programs, and calling the
kfunc as vmlinux.h declares it fails: scx_bpf_dsq_insert___v2 strips to
scx_bpf_dsq_insert, whose prototype is incompatible.

If the kfunc with the stripped name is missing or its prototype is
incompatible, look among the kfuncs named <name>___<flavor>, in vmlinux
first and then in the first module that has any, and bind the one whose
prototype is compatible. Only FUNCs with the bpf_kfunc decl tag are
candidates. More than one compatible flavor is an error even for a kfunc
declared __weak: ambiguity isn't absence.

The lookup of the stripped name comes first and is unchanged, so
bindings that work today stay the same.

Print the name of the chosen kfunc in the debug message.

Signed-off-by: Ihor Solodrai <ihor.solodrai@linux.dev>
---
 tools/lib/bpf/btf.c             |  26 ++++++
 tools/lib/bpf/libbpf.c          | 140 +++++++++++++++++++++++++++++++-
 tools/lib/bpf/libbpf_internal.h |   1 +
 3 files changed, 165 insertions(+), 2 deletions(-)

diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
index a4bf01403a43..9eb13c8a95a5 100644
--- a/tools/lib/bpf/btf.c
+++ b/tools/lib/bpf/btf.c
@@ -1164,6 +1164,32 @@ static __s32 btf_find_type_by_name_bsearch(const struct btf *btf, const char *na
 	return btf__type_cnt(btf);
 }
 
+/*
+ * Set [*start, *end) to the IDs of btf's own types that can have a name
+ * starting with prefix. On BTF sorted by name these are exactly the ones that
+ * do, a contiguous run found by binary search; otherwise all own types.
+ */
+void btf_name_prefix_range(const struct btf *btf, const char *prefix, __u32 *start, __u32 *end)
+{
+	__u32 i, n = btf__type_cnt(btf);
+	size_t len = strlen(prefix);
+
+	if (btf->named_start_id <= 0) {
+		*start = btf->start_id;
+		*end = n;
+		return;
+	}
+	*start = btf_find_type_by_name_bsearch(btf, prefix, btf->named_start_id);
+	for (i = *start; i < n; i++) {
+		const struct btf_type *t = btf_type_by_id(btf, i);
+		const char *name = btf__str_by_offset(btf, t->name_off);
+
+		if (strncmp(name, prefix, len) != 0)
+			break;
+	}
+	*end = i;
+}
+
 static __s32 btf_find_by_name_kind(const struct btf *btf, int start_id,
 				   const char *type_name, __s32 kind)
 {
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 1efc0dd37b2e..9089e9c55dfd 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -9513,6 +9513,130 @@ static int bpf_object__resolve_ksym_var_btf_id(struct bpf_object *obj,
 	return 0;
 }
 
+/* t is the bpf_kfunc decl tag of FUNC id */
+static inline bool btf_is_kfunc_tag(const struct btf *btf, const struct btf_type *t, int id)
+{
+	const char *name;
+
+	if (!btf_is_decl_tag(t) || t->type != id)
+		return false;
+	if (btf_decl_tag(t)->component_idx != -1)
+		return false;
+	name = btf__name_by_offset(btf, t->name_off);
+	return strcmp(name, "bpf_kfunc") == 0;
+}
+
+/* FUNC id has the bpf_kfunc decl tag */
+static bool btf_is_kfunc(const struct btf *btf, int id)
+{
+	__u32 i, n;
+
+	btf_name_prefix_range(btf, "bpf_kfunc", &i, &n);
+	for (; i < n; i++) {
+		if (btf_is_kfunc_tag(btf, btf__type_by_id(btf, i), id))
+			return true;
+	}
+	return false;
+}
+
+/* id is a kfunc named <name>___<flavor>, where len is strlen(name) */
+static bool btf_is_kfunc_flavor(const struct btf *btf, int id, const char *name, size_t len)
+{
+	const struct btf_type *t = btf__type_by_id(btf, id);
+	const char *kname;
+
+	if (!btf_is_func(t))
+		return false;
+	kname = btf__name_by_offset(btf, t->name_off);
+	if (strncmp(kname, name, len) != 0)
+		return false;
+	/* kname is <name> itself, already tried by the exact lookup */
+	if (kname[len] == '\0')
+		return false;
+	if (bpf_core_essential_name_len(kname) != len)
+		return false;
+	return btf_is_kfunc(btf, id);
+}
+
+/*
+ * Find the kfunc named <name>___<flavor> among btf's own types whose
+ * prototype is compatible with ext's.
+ *
+ * Returns the kfunc's type id, 0 if no flavor is compatible, -ESRCH if there
+ * are no flavors, or -ENOTUNIQ if more than one is compatible.
+ */
+static int find_kfunc_flavor(const struct bpf_object *obj, const struct extern_desc *ext,
+			     const struct btf *btf)
+{
+	const char *name = ext->essent_name ?: ext->name;
+	size_t len = strlen(name);
+	bool has_flavors = false;
+	int found = 0;
+	__u32 i, n;
+
+	btf_name_prefix_range(btf, name, &i, &n);
+	for (; i < n; i++) {
+		const struct btf_type *t = btf__type_by_id(btf, i);
+
+		if (!btf_is_kfunc_flavor(btf, i, name, len))
+			continue;
+		has_flavors = true;
+		if (bpf_core_types_are_compat(obj->btf, ext->ksym.type_id, btf, t->type) <= 0)
+			continue;
+		if (found)
+			return -ENOTUNIQ;
+		found = i;
+	}
+	if (!has_flavors)
+		return -ESRCH;
+	return found;
+}
+
+/*
+ * Like find_ksym_btf_id(), but for a ___flavor of ext's kfunc: vmlinux first,
+ * then the first module that has any. *res_btf and *res_mod_btf are only set
+ * when a compatible flavor is found.
+ */
+static int find_kfunc_flavor_btf_id(struct bpf_object *obj, const struct extern_desc *ext,
+				    struct btf **res_btf, struct module_btf **res_mod_btf)
+{
+	struct module_btf *mod_btf = NULL;
+	struct btf *btf = obj->btf_vmlinux;
+	int i, id, err;
+
+	id = find_kfunc_flavor(obj, ext, btf);
+	if (id == -ESRCH) {
+		err = load_module_btfs(obj);
+		if (err)
+			return err;
+
+		for (i = 0; i < obj->btf_module_cnt && id == -ESRCH; i++) {
+			mod_btf = &obj->btf_modules[i];
+			btf = mod_btf->btf;
+			id = find_kfunc_flavor(obj, ext, btf);
+		}
+	}
+	if (id > 0) {
+		*res_btf = btf;
+		*res_mod_btf = mod_btf;
+	}
+	return id;
+}
+
+/* ext's kfunc is missing, or kfunc_id's prototype is incompatible with ext's */
+static bool need_kfunc_flavor(const struct bpf_object *obj, const struct extern_desc *ext,
+			      const struct btf *kern_btf, int kfunc_id)
+{
+	const struct btf_type *t;
+
+	if (kfunc_id == -ESRCH)
+		return true;
+	if (kfunc_id < 0)
+		return false;
+	t = btf__type_by_id(kern_btf, kfunc_id);
+	return bpf_core_types_are_compat(obj->btf, ext->ksym.type_id, kern_btf, t->type) <= 0;
+}
+
 static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object *obj,
 						struct extern_desc *ext)
 {
@@ -9526,6 +9650,17 @@ static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object *obj,
 
 	kfunc_id = find_ksym_btf_id(obj, ext->essent_name ?: ext->name, BTF_KIND_FUNC, &kern_btf,
 				    &mod_btf);
+	if (need_kfunc_flavor(obj, ext, kern_btf, kfunc_id)) {
+		ret = find_kfunc_flavor_btf_id(obj, ext, &kern_btf, &mod_btf);
+		if (ret == -ENOTUNIQ) {
+			/* an error even if declared __weak: ambiguity isn't absence */
+			pr_warn("extern (func ksym) '%s': more than one compatible kfunc flavor\n",
+				ext->name);
+			return ret;
+		}
+		if (ret && ret != -ESRCH)
+			kfunc_id = ret;
+	}
 	if (kfunc_id < 0) {
 		if (kfunc_id == -ESRCH && ext->is_weak)
 			return 0;
@@ -9579,8 +9714,9 @@ static int bpf_object__resolve_ksym_func_btf_id(struct bpf_object *obj,
 	 * {kernel_btf_id, kernel_btf_obj_fd} -> fixup ld_imm64.
 	 */
 	ext->ksym.kernel_btf_obj_fd = 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);
 
 	return 0;
 }
diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h
index e90b5336bac0..c84521ea562e 100644
--- a/tools/lib/bpf/libbpf_internal.h
+++ b/tools/lib/bpf/libbpf_internal.h
@@ -248,6 +248,7 @@ 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_name_prefix_range(const struct btf *btf, const char *prefix, __u32 *start, __u32 *end);
 void btf_set_base_btf(struct btf *btf, const struct btf *base_btf);
 int btf_relocate(struct btf *btf, const struct btf *base_btf, __u32 **id_map,
 		 __u32 **str_map);
-- 
2.56.1


  reply	other threads:[~2026-11-17 19:22 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 19:21 [PATCH bpf-next v1 0/4] libbpf: Support kernel-side kfunc flavors Ihor Solodrai
2026-11-17 19:21 ` Ihor Solodrai [this message]
2026-11-17 19:21 ` [PATCH bpf-next v1 2/4] selftests/bpf: Test kfunc flavor resolution Ihor Solodrai
2026-11-17 19:21 ` [PATCH bpf-next v1 3/4] resolve_btfids: Reject kfunc families that libbpf can't disambiguate Ihor Solodrai
2026-11-17 19:21 ` [PATCH bpf-next v1 4/4] docs/bpf: Describe how to change a kfunc's prototype Ihor Solodrai
2026-11-17 20:53 ` [PATCH bpf-next v1 0/4] libbpf: Support kernel-side kfunc flavors Tejun Heo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261117192153.3384031-2-ihor.solodrai@linux.dev \
    --to=ihor.solodrai@linux.dev \
    --cc=alan.maguire@oracle.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=memxor@gmail.com \
    --cc=sched-ext@lists.linux.dev \
    --cc=song@kernel.org \
    --cc=tj@kernel.org \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®