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/module.h>
11 #include <linux/uaccess.h>
12 #include <linux/uprobes.h>
13 #include <linux/namei.h>
14 #include <linux/string.h>
15 #include <linux/rculist.h>
17 #include "trace_probe.h"
18 #include "trace_probe_tmpl.h"
20 #define UPROBE_EVENT_SYSTEM "uprobes"
22 struct uprobe_trace_entry_head {
23 struct trace_entry ent;
24 unsigned long vaddr[];
27 #define SIZEOF_TRACE_ENTRY(is_return) \
28 (sizeof(struct uprobe_trace_entry_head) + \
29 sizeof(unsigned long) * (is_return ? 2 : 1))
31 #define DATAOF_TRACE_ENTRY(entry, is_return) \
32 ((void*)(entry) + SIZEOF_TRACE_ENTRY(is_return))
34 struct trace_uprobe_filter {
37 struct list_head perf_events;
41 * uprobe event core functions
44 struct list_head list;
45 struct trace_uprobe_filter filter;
46 struct uprobe_consumer consumer;
51 unsigned long ref_ctr_offset;
53 struct trace_probe tp;
56 #define SIZEOF_TRACE_UPROBE(n) \
57 (offsetof(struct trace_uprobe, tp.args) + \
58 (sizeof(struct probe_arg) * (n)))
60 static int register_uprobe_event(struct trace_uprobe *tu);
61 static int unregister_uprobe_event(struct trace_uprobe *tu);
63 static DEFINE_MUTEX(uprobe_lock);
64 static LIST_HEAD(uprobe_list);
66 struct uprobe_dispatch_data {
67 struct trace_uprobe *tu;
68 unsigned long bp_addr;
71 static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs);
72 static int uretprobe_dispatcher(struct uprobe_consumer *con,
73 unsigned long func, struct pt_regs *regs);
75 #ifdef CONFIG_STACK_GROWSUP
76 static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n)
78 return addr - (n * sizeof(long));
81 static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n)
83 return addr + (n * sizeof(long));
87 static unsigned long get_user_stack_nth(struct pt_regs *regs, unsigned int n)
90 unsigned long addr = user_stack_pointer(regs);
92 addr = adjust_stack_addr(addr, n);
94 if (copy_from_user(&ret, (void __force __user *) addr, sizeof(ret)))
101 * Uprobes-specific fetch functions
103 static nokprobe_inline int
104 probe_mem_read(void *dest, void *src, size_t size)
106 void __user *vaddr = (void __force __user *)src;
108 return copy_from_user(dest, vaddr, size) ? -EFAULT : 0;
111 * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
112 * length and relative data location.
114 static nokprobe_inline int
115 fetch_store_string(unsigned long addr, void *dest, void *base)
118 u32 loc = *(u32 *)dest;
119 int maxlen = get_loc_len(loc);
120 u8 *dst = get_loc_data(dest, base);
121 void __user *src = (void __force __user *) addr;
123 if (unlikely(!maxlen))
126 ret = strncpy_from_user(dst, src, maxlen);
130 *(u32 *)dest = make_data_loc(ret, (void *)dst - base);
136 /* Return the length of string -- including null terminal byte */
137 static nokprobe_inline int
138 fetch_store_strlen(unsigned long addr)
141 void __user *vaddr = (void __force __user *) addr;
143 len = strnlen_user(vaddr, MAX_STRING_SIZE);
145 return (len > MAX_STRING_SIZE) ? 0 : len;
148 static unsigned long translate_user_vaddr(unsigned long file_offset)
150 unsigned long base_addr;
151 struct uprobe_dispatch_data *udd;
153 udd = (void *) current->utask->vaddr;
155 base_addr = udd->bp_addr - udd->tu->offset;
156 return base_addr + file_offset;
159 /* Note that we don't verify it, since the code does not come from user space */
161 process_fetch_insn(struct fetch_insn *code, struct pt_regs *regs, void *dest,
166 /* 1st stage: get value from context */
169 val = regs_get_register(regs, code->param);
172 val = get_user_stack_nth(regs, code->param);
174 case FETCH_OP_STACKP:
175 val = user_stack_pointer(regs);
177 case FETCH_OP_RETVAL:
178 val = regs_return_value(regs);
181 val = code->immediate;
184 val = translate_user_vaddr(code->immediate);
191 return process_fetch_insn_bottom(code, val, dest, base);
193 NOKPROBE_SYMBOL(process_fetch_insn)
195 static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter)
197 rwlock_init(&filter->rwlock);
198 filter->nr_systemwide = 0;
199 INIT_LIST_HEAD(&filter->perf_events);
202 static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter *filter)
204 return !filter->nr_systemwide && list_empty(&filter->perf_events);
207 static inline bool is_ret_probe(struct trace_uprobe *tu)
209 return tu->consumer.ret_handler != NULL;
213 * Allocate new trace_uprobe and initialize it (including uprobes).
215 static struct trace_uprobe *
216 alloc_trace_uprobe(const char *group, const char *event, int nargs, bool is_ret)
218 struct trace_uprobe *tu;
220 if (!event || !is_good_name(event))
221 return ERR_PTR(-EINVAL);
223 if (!group || !is_good_name(group))
224 return ERR_PTR(-EINVAL);
226 tu = kzalloc(SIZEOF_TRACE_UPROBE(nargs), GFP_KERNEL);
228 return ERR_PTR(-ENOMEM);
230 tu->tp.call.class = &tu->tp.class;
231 tu->tp.call.name = kstrdup(event, GFP_KERNEL);
232 if (!tu->tp.call.name)
235 tu->tp.class.system = kstrdup(group, GFP_KERNEL);
236 if (!tu->tp.class.system)
239 INIT_LIST_HEAD(&tu->list);
240 INIT_LIST_HEAD(&tu->tp.files);
241 tu->consumer.handler = uprobe_dispatcher;
243 tu->consumer.ret_handler = uretprobe_dispatcher;
244 init_trace_uprobe_filter(&tu->filter);
248 kfree(tu->tp.call.name);
251 return ERR_PTR(-ENOMEM);
254 static void free_trace_uprobe(struct trace_uprobe *tu)
258 for (i = 0; i < tu->tp.nr_args; i++)
259 traceprobe_free_probe_arg(&tu->tp.args[i]);
262 kfree(tu->tp.call.class->system);
263 kfree(tu->tp.call.name);
268 static struct trace_uprobe *find_probe_event(const char *event, const char *group)
270 struct trace_uprobe *tu;
272 list_for_each_entry(tu, &uprobe_list, list)
273 if (strcmp(trace_event_name(&tu->tp.call), event) == 0 &&
274 strcmp(tu->tp.call.class->system, group) == 0)
280 /* Unregister a trace_uprobe and probe_event: call with locking uprobe_lock */
281 static int unregister_trace_uprobe(struct trace_uprobe *tu)
285 ret = unregister_uprobe_event(tu);
290 free_trace_uprobe(tu);
295 * Uprobe with multiple reference counter is not allowed. i.e.
296 * If inode and offset matches, reference counter offset *must*
297 * match as well. Though, there is one exception: If user is
298 * replacing old trace_uprobe with new one(same group/event),
299 * then we allow same uprobe with new reference counter as far
300 * as the new one does not conflict with any other existing
303 static struct trace_uprobe *find_old_trace_uprobe(struct trace_uprobe *new)
305 struct trace_uprobe *tmp, *old = NULL;
306 struct inode *new_inode = d_real_inode(new->path.dentry);
308 old = find_probe_event(trace_event_name(&new->tp.call),
309 new->tp.call.class->system);
311 list_for_each_entry(tmp, &uprobe_list, list) {
312 if ((old ? old != tmp : true) &&
313 new_inode == d_real_inode(tmp->path.dentry) &&
314 new->offset == tmp->offset &&
315 new->ref_ctr_offset != tmp->ref_ctr_offset) {
316 pr_warn("Reference counter offset mismatch.");
317 return ERR_PTR(-EINVAL);
323 /* Register a trace_uprobe and probe_event */
324 static int register_trace_uprobe(struct trace_uprobe *tu)
326 struct trace_uprobe *old_tu;
329 mutex_lock(&uprobe_lock);
331 /* register as an event */
332 old_tu = find_old_trace_uprobe(tu);
333 if (IS_ERR(old_tu)) {
334 ret = PTR_ERR(old_tu);
339 /* delete old event */
340 ret = unregister_trace_uprobe(old_tu);
345 ret = register_uprobe_event(tu);
347 pr_warn("Failed to register probe event(%d)\n", ret);
351 list_add_tail(&tu->list, &uprobe_list);
354 mutex_unlock(&uprobe_lock);
361 * - Add uprobe: p|r[:[GRP/]EVENT] PATH:OFFSET [FETCHARGS]
363 * - Remove uprobe: -:[GRP/]EVENT
365 static int create_trace_uprobe(int argc, char **argv)
367 struct trace_uprobe *tu;
368 char *arg, *event, *group, *filename, *rctr, *rctr_end;
369 char buf[MAX_EVENT_NAME_LEN];
371 unsigned long offset, ref_ctr_offset;
372 bool is_delete, is_return;
382 /* argc must be >= 1 */
383 if (argv[0][0] == '-')
385 else if (argv[0][0] == 'r')
387 else if (argv[0][0] != 'p') {
388 pr_info("Probe definition must be started with 'p', 'r' or '-'.\n");
392 if (argv[0][1] == ':') {
394 arg = strchr(event, '/');
401 if (strlen(group) == 0) {
402 pr_info("Group name is not specified\n");
406 if (strlen(event) == 0) {
407 pr_info("Event name is not specified\n");
412 group = UPROBE_EVENT_SYSTEM;
418 pr_info("Delete command needs an event name.\n");
421 mutex_lock(&uprobe_lock);
422 tu = find_probe_event(event, group);
425 mutex_unlock(&uprobe_lock);
426 pr_info("Event %s/%s doesn't exist.\n", group, event);
429 /* delete an event */
430 ret = unregister_trace_uprobe(tu);
431 mutex_unlock(&uprobe_lock);
436 pr_info("Probe point is not specified.\n");
439 /* Find the last occurrence, in case the path contains ':' too. */
440 arg = strrchr(argv[1], ':');
446 ret = kern_path(filename, LOOKUP_FOLLOW, &path);
450 if (!d_is_reg(path.dentry)) {
452 goto fail_address_parse;
455 /* Parse reference counter offset if specified. */
456 rctr = strchr(arg, '(');
458 rctr_end = strchr(rctr, ')');
459 if (rctr > rctr_end || *(rctr_end + 1) != 0) {
461 pr_info("Invalid reference counter offset.\n");
462 goto fail_address_parse;
467 ret = kstrtoul(rctr, 0, &ref_ctr_offset);
469 pr_info("Invalid reference counter offset.\n");
470 goto fail_address_parse;
474 /* Parse uprobe offset. */
475 ret = kstrtoul(arg, 0, &offset);
477 goto fail_address_parse;
487 tail = kstrdup(kbasename(filename), GFP_KERNEL);
490 goto fail_address_parse;
493 ptr = strpbrk(tail, ".-_");
497 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset);
502 tu = alloc_trace_uprobe(group, event, argc, is_return);
504 pr_info("Failed to allocate trace_uprobe.(%d)\n", (int)PTR_ERR(tu));
506 goto fail_address_parse;
509 tu->ref_ctr_offset = ref_ctr_offset;
511 tu->filename = kstrdup(filename, GFP_KERNEL);
514 pr_info("Failed to allocate filename.\n");
519 /* parse arguments */
521 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
522 struct probe_arg *parg = &tu->tp.args[i];
524 /* Increment count for freeing args in error case */
527 /* Parse argument name */
528 arg = strchr(argv[i], '=');
531 parg->name = kstrdup(argv[i], GFP_KERNEL);
534 /* If argument name is omitted, set "argN" */
535 snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
536 parg->name = kstrdup(buf, GFP_KERNEL);
540 pr_info("Failed to allocate argument[%d] name.\n", i);
545 if (!is_good_name(parg->name)) {
546 pr_info("Invalid argument[%d] name: %s\n", i, parg->name);
551 if (traceprobe_conflict_field_name(parg->name, tu->tp.args, i)) {
552 pr_info("Argument[%d] name '%s' conflicts with "
553 "another field.\n", i, argv[i]);
558 /* Parse fetch argument */
559 ret = traceprobe_parse_probe_arg(arg, &tu->tp.size, parg,
560 is_return ? TPARG_FL_RETURN : 0);
562 pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
567 ret = register_trace_uprobe(tu);
573 free_trace_uprobe(tu);
579 pr_info("Failed to parse address or file.\n");
584 static int cleanup_all_probes(void)
586 struct trace_uprobe *tu;
589 mutex_lock(&uprobe_lock);
590 while (!list_empty(&uprobe_list)) {
591 tu = list_entry(uprobe_list.next, struct trace_uprobe, list);
592 ret = unregister_trace_uprobe(tu);
596 mutex_unlock(&uprobe_lock);
600 /* Probes listing interfaces */
601 static void *probes_seq_start(struct seq_file *m, loff_t *pos)
603 mutex_lock(&uprobe_lock);
604 return seq_list_start(&uprobe_list, *pos);
607 static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
609 return seq_list_next(v, &uprobe_list, pos);
612 static void probes_seq_stop(struct seq_file *m, void *v)
614 mutex_unlock(&uprobe_lock);
617 static int probes_seq_show(struct seq_file *m, void *v)
619 struct trace_uprobe *tu = v;
620 char c = is_ret_probe(tu) ? 'r' : 'p';
623 seq_printf(m, "%c:%s/%s %s:0x%0*lx", c, tu->tp.call.class->system,
624 trace_event_name(&tu->tp.call), tu->filename,
625 (int)(sizeof(void *) * 2), tu->offset);
627 if (tu->ref_ctr_offset)
628 seq_printf(m, "(0x%lx)", tu->ref_ctr_offset);
630 for (i = 0; i < tu->tp.nr_args; i++)
631 seq_printf(m, " %s=%s", tu->tp.args[i].name, tu->tp.args[i].comm);
637 static const struct seq_operations probes_seq_op = {
638 .start = probes_seq_start,
639 .next = probes_seq_next,
640 .stop = probes_seq_stop,
641 .show = probes_seq_show
644 static int probes_open(struct inode *inode, struct file *file)
648 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
649 ret = cleanup_all_probes();
654 return seq_open(file, &probes_seq_op);
657 static ssize_t probes_write(struct file *file, const char __user *buffer,
658 size_t count, loff_t *ppos)
660 return trace_parse_run_command(file, buffer, count, ppos, create_trace_uprobe);
663 static const struct file_operations uprobe_events_ops = {
664 .owner = THIS_MODULE,
668 .release = seq_release,
669 .write = probes_write,
672 /* Probes profiling interfaces */
673 static int probes_profile_seq_show(struct seq_file *m, void *v)
675 struct trace_uprobe *tu = v;
677 seq_printf(m, " %s %-44s %15lu\n", tu->filename,
678 trace_event_name(&tu->tp.call), tu->nhit);
682 static const struct seq_operations profile_seq_op = {
683 .start = probes_seq_start,
684 .next = probes_seq_next,
685 .stop = probes_seq_stop,
686 .show = probes_profile_seq_show
689 static int profile_open(struct inode *inode, struct file *file)
691 return seq_open(file, &profile_seq_op);
694 static const struct file_operations uprobe_profile_ops = {
695 .owner = THIS_MODULE,
696 .open = profile_open,
699 .release = seq_release,
702 struct uprobe_cpu_buffer {
706 static struct uprobe_cpu_buffer __percpu *uprobe_cpu_buffer;
707 static int uprobe_buffer_refcnt;
709 static int uprobe_buffer_init(void)
713 uprobe_cpu_buffer = alloc_percpu(struct uprobe_cpu_buffer);
714 if (uprobe_cpu_buffer == NULL)
717 for_each_possible_cpu(cpu) {
718 struct page *p = alloc_pages_node(cpu_to_node(cpu),
724 per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf = page_address(p);
725 mutex_init(&per_cpu_ptr(uprobe_cpu_buffer, cpu)->mutex);
731 for_each_possible_cpu(cpu) {
734 free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf);
737 free_percpu(uprobe_cpu_buffer);
741 static int uprobe_buffer_enable(void)
745 BUG_ON(!mutex_is_locked(&event_mutex));
747 if (uprobe_buffer_refcnt++ == 0) {
748 ret = uprobe_buffer_init();
750 uprobe_buffer_refcnt--;
756 static void uprobe_buffer_disable(void)
760 BUG_ON(!mutex_is_locked(&event_mutex));
762 if (--uprobe_buffer_refcnt == 0) {
763 for_each_possible_cpu(cpu)
764 free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer,
767 free_percpu(uprobe_cpu_buffer);
768 uprobe_cpu_buffer = NULL;
772 static struct uprobe_cpu_buffer *uprobe_buffer_get(void)
774 struct uprobe_cpu_buffer *ucb;
777 cpu = raw_smp_processor_id();
778 ucb = per_cpu_ptr(uprobe_cpu_buffer, cpu);
781 * Use per-cpu buffers for fastest access, but we might migrate
782 * so the mutex makes sure we have sole access to it.
784 mutex_lock(&ucb->mutex);
789 static void uprobe_buffer_put(struct uprobe_cpu_buffer *ucb)
791 mutex_unlock(&ucb->mutex);
794 static void __uprobe_trace_func(struct trace_uprobe *tu,
795 unsigned long func, struct pt_regs *regs,
796 struct uprobe_cpu_buffer *ucb, int dsize,
797 struct trace_event_file *trace_file)
799 struct uprobe_trace_entry_head *entry;
800 struct ring_buffer_event *event;
801 struct ring_buffer *buffer;
804 struct trace_event_call *call = &tu->tp.call;
806 WARN_ON(call != trace_file->event_call);
808 if (WARN_ON_ONCE(tu->tp.size + dsize > PAGE_SIZE))
811 if (trace_trigger_soft_disabled(trace_file))
814 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
815 size = esize + tu->tp.size + dsize;
816 event = trace_event_buffer_lock_reserve(&buffer, trace_file,
817 call->event.type, size, 0, 0);
821 entry = ring_buffer_event_data(event);
822 if (is_ret_probe(tu)) {
823 entry->vaddr[0] = func;
824 entry->vaddr[1] = instruction_pointer(regs);
825 data = DATAOF_TRACE_ENTRY(entry, true);
827 entry->vaddr[0] = instruction_pointer(regs);
828 data = DATAOF_TRACE_ENTRY(entry, false);
831 memcpy(data, ucb->buf, tu->tp.size + dsize);
833 event_trigger_unlock_commit(trace_file, buffer, event, entry, 0, 0);
837 static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs,
838 struct uprobe_cpu_buffer *ucb, int dsize)
840 struct event_file_link *link;
842 if (is_ret_probe(tu))
846 list_for_each_entry_rcu(link, &tu->tp.files, list)
847 __uprobe_trace_func(tu, 0, regs, ucb, dsize, link->file);
853 static void uretprobe_trace_func(struct trace_uprobe *tu, unsigned long func,
854 struct pt_regs *regs,
855 struct uprobe_cpu_buffer *ucb, int dsize)
857 struct event_file_link *link;
860 list_for_each_entry_rcu(link, &tu->tp.files, list)
861 __uprobe_trace_func(tu, func, regs, ucb, dsize, link->file);
865 /* Event entry printers */
866 static enum print_line_t
867 print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event)
869 struct uprobe_trace_entry_head *entry;
870 struct trace_seq *s = &iter->seq;
871 struct trace_uprobe *tu;
874 entry = (struct uprobe_trace_entry_head *)iter->ent;
875 tu = container_of(event, struct trace_uprobe, tp.call.event);
877 if (is_ret_probe(tu)) {
878 trace_seq_printf(s, "%s: (0x%lx <- 0x%lx)",
879 trace_event_name(&tu->tp.call),
880 entry->vaddr[1], entry->vaddr[0]);
881 data = DATAOF_TRACE_ENTRY(entry, true);
883 trace_seq_printf(s, "%s: (0x%lx)",
884 trace_event_name(&tu->tp.call),
886 data = DATAOF_TRACE_ENTRY(entry, false);
889 if (print_probe_args(s, tu->tp.args, tu->tp.nr_args, data, entry) < 0)
892 trace_seq_putc(s, '\n');
895 return trace_handle_return(s);
898 typedef bool (*filter_func_t)(struct uprobe_consumer *self,
899 enum uprobe_filter_ctx ctx,
900 struct mm_struct *mm);
903 probe_event_enable(struct trace_uprobe *tu, struct trace_event_file *file,
904 filter_func_t filter)
906 bool enabled = trace_probe_is_enabled(&tu->tp);
907 struct event_file_link *link = NULL;
911 if (tu->tp.flags & TP_FLAG_PROFILE)
914 link = kmalloc(sizeof(*link), GFP_KERNEL);
919 list_add_tail_rcu(&link->list, &tu->tp.files);
921 tu->tp.flags |= TP_FLAG_TRACE;
923 if (tu->tp.flags & TP_FLAG_TRACE)
926 tu->tp.flags |= TP_FLAG_PROFILE;
929 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
934 ret = uprobe_buffer_enable();
938 tu->consumer.filter = filter;
939 tu->inode = d_real_inode(tu->path.dentry);
940 if (tu->ref_ctr_offset) {
941 ret = uprobe_register_refctr(tu->inode, tu->offset,
942 tu->ref_ctr_offset, &tu->consumer);
944 ret = uprobe_register(tu->inode, tu->offset, &tu->consumer);
953 uprobe_buffer_disable();
957 list_del(&link->list);
959 tu->tp.flags &= ~TP_FLAG_TRACE;
961 tu->tp.flags &= ~TP_FLAG_PROFILE;
967 probe_event_disable(struct trace_uprobe *tu, struct trace_event_file *file)
969 if (!trace_probe_is_enabled(&tu->tp))
973 struct event_file_link *link;
975 link = find_event_file_link(&tu->tp, file);
979 list_del_rcu(&link->list);
980 /* synchronize with u{,ret}probe_trace_func */
984 if (!list_empty(&tu->tp.files))
988 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
990 uprobe_unregister(tu->inode, tu->offset, &tu->consumer);
992 tu->tp.flags &= file ? ~TP_FLAG_TRACE : ~TP_FLAG_PROFILE;
994 uprobe_buffer_disable();
997 static int uprobe_event_define_fields(struct trace_event_call *event_call)
1000 struct uprobe_trace_entry_head field;
1001 struct trace_uprobe *tu = event_call->data;
1003 if (is_ret_probe(tu)) {
1004 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_FUNC, 0);
1005 DEFINE_FIELD(unsigned long, vaddr[1], FIELD_STRING_RETIP, 0);
1006 size = SIZEOF_TRACE_ENTRY(true);
1008 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0);
1009 size = SIZEOF_TRACE_ENTRY(false);
1012 return traceprobe_define_arg_fields(event_call, size, &tu->tp);
1015 #ifdef CONFIG_PERF_EVENTS
1017 __uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm)
1019 struct perf_event *event;
1021 if (filter->nr_systemwide)
1024 list_for_each_entry(event, &filter->perf_events, hw.tp_list) {
1025 if (event->hw.target->mm == mm)
1033 uprobe_filter_event(struct trace_uprobe *tu, struct perf_event *event)
1035 return __uprobe_perf_filter(&tu->filter, event->hw.target->mm);
1038 static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event)
1042 write_lock(&tu->filter.rwlock);
1043 if (event->hw.target) {
1044 list_del(&event->hw.tp_list);
1045 done = tu->filter.nr_systemwide ||
1046 (event->hw.target->flags & PF_EXITING) ||
1047 uprobe_filter_event(tu, event);
1049 tu->filter.nr_systemwide--;
1050 done = tu->filter.nr_systemwide;
1052 write_unlock(&tu->filter.rwlock);
1055 return uprobe_apply(tu->inode, tu->offset, &tu->consumer, false);
1060 static int uprobe_perf_open(struct trace_uprobe *tu, struct perf_event *event)
1065 write_lock(&tu->filter.rwlock);
1066 if (event->hw.target) {
1068 * event->parent != NULL means copy_process(), we can avoid
1069 * uprobe_apply(). current->mm must be probed and we can rely
1070 * on dup_mmap() which preserves the already installed bp's.
1072 * attr.enable_on_exec means that exec/mmap will install the
1073 * breakpoints we need.
1075 done = tu->filter.nr_systemwide ||
1076 event->parent || event->attr.enable_on_exec ||
1077 uprobe_filter_event(tu, event);
1078 list_add(&event->hw.tp_list, &tu->filter.perf_events);
1080 done = tu->filter.nr_systemwide;
1081 tu->filter.nr_systemwide++;
1083 write_unlock(&tu->filter.rwlock);
1087 err = uprobe_apply(tu->inode, tu->offset, &tu->consumer, true);
1089 uprobe_perf_close(tu, event);
1094 static bool uprobe_perf_filter(struct uprobe_consumer *uc,
1095 enum uprobe_filter_ctx ctx, struct mm_struct *mm)
1097 struct trace_uprobe *tu;
1100 tu = container_of(uc, struct trace_uprobe, consumer);
1101 read_lock(&tu->filter.rwlock);
1102 ret = __uprobe_perf_filter(&tu->filter, mm);
1103 read_unlock(&tu->filter.rwlock);
1108 static void __uprobe_perf_func(struct trace_uprobe *tu,
1109 unsigned long func, struct pt_regs *regs,
1110 struct uprobe_cpu_buffer *ucb, int dsize)
1112 struct trace_event_call *call = &tu->tp.call;
1113 struct uprobe_trace_entry_head *entry;
1114 struct hlist_head *head;
1119 if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs))
1122 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1124 size = esize + tu->tp.size + dsize;
1125 size = ALIGN(size + sizeof(u32), sizeof(u64)) - sizeof(u32);
1126 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough"))
1130 head = this_cpu_ptr(call->perf_events);
1131 if (hlist_empty(head))
1134 entry = perf_trace_buf_alloc(size, NULL, &rctx);
1138 if (is_ret_probe(tu)) {
1139 entry->vaddr[0] = func;
1140 entry->vaddr[1] = instruction_pointer(regs);
1141 data = DATAOF_TRACE_ENTRY(entry, true);
1143 entry->vaddr[0] = instruction_pointer(regs);
1144 data = DATAOF_TRACE_ENTRY(entry, false);
1147 memcpy(data, ucb->buf, tu->tp.size + dsize);
1149 if (size - esize > tu->tp.size + dsize) {
1150 int len = tu->tp.size + dsize;
1152 memset(data + len, 0, size - esize - len);
1155 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1161 /* uprobe profile handler */
1162 static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs,
1163 struct uprobe_cpu_buffer *ucb, int dsize)
1165 if (!uprobe_perf_filter(&tu->consumer, 0, current->mm))
1166 return UPROBE_HANDLER_REMOVE;
1168 if (!is_ret_probe(tu))
1169 __uprobe_perf_func(tu, 0, regs, ucb, dsize);
1173 static void uretprobe_perf_func(struct trace_uprobe *tu, unsigned long func,
1174 struct pt_regs *regs,
1175 struct uprobe_cpu_buffer *ucb, int dsize)
1177 __uprobe_perf_func(tu, func, regs, ucb, dsize);
1180 int bpf_get_uprobe_info(const struct perf_event *event, u32 *fd_type,
1181 const char **filename, u64 *probe_offset,
1182 bool perf_type_tracepoint)
1184 const char *pevent = trace_event_name(event->tp_event);
1185 const char *group = event->tp_event->class->system;
1186 struct trace_uprobe *tu;
1188 if (perf_type_tracepoint)
1189 tu = find_probe_event(pevent, group);
1191 tu = event->tp_event->data;
1195 *fd_type = is_ret_probe(tu) ? BPF_FD_TYPE_URETPROBE
1196 : BPF_FD_TYPE_UPROBE;
1197 *filename = tu->filename;
1198 *probe_offset = tu->offset;
1201 #endif /* CONFIG_PERF_EVENTS */
1204 trace_uprobe_register(struct trace_event_call *event, enum trace_reg type,
1207 struct trace_uprobe *tu = event->data;
1208 struct trace_event_file *file = data;
1211 case TRACE_REG_REGISTER:
1212 return probe_event_enable(tu, file, NULL);
1214 case TRACE_REG_UNREGISTER:
1215 probe_event_disable(tu, file);
1218 #ifdef CONFIG_PERF_EVENTS
1219 case TRACE_REG_PERF_REGISTER:
1220 return probe_event_enable(tu, NULL, uprobe_perf_filter);
1222 case TRACE_REG_PERF_UNREGISTER:
1223 probe_event_disable(tu, NULL);
1226 case TRACE_REG_PERF_OPEN:
1227 return uprobe_perf_open(tu, data);
1229 case TRACE_REG_PERF_CLOSE:
1230 return uprobe_perf_close(tu, data);
1239 static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs)
1241 struct trace_uprobe *tu;
1242 struct uprobe_dispatch_data udd;
1243 struct uprobe_cpu_buffer *ucb;
1248 tu = container_of(con, struct trace_uprobe, consumer);
1252 udd.bp_addr = instruction_pointer(regs);
1254 current->utask->vaddr = (unsigned long) &udd;
1256 if (WARN_ON_ONCE(!uprobe_cpu_buffer))
1259 dsize = __get_data_size(&tu->tp, regs);
1260 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1262 ucb = uprobe_buffer_get();
1263 store_trace_args(ucb->buf, &tu->tp, regs, esize, dsize);
1265 if (tu->tp.flags & TP_FLAG_TRACE)
1266 ret |= uprobe_trace_func(tu, regs, ucb, dsize);
1268 #ifdef CONFIG_PERF_EVENTS
1269 if (tu->tp.flags & TP_FLAG_PROFILE)
1270 ret |= uprobe_perf_func(tu, regs, ucb, dsize);
1272 uprobe_buffer_put(ucb);
1276 static int uretprobe_dispatcher(struct uprobe_consumer *con,
1277 unsigned long func, struct pt_regs *regs)
1279 struct trace_uprobe *tu;
1280 struct uprobe_dispatch_data udd;
1281 struct uprobe_cpu_buffer *ucb;
1284 tu = container_of(con, struct trace_uprobe, consumer);
1289 current->utask->vaddr = (unsigned long) &udd;
1291 if (WARN_ON_ONCE(!uprobe_cpu_buffer))
1294 dsize = __get_data_size(&tu->tp, regs);
1295 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1297 ucb = uprobe_buffer_get();
1298 store_trace_args(ucb->buf, &tu->tp, regs, esize, dsize);
1300 if (tu->tp.flags & TP_FLAG_TRACE)
1301 uretprobe_trace_func(tu, func, regs, ucb, dsize);
1303 #ifdef CONFIG_PERF_EVENTS
1304 if (tu->tp.flags & TP_FLAG_PROFILE)
1305 uretprobe_perf_func(tu, func, regs, ucb, dsize);
1307 uprobe_buffer_put(ucb);
1311 static struct trace_event_functions uprobe_funcs = {
1312 .trace = print_uprobe_event
1315 static inline void init_trace_event_call(struct trace_uprobe *tu,
1316 struct trace_event_call *call)
1318 INIT_LIST_HEAD(&call->class->fields);
1319 call->event.funcs = &uprobe_funcs;
1320 call->class->define_fields = uprobe_event_define_fields;
1322 call->flags = TRACE_EVENT_FL_UPROBE;
1323 call->class->reg = trace_uprobe_register;
1327 static int register_uprobe_event(struct trace_uprobe *tu)
1329 struct trace_event_call *call = &tu->tp.call;
1332 init_trace_event_call(tu, call);
1334 if (traceprobe_set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0)
1337 ret = register_trace_event(&call->event);
1339 kfree(call->print_fmt);
1343 ret = trace_add_event_call(call);
1346 pr_info("Failed to register uprobe event: %s\n",
1347 trace_event_name(call));
1348 kfree(call->print_fmt);
1349 unregister_trace_event(&call->event);
1355 static int unregister_uprobe_event(struct trace_uprobe *tu)
1359 /* tu->event is unregistered in trace_remove_event_call() */
1360 ret = trace_remove_event_call(&tu->tp.call);
1363 kfree(tu->tp.call.print_fmt);
1364 tu->tp.call.print_fmt = NULL;
1368 #ifdef CONFIG_PERF_EVENTS
1369 struct trace_event_call *
1370 create_local_trace_uprobe(char *name, unsigned long offs,
1371 unsigned long ref_ctr_offset, bool is_return)
1373 struct trace_uprobe *tu;
1377 ret = kern_path(name, LOOKUP_FOLLOW, &path);
1379 return ERR_PTR(ret);
1381 if (!d_is_reg(path.dentry)) {
1383 return ERR_PTR(-EINVAL);
1387 * local trace_kprobes are not added to probe_list, so they are never
1388 * searched in find_trace_kprobe(). Therefore, there is no concern of
1389 * duplicated name "DUMMY_EVENT" here.
1391 tu = alloc_trace_uprobe(UPROBE_EVENT_SYSTEM, "DUMMY_EVENT", 0,
1395 pr_info("Failed to allocate trace_uprobe.(%d)\n",
1398 return ERR_CAST(tu);
1403 tu->ref_ctr_offset = ref_ctr_offset;
1404 tu->filename = kstrdup(name, GFP_KERNEL);
1405 init_trace_event_call(tu, &tu->tp.call);
1407 if (traceprobe_set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0) {
1412 return &tu->tp.call;
1414 free_trace_uprobe(tu);
1415 return ERR_PTR(ret);
1418 void destroy_local_trace_uprobe(struct trace_event_call *event_call)
1420 struct trace_uprobe *tu;
1422 tu = container_of(event_call, struct trace_uprobe, tp.call);
1424 kfree(tu->tp.call.print_fmt);
1425 tu->tp.call.print_fmt = NULL;
1427 free_trace_uprobe(tu);
1429 #endif /* CONFIG_PERF_EVENTS */
1431 /* Make a trace interface for controling probe points */
1432 static __init int init_uprobe_trace(void)
1434 struct dentry *d_tracer;
1436 d_tracer = tracing_init_dentry();
1437 if (IS_ERR(d_tracer))
1440 trace_create_file("uprobe_events", 0644, d_tracer,
1441 NULL, &uprobe_events_ops);
1442 /* Profile interface */
1443 trace_create_file("uprobe_profile", 0444, d_tracer,
1444 NULL, &uprobe_profile_ops);
1448 fs_initcall(init_uprobe_trace);