1 // SPDX-License-Identifier: GPL-2.0
3 * uprobes-based tracing events
5 * Copyright (C) IBM Corporation, 2010-2012
6 * Author: Srikar Dronamraju <srikar@linux.vnet.ibm.com>
8 #define pr_fmt(fmt) "trace_uprobe: " fmt
10 #include <linux/security.h>
11 #include <linux/ctype.h>
12 #include <linux/module.h>
13 #include <linux/uaccess.h>
14 #include <linux/uprobes.h>
15 #include <linux/namei.h>
16 #include <linux/string.h>
17 #include <linux/rculist.h>
19 #include "trace_dynevent.h"
20 #include "trace_probe.h"
21 #include "trace_probe_tmpl.h"
23 #define UPROBE_EVENT_SYSTEM "uprobes"
25 struct uprobe_trace_entry_head {
26 struct trace_entry ent;
27 unsigned long vaddr[];
30 #define SIZEOF_TRACE_ENTRY(is_return) \
31 (sizeof(struct uprobe_trace_entry_head) + \
32 sizeof(unsigned long) * (is_return ? 2 : 1))
34 #define DATAOF_TRACE_ENTRY(entry, is_return) \
35 ((void*)(entry) + SIZEOF_TRACE_ENTRY(is_return))
37 static int trace_uprobe_create(const char *raw_command);
38 static int trace_uprobe_show(struct seq_file *m, struct dyn_event *ev);
39 static int trace_uprobe_release(struct dyn_event *ev);
40 static bool trace_uprobe_is_busy(struct dyn_event *ev);
41 static bool trace_uprobe_match(const char *system, const char *event,
42 int argc, const char **argv, struct dyn_event *ev);
44 static struct dyn_event_operations trace_uprobe_ops = {
45 .create = trace_uprobe_create,
46 .show = trace_uprobe_show,
47 .is_busy = trace_uprobe_is_busy,
48 .free = trace_uprobe_release,
49 .match = trace_uprobe_match,
53 * uprobe event core functions
56 struct dyn_event devent;
57 struct uprobe_consumer consumer;
62 unsigned long ref_ctr_offset;
64 struct trace_probe tp;
67 static bool is_trace_uprobe(struct dyn_event *ev)
69 return ev->ops == &trace_uprobe_ops;
72 static struct trace_uprobe *to_trace_uprobe(struct dyn_event *ev)
74 return container_of(ev, struct trace_uprobe, devent);
78 * for_each_trace_uprobe - iterate over the trace_uprobe list
79 * @pos: the struct trace_uprobe * for each entry
80 * @dpos: the struct dyn_event * to use as a loop cursor
82 #define for_each_trace_uprobe(pos, dpos) \
83 for_each_dyn_event(dpos) \
84 if (is_trace_uprobe(dpos) && (pos = to_trace_uprobe(dpos)))
86 static int register_uprobe_event(struct trace_uprobe *tu);
87 static int unregister_uprobe_event(struct trace_uprobe *tu);
89 struct uprobe_dispatch_data {
90 struct trace_uprobe *tu;
91 unsigned long bp_addr;
94 static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs);
95 static int uretprobe_dispatcher(struct uprobe_consumer *con,
96 unsigned long func, struct pt_regs *regs);
98 #ifdef CONFIG_STACK_GROWSUP
99 static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n)
101 return addr - (n * sizeof(long));
104 static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n)
106 return addr + (n * sizeof(long));
110 static unsigned long get_user_stack_nth(struct pt_regs *regs, unsigned int n)
113 unsigned long addr = user_stack_pointer(regs);
115 addr = adjust_stack_addr(addr, n);
117 if (copy_from_user(&ret, (void __force __user *) addr, sizeof(ret)))
124 * Uprobes-specific fetch functions
126 static nokprobe_inline int
127 probe_mem_read(void *dest, void *src, size_t size)
129 void __user *vaddr = (void __force __user *)src;
131 return copy_from_user(dest, vaddr, size) ? -EFAULT : 0;
134 static nokprobe_inline int
135 probe_mem_read_user(void *dest, void *src, size_t size)
137 return probe_mem_read(dest, src, size);
141 * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
142 * length and relative data location.
144 static nokprobe_inline int
145 fetch_store_string(unsigned long addr, void *dest, void *base)
148 u32 loc = *(u32 *)dest;
149 int maxlen = get_loc_len(loc);
150 u8 *dst = get_loc_data(dest, base);
151 void __user *src = (void __force __user *) addr;
153 if (unlikely(!maxlen))
156 if (addr == FETCH_TOKEN_COMM)
157 ret = strlcpy(dst, current->comm, maxlen);
159 ret = strncpy_from_user(dst, src, maxlen);
165 * Include the terminating null byte. In this case it
166 * was copied by strncpy_from_user but not accounted
170 *(u32 *)dest = make_data_loc(ret, (void *)dst - base);
172 *(u32 *)dest = make_data_loc(0, (void *)dst - base);
177 static nokprobe_inline int
178 fetch_store_string_user(unsigned long addr, void *dest, void *base)
180 return fetch_store_string(addr, dest, base);
183 /* Return the length of string -- including null terminal byte */
184 static nokprobe_inline int
185 fetch_store_strlen(unsigned long addr)
188 void __user *vaddr = (void __force __user *) addr;
190 if (addr == FETCH_TOKEN_COMM)
191 len = strlen(current->comm) + 1;
193 len = strnlen_user(vaddr, MAX_STRING_SIZE);
195 return (len > MAX_STRING_SIZE) ? 0 : len;
198 static nokprobe_inline int
199 fetch_store_strlen_user(unsigned long addr)
201 return fetch_store_strlen(addr);
204 static unsigned long translate_user_vaddr(unsigned long file_offset)
206 unsigned long base_addr;
207 struct uprobe_dispatch_data *udd;
209 udd = (void *) current->utask->vaddr;
211 base_addr = udd->bp_addr - udd->tu->offset;
212 return base_addr + file_offset;
215 /* Note that we don't verify it, since the code does not come from user space */
217 process_fetch_insn(struct fetch_insn *code, void *rec, void *dest,
220 struct pt_regs *regs = rec;
223 /* 1st stage: get value from context */
226 val = regs_get_register(regs, code->param);
229 val = get_user_stack_nth(regs, code->param);
231 case FETCH_OP_STACKP:
232 val = user_stack_pointer(regs);
234 case FETCH_OP_RETVAL:
235 val = regs_return_value(regs);
238 val = code->immediate;
241 val = FETCH_TOKEN_COMM;
244 val = (unsigned long)code->data;
247 val = translate_user_vaddr(code->immediate);
254 return process_fetch_insn_bottom(code, val, dest, base);
256 NOKPROBE_SYMBOL(process_fetch_insn)
258 static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter)
260 rwlock_init(&filter->rwlock);
261 filter->nr_systemwide = 0;
262 INIT_LIST_HEAD(&filter->perf_events);
265 static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter *filter)
267 return !filter->nr_systemwide && list_empty(&filter->perf_events);
270 static inline bool is_ret_probe(struct trace_uprobe *tu)
272 return tu->consumer.ret_handler != NULL;
275 static bool trace_uprobe_is_busy(struct dyn_event *ev)
277 struct trace_uprobe *tu = to_trace_uprobe(ev);
279 return trace_probe_is_enabled(&tu->tp);
282 static bool trace_uprobe_match_command_head(struct trace_uprobe *tu,
283 int argc, const char **argv)
285 char buf[MAX_ARGSTR_LEN + 1];
291 len = strlen(tu->filename);
292 if (strncmp(tu->filename, argv[0], len) || argv[0][len] != ':')
295 if (tu->ref_ctr_offset == 0)
296 snprintf(buf, sizeof(buf), "0x%0*lx",
297 (int)(sizeof(void *) * 2), tu->offset);
299 snprintf(buf, sizeof(buf), "0x%0*lx(0x%lx)",
300 (int)(sizeof(void *) * 2), tu->offset,
302 if (strcmp(buf, &argv[0][len + 1]))
307 return trace_probe_match_command_args(&tu->tp, argc, argv);
310 static bool trace_uprobe_match(const char *system, const char *event,
311 int argc, const char **argv, struct dyn_event *ev)
313 struct trace_uprobe *tu = to_trace_uprobe(ev);
315 return strcmp(trace_probe_name(&tu->tp), event) == 0 &&
316 (!system || strcmp(trace_probe_group_name(&tu->tp), system) == 0) &&
317 trace_uprobe_match_command_head(tu, argc, argv);
320 static nokprobe_inline struct trace_uprobe *
321 trace_uprobe_primary_from_call(struct trace_event_call *call)
323 struct trace_probe *tp;
325 tp = trace_probe_primary_from_call(call);
326 if (WARN_ON_ONCE(!tp))
329 return container_of(tp, struct trace_uprobe, tp);
333 * Allocate new trace_uprobe and initialize it (including uprobes).
335 static struct trace_uprobe *
336 alloc_trace_uprobe(const char *group, const char *event, int nargs, bool is_ret)
338 struct trace_uprobe *tu;
341 tu = kzalloc(struct_size(tu, tp.args, nargs), GFP_KERNEL);
343 return ERR_PTR(-ENOMEM);
345 ret = trace_probe_init(&tu->tp, event, group, true);
349 dyn_event_init(&tu->devent, &trace_uprobe_ops);
350 tu->consumer.handler = uprobe_dispatcher;
352 tu->consumer.ret_handler = uretprobe_dispatcher;
353 init_trace_uprobe_filter(tu->tp.event->filter);
362 static void free_trace_uprobe(struct trace_uprobe *tu)
368 trace_probe_cleanup(&tu->tp);
373 static struct trace_uprobe *find_probe_event(const char *event, const char *group)
375 struct dyn_event *pos;
376 struct trace_uprobe *tu;
378 for_each_trace_uprobe(tu, pos)
379 if (strcmp(trace_probe_name(&tu->tp), event) == 0 &&
380 strcmp(trace_probe_group_name(&tu->tp), group) == 0)
386 /* Unregister a trace_uprobe and probe_event */
387 static int unregister_trace_uprobe(struct trace_uprobe *tu)
391 if (trace_probe_has_sibling(&tu->tp))
394 /* If there's a reference to the dynamic event */
395 if (trace_event_dyn_busy(trace_probe_event_call(&tu->tp)))
398 ret = unregister_uprobe_event(tu);
403 dyn_event_remove(&tu->devent);
404 trace_probe_unlink(&tu->tp);
405 free_trace_uprobe(tu);
409 static bool trace_uprobe_has_same_uprobe(struct trace_uprobe *orig,
410 struct trace_uprobe *comp)
412 struct trace_probe_event *tpe = orig->tp.event;
413 struct trace_probe *pos;
414 struct inode *comp_inode = d_real_inode(comp->path.dentry);
417 list_for_each_entry(pos, &tpe->probes, list) {
418 orig = container_of(pos, struct trace_uprobe, tp);
419 if (comp_inode != d_real_inode(orig->path.dentry) ||
420 comp->offset != orig->offset)
424 * trace_probe_compare_arg_type() ensured that nr_args and
425 * each argument name and type are same. Let's compare comm.
427 for (i = 0; i < orig->tp.nr_args; i++) {
428 if (strcmp(orig->tp.args[i].comm,
429 comp->tp.args[i].comm))
433 if (i == orig->tp.nr_args)
440 static int append_trace_uprobe(struct trace_uprobe *tu, struct trace_uprobe *to)
444 ret = trace_probe_compare_arg_type(&tu->tp, &to->tp);
446 /* Note that argument starts index = 2 */
447 trace_probe_log_set_index(ret + 1);
448 trace_probe_log_err(0, DIFF_ARG_TYPE);
451 if (trace_uprobe_has_same_uprobe(to, tu)) {
452 trace_probe_log_set_index(0);
453 trace_probe_log_err(0, SAME_PROBE);
457 /* Append to existing event */
458 ret = trace_probe_append(&tu->tp, &to->tp);
460 dyn_event_add(&tu->devent, trace_probe_event_call(&tu->tp));
466 * Uprobe with multiple reference counter is not allowed. i.e.
467 * If inode and offset matches, reference counter offset *must*
468 * match as well. Though, there is one exception: If user is
469 * replacing old trace_uprobe with new one(same group/event),
470 * then we allow same uprobe with new reference counter as far
471 * as the new one does not conflict with any other existing
474 static int validate_ref_ctr_offset(struct trace_uprobe *new)
476 struct dyn_event *pos;
477 struct trace_uprobe *tmp;
478 struct inode *new_inode = d_real_inode(new->path.dentry);
480 for_each_trace_uprobe(tmp, pos) {
481 if (new_inode == d_real_inode(tmp->path.dentry) &&
482 new->offset == tmp->offset &&
483 new->ref_ctr_offset != tmp->ref_ctr_offset) {
484 pr_warn("Reference counter offset mismatch.");
491 /* Register a trace_uprobe and probe_event */
492 static int register_trace_uprobe(struct trace_uprobe *tu)
494 struct trace_uprobe *old_tu;
497 mutex_lock(&event_mutex);
499 ret = validate_ref_ctr_offset(tu);
503 /* register as an event */
504 old_tu = find_probe_event(trace_probe_name(&tu->tp),
505 trace_probe_group_name(&tu->tp));
507 if (is_ret_probe(tu) != is_ret_probe(old_tu)) {
508 trace_probe_log_set_index(0);
509 trace_probe_log_err(0, DIFF_PROBE_TYPE);
512 ret = append_trace_uprobe(tu, old_tu);
517 ret = register_uprobe_event(tu);
519 if (ret == -EEXIST) {
520 trace_probe_log_set_index(0);
521 trace_probe_log_err(0, EVENT_EXIST);
523 pr_warn("Failed to register probe event(%d)\n", ret);
527 dyn_event_add(&tu->devent, trace_probe_event_call(&tu->tp));
530 mutex_unlock(&event_mutex);
537 * - Add uprobe: p|r[:[GRP/]EVENT] PATH:OFFSET[%return][(REF)] [FETCHARGS]
539 static int __trace_uprobe_create(int argc, const char **argv)
541 struct trace_uprobe *tu;
542 const char *event = NULL, *group = UPROBE_EVENT_SYSTEM;
543 char *arg, *filename, *rctr, *rctr_end, *tmp;
544 char buf[MAX_EVENT_NAME_LEN];
545 enum probe_print_type ptype;
547 unsigned long offset, ref_ctr_offset;
548 bool is_return = false;
554 switch (argv[0][0]) {
567 if (argv[0][1] == ':')
570 if (!strchr(argv[1], '/'))
573 filename = kstrdup(argv[1], GFP_KERNEL);
577 /* Find the last occurrence, in case the path contains ':' too. */
578 arg = strrchr(filename, ':');
579 if (!arg || !isdigit(arg[1])) {
584 trace_probe_log_init("trace_uprobe", argc, argv);
585 trace_probe_log_set_index(1); /* filename is the 2nd argument */
588 ret = kern_path(filename, LOOKUP_FOLLOW, &path);
590 trace_probe_log_err(0, FILE_NOT_FOUND);
592 trace_probe_log_clear();
595 if (!d_is_reg(path.dentry)) {
596 trace_probe_log_err(0, NO_REGULAR_FILE);
598 goto fail_address_parse;
601 /* Parse reference counter offset if specified. */
602 rctr = strchr(arg, '(');
604 rctr_end = strchr(rctr, ')');
607 rctr_end = rctr + strlen(rctr);
608 trace_probe_log_err(rctr_end - filename,
610 goto fail_address_parse;
611 } else if (rctr_end[1] != '\0') {
613 trace_probe_log_err(rctr_end + 1 - filename,
615 goto fail_address_parse;
620 ret = kstrtoul(rctr, 0, &ref_ctr_offset);
622 trace_probe_log_err(rctr - filename, BAD_REFCNT);
623 goto fail_address_parse;
627 /* Check if there is %return suffix */
628 tmp = strchr(arg, '%');
630 if (!strcmp(tmp, "%return")) {
634 trace_probe_log_err(tmp - filename, BAD_ADDR_SUFFIX);
636 goto fail_address_parse;
640 /* Parse uprobe offset. */
641 ret = kstrtoul(arg, 0, &offset);
643 trace_probe_log_err(arg - filename, BAD_UPROBE_OFFS);
644 goto fail_address_parse;
648 trace_probe_log_set_index(0);
650 ret = traceprobe_parse_event_name(&event, &group, buf,
653 goto fail_address_parse;
658 tail = kstrdup(kbasename(filename), GFP_KERNEL);
661 goto fail_address_parse;
664 ptr = strpbrk(tail, ".-_");
668 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset);
676 tu = alloc_trace_uprobe(group, event, argc, is_return);
679 /* This must return -ENOMEM otherwise there is a bug */
680 WARN_ON_ONCE(ret != -ENOMEM);
681 goto fail_address_parse;
684 tu->ref_ctr_offset = ref_ctr_offset;
686 tu->filename = filename;
688 /* parse arguments */
689 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
690 trace_probe_log_set_index(i + 2);
691 ret = traceprobe_parse_probe_arg(&tu->tp, i, argv[i],
692 is_return ? TPARG_FL_RETURN : 0);
697 ptype = is_ret_probe(tu) ? PROBE_PRINT_RETURN : PROBE_PRINT_NORMAL;
698 ret = traceprobe_set_print_fmt(&tu->tp, ptype);
702 ret = register_trace_uprobe(tu);
707 free_trace_uprobe(tu);
709 trace_probe_log_clear();
713 trace_probe_log_clear();
720 int trace_uprobe_create(const char *raw_command)
722 return trace_probe_create(raw_command, __trace_uprobe_create);
725 static int create_or_delete_trace_uprobe(const char *raw_command)
729 if (raw_command[0] == '-')
730 return dyn_event_release(raw_command, &trace_uprobe_ops);
732 ret = trace_uprobe_create(raw_command);
733 return ret == -ECANCELED ? -EINVAL : ret;
736 static int trace_uprobe_release(struct dyn_event *ev)
738 struct trace_uprobe *tu = to_trace_uprobe(ev);
740 return unregister_trace_uprobe(tu);
743 /* Probes listing interfaces */
744 static int trace_uprobe_show(struct seq_file *m, struct dyn_event *ev)
746 struct trace_uprobe *tu = to_trace_uprobe(ev);
747 char c = is_ret_probe(tu) ? 'r' : 'p';
750 seq_printf(m, "%c:%s/%s %s:0x%0*lx", c, trace_probe_group_name(&tu->tp),
751 trace_probe_name(&tu->tp), tu->filename,
752 (int)(sizeof(void *) * 2), tu->offset);
754 if (tu->ref_ctr_offset)
755 seq_printf(m, "(0x%lx)", tu->ref_ctr_offset);
757 for (i = 0; i < tu->tp.nr_args; i++)
758 seq_printf(m, " %s=%s", tu->tp.args[i].name, tu->tp.args[i].comm);
764 static int probes_seq_show(struct seq_file *m, void *v)
766 struct dyn_event *ev = v;
768 if (!is_trace_uprobe(ev))
771 return trace_uprobe_show(m, ev);
774 static const struct seq_operations probes_seq_op = {
775 .start = dyn_event_seq_start,
776 .next = dyn_event_seq_next,
777 .stop = dyn_event_seq_stop,
778 .show = probes_seq_show
781 static int probes_open(struct inode *inode, struct file *file)
785 ret = security_locked_down(LOCKDOWN_TRACEFS);
789 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
790 ret = dyn_events_release_all(&trace_uprobe_ops);
795 return seq_open(file, &probes_seq_op);
798 static ssize_t probes_write(struct file *file, const char __user *buffer,
799 size_t count, loff_t *ppos)
801 return trace_parse_run_command(file, buffer, count, ppos,
802 create_or_delete_trace_uprobe);
805 static const struct file_operations uprobe_events_ops = {
806 .owner = THIS_MODULE,
810 .release = seq_release,
811 .write = probes_write,
814 /* Probes profiling interfaces */
815 static int probes_profile_seq_show(struct seq_file *m, void *v)
817 struct dyn_event *ev = v;
818 struct trace_uprobe *tu;
820 if (!is_trace_uprobe(ev))
823 tu = to_trace_uprobe(ev);
824 seq_printf(m, " %s %-44s %15lu\n", tu->filename,
825 trace_probe_name(&tu->tp), tu->nhit);
829 static const struct seq_operations profile_seq_op = {
830 .start = dyn_event_seq_start,
831 .next = dyn_event_seq_next,
832 .stop = dyn_event_seq_stop,
833 .show = probes_profile_seq_show
836 static int profile_open(struct inode *inode, struct file *file)
840 ret = security_locked_down(LOCKDOWN_TRACEFS);
844 return seq_open(file, &profile_seq_op);
847 static const struct file_operations uprobe_profile_ops = {
848 .owner = THIS_MODULE,
849 .open = profile_open,
852 .release = seq_release,
855 struct uprobe_cpu_buffer {
859 static struct uprobe_cpu_buffer __percpu *uprobe_cpu_buffer;
860 static int uprobe_buffer_refcnt;
862 static int uprobe_buffer_init(void)
866 uprobe_cpu_buffer = alloc_percpu(struct uprobe_cpu_buffer);
867 if (uprobe_cpu_buffer == NULL)
870 for_each_possible_cpu(cpu) {
871 struct page *p = alloc_pages_node(cpu_to_node(cpu),
877 per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf = page_address(p);
878 mutex_init(&per_cpu_ptr(uprobe_cpu_buffer, cpu)->mutex);
884 for_each_possible_cpu(cpu) {
887 free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf);
890 free_percpu(uprobe_cpu_buffer);
894 static int uprobe_buffer_enable(void)
898 BUG_ON(!mutex_is_locked(&event_mutex));
900 if (uprobe_buffer_refcnt++ == 0) {
901 ret = uprobe_buffer_init();
903 uprobe_buffer_refcnt--;
909 static void uprobe_buffer_disable(void)
913 BUG_ON(!mutex_is_locked(&event_mutex));
915 if (--uprobe_buffer_refcnt == 0) {
916 for_each_possible_cpu(cpu)
917 free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer,
920 free_percpu(uprobe_cpu_buffer);
921 uprobe_cpu_buffer = NULL;
925 static struct uprobe_cpu_buffer *uprobe_buffer_get(void)
927 struct uprobe_cpu_buffer *ucb;
930 cpu = raw_smp_processor_id();
931 ucb = per_cpu_ptr(uprobe_cpu_buffer, cpu);
934 * Use per-cpu buffers for fastest access, but we might migrate
935 * so the mutex makes sure we have sole access to it.
937 mutex_lock(&ucb->mutex);
942 static void uprobe_buffer_put(struct uprobe_cpu_buffer *ucb)
944 mutex_unlock(&ucb->mutex);
947 static void __uprobe_trace_func(struct trace_uprobe *tu,
948 unsigned long func, struct pt_regs *regs,
949 struct uprobe_cpu_buffer *ucb, int dsize,
950 struct trace_event_file *trace_file)
952 struct uprobe_trace_entry_head *entry;
953 struct trace_buffer *buffer;
954 struct ring_buffer_event *event;
957 struct trace_event_call *call = trace_probe_event_call(&tu->tp);
959 WARN_ON(call != trace_file->event_call);
961 if (WARN_ON_ONCE(tu->tp.size + dsize > PAGE_SIZE))
964 if (trace_trigger_soft_disabled(trace_file))
967 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
968 size = esize + tu->tp.size + dsize;
969 event = trace_event_buffer_lock_reserve(&buffer, trace_file,
970 call->event.type, size, 0);
974 entry = ring_buffer_event_data(event);
975 if (is_ret_probe(tu)) {
976 entry->vaddr[0] = func;
977 entry->vaddr[1] = instruction_pointer(regs);
978 data = DATAOF_TRACE_ENTRY(entry, true);
980 entry->vaddr[0] = instruction_pointer(regs);
981 data = DATAOF_TRACE_ENTRY(entry, false);
984 memcpy(data, ucb->buf, tu->tp.size + dsize);
986 event_trigger_unlock_commit(trace_file, buffer, event, entry, 0);
990 static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs,
991 struct uprobe_cpu_buffer *ucb, int dsize)
993 struct event_file_link *link;
995 if (is_ret_probe(tu))
999 trace_probe_for_each_link_rcu(link, &tu->tp)
1000 __uprobe_trace_func(tu, 0, regs, ucb, dsize, link->file);
1006 static void uretprobe_trace_func(struct trace_uprobe *tu, unsigned long func,
1007 struct pt_regs *regs,
1008 struct uprobe_cpu_buffer *ucb, int dsize)
1010 struct event_file_link *link;
1013 trace_probe_for_each_link_rcu(link, &tu->tp)
1014 __uprobe_trace_func(tu, func, regs, ucb, dsize, link->file);
1018 /* Event entry printers */
1019 static enum print_line_t
1020 print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event)
1022 struct uprobe_trace_entry_head *entry;
1023 struct trace_seq *s = &iter->seq;
1024 struct trace_uprobe *tu;
1027 entry = (struct uprobe_trace_entry_head *)iter->ent;
1028 tu = trace_uprobe_primary_from_call(
1029 container_of(event, struct trace_event_call, event));
1033 if (is_ret_probe(tu)) {
1034 trace_seq_printf(s, "%s: (0x%lx <- 0x%lx)",
1035 trace_probe_name(&tu->tp),
1036 entry->vaddr[1], entry->vaddr[0]);
1037 data = DATAOF_TRACE_ENTRY(entry, true);
1039 trace_seq_printf(s, "%s: (0x%lx)",
1040 trace_probe_name(&tu->tp),
1042 data = DATAOF_TRACE_ENTRY(entry, false);
1045 if (print_probe_args(s, tu->tp.args, tu->tp.nr_args, data, entry) < 0)
1048 trace_seq_putc(s, '\n');
1051 return trace_handle_return(s);
1054 typedef bool (*filter_func_t)(struct uprobe_consumer *self,
1055 enum uprobe_filter_ctx ctx,
1056 struct mm_struct *mm);
1058 static int trace_uprobe_enable(struct trace_uprobe *tu, filter_func_t filter)
1062 tu->consumer.filter = filter;
1063 tu->inode = d_real_inode(tu->path.dentry);
1065 if (tu->ref_ctr_offset)
1066 ret = uprobe_register_refctr(tu->inode, tu->offset,
1067 tu->ref_ctr_offset, &tu->consumer);
1069 ret = uprobe_register(tu->inode, tu->offset, &tu->consumer);
1077 static void __probe_event_disable(struct trace_probe *tp)
1079 struct trace_probe *pos;
1080 struct trace_uprobe *tu;
1082 tu = container_of(tp, struct trace_uprobe, tp);
1083 WARN_ON(!uprobe_filter_is_empty(tu->tp.event->filter));
1085 list_for_each_entry(pos, trace_probe_probe_list(tp), list) {
1086 tu = container_of(pos, struct trace_uprobe, tp);
1090 uprobe_unregister(tu->inode, tu->offset, &tu->consumer);
1095 static int probe_event_enable(struct trace_event_call *call,
1096 struct trace_event_file *file, filter_func_t filter)
1098 struct trace_probe *pos, *tp;
1099 struct trace_uprobe *tu;
1103 tp = trace_probe_primary_from_call(call);
1104 if (WARN_ON_ONCE(!tp))
1106 enabled = trace_probe_is_enabled(tp);
1108 /* This may also change "enabled" state */
1110 if (trace_probe_test_flag(tp, TP_FLAG_PROFILE))
1113 ret = trace_probe_add_file(tp, file);
1117 if (trace_probe_test_flag(tp, TP_FLAG_TRACE))
1120 trace_probe_set_flag(tp, TP_FLAG_PROFILE);
1123 tu = container_of(tp, struct trace_uprobe, tp);
1124 WARN_ON(!uprobe_filter_is_empty(tu->tp.event->filter));
1129 ret = uprobe_buffer_enable();
1133 list_for_each_entry(pos, trace_probe_probe_list(tp), list) {
1134 tu = container_of(pos, struct trace_uprobe, tp);
1135 ret = trace_uprobe_enable(tu, filter);
1137 __probe_event_disable(tp);
1145 uprobe_buffer_disable();
1149 trace_probe_remove_file(tp, file);
1151 trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
1156 static void probe_event_disable(struct trace_event_call *call,
1157 struct trace_event_file *file)
1159 struct trace_probe *tp;
1161 tp = trace_probe_primary_from_call(call);
1162 if (WARN_ON_ONCE(!tp))
1165 if (!trace_probe_is_enabled(tp))
1169 if (trace_probe_remove_file(tp, file) < 0)
1172 if (trace_probe_is_enabled(tp))
1175 trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
1177 __probe_event_disable(tp);
1178 uprobe_buffer_disable();
1181 static int uprobe_event_define_fields(struct trace_event_call *event_call)
1184 struct uprobe_trace_entry_head field;
1185 struct trace_uprobe *tu;
1187 tu = trace_uprobe_primary_from_call(event_call);
1191 if (is_ret_probe(tu)) {
1192 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_FUNC, 0);
1193 DEFINE_FIELD(unsigned long, vaddr[1], FIELD_STRING_RETIP, 0);
1194 size = SIZEOF_TRACE_ENTRY(true);
1196 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0);
1197 size = SIZEOF_TRACE_ENTRY(false);
1200 return traceprobe_define_arg_fields(event_call, size, &tu->tp);
1203 #ifdef CONFIG_PERF_EVENTS
1205 __uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm)
1207 struct perf_event *event;
1209 if (filter->nr_systemwide)
1212 list_for_each_entry(event, &filter->perf_events, hw.tp_list) {
1213 if (event->hw.target->mm == mm)
1221 trace_uprobe_filter_event(struct trace_uprobe_filter *filter,
1222 struct perf_event *event)
1224 return __uprobe_perf_filter(filter, event->hw.target->mm);
1227 static bool trace_uprobe_filter_remove(struct trace_uprobe_filter *filter,
1228 struct perf_event *event)
1232 write_lock(&filter->rwlock);
1233 if (event->hw.target) {
1234 list_del(&event->hw.tp_list);
1235 done = filter->nr_systemwide ||
1236 (event->hw.target->flags & PF_EXITING) ||
1237 trace_uprobe_filter_event(filter, event);
1239 filter->nr_systemwide--;
1240 done = filter->nr_systemwide;
1242 write_unlock(&filter->rwlock);
1247 /* This returns true if the filter always covers target mm */
1248 static bool trace_uprobe_filter_add(struct trace_uprobe_filter *filter,
1249 struct perf_event *event)
1253 write_lock(&filter->rwlock);
1254 if (event->hw.target) {
1256 * event->parent != NULL means copy_process(), we can avoid
1257 * uprobe_apply(). current->mm must be probed and we can rely
1258 * on dup_mmap() which preserves the already installed bp's.
1260 * attr.enable_on_exec means that exec/mmap will install the
1261 * breakpoints we need.
1263 done = filter->nr_systemwide ||
1264 event->parent || event->attr.enable_on_exec ||
1265 trace_uprobe_filter_event(filter, event);
1266 list_add(&event->hw.tp_list, &filter->perf_events);
1268 done = filter->nr_systemwide;
1269 filter->nr_systemwide++;
1271 write_unlock(&filter->rwlock);
1276 static int uprobe_perf_close(struct trace_event_call *call,
1277 struct perf_event *event)
1279 struct trace_probe *pos, *tp;
1280 struct trace_uprobe *tu;
1283 tp = trace_probe_primary_from_call(call);
1284 if (WARN_ON_ONCE(!tp))
1287 tu = container_of(tp, struct trace_uprobe, tp);
1288 if (trace_uprobe_filter_remove(tu->tp.event->filter, event))
1291 list_for_each_entry(pos, trace_probe_probe_list(tp), list) {
1292 tu = container_of(pos, struct trace_uprobe, tp);
1293 ret = uprobe_apply(tu->inode, tu->offset, &tu->consumer, false);
1301 static int uprobe_perf_open(struct trace_event_call *call,
1302 struct perf_event *event)
1304 struct trace_probe *pos, *tp;
1305 struct trace_uprobe *tu;
1308 tp = trace_probe_primary_from_call(call);
1309 if (WARN_ON_ONCE(!tp))
1312 tu = container_of(tp, struct trace_uprobe, tp);
1313 if (trace_uprobe_filter_add(tu->tp.event->filter, event))
1316 list_for_each_entry(pos, trace_probe_probe_list(tp), list) {
1317 tu = container_of(pos, struct trace_uprobe, tp);
1318 err = uprobe_apply(tu->inode, tu->offset, &tu->consumer, true);
1320 uprobe_perf_close(call, event);
1328 static bool uprobe_perf_filter(struct uprobe_consumer *uc,
1329 enum uprobe_filter_ctx ctx, struct mm_struct *mm)
1331 struct trace_uprobe_filter *filter;
1332 struct trace_uprobe *tu;
1335 tu = container_of(uc, struct trace_uprobe, consumer);
1336 filter = tu->tp.event->filter;
1338 read_lock(&filter->rwlock);
1339 ret = __uprobe_perf_filter(filter, mm);
1340 read_unlock(&filter->rwlock);
1345 static void __uprobe_perf_func(struct trace_uprobe *tu,
1346 unsigned long func, struct pt_regs *regs,
1347 struct uprobe_cpu_buffer *ucb, int dsize)
1349 struct trace_event_call *call = trace_probe_event_call(&tu->tp);
1350 struct uprobe_trace_entry_head *entry;
1351 struct hlist_head *head;
1356 if (bpf_prog_array_valid(call)) {
1360 ret = trace_call_bpf(call, regs);
1366 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1368 size = esize + tu->tp.size + dsize;
1369 size = ALIGN(size + sizeof(u32), sizeof(u64)) - sizeof(u32);
1370 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough"))
1374 head = this_cpu_ptr(call->perf_events);
1375 if (hlist_empty(head))
1378 entry = perf_trace_buf_alloc(size, NULL, &rctx);
1382 if (is_ret_probe(tu)) {
1383 entry->vaddr[0] = func;
1384 entry->vaddr[1] = instruction_pointer(regs);
1385 data = DATAOF_TRACE_ENTRY(entry, true);
1387 entry->vaddr[0] = instruction_pointer(regs);
1388 data = DATAOF_TRACE_ENTRY(entry, false);
1391 memcpy(data, ucb->buf, tu->tp.size + dsize);
1393 if (size - esize > tu->tp.size + dsize) {
1394 int len = tu->tp.size + dsize;
1396 memset(data + len, 0, size - esize - len);
1399 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1405 /* uprobe profile handler */
1406 static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs,
1407 struct uprobe_cpu_buffer *ucb, int dsize)
1409 if (!uprobe_perf_filter(&tu->consumer, 0, current->mm))
1410 return UPROBE_HANDLER_REMOVE;
1412 if (!is_ret_probe(tu))
1413 __uprobe_perf_func(tu, 0, regs, ucb, dsize);
1417 static void uretprobe_perf_func(struct trace_uprobe *tu, unsigned long func,
1418 struct pt_regs *regs,
1419 struct uprobe_cpu_buffer *ucb, int dsize)
1421 __uprobe_perf_func(tu, func, regs, ucb, dsize);
1424 int bpf_get_uprobe_info(const struct perf_event *event, u32 *fd_type,
1425 const char **filename, u64 *probe_offset,
1426 u64 *probe_addr, bool perf_type_tracepoint)
1428 const char *pevent = trace_event_name(event->tp_event);
1429 const char *group = event->tp_event->class->system;
1430 struct trace_uprobe *tu;
1432 if (perf_type_tracepoint)
1433 tu = find_probe_event(pevent, group);
1435 tu = trace_uprobe_primary_from_call(event->tp_event);
1439 *fd_type = is_ret_probe(tu) ? BPF_FD_TYPE_URETPROBE
1440 : BPF_FD_TYPE_UPROBE;
1441 *filename = tu->filename;
1442 *probe_offset = tu->offset;
1446 #endif /* CONFIG_PERF_EVENTS */
1449 trace_uprobe_register(struct trace_event_call *event, enum trace_reg type,
1452 struct trace_event_file *file = data;
1455 case TRACE_REG_REGISTER:
1456 return probe_event_enable(event, file, NULL);
1458 case TRACE_REG_UNREGISTER:
1459 probe_event_disable(event, file);
1462 #ifdef CONFIG_PERF_EVENTS
1463 case TRACE_REG_PERF_REGISTER:
1464 return probe_event_enable(event, NULL, uprobe_perf_filter);
1466 case TRACE_REG_PERF_UNREGISTER:
1467 probe_event_disable(event, NULL);
1470 case TRACE_REG_PERF_OPEN:
1471 return uprobe_perf_open(event, data);
1473 case TRACE_REG_PERF_CLOSE:
1474 return uprobe_perf_close(event, data);
1482 static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs)
1484 struct trace_uprobe *tu;
1485 struct uprobe_dispatch_data udd;
1486 struct uprobe_cpu_buffer *ucb;
1491 tu = container_of(con, struct trace_uprobe, consumer);
1495 udd.bp_addr = instruction_pointer(regs);
1497 current->utask->vaddr = (unsigned long) &udd;
1499 if (WARN_ON_ONCE(!uprobe_cpu_buffer))
1502 dsize = __get_data_size(&tu->tp, regs);
1503 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1505 ucb = uprobe_buffer_get();
1506 store_trace_args(ucb->buf, &tu->tp, regs, esize, dsize);
1508 if (trace_probe_test_flag(&tu->tp, TP_FLAG_TRACE))
1509 ret |= uprobe_trace_func(tu, regs, ucb, dsize);
1511 #ifdef CONFIG_PERF_EVENTS
1512 if (trace_probe_test_flag(&tu->tp, TP_FLAG_PROFILE))
1513 ret |= uprobe_perf_func(tu, regs, ucb, dsize);
1515 uprobe_buffer_put(ucb);
1519 static int uretprobe_dispatcher(struct uprobe_consumer *con,
1520 unsigned long func, struct pt_regs *regs)
1522 struct trace_uprobe *tu;
1523 struct uprobe_dispatch_data udd;
1524 struct uprobe_cpu_buffer *ucb;
1527 tu = container_of(con, struct trace_uprobe, consumer);
1532 current->utask->vaddr = (unsigned long) &udd;
1534 if (WARN_ON_ONCE(!uprobe_cpu_buffer))
1537 dsize = __get_data_size(&tu->tp, regs);
1538 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1540 ucb = uprobe_buffer_get();
1541 store_trace_args(ucb->buf, &tu->tp, regs, esize, dsize);
1543 if (trace_probe_test_flag(&tu->tp, TP_FLAG_TRACE))
1544 uretprobe_trace_func(tu, func, regs, ucb, dsize);
1546 #ifdef CONFIG_PERF_EVENTS
1547 if (trace_probe_test_flag(&tu->tp, TP_FLAG_PROFILE))
1548 uretprobe_perf_func(tu, func, regs, ucb, dsize);
1550 uprobe_buffer_put(ucb);
1554 static struct trace_event_functions uprobe_funcs = {
1555 .trace = print_uprobe_event
1558 static struct trace_event_fields uprobe_fields_array[] = {
1559 { .type = TRACE_FUNCTION_TYPE,
1560 .define_fields = uprobe_event_define_fields },
1564 static inline void init_trace_event_call(struct trace_uprobe *tu)
1566 struct trace_event_call *call = trace_probe_event_call(&tu->tp);
1567 call->event.funcs = &uprobe_funcs;
1568 call->class->fields_array = uprobe_fields_array;
1570 call->flags = TRACE_EVENT_FL_UPROBE | TRACE_EVENT_FL_CAP_ANY;
1571 call->class->reg = trace_uprobe_register;
1574 static int register_uprobe_event(struct trace_uprobe *tu)
1576 init_trace_event_call(tu);
1578 return trace_probe_register_event_call(&tu->tp);
1581 static int unregister_uprobe_event(struct trace_uprobe *tu)
1583 return trace_probe_unregister_event_call(&tu->tp);
1586 #ifdef CONFIG_PERF_EVENTS
1587 struct trace_event_call *
1588 create_local_trace_uprobe(char *name, unsigned long offs,
1589 unsigned long ref_ctr_offset, bool is_return)
1591 enum probe_print_type ptype;
1592 struct trace_uprobe *tu;
1596 ret = kern_path(name, LOOKUP_FOLLOW, &path);
1598 return ERR_PTR(ret);
1600 if (!d_is_reg(path.dentry)) {
1602 return ERR_PTR(-EINVAL);
1606 * local trace_kprobes are not added to dyn_event, so they are never
1607 * searched in find_trace_kprobe(). Therefore, there is no concern of
1608 * duplicated name "DUMMY_EVENT" here.
1610 tu = alloc_trace_uprobe(UPROBE_EVENT_SYSTEM, "DUMMY_EVENT", 0,
1614 pr_info("Failed to allocate trace_uprobe.(%d)\n",
1617 return ERR_CAST(tu);
1622 tu->ref_ctr_offset = ref_ctr_offset;
1623 tu->filename = kstrdup(name, GFP_KERNEL);
1624 if (!tu->filename) {
1629 init_trace_event_call(tu);
1631 ptype = is_ret_probe(tu) ? PROBE_PRINT_RETURN : PROBE_PRINT_NORMAL;
1632 if (traceprobe_set_print_fmt(&tu->tp, ptype) < 0) {
1637 return trace_probe_event_call(&tu->tp);
1639 free_trace_uprobe(tu);
1640 return ERR_PTR(ret);
1643 void destroy_local_trace_uprobe(struct trace_event_call *event_call)
1645 struct trace_uprobe *tu;
1647 tu = trace_uprobe_primary_from_call(event_call);
1649 free_trace_uprobe(tu);
1651 #endif /* CONFIG_PERF_EVENTS */
1653 /* Make a trace interface for controlling probe points */
1654 static __init int init_uprobe_trace(void)
1658 ret = dyn_event_register(&trace_uprobe_ops);
1662 ret = tracing_init_dentry();
1666 trace_create_file("uprobe_events", TRACE_MODE_WRITE, NULL,
1667 NULL, &uprobe_events_ops);
1668 /* Profile interface */
1669 trace_create_file("uprobe_profile", TRACE_MODE_READ, NULL,
1670 NULL, &uprobe_profile_ops);
1674 fs_initcall(init_uprobe_trace);