blob: 2abf27d7df6b8b8b46b972a52619993075f0812b [file] [log] [blame]
Ingo Molnar21b32bb2006-07-03 00:24:40 -07001/*
Ingo Molnar21b32bb2006-07-03 00:24:40 -07002 * Stack trace management functions
3 *
Ingo Molnar8f47e162009-01-31 02:03:42 +01004 * Copyright (C) 2006-2009 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
Ingo Molnar21b32bb2006-07-03 00:24:40 -07005 */
6#include <linux/sched.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +01007#include <linux/sched/debug.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +01008#include <linux/sched/task_stack.h>
Ingo Molnar21b32bb2006-07-03 00:24:40 -07009#include <linux/stacktrace.h>
Paul Gortmaker186f4362016-07-13 20:18:56 -040010#include <linux/export.h>
Török Edwin02b67512008-11-22 13:28:47 +020011#include <linux/uaccess.h>
Andi Kleenc0b766f2006-09-26 10:52:34 +020012#include <asm/stacktrace.h>
Josh Poimboeuf49a612c2016-09-16 14:18:14 -050013#include <asm/unwind.h>
Ingo Molnar21b32bb2006-07-03 00:24:40 -070014
Thomas Gleixner3599fe12019-04-25 11:45:22 +020015void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie,
16 struct task_struct *task, struct pt_regs *regs)
Josh Poimboeuf49a612c2016-09-16 14:18:14 -050017{
18 struct unwind_state state;
19 unsigned long addr;
20
Thomas Gleixner3599fe12019-04-25 11:45:22 +020021 if (regs && !consume_entry(cookie, regs->ip, false))
22 return;
Josh Poimboeuf49a612c2016-09-16 14:18:14 -050023
24 for (unwind_start(&state, task, regs, NULL); !unwind_done(&state);
25 unwind_next_frame(&state)) {
26 addr = unwind_get_return_address(&state);
Thomas Gleixner3599fe12019-04-25 11:45:22 +020027 if (!addr || !consume_entry(cookie, addr, false))
Josh Poimboeuf49a612c2016-09-16 14:18:14 -050028 break;
Alexei Starovoitov568b3292016-02-17 19:58:57 -080029 }
Andi Kleenc0b766f2006-09-26 10:52:34 +020030}
31
Ingo Molnar21b32bb2006-07-03 00:24:40 -070032/*
Thomas Gleixner3599fe12019-04-25 11:45:22 +020033 * This function returns an error if it detects any unreliable features of the
34 * stack. Otherwise it guarantees that the stack trace is reliable.
35 *
36 * If the task is not 'current', the caller *must* ensure the task is inactive.
Ingo Molnar21b32bb2006-07-03 00:24:40 -070037 */
Thomas Gleixner3599fe12019-04-25 11:45:22 +020038int arch_stack_walk_reliable(stack_trace_consume_fn consume_entry,
39 void *cookie, struct task_struct *task)
Josh Poimboeufaf085d92017-02-13 19:42:28 -060040{
41 struct unwind_state state;
42 struct pt_regs *regs;
43 unsigned long addr;
44
Jiri Slaby441ccc32018-05-18 08:47:10 +020045 for (unwind_start(&state, task, NULL, NULL);
46 !unwind_done(&state) && !unwind_error(&state);
Josh Poimboeufaf085d92017-02-13 19:42:28 -060047 unwind_next_frame(&state)) {
48
Josh Poimboeufa9cdbe72017-12-31 10:18:06 -060049 regs = unwind_get_entry_regs(&state, NULL);
Josh Poimboeufaf085d92017-02-13 19:42:28 -060050 if (regs) {
Jiri Slaby441ccc32018-05-18 08:47:10 +020051 /* Success path for user tasks */
52 if (user_mode(regs))
Thomas Gleixnerc5c27a02019-04-10 12:27:56 +020053 return 0;
Jiri Slaby441ccc32018-05-18 08:47:10 +020054
Josh Poimboeufaf085d92017-02-13 19:42:28 -060055 /*
56 * Kernel mode registers on the stack indicate an
57 * in-kernel interrupt or exception (e.g., preemption
58 * or a page fault), which can make frame pointers
59 * unreliable.
60 */
Josh Poimboeufaf085d92017-02-13 19:42:28 -060061
Jiri Slaby0c414362018-05-18 08:47:11 +020062 if (IS_ENABLED(CONFIG_FRAME_POINTER))
63 return -EINVAL;
Josh Poimboeufaf085d92017-02-13 19:42:28 -060064 }
65
66 addr = unwind_get_return_address(&state);
67
68 /*
69 * A NULL or invalid return address probably means there's some
70 * generated code which __kernel_text_address() doesn't know
71 * about.
72 */
Jiri Slaby17426922018-05-18 08:47:09 +020073 if (!addr)
Josh Poimboeufaf085d92017-02-13 19:42:28 -060074 return -EINVAL;
Josh Poimboeufaf085d92017-02-13 19:42:28 -060075
Thomas Gleixner3599fe12019-04-25 11:45:22 +020076 if (!consume_entry(cookie, addr, false))
Josh Poimboeufaf085d92017-02-13 19:42:28 -060077 return -EINVAL;
78 }
79
80 /* Check for stack corruption */
Jiri Slaby17426922018-05-18 08:47:09 +020081 if (unwind_error(&state))
Josh Poimboeufaf085d92017-02-13 19:42:28 -060082 return -EINVAL;
Josh Poimboeufaf085d92017-02-13 19:42:28 -060083
Jiri Slaby441ccc32018-05-18 08:47:10 +020084 /* Success path for non-user tasks, i.e. kthreads and idle tasks */
85 if (!(task->flags & (PF_KTHREAD | PF_IDLE)))
86 return -EINVAL;
87
Josh Poimboeufaf085d92017-02-13 19:42:28 -060088 return 0;
89}
90
Török Edwin02b67512008-11-22 13:28:47 +020091/* Userspace stacktrace - based on kernel/trace/trace_sysprof.c */
92
Frederic Weisbeckerc9cf4db2010-05-19 21:35:17 +020093struct stack_frame_user {
Török Edwin02b67512008-11-22 13:28:47 +020094 const void __user *next_fp;
Török Edwin8d7c6a92008-11-23 12:39:06 +020095 unsigned long ret_addr;
Török Edwin02b67512008-11-22 13:28:47 +020096};
97
Frederic Weisbeckerc9cf4db2010-05-19 21:35:17 +020098static int
99copy_stack_frame(const void __user *fp, struct stack_frame_user *frame)
Török Edwin02b67512008-11-22 13:28:47 +0200100{
101 int ret;
102
Linus Torvalds96d4f262019-01-03 18:57:57 -0800103 if (!access_ok(fp, sizeof(*frame)))
Török Edwin02b67512008-11-22 13:28:47 +0200104 return 0;
105
106 ret = 1;
107 pagefault_disable();
108 if (__copy_from_user_inatomic(frame, fp, sizeof(*frame)))
109 ret = 0;
110 pagefault_enable();
111
112 return ret;
113}
114
Thomas Gleixner3599fe12019-04-25 11:45:22 +0200115void arch_stack_walk_user(stack_trace_consume_fn consume_entry, void *cookie,
116 const struct pt_regs *regs)
Török Edwin8d7c6a92008-11-23 12:39:06 +0200117{
Török Edwin8d7c6a92008-11-23 12:39:06 +0200118 const void __user *fp = (const void __user *)regs->bp;
119
Thomas Gleixner3599fe12019-04-25 11:45:22 +0200120 if (!consume_entry(cookie, regs->ip, false))
121 return;
Török Edwin8d7c6a92008-11-23 12:39:06 +0200122
Thomas Gleixner3599fe12019-04-25 11:45:22 +0200123 while (1) {
Frederic Weisbeckerc9cf4db2010-05-19 21:35:17 +0200124 struct stack_frame_user frame;
Török Edwin8d7c6a92008-11-23 12:39:06 +0200125
126 frame.next_fp = NULL;
127 frame.ret_addr = 0;
128 if (!copy_stack_frame(fp, &frame))
129 break;
130 if ((unsigned long)fp < regs->sp)
131 break;
132 if (frame.ret_addr) {
Thomas Gleixner3599fe12019-04-25 11:45:22 +0200133 if (!consume_entry(cookie, frame.ret_addr, false))
134 return;
Török Edwin8d7c6a92008-11-23 12:39:06 +0200135 }
136 if (fp == frame.next_fp)
137 break;
138 fp = frame.next_fp;
139 }
140}
141