blob: 573490530194f8c2dd0c755c3524cba39b05f1f2 [file] [log] [blame]
Arnaldo Carvalho de Melo5875cf42019-06-13 18:29:05 -03001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03002#include <errno.h>
Wang Nanba1fae42015-11-06 13:49:43 +00003#include <stdio.h>
Riccardo Mancini937654c2021-07-15 18:07:24 +02004#include <stdlib.h>
Wang Nanba1fae42015-11-06 13:49:43 +00005#include <sys/epoll.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -03006#include <sys/types.h>
7#include <sys/stat.h>
Arnaldo Carvalho de Melobafae982018-01-22 16:42:16 -03008#include <fcntl.h>
Arnaldo Carvalho de Meloaeb00b12019-08-22 15:40:29 -03009#include <util/record.h>
Wang Nan6a7d5502016-01-25 09:55:53 +000010#include <util/util.h>
Wang Nanba1fae42015-11-06 13:49:43 +000011#include <util/bpf-loader.h>
12#include <util/evlist.h>
Wang Nan6a7d5502016-01-25 09:55:53 +000013#include <linux/filter.h>
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030014#include <linux/kernel.h>
Arnaldo Carvalho de Melo8520a982019-08-29 16:18:59 -030015#include <linux/string.h>
Joe Stringera2630532017-01-26 13:20:01 -080016#include <api/fs/fs.h>
Jiri Olsa7728fa02019-10-07 14:53:17 +020017#include <perf/mmap.h>
Wang Nanba1fae42015-11-06 13:49:43 +000018#include "tests.h"
19#include "llvm.h"
20#include "debug.h"
Arnaldo Carvalho de Melof2a39fe2019-08-30 14:45:20 -030021#include "parse-events.h"
Arnaldo Carvalho de Meloe0fcfb02019-09-23 12:20:38 -030022#include "util/mmap.h"
Wang Nanba1fae42015-11-06 13:49:43 +000023#define NR_ITERS 111
Joe Stringera2630532017-01-26 13:20:01 -080024#define PERF_TEST_BPF_PATH "/sys/fs/bpf/perf_test"
Wang Nanba1fae42015-11-06 13:49:43 +000025
26#ifdef HAVE_LIBBPF_SUPPORT
Arnaldo Carvalho de Melo38219f22020-10-20 15:00:51 -030027#include <linux/bpf.h>
28#include <bpf/bpf.h>
Wang Nanba1fae42015-11-06 13:49:43 +000029
Arnaldo Carvalho de Meloe0337f42018-01-04 12:43:32 -030030static int epoll_pwait_loop(void)
Wang Nanba1fae42015-11-06 13:49:43 +000031{
32 int i;
33
34 /* Should fail NR_ITERS times */
35 for (i = 0; i < NR_ITERS; i++)
Arnaldo Carvalho de Meloe0337f42018-01-04 12:43:32 -030036 epoll_pwait(-(i + 1), NULL, 0, 0, NULL);
Wang Nanba1fae42015-11-06 13:49:43 +000037 return 0;
38}
39
Wang Nanbbb7d492015-11-16 12:10:14 +000040#ifdef HAVE_BPF_PROLOGUE
41
42static int llseek_loop(void)
43{
44 int fds[2], i;
45
46 fds[0] = open("/dev/null", O_RDONLY);
47 fds[1] = open("/dev/null", O_RDWR);
48
49 if (fds[0] < 0 || fds[1] < 0)
50 return -1;
51
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);
55 }
56 close(fds[0]);
57 close(fds[1]);
58 return 0;
59}
60
61#endif
62
Wang Nanba1fae42015-11-06 13:49:43 +000063static struct {
64 enum test_llvm__testcase prog_id;
Wang Nanba1fae42015-11-06 13:49:43 +000065 const char *name;
66 const char *msg_compile_fail;
67 const char *msg_load_fail;
68 int (*target_func)(void);
69 int expect_result;
Joe Stringera2630532017-01-26 13:20:01 -080070 bool pin;
Wang Nanba1fae42015-11-06 13:49:43 +000071} bpf_testcase_table[] = {
72 {
Arnaldo Carvalho de Melo13cb2d02018-01-02 15:18:07 -030073 .prog_id = LLVM_TESTCASE_BASE,
Arnaldo Carvalho de Melo13cb2d02018-01-02 15:18:07 -030074 .name = "[basic_bpf_test]",
75 .msg_compile_fail = "fix 'perf test LLVM' first",
76 .msg_load_fail = "load bpf object failed",
Arnaldo Carvalho de Meloe0337f42018-01-04 12:43:32 -030077 .target_func = &epoll_pwait_loop,
Arnaldo Carvalho de Melo13cb2d02018-01-02 15:18:07 -030078 .expect_result = (NR_ITERS + 1) / 2,
Joe Stringera2630532017-01-26 13:20:01 -080079 },
80 {
Arnaldo Carvalho de Melo13cb2d02018-01-02 15:18:07 -030081 .prog_id = LLVM_TESTCASE_BASE,
Arnaldo Carvalho de Melo13cb2d02018-01-02 15:18:07 -030082 .name = "[bpf_pinning]",
83 .msg_compile_fail = "fix kbuild first",
84 .msg_load_fail = "check your vmlinux setting?",
Arnaldo Carvalho de Meloe0337f42018-01-04 12:43:32 -030085 .target_func = &epoll_pwait_loop,
Arnaldo Carvalho de Melo13cb2d02018-01-02 15:18:07 -030086 .expect_result = (NR_ITERS + 1) / 2,
Thomas Richtereb8f9982021-03-24 09:37:34 +010087 .pin = true,
Wang Nanba1fae42015-11-06 13:49:43 +000088 },
Wang Nanbbb7d492015-11-16 12:10:14 +000089#ifdef HAVE_BPF_PROLOGUE
90 {
Arnaldo Carvalho de Melo13cb2d02018-01-02 15:18:07 -030091 .prog_id = LLVM_TESTCASE_BPF_PROLOGUE,
Arnaldo Carvalho de Melo13cb2d02018-01-02 15:18:07 -030092 .name = "[bpf_prologue_test]",
93 .msg_compile_fail = "fix kbuild first",
94 .msg_load_fail = "check your vmlinux setting?",
95 .target_func = &llseek_loop,
96 .expect_result = (NR_ITERS + 1) / 4,
Wang Nanbbb7d492015-11-16 12:10:14 +000097 },
98#endif
Wang Nanba1fae42015-11-06 13:49:43 +000099};
100
101static int do_test(struct bpf_object *obj, int (*func)(void),
102 int expect)
103{
104 struct record_opts opts = {
105 .target = {
106 .uid = UINT_MAX,
107 .uses_mmap = true,
108 },
109 .freq = 0,
110 .mmap_pages = 256,
111 .default_interval = 1,
112 };
113
114 char pid[16];
115 char sbuf[STRERR_BUFSIZE];
Jiri Olsa63503db2019-07-21 13:23:52 +0200116 struct evlist *evlist;
Wang Nanba1fae42015-11-06 13:49:43 +0000117 int i, ret = TEST_FAIL, err = 0, count = 0;
118
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300119 struct parse_events_state parse_state;
Wang Nanba1fae42015-11-06 13:49:43 +0000120 struct parse_events_error parse_error;
121
Ian Rogers07eafd42021-11-07 01:00:01 -0800122 parse_events_error__init(&parse_error);
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300123 bzero(&parse_state, sizeof(parse_state));
124 parse_state.error = &parse_error;
125 INIT_LIST_HEAD(&parse_state.list);
Wang Nanba1fae42015-11-06 13:49:43 +0000126
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300127 err = parse_events_load_bpf_obj(&parse_state, &parse_state.list, obj, NULL);
Ian Rogers07eafd42021-11-07 01:00:01 -0800128 parse_events_error__exit(&parse_error);
Arnaldo Carvalho de Melo5d9cdc12017-08-17 16:13:34 -0300129 if (err || list_empty(&parse_state.list)) {
Wang Nanba1fae42015-11-06 13:49:43 +0000130 pr_debug("Failed to add events selected by BPF\n");
Wang Nanad0dd7a2015-11-17 08:32:46 +0000131 return TEST_FAIL;
Wang Nanba1fae42015-11-06 13:49:43 +0000132 }
133
134 snprintf(pid, sizeof(pid), "%d", getpid());
135 pid[sizeof(pid) - 1] = '\0';
136 opts.target.tid = opts.target.pid = pid;
137
Arnaldo Carvalho de Melo606e2c22020-11-30 15:04:05 -0300138 /* Instead of evlist__new_default, don't add default events */
Jiri Olsa0f98b112019-07-21 13:23:55 +0200139 evlist = evlist__new();
Wang Nanba1fae42015-11-06 13:49:43 +0000140 if (!evlist) {
Alexander Alemayhu042cfb52016-10-13 18:18:11 +0200141 pr_debug("Not enough memory to create evlist\n");
Wang Nanba1fae42015-11-06 13:49:43 +0000142 return TEST_FAIL;
143 }
144
Arnaldo Carvalho de Melo7748bb72020-11-30 14:56:52 -0300145 err = evlist__create_maps(evlist, &opts.target);
Wang Nanba1fae42015-11-06 13:49:43 +0000146 if (err < 0) {
147 pr_debug("Not enough memory to create thread/cpu maps\n");
148 goto out_delete_evlist;
149 }
150
Arnaldo Carvalho de Meloe414fd12020-11-30 14:52:44 -0300151 evlist__splice_list_tail(evlist, &parse_state.list);
Jiri Olsa3a683122021-07-06 17:17:01 +0200152 evlist->core.nr_groups = parse_state.nr_groups;
Wang Nanba1fae42015-11-06 13:49:43 +0000153
Arnaldo Carvalho de Melo78e1bc22020-11-30 15:07:49 -0300154 evlist__config(evlist, &opts, NULL);
Wang Nanba1fae42015-11-06 13:49:43 +0000155
Jiri Olsa474ddc42019-07-21 13:24:06 +0200156 err = evlist__open(evlist);
Wang Nanba1fae42015-11-06 13:49:43 +0000157 if (err < 0) {
158 pr_debug("perf_evlist__open: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300159 str_error_r(errno, sbuf, sizeof(sbuf)));
Wang Nanba1fae42015-11-06 13:49:43 +0000160 goto out_delete_evlist;
161 }
162
Jiri Olsa9521b5f2019-07-28 12:45:35 +0200163 err = evlist__mmap(evlist, opts.mmap_pages);
Wang Nanba1fae42015-11-06 13:49:43 +0000164 if (err < 0) {
Jiri Olsa9521b5f2019-07-28 12:45:35 +0200165 pr_debug("evlist__mmap: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300166 str_error_r(errno, sbuf, sizeof(sbuf)));
Wang Nanba1fae42015-11-06 13:49:43 +0000167 goto out_delete_evlist;
168 }
169
Jiri Olsa1c87f162019-07-21 13:24:08 +0200170 evlist__enable(evlist);
Wang Nanba1fae42015-11-06 13:49:43 +0000171 (*func)();
Jiri Olsae74676d2019-07-21 13:24:09 +0200172 evlist__disable(evlist);
Wang Nanba1fae42015-11-06 13:49:43 +0000173
Jiri Olsac976ee12019-07-30 13:04:59 +0200174 for (i = 0; i < evlist->core.nr_mmaps; i++) {
Wang Nanba1fae42015-11-06 13:49:43 +0000175 union perf_event *event;
Jiri Olsaa5830532019-07-27 20:30:53 +0200176 struct mmap *md;
Wang Nanba1fae42015-11-06 13:49:43 +0000177
Kan Liang2f54f3a2018-03-01 18:09:01 -0500178 md = &evlist->mmap[i];
Jiri Olsa7c4d4182019-10-07 14:53:18 +0200179 if (perf_mmap__read_init(&md->core) < 0)
Kan Liang2f54f3a2018-03-01 18:09:01 -0500180 continue;
181
Jiri Olsa151ed5d2019-10-07 14:53:20 +0200182 while ((event = perf_mmap__read_event(&md->core)) != NULL) {
Wang Nanba1fae42015-11-06 13:49:43 +0000183 const u32 type = event->header.type;
184
185 if (type == PERF_RECORD_SAMPLE)
186 count ++;
187 }
Jiri Olsa32fdc2c2019-10-07 14:53:19 +0200188 perf_mmap__read_done(&md->core);
Wang Nanba1fae42015-11-06 13:49:43 +0000189 }
190
Sumanth Korikkar4b04e0d2020-08-17 09:27:54 +0200191 if (count != expect * evlist->core.nr_entries) {
Michael Petlan3e113002021-08-05 18:06:11 +0200192 pr_debug("BPF filter result incorrect, expected %d, got %d samples\n", expect * evlist->core.nr_entries, count);
Wang Nanad0dd7a2015-11-17 08:32:46 +0000193 goto out_delete_evlist;
194 }
Wang Nanba1fae42015-11-06 13:49:43 +0000195
196 ret = TEST_OK;
197
198out_delete_evlist:
Jiri Olsac12995a2019-07-21 13:23:56 +0200199 evlist__delete(evlist);
Wang Nanba1fae42015-11-06 13:49:43 +0000200 return ret;
201}
202
203static struct bpf_object *
204prepare_bpf(void *obj_buf, size_t obj_buf_sz, const char *name)
205{
206 struct bpf_object *obj;
207
208 obj = bpf__prepare_load_buffer(obj_buf, obj_buf_sz, name);
209 if (IS_ERR(obj)) {
210 pr_debug("Compile BPF program failed.\n");
211 return NULL;
212 }
213 return obj;
214}
215
Arnaldo Carvalho de Melo916d4092015-11-18 17:38:49 -0300216static int __test__bpf(int idx)
Wang Nanba1fae42015-11-06 13:49:43 +0000217{
218 int ret;
219 void *obj_buf;
220 size_t obj_buf_sz;
221 struct bpf_object *obj;
222
223 ret = test_llvm__fetch_bpf_obj(&obj_buf, &obj_buf_sz,
Arnaldo Carvalho de Melo916d4092015-11-18 17:38:49 -0300224 bpf_testcase_table[idx].prog_id,
Wang Nan7b6982c2016-01-25 09:55:48 +0000225 true, NULL);
Wang Nanba1fae42015-11-06 13:49:43 +0000226 if (ret != TEST_OK || !obj_buf || !obj_buf_sz) {
227 pr_debug("Unable to get BPF object, %s\n",
Arnaldo Carvalho de Melo916d4092015-11-18 17:38:49 -0300228 bpf_testcase_table[idx].msg_compile_fail);
229 if (idx == 0)
Wang Nanba1fae42015-11-06 13:49:43 +0000230 return TEST_SKIP;
231 else
232 return TEST_FAIL;
233 }
234
235 obj = prepare_bpf(obj_buf, obj_buf_sz,
Arnaldo Carvalho de Melo916d4092015-11-18 17:38:49 -0300236 bpf_testcase_table[idx].name);
Wang Nan7b6982c2016-01-25 09:55:48 +0000237 if ((!!bpf_testcase_table[idx].target_func) != (!!obj)) {
238 if (!obj)
239 pr_debug("Fail to load BPF object: %s\n",
240 bpf_testcase_table[idx].msg_load_fail);
241 else
242 pr_debug("Success unexpectedly: %s\n",
243 bpf_testcase_table[idx].msg_load_fail);
Wang Nanba1fae42015-11-06 13:49:43 +0000244 ret = TEST_FAIL;
245 goto out;
246 }
247
Joe Stringera2630532017-01-26 13:20:01 -0800248 if (obj) {
Wang Nan7b6982c2016-01-25 09:55:48 +0000249 ret = do_test(obj,
250 bpf_testcase_table[idx].target_func,
251 bpf_testcase_table[idx].expect_result);
Joe Stringera2630532017-01-26 13:20:01 -0800252 if (ret != TEST_OK)
253 goto out;
254 if (bpf_testcase_table[idx].pin) {
255 int err;
256
257 if (!bpf_fs__mount()) {
258 pr_debug("BPF filesystem not mounted\n");
259 ret = TEST_FAIL;
260 goto out;
261 }
262 err = mkdir(PERF_TEST_BPF_PATH, 0777);
263 if (err && errno != EEXIST) {
264 pr_debug("Failed to make perf_test dir: %s\n",
265 strerror(errno));
266 ret = TEST_FAIL;
267 goto out;
268 }
269 if (bpf_object__pin(obj, PERF_TEST_BPF_PATH))
270 ret = TEST_FAIL;
271 if (rm_rf(PERF_TEST_BPF_PATH))
272 ret = TEST_FAIL;
273 }
274 }
275
Wang Nanba1fae42015-11-06 13:49:43 +0000276out:
Riccardo Mancini937654c2021-07-15 18:07:24 +0200277 free(obj_buf);
Wang Nanba1fae42015-11-06 13:49:43 +0000278 bpf__clear();
279 return ret;
280}
281
Wang Nan6a7d5502016-01-25 09:55:53 +0000282static int check_env(void)
283{
284 int err;
285 unsigned int kver_int;
286 char license[] = "GPL";
287
288 struct bpf_insn insns[] = {
289 BPF_MOV64_IMM(BPF_REG_0, 1),
290 BPF_EXIT_INSN(),
291 };
292
293 err = fetch_kernel_version(&kver_int, NULL, 0);
294 if (err) {
295 pr_debug("Unable to get kernel version\n");
296 return err;
297 }
298
Andrii Nakryiko0bf40542021-12-02 16:46:40 -0800299/* temporarily disable libbpf deprecation warnings */
300#pragma GCC diagnostic push
301#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
Wang Nan6a7d5502016-01-25 09:55:53 +0000302 err = bpf_load_program(BPF_PROG_TYPE_KPROBE, insns,
Guo Zhengkuif08a8fc2021-11-08 15:07:52 +0800303 ARRAY_SIZE(insns),
Wang Nan6a7d5502016-01-25 09:55:53 +0000304 license, kver_int, NULL, 0);
Andrii Nakryiko0bf40542021-12-02 16:46:40 -0800305#pragma GCC diagnostic pop
Wang Nan6a7d5502016-01-25 09:55:53 +0000306 if (err < 0) {
307 pr_err("Missing basic BPF support, skip this test: %s\n",
308 strerror(errno));
309 return err;
310 }
311 close(err);
312
313 return 0;
314}
315
Ian Rogers5801e962021-11-03 23:41:59 -0700316static int test__bpf(int i)
Wang Nan77a0cf62015-11-17 08:32:49 +0000317{
Wang Nanba1fae42015-11-06 13:49:43 +0000318 int err;
319
Wang Nan77a0cf62015-11-17 08:32:49 +0000320 if (i < 0 || i >= (int)ARRAY_SIZE(bpf_testcase_table))
321 return TEST_FAIL;
322
Wang Nanba1fae42015-11-06 13:49:43 +0000323 if (geteuid() != 0) {
324 pr_debug("Only root can run BPF test\n");
325 return TEST_SKIP;
326 }
327
Wang Nan6a7d5502016-01-25 09:55:53 +0000328 if (check_env())
329 return TEST_SKIP;
330
Wang Nan77a0cf62015-11-17 08:32:49 +0000331 err = __test__bpf(i);
332 return err;
Wang Nanba1fae42015-11-06 13:49:43 +0000333}
Ian Rogers5801e962021-11-03 23:41:59 -0700334#endif
Wang Nanba1fae42015-11-06 13:49:43 +0000335
Ian Rogers5801e962021-11-03 23:41:59 -0700336static int test__basic_bpf_test(struct test_suite *test __maybe_unused,
337 int subtest __maybe_unused)
338{
339#ifdef HAVE_LIBBPF_SUPPORT
340 return test__bpf(0);
Wang Nanba1fae42015-11-06 13:49:43 +0000341#else
Wang Nanba1fae42015-11-06 13:49:43 +0000342 pr_debug("Skip BPF test because BPF support is not compiled\n");
343 return TEST_SKIP;
Wang Nanba1fae42015-11-06 13:49:43 +0000344#endif
Ian Rogers5801e962021-11-03 23:41:59 -0700345}
346
347static int test__bpf_pinning(struct test_suite *test __maybe_unused,
348 int subtest __maybe_unused)
349{
350#ifdef HAVE_LIBBPF_SUPPORT
351 return test__bpf(1);
352#else
353 pr_debug("Skip BPF test because BPF support is not compiled\n");
354 return TEST_SKIP;
355#endif
356}
357
358static int test__bpf_prologue_test(struct test_suite *test __maybe_unused,
359 int subtest __maybe_unused)
360{
361#if defined(HAVE_LIBBPF_SUPPORT) && defined(HAVE_BPF_PROLOGUE)
362 return test__bpf(2);
363#else
364 pr_debug("Skip BPF test because BPF support is not compiled\n");
365 return TEST_SKIP;
366#endif
367}
368
369
370static struct test_case bpf_tests[] = {
371#ifdef HAVE_LIBBPF_SUPPORT
372 TEST_CASE("Basic BPF filtering", basic_bpf_test),
373 TEST_CASE("BPF pinning", bpf_pinning),
374#ifdef HAVE_BPF_PROLOGUE
375 TEST_CASE("BPF prologue generation", bpf_prologue_test),
376#else
377 TEST_CASE_REASON("BPF prologue generation", bpf_prologue_test, "not compiled in"),
378#endif
379#else
380 TEST_CASE_REASON("Basic BPF filtering", basic_bpf_test, "not compiled in"),
381 TEST_CASE_REASON("BPF pinning", bpf_pinning, "not compiled in"),
382 TEST_CASE_REASON("BPF prologue generation", bpf_prologue_test, "not compiled in"),
383#endif
384 { .name = NULL, }
385};
Ian Rogersd68f0362021-11-03 23:41:50 -0700386
Ian Rogers33f44bf2021-11-03 23:41:51 -0700387struct test_suite suite__bpf = {
Ian Rogersd68f0362021-11-03 23:41:50 -0700388 .desc = "BPF filter",
Ian Rogers5801e962021-11-03 23:41:59 -0700389 .test_cases = bpf_tests,
Ian Rogersd68f0362021-11-03 23:41:50 -0700390};