blob: f9a0cd094b8104d3587cf2b4c70f3295301701c7 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// 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 */
24struct 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
36static nokprobe_inline bool trace_kprobe_is_return(struct trace_kprobe *tk)
37{
38 return tk->rp.handler != NULL;
39}
40
41static nokprobe_inline const char *trace_kprobe_symbol(struct trace_kprobe *tk)
42{
43 return tk->symbol ? tk->symbol : "unknown";
44}
45
46static nokprobe_inline unsigned long trace_kprobe_offset(struct trace_kprobe *tk)
47{
48 return tk->rp.kp.offset;
49}
50
51static nokprobe_inline bool trace_kprobe_has_gone(struct trace_kprobe *tk)
52{
53 return !!(kprobe_gone(&tk->rp.kp));
54}
55
56static 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
64static 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
83static 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 */
95static nokprobe_inline
96unsigned 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
111bool 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);
118}
119
120bool 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
127static int register_kprobe_event(struct trace_kprobe *tk);
128static int unregister_kprobe_event(struct trace_kprobe *tk);
129
130static DEFINE_MUTEX(probe_lock);
131static LIST_HEAD(probe_list);
132
133static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
134static int kretprobe_dispatcher(struct kretprobe_instance *ri,
135 struct pt_regs *regs);
136
137/* Memory fetching by symbol */
138struct symbol_cache {
139 char *symbol;
140 long offset;
141 unsigned long addr;
142};
143
144unsigned 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
154void free_symbol_cache(struct symbol_cache *sc)
155{
156 kfree(sc->symbol);
157 kfree(sc);
158}
159
160struct 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) \
186static 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} \
192NOKPROBE_SYMBOL(FETCH_FUNC_NAME(stack, type));
193
194DEFINE_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) \
200static 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} \
209NOKPROBE_SYMBOL(FETCH_FUNC_NAME(memory, type));
210
211DEFINE_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 */
216static 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}
239NOKPROBE_SYMBOL(FETCH_FUNC_NAME(memory, string));
240
241/* Return the length of string -- including null terminal byte */
242static 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}
266NOKPROBE_SYMBOL(FETCH_FUNC_NAME(memory, string_size));
267
268#define DEFINE_FETCH_symbol(type) \
269void 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} \
277NOKPROBE_SYMBOL(FETCH_FUNC_NAME(symbol, type));
278
279DEFINE_BASIC_FETCH_FUNCS(symbol)
280DEFINE_FETCH_symbol(string)
281DEFINE_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 */
292static 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 */
318static 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;
375error:
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
383static 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
397static 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
409static 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 */
427static int
428enable_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 */
465static int
466disable_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_KPROBES_ON_FTRACE) && \
521 !defined(CONFIG_KPROBE_EVENTS_ON_NOTRACE)
522static bool within_notrace_func(struct trace_kprobe *tk)
523{
524 unsigned long offset, size, addr;
525
526 addr = trace_kprobe_address(tk);
527 if (!addr || !kallsyms_lookup_size_offset(addr, &size, &offset))
528 return false;
529
530 /* Get the entry address of the target function */
531 addr -= offset;
532
533 /*
534 * Since ftrace_location_range() does inclusive range check, we need
535 * to subtract 1 byte from the end address.
536 */
537 return !ftrace_location_range(addr, addr + size - 1);
538}
539#else
540#define within_notrace_func(tk) (false)
541#endif
542
543/* Internal register function - just handle k*probes and flags */
544static int __register_trace_kprobe(struct trace_kprobe *tk)
545{
546 int i, ret;
547
548 if (trace_probe_is_registered(&tk->tp))
549 return -EINVAL;
550
551 if (within_notrace_func(tk)) {
552 pr_warn("Could not probe notrace function %s\n",
553 trace_kprobe_symbol(tk));
554 return -EINVAL;
555 }
556
557 for (i = 0; i < tk->tp.nr_args; i++)
558 traceprobe_update_arg(&tk->tp.args[i]);
559
560 /* Set/clear disabled flag according to tp->flag */
561 if (trace_probe_is_enabled(&tk->tp))
562 tk->rp.kp.flags &= ~KPROBE_FLAG_DISABLED;
563 else
564 tk->rp.kp.flags |= KPROBE_FLAG_DISABLED;
565
566 if (trace_kprobe_is_return(tk))
567 ret = register_kretprobe(&tk->rp);
568 else
569 ret = register_kprobe(&tk->rp.kp);
570
571 if (ret == 0) {
572 tk->tp.flags |= TP_FLAG_REGISTERED;
573 } else if (ret == -EILSEQ) {
574 pr_warn("Probing address(0x%p) is not an instruction boundary.\n",
575 tk->rp.kp.addr);
576 ret = -EINVAL;
577 }
578 return ret;
579}
580
581/* Internal unregister function - just handle k*probes and flags */
582static void __unregister_trace_kprobe(struct trace_kprobe *tk)
583{
584 if (trace_probe_is_registered(&tk->tp)) {
585 if (trace_kprobe_is_return(tk))
586 unregister_kretprobe(&tk->rp);
587 else
588 unregister_kprobe(&tk->rp.kp);
589 tk->tp.flags &= ~TP_FLAG_REGISTERED;
590 /* Cleanup kprobe for reuse */
591 if (tk->rp.kp.symbol_name)
592 tk->rp.kp.addr = NULL;
593 }
594}
595
596/* Unregister a trace_probe and probe_event: call with locking probe_lock */
597static int unregister_trace_kprobe(struct trace_kprobe *tk)
598{
599 /* Enabled event can not be unregistered */
600 if (trace_probe_is_enabled(&tk->tp))
601 return -EBUSY;
602
603 /* Will fail if probe is being used by ftrace or perf */
604 if (unregister_kprobe_event(tk))
605 return -EBUSY;
606
607 __unregister_trace_kprobe(tk);
608 list_del(&tk->list);
609
610 return 0;
611}
612
613/* Register a trace_probe and probe_event */
614static int register_trace_kprobe(struct trace_kprobe *tk)
615{
616 struct trace_kprobe *old_tk;
617 int ret;
618
619 mutex_lock(&probe_lock);
620
621 /* Delete old (same name) event if exist */
622 old_tk = find_trace_kprobe(trace_event_name(&tk->tp.call),
623 tk->tp.call.class->system);
624 if (old_tk) {
625 ret = unregister_trace_kprobe(old_tk);
626 if (ret < 0)
627 goto end;
628 free_trace_kprobe(old_tk);
629 }
630
631 /* Register new event */
632 ret = register_kprobe_event(tk);
633 if (ret) {
634 pr_warn("Failed to register probe event(%d)\n", ret);
635 goto end;
636 }
637
638 /* Register k*probe */
639 ret = __register_trace_kprobe(tk);
640 if (ret == -ENOENT && !trace_kprobe_module_exist(tk)) {
641 pr_warn("This probe might be able to register after target module is loaded. Continue.\n");
642 ret = 0;
643 }
644
645 if (ret < 0)
646 unregister_kprobe_event(tk);
647 else
648 list_add_tail(&tk->list, &probe_list);
649
650end:
651 mutex_unlock(&probe_lock);
652 return ret;
653}
654
655/* Module notifier call back, checking event on the module */
656static int trace_kprobe_module_callback(struct notifier_block *nb,
657 unsigned long val, void *data)
658{
659 struct module *mod = data;
660 struct trace_kprobe *tk;
661 int ret;
662
663 if (val != MODULE_STATE_COMING)
664 return NOTIFY_DONE;
665
666 /* Update probes on coming module */
667 mutex_lock(&probe_lock);
668 list_for_each_entry(tk, &probe_list, list) {
669 if (trace_kprobe_within_module(tk, mod)) {
670 /* Don't need to check busy - this should have gone. */
671 __unregister_trace_kprobe(tk);
672 ret = __register_trace_kprobe(tk);
673 if (ret)
674 pr_warn("Failed to re-register probe %s on %s: %d\n",
675 trace_event_name(&tk->tp.call),
676 mod->name, ret);
677 }
678 }
679 mutex_unlock(&probe_lock);
680
681 return NOTIFY_DONE;
682}
683
684static struct notifier_block trace_kprobe_module_nb = {
685 .notifier_call = trace_kprobe_module_callback,
686 .priority = 1 /* Invoked after kprobe module callback */
687};
688
689/* Convert certain expected symbols into '_' when generating event names */
690static inline void sanitize_event_name(char *name)
691{
692 while (*name++ != '\0')
693 if (*name == ':' || *name == '.')
694 *name = '_';
695}
696
697static int create_trace_kprobe(int argc, char **argv)
698{
699 /*
700 * Argument syntax:
701 * - Add kprobe:
702 * p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS]
703 * - Add kretprobe:
704 * r[MAXACTIVE][:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS]
705 * Fetch args:
706 * $retval : fetch return value
707 * $stack : fetch stack address
708 * $stackN : fetch Nth of stack (N:0-)
709 * $comm : fetch current task comm
710 * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
711 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
712 * %REG : fetch register REG
713 * Dereferencing memory fetch:
714 * +|-offs(ARG) : fetch memory at ARG +|- offs address.
715 * Alias name of args:
716 * NAME=FETCHARG : set NAME as alias of FETCHARG.
717 * Type of args:
718 * FETCHARG:TYPE : use TYPE instead of unsigned long.
719 */
720 struct trace_kprobe *tk;
721 int i, ret = 0;
722 bool is_return = false, is_delete = false;
723 char *symbol = NULL, *event = NULL, *group = NULL;
724 int maxactive = 0;
725 char *arg;
726 long offset = 0;
727 void *addr = NULL;
728 char buf[MAX_EVENT_NAME_LEN];
729
730 /* argc must be >= 1 */
731 if (argv[0][0] == 'p')
732 is_return = false;
733 else if (argv[0][0] == 'r')
734 is_return = true;
735 else if (argv[0][0] == '-')
736 is_delete = true;
737 else {
738 pr_info("Probe definition must be started with 'p', 'r' or"
739 " '-'.\n");
740 return -EINVAL;
741 }
742
743 event = strchr(&argv[0][1], ':');
744 if (event) {
745 event[0] = '\0';
746 event++;
747 }
748 if (is_return && isdigit(argv[0][1])) {
749 ret = kstrtouint(&argv[0][1], 0, &maxactive);
750 if (ret) {
751 pr_info("Failed to parse maxactive.\n");
752 return ret;
753 }
754 /* kretprobes instances are iterated over via a list. The
755 * maximum should stay reasonable.
756 */
757 if (maxactive > KRETPROBE_MAXACTIVE_MAX) {
758 pr_info("Maxactive is too big (%d > %d).\n",
759 maxactive, KRETPROBE_MAXACTIVE_MAX);
760 return -E2BIG;
761 }
762 }
763
764 if (event) {
765 if (strchr(event, '/')) {
766 group = event;
767 event = strchr(group, '/') + 1;
768 event[-1] = '\0';
769 if (strlen(group) == 0) {
770 pr_info("Group name is not specified\n");
771 return -EINVAL;
772 }
773 }
774 if (strlen(event) == 0) {
775 pr_info("Event name is not specified\n");
776 return -EINVAL;
777 }
778 }
779 if (!group)
780 group = KPROBE_EVENT_SYSTEM;
781
782 if (is_delete) {
783 if (!event) {
784 pr_info("Delete command needs an event name.\n");
785 return -EINVAL;
786 }
787 mutex_lock(&probe_lock);
788 tk = find_trace_kprobe(event, group);
789 if (!tk) {
790 mutex_unlock(&probe_lock);
791 pr_info("Event %s/%s doesn't exist.\n", group, event);
792 return -ENOENT;
793 }
794 /* delete an event */
795 ret = unregister_trace_kprobe(tk);
796 if (ret == 0)
797 free_trace_kprobe(tk);
798 mutex_unlock(&probe_lock);
799 return ret;
800 }
801
802 if (argc < 2) {
803 pr_info("Probe point is not specified.\n");
804 return -EINVAL;
805 }
806
807 /* try to parse an address. if that fails, try to read the
808 * input as a symbol. */
809 if (kstrtoul(argv[1], 0, (unsigned long *)&addr)) {
810 /* a symbol specified */
811 symbol = argv[1];
812 /* TODO: support .init module functions */
813 ret = traceprobe_split_symbol_offset(symbol, &offset);
814 if (ret || offset < 0 || offset > UINT_MAX) {
815 pr_info("Failed to parse either an address or a symbol.\n");
816 return ret;
817 }
818 if (offset && is_return &&
819 !kprobe_on_func_entry(NULL, symbol, offset)) {
820 pr_info("Given offset is not valid for return probe.\n");
821 return -EINVAL;
822 }
823 }
824 argc -= 2; argv += 2;
825
826 /* setup a probe */
827 if (!event) {
828 /* Make a new event name */
829 if (symbol)
830 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
831 is_return ? 'r' : 'p', symbol, offset);
832 else
833 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
834 is_return ? 'r' : 'p', addr);
835 sanitize_event_name(buf);
836 event = buf;
837 }
838 tk = alloc_trace_kprobe(group, event, addr, symbol, offset, maxactive,
839 argc, is_return);
840 if (IS_ERR(tk)) {
841 pr_info("Failed to allocate trace_probe.(%d)\n",
842 (int)PTR_ERR(tk));
843 return PTR_ERR(tk);
844 }
845
846 /* parse arguments */
847 ret = 0;
848 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
849 struct probe_arg *parg = &tk->tp.args[i];
850
851 /* Increment count for freeing args in error case */
852 tk->tp.nr_args++;
853
854 /* Parse argument name */
855 arg = strchr(argv[i], '=');
856 if (arg) {
857 *arg++ = '\0';
858 parg->name = kstrdup(argv[i], GFP_KERNEL);
859 } else {
860 arg = argv[i];
861 /* If argument name is omitted, set "argN" */
862 snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
863 parg->name = kstrdup(buf, GFP_KERNEL);
864 }
865
866 if (!parg->name) {
867 pr_info("Failed to allocate argument[%d] name.\n", i);
868 ret = -ENOMEM;
869 goto error;
870 }
871
872 if (!is_good_name(parg->name)) {
873 pr_info("Invalid argument[%d] name: %s\n",
874 i, parg->name);
875 ret = -EINVAL;
876 goto error;
877 }
878
879 if (traceprobe_conflict_field_name(parg->name,
880 tk->tp.args, i)) {
881 pr_info("Argument[%d] name '%s' conflicts with "
882 "another field.\n", i, argv[i]);
883 ret = -EINVAL;
884 goto error;
885 }
886
887 /* Parse fetch argument */
888 ret = traceprobe_parse_probe_arg(arg, &tk->tp.size, parg,
889 is_return, true,
890 kprobes_fetch_type_table);
891 if (ret) {
892 pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
893 goto error;
894 }
895 }
896
897 ret = register_trace_kprobe(tk);
898 if (ret)
899 goto error;
900 return 0;
901
902error:
903 free_trace_kprobe(tk);
904 return ret;
905}
906
907static int release_all_trace_kprobes(void)
908{
909 struct trace_kprobe *tk;
910 int ret = 0;
911
912 mutex_lock(&probe_lock);
913 /* Ensure no probe is in use. */
914 list_for_each_entry(tk, &probe_list, list)
915 if (trace_probe_is_enabled(&tk->tp)) {
916 ret = -EBUSY;
917 goto end;
918 }
919 /* TODO: Use batch unregistration */
920 while (!list_empty(&probe_list)) {
921 tk = list_entry(probe_list.next, struct trace_kprobe, list);
922 ret = unregister_trace_kprobe(tk);
923 if (ret)
924 goto end;
925 free_trace_kprobe(tk);
926 }
927
928end:
929 mutex_unlock(&probe_lock);
930
931 return ret;
932}
933
934/* Probes listing interfaces */
935static void *probes_seq_start(struct seq_file *m, loff_t *pos)
936{
937 mutex_lock(&probe_lock);
938 return seq_list_start(&probe_list, *pos);
939}
940
941static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
942{
943 return seq_list_next(v, &probe_list, pos);
944}
945
946static void probes_seq_stop(struct seq_file *m, void *v)
947{
948 mutex_unlock(&probe_lock);
949}
950
951static int probes_seq_show(struct seq_file *m, void *v)
952{
953 struct trace_kprobe *tk = v;
954 int i;
955
956 seq_putc(m, trace_kprobe_is_return(tk) ? 'r' : 'p');
957 seq_printf(m, ":%s/%s", tk->tp.call.class->system,
958 trace_event_name(&tk->tp.call));
959
960 if (!tk->symbol)
961 seq_printf(m, " 0x%p", tk->rp.kp.addr);
962 else if (tk->rp.kp.offset)
963 seq_printf(m, " %s+%u", trace_kprobe_symbol(tk),
964 tk->rp.kp.offset);
965 else
966 seq_printf(m, " %s", trace_kprobe_symbol(tk));
967
968 for (i = 0; i < tk->tp.nr_args; i++)
969 seq_printf(m, " %s=%s", tk->tp.args[i].name, tk->tp.args[i].comm);
970 seq_putc(m, '\n');
971
972 return 0;
973}
974
975static const struct seq_operations probes_seq_op = {
976 .start = probes_seq_start,
977 .next = probes_seq_next,
978 .stop = probes_seq_stop,
979 .show = probes_seq_show
980};
981
982static int probes_open(struct inode *inode, struct file *file)
983{
984 int ret;
985
986 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
987 ret = release_all_trace_kprobes();
988 if (ret < 0)
989 return ret;
990 }
991
992 return seq_open(file, &probes_seq_op);
993}
994
995static ssize_t probes_write(struct file *file, const char __user *buffer,
996 size_t count, loff_t *ppos)
997{
998 return trace_parse_run_command(file, buffer, count, ppos,
999 create_trace_kprobe);
1000}
1001
1002static const struct file_operations kprobe_events_ops = {
1003 .owner = THIS_MODULE,
1004 .open = probes_open,
1005 .read = seq_read,
1006 .llseek = seq_lseek,
1007 .release = seq_release,
1008 .write = probes_write,
1009};
1010
1011/* Probes profiling interfaces */
1012static int probes_profile_seq_show(struct seq_file *m, void *v)
1013{
1014 struct trace_kprobe *tk = v;
1015
1016 seq_printf(m, " %-44s %15lu %15lu\n",
1017 trace_event_name(&tk->tp.call),
1018 trace_kprobe_nhit(tk),
1019 tk->rp.kp.nmissed);
1020
1021 return 0;
1022}
1023
1024static const struct seq_operations profile_seq_op = {
1025 .start = probes_seq_start,
1026 .next = probes_seq_next,
1027 .stop = probes_seq_stop,
1028 .show = probes_profile_seq_show
1029};
1030
1031static int profile_open(struct inode *inode, struct file *file)
1032{
1033 return seq_open(file, &profile_seq_op);
1034}
1035
1036static const struct file_operations kprobe_profile_ops = {
1037 .owner = THIS_MODULE,
1038 .open = profile_open,
1039 .read = seq_read,
1040 .llseek = seq_lseek,
1041 .release = seq_release,
1042};
1043
1044/* Kprobe handler */
1045static nokprobe_inline void
1046__kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs,
1047 struct trace_event_file *trace_file)
1048{
1049 struct kprobe_trace_entry_head *entry;
1050 struct ring_buffer_event *event;
1051 struct ring_buffer *buffer;
1052 int size, dsize, pc;
1053 unsigned long irq_flags;
1054 struct trace_event_call *call = &tk->tp.call;
1055
1056 WARN_ON(call != trace_file->event_call);
1057
1058 if (trace_trigger_soft_disabled(trace_file))
1059 return;
1060
1061 local_save_flags(irq_flags);
1062 pc = preempt_count();
1063
1064 dsize = __get_data_size(&tk->tp, regs);
1065 size = sizeof(*entry) + tk->tp.size + dsize;
1066
1067 event = trace_event_buffer_lock_reserve(&buffer, trace_file,
1068 call->event.type,
1069 size, irq_flags, pc);
1070 if (!event)
1071 return;
1072
1073 entry = ring_buffer_event_data(event);
1074 entry->ip = (unsigned long)tk->rp.kp.addr;
1075 store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
1076
1077 event_trigger_unlock_commit_regs(trace_file, buffer, event,
1078 entry, irq_flags, pc, regs);
1079}
1080
1081static void
1082kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs)
1083{
1084 struct event_file_link *link;
1085
1086 list_for_each_entry_rcu(link, &tk->tp.files, list)
1087 __kprobe_trace_func(tk, regs, link->file);
1088}
1089NOKPROBE_SYMBOL(kprobe_trace_func);
1090
1091/* Kretprobe handler */
1092static nokprobe_inline void
1093__kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
1094 struct pt_regs *regs,
1095 struct trace_event_file *trace_file)
1096{
1097 struct kretprobe_trace_entry_head *entry;
1098 struct ring_buffer_event *event;
1099 struct ring_buffer *buffer;
1100 int size, pc, dsize;
1101 unsigned long irq_flags;
1102 struct trace_event_call *call = &tk->tp.call;
1103
1104 WARN_ON(call != trace_file->event_call);
1105
1106 if (trace_trigger_soft_disabled(trace_file))
1107 return;
1108
1109 local_save_flags(irq_flags);
1110 pc = preempt_count();
1111
1112 dsize = __get_data_size(&tk->tp, regs);
1113 size = sizeof(*entry) + tk->tp.size + dsize;
1114
1115 event = trace_event_buffer_lock_reserve(&buffer, trace_file,
1116 call->event.type,
1117 size, irq_flags, pc);
1118 if (!event)
1119 return;
1120
1121 entry = ring_buffer_event_data(event);
1122 entry->func = (unsigned long)tk->rp.kp.addr;
1123 entry->ret_ip = (unsigned long)ri->ret_addr;
1124 store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
1125
1126 event_trigger_unlock_commit_regs(trace_file, buffer, event,
1127 entry, irq_flags, pc, regs);
1128}
1129
1130static void
1131kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
1132 struct pt_regs *regs)
1133{
1134 struct event_file_link *link;
1135
1136 list_for_each_entry_rcu(link, &tk->tp.files, list)
1137 __kretprobe_trace_func(tk, ri, regs, link->file);
1138}
1139NOKPROBE_SYMBOL(kretprobe_trace_func);
1140
1141/* Event entry printers */
1142static enum print_line_t
1143print_kprobe_event(struct trace_iterator *iter, int flags,
1144 struct trace_event *event)
1145{
1146 struct kprobe_trace_entry_head *field;
1147 struct trace_seq *s = &iter->seq;
1148 struct trace_probe *tp;
1149 u8 *data;
1150 int i;
1151
1152 field = (struct kprobe_trace_entry_head *)iter->ent;
1153 tp = container_of(event, struct trace_probe, call.event);
1154
1155 trace_seq_printf(s, "%s: (", trace_event_name(&tp->call));
1156
1157 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
1158 goto out;
1159
1160 trace_seq_putc(s, ')');
1161
1162 data = (u8 *)&field[1];
1163 for (i = 0; i < tp->nr_args; i++)
1164 if (!tp->args[i].type->print(s, tp->args[i].name,
1165 data + tp->args[i].offset, field))
1166 goto out;
1167
1168 trace_seq_putc(s, '\n');
1169 out:
1170 return trace_handle_return(s);
1171}
1172
1173static enum print_line_t
1174print_kretprobe_event(struct trace_iterator *iter, int flags,
1175 struct trace_event *event)
1176{
1177 struct kretprobe_trace_entry_head *field;
1178 struct trace_seq *s = &iter->seq;
1179 struct trace_probe *tp;
1180 u8 *data;
1181 int i;
1182
1183 field = (struct kretprobe_trace_entry_head *)iter->ent;
1184 tp = container_of(event, struct trace_probe, call.event);
1185
1186 trace_seq_printf(s, "%s: (", trace_event_name(&tp->call));
1187
1188 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
1189 goto out;
1190
1191 trace_seq_puts(s, " <- ");
1192
1193 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
1194 goto out;
1195
1196 trace_seq_putc(s, ')');
1197
1198 data = (u8 *)&field[1];
1199 for (i = 0; i < tp->nr_args; i++)
1200 if (!tp->args[i].type->print(s, tp->args[i].name,
1201 data + tp->args[i].offset, field))
1202 goto out;
1203
1204 trace_seq_putc(s, '\n');
1205
1206 out:
1207 return trace_handle_return(s);
1208}
1209
1210
1211static int kprobe_event_define_fields(struct trace_event_call *event_call)
1212{
1213 int ret, i;
1214 struct kprobe_trace_entry_head field;
1215 struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data;
1216
1217 DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
1218 /* Set argument names as fields */
1219 for (i = 0; i < tk->tp.nr_args; i++) {
1220 struct probe_arg *parg = &tk->tp.args[i];
1221
1222 ret = trace_define_field(event_call, parg->type->fmttype,
1223 parg->name,
1224 sizeof(field) + parg->offset,
1225 parg->type->size,
1226 parg->type->is_signed,
1227 FILTER_OTHER);
1228 if (ret)
1229 return ret;
1230 }
1231 return 0;
1232}
1233
1234static int kretprobe_event_define_fields(struct trace_event_call *event_call)
1235{
1236 int ret, i;
1237 struct kretprobe_trace_entry_head field;
1238 struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data;
1239
1240 DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
1241 DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 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#ifdef CONFIG_PERF_EVENTS
1259
1260/* Kprobe profile handler */
1261static int
1262kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs)
1263{
1264 struct trace_event_call *call = &tk->tp.call;
1265 struct kprobe_trace_entry_head *entry;
1266 struct hlist_head *head;
1267 int size, __size, dsize;
1268 int rctx;
1269
1270 if (bpf_prog_array_valid(call)) {
1271 unsigned long orig_ip = instruction_pointer(regs);
1272 int ret;
1273
1274 ret = trace_call_bpf(call, regs);
1275
1276 /*
1277 * We need to check and see if we modified the pc of the
1278 * pt_regs, and if so return 1 so that we don't do the
1279 * single stepping.
1280 */
1281 if (orig_ip != instruction_pointer(regs))
1282 return 1;
1283 if (!ret)
1284 return 0;
1285 }
1286
1287 head = this_cpu_ptr(call->perf_events);
1288 if (hlist_empty(head))
1289 return 0;
1290
1291 dsize = __get_data_size(&tk->tp, regs);
1292 __size = sizeof(*entry) + tk->tp.size + dsize;
1293 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1294 size -= sizeof(u32);
1295
1296 entry = perf_trace_buf_alloc(size, NULL, &rctx);
1297 if (!entry)
1298 return 0;
1299
1300 entry->ip = (unsigned long)tk->rp.kp.addr;
1301 memset(&entry[1], 0, dsize);
1302 store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
1303 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1304 head, NULL);
1305 return 0;
1306}
1307NOKPROBE_SYMBOL(kprobe_perf_func);
1308
1309/* Kretprobe profile handler */
1310static void
1311kretprobe_perf_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
1312 struct pt_regs *regs)
1313{
1314 struct trace_event_call *call = &tk->tp.call;
1315 struct kretprobe_trace_entry_head *entry;
1316 struct hlist_head *head;
1317 int size, __size, dsize;
1318 int rctx;
1319
1320 if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs))
1321 return;
1322
1323 head = this_cpu_ptr(call->perf_events);
1324 if (hlist_empty(head))
1325 return;
1326
1327 dsize = __get_data_size(&tk->tp, regs);
1328 __size = sizeof(*entry) + tk->tp.size + dsize;
1329 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1330 size -= sizeof(u32);
1331
1332 entry = perf_trace_buf_alloc(size, NULL, &rctx);
1333 if (!entry)
1334 return;
1335
1336 entry->func = (unsigned long)tk->rp.kp.addr;
1337 entry->ret_ip = (unsigned long)ri->ret_addr;
1338 store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
1339 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1340 head, NULL);
1341}
1342NOKPROBE_SYMBOL(kretprobe_perf_func);
1343
1344int bpf_get_kprobe_info(const struct perf_event *event, u32 *fd_type,
1345 const char **symbol, u64 *probe_offset,
1346 u64 *probe_addr, bool perf_type_tracepoint)
1347{
1348 const char *pevent = trace_event_name(event->tp_event);
1349 const char *group = event->tp_event->class->system;
1350 struct trace_kprobe *tk;
1351
1352 if (perf_type_tracepoint)
1353 tk = find_trace_kprobe(pevent, group);
1354 else
1355 tk = event->tp_event->data;
1356 if (!tk)
1357 return -EINVAL;
1358
1359 *fd_type = trace_kprobe_is_return(tk) ? BPF_FD_TYPE_KRETPROBE
1360 : BPF_FD_TYPE_KPROBE;
1361 if (tk->symbol) {
1362 *symbol = tk->symbol;
1363 *probe_offset = tk->rp.kp.offset;
1364 *probe_addr = 0;
1365 } else {
1366 *symbol = NULL;
1367 *probe_offset = 0;
1368 *probe_addr = (unsigned long)tk->rp.kp.addr;
1369 }
1370 return 0;
1371}
1372#endif /* CONFIG_PERF_EVENTS */
1373
1374/*
1375 * called by perf_trace_init() or __ftrace_set_clr_event() under event_mutex.
1376 *
1377 * kprobe_trace_self_tests_init() does enable_trace_probe/disable_trace_probe
1378 * lockless, but we can't race with this __init function.
1379 */
1380static int kprobe_register(struct trace_event_call *event,
1381 enum trace_reg type, void *data)
1382{
1383 struct trace_kprobe *tk = (struct trace_kprobe *)event->data;
1384 struct trace_event_file *file = data;
1385
1386 switch (type) {
1387 case TRACE_REG_REGISTER:
1388 return enable_trace_kprobe(tk, file);
1389 case TRACE_REG_UNREGISTER:
1390 return disable_trace_kprobe(tk, file);
1391
1392#ifdef CONFIG_PERF_EVENTS
1393 case TRACE_REG_PERF_REGISTER:
1394 return enable_trace_kprobe(tk, NULL);
1395 case TRACE_REG_PERF_UNREGISTER:
1396 return disable_trace_kprobe(tk, NULL);
1397 case TRACE_REG_PERF_OPEN:
1398 case TRACE_REG_PERF_CLOSE:
1399 case TRACE_REG_PERF_ADD:
1400 case TRACE_REG_PERF_DEL:
1401 return 0;
1402#endif
1403 }
1404 return 0;
1405}
1406
1407static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
1408{
1409 struct trace_kprobe *tk = container_of(kp, struct trace_kprobe, rp.kp);
1410 int ret = 0;
1411
1412 raw_cpu_inc(*tk->nhit);
1413
1414 if (tk->tp.flags & TP_FLAG_TRACE)
1415 kprobe_trace_func(tk, regs);
1416#ifdef CONFIG_PERF_EVENTS
1417 if (tk->tp.flags & TP_FLAG_PROFILE)
1418 ret = kprobe_perf_func(tk, regs);
1419#endif
1420 return ret;
1421}
1422NOKPROBE_SYMBOL(kprobe_dispatcher);
1423
1424static int
1425kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
1426{
1427 struct trace_kprobe *tk = container_of(ri->rp, struct trace_kprobe, rp);
1428
1429 raw_cpu_inc(*tk->nhit);
1430
1431 if (tk->tp.flags & TP_FLAG_TRACE)
1432 kretprobe_trace_func(tk, ri, regs);
1433#ifdef CONFIG_PERF_EVENTS
1434 if (tk->tp.flags & TP_FLAG_PROFILE)
1435 kretprobe_perf_func(tk, ri, regs);
1436#endif
1437 return 0; /* We don't tweek kernel, so just return 0 */
1438}
1439NOKPROBE_SYMBOL(kretprobe_dispatcher);
1440
1441static struct trace_event_functions kretprobe_funcs = {
1442 .trace = print_kretprobe_event
1443};
1444
1445static struct trace_event_functions kprobe_funcs = {
1446 .trace = print_kprobe_event
1447};
1448
1449static inline void init_trace_event_call(struct trace_kprobe *tk,
1450 struct trace_event_call *call)
1451{
1452 INIT_LIST_HEAD(&call->class->fields);
1453 if (trace_kprobe_is_return(tk)) {
1454 call->event.funcs = &kretprobe_funcs;
1455 call->class->define_fields = kretprobe_event_define_fields;
1456 } else {
1457 call->event.funcs = &kprobe_funcs;
1458 call->class->define_fields = kprobe_event_define_fields;
1459 }
1460
1461 call->flags = TRACE_EVENT_FL_KPROBE;
1462 call->class->reg = kprobe_register;
1463 call->data = tk;
1464}
1465
1466static int register_kprobe_event(struct trace_kprobe *tk)
1467{
1468 struct trace_event_call *call = &tk->tp.call;
1469 int ret = 0;
1470
1471 init_trace_event_call(tk, call);
1472
1473 if (set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0)
1474 return -ENOMEM;
1475 ret = register_trace_event(&call->event);
1476 if (!ret) {
1477 kfree(call->print_fmt);
1478 return -ENODEV;
1479 }
1480 ret = trace_add_event_call(call);
1481 if (ret) {
1482 pr_info("Failed to register kprobe event: %s\n",
1483 trace_event_name(call));
1484 kfree(call->print_fmt);
1485 unregister_trace_event(&call->event);
1486 }
1487 return ret;
1488}
1489
1490static int unregister_kprobe_event(struct trace_kprobe *tk)
1491{
1492 int ret;
1493
1494 /* tp->event is unregistered in trace_remove_event_call() */
1495 ret = trace_remove_event_call(&tk->tp.call);
1496 if (!ret)
1497 kfree(tk->tp.call.print_fmt);
1498 return ret;
1499}
1500
1501#ifdef CONFIG_PERF_EVENTS
1502/* create a trace_kprobe, but don't add it to global lists */
1503struct trace_event_call *
1504create_local_trace_kprobe(char *func, void *addr, unsigned long offs,
1505 bool is_return)
1506{
1507 struct trace_kprobe *tk;
1508 int ret;
1509 char *event;
1510
1511 /*
1512 * local trace_kprobes are not added to probe_list, so they are never
1513 * searched in find_trace_kprobe(). Therefore, there is no concern of
1514 * duplicated name here.
1515 */
1516 event = func ? func : "DUMMY_EVENT";
1517
1518 tk = alloc_trace_kprobe(KPROBE_EVENT_SYSTEM, event, (void *)addr, func,
1519 offs, 0 /* maxactive */, 0 /* nargs */,
1520 is_return);
1521
1522 if (IS_ERR(tk)) {
1523 pr_info("Failed to allocate trace_probe.(%d)\n",
1524 (int)PTR_ERR(tk));
1525 return ERR_CAST(tk);
1526 }
1527
1528 init_trace_event_call(tk, &tk->tp.call);
1529
1530 if (set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0) {
1531 ret = -ENOMEM;
1532 goto error;
1533 }
1534
1535 ret = __register_trace_kprobe(tk);
1536 if (ret < 0) {
1537 kfree(tk->tp.call.print_fmt);
1538 goto error;
1539 }
1540
1541 return &tk->tp.call;
1542error:
1543 free_trace_kprobe(tk);
1544 return ERR_PTR(ret);
1545}
1546
1547void destroy_local_trace_kprobe(struct trace_event_call *event_call)
1548{
1549 struct trace_kprobe *tk;
1550
1551 tk = container_of(event_call, struct trace_kprobe, tp.call);
1552
1553 if (trace_probe_is_enabled(&tk->tp)) {
1554 WARN_ON(1);
1555 return;
1556 }
1557
1558 __unregister_trace_kprobe(tk);
1559
1560 kfree(tk->tp.call.print_fmt);
1561 free_trace_kprobe(tk);
1562}
1563#endif /* CONFIG_PERF_EVENTS */
1564
1565/* Make a tracefs interface for controlling probe points */
1566static __init int init_kprobe_trace(void)
1567{
1568 struct dentry *d_tracer;
1569 struct dentry *entry;
1570
1571 if (register_module_notifier(&trace_kprobe_module_nb))
1572 return -EINVAL;
1573
1574 d_tracer = tracing_init_dentry();
1575 if (IS_ERR(d_tracer))
1576 return 0;
1577
1578 entry = tracefs_create_file("kprobe_events", 0644, d_tracer,
1579 NULL, &kprobe_events_ops);
1580
1581 /* Event list interface */
1582 if (!entry)
1583 pr_warn("Could not create tracefs 'kprobe_events' entry\n");
1584
1585 /* Profile interface */
1586 entry = tracefs_create_file("kprobe_profile", 0444, d_tracer,
1587 NULL, &kprobe_profile_ops);
1588
1589 if (!entry)
1590 pr_warn("Could not create tracefs 'kprobe_profile' entry\n");
1591 return 0;
1592}
1593fs_initcall(init_kprobe_trace);
1594
1595
1596#ifdef CONFIG_FTRACE_STARTUP_TEST
1597static __init struct trace_event_file *
1598find_trace_probe_file(struct trace_kprobe *tk, struct trace_array *tr)
1599{
1600 struct trace_event_file *file;
1601
1602 list_for_each_entry(file, &tr->events, list)
1603 if (file->event_call == &tk->tp.call)
1604 return file;
1605
1606 return NULL;
1607}
1608
1609/*
1610 * Nobody but us can call enable_trace_kprobe/disable_trace_kprobe at this
1611 * stage, we can do this lockless.
1612 */
1613static __init int kprobe_trace_self_tests_init(void)
1614{
1615 int ret, warn = 0;
1616 int (*target)(int, int, int, int, int, int);
1617 struct trace_kprobe *tk;
1618 struct trace_event_file *file;
1619
1620 if (tracing_is_disabled())
1621 return -ENODEV;
1622
1623 target = kprobe_trace_selftest_target;
1624
1625 pr_info("Testing kprobe tracing: ");
1626
1627 ret = trace_run_command("p:testprobe kprobe_trace_selftest_target "
1628 "$stack $stack0 +0($stack)",
1629 create_trace_kprobe);
1630 if (WARN_ON_ONCE(ret)) {
1631 pr_warn("error on probing function entry.\n");
1632 warn++;
1633 } else {
1634 /* Enable trace point */
1635 tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
1636 if (WARN_ON_ONCE(tk == NULL)) {
1637 pr_warn("error on getting new probe.\n");
1638 warn++;
1639 } else {
1640 file = find_trace_probe_file(tk, top_trace_array());
1641 if (WARN_ON_ONCE(file == NULL)) {
1642 pr_warn("error on getting probe file.\n");
1643 warn++;
1644 } else
1645 enable_trace_kprobe(tk, file);
1646 }
1647 }
1648
1649 ret = trace_run_command("r:testprobe2 kprobe_trace_selftest_target "
1650 "$retval", create_trace_kprobe);
1651 if (WARN_ON_ONCE(ret)) {
1652 pr_warn("error on probing function return.\n");
1653 warn++;
1654 } else {
1655 /* Enable trace point */
1656 tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
1657 if (WARN_ON_ONCE(tk == NULL)) {
1658 pr_warn("error on getting 2nd new probe.\n");
1659 warn++;
1660 } else {
1661 file = find_trace_probe_file(tk, top_trace_array());
1662 if (WARN_ON_ONCE(file == NULL)) {
1663 pr_warn("error on getting probe file.\n");
1664 warn++;
1665 } else
1666 enable_trace_kprobe(tk, file);
1667 }
1668 }
1669
1670 if (warn)
1671 goto end;
1672
1673 ret = target(1, 2, 3, 4, 5, 6);
1674
1675 /*
1676 * Not expecting an error here, the check is only to prevent the
1677 * optimizer from removing the call to target() as otherwise there
1678 * are no side-effects and the call is never performed.
1679 */
1680 if (ret != 21)
1681 warn++;
1682
1683 /* Disable trace points before removing it */
1684 tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
1685 if (WARN_ON_ONCE(tk == NULL)) {
1686 pr_warn("error on getting test probe.\n");
1687 warn++;
1688 } else {
1689 if (trace_kprobe_nhit(tk) != 1) {
1690 pr_warn("incorrect number of testprobe hits\n");
1691 warn++;
1692 }
1693
1694 file = find_trace_probe_file(tk, top_trace_array());
1695 if (WARN_ON_ONCE(file == NULL)) {
1696 pr_warn("error on getting probe file.\n");
1697 warn++;
1698 } else
1699 disable_trace_kprobe(tk, file);
1700 }
1701
1702 tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
1703 if (WARN_ON_ONCE(tk == NULL)) {
1704 pr_warn("error on getting 2nd test probe.\n");
1705 warn++;
1706 } else {
1707 if (trace_kprobe_nhit(tk) != 1) {
1708 pr_warn("incorrect number of testprobe2 hits\n");
1709 warn++;
1710 }
1711
1712 file = find_trace_probe_file(tk, top_trace_array());
1713 if (WARN_ON_ONCE(file == NULL)) {
1714 pr_warn("error on getting probe file.\n");
1715 warn++;
1716 } else
1717 disable_trace_kprobe(tk, file);
1718 }
1719
1720 ret = trace_run_command("-:testprobe", create_trace_kprobe);
1721 if (WARN_ON_ONCE(ret)) {
1722 pr_warn("error on deleting a probe.\n");
1723 warn++;
1724 }
1725
1726 ret = trace_run_command("-:testprobe2", create_trace_kprobe);
1727 if (WARN_ON_ONCE(ret)) {
1728 pr_warn("error on deleting a probe.\n");
1729 warn++;
1730 }
1731
1732end:
1733 release_all_trace_kprobes();
1734 /*
1735 * Wait for the optimizer work to finish. Otherwise it might fiddle
1736 * with probes in already freed __init text.
1737 */
1738 wait_for_kprobe_optimizer();
1739 if (warn)
1740 pr_cont("NG: Some tests are failed. Please check them.\n");
1741 else
1742 pr_cont("OK\n");
1743 return 0;
1744}
1745
1746late_initcall(kprobe_trace_self_tests_init);
1747
1748#endif