All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v1] module/kallsyms: Filter out local mapping symbols during module load
@ 2026-08-08  3:37 Tiezhu Yang
  2026-08-08  3:53 ` sashiko-bot
  2026-08-10  2:06 ` Tiezhu Yang
  0 siblings, 2 replies; 4+ messages in thread
From: Tiezhu Yang @ 2026-08-08  3:37 UTC (permalink / raw)
  To: Luis Chamberlain, Petr Pavlu, Daniel Gomez, Sami Tolvanen,
	Aaron Tomlin
  Cc: linux-modules, loongarch, linux-kernel

The compiler toolchains generate internal local labels on certain
architectures (such as LoongArch) for optimizations and relocations.

While these local labels are filtered out during runtime lookups in
find_kallsyms_symbol(), they still leak into the permanent symbol
tables of loaded modules, because layout_symtab() and add_kallsyms()
do not check for the mapping symbols during layout generation.

Consequently, tracing tools like bpftrace resolve identical addresses
into confusing local labels instead of actual clear C function names.

Fix this by adding is_mapping_symbol() checks directly into the symbol
tracking loops of layout_symtab() and add_kallsyms(). This prevents the
mapping symbols from entering the module's memory symbol arrays at load
time.

Reproduce steps:

1. Set up a LoongArch VM with "-accel kvm":

   $ sudo qemu-system-loongarch64 -serial stdio \
     -machine virt -cpu la464 -smp 4 -m 4G \
     -bios /usr/share/edk2/loongarch64/QEMU_EFI.fd \
     -nodefaults -no-reboot -nographic -accel kvm

2. Use bpftrace to capture kstack when vCPU is scheduled out:

   $ cat trace_sched.bt
   kprobe:kvm:kvm_sched_out
   {
           if (pid == $1) {
                   print(kstack());
           }
   }

   $ sudo bpftrace trace_sched.bt `pgrep -o qemu-system`

Test results:

1. Before this patch (Confusing stack with local labels):

        kvm_sched_out+0
        __schedule+1584
        schedule+48
        .LVL3767+36
        .LVL3801+8
        .LVL285+12
        .LVL425+44
        .LVL1419+20
        kvm_exc_entry+260

2. After this patch (Accurate stack with function names):

        kvm_sched_out+0
        __schedule+1584
        schedule+48
        kvm_vcpu_block+112
        kvm_vcpu_halt+104
        kvm_emu_idle+284
        kvm_handle_gspr+1316
        kvm_handle_exit+456
        kvm_exc_entry+260

Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
Based on the latest modules-next branch of
https://git.kernel.org/pub/scm/linux/kernel/git/modules/linux.git

 kernel/module/kallsyms.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/kernel/module/kallsyms.c b/kernel/module/kallsyms.c
