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 | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 26 | static int perf_trace_event_init(struct ftrace_event_call *tp_event, |
| 27 | struct perf_event *p_event) |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 28 | { |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 29 | struct hlist_head *list; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 30 | int ret = -ENOMEM; |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 31 | int cpu; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 32 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 33 | p_event->tp_event = tp_event; |
| 34 | if (tp_event->perf_refcount++ > 0) |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 35 | return 0; |
| 36 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 37 | list = alloc_percpu(struct hlist_head); |
| 38 | if (!list) |
| 39 | goto fail; |
| 40 | |
| 41 | for_each_possible_cpu(cpu) |
| 42 | INIT_HLIST_HEAD(per_cpu_ptr(list, cpu)); |
| 43 | |
| 44 | tp_event->perf_events = list; |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 45 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 46 | if (!total_ref_count) { |
Peter Zijlstra | b7e2ece | 2010-05-19 10:52:27 +0200 | [diff] [blame] | 47 | char *buf; |
| 48 | int i; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 49 | |
Peter Zijlstra | b7e2ece | 2010-05-19 10:52:27 +0200 | [diff] [blame] | 50 | for (i = 0; i < 4; i++) { |
| 51 | buf = (char *)alloc_percpu(perf_trace_t); |
| 52 | if (!buf) |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 53 | goto fail; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 54 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 55 | perf_trace_buf[i] = buf; |
Peter Zijlstra | b7e2ece | 2010-05-19 10:52:27 +0200 | [diff] [blame] | 56 | } |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 57 | } |
| 58 | |
Steven Rostedt | ff5f149 | 2010-05-21 11:49:57 -0400 | [diff] [blame^] | 59 | if (tp_event->class->reg) |
| 60 | ret = tp_event->class->reg(tp_event, TRACE_REG_PERF_REGISTER); |
Steven Rostedt | 2239291 | 2010-04-21 12:27:06 -0400 | [diff] [blame] | 61 | else |
Steven Rostedt | ff5f149 | 2010-05-21 11:49:57 -0400 | [diff] [blame^] | 62 | ret = tracepoint_probe_register(tp_event->name, |
| 63 | tp_event->class->perf_probe, |
| 64 | tp_event); |
| 65 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 66 | if (ret) |
| 67 | goto fail; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 68 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 69 | total_ref_count++; |
| 70 | return 0; |
| 71 | |
| 72 | fail: |
Peter Zijlstra | b7e2ece | 2010-05-19 10:52:27 +0200 | [diff] [blame] | 73 | if (!total_ref_count) { |
| 74 | int i; |
| 75 | |
| 76 | for (i = 0; i < 4; i++) { |
| 77 | free_percpu(perf_trace_buf[i]); |
| 78 | perf_trace_buf[i] = NULL; |
| 79 | } |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 80 | } |
| 81 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 82 | if (!--tp_event->perf_refcount) { |
| 83 | free_percpu(tp_event->perf_events); |
| 84 | tp_event->perf_events = NULL; |
Frederic Weisbecker | fe8e5b5 | 2009-10-03 14:55:18 +0200 | [diff] [blame] | 85 | } |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 86 | |
| 87 | return ret; |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 88 | } |
| 89 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 90 | int perf_trace_init(struct perf_event *p_event) |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 91 | { |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 92 | struct ftrace_event_call *tp_event; |
| 93 | int event_id = p_event->attr.config; |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 94 | int ret = -EINVAL; |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 95 | |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 96 | mutex_lock(&event_mutex); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 97 | list_for_each_entry(tp_event, &ftrace_events, list) { |
Steven Rostedt | ff5f149 | 2010-05-21 11:49:57 -0400 | [diff] [blame^] | 98 | if (tp_event->event.type == event_id && |
| 99 | tp_event->class && tp_event->class->perf_probe && |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 100 | try_module_get(tp_event->mod)) { |
| 101 | ret = perf_trace_event_init(tp_event, p_event); |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 102 | break; |
| 103 | } |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 104 | } |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 105 | mutex_unlock(&event_mutex); |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 106 | |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 107 | return ret; |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 108 | } |
| 109 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 110 | int perf_trace_enable(struct perf_event *p_event) |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 111 | { |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 112 | struct ftrace_event_call *tp_event = p_event->tp_event; |
| 113 | struct hlist_head *list; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 114 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 115 | list = tp_event->perf_events; |
| 116 | if (WARN_ON_ONCE(!list)) |
| 117 | return -EINVAL; |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 118 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 119 | list = per_cpu_ptr(list, smp_processor_id()); |
| 120 | hlist_add_head_rcu(&p_event->hlist_entry, list); |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 121 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 122 | return 0; |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 123 | } |
| 124 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 125 | void perf_trace_disable(struct perf_event *p_event) |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 126 | { |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 127 | hlist_del_rcu(&p_event->hlist_entry); |
| 128 | } |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 129 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 130 | void perf_trace_destroy(struct perf_event *p_event) |
| 131 | { |
| 132 | struct ftrace_event_call *tp_event = p_event->tp_event; |
| 133 | int i; |
| 134 | |
| 135 | if (--tp_event->perf_refcount > 0) |
| 136 | return; |
| 137 | |
Steven Rostedt | ff5f149 | 2010-05-21 11:49:57 -0400 | [diff] [blame^] | 138 | if (tp_event->class->reg) |
| 139 | tp_event->class->reg(tp_event, TRACE_REG_PERF_UNREGISTER); |
| 140 | else |
| 141 | tracepoint_probe_unregister(tp_event->name, |
| 142 | tp_event->class->perf_probe, |
| 143 | tp_event); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 144 | |
| 145 | free_percpu(tp_event->perf_events); |
| 146 | tp_event->perf_events = NULL; |
| 147 | |
| 148 | if (!--total_ref_count) { |
| 149 | for (i = 0; i < 4; i++) { |
| 150 | free_percpu(perf_trace_buf[i]); |
| 151 | perf_trace_buf[i] = NULL; |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 152 | } |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 153 | } |
| 154 | } |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 155 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 156 | __kprobes void *perf_trace_buf_prepare(int size, unsigned short type, |
Peter Zijlstra | b7e2ece | 2010-05-19 10:52:27 +0200 | [diff] [blame] | 157 | struct pt_regs *regs, int *rctxp) |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 158 | { |
| 159 | struct trace_entry *entry; |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 160 | char *raw_data; |
Peter Zijlstra | b7e2ece | 2010-05-19 10:52:27 +0200 | [diff] [blame] | 161 | int pc; |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 162 | |
Frederic Weisbecker | eb1e796 | 2010-03-23 00:08:59 +0100 | [diff] [blame] | 163 | BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(unsigned long)); |
| 164 | |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 165 | pc = preempt_count(); |
| 166 | |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 167 | *rctxp = perf_swevent_get_recursion_context(); |
| 168 | if (*rctxp < 0) |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 169 | return NULL; |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 170 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 171 | raw_data = per_cpu_ptr(perf_trace_buf[*rctxp], smp_processor_id()); |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 172 | |
| 173 | /* zero the dead bytes from align to not leak stack to user */ |
Frederic Weisbecker | eb1e796 | 2010-03-23 00:08:59 +0100 | [diff] [blame] | 174 | memset(&raw_data[size - sizeof(u64)], 0, sizeof(u64)); |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 175 | |
| 176 | entry = (struct trace_entry *)raw_data; |
Peter Zijlstra | b7e2ece | 2010-05-19 10:52:27 +0200 | [diff] [blame] | 177 | tracing_generic_entry_update(entry, regs->flags, pc); |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 178 | entry->type = type; |
| 179 | |
| 180 | return raw_data; |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 181 | } |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 182 | EXPORT_SYMBOL_GPL(perf_trace_buf_prepare); |