xref: /linux-6.15/arch/x86/kernel/stacktrace.c (revision 0797a8d0)
1 /*
2  * Stack trace management functions
3  *
4  *  Copyright (C) 2006-2009 Red Hat, Inc., Ingo Molnar <[email protected]>
5  */
6 #include <linux/sched.h>
7 #include <linux/sched/debug.h>
8 #include <linux/sched/task_stack.h>
9 #include <linux/stacktrace.h>
10 #include <linux/export.h>
11 #include <linux/uaccess.h>
12 #include <asm/stacktrace.h>
13 #include <asm/unwind.h>
14 
15 static int save_stack_address(struct stack_trace *trace, unsigned long addr,
16 			      bool nosched)
17 {
18 	if (nosched && in_sched_functions(addr))
19 		return 0;
20 
21 	if (trace->skip > 0) {
22 		trace->skip--;
23 		return 0;
24 	}
25 
26 	if (trace->nr_entries >= trace->max_entries)
27 		return -1;
28 
29 	trace->entries[trace->nr_entries++] = addr;
30 	return 0;
31 }
32 
33 static void noinline __save_stack_trace(struct stack_trace *trace,
34 			       struct task_struct *task, struct pt_regs *regs,
35 			       bool nosched)
36 {
37 	struct unwind_state state;
38 	unsigned long addr;
39 
40 	if (regs)
41 		save_stack_address(trace, regs->ip, nosched);
42 
43 	for (unwind_start(&state, task, regs, NULL); !unwind_done(&state);
44 	     unwind_next_frame(&state)) {
45 		addr = unwind_get_return_address(&state);
46 		if (!addr || save_stack_address(trace, addr, nosched))
47 			break;
48 	}
49 
50 	if (trace->nr_entries < trace->max_entries)
51 		trace->entries[trace->nr_entries++] = ULONG_MAX;
52 }
53 
54 /*
55  * Save stack-backtrace addresses into a stack_trace buffer.
56  */
57 void save_stack_trace(struct stack_trace *trace)
58 {
59 	trace->skip++;
60 	__save_stack_trace(trace, current, NULL, false);
61 }
62 EXPORT_SYMBOL_GPL(save_stack_trace);
63 
64 void save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
65 {
66 	__save_stack_trace(trace, current, regs, false);
67 }
68 
69 void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
70 {
71 	if (!try_get_task_stack(tsk))
72 		return;
73 
74 	if (tsk == current)
75 		trace->skip++;
76 	__save_stack_trace(trace, tsk, NULL, true);
77 
78 	put_task_stack(tsk);
79 }
80 EXPORT_SYMBOL_GPL(save_stack_trace_tsk);
81 
82 #ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
83 
84 #define STACKTRACE_DUMP_ONCE(task) ({				\
85 	static bool __section(.data.unlikely) __dumped;		\
86 								\
87 	if (!__dumped) {					\
88 		__dumped = true;				\
89 		WARN_ON(1);					\
90 		show_stack(task, NULL);				\
91 	}							\
92 })
93 
94 static int __always_inline
95 __save_stack_trace_reliable(struct stack_trace *trace,
96 			    struct task_struct *task)
97 {
98 	struct unwind_state state;
99 	struct pt_regs *regs;
100 	unsigned long addr;
101 
102 	for (unwind_start(&state, task, NULL, NULL); !unwind_done(&state);
103 	     unwind_next_frame(&state)) {
104 
105 		regs = unwind_get_entry_regs(&state, NULL);
106 		if (regs) {
107 			/*
108 			 * Kernel mode registers on the stack indicate an
109 			 * in-kernel interrupt or exception (e.g., preemption
110 			 * or a page fault), which can make frame pointers
111 			 * unreliable.
112 			 */
113 			if (!user_mode(regs))
114 				return -EINVAL;
115 
116 			break;
117 		}
118 
119 		addr = unwind_get_return_address(&state);
120 
121 		/*
122 		 * A NULL or invalid return address probably means there's some
123 		 * generated code which __kernel_text_address() doesn't know
124 		 * about.
125 		 */
126 		if (!addr) {
127 			STACKTRACE_DUMP_ONCE(task);
128 			return -EINVAL;
129 		}
130 
131 		if (save_stack_address(trace, addr, false))
132 			return -EINVAL;
133 	}
134 
135 	/* Check for stack corruption */
136 	if (unwind_error(&state)) {
137 		STACKTRACE_DUMP_ONCE(task);
138 		return -EINVAL;
139 	}
140 
141 	if (trace->nr_entries < trace->max_entries)
142 		trace->entries[trace->nr_entries++] = ULONG_MAX;
143 
144 	return 0;
145 }
146 
147 /*
148  * This function returns an error if it detects any unreliable features of the
149  * stack.  Otherwise it guarantees that the stack trace is reliable.
150  *
151  * If the task is not 'current', the caller *must* ensure the task is inactive.
152  */
153 int save_stack_trace_tsk_reliable(struct task_struct *tsk,
154 				  struct stack_trace *trace)
155 {
156 	int ret;
157 
158 	/*
159 	 * If the task doesn't have a stack (e.g., a zombie), the stack is
160 	 * "reliably" empty.
161 	 */
162 	if (!try_get_task_stack(tsk))
163 		return 0;
164 
165 	ret = __save_stack_trace_reliable(trace, tsk);
166 
167 	put_task_stack(tsk);
168 
169 	return ret;
170 }
171 #endif /* CONFIG_HAVE_RELIABLE_STACKTRACE */
172 
173 /* Userspace stacktrace - based on kernel/trace/trace_sysprof.c */
174 
175 struct stack_frame_user {
176 	const void __user	*next_fp;
177 	unsigned long		ret_addr;
178 };
179 
180 static int
181 copy_stack_frame(const void __user *fp, struct stack_frame_user *frame)
182 {
183 	int ret;
184 
185 	if (!access_ok(VERIFY_READ, fp, sizeof(*frame)))
186 		return 0;
187 
188 	ret = 1;
189 	pagefault_disable();
190 	if (__copy_from_user_inatomic(frame, fp, sizeof(*frame)))
191 		ret = 0;
192 	pagefault_enable();
193 
194 	return ret;
195 }
196 
197 static inline void __save_stack_trace_user(struct stack_trace *trace)
198 {
199 	const struct pt_regs *regs = task_pt_regs(current);
200 	const void __user *fp = (const void __user *)regs->bp;
201 
202 	if (trace->nr_entries < trace->max_entries)
203 		trace->entries[trace->nr_entries++] = regs->ip;
204 
205 	while (trace->nr_entries < trace->max_entries) {
206 		struct stack_frame_user frame;
207 
208 		frame.next_fp = NULL;
209 		frame.ret_addr = 0;
210 		if (!copy_stack_frame(fp, &frame))
211 			break;
212 		if ((unsigned long)fp < regs->sp)
213 			break;
214 		if (frame.ret_addr) {
215 			trace->entries[trace->nr_entries++] =
216 				frame.ret_addr;
217 		}
218 		if (fp == frame.next_fp)
219 			break;
220 		fp = frame.next_fp;
221 	}
222 }
223 
224 void save_stack_trace_user(struct stack_trace *trace)
225 {
226 	/*
227 	 * Trace user stack if we are not a kernel thread
228 	 */
229 	if (current->mm) {
230 		__save_stack_trace_user(trace);
231 	}
232 	if (trace->nr_entries < trace->max_entries)
233 		trace->entries[trace->nr_entries++] = ULONG_MAX;
234 }
235