mm: move MAP_SYNC to asm-generic/mman-common.h
[linux-2.6-block.git] / kernel / stacktrace.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
8637c099
IM
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 */
214d8ca6
TG
9#include <linux/sched/task_stack.h>
10#include <linux/sched/debug.h>
8637c099 11#include <linux/sched.h>
9212ddb5 12#include <linux/kernel.h>
9984de1a 13#include <linux/export.h>
8637c099
IM
14#include <linux/kallsyms.h>
15#include <linux/stacktrace.h>
16
e9b98e16
TG
17/**
18 * stack_trace_print - Print the entries in the stack trace
19 * @entries: Pointer to storage array
20 * @nr_entries: Number of entries in the storage array
21 * @spaces: Number of leading spaces to print
22 */
23void stack_trace_print(unsigned long *entries, unsigned int nr_entries,
24 int spaces)
8637c099 25{
e9b98e16 26 unsigned int i;
8637c099 27
e9b98e16 28 if (WARN_ON(!entries))
bfeeeeb9
JB
29 return;
30
e9b98e16
TG
31 for (i = 0; i < nr_entries; i++)
32 printk("%*c%pS\n", 1 + spaces, ' ', (void *)entries[i]);
33}
34EXPORT_SYMBOL_GPL(stack_trace_print);
35
e9b98e16
TG
36/**
37 * stack_trace_snprint - Print the entries in the stack trace into a buffer
38 * @buf: Pointer to the print buffer
39 * @size: Size of the print buffer
40 * @entries: Pointer to storage array
41 * @nr_entries: Number of entries in the storage array
42 * @spaces: Number of leading spaces to print
43 *
44 * Return: Number of bytes printed.
45 */
46int stack_trace_snprint(char *buf, size_t size, unsigned long *entries,
47 unsigned int nr_entries, int spaces)
9a92a6ce 48{
e9b98e16 49 unsigned int generated, i, total = 0;
9a92a6ce 50
e9b98e16 51 if (WARN_ON(!entries))
9a92a6ce
JK
52 return 0;
53
e9b98e16 54 for (i = 0; i < nr_entries && size; i++) {
bfeda41d 55 generated = snprintf(buf, size, "%*c%pS\n", 1 + spaces, ' ',
e9b98e16 56 (void *)entries[i]);
9a92a6ce
JK
57
58 total += generated;
9a92a6ce
JK
59 if (generated >= size) {
60 buf += size;
61 size = 0;
62 } else {
63 buf += generated;
64 size -= generated;
65 }
66 }
67
68 return total;
69}
e9b98e16
TG
70EXPORT_SYMBOL_GPL(stack_trace_snprint);
71
214d8ca6
TG
72#ifdef CONFIG_ARCH_STACKWALK
73
74struct stacktrace_cookie {
75 unsigned long *store;
76 unsigned int size;
77 unsigned int skip;
78 unsigned int len;
79};
80
81static bool stack_trace_consume_entry(void *cookie, unsigned long addr,
82 bool reliable)
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
97static bool stack_trace_consume_entry_nosched(void *cookie, unsigned long addr,
98 bool reliable)
99{
100 if (in_sched_functions(addr))
101 return true;
102 return stack_trace_consume_entry(cookie, addr, reliable);
103}
104
105/**
106 * stack_trace_save - Save a stack trace into a storage array
107 * @store: Pointer to storage array
108 * @size: Size of the storage array
109 * @skipnr: Number of entries to skip at the start of the stack trace
110 *
111 * Return: Number of trace entries stored.
112 */
113unsigned int stack_trace_save(unsigned long *store, unsigned int size,
114 unsigned int skipnr)
115{
116 stack_trace_consume_fn consume_entry = stack_trace_consume_entry;
117 struct stacktrace_cookie c = {
118 .store = store,
119 .size = size,
120 .skip = skipnr + 1,
121 };
122
123 arch_stack_walk(consume_entry, &c, current, NULL);
124 return c.len;
125}
126EXPORT_SYMBOL_GPL(stack_trace_save);
127
128/**
129 * stack_trace_save_tsk - Save a task stack trace into a storage array
130 * @task: The task to examine
131 * @store: Pointer to storage array
132 * @size: Size of the storage array
133 * @skipnr: Number of entries to skip at the start of the stack trace
134 *
135 * Return: Number of trace entries stored.
136 */
137unsigned int stack_trace_save_tsk(struct task_struct *tsk, unsigned long *store,
138 unsigned int size, unsigned int skipnr)
139{
140 stack_trace_consume_fn consume_entry = stack_trace_consume_entry_nosched;
141 struct stacktrace_cookie c = {
142 .store = store,
143 .size = size,
144 .skip = skipnr + 1,
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
155/**
156 * stack_trace_save_regs - Save a stack trace based on pt_regs into a storage array
157 * @regs: Pointer to pt_regs to examine
158 * @store: Pointer to storage array
159 * @size: Size of the storage array
160 * @skipnr: Number of entries to skip at the start of the stack trace
161 *
162 * Return: Number of trace entries stored.
163 */
164unsigned int stack_trace_save_regs(struct pt_regs *regs, unsigned long *store,
165 unsigned int size, unsigned int skipnr)
166{
167 stack_trace_consume_fn consume_entry = stack_trace_consume_entry;
168 struct stacktrace_cookie c = {
169 .store = store,
170 .size = size,
171 .skip = skipnr,
172 };
173
174 arch_stack_walk(consume_entry, &c, current, regs);
175 return c.len;
176}
177
178#ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
179/**
180 * stack_trace_save_tsk_reliable - Save task stack with verification
181 * @tsk: Pointer to the task to examine
182 * @store: Pointer to storage array
183 * @size: Size of the storage array
184 *
185 * Return: An error if it detects any unreliable features of the
186 * stack. Otherwise it guarantees that the stack trace is
187 * reliable and returns the number of entries stored.
188 *
189 * If the task is not 'current', the caller *must* ensure the task is inactive.
190 */
191int stack_trace_save_tsk_reliable(struct task_struct *tsk, unsigned long *store,
192 unsigned int size)
193{
194 stack_trace_consume_fn consume_entry = stack_trace_consume_entry;
195 struct stacktrace_cookie c = {
196 .store = store,
197 .size = size,
198 };
199 int ret;
200
201 /*
202 * If the task doesn't have a stack (e.g., a zombie), the stack is
203 * "reliably" empty.
204 */
205 if (!try_get_task_stack(tsk))
206 return 0;
207
208 ret = arch_stack_walk_reliable(consume_entry, &c, tsk);
209 put_task_stack(tsk);
7eaf51a2 210 return ret ? ret : c.len;
214d8ca6
TG
211}
212#endif
213
214#ifdef CONFIG_USER_STACKTRACE_SUPPORT
215/**
216 * stack_trace_save_user - Save a user space stack trace into a storage array
217 * @store: Pointer to storage array
218 * @size: Size of the storage array
219 *
220 * Return: Number of trace entries stored.
221 */
222unsigned int stack_trace_save_user(unsigned long *store, unsigned int size)
223{
224 stack_trace_consume_fn consume_entry = stack_trace_consume_entry;
225 struct stacktrace_cookie c = {
226 .store = store,
227 .size = size,
228 };
229
230 /* Trace user stack if not a kernel thread */
7e8e6816 231 if (current->flags & PF_KTHREAD)
214d8ca6
TG
232 return 0;
233
234 arch_stack_walk_user(consume_entry, &c, task_pt_regs(current));
235 return c.len;
236}
237#endif
238
239#else /* CONFIG_ARCH_STACKWALK */
240
9212ddb5 241/*
af085d90
JP
242 * Architectures that do not implement save_stack_trace_*()
243 * get these weak aliases and once-per-bootup warnings
c624d33f 244 * (whenever this facility is utilized - for example by procfs):
9212ddb5
IM
245 */
246__weak void
247save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
248{
249 WARN_ONCE(1, KERN_INFO "save_stack_trace_tsk() not implemented yet.\n");
250}
c624d33f
MH
251
252__weak void
253save_stack_trace_regs(struct pt_regs *regs, struct stack_trace *trace)
254{
255 WARN_ONCE(1, KERN_INFO "save_stack_trace_regs() not implemented yet.\n");
256}
af085d90 257
e9b98e16
TG
258/**
259 * stack_trace_save - Save a stack trace into a storage array
260 * @store: Pointer to storage array
261 * @size: Size of the storage array
262 * @skipnr: Number of entries to skip at the start of the stack trace
263 *
264 * Return: Number of trace entries stored
265 */
266unsigned int stack_trace_save(unsigned long *store, unsigned int size,
267 unsigned int skipnr)
268{
269 struct stack_trace trace = {
270 .entries = store,
271 .max_entries = size,
272 .skip = skipnr + 1,
273 };
274
275 save_stack_trace(&trace);
276 return trace.nr_entries;
277}
278EXPORT_SYMBOL_GPL(stack_trace_save);
279
280/**
281 * stack_trace_save_tsk - Save a task stack trace into a storage array
282 * @task: The task to examine
283 * @store: Pointer to storage array
284 * @size: Size of the storage array
285 * @skipnr: Number of entries to skip at the start of the stack trace
286 *
287 * Return: Number of trace entries stored
288 */
289unsigned int stack_trace_save_tsk(struct task_struct *task,
290 unsigned long *store, unsigned int size,
291 unsigned int skipnr)
292{
293 struct stack_trace trace = {
294 .entries = store,
295 .max_entries = size,
296 .skip = skipnr + 1,
297 };
298
299 save_stack_trace_tsk(task, &trace);
300 return trace.nr_entries;
301}
302
303/**
304 * stack_trace_save_regs - Save a stack trace based on pt_regs into a storage array
305 * @regs: Pointer to pt_regs to examine
306 * @store: Pointer to storage array
307 * @size: Size of the storage array
308 * @skipnr: Number of entries to skip at the start of the stack trace
309 *
310 * Return: Number of trace entries stored
311 */
312unsigned int stack_trace_save_regs(struct pt_regs *regs, unsigned long *store,
313 unsigned int size, unsigned int skipnr)
314{
315 struct stack_trace trace = {
316 .entries = store,
317 .max_entries = size,
318 .skip = skipnr,
319 };
320
321 save_stack_trace_regs(regs, &trace);
322 return trace.nr_entries;
323}
324
325#ifdef CONFIG_HAVE_RELIABLE_STACKTRACE
326/**
327 * stack_trace_save_tsk_reliable - Save task stack with verification
328 * @tsk: Pointer to the task to examine
329 * @store: Pointer to storage array
330 * @size: Size of the storage array
331 *
332 * Return: An error if it detects any unreliable features of the
333 * stack. Otherwise it guarantees that the stack trace is
334 * reliable and returns the number of entries stored.
335 *
336 * If the task is not 'current', the caller *must* ensure the task is inactive.
337 */
338int stack_trace_save_tsk_reliable(struct task_struct *tsk, unsigned long *store,
339 unsigned int size)
340{
341 struct stack_trace trace = {
342 .entries = store,
343 .max_entries = size,
344 };
345 int ret = save_stack_trace_tsk_reliable(tsk, &trace);
346
347 return ret ? ret : trace.nr_entries;
348}
349#endif
350
351#ifdef CONFIG_USER_STACKTRACE_SUPPORT
352/**
353 * stack_trace_save_user - Save a user space stack trace into a storage array
354 * @store: Pointer to storage array
355 * @size: Size of the storage array
356 *
357 * Return: Number of trace entries stored
358 */
359unsigned int stack_trace_save_user(unsigned long *store, unsigned int size)
360{
361 struct stack_trace trace = {
362 .entries = store,
363 .max_entries = size,
364 };
365
366 save_stack_trace_user(&trace);
367 return trace.nr_entries;
368}
369#endif /* CONFIG_USER_STACKTRACE_SUPPORT */
214d8ca6
TG
370
371#endif /* !CONFIG_ARCH_STACKWALK */