xref: /openbmc/linux/tools/testing/selftests/bpf/testing_helpers.c (revision 34d6f206a88c2651d216bd3487ac956a40b2ba8e)
1cd49291cSAndrii Nakryiko // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2f87c1930SAndrii Nakryiko /* Copyright (C) 2019 Netronome Systems, Inc. */
3cd49291cSAndrii Nakryiko /* Copyright (C) 2020 Facebook, Inc. */
464276f01SStephen Veiss #include <ctype.h>
5cd49291cSAndrii Nakryiko #include <stdlib.h>
6f87c1930SAndrii Nakryiko #include <string.h>
7cd49291cSAndrii Nakryiko #include <errno.h>
8f87c1930SAndrii Nakryiko #include <bpf/bpf.h>
9f87c1930SAndrii Nakryiko #include <bpf/libbpf.h>
1061ddff37SMykola Lysenko #include "test_progs.h"
11cd49291cSAndrii Nakryiko #include "testing_helpers.h"
1245db3109SJiri Olsa #include <linux/membarrier.h>
13cd49291cSAndrii Nakryiko 
parse_num_list(const char * s,bool ** num_set,int * num_set_len)14cd49291cSAndrii Nakryiko int parse_num_list(const char *s, bool **num_set, int *num_set_len)
15cd49291cSAndrii Nakryiko {
16cd49291cSAndrii Nakryiko 	int i, set_len = 0, new_len, num, start = 0, end = -1;
17cd49291cSAndrii Nakryiko 	bool *set = NULL, *tmp, parsing_end = false;
18cd49291cSAndrii Nakryiko 	char *next;
19cd49291cSAndrii Nakryiko 
20cd49291cSAndrii Nakryiko 	while (s[0]) {
21cd49291cSAndrii Nakryiko 		errno = 0;
22cd49291cSAndrii Nakryiko 		num = strtol(s, &next, 10);
23cd49291cSAndrii Nakryiko 		if (errno)
24cd49291cSAndrii Nakryiko 			return -errno;
25cd49291cSAndrii Nakryiko 
26cd49291cSAndrii Nakryiko 		if (parsing_end)
27cd49291cSAndrii Nakryiko 			end = num;
28cd49291cSAndrii Nakryiko 		else
29cd49291cSAndrii Nakryiko 			start = num;
30cd49291cSAndrii Nakryiko 
31cd49291cSAndrii Nakryiko 		if (!parsing_end && *next == '-') {
32cd49291cSAndrii Nakryiko 			s = next + 1;
33cd49291cSAndrii Nakryiko 			parsing_end = true;
34cd49291cSAndrii Nakryiko 			continue;
35cd49291cSAndrii Nakryiko 		} else if (*next == ',') {
36cd49291cSAndrii Nakryiko 			parsing_end = false;
37cd49291cSAndrii Nakryiko 			s = next + 1;
38cd49291cSAndrii Nakryiko 			end = num;
39cd49291cSAndrii Nakryiko 		} else if (*next == '\0') {
40cd49291cSAndrii Nakryiko 			parsing_end = false;
41cd49291cSAndrii Nakryiko 			s = next;
42cd49291cSAndrii Nakryiko 			end = num;
43cd49291cSAndrii Nakryiko 		} else {
44cd49291cSAndrii Nakryiko 			return -EINVAL;
45cd49291cSAndrii Nakryiko 		}
46cd49291cSAndrii Nakryiko 
47cd49291cSAndrii Nakryiko 		if (start > end)
48cd49291cSAndrii Nakryiko 			return -EINVAL;
49cd49291cSAndrii Nakryiko 
50cd49291cSAndrii Nakryiko 		if (end + 1 > set_len) {
51cd49291cSAndrii Nakryiko 			new_len = end + 1;
52cd49291cSAndrii Nakryiko 			tmp = realloc(set, new_len);
53cd49291cSAndrii Nakryiko 			if (!tmp) {
54cd49291cSAndrii Nakryiko 				free(set);
55cd49291cSAndrii Nakryiko 				return -ENOMEM;
56cd49291cSAndrii Nakryiko 			}
57cd49291cSAndrii Nakryiko 			for (i = set_len; i < start; i++)
58cd49291cSAndrii Nakryiko 				tmp[i] = false;
59cd49291cSAndrii Nakryiko 			set = tmp;
60cd49291cSAndrii Nakryiko 			set_len = new_len;
61cd49291cSAndrii Nakryiko 		}
62cd49291cSAndrii Nakryiko 		for (i = start; i <= end; i++)
63cd49291cSAndrii Nakryiko 			set[i] = true;
64cd49291cSAndrii Nakryiko 	}
65cd49291cSAndrii Nakryiko 
66958ddfd7SYuntao Wang 	if (!set || parsing_end)
67cd49291cSAndrii Nakryiko 		return -EINVAL;
68cd49291cSAndrii Nakryiko 
69cd49291cSAndrii Nakryiko 	*num_set = set;
70cd49291cSAndrii Nakryiko 	*num_set_len = set_len;
71cd49291cSAndrii Nakryiko 
72cd49291cSAndrii Nakryiko 	return 0;
73cd49291cSAndrii Nakryiko }
7490806cccSAndrii Nakryiko 
do_insert_test(struct test_filter_set * set,char * test_str,char * subtest_str)750a5c0de8SStephen Veiss static int do_insert_test(struct test_filter_set *set,
760a5c0de8SStephen Veiss 			  char *test_str,
770a5c0de8SStephen Veiss 			  char *subtest_str)
7861ddff37SMykola Lysenko {
790a5c0de8SStephen Veiss 	struct test_filter *tmp, *test;
800a5c0de8SStephen Veiss 	char **ctmp;
810a5c0de8SStephen Veiss 	int i;
8261ddff37SMykola Lysenko 
830a5c0de8SStephen Veiss 	for (i = 0; i < set->cnt; i++) {
840a5c0de8SStephen Veiss 		test = &set->tests[i];
850a5c0de8SStephen Veiss 
860a5c0de8SStephen Veiss 		if (strcmp(test_str, test->name) == 0) {
870a5c0de8SStephen Veiss 			free(test_str);
880a5c0de8SStephen Veiss 			goto subtest;
890a5c0de8SStephen Veiss 		}
900a5c0de8SStephen Veiss 	}
910a5c0de8SStephen Veiss 
920a5c0de8SStephen Veiss 	tmp = realloc(set->tests, sizeof(*test) * (set->cnt + 1));
930a5c0de8SStephen Veiss 	if (!tmp)
9461ddff37SMykola Lysenko 		return -ENOMEM;
9561ddff37SMykola Lysenko 
960a5c0de8SStephen Veiss 	set->tests = tmp;
970a5c0de8SStephen Veiss 	test = &set->tests[set->cnt];
980a5c0de8SStephen Veiss 
990a5c0de8SStephen Veiss 	test->name = test_str;
1000a5c0de8SStephen Veiss 	test->subtests = NULL;
1010a5c0de8SStephen Veiss 	test->subtest_cnt = 0;
1020a5c0de8SStephen Veiss 
1030a5c0de8SStephen Veiss 	set->cnt++;
1040a5c0de8SStephen Veiss 
1050a5c0de8SStephen Veiss subtest:
1060a5c0de8SStephen Veiss 	if (!subtest_str)
1070a5c0de8SStephen Veiss 		return 0;
1080a5c0de8SStephen Veiss 
1090a5c0de8SStephen Veiss 	for (i = 0; i < test->subtest_cnt; i++) {
1100a5c0de8SStephen Veiss 		if (strcmp(subtest_str, test->subtests[i]) == 0) {
1110a5c0de8SStephen Veiss 			free(subtest_str);
1120a5c0de8SStephen Veiss 			return 0;
1130a5c0de8SStephen Veiss 		}
1140a5c0de8SStephen Veiss 	}
1150a5c0de8SStephen Veiss 
1160a5c0de8SStephen Veiss 	ctmp = realloc(test->subtests,
1170a5c0de8SStephen Veiss 		       sizeof(*test->subtests) * (test->subtest_cnt + 1));
1180a5c0de8SStephen Veiss 	if (!ctmp)
1190a5c0de8SStephen Veiss 		return -ENOMEM;
1200a5c0de8SStephen Veiss 
1210a5c0de8SStephen Veiss 	test->subtests = ctmp;
1220a5c0de8SStephen Veiss 	test->subtests[test->subtest_cnt] = subtest_str;
1230a5c0de8SStephen Veiss 
1240a5c0de8SStephen Veiss 	test->subtest_cnt++;
1250a5c0de8SStephen Veiss 
1260a5c0de8SStephen Veiss 	return 0;
1270a5c0de8SStephen Veiss }
1280a5c0de8SStephen Veiss 
insert_test(struct test_filter_set * set,char * test_spec,bool is_glob_pattern)1290a5c0de8SStephen Veiss static int insert_test(struct test_filter_set *set,
1300a5c0de8SStephen Veiss 		       char *test_spec,
1310a5c0de8SStephen Veiss 		       bool is_glob_pattern)
1320a5c0de8SStephen Veiss {
1330a5c0de8SStephen Veiss 	char *pattern, *subtest_str, *ext_test_str, *ext_subtest_str = NULL;
13461ddff37SMykola Lysenko 	int glob_chars = 0;
13561ddff37SMykola Lysenko 
13661ddff37SMykola Lysenko 	if (is_glob_pattern) {
13761ddff37SMykola Lysenko 		pattern = "%s";
13861ddff37SMykola Lysenko 	} else {
13961ddff37SMykola Lysenko 		pattern = "*%s*";
14061ddff37SMykola Lysenko 		glob_chars = 2;
14161ddff37SMykola Lysenko 	}
14261ddff37SMykola Lysenko 
1430a5c0de8SStephen Veiss 	subtest_str = strchr(test_spec, '/');
14461ddff37SMykola Lysenko 	if (subtest_str) {
14561ddff37SMykola Lysenko 		*subtest_str = '\0';
14661ddff37SMykola Lysenko 		subtest_str += 1;
14761ddff37SMykola Lysenko 	}
14861ddff37SMykola Lysenko 
1490a5c0de8SStephen Veiss 	ext_test_str = malloc(strlen(test_spec) + glob_chars + 1);
1500a5c0de8SStephen Veiss 	if (!ext_test_str)
15161ddff37SMykola Lysenko 		goto err;
15261ddff37SMykola Lysenko 
1530a5c0de8SStephen Veiss 	sprintf(ext_test_str, pattern, test_spec);
1540a5c0de8SStephen Veiss 
1550a5c0de8SStephen Veiss 	if (subtest_str) {
1560a5c0de8SStephen Veiss 		ext_subtest_str = malloc(strlen(subtest_str) + glob_chars + 1);
1570a5c0de8SStephen Veiss 		if (!ext_subtest_str)
1580a5c0de8SStephen Veiss 			goto err;
1590a5c0de8SStephen Veiss 
1600a5c0de8SStephen Veiss 		sprintf(ext_subtest_str, pattern, subtest_str);
16161ddff37SMykola Lysenko 	}
16261ddff37SMykola Lysenko 
1630a5c0de8SStephen Veiss 	return do_insert_test(set, ext_test_str, ext_subtest_str);
16461ddff37SMykola Lysenko 
16561ddff37SMykola Lysenko err:
1660a5c0de8SStephen Veiss 	free(ext_test_str);
1670a5c0de8SStephen Veiss 	free(ext_subtest_str);
16861ddff37SMykola Lysenko 
16961ddff37SMykola Lysenko 	return -ENOMEM;
17061ddff37SMykola Lysenko }
17161ddff37SMykola Lysenko 
parse_test_list_file(const char * path,struct test_filter_set * set,bool is_glob_pattern)17264276f01SStephen Veiss int parse_test_list_file(const char *path,
17364276f01SStephen Veiss 			 struct test_filter_set *set,
17464276f01SStephen Veiss 			 bool is_glob_pattern)
17564276f01SStephen Veiss {
17664276f01SStephen Veiss 	char *buf = NULL, *capture_start, *capture_end, *scan_end;
17764276f01SStephen Veiss 	size_t buflen = 0;
17864276f01SStephen Veiss 	int err = 0;
17964276f01SStephen Veiss 	FILE *f;
18064276f01SStephen Veiss 
18164276f01SStephen Veiss 	f = fopen(path, "r");
18264276f01SStephen Veiss 	if (!f) {
18364276f01SStephen Veiss 		err = -errno;
18464276f01SStephen Veiss 		fprintf(stderr, "Failed to open '%s': %d\n", path, err);
18564276f01SStephen Veiss 		return err;
18664276f01SStephen Veiss 	}
18764276f01SStephen Veiss 
18864276f01SStephen Veiss 	while (getline(&buf, &buflen, f) != -1) {
18964276f01SStephen Veiss 		capture_start = buf;
19064276f01SStephen Veiss 
19164276f01SStephen Veiss 		while (isspace(*capture_start))
19264276f01SStephen Veiss 			++capture_start;
19364276f01SStephen Veiss 
19464276f01SStephen Veiss 		capture_end = capture_start;
19564276f01SStephen Veiss 		scan_end = capture_start;
19664276f01SStephen Veiss 
19764276f01SStephen Veiss 		while (*scan_end && *scan_end != '#') {
19864276f01SStephen Veiss 			if (!isspace(*scan_end))
19964276f01SStephen Veiss 				capture_end = scan_end;
20064276f01SStephen Veiss 
20164276f01SStephen Veiss 			++scan_end;
20264276f01SStephen Veiss 		}
20364276f01SStephen Veiss 
20464276f01SStephen Veiss 		if (capture_end == capture_start)
20564276f01SStephen Veiss 			continue;
20664276f01SStephen Veiss 
20764276f01SStephen Veiss 		*(++capture_end) = '\0';
20864276f01SStephen Veiss 
20964276f01SStephen Veiss 		err = insert_test(set, capture_start, is_glob_pattern);
21064276f01SStephen Veiss 		if (err)
21164276f01SStephen Veiss 			break;
21264276f01SStephen Veiss 	}
21364276f01SStephen Veiss 
21464276f01SStephen Veiss 	fclose(f);
21564276f01SStephen Veiss 	return err;
21664276f01SStephen Veiss }
21764276f01SStephen Veiss 
parse_test_list(const char * s,struct test_filter_set * set,bool is_glob_pattern)2180a5c0de8SStephen Veiss int parse_test_list(const char *s,
2190a5c0de8SStephen Veiss 		    struct test_filter_set *set,
2200a5c0de8SStephen Veiss 		    bool is_glob_pattern)
2210a5c0de8SStephen Veiss {
2220a5c0de8SStephen Veiss 	char *input, *state = NULL, *test_spec;
223*564d1abfSTony Ambardar 	int err = 0, cnt = 0;
2240a5c0de8SStephen Veiss 
2250a5c0de8SStephen Veiss 	input = strdup(s);
2260a5c0de8SStephen Veiss 	if (!input)
2270a5c0de8SStephen Veiss 		return -ENOMEM;
2280a5c0de8SStephen Veiss 
229*564d1abfSTony Ambardar 	while ((test_spec = strtok_r(cnt++ ? NULL : input, ",", &state))) {
2300a5c0de8SStephen Veiss 		err = insert_test(set, test_spec, is_glob_pattern);
2310a5c0de8SStephen Veiss 		if (err)
2320a5c0de8SStephen Veiss 			break;
2330a5c0de8SStephen Veiss 	}
2340a5c0de8SStephen Veiss 
2350a5c0de8SStephen Veiss 	free(input);
2360a5c0de8SStephen Veiss 	return err;
2370a5c0de8SStephen Veiss }
2380a5c0de8SStephen Veiss 
link_info_prog_id(const struct bpf_link * link,struct bpf_link_info * info)23990806cccSAndrii Nakryiko __u32 link_info_prog_id(const struct bpf_link *link, struct bpf_link_info *info)
24090806cccSAndrii Nakryiko {
24190806cccSAndrii Nakryiko 	__u32 info_len = sizeof(*info);
24290806cccSAndrii Nakryiko 	int err;
24390806cccSAndrii Nakryiko 
24490806cccSAndrii Nakryiko 	memset(info, 0, sizeof(*info));
245c5a237a4SIlya Leoshkevich 	err = bpf_link_get_info_by_fd(bpf_link__fd(link), info, &info_len);
24690806cccSAndrii Nakryiko 	if (err) {
24790806cccSAndrii Nakryiko 		printf("failed to get link info: %d\n", -errno);
24890806cccSAndrii Nakryiko 		return 0;
24990806cccSAndrii Nakryiko 	}
25090806cccSAndrii Nakryiko 	return info->prog_id;
25190806cccSAndrii Nakryiko }
252f87c1930SAndrii Nakryiko 
253f87c1930SAndrii Nakryiko int extra_prog_load_log_flags = 0;
254f87c1930SAndrii Nakryiko 
bpf_prog_test_load(const char * file,enum bpf_prog_type type,struct bpf_object ** pobj,int * prog_fd)255f87c1930SAndrii Nakryiko int bpf_prog_test_load(const char *file, enum bpf_prog_type type,
256f87c1930SAndrii Nakryiko 		       struct bpf_object **pobj, int *prog_fd)
257f87c1930SAndrii Nakryiko {
2583fc5fdccSAndrii Nakryiko 	LIBBPF_OPTS(bpf_object_open_opts, opts,
2593fc5fdccSAndrii Nakryiko 		.kernel_log_level = extra_prog_load_log_flags,
2603fc5fdccSAndrii Nakryiko 	);
261f87c1930SAndrii Nakryiko 	struct bpf_object *obj;
262f87c1930SAndrii Nakryiko 	struct bpf_program *prog;
2638cccee9eSFlorent Revest 	__u32 flags;
264f87c1930SAndrii Nakryiko 	int err;
265f87c1930SAndrii Nakryiko 
2663fc5fdccSAndrii Nakryiko 	obj = bpf_object__open_file(file, &opts);
267f87c1930SAndrii Nakryiko 	if (!obj)
268f87c1930SAndrii Nakryiko 		return -errno;
269f87c1930SAndrii Nakryiko 
270f87c1930SAndrii Nakryiko 	prog = bpf_object__next_program(obj, NULL);
271f87c1930SAndrii Nakryiko 	if (!prog) {
272f87c1930SAndrii Nakryiko 		err = -ENOENT;
273f87c1930SAndrii Nakryiko 		goto err_out;
274f87c1930SAndrii Nakryiko 	}
275f87c1930SAndrii Nakryiko 
276d6e6286aSAndrii Nakryiko 	if (type != BPF_PROG_TYPE_UNSPEC && bpf_program__type(prog) != type)
277f87c1930SAndrii Nakryiko 		bpf_program__set_type(prog, type);
278f87c1930SAndrii Nakryiko 
2798cccee9eSFlorent Revest 	flags = bpf_program__flags(prog) | BPF_F_TEST_RND_HI32;
2808cccee9eSFlorent Revest 	bpf_program__set_flags(prog, flags);
28150dee707SAndrii Nakryiko 
2823fc5fdccSAndrii Nakryiko 	err = bpf_object__load(obj);
283f87c1930SAndrii Nakryiko 	if (err)
284f87c1930SAndrii Nakryiko 		goto err_out;
285f87c1930SAndrii Nakryiko 
286f87c1930SAndrii Nakryiko 	*pobj = obj;
287f87c1930SAndrii Nakryiko 	*prog_fd = bpf_program__fd(prog);
288f87c1930SAndrii Nakryiko 
289f87c1930SAndrii Nakryiko 	return 0;
290f87c1930SAndrii Nakryiko err_out:
291f87c1930SAndrii Nakryiko 	bpf_object__close(obj);
292f87c1930SAndrii Nakryiko 	return err;
293f87c1930SAndrii Nakryiko }
294f87c1930SAndrii Nakryiko 
bpf_test_load_program(enum bpf_prog_type type,const struct bpf_insn * insns,size_t insns_cnt,const char * license,__u32 kern_version,char * log_buf,size_t log_buf_sz)295f87c1930SAndrii Nakryiko int bpf_test_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
296f87c1930SAndrii Nakryiko 			  size_t insns_cnt, const char *license,
297f87c1930SAndrii Nakryiko 			  __u32 kern_version, char *log_buf,
298f87c1930SAndrii Nakryiko 			  size_t log_buf_sz)
299f87c1930SAndrii Nakryiko {
300f87c1930SAndrii Nakryiko 	LIBBPF_OPTS(bpf_prog_load_opts, opts,
301f87c1930SAndrii Nakryiko 		.kern_version = kern_version,
302f87c1930SAndrii Nakryiko 		.prog_flags = BPF_F_TEST_RND_HI32,
303f87c1930SAndrii Nakryiko 		.log_level = extra_prog_load_log_flags,
304f87c1930SAndrii Nakryiko 		.log_buf = log_buf,
305f87c1930SAndrii Nakryiko 		.log_size = log_buf_sz,
306f87c1930SAndrii Nakryiko 	);
307f87c1930SAndrii Nakryiko 
308f87c1930SAndrii Nakryiko 	return bpf_prog_load(type, NULL, license, insns, insns_cnt, &opts);
309f87c1930SAndrii Nakryiko }
310de6d014aSSong Liu 
read_perf_max_sample_freq(void)311de6d014aSSong Liu __u64 read_perf_max_sample_freq(void)
312de6d014aSSong Liu {
313de6d014aSSong Liu 	__u64 sample_freq = 5000; /* fallback to 5000 on error */
314de6d014aSSong Liu 	FILE *f;
315de6d014aSSong Liu 
316de6d014aSSong Liu 	f = fopen("/proc/sys/kernel/perf_event_max_sample_rate", "r");
317de6d014aSSong Liu 	if (f == NULL) {
318de6d014aSSong Liu 		printf("Failed to open /proc/sys/kernel/perf_event_max_sample_rate: err %d\n"
319de6d014aSSong Liu 		       "return default value: 5000\n", -errno);
320de6d014aSSong Liu 		return sample_freq;
321de6d014aSSong Liu 	}
322de6d014aSSong Liu 	if (fscanf(f, "%llu", &sample_freq) != 1) {
323de6d014aSSong Liu 		printf("Failed to parse /proc/sys/kernel/perf_event_max_sample_rate: err %d\n"
324de6d014aSSong Liu 		       "return default value: 5000\n", -errno);
325de6d014aSSong Liu 	}
326de6d014aSSong Liu 
327de6d014aSSong Liu 	fclose(f);
328de6d014aSSong Liu 	return sample_freq;
329de6d014aSSong Liu }
33045db3109SJiri Olsa 
finit_module(int fd,const char * param_values,int flags)33145db3109SJiri Olsa static int finit_module(int fd, const char *param_values, int flags)
33245db3109SJiri Olsa {
33345db3109SJiri Olsa 	return syscall(__NR_finit_module, fd, param_values, flags);
33445db3109SJiri Olsa }
33545db3109SJiri Olsa 
delete_module(const char * name,int flags)33645db3109SJiri Olsa static int delete_module(const char *name, int flags)
33745db3109SJiri Olsa {
33845db3109SJiri Olsa 	return syscall(__NR_delete_module, name, flags);
33945db3109SJiri Olsa }
34045db3109SJiri Olsa 
unload_bpf_testmod(bool verbose)34111642eb9SJiri Olsa int unload_bpf_testmod(bool verbose)
34245db3109SJiri Olsa {
34345db3109SJiri Olsa 	if (kern_sync_rcu())
344d18deccaSJiri Olsa 		fprintf(stdout, "Failed to trigger kernel-side RCU sync!\n");
34545db3109SJiri Olsa 	if (delete_module("bpf_testmod", 0)) {
34645db3109SJiri Olsa 		if (errno == ENOENT) {
34745db3109SJiri Olsa 			if (verbose)
34845db3109SJiri Olsa 				fprintf(stdout, "bpf_testmod.ko is already unloaded.\n");
34911642eb9SJiri Olsa 			return -1;
35045db3109SJiri Olsa 		}
351d18deccaSJiri Olsa 		fprintf(stdout, "Failed to unload bpf_testmod.ko from kernel: %d\n", -errno);
35211642eb9SJiri Olsa 		return -1;
35345db3109SJiri Olsa 	}
35445db3109SJiri Olsa 	if (verbose)
35545db3109SJiri Olsa 		fprintf(stdout, "Successfully unloaded bpf_testmod.ko.\n");
35611642eb9SJiri Olsa 	return 0;
35745db3109SJiri Olsa }
35845db3109SJiri Olsa 
load_bpf_testmod(bool verbose)35945db3109SJiri Olsa int load_bpf_testmod(bool verbose)
36045db3109SJiri Olsa {
36145db3109SJiri Olsa 	int fd;
36245db3109SJiri Olsa 
36345db3109SJiri Olsa 	if (verbose)
36445db3109SJiri Olsa 		fprintf(stdout, "Loading bpf_testmod.ko...\n");
36545db3109SJiri Olsa 
36645db3109SJiri Olsa 	fd = open("bpf_testmod.ko", O_RDONLY);
36745db3109SJiri Olsa 	if (fd < 0) {
368d18deccaSJiri Olsa 		fprintf(stdout, "Can't find bpf_testmod.ko kernel module: %d\n", -errno);
36945db3109SJiri Olsa 		return -ENOENT;
37045db3109SJiri Olsa 	}
37145db3109SJiri Olsa 	if (finit_module(fd, "", 0)) {
372d18deccaSJiri Olsa 		fprintf(stdout, "Failed to load bpf_testmod.ko into the kernel: %d\n", -errno);
37345db3109SJiri Olsa 		close(fd);
37445db3109SJiri Olsa 		return -EINVAL;
37545db3109SJiri Olsa 	}
37645db3109SJiri Olsa 	close(fd);
37745db3109SJiri Olsa 
37845db3109SJiri Olsa 	if (verbose)
37945db3109SJiri Olsa 		fprintf(stdout, "Successfully loaded bpf_testmod.ko.\n");
38045db3109SJiri Olsa 	return 0;
38145db3109SJiri Olsa }
38245db3109SJiri Olsa 
38345db3109SJiri Olsa /*
38445db3109SJiri Olsa  * Trigger synchronize_rcu() in kernel.
38545db3109SJiri Olsa  */
kern_sync_rcu(void)38645db3109SJiri Olsa int kern_sync_rcu(void)
38745db3109SJiri Olsa {
38845db3109SJiri Olsa 	return syscall(__NR_membarrier, MEMBARRIER_CMD_SHARED, 0, 0);
38945db3109SJiri Olsa }
390