kallsyms: embed source file:line info in kernel stack traces
Subsystems (4)
v2 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 [1] 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_symbol() 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 a .mod_lineinfo section embedded at build time. The module loader picks it up at load time. Same zero-allocation, NMI-safe lookup.
Patch 3: delta compression
Block-indexed delta-encoding with ULEB128 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.
Patch 4: KUnit tests
New in v2. 30 KUnit tests covering the lineinfo lookup paths, delta-decode logic, boundary conditions, 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)
Lineinfo data breakdown:
lineinfo_data (delta-compressed): 5,728 KiB (97%)
lineinfo_block_addrs: 99 KiB
lineinfo_block_offsets: 99 KiB
lineinfo_filenames: 111 KiB
lineinfo_file_offsets: 17 KiB
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 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.
- Fix vmlinux compressed-path data_end for the last block: use lineinfo_data_size instead of UINT_MAX.
- Add file_offsets[] and filenames_size bounds checks in vmlinux lookup path (the module path already had them).
- Add alignment padding for file_offsets[] in module .mod_lineinfo binary format (data[] is variable-length u8, followed by u32[]).
- Remove sym_start cross-validation check that incorrectly rejected valid lineinfo entries for assembly-adjacent functions.
- Add KUnit test suite (new patch 4/4): 30 tests covering vmlinux lookup, module lookup, delta decode, boundary conditions, and backtrace formatting integration.
Changes since RFC
=================
- Added module support (patch 2)
- Added delta compression (patch 3), reducing size from ~44 MiB to ~11 MiB, addressing the primary concern from RFC review
- Added documentation (Documentation/admin-guide/kallsyms-lineinfo.rst)
- Added MAINTAINERS entry
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 | 32 +- include/linux/mod_lineinfo.h | 140 +++ include/linux/module.h | 19 + init/Kconfig | 35 + kernel/kallsyms.c | 136 +++ kernel/kallsyms_internal.h | 13 + kernel/module/kallsyms.c | 153 +++ kernel/module/main.c | 4 + lib/Kconfig.debug | 10 + lib/tests/Makefile | 3 + lib/tests/lineinfo_kunit.c | 772 +++++++++++++++ scripts/.gitignore | 1 + scripts/Makefile | 4 + scripts/Makefile.modfinal | 6 + scripts/gen-mod-lineinfo.sh | 48 + scripts/gen_lineinfo.c | 928 ++++++++++++++++++ scripts/kallsyms.c | 17 + scripts/link-vmlinux.sh | 78 +- 21 files changed, 2502 insertions(+), 4 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 100755 scripts/gen-mod-lineinfo.sh create mode 100644 scripts/gen_lineinfo.c
Signature · 1 line
2.51.0
PATCH v2 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%)
Note: this probably won't be something we roll into "production", but it might be useful for the average user given the relatively low memory footprint, in canary deployments for hyperscalers, or by default for folks who run tests/fuzzing/etc.
Disclaimer: this was vibe coded over an afternoon with an AI coding assistant.
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 | 32 +- init/Kconfig | 20 + kernel/kallsyms.c | 61 +++ kernel/kallsyms_internal.h | 11 + scripts/.gitignore | 1 + scripts/Makefile | 3 + scripts/gen_lineinfo.c | 510 ++++++++++++++++++ scripts/kallsyms.c | 16 + scripts/link-vmlinux.sh | 70 ++- 12 files changed, 799 insertions(+), 4 deletions(-) create mode 100644 Documentation/admin-guide/kallsyms-lineinfo.rst 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
@@ -13728,6 +13728,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 +31 -1
@@ -16,10 +16,19 @@#include <asm/sections.h> #define KSYM_NAME_LEN 512 + +#ifdef CONFIG_KALLSYMS_LINEINFO +/* Extra space for " (path/to/file.c:12345)" suffix */ +#define KSYM_LINEINFO_LEN 128 +#else +#define KSYM_LINEINFO_LEN 0 +#endif +#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 +105,19 @@ extern int sprint_backtrace_build_id(char *buffer, unsigned long address);int lookup_symbol_name (unsigned long addr,char *symname); +#ifdef CONFIG_KALLSYMS_LINEINFO +bool kallsyms_lookup_lineinfo (unsigned long addr,unsigned long sym_start, +const char **file,unsigned int *line); +#else +static inline bool kallsyms_lookup_lineinfo (unsigned long addr, +unsigned long sym_start, +const char **file, +unsigned int *line) +{ +return false; +} +#endif +#else /* !CONFIG_KALLSYMS */ static inline unsigned long kallsyms_lookup_name (const char *name)
@@ -164,6 +186,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
@@ -2050,6 +2050,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 +61 -0
73 lines not on this page · raw message
kernel/kallsyms_internal.h +11 -0
@@ -15,4 +15,15 @@ extern const u16 kallsyms_token_index[];extern const unsigned int kallsyms_markers[];extern const u8 kallsyms_seqs_of_names[]; +#ifdef CONFIG_KALLSYMS_LINEINFO +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 +#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
@@ -36,6 +37,8 @@ HOSTLDLIBS_sorttable = -lpthread HOSTCFLAGS_asn1_compiler.o = -I$(srctree)/include HOSTCFLAGS_sign-file.o = $(shell $(HOSTPKG_CONFIG) --cflags libcrypto 2> /dev/null) 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/gen_lineinfo.c new file +510 -0
510 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 +67 -3
108 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v2 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 a .mod_lineinfo section containing a compact binary table of .text-relative offsets, file IDs, line numbers, and filenames, and embeds it back into the .ko via objcopy.
At runtime, module_lookup_lineinfo() performs a binary search on the module's .mod_lineinfo section, and __sprint_symbol() calls it for addresses that fall within a module. 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 is unloaded.
The gen_lineinfo tool gains --module mode which:
- Uses .text section address as base (ET_REL files have no _text symbol)
- Filters entries to .text-only (excludes .init.text/.exit.text)
- Handles libdw's ET_REL path-doubling quirk in make_relative()
- Outputs a flat binary-format section instead of named global symbols
Per-module overhead is approximately 10 bytes per DWARF line entry.
--- .../admin-guide/kallsyms-lineinfo.rst | 40 +- MAINTAINERS | 2 + include/linux/mod_lineinfo.h | 68 ++++ include/linux/module.h | 19 + init/Kconfig | 13 + kernel/kallsyms.c | 22 +- kernel/module/kallsyms.c | 91 +++++ kernel/module/main.c | 4 + scripts/Makefile | 1 + scripts/Makefile.modfinal | 6 + scripts/gen-mod-lineinfo.sh | 48 +++ scripts/gen_lineinfo.c | 349 ++++++++++++++++-- 12 files changed, 622 insertions(+), 41 deletions(-) create mode 100644 include/linux/mod_lineinfo.h create mode 100755 scripts/gen-mod-lineinfo.sh
Documentation/admin-guide/kallsyms-lineinfo.rst +32 -8
@@ -51,22 +51,46 @@ 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 44 MiB to the kernel image for a standard configuration +(~4.6 million DWARF line entries, ~10 bytes per entry after deduplication). + +Per-module lineinfo adds approximately 10 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 +2 -0
@@ -13732,6 +13732,8 @@ KALLSYMS LINEINFO M: Sasha Levin <[email protected]> S: Maintained F: Documentation/admin-guide/kallsyms-lineinfo.rst +F: include/linux/mod_lineinfo.h +F: scripts/gen-mod-lineinfo.sh F: scripts/gen_lineinfo.c KASAN
include/linux/mod_lineinfo.h new file +68 -0
68 lines not on this page · raw message
include/linux/module.h +19 -0
@@ -508,6 +508,10 @@ 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; +#endif #ifdef CONFIG_JUMP_LABEL struct jump_entry *jump_entries;unsigned int num_jump_entries;
@@ -1021,6 +1025,21 @@ static inline unsigned long find_kallsyms_symbol_value(struct module *mod,#endif /* CONFIG_MODULES && CONFIG_KALLSYMS */ +#ifdef CONFIG_KALLSYMS_LINEINFO_MODULES +bool module_lookup_lineinfo (struct module *mod,unsigned long addr, +unsigned long sym_start, +const char **file,unsigned int *line); +#else +static inline bool module_lookup_lineinfo (struct module *mod, +unsigned long addr, +unsigned long sym_start, +const char **file, +unsigned int *line) +{ +return false; +} +#endif +/* Define __free(module_put) macro for struct module *. */ DEFINE_FREE(module_put, struct module *, if (_T) module_put(_T))
init/Kconfig +13 -0
@@ -2070,6 +2070,19 @@ config KALLSYMS_LINEINFO 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 10 bytes per DWARF line entry. + + If unsure, say N. + # end of the "standard kernel features (expert users)" menu config ARCH_HAS_MEMBARRIER_CALLBACKS
kernel/kallsyms.c +18 -4
@@ -546,13 +546,27 @@ static int __sprint_symbol(char *buffer, unsigned long address, }#ifdef CONFIG_KALLSYMS_LINEINFO -if (!modname) { + {const char *li_file;unsigned int li_line;unsigned long sym_start = address - offset; - -if (kallsyms_lookup_lineinfo (address, sym_start, - &li_file, &li_line)) +bool found = false; + +if (!modname) + found =kallsyms_lookup_lineinfo (address, sym_start, + &li_file, &li_line); +#ifdef CONFIG_KALLSYMS_LINEINFO_MODULES + else { +struct module *mod =__module_address (address); + +if (mod) + found =module_lookup_lineinfo (mod, address, + sym_start, + &li_file, + &li_line); + } +#endif +if (found) len +=snprintf (buffer + len,KSYM_SYMBOL_LEN - len," (%s:%u)" , li_file, li_line); }
kernel/module/kallsyms.c +91 -0
94 lines not on this page · raw message
kernel/module/main.c +4 -0
@@ -2648,6 +2648,10 @@ 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 + mod->lineinfo_data =any_section_objs (info,".mod_lineinfo" ,1 , + &mod->lineinfo_data_size); +#endif #ifdef CONFIG_JUMP_LABEL mod->jump_entries = section_objs(info, "__jump_table", sizeof(*mod->jump_entries),
scripts/Makefile +1 -0
@@ -5,6 +5,7 @@ hostprogs-always-$(CONFIG_KALLSYMS) += kallsyms hostprogs-always-$(CONFIG_KALLSYMS_LINEINFO) += gen_lineinfo +hostprogs-always-$(CONFIG_KALLSYMS_LINEINFO_MODULES) += gen_lineinfo hostprogs-always-$(BUILD_C_RECORDMCOUNT) += recordmcount hostprogs-always-$(CONFIG_BUILDTIME_TABLE_SORT) += sorttable hostprogs-always-$(CONFIG_ASN1) += asn1_compiler
scripts/Makefile.modfinal +6 -0
@@ -46,6 +46,9 @@ 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 $@ + # Same as newer-prereqs, but allows to exclude specified extra dependencies newer_prereqs_except = $(filter-out $(PHONY) $(1),$?)
@@ -59,6 +62,9 @@ if_changed_except = $(if $(call newer_prereqs_except,$(2))$(cmd-check), \ +$(call if_changed_except,ld_ko_o,$(objtree)/vmlinux) 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 +48 -0
48 lines not on this page · raw message
scripts/gen_lineinfo.c +320 -29
415 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v2 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)
--- .../admin-guide/kallsyms-lineinfo.rst | 7 +- include/linux/mod_lineinfo.h | 106 ++++++++-- init/Kconfig | 8 +- kernel/kallsyms.c | 83 ++++++-- kernel/kallsyms_internal.h | 8 +- kernel/module/kallsyms.c | 104 +++++++-- scripts/gen_lineinfo.c | 199 ++++++++++++++---- scripts/kallsyms.c | 7 +- scripts/link-vmlinux.sh | 20 +- 9 files changed, 438 insertions(+), 104 deletions(-)
Documentation/admin-guide/kallsyms-lineinfo.rst +4 -3
@@ -76,10 +76,11 @@ Memory Overhead =============== The vmlinux 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). +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 10 bytes per DWARF line entry to each +Per-module lineinfo adds approximately 2-3 bytes per DWARF line entry to each ``.ko`` file. Known Limitations
include/linux/mod_lineinfo.h +89 -17
146 lines not on this page · raw message
init/Kconfig +5 -3
@@ -2065,8 +2065,9 @@ 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.
@@ -2079,7 +2080,8 @@ config KALLSYMS_LINEINFO_MODULES 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 10 bytes per DWARF line entry. + Increases .ko sizes by approximately 2-3 bytes per DWARF line + entry after delta compression. If unsure, say N.
kernel/kallsyms.c +71 -12
114 lines not on this page · raw message
kernel/kallsyms_internal.h +5 -3
@@ -17,10 +17,12 @@ extern const u8 kallsyms_seqs_of_names[];#ifdef CONFIG_KALLSYMS_LINEINFO 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 +83 -21
147 lines not on this page · raw message
scripts/gen_lineinfo.c +163 -36
261 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" , };
scripts/link-vmlinux.sh +14 -6
@@ -235,12 +235,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
Signature · 1 line
2.51.0
PATCH v2 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.
--- MAINTAINERS | 1 + kernel/kallsyms.c | 2 + lib/Kconfig.debug | 10 + lib/tests/Makefile | 3 + lib/tests/lineinfo_kunit.c | 772 +++++++++++++++++++++++++++++++++++++ 5 files changed, 788 insertions(+) create mode 100644 lib/tests/lineinfo_kunit.c
MAINTAINERS +1 -0
@@ -13733,6 +13733,7 @@ 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
kernel/kallsyms.c +2 -0
@@ -703,6 +703,7 @@ int sprint_backtrace(char *buffer, unsigned long address) {return __sprint_symbol (buffer, address,-1 ,1 ,0 ); } +EXPORT_SYMBOL_GPL (sprint_backtrace);/** * sprint_backtrace_build_id - Look up a backtrace symbol and return it in a text buffer
@@ -723,6 +724,7 @@ int sprint_backtrace_build_id(char *buffer, unsigned long address) {return __sprint_symbol (buffer, address,-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
@@ -3048,6 +3048,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
@@ -36,6 +36,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 += -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 +772 -0
772 lines not on this page · raw message
Signature · 1 line
2.51.0
Hi Sasha,
Quoted 14 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) [...]
Quoted 2 lines
Assisted-by Claude:claude-opus-4-6Signed-off-by Sasha Levin <[email protected]>
Shows relative paths and works OK on 32- and 64-bit parisc kernel. You may add
A few notes/suggestions below....
Quoted 24 lines
--- Documentation/admin-guide/index.rst | 1 + .../admin-guide/kallsyms-lineinfo.rst | 72 +++ MAINTAINERS | 6 + include/linux/kallsyms.h | 32 +- init/Kconfig | 20 + kernel/kallsyms.c | 61 +++ kernel/kallsyms_internal.h | 11 + scripts/.gitignore | 1 + scripts/Makefile | 3 + scripts/gen_lineinfo.c | 510 ++++++++++++++++++ scripts/kallsyms.c | 16 + scripts/link-vmlinux.sh | 70 ++- 12 files changed, 799 insertions(+), 4 deletions(-)create mode 100644
Documentation/admin-guide/kallsyms-lineinfo.rstcreate mode 100644scripts/gen_lineinfo.ckernel/kallsyms.c +0
-0@@ -467,6 +467,54 @@ static int append_buildid(char *buffer, const char *modname,#endif /* CONFIG_STACKTRACE_BUILD_ID */
kernel/kallsyms.c static int append_buildid(char *buffer, const char *modname, · line 478
Quoted 10 lines
kernel/kallsyms.c +9
-0+#ifdef CONFIG_KALLSYMS_LINEINFO +bool kallsyms_lookup_lineinfo (unsigned long addr,unsigned long sym_start, +const char **file,unsigned int *line) +{ +unsigned long long raw_offset; +unsigned int offset, low, high, mid, file_id; + +if (!lineinfo_num_entries) +return false;
The "#ifdef CONFIG_KALLSYMS_LINEINFO" above prevents that this function is compiled when the option is disabled.
Instead, you *could* move the "CONFIG_KALLSYMS_LINEINFO" option into the function with "IS_ENABLED()", like this...:
+ if (!IS_ENABLED(CONFIG_KALLSYMS_LINEINFO) || !lineinfo_num_entries)
+ return false;
That way your code will always be compiled, and the code will be optimized away by the compiler if the option is disabled. The huge benefit is, that the compiler will do syntax-checking at every build, so you will see coding bugs early.
You could use the same semantic at other places in your patches, and of course you then need to remove the #ifdef ...
kernel/kallsyms_internal.h extern const u16 kallsyms_token_index[]; · line 20
Quoted 11 lines
kernel/kallsyms_internal.h +3
-0@@ -15,4 +15,15 @@ extern const u16 kallsyms_token_index[];extern const unsigned int kallsyms_markers[];extern const u8 kallsyms_seqs_of_names[]; +#ifdef CONFIG_KALLSYMS_LINEINFO +extern const u32 lineinfo_num_entries; +extern const u32 lineinfo_addrs[];
^^ for exampe here...
Helge
Quoted 17 lines · Helge Deller
Hi Sasha,
Quoted 14 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) [...]
Quoted 6 lines
Quoted 2 lines
Assisted-by Claude:claude-opus-4-6Signed-off-by Sasha Levin <[email protected]>Shows relative paths and works OK on 32- and 64-bit parisc kernel. You may add
Tested-by Helge Deller <[email protected]>
Thanks for testing! I'm happy the regression was fixed.
Quoted 44 lines
A few notes/suggestions below....
Quoted 22 lines
--- Documentation/admin-guide/index.rst | 1 + .../admin-guide/kallsyms-lineinfo.rst | 72 +++ MAINTAINERS | 6 + include/linux/kallsyms.h | 32 +- init/Kconfig | 20 + kernel/kallsyms.c | 61 +++ kernel/kallsyms_internal.h | 11 + scripts/.gitignore | 1 + scripts/Makefile | 3 + scripts/gen_lineinfo.c | 510 ++++++++++++++++++ scripts/kallsyms.c | 16 + scripts/link-vmlinux.sh | 70 ++- 12 files changed, 799 insertions(+), 4 deletions(-) create mode 100644 Documentation/admin-guide/kallsyms-lineinfo.rst create mode 100644 scripts/gen_lineinfo.ckernel/kallsyms.c +0
-0kernel/kallsyms.c static int append_buildid(char *buffer, const char *modname, · line 476
Quoted 10 lines
kernel/kallsyms.c +9
-0@@ -467,6 +467,54 @@ static int append_buildid(char *buffer, const char *modname,#endif /* CONFIG_STACKTRACE_BUILD_ID */ +#ifdef CONFIG_KALLSYMS_LINEINFO +bool kallsyms_lookup_lineinfo (unsigned long addr,unsigned long sym_start, +const char **file,unsigned int *line) +{ +unsigned long long raw_offset; +unsigned int offset, low, high, mid, file_id; + +if (!lineinfo_num_entries) +return false;The "#ifdef
CONFIG_KALLSYMS_LINEINFO" above prevents that this function is compiled when the option is disabled.Instead, you *could* move the "
CONFIG_KALLSYMS_LINEINFO" option into the function with "IS_ENABLED()", like this...:(quoted diff) +2
-0+ if (!IS_ENABLED(CONFIG_KALLSYMS_LINEINFO) || !lineinfo_num_entries) + return false;
Quoted 7 lines
That way your code will always be compiled, and the code will be optimized away by the compiler if the option is disabled. The huge benefit is, that the compiler will do syntax-checking at every build, so you will see coding bugs early.
You could use the same semantic at other places in your patches, and of course you then need to remove the #ifdef ...
Ack, this makes sense. I'll do it in the v3 if it's needed.
Signature · 2 lines
Thanks, Sasha