History

ftrace: Add recording of functions that caused recursion

This adds CONFIG_FTRACE_RECORD_RECURSION that will record to a file "recursed_functions" all the functions that caused recursion while a callback to the function tracer was running. Link: https://lkml.kernel.org/r/20201106023548.102375687@goodmis.org

Steven Rostedt (VMware) committed 6 years ago

Commit 773c1671 parent a25d03617 files · +306 −20Browse files

Unified · Split

17 files
  1. Documentation/trace/ftrace-uses.rst +4 −2
  2. arch/csky/kernel/probes/ftrace.c +1 −1
  3. arch/parisc/kernel/ftrace.c +1 −1
  4. arch/powerpc/kernel/kprobes-ftrace.c +1 −1
  5. arch/s390/kernel/ftrace.c +1 −1
  6. arch/x86/kernel/kprobes/ftrace.c +1 −1
  7. fs/pstore/ftrace.c +1 −1
  8. include/linux/trace_recursion.h +25 −4
  9. kernel/livepatch/patch.c +1 −1
  10. kernel/trace/Kconfig +25 −0
  11. kernel/trace/Makefile +1 −0
  12. kernel/trace/ftrace.c +2 −2
  13. kernel/trace/trace_event_perf.c +1 −1
  14. kernel/trace/trace_functions.c +1 −1
  15. kernel/trace/trace_output.c +3 −3
  16. kernel/trace/trace_output.h +1 −0
  17. kernel/trace/trace_recursion_record.c +236 −0
