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 2/4] selftests/bpf: Test kfunc flavor resolution
Date: Tue, 17 Nov 2026 12:21:51 -0700	[thread overview]
Message-ID: <20261117192153.3384031-3-ihor.solodrai@linux.dev> (raw)
In-Reply-To: <20261117192153.3384031-1-ihor.solodrai@linux.dev>

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@linux.dev>
---
 .../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


  parent 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 ` [PATCH bpf-next v1 1/4] libbpf: Fall back to kernel ___ flavors when resolving kfuncs Ihor Solodrai
2026-11-17 19:21 ` Ihor Solodrai [this message]
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-3-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®