module/kallsyms: sort function symbols and use binary search
Subsystems (4)
v3 cover module/kallsyms: sort function symbols and use binary search
This series enables binary search over a separate index of core text symbols, preserving symbol-table order and supporting livepatch modules.
v3:
- preserve core symtab order by sorting a separate index array
- index core text symbols rather than only function symbols
- enable indexed text lookup for livepatch modules
- rebase and update commit messages and subjects
- sort equal-address by descending symbol index so binary search selects the same alias as linear search
v2: https://lore.kernel.org/linux-modules/[email protected]/
- fix searching data symbols for CONFIG_KALLSYMS_ALL
- use kallsyms_symbol_value() in elf_sym_cmp()
v1: https://lore.kernel.org/linux-modules/[email protected]/
Stanislaw Gruszka (3):
module/kallsyms: include empty names in is_ignored_kernel_symbol() module/kallsyms: sort text symbol indices and use binary search module/kallsyms: enable indexed text lookup for livepatch modules
include/linux/module.h | 2 + include/linux/module_symbol.h | 6 +- kernel/module/internal.h | 2 + kernel/module/kallsyms.c | 202 +++++++++++++++++++++++++--------- scripts/mod/modpost.h | 2 +- 5 files changed, 161 insertions(+), 53 deletions(-)
Signature · 1 line
2.52.0
PATCH v3 1/3 module/kallsyms: include empty names in is_ignored_kernel_symbol()
Both modpost and find_kallsyms_symbol() reject empty symbol names. Handle them in the shared helper to avoid duplicating this check.
This also lets is_core_symbol() reject empty names referenced by a nonzero st_name. The reserved ELF null symbol at index 0 is preserved before calling the helper.
--- include/linux/module_symbol.h | 6 ++++-- kernel/module/kallsyms.c | 7 +------ scripts/mod/modpost.h | 2 +- 3 files changed, 6 insertions(+), 9 deletions(-)
include/linux/module_symbol.h +4 -2
@@ -8,11 +8,13 @@ enum ksym_flags { };/* -* Ignore local labels (.L*, L0*) and mapping symbols ($*). These symbols are -* not useful for the kernel, for example, they should not appear in kallsyms. +* Ignore empty names, local labels (.L*, L0*) and mapping symbols ($*). +* These symbols are not useful for the kernel and should not appear in kallsyms. */ static inline bool is_ignored_kernel_symbol (const char *str) { +if (!str[0 ]) +return true;if (str[0 ] =='.' && str[1 ] =='L' )return true;if (str[0 ] =='L' && str[1 ] =='0' )
kernel/module/kallsyms.c +1 -6
@@ -292,12 +292,7 @@ static const char *find_kallsyms_symbol(struct module *mod,if (sym->st_shndx ==SHN_UNDEF )continue ; -/* -* We ignore unnamed symbols: they're uninformative -* and inserted at a whim. -*/ -if (*kallsyms_symbol_name (kallsyms, i) =='\0' || -is_ignored_kernel_symbol (kallsyms_symbol_name (kallsyms, i))) +if (is_ignored_kernel_symbol (kallsyms_symbol_name (kallsyms, i)))continue ;if (thisval <= addr && thisval > bestval) {
scripts/mod/modpost.h +1 -1
@@ -196,7 +196,7 @@ static inline bool is_valid_name(struct elf_info *elf, Elf_Sym *sym) {const char *name = elf->strtab + sym->st_name; -if (!name || !strlen (name)) +if (!name)return false;return !is_ignored_kernel_symbol (name); }
Signature · 1 line
2.52.0
PATCH v3 2/3 module/kallsyms: sort text symbol indices and use binary search
In reply to Stanislaw GruszkaModule symbol lookup via find_kallsyms_symbol() performs a linear scan over the entire symtab when resolving an address. The number of symbols in module symtabs has grown over the years, largely due to additional metadata in non-standard sections, making this lookup very slow.
Build a separate array of indices to core text symbols and sort it by resolved address during module load. Binary-search this array for core text address lookups to avoid scanning the entire symbol table. Sorting only the index array preserves symbol-table order for existing consumers.
The additional module load overhead is small. In repeated module load tests [1], this change increased load time by up to 4%, depending on the kernel configuration. No measurable increase was observed with cold caches or with configurations enabling other features that increase module load time.
Sorting could theoretically be done at build time, but would require reproducing the kernel's symbol address resolution. Keeping that logic in sync with the module loader would be error-prone. Build-time sorting would also increase kernel build time, contrary to recent efforts to reduce it [2].
The improvement can be observed when listing ftrace filter functions.
Before:
root@nano:~# time cat /sys/kernel/tracing/available_filter_functions | wc -l 74908
real 0m1.315s
user 0m0.000s
sys 0m1.312s
After:
root@nano:~# time cat /sys/kernel/tracing/available_filter_functions | wc -l 74911
real 0m0.167s
user 0m0.004s
sys 0m0.175s
(there are three more symbols introduced by the patch)
--- include/linux/module.h | 2 + kernel/module/internal.h | 2 + kernel/module/kallsyms.c | 194 ++++++++++++++++++++++++++++++--------- 3 files changed, 154 insertions(+), 44 deletions(-)
include/linux/module.h +2 -0
@@ -374,8 +374,10 @@ struct module_memory {struct mod_kallsyms {Elf_Sym *symtab;unsigned int num_symtab; +unsigned int num_text_syms;char *strtab;char *typetab; +unsigned int *text_idxtab; };#ifdef CONFIG_LIVEPATCH
kernel/module/internal.h +2 -0
@@ -71,6 +71,8 @@ struct load_info {bool sig_ok;#ifdef CONFIG_KALLSYMS unsigned long mod_kallsyms_init_off; +unsigned long num_text_syms; +unsigned long text_idxtab_off;#endif #ifdef CONFIG_MODULE_DECOMPRESS #ifdef CONFIG_MODULE_STATS
kernel/module/kallsyms.c +150 -44
309 lines not on this page · raw message
Signature · 1 line
2.52.0
[email protected]
AI reviewRe: [PATCH v3 2/3] module/kallsyms: sort text symbol indices and use binary search
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] module/kallsyms: leftover commented-out debug function pr_tab() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] module/kallsyms: leftover commented-out debug function pr_tab() --
commit 70739117310db5d1cdad00c748372a0032a51a0f
Author: Stanislaw Gruszka <[email protected]>
module/kallsyms: sort text symbol indices and use binary search
This commit builds a sorted index array of core text symbols during module load and uses binary search to speed up module text address lookups in find_kallsyms_symbol().
Quoted 4 lines
kernel/module/kallsyms.c +0
-0
[ ... ]
Quoted 16 lines
kernel/module/kallsyms.c +6
-0@@ -161,12 +259,28 @@ void layout_symtab(struct module *mod, struct load_info *info) mod_mem_init_data->size =ALIGN (mod_mem_init_data->size, __alignof__(struct mod_kallsyms )); info->mod_kallsyms_init_off = mod_mem_init_data->size; + info->num_text_syms = ntxt; mod_mem_init_data->size +=sizeof (struct mod_kallsyms ); info->init_typeoffs = mod_mem_init_data->size; mod_mem_init_data->size += nsrc *sizeof (char ); } +// static void pr_tab(unsigned int *tab, unsigned int n, Elf_Sym *syms) +// { +// int i; +// // printk("\n"); +// for (i = 0; i < n; i++) {
kernel/module/kallsyms.c void layout_symtab(struct module *mod, struct load_info *info) · line 283
Quoted 10 lines
kernel/module/kallsyms.c +10
-0+// printk(KERN_CONT "%d,", tab[i]); +// } +// printk("VALUES \n"); +// for (i = 0; i < n; i++) { +// printk(KERN_CONT "0x%08llx,", +// (unsigned long long)syms[tab[i]].st_value); +// } +// printk("\n"); +// } +//
[Severity: Low]
Was this commented-out pr_tab() debug helper left in by mistake?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH v3 2/3] module/kallsyms: sort text symbol indices and use binary search
Quoted 10 lines · On Wed, Nov 18, 2026 at 03:02:42PM +0000, [email protected] wrote:
Quoted 10 lines
kernel/module/kallsyms.c +10
-0+// static void pr_tab(unsigned int *tab, unsigned int n, Elf_Sym *syms) +// { +// int i; +// // printk("\n"); +// for (i = 0; i < n; i++) { +// printk(KERN_CONT "%d,", tab[i]); +// } +// printk("VALUES \n"); +// for (i = 0; i < n; i++) { +// printk(KERN_CONT "0x%08llx,",
Quoted 8 lines
kernel/module/kallsyms.c void layout_symtab(struct module *mod, struct load_info *info) · line 283
Quoted 5 lines
kernel/module/kallsyms.c +5
-0+// (unsigned long long)syms[tab[i]].st_value); +// } +// printk("\n"); +// } +// [Severity: Low]
Was this commented-outpr_tab()debug helper left in by mistake?
Yes.
Quoted 3 lines
Signature · 2 lines
-- Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH v3 3/3 module/kallsyms: enable indexed text lookup for livepatch modules
In reply to Stanislaw Gruszkais_core_symbol() returns early for livepatch modules, preventing their core text symbols from entering the sorted index. Address lookup therefore falls back to a linear scan.
Allow eligible core text symbols to be indexed while preserving all livepatch symbols, so core text address lookups can use binary search.
--- kernel/module/kallsyms.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
kernel/module/kallsyms.c +5 -4
@@ -82,17 +82,18 @@ static unsigned int is_core_symbol(struct module *mod, {const Elf_Shdr *sec;enum mod_mem_type type; +bool is_lp =is_livepatch_module (mod); -if (symnum ==0 ||is_livepatch_module (mod)) +if (symnum ==0 )return 1 ;if (src->st_shndx ==SHN_UNDEF || src->st_shndx >= info->hdr->e_shnum || !src->st_name) -return 0 ; +return is_lp;if (is_ignored_kernel_symbol (&info->strtab[src->st_name])) -return 0 ; +return is_lp;#ifdef CONFIG_KALLSYMS_ALL if (src->st_shndx == info->index.pcpu)
@@ -106,7 +107,7 @@ static unsigned int is_core_symbol(struct module *mod, || !(sec->sh_flags & SHF_EXECINSTR) #endif || mod_mem_type_is_init(type)) -return 0 ; +return is_lp; return 1 << (type == MOD_TEXT); }
Signature · 1 line
2.52.0