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
),
92 ITEM_DATA(vmap_area_list
),
96 static char stub_name
[KSYM_NAME_LEN
];
98 static int stat_symbol_len(void *data
, const char *name
, unsigned long addr
)
100 *(u32
*)data
+= strlen(name
);
105 static void test_kallsyms_compression_ratio(void)
107 u32 pos
, off
, len
, num
;
108 u32 ratio
, total_size
, total_len
= 0;
110 kallsyms_on_each_symbol(stat_symbol_len
, &total_len
);
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.
118 memset(stub_name
, '4', sizeof(stub_name
));
119 pos
= total_len
/ kallsyms_num_syms
;
125 while (pos
< kallsyms_num_syms
) {
126 len
= kallsyms_names
[off
];
130 if ((len
& 0x80) != 0) {
131 len
= (len
& 0x7f) | (kallsyms_names
[off
] << 7);
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.
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
);
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");
157 static int lookup_name(void *data
, const char *name
, unsigned long addr
)
160 struct test_stat
*stat
= (struct test_stat
*)data
;
163 (void)kallsyms_lookup_name(name
);
179 static void test_perf_kallsyms_lookup_name(void)
181 struct test_stat stat
;
183 memset(&stat
, 0, sizeof(stat
));
185 kallsyms_on_each_symbol(lookup_name
, &stat
);
186 pr_info("kallsyms_lookup_name() looked up %d symbols\n", stat
.real_cnt
);
187 pr_info("The time spent on each symbol is (ns): min=%d, max=%d, avg=%lld\n",
188 stat
.min
, stat
.max
, div_u64(stat
.sum
, stat
.real_cnt
));
191 static bool match_cleanup_name(const char *s
, const char *name
)
196 if (!IS_ENABLED(CONFIG_LTO_CLANG
))
207 return !strncmp(s
, name
, len
);
210 static int find_symbol(void *data
, const char *name
, unsigned long addr
)
212 struct test_stat
*stat
= (struct test_stat
*)data
;
214 if (strcmp(name
, stat
->name
) == 0 ||
215 (!stat
->perf
&& match_cleanup_name(name
, stat
->name
))) {
219 if (stat
->save_cnt
< MAX_NUM_OF_RECORDS
) {
220 stat
->addrs
[stat
->save_cnt
] = addr
;
224 if (stat
->real_cnt
== stat
->max
)
231 static void test_perf_kallsyms_on_each_symbol(void)
234 struct test_stat stat
;
236 memset(&stat
, 0, sizeof(stat
));
238 stat
.name
= stub_name
;
241 kallsyms_on_each_symbol(find_symbol
, &stat
);
243 pr_info("kallsyms_on_each_symbol() traverse all: %lld ns\n", t1
- t0
);
246 static int match_symbol(void *data
, unsigned long addr
)
248 struct test_stat
*stat
= (struct test_stat
*)data
;
253 if (stat
->save_cnt
< MAX_NUM_OF_RECORDS
) {
254 stat
->addrs
[stat
->save_cnt
] = addr
;
258 if (stat
->real_cnt
== stat
->max
)
264 static void test_perf_kallsyms_on_each_match_symbol(void)
267 struct test_stat stat
;
269 memset(&stat
, 0, sizeof(stat
));
271 stat
.name
= stub_name
;
273 kallsyms_on_each_match_symbol(match_symbol
, stat
.name
, &stat
);
275 pr_info("kallsyms_on_each_match_symbol() traverse all: %lld ns\n", t1
- t0
);
278 static int test_kallsyms_basic_function(void)
281 int next
= 0, nr_failed
= 0;
284 unsigned long addr
, lookup_addr
;
285 char namebuf
[KSYM_NAME_LEN
];
286 struct test_stat
*stat
, *stat2
;
288 stat
= kmalloc(sizeof(*stat
) * 2, GFP_KERNEL
);
293 prefix
= "kallsyms_lookup_name() for";
294 for (i
= 0; i
< ARRAY_SIZE(test_items
); i
++) {
295 addr
= kallsyms_lookup_name(test_items
[i
].name
);
296 if (addr
!= test_items
[i
].addr
) {
298 pr_info("%s %s failed: addr=%lx, expect %lx\n",
299 prefix
, test_items
[i
].name
, addr
, test_items
[i
].addr
);
303 prefix
= "kallsyms_on_each_symbol() for";
304 for (i
= 0; i
< ARRAY_SIZE(test_items
); i
++) {
305 memset(stat
, 0, sizeof(*stat
));
307 stat
->name
= test_items
[i
].name
;
308 kallsyms_on_each_symbol(find_symbol
, stat
);
309 if (stat
->addr
!= test_items
[i
].addr
|| stat
->real_cnt
!= 1) {
311 pr_info("%s %s failed: count=%d, addr=%lx, expect %lx\n",
312 prefix
, test_items
[i
].name
,
313 stat
->real_cnt
, stat
->addr
, test_items
[i
].addr
);
317 prefix
= "kallsyms_on_each_match_symbol() for";
318 for (i
= 0; i
< ARRAY_SIZE(test_items
); i
++) {
319 memset(stat
, 0, sizeof(*stat
));
321 stat
->name
= test_items
[i
].name
;
322 kallsyms_on_each_match_symbol(match_symbol
, test_items
[i
].name
, stat
);
323 if (stat
->addr
!= test_items
[i
].addr
|| stat
->real_cnt
!= 1) {
325 pr_info("%s %s failed: count=%d, addr=%lx, expect %lx\n",
326 prefix
, test_items
[i
].name
,
327 stat
->real_cnt
, stat
->addr
, test_items
[i
].addr
);
336 for (i
= 0; i
< kallsyms_num_syms
; i
++) {
337 addr
= kallsyms_sym_address(i
);
338 if (!is_ksym_addr(addr
))
341 ret
= lookup_symbol_name(addr
, namebuf
);
348 * The first '.' may be the initial letter, in which case the
349 * entire symbol name will be truncated to an empty string in
350 * cleanup_symbol_name(). Do not test these symbols.
353 * cat /proc/kallsyms | awk '{print $3}' | grep -E "^\." | head
359 * .str.292.llvm.12122243386960820698
360 * .str.24.llvm.12122243386960820698
361 * .str.29.llvm.12122243386960820698
362 * .str.75.llvm.12122243386960820698
363 * .str.99.llvm.12122243386960820698
365 if (IS_ENABLED(CONFIG_LTO_CLANG
) && !namebuf
[0])
368 lookup_addr
= kallsyms_lookup_name(namebuf
);
370 memset(stat
, 0, sizeof(*stat
));
372 kallsyms_on_each_match_symbol(match_symbol
, namebuf
, stat
);
375 * kallsyms_on_each_symbol() is too slow, randomly select some
379 memset(stat2
, 0, sizeof(*stat2
));
380 stat2
->max
= INT_MAX
;
381 stat2
->name
= namebuf
;
382 kallsyms_on_each_symbol(find_symbol
, stat2
);
385 * kallsyms_on_each_symbol() and kallsyms_on_each_match_symbol()
386 * need to get the same traversal result.
388 if (stat
->addr
!= stat2
->addr
||
389 stat
->real_cnt
!= stat2
->real_cnt
||
390 memcmp(stat
->addrs
, stat2
->addrs
,
391 stat
->save_cnt
* sizeof(stat
->addrs
[0])))
395 * The average of random increments is 128, that is, one of
396 * them is tested every 128 symbols.
398 get_random_bytes(&rand
, sizeof(rand
));
399 next
= i
+ (rand
& 0xff) + 1;
402 /* Need to be found at least once */
407 * kallsyms_lookup_name() returns the address of the first
408 * symbol found and cannot be NULL.
410 if (!lookup_addr
|| lookup_addr
!= stat
->addrs
[0])
414 * If the addresses of all matching symbols are recorded, the
415 * target address needs to be exist.
417 if (stat
->real_cnt
<= MAX_NUM_OF_RECORDS
) {
418 for (j
= 0; j
< stat
->save_cnt
; j
++) {
419 if (stat
->addrs
[j
] == addr
)
423 if (j
== stat
->save_cnt
)
433 pr_info("Test for %dth symbol failed: (%s) addr=%lx", i
, namebuf
, addr
);
438 static int test_entry(void *p
)
443 schedule_timeout(5 * HZ
);
444 } while (system_state
!= SYSTEM_RUNNING
);
447 ret
= test_kallsyms_basic_function();
453 test_kallsyms_compression_ratio();
454 test_perf_kallsyms_lookup_name();
455 test_perf_kallsyms_on_each_symbol();
456 test_perf_kallsyms_on_each_match_symbol();
462 static int __init
kallsyms_test_init(void)
464 struct task_struct
*t
;
466 t
= kthread_create(test_entry
, NULL
, "kallsyms_test");
468 pr_info("Create kallsyms selftest task failed\n");
476 late_initcall(kallsyms_test_init
);