Skip to contentSkip to conversation

unwind, arm64: add sframe unwinder for kernel

PATCH Superseded Dylan Hatch 2 participants 16 messages Apr 21 – Apr 27

Subsystems (7)

v4 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v4 cover unwind, arm64: add sframe unwinder for kernel

Implement a generic kernel sframe-based [1] unwinder. The main goal is to improve reliable stacktrace on arm64 by unwinding across exception boundaries.

On x86, the ORC unwinder provides reliable stacktrace through similar methodology, but arm64 lacks the necessary support from objtool to create ORC unwind tables.

Currently, there's already a sframe unwinder proposed for userspace: [2]. To maintain common definitions and algorithms for sframe lookup, a substantial portion of this patch series aims to refactor the sframe lookup code to support both kernel and userspace sframe sections.

Currently, only GNU Binutils support sframe. This series relies on the Sframe V3 format, which is supported in binutils 2.46.

These patches are based on Steven Rostedt's sframe/core branch [3], which is and aggregation of existing work done for x86 sframe userspace unwind, and contains [2]. This branch is, in turn, based on Linux v7.0-rc3. This full series (applied to the sframe/core branch) is available on github: [4].

Ref:
[1]: https://sourceware.org/binutils/docs/sframe-spec.html
[2]: https://lore.kernel.org/lkml/[email protected]/
[3]: https://git.kernel.org/pub/scm/linux/kernel/git/rostedt/linux-trace.git/log/?h=sframe/core
[4]: https://github.com/dylanbhatch/linux/tree/sframe-v3-with-v3

Changes since v3:

- (Jens) Clean up patch summaries.
- (Jens) Rename SFRAME_LOOKUP -> UNWIND_SFRAME_LOOKUP to fit existing naming convention.
- (Randy) Correct typo errors in new config options.
- (Jens) Move unwind types to a new unwind_types.h to match their usage.
- (Jens) Update KERNEL_[COPY|GET] to use label-based error handling like their userspace counterparts.
- (Jens) Rename SFRAME_UNWINDER -> HAVE_UNWIND_KERNEL_SFRAME and ARCH_SUPPORTS_SFRAME_UNWINDER -> ARCH_SUPPORTS_UNWIND_KERNEL_SFRAME to match existing naming convention.
- (Jens) Move HAVE_UNWIND_KERNEL_SFRAME config option to arch/Kconfig.
- (Jens) Rename/move extern definitions of __[start|end]_sframe into include/asm-generic/sections.h.
- (Jens) Fix up CFI annotations at kernel entry.
- (Jens) Fix error path for unsorted FDE lookup.
- (Jens) Zero-out module sframe_section before init.
- (Jens) For SFRAME_VALIDATION, use an arch-specific function-address validation helper so that .rodata.text can be correctly handled on arm64 vmlinux.
- (Jens) Fixup and better comment kernel stacktrace code.

Changes since v2:

The biggest change from v2 is the switch from adding a dedicated, in-kernel sframe-lookup library, to refactoring/using the existing library developed by Josh, Jens, and Steve. Consequently, this series now depends on Sframe V3, though this upgrade would likely have been necessary anyway. Below is a full accounting of the changes since v2.

- (Josh) Add stricter reliability checks during unwind.
- (Puranjay, Indu, Jens) Update to use a common sframe library with userpace unwind, thus resolving the need to support SFRAME_F_FDE_FUNC_START_PCREL, added in binutils 2.45.
- (Jens) Add check for sframe V3, thus resolving the prior need for V2 and SFRAME_F_FDE_FUNC_START_PCREL support.
- (Will) Add ARCH_SUPPORTS_SFRAME_UNWINDER, remove SFRAME_UNWIND_TABLE
- (Indu) add support for unsorted FDE tables, allowing for module sframe lookups.
- (Mark) Prefer frame-pointer unwind when possible, for better performance.
- Simplify compile-time logic, adding stubbs when necessary.
- Add support for in-kernel SFRAME_VALIDATION.
- Rebase onto core/sframe (with v7.0-rc3 base)

Dylan Hatch (7):
sframe: Allow kernelspace sframe sections arm64, unwind: build kernel with sframe V3 info
sframe: Provide PC lookup for vmlinux .sframe section
sframe: Allow unsorted FDEs
arm64/module, sframe: Add sframe support for modules
sframe: Introduce in-kernel SFRAME_VALIDATION
unwind: arm64: Use sframe to unwind interrupt frames

Weinan Liu (1):
arm64: entry: add unwind info for various kernel entries

 MAINTAINERS                                   |   3 +-
 Makefile                                      |   8 +
 arch/Kconfig                                  |  27 +-
 arch/arm64/Kconfig                            |   1 +
 arch/arm64/include/asm/module.h               |   6 +
 arch/arm64/include/asm/sections.h             |   1 +
 arch/arm64/include/asm/stacktrace/common.h    |   6 +
 arch/arm64/include/asm/unwind_sframe.h        |  29 ++
 arch/arm64/kernel/entry.S                     |  23 +
 arch/arm64/kernel/module.c                    |   8 +
 arch/arm64/kernel/setup.c                     |   2 +
 arch/arm64/kernel/stacktrace.c                | 246 ++++++++++-
 arch/arm64/kernel/vdso/Makefile               |   2 +-
 arch/arm64/kernel/vmlinux.lds.S               |   2 +
 .../{unwind_user_sframe.h => unwind_sframe.h} |   6 +-
 arch/x86/include/asm/unwind_user.h            |  12 +-
 include/asm-generic/sections.h                |   4 +
 include/asm-generic/vmlinux.lds.h             |  15 +
 include/linux/sframe.h                        |  67 ++-
 include/linux/unwind_types.h                  |  46 ++
 include/linux/unwind_user_types.h             |  41 --
 kernel/unwind/Makefile                        |   2 +-
 kernel/unwind/sframe.c                        | 410 ++++++++++++++----
 kernel/unwind/user.c                          |  41 +-
 24 files changed, 801 insertions(+), 207 deletions(-)
 create mode 100644 arch/arm64/include/asm/unwind_sframe.h
 rename arch/x86/include/asm/{unwind_user_sframe.h => unwind_sframe.h} (50%)
 create mode 100644 include/linux/unwind_types.h
Signature · 1 line
2.54.0.rc1.555.g9c883467ad-goog
#1 lore raw

PATCH v4 1/8 sframe: Allow kernelspace sframe sections

Generalize the sframe lookup code to support kernelspace sections. This is done by defining a SFRAME_LOOKUP option that can be activated separate from HAVE_UNWIND_USER_SFRAME, as there will be other client to this library than just userspace unwind.

Sframe section location is now tracked in a separate sec_type field to determine whether user-access functions are necessary to read the sframe data. Relevant type delarations are moved and renamed to reflect the non-user sframe support.

---
 MAINTAINERS                                   |   2 +-
 arch/Kconfig                                  |   4 +
 .../{unwind_user_sframe.h => unwind_sframe.h} |   6 +-
 arch/x86/include/asm/unwind_user.h            |  12 +-
 include/linux/sframe.h                        |  48 ++--
 include/linux/unwind_types.h                  |  46 +++
 include/linux/unwind_user_types.h             |  41 ---
 kernel/unwind/Makefile                        |   2 +-
 kernel/unwind/sframe.c                        | 270 ++++++++++++------
 kernel/unwind/user.c                          |  41 +--
 10 files changed, 293 insertions(+), 179 deletions(-)
 rename arch/x86/include/asm/{unwind_user_sframe.h => unwind_sframe.h} (50%)
 create mode 100644 include/linux/unwind_types.h
MAINTAINERS +1 -1
index 8c46465ee7a9..cfc7dec88da4 100644
@@ -27557,7 +27557,7 @@ F:	Documentation/driver-api/uio-howto.rst
 F:	drivers/uio/
 F:	include/linux/uio_driver.h
 
-USERSPACE STACK UNWINDING
+STACK UNWINDING
 M:	Josh Poimboeuf <[email protected]>
 M:	Steven Rostedt <[email protected]>
 S:	Maintained
arch/Kconfig +4 -0
index f1ed8bc0806d..d7caf2e245ce 100644
@@ -486,6 +486,9 @@ config AS_SFRAME3
 	def_bool $(as-instr,.cfi_startproc\n.cfi_endproc,-Wa$(comma)--gsframe-3)
 	select AS_SFRAME
 
+config UNWIND_SFRAME_LOOKUP
+	bool
+
 config UNWIND_USER
 	bool
 
