]>
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 | */ | |
72576341 | 8 | #define pr_fmt(fmt) "trace_kprobe: " fmt |
f3f096cf SD |
9 | |
10 | #include <linux/module.h> | |
11 | #include <linux/uaccess.h> | |
12 | #include <linux/uprobes.h> | |
13 | #include <linux/namei.h> | |
b2e902f0 | 14 | #include <linux/string.h> |
b2d09103 | 15 | #include <linux/rculist.h> |
f3f096cf SD |
16 | |
17 | #include "trace_probe.h" | |
53305928 | 18 | #include "trace_probe_tmpl.h" |
f3f096cf SD |
19 | |
20 | #define UPROBE_EVENT_SYSTEM "uprobes" | |
21 | ||
457d1772 ON |
22 | struct uprobe_trace_entry_head { |
23 | struct trace_entry ent; | |
24 | unsigned long vaddr[]; | |
25 | }; | |
26 | ||
27 | #define SIZEOF_TRACE_ENTRY(is_return) \ | |
28 | (sizeof(struct uprobe_trace_entry_head) + \ | |
29 | sizeof(unsigned long) * (is_return ? 2 : 1)) | |
30 | ||
31 | #define DATAOF_TRACE_ENTRY(entry, is_return) \ | |
32 | ((void*)(entry) + SIZEOF_TRACE_ENTRY(is_return)) | |
33 | ||
736288ba ON |
34 | struct trace_uprobe_filter { |
35 | rwlock_t rwlock; | |
36 | int nr_systemwide; | |
37 | struct list_head perf_events; | |
38 | }; | |
39 | ||
f3f096cf SD |
40 | /* |
41 | * uprobe event core functions | |
42 | */ | |
f3f096cf SD |
43 | struct trace_uprobe { |
44 | struct list_head list; | |
736288ba | 45 | struct trace_uprobe_filter filter; |
a932b738 | 46 | struct uprobe_consumer consumer; |
0c92c7a3 | 47 | struct path path; |
f3f096cf SD |
48 | struct inode *inode; |
49 | char *filename; | |
50 | unsigned long offset; | |
1cc33161 | 51 | unsigned long ref_ctr_offset; |
f3f096cf | 52 | unsigned long nhit; |
14577c39 | 53 | struct trace_probe tp; |
f3f096cf SD |
54 | }; |
55 | ||
14577c39 NK |
56 | #define SIZEOF_TRACE_UPROBE(n) \ |
57 | (offsetof(struct trace_uprobe, tp.args) + \ | |
f3f096cf SD |
58 | (sizeof(struct probe_arg) * (n))) |
59 | ||
60 | static int register_uprobe_event(struct trace_uprobe *tu); | |
c6c2401d | 61 | static int unregister_uprobe_event(struct trace_uprobe *tu); |
f3f096cf SD |
62 | |
63 | static DEFINE_MUTEX(uprobe_lock); | |
64 | static LIST_HEAD(uprobe_list); | |
65 | ||
b7e0bf34 NK |
66 | struct uprobe_dispatch_data { |
67 | struct trace_uprobe *tu; | |
68 | unsigned long bp_addr; | |
69 | }; | |
70 | ||
f3f096cf | 71 | static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs); |
c1ae5c75 ON |
72 | static int uretprobe_dispatcher(struct uprobe_consumer *con, |
73 | unsigned long func, struct pt_regs *regs); | |
f3f096cf | 74 | |
3fd996a2 NK |
75 | #ifdef CONFIG_STACK_GROWSUP |
76 | static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n) | |
77 | { | |
78 | return addr - (n * sizeof(long)); | |
79 | } | |
80 | #else | |
81 | static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n) | |
82 | { | |
83 | return addr + (n * sizeof(long)); | |
84 | } | |
85 | #endif | |
86 | ||
87 | static unsigned long get_user_stack_nth(struct pt_regs *regs, unsigned int n) | |
88 | { | |
89 | unsigned long ret; | |
90 | unsigned long addr = user_stack_pointer(regs); | |
91 | ||
92 | addr = adjust_stack_addr(addr, n); | |
93 | ||
94 | if (copy_from_user(&ret, (void __force __user *) addr, sizeof(ret))) | |
95 | return 0; | |
96 | ||
97 | return ret; | |
98 | } | |
99 | ||
100 | /* | |
101 | * Uprobes-specific fetch functions | |
102 | */ | |
53305928 | 103 | static nokprobe_inline int |
9b960a38 | 104 | probe_mem_read(void *dest, void *src, size_t size) |
53305928 MH |
105 | { |
106 | void __user *vaddr = (void __force __user *)src; | |
107 | ||
f3f58935 | 108 | return copy_from_user(dest, vaddr, size) ? -EFAULT : 0; |
5baaa59e | 109 | } |
5baaa59e NK |
110 | /* |
111 | * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max | |
112 | * length and relative data location. | |
113 | */ | |
9178412d MH |
114 | static nokprobe_inline int |
115 | fetch_store_string(unsigned long addr, void *dest, void *base) | |
5baaa59e NK |
116 | { |
117 | long ret; | |
9178412d MH |
118 | u32 loc = *(u32 *)dest; |
119 | int maxlen = get_loc_len(loc); | |
120 | u8 *dst = get_loc_data(dest, base); | |
5baaa59e NK |
121 | void __user *src = (void __force __user *) addr; |
122 | ||
9178412d MH |
123 | if (unlikely(!maxlen)) |
124 | return -ENOMEM; | |
5baaa59e NK |
125 | |
126 | ret = strncpy_from_user(dst, src, maxlen); | |
9178412d MH |
127 | if (ret >= 0) { |
128 | if (ret == maxlen) | |
129 | dst[ret - 1] = '\0'; | |
130 | *(u32 *)dest = make_data_loc(ret, (void *)dst - base); | |
5baaa59e | 131 | } |
9178412d MH |
132 | |
133 | return ret; | |
5baaa59e NK |
134 | } |
135 | ||
53305928 | 136 | /* Return the length of string -- including null terminal byte */ |
9178412d MH |
137 | static nokprobe_inline int |
138 | fetch_store_strlen(unsigned long addr) | |
5baaa59e NK |
139 | { |
140 | int len; | |
141 | void __user *vaddr = (void __force __user *) addr; | |
142 | ||
143 | len = strnlen_user(vaddr, MAX_STRING_SIZE); | |
144 | ||
9178412d | 145 | return (len > MAX_STRING_SIZE) ? 0 : len; |
5baaa59e | 146 | } |
3fd996a2 | 147 | |
53305928 | 148 | static unsigned long translate_user_vaddr(unsigned long file_offset) |
b7e0bf34 NK |
149 | { |
150 | unsigned long base_addr; | |
151 | struct uprobe_dispatch_data *udd; | |
152 | ||
153 | udd = (void *) current->utask->vaddr; | |
154 | ||
155 | base_addr = udd->bp_addr - udd->tu->offset; | |
53305928 | 156 | return base_addr + file_offset; |
b7e0bf34 | 157 | } |
b7e0bf34 | 158 | |
53305928 MH |
159 | /* Note that we don't verify it, since the code does not come from user space */ |
160 | static int | |
161 | process_fetch_insn(struct fetch_insn *code, struct pt_regs *regs, void *dest, | |
9178412d | 162 | void *base) |
53305928 MH |
163 | { |
164 | unsigned long val; | |
53305928 MH |
165 | |
166 | /* 1st stage: get value from context */ | |
167 | switch (code->op) { | |
168 | case FETCH_OP_REG: | |
169 | val = regs_get_register(regs, code->param); | |
170 | break; | |
171 | case FETCH_OP_STACK: | |
172 | val = get_user_stack_nth(regs, code->param); | |
173 | break; | |
174 | case FETCH_OP_STACKP: | |
175 | val = user_stack_pointer(regs); | |
176 | break; | |
177 | case FETCH_OP_RETVAL: | |
178 | val = regs_return_value(regs); | |
179 | break; | |
180 | case FETCH_OP_IMM: | |
181 | val = code->immediate; | |
182 | break; | |
183 | case FETCH_OP_FOFFS: | |
184 | val = translate_user_vaddr(code->immediate); | |
185 | break; | |
186 | default: | |
187 | return -EILSEQ; | |
188 | } | |
189 | code++; | |
190 | ||
9b960a38 | 191 | return process_fetch_insn_bottom(code, val, dest, base); |
53305928 MH |
192 | } |
193 | NOKPROBE_SYMBOL(process_fetch_insn) | |
194 | ||
736288ba ON |
195 | static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter) |
196 | { | |
197 | rwlock_init(&filter->rwlock); | |
198 | filter->nr_systemwide = 0; | |
199 | INIT_LIST_HEAD(&filter->perf_events); | |
200 | } | |
201 | ||
202 | static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter *filter) | |
203 | { | |
204 | return !filter->nr_systemwide && list_empty(&filter->perf_events); | |
205 | } | |
206 | ||
c1ae5c75 ON |
207 | static inline bool is_ret_probe(struct trace_uprobe *tu) |
208 | { | |
209 | return tu->consumer.ret_handler != NULL; | |
210 | } | |
211 | ||
f3f096cf SD |
212 | /* |
213 | * Allocate new trace_uprobe and initialize it (including uprobes). | |
214 | */ | |
215 | static struct trace_uprobe * | |
c1ae5c75 | 216 | alloc_trace_uprobe(const char *group, const char *event, int nargs, bool is_ret) |
f3f096cf SD |
217 | { |
218 | struct trace_uprobe *tu; | |
219 | ||
220 | if (!event || !is_good_name(event)) | |
221 | return ERR_PTR(-EINVAL); | |
222 | ||
223 | if (!group || !is_good_name(group)) | |
224 | return ERR_PTR(-EINVAL); | |
225 | ||
226 | tu = kzalloc(SIZEOF_TRACE_UPROBE(nargs), GFP_KERNEL); | |
227 | if (!tu) | |
228 | return ERR_PTR(-ENOMEM); | |
229 | ||
14577c39 NK |
230 | tu->tp.call.class = &tu->tp.class; |
231 | tu->tp.call.name = kstrdup(event, GFP_KERNEL); | |
232 | if (!tu->tp.call.name) | |
f3f096cf SD |
233 | goto error; |
234 | ||
14577c39 NK |
235 | tu->tp.class.system = kstrdup(group, GFP_KERNEL); |
236 | if (!tu->tp.class.system) | |
f3f096cf SD |
237 | goto error; |
238 | ||
239 | INIT_LIST_HEAD(&tu->list); | |
70ed91c6 | 240 | INIT_LIST_HEAD(&tu->tp.files); |
a932b738 | 241 | tu->consumer.handler = uprobe_dispatcher; |
c1ae5c75 ON |
242 | if (is_ret) |
243 | tu->consumer.ret_handler = uretprobe_dispatcher; | |
736288ba | 244 | init_trace_uprobe_filter(&tu->filter); |
f3f096cf SD |
245 | return tu; |
246 | ||
247 | error: | |
14577c39 | 248 | kfree(tu->tp.call.name); |
f3f096cf SD |
249 | kfree(tu); |
250 | ||
251 | return ERR_PTR(-ENOMEM); | |
252 | } | |
253 | ||
254 | static void free_trace_uprobe(struct trace_uprobe *tu) | |
255 | { | |
256 | int i; | |
257 | ||
14577c39 NK |
258 | for (i = 0; i < tu->tp.nr_args; i++) |
259 | traceprobe_free_probe_arg(&tu->tp.args[i]); | |
f3f096cf | 260 | |
0c92c7a3 | 261 | path_put(&tu->path); |
14577c39 NK |
262 | kfree(tu->tp.call.class->system); |
263 | kfree(tu->tp.call.name); | |
f3f096cf SD |
264 | kfree(tu->filename); |
265 | kfree(tu); | |
266 | } | |
267 | ||
268 | static struct trace_uprobe *find_probe_event(const char *event, const char *group) | |
269 | { | |
270 | struct trace_uprobe *tu; | |
271 | ||
272 | list_for_each_entry(tu, &uprobe_list, list) | |
687fcc4a | 273 | if (strcmp(trace_event_name(&tu->tp.call), event) == 0 && |
14577c39 | 274 | strcmp(tu->tp.call.class->system, group) == 0) |
f3f096cf SD |
275 | return tu; |
276 | ||
277 | return NULL; | |
278 | } | |
279 | ||
280 | /* Unregister a trace_uprobe and probe_event: call with locking uprobe_lock */ | |
c6c2401d | 281 | static int unregister_trace_uprobe(struct trace_uprobe *tu) |
f3f096cf | 282 | { |
c6c2401d SRRH |
283 | int ret; |
284 | ||
285 | ret = unregister_uprobe_event(tu); | |
286 | if (ret) | |
287 | return ret; | |
288 | ||
f3f096cf | 289 | list_del(&tu->list); |
f3f096cf | 290 | free_trace_uprobe(tu); |
c6c2401d | 291 | return 0; |
f3f096cf SD |
292 | } |
293 | ||
ccea8727 RB |
294 | /* |
295 | * Uprobe with multiple reference counter is not allowed. i.e. | |
296 | * If inode and offset matches, reference counter offset *must* | |
297 | * match as well. Though, there is one exception: If user is | |
298 | * replacing old trace_uprobe with new one(same group/event), | |
299 | * then we allow same uprobe with new reference counter as far | |
300 | * as the new one does not conflict with any other existing | |
301 | * ones. | |
302 | */ | |
303 | static struct trace_uprobe *find_old_trace_uprobe(struct trace_uprobe *new) | |
304 | { | |
305 | struct trace_uprobe *tmp, *old = NULL; | |
306 | struct inode *new_inode = d_real_inode(new->path.dentry); | |
307 | ||
308 | old = find_probe_event(trace_event_name(&new->tp.call), | |
309 | new->tp.call.class->system); | |
310 | ||
311 | list_for_each_entry(tmp, &uprobe_list, list) { | |
312 | if ((old ? old != tmp : true) && | |
313 | new_inode == d_real_inode(tmp->path.dentry) && | |
314 | new->offset == tmp->offset && | |
315 | new->ref_ctr_offset != tmp->ref_ctr_offset) { | |
316 | pr_warn("Reference counter offset mismatch."); | |
317 | return ERR_PTR(-EINVAL); | |
318 | } | |
319 | } | |
320 | return old; | |
321 | } | |
322 | ||
f3f096cf SD |
323 | /* Register a trace_uprobe and probe_event */ |
324 | static int register_trace_uprobe(struct trace_uprobe *tu) | |
325 | { | |
14577c39 | 326 | struct trace_uprobe *old_tu; |
f3f096cf SD |
327 | int ret; |
328 | ||
329 | mutex_lock(&uprobe_lock); | |
330 | ||
331 | /* register as an event */ | |
ccea8727 RB |
332 | old_tu = find_old_trace_uprobe(tu); |
333 | if (IS_ERR(old_tu)) { | |
334 | ret = PTR_ERR(old_tu); | |
335 | goto end; | |
336 | } | |
337 | ||
14577c39 | 338 | if (old_tu) { |
f3f096cf | 339 | /* delete old event */ |
14577c39 | 340 | ret = unregister_trace_uprobe(old_tu); |
c6c2401d SRRH |
341 | if (ret) |
342 | goto end; | |
343 | } | |
f3f096cf SD |
344 | |
345 | ret = register_uprobe_event(tu); | |
346 | if (ret) { | |
a395d6a7 | 347 | pr_warn("Failed to register probe event(%d)\n", ret); |
f3f096cf SD |
348 | goto end; |
349 | } | |
350 | ||
351 | list_add_tail(&tu->list, &uprobe_list); | |
352 | ||
353 | end: | |
354 | mutex_unlock(&uprobe_lock); | |
355 | ||
356 | return ret; | |
357 | } | |
358 | ||
359 | /* | |
360 | * Argument syntax: | |
306cfe20 | 361 | * - Add uprobe: p|r[:[GRP/]EVENT] PATH:OFFSET [FETCHARGS] |
f3f096cf SD |
362 | * |
363 | * - Remove uprobe: -:[GRP/]EVENT | |
364 | */ | |
365 | static int create_trace_uprobe(int argc, char **argv) | |
366 | { | |
367 | struct trace_uprobe *tu; | |
1cc33161 | 368 | char *arg, *event, *group, *filename, *rctr, *rctr_end; |
f3f096cf SD |
369 | char buf[MAX_EVENT_NAME_LEN]; |
370 | struct path path; | |
1cc33161 | 371 | unsigned long offset, ref_ctr_offset; |
4ee5a52e | 372 | bool is_delete, is_return; |
f3f096cf SD |
373 | int i, ret; |
374 | ||
f3f096cf SD |
375 | ret = 0; |
376 | is_delete = false; | |
4ee5a52e | 377 | is_return = false; |
f3f096cf SD |
378 | event = NULL; |
379 | group = NULL; | |
1cc33161 | 380 | ref_ctr_offset = 0; |
f3f096cf SD |
381 | |
382 | /* argc must be >= 1 */ | |
383 | if (argv[0][0] == '-') | |
384 | is_delete = true; | |
4ee5a52e ON |
385 | else if (argv[0][0] == 'r') |
386 | is_return = true; | |
f3f096cf | 387 | else if (argv[0][0] != 'p') { |
4ee5a52e | 388 | pr_info("Probe definition must be started with 'p', 'r' or '-'.\n"); |
f3f096cf SD |
389 | return -EINVAL; |
390 | } | |
391 | ||
392 | if (argv[0][1] == ':') { | |
393 | event = &argv[0][2]; | |
394 | arg = strchr(event, '/'); | |
395 | ||
396 | if (arg) { | |
397 | group = event; | |
398 | event = arg + 1; | |
399 | event[-1] = '\0'; | |
400 | ||
401 | if (strlen(group) == 0) { | |
402 | pr_info("Group name is not specified\n"); | |
403 | return -EINVAL; | |
404 | } | |
405 | } | |
406 | if (strlen(event) == 0) { | |
407 | pr_info("Event name is not specified\n"); | |
408 | return -EINVAL; | |
409 | } | |
410 | } | |
411 | if (!group) | |
412 | group = UPROBE_EVENT_SYSTEM; | |
413 | ||
414 | if (is_delete) { | |
c6c2401d SRRH |
415 | int ret; |
416 | ||
f3f096cf SD |
417 | if (!event) { |
418 | pr_info("Delete command needs an event name.\n"); | |
419 | return -EINVAL; | |
420 | } | |
421 | mutex_lock(&uprobe_lock); | |
422 | tu = find_probe_event(event, group); | |
423 | ||
424 | if (!tu) { | |
425 | mutex_unlock(&uprobe_lock); | |
426 | pr_info("Event %s/%s doesn't exist.\n", group, event); | |
427 | return -ENOENT; | |
428 | } | |
429 | /* delete an event */ | |
c6c2401d | 430 | ret = unregister_trace_uprobe(tu); |
f3f096cf | 431 | mutex_unlock(&uprobe_lock); |
c6c2401d | 432 | return ret; |
f3f096cf SD |
433 | } |
434 | ||
435 | if (argc < 2) { | |
436 | pr_info("Probe point is not specified.\n"); | |
437 | return -EINVAL; | |
438 | } | |
6496bb72 KY |
439 | /* Find the last occurrence, in case the path contains ':' too. */ |
440 | arg = strrchr(argv[1], ':'); | |
0c92c7a3 SL |
441 | if (!arg) |
442 | return -EINVAL; | |
f3f096cf SD |
443 | |
444 | *arg++ = '\0'; | |
445 | filename = argv[1]; | |
446 | ret = kern_path(filename, LOOKUP_FOLLOW, &path); | |
447 | if (ret) | |
0c92c7a3 | 448 | return ret; |
84d7ed79 | 449 | |
0c92c7a3 | 450 | if (!d_is_reg(path.dentry)) { |
d24d7dbf JZ |
451 | ret = -EINVAL; |
452 | goto fail_address_parse; | |
453 | } | |
f3f096cf | 454 | |
1cc33161 RB |
455 | /* Parse reference counter offset if specified. */ |
456 | rctr = strchr(arg, '('); | |
457 | if (rctr) { | |
458 | rctr_end = strchr(rctr, ')'); | |
459 | if (rctr > rctr_end || *(rctr_end + 1) != 0) { | |
460 | ret = -EINVAL; | |
461 | pr_info("Invalid reference counter offset.\n"); | |
462 | goto fail_address_parse; | |
463 | } | |
464 | ||
465 | *rctr++ = '\0'; | |
466 | *rctr_end = '\0'; | |
467 | ret = kstrtoul(rctr, 0, &ref_ctr_offset); | |
468 | if (ret) { | |
469 | pr_info("Invalid reference counter offset.\n"); | |
470 | goto fail_address_parse; | |
471 | } | |
472 | } | |
473 | ||
474 | /* Parse uprobe offset. */ | |
84d7ed79 ON |
475 | ret = kstrtoul(arg, 0, &offset); |
476 | if (ret) | |
477 | goto fail_address_parse; | |
478 | ||
f3f096cf SD |
479 | argc -= 2; |
480 | argv += 2; | |
481 | ||
482 | /* setup a probe */ | |
483 | if (!event) { | |
b2e902f0 | 484 | char *tail; |
f3f096cf SD |
485 | char *ptr; |
486 | ||
b2e902f0 AS |
487 | tail = kstrdup(kbasename(filename), GFP_KERNEL); |
488 | if (!tail) { | |
f3f096cf SD |
489 | ret = -ENOMEM; |
490 | goto fail_address_parse; | |
491 | } | |
492 | ||
f3f096cf SD |
493 | ptr = strpbrk(tail, ".-_"); |
494 | if (ptr) | |
495 | *ptr = '\0'; | |
496 | ||
497 | snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset); | |
498 | event = buf; | |
499 | kfree(tail); | |
500 | } | |
501 | ||
4ee5a52e | 502 | tu = alloc_trace_uprobe(group, event, argc, is_return); |
f3f096cf SD |
503 | if (IS_ERR(tu)) { |
504 | pr_info("Failed to allocate trace_uprobe.(%d)\n", (int)PTR_ERR(tu)); | |
505 | ret = PTR_ERR(tu); | |
506 | goto fail_address_parse; | |
507 | } | |
508 | tu->offset = offset; | |
1cc33161 | 509 | tu->ref_ctr_offset = ref_ctr_offset; |
0c92c7a3 | 510 | tu->path = path; |
f3f096cf SD |
511 | tu->filename = kstrdup(filename, GFP_KERNEL); |
512 | ||
513 | if (!tu->filename) { | |
514 | pr_info("Failed to allocate filename.\n"); | |
515 | ret = -ENOMEM; | |
516 | goto error; | |
517 | } | |
518 | ||
519 | /* parse arguments */ | |
f3f096cf | 520 | for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) { |
d00bbea9 | 521 | ret = traceprobe_parse_probe_arg(&tu->tp, i, argv[i], |
a1303af5 | 522 | is_return ? TPARG_FL_RETURN : 0); |
d00bbea9 | 523 | if (ret) |
f3f096cf | 524 | goto error; |
f3f096cf SD |
525 | } |
526 | ||
527 | ret = register_trace_uprobe(tu); | |
528 | if (ret) | |
529 | goto error; | |
530 | return 0; | |
531 | ||
532 | error: | |
533 | free_trace_uprobe(tu); | |
534 | return ret; | |
535 | ||
536 | fail_address_parse: | |
0c92c7a3 | 537 | path_put(&path); |
f3f096cf | 538 | |
d24d7dbf | 539 | pr_info("Failed to parse address or file.\n"); |
f3f096cf SD |
540 | |
541 | return ret; | |
542 | } | |
543 | ||
c6c2401d | 544 | static int cleanup_all_probes(void) |
f3f096cf SD |
545 | { |
546 | struct trace_uprobe *tu; | |
c6c2401d | 547 | int ret = 0; |
f3f096cf SD |
548 | |
549 | mutex_lock(&uprobe_lock); | |
547cd9ea MH |
550 | /* Ensure no probe is in use. */ |
551 | list_for_each_entry(tu, &uprobe_list, list) | |
552 | if (trace_probe_is_enabled(&tu->tp)) { | |
553 | ret = -EBUSY; | |
554 | goto end; | |
555 | } | |
f3f096cf SD |
556 | while (!list_empty(&uprobe_list)) { |
557 | tu = list_entry(uprobe_list.next, struct trace_uprobe, list); | |
c6c2401d SRRH |
558 | ret = unregister_trace_uprobe(tu); |
559 | if (ret) | |
560 | break; | |
f3f096cf | 561 | } |
547cd9ea | 562 | end: |
f3f096cf | 563 | mutex_unlock(&uprobe_lock); |
c6c2401d | 564 | return ret; |
f3f096cf SD |
565 | } |
566 | ||
567 | /* Probes listing interfaces */ | |
568 | static void *probes_seq_start(struct seq_file *m, loff_t *pos) | |
569 | { | |
570 | mutex_lock(&uprobe_lock); | |
571 | return seq_list_start(&uprobe_list, *pos); | |
572 | } | |
573 | ||
574 | static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos) | |
575 | { | |
576 | return seq_list_next(v, &uprobe_list, pos); | |
577 | } | |
578 | ||
579 | static void probes_seq_stop(struct seq_file *m, void *v) | |
580 | { | |
581 | mutex_unlock(&uprobe_lock); | |
582 | } | |
583 | ||
584 | static int probes_seq_show(struct seq_file *m, void *v) | |
585 | { | |
586 | struct trace_uprobe *tu = v; | |
3ede82dd | 587 | char c = is_ret_probe(tu) ? 'r' : 'p'; |
f3f096cf SD |
588 | int i; |
589 | ||
a64b2c01 RB |
590 | seq_printf(m, "%c:%s/%s %s:0x%0*lx", c, tu->tp.call.class->system, |
591 | trace_event_name(&tu->tp.call), tu->filename, | |
592 | (int)(sizeof(void *) * 2), tu->offset); | |
f3f096cf | 593 | |
1cc33161 RB |
594 | if (tu->ref_ctr_offset) |
595 | seq_printf(m, "(0x%lx)", tu->ref_ctr_offset); | |
596 | ||
14577c39 NK |
597 | for (i = 0; i < tu->tp.nr_args; i++) |
598 | seq_printf(m, " %s=%s", tu->tp.args[i].name, tu->tp.args[i].comm); | |
f3f096cf | 599 | |
fa6f0cc7 | 600 | seq_putc(m, '\n'); |
f3f096cf SD |
601 | return 0; |
602 | } | |
603 | ||
604 | static const struct seq_operations probes_seq_op = { | |
605 | .start = probes_seq_start, | |
606 | .next = probes_seq_next, | |
607 | .stop = probes_seq_stop, | |
608 | .show = probes_seq_show | |
609 | }; | |
610 | ||
611 | static int probes_open(struct inode *inode, struct file *file) | |
612 | { | |
c6c2401d SRRH |
613 | int ret; |
614 | ||
615 | if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) { | |
616 | ret = cleanup_all_probes(); | |
617 | if (ret) | |
618 | return ret; | |
619 | } | |
f3f096cf SD |
620 | |
621 | return seq_open(file, &probes_seq_op); | |
622 | } | |
623 | ||
624 | static ssize_t probes_write(struct file *file, const char __user *buffer, | |
625 | size_t count, loff_t *ppos) | |
626 | { | |
7e465baa | 627 | return trace_parse_run_command(file, buffer, count, ppos, create_trace_uprobe); |
f3f096cf SD |
628 | } |
629 | ||
630 | static const struct file_operations uprobe_events_ops = { | |
631 | .owner = THIS_MODULE, | |
632 | .open = probes_open, | |
633 | .read = seq_read, | |
634 | .llseek = seq_lseek, | |
635 | .release = seq_release, | |
636 | .write = probes_write, | |
637 | }; | |
638 | ||
639 | /* Probes profiling interfaces */ | |
640 | static int probes_profile_seq_show(struct seq_file *m, void *v) | |
641 | { | |
642 | struct trace_uprobe *tu = v; | |
643 | ||
de7b2973 | 644 | seq_printf(m, " %s %-44s %15lu\n", tu->filename, |
687fcc4a | 645 | trace_event_name(&tu->tp.call), tu->nhit); |
f3f096cf SD |
646 | return 0; |
647 | } | |
648 | ||
649 | static const struct seq_operations profile_seq_op = { | |
650 | .start = probes_seq_start, | |
651 | .next = probes_seq_next, | |
652 | .stop = probes_seq_stop, | |
653 | .show = probes_profile_seq_show | |
654 | }; | |
655 | ||
656 | static int profile_open(struct inode *inode, struct file *file) | |
657 | { | |
658 | return seq_open(file, &profile_seq_op); | |
659 | } | |
660 | ||
661 | static const struct file_operations uprobe_profile_ops = { | |
662 | .owner = THIS_MODULE, | |
663 | .open = profile_open, | |
664 | .read = seq_read, | |
665 | .llseek = seq_lseek, | |
666 | .release = seq_release, | |
667 | }; | |
668 | ||
dcad1a20 NK |
669 | struct uprobe_cpu_buffer { |
670 | struct mutex mutex; | |
671 | void *buf; | |
672 | }; | |
673 | static struct uprobe_cpu_buffer __percpu *uprobe_cpu_buffer; | |
674 | static int uprobe_buffer_refcnt; | |
675 | ||
676 | static int uprobe_buffer_init(void) | |
677 | { | |
678 | int cpu, err_cpu; | |
679 | ||
680 | uprobe_cpu_buffer = alloc_percpu(struct uprobe_cpu_buffer); | |
681 | if (uprobe_cpu_buffer == NULL) | |
682 | return -ENOMEM; | |
683 | ||
684 | for_each_possible_cpu(cpu) { | |
685 | struct page *p = alloc_pages_node(cpu_to_node(cpu), | |
686 | GFP_KERNEL, 0); | |
687 | if (p == NULL) { | |
688 | err_cpu = cpu; | |
689 | goto err; | |
690 | } | |
691 | per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf = page_address(p); | |
692 | mutex_init(&per_cpu_ptr(uprobe_cpu_buffer, cpu)->mutex); | |
693 | } | |
694 | ||
695 | return 0; | |
696 | ||
697 | err: | |
698 | for_each_possible_cpu(cpu) { | |
699 | if (cpu == err_cpu) | |
700 | break; | |
701 | free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf); | |
702 | } | |
703 | ||
704 | free_percpu(uprobe_cpu_buffer); | |
705 | return -ENOMEM; | |
706 | } | |
707 | ||
708 | static int uprobe_buffer_enable(void) | |
709 | { | |
710 | int ret = 0; | |
711 | ||
712 | BUG_ON(!mutex_is_locked(&event_mutex)); | |
713 | ||
714 | if (uprobe_buffer_refcnt++ == 0) { | |
715 | ret = uprobe_buffer_init(); | |
716 | if (ret < 0) | |
717 | uprobe_buffer_refcnt--; | |
718 | } | |
719 | ||
720 | return ret; | |
721 | } | |
722 | ||
723 | static void uprobe_buffer_disable(void) | |
724 | { | |
6ea6215f J |
725 | int cpu; |
726 | ||
dcad1a20 NK |
727 | BUG_ON(!mutex_is_locked(&event_mutex)); |
728 | ||
729 | if (--uprobe_buffer_refcnt == 0) { | |
6ea6215f J |
730 | for_each_possible_cpu(cpu) |
731 | free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer, | |
732 | cpu)->buf); | |
733 | ||
dcad1a20 NK |
734 | free_percpu(uprobe_cpu_buffer); |
735 | uprobe_cpu_buffer = NULL; | |
736 | } | |
737 | } | |
738 | ||
739 | static struct uprobe_cpu_buffer *uprobe_buffer_get(void) | |
740 | { | |
741 | struct uprobe_cpu_buffer *ucb; | |
742 | int cpu; | |
743 | ||
744 | cpu = raw_smp_processor_id(); | |
745 | ucb = per_cpu_ptr(uprobe_cpu_buffer, cpu); | |
746 | ||
747 | /* | |
748 | * Use per-cpu buffers for fastest access, but we might migrate | |
749 | * so the mutex makes sure we have sole access to it. | |
750 | */ | |
751 | mutex_lock(&ucb->mutex); | |
752 | ||
753 | return ucb; | |
754 | } | |
755 | ||
756 | static void uprobe_buffer_put(struct uprobe_cpu_buffer *ucb) | |
757 | { | |
758 | mutex_unlock(&ucb->mutex); | |
759 | } | |
760 | ||
a43b9704 | 761 | static void __uprobe_trace_func(struct trace_uprobe *tu, |
dd9fa555 | 762 | unsigned long func, struct pt_regs *regs, |
70ed91c6 | 763 | struct uprobe_cpu_buffer *ucb, int dsize, |
7f1d2f82 | 764 | struct trace_event_file *trace_file) |
f3f096cf SD |
765 | { |
766 | struct uprobe_trace_entry_head *entry; | |
767 | struct ring_buffer_event *event; | |
768 | struct ring_buffer *buffer; | |
457d1772 | 769 | void *data; |
dd9fa555 | 770 | int size, esize; |
2425bcb9 | 771 | struct trace_event_call *call = &tu->tp.call; |
f3f096cf | 772 | |
7f1d2f82 | 773 | WARN_ON(call != trace_file->event_call); |
70ed91c6 | 774 | |
dd9fa555 | 775 | if (WARN_ON_ONCE(tu->tp.size + dsize > PAGE_SIZE)) |
dcad1a20 NK |
776 | return; |
777 | ||
09a5059a | 778 | if (trace_trigger_soft_disabled(trace_file)) |
ca3b1620 NK |
779 | return; |
780 | ||
dd9fa555 | 781 | esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu)); |
dcad1a20 | 782 | size = esize + tu->tp.size + dsize; |
7f1d2f82 | 783 | event = trace_event_buffer_lock_reserve(&buffer, trace_file, |
70ed91c6 | 784 | call->event.type, size, 0, 0); |
f3f096cf | 785 | if (!event) |
dd9fa555 | 786 | return; |
f3f096cf SD |
787 | |
788 | entry = ring_buffer_event_data(event); | |
393a736c ON |
789 | if (is_ret_probe(tu)) { |
790 | entry->vaddr[0] = func; | |
791 | entry->vaddr[1] = instruction_pointer(regs); | |
792 | data = DATAOF_TRACE_ENTRY(entry, true); | |
793 | } else { | |
794 | entry->vaddr[0] = instruction_pointer(regs); | |
795 | data = DATAOF_TRACE_ENTRY(entry, false); | |
796 | } | |
797 | ||
dcad1a20 | 798 | memcpy(data, ucb->buf, tu->tp.size + dsize); |
f3f096cf | 799 | |
7f1d2f82 | 800 | event_trigger_unlock_commit(trace_file, buffer, event, entry, 0, 0); |
a51cc604 | 801 | } |
f42d24a1 | 802 | |
a51cc604 | 803 | /* uprobe handler */ |
dd9fa555 NK |
804 | static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs, |
805 | struct uprobe_cpu_buffer *ucb, int dsize) | |
a51cc604 | 806 | { |
70ed91c6 J |
807 | struct event_file_link *link; |
808 | ||
809 | if (is_ret_probe(tu)) | |
810 | return 0; | |
811 | ||
812 | rcu_read_lock(); | |
813 | list_for_each_entry_rcu(link, &tu->tp.files, list) | |
814 | __uprobe_trace_func(tu, 0, regs, ucb, dsize, link->file); | |
815 | rcu_read_unlock(); | |
816 | ||
f42d24a1 | 817 | return 0; |
f3f096cf SD |
818 | } |
819 | ||
c1ae5c75 | 820 | static void uretprobe_trace_func(struct trace_uprobe *tu, unsigned long func, |
dd9fa555 NK |
821 | struct pt_regs *regs, |
822 | struct uprobe_cpu_buffer *ucb, int dsize) | |
c1ae5c75 | 823 | { |
70ed91c6 J |
824 | struct event_file_link *link; |
825 | ||
826 | rcu_read_lock(); | |
827 | list_for_each_entry_rcu(link, &tu->tp.files, list) | |
828 | __uprobe_trace_func(tu, func, regs, ucb, dsize, link->file); | |
829 | rcu_read_unlock(); | |
c1ae5c75 ON |
830 | } |
831 | ||
f3f096cf SD |
832 | /* Event entry printers */ |
833 | static enum print_line_t | |
834 | print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event) | |
835 | { | |
457d1772 | 836 | struct uprobe_trace_entry_head *entry; |
f3f096cf SD |
837 | struct trace_seq *s = &iter->seq; |
838 | struct trace_uprobe *tu; | |
839 | u8 *data; | |
f3f096cf | 840 | |
457d1772 | 841 | entry = (struct uprobe_trace_entry_head *)iter->ent; |
14577c39 | 842 | tu = container_of(event, struct trace_uprobe, tp.call.event); |
f3f096cf | 843 | |
3ede82dd | 844 | if (is_ret_probe(tu)) { |
8579a107 | 845 | trace_seq_printf(s, "%s: (0x%lx <- 0x%lx)", |
687fcc4a | 846 | trace_event_name(&tu->tp.call), |
8579a107 | 847 | entry->vaddr[1], entry->vaddr[0]); |
3ede82dd ON |
848 | data = DATAOF_TRACE_ENTRY(entry, true); |
849 | } else { | |
8579a107 | 850 | trace_seq_printf(s, "%s: (0x%lx)", |
687fcc4a | 851 | trace_event_name(&tu->tp.call), |
8579a107 | 852 | entry->vaddr[0]); |
3ede82dd ON |
853 | data = DATAOF_TRACE_ENTRY(entry, false); |
854 | } | |
f3f096cf | 855 | |
56de7630 MH |
856 | if (print_probe_args(s, tu->tp.args, tu->tp.nr_args, data, entry) < 0) |
857 | goto out; | |
f3f096cf | 858 | |
8579a107 | 859 | trace_seq_putc(s, '\n'); |
f3f096cf | 860 | |
8579a107 SRRH |
861 | out: |
862 | return trace_handle_return(s); | |
f3f096cf SD |
863 | } |
864 | ||
31ba3348 ON |
865 | typedef bool (*filter_func_t)(struct uprobe_consumer *self, |
866 | enum uprobe_filter_ctx ctx, | |
867 | struct mm_struct *mm); | |
868 | ||
869 | static int | |
7f1d2f82 | 870 | probe_event_enable(struct trace_uprobe *tu, struct trace_event_file *file, |
70ed91c6 | 871 | filter_func_t filter) |
f3f096cf | 872 | { |
70ed91c6 J |
873 | bool enabled = trace_probe_is_enabled(&tu->tp); |
874 | struct event_file_link *link = NULL; | |
875 | int ret; | |
876 | ||
877 | if (file) { | |
48212542 ON |
878 | if (tu->tp.flags & TP_FLAG_PROFILE) |
879 | return -EINTR; | |
880 | ||
70ed91c6 J |
881 | link = kmalloc(sizeof(*link), GFP_KERNEL); |
882 | if (!link) | |
883 | return -ENOMEM; | |
884 | ||
885 | link->file = file; | |
886 | list_add_tail_rcu(&link->list, &tu->tp.files); | |
887 | ||
888 | tu->tp.flags |= TP_FLAG_TRACE; | |
48212542 ON |
889 | } else { |
890 | if (tu->tp.flags & TP_FLAG_TRACE) | |
891 | return -EINTR; | |
892 | ||
70ed91c6 | 893 | tu->tp.flags |= TP_FLAG_PROFILE; |
48212542 | 894 | } |
f3f096cf | 895 | |
736288ba ON |
896 | WARN_ON(!uprobe_filter_is_empty(&tu->filter)); |
897 | ||
70ed91c6 J |
898 | if (enabled) |
899 | return 0; | |
900 | ||
fb6bab6a ON |
901 | ret = uprobe_buffer_enable(); |
902 | if (ret) | |
903 | goto err_flags; | |
904 | ||
31ba3348 | 905 | tu->consumer.filter = filter; |
0c92c7a3 | 906 | tu->inode = d_real_inode(tu->path.dentry); |
1cc33161 RB |
907 | if (tu->ref_ctr_offset) { |
908 | ret = uprobe_register_refctr(tu->inode, tu->offset, | |
909 | tu->ref_ctr_offset, &tu->consumer); | |
910 | } else { | |
911 | ret = uprobe_register(tu->inode, tu->offset, &tu->consumer); | |
912 | } | |
913 | ||
fb6bab6a ON |
914 | if (ret) |
915 | goto err_buffer; | |
916 | ||
917 | return 0; | |
918 | ||
919 | err_buffer: | |
920 | uprobe_buffer_disable(); | |
f3f096cf | 921 | |
fb6bab6a ON |
922 | err_flags: |
923 | if (file) { | |
924 | list_del(&link->list); | |
925 | kfree(link); | |
926 | tu->tp.flags &= ~TP_FLAG_TRACE; | |
927 | } else { | |
928 | tu->tp.flags &= ~TP_FLAG_PROFILE; | |
929 | } | |
4161824f | 930 | return ret; |
f3f096cf SD |
931 | } |
932 | ||
70ed91c6 | 933 | static void |
7f1d2f82 | 934 | probe_event_disable(struct trace_uprobe *tu, struct trace_event_file *file) |
f3f096cf | 935 | { |
14577c39 | 936 | if (!trace_probe_is_enabled(&tu->tp)) |
f3f096cf SD |
937 | return; |
938 | ||
70ed91c6 J |
939 | if (file) { |
940 | struct event_file_link *link; | |
941 | ||
942 | link = find_event_file_link(&tu->tp, file); | |
943 | if (!link) | |
944 | return; | |
945 | ||
946 | list_del_rcu(&link->list); | |
947 | /* synchronize with u{,ret}probe_trace_func */ | |
016f8ffc | 948 | synchronize_rcu(); |
70ed91c6 J |
949 | kfree(link); |
950 | ||
951 | if (!list_empty(&tu->tp.files)) | |
952 | return; | |
953 | } | |
954 | ||
736288ba ON |
955 | WARN_ON(!uprobe_filter_is_empty(&tu->filter)); |
956 | ||
a932b738 | 957 | uprobe_unregister(tu->inode, tu->offset, &tu->consumer); |
0c92c7a3 | 958 | tu->inode = NULL; |
70ed91c6 | 959 | tu->tp.flags &= file ? ~TP_FLAG_TRACE : ~TP_FLAG_PROFILE; |
dcad1a20 NK |
960 | |
961 | uprobe_buffer_disable(); | |
f3f096cf SD |
962 | } |
963 | ||
2425bcb9 | 964 | static int uprobe_event_define_fields(struct trace_event_call *event_call) |
f3f096cf | 965 | { |
eeb07b06 | 966 | int ret, size; |
f3f096cf | 967 | struct uprobe_trace_entry_head field; |
457d1772 | 968 | struct trace_uprobe *tu = event_call->data; |
f3f096cf | 969 | |
4d1298e2 ON |
970 | if (is_ret_probe(tu)) { |
971 | DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_FUNC, 0); | |
972 | DEFINE_FIELD(unsigned long, vaddr[1], FIELD_STRING_RETIP, 0); | |
973 | size = SIZEOF_TRACE_ENTRY(true); | |
974 | } else { | |
975 | DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0); | |
976 | size = SIZEOF_TRACE_ENTRY(false); | |
977 | } | |
f3f096cf | 978 | |
eeb07b06 | 979 | return traceprobe_define_arg_fields(event_call, size, &tu->tp); |
f3f096cf SD |
980 | } |
981 | ||
f3f096cf | 982 | #ifdef CONFIG_PERF_EVENTS |
31ba3348 ON |
983 | static bool |
984 | __uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm) | |
985 | { | |
986 | struct perf_event *event; | |
987 | ||
988 | if (filter->nr_systemwide) | |
989 | return true; | |
990 | ||
991 | list_for_each_entry(event, &filter->perf_events, hw.tp_list) { | |
50f16a8b | 992 | if (event->hw.target->mm == mm) |
31ba3348 ON |
993 | return true; |
994 | } | |
995 | ||
996 | return false; | |
997 | } | |
998 | ||
b2fe8ba6 ON |
999 | static inline bool |
1000 | uprobe_filter_event(struct trace_uprobe *tu, struct perf_event *event) | |
1001 | { | |
50f16a8b | 1002 | return __uprobe_perf_filter(&tu->filter, event->hw.target->mm); |
b2fe8ba6 ON |
1003 | } |
1004 | ||
ce5f36a5 | 1005 | static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event) |
736288ba | 1006 | { |
b2fe8ba6 ON |
1007 | bool done; |
1008 | ||
736288ba | 1009 | write_lock(&tu->filter.rwlock); |
50f16a8b | 1010 | if (event->hw.target) { |
ce5f36a5 | 1011 | list_del(&event->hw.tp_list); |
b2fe8ba6 | 1012 | done = tu->filter.nr_systemwide || |
50f16a8b | 1013 | (event->hw.target->flags & PF_EXITING) || |
b2fe8ba6 | 1014 | uprobe_filter_event(tu, event); |
b2fe8ba6 | 1015 | } else { |
ce5f36a5 | 1016 | tu->filter.nr_systemwide--; |
b2fe8ba6 | 1017 | done = tu->filter.nr_systemwide; |
b2fe8ba6 | 1018 | } |
736288ba ON |
1019 | write_unlock(&tu->filter.rwlock); |
1020 | ||
b2fe8ba6 | 1021 | if (!done) |
927d6874 | 1022 | return uprobe_apply(tu->inode, tu->offset, &tu->consumer, false); |
31ba3348 | 1023 | |
736288ba ON |
1024 | return 0; |
1025 | } | |
1026 | ||
ce5f36a5 | 1027 | static int uprobe_perf_open(struct trace_uprobe *tu, struct perf_event *event) |
736288ba | 1028 | { |
b2fe8ba6 | 1029 | bool done; |
927d6874 | 1030 | int err; |
b2fe8ba6 | 1031 | |
736288ba | 1032 | write_lock(&tu->filter.rwlock); |
50f16a8b | 1033 | if (event->hw.target) { |
ce5f36a5 ON |
1034 | /* |
1035 | * event->parent != NULL means copy_process(), we can avoid | |
1036 | * uprobe_apply(). current->mm must be probed and we can rely | |
1037 | * on dup_mmap() which preserves the already installed bp's. | |
1038 | * | |
1039 | * attr.enable_on_exec means that exec/mmap will install the | |
1040 | * breakpoints we need. | |
1041 | */ | |
b2fe8ba6 | 1042 | done = tu->filter.nr_systemwide || |
ce5f36a5 | 1043 | event->parent || event->attr.enable_on_exec || |
b2fe8ba6 | 1044 | uprobe_filter_event(tu, event); |
ce5f36a5 | 1045 | list_add(&event->hw.tp_list, &tu->filter.perf_events); |
b2fe8ba6 | 1046 | } else { |
b2fe8ba6 | 1047 | done = tu->filter.nr_systemwide; |
ce5f36a5 | 1048 | tu->filter.nr_systemwide++; |
b2fe8ba6 | 1049 | } |
736288ba ON |
1050 | write_unlock(&tu->filter.rwlock); |
1051 | ||
927d6874 ON |
1052 | err = 0; |
1053 | if (!done) { | |
1054 | err = uprobe_apply(tu->inode, tu->offset, &tu->consumer, true); | |
1055 | if (err) | |
1056 | uprobe_perf_close(tu, event); | |
1057 | } | |
1058 | return err; | |
736288ba ON |
1059 | } |
1060 | ||
31ba3348 ON |
1061 | static bool uprobe_perf_filter(struct uprobe_consumer *uc, |
1062 | enum uprobe_filter_ctx ctx, struct mm_struct *mm) | |
1063 | { | |
1064 | struct trace_uprobe *tu; | |
1065 | int ret; | |
1066 | ||
1067 | tu = container_of(uc, struct trace_uprobe, consumer); | |
1068 | read_lock(&tu->filter.rwlock); | |
1069 | ret = __uprobe_perf_filter(&tu->filter, mm); | |
1070 | read_unlock(&tu->filter.rwlock); | |
1071 | ||
1072 | return ret; | |
1073 | } | |
1074 | ||
a43b9704 | 1075 | static void __uprobe_perf_func(struct trace_uprobe *tu, |
dd9fa555 NK |
1076 | unsigned long func, struct pt_regs *regs, |
1077 | struct uprobe_cpu_buffer *ucb, int dsize) | |
f3f096cf | 1078 | { |
2425bcb9 | 1079 | struct trace_event_call *call = &tu->tp.call; |
f3f096cf SD |
1080 | struct uprobe_trace_entry_head *entry; |
1081 | struct hlist_head *head; | |
457d1772 | 1082 | void *data; |
dd9fa555 | 1083 | int size, esize; |
dcad1a20 NK |
1084 | int rctx; |
1085 | ||
e87c6bc3 | 1086 | if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs)) |
04a22fae WN |
1087 | return; |
1088 | ||
dcad1a20 | 1089 | esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu)); |
f3f096cf | 1090 | |
dcad1a20 NK |
1091 | size = esize + tu->tp.size + dsize; |
1092 | size = ALIGN(size + sizeof(u32), sizeof(u64)) - sizeof(u32); | |
1093 | if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough")) | |
1094 | return; | |
1095 | ||
f3f096cf | 1096 | preempt_disable(); |
515619f2 ON |
1097 | head = this_cpu_ptr(call->perf_events); |
1098 | if (hlist_empty(head)) | |
1099 | goto out; | |
1100 | ||
1e1dcd93 | 1101 | entry = perf_trace_buf_alloc(size, NULL, &rctx); |
f3f096cf SD |
1102 | if (!entry) |
1103 | goto out; | |
1104 | ||
393a736c ON |
1105 | if (is_ret_probe(tu)) { |
1106 | entry->vaddr[0] = func; | |
32520b2c | 1107 | entry->vaddr[1] = instruction_pointer(regs); |
393a736c ON |
1108 | data = DATAOF_TRACE_ENTRY(entry, true); |
1109 | } else { | |
32520b2c | 1110 | entry->vaddr[0] = instruction_pointer(regs); |
393a736c ON |
1111 | data = DATAOF_TRACE_ENTRY(entry, false); |
1112 | } | |
1113 | ||
dcad1a20 NK |
1114 | memcpy(data, ucb->buf, tu->tp.size + dsize); |
1115 | ||
1116 | if (size - esize > tu->tp.size + dsize) { | |
1117 | int len = tu->tp.size + dsize; | |
14577c39 | 1118 | |
dcad1a20 | 1119 | memset(data + len, 0, size - esize - len); |
14577c39 | 1120 | } |
f3f096cf | 1121 | |
1e1dcd93 | 1122 | perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs, |
8fd0fbbe | 1123 | head, NULL); |
f3f096cf SD |
1124 | out: |
1125 | preempt_enable(); | |
a51cc604 ON |
1126 | } |
1127 | ||
1128 | /* uprobe profile handler */ | |
dd9fa555 NK |
1129 | static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs, |
1130 | struct uprobe_cpu_buffer *ucb, int dsize) | |
a51cc604 ON |
1131 | { |
1132 | if (!uprobe_perf_filter(&tu->consumer, 0, current->mm)) | |
1133 | return UPROBE_HANDLER_REMOVE; | |
1134 | ||
393a736c | 1135 | if (!is_ret_probe(tu)) |
dd9fa555 | 1136 | __uprobe_perf_func(tu, 0, regs, ucb, dsize); |
f42d24a1 | 1137 | return 0; |
f3f096cf | 1138 | } |
c1ae5c75 ON |
1139 | |
1140 | static void uretprobe_perf_func(struct trace_uprobe *tu, unsigned long func, | |
dd9fa555 NK |
1141 | struct pt_regs *regs, |
1142 | struct uprobe_cpu_buffer *ucb, int dsize) | |
c1ae5c75 | 1143 | { |
dd9fa555 | 1144 | __uprobe_perf_func(tu, func, regs, ucb, dsize); |
c1ae5c75 | 1145 | } |
41bdc4b4 YS |
1146 | |
1147 | int bpf_get_uprobe_info(const struct perf_event *event, u32 *fd_type, | |
1148 | const char **filename, u64 *probe_offset, | |
1149 | bool perf_type_tracepoint) | |
1150 | { | |
1151 | const char *pevent = trace_event_name(event->tp_event); | |
1152 | const char *group = event->tp_event->class->system; | |
1153 | struct trace_uprobe *tu; | |
1154 | ||
1155 | if (perf_type_tracepoint) | |
1156 | tu = find_probe_event(pevent, group); | |
1157 | else | |
1158 | tu = event->tp_event->data; | |
1159 | if (!tu) | |
1160 | return -EINVAL; | |
1161 | ||
1162 | *fd_type = is_ret_probe(tu) ? BPF_FD_TYPE_URETPROBE | |
1163 | : BPF_FD_TYPE_UPROBE; | |
1164 | *filename = tu->filename; | |
1165 | *probe_offset = tu->offset; | |
1166 | return 0; | |
1167 | } | |
f3f096cf SD |
1168 | #endif /* CONFIG_PERF_EVENTS */ |
1169 | ||
70ed91c6 | 1170 | static int |
2425bcb9 | 1171 | trace_uprobe_register(struct trace_event_call *event, enum trace_reg type, |
70ed91c6 | 1172 | void *data) |
f3f096cf | 1173 | { |
457d1772 | 1174 | struct trace_uprobe *tu = event->data; |
7f1d2f82 | 1175 | struct trace_event_file *file = data; |
f3f096cf SD |
1176 | |
1177 | switch (type) { | |
1178 | case TRACE_REG_REGISTER: | |
70ed91c6 | 1179 | return probe_event_enable(tu, file, NULL); |
f3f096cf SD |
1180 | |
1181 | case TRACE_REG_UNREGISTER: | |
70ed91c6 | 1182 | probe_event_disable(tu, file); |
f3f096cf SD |
1183 | return 0; |
1184 | ||
1185 | #ifdef CONFIG_PERF_EVENTS | |
1186 | case TRACE_REG_PERF_REGISTER: | |
70ed91c6 | 1187 | return probe_event_enable(tu, NULL, uprobe_perf_filter); |
f3f096cf SD |
1188 | |
1189 | case TRACE_REG_PERF_UNREGISTER: | |
70ed91c6 | 1190 | probe_event_disable(tu, NULL); |
f3f096cf | 1191 | return 0; |
736288ba ON |
1192 | |
1193 | case TRACE_REG_PERF_OPEN: | |
1194 | return uprobe_perf_open(tu, data); | |
1195 | ||
1196 | case TRACE_REG_PERF_CLOSE: | |
1197 | return uprobe_perf_close(tu, data); | |
1198 | ||
f3f096cf SD |
1199 | #endif |
1200 | default: | |
1201 | return 0; | |
1202 | } | |
1203 | return 0; | |
1204 | } | |
1205 | ||
1206 | static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs) | |
1207 | { | |
f3f096cf | 1208 | struct trace_uprobe *tu; |
b7e0bf34 | 1209 | struct uprobe_dispatch_data udd; |
dd9fa555 NK |
1210 | struct uprobe_cpu_buffer *ucb; |
1211 | int dsize, esize; | |
f42d24a1 | 1212 | int ret = 0; |
f3f096cf | 1213 | |
dd9fa555 | 1214 | |
a932b738 | 1215 | tu = container_of(con, struct trace_uprobe, consumer); |
1b47aefd | 1216 | tu->nhit++; |
f3f096cf | 1217 | |
b7e0bf34 NK |
1218 | udd.tu = tu; |
1219 | udd.bp_addr = instruction_pointer(regs); | |
1220 | ||
1221 | current->utask->vaddr = (unsigned long) &udd; | |
1222 | ||
dd9fa555 NK |
1223 | if (WARN_ON_ONCE(!uprobe_cpu_buffer)) |
1224 | return 0; | |
1225 | ||
1226 | dsize = __get_data_size(&tu->tp, regs); | |
1227 | esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu)); | |
1228 | ||
1229 | ucb = uprobe_buffer_get(); | |
9178412d | 1230 | store_trace_args(ucb->buf, &tu->tp, regs, esize, dsize); |
dd9fa555 | 1231 | |
14577c39 | 1232 | if (tu->tp.flags & TP_FLAG_TRACE) |
dd9fa555 | 1233 | ret |= uprobe_trace_func(tu, regs, ucb, dsize); |
f3f096cf SD |
1234 | |
1235 | #ifdef CONFIG_PERF_EVENTS | |
14577c39 | 1236 | if (tu->tp.flags & TP_FLAG_PROFILE) |
dd9fa555 | 1237 | ret |= uprobe_perf_func(tu, regs, ucb, dsize); |
f3f096cf | 1238 | #endif |
dd9fa555 | 1239 | uprobe_buffer_put(ucb); |
f42d24a1 | 1240 | return ret; |
f3f096cf SD |
1241 | } |
1242 | ||
c1ae5c75 ON |
1243 | static int uretprobe_dispatcher(struct uprobe_consumer *con, |
1244 | unsigned long func, struct pt_regs *regs) | |
1245 | { | |
1246 | struct trace_uprobe *tu; | |
b7e0bf34 | 1247 | struct uprobe_dispatch_data udd; |
dd9fa555 NK |
1248 | struct uprobe_cpu_buffer *ucb; |
1249 | int dsize, esize; | |
c1ae5c75 ON |
1250 | |
1251 | tu = container_of(con, struct trace_uprobe, consumer); | |
1252 | ||
b7e0bf34 NK |
1253 | udd.tu = tu; |
1254 | udd.bp_addr = func; | |
1255 | ||
1256 | current->utask->vaddr = (unsigned long) &udd; | |
1257 | ||
dd9fa555 NK |
1258 | if (WARN_ON_ONCE(!uprobe_cpu_buffer)) |
1259 | return 0; | |
1260 | ||
1261 | dsize = __get_data_size(&tu->tp, regs); | |
1262 | esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu)); | |
1263 | ||
1264 | ucb = uprobe_buffer_get(); | |
9178412d | 1265 | store_trace_args(ucb->buf, &tu->tp, regs, esize, dsize); |
dd9fa555 | 1266 | |
14577c39 | 1267 | if (tu->tp.flags & TP_FLAG_TRACE) |
dd9fa555 | 1268 | uretprobe_trace_func(tu, func, regs, ucb, dsize); |
c1ae5c75 ON |
1269 | |
1270 | #ifdef CONFIG_PERF_EVENTS | |
14577c39 | 1271 | if (tu->tp.flags & TP_FLAG_PROFILE) |
dd9fa555 | 1272 | uretprobe_perf_func(tu, func, regs, ucb, dsize); |
c1ae5c75 | 1273 | #endif |
dd9fa555 | 1274 | uprobe_buffer_put(ucb); |
c1ae5c75 ON |
1275 | return 0; |
1276 | } | |
1277 | ||
f3f096cf SD |
1278 | static struct trace_event_functions uprobe_funcs = { |
1279 | .trace = print_uprobe_event | |
1280 | }; | |
1281 | ||
33ea4b24 SL |
1282 | static inline void init_trace_event_call(struct trace_uprobe *tu, |
1283 | struct trace_event_call *call) | |
f3f096cf | 1284 | { |
f3f096cf SD |
1285 | INIT_LIST_HEAD(&call->class->fields); |
1286 | call->event.funcs = &uprobe_funcs; | |
1287 | call->class->define_fields = uprobe_event_define_fields; | |
1288 | ||
33ea4b24 SL |
1289 | call->flags = TRACE_EVENT_FL_UPROBE; |
1290 | call->class->reg = trace_uprobe_register; | |
1291 | call->data = tu; | |
1292 | } | |
1293 | ||
1294 | static int register_uprobe_event(struct trace_uprobe *tu) | |
1295 | { | |
1296 | struct trace_event_call *call = &tu->tp.call; | |
1297 | int ret = 0; | |
1298 | ||
1299 | init_trace_event_call(tu, call); | |
1300 | ||
0a46c854 | 1301 | if (traceprobe_set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0) |
f3f096cf SD |
1302 | return -ENOMEM; |
1303 | ||
9023c930 | 1304 | ret = register_trace_event(&call->event); |
f3f096cf SD |
1305 | if (!ret) { |
1306 | kfree(call->print_fmt); | |
1307 | return -ENODEV; | |
1308 | } | |
ede392a7 | 1309 | |
f3f096cf SD |
1310 | ret = trace_add_event_call(call); |
1311 | ||
1312 | if (ret) { | |
de7b2973 | 1313 | pr_info("Failed to register uprobe event: %s\n", |
687fcc4a | 1314 | trace_event_name(call)); |
f3f096cf | 1315 | kfree(call->print_fmt); |
9023c930 | 1316 | unregister_trace_event(&call->event); |
f3f096cf SD |
1317 | } |
1318 | ||
1319 | return ret; | |
1320 | } | |
1321 | ||
c6c2401d | 1322 | static int unregister_uprobe_event(struct trace_uprobe *tu) |
f3f096cf | 1323 | { |
c6c2401d SRRH |
1324 | int ret; |
1325 | ||
f3f096cf | 1326 | /* tu->event is unregistered in trace_remove_event_call() */ |
14577c39 | 1327 | ret = trace_remove_event_call(&tu->tp.call); |
c6c2401d SRRH |
1328 | if (ret) |
1329 | return ret; | |
14577c39 NK |
1330 | kfree(tu->tp.call.print_fmt); |
1331 | tu->tp.call.print_fmt = NULL; | |
c6c2401d | 1332 | return 0; |
f3f096cf SD |
1333 | } |
1334 | ||
33ea4b24 SL |
1335 | #ifdef CONFIG_PERF_EVENTS |
1336 | struct trace_event_call * | |
a6ca88b2 SL |
1337 | create_local_trace_uprobe(char *name, unsigned long offs, |
1338 | unsigned long ref_ctr_offset, bool is_return) | |
33ea4b24 SL |
1339 | { |
1340 | struct trace_uprobe *tu; | |
33ea4b24 SL |
1341 | struct path path; |
1342 | int ret; | |
1343 | ||
1344 | ret = kern_path(name, LOOKUP_FOLLOW, &path); | |
1345 | if (ret) | |
1346 | return ERR_PTR(ret); | |
1347 | ||
0c92c7a3 SL |
1348 | if (!d_is_reg(path.dentry)) { |
1349 | path_put(&path); | |
33ea4b24 SL |
1350 | return ERR_PTR(-EINVAL); |
1351 | } | |
1352 | ||
1353 | /* | |
1354 | * local trace_kprobes are not added to probe_list, so they are never | |
1355 | * searched in find_trace_kprobe(). Therefore, there is no concern of | |
1356 | * duplicated name "DUMMY_EVENT" here. | |
1357 | */ | |
1358 | tu = alloc_trace_uprobe(UPROBE_EVENT_SYSTEM, "DUMMY_EVENT", 0, | |
1359 | is_return); | |
1360 | ||
1361 | if (IS_ERR(tu)) { | |
1362 | pr_info("Failed to allocate trace_uprobe.(%d)\n", | |
1363 | (int)PTR_ERR(tu)); | |
0c92c7a3 | 1364 | path_put(&path); |
33ea4b24 SL |
1365 | return ERR_CAST(tu); |
1366 | } | |
1367 | ||
1368 | tu->offset = offs; | |
0c92c7a3 | 1369 | tu->path = path; |
a6ca88b2 | 1370 | tu->ref_ctr_offset = ref_ctr_offset; |
33ea4b24 SL |
1371 | tu->filename = kstrdup(name, GFP_KERNEL); |
1372 | init_trace_event_call(tu, &tu->tp.call); | |
1373 | ||
0a46c854 | 1374 | if (traceprobe_set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0) { |
33ea4b24 SL |
1375 | ret = -ENOMEM; |
1376 | goto error; | |
1377 | } | |
1378 | ||
1379 | return &tu->tp.call; | |
1380 | error: | |
1381 | free_trace_uprobe(tu); | |
1382 | return ERR_PTR(ret); | |
1383 | } | |
1384 | ||
1385 | void destroy_local_trace_uprobe(struct trace_event_call *event_call) | |
1386 | { | |
1387 | struct trace_uprobe *tu; | |
1388 | ||
1389 | tu = container_of(event_call, struct trace_uprobe, tp.call); | |
1390 | ||
1391 | kfree(tu->tp.call.print_fmt); | |
1392 | tu->tp.call.print_fmt = NULL; | |
1393 | ||
1394 | free_trace_uprobe(tu); | |
1395 | } | |
1396 | #endif /* CONFIG_PERF_EVENTS */ | |
1397 | ||
f3f096cf SD |
1398 | /* Make a trace interface for controling probe points */ |
1399 | static __init int init_uprobe_trace(void) | |
1400 | { | |
1401 | struct dentry *d_tracer; | |
1402 | ||
1403 | d_tracer = tracing_init_dentry(); | |
14a5ae40 | 1404 | if (IS_ERR(d_tracer)) |
f3f096cf SD |
1405 | return 0; |
1406 | ||
1407 | trace_create_file("uprobe_events", 0644, d_tracer, | |
1408 | NULL, &uprobe_events_ops); | |
1409 | /* Profile interface */ | |
1410 | trace_create_file("uprobe_profile", 0444, d_tracer, | |
1411 | NULL, &uprobe_profile_ops); | |
1412 | return 0; | |
1413 | } | |
1414 | ||
1415 | fs_initcall(init_uprobe_trace); |