kallsyms: embed source file:line info in kernel stack traces
Subsystems (4)
v7 cover kallsyms: embed source file:line info in kernel stack traces
This series adds CONFIG_KALLSYMS_LINEINFO, which embeds source file:line information directly in the kernel image so that stack traces annotate every frame with the originating source location - no external tools, no debug symbols at runtime, and safe to use in NMI/panic context.
Motivation
==========
The recent "slowly decommission bugzilla?" thread surfaced a recurring
problem: when users encounter kernel crashes they see stack traces like func+0x1ec/0x240 but have no way to identify which subsystem or maintainer to contact. Richard Weinberger proposed building a database mapping symbols to source files using nm/DWARF. Linus pointed to scripts/decode_stacktrace.sh as the existing solution. But as the discussion progressed, it became clear that decode_stacktrace.sh has significant practical barriers that prevent it from being useful in the common case.
Problems with scripts/decode_stacktrace.sh
==========================================
- Requires debug symbols: the script needs vmlinux with DWARF debug info. Many distros don't retain debug symbols for older or security kernels, and even when available, asking users to obtain matching debuginfo packages is a significant hurdle.
- Requires toolchain: users need addr2line and nm installed.
- Version-matching requirement: debug symbols must exactly match the running kernel binary.
What this series does
=====================
Patch 1: CONFIG_KALLSYMS_LINEINFO
At build time, a host tool (scripts/gen_lineinfo) reads DWARF .debug_line from vmlinux, extracts address-to-file:line mappings, and embeds them as sorted lookup tables in .rodata. At runtime, kallsyms_lookup_lineinfo() binary-searches the table and sprint_backtrace() appends "(file:line)" to each stack frame. NMI/panic-safe (no locks, no allocations), KASLR-compatible.
Patch 2: CONFIG_KALLSYMS_LINEINFO_MODULES
Extends lineinfo to loadable modules. Each .ko gets .mod_lineinfo and .init.mod_lineinfo sections embedded at build time, each carrying one or more per-section sub-tables identified by an ELF relocation against the covered text section's symbol. The module loader's existing apply_relocations() pass resolves the anchor on load - no special-cased loader logic, no implicit MOD_TEXT-base assumption. Coverage extends to .text, .exit.text, and .init.text; the init blob is freed alongside init memory.
Patch 3: delta compression
Block-indexed delta-encoding with LEB128 varints, implementing the approach suggested by Juergen Gross in the RFC review. Reduces overhead from ~44 MiB to ~11 MiB (~3.7 bytes/entry), addressing the primary size concern from the RFC. Compression applies independently to each per-section table inside the module blobs.
Patch 4: KUnit tests
30 KUnit tests covering the lineinfo lookup paths, delta-decode logic, boundary conditions, the module-side multi-section format, and integration with the backtrace formatting APIs.
Example output
==============
[ 11.206749] dump_stack_lvl+0x5d/0x80 (lib/dump_stack.c:94) [ 11.207403] vpanic+0x36e/0x620 (kernel/panic.c:650) [ 11.209324] panic+0xc9/0xd0 (kernel/panic.c:787) [ 11.213312] sysrq_handle_crash+0x1a/0x20 (drivers/tty/sysrq.c:154) [ 11.214005] __handle_sysrq.cold+0x66/0x256 (drivers/tty/sysrq.c:611) [ 11.214712] write_sysrq_trigger+0x65/0x80 (drivers/tty/sysrq.c:1221) [ 11.215424] proc_reg_write+0x1bd/0x3c0 (fs/proc/inode.c:330) [ 11.216061] vfs_write+0x1c6/0xff0 (fs/read_write.c:686) [ 11.218848] ksys_write+0xfa/0x200 (fs/read_write.c:740) [ 11.222394] do_syscall_64+0xf3/0x690 (arch/x86/entry/syscall_64.c:63)
Size impact
===========
Measured with a Debian kernel config:
- bzImage: +3.6 MiB (14 MiB -> 18 MiB, +26%)
- Runtime memory: +5.9 MiB (text+data+bss)
- Code overhead: +5.0 KiB (.text, lookup functions only)
- Data overhead: +5.9 MiB (.data, lineinfo tables)
The ~5.9 MiB is after 2.7x delta compression; uncompressed would be ~16 MiB. This is a fraction of the cost of shipping full DWARF debug info (hundreds of MiB), which distros must store and serve for every kernel version.
For distros, maintaining debug symbol repositories is expensive:
storage, mirrors, and CDN bandwidth for hundreds of MiB per kernel build add up quickly. A ~5.9 MiB increase in the kernel image itself is a modest cost that eliminates the need for users to find, download, and version-match debuginfo packages just to make a crash report useful.
For developers, the file:line annotations appear immediately in crash traces - no post-processing with decode_stacktrace.sh needed.
Changes since v6
================
- Address Sashiko AI review comments.
Changes since v5
================
- Rebased onto v7.2-rc2:
- scripts/Makefile.modfinal: adapt to the removal of if_changed_except in commit 7abef41afad05b ("kbuild/btf: Remove broken module relinking exclusion").
- Drop unrelated MAINTAINERS hunks that had leaked into patch 2 during v5 preparation; those updates are already upstream.
Changes since v4
================
- Replace mod->mem[MOD_TEXT].base derivation with an ELF relocation against the covered section's symbol; resolved by the module loader on load. (Suggested by Petr Pavlu)
- Cover .exit.text and .init.text in addition to .text. Init code lives in a parallel .init.mod_lineinfo blob (MOD_INIT_RODATA) that is revoked and freed alongside init memory.
- Switch the build step from objcopy --add-section to ld -r, so .rela.mod_lineinfo / .rela.init.mod_lineinfo ride along into the .ko.
- Pick up lineinfo sections via section_objs() (not any_section_objs) to validate SHF_ALLOC and avoid dangling pointers into the temporary load image.
- Drop the unused "reserved" placeholder field from mod_lineinfo_root and mod_lineinfo_header. (Suggested by Petr Pavlu)
- Guard the per-module fields with #ifdef CONFIG_KALLSYMS_LINEINFO_MODULES and expose them via inline reader accessors. (Suggested by Petr Pavlu)
- Update the per-section header layout comment to name struct fields. (Suggested by Petr Pavlu)
- Cap vmlinux-side captured DWARF addresses at _etext: sections placed after .rodata shift as the real lineinfo replaces the empty stub during the multi-pass link, so addresses outside the invariant .text window become stale in the final image.
- Append "(file:line)" only via sprint_backtrace*(), not plain sprint_symbol*() (which backs %ps); many format strings already follow %ps with literal "()" and would otherwise render as "foo (file:line)()". KUnit gains a sprint_with_lineinfo() helper.
- Build-side fixes in scripts/gen_lineinfo: apply 64-bit absolute relocations to .debug_line (libdw doesn't relocate ET_REL inputs); endian-safe writes for cross-builds; declare empty .text/.exit.text/.init.text stanzas so ld -r binds anchors to LOCAL rather than GLOBAL UND symbols.
Changes since v3
=================
- Remove redundant gen_lineinfo entry in scripts/Makefile for CONFIG_KALLSYMS_LINEINFO_MODULES (depends on CONFIG_KALLSYMS_LINEINFO which already builds it). (Reported by Petr Pavlu)
- Use R_* constants from <elf.h> instead of hardcoded relocation type values in r_type_abs32(). (Reported by Petr Pavlu)
- Simplify duplicated-path detection in make_relative(): replace loop over every '/' with a direct midpoint check, since true path duplication always splits at len/2. (Suggested by Petr Pavlu)
- Fix comment in process_dwarf(): sections in ET_REL objects have sh_addr == 0 and therefore overlapping address ranges; this is expected behavior, not a "may" situation. (Reported by Petr Pavlu)
- Use U32_MAX instead of UINT_MAX for the module raw_offset bounds check, matching the u32 type of the addrs array. (Reported by Petr Pavlu)
- Document the assumption that .text is at the start of the MOD_TEXT segment in module_lookup_lineinfo(). A proper fix using ELF relocations is planned for a future series.
(Reported by Petr Pavlu)
- Wrap -fno-inline-functions-called-once in $(call cc-option,...) for clang compatibility. Clang does not support this GCC-specific flag; the noinline attribute is sufficient.
Changes since v2
=================
- Replace #ifdef CONFIG_KALLSYMS_LINEINFO with IS_ENABLED() throughout, so the compiler checks the code for syntax errors regardless of configuration. (Suggested by Helge Deller)
- Replace zigzag + ULEB128 encoding of signed deltas with native SLEB128, removing the unnecessary zigzag transform layer.
(Suggested by Vivian Wang)
- Deduplicate the binary search and delta-decode logic: extract shared struct lineinfo_table and lineinfo_search() into mod_lineinfo.h instead of maintaining near-identical copies in kernel/kallsyms.c and kernel/module/kallsyms.c. (Suggested by Vivian Wang)
- Use .uleb128 / .sleb128 assembler directives in gen_lineinfo output instead of encoding varints in C and emitting .byte hex literals. (Suggested by Vivian Wang)
- Redesign module mod_lineinfo_header to use explicit (offset, size) pairs for each sub-array, similar to flattened devicetree layout. This makes bounds validation straightforward: offset + size <= section_size. (Suggested by Vivian Wang)
- Remove dead sym_start parameter from kallsyms_lookup_lineinfo() and module_lookup_lineinfo().
Changes since v1
=================
- Fix path stripping regression on architectures where DWARF comp_dir is a subdirectory (e.g. arch/parisc/kernel) rather than the source tree
root: paths now correctly show "kernel/traps.c:212" instead of bare "traps.c:212". Added kernel_dirs[] fallback scan and bare-filename recovery via comp_dir. (Reported by Helge Deller)
- Fix RST heading: overline/underline must be at least as long as the heading text in kallsyms-lineinfo.rst. (Reported by Randy Dunlap)
- Fix MAINTAINERS alphabetical ordering: move KALLSYMS LINEINFO entry before KASAN. (Reported by Randy Dunlap)
- Fix arch-portability of .debug_line relocation handling: replace hardcoded R_X86_64_32 with r_type_abs32() supporting x86, arm, arm64, riscv, s390, mips, ppc, loongarch, and parisc.
Sasha Levin (4):
kallsyms: embed source file:line info in kernel stack traces
kallsyms: extend lineinfo to loadable modules
kallsyms: delta-compress lineinfo tables for ~2.7x size reduction
kallsyms: add KUnit tests for lineinfo feature
Documentation/admin-guide/index.rst | 1 + .../admin-guide/kallsyms-lineinfo.rst | 97 ++ MAINTAINERS | 9 + include/linux/kallsyms.h | 18 +- include/linux/mod_lineinfo.h | 312 ++++ include/linux/module.h | 40 + init/Kconfig | 35 + kernel/kallsyms.c | 98 +- kernel/kallsyms_internal.h | 11 + kernel/module/kallsyms.c | 180 ++ kernel/module/main.c | 26 + lib/Kconfig.debug | 10 + lib/tests/Makefile | 3 + lib/tests/lineinfo_kunit.c | 973 +++++++++++ scripts/.gitignore | 1 + scripts/Makefile | 3 + scripts/Makefile.modfinal | 6 + scripts/empty_lineinfo.S | 38 + scripts/gen-mod-lineinfo.sh | 50 + scripts/gen_lineinfo.c | 1458 +++++++++++++++++ scripts/kallsyms.c | 17 + scripts/link-vmlinux.sh | 43 +- 22 files changed, 3418 insertions(+), 11 deletions(-) create mode 100644 Documentation/admin-guide/kallsyms-lineinfo.rst create mode 100644 include/linux/mod_lineinfo.h create mode 100644 lib/tests/lineinfo_kunit.c create mode 100644 scripts/empty_lineinfo.S create mode 100644 scripts/gen-mod-lineinfo.sh create mode 100644 scripts/gen_lineinfo.c
base-commit: 8cdeaa50eae8dad34885515f62559ee83e7e8dda
Signature · 1 line
2.53.0
PATCH v7 1/4 kallsyms: embed source file:line info in kernel stack traces
Add CONFIG_KALLSYMS_LINEINFO, which embeds a compact address-to-line lookup table in the kernel image so stack traces directly print source file and line number information:
root@localhost:~# echo c > /proc/sysrq-trigger [ 11.201987] sysrq: Trigger a crash [ 11.202831] Kernel panic - not syncing: sysrq triggered crash [ 11.206218] Call Trace: [ 11.206501] <TASK> [ 11.206749] dump_stack_lvl+0x5d/0x80 (lib/dump_stack.c:94) [ 11.207403] vpanic+0x36e/0x620 (kernel/panic.c:650) [ 11.208565] ? __lock_acquire+0x465/0x2240 (kernel/locking/lockdep.c:4674) [ 11.209324] panic+0xc9/0xd0 (kernel/panic.c:787) [ 11.211873] ? find_held_lock+0x2b/0x80 (kernel/locking/lockdep.c:5350) [ 11.212597] ? lock_release+0xd3/0x300 (kernel/locking/lockdep.c:5535) [ 11.213312] sysrq_handle_crash+0x1a/0x20 (drivers/tty/sysrq.c:154) [ 11.214005] __handle_sysrq.cold+0x66/0x256 (drivers/tty/sysrq.c:611) [ 11.214712] write_sysrq_trigger+0x65/0x80 (drivers/tty/sysrq.c:1221) [ 11.215424] proc_reg_write+0x1bd/0x3c0 (fs/proc/inode.c:330) [ 11.216061] vfs_write+0x1c6/0xff0 (fs/read_write.c:686) [ 11.218848] ksys_write+0xfa/0x200 (fs/read_write.c:740) [ 11.222394] do_syscall_64+0xf3/0x690 (arch/x86/entry/syscall_64.c:63) [ 11.223942] entry_SYSCALL_64_after_hwframe+0x77/0x7f (arch/x86/entry/entry_64.S:121)
At build time, a new host tool (scripts/gen_lineinfo) reads DWARF .debug_line from vmlinux using libdw (elfutils), extracts all address-to-file:line mappings, and generates an assembly file with sorted parallel arrays (offsets from _text, file IDs, and line numbers). These are linked into vmlinux as .rodata.
At runtime, kallsyms_lookup_lineinfo() does a binary search on the table and __sprint_symbol() appends "(file:line)" to each stack frame. The lookup uses offsets from _text so it works with KASLR, requires no locks or allocations, and is safe in any context including panic.
The feature requires CONFIG_DEBUG_INFO (for DWARF data) and elfutils (libdw-dev) on the build host.
Memory footprint measured with a 1852-option x86_64 config:
Table: 4,597,583 entries from 4,841 source files
lineinfo_addrs[] 4,597,583 x u32 = 17.5 MiB
lineinfo_file_ids[] 4,597,583 x u16 = 8.8 MiB
lineinfo_lines[] 4,597,583 x u32 = 17.5 MiB
file_offsets + filenames ~ 0.1 MiBTotal .rodata increase: ~ 44.0 MiB
vmlinux (stripped): 529 MiB -> 573 MiB (+44 MiB / +8.3%)
The .config used for testing is a simple KVM guest configuration for local development and testing.
--- Documentation/admin-guide/index.rst | 1 + .../admin-guide/kallsyms-lineinfo.rst | 72 +++ MAINTAINERS | 6 + include/linux/kallsyms.h | 18 +- init/Kconfig | 20 + kernel/kallsyms.c | 96 ++- kernel/kallsyms_internal.h | 9 + scripts/.gitignore | 1 + scripts/Makefile | 3 + scripts/empty_lineinfo.S | 30 + scripts/gen_lineinfo.c | 557 ++++++++++++++++++ scripts/kallsyms.c | 16 + scripts/link-vmlinux.sh | 43 +- 13 files changed, 861 insertions(+), 11 deletions(-) create mode 100644 Documentation/admin-guide/kallsyms-lineinfo.rst create mode 100644 scripts/empty_lineinfo.S create mode 100644 scripts/gen_lineinfo.c
Documentation/admin-guide/index.rst +1 -0
@@ -73,6 +73,7 @@ problems and bugs in particular. ramoops dynamic-debug-howto init + kallsyms-lineinfo kdump/index perf/index pstore-blk
Documentation/admin-guide/kallsyms-lineinfo.rst new file +72 -0
72 lines not on this page · raw message
MAINTAINERS +6 -0
@@ -13931,6 +13931,12 @@ S: Maintained F: Documentation/hwmon/k8temp.rst F: drivers/hwmon/k8temp.c +KALLSYMS LINEINFO +M: Sasha Levin <[email protected]> +S: Maintained +F: Documentation/admin-guide/kallsyms-lineinfo.rst +F: scripts/gen_lineinfo.c + KASAN M: Andrey Ryabinin <[email protected]> R: Alexander Potapenko <[email protected]>
include/linux/kallsyms.h +17 -1
@@ -16,10 +16,15 @@#include <asm/sections.h> #define KSYM_NAME_LEN 512 + +/* Extra space for " (path/to/file.c:12345)" suffix when lineinfo is enabled */ +#define KSYM_LINEINFO_LEN (IS_ENABLED(CONFIG_KALLSYMS_LINEINFO) ? 128 : 0) +#define KSYM_SYMBOL_LEN (sizeof("%s+%#lx/%#lx [%s %s]") + \ (KSYM_NAME_LEN - 1) + \ 2*(BITS_PER_LONG*3/10) + (MODULE_NAME_LEN - 1) + \ - (BUILD_ID_SIZE_MAX * 2) + 1) + (BUILD_ID_SIZE_MAX * 2) + 1 + \ + KSYM_LINEINFO_LEN)struct cred ;struct module ;
@@ -96,6 +101,9 @@ extern int sprint_backtrace_build_id(char *buffer, unsigned long address);int lookup_symbol_name (unsigned long addr,char *symname); +bool kallsyms_lookup_lineinfo (unsigned long addr,unsigned long sym_start, +const char **file,unsigned int *line); +#else /* !CONFIG_KALLSYMS */ static inline unsigned long kallsyms_lookup_name (const char *name)
@@ -164,6 +172,14 @@ static inline int kallsyms_on_each_match_symbol(int (*fn)(void *, unsigned long) {return -EOPNOTSUPP ; } + +static inline bool kallsyms_lookup_lineinfo (unsigned long addr, +unsigned long sym_start, +const char **file, +unsigned int *line) +{ +return false; +}#endif /*CONFIG_KALLSYMS*/ static inline void print_ip_sym (const char *loglvl,unsigned long ip)
init/Kconfig +20 -0
@@ -2087,6 +2087,26 @@ config KALLSYMS_ALL Say N unless you really need all symbols, or kernel live patching. +config KALLSYMS_LINEINFO + bool "Embed source file:line information in stack traces" + depends on KALLSYMS && DEBUG_INFO + help + Embeds an address-to-source-line mapping table in the kernel + image so that stack traces directly include file:line information, + similar to what scripts/decode_stacktrace.sh provides but without + needing external tools or a vmlinux with debug info at runtime. + + When enabled, stack traces will look like: + + kmem_cache_alloc_noprof+0x60/0x630 (mm/slub.c:3456) + anon_vma_clone+0x2ed/0xcf0 (mm/rmap.c:412) + + This requires elfutils (libdw-dev/elfutils-devel) on the build host. + Adds approximately 44MB to a typical kernel image (10 bytes per + DWARF line-table entry, ~4.6M entries for a typical config). + + If unsure, say N. + # end of the "standard kernel features (expert users)" menu config ARCH_HAS_MEMBARRIER_CALLBACKS
kernel/kallsyms.c +89 -7
149 lines not on this page · raw message
kernel/kallsyms_internal.h +9 -0
@@ -15,4 +15,13 @@ extern const u16 kallsyms_token_index[];extern const unsigned int kallsyms_markers[];extern const u8 kallsyms_seqs_of_names[]; +extern const u32 lineinfo_num_entries; +extern const u32 lineinfo_addrs[]; +extern const u16 lineinfo_file_ids[]; +extern const u32 lineinfo_lines[]; +extern const u32 lineinfo_num_files; +extern const u32 lineinfo_file_offsets[]; +extern const u32 lineinfo_filenames_size; +extern const char lineinfo_filenames[]; +#endif // LINUX_KALLSYMS_INTERNAL_H_
scripts/.gitignore +1 -0
@@ -1,5 +1,6 @@ # SPDX-License-Identifier: GPL-2.0-only /asn1_compiler +/gen_lineinfo /gen_packed_field_checks /generate_rust_target /insert-sys-cert
scripts/Makefile +3 -0
@@ -4,6 +4,7 @@ # the kernel for the build process. hostprogs-always-$(CONFIG_KALLSYMS) += kallsyms +hostprogs-always-$(CONFIG_KALLSYMS_LINEINFO) += gen_lineinfo hostprogs-always-$(BUILD_C_RECORDMCOUNT) += recordmcount hostprogs-always-$(CONFIG_BUILDTIME_TABLE_SORT) += sorttable hostprogs-always-$(CONFIG_ASN1) += asn1_compiler
@@ -37,6 +38,8 @@ HOSTCFLAGS_asn1_compiler.o = -I$(srctree)/include HOSTCFLAGS_sign-file.o = $(shell $(HOSTPKG_CONFIG) --cflags libcrypto 2> /dev/null) HOSTCFLAGS_sign-file.o += -I$(srctree)/tools/include/uapi/ HOSTLDLIBS_sign-file = $(shell $(HOSTPKG_CONFIG) --libs libcrypto 2> /dev/null || echo -lcrypto) +HOSTCFLAGS_gen_lineinfo.o = $(shell $(HOSTPKG_CONFIG) --cflags libdw 2> /dev/null) +HOSTLDLIBS_gen_lineinfo = $(shell $(HOSTPKG_CONFIG) --libs libdw 2> /dev/null || echo -ldw -lelf -lz) ifdef CONFIG_UNWINDER_ORC ifeq ($(ARCH),x86_64)
scripts/empty_lineinfo.S new file +30 -0
@@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (C) 2026 Sasha Levin <[email protected]> + * + * Empty lineinfo stub for the initial vmlinux link. + * The real lineinfo is generated from .tmp_vmlinux1 by gen_lineinfo. + */ + .section .rodata, "a" + .globl lineinfo_num_entries + .balign 4 +lineinfo_num_entries: + .long 0 + .globl lineinfo_num_files + .balign 4 +lineinfo_num_files: + .long 0 + .globl lineinfo_addrs +lineinfo_addrs: + .globl lineinfo_file_ids +lineinfo_file_ids: + .globl lineinfo_lines +lineinfo_lines: + .globl lineinfo_file_offsets +lineinfo_file_offsets: + .globl lineinfo_filenames_size + .balign 4 +lineinfo_filenames_size: + .long 0 + .globl lineinfo_filenames +lineinfo_filenames:
scripts/gen_lineinfo.c new file +557 -0
557 lines not on this page · raw message
scripts/kallsyms.c +16 -0
@@ -78,6 +78,17 @@ static char *sym_name(const struct sym_entry *s)static bool is_ignored_symbol (const char *name,char type) { +/* Ignore lineinfo symbols for kallsyms pass stability */ +static const char *const lineinfo_syms[] = { +"lineinfo_addrs" , +"lineinfo_file_ids" , +"lineinfo_file_offsets" , +"lineinfo_filenames" , +"lineinfo_lines" , +"lineinfo_num_entries" , +"lineinfo_num_files" , + }; +if (type =='u' || type =='n' )return true;
@@ -90,6 +101,11 @@ static bool is_ignored_symbol(const char *name, char type)return true; } +for (size_t i =0 ; i <ARRAY_SIZE (lineinfo_syms); i++) { +if (!strcmp (name, lineinfo_syms[i])) +return true; + } +return false; }
scripts/link-vmlinux.sh +40 -3
81 lines not on this page · raw message
Signature · 1 line
2.53.0
PATCH v7 2/4 kallsyms: extend lineinfo to loadable modules
In reply to Sasha LevinAdd CONFIG_KALLSYMS_LINEINFO_MODULES, which extends the CONFIG_KALLSYMS_LINEINFO feature to loadable kernel modules.
At build time, each .ko is post-processed by scripts/gen-mod-lineinfo.sh (modeled on gen-btf.sh) which runs scripts/gen_lineinfo --module on the .ko, generates per-section .mod_lineinfo and .init.mod_lineinfo sections containing compact binary tables of section-relative offsets, file IDs, line numbers, and filenames, and embeds them back into the .ko via a partial link (ld -r).
At runtime, module_lookup_lineinfo() walks the section descriptors in each blob, finds the one whose runtime range contains the queried address, and binary-searches that section's table. The lookup is NMI/panic-safe (no locks, no allocations) — the data lives in read-only module memory and is freed automatically when the module (or its init memory) is unloaded.
The gen_lineinfo tool gains --module mode which:
- Walks an allowlist of text-like sections (.text, .exit.text, .init.text), gating each on its presence in the .ko.
- Uses an ELF relocation against each covered section's symbol as the runtime "anchor", resolved by the module loader's standard apply_relocations() pass — no implicit base derivation from mod->mem[].base, no special-cased loader logic.
- Disambiguates DWARF addresses across sections that all share sh_addr == 0 in ET_REL files via per-section synthetic biases applied inside apply_debug_line_relocations() (handles both abs32 and abs64 width relocs).
- Handles libdw's ET_REL path-doubling quirk in make_relative().
- Declares empty section stanzas in its output assembly so the resulting lineinfo.o has LOCAL SECTION symbols rather than GLOBAL UND ones; otherwise ld -r would not bind the relocation to the .ko's existing section symbol of the same name and depmod would warn.
The build pipeline runs gen-mod-lineinfo.sh after the existing modfinal step:
gen_lineinfo --module ${KO} > ${KO}.lineinfo.S
${CC} -c -o ${KO}.lineinfo.o ${KO}.lineinfo.S
${LD} -r ${KO}.lineinfo.o ${KO} -o ${KO}.tmp && mv ${KO}.tmp ${KO}Order matters: lineinfo.o must come first so its zero-byte text contributions stay at offset 0 of the merged sections.
The init blob lives in MOD_INIT_RODATA and is revoked via WRITE_ONCE in do_init_module() before do_free_init() releases the memory; the module_init_lineinfo_data() reader uses READ_ONCE so concurrent lookups either see the old pointer (still valid until do_free_init's synchronize_rcu) or NULL.
The struct module fields are guarded by
#ifdef CONFIG_KALLSYMS_LINEINFO_MODULES and accessed through inline reader accessors so callers don't duplicate the guard.
Per-module overhead is approximately 14 bytes per DWARF line entry plus a small fixed cost per covered section descriptor. The next patch in this series delta-compresses the per-section streams to ~3-4 bytes per entry.
--- .../admin-guide/kallsyms-lineinfo.rst | 41 +- MAINTAINERS | 3 + include/linux/mod_lineinfo.h | 122 +++ include/linux/module.h | 40 + init/Kconfig | 19 +- kernel/kallsyms.c | 19 +- kernel/module/kallsyms.c | 198 ++++ kernel/module/main.c | 26 + scripts/Makefile.modfinal | 6 + scripts/gen-mod-lineinfo.sh | 50 + scripts/gen_lineinfo.c | 990 ++++++++++++++++-- 11 files changed, 1423 insertions(+), 91 deletions(-) create mode 100644 include/linux/mod_lineinfo.h create mode 100644 scripts/gen-mod-lineinfo.sh
Documentation/admin-guide/kallsyms-lineinfo.rst +33 -8
@@ -51,22 +51,47 @@ With ``CONFIG_KALLSYMS_LINEINFO``:: Note that assembly routines (such as ``entry_SYSCALL_64_after_hwframe``) are not annotated because they lack DWARF debug information. +Module Support +============== + +``CONFIG_KALLSYMS_LINEINFO_MODULES`` extends the feature to loadable kernel +modules. When enabled, each ``.ko`` is post-processed at build time to embed +a ``.mod_lineinfo`` section containing the same kind of address-to-source +mapping. + +Enable in addition to the base options:: + + CONFIG_MODULES=y + CONFIG_KALLSYMS_LINEINFO_MODULES=y + +Stack traces from module code will then include annotations:: + + my_driver_func+0x30/0x100 [my_driver] (drivers/foo/bar.c:123) + +The ``.mod_lineinfo`` section is loaded into read-only module memory alongside +the module text. No additional runtime memory allocation is required; the data +is freed when the module is unloaded. + Memory Overhead =============== -The lineinfo tables are stored in ``.rodata`` and typically add approximately -44 MiB to the kernel image for a standard configuration (~4.6 million DWARF -line entries, ~10 bytes per entry after deduplication). +The vmlinux lineinfo tables are stored in ``.rodata`` and typically add +approximately 10-15 MiB to the kernel image for a standard configuration +(~4.6 million DWARF line entries, ~2-3 bytes per entry after delta +compression). + +Per-module lineinfo adds approximately 2-3 bytes per DWARF line entry to each +``.ko`` file. Known Limitations ================= -- **vmlinux only**: Only symbols in the core kernel image are annotated. - Module symbols are not covered. -- **4 GiB offset limit**: Address offsets from ``_text`` are stored as 32-bit - values. Entries beyond 4 GiB from ``_text`` are skipped at build time with - a warning. +- **4 GiB offset limit**: Address offsets from ``_text`` (vmlinux) or + ``.text`` base (modules) are stored as 32-bit values. Entries beyond + 4 GiB are skipped at build time with a warning. - **65535 file limit**: Source file IDs are stored as 16-bit values. Builds with more than 65535 unique source files will fail with an error. - **No assembly annotations**: Functions implemented in assembly that lack DWARF ``.debug_line`` data are not annotated. +- **No init text**: For modules, functions in ``.init.text`` are not annotated + because that memory is freed after module initialization.
MAINTAINERS +3 -0
@@ -13935,6 +13935,9 @@ KALLSYMS LINEINFO M: Sasha Levin <[email protected]> S: Maintained F: Documentation/admin-guide/kallsyms-lineinfo.rst +F: include/linux/mod_lineinfo.h +F: lib/tests/lineinfo_kunit.c +F: scripts/gen-mod-lineinfo.sh F: scripts/gen_lineinfo.c KASAN
include/linux/mod_lineinfo.h new file +122 -0
122 lines not on this page · raw message
include/linux/module.h +40 -0
@@ -507,6 +507,12 @@ struct module {void *btf_data;void *btf_base_data;#endif +#ifdef CONFIG_KALLSYMS_LINEINFO_MODULES +void *lineinfo_data;/* .mod_lineinfo section in MOD_RODATA */ +unsigned int lineinfo_data_size; +void *init_lineinfo_data;/* .init.mod_lineinfo, NULL after init runs */ +unsigned int init_lineinfo_data_size; +#endif #ifdef CONFIG_JUMP_LABEL struct jump_entry *jump_entries;unsigned int num_jump_entries;
@@ -1020,6 +1026,40 @@ static inline unsigned long find_kallsyms_symbol_value(struct module *mod,#endif /* CONFIG_MODULES && CONFIG_KALLSYMS */ +bool module_lookup_lineinfo (struct module *mod,unsigned long addr, +unsigned long sym_start, +const char **file,unsigned int *line); + +/* +* Reader accessors so callers don't need to duplicate the +* CONFIG_KALLSYMS_LINEINFO_MODULES guard around mod->lineinfo_data / +* mod->init_lineinfo_data field access. Setters/clearers in the loader +* use the field directly under a matching #ifdef. +*/ +static inline void *module_lineinfo_data (const struct module *mod, +unsigned int *size) +{ +#ifdef CONFIG_KALLSYMS_LINEINFO_MODULES + *size = mod->lineinfo_data_size; +return mod->lineinfo_data; +#else + *size =0 ; +return NULL ; +#endif +} + +static inline void *module_init_lineinfo_data (const struct module *mod, +unsigned int *size) +{ +#ifdef CONFIG_KALLSYMS_LINEINFO_MODULES + *size =READ_ONCE (mod->init_lineinfo_data_size); +return READ_ONCE (mod->init_lineinfo_data); +#else + *size =0 ; +return NULL ; +#endif +} +/* Define __free(module_put) macro for struct module *. */ DEFINE_FREE(module_put, struct module *, if (_T) module_put(_T))
init/Kconfig +17 -2
@@ -2102,8 +2102,23 @@ config KALLSYMS_LINEINFO anon_vma_clone+0x2ed/0xcf0 (mm/rmap.c:412) This requires elfutils (libdw-dev/elfutils-devel) on the build host. - Adds approximately 44MB to a typical kernel image (10 bytes per - DWARF line-table entry, ~4.6M entries for a typical config). + Adds approximately 10-15MB to a typical kernel image (~2-3 bytes + per entry after delta compression, ~4.6M entries for a typical + config). + + If unsure, say N. + +config KALLSYMS_LINEINFO_MODULES + bool "Embed source file:line information in module stack traces" + depends on KALLSYMS_LINEINFO && MODULES + help + Extends KALLSYMS_LINEINFO to loadable kernel modules. Each .ko + gets a lineinfo table generated from its DWARF data at build time, + so stack traces from module code include (file.c:123) annotations. + + Requires elfutils (libdw-dev/elfutils-devel) on the build host. + Increases .ko sizes by approximately 2-3 bytes per DWARF line + entry after delta compression. If unsure, say N.
kernel/kallsyms.c +16 -3
@@ -569,12 +569,25 @@ static int __sprint_symbol(char *buffer, unsigned long address,* replaced with bar()"); appending lineinfo there would produce a * confusing "foo (file:line)()". */ -if (add_lineinfo &&IS_ENABLED (CONFIG_KALLSYMS_LINEINFO ) && !modname) { +if (add_lineinfo &&IS_ENABLED (CONFIG_KALLSYMS_LINEINFO )) {const char *li_file;unsigned int li_line; +bool found = false; + +if (!modname) { + found =kallsyms_lookup_lineinfo (address, sym_start, + &li_file, &li_line); + }else if (IS_ENABLED (CONFIG_KALLSYMS_LINEINFO_MODULES )) { +struct module *mod =__module_address (address); + +if (mod) + found =module_lookup_lineinfo (mod, address, + sym_start, + &li_file, + &li_line); + } -if (kallsyms_lookup_lineinfo (address, sym_start, - &li_file, &li_line)) +if (found) len +=snprintf (buffer + len,KSYM_SYMBOL_LEN - len," (%s:%u)" , li_file, li_line); }
kernel/module/kallsyms.c +198 -0
201 lines not on this page · raw message
kernel/module/main.c +26 -0
@@ -2712,6 +2712,19 @@ static int find_module_sections(struct module *mod, struct load_info *info) mod->btf_base_data =any_section_objs (info,".BTF.base" ,1 , &mod->btf_base_data_size);#endif +#ifdef CONFIG_KALLSYMS_LINEINFO_MODULES +/* +* Use section_objs() (not any_section_objs) — both blobs carry an +* ELF anchor relocation that the module loader resolves via its +* standard apply_relocations() pass, which only walks SHF_ALLOC +* sections. Picking up a non-ALLOC section here would also leave +* the pointer dangling into the temporary load image once freed. +*/ + mod->lineinfo_data =section_objs (info,".mod_lineinfo" ,1 , + &mod->lineinfo_data_size); + mod->init_lineinfo_data =section_objs (info,".init.mod_lineinfo" ,1 , + &mod->init_lineinfo_data_size); +#endif #ifdef CONFIG_JUMP_LABEL mod->jump_entries = section_objs(info, "__jump_table", sizeof(*mod->jump_entries),
@@ -3165,6 +3178,19 @@ static noinline int do_init_module(struct module *mod)/* .BTF is not SHF_ALLOC and will get removed, so sanitize pointers */ mod->btf_data =NULL ; mod->btf_base_data =NULL ; +#endif +#ifdef CONFIG_KALLSYMS_LINEINFO_MODULES +/* +* .init.mod_lineinfo lives in MOD_INIT_RODATA which do_free_init() is +* about to release. Clear the pointer so concurrent stack-trace +* lookups stop dereferencing it; do_free_init()'s synchronize_rcu() +* then waits out any reader that already captured the old pointer. +* WRITE_ONCE pairs with the READ_ONCE inside module_init_lineinfo_data() +* so the compiler can't tear or reorder the revocation across the +* llist_add() that follows. +*/ +WRITE_ONCE (mod->init_lineinfo_data,NULL ); +WRITE_ONCE (mod->init_lineinfo_data_size,0 );#endif /* * We want to free module_init, but be aware that kallsyms may be
scripts/Makefile.modfinal +6 -0
@@ -46,11 +46,17 @@ quiet_cmd_btf_ko = BTF [M] $@ $(CONFIG_SHELL) $(srctree)/scripts/gen-btf.sh --btf_base $(objtree)/vmlinux $@; \ fi; +quiet_cmd_lineinfo_ko = LINEINFO [M] $@ + cmd_lineinfo_ko = $(CONFIG_SHELL) $(srctree)/scripts/gen-mod-lineinfo.sh $@ + # Re-generate module BTFs if either module's .ko or vmlinux changed %.ko: %.o %.mod.o .module-common.o $(objtree)/scripts/module.lds $(and $(CONFIG_DEBUG_INFO_BTF_MODULES),$(KBUILD_BUILTIN),$(objtree)/vmlinux) FORCE +$(call if_changed,ld_ko_o) ifdef CONFIG_DEBUG_INFO_BTF_MODULES +$(if $(newer-prereqs),$(call cmd,btf_ko)) +endif +ifdef CONFIG_KALLSYMS_LINEINFO_MODULES + +$(if $(newer-prereqs),$(call cmd,lineinfo_ko)) endif +$(call cmd,check_tracepoint)
scripts/gen-mod-lineinfo.sh new file +50 -0
50 lines not on this page · raw message
scripts/gen_lineinfo.c +912 -78
1,148 lines not on this page · raw message
Signature · 1 line
2.53.0
PATCH v7 3/4 kallsyms: delta-compress lineinfo tables for ~2.7x size reduction
In reply to Sasha LevinReplace the flat uncompressed parallel arrays (lineinfo_addrs[], lineinfo_file_ids[], lineinfo_lines[]) with a block-indexed, delta-encoded, ULEB128 varint compressed format.
The sorted address array has small deltas between consecutive entries (typically 1-50 bytes), file IDs have high locality (delta often 0, same file), and line numbers change slowly. Delta-encoding followed by ULEB128 varint compression shrinks most values from 4 bytes to 1.
Entries are grouped into blocks of 64. A small uncompressed block index (first addr + byte offset per block) enables O(log(N/64)) binary search, followed by sequential decode of at most 64 varints within the matching block. All decode state lives on the stack -- zero allocations, still safe for NMI/panic context.
Measured on a defconfig+debug x86_64 build (3,017,154 entries, 4,822 source files, 47,144 blocks):
Before (flat arrays):
lineinfo_addrs[] 12,068,616 bytes (u32 x 3.0M)
lineinfo_file_ids[] 6,034,308 bytes (u16 x 3.0M)
lineinfo_lines[] 12,068,616 bytes (u32 x 3.0M)
Total: 30,171,540 bytes (28.8 MiB, 10.0 bytes/entry)After (block-indexed delta + ULEB128):
lineinfo_block_addrs[] 188,576 bytes (184 KiB)
lineinfo_block_offsets[] 188,576 bytes (184 KiB)
lineinfo_data[] 10,926,128 bytes (10.4 MiB)
Total: 11,303,280 bytes (10.8 MiB, 3.7 bytes/entry)Savings: 18.0 MiB (2.7x reduction)
Booted in QEMU and verified with SysRq-l that annotations still work:
default_idle+0x9/0x10 (arch/x86/kernel/process.c:767) default_idle_call+0x6c/0xb0 (kernel/sched/idle.c:122) do_idle+0x335/0x490 (kernel/sched/idle.c:191) cpu_startup_entry+0x4e/0x60 (kernel/sched/idle.c:429) rest_init+0x1aa/0x1b0 (init/main.c:760)
--- include/linux/mod_lineinfo.h | 250 ++++++++++++++++++++++++++++++----- kernel/kallsyms.c | 55 +++----- kernel/kallsyms_internal.h | 8 +- kernel/module/kallsyms.c | 110 +++++++-------- scripts/empty_lineinfo.S | 20 ++- scripts/gen_lineinfo.c | 185 +++++++++++++++++--------- scripts/kallsyms.c | 7 +- 7 files changed, 436 insertions(+), 199 deletions(-)
include/linux/mod_lineinfo.h +220 -30
@@ -3,9 +3,9 @@* mod_lineinfo.h - Binary format for per-module source line information * * This header defines the layout of the .mod_lineinfo and -* .init.mod_lineinfo sections embedded in loadable kernel modules. It -* is dual-use: included from both the kernel and the userspace -* gen_lineinfo tool. +* .init.mod_lineinfo sections embedded in loadable kernel modules. It is +* dual-use: included from both the kernel and the userspace gen_lineinfo +* tool. * * Top-level layout (all values in target-native endianness): *
@@ -20,16 +20,27 @@* If the relocation fails to resolve (e.g. unknown reloc type), .anchor * stays zero and lookups silently degrade to "no annotation". * -* Each per-section sub-table is laid out as a stand-alone -* mod_lineinfo_header followed by parallel arrays: +* Each per-section sub-table is laid out exactly as a stand-alone +* mod_lineinfo_header followed by its arrays: * -* struct mod_lineinfo_header (16 bytes) -* u32 addrs[num_entries] -- offsets from this section's base, sorted -* u16 file_ids[num_entries] -- parallel to addrs -* <2-byte pad if num_entries is odd> -* u32 lines[num_entries] -- parallel to addrs +* struct mod_lineinfo_header +* u32 block_addrs[num_blocks] -- first addr per block, for binary search +* u32 block_offsets[num_blocks] -- byte offset into compressed data stream +* u8 data[data_size] -- LEB128 delta-compressed entries * u32 file_offsets[num_files] -- byte offset into filenames[] * char filenames[filenames_size] -- concatenated NUL-terminated strings +* +* Each sub-array is located by an explicit (offset, size) pair in the +* header, similar to a flattened devicetree. All offsets in the per-section +* header are relative to that header itself, so a sub-table is fully +* self-describing. +* +* Compressed stream format (per block of LINEINFO_BLOCK_ENTRIES entries): +* Entry 0: file_id (ULEB128), line (ULEB128) +* addr is in block_addrs[] +* Entry 1..N: addr_delta (ULEB128), +* file_id_delta (SLEB128), +* line_delta (SLEB128) */ #ifndef _LINUX_MOD_LINEINFO_H#define _LINUX_MOD_LINEINFO_H
@@ -44,6 +55,7 @@#include <stdint.h> typedef uint32_t u32 ;typedef uint16_t u16 ; +typedef uint8_t u8 ;typedef uint64_t u64 ;#ifndef __aligned#define __aligned (x) __attribute__((__aligned__(x)))
@@ -53,6 +65,8 @@ typedef uint64_t u64;#endif #endif +#define LINEINFO_BLOCK_ENTRIES 64 +/* * Per-section descriptor. One entry per ELF text section covered by the * blob (.text, .exit.text, .init.text, ...).
@@ -84,39 +98,215 @@ static_assert(sizeof(struct mod_lineinfo_section) == 16,struct mod_lineinfo_header {u32 num_entries; +u32 num_blocks;u32 num_files; -u32 filenames_size;/* total bytes of concatenated filenames */ +u32 blocks_offset;/* offset to block_addrs[] from this header */ +u32 blocks_size;/* bytes: num_blocks * 2 * sizeof(u32) */ +u32 data_offset;/* offset to compressed stream */ +u32 data_size;/* bytes of compressed data */ +u32 files_offset;/* offset to file_offsets[] */ +u32 files_size;/* bytes: num_files * sizeof(u32) */ +u32 filenames_offset; +u32 filenames_size; }; -/* Offset helpers: compute byte offset from the per-section header to each array. */ +/* +* Descriptor for a lineinfo table, used by the shared lookup function. +* Callers populate this from either linker globals (vmlinux) or a +* validated mod_lineinfo_header (modules). +*/ +struct lineinfo_table { +const u32 *blk_addrs; +const u32 *blk_offsets; +const u8 *data; +u32 data_size; +const u32 *file_offsets; +const char *filenames; +u32 num_entries; +u32 num_blocks; +u32 num_files; +u32 filenames_size; +}; -static inline u32 mod_lineinfo_addrs_off (void ) +/* +* Read a ULEB128 varint from a byte stream. +* Returns the decoded value and advances *pos past the encoded bytes. +* If *pos would exceed 'end', returns 0 and sets *pos = end (safe for +* NMI/panic context: no crash, just a missed annotation). +*/ +static inline u32 lineinfo_read_uleb128 (const u8 *data,u32 *pos,u32 end) { -return sizeof (struct mod_lineinfo_header ); -} +u32 result =0 ; +unsigned int shift =0 ; -static inline u32 mod_lineinfo_file_ids_off (u32 num_entries) -{ -return mod_lineinfo_addrs_off () + num_entries *sizeof (u32); +while (*pos < end) { +u8 byte = data[*pos]; + (*pos)++; + result |= (u32)(byte &0x7f ) << shift; +if (!(byte &0x80 )) +return result; + shift +=7 ; +if (shift >=32 ) { +/* Malformed: skip remaining continuation bytes */ +while (*pos < end && (data[*pos] &0x80 )) + (*pos)++; +if (*pos < end) + (*pos)++; +return result; + } + } +return result; } -static inline u32 mod_lineinfo_lines_off (u32 num_entries) +/* Read an SLEB128 varint. Same safety guarantees as above. */ +static inline int32_t lineinfo_read_sleb128 (const u8 *data,u32 *pos,u32 end) { -/* u16 file_ids[] may need 2-byte padding to align lines[] to 4 bytes */ -u32 off =mod_lineinfo_file_ids_off (num_entries) + - num_entries *sizeof (u16); -return (off +3 ) & ~3u ; -} +int32_t result =0 ; +unsigned int shift =0 ; +u8 byte =0 ; -static inline u32 mod_lineinfo_file_offsets_off (u32 num_entries) -{ -return mod_lineinfo_lines_off (num_entries) + num_entries *sizeof (u32); +while (*pos < end) { + byte = data[*pos]; + (*pos)++; + result |= (int32_t )((u32)(byte &0x7f ) << shift); + shift +=7 ; +if (!(byte &0x80 )) +break ; +if (shift >=32 ) { +while (*pos < end && (data[*pos] &0x80 )) + (*pos)++; +if (*pos < end) + (*pos)++; +return result; + } + } + +/* Sign-extend if the high bit of the last byte was set */ +if (shift <32 && (byte &0x40 )) + result |= -(1 << shift); + +return result; } -static inline u32 mod_lineinfo_filenames_off (u32 num_entries,u32 num_files) +/* +* Search a lineinfo table for the source file and line corresponding to a +* given offset (from _text for vmlinux, from the covered section's base for +* modules). @min_offset is the containing symbol's start in the same offset +* space: entries below it belong to a preceding symbol and are rejected. +* +* Safe for NMI and panic context: no locks, no allocations, all state on stack. +* Returns true and sets @file and @line on success; false on any failure. +*/ +static inline bool lineinfo_search (const struct lineinfo_table *tbl, +unsigned int offset, +unsigned int min_offset, +const char **file,unsigned int *line) { -return mod_lineinfo_file_offsets_off (num_entries) + - num_files *sizeof (u32); +unsigned int low, high, mid, block; +unsigned int cur_addr, cur_file_id, cur_line; +unsigned int best_addr =0 , best_file_id =0 , best_line =0 ; +unsigned int block_entries, data_end; +bool found = false; +u32 pos; + +if (!tbl->num_entries || !tbl->num_blocks) +return false; + +/* Binary search on blk_addrs[] to find the right block */ + low =0 ; + high = tbl->num_blocks; +while (low < high) { + mid = low + (high - low) /2 ; +if (tbl->blk_addrs[mid] <= offset) + low = mid +1 ; +else + high = mid; + } + +if (low ==0 ) +return false; + block = low -1 ; + +/* How many entries in this block? */ + block_entries =LINEINFO_BLOCK_ENTRIES ; +if (block == tbl->num_blocks -1 ) { +unsigned int remaining = tbl->num_entries - + block *LINEINFO_BLOCK_ENTRIES ; + +if (remaining < block_entries) + block_entries = remaining; + } + +/* Determine end of this block's data in the compressed stream */ +if (block +1 < tbl->num_blocks) + data_end = tbl->blk_offsets[block +1 ]; +else + data_end = tbl->data_size; + +/* Clamp data_end to actual data size */ +if (data_end > tbl->data_size) + data_end = tbl->data_size; + +/* Decode entry 0: addr from blk_addrs, file_id and line from stream */ + pos = tbl->blk_offsets[block]; +if (pos >= data_end) +return false; + + cur_addr = tbl->blk_addrs[block]; + cur_file_id =lineinfo_read_uleb128 (tbl->data, &pos, data_end); + cur_line =lineinfo_read_uleb128 (tbl->data, &pos, data_end); + +/* Check entry 0 */ +if (cur_addr <= offset) { + best_addr = cur_addr; + best_file_id = cur_file_id; + best_line = cur_line; + found = true; + } + +/* Decode entries 1..N */ +for (unsigned int i =1 ; i < block_entries; i++) { +unsigned int addr_delta; +int32_t file_delta, line_delta; + + addr_delta =lineinfo_read_uleb128 (tbl->data, &pos, data_end); + file_delta =lineinfo_read_sleb128 (tbl->data, &pos, data_end); + line_delta =lineinfo_read_sleb128 (tbl->data, &pos, data_end); + + cur_addr += addr_delta; + cur_file_id = (unsigned int )((int32_t )cur_file_id + file_delta); + cur_line = (unsigned int )((int32_t )cur_line + line_delta); + +if (cur_addr > offset) +break ; + + best_addr = cur_addr; + best_file_id = cur_file_id; + best_line = cur_line; + found = true; + } + +if (!found) +return false; + +/* +* The best entry is the closest one at or below @offset; reject it +* if it lies below the resolved symbol's start, so a symbol without +* line entries of its own does not inherit the preceding symbol's +* annotation. +*/ +if (best_addr < min_offset) +return false; + +if (best_file_id >= tbl->num_files) +return false; + +if (tbl->file_offsets[best_file_id] >= tbl->filenames_size) +return false; + + *file = &tbl->filenames[tbl->file_offsets[best_file_id]]; + *line = best_line; +return true; }#endif /* _LINUX_MOD_LINEINFO_H */
kernel/kallsyms.c +21 -34
80 lines not on this page · raw message
kernel/kallsyms_internal.h +5 -3
@@ -16,10 +16,12 @@ extern const unsigned int kallsyms_markers[];extern const u8 kallsyms_seqs_of_names[];extern const u32 lineinfo_num_entries; -extern const u32 lineinfo_addrs[]; -extern const u16 lineinfo_file_ids[]; -extern const u32 lineinfo_lines[];extern const u32 lineinfo_num_files; +extern const u32 lineinfo_num_blocks; +extern const u32 lineinfo_block_addrs[]; +extern const u32 lineinfo_block_offsets[]; +extern const u32 lineinfo_data_size; +extern const u8 lineinfo_data[];extern const u32 lineinfo_file_offsets[];extern const u32 lineinfo_filenames_size;extern const char lineinfo_filenames[];
kernel/module/kallsyms.c +46 -64
160 lines not on this page · raw message
scripts/empty_lineinfo.S +14 -6
@@ -14,12 +14,20 @@ lineinfo_num_entries: .balign 4 lineinfo_num_files: .long 0 - .globl lineinfo_addrs -lineinfo_addrs: - .globl lineinfo_file_ids -lineinfo_file_ids: - .globl lineinfo_lines -lineinfo_lines: + .globl lineinfo_num_blocks + .balign 4 +lineinfo_num_blocks: + .long 0 + .globl lineinfo_block_addrs +lineinfo_block_addrs: + .globl lineinfo_block_offsets +lineinfo_block_offsets: + .globl lineinfo_data_size + .balign 4 +lineinfo_data_size: + .long 0 + .globl lineinfo_data +lineinfo_data: .globl lineinfo_file_offsets lineinfo_file_offsets: .globl lineinfo_filenames_size
scripts/gen_lineinfo.c +126 -59
246 lines not on this page · raw message
scripts/kallsyms.c +4 -3
@@ -80,11 +80,12 @@ static bool is_ignored_symbol(const char *name, char type) {/* Ignore lineinfo symbols for kallsyms pass stability */ static const char *const lineinfo_syms[] = { -"lineinfo_addrs" , -"lineinfo_file_ids" , +"lineinfo_block_addrs" , +"lineinfo_block_offsets" , +"lineinfo_data" ,"lineinfo_file_offsets" ,"lineinfo_filenames" , -"lineinfo_lines" , +"lineinfo_num_blocks" ,"lineinfo_num_entries" ,"lineinfo_num_files" , };
Signature · 1 line
2.53.0
PATCH v7 4/4 kallsyms: add KUnit tests for lineinfo feature
In reply to Sasha LevinAdd a KUnit test module (CONFIG_LINEINFO_KUNIT_TEST) that verifies the kallsyms lineinfo feature produces correct source file:line annotations in stack traces.
Export sprint_backtrace() and sprint_backtrace_build_id() as GPL symbols so the test module can exercise the backtrace APIs.
--- kernel/kallsyms.c | 2 + lib/Kconfig.debug | 10 + lib/tests/Makefile | 3 + lib/tests/lineinfo_kunit.c | 973 +++++++++++++++++++++++++++++++++++++ 4 files changed, 988 insertions(+) create mode 100644 lib/tests/lineinfo_kunit.c
kernel/kallsyms.c +2 -0
@@ -651,6 +651,7 @@ int sprint_backtrace(char *buffer, unsigned long address) {return __sprint_symbol (buffer, address,-1 ,1 ,0 ,1 ); } +EXPORT_SYMBOL_GPL (sprint_backtrace);/** * sprint_backtrace_build_id - Look up a backtrace symbol and return it in a text buffer
@@ -671,6 +672,7 @@ int sprint_backtrace_build_id(char *buffer, unsigned long address) {return __sprint_symbol (buffer, address,-1 ,1 ,1 ,1 ); } +EXPORT_SYMBOL_GPL (sprint_backtrace_build_id);/* To avoid using get_symbol_offset for every symbol, we carry prefix along. */ struct kallsym_iter {
lib/Kconfig.debug +10 -0
@@ -3092,6 +3092,16 @@ config LONGEST_SYM_KUNIT_TEST If unsure, say N. +config LINEINFO_KUNIT_TEST + tristate "KUnit tests for kallsyms lineinfo" if !KUNIT_ALL_TESTS + depends on KUNIT && KALLSYMS_LINEINFO + default KUNIT_ALL_TESTS + help + KUnit tests for the kallsyms source line info feature. + Verifies that stack traces include correct (file.c:line) annotations. + + If unsure, say N. + config HW_BREAKPOINT_KUNIT_TEST bool "Test hw_breakpoint constraints accounting" if !KUNIT_ALL_TESTS depends on HAVE_HW_BREAKPOINT
lib/tests/Makefile +3 -0
@@ -37,6 +37,9 @@ obj-$(CONFIG_LIVEUPDATE_TEST) += liveupdate.o CFLAGS_longest_symbol_kunit.o += $(call cc-disable-warning, missing-prototypes) obj-$(CONFIG_LONGEST_SYM_KUNIT_TEST) += longest_symbol_kunit.o +CFLAGS_lineinfo_kunit.o += $(call cc-option,-fno-inline-functions-called-once) +obj-$(CONFIG_LINEINFO_KUNIT_TEST) += lineinfo_kunit.o + obj-$(CONFIG_MEMCPY_KUNIT_TEST) += memcpy_kunit.o obj-$(CONFIG_MIN_HEAP_KUNIT_TEST) += min_heap_kunit.o CFLAGS_overflow_kunit.o = $(call cc-disable-warning, tautological-constant-out-of-range-compare)
lib/tests/lineinfo_kunit.c new file +973 -0
973 lines not on this page · raw message
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v7 2/4] kallsyms: extend lineinfo to loadable modules
In reply to Sasha LevinThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] Unvalidated module blob offsets cause unaligned memory accesses, potentially triggering alignment…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Unvalidated module blob offsets cause unaligned memory accesses, potentially triggering alignment faults in critical contexts.
- [Medium] Integer overflow in the apply_one_dl_reloc bounds check allows out-of-bounds heap access in the gen_lineinfo tool.
- [Low] Dynamically allocated memory for extra_sections is never freed in the gen_lineinfo tool.
--
--- Patch [2]: [PATCH v7 2/4] kallsyms: extend lineinfo to loadable modules --- commit be00704ef1eba0bdfe61160ba94617348dfdc7dc
Author: Sasha Levin <[email protected]>
kallsyms: extend lineinfo to loadable modules
This patch extends the CONFIG_KALLSYMS_LINEINFO feature to loadable kernel modules. It uses scripts/gen-mod-lineinfo.sh to generate .mod_lineinfo sections containing binary tables of source file and line number mappings. At runtime, module_lookup_lineinfo() searches these read-only tables to provide NMI-safe file and line annotations for module stack traces.
Quoted 4 lines
kernel/module/kallsyms.c +0
-0
[ ... ]
Quoted 13 lines
kernel/module/kallsyms.c +12
-0@@ -494,3 +494,201 @@ int module_kallsyms_on_each_symbol(const char *modname, +static bool module_lookup_lineinfo_section (const void *blob,u32 blob_size, +u32 hdr_offset, +unsigned int section_offset, +unsigned int min_offset, +const char **file, +unsigned int *line) +{ +const struct mod_lineinfo_header *hdr; +const u8 *base; +const u32 *addrs, *lines, *file_offsets; +const u16 *file_ids; +const char *filenames;
kernel/module/kallsyms.c int module_kallsyms_on_each_symbol(const char *modname, · line 515
Quoted 10 lines
kernel/module/kallsyms.c +10
-0+u32 num_entries, num_files, filenames_size; +unsigned int low, high, mid; +u16 file_id; + +if (hdr_offset > blob_size || + blob_size - hdr_offset <sizeof (*hdr)) +return false; + + base = (const u8 *)blob + hdr_offset; + hdr = (const struct mod_lineinfo_header *)base;
[Severity: High]
Does this code safely handle unaligned memory accesses?
The hdr_offset value is read directly from the binary blob and is never validated for 4-byte alignment before being cast to struct mod_lineinfo_header * and subsequently u32 * for the internal arrays.
If a malformed or corrupted .ko file provides an unaligned hdr_offset, dereferencing these pointers could trigger a hardware alignment trap on strict-alignment architectures. Since this code is designed to be called during NMI or panic stack traces, taking an alignment fault here would cause a recursive fault and hang the system without producing the crash dump.
Could we add an alignment check for hdr_offset before casting it?
[ ... ]
Quoted 4 lines
scripts/gen_lineinfo.c +0
-0
[ ... ]
Quoted 12 lines
scripts/gen_lineinfo.c +12
-0+static void apply_one_dl_reloc (Elf_Data *dl_data,Elf_Data *sym_data, +bool target_le, +unsigned int abs32_type,unsigned int abs64_type, +GElf_Xword r_info,GElf_Addr r_offset, +GElf_Sxword addend,bool has_addend) +{ +GElf_Sym sym; +unsigned int r_type =GELF_R_TYPE (r_info); +size_t r_sym =GELF_R_SYM (r_info); +bool is_abs64; +size_t width; +uint64_t value;
[ ... ]
scripts/gen_lineinfo.c static unsigned long long find_text_addr(Elf *elf) · line 631
Quoted 4 lines
scripts/gen_lineinfo.c +4
-0+ width = is_abs64 ?8 :4 ; + +if (r_offset + width > dl_data->d_size) +return ;
[Severity: Medium]
Can this bounds check overflow?
Because r_offset is a 64-bit integer, a maliciously crafted .ko file fed to the gen_lineinfo tool could provide an extremely large value (such as 0xFFFFFFFFFFFFFFFE). Adding width to it would wrap around to a small number, successfully bypassing the bounds check.
This could lead to out-of-bounds pointer arithmetic and a subsequent out-of-bounds write in elf_write_uint() below, potentially causing a crash or code execution within the kernel build environment.
[ ... ]
Quoted 4 lines
scripts/gen_lineinfo.c +4
-0+static void resolve_covered_sections (Elf *elf, +struct covered_section *sections, +unsigned int num_sections) +{
[ ... ]
Quoted 8 lines
scripts/gen_lineinfo.c +8
-0+if (!covered && + (shdr.sh_flags &SHF_EXECINSTR ) && (shdr.sh_flags &SHF_ALLOC ) && + shdr.sh_size && shdr.sh_size <=UINT_MAX ) { +struct covered_section *es; + + extra_sections =realloc (extra_sections, + (num_extra_sections +1 ) * +sizeof (*extra_sections));
[ ... ]
scripts/gen_lineinfo.c int main(int argc, char *argv[]) · line 1391
Quoted 7 lines
scripts/gen_lineinfo.c +0
-1@@ -552,6 +1387,5 @@ int main(int argc, char *argv[])for (unsigned int i =0 ; i < num_files; i++)free (files[i].name);free (files); -return 0 ; }
[Severity: Low]
Does this code leak the dynamically allocated extra_sections array?
In resolve_covered_sections(), extra_sections is allocated and expanded via realloc(). However, the cleanup block in main() only frees entries and files, missing the extra_sections array.
While this is a short-lived userspace tool and the OS will automatically reclaim the memory upon exit, would it be better to explicitly free extra_sections to keep the cleanup complete?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewRe: [PATCH v7 4/4] kallsyms: add KUnit tests for lineinfo feature
In reply to Sasha LevinThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Unchecked NULL pointer dereference when allocating test symbol buffer. --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Unchecked NULL pointer dereference when allocating test symbol buffer. --
--- Patch [4]: [PATCH v7 4/4] kallsyms: add KUnit tests for lineinfo feature --- commit aa7a75a3dcd2a5f7456eb96f884496c36509f6d7
Author: Sasha Levin <[email protected]>
kallsyms: add KUnit tests for lineinfo feature
This commit adds a KUnit test module to verify that the kallsyms lineinfo feature produces correct annotations in stack traces. It also exports sprint_backtrace() and sprint_backtrace_build_id() so the test module can exercise the backtrace APIs.
Quoted 5 lines
lib/tests/lineinfo_kunit.c new file +0
-0
[ ... ]
Quoted 4 lines
lib/tests/lineinfo_kunit.c new file +4
-0+static char *alloc_sym_buf (struct kunit *test) +{ +return kunit_kzalloc (test,KSYM_SYMBOL_LEN ,GFP_KERNEL ); +}
[ ... ]
lib/tests/lineinfo_kunit.c line 219
Quoted 8 lines
lib/tests/lineinfo_kunit.c new file +8
-0+static void test_normal_function (struct kunit *test) +{ +char *buf =alloc_sym_buf (test); +unsigned long addr =target_addr (lineinfo_target_normal); + +skip_without_module_lineinfo (test); + +sprint_with_lineinfo (buf, addr);
[Severity: Medium]
Will this crash the kernel if kunit_kzalloc() fails and returns NULL?
If memory allocation fails under pressure, alloc_sym_buf() returns NULL. The callers like test_normal_function() pass this buffer unchecked into sprint_with_lineinfo(), which eventually passes it to kallsyms_lookup_buildid() as the namebuf parameter.
Looking at kallsyms_lookup_buildid() in kernel/kallsyms.c, the buffer is dereferenced immediately:
kernel/kallsyms.c:kallsyms_lookup_buildid() {
...
namebuf[0] = 0;
...
}Should alloc_sym_buf() use KUNIT_ASSERT_NOT_NULL(test, buf) to gracefully fail the test instead of causing a NULL pointer dereference?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] Use of `snprintf` without checking for truncation leaks uninitialized kernel memory via trace…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] Use of snprintf without checking for truncation leaks uninitialized kernel memory via trace buffers.
- [Medium] The host tool scripts/gen_lineinfo will enter an infinite loop during the kernel build if the number of unique source files exceeds 8,192.
- [Medium] Deduplication logic drops valid lineinfo entries with different offsets, breaking symbol boundary checks.
- [Low] The symbol lineinfo_filenames_size is missing from the kallsyms ignore list, exposing it while hiding its siblings. --
--- Patch [1]: [PATCH v7 1/4] kallsyms: embed source file:line info in kernel stack traces --- commit ca3ea968a16862e3d8b4586ef0bfc722a4a2f652
Author: Sasha Levin <[email protected]>
kallsyms: embed source file:line info in kernel stack traces
This commit adds a configuration option to embed a compact address-to-line lookup table in the kernel image. It generates this table at build time from DWARF debug information using a host tool. Stack traces can then directly print source file and line number information without external tools.
Quoted 4 lines
kernel/kallsyms.c +0
-0
[ ... ]
Quoted 7 lines
kernel/kallsyms.c +3
-0@@ -467,13 +467,77 @@ static int append_buildid(char *buffer, const char *modname,#endif /* CONFIG_STACKTRACE_BUILD_ID */ +bool kallsyms_lookup_lineinfo (unsigned long addr,unsigned long sym_start, +const char **file,unsigned int *line) +{
[ ... ]
kernel/kallsyms.c static int append_buildid(char *buffer, const char *modname, · line 518
Quoted 6 lines
kernel/kallsyms.c +6
-0+if (low ==0 ) +return false; + low--; + +if (lineinfo_addrs[low] < min_offset) +return false;
[Severity: Medium]
Will this boundary check unintentionally reject valid entries for functions that start on the same source line (e.g. via macro instantiation)?
When deduplicate() in scripts/gen_lineinfo.c processes entries, it drops the second function's entry because it shares the same file and line. When querying the second function, kallsyms_lookup_lineinfo() finds the first function's entry but rejects it here since its offset is earlier than the second function's sym_start.
Quoted 2 lines
kernel/kallsyms.c +2
-0+ + file_id = lineinfo_file_ids[low];
[ ... ]
Quoted 9 lines
kernel/kallsyms.c +5
-0@@ -497,6 +562,23 @@ static int __sprint_symbol(char *buffer, unsigned long address, len +=sprintf (buffer + len,"]" ); } +/* +* Append "(file:line)" only for stack-backtrace consumers. Plain +* sprint_symbol() backs %ps, and many existing format strings tack +* literal "()" after %ps to indicate a function call ("foo() +* replaced with bar()"); appending lineinfo there would produce a
kernel/kallsyms.c static int __sprint_symbol(char *buffer, unsigned long address, · line 579
Quoted 10 lines
kernel/kallsyms.c +10
-0+* confusing "foo (file:line)()". +*/ +if (add_lineinfo &&IS_ENABLED (CONFIG_KALLSYMS_LINEINFO ) && !modname) { +const char *li_file; +unsigned int li_line; + +if (kallsyms_lookup_lineinfo (address, sym_start, + &li_file, &li_line)) + len +=snprintf (buffer + len,KSYM_SYMBOL_LEN - len, +" (%s:%u)" , li_file, li_line);
[Severity: High]
Does this unconditionally add snprintf()'s return value to the string length?
If the formatted string is truncated, snprintf() returns the length it would have written. This makes len larger than the actual bytes written to the buffer.
Can this cause callers (like fetch_store_sym() in the trace framework) to expose uninitialized kernel memory to userspace by copying the unwritten portion of the buffer?
Quoted 10 lines
kernel/kallsyms.c +2
-0+ } +return len; }scripts/gen_lineinfo.c new file +0
-0
[ ... ]
scripts/gen_lineinfo.c line 100
Quoted 10 lines
scripts/gen_lineinfo.c new file +10
-0+static unsigned int find_or_add_file (const char *name) +{ +unsigned int h =hash_str (name); + +/* Open-addressing lookup with linear probing */ +while (file_hash[h].name) { +if (!strcmp (file_hash[h].name, name)) +return file_hash[h].id; + h = (h +1 ) & (FILE_HASH_SIZE -1 ); + }
[Severity: Medium]
Will this open-addressing lookup loop indefinitely if the hash table is fully saturated?
If a kernel configuration (like allyesconfig) includes 8192 or more unique source files, the static FILE_HASH_SIZE array becomes completely full. Any subsequent file lookup will probe all slots without finding a NULL entry and loop infinitely.
Quoted 2 lines
scripts/gen_lineinfo.c new file +2
-0+ +if (num_files >=65535 ) {
[ ... ]
Quoted 13 lines
scripts/gen_lineinfo.c new file +13
-0+static void deduplicate (void ) +{ +unsigned int i, j; + +if (num_entries <2 ) +return ; + +/* Sort by offset, then file_id, then line for stability */ +qsort (entries, num_entries,sizeof (*entries), compare_entries); + +/* +* Remove duplicate entries: +* - Same offset: keep first (deterministic from stable sort keys)
scripts/gen_lineinfo.c line 386
Quoted 10 lines
scripts/gen_lineinfo.c new file +10
-0+* - Same file:line as previous kept entry: redundant for binary +* search -- any address between them resolves to the earlier one +*/ + j =0 ; +for (i =1 ; i < num_entries; i++) { +if (entries[i].offset == entries[j].offset) +continue ; +if (entries[i].file_id == entries[j].file_id && + entries[i].line == entries[j].line) +continue ;
[Severity: Medium]
Does this logic erroneously drop valid lineinfo entries that share the same file and line but have different address offsets?
As mentioned in kallsyms_lookup_lineinfo(), dropping these entries causes the lookup to fail its symbol boundary check and reject the previous entry.
Quoted 11 lines
scripts/gen_lineinfo.c new file +6
-0+ j++; +if (j != i) + entries[j] = entries[i]; + } + num_entries = j +1 ; +}scripts/kallsyms.c +0
-0
[ ... ]
Quoted 4 lines
scripts/kallsyms.c +0
-0@@ -78,6 +78,17 @@ static char *sym_name(const struct sym_entry *s)static bool is_ignored_symbol (const char *name,char type) {
scripts/kallsyms.c static char *sym_name(const struct sym_entry *s) · line 90
Quoted 10 lines
scripts/kallsyms.c +10
-0+/* Ignore lineinfo symbols for kallsyms pass stability */ +static const char *const lineinfo_syms[] = { +"lineinfo_addrs" , +"lineinfo_file_ids" , +"lineinfo_file_offsets" , +"lineinfo_filenames" , +"lineinfo_lines" , +"lineinfo_num_entries" , +"lineinfo_num_files" , + };
[Severity: Low]
Is lineinfo_filenames_size intentionally omitted from this ignore list?
Missing this symbol causes it to be exposed in /proc/kallsyms while hiding its siblings.
Quoted 3 lines
scripts/kallsyms.c +1
-0+if (type =='u' || type =='n' )return true;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Hi Sasha,
Quoted 4 lines · Sasha Levin
Changes since v6
================- Address Sashiko AI review comments.
What does that mean?
Please list the changes, so reviewers know what to look at. Thanks!
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Quoted 11 lines · Geert Uytterhoeven
Hi Sasha,
Quoted 4 lines · Sasha Levin
Changes since v6
================- Address Sashiko AI review comments.
What does that mean?
Please list the changes, so reviewers know what to look at. Thanks!
Sorry! I've missed your mail.
There were a few smaller changes, mostly on the kunit tests side, here it is:
- Bound lineinfo lookups to the resolved symbol's start so it stops stale annotations on __init code and __pfx_* padding
- gen_lineinfo: handle SHT_REL .rel.debug_line (i386/arm32 modules)
- gen_lineinfo: bias all exec sections, drop foreign DWARF sequences; cover .noinstr.text (kvm.ko)
- __aligned(8) + static_asserts on the blob structs — fixes 32-bit layout mismatch (i386/m68k)
- SLEB128 decoder: do the shift unsigned
- KUnit: skip instead of fail when =m without KALLSYMS_LINEINFO_MODULES
- KUnit: dereference_function_descriptor() for target addresses (ppc64 ELFv1/parisc)
- KUnit: volatile accumulator in lineinfo_target_many_lines() (compilers fold it otherwise)
- KUnit: OPTIMIZER_HIDE_VAR for the mid-function address — fixes objtool !ENDBR warning
- KUnit: new regression tests (init-text stale annotation, 5-byte SLEB128 decode)
Signature · 2 lines
Thanks, Sasha
Petr Pavlu
maintainerQuoted 45 lines · Sasha Levin
Add
CONFIG_KALLSYMS_LINEINFO, which embeds a compact address-to-line lookup table in the kernel image so stack traces directly print source file and line number information:root@localhost:~# echo c > /proc/sysrq-trigger [ 11.201987] sysrq: Trigger a crash [ 11.202831] Kernel panic - not syncing: sysrq triggered crash [ 11.206218] Call Trace: [ 11.206501] <TASK> [ 11.206749] dump_stack_lvl+0x5d/0x80 (lib/dump_stack.c:94) [ 11.207403] vpanic+0x36e/0x620 (kernel/panic.c:650) [ 11.208565] ? __lock_acquire+0x465/0x2240 (kernel/locking/lockdep.c:4674) [ 11.209324] panic+0xc9/0xd0 (kernel/panic.c:787) [ 11.211873] ? find_held_lock+0x2b/0x80 (kernel/locking/lockdep.c:5350) [ 11.212597] ? lock_release+0xd3/0x300 (kernel/locking/lockdep.c:5535) [ 11.213312] sysrq_handle_crash+0x1a/0x20 (drivers/tty/sysrq.c:154) [ 11.214005] __handle_sysrq.cold+0x66/0x256 (drivers/tty/sysrq.c:611) [ 11.214712] write_sysrq_trigger+0x65/0x80 (drivers/tty/sysrq.c:1221) [ 11.215424] proc_reg_write+0x1bd/0x3c0 (fs/proc/inode.c:330) [ 11.216061] vfs_write+0x1c6/0xff0 (fs/read_write.c:686) [ 11.218848] ksys_write+0xfa/0x200 (fs/read_write.c:740) [ 11.222394] do_syscall_64+0xf3/0x690 (arch/x86/entry/syscall_64.c:63) [ 11.223942] entry_SYSCALL_64_after_hwframe+0x77/0x7f (arch/x86/entry/entry_64.S:121)At build time, a new host tool (
scripts/gen_lineinfo) reads DWARF .debug_line from vmlinux using libdw (elfutils), extracts all address-to-file:line mappings, and generates an assembly file with sorted parallel arrays (offsets from_text, file IDs, and line numbers). These are linked into vmlinux as .rodata.At runtime,
kallsyms_lookup_lineinfo()does a binary search on the table and__sprint_symbol()appends "(file:line)" to each stack frame. The lookup uses offsets from_textso it works with KASLR, requires no locks or allocations, and is safe in any context including panic.The feature requires
CONFIG_DEBUG_INFO(for DWARF data) and elfutils (libdw-dev) on the build host.Memory footprint measured with a 1852-option
x86_64config:Table: 4,597,583 entries from 4,841 source files
lineinfo_addrs[] 4,597,583 x u32 = 17.5 MiB lineinfo_file_ids[] 4,597,583 x u16 = 8.8 MiB lineinfo_lines[] 4,597,583 x u32 = 17.5 MiB file_offsets + filenames ~ 0.1 MiB
Quoted 6 lines
Total .rodata increase: ~ 44.0 MiB
vmlinux (stripped): 529 MiB -> 573 MiB (+44 MiB / +8.3%)
The .config used for testing is a simple KVM guest configuration for local development and testing.
I'm unable to reproduce these numbers. Could it be that the "stripped" vmlinux here still includes debug information? Otherwise, I'm not sure how to get to such a large binary.
With x86_64_defconfig + CONFIG_DEBUG_INFO=y, and after running strip -g on the produced vmlinux, I see the following increase.
Without the optimization in patch 3:
51 MiB -> 69 MiB (+18 MiB / +35.3%)
With the optimization in patch 3:
51 MiB -> 59 MiB (+8 MiB / +15.7%)
I notice that there is another set of numbers in the cover letter, measured using the Debian config. Those seem closer to my test results.
Quoted 56 lines
Assisted-by Claude:claude-opus-4-6Signed-off-by Sasha Levin <[email protected]>--- Documentation/admin-guide/index.rst | 1 + .../admin-guide/kallsyms-lineinfo.rst | 72 +++ MAINTAINERS | 6 + include/linux/kallsyms.h | 18 +- init/Kconfig | 20 + kernel/kallsyms.c | 96 ++- kernel/kallsyms_internal.h | 9 + scripts/.gitignore | 1 + scripts/Makefile | 3 + scripts/empty_lineinfo.S | 30 + scripts/gen_lineinfo.c | 557 ++++++++++++++++++ scripts/kallsyms.c | 16 + scripts/link-vmlinux.sh | 43 +- 13 files changed, 861 insertions(+), 11 deletions(-) create mode 100644 Documentation/admin-guide/kallsyms-lineinfo.rst create mode 100644 scripts/empty_lineinfo.S create mode 100644 scripts/gen_lineinfo.cDocumentation/admin-guide/index.rst +1
-0@@ -73,6 +73,7 @@ problems and bugs in particular. ramoops dynamic-debug-howto init + kallsyms-lineinfo kdump/index perf/index pstore-blkDocumentation/admin-guide/kallsyms-lineinfo.rst new file +16
-0@@ -0,0 +1,72 @@ +.. SPDX-License-Identifier: GPL-2.0 + +==================================== +Kallsyms Source Line Info (LINEINFO) +==================================== + +Overview +======== + +``CONFIG_KALLSYMS_LINEINFO`` embeds DWARF-derived source file and line number +mappings into the kernel image so that stack traces include +``(file.c:123)`` annotations next to each symbol. This makes it significantly +easier to pinpoint the exact source location during debugging, without needing +to manually cross-reference addresses with ``addr2line``. + +Enabling the Feature
Documentation/admin-guide/kallsyms-lineinfo.rst line 26
Quoted 10 lines
Documentation/admin-guide/kallsyms-lineinfo.rst new file +10
-0+==================== + +Enable the following kernel configuration options:: + + CONFIG_KALLSYMS=y + CONFIG_DEBUG_INFO=y + CONFIG_KALLSYMS_LINEINFO=y + +Build dependency: the host tool ``scripts/gen_lineinfo`` requires ``libdw`` +from elfutils. Install the development package:
Nit: It might be worth explicitly mentioning libelf as a dependency as well, since both libelf and libdw are required, even though libdw somewhat implies libelf.
Documentation/admin-guide/kallsyms-lineinfo.rst line 30
Quoted 4 lines
Documentation/admin-guide/kallsyms-lineinfo.rst new file +4
-0+ +- Debian/Ubuntu: ``apt install libdw-dev`` +- Fedora/RHEL: ``dnf install elfutils-devel`` +- Arch Linux: ``pacman -S elfutils``
libelf and libdw are available on Arch in libelf, not elfutils.
Quoted 118 lines
Documentation/admin-guide/kallsyms-lineinfo.rst new file +42
-0+ +Example Output +============== + +Without ``CONFIG_KALLSYMS_LINEINFO``:: + + Call Trace: + <TASK> + dump_stack_lvl+0x5d/0x80 + do_syscall_64+0x82/0x190 + entry_SYSCALL_64_after_hwframe+0x76/0x7e + +With ``CONFIG_KALLSYMS_LINEINFO``:: + + Call Trace: + <TASK> + dump_stack_lvl+0x5d/0x80 (lib/dump_stack.c:123) + do_syscall_64+0x82/0x190 (arch/x86/entry/common.c:52) + entry_SYSCALL_64_after_hwframe+0x76/0x7e + +Note that assembly routines (such as ``entry_SYSCALL_64_after_hwframe``) are +not annotated because they lack DWARF debug information. + +Memory Overhead +=============== + +The lineinfo tables are stored in ``.rodata`` and typically add approximately +44 MiB to the kernel image for a standard configuration (~4.6 million DWARF +line entries, ~10 bytes per entry after deduplication). + +Known Limitations +================= + +- **vmlinux only**: Only symbols in the core kernel image are annotated. + Module symbols are not covered. +- **4 GiB offset limit**: Address offsets from ``_text`` are stored as 32-bit + values. Entries beyond 4 GiB from ``_text`` are skipped at build time with + a warning. +- **65535 file limit**: Source file IDs are stored as 16-bit values. Builds + with more than 65535 unique source files will fail with an error. +- **No assembly annotations**: Functions implemented in assembly that lack + DWARF ``.debug_line`` data are not annotated.MAINTAINERS +6
-0@@ -13931,6 +13931,12 @@ S: Maintained F: Documentation/hwmon/k8temp.rst F: drivers/hwmon/k8temp.c +KALLSYMS LINEINFO +M: Sasha Levin <[email protected]> +S: Maintained +F: Documentation/admin-guide/kallsyms-lineinfo.rst +F: scripts/gen_lineinfo.c + KASAN M: Andrey Ryabinin <[email protected]> R: Alexander Potapenko <[email protected]>include/linux/kallsyms.h +17
-1@@ -16,10 +16,15 @@#include <asm/sections.h> #define KSYM_NAME_LEN 512 + +/* Extra space for " (path/to/file.c:12345)" suffix when lineinfo is enabled */ +#define KSYM_LINEINFO_LEN (IS_ENABLED(CONFIG_KALLSYMS_LINEINFO) ? 128 : 0) +#define KSYM_SYMBOL_LEN (sizeof("%s+%#lx/%#lx [%s %s]") + \ (KSYM_NAME_LEN - 1) + \ 2*(BITS_PER_LONG*3/10) + (MODULE_NAME_LEN - 1) + \ - (BUILD_ID_SIZE_MAX * 2) + 1) + (BUILD_ID_SIZE_MAX * 2) + 1 + \ + KSYM_LINEINFO_LEN)struct cred ;struct module ;@@ -96,6 +101,9 @@ extern int sprint_backtrace_build_id(char *buffer, unsigned long address);int lookup_symbol_name (unsigned long addr,char *symname); +bool kallsyms_lookup_lineinfo (unsigned long addr,unsigned long sym_start, +const char **file,unsigned int *line); +#else /* !CONFIG_KALLSYMS */ static inline unsigned long kallsyms_lookup_name (const char *name)@@ -164,6 +172,14 @@ static inline int kallsyms_on_each_match_symbol(int (*fn)(void *, unsigned long) {return -EOPNOTSUPP ; } + +static inline bool kallsyms_lookup_lineinfo (unsigned long addr, +unsigned long sym_start, +const char **file, +unsigned int *line) +{ +return false; +}#endif /*CONFIG_KALLSYMS*/ static inline void print_ip_sym (const char *loglvl,unsigned long ip)init/Kconfig +5
-0@@ -2087,6 +2087,26 @@ config KALLSYMS_ALL Say N unless you really need all symbols, or kernel live patching. +config KALLSYMS_LINEINFO + bool "Embed source file:line information in stack traces" + depends on KALLSYMS && DEBUG_INFO + help + Embeds an address-to-source-line mapping table in the kernel
init/Kconfig config KALLSYMS_ALL · line 2104
Quoted 10 lines
init/Kconfig +10
-0+ image so that stack traces directly include file:line information, + similar to what scripts/decode_stacktrace.sh provides but without + needing external tools or a vmlinux with debug info at runtime. + + When enabled, stack traces will look like: + + kmem_cache_alloc_noprof+0x60/0x630 (mm/slub.c:3456) + anon_vma_clone+0x2ed/0xcf0 (mm/rmap.c:412) + + This requires elfutils (libdw-dev/elfutils-devel) on the build host.
Nit: Without more context, elfutils could be understood to mean the eu-* utilities, but that is not what the feature actually requires. Something like the following would be clearer, at least to me:
This requires libelf and libdw (from elfutils) on the build host.
Quoted 13 lines
init/Kconfig +5
-0+ Adds approximately 44MB to a typical kernel image (10 bytes per + DWARF line-table entry, ~4.6M entries for a typical config). + + If unsure, say N. + # end of the "standard kernel features (expert users)" menu config ARCH_HAS_MEMBARRIER_CALLBACKSkernel/kallsyms.c +0
-0
kernel/kallsyms.c static int append_buildid(char *buffer, const char *modname, · line 473
Quoted 7 lines
kernel/kallsyms.c +4
-0@@ -467,13 +467,77 @@ static int append_buildid(char *buffer, const char *modname,#endif /* CONFIG_STACKTRACE_BUILD_ID */ +bool kallsyms_lookup_lineinfo (unsigned long addr,unsigned long sym_start, +const char **file,unsigned int *line) +{ +unsigned long long raw_offset;
Nit: raw_offset can be declared as unsigned long because it stores the result of A - B where both A and B are unsigned long and A > B.
Quoted 13 lines
kernel/kallsyms.c +13
-0+unsigned int offset, min_offset =0 , low, high, mid, file_id; + +if (!IS_ENABLED (CONFIG_KALLSYMS_LINEINFO ) || !lineinfo_num_entries) +return false; + +/* Compute offset from _text */ +if (addr < (unsigned long )_text) +return false; + + raw_offset = addr - (unsigned long )_text; +if (raw_offset >UINT_MAX ) +return false; + offset = (unsigned int )raw_offset;
kernel/kallsyms.c static int append_buildid(char *buffer, const char *modname, · line 496
Quoted 10 lines
kernel/kallsyms.c +10
-0+ +/* +* The search below returns the closest entry at or below @offset, so +* a symbol without line entries of its own (assembly without debug +* info, or anything past the _etext cap like .init.text) would +* inherit the last entry of whatever precedes it. Bound the result +* to entries at or above the resolved symbol's start. +*/ +if (sym_start > (unsigned long )_text) { +unsigned long long raw_min = sym_start - (unsigned long )_text;
Similarly here, raw_min can be declared as unsigned long.
Quoted 168 lines
kernel/kallsyms.c +62
-7+ +if (raw_min <= raw_offset) + min_offset = (unsigned int )raw_min; + } + +/* Binary search for largest entry <= offset */ + low =0 ; + high = lineinfo_num_entries; +while (low < high) { + mid = low + (high - low) /2 ; +if (lineinfo_addrs[mid] <= offset) + low = mid +1 ; +else + high = mid; + } + +if (low ==0 ) +return false; + low--; + +if (lineinfo_addrs[low] < min_offset) +return false; + + file_id = lineinfo_file_ids[low]; + *line = lineinfo_lines[low]; + +if (file_id >= lineinfo_num_files) +return false; + +if (lineinfo_file_offsets[file_id] >= lineinfo_filenames_size) +return false; + + *file = &lineinfo_filenames[lineinfo_file_offsets[file_id]]; +return true; +} +/* Look up a kernel symbol and return it in a text buffer. */ static int __sprint_symbol (char *buffer,unsigned long address, -int symbol_offset,int add_offset,int add_buildid) +int symbol_offset,int add_offset,int add_buildid, +int add_lineinfo) {char *modname;const unsigned char *buildid; -unsigned long offset, size; +unsigned long offset, size, sym_start;int len;/* Prevent module removal until modname and modbuildid are printed */ @@ -485,6 +549,7 @@ static int __sprint_symbol(char *buffer, unsigned long address,if (!len)return sprintf (buffer,"0x%lx" , address - symbol_offset); + sym_start = address - offset; offset -= symbol_offset;if (add_offset)@@ -497,6 +562,23 @@ static int __sprint_symbol(char *buffer, unsigned long address, len +=sprintf (buffer + len,"]" ); } +/* +* Append "(file:line)" only for stack-backtrace consumers. Plain +* sprint_symbol() backs %ps, and many existing format strings tack +* literal "()" after %ps to indicate a function call ("foo() +* replaced with bar()"); appending lineinfo there would produce a +* confusing "foo (file:line)()". +*/ +if (add_lineinfo &&IS_ENABLED (CONFIG_KALLSYMS_LINEINFO ) && !modname) { +const char *li_file; +unsigned int li_line; + +if (kallsyms_lookup_lineinfo (address, sym_start, + &li_file, &li_line)) + len +=snprintf (buffer + len,KSYM_SYMBOL_LEN - len, +" (%s:%u)" , li_file, li_line); + } +return len; }@@ -513,7 +595,7 @@ static int __sprint_symbol(char *buffer, unsigned long address,*/ int sprint_symbol (char *buffer,unsigned long address) { -return __sprint_symbol (buffer, address,0 ,1 ,0 ); +return __sprint_symbol (buffer, address,0 ,1 ,0 ,0 ); }EXPORT_SYMBOL_GPL (sprint_symbol);@@ -530,7 +612,7 @@ EXPORT_SYMBOL_GPL(sprint_symbol);*/ int sprint_symbol_build_id (char *buffer,unsigned long address) { -return __sprint_symbol (buffer, address,0 ,1 ,1 ); +return __sprint_symbol (buffer, address,0 ,1 ,1 ,0 ); }EXPORT_SYMBOL_GPL (sprint_symbol_build_id);@@ -547,7 +629,7 @@ EXPORT_SYMBOL_GPL(sprint_symbol_build_id);*/ int sprint_symbol_no_offset (char *buffer,unsigned long address) { -return __sprint_symbol (buffer, address,0 ,0 ,0 ); +return __sprint_symbol (buffer, address,0 ,0 ,0 ,0 ); }EXPORT_SYMBOL_GPL (sprint_symbol_no_offset);@@ -567,7 +649,7 @@ EXPORT_SYMBOL_GPL(sprint_symbol_no_offset);*/ int sprint_backtrace (char *buffer,unsigned long address) { -return __sprint_symbol (buffer, address,-1 ,1 ,0 ); +return __sprint_symbol (buffer, address,-1 ,1 ,0 ,1 ); }/** @@ -587,7 +669,7 @@ int sprint_backtrace(char *buffer, unsigned long address)*/ int sprint_backtrace_build_id (char *buffer,unsigned long address) { -return __sprint_symbol (buffer, address,-1 ,1 ,1 ); +return __sprint_symbol (buffer, address,-1 ,1 ,1 ,1 ); }/* To avoid using get_symbol_offset for every symbol, we carry prefix along. */ kernel/kallsyms_internal.h +9
-0@@ -15,4 +15,13 @@ extern const u16 kallsyms_token_index[];extern const unsigned int kallsyms_markers[];extern const u8 kallsyms_seqs_of_names[]; +extern const u32 lineinfo_num_entries; +extern const u32 lineinfo_addrs[]; +extern const u16 lineinfo_file_ids[]; +extern const u32 lineinfo_lines[]; +extern const u32 lineinfo_num_files; +extern const u32 lineinfo_file_offsets[]; +extern const u32 lineinfo_filenames_size; +extern const char lineinfo_filenames[]; +#endif // LINUX_KALLSYMS_INTERNAL_H_ scripts/.gitignore +1
-0@@ -1,5 +1,6 @@ # SPDX-License-Identifier: GPL-2.0-only /asn1_compiler +/gen_lineinfo /gen_packed_field_checks /generate_rust_target /insert-sys-certscripts/Makefile +1
-0@@ -4,6 +4,7 @@ # the kernel for the build process. hostprogs-always-$(CONFIG_KALLSYMS) += kallsyms +hostprogs-always-$(CONFIG_KALLSYMS_LINEINFO) += gen_lineinfo hostprogs-always-$(BUILD_C_RECORDMCOUNT) += recordmcount hostprogs-always-$(CONFIG_BUILDTIME_TABLE_SORT) += sorttable hostprogs-always-$(CONFIG_ASN1) += asn1_compiler
scripts/Makefile HOSTCFLAGS_asn1_compiler.o = -I$(srctree)/include · line 42
Quoted 5 lines
scripts/Makefile +2
-0@@ -37,6 +38,8 @@ HOSTCFLAGS_asn1_compiler.o = -I$(srctree)/include HOSTCFLAGS_sign-file.o = $(shell $(HOSTPKG_CONFIG) --cflags libcrypto 2> /dev/null) HOSTCFLAGS_sign-file.o += -I$(srctree)/tools/include/uapi/ HOSTLDLIBS_sign-file = $(shell $(HOSTPKG_CONFIG) --libs libcrypto 2> /dev/null || echo -lcrypto) +HOSTCFLAGS_gen_lineinfo.o = $(shell $(HOSTPKG_CONFIG) --cflags libdw 2> /dev/null) +HOSTLDLIBS_gen_lineinfo = $(shell $(HOSTPKG_CONFIG) --libs libdw 2> /dev/null || echo -ldw -lelf -lz)
The fallback output printed by echo includes -lz. Is this library really required by gen_lineinfo?
Quoted 46 lines
scripts/Makefile +0
-0ifdef CONFIG_UNWINDER_ORC ifeq ($(ARCH),x86_64)scripts/empty_lineinfo.S new file +30
-0@@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (C) 2026 Sasha Levin <[email protected]> + * + * Empty lineinfo stub for the initial vmlinux link. + * The real lineinfo is generated from .tmp_vmlinux1 by gen_lineinfo. + */ + .section .rodata, "a" + .globl lineinfo_num_entries + .balign 4 +lineinfo_num_entries: + .long 0 + .globl lineinfo_num_files + .balign 4 +lineinfo_num_files: + .long 0 + .globl lineinfo_addrs +lineinfo_addrs: + .globl lineinfo_file_ids +lineinfo_file_ids: + .globl lineinfo_lines +lineinfo_lines: + .globl lineinfo_file_offsets +lineinfo_file_offsets: + .globl lineinfo_filenames_size + .balign 4 +lineinfo_filenames_size: + .long 0 + .globl lineinfo_filenames +lineinfo_filenames:scripts/gen_lineinfo.c new file +1
-0@@ -0,0 +1,557 @@ +// SPDX-License-Identifier: GPL-2.0-only
scripts/gen_lineinfo.c line 11
Quoted 10 lines
scripts/gen_lineinfo.c new file +10
-0+/* +* gen_lineinfo.c - Generate address-to-source-line lookup tables from DWARF +* +* Copyright (C) 2026 Sasha Levin <[email protected]> +* +* Reads DWARF .debug_line from a vmlinux ELF file and outputs an assembly +* file containing sorted lookup tables that the kernel uses to annotate +* stack traces with source file:line information. +* +* Requires libdw from elfutils.
Nit: Again, I suggest mentioning both libelf and libdw for clarity.
Quoted 51 lines
scripts/gen_lineinfo.c new file +51
-0+*/ + +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <fcntl.h> +#include <unistd.h> +#include <elfutils/libdw.h> +#include <dwarf.h> +#include <elf.h> +#include <gelf.h> +#include <limits.h> + +static unsigned int skipped_overflow; + +/* +* vmlinux mode: end of the invariant .text region. Zero means "no cap" +* (graceful fallback when _etext is absent on some build). +*/ +static unsigned long long text_end_addr; + +struct line_entry { +unsigned int offset;/* offset from _text */ +unsigned int file_id; +unsigned int line; +}; + +struct file_entry { +char *name; +unsigned int id; +unsigned int str_offset; +}; + +static struct line_entry *entries; +static unsigned int num_entries; +static unsigned int entries_capacity; + +static struct file_entry *files; +static unsigned int num_files; +static unsigned int files_capacity; + +#define FILE_HASH_BITS 13 +#define FILE_HASH_SIZE (1 << FILE_HASH_BITS) + +struct file_hash_entry { +const char *name; +unsigned int id; +}; +
scripts/gen_lineinfo.c line 72
Quoted 10 lines
scripts/gen_lineinfo.c new file +10
-0+static struct file_hash_entry file_hash[FILE_HASH_SIZE ]; + +static unsigned int hash_str (const char *s) +{ +unsigned int h =5381 ; + +for (; *s; s++) + h = h *33 + (unsigned char )*s; +return h & (FILE_HASH_SIZE -1 ); +}
Does gen_lineinfo need to define own hash map and associated functions? Could it be replaced by the shared functionality in scripts/include/hashtable.h and hash.h?
scripts/gen_lineinfo.c line 83
Quoted 11 lines
scripts/gen_lineinfo.c new file +11
-0+ +static void add_entry (unsigned int offset,unsigned int file_id, +unsigned int line) +{ +if (num_entries >= entries_capacity) { + entries_capacity = entries_capacity ? entries_capacity *2 :65536 ; + entries =realloc (entries, entries_capacity *sizeof (*entries)); +if (!entries) { +fprintf (stderr,"out of memory\n" ); +exit (1 ); + }
Programs under the scripts/ directory normally use xmalloc() and related allocation functions from scripts/include/xalloc.h.
Quoted 24 lines
scripts/gen_lineinfo.c new file +24
-0+ } + entries[num_entries].offset = offset; + entries[num_entries].file_id = file_id; + entries[num_entries].line = line; + num_entries++; +} + +static unsigned int find_or_add_file (const char *name) +{ +unsigned int h =hash_str (name); + +/* Open-addressing lookup with linear probing */ +while (file_hash[h].name) { +if (!strcmp (file_hash[h].name, name)) +return file_hash[h].id; + h = (h +1 ) & (FILE_HASH_SIZE -1 ); + } + +if (num_files >=65535 ) { +fprintf (stderr, +"gen_lineinfo: too many source files (%u > 65535)\n" , + num_files); +exit (1 ); + }
scripts/gen_lineinfo.c line 117
Quoted 10 lines
scripts/gen_lineinfo.c new file +10
-0+ +if (num_files >= files_capacity) { + files_capacity = files_capacity ? files_capacity *2 :4096 ; + files =realloc (files, files_capacity *sizeof (*files)); +if (!files) { +fprintf (stderr,"out of memory\n" ); +exit (1 ); + } + } + files[num_files].name =strdup (name);
The return value of strdup() should be checked for NULL. xstrdup() is available in scripts/include/xalloc.h.
Quoted 62 lines
scripts/gen_lineinfo.c new file +62
-0+ files[num_files].id = num_files; + +/* Insert into hash table (points to files[] entry) */ + file_hash[h].name = files[num_files].name; + file_hash[h].id = num_files; + + num_files++; +return num_files -1 ; +} + +/* +* Well-known top-level directories in the kernel source tree. +* Used as a fallback to recover relative paths from absolute DWARF paths +* when comp_dir doesn't match (e.g. O= out-of-tree builds where comp_dir +* is the build directory but source paths point into the source tree). +*/ +static const char *const kernel_dirs[] = { +"arch/" ,"block/" ,"certs/" ,"crypto/" ,"drivers/" ,"fs/" , +"include/" ,"init/" ,"io_uring/" ,"ipc/" ,"kernel/" ,"lib/" , +"mm/" ,"net/" ,"rust/" ,"samples/" ,"scripts/" ,"security/" , +"sound/" ,"tools/" ,"usr/" ,"virt/" , +}; + +/* +* Strip a filename to a kernel-relative path. +* +* For absolute paths, strip the comp_dir prefix (from DWARF) to get +* a kernel-tree-relative path. When that fails (e.g. O= builds where +* comp_dir is the build directory), scan for a well-known kernel +* top-level directory name in the path to recover the relative path. +* Fall back to the basename as a last resort. +* +* For relative paths (common in modules), libdw may produce a bogus +* doubled path like "net/foo/bar.c/net/foo/bar.c" due to ET_REL DWARF +* quirks. Detect and strip such duplicates. +*/ +static const char *make_relative (const char *path,const char *comp_dir) +{ +const char *p; + +/* If already relative, use as-is */ +if (path[0 ] !='/' ) +return path; + +/* comp_dir from DWARF is the most reliable method */ +if (comp_dir) { +size_t len =strlen (comp_dir); + +if (!strncmp (path, comp_dir, len) && path[len] =='/' ) { +const char *rel = path + len +1 ; + +/* +* If comp_dir pointed to a subdirectory +* (e.g. arch/parisc/kernel) rather than +* the tree root, stripping it leaves a +* bare filename. Fall through to the +* kernel_dirs scan so we recover the full +* relative path instead. +*/ +if (strchr (rel,'/' )) +return rel; + }
scripts/gen_lineinfo.c line 189
Quoted 10 lines
scripts/gen_lineinfo.c new file +10
-0+ +/* +* comp_dir prefix didn't help — either it didn't match +* or it was too specific and left a bare filename. +* Scan for a known kernel top-level directory component +* to find where the relative path starts. This handles +* O= builds and arches where comp_dir is a subdirectory. +*/ +for (p = path +1 ; *p; p++) { +if (*(p -1 ) =='/' ) {
Nit: This can be simplified to something like the following to reduce code indentation:
for (p = strchr(path, '/'); p; p = strchr(p + 1, '/')) {
and using p + 1 inside the loop.
scripts/gen_lineinfo.c line 191
Quoted 2 lines
scripts/gen_lineinfo.c new file +2
-0+for (unsigned int i =0 ; i <sizeof (kernel_dirs) / +sizeof (kernel_dirs[0 ]); i++) {
Nit: scripts/include/array_size.h provides ARRAY_SIZE().
scripts/gen_lineinfo.c line 194
Quoted 3 lines
scripts/gen_lineinfo.c new file +3
-0+if (!strncmp (p, kernel_dirs[i], +strlen (kernel_dirs[i]))) +return p;
I think using well-known top-level directories in the kernel tree to determine a kernel-tree-relative path for a specific object is rather fragile. The main problem is that the input path may contain components with the same name as one of those top-level directories.
An alternative approach would be to pass $srctree and $objtree to gen_lineinfo, either on the command line or during compilation using:
-DSRCTREE='"$(abspath $(srctree))"' -DOBJTREE='"$(abspath $(objtree))"'
and then try matching the path name against those values.
if name is relative:
name = comp_dir + name
resolve . and .. in name
if name startswith objtree:
name += strlen(objtree)
elif name startswith srctree:
name += strlen(srctree)
I'm not sure how well this would work with symlinks, though, and some consideration also needs to be given to handling external modules properly.
scripts/gen_lineinfo.c line 201
Quoted 7 lines
scripts/gen_lineinfo.c new file +7
-0+ } + } + } + +/* Fall back to basename */ + p =strrchr (path,'/' ); +return p ? p +1 : path;
These two lines are unnecessary as the same code is repeated below and it is sufficient for control to fall through to that instance.
Quoted 298 lines
scripts/gen_lineinfo.c new file +298
-0+ } + +/* Fall back to basename */ + p =strrchr (path,'/' ); +return p ? p +1 : path; +} + +static int compare_entries (const void *a,const void *b) +{ +const struct line_entry *ea = a; +const struct line_entry *eb = b; + +if (ea->offset != eb->offset) +return ea->offset < eb->offset ?-1 :1 ; +if (ea->file_id != eb->file_id) +return ea->file_id < eb->file_id ?-1 :1 ; +if (ea->line != eb->line) +return ea->line < eb->line ?-1 :1 ; +return 0 ; +} + +/* +* Look up a vmlinux symbol by exact name and return its st_value, or +* @fallback if absent. Aborts when @required and the symbol is missing. +*/ +static unsigned long long find_vmlinux_sym (Elf *elf,const char *name, +unsigned long long fallback, +bool required) +{ +size_t nsyms, i; +Elf_Scn *scn =NULL ; +GElf_Shdr shdr; + +while ((scn =elf_nextscn (elf, scn)) !=NULL ) { +Elf_Data *data; + +if (!gelf_getshdr (scn, &shdr)) +continue ; +if (shdr.sh_type !=SHT_SYMTAB ) +continue ; + + data =elf_getdata (scn,NULL ); +if (!data) +continue ; + + nsyms = shdr.sh_size / shdr.sh_entsize; +for (i =0 ; i < nsyms; i++) { +GElf_Sym sym; +const char *sname; + +if (!gelf_getsym (data, i, &sym)) +continue ; + sname =elf_strptr (elf, shdr.sh_link, sym.st_name); +if (sname && !strcmp (sname, name)) +return sym.st_value; + } + } + +if (required) { +fprintf (stderr,"Cannot find %s symbol\n" , name); +exit (1 ); + } +return fallback; +} + +static unsigned long long find_text_addr (Elf *elf) +{ +return find_vmlinux_sym (elf,"_text" ,0 , true); +} + +/* +* vmlinux is linked in multiple passes: gen_lineinfo runs against +* .tmp_vmlinux1 (which carries an empty lineinfo stub), then real tables +* are linked in for the final image. Sections placed AFTER .rodata +* (.init.text, .exit.text, ...) shift forward as .rodata grows to hold +* the real lineinfo blob, so DWARF addresses we'd capture for them in +* pass 1 would be stale in the final kernel. Cap captured addresses at +* _etext, the symbol that marks the end of .text — placed before .rodata +* in every architecture's vmlinux.lds.S, so its addresses are invariant +* across the relink. Returns 0 if _etext is absent (no cap; v3 behavior). +*/ +static unsigned long long find_text_end_addr (Elf *elf) +{ +return find_vmlinux_sym (elf,"_etext" ,0 , false); +} + +static void process_dwarf (Dwarf *dwarf,unsigned long long text_addr) +{ +Dwarf_Off off =0 , next_off; +size_t hdr_size; + +while (dwarf_nextcu (dwarf, off, &next_off, &hdr_size, +NULL ,NULL ,NULL ) ==0 ) { +Dwarf_Die cudie; +Dwarf_Lines *lines; +size_t nlines; +Dwarf_Attribute attr; +const char *comp_dir =NULL ; + +if (!dwarf_offdie (dwarf, off + hdr_size, &cudie)) + goto next; + +if (dwarf_attr (&cudie, DW_AT_comp_dir, &attr)) + comp_dir =dwarf_formstring (&attr); + +if (dwarf_getsrclines (&cudie, &lines, &nlines) !=0 ) + goto next; + +for (size_t i =0 ; i < nlines; i++) { +Dwarf_Line *line =dwarf_onesrcline (lines, i); +Dwarf_Addr addr; +const char *src; +const char *rel; +unsigned int file_id, loffset; +int lineno; + +if (!line) +continue ; + +if (dwarf_lineaddr (line, &addr) !=0 ) +continue ; +if (dwarf_lineno (line, &lineno) !=0 ) +continue ; +if (lineno ==0 ) +continue ; + + src =dwarf_linesrc (line,NULL ,NULL ); +if (!src) +continue ; + +if (addr < text_addr) +continue ; +/* +* Skip addresses past _etext. Sections after .rodata +* shift when the real lineinfo replaces the empty stub +* during the multi-pass vmlinux link, so any address +* we'd capture there would be stale by the time the +* final kernel runs. +*/ +if (text_end_addr && addr >= text_end_addr) +continue ; + + { +unsigned long long raw_offset = addr - text_addr; + +if (raw_offset >UINT_MAX ) { + skipped_overflow++; +continue ; + } + loffset = (unsigned int )raw_offset; + } + + rel =make_relative (src, comp_dir); + file_id =find_or_add_file (rel); + +add_entry (loffset, file_id, (unsigned int )lineno); + } +next: + off = next_off; + } +} + +static void deduplicate (void ) +{ +unsigned int i, j; + +if (num_entries <2 ) +return ; + +/* Sort by offset, then file_id, then line for stability */ +qsort (entries, num_entries,sizeof (*entries), compare_entries); + +/* +* Remove duplicate entries: +* - Same offset: keep first (deterministic from stable sort keys) +* - Same file:line as previous kept entry: redundant for binary +* search -- any address between them resolves to the earlier one +*/ + j =0 ; +for (i =1 ; i < num_entries; i++) { +if (entries[i].offset == entries[j].offset) +continue ; +if (entries[i].file_id == entries[j].file_id && + entries[i].line == entries[j].line) +continue ; + j++; +if (j != i) + entries[j] = entries[i]; + } + num_entries = j +1 ; +} + +static void compute_file_offsets (void ) +{ +unsigned int offset =0 ; + +for (unsigned int i =0 ; i < num_files; i++) { + files[i].str_offset = offset; + offset +=strlen (files[i].name) +1 ; + } +} + +static void print_escaped_asciz (const char *s) +{ +printf ("\t.asciz \"" ); +for (; *s; s++) { +if (*s =='"' || *s =='\\' ) +putchar ('\\' ); +putchar (*s); + } +printf ("\"\n" ); +} + +static void output_assembly (void ) +{ +printf ("/* SPDX-License-Identifier: GPL-2.0 */\n" ); +printf ("/*\n" ); +printf (" * Automatically generated by scripts/gen_lineinfo\n" ); +printf (" * Do not edit.\n" ); +printf (" */\n\n" ); + +printf ("\t.section .rodata, \"a\"\n\n" ); + +/* Number of entries */ +printf ("\t.globl lineinfo_num_entries\n" ); +printf ("\t.balign 4\n" ); +printf ("lineinfo_num_entries:\n" ); +printf ("\t.long %u\n\n" , num_entries); + +/* Number of files */ +printf ("\t.globl lineinfo_num_files\n" ); +printf ("\t.balign 4\n" ); +printf ("lineinfo_num_files:\n" ); +printf ("\t.long %u\n\n" , num_files); + +/* Sorted address offsets from _text */ +printf ("\t.globl lineinfo_addrs\n" ); +printf ("\t.balign 4\n" ); +printf ("lineinfo_addrs:\n" ); +for (unsigned int i =0 ; i < num_entries; i++) +printf ("\t.long 0x%x\n" , entries[i].offset); +printf ("\n" ); + +/* File IDs, parallel to addrs (u16 -- supports up to 65535 files) */ +printf ("\t.globl lineinfo_file_ids\n" ); +printf ("\t.balign 2\n" ); +printf ("lineinfo_file_ids:\n" ); +for (unsigned int i =0 ; i < num_entries; i++) +printf ("\t.short %u\n" , entries[i].file_id); +printf ("\n" ); + +/* Line numbers, parallel to addrs */ +printf ("\t.globl lineinfo_lines\n" ); +printf ("\t.balign 4\n" ); +printf ("lineinfo_lines:\n" ); +for (unsigned int i =0 ; i < num_entries; i++) +printf ("\t.long %u\n" , entries[i].line); +printf ("\n" ); + +/* File string offset table */ +printf ("\t.globl lineinfo_file_offsets\n" ); +printf ("\t.balign 4\n" ); +printf ("lineinfo_file_offsets:\n" ); +for (unsigned int i =0 ; i < num_files; i++) +printf ("\t.long %u\n" , files[i].str_offset); +printf ("\n" ); + +/* Filenames size */ + { +unsigned int fsize =0 ; + +for (unsigned int i =0 ; i < num_files; i++) + fsize +=strlen (files[i].name) +1 ; +printf ("\t.globl lineinfo_filenames_size\n" ); +printf ("\t.balign 4\n" ); +printf ("lineinfo_filenames_size:\n" ); +printf ("\t.long %u\n\n" , fsize); + } + +/* Concatenated NUL-terminated filenames */ +printf ("\t.globl lineinfo_filenames\n" ); +printf ("lineinfo_filenames:\n" ); +for (unsigned int i =0 ; i < num_files; i++) +print_escaped_asciz (files[i].name); +printf ("\n" ); +} + +int main (int argc,char *argv[]) +{ +int fd; +Elf *elf; +Dwarf *dwarf; +unsigned long long text_addr; + +if (argc !=2 ) { +fprintf (stderr,"Usage: %s <vmlinux>\n" , argv[0 ]); +return 1 ; + }
scripts/gen_lineinfo.c line 509
Quoted 10 lines
scripts/gen_lineinfo.c new file +10
-0+ + fd =open (argv[1 ],O_RDONLY ); +if (fd <0 ) { +fprintf (stderr,"Cannot open %s: %s\n" , argv[1 ], +strerror (errno)); +return 1 ; + } + +elf_version (EV_CURRENT ); + elf =elf_begin (fd,ELF_C_READ ,NULL );
I think using ELF_C_READ_MMAP is normally preferred. Even better, this could use ELF_C_READ_MMAP_PRIVATE to avoid opening modules as read-write in patch 2.
Quoted 9 lines
scripts/gen_lineinfo.c new file +9
-0+if (!elf) { +fprintf (stderr,"elf_begin failed: %s\n" , +elf_errmsg (elf_errno ())); +close (fd); +return 1 ; + } + + text_addr =find_text_addr (elf); + text_end_addr =find_text_end_addr (elf);
scripts/gen_lineinfo.c line 528
Quoted 10 lines
scripts/gen_lineinfo.c new file +10
-0+ + dwarf =dwarf_begin_elf (elf,DWARF_C_READ ,NULL ); +if (!dwarf) { +fprintf (stderr,"dwarf_begin_elf failed: %s\n" , +dwarf_errmsg (dwarf_errno ())); +fprintf (stderr,"Is %s built with CONFIG_DEBUG_INFO?\n" , + argv[1 ]); +elf_end (elf); +close (fd); +return 1 ;
The program has inconsistent error handling. Other functions, such as find_or_add_file() and find_vmlinux_sym(), call exit(1) on error without unwinding and cleaning up resources, whereas main() attempts cleanup. I believe the preferred error-handling style for utility programs under scripts/ is the former, as it keeps the code short.
Quoted 4 lines
scripts/gen_lineinfo.c new file +4
-0+ } + +process_dwarf (dwarf, text_addr); +
scripts/gen_lineinfo.c line 542
Quoted 10 lines
scripts/gen_lineinfo.c new file +10
-0+if (skipped_overflow) +fprintf (stderr, +"lineinfo: warning: %u entries skipped (offset > 4 GiB from _text)\n" , + skipped_overflow); + +deduplicate (); +compute_file_offsets (); + +fprintf (stderr,"lineinfo: %u entries, %u files\n" , + num_entries, num_files);
This output clutters the build log:
LD .tmp_vmlinux1
LINEINFO .tmp_lineinfo.S
lineinfo: 1372576 entries, 4193 files
AS .tmp_lineinfo.o NM .tmp_vmlinux1.syms KSYMS .tmp_vmlinux1.kallsyms.S AS .tmp_vmlinux1.kallsyms.o
It should probably be hidden behind a --verbose option and printed only when KBUILD_VERBOSE=1.
Quoted 140 lines
scripts/gen_lineinfo.c new file +15
-0+ +output_assembly (); + +dwarf_end (dwarf); +elf_end (elf); +close (fd); + +/* Cleanup */ +free (entries); +for (unsigned int i =0 ; i < num_files; i++) +free (files[i].name); +free (files); + +return 0 ; +}scripts/kallsyms.c +16
-0@@ -78,6 +78,17 @@ static char *sym_name(const struct sym_entry *s)static bool is_ignored_symbol (const char *name,char type) { +/* Ignore lineinfo symbols for kallsyms pass stability */ +static const char *const lineinfo_syms[] = { +"lineinfo_addrs" , +"lineinfo_file_ids" , +"lineinfo_file_offsets" , +"lineinfo_filenames" , +"lineinfo_lines" , +"lineinfo_num_entries" , +"lineinfo_num_files" , + }; +if (type =='u' || type =='n' )return true;@@ -90,6 +101,11 @@ static bool is_ignored_symbol(const char *name, char type)return true; } +for (size_t i =0 ; i <ARRAY_SIZE (lineinfo_syms); i++) { +if (!strcmp (name, lineinfo_syms[i])) +return true; + } +return false; }scripts/link-vmlinux.sh +40
-3@@ -103,7 +103,7 @@ vmlinux_link() ${ld} ${ldflags} -o ${output} \ ${wl}--whole-archive ${objs} ${wl}--no-whole-archive \ ${wl}--start-group ${libs} ${wl}--end-group \ - ${kallsymso} ${btf_vmlinux_bin_o} ${arch_vmlinux_o} ${ldlibs} + ${kallsymso} ${lineinfo_o} ${btf_vmlinux_bin_o} ${arch_vmlinux_o} ${ldlibs} } # Create ${2}.o file with all symbols from the ${1} object file@@ -129,6 +129,26 @@ kallsyms() kallsymso=${2}.o } +# Generate lineinfo tables from DWARF debug info in a temporary vmlinux. +# ${1} - temporary vmlinux with debug info +# Output: sets lineinfo_o to the generated .o file +gen_lineinfo() +{ + info LINEINFO .tmp_lineinfo.S + if ! scripts/gen_lineinfo "${1}" > .tmp_lineinfo.S; then + echo >&2 "Failed to generate lineinfo from ${1}" + echo >&2 "Try to disable CONFIG_KALLSYMS_LINEINFO" + exit 1 + fi + + info AS .tmp_lineinfo.o + ${CC} ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS} \ + ${KBUILD_AFLAGS} ${KBUILD_AFLAGS_KERNEL} \ + -c -o .tmp_lineinfo.o .tmp_lineinfo.S + + lineinfo_o=.tmp_lineinfo.o +} + # Perform kallsyms for the given temporary vmlinux. sysmap_and_kallsyms() {@@ -155,6 +175,7 @@ sorttable() cleanup() { rm -f .btf.* + rm -f .tmp_lineinfo.* rm -f .tmp_vmlinux.nm-sort rm -f System.map rm -f vmlinux@@ -183,6 +204,7 @@ fi btf_vmlinux_bin_o= btfids_vmlinux= kallsymso= +lineinfo_o= strip_debug= generate_map=@@ -198,10 +220,21 @@ if is_enabled CONFIG_KALLSYMS; then kallsyms .tmp_vmlinux0.syms .tmp_vmlinux0.kallsyms fi +if is_enabled CONFIG_KALLSYMS_LINEINFO; then + # Assemble an empty lineinfo stub for the initial link. + # The real lineinfo is generated from .tmp_vmlinux1 by gen_lineinfo. + ${CC} ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS} \ + ${KBUILD_AFLAGS} ${KBUILD_AFLAGS_KERNEL} \ + -c -o .tmp_lineinfo.o "${srctree}/scripts/empty_lineinfo.S" + lineinfo_o=.tmp_lineinfo.o +fi + if is_enabled CONFIG_KALLSYMS || is_enabled CONFIG_DEBUG_INFO_BTF; then - # The kallsyms linking does not need debug symbols, but the BTF does. - if ! is_enabled CONFIG_DEBUG_INFO_BTF; then + # The kallsyms linking does not need debug symbols, but BTF and + # lineinfo generation do. + if ! is_enabled CONFIG_DEBUG_INFO_BTF && + ! is_enabled CONFIG_KALLSYMS_LINEINFO; then strip_debug=1 fi@@ -219,6 +252,10 @@ if is_enabled CONFIG_DEBUG_INFO_BTF; then btfids_vmlinux=.tmp_vmlinux1.BTF_ids fi +if is_enabled CONFIG_KALLSYMS_LINEINFO; then + gen_lineinfo .tmp_vmlinux1 +fi + if is_enabled CONFIG_KALLSYMS; then # kallsyms support
Signature · 2 lines
Thanks, Petr
Quoted 2 lines · Petr Pavlu
I'm unable to reproduce these numbers. Could it be that the "stripped" vmlinux here still includes debug information?
Yup! the figure was taken from an unstripped vmlinux, so it was really reporting DWARF size. Will fix, and state the recipe inline so it's reproducible. Your numbers reproduce here exactly:
no lineinfo: 51.1 MiB
lineinfo, before 3/4: 69.1 MiB (+18.0 MiB / +35.3%) lineinfo, after 3/4: 59.1 MiB ( +8.0 MiB / +15.7%)
Quoted 6 lines
Nit: It might be worth explicitly mentioning libelf as a dependency [...] libelf and libdw are available on Arch in libelf, not elfutils. [...]
Nit: Without more context, elfutils could be understood to mean the eu-* utilities [...]
Nit: Again, I suggest mentioning both libelf and libdw for clarity.
Will fix.
Quoted 2 lines
Nit:
raw_offsetcan be declared asunsigned long[...] Similarly here,raw_mincan be declared asunsigned long.
Will fix.
Quoted 2 lines
The fallback output printed by echo includes -lz. Is this library really required by
gen_lineinfo?
No. Will drop it.
Quoted 2 lines
Does
gen_lineinfoneed to define own hash map [...] Could it be replaced by the shared functionality inscripts/include/hashtable.handhash.h?
Will fix.
Quoted 2 lines
Programs under the
scripts/directory normally usexmalloc()[...] The return value ofstrdup()should be checked for NULL.
Will fix.
Quoted 3 lines
Nit: This can be simplified [...]
Nit:scripts/include/array_size.hprovidesARRAY_SIZE(). These two lines are unnecessary as the same code is repeated below.
Will fix.
Quoted 3 lines
I think using well-known top-level directories in the kernel tree [...] is rather fragile. [...] An alternative approach would be to pass $srctree and $objtree to
gen_lineinfo
Agreed, will do it that way. Reading them with getenv() rather than -D:
kbuild already exports objtree, srctree and srcroot, and kconfig reads them the same way, so no Makefile change is needed. Each gets registered both as spelled and as realpath() so a symlinked tree matches either form, longest match wins.
On your two caveats: realpath() is applied only to the roots, never to a DWARF path, since generated sources need not exist when gen_lineinfo runs -- DWARF paths get purely lexical normalization. And srcroot is what covers M= external modules, whose sources are under neither srctree nor objtree.
I'll keep kernel_dirs[] as a last-resort fallback rather than deleting it, so an object built outside all three roots degrades as it does today instead of dropping to a bare basename.
Worth noting the rework is not just cosmetic: 1136 of 4450 filenames here carried a "./" prefix from DWARF directory entries (addr2line shows it too), which normalization removes, folding the file count to 4354.
Quoted 2 lines
I think using
ELF_C_READ_MMAPis normally preferred. Even better, this could useELF_C_READ_MMAP_PRIVATEto avoid opening modules as read-write
Will fix.
Quoted 1 line
The program has inconsistent error handling.
Will fix.
Quoted 2 lines
This output clutters the build log [...] It should probably be hidden behind a --verbose option and printed only when KBUILD_VERBOSE=1.
Will fix.
Separately, working through your review turned up two bugs, both of which v8 will fix with regression coverage:
- Module lineinfo blobs are malformed. emit_section_table() emits the .Lhdr label before its .balign 4, so table_offset (.Lhdr - .Lroot) points into the padding and the kernel reads the header 1-3 bytes early.
- The symbol-boundary check added in v7 over-corrected. Functions whose only line row deduplicate() had collapsed, or that never had a row at their own first byte, stopped resolving: 5017 here, including 1542 .cold fragments, all __probestub_*, and the __ia32_sys_* compat wrappers. v6 printed these correctly.
Thanks for the thorough review.
Signature · 2 lines
Thanks, Sasha