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