blob: 2b0d1ee3241cb515e441e88389ed1da459cc2c15 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Steven Rostedte5a81b62008-08-27 23:31:01 -04002/*
3 * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
4 *
5 */
Ingo Molnar68db0cf2017-02-08 18:51:37 +01006#include <linux/sched/task_stack.h>
Steven Rostedte5a81b62008-08-27 23:31:01 -04007#include <linux/stacktrace.h>
8#include <linux/kallsyms.h>
9#include <linux/seq_file.h>
10#include <linux/spinlock.h>
11#include <linux/uaccess.h>
Steven Rostedte5a81b62008-08-27 23:31:01 -040012#include <linux/ftrace.h>
13#include <linux/module.h>
Steven Rostedtf38f1d22008-12-16 23:06:40 -050014#include <linux/sysctl.h>
Steven Rostedte5a81b62008-08-27 23:31:01 -040015#include <linux/init.h>
Steven Rostedt762e1202011-12-19 22:01:00 -050016
17#include <asm/setup.h>
18
Steven Rostedte5a81b62008-08-27 23:31:01 -040019#include "trace.h"
20
Steven Rostedt1b6cced2008-08-29 16:51:43 -040021static unsigned long stack_dump_trace[STACK_TRACE_ENTRIES+1] =
22 { [0 ... (STACK_TRACE_ENTRIES)] = ULONG_MAX };
AKASHI Takahirobb99d8c2015-10-30 14:25:39 +090023unsigned stack_trace_index[STACK_TRACE_ENTRIES];
Steven Rostedt1b6cced2008-08-29 16:51:43 -040024
Steven Rostedt (Red Hat)4df29712013-03-13 23:34:22 -040025/*
26 * Reserve one entry for the passed in ip. This will allow
27 * us to remove most or all of the stack size overhead
28 * added by the stack tracer itself.
29 */
AKASHI Takahirobb99d8c2015-10-30 14:25:39 +090030struct stack_trace stack_trace_max = {
Steven Rostedt (Red Hat)4df29712013-03-13 23:34:22 -040031 .max_entries = STACK_TRACE_ENTRIES - 1,
Steven Rostedt (Red Hat)72ac4262015-07-16 13:24:54 -040032 .entries = &stack_dump_trace[0],
Steven Rostedte5a81b62008-08-27 23:31:01 -040033};
34
AKASHI Takahirobb99d8c2015-10-30 14:25:39 +090035unsigned long stack_trace_max_size;
Steven Rostedt (Red Hat)d3327362015-11-03 14:50:15 -050036arch_spinlock_t stack_trace_max_lock =
Thomas Gleixneredc35bd2009-12-03 12:38:57 +010037 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Steven Rostedte5a81b62008-08-27 23:31:01 -040038
Steven Rostedt (VMware)8aaf1ee2017-04-06 15:47:32 -040039DEFINE_PER_CPU(int, disable_stack_tracer);
Steven Rostedtf38f1d22008-12-16 23:06:40 -050040static DEFINE_MUTEX(stack_sysctl_mutex);
41
42int stack_tracer_enabled;
43static int last_stack_tracer_enabled;
Steven Rostedte5a81b62008-08-27 23:31:01 -040044
AKASHI Takahirobb99d8c2015-10-30 14:25:39 +090045void stack_trace_print(void)
Minchan Kime3172182014-06-02 13:33:12 +090046{
47 long i;
48 int size;
49
50 pr_emerg(" Depth Size Location (%d entries)\n"
51 " ----- ---- --------\n",
AKASHI Takahirobb99d8c2015-10-30 14:25:39 +090052 stack_trace_max.nr_entries);
Minchan Kime3172182014-06-02 13:33:12 +090053
AKASHI Takahirobb99d8c2015-10-30 14:25:39 +090054 for (i = 0; i < stack_trace_max.nr_entries; i++) {
Minchan Kime3172182014-06-02 13:33:12 +090055 if (stack_dump_trace[i] == ULONG_MAX)
56 break;
AKASHI Takahirobb99d8c2015-10-30 14:25:39 +090057 if (i+1 == stack_trace_max.nr_entries ||
Minchan Kime3172182014-06-02 13:33:12 +090058 stack_dump_trace[i+1] == ULONG_MAX)
AKASHI Takahirobb99d8c2015-10-30 14:25:39 +090059 size = stack_trace_index[i];
Minchan Kime3172182014-06-02 13:33:12 +090060 else
AKASHI Takahirobb99d8c2015-10-30 14:25:39 +090061 size = stack_trace_index[i] - stack_trace_index[i+1];
Minchan Kime3172182014-06-02 13:33:12 +090062
AKASHI Takahirobb99d8c2015-10-30 14:25:39 +090063 pr_emerg("%3ld) %8d %5d %pS\n", i, stack_trace_index[i],
Minchan Kime3172182014-06-02 13:33:12 +090064 size, (void *)stack_dump_trace[i]);
65 }
66}
67
AKASHI Takahirobb99d8c2015-10-30 14:25:39 +090068/*
Masahiro Yamada505d3082017-03-09 16:16:33 -080069 * When arch-specific code overrides this function, the following
Steven Rostedt (Red Hat)d3327362015-11-03 14:50:15 -050070 * data should be filled up, assuming stack_trace_max_lock is held to
AKASHI Takahirobb99d8c2015-10-30 14:25:39 +090071 * prevent concurrent updates.
72 * stack_trace_index[]
73 * stack_trace_max
74 * stack_trace_max_size
75 */
76void __weak
Steven Rostedt (Red Hat)d4ecbfc2013-03-13 21:25:35 -040077check_stack(unsigned long ip, unsigned long *stack)
Steven Rostedte5a81b62008-08-27 23:31:01 -040078{
Minchan Kime3172182014-06-02 13:33:12 +090079 unsigned long this_size, flags; unsigned long *p, *top, *start;
Steven Rostedt (Red Hat)4df29712013-03-13 23:34:22 -040080 static int tracer_frame;
Mark Rutland6aa7de02017-10-23 14:07:29 -070081 int frame_size = READ_ONCE(tracer_frame);
Steven Rostedt (Red Hat)72ac4262015-07-16 13:24:54 -040082 int i, x;
Steven Rostedte5a81b62008-08-27 23:31:01 -040083
Steven Rostedt (Red Hat)878895012013-03-13 20:43:57 -040084 this_size = ((unsigned long)stack) & (THREAD_SIZE-1);
Steven Rostedte5a81b62008-08-27 23:31:01 -040085 this_size = THREAD_SIZE - this_size;
Steven Rostedt (Red Hat)4df29712013-03-13 23:34:22 -040086 /* Remove the frame of the tracer */
87 this_size -= frame_size;
Steven Rostedte5a81b62008-08-27 23:31:01 -040088
AKASHI Takahirobb99d8c2015-10-30 14:25:39 +090089 if (this_size <= stack_trace_max_size)
Steven Rostedte5a81b62008-08-27 23:31:01 -040090 return;
91
Steven Rostedt81520a12008-10-06 21:24:18 -040092 /* we do not handle interrupt stacks yet */
Steven Rostedt (Red Hat)878895012013-03-13 20:43:57 -040093 if (!object_is_on_stack(stack))
Steven Rostedt81520a12008-10-06 21:24:18 -040094 return;
95
Steven Rostedt (Red Hat)1904be12015-10-20 21:48:02 -040096 /* Can't do this from NMI context (can cause deadlocks) */
97 if (in_nmi())
98 return;
99
Steven Rostedta5e25882008-12-02 15:34:05 -0500100 local_irq_save(flags);
Steven Rostedt (Red Hat)d3327362015-11-03 14:50:15 -0500101 arch_spin_lock(&stack_trace_max_lock);
Steven Rostedte5a81b62008-08-27 23:31:01 -0400102
Steven Rostedt (Red Hat)4df29712013-03-13 23:34:22 -0400103 /* In case another CPU set the tracer_frame on us */
104 if (unlikely(!frame_size))
105 this_size -= tracer_frame;
106
Steven Rostedte5a81b62008-08-27 23:31:01 -0400107 /* a race could have already updated it */
AKASHI Takahirobb99d8c2015-10-30 14:25:39 +0900108 if (this_size <= stack_trace_max_size)
Steven Rostedte5a81b62008-08-27 23:31:01 -0400109 goto out;
110
AKASHI Takahirobb99d8c2015-10-30 14:25:39 +0900111 stack_trace_max_size = this_size;
Steven Rostedte5a81b62008-08-27 23:31:01 -0400112
AKASHI Takahirobb99d8c2015-10-30 14:25:39 +0900113 stack_trace_max.nr_entries = 0;
Steven Rostedt (VMware)a2acce52018-10-15 23:14:28 -0400114 stack_trace_max.skip = 0;
Steven Rostedte5a81b62008-08-27 23:31:01 -0400115
AKASHI Takahirobb99d8c2015-10-30 14:25:39 +0900116 save_stack_trace(&stack_trace_max);
Steven Rostedte5a81b62008-08-27 23:31:01 -0400117
Steven Rostedt (Red Hat)72ac4262015-07-16 13:24:54 -0400118 /* Skip over the overhead of the stack tracer itself */
AKASHI Takahirobb99d8c2015-10-30 14:25:39 +0900119 for (i = 0; i < stack_trace_max.nr_entries; i++) {
Steven Rostedt (Red Hat)72ac4262015-07-16 13:24:54 -0400120 if (stack_dump_trace[i] == ip)
121 break;
122 }
Steven Rostedt (Red Hat)d4ecbfc2013-03-13 21:25:35 -0400123
124 /*
Steven Rostedt6ccd8372016-01-29 10:22:41 -0500125 * Some archs may not have the passed in ip in the dump.
126 * If that happens, we need to show everything.
127 */
128 if (i == stack_trace_max.nr_entries)
129 i = 0;
130
131 /*
Steven Rostedt1b6cced2008-08-29 16:51:43 -0400132 * Now find where in the stack these are.
133 */
Steven Rostedt (Red Hat)72ac4262015-07-16 13:24:54 -0400134 x = 0;
Steven Rostedt (Red Hat)878895012013-03-13 20:43:57 -0400135 start = stack;
Steven Rostedt1b6cced2008-08-29 16:51:43 -0400136 top = (unsigned long *)
137 (((unsigned long)start & ~(THREAD_SIZE-1)) + THREAD_SIZE);
138
139 /*
140 * Loop through all the entries. One of the entries may
141 * for some reason be missed on the stack, so we may
142 * have to account for them. If they are all there, this
143 * loop will only happen once. This code only takes place
144 * on a new max, so it is far from a fast path.
145 */
AKASHI Takahirobb99d8c2015-10-30 14:25:39 +0900146 while (i < stack_trace_max.nr_entries) {
Steven Rostedt0a371192008-12-03 11:04:50 -0500147 int found = 0;
Steven Rostedt1b6cced2008-08-29 16:51:43 -0400148
AKASHI Takahirobb99d8c2015-10-30 14:25:39 +0900149 stack_trace_index[x] = this_size;
Steven Rostedt1b6cced2008-08-29 16:51:43 -0400150 p = start;
151
AKASHI Takahirobb99d8c2015-10-30 14:25:39 +0900152 for (; p < top && i < stack_trace_max.nr_entries; p++) {
Steven Rostedt (Red Hat)72ac4262015-07-16 13:24:54 -0400153 if (stack_dump_trace[i] == ULONG_MAX)
154 break;
Yang Shi6e22c832016-02-12 12:46:00 -0800155 /*
156 * The READ_ONCE_NOCHECK is used to let KASAN know that
157 * this is not a stack-out-of-bounds error.
158 */
159 if ((READ_ONCE_NOCHECK(*p)) == stack_dump_trace[i]) {
Steven Rostedt (Red Hat)72ac4262015-07-16 13:24:54 -0400160 stack_dump_trace[x] = stack_dump_trace[i++];
AKASHI Takahirobb99d8c2015-10-30 14:25:39 +0900161 this_size = stack_trace_index[x++] =
Steven Rostedt1b6cced2008-08-29 16:51:43 -0400162 (top - p) * sizeof(unsigned long);
Steven Rostedt0a371192008-12-03 11:04:50 -0500163 found = 1;
Steven Rostedt1b6cced2008-08-29 16:51:43 -0400164 /* Start the search from here */
165 start = p + 1;
Steven Rostedt (Red Hat)4df29712013-03-13 23:34:22 -0400166 /*
167 * We do not want to show the overhead
168 * of the stack tracer stack in the
169 * max stack. If we haven't figured
170 * out what that is, then figure it out
171 * now.
172 */
Steven Rostedt (Red Hat)72ac4262015-07-16 13:24:54 -0400173 if (unlikely(!tracer_frame)) {
Steven Rostedt (Red Hat)4df29712013-03-13 23:34:22 -0400174 tracer_frame = (p - stack) *
175 sizeof(unsigned long);
AKASHI Takahirobb99d8c2015-10-30 14:25:39 +0900176 stack_trace_max_size -= tracer_frame;
Steven Rostedt (Red Hat)4df29712013-03-13 23:34:22 -0400177 }
Steven Rostedt1b6cced2008-08-29 16:51:43 -0400178 }
179 }
180
Steven Rostedt0a371192008-12-03 11:04:50 -0500181 if (!found)
182 i++;
Steven Rostedt1b6cced2008-08-29 16:51:43 -0400183 }
184
AKASHI Takahirobb99d8c2015-10-30 14:25:39 +0900185 stack_trace_max.nr_entries = x;
Steven Rostedt (Red Hat)72ac4262015-07-16 13:24:54 -0400186 for (; x < i; x++)
187 stack_dump_trace[x] = ULONG_MAX;
188
Aaron Tomlina70857e2014-09-12 14:16:18 +0100189 if (task_stack_end_corrupted(current)) {
AKASHI Takahirobb99d8c2015-10-30 14:25:39 +0900190 stack_trace_print();
Minchan Kime3172182014-06-02 13:33:12 +0900191 BUG();
192 }
193
Steven Rostedte5a81b62008-08-27 23:31:01 -0400194 out:
Steven Rostedt (Red Hat)d3327362015-11-03 14:50:15 -0500195 arch_spin_unlock(&stack_trace_max_lock);
Steven Rostedta5e25882008-12-02 15:34:05 -0500196 local_irq_restore(flags);
Steven Rostedte5a81b62008-08-27 23:31:01 -0400197}
198
199static void
Steven Rostedta1e2e312011-08-09 12:50:46 -0400200stack_trace_call(unsigned long ip, unsigned long parent_ip,
201 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedte5a81b62008-08-27 23:31:01 -0400202{
Steven Rostedt (Red Hat)878895012013-03-13 20:43:57 -0400203 unsigned long stack;
Steven Rostedte5a81b62008-08-27 23:31:01 -0400204
Steven Rostedt5168ae52010-06-03 09:36:50 -0400205 preempt_disable_notrace();
Steven Rostedte5a81b62008-08-27 23:31:01 -0400206
Steven Rostedte5a81b62008-08-27 23:31:01 -0400207 /* no atomic needed, we only modify this variable by this cpu */
Steven Rostedt (VMware)8aaf1ee2017-04-06 15:47:32 -0400208 __this_cpu_inc(disable_stack_tracer);
209 if (__this_cpu_read(disable_stack_tracer) != 1)
Steven Rostedte5a81b62008-08-27 23:31:01 -0400210 goto out;
211
Steven Rostedt (VMware)b00d6072017-12-05 04:41:51 -0500212 /* If rcu is not watching, then save stack trace can fail */
213 if (!rcu_is_watching())
214 goto out;
215
Steven Rostedt (Red Hat)72ac4262015-07-16 13:24:54 -0400216 ip += MCOUNT_INSN_SIZE;
Steven Rostedt (Red Hat)4df29712013-03-13 23:34:22 -0400217
218 check_stack(ip, &stack);
Steven Rostedte5a81b62008-08-27 23:31:01 -0400219
220 out:
Steven Rostedt (VMware)8aaf1ee2017-04-06 15:47:32 -0400221 __this_cpu_dec(disable_stack_tracer);
Steven Rostedte5a81b62008-08-27 23:31:01 -0400222 /* prevent recursion in schedule */
Steven Rostedt5168ae52010-06-03 09:36:50 -0400223 preempt_enable_notrace();
Steven Rostedte5a81b62008-08-27 23:31:01 -0400224}
225
226static struct ftrace_ops trace_ops __read_mostly =
227{
228 .func = stack_trace_call,
Steven Rostedt47409742012-07-20 11:04:44 -0400229 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedte5a81b62008-08-27 23:31:01 -0400230};
231
232static ssize_t
233stack_max_size_read(struct file *filp, char __user *ubuf,
234 size_t count, loff_t *ppos)
235{
236 unsigned long *ptr = filp->private_data;
237 char buf[64];
238 int r;
239
240 r = snprintf(buf, sizeof(buf), "%ld\n", *ptr);
241 if (r > sizeof(buf))
242 r = sizeof(buf);
243 return simple_read_from_buffer(ubuf, count, ppos, buf, r);
244}
245
246static ssize_t
247stack_max_size_write(struct file *filp, const char __user *ubuf,
248 size_t count, loff_t *ppos)
249{
250 long *ptr = filp->private_data;
251 unsigned long val, flags;
Steven Rostedte5a81b62008-08-27 23:31:01 -0400252 int ret;
253
Peter Huewe22fe9b52011-06-07 21:58:27 +0200254 ret = kstrtoul_from_user(ubuf, count, 10, &val);
255 if (ret)
Steven Rostedte5a81b62008-08-27 23:31:01 -0400256 return ret;
257
Steven Rostedta5e25882008-12-02 15:34:05 -0500258 local_irq_save(flags);
Lai Jiangshan4f48f8b2010-02-02 15:32:09 +0800259
260 /*
261 * In case we trace inside arch_spin_lock() or after (NMI),
262 * we will cause circular lock, so we also need to increase
Steven Rostedt (VMware)8aaf1ee2017-04-06 15:47:32 -0400263 * the percpu disable_stack_tracer here.
Lai Jiangshan4f48f8b2010-02-02 15:32:09 +0800264 */
Steven Rostedt (VMware)8aaf1ee2017-04-06 15:47:32 -0400265 __this_cpu_inc(disable_stack_tracer);
Lai Jiangshan4f48f8b2010-02-02 15:32:09 +0800266
Steven Rostedt (Red Hat)d3327362015-11-03 14:50:15 -0500267 arch_spin_lock(&stack_trace_max_lock);
Steven Rostedte5a81b62008-08-27 23:31:01 -0400268 *ptr = val;
Steven Rostedt (Red Hat)d3327362015-11-03 14:50:15 -0500269 arch_spin_unlock(&stack_trace_max_lock);
Lai Jiangshan4f48f8b2010-02-02 15:32:09 +0800270
Steven Rostedt (VMware)8aaf1ee2017-04-06 15:47:32 -0400271 __this_cpu_dec(disable_stack_tracer);
Steven Rostedta5e25882008-12-02 15:34:05 -0500272 local_irq_restore(flags);
Steven Rostedte5a81b62008-08-27 23:31:01 -0400273
274 return count;
275}
276
Steven Rostedtf38f1d22008-12-16 23:06:40 -0500277static const struct file_operations stack_max_size_fops = {
Steven Rostedte5a81b62008-08-27 23:31:01 -0400278 .open = tracing_open_generic,
279 .read = stack_max_size_read,
280 .write = stack_max_size_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200281 .llseek = default_llseek,
Steven Rostedte5a81b62008-08-27 23:31:01 -0400282};
283
284static void *
Li Zefan2fc5f0c2009-08-17 16:53:37 +0800285__next(struct seq_file *m, loff_t *pos)
Steven Rostedte5a81b62008-08-27 23:31:01 -0400286{
Li Zefan2fc5f0c2009-08-17 16:53:37 +0800287 long n = *pos - 1;
Steven Rostedte5a81b62008-08-27 23:31:01 -0400288
AKASHI Takahirobb99d8c2015-10-30 14:25:39 +0900289 if (n > stack_trace_max.nr_entries || stack_dump_trace[n] == ULONG_MAX)
Steven Rostedte5a81b62008-08-27 23:31:01 -0400290 return NULL;
291
Li Zefan2fc5f0c2009-08-17 16:53:37 +0800292 m->private = (void *)n;
Steven Rostedt1b6cced2008-08-29 16:51:43 -0400293 return &m->private;
Steven Rostedte5a81b62008-08-27 23:31:01 -0400294}
295
Li Zefan2fc5f0c2009-08-17 16:53:37 +0800296static void *
297t_next(struct seq_file *m, void *v, loff_t *pos)
298{
299 (*pos)++;
300 return __next(m, pos);
301}
302
Steven Rostedte5a81b62008-08-27 23:31:01 -0400303static void *t_start(struct seq_file *m, loff_t *pos)
304{
Steven Rostedte5a81b62008-08-27 23:31:01 -0400305 local_irq_disable();
Lai Jiangshan4f48f8b2010-02-02 15:32:09 +0800306
Steven Rostedt (VMware)8aaf1ee2017-04-06 15:47:32 -0400307 __this_cpu_inc(disable_stack_tracer);
Lai Jiangshan4f48f8b2010-02-02 15:32:09 +0800308
Steven Rostedt (Red Hat)d3327362015-11-03 14:50:15 -0500309 arch_spin_lock(&stack_trace_max_lock);
Steven Rostedte5a81b62008-08-27 23:31:01 -0400310
Liming Wang522a1102008-11-21 11:00:18 +0800311 if (*pos == 0)
312 return SEQ_START_TOKEN;
313
Li Zefan2fc5f0c2009-08-17 16:53:37 +0800314 return __next(m, pos);
Steven Rostedte5a81b62008-08-27 23:31:01 -0400315}
316
317static void t_stop(struct seq_file *m, void *p)
318{
Steven Rostedt (Red Hat)d3327362015-11-03 14:50:15 -0500319 arch_spin_unlock(&stack_trace_max_lock);
Lai Jiangshan4f48f8b2010-02-02 15:32:09 +0800320
Steven Rostedt (VMware)8aaf1ee2017-04-06 15:47:32 -0400321 __this_cpu_dec(disable_stack_tracer);
Lai Jiangshan4f48f8b2010-02-02 15:32:09 +0800322
Steven Rostedte5a81b62008-08-27 23:31:01 -0400323 local_irq_enable();
324}
325
Joe Perches962e3702015-04-15 16:18:22 -0700326static void trace_lookup_stack(struct seq_file *m, long i)
Steven Rostedte5a81b62008-08-27 23:31:01 -0400327{
Steven Rostedt1b6cced2008-08-29 16:51:43 -0400328 unsigned long addr = stack_dump_trace[i];
Steven Rostedte5a81b62008-08-27 23:31:01 -0400329
Joe Perches962e3702015-04-15 16:18:22 -0700330 seq_printf(m, "%pS\n", (void *)addr);
Steven Rostedte5a81b62008-08-27 23:31:01 -0400331}
332
Steven Rostedte447e1df2009-03-12 19:42:29 -0400333static void print_disabled(struct seq_file *m)
334{
335 seq_puts(m, "#\n"
336 "# Stack tracer disabled\n"
337 "#\n"
338 "# To enable the stack tracer, either add 'stacktrace' to the\n"
339 "# kernel command line\n"
340 "# or 'echo 1 > /proc/sys/kernel/stack_tracer_enabled'\n"
341 "#\n");
342}
343
Steven Rostedte5a81b62008-08-27 23:31:01 -0400344static int t_show(struct seq_file *m, void *v)
345{
Liming Wang522a1102008-11-21 11:00:18 +0800346 long i;
Steven Rostedt1b6cced2008-08-29 16:51:43 -0400347 int size;
Steven Rostedte5a81b62008-08-27 23:31:01 -0400348
Liming Wang522a1102008-11-21 11:00:18 +0800349 if (v == SEQ_START_TOKEN) {
Steven Rostedteb1871f2009-03-13 00:00:58 -0400350 seq_printf(m, " Depth Size Location"
Steven Rostedt1b6cced2008-08-29 16:51:43 -0400351 " (%d entries)\n"
Steven Rostedteb1871f2009-03-13 00:00:58 -0400352 " ----- ---- --------\n",
AKASHI Takahirobb99d8c2015-10-30 14:25:39 +0900353 stack_trace_max.nr_entries);
Steven Rostedte447e1df2009-03-12 19:42:29 -0400354
AKASHI Takahirobb99d8c2015-10-30 14:25:39 +0900355 if (!stack_tracer_enabled && !stack_trace_max_size)
Steven Rostedte447e1df2009-03-12 19:42:29 -0400356 print_disabled(m);
357
Steven Rostedt1b6cced2008-08-29 16:51:43 -0400358 return 0;
359 }
360
Liming Wang522a1102008-11-21 11:00:18 +0800361 i = *(long *)v;
362
AKASHI Takahirobb99d8c2015-10-30 14:25:39 +0900363 if (i >= stack_trace_max.nr_entries ||
Steven Rostedt1b6cced2008-08-29 16:51:43 -0400364 stack_dump_trace[i] == ULONG_MAX)
Steven Rostedte5a81b62008-08-27 23:31:01 -0400365 return 0;
366
AKASHI Takahirobb99d8c2015-10-30 14:25:39 +0900367 if (i+1 == stack_trace_max.nr_entries ||
Steven Rostedt1b6cced2008-08-29 16:51:43 -0400368 stack_dump_trace[i+1] == ULONG_MAX)
AKASHI Takahirobb99d8c2015-10-30 14:25:39 +0900369 size = stack_trace_index[i];
Steven Rostedt1b6cced2008-08-29 16:51:43 -0400370 else
AKASHI Takahirobb99d8c2015-10-30 14:25:39 +0900371 size = stack_trace_index[i] - stack_trace_index[i+1];
Steven Rostedt1b6cced2008-08-29 16:51:43 -0400372
AKASHI Takahirobb99d8c2015-10-30 14:25:39 +0900373 seq_printf(m, "%3ld) %8d %5d ", i, stack_trace_index[i], size);
Steven Rostedt1b6cced2008-08-29 16:51:43 -0400374
375 trace_lookup_stack(m, i);
Steven Rostedte5a81b62008-08-27 23:31:01 -0400376
377 return 0;
378}
379
Steven Rostedtf38f1d22008-12-16 23:06:40 -0500380static const struct seq_operations stack_trace_seq_ops = {
Steven Rostedte5a81b62008-08-27 23:31:01 -0400381 .start = t_start,
382 .next = t_next,
383 .stop = t_stop,
384 .show = t_show,
385};
386
387static int stack_trace_open(struct inode *inode, struct file *file)
388{
Li Zefand8cc1ab2009-07-23 11:28:40 +0800389 return seq_open(file, &stack_trace_seq_ops);
Steven Rostedte5a81b62008-08-27 23:31:01 -0400390}
391
Steven Rostedtf38f1d22008-12-16 23:06:40 -0500392static const struct file_operations stack_trace_fops = {
Steven Rostedte5a81b62008-08-27 23:31:01 -0400393 .open = stack_trace_open,
394 .read = seq_read,
395 .llseek = seq_lseek,
Li Zefand8cc1ab2009-07-23 11:28:40 +0800396 .release = seq_release,
Steven Rostedte5a81b62008-08-27 23:31:01 -0400397};
398
Steven Rostedt (VMware)bbd1d272017-07-11 19:21:04 -0400399#ifdef CONFIG_DYNAMIC_FTRACE
400
Steven Rostedtd2d45c72011-12-19 14:44:09 -0500401static int
402stack_trace_filter_open(struct inode *inode, struct file *file)
403{
Steven Rostedt (VMware)0f179762017-06-29 10:05:45 -0400404 struct ftrace_ops *ops = inode->i_private;
405
406 return ftrace_regex_open(ops, FTRACE_ITER_FILTER,
Steven Rostedtd2d45c72011-12-19 14:44:09 -0500407 inode, file);
408}
409
410static const struct file_operations stack_trace_filter_fops = {
411 .open = stack_trace_filter_open,
412 .read = seq_read,
413 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -0500414 .llseek = tracing_lseek,
Steven Rostedtd2d45c72011-12-19 14:44:09 -0500415 .release = ftrace_regex_release,
416};
417
Steven Rostedt (VMware)bbd1d272017-07-11 19:21:04 -0400418#endif /* CONFIG_DYNAMIC_FTRACE */
419
Steven Rostedtf38f1d22008-12-16 23:06:40 -0500420int
421stack_trace_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700422 void __user *buffer, size_t *lenp,
Steven Rostedtf38f1d22008-12-16 23:06:40 -0500423 loff_t *ppos)
424{
425 int ret;
426
427 mutex_lock(&stack_sysctl_mutex);
428
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700429 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtf38f1d22008-12-16 23:06:40 -0500430
431 if (ret || !write ||
Li Zefana32c7762009-06-26 16:55:51 +0800432 (last_stack_tracer_enabled == !!stack_tracer_enabled))
Steven Rostedtf38f1d22008-12-16 23:06:40 -0500433 goto out;
434
Li Zefana32c7762009-06-26 16:55:51 +0800435 last_stack_tracer_enabled = !!stack_tracer_enabled;
Steven Rostedtf38f1d22008-12-16 23:06:40 -0500436
437 if (stack_tracer_enabled)
438 register_ftrace_function(&trace_ops);
439 else
440 unregister_ftrace_function(&trace_ops);
441
442 out:
443 mutex_unlock(&stack_sysctl_mutex);
444 return ret;
445}
446
Steven Rostedt762e1202011-12-19 22:01:00 -0500447static char stack_trace_filter_buf[COMMAND_LINE_SIZE+1] __initdata;
448
Steven Rostedtf38f1d22008-12-16 23:06:40 -0500449static __init int enable_stacktrace(char *str)
450{
Steven Rostedt762e1202011-12-19 22:01:00 -0500451 if (strncmp(str, "_filter=", 8) == 0)
452 strncpy(stack_trace_filter_buf, str+8, COMMAND_LINE_SIZE);
453
Steven Rostedte05a43b2008-12-17 09:43:00 -0500454 stack_tracer_enabled = 1;
455 last_stack_tracer_enabled = 1;
Steven Rostedtf38f1d22008-12-16 23:06:40 -0500456 return 1;
457}
458__setup("stacktrace", enable_stacktrace);
459
Steven Rostedte5a81b62008-08-27 23:31:01 -0400460static __init int stack_trace_init(void)
461{
462 struct dentry *d_tracer;
Steven Rostedte5a81b62008-08-27 23:31:01 -0400463
464 d_tracer = tracing_init_dentry();
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -0500465 if (IS_ERR(d_tracer))
Namhyung Kimed6f1c92013-04-10 09:18:12 +0900466 return 0;
Steven Rostedte5a81b62008-08-27 23:31:01 -0400467
Frederic Weisbecker5452af62009-03-27 00:25:38 +0100468 trace_create_file("stack_max_size", 0644, d_tracer,
AKASHI Takahirobb99d8c2015-10-30 14:25:39 +0900469 &stack_trace_max_size, &stack_max_size_fops);
Steven Rostedte5a81b62008-08-27 23:31:01 -0400470
Frederic Weisbecker5452af62009-03-27 00:25:38 +0100471 trace_create_file("stack_trace", 0444, d_tracer,
472 NULL, &stack_trace_fops);
Steven Rostedte5a81b62008-08-27 23:31:01 -0400473
Steven Rostedt (VMware)bbd1d272017-07-11 19:21:04 -0400474#ifdef CONFIG_DYNAMIC_FTRACE
Zhengyuan Liu0c5a9ac2018-02-08 09:41:53 +0800475 trace_create_file("stack_trace_filter", 0644, d_tracer,
Steven Rostedt (VMware)0f179762017-06-29 10:05:45 -0400476 &trace_ops, &stack_trace_filter_fops);
Steven Rostedt (VMware)bbd1d272017-07-11 19:21:04 -0400477#endif
Steven Rostedtd2d45c72011-12-19 14:44:09 -0500478
Steven Rostedt762e1202011-12-19 22:01:00 -0500479 if (stack_trace_filter_buf[0])
480 ftrace_set_early_filter(&trace_ops, stack_trace_filter_buf, 1);
481
Steven Rostedte05a43b2008-12-17 09:43:00 -0500482 if (stack_tracer_enabled)
Steven Rostedtf38f1d22008-12-16 23:06:40 -0500483 register_ftrace_function(&trace_ops);
Steven Rostedte5a81b62008-08-27 23:31:01 -0400484
485 return 0;
486}
487
488device_initcall(stack_trace_init);