Skip to contentSkip to conversation

xtensa: improve stack dumping

PATCH Superseded Max Filippov 2 participants 3 messages Nov 8, 2019 – Nov 12, 2019 Tensilica Xtensa port (xtensa)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Max Filippov

author maintainer
#0 lore raw

PATCH v2 1/1 xtensa: improve stack dumping

Calculate printable stack size and use print_hex_dump instead of opencoding it.
Make size of stack dump configurable.
Drop extra newline output in show_trace as its output format does not depend on CONFIG_KALLSYMS.

---
Changes v1->v2:
- use print_hex_dump.

 arch/xtensa/Kconfig.debug  |  7 +++++++
 arch/xtensa/kernel/traps.c | 24 ++++++++----------------
 2 files changed, 15 insertions(+), 16 deletions(-)
arch/xtensa/Kconfig.debug +7 -0
index 39de98e20018..83cc8d12fa0e 100644
@@ -31,3 +31,10 @@ config S32C1I_SELFTEST
 	  It is easy to make wrong hardware configuration, this test should catch it early.
 
 	  Say 'N' on stable hardware.
+
+config PRINT_STACK_DEPTH
+	int "Stack depth to print" if DEBUG_KERNEL
+	default 64
+	help
+	  This option allows you to set the stack depth that the kernel
+	  prints in stack traces.
arch/xtensa/kernel/traps.c +8 -16
index 4a6c495ce9b6..fe090ab1cab8 100644
@@ -491,32 +491,24 @@ void show_trace(struct task_struct *task, unsigned long *sp)
 
 	pr_info("Call Trace:\n");
 	walk_stackframe(sp, show_trace_cb, NULL);
-#ifndef CONFIG_KALLSYMS
-	pr_cont("\n");
-#endif
 }
 
-static int kstack_depth_to_print = 24;
+static int kstack_depth_to_print = CONFIG_PRINT_STACK_DEPTH;
 
 void show_stack(struct task_struct *task, unsigned long *sp)
 {
-	int i = 0;
-	unsigned long *stack;
+	size_t len;
 
 	if (!sp)
 		sp = stack_pointer(task);
-	stack = sp;
 
-	pr_info("Stack:\n");
+	len = min((-(unsigned long)sp) & (THREAD_SIZE - 4),
+		  kstack_depth_to_print * 4ul);
 
-	for (i = 0; i < kstack_depth_to_print; i++) {
-		if (kstack_end(sp))
-			break;
-		pr_cont(" %08lx", *sp++);
-		if (i % 8 == 7)
-			pr_cont("\n");
-	}
-	show_trace(task, stack);
+	pr_info("Stack:\n");
+	print_hex_dump(KERN_INFO, " ", DUMP_PREFIX_NONE, 32, 4,
+		       sp, len, false);
+	show_trace(task, sp);
 }
 
 DEFINE_SPINLOCK(die_lock);
Signature · 1 line
2.20.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 11 lines · Max Filippov

Calculate printable stack size and use print_hex_dump instead of opencoding it.
Make size of stack dump configurable.
Drop extra newline output in show_trace as its output format does not depend on CONFIG_KALLSYMS.

arch/xtensa/Kconfig.debug +0 -0
index 39de98e20018..83cc8d12fa0e 100644

arch/xtensa/Kconfig.debug config S32C1I_SELFTEST · line 40

Quoted 10 lines
arch/xtensa/Kconfig.debug +7 -0
@@ -31,3 +31,10 @@ config S32C1I_SELFTEST
 	  It is easy to make wrong hardware configuration, this test should catch it early.
 
 	  Say 'N' on stable hardware.
+
+config PRINT_STACK_DEPTH
+	int "Stack depth to print" if DEBUG_KERNEL
+	default 64
+	help
+	  This option allows you to set the stack depth that the kernel
+	  prints in stack traces.

I would split this into separate patch.

Quoted 19 lines
arch/xtensa/kernel/traps.c +1 -5
index 4a6c495ce9b6..fe090ab1cab8 100644
@@ -491,32 +491,24 @@ void show_trace(struct task_struct *task, unsigned long *sp)
 
 	pr_info("Call Trace:\n");
 	walk_stackframe(sp, show_trace_cb, NULL);
-#ifndef CONFIG_KALLSYMS
-	pr_cont("\n");
-#endif
 }
 
