Skip to contentSkip to conversation

tracing/arm: Fix the stack tracer when LR is saved after local storage

PATCH In review Steven Rostedt 5 participants 14 messages Aug 7, 2019 – Aug 13, 2019

Subsystems (3)
lore
Share & tools
lore

Steven Rostedt

author maintainer
#0 lore raw

v2 cover tracing/arm: Fix the stack tracer when LR is saved after local storage

As arm64 saves the link register after a function's local variables are stored, it causes the max stack tracer to be off by one in its output of which function has the bloated stack frame.

The first patch fixes this by creating a ARCH_RET_ADDR_BEFORE_LOCAL_VARS define that an achitecture (arm64) may set in asm/ftrace.h, and this will cause the stack tracer to make the shift.

As it has been proven that the stack tracer isn't the most trivial algorithm to understand by staring at the code, the second patch adds comments to the code to explain the algorithm with and without the ARCH_RET_ADDR_BEFORE_LOCAL_VARS.

Hmm, should this be sent to stable (and for inclusion now?)

-- Steve

Changes since v1:

- Fixed wrong value in stack_trace_index[] array in comment

- Added a comment about gcc currently saves the LR after local variables, but there's no guarantee that it will be like that in the future. (Notified of this by Mark Rutland)

Steven Rostedt (VMware) (2):
tracing/arm64: Have max stack tracer handle the case of return address after data
tracing: Document the stack trace algorithm in the comments

----

 arch/arm64/include/asm/ftrace.h |  13 +++++
 kernel/trace/trace_stack.c      | 112 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 125 insertions(+)
author maintainer
#1 lore raw

PATCH v2 1/2 tracing/arm64: Have max stack tracer handle the case of return address after data

From: "Steven Rostedt (VMware)" <[email protected]>

Most archs (well at least x86) store the function call return address on the stack before storing the local variables for the function. The max stack tracer depends on this in its algorithm to display the stack size of each function it finds in the back trace.

Some archs (arm64), may store the return address (from its link register) just before calling a nested function. There's no reason to save the link register on leaf functions, as it wont be updated. This breaks the algorithm of the max stack tracer.

Add a new define ARCH_RET_ADDR_AFTER_LOCAL_VARS that an architecture may set if it stores the return address (link register) after it stores the function's local variables, and have the stack trace shift the values of the mapped stack size to the appropriate functions.

---
 arch/arm64/include/asm/ftrace.h | 13 +++++++++++++
 kernel/trace/trace_stack.c      | 14 ++++++++++++++
 2 files changed, 27 insertions(+)
arch/arm64/include/asm/ftrace.h +13 -0
index 5ab5200b2bdc..961e98618db4 100644
@@ -14,6 +14,19 @@
 #define MCOUNT_ADDR		((unsigned long)_mcount)
 #define MCOUNT_INSN_SIZE	AARCH64_INSN_SIZE
 
+/*
+ * Currently, gcc tends to save the link register after the local variables
+ * on the stack. This causes the max stack tracer to report the function
+ * frame sizes for the wrong functions. By defining
+ * ARCH_RET_ADDR_AFTER_LOCAL_VARS, it will tell the stack tracer to expect
+ * to find the return address on the stack after the local variables have
+ * been set up.
+ *
+ * Note, this may change in the future, and we will need to deal with that
+ * if it were to happen.
+ */
+#define ARCH_RET_ADDR_AFTER_LOCAL_VARS 1
+
 #ifndef __ASSEMBLY__
 #include <linux/compat.h>
 
kernel/trace/trace_stack.c +14 -0
index 5d16f73898db..40e4a88eea8f 100644
@@ -158,6 +158,20 @@ static void check_stack(unsigned long ip, unsigned long *stack)
 			i++;
 	}
 
