Skip to contentSkip to conversation

unwind, arm64: add sframe unwinder for kernel

PATCH Superseded Dylan Hatch 3 participants 30 messages Apr 6 – Apr 21

Subsystems (7)

v3 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v3 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 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                                  |  13 +-
 arch/arm64/Kconfig                            |   1 +
 arch/arm64/Kconfig.debug                      |  13 +
 arch/arm64/include/asm/module.h               |   6 +
 arch/arm64/include/asm/stacktrace/common.h    |   6 +
 arch/arm64/include/asm/unwind_sframe.h        |  12 +
 arch/arm64/kernel/entry.S                     |  10 +
 arch/arm64/kernel/module.c                    |   8 +
 arch/arm64/kernel/setup.c                     |   2 +
 arch/arm64/kernel/stacktrace.c                | 242 ++++++++++-
 arch/arm64/kernel/vdso/Makefile               |   2 +-
 .../{unwind_user_sframe.h => unwind_sframe.h} |   6 +-
 arch/x86/include/asm/unwind_user.h            |  12 +-
 include/asm-generic/vmlinux.lds.h             |  15 +
 include/linux/sframe.h                        | 105 ++++-
 include/linux/unwind_user_types.h             |  41 --
 kernel/unwind/Makefile                        |   2 +-
 kernel/unwind/sframe.c                        | 408 ++++++++++++++----
 kernel/unwind/user.c                          |  40 +-
 21 files changed, 749 insertions(+), 206 deletions(-)
 create mode 100644 arch/arm64/include/asm/unwind_sframe.h
 rename arch/x86/include/asm/{unwind_user_sframe.h => unwind_sframe.h} (50%)
Signature · 1 line
2.53.0.1213.gd9a14994de-goog
#1 lore raw

PATCH v3 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 UNWIND_USER_SFRAME, as there will be other clients 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                        |  88 ++++--
 include/linux/unwind_user_types.h             |  41 ---
 kernel/unwind/Makefile                        |   2 +-
 kernel/unwind/sframe.c                        | 270 ++++++++++++------
 kernel/unwind/user.c                          |  40 +--
 9 files changed, 286 insertions(+), 179 deletions(-)
 rename arch/x86/include/asm/{unwind_user_sframe.h => unwind_sframe.h} (50%)
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..6695c222c728 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 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 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 +70 -18

121 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..6b51302308d0 100644
@@ -1,2 +1,2 @@
  obj-$(CONFIG_UNWIND_USER)		+= user.o deferred.o
- obj-$(CONFIG_HAVE_UNWIND_USER_SFRAME)	+= sframe.o
+ obj-$(CONFIG_SFRAME_LOOKUP)	+= sframe.o
kernel/unwind/sframe.c +181 -89

480 lines not on this page · raw message

