[PATCH bpf-next v1 2/4] selftests/bpf: Test kfunc flavor resolution
From: Ihor Solodrai
Date: Tue Nov 17 2026 - 14:22:44 EST
Add a kfunc family to bpf_testmod: bpf_kfunc_flavor() with the current
prototype, bpf_kfunc_flavor___v1() with an older one that takes one
fewer parameter, and bpf_kfunc_flavor___v2(), an alias with the exact
prototype of bpf_kfunc_flavor().
Check that libbpf binds:
- bpf_kfunc_flavor to the kfunc itself rather than to its alias ___v2;
- the old prototype to ___v1, both under a local flavor and under the
kernel name, as a header generated from BTF spells it;
- the kernel name with a local flavor on top to ___v1;
- a __weak kfunc that matches no member to nothing.
Signed-off-by: Ihor Solodrai <ihor.solodrai@xxxxxxxxx>
---
.../selftests/bpf/prog_tests/verifier.c | 3 +
.../selftests/bpf/progs/kfunc_flavors.c | 55 +++++++++++++++++++
.../selftests/bpf/test_kmods/bpf_testmod.c | 24 ++++++++
.../bpf/test_kmods/bpf_testmod_kfunc.h | 4 ++
4 files changed, 86 insertions(+)
create mode 100644 tools/testing/selftests/bpf/progs/kfunc_flavors.c
diff --git a/tools/testing/selftests/bpf/prog_tests/verifier.c b/tools/testing/selftests/bpf/prog_tests/verifier.c
index 57ff2578a991..351e53744e37 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_flavors.skel.h"
#include "verifier_aggregate_arg.skel.h"
#include "verifier_aggregate_ret.skel.h"
#include "verifier_alu_range.skel.h"
@@ -220,6 +221,8 @@ void test_arena_kfunc_jit(void) { RUN_TESTS(arena_kfunc_jit); }
void test_cb_refs(void) { RUN_TESTS(cb_refs); }
+void test_kfunc_flavors(void) { RUN_TESTS(kfunc_flavors); }
+
void test_verifier_aggregate_arg(void) { RUN_TESTS(verifier_aggregate_arg); }
void test_verifier_aggregate_ret(void) { RUN_TESTS(verifier_aggregate_ret); }
void test_verifier_alu_range(void) { RUN(verifier_alu_range); }
diff --git a/tools/testing/selftests/bpf/progs/kfunc_flavors.c b/tools/testing/selftests/bpf/progs/kfunc_flavors.c
new file mode 100644
index 000000000000..6b06cb8d8447
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/kfunc_flavors.c
@@ -0,0 +1,55 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
+
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+#include "../test_kmods/bpf_testmod_kfunc.h"
+
+/* the old prototype of bpf_kfunc_flavor(), under local flavors */
+extern int bpf_kfunc_flavor___old(int a) __ksym;
+extern int bpf_kfunc_flavor___v1___local(int a) __ksym;
+/* a prototype that no member of the family has */
+extern int bpf_kfunc_flavor___none(void) __weak __ksym;
+
+char _license[] SEC("license") = "GPL";
+
+/* bpf_kfunc_flavor binds itself, though its alias ___v2 matches too */
+SEC("syscall")
+__retval(3)
+int exact_name(void *ctx)
+{
+ return bpf_kfunc_flavor(1, 2);
+}
+
+/* the old prototype under a local flavor binds ___v1 */
+SEC("syscall")
+__retval(1)
+int local_flavor(void *ctx)
+{
+ return bpf_kfunc_flavor___old(1);
+}
+
+/* so does the kernel name, which strips to bpf_kfunc_flavor */
+SEC("syscall")
+__retval(1)
+int kernel_flavor(void *ctx)
+{
+ return bpf_kfunc_flavor___v1(1);
+}
+
+/* a local flavor on top of the kernel name binds ___v1 by name */
+SEC("syscall")
+__retval(1)
+int stacked_flavor(void *ctx)
+{
+ return bpf_kfunc_flavor___v1___local(1);
+}
+
+/* a __weak kfunc that matches no member stays unresolved */
+SEC("syscall")
+__retval(0)
+int no_match(void *ctx)
+{
+ return bpf_ksym_exists(bpf_kfunc_flavor___none);
+}
diff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
index 1f8bc0c99a71..e16559607270 100644
--- a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
+++ b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
@@ -1926,6 +1926,27 @@ __bpf_kfunc void bpf_kfunc_trigger_ctx_check(void)
irq_work_queue(&ctx_check_irq);
}
+/*
+ * A kfunc family for the kfunc_flavors test: bpf_kfunc_flavor() has the
+ * current prototype, ___v1 an older one, and ___v2 is an alias with its exact
+ * prototype. ___v2 negates the result, so that the test can tell which of the
+ * two it called.
+ */
+__bpf_kfunc int bpf_kfunc_flavor(int a, int b)
+{
+ return a + b;
+}
+
+__bpf_kfunc int bpf_kfunc_flavor___v1(int a)
+{
+ return bpf_kfunc_flavor(a, 0);
+}
+
+__bpf_kfunc int bpf_kfunc_flavor___v2(int a, int b)
+{
+ return -bpf_kfunc_flavor(a, b);
+}
+
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)
@@ -2017,6 +2038,9 @@ BTF_ID_FLAGS(func, bpf_kfunc_implicit_arg, KF_IMPLICIT_ARGS)
BTF_ID_FLAGS(func, bpf_kfunc_implicit_arg_legacy, KF_IMPLICIT_ARGS)
BTF_ID_FLAGS(func, bpf_kfunc_implicit_arg_legacy_impl)
BTF_ID_FLAGS(func, bpf_kfunc_trigger_ctx_check)
+BTF_ID_FLAGS(func, bpf_kfunc_flavor)
+BTF_ID_FLAGS(func, bpf_kfunc_flavor___v1)
+BTF_ID_FLAGS(func, bpf_kfunc_flavor___v2)
BTF_KFUNCS_END(bpf_testmod_check_kfunc_ids)
static int bpf_testmod_ops_init(struct btf *btf)
diff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h b/tools/testing/selftests/bpf/test_kmods/bpf_testmod_kfunc.h
index 0a7df1798603..2c6126e4f0d0 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,8 @@ void bpf_testmod_test_hardirq_fn(void);
void bpf_testmod_test_softirq_fn(void);
void bpf_kfunc_trigger_ctx_check(void) __ksym;
+int bpf_kfunc_flavor(int a, int b) __ksym;
+int bpf_kfunc_flavor___v1(int a) __ksym;
+int bpf_kfunc_flavor___v2(int a, int b) __ksym;
+
#endif /* _BPF_TESTMOD_KFUNC_H */
--
2.56.1