@@ -496,6 +499,7 @@ config HAVE_UNWIND_USER_FP
 config HAVE_UNWIND_USER_SFRAME
 	bool
 	select UNWIND_USER
+	select UNWIND_SFRAME_LOOKUP
 
 config SFRAME_VALIDATION
 	bool "Enable .sframe section debugging"
arch/x86/include/asm/unwind_sframe.h renamed from arch/x86/include/asm/unwind_user_sframe.h +3 -3
similarity index 50%
rename from arch/x86/include/asm/unwind_user_sframe.h
rename to arch/x86/include/asm/unwind_sframe.h
index d828ae1a4aac..44d42e6ffde4 100644
@@ -1,6 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0 */
-#ifndef _ASM_X86_UNWIND_USER_SFRAME_H
-#define _ASM_X86_UNWIND_USER_SFRAME_H
+#ifndef _ASM_X86_UNWIND_SFRAME_H
+#define _ASM_X86_UNWIND_SFRAME_H
 
 #ifdef CONFIG_X86_64
 
@@ -9,4 +9,4 @@
 
 #endif
 
-#endif /* _ASM_X86_UNWIND_USER_SFRAME_H */
+#endif /* _ASM_X86_UNWIND_SFRAME_H */
arch/x86/include/asm/unwind_user.h +6 -6
index ae46906c3b39..8fdab3581b86 100644
@@ -55,30 +55,30 @@ static inline int unwind_user_get_reg(unsigned long *val, unsigned int regnum)
 
 #define ARCH_INIT_USER_FP_FRAME(ws)			\
 	.cfa		= {				\
-		.rule		= UNWIND_USER_CFA_RULE_FP_OFFSET,\
+		.rule		= UNWIND_CFA_RULE_FP_OFFSET,\
 		.offset		=  2*(ws),		\
 			},				\
 	.ra		= {				\
-		.rule		= UNWIND_USER_RULE_CFA_OFFSET_DEREF,\
+		.rule		= UNWIND_RULE_CFA_OFFSET_DEREF,\
 		.offset		= -1*(ws),		\
 			},				\
 	.fp		= {				\
-		.rule		= UNWIND_USER_RULE_CFA_OFFSET_DEREF,\
+		.rule		= UNWIND_RULE_CFA_OFFSET_DEREF,\
 		.offset		= -2*(ws),		\
 			},				\
 	.outermost	= false,
 
 #define ARCH_INIT_USER_FP_ENTRY_FRAME(ws)		\
 	.cfa		= {				\
-		.rule		= UNWIND_USER_CFA_RULE_SP_OFFSET,\
+		.rule		= UNWIND_CFA_RULE_SP_OFFSET,\
 		.offset		=  1*(ws),		\
 			},				\
 	.ra		= {				\
-		.rule		= UNWIND_USER_RULE_CFA_OFFSET_DEREF,\
+		.rule		= UNWIND_RULE_CFA_OFFSET_DEREF,\
 		.offset		= -1*(ws),		\
 			},				\
 	.fp		= {				\
-		.rule		= UNWIND_USER_RULE_RETAIN,\
+		.rule		= UNWIND_RULE_RETAIN,\
 			},				\
 	.outermost	= false,
 
include/linux/sframe.h +30 -18

82 lines not on this page · raw message

include/linux/unwind_types.h new file +46 -0

46 lines not on this page · raw message

include/linux/unwind_user_types.h +0 -41

47 lines not on this page · raw message

kernel/unwind/Makefile +1 -1
index 146038165865..c5f9f8124564 100644
@@ -1,2 +1,2 @@
  obj-$(CONFIG_UNWIND_USER)		+= user.o deferred.o
- obj-$(CONFIG_HAVE_UNWIND_USER_SFRAME)	+= sframe.o
+ obj-$(CONFIG_UNWIND_SFRAME_LOOKUP)	+= sframe.o
kernel/unwind/sframe.c +181 -89

480 lines not on this page · raw message

kernel/unwind/user.c +21 -20

124 lines not on this page · raw message

Signature · 1 line
2.54.0.rc1.555.g9c883467ad-goog
#4 lore raw

PATCH v4 2/8 arm64, unwind: build kernel with sframe V3 info

In reply to Dylan Hatch

Build with -Wa,--gsframe-3 flags to generate a .sframe section. This will be used for in-kernel reliable stacktrace in cases where the frame pointer alone is insufficient.

Currently, the sframe format only supports arm64, x86_64 and s390x architectures.

---
 MAINTAINERS                            |  1 +
 Makefile                               |  8 ++++++++
 arch/Kconfig                           | 21 +++++++++++++++++++++
 arch/arm64/Kconfig                     |  1 +
 arch/arm64/include/asm/unwind_sframe.h |  8 ++++++++
 arch/arm64/kernel/vdso/Makefile        |  2 +-
 include/asm-generic/sections.h         |  4 ++++
 include/asm-generic/vmlinux.lds.h      | 15 +++++++++++++++
 8 files changed, 59 insertions(+), 1 deletion(-)
 create mode 100644 arch/arm64/include/asm/unwind_sframe.h
MAINTAINERS +1 -0
index cfc7dec88da4..a7d75f9cb5f4 100644
@@ -27561,6 +27561,7 @@ STACK UNWINDING
 M:	Josh Poimboeuf <[email protected]>
 M:	Steven Rostedt <[email protected]>
 S:	Maintained
+F:	arch/*/include/asm/unwind_sframe.h
 F:	include/linux/sframe.h
 F:	include/linux/unwind*.h
 F:	kernel/unwind/
Makefile +8 -0
index 2b15f0b4a0cb..6c94a5257679 100644
@@ -1110,6 +1110,14 @@ endif
 # Ensure compilers do not transform certain loops into calls to wcslen()
 KBUILD_CFLAGS += -fno-builtin-wcslen
 
+# build with sframe table
+ifdef CONFIG_HAVE_UNWIND_KERNEL_SFRAME
+CC_FLAGS_SFRAME := -Wa,--gsframe-3
+KBUILD_CFLAGS	+= $(CC_FLAGS_SFRAME)
+KBUILD_AFLAGS	+= $(CC_FLAGS_SFRAME)
+export CC_FLAGS_SFRAME
+endif
+
 # change __FILE__ to the relative path to the source directory
 ifdef building_out_of_srctree
 KBUILD_CPPFLAGS += -fmacro-prefix-map=$(srcroot)/=
arch/Kconfig +21 -0
index d7caf2e245ce..8d27b3249e7a 100644
@@ -520,6 +520,27 @@ config SFRAME_VALIDATION
 
 	  If unsure, say N.
 
+config ARCH_SUPPORTS_UNWIND_KERNEL_SFRAME
+	bool
+	help
+	  An architecture can select this if it enables the SFrame (Simple
+	  Frame) unwinder for unwinding kernel stack traces. It uses an unwind
+	  table that is directly generated by the toolchain based on DWARF CFI
+	  information.
+
+config HAVE_UNWIND_KERNEL_SFRAME
+	bool "Sframe unwinder"
+	depends on AS_SFRAME3
+	depends on 64BIT
+	depends on ARCH_SUPPORTS_UNWIND_KERNEL_SFRAME
+	select UNWIND_SFRAME_LOOKUP
+	help
+	  This option enables the SFrame (Simple Frame) unwinder for unwinding
+	  kernel stack traces. It uses unwind an table that is directly
+	  generated by the toolchain based on DWARF CFI information. In
+	  practice, this can provide more reliable stacktrace results than
+	  unwinding with frame pointers alone.
+
 config HAVE_PERF_REGS
 	bool
 	help
arch/arm64/Kconfig +1 -0
index 38dba5f7e4d2..f7ae8eaaadc4 100644
@@ -112,6 +112,7 @@ config ARM64
 	select ARCH_SUPPORTS_SCHED_SMT
 	select ARCH_SUPPORTS_SCHED_CLUSTER
 	select ARCH_SUPPORTS_SCHED_MC
+	select ARCH_SUPPORTS_UNWIND_KERNEL_SFRAME
 	select ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
 	select ARCH_WANT_COMPAT_IPC_PARSE_VERSION if COMPAT
 	select ARCH_WANT_DEFAULT_BPF_JIT
arch/arm64/include/asm/unwind_sframe.h new file +8 -0
new file mode 100644
index 000000000000..876412881196
@@ -0,0 +1,8 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_ARM64_UNWIND_SFRAME_H
+#define _ASM_ARM64_UNWIND_SFRAME_H
+
+#define SFRAME_REG_SP	31
+#define SFRAME_REG_FP	29
+
+#endif /* _ASM_ARM64_UNWIND_SFRAME_H */
arch/arm64/kernel/vdso/Makefile +1 -1
index 7dec05dd33b7..c60ef921956f 100644
@@ -38,7 +38,7 @@ ccflags-y += -DDISABLE_BRANCH_PROFILING -DBUILD_VDSO
 CC_FLAGS_REMOVE_VDSO := $(CC_FLAGS_FTRACE) -Os $(CC_FLAGS_SCS) \
 			$(RANDSTRUCT_CFLAGS) $(KSTACK_ERASE_CFLAGS) \
 			$(GCC_PLUGINS_CFLAGS) \
