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