+#ifdef ARCH_RET_ADDR_AFTER_LOCAL_VARS
+	/*
+	 * Some archs will store the link register before calling
+	 * nested functions. This means the saved return address
+	 * comes after the local storage, and we need to shift
+	 * for that.
+	 */
+	if (x > 1) {
+		memmove(&stack_trace_index[0], &stack_trace_index[1],
+			sizeof(stack_trace_index[0]) * (x - 1));
+		x--;
+	}
+#endif
+
 	stack_trace_nr_entries = x;
 
 	if (task_stack_end_corrupted(current)) {
Signature · 1 line
2.20.1
author maintainer
#2 lore raw

Re: [PATCH 1/2 v2] tracing/arm64: Have max stack tracer handle the case of return address after data

[ I should have added Mark as Cc ]

Dear ARM64 folks,

Are you OK with this patch set?

If so, please ACK.

Should it be marked for stable?

Hmm, I'm starting to think not.

-- Steve

Quoted 75 lines · On Wed, 07 Aug 2019 13:28:27 -0400 Steven Rostedt <[email protected]> wrote:

From: "Steven Rostedt (VMware)" <[email protected]>

Most archs (well at least x86) store the function call return address on the stack before storing the local variables for the function. The max stack tracer depends on this in its algorithm to display the stack size of each function it finds in the back trace.

Some archs (arm64), may store the return address (from its link register) just before calling a nested function. There's no reason to save the link register on leaf functions, as it wont be updated. This breaks the algorithm of the max stack tracer.

Add a new define ARCH_RET_ADDR_AFTER_LOCAL_VARS that an architecture may set if it stores the return address (link register) after it stores the function's local variables, and have the stack trace shift the values of the mapped stack size to the appropriate functions.

---
 arch/arm64/include/asm/ftrace.h | 13 +++++++++++++
 kernel/trace/trace_stack.c      | 14 ++++++++++++++
 2 files changed, 27 insertions(+)
arch/arm64/include/asm/ftrace.h +13 -0
index 5ab5200b2bdc..961e98618db4 100644
@@ -14,6 +14,19 @@
 #define MCOUNT_ADDR		((unsigned long)_mcount)
 #define MCOUNT_INSN_SIZE	AARCH64_INSN_SIZE
 
+/*
+ * Currently, gcc tends to save the link register after the local variables
+ * on the stack. This causes the max stack tracer to report the function
+ * frame sizes for the wrong functions. By defining
+ * ARCH_RET_ADDR_AFTER_LOCAL_VARS, it will tell the stack tracer to expect
+ * to find the return address on the stack after the local variables have
+ * been set up.
+ *
+ * Note, this may change in the future, and we will need to deal with that
+ * if it were to happen.
+ */
+#define ARCH_RET_ADDR_AFTER_LOCAL_VARS 1
+
 #ifndef __ASSEMBLY__
 #include <linux/compat.h>
 
kernel/trace/trace_stack.c +14 -0
index 5d16f73898db..40e4a88eea8f 100644
@@ -158,6 +158,20 @@ static void check_stack(unsigned long ip, unsigned long *stack)
 			i++;
 	}
 
