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) 2016 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 <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/perf_event.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <stdbool.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) #include "trace_helpers.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #define PRINT_RAW_ADDR 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) /* counts, stackmap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) static int map_fd[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) static void print_ksym(__u64 addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	struct ksym *sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	if (!addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	sym = ksym_search(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	if (!sym) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 		printf("ksym not found. Is kallsyms loaded?\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	if (PRINT_RAW_ADDR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		printf("%s/%llx;", sym->name, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		printf("%s;", sym->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define TASK_COMM_LEN 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) struct key_t {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	char waker[TASK_COMM_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	char target[TASK_COMM_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	__u32 wret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	__u32 tret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) static void print_stack(struct key_t *key, __u64 count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	__u64 ip[PERF_MAX_STACK_DEPTH] = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	static bool warned;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	printf("%s;", key->target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	if (bpf_map_lookup_elem(map_fd[1], &key->tret, ip) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		printf("---;");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		for (i = PERF_MAX_STACK_DEPTH - 1; i >= 0; i--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 			print_ksym(ip[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	printf("-;");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	if (bpf_map_lookup_elem(map_fd[1], &key->wret, ip) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		printf("---;");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		for (i = 0; i < PERF_MAX_STACK_DEPTH; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 			print_ksym(ip[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	printf(";%s %lld\n", key->waker, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	if ((key->tret == -EEXIST || key->wret == -EEXIST) && !warned) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		printf("stackmap collisions seen. Consider increasing size\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		warned = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	} else if (((int)(key->tret) < 0 || (int)(key->wret) < 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		printf("err stackid %d %d\n", key->tret, key->wret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) static void print_stacks(int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	struct key_t key = {}, next_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	__u64 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		bpf_map_lookup_elem(fd, &next_key, &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		print_stack(&next_key, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		key = next_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) static void int_exit(int sig)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	print_stacks(map_fd[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) int main(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	struct bpf_object *obj = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	struct bpf_link *links[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	struct bpf_program *prog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	int delay = 1, i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	char filename[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (setrlimit(RLIMIT_MEMLOCK, &r)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		perror("setrlimit(RLIMIT_MEMLOCK)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	if (load_kallsyms()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		printf("failed to process /proc/kallsyms\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		return 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	obj = bpf_object__open_file(filename, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	if (libbpf_get_error(obj)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		fprintf(stderr, "ERROR: opening BPF object file failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		obj = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	/* load BPF program */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	if (bpf_object__load(obj)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		fprintf(stderr, "ERROR: loading BPF object file failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	map_fd[0] = bpf_object__find_map_fd_by_name(obj, "counts");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	map_fd[1] = bpf_object__find_map_fd_by_name(obj, "stackmap");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	if (map_fd[0] < 0 || map_fd[1] < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		fprintf(stderr, "ERROR: finding a map in obj file failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	signal(SIGINT, int_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	signal(SIGTERM, int_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	bpf_object__for_each_program(prog, obj) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		links[i] = bpf_program__attach(prog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		if (libbpf_get_error(links[i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 			fprintf(stderr, "ERROR: bpf_program__attach failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 			links[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 			goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	if (argc > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		delay = atoi(argv[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	sleep(delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	print_stacks(map_fd[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	for (i--; i >= 0; i--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		bpf_link__destroy(links[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	bpf_object__close(obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }