xref: /openbmc/linux/arch/x86/kernel/stacktrace.c (revision af085d9084b48530153f51e6cad19fd0b1a13ed7)
1250c2277SThomas Gleixner /*
2250c2277SThomas Gleixner  * Stack trace management functions
3250c2277SThomas Gleixner  *
48f47e163SIngo Molnar  *  Copyright (C) 2006-2009 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
5250c2277SThomas Gleixner  */
6250c2277SThomas Gleixner #include <linux/sched.h>
7b17b0153SIngo Molnar #include <linux/sched/debug.h>
868db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
9250c2277SThomas Gleixner #include <linux/stacktrace.h>
10186f4360SPaul Gortmaker #include <linux/export.h>
1102b67518STörök Edwin #include <linux/uaccess.h>
12250c2277SThomas Gleixner #include <asm/stacktrace.h>
1349a612c6SJosh Poimboeuf #include <asm/unwind.h>
14250c2277SThomas Gleixner 
1549a612c6SJosh Poimboeuf static int save_stack_address(struct stack_trace *trace, unsigned long addr,
1649a612c6SJosh Poimboeuf 			      bool nosched)
17250c2277SThomas Gleixner {
18018378c5SOleg Nesterov 	if (nosched && in_sched_functions(addr))
19568b329aSAlexei Starovoitov 		return 0;
2049a612c6SJosh Poimboeuf 
21250c2277SThomas Gleixner 	if (trace->skip > 0) {
22250c2277SThomas Gleixner 		trace->skip--;
23568b329aSAlexei Starovoitov 		return 0;
24250c2277SThomas Gleixner 	}
2549a612c6SJosh Poimboeuf 
2649a612c6SJosh Poimboeuf 	if (trace->nr_entries >= trace->max_entries)
2749a612c6SJosh Poimboeuf 		return -1;
2849a612c6SJosh Poimboeuf 
29250c2277SThomas Gleixner 	trace->entries[trace->nr_entries++] = addr;
30568b329aSAlexei Starovoitov 	return 0;
31250c2277SThomas Gleixner }
32250c2277SThomas Gleixner 
3349a612c6SJosh Poimboeuf static void __save_stack_trace(struct stack_trace *trace,
3449a612c6SJosh Poimboeuf 			       struct task_struct *task, struct pt_regs *regs,
3549a612c6SJosh Poimboeuf 			       bool nosched)
36018378c5SOleg Nesterov {
3749a612c6SJosh Poimboeuf 	struct unwind_state state;
3849a612c6SJosh Poimboeuf 	unsigned long addr;
3949a612c6SJosh Poimboeuf 
4049a612c6SJosh Poimboeuf 	if (regs)
4149a612c6SJosh Poimboeuf 		save_stack_address(trace, regs->ip, nosched);
4249a612c6SJosh Poimboeuf 
4349a612c6SJosh Poimboeuf 	for (unwind_start(&state, task, regs, NULL); !unwind_done(&state);
4449a612c6SJosh Poimboeuf 	     unwind_next_frame(&state)) {
4549a612c6SJosh Poimboeuf 		addr = unwind_get_return_address(&state);
4649a612c6SJosh Poimboeuf 		if (!addr || save_stack_address(trace, addr, nosched))
4749a612c6SJosh Poimboeuf 			break;
48018378c5SOleg Nesterov 	}
49018378c5SOleg Nesterov 
5049a612c6SJosh Poimboeuf 	if (trace->nr_entries < trace->max_entries)
5149a612c6SJosh Poimboeuf 		trace->entries[trace->nr_entries++] = ULONG_MAX;
529745512cSArjan van de Ven }
539745512cSArjan van de Ven 
54250c2277SThomas Gleixner /*
55250c2277SThomas Gleixner  * Save stack-backtrace addresses into a stack_trace buffer.
56250c2277SThomas Gleixner  */
57250c2277SThomas Gleixner void save_stack_trace(struct stack_trace *trace)
58250c2277SThomas Gleixner {
5949a612c6SJosh Poimboeuf 	__save_stack_trace(trace, current, NULL, false);
60250c2277SThomas Gleixner }
618594698eSIngo Molnar EXPORT_SYMBOL_GPL(save_stack_trace);
629745512cSArjan van de Ven 
6339581062SMasami Hiramatsu void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
64acc6be54SVegard Nossum {
6549a612c6SJosh Poimboeuf 	__save_stack_trace(trace, current, regs, false);
66acc6be54SVegard Nossum }
67acc6be54SVegard Nossum 
689745512cSArjan van de Ven void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
699745512cSArjan van de Ven {
701959a601SAndy Lutomirski 	if (!try_get_task_stack(tsk))
711959a601SAndy Lutomirski 		return;
721959a601SAndy Lutomirski 
7349a612c6SJosh Poimboeuf 	__save_stack_trace(trace, tsk, NULL, true);
741959a601SAndy Lutomirski 
751959a601SAndy Lutomirski 	put_task_stack(tsk);
769745512cSArjan van de Ven }
778594698eSIngo Molnar EXPORT_SYMBOL_GPL(save_stack_trace_tsk);
7802b67518STörök Edwin 
79*af085d90SJosh Poimboeuf #ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
80*af085d90SJosh Poimboeuf 
81*af085d90SJosh Poimboeuf #define STACKTRACE_DUMP_ONCE(task) ({				\
82*af085d90SJosh Poimboeuf 	static bool __section(.data.unlikely) __dumped;		\
83*af085d90SJosh Poimboeuf 								\
84*af085d90SJosh Poimboeuf 	if (!__dumped) {					\
85*af085d90SJosh Poimboeuf 		__dumped = true;				\
86*af085d90SJosh Poimboeuf 		WARN_ON(1);					\
87*af085d90SJosh Poimboeuf 		show_stack(task, NULL);				\
88*af085d90SJosh Poimboeuf 	}							\
89*af085d90SJosh Poimboeuf })
90*af085d90SJosh Poimboeuf 
91*af085d90SJosh Poimboeuf static int __save_stack_trace_reliable(struct stack_trace *trace,
92*af085d90SJosh Poimboeuf 				       struct task_struct *task)
93*af085d90SJosh Poimboeuf {
94*af085d90SJosh Poimboeuf 	struct unwind_state state;
95*af085d90SJosh Poimboeuf 	struct pt_regs *regs;
96*af085d90SJosh Poimboeuf 	unsigned long addr;
97*af085d90SJosh Poimboeuf 
98*af085d90SJosh Poimboeuf 	for (unwind_start(&state, task, NULL, NULL); !unwind_done(&state);
99*af085d90SJosh Poimboeuf 	     unwind_next_frame(&state)) {
100*af085d90SJosh Poimboeuf 
101*af085d90SJosh Poimboeuf 		regs = unwind_get_entry_regs(&state);
102*af085d90SJosh Poimboeuf 		if (regs) {
103*af085d90SJosh Poimboeuf 			/*
104*af085d90SJosh Poimboeuf 			 * Kernel mode registers on the stack indicate an
105*af085d90SJosh Poimboeuf 			 * in-kernel interrupt or exception (e.g., preemption
106*af085d90SJosh Poimboeuf 			 * or a page fault), which can make frame pointers
107*af085d90SJosh Poimboeuf 			 * unreliable.
108*af085d90SJosh Poimboeuf 			 */
109*af085d90SJosh Poimboeuf 			if (!user_mode(regs))
110*af085d90SJosh Poimboeuf 				return -EINVAL;
111*af085d90SJosh Poimboeuf 
112*af085d90SJosh Poimboeuf 			/*
113*af085d90SJosh Poimboeuf 			 * The last frame contains the user mode syscall
114*af085d90SJosh Poimboeuf 			 * pt_regs.  Skip it and finish the unwind.
115*af085d90SJosh Poimboeuf 			 */
116*af085d90SJosh Poimboeuf 			unwind_next_frame(&state);
117*af085d90SJosh Poimboeuf 			if (!unwind_done(&state)) {
118*af085d90SJosh Poimboeuf 				STACKTRACE_DUMP_ONCE(task);
119*af085d90SJosh Poimboeuf 				return -EINVAL;
120*af085d90SJosh Poimboeuf 			}
121*af085d90SJosh Poimboeuf 			break;
122*af085d90SJosh Poimboeuf 		}
123*af085d90SJosh Poimboeuf 
124*af085d90SJosh Poimboeuf 		addr = unwind_get_return_address(&state);
125*af085d90SJosh Poimboeuf 
126*af085d90SJosh Poimboeuf 		/*
127*af085d90SJosh Poimboeuf 		 * A NULL or invalid return address probably means there's some
128*af085d90SJosh Poimboeuf 		 * generated code which __kernel_text_address() doesn't know
129*af085d90SJosh Poimboeuf 		 * about.
130*af085d90SJosh Poimboeuf 		 */
131*af085d90SJosh Poimboeuf 		if (!addr) {
132*af085d90SJosh Poimboeuf 			STACKTRACE_DUMP_ONCE(task);
133*af085d90SJosh Poimboeuf 			return -EINVAL;
134*af085d90SJosh Poimboeuf 		}
135*af085d90SJosh Poimboeuf 
136*af085d90SJosh Poimboeuf 		if (save_stack_address(trace, addr, false))
137*af085d90SJosh Poimboeuf 			return -EINVAL;
138*af085d90SJosh Poimboeuf 	}
139*af085d90SJosh Poimboeuf 
140*af085d90SJosh Poimboeuf 	/* Check for stack corruption */
141*af085d90SJosh Poimboeuf 	if (unwind_error(&state)) {
142*af085d90SJosh Poimboeuf 		STACKTRACE_DUMP_ONCE(task);
143*af085d90SJosh Poimboeuf 		return -EINVAL;
144*af085d90SJosh Poimboeuf 	}
145*af085d90SJosh Poimboeuf 
146*af085d90SJosh Poimboeuf 	if (trace->nr_entries < trace->max_entries)
147*af085d90SJosh Poimboeuf 		trace->entries[trace->nr_entries++] = ULONG_MAX;
148*af085d90SJosh Poimboeuf 
149*af085d90SJosh Poimboeuf 	return 0;
150*af085d90SJosh Poimboeuf }
151*af085d90SJosh Poimboeuf 
152*af085d90SJosh Poimboeuf /*
153*af085d90SJosh Poimboeuf  * This function returns an error if it detects any unreliable features of the
154*af085d90SJosh Poimboeuf  * stack.  Otherwise it guarantees that the stack trace is reliable.
155*af085d90SJosh Poimboeuf  *
156*af085d90SJosh Poimboeuf  * If the task is not 'current', the caller *must* ensure the task is inactive.
157*af085d90SJosh Poimboeuf  */
158*af085d90SJosh Poimboeuf int save_stack_trace_tsk_reliable(struct task_struct *tsk,
159*af085d90SJosh Poimboeuf 				  struct stack_trace *trace)
160*af085d90SJosh Poimboeuf {
161*af085d90SJosh Poimboeuf 	int ret;
162*af085d90SJosh Poimboeuf 
163*af085d90SJosh Poimboeuf 	if (!try_get_task_stack(tsk))
164*af085d90SJosh Poimboeuf 		return -EINVAL;
165*af085d90SJosh Poimboeuf 
166*af085d90SJosh Poimboeuf 	ret = __save_stack_trace_reliable(trace, tsk);
167*af085d90SJosh Poimboeuf 
168*af085d90SJosh Poimboeuf 	put_task_stack(tsk);
169*af085d90SJosh Poimboeuf 
170*af085d90SJosh Poimboeuf 	return ret;
171*af085d90SJosh Poimboeuf }
172*af085d90SJosh Poimboeuf #endif /* CONFIG_HAVE_RELIABLE_STACKTRACE */
173*af085d90SJosh Poimboeuf 
17402b67518STörök Edwin /* Userspace stacktrace - based on kernel/trace/trace_sysprof.c */
17502b67518STörök Edwin 
176c9cf4dbbSFrederic Weisbecker struct stack_frame_user {
17702b67518STörök Edwin 	const void __user	*next_fp;
1788d7c6a96STörök Edwin 	unsigned long		ret_addr;
17902b67518STörök Edwin };
18002b67518STörök Edwin 
181c9cf4dbbSFrederic Weisbecker static int
182c9cf4dbbSFrederic Weisbecker copy_stack_frame(const void __user *fp, struct stack_frame_user *frame)
18302b67518STörök Edwin {
18402b67518STörök Edwin 	int ret;
18502b67518STörök Edwin 
18602b67518STörök Edwin 	if (!access_ok(VERIFY_READ, fp, sizeof(*frame)))
18702b67518STörök Edwin 		return 0;
18802b67518STörök Edwin 
18902b67518STörök Edwin 	ret = 1;
19002b67518STörök Edwin 	pagefault_disable();
19102b67518STörök Edwin 	if (__copy_from_user_inatomic(frame, fp, sizeof(*frame)))
19202b67518STörök Edwin 		ret = 0;
19302b67518STörök Edwin 	pagefault_enable();
19402b67518STörök Edwin 
19502b67518STörök Edwin 	return ret;
19602b67518STörök Edwin }
19702b67518STörök Edwin 
1988d7c6a96STörök Edwin static inline void __save_stack_trace_user(struct stack_trace *trace)
19902b67518STörök Edwin {
20002b67518STörök Edwin 	const struct pt_regs *regs = task_pt_regs(current);
20102b67518STörök Edwin 	const void __user *fp = (const void __user *)regs->bp;
20202b67518STörök Edwin 
20302b67518STörök Edwin 	if (trace->nr_entries < trace->max_entries)
20402b67518STörök Edwin 		trace->entries[trace->nr_entries++] = regs->ip;
20502b67518STörök Edwin 
20602b67518STörök Edwin 	while (trace->nr_entries < trace->max_entries) {
207c9cf4dbbSFrederic Weisbecker 		struct stack_frame_user frame;
2088d7c6a96STörök Edwin 
20902b67518STörök Edwin 		frame.next_fp = NULL;
2108d7c6a96STörök Edwin 		frame.ret_addr = 0;
21102b67518STörök Edwin 		if (!copy_stack_frame(fp, &frame))
21202b67518STörök Edwin 			break;
21302b67518STörök Edwin 		if ((unsigned long)fp < regs->sp)
21402b67518STörök Edwin 			break;
2158d7c6a96STörök Edwin 		if (frame.ret_addr) {
21602b67518STörök Edwin 			trace->entries[trace->nr_entries++] =
2178d7c6a96STörök Edwin 				frame.ret_addr;
2188d7c6a96STörök Edwin 		}
21902b67518STörök Edwin 		if (fp == frame.next_fp)
22002b67518STörök Edwin 			break;
22102b67518STörök Edwin 		fp = frame.next_fp;
22202b67518STörök Edwin 	}
22302b67518STörök Edwin }
2248d7c6a96STörök Edwin 
2258d7c6a96STörök Edwin void save_stack_trace_user(struct stack_trace *trace)
2268d7c6a96STörök Edwin {
2278d7c6a96STörök Edwin 	/*
2288d7c6a96STörök Edwin 	 * Trace user stack if we are not a kernel thread
2298d7c6a96STörök Edwin 	 */
2308d7c6a96STörök Edwin 	if (current->mm) {
2318d7c6a96STörök Edwin 		__save_stack_trace_user(trace);
2328d7c6a96STörök Edwin 	}
23302b67518STörök Edwin 	if (trace->nr_entries < trace->max_entries)
23402b67518STörök Edwin 		trace->entries[trace->nr_entries++] = ULONG_MAX;
23502b67518STörök Edwin }
236