]> asedeno.scripts.mit.edu Git - linux.git/blob - kernel/trace/trace_uprobe.c
tracing/uprobe: Set print format when parsing command
[linux.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                                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_FOFFS:
252                 val = translate_user_vaddr(code->immediate);
253                 break;
254         default:
255                 return -EILSEQ;
256         }
257         code++;
258
259         return process_fetch_insn_bottom(code, val, dest, base);
260 }
261 NOKPROBE_SYMBOL(process_fetch_insn)
262
263 static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter)
264 {
265         rwlock_init(&filter->rwlock);
266         filter->nr_systemwide = 0;
267         INIT_LIST_HEAD(&filter->perf_events);
268 }
269
270 static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter *filter)
271 {
272         return !filter->nr_systemwide && list_empty(&filter->perf_events);
273 }
274
275 static inline bool is_ret_probe(struct trace_uprobe *tu)
276 {
277         return tu->consumer.ret_handler != NULL;
278 }
279
280 static bool trace_uprobe_is_busy(struct dyn_event *ev)
281 {
282         struct trace_uprobe *tu = to_trace_uprobe(ev);
283
284         return trace_probe_is_enabled(&tu->tp);
285 }
286
287 static bool trace_uprobe_match(const char *system, const char *event,
288                                struct dyn_event *ev)
289 {
290         struct trace_uprobe *tu = to_trace_uprobe(ev);
291
292         return strcmp(trace_event_name(&tu->tp.call), event) == 0 &&
293                 (!system || strcmp(tu->tp.call.class->system, system) == 0);
294 }
295
296 /*
297  * Allocate new trace_uprobe and initialize it (including uprobes).
298  */
299 static struct trace_uprobe *
300 alloc_trace_uprobe(const char *group, const char *event, int nargs, bool is_ret)
301 {
302         struct trace_uprobe *tu;
303
304         if (!event || !group)
305                 return ERR_PTR(-EINVAL);
306
307         tu = kzalloc(SIZEOF_TRACE_UPROBE(nargs), GFP_KERNEL);
308         if (!tu)
309                 return ERR_PTR(-ENOMEM);
310
311         tu->tp.call.class = &tu->tp.class;
312         tu->tp.call.name = kstrdup(event, GFP_KERNEL);
313         if (!tu->tp.call.name)
314                 goto error;
315
316         tu->tp.class.system = kstrdup(group, GFP_KERNEL);
317         if (!tu->tp.class.system)
318                 goto error;
319
320         dyn_event_init(&tu->devent, &trace_uprobe_ops);
321         INIT_LIST_HEAD(&tu->tp.files);
322         tu->consumer.handler = uprobe_dispatcher;
323         if (is_ret)
324                 tu->consumer.ret_handler = uretprobe_dispatcher;
325         init_trace_uprobe_filter(&tu->filter);
326         return tu;
327
328 error:
329         kfree(tu->tp.call.name);
330         kfree(tu);
331
332         return ERR_PTR(-ENOMEM);
333 }
334
335 static void free_trace_uprobe(struct trace_uprobe *tu)
336 {
337         int i;
338
339         if (!tu)
340                 return;
341
342         for (i = 0; i < tu->tp.nr_args; i++)
343                 traceprobe_free_probe_arg(&tu->tp.args[i]);
344
345         path_put(&tu->path);
346         kfree(tu->tp.call.class->system);
347         kfree(tu->tp.call.name);
348         kfree(tu->tp.call.print_fmt);
349         kfree(tu->filename);
350         kfree(tu);
351 }
352
353 static struct trace_uprobe *find_probe_event(const char *event, const char *group)
354 {
355         struct dyn_event *pos;
356         struct trace_uprobe *tu;
357
358         for_each_trace_uprobe(tu, pos)
359                 if (strcmp(trace_event_name(&tu->tp.call), event) == 0 &&
360                     strcmp(tu->tp.call.class->system, group) == 0)
361                         return tu;
362
363         return NULL;
364 }
365
366 /* Unregister a trace_uprobe and probe_event */
367 static int unregister_trace_uprobe(struct trace_uprobe *tu)
368 {
369         int ret;
370
371         ret = unregister_uprobe_event(tu);
372         if (ret)
373                 return ret;
374
375         dyn_event_remove(&tu->devent);
376         free_trace_uprobe(tu);
377         return 0;
378 }
379
380 /*
381  * Uprobe with multiple reference counter is not allowed. i.e.
382  * If inode and offset matches, reference counter offset *must*
383  * match as well. Though, there is one exception: If user is
384  * replacing old trace_uprobe with new one(same group/event),
385  * then we allow same uprobe with new reference counter as far
386  * as the new one does not conflict with any other existing
387  * ones.
388  */
389 static struct trace_uprobe *find_old_trace_uprobe(struct trace_uprobe *new)
390 {
391         struct dyn_event *pos;
392         struct trace_uprobe *tmp, *old = NULL;
393         struct inode *new_inode = d_real_inode(new->path.dentry);
394
395         old = find_probe_event(trace_event_name(&new->tp.call),
396                                 new->tp.call.class->system);
397
398         for_each_trace_uprobe(tmp, pos) {
399                 if ((old ? old != tmp : true) &&
400                     new_inode == d_real_inode(tmp->path.dentry) &&
401                     new->offset == tmp->offset &&
402                     new->ref_ctr_offset != tmp->ref_ctr_offset) {
403                         pr_warn("Reference counter offset mismatch.");
404                         return ERR_PTR(-EINVAL);
405                 }
406         }
407         return old;
408 }
409
410 /* Register a trace_uprobe and probe_event */
411 static int register_trace_uprobe(struct trace_uprobe *tu)
412 {
413         struct trace_uprobe *old_tu;
414         int ret;
415
416         mutex_lock(&event_mutex);
417
418         /* register as an event */
419         old_tu = find_old_trace_uprobe(tu);
420         if (IS_ERR(old_tu)) {
421                 ret = PTR_ERR(old_tu);
422                 goto end;
423         }
424
425         if (old_tu) {
426                 /* delete old event */
427                 ret = unregister_trace_uprobe(old_tu);
428                 if (ret)
429                         goto end;
430         }
431
432         ret = register_uprobe_event(tu);
433         if (ret) {
434                 pr_warn("Failed to register probe event(%d)\n", ret);
435                 goto end;
436         }
437
438         dyn_event_add(&tu->devent);
439
440 end:
441         mutex_unlock(&event_mutex);
442
443         return ret;
444 }
445
446 /*
447  * Argument syntax:
448  *  - Add uprobe: p|r[:[GRP/]EVENT] PATH:OFFSET [FETCHARGS]
449  *
450  *  - Remove uprobe: -:[GRP/]EVENT
451  */
452 static int trace_uprobe_create(int argc, const char **argv)
453 {
454         struct trace_uprobe *tu;
455         const char *event = NULL, *group = UPROBE_EVENT_SYSTEM;
456         char *arg, *filename, *rctr, *rctr_end, *tmp;
457         char buf[MAX_EVENT_NAME_LEN];
458         struct path path;
459         unsigned long offset, ref_ctr_offset;
460         bool is_return = false;
461         int i, ret;
462
463         ret = 0;
464         ref_ctr_offset = 0;
465
466         /* argc must be >= 1 */
467         if (argv[0][0] == 'r')
468                 is_return = true;
469         else if (argv[0][0] != 'p' || argc < 2)
470                 return -ECANCELED;
471
472         if (argv[0][1] == ':')
473                 event = &argv[0][2];
474
475         if (!strchr(argv[1], '/'))
476                 return -ECANCELED;
477
478         filename = kstrdup(argv[1], GFP_KERNEL);
479         if (!filename)
480                 return -ENOMEM;
481
482         /* Find the last occurrence, in case the path contains ':' too. */
483         arg = strrchr(filename, ':');
484         if (!arg || !isdigit(arg[1])) {
485                 kfree(filename);
486                 return -ECANCELED;
487         }
488
489         trace_probe_log_init("trace_uprobe", argc, argv);
490         trace_probe_log_set_index(1);   /* filename is the 2nd argument */
491
492         *arg++ = '\0';
493         ret = kern_path(filename, LOOKUP_FOLLOW, &path);
494         if (ret) {
495                 trace_probe_log_err(0, FILE_NOT_FOUND);
496                 kfree(filename);
497                 trace_probe_log_clear();
498                 return ret;
499         }
500         if (!d_is_reg(path.dentry)) {
501                 trace_probe_log_err(0, NO_REGULAR_FILE);
502                 ret = -EINVAL;
503                 goto fail_address_parse;
504         }
505
506         /* Parse reference counter offset if specified. */
507         rctr = strchr(arg, '(');
508         if (rctr) {
509                 rctr_end = strchr(rctr, ')');
510                 if (!rctr_end) {
511                         ret = -EINVAL;
512                         rctr_end = rctr + strlen(rctr);
513                         trace_probe_log_err(rctr_end - filename,
514                                             REFCNT_OPEN_BRACE);
515                         goto fail_address_parse;
516                 } else if (rctr_end[1] != '\0') {
517                         ret = -EINVAL;
518                         trace_probe_log_err(rctr_end + 1 - filename,
519                                             BAD_REFCNT_SUFFIX);
520                         goto fail_address_parse;
521                 }
522
523                 *rctr++ = '\0';
524                 *rctr_end = '\0';
525                 ret = kstrtoul(rctr, 0, &ref_ctr_offset);
526                 if (ret) {
527                         trace_probe_log_err(rctr - filename, BAD_REFCNT);
528                         goto fail_address_parse;
529                 }
530         }
531
532         /* Parse uprobe offset. */
533         ret = kstrtoul(arg, 0, &offset);
534         if (ret) {
535                 trace_probe_log_err(arg - filename, BAD_UPROBE_OFFS);
536                 goto fail_address_parse;
537         }
538
539         /* setup a probe */
540         trace_probe_log_set_index(0);
541         if (event) {
542                 ret = traceprobe_parse_event_name(&event, &group, buf,
543                                                   event - argv[0]);
544                 if (ret)
545                         goto fail_address_parse;
546         } else {
547                 char *tail;
548                 char *ptr;
549
550                 tail = kstrdup(kbasename(filename), GFP_KERNEL);
551                 if (!tail) {
552                         ret = -ENOMEM;
553                         goto fail_address_parse;
554                 }
555
556                 ptr = strpbrk(tail, ".-_");
557                 if (ptr)
558                         *ptr = '\0';
559
560                 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset);
561                 event = buf;
562                 kfree(tail);
563         }
564
565         argc -= 2;
566         argv += 2;
567
568         tu = alloc_trace_uprobe(group, event, argc, is_return);
569         if (IS_ERR(tu)) {
570                 ret = PTR_ERR(tu);
571                 /* This must return -ENOMEM otherwise there is a bug */
572                 WARN_ON_ONCE(ret != -ENOMEM);
573                 goto fail_address_parse;
574         }
575         tu->offset = offset;
576         tu->ref_ctr_offset = ref_ctr_offset;
577         tu->path = path;
578         tu->filename = filename;
579
580         /* parse arguments */
581         for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
582                 tmp = kstrdup(argv[i], GFP_KERNEL);
583                 if (!tmp) {
584                         ret = -ENOMEM;
585                         goto error;
586                 }
587
588                 trace_probe_log_set_index(i + 2);
589                 ret = traceprobe_parse_probe_arg(&tu->tp, i, tmp,
590                                         is_return ? TPARG_FL_RETURN : 0);
591                 kfree(tmp);
592                 if (ret)
593                         goto error;
594         }
595
596         ret = traceprobe_set_print_fmt(&tu->tp, is_ret_probe(tu));
597         if (ret < 0)
598                 goto error;
599
600         ret = register_trace_uprobe(tu);
601         if (!ret)
602                 goto out;
603
604 error:
605         free_trace_uprobe(tu);
606 out:
607         trace_probe_log_clear();
608         return ret;
609
610 fail_address_parse:
611         trace_probe_log_clear();
612         path_put(&path);
613         kfree(filename);
614
615         return ret;
616 }
617
618 static int create_or_delete_trace_uprobe(int argc, char **argv)
619 {
620         int ret;
621
622         if (argv[0][0] == '-')
623                 return dyn_event_release(argc, argv, &trace_uprobe_ops);
624
625         ret = trace_uprobe_create(argc, (const char **)argv);
626         return ret == -ECANCELED ? -EINVAL : ret;
627 }
628
629 static int trace_uprobe_release(struct dyn_event *ev)
630 {
631         struct trace_uprobe *tu = to_trace_uprobe(ev);
632
633         return unregister_trace_uprobe(tu);
634 }
635
636 /* Probes listing interfaces */
637 static int trace_uprobe_show(struct seq_file *m, struct dyn_event *ev)
638 {
639         struct trace_uprobe *tu = to_trace_uprobe(ev);
640         char c = is_ret_probe(tu) ? 'r' : 'p';
641         int i;
642
643         seq_printf(m, "%c:%s/%s %s:0x%0*lx", c, tu->tp.call.class->system,
644                         trace_event_name(&tu->tp.call), tu->filename,
645                         (int)(sizeof(void *) * 2), tu->offset);
646
647         if (tu->ref_ctr_offset)
648                 seq_printf(m, "(0x%lx)", tu->ref_ctr_offset);
649
650         for (i = 0; i < tu->tp.nr_args; i++)
651                 seq_printf(m, " %s=%s", tu->tp.args[i].name, tu->tp.args[i].comm);
652
653         seq_putc(m, '\n');
654         return 0;
655 }
656
657 static int probes_seq_show(struct seq_file *m, void *v)
658 {
659         struct dyn_event *ev = v;
660
661         if (!is_trace_uprobe(ev))
662                 return 0;
663
664         return trace_uprobe_show(m, ev);
665 }
666
667 static const struct seq_operations probes_seq_op = {
668         .start  = dyn_event_seq_start,
669         .next   = dyn_event_seq_next,
670         .stop   = dyn_event_seq_stop,
671         .show   = probes_seq_show
672 };
673
674 static int probes_open(struct inode *inode, struct file *file)
675 {
676         int ret;
677
678         if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
679                 ret = dyn_events_release_all(&trace_uprobe_ops);
680                 if (ret)
681                         return ret;
682         }
683
684         return seq_open(file, &probes_seq_op);
685 }
686
687 static ssize_t probes_write(struct file *file, const char __user *buffer,
688                             size_t count, loff_t *ppos)
689 {
690         return trace_parse_run_command(file, buffer, count, ppos,
691                                         create_or_delete_trace_uprobe);
692 }
693
694 static const struct file_operations uprobe_events_ops = {
695         .owner          = THIS_MODULE,
696         .open           = probes_open,
697         .read           = seq_read,
698         .llseek         = seq_lseek,
699         .release        = seq_release,
700         .write          = probes_write,
701 };
702
703 /* Probes profiling interfaces */
704 static int probes_profile_seq_show(struct seq_file *m, void *v)
705 {
706         struct dyn_event *ev = v;
707         struct trace_uprobe *tu;
708
709         if (!is_trace_uprobe(ev))
710                 return 0;
711
712         tu = to_trace_uprobe(ev);
713         seq_printf(m, "  %s %-44s %15lu\n", tu->filename,
714                         trace_event_name(&tu->tp.call), tu->nhit);
715         return 0;
716 }
717
718 static const struct seq_operations profile_seq_op = {
719         .start  = dyn_event_seq_start,
720         .next   = dyn_event_seq_next,
721         .stop   = dyn_event_seq_stop,
722         .show   = probes_profile_seq_show
723 };
724
725 static int profile_open(struct inode *inode, struct file *file)
726 {
727         return seq_open(file, &profile_seq_op);
728 }
729
730 static const struct file_operations uprobe_profile_ops = {
731         .owner          = THIS_MODULE,
732         .open           = profile_open,
733         .read           = seq_read,
734         .llseek         = seq_lseek,
735         .release        = seq_release,
736 };
737
738 struct uprobe_cpu_buffer {
739         struct mutex mutex;
740         void *buf;
741 };
742 static struct uprobe_cpu_buffer __percpu *uprobe_cpu_buffer;
743 static int uprobe_buffer_refcnt;
744
745 static int uprobe_buffer_init(void)
746 {
747         int cpu, err_cpu;
748
749         uprobe_cpu_buffer = alloc_percpu(struct uprobe_cpu_buffer);
750         if (uprobe_cpu_buffer == NULL)
751                 return -ENOMEM;
752
753         for_each_possible_cpu(cpu) {
754                 struct page *p = alloc_pages_node(cpu_to_node(cpu),
755                                                   GFP_KERNEL, 0);
756                 if (p == NULL) {
757                         err_cpu = cpu;
758                         goto err;
759                 }
760                 per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf = page_address(p);
761                 mutex_init(&per_cpu_ptr(uprobe_cpu_buffer, cpu)->mutex);
762         }
763
764         return 0;
765
766 err:
767         for_each_possible_cpu(cpu) {
768                 if (cpu == err_cpu)
769                         break;
770                 free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf);
771         }
772
773         free_percpu(uprobe_cpu_buffer);
774         return -ENOMEM;
775 }
776
777 static int uprobe_buffer_enable(void)
778 {
779         int ret = 0;
780
781         BUG_ON(!mutex_is_locked(&event_mutex));
782
783         if (uprobe_buffer_refcnt++ == 0) {
784                 ret = uprobe_buffer_init();
785                 if (ret < 0)
786                         uprobe_buffer_refcnt--;
787         }
788
789         return ret;
790 }
791
792 static void uprobe_buffer_disable(void)
793 {
794         int cpu;
795
796         BUG_ON(!mutex_is_locked(&event_mutex));
797
798         if (--uprobe_buffer_refcnt == 0) {
799                 for_each_possible_cpu(cpu)
800                         free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer,
801                                                              cpu)->buf);
802
803                 free_percpu(uprobe_cpu_buffer);
804                 uprobe_cpu_buffer = NULL;
805         }
806 }
807
808 static struct uprobe_cpu_buffer *uprobe_buffer_get(void)
809 {
810         struct uprobe_cpu_buffer *ucb;
811         int cpu;
812
813         cpu = raw_smp_processor_id();
814         ucb = per_cpu_ptr(uprobe_cpu_buffer, cpu);
815
816         /*
817          * Use per-cpu buffers for fastest access, but we might migrate
818          * so the mutex makes sure we have sole access to it.
819          */
820         mutex_lock(&ucb->mutex);
821
822         return ucb;
823 }
824
825 static void uprobe_buffer_put(struct uprobe_cpu_buffer *ucb)
826 {
827         mutex_unlock(&ucb->mutex);
828 }
829
830 static void __uprobe_trace_func(struct trace_uprobe *tu,
831                                 unsigned long func, struct pt_regs *regs,
832                                 struct uprobe_cpu_buffer *ucb, int dsize,
833                                 struct trace_event_file *trace_file)
834 {
835         struct uprobe_trace_entry_head *entry;
836         struct ring_buffer_event *event;
837         struct ring_buffer *buffer;
838         void *data;
839         int size, esize;
840         struct trace_event_call *call = &tu->tp.call;
841
842         WARN_ON(call != trace_file->event_call);
843
844         if (WARN_ON_ONCE(tu->tp.size + dsize > PAGE_SIZE))
845                 return;
846
847         if (trace_trigger_soft_disabled(trace_file))
848                 return;
849
850         esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
851         size = esize + tu->tp.size + dsize;
852         event = trace_event_buffer_lock_reserve(&buffer, trace_file,
853                                                 call->event.type, size, 0, 0);
854         if (!event)
855                 return;
856
857         entry = ring_buffer_event_data(event);
858         if (is_ret_probe(tu)) {
859                 entry->vaddr[0] = func;
860                 entry->vaddr[1] = instruction_pointer(regs);
861                 data = DATAOF_TRACE_ENTRY(entry, true);
862         } else {
863                 entry->vaddr[0] = instruction_pointer(regs);
864                 data = DATAOF_TRACE_ENTRY(entry, false);
865         }
866
867         memcpy(data, ucb->buf, tu->tp.size + dsize);
868
869         event_trigger_unlock_commit(trace_file, buffer, event, entry, 0, 0);
870 }
871
872 /* uprobe handler */
873 static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs,
874                              struct uprobe_cpu_buffer *ucb, int dsize)
875 {
876         struct event_file_link *link;
877
878         if (is_ret_probe(tu))
879                 return 0;
880
881         rcu_read_lock();
882         list_for_each_entry_rcu(link, &tu->tp.files, list)
883                 __uprobe_trace_func(tu, 0, regs, ucb, dsize, link->file);
884         rcu_read_unlock();
885
886         return 0;
887 }
888
889 static void uretprobe_trace_func(struct trace_uprobe *tu, unsigned long func,
890                                  struct pt_regs *regs,
891                                  struct uprobe_cpu_buffer *ucb, int dsize)
892 {
893         struct event_file_link *link;
894
895         rcu_read_lock();
896         list_for_each_entry_rcu(link, &tu->tp.files, list)
897                 __uprobe_trace_func(tu, func, regs, ucb, dsize, link->file);
898         rcu_read_unlock();
899 }
900
901 /* Event entry printers */
902 static enum print_line_t
903 print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event)
904 {
905         struct uprobe_trace_entry_head *entry;
906         struct trace_seq *s = &iter->seq;
907         struct trace_uprobe *tu;
908         u8 *data;
909
910         entry = (struct uprobe_trace_entry_head *)iter->ent;
911         tu = container_of(event, struct trace_uprobe, tp.call.event);
912
913         if (is_ret_probe(tu)) {
914                 trace_seq_printf(s, "%s: (0x%lx <- 0x%lx)",
915                                  trace_event_name(&tu->tp.call),
916                                  entry->vaddr[1], entry->vaddr[0]);
917                 data = DATAOF_TRACE_ENTRY(entry, true);
918         } else {
919                 trace_seq_printf(s, "%s: (0x%lx)",
920                                  trace_event_name(&tu->tp.call),
921                                  entry->vaddr[0]);
922                 data = DATAOF_TRACE_ENTRY(entry, false);
923         }
924
925         if (print_probe_args(s, tu->tp.args, tu->tp.nr_args, data, entry) < 0)
926                 goto out;
927
928         trace_seq_putc(s, '\n');
929
930  out:
931         return trace_handle_return(s);
932 }
933
934 typedef bool (*filter_func_t)(struct uprobe_consumer *self,
935                                 enum uprobe_filter_ctx ctx,
936                                 struct mm_struct *mm);
937
938 static int
939 probe_event_enable(struct trace_uprobe *tu, struct trace_event_file *file,
940                    filter_func_t filter)
941 {
942         bool enabled = trace_probe_is_enabled(&tu->tp);
943         struct event_file_link *link = NULL;
944         int ret;
945
946         if (file) {
947                 if (tu->tp.flags & TP_FLAG_PROFILE)
948                         return -EINTR;
949
950                 link = kmalloc(sizeof(*link), GFP_KERNEL);
951                 if (!link)
952                         return -ENOMEM;
953
954                 link->file = file;
955                 list_add_tail_rcu(&link->list, &tu->tp.files);
956
957                 tu->tp.flags |= TP_FLAG_TRACE;
958         } else {
959                 if (tu->tp.flags & TP_FLAG_TRACE)
960                         return -EINTR;
961
962                 tu->tp.flags |= TP_FLAG_PROFILE;
963         }
964
965         WARN_ON(!uprobe_filter_is_empty(&tu->filter));
966
967         if (enabled)
968                 return 0;
969
970         ret = uprobe_buffer_enable();
971         if (ret)
972                 goto err_flags;
973
974         tu->consumer.filter = filter;
975         tu->inode = d_real_inode(tu->path.dentry);
976         if (tu->ref_ctr_offset) {
977                 ret = uprobe_register_refctr(tu->inode, tu->offset,
978                                 tu->ref_ctr_offset, &tu->consumer);
979         } else {
980                 ret = uprobe_register(tu->inode, tu->offset, &tu->consumer);
981         }
982
983         if (ret)
984                 goto err_buffer;
985
986         return 0;
987
988  err_buffer:
989         uprobe_buffer_disable();
990
991  err_flags:
992         if (file) {
993                 list_del(&link->list);
994                 kfree(link);
995                 tu->tp.flags &= ~TP_FLAG_TRACE;
996         } else {
997                 tu->tp.flags &= ~TP_FLAG_PROFILE;
998         }
999         return ret;
1000 }
1001
1002 static void
1003 probe_event_disable(struct trace_uprobe *tu, struct trace_event_file *file)
1004 {
1005         if (!trace_probe_is_enabled(&tu->tp))
1006                 return;
1007
1008         if (file) {
1009                 struct event_file_link *link;
1010
1011                 link = find_event_file_link(&tu->tp, file);
1012                 if (!link)
1013                         return;
1014
1015                 list_del_rcu(&link->list);
1016                 /* synchronize with u{,ret}probe_trace_func */
1017                 synchronize_rcu();
1018                 kfree(link);
1019
1020                 if (!list_empty(&tu->tp.files))
1021                         return;
1022         }
1023
1024         WARN_ON(!uprobe_filter_is_empty(&tu->filter));
1025
1026         uprobe_unregister(tu->inode, tu->offset, &tu->consumer);
1027         tu->inode = NULL;
1028         tu->tp.flags &= file ? ~TP_FLAG_TRACE : ~TP_FLAG_PROFILE;
1029
1030         uprobe_buffer_disable();
1031 }
1032
1033 static int uprobe_event_define_fields(struct trace_event_call *event_call)
1034 {
1035         int ret, size;
1036         struct uprobe_trace_entry_head field;
1037         struct trace_uprobe *tu = event_call->data;
1038
1039         if (is_ret_probe(tu)) {
1040                 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_FUNC, 0);
1041                 DEFINE_FIELD(unsigned long, vaddr[1], FIELD_STRING_RETIP, 0);
1042                 size = SIZEOF_TRACE_ENTRY(true);
1043         } else {
1044                 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0);
1045                 size = SIZEOF_TRACE_ENTRY(false);
1046         }
1047
1048         return traceprobe_define_arg_fields(event_call, size, &tu->tp);
1049 }
1050
1051 #ifdef CONFIG_PERF_EVENTS
1052 static bool
1053 __uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm)
1054 {
1055         struct perf_event *event;
1056
1057         if (filter->nr_systemwide)
1058                 return true;
1059
1060         list_for_each_entry(event, &filter->perf_events, hw.tp_list) {
1061                 if (event->hw.target->mm == mm)
1062                         return true;
1063         }
1064
1065         return false;
1066 }
1067
1068 static inline bool
1069 uprobe_filter_event(struct trace_uprobe *tu, struct perf_event *event)
1070 {
1071         return __uprobe_perf_filter(&tu->filter, event->hw.target->mm);
1072 }
1073
1074 static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event)
1075 {
1076         bool done;
1077
1078         write_lock(&tu->filter.rwlock);
1079         if (event->hw.target) {
1080                 list_del(&event->hw.tp_list);
1081                 done = tu->filter.nr_systemwide ||
1082                         (event->hw.target->flags & PF_EXITING) ||
1083                         uprobe_filter_event(tu, event);
1084         } else {
1085                 tu->filter.nr_systemwide--;
1086                 done = tu->filter.nr_systemwide;
1087         }
1088         write_unlock(&tu->filter.rwlock);
1089
1090         if (!done)
1091                 return uprobe_apply(tu->inode, tu->offset, &tu->consumer, false);
1092
1093         return 0;
1094 }
1095
1096 static int uprobe_perf_open(struct trace_uprobe *tu, struct perf_event *event)
1097 {
1098         bool done;
1099         int err;
1100
1101         write_lock(&tu->filter.rwlock);
1102         if (event->hw.target) {
1103                 /*
1104                  * event->parent != NULL means copy_process(), we can avoid
1105                  * uprobe_apply(). current->mm must be probed and we can rely
1106                  * on dup_mmap() which preserves the already installed bp's.
1107                  *
1108                  * attr.enable_on_exec means that exec/mmap will install the
1109                  * breakpoints we need.
1110                  */
1111                 done = tu->filter.nr_systemwide ||
1112                         event->parent || event->attr.enable_on_exec ||
1113                         uprobe_filter_event(tu, event);
1114                 list_add(&event->hw.tp_list, &tu->filter.perf_events);
1115         } else {
1116                 done = tu->filter.nr_systemwide;
1117                 tu->filter.nr_systemwide++;
1118         }
1119         write_unlock(&tu->filter.rwlock);
1120
1121         err = 0;
1122         if (!done) {
1123                 err = uprobe_apply(tu->inode, tu->offset, &tu->consumer, true);
1124                 if (err)
1125                         uprobe_perf_close(tu, event);
1126         }
1127         return err;
1128 }
1129
1130 static bool uprobe_perf_filter(struct uprobe_consumer *uc,
1131                                 enum uprobe_filter_ctx ctx, struct mm_struct *mm)
1132 {
1133         struct trace_uprobe *tu;
1134         int ret;
1135
1136         tu = container_of(uc, struct trace_uprobe, consumer);
1137         read_lock(&tu->filter.rwlock);
1138         ret = __uprobe_perf_filter(&tu->filter, mm);
1139         read_unlock(&tu->filter.rwlock);
1140
1141         return ret;
1142 }
1143
1144 static void __uprobe_perf_func(struct trace_uprobe *tu,
1145                                unsigned long func, struct pt_regs *regs,
1146                                struct uprobe_cpu_buffer *ucb, int dsize)
1147 {
1148         struct trace_event_call *call = &tu->tp.call;
1149         struct uprobe_trace_entry_head *entry;
1150         struct hlist_head *head;
1151         void *data;
1152         int size, esize;
1153         int rctx;
1154
1155         if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs))
1156                 return;
1157
1158         esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1159
1160         size = esize + tu->tp.size + dsize;
1161         size = ALIGN(size + sizeof(u32), sizeof(u64)) - sizeof(u32);
1162         if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough"))
1163                 return;
1164
1165         preempt_disable();
1166         head = this_cpu_ptr(call->perf_events);
1167         if (hlist_empty(head))
1168                 goto out;
1169
1170         entry = perf_trace_buf_alloc(size, NULL, &rctx);
1171         if (!entry)
1172                 goto out;
1173
1174         if (is_ret_probe(tu)) {
1175                 entry->vaddr[0] = func;
1176                 entry->vaddr[1] = instruction_pointer(regs);
1177                 data = DATAOF_TRACE_ENTRY(entry, true);
1178         } else {
1179                 entry->vaddr[0] = instruction_pointer(regs);
1180                 data = DATAOF_TRACE_ENTRY(entry, false);
1181         }
1182
1183         memcpy(data, ucb->buf, tu->tp.size + dsize);
1184
1185         if (size - esize > tu->tp.size + dsize) {
1186                 int len = tu->tp.size + dsize;
1187
1188                 memset(data + len, 0, size - esize - len);
1189         }
1190
1191         perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1192                               head, NULL);
1193  out:
1194         preempt_enable();
1195 }
1196
1197 /* uprobe profile handler */
1198 static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs,
1199                             struct uprobe_cpu_buffer *ucb, int dsize)
1200 {
1201         if (!uprobe_perf_filter(&tu->consumer, 0, current->mm))
1202                 return UPROBE_HANDLER_REMOVE;
1203
1204         if (!is_ret_probe(tu))
1205                 __uprobe_perf_func(tu, 0, regs, ucb, dsize);
1206         return 0;
1207 }
1208
1209 static void uretprobe_perf_func(struct trace_uprobe *tu, unsigned long func,
1210                                 struct pt_regs *regs,
1211                                 struct uprobe_cpu_buffer *ucb, int dsize)
1212 {
1213         __uprobe_perf_func(tu, func, regs, ucb, dsize);
1214 }
1215
1216 int bpf_get_uprobe_info(const struct perf_event *event, u32 *fd_type,
1217                         const char **filename, u64 *probe_offset,
1218                         bool perf_type_tracepoint)
1219 {
1220         const char *pevent = trace_event_name(event->tp_event);
1221         const char *group = event->tp_event->class->system;
1222         struct trace_uprobe *tu;
1223
1224         if (perf_type_tracepoint)
1225                 tu = find_probe_event(pevent, group);
1226         else
1227                 tu = event->tp_event->data;
1228         if (!tu)
1229                 return -EINVAL;
1230
1231         *fd_type = is_ret_probe(tu) ? BPF_FD_TYPE_URETPROBE
1232                                     : BPF_FD_TYPE_UPROBE;
1233         *filename = tu->filename;
1234         *probe_offset = tu->offset;
1235         return 0;
1236 }
1237 #endif  /* CONFIG_PERF_EVENTS */
1238
1239 static int
1240 trace_uprobe_register(struct trace_event_call *event, enum trace_reg type,
1241                       void *data)
1242 {
1243         struct trace_uprobe *tu = event->data;
1244         struct trace_event_file *file = data;
1245
1246         switch (type) {
1247         case TRACE_REG_REGISTER:
1248                 return probe_event_enable(tu, file, NULL);
1249
1250         case TRACE_REG_UNREGISTER:
1251                 probe_event_disable(tu, file);
1252                 return 0;
1253
1254 #ifdef CONFIG_PERF_EVENTS
1255         case TRACE_REG_PERF_REGISTER:
1256                 return probe_event_enable(tu, NULL, uprobe_perf_filter);
1257
1258         case TRACE_REG_PERF_UNREGISTER:
1259                 probe_event_disable(tu, NULL);
1260                 return 0;
1261
1262         case TRACE_REG_PERF_OPEN:
1263                 return uprobe_perf_open(tu, data);
1264
1265         case TRACE_REG_PERF_CLOSE:
1266                 return uprobe_perf_close(tu, data);
1267
1268 #endif
1269         default:
1270                 return 0;
1271         }
1272         return 0;
1273 }
1274
1275 static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs)
1276 {
1277         struct trace_uprobe *tu;
1278         struct uprobe_dispatch_data udd;
1279         struct uprobe_cpu_buffer *ucb;
1280         int dsize, esize;
1281         int ret = 0;
1282
1283
1284         tu = container_of(con, struct trace_uprobe, consumer);
1285         tu->nhit++;
1286
1287         udd.tu = tu;
1288         udd.bp_addr = instruction_pointer(regs);
1289
1290         current->utask->vaddr = (unsigned long) &udd;
1291
1292         if (WARN_ON_ONCE(!uprobe_cpu_buffer))
1293                 return 0;
1294
1295         dsize = __get_data_size(&tu->tp, regs);
1296         esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1297
1298         ucb = uprobe_buffer_get();
1299         store_trace_args(ucb->buf, &tu->tp, regs, esize, dsize);
1300
1301         if (tu->tp.flags & TP_FLAG_TRACE)
1302                 ret |= uprobe_trace_func(tu, regs, ucb, dsize);
1303
1304 #ifdef CONFIG_PERF_EVENTS
1305         if (tu->tp.flags & TP_FLAG_PROFILE)
1306                 ret |= uprobe_perf_func(tu, regs, ucb, dsize);
1307 #endif
1308         uprobe_buffer_put(ucb);
1309         return ret;
1310 }
1311
1312 static int uretprobe_dispatcher(struct uprobe_consumer *con,
1313                                 unsigned long func, struct pt_regs *regs)
1314 {
1315         struct trace_uprobe *tu;
1316         struct uprobe_dispatch_data udd;
1317         struct uprobe_cpu_buffer *ucb;
1318         int dsize, esize;
1319
1320         tu = container_of(con, struct trace_uprobe, consumer);
1321
1322         udd.tu = tu;
1323         udd.bp_addr = func;
1324
1325         current->utask->vaddr = (unsigned long) &udd;
1326
1327         if (WARN_ON_ONCE(!uprobe_cpu_buffer))
1328                 return 0;
1329
1330         dsize = __get_data_size(&tu->tp, regs);
1331         esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1332
1333         ucb = uprobe_buffer_get();
1334         store_trace_args(ucb->buf, &tu->tp, regs, esize, dsize);
1335
1336         if (tu->tp.flags & TP_FLAG_TRACE)
1337                 uretprobe_trace_func(tu, func, regs, ucb, dsize);
1338
1339 #ifdef CONFIG_PERF_EVENTS
1340         if (tu->tp.flags & TP_FLAG_PROFILE)
1341                 uretprobe_perf_func(tu, func, regs, ucb, dsize);
1342 #endif
1343         uprobe_buffer_put(ucb);
1344         return 0;
1345 }
1346
1347 static struct trace_event_functions uprobe_funcs = {
1348         .trace          = print_uprobe_event
1349 };
1350
1351 static inline void init_trace_event_call(struct trace_uprobe *tu,
1352                                          struct trace_event_call *call)
1353 {
1354         INIT_LIST_HEAD(&call->class->fields);
1355         call->event.funcs = &uprobe_funcs;
1356         call->class->define_fields = uprobe_event_define_fields;
1357
1358         call->flags = TRACE_EVENT_FL_UPROBE;
1359         call->class->reg = trace_uprobe_register;
1360         call->data = tu;
1361 }
1362
1363 static int register_uprobe_event(struct trace_uprobe *tu)
1364 {
1365         struct trace_event_call *call = &tu->tp.call;
1366         int ret = 0;
1367
1368         init_trace_event_call(tu, call);
1369
1370         ret = register_trace_event(&call->event);
1371         if (!ret)
1372                 return -ENODEV;
1373
1374         ret = trace_add_event_call(call);
1375
1376         if (ret) {
1377                 pr_info("Failed to register uprobe event: %s\n",
1378                         trace_event_name(call));
1379                 unregister_trace_event(&call->event);
1380         }
1381
1382         return ret;
1383 }
1384
1385 static int unregister_uprobe_event(struct trace_uprobe *tu)
1386 {
1387         /* tu->event is unregistered in trace_remove_event_call() */
1388         return trace_remove_event_call(&tu->tp.call);
1389 }
1390
1391 #ifdef CONFIG_PERF_EVENTS
1392 struct trace_event_call *
1393 create_local_trace_uprobe(char *name, unsigned long offs,
1394                           unsigned long ref_ctr_offset, bool is_return)
1395 {
1396         struct trace_uprobe *tu;
1397         struct path path;
1398         int ret;
1399
1400         ret = kern_path(name, LOOKUP_FOLLOW, &path);
1401         if (ret)
1402                 return ERR_PTR(ret);
1403
1404         if (!d_is_reg(path.dentry)) {
1405                 path_put(&path);
1406                 return ERR_PTR(-EINVAL);
1407         }
1408
1409         /*
1410          * local trace_kprobes are not added to dyn_event, so they are never
1411          * searched in find_trace_kprobe(). Therefore, there is no concern of
1412          * duplicated name "DUMMY_EVENT" here.
1413          */
1414         tu = alloc_trace_uprobe(UPROBE_EVENT_SYSTEM, "DUMMY_EVENT", 0,
1415                                 is_return);
1416
1417         if (IS_ERR(tu)) {
1418                 pr_info("Failed to allocate trace_uprobe.(%d)\n",
1419                         (int)PTR_ERR(tu));
1420                 path_put(&path);
1421                 return ERR_CAST(tu);
1422         }
1423
1424         tu->offset = offs;
1425         tu->path = path;
1426         tu->ref_ctr_offset = ref_ctr_offset;
1427         tu->filename = kstrdup(name, GFP_KERNEL);
1428         init_trace_event_call(tu, &tu->tp.call);
1429
1430         if (traceprobe_set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0) {
1431                 ret = -ENOMEM;
1432                 goto error;
1433         }
1434
1435         return &tu->tp.call;
1436 error:
1437         free_trace_uprobe(tu);
1438         return ERR_PTR(ret);
1439 }
1440
1441 void destroy_local_trace_uprobe(struct trace_event_call *event_call)
1442 {
1443         struct trace_uprobe *tu;
1444
1445         tu = container_of(event_call, struct trace_uprobe, tp.call);
1446
1447         free_trace_uprobe(tu);
1448 }
1449 #endif /* CONFIG_PERF_EVENTS */
1450
1451 /* Make a trace interface for controling probe points */
1452 static __init int init_uprobe_trace(void)
1453 {
1454         struct dentry *d_tracer;
1455         int ret;
1456
1457         ret = dyn_event_register(&trace_uprobe_ops);
1458         if (ret)
1459                 return ret;
1460
1461         d_tracer = tracing_init_dentry();
1462         if (IS_ERR(d_tracer))
1463                 return 0;
1464
1465         trace_create_file("uprobe_events", 0644, d_tracer,
1466                                     NULL, &uprobe_events_ops);
1467         /* Profile interface */
1468         trace_create_file("uprobe_profile", 0444, d_tracer,
1469                                     NULL, &uprobe_profile_ops);
1470         return 0;
1471 }
1472
1473 fs_initcall(init_uprobe_trace);