[RFC PATCH 09/12] arm64/module, unwind: add .lr_unwind support for modules

From: Dylan Hatch

Date: Mon Nov 16 2026 - 23:35:57 EST


Generate .lr_unwind_fde and .lr_unwind_ranges sections for loadable
kernel modules at modfinal link time, sort each module's FDE table in
module_finalize(), and support module IP lookups in lr_unwind_find().

Co-developed-by: Weinan Liu <wnliu@xxxxxxxxxx>
Signed-off-by: Weinan Liu <wnliu@xxxxxxxxxx>
Suggested-by: Jens Remus <jremus@xxxxxxxxxxxxx>
Signed-off-by: Dylan Hatch <dylanbhatch@xxxxxxxxxx>
---
arch/arm64/include/asm/lr_unwind.h | 7 ++
arch/arm64/include/asm/module.h | 6 ++
arch/arm64/kernel/lr_unwind.c | 125 ++++++++++++++++++++++++++++-
arch/arm64/kernel/module.c | 11 +++
scripts/Makefile.modfinal | 10 ++-
scripts/gen_lr_unwind.c | 4 +-
6 files changed, 157 insertions(+), 6 deletions(-)

diff --git a/arch/arm64/include/asm/lr_unwind.h b/arch/arm64/include/asm/lr_unwind.h
index bfb6abed05f76..649009b49becf 100644
--- a/arch/arm64/include/asm/lr_unwind.h
+++ b/arch/arm64/include/asm/lr_unwind.h
@@ -113,6 +113,8 @@ static __always_inline void lr_fde_unpack_inline(u32 desc, lr_range_t ranges[4])
#include <linux/errno.h>
#include <linux/init.h>

