130f3bb09SZhen Lei // SPDX-License-Identifier: GPL-2.0-or-later
230f3bb09SZhen Lei /*
330f3bb09SZhen Lei * Test the function and performance of kallsyms
430f3bb09SZhen Lei *
530f3bb09SZhen Lei * Copyright (C) Huawei Technologies Co., Ltd., 2022
630f3bb09SZhen Lei *
730f3bb09SZhen Lei * Authors: Zhen Lei <thunder.leizhen@huawei.com> Huawei
830f3bb09SZhen Lei */
930f3bb09SZhen Lei
1030f3bb09SZhen Lei #define pr_fmt(fmt) "kallsyms_selftest: " fmt
1130f3bb09SZhen Lei
1230f3bb09SZhen Lei #include <linux/init.h>
1330f3bb09SZhen Lei #include <linux/module.h>
1430f3bb09SZhen Lei #include <linux/kallsyms.h>
1530f3bb09SZhen Lei #include <linux/random.h>
1630f3bb09SZhen Lei #include <linux/sched/clock.h>
1730f3bb09SZhen Lei #include <linux/kthread.h>
1830f3bb09SZhen Lei #include <linux/vmalloc.h>
1930f3bb09SZhen Lei
2030f3bb09SZhen Lei #include "kallsyms_internal.h"
2130f3bb09SZhen Lei #include "kallsyms_selftest.h"
2230f3bb09SZhen Lei
2330f3bb09SZhen Lei
2430f3bb09SZhen Lei #define MAX_NUM_OF_RECORDS 64
2530f3bb09SZhen Lei
2630f3bb09SZhen Lei struct test_stat {
2730f3bb09SZhen Lei int min;
2830f3bb09SZhen Lei int max;
2930f3bb09SZhen Lei int save_cnt;
3030f3bb09SZhen Lei int real_cnt;
3130f3bb09SZhen Lei int perf;
3230f3bb09SZhen Lei u64 sum;
3330f3bb09SZhen Lei char *name;
3430f3bb09SZhen Lei unsigned long addr;
3530f3bb09SZhen Lei unsigned long addrs[MAX_NUM_OF_RECORDS];
3630f3bb09SZhen Lei };
3730f3bb09SZhen Lei
3830f3bb09SZhen Lei struct test_item {
3930f3bb09SZhen Lei char *name;
4030f3bb09SZhen Lei unsigned long addr;
4130f3bb09SZhen Lei };
4230f3bb09SZhen Lei
4330f3bb09SZhen Lei #define ITEM_FUNC(s) \
4430f3bb09SZhen Lei { \
4530f3bb09SZhen Lei .name = #s, \
4630f3bb09SZhen Lei .addr = (unsigned long)s, \
4730f3bb09SZhen Lei }
4830f3bb09SZhen Lei
4930f3bb09SZhen Lei #define ITEM_DATA(s) \
5030f3bb09SZhen Lei { \
5130f3bb09SZhen Lei .name = #s, \
5230f3bb09SZhen Lei .addr = (unsigned long)&s, \
5330f3bb09SZhen Lei }
5430f3bb09SZhen Lei
5530f3bb09SZhen Lei
5630f3bb09SZhen Lei static int kallsyms_test_var_bss_static;
5730f3bb09SZhen Lei static int kallsyms_test_var_data_static = 1;
5830f3bb09SZhen Lei int kallsyms_test_var_bss;
5930f3bb09SZhen Lei int kallsyms_test_var_data = 1;
6030f3bb09SZhen Lei
kallsyms_test_func_static(void)6130f3bb09SZhen Lei static int kallsyms_test_func_static(void)
6230f3bb09SZhen Lei {
6330f3bb09SZhen Lei kallsyms_test_var_bss_static++;
6430f3bb09SZhen Lei kallsyms_test_var_data_static++;
6530f3bb09SZhen Lei
6630f3bb09SZhen Lei return 0;
6730f3bb09SZhen Lei }
6830f3bb09SZhen Lei
kallsyms_test_func(void)6930f3bb09SZhen Lei int kallsyms_test_func(void)
7030f3bb09SZhen Lei {
7130f3bb09SZhen Lei return kallsyms_test_func_static();
7230f3bb09SZhen Lei }
7330f3bb09SZhen Lei
kallsyms_test_func_weak(void)7430f3bb09SZhen Lei __weak int kallsyms_test_func_weak(void)
7530f3bb09SZhen Lei {
7630f3bb09SZhen Lei kallsyms_test_var_bss++;
7730f3bb09SZhen Lei kallsyms_test_var_data++;
7830f3bb09SZhen Lei return 0;
7930f3bb09SZhen Lei }
8030f3bb09SZhen Lei
8130f3bb09SZhen Lei static struct test_item test_items[] = {
8230f3bb09SZhen Lei ITEM_FUNC(kallsyms_test_func_static),
8330f3bb09SZhen Lei ITEM_FUNC(kallsyms_test_func),
8430f3bb09SZhen Lei ITEM_FUNC(kallsyms_test_func_weak),
8530f3bb09SZhen Lei ITEM_FUNC(vmalloc),
8630f3bb09SZhen Lei ITEM_FUNC(vfree),
8730f3bb09SZhen Lei #ifdef CONFIG_KALLSYMS_ALL
8830f3bb09SZhen Lei ITEM_DATA(kallsyms_test_var_bss_static),
8930f3bb09SZhen Lei ITEM_DATA(kallsyms_test_var_data_static),
9030f3bb09SZhen Lei ITEM_DATA(kallsyms_test_var_bss),
9130f3bb09SZhen Lei ITEM_DATA(kallsyms_test_var_data),
9230f3bb09SZhen Lei ITEM_DATA(vmap_area_list),
9330f3bb09SZhen Lei #endif
9430f3bb09SZhen Lei };
9530f3bb09SZhen Lei
9630f3bb09SZhen Lei static char stub_name[KSYM_NAME_LEN];
9730f3bb09SZhen Lei
stat_symbol_len(void * data,const char * name,unsigned long addr)983703bd54SZhen Lei static int stat_symbol_len(void *data, const char *name, unsigned long addr)
9930f3bb09SZhen Lei {
10030f3bb09SZhen Lei *(u32 *)data += strlen(name);
10130f3bb09SZhen Lei
10230f3bb09SZhen Lei return 0;
10330f3bb09SZhen Lei }
10430f3bb09SZhen Lei
test_kallsyms_compression_ratio(void)10530f3bb09SZhen Lei static void test_kallsyms_compression_ratio(void)
10630f3bb09SZhen Lei {
10730f3bb09SZhen Lei u32 pos, off, len, num;
10830f3bb09SZhen Lei u32 ratio, total_size, total_len = 0;
10930f3bb09SZhen Lei
11030f3bb09SZhen Lei kallsyms_on_each_symbol(stat_symbol_len, &total_len);
11130f3bb09SZhen Lei
11230f3bb09SZhen Lei /*
11330f3bb09SZhen Lei * A symbol name cannot start with a number. This stub name helps us
11430f3bb09SZhen Lei * traverse the entire symbol table without finding a match. It's used
11530f3bb09SZhen Lei * for subsequent performance tests, and its length is the average
11630f3bb09SZhen Lei * length of all symbol names.
11730f3bb09SZhen Lei */
11830f3bb09SZhen Lei memset(stub_name, '4', sizeof(stub_name));
11930f3bb09SZhen Lei pos = total_len / kallsyms_num_syms;
12030f3bb09SZhen Lei stub_name[pos] = 0;
12130f3bb09SZhen Lei
12230f3bb09SZhen Lei pos = 0;
12330f3bb09SZhen Lei num = 0;
12430f3bb09SZhen Lei off = 0;
12530f3bb09SZhen Lei while (pos < kallsyms_num_syms) {
12630f3bb09SZhen Lei len = kallsyms_names[off];
12730f3bb09SZhen Lei num++;
12830f3bb09SZhen Lei off++;
12930f3bb09SZhen Lei pos++;
13030f3bb09SZhen Lei if ((len & 0x80) != 0) {
13130f3bb09SZhen Lei len = (len & 0x7f) | (kallsyms_names[off] << 7);
13230f3bb09SZhen Lei num++;
13330f3bb09SZhen Lei off++;
13430f3bb09SZhen Lei }
13530f3bb09SZhen Lei off += len;
136c86a514fSYang Li }
13730f3bb09SZhen Lei
13830f3bb09SZhen Lei /*
13930f3bb09SZhen Lei * 1. The length fields is not counted
14030f3bb09SZhen Lei * 2. The memory occupied by array kallsyms_token_table[] and
14130f3bb09SZhen Lei * kallsyms_token_index[] needs to be counted.
14230f3bb09SZhen Lei */
14330f3bb09SZhen Lei total_size = off - num;
14430f3bb09SZhen Lei pos = kallsyms_token_index[0xff];
14530f3bb09SZhen Lei total_size += pos + strlen(&kallsyms_token_table[pos]) + 1;
14630f3bb09SZhen Lei total_size += 0x100 * sizeof(u16);
14730f3bb09SZhen Lei
14830f3bb09SZhen Lei pr_info(" ---------------------------------------------------------\n");
14930f3bb09SZhen Lei pr_info("| nr_symbols | compressed size | original size | ratio(%%) |\n");
15030f3bb09SZhen Lei pr_info("|---------------------------------------------------------|\n");
15130f3bb09SZhen Lei ratio = (u32)div_u64(10000ULL * total_size, total_len);
15230f3bb09SZhen Lei pr_info("| %10d | %10d | %10d | %2d.%-2d |\n",
15330f3bb09SZhen Lei kallsyms_num_syms, total_size, total_len, ratio / 100, ratio % 100);
15430f3bb09SZhen Lei pr_info(" ---------------------------------------------------------\n");
15530f3bb09SZhen Lei }
15630f3bb09SZhen Lei
lookup_name(void * data,const char * name,unsigned long addr)1573703bd54SZhen Lei static int lookup_name(void *data, const char *name, unsigned long addr)
15830f3bb09SZhen Lei {
15930f3bb09SZhen Lei u64 t0, t1, t;
16030f3bb09SZhen Lei struct test_stat *stat = (struct test_stat *)data;
16130f3bb09SZhen Lei
162da35048fSNicholas Piggin t0 = ktime_get_ns();
16330f3bb09SZhen Lei (void)kallsyms_lookup_name(name);
164da35048fSNicholas Piggin t1 = ktime_get_ns();
16530f3bb09SZhen Lei
16630f3bb09SZhen Lei t = t1 - t0;
16730f3bb09SZhen Lei if (t < stat->min)
16830f3bb09SZhen Lei stat->min = t;
16930f3bb09SZhen Lei
17030f3bb09SZhen Lei if (t > stat->max)
17130f3bb09SZhen Lei stat->max = t;
17230f3bb09SZhen Lei
17330f3bb09SZhen Lei stat->real_cnt++;
17430f3bb09SZhen Lei stat->sum += t;
17530f3bb09SZhen Lei
17630f3bb09SZhen Lei return 0;
17730f3bb09SZhen Lei }
17830f3bb09SZhen Lei
test_perf_kallsyms_lookup_name(void)17930f3bb09SZhen Lei static void test_perf_kallsyms_lookup_name(void)
18030f3bb09SZhen Lei {
18130f3bb09SZhen Lei struct test_stat stat;
18230f3bb09SZhen Lei
18330f3bb09SZhen Lei memset(&stat, 0, sizeof(stat));
18430f3bb09SZhen Lei stat.min = INT_MAX;
18530f3bb09SZhen Lei kallsyms_on_each_symbol(lookup_name, &stat);
18630f3bb09SZhen Lei pr_info("kallsyms_lookup_name() looked up %d symbols\n", stat.real_cnt);
18730f3bb09SZhen Lei pr_info("The time spent on each symbol is (ns): min=%d, max=%d, avg=%lld\n",
18830f3bb09SZhen Lei stat.min, stat.max, div_u64(stat.sum, stat.real_cnt));
18930f3bb09SZhen Lei }
19030f3bb09SZhen Lei
match_cleanup_name(const char * s,const char * name)19130f3bb09SZhen Lei static bool match_cleanup_name(const char *s, const char *name)
19230f3bb09SZhen Lei {
19330f3bb09SZhen Lei char *p;
19430f3bb09SZhen Lei int len;
19530f3bb09SZhen Lei
19630f3bb09SZhen Lei if (!IS_ENABLED(CONFIG_LTO_CLANG))
19730f3bb09SZhen Lei return false;
19830f3bb09SZhen Lei
199*33f0467fSYonghong Song p = strstr(s, ".llvm.");
20030f3bb09SZhen Lei if (!p)
20130f3bb09SZhen Lei return false;
20230f3bb09SZhen Lei
20330f3bb09SZhen Lei len = strlen(name);
20430f3bb09SZhen Lei if (p - s != len)
20530f3bb09SZhen Lei return false;
20630f3bb09SZhen Lei
20730f3bb09SZhen Lei return !strncmp(s, name, len);
20830f3bb09SZhen Lei }
20930f3bb09SZhen Lei
find_symbol(void * data,const char * name,unsigned long addr)2103703bd54SZhen Lei static int find_symbol(void *data, const char *name, unsigned long addr)
21130f3bb09SZhen Lei {
21230f3bb09SZhen Lei struct test_stat *stat = (struct test_stat *)data;
21330f3bb09SZhen Lei
21430f3bb09SZhen Lei if (strcmp(name, stat->name) == 0 ||
21530f3bb09SZhen Lei (!stat->perf && match_cleanup_name(name, stat->name))) {
21630f3bb09SZhen Lei stat->real_cnt++;
21730f3bb09SZhen Lei stat->addr = addr;
21830f3bb09SZhen Lei
21930f3bb09SZhen Lei if (stat->save_cnt < MAX_NUM_OF_RECORDS) {
22030f3bb09SZhen Lei stat->addrs[stat->save_cnt] = addr;
22130f3bb09SZhen Lei stat->save_cnt++;
22230f3bb09SZhen Lei }
22330f3bb09SZhen Lei
22430f3bb09SZhen Lei if (stat->real_cnt == stat->max)
22530f3bb09SZhen Lei return 1;
22630f3bb09SZhen Lei }
22730f3bb09SZhen Lei
22830f3bb09SZhen Lei return 0;
22930f3bb09SZhen Lei }
23030f3bb09SZhen Lei
test_perf_kallsyms_on_each_symbol(void)23130f3bb09SZhen Lei static void test_perf_kallsyms_on_each_symbol(void)
23230f3bb09SZhen Lei {
23330f3bb09SZhen Lei u64 t0, t1;
23430f3bb09SZhen Lei struct test_stat stat;
23530f3bb09SZhen Lei
23630f3bb09SZhen Lei memset(&stat, 0, sizeof(stat));
23730f3bb09SZhen Lei stat.max = INT_MAX;
23830f3bb09SZhen Lei stat.name = stub_name;
23930f3bb09SZhen Lei stat.perf = 1;
240da35048fSNicholas Piggin t0 = ktime_get_ns();
24130f3bb09SZhen Lei kallsyms_on_each_symbol(find_symbol, &stat);
242da35048fSNicholas Piggin t1 = ktime_get_ns();
24330f3bb09SZhen Lei pr_info("kallsyms_on_each_symbol() traverse all: %lld ns\n", t1 - t0);
24430f3bb09SZhen Lei }
24530f3bb09SZhen Lei
match_symbol(void * data,unsigned long addr)24630f3bb09SZhen Lei static int match_symbol(void *data, unsigned long addr)
24730f3bb09SZhen Lei {
24830f3bb09SZhen Lei struct test_stat *stat = (struct test_stat *)data;
24930f3bb09SZhen Lei
25030f3bb09SZhen Lei stat->real_cnt++;
25130f3bb09SZhen Lei stat->addr = addr;
25230f3bb09SZhen Lei
25330f3bb09SZhen Lei if (stat->save_cnt < MAX_NUM_OF_RECORDS) {
25430f3bb09SZhen Lei stat->addrs[stat->save_cnt] = addr;
25530f3bb09SZhen Lei stat->save_cnt++;
25630f3bb09SZhen Lei }
25730f3bb09SZhen Lei
25830f3bb09SZhen Lei if (stat->real_cnt == stat->max)
25930f3bb09SZhen Lei return 1;
26030f3bb09SZhen Lei
26130f3bb09SZhen Lei return 0;
26230f3bb09SZhen Lei }
26330f3bb09SZhen Lei
test_perf_kallsyms_on_each_match_symbol(void)26430f3bb09SZhen Lei static void test_perf_kallsyms_on_each_match_symbol(void)
26530f3bb09SZhen Lei {
26630f3bb09SZhen Lei u64 t0, t1;
26730f3bb09SZhen Lei struct test_stat stat;
26830f3bb09SZhen Lei
26930f3bb09SZhen Lei memset(&stat, 0, sizeof(stat));
27030f3bb09SZhen Lei stat.max = INT_MAX;
27130f3bb09SZhen Lei stat.name = stub_name;
272da35048fSNicholas Piggin t0 = ktime_get_ns();
27330f3bb09SZhen Lei kallsyms_on_each_match_symbol(match_symbol, stat.name, &stat);
274da35048fSNicholas Piggin t1 = ktime_get_ns();
27530f3bb09SZhen Lei pr_info("kallsyms_on_each_match_symbol() traverse all: %lld ns\n", t1 - t0);
27630f3bb09SZhen Lei }
27730f3bb09SZhen Lei
test_kallsyms_basic_function(void)27830f3bb09SZhen Lei static int test_kallsyms_basic_function(void)
27930f3bb09SZhen Lei {
28030f3bb09SZhen Lei int i, j, ret;
28130f3bb09SZhen Lei int next = 0, nr_failed = 0;
28230f3bb09SZhen Lei char *prefix;
28330f3bb09SZhen Lei unsigned short rand;
28430f3bb09SZhen Lei unsigned long addr, lookup_addr;
28530f3bb09SZhen Lei char namebuf[KSYM_NAME_LEN];
28630f3bb09SZhen Lei struct test_stat *stat, *stat2;
28730f3bb09SZhen Lei
28830f3bb09SZhen Lei stat = kmalloc(sizeof(*stat) * 2, GFP_KERNEL);
28930f3bb09SZhen Lei if (!stat)
29030f3bb09SZhen Lei return -ENOMEM;
29130f3bb09SZhen Lei stat2 = stat + 1;
29230f3bb09SZhen Lei
29330f3bb09SZhen Lei prefix = "kallsyms_lookup_name() for";
29430f3bb09SZhen Lei for (i = 0; i < ARRAY_SIZE(test_items); i++) {
29530f3bb09SZhen Lei addr = kallsyms_lookup_name(test_items[i].name);
29630f3bb09SZhen Lei if (addr != test_items[i].addr) {
29730f3bb09SZhen Lei nr_failed++;
29830f3bb09SZhen Lei pr_info("%s %s failed: addr=%lx, expect %lx\n",
29930f3bb09SZhen Lei prefix, test_items[i].name, addr, test_items[i].addr);
30030f3bb09SZhen Lei }
30130f3bb09SZhen Lei }
30230f3bb09SZhen Lei
30330f3bb09SZhen Lei prefix = "kallsyms_on_each_symbol() for";
30430f3bb09SZhen Lei for (i = 0; i < ARRAY_SIZE(test_items); i++) {
30530f3bb09SZhen Lei memset(stat, 0, sizeof(*stat));
30630f3bb09SZhen Lei stat->max = INT_MAX;
30730f3bb09SZhen Lei stat->name = test_items[i].name;
30830f3bb09SZhen Lei kallsyms_on_each_symbol(find_symbol, stat);
30930f3bb09SZhen Lei if (stat->addr != test_items[i].addr || stat->real_cnt != 1) {
31030f3bb09SZhen Lei nr_failed++;
31130f3bb09SZhen Lei pr_info("%s %s failed: count=%d, addr=%lx, expect %lx\n",
31230f3bb09SZhen Lei prefix, test_items[i].name,
31330f3bb09SZhen Lei stat->real_cnt, stat->addr, test_items[i].addr);
31430f3bb09SZhen Lei }
31530f3bb09SZhen Lei }
31630f3bb09SZhen Lei
31730f3bb09SZhen Lei prefix = "kallsyms_on_each_match_symbol() for";
31830f3bb09SZhen Lei for (i = 0; i < ARRAY_SIZE(test_items); i++) {
31930f3bb09SZhen Lei memset(stat, 0, sizeof(*stat));
32030f3bb09SZhen Lei stat->max = INT_MAX;
32130f3bb09SZhen Lei stat->name = test_items[i].name;
32230f3bb09SZhen Lei kallsyms_on_each_match_symbol(match_symbol, test_items[i].name, stat);
32330f3bb09SZhen Lei if (stat->addr != test_items[i].addr || stat->real_cnt != 1) {
32430f3bb09SZhen Lei nr_failed++;
32530f3bb09SZhen Lei pr_info("%s %s failed: count=%d, addr=%lx, expect %lx\n",
32630f3bb09SZhen Lei prefix, test_items[i].name,
32730f3bb09SZhen Lei stat->real_cnt, stat->addr, test_items[i].addr);
32830f3bb09SZhen Lei }
32930f3bb09SZhen Lei }
33030f3bb09SZhen Lei
33130f3bb09SZhen Lei if (nr_failed) {
33230f3bb09SZhen Lei kfree(stat);
33330f3bb09SZhen Lei return -ESRCH;
33430f3bb09SZhen Lei }
33530f3bb09SZhen Lei
33630f3bb09SZhen Lei for (i = 0; i < kallsyms_num_syms; i++) {
33730f3bb09SZhen Lei addr = kallsyms_sym_address(i);
33830f3bb09SZhen Lei if (!is_ksym_addr(addr))
33930f3bb09SZhen Lei continue;
34030f3bb09SZhen Lei
34130f3bb09SZhen Lei ret = lookup_symbol_name(addr, namebuf);
34230f3bb09SZhen Lei if (unlikely(ret)) {
34330f3bb09SZhen Lei namebuf[0] = 0;
34433c24beeSKees Cook pr_info("%d: lookup_symbol_name(%lx) failed\n", i, addr);
34530f3bb09SZhen Lei goto failed;
34630f3bb09SZhen Lei }
34730f3bb09SZhen Lei
34830f3bb09SZhen Lei lookup_addr = kallsyms_lookup_name(namebuf);
34930f3bb09SZhen Lei
35030f3bb09SZhen Lei memset(stat, 0, sizeof(*stat));
35130f3bb09SZhen Lei stat->max = INT_MAX;
35230f3bb09SZhen Lei kallsyms_on_each_match_symbol(match_symbol, namebuf, stat);
35330f3bb09SZhen Lei
35430f3bb09SZhen Lei /*
35530f3bb09SZhen Lei * kallsyms_on_each_symbol() is too slow, randomly select some
35630f3bb09SZhen Lei * symbols for test.
35730f3bb09SZhen Lei */
35830f3bb09SZhen Lei if (i >= next) {
35930f3bb09SZhen Lei memset(stat2, 0, sizeof(*stat2));
36030f3bb09SZhen Lei stat2->max = INT_MAX;
36130f3bb09SZhen Lei stat2->name = namebuf;
36230f3bb09SZhen Lei kallsyms_on_each_symbol(find_symbol, stat2);
36330f3bb09SZhen Lei
36430f3bb09SZhen Lei /*
36530f3bb09SZhen Lei * kallsyms_on_each_symbol() and kallsyms_on_each_match_symbol()
36630f3bb09SZhen Lei * need to get the same traversal result.
36730f3bb09SZhen Lei */
36830f3bb09SZhen Lei if (stat->addr != stat2->addr ||
36930f3bb09SZhen Lei stat->real_cnt != stat2->real_cnt ||
37030f3bb09SZhen Lei memcmp(stat->addrs, stat2->addrs,
37133c24beeSKees Cook stat->save_cnt * sizeof(stat->addrs[0]))) {
37233c24beeSKees Cook pr_info("%s: mismatch between kallsyms_on_each_symbol() and kallsyms_on_each_match_symbol()\n",
37333c24beeSKees Cook namebuf);
37430f3bb09SZhen Lei goto failed;
37533c24beeSKees Cook }
37630f3bb09SZhen Lei
37730f3bb09SZhen Lei /*
37830f3bb09SZhen Lei * The average of random increments is 128, that is, one of
37930f3bb09SZhen Lei * them is tested every 128 symbols.
38030f3bb09SZhen Lei */
38130f3bb09SZhen Lei get_random_bytes(&rand, sizeof(rand));
38230f3bb09SZhen Lei next = i + (rand & 0xff) + 1;
38330f3bb09SZhen Lei }
38430f3bb09SZhen Lei
38530f3bb09SZhen Lei /* Need to be found at least once */
38633c24beeSKees Cook if (!stat->real_cnt) {
38733c24beeSKees Cook pr_info("%s: Never found\n", namebuf);
38830f3bb09SZhen Lei goto failed;
38933c24beeSKees Cook }
39030f3bb09SZhen Lei
39130f3bb09SZhen Lei /*
39230f3bb09SZhen Lei * kallsyms_lookup_name() returns the address of the first
39330f3bb09SZhen Lei * symbol found and cannot be NULL.
39430f3bb09SZhen Lei */
39533c24beeSKees Cook if (!lookup_addr) {
39633c24beeSKees Cook pr_info("%s: NULL lookup_addr?!\n", namebuf);
39730f3bb09SZhen Lei goto failed;
39833c24beeSKees Cook }
39933c24beeSKees Cook if (lookup_addr != stat->addrs[0]) {
40033c24beeSKees Cook pr_info("%s: lookup_addr != stat->addrs[0]\n", namebuf);
40133c24beeSKees Cook goto failed;
40233c24beeSKees Cook }
40330f3bb09SZhen Lei
40430f3bb09SZhen Lei /*
40530f3bb09SZhen Lei * If the addresses of all matching symbols are recorded, the
40630f3bb09SZhen Lei * target address needs to be exist.
40730f3bb09SZhen Lei */
40830f3bb09SZhen Lei if (stat->real_cnt <= MAX_NUM_OF_RECORDS) {
40930f3bb09SZhen Lei for (j = 0; j < stat->save_cnt; j++) {
41030f3bb09SZhen Lei if (stat->addrs[j] == addr)
41130f3bb09SZhen Lei break;
41230f3bb09SZhen Lei }
41330f3bb09SZhen Lei
41433c24beeSKees Cook if (j == stat->save_cnt) {
41533c24beeSKees Cook pr_info("%s: j == save_cnt?!\n", namebuf);
41630f3bb09SZhen Lei goto failed;
41730f3bb09SZhen Lei }
41830f3bb09SZhen Lei }
41933c24beeSKees Cook }
42030f3bb09SZhen Lei
42130f3bb09SZhen Lei kfree(stat);
42230f3bb09SZhen Lei
42330f3bb09SZhen Lei return 0;
42430f3bb09SZhen Lei
42530f3bb09SZhen Lei failed:
42630f3bb09SZhen Lei pr_info("Test for %dth symbol failed: (%s) addr=%lx", i, namebuf, addr);
42730f3bb09SZhen Lei kfree(stat);
42830f3bb09SZhen Lei return -ESRCH;
42930f3bb09SZhen Lei }
43030f3bb09SZhen Lei
test_entry(void * p)43130f3bb09SZhen Lei static int test_entry(void *p)
43230f3bb09SZhen Lei {
43330f3bb09SZhen Lei int ret;
43430f3bb09SZhen Lei
43530f3bb09SZhen Lei do {
43630f3bb09SZhen Lei schedule_timeout(5 * HZ);
43730f3bb09SZhen Lei } while (system_state != SYSTEM_RUNNING);
43830f3bb09SZhen Lei
43930f3bb09SZhen Lei pr_info("start\n");
44030f3bb09SZhen Lei ret = test_kallsyms_basic_function();
44130f3bb09SZhen Lei if (ret) {
44230f3bb09SZhen Lei pr_info("abort\n");
44330f3bb09SZhen Lei return 0;
44430f3bb09SZhen Lei }
44530f3bb09SZhen Lei
44630f3bb09SZhen Lei test_kallsyms_compression_ratio();
44730f3bb09SZhen Lei test_perf_kallsyms_lookup_name();
44830f3bb09SZhen Lei test_perf_kallsyms_on_each_symbol();
44930f3bb09SZhen Lei test_perf_kallsyms_on_each_match_symbol();
45030f3bb09SZhen Lei pr_info("finish\n");
45130f3bb09SZhen Lei
45230f3bb09SZhen Lei return 0;
45330f3bb09SZhen Lei }
45430f3bb09SZhen Lei
kallsyms_test_init(void)45530f3bb09SZhen Lei static int __init kallsyms_test_init(void)
45630f3bb09SZhen Lei {
45730f3bb09SZhen Lei struct task_struct *t;
45830f3bb09SZhen Lei
45930f3bb09SZhen Lei t = kthread_create(test_entry, NULL, "kallsyms_test");
46030f3bb09SZhen Lei if (IS_ERR(t)) {
46130f3bb09SZhen Lei pr_info("Create kallsyms selftest task failed\n");
46230f3bb09SZhen Lei return PTR_ERR(t);
46330f3bb09SZhen Lei }
46430f3bb09SZhen Lei kthread_bind(t, 0);
46530f3bb09SZhen Lei wake_up_process(t);
46630f3bb09SZhen Lei
46730f3bb09SZhen Lei return 0;
46830f3bb09SZhen Lei }
46930f3bb09SZhen Lei late_initcall(kallsyms_test_init);
470