1 // SPDX-License-Identifier: GPL-2.0
3 * Kprobes-based tracing events
5 * Created by Masami Hiramatsu <mhiramat@redhat.com>
8 #define pr_fmt(fmt) "trace_kprobe: " fmt
10 #include <linux/module.h>
11 #include <linux/uaccess.h>
12 #include <linux/rculist.h>
13 #include <linux/error-injection.h>
15 #include "trace_dynevent.h"
16 #include "trace_kprobe_selftest.h"
17 #include "trace_probe.h"
18 #include "trace_probe_tmpl.h"
20 #define KPROBE_EVENT_SYSTEM "kprobes"
21 #define KRETPROBE_MAXACTIVE_MAX 4096
23 static int trace_kprobe_create(int argc, const char **argv);
24 static int trace_kprobe_show(struct seq_file *m, struct dyn_event *ev);
25 static int trace_kprobe_release(struct dyn_event *ev);
26 static bool trace_kprobe_is_busy(struct dyn_event *ev);
27 static bool trace_kprobe_match(const char *system, const char *event,
28 struct dyn_event *ev);
30 static 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,
39 * Kprobe event core functions
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;
49 static bool is_trace_kprobe(struct dyn_event *ev)
51 return ev->ops == &trace_kprobe_ops;
54 static struct trace_kprobe *to_trace_kprobe(struct dyn_event *ev)
56 return container_of(ev, struct trace_kprobe, devent);
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
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)))
68 #define SIZEOF_TRACE_KPROBE(n) \
69 (offsetof(struct trace_kprobe, tp.args) + \
70 (sizeof(struct probe_arg) * (n)))
72 static nokprobe_inline bool trace_kprobe_is_return(struct trace_kprobe *tk)
74 return tk->rp.handler != NULL;
77 static nokprobe_inline const char *trace_kprobe_symbol(struct trace_kprobe *tk)
79 return tk->symbol ? tk->symbol : "unknown";
82 static nokprobe_inline unsigned long trace_kprobe_offset(struct trace_kprobe *tk)
84 return tk->rp.kp.offset;
87 static nokprobe_inline bool trace_kprobe_has_gone(struct trace_kprobe *tk)
89 return !!(kprobe_gone(&tk->rp.kp));
92 static nokprobe_inline bool trace_kprobe_within_module(struct trace_kprobe *tk,
95 int len = strlen(mod->name);
96 const char *name = trace_kprobe_symbol(tk);
97 return strncmp(mod->name, name, len) == 0 && name[len] == ':';
100 static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprobe *tk)
107 p = strchr(tk->symbol, ':');
111 mutex_lock(&module_mutex);
112 ret = !!find_module(tk->symbol);
113 mutex_unlock(&module_mutex);
119 static bool trace_kprobe_is_busy(struct dyn_event *ev)
121 struct trace_kprobe *tk = to_trace_kprobe(ev);
123 return trace_probe_is_enabled(&tk->tp);
126 static bool trace_kprobe_match(const char *system, const char *event,
127 struct dyn_event *ev)
129 struct trace_kprobe *tk = to_trace_kprobe(ev);
131 return strcmp(trace_event_name(&tk->tp.call), event) == 0 &&
132 (!system || strcmp(tk->tp.call.class->system, system) == 0);
135 static nokprobe_inline unsigned long trace_kprobe_nhit(struct trace_kprobe *tk)
137 unsigned long nhit = 0;
140 for_each_possible_cpu(cpu)
141 nhit += *per_cpu_ptr(tk->nhit, cpu);
146 /* Return 0 if it fails to find the symbol address */
147 static nokprobe_inline
148 unsigned long trace_kprobe_address(struct trace_kprobe *tk)
153 addr = (unsigned long)
154 kallsyms_lookup_name(trace_kprobe_symbol(tk));
156 addr += tk->rp.kp.offset;
158 addr = (unsigned long)tk->rp.kp.addr;
163 bool trace_kprobe_on_func_entry(struct trace_event_call *call)
165 struct trace_kprobe *tk = (struct trace_kprobe *)call->data;
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);
172 bool trace_kprobe_error_injectable(struct trace_event_call *call)
174 struct trace_kprobe *tk = (struct trace_kprobe *)call->data;
176 return within_error_injection_list(trace_kprobe_address(tk));
179 static int register_kprobe_event(struct trace_kprobe *tk);
180 static int unregister_kprobe_event(struct trace_kprobe *tk);
182 static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
183 static int kretprobe_dispatcher(struct kretprobe_instance *ri,
184 struct pt_regs *regs);
187 * Allocate new trace_probe and initialize it (including kprobes).
189 static struct trace_kprobe *alloc_trace_kprobe(const char *group,
195 int nargs, bool is_return)
197 struct trace_kprobe *tk;
200 tk = kzalloc(SIZEOF_TRACE_KPROBE(nargs), GFP_KERNEL);
204 tk->nhit = alloc_percpu(unsigned long);
209 tk->symbol = kstrdup(symbol, GFP_KERNEL);
212 tk->rp.kp.symbol_name = tk->symbol;
213 tk->rp.kp.offset = offs;
215 tk->rp.kp.addr = addr;
218 tk->rp.handler = kretprobe_dispatcher;
220 tk->rp.kp.pre_handler = kprobe_dispatcher;
222 tk->rp.maxactive = maxactive;
224 if (!event || !group) {
229 tk->tp.call.class = &tk->tp.class;
230 tk->tp.call.name = kstrdup(event, GFP_KERNEL);
231 if (!tk->tp.call.name)
234 tk->tp.class.system = kstrdup(group, GFP_KERNEL);
235 if (!tk->tp.class.system)
238 dyn_event_init(&tk->devent, &trace_kprobe_ops);
239 INIT_LIST_HEAD(&tk->tp.files);
242 kfree(tk->tp.call.name);
244 free_percpu(tk->nhit);
249 static void free_trace_kprobe(struct trace_kprobe *tk)
256 for (i = 0; i < tk->tp.nr_args; i++)
257 traceprobe_free_probe_arg(&tk->tp.args[i]);
259 kfree(tk->tp.call.class->system);
260 kfree(tk->tp.call.name);
262 free_percpu(tk->nhit);
266 static struct trace_kprobe *find_trace_kprobe(const char *event,
269 struct dyn_event *pos;
270 struct trace_kprobe *tk;
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)
279 static inline int __enable_trace_kprobe(struct trace_kprobe *tk)
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);
287 ret = enable_kprobe(&tk->rp.kp);
295 * if the file is NULL, enable "perf" handler, or enable "trace" handler.
298 enable_trace_kprobe(struct trace_kprobe *tk, struct trace_event_file *file)
300 struct event_file_link *link;
304 link = kmalloc(sizeof(*link), GFP_KERNEL);
311 list_add_tail_rcu(&link->list, &tk->tp.files);
313 tk->tp.flags |= TP_FLAG_TRACE;
314 ret = __enable_trace_kprobe(tk);
316 list_del_rcu(&link->list);
318 tk->tp.flags &= ~TP_FLAG_TRACE;
322 tk->tp.flags |= TP_FLAG_PROFILE;
323 ret = __enable_trace_kprobe(tk);
325 tk->tp.flags &= ~TP_FLAG_PROFILE;
332 * Disable trace_probe
333 * if the file is NULL, disable "perf" handler, or disable "trace" handler.
336 disable_trace_kprobe(struct trace_kprobe *tk, struct trace_event_file *file)
338 struct event_file_link *link = NULL;
343 link = find_event_file_link(&tk->tp, file);
349 list_del_rcu(&link->list);
351 if (!list_empty(&tk->tp.files))
354 tk->tp.flags &= ~TP_FLAG_TRACE;
356 tk->tp.flags &= ~TP_FLAG_PROFILE;
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);
362 disable_kprobe(&tk->rp.kp);
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
371 if (list_empty(&tk->devent.list))
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.
384 kfree(link); /* Ignored if link == NULL */
390 #if defined(CONFIG_KPROBES_ON_FTRACE) && \
391 !defined(CONFIG_KPROBE_EVENTS_ON_NOTRACE)
392 static bool within_notrace_func(struct trace_kprobe *tk)
394 unsigned long offset, size, addr;
396 addr = trace_kprobe_address(tk);
397 if (!addr || !kallsyms_lookup_size_offset(addr, &size, &offset))
400 /* Get the entry address of the target function */
404 * Since ftrace_location_range() does inclusive range check, we need
405 * to subtract 1 byte from the end address.
407 return !ftrace_location_range(addr, addr + size - 1);
410 #define within_notrace_func(tk) (false)
413 /* Internal register function - just handle k*probes and flags */
414 static int __register_trace_kprobe(struct trace_kprobe *tk)
418 if (trace_probe_is_registered(&tk->tp))
421 if (within_notrace_func(tk)) {
422 pr_warn("Could not probe notrace function %s\n",
423 trace_kprobe_symbol(tk));
427 for (i = 0; i < tk->tp.nr_args; i++) {
428 ret = traceprobe_update_arg(&tk->tp.args[i]);
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;
437 tk->rp.kp.flags |= KPROBE_FLAG_DISABLED;
439 if (trace_kprobe_is_return(tk))
440 ret = register_kretprobe(&tk->rp);
442 ret = register_kprobe(&tk->rp.kp);
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",
454 /* Internal unregister function - just handle k*probes and flags */
455 static void __unregister_trace_kprobe(struct trace_kprobe *tk)
457 if (trace_probe_is_registered(&tk->tp)) {
458 if (trace_kprobe_is_return(tk))
459 unregister_kretprobe(&tk->rp);
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;
469 /* Unregister a trace_probe and probe_event */
470 static int unregister_trace_kprobe(struct trace_kprobe *tk)
472 /* Enabled event can not be unregistered */
473 if (trace_probe_is_enabled(&tk->tp))
476 /* Will fail if probe is being used by ftrace or perf */
477 if (unregister_kprobe_event(tk))
480 __unregister_trace_kprobe(tk);
481 dyn_event_remove(&tk->devent);
486 /* Register a trace_probe and probe_event */
487 static int register_trace_kprobe(struct trace_kprobe *tk)
489 struct trace_kprobe *old_tk;
492 mutex_lock(&event_mutex);
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);
498 ret = unregister_trace_kprobe(old_tk);
501 free_trace_kprobe(old_tk);
504 /* Register new event */
505 ret = register_kprobe_event(tk);
507 pr_warn("Failed to register probe event(%d)\n", ret);
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");
519 unregister_kprobe_event(tk);
521 dyn_event_add(&tk->devent);
524 mutex_unlock(&event_mutex);
528 /* Module notifier call back, checking event on the module */
529 static int trace_kprobe_module_callback(struct notifier_block *nb,
530 unsigned long val, void *data)
532 struct module *mod = data;
533 struct dyn_event *pos;
534 struct trace_kprobe *tk;
537 if (val != MODULE_STATE_COMING)
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);
548 pr_warn("Failed to re-register probe %s on %s: %d\n",
549 trace_event_name(&tk->tp.call),
553 mutex_unlock(&event_mutex);
558 static struct notifier_block trace_kprobe_module_nb = {
559 .notifier_call = trace_kprobe_module_callback,
560 .priority = 1 /* Invoked after kprobe module callback */
563 /* Convert certain expected symbols into '_' when generating event names */
564 static inline void sanitize_event_name(char *name)
566 while (*name++ != '\0')
567 if (*name == ':' || *name == '.')
571 static int trace_kprobe_create(int argc, const char *argv[])
576 * p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS]
578 * r[MAXACTIVE][:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS]
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.
592 * FETCHARG:TYPE : use TYPE instead of unsigned long.
594 struct trace_kprobe *tk;
596 bool is_return = false;
597 char *symbol = NULL, *tmp = NULL;
598 const char *event = NULL, *group = KPROBE_EVENT_SYSTEM;
602 char buf[MAX_EVENT_NAME_LEN];
603 unsigned int flags = TPARG_FL_KERNEL;
605 switch (argv[0][0]) {
608 flags |= TPARG_FL_RETURN;
618 event = strchr(&argv[0][1], ':');
622 if (isdigit(argv[0][1])) {
624 pr_info("Maxactive is not for kprobe");
628 len = event - &argv[0][1] - 1;
630 len = strlen(&argv[0][1]);
631 if (len > MAX_EVENT_NAME_LEN - 1)
633 memcpy(buf, &argv[0][1], len);
635 ret = kstrtouint(buf, 0, &maxactive);
636 if (ret || !maxactive) {
637 pr_info("Invalid maxactive number\n");
640 /* kretprobes instances are iterated over via a list. The
641 * maximum should stay reasonable.
643 if (maxactive > KRETPROBE_MAXACTIVE_MAX) {
644 pr_info("Maxactive is too big (%d > %d).\n",
645 maxactive, KRETPROBE_MAXACTIVE_MAX);
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], ':'))
656 /* a symbol specified */
657 symbol = kstrdup(argv[1], GFP_KERNEL);
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");
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");
674 argc -= 2; argv += 2;
677 ret = traceprobe_parse_event_name(&event, &group, buf);
681 /* Make a new event name */
683 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
684 is_return ? 'r' : 'p', symbol, offset);
686 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
687 is_return ? 'r' : 'p', addr);
688 sanitize_event_name(buf);
693 tk = alloc_trace_kprobe(group, event, addr, symbol, offset, maxactive,
697 /* This must return -ENOMEM otherwise there is a bug */
698 WARN_ON_ONCE(ret != -ENOMEM);
702 /* parse arguments */
703 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
704 tmp = kstrdup(argv[i], GFP_KERNEL);
710 ret = traceprobe_parse_probe_arg(&tk->tp, i, tmp, flags);
716 ret = register_trace_kprobe(tk);
724 free_trace_kprobe(tk);
728 static int create_or_delete_trace_kprobe(int argc, char **argv)
732 if (argv[0][0] == '-')
733 return dyn_event_release(argc, argv, &trace_kprobe_ops);
735 ret = trace_kprobe_create(argc, (const char **)argv);
736 return ret == -ECANCELED ? -EINVAL : ret;
739 static int trace_kprobe_release(struct dyn_event *ev)
741 struct trace_kprobe *tk = to_trace_kprobe(ev);
742 int ret = unregister_trace_kprobe(tk);
745 free_trace_kprobe(tk);
749 static int trace_kprobe_show(struct seq_file *m, struct dyn_event *ev)
751 struct trace_kprobe *tk = to_trace_kprobe(ev);
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));
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),
764 seq_printf(m, " %s", trace_kprobe_symbol(tk));
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);
773 static int probes_seq_show(struct seq_file *m, void *v)
775 struct dyn_event *ev = v;
777 if (!is_trace_kprobe(ev))
780 return trace_kprobe_show(m, ev);
783 static 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
790 static int probes_open(struct inode *inode, struct file *file)
794 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
795 ret = dyn_events_release_all(&trace_kprobe_ops);
800 return seq_open(file, &probes_seq_op);
803 static ssize_t probes_write(struct file *file, const char __user *buffer,
804 size_t count, loff_t *ppos)
806 return trace_parse_run_command(file, buffer, count, ppos,
807 create_or_delete_trace_kprobe);
810 static const struct file_operations kprobe_events_ops = {
811 .owner = THIS_MODULE,
815 .release = seq_release,
816 .write = probes_write,
819 /* Probes profiling interfaces */
820 static int probes_profile_seq_show(struct seq_file *m, void *v)
822 struct dyn_event *ev = v;
823 struct trace_kprobe *tk;
825 if (!is_trace_kprobe(ev))
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),
837 static 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
844 static int profile_open(struct inode *inode, struct file *file)
846 return seq_open(file, &profile_seq_op);
849 static const struct file_operations kprobe_profile_ops = {
850 .owner = THIS_MODULE,
851 .open = profile_open,
854 .release = seq_release,
857 /* Kprobe specific fetch functions */
859 /* Return the length of string -- including null terminal byte */
860 static nokprobe_inline int
861 fetch_store_strlen(unsigned long addr)
872 ret = __copy_from_user_inatomic(&c, (u8 *)addr + len, 1);
874 } while (c && ret == 0 && len < MAX_STRING_SIZE);
879 return (ret < 0) ? ret : len;
883 * Fetch a null-terminated string. Caller MUST set *(u32 *)buf with max
884 * length and relative data location.
886 static nokprobe_inline int
887 fetch_store_string(unsigned long addr, void *dest, void *base)
889 int maxlen = get_loc_len(*(u32 *)dest);
890 u8 *dst = get_loc_data(dest, base);
893 if (unlikely(!maxlen))
896 * Try to get string again, since the string can be changed while
899 ret = strncpy_from_unsafe(dst, (void *)addr, maxlen);
902 *(u32 *)dest = make_data_loc(ret, (void *)dst - base);
906 static nokprobe_inline int
907 probe_mem_read(void *dest, void *src, size_t size)
909 return probe_kernel_read(dest, src, size);
912 /* Note that we don't verify it, since the code does not come from user space */
914 process_fetch_insn(struct fetch_insn *code, struct pt_regs *regs, void *dest,
920 /* 1st stage: get value from context */
923 val = regs_get_register(regs, code->param);
926 val = regs_get_kernel_stack_nth(regs, code->param);
928 case FETCH_OP_STACKP:
929 val = kernel_stack_pointer(regs);
931 case FETCH_OP_RETVAL:
932 val = regs_return_value(regs);
935 val = code->immediate;
938 val = (unsigned long)current->comm;
940 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
942 val = regs_get_kernel_argument(regs, code->param);
945 case FETCH_NOP_SYMBOL: /* Ignore a place holder */
953 return process_fetch_insn_bottom(code, val, dest, base);
955 NOKPROBE_SYMBOL(process_fetch_insn)
958 static nokprobe_inline void
959 __kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs,
960 struct trace_event_file *trace_file)
962 struct kprobe_trace_entry_head *entry;
963 struct ring_buffer_event *event;
964 struct ring_buffer *buffer;
966 unsigned long irq_flags;
967 struct trace_event_call *call = &tk->tp.call;
969 WARN_ON(call != trace_file->event_call);
971 if (trace_trigger_soft_disabled(trace_file))
974 local_save_flags(irq_flags);
975 pc = preempt_count();
977 dsize = __get_data_size(&tk->tp, regs);
978 size = sizeof(*entry) + tk->tp.size + dsize;
980 event = trace_event_buffer_lock_reserve(&buffer, trace_file,
982 size, irq_flags, pc);
986 entry = ring_buffer_event_data(event);
987 entry->ip = (unsigned long)tk->rp.kp.addr;
988 store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
990 event_trigger_unlock_commit_regs(trace_file, buffer, event,
991 entry, irq_flags, pc, regs);
995 kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs)
997 struct event_file_link *link;
999 list_for_each_entry_rcu(link, &tk->tp.files, list)
1000 __kprobe_trace_func(tk, regs, link->file);
1002 NOKPROBE_SYMBOL(kprobe_trace_func);
1004 /* Kretprobe handler */
1005 static nokprobe_inline void
1006 __kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
1007 struct pt_regs *regs,
1008 struct trace_event_file *trace_file)
1010 struct kretprobe_trace_entry_head *entry;
1011 struct ring_buffer_event *event;
1012 struct ring_buffer *buffer;
1013 int size, pc, dsize;
1014 unsigned long irq_flags;
1015 struct trace_event_call *call = &tk->tp.call;
1017 WARN_ON(call != trace_file->event_call);
1019 if (trace_trigger_soft_disabled(trace_file))
1022 local_save_flags(irq_flags);
1023 pc = preempt_count();
1025 dsize = __get_data_size(&tk->tp, regs);
1026 size = sizeof(*entry) + tk->tp.size + dsize;
1028 event = trace_event_buffer_lock_reserve(&buffer, trace_file,
1030 size, irq_flags, pc);
1034 entry = ring_buffer_event_data(event);
1035 entry->func = (unsigned long)tk->rp.kp.addr;
1036 entry->ret_ip = (unsigned long)ri->ret_addr;
1037 store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
1039 event_trigger_unlock_commit_regs(trace_file, buffer, event,
1040 entry, irq_flags, pc, regs);
1044 kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
1045 struct pt_regs *regs)
1047 struct event_file_link *link;
1049 list_for_each_entry_rcu(link, &tk->tp.files, list)
1050 __kretprobe_trace_func(tk, ri, regs, link->file);
1052 NOKPROBE_SYMBOL(kretprobe_trace_func);
1054 /* Event entry printers */
1055 static enum print_line_t
1056 print_kprobe_event(struct trace_iterator *iter, int flags,
1057 struct trace_event *event)
1059 struct kprobe_trace_entry_head *field;
1060 struct trace_seq *s = &iter->seq;
1061 struct trace_probe *tp;
1063 field = (struct kprobe_trace_entry_head *)iter->ent;
1064 tp = container_of(event, struct trace_probe, call.event);
1066 trace_seq_printf(s, "%s: (", trace_event_name(&tp->call));
1068 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
1071 trace_seq_putc(s, ')');
1073 if (print_probe_args(s, tp->args, tp->nr_args,
1074 (u8 *)&field[1], field) < 0)
1077 trace_seq_putc(s, '\n');
1079 return trace_handle_return(s);
1082 static enum print_line_t
1083 print_kretprobe_event(struct trace_iterator *iter, int flags,
1084 struct trace_event *event)
1086 struct kretprobe_trace_entry_head *field;
1087 struct trace_seq *s = &iter->seq;
1088 struct trace_probe *tp;
1090 field = (struct kretprobe_trace_entry_head *)iter->ent;
1091 tp = container_of(event, struct trace_probe, call.event);
1093 trace_seq_printf(s, "%s: (", trace_event_name(&tp->call));
1095 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
1098 trace_seq_puts(s, " <- ");
1100 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
1103 trace_seq_putc(s, ')');
1105 if (print_probe_args(s, tp->args, tp->nr_args,
1106 (u8 *)&field[1], field) < 0)
1109 trace_seq_putc(s, '\n');
1112 return trace_handle_return(s);
1116 static int kprobe_event_define_fields(struct trace_event_call *event_call)
1119 struct kprobe_trace_entry_head field;
1120 struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data;
1122 DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
1124 return traceprobe_define_arg_fields(event_call, sizeof(field), &tk->tp);
1127 static int kretprobe_event_define_fields(struct trace_event_call *event_call)
1130 struct kretprobe_trace_entry_head field;
1131 struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data;
1133 DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
1134 DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
1136 return traceprobe_define_arg_fields(event_call, sizeof(field), &tk->tp);
1139 #ifdef CONFIG_PERF_EVENTS
1141 /* Kprobe profile handler */
1143 kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs)
1145 struct trace_event_call *call = &tk->tp.call;
1146 struct kprobe_trace_entry_head *entry;
1147 struct hlist_head *head;
1148 int size, __size, dsize;
1151 if (bpf_prog_array_valid(call)) {
1152 unsigned long orig_ip = instruction_pointer(regs);
1155 ret = trace_call_bpf(call, regs);
1158 * We need to check and see if we modified the pc of the
1159 * pt_regs, and if so return 1 so that we don't do the
1162 if (orig_ip != instruction_pointer(regs))
1168 head = this_cpu_ptr(call->perf_events);
1169 if (hlist_empty(head))
1172 dsize = __get_data_size(&tk->tp, regs);
1173 __size = sizeof(*entry) + tk->tp.size + dsize;
1174 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1175 size -= sizeof(u32);
1177 entry = perf_trace_buf_alloc(size, NULL, &rctx);
1181 entry->ip = (unsigned long)tk->rp.kp.addr;
1182 memset(&entry[1], 0, dsize);
1183 store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
1184 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1188 NOKPROBE_SYMBOL(kprobe_perf_func);
1190 /* Kretprobe profile handler */
1192 kretprobe_perf_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
1193 struct pt_regs *regs)
1195 struct trace_event_call *call = &tk->tp.call;
1196 struct kretprobe_trace_entry_head *entry;
1197 struct hlist_head *head;
1198 int size, __size, dsize;
1201 if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs))
1204 head = this_cpu_ptr(call->perf_events);
1205 if (hlist_empty(head))
1208 dsize = __get_data_size(&tk->tp, regs);
1209 __size = sizeof(*entry) + tk->tp.size + dsize;
1210 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1211 size -= sizeof(u32);
1213 entry = perf_trace_buf_alloc(size, NULL, &rctx);
1217 entry->func = (unsigned long)tk->rp.kp.addr;
1218 entry->ret_ip = (unsigned long)ri->ret_addr;
1219 store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
1220 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1223 NOKPROBE_SYMBOL(kretprobe_perf_func);
1225 int bpf_get_kprobe_info(const struct perf_event *event, u32 *fd_type,
1226 const char **symbol, u64 *probe_offset,
1227 u64 *probe_addr, bool perf_type_tracepoint)
1229 const char *pevent = trace_event_name(event->tp_event);
1230 const char *group = event->tp_event->class->system;
1231 struct trace_kprobe *tk;
1233 if (perf_type_tracepoint)
1234 tk = find_trace_kprobe(pevent, group);
1236 tk = event->tp_event->data;
1240 *fd_type = trace_kprobe_is_return(tk) ? BPF_FD_TYPE_KRETPROBE
1241 : BPF_FD_TYPE_KPROBE;
1243 *symbol = tk->symbol;
1244 *probe_offset = tk->rp.kp.offset;
1249 *probe_addr = (unsigned long)tk->rp.kp.addr;
1253 #endif /* CONFIG_PERF_EVENTS */
1256 * called by perf_trace_init() or __ftrace_set_clr_event() under event_mutex.
1258 * kprobe_trace_self_tests_init() does enable_trace_probe/disable_trace_probe
1259 * lockless, but we can't race with this __init function.
1261 static int kprobe_register(struct trace_event_call *event,
1262 enum trace_reg type, void *data)
1264 struct trace_kprobe *tk = (struct trace_kprobe *)event->data;
1265 struct trace_event_file *file = data;
1268 case TRACE_REG_REGISTER:
1269 return enable_trace_kprobe(tk, file);
1270 case TRACE_REG_UNREGISTER:
1271 return disable_trace_kprobe(tk, file);
1273 #ifdef CONFIG_PERF_EVENTS
1274 case TRACE_REG_PERF_REGISTER:
1275 return enable_trace_kprobe(tk, NULL);
1276 case TRACE_REG_PERF_UNREGISTER:
1277 return disable_trace_kprobe(tk, NULL);
1278 case TRACE_REG_PERF_OPEN:
1279 case TRACE_REG_PERF_CLOSE:
1280 case TRACE_REG_PERF_ADD:
1281 case TRACE_REG_PERF_DEL:
1288 static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
1290 struct trace_kprobe *tk = container_of(kp, struct trace_kprobe, rp.kp);
1293 raw_cpu_inc(*tk->nhit);
1295 if (tk->tp.flags & TP_FLAG_TRACE)
1296 kprobe_trace_func(tk, regs);
1297 #ifdef CONFIG_PERF_EVENTS
1298 if (tk->tp.flags & TP_FLAG_PROFILE)
1299 ret = kprobe_perf_func(tk, regs);
1303 NOKPROBE_SYMBOL(kprobe_dispatcher);
1306 kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
1308 struct trace_kprobe *tk = container_of(ri->rp, struct trace_kprobe, rp);
1310 raw_cpu_inc(*tk->nhit);
1312 if (tk->tp.flags & TP_FLAG_TRACE)
1313 kretprobe_trace_func(tk, ri, regs);
1314 #ifdef CONFIG_PERF_EVENTS
1315 if (tk->tp.flags & TP_FLAG_PROFILE)
1316 kretprobe_perf_func(tk, ri, regs);
1318 return 0; /* We don't tweek kernel, so just return 0 */
1320 NOKPROBE_SYMBOL(kretprobe_dispatcher);
1322 static struct trace_event_functions kretprobe_funcs = {
1323 .trace = print_kretprobe_event
1326 static struct trace_event_functions kprobe_funcs = {
1327 .trace = print_kprobe_event
1330 static inline void init_trace_event_call(struct trace_kprobe *tk,
1331 struct trace_event_call *call)
1333 INIT_LIST_HEAD(&call->class->fields);
1334 if (trace_kprobe_is_return(tk)) {
1335 call->event.funcs = &kretprobe_funcs;
1336 call->class->define_fields = kretprobe_event_define_fields;
1338 call->event.funcs = &kprobe_funcs;
1339 call->class->define_fields = kprobe_event_define_fields;
1342 call->flags = TRACE_EVENT_FL_KPROBE;
1343 call->class->reg = kprobe_register;
1347 static int register_kprobe_event(struct trace_kprobe *tk)
1349 struct trace_event_call *call = &tk->tp.call;
1352 init_trace_event_call(tk, call);
1354 if (traceprobe_set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0)
1356 ret = register_trace_event(&call->event);
1358 kfree(call->print_fmt);
1361 ret = trace_add_event_call(call);
1363 pr_info("Failed to register kprobe event: %s\n",
1364 trace_event_name(call));
1365 kfree(call->print_fmt);
1366 unregister_trace_event(&call->event);
1371 static int unregister_kprobe_event(struct trace_kprobe *tk)
1375 /* tp->event is unregistered in trace_remove_event_call() */
1376 ret = trace_remove_event_call(&tk->tp.call);
1378 kfree(tk->tp.call.print_fmt);
1382 #ifdef CONFIG_PERF_EVENTS
1383 /* create a trace_kprobe, but don't add it to global lists */
1384 struct trace_event_call *
1385 create_local_trace_kprobe(char *func, void *addr, unsigned long offs,
1388 struct trace_kprobe *tk;
1393 * local trace_kprobes are not added to dyn_event, so they are never
1394 * searched in find_trace_kprobe(). Therefore, there is no concern of
1395 * duplicated name here.
1397 event = func ? func : "DUMMY_EVENT";
1399 tk = alloc_trace_kprobe(KPROBE_EVENT_SYSTEM, event, (void *)addr, func,
1400 offs, 0 /* maxactive */, 0 /* nargs */,
1404 pr_info("Failed to allocate trace_probe.(%d)\n",
1406 return ERR_CAST(tk);
1409 init_trace_event_call(tk, &tk->tp.call);
1411 if (traceprobe_set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0) {
1416 ret = __register_trace_kprobe(tk);
1418 kfree(tk->tp.call.print_fmt);
1422 return &tk->tp.call;
1424 free_trace_kprobe(tk);
1425 return ERR_PTR(ret);
1428 void destroy_local_trace_kprobe(struct trace_event_call *event_call)
1430 struct trace_kprobe *tk;
1432 tk = container_of(event_call, struct trace_kprobe, tp.call);
1434 if (trace_probe_is_enabled(&tk->tp)) {
1439 __unregister_trace_kprobe(tk);
1441 kfree(tk->tp.call.print_fmt);
1442 free_trace_kprobe(tk);
1444 #endif /* CONFIG_PERF_EVENTS */
1446 /* Make a tracefs interface for controlling probe points */
1447 static __init int init_kprobe_trace(void)
1449 struct dentry *d_tracer;
1450 struct dentry *entry;
1453 ret = dyn_event_register(&trace_kprobe_ops);
1457 if (register_module_notifier(&trace_kprobe_module_nb))
1460 d_tracer = tracing_init_dentry();
1461 if (IS_ERR(d_tracer))
1464 entry = tracefs_create_file("kprobe_events", 0644, d_tracer,
1465 NULL, &kprobe_events_ops);
1467 /* Event list interface */
1469 pr_warn("Could not create tracefs 'kprobe_events' entry\n");
1471 /* Profile interface */
1472 entry = tracefs_create_file("kprobe_profile", 0444, d_tracer,
1473 NULL, &kprobe_profile_ops);
1476 pr_warn("Could not create tracefs 'kprobe_profile' entry\n");
1479 fs_initcall(init_kprobe_trace);
1482 #ifdef CONFIG_FTRACE_STARTUP_TEST
1483 static __init struct trace_event_file *
1484 find_trace_probe_file(struct trace_kprobe *tk, struct trace_array *tr)
1486 struct trace_event_file *file;
1488 list_for_each_entry(file, &tr->events, list)
1489 if (file->event_call == &tk->tp.call)
1496 * Nobody but us can call enable_trace_kprobe/disable_trace_kprobe at this
1497 * stage, we can do this lockless.
1499 static __init int kprobe_trace_self_tests_init(void)
1502 int (*target)(int, int, int, int, int, int);
1503 struct trace_kprobe *tk;
1504 struct trace_event_file *file;
1506 if (tracing_is_disabled())
1509 target = kprobe_trace_selftest_target;
1511 pr_info("Testing kprobe tracing: ");
1513 ret = trace_run_command("p:testprobe kprobe_trace_selftest_target $stack $stack0 +0($stack)",
1514 create_or_delete_trace_kprobe);
1515 if (WARN_ON_ONCE(ret)) {
1516 pr_warn("error on probing function entry.\n");
1519 /* Enable trace point */
1520 tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
1521 if (WARN_ON_ONCE(tk == NULL)) {
1522 pr_warn("error on getting new probe.\n");
1525 file = find_trace_probe_file(tk, top_trace_array());
1526 if (WARN_ON_ONCE(file == NULL)) {
1527 pr_warn("error on getting probe file.\n");
1530 enable_trace_kprobe(tk, file);
1534 ret = trace_run_command("r:testprobe2 kprobe_trace_selftest_target $retval",
1535 create_or_delete_trace_kprobe);
1536 if (WARN_ON_ONCE(ret)) {
1537 pr_warn("error on probing function return.\n");
1540 /* Enable trace point */
1541 tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
1542 if (WARN_ON_ONCE(tk == NULL)) {
1543 pr_warn("error on getting 2nd new probe.\n");
1546 file = find_trace_probe_file(tk, top_trace_array());
1547 if (WARN_ON_ONCE(file == NULL)) {
1548 pr_warn("error on getting probe file.\n");
1551 enable_trace_kprobe(tk, file);
1558 ret = target(1, 2, 3, 4, 5, 6);
1561 * Not expecting an error here, the check is only to prevent the
1562 * optimizer from removing the call to target() as otherwise there
1563 * are no side-effects and the call is never performed.
1568 /* Disable trace points before removing it */
1569 tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
1570 if (WARN_ON_ONCE(tk == NULL)) {
1571 pr_warn("error on getting test probe.\n");
1574 if (trace_kprobe_nhit(tk) != 1) {
1575 pr_warn("incorrect number of testprobe hits\n");
1579 file = find_trace_probe_file(tk, top_trace_array());
1580 if (WARN_ON_ONCE(file == NULL)) {
1581 pr_warn("error on getting probe file.\n");
1584 disable_trace_kprobe(tk, file);
1587 tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
1588 if (WARN_ON_ONCE(tk == NULL)) {
1589 pr_warn("error on getting 2nd test probe.\n");
1592 if (trace_kprobe_nhit(tk) != 1) {
1593 pr_warn("incorrect number of testprobe2 hits\n");
1597 file = find_trace_probe_file(tk, top_trace_array());
1598 if (WARN_ON_ONCE(file == NULL)) {
1599 pr_warn("error on getting probe file.\n");
1602 disable_trace_kprobe(tk, file);
1605 ret = trace_run_command("-:testprobe", create_or_delete_trace_kprobe);
1606 if (WARN_ON_ONCE(ret)) {
1607 pr_warn("error on deleting a probe.\n");
1611 ret = trace_run_command("-:testprobe2", create_or_delete_trace_kprobe);
1612 if (WARN_ON_ONCE(ret)) {
1613 pr_warn("error on deleting a probe.\n");
1618 ret = dyn_events_release_all(&trace_kprobe_ops);
1619 if (WARN_ON_ONCE(ret)) {
1620 pr_warn("error on cleaning up probes.\n");
1624 * Wait for the optimizer work to finish. Otherwise it might fiddle
1625 * with probes in already freed __init text.
1627 wait_for_kprobe_optimizer();
1629 pr_cont("NG: Some tests are failed. Please check them.\n");
1635 late_initcall(kprobe_trace_self_tests_init);