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>
8 #include <linux/module.h>
9 #include <linux/kprobes.h>
12 static char __percpu
*perf_trace_buf
[PERF_NR_CONTEXTS
];
15 * Force it to be aligned to unsigned long to avoid misaligned accesses
18 typedef typeof(unsigned long [PERF_MAX_TRACE_SIZE
/ sizeof(unsigned long)])
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
))
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
)
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
))
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
;
54 ret
= perf_trace_event_perm(tp_event
, p_event
);
58 p_event
->tp_event
= tp_event
;
59 if (tp_event
->perf_refcount
++ > 0)
64 list
= alloc_percpu(struct hlist_head
);
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
) {
77 for (i
= 0; i
< PERF_NR_CONTEXTS
; i
++) {
78 buf
= (char __percpu
*)alloc_percpu(perf_trace_t
);
82 perf_trace_buf
[i
] = buf
;
86 ret
= tp_event
->class->reg(tp_event
, TRACE_REG_PERF_REGISTER
);
94 if (!total_ref_count
) {
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
;
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
;
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
);
124 module_put(tp_event
->mod
);
128 mutex_unlock(&event_mutex
);
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
))
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
);
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
;
162 mutex_lock(&event_mutex
);
163 if (--tp_event
->perf_refcount
> 0)
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
;
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
;
196 BUILD_BUG_ON(PERF_MAX_TRACE_SIZE
% sizeof(unsigned long));
198 pc
= preempt_count();
200 *rctxp
= perf_swevent_get_recursion_context();
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
);
216 EXPORT_SYMBOL_GPL(perf_trace_buf_prepare
);