+#ifdef ARCH_RET_ADDR_AFTER_LOCAL_VARS
+	/*
+	 * Some archs will store the link register before calling
+	 * nested functions. This means the saved return address
+	 * comes after the local storage, and we need to shift
+	 * for that.
+	 */
+	if (x > 1) {
+		memmove(&stack_trace_index[0], &stack_trace_index[1],
+			sizeof(stack_trace_index[0]) * (x - 1));
+		x--;
+	}
+#endif
+
 	stack_trace_nr_entries = x;
 
 	if (task_stack_end_corrupted(current)) {
maintainer
#3 lore raw

Re: [PATCH 1/2 v2] tracing/arm64: Have max stack tracer handle the case of return address after data

In reply to Steven Rostedt

Hi Steve,

Quoted 17 lines · Steven Rostedt

From: "Steven Rostedt (VMware)" <[email protected]>

Most archs (well at least x86) store the function call return address on the stack before storing the local variables for the function. The max stack tracer depends on this in its algorithm to display the stack size of each function it finds in the back trace.

Some archs (arm64), may store the return address (from its link register) just before calling a nested function. There's no reason to save the link register on leaf functions, as it wont be updated. This breaks the algorithm of the max stack tracer.

Add a new define ARCH_RET_ADDR_AFTER_LOCAL_VARS that an architecture may set if it stores the return address (link register) after it stores the function's local variables, and have the stack trace shift the values of the mapped stack size to the appropriate functions.

Quoted 8 lines
---
 arch/arm64/include/asm/ftrace.h | 13 +++++++++++++
 kernel/trace/trace_stack.c      | 14 ++++++++++++++
 2 files changed, 27 insertions(+)

I agree with your later comment that this should NOT go to stable.

Quoted 10 lines
arch/arm64/include/asm/ftrace.h +2 -0
index 5ab5200b2bdc..961e98618db4 100644
@@ -14,6 +14,19 @@
 #define MCOUNT_ADDR		((unsigned long)_mcount)
 #define MCOUNT_INSN_SIZE	AARCH64_INSN_SIZE
 
+/*
+ * Currently, gcc tends to save the link register after the local variables

arch/arm64/include/asm/ftrace.h line 28

Quoted 10 lines
arch/arm64/include/asm/ftrace.h +10 -0
+ * on the stack. This causes the max stack tracer to report the function
+ * frame sizes for the wrong functions. By defining
+ * ARCH_RET_ADDR_AFTER_LOCAL_VARS, it will tell the stack tracer to expect
+ * to find the return address on the stack after the local variables have
+ * been set up.
+ *
+ * Note, this may change in the future, and we will need to deal with that
+ * if it were to happen.
+ */
+#define ARCH_RET_ADDR_AFTER_LOCAL_VARS 1

I know it's long already, but prefixing this with FTRACE_ would be good so that other code doesn't use it for anything. It's not the end of the world if the ftrace stack usage statistics are wonky, but if people tried to use this for crazy things like livepatching then we'd be in trouble.

Maybe FTRACE_ARCH_FRAME_AFTER_LOCALS, which is the same length as what you currently have?

Will

author maintainer
#4 lore raw

Re: [PATCH 1/2 v2] tracing/arm64: Have max stack tracer handle the case of return address after data

Quoted 5 lines · On Thu, 8 Aug 2019 17:28:26 +0100 Will Deacon <[email protected]> wrote:
Quoted 4 lines

+ * Note, this may change in the future, and we will need to deal with that
+ * if it were to happen.
+ */
+#define ARCH_RET_ADDR_AFTER_LOCAL_VARS 1

Quoted 7 lines

I know it's long already, but prefixing this with FTRACE_ would be good so that other code doesn't use it for anything. It's not the end of the world if the ftrace stack usage statistics are wonky, but if people tried to use this for crazy things like livepatching then we'd be in trouble.

Maybe FTRACE_ARCH_FRAME_AFTER_LOCALS, which is the same length as what you currently have?

Note, it would still need to be prefixed with "ARCH_" as that's the way of showing arch specific defines.

We could make it more descriptive of what it will do and not the reason for why it is done...

ARCH_FTRACE_SHIFT_STACK_TRACER

?

-- Steve

maintainer Acked-by
#5 lore raw

Re: [PATCH 1/2 v2] tracing/arm64: Have max stack tracer handle the case of return address after data

Quoted 16 lines · Steven Rostedt
Quoted 5 lines · On Thu, 8 Aug 2019 17:28:26 +0100 Will Deacon <[email protected]> wrote:
Quoted 4 lines

+ * Note, this may change in the future, and we will need to deal with that
+ * if it were to happen.
+ */
+#define ARCH_RET_ADDR_AFTER_LOCAL_VARS 1

Quoted 7 lines

I know it's long already, but prefixing this with FTRACE_ would be good so that other code doesn't use it for anything. It's not the end of the world if the ftrace stack usage statistics are wonky, but if people tried to use this for crazy things like livepatching then we'd be in trouble.

Maybe FTRACE_ARCH_FRAME_AFTER_LOCALS, which is the same length as what you currently have?

Quoted 8 lines

Note, it would still need to be prefixed with "ARCH_" as that's the way of showing arch specific defines.

We could make it more descriptive of what it will do and not the reason for why it is done...

ARCH_FTRACE_SHIFT_STACK_TRACER

Thanks, Steve.

Will

author maintainer Acked-by Will Deacon
#6 lore raw

Re: [PATCH 1/2 v2] tracing/arm64: Have max stack tracer handle the case of return address after data

Quoted 7 lines · On Thu, 8 Aug 2019 18:11:53 +0100 Will Deacon <[email protected]> wrote:
Quoted 5 lines

We could make it more descriptive of what it will do and not the reason for why it is done...

ARCH_FTRACE_SHIFT_STACK_TRACER

Thanks Will!

Here's the official patch.

From: "Steven Rostedt (VMware)" <[email protected]>

Most archs (well at least x86) store the function call return address on the stack before storing the local variables for the function. The max stack tracer depends on this in its algorithm to display the stack size of each function it finds in the back trace.

Some archs (arm64), may store the return address (from its link register) just before calling a nested function. There's no reason to save the link register on leaf functions, as it wont be updated. This breaks the algorithm of the max stack tracer.

Add a new define ARCH_RET_ADDR_AFTER_LOCAL_VARS that an architecture may set if it stores the return address (link register) after it stores the function's local variables, and have the stack trace shift the values of the mapped stack size to the appropriate functions.

---
 arch/arm64/include/asm/ftrace.h | 13 +++++++++++++
 kernel/trace/trace_stack.c      | 14 ++++++++++++++
 2 files changed, 27 insertions(+)
arch/arm64/include/asm/ftrace.h +13 -0
index 5ab5200b2bdc..d48667b04c41 100644
@@ -14,6 +14,19 @@
 #define MCOUNT_ADDR		((unsigned long)_mcount)
 #define MCOUNT_INSN_SIZE	AARCH64_INSN_SIZE
 
+/*
+ * Currently, gcc tends to save the link register after the local variables
+ * on the stack. This causes the max stack tracer to report the function
+ * frame sizes for the wrong functions. By defining
+ * ARCH_FTRACE_SHIFT_STACK_TRACER, it will tell the stack tracer to expect
+ * to find the return address on the stack after the local variables have
+ * been set up.
+ *
+ * Note, this may change in the future, and we will need to deal with that
+ * if it were to happen.
+ */
+#define ARCH_FTRACE_SHIFT_STACK_TRACER 1
+
 #ifndef __ASSEMBLY__
 #include <linux/compat.h>
 
kernel/trace/trace_stack.c +14 -0
index 5d16f73898db..642a850af81a 100644
@@ -158,6 +158,20 @@ static void check_stack(unsigned long ip, unsigned long *stack)
 			i++;
 	}
 