+struct module;
+
struct lr_unwind_section {
const struct lr_fde *fdes;
unsigned int num_fdes;
@@ -123,12 +125,17 @@ struct lr_unwind_section {
#ifdef CONFIG_ARM64_LR_UNWIND

void __init init_lr_unwind_table(void);
+void lr_unwind_module_init(struct module *mod, void *fdes, size_t fdes_size,
+ const void *ranges, size_t ranges_size);
int lr_unwind_find(unsigned long ip, enum lr_unwind_mode *mode,
unsigned int *sp_off);

#else /* !CONFIG_ARM64_LR_UNWIND */

static inline void __init init_lr_unwind_table(void) {}
+static inline void lr_unwind_module_init(struct module *mod, void *fdes,
+ size_t fdes_size, const void *ranges,
+ size_t ranges_size) {}
static inline int lr_unwind_find(unsigned long ip, enum lr_unwind_mode *mode,
unsigned int *sp_off)
{
diff --git a/arch/arm64/include/asm/module.h b/arch/arm64/include/asm/module.h
index fb9b88eebeb15..3b6457875e453 100644
--- a/arch/arm64/include/asm/module.h
+++ b/arch/arm64/include/asm/module.h
@@ -6,6 +6,7 @@
#define __ASM_MODULE_H

#include <asm-generic/module.h>
+#include <asm/lr_unwind.h>

struct mod_plt_sec {
int plt_shndx;
@@ -17,6 +18,11 @@ struct mod_arch_specific {
struct mod_plt_sec core;
struct mod_plt_sec init;

+#ifdef CONFIG_ARM64_LR_UNWIND
+ struct lr_unwind_section lr_unwind_sec;
+ bool lr_unwind_init;
+#endif
+
/* for CONFIG_DYNAMIC_FTRACE */
struct plt_entry *ftrace_trampolines;
struct plt_entry *init_ftrace_trampolines;
diff --git a/arch/arm64/kernel/lr_unwind.c b/arch/arm64/kernel/lr_unwind.c
index 53e2c78a8bdaf..d554c2e1e57e2 100644
--- a/arch/arm64/kernel/lr_unwind.c
+++ b/arch/arm64/kernel/lr_unwind.c
@@ -5,11 +5,15 @@

#define pr_fmt(fmt) "lr_unwind: " fmt

+#include <linux/cleanup.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/kprobes.h>
#include <linux/limits.h>
+#include <linux/module.h>
#include <linux/printk.h>
+#include <linux/rcupdate.h>
+#include <linux/sort.h>

#include <asm/lr_unwind.h>
#include <asm/sections.h>
@@ -17,10 +21,20 @@
static bool lr_unwind_init __ro_after_init;
static struct lr_unwind_section kernel_lr_sec __ro_after_init;

+#ifdef CONFIG_MODULES
+static const char *objname(const struct lr_unwind_section *sec)
+{
+ if (sec == &kernel_lr_sec)
+ return "vmlinux";
+
+ return container_of(sec, struct module, arch.lr_unwind_sec)->name;
+}
+#else
static const char *objname(const struct lr_unwind_section *sec)
{
return "vmlinux";
}
+#endif

#define dbg_sec(sec, fmt, ...) \
pr_debug("(%s): " fmt, objname(sec), ##__VA_ARGS__)
@@ -55,10 +69,36 @@ static __always_inline bool is_kernel_ip_valid(unsigned long ip)
return false;
}

+#ifdef CONFIG_MODULES
+static __always_inline bool within_mod_mem(unsigned long ip,
+ const struct module_memory *mem)
+{
+ return ip - (unsigned long)mem->base < mem->size;
+}
+
+static __always_inline bool
+is_sec_module_ip_valid(const struct lr_unwind_section *sec, unsigned long ip)
+{
+ struct module *mod = container_of(sec, struct module, arch.lr_unwind_sec);
+
+ return within_mod_mem(ip, &mod->mem[MOD_TEXT]) ||
+ within_mod_mem(ip, &mod->mem[MOD_INIT_TEXT]);
+}
+#else
+static __always_inline bool
+is_sec_module_ip_valid(const struct lr_unwind_section *sec, unsigned long ip)
+{
+ return false;
+}
+#endif
+
static __always_inline bool is_sec_ip_valid(const struct lr_unwind_section *sec,
unsigned long ip)
{
- return is_kernel_ip_valid(ip);
+ if (sec == &kernel_lr_sec)
+ return is_kernel_ip_valid(ip);
+
+ return is_sec_module_ip_valid(sec, ip);
}

static __always_inline int __find_fde(const struct lr_unwind_section *sec,
@@ -151,9 +191,31 @@ static __always_inline int __lr_unwind_find(const struct lr_unwind_section *sec,
return -EINVAL;
}

+#ifdef CONFIG_MODULES
+static noinline int __lr_unwind_find_module(unsigned long ip,
+ enum lr_unwind_mode *mode,
+ unsigned int *sp_off)
+{
+ struct module *mod;
+
+ guard(preempt_notrace)();
+
+ mod = __module_text_address(ip);
+ /* Pairs with smp_store_release() in lr_unwind_module_init() */
+ if (mod && smp_load_acquire(&mod->arch.lr_unwind_init))
+ return __lr_unwind_find(&mod->arch.lr_unwind_sec,
+ ip, mode, sp_off);
+
+ return -EINVAL;
+}
+NOKPROBE_SYMBOL(__lr_unwind_find_module);
+#endif
+
noinstr int lr_unwind_find(unsigned long ip, enum lr_unwind_mode *mode,
unsigned int *sp_off)
{
+ int ret = -EINVAL;
+
if (!mode || !sp_off)
return -EINVAL;

@@ -164,7 +226,16 @@ noinstr int lr_unwind_find(unsigned long ip, enum lr_unwind_mode *mode,
return __lr_unwind_find(&kernel_lr_sec, ip, mode, sp_off);
}

- return -EINVAL;
+#ifdef CONFIG_MODULES
+ if (!rcu_is_watching())
+ return -EINVAL;
+
+ instrumentation_begin();
+ ret = __lr_unwind_find_module(ip, mode, sp_off);
+ instrumentation_end();
+#endif
+
+ return ret;
}
NOKPROBE_SYMBOL(lr_unwind_find);

@@ -272,3 +343,53 @@ void __init init_lr_unwind_table(void)
lr_unwind_init = true;
dbg_print_section(&kernel_lr_sec);
}
+
+#ifdef CONFIG_MODULES
+
+static int lr_unwind_sort_cmp_fde(const void *a, const void *b)
+{
+ return cmp_int(lr_fde_start_addr(a), lr_fde_start_addr(b));
+}
+
+static void lr_unwind_sort_swap_fde(void *a, void *b, int size)
+{
+ struct lr_fde *x = a, *y = b, tmp = *x;
+ int delta = b - a;
+
+ x->start_offset = y->start_offset + delta;
+ x->range_desc = y->range_desc;
+ y->start_offset = tmp.start_offset - delta;
+ y->range_desc = tmp.range_desc;
+}
+
+void lr_unwind_module_init(struct module *mod, void *fdes, size_t fdes_size,
+ const void *ranges, size_t ranges_size)
+{
+ struct lr_unwind_section *sec = &mod->arch.lr_unwind_sec;
+
+ if (!fdes_size || (fdes_size % sizeof(struct lr_fde)) ||
+ (ranges_size % sizeof(lr_range_t))) {
+ pr_warn("invalid .lr_unwind section sizes in module %s\n",
+ mod->name);
+ return;
+ }
+
+ sec->fdes = fdes;
+ sec->num_fdes = fdes_size / sizeof(struct lr_fde);
+ sec->ranges = ranges;
+ sec->num_ranges = ranges_size / sizeof(lr_range_t);
+
+ sort(fdes, sec->num_fdes, sizeof(struct lr_fde),
+ lr_unwind_sort_cmp_fde, lr_unwind_sort_swap_fde);
+
+ if (lr_unwind_validate_section(sec)) {
+ pr_warn("invalid .lr_unwind section in module %s\n", mod->name);
+ return;
+ }
+
+ /* Ensure table is initialized before lr_unwind_find() observes it */
+ smp_store_release(&mod->arch.lr_unwind_init, true);
+ dbg_print_section(sec);
+}
+
+#endif /* CONFIG_MODULES */
diff --git a/arch/arm64/kernel/module.c b/arch/arm64/kernel/module.c
index 24adb581af0eb..685b4301df96e 100644
--- a/arch/arm64/kernel/module.c
+++ b/arch/arm64/kernel/module.c
@@ -21,6 +21,7 @@

#include <asm/alternative.h>
#include <asm/insn.h>
+#include <asm/lr_unwind.h>
#include <asm/scs.h>
#include <asm/sections.h>
#include <asm/text-patching.h>
@@ -515,5 +516,15 @@ int module_finalize(const Elf_Ehdr *hdr,
}
}

+ s = find_section(hdr, sechdrs, ".lr_unwind_fde");
+ if (s) {
+ const Elf_Shdr *r = find_section(hdr, sechdrs,
+ ".lr_unwind_ranges");
+
+ lr_unwind_module_init(me, (void *)s->sh_addr, s->sh_size,
+ r ? (const void *)r->sh_addr : NULL,
+ r ? r->sh_size : 0);
+ }
+
return module_init_ftrace_plt(hdr, sechdrs, me);
}
diff --git a/scripts/Makefile.modfinal b/scripts/Makefile.modfinal
index 01a37ec872b90..301393dbb9dd3 100644
--- a/scripts/Makefile.modfinal
+++ b/scripts/Makefile.modfinal
@@ -32,11 +32,17 @@ ifneq ($(WARN_ON_UNUSED_TRACEPOINTS),)
cmd_check_tracepoint = $(objtree)/scripts/tracepoint-update --module $<;
endif

+ifdef CONFIG_ARM64_LR_UNWIND
+cmd_gen_lr_unwind = ; $(objtree)/scripts/gen_lr_unwind $@ && \
+ $(OBJCOPY) --remove-section=.eh_frame --remove-section=.rela.eh_frame $@
+endif
+
quiet_cmd_ld_ko_o = LD [M] $@
cmd_ld_ko_o = \
$(LD) -r $(KBUILD_LDFLAGS) \
$(KBUILD_LDFLAGS_MODULE) $(LDFLAGS_MODULE) \
- -T $(objtree)/scripts/module.lds -o $@ $(filter %.o, $^)
+ -T $(objtree)/scripts/module.lds -o $@ $(filter %.o, $^) \
+ $(cmd_gen_lr_unwind)

quiet_cmd_btf_ko = BTF [M] $@
cmd_btf_ko = \
@@ -47,7 +53,7 @@ quiet_cmd_btf_ko = BTF [M] $@
fi;

# 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
+%.ko: %.o %.mod.o .module-common.o $(objtree)/scripts/module.lds $(if $(CONFIG_ARM64_LR_UNWIND),$(objtree)/scripts/gen_lr_unwind) $(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))
diff --git a/scripts/gen_lr_unwind.c b/scripts/gen_lr_unwind.c
index 2058601ec6135..cc6b8790e00d8 100644
--- a/scripts/gen_lr_unwind.c
+++ b/scripts/gen_lr_unwind.c
@@ -797,12 +797,12 @@ int main(int argc, char *argv[])
goto out;
}

- if (!ctx.eh_sec || !ctx.rela_eh_sec || !ctx.symtab_shndx) {
+ if (!ctx.eh_sec || !ctx.rela_eh_sec) {
ret = 0;
goto out;
}

- if (!parse_eh_frame(&ctx) &&
+ if (ctx.symtab_shndx && !parse_eh_frame(&ctx) &&
(!ctx.nr_fdes ||
!emit_lr_unwind_sections(fname, &ctx, shnum, shstrndx)))
ret = 0;
--
2.56.0.679.gc527c65306-goog