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 <asm/setup.h> /* for COMMAND_LINE_SIZE */
17 #include "trace_dynevent.h"
18 #include "trace_kprobe_selftest.h"
19 #include "trace_probe.h"
20 #include "trace_probe_tmpl.h"
22 #define KPROBE_EVENT_SYSTEM "kprobes"
23 #define KRETPROBE_MAXACTIVE_MAX 4096
24 #define MAX_KPROBE_CMDLINE_SIZE 1024
26 /* Kprobe early definition from command line */
27 static char kprobe_boot_events_buf[COMMAND_LINE_SIZE] __initdata;
28 static bool kprobe_boot_events_enabled __initdata;
30 static int __init set_kprobe_boot_events(char *str)
32 strlcpy(kprobe_boot_events_buf, str, COMMAND_LINE_SIZE);
35 __setup("kprobe_event=", set_kprobe_boot_events);
37 static int trace_kprobe_create(int argc, const char **argv);
38 static int trace_kprobe_show(struct seq_file *m, struct dyn_event *ev);
39 static int trace_kprobe_release(struct dyn_event *ev);
40 static bool trace_kprobe_is_busy(struct dyn_event *ev);
41 static bool trace_kprobe_match(const char *system, const char *event,
42 struct dyn_event *ev);
44 static struct dyn_event_operations trace_kprobe_ops = {
45 .create = trace_kprobe_create,
46 .show = trace_kprobe_show,
47 .is_busy = trace_kprobe_is_busy,
48 .free = trace_kprobe_release,
49 .match = trace_kprobe_match,
53 * Kprobe event core functions
56 struct dyn_event devent;
57 struct kretprobe rp; /* Use rp.kp for kprobe use */
58 unsigned long __percpu *nhit;
59 const char *symbol; /* symbol name */
60 struct trace_probe tp;
63 static bool is_trace_kprobe(struct dyn_event *ev)
65 return ev->ops == &trace_kprobe_ops;
68 static struct trace_kprobe *to_trace_kprobe(struct dyn_event *ev)
70 return container_of(ev, struct trace_kprobe, devent);
74 * for_each_trace_kprobe - iterate over the trace_kprobe list
75 * @pos: the struct trace_kprobe * for each entry
76 * @dpos: the struct dyn_event * to use as a loop cursor
78 #define for_each_trace_kprobe(pos, dpos) \
79 for_each_dyn_event(dpos) \
80 if (is_trace_kprobe(dpos) && (pos = to_trace_kprobe(dpos)))
82 #define SIZEOF_TRACE_KPROBE(n) \
83 (offsetof(struct trace_kprobe, tp.args) + \
84 (sizeof(struct probe_arg) * (n)))
86 static nokprobe_inline bool trace_kprobe_is_return(struct trace_kprobe *tk)
88 return tk->rp.handler != NULL;
91 static nokprobe_inline const char *trace_kprobe_symbol(struct trace_kprobe *tk)
93 return tk->symbol ? tk->symbol : "unknown";
96 static nokprobe_inline unsigned long trace_kprobe_offset(struct trace_kprobe *tk)
98 return tk->rp.kp.offset;
101 static nokprobe_inline bool trace_kprobe_has_gone(struct trace_kprobe *tk)
103 return !!(kprobe_gone(&tk->rp.kp));
106 static nokprobe_inline bool trace_kprobe_within_module(struct trace_kprobe *tk,
109 int len = strlen(mod->name);
110 const char *name = trace_kprobe_symbol(tk);
111 return strncmp(mod->name, name, len) == 0 && name[len] == ':';
114 static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprobe *tk)
121 p = strchr(tk->symbol, ':');
125 mutex_lock(&module_mutex);
126 ret = !!find_module(tk->symbol);
127 mutex_unlock(&module_mutex);
133 static bool trace_kprobe_is_busy(struct dyn_event *ev)
135 struct trace_kprobe *tk = to_trace_kprobe(ev);
137 return trace_probe_is_enabled(&tk->tp);
140 static bool trace_kprobe_match(const char *system, const char *event,
141 struct dyn_event *ev)
143 struct trace_kprobe *tk = to_trace_kprobe(ev);
145 return strcmp(trace_event_name(&tk->tp.call), event) == 0 &&
146 (!system || strcmp(tk->tp.call.class->system, system) == 0);
149 static nokprobe_inline unsigned long trace_kprobe_nhit(struct trace_kprobe *tk)
151 unsigned long nhit = 0;
154 for_each_possible_cpu(cpu)
155 nhit += *per_cpu_ptr(tk->nhit, cpu);
160 /* Return 0 if it fails to find the symbol address */
161 static nokprobe_inline
162 unsigned long trace_kprobe_address(struct trace_kprobe *tk)
167 addr = (unsigned long)
168 kallsyms_lookup_name(trace_kprobe_symbol(tk));
170 addr += tk->rp.kp.offset;
172 addr = (unsigned long)tk->rp.kp.addr;
177 bool trace_kprobe_on_func_entry(struct trace_event_call *call)
179 struct trace_kprobe *tk = (struct trace_kprobe *)call->data;
181 return kprobe_on_func_entry(tk->rp.kp.addr,
182 tk->rp.kp.addr ? NULL : tk->rp.kp.symbol_name,
183 tk->rp.kp.addr ? 0 : tk->rp.kp.offset);
186 bool trace_kprobe_error_injectable(struct trace_event_call *call)
188 struct trace_kprobe *tk = (struct trace_kprobe *)call->data;
190 return within_error_injection_list(trace_kprobe_address(tk));
193 static int register_kprobe_event(struct trace_kprobe *tk);
194 static int unregister_kprobe_event(struct trace_kprobe *tk);
196 static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
197 static int kretprobe_dispatcher(struct kretprobe_instance *ri,
198 struct pt_regs *regs);
201 * Allocate new trace_probe and initialize it (including kprobes).
203 static struct trace_kprobe *alloc_trace_kprobe(const char *group,
209 int nargs, bool is_return)
211 struct trace_kprobe *tk;
214 tk = kzalloc(SIZEOF_TRACE_KPROBE(nargs), GFP_KERNEL);
218 tk->nhit = alloc_percpu(unsigned long);
223 tk->symbol = kstrdup(symbol, GFP_KERNEL);
226 tk->rp.kp.symbol_name = tk->symbol;
227 tk->rp.kp.offset = offs;
229 tk->rp.kp.addr = addr;
232 tk->rp.handler = kretprobe_dispatcher;
234 tk->rp.kp.pre_handler = kprobe_dispatcher;
236 tk->rp.maxactive = maxactive;
238 if (!event || !group) {
243 tk->tp.call.class = &tk->tp.class;
244 tk->tp.call.name = kstrdup(event, GFP_KERNEL);
245 if (!tk->tp.call.name)
248 tk->tp.class.system = kstrdup(group, GFP_KERNEL);
249 if (!tk->tp.class.system)
252 dyn_event_init(&tk->devent, &trace_kprobe_ops);
253 INIT_LIST_HEAD(&tk->tp.files);
256 kfree(tk->tp.call.name);
258 free_percpu(tk->nhit);
263 static void free_trace_kprobe(struct trace_kprobe *tk)
270 for (i = 0; i < tk->tp.nr_args; i++)
271 traceprobe_free_probe_arg(&tk->tp.args[i]);
273 kfree(tk->tp.call.class->system);
274 kfree(tk->tp.call.name);
276 free_percpu(tk->nhit);
280 static struct trace_kprobe *find_trace_kprobe(const char *event,
283 struct dyn_event *pos;
284 struct trace_kprobe *tk;
286 for_each_trace_kprobe(tk, pos)
287 if (strcmp(trace_event_name(&tk->tp.call), event) == 0 &&
288 strcmp(tk->tp.call.class->system, group) == 0)
293 static inline int __enable_trace_kprobe(struct trace_kprobe *tk)
297 if (trace_probe_is_registered(&tk->tp) && !trace_kprobe_has_gone(tk)) {
298 if (trace_kprobe_is_return(tk))
299 ret = enable_kretprobe(&tk->rp);
301 ret = enable_kprobe(&tk->rp.kp);
309 * if the file is NULL, enable "perf" handler, or enable "trace" handler.
312 enable_trace_kprobe(struct trace_kprobe *tk, struct trace_event_file *file)
314 struct event_file_link *link;
318 link = kmalloc(sizeof(*link), GFP_KERNEL);
325 list_add_tail_rcu(&link->list, &tk->tp.files);
327 tk->tp.flags |= TP_FLAG_TRACE;
328 ret = __enable_trace_kprobe(tk);
330 list_del_rcu(&link->list);
332 tk->tp.flags &= ~TP_FLAG_TRACE;
336 tk->tp.flags |= TP_FLAG_PROFILE;
337 ret = __enable_trace_kprobe(tk);
339 tk->tp.flags &= ~TP_FLAG_PROFILE;
346 * Disable trace_probe
347 * if the file is NULL, disable "perf" handler, or disable "trace" handler.
350 disable_trace_kprobe(struct trace_kprobe *tk, struct trace_event_file *file)
352 struct event_file_link *link = NULL;
357 link = find_event_file_link(&tk->tp, file);
363 list_del_rcu(&link->list);
365 if (!list_empty(&tk->tp.files))
368 tk->tp.flags &= ~TP_FLAG_TRACE;
370 tk->tp.flags &= ~TP_FLAG_PROFILE;
372 if (!trace_probe_is_enabled(&tk->tp) && trace_probe_is_registered(&tk->tp)) {
373 if (trace_kprobe_is_return(tk))
374 disable_kretprobe(&tk->rp);
376 disable_kprobe(&tk->rp.kp);
381 * if tk is not added to any list, it must be a local trace_kprobe
382 * created with perf_event_open. We don't need to wait for these
385 if (list_empty(&tk->devent.list))
390 * Synchronize with kprobe_trace_func/kretprobe_trace_func
391 * to ensure disabled (all running handlers are finished).
392 * This is not only for kfree(), but also the caller,
393 * trace_remove_event_call() supposes it for releasing
394 * event_call related objects, which will be accessed in
395 * the kprobe_trace_func/kretprobe_trace_func.
398 kfree(link); /* Ignored if link == NULL */
404 #if defined(CONFIG_KPROBES_ON_FTRACE) && \
405 !defined(CONFIG_KPROBE_EVENTS_ON_NOTRACE)
406 static bool within_notrace_func(struct trace_kprobe *tk)
408 unsigned long offset, size, addr;
410 addr = trace_kprobe_address(tk);
411 if (!addr || !kallsyms_lookup_size_offset(addr, &size, &offset))
414 /* Get the entry address of the target function */
418 * Since ftrace_location_range() does inclusive range check, we need
419 * to subtract 1 byte from the end address.
421 return !ftrace_location_range(addr, addr + size - 1);
424 #define within_notrace_func(tk) (false)
427 /* Internal register function - just handle k*probes and flags */
428 static int __register_trace_kprobe(struct trace_kprobe *tk)
432 if (trace_probe_is_registered(&tk->tp))
435 if (within_notrace_func(tk)) {
436 pr_warn("Could not probe notrace function %s\n",
437 trace_kprobe_symbol(tk));
441 for (i = 0; i < tk->tp.nr_args; i++) {
442 ret = traceprobe_update_arg(&tk->tp.args[i]);
447 /* Set/clear disabled flag according to tp->flag */
448 if (trace_probe_is_enabled(&tk->tp))
449 tk->rp.kp.flags &= ~KPROBE_FLAG_DISABLED;
451 tk->rp.kp.flags |= KPROBE_FLAG_DISABLED;
453 if (trace_kprobe_is_return(tk))
454 ret = register_kretprobe(&tk->rp);
456 ret = register_kprobe(&tk->rp.kp);
459 tk->tp.flags |= TP_FLAG_REGISTERED;
463 /* Internal unregister function - just handle k*probes and flags */
464 static void __unregister_trace_kprobe(struct trace_kprobe *tk)
466 if (trace_probe_is_registered(&tk->tp)) {
467 if (trace_kprobe_is_return(tk))
468 unregister_kretprobe(&tk->rp);
470 unregister_kprobe(&tk->rp.kp);
471 tk->tp.flags &= ~TP_FLAG_REGISTERED;
472 /* Cleanup kprobe for reuse */
473 if (tk->rp.kp.symbol_name)
474 tk->rp.kp.addr = NULL;
478 /* Unregister a trace_probe and probe_event */
479 static int unregister_trace_kprobe(struct trace_kprobe *tk)
481 /* Enabled event can not be unregistered */
482 if (trace_probe_is_enabled(&tk->tp))
485 /* Will fail if probe is being used by ftrace or perf */
486 if (unregister_kprobe_event(tk))
489 __unregister_trace_kprobe(tk);
490 dyn_event_remove(&tk->devent);
495 /* Register a trace_probe and probe_event */
496 static int register_trace_kprobe(struct trace_kprobe *tk)
498 struct trace_kprobe *old_tk;
501 mutex_lock(&event_mutex);
503 /* Delete old (same name) event if exist */
504 old_tk = find_trace_kprobe(trace_event_name(&tk->tp.call),
505 tk->tp.call.class->system);
507 ret = unregister_trace_kprobe(old_tk);
510 free_trace_kprobe(old_tk);
513 /* Register new event */
514 ret = register_kprobe_event(tk);
516 pr_warn("Failed to register probe event(%d)\n", ret);
520 /* Register k*probe */
521 ret = __register_trace_kprobe(tk);
522 if (ret == -ENOENT && !trace_kprobe_module_exist(tk)) {
523 pr_warn("This probe might be able to register after target module is loaded. Continue.\n");
528 unregister_kprobe_event(tk);
530 dyn_event_add(&tk->devent);
533 mutex_unlock(&event_mutex);
537 /* Module notifier call back, checking event on the module */
538 static int trace_kprobe_module_callback(struct notifier_block *nb,
539 unsigned long val, void *data)
541 struct module *mod = data;
542 struct dyn_event *pos;
543 struct trace_kprobe *tk;
546 if (val != MODULE_STATE_COMING)
549 /* Update probes on coming module */
550 mutex_lock(&event_mutex);
551 for_each_trace_kprobe(tk, pos) {
552 if (trace_kprobe_within_module(tk, mod)) {
553 /* Don't need to check busy - this should have gone. */
554 __unregister_trace_kprobe(tk);
555 ret = __register_trace_kprobe(tk);
557 pr_warn("Failed to re-register probe %s on %s: %d\n",
558 trace_event_name(&tk->tp.call),
562 mutex_unlock(&event_mutex);
567 static struct notifier_block trace_kprobe_module_nb = {
568 .notifier_call = trace_kprobe_module_callback,
569 .priority = 1 /* Invoked after kprobe module callback */
572 /* Convert certain expected symbols into '_' when generating event names */
573 static inline void sanitize_event_name(char *name)
575 while (*name++ != '\0')
576 if (*name == ':' || *name == '.')
580 static int trace_kprobe_create(int argc, const char *argv[])
585 * p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS]
587 * r[MAXACTIVE][:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS]
589 * $retval : fetch return value
590 * $stack : fetch stack address
591 * $stackN : fetch Nth of stack (N:0-)
592 * $comm : fetch current task comm
593 * @ADDR : fetch memory at ADDR (ADDR should be in kernel)
594 * @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
595 * %REG : fetch register REG
596 * Dereferencing memory fetch:
597 * +|-offs(ARG) : fetch memory at ARG +|- offs address.
598 * Alias name of args:
599 * NAME=FETCHARG : set NAME as alias of FETCHARG.
601 * FETCHARG:TYPE : use TYPE instead of unsigned long.
603 struct trace_kprobe *tk = NULL;
605 bool is_return = false;
606 char *symbol = NULL, *tmp = NULL;
607 const char *event = NULL, *group = KPROBE_EVENT_SYSTEM;
611 char buf[MAX_EVENT_NAME_LEN];
612 unsigned int flags = TPARG_FL_KERNEL;
614 switch (argv[0][0]) {
617 flags |= TPARG_FL_RETURN;
627 trace_probe_log_init("trace_kprobe", argc, argv);
629 event = strchr(&argv[0][1], ':');
633 if (isdigit(argv[0][1])) {
635 trace_probe_log_err(1, MAXACT_NO_KPROBE);
639 len = event - &argv[0][1] - 1;
641 len = strlen(&argv[0][1]);
642 if (len > MAX_EVENT_NAME_LEN - 1) {
643 trace_probe_log_err(1, BAD_MAXACT);
646 memcpy(buf, &argv[0][1], len);
648 ret = kstrtouint(buf, 0, &maxactive);
649 if (ret || !maxactive) {
650 trace_probe_log_err(1, BAD_MAXACT);
653 /* kretprobes instances are iterated over via a list. The
654 * maximum should stay reasonable.
656 if (maxactive > KRETPROBE_MAXACTIVE_MAX) {
657 trace_probe_log_err(1, MAXACT_TOO_BIG);
662 /* try to parse an address. if that fails, try to read the
663 * input as a symbol. */
664 if (kstrtoul(argv[1], 0, (unsigned long *)&addr)) {
665 trace_probe_log_set_index(1);
666 /* Check whether uprobe event specified */
667 if (strchr(argv[1], '/') && strchr(argv[1], ':')) {
671 /* a symbol specified */
672 symbol = kstrdup(argv[1], GFP_KERNEL);
675 /* TODO: support .init module functions */
676 ret = traceprobe_split_symbol_offset(symbol, &offset);
677 if (ret || offset < 0 || offset > UINT_MAX) {
678 trace_probe_log_err(0, BAD_PROBE_ADDR);
681 if (kprobe_on_func_entry(NULL, symbol, offset))
682 flags |= TPARG_FL_FENTRY;
683 if (offset && is_return && !(flags & TPARG_FL_FENTRY)) {
684 trace_probe_log_err(0, BAD_RETPROBE);
689 trace_probe_log_set_index(0);
691 ret = traceprobe_parse_event_name(&event, &group, buf,
696 /* Make a new event name */
698 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
699 is_return ? 'r' : 'p', symbol, offset);
701 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
702 is_return ? 'r' : 'p', addr);
703 sanitize_event_name(buf);
708 tk = alloc_trace_kprobe(group, event, addr, symbol, offset, maxactive,
709 argc - 2, is_return);
712 /* This must return -ENOMEM, else there is a bug */
713 WARN_ON_ONCE(ret != -ENOMEM);
714 goto out; /* We know tk is not allocated */
716 argc -= 2; argv += 2;
718 /* parse arguments */
719 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
720 tmp = kstrdup(argv[i], GFP_KERNEL);
726 trace_probe_log_set_index(i + 2);
727 ret = traceprobe_parse_probe_arg(&tk->tp, i, tmp, flags);
730 goto error; /* This can be -ENOMEM */
733 ret = register_trace_kprobe(tk);
735 trace_probe_log_set_index(1);
737 trace_probe_log_err(0, BAD_INSN_BNDRY);
738 else if (ret == -ENOENT)
739 trace_probe_log_err(0, BAD_PROBE_ADDR);
740 else if (ret != -ENOMEM)
741 trace_probe_log_err(0, FAIL_REG_PROBE);
746 trace_probe_log_clear();
753 free_trace_kprobe(tk);
757 static int create_or_delete_trace_kprobe(int argc, char **argv)
761 if (argv[0][0] == '-')
762 return dyn_event_release(argc, argv, &trace_kprobe_ops);
764 ret = trace_kprobe_create(argc, (const char **)argv);
765 return ret == -ECANCELED ? -EINVAL : ret;
768 static int trace_kprobe_release(struct dyn_event *ev)
770 struct trace_kprobe *tk = to_trace_kprobe(ev);
771 int ret = unregister_trace_kprobe(tk);
774 free_trace_kprobe(tk);
778 static int trace_kprobe_show(struct seq_file *m, struct dyn_event *ev)
780 struct trace_kprobe *tk = to_trace_kprobe(ev);
783 seq_putc(m, trace_kprobe_is_return(tk) ? 'r' : 'p');
784 seq_printf(m, ":%s/%s", tk->tp.call.class->system,
785 trace_event_name(&tk->tp.call));
788 seq_printf(m, " 0x%p", tk->rp.kp.addr);
789 else if (tk->rp.kp.offset)
790 seq_printf(m, " %s+%u", trace_kprobe_symbol(tk),
793 seq_printf(m, " %s", trace_kprobe_symbol(tk));
795 for (i = 0; i < tk->tp.nr_args; i++)
796 seq_printf(m, " %s=%s", tk->tp.args[i].name, tk->tp.args[i].comm);
802 static int probes_seq_show(struct seq_file *m, void *v)
804 struct dyn_event *ev = v;
806 if (!is_trace_kprobe(ev))
809 return trace_kprobe_show(m, ev);
812 static const struct seq_operations probes_seq_op = {
813 .start = dyn_event_seq_start,
814 .next = dyn_event_seq_next,
815 .stop = dyn_event_seq_stop,
816 .show = probes_seq_show
819 static int probes_open(struct inode *inode, struct file *file)
823 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
824 ret = dyn_events_release_all(&trace_kprobe_ops);
829 return seq_open(file, &probes_seq_op);
832 static ssize_t probes_write(struct file *file, const char __user *buffer,
833 size_t count, loff_t *ppos)
835 return trace_parse_run_command(file, buffer, count, ppos,
836 create_or_delete_trace_kprobe);
839 static const struct file_operations kprobe_events_ops = {
840 .owner = THIS_MODULE,
844 .release = seq_release,
845 .write = probes_write,
848 /* Probes profiling interfaces */
849 static int probes_profile_seq_show(struct seq_file *m, void *v)
851 struct dyn_event *ev = v;
852 struct trace_kprobe *tk;
854 if (!is_trace_kprobe(ev))
857 tk = to_trace_kprobe(ev);
858 seq_printf(m, " %-44s %15lu %15lu\n",
859 trace_event_name(&tk->tp.call),
860 trace_kprobe_nhit(tk),
866 static const struct seq_operations profile_seq_op = {
867 .start = dyn_event_seq_start,
868 .next = dyn_event_seq_next,
869 .stop = dyn_event_seq_stop,
870 .show = probes_profile_seq_show
873 static int profile_open(struct inode *inode, struct file *file)
875 return seq_open(file, &profile_seq_op);
878 static const struct file_operations kprobe_profile_ops = {
879 .owner = THIS_MODULE,
880 .open = profile_open,
883 .release = seq_release,
886 /* Kprobe specific fetch functions */
888 /* Return the length of string -- including null terminal byte */
889 static nokprobe_inline int
890 fetch_store_strlen(unsigned long addr)
896 ret = probe_kernel_read(&c, (u8 *)addr + len, 1);
898 } while (c && ret == 0 && len < MAX_STRING_SIZE);
900 return (ret < 0) ? ret : len;
903 /* Return the length of string -- including null terminal byte */
904 static nokprobe_inline int
905 fetch_store_strlen_user(unsigned long addr)
907 const void __user *uaddr = (__force const void __user *)addr;
909 return strnlen_unsafe_user(uaddr, MAX_STRING_SIZE);
913 * Fetch a null-terminated string. Caller MUST set *(u32 *)buf with max
914 * length and relative data location.
916 static nokprobe_inline int
917 fetch_store_string(unsigned long addr, void *dest, void *base)
919 int maxlen = get_loc_len(*(u32 *)dest);
923 if (unlikely(!maxlen))
926 __dest = get_loc_data(dest, base);
929 * Try to get string again, since the string can be changed while
932 ret = strncpy_from_unsafe(__dest, (void *)addr, maxlen);
934 *(u32 *)dest = make_data_loc(ret, __dest - base);
940 * Fetch a null-terminated string from user. Caller MUST set *(u32 *)buf
941 * with max length and relative data location.
943 static nokprobe_inline int
944 fetch_store_string_user(unsigned long addr, void *dest, void *base)
946 const void __user *uaddr = (__force const void __user *)addr;
947 int maxlen = get_loc_len(*(u32 *)dest);
951 if (unlikely(!maxlen))
954 __dest = get_loc_data(dest, base);
956 ret = strncpy_from_unsafe_user(__dest, uaddr, maxlen);
958 *(u32 *)dest = make_data_loc(ret, __dest - base);
963 static nokprobe_inline int
964 probe_mem_read(void *dest, void *src, size_t size)
966 return probe_kernel_read(dest, src, size);
969 static nokprobe_inline int
970 probe_mem_read_user(void *dest, void *src, size_t size)
972 const void __user *uaddr = (__force const void __user *)src;
974 return probe_user_read(dest, uaddr, size);
977 /* Note that we don't verify it, since the code does not come from user space */
979 process_fetch_insn(struct fetch_insn *code, struct pt_regs *regs, void *dest,
985 /* 1st stage: get value from context */
988 val = regs_get_register(regs, code->param);
991 val = regs_get_kernel_stack_nth(regs, code->param);
993 case FETCH_OP_STACKP:
994 val = kernel_stack_pointer(regs);
996 case FETCH_OP_RETVAL:
997 val = regs_return_value(regs);
1000 val = code->immediate;
1003 val = (unsigned long)current->comm;
1005 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
1007 val = regs_get_kernel_argument(regs, code->param);
1010 case FETCH_NOP_SYMBOL: /* Ignore a place holder */
1018 return process_fetch_insn_bottom(code, val, dest, base);
1020 NOKPROBE_SYMBOL(process_fetch_insn)
1022 /* Kprobe handler */
1023 static nokprobe_inline void
1024 __kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs,
1025 struct trace_event_file *trace_file)
1027 struct kprobe_trace_entry_head *entry;
1028 struct ring_buffer_event *event;
1029 struct ring_buffer *buffer;
1030 int size, dsize, pc;
1031 unsigned long irq_flags;
1032 struct trace_event_call *call = &tk->tp.call;
1034 WARN_ON(call != trace_file->event_call);
1036 if (trace_trigger_soft_disabled(trace_file))
1039 local_save_flags(irq_flags);
1040 pc = preempt_count();
1042 dsize = __get_data_size(&tk->tp, regs);
1043 size = sizeof(*entry) + tk->tp.size + dsize;
1045 event = trace_event_buffer_lock_reserve(&buffer, trace_file,
1047 size, irq_flags, pc);
1051 entry = ring_buffer_event_data(event);
1052 entry->ip = (unsigned long)tk->rp.kp.addr;
1053 store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
1055 event_trigger_unlock_commit_regs(trace_file, buffer, event,
1056 entry, irq_flags, pc, regs);
1060 kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs)
1062 struct event_file_link *link;
1064 list_for_each_entry_rcu(link, &tk->tp.files, list)
1065 __kprobe_trace_func(tk, regs, link->file);
1067 NOKPROBE_SYMBOL(kprobe_trace_func);
1069 /* Kretprobe handler */
1070 static nokprobe_inline void
1071 __kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
1072 struct pt_regs *regs,
1073 struct trace_event_file *trace_file)
1075 struct kretprobe_trace_entry_head *entry;
1076 struct ring_buffer_event *event;
1077 struct ring_buffer *buffer;
1078 int size, pc, dsize;
1079 unsigned long irq_flags;
1080 struct trace_event_call *call = &tk->tp.call;
1082 WARN_ON(call != trace_file->event_call);
1084 if (trace_trigger_soft_disabled(trace_file))
1087 local_save_flags(irq_flags);
1088 pc = preempt_count();
1090 dsize = __get_data_size(&tk->tp, regs);
1091 size = sizeof(*entry) + tk->tp.size + dsize;
1093 event = trace_event_buffer_lock_reserve(&buffer, trace_file,
1095 size, irq_flags, pc);
1099 entry = ring_buffer_event_data(event);
1100 entry->func = (unsigned long)tk->rp.kp.addr;
1101 entry->ret_ip = (unsigned long)ri->ret_addr;
1102 store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
1104 event_trigger_unlock_commit_regs(trace_file, buffer, event,
1105 entry, irq_flags, pc, regs);
1109 kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
1110 struct pt_regs *regs)
1112 struct event_file_link *link;
1114 list_for_each_entry_rcu(link, &tk->tp.files, list)
1115 __kretprobe_trace_func(tk, ri, regs, link->file);
1117 NOKPROBE_SYMBOL(kretprobe_trace_func);
1119 /* Event entry printers */
1120 static enum print_line_t
1121 print_kprobe_event(struct trace_iterator *iter, int flags,
1122 struct trace_event *event)
1124 struct kprobe_trace_entry_head *field;
1125 struct trace_seq *s = &iter->seq;
1126 struct trace_probe *tp;
1128 field = (struct kprobe_trace_entry_head *)iter->ent;
1129 tp = container_of(event, struct trace_probe, call.event);
1131 trace_seq_printf(s, "%s: (", trace_event_name(&tp->call));
1133 if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
1136 trace_seq_putc(s, ')');
1138 if (print_probe_args(s, tp->args, tp->nr_args,
1139 (u8 *)&field[1], field) < 0)
1142 trace_seq_putc(s, '\n');
1144 return trace_handle_return(s);
1147 static enum print_line_t
1148 print_kretprobe_event(struct trace_iterator *iter, int flags,
1149 struct trace_event *event)
1151 struct kretprobe_trace_entry_head *field;
1152 struct trace_seq *s = &iter->seq;
1153 struct trace_probe *tp;
1155 field = (struct kretprobe_trace_entry_head *)iter->ent;
1156 tp = container_of(event, struct trace_probe, call.event);
1158 trace_seq_printf(s, "%s: (", trace_event_name(&tp->call));
1160 if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
1163 trace_seq_puts(s, " <- ");
1165 if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
1168 trace_seq_putc(s, ')');
1170 if (print_probe_args(s, tp->args, tp->nr_args,
1171 (u8 *)&field[1], field) < 0)
1174 trace_seq_putc(s, '\n');
1177 return trace_handle_return(s);
1181 static int kprobe_event_define_fields(struct trace_event_call *event_call)
1184 struct kprobe_trace_entry_head field;
1185 struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data;
1187 DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
1189 return traceprobe_define_arg_fields(event_call, sizeof(field), &tk->tp);
1192 static int kretprobe_event_define_fields(struct trace_event_call *event_call)
1195 struct kretprobe_trace_entry_head field;
1196 struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data;
1198 DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
1199 DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
1201 return traceprobe_define_arg_fields(event_call, sizeof(field), &tk->tp);
1204 #ifdef CONFIG_PERF_EVENTS
1206 /* Kprobe profile handler */
1208 kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs)
1210 struct trace_event_call *call = &tk->tp.call;
1211 struct kprobe_trace_entry_head *entry;
1212 struct hlist_head *head;
1213 int size, __size, dsize;
1216 if (bpf_prog_array_valid(call)) {
1217 unsigned long orig_ip = instruction_pointer(regs);
1220 ret = trace_call_bpf(call, regs);
1223 * We need to check and see if we modified the pc of the
1224 * pt_regs, and if so return 1 so that we don't do the
1227 if (orig_ip != instruction_pointer(regs))
1233 head = this_cpu_ptr(call->perf_events);
1234 if (hlist_empty(head))
1237 dsize = __get_data_size(&tk->tp, regs);
1238 __size = sizeof(*entry) + tk->tp.size + dsize;
1239 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1240 size -= sizeof(u32);
1242 entry = perf_trace_buf_alloc(size, NULL, &rctx);
1246 entry->ip = (unsigned long)tk->rp.kp.addr;
1247 memset(&entry[1], 0, dsize);
1248 store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
1249 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1253 NOKPROBE_SYMBOL(kprobe_perf_func);
1255 /* Kretprobe profile handler */
1257 kretprobe_perf_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
1258 struct pt_regs *regs)
1260 struct trace_event_call *call = &tk->tp.call;
1261 struct kretprobe_trace_entry_head *entry;
1262 struct hlist_head *head;
1263 int size, __size, dsize;
1266 if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs))
1269 head = this_cpu_ptr(call->perf_events);
1270 if (hlist_empty(head))
1273 dsize = __get_data_size(&tk->tp, regs);
1274 __size = sizeof(*entry) + tk->tp.size + dsize;
1275 size = ALIGN(__size + sizeof(u32), sizeof(u64));
1276 size -= sizeof(u32);
1278 entry = perf_trace_buf_alloc(size, NULL, &rctx);
1282 entry->func = (unsigned long)tk->rp.kp.addr;
1283 entry->ret_ip = (unsigned long)ri->ret_addr;
1284 store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
1285 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1288 NOKPROBE_SYMBOL(kretprobe_perf_func);
1290 int bpf_get_kprobe_info(const struct perf_event *event, u32 *fd_type,
1291 const char **symbol, u64 *probe_offset,
1292 u64 *probe_addr, bool perf_type_tracepoint)
1294 const char *pevent = trace_event_name(event->tp_event);
1295 const char *group = event->tp_event->class->system;
1296 struct trace_kprobe *tk;
1298 if (perf_type_tracepoint)
1299 tk = find_trace_kprobe(pevent, group);
1301 tk = event->tp_event->data;
1305 *fd_type = trace_kprobe_is_return(tk) ? BPF_FD_TYPE_KRETPROBE
1306 : BPF_FD_TYPE_KPROBE;
1308 *symbol = tk->symbol;
1309 *probe_offset = tk->rp.kp.offset;
1314 *probe_addr = (unsigned long)tk->rp.kp.addr;
1318 #endif /* CONFIG_PERF_EVENTS */
1321 * called by perf_trace_init() or __ftrace_set_clr_event() under event_mutex.
1323 * kprobe_trace_self_tests_init() does enable_trace_probe/disable_trace_probe
1324 * lockless, but we can't race with this __init function.
1326 static int kprobe_register(struct trace_event_call *event,
1327 enum trace_reg type, void *data)
1329 struct trace_kprobe *tk = (struct trace_kprobe *)event->data;
1330 struct trace_event_file *file = data;
1333 case TRACE_REG_REGISTER:
1334 return enable_trace_kprobe(tk, file);
1335 case TRACE_REG_UNREGISTER:
1336 return disable_trace_kprobe(tk, file);
1338 #ifdef CONFIG_PERF_EVENTS
1339 case TRACE_REG_PERF_REGISTER:
1340 return enable_trace_kprobe(tk, NULL);
1341 case TRACE_REG_PERF_UNREGISTER:
1342 return disable_trace_kprobe(tk, NULL);
1343 case TRACE_REG_PERF_OPEN:
1344 case TRACE_REG_PERF_CLOSE:
1345 case TRACE_REG_PERF_ADD:
1346 case TRACE_REG_PERF_DEL:
1353 static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
1355 struct trace_kprobe *tk = container_of(kp, struct trace_kprobe, rp.kp);
1358 raw_cpu_inc(*tk->nhit);
1360 if (tk->tp.flags & TP_FLAG_TRACE)
1361 kprobe_trace_func(tk, regs);
1362 #ifdef CONFIG_PERF_EVENTS
1363 if (tk->tp.flags & TP_FLAG_PROFILE)
1364 ret = kprobe_perf_func(tk, regs);
1368 NOKPROBE_SYMBOL(kprobe_dispatcher);
1371 kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
1373 struct trace_kprobe *tk = container_of(ri->rp, struct trace_kprobe, rp);
1375 raw_cpu_inc(*tk->nhit);
1377 if (tk->tp.flags & TP_FLAG_TRACE)
1378 kretprobe_trace_func(tk, ri, regs);
1379 #ifdef CONFIG_PERF_EVENTS
1380 if (tk->tp.flags & TP_FLAG_PROFILE)
1381 kretprobe_perf_func(tk, ri, regs);
1383 return 0; /* We don't tweek kernel, so just return 0 */
1385 NOKPROBE_SYMBOL(kretprobe_dispatcher);
1387 static struct trace_event_functions kretprobe_funcs = {
1388 .trace = print_kretprobe_event
1391 static struct trace_event_functions kprobe_funcs = {
1392 .trace = print_kprobe_event
1395 static inline void init_trace_event_call(struct trace_kprobe *tk,
1396 struct trace_event_call *call)
1398 INIT_LIST_HEAD(&call->class->fields);
1399 if (trace_kprobe_is_return(tk)) {
1400 call->event.funcs = &kretprobe_funcs;
1401 call->class->define_fields = kretprobe_event_define_fields;
1403 call->event.funcs = &kprobe_funcs;
1404 call->class->define_fields = kprobe_event_define_fields;
1407 call->flags = TRACE_EVENT_FL_KPROBE;
1408 call->class->reg = kprobe_register;
1412 static int register_kprobe_event(struct trace_kprobe *tk)
1414 struct trace_event_call *call = &tk->tp.call;
1417 init_trace_event_call(tk, call);
1419 if (traceprobe_set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0)
1421 ret = register_trace_event(&call->event);
1423 kfree(call->print_fmt);
1426 ret = trace_add_event_call(call);
1428 pr_info("Failed to register kprobe event: %s\n",
1429 trace_event_name(call));
1430 kfree(call->print_fmt);
1431 unregister_trace_event(&call->event);
1436 static int unregister_kprobe_event(struct trace_kprobe *tk)
1440 /* tp->event is unregistered in trace_remove_event_call() */
1441 ret = trace_remove_event_call(&tk->tp.call);
1443 kfree(tk->tp.call.print_fmt);
1447 #ifdef CONFIG_PERF_EVENTS
1448 /* create a trace_kprobe, but don't add it to global lists */
1449 struct trace_event_call *
1450 create_local_trace_kprobe(char *func, void *addr, unsigned long offs,
1453 struct trace_kprobe *tk;
1458 * local trace_kprobes are not added to dyn_event, so they are never
1459 * searched in find_trace_kprobe(). Therefore, there is no concern of
1460 * duplicated name here.
1462 event = func ? func : "DUMMY_EVENT";
1464 tk = alloc_trace_kprobe(KPROBE_EVENT_SYSTEM, event, (void *)addr, func,
1465 offs, 0 /* maxactive */, 0 /* nargs */,
1469 pr_info("Failed to allocate trace_probe.(%d)\n",
1471 return ERR_CAST(tk);
1474 init_trace_event_call(tk, &tk->tp.call);
1476 if (traceprobe_set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0) {
1481 ret = __register_trace_kprobe(tk);
1483 kfree(tk->tp.call.print_fmt);
1487 return &tk->tp.call;
1489 free_trace_kprobe(tk);
1490 return ERR_PTR(ret);
1493 void destroy_local_trace_kprobe(struct trace_event_call *event_call)
1495 struct trace_kprobe *tk;
1497 tk = container_of(event_call, struct trace_kprobe, tp.call);
1499 if (trace_probe_is_enabled(&tk->tp)) {
1504 __unregister_trace_kprobe(tk);
1506 kfree(tk->tp.call.print_fmt);
1507 free_trace_kprobe(tk);
1509 #endif /* CONFIG_PERF_EVENTS */
1511 static __init void enable_boot_kprobe_events(void)
1513 struct trace_array *tr = top_trace_array();
1514 struct trace_event_file *file;
1515 struct trace_kprobe *tk;
1516 struct dyn_event *pos;
1518 mutex_lock(&event_mutex);
1519 for_each_trace_kprobe(tk, pos) {
1520 list_for_each_entry(file, &tr->events, list)
1521 if (file->event_call == &tk->tp.call)
1522 trace_event_enable_disable(file, 1, 0);
1524 mutex_unlock(&event_mutex);
1527 static __init void setup_boot_kprobe_events(void)
1529 char *p, *cmd = kprobe_boot_events_buf;
1532 strreplace(kprobe_boot_events_buf, ',', ' ');
1534 while (cmd && *cmd != '\0') {
1535 p = strchr(cmd, ';');
1539 ret = trace_run_command(cmd, create_or_delete_trace_kprobe);
1541 pr_warn("Failed to add event(%d): %s\n", ret, cmd);
1543 kprobe_boot_events_enabled = true;
1548 enable_boot_kprobe_events();
1551 /* Make a tracefs interface for controlling probe points */
1552 static __init int init_kprobe_trace(void)
1554 struct dentry *d_tracer;
1555 struct dentry *entry;
1558 ret = dyn_event_register(&trace_kprobe_ops);
1562 if (register_module_notifier(&trace_kprobe_module_nb))
1565 d_tracer = tracing_init_dentry();
1566 if (IS_ERR(d_tracer))
1569 entry = tracefs_create_file("kprobe_events", 0644, d_tracer,
1570 NULL, &kprobe_events_ops);
1572 /* Event list interface */
1574 pr_warn("Could not create tracefs 'kprobe_events' entry\n");
1576 /* Profile interface */
1577 entry = tracefs_create_file("kprobe_profile", 0444, d_tracer,
1578 NULL, &kprobe_profile_ops);
1581 pr_warn("Could not create tracefs 'kprobe_profile' entry\n");
1583 setup_boot_kprobe_events();
1587 fs_initcall(init_kprobe_trace);
1590 #ifdef CONFIG_FTRACE_STARTUP_TEST
1591 static __init struct trace_event_file *
1592 find_trace_probe_file(struct trace_kprobe *tk, struct trace_array *tr)
1594 struct trace_event_file *file;
1596 list_for_each_entry(file, &tr->events, list)
1597 if (file->event_call == &tk->tp.call)
1604 * Nobody but us can call enable_trace_kprobe/disable_trace_kprobe at this
1605 * stage, we can do this lockless.
1607 static __init int kprobe_trace_self_tests_init(void)
1610 int (*target)(int, int, int, int, int, int);
1611 struct trace_kprobe *tk;
1612 struct trace_event_file *file;
1614 if (tracing_is_disabled())
1617 if (kprobe_boot_events_enabled) {
1618 pr_info("Skipping kprobe tests due to kprobe_event on cmdline\n");
1622 target = kprobe_trace_selftest_target;
1624 pr_info("Testing kprobe tracing: ");
1626 ret = trace_run_command("p:testprobe kprobe_trace_selftest_target $stack $stack0 +0($stack)",
1627 create_or_delete_trace_kprobe);
1628 if (WARN_ON_ONCE(ret)) {
1629 pr_warn("error on probing function entry.\n");
1632 /* Enable trace point */
1633 tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
1634 if (WARN_ON_ONCE(tk == NULL)) {
1635 pr_warn("error on getting new probe.\n");
1638 file = find_trace_probe_file(tk, top_trace_array());
1639 if (WARN_ON_ONCE(file == NULL)) {
1640 pr_warn("error on getting probe file.\n");
1643 enable_trace_kprobe(tk, file);
1647 ret = trace_run_command("r:testprobe2 kprobe_trace_selftest_target $retval",
1648 create_or_delete_trace_kprobe);
1649 if (WARN_ON_ONCE(ret)) {
1650 pr_warn("error on probing function return.\n");
1653 /* Enable trace point */
1654 tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
1655 if (WARN_ON_ONCE(tk == NULL)) {
1656 pr_warn("error on getting 2nd new probe.\n");
1659 file = find_trace_probe_file(tk, top_trace_array());
1660 if (WARN_ON_ONCE(file == NULL)) {
1661 pr_warn("error on getting probe file.\n");
1664 enable_trace_kprobe(tk, file);
1671 ret = target(1, 2, 3, 4, 5, 6);
1674 * Not expecting an error here, the check is only to prevent the
1675 * optimizer from removing the call to target() as otherwise there
1676 * are no side-effects and the call is never performed.
1681 /* Disable trace points before removing it */
1682 tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
1683 if (WARN_ON_ONCE(tk == NULL)) {
1684 pr_warn("error on getting test probe.\n");
1687 if (trace_kprobe_nhit(tk) != 1) {
1688 pr_warn("incorrect number of testprobe hits\n");
1692 file = find_trace_probe_file(tk, top_trace_array());
1693 if (WARN_ON_ONCE(file == NULL)) {
1694 pr_warn("error on getting probe file.\n");
1697 disable_trace_kprobe(tk, file);
1700 tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
1701 if (WARN_ON_ONCE(tk == NULL)) {
1702 pr_warn("error on getting 2nd test probe.\n");
1705 if (trace_kprobe_nhit(tk) != 1) {
1706 pr_warn("incorrect number of testprobe2 hits\n");
1710 file = find_trace_probe_file(tk, top_trace_array());
1711 if (WARN_ON_ONCE(file == NULL)) {
1712 pr_warn("error on getting probe file.\n");
1715 disable_trace_kprobe(tk, file);
1718 ret = trace_run_command("-:testprobe", create_or_delete_trace_kprobe);
1719 if (WARN_ON_ONCE(ret)) {
1720 pr_warn("error on deleting a probe.\n");
1724 ret = trace_run_command("-:testprobe2", create_or_delete_trace_kprobe);
1725 if (WARN_ON_ONCE(ret)) {
1726 pr_warn("error on deleting a probe.\n");
1731 ret = dyn_events_release_all(&trace_kprobe_ops);
1732 if (WARN_ON_ONCE(ret)) {
1733 pr_warn("error on cleaning up probes.\n");
1737 * Wait for the optimizer work to finish. Otherwise it might fiddle
1738 * with probes in already freed __init text.
1740 wait_for_kprobe_optimizer();
1742 pr_cont("NG: Some tests are failed. Please check them.\n");
1748 late_initcall(kprobe_trace_self_tests_init);