+#ifdef ARCH_FTRACE_SHIFT_STACK_TRACER
+	/*
+	 * Some archs will store the link register before calling
+	 * nested functions. This means the saved return address
+	 * comes after the local storage, and we need to shift
+	 * for that.
+	 */
+	if (x > 1) {
+		memmove(&stack_trace_index[0], &stack_trace_index[1],
+			sizeof(stack_trace_index[0]) * (x - 1));
+		x--;
+	}
+#endif
+
 	stack_trace_nr_entries = x;
 
 	if (task_stack_end_corrupted(current)) {
Signature · 1 line
2.20.1
#7 lore raw

Re: [PATCH 1/2 v2] tracing/arm64: Have max stack tracer handle the case of return address after data

Quoted 20 lines · Steven Rostedt
Quoted 6 lines · On Thu, 8 Aug 2019 18:11:53 +0100 Will Deacon <[email protected]> wrote:
Quoted 5 lines

We could make it more descriptive of what it will do and not the reason for why it is done...

ARCH_FTRACE_SHIFT_STACK_TRACER

Thanks Will!

Here's the official patch.

From: "Steven Rostedt (VMware)" <[email protected]>

Most archs (well at least x86) store the function call return address on the stack before storing the local variables for the function. The max stack tracer depends on this in its algorithm to display the stack size of each function it finds in the back trace.

Quoted 6 lines

Some archs (arm64), may store the return address (from its link register) just before calling a nested function. There's no reason to save the link register on leaf functions, as it wont be updated. This breaks the algorithm of the max stack tracer.

Add a new define ARCH_RET_ADDR_AFTER_LOCAL_VARS that an architecture may set

ARCH_FTRACE_SHIFT_STACK_TRACER is used in the code.

Jiping

Quoted 63 lines

if it stores the return address (link register) after it stores the function's local variables, and have the stack trace shift the values of the mapped stack size to the appropriate functions.

---
  arch/arm64/include/asm/ftrace.h | 13 +++++++++++++
  kernel/trace/trace_stack.c      | 14 ++++++++++++++
  2 files changed, 27 insertions(+)
arch/arm64/include/asm/ftrace.h +13 -0
index 5ab5200b2bdc..d48667b04c41 100644
@@ -14,6 +14,19 @@
  #define MCOUNT_ADDR		((unsigned long)_mcount)
  #define MCOUNT_INSN_SIZE	AARCH64_INSN_SIZE
 
+/*
+ * Currently, gcc tends to save the link register after the local variables
+ * on the stack. This causes the max stack tracer to report the function
+ * frame sizes for the wrong functions. By defining
+ * ARCH_FTRACE_SHIFT_STACK_TRACER, it will tell the stack tracer to expect
+ * to find the return address on the stack after the local variables have
+ * been set up.
+ *
+ * Note, this may change in the future, and we will need to deal with that
+ * if it were to happen.
+ */
+#define ARCH_FTRACE_SHIFT_STACK_TRACER 1
+
  #ifndef __ASSEMBLY__
  #include <linux/compat.h>
 
kernel/trace/trace_stack.c +14 -0
index 5d16f73898db..642a850af81a 100644
@@ -158,6 +158,20 @@ static void check_stack(unsigned long ip, unsigned long *stack)
  			i++;
  	}
 
