Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /* Copyright (c) 2017 Facebook
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/perf_event.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <sys/resource.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <bpf/libbpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <bpf/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) /* This program verifies bpf attachment to tracepoint sys_enter_* and sys_exit_*.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * This requires kernel CONFIG_FTRACE_SYSCALLS to be set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) static void usage(const char *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	printf("USAGE: %s [-i num_progs] [-h]\n", cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	printf("       -i num_progs      # number of progs of the test\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	printf("       -h                # help\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) static void verify_map(int map_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	__u32 key = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	__u32 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	if (bpf_map_lookup_elem(map_id, &key, &val) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		fprintf(stderr, "map_lookup failed: %s\n", strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	if (val == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		fprintf(stderr, "failed: map #%d returns value 0\n", map_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	if (bpf_map_update_elem(map_id, &key, &val, BPF_ANY) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		fprintf(stderr, "map_update failed: %s\n", strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) static int test(char *filename, int num_progs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	int map0_fds[num_progs], map1_fds[num_progs], fd, i, j = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	struct bpf_link *links[num_progs * 4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	struct bpf_object *objs[num_progs];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	struct bpf_program *prog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	for (i = 0; i < num_progs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		objs[i] = bpf_object__open_file(filename, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		if (libbpf_get_error(objs[i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 			fprintf(stderr, "opening BPF object file failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 			objs[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 			goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		/* load BPF program */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		if (bpf_object__load(objs[i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 			fprintf(stderr, "loading BPF object file failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 			goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		map0_fds[i] = bpf_object__find_map_fd_by_name(objs[i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 							      "enter_open_map");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		map1_fds[i] = bpf_object__find_map_fd_by_name(objs[i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 							      "exit_open_map");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		if (map0_fds[i] < 0 || map1_fds[i] < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 			fprintf(stderr, "finding a map in obj file failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 			goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		bpf_object__for_each_program(prog, objs[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 			links[j] = bpf_program__attach(prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 			if (libbpf_get_error(links[j])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 				fprintf(stderr, "bpf_program__attach failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 				links[j] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 				goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 			j++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		printf("prog #%d: map ids %d %d\n", i, map0_fds[i], map1_fds[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	/* current load_bpf_file has perf_event_open default pid = -1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	 * and cpu = 0, which permits attached bpf execution on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	 * all cpus for all pid's. bpf program execution ignores
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	 * cpu affinity.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	/* trigger some "open" operations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	fd = open(filename, O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		fprintf(stderr, "open failed: %s\n", strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	/* verify the map */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	for (i = 0; i < num_progs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		verify_map(map0_fds[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		verify_map(map1_fds[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	for (j--; j >= 0; j--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		bpf_link__destroy(links[j]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	for (i--; i >= 0; i--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		bpf_object__close(objs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) int main(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	int opt, num_progs = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	char filename[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	while ((opt = getopt(argc, argv, "i:h")) != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		switch (opt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		case 'i':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 			num_progs = atoi(optarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		case 'h':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 			usage(argv[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	setrlimit(RLIMIT_MEMLOCK, &r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	return test(filename, num_progs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }