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 | c530665 | 2010-03-03 07:16:16 +0100 | [diff] [blame] | 12 | DEFINE_PER_CPU(struct pt_regs, perf_trace_regs); |
Xiao Guangrong | 639fe4b | 2010-03-11 15:30:35 +0800 | [diff] [blame] | 13 | EXPORT_PER_CPU_SYMBOL_GPL(perf_trace_regs); |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 14 | |
Frederic Weisbecker | dcd5c16 | 2010-03-16 01:05:02 +0100 | [diff] [blame] | 15 | EXPORT_SYMBOL_GPL(perf_arch_fetch_caller_regs); |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 16 | |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 17 | static char *perf_trace_buf; |
| 18 | static char *perf_trace_buf_nmi; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 19 | |
Frederic Weisbecker | eb1e796 | 2010-03-23 00:08:59 +0100 | [diff] [blame] | 20 | /* |
| 21 | * Force it to be aligned to unsigned long to avoid misaligned accesses |
| 22 | * suprises |
| 23 | */ |
| 24 | typedef typeof(unsigned long [PERF_MAX_TRACE_SIZE / sizeof(unsigned long)]) |
| 25 | perf_trace_t; |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 26 | |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 27 | /* Count the events in use (per event id, not per instance) */ |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 28 | static int total_ref_count; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 29 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 30 | static int perf_trace_event_enable(struct ftrace_event_call *event) |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 31 | { |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 32 | char *buf; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 33 | int ret = -ENOMEM; |
| 34 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 35 | if (event->perf_refcount++ > 0) |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 36 | return 0; |
| 37 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 38 | if (!total_ref_count) { |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 39 | buf = (char *)alloc_percpu(perf_trace_t); |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 40 | if (!buf) |
| 41 | goto fail_buf; |
| 42 | |
Frederic Weisbecker | 444a2a3 | 2009-11-06 04:13:05 +0100 | [diff] [blame] | 43 | rcu_assign_pointer(perf_trace_buf, buf); |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 44 | |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 45 | buf = (char *)alloc_percpu(perf_trace_t); |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 46 | if (!buf) |
| 47 | goto fail_buf_nmi; |
| 48 | |
Frederic Weisbecker | 444a2a3 | 2009-11-06 04:13:05 +0100 | [diff] [blame] | 49 | rcu_assign_pointer(perf_trace_buf_nmi, buf); |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 50 | } |
| 51 | |
Steven Rostedt | 2239291 | 2010-04-21 12:27:06 -0400 | [diff] [blame] | 52 | if (event->class->reg) |
| 53 | ret = event->class->reg(event, TRACE_REG_PERF_REGISTER); |
| 54 | else |
| 55 | ret = tracepoint_probe_register(event->name, |
| 56 | event->class->perf_probe, |
| 57 | event); |
Frederic Weisbecker | fe8e5b5 | 2009-10-03 14:55:18 +0200 | [diff] [blame] | 58 | if (!ret) { |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 59 | total_ref_count++; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 60 | return 0; |
Frederic Weisbecker | fe8e5b5 | 2009-10-03 14:55:18 +0200 | [diff] [blame] | 61 | } |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 62 | |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 63 | fail_buf_nmi: |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 64 | if (!total_ref_count) { |
Frederic Weisbecker | 444a2a3 | 2009-11-06 04:13:05 +0100 | [diff] [blame] | 65 | free_percpu(perf_trace_buf_nmi); |
| 66 | free_percpu(perf_trace_buf); |
| 67 | perf_trace_buf_nmi = NULL; |
| 68 | perf_trace_buf = NULL; |
Frederic Weisbecker | fe8e5b5 | 2009-10-03 14:55:18 +0200 | [diff] [blame] | 69 | } |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 70 | fail_buf: |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 71 | event->perf_refcount--; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 72 | |
| 73 | return ret; |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 74 | } |
| 75 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 76 | int perf_trace_enable(int event_id) |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 77 | { |
| 78 | struct ftrace_event_call *event; |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 79 | int ret = -EINVAL; |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 80 | |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 81 | mutex_lock(&event_mutex); |
Steven Rostedt | a59fd60 | 2009-04-10 13:52:20 -0400 | [diff] [blame] | 82 | list_for_each_entry(event, &ftrace_events, list) { |
Steven Rostedt | 32c0eda | 2010-04-23 10:38:03 -0400 | [diff] [blame] | 83 | if (event->event.type == event_id && |
Steven Rostedt | 2239291 | 2010-04-21 12:27:06 -0400 | [diff] [blame] | 84 | event->class && event->class->perf_probe && |
Li Zefan | 558e654 | 2009-08-24 12:19:47 +0800 | [diff] [blame] | 85 | try_module_get(event->mod)) { |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 86 | ret = perf_trace_event_enable(event); |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 87 | break; |
| 88 | } |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 89 | } |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 90 | mutex_unlock(&event_mutex); |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 91 | |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 92 | return ret; |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 95 | static void perf_trace_event_disable(struct ftrace_event_call *event) |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 96 | { |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 97 | char *buf, *nmi_buf; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 98 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 99 | if (--event->perf_refcount > 0) |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 100 | return; |
| 101 | |
Steven Rostedt | 2239291 | 2010-04-21 12:27:06 -0400 | [diff] [blame] | 102 | if (event->class->reg) |
| 103 | event->class->reg(event, TRACE_REG_PERF_UNREGISTER); |
| 104 | else |
| 105 | tracepoint_probe_unregister(event->name, event->class->perf_probe, event); |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 106 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 107 | if (!--total_ref_count) { |
Frederic Weisbecker | 444a2a3 | 2009-11-06 04:13:05 +0100 | [diff] [blame] | 108 | buf = perf_trace_buf; |
| 109 | rcu_assign_pointer(perf_trace_buf, NULL); |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 110 | |
Frederic Weisbecker | 444a2a3 | 2009-11-06 04:13:05 +0100 | [diff] [blame] | 111 | nmi_buf = perf_trace_buf_nmi; |
| 112 | rcu_assign_pointer(perf_trace_buf_nmi, NULL); |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 113 | |
| 114 | /* |
| 115 | * Ensure every events in profiling have finished before |
| 116 | * releasing the buffers |
| 117 | */ |
| 118 | synchronize_sched(); |
| 119 | |
| 120 | free_percpu(buf); |
| 121 | free_percpu(nmi_buf); |
| 122 | } |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 123 | } |
| 124 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 125 | void perf_trace_disable(int event_id) |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 126 | { |
| 127 | struct ftrace_event_call *event; |
| 128 | |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 129 | mutex_lock(&event_mutex); |
Steven Rostedt | a59fd60 | 2009-04-10 13:52:20 -0400 | [diff] [blame] | 130 | list_for_each_entry(event, &ftrace_events, list) { |
Steven Rostedt | 32c0eda | 2010-04-23 10:38:03 -0400 | [diff] [blame] | 131 | if (event->event.type == event_id) { |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 132 | perf_trace_event_disable(event); |
Li Zefan | 558e654 | 2009-08-24 12:19:47 +0800 | [diff] [blame] | 133 | module_put(event->mod); |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 134 | break; |
| 135 | } |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 136 | } |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 137 | mutex_unlock(&event_mutex); |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 138 | } |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 139 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 140 | __kprobes void *perf_trace_buf_prepare(int size, unsigned short type, |
| 141 | int *rctxp, unsigned long *irq_flags) |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 142 | { |
| 143 | struct trace_entry *entry; |
| 144 | char *trace_buf, *raw_data; |
| 145 | int pc, cpu; |
| 146 | |
Frederic Weisbecker | eb1e796 | 2010-03-23 00:08:59 +0100 | [diff] [blame] | 147 | BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(unsigned long)); |
| 148 | |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 149 | pc = preempt_count(); |
| 150 | |
| 151 | /* Protect the per cpu buffer, begin the rcu read side */ |
| 152 | local_irq_save(*irq_flags); |
| 153 | |
| 154 | *rctxp = perf_swevent_get_recursion_context(); |
| 155 | if (*rctxp < 0) |
| 156 | goto err_recursion; |
| 157 | |
| 158 | cpu = smp_processor_id(); |
| 159 | |
| 160 | if (in_nmi()) |
Paul E. McKenney | 8d53dd5 | 2010-03-03 17:50:18 -0800 | [diff] [blame] | 161 | trace_buf = rcu_dereference_sched(perf_trace_buf_nmi); |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 162 | else |
Paul E. McKenney | 8d53dd5 | 2010-03-03 17:50:18 -0800 | [diff] [blame] | 163 | trace_buf = rcu_dereference_sched(perf_trace_buf); |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 164 | |
| 165 | if (!trace_buf) |
| 166 | goto err; |
| 167 | |
| 168 | raw_data = per_cpu_ptr(trace_buf, cpu); |
| 169 | |
| 170 | /* zero the dead bytes from align to not leak stack to user */ |
Frederic Weisbecker | eb1e796 | 2010-03-23 00:08:59 +0100 | [diff] [blame] | 171 | memset(&raw_data[size - sizeof(u64)], 0, sizeof(u64)); |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 172 | |
| 173 | entry = (struct trace_entry *)raw_data; |
| 174 | tracing_generic_entry_update(entry, *irq_flags, pc); |
| 175 | entry->type = type; |
| 176 | |
| 177 | return raw_data; |
| 178 | err: |
| 179 | perf_swevent_put_recursion_context(*rctxp); |
| 180 | err_recursion: |
| 181 | local_irq_restore(*irq_flags); |
| 182 | return NULL; |
| 183 | } |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 184 | EXPORT_SYMBOL_GPL(perf_trace_buf_prepare); |