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