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
118118
119119 int bit;
120120
121− bit = ftrace_test_recursion_trylock();
121+ bit = ftrace_test_recursion_trylock(ip, parent_ip);
122122 if (bit < 0)
123123 return;
124124
@@ -130,7 +130,9 @@ The code in between will be safe to use, even if it ends up calling a · 5 unmodified lines
130130 function that the callback is tracing. Note, on success,
131131 ftrace_test_recursion_trylock() will disable preemption, and the
132132 ftrace_test_recursion_unlock() will enable it again (if it was previously
133−enabled).
133+enabled). The instruction pointer (ip) and its parent (parent_ip) is passed to
134+ftrace_test_recursion_trylock() to record where the recursion happened
135+(if CONFIG_FTRACE_RECORD_RECURSION is set).
134136
135137 Alternatively, if the FTRACE_OPS_FL_RECURSION flag is set on the ftrace_ops
136138 (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
1818 struct kprobe *p;
1919 struct kprobe_ctlblk *kcb;
2020
21− bit = ftrace_test_recursion_trylock();
21+ bit = ftrace_test_recursion_trylock(ip, parent_ip);
2222 if (bit < 0)
2323 return;
2424
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
210210 struct kprobe *p;
211211 int bit;
212212
213− bit = ftrace_test_recursion_trylock();
213+ bit = ftrace_test_recursion_trylock(ip, parent_ip);
214214 if (bit < 0)
215215 return;
216216
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
2020 struct kprobe_ctlblk *kcb;
2121 int bit;
2222
23− bit = ftrace_test_recursion_trylock();
23+ bit = ftrace_test_recursion_trylock(nip, parent_nip);
2424 if (bit < 0)
2525 return;
2626
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
204204 struct kprobe *p;
205205 int bit;
206206
207− bit = ftrace_test_recursion_trylock();
207+ bit = ftrace_test_recursion_trylock(ip, parent_ip);
208208 if (bit < 0)
209209 return;
210210
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
2020 struct kprobe_ctlblk *kcb;
2121 int bit;
2222
23− bit = ftrace_test_recursion_trylock();
23+ bit = ftrace_test_recursion_trylock(ip, parent_ip);
2424 if (bit < 0)
2525 return;
2626
fs/pstore/ftrace.c+1−1
@@ -41,7 +41,7 @@ static void notrace pstore_ftrace_call(unsigned long ip, · 40 unmodified lines
4141 if (unlikely(oops_in_progress))
4242 return;
4343
44− bit = ftrace_test_recursion_trylock();
44+ bit = ftrace_test_recursion_trylock(ip, parent_ip);
4545 if (bit < 0)
4646 return;
4747
include/linux/trace_recursion.h+25−4
@@ -91,6 +91,9 @@ enum { · 90 unmodified lines
9191 * not be correct. Allow for a single recursion to cover this case.
9292 */
9393 TRACE_TRANSITION_BIT,
94+
95+ /* Used to prevent recursion recording from recursing. */
96+ TRACE_RECORD_RECURSION_BIT,
9497 };
9598
9699 #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
142145 pc & HARDIRQ_MASK ? TRACE_CTX_IRQ : TRACE_CTX_SOFTIRQ;
143146 }
144147
145−static __always_inline int trace_test_and_set_recursion(int start, int max)
148+#ifdef CONFIG_FTRACE_RECORD_RECURSION
149+extern 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+
162+static __always_inline int trace_test_and_set_recursion(unsigned long ip, unsigned long pip,
163+ int start, int max)
146164 {
147165 unsigned int val = current->trace_recursion;
148166 int bit;
@@ -158,8 +176,10 @@ static __always_inline int trace_test_and_set_recursion(int start, int max) · 9 unmodified lines
158176 * a switch between contexts. Allow for a single recursion.
159177 */
160178 bit = TRACE_TRANSITION_BIT;
161− if (trace_recursion_test(bit))
179+ if (trace_recursion_test(bit)) {
180+ do_ftrace_record_recursion(ip, pip);
162181 return -1;
182+ }
163183 trace_recursion_set(bit);
164184 barrier();
165185 return bit + 1;
@@ -199,9 +219,10 @@ static __always_inline void trace_clear_recursion(int bit) · 33 unmodified lines
199219 * Returns: -1 if a recursion happened.
200220 * >= 0 if no recursion
201221 */
202−static __always_inline int ftrace_test_recursion_trylock(void)
222+static __always_inline int ftrace_test_recursion_trylock(unsigned long ip,
223+ unsigned long parent_ip)
203224 {
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);
205226 }
206227
207228 /**
kernel/livepatch/patch.c+1−1
@@ -49,7 +49,7 @@ static void notrace klp_ftrace_handler(unsigned long ip, · 48 unmodified lines
4949
5050 ops = container_of(fops, struct klp_ops, fops);
5151
52− bit = ftrace_test_recursion_trylock();
52+ bit = ftrace_test_recursion_trylock(ip, parent_ip);
5353 if (WARN_ON_ONCE(bit < 0))
5454 return;
5555 /*
kernel/trace/Kconfig+25−0
@@ -727,6 +727,31 @@ config TRACE_EVAL_MAP_FILE · 726 unmodified lines
727727
728728 If unsure, say N.
729729
730+config 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+
744+config 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+
730755 config GCOV_PROFILE_FTRACE
731756 bool "Enable GCOV profiling on ftrace subsystem"
732757 depends on GCOV_KERNEL
kernel/trace/Makefile+1−0
@@ -92,6 +92,7 @@ obj-$(CONFIG_DYNAMIC_EVENTS) += trace_dynevent.o · 91 unmodified lines
9292 obj-$(CONFIG_PROBE_EVENTS) += trace_probe.o
9393 obj-$(CONFIG_UPROBE_EVENTS) += trace_uprobe.o
9494 obj-$(CONFIG_BOOTTIME_TRACING) += trace_boot.o
95+obj-$(CONFIG_FTRACE_RECORD_RECURSION) += trace_recursion_record.o
9596
9697 obj-$(CONFIG_TRACEPOINT_BENCHMARK) += trace_benchmark.o
9798
kernel/trace/ftrace.c+2−2
@@ -6918,7 +6918,7 @@ __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip, · 6917 unmodified lines
69186918 struct ftrace_ops *op;
69196919 int bit;
69206920
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);
69226922 if (bit < 0)
69236923 return;
69246924
@@ -6993,7 +6993,7 @@ static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip, · 68 unmodified lines
69936993 {
69946994 int bit;
69956995
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);
69976997 if (bit < 0)
69986998 return;
69996999
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
447447 if ((unsigned long)ops->private != smp_processor_id())
448448 return;
449449
450− bit = ftrace_test_recursion_trylock();
450+ bit = ftrace_test_recursion_trylock(ip, parent_ip);
451451 if (bit < 0)
452452 return;
453453
kernel/trace/trace_functions.c+1−1
@@ -141,7 +141,7 @@ function_trace_call(unsigned long ip, unsigned long parent_ip, · 140 unmodified lines
141141 if (unlikely(!tr->function_enabled))
142142 return;
143143
144− bit = ftrace_test_recursion_trylock();
144+ bit = ftrace_test_recursion_trylock(ip, parent_ip);
145145 if (bit < 0)
146146 return;
147147
kernel/trace/trace_output.c+3−3
@@ -353,8 +353,8 @@ static inline const char *kretprobed(const char *name) · 352 unmodified lines
353353 }
354354 #endif /* CONFIG_KRETPROBES */
355355
356−static void
357−seq_print_sym(struct trace_seq *s, unsigned long address, bool offset)
356+void
357+trace_seq_print_sym(struct trace_seq *s, unsigned long address, bool offset)
358358 {
359359 #ifdef CONFIG_KALLSYMS
360360 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
420420 goto out;
421421 }
422422
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);
424424
425425 if (sym_flags & TRACE_ITER_SYM_ADDR)
426426 trace_seq_printf(s, " <" IP_FMT ">", ip);
kernel/trace/trace_output.h+1−0
@@ -16,6 +16,7 @@ extern int · 15 unmodified lines
1616 seq_print_ip_sym(struct trace_seq *s, unsigned long ip,
1717 unsigned long sym_flags);
1818
19+extern void trace_seq_print_sym(struct trace_seq *s, unsigned long address, bool offset);
1920 extern int trace_print_context(struct trace_iterator *iter);
2021 extern int trace_print_lat_context(struct trace_iterator *iter);
2122
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+
11+struct recursed_functions {
12+ unsigned long ip;
13+ unsigned long parent_ip;
14+};
15+
16+static struct recursed_functions recursed_functions[CONFIG_FTRACE_RECORD_RECURSION_SIZE];
17+static 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+ */
23+static unsigned long cached_function;
24+
25+void 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+}
116+EXPORT_SYMBOL_GPL(ftrace_record_recursion);
117+
118+static DEFINE_MUTEX(recursed_function_lock);
119+static struct trace_seq *tseq;
120+
121+static 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+
141+static 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+
152+static void recursed_function_seq_stop(struct seq_file *m, void *v)
153+{
154+ kfree(tseq);
155+ mutex_unlock(&recursed_function_lock);
156+}
157+
158+static 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+
174+static 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+
181+static 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+
203+static 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+
210+static 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+
217+static 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+
236+fs_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