blob: 0a47e8d6b4914e555ece3abec80a45db64f3c3d3 [file] [log] [blame]
Peter Zijlstraac199db2009-03-19 20:26:15 +01001/*
Frederic Weisbecker97d5a222010-03-05 05:35:37 +01002 * trace event based perf event profiling/tracing
Peter Zijlstraac199db2009-03-19 20:26:15 +01003 *
4 * Copyright (C) 2009 Red Hat Inc, Peter Zijlstra <pzijlstr@redhat.com>
Frederic Weisbeckerc5306652010-03-03 07:16:16 +01005 * Copyright (C) 2009-2010 Frederic Weisbecker <fweisbec@gmail.com>
Peter Zijlstraac199db2009-03-19 20:26:15 +01006 */
7
Li Zefan558e6542009-08-24 12:19:47 +08008#include <linux/module.h>
Xiao Guangrong430ad5a2010-01-28 09:32:29 +08009#include <linux/kprobes.h>
Peter Zijlstraac199db2009-03-19 20:26:15 +010010#include "trace.h"
11
Frederic Weisbeckerc5306652010-03-03 07:16:16 +010012DEFINE_PER_CPU(struct pt_regs, perf_trace_regs);
Xiao Guangrong639fe4b2010-03-11 15:30:35 +080013EXPORT_PER_CPU_SYMBOL_GPL(perf_trace_regs);
Frederic Weisbecker20ab44252009-09-18 06:10:28 +020014
Frederic Weisbeckerdcd5c162010-03-16 01:05:02 +010015EXPORT_SYMBOL_GPL(perf_arch_fetch_caller_regs);
Frederic Weisbecker20ab44252009-09-18 06:10:28 +020016
Xiao Guangrong430ad5a2010-01-28 09:32:29 +080017static char *perf_trace_buf;
18static char *perf_trace_buf_nmi;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +020019
Frederic Weisbeckereb1e7962010-03-23 00:08:59 +010020/*
21 * Force it to be aligned to unsigned long to avoid misaligned accesses
22 * suprises
23 */
24typedef typeof(unsigned long [PERF_MAX_TRACE_SIZE / sizeof(unsigned long)])
25 perf_trace_t;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +010026
Frederic Weisbecker20ab44252009-09-18 06:10:28 +020027/* Count the events in use (per event id, not per instance) */
Frederic Weisbecker97d5a222010-03-05 05:35:37 +010028static int total_ref_count;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +020029
Frederic Weisbecker97d5a222010-03-05 05:35:37 +010030static int perf_trace_event_enable(struct ftrace_event_call *event)
Frederic Weisbeckere5e25cf2009-09-18 00:54:43 +020031{
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +010032 char *buf;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +020033 int ret = -ENOMEM;
34
Frederic Weisbecker97d5a222010-03-05 05:35:37 +010035 if (event->perf_refcount++ > 0)
Frederic Weisbeckere5e25cf2009-09-18 00:54:43 +020036 return 0;
37
Frederic Weisbecker97d5a222010-03-05 05:35:37 +010038 if (!total_ref_count) {
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +010039 buf = (char *)alloc_percpu(perf_trace_t);
Frederic Weisbecker20ab44252009-09-18 06:10:28 +020040 if (!buf)
41 goto fail_buf;
42
Frederic Weisbecker444a2a32009-11-06 04:13:05 +010043 rcu_assign_pointer(perf_trace_buf, buf);
Frederic Weisbecker20ab44252009-09-18 06:10:28 +020044
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +010045 buf = (char *)alloc_percpu(perf_trace_t);
Frederic Weisbecker20ab44252009-09-18 06:10:28 +020046 if (!buf)
47 goto fail_buf_nmi;
48
Frederic Weisbecker444a2a32009-11-06 04:13:05 +010049 rcu_assign_pointer(perf_trace_buf_nmi, buf);
Frederic Weisbecker20ab44252009-09-18 06:10:28 +020050 }
51
Steven Rostedt22392912010-04-21 12:27:06 -040052 if (event->class->reg)
53 ret = event->class->reg(event, TRACE_REG_PERF_REGISTER);
54 else
55 ret = tracepoint_probe_register(event->name,
56 event->class->perf_probe,
57 event);
Frederic Weisbeckerfe8e5b52009-10-03 14:55:18 +020058 if (!ret) {
Frederic Weisbecker97d5a222010-03-05 05:35:37 +010059 total_ref_count++;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +020060 return 0;
Frederic Weisbeckerfe8e5b52009-10-03 14:55:18 +020061 }
Frederic Weisbecker20ab44252009-09-18 06:10:28 +020062
Frederic Weisbecker20ab44252009-09-18 06:10:28 +020063fail_buf_nmi:
Frederic Weisbecker97d5a222010-03-05 05:35:37 +010064 if (!total_ref_count) {
Frederic Weisbecker444a2a32009-11-06 04:13:05 +010065 free_percpu(perf_trace_buf_nmi);
66 free_percpu(perf_trace_buf);
67 perf_trace_buf_nmi = NULL;
68 perf_trace_buf = NULL;
Frederic Weisbeckerfe8e5b52009-10-03 14:55:18 +020069 }
Frederic Weisbecker20ab44252009-09-18 06:10:28 +020070fail_buf:
Frederic Weisbecker97d5a222010-03-05 05:35:37 +010071 event->perf_refcount--;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +020072
73 return ret;
Frederic Weisbeckere5e25cf2009-09-18 00:54:43 +020074}
75
Frederic Weisbecker97d5a222010-03-05 05:35:37 +010076int perf_trace_enable(int event_id)
Peter Zijlstraac199db2009-03-19 20:26:15 +010077{
78 struct ftrace_event_call *event;
Li Zefan20c89282009-05-06 10:33:45 +080079 int ret = -EINVAL;
Peter Zijlstraac199db2009-03-19 20:26:15 +010080
Li Zefan20c89282009-05-06 10:33:45 +080081 mutex_lock(&event_mutex);
Steven Rostedta59fd602009-04-10 13:52:20 -040082 list_for_each_entry(event, &ftrace_events, list) {
Steven Rostedt32c0eda2010-04-23 10:38:03 -040083 if (event->event.type == event_id &&
Steven Rostedt22392912010-04-21 12:27:06 -040084 event->class && event->class->perf_probe &&
Li Zefan558e6542009-08-24 12:19:47 +080085 try_module_get(event->mod)) {
Frederic Weisbecker97d5a222010-03-05 05:35:37 +010086 ret = perf_trace_event_enable(event);
Li Zefan20c89282009-05-06 10:33:45 +080087 break;
88 }
Peter Zijlstraac199db2009-03-19 20:26:15 +010089 }
Li Zefan20c89282009-05-06 10:33:45 +080090 mutex_unlock(&event_mutex);
Peter Zijlstraac199db2009-03-19 20:26:15 +010091
Li Zefan20c89282009-05-06 10:33:45 +080092 return ret;
Peter Zijlstraac199db2009-03-19 20:26:15 +010093}
94
Frederic Weisbecker97d5a222010-03-05 05:35:37 +010095static void perf_trace_event_disable(struct ftrace_event_call *event)
Frederic Weisbeckere5e25cf2009-09-18 00:54:43 +020096{
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +010097 char *buf, *nmi_buf;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +020098
Frederic Weisbecker97d5a222010-03-05 05:35:37 +010099 if (--event->perf_refcount > 0)
Frederic Weisbeckere5e25cf2009-09-18 00:54:43 +0200100 return;
101
Steven Rostedt22392912010-04-21 12:27:06 -0400102 if (event->class->reg)
103 event->class->reg(event, TRACE_REG_PERF_UNREGISTER);
104 else
105 tracepoint_probe_unregister(event->name, event->class->perf_probe, event);
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200106
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100107 if (!--total_ref_count) {
Frederic Weisbecker444a2a32009-11-06 04:13:05 +0100108 buf = perf_trace_buf;
109 rcu_assign_pointer(perf_trace_buf, NULL);
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200110
Frederic Weisbecker444a2a32009-11-06 04:13:05 +0100111 nmi_buf = perf_trace_buf_nmi;
112 rcu_assign_pointer(perf_trace_buf_nmi, NULL);
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200113
114 /*
115 * Ensure every events in profiling have finished before
116 * releasing the buffers
117 */
118 synchronize_sched();
119
120 free_percpu(buf);
121 free_percpu(nmi_buf);
122 }
Frederic Weisbeckere5e25cf2009-09-18 00:54:43 +0200123}
124
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100125void perf_trace_disable(int event_id)
Peter Zijlstraac199db2009-03-19 20:26:15 +0100126{
127 struct ftrace_event_call *event;
128
Li Zefan20c89282009-05-06 10:33:45 +0800129 mutex_lock(&event_mutex);
Steven Rostedta59fd602009-04-10 13:52:20 -0400130 list_for_each_entry(event, &ftrace_events, list) {
Steven Rostedt32c0eda2010-04-23 10:38:03 -0400131 if (event->event.type == event_id) {
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100132 perf_trace_event_disable(event);
Li Zefan558e6542009-08-24 12:19:47 +0800133 module_put(event->mod);
Li Zefan20c89282009-05-06 10:33:45 +0800134 break;
135 }
Peter Zijlstraac199db2009-03-19 20:26:15 +0100136 }
Li Zefan20c89282009-05-06 10:33:45 +0800137 mutex_unlock(&event_mutex);
Peter Zijlstraac199db2009-03-19 20:26:15 +0100138}
Xiao Guangrong430ad5a2010-01-28 09:32:29 +0800139
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100140__kprobes void *perf_trace_buf_prepare(int size, unsigned short type,
141 int *rctxp, unsigned long *irq_flags)
Xiao Guangrong430ad5a2010-01-28 09:32:29 +0800142{
143 struct trace_entry *entry;
144 char *trace_buf, *raw_data;
145 int pc, cpu;
146
Frederic Weisbeckereb1e7962010-03-23 00:08:59 +0100147 BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(unsigned long));
148
Xiao Guangrong430ad5a2010-01-28 09:32:29 +0800149 pc = preempt_count();
150
151 /* Protect the per cpu buffer, begin the rcu read side */
152 local_irq_save(*irq_flags);
153
154 *rctxp = perf_swevent_get_recursion_context();
155 if (*rctxp < 0)
156 goto err_recursion;
157
158 cpu = smp_processor_id();
159
160 if (in_nmi())
Paul E. McKenney8d53dd52010-03-03 17:50:18 -0800161 trace_buf = rcu_dereference_sched(perf_trace_buf_nmi);
Xiao Guangrong430ad5a2010-01-28 09:32:29 +0800162 else
Paul E. McKenney8d53dd52010-03-03 17:50:18 -0800163 trace_buf = rcu_dereference_sched(perf_trace_buf);
Xiao Guangrong430ad5a2010-01-28 09:32:29 +0800164
165 if (!trace_buf)
166 goto err;
167
168 raw_data = per_cpu_ptr(trace_buf, cpu);
169
170 /* zero the dead bytes from align to not leak stack to user */
Frederic Weisbeckereb1e7962010-03-23 00:08:59 +0100171 memset(&raw_data[size - sizeof(u64)], 0, sizeof(u64));
Xiao Guangrong430ad5a2010-01-28 09:32:29 +0800172
173 entry = (struct trace_entry *)raw_data;
174 tracing_generic_entry_update(entry, *irq_flags, pc);
175 entry->type = type;
176
177 return raw_data;
178err:
179 perf_swevent_put_recursion_context(*rctxp);
180err_recursion:
181 local_irq_restore(*irq_flags);
182 return NULL;
183}
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100184EXPORT_SYMBOL_GPL(perf_trace_buf_prepare);