x86/unwind: Dump stack data on warnings
authorJosh Poimboeuf <jpoimboe@redhat.com>
Fri, 16 Dec 2016 16:05:06 +0000 (10:05 -0600)
committerThomas Gleixner <tglx@linutronix.de>
Mon, 19 Dec 2016 10:47:05 +0000 (11:47 +0100)
The unwinder warnings are good at finding unexpected unwinder issues,
but they often don't give enough data to be able to fully diagnose them.
Print a one-time stack dump when a warning is detected.

Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Andy Lutomirski <luto@amacapital.net>
Link: http://lkml.kernel.org/r/15607370e3ddb1732b6a73d5c65937864df16ac8.1481904011.git.jpoimboe@redhat.com
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
arch/x86/include/asm/unwind.h
arch/x86/kernel/unwind_frame.c

index c5a7f3a930dd5bf8ae18b0c89059aca7cf23aaac..6fa75b17aec33aea051e1de2aea9199b0f6c836f 100644 (file)
@@ -12,7 +12,7 @@ struct unwind_state {
        struct task_struct *task;
        int graph_idx;
 #ifdef CONFIG_FRAME_POINTER
-       unsigned long *bp;
+       unsigned long *bp, *orig_sp;
        struct pt_regs *regs;
 #else
        unsigned long *sp;
index 33aeaae961aa3c5eab92f56701e312841eeb48df..20d4b4e0800c98df540fdcfaf4c3853d51b17edb 100644 (file)
@@ -6,6 +6,37 @@
 
 #define FRAME_HEADER_SIZE (sizeof(long) * 2)
 
+static void unwind_dump(struct unwind_state *state, unsigned long *sp)
+{
+       static bool dumped_before = false;
+       bool prev_zero, zero = false;
+       unsigned long word;
+
+       if (dumped_before)
+               return;
+
+       dumped_before = true;
+
+       printk_deferred("unwind stack type:%d next_sp:%p mask:%lx graph_idx:%d\n",
+                       state->stack_info.type, state->stack_info.next_sp,
+                       state->stack_mask, state->graph_idx);
+
+       for (sp = state->orig_sp; sp < state->stack_info.end; sp++) {
+               word = READ_ONCE_NOCHECK(*sp);
+
+               prev_zero = zero;
+               zero = word == 0;
+
+               if (zero) {
+                       if (!prev_zero)
+                               printk_deferred("%p: %016x ...\n", sp, 0);
+                       continue;
+               }
+
+               printk_deferred("%p: %016lx (%pB)\n", sp, word, (void *)word);
+       }
+}
+
 unsigned long unwind_get_return_address(struct unwind_state *state)
 {
        unsigned long addr;
@@ -25,6 +56,7 @@ unsigned long unwind_get_return_address(struct unwind_state *state)
                        "WARNING: unrecognized kernel stack return address %p at %p in %s:%d\n",
                        (void *)addr, addr_p, state->task->comm,
                        state->task->pid);
+               unwind_dump(state, addr_p);
                return 0;
        }
 
@@ -74,6 +106,7 @@ static bool update_stack_state(struct unwind_state *state, void *addr,
                               size_t len)
 {
        struct stack_info *info = &state->stack_info;
+       enum stack_type orig_type = info->type;
 
        /*
         * If addr isn't on the current stack, switch to the next one.
@@ -87,6 +120,9 @@ static bool update_stack_state(struct unwind_state *state, void *addr,
                                   &state->stack_mask))
                        return false;
 
+       if (!state->orig_sp || info->type != orig_type)
+               state->orig_sp = addr;
+
        return true;
 }
 
@@ -185,11 +221,13 @@ bad_address:
                        "WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n",
                        state->regs, state->task->comm,
                        state->task->pid, next_frame);
+               unwind_dump(state, (unsigned long *)state->regs);
        } else {
                printk_deferred_once(KERN_WARNING
                        "WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n",
                        state->bp, state->task->comm,
                        state->task->pid, next_frame);
+               unwind_dump(state, state->bp);
        }
 the_end:
        state->stack_info.type = STACK_TYPE_UNKNOWN;