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 | * |
Peter Zijlstra | 90eec10 | 2015-11-16 11:08:45 +0100 | [diff] [blame] | 4 | * Copyright (C) 2009 Red Hat Inc, Peter Zijlstra |
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 | |
Namhyung Kim | 6016ee1 | 2010-08-11 12:47:59 +0900 | [diff] [blame] | 12 | static char __percpu *perf_trace_buf[PERF_NR_CONTEXTS]; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 13 | |
Frederic Weisbecker | eb1e796 | 2010-03-23 00:08:59 +0100 | [diff] [blame] | 14 | /* |
| 15 | * Force it to be aligned to unsigned long to avoid misaligned accesses |
| 16 | * suprises |
| 17 | */ |
| 18 | typedef typeof(unsigned long [PERF_MAX_TRACE_SIZE / sizeof(unsigned long)]) |
| 19 | perf_trace_t; |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 20 | |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 21 | /* Count the events in use (per event id, not per instance) */ |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 22 | static int total_ref_count; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 23 | |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 24 | static int perf_trace_event_perm(struct trace_event_call *tp_event, |
Frederic Weisbecker | 61c3265 | 2010-11-18 01:39:17 +0100 | [diff] [blame] | 25 | struct perf_event *p_event) |
| 26 | { |
Peter Zijlstra | d5b5f39 | 2013-11-14 16:23:04 +0100 | [diff] [blame] | 27 | if (tp_event->perf_perm) { |
| 28 | int ret = tp_event->perf_perm(tp_event, p_event); |
| 29 | if (ret) |
| 30 | return ret; |
| 31 | } |
| 32 | |
Jiri Olsa | f4be073 | 2014-07-16 14:33:29 +0200 | [diff] [blame] | 33 | /* |
| 34 | * We checked and allowed to create parent, |
| 35 | * allow children without checking. |
| 36 | */ |
| 37 | if (p_event->parent) |
| 38 | return 0; |
| 39 | |
| 40 | /* |
| 41 | * It's ok to check current process (owner) permissions in here, |
| 42 | * because code below is called only via perf_event_open syscall. |
| 43 | */ |
| 44 | |
Jiri Olsa | ced3900 | 2012-02-15 15:51:52 +0100 | [diff] [blame] | 45 | /* The ftrace function trace is allowed only for root. */ |
Jiri Olsa | cfa77bc | 2014-03-02 16:56:38 +0100 | [diff] [blame] | 46 | if (ftrace_event_is_function(tp_event)) { |
| 47 | if (perf_paranoid_tracepoint_raw() && !capable(CAP_SYS_ADMIN)) |
| 48 | return -EPERM; |
| 49 | |
Jiri Olsa | 0a74c5b | 2016-03-16 15:34:29 +0100 | [diff] [blame] | 50 | if (!is_sampling_event(p_event)) |
| 51 | return 0; |
| 52 | |
Jiri Olsa | cfa77bc | 2014-03-02 16:56:38 +0100 | [diff] [blame] | 53 | /* |
| 54 | * We don't allow user space callchains for function trace |
| 55 | * event, due to issues with page faults while tracing page |
| 56 | * fault handler and its overall trickiness nature. |
| 57 | */ |
| 58 | if (!p_event->attr.exclude_callchain_user) |
| 59 | return -EINVAL; |
Jiri Olsa | 63c45f4 | 2014-03-02 16:56:39 +0100 | [diff] [blame] | 60 | |
| 61 | /* |
| 62 | * Same reason to disable user stack dump as for user space |
| 63 | * callchains above. |
| 64 | */ |
| 65 | if (p_event->attr.sample_type & PERF_SAMPLE_STACK_USER) |
| 66 | return -EINVAL; |
Jiri Olsa | cfa77bc | 2014-03-02 16:56:38 +0100 | [diff] [blame] | 67 | } |
Jiri Olsa | ced3900 | 2012-02-15 15:51:52 +0100 | [diff] [blame] | 68 | |
Frederic Weisbecker | 61c3265 | 2010-11-18 01:39:17 +0100 | [diff] [blame] | 69 | /* No tracing, just counting, so no obvious leak */ |
| 70 | if (!(p_event->attr.sample_type & PERF_SAMPLE_RAW)) |
| 71 | return 0; |
| 72 | |
| 73 | /* Some events are ok to be traced by non-root users... */ |
| 74 | if (p_event->attach_state == PERF_ATTACH_TASK) { |
| 75 | if (tp_event->flags & TRACE_EVENT_FL_CAP_ANY) |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * ...otherwise raw tracepoint data can be a severe data leak, |
| 81 | * only allow root to have these. |
| 82 | */ |
| 83 | if (perf_paranoid_tracepoint_raw() && !capable(CAP_SYS_ADMIN)) |
| 84 | return -EPERM; |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 89 | static int perf_trace_event_reg(struct trace_event_call *tp_event, |
Jiri Olsa | ceec0b6 | 2012-02-15 15:51:49 +0100 | [diff] [blame] | 90 | struct perf_event *p_event) |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 91 | { |
Namhyung Kim | 6016ee1 | 2010-08-11 12:47:59 +0900 | [diff] [blame] | 92 | struct hlist_head __percpu *list; |
Jiri Olsa | ceec0b6 | 2012-02-15 15:51:49 +0100 | [diff] [blame] | 93 | int ret = -ENOMEM; |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 94 | int cpu; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 95 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 96 | p_event->tp_event = tp_event; |
| 97 | if (tp_event->perf_refcount++ > 0) |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 98 | return 0; |
| 99 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 100 | list = alloc_percpu(struct hlist_head); |
| 101 | if (!list) |
| 102 | goto fail; |
| 103 | |
| 104 | for_each_possible_cpu(cpu) |
| 105 | INIT_HLIST_HEAD(per_cpu_ptr(list, cpu)); |
| 106 | |
| 107 | tp_event->perf_events = list; |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 108 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 109 | if (!total_ref_count) { |
Namhyung Kim | 6016ee1 | 2010-08-11 12:47:59 +0900 | [diff] [blame] | 110 | char __percpu *buf; |
Peter Zijlstra | b7e2ece | 2010-05-19 10:52:27 +0200 | [diff] [blame] | 111 | int i; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 112 | |
Frederic Weisbecker | 7ae07ea | 2010-08-14 20:45:13 +0200 | [diff] [blame] | 113 | for (i = 0; i < PERF_NR_CONTEXTS; i++) { |
Namhyung Kim | 6016ee1 | 2010-08-11 12:47:59 +0900 | [diff] [blame] | 114 | buf = (char __percpu *)alloc_percpu(perf_trace_t); |
Peter Zijlstra | b7e2ece | 2010-05-19 10:52:27 +0200 | [diff] [blame] | 115 | if (!buf) |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 116 | goto fail; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 117 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 118 | perf_trace_buf[i] = buf; |
Peter Zijlstra | b7e2ece | 2010-05-19 10:52:27 +0200 | [diff] [blame] | 119 | } |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 120 | } |
| 121 | |
Jiri Olsa | ceec0b6 | 2012-02-15 15:51:49 +0100 | [diff] [blame] | 122 | ret = tp_event->class->reg(tp_event, TRACE_REG_PERF_REGISTER, NULL); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 123 | if (ret) |
| 124 | goto fail; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 125 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 126 | total_ref_count++; |
| 127 | return 0; |
| 128 | |
| 129 | fail: |
Peter Zijlstra | b7e2ece | 2010-05-19 10:52:27 +0200 | [diff] [blame] | 130 | if (!total_ref_count) { |
| 131 | int i; |
| 132 | |
Frederic Weisbecker | 7ae07ea | 2010-08-14 20:45:13 +0200 | [diff] [blame] | 133 | for (i = 0; i < PERF_NR_CONTEXTS; i++) { |
Peter Zijlstra | b7e2ece | 2010-05-19 10:52:27 +0200 | [diff] [blame] | 134 | free_percpu(perf_trace_buf[i]); |
| 135 | perf_trace_buf[i] = NULL; |
| 136 | } |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 137 | } |
| 138 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 139 | if (!--tp_event->perf_refcount) { |
| 140 | free_percpu(tp_event->perf_events); |
| 141 | tp_event->perf_events = NULL; |
Frederic Weisbecker | fe8e5b5 | 2009-10-03 14:55:18 +0200 | [diff] [blame] | 142 | } |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 143 | |
| 144 | return ret; |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 145 | } |
| 146 | |
Jiri Olsa | ceec0b6 | 2012-02-15 15:51:49 +0100 | [diff] [blame] | 147 | static void perf_trace_event_unreg(struct perf_event *p_event) |
| 148 | { |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 149 | struct trace_event_call *tp_event = p_event->tp_event; |
Jiri Olsa | ceec0b6 | 2012-02-15 15:51:49 +0100 | [diff] [blame] | 150 | int i; |
| 151 | |
| 152 | if (--tp_event->perf_refcount > 0) |
| 153 | goto out; |
| 154 | |
| 155 | tp_event->class->reg(tp_event, TRACE_REG_PERF_UNREGISTER, NULL); |
| 156 | |
| 157 | /* |
| 158 | * Ensure our callback won't be called anymore. The buffers |
| 159 | * will be freed after that. |
| 160 | */ |
| 161 | tracepoint_synchronize_unregister(); |
| 162 | |
| 163 | free_percpu(tp_event->perf_events); |
| 164 | tp_event->perf_events = NULL; |
| 165 | |
| 166 | if (!--total_ref_count) { |
| 167 | for (i = 0; i < PERF_NR_CONTEXTS; i++) { |
| 168 | free_percpu(perf_trace_buf[i]); |
| 169 | perf_trace_buf[i] = NULL; |
| 170 | } |
| 171 | } |
| 172 | out: |
| 173 | module_put(tp_event->mod); |
| 174 | } |
| 175 | |
| 176 | static int perf_trace_event_open(struct perf_event *p_event) |
| 177 | { |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 178 | struct trace_event_call *tp_event = p_event->tp_event; |
Jiri Olsa | ceec0b6 | 2012-02-15 15:51:49 +0100 | [diff] [blame] | 179 | return tp_event->class->reg(tp_event, TRACE_REG_PERF_OPEN, p_event); |
| 180 | } |
| 181 | |
| 182 | static void perf_trace_event_close(struct perf_event *p_event) |
| 183 | { |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 184 | struct trace_event_call *tp_event = p_event->tp_event; |
Jiri Olsa | ceec0b6 | 2012-02-15 15:51:49 +0100 | [diff] [blame] | 185 | tp_event->class->reg(tp_event, TRACE_REG_PERF_CLOSE, p_event); |
| 186 | } |
| 187 | |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 188 | static int perf_trace_event_init(struct trace_event_call *tp_event, |
Jiri Olsa | ceec0b6 | 2012-02-15 15:51:49 +0100 | [diff] [blame] | 189 | struct perf_event *p_event) |
| 190 | { |
| 191 | int ret; |
| 192 | |
| 193 | ret = perf_trace_event_perm(tp_event, p_event); |
| 194 | if (ret) |
| 195 | return ret; |
| 196 | |
| 197 | ret = perf_trace_event_reg(tp_event, p_event); |
| 198 | if (ret) |
| 199 | return ret; |
| 200 | |
| 201 | ret = perf_trace_event_open(p_event); |
| 202 | if (ret) { |
| 203 | perf_trace_event_unreg(p_event); |
| 204 | return ret; |
| 205 | } |
| 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 210 | int perf_trace_init(struct perf_event *p_event) |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 211 | { |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 212 | struct trace_event_call *tp_event; |
Vince Weaver | 0022ced | 2013-11-15 12:39:45 -0500 | [diff] [blame] | 213 | u64 event_id = p_event->attr.config; |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 214 | int ret = -EINVAL; |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 215 | |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 216 | mutex_lock(&event_mutex); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 217 | list_for_each_entry(tp_event, &ftrace_events, list) { |
Steven Rostedt | ff5f149 | 2010-05-21 11:49:57 -0400 | [diff] [blame] | 218 | if (tp_event->event.type == event_id && |
Steven Rostedt | a1d0ce8 | 2010-06-08 11:22:06 -0400 | [diff] [blame] | 219 | tp_event->class && tp_event->class->reg && |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 220 | try_module_get(tp_event->mod)) { |
| 221 | ret = perf_trace_event_init(tp_event, p_event); |
Li Zefan | 9cb627d | 2010-09-01 12:58:43 +0200 | [diff] [blame] | 222 | if (ret) |
| 223 | module_put(tp_event->mod); |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 224 | break; |
| 225 | } |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 226 | } |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 227 | mutex_unlock(&event_mutex); |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 228 | |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 229 | return ret; |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 230 | } |
| 231 | |
Jiri Olsa | ceec0b6 | 2012-02-15 15:51:49 +0100 | [diff] [blame] | 232 | void perf_trace_destroy(struct perf_event *p_event) |
| 233 | { |
| 234 | mutex_lock(&event_mutex); |
| 235 | perf_trace_event_close(p_event); |
| 236 | perf_trace_event_unreg(p_event); |
| 237 | mutex_unlock(&event_mutex); |
| 238 | } |
| 239 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 240 | int perf_trace_add(struct perf_event *p_event, int flags) |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 241 | { |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 242 | struct trace_event_call *tp_event = p_event->tp_event; |
Namhyung Kim | 6016ee1 | 2010-08-11 12:47:59 +0900 | [diff] [blame] | 243 | struct hlist_head __percpu *pcpu_list; |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 244 | struct hlist_head *list; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 245 | |
Namhyung Kim | 6016ee1 | 2010-08-11 12:47:59 +0900 | [diff] [blame] | 246 | pcpu_list = tp_event->perf_events; |
| 247 | if (WARN_ON_ONCE(!pcpu_list)) |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 248 | return -EINVAL; |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 249 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 250 | if (!(flags & PERF_EF_START)) |
| 251 | p_event->hw.state = PERF_HES_STOPPED; |
| 252 | |
Namhyung Kim | 6016ee1 | 2010-08-11 12:47:59 +0900 | [diff] [blame] | 253 | list = this_cpu_ptr(pcpu_list); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 254 | hlist_add_head_rcu(&p_event->hlist_entry, list); |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 255 | |
Jiri Olsa | 489c75c | 2012-02-15 15:51:50 +0100 | [diff] [blame] | 256 | return tp_event->class->reg(tp_event, TRACE_REG_PERF_ADD, p_event); |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 257 | } |
| 258 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 259 | void perf_trace_del(struct perf_event *p_event, int flags) |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 260 | { |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 261 | struct trace_event_call *tp_event = p_event->tp_event; |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 262 | hlist_del_rcu(&p_event->hlist_entry); |
Jiri Olsa | 489c75c | 2012-02-15 15:51:50 +0100 | [diff] [blame] | 263 | tp_event->class->reg(tp_event, TRACE_REG_PERF_DEL, p_event); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 264 | } |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 265 | |
Alexei Starovoitov | 1e1dcd9 | 2016-04-06 18:43:24 -0700 | [diff] [blame] | 266 | void *perf_trace_buf_alloc(int size, struct pt_regs **regs, int *rctxp) |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 267 | { |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 268 | char *raw_data; |
Alexei Starovoitov | 1e1dcd9 | 2016-04-06 18:43:24 -0700 | [diff] [blame] | 269 | int rctx; |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 270 | |
Frederic Weisbecker | eb1e796 | 2010-03-23 00:08:59 +0100 | [diff] [blame] | 271 | BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(unsigned long)); |
| 272 | |
Oleg Nesterov | cd92bf6 | 2013-06-17 19:02:11 +0200 | [diff] [blame] | 273 | if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, |
Alexei Starovoitov | 1e1dcd9 | 2016-04-06 18:43:24 -0700 | [diff] [blame] | 274 | "perf buffer not large enough")) |
Oleg Nesterov | cd92bf6 | 2013-06-17 19:02:11 +0200 | [diff] [blame] | 275 | return NULL; |
| 276 | |
Alexei Starovoitov | 1e1dcd9 | 2016-04-06 18:43:24 -0700 | [diff] [blame] | 277 | *rctxp = rctx = perf_swevent_get_recursion_context(); |
| 278 | if (rctx < 0) |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 279 | return NULL; |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 280 | |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 281 | if (regs) |
Alexei Starovoitov | 1e1dcd9 | 2016-04-06 18:43:24 -0700 | [diff] [blame] | 282 | *regs = this_cpu_ptr(&__perf_regs[rctx]); |
| 283 | raw_data = this_cpu_ptr(perf_trace_buf[rctx]); |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 284 | |
| 285 | /* zero the dead bytes from align to not leak stack to user */ |
Frederic Weisbecker | eb1e796 | 2010-03-23 00:08:59 +0100 | [diff] [blame] | 286 | memset(&raw_data[size - sizeof(u64)], 0, sizeof(u64)); |
Alexei Starovoitov | 1e1dcd9 | 2016-04-06 18:43:24 -0700 | [diff] [blame] | 287 | return raw_data; |
| 288 | } |
| 289 | EXPORT_SYMBOL_GPL(perf_trace_buf_alloc); |
| 290 | NOKPROBE_SYMBOL(perf_trace_buf_alloc); |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 291 | |
Alexei Starovoitov | 1e1dcd9 | 2016-04-06 18:43:24 -0700 | [diff] [blame] | 292 | void perf_trace_buf_update(void *record, u16 type) |
| 293 | { |
| 294 | struct trace_entry *entry = record; |
| 295 | int pc = preempt_count(); |
| 296 | unsigned long flags; |
| 297 | |
Peter Zijlstra | 87f44bb | 2010-05-25 11:02:55 +0200 | [diff] [blame] | 298 | local_save_flags(flags); |
| 299 | tracing_generic_entry_update(entry, flags, pc); |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 300 | entry->type = type; |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 301 | } |
Alexei Starovoitov | 1e1dcd9 | 2016-04-06 18:43:24 -0700 | [diff] [blame] | 302 | NOKPROBE_SYMBOL(perf_trace_buf_update); |
Jiri Olsa | ced3900 | 2012-02-15 15:51:52 +0100 | [diff] [blame] | 303 | |
| 304 | #ifdef CONFIG_FUNCTION_TRACER |
| 305 | static void |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 306 | perf_ftrace_function_call(unsigned long ip, unsigned long parent_ip, |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 307 | struct ftrace_ops *ops, struct pt_regs *pt_regs) |
Jiri Olsa | ced3900 | 2012-02-15 15:51:52 +0100 | [diff] [blame] | 308 | { |
| 309 | struct ftrace_entry *entry; |
| 310 | struct hlist_head *head; |
| 311 | struct pt_regs regs; |
| 312 | int rctx; |
| 313 | |
Oleg Nesterov | b8ebfd3 | 2013-06-17 19:02:04 +0200 | [diff] [blame] | 314 | head = this_cpu_ptr(event_function.perf_events); |
| 315 | if (hlist_empty(head)) |
| 316 | return; |
| 317 | |
Jiri Olsa | ced3900 | 2012-02-15 15:51:52 +0100 | [diff] [blame] | 318 | #define ENTRY_SIZE (ALIGN(sizeof(struct ftrace_entry) + sizeof(u32), \ |
| 319 | sizeof(u64)) - sizeof(u32)) |
| 320 | |
| 321 | BUILD_BUG_ON(ENTRY_SIZE > PERF_MAX_TRACE_SIZE); |
| 322 | |
Alexei Starovoitov | ec5e099 | 2016-04-06 18:43:22 -0700 | [diff] [blame] | 323 | memset(®s, 0, sizeof(regs)); |
Jiri Olsa | ced3900 | 2012-02-15 15:51:52 +0100 | [diff] [blame] | 324 | perf_fetch_caller_regs(®s); |
| 325 | |
Alexei Starovoitov | 1e1dcd9 | 2016-04-06 18:43:24 -0700 | [diff] [blame] | 326 | entry = perf_trace_buf_alloc(ENTRY_SIZE, NULL, &rctx); |
Jiri Olsa | ced3900 | 2012-02-15 15:51:52 +0100 | [diff] [blame] | 327 | if (!entry) |
| 328 | return; |
| 329 | |
| 330 | entry->ip = ip; |
| 331 | entry->parent_ip = parent_ip; |
Alexei Starovoitov | 1e1dcd9 | 2016-04-06 18:43:24 -0700 | [diff] [blame] | 332 | perf_trace_buf_submit(entry, ENTRY_SIZE, rctx, TRACE_FN, |
Andrew Vagin | e6dab5f | 2012-07-11 18:14:58 +0400 | [diff] [blame] | 333 | 1, ®s, head, NULL); |
Jiri Olsa | ced3900 | 2012-02-15 15:51:52 +0100 | [diff] [blame] | 334 | |
| 335 | #undef ENTRY_SIZE |
| 336 | } |
| 337 | |
| 338 | static int perf_ftrace_function_register(struct perf_event *event) |
| 339 | { |
| 340 | struct ftrace_ops *ops = &event->ftrace_ops; |
| 341 | |
Steven Rostedt (Red Hat) | ba27f2b | 2015-11-30 17:23:39 -0500 | [diff] [blame] | 342 | ops->flags |= FTRACE_OPS_FL_PER_CPU | FTRACE_OPS_FL_RCU; |
Jiri Olsa | ced3900 | 2012-02-15 15:51:52 +0100 | [diff] [blame] | 343 | ops->func = perf_ftrace_function_call; |
| 344 | return register_ftrace_function(ops); |
| 345 | } |
| 346 | |
| 347 | static int perf_ftrace_function_unregister(struct perf_event *event) |
| 348 | { |
| 349 | struct ftrace_ops *ops = &event->ftrace_ops; |
Jiri Olsa | 5500fa5 | 2012-02-15 15:51:54 +0100 | [diff] [blame] | 350 | int ret = unregister_ftrace_function(ops); |
| 351 | ftrace_free_filter(ops); |
| 352 | return ret; |
Jiri Olsa | ced3900 | 2012-02-15 15:51:52 +0100 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | static void perf_ftrace_function_enable(struct perf_event *event) |
| 356 | { |
| 357 | ftrace_function_local_enable(&event->ftrace_ops); |
| 358 | } |
| 359 | |
| 360 | static void perf_ftrace_function_disable(struct perf_event *event) |
| 361 | { |
| 362 | ftrace_function_local_disable(&event->ftrace_ops); |
| 363 | } |
| 364 | |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 365 | int perf_ftrace_event_register(struct trace_event_call *call, |
Jiri Olsa | ced3900 | 2012-02-15 15:51:52 +0100 | [diff] [blame] | 366 | enum trace_reg type, void *data) |
| 367 | { |
| 368 | switch (type) { |
| 369 | case TRACE_REG_REGISTER: |
| 370 | case TRACE_REG_UNREGISTER: |
| 371 | break; |
| 372 | case TRACE_REG_PERF_REGISTER: |
| 373 | case TRACE_REG_PERF_UNREGISTER: |
| 374 | return 0; |
| 375 | case TRACE_REG_PERF_OPEN: |
| 376 | return perf_ftrace_function_register(data); |
| 377 | case TRACE_REG_PERF_CLOSE: |
| 378 | return perf_ftrace_function_unregister(data); |
| 379 | case TRACE_REG_PERF_ADD: |
| 380 | perf_ftrace_function_enable(data); |
| 381 | return 0; |
| 382 | case TRACE_REG_PERF_DEL: |
| 383 | perf_ftrace_function_disable(data); |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | return -EINVAL; |
| 388 | } |
| 389 | #endif /* CONFIG_FUNCTION_TRACER */ |