diff options
Diffstat (limited to 'kernel/trace/trace.c')
-rw-r--r-- | kernel/trace/trace.c | 144 |
1 files changed, 70 insertions, 74 deletions
diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index 21153e64bf1c..ec439999f387 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c | |||
@@ -159,6 +159,8 @@ static union trace_eval_map_item *trace_eval_maps; | |||
159 | #endif /* CONFIG_TRACE_EVAL_MAP_FILE */ | 159 | #endif /* CONFIG_TRACE_EVAL_MAP_FILE */ |
160 | 160 | ||
161 | static int tracing_set_tracer(struct trace_array *tr, const char *buf); | 161 | static int tracing_set_tracer(struct trace_array *tr, const char *buf); |
162 | static void ftrace_trace_userstack(struct ring_buffer *buffer, | ||
163 | unsigned long flags, int pc); | ||
162 | 164 | ||
163 | #define MAX_TRACER_SIZE 100 | 165 | #define MAX_TRACER_SIZE 100 |
164 | static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata; | 166 | static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata; |
@@ -496,8 +498,10 @@ int trace_pid_write(struct trace_pid_list *filtered_pids, | |||
496 | * not modified. | 498 | * not modified. |
497 | */ | 499 | */ |
498 | pid_list = kmalloc(sizeof(*pid_list), GFP_KERNEL); | 500 | pid_list = kmalloc(sizeof(*pid_list), GFP_KERNEL); |
499 | if (!pid_list) | 501 | if (!pid_list) { |
502 | trace_parser_put(&parser); | ||
500 | return -ENOMEM; | 503 | return -ENOMEM; |
504 | } | ||
501 | 505 | ||
502 | pid_list->pid_max = READ_ONCE(pid_max); | 506 | pid_list->pid_max = READ_ONCE(pid_max); |
503 | 507 | ||
@@ -507,6 +511,7 @@ int trace_pid_write(struct trace_pid_list *filtered_pids, | |||
507 | 511 | ||
508 | pid_list->pids = vzalloc((pid_list->pid_max + 7) >> 3); | 512 | pid_list->pids = vzalloc((pid_list->pid_max + 7) >> 3); |
509 | if (!pid_list->pids) { | 513 | if (!pid_list->pids) { |
514 | trace_parser_put(&parser); | ||
510 | kfree(pid_list); | 515 | kfree(pid_list); |
511 | return -ENOMEM; | 516 | return -ENOMEM; |
512 | } | 517 | } |
@@ -2749,12 +2754,21 @@ trace_function(struct trace_array *tr, | |||
2749 | 2754 | ||
2750 | #ifdef CONFIG_STACKTRACE | 2755 | #ifdef CONFIG_STACKTRACE |
2751 | 2756 | ||
2752 | #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long)) | 2757 | /* Allow 4 levels of nesting: normal, softirq, irq, NMI */ |
2758 | #define FTRACE_KSTACK_NESTING 4 | ||
2759 | |||
2760 | #define FTRACE_KSTACK_ENTRIES (PAGE_SIZE / FTRACE_KSTACK_NESTING) | ||
2761 | |||
2753 | struct ftrace_stack { | 2762 | struct ftrace_stack { |
2754 | unsigned long calls[FTRACE_STACK_MAX_ENTRIES]; | 2763 | unsigned long calls[FTRACE_KSTACK_ENTRIES]; |
2755 | }; | 2764 | }; |
2756 | 2765 | ||
2757 | static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack); | 2766 | |
2767 | struct ftrace_stacks { | ||
2768 | struct ftrace_stack stacks[FTRACE_KSTACK_NESTING]; | ||
2769 | }; | ||
2770 | |||
2771 | static DEFINE_PER_CPU(struct ftrace_stacks, ftrace_stacks); | ||
2758 | static DEFINE_PER_CPU(int, ftrace_stack_reserve); | 2772 | static DEFINE_PER_CPU(int, ftrace_stack_reserve); |
2759 | 2773 | ||
2760 | static void __ftrace_trace_stack(struct ring_buffer *buffer, | 2774 | static void __ftrace_trace_stack(struct ring_buffer *buffer, |
@@ -2763,13 +2777,10 @@ static void __ftrace_trace_stack(struct ring_buffer *buffer, | |||
2763 | { | 2777 | { |
2764 | struct trace_event_call *call = &event_kernel_stack; | 2778 | struct trace_event_call *call = &event_kernel_stack; |
2765 | struct ring_buffer_event *event; | 2779 | struct ring_buffer_event *event; |
2780 | unsigned int size, nr_entries; | ||
2781 | struct ftrace_stack *fstack; | ||
2766 | struct stack_entry *entry; | 2782 | struct stack_entry *entry; |
2767 | struct stack_trace trace; | 2783 | int stackidx; |
2768 | int use_stack; | ||
2769 | int size = FTRACE_STACK_ENTRIES; | ||
2770 | |||
2771 | trace.nr_entries = 0; | ||
2772 | trace.skip = skip; | ||
2773 | 2784 | ||
2774 | /* | 2785 | /* |
2775 | * Add one, for this function and the call to save_stack_trace() | 2786 | * Add one, for this function and the call to save_stack_trace() |
@@ -2777,7 +2788,7 @@ static void __ftrace_trace_stack(struct ring_buffer *buffer, | |||
2777 | */ | 2788 | */ |
2778 | #ifndef CONFIG_UNWINDER_ORC | 2789 | #ifndef CONFIG_UNWINDER_ORC |
2779 | if (!regs) | 2790 | if (!regs) |
2780 | trace.skip++; | 2791 | skip++; |
2781 | #endif | 2792 | #endif |
2782 | 2793 | ||
2783 | /* | 2794 | /* |
@@ -2788,53 +2799,40 @@ static void __ftrace_trace_stack(struct ring_buffer *buffer, | |||
2788 | */ | 2799 | */ |
2789 | preempt_disable_notrace(); | 2800 | preempt_disable_notrace(); |
2790 | 2801 | ||
2791 | use_stack = __this_cpu_inc_return(ftrace_stack_reserve); | 2802 | stackidx = __this_cpu_inc_return(ftrace_stack_reserve) - 1; |
2803 | |||
2804 | /* This should never happen. If it does, yell once and skip */ | ||
2805 | if (WARN_ON_ONCE(stackidx > FTRACE_KSTACK_NESTING)) | ||
2806 | goto out; | ||
2807 | |||
2792 | /* | 2808 | /* |
2793 | * We don't need any atomic variables, just a barrier. | 2809 | * The above __this_cpu_inc_return() is 'atomic' cpu local. An |
2794 | * If an interrupt comes in, we don't care, because it would | 2810 | * interrupt will either see the value pre increment or post |
2795 | * have exited and put the counter back to what we want. | 2811 | * increment. If the interrupt happens pre increment it will have |
2796 | * We just need a barrier to keep gcc from moving things | 2812 | * restored the counter when it returns. We just need a barrier to |
2797 | * around. | 2813 | * keep gcc from moving things around. |
2798 | */ | 2814 | */ |
2799 | barrier(); | 2815 | barrier(); |
2800 | if (use_stack == 1) { | ||
2801 | trace.entries = this_cpu_ptr(ftrace_stack.calls); | ||
2802 | trace.max_entries = FTRACE_STACK_MAX_ENTRIES; | ||
2803 | 2816 | ||
2804 | if (regs) | 2817 | fstack = this_cpu_ptr(ftrace_stacks.stacks) + stackidx; |
2805 | save_stack_trace_regs(regs, &trace); | 2818 | size = ARRAY_SIZE(fstack->calls); |
2806 | else | ||
2807 | save_stack_trace(&trace); | ||
2808 | |||
2809 | if (trace.nr_entries > size) | ||
2810 | size = trace.nr_entries; | ||
2811 | } else | ||
2812 | /* From now on, use_stack is a boolean */ | ||
2813 | use_stack = 0; | ||
2814 | 2819 | ||
2815 | size *= sizeof(unsigned long); | 2820 | if (regs) { |
2821 | nr_entries = stack_trace_save_regs(regs, fstack->calls, | ||
2822 | size, skip); | ||
2823 | } else { | ||
2824 | nr_entries = stack_trace_save(fstack->calls, size, skip); | ||
2825 | } | ||
2816 | 2826 | ||
2827 | size = nr_entries * sizeof(unsigned long); | ||
2817 | event = __trace_buffer_lock_reserve(buffer, TRACE_STACK, | 2828 | event = __trace_buffer_lock_reserve(buffer, TRACE_STACK, |
2818 | sizeof(*entry) + size, flags, pc); | 2829 | sizeof(*entry) + size, flags, pc); |
2819 | if (!event) | 2830 | if (!event) |
2820 | goto out; | 2831 | goto out; |
2821 | entry = ring_buffer_event_data(event); | 2832 | entry = ring_buffer_event_data(event); |
2822 | 2833 | ||
2823 | memset(&entry->caller, 0, size); | 2834 | memcpy(&entry->caller, fstack->calls, size); |
2824 | 2835 | entry->size = nr_entries; | |
2825 | if (use_stack) | ||
2826 | memcpy(&entry->caller, trace.entries, | ||
2827 | trace.nr_entries * sizeof(unsigned long)); | ||
2828 | else { | ||
2829 | trace.max_entries = FTRACE_STACK_ENTRIES; | ||
2830 | trace.entries = entry->caller; | ||
2831 | if (regs) | ||
2832 | save_stack_trace_regs(regs, &trace); | ||
2833 | else | ||
2834 | save_stack_trace(&trace); | ||
2835 | } | ||
2836 | |||
2837 | entry->size = trace.nr_entries; | ||
2838 | 2836 | ||
2839 | if (!call_filter_check_discard(call, entry, buffer, event)) | 2837 | if (!call_filter_check_discard(call, entry, buffer, event)) |
2840 | __buffer_unlock_commit(buffer, event); | 2838 | __buffer_unlock_commit(buffer, event); |
@@ -2904,15 +2902,15 @@ void trace_dump_stack(int skip) | |||
2904 | } | 2902 | } |
2905 | EXPORT_SYMBOL_GPL(trace_dump_stack); | 2903 | EXPORT_SYMBOL_GPL(trace_dump_stack); |
2906 | 2904 | ||
2905 | #ifdef CONFIG_USER_STACKTRACE_SUPPORT | ||
2907 | static DEFINE_PER_CPU(int, user_stack_count); | 2906 | static DEFINE_PER_CPU(int, user_stack_count); |
2908 | 2907 | ||
2909 | void | 2908 | static void |
2910 | ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc) | 2909 | ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc) |
2911 | { | 2910 | { |
2912 | struct trace_event_call *call = &event_user_stack; | 2911 | struct trace_event_call *call = &event_user_stack; |
2913 | struct ring_buffer_event *event; | 2912 | struct ring_buffer_event *event; |
2914 | struct userstack_entry *entry; | 2913 | struct userstack_entry *entry; |
2915 | struct stack_trace trace; | ||
2916 | 2914 | ||
2917 | if (!(global_trace.trace_flags & TRACE_ITER_USERSTACKTRACE)) | 2915 | if (!(global_trace.trace_flags & TRACE_ITER_USERSTACKTRACE)) |
2918 | return; | 2916 | return; |
@@ -2943,12 +2941,7 @@ ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc) | |||
2943 | entry->tgid = current->tgid; | 2941 | entry->tgid = current->tgid; |
2944 | memset(&entry->caller, 0, sizeof(entry->caller)); | 2942 | memset(&entry->caller, 0, sizeof(entry->caller)); |
2945 | 2943 | ||
2946 | trace.nr_entries = 0; | 2944 | stack_trace_save_user(entry->caller, FTRACE_STACK_ENTRIES); |
2947 | trace.max_entries = FTRACE_STACK_ENTRIES; | ||
2948 | trace.skip = 0; | ||
2949 | trace.entries = entry->caller; | ||
2950 | |||
2951 | save_stack_trace_user(&trace); | ||
2952 | if (!call_filter_check_discard(call, entry, buffer, event)) | 2945 | if (!call_filter_check_discard(call, entry, buffer, event)) |
2953 | __buffer_unlock_commit(buffer, event); | 2946 | __buffer_unlock_commit(buffer, event); |
2954 | 2947 | ||
@@ -2957,13 +2950,12 @@ ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc) | |||
2957 | out: | 2950 | out: |
2958 | preempt_enable(); | 2951 | preempt_enable(); |
2959 | } | 2952 | } |
2960 | 2953 | #else /* CONFIG_USER_STACKTRACE_SUPPORT */ | |
2961 | #ifdef UNUSED | 2954 | static void ftrace_trace_userstack(struct ring_buffer *buffer, |
2962 | static void __trace_userstack(struct trace_array *tr, unsigned long flags) | 2955 | unsigned long flags, int pc) |
2963 | { | 2956 | { |
2964 | ftrace_trace_userstack(tr, flags, preempt_count()); | ||
2965 | } | 2957 | } |
2966 | #endif /* UNUSED */ | 2958 | #endif /* !CONFIG_USER_STACKTRACE_SUPPORT */ |
2967 | 2959 | ||
2968 | #endif /* CONFIG_STACKTRACE */ | 2960 | #endif /* CONFIG_STACKTRACE */ |
2969 | 2961 | ||
@@ -7025,35 +7017,43 @@ struct buffer_ref { | |||
7025 | struct ring_buffer *buffer; | 7017 | struct ring_buffer *buffer; |
7026 | void *page; | 7018 | void *page; |
7027 | int cpu; | 7019 | int cpu; |
7028 | int ref; | 7020 | refcount_t refcount; |
7029 | }; | 7021 | }; |
7030 | 7022 | ||
7023 | static void buffer_ref_release(struct buffer_ref *ref) | ||
7024 | { | ||
7025 | if (!refcount_dec_and_test(&ref->refcount)) | ||
7026 | return; | ||
7027 | ring_buffer_free_read_page(ref->buffer, ref->cpu, ref->page); | ||
7028 | kfree(ref); | ||
7029 | } | ||
7030 | |||
7031 | static void buffer_pipe_buf_release(struct pipe_inode_info *pipe, | 7031 | static void buffer_pipe_buf_release(struct pipe_inode_info *pipe, |
7032 | struct pipe_buffer *buf) | 7032 | struct pipe_buffer *buf) |
7033 | { | 7033 | { |
7034 | struct buffer_ref *ref = (struct buffer_ref *)buf->private; | 7034 | struct buffer_ref *ref = (struct buffer_ref *)buf->private; |
7035 | 7035 | ||
7036 | if (--ref->ref) | 7036 | buffer_ref_release(ref); |
7037 | return; | ||
7038 | |||
7039 | ring_buffer_free_read_page(ref->buffer, ref->cpu, ref->page); | ||
7040 | kfree(ref); | ||
7041 | buf->private = 0; | 7037 | buf->private = 0; |
7042 | } | 7038 | } |
7043 | 7039 | ||
7044 | static void buffer_pipe_buf_get(struct pipe_inode_info *pipe, | 7040 | static bool buffer_pipe_buf_get(struct pipe_inode_info *pipe, |
7045 | struct pipe_buffer *buf) | 7041 | struct pipe_buffer *buf) |
7046 | { | 7042 | { |
7047 | struct buffer_ref *ref = (struct buffer_ref *)buf->private; | 7043 | struct buffer_ref *ref = (struct buffer_ref *)buf->private; |
7048 | 7044 | ||
7049 | ref->ref++; | 7045 | if (refcount_read(&ref->refcount) > INT_MAX/2) |
7046 | return false; | ||
7047 | |||
7048 | refcount_inc(&ref->refcount); | ||
7049 | return true; | ||
7050 | } | 7050 | } |
7051 | 7051 | ||
7052 | /* Pipe buffer operations for a buffer. */ | 7052 | /* Pipe buffer operations for a buffer. */ |
7053 | static const struct pipe_buf_operations buffer_pipe_buf_ops = { | 7053 | static const struct pipe_buf_operations buffer_pipe_buf_ops = { |
7054 | .confirm = generic_pipe_buf_confirm, | 7054 | .confirm = generic_pipe_buf_confirm, |
7055 | .release = buffer_pipe_buf_release, | 7055 | .release = buffer_pipe_buf_release, |
7056 | .steal = generic_pipe_buf_steal, | 7056 | .steal = generic_pipe_buf_nosteal, |
7057 | .get = buffer_pipe_buf_get, | 7057 | .get = buffer_pipe_buf_get, |
7058 | }; | 7058 | }; |
7059 | 7059 | ||
@@ -7066,11 +7066,7 @@ static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i) | |||
7066 | struct buffer_ref *ref = | 7066 | struct buffer_ref *ref = |
7067 | (struct buffer_ref *)spd->partial[i].private; | 7067 | (struct buffer_ref *)spd->partial[i].private; |
7068 | 7068 | ||
7069 | if (--ref->ref) | 7069 | buffer_ref_release(ref); |
7070 | return; | ||
7071 | |||
7072 | ring_buffer_free_read_page(ref->buffer, ref->cpu, ref->page); | ||
7073 | kfree(ref); | ||
7074 | spd->partial[i].private = 0; | 7070 | spd->partial[i].private = 0; |
7075 | } | 7071 | } |
7076 | 7072 | ||
@@ -7125,7 +7121,7 @@ tracing_buffers_splice_read(struct file *file, loff_t *ppos, | |||
7125 | break; | 7121 | break; |
7126 | } | 7122 | } |
7127 | 7123 | ||
7128 | ref->ref = 1; | 7124 | refcount_set(&ref->refcount, 1); |
7129 | ref->buffer = iter->trace_buffer->buffer; | 7125 | ref->buffer = iter->trace_buffer->buffer; |
7130 | ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file); | 7126 | ref->page = ring_buffer_alloc_read_page(ref->buffer, iter->cpu_file); |
7131 | if (IS_ERR(ref->page)) { | 7127 | if (IS_ERR(ref->page)) { |