[PATCH v3 2/3] module/kallsyms: Refactor current symbol filtering into is_kept_symbol()

From: Tiezhu Yang

Date: Tue Aug 11 2026 - 23:29:46 EST


Currently, the loops in layout_symtab() and add_kallsyms() duplicate the
exact same checks (i == 0 || is_livepatch_module() || is_core_symbol())
to determine whether a symbol should be preserved in the module's core
symbol table.

Rename is_core_symbol() to is_kept_symbol() and move the entire checking
logic inside it, including the special handlers for the first symbol and
livepatch modules.

No functional change.

Suggested-by: Petr Pavlu <petr.pavlu@xxxxxxxx>
Signed-off-by: Tiezhu Yang <yangtiezhu@xxxxxxxxxxx>
---
kernel/module/kallsyms.c | 54 ++++++++++++++++++++--------------------
1 file changed, 27 insertions(+), 27 deletions(-)

diff --git a/kernel/module/kallsyms.c b/kernel/module/kallsyms.c
index a595f8cd29b2..ff8d904afe56 100644
--- a/kernel/module/kallsyms.c
+++ b/kernel/module/kallsyms.c
@@ -75,23 +75,26 @@ static char elf_type(const Elf_Sym *sym, const struct load_info *info)
return '?';
}

-static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs,
- unsigned int shnum, unsigned int pcpundx)
+static bool is_kept_symbol(struct module *mod, const struct load_info *info,
+ unsigned int symnum, const Elf_Sym *src)
{
const Elf_Shdr *sec;
enum mod_mem_type type;

+ if (symnum == 0 || is_livepatch_module(mod))
+ return true;
+
if (src->st_shndx == SHN_UNDEF ||
- src->st_shndx >= shnum ||
+ src->st_shndx >= info->hdr->e_shnum ||
!src->st_name)
return false;

#ifdef CONFIG_KALLSYMS_ALL
- if (src->st_shndx == pcpundx)
+ if (src->st_shndx == info->index.pcpu)
return true;
#endif

- sec = sechdrs + src->st_shndx;
+ sec = info->sechdrs + src->st_shndx;
type = sec->sh_entsize >> SH_ENTSIZE_TYPE_SHIFT;
if (!(sec->sh_flags & SHF_ALLOC)
#ifndef CONFIG_KALLSYMS_ALL
@@ -130,12 +133,11 @@ void layout_symtab(struct module *mod, struct load_info *info)

/* Compute total space required for the core symbols' strtab. */
for (ndst = i = 0; i < nsrc; i++) {
- if (i == 0 || is_livepatch_module(mod) ||
- is_core_symbol(src + i, info->sechdrs, info->hdr->e_shnum,
- info->index.pcpu)) {
- strtab_size += strlen(&info->strtab[src[i].st_name]) + 1;
- ndst++;
- }
+ if (!is_kept_symbol(mod, info, i, src + i))
+ continue;
+
+ strtab_size += strlen(&info->strtab[src[i].st_name]) + 1;
+ ndst++;
}

/* Append room for core symbols at end of core part. */
@@ -197,23 +199,21 @@ void add_kallsyms(struct module *mod, const struct load_info *info)
strtab_size = info->core_typeoffs - info->stroffs;
src = kallsyms->symtab;
for (ndst = i = 0; i < kallsyms->num_symtab; i++) {
+ ssize_t ret;
+
kallsyms->typetab[i] = elf_type(src + i, info);
- if (i == 0 || is_livepatch_module(mod) ||
- is_core_symbol(src + i, info->sechdrs, info->hdr->e_shnum,
- info->index.pcpu)) {
- ssize_t ret;
-
- mod->core_kallsyms.typetab[ndst] =
- kallsyms->typetab[i];
- dst[ndst] = src[i];
- dst[ndst++].st_name = s - mod->core_kallsyms.strtab;
- ret = strscpy(s, &kallsyms->strtab[src[i].st_name],
- strtab_size);
- if (ret < 0)
- break;
- s += ret + 1;
- strtab_size -= ret + 1;
- }
+
+ if (!is_kept_symbol(mod, info, i, src + i))
+ continue;
+
+ mod->core_kallsyms.typetab[ndst] = kallsyms->typetab[i];
+ dst[ndst] = src[i];
+ dst[ndst++].st_name = s - mod->core_kallsyms.strtab;
+ ret = strscpy(s, &kallsyms->strtab[src[i].st_name], strtab_size);
+ if (ret < 0)
+ break;
+ s += ret + 1;
+ strtab_size -= ret + 1;
}

/* Set up to point into init section. */
--
2.42.0