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
Unified · Split
| @@ -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 | |||
| 130 | function that the callback is tracing. Note, on success, | 130 | function that the callback is tracing. Note, on success, |
| 131 | ftrace_test_recursion_trylock() will disable preemption, and the | 131 | ftrace_test_recursion_trylock() will disable preemption, and the |
| 132 | ftrace_test_recursion_unlock() will enable it again (if it was previously | 132 | 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). | ||
| 134 | 136 | ||
| 135 | Alternatively, if the FTRACE_OPS_FL_RECURSION flag is set on the ftrace_ops | 137 | Alternatively, 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 test | 138 | (as explained below), then a helper trampoline will be used to test |
| @@ -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 | ||
| @@ -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 | ||
| @@ -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 | ||
| @@ -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 | ||
| @@ -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 | ||
| @@ -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 | ||
| @@ -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 | ||
| 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) | ||
| 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 recursion | 220 | * >= 0 if no recursion |
| 201 | */ | 221 | */ |
| 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) | ||
| 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 | /** |
| @@ -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 | /* |
| @@ -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 | ||
| 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 | |||
| 730 | config GCOV_PROFILE_FTRACE | 755 | config GCOV_PROFILE_FTRACE |
| 731 | bool "Enable GCOV profiling on ftrace subsystem" | 756 | bool "Enable GCOV profiling on ftrace subsystem" |
| 732 | depends on GCOV_KERNEL | 757 | depends on GCOV_KERNEL |
| @@ -92,6 +92,7 @@ obj-$(CONFIG_DYNAMIC_EVENTS) += trace_dynevent.o · 91 unmodified lines | |||
| 92 | obj-$(CONFIG_PROBE_EVENTS) += trace_probe.o | 92 | obj-$(CONFIG_PROBE_EVENTS) += trace_probe.o |
| 93 | obj-$(CONFIG_UPROBE_EVENTS) += trace_uprobe.o | 93 | obj-$(CONFIG_UPROBE_EVENTS) += trace_uprobe.o |
| 94 | obj-$(CONFIG_BOOTTIME_TRACING) += trace_boot.o | 94 | obj-$(CONFIG_BOOTTIME_TRACING) += trace_boot.o |
| 95 | obj-$(CONFIG_FTRACE_RECORD_RECURSION) += trace_recursion_record.o | ||
| 95 | 96 | ||
| 96 | obj-$(CONFIG_TRACEPOINT_BENCHMARK) += trace_benchmark.o | 97 | obj-$(CONFIG_TRACEPOINT_BENCHMARK) += trace_benchmark.o |
| 97 | 98 | ||
| @@ -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 | ||
| @@ -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 | ||
| @@ -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 | ||
| @@ -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 | ||
| 356 | static void | 356 | void |
| 357 | seq_print_sym(struct trace_seq *s, unsigned long address, bool offset) | 357 | trace_seq_print_sym(struct trace_seq *s, unsigned long address, bool offset) |
| 358 | { | 358 | { |
| 359 | #ifdef CONFIG_KALLSYMS | 359 | #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); |
| @@ -16,6 +16,7 @@ extern int · 15 unmodified lines | |||
| 16 | seq_print_ip_sym(struct trace_seq *s, unsigned long ip, | 16 | seq_print_ip_sym(struct trace_seq *s, unsigned long ip, |
| 17 | unsigned long sym_flags); | 17 | unsigned long sym_flags); |
| 18 | 18 | ||
| 19 | extern void trace_seq_print_sym(struct trace_seq *s, unsigned long address, bool offset); | ||
| 19 | extern int trace_print_context(struct trace_iterator *iter); | 20 | extern int trace_print_context(struct trace_iterator *iter); |
| 20 | extern int trace_print_lat_context(struct trace_iterator *iter); | 21 | extern int trace_print_lat_context(struct trace_iterator *iter); |
| 21 | 22 | ||
| @@ -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); | ||