-			$(CC_FLAGS_LTO) $(CC_FLAGS_CFI) \
+			$(CC_FLAGS_LTO) $(CC_FLAGS_CFI) $(CC_FLAGS_SFRAME) \
 			-Wmissing-prototypes -Wmissing-declarations
 
 CC_FLAGS_ADD_VDSO := -O2 -mcmodel=tiny -fasynchronous-unwind-tables
include/asm-generic/sections.h +4 -0
index 0755bc39b0d8..336d27011a58 100644
@@ -31,6 +31,7 @@
  *	__irqentry_text_start, __irqentry_text_end
  *	__softirqentry_text_start, __softirqentry_text_end
  *	__start_opd, __end_opd
+ *	__start_sframe, __end_sframe
  */
 extern char _text[], _stext[], _etext[];
 extern char _data[], _sdata[], _edata[];
@@ -53,6 +54,9 @@ extern char __ctors_start[], __ctors_end[];
 /* Start and end of .opd section - used for function descriptors. */
 extern char __start_opd[], __end_opd[];
 
+/* Start and end of .sframe section - used for stack unwinding. */
+extern char __start_sframe[], __end_sframe[];
+
 /* Start and end of instrumentation protected text section */
 extern char __noinstr_text_start[], __noinstr_text_end[];
 
include/asm-generic/vmlinux.lds.h +15 -0
index 1e1580febe4b..090da633db92 100644
@@ -491,6 +491,8 @@
 		*(.rodata1)						\
 	}								\
 									\
+	SFRAME								\
+									\
 	/* PCI quirks */						\
 	.pci_fixup        : AT(ADDR(.pci_fixup) - LOAD_OFFSET) {	\
 		BOUNDED_SECTION_PRE_LABEL(.pci_fixup_early,  _pci_fixups_early,  __start, __end) \
@@ -911,6 +913,19 @@
 #define TRACEDATA
 #endif
 
+#ifdef CONFIG_HAVE_UNWIND_KERNEL_SFRAME
+#define SFRAME							\
+	/* sframe */						\
+	.sframe : AT(ADDR(.sframe) - LOAD_OFFSET) {		\
+		__start_sframe = .;			\
+		KEEP(*(.sframe))				\
+		KEEP(*(.init.sframe))				\
+		__end_sframe = .;			\
+	}
+#else
+#define SFRAME
+#endif
+
 #ifdef CONFIG_PRINTK_INDEX
 #define PRINTK_INDEX							\
 	.printk_index : AT(ADDR(.printk_index) - LOAD_OFFSET) {		\
Signature · 1 line
2.54.0.rc1.555.g9c883467ad-goog
#6 lore raw

PATCH v4 3/8 arm64: entry: add unwind info for various kernel entries

In reply to Dylan Hatch

From: Weinan Liu <[email protected]>

DWARF CFI (Call Frame Information) specifies how to recover the return address and callee-saved registers at each PC in a given function. Compilers are able to generate the CFI annotations when they compile the code to assembly language. For handcrafted assembly, we need to annotate them by hand.

Annotate minimal CFI to enable stacktracing using SFrame for kernel exception entries through el1*_64_*() paths and irq entries through call_on_irq_stack()

---
 arch/arm64/kernel/entry.S | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
arch/arm64/kernel/entry.S +23 -0
index f8018b5c1f9a..dc55b0b19cfa 100644
@@ -30,6 +30,12 @@
 #include <asm/asm-uaccess.h>
 #include <asm/unistd.h>
 
+/*
+ * Do not generate .eh_frame.  Only generate .debug_frame and optionally
+ * .sframe (via assembler option --gsframe[-N]).
+ */
+	.cfi_sections .debug_frame
+
 	.macro	clear_gp_regs
 	.irp	n,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29
 	mov	x\n, xzr
@@ -575,7 +581,16 @@ SYM_CODE_START_LOCAL(el\el\ht\()_\regsize\()_\label)
 	.if \el == 0
 	b	ret_to_user
 	.else
+	/*
+	 * Minimal DWARF CFI for unwinding across the call above.
+	 * Enable unwinding for el1*_64_*() path only.
+	 */
+	.cfi_startproc
+	.cfi_def_cfa_offset PT_REGS_SIZE
+	.cfi_offset 29, S_FP - PT_REGS_SIZE
+	.cfi_offset 30, S_LR - PT_REGS_SIZE
 	b	ret_to_kernel
+	.cfi_endproc
 	.endif
 SYM_CODE_END(el\el\ht\()_\regsize\()_\label)
 	.endm
@@ -872,6 +887,7 @@ NOKPROBE(ret_from_fork)
  * Calls func(regs) using this CPU's irq stack and shadow irq stack.
  */
 SYM_FUNC_START(call_on_irq_stack)
+	.cfi_startproc
 	save_and_disable_daif x9
 #ifdef CONFIG_SHADOW_CALL_STACK
 	get_current_task x16
@@ -882,6 +898,9 @@ SYM_FUNC_START(call_on_irq_stack)
 	/* Create a frame record to save our LR and SP (implicit in FP) */
 	stp	x29, x30, [sp, #-16]!
 	mov	x29, sp
+	.cfi_def_cfa 29, 16
+	.cfi_offset 29, -16
+	.cfi_offset 30, -8
 
 	ldr_this_cpu x16, irq_stack_ptr, x17
 
@@ -897,9 +916,13 @@ SYM_FUNC_START(call_on_irq_stack)
 	 */
 	mov	sp, x29
 	ldp	x29, x30, [sp], #16
+	.cfi_restore 29
+	.cfi_restore 30
+	.cfi_def_cfa 31, 0
 	scs_load_current
 	restore_irq x9
 	ret
+	.cfi_endproc
 SYM_FUNC_END(call_on_irq_stack)
 NOKPROBE(call_on_irq_stack)
 
Signature · 1 line
2.54.0.rc1.555.g9c883467ad-goog
#8 lore raw

PATCH v4 4/8 sframe: Provide PC lookup for vmlinux .sframe section

In reply to Dylan Hatch

With SFRAME_UNWINDER, read in the .sframe section at boot. This provides unwind data as an alternative/supplement to frame pointer-based unwinding.

---
 arch/arm64/kernel/setup.c |  2 ++
 include/linux/sframe.h    | 14 ++++++++++++++
 kernel/unwind/sframe.c    | 36 ++++++++++++++++++++++++++++++++++++
 3 files changed, 52 insertions(+)
arch/arm64/kernel/setup.c +2 -0
index 23c05dc7a8f2..4a633bc7aefb 100644
@@ -32,6 +32,7 @@
 #include <linux/sched/task.h>
 #include <linux/scs.h>
 #include <linux/mm.h>
+#include <linux/sframe.h>
 
 #include <asm/acpi.h>
 #include <asm/fixmap.h>
@@ -375,6 +376,7 @@ void __init __no_sanitize_address setup_arch(char **cmdline_p)
 			"This indicates a broken bootloader or old kernel\n",
 			boot_args[1], boot_args[2], boot_args[3]);
 	}
+	init_sframe_table();
 }
 
 static inline bool cpu_can_disable(unsigned int cpu)
include/linux/sframe.h +14 -0
index 0cb2924367bc..5b7341b61a7c 100644
@@ -69,4 +69,18 @@ static inline int sframe_find_user(unsigned long ip, struct unwind_frame *frame)
 
 #endif /* CONFIG_HAVE_UNWIND_USER_SFRAME */
 
+#ifdef CONFIG_HAVE_UNWIND_KERNEL_SFRAME
+
+void __init init_sframe_table(void);
+void sframe_module_init(struct module *mod, void *sframe, size_t sframe_size,
+			void *text, size_t text_size);
+
+extern int sframe_find_kernel(unsigned long ip, struct unwind_frame *frame);
+
+#else
+
+static inline void __init init_sframe_table(void) {}
+
+#endif /* CONFIG_HAVE_UNWIND_KERNEL_SFRAME */
+
 #endif /* _LINUX_SFRAME_H */