index f23126d804b2..61dd5c014a59 100644
--- a/kernel/module/kallsyms.c
+++ b/kernel/module/kallsyms.c
@@ -130,6 +130,9 @@ 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 (is_mapping_symbol(&info->strtab[src[i].st_name]))
+			continue;
+
 		if (i == 0 || is_livepatch_module(mod) ||
 		    is_core_symbol(src + i, info->sechdrs, info->hdr->e_shnum,
 				   info->index.pcpu)) {
@@ -198,6 +201,10 @@ void add_kallsyms(struct module *mod, const struct load_info *info)
 	src = kallsyms->symtab;
 	for (ndst = i = 0; i < kallsyms->num_symtab; i++) {
 		kallsyms->typetab[i] = elf_type(src + i, info);
+
+		if (is_mapping_symbol(&kallsyms->strtab[src[i].st_name]))
+			continue;
+
 		if (i == 0 || is_livepatch_module(mod) ||
 		    is_core_symbol(src + i, info->sechdrs, info->hdr->e_shnum,
 				   info->index.pcpu)) {
-- 
2.42.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v1] module/kallsyms: Filter out local mapping symbols during module load
  2026-08-08  3:37 [PATCH v1] module/kallsyms: Filter out local mapping symbols during module load Tiezhu Yang
@ 2026-08-08  3:53 ` sashiko-bot
  2026-08-10  2:06 ` Tiezhu Yang
  1 sibling, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-08-08  3:53 UTC (permalink / raw)
  To: Tiezhu Yang; +Cc: linux-modules

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Filtering mapping symbols from `core_kallsyms.symtab` before checking for livepatch modules fundamentally breaks the 1:1 symbol index mapping required for livepatch delayed relocations.
--

--- Patch [1]: [PATCH v1] module/kallsyms: Filter out local mapping symbols during module load ---
commit 341d5ac5814c0e09a7b6cda2b0753c3161fb7f96
Author: Tiezhu Yang <yangtiezhu@loongson.cn>

module/kallsyms: Filter out local mapping symbols during module load

This patch filters out compiler-generated local mapping symbols from loaded
modules' permanent symbol tables. It modifies layout_symtab() and
add_kallsyms() to skip these mapping symbols during layout generation so
that tracing tools resolve to clear C function names instead.

> --- a/kernel/module/kallsyms.c
> +++ b/kernel/module/kallsyms.c
> @@ -130,6 +130,9 @@ 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 (is_mapping_symbol(&info->strtab[src[i].st_name]))
> +			continue;
> +
>  		if (i == 0 || is_livepatch_module(mod) ||
>  		    is_core_symbol(src + i, info->sechdrs, info->hdr->e_shnum,
>  				   info->index.pcpu)) {

[Severity: High]
Does skipping mapping symbols before the is_livepatch_module() check
incorrectly alter the total symbol count and layout for livepatch modules?

The livepatch subsystem requires mod->core_kallsyms.symtab to be an exact,
1:1 copy of the original ELF symbol table.

[ ... ]

> @@ -198,6 +201,10 @@ void add_kallsyms(struct module *mod, const struct load_info *info)
>  	src = kallsyms->symtab;
>  	for (ndst = i = 0; i < kallsyms->num_symtab; i++) {
>  		kallsyms->typetab[i] = elf_type(src + i, info);
> +
> +		if (is_mapping_symbol(&kallsyms->strtab[src[i].st_name]))
> +			continue;
> +
>  		if (i == 0 || is_livepatch_module(mod) ||
>  		    is_core_symbol(src + i, info->sechdrs, info->hdr->e_shnum,
>  				   info->index.pcpu)) {

[Severity: High]
Will filtering mapping symbols while copying to core_kallsyms.symtab cause
all subsequent symbol indices to physically shift down, breaking livepatch
delayed relocations?

kernel/module/livepatch.c:copy_module_elf() explicitly documents this
requirement:

    /*
     * For livepatch modules, core_kallsyms.symtab is a complete
     * copy of the original symbol table. Adjust sh_addr to point
     * to core_kallsyms.symtab since the copy of the symtab in module
     * init memory is freed at the end of do_init_module().
     */
    mod->klp_info->sechdrs[symndx].sh_addr =
        (unsigned long)mod->core_kallsyms.symtab;

When klp_resolve_symbols() indexes into the table using the original
(unshifted) ELF indices, will it read the wrong symbol or go out of bounds?

kernel/livepatch/core.c:klp_resolve_symbols() {
    ...
        sym = (Elf_Sym *)sechdrs[symndx].sh_addr + ELF_R_SYM(relas[i].r_info);
    ...
}

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260808033715.13117-1-yangtiezhu@loongson.cn?part=1

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v1] module/kallsyms: Filter out local mapping symbols during module load
  2026-08-08  3:37 [PATCH v1] module/kallsyms: Filter out local mapping symbols during module load Tiezhu Yang
  2026-08-08  3:53 ` sashiko-bot
@ 2026-08-10  2:06 ` Tiezhu Yang
  2026-08-10  3:55   ` Huacai Chen
  1 sibling, 1 reply; 4+ messages in thread
From: Tiezhu Yang @ 2026-08-10  2:06 UTC (permalink / raw)
  To: Luis Chamberlain, Petr Pavlu, Daniel Gomez, Sami Tolvanen,
	Aaron Tomlin
  Cc: linux-modules, loongarch, linux-kernel

On 2026/8/8 上午11:37, Tiezhu Yang wrote:
> The compiler toolchains generate internal local labels on certain
> architectures (such as LoongArch) for optimizations and relocations.

...

> Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
> ---
> Based on the latest modules-next branch of
> https://git.kernel.org/pub/scm/linux/kernel/git/modules/linux.git
> 
>   kernel/module/kallsyms.c | 7 +++++++
>   1 file changed, 7 insertions(+)
> 
> diff --git a/kernel/module/kallsyms.c b/kernel/module/kallsyms.c
> index f23126d804b2..61dd5c014a59 100644
> --- a/kernel/module/kallsyms.c
> +++ b/kernel/module/kallsyms.c
> @@ -130,6 +130,9 @@ 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 (is_mapping_symbol(&info->strtab[src[i].st_name]))
> +			continue;
> +
>   		if (i == 0 || is_livepatch_module(mod) ||
>   		    is_core_symbol(src + i, info->sechdrs, info->hdr->e_shnum,
>   				   info->index.pcpu)) {
> @@ -198,6 +201,10 @@ void add_kallsyms(struct module *mod, const struct load_info *info)
>   	src = kallsyms->symtab;
>   	for (ndst = i = 0; i < kallsyms->num_symtab; i++) {
>   		kallsyms->typetab[i] = elf_type(src + i, info);
> +
> +		if (is_mapping_symbol(&kallsyms->strtab[src[i].st_name]))
> +			continue;
> +
>   		if (i == 0 || is_livepatch_module(mod) ||
>   		    is_core_symbol(src + i, info->sechdrs, info->hdr->e_shnum,
>   				   info->index.pcpu)) {
> 

Hi module maintainers,

Regarding the feedback from the AI bot about livepatch breaking [1],
there are two ways to fix it, which style do you prefer?

(1) Using continue

```
diff --git a/kernel/module/kallsyms.c b/kernel/module/kallsyms.c
index f23126d804b2..aece7aa49dd4 100644
--- a/kernel/module/kallsyms.c
+++ b/kernel/module/kallsyms.c
@@ -130,6 +130,10 @@ 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 (!is_livepatch_module(mod) &&
+                   is_mapping_symbol(&info->strtab[src[i].st_name]))
+                       continue;
+
                 if (i == 0 || is_livepatch_module(mod) ||
                     is_core_symbol(src + i, info->sechdrs, 
info->hdr->e_shnum,
                                    info->index.pcpu)) {
@@ -198,6 +202,11 @@ void add_kallsyms(struct module *mod, const struct 
load_info *info)
         src = kallsyms->symtab;
         for (ndst = i = 0; i < kallsyms->num_symtab; i++) {
                 kallsyms->typetab[i] = elf_type(src + i, info);
+
+               if (!is_livepatch_module(mod) &&
+                   is_mapping_symbol(&kallsyms->strtab[src[i].st_name]))
+                       continue;
+
                 if (i == 0 || is_livepatch_module(mod) ||
                     is_core_symbol(src + i, info->sechdrs, 
info->hdr->e_shnum,
                                    info->index.pcpu)) {
```

(2) Using if-statement
```
diff --git a/kernel/module/kallsyms.c b/kernel/module/kallsyms.c
index f23126d804b2..b2b22b3487a8 100644
--- a/kernel/module/kallsyms.c
+++ b/kernel/module/kallsyms.c
@@ -130,9 +130,12 @@ 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) ||
+               bool is_mapping = !is_livepatch_module(mod) &&
+ 
is_mapping_symbol(&info->strtab[src[i].st_name]);
+
+               if (!is_mapping && (i == 0 || is_livepatch_module(mod) ||
                     is_core_symbol(src + i, info->sechdrs, 
info->hdr->e_shnum,
-                                  info->index.pcpu)) {
+                                  info->index.pcpu))) {
                         strtab_size += 
strlen(&info->strtab[src[i].st_name]) + 1;
                         ndst++;
                 }
@@ -198,9 +201,13 @@ void add_kallsyms(struct module *mod, const struct 
load_info *info)
         src = kallsyms->symtab;
         for (ndst = i = 0; i < kallsyms->num_symtab; i++) {
                 kallsyms->typetab[i] = elf_type(src + i, info);
-               if (i == 0 || is_livepatch_module(mod) ||
+
+               bool is_mapping = !is_livepatch_module(mod) &&
+ 
is_mapping_symbol(&kallsyms->strtab[src[i].st_name]);
+
+               if (!is_mapping && (i == 0 || is_livepatch_module(mod) ||
                     is_core_symbol(src + i, info->sechdrs, 
info->hdr->e_shnum,
-                                  info->index.pcpu)) {
+                                  info->index.pcpu))) {
                         ssize_t ret;

                         mod->core_kallsyms.typetab[ndst] =
```

If you have any more comments, please let me know.

[1] 
https://lore.kernel.org/linux-modules/20260808035301.C3E6F1F000E9@smtp.kernel.org/

Thanks,
Tiezhu


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v1] module/kallsyms: Filter out local mapping symbols during module load
  2026-08-10  2:06 ` Tiezhu Yang
@ 2026-08-10  3:55   ` Huacai Chen
  0 siblings, 0 replies; 4+ messages in thread
From: Huacai Chen @ 2026-08-10  3:55 UTC (permalink / raw)
  To: Tiezhu Yang
  Cc: Luis Chamberlain, Petr Pavlu, Daniel Gomez, Sami Tolvanen,
	Aaron Tomlin, linux-modules, loongarch, linux-kernel

Hi, Tiezhu,

On Mon, Aug 10, 2026 at 10:07 AM Tiezhu Yang <yangtiezhu@loongson.cn> wrote:
>
> On 2026/8/8 上午11:37, Tiezhu Yang wrote:
> > The compiler toolchains generate internal local labels on certain
> > architectures (such as LoongArch) for optimizations and relocations.
>
> ...
>
> > Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
> > ---
> > Based on the latest modules-next branch of
> > https://git.kernel.org/pub/scm/linux/kernel/git/modules/linux.git
> >
> >   kernel/module/kallsyms.c | 7 +++++++
> >   1 file changed, 7 insertions(+)
> >
> > diff --git a/kernel/module/kallsyms.c b/kernel/module/kallsyms.c
> > index f23126d804b2..61dd5c014a59 100644
> > --- a/kernel/module/kallsyms.c
> > +++ b/kernel/module/kallsyms.c
> > @@ -130,6 +130,9 @@ 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 (is_mapping_symbol(&info->strtab[src[i].st_name]))
> > +                     continue;
> > +
> >               if (i == 0 || is_livepatch_module(mod) ||
> >                   is_core_symbol(src + i, info->sechdrs, info->hdr->e_shnum,
> >                                  info->index.pcpu)) {
> > @@ -198,6 +201,10 @@ void add_kallsyms(struct module *mod, const struct load_info *info)
> >       src = kallsyms->symtab;
> >       for (ndst = i = 0; i < kallsyms->num_symtab; i++) {
> >               kallsyms->typetab[i] = elf_type(src + i, info);
> > +
> > +             if (is_mapping_symbol(&kallsyms->strtab[src[i].st_name]))
> > +                     continue;
> > +
> >               if (i == 0 || is_livepatch_module(mod) ||
> >                   is_core_symbol(src + i, info->sechdrs, info->hdr->e_shnum,
> >                                  info->index.pcpu)) {
> >
>
> Hi module maintainers,
>
> Regarding the feedback from the AI bot about livepatch breaking [1],
> there are two ways to fix it, which style do you prefer?
I think the first is better.


Huacai
>
> (1) Using continue
>
> ```
> diff --git a/kernel/module/kallsyms.c b/kernel/module/kallsyms.c
> index f23126d804b2..aece7aa49dd4 100644
> --- a/kernel/module/kallsyms.c
> +++ b/kernel/module/kallsyms.c
> @@ -130,6 +130,10 @@ 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 (!is_livepatch_module(mod) &&
> +                   is_mapping_symbol(&info->strtab[src[i].st_name]))
> +                       continue;
> +
>                  if (i == 0 || is_livepatch_module(mod) ||
>                      is_core_symbol(src + i, info->sechdrs,
> info->hdr->e_shnum,
>                                     info->index.pcpu)) {
> @@ -198,6 +202,11 @@ void add_kallsyms(struct module *mod, const struct
> load_info *info)
>          src = kallsyms->symtab;
>          for (ndst = i = 0; i < kallsyms->num_symtab; i++) {
>                  kallsyms->typetab[i] = elf_type(src + i, info);
> +
> +               if (!is_livepatch_module(mod) &&
> +                   is_mapping_symbol(&kallsyms->strtab[src[i].st_name]))
> +                       continue;
> +
>                  if (i == 0 || is_livepatch_module(mod) ||
>                      is_core_symbol(src + i, info->sechdrs,
> info->hdr->e_shnum,
>                                     info->index.pcpu)) {
> ```
>
> (2) Using if-statement
> ```
> diff --git a/kernel/module/kallsyms.c b/kernel/module/kallsyms.c
> index f23126d804b2..b2b22b3487a8 100644
> --- a/kernel/module/kallsyms.c
> +++ b/kernel/module/kallsyms.c
> @@ -130,9 +130,12 @@ 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) ||
> +               bool is_mapping = !is_livepatch_module(mod) &&
> +
> is_mapping_symbol(&info->strtab[src[i].st_name]);
> +
> +               if (!is_mapping && (i == 0 || is_livepatch_module(mod) ||
>                      is_core_symbol(src + i, info->sechdrs,
> info->hdr->e_shnum,
> -                                  info->index.pcpu)) {
> +                                  info->index.pcpu))) {
>                          strtab_size +=
> strlen(&info->strtab[src[i].st_name]) + 1;
>                          ndst++;
>                  }
> @@ -198,9 +201,13 @@ void add_kallsyms(struct module *mod, const struct
> load_info *info)
>          src = kallsyms->symtab;
>          for (ndst = i = 0; i < kallsyms->num_symtab; i++) {
>                  kallsyms->typetab[i] = elf_type(src + i, info);
> -               if (i == 0 || is_livepatch_module(mod) ||
> +
> +               bool is_mapping = !is_livepatch_module(mod) &&
> +
> is_mapping_symbol(&kallsyms->strtab[src[i].st_name]);
> +
> +               if (!is_mapping && (i == 0 || is_livepatch_module(mod) ||
>                      is_core_symbol(src + i, info->sechdrs,
> info->hdr->e_shnum,
> -                                  info->index.pcpu)) {
> +                                  info->index.pcpu))) {
>                          ssize_t ret;
>
>                          mod->core_kallsyms.typetab[ndst] =
> ```
>
> If you have any more comments, please let me know.
>
> [1]
> https://lore.kernel.org/linux-modules/20260808035301.C3E6F1F000E9@smtp.kernel.org/
>
> Thanks,
> Tiezhu
>
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-08-10  3:55 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-08  3:37 [PATCH v1] module/kallsyms: Filter out local mapping symbols during module load Tiezhu Yang
2026-08-08  3:53 ` sashiko-bot
2026-08-10  2:06 ` Tiezhu Yang
2026-08-10  3:55   ` Huacai Chen

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.