kernel/unwind/user.c +20 -20
index eb7d9489f671..f9abd08ed83b 100644
@@ -28,7 +28,7 @@ get_user_word(unsigned long *word, unsigned long base, int off, unsigned int ws)
 }
 
 static int unwind_user_next_common(struct unwind_user_state *state,
-				   const struct unwind_user_frame *frame)
+				   const struct unwind_frame *frame)
 {
 	unsigned long cfa, fp, ra;
 
@@ -40,16 +40,16 @@ static int unwind_user_next_common(struct unwind_user_state *state,
 
 	/* Get the Canonical Frame Address (CFA) */
 	switch (frame->cfa.rule) {
-	case UNWIND_USER_CFA_RULE_SP_OFFSET:
+	case UNWIND_CFA_RULE_SP_OFFSET:
 		cfa = state->sp;
 		break;
-	case UNWIND_USER_CFA_RULE_FP_OFFSET:
+	case UNWIND_CFA_RULE_FP_OFFSET:
 		if (state->fp < state->sp)
 			return -EINVAL;
 		cfa = state->fp;
 		break;
-	case UNWIND_USER_CFA_RULE_REG_OFFSET:
-	case UNWIND_USER_CFA_RULE_REG_OFFSET_DEREF:
+	case UNWIND_CFA_RULE_REG_OFFSET:
+	case UNWIND_CFA_RULE_REG_OFFSET_DEREF:
 		if (!state->topmost || unwind_user_get_reg(&cfa, frame->cfa.regnum))
 			return -EINVAL;
 		break;
@@ -58,7 +58,7 @@ static int unwind_user_next_common(struct unwind_user_state *state,
 		return -EINVAL;
 	}
 	cfa += frame->cfa.offset;
-	if (frame->cfa.rule & UNWIND_USER_RULE_DEREF &&
+	if (frame->cfa.rule & UNWIND_RULE_DEREF &&
 	    get_user_word(&cfa, cfa, 0, state->ws))
 		return -EINVAL;
 
@@ -76,16 +76,16 @@ static int unwind_user_next_common(struct unwind_user_state *state,
 
 	/* Get the Return Address (RA) */
 	switch (frame->ra.rule) {
-	case UNWIND_USER_RULE_RETAIN:
+	case UNWIND_RULE_RETAIN:
 		if (!state->topmost || unwind_user_get_ra_reg(&ra))
 			return -EINVAL;
 		break;
 	/* UNWIND_USER_RULE_CFA_OFFSET not implemented on purpose */
-	case UNWIND_USER_RULE_CFA_OFFSET_DEREF:
+	case UNWIND_RULE_CFA_OFFSET_DEREF:
 		ra = cfa + frame->ra.offset;
 		break;
-	case UNWIND_USER_RULE_REG_OFFSET:
-	case UNWIND_USER_RULE_REG_OFFSET_DEREF:
+	case UNWIND_RULE_REG_OFFSET:
+	case UNWIND_RULE_REG_OFFSET_DEREF:
 		if (!state->topmost || unwind_user_get_reg(&ra, frame->ra.regnum))
 			return -EINVAL;
 		ra += frame->ra.offset;
@@ -94,21 +94,21 @@ static int unwind_user_next_common(struct unwind_user_state *state,
 		WARN_ON_ONCE(1);
 		return -EINVAL;
 	}
-	if (frame->ra.rule & UNWIND_USER_RULE_DEREF &&
+	if (frame->ra.rule & UNWIND_RULE_DEREF &&
 	    get_user_word(&ra, ra, 0, state->ws))
 		return -EINVAL;
 
 	/* Get the Frame Pointer (FP) */
 	switch (frame->fp.rule) {
-	case UNWIND_USER_RULE_RETAIN:
+	case UNWIND_RULE_RETAIN:
 		fp = state->fp;
 		break;
 	/* UNWIND_USER_RULE_CFA_OFFSET not implemented on purpose */
-	case UNWIND_USER_RULE_CFA_OFFSET_DEREF:
+	case UNWIND_RULE_CFA_OFFSET_DEREF:
 		fp = cfa + frame->fp.offset;
 		break;
-	case UNWIND_USER_RULE_REG_OFFSET:
-	case UNWIND_USER_RULE_REG_OFFSET_DEREF:
+	case UNWIND_RULE_REG_OFFSET:
+	case UNWIND_RULE_REG_OFFSET_DEREF:
 		if (!state->topmost || unwind_user_get_reg(&fp, frame->fp.regnum))
 			return -EINVAL;
 		fp += frame->fp.offset;
@@ -117,7 +117,7 @@ static int unwind_user_next_common(struct unwind_user_state *state,
 		WARN_ON_ONCE(1);
 		return -EINVAL;
 	}
-	if (frame->fp.rule & UNWIND_USER_RULE_DEREF &&
+	if (frame->fp.rule & UNWIND_RULE_DEREF &&
 	    get_user_word(&fp, fp, 0, state->ws))
 		return -EINVAL;
 
@@ -133,13 +133,13 @@ static int unwind_user_next_fp(struct unwind_user_state *state)
 	struct pt_regs *regs = task_pt_regs(current);
 
 	if (state->topmost && unwind_user_at_function_start(regs)) {
-		const struct unwind_user_frame fp_entry_frame = {
+		const struct unwind_frame fp_entry_frame = {
 			ARCH_INIT_USER_FP_ENTRY_FRAME(state->ws)
 		};
 		return unwind_user_next_common(state, &fp_entry_frame);
 	}
 
-	const struct unwind_user_frame fp_frame = {
+	const struct unwind_frame fp_frame = {
 		ARCH_INIT_USER_FP_FRAME(state->ws)
 	};
 	return unwind_user_next_common(state, &fp_frame);
@@ -147,10 +147,10 @@ static int unwind_user_next_fp(struct unwind_user_state *state)
 
 static int unwind_user_next_sframe(struct unwind_user_state *state)
 {
-	struct unwind_user_frame frame;
+	struct unwind_frame frame;
 
 	/* sframe expects the frame to be local storage */
-	if (sframe_find(state->ip, &frame))
+	if (sframe_find_user(state->ip, &frame))
 		return -ENOENT;
 	return unwind_user_next_common(state, &frame);
 }
Signature · 1 line
2.53.0.1213.gd9a14994de-goog
#2 lore raw

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

Hello Dylan!

Quoted 4 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 UNWIND_USER_SFRAME, as there will be other clients to this library than just userspace unwind.

Nit: s/UNWIND_USER_SFRAME/HAVE_UNWIND_USER_SFRAME/

This actually uses the following two new Kconfig options (with SFRAME_UNWINDER technically being introduced in the next patch):

SFRAME_LOOKUP
SFRAME_UNWINDER

IIUC SFRAME_UNWINDER is the kernel counterpart to the existing HAVE_UNWIND_USER_SFRAME. Would it therefore make sense to align the naming as follows?

HAVE_UNWIND_KERNEL_SFRAME (instead of SFRAME_UNWINDER) HAVE_UNWIND_USER_SFRAME

Quoted 20 lines

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.

arch/Kconfig +3 -0
@@ -486,6 +486,9 @@ config AS_SFRAME3
 	def_bool $(as-instr,.cfi_startproc\n.cfi_endproc,-Wa$(comma)--gsframe-3)
 	select AS_SFRAME
 
+config SFRAME_LOOKUP
+	bool
+
 config UNWIND_USER
 	bool
 

arch/Kconfig config HAVE_UNWIND_USER_FP · line 505

Quoted 7 lines
arch/Kconfig +1 -0
@@ -496,6 +499,7 @@ config HAVE_UNWIND_USER_FP
 config HAVE_UNWIND_USER_SFRAME
 	bool
 	select UNWIND_USER
+	select SFRAME_LOOKUP
 
 config SFRAME_VALIDATION
 	bool "Enable .sframe section debugging"

IIUC SFRAME_LOOKUP only exists to pull in the common (kernel and user) sframe lookup code if SFRAME_UNWINDER and/or UNWIND_USER_SFRAME are enabled. Given there is currently no other use case than kernel/user stacktrace unwinding, would it make sense to rename it as follows to group all of the related options with the UNWIND prefix?

UNWIND_SFRAME[_LOOKUP]

Quoted 39 lines
include/linux/sframe.h +30 -2
@@ -4,36 +4,85 @@
 
 #include <linux/mm_types.h>
 #include <linux/srcu.h>
-#include <linux/unwind_user_types.h>
 
-#ifdef CONFIG_HAVE_UNWIND_USER_SFRAME
+#define UNWIND_RULE_DEREF			BIT(31)
+
+enum unwind_cfa_rule {
+	UNWIND_CFA_RULE_SP_OFFSET,		/* CFA = SP + offset */
+	UNWIND_CFA_RULE_FP_OFFSET,		/* CFA = FP + offset */
+	UNWIND_CFA_RULE_REG_OFFSET,	/* CFA = reg + offset */
+	/* DEREF variants */
+	UNWIND_CFA_RULE_REG_OFFSET_DEREF =	/* CFA = *(reg + offset) */
+		UNWIND_CFA_RULE_REG_OFFSET | UNWIND_RULE_DEREF,
+};
+
+struct unwind_cfa_rule_data {
+	enum unwind_cfa_rule rule;
+	s32 offset;
+	unsigned int regnum;
+};
+
+enum unwind_rule {
+	UNWIND_RULE_RETAIN,		/* entity = entity */
+	UNWIND_RULE_CFA_OFFSET,		/* entity = CFA + offset */
+	UNWIND_RULE_REG_OFFSET,		/* entity = register + offset */
+	/* DEREF variants */
+	UNWIND_RULE_CFA_OFFSET_DEREF =	/* entity = *(CFA + offset) */
+		UNWIND_RULE_CFA_OFFSET | UNWIND_RULE_DEREF,
+	UNWIND_RULE_REG_OFFSET_DEREF =	/* entity = *(register + offset) */
+		UNWIND_RULE_REG_OFFSET | UNWIND_RULE_DEREF,
+};
+
+struct unwind_rule_data {
+	enum unwind_rule rule;

include/linux/sframe.h line 47

Quoted 10 lines
include/linux/sframe.h +10 -0
+	s32 offset;
+	unsigned int regnum;
+};
+
+struct unwind_frame {
+	struct unwind_cfa_rule_data cfa;
+	struct unwind_rule_data ra;
+	struct unwind_rule_data fp;
+	bool outermost;
+};

You are moving (and renaming to generalize for kernel and user unwind use) the above definitions from include/linux/unwind_user_types.h to include/linux/sframe.h. Given the definitions are used in kernel/unwind/user.c for FP and SFRAME unwinding this seems wrong to me. The definitions should better be moved (and renamed as you did) into a new include/linux/unwind_types.h (or the like).

Quoted 103 lines
include/linux/unwind_user_types.h +0 -41
@@ -27,47 +27,6 @@ struct unwind_stacktrace {
 	unsigned long	*entries;
 };
 
-#define UNWIND_USER_RULE_DEREF			BIT(31)
-
-enum unwind_user_cfa_rule {
-	UNWIND_USER_CFA_RULE_SP_OFFSET,		/* CFA = SP + offset */
-	UNWIND_USER_CFA_RULE_FP_OFFSET,		/* CFA = FP + offset */
-	UNWIND_USER_CFA_RULE_REG_OFFSET,	/* CFA = reg + offset */
-	/* DEREF variants */
-	UNWIND_USER_CFA_RULE_REG_OFFSET_DEREF =	/* CFA = *(reg + offset) */
-		UNWIND_USER_CFA_RULE_REG_OFFSET | UNWIND_USER_RULE_DEREF,
-};
-
-struct unwind_user_cfa_rule_data {
-	enum unwind_user_cfa_rule rule;
-	s32 offset;
-	unsigned int regnum;
-};
-
-enum unwind_user_rule {
-	UNWIND_USER_RULE_RETAIN,		/* entity = entity */
-	UNWIND_USER_RULE_CFA_OFFSET,		/* entity = CFA + offset */
-	UNWIND_USER_RULE_REG_OFFSET,		/* entity = register + offset */
-	/* DEREF variants */
-	UNWIND_USER_RULE_CFA_OFFSET_DEREF =	/* entity = *(CFA + offset) */
-		UNWIND_USER_RULE_CFA_OFFSET | UNWIND_USER_RULE_DEREF,
-	UNWIND_USER_RULE_REG_OFFSET_DEREF =	/* entity = *(register + offset) */
-		UNWIND_USER_RULE_REG_OFFSET | UNWIND_USER_RULE_DEREF,
-};
-
-struct unwind_user_rule_data {
-	enum unwind_user_rule rule;
-	s32 offset;
-	unsigned int regnum;
-};
-
-struct unwind_user_frame {
-	struct unwind_user_cfa_rule_data cfa;
-	struct unwind_user_rule_data ra;
-	struct unwind_user_rule_data fp;
-	bool outermost;
-};
-
 struct unwind_user_state {
 	unsigned long				ip;
 	unsigned long				sp;
kernel/unwind/Makefile +1 -1
@@ -1,2 +1,2 @@
  obj-$(CONFIG_UNWIND_USER)		+= user.o deferred.o
- obj-$(CONFIG_HAVE_UNWIND_USER_SFRAME)	+= sframe.o
+ obj-$(CONFIG_SFRAME_LOOKUP)	+= sframe.o
kernel/unwind/sframe.c +30 -2
@@ -44,8 +43,6 @@ struct sframe_fre_internal {
 	unsigned char	dw_size;
 };
 
-DEFINE_STATIC_SRCU(sframe_srcu);
-
 static __always_inline unsigned char fre_type_to_size(unsigned char fre_type)
 {
 	if (fre_type > 2)
@@ -60,6 +57,78 @@ static __always_inline unsigned char dataword_size_enum_to_size(unsigned char da
 	return 1 << dataword_size;
 }
 
+#ifdef CONFIG_HAVE_UNWIND_USER_SFRAME
+
+DEFINE_STATIC_SRCU(sframe_srcu);
+
+#define UNSAFE_USER_COPY(to, from, size, label)				\
+	unsafe_copy_from_user(to, (void __user *)from, size, label)
+
+#define UNSAFE_USER_GET(to, from, type, label)				\
+	unsafe_get_user(to, (type __user *)from, label)
+
+#else /* !CONFIG_HAVE_UNWIND_USER_SFRAME */
+
+#define UNSAFE_USER_COPY(to, from, size, label) do {			\
+	(void)to; (void)from; (void)size;				\
+	goto label;							\
+} while (0)
+
+#define UNSAFE_USER_GET(to, from, type, label) do {			\
+	(void)to; (void)from;						\
+	goto label;							\
+} while (0)
+
+#endif /* !CONFIG_HAVE_UNWIND_USER_SFRAME */
+
+#ifdef CONFIG_SFRAME_UNWINDER
+
+#define KERNEL_COPY(to, from, size) memcpy(to, (void *)from, size)
+#define KERNEL_GET(to, from, type) ({ (to) = *(type *)(from); })
+
+#else /* !CONFIG_SFRAME_UNWINDER */

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

Quoted 10 lines
kernel/unwind/sframe.c +10 -0
+
+#define KERNEL_COPY(to, from, size) do {				\
+	(void)(to); (void)(from); (void)size;				\
+	return -EFAULT;							\
+} while (0)
+
+#define KERNEL_GET(to, from, type) do {					\
+	(void)(to); (void)(from);					\
+	return -EFAULT;							\
+} while (0)

The error return value in above dummy implementations is never used (see DATA_COPY() and DATA_GET() below). Maybe better define the KERNEL flavors with the same interface as the UNSAFE_USER ones (with error label) and have the dummy implementations goto that label?

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

Quoted 2 lines
kernel/unwind/sframe.c +2 -0
+
+

Nit: Two instead of one empty line.

Quoted 23 lines
kernel/unwind/sframe.c +23 -0
+#endif /* !CONFIG_SFRAME_UNWINDER */
+
+#define DATA_COPY(sec, to, from, size, label)			\
+({								\
+	switch (sec->sec_type) {				\
+	case SFRAME_KERNEL:					\
+		KERNEL_COPY(to, from, size);			\
+		break;						\
+	case SFRAME_USER:					\
+		UNSAFE_USER_COPY(to, from, size, label);	\
+		break;						\
+	default:						\
+		return -EFAULT;					\
+	}							\
+})
+
+#define DATA_GET(sec, to, from, type, label)			\
+({								\
+	switch (sec->sec_type) {				\
+	case SFRAME_KERNEL:					\
+		KERNEL_GET(to, from, type);			\
+		break;						\
+	case SFRAME_USER:					\

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

Quoted 10 lines
kernel/unwind/sframe.c +7 -0
+		UNSAFE_USER_GET(to, from, type, label);		\
+		break;						\
+	default:						\
+		return -EFAULT;					\
+	}							\
+})
+
 static __always_inline int __read_fde(struct sframe_section *sec,
 				      unsigned int fde_num,
 				      struct sframe_fde_internal *fde)

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

PATCH v3 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                           |  7 +++++++
 arch/arm64/Kconfig                     |  1 +
 arch/arm64/Kconfig.debug               | 13 +++++++++++++
 arch/arm64/include/asm/unwind_sframe.h | 12 ++++++++++++
 arch/arm64/kernel/vdso/Makefile        |  2 +-
 include/asm-generic/vmlinux.lds.h      | 15 +++++++++++++++
 8 files changed, 58 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..e03d09ea6a23 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_SFRAME_UNWINDER
+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 +7 -0
index 6695c222c728..c87e489fa978 100644
@@ -520,6 +520,13 @@ config SFRAME_VALIDATION
 
 	  If unsure, say N.
 
+config ARCH_SUPPORTS_SFRAME_UNWINDER
+	bool
+	help
+	  An architecture can select this if it  enables the sframe (Simple
+	  Frame) unwinder for unwinding kernel stack traces. It uses unwind
+	  table that is directly generatedby toolchain based on DWARF CFI information.
+
 config HAVE_PERF_REGS
 	bool
 	help
arch/arm64/Kconfig +1 -0
index 38dba5f7e4d2..189bc199ad2e 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_SFRAME_UNWINDER
 	select ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
 	select ARCH_WANT_COMPAT_IPC_PARSE_VERSION if COMPAT
 	select ARCH_WANT_DEFAULT_BPF_JIT
arch/arm64/Kconfig.debug +13 -0
index 265c4461031f..df291d64812f 100644
@@ -20,4 +20,17 @@ config ARM64_RELOC_TEST
 	depends on m
 	tristate "Relocation testing module"
 
+config SFRAME_UNWINDER
+	bool "Sframe unwinder"
+	depends on AS_SFRAME3
+	depends on 64BIT
+	depends on ARCH_SUPPORTS_SFRAME_UNWINDER
+	select SFRAME_LOOKUP
+	help
+	  This option enables the sframe (Simple Frame) unwinder for unwinding
+	  kernel stack traces. It uses unwind table that is directly generated
+	  by toolchain based on DWARF CFI information. In, practice this can
+	  provide more reliable stacktrace results than unwinding with frame
+	  pointers alone.
+
 source "drivers/hwtracing/coresight/Kconfig"
arch/arm64/include/asm/unwind_sframe.h new file +12 -0
new file mode 100644
index 000000000000..1682c079e387
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_ARM64_UNWIND_SFRAME_H
+#define _ASM_ARM64_UNWIND_SFRAME_H
+
+#ifdef CONFIG_ARM64
+
+#define SFRAME_REG_SP	31
+#define SFRAME_REG_FP	29
+
+#endif
+
+#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/vmlinux.lds.h +15 -0
index 1e1580febe4b..0a5c2f6cc4c0 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_SFRAME_UNWINDER
+#define SFRAME							\
+	/* sframe */						\
+	.sframe : AT(ADDR(.sframe) - LOAD_OFFSET) {		\
+		__start_sframe_header = .;			\
+		KEEP(*(.sframe))				\
+		KEEP(*(.init.sframe))				\
+		__stop_sframe_header = .;			\
+	}
+#else
+#define SFRAME
+#endif
+
 #ifdef CONFIG_PRINTK_INDEX
 #define PRINTK_INDEX							\
 	.printk_index : AT(ADDR(.printk_index) - LOAD_OFFSET) {		\
Signature · 1 line
2.53.0.1213.gd9a14994de-goog
#4 lore raw

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

Quoted 29 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.

---
 MAINTAINERS                            |  1 +
 Makefile                               |  8 ++++++++
 arch/Kconfig                           |  7 +++++++
 arch/arm64/Kconfig                     |  1 +
 arch/arm64/Kconfig.debug               | 13 +++++++++++++
 arch/arm64/include/asm/unwind_sframe.h | 12 ++++++++++++
 arch/arm64/kernel/vdso/Makefile        |  2 +-
 include/asm-generic/vmlinux.lds.h      | 15 +++++++++++++++
 8 files changed, 58 insertions(+), 1 deletion(-)
 create mode 100644 arch/arm64/include/asm/unwind_sframe.h
arch/Kconfig +0 -0
index 6695c222c728..c87e489fa978 100644

arch/Kconfig config SFRAME_VALIDATION · line 526

Quoted 7 lines
arch/Kconfig +4 -0
@@ -520,6 +520,13 @@ config SFRAME_VALIDATION
 
 	  If unsure, say N.
 
+config ARCH_SUPPORTS_SFRAME_UNWINDER
+	bool
+	help
+	  An architecture can select this if it  enables the sframe (Simple

drop one space^^ (if it)

arch/Kconfig config SFRAME_VALIDATION · line 527

Quoted 1 line
arch/Kconfig +1 -0
+	  Frame) unwinder for unwinding kernel stack traces. It uses unwind

an unwind

arch/Kconfig config SFRAME_VALIDATION · line 528

Quoted 1 line
arch/Kconfig +1 -0
+	  table that is directly generatedby toolchain based on DWARF CFI information.

generated by the toolchain

Quoted 12 lines
arch/Kconfig +1 -0
+
 config HAVE_PERF_REGS
 	bool
 	help

index 265c4461031f..df291d64812f 100644

arch/arm64/Kconfig.debug +0 -0
@@ -20,4 +20,17 @@ config ARM64_RELOC_TEST
 	depends on m
 	tristate "Relocation testing module"

arch/arm64/Kconfig.debug config ARM64_RELOC_TEST · line 31

Quoted 10 lines
arch/arm64/Kconfig.debug +9 -0
 
+config SFRAME_UNWINDER
+	bool "Sframe unwinder"
+	depends on AS_SFRAME3
+	depends on 64BIT
+	depends on ARCH_SUPPORTS_SFRAME_UNWINDER
+	select SFRAME_LOOKUP
+	help
+	  This option enables the sframe (Simple Frame) unwinder for unwinding
+	  kernel stack traces. It uses unwind table that is directly generated

uses an unwind table

arch/arm64/Kconfig.debug config ARM64_RELOC_TEST · line 32

Quoted 1 line
arch/arm64/Kconfig.debug +1 -0
+	  by toolchain based on DWARF CFI information. In, practice this can

by the toolchain

Quoted 4 lines
arch/arm64/Kconfig.debug +3 -0
+	  provide more reliable stacktrace results than unwinding with frame
+	  pointers alone.
+
 source "drivers/hwtracing/coresight/Kconfig"
Signature · 1 line
~Randy
#5 lore raw

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

In reply to Dylan Hatch
Quoted 13 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.

MAINTAINERS STACK UNWINDING · line 27567

Quoted 7 lines
MAINTAINERS +1 -0
@@ -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/

Good catch! Should we rather add the following in the series you are basing on, as there are already arch-specific unwind_user.h and unwind_user_sframe.h?

F: arch/*/include/asm/unwind*.h

On the other hand I wonder whether the arch-specific headers should remain maintained by the respective arch maintainers? How is that handled in general?

arch/Kconfig config SFRAME_VALIDATION · line 528

Quoted 12 lines
arch/Kconfig +6 -0
@@ -520,6 +520,13 @@ config SFRAME_VALIDATION
 
 	  If unsure, say N.
 
+config ARCH_SUPPORTS_SFRAME_UNWINDER
+	bool
+	help
+	  An architecture can select this if it  enables the sframe (Simple
+	  Frame) unwinder for unwinding kernel stack traces. It uses unwind
+	  table that is directly generatedby toolchain based on DWARF CFI information.

Nit: s/sframe/SFrame/

Quoted 19 lines
arch/Kconfig +1 -0
+
 config HAVE_PERF_REGS
 	bool
 	help
arch/arm64/Kconfig +1 -0
@@ -112,6 +112,7 @@ config ARM64
 	select ARCH_SUPPORTS_SCHED_SMT
 	select ARCH_SUPPORTS_SCHED_CLUSTER
 	select ARCH_SUPPORTS_SCHED_MC
+	select ARCH_SUPPORTS_SFRAME_UNWINDER
 	select ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
 	select ARCH_WANT_COMPAT_IPC_PARSE_VERSION if COMPAT
 	select ARCH_WANT_DEFAULT_BPF_JIT

arch/arm64/Kconfig.debug config ARM64_RELOC_TEST · line 23

Quoted 4 lines
arch/arm64/Kconfig.debug +1 -0
@@ -20,4 +20,17 @@ config ARM64_RELOC_TEST
 	depends on m
 	tristate "Relocation testing module"
 
+config SFRAME_UNWINDER

Why do you introduce this for arm64 (and debug) only? If s390 were to add support (as replacement for s390 backchain), this would have to be moved or duplicated. It would not suffice to enable ARCH_SUPPORTS_SFRAME_UNWINDER to also enable SFRAME_UNWINDER.

As mentioned in my feedback on the previous patch in this series:
Would it make sense to align the naming to the existing HAVE_UNWIND_USER_SFRAME, for instance:

HAVE_UNWIND_KERNEL_SFRAME

arch/arm64/Kconfig.debug config ARM64_RELOC_TEST · line 34

Quoted 11 lines
arch/arm64/Kconfig.debug +11 -0
+	bool "Sframe unwinder"
+	depends on AS_SFRAME3
+	depends on 64BIT
+	depends on ARCH_SUPPORTS_SFRAME_UNWINDER
+	select SFRAME_LOOKUP
+	help
+	  This option enables the sframe (Simple Frame) unwinder for unwinding
+	  kernel stack traces. It uses unwind table that is directly generated
+	  by toolchain based on DWARF CFI information. In, practice this can
+	  provide more reliable stacktrace results than unwinding with frame
+	  pointers alone.

Nit: s/sframe/SFrame/

arch/arm64/Kconfig.debug config ARM64_RELOC_TEST · line 36

Quoted 2 lines
arch/arm64/Kconfig.debug +1 -0
+
 source "drivers/hwtracing/coresight/Kconfig"

You are introducing two new Kconfig options (SFRAME_UNWINDER and ARCH_SUPPORTS_SFRAME_UNWINDER). I wonder whether they could somehow be combined into a single new option. Although I am not sure how an option can be both selectable and depending at the same time, so that the ARM64 config could select it, but it would also depend on the above.

Quoted 12 lines
include/asm-generic/vmlinux.lds.h +2 -0
@@ -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) \

include/asm-generic/vmlinux.lds.h line 920

Quoted 8 lines
include/asm-generic/vmlinux.lds.h +5 -0
@@ -911,6 +913,19 @@
 #define TRACEDATA
 #endif
 
+#ifdef CONFIG_SFRAME_UNWINDER
+#define SFRAME							\
+	/* sframe */						\
+	.sframe : AT(ADDR(.sframe) - LOAD_OFFSET) {		\
+		__start_sframe_header = .;			\
		__start_sframe[_section] = .;

include/asm-generic/vmlinux.lds.h line 923

Quoted 3 lines
include/asm-generic/vmlinux.lds.h +3 -0
+		KEEP(*(.sframe))				\
+		KEEP(*(.init.sframe))				\
+		__stop_sframe_header = .;			\
		__stop_sframe[_section] = .;

Unless I am missing something both are not the start/stop of the .sframe header (in the .sframe section) but the .sframe section itself (see also your subsequent "[PATCH v3 4/8] sframe: Provide PC lookup for vmlinux .sframe section." where you assign both to kernel_sfsec.sframe_start and kernel_sfsec.sframe_end.

include/asm-generic/vmlinux.lds.h line 931

Quoted 8 lines
include/asm-generic/vmlinux.lds.h +5 -0
+	}
+#else
+#define SFRAME
+#endif
+
 #ifdef CONFIG_PRINTK_INDEX
 #define PRINTK_INDEX							\
 	.printk_index : AT(ADDR(.printk_index) - LOAD_OFFSET) {		\

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/
#6 lore raw

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

Quoted 7 lines · Jens Remus

Good catch! Should we rather add the following in the series you are basing on, as there are already arch-specific unwind_user.h and unwind_user_sframe.h?

F: arch/*/include/asm/unwind*.h

Thanks for the suggestion, this works for me.

Quoted 4 lines

On the other hand I wonder whether the arch-specific headers should remain maintained by the respective arch maintainers? How is that handled in general?

I had the same question. My scan of MAINTAINERS shows both patterns are present, so I defer to those who know more about this kind of maintainership configuration.

Quoted 38 lines

arch/Kconfig config SFRAME_VALIDATION · line 528

Quoted 13 lines
arch/Kconfig +6 -0
@@ -520,6 +520,13 @@ config SFRAME_VALIDATION
 
        If unsure, say N.
 
+config ARCH_SUPPORTS_SFRAME_UNWINDER
+     bool
+     help
+       An architecture can select this if it  enables the sframe (Simple
+       Frame) unwinder for unwinding kernel stack traces. It uses unwind
+       table that is directly generatedby toolchain based on DWARF CFI information.

Nit: s/sframe/SFrame/

Quoted 22 lines
arch/Kconfig +1 -0
+
 config HAVE_PERF_REGS
      bool
      help
arch/arm64/Kconfig +1 -0
@@ -112,6 +112,7 @@ config ARM64
      select ARCH_SUPPORTS_SCHED_SMT
      select ARCH_SUPPORTS_SCHED_CLUSTER
      select ARCH_SUPPORTS_SCHED_MC
+     select ARCH_SUPPORTS_SFRAME_UNWINDER
      select ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
      select ARCH_WANT_COMPAT_IPC_PARSE_VERSION if COMPAT
      select ARCH_WANT_DEFAULT_BPF_JIT
arch/arm64/Kconfig.debug +0 -0
@@ -20,4 +20,17 @@ config ARM64_RELOC_TEST
      depends on m
      tristate "Relocation testing module"
 
Quoted 6 lines

arch/arm64/Kconfig.debug config ARM64_RELOC_TEST · line 23

Quoted 1 line
arch/arm64/Kconfig.debug +1 -0
+config SFRAME_UNWINDER

Why do you introduce this for arm64 (and debug) only? If s390 were to add support (as replacement for s390 backchain), this would have to be moved or duplicated. It would not suffice to enable ARCH_SUPPORTS_SFRAME_UNWINDER to also enable SFRAME_UNWINDER.

Makes sense, I'll look into introducing this as arch-generic.

Quoted 21 lines

As mentioned in my feedback on the previous patch in this series:
Would it make sense to align the naming to the existing HAVE_UNWIND_USER_SFRAME, for instance:

HAVE_UNWIND_KERNEL_SFRAME

arch/arm64/Kconfig.debug config ARM64_RELOC_TEST · line 34

Quoted 11 lines
arch/arm64/Kconfig.debug +11 -0
+     bool "Sframe unwinder"
+     depends on AS_SFRAME3
+     depends on 64BIT
+     depends on ARCH_SUPPORTS_SFRAME_UNWINDER
+     select SFRAME_LOOKUP
+     help
+       This option enables the sframe (Simple Frame) unwinder for unwinding
+       kernel stack traces. It uses unwind table that is directly generated
+       by toolchain based on DWARF CFI information. In, practice this can
+       provide more reliable stacktrace results than unwinding with frame
+       pointers alone.

Nit: s/sframe/SFrame/

Quoted 8 lines

arch/arm64/Kconfig.debug config ARM64_RELOC_TEST · line 36

Quoted 2 lines
arch/arm64/Kconfig.debug +1 -0
+
 source "drivers/hwtracing/coresight/Kconfig"

You are introducing two new Kconfig options (SFRAME_UNWINDER and ARCH_SUPPORTS_SFRAME_UNWINDER). I wonder whether they could somehow be combined into a single new option. Although I am not sure how an option can be both selectable and depending at the same time, so that the ARM64 config could select it, but it would also depend on the above.

I don't think this is recommended, since the behavior of 'select' appears to override a 'depends' requirement.

From Documentation/kbuild/kconfig-language.rst: "select should be used with care. select will force a symbol to a value without visiting the dependencies. By abusing select you are able to select a symbol FOO even if FOO depends on BAR that is not set. In general use select only for non-visible symbols (no prompts anywhere) and for symbols with no dependencies. That will limit the usefulness but on the other hand avoid the illegal configurations all over."

Quoted 52 lines
Quoted 13 lines
include/asm-generic/vmlinux.lds.h +2 -0
@@ -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) \

include/asm-generic/vmlinux.lds.h line 920

Quoted 8 lines
include/asm-generic/vmlinux.lds.h +5 -0
@@ -911,6 +913,19 @@
 #define TRACEDATA
 #endif
 
+#ifdef CONFIG_SFRAME_UNWINDER
+#define SFRAME                                                       \
+     /* sframe */                                            \
+     .sframe : AT(ADDR(.sframe) - LOAD_OFFSET) {             \
+             __start_sframe_header = .;                      \
                __start_sframe[_section] = .;

include/asm-generic/vmlinux.lds.h line 923

Quoted 3 lines
include/asm-generic/vmlinux.lds.h +3 -0
+             KEEP(*(.sframe))                                \
+             KEEP(*(.init.sframe))                           \
+             __stop_sframe_header = .;                       \
                __stop_sframe[_section] = .;

Unless I am missing something both are not the start/stop of the .sframe header (in the .sframe section) but the .sframe section itself (see also your subsequent "[PATCH v3 4/8] sframe: Provide PC lookup for vmlinux .sframe section." where you assign both to kernel_sfsec.sframe_start and kernel_sfsec.sframe_end.

include/asm-generic/vmlinux.lds.h line 931

Quoted 8 lines
include/asm-generic/vmlinux.lds.h +5 -0
+     }
+#else
+#define SFRAME
+#endif
+
 #ifdef CONFIG_PRINTK_INDEX
 #define PRINTK_INDEX                                                 \
      .printk_index : AT(ADDR(.printk_index) - LOAD_OFFSET) {         \

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/

I'm working on a new version that I'm hoping to be able to send sometime next week, which should address your other comments.

Thanks,
Dylan

#7 lore raw

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

Quoted 11 lines · Dylan Hatch
Quoted 5 lines · Jens Remus

You are introducing two new Kconfig options (SFRAME_UNWINDER and ARCH_SUPPORTS_SFRAME_UNWINDER). I wonder whether they could somehow be combined into a single new option. Although I am not sure how an option can be both selectable and depending at the same time, so that the ARM64 config could select it, but it would also depend on the above.

I don't think this is recommended, since the behavior of 'select' appears to override a 'depends' requirement.

Quoted 7 lines

From Documentation/kbuild/kconfig-language.rst: "select should be used with care. select will force a symbol to a value without visiting the dependencies. By abusing select you are able to select a symbol FOO even if FOO depends on BAR that is not set. In general use select only for non-visible symbols (no prompts anywhere) and for symbols with no dependencies. That will limit the usefulness but on the other hand avoid the illegal configurations all over."

Thanks for the explanation! So both options cannot be merged into one. Maybe the option names can still be aligned, so that they have UNWIND_KERNEL_SFRAME in common and the kernel and user space sframe unwinder options have HAVE_UNWIND in common?

SFRAME_UNWINDER -> HAVE_UNWIND_KERNEL_SFRAME ARCH_SUPPORTS_SFRAME_UNWINDER -> ARCH_SUPPORTS_UNWIND_KERNEL_SFRAME

That would then align nicely with the existing:

HAVE_UNWIND_USER_SFRAME

The only downside is that the user variant would get selected via HAVE_UNWIND_USER_SFRAME and the kernel variant via ARCH_SUPPORTS_UNWIND_KERNEL_SFRAME.

Quoted 35 lines
Quoted 35 lines
Quoted 12 lines
include/asm-generic/vmlinux.lds.h +2 -0
@@ -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) \

include/asm-generic/vmlinux.lds.h line 920

Quoted 8 lines
include/asm-generic/vmlinux.lds.h +5 -0
@@ -911,6 +913,19 @@
 #define TRACEDATA
 #endif
 
+#ifdef CONFIG_SFRAME_UNWINDER
+#define SFRAME                                                       \
+     /* sframe */                                            \
+     .sframe : AT(ADDR(.sframe) - LOAD_OFFSET) {             \
+             __start_sframe_header = .;                      \
                __start_sframe[_section] = .;

include/asm-generic/vmlinux.lds.h line 923

Quoted 3 lines
include/asm-generic/vmlinux.lds.h +3 -0
+             KEEP(*(.sframe))                                \
+             KEEP(*(.init.sframe))                           \
+             __stop_sframe_header = .;                       \
                __stop_sframe[_section] = .;

Unless I am missing something both are not the start/stop of the .sframe header (in the .sframe section) but the .sframe section itself (see also your subsequent "[PATCH v3 4/8] sframe: Provide PC lookup for vmlinux .sframe section." where you assign both to kernel_sfsec.sframe_start and kernel_sfsec.sframe_end.

Quoted 8 lines
Quoted 8 lines

include/asm-generic/vmlinux.lds.h line 931

Quoted 8 lines
include/asm-generic/vmlinux.lds.h +5 -0
+     }
+#else
+#define SFRAME
+#endif
+
 #ifdef CONFIG_PRINTK_INDEX
 #define PRINTK_INDEX                                                 \
      .printk_index : AT(ADDR(.printk_index) - LOAD_OFFSET) {         \

What about the following? Note that I also aligned the indentation in vmlinux.lds.h to the one in the blocks above/below.

include/asm-generic/sections.h +4 -0
@@ -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[];
 
+/* Stand end 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 +7 -7
@@ -914,13 +914,13 @@
 #endif
 
 #ifdef CONFIG_SFRAME_UNWINDER
-#define SFRAME							\
-	/* sframe */						\
-	.sframe : AT(ADDR(.sframe) - LOAD_OFFSET) {		\
-		__start_sframe_header = .;			\
-		KEEP(*(.sframe))				\
-		KEEP(*(.init.sframe))				\
-		__stop_sframe_header = .;			\
+#define SFRAME								\
+	/* sframe */							\
+	.sframe : AT(ADDR(.sframe) - LOAD_OFFSET) {			\
+		__start_sframe = .;					\
+		KEEP(*(.sframe))					\
+		KEEP(*(.init.sframe))					\
+		__end_sframe = .;					\
        }
 #else
 #define SFRAME

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/
#8 lore raw

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

In reply to Dylan Hatch
Quoted 14 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.

arch/arm64/include/asm/unwind_sframe.h line 5

Quoted 5 lines
arch/arm64/include/asm/unwind_sframe.h +5 -0
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_ARM64_UNWIND_SFRAME_H
+#define _ASM_ARM64_UNWIND_SFRAME_H
+
+#ifdef CONFIG_ARM64

Nit: If I am not mistaken above guard for CONFIG_ARM64 is superfluous, as this resides in arch/arm64/ anyway.

x86 needs to guard with CONFIG_X86_64, as only x86 64-bit supports SFrame (and not x86 32-bit nor x86 X32).

arch/arm64/include/asm/unwind_sframe.h line 12

Quoted 7 lines
arch/arm64/include/asm/unwind_sframe.h +7 -0
+
+#define SFRAME_REG_SP	31
+#define SFRAME_REG_FP	29
+
+#endif
+
+#endif /* _ASM_ARM64_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/
#9 lore raw

PATCH v3 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 CFI unwind info for assembly for interrupt and exception handlers.

---
 arch/arm64/kernel/entry.S | 10 ++++++++++
 1 file changed, 10 insertions(+)
arch/arm64/kernel/entry.S +10 -0
index f8018b5c1f9a..3148ede8c2c6 100644
@@ -575,7 +575,12 @@ SYM_CODE_START_LOCAL(el\el\ht\()_\regsize\()_\label)
 	.if \el == 0
 	b	ret_to_user
 	.else
+	.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
@@ -889,6 +894,10 @@ SYM_FUNC_START(call_on_irq_stack)
 	add	sp, x16, #IRQ_STACK_SIZE
 	restore_irq x9
 	blr	x1
+	.cfi_startproc
+	.cfi_def_cfa 29, 16
+	.cfi_offset 29, -16
+	.cfi_offset 30, -8
 
 	save_and_disable_daif x9
 	/*
@@ -900,6 +909,7 @@ SYM_FUNC_START(call_on_irq_stack)
 	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.53.0.1213.gd9a14994de-goog
#10 lore raw

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

Hello Dylan and Weinan!

Quoted 10 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.

Annotate CFI unwind info for assembly for interrupt and exception handlers.

It took me a while to figure, why CFI annotations are uncommonly only added to selected instruction (ranges) and not the whole functions. I guess you only want to enable stacktracing using SFrame through el1*_64_*() (from el1*_64_*_handler()) and call_on_irq_stack(), that is why the added CFI annotations start after the bl/blr instructions, so that whenever an unwound return address points after those bl/blr SFrame can recover the stack pointer, frame pointer, and return address.

Wouldn't that be worth to be documented in the commit message?

arch/arm64/kernel/entry.S SYM_CODE_START_LOCAL(el\el\ht\()_\regsize\()_\label) · line 577

Quoted 9 lines
arch/arm64/kernel/entry.S +0 -0
@@ -575,7 +575,12 @@ SYM_CODE_START_LOCAL(el\el\ht\()_\regsize\()_\label)
 	.if \el == 0
 	b	ret_to_user
 	.else
	/* Minimal DWARF CFI for unwinding across call above. */
Quoted 11 lines
arch/arm64/kernel/entry.S +5 -0
+	.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

arch/arm64/kernel/entry.S SYM_FUNC_START(call_on_irq_stack) · line 896

Quoted 3 lines
arch/arm64/kernel/entry.S +0 -0
@@ -889,6 +894,10 @@ SYM_FUNC_START(call_on_irq_stack)
 	add	sp, x16, #IRQ_STACK_SIZE
 	restore_irq x9
 	blr	x1
	/* Minimal DWARF CFI for unwinding across indirect call above. */
Quoted 8 lines
arch/arm64/kernel/entry.S +4 -0
+	.cfi_startproc
+	.cfi_def_cfa 29, 16
+	.cfi_offset 29, -16
+	.cfi_offset 30, -8
 
 	save_and_disable_daif x9
 	/*

arch/arm64/kernel/entry.S SYM_FUNC_START(call_on_irq_stack) · line 914

Quoted 6 lines
arch/arm64/kernel/entry.S +1 -0
@@ -900,6 +909,7 @@ SYM_FUNC_START(call_on_irq_stack)
 	scs_load_current
 	restore_irq x9
 	ret
+	.cfi_endproc
 SYM_FUNC_END(call_on_irq_stack)
 NOKPROBE(call_on_irq_stack)

While above minimal DWARF CFI works for your use case, the following minor issue should probably be better corrected (excerpt from call_on_irq_stack with your patch applied):

	blr	x1
	.cfi_startproc
	.cfi_def_cfa 29, 16   <-- CFA is defined as FP + 16
	.cfi_offset 29, -16
	.cfi_offset 30, -8

	save_and_disable_daif x9
	/*
	 * Restore the SP from the FP, and restore the FP and LR from the frame
	 * record.
	 */
	mov	sp, x29
	ldp	x29, x30, [sp], #16   <-- FP is restored, so that the CFA definition is no longer valid
[CORRECTION]
	.cfi_restore 29
	.cfi_restore 30
	.cfi_def_cfa 31, 0

[/CORRECTION]
scs_load_current
restore_irq x9
ret
.cfi_endproc
SYM_FUNC_END(call_on_irq_stack)

Would it alternatively make sense to add complete DWARF CFI annotations to call_on_irq_stack()? I think the following would do:

SYM_FUNC_START(call_on_irq_stack)
.cfi_startproc
...

	/* 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
...
	/*
	 * Restore the SP from the FP, and restore the FP and LR from the frame
	 * record.
	 */
	mov	sp, x29
	ldp	x29, x30, [sp], #16
	.cfi_restore 29
	.cfi_restore 30
	.cfi_def_cfa 31, 0

...
ret
.cfi_endproc
SYM_FUNC_END(call_on_irq_stack)

Thanks and 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/
#11 lore raw

Re: [PATCH v3 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 8 lines

Annotate CFI unwind info for assembly for interrupt and exception handlers.

---
 arch/arm64/kernel/entry.S | 10 ++++++++++
 1 file changed, 10 insertions(+)

The added CFI directives somehow cause .eh_frame (instead of .debug_frame) to be generated in addition to .sframe. This causes the following warning when linking vmlinux (.tmp_vmlinux1, .tmp_vmlinux2, and vmlinux.unstripped):

LD vmlinux.unstripped
aarch64-linux-gnu-ld: warning: orphan section .eh_frame' from arch/arm64/kernel/entry.o' being placed in section `.eh_frame'

I don't think this can be controlled using compiler options -fno-asynchronous-unwind-tables -fno-unwind-tables, as entry.S is only preprocessed and then fed into the assembler.

The following at the top of entry.S would resolve the issue:

arch/arm64/kernel/entry.S +6 -0
@@ -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

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/
#12 lore raw

PATCH v3 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    | 39 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 55 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 673b9edfc921..905775c3fde2 100644
@@ -109,4 +109,18 @@ static inline int sframe_find_user(unsigned long ip, struct unwind_frame *frame)
 
 #endif /* CONFIG_HAVE_UNWIND_USER_SFRAME */
 
+#ifdef CONFIG_SFRAME_UNWINDER
+
+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_SFRAME_UNWINDER */
+
 #endif /* _LINUX_SFRAME_H */
kernel/unwind/sframe.c +39 -0
index cad4384dfb4f..321d0615aec7 100644
@@ -13,10 +13,23 @@
 #include <linux/string_helpers.h>
 #include <linux/sframe.h>
 #include <asm/unwind_sframe.h>
+#ifdef CONFIG_SFRAME_UNWINDER
+#include <linux/kallsyms.h>
+#endif
 
 #include "sframe.h"
 #include "sframe_debug.h"
 
+#ifdef CONFIG_SFRAME_UNWINDER
+
+extern char __start_sframe_header[];
+extern char __stop_sframe_header[];
+
+static bool sframe_init __ro_after_init;
+static struct sframe_section kernel_sfsec __ro_after_init;
+
+#endif /* CONFIG_SFRAME_UNWINDER */
+
 struct sframe_fde_internal {
 	unsigned long	func_addr;
 	u32		func_size;
@@ -930,3 +943,29 @@ void sframe_free_mm(struct mm_struct *mm)
 }
 
 #endif /* CONFIG_HAVE_UNWIND_USER_SFRAME */
+
+#ifdef CONFIG_SFRAME_UNWINDER
+
+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_header;
+	kernel_sfsec.sframe_end		= (unsigned long)__stop_sframe_header;
+	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_SFRAME_UNWINDER */
Signature · 1 line
2.53.0.1213.gd9a14994de-goog
Reviewed-by on v3 4/8
#13 lore raw

Re: [PATCH v3 4/8] sframe: Provide PC lookup for vmlinux .sframe section.

Quoted 5 lines · 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.

Quoted 5 lines
---
 arch/arm64/kernel/setup.c |  2 ++
 include/linux/sframe.h    | 14 ++++++++++++++
 kernel/unwind/sframe.c    | 39 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 55 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/
#14 lore raw

PATCH v3 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 | 44 +++++++++++++++++++++++++++++++++++++-----
 2 files changed, 40 insertions(+), 5 deletions(-)
include/linux/sframe.h +1 -0
index 905775c3fde2..593b60715cd6 100644
@@ -64,6 +64,7 @@ struct sframe_section {
 	unsigned long		text_start;
 	unsigned long		text_end;
 
+	bool			fdes_sorted;
 	unsigned long		fdes_start;
 	unsigned long		fres_start;
 	unsigned long		fres_end;
kernel/unwind/sframe.c +39 -5

74 lines not on this page · raw message

Signature · 1 line
2.53.0.1213.gd9a14994de-goog
Reviewed-by on v3 5/8
#15 lore raw

Re: [PATCH v3 5/8] sframe: Allow unsorted FDEs.

Quoted 5 lines · 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.

With my below two minor comments considered:

include/linux/sframe.h struct sframe_section { · line 70

Quoted 10 lines
include/linux/sframe.h +1 -0
@@ -64,6 +64,7 @@ struct sframe_section {
 	unsigned long		text_start;
 	unsigned long		text_end;
 
+	bool			fdes_sorted;
 	unsigned long		fdes_start;
 	unsigned long		fres_start;
 	unsigned long		fres_end;

The struct would be smaller if the bool fdes_sorted flag would be inserted after the unsigned int num_fdes field:

$ pahole -C sframe_section kernel/unwind/sframe.o

Yours:
size: 96

With bool fdes_sorted moved after unsigned int num_fdes:
size: 88

Quoted 20 lines
kernel/unwind/sframe.c +11 -3
@@ -179,9 +179,34 @@ static __always_inline int __read_fde(struct sframe_section *sec,
 	return -EFAULT;
 }
 
-static __always_inline int __find_fde(struct sframe_section *sec,
-				      unsigned long ip,
-				      struct sframe_fde_internal *fde)
+static __always_inline int __find_fde_unsorted(struct sframe_section *sec,
+					       unsigned long ip,
+					       struct sframe_fde_internal *fde)
+{
+	struct sframe_fde_v3 *cur, *start, *end;
+
+	start = (struct sframe_fde_v3 *)sec->fdes_start;
+	end = start + sec->num_fdes;
+
+	for (cur = start; cur < end; cur++) {
+		s64 func_off;

kernel/unwind/sframe.c static __always_inline int __read_fde(struct sframe_section *sec, · line 202

Quoted 10 lines
kernel/unwind/sframe.c +10 -0
+		u32 func_size;
+		unsigned long func_addr;
+
+		DATA_GET(sec, func_off, &cur->func_start_off, s64, Efault);
+		DATA_GET(sec, func_size, &cur->func_size, u32, Efault);
+		func_addr = (unsigned long)cur + func_off;
+
+		if (ip >= func_addr && ip < func_addr + func_size)
+			return __read_fde(sec, cur - start, fde);
+	}

__find_fde() (now __find_fde_sorted()) returns -EINVAL, if no FDE is found:

	return -EINVAL;

kernel/unwind/sframe.c static __always_inline int __read_fde(struct sframe_section *sec, · line 205

Quoted 3 lines
kernel/unwind/sframe.c +3 -0
+Efault:
+	return -EFAULT;
+}

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/
#16 lore raw

PATCH v3 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          | 39 +++++++++++++++++++++++++++++++--
 4 files changed, 53 insertions(+), 2 deletions(-)
arch/arm64/include/asm/module.h +6 -0
index fb9b88eebeb1..59fb6fba88d0 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_SFRAME_UNWINDER
+	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 593b60715cd6..06fdda1dd116 100644
@@ -121,6 +121,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_SFRAME_UNWINDER */
 
kernel/unwind/sframe.c +37 -2
index 4dd3612f9e7a..180f64040846 100644
@@ -982,10 +982,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)
@@ -1002,4 +1019,22 @@ 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;
+
+	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_SFRAME_UNWINDER */
Signature · 1 line
2.53.0.1213.gd9a14994de-goog
Reviewed-by on v3 6/8
#17 lore raw

Re: [PATCH v3 6/8] arm64/module, sframe: Add sframe support for modules.

Quoted 5 lines · Dylan Hatch

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

Quoted 6 lines
---
 arch/arm64/include/asm/module.h |  6 +++++
 arch/arm64/kernel/module.c      |  8 +++++++
 include/linux/sframe.h          |  2 ++
 kernel/unwind/sframe.c          | 39 +++++++++++++++++++++++++++++++--
 4 files changed, 53 insertions(+), 2 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/
#18 lore raw

Re: [PATCH v3 6/8] arm64/module, sframe: Add sframe support for modules.

In reply to Dylan Hatch

kernel/unwind/sframe.c void __init init_sframe_table(void) · line 1026

Quoted 13 lines · Dylan Hatch

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

kernel/unwind/sframe.c +5 -0
 
+void sframe_module_init(struct module *mod, void *sframe, size_t sframe_size,
+			void *text, size_t text_size)
+{
+	struct sframe_section sec;
+

I think sec should better be explicitly zero-initialized, as it is done in sframe_add_section() for user space. IIUC likewise is not needed in init_sframe_table() for the kernel vmlinux, as static variables are zero-initialized.

	memset(&sec, 0, sizeof(sec));

kernel/unwind/sframe.c void __init init_sframe_table(void) · line 1038

Quoted 12 lines
kernel/unwind/sframe.c +12 -0
+	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;
+}

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/
#19 lore raw

PATCH v3 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. Allow for kernel FDE read failures due to the presence of .rodata.text. This section contains code that can't be executed by the kernel direclty, and thus lies ouside the normal kernel-text bounds.

---
 arch/Kconfig           |  2 +-
 kernel/unwind/sframe.c | 20 ++++++++++++++++++++
 2 files changed, 21 insertions(+), 1 deletion(-)
arch/Kconfig +1 -1
index c87e489fa978..6e9f21231b98 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
kernel/unwind/sframe.c +20 -0
index 180f64040846..7096e0a244b4 100644
@@ -638,6 +638,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;
@@ -653,6 +656,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;
@@ -667,6 +673,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;
@@ -690,6 +699,13 @@ static int sframe_validate_section(struct sframe_section *sec)
 		int ret;
 
 		ret = safe_read_fde(sec, i, &fde);
+		/*
+		 * Code in .rodata.text is not considered part of normal kernel
+		 * text, but there is no easy way to prevent sframe data from
+		 * being generated for it.
+		 */
+		if (ret && sec->sec_type == SFRAME_KERNEL)
+			continue;
 		if (ret)
 			return ret;
 
@@ -1015,6 +1031,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;
 }
@@ -1032,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.53.0.1213.gd9a14994de-goog
#20 lore raw

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

Hello Dylan!

Quoted 4 lines · Dylan Hatch

Generalize the __safe* helpers to support a non-user-access code path. Allow for kernel FDE read failures due to the presence of .rodata.text. This section contains code that can't be executed by the kernel direclty, and thus lies ouside the normal kernel-text bounds.

Nits: s/direclty/directly/ s/ouside/outside/

Could you please explain the issue? How/why does .sframe for .rodata.text pose an issue for .sframe verification?

Quoted 8 lines
kernel/unwind/sframe.c +0 -0
@@ -690,6 +699,13 @@ static int sframe_validate_section(struct sframe_section *sec)
 		int ret;
 
 		ret = safe_read_fde(sec, i, &fde);

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

Quoted 10 lines
kernel/unwind/sframe.c +7 -0
+		/*
+		 * Code in .rodata.text is not considered part of normal kernel
+		 * text, but there is no easy way to prevent sframe data from
+		 * being generated for it.
+		 */
+		if (ret && sec->sec_type == SFRAME_KERNEL)
+			continue;
 		if (ret)
 			return ret;
 

Thanks and 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/
#21 lore raw

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

Quoted 9 lines · Jens Remus

Hello Dylan!

Quoted 4 lines · Dylan Hatch

Generalize the __safe* helpers to support a non-user-access code path. Allow for kernel FDE read failures due to the presence of .rodata.text. This section contains code that can't be executed by the kernel direclty, and thus lies ouside the normal kernel-text bounds.

Quoted 4 lines

Nits: s/direclty/directly/ s/ouside/outside/

Could you please explain the issue? How/why does .sframe for .rodata.text pose an issue for .sframe verification?

__read_fde checks that the fde_addr it extracts is within the bounds of sec->text_start and sec->text_end. In the case of the vmlinux .sframe section, this is _stext and _etext. However on arm64, there is an .rodata.text section that lies outside this range. From arch/arm64/kernel/vmlinux.lds.S:

        /* code sections that are never executed via the kernel mapping */
        .rodata.text : {
                TRAMP_TEXT
                HIBERNATE_TEXT
                KEXEC_TEXT
                IDMAP_TEXT
                . = ALIGN(PAGE_SIZE);
        }

So __read_fde fails for functions in this section. Under normal SFrame usage for unwinding, we should never need to look up a PC value in these functions because they will never be executed by the kernel. However, we still hit this error when validating all FDEs.

I think ideally we might prevent sframe data from being generated for this code (maybe from the linker script somehow?), but I don't know of a simple way to do this. Alternatively, we can check for FDEs located in .rodata.text during validation, but this seems to only be present in arm64, so maybe we would need an arch-specific hook to do this? I'm open to suggestions.

Quoted 26 lines
Quoted 9 lines
kernel/unwind/sframe.c +0 -0
@@ -690,6 +699,13 @@ static int sframe_validate_section(struct sframe_section *sec)
              int ret;
 
              ret = safe_read_fde(sec, i, &fde);

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

Quoted 10 lines
kernel/unwind/sframe.c +7 -0
+             /*
+              * Code in .rodata.text is not considered part of normal kernel
+              * text, but there is no easy way to prevent sframe data from
+              * being generated for it.
+              */
+             if (ret && sec->sec_type == SFRAME_KERNEL)
+                     continue;
              if (ret)
                      return ret;
 

Thanks and 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

#22 lore raw

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

Quoted 8 lines · Dylan Hatch
Quoted 7 lines · Jens Remus
Quoted 4 lines · Dylan Hatch

Generalize the __safe* helpers to support a non-user-access code path. Allow for kernel FDE read failures due to the presence of .rodata.text. This section contains code that can't be executed by the kernel direclty, and thus lies ouside the normal kernel-text bounds.

Quoted 7 lines
Quoted 4 lines

Nits: s/direclty/directly/ s/ouside/outside/

Could you please explain the issue? How/why does .sframe for .rodata.text pose an issue for .sframe verification?

__read_fde checks that the fde_addr it extracts is within the bounds of sec->text_start and sec->text_end. In the case of the vmlinux

Looking at the existing check in __read_fde(), do you agree that it is wrong, as sec->text_end IIUC points behind .text and thus the check should be:

	if (func_addr < sec->text_start || func_addr >= sec->text_end)
        	return -EINVAL;
Quoted 13 lines

.sframe section, this is _stext and _etext. However on arm64, there is an .rodata.text section that lies outside this range. From arch/arm64/kernel/vmlinux.lds.S:

        /* code sections that are never executed via the kernel mapping */
        .rodata.text : {
                TRAMP_TEXT
                HIBERNATE_TEXT
                KEXEC_TEXT
                IDMAP_TEXT
                . = ALIGN(PAGE_SIZE);
        }
Quoted 4 lines

So __read_fde fails for functions in this section. Under normal SFrame usage for unwinding, we should never need to look up a PC value in these functions because they will never be executed by the kernel. However, we still hit this error when validating all FDEs.

Thanks for the explanation! Could you please improve the commit message, for instance as follows:

__read_fde() checks that the extracted FDE function start address is within the bounds of the .text section start and end. In case of vmlinux this is _stext and _etext. However on arm64, .rodata.text resides outside this range, causing __read_fde() to fail.

Quoted 3 lines

I think ideally we might prevent sframe data from being generated for this code (maybe from the linker script somehow?), but I don't know of a simple way to do this.

I dont't know of any way to exclude a single function or a whole section from .sframe generation. The GNU linker would discard SFrame FDEs and its FREs for discarded functions. But in this case the function itself is not discarded. As .sframe is not generated separately per section it is also not possible to discard e.g. .sframe.rodata.

Quoted 4 lines

Alternatively, we can check for FDEs located
in .rodata.text during validation, but this seems to only be present in arm64, so maybe we would need an arch-specific hook to do this? I'm open to suggestions.

Maybe that is better than ignoring __read_fde() failures? I first thought this would get nasty, but maybe it would not be too bad. Following is what I came up with (note tabs replaced by spaces due to copy&paste from terminal):

arch/arm64/include/asm/sections.h +1 -0
@@ -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 +17 -0
@@ -2,11 +2,28 @@
 #ifndef _ASM_ARM64_UNWIND_SFRAME_H
 #define _ASM_ARM64_UNWIND_SFRAME_H
 
+#include <linux/sframe.h>
+
 #ifdef CONFIG_ARM64
 
 #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)
+{
+       return (sec->text_start >= func_addr && func_addr < sec->text_end) ||
+              (sec->rodatatext_start >= func_addr && func_addr < sec->rodatatext_end);
+}
+#define sframe_func_start_addr_valid sframe_func_start_addr_valid
+
+static void arch_init_sframe_table(struct sframe_section *kernel_sfsec)
+{
+       kernel_sfsec->rodatatext_start  = (unsigned long)_srodatatext;
+       kernel_sfsec->rodatatext_end    = (unsigned long)_erodatatext;
+}
+#define arch_init_sframe_table arch_init_sframe_table
+
 #endif
 
 #endif /* _ASM_ARM64_UNWIND_SFRAME_H */
arch/arm64/kernel/vmlinux.lds.S +2 -0
@@ -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 +4 -0
@@ -63,6 +63,10 @@ struct sframe_section {
        unsigned long           sframe_end;
        unsigned long           text_start;
        unsigned long           text_end;
+#if defined(CONFIG_SFRAME_UNWINDER) && defined(CONFIG_ARM64)
+       unsigned long           rodatatext_start;
+       unsigned long           rodatatext_end;
+#endif
 
        bool                    fdes_sorted;
        unsigned long           fdes_start;
kernel/unwind/sframe.c +15 -8
@@ -20,11 +20,23 @@
 #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_SFRAME_UNWINDER
 
 static bool sframe_init __ro_after_init;
 static struct sframe_section kernel_sfsec __ro_after_init;
 
+#ifndef arch_init_sframe_table
+static void arch_init_sframe_table(struct sframe_section *kernel_sfsec) {}
+#endif
+
 #endif /* CONFIG_SFRAME_UNWINDER */
 
 struct sframe_fde_internal {
@@ -152,7 +164,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;
@@ -696,13 +708,6 @@ static int sframe_validate_section(struct sframe_section *sec)
                int ret;
 
                ret = safe_read_fde(sec, i, &fde);
-               /*
-                * Code in .rodata.text is not considered part of normal kernel
-                * text, but there is no easy way to prevent sframe data from
-                * being generated for it.
-                */
-               if (ret && sec->sec_type == SFRAME_KERNEL)
-                       continue;
                if (ret)
                        return ret;
 
@@ -1031,6 +1036,8 @@ void __init init_sframe_table(void)
        if (WARN_ON(sframe_validate_section(&kernel_sfsec)))
                return;
 
+       arch_init_sframe_table(&kernel_sfsec);
+
        sframe_init = true;
 }
 

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/
#23 lore raw

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

Quoted 18 lines · Jens Remus
Quoted 8 lines · Dylan Hatch
Quoted 7 lines · Jens Remus
Quoted 4 lines · Dylan Hatch

Generalize the __safe* helpers to support a non-user-access code path. Allow for kernel FDE read failures due to the presence of .rodata.text. This section contains code that can't be executed by the kernel direclty, and thus lies ouside the normal kernel-text bounds.

Quoted 7 lines
Quoted 4 lines

Nits: s/direclty/directly/ s/ouside/outside/

Could you please explain the issue? How/why does .sframe for .rodata.text pose an issue for .sframe verification?

__read_fde checks that the fde_addr it extracts is within the bounds of sec->text_start and sec->text_end. In the case of the vmlinux

Quoted 6 lines

Looking at the existing check in __read_fde(), do you agree that it is wrong, as sec->text_end IIUC points behind .text and thus the check should be:

        if (func_addr < sec->text_start || func_addr >= sec->text_end)
                return -EINVAL;

I agree this is correct. Is this a fix that would be folded into your previous patch series?

Quoted 42 lines
Quoted 14 lines

.sframe section, this is _stext and _etext. However on arm64, there is an .rodata.text section that lies outside this range. From arch/arm64/kernel/vmlinux.lds.S:

        /* code sections that are never executed via the kernel mapping */
        .rodata.text : {
                TRAMP_TEXT
                HIBERNATE_TEXT
                KEXEC_TEXT
                IDMAP_TEXT
                . = ALIGN(PAGE_SIZE);
        }
Quoted 4 lines

So __read_fde fails for functions in this section. Under normal SFrame usage for unwinding, we should never need to look up a PC value in these functions because they will never be executed by the kernel. However, we still hit this error when validating all FDEs.

Thanks for the explanation! Could you please improve the commit message, for instance as follows:

__read_fde() checks that the extracted FDE function start address is within the bounds of the .text section start and end. In case of vmlinux this is _stext and _etext. However on arm64, .rodata.text resides outside this range, causing __read_fde() to fail.

Quoted 3 lines

I think ideally we might prevent sframe data from being generated for this code (maybe from the linker script somehow?), but I don't know of a simple way to do this.

I dont't know of any way to exclude a single function or a whole section from .sframe generation. The GNU linker would discard SFrame FDEs and its FREs for discarded functions. But in this case the function itself is not discarded. As .sframe is not generated separately per section it is also not possible to discard e.g. .sframe.rodata.

Quoted 4 lines

Alternatively, we can check for FDEs located
in .rodata.text during validation, but this seems to only be present in arm64, so maybe we would need an arch-specific hook to do this? I'm open to suggestions.

Quoted 4 lines

Maybe that is better than ignoring __read_fde() failures? I first thought this would get nasty, but maybe it would not be too bad. Following is what I came up with (note tabs replaced by spaces due to copy&paste from terminal):

Thanks for the suggestion! I'll look into incorporating this.

Quoted 58 lines
arch/arm64/include/asm/sections.h +1 -0
@@ -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 +17 -0
@@ -2,11 +2,28 @@
 #ifndef _ASM_ARM64_UNWIND_SFRAME_H
 #define _ASM_ARM64_UNWIND_SFRAME_H
 
+#include <linux/sframe.h>
+
 #ifdef CONFIG_ARM64
 
 #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)
+{
+       return (sec->text_start >= func_addr && func_addr < sec->text_end) ||
+              (sec->rodatatext_start >= func_addr && func_addr < sec->rodatatext_end);
+}
+#define sframe_func_start_addr_valid sframe_func_start_addr_valid
+
+static void arch_init_sframe_table(struct sframe_section *kernel_sfsec)
+{
+       kernel_sfsec->rodatatext_start  = (unsigned long)_srodatatext;
+       kernel_sfsec->rodatatext_end    = (unsigned long)_erodatatext;
+}
+#define arch_init_sframe_table arch_init_sframe_table
+
 #endif
 
 #endif /* _ASM_ARM64_UNWIND_SFRAME_H */
arch/arm64/kernel/vmlinux.lds.S +2 -0
@@ -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 struct sframe_section { · line 69

Quoted 7 lines
include/linux/sframe.h +4 -0
@@ -63,6 +63,10 @@ struct sframe_section {
        unsigned long           sframe_end;
        unsigned long           text_start;
        unsigned long           text_end;
+#if defined(CONFIG_SFRAME_UNWINDER) && defined(CONFIG_ARM64)
+       unsigned long           rodatatext_start;
+       unsigned long           rodatatext_end;
+#endif

It looks to me like .rodata.text only exists for vmlinux. I wonder if in sframe_func_start_addr_valid we can just use the global _srodatatext and _erodatatext after identifying if an sframe_section corresponds to vmlinux (kernel_sfsec)? That way we don't need to add these extra fields.

Quoted 67 lines
include/linux/sframe.h +0 -0
        bool                    fdes_sorted;
        unsigned long           fdes_start;
kernel/unwind/sframe.c +15 -8
@@ -20,11 +20,23 @@
 #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_SFRAME_UNWINDER
 
 static bool sframe_init __ro_after_init;
 static struct sframe_section kernel_sfsec __ro_after_init;
 
+#ifndef arch_init_sframe_table
+static void arch_init_sframe_table(struct sframe_section *kernel_sfsec) {}
+#endif
+
 #endif /* CONFIG_SFRAME_UNWINDER */
 
 struct sframe_fde_internal {
@@ -152,7 +164,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;
@@ -696,13 +708,6 @@ static int sframe_validate_section(struct sframe_section *sec)
                int ret;
 
                ret = safe_read_fde(sec, i, &fde);
-               /*
-                * Code in .rodata.text is not considered part of normal kernel
-                * text, but there is no easy way to prevent sframe data from
-                * being generated for it.
-                */
-               if (ret && sec->sec_type == SFRAME_KERNEL)
-                       continue;
                if (ret)
                        return ret;
 
@@ -1031,6 +1036,8 @@ void __init init_sframe_table(void)
        if (WARN_ON(sframe_validate_section(&kernel_sfsec)))
                return;
 
+       arch_init_sframe_table(&kernel_sfsec);
+
        sframe_init = true;
 }
 

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

#24 lore raw

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

Quoted 23 lines · Dylan Hatch
Quoted 22 lines · Jens Remus
Quoted 8 lines · Dylan Hatch
Quoted 7 lines · Jens Remus
Quoted 4 lines · Dylan Hatch

Generalize the __safe* helpers to support a non-user-access code path. Allow for kernel FDE read failures due to the presence of .rodata.text. This section contains code that can't be executed by the kernel direclty, and thus lies ouside the normal kernel-text bounds.

Quoted 7 lines
Quoted 4 lines

Nits: s/direclty/directly/ s/ouside/outside/

Could you please explain the issue? How/why does .sframe for .rodata.text pose an issue for .sframe verification?

__read_fde checks that the fde_addr it extracts is within the bounds of sec->text_start and sec->text_end. In the case of the vmlinux

Looking at the existing check in __read_fde(), do you agree that it is wrong, as sec->text_end IIUC points behind .text and thus the check should be:

Quoted 5 lines
Quoted 2 lines
        if (func_addr < sec->text_start || func_addr >= sec->text_end)
                return -EINVAL;

I agree this is correct. Is this a fix that would be folded into your previous patch series?

Yes. I would send a new version once we have clarified how to move forward in general.

Quoted 10 lines
Quoted 9 lines
Quoted 4 lines

Alternatively, we can check for FDEs located
in .rodata.text during validation, but this seems to only be present in arm64, so maybe we would need an arch-specific hook to do this? I'm open to suggestions.

Maybe that is better than ignoring __read_fde() failures? I first thought this would get nasty, but maybe it would not be too bad. Following is what I came up with (note tabs replaced by spaces due to copy&paste from terminal):

Quoted 15 lines

include/linux/sframe.h struct sframe_section { · line 69

Quoted 9 lines
include/linux/sframe.h +4 -0
@@ -63,6 +63,10 @@ struct sframe_section {
        unsigned long           sframe_end;
        unsigned long           text_start;
        unsigned long           text_end;
+#if defined(CONFIG_SFRAME_UNWINDER) && defined(CONFIG_ARM64)
+       unsigned long           rodatatext_start;
+       unsigned long           rodatatext_end;
+#endif

It looks to me like .rodata.text only exists for vmlinux. I wonder if in sframe_func_start_addr_valid we can just use the global _srodatatext and _erodatatext after identifying if an sframe_section corresponds to vmlinux (kernel_sfsec)? That way we don't need to add these extra fields.

Sure. I don't have and preferences.

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/
#25 lore raw

PATCH v3 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             | 242 +++++++++++++++++++--
 2 files changed, 228 insertions(+), 20 deletions(-)
arch/arm64/include/asm/stacktrace/common.h +6 -0
index 821a8fdd31af..96c4c0a7e6de 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_SFRAME_UNWINDER
+	unsigned long sp;
+#endif
 
+	bool unreliable;
 	struct stack_info stack;
 	struct stack_info *stacks;
 	int nr_stacks;
arch/arm64/kernel/stacktrace.c +222 -20

329 lines not on this page · raw message

Signature · 1 line
2.53.0.1213.gd9a14994de-goog
#26 lore raw

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

Hello Dylan and Weinan!

Quoted 49 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")

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/kernel/stacktrace.c +26 -0
 
+/*
+ * Unwind to the next frame according to sframe.
+ */
+static __always_inline int
+unwind_next_frame_sframe(struct kunwind_state *state)
+{
+	struct unwind_frame frame;
+	unsigned long cfa, fp, ra;
+	enum kunwind_source source = KUNWIND_SOURCE_FRAME;
+	struct pt_regs *regs = state->regs;
+
+	int err;
+
+	/* FP/SP alignment 8 bytes */
+	if (state->common.fp & 0x7 || state->common.sp & 0x7)
+		return -EINVAL;
+
+	/*
+	 * Most/all outermost functions are not visible to sframe. So, check for
+	 * a meta frame record if the sframe lookup fails.
+	 */
+	err = sframe_find_kernel(state->common.pc, &frame);
+	if (err)
+		return kunwind_next_frame_record_meta(state);
+
+	if (frame.outermost)

arch/arm64/kernel/stacktrace.c kunwind_next_frame_record(struct kunwind_state *state) · line 326

Quoted 10 lines
arch/arm64/kernel/stacktrace.c +10 -0
+		return -ENOENT;
+
+	/* Get the Canonical Frame Address (CFA) */
+	switch (frame.cfa.rule) {
+	case UNWIND_CFA_RULE_SP_OFFSET:
+		cfa = state->common.sp;
+		break;
+	case UNWIND_CFA_RULE_FP_OFFSET:
+		if (state->common.fp < state->common.sp)
+			return -EINVAL;

I wonder whether that check is valid in kernel? Looking at call_on_irq_stack() saving SP in FP and then loading SP with the IRQ SP. Is that condition always true then?

arch/arm64/kernel/stacktrace.c kunwind_next_frame_record(struct kunwind_state *state) · line 331

Quoted 5 lines
arch/arm64/kernel/stacktrace.c +5 -0
+		cfa = state->common.fp;
+		break;
+	case UNWIND_CFA_RULE_REG_OFFSET:
+	case UNWIND_CFA_RULE_REG_OFFSET_DEREF:
+		if (!regs)
		if (!regs || frame.cfa.regnum > 30)

arch/arm64/kernel/stacktrace.c

Quoted 2 lines
arch/arm64/kernel/stacktrace.c +2 -0
+			return -EINVAL;
+		cfa = regs->regs[frame.cfa.regnum];

In unwind user this is guarded by a topmost frame check, as arbitrary registers are otherwise not available. Isn't this necessary in the kernel case?

Quoted 15 lines
arch/arm64/kernel/stacktrace.c +15 -0
+		break;
+	default:
+		WARN_ON_ONCE(1);
+		return -EINVAL;
+	}
+	cfa += frame.cfa.offset;
+
+	/*
+	 * CFA typically points to a higher address than RA or FP, so don't
+	 * consume from the stack when we read it.
+	 */
+	if (frame.cfa.rule & UNWIND_RULE_DEREF &&
+	    !get_word(&state->common, &cfa))
+		return -EINVAL;
+

arch/arm64/kernel/stacktrace.c kunwind_next_frame_record(struct kunwind_state *state) · line 358

Quoted 10 lines
arch/arm64/kernel/stacktrace.c +10 -0
+	/* CFA alignment 8 bytes */
+	if (cfa & 0x7)
+		return -EINVAL;
+
+	/* Get the Return Address (RA) */
+	switch (frame.ra.rule) {
+	case UNWIND_RULE_RETAIN:
+		if (!regs)
+			return -EINVAL;
+		ra = regs->regs[30];

Likewise: Topmost frame check not required to access arbitrary registers (including RA/LR)? Furthermore, provided don't have a thinko, LR may only be in LR in the topmost frame. In any other frame it must have been saved. Otherwise there would be an endless return loop.

arch/arm64/kernel/stacktrace.c kunwind_next_frame_record(struct kunwind_state *state) · line 367

Quoted 9 lines
arch/arm64/kernel/stacktrace.c +9 -0
+		source = KUNWIND_SOURCE_REGS_LR;
+		break;
+	/* UNWIND_USER_RULE_CFA_OFFSET not implemented on purpose */
+	case UNWIND_RULE_CFA_OFFSET_DEREF:
+		ra = cfa + frame.ra.offset;
+		break;
+	case UNWIND_RULE_REG_OFFSET:
+	case UNWIND_RULE_REG_OFFSET_DEREF:
+		if (!regs)
		if (!regs || frame.cfa.regnum > 30)

arch/arm64/kernel/stacktrace.c

Quoted 2 lines
arch/arm64/kernel/stacktrace.c +2 -0
+			return -EINVAL;
+		ra = regs->regs[frame.cfa.regnum];

Likewise: Topmost frame check not required to access arbitrary registers?

Quoted 9 lines
arch/arm64/kernel/stacktrace.c +9 -0
+		ra += frame.ra.offset;
+		break;
+	default:
+		WARN_ON_ONCE(1);
+		return -EINVAL;
+	}
+
+	/* Get the Frame Pointer (FP) */
+	switch (frame.fp.rule) {

arch/arm64/kernel/stacktrace.c kunwind_next_frame_record(struct kunwind_state *state) · line 388

Quoted 10 lines
arch/arm64/kernel/stacktrace.c +10 -0
+	case UNWIND_RULE_RETAIN:
+		fp = state->common.fp;
+		break;
+	/* UNWIND_USER_RULE_CFA_OFFSET not implemented on purpose */
+	case UNWIND_RULE_CFA_OFFSET_DEREF:
+		fp = cfa + frame.fp.offset;
+		break;
+	case UNWIND_RULE_REG_OFFSET:
+	case UNWIND_RULE_REG_OFFSET_DEREF:
+		if (!regs)
		if (!regs || frame.cfa.regnum > 30)

arch/arm64/kernel/stacktrace.c

Quoted 2 lines
arch/arm64/kernel/stacktrace.c +2 -0
+			return -EINVAL;
+		fp = regs->regs[frame.fp.regnum];

Likewise: Topmost frame check not required to access arbitrary registers?

Quoted 17 lines
arch/arm64/kernel/stacktrace.c +17 -0
+		fp += frame.fp.offset;
+		break;
+	default:
+		WARN_ON_ONCE(1);
+		return -EINVAL;
+	}
+
+	/*
+	 * Consume RA and FP from the stack. The frame record puts FP at a lower
+	 * address than RA, so we always read FP first.
+	 */
+	if (frame.fp.rule & UNWIND_RULE_DEREF &&
+	    !get_word(&state->common, &fp))
+		return -EINVAL;
+
+	if (frame.ra.rule & UNWIND_RULE_DEREF &&
+	    get_consume_word(&state->common, &ra))

arch/arm64/kernel/stacktrace.c kunwind_next_frame_record(struct kunwind_state *state) · line 417

Quoted 10 lines
arch/arm64/kernel/stacktrace.c +10 -0
+		return -EINVAL;
+
+	state->common.pc = ra;
+	state->common.sp = cfa;
+	state->common.fp = fp;
+
+	state->source = source;
+
+	return 0;
+}

Thanks and 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/
#27 lore raw

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

Quoted 8 lines · Jens Remus

arch/arm64/kernel/stacktrace.c kunwind_next_frame_record(struct kunwind_state *state) · line 326

Quoted 4 lines
arch/arm64/kernel/stacktrace.c +3 -0
+     case UNWIND_CFA_RULE_FP_OFFSET:
+             if (state->common.fp < state->common.sp)
+                     return -EINVAL;

I wonder whether that check is valid in kernel? Looking at call_on_irq_stack() saving SP in FP and then loading SP with the IRQ SP. Is that condition always true then?

Good catch. I will double-check this.

Quoted 7 lines

arch/arm64/kernel/stacktrace.c kunwind_next_frame_record(struct kunwind_state *state) · line 331

Quoted 6 lines
arch/arm64/kernel/stacktrace.c +5 -0
+             cfa = state->common.fp;
+             break;
+     case UNWIND_CFA_RULE_REG_OFFSET:
+     case UNWIND_CFA_RULE_REG_OFFSET_DEREF:
+             if (!regs)
Quoted 8 lines
                if (!regs || frame.cfa.regnum > 30)

Review

Quoted 2 lines
(quoted diff) +2 -0
+                     return -EINVAL;
+             cfa = regs->regs[frame.cfa.regnum];

In unwind user this is guarded by a topmost frame check, as arbitrary registers are otherwise not available. Isn't this necessary in the kernel case?

It is necessary, though as you point out the way I wrote the check is not as obvious as it probably should be.

The saved state->regs is set when the current frame is recovered from the saved PC of a struct pt_regs, and then immediately set back to NULL after the next frame has been recovered. In other words, the state->regs is only ever set when it is relevant to the current frame, which occurs when state->source == KUNWIND_SOURCE_REGS_PC. This only happens when the topmost frame is recovered from a pt_regs, or when a pt_regs is recovered from the stack due to an interrupt.

I can make this more readable by adding an explicit check for KUNWIND_SOURCE_REGS_PC in addition to state->regs != NULL.

Quoted 110 lines
Quoted 16 lines
arch/arm64/kernel/stacktrace.c +15 -0
+             break;
+     default:
+             WARN_ON_ONCE(1);
+             return -EINVAL;
+     }
+     cfa += frame.cfa.offset;
+
+     /*
+      * CFA typically points to a higher address than RA or FP, so don't
+      * consume from the stack when we read it.
+      */
+     if (frame.cfa.rule & UNWIND_RULE_DEREF &&
+         !get_word(&state->common, &cfa))
+             return -EINVAL;
+

arch/arm64/kernel/stacktrace.c kunwind_next_frame_record(struct kunwind_state *state) · line 358

Quoted 10 lines
arch/arm64/kernel/stacktrace.c +10 -0
+     /* CFA alignment 8 bytes */
+     if (cfa & 0x7)
+             return -EINVAL;
+
+     /* Get the Return Address (RA) */
+     switch (frame.ra.rule) {
+     case UNWIND_RULE_RETAIN:
+             if (!regs)
+                     return -EINVAL;
+             ra = regs->regs[30];

Likewise: Topmost frame check not required to access arbitrary registers (including RA/LR)? Furthermore, provided don't have a thinko, LR may only be in LR in the topmost frame. In any other frame it must have been saved. Otherwise there would be an endless return loop.

arch/arm64/kernel/stacktrace.c kunwind_next_frame_record(struct kunwind_state *state) · line 367

Quoted 9 lines
arch/arm64/kernel/stacktrace.c +9 -0
+             source = KUNWIND_SOURCE_REGS_LR;
+             break;
+     /* UNWIND_USER_RULE_CFA_OFFSET not implemented on purpose */
+     case UNWIND_RULE_CFA_OFFSET_DEREF:
+             ra = cfa + frame.ra.offset;
+             break;
+     case UNWIND_RULE_REG_OFFSET:
+     case UNWIND_RULE_REG_OFFSET_DEREF:
+             if (!regs)
                if (!regs || frame.cfa.regnum > 30)

Review

Quoted 2 lines
(quoted diff) +2 -0
+                     return -EINVAL;
+             ra = regs->regs[frame.cfa.regnum];

Likewise: Topmost frame check not required to access arbitrary registers?

Quoted 9 lines
arch/arm64/kernel/stacktrace.c +9 -0
+             ra += frame.ra.offset;
+             break;
+     default:
+             WARN_ON_ONCE(1);
+             return -EINVAL;
+     }
+
+     /* Get the Frame Pointer (FP) */
+     switch (frame.fp.rule) {

arch/arm64/kernel/stacktrace.c kunwind_next_frame_record(struct kunwind_state *state) · line 388

Quoted 10 lines
arch/arm64/kernel/stacktrace.c +10 -0
+     case UNWIND_RULE_RETAIN:
+             fp = state->common.fp;
+             break;
+     /* UNWIND_USER_RULE_CFA_OFFSET not implemented on purpose */
+     case UNWIND_RULE_CFA_OFFSET_DEREF:
+             fp = cfa + frame.fp.offset;
+             break;
+     case UNWIND_RULE_REG_OFFSET:
+     case UNWIND_RULE_REG_OFFSET_DEREF:
+             if (!regs)
                if (!regs || frame.cfa.regnum > 30)

Review

Quoted 2 lines
(quoted diff) +2 -0
+                     return -EINVAL;
+             fp = regs->regs[frame.fp.regnum];

Likewise: Topmost frame check not required to access arbitrary registers?

Quoted 17 lines
arch/arm64/kernel/stacktrace.c +17 -0
+             fp += frame.fp.offset;
+             break;
+     default:
+             WARN_ON_ONCE(1);
+             return -EINVAL;
+     }
+
+     /*
+      * Consume RA and FP from the stack. The frame record puts FP at a lower
+      * address than RA, so we always read FP first.
+      */
+     if (frame.fp.rule & UNWIND_RULE_DEREF &&
+         !get_word(&state->common, &fp))
+             return -EINVAL;
+
+     if (frame.ra.rule & UNWIND_RULE_DEREF &&
+         get_consume_word(&state->common, &ra))

arch/arm64/kernel/stacktrace.c kunwind_next_frame_record(struct kunwind_state *state) · line 417

Quoted 10 lines
arch/arm64/kernel/stacktrace.c +10 -0
+             return -EINVAL;
+
+     state->common.pc = ra;
+     state->common.sp = cfa;
+     state->common.fp = fp;
+
+     state->source = source;
+
+     return 0;
+}

Thanks and 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

#28 lore raw

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

Quoted 26 lines · Dylan Hatch
Quoted 4 lines · Jens Remus

arch/arm64/kernel/stacktrace.c kunwind_next_frame_record(struct kunwind_state *state) · line 331

Quoted 3 lines
arch/arm64/kernel/stacktrace.c +3 -0
+     case UNWIND_CFA_RULE_REG_OFFSET:
+     case UNWIND_CFA_RULE_REG_OFFSET_DEREF:
+             if (!regs)
Quoted 8 lines
                if (!regs || frame.cfa.regnum > 30)

Review

Quoted 2 lines
(quoted diff) +2 -0
+                     return -EINVAL;
+             cfa = regs->regs[frame.cfa.regnum];

In unwind user this is guarded by a topmost frame check, as arbitrary registers are otherwise not available. Isn't this necessary in the kernel case?

It is necessary, though as you point out the way I wrote the check is not as obvious as it probably should be.

The saved state->regs is set when the current frame is recovered from the saved PC of a struct pt_regs, and then immediately set back to NULL after the next frame has been recovered. In other words, the state->regs is only ever set when it is relevant to the current frame, which occurs when state->source == KUNWIND_SOURCE_REGS_PC. This only happens when the topmost frame is recovered from a pt_regs, or when a pt_regs is recovered from the stack due to an interrupt.

Quoted 2 lines

I can make this more readable by adding an explicit check for KUNWIND_SOURCE_REGS_PC in addition to state->regs != NULL.

Thanks for the explanation! Maybe just add an explanation to the commit message and a short comment above the (!regs) test?

/* regs only available in topmost frame */

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/
#29 lore raw
In reply to Dylan Hatch
Quoted 8 lines · Dylan Hatch

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.

Nit: Trailing dot in commit subjects seems unusual. Remove?

Quoted 2 lines

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

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/