xref: /linux/tools/testing/selftests/bpf/prog_tests/fexit_stress.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 Facebook */
3 #include <test_progs.h>
4 #include "bpf_util.h"
5 
6 void serial_test_fexit_stress(void)
7 {
8 	int bpf_max_tramp_links, err, i;
9 	int *fd, *fexit_fd, *link_fd;
10 
11 	bpf_max_tramp_links = get_bpf_max_tramp_links();
12 	if (!ASSERT_GE(bpf_max_tramp_links, 1, "bpf_max_tramp_links"))
13 		return;
14 	fd = calloc(bpf_max_tramp_links * 2, sizeof(*fd));
15 	if (!ASSERT_OK_PTR(fd, "fd"))
16 		return;
17 	fexit_fd = fd;
18 	link_fd = fd + bpf_max_tramp_links;
19 
20 	const struct bpf_insn trace_program[] = {
21 		BPF_MOV64_IMM(BPF_REG_0, 0),
22 		BPF_EXIT_INSN(),
23 	};
24 
25 	LIBBPF_OPTS(bpf_prog_load_opts, trace_opts,
26 		.expected_attach_type = BPF_TRACE_FEXIT,
27 	);
28 
29 	LIBBPF_OPTS(bpf_test_run_opts, topts);
30 
31 	err = libbpf_find_vmlinux_btf_id("bpf_fentry_test1",
32 					 trace_opts.expected_attach_type);
33 	if (!ASSERT_GT(err, 0, "find_vmlinux_btf_id"))
34 		goto out;
35 	trace_opts.attach_btf_id = err;
36 
37 	for (i = 0; i < bpf_max_tramp_links; i++) {
38 		fexit_fd[i] = bpf_prog_load(BPF_PROG_TYPE_TRACING, NULL, "GPL",
39 					    trace_program,
40 					    ARRAY_SIZE(trace_program),
41 					    &trace_opts);
42 		if (!ASSERT_GE(fexit_fd[i], 0, "fexit load"))
43 			goto out;
44 		link_fd[i] = bpf_link_create(fexit_fd[i], 0, BPF_TRACE_FEXIT, NULL);
45 		if (!ASSERT_GE(link_fd[i], 0, "fexit attach"))
46 			goto out;
47 	}
48 
49 	err = bpf_prog_test_run_opts(fexit_fd[0], &topts);
50 	ASSERT_OK(err, "bpf_prog_test_run_opts");
51 
52 out:
53 	for (i = 0; i < bpf_max_tramp_links; i++) {
54 		if (link_fd[i] > 0)
55 			close(link_fd[i]);
56 		if (fexit_fd[i] > 0)
57 			close(fexit_fd[i]);
58 	}
59 	free(fd);
60 }
61