1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include "test_stacktrace_build_id.skel.h"
4 
5 static __u64 read_perf_max_sample_freq(void)
6 {
7 	__u64 sample_freq = 5000; /* fallback to 5000 on error */
8 	FILE *f;
9 
10 	f = fopen("/proc/sys/kernel/perf_event_max_sample_rate", "r");
11 	if (f == NULL)
12 		return sample_freq;
13 	fscanf(f, "%llu", &sample_freq);
14 	fclose(f);
15 	return sample_freq;
16 }
17 
18 void test_stacktrace_build_id_nmi(void)
19 {
20 	int control_map_fd, stackid_hmap_fd, stackmap_fd;
21 	struct test_stacktrace_build_id *skel;
22 	int err, pmu_fd;
23 	struct perf_event_attr attr = {
24 		.freq = 1,
25 		.type = PERF_TYPE_HARDWARE,
26 		.config = PERF_COUNT_HW_CPU_CYCLES,
27 	};
28 	__u32 key, previous_key, val, duration = 0;
29 	char buf[256];
30 	int i, j;
31 	struct bpf_stack_build_id id_offs[PERF_MAX_STACK_DEPTH];
32 	int build_id_matches = 0;
33 	int retry = 1;
34 
35 	attr.sample_freq = read_perf_max_sample_freq();
36 
37 retry:
38 	skel = test_stacktrace_build_id__open();
39 	if (CHECK(!skel, "skel_open", "skeleton open failed\n"))
40 		return;
41 
42 	/* override program type */
43 	bpf_program__set_perf_event(skel->progs.oncpu);
44 
45 	err = test_stacktrace_build_id__load(skel);
46 	if (CHECK(err, "skel_load", "skeleton load failed: %d\n", err))
47 		goto cleanup;
48 
49 	pmu_fd = syscall(__NR_perf_event_open, &attr, -1 /* pid */,
50 			 0 /* cpu 0 */, -1 /* group id */,
51 			 0 /* flags */);
52 	if (pmu_fd < 0 && errno == ENOENT) {
53 		printf("%s:SKIP:no PERF_COUNT_HW_CPU_CYCLES\n", __func__);
54 		test__skip();
55 		goto cleanup;
56 	}
57 	if (CHECK(pmu_fd < 0, "perf_event_open", "err %d errno %d\n",
58 		  pmu_fd, errno))
59 		goto cleanup;
60 
61 	skel->links.oncpu = bpf_program__attach_perf_event(skel->progs.oncpu,
62 							   pmu_fd);
63 	if (CHECK(IS_ERR(skel->links.oncpu), "attach_perf_event",
64 		  "err %ld\n", PTR_ERR(skel->links.oncpu))) {
65 		close(pmu_fd);
66 		goto cleanup;
67 	}
68 
69 	/* find map fds */
70 	control_map_fd = bpf_map__fd(skel->maps.control_map);
71 	stackid_hmap_fd = bpf_map__fd(skel->maps.stackid_hmap);
72 	stackmap_fd = bpf_map__fd(skel->maps.stackmap);
73 
74 	if (CHECK_FAIL(system("dd if=/dev/urandom of=/dev/zero count=4 2> /dev/null")))
75 		goto cleanup;
76 	if (CHECK_FAIL(system("taskset 0x1 ./urandom_read 100000")))
77 		goto cleanup;
78 	/* disable stack trace collection */
79 	key = 0;
80 	val = 1;
81 	bpf_map_update_elem(control_map_fd, &key, &val, 0);
82 
83 	/* for every element in stackid_hmap, we can find a corresponding one
84 	 * in stackmap, and vise versa.
85 	 */
86 	err = compare_map_keys(stackid_hmap_fd, stackmap_fd);
87 	if (CHECK(err, "compare_map_keys stackid_hmap vs. stackmap",
88 		  "err %d errno %d\n", err, errno))
89 		goto cleanup;
90 
91 	err = compare_map_keys(stackmap_fd, stackid_hmap_fd);
92 	if (CHECK(err, "compare_map_keys stackmap vs. stackid_hmap",
93 		  "err %d errno %d\n", err, errno))
94 		goto cleanup;
95 
96 	err = extract_build_id(buf, 256);
97 
98 	if (CHECK(err, "get build_id with readelf",
99 		  "err %d errno %d\n", err, errno))
100 		goto cleanup;
101 
102 	err = bpf_map_get_next_key(stackmap_fd, NULL, &key);
103 	if (CHECK(err, "get_next_key from stackmap",
104 		  "err %d, errno %d\n", err, errno))
105 		goto cleanup;
106 
107 	do {
108 		char build_id[64];
109 
110 		err = bpf_map_lookup_elem(stackmap_fd, &key, id_offs);
111 		if (CHECK(err, "lookup_elem from stackmap",
112 			  "err %d, errno %d\n", err, errno))
113 			goto cleanup;
114 		for (i = 0; i < PERF_MAX_STACK_DEPTH; ++i)
115 			if (id_offs[i].status == BPF_STACK_BUILD_ID_VALID &&
116 			    id_offs[i].offset != 0) {
117 				for (j = 0; j < 20; ++j)
118 					sprintf(build_id + 2 * j, "%02x",
119 						id_offs[i].build_id[j] & 0xff);
120 				if (strstr(buf, build_id) != NULL)
121 					build_id_matches = 1;
122 			}
123 		previous_key = key;
124 	} while (bpf_map_get_next_key(stackmap_fd, &previous_key, &key) == 0);
125 
126 	/* stack_map_get_build_id_offset() is racy and sometimes can return
127 	 * BPF_STACK_BUILD_ID_IP instead of BPF_STACK_BUILD_ID_VALID;
128 	 * try it one more time.
129 	 */
130 	if (build_id_matches < 1 && retry--) {
131 		test_stacktrace_build_id__destroy(skel);
132 		printf("%s:WARN:Didn't find expected build ID from the map, retrying\n",
133 		       __func__);
134 		goto retry;
135 	}
136 
137 	if (CHECK(build_id_matches < 1, "build id match",
138 		  "Didn't find expected build ID from the map\n"))
139 		goto cleanup;
140 
141 	/*
142 	 * We intentionally skip compare_stack_ips(). This is because we
143 	 * only support one in_nmi() ips-to-build_id translation per cpu
144 	 * at any time, thus stack_amap here will always fallback to
145 	 * BPF_STACK_BUILD_ID_IP;
146 	 */
147 
148 cleanup:
149 	test_stacktrace_build_id__destroy(skel);
150 }
151