[PATCH 07/14] module: Add module_kallsyms_on_each_core_symbol()
Josh Poimboeuf <[email protected]> Sun, 2 Aug 2026 20:24:29 -0700
| Newsgroups | org.kernel.vger.linux-modules,org.kernel.vger.linux-kernel,org.kernel.vger.live-patching |
|---|---|
| Message-ID | <9ff8d9f0ad20a9a5cdbab3fe650d3532688065d4.1785727106.git.jpoimboe@kernel.org> |
module_kallsyms_on_each_symbol() iterates mod->kallsyms, which points at the full init symbol table until do_init_module() swaps it out. The set of symbols it reports thus differs based on whether init memory has been freed yet. Add module_kallsyms_on_each_core_symbol() for callers which need a symbol's position to be the same before and after that swap. core_kallsyms is fully populated by add_kallsyms() before the module leaves MODULE_STATE_UNFORMED, so it's readable on both paths. Signed-off-by: Josh Poimboeuf <[email protected]> --- include/linux/module.h | 11 ++++++++++ kernel/module/kallsyms.c | 46 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/include/linux/module.h b/include/linux/module.h index 7566815fabbe..4ea4522a5fc5 100644 --- a/include/linux/module.h +++ b/include/linux/module.h @@ -947,6 +947,9 @@ static inline bool module_sig_ok(struct module *module) int module_kallsyms_on_each_symbol(const char *modname, int (*fn)(void *, const char *, unsigned long), void *data); +int module_kallsyms_on_each_core_symbol(const char *modname, + int (*fn)(void *, const char *, unsigned long), + void *data); /* For kallsyms to ask for address resolution. namebuf should be at * least KSYM_NAME_LEN long: a pointer to namebuf is returned if @@ -984,6 +987,14 @@ static inline int module_kallsyms_on_each_symbol(const char *modname, return -EOPNOTSUPP; } +static inline int +module_kallsyms_on_each_core_symbol(const char *modname, + int (*fn)(void *, const char *, unsigned long), + void *data) +{ + return -EOPNOTSUPP; +} + /* For kallsyms to ask for address resolution. NULL means not found. */ static inline int module_address_lookup(unsigned long addr, unsigned long *symbolsize, diff --git a/kernel/module/kallsyms.c b/kernel/module/kallsyms.c index 0fc11e45df9b..3a959c3c9f9b 100644 --- a/kernel/module/kallsyms.c +++ b/kernel/module/kallsyms.c @@ -494,3 +494,49 @@ int module_kallsyms_on_each_symbol(const char *modname, mutex_unlock(&module_mutex); return ret; } + +/* + * Iterate @modname's cut-down core symbol table, rather than mod->kallsyms + * which points at the full init symbol table until do_init_module() swaps it + * out. For callers which need a symbol's position to be the same before and + * after that swap. + * + * core_kallsyms is populated by add_kallsyms(), which runs before the module + * leaves MODULE_STATE_UNFORMED. + * + * Unlike module_kallsyms_on_each_symbol(), @modname is required. + */ +int module_kallsyms_on_each_core_symbol(const char *modname, + int (*fn)(void *, const char *, unsigned long), + void *data) +{ + struct mod_kallsyms *kallsyms; + struct module *mod; + unsigned int i; + int ret = 0; + + if (!modname) + return -EINVAL; + + guard(mutex)(&module_mutex); + + mod = find_module_all(modname, strlen(modname), false); + if (!mod) + return -ENOENT; + + kallsyms = &mod->core_kallsyms; + + for (i = 0; i < kallsyms->num_symtab; i++) { + const Elf_Sym *sym = &kallsyms->symtab[i]; + + if (sym->st_shndx == SHN_UNDEF) + continue; + + ret = fn(data, kallsyms_symbol_name(kallsyms, i), + kallsyms_symbol_value(sym)); + if (ret) + break; + } + + return ret; +} -- 2.54.0