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