All of lore.kernel.org
 help / color / mirror / Atom feed
From: Dylan Hatch <dylanbhatch@google.com>
To: Catalin Marinas <catalin.marinas@arm.com>,
	Will Deacon <will@kernel.org>,
	 Mark Rutland <mark.rutland@arm.com>
Cc: Herbert Xu <herbert@gondor.apana.org.au>,
	"David S . Miller" <davem@davemloft.net>,
	 Nathan Chancellor <nathan@kernel.org>,
	Nicolas Schier <nsc@kernel.org>,
	 Nick Desaulniers <ndesaulniers@google.com>,
	Bill Wendling <morbo@google.com>,
	 Justin Stitt <justinstitt@google.com>,
	Weinan Liu <wnliu@google.com>,  Jens Remus <jremus@linux.ibm.com>,
	linux-arm-kernel@lists.infradead.org,
	 linux-kernel@vger.kernel.org, linux-kbuild@vger.kernel.org,
	 linux-crypto@vger.kernel.org, llvm@lists.linux.dev,
	 Josh Poimboeuf <jpoimboe@kernel.org>,
	Steven Rostedt <rostedt@goodmis.org>,
	 Ard Biesheuvel <ardb@kernel.org>,
	Indu Bhagat <ibhagatgnu@gmail.com>,
	 Prasanna Kumar T S M <ptsm@linux.microsoft.com>,
	live-patching@vger.kernel.org,
	 Dylan Hatch <dylanbhatch@google.com>
Subject: [RFC PATCH 09/12] arm64/module, unwind: add .lr_unwind support for modules
Date: Tue, 17 Nov 2026 04:35:01 +0000	[thread overview]
Message-ID: <20261117043504.1021063-10-dylanbhatch@google.com> (raw)
In-Reply-To: <20261117043504.1021063-1-dylanbhatch@google.com>

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@google.com>
Signed-off-by: Weinan Liu <wnliu@google.com>
Suggested-by: Jens Remus <jremus@linux.ibm.com>
Signed-off-by: Dylan Hatch <dylanbhatch@google.com>
---
 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


  parent reply	other threads:[~2026-11-17  4:35 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17  4:34 [RFC PATCH 00/12] arm64, unwind: add .lr_unwind unwinder for reliable stacktrace Dylan Hatch
2026-11-17  4:34 ` [RFC PATCH 01/12] arm64, unwind: build with .eh_frame and add gen_lr_unwind skeleton Dylan Hatch
2026-11-18  4:36   ` sashiko-bot
2026-11-17  4:34 ` [RFC PATCH 02/12] arm64: factor .eh_frame parser out of patch-scs.c into <asm/eh_frame.h> Dylan Hatch
2026-11-18  4:36   ` sashiko-bot
2026-11-17  4:34 ` [RFC PATCH 03/12] arm64: eh_frame: track CFA and x29/x30 register state in CFI parser Dylan Hatch
2026-11-17  4:34 ` [RFC PATCH 04/12] arm64, unwind: parse .eh_frame into .lr_unwind entries in gen_lr_unwind Dylan Hatch
2026-11-17  4:34 ` [RFC PATCH 05/12] scripts/gen_lr_unwind: emit .lr_unwind_fde and .lr_unwind_ranges Dylan Hatch
2026-11-17  4:34 ` [RFC PATCH 06/12] scripts/gen_lr_unwind: add --dump option to inspect .lr_unwind tables Dylan Hatch
2026-11-18  4:36   ` sashiko-bot
2026-11-17  4:34 ` [RFC PATCH 07/12] scripts/sorttable: sort .lr_unwind_fde by start_offset Dylan Hatch
2026-11-17  4:35 ` [RFC PATCH 08/12] arm64, unwind: add in-kernel .lr_unwind lookup and validation Dylan Hatch
2026-11-17  4:35 ` Dylan Hatch [this message]
2026-11-17  4:35 ` [RFC PATCH 10/12] arm64: entry: add unwind info for call_on_irq_stack() Dylan Hatch
2026-11-17  4:35 ` [RFC PATCH 11/12] arm64, crypto/lib: annotate leaf functions with CFI info Dylan Hatch
2026-11-18  5:07   ` Eric Biggers
2026-11-17  4:35 ` [RFC PATCH 12/12] unwind: arm64: use .lr_unwind to unwind interrupt frames Dylan Hatch
2026-11-18  4:36   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261117043504.1021063-10-dylanbhatch@google.com \
    --to=dylanbhatch@google.com \
    --cc=ardb@kernel.org \
    --cc=catalin.marinas@arm.com \
    --cc=davem@davemloft.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=ibhagatgnu@gmail.com \
    --cc=jpoimboe@kernel.org \
    --cc=jremus@linux.ibm.com \
    --cc=justinstitt@google.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=live-patching@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=mark.rutland@arm.com \
    --cc=morbo@google.com \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=nsc@kernel.org \
    --cc=ptsm@linux.microsoft.com \
    --cc=rostedt@goodmis.org \
    --cc=will@kernel.org \
    --cc=wnliu@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.