Skip to content

Commit 30f3bb0

Browse files
Zhen Leimcgrof
Zhen Lei
authored andcommitted
kallsyms: Add self-test facility
Added test cases for basic functions and performance of functions kallsyms_lookup_name(), kallsyms_on_each_symbol() and kallsyms_on_each_match_symbol(). It also calculates the compression rate of the kallsyms compression algorithm for the current symbol set. The basic functions test begins by testing a set of symbols whose address values are known. Then, traverse all symbol addresses and find the corresponding symbol name based on the address. It's impossible to determine whether these addresses are correct, but we can use the above three functions along with the addresses to test each other. Due to the traversal operation of kallsyms_on_each_symbol() is too slow, only 60 symbols can be tested in one second, so let it test on average once every 128 symbols. The other two functions validate all symbols. If the basic functions test is passed, print only performance test results. If the test fails, print error information, but do not perform subsequent performance tests. Start self-test automatically after system startup if CONFIG_KALLSYMS_SELFTEST=y. Example of output content: (prefix 'kallsyms_selftest:' is omitted start --------------------------------------------------------- | nr_symbols | compressed size | original size | ratio(%) | |---------------------------------------------------------| | 107543 | 1357912 | 2407433 | 56.40 | --------------------------------------------------------- kallsyms_lookup_name() looked up 107543 symbols The time spent on each symbol is (ns): min=630, max=35295, avg=7353 kallsyms_on_each_symbol() traverse all: 11782628 ns kallsyms_on_each_match_symbol() traverse all: 9261 ns finish Signed-off-by: Zhen Lei <[email protected]> Signed-off-by: Luis Chamberlain <[email protected]>
1 parent 9cb3735 commit 30f3bb0

File tree

6 files changed

+514
-1
lines changed

6 files changed

+514
-1
lines changed

include/linux/kallsyms.h

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ static inline void *dereference_symbol_descriptor(void *ptr)
6666
}
6767

6868
#ifdef CONFIG_KALLSYMS
69+
unsigned long kallsyms_sym_address(int idx);
6970
int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *,
7071
unsigned long),
7172
void *data);

init/Kconfig

+13
Original file line numberDiff line numberDiff line change
@@ -1723,6 +1723,19 @@ config KALLSYMS
17231723
symbolic stack backtraces. This increases the size of the kernel
17241724
somewhat, as all symbols have to be loaded into the kernel image.
17251725

1726+
config KALLSYMS_SELFTEST
1727+
bool "Test the basic functions and performance of kallsyms"
1728+
depends on KALLSYMS
1729+
default n
1730+
help
1731+
Test the basic functions and performance of some interfaces, such as
1732+
kallsyms_lookup_name. It also calculates the compression rate of the
1733+
kallsyms compression algorithm for the current symbol set.
1734+
1735+
Start self-test automatically after system startup. Suggest executing
1736+
"dmesg | grep kallsyms_selftest" to collect test results. "finish" is
1737+
displayed in the last line, indicating that the test is complete.
1738+
17261739
config KALLSYMS_ALL
17271740
bool "Include all symbols in kallsyms"
17281741
depends on DEBUG_KERNEL && KALLSYMS

kernel/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ endif
6969
obj-$(CONFIG_UID16) += uid16.o
7070
obj-$(CONFIG_MODULE_SIG_FORMAT) += module_signature.o
7171
obj-$(CONFIG_KALLSYMS) += kallsyms.o
72+
obj-$(CONFIG_KALLSYMS_SELFTEST) += kallsyms_selftest.o
7273
obj-$(CONFIG_BSD_PROCESS_ACCT) += acct.o
7374
obj-$(CONFIG_CRASH_CORE) += crash_core.o
7475
obj-$(CONFIG_KEXEC_CORE) += kexec_core.o

kernel/kallsyms.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ static unsigned int get_symbol_offset(unsigned long pos)
146146
return name - kallsyms_names;
147147
}
148148

149-
static unsigned long kallsyms_sym_address(int idx)
149+
unsigned long kallsyms_sym_address(int idx)
150150
{
151151
if (!IS_ENABLED(CONFIG_KALLSYMS_BASE_RELATIVE))
152152
return kallsyms_addresses[idx];

0 commit comments

Comments
 (0)