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