kernel/unwind/sframe.c +36 -0
index ae15f5c06939..fb3b6b2d8677 100644
@@ -14,10 +14,20 @@
 #include <linux/sframe.h>
 #include <linux/unwind_types.h>
 #include <asm/unwind_sframe.h>
+#ifdef CONFIG_HAVE_UNWIND_KERNEL_SFRAME
+#include <linux/kallsyms.h>
+#endif
 
 #include "sframe.h"
 #include "sframe_debug.h"
 
+#ifdef CONFIG_HAVE_UNWIND_KERNEL_SFRAME
+
+static bool sframe_init __ro_after_init;
+static struct sframe_section kernel_sfsec __ro_after_init;
+
+#endif /* CONFIG_HAVE_UNWIND_KERNEL_SFRAME */
+
 struct sframe_fde_internal {
 	unsigned long	func_addr;
 	u32		func_size;
@@ -930,3 +940,29 @@ void sframe_free_mm(struct mm_struct *mm)
 }
 
 #endif /* CONFIG_HAVE_UNWIND_USER_SFRAME */
+
+#ifdef CONFIG_HAVE_UNWIND_KERNEL_SFRAME
+
+int sframe_find_kernel(unsigned long ip, struct unwind_frame *frame)
+{
+	if (!frame || !sframe_init)
+		return -EINVAL;
+
+	return  __sframe_find(&kernel_sfsec, ip, frame);
+}
+
+void __init init_sframe_table(void)
+{
+	kernel_sfsec.sec_type		= SFRAME_KERNEL;
+	kernel_sfsec.sframe_start	= (unsigned long)__start_sframe;
+	kernel_sfsec.sframe_end		= (unsigned long)__end_sframe;
+	kernel_sfsec.text_start		= (unsigned long)_stext;
+	kernel_sfsec.text_end		= (unsigned long)_etext;
+
+	if (WARN_ON(sframe_read_header(&kernel_sfsec)))
+		return;
+
+	sframe_init = true;
+}
+
+#endif /* CONFIG_HAVE_UNWIND_KERNEL_SFRAME */
Signature · 1 line
2.54.0.rc1.555.g9c883467ad-goog
#9 lore raw

PATCH v4 5/8 sframe: Allow unsorted FDEs

In reply to Dylan Hatch

The .sframe in kernel modules is built without SFRAME_F_FDE_SORTED set. In order to allow sframe PC lookup in modules, add a code path to handle unsorted FDE tables by doing a simple linear search.

---
 include/linux/sframe.h |  1 +
 kernel/unwind/sframe.c | 45 +++++++++++++++++++++++++++++++++++++-----
 2 files changed, 41 insertions(+), 5 deletions(-)
include/linux/sframe.h +1 -0
index 5b7341b61a7c..8ae31ed36226 100644
@@ -28,6 +28,7 @@ struct sframe_section {
 	unsigned long		fres_start;
 	unsigned long		fres_end;
 	unsigned int		num_fdes;
+	bool			fdes_sorted;
 
 	signed char		ra_off;
 	signed char		fp_off;
kernel/unwind/sframe.c +40 -5

75 lines not on this page · raw message

Signature · 1 line
2.54.0.rc1.555.g9c883467ad-goog
#10 lore raw

PATCH v4 6/8 arm64/module, sframe: Add sframe support for modules

In reply to Dylan Hatch

Add sframe table to mod_arch_specific and support sframe PC lookups when an .sframe section can be found on incoming modules.

---
 arch/arm64/include/asm/module.h |  6 +++++
 arch/arm64/kernel/module.c      |  8 +++++++
 include/linux/sframe.h          |  2 ++
 kernel/unwind/sframe.c          | 40 +++++++++++++++++++++++++++++++--
 4 files changed, 54 insertions(+), 2 deletions(-)
arch/arm64/include/asm/module.h +6 -0
index fb9b88eebeb1..07f309c51eee 100644
@@ -6,6 +6,7 @@
 #define __ASM_MODULE_H
 
 #include <asm-generic/module.h>
+#include <linux/sframe.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_HAVE_UNWIND_KERNEL_SFRAME
+	struct sframe_section sframe_sec;
+	bool sframe_init;
+#endif
+
 	/* for CONFIG_DYNAMIC_FTRACE */
 	struct plt_entry	*ftrace_trampolines;
 	struct plt_entry	*init_ftrace_trampolines;
arch/arm64/kernel/module.c +8 -0
index 24adb581af0e..427f187e9531 100644
@@ -18,6 +18,7 @@
 #include <linux/moduleloader.h>
 #include <linux/random.h>
 #include <linux/scs.h>
+#include <linux/sframe.h>
 
 #include <asm/alternative.h>
 #include <asm/insn.h>
@@ -515,5 +516,12 @@ int module_finalize(const Elf_Ehdr *hdr,
 		}
 	}
 
+	s = find_section(hdr, sechdrs, ".sframe");
+	if (s) {
+		struct module_memory *t = &me->mem[MOD_TEXT];
+
+		sframe_module_init(me, (void *)s->sh_addr, s->sh_size,
+				   t->base, t->size);
+	}
 	return module_init_ftrace_plt(hdr, sechdrs, me);
 }
include/linux/sframe.h +2 -0
index 8ae31ed36226..27f5a66190af 100644
@@ -81,6 +81,8 @@ extern int sframe_find_kernel(unsigned long ip, struct unwind_frame *frame);
 #else
 
 static inline void __init init_sframe_table(void) {}
+static inline void sframe_module_init(struct module *mod, void *sframe, size_t sframe_size,
+				      void *text, size_t text_size) {}
 
 #endif /* CONFIG_HAVE_UNWIND_KERNEL_SFRAME */
 
kernel/unwind/sframe.c +38 -2
index 243027244854..20178e02f428 100644
@@ -980,10 +980,27 @@ void sframe_free_mm(struct mm_struct *mm)
 
 int sframe_find_kernel(unsigned long ip, struct unwind_frame *frame)
 {
-	if (!frame || !sframe_init)
+	struct sframe_section *sec;
+
+	if (!frame)
 		return -EINVAL;
 
-	return  __sframe_find(&kernel_sfsec, ip, frame);
+	if (is_ksym_addr(ip)) {
+		if (!sframe_init)
+			return -EINVAL;
+
+		sec = &kernel_sfsec;
+	} else {
+		struct module *mod;
+
+		mod = __module_address(ip);
+		if (!mod || !mod->arch.sframe_init)
+			return -EINVAL;
+
+		sec = &mod->arch.sframe_sec;
+	}
+
+	return  __sframe_find(sec, ip, frame);
 }
 
 void __init init_sframe_table(void)
@@ -1000,4 +1017,23 @@ void __init init_sframe_table(void)
 	sframe_init = true;
 }
 
+void sframe_module_init(struct module *mod, void *sframe, size_t sframe_size,
+			void *text, size_t text_size)
+{
+	struct sframe_section sec;
+
+	memset(&sec, 0, sizeof(sec));
+	sec.sec_type	 = SFRAME_KERNEL;
+	sec.sframe_start = (unsigned long)sframe;
+	sec.sframe_end   = (unsigned long)sframe + sframe_size;
+	sec.text_start   = (unsigned long)text;
+	sec.text_end     = (unsigned long)text + text_size;
+
+	if (WARN_ON(sframe_read_header(&sec)))
+		return;
+
+	mod->arch.sframe_sec = sec;
+	mod->arch.sframe_init = true;
+}
+
 #endif /* CONFIG_HAVE_UNWIND_KERNEL_SFRAME */
Signature · 1 line
2.54.0.rc1.555.g9c883467ad-goog
#11 lore raw

PATCH v4 7/8 sframe: Introduce in-kernel SFRAME_VALIDATION

In reply to Dylan Hatch

Generalize the __safe* helpers to support a non-user-access code path.

This requires arch-specific function address validation. This is because arm64 vmlinux has an .rodata.text section which lies outside the bounds of the normal .text. It contains code that is never executed by the kernel mapping, but for which the toolchain nonetheless generates sframe data, and needs to be considered valid for a PC lookup.

This arch-specific address validation logic is only necessary to support SFRAME_VALIDATION for the vmlinux .sframe, since these .rodata.text functions would never be encountered during normal unwinding.

