Commit | Line | Data |
---|---|---|
bcea3f96 | 1 | // SPDX-License-Identifier: GPL-2.0 |
f3f096cf SD |
2 | /* |
3 | * uprobes-based tracing events | |
4 | * | |
f3f096cf SD |
5 | * Copyright (C) IBM Corporation, 2010-2012 |
6 | * Author: Srikar Dronamraju <srikar@linux.vnet.ibm.com> | |
7 | */ | |
ea6eb5e7 | 8 | #define pr_fmt(fmt) "trace_uprobe: " fmt |
f3f096cf | 9 | |
aef2feda | 10 | #include <linux/bpf-cgroup.h> |
97e8230f | 11 | #include <linux/cleanup.h> |
0597c49c | 12 | #include <linux/ctype.h> |
2f02a61d | 13 | #include <linux/filter.h> |
f3f096cf | 14 | #include <linux/module.h> |
f3f096cf | 15 | #include <linux/namei.h> |
10cdb82a | 16 | #include <linux/percpu.h> |
2f02a61d MHG |
17 | #include <linux/rculist.h> |
18 | #include <linux/security.h> | |
19 | #include <linux/string.h> | |
20 | #include <linux/uaccess.h> | |
21 | #include <linux/uprobes.h> | |
f3f096cf | 22 | |
97e8230f | 23 | #include "trace.h" |
0597c49c | 24 | #include "trace_dynevent.h" |
f3f096cf | 25 | #include "trace_probe.h" |
53305928 | 26 | #include "trace_probe_tmpl.h" |
f3f096cf SD |
27 | |
28 | #define UPROBE_EVENT_SYSTEM "uprobes" | |
29 | ||
457d1772 ON |
30 | struct uprobe_trace_entry_head { |
31 | struct trace_entry ent; | |
32 | unsigned long vaddr[]; | |
33 | }; | |
34 | ||
35 | #define SIZEOF_TRACE_ENTRY(is_return) \ | |
36 | (sizeof(struct uprobe_trace_entry_head) + \ | |
37 | sizeof(unsigned long) * (is_return ? 2 : 1)) | |
38 | ||
39 | #define DATAOF_TRACE_ENTRY(entry, is_return) \ | |
40 | ((void*)(entry) + SIZEOF_TRACE_ENTRY(is_return)) | |
41 | ||
d262271d | 42 | static int trace_uprobe_create(const char *raw_command); |
0597c49c MH |
43 | static int trace_uprobe_show(struct seq_file *m, struct dyn_event *ev); |
44 | static int trace_uprobe_release(struct dyn_event *ev); | |
45 | static bool trace_uprobe_is_busy(struct dyn_event *ev); | |
46 | static bool trace_uprobe_match(const char *system, const char *event, | |
30199137 | 47 | int argc, const char **argv, struct dyn_event *ev); |
0597c49c MH |
48 | |
49 | static struct dyn_event_operations trace_uprobe_ops = { | |
50 | .create = trace_uprobe_create, | |
51 | .show = trace_uprobe_show, | |
52 | .is_busy = trace_uprobe_is_busy, | |
53 | .free = trace_uprobe_release, | |
54 | .match = trace_uprobe_match, | |
55 | }; | |
56 | ||
f3f096cf SD |
57 | /* |
58 | * uprobe event core functions | |
59 | */ | |
f3f096cf | 60 | struct trace_uprobe { |
0597c49c | 61 | struct dyn_event devent; |
a932b738 | 62 | struct uprobe_consumer consumer; |
0c92c7a3 | 63 | struct path path; |
f3f096cf | 64 | char *filename; |
3c83a9ad | 65 | struct uprobe *uprobe; |
f3f096cf | 66 | unsigned long offset; |
1cc33161 | 67 | unsigned long ref_ctr_offset; |
10cdb82a | 68 | unsigned long __percpu *nhits; |
14577c39 | 69 | struct trace_probe tp; |
f3f096cf SD |
70 | }; |
71 | ||
0597c49c MH |
72 | static bool is_trace_uprobe(struct dyn_event *ev) |
73 | { | |
74 | return ev->ops == &trace_uprobe_ops; | |
75 | } | |
76 | ||
77 | static struct trace_uprobe *to_trace_uprobe(struct dyn_event *ev) | |
78 | { | |
79 | return container_of(ev, struct trace_uprobe, devent); | |
80 | } | |
81 | ||
82 | /** | |
83 | * for_each_trace_uprobe - iterate over the trace_uprobe list | |
84 | * @pos: the struct trace_uprobe * for each entry | |
85 | * @dpos: the struct dyn_event * to use as a loop cursor | |
86 | */ | |
87 | #define for_each_trace_uprobe(pos, dpos) \ | |
88 | for_each_dyn_event(dpos) \ | |
89 | if (is_trace_uprobe(dpos) && (pos = to_trace_uprobe(dpos))) | |
90 | ||
f3f096cf | 91 | static int register_uprobe_event(struct trace_uprobe *tu); |
c6c2401d | 92 | static int unregister_uprobe_event(struct trace_uprobe *tu); |
f3f096cf | 93 | |
da09a9e0 JO |
94 | static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs, |
95 | __u64 *data); | |
c1ae5c75 | 96 | static int uretprobe_dispatcher(struct uprobe_consumer *con, |
da09a9e0 JO |
97 | unsigned long func, struct pt_regs *regs, |
98 | __u64 *data); | |
f3f096cf | 99 | |
3fd996a2 NK |
100 | #ifdef CONFIG_STACK_GROWSUP |
101 | static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n) | |
102 | { | |
103 | return addr - (n * sizeof(long)); | |
104 | } | |
105 | #else | |
106 | static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n) | |
107 | { | |
108 | return addr + (n * sizeof(long)); | |
109 | } | |
110 | #endif | |
111 | ||
112 | static unsigned long get_user_stack_nth(struct pt_regs *regs, unsigned int n) | |
113 | { | |
114 | unsigned long ret; | |
115 | unsigned long addr = user_stack_pointer(regs); | |
116 | ||
117 | addr = adjust_stack_addr(addr, n); | |
118 | ||
119 | if (copy_from_user(&ret, (void __force __user *) addr, sizeof(ret))) | |
120 | return 0; | |
121 | ||
122 | return ret; | |
123 | } | |
124 | ||
125 | /* | |
126 | * Uprobes-specific fetch functions | |
127 | */ | |
53305928 | 128 | static nokprobe_inline int |
9b960a38 | 129 | probe_mem_read(void *dest, void *src, size_t size) |
53305928 MH |
130 | { |
131 | void __user *vaddr = (void __force __user *)src; | |
132 | ||
f3f58935 | 133 | return copy_from_user(dest, vaddr, size) ? -EFAULT : 0; |
5baaa59e | 134 | } |
e65f7ae7 MH |
135 | |
136 | static nokprobe_inline int | |
137 | probe_mem_read_user(void *dest, void *src, size_t size) | |
138 | { | |
139 | return probe_mem_read(dest, src, size); | |
140 | } | |
141 | ||
5baaa59e NK |
142 | /* |
143 | * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max | |
144 | * length and relative data location. | |
145 | */ | |
9178412d MH |
146 | static nokprobe_inline int |
147 | fetch_store_string(unsigned long addr, void *dest, void *base) | |
5baaa59e NK |
148 | { |
149 | long ret; | |
9178412d MH |
150 | u32 loc = *(u32 *)dest; |
151 | int maxlen = get_loc_len(loc); | |
152 | u8 *dst = get_loc_data(dest, base); | |
5baaa59e NK |
153 | void __user *src = (void __force __user *) addr; |
154 | ||
9178412d MH |
155 | if (unlikely(!maxlen)) |
156 | return -ENOMEM; | |
5baaa59e | 157 | |
4dd537ac | 158 | if (addr == FETCH_TOKEN_COMM) |
8a3750ec | 159 | ret = strscpy(dst, current->comm, maxlen); |
4dd537ac MH |
160 | else |
161 | ret = strncpy_from_user(dst, src, maxlen); | |
9178412d MH |
162 | if (ret >= 0) { |
163 | if (ret == maxlen) | |
164 | dst[ret - 1] = '\0'; | |
0722069a AZ |
165 | else |
166 | /* | |
167 | * Include the terminating null byte. In this case it | |
168 | * was copied by strncpy_from_user but not accounted | |
169 | * for in ret. | |
170 | */ | |
171 | ret++; | |
9178412d | 172 | *(u32 *)dest = make_data_loc(ret, (void *)dst - base); |
797311bc MHG |
173 | } else |
174 | *(u32 *)dest = make_data_loc(0, (void *)dst - base); | |
9178412d MH |
175 | |
176 | return ret; | |
5baaa59e NK |
177 | } |
178 | ||
88903c46 MH |
179 | static nokprobe_inline int |
180 | fetch_store_string_user(unsigned long addr, void *dest, void *base) | |
181 | { | |
182 | return fetch_store_string(addr, dest, base); | |
183 | } | |
184 | ||
53305928 | 185 | /* Return the length of string -- including null terminal byte */ |
9178412d MH |
186 | static nokprobe_inline int |
187 | fetch_store_strlen(unsigned long addr) | |
5baaa59e NK |
188 | { |
189 | int len; | |
190 | void __user *vaddr = (void __force __user *) addr; | |
191 | ||
4dd537ac MH |
192 | if (addr == FETCH_TOKEN_COMM) |
193 | len = strlen(current->comm) + 1; | |
194 | else | |
195 | len = strnlen_user(vaddr, MAX_STRING_SIZE); | |
5baaa59e | 196 | |
9178412d | 197 | return (len > MAX_STRING_SIZE) ? 0 : len; |
5baaa59e | 198 | } |
3fd996a2 | 199 | |
88903c46 MH |
200 | static nokprobe_inline int |
201 | fetch_store_strlen_user(unsigned long addr) | |
202 | { | |
203 | return fetch_store_strlen(addr); | |
204 | } | |
205 | ||
53305928 | 206 | static unsigned long translate_user_vaddr(unsigned long file_offset) |
b7e0bf34 NK |
207 | { |
208 | unsigned long base_addr; | |
209 | struct uprobe_dispatch_data *udd; | |
210 | ||
211 | udd = (void *) current->utask->vaddr; | |
212 | ||
213 | base_addr = udd->bp_addr - udd->tu->offset; | |
53305928 | 214 | return base_addr + file_offset; |
b7e0bf34 | 215 | } |
b7e0bf34 | 216 | |
53305928 MH |
217 | /* Note that we don't verify it, since the code does not come from user space */ |
218 | static int | |
25f00e40 MHG |
219 | process_fetch_insn(struct fetch_insn *code, void *rec, void *edata, |
220 | void *dest, void *base) | |
53305928 | 221 | { |
8565a45d | 222 | struct pt_regs *regs = rec; |
53305928 | 223 | unsigned long val; |
bd78acc8 | 224 | int ret; |
53305928 MH |
225 | |
226 | /* 1st stage: get value from context */ | |
227 | switch (code->op) { | |
228 | case FETCH_OP_REG: | |
229 | val = regs_get_register(regs, code->param); | |
230 | break; | |
231 | case FETCH_OP_STACK: | |
232 | val = get_user_stack_nth(regs, code->param); | |
233 | break; | |
234 | case FETCH_OP_STACKP: | |
235 | val = user_stack_pointer(regs); | |
236 | break; | |
237 | case FETCH_OP_RETVAL: | |
238 | val = regs_return_value(regs); | |
239 | break; | |
4dd537ac MH |
240 | case FETCH_OP_COMM: |
241 | val = FETCH_TOKEN_COMM; | |
242 | break; | |
53305928 MH |
243 | case FETCH_OP_FOFFS: |
244 | val = translate_user_vaddr(code->immediate); | |
245 | break; | |
246 | default: | |
bd78acc8 SC |
247 | ret = process_common_fetch_insn(code, &val); |
248 | if (ret < 0) | |
249 | return ret; | |
53305928 MH |
250 | } |
251 | code++; | |
252 | ||
9b960a38 | 253 | return process_fetch_insn_bottom(code, val, dest, base); |
53305928 MH |
254 | } |
255 | NOKPROBE_SYMBOL(process_fetch_insn) | |
256 | ||
736288ba ON |
257 | static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter) |
258 | { | |
259 | rwlock_init(&filter->rwlock); | |
260 | filter->nr_systemwide = 0; | |
261 | INIT_LIST_HEAD(&filter->perf_events); | |
262 | } | |
263 | ||
264 | static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter *filter) | |
265 | { | |
266 | return !filter->nr_systemwide && list_empty(&filter->perf_events); | |
267 | } | |
268 | ||
c1ae5c75 ON |
269 | static inline bool is_ret_probe(struct trace_uprobe *tu) |
270 | { | |
271 | return tu->consumer.ret_handler != NULL; | |
272 | } | |
273 | ||
0597c49c MH |
274 | static bool trace_uprobe_is_busy(struct dyn_event *ev) |
275 | { | |
276 | struct trace_uprobe *tu = to_trace_uprobe(ev); | |
277 | ||
278 | return trace_probe_is_enabled(&tu->tp); | |
279 | } | |
280 | ||
ab10d69e MH |
281 | static bool trace_uprobe_match_command_head(struct trace_uprobe *tu, |
282 | int argc, const char **argv) | |
283 | { | |
284 | char buf[MAX_ARGSTR_LEN + 1]; | |
285 | int len; | |
286 | ||
287 | if (!argc) | |
288 | return true; | |
289 | ||
290 | len = strlen(tu->filename); | |
291 | if (strncmp(tu->filename, argv[0], len) || argv[0][len] != ':') | |
292 | return false; | |
293 | ||
294 | if (tu->ref_ctr_offset == 0) | |
295 | snprintf(buf, sizeof(buf), "0x%0*lx", | |
296 | (int)(sizeof(void *) * 2), tu->offset); | |
297 | else | |
298 | snprintf(buf, sizeof(buf), "0x%0*lx(0x%lx)", | |
299 | (int)(sizeof(void *) * 2), tu->offset, | |
300 | tu->ref_ctr_offset); | |
301 | if (strcmp(buf, &argv[0][len + 1])) | |
302 | return false; | |
303 | ||
304 | argc--; argv++; | |
305 | ||
306 | return trace_probe_match_command_args(&tu->tp, argc, argv); | |
307 | } | |
308 | ||
0597c49c | 309 | static bool trace_uprobe_match(const char *system, const char *event, |
30199137 | 310 | int argc, const char **argv, struct dyn_event *ev) |
0597c49c MH |
311 | { |
312 | struct trace_uprobe *tu = to_trace_uprobe(ev); | |
313 | ||
95c104c3 LY |
314 | return (event[0] == '\0' || |
315 | strcmp(trace_probe_name(&tu->tp), event) == 0) && | |
ab10d69e MH |
316 | (!system || strcmp(trace_probe_group_name(&tu->tp), system) == 0) && |
317 | trace_uprobe_match_command_head(tu, argc, argv); | |
0597c49c MH |
318 | } |
319 | ||
60d53e2c MH |
320 | static nokprobe_inline struct trace_uprobe * |
321 | trace_uprobe_primary_from_call(struct trace_event_call *call) | |
322 | { | |
323 | struct trace_probe *tp; | |
324 | ||
325 | tp = trace_probe_primary_from_call(call); | |
326 | if (WARN_ON_ONCE(!tp)) | |
327 | return NULL; | |
328 | ||
329 | return container_of(tp, struct trace_uprobe, tp); | |
330 | } | |
331 | ||
f3f096cf SD |
332 | /* |
333 | * Allocate new trace_uprobe and initialize it (including uprobes). | |
334 | */ | |
335 | static struct trace_uprobe * | |
c1ae5c75 | 336 | alloc_trace_uprobe(const char *group, const char *event, int nargs, bool is_ret) |
f3f096cf SD |
337 | { |
338 | struct trace_uprobe *tu; | |
455b2899 | 339 | int ret; |
f3f096cf | 340 | |
845cbf3e | 341 | tu = kzalloc(struct_size(tu, tp.args, nargs), GFP_KERNEL); |
f3f096cf SD |
342 | if (!tu) |
343 | return ERR_PTR(-ENOMEM); | |
344 | ||
10cdb82a AN |
345 | tu->nhits = alloc_percpu(unsigned long); |
346 | if (!tu->nhits) { | |
347 | ret = -ENOMEM; | |
348 | goto error; | |
349 | } | |
350 | ||
035ba760 | 351 | ret = trace_probe_init(&tu->tp, event, group, true, nargs); |
455b2899 | 352 | if (ret < 0) |
f3f096cf SD |
353 | goto error; |
354 | ||
0597c49c | 355 | dyn_event_init(&tu->devent, &trace_uprobe_ops); |
a932b738 | 356 | tu->consumer.handler = uprobe_dispatcher; |
c1ae5c75 ON |
357 | if (is_ret) |
358 | tu->consumer.ret_handler = uretprobe_dispatcher; | |
b61387cb | 359 | init_trace_uprobe_filter(tu->tp.event->filter); |
f3f096cf SD |
360 | return tu; |
361 | ||
362 | error: | |
10cdb82a | 363 | free_percpu(tu->nhits); |
f3f096cf SD |
364 | kfree(tu); |
365 | ||
455b2899 | 366 | return ERR_PTR(ret); |
f3f096cf SD |
367 | } |
368 | ||
369 | static void free_trace_uprobe(struct trace_uprobe *tu) | |
370 | { | |
0597c49c MH |
371 | if (!tu) |
372 | return; | |
373 | ||
0c92c7a3 | 374 | path_put(&tu->path); |
455b2899 | 375 | trace_probe_cleanup(&tu->tp); |
f3f096cf | 376 | kfree(tu->filename); |
10cdb82a | 377 | free_percpu(tu->nhits); |
f3f096cf SD |
378 | kfree(tu); |
379 | } | |
380 | ||
381 | static struct trace_uprobe *find_probe_event(const char *event, const char *group) | |
382 | { | |
0597c49c | 383 | struct dyn_event *pos; |
f3f096cf SD |
384 | struct trace_uprobe *tu; |
385 | ||
0597c49c | 386 | for_each_trace_uprobe(tu, pos) |
b55ce203 MH |
387 | if (strcmp(trace_probe_name(&tu->tp), event) == 0 && |
388 | strcmp(trace_probe_group_name(&tu->tp), group) == 0) | |
f3f096cf SD |
389 | return tu; |
390 | ||
391 | return NULL; | |
392 | } | |
393 | ||
0597c49c | 394 | /* Unregister a trace_uprobe and probe_event */ |
c6c2401d | 395 | static int unregister_trace_uprobe(struct trace_uprobe *tu) |
f3f096cf | 396 | { |
c6c2401d SRRH |
397 | int ret; |
398 | ||
41af3cf5 MH |
399 | if (trace_probe_has_sibling(&tu->tp)) |
400 | goto unreg; | |
401 | ||
1d18538e SRV |
402 | /* If there's a reference to the dynamic event */ |
403 | if (trace_event_dyn_busy(trace_probe_event_call(&tu->tp))) | |
404 | return -EBUSY; | |
405 | ||
c6c2401d SRRH |
406 | ret = unregister_uprobe_event(tu); |
407 | if (ret) | |
408 | return ret; | |
409 | ||
41af3cf5 | 410 | unreg: |
0597c49c | 411 | dyn_event_remove(&tu->devent); |
41af3cf5 | 412 | trace_probe_unlink(&tu->tp); |
f3f096cf | 413 | free_trace_uprobe(tu); |
c6c2401d | 414 | return 0; |
f3f096cf SD |
415 | } |
416 | ||
fe60b0ce MH |
417 | static bool trace_uprobe_has_same_uprobe(struct trace_uprobe *orig, |
418 | struct trace_uprobe *comp) | |
419 | { | |
420 | struct trace_probe_event *tpe = orig->tp.event; | |
fe60b0ce MH |
421 | struct inode *comp_inode = d_real_inode(comp->path.dentry); |
422 | int i; | |
423 | ||
e161c6bf | 424 | list_for_each_entry(orig, &tpe->probes, tp.list) { |
fe60b0ce MH |
425 | if (comp_inode != d_real_inode(orig->path.dentry) || |
426 | comp->offset != orig->offset) | |
427 | continue; | |
428 | ||
429 | /* | |
430 | * trace_probe_compare_arg_type() ensured that nr_args and | |
431 | * each argument name and type are same. Let's compare comm. | |
432 | */ | |
433 | for (i = 0; i < orig->tp.nr_args; i++) { | |
434 | if (strcmp(orig->tp.args[i].comm, | |
435 | comp->tp.args[i].comm)) | |
f8d7ab2b | 436 | break; |
fe60b0ce MH |
437 | } |
438 | ||
f8d7ab2b SD |
439 | if (i == orig->tp.nr_args) |
440 | return true; | |
fe60b0ce MH |
441 | } |
442 | ||
443 | return false; | |
444 | } | |
445 | ||
41af3cf5 MH |
446 | static int append_trace_uprobe(struct trace_uprobe *tu, struct trace_uprobe *to) |
447 | { | |
448 | int ret; | |
449 | ||
fe60b0ce MH |
450 | ret = trace_probe_compare_arg_type(&tu->tp, &to->tp); |
451 | if (ret) { | |
452 | /* Note that argument starts index = 2 */ | |
453 | trace_probe_log_set_index(ret + 1); | |
454 | trace_probe_log_err(0, DIFF_ARG_TYPE); | |
455 | return -EEXIST; | |
456 | } | |
457 | if (trace_uprobe_has_same_uprobe(to, tu)) { | |
458 | trace_probe_log_set_index(0); | |
459 | trace_probe_log_err(0, SAME_PROBE); | |
460 | return -EEXIST; | |
461 | } | |
462 | ||
41af3cf5 MH |
463 | /* Append to existing event */ |
464 | ret = trace_probe_append(&tu->tp, &to->tp); | |
465 | if (!ret) | |
8b0e6c74 | 466 | dyn_event_add(&tu->devent, trace_probe_event_call(&tu->tp)); |
41af3cf5 MH |
467 | |
468 | return ret; | |
469 | } | |
470 | ||
ccea8727 RB |
471 | /* |
472 | * Uprobe with multiple reference counter is not allowed. i.e. | |
473 | * If inode and offset matches, reference counter offset *must* | |
474 | * match as well. Though, there is one exception: If user is | |
475 | * replacing old trace_uprobe with new one(same group/event), | |
476 | * then we allow same uprobe with new reference counter as far | |
477 | * as the new one does not conflict with any other existing | |
478 | * ones. | |
479 | */ | |
41af3cf5 | 480 | static int validate_ref_ctr_offset(struct trace_uprobe *new) |
ccea8727 | 481 | { |
0597c49c | 482 | struct dyn_event *pos; |
41af3cf5 | 483 | struct trace_uprobe *tmp; |
ccea8727 RB |
484 | struct inode *new_inode = d_real_inode(new->path.dentry); |
485 | ||
0597c49c | 486 | for_each_trace_uprobe(tmp, pos) { |
41af3cf5 | 487 | if (new_inode == d_real_inode(tmp->path.dentry) && |
ccea8727 RB |
488 | new->offset == tmp->offset && |
489 | new->ref_ctr_offset != tmp->ref_ctr_offset) { | |
490 | pr_warn("Reference counter offset mismatch."); | |
41af3cf5 | 491 | return -EINVAL; |
ccea8727 RB |
492 | } |
493 | } | |
41af3cf5 | 494 | return 0; |
ccea8727 RB |
495 | } |
496 | ||
f3f096cf SD |
497 | /* Register a trace_uprobe and probe_event */ |
498 | static int register_trace_uprobe(struct trace_uprobe *tu) | |
499 | { | |
14577c39 | 500 | struct trace_uprobe *old_tu; |
f3f096cf SD |
501 | int ret; |
502 | ||
f8821732 | 503 | guard(mutex)(&event_mutex); |
f3f096cf | 504 | |
41af3cf5 MH |
505 | ret = validate_ref_ctr_offset(tu); |
506 | if (ret) | |
f8821732 | 507 | return ret; |
ccea8727 | 508 | |
41af3cf5 MH |
509 | /* register as an event */ |
510 | old_tu = find_probe_event(trace_probe_name(&tu->tp), | |
511 | trace_probe_group_name(&tu->tp)); | |
14577c39 | 512 | if (old_tu) { |
41af3cf5 MH |
513 | if (is_ret_probe(tu) != is_ret_probe(old_tu)) { |
514 | trace_probe_log_set_index(0); | |
515 | trace_probe_log_err(0, DIFF_PROBE_TYPE); | |
f8821732 | 516 | return -EEXIST; |
41af3cf5 | 517 | } |
f8821732 | 518 | return append_trace_uprobe(tu, old_tu); |
c6c2401d | 519 | } |
f3f096cf SD |
520 | |
521 | ret = register_uprobe_event(tu); | |
522 | if (ret) { | |
8e242060 MH |
523 | if (ret == -EEXIST) { |
524 | trace_probe_log_set_index(0); | |
525 | trace_probe_log_err(0, EVENT_EXIST); | |
526 | } else | |
527 | pr_warn("Failed to register probe event(%d)\n", ret); | |
f8821732 | 528 | return ret; |
f3f096cf SD |
529 | } |
530 | ||
8b0e6c74 | 531 | dyn_event_add(&tu->devent, trace_probe_event_call(&tu->tp)); |
f3f096cf | 532 | |
f3f096cf SD |
533 | return ret; |
534 | } | |
535 | ||
536 | /* | |
537 | * Argument syntax: | |
95c104c3 | 538 | * - Add uprobe: p|r[:[GRP/][EVENT]] PATH:OFFSET[%return][(REF)] [FETCHARGS] |
f3f096cf | 539 | */ |
d262271d | 540 | static int __trace_uprobe_create(int argc, const char **argv) |
f3f096cf | 541 | { |
0597c49c MH |
542 | const char *event = NULL, *group = UPROBE_EVENT_SYSTEM; |
543 | char *arg, *filename, *rctr, *rctr_end, *tmp; | |
1cc33161 | 544 | unsigned long offset, ref_ctr_offset; |
97e8230f MHG |
545 | char *gbuf __free(kfree) = NULL; |
546 | char *buf __free(kfree) = NULL; | |
547 | enum probe_print_type ptype; | |
548 | struct trace_uprobe *tu; | |
0597c49c | 549 | bool is_return = false; |
97e8230f | 550 | struct path path; |
f3f096cf SD |
551 | int i, ret; |
552 | ||
1cc33161 | 553 | ref_ctr_offset = 0; |
f3f096cf | 554 | |
f01098c7 ET |
555 | switch (argv[0][0]) { |
556 | case 'r': | |
4ee5a52e | 557 | is_return = true; |
f01098c7 ET |
558 | break; |
559 | case 'p': | |
560 | break; | |
561 | default: | |
562 | return -ECANCELED; | |
563 | } | |
564 | ||
565 | if (argc < 2) | |
0597c49c | 566 | return -ECANCELED; |
57faaa04 MHG |
567 | |
568 | trace_probe_log_init("trace_uprobe", argc, argv); | |
569 | ||
570 | if (argc - 2 > MAX_TRACE_ARGS) { | |
571 | trace_probe_log_set_index(2); | |
572 | trace_probe_log_err(0, TOO_MANY_ARGS); | |
73f35080 | 573 | return -E2BIG; |
57faaa04 | 574 | } |
f3f096cf | 575 | |
0597c49c | 576 | if (argv[0][1] == ':') |
f3f096cf | 577 | event = &argv[0][2]; |
f3f096cf | 578 | |
0597c49c MH |
579 | if (!strchr(argv[1], '/')) |
580 | return -ECANCELED; | |
f3f096cf | 581 | |
0597c49c MH |
582 | filename = kstrdup(argv[1], GFP_KERNEL); |
583 | if (!filename) | |
584 | return -ENOMEM; | |
f3f096cf | 585 | |
6496bb72 | 586 | /* Find the last occurrence, in case the path contains ':' too. */ |
0597c49c MH |
587 | arg = strrchr(filename, ':'); |
588 | if (!arg || !isdigit(arg[1])) { | |
589 | kfree(filename); | |
590 | return -ECANCELED; | |
591 | } | |
f3f096cf | 592 | |
ab105a4f MH |
593 | trace_probe_log_set_index(1); /* filename is the 2nd argument */ |
594 | ||
f3f096cf | 595 | *arg++ = '\0'; |
f3f096cf | 596 | ret = kern_path(filename, LOOKUP_FOLLOW, &path); |
0597c49c | 597 | if (ret) { |
ab105a4f | 598 | trace_probe_log_err(0, FILE_NOT_FOUND); |
0597c49c | 599 | kfree(filename); |
ab105a4f | 600 | trace_probe_log_clear(); |
0c92c7a3 | 601 | return ret; |
0597c49c | 602 | } |
0c92c7a3 | 603 | if (!d_is_reg(path.dentry)) { |
ab105a4f | 604 | trace_probe_log_err(0, NO_REGULAR_FILE); |
d24d7dbf JZ |
605 | ret = -EINVAL; |
606 | goto fail_address_parse; | |
607 | } | |
f3f096cf | 608 | |
1cc33161 RB |
609 | /* Parse reference counter offset if specified. */ |
610 | rctr = strchr(arg, '('); | |
611 | if (rctr) { | |
612 | rctr_end = strchr(rctr, ')'); | |
ab105a4f MH |
613 | if (!rctr_end) { |
614 | ret = -EINVAL; | |
615 | rctr_end = rctr + strlen(rctr); | |
616 | trace_probe_log_err(rctr_end - filename, | |
617 | REFCNT_OPEN_BRACE); | |
618 | goto fail_address_parse; | |
619 | } else if (rctr_end[1] != '\0') { | |
1cc33161 | 620 | ret = -EINVAL; |
ab105a4f MH |
621 | trace_probe_log_err(rctr_end + 1 - filename, |
622 | BAD_REFCNT_SUFFIX); | |
1cc33161 RB |
623 | goto fail_address_parse; |
624 | } | |
625 | ||
626 | *rctr++ = '\0'; | |
627 | *rctr_end = '\0'; | |
628 | ret = kstrtoul(rctr, 0, &ref_ctr_offset); | |
629 | if (ret) { | |
ab105a4f | 630 | trace_probe_log_err(rctr - filename, BAD_REFCNT); |
1cc33161 RB |
631 | goto fail_address_parse; |
632 | } | |
633 | } | |
634 | ||
3dd3aae3 MH |
635 | /* Check if there is %return suffix */ |
636 | tmp = strchr(arg, '%'); | |
637 | if (tmp) { | |
638 | if (!strcmp(tmp, "%return")) { | |
639 | *tmp = '\0'; | |
640 | is_return = true; | |
641 | } else { | |
642 | trace_probe_log_err(tmp - filename, BAD_ADDR_SUFFIX); | |
643 | ret = -EINVAL; | |
644 | goto fail_address_parse; | |
645 | } | |
646 | } | |
647 | ||
1cc33161 | 648 | /* Parse uprobe offset. */ |
84d7ed79 | 649 | ret = kstrtoul(arg, 0, &offset); |
ab105a4f MH |
650 | if (ret) { |
651 | trace_probe_log_err(arg - filename, BAD_UPROBE_OFFS); | |
84d7ed79 | 652 | goto fail_address_parse; |
ab105a4f | 653 | } |
f3f096cf SD |
654 | |
655 | /* setup a probe */ | |
ab105a4f | 656 | trace_probe_log_set_index(0); |
0597c49c | 657 | if (event) { |
97e8230f MHG |
658 | gbuf = kmalloc(MAX_EVENT_NAME_LEN, GFP_KERNEL); |
659 | if (!gbuf) | |
660 | goto fail_mem; | |
661 | ||
95c104c3 | 662 | ret = traceprobe_parse_event_name(&event, &group, gbuf, |
ab105a4f | 663 | event - argv[0]); |
0597c49c MH |
664 | if (ret) |
665 | goto fail_address_parse; | |
95c104c3 LY |
666 | } |
667 | ||
668 | if (!event) { | |
b2e902f0 | 669 | char *tail; |
f3f096cf SD |
670 | char *ptr; |
671 | ||
b2e902f0 | 672 | tail = kstrdup(kbasename(filename), GFP_KERNEL); |
97e8230f MHG |
673 | if (!tail) |
674 | goto fail_mem; | |
f3f096cf | 675 | |
f3f096cf SD |
676 | ptr = strpbrk(tail, ".-_"); |
677 | if (ptr) | |
678 | *ptr = '\0'; | |
679 | ||
97e8230f MHG |
680 | buf = kmalloc(MAX_EVENT_NAME_LEN, GFP_KERNEL); |
681 | if (!buf) | |
682 | goto fail_mem; | |
f3f096cf SD |
683 | snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset); |
684 | event = buf; | |
685 | kfree(tail); | |
686 | } | |
687 | ||
ab105a4f MH |
688 | argc -= 2; |
689 | argv += 2; | |
690 | ||
4ee5a52e | 691 | tu = alloc_trace_uprobe(group, event, argc, is_return); |
f3f096cf | 692 | if (IS_ERR(tu)) { |
f3f096cf | 693 | ret = PTR_ERR(tu); |
a039480e MH |
694 | /* This must return -ENOMEM otherwise there is a bug */ |
695 | WARN_ON_ONCE(ret != -ENOMEM); | |
f3f096cf SD |
696 | goto fail_address_parse; |
697 | } | |
698 | tu->offset = offset; | |
1cc33161 | 699 | tu->ref_ctr_offset = ref_ctr_offset; |
0c92c7a3 | 700 | tu->path = path; |
0597c49c | 701 | tu->filename = filename; |
f3f096cf SD |
702 | |
703 | /* parse arguments */ | |
73f35080 | 704 | for (i = 0; i < argc; i++) { |
43beb5e8 MHG |
705 | struct traceprobe_parse_context *ctx __free(traceprobe_parse_context) |
706 | = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
1b8b0cd7 | 707 | |
43beb5e8 MHG |
708 | if (!ctx) { |
709 | ret = -ENOMEM; | |
710 | goto error; | |
711 | } | |
712 | ctx->flags = (is_return ? TPARG_FL_RETURN : 0) | TPARG_FL_USER; | |
ab105a4f | 713 | trace_probe_log_set_index(i + 2); |
43beb5e8 | 714 | ret = traceprobe_parse_probe_arg(&tu->tp, i, argv[i], ctx); |
d00bbea9 | 715 | if (ret) |
f3f096cf | 716 | goto error; |
f3f096cf SD |
717 | } |
718 | ||
007517a0 SRV |
719 | ptype = is_ret_probe(tu) ? PROBE_PRINT_RETURN : PROBE_PRINT_NORMAL; |
720 | ret = traceprobe_set_print_fmt(&tu->tp, ptype); | |
b4d4b96b MH |
721 | if (ret < 0) |
722 | goto error; | |
723 | ||
f3f096cf | 724 | ret = register_trace_uprobe(tu); |
ab105a4f MH |
725 | if (!ret) |
726 | goto out; | |
f3f096cf SD |
727 | |
728 | error: | |
729 | free_trace_uprobe(tu); | |
ab105a4f MH |
730 | out: |
731 | trace_probe_log_clear(); | |
f3f096cf SD |
732 | return ret; |
733 | ||
97e8230f MHG |
734 | fail_mem: |
735 | ret = -ENOMEM; | |
736 | ||
f3f096cf | 737 | fail_address_parse: |
ab105a4f | 738 | trace_probe_log_clear(); |
0c92c7a3 | 739 | path_put(&path); |
0597c49c | 740 | kfree(filename); |
f3f096cf | 741 | |
f3f096cf SD |
742 | return ret; |
743 | } | |
744 | ||
d262271d MH |
745 | int trace_uprobe_create(const char *raw_command) |
746 | { | |
747 | return trace_probe_create(raw_command, __trace_uprobe_create); | |
748 | } | |
749 | ||
750 | static int create_or_delete_trace_uprobe(const char *raw_command) | |
f3f096cf | 751 | { |
0597c49c | 752 | int ret; |
f3f096cf | 753 | |
d262271d MH |
754 | if (raw_command[0] == '-') |
755 | return dyn_event_release(raw_command, &trace_uprobe_ops); | |
f3f096cf | 756 | |
fd837de3 | 757 | ret = dyn_event_create(raw_command, &trace_uprobe_ops); |
0597c49c | 758 | return ret == -ECANCELED ? -EINVAL : ret; |
f3f096cf SD |
759 | } |
760 | ||
0597c49c | 761 | static int trace_uprobe_release(struct dyn_event *ev) |
f3f096cf | 762 | { |
0597c49c | 763 | struct trace_uprobe *tu = to_trace_uprobe(ev); |
f3f096cf | 764 | |
0597c49c | 765 | return unregister_trace_uprobe(tu); |
f3f096cf SD |
766 | } |
767 | ||
0597c49c MH |
768 | /* Probes listing interfaces */ |
769 | static int trace_uprobe_show(struct seq_file *m, struct dyn_event *ev) | |
f3f096cf | 770 | { |
0597c49c | 771 | struct trace_uprobe *tu = to_trace_uprobe(ev); |
3ede82dd | 772 | char c = is_ret_probe(tu) ? 'r' : 'p'; |
f3f096cf SD |
773 | int i; |
774 | ||
b55ce203 MH |
775 | seq_printf(m, "%c:%s/%s %s:0x%0*lx", c, trace_probe_group_name(&tu->tp), |
776 | trace_probe_name(&tu->tp), tu->filename, | |
a64b2c01 | 777 | (int)(sizeof(void *) * 2), tu->offset); |
f3f096cf | 778 | |
1cc33161 RB |
779 | if (tu->ref_ctr_offset) |
780 | seq_printf(m, "(0x%lx)", tu->ref_ctr_offset); | |
781 | ||
14577c39 NK |
782 | for (i = 0; i < tu->tp.nr_args; i++) |
783 | seq_printf(m, " %s=%s", tu->tp.args[i].name, tu->tp.args[i].comm); | |
f3f096cf | 784 | |
fa6f0cc7 | 785 | seq_putc(m, '\n'); |
f3f096cf SD |
786 | return 0; |
787 | } | |
788 | ||
0597c49c MH |
789 | static int probes_seq_show(struct seq_file *m, void *v) |
790 | { | |
791 | struct dyn_event *ev = v; | |
792 | ||
793 | if (!is_trace_uprobe(ev)) | |
794 | return 0; | |
795 | ||
796 | return trace_uprobe_show(m, ev); | |
797 | } | |
798 | ||
f3f096cf | 799 | static const struct seq_operations probes_seq_op = { |
0597c49c MH |
800 | .start = dyn_event_seq_start, |
801 | .next = dyn_event_seq_next, | |
802 | .stop = dyn_event_seq_stop, | |
803 | .show = probes_seq_show | |
f3f096cf SD |
804 | }; |
805 | ||
806 | static int probes_open(struct inode *inode, struct file *file) | |
807 | { | |
c6c2401d SRRH |
808 | int ret; |
809 | ||
17911ff3 SRV |
810 | ret = security_locked_down(LOCKDOWN_TRACEFS); |
811 | if (ret) | |
812 | return ret; | |
813 | ||
c6c2401d | 814 | if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) { |
0597c49c | 815 | ret = dyn_events_release_all(&trace_uprobe_ops); |
c6c2401d SRRH |
816 | if (ret) |
817 | return ret; | |
818 | } | |
f3f096cf SD |
819 | |
820 | return seq_open(file, &probes_seq_op); | |
821 | } | |
822 | ||
823 | static ssize_t probes_write(struct file *file, const char __user *buffer, | |
824 | size_t count, loff_t *ppos) | |
825 | { | |
0597c49c MH |
826 | return trace_parse_run_command(file, buffer, count, ppos, |
827 | create_or_delete_trace_uprobe); | |
f3f096cf SD |
828 | } |
829 | ||
830 | static const struct file_operations uprobe_events_ops = { | |
831 | .owner = THIS_MODULE, | |
832 | .open = probes_open, | |
833 | .read = seq_read, | |
834 | .llseek = seq_lseek, | |
835 | .release = seq_release, | |
836 | .write = probes_write, | |
837 | }; | |
838 | ||
839 | /* Probes profiling interfaces */ | |
840 | static int probes_profile_seq_show(struct seq_file *m, void *v) | |
841 | { | |
0597c49c MH |
842 | struct dyn_event *ev = v; |
843 | struct trace_uprobe *tu; | |
10cdb82a AN |
844 | unsigned long nhits; |
845 | int cpu; | |
0597c49c MH |
846 | |
847 | if (!is_trace_uprobe(ev)) | |
848 | return 0; | |
f3f096cf | 849 | |
0597c49c | 850 | tu = to_trace_uprobe(ev); |
10cdb82a AN |
851 | |
852 | nhits = 0; | |
853 | for_each_possible_cpu(cpu) { | |
854 | nhits += per_cpu(*tu->nhits, cpu); | |
855 | } | |
856 | ||
de7b2973 | 857 | seq_printf(m, " %s %-44s %15lu\n", tu->filename, |
10cdb82a | 858 | trace_probe_name(&tu->tp), nhits); |
f3f096cf SD |
859 | return 0; |
860 | } | |
861 | ||
862 | static const struct seq_operations profile_seq_op = { | |
0597c49c MH |
863 | .start = dyn_event_seq_start, |
864 | .next = dyn_event_seq_next, | |
865 | .stop = dyn_event_seq_stop, | |
f3f096cf SD |
866 | .show = probes_profile_seq_show |
867 | }; | |
868 | ||
869 | static int profile_open(struct inode *inode, struct file *file) | |
870 | { | |
17911ff3 SRV |
871 | int ret; |
872 | ||
873 | ret = security_locked_down(LOCKDOWN_TRACEFS); | |
874 | if (ret) | |
875 | return ret; | |
876 | ||
f3f096cf SD |
877 | return seq_open(file, &profile_seq_op); |
878 | } | |
879 | ||
880 | static const struct file_operations uprobe_profile_ops = { | |
881 | .owner = THIS_MODULE, | |
882 | .open = profile_open, | |
883 | .read = seq_read, | |
884 | .llseek = seq_lseek, | |
885 | .release = seq_release, | |
886 | }; | |
887 | ||
dcad1a20 NK |
888 | struct uprobe_cpu_buffer { |
889 | struct mutex mutex; | |
890 | void *buf; | |
3eaea21b | 891 | int dsize; |
dcad1a20 NK |
892 | }; |
893 | static struct uprobe_cpu_buffer __percpu *uprobe_cpu_buffer; | |
894 | static int uprobe_buffer_refcnt; | |
373b9338 | 895 | #define MAX_UCB_BUFFER_SIZE PAGE_SIZE |
dcad1a20 NK |
896 | |
897 | static int uprobe_buffer_init(void) | |
898 | { | |
899 | int cpu, err_cpu; | |
900 | ||
901 | uprobe_cpu_buffer = alloc_percpu(struct uprobe_cpu_buffer); | |
902 | if (uprobe_cpu_buffer == NULL) | |
903 | return -ENOMEM; | |
904 | ||
905 | for_each_possible_cpu(cpu) { | |
906 | struct page *p = alloc_pages_node(cpu_to_node(cpu), | |
907 | GFP_KERNEL, 0); | |
908 | if (p == NULL) { | |
909 | err_cpu = cpu; | |
910 | goto err; | |
911 | } | |
912 | per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf = page_address(p); | |
913 | mutex_init(&per_cpu_ptr(uprobe_cpu_buffer, cpu)->mutex); | |
914 | } | |
915 | ||
916 | return 0; | |
917 | ||
918 | err: | |
919 | for_each_possible_cpu(cpu) { | |
920 | if (cpu == err_cpu) | |
921 | break; | |
922 | free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf); | |
923 | } | |
924 | ||
925 | free_percpu(uprobe_cpu_buffer); | |
926 | return -ENOMEM; | |
927 | } | |
928 | ||
929 | static int uprobe_buffer_enable(void) | |
930 | { | |
931 | int ret = 0; | |
932 | ||
933 | BUG_ON(!mutex_is_locked(&event_mutex)); | |
934 | ||
935 | if (uprobe_buffer_refcnt++ == 0) { | |
936 | ret = uprobe_buffer_init(); | |
937 | if (ret < 0) | |
938 | uprobe_buffer_refcnt--; | |
939 | } | |
940 | ||
941 | return ret; | |
942 | } | |
943 | ||
944 | static void uprobe_buffer_disable(void) | |
945 | { | |
6ea6215f J |
946 | int cpu; |
947 | ||
dcad1a20 NK |
948 | BUG_ON(!mutex_is_locked(&event_mutex)); |
949 | ||
950 | if (--uprobe_buffer_refcnt == 0) { | |
6ea6215f J |
951 | for_each_possible_cpu(cpu) |
952 | free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer, | |
953 | cpu)->buf); | |
954 | ||
dcad1a20 NK |
955 | free_percpu(uprobe_cpu_buffer); |
956 | uprobe_cpu_buffer = NULL; | |
957 | } | |
958 | } | |
959 | ||
960 | static struct uprobe_cpu_buffer *uprobe_buffer_get(void) | |
961 | { | |
962 | struct uprobe_cpu_buffer *ucb; | |
963 | int cpu; | |
964 | ||
965 | cpu = raw_smp_processor_id(); | |
966 | ucb = per_cpu_ptr(uprobe_cpu_buffer, cpu); | |
967 | ||
968 | /* | |
969 | * Use per-cpu buffers for fastest access, but we might migrate | |
970 | * so the mutex makes sure we have sole access to it. | |
971 | */ | |
972 | mutex_lock(&ucb->mutex); | |
973 | ||
974 | return ucb; | |
975 | } | |
976 | ||
977 | static void uprobe_buffer_put(struct uprobe_cpu_buffer *ucb) | |
978 | { | |
1b8f85de AN |
979 | if (!ucb) |
980 | return; | |
dcad1a20 NK |
981 | mutex_unlock(&ucb->mutex); |
982 | } | |
983 | ||
3eaea21b | 984 | static struct uprobe_cpu_buffer *prepare_uprobe_buffer(struct trace_uprobe *tu, |
1b8f85de AN |
985 | struct pt_regs *regs, |
986 | struct uprobe_cpu_buffer **ucbp) | |
3eaea21b AN |
987 | { |
988 | struct uprobe_cpu_buffer *ucb; | |
989 | int dsize, esize; | |
990 | ||
1b8f85de AN |
991 | if (*ucbp) |
992 | return *ucbp; | |
993 | ||
3eaea21b AN |
994 | esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu)); |
995 | dsize = __get_data_size(&tu->tp, regs, NULL); | |
996 | ||
997 | ucb = uprobe_buffer_get(); | |
998 | ucb->dsize = tu->tp.size + dsize; | |
999 | ||
373b9338 QM |
1000 | if (WARN_ON_ONCE(ucb->dsize > MAX_UCB_BUFFER_SIZE)) { |
1001 | ucb->dsize = MAX_UCB_BUFFER_SIZE; | |
1002 | dsize = MAX_UCB_BUFFER_SIZE - tu->tp.size; | |
1003 | } | |
1004 | ||
3eaea21b AN |
1005 | store_trace_args(ucb->buf, &tu->tp, regs, NULL, esize, dsize); |
1006 | ||
1b8f85de | 1007 | *ucbp = ucb; |
3eaea21b AN |
1008 | return ucb; |
1009 | } | |
1010 | ||
a43b9704 | 1011 | static void __uprobe_trace_func(struct trace_uprobe *tu, |
dd9fa555 | 1012 | unsigned long func, struct pt_regs *regs, |
69964673 | 1013 | struct uprobe_cpu_buffer *ucb, |
7f1d2f82 | 1014 | struct trace_event_file *trace_file) |
f3f096cf SD |
1015 | { |
1016 | struct uprobe_trace_entry_head *entry; | |
b7d5eb26 | 1017 | struct trace_event_buffer fbuffer; |
457d1772 | 1018 | void *data; |
dd9fa555 | 1019 | int size, esize; |
e3dc9f89 | 1020 | struct trace_event_call *call = trace_probe_event_call(&tu->tp); |
f3f096cf | 1021 | |
7f1d2f82 | 1022 | WARN_ON(call != trace_file->event_call); |
70ed91c6 | 1023 | |
09a5059a | 1024 | if (trace_trigger_soft_disabled(trace_file)) |
ca3b1620 NK |
1025 | return; |
1026 | ||
dd9fa555 | 1027 | esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu)); |
3eaea21b | 1028 | size = esize + ucb->dsize; |
b7d5eb26 SRV |
1029 | entry = trace_event_buffer_reserve(&fbuffer, trace_file, size); |
1030 | if (!entry) | |
dd9fa555 | 1031 | return; |
f3f096cf | 1032 | |
393a736c ON |
1033 | if (is_ret_probe(tu)) { |
1034 | entry->vaddr[0] = func; | |
1035 | entry->vaddr[1] = instruction_pointer(regs); | |
1036 | data = DATAOF_TRACE_ENTRY(entry, true); | |
1037 | } else { | |
1038 | entry->vaddr[0] = instruction_pointer(regs); | |
1039 | data = DATAOF_TRACE_ENTRY(entry, false); | |
1040 | } | |
1041 | ||
3eaea21b | 1042 | memcpy(data, ucb->buf, ucb->dsize); |
f3f096cf | 1043 | |
b7d5eb26 | 1044 | trace_event_buffer_commit(&fbuffer); |
a51cc604 | 1045 | } |
f42d24a1 | 1046 | |
a51cc604 | 1047 | /* uprobe handler */ |
dd9fa555 | 1048 | static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs, |
1b8f85de | 1049 | struct uprobe_cpu_buffer **ucbp) |
a51cc604 | 1050 | { |
70ed91c6 | 1051 | struct event_file_link *link; |
69964673 | 1052 | struct uprobe_cpu_buffer *ucb; |
70ed91c6 J |
1053 | |
1054 | if (is_ret_probe(tu)) | |
1055 | return 0; | |
1056 | ||
69964673 AN |
1057 | ucb = prepare_uprobe_buffer(tu, regs, ucbp); |
1058 | ||
70ed91c6 | 1059 | rcu_read_lock(); |
b5f935ee | 1060 | trace_probe_for_each_link_rcu(link, &tu->tp) |
69964673 | 1061 | __uprobe_trace_func(tu, 0, regs, ucb, link->file); |
70ed91c6 J |
1062 | rcu_read_unlock(); |
1063 | ||
f42d24a1 | 1064 | return 0; |
f3f096cf SD |
1065 | } |
1066 | ||
c1ae5c75 | 1067 | static void uretprobe_trace_func(struct trace_uprobe *tu, unsigned long func, |
dd9fa555 | 1068 | struct pt_regs *regs, |
1b8f85de | 1069 | struct uprobe_cpu_buffer **ucbp) |
c1ae5c75 | 1070 | { |
70ed91c6 | 1071 | struct event_file_link *link; |
69964673 AN |
1072 | struct uprobe_cpu_buffer *ucb; |
1073 | ||
1074 | ucb = prepare_uprobe_buffer(tu, regs, ucbp); | |
70ed91c6 J |
1075 | |
1076 | rcu_read_lock(); | |
b5f935ee | 1077 | trace_probe_for_each_link_rcu(link, &tu->tp) |
69964673 | 1078 | __uprobe_trace_func(tu, func, regs, ucb, link->file); |
70ed91c6 | 1079 | rcu_read_unlock(); |
c1ae5c75 ON |
1080 | } |
1081 | ||
f3f096cf SD |
1082 | /* Event entry printers */ |
1083 | static enum print_line_t | |
1084 | print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event) | |
1085 | { | |
457d1772 | 1086 | struct uprobe_trace_entry_head *entry; |
f3f096cf SD |
1087 | struct trace_seq *s = &iter->seq; |
1088 | struct trace_uprobe *tu; | |
1089 | u8 *data; | |
f3f096cf | 1090 | |
457d1772 | 1091 | entry = (struct uprobe_trace_entry_head *)iter->ent; |
60d53e2c MH |
1092 | tu = trace_uprobe_primary_from_call( |
1093 | container_of(event, struct trace_event_call, event)); | |
1094 | if (unlikely(!tu)) | |
1095 | goto out; | |
f3f096cf | 1096 | |
3ede82dd | 1097 | if (is_ret_probe(tu)) { |
8579a107 | 1098 | trace_seq_printf(s, "%s: (0x%lx <- 0x%lx)", |
b55ce203 | 1099 | trace_probe_name(&tu->tp), |
8579a107 | 1100 | entry->vaddr[1], entry->vaddr[0]); |
3ede82dd ON |
1101 | data = DATAOF_TRACE_ENTRY(entry, true); |
1102 | } else { | |
8579a107 | 1103 | trace_seq_printf(s, "%s: (0x%lx)", |
b55ce203 | 1104 | trace_probe_name(&tu->tp), |
8579a107 | 1105 | entry->vaddr[0]); |
3ede82dd ON |
1106 | data = DATAOF_TRACE_ENTRY(entry, false); |
1107 | } | |
f3f096cf | 1108 | |
196b6389 | 1109 | if (trace_probe_print_args(s, tu->tp.args, tu->tp.nr_args, data, entry) < 0) |
56de7630 | 1110 | goto out; |
f3f096cf | 1111 | |
8579a107 | 1112 | trace_seq_putc(s, '\n'); |
f3f096cf | 1113 | |
8579a107 SRRH |
1114 | out: |
1115 | return trace_handle_return(s); | |
f3f096cf SD |
1116 | } |
1117 | ||
59da880a | 1118 | typedef bool (*filter_func_t)(struct uprobe_consumer *self, struct mm_struct *mm); |
31ba3348 | 1119 | |
60d53e2c | 1120 | static int trace_uprobe_enable(struct trace_uprobe *tu, filter_func_t filter) |
f3f096cf | 1121 | { |
3c83a9ad ON |
1122 | struct inode *inode = d_real_inode(tu->path.dentry); |
1123 | struct uprobe *uprobe; | |
70ed91c6 | 1124 | |
60d53e2c | 1125 | tu->consumer.filter = filter; |
3c83a9ad ON |
1126 | uprobe = uprobe_register(inode, tu->offset, tu->ref_ctr_offset, &tu->consumer); |
1127 | if (IS_ERR(uprobe)) | |
1128 | return PTR_ERR(uprobe); | |
60d53e2c | 1129 | |
3c83a9ad ON |
1130 | tu->uprobe = uprobe; |
1131 | return 0; | |
60d53e2c MH |
1132 | } |
1133 | ||
1134 | static void __probe_event_disable(struct trace_probe *tp) | |
1135 | { | |
60d53e2c | 1136 | struct trace_uprobe *tu; |
04b01625 | 1137 | bool sync = false; |
60d53e2c | 1138 | |
99c9a923 | 1139 | tu = container_of(tp, struct trace_uprobe, tp); |
b61387cb | 1140 | WARN_ON(!uprobe_filter_is_empty(tu->tp.event->filter)); |
99c9a923 | 1141 | |
e161c6bf | 1142 | list_for_each_entry(tu, trace_probe_probe_list(tp), tp.list) { |
3c83a9ad | 1143 | if (!tu->uprobe) |
60d53e2c MH |
1144 | continue; |
1145 | ||
04b01625 PZ |
1146 | uprobe_unregister_nosync(tu->uprobe, &tu->consumer); |
1147 | sync = true; | |
3c83a9ad | 1148 | tu->uprobe = NULL; |
60d53e2c | 1149 | } |
04b01625 PZ |
1150 | if (sync) |
1151 | uprobe_unregister_sync(); | |
60d53e2c MH |
1152 | } |
1153 | ||
1154 | static int probe_event_enable(struct trace_event_call *call, | |
1155 | struct trace_event_file *file, filter_func_t filter) | |
1156 | { | |
e161c6bf | 1157 | struct trace_probe *tp; |
60d53e2c MH |
1158 | struct trace_uprobe *tu; |
1159 | bool enabled; | |
1160 | int ret; | |
1161 | ||
1162 | tp = trace_probe_primary_from_call(call); | |
1163 | if (WARN_ON_ONCE(!tp)) | |
1164 | return -ENODEV; | |
1165 | enabled = trace_probe_is_enabled(tp); | |
1166 | ||
1167 | /* This may also change "enabled" state */ | |
70ed91c6 | 1168 | if (file) { |
60d53e2c | 1169 | if (trace_probe_test_flag(tp, TP_FLAG_PROFILE)) |
48212542 ON |
1170 | return -EINTR; |
1171 | ||
60d53e2c | 1172 | ret = trace_probe_add_file(tp, file); |
b5f935ee MH |
1173 | if (ret < 0) |
1174 | return ret; | |
48212542 | 1175 | } else { |
60d53e2c | 1176 | if (trace_probe_test_flag(tp, TP_FLAG_TRACE)) |
48212542 ON |
1177 | return -EINTR; |
1178 | ||
60d53e2c | 1179 | trace_probe_set_flag(tp, TP_FLAG_PROFILE); |
48212542 | 1180 | } |
f3f096cf | 1181 | |
60d53e2c | 1182 | tu = container_of(tp, struct trace_uprobe, tp); |
b61387cb | 1183 | WARN_ON(!uprobe_filter_is_empty(tu->tp.event->filter)); |
736288ba | 1184 | |
70ed91c6 J |
1185 | if (enabled) |
1186 | return 0; | |
1187 | ||
fb6bab6a ON |
1188 | ret = uprobe_buffer_enable(); |
1189 | if (ret) | |
1190 | goto err_flags; | |
1191 | ||
e161c6bf | 1192 | list_for_each_entry(tu, trace_probe_probe_list(tp), tp.list) { |
60d53e2c MH |
1193 | ret = trace_uprobe_enable(tu, filter); |
1194 | if (ret) { | |
1195 | __probe_event_disable(tp); | |
1196 | goto err_buffer; | |
1197 | } | |
1cc33161 RB |
1198 | } |
1199 | ||
fb6bab6a ON |
1200 | return 0; |
1201 | ||
1202 | err_buffer: | |
1203 | uprobe_buffer_disable(); | |
f3f096cf | 1204 | |
fb6bab6a | 1205 | err_flags: |
b5f935ee | 1206 | if (file) |
60d53e2c | 1207 | trace_probe_remove_file(tp, file); |
b5f935ee | 1208 | else |
60d53e2c | 1209 | trace_probe_clear_flag(tp, TP_FLAG_PROFILE); |
b5f935ee | 1210 | |
4161824f | 1211 | return ret; |
f3f096cf SD |
1212 | } |
1213 | ||
60d53e2c MH |
1214 | static void probe_event_disable(struct trace_event_call *call, |
1215 | struct trace_event_file *file) | |
f3f096cf | 1216 | { |
60d53e2c MH |
1217 | struct trace_probe *tp; |
1218 | ||
1219 | tp = trace_probe_primary_from_call(call); | |
1220 | if (WARN_ON_ONCE(!tp)) | |
1221 | return; | |
1222 | ||
1223 | if (!trace_probe_is_enabled(tp)) | |
f3f096cf SD |
1224 | return; |
1225 | ||
70ed91c6 | 1226 | if (file) { |
60d53e2c | 1227 | if (trace_probe_remove_file(tp, file) < 0) |
70ed91c6 J |
1228 | return; |
1229 | ||
60d53e2c | 1230 | if (trace_probe_is_enabled(tp)) |
70ed91c6 | 1231 | return; |
b5f935ee | 1232 | } else |
60d53e2c | 1233 | trace_probe_clear_flag(tp, TP_FLAG_PROFILE); |
dcad1a20 | 1234 | |
60d53e2c | 1235 | __probe_event_disable(tp); |
dcad1a20 | 1236 | uprobe_buffer_disable(); |
f3f096cf SD |
1237 | } |
1238 | ||
2425bcb9 | 1239 | static int uprobe_event_define_fields(struct trace_event_call *event_call) |
f3f096cf | 1240 | { |
eeb07b06 | 1241 | int ret, size; |
f3f096cf | 1242 | struct uprobe_trace_entry_head field; |
60d53e2c MH |
1243 | struct trace_uprobe *tu; |
1244 | ||
1245 | tu = trace_uprobe_primary_from_call(event_call); | |
1246 | if (unlikely(!tu)) | |
1247 | return -ENODEV; | |
f3f096cf | 1248 | |
4d1298e2 ON |
1249 | if (is_ret_probe(tu)) { |
1250 | DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_FUNC, 0); | |
1251 | DEFINE_FIELD(unsigned long, vaddr[1], FIELD_STRING_RETIP, 0); | |
1252 | size = SIZEOF_TRACE_ENTRY(true); | |
1253 | } else { | |
1254 | DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0); | |
1255 | size = SIZEOF_TRACE_ENTRY(false); | |
1256 | } | |
f3f096cf | 1257 | |
eeb07b06 | 1258 | return traceprobe_define_arg_fields(event_call, size, &tu->tp); |
f3f096cf SD |
1259 | } |
1260 | ||
f3f096cf | 1261 | #ifdef CONFIG_PERF_EVENTS |
31ba3348 ON |
1262 | static bool |
1263 | __uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm) | |
1264 | { | |
1265 | struct perf_event *event; | |
1266 | ||
31ba3348 | 1267 | list_for_each_entry(event, &filter->perf_events, hw.tp_list) { |
50f16a8b | 1268 | if (event->hw.target->mm == mm) |
31ba3348 ON |
1269 | return true; |
1270 | } | |
1271 | ||
1272 | return false; | |
1273 | } | |
1274 | ||
b2fe8ba6 | 1275 | static inline bool |
99c9a923 MH |
1276 | trace_uprobe_filter_event(struct trace_uprobe_filter *filter, |
1277 | struct perf_event *event) | |
b2fe8ba6 | 1278 | { |
99c9a923 | 1279 | return __uprobe_perf_filter(filter, event->hw.target->mm); |
b2fe8ba6 ON |
1280 | } |
1281 | ||
99c9a923 MH |
1282 | static bool trace_uprobe_filter_remove(struct trace_uprobe_filter *filter, |
1283 | struct perf_event *event) | |
736288ba | 1284 | { |
b2fe8ba6 ON |
1285 | bool done; |
1286 | ||
99c9a923 | 1287 | write_lock(&filter->rwlock); |
50f16a8b | 1288 | if (event->hw.target) { |
ce5f36a5 | 1289 | list_del(&event->hw.tp_list); |
99c9a923 | 1290 | done = filter->nr_systemwide || |
50f16a8b | 1291 | (event->hw.target->flags & PF_EXITING) || |
99c9a923 | 1292 | trace_uprobe_filter_event(filter, event); |
b2fe8ba6 | 1293 | } else { |
99c9a923 MH |
1294 | filter->nr_systemwide--; |
1295 | done = filter->nr_systemwide; | |
b2fe8ba6 | 1296 | } |
99c9a923 | 1297 | write_unlock(&filter->rwlock); |
31ba3348 | 1298 | |
99c9a923 | 1299 | return done; |
736288ba ON |
1300 | } |
1301 | ||
99c9a923 MH |
1302 | /* This returns true if the filter always covers target mm */ |
1303 | static bool trace_uprobe_filter_add(struct trace_uprobe_filter *filter, | |
1304 | struct perf_event *event) | |
736288ba | 1305 | { |
b2fe8ba6 ON |
1306 | bool done; |
1307 | ||
99c9a923 | 1308 | write_lock(&filter->rwlock); |
50f16a8b | 1309 | if (event->hw.target) { |
ce5f36a5 ON |
1310 | /* |
1311 | * event->parent != NULL means copy_process(), we can avoid | |
1312 | * uprobe_apply(). current->mm must be probed and we can rely | |
1313 | * on dup_mmap() which preserves the already installed bp's. | |
1314 | * | |
1315 | * attr.enable_on_exec means that exec/mmap will install the | |
1316 | * breakpoints we need. | |
1317 | */ | |
99c9a923 | 1318 | done = filter->nr_systemwide || |
ce5f36a5 | 1319 | event->parent || event->attr.enable_on_exec || |
99c9a923 MH |
1320 | trace_uprobe_filter_event(filter, event); |
1321 | list_add(&event->hw.tp_list, &filter->perf_events); | |
b2fe8ba6 | 1322 | } else { |
99c9a923 MH |
1323 | done = filter->nr_systemwide; |
1324 | filter->nr_systemwide++; | |
b2fe8ba6 | 1325 | } |
99c9a923 | 1326 | write_unlock(&filter->rwlock); |
736288ba | 1327 | |
99c9a923 | 1328 | return done; |
736288ba ON |
1329 | } |
1330 | ||
99c9a923 MH |
1331 | static int uprobe_perf_close(struct trace_event_call *call, |
1332 | struct perf_event *event) | |
60d53e2c | 1333 | { |
e161c6bf | 1334 | struct trace_probe *tp; |
60d53e2c MH |
1335 | struct trace_uprobe *tu; |
1336 | int ret = 0; | |
1337 | ||
1338 | tp = trace_probe_primary_from_call(call); | |
1339 | if (WARN_ON_ONCE(!tp)) | |
1340 | return -ENODEV; | |
1341 | ||
99c9a923 | 1342 | tu = container_of(tp, struct trace_uprobe, tp); |
b61387cb | 1343 | if (trace_uprobe_filter_remove(tu->tp.event->filter, event)) |
99c9a923 MH |
1344 | return 0; |
1345 | ||
e161c6bf | 1346 | list_for_each_entry(tu, trace_probe_probe_list(tp), tp.list) { |
3c83a9ad | 1347 | ret = uprobe_apply(tu->uprobe, &tu->consumer, false); |
60d53e2c MH |
1348 | if (ret) |
1349 | break; | |
1350 | } | |
1351 | ||
1352 | return ret; | |
1353 | } | |
99c9a923 MH |
1354 | |
1355 | static int uprobe_perf_open(struct trace_event_call *call, | |
1356 | struct perf_event *event) | |
1357 | { | |
e161c6bf | 1358 | struct trace_probe *tp; |
99c9a923 MH |
1359 | struct trace_uprobe *tu; |
1360 | int err = 0; | |
1361 | ||
1362 | tp = trace_probe_primary_from_call(call); | |
1363 | if (WARN_ON_ONCE(!tp)) | |
1364 | return -ENODEV; | |
1365 | ||
1366 | tu = container_of(tp, struct trace_uprobe, tp); | |
b61387cb | 1367 | if (trace_uprobe_filter_add(tu->tp.event->filter, event)) |
99c9a923 MH |
1368 | return 0; |
1369 | ||
e161c6bf | 1370 | list_for_each_entry(tu, trace_probe_probe_list(tp), tp.list) { |
3c83a9ad | 1371 | err = uprobe_apply(tu->uprobe, &tu->consumer, true); |
99c9a923 MH |
1372 | if (err) { |
1373 | uprobe_perf_close(call, event); | |
1374 | break; | |
1375 | } | |
1376 | } | |
1377 | ||
1378 | return err; | |
1379 | } | |
1380 | ||
59da880a | 1381 | static bool uprobe_perf_filter(struct uprobe_consumer *uc, struct mm_struct *mm) |
31ba3348 | 1382 | { |
99c9a923 | 1383 | struct trace_uprobe_filter *filter; |
31ba3348 ON |
1384 | struct trace_uprobe *tu; |
1385 | int ret; | |
1386 | ||
1387 | tu = container_of(uc, struct trace_uprobe, consumer); | |
b61387cb | 1388 | filter = tu->tp.event->filter; |
99c9a923 | 1389 | |
cdf355cc AN |
1390 | /* |
1391 | * speculative short-circuiting check to avoid unnecessarily taking | |
1392 | * filter->rwlock below, if the uprobe has system-wide consumer | |
1393 | */ | |
1394 | if (READ_ONCE(filter->nr_systemwide)) | |
1395 | return true; | |
1396 | ||
99c9a923 MH |
1397 | read_lock(&filter->rwlock); |
1398 | ret = __uprobe_perf_filter(filter, mm); | |
1399 | read_unlock(&filter->rwlock); | |
31ba3348 ON |
1400 | |
1401 | return ret; | |
1402 | } | |
1403 | ||
a43b9704 | 1404 | static void __uprobe_perf_func(struct trace_uprobe *tu, |
dd9fa555 | 1405 | unsigned long func, struct pt_regs *regs, |
1b8f85de | 1406 | struct uprobe_cpu_buffer **ucbp) |
f3f096cf | 1407 | { |
e3dc9f89 | 1408 | struct trace_event_call *call = trace_probe_event_call(&tu->tp); |
f3f096cf | 1409 | struct uprobe_trace_entry_head *entry; |
1b8f85de | 1410 | struct uprobe_cpu_buffer *ucb; |
f3f096cf | 1411 | struct hlist_head *head; |
457d1772 | 1412 | void *data; |
dd9fa555 | 1413 | int size, esize; |
dcad1a20 NK |
1414 | int rctx; |
1415 | ||
aca80dd9 | 1416 | #ifdef CONFIG_BPF_EVENTS |
70ed0706 | 1417 | if (bpf_prog_array_valid(call)) { |
7d0d6736 | 1418 | const struct bpf_prog_array *array; |
70ed0706 AS |
1419 | u32 ret; |
1420 | ||
7d0d6736 JH |
1421 | rcu_read_lock_trace(); |
1422 | array = rcu_dereference_check(call->prog_array, rcu_read_lock_trace_held()); | |
1423 | ret = bpf_prog_run_array_uprobe(array, regs, bpf_prog_run); | |
1424 | rcu_read_unlock_trace(); | |
70ed0706 AS |
1425 | if (!ret) |
1426 | return; | |
1427 | } | |
aca80dd9 | 1428 | #endif /* CONFIG_BPF_EVENTS */ |
04a22fae | 1429 | |
dcad1a20 | 1430 | esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu)); |
f3f096cf | 1431 | |
1b8f85de | 1432 | ucb = prepare_uprobe_buffer(tu, regs, ucbp); |
3eaea21b | 1433 | size = esize + ucb->dsize; |
dcad1a20 NK |
1434 | size = ALIGN(size + sizeof(u32), sizeof(u64)) - sizeof(u32); |
1435 | if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough")) | |
1436 | return; | |
1437 | ||
f3f096cf | 1438 | preempt_disable(); |
515619f2 ON |
1439 | head = this_cpu_ptr(call->perf_events); |
1440 | if (hlist_empty(head)) | |
1441 | goto out; | |
1442 | ||
1e1dcd93 | 1443 | entry = perf_trace_buf_alloc(size, NULL, &rctx); |
f3f096cf SD |
1444 | if (!entry) |
1445 | goto out; | |
1446 | ||
393a736c ON |
1447 | if (is_ret_probe(tu)) { |
1448 | entry->vaddr[0] = func; | |
32520b2c | 1449 | entry->vaddr[1] = instruction_pointer(regs); |
393a736c ON |
1450 | data = DATAOF_TRACE_ENTRY(entry, true); |
1451 | } else { | |
32520b2c | 1452 | entry->vaddr[0] = instruction_pointer(regs); |
393a736c ON |
1453 | data = DATAOF_TRACE_ENTRY(entry, false); |
1454 | } | |
1455 | ||
3eaea21b | 1456 | memcpy(data, ucb->buf, ucb->dsize); |
dcad1a20 | 1457 | |
3eaea21b AN |
1458 | if (size - esize > ucb->dsize) |
1459 | memset(data + ucb->dsize, 0, size - esize - ucb->dsize); | |
f3f096cf | 1460 | |
1e1dcd93 | 1461 | perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs, |
8fd0fbbe | 1462 | head, NULL); |
f3f096cf SD |
1463 | out: |
1464 | preempt_enable(); | |
a51cc604 ON |
1465 | } |
1466 | ||
1467 | /* uprobe profile handler */ | |
dd9fa555 | 1468 | static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs, |
1b8f85de | 1469 | struct uprobe_cpu_buffer **ucbp) |
a51cc604 | 1470 | { |
59da880a | 1471 | if (!uprobe_perf_filter(&tu->consumer, current->mm)) |
a51cc604 ON |
1472 | return UPROBE_HANDLER_REMOVE; |
1473 | ||
393a736c | 1474 | if (!is_ret_probe(tu)) |
1b8f85de | 1475 | __uprobe_perf_func(tu, 0, regs, ucbp); |
f42d24a1 | 1476 | return 0; |
f3f096cf | 1477 | } |
c1ae5c75 ON |
1478 | |
1479 | static void uretprobe_perf_func(struct trace_uprobe *tu, unsigned long func, | |
dd9fa555 | 1480 | struct pt_regs *regs, |
1b8f85de | 1481 | struct uprobe_cpu_buffer **ucbp) |
c1ae5c75 | 1482 | { |
1b8f85de | 1483 | __uprobe_perf_func(tu, func, regs, ucbp); |
c1ae5c75 | 1484 | } |
41bdc4b4 YS |
1485 | |
1486 | int bpf_get_uprobe_info(const struct perf_event *event, u32 *fd_type, | |
1487 | const char **filename, u64 *probe_offset, | |
5125e757 | 1488 | u64 *probe_addr, bool perf_type_tracepoint) |
41bdc4b4 YS |
1489 | { |
1490 | const char *pevent = trace_event_name(event->tp_event); | |
1491 | const char *group = event->tp_event->class->system; | |
1492 | struct trace_uprobe *tu; | |
1493 | ||
1494 | if (perf_type_tracepoint) | |
1495 | tu = find_probe_event(pevent, group); | |
1496 | else | |
22d5bd68 | 1497 | tu = trace_uprobe_primary_from_call(event->tp_event); |
41bdc4b4 YS |
1498 | if (!tu) |
1499 | return -EINVAL; | |
1500 | ||
1501 | *fd_type = is_ret_probe(tu) ? BPF_FD_TYPE_URETPROBE | |
1502 | : BPF_FD_TYPE_UPROBE; | |
1503 | *filename = tu->filename; | |
1504 | *probe_offset = tu->offset; | |
82315333 | 1505 | *probe_addr = tu->ref_ctr_offset; |
41bdc4b4 YS |
1506 | return 0; |
1507 | } | |
f3f096cf SD |
1508 | #endif /* CONFIG_PERF_EVENTS */ |
1509 | ||
70ed91c6 | 1510 | static int |
2425bcb9 | 1511 | trace_uprobe_register(struct trace_event_call *event, enum trace_reg type, |
70ed91c6 | 1512 | void *data) |
f3f096cf | 1513 | { |
7f1d2f82 | 1514 | struct trace_event_file *file = data; |
f3f096cf SD |
1515 | |
1516 | switch (type) { | |
1517 | case TRACE_REG_REGISTER: | |
60d53e2c | 1518 | return probe_event_enable(event, file, NULL); |
f3f096cf SD |
1519 | |
1520 | case TRACE_REG_UNREGISTER: | |
60d53e2c | 1521 | probe_event_disable(event, file); |
f3f096cf SD |
1522 | return 0; |
1523 | ||
1524 | #ifdef CONFIG_PERF_EVENTS | |
1525 | case TRACE_REG_PERF_REGISTER: | |
60d53e2c | 1526 | return probe_event_enable(event, NULL, uprobe_perf_filter); |
f3f096cf SD |
1527 | |
1528 | case TRACE_REG_PERF_UNREGISTER: | |
60d53e2c | 1529 | probe_event_disable(event, NULL); |
f3f096cf | 1530 | return 0; |
736288ba ON |
1531 | |
1532 | case TRACE_REG_PERF_OPEN: | |
99c9a923 | 1533 | return uprobe_perf_open(event, data); |
736288ba ON |
1534 | |
1535 | case TRACE_REG_PERF_CLOSE: | |
99c9a923 | 1536 | return uprobe_perf_close(event, data); |
736288ba | 1537 | |
f3f096cf SD |
1538 | #endif |
1539 | default: | |
1540 | return 0; | |
1541 | } | |
f3f096cf SD |
1542 | } |
1543 | ||
da09a9e0 JO |
1544 | static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs, |
1545 | __u64 *data) | |
f3f096cf | 1546 | { |
f3f096cf | 1547 | struct trace_uprobe *tu; |
b7e0bf34 | 1548 | struct uprobe_dispatch_data udd; |
1b8f85de | 1549 | struct uprobe_cpu_buffer *ucb = NULL; |
f42d24a1 | 1550 | int ret = 0; |
f3f096cf | 1551 | |
a932b738 | 1552 | tu = container_of(con, struct trace_uprobe, consumer); |
10cdb82a AN |
1553 | |
1554 | this_cpu_inc(*tu->nhits); | |
f3f096cf | 1555 | |
b7e0bf34 NK |
1556 | udd.tu = tu; |
1557 | udd.bp_addr = instruction_pointer(regs); | |
1558 | ||
1559 | current->utask->vaddr = (unsigned long) &udd; | |
1560 | ||
dd9fa555 NK |
1561 | if (WARN_ON_ONCE(!uprobe_cpu_buffer)) |
1562 | return 0; | |
1563 | ||
747774d6 | 1564 | if (trace_probe_test_flag(&tu->tp, TP_FLAG_TRACE)) |
1b8f85de | 1565 | ret |= uprobe_trace_func(tu, regs, &ucb); |
f3f096cf SD |
1566 | |
1567 | #ifdef CONFIG_PERF_EVENTS | |
747774d6 | 1568 | if (trace_probe_test_flag(&tu->tp, TP_FLAG_PROFILE)) |
1b8f85de | 1569 | ret |= uprobe_perf_func(tu, regs, &ucb); |
f3f096cf | 1570 | #endif |
dd9fa555 | 1571 | uprobe_buffer_put(ucb); |
f42d24a1 | 1572 | return ret; |
f3f096cf SD |
1573 | } |
1574 | ||
c1ae5c75 | 1575 | static int uretprobe_dispatcher(struct uprobe_consumer *con, |
da09a9e0 JO |
1576 | unsigned long func, struct pt_regs *regs, |
1577 | __u64 *data) | |
c1ae5c75 ON |
1578 | { |
1579 | struct trace_uprobe *tu; | |
b7e0bf34 | 1580 | struct uprobe_dispatch_data udd; |
1b8f85de | 1581 | struct uprobe_cpu_buffer *ucb = NULL; |
c1ae5c75 ON |
1582 | |
1583 | tu = container_of(con, struct trace_uprobe, consumer); | |
1584 | ||
b7e0bf34 NK |
1585 | udd.tu = tu; |
1586 | udd.bp_addr = func; | |
1587 | ||
1588 | current->utask->vaddr = (unsigned long) &udd; | |
1589 | ||
dd9fa555 NK |
1590 | if (WARN_ON_ONCE(!uprobe_cpu_buffer)) |
1591 | return 0; | |
1592 | ||
747774d6 | 1593 | if (trace_probe_test_flag(&tu->tp, TP_FLAG_TRACE)) |
1b8f85de | 1594 | uretprobe_trace_func(tu, func, regs, &ucb); |
c1ae5c75 ON |
1595 | |
1596 | #ifdef CONFIG_PERF_EVENTS | |
747774d6 | 1597 | if (trace_probe_test_flag(&tu->tp, TP_FLAG_PROFILE)) |
1b8f85de | 1598 | uretprobe_perf_func(tu, func, regs, &ucb); |
c1ae5c75 | 1599 | #endif |
dd9fa555 | 1600 | uprobe_buffer_put(ucb); |
c1ae5c75 ON |
1601 | return 0; |
1602 | } | |
1603 | ||
f3f096cf SD |
1604 | static struct trace_event_functions uprobe_funcs = { |
1605 | .trace = print_uprobe_event | |
1606 | }; | |
1607 | ||
04ae87a5 PZ |
1608 | static struct trace_event_fields uprobe_fields_array[] = { |
1609 | { .type = TRACE_FUNCTION_TYPE, | |
1610 | .define_fields = uprobe_event_define_fields }, | |
1611 | {} | |
1612 | }; | |
1613 | ||
e3dc9f89 | 1614 | static inline void init_trace_event_call(struct trace_uprobe *tu) |
f3f096cf | 1615 | { |
e3dc9f89 | 1616 | struct trace_event_call *call = trace_probe_event_call(&tu->tp); |
f3f096cf | 1617 | call->event.funcs = &uprobe_funcs; |
04ae87a5 | 1618 | call->class->fields_array = uprobe_fields_array; |
f3f096cf | 1619 | |
9fd2e48b | 1620 | call->flags = TRACE_EVENT_FL_UPROBE | TRACE_EVENT_FL_CAP_ANY; |
33ea4b24 | 1621 | call->class->reg = trace_uprobe_register; |
33ea4b24 SL |
1622 | } |
1623 | ||
1624 | static int register_uprobe_event(struct trace_uprobe *tu) | |
1625 | { | |
e3dc9f89 | 1626 | init_trace_event_call(tu); |
f3f096cf | 1627 | |
46e5376d | 1628 | return trace_probe_register_event_call(&tu->tp); |
f3f096cf SD |
1629 | } |
1630 | ||
c6c2401d | 1631 | static int unregister_uprobe_event(struct trace_uprobe *tu) |
f3f096cf | 1632 | { |
46e5376d | 1633 | return trace_probe_unregister_event_call(&tu->tp); |
f3f096cf SD |
1634 | } |
1635 | ||
33ea4b24 SL |
1636 | #ifdef CONFIG_PERF_EVENTS |
1637 | struct trace_event_call * | |
a6ca88b2 SL |
1638 | create_local_trace_uprobe(char *name, unsigned long offs, |
1639 | unsigned long ref_ctr_offset, bool is_return) | |
33ea4b24 | 1640 | { |
007517a0 | 1641 | enum probe_print_type ptype; |
33ea4b24 | 1642 | struct trace_uprobe *tu; |
33ea4b24 SL |
1643 | struct path path; |
1644 | int ret; | |
1645 | ||
1646 | ret = kern_path(name, LOOKUP_FOLLOW, &path); | |
1647 | if (ret) | |
1648 | return ERR_PTR(ret); | |
1649 | ||
0c92c7a3 SL |
1650 | if (!d_is_reg(path.dentry)) { |
1651 | path_put(&path); | |
33ea4b24 SL |
1652 | return ERR_PTR(-EINVAL); |
1653 | } | |
1654 | ||
1655 | /* | |
0597c49c | 1656 | * local trace_kprobes are not added to dyn_event, so they are never |
33ea4b24 SL |
1657 | * searched in find_trace_kprobe(). Therefore, there is no concern of |
1658 | * duplicated name "DUMMY_EVENT" here. | |
1659 | */ | |
1660 | tu = alloc_trace_uprobe(UPROBE_EVENT_SYSTEM, "DUMMY_EVENT", 0, | |
1661 | is_return); | |
1662 | ||
1663 | if (IS_ERR(tu)) { | |
1664 | pr_info("Failed to allocate trace_uprobe.(%d)\n", | |
1665 | (int)PTR_ERR(tu)); | |
0c92c7a3 | 1666 | path_put(&path); |
33ea4b24 SL |
1667 | return ERR_CAST(tu); |
1668 | } | |
1669 | ||
1670 | tu->offset = offs; | |
0c92c7a3 | 1671 | tu->path = path; |
a6ca88b2 | 1672 | tu->ref_ctr_offset = ref_ctr_offset; |
33ea4b24 | 1673 | tu->filename = kstrdup(name, GFP_KERNEL); |
8c722424 XW |
1674 | if (!tu->filename) { |
1675 | ret = -ENOMEM; | |
1676 | goto error; | |
1677 | } | |
1678 | ||
e3dc9f89 | 1679 | init_trace_event_call(tu); |
33ea4b24 | 1680 | |
007517a0 SRV |
1681 | ptype = is_ret_probe(tu) ? PROBE_PRINT_RETURN : PROBE_PRINT_NORMAL; |
1682 | if (traceprobe_set_print_fmt(&tu->tp, ptype) < 0) { | |
33ea4b24 SL |
1683 | ret = -ENOMEM; |
1684 | goto error; | |
1685 | } | |
1686 | ||
e3dc9f89 | 1687 | return trace_probe_event_call(&tu->tp); |
33ea4b24 SL |
1688 | error: |
1689 | free_trace_uprobe(tu); | |
1690 | return ERR_PTR(ret); | |
1691 | } | |
1692 | ||
1693 | void destroy_local_trace_uprobe(struct trace_event_call *event_call) | |
1694 | { | |
1695 | struct trace_uprobe *tu; | |
1696 | ||
60d53e2c | 1697 | tu = trace_uprobe_primary_from_call(event_call); |
33ea4b24 | 1698 | |
33ea4b24 SL |
1699 | free_trace_uprobe(tu); |
1700 | } | |
1701 | #endif /* CONFIG_PERF_EVENTS */ | |
1702 | ||
39bcdd6a | 1703 | /* Make a trace interface for controlling probe points */ |
f3f096cf SD |
1704 | static __init int init_uprobe_trace(void) |
1705 | { | |
0597c49c MH |
1706 | int ret; |
1707 | ||
1708 | ret = dyn_event_register(&trace_uprobe_ops); | |
1709 | if (ret) | |
1710 | return ret; | |
f3f096cf | 1711 | |
22c36b18 WY |
1712 | ret = tracing_init_dentry(); |
1713 | if (ret) | |
f3f096cf SD |
1714 | return 0; |
1715 | ||
21ccc9cd | 1716 | trace_create_file("uprobe_events", TRACE_MODE_WRITE, NULL, |
f3f096cf SD |
1717 | NULL, &uprobe_events_ops); |
1718 | /* Profile interface */ | |
21ccc9cd | 1719 | trace_create_file("uprobe_profile", TRACE_MODE_READ, NULL, |
f3f096cf SD |
1720 | NULL, &uprobe_profile_ops); |
1721 | return 0; | |
1722 | } | |
1723 | ||
1724 | fs_initcall(init_uprobe_trace); |