]> rtime.felk.cvut.cz Git - linux-imx.git/blob - kernel/trace/trace_uprobe.c
0c0f0a7d4ff1cd24a4810c091a255f0f599446ba
[linux-imx.git] / kernel / trace / trace_uprobe.c
1 /*
2  * uprobes-based tracing events
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  *
17  * Copyright (C) IBM Corporation, 2010-2012
18  * Author:      Srikar Dronamraju <srikar@linux.vnet.ibm.com>
19  */
20
21 #include <linux/module.h>
22 #include <linux/uaccess.h>
23 #include <linux/uprobes.h>
24 #include <linux/namei.h>
25 #include <linux/string.h>
26
27 #include "trace_probe.h"
28
29 #define UPROBE_EVENT_SYSTEM     "uprobes"
30
31 struct uprobe_trace_entry_head {
32         struct trace_entry      ent;
33         unsigned long           vaddr[];
34 };
35
36 #define SIZEOF_TRACE_ENTRY(is_return)                   \
37         (sizeof(struct uprobe_trace_entry_head) +       \
38          sizeof(unsigned long) * (is_return ? 2 : 1))
39
40 #define DATAOF_TRACE_ENTRY(entry, is_return)            \
41         ((void*)(entry) + SIZEOF_TRACE_ENTRY(is_return))
42
43 struct trace_uprobe_filter {
44         rwlock_t                rwlock;
45         int                     nr_systemwide;
46         struct list_head        perf_events;
47 };
48
49 /*
50  * uprobe event core functions
51  */
52 struct trace_uprobe {
53         struct list_head                list;
54         struct ftrace_event_class       class;
55         struct ftrace_event_call        call;
56         struct trace_uprobe_filter      filter;
57         struct uprobe_consumer          consumer;
58         struct inode                    *inode;
59         char                            *filename;
60         unsigned long                   offset;
61         unsigned long                   nhit;
62         unsigned int                    flags;  /* For TP_FLAG_* */
63         ssize_t                         size;   /* trace entry size */
64         unsigned int                    nr_args;
65         struct probe_arg                args[];
66 };
67
68 #define SIZEOF_TRACE_UPROBE(n)                  \
69         (offsetof(struct trace_uprobe, args) +  \
70         (sizeof(struct probe_arg) * (n)))
71
72 static int register_uprobe_event(struct trace_uprobe *tu);
73 static void unregister_uprobe_event(struct trace_uprobe *tu);
74
75 static DEFINE_MUTEX(uprobe_lock);
76 static LIST_HEAD(uprobe_list);
77
78 static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs);
79
80 static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter)
81 {
82         rwlock_init(&filter->rwlock);
83         filter->nr_systemwide = 0;
84         INIT_LIST_HEAD(&filter->perf_events);
85 }
86
87 static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter *filter)
88 {
89         return !filter->nr_systemwide && list_empty(&filter->perf_events);
90 }
91
92 /*
93  * Allocate new trace_uprobe and initialize it (including uprobes).
94  */
95 static struct trace_uprobe *
96 alloc_trace_uprobe(const char *group, const char *event, int nargs)
97 {
98         struct trace_uprobe *tu;
99
100         if (!event || !is_good_name(event))
101                 return ERR_PTR(-EINVAL);
102
103         if (!group || !is_good_name(group))
104                 return ERR_PTR(-EINVAL);
105
106         tu = kzalloc(SIZEOF_TRACE_UPROBE(nargs), GFP_KERNEL);
107         if (!tu)
108                 return ERR_PTR(-ENOMEM);
109
110         tu->call.class = &tu->class;
111         tu->call.name = kstrdup(event, GFP_KERNEL);
112         if (!tu->call.name)
113                 goto error;
114
115         tu->class.system = kstrdup(group, GFP_KERNEL);
116         if (!tu->class.system)
117                 goto error;
118
119         INIT_LIST_HEAD(&tu->list);
120         tu->consumer.handler = uprobe_dispatcher;
121         init_trace_uprobe_filter(&tu->filter);
122         return tu;
123
124 error:
125         kfree(tu->call.name);
126         kfree(tu);
127
128         return ERR_PTR(-ENOMEM);
129 }
130
131 static void free_trace_uprobe(struct trace_uprobe *tu)
132 {
133         int i;
134
135         for (i = 0; i < tu->nr_args; i++)
136                 traceprobe_free_probe_arg(&tu->args[i]);
137
138         iput(tu->inode);
139         kfree(tu->call.class->system);
140         kfree(tu->call.name);
141         kfree(tu->filename);
142         kfree(tu);
143 }
144
145 static struct trace_uprobe *find_probe_event(const char *event, const char *group)
146 {
147         struct trace_uprobe *tu;
148
149         list_for_each_entry(tu, &uprobe_list, list)
150                 if (strcmp(tu->call.name, event) == 0 &&
151                     strcmp(tu->call.class->system, group) == 0)
152                         return tu;
153
154         return NULL;
155 }
156
157 /* Unregister a trace_uprobe and probe_event: call with locking uprobe_lock */
158 static void unregister_trace_uprobe(struct trace_uprobe *tu)
159 {
160         list_del(&tu->list);
161         unregister_uprobe_event(tu);
162         free_trace_uprobe(tu);
163 }
164
165 /* Register a trace_uprobe and probe_event */
166 static int register_trace_uprobe(struct trace_uprobe *tu)
167 {
168         struct trace_uprobe *old_tp;
169         int ret;
170
171         mutex_lock(&uprobe_lock);
172
173         /* register as an event */
174         old_tp = find_probe_event(tu->call.name, tu->call.class->system);
175         if (old_tp)
176                 /* delete old event */
177                 unregister_trace_uprobe(old_tp);
178
179         ret = register_uprobe_event(tu);
180         if (ret) {
181                 pr_warning("Failed to register probe event(%d)\n", ret);
182                 goto end;
183         }
184
185         list_add_tail(&tu->list, &uprobe_list);
186
187 end:
188         mutex_unlock(&uprobe_lock);
189
190         return ret;
191 }
192
193 /*
194  * Argument syntax:
195  *  - Add uprobe: p[:[GRP/]EVENT] PATH:SYMBOL[+offs] [FETCHARGS]
196  *
197  *  - Remove uprobe: -:[GRP/]EVENT
198  */
199 static int create_trace_uprobe(int argc, char **argv)
200 {
201         struct trace_uprobe *tu;
202         struct inode *inode;
203         char *arg, *event, *group, *filename;
204         char buf[MAX_EVENT_NAME_LEN];
205         struct path path;
206         unsigned long offset;
207         bool is_delete;
208         int i, ret;
209
210         inode = NULL;
211         ret = 0;
212         is_delete = false;
213         event = NULL;
214         group = NULL;
215
216         /* argc must be >= 1 */
217         if (argv[0][0] == '-')
218                 is_delete = true;
219         else if (argv[0][0] != 'p') {
220                 pr_info("Probe definition must be started with 'p' or '-'.\n");
221                 return -EINVAL;
222         }
223
224         if (argv[0][1] == ':') {
225                 event = &argv[0][2];
226                 arg = strchr(event, '/');
227
228                 if (arg) {
229                         group = event;
230                         event = arg + 1;
231                         event[-1] = '\0';
232
233                         if (strlen(group) == 0) {
234                                 pr_info("Group name is not specified\n");
235                                 return -EINVAL;
236                         }
237                 }
238                 if (strlen(event) == 0) {
239                         pr_info("Event name is not specified\n");
240                         return -EINVAL;
241                 }
242         }
243         if (!group)
244                 group = UPROBE_EVENT_SYSTEM;
245
246         if (is_delete) {
247                 if (!event) {
248                         pr_info("Delete command needs an event name.\n");
249                         return -EINVAL;
250                 }
251                 mutex_lock(&uprobe_lock);
252                 tu = find_probe_event(event, group);
253
254                 if (!tu) {
255                         mutex_unlock(&uprobe_lock);
256                         pr_info("Event %s/%s doesn't exist.\n", group, event);
257                         return -ENOENT;
258                 }
259                 /* delete an event */
260                 unregister_trace_uprobe(tu);
261                 mutex_unlock(&uprobe_lock);
262                 return 0;
263         }
264
265         if (argc < 2) {
266                 pr_info("Probe point is not specified.\n");
267                 return -EINVAL;
268         }
269         if (isdigit(argv[1][0])) {
270                 pr_info("probe point must be have a filename.\n");
271                 return -EINVAL;
272         }
273         arg = strchr(argv[1], ':');
274         if (!arg)
275                 goto fail_address_parse;
276
277         *arg++ = '\0';
278         filename = argv[1];
279         ret = kern_path(filename, LOOKUP_FOLLOW, &path);
280         if (ret)
281                 goto fail_address_parse;
282
283         inode = igrab(path.dentry->d_inode);
284         path_put(&path);
285
286         if (!inode || !S_ISREG(inode->i_mode)) {
287                 ret = -EINVAL;
288                 goto fail_address_parse;
289         }
290
291         ret = kstrtoul(arg, 0, &offset);
292         if (ret)
293                 goto fail_address_parse;
294
295         argc -= 2;
296         argv += 2;
297
298         /* setup a probe */
299         if (!event) {
300                 char *tail;
301                 char *ptr;
302
303                 tail = kstrdup(kbasename(filename), GFP_KERNEL);
304                 if (!tail) {
305                         ret = -ENOMEM;
306                         goto fail_address_parse;
307                 }
308
309                 ptr = strpbrk(tail, ".-_");
310                 if (ptr)
311                         *ptr = '\0';
312
313                 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset);
314                 event = buf;
315                 kfree(tail);
316         }
317
318         tu = alloc_trace_uprobe(group, event, argc);
319         if (IS_ERR(tu)) {
320                 pr_info("Failed to allocate trace_uprobe.(%d)\n", (int)PTR_ERR(tu));
321                 ret = PTR_ERR(tu);
322                 goto fail_address_parse;
323         }
324         tu->offset = offset;
325         tu->inode = inode;
326         tu->filename = kstrdup(filename, GFP_KERNEL);
327
328         if (!tu->filename) {
329                 pr_info("Failed to allocate filename.\n");
330                 ret = -ENOMEM;
331                 goto error;
332         }
333
334         /* parse arguments */
335         ret = 0;
336         for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
337                 /* Increment count for freeing args in error case */
338                 tu->nr_args++;
339
340                 /* Parse argument name */
341                 arg = strchr(argv[i], '=');
342                 if (arg) {
343                         *arg++ = '\0';
344                         tu->args[i].name = kstrdup(argv[i], GFP_KERNEL);
345                 } else {
346                         arg = argv[i];
347                         /* If argument name is omitted, set "argN" */
348                         snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
349                         tu->args[i].name = kstrdup(buf, GFP_KERNEL);
350                 }
351
352                 if (!tu->args[i].name) {
353                         pr_info("Failed to allocate argument[%d] name.\n", i);
354                         ret = -ENOMEM;
355                         goto error;
356                 }
357
358                 if (!is_good_name(tu->args[i].name)) {
359                         pr_info("Invalid argument[%d] name: %s\n", i, tu->args[i].name);
360                         ret = -EINVAL;
361                         goto error;
362                 }
363
364                 if (traceprobe_conflict_field_name(tu->args[i].name, tu->args, i)) {
365                         pr_info("Argument[%d] name '%s' conflicts with "
366                                 "another field.\n", i, argv[i]);
367                         ret = -EINVAL;
368                         goto error;
369                 }
370
371                 /* Parse fetch argument */
372                 ret = traceprobe_parse_probe_arg(arg, &tu->size, &tu->args[i], false, false);
373                 if (ret) {
374                         pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
375                         goto error;
376                 }
377         }
378
379         ret = register_trace_uprobe(tu);
380         if (ret)
381                 goto error;
382         return 0;
383
384 error:
385         free_trace_uprobe(tu);
386         return ret;
387
388 fail_address_parse:
389         if (inode)
390                 iput(inode);
391
392         pr_info("Failed to parse address or file.\n");
393
394         return ret;
395 }
396
397 static void cleanup_all_probes(void)
398 {
399         struct trace_uprobe *tu;
400
401         mutex_lock(&uprobe_lock);
402         while (!list_empty(&uprobe_list)) {
403                 tu = list_entry(uprobe_list.next, struct trace_uprobe, list);
404                 unregister_trace_uprobe(tu);
405         }
406         mutex_unlock(&uprobe_lock);
407 }
408
409 /* Probes listing interfaces */
410 static void *probes_seq_start(struct seq_file *m, loff_t *pos)
411 {
412         mutex_lock(&uprobe_lock);
413         return seq_list_start(&uprobe_list, *pos);
414 }
415
416 static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
417 {
418         return seq_list_next(v, &uprobe_list, pos);
419 }
420
421 static void probes_seq_stop(struct seq_file *m, void *v)
422 {
423         mutex_unlock(&uprobe_lock);
424 }
425
426 static int probes_seq_show(struct seq_file *m, void *v)
427 {
428         struct trace_uprobe *tu = v;
429         int i;
430
431         seq_printf(m, "p:%s/%s", tu->call.class->system, tu->call.name);
432         seq_printf(m, " %s:0x%p", tu->filename, (void *)tu->offset);
433
434         for (i = 0; i < tu->nr_args; i++)
435                 seq_printf(m, " %s=%s", tu->args[i].name, tu->args[i].comm);
436
437         seq_printf(m, "\n");
438         return 0;
439 }
440
441 static const struct seq_operations probes_seq_op = {
442         .start  = probes_seq_start,
443         .next   = probes_seq_next,
444         .stop   = probes_seq_stop,
445         .show   = probes_seq_show
446 };
447
448 static int probes_open(struct inode *inode, struct file *file)
449 {
450         if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC))
451                 cleanup_all_probes();
452
453         return seq_open(file, &probes_seq_op);
454 }
455
456 static ssize_t probes_write(struct file *file, const char __user *buffer,
457                             size_t count, loff_t *ppos)
458 {
459         return traceprobe_probes_write(file, buffer, count, ppos, create_trace_uprobe);
460 }
461
462 static const struct file_operations uprobe_events_ops = {
463         .owner          = THIS_MODULE,
464         .open           = probes_open,
465         .read           = seq_read,
466         .llseek         = seq_lseek,
467         .release        = seq_release,
468         .write          = probes_write,
469 };
470
471 /* Probes profiling interfaces */
472 static int probes_profile_seq_show(struct seq_file *m, void *v)
473 {
474         struct trace_uprobe *tu = v;
475
476         seq_printf(m, "  %s %-44s %15lu\n", tu->filename, tu->call.name, tu->nhit);
477         return 0;
478 }
479
480 static const struct seq_operations profile_seq_op = {
481         .start  = probes_seq_start,
482         .next   = probes_seq_next,
483         .stop   = probes_seq_stop,
484         .show   = probes_profile_seq_show
485 };
486
487 static int profile_open(struct inode *inode, struct file *file)
488 {
489         return seq_open(file, &profile_seq_op);
490 }
491
492 static const struct file_operations uprobe_profile_ops = {
493         .owner          = THIS_MODULE,
494         .open           = profile_open,
495         .read           = seq_read,
496         .llseek         = seq_lseek,
497         .release        = seq_release,
498 };
499
500 static void uprobe_trace_print(struct trace_uprobe *tu,
501                                 unsigned long func, struct pt_regs *regs)
502 {
503         struct uprobe_trace_entry_head *entry;
504         struct ring_buffer_event *event;
505         struct ring_buffer *buffer;
506         void *data;
507         int size, i;
508         struct ftrace_event_call *call = &tu->call;
509
510         size = SIZEOF_TRACE_ENTRY(false) + tu->size;
511         event = trace_current_buffer_lock_reserve(&buffer, call->event.type,
512                                                   size, 0, 0);
513         if (!event)
514                 return;
515
516         entry = ring_buffer_event_data(event);
517         entry->vaddr[0] = instruction_pointer(regs);
518         data = DATAOF_TRACE_ENTRY(entry, false);
519         for (i = 0; i < tu->nr_args; i++)
520                 call_fetch(&tu->args[i].fetch, regs, data + tu->args[i].offset);
521
522         if (!filter_current_check_discard(buffer, call, entry, event))
523                 trace_buffer_unlock_commit(buffer, event, 0, 0);
524 }
525
526 /* uprobe handler */
527 static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs)
528 {
529         uprobe_trace_print(tu, 0, regs);
530         return 0;
531 }
532
533 /* Event entry printers */
534 static enum print_line_t
535 print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event)
536 {
537         struct uprobe_trace_entry_head *entry;
538         struct trace_seq *s = &iter->seq;
539         struct trace_uprobe *tu;
540         u8 *data;
541         int i;
542
543         entry = (struct uprobe_trace_entry_head *)iter->ent;
544         tu = container_of(event, struct trace_uprobe, call.event);
545
546         if (!trace_seq_printf(s, "%s: (0x%lx)", tu->call.name, entry->vaddr[0]))
547                 goto partial;
548
549         data = DATAOF_TRACE_ENTRY(entry, false);
550         for (i = 0; i < tu->nr_args; i++) {
551                 if (!tu->args[i].type->print(s, tu->args[i].name,
552                                              data + tu->args[i].offset, entry))
553                         goto partial;
554         }
555
556         if (trace_seq_puts(s, "\n"))
557                 return TRACE_TYPE_HANDLED;
558
559 partial:
560         return TRACE_TYPE_PARTIAL_LINE;
561 }
562
563 static inline bool is_trace_uprobe_enabled(struct trace_uprobe *tu)
564 {
565         return tu->flags & (TP_FLAG_TRACE | TP_FLAG_PROFILE);
566 }
567
568 typedef bool (*filter_func_t)(struct uprobe_consumer *self,
569                                 enum uprobe_filter_ctx ctx,
570                                 struct mm_struct *mm);
571
572 static int
573 probe_event_enable(struct trace_uprobe *tu, int flag, filter_func_t filter)
574 {
575         int ret = 0;
576
577         if (is_trace_uprobe_enabled(tu))
578                 return -EINTR;
579
580         WARN_ON(!uprobe_filter_is_empty(&tu->filter));
581
582         tu->flags |= flag;
583         tu->consumer.filter = filter;
584         ret = uprobe_register(tu->inode, tu->offset, &tu->consumer);
585         if (ret)
586                 tu->flags &= ~flag;
587
588         return ret;
589 }
590
591 static void probe_event_disable(struct trace_uprobe *tu, int flag)
592 {
593         if (!is_trace_uprobe_enabled(tu))
594                 return;
595
596         WARN_ON(!uprobe_filter_is_empty(&tu->filter));
597
598         uprobe_unregister(tu->inode, tu->offset, &tu->consumer);
599         tu->flags &= ~flag;
600 }
601
602 static int uprobe_event_define_fields(struct ftrace_event_call *event_call)
603 {
604         int ret, i, size;
605         struct uprobe_trace_entry_head field;
606         struct trace_uprobe *tu = event_call->data;
607
608         DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0);
609         size = SIZEOF_TRACE_ENTRY(false);
610         /* Set argument names as fields */
611         for (i = 0; i < tu->nr_args; i++) {
612                 ret = trace_define_field(event_call, tu->args[i].type->fmttype,
613                                          tu->args[i].name,
614                                          size + tu->args[i].offset,
615                                          tu->args[i].type->size,
616                                          tu->args[i].type->is_signed,
617                                          FILTER_OTHER);
618
619                 if (ret)
620                         return ret;
621         }
622         return 0;
623 }
624
625 #define LEN_OR_ZERO             (len ? len - pos : 0)
626 static int __set_print_fmt(struct trace_uprobe *tu, char *buf, int len)
627 {
628         const char *fmt, *arg;
629         int i;
630         int pos = 0;
631
632         fmt = "(%lx)";
633         arg = "REC->" FIELD_STRING_IP;
634
635         /* When len=0, we just calculate the needed length */
636
637         pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt);
638
639         for (i = 0; i < tu->nr_args; i++) {
640                 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=%s",
641                                 tu->args[i].name, tu->args[i].type->fmt);
642         }
643
644         pos += snprintf(buf + pos, LEN_OR_ZERO, "\", %s", arg);
645
646         for (i = 0; i < tu->nr_args; i++) {
647                 pos += snprintf(buf + pos, LEN_OR_ZERO, ", REC->%s",
648                                 tu->args[i].name);
649         }
650
651         return pos;     /* return the length of print_fmt */
652 }
653 #undef LEN_OR_ZERO
654
655 static int set_print_fmt(struct trace_uprobe *tu)
656 {
657         char *print_fmt;
658         int len;
659
660         /* First: called with 0 length to calculate the needed length */
661         len = __set_print_fmt(tu, NULL, 0);
662         print_fmt = kmalloc(len + 1, GFP_KERNEL);
663         if (!print_fmt)
664                 return -ENOMEM;
665
666         /* Second: actually write the @print_fmt */
667         __set_print_fmt(tu, print_fmt, len + 1);
668         tu->call.print_fmt = print_fmt;
669
670         return 0;
671 }
672
673 #ifdef CONFIG_PERF_EVENTS
674 static bool
675 __uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm)
676 {
677         struct perf_event *event;
678
679         if (filter->nr_systemwide)
680                 return true;
681
682         list_for_each_entry(event, &filter->perf_events, hw.tp_list) {
683                 if (event->hw.tp_target->mm == mm)
684                         return true;
685         }
686
687         return false;
688 }
689
690 static inline bool
691 uprobe_filter_event(struct trace_uprobe *tu, struct perf_event *event)
692 {
693         return __uprobe_perf_filter(&tu->filter, event->hw.tp_target->mm);
694 }
695
696 static int uprobe_perf_open(struct trace_uprobe *tu, struct perf_event *event)
697 {
698         bool done;
699
700         write_lock(&tu->filter.rwlock);
701         if (event->hw.tp_target) {
702                 /*
703                  * event->parent != NULL means copy_process(), we can avoid
704                  * uprobe_apply(). current->mm must be probed and we can rely
705                  * on dup_mmap() which preserves the already installed bp's.
706                  *
707                  * attr.enable_on_exec means that exec/mmap will install the
708                  * breakpoints we need.
709                  */
710                 done = tu->filter.nr_systemwide ||
711                         event->parent || event->attr.enable_on_exec ||
712                         uprobe_filter_event(tu, event);
713                 list_add(&event->hw.tp_list, &tu->filter.perf_events);
714         } else {
715                 done = tu->filter.nr_systemwide;
716                 tu->filter.nr_systemwide++;
717         }
718         write_unlock(&tu->filter.rwlock);
719
720         if (!done)
721                 uprobe_apply(tu->inode, tu->offset, &tu->consumer, true);
722
723         return 0;
724 }
725
726 static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event)
727 {
728         bool done;
729
730         write_lock(&tu->filter.rwlock);
731         if (event->hw.tp_target) {
732                 list_del(&event->hw.tp_list);
733                 done = tu->filter.nr_systemwide ||
734                         (event->hw.tp_target->flags & PF_EXITING) ||
735                         uprobe_filter_event(tu, event);
736         } else {
737                 tu->filter.nr_systemwide--;
738                 done = tu->filter.nr_systemwide;
739         }
740         write_unlock(&tu->filter.rwlock);
741
742         if (!done)
743                 uprobe_apply(tu->inode, tu->offset, &tu->consumer, false);
744
745         return 0;
746 }
747
748 static bool uprobe_perf_filter(struct uprobe_consumer *uc,
749                                 enum uprobe_filter_ctx ctx, struct mm_struct *mm)
750 {
751         struct trace_uprobe *tu;
752         int ret;
753
754         tu = container_of(uc, struct trace_uprobe, consumer);
755         read_lock(&tu->filter.rwlock);
756         ret = __uprobe_perf_filter(&tu->filter, mm);
757         read_unlock(&tu->filter.rwlock);
758
759         return ret;
760 }
761
762 static void uprobe_perf_print(struct trace_uprobe *tu,
763                                 unsigned long func, struct pt_regs *regs)
764 {
765         struct ftrace_event_call *call = &tu->call;
766         struct uprobe_trace_entry_head *entry;
767         struct hlist_head *head;
768         unsigned long ip;
769         void *data;
770         int size, rctx, i;
771
772         size = SIZEOF_TRACE_ENTRY(false);
773         size = ALIGN(size + tu->size + sizeof(u32), sizeof(u64)) - sizeof(u32);
774         if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough"))
775                 return;
776
777         preempt_disable();
778         entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
779         if (!entry)
780                 goto out;
781
782         ip = instruction_pointer(regs);
783         entry->vaddr[0] = ip;
784         data = DATAOF_TRACE_ENTRY(entry, false);
785         for (i = 0; i < tu->nr_args; i++)
786                 call_fetch(&tu->args[i].fetch, regs, data + tu->args[i].offset);
787
788         head = this_cpu_ptr(call->perf_events);
789         perf_trace_buf_submit(entry, size, rctx, ip, 1, regs, head, NULL);
790  out:
791         preempt_enable();
792 }
793
794 /* uprobe profile handler */
795 static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs)
796 {
797         if (!uprobe_perf_filter(&tu->consumer, 0, current->mm))
798                 return UPROBE_HANDLER_REMOVE;
799
800         uprobe_perf_print(tu, 0, regs);
801         return 0;
802 }
803 #endif  /* CONFIG_PERF_EVENTS */
804
805 static
806 int trace_uprobe_register(struct ftrace_event_call *event, enum trace_reg type, void *data)
807 {
808         struct trace_uprobe *tu = event->data;
809
810         switch (type) {
811         case TRACE_REG_REGISTER:
812                 return probe_event_enable(tu, TP_FLAG_TRACE, NULL);
813
814         case TRACE_REG_UNREGISTER:
815                 probe_event_disable(tu, TP_FLAG_TRACE);
816                 return 0;
817
818 #ifdef CONFIG_PERF_EVENTS
819         case TRACE_REG_PERF_REGISTER:
820                 return probe_event_enable(tu, TP_FLAG_PROFILE, uprobe_perf_filter);
821
822         case TRACE_REG_PERF_UNREGISTER:
823                 probe_event_disable(tu, TP_FLAG_PROFILE);
824                 return 0;
825
826         case TRACE_REG_PERF_OPEN:
827                 return uprobe_perf_open(tu, data);
828
829         case TRACE_REG_PERF_CLOSE:
830                 return uprobe_perf_close(tu, data);
831
832 #endif
833         default:
834                 return 0;
835         }
836         return 0;
837 }
838
839 static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs)
840 {
841         struct trace_uprobe *tu;
842         int ret = 0;
843
844         tu = container_of(con, struct trace_uprobe, consumer);
845         tu->nhit++;
846
847         if (tu->flags & TP_FLAG_TRACE)
848                 ret |= uprobe_trace_func(tu, regs);
849
850 #ifdef CONFIG_PERF_EVENTS
851         if (tu->flags & TP_FLAG_PROFILE)
852                 ret |= uprobe_perf_func(tu, regs);
853 #endif
854         return ret;
855 }
856
857 static struct trace_event_functions uprobe_funcs = {
858         .trace          = print_uprobe_event
859 };
860
861 static int register_uprobe_event(struct trace_uprobe *tu)
862 {
863         struct ftrace_event_call *call = &tu->call;
864         int ret;
865
866         /* Initialize ftrace_event_call */
867         INIT_LIST_HEAD(&call->class->fields);
868         call->event.funcs = &uprobe_funcs;
869         call->class->define_fields = uprobe_event_define_fields;
870
871         if (set_print_fmt(tu) < 0)
872                 return -ENOMEM;
873
874         ret = register_ftrace_event(&call->event);
875         if (!ret) {
876                 kfree(call->print_fmt);
877                 return -ENODEV;
878         }
879         call->flags = 0;
880         call->class->reg = trace_uprobe_register;
881         call->data = tu;
882         ret = trace_add_event_call(call);
883
884         if (ret) {
885                 pr_info("Failed to register uprobe event: %s\n", call->name);
886                 kfree(call->print_fmt);
887                 unregister_ftrace_event(&call->event);
888         }
889
890         return ret;
891 }
892
893 static void unregister_uprobe_event(struct trace_uprobe *tu)
894 {
895         /* tu->event is unregistered in trace_remove_event_call() */
896         trace_remove_event_call(&tu->call);
897         kfree(tu->call.print_fmt);
898         tu->call.print_fmt = NULL;
899 }
900
901 /* Make a trace interface for controling probe points */
902 static __init int init_uprobe_trace(void)
903 {
904         struct dentry *d_tracer;
905
906         d_tracer = tracing_init_dentry();
907         if (!d_tracer)
908                 return 0;
909
910         trace_create_file("uprobe_events", 0644, d_tracer,
911                                     NULL, &uprobe_events_ops);
912         /* Profile interface */
913         trace_create_file("uprobe_profile", 0444, d_tracer,
914                                     NULL, &uprobe_profile_ops);
915         return 0;
916 }
917
918 fs_initcall(init_uprobe_trace);