trace_stack.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
  3. *
  4. */
  5. #include <linux/sched/task_stack.h>
  6. #include <linux/stacktrace.h>
  7. #include <linux/kallsyms.h>
  8. #include <linux/seq_file.h>
  9. #include <linux/spinlock.h>
  10. #include <linux/uaccess.h>
  11. #include <linux/ftrace.h>
  12. #include <linux/module.h>
  13. #include <linux/sysctl.h>
  14. #include <linux/init.h>
  15. #include <asm/setup.h>
  16. #include "trace.h"
  17. static unsigned long stack_dump_trace[STACK_TRACE_ENTRIES+1] =
  18. { [0 ... (STACK_TRACE_ENTRIES)] = ULONG_MAX };
  19. unsigned stack_trace_index[STACK_TRACE_ENTRIES];
  20. /*
  21. * Reserve one entry for the passed in ip. This will allow
  22. * us to remove most or all of the stack size overhead
  23. * added by the stack tracer itself.
  24. */
  25. struct stack_trace stack_trace_max = {
  26. .max_entries = STACK_TRACE_ENTRIES - 1,
  27. .entries = &stack_dump_trace[0],
  28. };
  29. unsigned long stack_trace_max_size;
  30. arch_spinlock_t stack_trace_max_lock =
  31. (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
  32. DEFINE_PER_CPU(int, disable_stack_tracer);
  33. static DEFINE_MUTEX(stack_sysctl_mutex);
  34. int stack_tracer_enabled;
  35. static int last_stack_tracer_enabled;
  36. void stack_trace_print(void)
  37. {
  38. long i;
  39. int size;
  40. pr_emerg(" Depth Size Location (%d entries)\n"
  41. " ----- ---- --------\n",
  42. stack_trace_max.nr_entries);
  43. for (i = 0; i < stack_trace_max.nr_entries; i++) {
  44. if (stack_dump_trace[i] == ULONG_MAX)
  45. break;
  46. if (i+1 == stack_trace_max.nr_entries ||
  47. stack_dump_trace[i+1] == ULONG_MAX)
  48. size = stack_trace_index[i];
  49. else
  50. size = stack_trace_index[i] - stack_trace_index[i+1];
  51. pr_emerg("%3ld) %8d %5d %pS\n", i, stack_trace_index[i],
  52. size, (void *)stack_dump_trace[i]);
  53. }
  54. }
  55. /*
  56. * When arch-specific code overrides this function, the following
  57. * data should be filled up, assuming stack_trace_max_lock is held to
  58. * prevent concurrent updates.
  59. * stack_trace_index[]
  60. * stack_trace_max
  61. * stack_trace_max_size
  62. */
  63. void __weak
  64. check_stack(unsigned long ip, unsigned long *stack)
  65. {
  66. unsigned long this_size, flags; unsigned long *p, *top, *start;
  67. static int tracer_frame;
  68. int frame_size = ACCESS_ONCE(tracer_frame);
  69. int i, x;
  70. this_size = ((unsigned long)stack) & (THREAD_SIZE-1);
  71. this_size = THREAD_SIZE - this_size;
  72. /* Remove the frame of the tracer */
  73. this_size -= frame_size;
  74. if (this_size <= stack_trace_max_size)
  75. return;
  76. /* we do not handle interrupt stacks yet */
  77. if (!object_is_on_stack(stack))
  78. return;
  79. /* Can't do this from NMI context (can cause deadlocks) */
  80. if (in_nmi())
  81. return;
  82. local_irq_save(flags);
  83. arch_spin_lock(&stack_trace_max_lock);
  84. /* In case another CPU set the tracer_frame on us */
  85. if (unlikely(!frame_size))
  86. this_size -= tracer_frame;
  87. /* a race could have already updated it */
  88. if (this_size <= stack_trace_max_size)
  89. goto out;
  90. stack_trace_max_size = this_size;
  91. stack_trace_max.nr_entries = 0;
  92. stack_trace_max.skip = 3;
  93. save_stack_trace(&stack_trace_max);
  94. /* Skip over the overhead of the stack tracer itself */
  95. for (i = 0; i < stack_trace_max.nr_entries; i++) {
  96. if (stack_dump_trace[i] == ip)
  97. break;
  98. }
  99. /*
  100. * Some archs may not have the passed in ip in the dump.
  101. * If that happens, we need to show everything.
  102. */
  103. if (i == stack_trace_max.nr_entries)
  104. i = 0;
  105. /*
  106. * Now find where in the stack these are.
  107. */
  108. x = 0;
  109. start = stack;
  110. top = (unsigned long *)
  111. (((unsigned long)start & ~(THREAD_SIZE-1)) + THREAD_SIZE);
  112. /*
  113. * Loop through all the entries. One of the entries may
  114. * for some reason be missed on the stack, so we may
  115. * have to account for them. If they are all there, this
  116. * loop will only happen once. This code only takes place
  117. * on a new max, so it is far from a fast path.
  118. */
  119. while (i < stack_trace_max.nr_entries) {
  120. int found = 0;
  121. stack_trace_index[x] = this_size;
  122. p = start;
  123. for (; p < top && i < stack_trace_max.nr_entries; p++) {
  124. if (stack_dump_trace[i] == ULONG_MAX)
  125. break;
  126. /*
  127. * The READ_ONCE_NOCHECK is used to let KASAN know that
  128. * this is not a stack-out-of-bounds error.
  129. */
  130. if ((READ_ONCE_NOCHECK(*p)) == stack_dump_trace[i]) {
  131. stack_dump_trace[x] = stack_dump_trace[i++];
  132. this_size = stack_trace_index[x++] =
  133. (top - p) * sizeof(unsigned long);
  134. found = 1;
  135. /* Start the search from here */
  136. start = p + 1;
  137. /*
  138. * We do not want to show the overhead
  139. * of the stack tracer stack in the
  140. * max stack. If we haven't figured
  141. * out what that is, then figure it out
  142. * now.
  143. */
  144. if (unlikely(!tracer_frame)) {
  145. tracer_frame = (p - stack) *
  146. sizeof(unsigned long);
  147. stack_trace_max_size -= tracer_frame;
  148. }
  149. }
  150. }
  151. if (!found)
  152. i++;
  153. }
  154. stack_trace_max.nr_entries = x;
  155. for (; x < i; x++)
  156. stack_dump_trace[x] = ULONG_MAX;
  157. if (task_stack_end_corrupted(current)) {
  158. stack_trace_print();
  159. BUG();
  160. }
  161. out:
  162. arch_spin_unlock(&stack_trace_max_lock);
  163. local_irq_restore(flags);
  164. }
  165. static void
  166. stack_trace_call(unsigned long ip, unsigned long parent_ip,
  167. struct ftrace_ops *op, struct pt_regs *pt_regs)
  168. {
  169. unsigned long stack;
  170. preempt_disable_notrace();
  171. /* no atomic needed, we only modify this variable by this cpu */
  172. __this_cpu_inc(disable_stack_tracer);
  173. if (__this_cpu_read(disable_stack_tracer) != 1)
  174. goto out;
  175. ip += MCOUNT_INSN_SIZE;
  176. check_stack(ip, &stack);
  177. out:
  178. __this_cpu_dec(disable_stack_tracer);
  179. /* prevent recursion in schedule */
  180. preempt_enable_notrace();
  181. }
  182. static struct ftrace_ops trace_ops __read_mostly =
  183. {
  184. .func = stack_trace_call,
  185. .flags = FTRACE_OPS_FL_RECURSION_SAFE,
  186. };
  187. static ssize_t
  188. stack_max_size_read(struct file *filp, char __user *ubuf,
  189. size_t count, loff_t *ppos)
  190. {
  191. unsigned long *ptr = filp->private_data;
  192. char buf[64];
  193. int r;
  194. r = snprintf(buf, sizeof(buf), "%ld\n", *ptr);
  195. if (r > sizeof(buf))
  196. r = sizeof(buf);
  197. return simple_read_from_buffer(ubuf, count, ppos, buf, r);
  198. }
  199. static ssize_t
  200. stack_max_size_write(struct file *filp, const char __user *ubuf,
  201. size_t count, loff_t *ppos)
  202. {
  203. long *ptr = filp->private_data;
  204. unsigned long val, flags;
  205. int ret;
  206. ret = kstrtoul_from_user(ubuf, count, 10, &val);
  207. if (ret)
  208. return ret;
  209. local_irq_save(flags);
  210. /*
  211. * In case we trace inside arch_spin_lock() or after (NMI),
  212. * we will cause circular lock, so we also need to increase
  213. * the percpu disable_stack_tracer here.
  214. */
  215. __this_cpu_inc(disable_stack_tracer);
  216. arch_spin_lock(&stack_trace_max_lock);
  217. *ptr = val;
  218. arch_spin_unlock(&stack_trace_max_lock);
  219. __this_cpu_dec(disable_stack_tracer);
  220. local_irq_restore(flags);
  221. return count;
  222. }
  223. static const struct file_operations stack_max_size_fops = {
  224. .open = tracing_open_generic,
  225. .read = stack_max_size_read,
  226. .write = stack_max_size_write,
  227. .llseek = default_llseek,
  228. };
  229. static void *
  230. __next(struct seq_file *m, loff_t *pos)
  231. {
  232. long n = *pos - 1;
  233. if (n > stack_trace_max.nr_entries || stack_dump_trace[n] == ULONG_MAX)
  234. return NULL;
  235. m->private = (void *)n;
  236. return &m->private;
  237. }
  238. static void *
  239. t_next(struct seq_file *m, void *v, loff_t *pos)
  240. {
  241. (*pos)++;
  242. return __next(m, pos);
  243. }
  244. static void *t_start(struct seq_file *m, loff_t *pos)
  245. {
  246. local_irq_disable();
  247. __this_cpu_inc(disable_stack_tracer);
  248. arch_spin_lock(&stack_trace_max_lock);
  249. if (*pos == 0)
  250. return SEQ_START_TOKEN;
  251. return __next(m, pos);
  252. }
  253. static void t_stop(struct seq_file *m, void *p)
  254. {
  255. arch_spin_unlock(&stack_trace_max_lock);
  256. __this_cpu_dec(disable_stack_tracer);
  257. local_irq_enable();
  258. }
  259. static void trace_lookup_stack(struct seq_file *m, long i)
  260. {
  261. unsigned long addr = stack_dump_trace[i];
  262. seq_printf(m, "%pS\n", (void *)addr);
  263. }
  264. static void print_disabled(struct seq_file *m)
  265. {
  266. seq_puts(m, "#\n"
  267. "# Stack tracer disabled\n"
  268. "#\n"
  269. "# To enable the stack tracer, either add 'stacktrace' to the\n"
  270. "# kernel command line\n"
  271. "# or 'echo 1 > /proc/sys/kernel/stack_tracer_enabled'\n"
  272. "#\n");
  273. }
  274. static int t_show(struct seq_file *m, void *v)
  275. {
  276. long i;
  277. int size;
  278. if (v == SEQ_START_TOKEN) {
  279. seq_printf(m, " Depth Size Location"
  280. " (%d entries)\n"
  281. " ----- ---- --------\n",
  282. stack_trace_max.nr_entries);
  283. if (!stack_tracer_enabled && !stack_trace_max_size)
  284. print_disabled(m);
  285. return 0;
  286. }
  287. i = *(long *)v;
  288. if (i >= stack_trace_max.nr_entries ||
  289. stack_dump_trace[i] == ULONG_MAX)
  290. return 0;
  291. if (i+1 == stack_trace_max.nr_entries ||
  292. stack_dump_trace[i+1] == ULONG_MAX)
  293. size = stack_trace_index[i];
  294. else
  295. size = stack_trace_index[i] - stack_trace_index[i+1];
  296. seq_printf(m, "%3ld) %8d %5d ", i, stack_trace_index[i], size);
  297. trace_lookup_stack(m, i);
  298. return 0;
  299. }
  300. static const struct seq_operations stack_trace_seq_ops = {
  301. .start = t_start,
  302. .next = t_next,
  303. .stop = t_stop,
  304. .show = t_show,
  305. };
  306. static int stack_trace_open(struct inode *inode, struct file *file)
  307. {
  308. return seq_open(file, &stack_trace_seq_ops);
  309. }
  310. static const struct file_operations stack_trace_fops = {
  311. .open = stack_trace_open,
  312. .read = seq_read,
  313. .llseek = seq_lseek,
  314. .release = seq_release,
  315. };
  316. #ifdef CONFIG_DYNAMIC_FTRACE
  317. static int
  318. stack_trace_filter_open(struct inode *inode, struct file *file)
  319. {
  320. struct ftrace_ops *ops = inode->i_private;
  321. return ftrace_regex_open(ops, FTRACE_ITER_FILTER,
  322. inode, file);
  323. }
  324. static const struct file_operations stack_trace_filter_fops = {
  325. .open = stack_trace_filter_open,
  326. .read = seq_read,
  327. .write = ftrace_filter_write,
  328. .llseek = tracing_lseek,
  329. .release = ftrace_regex_release,
  330. };
  331. #endif /* CONFIG_DYNAMIC_FTRACE */
  332. int
  333. stack_trace_sysctl(struct ctl_table *table, int write,
  334. void __user *buffer, size_t *lenp,
  335. loff_t *ppos)
  336. {
  337. int ret;
  338. mutex_lock(&stack_sysctl_mutex);
  339. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  340. if (ret || !write ||
  341. (last_stack_tracer_enabled == !!stack_tracer_enabled))
  342. goto out;
  343. last_stack_tracer_enabled = !!stack_tracer_enabled;
  344. if (stack_tracer_enabled)
  345. register_ftrace_function(&trace_ops);
  346. else
  347. unregister_ftrace_function(&trace_ops);
  348. out:
  349. mutex_unlock(&stack_sysctl_mutex);
  350. return ret;
  351. }
  352. static char stack_trace_filter_buf[COMMAND_LINE_SIZE+1] __initdata;
  353. static __init int enable_stacktrace(char *str)
  354. {
  355. if (strncmp(str, "_filter=", 8) == 0)
  356. strncpy(stack_trace_filter_buf, str+8, COMMAND_LINE_SIZE);
  357. stack_tracer_enabled = 1;
  358. last_stack_tracer_enabled = 1;
  359. return 1;
  360. }
  361. __setup("stacktrace", enable_stacktrace);
  362. static __init int stack_trace_init(void)
  363. {
  364. struct dentry *d_tracer;
  365. d_tracer = tracing_init_dentry();
  366. if (IS_ERR(d_tracer))
  367. return 0;
  368. trace_create_file("stack_max_size", 0644, d_tracer,
  369. &stack_trace_max_size, &stack_max_size_fops);
  370. trace_create_file("stack_trace", 0444, d_tracer,
  371. NULL, &stack_trace_fops);
  372. #ifdef CONFIG_DYNAMIC_FTRACE
  373. trace_create_file("stack_trace_filter", 0444, d_tracer,
  374. &trace_ops, &stack_trace_filter_fops);
  375. #endif
  376. if (stack_trace_filter_buf[0])
  377. ftrace_set_early_filter(&trace_ops, stack_trace_filter_buf, 1);
  378. if (stack_tracer_enabled)
  379. register_ftrace_function(&trace_ops);
  380. return 0;
  381. }
  382. device_initcall(stack_trace_init);