1 // SPDX-License-Identifier: GPL-2.0
9 #include <util/record.h>
10 #include <util/util.h>
11 #include <util/bpf-loader.h>
12 #include <util/evlist.h>
13 #include <linux/bpf.h>
14 #include <linux/filter.h>
15 #include <linux/kernel.h>
16 #include <linux/string.h>
17 #include <api/fs/fs.h>
19 #include <perf/mmap.h>
23 #include "parse-events.h"
24 #include "util/mmap.h"
26 #define PERF_TEST_BPF_PATH "/sys/fs/bpf/perf_test"
28 #ifdef HAVE_LIBBPF_SUPPORT
30 static int epoll_pwait_loop(void)
34 /* Should fail NR_ITERS times */
35 for (i = 0; i < NR_ITERS; i++)
36 epoll_pwait(-(i + 1), NULL, 0, 0, NULL);
40 #ifdef HAVE_BPF_PROLOGUE
42 static int llseek_loop(void)
46 fds[0] = open("/dev/null", O_RDONLY);
47 fds[1] = open("/dev/null", O_RDWR);
49 if (fds[0] < 0 || fds[1] < 0)
52 for (i = 0; i < NR_ITERS; i++) {
53 lseek(fds[i % 2], i, (i / 2) % 2 ? SEEK_CUR : SEEK_SET);
54 lseek(fds[(i + 1) % 2], i, (i / 2) % 2 ? SEEK_CUR : SEEK_SET);
64 enum test_llvm__testcase prog_id;
67 const char *msg_compile_fail;
68 const char *msg_load_fail;
69 int (*target_func)(void);
72 } bpf_testcase_table[] = {
74 .prog_id = LLVM_TESTCASE_BASE,
75 .desc = "Basic BPF filtering",
76 .name = "[basic_bpf_test]",
77 .msg_compile_fail = "fix 'perf test LLVM' first",
78 .msg_load_fail = "load bpf object failed",
79 .target_func = &epoll_pwait_loop,
80 .expect_result = (NR_ITERS + 1) / 2,
83 .prog_id = LLVM_TESTCASE_BASE,
84 .desc = "BPF pinning",
85 .name = "[bpf_pinning]",
86 .msg_compile_fail = "fix kbuild first",
87 .msg_load_fail = "check your vmlinux setting?",
88 .target_func = &epoll_pwait_loop,
89 .expect_result = (NR_ITERS + 1) / 2,
92 #ifdef HAVE_BPF_PROLOGUE
94 .prog_id = LLVM_TESTCASE_BPF_PROLOGUE,
95 .desc = "BPF prologue generation",
96 .name = "[bpf_prologue_test]",
97 .msg_compile_fail = "fix kbuild first",
98 .msg_load_fail = "check your vmlinux setting?",
99 .target_func = &llseek_loop,
100 .expect_result = (NR_ITERS + 1) / 4,
104 .prog_id = LLVM_TESTCASE_BPF_RELOCATION,
105 .desc = "BPF relocation checker",
106 .name = "[bpf_relocation_test]",
107 .msg_compile_fail = "fix 'perf test LLVM' first",
108 .msg_load_fail = "libbpf error when dealing with relocation",
112 static int do_test(struct bpf_object *obj, int (*func)(void),
115 struct record_opts opts = {
122 .default_interval = 1,
126 char sbuf[STRERR_BUFSIZE];
127 struct evlist *evlist;
128 int i, ret = TEST_FAIL, err = 0, count = 0;
130 struct parse_events_state parse_state;
131 struct parse_events_error parse_error;
133 bzero(&parse_error, sizeof(parse_error));
134 bzero(&parse_state, sizeof(parse_state));
135 parse_state.error = &parse_error;
136 INIT_LIST_HEAD(&parse_state.list);
138 err = parse_events_load_bpf_obj(&parse_state, &parse_state.list, obj, NULL);
139 if (err || list_empty(&parse_state.list)) {
140 pr_debug("Failed to add events selected by BPF\n");
144 snprintf(pid, sizeof(pid), "%d", getpid());
145 pid[sizeof(pid) - 1] = '\0';
146 opts.target.tid = opts.target.pid = pid;
148 /* Instead of perf_evlist__new_default, don't add default events */
149 evlist = evlist__new();
151 pr_debug("Not enough memory to create evlist\n");
155 err = perf_evlist__create_maps(evlist, &opts.target);
157 pr_debug("Not enough memory to create thread/cpu maps\n");
158 goto out_delete_evlist;
161 perf_evlist__splice_list_tail(evlist, &parse_state.list);
162 evlist->nr_groups = parse_state.nr_groups;
164 perf_evlist__config(evlist, &opts, NULL);
166 err = evlist__open(evlist);
168 pr_debug("perf_evlist__open: %s\n",
169 str_error_r(errno, sbuf, sizeof(sbuf)));
170 goto out_delete_evlist;
173 err = evlist__mmap(evlist, opts.mmap_pages);
175 pr_debug("evlist__mmap: %s\n",
176 str_error_r(errno, sbuf, sizeof(sbuf)));
177 goto out_delete_evlist;
180 evlist__enable(evlist);
182 evlist__disable(evlist);
184 for (i = 0; i < evlist->core.nr_mmaps; i++) {
185 union perf_event *event;
188 md = &evlist->mmap[i];
189 if (perf_mmap__read_init(&md->core) < 0)
192 while ((event = perf_mmap__read_event(&md->core)) != NULL) {
193 const u32 type = event->header.type;
195 if (type == PERF_RECORD_SAMPLE)
198 perf_mmap__read_done(&md->core);
201 if (count != expect * evlist->core.nr_entries) {
202 pr_debug("BPF filter result incorrect, expected %d, got %d samples\n", expect * evlist->core.nr_entries, count);
203 goto out_delete_evlist;
209 evlist__delete(evlist);
213 static struct bpf_object *
214 prepare_bpf(void *obj_buf, size_t obj_buf_sz, const char *name)
216 struct bpf_object *obj;
218 obj = bpf__prepare_load_buffer(obj_buf, obj_buf_sz, name);
220 pr_debug("Compile BPF program failed.\n");
226 static int __test__bpf(int idx)
231 struct bpf_object *obj;
233 ret = test_llvm__fetch_bpf_obj(&obj_buf, &obj_buf_sz,
234 bpf_testcase_table[idx].prog_id,
236 if (ret != TEST_OK || !obj_buf || !obj_buf_sz) {
237 pr_debug("Unable to get BPF object, %s\n",
238 bpf_testcase_table[idx].msg_compile_fail);
245 obj = prepare_bpf(obj_buf, obj_buf_sz,
246 bpf_testcase_table[idx].name);
247 if ((!!bpf_testcase_table[idx].target_func) != (!!obj)) {
249 pr_debug("Fail to load BPF object: %s\n",
250 bpf_testcase_table[idx].msg_load_fail);
252 pr_debug("Success unexpectedly: %s\n",
253 bpf_testcase_table[idx].msg_load_fail);
260 bpf_testcase_table[idx].target_func,
261 bpf_testcase_table[idx].expect_result);
264 if (bpf_testcase_table[idx].pin) {
267 if (!bpf_fs__mount()) {
268 pr_debug("BPF filesystem not mounted\n");
272 err = mkdir(PERF_TEST_BPF_PATH, 0777);
273 if (err && errno != EEXIST) {
274 pr_debug("Failed to make perf_test dir: %s\n",
279 if (bpf_object__pin(obj, PERF_TEST_BPF_PATH))
281 if (rm_rf(PERF_TEST_BPF_PATH))
292 int test__bpf_subtest_get_nr(void)
294 return (int)ARRAY_SIZE(bpf_testcase_table);
297 const char *test__bpf_subtest_get_desc(int i)
299 if (i < 0 || i >= (int)ARRAY_SIZE(bpf_testcase_table))
301 return bpf_testcase_table[i].desc;
304 static int check_env(void)
307 unsigned int kver_int;
308 char license[] = "GPL";
310 struct bpf_insn insns[] = {
311 BPF_MOV64_IMM(BPF_REG_0, 1),
315 err = fetch_kernel_version(&kver_int, NULL, 0);
317 pr_debug("Unable to get kernel version\n");
321 err = bpf_load_program(BPF_PROG_TYPE_KPROBE, insns,
322 sizeof(insns) / sizeof(insns[0]),
323 license, kver_int, NULL, 0);
325 pr_err("Missing basic BPF support, skip this test: %s\n",
334 int test__bpf(struct test *test __maybe_unused, int i)
338 if (i < 0 || i >= (int)ARRAY_SIZE(bpf_testcase_table))
341 if (geteuid() != 0) {
342 pr_debug("Only root can run BPF test\n");
349 err = __test__bpf(i);
354 int test__bpf_subtest_get_nr(void)
359 const char *test__bpf_subtest_get_desc(int i __maybe_unused)
364 int test__bpf(struct test *test __maybe_unused, int i __maybe_unused)
366 pr_debug("Skip BPF test because BPF support is not compiled\n");