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