]> asedeno.scripts.mit.edu Git - linux.git/blob - kernel/trace/trace_kprobe.c
tracing/kprobe: Add multi-probe per event support
[linux.git] / kernel / trace / trace_kprobe.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Kprobes-based tracing events
4  *
5  * Created by Masami Hiramatsu <mhiramat@redhat.com>
6  *
7  */
8 #define pr_fmt(fmt)     "trace_kprobe: " fmt
9
10 #include <linux/module.h>
11 #include <linux/uaccess.h>
12 #include <linux/rculist.h>
13 #include <linux/error-injection.h>
14
15 #include <asm/setup.h>  /* for COMMAND_LINE_SIZE */
16
17 #include "trace_dynevent.h"
18 #include "trace_kprobe_selftest.h"
19 #include "trace_probe.h"
20 #include "trace_probe_tmpl.h"
21
22 #define KPROBE_EVENT_SYSTEM "kprobes"
23 #define KRETPROBE_MAXACTIVE_MAX 4096
24 #define MAX_KPROBE_CMDLINE_SIZE 1024
25
26 /* Kprobe early definition from command line */
27 static char kprobe_boot_events_buf[COMMAND_LINE_SIZE] __initdata;
28 static bool kprobe_boot_events_enabled __initdata;
29
30 static int __init set_kprobe_boot_events(char *str)
31 {
32         strlcpy(kprobe_boot_events_buf, str, COMMAND_LINE_SIZE);
33         return 0;
34 }
35 __setup("kprobe_event=", set_kprobe_boot_events);
36
37 static int trace_kprobe_create(int argc, const char **argv);
38 static int trace_kprobe_show(struct seq_file *m, struct dyn_event *ev);
39 static int trace_kprobe_release(struct dyn_event *ev);
40 static bool trace_kprobe_is_busy(struct dyn_event *ev);
41 static bool trace_kprobe_match(const char *system, const char *event,
42                         int argc, const char **argv, struct dyn_event *ev);
43
44 static struct dyn_event_operations trace_kprobe_ops = {
45         .create = trace_kprobe_create,
46         .show = trace_kprobe_show,
47         .is_busy = trace_kprobe_is_busy,
48         .free = trace_kprobe_release,
49         .match = trace_kprobe_match,
50 };
51
52 /*
53  * Kprobe event core functions
54  */
55 struct trace_kprobe {
56         struct dyn_event        devent;
57         struct kretprobe        rp;     /* Use rp.kp for kprobe use */
58         unsigned long __percpu *nhit;
59         const char              *symbol;        /* symbol name */
60         struct trace_probe      tp;
61 };
62
63 static bool is_trace_kprobe(struct dyn_event *ev)
64 {
65         return ev->ops == &trace_kprobe_ops;
66 }
67
68 static struct trace_kprobe *to_trace_kprobe(struct dyn_event *ev)
69 {
70         return container_of(ev, struct trace_kprobe, devent);
71 }
72
73 /**
74  * for_each_trace_kprobe - iterate over the trace_kprobe list
75  * @pos:        the struct trace_kprobe * for each entry
76  * @dpos:       the struct dyn_event * to use as a loop cursor
77  */
78 #define for_each_trace_kprobe(pos, dpos)        \
79         for_each_dyn_event(dpos)                \
80                 if (is_trace_kprobe(dpos) && (pos = to_trace_kprobe(dpos)))
81
82 #define SIZEOF_TRACE_KPROBE(n)                          \
83         (offsetof(struct trace_kprobe, tp.args) +       \
84         (sizeof(struct probe_arg) * (n)))
85
86 static nokprobe_inline bool trace_kprobe_is_return(struct trace_kprobe *tk)
87 {
88         return tk->rp.handler != NULL;
89 }
90
91 static nokprobe_inline const char *trace_kprobe_symbol(struct trace_kprobe *tk)
92 {
93         return tk->symbol ? tk->symbol : "unknown";
94 }
95
96 static nokprobe_inline unsigned long trace_kprobe_offset(struct trace_kprobe *tk)
97 {
98         return tk->rp.kp.offset;
99 }
100
101 static nokprobe_inline bool trace_kprobe_has_gone(struct trace_kprobe *tk)
102 {
103         return !!(kprobe_gone(&tk->rp.kp));
104 }
105
106 static nokprobe_inline bool trace_kprobe_within_module(struct trace_kprobe *tk,
107                                                  struct module *mod)
108 {
109         int len = strlen(mod->name);
110         const char *name = trace_kprobe_symbol(tk);
111         return strncmp(mod->name, name, len) == 0 && name[len] == ':';
112 }
113
114 static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprobe *tk)
115 {
116         char *p;
117         bool ret;
118
119         if (!tk->symbol)
120                 return false;
121         p = strchr(tk->symbol, ':');
122         if (!p)
123                 return true;
124         *p = '\0';
125         mutex_lock(&module_mutex);
126         ret = !!find_module(tk->symbol);
127         mutex_unlock(&module_mutex);
128         *p = ':';
129
130         return ret;
131 }
132
133 static bool trace_kprobe_is_busy(struct dyn_event *ev)
134 {
135         struct trace_kprobe *tk = to_trace_kprobe(ev);
136
137         return trace_probe_is_enabled(&tk->tp);
138 }
139
140 static bool trace_kprobe_match(const char *system, const char *event,
141                         int argc, const char **argv, struct dyn_event *ev)
142 {
143         struct trace_kprobe *tk = to_trace_kprobe(ev);
144
145         return strcmp(trace_probe_name(&tk->tp), event) == 0 &&
146             (!system || strcmp(trace_probe_group_name(&tk->tp), system) == 0);
147 }
148
149 static nokprobe_inline unsigned long trace_kprobe_nhit(struct trace_kprobe *tk)
150 {
151         unsigned long nhit = 0;
152         int cpu;
153
154         for_each_possible_cpu(cpu)
155                 nhit += *per_cpu_ptr(tk->nhit, cpu);
156
157         return nhit;
158 }
159
160 static nokprobe_inline bool trace_kprobe_is_registered(struct trace_kprobe *tk)
161 {
162         return !(list_empty(&tk->rp.kp.list) &&
163                  hlist_unhashed(&tk->rp.kp.hlist));
164 }
165
166 /* Return 0 if it fails to find the symbol address */
167 static nokprobe_inline
168 unsigned long trace_kprobe_address(struct trace_kprobe *tk)
169 {
170         unsigned long addr;
171
172         if (tk->symbol) {
173                 addr = (unsigned long)
174                         kallsyms_lookup_name(trace_kprobe_symbol(tk));
175                 if (addr)
176                         addr += tk->rp.kp.offset;
177         } else {
178                 addr = (unsigned long)tk->rp.kp.addr;
179         }
180         return addr;
181 }
182
183 static nokprobe_inline struct trace_kprobe *
184 trace_kprobe_primary_from_call(struct trace_event_call *call)
185 {
186         struct trace_probe *tp;
187
188         tp = trace_probe_primary_from_call(call);
189         if (WARN_ON_ONCE(!tp))
190                 return NULL;
191
192         return container_of(tp, struct trace_kprobe, tp);
193 }
194
195 bool trace_kprobe_on_func_entry(struct trace_event_call *call)
196 {
197         struct trace_kprobe *tk = trace_kprobe_primary_from_call(call);
198
199         return tk ? kprobe_on_func_entry(tk->rp.kp.addr,
200                         tk->rp.kp.addr ? NULL : tk->rp.kp.symbol_name,
201                         tk->rp.kp.addr ? 0 : tk->rp.kp.offset) : false;
202 }
203
204 bool trace_kprobe_error_injectable(struct trace_event_call *call)
205 {
206         struct trace_kprobe *tk = trace_kprobe_primary_from_call(call);
207
208         return tk ? within_error_injection_list(trace_kprobe_address(tk)) :
209                false;
210 }
211
212 static int register_kprobe_event(struct trace_kprobe *tk);
213 static int unregister_kprobe_event(struct trace_kprobe *tk);
214
215 static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
216 static int kretprobe_dispatcher(struct kretprobe_instance *ri,
217                                 struct pt_regs *regs);
218
219 static void free_trace_kprobe(struct trace_kprobe *tk)
220 {
221         if (tk) {
222                 trace_probe_cleanup(&tk->tp);
223                 kfree(tk->symbol);
224                 free_percpu(tk->nhit);
225                 kfree(tk);
226         }
227 }
228
229 /*
230  * Allocate new trace_probe and initialize it (including kprobes).
231  */
232 static struct trace_kprobe *alloc_trace_kprobe(const char *group,
233                                              const char *event,
234                                              void *addr,
235                                              const char *symbol,
236                                              unsigned long offs,
237                                              int maxactive,
238                                              int nargs, bool is_return)
239 {
240         struct trace_kprobe *tk;
241         int ret = -ENOMEM;
242
243         tk = kzalloc(SIZEOF_TRACE_KPROBE(nargs), GFP_KERNEL);
244         if (!tk)
245                 return ERR_PTR(ret);
246
247         tk->nhit = alloc_percpu(unsigned long);
248         if (!tk->nhit)
249                 goto error;
250
251         if (symbol) {
252                 tk->symbol = kstrdup(symbol, GFP_KERNEL);
253                 if (!tk->symbol)
254                         goto error;
255                 tk->rp.kp.symbol_name = tk->symbol;
256                 tk->rp.kp.offset = offs;
257         } else
258                 tk->rp.kp.addr = addr;
259
260         if (is_return)
261                 tk->rp.handler = kretprobe_dispatcher;
262         else
263                 tk->rp.kp.pre_handler = kprobe_dispatcher;
264
265         tk->rp.maxactive = maxactive;
266         INIT_HLIST_NODE(&tk->rp.kp.hlist);
267         INIT_LIST_HEAD(&tk->rp.kp.list);
268
269         ret = trace_probe_init(&tk->tp, event, group);
270         if (ret < 0)
271                 goto error;
272
273         dyn_event_init(&tk->devent, &trace_kprobe_ops);
274         return tk;
275 error:
276         free_trace_kprobe(tk);
277         return ERR_PTR(ret);
278 }
279
280 static struct trace_kprobe *find_trace_kprobe(const char *event,
281                                               const char *group)
282 {
283         struct dyn_event *pos;
284         struct trace_kprobe *tk;
285
286         for_each_trace_kprobe(tk, pos)
287                 if (strcmp(trace_probe_name(&tk->tp), event) == 0 &&
288                     strcmp(trace_probe_group_name(&tk->tp), group) == 0)
289                         return tk;
290         return NULL;
291 }
292
293 static inline int __enable_trace_kprobe(struct trace_kprobe *tk)
294 {
295         int ret = 0;
296
297         if (trace_kprobe_is_registered(tk) && !trace_kprobe_has_gone(tk)) {
298                 if (trace_kprobe_is_return(tk))
299                         ret = enable_kretprobe(&tk->rp);
300                 else
301                         ret = enable_kprobe(&tk->rp.kp);
302         }
303
304         return ret;
305 }
306
307 static void __disable_trace_kprobe(struct trace_probe *tp)
308 {
309         struct trace_probe *pos;
310         struct trace_kprobe *tk;
311
312         list_for_each_entry(pos, trace_probe_probe_list(tp), list) {
313                 tk = container_of(pos, struct trace_kprobe, tp);
314                 if (!trace_kprobe_is_registered(tk))
315                         continue;
316                 if (trace_kprobe_is_return(tk))
317                         disable_kretprobe(&tk->rp);
318                 else
319                         disable_kprobe(&tk->rp.kp);
320         }
321 }
322
323 /*
324  * Enable trace_probe
325  * if the file is NULL, enable "perf" handler, or enable "trace" handler.
326  */
327 static int enable_trace_kprobe(struct trace_event_call *call,
328                                 struct trace_event_file *file)
329 {
330         struct trace_probe *pos, *tp;
331         struct trace_kprobe *tk;
332         bool enabled;
333         int ret = 0;
334
335         tp = trace_probe_primary_from_call(call);
336         if (WARN_ON_ONCE(!tp))
337                 return -ENODEV;
338         enabled = trace_probe_is_enabled(tp);
339
340         /* This also changes "enabled" state */
341         if (file) {
342                 ret = trace_probe_add_file(tp, file);
343                 if (ret)
344                         return ret;
345         } else
346                 trace_probe_set_flag(tp, TP_FLAG_PROFILE);
347
348         if (enabled)
349                 return 0;
350
351         enabled = false;
352         list_for_each_entry(pos, trace_probe_probe_list(tp), list) {
353                 tk = container_of(pos, struct trace_kprobe, tp);
354                 if (trace_kprobe_has_gone(tk))
355                         continue;
356                 ret = __enable_trace_kprobe(tk);
357                 if (ret) {
358                         if (enabled) {
359                                 __disable_trace_kprobe(tp);
360                                 enabled = false;
361                         }
362                         break;
363                 }
364                 enabled = true;
365         }
366
367         if (!enabled) {
368                 /* No probe is enabled. Roll back */
369                 if (file)
370                         trace_probe_remove_file(tp, file);
371                 else
372                         trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
373                 if (!ret)
374                         /* Since all probes are gone, this is not available */
375                         ret = -EADDRNOTAVAIL;
376         }
377
378         return ret;
379 }
380
381 /*
382  * Disable trace_probe
383  * if the file is NULL, disable "perf" handler, or disable "trace" handler.
384  */
385 static int disable_trace_kprobe(struct trace_event_call *call,
386                                 struct trace_event_file *file)
387 {
388         struct trace_probe *tp;
389
390         tp = trace_probe_primary_from_call(call);
391         if (WARN_ON_ONCE(!tp))
392                 return -ENODEV;
393
394         if (file) {
395                 if (!trace_probe_get_file_link(tp, file))
396                         return -ENOENT;
397                 if (!trace_probe_has_single_file(tp))
398                         goto out;
399                 trace_probe_clear_flag(tp, TP_FLAG_TRACE);
400         } else
401                 trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
402
403         if (!trace_probe_is_enabled(tp))
404                 __disable_trace_kprobe(tp);
405
406  out:
407         if (file)
408                 /*
409                  * Synchronization is done in below function. For perf event,
410                  * file == NULL and perf_trace_event_unreg() calls
411                  * tracepoint_synchronize_unregister() to ensure synchronize
412                  * event. We don't need to care about it.
413                  */
414                 trace_probe_remove_file(tp, file);
415
416         return 0;
417 }
418
419 #if defined(CONFIG_KPROBES_ON_FTRACE) && \
420         !defined(CONFIG_KPROBE_EVENTS_ON_NOTRACE)
421 static bool within_notrace_func(struct trace_kprobe *tk)
422 {
423         unsigned long offset, size, addr;
424
425         addr = trace_kprobe_address(tk);
426         if (!addr || !kallsyms_lookup_size_offset(addr, &size, &offset))
427                 return false;
428
429         /* Get the entry address of the target function */
430         addr -= offset;
431
432         /*
433          * Since ftrace_location_range() does inclusive range check, we need
434          * to subtract 1 byte from the end address.
435          */
436         return !ftrace_location_range(addr, addr + size - 1);
437 }
438 #else
439 #define within_notrace_func(tk) (false)
440 #endif
441
442 /* Internal register function - just handle k*probes and flags */
443 static int __register_trace_kprobe(struct trace_kprobe *tk)
444 {
445         int i, ret;
446
447         if (trace_kprobe_is_registered(tk))
448                 return -EINVAL;
449
450         if (within_notrace_func(tk)) {
451                 pr_warn("Could not probe notrace function %s\n",
452                         trace_kprobe_symbol(tk));
453                 return -EINVAL;
454         }
455
456         for (i = 0; i < tk->tp.nr_args; i++) {
457                 ret = traceprobe_update_arg(&tk->tp.args[i]);
458                 if (ret)
459                         return ret;
460         }
461
462         /* Set/clear disabled flag according to tp->flag */
463         if (trace_probe_is_enabled(&tk->tp))
464                 tk->rp.kp.flags &= ~KPROBE_FLAG_DISABLED;
465         else
466                 tk->rp.kp.flags |= KPROBE_FLAG_DISABLED;
467
468         if (trace_kprobe_is_return(tk))
469                 ret = register_kretprobe(&tk->rp);
470         else
471                 ret = register_kprobe(&tk->rp.kp);
472
473         return ret;
474 }
475
476 /* Internal unregister function - just handle k*probes and flags */
477 static void __unregister_trace_kprobe(struct trace_kprobe *tk)
478 {
479         if (trace_kprobe_is_registered(tk)) {
480                 if (trace_kprobe_is_return(tk))
481                         unregister_kretprobe(&tk->rp);
482                 else
483                         unregister_kprobe(&tk->rp.kp);
484                 /* Cleanup kprobe for reuse and mark it unregistered */
485                 INIT_HLIST_NODE(&tk->rp.kp.hlist);
486                 INIT_LIST_HEAD(&tk->rp.kp.list);
487                 if (tk->rp.kp.symbol_name)
488                         tk->rp.kp.addr = NULL;
489         }
490 }
491
492 /* Unregister a trace_probe and probe_event */
493 static int unregister_trace_kprobe(struct trace_kprobe *tk)
494 {
495         /* If other probes are on the event, just unregister kprobe */
496         if (trace_probe_has_sibling(&tk->tp))
497                 goto unreg;
498
499         /* Enabled event can not be unregistered */
500         if (trace_probe_is_enabled(&tk->tp))
501                 return -EBUSY;
502
503         /* Will fail if probe is being used by ftrace or perf */
504         if (unregister_kprobe_event(tk))
505                 return -EBUSY;
506
507 unreg:
508         __unregister_trace_kprobe(tk);
509         dyn_event_remove(&tk->devent);
510         trace_probe_unlink(&tk->tp);
511
512         return 0;
513 }
514
515 static int append_trace_kprobe(struct trace_kprobe *tk, struct trace_kprobe *to)
516 {
517         int ret;
518
519         /* Append to existing event */
520         ret = trace_probe_append(&tk->tp, &to->tp);
521         if (ret)
522                 return ret;
523
524         /* Register k*probe */
525         ret = __register_trace_kprobe(tk);
526         if (ret == -ENOENT && !trace_kprobe_module_exist(tk)) {
527                 pr_warn("This probe might be able to register after target module is loaded. Continue.\n");
528                 ret = 0;
529         }
530
531         if (ret)
532                 trace_probe_unlink(&tk->tp);
533         else
534                 dyn_event_add(&tk->devent);
535
536         return ret;
537 }
538
539 /* Register a trace_probe and probe_event */
540 static int register_trace_kprobe(struct trace_kprobe *tk)
541 {
542         struct trace_kprobe *old_tk;
543         int ret;
544
545         mutex_lock(&event_mutex);
546
547         old_tk = find_trace_kprobe(trace_probe_name(&tk->tp),
548                                    trace_probe_group_name(&tk->tp));
549         if (old_tk) {
550                 if (trace_kprobe_is_return(tk) != trace_kprobe_is_return(old_tk)) {
551                         trace_probe_log_set_index(0);
552                         trace_probe_log_err(0, DIFF_PROBE_TYPE);
553                         ret = -EEXIST;
554                 } else {
555                         ret = trace_probe_compare_arg_type(&tk->tp, &old_tk->tp);
556                         if (ret) {
557                                 /* Note that argument starts index = 2 */
558                                 trace_probe_log_set_index(ret + 1);
559                                 trace_probe_log_err(0, DIFF_ARG_TYPE);
560                                 ret = -EEXIST;
561                         } else
562                                 ret = append_trace_kprobe(tk, old_tk);
563                 }
564                 goto end;
565         }
566
567         /* Register new event */
568         ret = register_kprobe_event(tk);
569         if (ret) {
570                 pr_warn("Failed to register probe event(%d)\n", ret);
571                 goto end;
572         }
573
574         /* Register k*probe */
575         ret = __register_trace_kprobe(tk);
576         if (ret == -ENOENT && !trace_kprobe_module_exist(tk)) {
577                 pr_warn("This probe might be able to register after target module is loaded. Continue.\n");
578                 ret = 0;
579         }
580
581         if (ret < 0)
582                 unregister_kprobe_event(tk);
583         else
584                 dyn_event_add(&tk->devent);
585
586 end:
587         mutex_unlock(&event_mutex);
588         return ret;
589 }
590
591 /* Module notifier call back, checking event on the module */
592 static int trace_kprobe_module_callback(struct notifier_block *nb,
593                                        unsigned long val, void *data)
594 {
595         struct module *mod = data;
596         struct dyn_event *pos;
597         struct trace_kprobe *tk;
598         int ret;
599
600         if (val != MODULE_STATE_COMING)
601                 return NOTIFY_DONE;
602
603         /* Update probes on coming module */
604         mutex_lock(&event_mutex);
605         for_each_trace_kprobe(tk, pos) {
606                 if (trace_kprobe_within_module(tk, mod)) {
607                         /* Don't need to check busy - this should have gone. */
608                         __unregister_trace_kprobe(tk);
609                         ret = __register_trace_kprobe(tk);
610                         if (ret)
611                                 pr_warn("Failed to re-register probe %s on %s: %d\n",
612                                         trace_probe_name(&tk->tp),
613                                         mod->name, ret);
614                 }
615         }
616         mutex_unlock(&event_mutex);
617
618         return NOTIFY_DONE;
619 }
620
621 static struct notifier_block trace_kprobe_module_nb = {
622         .notifier_call = trace_kprobe_module_callback,
623         .priority = 1   /* Invoked after kprobe module callback */
624 };
625
626 /* Convert certain expected symbols into '_' when generating event names */
627 static inline void sanitize_event_name(char *name)
628 {
629         while (*name++ != '\0')
630                 if (*name == ':' || *name == '.')
631                         *name = '_';
632 }
633
634 static int trace_kprobe_create(int argc, const char *argv[])
635 {
636         /*
637          * Argument syntax:
638          *  - Add kprobe:
639          *      p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS]
640          *  - Add kretprobe:
641          *      r[MAXACTIVE][:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS]
642          * Fetch args:
643          *  $retval     : fetch return value
644          *  $stack      : fetch stack address
645          *  $stackN     : fetch Nth of stack (N:0-)
646          *  $comm       : fetch current task comm
647          *  @ADDR       : fetch memory at ADDR (ADDR should be in kernel)
648          *  @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
649          *  %REG        : fetch register REG
650          * Dereferencing memory fetch:
651          *  +|-offs(ARG) : fetch memory at ARG +|- offs address.
652          * Alias name of args:
653          *  NAME=FETCHARG : set NAME as alias of FETCHARG.
654          * Type of args:
655          *  FETCHARG:TYPE : use TYPE instead of unsigned long.
656          */
657         struct trace_kprobe *tk = NULL;
658         int i, len, ret = 0;
659         bool is_return = false;
660         char *symbol = NULL, *tmp = NULL;
661         const char *event = NULL, *group = KPROBE_EVENT_SYSTEM;
662         int maxactive = 0;
663         long offset = 0;
664         void *addr = NULL;
665         char buf[MAX_EVENT_NAME_LEN];
666         unsigned int flags = TPARG_FL_KERNEL;
667
668         switch (argv[0][0]) {
669         case 'r':
670                 is_return = true;
671                 flags |= TPARG_FL_RETURN;
672                 break;
673         case 'p':
674                 break;
675         default:
676                 return -ECANCELED;
677         }
678         if (argc < 2)
679                 return -ECANCELED;
680
681         trace_probe_log_init("trace_kprobe", argc, argv);
682
683         event = strchr(&argv[0][1], ':');
684         if (event)
685                 event++;
686
687         if (isdigit(argv[0][1])) {
688                 if (!is_return) {
689                         trace_probe_log_err(1, MAXACT_NO_KPROBE);
690                         goto parse_error;
691                 }
692                 if (event)
693                         len = event - &argv[0][1] - 1;
694                 else
695                         len = strlen(&argv[0][1]);
696                 if (len > MAX_EVENT_NAME_LEN - 1) {
697                         trace_probe_log_err(1, BAD_MAXACT);
698                         goto parse_error;
699                 }
700                 memcpy(buf, &argv[0][1], len);
701                 buf[len] = '\0';
702                 ret = kstrtouint(buf, 0, &maxactive);
703                 if (ret || !maxactive) {
704                         trace_probe_log_err(1, BAD_MAXACT);
705                         goto parse_error;
706                 }
707                 /* kretprobes instances are iterated over via a list. The
708                  * maximum should stay reasonable.
709                  */
710                 if (maxactive > KRETPROBE_MAXACTIVE_MAX) {
711                         trace_probe_log_err(1, MAXACT_TOO_BIG);
712                         goto parse_error;
713                 }
714         }
715
716         /* try to parse an address. if that fails, try to read the
717          * input as a symbol. */
718         if (kstrtoul(argv[1], 0, (unsigned long *)&addr)) {
719                 trace_probe_log_set_index(1);
720                 /* Check whether uprobe event specified */
721                 if (strchr(argv[1], '/') && strchr(argv[1], ':')) {
722                         ret = -ECANCELED;
723                         goto error;
724                 }
725                 /* a symbol specified */
726                 symbol = kstrdup(argv[1], GFP_KERNEL);
727                 if (!symbol)
728                         return -ENOMEM;
729                 /* TODO: support .init module functions */
730                 ret = traceprobe_split_symbol_offset(symbol, &offset);
731                 if (ret || offset < 0 || offset > UINT_MAX) {
732                         trace_probe_log_err(0, BAD_PROBE_ADDR);
733                         goto parse_error;
734                 }
735                 if (kprobe_on_func_entry(NULL, symbol, offset))
736                         flags |= TPARG_FL_FENTRY;
737                 if (offset && is_return && !(flags & TPARG_FL_FENTRY)) {
738                         trace_probe_log_err(0, BAD_RETPROBE);
739                         goto parse_error;
740                 }
741         }
742
743         trace_probe_log_set_index(0);
744         if (event) {
745                 ret = traceprobe_parse_event_name(&event, &group, buf,
746                                                   event - argv[0]);
747                 if (ret)
748                         goto parse_error;
749         } else {
750                 /* Make a new event name */
751                 if (symbol)
752                         snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
753                                  is_return ? 'r' : 'p', symbol, offset);
754                 else
755                         snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
756                                  is_return ? 'r' : 'p', addr);
757                 sanitize_event_name(buf);
758                 event = buf;
759         }
760
761         /* setup a probe */
762         tk = alloc_trace_kprobe(group, event, addr, symbol, offset, maxactive,
763                                argc - 2, is_return);
764         if (IS_ERR(tk)) {
765                 ret = PTR_ERR(tk);
766                 /* This must return -ENOMEM, else there is a bug */
767                 WARN_ON_ONCE(ret != -ENOMEM);
768                 goto out;       /* We know tk is not allocated */
769         }
770         argc -= 2; argv += 2;
771
772         /* parse arguments */
773         for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
774                 tmp = kstrdup(argv[i], GFP_KERNEL);
775                 if (!tmp) {
776                         ret = -ENOMEM;
777                         goto error;
778                 }
779
780                 trace_probe_log_set_index(i + 2);
781                 ret = traceprobe_parse_probe_arg(&tk->tp, i, tmp, flags);
782                 kfree(tmp);
783                 if (ret)
784                         goto error;     /* This can be -ENOMEM */
785         }
786
787         ret = traceprobe_set_print_fmt(&tk->tp, is_return);
788         if (ret < 0)
789                 goto error;
790
791         ret = register_trace_kprobe(tk);
792         if (ret) {
793                 trace_probe_log_set_index(1);
794                 if (ret == -EILSEQ)
795                         trace_probe_log_err(0, BAD_INSN_BNDRY);
796                 else if (ret == -ENOENT)
797                         trace_probe_log_err(0, BAD_PROBE_ADDR);
798                 else if (ret != -ENOMEM && ret != -EEXIST)
799                         trace_probe_log_err(0, FAIL_REG_PROBE);
800                 goto error;
801         }
802
803 out:
804         trace_probe_log_clear();
805         kfree(symbol);
806         return ret;
807
808 parse_error:
809         ret = -EINVAL;
810 error:
811         free_trace_kprobe(tk);
812         goto out;
813 }
814
815 static int create_or_delete_trace_kprobe(int argc, char **argv)
816 {
817         int ret;
818
819         if (argv[0][0] == '-')
820                 return dyn_event_release(argc, argv, &trace_kprobe_ops);
821
822         ret = trace_kprobe_create(argc, (const char **)argv);
823         return ret == -ECANCELED ? -EINVAL : ret;
824 }
825
826 static int trace_kprobe_release(struct dyn_event *ev)
827 {
828         struct trace_kprobe *tk = to_trace_kprobe(ev);
829         int ret = unregister_trace_kprobe(tk);
830
831         if (!ret)
832                 free_trace_kprobe(tk);
833         return ret;
834 }
835
836 static int trace_kprobe_show(struct seq_file *m, struct dyn_event *ev)
837 {
838         struct trace_kprobe *tk = to_trace_kprobe(ev);
839         int i;
840
841         seq_putc(m, trace_kprobe_is_return(tk) ? 'r' : 'p');
842         seq_printf(m, ":%s/%s", trace_probe_group_name(&tk->tp),
843                                 trace_probe_name(&tk->tp));
844
845         if (!tk->symbol)
846                 seq_printf(m, " 0x%p", tk->rp.kp.addr);
847         else if (tk->rp.kp.offset)
848                 seq_printf(m, " %s+%u", trace_kprobe_symbol(tk),
849                            tk->rp.kp.offset);
850         else
851                 seq_printf(m, " %s", trace_kprobe_symbol(tk));
852
853         for (i = 0; i < tk->tp.nr_args; i++)
854                 seq_printf(m, " %s=%s", tk->tp.args[i].name, tk->tp.args[i].comm);
855         seq_putc(m, '\n');
856
857         return 0;
858 }
859
860 static int probes_seq_show(struct seq_file *m, void *v)
861 {
862         struct dyn_event *ev = v;
863
864         if (!is_trace_kprobe(ev))
865                 return 0;
866
867         return trace_kprobe_show(m, ev);
868 }
869
870 static const struct seq_operations probes_seq_op = {
871         .start  = dyn_event_seq_start,
872         .next   = dyn_event_seq_next,
873         .stop   = dyn_event_seq_stop,
874         .show   = probes_seq_show
875 };
876
877 static int probes_open(struct inode *inode, struct file *file)
878 {
879         int ret;
880
881         if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
882                 ret = dyn_events_release_all(&trace_kprobe_ops);
883                 if (ret < 0)
884                         return ret;
885         }
886
887         return seq_open(file, &probes_seq_op);
888 }
889
890 static ssize_t probes_write(struct file *file, const char __user *buffer,
891                             size_t count, loff_t *ppos)
892 {
893         return trace_parse_run_command(file, buffer, count, ppos,
894                                        create_or_delete_trace_kprobe);
895 }
896
897 static const struct file_operations kprobe_events_ops = {
898         .owner          = THIS_MODULE,
899         .open           = probes_open,
900         .read           = seq_read,
901         .llseek         = seq_lseek,
902         .release        = seq_release,
903         .write          = probes_write,
904 };
905
906 /* Probes profiling interfaces */
907 static int probes_profile_seq_show(struct seq_file *m, void *v)
908 {
909         struct dyn_event *ev = v;
910         struct trace_kprobe *tk;
911
912         if (!is_trace_kprobe(ev))
913                 return 0;
914
915         tk = to_trace_kprobe(ev);
916         seq_printf(m, "  %-44s %15lu %15lu\n",
917                    trace_probe_name(&tk->tp),
918                    trace_kprobe_nhit(tk),
919                    tk->rp.kp.nmissed);
920
921         return 0;
922 }
923
924 static const struct seq_operations profile_seq_op = {
925         .start  = dyn_event_seq_start,
926         .next   = dyn_event_seq_next,
927         .stop   = dyn_event_seq_stop,
928         .show   = probes_profile_seq_show
929 };
930
931 static int profile_open(struct inode *inode, struct file *file)
932 {
933         return seq_open(file, &profile_seq_op);
934 }
935
936 static const struct file_operations kprobe_profile_ops = {
937         .owner          = THIS_MODULE,
938         .open           = profile_open,
939         .read           = seq_read,
940         .llseek         = seq_lseek,
941         .release        = seq_release,
942 };
943
944 /* Kprobe specific fetch functions */
945
946 /* Return the length of string -- including null terminal byte */
947 static nokprobe_inline int
948 fetch_store_strlen(unsigned long addr)
949 {
950         int ret, len = 0;
951         u8 c;
952
953         do {
954                 ret = probe_kernel_read(&c, (u8 *)addr + len, 1);
955                 len++;
956         } while (c && ret == 0 && len < MAX_STRING_SIZE);
957
958         return (ret < 0) ? ret : len;
959 }
960
961 /* Return the length of string -- including null terminal byte */
962 static nokprobe_inline int
963 fetch_store_strlen_user(unsigned long addr)
964 {
965         const void __user *uaddr =  (__force const void __user *)addr;
966
967         return strnlen_unsafe_user(uaddr, MAX_STRING_SIZE);
968 }
969
970 /*
971  * Fetch a null-terminated string. Caller MUST set *(u32 *)buf with max
972  * length and relative data location.
973  */
974 static nokprobe_inline int
975 fetch_store_string(unsigned long addr, void *dest, void *base)
976 {
977         int maxlen = get_loc_len(*(u32 *)dest);
978         void *__dest;
979         long ret;
980
981         if (unlikely(!maxlen))
982                 return -ENOMEM;
983
984         __dest = get_loc_data(dest, base);
985
986         /*
987          * Try to get string again, since the string can be changed while
988          * probing.
989          */
990         ret = strncpy_from_unsafe(__dest, (void *)addr, maxlen);
991         if (ret >= 0)
992                 *(u32 *)dest = make_data_loc(ret, __dest - base);
993
994         return ret;
995 }
996
997 /*
998  * Fetch a null-terminated string from user. Caller MUST set *(u32 *)buf
999  * with max length and relative data location.
1000  */
1001 static nokprobe_inline int
1002 fetch_store_string_user(unsigned long addr, void *dest, void *base)
1003 {
1004         const void __user *uaddr =  (__force const void __user *)addr;
1005         int maxlen = get_loc_len(*(u32 *)dest);
1006         void *__dest;
1007         long ret;
1008
1009         if (unlikely(!maxlen))
1010                 return -ENOMEM;
1011
1012         __dest = get_loc_data(dest, base);
1013
1014         ret = strncpy_from_unsafe_user(__dest, uaddr, maxlen);
1015         if (ret >= 0)
1016                 *(u32 *)dest = make_data_loc(ret, __dest - base);
1017
1018         return ret;
1019 }
1020
1021 static nokprobe_inline int
1022 probe_mem_read(void *dest, void *src, size_t size)
1023 {
1024         return probe_kernel_read(dest, src, size);
1025 }
1026
1027 static nokprobe_inline int
1028 probe_mem_read_user(void *dest, void *src, size_t size)
1029 {
1030         const void __user *uaddr =  (__force const void __user *)src;
1031
1032         return probe_user_read(dest, uaddr, size);
1033 }
1034
1035 /* Note that we don't verify it, since the code does not come from user space */
1036 static int
1037 process_fetch_insn(struct fetch_insn *code, struct pt_regs *regs, void *dest,
1038                    void *base)
1039 {
1040         unsigned long val;
1041
1042 retry:
1043         /* 1st stage: get value from context */
1044         switch (code->op) {
1045         case FETCH_OP_REG:
1046                 val = regs_get_register(regs, code->param);
1047                 break;
1048         case FETCH_OP_STACK:
1049                 val = regs_get_kernel_stack_nth(regs, code->param);
1050                 break;
1051         case FETCH_OP_STACKP:
1052                 val = kernel_stack_pointer(regs);
1053                 break;
1054         case FETCH_OP_RETVAL:
1055                 val = regs_return_value(regs);
1056                 break;
1057         case FETCH_OP_IMM:
1058                 val = code->immediate;
1059                 break;
1060         case FETCH_OP_COMM:
1061                 val = (unsigned long)current->comm;
1062                 break;
1063 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
1064         case FETCH_OP_ARG:
1065                 val = regs_get_kernel_argument(regs, code->param);
1066                 break;
1067 #endif
1068         case FETCH_NOP_SYMBOL:  /* Ignore a place holder */
1069                 code++;
1070                 goto retry;
1071         default:
1072                 return -EILSEQ;
1073         }
1074         code++;
1075
1076         return process_fetch_insn_bottom(code, val, dest, base);
1077 }
1078 NOKPROBE_SYMBOL(process_fetch_insn)
1079
1080 /* Kprobe handler */
1081 static nokprobe_inline void
1082 __kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs,
1083                     struct trace_event_file *trace_file)
1084 {
1085         struct kprobe_trace_entry_head *entry;
1086         struct ring_buffer_event *event;
1087         struct ring_buffer *buffer;
1088         int size, dsize, pc;
1089         unsigned long irq_flags;
1090         struct trace_event_call *call = trace_probe_event_call(&tk->tp);
1091
1092         WARN_ON(call != trace_file->event_call);
1093
1094         if (trace_trigger_soft_disabled(trace_file))
1095                 return;
1096
1097         local_save_flags(irq_flags);
1098         pc = preempt_count();
1099
1100         dsize = __get_data_size(&tk->tp, regs);
1101         size = sizeof(*entry) + tk->tp.size + dsize;
1102
1103         event = trace_event_buffer_lock_reserve(&buffer, trace_file,
1104                                                 call->event.type,
1105                                                 size, irq_flags, pc);
1106         if (!event)
1107                 return;
1108
1109         entry = ring_buffer_event_data(event);
1110         entry->ip = (unsigned long)tk->rp.kp.addr;
1111         store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
1112
1113         event_trigger_unlock_commit_regs(trace_file, buffer, event,
1114                                          entry, irq_flags, pc, regs);
1115 }
1116
1117 static void
1118 kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs)
1119 {
1120         struct event_file_link *link;
1121
1122         trace_probe_for_each_link_rcu(link, &tk->tp)
1123                 __kprobe_trace_func(tk, regs, link->file);
1124 }
1125 NOKPROBE_SYMBOL(kprobe_trace_func);
1126
1127 /* Kretprobe handler */
1128 static nokprobe_inline void
1129 __kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
1130                        struct pt_regs *regs,
1131                        struct trace_event_file *trace_file)
1132 {
1133         struct kretprobe_trace_entry_head *entry;
1134         struct ring_buffer_event *event;
1135         struct ring_buffer *buffer;
1136         int size, pc, dsize;
1137         unsigned long irq_flags;
1138         struct trace_event_call *call = trace_probe_event_call(&tk->tp);
1139
1140         WARN_ON(call != trace_file->event_call);
1141
1142         if (trace_trigger_soft_disabled(trace_file))
1143                 return;
1144
1145         local_save_flags(irq_flags);
1146         pc = preempt_count();
1147
1148         dsize = __get_data_size(&tk->tp, regs);
1149         size = sizeof(*entry) + tk->tp.size + dsize;
1150
1151         event = trace_event_buffer_lock_reserve(&buffer, trace_file,
1152                                                 call->event.type,
1153                                                 size, irq_flags, pc);
1154         if (!event)
1155                 return;
1156
1157         entry = ring_buffer_event_data(event);
1158         entry->func = (unsigned long)tk->rp.kp.addr;
1159         entry->ret_ip = (unsigned long)ri->ret_addr;
1160         store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
1161
1162         event_trigger_unlock_commit_regs(trace_file, buffer, event,
1163                                          entry, irq_flags, pc, regs);
1164 }
1165
1166 static void
1167 kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
1168                      struct pt_regs *regs)
1169 {
1170         struct event_file_link *link;
1171
1172         trace_probe_for_each_link_rcu(link, &tk->tp)
1173                 __kretprobe_trace_func(tk, ri, regs, link->file);
1174 }
1175 NOKPROBE_SYMBOL(kretprobe_trace_func);
1176
1177 /* Event entry printers */
1178 static enum print_line_t
1179 print_kprobe_event(struct trace_iterator *iter, int flags,
1180                    struct trace_event *event)
1181 {
1182         struct kprobe_trace_entry_head *field;
1183         struct trace_seq *s = &iter->seq;
1184         struct trace_probe *tp;
1185
1186         field = (struct kprobe_trace_entry_head *)iter->ent;
1187         tp = trace_probe_primary_from_call(
1188                 container_of(event, struct trace_event_call, event));
1189         if (WARN_ON_ONCE(!tp))
1190                 goto out;
1191
1192         trace_seq_printf(s, "%s: (", trace_probe_name(tp));
1193
1194         if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
1195                 goto out;
1196
1197         trace_seq_putc(s, ')');
1198
1199         if (print_probe_args(s, tp->args, tp->nr_args,
1200                              (u8 *)&field[1], field) < 0)
1201                 goto out;
1202
1203         trace_seq_putc(s, '\n');
1204  out:
1205         return trace_handle_return(s);
1206 }
1207
1208 static enum print_line_t
1209 print_kretprobe_event(struct trace_iterator *iter, int flags,
1210                       struct trace_event *event)
1211 {
1212         struct kretprobe_trace_entry_head *field;
1213         struct trace_seq *s = &iter->seq;
1214         struct trace_probe *tp;
1215
1216         field = (struct kretprobe_trace_entry_head *)iter->ent;
1217         tp = trace_probe_primary_from_call(
1218                 container_of(event, struct trace_event_call, event));
1219         if (WARN_ON_ONCE(!tp))
1220                 goto out;
1221
1222         trace_seq_printf(s, "%s: (", trace_probe_name(tp));
1223
1224         if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
1225                 goto out;
1226
1227         trace_seq_puts(s, " <- ");
1228
1229         if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
1230                 goto out;
1231
1232         trace_seq_putc(s, ')');
1233
1234         if (print_probe_args(s, tp->args, tp->nr_args,
1235                              (u8 *)&field[1], field) < 0)
1236                 goto out;
1237
1238         trace_seq_putc(s, '\n');
1239
1240  out:
1241         return trace_handle_return(s);
1242 }
1243
1244
1245 static int kprobe_event_define_fields(struct trace_event_call *event_call)
1246 {
1247         int ret;
1248         struct kprobe_trace_entry_head field;
1249         struct trace_probe *tp;
1250
1251         tp = trace_probe_primary_from_call(event_call);
1252         if (WARN_ON_ONCE(!tp))
1253                 return -ENOENT;
1254
1255         DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
1256
1257         return traceprobe_define_arg_fields(event_call, sizeof(field), tp);
1258 }
1259
1260 static int kretprobe_event_define_fields(struct trace_event_call *event_call)
1261 {
1262         int ret;
1263         struct kretprobe_trace_entry_head field;
1264         struct trace_probe *tp;
1265
1266         tp = trace_probe_primary_from_call(event_call);
1267         if (WARN_ON_ONCE(!tp))
1268                 return -ENOENT;
1269
1270         DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
1271         DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
1272
1273         return traceprobe_define_arg_fields(event_call, sizeof(field), tp);
1274 }
1275
1276 #ifdef CONFIG_PERF_EVENTS
1277
1278 /* Kprobe profile handler */
1279 static int
1280 kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs)
1281 {
1282         struct trace_event_call *call = trace_probe_event_call(&tk->tp);
1283         struct kprobe_trace_entry_head *entry;
1284         struct hlist_head *head;
1285         int size, __size, dsize;
1286         int rctx;
1287
1288         if (bpf_prog_array_valid(call)) {
1289                 unsigned long orig_ip = instruction_pointer(regs);
1290                 int ret;
1291
1292                 ret = trace_call_bpf(call, regs);
1293
1294                 /*
1295                  * We need to check and see if we modified the pc of the
1296                  * pt_regs, and if so return 1 so that we don't do the
1297                  * single stepping.
1298                  */
1299                 if (orig_ip != instruction_pointer(regs))
1300                         return 1;
1301                 if (!ret)
1302                         return 0;
1303         }
1304
1305         head = this_cpu_ptr(call->perf_events);
1306         if (hlist_empty(head))
1307                 return 0;
1308
1309         dsize = __get_data_size(&tk->tp, regs);
1310         __size = sizeof(*entry) + tk->tp.size + dsize;
1311         size = ALIGN(__size + sizeof(u32), sizeof(u64));
1312         size -= sizeof(u32);
1313
1314         entry = perf_trace_buf_alloc(size, NULL, &rctx);
1315         if (!entry)
1316                 return 0;
1317
1318         entry->ip = (unsigned long)tk->rp.kp.addr;
1319         memset(&entry[1], 0, dsize);
1320         store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
1321         perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1322                               head, NULL);
1323         return 0;
1324 }
1325 NOKPROBE_SYMBOL(kprobe_perf_func);
1326
1327 /* Kretprobe profile handler */
1328 static void
1329 kretprobe_perf_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
1330                     struct pt_regs *regs)
1331 {
1332         struct trace_event_call *call = trace_probe_event_call(&tk->tp);
1333         struct kretprobe_trace_entry_head *entry;
1334         struct hlist_head *head;
1335         int size, __size, dsize;
1336         int rctx;
1337
1338         if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs))
1339                 return;
1340
1341         head = this_cpu_ptr(call->perf_events);
1342         if (hlist_empty(head))
1343                 return;
1344
1345         dsize = __get_data_size(&tk->tp, regs);
1346         __size = sizeof(*entry) + tk->tp.size + dsize;
1347         size = ALIGN(__size + sizeof(u32), sizeof(u64));
1348         size -= sizeof(u32);
1349
1350         entry = perf_trace_buf_alloc(size, NULL, &rctx);
1351         if (!entry)
1352                 return;
1353
1354         entry->func = (unsigned long)tk->rp.kp.addr;
1355         entry->ret_ip = (unsigned long)ri->ret_addr;
1356         store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
1357         perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1358                               head, NULL);
1359 }
1360 NOKPROBE_SYMBOL(kretprobe_perf_func);
1361
1362 int bpf_get_kprobe_info(const struct perf_event *event, u32 *fd_type,
1363                         const char **symbol, u64 *probe_offset,
1364                         u64 *probe_addr, bool perf_type_tracepoint)
1365 {
1366         const char *pevent = trace_event_name(event->tp_event);
1367         const char *group = event->tp_event->class->system;
1368         struct trace_kprobe *tk;
1369
1370         if (perf_type_tracepoint)
1371                 tk = find_trace_kprobe(pevent, group);
1372         else
1373                 tk = event->tp_event->data;
1374         if (!tk)
1375                 return -EINVAL;
1376
1377         *fd_type = trace_kprobe_is_return(tk) ? BPF_FD_TYPE_KRETPROBE
1378                                               : BPF_FD_TYPE_KPROBE;
1379         if (tk->symbol) {
1380                 *symbol = tk->symbol;
1381                 *probe_offset = tk->rp.kp.offset;
1382                 *probe_addr = 0;
1383         } else {
1384                 *symbol = NULL;
1385                 *probe_offset = 0;
1386                 *probe_addr = (unsigned long)tk->rp.kp.addr;
1387         }
1388         return 0;
1389 }
1390 #endif  /* CONFIG_PERF_EVENTS */
1391
1392 /*
1393  * called by perf_trace_init() or __ftrace_set_clr_event() under event_mutex.
1394  *
1395  * kprobe_trace_self_tests_init() does enable_trace_probe/disable_trace_probe
1396  * lockless, but we can't race with this __init function.
1397  */
1398 static int kprobe_register(struct trace_event_call *event,
1399                            enum trace_reg type, void *data)
1400 {
1401         struct trace_event_file *file = data;
1402
1403         switch (type) {
1404         case TRACE_REG_REGISTER:
1405                 return enable_trace_kprobe(event, file);
1406         case TRACE_REG_UNREGISTER:
1407                 return disable_trace_kprobe(event, file);
1408
1409 #ifdef CONFIG_PERF_EVENTS
1410         case TRACE_REG_PERF_REGISTER:
1411                 return enable_trace_kprobe(event, NULL);
1412         case TRACE_REG_PERF_UNREGISTER:
1413                 return disable_trace_kprobe(event, NULL);
1414         case TRACE_REG_PERF_OPEN:
1415         case TRACE_REG_PERF_CLOSE:
1416         case TRACE_REG_PERF_ADD:
1417         case TRACE_REG_PERF_DEL:
1418                 return 0;
1419 #endif
1420         }
1421         return 0;
1422 }
1423
1424 static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
1425 {
1426         struct trace_kprobe *tk = container_of(kp, struct trace_kprobe, rp.kp);
1427         int ret = 0;
1428
1429         raw_cpu_inc(*tk->nhit);
1430
1431         if (trace_probe_test_flag(&tk->tp, TP_FLAG_TRACE))
1432                 kprobe_trace_func(tk, regs);
1433 #ifdef CONFIG_PERF_EVENTS
1434         if (trace_probe_test_flag(&tk->tp, TP_FLAG_PROFILE))
1435                 ret = kprobe_perf_func(tk, regs);
1436 #endif
1437         return ret;
1438 }
1439 NOKPROBE_SYMBOL(kprobe_dispatcher);
1440
1441 static int
1442 kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
1443 {
1444         struct trace_kprobe *tk = container_of(ri->rp, struct trace_kprobe, rp);
1445
1446         raw_cpu_inc(*tk->nhit);
1447
1448         if (trace_probe_test_flag(&tk->tp, TP_FLAG_TRACE))
1449                 kretprobe_trace_func(tk, ri, regs);
1450 #ifdef CONFIG_PERF_EVENTS
1451         if (trace_probe_test_flag(&tk->tp, TP_FLAG_PROFILE))
1452                 kretprobe_perf_func(tk, ri, regs);
1453 #endif
1454         return 0;       /* We don't tweek kernel, so just return 0 */
1455 }
1456 NOKPROBE_SYMBOL(kretprobe_dispatcher);
1457
1458 static struct trace_event_functions kretprobe_funcs = {
1459         .trace          = print_kretprobe_event
1460 };
1461
1462 static struct trace_event_functions kprobe_funcs = {
1463         .trace          = print_kprobe_event
1464 };
1465
1466 static inline void init_trace_event_call(struct trace_kprobe *tk)
1467 {
1468         struct trace_event_call *call = trace_probe_event_call(&tk->tp);
1469
1470         if (trace_kprobe_is_return(tk)) {
1471                 call->event.funcs = &kretprobe_funcs;
1472                 call->class->define_fields = kretprobe_event_define_fields;
1473         } else {
1474                 call->event.funcs = &kprobe_funcs;
1475                 call->class->define_fields = kprobe_event_define_fields;
1476         }
1477
1478         call->flags = TRACE_EVENT_FL_KPROBE;
1479         call->class->reg = kprobe_register;
1480 }
1481
1482 static int register_kprobe_event(struct trace_kprobe *tk)
1483 {
1484         init_trace_event_call(tk);
1485
1486         return trace_probe_register_event_call(&tk->tp);
1487 }
1488
1489 static int unregister_kprobe_event(struct trace_kprobe *tk)
1490 {
1491         return trace_probe_unregister_event_call(&tk->tp);
1492 }
1493
1494 #ifdef CONFIG_PERF_EVENTS
1495 /* create a trace_kprobe, but don't add it to global lists */
1496 struct trace_event_call *
1497 create_local_trace_kprobe(char *func, void *addr, unsigned long offs,
1498                           bool is_return)
1499 {
1500         struct trace_kprobe *tk;
1501         int ret;
1502         char *event;
1503
1504         /*
1505          * local trace_kprobes are not added to dyn_event, so they are never
1506          * searched in find_trace_kprobe(). Therefore, there is no concern of
1507          * duplicated name here.
1508          */
1509         event = func ? func : "DUMMY_EVENT";
1510
1511         tk = alloc_trace_kprobe(KPROBE_EVENT_SYSTEM, event, (void *)addr, func,
1512                                 offs, 0 /* maxactive */, 0 /* nargs */,
1513                                 is_return);
1514
1515         if (IS_ERR(tk)) {
1516                 pr_info("Failed to allocate trace_probe.(%d)\n",
1517                         (int)PTR_ERR(tk));
1518                 return ERR_CAST(tk);
1519         }
1520
1521         init_trace_event_call(tk);
1522
1523         if (traceprobe_set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0) {
1524                 ret = -ENOMEM;
1525                 goto error;
1526         }
1527
1528         ret = __register_trace_kprobe(tk);
1529         if (ret < 0)
1530                 goto error;
1531
1532         return trace_probe_event_call(&tk->tp);
1533 error:
1534         free_trace_kprobe(tk);
1535         return ERR_PTR(ret);
1536 }
1537
1538 void destroy_local_trace_kprobe(struct trace_event_call *event_call)
1539 {
1540         struct trace_kprobe *tk;
1541
1542         tk = trace_kprobe_primary_from_call(event_call);
1543         if (unlikely(!tk))
1544                 return;
1545
1546         if (trace_probe_is_enabled(&tk->tp)) {
1547                 WARN_ON(1);
1548                 return;
1549         }
1550
1551         __unregister_trace_kprobe(tk);
1552
1553         free_trace_kprobe(tk);
1554 }
1555 #endif /* CONFIG_PERF_EVENTS */
1556
1557 static __init void enable_boot_kprobe_events(void)
1558 {
1559         struct trace_array *tr = top_trace_array();
1560         struct trace_event_file *file;
1561         struct trace_kprobe *tk;
1562         struct dyn_event *pos;
1563
1564         mutex_lock(&event_mutex);
1565         for_each_trace_kprobe(tk, pos) {
1566                 list_for_each_entry(file, &tr->events, list)
1567                         if (file->event_call == trace_probe_event_call(&tk->tp))
1568                                 trace_event_enable_disable(file, 1, 0);
1569         }
1570         mutex_unlock(&event_mutex);
1571 }
1572
1573 static __init void setup_boot_kprobe_events(void)
1574 {
1575         char *p, *cmd = kprobe_boot_events_buf;
1576         int ret;
1577
1578         strreplace(kprobe_boot_events_buf, ',', ' ');
1579
1580         while (cmd && *cmd != '\0') {
1581                 p = strchr(cmd, ';');
1582                 if (p)
1583                         *p++ = '\0';
1584
1585                 ret = trace_run_command(cmd, create_or_delete_trace_kprobe);
1586                 if (ret)
1587                         pr_warn("Failed to add event(%d): %s\n", ret, cmd);
1588                 else
1589                         kprobe_boot_events_enabled = true;
1590
1591                 cmd = p;
1592         }
1593
1594         enable_boot_kprobe_events();
1595 }
1596
1597 /* Make a tracefs interface for controlling probe points */
1598 static __init int init_kprobe_trace(void)
1599 {
1600         struct dentry *d_tracer;
1601         struct dentry *entry;
1602         int ret;
1603
1604         ret = dyn_event_register(&trace_kprobe_ops);
1605         if (ret)
1606                 return ret;
1607
1608         if (register_module_notifier(&trace_kprobe_module_nb))
1609                 return -EINVAL;
1610
1611         d_tracer = tracing_init_dentry();
1612         if (IS_ERR(d_tracer))
1613                 return 0;
1614
1615         entry = tracefs_create_file("kprobe_events", 0644, d_tracer,
1616                                     NULL, &kprobe_events_ops);
1617
1618         /* Event list interface */
1619         if (!entry)
1620                 pr_warn("Could not create tracefs 'kprobe_events' entry\n");
1621
1622         /* Profile interface */
1623         entry = tracefs_create_file("kprobe_profile", 0444, d_tracer,
1624                                     NULL, &kprobe_profile_ops);
1625
1626         if (!entry)
1627                 pr_warn("Could not create tracefs 'kprobe_profile' entry\n");
1628
1629         setup_boot_kprobe_events();
1630
1631         return 0;
1632 }
1633 fs_initcall(init_kprobe_trace);
1634
1635
1636 #ifdef CONFIG_FTRACE_STARTUP_TEST
1637 static __init struct trace_event_file *
1638 find_trace_probe_file(struct trace_kprobe *tk, struct trace_array *tr)
1639 {
1640         struct trace_event_file *file;
1641
1642         list_for_each_entry(file, &tr->events, list)
1643                 if (file->event_call == trace_probe_event_call(&tk->tp))
1644                         return file;
1645
1646         return NULL;
1647 }
1648
1649 /*
1650  * Nobody but us can call enable_trace_kprobe/disable_trace_kprobe at this
1651  * stage, we can do this lockless.
1652  */
1653 static __init int kprobe_trace_self_tests_init(void)
1654 {
1655         int ret, warn = 0;
1656         int (*target)(int, int, int, int, int, int);
1657         struct trace_kprobe *tk;
1658         struct trace_event_file *file;
1659
1660         if (tracing_is_disabled())
1661                 return -ENODEV;
1662
1663         if (kprobe_boot_events_enabled) {
1664                 pr_info("Skipping kprobe tests due to kprobe_event on cmdline\n");
1665                 return 0;
1666         }
1667
1668         target = kprobe_trace_selftest_target;
1669
1670         pr_info("Testing kprobe tracing: ");
1671
1672         ret = trace_run_command("p:testprobe kprobe_trace_selftest_target $stack $stack0 +0($stack)",
1673                                 create_or_delete_trace_kprobe);
1674         if (WARN_ON_ONCE(ret)) {
1675                 pr_warn("error on probing function entry.\n");
1676                 warn++;
1677         } else {
1678                 /* Enable trace point */
1679                 tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
1680                 if (WARN_ON_ONCE(tk == NULL)) {
1681                         pr_warn("error on getting new probe.\n");
1682                         warn++;
1683                 } else {
1684                         file = find_trace_probe_file(tk, top_trace_array());
1685                         if (WARN_ON_ONCE(file == NULL)) {
1686                                 pr_warn("error on getting probe file.\n");
1687                                 warn++;
1688                         } else
1689                                 enable_trace_kprobe(
1690                                         trace_probe_event_call(&tk->tp), file);
1691                 }
1692         }
1693
1694         ret = trace_run_command("r:testprobe2 kprobe_trace_selftest_target $retval",
1695                                 create_or_delete_trace_kprobe);
1696         if (WARN_ON_ONCE(ret)) {
1697                 pr_warn("error on probing function return.\n");
1698                 warn++;
1699         } else {
1700                 /* Enable trace point */
1701                 tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
1702                 if (WARN_ON_ONCE(tk == NULL)) {
1703                         pr_warn("error on getting 2nd new probe.\n");
1704                         warn++;
1705                 } else {
1706                         file = find_trace_probe_file(tk, top_trace_array());
1707                         if (WARN_ON_ONCE(file == NULL)) {
1708                                 pr_warn("error on getting probe file.\n");
1709                                 warn++;
1710                         } else
1711                                 enable_trace_kprobe(
1712                                         trace_probe_event_call(&tk->tp), file);
1713                 }
1714         }
1715
1716         if (warn)
1717                 goto end;
1718
1719         ret = target(1, 2, 3, 4, 5, 6);
1720
1721         /*
1722          * Not expecting an error here, the check is only to prevent the
1723          * optimizer from removing the call to target() as otherwise there
1724          * are no side-effects and the call is never performed.
1725          */
1726         if (ret != 21)
1727                 warn++;
1728
1729         /* Disable trace points before removing it */
1730         tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
1731         if (WARN_ON_ONCE(tk == NULL)) {
1732                 pr_warn("error on getting test probe.\n");
1733                 warn++;
1734         } else {
1735                 if (trace_kprobe_nhit(tk) != 1) {
1736                         pr_warn("incorrect number of testprobe hits\n");
1737                         warn++;
1738                 }
1739
1740                 file = find_trace_probe_file(tk, top_trace_array());
1741                 if (WARN_ON_ONCE(file == NULL)) {
1742                         pr_warn("error on getting probe file.\n");
1743                         warn++;
1744                 } else
1745                         disable_trace_kprobe(
1746                                 trace_probe_event_call(&tk->tp), file);
1747         }
1748
1749         tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
1750         if (WARN_ON_ONCE(tk == NULL)) {
1751                 pr_warn("error on getting 2nd test probe.\n");
1752                 warn++;
1753         } else {
1754                 if (trace_kprobe_nhit(tk) != 1) {
1755                         pr_warn("incorrect number of testprobe2 hits\n");
1756                         warn++;
1757                 }
1758
1759                 file = find_trace_probe_file(tk, top_trace_array());
1760                 if (WARN_ON_ONCE(file == NULL)) {
1761                         pr_warn("error on getting probe file.\n");
1762                         warn++;
1763                 } else
1764                         disable_trace_kprobe(
1765                                 trace_probe_event_call(&tk->tp), file);
1766         }
1767
1768         ret = trace_run_command("-:testprobe", create_or_delete_trace_kprobe);
1769         if (WARN_ON_ONCE(ret)) {
1770                 pr_warn("error on deleting a probe.\n");
1771                 warn++;
1772         }
1773
1774         ret = trace_run_command("-:testprobe2", create_or_delete_trace_kprobe);
1775         if (WARN_ON_ONCE(ret)) {
1776                 pr_warn("error on deleting a probe.\n");
1777                 warn++;
1778         }
1779
1780 end:
1781         ret = dyn_events_release_all(&trace_kprobe_ops);
1782         if (WARN_ON_ONCE(ret)) {
1783                 pr_warn("error on cleaning up probes.\n");
1784                 warn++;
1785         }
1786         /*
1787          * Wait for the optimizer work to finish. Otherwise it might fiddle
1788          * with probes in already freed __init text.
1789          */
1790         wait_for_kprobe_optimizer();
1791         if (warn)
1792                 pr_cont("NG: Some tests are failed. Please check them.\n");
1793         else
1794                 pr_cont("OK\n");
1795         return 0;
1796 }
1797
1798 late_initcall(kprobe_trace_self_tests_init);
1799
1800 #endif