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