x86/stacktrace: Convert save_stack_trace_*() to use the new unwinder
authorJosh Poimboeuf <jpoimboe@redhat.com>
Fri, 16 Sep 2016 19:18:14 +0000 (14:18 -0500)
committerIngo Molnar <mingo@kernel.org>
Tue, 20 Sep 2016 06:29:33 +0000 (08:29 +0200)
Convert save_stack_trace_*() to use the new unwinder.  dump_trace() has
been deprecated.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Brian Gerst <brgerst@gmail.com>
Cc: Byungchul Park <byungchul.park@lge.com>
Cc: Denys Vlasenko <dvlasenk@redhat.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Nilay Vaish <nilayvaish@gmail.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Link: http://lkml.kernel.org/r/815494c627d89887db0ce56ceffd58ad16ee6c21.1474045023.git.jpoimboe@redhat.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
arch/x86/kernel/stacktrace.c

index 23fa81e24c8a7f7437ee0c0149a30753df15241c..0653788026e28863c099955b9f5d61b4983b017c 100644 (file)
@@ -8,73 +8,59 @@
 #include <linux/export.h>
 #include <linux/uaccess.h>
 #include <asm/stacktrace.h>
+#include <asm/unwind.h>
 
-static int save_stack_stack(void *data, const char *name)
+static int save_stack_address(struct stack_trace *trace, unsigned long addr,
+                             bool nosched)
 {
-       return 0;
-}
-
-static int
-__save_stack_address(void *data, unsigned long addr, bool reliable, bool nosched)
-{
-       struct stack_trace *trace = data;
-#ifdef CONFIG_FRAME_POINTER
-       if (!reliable)
-               return 0;
-#endif
        if (nosched && in_sched_functions(addr))
                return 0;
+
        if (trace->skip > 0) {
                trace->skip--;
                return 0;
        }
-       if (trace->nr_entries < trace->max_entries) {
-               trace->entries[trace->nr_entries++] = addr;
-               return 0;
-       } else {
-               return -1; /* no more room, stop walking the stack */
-       }
-}
 
-static int save_stack_address(void *data, unsigned long addr, int reliable)
-{
-       return __save_stack_address(data, addr, reliable, false);
+       if (trace->nr_entries >= trace->max_entries)
+               return -1;
+
+       trace->entries[trace->nr_entries++] = addr;
+       return 0;
 }
 
-static int
-save_stack_address_nosched(void *data, unsigned long addr, int reliable)
+static void __save_stack_trace(struct stack_trace *trace,
+                              struct task_struct *task, struct pt_regs *regs,
+                              bool nosched)
 {
-       return __save_stack_address(data, addr, reliable, true);
-}
+       struct unwind_state state;
+       unsigned long addr;
 
-static const struct stacktrace_ops save_stack_ops = {
-       .stack          = save_stack_stack,
-       .address        = save_stack_address,
-       .walk_stack     = print_context_stack,
-};
+       if (regs)
+               save_stack_address(trace, regs->ip, nosched);
 
-static const struct stacktrace_ops save_stack_ops_nosched = {
-       .stack          = save_stack_stack,
-       .address        = save_stack_address_nosched,
-       .walk_stack     = print_context_stack,
-};
+       for (unwind_start(&state, task, regs, NULL); !unwind_done(&state);
+            unwind_next_frame(&state)) {
+               addr = unwind_get_return_address(&state);
+               if (!addr || save_stack_address(trace, addr, nosched))
+                       break;
+       }
+
+       if (trace->nr_entries < trace->max_entries)
+               trace->entries[trace->nr_entries++] = ULONG_MAX;
+}
 
 /*
  * Save stack-backtrace addresses into a stack_trace buffer.
  */
 void save_stack_trace(struct stack_trace *trace)
 {
-       dump_trace(current, NULL, NULL, 0, &save_stack_ops, trace);
-       if (trace->nr_entries < trace->max_entries)
-               trace->entries[trace->nr_entries++] = ULONG_MAX;
+       __save_stack_trace(trace, current, NULL, false);
 }
 EXPORT_SYMBOL_GPL(save_stack_trace);
 
 void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
 {
-       dump_trace(current, regs, NULL, 0, &save_stack_ops, trace);
-       if (trace->nr_entries < trace->max_entries)
-               trace->entries[trace->nr_entries++] = ULONG_MAX;
+       __save_stack_trace(trace, current, regs, false);
 }
 
 void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
@@ -82,9 +68,7 @@ void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
        if (!try_get_task_stack(tsk))
                return;
 
-       dump_trace(tsk, NULL, NULL, 0, &save_stack_ops_nosched, trace);
-       if (trace->nr_entries < trace->max_entries)
-               trace->entries[trace->nr_entries++] = ULONG_MAX;
+       __save_stack_trace(trace, tsk, NULL, true);
 
        put_task_stack(tsk);
 }