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/ctype.h>
11 #include <linux/module.h>
12 #include <linux/uaccess.h>
13 #include <linux/uprobes.h>
14 #include <linux/namei.h>
15 #include <linux/string.h>
16 #include <linux/rculist.h>
18 #include "trace_dynevent.h"
19 #include "trace_probe.h"
20 #include "trace_probe_tmpl.h"
22 #define UPROBE_EVENT_SYSTEM "uprobes"
24 struct uprobe_trace_entry_head {
25 struct trace_entry ent;
26 unsigned long vaddr[];
29 #define SIZEOF_TRACE_ENTRY(is_return) \
30 (sizeof(struct uprobe_trace_entry_head) + \
31 sizeof(unsigned long) * (is_return ? 2 : 1))
33 #define DATAOF_TRACE_ENTRY(entry, is_return) \
34 ((void*)(entry) + SIZEOF_TRACE_ENTRY(is_return))
36 struct trace_uprobe_filter {
39 struct list_head perf_events;
42 static int trace_uprobe_create(int argc, const char **argv);
43 static int trace_uprobe_show(struct seq_file *m, struct dyn_event *ev);
44 static int trace_uprobe_release(struct dyn_event *ev);
45 static bool trace_uprobe_is_busy(struct dyn_event *ev);
46 static bool trace_uprobe_match(const char *system, const char *event,
47 struct dyn_event *ev);
49 static struct dyn_event_operations trace_uprobe_ops = {
50 .create = trace_uprobe_create,
51 .show = trace_uprobe_show,
52 .is_busy = trace_uprobe_is_busy,
53 .free = trace_uprobe_release,
54 .match = trace_uprobe_match,
58 * uprobe event core functions
61 struct dyn_event devent;
62 struct trace_uprobe_filter filter;
63 struct uprobe_consumer consumer;
68 unsigned long ref_ctr_offset;
70 struct trace_probe tp;
73 static bool is_trace_uprobe(struct dyn_event *ev)
75 return ev->ops == &trace_uprobe_ops;
78 static struct trace_uprobe *to_trace_uprobe(struct dyn_event *ev)
80 return container_of(ev, struct trace_uprobe, devent);
84 * for_each_trace_uprobe - iterate over the trace_uprobe list
85 * @pos: the struct trace_uprobe * for each entry
86 * @dpos: the struct dyn_event * to use as a loop cursor
88 #define for_each_trace_uprobe(pos, dpos) \
89 for_each_dyn_event(dpos) \
90 if (is_trace_uprobe(dpos) && (pos = to_trace_uprobe(dpos)))
92 #define SIZEOF_TRACE_UPROBE(n) \
93 (offsetof(struct trace_uprobe, tp.args) + \
94 (sizeof(struct probe_arg) * (n)))
96 static int register_uprobe_event(struct trace_uprobe *tu);
97 static int unregister_uprobe_event(struct trace_uprobe *tu);
99 struct uprobe_dispatch_data {
100 struct trace_uprobe *tu;
101 unsigned long bp_addr;
104 static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs);
105 static int uretprobe_dispatcher(struct uprobe_consumer *con,
106 unsigned long func, struct pt_regs *regs);
108 #ifdef CONFIG_STACK_GROWSUP
109 static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n)
111 return addr - (n * sizeof(long));
114 static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n)
116 return addr + (n * sizeof(long));
120 static unsigned long get_user_stack_nth(struct pt_regs *regs, unsigned int n)
123 unsigned long addr = user_stack_pointer(regs);
125 addr = adjust_stack_addr(addr, n);
127 if (copy_from_user(&ret, (void __force __user *) addr, sizeof(ret)))
134 * Uprobes-specific fetch functions
136 static nokprobe_inline int
137 probe_mem_read(void *dest, void *src, size_t size)
139 void __user *vaddr = (void __force __user *)src;
141 return copy_from_user(dest, vaddr, size) ? -EFAULT : 0;
144 static nokprobe_inline int
145 probe_mem_read_user(void *dest, void *src, size_t size)
147 return probe_mem_read(dest, src, size);
151 * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
152 * length and relative data location.
154 static nokprobe_inline int
155 fetch_store_string(unsigned long addr, void *dest, void *base)
158 u32 loc = *(u32 *)dest;
159 int maxlen = get_loc_len(loc);
160 u8 *dst = get_loc_data(dest, base);
161 void __user *src = (void __force __user *) addr;
163 if (unlikely(!maxlen))
166 if (addr == FETCH_TOKEN_COMM)
167 ret = strlcpy(dst, current->comm, maxlen);
169 ret = strncpy_from_user(dst, src, maxlen);
175 * Include the terminating null byte. In this case it
176 * was copied by strncpy_from_user but not accounted
180 *(u32 *)dest = make_data_loc(ret, (void *)dst - base);
186 static nokprobe_inline int
187 fetch_store_string_user(unsigned long addr, void *dest, void *base)
189 return fetch_store_string(addr, dest, base);
192 /* Return the length of string -- including null terminal byte */
193 static nokprobe_inline int
194 fetch_store_strlen(unsigned long addr)
197 void __user *vaddr = (void __force __user *) addr;
199 if (addr == FETCH_TOKEN_COMM)
200 len = strlen(current->comm) + 1;
202 len = strnlen_user(vaddr, MAX_STRING_SIZE);
204 return (len > MAX_STRING_SIZE) ? 0 : len;
207 static nokprobe_inline int
208 fetch_store_strlen_user(unsigned long addr)
210 return fetch_store_strlen(addr);
213 static unsigned long translate_user_vaddr(unsigned long file_offset)
215 unsigned long base_addr;
216 struct uprobe_dispatch_data *udd;
218 udd = (void *) current->utask->vaddr;
220 base_addr = udd->bp_addr - udd->tu->offset;
221 return base_addr + file_offset;
224 /* Note that we don't verify it, since the code does not come from user space */
226 process_fetch_insn(struct fetch_insn *code, struct pt_regs *regs, void *dest,
231 /* 1st stage: get value from context */
234 val = regs_get_register(regs, code->param);
237 val = get_user_stack_nth(regs, code->param);
239 case FETCH_OP_STACKP:
240 val = user_stack_pointer(regs);
242 case FETCH_OP_RETVAL:
243 val = regs_return_value(regs);
246 val = code->immediate;
249 val = FETCH_TOKEN_COMM;
252 val = translate_user_vaddr(code->immediate);
259 return process_fetch_insn_bottom(code, val, dest, base);
261 NOKPROBE_SYMBOL(process_fetch_insn)
263 static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter)
265 rwlock_init(&filter->rwlock);
266 filter->nr_systemwide = 0;
267 INIT_LIST_HEAD(&filter->perf_events);
270 static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter *filter)
272 return !filter->nr_systemwide && list_empty(&filter->perf_events);
275 static inline bool is_ret_probe(struct trace_uprobe *tu)
277 return tu->consumer.ret_handler != NULL;
280 static bool trace_uprobe_is_busy(struct dyn_event *ev)
282 struct trace_uprobe *tu = to_trace_uprobe(ev);
284 return trace_probe_is_enabled(&tu->tp);
287 static bool trace_uprobe_match(const char *system, const char *event,
288 struct dyn_event *ev)
290 struct trace_uprobe *tu = to_trace_uprobe(ev);
292 return strcmp(trace_probe_name(&tu->tp), event) == 0 &&
293 (!system || strcmp(trace_probe_group_name(&tu->tp), system) == 0);
297 * Allocate new trace_uprobe and initialize it (including uprobes).
299 static struct trace_uprobe *
300 alloc_trace_uprobe(const char *group, const char *event, int nargs, bool is_ret)
302 struct trace_uprobe *tu;
305 tu = kzalloc(SIZEOF_TRACE_UPROBE(nargs), GFP_KERNEL);
307 return ERR_PTR(-ENOMEM);
309 ret = trace_probe_init(&tu->tp, event, group);
313 dyn_event_init(&tu->devent, &trace_uprobe_ops);
314 tu->consumer.handler = uprobe_dispatcher;
316 tu->consumer.ret_handler = uretprobe_dispatcher;
317 init_trace_uprobe_filter(&tu->filter);
326 static void free_trace_uprobe(struct trace_uprobe *tu)
332 trace_probe_cleanup(&tu->tp);
337 static struct trace_uprobe *find_probe_event(const char *event, const char *group)
339 struct dyn_event *pos;
340 struct trace_uprobe *tu;
342 for_each_trace_uprobe(tu, pos)
343 if (strcmp(trace_probe_name(&tu->tp), event) == 0 &&
344 strcmp(trace_probe_group_name(&tu->tp), group) == 0)
350 /* Unregister a trace_uprobe and probe_event */
351 static int unregister_trace_uprobe(struct trace_uprobe *tu)
355 ret = unregister_uprobe_event(tu);
359 dyn_event_remove(&tu->devent);
360 free_trace_uprobe(tu);
365 * Uprobe with multiple reference counter is not allowed. i.e.
366 * If inode and offset matches, reference counter offset *must*
367 * match as well. Though, there is one exception: If user is
368 * replacing old trace_uprobe with new one(same group/event),
369 * then we allow same uprobe with new reference counter as far
370 * as the new one does not conflict with any other existing
373 static struct trace_uprobe *find_old_trace_uprobe(struct trace_uprobe *new)
375 struct dyn_event *pos;
376 struct trace_uprobe *tmp, *old = NULL;
377 struct inode *new_inode = d_real_inode(new->path.dentry);
379 old = find_probe_event(trace_probe_name(&new->tp),
380 trace_probe_group_name(&new->tp));
382 for_each_trace_uprobe(tmp, pos) {
383 if ((old ? old != tmp : true) &&
384 new_inode == d_real_inode(tmp->path.dentry) &&
385 new->offset == tmp->offset &&
386 new->ref_ctr_offset != tmp->ref_ctr_offset) {
387 pr_warn("Reference counter offset mismatch.");
388 return ERR_PTR(-EINVAL);
394 /* Register a trace_uprobe and probe_event */
395 static int register_trace_uprobe(struct trace_uprobe *tu)
397 struct trace_uprobe *old_tu;
400 mutex_lock(&event_mutex);
402 /* register as an event */
403 old_tu = find_old_trace_uprobe(tu);
404 if (IS_ERR(old_tu)) {
405 ret = PTR_ERR(old_tu);
410 /* delete old event */
411 ret = unregister_trace_uprobe(old_tu);
416 ret = register_uprobe_event(tu);
418 pr_warn("Failed to register probe event(%d)\n", ret);
422 dyn_event_add(&tu->devent);
425 mutex_unlock(&event_mutex);
432 * - Add uprobe: p|r[:[GRP/]EVENT] PATH:OFFSET [FETCHARGS]
434 * - Remove uprobe: -:[GRP/]EVENT
436 static int trace_uprobe_create(int argc, const char **argv)
438 struct trace_uprobe *tu;
439 const char *event = NULL, *group = UPROBE_EVENT_SYSTEM;
440 char *arg, *filename, *rctr, *rctr_end, *tmp;
441 char buf[MAX_EVENT_NAME_LEN];
443 unsigned long offset, ref_ctr_offset;
444 bool is_return = false;
450 /* argc must be >= 1 */
451 if (argv[0][0] == 'r')
453 else if (argv[0][0] != 'p' || argc < 2)
456 if (argv[0][1] == ':')
459 if (!strchr(argv[1], '/'))
462 filename = kstrdup(argv[1], GFP_KERNEL);
466 /* Find the last occurrence, in case the path contains ':' too. */
467 arg = strrchr(filename, ':');
468 if (!arg || !isdigit(arg[1])) {
473 trace_probe_log_init("trace_uprobe", argc, argv);
474 trace_probe_log_set_index(1); /* filename is the 2nd argument */
477 ret = kern_path(filename, LOOKUP_FOLLOW, &path);
479 trace_probe_log_err(0, FILE_NOT_FOUND);
481 trace_probe_log_clear();
484 if (!d_is_reg(path.dentry)) {
485 trace_probe_log_err(0, NO_REGULAR_FILE);
487 goto fail_address_parse;
490 /* Parse reference counter offset if specified. */
491 rctr = strchr(arg, '(');
493 rctr_end = strchr(rctr, ')');
496 rctr_end = rctr + strlen(rctr);
497 trace_probe_log_err(rctr_end - filename,
499 goto fail_address_parse;
500 } else if (rctr_end[1] != '\0') {
502 trace_probe_log_err(rctr_end + 1 - filename,
504 goto fail_address_parse;
509 ret = kstrtoul(rctr, 0, &ref_ctr_offset);
511 trace_probe_log_err(rctr - filename, BAD_REFCNT);
512 goto fail_address_parse;
516 /* Parse uprobe offset. */
517 ret = kstrtoul(arg, 0, &offset);
519 trace_probe_log_err(arg - filename, BAD_UPROBE_OFFS);
520 goto fail_address_parse;
524 trace_probe_log_set_index(0);
526 ret = traceprobe_parse_event_name(&event, &group, buf,
529 goto fail_address_parse;
534 tail = kstrdup(kbasename(filename), GFP_KERNEL);
537 goto fail_address_parse;
540 ptr = strpbrk(tail, ".-_");
544 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset);
552 tu = alloc_trace_uprobe(group, event, argc, is_return);
555 /* This must return -ENOMEM otherwise there is a bug */
556 WARN_ON_ONCE(ret != -ENOMEM);
557 goto fail_address_parse;
560 tu->ref_ctr_offset = ref_ctr_offset;
562 tu->filename = filename;
564 /* parse arguments */
565 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
566 tmp = kstrdup(argv[i], GFP_KERNEL);
572 trace_probe_log_set_index(i + 2);
573 ret = traceprobe_parse_probe_arg(&tu->tp, i, tmp,
574 is_return ? TPARG_FL_RETURN : 0);
580 ret = traceprobe_set_print_fmt(&tu->tp, is_ret_probe(tu));
584 ret = register_trace_uprobe(tu);
589 free_trace_uprobe(tu);
591 trace_probe_log_clear();
595 trace_probe_log_clear();
602 static int create_or_delete_trace_uprobe(int argc, char **argv)
606 if (argv[0][0] == '-')
607 return dyn_event_release(argc, argv, &trace_uprobe_ops);
609 ret = trace_uprobe_create(argc, (const char **)argv);
610 return ret == -ECANCELED ? -EINVAL : ret;
613 static int trace_uprobe_release(struct dyn_event *ev)
615 struct trace_uprobe *tu = to_trace_uprobe(ev);
617 return unregister_trace_uprobe(tu);
620 /* Probes listing interfaces */
621 static int trace_uprobe_show(struct seq_file *m, struct dyn_event *ev)
623 struct trace_uprobe *tu = to_trace_uprobe(ev);
624 char c = is_ret_probe(tu) ? 'r' : 'p';
627 seq_printf(m, "%c:%s/%s %s:0x%0*lx", c, trace_probe_group_name(&tu->tp),
628 trace_probe_name(&tu->tp), tu->filename,
629 (int)(sizeof(void *) * 2), tu->offset);
631 if (tu->ref_ctr_offset)
632 seq_printf(m, "(0x%lx)", tu->ref_ctr_offset);
634 for (i = 0; i < tu->tp.nr_args; i++)
635 seq_printf(m, " %s=%s", tu->tp.args[i].name, tu->tp.args[i].comm);
641 static int probes_seq_show(struct seq_file *m, void *v)
643 struct dyn_event *ev = v;
645 if (!is_trace_uprobe(ev))
648 return trace_uprobe_show(m, ev);
651 static const struct seq_operations probes_seq_op = {
652 .start = dyn_event_seq_start,
653 .next = dyn_event_seq_next,
654 .stop = dyn_event_seq_stop,
655 .show = probes_seq_show
658 static int probes_open(struct inode *inode, struct file *file)
662 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
663 ret = dyn_events_release_all(&trace_uprobe_ops);
668 return seq_open(file, &probes_seq_op);
671 static ssize_t probes_write(struct file *file, const char __user *buffer,
672 size_t count, loff_t *ppos)
674 return trace_parse_run_command(file, buffer, count, ppos,
675 create_or_delete_trace_uprobe);
678 static const struct file_operations uprobe_events_ops = {
679 .owner = THIS_MODULE,
683 .release = seq_release,
684 .write = probes_write,
687 /* Probes profiling interfaces */
688 static int probes_profile_seq_show(struct seq_file *m, void *v)
690 struct dyn_event *ev = v;
691 struct trace_uprobe *tu;
693 if (!is_trace_uprobe(ev))
696 tu = to_trace_uprobe(ev);
697 seq_printf(m, " %s %-44s %15lu\n", tu->filename,
698 trace_probe_name(&tu->tp), tu->nhit);
702 static const struct seq_operations profile_seq_op = {
703 .start = dyn_event_seq_start,
704 .next = dyn_event_seq_next,
705 .stop = dyn_event_seq_stop,
706 .show = probes_profile_seq_show
709 static int profile_open(struct inode *inode, struct file *file)
711 return seq_open(file, &profile_seq_op);
714 static const struct file_operations uprobe_profile_ops = {
715 .owner = THIS_MODULE,
716 .open = profile_open,
719 .release = seq_release,
722 struct uprobe_cpu_buffer {
726 static struct uprobe_cpu_buffer __percpu *uprobe_cpu_buffer;
727 static int uprobe_buffer_refcnt;
729 static int uprobe_buffer_init(void)
733 uprobe_cpu_buffer = alloc_percpu(struct uprobe_cpu_buffer);
734 if (uprobe_cpu_buffer == NULL)
737 for_each_possible_cpu(cpu) {
738 struct page *p = alloc_pages_node(cpu_to_node(cpu),
744 per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf = page_address(p);
745 mutex_init(&per_cpu_ptr(uprobe_cpu_buffer, cpu)->mutex);
751 for_each_possible_cpu(cpu) {
754 free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf);
757 free_percpu(uprobe_cpu_buffer);
761 static int uprobe_buffer_enable(void)
765 BUG_ON(!mutex_is_locked(&event_mutex));
767 if (uprobe_buffer_refcnt++ == 0) {
768 ret = uprobe_buffer_init();
770 uprobe_buffer_refcnt--;
776 static void uprobe_buffer_disable(void)
780 BUG_ON(!mutex_is_locked(&event_mutex));
782 if (--uprobe_buffer_refcnt == 0) {
783 for_each_possible_cpu(cpu)
784 free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer,
787 free_percpu(uprobe_cpu_buffer);
788 uprobe_cpu_buffer = NULL;
792 static struct uprobe_cpu_buffer *uprobe_buffer_get(void)
794 struct uprobe_cpu_buffer *ucb;
797 cpu = raw_smp_processor_id();
798 ucb = per_cpu_ptr(uprobe_cpu_buffer, cpu);
801 * Use per-cpu buffers for fastest access, but we might migrate
802 * so the mutex makes sure we have sole access to it.
804 mutex_lock(&ucb->mutex);
809 static void uprobe_buffer_put(struct uprobe_cpu_buffer *ucb)
811 mutex_unlock(&ucb->mutex);
814 static void __uprobe_trace_func(struct trace_uprobe *tu,
815 unsigned long func, struct pt_regs *regs,
816 struct uprobe_cpu_buffer *ucb, int dsize,
817 struct trace_event_file *trace_file)
819 struct uprobe_trace_entry_head *entry;
820 struct ring_buffer_event *event;
821 struct ring_buffer *buffer;
824 struct trace_event_call *call = &tu->tp.call;
826 WARN_ON(call != trace_file->event_call);
828 if (WARN_ON_ONCE(tu->tp.size + dsize > PAGE_SIZE))
831 if (trace_trigger_soft_disabled(trace_file))
834 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
835 size = esize + tu->tp.size + dsize;
836 event = trace_event_buffer_lock_reserve(&buffer, trace_file,
837 call->event.type, size, 0, 0);
841 entry = ring_buffer_event_data(event);
842 if (is_ret_probe(tu)) {
843 entry->vaddr[0] = func;
844 entry->vaddr[1] = instruction_pointer(regs);
845 data = DATAOF_TRACE_ENTRY(entry, true);
847 entry->vaddr[0] = instruction_pointer(regs);
848 data = DATAOF_TRACE_ENTRY(entry, false);
851 memcpy(data, ucb->buf, tu->tp.size + dsize);
853 event_trigger_unlock_commit(trace_file, buffer, event, entry, 0, 0);
857 static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs,
858 struct uprobe_cpu_buffer *ucb, int dsize)
860 struct event_file_link *link;
862 if (is_ret_probe(tu))
866 trace_probe_for_each_link_rcu(link, &tu->tp)
867 __uprobe_trace_func(tu, 0, regs, ucb, dsize, link->file);
873 static void uretprobe_trace_func(struct trace_uprobe *tu, unsigned long func,
874 struct pt_regs *regs,
875 struct uprobe_cpu_buffer *ucb, int dsize)
877 struct event_file_link *link;
880 trace_probe_for_each_link_rcu(link, &tu->tp)
881 __uprobe_trace_func(tu, func, regs, ucb, dsize, link->file);
885 /* Event entry printers */
886 static enum print_line_t
887 print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event)
889 struct uprobe_trace_entry_head *entry;
890 struct trace_seq *s = &iter->seq;
891 struct trace_uprobe *tu;
894 entry = (struct uprobe_trace_entry_head *)iter->ent;
895 tu = container_of(event, struct trace_uprobe, tp.call.event);
897 if (is_ret_probe(tu)) {
898 trace_seq_printf(s, "%s: (0x%lx <- 0x%lx)",
899 trace_probe_name(&tu->tp),
900 entry->vaddr[1], entry->vaddr[0]);
901 data = DATAOF_TRACE_ENTRY(entry, true);
903 trace_seq_printf(s, "%s: (0x%lx)",
904 trace_probe_name(&tu->tp),
906 data = DATAOF_TRACE_ENTRY(entry, false);
909 if (print_probe_args(s, tu->tp.args, tu->tp.nr_args, data, entry) < 0)
912 trace_seq_putc(s, '\n');
915 return trace_handle_return(s);
918 typedef bool (*filter_func_t)(struct uprobe_consumer *self,
919 enum uprobe_filter_ctx ctx,
920 struct mm_struct *mm);
923 probe_event_enable(struct trace_uprobe *tu, struct trace_event_file *file,
924 filter_func_t filter)
926 bool enabled = trace_probe_is_enabled(&tu->tp);
930 if (trace_probe_test_flag(&tu->tp, TP_FLAG_PROFILE))
933 ret = trace_probe_add_file(&tu->tp, file);
937 if (trace_probe_test_flag(&tu->tp, TP_FLAG_TRACE))
940 trace_probe_set_flag(&tu->tp, TP_FLAG_PROFILE);
943 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
948 ret = uprobe_buffer_enable();
952 tu->consumer.filter = filter;
953 tu->inode = d_real_inode(tu->path.dentry);
954 if (tu->ref_ctr_offset) {
955 ret = uprobe_register_refctr(tu->inode, tu->offset,
956 tu->ref_ctr_offset, &tu->consumer);
958 ret = uprobe_register(tu->inode, tu->offset, &tu->consumer);
967 uprobe_buffer_disable();
971 trace_probe_remove_file(&tu->tp, file);
973 trace_probe_clear_flag(&tu->tp, TP_FLAG_PROFILE);
979 probe_event_disable(struct trace_uprobe *tu, struct trace_event_file *file)
981 if (!trace_probe_is_enabled(&tu->tp))
985 if (trace_probe_remove_file(&tu->tp, file) < 0)
988 if (trace_probe_is_enabled(&tu->tp))
991 trace_probe_clear_flag(&tu->tp, TP_FLAG_PROFILE);
993 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
995 uprobe_unregister(tu->inode, tu->offset, &tu->consumer);
998 uprobe_buffer_disable();
1001 static int uprobe_event_define_fields(struct trace_event_call *event_call)
1004 struct uprobe_trace_entry_head field;
1005 struct trace_uprobe *tu = event_call->data;
1007 if (is_ret_probe(tu)) {
1008 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_FUNC, 0);
1009 DEFINE_FIELD(unsigned long, vaddr[1], FIELD_STRING_RETIP, 0);
1010 size = SIZEOF_TRACE_ENTRY(true);
1012 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0);
1013 size = SIZEOF_TRACE_ENTRY(false);
1016 return traceprobe_define_arg_fields(event_call, size, &tu->tp);
1019 #ifdef CONFIG_PERF_EVENTS
1021 __uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm)
1023 struct perf_event *event;
1025 if (filter->nr_systemwide)
1028 list_for_each_entry(event, &filter->perf_events, hw.tp_list) {
1029 if (event->hw.target->mm == mm)
1037 uprobe_filter_event(struct trace_uprobe *tu, struct perf_event *event)
1039 return __uprobe_perf_filter(&tu->filter, event->hw.target->mm);
1042 static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event)
1046 write_lock(&tu->filter.rwlock);
1047 if (event->hw.target) {
1048 list_del(&event->hw.tp_list);
1049 done = tu->filter.nr_systemwide ||
1050 (event->hw.target->flags & PF_EXITING) ||
1051 uprobe_filter_event(tu, event);
1053 tu->filter.nr_systemwide--;
1054 done = tu->filter.nr_systemwide;
1056 write_unlock(&tu->filter.rwlock);
1059 return uprobe_apply(tu->inode, tu->offset, &tu->consumer, false);
1064 static int uprobe_perf_open(struct trace_uprobe *tu, struct perf_event *event)
1069 write_lock(&tu->filter.rwlock);
1070 if (event->hw.target) {
1072 * event->parent != NULL means copy_process(), we can avoid
1073 * uprobe_apply(). current->mm must be probed and we can rely
1074 * on dup_mmap() which preserves the already installed bp's.
1076 * attr.enable_on_exec means that exec/mmap will install the
1077 * breakpoints we need.
1079 done = tu->filter.nr_systemwide ||
1080 event->parent || event->attr.enable_on_exec ||
1081 uprobe_filter_event(tu, event);
1082 list_add(&event->hw.tp_list, &tu->filter.perf_events);
1084 done = tu->filter.nr_systemwide;
1085 tu->filter.nr_systemwide++;
1087 write_unlock(&tu->filter.rwlock);
1091 err = uprobe_apply(tu->inode, tu->offset, &tu->consumer, true);
1093 uprobe_perf_close(tu, event);
1098 static bool uprobe_perf_filter(struct uprobe_consumer *uc,
1099 enum uprobe_filter_ctx ctx, struct mm_struct *mm)
1101 struct trace_uprobe *tu;
1104 tu = container_of(uc, struct trace_uprobe, consumer);
1105 read_lock(&tu->filter.rwlock);
1106 ret = __uprobe_perf_filter(&tu->filter, mm);
1107 read_unlock(&tu->filter.rwlock);
1112 static void __uprobe_perf_func(struct trace_uprobe *tu,
1113 unsigned long func, struct pt_regs *regs,
1114 struct uprobe_cpu_buffer *ucb, int dsize)
1116 struct trace_event_call *call = &tu->tp.call;
1117 struct uprobe_trace_entry_head *entry;
1118 struct hlist_head *head;
1123 if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs))
1126 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1128 size = esize + tu->tp.size + dsize;
1129 size = ALIGN(size + sizeof(u32), sizeof(u64)) - sizeof(u32);
1130 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough"))
1134 head = this_cpu_ptr(call->perf_events);
1135 if (hlist_empty(head))
1138 entry = perf_trace_buf_alloc(size, NULL, &rctx);
1142 if (is_ret_probe(tu)) {
1143 entry->vaddr[0] = func;
1144 entry->vaddr[1] = instruction_pointer(regs);
1145 data = DATAOF_TRACE_ENTRY(entry, true);
1147 entry->vaddr[0] = instruction_pointer(regs);
1148 data = DATAOF_TRACE_ENTRY(entry, false);
1151 memcpy(data, ucb->buf, tu->tp.size + dsize);
1153 if (size - esize > tu->tp.size + dsize) {
1154 int len = tu->tp.size + dsize;
1156 memset(data + len, 0, size - esize - len);
1159 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1165 /* uprobe profile handler */
1166 static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs,
1167 struct uprobe_cpu_buffer *ucb, int dsize)
1169 if (!uprobe_perf_filter(&tu->consumer, 0, current->mm))
1170 return UPROBE_HANDLER_REMOVE;
1172 if (!is_ret_probe(tu))
1173 __uprobe_perf_func(tu, 0, regs, ucb, dsize);
1177 static void uretprobe_perf_func(struct trace_uprobe *tu, unsigned long func,
1178 struct pt_regs *regs,
1179 struct uprobe_cpu_buffer *ucb, int dsize)
1181 __uprobe_perf_func(tu, func, regs, ucb, dsize);
1184 int bpf_get_uprobe_info(const struct perf_event *event, u32 *fd_type,
1185 const char **filename, u64 *probe_offset,
1186 bool perf_type_tracepoint)
1188 const char *pevent = trace_event_name(event->tp_event);
1189 const char *group = event->tp_event->class->system;
1190 struct trace_uprobe *tu;
1192 if (perf_type_tracepoint)
1193 tu = find_probe_event(pevent, group);
1195 tu = event->tp_event->data;
1199 *fd_type = is_ret_probe(tu) ? BPF_FD_TYPE_URETPROBE
1200 : BPF_FD_TYPE_UPROBE;
1201 *filename = tu->filename;
1202 *probe_offset = tu->offset;
1205 #endif /* CONFIG_PERF_EVENTS */
1208 trace_uprobe_register(struct trace_event_call *event, enum trace_reg type,
1211 struct trace_uprobe *tu = event->data;
1212 struct trace_event_file *file = data;
1215 case TRACE_REG_REGISTER:
1216 return probe_event_enable(tu, file, NULL);
1218 case TRACE_REG_UNREGISTER:
1219 probe_event_disable(tu, file);
1222 #ifdef CONFIG_PERF_EVENTS
1223 case TRACE_REG_PERF_REGISTER:
1224 return probe_event_enable(tu, NULL, uprobe_perf_filter);
1226 case TRACE_REG_PERF_UNREGISTER:
1227 probe_event_disable(tu, NULL);
1230 case TRACE_REG_PERF_OPEN:
1231 return uprobe_perf_open(tu, data);
1233 case TRACE_REG_PERF_CLOSE:
1234 return uprobe_perf_close(tu, data);
1243 static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs)
1245 struct trace_uprobe *tu;
1246 struct uprobe_dispatch_data udd;
1247 struct uprobe_cpu_buffer *ucb;
1252 tu = container_of(con, struct trace_uprobe, consumer);
1256 udd.bp_addr = instruction_pointer(regs);
1258 current->utask->vaddr = (unsigned long) &udd;
1260 if (WARN_ON_ONCE(!uprobe_cpu_buffer))
1263 dsize = __get_data_size(&tu->tp, regs);
1264 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1266 ucb = uprobe_buffer_get();
1267 store_trace_args(ucb->buf, &tu->tp, regs, esize, dsize);
1269 if (trace_probe_test_flag(&tu->tp, TP_FLAG_TRACE))
1270 ret |= uprobe_trace_func(tu, regs, ucb, dsize);
1272 #ifdef CONFIG_PERF_EVENTS
1273 if (trace_probe_test_flag(&tu->tp, TP_FLAG_PROFILE))
1274 ret |= uprobe_perf_func(tu, regs, ucb, dsize);
1276 uprobe_buffer_put(ucb);
1280 static int uretprobe_dispatcher(struct uprobe_consumer *con,
1281 unsigned long func, struct pt_regs *regs)
1283 struct trace_uprobe *tu;
1284 struct uprobe_dispatch_data udd;
1285 struct uprobe_cpu_buffer *ucb;
1288 tu = container_of(con, struct trace_uprobe, consumer);
1293 current->utask->vaddr = (unsigned long) &udd;
1295 if (WARN_ON_ONCE(!uprobe_cpu_buffer))
1298 dsize = __get_data_size(&tu->tp, regs);
1299 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1301 ucb = uprobe_buffer_get();
1302 store_trace_args(ucb->buf, &tu->tp, regs, esize, dsize);
1304 if (trace_probe_test_flag(&tu->tp, TP_FLAG_TRACE))
1305 uretprobe_trace_func(tu, func, regs, ucb, dsize);
1307 #ifdef CONFIG_PERF_EVENTS
1308 if (trace_probe_test_flag(&tu->tp, TP_FLAG_PROFILE))
1309 uretprobe_perf_func(tu, func, regs, ucb, dsize);
1311 uprobe_buffer_put(ucb);
1315 static struct trace_event_functions uprobe_funcs = {
1316 .trace = print_uprobe_event
1319 static inline void init_trace_event_call(struct trace_uprobe *tu,
1320 struct trace_event_call *call)
1322 call->event.funcs = &uprobe_funcs;
1323 call->class->define_fields = uprobe_event_define_fields;
1325 call->flags = TRACE_EVENT_FL_UPROBE;
1326 call->class->reg = trace_uprobe_register;
1330 static int register_uprobe_event(struct trace_uprobe *tu)
1332 init_trace_event_call(tu, &tu->tp.call);
1334 return trace_probe_register_event_call(&tu->tp);
1337 static int unregister_uprobe_event(struct trace_uprobe *tu)
1339 return trace_probe_unregister_event_call(&tu->tp);
1342 #ifdef CONFIG_PERF_EVENTS
1343 struct trace_event_call *
1344 create_local_trace_uprobe(char *name, unsigned long offs,
1345 unsigned long ref_ctr_offset, bool is_return)
1347 struct trace_uprobe *tu;
1351 ret = kern_path(name, LOOKUP_FOLLOW, &path);
1353 return ERR_PTR(ret);
1355 if (!d_is_reg(path.dentry)) {
1357 return ERR_PTR(-EINVAL);
1361 * local trace_kprobes are not added to dyn_event, so they are never
1362 * searched in find_trace_kprobe(). Therefore, there is no concern of
1363 * duplicated name "DUMMY_EVENT" here.
1365 tu = alloc_trace_uprobe(UPROBE_EVENT_SYSTEM, "DUMMY_EVENT", 0,
1369 pr_info("Failed to allocate trace_uprobe.(%d)\n",
1372 return ERR_CAST(tu);
1377 tu->ref_ctr_offset = ref_ctr_offset;
1378 tu->filename = kstrdup(name, GFP_KERNEL);
1379 init_trace_event_call(tu, &tu->tp.call);
1381 if (traceprobe_set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0) {
1386 return &tu->tp.call;
1388 free_trace_uprobe(tu);
1389 return ERR_PTR(ret);
1392 void destroy_local_trace_uprobe(struct trace_event_call *event_call)
1394 struct trace_uprobe *tu;
1396 tu = container_of(event_call, struct trace_uprobe, tp.call);
1398 free_trace_uprobe(tu);
1400 #endif /* CONFIG_PERF_EVENTS */
1402 /* Make a trace interface for controling probe points */
1403 static __init int init_uprobe_trace(void)
1405 struct dentry *d_tracer;
1408 ret = dyn_event_register(&trace_uprobe_ops);
1412 d_tracer = tracing_init_dentry();
1413 if (IS_ERR(d_tracer))
1416 trace_create_file("uprobe_events", 0644, d_tracer,
1417 NULL, &uprobe_events_ops);
1418 /* Profile interface */
1419 trace_create_file("uprobe_profile", 0444, d_tracer,
1420 NULL, &uprobe_profile_ops);
1424 fs_initcall(init_uprobe_trace);