Steven Rostedt (VMware) | bcea3f9 | 2018-08-16 11:23:53 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 2 | /* |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 3 | * trace event based perf event profiling/tracing |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 4 | * |
Peter Zijlstra | 90eec10 | 2015-11-16 11:08:45 +0100 | [diff] [blame] | 5 | * Copyright (C) 2009 Red Hat Inc, Peter Zijlstra |
Frederic Weisbecker | c530665 | 2010-03-03 07:16:16 +0100 | [diff] [blame] | 6 | * Copyright (C) 2009-2010 Frederic Weisbecker <fweisbec@gmail.com> |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
Li Zefan | 558e654 | 2009-08-24 12:19:47 +0800 | [diff] [blame] | 9 | #include <linux/module.h> |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 10 | #include <linux/kprobes.h> |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 11 | #include "trace.h" |
Song Liu | e12f03d | 2017-12-06 14:45:15 -0800 | [diff] [blame] | 12 | #include "trace_probe.h" |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 13 | |
Namhyung Kim | 6016ee1 | 2010-08-11 12:47:59 +0900 | [diff] [blame] | 14 | static char __percpu *perf_trace_buf[PERF_NR_CONTEXTS]; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 15 | |
Frederic Weisbecker | eb1e796 | 2010-03-23 00:08:59 +0100 | [diff] [blame] | 16 | /* |
| 17 | * Force it to be aligned to unsigned long to avoid misaligned accesses |
| 18 | * suprises |
| 19 | */ |
| 20 | typedef typeof(unsigned long [PERF_MAX_TRACE_SIZE / sizeof(unsigned long)]) |
| 21 | perf_trace_t; |
Frederic Weisbecker | ce71b9d | 2009-11-22 05:26:55 +0100 | [diff] [blame] | 22 | |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 23 | /* Count the events in use (per event id, not per instance) */ |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 24 | static int total_ref_count; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 25 | |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 26 | static int perf_trace_event_perm(struct trace_event_call *tp_event, |
Frederic Weisbecker | 61c3265 | 2010-11-18 01:39:17 +0100 | [diff] [blame] | 27 | struct perf_event *p_event) |
| 28 | { |
Peter Zijlstra | d5b5f39 | 2013-11-14 16:23:04 +0100 | [diff] [blame] | 29 | if (tp_event->perf_perm) { |
| 30 | int ret = tp_event->perf_perm(tp_event, p_event); |
| 31 | if (ret) |
| 32 | return ret; |
| 33 | } |
| 34 | |
Jiri Olsa | f4be073 | 2014-07-16 14:33:29 +0200 | [diff] [blame] | 35 | /* |
| 36 | * We checked and allowed to create parent, |
| 37 | * allow children without checking. |
| 38 | */ |
| 39 | if (p_event->parent) |
| 40 | return 0; |
| 41 | |
| 42 | /* |
| 43 | * It's ok to check current process (owner) permissions in here, |
| 44 | * because code below is called only via perf_event_open syscall. |
| 45 | */ |
| 46 | |
Jiri Olsa | ced3900 | 2012-02-15 15:51:52 +0100 | [diff] [blame] | 47 | /* The ftrace function trace is allowed only for root. */ |
Jiri Olsa | cfa77bc | 2014-03-02 16:56:38 +0100 | [diff] [blame] | 48 | if (ftrace_event_is_function(tp_event)) { |
| 49 | if (perf_paranoid_tracepoint_raw() && !capable(CAP_SYS_ADMIN)) |
| 50 | return -EPERM; |
| 51 | |
Jiri Olsa | 0a74c5b | 2016-03-16 15:34:29 +0100 | [diff] [blame] | 52 | if (!is_sampling_event(p_event)) |
| 53 | return 0; |
| 54 | |
Jiri Olsa | cfa77bc | 2014-03-02 16:56:38 +0100 | [diff] [blame] | 55 | /* |
| 56 | * We don't allow user space callchains for function trace |
| 57 | * event, due to issues with page faults while tracing page |
| 58 | * fault handler and its overall trickiness nature. |
| 59 | */ |
| 60 | if (!p_event->attr.exclude_callchain_user) |
| 61 | return -EINVAL; |
Jiri Olsa | 63c45f4 | 2014-03-02 16:56:39 +0100 | [diff] [blame] | 62 | |
| 63 | /* |
| 64 | * Same reason to disable user stack dump as for user space |
| 65 | * callchains above. |
| 66 | */ |
| 67 | if (p_event->attr.sample_type & PERF_SAMPLE_STACK_USER) |
| 68 | return -EINVAL; |
Jiri Olsa | cfa77bc | 2014-03-02 16:56:38 +0100 | [diff] [blame] | 69 | } |
Jiri Olsa | ced3900 | 2012-02-15 15:51:52 +0100 | [diff] [blame] | 70 | |
Frederic Weisbecker | 61c3265 | 2010-11-18 01:39:17 +0100 | [diff] [blame] | 71 | /* No tracing, just counting, so no obvious leak */ |
| 72 | if (!(p_event->attr.sample_type & PERF_SAMPLE_RAW)) |
| 73 | return 0; |
| 74 | |
| 75 | /* Some events are ok to be traced by non-root users... */ |
| 76 | if (p_event->attach_state == PERF_ATTACH_TASK) { |
| 77 | if (tp_event->flags & TRACE_EVENT_FL_CAP_ANY) |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * ...otherwise raw tracepoint data can be a severe data leak, |
| 83 | * only allow root to have these. |
| 84 | */ |
| 85 | if (perf_paranoid_tracepoint_raw() && !capable(CAP_SYS_ADMIN)) |
| 86 | return -EPERM; |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 91 | static int perf_trace_event_reg(struct trace_event_call *tp_event, |
Jiri Olsa | ceec0b6 | 2012-02-15 15:51:49 +0100 | [diff] [blame] | 92 | struct perf_event *p_event) |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 93 | { |
Namhyung Kim | 6016ee1 | 2010-08-11 12:47:59 +0900 | [diff] [blame] | 94 | struct hlist_head __percpu *list; |
Jiri Olsa | ceec0b6 | 2012-02-15 15:51:49 +0100 | [diff] [blame] | 95 | int ret = -ENOMEM; |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 96 | int cpu; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 97 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 98 | p_event->tp_event = tp_event; |
| 99 | if (tp_event->perf_refcount++ > 0) |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 100 | return 0; |
| 101 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 102 | list = alloc_percpu(struct hlist_head); |
| 103 | if (!list) |
| 104 | goto fail; |
| 105 | |
| 106 | for_each_possible_cpu(cpu) |
| 107 | INIT_HLIST_HEAD(per_cpu_ptr(list, cpu)); |
| 108 | |
| 109 | tp_event->perf_events = list; |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 110 | |
Frederic Weisbecker | 97d5a22 | 2010-03-05 05:35:37 +0100 | [diff] [blame] | 111 | if (!total_ref_count) { |
Namhyung Kim | 6016ee1 | 2010-08-11 12:47:59 +0900 | [diff] [blame] | 112 | char __percpu *buf; |
Peter Zijlstra | b7e2ece | 2010-05-19 10:52:27 +0200 | [diff] [blame] | 113 | int i; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 114 | |
Frederic Weisbecker | 7ae07ea | 2010-08-14 20:45:13 +0200 | [diff] [blame] | 115 | for (i = 0; i < PERF_NR_CONTEXTS; i++) { |
Namhyung Kim | 6016ee1 | 2010-08-11 12:47:59 +0900 | [diff] [blame] | 116 | buf = (char __percpu *)alloc_percpu(perf_trace_t); |
Peter Zijlstra | b7e2ece | 2010-05-19 10:52:27 +0200 | [diff] [blame] | 117 | if (!buf) |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 118 | goto fail; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 119 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 120 | perf_trace_buf[i] = buf; |
Peter Zijlstra | b7e2ece | 2010-05-19 10:52:27 +0200 | [diff] [blame] | 121 | } |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 122 | } |
| 123 | |
Jiri Olsa | ceec0b6 | 2012-02-15 15:51:49 +0100 | [diff] [blame] | 124 | ret = tp_event->class->reg(tp_event, TRACE_REG_PERF_REGISTER, NULL); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 125 | if (ret) |
| 126 | goto fail; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 127 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 128 | total_ref_count++; |
| 129 | return 0; |
| 130 | |
| 131 | fail: |
Peter Zijlstra | b7e2ece | 2010-05-19 10:52:27 +0200 | [diff] [blame] | 132 | if (!total_ref_count) { |
| 133 | int i; |
| 134 | |
Frederic Weisbecker | 7ae07ea | 2010-08-14 20:45:13 +0200 | [diff] [blame] | 135 | for (i = 0; i < PERF_NR_CONTEXTS; i++) { |
Peter Zijlstra | b7e2ece | 2010-05-19 10:52:27 +0200 | [diff] [blame] | 136 | free_percpu(perf_trace_buf[i]); |
| 137 | perf_trace_buf[i] = NULL; |
| 138 | } |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 139 | } |
| 140 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 141 | if (!--tp_event->perf_refcount) { |
| 142 | free_percpu(tp_event->perf_events); |
| 143 | tp_event->perf_events = NULL; |
Frederic Weisbecker | fe8e5b5 | 2009-10-03 14:55:18 +0200 | [diff] [blame] | 144 | } |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 145 | |
| 146 | return ret; |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 147 | } |
| 148 | |
Jiri Olsa | ceec0b6 | 2012-02-15 15:51:49 +0100 | [diff] [blame] | 149 | static void perf_trace_event_unreg(struct perf_event *p_event) |
| 150 | { |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 151 | struct trace_event_call *tp_event = p_event->tp_event; |
Jiri Olsa | ceec0b6 | 2012-02-15 15:51:49 +0100 | [diff] [blame] | 152 | int i; |
| 153 | |
| 154 | if (--tp_event->perf_refcount > 0) |
| 155 | goto out; |
| 156 | |
| 157 | tp_event->class->reg(tp_event, TRACE_REG_PERF_UNREGISTER, NULL); |
| 158 | |
| 159 | /* |
| 160 | * Ensure our callback won't be called anymore. The buffers |
| 161 | * will be freed after that. |
| 162 | */ |
| 163 | tracepoint_synchronize_unregister(); |
| 164 | |
| 165 | free_percpu(tp_event->perf_events); |
| 166 | tp_event->perf_events = NULL; |
| 167 | |
| 168 | if (!--total_ref_count) { |
| 169 | for (i = 0; i < PERF_NR_CONTEXTS; i++) { |
| 170 | free_percpu(perf_trace_buf[i]); |
| 171 | perf_trace_buf[i] = NULL; |
| 172 | } |
| 173 | } |
| 174 | out: |
| 175 | module_put(tp_event->mod); |
| 176 | } |
| 177 | |
| 178 | static int perf_trace_event_open(struct perf_event *p_event) |
| 179 | { |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 180 | struct trace_event_call *tp_event = p_event->tp_event; |
Jiri Olsa | ceec0b6 | 2012-02-15 15:51:49 +0100 | [diff] [blame] | 181 | return tp_event->class->reg(tp_event, TRACE_REG_PERF_OPEN, p_event); |
| 182 | } |
| 183 | |
| 184 | static void perf_trace_event_close(struct perf_event *p_event) |
| 185 | { |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 186 | struct trace_event_call *tp_event = p_event->tp_event; |
Jiri Olsa | ceec0b6 | 2012-02-15 15:51:49 +0100 | [diff] [blame] | 187 | tp_event->class->reg(tp_event, TRACE_REG_PERF_CLOSE, p_event); |
| 188 | } |
| 189 | |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 190 | static int perf_trace_event_init(struct trace_event_call *tp_event, |
Jiri Olsa | ceec0b6 | 2012-02-15 15:51:49 +0100 | [diff] [blame] | 191 | struct perf_event *p_event) |
| 192 | { |
| 193 | int ret; |
| 194 | |
| 195 | ret = perf_trace_event_perm(tp_event, p_event); |
| 196 | if (ret) |
| 197 | return ret; |
| 198 | |
| 199 | ret = perf_trace_event_reg(tp_event, p_event); |
| 200 | if (ret) |
| 201 | return ret; |
| 202 | |
| 203 | ret = perf_trace_event_open(p_event); |
| 204 | if (ret) { |
| 205 | perf_trace_event_unreg(p_event); |
| 206 | return ret; |
| 207 | } |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 212 | int perf_trace_init(struct perf_event *p_event) |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 213 | { |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 214 | struct trace_event_call *tp_event; |
Vince Weaver | 0022ced | 2013-11-15 12:39:45 -0500 | [diff] [blame] | 215 | u64 event_id = p_event->attr.config; |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 216 | int ret = -EINVAL; |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 217 | |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 218 | mutex_lock(&event_mutex); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 219 | list_for_each_entry(tp_event, &ftrace_events, list) { |
Steven Rostedt | ff5f149 | 2010-05-21 11:49:57 -0400 | [diff] [blame] | 220 | if (tp_event->event.type == event_id && |
Steven Rostedt | a1d0ce8 | 2010-06-08 11:22:06 -0400 | [diff] [blame] | 221 | tp_event->class && tp_event->class->reg && |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 222 | try_module_get(tp_event->mod)) { |
| 223 | ret = perf_trace_event_init(tp_event, p_event); |
Li Zefan | 9cb627d | 2010-09-01 12:58:43 +0200 | [diff] [blame] | 224 | if (ret) |
| 225 | module_put(tp_event->mod); |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 226 | break; |
| 227 | } |
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 | mutex_unlock(&event_mutex); |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 230 | |
Li Zefan | 20c8928 | 2009-05-06 10:33:45 +0800 | [diff] [blame] | 231 | return ret; |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 232 | } |
| 233 | |
Jiri Olsa | ceec0b6 | 2012-02-15 15:51:49 +0100 | [diff] [blame] | 234 | void perf_trace_destroy(struct perf_event *p_event) |
| 235 | { |
| 236 | mutex_lock(&event_mutex); |
| 237 | perf_trace_event_close(p_event); |
| 238 | perf_trace_event_unreg(p_event); |
| 239 | mutex_unlock(&event_mutex); |
| 240 | } |
| 241 | |
Song Liu | e12f03d | 2017-12-06 14:45:15 -0800 | [diff] [blame] | 242 | #ifdef CONFIG_KPROBE_EVENTS |
| 243 | int perf_kprobe_init(struct perf_event *p_event, bool is_retprobe) |
| 244 | { |
| 245 | int ret; |
| 246 | char *func = NULL; |
| 247 | struct trace_event_call *tp_event; |
| 248 | |
| 249 | if (p_event->attr.kprobe_func) { |
| 250 | func = kzalloc(KSYM_NAME_LEN, GFP_KERNEL); |
| 251 | if (!func) |
| 252 | return -ENOMEM; |
| 253 | ret = strncpy_from_user( |
| 254 | func, u64_to_user_ptr(p_event->attr.kprobe_func), |
| 255 | KSYM_NAME_LEN); |
Masami Hiramatsu | 5da13ab | 2018-04-09 21:16:54 +0900 | [diff] [blame] | 256 | if (ret == KSYM_NAME_LEN) |
| 257 | ret = -E2BIG; |
Song Liu | e12f03d | 2017-12-06 14:45:15 -0800 | [diff] [blame] | 258 | if (ret < 0) |
| 259 | goto out; |
| 260 | |
| 261 | if (func[0] == '\0') { |
| 262 | kfree(func); |
| 263 | func = NULL; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | tp_event = create_local_trace_kprobe( |
| 268 | func, (void *)(unsigned long)(p_event->attr.kprobe_addr), |
| 269 | p_event->attr.probe_offset, is_retprobe); |
| 270 | if (IS_ERR(tp_event)) { |
| 271 | ret = PTR_ERR(tp_event); |
| 272 | goto out; |
| 273 | } |
| 274 | |
| 275 | ret = perf_trace_event_init(tp_event, p_event); |
| 276 | if (ret) |
| 277 | destroy_local_trace_kprobe(tp_event); |
| 278 | out: |
| 279 | kfree(func); |
| 280 | return ret; |
| 281 | } |
| 282 | |
| 283 | void perf_kprobe_destroy(struct perf_event *p_event) |
| 284 | { |
| 285 | perf_trace_event_close(p_event); |
| 286 | perf_trace_event_unreg(p_event); |
| 287 | |
| 288 | destroy_local_trace_kprobe(p_event->tp_event); |
| 289 | } |
| 290 | #endif /* CONFIG_KPROBE_EVENTS */ |
| 291 | |
Song Liu | 33ea4b2 | 2017-12-06 14:45:16 -0800 | [diff] [blame] | 292 | #ifdef CONFIG_UPROBE_EVENTS |
Song Liu | a6ca88b | 2018-10-01 22:36:36 -0700 | [diff] [blame] | 293 | int perf_uprobe_init(struct perf_event *p_event, |
| 294 | unsigned long ref_ctr_offset, bool is_retprobe) |
Song Liu | 33ea4b2 | 2017-12-06 14:45:16 -0800 | [diff] [blame] | 295 | { |
| 296 | int ret; |
| 297 | char *path = NULL; |
| 298 | struct trace_event_call *tp_event; |
| 299 | |
| 300 | if (!p_event->attr.uprobe_path) |
| 301 | return -EINVAL; |
| 302 | path = kzalloc(PATH_MAX, GFP_KERNEL); |
| 303 | if (!path) |
| 304 | return -ENOMEM; |
| 305 | ret = strncpy_from_user( |
| 306 | path, u64_to_user_ptr(p_event->attr.uprobe_path), PATH_MAX); |
Song Liu | 0eadcc7 | 2018-04-09 18:31:30 +0000 | [diff] [blame] | 307 | if (ret == PATH_MAX) |
| 308 | return -E2BIG; |
Song Liu | 33ea4b2 | 2017-12-06 14:45:16 -0800 | [diff] [blame] | 309 | if (ret < 0) |
| 310 | goto out; |
| 311 | if (path[0] == '\0') { |
| 312 | ret = -EINVAL; |
| 313 | goto out; |
| 314 | } |
| 315 | |
Song Liu | a6ca88b | 2018-10-01 22:36:36 -0700 | [diff] [blame] | 316 | tp_event = create_local_trace_uprobe(path, p_event->attr.probe_offset, |
| 317 | ref_ctr_offset, is_retprobe); |
Song Liu | 33ea4b2 | 2017-12-06 14:45:16 -0800 | [diff] [blame] | 318 | if (IS_ERR(tp_event)) { |
| 319 | ret = PTR_ERR(tp_event); |
| 320 | goto out; |
| 321 | } |
| 322 | |
| 323 | /* |
| 324 | * local trace_uprobe need to hold event_mutex to call |
| 325 | * uprobe_buffer_enable() and uprobe_buffer_disable(). |
| 326 | * event_mutex is not required for local trace_kprobes. |
| 327 | */ |
| 328 | mutex_lock(&event_mutex); |
| 329 | ret = perf_trace_event_init(tp_event, p_event); |
| 330 | if (ret) |
| 331 | destroy_local_trace_uprobe(tp_event); |
| 332 | mutex_unlock(&event_mutex); |
| 333 | out: |
| 334 | kfree(path); |
| 335 | return ret; |
| 336 | } |
| 337 | |
| 338 | void perf_uprobe_destroy(struct perf_event *p_event) |
| 339 | { |
| 340 | mutex_lock(&event_mutex); |
| 341 | perf_trace_event_close(p_event); |
| 342 | perf_trace_event_unreg(p_event); |
| 343 | mutex_unlock(&event_mutex); |
| 344 | destroy_local_trace_uprobe(p_event->tp_event); |
| 345 | } |
| 346 | #endif /* CONFIG_UPROBE_EVENTS */ |
| 347 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 348 | int perf_trace_add(struct perf_event *p_event, int flags) |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 349 | { |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 350 | struct trace_event_call *tp_event = p_event->tp_event; |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 351 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 352 | if (!(flags & PERF_EF_START)) |
| 353 | p_event->hw.state = PERF_HES_STOPPED; |
| 354 | |
Peter Zijlstra | 466c81c | 2017-10-10 17:15:47 +0200 | [diff] [blame] | 355 | /* |
| 356 | * If TRACE_REG_PERF_ADD returns false; no custom action was performed |
| 357 | * and we need to take the default action of enqueueing our event on |
| 358 | * the right per-cpu hlist. |
| 359 | */ |
| 360 | if (!tp_event->class->reg(tp_event, TRACE_REG_PERF_ADD, p_event)) { |
| 361 | struct hlist_head __percpu *pcpu_list; |
| 362 | struct hlist_head *list; |
Frederic Weisbecker | 20ab4425 | 2009-09-18 06:10:28 +0200 | [diff] [blame] | 363 | |
Peter Zijlstra | 466c81c | 2017-10-10 17:15:47 +0200 | [diff] [blame] | 364 | pcpu_list = tp_event->perf_events; |
| 365 | if (WARN_ON_ONCE(!pcpu_list)) |
| 366 | return -EINVAL; |
| 367 | |
| 368 | list = this_cpu_ptr(pcpu_list); |
| 369 | hlist_add_head_rcu(&p_event->hlist_entry, list); |
| 370 | } |
| 371 | |
| 372 | return 0; |
Frederic Weisbecker | e5e25cf | 2009-09-18 00:54:43 +0200 | [diff] [blame] | 373 | } |
| 374 | |
Peter Zijlstra | a4eaf7f | 2010-06-16 14:37:10 +0200 | [diff] [blame] | 375 | void perf_trace_del(struct perf_event *p_event, int flags) |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 376 | { |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 377 | struct trace_event_call *tp_event = p_event->tp_event; |
Peter Zijlstra | 466c81c | 2017-10-10 17:15:47 +0200 | [diff] [blame] | 378 | |
| 379 | /* |
| 380 | * If TRACE_REG_PERF_DEL returns false; no custom action was performed |
| 381 | * and we need to take the default action of dequeueing our event from |
| 382 | * the right per-cpu hlist. |
| 383 | */ |
| 384 | if (!tp_event->class->reg(tp_event, TRACE_REG_PERF_DEL, p_event)) |
| 385 | hlist_del_rcu(&p_event->hlist_entry); |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 386 | } |
Peter Zijlstra | ac199db | 2009-03-19 20:26:15 +0100 | [diff] [blame] | 387 | |
Alexei Starovoitov | 1e1dcd9 | 2016-04-06 18:43:24 -0700 | [diff] [blame] | 388 | 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] | 389 | { |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 390 | char *raw_data; |
Alexei Starovoitov | 1e1dcd9 | 2016-04-06 18:43:24 -0700 | [diff] [blame] | 391 | int rctx; |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 392 | |
Frederic Weisbecker | eb1e796 | 2010-03-23 00:08:59 +0100 | [diff] [blame] | 393 | BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(unsigned long)); |
| 394 | |
Oleg Nesterov | cd92bf6 | 2013-06-17 19:02:11 +0200 | [diff] [blame] | 395 | if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, |
Alexei Starovoitov | 1e1dcd9 | 2016-04-06 18:43:24 -0700 | [diff] [blame] | 396 | "perf buffer not large enough")) |
Oleg Nesterov | cd92bf6 | 2013-06-17 19:02:11 +0200 | [diff] [blame] | 397 | return NULL; |
| 398 | |
Alexei Starovoitov | 1e1dcd9 | 2016-04-06 18:43:24 -0700 | [diff] [blame] | 399 | *rctxp = rctx = perf_swevent_get_recursion_context(); |
| 400 | if (rctx < 0) |
Peter Zijlstra | 1c024eca | 2010-05-19 14:02:22 +0200 | [diff] [blame] | 401 | return NULL; |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 402 | |
Peter Zijlstra (Intel) | 86038c5 | 2014-12-16 12:47:34 +0100 | [diff] [blame] | 403 | if (regs) |
Alexei Starovoitov | 1e1dcd9 | 2016-04-06 18:43:24 -0700 | [diff] [blame] | 404 | *regs = this_cpu_ptr(&__perf_regs[rctx]); |
| 405 | raw_data = this_cpu_ptr(perf_trace_buf[rctx]); |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 406 | |
| 407 | /* zero the dead bytes from align to not leak stack to user */ |
Frederic Weisbecker | eb1e796 | 2010-03-23 00:08:59 +0100 | [diff] [blame] | 408 | memset(&raw_data[size - sizeof(u64)], 0, sizeof(u64)); |
Alexei Starovoitov | 1e1dcd9 | 2016-04-06 18:43:24 -0700 | [diff] [blame] | 409 | return raw_data; |
| 410 | } |
| 411 | EXPORT_SYMBOL_GPL(perf_trace_buf_alloc); |
| 412 | NOKPROBE_SYMBOL(perf_trace_buf_alloc); |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 413 | |
Alexei Starovoitov | 1e1dcd9 | 2016-04-06 18:43:24 -0700 | [diff] [blame] | 414 | void perf_trace_buf_update(void *record, u16 type) |
| 415 | { |
| 416 | struct trace_entry *entry = record; |
| 417 | int pc = preempt_count(); |
| 418 | unsigned long flags; |
| 419 | |
Peter Zijlstra | 87f44bb | 2010-05-25 11:02:55 +0200 | [diff] [blame] | 420 | local_save_flags(flags); |
| 421 | tracing_generic_entry_update(entry, flags, pc); |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 422 | entry->type = type; |
Xiao Guangrong | 430ad5a | 2010-01-28 09:32:29 +0800 | [diff] [blame] | 423 | } |
Alexei Starovoitov | 1e1dcd9 | 2016-04-06 18:43:24 -0700 | [diff] [blame] | 424 | NOKPROBE_SYMBOL(perf_trace_buf_update); |
Jiri Olsa | ced3900 | 2012-02-15 15:51:52 +0100 | [diff] [blame] | 425 | |
| 426 | #ifdef CONFIG_FUNCTION_TRACER |
| 427 | static void |
Steven Rostedt | 2f5f6ad | 2011-08-08 16:57:47 -0400 | [diff] [blame] | 428 | perf_ftrace_function_call(unsigned long ip, unsigned long parent_ip, |
Steven Rostedt | a1e2e31 | 2011-08-09 12:50:46 -0400 | [diff] [blame] | 429 | struct ftrace_ops *ops, struct pt_regs *pt_regs) |
Jiri Olsa | ced3900 | 2012-02-15 15:51:52 +0100 | [diff] [blame] | 430 | { |
| 431 | struct ftrace_entry *entry; |
Peter Zijlstra | 466c81c | 2017-10-10 17:15:47 +0200 | [diff] [blame] | 432 | struct perf_event *event; |
| 433 | struct hlist_head head; |
Jiri Olsa | ced3900 | 2012-02-15 15:51:52 +0100 | [diff] [blame] | 434 | struct pt_regs regs; |
| 435 | int rctx; |
| 436 | |
Peter Zijlstra | 466c81c | 2017-10-10 17:15:47 +0200 | [diff] [blame] | 437 | if ((unsigned long)ops->private != smp_processor_id()) |
Oleg Nesterov | b8ebfd3 | 2013-06-17 19:02:04 +0200 | [diff] [blame] | 438 | return; |
| 439 | |
Peter Zijlstra | 466c81c | 2017-10-10 17:15:47 +0200 | [diff] [blame] | 440 | event = container_of(ops, struct perf_event, ftrace_ops); |
| 441 | |
| 442 | /* |
| 443 | * @event->hlist entry is NULL (per INIT_HLIST_NODE), and all |
| 444 | * the perf code does is hlist_for_each_entry_rcu(), so we can |
| 445 | * get away with simply setting the @head.first pointer in order |
| 446 | * to create a singular list. |
| 447 | */ |
| 448 | head.first = &event->hlist_entry; |
| 449 | |
Jiri Olsa | ced3900 | 2012-02-15 15:51:52 +0100 | [diff] [blame] | 450 | #define ENTRY_SIZE (ALIGN(sizeof(struct ftrace_entry) + sizeof(u32), \ |
| 451 | sizeof(u64)) - sizeof(u32)) |
| 452 | |
| 453 | BUILD_BUG_ON(ENTRY_SIZE > PERF_MAX_TRACE_SIZE); |
| 454 | |
Alexei Starovoitov | ec5e099 | 2016-04-06 18:43:22 -0700 | [diff] [blame] | 455 | memset(®s, 0, sizeof(regs)); |
Jiri Olsa | ced3900 | 2012-02-15 15:51:52 +0100 | [diff] [blame] | 456 | perf_fetch_caller_regs(®s); |
| 457 | |
Alexei Starovoitov | 1e1dcd9 | 2016-04-06 18:43:24 -0700 | [diff] [blame] | 458 | entry = perf_trace_buf_alloc(ENTRY_SIZE, NULL, &rctx); |
Jiri Olsa | ced3900 | 2012-02-15 15:51:52 +0100 | [diff] [blame] | 459 | if (!entry) |
| 460 | return; |
| 461 | |
| 462 | entry->ip = ip; |
| 463 | entry->parent_ip = parent_ip; |
Alexei Starovoitov | 1e1dcd9 | 2016-04-06 18:43:24 -0700 | [diff] [blame] | 464 | perf_trace_buf_submit(entry, ENTRY_SIZE, rctx, TRACE_FN, |
Peter Zijlstra | 466c81c | 2017-10-10 17:15:47 +0200 | [diff] [blame] | 465 | 1, ®s, &head, NULL); |
Jiri Olsa | ced3900 | 2012-02-15 15:51:52 +0100 | [diff] [blame] | 466 | |
| 467 | #undef ENTRY_SIZE |
| 468 | } |
| 469 | |
| 470 | static int perf_ftrace_function_register(struct perf_event *event) |
| 471 | { |
| 472 | struct ftrace_ops *ops = &event->ftrace_ops; |
| 473 | |
Peter Zijlstra | 1dd311e | 2017-10-11 09:45:31 +0200 | [diff] [blame] | 474 | ops->flags = FTRACE_OPS_FL_RCU; |
Peter Zijlstra | 466c81c | 2017-10-10 17:15:47 +0200 | [diff] [blame] | 475 | ops->func = perf_ftrace_function_call; |
| 476 | ops->private = (void *)(unsigned long)nr_cpu_ids; |
| 477 | |
Jiri Olsa | ced3900 | 2012-02-15 15:51:52 +0100 | [diff] [blame] | 478 | return register_ftrace_function(ops); |
| 479 | } |
| 480 | |
| 481 | static int perf_ftrace_function_unregister(struct perf_event *event) |
| 482 | { |
| 483 | struct ftrace_ops *ops = &event->ftrace_ops; |
Jiri Olsa | 5500fa5 | 2012-02-15 15:51:54 +0100 | [diff] [blame] | 484 | int ret = unregister_ftrace_function(ops); |
| 485 | ftrace_free_filter(ops); |
| 486 | return ret; |
Jiri Olsa | ced3900 | 2012-02-15 15:51:52 +0100 | [diff] [blame] | 487 | } |
| 488 | |
Steven Rostedt (Red Hat) | 2425bcb | 2015-05-05 11:45:27 -0400 | [diff] [blame] | 489 | int perf_ftrace_event_register(struct trace_event_call *call, |
Jiri Olsa | ced3900 | 2012-02-15 15:51:52 +0100 | [diff] [blame] | 490 | enum trace_reg type, void *data) |
| 491 | { |
Peter Zijlstra | 466c81c | 2017-10-10 17:15:47 +0200 | [diff] [blame] | 492 | struct perf_event *event = data; |
| 493 | |
Jiri Olsa | ced3900 | 2012-02-15 15:51:52 +0100 | [diff] [blame] | 494 | switch (type) { |
| 495 | case TRACE_REG_REGISTER: |
| 496 | case TRACE_REG_UNREGISTER: |
| 497 | break; |
| 498 | case TRACE_REG_PERF_REGISTER: |
| 499 | case TRACE_REG_PERF_UNREGISTER: |
| 500 | return 0; |
| 501 | case TRACE_REG_PERF_OPEN: |
| 502 | return perf_ftrace_function_register(data); |
| 503 | case TRACE_REG_PERF_CLOSE: |
| 504 | return perf_ftrace_function_unregister(data); |
| 505 | case TRACE_REG_PERF_ADD: |
Peter Zijlstra | 466c81c | 2017-10-10 17:15:47 +0200 | [diff] [blame] | 506 | event->ftrace_ops.private = (void *)(unsigned long)smp_processor_id(); |
| 507 | return 1; |
Jiri Olsa | ced3900 | 2012-02-15 15:51:52 +0100 | [diff] [blame] | 508 | case TRACE_REG_PERF_DEL: |
Peter Zijlstra | 466c81c | 2017-10-10 17:15:47 +0200 | [diff] [blame] | 509 | event->ftrace_ops.private = (void *)(unsigned long)nr_cpu_ids; |
| 510 | return 1; |
Jiri Olsa | ced3900 | 2012-02-15 15:51:52 +0100 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | return -EINVAL; |
| 514 | } |
| 515 | #endif /* CONFIG_FUNCTION_TRACER */ |