Skip to content

Commit b191b0f

Browse files
Martin KaFai LauAlexei Starovoitov
authored andcommitted
selftests/bpf: Add tailcall epilogue test
This patch adds a gen_epilogue test to test a main prog using a bpf_tail_call. A non test_loader test is used. The tailcall target program, "test_epilogue_subprog", needs to be used in a struct_ops map before it can be loaded. Another struct_ops map is also needed to host the actual "test_epilogue_tailcall" struct_ops program that does the bpf_tail_call. The earlier test_loader patch will attach all struct_ops maps but the bpf_testmod.c does not support >1 attached struct_ops. The earlier patch used the test_loader which has already covered checking for the patched pro/epilogue instructions. This is done by the __xlated tag. This patch goes for the regular skel load and syscall test to do the tailcall test that can also allow to directly pass the the "struct st_ops_args *args" as ctx_in to the SEC("syscall") program. Acked-by: Eduard Zingerman <[email protected]> Signed-off-by: Martin KaFai Lau <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 47e6943 commit b191b0f

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

tools/testing/selftests/bpf/prog_tests/pro_epilogue.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,54 @@
33

44
#include <test_progs.h>
55
#include "pro_epilogue.skel.h"
6+
#include "epilogue_tailcall.skel.h"
7+
8+
struct st_ops_args {
9+
__u64 a;
10+
};
11+
12+
static void test_tailcall(void)
13+
{
14+
LIBBPF_OPTS(bpf_test_run_opts, topts);
15+
struct epilogue_tailcall *skel;
16+
struct st_ops_args args;
17+
int err, prog_fd;
18+
19+
skel = epilogue_tailcall__open_and_load();
20+
if (!ASSERT_OK_PTR(skel, "epilogue_tailcall__open_and_load"))
21+
return;
22+
23+
topts.ctx_in = &args;
24+
topts.ctx_size_in = sizeof(args);
25+
26+
skel->links.epilogue_tailcall =
27+
bpf_map__attach_struct_ops(skel->maps.epilogue_tailcall);
28+
if (!ASSERT_OK_PTR(skel->links.epilogue_tailcall, "attach_struct_ops"))
29+
goto done;
30+
31+
/* Both test_epilogue_tailcall and test_epilogue_subprog are
32+
* patched with epilogue. When syscall_epilogue_tailcall()
33+
* is run, test_epilogue_tailcall() is triggered.
34+
* It executes a tail call and control is transferred to
35+
* test_epilogue_subprog(). Only test_epilogue_subprog()
36+
* does args->a += 1, thus final args.a value of 10001
37+
* guarantees that only the epilogue of the
38+
* test_epilogue_subprog is executed.
39+
*/
40+
memset(&args, 0, sizeof(args));
41+
prog_fd = bpf_program__fd(skel->progs.syscall_epilogue_tailcall);
42+
err = bpf_prog_test_run_opts(prog_fd, &topts);
43+
ASSERT_OK(err, "bpf_prog_test_run_opts");
44+
ASSERT_EQ(args.a, 10001, "args.a");
45+
ASSERT_EQ(topts.retval, 10001 * 2, "topts.retval");
46+
47+
done:
48+
epilogue_tailcall__destroy(skel);
49+
}
650

751
void test_pro_epilogue(void)
852
{
953
RUN_TESTS(pro_epilogue);
54+
if (test__start_subtest("tailcall"))
55+
test_tailcall();
1056
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
3+
4+
#include <vmlinux.h>
5+
#include <bpf/bpf_tracing.h>
6+
#include "bpf_misc.h"
7+
#include "../bpf_testmod/bpf_testmod.h"
8+
#include "../bpf_testmod/bpf_testmod_kfunc.h"
9+
10+
char _license[] SEC("license") = "GPL";
11+
12+
static __noinline __used int subprog(struct st_ops_args *args)
13+
{
14+
args->a += 1;
15+
return args->a;
16+
}
17+
18+
SEC("struct_ops/test_epilogue_subprog")
19+
int BPF_PROG(test_epilogue_subprog, struct st_ops_args *args)
20+
{
21+
subprog(args);
22+
return args->a;
23+
}
24+
25+
struct {
26+
__uint(type, BPF_MAP_TYPE_PROG_ARRAY);
27+
__uint(max_entries, 1);
28+
__uint(key_size, sizeof(__u32));
29+
__uint(value_size, sizeof(__u32));
30+
__array(values, void (void));
31+
} epilogue_map SEC(".maps") = {
32+
.values = {
33+
[0] = (void *)&test_epilogue_subprog,
34+
}
35+
};
36+
37+
SEC("struct_ops/test_epilogue_tailcall")
38+
int test_epilogue_tailcall(unsigned long long *ctx)
39+
{
40+
bpf_tail_call(ctx, &epilogue_map, 0);
41+
return 0;
42+
}
43+
44+
SEC(".struct_ops.link")
45+
struct bpf_testmod_st_ops epilogue_tailcall = {
46+
.test_epilogue = (void *)test_epilogue_tailcall,
47+
};
48+
49+
SEC(".struct_ops.link")
50+
struct bpf_testmod_st_ops epilogue_subprog = {
51+
.test_epilogue = (void *)test_epilogue_subprog,
52+
};
53+
54+
SEC("syscall")
55+
int syscall_epilogue_tailcall(struct st_ops_args *args)
56+
{
57+
return bpf_kfunc_st_ops_test_epilogue(args);
58+
}

0 commit comments

Comments
 (0)