1 // SPDX-License-Identifier: GPL-2.0
2 #include <errno.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <sys/epoll.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <util/record.h>
10 #include <util/util.h>
11 #include <util/bpf-loader.h>
12 #include <util/evlist.h>
13 #include <linux/filter.h>
14 #include <linux/kernel.h>
15 #include <linux/string.h>
16 #include <api/fs/fs.h>
17 #include <perf/mmap.h>
18 #include "tests.h"
19 #include "llvm.h"
20 #include "debug.h"
21 #include "parse-events.h"
22 #include "util/mmap.h"
23 #define NR_ITERS       111
24 #define PERF_TEST_BPF_PATH "/sys/fs/bpf/perf_test"
25 
26 #if defined(HAVE_LIBBPF_SUPPORT) && defined(HAVE_LIBTRACEEVENT)
27 #include <linux/bpf.h>
28 #include <bpf/bpf.h>
29 
epoll_pwait_loop(void)30 static int epoll_pwait_loop(void)
31 {
32 	int i;
33 
34 	/* Should fail NR_ITERS times */
35 	for (i = 0; i < NR_ITERS; i++)
36 		epoll_pwait(-(i + 1), NULL, 0, 0, NULL);
37 	return 0;
38 }
39 
40 #ifdef HAVE_BPF_PROLOGUE
41 
llseek_loop(void)42 static 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 
63 static struct {
64 	enum test_llvm__testcase prog_id;
65 	const char *name;
66 	const char *msg_compile_fail;
67 	const char *msg_load_fail;
68 	int (*target_func)(void);
69 	int expect_result;
70 	bool	pin;
71 } bpf_testcase_table[] = {
72 	{
73 		.prog_id	  = LLVM_TESTCASE_BASE,
74 		.name		  = "[basic_bpf_test]",
75 		.msg_compile_fail = "fix 'perf test LLVM' first",
76 		.msg_load_fail	  = "load bpf object failed",
77 		.target_func	  = &epoll_pwait_loop,
78 		.expect_result	  = (NR_ITERS + 1) / 2,
79 	},
80 	{
81 		.prog_id	  = LLVM_TESTCASE_BASE,
82 		.name		  = "[bpf_pinning]",
83 		.msg_compile_fail = "fix kbuild first",
84 		.msg_load_fail	  = "check your vmlinux setting?",
85 		.target_func	  = &epoll_pwait_loop,
86 		.expect_result	  = (NR_ITERS + 1) / 2,
87 		.pin		  = true,
88 	},
89 #ifdef HAVE_BPF_PROLOGUE
90 	{
91 		.prog_id	  = LLVM_TESTCASE_BPF_PROLOGUE,
92 		.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,
97 	},
98 #endif
99 };
100 
do_test(struct bpf_object * obj,int (* func)(void),int expect)101 static 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];
116 	struct evlist *evlist;
117 	int i, ret = TEST_FAIL, err = 0, count = 0;
118 
119 	struct parse_events_state parse_state;
120 	struct parse_events_error parse_error;
121 
122 	parse_events_error__init(&parse_error);
123 	bzero(&parse_state, sizeof(parse_state));
124 	parse_state.error = &parse_error;
125 	INIT_LIST_HEAD(&parse_state.list);
126 
127 	err = parse_events_load_bpf_obj(&parse_state, &parse_state.list, obj, NULL);
128 	parse_events_error__exit(&parse_error);
129 	if (err == -ENODATA) {
130 		pr_debug("Failed to add events selected by BPF, debuginfo package not installed\n");
131 		return TEST_SKIP;
132 	}
133 	if (err || list_empty(&parse_state.list)) {
134 		pr_debug("Failed to add events selected by BPF\n");
135 		return TEST_FAIL;
136 	}
137 
138 	snprintf(pid, sizeof(pid), "%d", getpid());
139 	pid[sizeof(pid) - 1] = '\0';
140 	opts.target.tid = opts.target.pid = pid;
141 
142 	/* Instead of evlist__new_default, don't add default events */
143 	evlist = evlist__new();
144 	if (!evlist) {
145 		pr_debug("Not enough memory to create evlist\n");
146 		return TEST_FAIL;
147 	}
148 
149 	err = evlist__create_maps(evlist, &opts.target);
150 	if (err < 0) {
151 		pr_debug("Not enough memory to create thread/cpu maps\n");
152 		goto out_delete_evlist;
153 	}
154 
155 	evlist__splice_list_tail(evlist, &parse_state.list);
156 	evlist->core.nr_groups = parse_state.nr_groups;
157 
158 	evlist__config(evlist, &opts, NULL);
159 
160 	err = evlist__open(evlist);
161 	if (err < 0) {
162 		pr_debug("perf_evlist__open: %s\n",
163 			 str_error_r(errno, sbuf, sizeof(sbuf)));
164 		goto out_delete_evlist;
165 	}
166 
167 	err = evlist__mmap(evlist, opts.mmap_pages);
168 	if (err < 0) {
169 		pr_debug("evlist__mmap: %s\n",
170 			 str_error_r(errno, sbuf, sizeof(sbuf)));
171 		goto out_delete_evlist;
172 	}
173 
174 	evlist__enable(evlist);
175 	(*func)();
176 	evlist__disable(evlist);
177 
178 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
179 		union perf_event *event;
180 		struct mmap *md;
181 
182 		md = &evlist->mmap[i];
183 		if (perf_mmap__read_init(&md->core) < 0)
184 			continue;
185 
186 		while ((event = perf_mmap__read_event(&md->core)) != NULL) {
187 			const u32 type = event->header.type;
188 
189 			if (type == PERF_RECORD_SAMPLE)
190 				count ++;
191 		}
192 		perf_mmap__read_done(&md->core);
193 	}
194 
195 	if (count != expect * evlist->core.nr_entries) {
196 		pr_debug("BPF filter result incorrect, expected %d, got %d samples\n", expect * evlist->core.nr_entries, count);
197 		goto out_delete_evlist;
198 	}
199 
200 	ret = TEST_OK;
201 
202 out_delete_evlist:
203 	evlist__delete(evlist);
204 	return ret;
205 }
206 
207 static struct bpf_object *
prepare_bpf(void * obj_buf,size_t obj_buf_sz,const char * name)208 prepare_bpf(void *obj_buf, size_t obj_buf_sz, const char *name)
209 {
210 	struct bpf_object *obj;
211 
212 	obj = bpf__prepare_load_buffer(obj_buf, obj_buf_sz, name);
213 	if (IS_ERR(obj)) {
214 		pr_debug("Compile BPF program failed.\n");
215 		return NULL;
216 	}
217 	return obj;
218 }
219 
__test__bpf(int idx)220 static int __test__bpf(int idx)
221 {
222 	int ret;
223 	void *obj_buf;
224 	size_t obj_buf_sz;
225 	struct bpf_object *obj;
226 
227 	ret = test_llvm__fetch_bpf_obj(&obj_buf, &obj_buf_sz,
228 				       bpf_testcase_table[idx].prog_id,
229 				       false, NULL);
230 	if (ret != TEST_OK || !obj_buf || !obj_buf_sz) {
231 		pr_debug("Unable to get BPF object, %s\n",
232 			 bpf_testcase_table[idx].msg_compile_fail);
233 		if ((idx == 0) || (ret == TEST_SKIP))
234 			return TEST_SKIP;
235 		else
236 			return TEST_FAIL;
237 	}
238 
239 	obj = prepare_bpf(obj_buf, obj_buf_sz,
240 			  bpf_testcase_table[idx].name);
241 	if ((!!bpf_testcase_table[idx].target_func) != (!!obj)) {
242 		if (!obj)
243 			pr_debug("Fail to load BPF object: %s\n",
244 				 bpf_testcase_table[idx].msg_load_fail);
245 		else
246 			pr_debug("Success unexpectedly: %s\n",
247 				 bpf_testcase_table[idx].msg_load_fail);
248 		ret = TEST_FAIL;
249 		goto out;
250 	}
251 
252 	if (obj) {
253 		ret = do_test(obj,
254 			      bpf_testcase_table[idx].target_func,
255 			      bpf_testcase_table[idx].expect_result);
256 		if (ret != TEST_OK)
257 			goto out;
258 		if (bpf_testcase_table[idx].pin) {
259 			int err;
260 
261 			if (!bpf_fs__mount()) {
262 				pr_debug("BPF filesystem not mounted\n");
263 				ret = TEST_FAIL;
264 				goto out;
265 			}
266 			err = mkdir(PERF_TEST_BPF_PATH, 0777);
267 			if (err && errno != EEXIST) {
268 				pr_debug("Failed to make perf_test dir: %s\n",
269 					 strerror(errno));
270 				ret = TEST_FAIL;
271 				goto out;
272 			}
273 			if (bpf_object__pin(obj, PERF_TEST_BPF_PATH))
274 				ret = TEST_FAIL;
275 			if (rm_rf(PERF_TEST_BPF_PATH))
276 				ret = TEST_FAIL;
277 		}
278 	}
279 
280 out:
281 	free(obj_buf);
282 	bpf__clear();
283 	return ret;
284 }
285 
check_env(void)286 static int check_env(void)
287 {
288 	LIBBPF_OPTS(bpf_prog_load_opts, opts);
289 	int err;
290 	char license[] = "GPL";
291 
292 	struct bpf_insn insns[] = {
293 		BPF_MOV64_IMM(BPF_REG_0, 1),
294 		BPF_EXIT_INSN(),
295 	};
296 
297 	err = fetch_kernel_version(&opts.kern_version, NULL, 0);
298 	if (err) {
299 		pr_debug("Unable to get kernel version\n");
300 		return err;
301 	}
302 	err = bpf_prog_load(BPF_PROG_TYPE_KPROBE, NULL, license, insns,
303 			    ARRAY_SIZE(insns), &opts);
304 	if (err < 0) {
305 		pr_err("Missing basic BPF support, skip this test: %s\n",
306 		       strerror(errno));
307 		return err;
308 	}
309 	close(err);
310 
311 	return 0;
312 }
313 
test__bpf(int i)314 static int test__bpf(int i)
315 {
316 	int err;
317 
318 	if (i < 0 || i >= (int)ARRAY_SIZE(bpf_testcase_table))
319 		return TEST_FAIL;
320 
321 	if (geteuid() != 0) {
322 		pr_debug("Only root can run BPF test\n");
323 		return TEST_SKIP;
324 	}
325 
326 	if (check_env())
327 		return TEST_SKIP;
328 
329 	err = __test__bpf(i);
330 	return err;
331 }
332 #endif
333 
test__basic_bpf_test(struct test_suite * test __maybe_unused,int subtest __maybe_unused)334 static int test__basic_bpf_test(struct test_suite *test __maybe_unused,
335 				int subtest __maybe_unused)
336 {
337 #if defined(HAVE_LIBBPF_SUPPORT) && defined(HAVE_LIBTRACEEVENT)
338 	return test__bpf(0);
339 #else
340 	pr_debug("Skip BPF test because BPF or libtraceevent support is not compiled\n");
341 	return TEST_SKIP;
342 #endif
343 }
344 
test__bpf_pinning(struct test_suite * test __maybe_unused,int subtest __maybe_unused)345 static int test__bpf_pinning(struct test_suite *test __maybe_unused,
346 			     int subtest __maybe_unused)
347 {
348 #if defined(HAVE_LIBBPF_SUPPORT) && defined(HAVE_LIBTRACEEVENT)
349 	return test__bpf(1);
350 #else
351 	pr_debug("Skip BPF test because BPF or libtraceevent support is not compiled\n");
352 	return TEST_SKIP;
353 #endif
354 }
355 
test__bpf_prologue_test(struct test_suite * test __maybe_unused,int subtest __maybe_unused)356 static int test__bpf_prologue_test(struct test_suite *test __maybe_unused,
357 				   int subtest __maybe_unused)
358 {
359 #if defined(HAVE_LIBBPF_SUPPORT) && defined(HAVE_BPF_PROLOGUE) && defined(HAVE_LIBTRACEEVENT)
360 	return test__bpf(2);
361 #else
362 	pr_debug("Skip BPF test because BPF or libtraceevent support is not compiled\n");
363 	return TEST_SKIP;
364 #endif
365 }
366 
367 
368 static struct test_case bpf_tests[] = {
369 #if defined(HAVE_LIBBPF_SUPPORT) && defined(HAVE_LIBTRACEEVENT)
370 	TEST_CASE("Basic BPF filtering", basic_bpf_test),
371 	TEST_CASE_REASON("BPF pinning", bpf_pinning,
372 			"clang isn't installed or environment missing BPF support"),
373 #ifdef HAVE_BPF_PROLOGUE
374 	TEST_CASE_REASON("BPF prologue generation", bpf_prologue_test,
375 			"clang/debuginfo isn't installed or environment missing BPF support"),
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 or missing libtraceevent support"),
381 	TEST_CASE_REASON("BPF pinning", bpf_pinning, "not compiled in or missing libtraceevent support"),
382 	TEST_CASE_REASON("BPF prologue generation", bpf_prologue_test, "not compiled in or missing libtraceevent support"),
383 #endif
384 	{ .name = NULL, }
385 };
386 
387 struct test_suite suite__bpf = {
388 	.desc = "BPF filter",
389 	.test_cases = bpf_tests,
390 };
391