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