au1200fb: fixup PM
[linux-2.6/linux-mips.git] / kernel / trace / trace_event_perf.c
blob19a359d5e6d58573cc1c74326e488a419b01b342
1 /*
2 * trace event based perf event profiling/tracing
4 * Copyright (C) 2009 Red Hat Inc, Peter Zijlstra <pzijlstr@redhat.com>
5 * Copyright (C) 2009-2010 Frederic Weisbecker <fweisbec@gmail.com>
6 */
8 #include <linux/module.h>
9 #include <linux/kprobes.h>
10 #include "trace.h"
12 static char __percpu *perf_trace_buf[PERF_NR_CONTEXTS];
15 * Force it to be aligned to unsigned long to avoid misaligned accesses
16 * suprises
18 typedef typeof(unsigned long [PERF_MAX_TRACE_SIZE / sizeof(unsigned long)])
19 perf_trace_t;
21 /* Count the events in use (per event id, not per instance) */
22 static int total_ref_count;
24 static int perf_trace_event_perm(struct ftrace_event_call *tp_event,
25 struct perf_event *p_event)
27 /* No tracing, just counting, so no obvious leak */
28 if (!(p_event->attr.sample_type & PERF_SAMPLE_RAW))
29 return 0;
31 /* Some events are ok to be traced by non-root users... */
32 if (p_event->attach_state == PERF_ATTACH_TASK) {
33 if (tp_event->flags & TRACE_EVENT_FL_CAP_ANY)
34 return 0;
38 * ...otherwise raw tracepoint data can be a severe data leak,
39 * only allow root to have these.
41 if (perf_paranoid_tracepoint_raw() && !capable(CAP_SYS_ADMIN))
42 return -EPERM;
44 return 0;
47 static int perf_trace_event_init(struct ftrace_event_call *tp_event,
48 struct perf_event *p_event)
50 struct hlist_head __percpu *list;
51 int ret;
52 int cpu;
54 ret = perf_trace_event_perm(tp_event, p_event);
55 if (ret)
56 return ret;
58 p_event->tp_event = tp_event;
59 if (tp_event->perf_refcount++ > 0)
60 return 0;
62 ret = -ENOMEM;
64 list = alloc_percpu(struct hlist_head);
65 if (!list)
66 goto fail;
68 for_each_possible_cpu(cpu)
69 INIT_HLIST_HEAD(per_cpu_ptr(list, cpu));
71 tp_event->perf_events = list;
73 if (!total_ref_count) {
74 char __percpu *buf;
75 int i;
77 for (i = 0; i < PERF_NR_CONTEXTS; i++) {
78 buf = (char __percpu *)alloc_percpu(perf_trace_t);
79 if (!buf)
80 goto fail;
82 perf_trace_buf[i] = buf;
86 ret = tp_event->class->reg(tp_event, TRACE_REG_PERF_REGISTER);
87 if (ret)
88 goto fail;
90 total_ref_count++;
91 return 0;
93 fail:
94 if (!total_ref_count) {
95 int i;
97 for (i = 0; i < PERF_NR_CONTEXTS; i++) {
98 free_percpu(perf_trace_buf[i]);
99 perf_trace_buf[i] = NULL;
103 if (!--tp_event->perf_refcount) {
104 free_percpu(tp_event->perf_events);
105 tp_event->perf_events = NULL;
108 return ret;
111 int perf_trace_init(struct perf_event *p_event)
113 struct ftrace_event_call *tp_event;
114 int event_id = p_event->attr.config;
115 int ret = -EINVAL;
117 mutex_lock(&event_mutex);
118 list_for_each_entry(tp_event, &ftrace_events, list) {
119 if (tp_event->event.type == event_id &&
120 tp_event->class && tp_event->class->reg &&
121 try_module_get(tp_event->mod)) {
122 ret = perf_trace_event_init(tp_event, p_event);
123 if (ret)
124 module_put(tp_event->mod);
125 break;
128 mutex_unlock(&event_mutex);
130 return ret;
133 int perf_trace_add(struct perf_event *p_event, int flags)
135 struct ftrace_event_call *tp_event = p_event->tp_event;
136 struct hlist_head __percpu *pcpu_list;
137 struct hlist_head *list;
139 pcpu_list = tp_event->perf_events;
140 if (WARN_ON_ONCE(!pcpu_list))
141 return -EINVAL;
143 if (!(flags & PERF_EF_START))
144 p_event->hw.state = PERF_HES_STOPPED;
146 list = this_cpu_ptr(pcpu_list);
147 hlist_add_head_rcu(&p_event->hlist_entry, list);
149 return 0;
152 void perf_trace_del(struct perf_event *p_event, int flags)
154 hlist_del_rcu(&p_event->hlist_entry);
157 void perf_trace_destroy(struct perf_event *p_event)
159 struct ftrace_event_call *tp_event = p_event->tp_event;
160 int i;
162 mutex_lock(&event_mutex);
163 if (--tp_event->perf_refcount > 0)
164 goto out;
166 tp_event->class->reg(tp_event, TRACE_REG_PERF_UNREGISTER);
169 * Ensure our callback won't be called anymore. The buffers
170 * will be freed after that.
172 tracepoint_synchronize_unregister();
174 free_percpu(tp_event->perf_events);
175 tp_event->perf_events = NULL;
177 if (!--total_ref_count) {
178 for (i = 0; i < PERF_NR_CONTEXTS; i++) {
179 free_percpu(perf_trace_buf[i]);
180 perf_trace_buf[i] = NULL;
183 out:
184 module_put(tp_event->mod);
185 mutex_unlock(&event_mutex);
188 __kprobes void *perf_trace_buf_prepare(int size, unsigned short type,
189 struct pt_regs *regs, int *rctxp)
191 struct trace_entry *entry;
192 unsigned long flags;
193 char *raw_data;
194 int pc;
196 BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(unsigned long));
198 pc = preempt_count();
200 *rctxp = perf_swevent_get_recursion_context();
201 if (*rctxp < 0)
202 return NULL;
204 raw_data = this_cpu_ptr(perf_trace_buf[*rctxp]);
206 /* zero the dead bytes from align to not leak stack to user */
207 memset(&raw_data[size - sizeof(u64)], 0, sizeof(u64));
209 entry = (struct trace_entry *)raw_data;
210 local_save_flags(flags);
211 tracing_generic_entry_update(entry, flags, pc);
212 entry->type = type;
214 return raw_data;
216 EXPORT_SYMBOL_GPL(perf_trace_buf_prepare);