Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 1 | /* |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 2 | * trace event based perf event profiling/tracing |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2009 Red Hat Inc, Peter Zijlstra <pzijlstr@redhat.com> |
Frederic Weisbecker | c530665 | 2010-03-03 07:16:16 +0100 | [diff] [blame] | 5 | * Copyright (C) 2009-2010 Frederic Weisbecker <fweisbec@gmail.com> |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
Li Zefan | 558e654 | 2009-08-24 12:19:47 +0800 | [diff] [blame] | 8 | #include <linux/module.h> |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 9 | #include <linux/kprobes.h> |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 10 | #include "trace.h" |
| 11 | |
Frederic Weisbecker | dcd5c16 | 2010-03-16 01:05:02 +0100 | [diff] [blame] | 12 | EXPORT_SYMBOL_GPL(perf_arch_fetch_caller_regs); |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 13 | |
Peter Zijlstra | b7e2ece | 2010-05-19 10:52:27 +0200 | [diff] [blame^] | 14 | static char *perf_trace_buf[4]; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 15 | |
Frederic Weisbecker | eb1e796 | 2010-03-23 00:08:59 +0100 | [diff] [blame] | 16 | /* |
| 17 | * Force it to be aligned to unsigned long to avoid misaligned accesses |
| 18 | * suprises |
| 19 | */ |
| 20 | typedef typeof(unsigned long [PERF_MAX_TRACE_SIZE / sizeof(unsigned long)]) |
| 21 | perf_trace_t; |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 22 | |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 23 | /* Count the events in use (per event id, not per instance) */ |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 24 | static int total_ref_count; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 25 | |
Peter Zijlstra | 4f41c01 | 2010-05-18 18:08:32 +0200 | [diff] [blame] | 26 | static int perf_trace_event_enable(struct ftrace_event_call *event, void *data) |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 27 | { |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 28 | int ret = -ENOMEM; |
| 29 | |
Peter Zijlstra | 4f41c01 | 2010-05-18 18:08:32 +0200 | [diff] [blame] | 30 | if (event->perf_refcount++ > 0) { |
| 31 | event->perf_data = NULL; |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 32 | return 0; |
Peter Zijlstra | 4f41c01 | 2010-05-18 18:08:32 +0200 | [diff] [blame] | 33 | } |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 34 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 35 | if (!total_ref_count) { |
Peter Zijlstra | b7e2ece | 2010-05-19 10:52:27 +0200 | [diff] [blame^] | 36 | char *buf; |
| 37 | int i; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 38 | |
Peter Zijlstra | b7e2ece | 2010-05-19 10:52:27 +0200 | [diff] [blame^] | 39 | for (i = 0; i < 4; i++) { |
| 40 | buf = (char *)alloc_percpu(perf_trace_t); |
| 41 | if (!buf) |
| 42 | goto fail_buf; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 43 | |
Peter Zijlstra | b7e2ece | 2010-05-19 10:52:27 +0200 | [diff] [blame^] | 44 | rcu_assign_pointer(perf_trace_buf[i], buf); |
| 45 | } |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 46 | } |
| 47 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 48 | ret = event->perf_event_enable(event); |
Frederic Weisbecker | fe8e5b5 | 2009-10-03 14:55:18 +0200 | [diff] [blame] | 49 | if (!ret) { |
Peter Zijlstra | 4f41c01 | 2010-05-18 18:08:32 +0200 | [diff] [blame] | 50 | event->perf_data = data; |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 51 | total_ref_count++; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 52 | return 0; |
Frederic Weisbecker | fe8e5b5 | 2009-10-03 14:55:18 +0200 | [diff] [blame] | 53 | } |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 54 | |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 55 | fail_buf: |
Peter Zijlstra | b7e2ece | 2010-05-19 10:52:27 +0200 | [diff] [blame^] | 56 | if (!total_ref_count) { |
| 57 | int i; |
| 58 | |
| 59 | for (i = 0; i < 4; i++) { |
| 60 | free_percpu(perf_trace_buf[i]); |
| 61 | perf_trace_buf[i] = NULL; |
| 62 | } |
| 63 | } |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 64 | event->perf_refcount--; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 65 | |
| 66 | return ret; |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 67 | } |
| 68 | |
Peter Zijlstra | 4f41c01 | 2010-05-18 18:08:32 +0200 | [diff] [blame] | 69 | int perf_trace_enable(int event_id, void *data) |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 70 | { |
| 71 | struct ftrace_event_call *event; |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 72 | int ret = -EINVAL; |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 73 | |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 74 | mutex_lock(&event_mutex); |
Steven Rostedt | a59fd60 | 2009-04-10 13:52:20 -0400 | [diff] [blame] | 75 | list_for_each_entry(event, &ftrace_events, list) { |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 76 | if (event->id == event_id && event->perf_event_enable && |
Li Zefan | 558e654 | 2009-08-24 12:19:47 +0800 | [diff] [blame] | 77 | try_module_get(event->mod)) { |
Peter Zijlstra | 4f41c01 | 2010-05-18 18:08:32 +0200 | [diff] [blame] | 78 | ret = perf_trace_event_enable(event, data); |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 79 | break; |
| 80 | } |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 81 | } |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 82 | mutex_unlock(&event_mutex); |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 83 | |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 84 | return ret; |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 85 | } |
| 86 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 87 | static void perf_trace_event_disable(struct ftrace_event_call *event) |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 88 | { |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 89 | if (--event->perf_refcount > 0) |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 90 | return; |
| 91 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 92 | event->perf_event_disable(event); |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 93 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 94 | if (!--total_ref_count) { |
Peter Zijlstra | b7e2ece | 2010-05-19 10:52:27 +0200 | [diff] [blame^] | 95 | char *buf[4]; |
| 96 | int i; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 97 | |
Peter Zijlstra | b7e2ece | 2010-05-19 10:52:27 +0200 | [diff] [blame^] | 98 | for (i = 0; i < 4; i++) { |
| 99 | buf[i] = perf_trace_buf[i]; |
| 100 | rcu_assign_pointer(perf_trace_buf[i], NULL); |
| 101 | } |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 102 | |
| 103 | /* |
| 104 | * Ensure every events in profiling have finished before |
| 105 | * releasing the buffers |
| 106 | */ |
| 107 | synchronize_sched(); |
| 108 | |
Peter Zijlstra | b7e2ece | 2010-05-19 10:52:27 +0200 | [diff] [blame^] | 109 | for (i = 0; i < 4; i++) |
| 110 | free_percpu(buf[i]); |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 111 | } |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 112 | } |
| 113 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 114 | void perf_trace_disable(int event_id) |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 115 | { |
| 116 | struct ftrace_event_call *event; |
| 117 | |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 118 | mutex_lock(&event_mutex); |
Steven Rostedt | a59fd60 | 2009-04-10 13:52:20 -0400 | [diff] [blame] | 119 | list_for_each_entry(event, &ftrace_events, list) { |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 120 | if (event->id == event_id) { |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 121 | perf_trace_event_disable(event); |
Li Zefan | 558e654 | 2009-08-24 12:19:47 +0800 | [diff] [blame] | 122 | module_put(event->mod); |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 123 | break; |
| 124 | } |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 125 | } |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 126 | mutex_unlock(&event_mutex); |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 127 | } |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 128 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 129 | __kprobes void *perf_trace_buf_prepare(int size, unsigned short type, |
Peter Zijlstra | b7e2ece | 2010-05-19 10:52:27 +0200 | [diff] [blame^] | 130 | struct pt_regs *regs, int *rctxp) |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 131 | { |
| 132 | struct trace_entry *entry; |
| 133 | char *trace_buf, *raw_data; |
Peter Zijlstra | b7e2ece | 2010-05-19 10:52:27 +0200 | [diff] [blame^] | 134 | int pc; |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 135 | |
Frederic Weisbecker | eb1e796 | 2010-03-23 00:08:59 +0100 | [diff] [blame] | 136 | BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(unsigned long)); |
| 137 | |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 138 | pc = preempt_count(); |
| 139 | |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 140 | *rctxp = perf_swevent_get_recursion_context(); |
| 141 | if (*rctxp < 0) |
| 142 | goto err_recursion; |
| 143 | |
Peter Zijlstra | b7e2ece | 2010-05-19 10:52:27 +0200 | [diff] [blame^] | 144 | trace_buf = rcu_dereference_sched(perf_trace_buf[*rctxp]); |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 145 | if (!trace_buf) |
| 146 | goto err; |
| 147 | |
Peter Zijlstra | b7e2ece | 2010-05-19 10:52:27 +0200 | [diff] [blame^] | 148 | raw_data = per_cpu_ptr(trace_buf, smp_processor_id()); |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 149 | |
| 150 | /* zero the dead bytes from align to not leak stack to user */ |
Frederic Weisbecker | eb1e796 | 2010-03-23 00:08:59 +0100 | [diff] [blame] | 151 | memset(&raw_data[size - sizeof(u64)], 0, sizeof(u64)); |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 152 | |
| 153 | entry = (struct trace_entry *)raw_data; |
Peter Zijlstra | b7e2ece | 2010-05-19 10:52:27 +0200 | [diff] [blame^] | 154 | tracing_generic_entry_update(entry, regs->flags, pc); |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 155 | entry->type = type; |
| 156 | |
| 157 | return raw_data; |
| 158 | err: |
| 159 | perf_swevent_put_recursion_context(*rctxp); |
| 160 | err_recursion: |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 161 | return NULL; |
| 162 | } |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 163 | EXPORT_SYMBOL_GPL(perf_trace_buf_prepare); |