Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 1 | /* |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 2 | * trace event based perf event profiling/tracing |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2009 Red Hat Inc, Peter Zijlstra <pzijlstr@redhat.com> |
Frederic Weisbecker | c530665 | 2010-03-03 07:16:16 +0100 | [diff] [blame] | 5 | * Copyright (C) 2009-2010 Frederic Weisbecker <fweisbec@gmail.com> |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
Li Zefan | 558e654 | 2009-08-24 12:19:47 +0800 | [diff] [blame] | 8 | #include <linux/module.h> |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 9 | #include <linux/kprobes.h> |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 10 | #include "trace.h" |
| 11 | |
Frederic Weisbecker | c530665 | 2010-03-03 07:16:16 +0100 | [diff] [blame] | 12 | DEFINE_PER_CPU(struct pt_regs, perf_trace_regs); |
Xiao Guangrong | 639fe4b | 2010-03-11 15:30:35 +0800 | [diff] [blame] | 13 | EXPORT_PER_CPU_SYMBOL_GPL(perf_trace_regs); |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 14 | |
Frederic Weisbecker | dcd5c16 | 2010-03-16 01:05:02 +0100 | [diff] [blame^] | 15 | EXPORT_SYMBOL_GPL(perf_arch_fetch_caller_regs); |
| 16 | |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 17 | static char *perf_trace_buf; |
| 18 | static char *perf_trace_buf_nmi; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 19 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 20 | typedef typeof(char [PERF_MAX_TRACE_SIZE]) perf_trace_t ; |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 21 | |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 22 | /* Count the events in use (per event id, not per instance) */ |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 23 | static int total_ref_count; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 24 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 25 | static int perf_trace_event_enable(struct ftrace_event_call *event) |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 26 | { |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 27 | char *buf; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 28 | int ret = -ENOMEM; |
| 29 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 30 | if (event->perf_refcount++ > 0) |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 31 | return 0; |
| 32 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 33 | if (!total_ref_count) { |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 34 | buf = (char *)alloc_percpu(perf_trace_t); |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 35 | if (!buf) |
| 36 | goto fail_buf; |
| 37 | |
Frederic Weisbecker | 444a2a3 | 2009-11-06 04:13:05 +0100 | [diff] [blame] | 38 | rcu_assign_pointer(perf_trace_buf, buf); |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 39 | |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 40 | buf = (char *)alloc_percpu(perf_trace_t); |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 41 | if (!buf) |
| 42 | goto fail_buf_nmi; |
| 43 | |
Frederic Weisbecker | 444a2a3 | 2009-11-06 04:13:05 +0100 | [diff] [blame] | 44 | rcu_assign_pointer(perf_trace_buf_nmi, buf); |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 45 | } |
| 46 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 47 | ret = event->perf_event_enable(event); |
Frederic Weisbecker | fe8e5b5 | 2009-10-03 14:55:18 +0200 | [diff] [blame] | 48 | if (!ret) { |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 49 | total_ref_count++; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 50 | return 0; |
Frederic Weisbecker | fe8e5b5 | 2009-10-03 14:55:18 +0200 | [diff] [blame] | 51 | } |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 52 | |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 53 | fail_buf_nmi: |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 54 | if (!total_ref_count) { |
Frederic Weisbecker | 444a2a3 | 2009-11-06 04:13:05 +0100 | [diff] [blame] | 55 | free_percpu(perf_trace_buf_nmi); |
| 56 | free_percpu(perf_trace_buf); |
| 57 | perf_trace_buf_nmi = NULL; |
| 58 | perf_trace_buf = NULL; |
Frederic Weisbecker | fe8e5b5 | 2009-10-03 14:55:18 +0200 | [diff] [blame] | 59 | } |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 60 | fail_buf: |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 61 | event->perf_refcount--; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 62 | |
| 63 | return ret; |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 64 | } |
| 65 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 66 | int perf_trace_enable(int event_id) |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 67 | { |
| 68 | struct ftrace_event_call *event; |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 69 | int ret = -EINVAL; |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 70 | |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 71 | mutex_lock(&event_mutex); |
Steven Rostedt | a59fd60 | 2009-04-10 13:52:20 -0400 | [diff] [blame] | 72 | list_for_each_entry(event, &ftrace_events, list) { |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 73 | if (event->id == event_id && event->perf_event_enable && |
Li Zefan | 558e654 | 2009-08-24 12:19:47 +0800 | [diff] [blame] | 74 | try_module_get(event->mod)) { |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 75 | ret = perf_trace_event_enable(event); |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 76 | break; |
| 77 | } |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 78 | } |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 79 | mutex_unlock(&event_mutex); |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 80 | |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 81 | return ret; |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 82 | } |
| 83 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 84 | static void perf_trace_event_disable(struct ftrace_event_call *event) |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 85 | { |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 86 | char *buf, *nmi_buf; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 87 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 88 | if (--event->perf_refcount > 0) |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 89 | return; |
| 90 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 91 | event->perf_event_disable(event); |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 92 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 93 | if (!--total_ref_count) { |
Frederic Weisbecker | 444a2a3 | 2009-11-06 04:13:05 +0100 | [diff] [blame] | 94 | buf = perf_trace_buf; |
| 95 | rcu_assign_pointer(perf_trace_buf, NULL); |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 96 | |
Frederic Weisbecker | 444a2a3 | 2009-11-06 04:13:05 +0100 | [diff] [blame] | 97 | nmi_buf = perf_trace_buf_nmi; |
| 98 | rcu_assign_pointer(perf_trace_buf_nmi, NULL); |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 99 | |
| 100 | /* |
| 101 | * Ensure every events in profiling have finished before |
| 102 | * releasing the buffers |
| 103 | */ |
| 104 | synchronize_sched(); |
| 105 | |
| 106 | free_percpu(buf); |
| 107 | free_percpu(nmi_buf); |
| 108 | } |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 111 | void perf_trace_disable(int event_id) |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 112 | { |
| 113 | struct ftrace_event_call *event; |
| 114 | |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 115 | mutex_lock(&event_mutex); |
Steven Rostedt | a59fd60 | 2009-04-10 13:52:20 -0400 | [diff] [blame] | 116 | list_for_each_entry(event, &ftrace_events, list) { |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 117 | if (event->id == event_id) { |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 118 | perf_trace_event_disable(event); |
Li Zefan | 558e654 | 2009-08-24 12:19:47 +0800 | [diff] [blame] | 119 | module_put(event->mod); |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 120 | break; |
| 121 | } |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 122 | } |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 123 | mutex_unlock(&event_mutex); |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 124 | } |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 125 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 126 | __kprobes void *perf_trace_buf_prepare(int size, unsigned short type, |
| 127 | int *rctxp, unsigned long *irq_flags) |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 128 | { |
| 129 | struct trace_entry *entry; |
| 130 | char *trace_buf, *raw_data; |
| 131 | int pc, cpu; |
| 132 | |
| 133 | pc = preempt_count(); |
| 134 | |
| 135 | /* Protect the per cpu buffer, begin the rcu read side */ |
| 136 | local_irq_save(*irq_flags); |
| 137 | |
| 138 | *rctxp = perf_swevent_get_recursion_context(); |
| 139 | if (*rctxp < 0) |
| 140 | goto err_recursion; |
| 141 | |
| 142 | cpu = smp_processor_id(); |
| 143 | |
| 144 | if (in_nmi()) |
| 145 | trace_buf = rcu_dereference(perf_trace_buf_nmi); |
| 146 | else |
| 147 | trace_buf = rcu_dereference(perf_trace_buf); |
| 148 | |
| 149 | if (!trace_buf) |
| 150 | goto err; |
| 151 | |
| 152 | raw_data = per_cpu_ptr(trace_buf, cpu); |
| 153 | |
| 154 | /* zero the dead bytes from align to not leak stack to user */ |
| 155 | *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL; |
| 156 | |
| 157 | entry = (struct trace_entry *)raw_data; |
| 158 | tracing_generic_entry_update(entry, *irq_flags, pc); |
| 159 | entry->type = type; |
| 160 | |
| 161 | return raw_data; |
| 162 | err: |
| 163 | perf_swevent_put_recursion_context(*rctxp); |
| 164 | err_recursion: |
| 165 | local_irq_restore(*irq_flags); |
| 166 | return NULL; |
| 167 | } |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 168 | EXPORT_SYMBOL_GPL(perf_trace_buf_prepare); |