---
 arch/Kconfig                           |  2 +-
 arch/arm64/include/asm/sections.h      |  1 +
 arch/arm64/include/asm/unwind_sframe.h | 21 +++++++++++++++++++++
 arch/arm64/kernel/vmlinux.lds.S        |  2 ++
 include/linux/sframe.h                 |  2 ++
 kernel/unwind/sframe.c                 | 25 +++++++++++++++++++++++--
 6 files changed, 50 insertions(+), 3 deletions(-)
arch/Kconfig +1 -1
index 8d27b3249e7a..cd4849bb675c 100644
@@ -503,7 +503,7 @@ config HAVE_UNWIND_USER_SFRAME
 
 config SFRAME_VALIDATION
 	bool "Enable .sframe section debugging"
-	depends on HAVE_UNWIND_USER_SFRAME
+	depends on SFRAME_LOOKUP
 	depends on DYNAMIC_DEBUG
 	help
 	  When adding an .sframe section for a task, validate the entire
arch/arm64/include/asm/sections.h +1 -0
index 51b0d594239e..5edb4304f661 100644
@@ -23,6 +23,7 @@ extern char __irqentry_text_start[], __irqentry_text_end[];
 extern char __mmuoff_data_start[], __mmuoff_data_end[];
 extern char __entry_tramp_text_start[], __entry_tramp_text_end[];
 extern char __relocate_new_kernel_start[], __relocate_new_kernel_end[];
