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_kprobe: " 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 * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
145 * length and relative data location.
147 static nokprobe_inline int
148 fetch_store_string(unsigned long addr, void *dest, void *base)
151 u32 loc = *(u32 *)dest;
152 int maxlen = get_loc_len(loc);
153 u8 *dst = get_loc_data(dest, base);
154 void __user *src = (void __force __user *) addr;
156 if (unlikely(!maxlen))
159 ret = strncpy_from_user(dst, src, maxlen);
163 *(u32 *)dest = make_data_loc(ret, (void *)dst - base);
169 /* Return the length of string -- including null terminal byte */
170 static nokprobe_inline int
171 fetch_store_strlen(unsigned long addr)
174 void __user *vaddr = (void __force __user *) addr;
176 len = strnlen_user(vaddr, MAX_STRING_SIZE);
178 return (len > MAX_STRING_SIZE) ? 0 : len;
181 static unsigned long translate_user_vaddr(unsigned long file_offset)
183 unsigned long base_addr;
184 struct uprobe_dispatch_data *udd;
186 udd = (void *) current->utask->vaddr;
188 base_addr = udd->bp_addr - udd->tu->offset;
189 return base_addr + file_offset;
192 /* Note that we don't verify it, since the code does not come from user space */
194 process_fetch_insn(struct fetch_insn *code, struct pt_regs *regs, void *dest,
199 /* 1st stage: get value from context */
202 val = regs_get_register(regs, code->param);
205 val = get_user_stack_nth(regs, code->param);
207 case FETCH_OP_STACKP:
208 val = user_stack_pointer(regs);
210 case FETCH_OP_RETVAL:
211 val = regs_return_value(regs);
214 val = code->immediate;
217 val = translate_user_vaddr(code->immediate);
224 return process_fetch_insn_bottom(code, val, dest, base);
226 NOKPROBE_SYMBOL(process_fetch_insn)
228 static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter)
230 rwlock_init(&filter->rwlock);
231 filter->nr_systemwide = 0;
232 INIT_LIST_HEAD(&filter->perf_events);
235 static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter *filter)
237 return !filter->nr_systemwide && list_empty(&filter->perf_events);
240 static inline bool is_ret_probe(struct trace_uprobe *tu)
242 return tu->consumer.ret_handler != NULL;
245 static bool trace_uprobe_is_busy(struct dyn_event *ev)
247 struct trace_uprobe *tu = to_trace_uprobe(ev);
249 return trace_probe_is_enabled(&tu->tp);
252 static bool trace_uprobe_match(const char *system, const char *event,
253 struct dyn_event *ev)
255 struct trace_uprobe *tu = to_trace_uprobe(ev);
257 return strcmp(trace_event_name(&tu->tp.call), event) == 0 &&
258 (!system || strcmp(tu->tp.call.class->system, system) == 0);
262 * Allocate new trace_uprobe and initialize it (including uprobes).
264 static struct trace_uprobe *
265 alloc_trace_uprobe(const char *group, const char *event, int nargs, bool is_ret)
267 struct trace_uprobe *tu;
269 if (!event || !is_good_name(event))
270 return ERR_PTR(-EINVAL);
272 if (!group || !is_good_name(group))
273 return ERR_PTR(-EINVAL);
275 tu = kzalloc(SIZEOF_TRACE_UPROBE(nargs), GFP_KERNEL);
277 return ERR_PTR(-ENOMEM);
279 tu->tp.call.class = &tu->tp.class;
280 tu->tp.call.name = kstrdup(event, GFP_KERNEL);
281 if (!tu->tp.call.name)
284 tu->tp.class.system = kstrdup(group, GFP_KERNEL);
285 if (!tu->tp.class.system)
288 dyn_event_init(&tu->devent, &trace_uprobe_ops);
289 INIT_LIST_HEAD(&tu->tp.files);
290 tu->consumer.handler = uprobe_dispatcher;
292 tu->consumer.ret_handler = uretprobe_dispatcher;
293 init_trace_uprobe_filter(&tu->filter);
297 kfree(tu->tp.call.name);
300 return ERR_PTR(-ENOMEM);
303 static void free_trace_uprobe(struct trace_uprobe *tu)
310 for (i = 0; i < tu->tp.nr_args; i++)
311 traceprobe_free_probe_arg(&tu->tp.args[i]);
314 kfree(tu->tp.call.class->system);
315 kfree(tu->tp.call.name);
320 static struct trace_uprobe *find_probe_event(const char *event, const char *group)
322 struct dyn_event *pos;
323 struct trace_uprobe *tu;
325 for_each_trace_uprobe(tu, pos)
326 if (strcmp(trace_event_name(&tu->tp.call), event) == 0 &&
327 strcmp(tu->tp.call.class->system, group) == 0)
333 /* Unregister a trace_uprobe and probe_event */
334 static int unregister_trace_uprobe(struct trace_uprobe *tu)
338 ret = unregister_uprobe_event(tu);
342 dyn_event_remove(&tu->devent);
343 free_trace_uprobe(tu);
348 * Uprobe with multiple reference counter is not allowed. i.e.
349 * If inode and offset matches, reference counter offset *must*
350 * match as well. Though, there is one exception: If user is
351 * replacing old trace_uprobe with new one(same group/event),
352 * then we allow same uprobe with new reference counter as far
353 * as the new one does not conflict with any other existing
356 static struct trace_uprobe *find_old_trace_uprobe(struct trace_uprobe *new)
358 struct dyn_event *pos;
359 struct trace_uprobe *tmp, *old = NULL;
360 struct inode *new_inode = d_real_inode(new->path.dentry);
362 old = find_probe_event(trace_event_name(&new->tp.call),
363 new->tp.call.class->system);
365 for_each_trace_uprobe(tmp, pos) {
366 if ((old ? old != tmp : true) &&
367 new_inode == d_real_inode(tmp->path.dentry) &&
368 new->offset == tmp->offset &&
369 new->ref_ctr_offset != tmp->ref_ctr_offset) {
370 pr_warn("Reference counter offset mismatch.");
371 return ERR_PTR(-EINVAL);
377 /* Register a trace_uprobe and probe_event */
378 static int register_trace_uprobe(struct trace_uprobe *tu)
380 struct trace_uprobe *old_tu;
383 mutex_lock(&event_mutex);
385 /* register as an event */
386 old_tu = find_old_trace_uprobe(tu);
387 if (IS_ERR(old_tu)) {
388 ret = PTR_ERR(old_tu);
393 /* delete old event */
394 ret = unregister_trace_uprobe(old_tu);
399 ret = register_uprobe_event(tu);
401 pr_warn("Failed to register probe event(%d)\n", ret);
405 dyn_event_add(&tu->devent);
408 mutex_unlock(&event_mutex);
415 * - Add uprobe: p|r[:[GRP/]EVENT] PATH:OFFSET [FETCHARGS]
417 * - Remove uprobe: -:[GRP/]EVENT
419 static int trace_uprobe_create(int argc, const char **argv)
421 struct trace_uprobe *tu;
422 const char *event = NULL, *group = UPROBE_EVENT_SYSTEM;
423 char *arg, *filename, *rctr, *rctr_end, *tmp;
424 char buf[MAX_EVENT_NAME_LEN];
426 unsigned long offset, ref_ctr_offset;
427 bool is_return = false;
433 /* argc must be >= 1 */
434 if (argv[0][0] == 'r')
436 else if (argv[0][0] != 'p' || argc < 2)
439 if (argv[0][1] == ':')
442 if (!strchr(argv[1], '/'))
445 filename = kstrdup(argv[1], GFP_KERNEL);
449 /* Find the last occurrence, in case the path contains ':' too. */
450 arg = strrchr(filename, ':');
451 if (!arg || !isdigit(arg[1])) {
457 ret = kern_path(filename, LOOKUP_FOLLOW, &path);
462 if (!d_is_reg(path.dentry)) {
464 goto fail_address_parse;
467 /* Parse reference counter offset if specified. */
468 rctr = strchr(arg, '(');
470 rctr_end = strchr(rctr, ')');
471 if (rctr > rctr_end || *(rctr_end + 1) != 0) {
473 pr_info("Invalid reference counter offset.\n");
474 goto fail_address_parse;
479 ret = kstrtoul(rctr, 0, &ref_ctr_offset);
481 pr_info("Invalid reference counter offset.\n");
482 goto fail_address_parse;
486 /* Parse uprobe offset. */
487 ret = kstrtoul(arg, 0, &offset);
489 goto fail_address_parse;
496 ret = traceprobe_parse_event_name(&event, &group, buf);
498 goto fail_address_parse;
503 tail = kstrdup(kbasename(filename), GFP_KERNEL);
506 goto fail_address_parse;
509 ptr = strpbrk(tail, ".-_");
513 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset);
518 tu = alloc_trace_uprobe(group, event, argc, is_return);
520 pr_info("Failed to allocate trace_uprobe.(%d)\n", (int)PTR_ERR(tu));
522 goto fail_address_parse;
525 tu->ref_ctr_offset = ref_ctr_offset;
527 tu->filename = filename;
529 /* parse arguments */
530 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
531 tmp = kstrdup(argv[i], GFP_KERNEL);
537 ret = traceprobe_parse_probe_arg(&tu->tp, i, tmp,
538 is_return ? TPARG_FL_RETURN : 0);
544 ret = register_trace_uprobe(tu);
550 free_trace_uprobe(tu);
557 pr_info("Failed to parse address or file.\n");
562 static int create_or_delete_trace_uprobe(int argc, char **argv)
566 if (argv[0][0] == '-')
567 return dyn_event_release(argc, argv, &trace_uprobe_ops);
569 ret = trace_uprobe_create(argc, (const char **)argv);
570 return ret == -ECANCELED ? -EINVAL : ret;
573 static int trace_uprobe_release(struct dyn_event *ev)
575 struct trace_uprobe *tu = to_trace_uprobe(ev);
577 return unregister_trace_uprobe(tu);
580 /* Probes listing interfaces */
581 static int trace_uprobe_show(struct seq_file *m, struct dyn_event *ev)
583 struct trace_uprobe *tu = to_trace_uprobe(ev);
584 char c = is_ret_probe(tu) ? 'r' : 'p';
587 seq_printf(m, "%c:%s/%s %s:0x%0*lx", c, tu->tp.call.class->system,
588 trace_event_name(&tu->tp.call), tu->filename,
589 (int)(sizeof(void *) * 2), tu->offset);
591 if (tu->ref_ctr_offset)
592 seq_printf(m, "(0x%lx)", tu->ref_ctr_offset);
594 for (i = 0; i < tu->tp.nr_args; i++)
595 seq_printf(m, " %s=%s", tu->tp.args[i].name, tu->tp.args[i].comm);
601 static int probes_seq_show(struct seq_file *m, void *v)
603 struct dyn_event *ev = v;
605 if (!is_trace_uprobe(ev))
608 return trace_uprobe_show(m, ev);
611 static const struct seq_operations probes_seq_op = {
612 .start = dyn_event_seq_start,
613 .next = dyn_event_seq_next,
614 .stop = dyn_event_seq_stop,
615 .show = probes_seq_show
618 static int probes_open(struct inode *inode, struct file *file)
622 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
623 ret = dyn_events_release_all(&trace_uprobe_ops);
628 return seq_open(file, &probes_seq_op);
631 static ssize_t probes_write(struct file *file, const char __user *buffer,
632 size_t count, loff_t *ppos)
634 return trace_parse_run_command(file, buffer, count, ppos,
635 create_or_delete_trace_uprobe);
638 static const struct file_operations uprobe_events_ops = {
639 .owner = THIS_MODULE,
643 .release = seq_release,
644 .write = probes_write,
647 /* Probes profiling interfaces */
648 static int probes_profile_seq_show(struct seq_file *m, void *v)
650 struct dyn_event *ev = v;
651 struct trace_uprobe *tu;
653 if (!is_trace_uprobe(ev))
656 tu = to_trace_uprobe(ev);
657 seq_printf(m, " %s %-44s %15lu\n", tu->filename,
658 trace_event_name(&tu->tp.call), tu->nhit);
662 static const struct seq_operations profile_seq_op = {
663 .start = dyn_event_seq_start,
664 .next = dyn_event_seq_next,
665 .stop = dyn_event_seq_stop,
666 .show = probes_profile_seq_show
669 static int profile_open(struct inode *inode, struct file *file)
671 return seq_open(file, &profile_seq_op);
674 static const struct file_operations uprobe_profile_ops = {
675 .owner = THIS_MODULE,
676 .open = profile_open,
679 .release = seq_release,
682 struct uprobe_cpu_buffer {
686 static struct uprobe_cpu_buffer __percpu *uprobe_cpu_buffer;
687 static int uprobe_buffer_refcnt;
689 static int uprobe_buffer_init(void)
693 uprobe_cpu_buffer = alloc_percpu(struct uprobe_cpu_buffer);
694 if (uprobe_cpu_buffer == NULL)
697 for_each_possible_cpu(cpu) {
698 struct page *p = alloc_pages_node(cpu_to_node(cpu),
704 per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf = page_address(p);
705 mutex_init(&per_cpu_ptr(uprobe_cpu_buffer, cpu)->mutex);
711 for_each_possible_cpu(cpu) {
714 free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf);
717 free_percpu(uprobe_cpu_buffer);
721 static int uprobe_buffer_enable(void)
725 BUG_ON(!mutex_is_locked(&event_mutex));
727 if (uprobe_buffer_refcnt++ == 0) {
728 ret = uprobe_buffer_init();
730 uprobe_buffer_refcnt--;
736 static void uprobe_buffer_disable(void)
740 BUG_ON(!mutex_is_locked(&event_mutex));
742 if (--uprobe_buffer_refcnt == 0) {
743 for_each_possible_cpu(cpu)
744 free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer,
747 free_percpu(uprobe_cpu_buffer);
748 uprobe_cpu_buffer = NULL;
752 static struct uprobe_cpu_buffer *uprobe_buffer_get(void)
754 struct uprobe_cpu_buffer *ucb;
757 cpu = raw_smp_processor_id();
758 ucb = per_cpu_ptr(uprobe_cpu_buffer, cpu);
761 * Use per-cpu buffers for fastest access, but we might migrate
762 * so the mutex makes sure we have sole access to it.
764 mutex_lock(&ucb->mutex);
769 static void uprobe_buffer_put(struct uprobe_cpu_buffer *ucb)
771 mutex_unlock(&ucb->mutex);
774 static void __uprobe_trace_func(struct trace_uprobe *tu,
775 unsigned long func, struct pt_regs *regs,
776 struct uprobe_cpu_buffer *ucb, int dsize,
777 struct trace_event_file *trace_file)
779 struct uprobe_trace_entry_head *entry;
780 struct ring_buffer_event *event;
781 struct ring_buffer *buffer;
784 struct trace_event_call *call = &tu->tp.call;
786 WARN_ON(call != trace_file->event_call);
788 if (WARN_ON_ONCE(tu->tp.size + dsize > PAGE_SIZE))
791 if (trace_trigger_soft_disabled(trace_file))
794 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
795 size = esize + tu->tp.size + dsize;
796 event = trace_event_buffer_lock_reserve(&buffer, trace_file,
797 call->event.type, size, 0, 0);
801 entry = ring_buffer_event_data(event);
802 if (is_ret_probe(tu)) {
803 entry->vaddr[0] = func;
804 entry->vaddr[1] = instruction_pointer(regs);
805 data = DATAOF_TRACE_ENTRY(entry, true);
807 entry->vaddr[0] = instruction_pointer(regs);
808 data = DATAOF_TRACE_ENTRY(entry, false);
811 memcpy(data, ucb->buf, tu->tp.size + dsize);
813 event_trigger_unlock_commit(trace_file, buffer, event, entry, 0, 0);
817 static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs,
818 struct uprobe_cpu_buffer *ucb, int dsize)
820 struct event_file_link *link;
822 if (is_ret_probe(tu))
826 list_for_each_entry_rcu(link, &tu->tp.files, list)
827 __uprobe_trace_func(tu, 0, regs, ucb, dsize, link->file);
833 static void uretprobe_trace_func(struct trace_uprobe *tu, unsigned long func,
834 struct pt_regs *regs,
835 struct uprobe_cpu_buffer *ucb, int dsize)
837 struct event_file_link *link;
840 list_for_each_entry_rcu(link, &tu->tp.files, list)
841 __uprobe_trace_func(tu, func, regs, ucb, dsize, link->file);
845 /* Event entry printers */
846 static enum print_line_t
847 print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event)
849 struct uprobe_trace_entry_head *entry;
850 struct trace_seq *s = &iter->seq;
851 struct trace_uprobe *tu;
854 entry = (struct uprobe_trace_entry_head *)iter->ent;
855 tu = container_of(event, struct trace_uprobe, tp.call.event);
857 if (is_ret_probe(tu)) {
858 trace_seq_printf(s, "%s: (0x%lx <- 0x%lx)",
859 trace_event_name(&tu->tp.call),
860 entry->vaddr[1], entry->vaddr[0]);
861 data = DATAOF_TRACE_ENTRY(entry, true);
863 trace_seq_printf(s, "%s: (0x%lx)",
864 trace_event_name(&tu->tp.call),
866 data = DATAOF_TRACE_ENTRY(entry, false);
869 if (print_probe_args(s, tu->tp.args, tu->tp.nr_args, data, entry) < 0)
872 trace_seq_putc(s, '\n');
875 return trace_handle_return(s);
878 typedef bool (*filter_func_t)(struct uprobe_consumer *self,
879 enum uprobe_filter_ctx ctx,
880 struct mm_struct *mm);
883 probe_event_enable(struct trace_uprobe *tu, struct trace_event_file *file,
884 filter_func_t filter)
886 bool enabled = trace_probe_is_enabled(&tu->tp);
887 struct event_file_link *link = NULL;
891 if (tu->tp.flags & TP_FLAG_PROFILE)
894 link = kmalloc(sizeof(*link), GFP_KERNEL);
899 list_add_tail_rcu(&link->list, &tu->tp.files);
901 tu->tp.flags |= TP_FLAG_TRACE;
903 if (tu->tp.flags & TP_FLAG_TRACE)
906 tu->tp.flags |= TP_FLAG_PROFILE;
909 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
914 ret = uprobe_buffer_enable();
918 tu->consumer.filter = filter;
919 tu->inode = d_real_inode(tu->path.dentry);
920 if (tu->ref_ctr_offset) {
921 ret = uprobe_register_refctr(tu->inode, tu->offset,
922 tu->ref_ctr_offset, &tu->consumer);
924 ret = uprobe_register(tu->inode, tu->offset, &tu->consumer);
933 uprobe_buffer_disable();
937 list_del(&link->list);
939 tu->tp.flags &= ~TP_FLAG_TRACE;
941 tu->tp.flags &= ~TP_FLAG_PROFILE;
947 probe_event_disable(struct trace_uprobe *tu, struct trace_event_file *file)
949 if (!trace_probe_is_enabled(&tu->tp))
953 struct event_file_link *link;
955 link = find_event_file_link(&tu->tp, file);
959 list_del_rcu(&link->list);
960 /* synchronize with u{,ret}probe_trace_func */
964 if (!list_empty(&tu->tp.files))
968 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
970 uprobe_unregister(tu->inode, tu->offset, &tu->consumer);
972 tu->tp.flags &= file ? ~TP_FLAG_TRACE : ~TP_FLAG_PROFILE;
974 uprobe_buffer_disable();
977 static int uprobe_event_define_fields(struct trace_event_call *event_call)
980 struct uprobe_trace_entry_head field;
981 struct trace_uprobe *tu = event_call->data;
983 if (is_ret_probe(tu)) {
984 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_FUNC, 0);
985 DEFINE_FIELD(unsigned long, vaddr[1], FIELD_STRING_RETIP, 0);
986 size = SIZEOF_TRACE_ENTRY(true);
988 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0);
989 size = SIZEOF_TRACE_ENTRY(false);
992 return traceprobe_define_arg_fields(event_call, size, &tu->tp);
995 #ifdef CONFIG_PERF_EVENTS
997 __uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm)
999 struct perf_event *event;
1001 if (filter->nr_systemwide)
1004 list_for_each_entry(event, &filter->perf_events, hw.tp_list) {
1005 if (event->hw.target->mm == mm)
1013 uprobe_filter_event(struct trace_uprobe *tu, struct perf_event *event)
1015 return __uprobe_perf_filter(&tu->filter, event->hw.target->mm);
1018 static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event)
1022 write_lock(&tu->filter.rwlock);
1023 if (event->hw.target) {
1024 list_del(&event->hw.tp_list);
1025 done = tu->filter.nr_systemwide ||
1026 (event->hw.target->flags & PF_EXITING) ||
1027 uprobe_filter_event(tu, event);
1029 tu->filter.nr_systemwide--;
1030 done = tu->filter.nr_systemwide;
1032 write_unlock(&tu->filter.rwlock);
1035 return uprobe_apply(tu->inode, tu->offset, &tu->consumer, false);
1040 static int uprobe_perf_open(struct trace_uprobe *tu, struct perf_event *event)
1045 write_lock(&tu->filter.rwlock);
1046 if (event->hw.target) {
1048 * event->parent != NULL means copy_process(), we can avoid
1049 * uprobe_apply(). current->mm must be probed and we can rely
1050 * on dup_mmap() which preserves the already installed bp's.
1052 * attr.enable_on_exec means that exec/mmap will install the
1053 * breakpoints we need.
1055 done = tu->filter.nr_systemwide ||
1056 event->parent || event->attr.enable_on_exec ||
1057 uprobe_filter_event(tu, event);
1058 list_add(&event->hw.tp_list, &tu->filter.perf_events);
1060 done = tu->filter.nr_systemwide;
1061 tu->filter.nr_systemwide++;
1063 write_unlock(&tu->filter.rwlock);
1067 err = uprobe_apply(tu->inode, tu->offset, &tu->consumer, true);
1069 uprobe_perf_close(tu, event);
1074 static bool uprobe_perf_filter(struct uprobe_consumer *uc,
1075 enum uprobe_filter_ctx ctx, struct mm_struct *mm)
1077 struct trace_uprobe *tu;
1080 tu = container_of(uc, struct trace_uprobe, consumer);
1081 read_lock(&tu->filter.rwlock);
1082 ret = __uprobe_perf_filter(&tu->filter, mm);
1083 read_unlock(&tu->filter.rwlock);
1088 static void __uprobe_perf_func(struct trace_uprobe *tu,
1089 unsigned long func, struct pt_regs *regs,
1090 struct uprobe_cpu_buffer *ucb, int dsize)
1092 struct trace_event_call *call = &tu->tp.call;
1093 struct uprobe_trace_entry_head *entry;
1094 struct hlist_head *head;
1099 if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs))
1102 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1104 size = esize + tu->tp.size + dsize;
1105 size = ALIGN(size + sizeof(u32), sizeof(u64)) - sizeof(u32);
1106 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough"))
1110 head = this_cpu_ptr(call->perf_events);
1111 if (hlist_empty(head))
1114 entry = perf_trace_buf_alloc(size, NULL, &rctx);
1118 if (is_ret_probe(tu)) {
1119 entry->vaddr[0] = func;
1120 entry->vaddr[1] = instruction_pointer(regs);
1121 data = DATAOF_TRACE_ENTRY(entry, true);
1123 entry->vaddr[0] = instruction_pointer(regs);
1124 data = DATAOF_TRACE_ENTRY(entry, false);
1127 memcpy(data, ucb->buf, tu->tp.size + dsize);
1129 if (size - esize > tu->tp.size + dsize) {
1130 int len = tu->tp.size + dsize;
1132 memset(data + len, 0, size - esize - len);
1135 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1141 /* uprobe profile handler */
1142 static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs,
1143 struct uprobe_cpu_buffer *ucb, int dsize)
1145 if (!uprobe_perf_filter(&tu->consumer, 0, current->mm))
1146 return UPROBE_HANDLER_REMOVE;
1148 if (!is_ret_probe(tu))
1149 __uprobe_perf_func(tu, 0, regs, ucb, dsize);
1153 static void uretprobe_perf_func(struct trace_uprobe *tu, unsigned long func,
1154 struct pt_regs *regs,
1155 struct uprobe_cpu_buffer *ucb, int dsize)
1157 __uprobe_perf_func(tu, func, regs, ucb, dsize);
1160 int bpf_get_uprobe_info(const struct perf_event *event, u32 *fd_type,
1161 const char **filename, u64 *probe_offset,
1162 bool perf_type_tracepoint)
1164 const char *pevent = trace_event_name(event->tp_event);
1165 const char *group = event->tp_event->class->system;
1166 struct trace_uprobe *tu;
1168 if (perf_type_tracepoint)
1169 tu = find_probe_event(pevent, group);
1171 tu = event->tp_event->data;
1175 *fd_type = is_ret_probe(tu) ? BPF_FD_TYPE_URETPROBE
1176 : BPF_FD_TYPE_UPROBE;
1177 *filename = tu->filename;
1178 *probe_offset = tu->offset;
1181 #endif /* CONFIG_PERF_EVENTS */
1184 trace_uprobe_register(struct trace_event_call *event, enum trace_reg type,
1187 struct trace_uprobe *tu = event->data;
1188 struct trace_event_file *file = data;
1191 case TRACE_REG_REGISTER:
1192 return probe_event_enable(tu, file, NULL);
1194 case TRACE_REG_UNREGISTER:
1195 probe_event_disable(tu, file);
1198 #ifdef CONFIG_PERF_EVENTS
1199 case TRACE_REG_PERF_REGISTER:
1200 return probe_event_enable(tu, NULL, uprobe_perf_filter);
1202 case TRACE_REG_PERF_UNREGISTER:
1203 probe_event_disable(tu, NULL);
1206 case TRACE_REG_PERF_OPEN:
1207 return uprobe_perf_open(tu, data);
1209 case TRACE_REG_PERF_CLOSE:
1210 return uprobe_perf_close(tu, data);
1219 static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs)
1221 struct trace_uprobe *tu;
1222 struct uprobe_dispatch_data udd;
1223 struct uprobe_cpu_buffer *ucb;
1228 tu = container_of(con, struct trace_uprobe, consumer);
1232 udd.bp_addr = instruction_pointer(regs);
1234 current->utask->vaddr = (unsigned long) &udd;
1236 if (WARN_ON_ONCE(!uprobe_cpu_buffer))
1239 dsize = __get_data_size(&tu->tp, regs);
1240 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1242 ucb = uprobe_buffer_get();
1243 store_trace_args(ucb->buf, &tu->tp, regs, esize, dsize);
1245 if (tu->tp.flags & TP_FLAG_TRACE)
1246 ret |= uprobe_trace_func(tu, regs, ucb, dsize);
1248 #ifdef CONFIG_PERF_EVENTS
1249 if (tu->tp.flags & TP_FLAG_PROFILE)
1250 ret |= uprobe_perf_func(tu, regs, ucb, dsize);
1252 uprobe_buffer_put(ucb);
1256 static int uretprobe_dispatcher(struct uprobe_consumer *con,
1257 unsigned long func, struct pt_regs *regs)
1259 struct trace_uprobe *tu;
1260 struct uprobe_dispatch_data udd;
1261 struct uprobe_cpu_buffer *ucb;
1264 tu = container_of(con, struct trace_uprobe, consumer);
1269 current->utask->vaddr = (unsigned long) &udd;
1271 if (WARN_ON_ONCE(!uprobe_cpu_buffer))
1274 dsize = __get_data_size(&tu->tp, regs);
1275 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1277 ucb = uprobe_buffer_get();
1278 store_trace_args(ucb->buf, &tu->tp, regs, esize, dsize);
1280 if (tu->tp.flags & TP_FLAG_TRACE)
1281 uretprobe_trace_func(tu, func, regs, ucb, dsize);
1283 #ifdef CONFIG_PERF_EVENTS
1284 if (tu->tp.flags & TP_FLAG_PROFILE)
1285 uretprobe_perf_func(tu, func, regs, ucb, dsize);
1287 uprobe_buffer_put(ucb);
1291 static struct trace_event_functions uprobe_funcs = {
1292 .trace = print_uprobe_event
1295 static inline void init_trace_event_call(struct trace_uprobe *tu,
1296 struct trace_event_call *call)
1298 INIT_LIST_HEAD(&call->class->fields);
1299 call->event.funcs = &uprobe_funcs;
1300 call->class->define_fields = uprobe_event_define_fields;
1302 call->flags = TRACE_EVENT_FL_UPROBE;
1303 call->class->reg = trace_uprobe_register;
1307 static int register_uprobe_event(struct trace_uprobe *tu)
1309 struct trace_event_call *call = &tu->tp.call;
1312 init_trace_event_call(tu, call);
1314 if (traceprobe_set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0)
1317 ret = register_trace_event(&call->event);
1319 kfree(call->print_fmt);
1323 ret = trace_add_event_call_nolock(call);
1326 pr_info("Failed to register uprobe event: %s\n",
1327 trace_event_name(call));
1328 kfree(call->print_fmt);
1329 unregister_trace_event(&call->event);
1335 static int unregister_uprobe_event(struct trace_uprobe *tu)
1339 /* tu->event is unregistered in trace_remove_event_call() */
1340 ret = trace_remove_event_call_nolock(&tu->tp.call);
1343 kfree(tu->tp.call.print_fmt);
1344 tu->tp.call.print_fmt = NULL;
1348 #ifdef CONFIG_PERF_EVENTS
1349 struct trace_event_call *
1350 create_local_trace_uprobe(char *name, unsigned long offs,
1351 unsigned long ref_ctr_offset, bool is_return)
1353 struct trace_uprobe *tu;
1357 ret = kern_path(name, LOOKUP_FOLLOW, &path);
1359 return ERR_PTR(ret);
1361 if (!d_is_reg(path.dentry)) {
1363 return ERR_PTR(-EINVAL);
1367 * local trace_kprobes are not added to dyn_event, so they are never
1368 * searched in find_trace_kprobe(). Therefore, there is no concern of
1369 * duplicated name "DUMMY_EVENT" here.
1371 tu = alloc_trace_uprobe(UPROBE_EVENT_SYSTEM, "DUMMY_EVENT", 0,
1375 pr_info("Failed to allocate trace_uprobe.(%d)\n",
1378 return ERR_CAST(tu);
1383 tu->ref_ctr_offset = ref_ctr_offset;
1384 tu->filename = kstrdup(name, GFP_KERNEL);
1385 init_trace_event_call(tu, &tu->tp.call);
1387 if (traceprobe_set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0) {
1392 return &tu->tp.call;
1394 free_trace_uprobe(tu);
1395 return ERR_PTR(ret);
1398 void destroy_local_trace_uprobe(struct trace_event_call *event_call)
1400 struct trace_uprobe *tu;
1402 tu = container_of(event_call, struct trace_uprobe, tp.call);
1404 kfree(tu->tp.call.print_fmt);
1405 tu->tp.call.print_fmt = NULL;
1407 free_trace_uprobe(tu);
1409 #endif /* CONFIG_PERF_EVENTS */
1411 /* Make a trace interface for controling probe points */
1412 static __init int init_uprobe_trace(void)
1414 struct dentry *d_tracer;
1417 ret = dyn_event_register(&trace_uprobe_ops);
1421 d_tracer = tracing_init_dentry();
1422 if (IS_ERR(d_tracer))
1425 trace_create_file("uprobe_events", 0644, d_tracer,
1426 NULL, &uprobe_events_ops);
1427 /* Profile interface */
1428 trace_create_file("uprobe_profile", 0444, d_tracer,
1429 NULL, &uprobe_profile_ops);
1433 fs_initcall(init_uprobe_trace);