Skip to content

Commit 0af15f3

Browse files
kkdwvdeddyz87
authored andcommitted
selftests/bpf: Cover tracing implicit kfunc args
KF_IMPLICIT_ARGS kfuncs have a BPF-call prototype and a real kernel target prototype. Add a tracing selftest that attaches fentry and fexit programs to bpf_kfunc_implicit_arg(), runs a syscall BPF program that calls it, and checks that the tracing context exposes both the explicit argument and the implicit prog aux pointer. Co-developed-by: Ihor Solodrai <ihor.solodrai@linux.dev> Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com> Signed-off-by: Ihor Solodrai <ihor.solodrai@linux.dev> Link: https://patch.msgid.link/20260713235223.1639022-3-ihor.solodrai@linux.dev Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
1 parent 3917b10 commit 0af15f3

2 files changed

Lines changed: 113 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
3+
4+
#include <test_progs.h>
5+
#include "kfunc_implicit_args_tracing.skel.h"
6+
7+
void test_kfunc_implicit_args_tracing(void)
8+
{
9+
struct kfunc_implicit_args_tracing *skel;
10+
LIBBPF_OPTS(bpf_test_run_opts, topts);
11+
int err, fd;
12+
13+
skel = kfunc_implicit_args_tracing__open_and_load();
14+
if (!ASSERT_OK_PTR(skel, "open_and_load"))
15+
return;
16+
17+
err = kfunc_implicit_args_tracing__attach(skel);
18+
if (!ASSERT_OK(err, "attach"))
19+
goto cleanup;
20+
21+
fd = bpf_program__fd(skel->progs.trigger_implicit_arg);
22+
err = bpf_prog_test_run_opts(fd, &topts);
23+
if (!ASSERT_OK(err, "test_run"))
24+
goto cleanup;
25+
26+
ASSERT_EQ(topts.retval, 5, "kfunc_retval");
27+
ASSERT_EQ(skel->bss->fentry_arg_cnt, 2, "fentry_arg_cnt");
28+
ASSERT_NEQ(skel->bss->fentry_aux_arg, 0, "fentry_aux_arg");
29+
ASSERT_EQ(skel->bss->fentry_result, 1, "fentry_result");
30+
ASSERT_EQ(skel->bss->fexit_arg_cnt, 2, "fexit_arg_cnt");
31+
ASSERT_NEQ(skel->bss->fexit_aux_arg, 0, "fexit_aux_arg");
32+
ASSERT_EQ(skel->bss->fexit_result, 1, "fexit_result");
33+
34+
cleanup:
35+
kfunc_implicit_args_tracing__destroy(skel);
36+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
3+
4+
#include <vmlinux.h>
5+
#include <bpf/bpf_helpers.h>
6+
#include <bpf/bpf_tracing.h>
7+
#include <errno.h>
8+
9+
extern int bpf_kfunc_implicit_arg(int a) __weak __ksym;
10+
11+
char _license[] SEC("license") = "GPL";
12+
13+
/* Shared arg checks; reports arg count and aux, returns 1 on success. */
14+
static __always_inline __u64
15+
check_implicit_args(void *ctx, __u64 *arg_cnt, __u64 *aux_arg)
16+
{
17+
__u64 a = 0, aux = 0, z = 0;
18+
__u64 result;
19+
__s64 err;
20+
21+
*arg_cnt = bpf_get_func_arg_cnt(ctx);
22+
result = *arg_cnt == 2;
23+
24+
err = bpf_get_func_arg(ctx, 0, &a);
25+
result &= err == 0 && (int)a == 5;
26+
27+
err = bpf_get_func_arg(ctx, 1, &aux);
28+
*aux_arg = aux;
29+
result &= err == 0 && aux != 0;
30+
31+
err = bpf_get_func_arg(ctx, 2, &z);
32+
result &= err == -EINVAL;
33+
34+
return result;
35+
}
36+
37+
__u64 fentry_result;
38+
__u64 fentry_arg_cnt;
39+
__u64 fentry_aux_arg;
40+
41+
SEC("fentry/bpf_kfunc_implicit_arg")
42+
int BPF_PROG(trace_implicit_arg_fentry)
43+
{
44+
__u64 ret = 0;
45+
__s64 err;
46+
47+
fentry_result = check_implicit_args(ctx, &fentry_arg_cnt, &fentry_aux_arg);
48+
49+
err = bpf_get_func_ret(ctx, &ret);
50+
fentry_result &= err == -EOPNOTSUPP;
51+
52+
return 0;
53+
}
54+
55+
__u64 fexit_result;
56+
__u64 fexit_arg_cnt;
57+
__u64 fexit_aux_arg;
58+
59+
SEC("fexit/bpf_kfunc_implicit_arg")
60+
int BPF_PROG(trace_implicit_arg_fexit)
61+
{
62+
__u64 ret = 0;
63+
__s64 err;
64+
65+
fexit_result = check_implicit_args(ctx, &fexit_arg_cnt, &fexit_aux_arg);
66+
67+
err = bpf_get_func_ret(ctx, &ret);
68+
fexit_result &= err == 0 && ret == 5;
69+
70+
return 0;
71+
}
72+
73+
SEC("syscall")
74+
int trigger_implicit_arg(void *ctx)
75+
{
76+
return bpf_kfunc_implicit_arg(5);
77+
}

0 commit comments

Comments
 (0)