+#ifdef ARCH_FTRACE_SHIFT_STACK_TRACER
+	/*
+	 * Some archs will store the link register before calling
+	 * nested functions. This means the saved return address
+	 * comes after the local storage, and we need to shift
+	 * for that.
+	 */
+	if (x > 1) {
+		memmove(&stack_trace_index[0], &stack_trace_index[1],
+			sizeof(stack_trace_index[0]) * (x - 1));
+		x--;
+	}
+#endif
+
  	stack_trace_nr_entries = x;
 
  	if (task_stack_end_corrupted(current)) {
author maintainer
#8 lore raw

Re: [PATCH 1/2 v2] tracing/arm64: Have max stack tracer handle the case of return address after data

Quoted 21 lines · On Fri, 9 Aug 2019 10:17:19 +0800 Jiping Ma <[email protected]> wrote:
Quoted 20 lines · Steven Rostedt
Quoted 6 lines · On Thu, 8 Aug 2019 18:11:53 +0100 Will Deacon <[email protected]> wrote:
Quoted 5 lines

We could make it more descriptive of what it will do and not the reason for why it is done...

ARCH_FTRACE_SHIFT_STACK_TRACER

Thanks Will!

Here's the official patch.

From: "Steven Rostedt (VMware)" <[email protected]>

Most archs (well at least x86) store the function call return address on the stack before storing the local variables for the function. The max stack tracer depends on this in its algorithm to display the stack size of each function it finds in the back trace.

Quoted 8 lines
Quoted 6 lines

Some archs (arm64), may store the return address (from its link register) just before calling a nested function. There's no reason to save the link register on leaf functions, as it wont be updated. This breaks the algorithm of the max stack tracer.

Add a new define ARCH_RET_ADDR_AFTER_LOCAL_VARS that an architecture may set

ARCH_FTRACE_SHIFT_STACK_TRACER is used in the code.

Ah, I did a s/x/y/ to the diff of the patch, but not the change log. Thanks for pointing that out. I also need to update the comment in 2/2.

-- Steve

Quoted 4 lines

Jiping

maintainer
#9 lore raw

Re: [PATCH 1/2 v2] tracing/arm64: Have max stack tracer handle the case of return address after data

Hi Steve,

Quoted 28 lines · Steven Rostedt
Quoted 26 lines · On Fri, 9 Aug 2019 10:17:19 +0800 Jiping Ma <[email protected]> wrote:
Quoted 25 lines · Steven Rostedt
Quoted 6 lines · On Thu, 8 Aug 2019 18:11:53 +0100 Will Deacon <[email protected]> wrote:
Quoted 5 lines

We could make it more descriptive of what it will do and not the reason for why it is done...

ARCH_FTRACE_SHIFT_STACK_TRACER

Thanks Will!

Here's the official patch.

From: "Steven Rostedt (VMware)" <[email protected]>

Most archs (well at least x86) store the function call return address on the stack before storing the local variables for the function. The max stack tracer depends on this in its algorithm to display the stack size of each function it finds in the back trace.

Some archs (arm64), may store the return address (from its link register) just before calling a nested function. There's no reason to save the link register on leaf functions, as it wont be updated. This breaks the algorithm of the max stack tracer.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Add a new define ARCH_RET_ADDR_AFTER_LOCAL_VARS that an architecture may set

ARCH_FTRACE_SHIFT_STACK_TRACER is used in the code.

Ah, I did a s/x/y/ to the diff of the patch, but not the change log. Thanks for pointing that out. I also need to update the comment in 2/2.

Are you going to post another version of this or have you queued it already? Just want to make sure it doesn't slip through the cracks.

Cheers,

Will

author maintainer
#10 lore raw

Re: [PATCH 1/2 v2] tracing/arm64: Have max stack tracer handle the case of return address after data

Quoted 33 lines · On Tue, 13 Aug 2019 18:31:14 +0100 Will Deacon <[email protected]> wrote:

Hi Steve,

Quoted 30 lines · Steven Rostedt
Quoted 28 lines · On Fri, 9 Aug 2019 10:17:19 +0800 Jiping Ma <[email protected]> wrote:
Quoted 26 lines · Steven Rostedt
Quoted 6 lines · On Thu, 8 Aug 2019 18:11:53 +0100 Will Deacon <[email protected]> wrote:
Quoted 5 lines

We could make it more descriptive of what it will do and not the reason for why it is done...

ARCH_FTRACE_SHIFT_STACK_TRACER

Thanks Will!

Here's the official patch.

From: "Steven Rostedt (VMware)" <[email protected]>

Most archs (well at least x86) store the function call return address on the stack before storing the local variables for the function. The max stack tracer depends on this in its algorithm to display the stack size of each function it finds in the back trace.

Some archs (arm64), may store the return address (from its link register) just before calling a nested function. There's no reason to save the link register on leaf functions, as it wont be updated. This breaks the algorithm of the max stack tracer.

Add a new define ARCH_RET_ADDR_AFTER_LOCAL_VARS that an architecture may set

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

ARCH_FTRACE_SHIFT_STACK_TRACER is used in the code.

Ah, I did a s/x/y/ to the diff of the patch, but not the change log. Thanks for pointing that out. I also need to update the comment in 2/2.

Are you going to post another version of this or have you queued it already? Just want to make sure it doesn't slip through the cracks.

Ah, it's in my queue. I should post a new version :-/

Thanks for the reminder.

-- Steve

reviewer Acked-by on v2 1/2
#11 lore raw

Re: [PATCH 1/2 v2] tracing/arm64: Have max stack tracer handle the case of return address after data

In reply to Steven Rostedt
Quoted 36 lines · Steven Rostedt

From: "Steven Rostedt (VMware)" <[email protected]>

Most archs (well at least x86) store the function call return address on the stack before storing the local variables for the function. The max stack tracer depends on this in its algorithm to display the stack size of each function it finds in the back trace.

Some archs (arm64), may store the return address (from its link register) just before calling a nested function. There's no reason to save the link register on leaf functions, as it wont be updated. This breaks the algorithm of the max stack tracer.

Add a new define ARCH_RET_ADDR_AFTER_LOCAL_VARS that an architecture may set if it stores the return address (link register) after it stores the function's local variables, and have the stack trace shift the values of the mapped stack size to the appropriate functions.

---
 arch/arm64/include/asm/ftrace.h | 13 +++++++++++++
 kernel/trace/trace_stack.c      | 14 ++++++++++++++
 2 files changed, 27 insertions(+)
arch/arm64/include/asm/ftrace.h +2 -0
index 5ab5200b2bdc..961e98618db4 100644
@@ -14,6 +14,19 @@
 #define MCOUNT_ADDR		((unsigned long)_mcount)
 #define MCOUNT_INSN_SIZE	AARCH64_INSN_SIZE
 
+/*
+ * Currently, gcc tends to save the link register after the local variables

arch/arm64/include/asm/ftrace.h line 28

Quoted 10 lines
arch/arm64/include/asm/ftrace.h +10 -0
+ * on the stack. This causes the max stack tracer to report the function
+ * frame sizes for the wrong functions. By defining
+ * ARCH_RET_ADDR_AFTER_LOCAL_VARS, it will tell the stack tracer to expect
+ * to find the return address on the stack after the local variables have
+ * been set up.
+ *
+ * Note, this may change in the future, and we will need to deal with that
+ * if it were to happen.
+ */
+#define ARCH_RET_ADDR_AFTER_LOCAL_VARS 1

FWIW (with whatever this got renamed to):

Thanks,
Mark.

author maintainer
#12 lore raw

PATCH v2 2/2 tracing: Document the stack trace algorithm in the comments

In reply to Steven Rostedt

From: "Steven Rostedt (VMware)" <[email protected]>

As the max stack tracer algorithm is not that easy to understand from the code, add comments that explain the algorithm and mentions how ARCH_RET_ADDR_AFTER_LOCAL_VARS affects it.

---
 kernel/trace/trace_stack.c | 98 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 98 insertions(+)
kernel/trace/trace_stack.c +98 -0
index 40e4a88eea8f..f94a2fc567de 100644
@@ -53,6 +53,104 @@ static void print_max_stack(void)
 	}
 }
 
