]>
Commit | Line | Data |
---|---|---|
ac199db0 | 1 | /* |
97d5a220 | 2 | * trace event based perf event profiling/tracing |
ac199db0 PZ |
3 | * |
4 | * Copyright (C) 2009 Red Hat Inc, Peter Zijlstra <pzijlstr@redhat.com> | |
c530665c | 5 | * Copyright (C) 2009-2010 Frederic Weisbecker <fweisbec@gmail.com> |
ac199db0 PZ |
6 | */ |
7 | ||
558e6547 | 8 | #include <linux/module.h> |
430ad5a6 | 9 | #include <linux/kprobes.h> |
ac199db0 PZ |
10 | #include "trace.h" |
11 | ||
b7e2ecef | 12 | static char *perf_trace_buf[4]; |
20ab4425 | 13 | |
eb1e7961 FW |
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; | |
ce71b9df | 20 | |
20ab4425 | 21 | /* Count the events in use (per event id, not per instance) */ |
97d5a220 | 22 | static int total_ref_count; |
20ab4425 | 23 | |
1c024eca PZ |
24 | static int perf_trace_event_init(struct ftrace_event_call *tp_event, |
25 | struct perf_event *p_event) | |
e5e25cf4 | 26 | { |
1c024eca | 27 | struct hlist_head *list; |
20ab4425 | 28 | int ret = -ENOMEM; |
1c024eca | 29 | int cpu; |
20ab4425 | 30 | |
1c024eca PZ |
31 | p_event->tp_event = tp_event; |
32 | if (tp_event->perf_refcount++ > 0) | |
e5e25cf4 FW |
33 | return 0; |
34 | ||
1c024eca PZ |
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)); | |
20ab4425 | 41 | |
1c024eca | 42 | tp_event->perf_events = list; |
e5e25cf4 | 43 | |
97d5a220 | 44 | if (!total_ref_count) { |
b7e2ecef PZ |
45 | char *buf; |
46 | int i; | |
20ab4425 | 47 | |
b7e2ecef PZ |
48 | for (i = 0; i < 4; i++) { |
49 | buf = (char *)alloc_percpu(perf_trace_t); | |
50 | if (!buf) | |
1c024eca | 51 | goto fail; |
20ab4425 | 52 | |
1c024eca | 53 | perf_trace_buf[i] = buf; |
b7e2ecef | 54 | } |
20ab4425 FW |
55 | } |
56 | ||
a1d0ce82 | 57 | ret = tp_event->class->reg(tp_event, TRACE_REG_PERF_REGISTER); |
1c024eca PZ |
58 | if (ret) |
59 | goto fail; | |
20ab4425 | 60 | |
1c024eca PZ |
61 | total_ref_count++; |
62 | return 0; | |
63 | ||
64 | fail: | |
97d5a220 | 65 | if (!total_ref_count) { |
b7e2ecef PZ |
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 | } | |
fe8e5b5a | 72 | } |
1c024eca PZ |
73 | |
74 | if (!--tp_event->perf_refcount) { | |
75 | free_percpu(tp_event->perf_events); | |
76 | tp_event->perf_events = NULL; | |
fe8e5b5a | 77 | } |
20ab4425 FW |
78 | |
79 | return ret; | |
e5e25cf4 FW |
80 | } |
81 | ||
1c024eca | 82 | int perf_trace_init(struct perf_event *p_event) |
ac199db0 | 83 | { |
1c024eca PZ |
84 | struct ftrace_event_call *tp_event; |
85 | int event_id = p_event->attr.config; | |
20c8928a | 86 | int ret = -EINVAL; |
ac199db0 | 87 | |
20c8928a | 88 | mutex_lock(&event_mutex); |
1c024eca | 89 | list_for_each_entry(tp_event, &ftrace_events, list) { |
ff5f149b | 90 | if (tp_event->event.type == event_id && |
a1d0ce82 | 91 | tp_event->class && tp_event->class->reg && |
1c024eca PZ |
92 | try_module_get(tp_event->mod)) { |
93 | ret = perf_trace_event_init(tp_event, p_event); | |
9cb627d5 LZ |
94 | if (ret) |
95 | module_put(tp_event->mod); | |
20c8928a LZ |
96 | break; |
97 | } | |
ac199db0 | 98 | } |
20c8928a | 99 | mutex_unlock(&event_mutex); |
ac199db0 | 100 | |
20c8928a | 101 | return ret; |
ac199db0 PZ |
102 | } |
103 | ||
1c024eca | 104 | int perf_trace_enable(struct perf_event *p_event) |
e5e25cf4 | 105 | { |
1c024eca PZ |
106 | struct ftrace_event_call *tp_event = p_event->tp_event; |
107 | struct hlist_head *list; | |
20ab4425 | 108 | |
1c024eca PZ |
109 | list = tp_event->perf_events; |
110 | if (WARN_ON_ONCE(!list)) | |
111 | return -EINVAL; | |
20ab4425 | 112 | |
3771f077 | 113 | list = this_cpu_ptr(list); |
1c024eca | 114 | hlist_add_head_rcu(&p_event->hlist_entry, list); |
20ab4425 | 115 | |
1c024eca PZ |
116 | return 0; |
117 | } | |
20ab4425 | 118 | |
1c024eca PZ |
119 | void perf_trace_disable(struct perf_event *p_event) |
120 | { | |
121 | hlist_del_rcu(&p_event->hlist_entry); | |
e5e25cf4 FW |
122 | } |
123 | ||
1c024eca | 124 | void perf_trace_destroy(struct perf_event *p_event) |
ac199db0 | 125 | { |
1c024eca PZ |
126 | struct ftrace_event_call *tp_event = p_event->tp_event; |
127 | int i; | |
ac199db0 | 128 | |
2e97942f | 129 | mutex_lock(&event_mutex); |
1c024eca | 130 | if (--tp_event->perf_refcount > 0) |
2e97942f | 131 | goto out; |
1c024eca | 132 | |
a1d0ce82 | 133 | tp_event->class->reg(tp_event, TRACE_REG_PERF_UNREGISTER); |
1c024eca | 134 | |
3771f077 | 135 | /* |
669336e4 FW |
136 | * Ensure our callback won't be called anymore. The buffers |
137 | * will be freed after that. | |
3771f077 | 138 | */ |
669336e4 | 139 | tracepoint_synchronize_unregister(); |
3771f077 | 140 | |
1c024eca PZ |
141 | free_percpu(tp_event->perf_events); |
142 | tp_event->perf_events = NULL; | |
143 | ||
144 | if (!--total_ref_count) { | |
145 | for (i = 0; i < 4; i++) { | |
146 | free_percpu(perf_trace_buf[i]); | |
147 | perf_trace_buf[i] = NULL; | |
20c8928a | 148 | } |
ac199db0 | 149 | } |
2e97942f | 150 | out: |
9cb627d5 | 151 | module_put(tp_event->mod); |
2e97942f | 152 | mutex_unlock(&event_mutex); |
ac199db0 | 153 | } |
430ad5a6 | 154 | |
97d5a220 | 155 | __kprobes void *perf_trace_buf_prepare(int size, unsigned short type, |
b7e2ecef | 156 | struct pt_regs *regs, int *rctxp) |
430ad5a6 XG |
157 | { |
158 | struct trace_entry *entry; | |
87f44bbc | 159 | unsigned long flags; |
1c024eca | 160 | char *raw_data; |
b7e2ecef | 161 | int pc; |
430ad5a6 | 162 | |
eb1e7961 FW |
163 | BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(unsigned long)); |
164 | ||
430ad5a6 XG |
165 | pc = preempt_count(); |
166 | ||
430ad5a6 XG |
167 | *rctxp = perf_swevent_get_recursion_context(); |
168 | if (*rctxp < 0) | |
1c024eca | 169 | return NULL; |
430ad5a6 | 170 | |
3771f077 | 171 | raw_data = this_cpu_ptr(perf_trace_buf[*rctxp]); |
430ad5a6 XG |
172 | |
173 | /* zero the dead bytes from align to not leak stack to user */ | |
eb1e7961 | 174 | memset(&raw_data[size - sizeof(u64)], 0, sizeof(u64)); |
430ad5a6 XG |
175 | |
176 | entry = (struct trace_entry *)raw_data; | |
87f44bbc PZ |
177 | local_save_flags(flags); |
178 | tracing_generic_entry_update(entry, flags, pc); | |
430ad5a6 XG |
179 | entry->type = type; |
180 | ||
181 | return raw_data; | |
430ad5a6 | 182 | } |
97d5a220 | 183 | EXPORT_SYMBOL_GPL(perf_trace_buf_prepare); |