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/bpf-cgroup.h>
11 #include <linux/cleanup.h>
12 #include <linux/ctype.h>
13 #include <linux/filter.h>
14 #include <linux/module.h>
15 #include <linux/namei.h>
16 #include <linux/percpu.h>
17 #include <linux/rculist.h>
18 #include <linux/security.h>
19 #include <linux/string.h>
20 #include <linux/uaccess.h>
21 #include <linux/uprobes.h>
24 #include "trace_dynevent.h"
25 #include "trace_probe.h"
26 #include "trace_probe_tmpl.h"
28 #define UPROBE_EVENT_SYSTEM "uprobes"
30 struct uprobe_trace_entry_head {
31 struct trace_entry ent;
32 unsigned long vaddr[];
35 #define SIZEOF_TRACE_ENTRY(is_return) \
36 (sizeof(struct uprobe_trace_entry_head) + \
37 sizeof(unsigned long) * (is_return ? 2 : 1))
39 #define DATAOF_TRACE_ENTRY(entry, is_return) \
40 ((void*)(entry) + SIZEOF_TRACE_ENTRY(is_return))
42 static int trace_uprobe_create(const char *raw_command);
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 int argc, const char **argv, 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 uprobe_consumer consumer;
65 struct uprobe *uprobe;
67 unsigned long ref_ctr_offset;
68 unsigned long __percpu *nhits;
69 struct trace_probe tp;
72 static bool is_trace_uprobe(struct dyn_event *ev)
74 return ev->ops == &trace_uprobe_ops;
77 static struct trace_uprobe *to_trace_uprobe(struct dyn_event *ev)
79 return container_of(ev, struct trace_uprobe, devent);
83 * for_each_trace_uprobe - iterate over the trace_uprobe list
84 * @pos: the struct trace_uprobe * for each entry
85 * @dpos: the struct dyn_event * to use as a loop cursor
87 #define for_each_trace_uprobe(pos, dpos) \
88 for_each_dyn_event(dpos) \
89 if (is_trace_uprobe(dpos) && (pos = to_trace_uprobe(dpos)))
91 static int register_uprobe_event(struct trace_uprobe *tu);
92 static int unregister_uprobe_event(struct trace_uprobe *tu);
94 static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs,
96 static int uretprobe_dispatcher(struct uprobe_consumer *con,
97 unsigned long func, struct pt_regs *regs,
100 #ifdef CONFIG_STACK_GROWSUP
101 static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n)
103 return addr - (n * sizeof(long));
106 static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n)
108 return addr + (n * sizeof(long));
112 static unsigned long get_user_stack_nth(struct pt_regs *regs, unsigned int n)
115 unsigned long addr = user_stack_pointer(regs);
117 addr = adjust_stack_addr(addr, n);
119 if (copy_from_user(&ret, (void __force __user *) addr, sizeof(ret)))
126 * Uprobes-specific fetch functions
128 static nokprobe_inline int
129 probe_mem_read(void *dest, void *src, size_t size)
131 void __user *vaddr = (void __force __user *)src;
133 return copy_from_user(dest, vaddr, size) ? -EFAULT : 0;
136 static nokprobe_inline int
137 probe_mem_read_user(void *dest, void *src, size_t size)
139 return probe_mem_read(dest, src, size);
143 * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
144 * length and relative data location.
146 static nokprobe_inline int
147 fetch_store_string(unsigned long addr, void *dest, void *base)
150 u32 loc = *(u32 *)dest;
151 int maxlen = get_loc_len(loc);
152 u8 *dst = get_loc_data(dest, base);
153 void __user *src = (void __force __user *) addr;
155 if (unlikely(!maxlen))
158 if (addr == FETCH_TOKEN_COMM)
159 ret = strscpy(dst, current->comm, maxlen);
161 ret = strncpy_from_user(dst, src, maxlen);
167 * Include the terminating null byte. In this case it
168 * was copied by strncpy_from_user but not accounted
172 *(u32 *)dest = make_data_loc(ret, (void *)dst - base);
174 *(u32 *)dest = make_data_loc(0, (void *)dst - base);
179 static nokprobe_inline int
180 fetch_store_string_user(unsigned long addr, void *dest, void *base)
182 return fetch_store_string(addr, dest, base);
185 /* Return the length of string -- including null terminal byte */
186 static nokprobe_inline int
187 fetch_store_strlen(unsigned long addr)
190 void __user *vaddr = (void __force __user *) addr;
192 if (addr == FETCH_TOKEN_COMM)
193 len = strlen(current->comm) + 1;
195 len = strnlen_user(vaddr, MAX_STRING_SIZE);
197 return (len > MAX_STRING_SIZE) ? 0 : len;
200 static nokprobe_inline int
201 fetch_store_strlen_user(unsigned long addr)
203 return fetch_store_strlen(addr);
206 static unsigned long translate_user_vaddr(unsigned long file_offset)
208 unsigned long base_addr;
209 struct uprobe_dispatch_data *udd;
211 udd = (void *) current->utask->vaddr;
213 base_addr = udd->bp_addr - udd->tu->offset;
214 return base_addr + file_offset;
217 /* Note that we don't verify it, since the code does not come from user space */
219 process_fetch_insn(struct fetch_insn *code, void *rec, void *edata,
220 void *dest, void *base)
222 struct pt_regs *regs = rec;
226 /* 1st stage: get value from context */
229 val = regs_get_register(regs, code->param);
232 val = get_user_stack_nth(regs, code->param);
234 case FETCH_OP_STACKP:
235 val = user_stack_pointer(regs);
237 case FETCH_OP_RETVAL:
238 val = regs_return_value(regs);
241 val = FETCH_TOKEN_COMM;
244 val = translate_user_vaddr(code->immediate);
247 ret = process_common_fetch_insn(code, &val);
253 return process_fetch_insn_bottom(code, val, dest, base);
255 NOKPROBE_SYMBOL(process_fetch_insn)
257 static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter)
259 rwlock_init(&filter->rwlock);
260 filter->nr_systemwide = 0;
261 INIT_LIST_HEAD(&filter->perf_events);
264 static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter *filter)
266 return !filter->nr_systemwide && list_empty(&filter->perf_events);
269 static inline bool is_ret_probe(struct trace_uprobe *tu)
271 return tu->consumer.ret_handler != NULL;
274 static bool trace_uprobe_is_busy(struct dyn_event *ev)
276 struct trace_uprobe *tu = to_trace_uprobe(ev);
278 return trace_probe_is_enabled(&tu->tp);
281 static bool trace_uprobe_match_command_head(struct trace_uprobe *tu,
282 int argc, const char **argv)
284 char buf[MAX_ARGSTR_LEN + 1];
290 len = strlen(tu->filename);
291 if (strncmp(tu->filename, argv[0], len) || argv[0][len] != ':')
294 if (tu->ref_ctr_offset == 0)
295 snprintf(buf, sizeof(buf), "0x%0*lx",
296 (int)(sizeof(void *) * 2), tu->offset);
298 snprintf(buf, sizeof(buf), "0x%0*lx(0x%lx)",
299 (int)(sizeof(void *) * 2), tu->offset,
301 if (strcmp(buf, &argv[0][len + 1]))
306 return trace_probe_match_command_args(&tu->tp, argc, argv);
309 static bool trace_uprobe_match(const char *system, const char *event,
310 int argc, const char **argv, struct dyn_event *ev)
312 struct trace_uprobe *tu = to_trace_uprobe(ev);
314 return (event[0] == '\0' ||
315 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 tu->nhits = alloc_percpu(unsigned long);
351 ret = trace_probe_init(&tu->tp, event, group, true, nargs);
355 dyn_event_init(&tu->devent, &trace_uprobe_ops);
356 tu->consumer.handler = uprobe_dispatcher;
358 tu->consumer.ret_handler = uretprobe_dispatcher;
359 init_trace_uprobe_filter(tu->tp.event->filter);
363 free_percpu(tu->nhits);
369 static void free_trace_uprobe(struct trace_uprobe *tu)
375 trace_probe_cleanup(&tu->tp);
377 free_percpu(tu->nhits);
381 static struct trace_uprobe *find_probe_event(const char *event, const char *group)
383 struct dyn_event *pos;
384 struct trace_uprobe *tu;
386 for_each_trace_uprobe(tu, pos)
387 if (strcmp(trace_probe_name(&tu->tp), event) == 0 &&
388 strcmp(trace_probe_group_name(&tu->tp), group) == 0)
394 /* Unregister a trace_uprobe and probe_event */
395 static int unregister_trace_uprobe(struct trace_uprobe *tu)
399 if (trace_probe_has_sibling(&tu->tp))
402 /* If there's a reference to the dynamic event */
403 if (trace_event_dyn_busy(trace_probe_event_call(&tu->tp)))
406 ret = unregister_uprobe_event(tu);
411 dyn_event_remove(&tu->devent);
412 trace_probe_unlink(&tu->tp);
413 free_trace_uprobe(tu);
417 static bool trace_uprobe_has_same_uprobe(struct trace_uprobe *orig,
418 struct trace_uprobe *comp)
420 struct trace_probe_event *tpe = orig->tp.event;
421 struct inode *comp_inode = d_real_inode(comp->path.dentry);
424 list_for_each_entry(orig, &tpe->probes, tp.list) {
425 if (comp_inode != d_real_inode(orig->path.dentry) ||
426 comp->offset != orig->offset)
430 * trace_probe_compare_arg_type() ensured that nr_args and
431 * each argument name and type are same. Let's compare comm.
433 for (i = 0; i < orig->tp.nr_args; i++) {
434 if (strcmp(orig->tp.args[i].comm,
435 comp->tp.args[i].comm))
439 if (i == orig->tp.nr_args)
446 static int append_trace_uprobe(struct trace_uprobe *tu, struct trace_uprobe *to)
450 ret = trace_probe_compare_arg_type(&tu->tp, &to->tp);
452 /* Note that argument starts index = 2 */
453 trace_probe_log_set_index(ret + 1);
454 trace_probe_log_err(0, DIFF_ARG_TYPE);
457 if (trace_uprobe_has_same_uprobe(to, tu)) {
458 trace_probe_log_set_index(0);
459 trace_probe_log_err(0, SAME_PROBE);
463 /* Append to existing event */
464 ret = trace_probe_append(&tu->tp, &to->tp);
466 dyn_event_add(&tu->devent, trace_probe_event_call(&tu->tp));
472 * Uprobe with multiple reference counter is not allowed. i.e.
473 * If inode and offset matches, reference counter offset *must*
474 * match as well. Though, there is one exception: If user is
475 * replacing old trace_uprobe with new one(same group/event),
476 * then we allow same uprobe with new reference counter as far
477 * as the new one does not conflict with any other existing
480 static int validate_ref_ctr_offset(struct trace_uprobe *new)
482 struct dyn_event *pos;
483 struct trace_uprobe *tmp;
484 struct inode *new_inode = d_real_inode(new->path.dentry);
486 for_each_trace_uprobe(tmp, pos) {
487 if (new_inode == d_real_inode(tmp->path.dentry) &&
488 new->offset == tmp->offset &&
489 new->ref_ctr_offset != tmp->ref_ctr_offset) {
490 pr_warn("Reference counter offset mismatch.");
497 /* Register a trace_uprobe and probe_event */
498 static int register_trace_uprobe(struct trace_uprobe *tu)
500 struct trace_uprobe *old_tu;
503 guard(mutex)(&event_mutex);
505 ret = validate_ref_ctr_offset(tu);
509 /* register as an event */
510 old_tu = find_probe_event(trace_probe_name(&tu->tp),
511 trace_probe_group_name(&tu->tp));
513 if (is_ret_probe(tu) != is_ret_probe(old_tu)) {
514 trace_probe_log_set_index(0);
515 trace_probe_log_err(0, DIFF_PROBE_TYPE);
518 return append_trace_uprobe(tu, old_tu);
521 ret = register_uprobe_event(tu);
523 if (ret == -EEXIST) {
524 trace_probe_log_set_index(0);
525 trace_probe_log_err(0, EVENT_EXIST);
527 pr_warn("Failed to register probe event(%d)\n", ret);
531 dyn_event_add(&tu->devent, trace_probe_event_call(&tu->tp));
538 * - Add uprobe: p|r[:[GRP/][EVENT]] PATH:OFFSET[%return][(REF)] [FETCHARGS]
540 static int __trace_uprobe_create(int argc, const char **argv)
542 const char *event = NULL, *group = UPROBE_EVENT_SYSTEM;
543 char *arg, *filename, *rctr, *rctr_end, *tmp;
544 unsigned long offset, ref_ctr_offset;
545 char *gbuf __free(kfree) = NULL;
546 char *buf __free(kfree) = NULL;
547 enum probe_print_type ptype;
548 struct trace_uprobe *tu;
549 bool is_return = false;
555 switch (argv[0][0]) {
568 trace_probe_log_init("trace_uprobe", argc, argv);
570 if (argc - 2 > MAX_TRACE_ARGS) {
571 trace_probe_log_set_index(2);
572 trace_probe_log_err(0, TOO_MANY_ARGS);
576 if (argv[0][1] == ':')
579 if (!strchr(argv[1], '/'))
582 filename = kstrdup(argv[1], GFP_KERNEL);
586 /* Find the last occurrence, in case the path contains ':' too. */
587 arg = strrchr(filename, ':');
588 if (!arg || !isdigit(arg[1])) {
593 trace_probe_log_set_index(1); /* filename is the 2nd argument */
596 ret = kern_path(filename, LOOKUP_FOLLOW, &path);
598 trace_probe_log_err(0, FILE_NOT_FOUND);
600 trace_probe_log_clear();
603 if (!d_is_reg(path.dentry)) {
604 trace_probe_log_err(0, NO_REGULAR_FILE);
606 goto fail_address_parse;
609 /* Parse reference counter offset if specified. */
610 rctr = strchr(arg, '(');
612 rctr_end = strchr(rctr, ')');
615 rctr_end = rctr + strlen(rctr);
616 trace_probe_log_err(rctr_end - filename,
618 goto fail_address_parse;
619 } else if (rctr_end[1] != '\0') {
621 trace_probe_log_err(rctr_end + 1 - filename,
623 goto fail_address_parse;
628 ret = kstrtoul(rctr, 0, &ref_ctr_offset);
630 trace_probe_log_err(rctr - filename, BAD_REFCNT);
631 goto fail_address_parse;
635 /* Check if there is %return suffix */
636 tmp = strchr(arg, '%');
638 if (!strcmp(tmp, "%return")) {
642 trace_probe_log_err(tmp - filename, BAD_ADDR_SUFFIX);
644 goto fail_address_parse;
648 /* Parse uprobe offset. */
649 ret = kstrtoul(arg, 0, &offset);
651 trace_probe_log_err(arg - filename, BAD_UPROBE_OFFS);
652 goto fail_address_parse;
656 trace_probe_log_set_index(0);
658 gbuf = kmalloc(MAX_EVENT_NAME_LEN, GFP_KERNEL);
662 ret = traceprobe_parse_event_name(&event, &group, gbuf,
665 goto fail_address_parse;
672 tail = kstrdup(kbasename(filename), GFP_KERNEL);
676 ptr = strpbrk(tail, ".-_");
680 buf = kmalloc(MAX_EVENT_NAME_LEN, GFP_KERNEL);
683 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset);
691 tu = alloc_trace_uprobe(group, event, argc, is_return);
694 /* This must return -ENOMEM otherwise there is a bug */
695 WARN_ON_ONCE(ret != -ENOMEM);
696 goto fail_address_parse;
699 tu->ref_ctr_offset = ref_ctr_offset;
701 tu->filename = filename;
703 /* parse arguments */
704 for (i = 0; i < argc; i++) {
705 struct traceprobe_parse_context *ctx __free(traceprobe_parse_context)
706 = kzalloc(sizeof(*ctx), GFP_KERNEL);
712 ctx->flags = (is_return ? TPARG_FL_RETURN : 0) | TPARG_FL_USER;
713 trace_probe_log_set_index(i + 2);
714 ret = traceprobe_parse_probe_arg(&tu->tp, i, argv[i], ctx);
719 ptype = is_ret_probe(tu) ? PROBE_PRINT_RETURN : PROBE_PRINT_NORMAL;
720 ret = traceprobe_set_print_fmt(&tu->tp, ptype);
724 ret = register_trace_uprobe(tu);
729 free_trace_uprobe(tu);
731 trace_probe_log_clear();
738 trace_probe_log_clear();
745 int trace_uprobe_create(const char *raw_command)
747 return trace_probe_create(raw_command, __trace_uprobe_create);
750 static int create_or_delete_trace_uprobe(const char *raw_command)
754 if (raw_command[0] == '-')
755 return dyn_event_release(raw_command, &trace_uprobe_ops);
757 ret = dyn_event_create(raw_command, &trace_uprobe_ops);
758 return ret == -ECANCELED ? -EINVAL : ret;
761 static int trace_uprobe_release(struct dyn_event *ev)
763 struct trace_uprobe *tu = to_trace_uprobe(ev);
765 return unregister_trace_uprobe(tu);
768 /* Probes listing interfaces */
769 static int trace_uprobe_show(struct seq_file *m, struct dyn_event *ev)
771 struct trace_uprobe *tu = to_trace_uprobe(ev);
772 char c = is_ret_probe(tu) ? 'r' : 'p';
775 seq_printf(m, "%c:%s/%s %s:0x%0*lx", c, trace_probe_group_name(&tu->tp),
776 trace_probe_name(&tu->tp), tu->filename,
777 (int)(sizeof(void *) * 2), tu->offset);
779 if (tu->ref_ctr_offset)
780 seq_printf(m, "(0x%lx)", tu->ref_ctr_offset);
782 for (i = 0; i < tu->tp.nr_args; i++)
783 seq_printf(m, " %s=%s", tu->tp.args[i].name, tu->tp.args[i].comm);
789 static int probes_seq_show(struct seq_file *m, void *v)
791 struct dyn_event *ev = v;
793 if (!is_trace_uprobe(ev))
796 return trace_uprobe_show(m, ev);
799 static const struct seq_operations probes_seq_op = {
800 .start = dyn_event_seq_start,
801 .next = dyn_event_seq_next,
802 .stop = dyn_event_seq_stop,
803 .show = probes_seq_show
806 static int probes_open(struct inode *inode, struct file *file)
810 ret = security_locked_down(LOCKDOWN_TRACEFS);
814 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
815 ret = dyn_events_release_all(&trace_uprobe_ops);
820 return seq_open(file, &probes_seq_op);
823 static ssize_t probes_write(struct file *file, const char __user *buffer,
824 size_t count, loff_t *ppos)
826 return trace_parse_run_command(file, buffer, count, ppos,
827 create_or_delete_trace_uprobe);
830 static const struct file_operations uprobe_events_ops = {
831 .owner = THIS_MODULE,
835 .release = seq_release,
836 .write = probes_write,
839 /* Probes profiling interfaces */
840 static int probes_profile_seq_show(struct seq_file *m, void *v)
842 struct dyn_event *ev = v;
843 struct trace_uprobe *tu;
847 if (!is_trace_uprobe(ev))
850 tu = to_trace_uprobe(ev);
853 for_each_possible_cpu(cpu) {
854 nhits += per_cpu(*tu->nhits, cpu);
857 seq_printf(m, " %s %-44s %15lu\n", tu->filename,
858 trace_probe_name(&tu->tp), nhits);
862 static const struct seq_operations profile_seq_op = {
863 .start = dyn_event_seq_start,
864 .next = dyn_event_seq_next,
865 .stop = dyn_event_seq_stop,
866 .show = probes_profile_seq_show
869 static int profile_open(struct inode *inode, struct file *file)
873 ret = security_locked_down(LOCKDOWN_TRACEFS);
877 return seq_open(file, &profile_seq_op);
880 static const struct file_operations uprobe_profile_ops = {
881 .owner = THIS_MODULE,
882 .open = profile_open,
885 .release = seq_release,
888 struct uprobe_cpu_buffer {
893 static struct uprobe_cpu_buffer __percpu *uprobe_cpu_buffer;
894 static int uprobe_buffer_refcnt;
895 #define MAX_UCB_BUFFER_SIZE PAGE_SIZE
897 static int uprobe_buffer_init(void)
901 uprobe_cpu_buffer = alloc_percpu(struct uprobe_cpu_buffer);
902 if (uprobe_cpu_buffer == NULL)
905 for_each_possible_cpu(cpu) {
906 struct page *p = alloc_pages_node(cpu_to_node(cpu),
912 per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf = page_address(p);
913 mutex_init(&per_cpu_ptr(uprobe_cpu_buffer, cpu)->mutex);
919 for_each_possible_cpu(cpu) {
922 free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf);
925 free_percpu(uprobe_cpu_buffer);
929 static int uprobe_buffer_enable(void)
933 BUG_ON(!mutex_is_locked(&event_mutex));
935 if (uprobe_buffer_refcnt++ == 0) {
936 ret = uprobe_buffer_init();
938 uprobe_buffer_refcnt--;
944 static void uprobe_buffer_disable(void)
948 BUG_ON(!mutex_is_locked(&event_mutex));
950 if (--uprobe_buffer_refcnt == 0) {
951 for_each_possible_cpu(cpu)
952 free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer,
955 free_percpu(uprobe_cpu_buffer);
956 uprobe_cpu_buffer = NULL;
960 static struct uprobe_cpu_buffer *uprobe_buffer_get(void)
962 struct uprobe_cpu_buffer *ucb;
965 cpu = raw_smp_processor_id();
966 ucb = per_cpu_ptr(uprobe_cpu_buffer, cpu);
969 * Use per-cpu buffers for fastest access, but we might migrate
970 * so the mutex makes sure we have sole access to it.
972 mutex_lock(&ucb->mutex);
977 static void uprobe_buffer_put(struct uprobe_cpu_buffer *ucb)
981 mutex_unlock(&ucb->mutex);
984 static struct uprobe_cpu_buffer *prepare_uprobe_buffer(struct trace_uprobe *tu,
985 struct pt_regs *regs,
986 struct uprobe_cpu_buffer **ucbp)
988 struct uprobe_cpu_buffer *ucb;
994 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
995 dsize = __get_data_size(&tu->tp, regs, NULL);
997 ucb = uprobe_buffer_get();
998 ucb->dsize = tu->tp.size + dsize;
1000 if (WARN_ON_ONCE(ucb->dsize > MAX_UCB_BUFFER_SIZE)) {
1001 ucb->dsize = MAX_UCB_BUFFER_SIZE;
1002 dsize = MAX_UCB_BUFFER_SIZE - tu->tp.size;
1005 store_trace_args(ucb->buf, &tu->tp, regs, NULL, esize, dsize);
1011 static void __uprobe_trace_func(struct trace_uprobe *tu,
1012 unsigned long func, struct pt_regs *regs,
1013 struct uprobe_cpu_buffer *ucb,
1014 struct trace_event_file *trace_file)
1016 struct uprobe_trace_entry_head *entry;
1017 struct trace_event_buffer fbuffer;
1020 struct trace_event_call *call = trace_probe_event_call(&tu->tp);
1022 WARN_ON(call != trace_file->event_call);
1024 if (trace_trigger_soft_disabled(trace_file))
1027 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1028 size = esize + ucb->dsize;
1029 entry = trace_event_buffer_reserve(&fbuffer, trace_file, size);
1033 if (is_ret_probe(tu)) {
1034 entry->vaddr[0] = func;
1035 entry->vaddr[1] = instruction_pointer(regs);
1036 data = DATAOF_TRACE_ENTRY(entry, true);
1038 entry->vaddr[0] = instruction_pointer(regs);
1039 data = DATAOF_TRACE_ENTRY(entry, false);
1042 memcpy(data, ucb->buf, ucb->dsize);
1044 trace_event_buffer_commit(&fbuffer);
1047 /* uprobe handler */
1048 static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs,
1049 struct uprobe_cpu_buffer **ucbp)
1051 struct event_file_link *link;
1052 struct uprobe_cpu_buffer *ucb;
1054 if (is_ret_probe(tu))
1057 ucb = prepare_uprobe_buffer(tu, regs, ucbp);
1060 trace_probe_for_each_link_rcu(link, &tu->tp)
1061 __uprobe_trace_func(tu, 0, regs, ucb, link->file);
1067 static void uretprobe_trace_func(struct trace_uprobe *tu, unsigned long func,
1068 struct pt_regs *regs,
1069 struct uprobe_cpu_buffer **ucbp)
1071 struct event_file_link *link;
1072 struct uprobe_cpu_buffer *ucb;
1074 ucb = prepare_uprobe_buffer(tu, regs, ucbp);
1077 trace_probe_for_each_link_rcu(link, &tu->tp)
1078 __uprobe_trace_func(tu, func, regs, ucb, link->file);
1082 /* Event entry printers */
1083 static enum print_line_t
1084 print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event)
1086 struct uprobe_trace_entry_head *entry;
1087 struct trace_seq *s = &iter->seq;
1088 struct trace_uprobe *tu;
1091 entry = (struct uprobe_trace_entry_head *)iter->ent;
1092 tu = trace_uprobe_primary_from_call(
1093 container_of(event, struct trace_event_call, event));
1097 if (is_ret_probe(tu)) {
1098 trace_seq_printf(s, "%s: (0x%lx <- 0x%lx)",
1099 trace_probe_name(&tu->tp),
1100 entry->vaddr[1], entry->vaddr[0]);
1101 data = DATAOF_TRACE_ENTRY(entry, true);
1103 trace_seq_printf(s, "%s: (0x%lx)",
1104 trace_probe_name(&tu->tp),
1106 data = DATAOF_TRACE_ENTRY(entry, false);
1109 if (trace_probe_print_args(s, tu->tp.args, tu->tp.nr_args, data, entry) < 0)
1112 trace_seq_putc(s, '\n');
1115 return trace_handle_return(s);
1118 typedef bool (*filter_func_t)(struct uprobe_consumer *self, struct mm_struct *mm);
1120 static int trace_uprobe_enable(struct trace_uprobe *tu, filter_func_t filter)
1122 struct inode *inode = d_real_inode(tu->path.dentry);
1123 struct uprobe *uprobe;
1125 tu->consumer.filter = filter;
1126 uprobe = uprobe_register(inode, tu->offset, tu->ref_ctr_offset, &tu->consumer);
1128 return PTR_ERR(uprobe);
1130 tu->uprobe = uprobe;
1134 static void __probe_event_disable(struct trace_probe *tp)
1136 struct trace_uprobe *tu;
1139 tu = container_of(tp, struct trace_uprobe, tp);
1140 WARN_ON(!uprobe_filter_is_empty(tu->tp.event->filter));
1142 list_for_each_entry(tu, trace_probe_probe_list(tp), tp.list) {
1146 uprobe_unregister_nosync(tu->uprobe, &tu->consumer);
1151 uprobe_unregister_sync();
1154 static int probe_event_enable(struct trace_event_call *call,
1155 struct trace_event_file *file, filter_func_t filter)
1157 struct trace_probe *tp;
1158 struct trace_uprobe *tu;
1162 tp = trace_probe_primary_from_call(call);
1163 if (WARN_ON_ONCE(!tp))
1165 enabled = trace_probe_is_enabled(tp);
1167 /* This may also change "enabled" state */
1169 if (trace_probe_test_flag(tp, TP_FLAG_PROFILE))
1172 ret = trace_probe_add_file(tp, file);
1176 if (trace_probe_test_flag(tp, TP_FLAG_TRACE))
1179 trace_probe_set_flag(tp, TP_FLAG_PROFILE);
1182 tu = container_of(tp, struct trace_uprobe, tp);
1183 WARN_ON(!uprobe_filter_is_empty(tu->tp.event->filter));
1188 ret = uprobe_buffer_enable();
1192 list_for_each_entry(tu, trace_probe_probe_list(tp), tp.list) {
1193 ret = trace_uprobe_enable(tu, filter);
1195 __probe_event_disable(tp);
1203 uprobe_buffer_disable();
1207 trace_probe_remove_file(tp, file);
1209 trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
1214 static void probe_event_disable(struct trace_event_call *call,
1215 struct trace_event_file *file)
1217 struct trace_probe *tp;
1219 tp = trace_probe_primary_from_call(call);
1220 if (WARN_ON_ONCE(!tp))
1223 if (!trace_probe_is_enabled(tp))
1227 if (trace_probe_remove_file(tp, file) < 0)
1230 if (trace_probe_is_enabled(tp))
1233 trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
1235 __probe_event_disable(tp);
1236 uprobe_buffer_disable();
1239 static int uprobe_event_define_fields(struct trace_event_call *event_call)
1242 struct uprobe_trace_entry_head field;
1243 struct trace_uprobe *tu;
1245 tu = trace_uprobe_primary_from_call(event_call);
1249 if (is_ret_probe(tu)) {
1250 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_FUNC, 0);
1251 DEFINE_FIELD(unsigned long, vaddr[1], FIELD_STRING_RETIP, 0);
1252 size = SIZEOF_TRACE_ENTRY(true);
1254 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0);
1255 size = SIZEOF_TRACE_ENTRY(false);
1258 return traceprobe_define_arg_fields(event_call, size, &tu->tp);
1261 #ifdef CONFIG_PERF_EVENTS
1263 __uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm)
1265 struct perf_event *event;
1267 list_for_each_entry(event, &filter->perf_events, hw.tp_list) {
1268 if (event->hw.target->mm == mm)
1276 trace_uprobe_filter_event(struct trace_uprobe_filter *filter,
1277 struct perf_event *event)
1279 return __uprobe_perf_filter(filter, event->hw.target->mm);
1282 static bool trace_uprobe_filter_remove(struct trace_uprobe_filter *filter,
1283 struct perf_event *event)
1287 write_lock(&filter->rwlock);
1288 if (event->hw.target) {
1289 list_del(&event->hw.tp_list);
1290 done = filter->nr_systemwide ||
1291 (event->hw.target->flags & PF_EXITING) ||
1292 trace_uprobe_filter_event(filter, event);
1294 filter->nr_systemwide--;
1295 done = filter->nr_systemwide;
1297 write_unlock(&filter->rwlock);
1302 /* This returns true if the filter always covers target mm */
1303 static bool trace_uprobe_filter_add(struct trace_uprobe_filter *filter,
1304 struct perf_event *event)
1308 write_lock(&filter->rwlock);
1309 if (event->hw.target) {
1311 * event->parent != NULL means copy_process(), we can avoid
1312 * uprobe_apply(). current->mm must be probed and we can rely
1313 * on dup_mmap() which preserves the already installed bp's.
1315 * attr.enable_on_exec means that exec/mmap will install the
1316 * breakpoints we need.
1318 done = filter->nr_systemwide ||
1319 event->parent || event->attr.enable_on_exec ||
1320 trace_uprobe_filter_event(filter, event);
1321 list_add(&event->hw.tp_list, &filter->perf_events);
1323 done = filter->nr_systemwide;
1324 filter->nr_systemwide++;
1326 write_unlock(&filter->rwlock);
1331 static int uprobe_perf_close(struct trace_event_call *call,
1332 struct perf_event *event)
1334 struct trace_probe *tp;
1335 struct trace_uprobe *tu;
1338 tp = trace_probe_primary_from_call(call);
1339 if (WARN_ON_ONCE(!tp))
1342 tu = container_of(tp, struct trace_uprobe, tp);
1343 if (trace_uprobe_filter_remove(tu->tp.event->filter, event))
1346 list_for_each_entry(tu, trace_probe_probe_list(tp), tp.list) {
1347 ret = uprobe_apply(tu->uprobe, &tu->consumer, false);
1355 static int uprobe_perf_open(struct trace_event_call *call,
1356 struct perf_event *event)
1358 struct trace_probe *tp;
1359 struct trace_uprobe *tu;
1362 tp = trace_probe_primary_from_call(call);
1363 if (WARN_ON_ONCE(!tp))
1366 tu = container_of(tp, struct trace_uprobe, tp);
1367 if (trace_uprobe_filter_add(tu->tp.event->filter, event))
1370 list_for_each_entry(tu, trace_probe_probe_list(tp), tp.list) {
1371 err = uprobe_apply(tu->uprobe, &tu->consumer, true);
1373 uprobe_perf_close(call, event);
1381 static bool uprobe_perf_filter(struct uprobe_consumer *uc, struct mm_struct *mm)
1383 struct trace_uprobe_filter *filter;
1384 struct trace_uprobe *tu;
1387 tu = container_of(uc, struct trace_uprobe, consumer);
1388 filter = tu->tp.event->filter;
1391 * speculative short-circuiting check to avoid unnecessarily taking
1392 * filter->rwlock below, if the uprobe has system-wide consumer
1394 if (READ_ONCE(filter->nr_systemwide))
1397 read_lock(&filter->rwlock);
1398 ret = __uprobe_perf_filter(filter, mm);
1399 read_unlock(&filter->rwlock);
1404 static void __uprobe_perf_func(struct trace_uprobe *tu,
1405 unsigned long func, struct pt_regs *regs,
1406 struct uprobe_cpu_buffer **ucbp)
1408 struct trace_event_call *call = trace_probe_event_call(&tu->tp);
1409 struct uprobe_trace_entry_head *entry;
1410 struct uprobe_cpu_buffer *ucb;
1411 struct hlist_head *head;
1416 #ifdef CONFIG_BPF_EVENTS
1417 if (bpf_prog_array_valid(call)) {
1418 const struct bpf_prog_array *array;
1421 rcu_read_lock_trace();
1422 array = rcu_dereference_check(call->prog_array, rcu_read_lock_trace_held());
1423 ret = bpf_prog_run_array_uprobe(array, regs, bpf_prog_run);
1424 rcu_read_unlock_trace();
1428 #endif /* CONFIG_BPF_EVENTS */
1430 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1432 ucb = prepare_uprobe_buffer(tu, regs, ucbp);
1433 size = esize + ucb->dsize;
1434 size = ALIGN(size + sizeof(u32), sizeof(u64)) - sizeof(u32);
1435 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough"))
1439 head = this_cpu_ptr(call->perf_events);
1440 if (hlist_empty(head))
1443 entry = perf_trace_buf_alloc(size, NULL, &rctx);
1447 if (is_ret_probe(tu)) {
1448 entry->vaddr[0] = func;
1449 entry->vaddr[1] = instruction_pointer(regs);
1450 data = DATAOF_TRACE_ENTRY(entry, true);
1452 entry->vaddr[0] = instruction_pointer(regs);
1453 data = DATAOF_TRACE_ENTRY(entry, false);
1456 memcpy(data, ucb->buf, ucb->dsize);
1458 if (size - esize > ucb->dsize)
1459 memset(data + ucb->dsize, 0, size - esize - ucb->dsize);
1461 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1467 /* uprobe profile handler */
1468 static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs,
1469 struct uprobe_cpu_buffer **ucbp)
1471 if (!uprobe_perf_filter(&tu->consumer, current->mm))
1472 return UPROBE_HANDLER_REMOVE;
1474 if (!is_ret_probe(tu))
1475 __uprobe_perf_func(tu, 0, regs, ucbp);
1479 static void uretprobe_perf_func(struct trace_uprobe *tu, unsigned long func,
1480 struct pt_regs *regs,
1481 struct uprobe_cpu_buffer **ucbp)
1483 __uprobe_perf_func(tu, func, regs, ucbp);
1486 int bpf_get_uprobe_info(const struct perf_event *event, u32 *fd_type,
1487 const char **filename, u64 *probe_offset,
1488 u64 *probe_addr, bool perf_type_tracepoint)
1490 const char *pevent = trace_event_name(event->tp_event);
1491 const char *group = event->tp_event->class->system;
1492 struct trace_uprobe *tu;
1494 if (perf_type_tracepoint)
1495 tu = find_probe_event(pevent, group);
1497 tu = trace_uprobe_primary_from_call(event->tp_event);
1501 *fd_type = is_ret_probe(tu) ? BPF_FD_TYPE_URETPROBE
1502 : BPF_FD_TYPE_UPROBE;
1503 *filename = tu->filename;
1504 *probe_offset = tu->offset;
1505 *probe_addr = tu->ref_ctr_offset;
1508 #endif /* CONFIG_PERF_EVENTS */
1511 trace_uprobe_register(struct trace_event_call *event, enum trace_reg type,
1514 struct trace_event_file *file = data;
1517 case TRACE_REG_REGISTER:
1518 return probe_event_enable(event, file, NULL);
1520 case TRACE_REG_UNREGISTER:
1521 probe_event_disable(event, file);
1524 #ifdef CONFIG_PERF_EVENTS
1525 case TRACE_REG_PERF_REGISTER:
1526 return probe_event_enable(event, NULL, uprobe_perf_filter);
1528 case TRACE_REG_PERF_UNREGISTER:
1529 probe_event_disable(event, NULL);
1532 case TRACE_REG_PERF_OPEN:
1533 return uprobe_perf_open(event, data);
1535 case TRACE_REG_PERF_CLOSE:
1536 return uprobe_perf_close(event, data);
1544 static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs,
1547 struct trace_uprobe *tu;
1548 struct uprobe_dispatch_data udd;
1549 struct uprobe_cpu_buffer *ucb = NULL;
1552 tu = container_of(con, struct trace_uprobe, consumer);
1554 this_cpu_inc(*tu->nhits);
1557 udd.bp_addr = instruction_pointer(regs);
1559 current->utask->vaddr = (unsigned long) &udd;
1561 if (WARN_ON_ONCE(!uprobe_cpu_buffer))
1564 if (trace_probe_test_flag(&tu->tp, TP_FLAG_TRACE))
1565 ret |= uprobe_trace_func(tu, regs, &ucb);
1567 #ifdef CONFIG_PERF_EVENTS
1568 if (trace_probe_test_flag(&tu->tp, TP_FLAG_PROFILE))
1569 ret |= uprobe_perf_func(tu, regs, &ucb);
1571 uprobe_buffer_put(ucb);
1575 static int uretprobe_dispatcher(struct uprobe_consumer *con,
1576 unsigned long func, struct pt_regs *regs,
1579 struct trace_uprobe *tu;
1580 struct uprobe_dispatch_data udd;
1581 struct uprobe_cpu_buffer *ucb = NULL;
1583 tu = container_of(con, struct trace_uprobe, consumer);
1588 current->utask->vaddr = (unsigned long) &udd;
1590 if (WARN_ON_ONCE(!uprobe_cpu_buffer))
1593 if (trace_probe_test_flag(&tu->tp, TP_FLAG_TRACE))
1594 uretprobe_trace_func(tu, func, regs, &ucb);
1596 #ifdef CONFIG_PERF_EVENTS
1597 if (trace_probe_test_flag(&tu->tp, TP_FLAG_PROFILE))
1598 uretprobe_perf_func(tu, func, regs, &ucb);
1600 uprobe_buffer_put(ucb);
1604 static struct trace_event_functions uprobe_funcs = {
1605 .trace = print_uprobe_event
1608 static struct trace_event_fields uprobe_fields_array[] = {
1609 { .type = TRACE_FUNCTION_TYPE,
1610 .define_fields = uprobe_event_define_fields },
1614 static inline void init_trace_event_call(struct trace_uprobe *tu)
1616 struct trace_event_call *call = trace_probe_event_call(&tu->tp);
1617 call->event.funcs = &uprobe_funcs;
1618 call->class->fields_array = uprobe_fields_array;
1620 call->flags = TRACE_EVENT_FL_UPROBE | TRACE_EVENT_FL_CAP_ANY;
1621 call->class->reg = trace_uprobe_register;
1624 static int register_uprobe_event(struct trace_uprobe *tu)
1626 init_trace_event_call(tu);
1628 return trace_probe_register_event_call(&tu->tp);
1631 static int unregister_uprobe_event(struct trace_uprobe *tu)
1633 return trace_probe_unregister_event_call(&tu->tp);
1636 #ifdef CONFIG_PERF_EVENTS
1637 struct trace_event_call *
1638 create_local_trace_uprobe(char *name, unsigned long offs,
1639 unsigned long ref_ctr_offset, bool is_return)
1641 enum probe_print_type ptype;
1642 struct trace_uprobe *tu;
1646 ret = kern_path(name, LOOKUP_FOLLOW, &path);
1648 return ERR_PTR(ret);
1650 if (!d_is_reg(path.dentry)) {
1652 return ERR_PTR(-EINVAL);
1656 * local trace_kprobes are not added to dyn_event, so they are never
1657 * searched in find_trace_kprobe(). Therefore, there is no concern of
1658 * duplicated name "DUMMY_EVENT" here.
1660 tu = alloc_trace_uprobe(UPROBE_EVENT_SYSTEM, "DUMMY_EVENT", 0,
1664 pr_info("Failed to allocate trace_uprobe.(%d)\n",
1667 return ERR_CAST(tu);
1672 tu->ref_ctr_offset = ref_ctr_offset;
1673 tu->filename = kstrdup(name, GFP_KERNEL);
1674 if (!tu->filename) {
1679 init_trace_event_call(tu);
1681 ptype = is_ret_probe(tu) ? PROBE_PRINT_RETURN : PROBE_PRINT_NORMAL;
1682 if (traceprobe_set_print_fmt(&tu->tp, ptype) < 0) {
1687 return trace_probe_event_call(&tu->tp);
1689 free_trace_uprobe(tu);
1690 return ERR_PTR(ret);
1693 void destroy_local_trace_uprobe(struct trace_event_call *event_call)
1695 struct trace_uprobe *tu;
1697 tu = trace_uprobe_primary_from_call(event_call);
1699 free_trace_uprobe(tu);
1701 #endif /* CONFIG_PERF_EVENTS */
1703 /* Make a trace interface for controlling probe points */
1704 static __init int init_uprobe_trace(void)
1708 ret = dyn_event_register(&trace_uprobe_ops);
1712 ret = tracing_init_dentry();
1716 trace_create_file("uprobe_events", TRACE_MODE_WRITE, NULL,
1717 NULL, &uprobe_events_ops);
1718 /* Profile interface */
1719 trace_create_file("uprobe_profile", TRACE_MODE_READ, NULL,
1720 NULL, &uprobe_profile_ops);
1724 fs_initcall(init_uprobe_trace);