+/*
+ * The stack tracer looks for a maximum stack at each call from a function. It
+ * registers a callback from ftrace, and in that callback it examines the stack
+ * size. It determines the stack size from the variable passed in, which is the
+ * address of a local variable in the stack_trace_call() callback function.
+ * The stack size is calculated by the address of the local variable to the top
+ * of the current stack. If that size is smaller than the currently saved max
+ * stack size, nothing more is done.
+ *
+ * If the size of the stack is greater than the maximum recorded size, then the
+ * following algorithm takes place.
+ *
+ * For architectures (like x86) that store the function's return address before
+ * saving the function's local variables, the stack will look something like
+ * this:
+ *
+ *   [ top of stack ]
+ *    0: sys call entry frame
+ *   10: return addr to entry code
+ *   11: start of sys_foo frame
+ *   20: return addr to sys_foo
+ *   21: start of kernel_func_bar frame
+ *   30: return addr to kernel_func_bar
+ *   31: [ do trace stack here ]
+ *
+ * The save_stack_trace() is called returning all the functions it finds in the
+ * current stack. Which would be (from the bottom of the stack to the top):
+ *
+ *   return addr to kernel_func_bar
+ *   return addr to sys_foo
+ *   return addr to entry code
+ *
+ * Now to figure out how much each of these functions' local variable size is,
+ * a search of the stack is made to find these values. When a match is made, it
+ * is added to the stack_dump_trace[] array. The offset into the stack is saved
+ * in the stack_trace_index[] array. The above example would show:
+ *
+ *        stack_dump_trace[]        |   stack_trace_index[]
+ *        ------------------        +   -------------------
+ *  return addr to kernel_func_bar  |          30
+ *  return addr to sys_foo          |          20
+ *  return addr to entry            |          10
+ *
+ * The print_max_stack() function above, uses these values to print the size of
+ * each function's portion of the stack.
+ *
+ *  for (i = 0; i < nr_entries; i++) {
+ *     size = i == nr_entries - 1 ? stack_trace_index[i] :
+ *                    stack_trace_index[i] - stack_trace_index[i+1]
+ *     print "%d %d %d %s\n", i, stack_trace_index[i], size, stack_dump_trace[i]);
+ *  }
+ *
+ * The above shows
+ *
+ *     depth size  location
+ *     ----- ----  --------
+ *  0    30   10   kernel_func_bar
+ *  1    20   10   sys_foo
+ *  2    10   10   entry code
+ *
+ * Now for architectures that might save the return address after the functions
+ * local variables (saving the link register before calling nested functions),
+ * this will cause the stack to look a little different:
+ *
+ * [ top of stack ]
+ *  0: sys call entry frame
+ * 10: start of sys_foo_frame
+ * 19: return addr to entry code << lr saved before calling kernel_func_bar
+ * 20: start of kernel_func_bar frame
+ * 29: return addr to sys_foo_frame << lr saved before calling next function
+ * 30: [ do trace stack here ]
+ *
+ * Although the functions returned by save_stack_trace() may be the same, the
+ * placement in the stack will be different. Using the same algorithm as above
+ * would yield:
+ *
+ *        stack_dump_trace[]        |   stack_trace_index[]
+ *        ------------------        +   -------------------
+ *  return addr to kernel_func_bar  |          30
+ *  return addr to sys_foo          |          29
+ *  return addr to entry            |          19
+ *
+ * Where the mapping is off by one:
+ *
+ *   kernel_func_bar stack frame size is 29 - 19 not 30 - 29!
+ *
+ * To fix this, if the architecture sets ARCH_RET_ADDR_AFTER_LOCAL_VARS the
+ * values in stack_trace_index[] are shifted by one to and the number of
+ * stack trace entries is decremented by one.
+ *
+ *        stack_dump_trace[]        |   stack_trace_index[]
+ *        ------------------        +   -------------------
+ *  return addr to kernel_func_bar  |          29
+ *  return addr to sys_foo          |          19
+ *
+ * Although the entry function is not displayed, the first function (sys_foo)
+ * will still include the stack size of it.
+ */
 static void check_stack(unsigned long ip, unsigned long *stack)
 {
 	unsigned long this_size, flags; unsigned long *p, *top, *start;
Signature · 1 line
2.20.1
Acked-by on v2 2/2
#13 lore raw

Re: [PATCH 2/2 v2] tracing: Document the stack trace algorithm in the comments

Quoted 8 lines · Steven Rostedt

From: "Steven Rostedt (VMware)" <[email protected]>

As the max stack tracer algorithm is not that easy to understand from the code, add comments that explain the algorithm and mentions how ARCH_RET_ADDR_AFTER_LOCAL_VARS affects it.

thanks!!

- Joel

Quoted 119 lines
---
 kernel/trace/trace_stack.c | 98 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 98 insertions(+)
kernel/trace/trace_stack.c +98 -0
index 40e4a88eea8f..f94a2fc567de 100644
@@ -53,6 +53,104 @@ static void print_max_stack(void)
 	}
 }
 