+extern char _srodatatext[], _erodatatext[];
 
 static inline size_t entry_tramp_text_size(void)
 {
arch/arm64/include/asm/unwind_sframe.h +21 -0
index 876412881196..1e9d7b74c0c8 100644
@@ -2,7 +2,28 @@
 #ifndef _ASM_ARM64_UNWIND_SFRAME_H
 #define _ASM_ARM64_UNWIND_SFRAME_H
 
+#include <linux/sframe.h>
+#include <asm/sections.h>
+
 #define SFRAME_REG_SP	31
 #define SFRAME_REG_FP	29
 
+static inline bool sframe_func_start_addr_valid(struct sframe_section *sec,
+						unsigned long func_addr)
+{
+	/*
+	 * The .rodata.text section is outside the normal kernel .text, but the
+	 * toolchain still generates sframe data for it. Allow sframe lookups
+	 * for these functions, even though they are never executed from the
+	 * kernel mapping.
+	 */
+	if (sec->sec_type == SFRAME_KERNEL && sec == &kernel_sfsec &&
+	    func_addr >= (unsigned long)_srodatatext &&
+	    func_addr < (unsigned long)_erodatatext)
+		return true;
+
+	return (sec->text_start <= func_addr && func_addr < sec->text_end);
+}
+#define sframe_func_start_addr_valid sframe_func_start_addr_valid
+
 #endif /* _ASM_ARM64_UNWIND_SFRAME_H */
arch/arm64/kernel/vmlinux.lds.S +2 -0
index 2964aad0362e..8c2dae6e7a86 100644
@@ -213,12 +213,14 @@ SECTIONS
 
 	/* code sections that are never executed via the kernel mapping */
 	.rodata.text : {
+		_srodatatext = .;
 		TRAMP_TEXT
 		HIBERNATE_TEXT
 		KEXEC_TEXT
 		IDMAP_TEXT
 		. = ALIGN(PAGE_SIZE);
 	}
+	_erodatatext = .;
 
 	idmap_pg_dir = .;
 	. += PAGE_SIZE;
include/linux/sframe.h +2 -0
index 27f5a66190af..ac3aa9db7d91 100644
@@ -34,6 +34,8 @@ struct sframe_section {
 	signed char		fp_off;
 };
 
+extern struct sframe_section kernel_sfsec __ro_after_init;
+
 #endif /* CONFIG_UNWIND_SFRAME_LOOKUP */
 
 #ifdef CONFIG_HAVE_UNWIND_USER_SFRAME
kernel/unwind/sframe.c +23 -2
index 20178e02f428..d76968547bad 100644
@@ -21,10 +21,18 @@
 #include "sframe.h"
 #include "sframe_debug.h"
 
+#ifndef sframe_func_start_addr_valid
+static inline bool sframe_func_start_addr_valid(struct sframe_section *sec,
+						unsigned long func_addr)
+{
+	return (sec->text_start <= func_addr && func_addr < sec->text_end);
+}
+#endif
+
 #ifdef CONFIG_HAVE_UNWIND_KERNEL_SFRAME
 
 static bool sframe_init __ro_after_init;
-static struct sframe_section kernel_sfsec __ro_after_init;
+struct sframe_section kernel_sfsec __ro_after_init;
 
 #endif /* CONFIG_HAVE_UNWIND_KERNEL_SFRAME */
 
@@ -152,7 +160,7 @@ static __always_inline int __read_fde(struct sframe_section *sec,
 		  sizeof(struct sframe_fde_v3), Efault);
 
 	func_addr = fde_addr + _fde.func_start_off;
-	if (func_addr < sec->text_start || func_addr > sec->text_end)
+	if (!sframe_func_start_addr_valid(sec, func_addr))
 		return -EINVAL;
 
 	fda_addr = sec->fres_start + _fde.fres_off;
@@ -636,6 +644,9 @@ static int safe_read_fde(struct sframe_section *sec,
 {
 	int ret;
 
+	if (sec->sec_type == SFRAME_KERNEL)
+		return __read_fde(sec, fde_num, fde);
+
 	if (!user_read_access_begin((void __user *)sec->sframe_start,
 				    sec->sframe_end - sec->sframe_start))
 		return -EFAULT;
@@ -651,6 +662,9 @@ static int safe_read_fre(struct sframe_section *sec,
 {
 	int ret;
 
+	if (sec->sec_type == SFRAME_KERNEL)
+		return __read_fre(sec, fde, fre_addr, fre);
+
 	if (!user_read_access_begin((void __user *)sec->sframe_start,
 				    sec->sframe_end - sec->sframe_start))
 		return -EFAULT;
@@ -665,6 +679,9 @@ static int safe_read_fre_datawords(struct sframe_section *sec,
 {
 	int ret;
 
+	if (sec->sec_type == SFRAME_KERNEL)
+		return __read_fre_datawords(sec, fde, fre);
+
 	if (!user_read_access_begin((void __user *)sec->sframe_start,
 				    sec->sframe_end - sec->sframe_start))
 		return -EFAULT;
@@ -1013,6 +1030,8 @@ void __init init_sframe_table(void)
 
 	if (WARN_ON(sframe_read_header(&kernel_sfsec)))
 		return;
+	if (WARN_ON(sframe_validate_section(&kernel_sfsec)))
+		return;
 
 	sframe_init = true;
 }
@@ -1031,6 +1050,8 @@ void sframe_module_init(struct module *mod, void *sframe, size_t sframe_size,
 
 	if (WARN_ON(sframe_read_header(&sec)))
 		return;
+	if (WARN_ON(sframe_validate_section(&sec)))
+		return;
 
 	mod->arch.sframe_sec = sec;
 	mod->arch.sframe_init = true;
Signature · 1 line
2.54.0.rc1.555.g9c883467ad-goog
#14 lore raw

PATCH v4 8/8 unwind: arm64: Use sframe to unwind interrupt frames

In reply to Dylan Hatch

Add unwind_next_frame_sframe() function to unwind by sframe info if present. Use this method at exception boundaries, falling back to frame-pointer unwind only on failure. In such failure cases, the stacktrace is considered unreliable.

During normal unwind, prefer frame pointer unwind (for better performance) with sframe as a backup.

This change restores the LR behavior originally introduced in commit c2c6b27b5aa14fa2 ("arm64: stacktrace: unwind exception boundaries"), But later removed in commit 32ed1205682e ("arm64: stacktrace: Skip reporting LR at exception boundaries")

This can be done because the sframe data can be used to determine whether the LR is current for the PC value recovered from pt_regs at the exception boundary.

---
 arch/arm64/include/asm/stacktrace/common.h |   6 +
 arch/arm64/kernel/stacktrace.c             | 246 +++++++++++++++++++--
 2 files changed, 232 insertions(+), 20 deletions(-)
arch/arm64/include/asm/stacktrace/common.h +6 -0
index 821a8fdd31af..4df68181e1b5 100644
@@ -21,6 +21,8 @@ struct stack_info {
  *
  * @fp:          The fp value in the frame record (or the real fp)
  * @pc:          The lr value in the frame record (or the real lr)
+ * @sp:          The sp value at the call site of the current function.
+ * @unreliable:  Stacktrace is unreliable.
  *
  * @stack:       The stack currently being unwound.
  * @stacks:      An array of stacks which can be unwound.
@@ -29,7 +31,11 @@ struct stack_info {
 struct unwind_state {
 	unsigned long fp;
 	unsigned long pc;
+#ifdef CONFIG_HAVE_UNWIND_KERNEL_SFRAME
+	unsigned long sp;
+#endif
 
+	bool unreliable;
 	struct stack_info stack;
 	struct stack_info *stacks;
 	int nr_stacks;
arch/arm64/kernel/stacktrace.c +226 -20

333 lines not on this page · raw message

Signature · 1 line
2.54.0.rc1.555.g9c883467ad-goog
Reviewed-by on v4 1/8
#2 lore raw

Re: [PATCH v4 1/8] sframe: Allow kernelspace sframe sections

In reply to Dylan Hatch
Quoted 11 lines · Dylan Hatch

Generalize the sframe lookup code to support kernelspace sections. This is done by defining a SFRAME_LOOKUP option that can be activated separate from HAVE_UNWIND_USER_SFRAME, as there will be other client to this library than just userspace unwind.

Sframe section location is now tracked in a separate sec_type field to determine whether user-access functions are necessary to read the sframe data. Relevant type delarations are moved and renamed to reflect the non-user sframe support.

With return -EFAULT changed to goto label in DATA_COPY() and DATA_GET():

Quoted 57 lines
---
 MAINTAINERS                                   |   2 +-
 arch/Kconfig                                  |   4 +
 .../{unwind_user_sframe.h => unwind_sframe.h} |   6 +-
 arch/x86/include/asm/unwind_user.h            |  12 +-
 include/linux/sframe.h                        |  48 ++--
 include/linux/unwind_types.h                  |  46 +++
 include/linux/unwind_user_types.h             |  41 ---
 kernel/unwind/Makefile                        |   2 +-
 kernel/unwind/sframe.c                        | 270 ++++++++++++------
 kernel/unwind/user.c                          |  41 +--
 10 files changed, 293 insertions(+), 179 deletions(-)
 rename arch/x86/include/asm/{unwind_user_sframe.h => unwind_sframe.h} (50%)
 create mode 100644 include/linux/unwind_types.h
include/linux/sframe.h +19 -14
 
+enum sframe_sec_type {
+	SFRAME_KERNEL,
+	SFRAME_USER,
+};
 
 struct sframe_section {
-	struct rcu_head	rcu;
+	struct rcu_head  rcu;
 #ifdef CONFIG_DYNAMIC_DEBUG
-	const char	*filename;
+	const char		*filename;
 #endif
-	unsigned long	sframe_start;
-	unsigned long	sframe_end;
-	unsigned long	text_start;
-	unsigned long	text_end;
-
-	unsigned long	fdes_start;
-	unsigned long	fres_start;
-	unsigned long	fres_end;
-	unsigned int	num_fdes;
-
-	signed char	ra_off;
-	signed char	fp_off;
+	enum sframe_sec_type	sec_type;
+	unsigned long		sframe_start;
+	unsigned long		sframe_end;
+	unsigned long		text_start;
+	unsigned long		text_end;
+
+	unsigned long		fdes_start;
+	unsigned long		fres_start;
+	unsigned long		fres_end;
+	unsigned int		num_fdes;
+
+	signed char		ra_off;
+	signed char		fp_off;
 };

kernel/unwind/sframe.c static __always_inline unsigned char dataword_size_enum_to_size(unsigned char da · line 112

Quoted 10 lines
kernel/unwind/sframe.c +9 -0
 
+#define DATA_COPY(sec, to, from, size, label)			\
+({								\
+	switch (sec->sec_type) {				\
+	case SFRAME_KERNEL:					\
+		KERNEL_COPY(to, from, size, label);		\
+		break;						\
+	case SFRAME_USER:					\
+		UNSAFE_USER_COPY(to, from, size, label);	\
+		break;						\

I wonder whether it would be worthwhile to come up with an approach where this would get evaluated at compile time instead at run time? Or is this overengineering? Of course such improvement could be made later on, so no need to solve that now.

Options that came into my mind:
A) Introduce and pass through a "bool user" parameter, whose value is specified in sframe_find_user() and sframe_find_kernel(). Due to inlining I would expect that to get any conditions based on that to get evaluated at compile time. See below. Downside is the ugly additional parameter.

B) Introduce lightweight .c wrappers, e.g. sframe_kernel.c and sframe_user.c, that define DATA_GET() and DATA_COPY() and include sframe.c. All HAVE_UNWIND_KERNEL_SFRAME code would be moved into sframe_kernel.c and likewise all HAVE_UNWIND_USER_SFRAME code into sframe_user.c.

kernel/unwind/sframe.c

Quoted 2 lines
kernel/unwind/sframe.c +2 -0
+	default:						\
+		return -EFAULT;					\
		goto label;					\

Users of DATA_COPY() do expect the macro to branch to the label in case of an error and therefore do not evaluate any return value. The wrapping then needs also be changed from "({ .. })" to
"do { ... } while (0)".

Quoted 4 lines
kernel/unwind/sframe.c +4 -0
+	}							\
+})
+
+#define DATA_GET(sec, to, from, type, label)			\

kernel/unwind/sframe.c static __always_inline unsigned char dataword_size_enum_to_size(unsigned char da · line 128

Quoted 10 lines
kernel/unwind/sframe.c +10 -0
+({								\
+	switch (sec->sec_type) {				\
+	case SFRAME_KERNEL:					\
+		KERNEL_GET(to, from, type, label);		\
+		break;						\
+	case SFRAME_USER:					\
+		UNSAFE_USER_GET(to, from, type, label);		\
+		break;						\
+	default:						\
+		return -EFAULT;					\

Likewise.

Quoted 15 lines
kernel/unwind/sframe.c +14 -0
+	}							\
+})
 
+#ifdef CONFIG_HAVE_UNWIND_USER_SFRAME
+
+int sframe_find_user(unsigned long ip, struct unwind_frame *frame)
+{
+	struct mm_struct *mm = current->mm;
+	struct sframe_section *sec;
+	int ret;
+
+	if (!mm)
+		return -EINVAL;
+
+	guard(srcu)(&sframe_srcu);

kernel/unwind/sframe.c static int sframe_read_header(struct sframe_section *sec) · line 791

Quoted 10 lines
kernel/unwind/sframe.c +10 -0
+
+	sec = mtree_load(&mm->sframe_mt, ip);
+	if (!sec)
+		return -EINVAL;
+
+	if (!user_read_access_begin((void __user *)sec->sframe_start,
+				    sec->sframe_end - sec->sframe_start))
+		return -EFAULT;
+
+	ret = __sframe_find(sec, ip, frame);

In sframe_find_user() sec->sec_type must be SFRAME_USER. Likewise in sframe_find_kernel() it must be SFRAME_KERNEL. So instead of introducing sec_type, we could add a parameter __sframe_find(..., bool user) and do:

	ret = __sframe_find(sec, ip, frame, true);

The downside is that this then requires to pass that flag through everywhere... (see below).

kernel/unwind/sframe.c static int sframe_read_header(struct sframe_section *sec) · line 801

Quoted 10 lines
kernel/unwind/sframe.c +10 -0
+
+	user_read_access_end();
+
+	if (ret == -EFAULT) {
+		dbg_sec("removing bad .sframe section\n");
+		WARN_ON_ONCE(sframe_remove_section(sec->sframe_start));
+	}
+
+	return ret;
+}

Regards,
Jens

Signature · 6 lines
Jens Remus
Linux on Z Development (D3303)
[email protected] / [email protected]

IBM Deutschland Research & Development GmbH; Vorsitzender des Aufsichtsrats: Wolfgang Wendt; Geschäftsführung: David Faller; Sitz der Gesellschaft: Ehningen; Registergericht: Amtsgericht Stuttgart, HRB 243294
IBM Data Privacy Statement: https://www.ibm.com/privacy/
Reviewed-by on v4 7/8
#12 lore raw

Re: [PATCH v4 7/8] sframe: Introduce in-kernel SFRAME_VALIDATION

In reply to Dylan Hatch
Quoted 8 lines · Dylan Hatch

Generalize the __safe* helpers to support a non-user-access code path.

This requires arch-specific function address validation. This is because arm64 vmlinux has an .rodata.text section which lies outside the bounds of the normal .text. It contains code that is never executed by the kernel mapping, but for which the toolchain nonetheless generates sframe data, and needs to be considered valid for a PC lookup.

Quoted 6 lines

This arch-specific address validation logic is only necessary to support SFRAME_VALIDATION for the vmlinux .sframe, since these .rodata.text functions would never be encountered during normal unwinding.

With the minor nit below fixed:

Quoted 11 lines
---
 arch/Kconfig                           |  2 +-
 arch/arm64/include/asm/sections.h      |  1 +
 arch/arm64/include/asm/unwind_sframe.h | 21 +++++++++++++++++++++
 arch/arm64/kernel/vmlinux.lds.S        |  2 ++
 include/linux/sframe.h                 |  2 ++
 kernel/unwind/sframe.c                 | 25 +++++++++++++++++++++++--
 6 files changed, 50 insertions(+), 3 deletions(-)

arch/Kconfig config HAVE_UNWIND_USER_SFRAME · line 506

Quoted 5 lines
arch/Kconfig +1 -1
@@ -503,7 +503,7 @@ config HAVE_UNWIND_USER_SFRAME
 
 config SFRAME_VALIDATION
 	bool "Enable .sframe section debugging"
-	depends on HAVE_UNWIND_USER_SFRAME
+	depends on SFRAME_LOOKUP

depends on UNWIND_SFRAME__LOOKUP

arch/Kconfig config HAVE_UNWIND_USER_SFRAME · line 509

Quoted 3 lines
arch/Kconfig +0 -0
 	depends on DYNAMIC_DEBUG
 	help
 	  When adding an .sframe section for a task, validate the entire

Regards,
Jens

Signature · 6 lines
Jens Remus
Linux on Z Development (D3303)
[email protected] / [email protected]

IBM Deutschland Research & Development GmbH; Vorsitzender des Aufsichtsrats: Wolfgang Wendt; Geschäftsführung: David Faller; Sitz der Gesellschaft: Ehningen; Registergericht: Amtsgericht Stuttgart, HRB 243294
IBM Data Privacy Statement: https://www.ibm.com/privacy/
Reviewed-by on v4 2/8
#5 lore raw

Re: [PATCH v4 2/8] arm64, unwind: build kernel with sframe V3 info

In reply to Dylan Hatch
Quoted 10 lines · Dylan Hatch

Build with -Wa,--gsframe-3 flags to generate a .sframe section. This will be used for in-kernel reliable stacktrace in cases where the frame pointer alone is insufficient.

Currently, the sframe format only supports arm64, x86_64 and s390x architectures.

Quoted 11 lines
---
 MAINTAINERS                            |  1 +
 Makefile                               |  8 ++++++++
 arch/Kconfig                           | 21 +++++++++++++++++++++
 arch/arm64/Kconfig                     |  1 +
 arch/arm64/include/asm/unwind_sframe.h |  8 ++++++++
 arch/arm64/kernel/vdso/Makefile        |  2 +-
 include/asm-generic/sections.h         |  4 ++++
 include/asm-generic/vmlinux.lds.h      | 15 +++++++++++++++
 8 files changed, 59 insertions(+), 1 deletion(-)
 create mode 100644 arch/arm64/include/asm/unwind_sframe.h

Regards,
Jens

Signature · 6 lines
Jens Remus
Linux on Z Development (D3303)
[email protected] / [email protected]

IBM Deutschland Research & Development GmbH; Vorsitzender des Aufsichtsrats: Wolfgang Wendt; Geschäftsführung: David Faller; Sitz der Gesellschaft: Ehningen; Registergericht: Amtsgericht Stuttgart, HRB 243294
IBM Data Privacy Statement: https://www.ibm.com/privacy/
Reviewed-by on v4 3/8
#7 lore raw

Re: [PATCH v4 3/8] arm64: entry: add unwind info for various kernel entries

In reply to Dylan Hatch
Quoted 8 lines · Dylan Hatch

From: Weinan Liu <[email protected]>

DWARF CFI (Call Frame Information) specifies how to recover the return address and callee-saved registers at each PC in a given function. Compilers are able to generate the CFI annotations when they compile the code to assembly language. For handcrafted assembly, we need to annotate them by hand.

Quoted 7 lines

Annotate minimal CFI to enable stacktracing using SFrame for kernel exception entries through el1*_64_*() paths and irq entries through call_on_irq_stack()

Quoted 3 lines
---
 arch/arm64/kernel/entry.S | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

Regards,
Jens

Signature · 6 lines
Jens Remus
Linux on Z Development (D3303)
[email protected] / [email protected]

IBM Deutschland Research & Development GmbH; Vorsitzender des Aufsichtsrats: Wolfgang Wendt; Geschäftsführung: David Faller; Sitz der Gesellschaft: Ehningen; Registergericht: Amtsgericht Stuttgart, HRB 243294
IBM Data Privacy Statement: https://www.ibm.com/privacy/
Reviewed-by on v4 8/8
#15 lore raw

Re: [PATCH v4 8/8] unwind: arm64: Use sframe to unwind interrupt frames

In reply to Dylan Hatch
Quoted 13 lines · Dylan Hatch

Add unwind_next_frame_sframe() function to unwind by sframe info if present. Use this method at exception boundaries, falling back to frame-pointer unwind only on failure. In such failure cases, the stacktrace is considered unreliable.

During normal unwind, prefer frame pointer unwind (for better performance) with sframe as a backup.

This change restores the LR behavior originally introduced in commit c2c6b27b5aa14fa2 ("arm64: stacktrace: unwind exception boundaries"), But later removed in commit 32ed1205682e ("arm64: stacktrace: Skip reporting LR at exception boundaries")

Quoted 7 lines

This can be done because the sframe data can be used to determine whether the LR is current for the PC value recovered from pt_regs at the exception boundary.

Quoted 4 lines
---
 arch/arm64/include/asm/stacktrace/common.h |   6 +
 arch/arm64/kernel/stacktrace.c             | 246 +++++++++++++++++++--
 2 files changed, 232 insertions(+), 20 deletions(-)

Regards,
Jens

Signature · 6 lines
Jens Remus
Linux on Z Development (D3303)
[email protected] / [email protected]

IBM Deutschland Research & Development GmbH; Vorsitzender des Aufsichtsrats: Wolfgang Wendt; Geschäftsführung: David Faller; Sitz der Gesellschaft: Ehningen; Registergericht: Amtsgericht Stuttgart, HRB 243294
IBM Data Privacy Statement: https://www.ibm.com/privacy/
#3 lore raw

Re: [PATCH v4 1/8] sframe: Allow kernelspace sframe sections

In reply to Jens Remus
Quoted 76 lines · Jens Remus
Quoted 11 lines · Dylan Hatch

Generalize the sframe lookup code to support kernelspace sections. This is done by defining a SFRAME_LOOKUP option that can be activated separate from HAVE_UNWIND_USER_SFRAME, as there will be other client to this library than just userspace unwind.

Sframe section location is now tracked in a separate sec_type field to determine whether user-access functions are necessary to read the sframe data. Relevant type delarations are moved and renamed to reflect the non-user sframe support.

With return -EFAULT changed to goto label in DATA_COPY() and DATA_GET():

Quoted 57 lines
---
 MAINTAINERS                                   |   2 +-
 arch/Kconfig                                  |   4 +
 .../{unwind_user_sframe.h => unwind_sframe.h} |   6 +-
 arch/x86/include/asm/unwind_user.h            |  12 +-
 include/linux/sframe.h                        |  48 ++--
 include/linux/unwind_types.h                  |  46 +++
 include/linux/unwind_user_types.h             |  41 ---
 kernel/unwind/Makefile                        |   2 +-
 kernel/unwind/sframe.c                        | 270 ++++++++++++------
 kernel/unwind/user.c                          |  41 +--
 10 files changed, 293 insertions(+), 179 deletions(-)
 rename arch/x86/include/asm/{unwind_user_sframe.h => unwind_sframe.h} (50%)
 create mode 100644 include/linux/unwind_types.h
include/linux/sframe.h +19 -14
 
+enum sframe_sec_type {
+     SFRAME_KERNEL,
+     SFRAME_USER,
+};
 
 struct sframe_section {
-     struct rcu_head rcu;
+     struct rcu_head  rcu;
 #ifdef CONFIG_DYNAMIC_DEBUG
-     const char      *filename;
+     const char              *filename;
 #endif
-     unsigned long   sframe_start;
-     unsigned long   sframe_end;
-     unsigned long   text_start;
-     unsigned long   text_end;
-
-     unsigned long   fdes_start;
-     unsigned long   fres_start;
-     unsigned long   fres_end;
-     unsigned int    num_fdes;
-
-     signed char     ra_off;
-     signed char     fp_off;
+     enum sframe_sec_type    sec_type;
+     unsigned long           sframe_start;
+     unsigned long           sframe_end;
+     unsigned long           text_start;
+     unsigned long           text_end;
+
+     unsigned long           fdes_start;
+     unsigned long           fres_start;
+     unsigned long           fres_end;
+     unsigned int            num_fdes;
+
+     signed char             ra_off;
+     signed char             fp_off;
 };
kernel/unwind/sframe.c +0 -0
Quoted 14 lines

kernel/unwind/sframe.c static __always_inline unsigned char dataword_size_enum_to_size(unsigned char da · line 112

Quoted 9 lines
kernel/unwind/sframe.c +9 -0
+#define DATA_COPY(sec, to, from, size, label)                        \
+({                                                           \
+     switch (sec->sec_type) {                                \
+     case SFRAME_KERNEL:                                     \
+             KERNEL_COPY(to, from, size, label);             \
+             break;                                          \
+     case SFRAME_USER:                                       \
+             UNSAFE_USER_COPY(to, from, size, label);        \
+             break;                                          \

I wonder whether it would be worthwhile to come up with an approach where this would get evaluated at compile time instead at run time? Or is this overengineering? Of course such improvement could be made later on, so no need to solve that now.

I had a similar thought when I was writing this patch, but I ended up deciding to avoid premature optimization before getting feedback. I'd definitely be interested in improving upon this later on.

Quoted 8 lines

Options that came into my mind:
A) Introduce and pass through a "bool user" parameter, whose value is specified in sframe_find_user() and sframe_find_kernel(). Due to inlining I would expect that to get any conditions based on that to get evaluated at compile time. See below. Downside is the ugly additional parameter.

Quoted 5 lines

B) Introduce lightweight .c wrappers, e.g. sframe_kernel.c and sframe_user.c, that define DATA_GET() and DATA_COPY() and include sframe.c. All HAVE_UNWIND_KERNEL_SFRAME code would be moved into sframe_kernel.c and likewise all HAVE_UNWIND_USER_SFRAME code into sframe_user.c.

(A) definitely sounds simpler to implement. For (B) it seems uncommon for .c files to include one another. Style-wise, is this something that is typically allowed (e.g. by checkpatch.pl)?

Quoted 84 lines

kernel/unwind/sframe.c

Quoted 3 lines
kernel/unwind/sframe.c +2 -0
+     default:                                                \
+             return -EFAULT;                                 \
                goto label;                                     \

Users of DATA_COPY() do expect the macro to branch to the label in case of an error and therefore do not evaluate any return value. The wrapping then needs also be changed from "({ .. })" to
"do { ... } while (0)".

Quoted 4 lines
kernel/unwind/sframe.c +4 -0
+     }                                                       \
+})
+
+#define DATA_GET(sec, to, from, type, label)                 \

kernel/unwind/sframe.c static __always_inline unsigned char dataword_size_enum_to_size(unsigned char da · line 128

Quoted 10 lines
kernel/unwind/sframe.c +10 -0
+({                                                           \
+     switch (sec->sec_type) {                                \
+     case SFRAME_KERNEL:                                     \
+             KERNEL_GET(to, from, type, label);              \
+             break;                                          \
+     case SFRAME_USER:                                       \
+             UNSAFE_USER_GET(to, from, type, label);         \
+             break;                                          \
+     default:                                                \
+             return -EFAULT;                                 \

Likewise.

Quoted 15 lines
kernel/unwind/sframe.c +14 -0
+     }                                                       \
+})
 
+#ifdef CONFIG_HAVE_UNWIND_USER_SFRAME
+
+int sframe_find_user(unsigned long ip, struct unwind_frame *frame)
+{
+     struct mm_struct *mm = current->mm;
+     struct sframe_section *sec;
+     int ret;
+
+     if (!mm)
+             return -EINVAL;
+
+     guard(srcu)(&sframe_srcu);

kernel/unwind/sframe.c static int sframe_read_header(struct sframe_section *sec) · line 791

Quoted 10 lines
kernel/unwind/sframe.c +10 -0
+
+     sec = mtree_load(&mm->sframe_mt, ip);
+     if (!sec)
+             return -EINVAL;
+
+     if (!user_read_access_begin((void __user *)sec->sframe_start,
+                                 sec->sframe_end - sec->sframe_start))
+             return -EFAULT;
+
+     ret = __sframe_find(sec, ip, frame);

In sframe_find_user() sec->sec_type must be SFRAME_USER. Likewise in sframe_find_kernel() it must be SFRAME_KERNEL. So instead of introducing sec_type, we could add a parameter __sframe_find(..., bool user) and do:

        ret = __sframe_find(sec, ip, frame, true);

The downside is that this then requires to pass that flag through everywhere... (see below).

kernel/unwind/sframe.c static int sframe_read_header(struct sframe_section *sec) · line 801

Quoted 10 lines
kernel/unwind/sframe.c +10 -0
+
+     user_read_access_end();
+
+     if (ret == -EFAULT) {
+             dbg_sec("removing bad .sframe section\n");
+             WARN_ON_ONCE(sframe_remove_section(sec->sframe_start));
+     }
+
+     return ret;
+}

Regards,
Jens
--
Jens Remus
Linux on Z Development (D3303)
[email protected] / [email protected]

IBM Deutschland Research & Development GmbH; Vorsitzender des Aufsichtsrats: Wolfgang Wendt; Geschäftsführung: David Faller; Sitz der Gesellschaft: Ehningen; Registergericht: Amtsgericht Stuttgart, HRB 243294 IBM Data Privacy Statement: https://www.ibm.com/privacy/

#13 lore raw

Re: [PATCH v4 7/8] sframe: Introduce in-kernel SFRAME_VALIDATION

In reply to Jens Remus
Quoted 33 lines · Jens Remus
Quoted 8 lines · Dylan Hatch

Generalize the __safe* helpers to support a non-user-access code path.

This requires arch-specific function address validation. This is because arm64 vmlinux has an .rodata.text section which lies outside the bounds of the normal .text. It contains code that is never executed by the kernel mapping, but for which the toolchain nonetheless generates sframe data, and needs to be considered valid for a PC lookup.

Quoted 6 lines

This arch-specific address validation logic is only necessary to support SFRAME_VALIDATION for the vmlinux .sframe, since these .rodata.text functions would never be encountered during normal unwinding.

With the minor nit below fixed:

Quoted 12 lines
---
 arch/Kconfig                           |  2 +-
 arch/arm64/include/asm/sections.h      |  1 +
 arch/arm64/include/asm/unwind_sframe.h | 21 +++++++++++++++++++++
 arch/arm64/kernel/vmlinux.lds.S        |  2 ++
 include/linux/sframe.h                 |  2 ++
 kernel/unwind/sframe.c                 | 25 +++++++++++++++++++++++--
 6 files changed, 50 insertions(+), 3 deletions(-)
arch/Kconfig +0 -0
@@ -503,7 +503,7 @@ config HAVE_UNWIND_USER_SFRAME
 
Quoted 6 lines

arch/Kconfig config HAVE_UNWIND_USER_SFRAME · line 506

Quoted 4 lines
arch/Kconfig +1 -1
 config SFRAME_VALIDATION
      bool "Enable .sframe section debugging"
-     depends on HAVE_UNWIND_USER_SFRAME
+     depends on SFRAME_LOOKUP

depends on UNWIND_SFRAME__LOOKUP

Ah my bad. This mistake was masking similar issues with .init.text and .exit.text as we had with .rodata.text. I'll send a new version accounting for those versions as well.

Quoted 12 lines

arch/Kconfig config HAVE_UNWIND_USER_SFRAME · line 509

Quoted 4 lines
arch/Kconfig +0 -0
      depends on DYNAMIC_DEBUG
      help
        When adding an .sframe section for a task, validate the entire

Regards,
Jens
--
Jens Remus
Linux on Z Development (D3303)
[email protected] / [email protected]

Quoted 3 lines

IBM Deutschland Research & Development GmbH; Vorsitzender des Aufsichtsrats: Wolfgang Wendt; Geschäftsführung: David Faller; Sitz der Gesellschaft: Ehningen; Registergericht: Amtsgericht Stuttgart, HRB 243294 IBM Data Privacy Statement: https://www.ibm.com/privacy/

Thanks,
Dylan