x86, mce: Rename MSR_IA32_MCx_CTL2 value
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / trace / trace_event_perf.c
blobe6f65887842c9a91649550daee07c593c818f9b5
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 EXPORT_SYMBOL_GPL(perf_arch_fetch_caller_regs);
14 static char *perf_trace_buf[4];
17 * Force it to be aligned to unsigned long to avoid misaligned accesses
18 * suprises
20 typedef typeof(unsigned long [PERF_MAX_TRACE_SIZE / sizeof(unsigned long)])
21 perf_trace_t;
23 /* Count the events in use (per event id, not per instance) */
24 static int total_ref_count;
26 static int perf_trace_event_init(struct ftrace_event_call *tp_event,
27 struct perf_event *p_event)
29 struct hlist_head *list;
30 int ret = -ENOMEM;
31 int cpu;
33 p_event->tp_event = tp_event;
34 if (tp_event->perf_refcount++ > 0)
35 return 0;
37 list = alloc_percpu(struct hlist_head);
38 if (!list)
39 goto fail;
41 for_each_possible_cpu(cpu)
42 INIT_HLIST_HEAD(per_cpu_ptr(list, cpu));
44 tp_event->perf_events = list;
46 if (!total_ref_count) {
47 char *buf;
48 int i;
50 for (i = 0; i < 4; i++) {
51 buf = (char *)alloc_percpu(perf_trace_t);
52 if (!buf)
53 goto fail;
55 perf_trace_buf[i] = buf;
59 if (tp_event->class->reg)
60 ret = tp_event->class->reg(tp_event, TRACE_REG_PERF_REGISTER);
61 else
62 ret = tracepoint_probe_register(tp_event->name,
63 tp_event->class->perf_probe,
64 tp_event);
66 if (ret)
67 goto fail;
69 total_ref_count++;
70 return 0;
72 fail:
73 if (!total_ref_count) {
74 int i;
76 for (i = 0; i < 4; i++) {
77 free_percpu(perf_trace_buf[i]);
78 perf_trace_buf[i] = NULL;
82 if (!--tp_event->perf_refcount) {
83 free_percpu(tp_event->perf_events);
84 tp_event->perf_events = NULL;
87 return ret;
90 int perf_trace_init(struct perf_event *p_event)
92 struct ftrace_event_call *tp_event;
93 int event_id = p_event->attr.config;
94 int ret = -EINVAL;
96 mutex_lock(&event_mutex);
97 list_for_each_entry(tp_event, &ftrace_events, list) {
98 if (tp_event->event.type == event_id &&
99 tp_event->class && tp_event->class->perf_probe &&
100 try_module_get(tp_event->mod)) {
101 ret = perf_trace_event_init(tp_event, p_event);
102 break;
105 mutex_unlock(&event_mutex);
107 return ret;
110 int perf_trace_enable(struct perf_event *p_event)
112 struct ftrace_event_call *tp_event = p_event->tp_event;
113 struct hlist_head *list;
115 list = tp_event->perf_events;
116 if (WARN_ON_ONCE(!list))
117 return -EINVAL;
119 list = this_cpu_ptr(list);
120 hlist_add_head_rcu(&p_event->hlist_entry, list);
122 return 0;
125 void perf_trace_disable(struct perf_event *p_event)
127 hlist_del_rcu(&p_event->hlist_entry);
130 void perf_trace_destroy(struct perf_event *p_event)
132 struct ftrace_event_call *tp_event = p_event->tp_event;
133 int i;
135 mutex_lock(&event_mutex);
136 if (--tp_event->perf_refcount > 0)
137 goto out;
139 if (tp_event->class->reg)
140 tp_event->class->reg(tp_event, TRACE_REG_PERF_UNREGISTER);
141 else
142 tracepoint_probe_unregister(tp_event->name,
143 tp_event->class->perf_probe,
144 tp_event);
147 * Ensure our callback won't be called anymore. See
148 * tracepoint_probe_unregister() and __DO_TRACE().
150 synchronize_sched();
152 free_percpu(tp_event->perf_events);
153 tp_event->perf_events = NULL;
155 if (!--total_ref_count) {
156 for (i = 0; i < 4; i++) {
157 free_percpu(perf_trace_buf[i]);
158 perf_trace_buf[i] = NULL;
161 out:
162 mutex_unlock(&event_mutex);
165 __kprobes void *perf_trace_buf_prepare(int size, unsigned short type,
166 struct pt_regs *regs, int *rctxp)
168 struct trace_entry *entry;
169 unsigned long flags;
170 char *raw_data;
171 int pc;
173 BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(unsigned long));
175 pc = preempt_count();
177 *rctxp = perf_swevent_get_recursion_context();
178 if (*rctxp < 0)
179 return NULL;
181 raw_data = this_cpu_ptr(perf_trace_buf[*rctxp]);
183 /* zero the dead bytes from align to not leak stack to user */
184 memset(&raw_data[size - sizeof(u64)], 0, sizeof(u64));
186 entry = (struct trace_entry *)raw_data;
187 local_save_flags(flags);
188 tracing_generic_entry_update(entry, flags, pc);
189 entry->type = type;
191 return raw_data;
193 EXPORT_SYMBOL_GPL(perf_trace_buf_prepare);