1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * kernel/stacktrace.c
4 *
5 * Stack trace management functions
6 *
7 * Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
8 */
9 #include <linux/sched/task_stack.h>
10 #include <linux/sched/debug.h>
11 #include <linux/sched.h>
12 #include <linux/kernel.h>
13 #include <linux/export.h>
14 #include <linux/kallsyms.h>
15 #include <linux/stacktrace.h>
16 #include <linux/interrupt.h>
17
18 /**
19 * stack_trace_print - Print the entries in the stack trace
20 * @entries: Pointer to storage array
21 * @nr_entries: Number of entries in the storage array
22 * @spaces: Number of leading spaces to print
23 */
stack_trace_print(const unsigned long * entries,unsigned int nr_entries,int spaces)24 void stack_trace_print(const unsigned long *entries, unsigned int nr_entries,
25 int spaces)
26 {
27 unsigned int i;
28
29 if (WARN_ON(!entries))
30 return;
31
32 for (i = 0; i < nr_entries; i++)
33 printk("%*c%pS\n", 1 + spaces, ' ', (void *)entries[i]);
34 }
35 EXPORT_SYMBOL_GPL(stack_trace_print);
36
37 /**
38 * stack_trace_snprint - Print the entries in the stack trace into a buffer
39 * @buf: Pointer to the print buffer
40 * @size: Size of the print buffer
41 * @entries: Pointer to storage array
42 * @nr_entries: Number of entries in the storage array
43 * @spaces: Number of leading spaces to print
44 *
45 * Return: Number of bytes printed.
46 */
stack_trace_snprint(char * buf,size_t size,const unsigned long * entries,unsigned int nr_entries,int spaces)47 int stack_trace_snprint(char *buf, size_t size, const unsigned long *entries,
48 unsigned int nr_entries, int spaces)
49 {
50 unsigned int generated, i, total = 0;
51
52 if (WARN_ON(!entries))
53 return 0;
54
55 for (i = 0; i < nr_entries && size; i++) {
56 generated = snprintf(buf, size, "%*c%pS\n", 1 + spaces, ' ',
57 (void *)entries[i]);
58
59 total += generated;
60 if (generated >= size) {
61 buf += size;
62 size = 0;
63 } else {
64 buf += generated;
65 size -= generated;
66 }
67 }
68
69 return total;
70 }
71 EXPORT_SYMBOL_GPL(stack_trace_snprint);
72
73 #ifdef CONFIG_ARCH_STACKWALK
74
75 struct stacktrace_cookie {
76 unsigned long *store;
77 unsigned int size;
78 unsigned int skip;
79 unsigned int len;
80 };
81
stack_trace_consume_entry(void * cookie,unsigned long addr)82 static bool stack_trace_consume_entry(void *cookie, unsigned long addr)
83 {
84 struct stacktrace_cookie *c = cookie;
85
86 if (c->len >= c->size)
87 return false;
88
89 if (c->skip > 0) {
90 c->skip--;
91 return true;
92 }
93 c->store[c->len++] = addr;
94 return c->len < c->size;
95 }
96
stack_trace_consume_entry_nosched(void * cookie,unsigned long addr)97 static bool stack_trace_consume_entry_nosched(void *cookie, unsigned long addr)
98 {
99 if (in_sched_functions(addr))
100 return true;
101 return stack_trace_consume_entry(cookie, addr);
102 }
103
104 /**
105 * stack_trace_save - Save a stack trace into a storage array
106 * @store: Pointer to storage array
107 * @size: Size of the storage array
108 * @skipnr: Number of entries to skip at the start of the stack trace
109 *
110 * Return: Number of trace entries stored.
111 */
stack_trace_save(unsigned long * store,unsigned int size,unsigned int skipnr)112 unsigned int stack_trace_save(unsigned long *store, unsigned int size,
113 unsigned int skipnr)
114 {
115 stack_trace_consume_fn consume_entry = stack_trace_consume_entry;
116 struct stacktrace_cookie c = {
117 .store = store,
118 .size = size,
119 .skip = skipnr + 1,
120 };
121
122 arch_stack_walk(consume_entry, &c, current, NULL);
123 return c.len;
124 }
125 EXPORT_SYMBOL_GPL(stack_trace_save);
126
127 /**
128 * stack_trace_save_tsk - Save a task stack trace into a storage array
129 * @task: The task to examine
130 * @store: Pointer to storage array
131 * @size: Size of the storage array
132 * @skipnr: Number of entries to skip at the start of the stack trace
133 *
134 * Return: Number of trace entries stored.
135 */
stack_trace_save_tsk(struct task_struct * tsk,unsigned long * store,unsigned int size,unsigned int skipnr)136 unsigned int stack_trace_save_tsk(struct task_struct *tsk, unsigned long *store,
137 unsigned int size, unsigned int skipnr)
138 {
139 stack_trace_consume_fn consume_entry = stack_trace_consume_entry_nosched;
140 struct stacktrace_cookie c = {
141 .store = store,
142 .size = size,
143 /* skip this function if they are tracing us */
144 .skip = skipnr + (current == tsk),
145 };
146
147 if (!try_get_task_stack(tsk))
148 return 0;
149
150 arch_stack_walk(consume_entry, &c, tsk, NULL);
151 put_task_stack(tsk);
152 return c.len;
153 }
154 EXPORT_SYMBOL_GPL(stack_trace_save_tsk);
155
156 /**
157 * stack_trace_save_regs - Save a stack trace based on pt_regs into a storage array
158 * @regs: Pointer to pt_regs to examine
159 * @store: Pointer to storage array
160 * @size: Size of the storage array
161 * @skipnr: Number of entries to skip at the start of the stack trace
162 *
163 * Return: Number of trace entries stored.
164 */
stack_trace_save_regs(struct pt_regs * regs,unsigned long * store,unsigned int size,unsigned int skipnr)165 unsigned int stack_trace_save_regs(struct pt_regs *regs, unsigned long *store,
166 unsigned int size, unsigned int skipnr)
167 {
168 stack_trace_consume_fn consume_entry = stack_trace_consume_entry;
169 struct stacktrace_cookie c = {
170 .store = store,
171 .size = size,
172 .skip = skipnr,
173 };
174
175 arch_stack_walk(consume_entry, &c, current, regs);
176 return c.len;
177 }
178 EXPORT_SYMBOL_GPL(stack_trace_save_regs);
179
180 #ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
181 /**
182 * stack_trace_save_tsk_reliable - Save task stack with verification
183 * @tsk: Pointer to the task to examine
184 * @store: Pointer to storage array
185 * @size: Size of the storage array
186 *
187 * Return: An error if it detects any unreliable features of the
188 * stack. Otherwise it guarantees that the stack trace is
189 * reliable and returns the number of entries stored.
190 *
191 * If the task is not 'current', the caller *must* ensure the task is inactive.
192 */
stack_trace_save_tsk_reliable(struct task_struct * tsk,unsigned long * store,unsigned int size)193 int stack_trace_save_tsk_reliable(struct task_struct *tsk, unsigned long *store,
194 unsigned int size)
195 {
196 stack_trace_consume_fn consume_entry = stack_trace_consume_entry;
197 struct stacktrace_cookie c = {
198 .store = store,
199 .size = size,
200 };
201 int ret;
202
203 /*
204 * If the task doesn't have a stack (e.g., a zombie), the stack is
205 * "reliably" empty.
206 */
207 if (!try_get_task_stack(tsk))
208 return 0;
209
210 ret = arch_stack_walk_reliable(consume_entry, &c, tsk);
211 put_task_stack(tsk);
212 return ret ? ret : c.len;
213 }
214 #endif
215
216 #ifdef CONFIG_USER_STACKTRACE_SUPPORT
217 /**
218 * stack_trace_save_user - Save a user space stack trace into a storage array
219 * @store: Pointer to storage array
220 * @size: Size of the storage array
221 *
222 * Return: Number of trace entries stored.
223 */
stack_trace_save_user(unsigned long * store,unsigned int size)224 unsigned int stack_trace_save_user(unsigned long *store, unsigned int size)
225 {
226 stack_trace_consume_fn consume_entry = stack_trace_consume_entry;
227 struct stacktrace_cookie c = {
228 .store = store,
229 .size = size,
230 };
231
232 /* Trace user stack if not a kernel thread */
233 if (current->flags & PF_KTHREAD)
234 return 0;
235
236 arch_stack_walk_user(consume_entry, &c, task_pt_regs(current));
237
238 return c.len;
239 }
240 #endif
241
242 #else /* CONFIG_ARCH_STACKWALK */
243
244 /*
245 * Architectures that do not implement save_stack_trace_*()
246 * get these weak aliases and once-per-bootup warnings
247 * (whenever this facility is utilized - for example by procfs):
248 */
249 __weak void
save_stack_trace_tsk(struct task_struct * tsk,struct stack_trace * trace)250 save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
251 {
252 WARN_ONCE(1, KERN_INFO "save_stack_trace_tsk() not implemented yet.\n");
253 }
254
255 __weak void
save_stack_trace_regs(struct pt_regs * regs,struct stack_trace * trace)256 save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
257 {
258 WARN_ONCE(1, KERN_INFO "save_stack_trace_regs() not implemented yet.\n");
259 }
260
261 /**
262 * stack_trace_save - Save a stack trace into a storage array
263 * @store: Pointer to storage array
264 * @size: Size of the storage array
265 * @skipnr: Number of entries to skip at the start of the stack trace
266 *
267 * Return: Number of trace entries stored
268 */
stack_trace_save(unsigned long * store,unsigned int size,unsigned int skipnr)269 unsigned int stack_trace_save(unsigned long *store, unsigned int size,
270 unsigned int skipnr)
271 {
272 struct stack_trace trace = {
273 .entries = store,
274 .max_entries = size,
275 .skip = skipnr + 1,
276 };
277
278 save_stack_trace(&trace);
279 return trace.nr_entries;
280 }
281 EXPORT_SYMBOL_GPL(stack_trace_save);
282
283 /**
284 * stack_trace_save_tsk - Save a task stack trace into a storage array
285 * @task: The task to examine
286 * @store: Pointer to storage array
287 * @size: Size of the storage array
288 * @skipnr: Number of entries to skip at the start of the stack trace
289 *
290 * Return: Number of trace entries stored
291 */
stack_trace_save_tsk(struct task_struct * task,unsigned long * store,unsigned int size,unsigned int skipnr)292 unsigned int stack_trace_save_tsk(struct task_struct *task,
293 unsigned long *store, unsigned int size,
294 unsigned int skipnr)
295 {
296 struct stack_trace trace = {
297 .entries = store,
298 .max_entries = size,
299 /* skip this function if they are tracing us */
300 .skip = skipnr + (current == task),
301 };
302
303 save_stack_trace_tsk(task, &trace);
304 return trace.nr_entries;
305 }
306
307 /**
308 * stack_trace_save_regs - Save a stack trace based on pt_regs into a storage array
309 * @regs: Pointer to pt_regs to examine
310 * @store: Pointer to storage array
311 * @size: Size of the storage array
312 * @skipnr: Number of entries to skip at the start of the stack trace
313 *
314 * Return: Number of trace entries stored
315 */
stack_trace_save_regs(struct pt_regs * regs,unsigned long * store,unsigned int size,unsigned int skipnr)316 unsigned int stack_trace_save_regs(struct pt_regs *regs, unsigned long *store,
317 unsigned int size, unsigned int skipnr)
318 {
319 struct stack_trace trace = {
320 .entries = store,
321 .max_entries = size,
322 .skip = skipnr,
323 };
324
325 save_stack_trace_regs(regs, &trace);
326 return trace.nr_entries;
327 }
328
329 #ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
330 /**
331 * stack_trace_save_tsk_reliable - Save task stack with verification
332 * @tsk: Pointer to the task to examine
333 * @store: Pointer to storage array
334 * @size: Size of the storage array
335 *
336 * Return: An error if it detects any unreliable features of the
337 * stack. Otherwise it guarantees that the stack trace is
338 * reliable and returns the number of entries stored.
339 *
340 * If the task is not 'current', the caller *must* ensure the task is inactive.
341 */
stack_trace_save_tsk_reliable(struct task_struct * tsk,unsigned long * store,unsigned int size)342 int stack_trace_save_tsk_reliable(struct task_struct *tsk, unsigned long *store,
343 unsigned int size)
344 {
345 struct stack_trace trace = {
346 .entries = store,
347 .max_entries = size,
348 };
349 int ret = save_stack_trace_tsk_reliable(tsk, &trace);
350
351 return ret ? ret : trace.nr_entries;
352 }
353 #endif
354
355 #ifdef CONFIG_USER_STACKTRACE_SUPPORT
356 /**
357 * stack_trace_save_user - Save a user space stack trace into a storage array
358 * @store: Pointer to storage array
359 * @size: Size of the storage array
360 *
361 * Return: Number of trace entries stored
362 */
stack_trace_save_user(unsigned long * store,unsigned int size)363 unsigned int stack_trace_save_user(unsigned long *store, unsigned int size)
364 {
365 struct stack_trace trace = {
366 .entries = store,
367 .max_entries = size,
368 };
369
370 save_stack_trace_user(&trace);
371 return trace.nr_entries;
372 }
373 #endif /* CONFIG_USER_STACKTRACE_SUPPORT */
374
375 #endif /* !CONFIG_ARCH_STACKWALK */
376
in_irqentry_text(unsigned long ptr)377 static inline bool in_irqentry_text(unsigned long ptr)
378 {
379 return (ptr >= (unsigned long)&__irqentry_text_start &&
380 ptr < (unsigned long)&__irqentry_text_end) ||
381 (ptr >= (unsigned long)&__softirqentry_text_start &&
382 ptr < (unsigned long)&__softirqentry_text_end);
383 }
384
385 /**
386 * filter_irq_stacks - Find first IRQ stack entry in trace
387 * @entries: Pointer to stack trace array
388 * @nr_entries: Number of entries in the storage array
389 *
390 * Return: Number of trace entries until IRQ stack starts.
391 */
filter_irq_stacks(unsigned long * entries,unsigned int nr_entries)392 unsigned int filter_irq_stacks(unsigned long *entries, unsigned int nr_entries)
393 {
394 unsigned int i;
395
396 for (i = 0; i < nr_entries; i++) {
397 if (in_irqentry_text(entries[i])) {
398 /* Include the irqentry function into the stack. */
399 return i + 1;
400 }
401 }
402 return nr_entries;
403 }
404 EXPORT_SYMBOL_GPL(filter_irq_stacks);
405