xref: /openbmc/linux/kernel/kallsyms_selftest.c (revision ecfb9f40)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Test the function and performance of kallsyms
4  *
5  * Copyright (C) Huawei Technologies Co., Ltd., 2022
6  *
7  * Authors: Zhen Lei <thunder.leizhen@huawei.com> Huawei
8  */
9 
10 #define pr_fmt(fmt) "kallsyms_selftest: " fmt
11 
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/kallsyms.h>
15 #include <linux/random.h>
16 #include <linux/sched/clock.h>
17 #include <linux/kthread.h>
18 #include <linux/vmalloc.h>
19 
20 #include "kallsyms_internal.h"
21 #include "kallsyms_selftest.h"
22 
23 
24 #define MAX_NUM_OF_RECORDS		64
25 
26 struct test_stat {
27 	int min;
28 	int max;
29 	int save_cnt;
30 	int real_cnt;
31 	int perf;
32 	u64 sum;
33 	char *name;
34 	unsigned long addr;
35 	unsigned long addrs[MAX_NUM_OF_RECORDS];
36 };
37 
38 struct test_item {
39 	char *name;
40 	unsigned long addr;
41 };
42 
43 #define ITEM_FUNC(s)				\
44 	{					\
45 		.name = #s,			\
46 		.addr = (unsigned long)s,	\
47 	}
48 
49 #define ITEM_DATA(s)				\
50 	{					\
51 		.name = #s,			\
52 		.addr = (unsigned long)&s,	\
53 	}
54 
55 
56 static int kallsyms_test_var_bss_static;
57 static int kallsyms_test_var_data_static = 1;
58 int kallsyms_test_var_bss;
59 int kallsyms_test_var_data = 1;
60 
61 static int kallsyms_test_func_static(void)
62 {
63 	kallsyms_test_var_bss_static++;
64 	kallsyms_test_var_data_static++;
65 
66 	return 0;
67 }
68 
69 int kallsyms_test_func(void)
70 {
71 	return kallsyms_test_func_static();
72 }
73 
74 __weak int kallsyms_test_func_weak(void)
75 {
76 	kallsyms_test_var_bss++;
77 	kallsyms_test_var_data++;
78 	return 0;
79 }
80 
81 static struct test_item test_items[] = {
82 	ITEM_FUNC(kallsyms_test_func_static),
83 	ITEM_FUNC(kallsyms_test_func),
84 	ITEM_FUNC(kallsyms_test_func_weak),
85 	ITEM_FUNC(vmalloc),
86 	ITEM_FUNC(vfree),
87 #ifdef CONFIG_KALLSYMS_ALL
88 	ITEM_DATA(kallsyms_test_var_bss_static),
89 	ITEM_DATA(kallsyms_test_var_data_static),
90 	ITEM_DATA(kallsyms_test_var_bss),
91 	ITEM_DATA(kallsyms_test_var_data),
92 	ITEM_DATA(vmap_area_list),
93 #endif
94 };
95 
96 static char stub_name[KSYM_NAME_LEN];
97 
98 static int stat_symbol_len(void *data, const char *name, struct module *mod, unsigned long addr)
99 {
100 	*(u32 *)data += strlen(name);
101 
102 	return 0;
103 }
104 
105 static void test_kallsyms_compression_ratio(void)
106 {
107 	u32 pos, off, len, num;
108 	u32 ratio, total_size, total_len = 0;
109 
110 	kallsyms_on_each_symbol(stat_symbol_len, &total_len);
111 
112 	/*
113 	 * A symbol name cannot start with a number. This stub name helps us
114 	 * traverse the entire symbol table without finding a match. It's used
115 	 * for subsequent performance tests, and its length is the average
116 	 * length of all symbol names.
117 	 */
118 	memset(stub_name, '4', sizeof(stub_name));
119 	pos = total_len / kallsyms_num_syms;
120 	stub_name[pos] = 0;
121 
122 	pos = 0;
123 	num = 0;
124 	off = 0;
125 	while (pos < kallsyms_num_syms) {
126 		len = kallsyms_names[off];
127 		num++;
128 		off++;
129 		pos++;
130 		if ((len & 0x80) != 0) {
131 			len = (len & 0x7f) | (kallsyms_names[off] << 7);
132 			num++;
133 			off++;
134 		}
135 		off += len;
136 	}
137 
138 	/*
139 	 * 1. The length fields is not counted
140 	 * 2. The memory occupied by array kallsyms_token_table[] and
141 	 *    kallsyms_token_index[] needs to be counted.
142 	 */
143 	total_size = off - num;
144 	pos = kallsyms_token_index[0xff];
145 	total_size += pos + strlen(&kallsyms_token_table[pos]) + 1;
146 	total_size += 0x100 * sizeof(u16);
147 
148 	pr_info(" ---------------------------------------------------------\n");
149 	pr_info("| nr_symbols | compressed size | original size | ratio(%%) |\n");
150 	pr_info("|---------------------------------------------------------|\n");
151 	ratio = (u32)div_u64(10000ULL * total_size, total_len);
152 	pr_info("| %10d |    %10d   |   %10d  |  %2d.%-2d   |\n",
153 		kallsyms_num_syms, total_size, total_len, ratio / 100, ratio % 100);
154 	pr_info(" ---------------------------------------------------------\n");
155 }
156 
157 static int lookup_name(void *data, const char *name, struct module *mod, unsigned long addr)
158 {
159 	u64 t0, t1, t;
160 	unsigned long flags;
161 	struct test_stat *stat = (struct test_stat *)data;
162 
163 	local_irq_save(flags);
164 	t0 = sched_clock();
165 	(void)kallsyms_lookup_name(name);
166 	t1 = sched_clock();
167 	local_irq_restore(flags);
168 
169 	t = t1 - t0;
170 	if (t < stat->min)
171 		stat->min = t;
172 
173 	if (t > stat->max)
174 		stat->max = t;
175 
176 	stat->real_cnt++;
177 	stat->sum += t;
178 
179 	return 0;
180 }
181 
182 static void test_perf_kallsyms_lookup_name(void)
183 {
184 	struct test_stat stat;
185 
186 	memset(&stat, 0, sizeof(stat));
187 	stat.min = INT_MAX;
188 	kallsyms_on_each_symbol(lookup_name, &stat);
189 	pr_info("kallsyms_lookup_name() looked up %d symbols\n", stat.real_cnt);
190 	pr_info("The time spent on each symbol is (ns): min=%d, max=%d, avg=%lld\n",
191 		stat.min, stat.max, div_u64(stat.sum, stat.real_cnt));
192 }
193 
194 static bool match_cleanup_name(const char *s, const char *name)
195 {
196 	char *p;
197 	int len;
198 
199 	if (!IS_ENABLED(CONFIG_LTO_CLANG))
200 		return false;
201 
202 	p = strchr(s, '.');
203 	if (!p)
204 		return false;
205 
206 	len = strlen(name);
207 	if (p - s != len)
208 		return false;
209 
210 	return !strncmp(s, name, len);
211 }
212 
213 static int find_symbol(void *data, const char *name, struct module *mod, unsigned long addr)
214 {
215 	struct test_stat *stat = (struct test_stat *)data;
216 
217 	if (strcmp(name, stat->name) == 0 ||
218 	    (!stat->perf && match_cleanup_name(name, stat->name))) {
219 		stat->real_cnt++;
220 		stat->addr = addr;
221 
222 		if (stat->save_cnt < MAX_NUM_OF_RECORDS) {
223 			stat->addrs[stat->save_cnt] = addr;
224 			stat->save_cnt++;
225 		}
226 
227 		if (stat->real_cnt == stat->max)
228 			return 1;
229 	}
230 
231 	return 0;
232 }
233 
234 static void test_perf_kallsyms_on_each_symbol(void)
235 {
236 	u64 t0, t1;
237 	unsigned long flags;
238 	struct test_stat stat;
239 
240 	memset(&stat, 0, sizeof(stat));
241 	stat.max = INT_MAX;
242 	stat.name = stub_name;
243 	stat.perf = 1;
244 	local_irq_save(flags);
245 	t0 = sched_clock();
246 	kallsyms_on_each_symbol(find_symbol, &stat);
247 	t1 = sched_clock();
248 	local_irq_restore(flags);
249 	pr_info("kallsyms_on_each_symbol() traverse all: %lld ns\n", t1 - t0);
250 }
251 
252 static int match_symbol(void *data, unsigned long addr)
253 {
254 	struct test_stat *stat = (struct test_stat *)data;
255 
256 	stat->real_cnt++;
257 	stat->addr = addr;
258 
259 	if (stat->save_cnt < MAX_NUM_OF_RECORDS) {
260 		stat->addrs[stat->save_cnt] = addr;
261 		stat->save_cnt++;
262 	}
263 
264 	if (stat->real_cnt == stat->max)
265 		return 1;
266 
267 	return 0;
268 }
269 
270 static void test_perf_kallsyms_on_each_match_symbol(void)
271 {
272 	u64 t0, t1;
273 	unsigned long flags;
274 	struct test_stat stat;
275 
276 	memset(&stat, 0, sizeof(stat));
277 	stat.max = INT_MAX;
278 	stat.name = stub_name;
279 	local_irq_save(flags);
280 	t0 = sched_clock();
281 	kallsyms_on_each_match_symbol(match_symbol, stat.name, &stat);
282 	t1 = sched_clock();
283 	local_irq_restore(flags);
284 	pr_info("kallsyms_on_each_match_symbol() traverse all: %lld ns\n", t1 - t0);
285 }
286 
287 static int test_kallsyms_basic_function(void)
288 {
289 	int i, j, ret;
290 	int next = 0, nr_failed = 0;
291 	char *prefix;
292 	unsigned short rand;
293 	unsigned long addr, lookup_addr;
294 	char namebuf[KSYM_NAME_LEN];
295 	struct test_stat *stat, *stat2;
296 
297 	stat = kmalloc(sizeof(*stat) * 2, GFP_KERNEL);
298 	if (!stat)
299 		return -ENOMEM;
300 	stat2 = stat + 1;
301 
302 	prefix = "kallsyms_lookup_name() for";
303 	for (i = 0; i < ARRAY_SIZE(test_items); i++) {
304 		addr = kallsyms_lookup_name(test_items[i].name);
305 		if (addr != test_items[i].addr) {
306 			nr_failed++;
307 			pr_info("%s %s failed: addr=%lx, expect %lx\n",
308 				prefix, test_items[i].name, addr, test_items[i].addr);
309 		}
310 	}
311 
312 	prefix = "kallsyms_on_each_symbol() for";
313 	for (i = 0; i < ARRAY_SIZE(test_items); i++) {
314 		memset(stat, 0, sizeof(*stat));
315 		stat->max = INT_MAX;
316 		stat->name = test_items[i].name;
317 		kallsyms_on_each_symbol(find_symbol, stat);
318 		if (stat->addr != test_items[i].addr || stat->real_cnt != 1) {
319 			nr_failed++;
320 			pr_info("%s %s failed: count=%d, addr=%lx, expect %lx\n",
321 				prefix, test_items[i].name,
322 				stat->real_cnt, stat->addr, test_items[i].addr);
323 		}
324 	}
325 
326 	prefix = "kallsyms_on_each_match_symbol() for";
327 	for (i = 0; i < ARRAY_SIZE(test_items); i++) {
328 		memset(stat, 0, sizeof(*stat));
329 		stat->max = INT_MAX;
330 		stat->name = test_items[i].name;
331 		kallsyms_on_each_match_symbol(match_symbol, test_items[i].name, stat);
332 		if (stat->addr != test_items[i].addr || stat->real_cnt != 1) {
333 			nr_failed++;
334 			pr_info("%s %s failed: count=%d, addr=%lx, expect %lx\n",
335 				prefix, test_items[i].name,
336 				stat->real_cnt, stat->addr, test_items[i].addr);
337 		}
338 	}
339 
340 	if (nr_failed) {
341 		kfree(stat);
342 		return -ESRCH;
343 	}
344 
345 	for (i = 0; i < kallsyms_num_syms; i++) {
346 		addr = kallsyms_sym_address(i);
347 		if (!is_ksym_addr(addr))
348 			continue;
349 
350 		ret = lookup_symbol_name(addr, namebuf);
351 		if (unlikely(ret)) {
352 			namebuf[0] = 0;
353 			goto failed;
354 		}
355 
356 		/*
357 		 * The first '.' may be the initial letter, in which case the
358 		 * entire symbol name will be truncated to an empty string in
359 		 * cleanup_symbol_name(). Do not test these symbols.
360 		 *
361 		 * For example:
362 		 * cat /proc/kallsyms | awk '{print $3}' | grep -E "^\." | head
363 		 * .E_read_words
364 		 * .E_leading_bytes
365 		 * .E_trailing_bytes
366 		 * .E_write_words
367 		 * .E_copy
368 		 * .str.292.llvm.12122243386960820698
369 		 * .str.24.llvm.12122243386960820698
370 		 * .str.29.llvm.12122243386960820698
371 		 * .str.75.llvm.12122243386960820698
372 		 * .str.99.llvm.12122243386960820698
373 		 */
374 		if (IS_ENABLED(CONFIG_LTO_CLANG) && !namebuf[0])
375 			continue;
376 
377 		lookup_addr = kallsyms_lookup_name(namebuf);
378 
379 		memset(stat, 0, sizeof(*stat));
380 		stat->max = INT_MAX;
381 		kallsyms_on_each_match_symbol(match_symbol, namebuf, stat);
382 
383 		/*
384 		 * kallsyms_on_each_symbol() is too slow, randomly select some
385 		 * symbols for test.
386 		 */
387 		if (i >= next) {
388 			memset(stat2, 0, sizeof(*stat2));
389 			stat2->max = INT_MAX;
390 			stat2->name = namebuf;
391 			kallsyms_on_each_symbol(find_symbol, stat2);
392 
393 			/*
394 			 * kallsyms_on_each_symbol() and kallsyms_on_each_match_symbol()
395 			 * need to get the same traversal result.
396 			 */
397 			if (stat->addr != stat2->addr ||
398 			    stat->real_cnt != stat2->real_cnt ||
399 			    memcmp(stat->addrs, stat2->addrs,
400 				   stat->save_cnt * sizeof(stat->addrs[0])))
401 				goto failed;
402 
403 			/*
404 			 * The average of random increments is 128, that is, one of
405 			 * them is tested every 128 symbols.
406 			 */
407 			get_random_bytes(&rand, sizeof(rand));
408 			next = i + (rand & 0xff) + 1;
409 		}
410 
411 		/* Need to be found at least once */
412 		if (!stat->real_cnt)
413 			goto failed;
414 
415 		/*
416 		 * kallsyms_lookup_name() returns the address of the first
417 		 * symbol found and cannot be NULL.
418 		 */
419 		if (!lookup_addr || lookup_addr != stat->addrs[0])
420 			goto failed;
421 
422 		/*
423 		 * If the addresses of all matching symbols are recorded, the
424 		 * target address needs to be exist.
425 		 */
426 		if (stat->real_cnt <= MAX_NUM_OF_RECORDS) {
427 			for (j = 0; j < stat->save_cnt; j++) {
428 				if (stat->addrs[j] == addr)
429 					break;
430 			}
431 
432 			if (j == stat->save_cnt)
433 				goto failed;
434 		}
435 	}
436 
437 	kfree(stat);
438 
439 	return 0;
440 
441 failed:
442 	pr_info("Test for %dth symbol failed: (%s) addr=%lx", i, namebuf, addr);
443 	kfree(stat);
444 	return -ESRCH;
445 }
446 
447 static int test_entry(void *p)
448 {
449 	int ret;
450 
451 	do {
452 		schedule_timeout(5 * HZ);
453 	} while (system_state != SYSTEM_RUNNING);
454 
455 	pr_info("start\n");
456 	ret = test_kallsyms_basic_function();
457 	if (ret) {
458 		pr_info("abort\n");
459 		return 0;
460 	}
461 
462 	test_kallsyms_compression_ratio();
463 	test_perf_kallsyms_lookup_name();
464 	test_perf_kallsyms_on_each_symbol();
465 	test_perf_kallsyms_on_each_match_symbol();
466 	pr_info("finish\n");
467 
468 	return 0;
469 }
470 
471 static int __init kallsyms_test_init(void)
472 {
473 	struct task_struct *t;
474 
475 	t = kthread_create(test_entry, NULL, "kallsyms_test");
476 	if (IS_ERR(t)) {
477 		pr_info("Create kallsyms selftest task failed\n");
478 		return PTR_ERR(t);
479 	}
480 	kthread_bind(t, 0);
481 	wake_up_process(t);
482 
483 	return 0;
484 }
485 late_initcall(kallsyms_test_init);
486