1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Test the function and performance of kallsyms
5 * Copyright (C) Huawei Technologies Co., Ltd., 2022
7 * Authors: Zhen Lei <thunder.leizhen@huawei.com> Huawei
10 #define pr_fmt(fmt) "kallsyms_selftest: " fmt
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>
20 #include "kallsyms_internal.h"
21 #include "kallsyms_selftest.h"
24 #define MAX_NUM_OF_RECORDS 64
35 unsigned long addrs[MAX_NUM_OF_RECORDS];
43 #define ITEM_FUNC(s) \
46 .addr = (unsigned long)s, \
49 #define ITEM_DATA(s) \
52 .addr = (unsigned long)&s, \
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;
61 static int kallsyms_test_func_static(void)
63 kallsyms_test_var_bss_static++;
64 kallsyms_test_var_data_static++;
69 int kallsyms_test_func(void)
71 return kallsyms_test_func_static();
74 __weak int kallsyms_test_func_weak(void)
76 kallsyms_test_var_bss++;
77 kallsyms_test_var_data++;
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),
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),
95 static char stub_name[KSYM_NAME_LEN];
97 static int stat_symbol_len(void *data, const char *name, unsigned long addr)
99 *(u32 *)data += strlen(name);
104 static void test_kallsyms_compression_ratio(void)
106 u32 pos, off, len, num;
107 u32 ratio, total_size, total_len = 0;
109 kallsyms_on_each_symbol(stat_symbol_len, &total_len);
112 * A symbol name cannot start with a number. This stub name helps us
113 * traverse the entire symbol table without finding a match. It's used
114 * for subsequent performance tests, and its length is the average
115 * length of all symbol names.
117 memset(stub_name, '4', sizeof(stub_name));
118 pos = total_len / kallsyms_num_syms;
124 while (pos < kallsyms_num_syms) {
125 len = kallsyms_names[off];
129 if ((len & 0x80) != 0) {
130 len = (len & 0x7f) | (kallsyms_names[off] << 7);
138 * 1. The length fields is not counted
139 * 2. The memory occupied by array kallsyms_token_table[] and
140 * kallsyms_token_index[] needs to be counted.
142 total_size = off - num;
143 pos = kallsyms_token_index[0xff];
144 total_size += pos + strlen(&kallsyms_token_table[pos]) + 1;
145 total_size += 0x100 * sizeof(u16);
147 pr_info(" ---------------------------------------------------------\n");
148 pr_info("| nr_symbols | compressed size | original size | ratio(%%) |\n");
149 pr_info("|---------------------------------------------------------|\n");
150 ratio = (u32)div_u64(10000ULL * total_size, total_len);
151 pr_info("| %10d | %10d | %10d | %2d.%-2d |\n",
152 kallsyms_num_syms, total_size, total_len, ratio / 100, ratio % 100);
153 pr_info(" ---------------------------------------------------------\n");
156 static int lookup_name(void *data, const char *name, unsigned long addr)
159 struct test_stat *stat = (struct test_stat *)data;
162 (void)kallsyms_lookup_name(name);
178 static void test_perf_kallsyms_lookup_name(void)
180 struct test_stat stat;
182 memset(&stat, 0, sizeof(stat));
184 kallsyms_on_each_symbol(lookup_name, &stat);
185 pr_info("kallsyms_lookup_name() looked up %d symbols\n", stat.real_cnt);
186 pr_info("The time spent on each symbol is (ns): min=%d, max=%d, avg=%lld\n",
187 stat.min, stat.max, div_u64(stat.sum, stat.real_cnt));
190 static bool match_cleanup_name(const char *s, const char *name)
195 if (!IS_ENABLED(CONFIG_LTO_CLANG))
198 p = strstr(s, ".llvm.");
206 return !strncmp(s, name, len);
209 static int find_symbol(void *data, const char *name, unsigned long addr)
211 struct test_stat *stat = (struct test_stat *)data;
213 if (strcmp(name, stat->name) == 0 ||
214 (!stat->perf && match_cleanup_name(name, stat->name))) {
218 if (stat->save_cnt < MAX_NUM_OF_RECORDS) {
219 stat->addrs[stat->save_cnt] = addr;
223 if (stat->real_cnt == stat->max)
230 static void test_perf_kallsyms_on_each_symbol(void)
233 struct test_stat stat;
235 memset(&stat, 0, sizeof(stat));
237 stat.name = stub_name;
240 kallsyms_on_each_symbol(find_symbol, &stat);
242 pr_info("kallsyms_on_each_symbol() traverse all: %lld ns\n", t1 - t0);
245 static int match_symbol(void *data, unsigned long addr)
247 struct test_stat *stat = (struct test_stat *)data;
252 if (stat->save_cnt < MAX_NUM_OF_RECORDS) {
253 stat->addrs[stat->save_cnt] = addr;
257 if (stat->real_cnt == stat->max)
263 static void test_perf_kallsyms_on_each_match_symbol(void)
266 struct test_stat stat;
268 memset(&stat, 0, sizeof(stat));
270 stat.name = stub_name;
272 kallsyms_on_each_match_symbol(match_symbol, stat.name, &stat);
274 pr_info("kallsyms_on_each_match_symbol() traverse all: %lld ns\n", t1 - t0);
277 static int test_kallsyms_basic_function(void)
280 int next = 0, nr_failed = 0;
283 unsigned long addr, lookup_addr;
284 char namebuf[KSYM_NAME_LEN];
285 struct test_stat *stat, *stat2;
287 stat = kmalloc(sizeof(*stat) * 2, GFP_KERNEL);
292 prefix = "kallsyms_lookup_name() for";
293 for (i = 0; i < ARRAY_SIZE(test_items); i++) {
294 addr = kallsyms_lookup_name(test_items[i].name);
295 if (addr != test_items[i].addr) {
297 pr_info("%s %s failed: addr=%lx, expect %lx\n",
298 prefix, test_items[i].name, addr, test_items[i].addr);
302 prefix = "kallsyms_on_each_symbol() for";
303 for (i = 0; i < ARRAY_SIZE(test_items); i++) {
304 memset(stat, 0, sizeof(*stat));
306 stat->name = test_items[i].name;
307 kallsyms_on_each_symbol(find_symbol, stat);
308 if (stat->addr != test_items[i].addr || stat->real_cnt != 1) {
310 pr_info("%s %s failed: count=%d, addr=%lx, expect %lx\n",
311 prefix, test_items[i].name,
312 stat->real_cnt, stat->addr, test_items[i].addr);
316 prefix = "kallsyms_on_each_match_symbol() for";
317 for (i = 0; i < ARRAY_SIZE(test_items); i++) {
318 memset(stat, 0, sizeof(*stat));
320 stat->name = test_items[i].name;
321 kallsyms_on_each_match_symbol(match_symbol, test_items[i].name, stat);
322 if (stat->addr != test_items[i].addr || stat->real_cnt != 1) {
324 pr_info("%s %s failed: count=%d, addr=%lx, expect %lx\n",
325 prefix, test_items[i].name,
326 stat->real_cnt, stat->addr, test_items[i].addr);
335 for (i = 0; i < kallsyms_num_syms; i++) {
336 addr = kallsyms_sym_address(i);
337 if (!is_ksym_addr(addr))
340 ret = lookup_symbol_name(addr, namebuf);
343 pr_info("%d: lookup_symbol_name(%lx) failed\n", i, addr);
347 lookup_addr = kallsyms_lookup_name(namebuf);
349 memset(stat, 0, sizeof(*stat));
351 kallsyms_on_each_match_symbol(match_symbol, namebuf, stat);
354 * kallsyms_on_each_symbol() is too slow, randomly select some
358 memset(stat2, 0, sizeof(*stat2));
359 stat2->max = INT_MAX;
360 stat2->name = namebuf;
361 kallsyms_on_each_symbol(find_symbol, stat2);
364 * kallsyms_on_each_symbol() and kallsyms_on_each_match_symbol()
365 * need to get the same traversal result.
367 if (stat->addr != stat2->addr ||
368 stat->real_cnt != stat2->real_cnt ||
369 memcmp(stat->addrs, stat2->addrs,
370 stat->save_cnt * sizeof(stat->addrs[0]))) {
371 pr_info("%s: mismatch between kallsyms_on_each_symbol() and kallsyms_on_each_match_symbol()\n",
377 * The average of random increments is 128, that is, one of
378 * them is tested every 128 symbols.
380 get_random_bytes(&rand, sizeof(rand));
381 next = i + (rand & 0xff) + 1;
384 /* Need to be found at least once */
385 if (!stat->real_cnt) {
386 pr_info("%s: Never found\n", namebuf);
391 * kallsyms_lookup_name() returns the address of the first
392 * symbol found and cannot be NULL.
395 pr_info("%s: NULL lookup_addr?!\n", namebuf);
398 if (lookup_addr != stat->addrs[0]) {
399 pr_info("%s: lookup_addr != stat->addrs[0]\n", namebuf);
404 * If the addresses of all matching symbols are recorded, the
405 * target address needs to be exist.
407 if (stat->real_cnt <= MAX_NUM_OF_RECORDS) {
408 for (j = 0; j < stat->save_cnt; j++) {
409 if (stat->addrs[j] == addr)
413 if (j == stat->save_cnt) {
414 pr_info("%s: j == save_cnt?!\n", namebuf);
425 pr_info("Test for %dth symbol failed: (%s) addr=%lx", i, namebuf, addr);
430 static int test_entry(void *p)
435 schedule_timeout(5 * HZ);
436 } while (system_state != SYSTEM_RUNNING);
439 ret = test_kallsyms_basic_function();
445 test_kallsyms_compression_ratio();
446 test_perf_kallsyms_lookup_name();
447 test_perf_kallsyms_on_each_symbol();
448 test_perf_kallsyms_on_each_match_symbol();
454 static int __init kallsyms_test_init(void)
456 struct task_struct *t;
458 t = kthread_create(test_entry, NULL, "kallsyms_test");
460 pr_info("Create kallsyms selftest task failed\n");
468 late_initcall(kallsyms_test_init);