Documentation/trace/ftrace-uses.rst+4−2
@@ -118,7 +118,7 @@ can help in this regard. If you start your code with: · 117 unmodified lines
118 118
119 int bit;119 int bit;
120 120
121 bit = ftrace_test_recursion_trylock();121 bit = ftrace_test_recursion_trylock(ip, parent_ip);
122 if (bit < 0)122 if (bit < 0)
123 return;123 return;
124 124
@@ -130,7 +130,9 @@ The code in between will be safe to use, even if it ends up calling a · 5 unmodified lines
130function that the callback is tracing. Note, on success,130function that the callback is tracing. Note, on success,
131ftrace_test_recursion_trylock() will disable preemption, and the131ftrace_test_recursion_trylock() will disable preemption, and the
132ftrace_test_recursion_unlock() will enable it again (if it was previously132ftrace_test_recursion_unlock() will enable it again (if it was previously
133enabled).133enabled). The instruction pointer (ip) and its parent (parent_ip) is passed to
134ftrace_test_recursion_trylock() to record where the recursion happened
135(if CONFIG_FTRACE_RECORD_RECURSION is set).
134 136
135Alternatively, if the FTRACE_OPS_FL_RECURSION flag is set on the ftrace_ops137Alternatively, if the FTRACE_OPS_FL_RECURSION flag is set on the ftrace_ops
136(as explained below), then a helper trampoline will be used to test138(as explained below), then a helper trampoline will be used to test
arch/csky/kernel/probes/ftrace.c+1−1
@@ -18,7 +18,7 @@ void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip, · 17 unmodified lines
18 struct kprobe *p;18 struct kprobe *p;
19 struct kprobe_ctlblk *kcb;19 struct kprobe_ctlblk *kcb;
20 20
21 bit = ftrace_test_recursion_trylock();21 bit = ftrace_test_recursion_trylock(ip, parent_ip);
22 if (bit < 0)22 if (bit < 0)
23 return;23 return;
24 24
arch/parisc/kernel/ftrace.c+1−1
@@ -210,7 +210,7 @@ void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip, · 209 unmodified lines
210 struct kprobe *p;210 struct kprobe *p;
211 int bit;211 int bit;
212 212
213 bit = ftrace_test_recursion_trylock();213 bit = ftrace_test_recursion_trylock(ip, parent_ip);
214 if (bit < 0)214 if (bit < 0)
215 return;215 return;
216 216
arch/powerpc/kernel/kprobes-ftrace.c+1−1
@@ -20,7 +20,7 @@ void kprobe_ftrace_handler(unsigned long nip, unsigned long parent_nip, · 19 unmodified lines
20 struct kprobe_ctlblk *kcb;20 struct kprobe_ctlblk *kcb;
21 int bit;21 int bit;
22 22
23 bit = ftrace_test_recursion_trylock();23 bit = ftrace_test_recursion_trylock(nip, parent_nip);
24 if (bit < 0)24 if (bit < 0)
25 return;25 return;
26 26
arch/s390/kernel/ftrace.c+1−1
@@ -204,7 +204,7 @@ void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip, · 203 unmodified lines
204 struct kprobe *p;204 struct kprobe *p;
205 int bit;205 int bit;
206 206
207 bit = ftrace_test_recursion_trylock();207 bit = ftrace_test_recursion_trylock(ip, parent_ip);
208 if (bit < 0)208 if (bit < 0)
209 return;209 return;
210 210
arch/x86/kernel/kprobes/ftrace.c+1−1
@@ -20,7 +20,7 @@ void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip, · 19 unmodified lines
20 struct kprobe_ctlblk *kcb;20 struct kprobe_ctlblk *kcb;
21 int bit;21 int bit;
22 22
23 bit = ftrace_test_recursion_trylock();23 bit = ftrace_test_recursion_trylock(ip, parent_ip);
24 if (bit < 0)24 if (bit < 0)
25 return;25 return;
26 26
fs/pstore/ftrace.c+1−1
@@ -41,7 +41,7 @@ static void notrace pstore_ftrace_call(unsigned long ip, · 40 unmodified lines
41 if (unlikely(oops_in_progress))41 if (unlikely(oops_in_progress))
42 return;42 return;
43 43
44 bit = ftrace_test_recursion_trylock();44 bit = ftrace_test_recursion_trylock(ip, parent_ip);
45 if (bit < 0)45 if (bit < 0)
46 return;46 return;
47 47
include/linux/trace_recursion.h+25−4
@@ -91,6 +91,9 @@ enum { · 90 unmodified lines
91 * not be correct. Allow for a single recursion to cover this case.91 * not be correct. Allow for a single recursion to cover this case.
92 */92 */
93 TRACE_TRANSITION_BIT,93 TRACE_TRANSITION_BIT,
94
95 /* Used to prevent recursion recording from recursing. */
96 TRACE_RECORD_RECURSION_BIT,
94};97};
95 98
96#define trace_recursion_set(bit) do { (current)->trace_recursion |= (1<<(bit)); } while (0)99#define trace_recursion_set(bit) do { (current)->trace_recursion |= (1<<(bit)); } while (0)
@@ -142,7 +145,22 @@ static __always_inline int trace_get_context_bit(void) · 45 unmodified lines
142 pc & HARDIRQ_MASK ? TRACE_CTX_IRQ : TRACE_CTX_SOFTIRQ;145 pc & HARDIRQ_MASK ? TRACE_CTX_IRQ : TRACE_CTX_SOFTIRQ;
143}146}
144 147
145static __always_inline int trace_test_and_set_recursion(int start, int max)148#ifdef CONFIG_FTRACE_RECORD_RECURSION
149extern void ftrace_record_recursion(unsigned long ip, unsigned long parent_ip);
150# define do_ftrace_record_recursion(ip, pip) \
151 do { \
152 if (!trace_recursion_test(TRACE_RECORD_RECURSION_BIT)) { \
153 trace_recursion_set(TRACE_RECORD_RECURSION_BIT); \
154 ftrace_record_recursion(ip, pip); \
155 trace_recursion_clear(TRACE_RECORD_RECURSION_BIT); \
156 } \
157 } while (0)
158#else
159# define do_ftrace_record_recursion(ip, pip) do { } while (0)
160#endif
161
162static __always_inline int trace_test_and_set_recursion(unsigned long ip, unsigned long pip,
163 int start, int max)
146{164{
147 unsigned int val = current->trace_recursion;165 unsigned int val = current->trace_recursion;
148 int bit;166 int bit;
@@ -158,8 +176,10 @@ static __always_inline int trace_test_and_set_recursion(int start, int max) · 9 unmodified lines
158 * a switch between contexts. Allow for a single recursion.176 * a switch between contexts. Allow for a single recursion.
159 */177 */
160 bit = TRACE_TRANSITION_BIT;178 bit = TRACE_TRANSITION_BIT;
161 if (trace_recursion_test(bit))179 if (trace_recursion_test(bit)) {
180 do_ftrace_record_recursion(ip, pip);
162 return -1;181 return -1;
182 }
163 trace_recursion_set(bit);183 trace_recursion_set(bit);
164 barrier();184 barrier();
165 return bit + 1;185 return bit + 1;
@@ -199,9 +219,10 @@ static __always_inline void trace_clear_recursion(int bit) · 33 unmodified lines
199 * Returns: -1 if a recursion happened.219 * Returns: -1 if a recursion happened.
200 * >= 0 if no recursion220 * >= 0 if no recursion
201 */221 */
202static __always_inline int ftrace_test_recursion_trylock(void)222static __always_inline int ftrace_test_recursion_trylock(unsigned long ip,
223 unsigned long parent_ip)
203{224{
204 return trace_test_and_set_recursion(TRACE_FTRACE_START, TRACE_FTRACE_MAX);225 return trace_test_and_set_recursion(ip, parent_ip, TRACE_FTRACE_START, TRACE_FTRACE_MAX);
205}226}
206 227
207/**228/**
kernel/livepatch/patch.c+1−1
@@ -49,7 +49,7 @@ static void notrace klp_ftrace_handler(unsigned long ip, · 48 unmodified lines
49 49
50 ops = container_of(fops, struct klp_ops, fops);50 ops = container_of(fops, struct klp_ops, fops);
51 51
52 bit = ftrace_test_recursion_trylock();52 bit = ftrace_test_recursion_trylock(ip, parent_ip);
53 if (WARN_ON_ONCE(bit < 0))53 if (WARN_ON_ONCE(bit < 0))
54 return;54 return;
55 /*55 /*
kernel/trace/Kconfig+25−0
@@ -727,6 +727,31 @@ config TRACE_EVAL_MAP_FILE · 726 unmodified lines
727 727
728 If unsure, say N.728 If unsure, say N.
729 729
730config FTRACE_RECORD_RECURSION
731 bool "Record functions that recurse in function tracing"
732 depends on FUNCTION_TRACER
733 help
734 All callbacks that attach to the function tracing have some sort
735 of protection against recursion. Even though the protection exists,
736 it adds overhead. This option will create a file in the tracefs
737 file system called "recursed_functions" that will list the functions
738 that triggered a recursion.
739
740 This will add more overhead to cases that have recursion.
741
742 If unsure, say N
743
744config FTRACE_RECORD_RECURSION_SIZE
745 int "Max number of recursed functions to record"
746 default 128
747 depends on FTRACE_RECORD_RECURSION
748 help
749 This defines the limit of number of functions that can be
750 listed in the "recursed_functions" file, that lists all
751 the functions that caused a recursion to happen.
752 This file can be reset, but the limit can not change in
753 size at runtime.
754
730config GCOV_PROFILE_FTRACE755config GCOV_PROFILE_FTRACE
731 bool "Enable GCOV profiling on ftrace subsystem"756 bool "Enable GCOV profiling on ftrace subsystem"
732 depends on GCOV_KERNEL757 depends on GCOV_KERNEL
kernel/trace/Makefile+1−0
@@ -92,6 +92,7 @@ obj-$(CONFIG_DYNAMIC_EVENTS) += trace_dynevent.o · 91 unmodified lines
92obj-$(CONFIG_PROBE_EVENTS) += trace_probe.o92obj-$(CONFIG_PROBE_EVENTS) += trace_probe.o
93obj-$(CONFIG_UPROBE_EVENTS) += trace_uprobe.o93obj-$(CONFIG_UPROBE_EVENTS) += trace_uprobe.o
94obj-$(CONFIG_BOOTTIME_TRACING) += trace_boot.o94obj-$(CONFIG_BOOTTIME_TRACING) += trace_boot.o
95obj-$(CONFIG_FTRACE_RECORD_RECURSION) += trace_recursion_record.o
95 96
96obj-$(CONFIG_TRACEPOINT_BENCHMARK) += trace_benchmark.o97obj-$(CONFIG_TRACEPOINT_BENCHMARK) += trace_benchmark.o
97 98
kernel/trace/ftrace.c+2−2
@@ -6918,7 +6918,7 @@ __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip, · 6917 unmodified lines
6918 struct ftrace_ops *op;6918 struct ftrace_ops *op;
6919 int bit;6919 int bit;
6920 6920
6921 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);6921 bit = trace_test_and_set_recursion(ip, parent_ip, TRACE_LIST_START, TRACE_LIST_MAX);
6922 if (bit < 0)6922 if (bit < 0)
6923 return;6923 return;
6924 6924
@@ -6993,7 +6993,7 @@ static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip, · 68 unmodified lines
6993{6993{
6994 int bit;6994 int bit;
6995 6995
6996 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);6996 bit = trace_test_and_set_recursion(ip, parent_ip, TRACE_LIST_START, TRACE_LIST_MAX);
6997 if (bit < 0)6997 if (bit < 0)
6998 return;6998 return;
6999 6999
kernel/trace/trace_event_perf.c+1−1
@@ -447,7 +447,7 @@ perf_ftrace_function_call(unsigned long ip, unsigned long parent_ip, · 446 unmodified lines
447 if ((unsigned long)ops->private != smp_processor_id())447 if ((unsigned long)ops->private != smp_processor_id())
448 return;448 return;
449 449
450 bit = ftrace_test_recursion_trylock();450 bit = ftrace_test_recursion_trylock(ip, parent_ip);
451 if (bit < 0)451 if (bit < 0)
452 return;452 return;
453 453
kernel/trace/trace_functions.c+1−1
@@ -141,7 +141,7 @@ function_trace_call(unsigned long ip, unsigned long parent_ip, · 140 unmodified lines
141 if (unlikely(!tr->function_enabled))141 if (unlikely(!tr->function_enabled))
142 return;142 return;
143 143
144 bit = ftrace_test_recursion_trylock();144 bit = ftrace_test_recursion_trylock(ip, parent_ip);
145 if (bit < 0)145 if (bit < 0)
146 return;146 return;
147 147
kernel/trace/trace_output.c+3−3
@@ -353,8 +353,8 @@ static inline const char *kretprobed(const char *name) · 352 unmodified lines
353}353}
354#endif /* CONFIG_KRETPROBES */354#endif /* CONFIG_KRETPROBES */
355 355
356static void356void
357seq_print_sym(struct trace_seq *s, unsigned long address, bool offset)357trace_seq_print_sym(struct trace_seq *s, unsigned long address, bool offset)
358{358{
359#ifdef CONFIG_KALLSYMS359#ifdef CONFIG_KALLSYMS
360 char str[KSYM_SYMBOL_LEN];360 char str[KSYM_SYMBOL_LEN];
@@ -420,7 +420,7 @@ seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags) · 59 unmodified lines
420 goto out;420 goto out;
421 }421 }
422 422
423 seq_print_sym(s, ip, sym_flags & TRACE_ITER_SYM_OFFSET);423 trace_seq_print_sym(s, ip, sym_flags & TRACE_ITER_SYM_OFFSET);
424 424
425 if (sym_flags & TRACE_ITER_SYM_ADDR)425 if (sym_flags & TRACE_ITER_SYM_ADDR)
426 trace_seq_printf(s, " <" IP_FMT ">", ip);426 trace_seq_printf(s, " <" IP_FMT ">", ip);
kernel/trace/trace_output.h+1−0
@@ -16,6 +16,7 @@ extern int · 15 unmodified lines
16seq_print_ip_sym(struct trace_seq *s, unsigned long ip,16seq_print_ip_sym(struct trace_seq *s, unsigned long ip,
17 unsigned long sym_flags);17 unsigned long sym_flags);
18 18
19extern void trace_seq_print_sym(struct trace_seq *s, unsigned long address, bool offset);
19extern int trace_print_context(struct trace_iterator *iter);20extern int trace_print_context(struct trace_iterator *iter);
20extern int trace_print_lat_context(struct trace_iterator *iter);21extern int trace_print_lat_context(struct trace_iterator *iter);
21 22
kernel/trace/trace_recursion_record.c+236−0
@@ -0,0 +1,236 @@
1// SPDX-License-Identifier: GPL-2.0
2
3#include <linux/seq_file.h>
4#include <linux/kallsyms.h>
5#include <linux/module.h>
6#include <linux/ftrace.h>
7#include <linux/fs.h>
8
9#include "trace_output.h"
10
11struct recursed_functions {
12 unsigned long ip;
13 unsigned long parent_ip;
14};
15
16static struct recursed_functions recursed_functions[CONFIG_FTRACE_RECORD_RECURSION_SIZE];
17static atomic_t nr_records;
18
19/*
20 * Cache the last found function. Yes, updates to this is racey, but
21 * so is memory cache ;-)
22 */
23static unsigned long cached_function;
24
25void ftrace_record_recursion(unsigned long ip, unsigned long parent_ip)
26{
27 int index = 0;
28 int i;
29 unsigned long old;
30
31 again:
32 /* First check the last one recorded */
33 if (ip == cached_function)
34 return;
35
36 i = atomic_read(&nr_records);
37 /* nr_records is -1 when clearing records */
38 smp_mb__after_atomic();
39 if (i < 0)
40 return;
41
42 /*
43 * If there's two writers and this writer comes in second,
44 * the cmpxchg() below to update the ip will fail. Then this
45 * writer will try again. It is possible that index will now
46 * be greater than nr_records. This is because the writer
47 * that succeeded has not updated the nr_records yet.
48 * This writer could keep trying again until the other writer
49 * updates nr_records. But if the other writer takes an
50 * interrupt, and that interrupt locks up that CPU, we do
51 * not want this CPU to lock up due to the recursion protection,
52 * and have a bug report showing this CPU as the cause of
53 * locking up the computer. To not lose this record, this
54 * writer will simply use the next position to update the
55 * recursed_functions, and it will update the nr_records
56 * accordingly.
57 */
58 if (index < i)
59 index = i;
60 if (index >= CONFIG_FTRACE_RECORD_RECURSION_SIZE)
61 return;
62
63 for (i = index - 1; i >= 0; i--) {
64 if (recursed_functions[i].ip == ip) {
65 cached_function = ip;
66 return;
67 }
68 }
69
70 cached_function = ip;
71
72 /*
73 * We only want to add a function if it hasn't been added before.
74 * Add to the current location before incrementing the count.
75 * If it fails to add, then increment the index (save in i)
76 * and try again.
77 */
78 old = cmpxchg(&recursed_functions[index].ip, 0, ip);
79 if (old != 0) {
80 /* Did something else already added this for us? */
81 if (old == ip)
82 return;
83 /* Try the next location (use i for the next index) */
84 index++;
85 goto again;
86 }
87
88 recursed_functions[index].parent_ip = parent_ip;
89
90 /*
91 * It's still possible that we could race with the clearing
92 * CPU0 CPU1
93 * ---- ----
94 * ip = func
95 * nr_records = -1;
96 * recursed_functions[0] = 0;
97 * i = -1
98 * if (i < 0)
99 * nr_records = 0;
100 * (new recursion detected)
101 * recursed_functions[0] = func
102 * cmpxchg(recursed_functions[0],
103 * func, 0)
104 *
105 * But the worse that could happen is that we get a zero in
106 * the recursed_functions array, and it's likely that "func" will
107 * be recorded again.
108 */
109 i = atomic_read(&nr_records);
110 smp_mb__after_atomic();
111 if (i < 0)
112 cmpxchg(&recursed_functions[index].ip, ip, 0);
113 else if (i <= index)
114 atomic_cmpxchg(&nr_records, i, index + 1);
115}
116EXPORT_SYMBOL_GPL(ftrace_record_recursion);
117
118static DEFINE_MUTEX(recursed_function_lock);
119static struct trace_seq *tseq;
120
121static void *recursed_function_seq_start(struct seq_file *m, loff_t *pos)
122{
123 void *ret = NULL;
124 int index;
125
126 mutex_lock(&recursed_function_lock);
127 index = atomic_read(&nr_records);
128 if (*pos < index) {
129 ret = &recursed_functions[*pos];
130 }
131
132 tseq = kzalloc(sizeof(*tseq), GFP_KERNEL);
133 if (!tseq)
134 return ERR_PTR(-ENOMEM);
135
136 trace_seq_init(tseq);
137
138 return ret;
139}
140
141static void *recursed_function_seq_next(struct seq_file *m, void *v, loff_t *pos)
142{
143 int index;
144 int p;
145
146 index = atomic_read(&nr_records);
147 p = ++(*pos);
148
149 return p < index ? &recursed_functions[p] : NULL;
150}
151
152static void recursed_function_seq_stop(struct seq_file *m, void *v)
153{
154 kfree(tseq);
155 mutex_unlock(&recursed_function_lock);
156}
157
158static int recursed_function_seq_show(struct seq_file *m, void *v)
159{
160 struct recursed_functions *record = v;
161 int ret = 0;
162
163 if (record) {
164 trace_seq_print_sym(tseq, record->parent_ip, true);
165 trace_seq_puts(tseq, ":\t");
166 trace_seq_print_sym(tseq, record->ip, true);
167 trace_seq_putc(tseq, '\n');
168 ret = trace_print_seq(m, tseq);
169 }
170
171 return ret;
172}
173
174static const struct seq_operations recursed_function_seq_ops = {
175 .start = recursed_function_seq_start,
176 .next = recursed_function_seq_next,
177 .stop = recursed_function_seq_stop,
178 .show = recursed_function_seq_show
179};
180
181static int recursed_function_open(struct inode *inode, struct file *file)
182{
183 int ret = 0;
184
185 mutex_lock(&recursed_function_lock);
186 /* If this file was opened for write, then erase contents */
187 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
188 /* disable updating records */
189 atomic_set(&nr_records, -1);
190 smp_mb__after_atomic();
191 memset(recursed_functions, 0, sizeof(recursed_functions));
192 smp_wmb();
193 /* enable them again */
194 atomic_set(&nr_records, 0);
195 }
196 if (file->f_mode & FMODE_READ)
197 ret = seq_open(file, &recursed_function_seq_ops);
198 mutex_unlock(&recursed_function_lock);
199
200 return ret;
201}
202
203static ssize_t recursed_function_write(struct file *file,
204 const char __user *buffer,
205 size_t count, loff_t *ppos)
206{
207 return count;
208}
209
210static int recursed_function_release(struct inode *inode, struct file *file)
211{
212 if (file->f_mode & FMODE_READ)
213 seq_release(inode, file);
214 return 0;
215}
216
217static const struct file_operations recursed_functions_fops = {
218 .open = recursed_function_open,
219 .write = recursed_function_write,
220 .read = seq_read,
221 .llseek = seq_lseek,
222 .release = recursed_function_release,
223};
224
225__init static int create_recursed_functions(void)
226{
227 struct dentry *dentry;
228
229 dentry = trace_create_file("recursed_functions", 0644, NULL, NULL,
230 &recursed_functions_fops);
231 if (!dentry)
232 pr_warn("WARNING: Failed to create recursed_functions\n");
233 return 0;
234}
235
236fs_initcall(create_recursed_functions);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t