+/*
+ * The stack tracer looks for a maximum stack at each call from a function. It
+ * registers a callback from ftrace, and in that callback it examines the stack
+ * size. It determines the stack size from the variable passed in, which is the
+ * address of a local variable in the stack_trace_call() callback function.
+ * The stack size is calculated by the address of the local variable to the top
+ * of the current stack. If that size is smaller than the currently saved max
+ * stack size, nothing more is done.
+ *
+ * If the size of the stack is greater than the maximum recorded size, then the
+ * following algorithm takes place.
+ *
+ * For architectures (like x86) that store the function's return address before
+ * saving the function's local variables, the stack will look something like
+ * this:
+ *
+ *   [ top of stack ]
+ *    0: sys call entry frame
+ *   10: return addr to entry code
+ *   11: start of sys_foo frame
+ *   20: return addr to sys_foo
+ *   21: start of kernel_func_bar frame
+ *   30: return addr to kernel_func_bar
+ *   31: [ do trace stack here ]
+ *
+ * The save_stack_trace() is called returning all the functions it finds in the
+ * current stack. Which would be (from the bottom of the stack to the top):
+ *
+ *   return addr to kernel_func_bar
+ *   return addr to sys_foo
+ *   return addr to entry code
+ *
+ * Now to figure out how much each of these functions' local variable size is,
+ * a search of the stack is made to find these values. When a match is made, it
+ * is added to the stack_dump_trace[] array. The offset into the stack is saved
+ * in the stack_trace_index[] array. The above example would show:
+ *
+ *        stack_dump_trace[]        |   stack_trace_index[]
+ *        ------------------        +   -------------------
+ *  return addr to kernel_func_bar  |          30
+ *  return addr to sys_foo          |          20
+ *  return addr to entry            |          10
+ *
+ * The print_max_stack() function above, uses these values to print the size of
+ * each function's portion of the stack.
+ *
+ *  for (i = 0; i < nr_entries; i++) {
+ *     size = i == nr_entries - 1 ? stack_trace_index[i] :
+ *                    stack_trace_index[i] - stack_trace_index[i+1]
+ *     print "%d %d %d %s\n", i, stack_trace_index[i], size, stack_dump_trace[i]);
+ *  }
+ *
+ * The above shows
+ *
+ *     depth size  location
+ *     ----- ----  --------
+ *  0    30   10   kernel_func_bar
+ *  1    20   10   sys_foo
+ *  2    10   10   entry code
+ *
+ * Now for architectures that might save the return address after the functions
+ * local variables (saving the link register before calling nested functions),
+ * this will cause the stack to look a little different:
+ *
+ * [ top of stack ]
+ *  0: sys call entry frame
+ * 10: start of sys_foo_frame
+ * 19: return addr to entry code << lr saved before calling kernel_func_bar
+ * 20: start of kernel_func_bar frame
+ * 29: return addr to sys_foo_frame << lr saved before calling next function
+ * 30: [ do trace stack here ]
+ *
+ * Although the functions returned by save_stack_trace() may be the same, the
+ * placement in the stack will be different. Using the same algorithm as above
+ * would yield:
+ *
+ *        stack_dump_trace[]        |   stack_trace_index[]
+ *        ------------------        +   -------------------
+ *  return addr to kernel_func_bar  |          30
+ *  return addr to sys_foo          |          29
+ *  return addr to entry            |          19
+ *
+ * Where the mapping is off by one:
+ *
+ *   kernel_func_bar stack frame size is 29 - 19 not 30 - 29!
+ *
+ * To fix this, if the architecture sets ARCH_RET_ADDR_AFTER_LOCAL_VARS the
+ * values in stack_trace_index[] are shifted by one to and the number of
+ * stack trace entries is decremented by one.
+ *
+ *        stack_dump_trace[]        |   stack_trace_index[]
+ *        ------------------        +   -------------------
+ *  return addr to kernel_func_bar  |          29
+ *  return addr to sys_foo          |          19
+ *
+ * Although the entry function is not displayed, the first function (sys_foo)
+ * will still include the stack size of it.
+ */
 static void check_stack(unsigned long ip, unsigned long *stack)
 {
 	unsigned long this_size, flags; unsigned long *p, *top, *start;
Signature · 1 line
2.20.1