-static int kstack_depth_to_print = 24;
+static int kstack_depth_to_print = CONFIG_PRINT_STACK_DEPTH;
 
 void show_stack(struct task_struct *task, unsigned long *sp)
 {
-	int i = 0;

arch/xtensa/kernel/traps.c void show_trace(struct task_struct *task, unsigned long *sp) · line 506

Quoted 10 lines
arch/xtensa/kernel/traps.c +3 -3
-	unsigned long *stack;
+	size_t len;
 
 	if (!sp)
 		sp = stack_pointer(task);
-	stack = sp;
 
-	pr_info("Stack:\n");
+	len = min((-(unsigned long)sp) & (THREAD_SIZE - 4),
+		  kstack_depth_to_print * 4ul);

I would replace the hardcoded 4 with sizeof(void *).

Quoted 4 lines
arch/xtensa/kernel/traps.c +0 -3
-	for (i = 0; i < kstack_depth_to_print; i++) {
-		if (kstack_end(sp))
-			break;

arch/xtensa/kernel/traps.c void show_trace(struct task_struct *task, unsigned long *sp) · line 512

Quoted 10 lines
arch/xtensa/kernel/traps.c +4 -5
-		pr_cont(" %08lx", *sp++);
-		if (i % 8 == 7)
-			pr_cont("\n");
-	}
-	show_trace(task, stack);
+	pr_info("Stack:\n");
+	print_hex_dump(KERN_INFO, " ", DUMP_PREFIX_NONE, 32, 4,
+		       sp, len, false);
+	show_trace(task, sp);
 }

The conversion looks fine to me. It is up to you (as a maintainer) what you do with the above proposal for two cosmetic changes ;-) Either way, feel free to use:

Best Regards,
Petr

author maintainer
#2 lore raw
Quoted 15 lines · Petr Mladek
Quoted 13 lines · Max Filippov

Calculate printable stack size and use print_hex_dump instead of opencoding it.
Make size of stack dump configurable.
Drop extra newline output in show_trace as its output format does not depend on CONFIG_KALLSYMS.

arch/xtensa/Kconfig.debug +0 -0
index 39de98e20018..83cc8d12fa0e 100644
@@ -31,3 +31,10 @@ config S32C1I_SELFTEST
        It is easy to make wrong hardware configuration, this test should catch it early.
 
Quoted 10 lines

arch/xtensa/Kconfig.debug config S32C1I_SELFTEST · line 40

Quoted 8 lines
arch/xtensa/Kconfig.debug +7 -0
        Say 'N' on stable hardware.
+
+config PRINT_STACK_DEPTH
+     int "Stack depth to print" if DEBUG_KERNEL
+     default 64
+     help
+       This option allows you to set the stack depth that the kernel
+       prints in stack traces.

I would split this into separate patch.

Sure, I'll split it out.

Quoted 26 lines
Quoted 26 lines
arch/xtensa/kernel/traps.c +2 -7
index 4a6c495ce9b6..fe090ab1cab8 100644
@@ -491,32 +491,24 @@ void show_trace(struct task_struct *task, unsigned long *sp)
 
      pr_info("Call Trace:\n");
      walk_stackframe(sp, show_trace_cb, NULL);
-#ifndef CONFIG_KALLSYMS
-     pr_cont("\n");
-#endif
 }
 
-static int kstack_depth_to_print = 24;
+static int kstack_depth_to_print = CONFIG_PRINT_STACK_DEPTH;
 
 void show_stack(struct task_struct *task, unsigned long *sp)
 {
-     int i = 0;
-     unsigned long *stack;
+     size_t len;
 
      if (!sp)
              sp = stack_pointer(task);
-     stack = sp;
 
Quoted 5 lines

arch/xtensa/kernel/traps.c void show_trace(struct task_struct *task, unsigned long *sp) · line 506

Quoted 3 lines
arch/xtensa/kernel/traps.c +2 -1
-     pr_info("Stack:\n");
+     len = min((-(unsigned long)sp) & (THREAD_SIZE - 4),
+               kstack_depth_to_print * 4ul);

I would replace the hardcoded 4 with sizeof(void *).

It's not necessarily pointers, more like register-wide entries on the stack. Ok, I guess I'll give it a name.

Quoted 14 lines

arch/xtensa/kernel/traps.c void show_trace(struct task_struct *task, unsigned long *sp) · line 512

Quoted 13 lines
arch/xtensa/kernel/traps.c +4 -8
-     for (i = 0; i < kstack_depth_to_print; i++) {
-             if (kstack_end(sp))
-                     break;
-             pr_cont(" %08lx", *sp++);
-             if (i % 8 == 7)
-                     pr_cont("\n");
-     }
-     show_trace(task, stack);
+     pr_info("Stack:\n");
+     print_hex_dump(KERN_INFO, " ", DUMP_PREFIX_NONE, 32, 4,
+                    sp, len, false);
+     show_trace(task, sp);
 }
Quoted 5 lines

The conversion looks fine to me. It is up to you (as a maintainer) what you do with the above proposal for two cosmetic changes ;-) Either way, feel free to use:

Thanks for the review.

-- Max