mm, oom: remove oom_lock from oom_reaper
[linux/fpc-iii.git] / kernel / trace / trace_uprobe.c
blobe696667da29a49aa2f94372fa301b8ab1e7ec373
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * uprobes-based tracing events
5 * Copyright (C) IBM Corporation, 2010-2012
6 * Author: Srikar Dronamraju <srikar@linux.vnet.ibm.com>
7 */
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"
19 #define UPROBE_EVENT_SYSTEM "uprobes"
21 struct uprobe_trace_entry_head {
22 struct trace_entry ent;
23 unsigned long vaddr[];
26 #define SIZEOF_TRACE_ENTRY(is_return) \
27 (sizeof(struct uprobe_trace_entry_head) + \
28 sizeof(unsigned long) * (is_return ? 2 : 1))
30 #define DATAOF_TRACE_ENTRY(entry, is_return) \
31 ((void*)(entry) + SIZEOF_TRACE_ENTRY(is_return))
33 struct trace_uprobe_filter {
34 rwlock_t rwlock;
35 int nr_systemwide;
36 struct list_head perf_events;
40 * uprobe event core functions
42 struct trace_uprobe {
43 struct list_head list;
44 struct trace_uprobe_filter filter;
45 struct uprobe_consumer consumer;
46 struct path path;
47 struct inode *inode;
48 char *filename;
49 unsigned long offset;
50 unsigned long nhit;
51 struct trace_probe tp;
54 #define SIZEOF_TRACE_UPROBE(n) \
55 (offsetof(struct trace_uprobe, tp.args) + \
56 (sizeof(struct probe_arg) * (n)))
58 static int register_uprobe_event(struct trace_uprobe *tu);
59 static int unregister_uprobe_event(struct trace_uprobe *tu);
61 static DEFINE_MUTEX(uprobe_lock);
62 static LIST_HEAD(uprobe_list);
64 struct uprobe_dispatch_data {
65 struct trace_uprobe *tu;
66 unsigned long bp_addr;
69 static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs);
70 static int uretprobe_dispatcher(struct uprobe_consumer *con,
71 unsigned long func, struct pt_regs *regs);
73 #ifdef CONFIG_STACK_GROWSUP
74 static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n)
76 return addr - (n * sizeof(long));
78 #else
79 static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n)
81 return addr + (n * sizeof(long));
83 #endif
85 static unsigned long get_user_stack_nth(struct pt_regs *regs, unsigned int n)
87 unsigned long ret;
88 unsigned long addr = user_stack_pointer(regs);
90 addr = adjust_stack_addr(addr, n);
92 if (copy_from_user(&ret, (void __force __user *) addr, sizeof(ret)))
93 return 0;
95 return ret;
99 * Uprobes-specific fetch functions
101 #define DEFINE_FETCH_stack(type) \
102 static void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs, \
103 void *offset, void *dest) \
105 *(type *)dest = (type)get_user_stack_nth(regs, \
106 ((unsigned long)offset)); \
108 DEFINE_BASIC_FETCH_FUNCS(stack)
109 /* No string on the stack entry */
110 #define fetch_stack_string NULL
111 #define fetch_stack_string_size NULL
113 #define DEFINE_FETCH_memory(type) \
114 static void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs, \
115 void *addr, void *dest) \
117 type retval; \
118 void __user *vaddr = (void __force __user *) addr; \
120 if (copy_from_user(&retval, vaddr, sizeof(type))) \
121 *(type *)dest = 0; \
122 else \
123 *(type *) dest = retval; \
125 DEFINE_BASIC_FETCH_FUNCS(memory)
127 * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
128 * length and relative data location.
130 static void FETCH_FUNC_NAME(memory, string)(struct pt_regs *regs,
131 void *addr, void *dest)
133 long ret;
134 u32 rloc = *(u32 *)dest;
135 int maxlen = get_rloc_len(rloc);
136 u8 *dst = get_rloc_data(dest);
137 void __user *src = (void __force __user *) addr;
139 if (!maxlen)
140 return;
142 ret = strncpy_from_user(dst, src, maxlen);
143 if (ret == maxlen)
144 dst[--ret] = '\0';
146 if (ret < 0) { /* Failed to fetch string */
147 ((u8 *)get_rloc_data(dest))[0] = '\0';
148 *(u32 *)dest = make_data_rloc(0, get_rloc_offs(rloc));
149 } else {
150 *(u32 *)dest = make_data_rloc(ret, get_rloc_offs(rloc));
154 static void FETCH_FUNC_NAME(memory, string_size)(struct pt_regs *regs,
155 void *addr, void *dest)
157 int len;
158 void __user *vaddr = (void __force __user *) addr;
160 len = strnlen_user(vaddr, MAX_STRING_SIZE);
162 if (len == 0 || len > MAX_STRING_SIZE) /* Failed to check length */
163 *(u32 *)dest = 0;
164 else
165 *(u32 *)dest = len;
168 static unsigned long translate_user_vaddr(void *file_offset)
170 unsigned long base_addr;
171 struct uprobe_dispatch_data *udd;
173 udd = (void *) current->utask->vaddr;
175 base_addr = udd->bp_addr - udd->tu->offset;
176 return base_addr + (unsigned long)file_offset;
179 #define DEFINE_FETCH_file_offset(type) \
180 static void FETCH_FUNC_NAME(file_offset, type)(struct pt_regs *regs, \
181 void *offset, void *dest)\
183 void *vaddr = (void *)translate_user_vaddr(offset); \
185 FETCH_FUNC_NAME(memory, type)(regs, vaddr, dest); \
187 DEFINE_BASIC_FETCH_FUNCS(file_offset)
188 DEFINE_FETCH_file_offset(string)
189 DEFINE_FETCH_file_offset(string_size)
191 /* Fetch type information table */
192 static const struct fetch_type uprobes_fetch_type_table[] = {
193 /* Special types */
194 [FETCH_TYPE_STRING] = __ASSIGN_FETCH_TYPE("string", string, string,
195 sizeof(u32), 1, "__data_loc char[]"),
196 [FETCH_TYPE_STRSIZE] = __ASSIGN_FETCH_TYPE("string_size", u32,
197 string_size, sizeof(u32), 0, "u32"),
198 /* Basic types */
199 ASSIGN_FETCH_TYPE(u8, u8, 0),
200 ASSIGN_FETCH_TYPE(u16, u16, 0),
201 ASSIGN_FETCH_TYPE(u32, u32, 0),
202 ASSIGN_FETCH_TYPE(u64, u64, 0),
203 ASSIGN_FETCH_TYPE(s8, u8, 1),
204 ASSIGN_FETCH_TYPE(s16, u16, 1),
205 ASSIGN_FETCH_TYPE(s32, u32, 1),
206 ASSIGN_FETCH_TYPE(s64, u64, 1),
207 ASSIGN_FETCH_TYPE_ALIAS(x8, u8, u8, 0),
208 ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0),
209 ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0),
210 ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0),
212 ASSIGN_FETCH_TYPE_END
215 static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter)
217 rwlock_init(&filter->rwlock);
218 filter->nr_systemwide = 0;
219 INIT_LIST_HEAD(&filter->perf_events);
222 static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter *filter)
224 return !filter->nr_systemwide && list_empty(&filter->perf_events);
227 static inline bool is_ret_probe(struct trace_uprobe *tu)
229 return tu->consumer.ret_handler != NULL;
233 * Allocate new trace_uprobe and initialize it (including uprobes).
235 static struct trace_uprobe *
236 alloc_trace_uprobe(const char *group, const char *event, int nargs, bool is_ret)
238 struct trace_uprobe *tu;
240 if (!event || !is_good_name(event))
241 return ERR_PTR(-EINVAL);
243 if (!group || !is_good_name(group))
244 return ERR_PTR(-EINVAL);
246 tu = kzalloc(SIZEOF_TRACE_UPROBE(nargs), GFP_KERNEL);
247 if (!tu)
248 return ERR_PTR(-ENOMEM);
250 tu->tp.call.class = &tu->tp.class;
251 tu->tp.call.name = kstrdup(event, GFP_KERNEL);
252 if (!tu->tp.call.name)
253 goto error;
255 tu->tp.class.system = kstrdup(group, GFP_KERNEL);
256 if (!tu->tp.class.system)
257 goto error;
259 INIT_LIST_HEAD(&tu->list);
260 INIT_LIST_HEAD(&tu->tp.files);
261 tu->consumer.handler = uprobe_dispatcher;
262 if (is_ret)
263 tu->consumer.ret_handler = uretprobe_dispatcher;
264 init_trace_uprobe_filter(&tu->filter);
265 return tu;
267 error:
268 kfree(tu->tp.call.name);
269 kfree(tu);
271 return ERR_PTR(-ENOMEM);
274 static void free_trace_uprobe(struct trace_uprobe *tu)
276 int i;
278 for (i = 0; i < tu->tp.nr_args; i++)
279 traceprobe_free_probe_arg(&tu->tp.args[i]);
281 path_put(&tu->path);
282 kfree(tu->tp.call.class->system);
283 kfree(tu->tp.call.name);
284 kfree(tu->filename);
285 kfree(tu);
288 static struct trace_uprobe *find_probe_event(const char *event, const char *group)
290 struct trace_uprobe *tu;
292 list_for_each_entry(tu, &uprobe_list, list)
293 if (strcmp(trace_event_name(&tu->tp.call), event) == 0 &&
294 strcmp(tu->tp.call.class->system, group) == 0)
295 return tu;
297 return NULL;
300 /* Unregister a trace_uprobe and probe_event: call with locking uprobe_lock */
301 static int unregister_trace_uprobe(struct trace_uprobe *tu)
303 int ret;
305 ret = unregister_uprobe_event(tu);
306 if (ret)
307 return ret;
309 list_del(&tu->list);
310 free_trace_uprobe(tu);
311 return 0;
314 /* Register a trace_uprobe and probe_event */
315 static int register_trace_uprobe(struct trace_uprobe *tu)
317 struct trace_uprobe *old_tu;
318 int ret;
320 mutex_lock(&uprobe_lock);
322 /* register as an event */
323 old_tu = find_probe_event(trace_event_name(&tu->tp.call),
324 tu->tp.call.class->system);
325 if (old_tu) {
326 /* delete old event */
327 ret = unregister_trace_uprobe(old_tu);
328 if (ret)
329 goto end;
332 ret = register_uprobe_event(tu);
333 if (ret) {
334 pr_warn("Failed to register probe event(%d)\n", ret);
335 goto end;
338 list_add_tail(&tu->list, &uprobe_list);
340 end:
341 mutex_unlock(&uprobe_lock);
343 return ret;
347 * Argument syntax:
348 * - Add uprobe: p|r[:[GRP/]EVENT] PATH:OFFSET [FETCHARGS]
350 * - Remove uprobe: -:[GRP/]EVENT
352 static int create_trace_uprobe(int argc, char **argv)
354 struct trace_uprobe *tu;
355 char *arg, *event, *group, *filename;
356 char buf[MAX_EVENT_NAME_LEN];
357 struct path path;
358 unsigned long offset;
359 bool is_delete, is_return;
360 int i, ret;
362 ret = 0;
363 is_delete = false;
364 is_return = false;
365 event = NULL;
366 group = NULL;
368 /* argc must be >= 1 */
369 if (argv[0][0] == '-')
370 is_delete = true;
371 else if (argv[0][0] == 'r')
372 is_return = true;
373 else if (argv[0][0] != 'p') {
374 pr_info("Probe definition must be started with 'p', 'r' or '-'.\n");
375 return -EINVAL;
378 if (argv[0][1] == ':') {
379 event = &argv[0][2];
380 arg = strchr(event, '/');
382 if (arg) {
383 group = event;
384 event = arg + 1;
385 event[-1] = '\0';
387 if (strlen(group) == 0) {
388 pr_info("Group name is not specified\n");
389 return -EINVAL;
392 if (strlen(event) == 0) {
393 pr_info("Event name is not specified\n");
394 return -EINVAL;
397 if (!group)
398 group = UPROBE_EVENT_SYSTEM;
400 if (is_delete) {
401 int ret;
403 if (!event) {
404 pr_info("Delete command needs an event name.\n");
405 return -EINVAL;
407 mutex_lock(&uprobe_lock);
408 tu = find_probe_event(event, group);
410 if (!tu) {
411 mutex_unlock(&uprobe_lock);
412 pr_info("Event %s/%s doesn't exist.\n", group, event);
413 return -ENOENT;
415 /* delete an event */
416 ret = unregister_trace_uprobe(tu);
417 mutex_unlock(&uprobe_lock);
418 return ret;
421 if (argc < 2) {
422 pr_info("Probe point is not specified.\n");
423 return -EINVAL;
425 /* Find the last occurrence, in case the path contains ':' too. */
426 arg = strrchr(argv[1], ':');
427 if (!arg)
428 return -EINVAL;
430 *arg++ = '\0';
431 filename = argv[1];
432 ret = kern_path(filename, LOOKUP_FOLLOW, &path);
433 if (ret)
434 return ret;
436 if (!d_is_reg(path.dentry)) {
437 ret = -EINVAL;
438 goto fail_address_parse;
441 ret = kstrtoul(arg, 0, &offset);
442 if (ret)
443 goto fail_address_parse;
445 argc -= 2;
446 argv += 2;
448 /* setup a probe */
449 if (!event) {
450 char *tail;
451 char *ptr;
453 tail = kstrdup(kbasename(filename), GFP_KERNEL);
454 if (!tail) {
455 ret = -ENOMEM;
456 goto fail_address_parse;
459 ptr = strpbrk(tail, ".-_");
460 if (ptr)
461 *ptr = '\0';
463 snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset);
464 event = buf;
465 kfree(tail);
468 tu = alloc_trace_uprobe(group, event, argc, is_return);
469 if (IS_ERR(tu)) {
470 pr_info("Failed to allocate trace_uprobe.(%d)\n", (int)PTR_ERR(tu));
471 ret = PTR_ERR(tu);
472 goto fail_address_parse;
474 tu->offset = offset;
475 tu->path = path;
476 tu->filename = kstrdup(filename, GFP_KERNEL);
478 if (!tu->filename) {
479 pr_info("Failed to allocate filename.\n");
480 ret = -ENOMEM;
481 goto error;
484 /* parse arguments */
485 ret = 0;
486 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
487 struct probe_arg *parg = &tu->tp.args[i];
489 /* Increment count for freeing args in error case */
490 tu->tp.nr_args++;
492 /* Parse argument name */
493 arg = strchr(argv[i], '=');
494 if (arg) {
495 *arg++ = '\0';
496 parg->name = kstrdup(argv[i], GFP_KERNEL);
497 } else {
498 arg = argv[i];
499 /* If argument name is omitted, set "argN" */
500 snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
501 parg->name = kstrdup(buf, GFP_KERNEL);
504 if (!parg->name) {
505 pr_info("Failed to allocate argument[%d] name.\n", i);
506 ret = -ENOMEM;
507 goto error;
510 if (!is_good_name(parg->name)) {
511 pr_info("Invalid argument[%d] name: %s\n", i, parg->name);
512 ret = -EINVAL;
513 goto error;
516 if (traceprobe_conflict_field_name(parg->name, tu->tp.args, i)) {
517 pr_info("Argument[%d] name '%s' conflicts with "
518 "another field.\n", i, argv[i]);
519 ret = -EINVAL;
520 goto error;
523 /* Parse fetch argument */
524 ret = traceprobe_parse_probe_arg(arg, &tu->tp.size, parg,
525 is_return, false,
526 uprobes_fetch_type_table);
527 if (ret) {
528 pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
529 goto error;
533 ret = register_trace_uprobe(tu);
534 if (ret)
535 goto error;
536 return 0;
538 error:
539 free_trace_uprobe(tu);
540 return ret;
542 fail_address_parse:
543 path_put(&path);
545 pr_info("Failed to parse address or file.\n");
547 return ret;
550 static int cleanup_all_probes(void)
552 struct trace_uprobe *tu;
553 int ret = 0;
555 mutex_lock(&uprobe_lock);
556 while (!list_empty(&uprobe_list)) {
557 tu = list_entry(uprobe_list.next, struct trace_uprobe, list);
558 ret = unregister_trace_uprobe(tu);
559 if (ret)
560 break;
562 mutex_unlock(&uprobe_lock);
563 return ret;
566 /* Probes listing interfaces */
567 static void *probes_seq_start(struct seq_file *m, loff_t *pos)
569 mutex_lock(&uprobe_lock);
570 return seq_list_start(&uprobe_list, *pos);
573 static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
575 return seq_list_next(v, &uprobe_list, pos);
578 static void probes_seq_stop(struct seq_file *m, void *v)
580 mutex_unlock(&uprobe_lock);
583 static int probes_seq_show(struct seq_file *m, void *v)
585 struct trace_uprobe *tu = v;
586 char c = is_ret_probe(tu) ? 'r' : 'p';
587 int i;
589 seq_printf(m, "%c:%s/%s %s:0x%0*lx", c, tu->tp.call.class->system,
590 trace_event_name(&tu->tp.call), tu->filename,
591 (int)(sizeof(void *) * 2), tu->offset);
593 for (i = 0; i < tu->tp.nr_args; i++)
594 seq_printf(m, " %s=%s", tu->tp.args[i].name, tu->tp.args[i].comm);
596 seq_putc(m, '\n');
597 return 0;
600 static const struct seq_operations probes_seq_op = {
601 .start = probes_seq_start,
602 .next = probes_seq_next,
603 .stop = probes_seq_stop,
604 .show = probes_seq_show
607 static int probes_open(struct inode *inode, struct file *file)
609 int ret;
611 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
612 ret = cleanup_all_probes();
613 if (ret)
614 return ret;
617 return seq_open(file, &probes_seq_op);
620 static ssize_t probes_write(struct file *file, const char __user *buffer,
621 size_t count, loff_t *ppos)
623 return trace_parse_run_command(file, buffer, count, ppos, create_trace_uprobe);
626 static const struct file_operations uprobe_events_ops = {
627 .owner = THIS_MODULE,
628 .open = probes_open,
629 .read = seq_read,
630 .llseek = seq_lseek,
631 .release = seq_release,
632 .write = probes_write,
635 /* Probes profiling interfaces */
636 static int probes_profile_seq_show(struct seq_file *m, void *v)
638 struct trace_uprobe *tu = v;
640 seq_printf(m, " %s %-44s %15lu\n", tu->filename,
641 trace_event_name(&tu->tp.call), tu->nhit);
642 return 0;
645 static const struct seq_operations profile_seq_op = {
646 .start = probes_seq_start,
647 .next = probes_seq_next,
648 .stop = probes_seq_stop,
649 .show = probes_profile_seq_show
652 static int profile_open(struct inode *inode, struct file *file)
654 return seq_open(file, &profile_seq_op);
657 static const struct file_operations uprobe_profile_ops = {
658 .owner = THIS_MODULE,
659 .open = profile_open,
660 .read = seq_read,
661 .llseek = seq_lseek,
662 .release = seq_release,
665 struct uprobe_cpu_buffer {
666 struct mutex mutex;
667 void *buf;
669 static struct uprobe_cpu_buffer __percpu *uprobe_cpu_buffer;
670 static int uprobe_buffer_refcnt;
672 static int uprobe_buffer_init(void)
674 int cpu, err_cpu;
676 uprobe_cpu_buffer = alloc_percpu(struct uprobe_cpu_buffer);
677 if (uprobe_cpu_buffer == NULL)
678 return -ENOMEM;
680 for_each_possible_cpu(cpu) {
681 struct page *p = alloc_pages_node(cpu_to_node(cpu),
682 GFP_KERNEL, 0);
683 if (p == NULL) {
684 err_cpu = cpu;
685 goto err;
687 per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf = page_address(p);
688 mutex_init(&per_cpu_ptr(uprobe_cpu_buffer, cpu)->mutex);
691 return 0;
693 err:
694 for_each_possible_cpu(cpu) {
695 if (cpu == err_cpu)
696 break;
697 free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf);
700 free_percpu(uprobe_cpu_buffer);
701 return -ENOMEM;
704 static int uprobe_buffer_enable(void)
706 int ret = 0;
708 BUG_ON(!mutex_is_locked(&event_mutex));
710 if (uprobe_buffer_refcnt++ == 0) {
711 ret = uprobe_buffer_init();
712 if (ret < 0)
713 uprobe_buffer_refcnt--;
716 return ret;
719 static void uprobe_buffer_disable(void)
721 int cpu;
723 BUG_ON(!mutex_is_locked(&event_mutex));
725 if (--uprobe_buffer_refcnt == 0) {
726 for_each_possible_cpu(cpu)
727 free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer,
728 cpu)->buf);
730 free_percpu(uprobe_cpu_buffer);
731 uprobe_cpu_buffer = NULL;
735 static struct uprobe_cpu_buffer *uprobe_buffer_get(void)
737 struct uprobe_cpu_buffer *ucb;
738 int cpu;
740 cpu = raw_smp_processor_id();
741 ucb = per_cpu_ptr(uprobe_cpu_buffer, cpu);
744 * Use per-cpu buffers for fastest access, but we might migrate
745 * so the mutex makes sure we have sole access to it.
747 mutex_lock(&ucb->mutex);
749 return ucb;
752 static void uprobe_buffer_put(struct uprobe_cpu_buffer *ucb)
754 mutex_unlock(&ucb->mutex);
757 static void __uprobe_trace_func(struct trace_uprobe *tu,
758 unsigned long func, struct pt_regs *regs,
759 struct uprobe_cpu_buffer *ucb, int dsize,
760 struct trace_event_file *trace_file)
762 struct uprobe_trace_entry_head *entry;
763 struct ring_buffer_event *event;
764 struct ring_buffer *buffer;
765 void *data;
766 int size, esize;
767 struct trace_event_call *call = &tu->tp.call;
769 WARN_ON(call != trace_file->event_call);
771 if (WARN_ON_ONCE(tu->tp.size + dsize > PAGE_SIZE))
772 return;
774 if (trace_trigger_soft_disabled(trace_file))
775 return;
777 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
778 size = esize + tu->tp.size + dsize;
779 event = trace_event_buffer_lock_reserve(&buffer, trace_file,
780 call->event.type, size, 0, 0);
781 if (!event)
782 return;
784 entry = ring_buffer_event_data(event);
785 if (is_ret_probe(tu)) {
786 entry->vaddr[0] = func;
787 entry->vaddr[1] = instruction_pointer(regs);
788 data = DATAOF_TRACE_ENTRY(entry, true);
789 } else {
790 entry->vaddr[0] = instruction_pointer(regs);
791 data = DATAOF_TRACE_ENTRY(entry, false);
794 memcpy(data, ucb->buf, tu->tp.size + dsize);
796 event_trigger_unlock_commit(trace_file, buffer, event, entry, 0, 0);
799 /* uprobe handler */
800 static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs,
801 struct uprobe_cpu_buffer *ucb, int dsize)
803 struct event_file_link *link;
805 if (is_ret_probe(tu))
806 return 0;
808 rcu_read_lock();
809 list_for_each_entry_rcu(link, &tu->tp.files, list)
810 __uprobe_trace_func(tu, 0, regs, ucb, dsize, link->file);
811 rcu_read_unlock();
813 return 0;
816 static void uretprobe_trace_func(struct trace_uprobe *tu, unsigned long func,
817 struct pt_regs *regs,
818 struct uprobe_cpu_buffer *ucb, int dsize)
820 struct event_file_link *link;
822 rcu_read_lock();
823 list_for_each_entry_rcu(link, &tu->tp.files, list)
824 __uprobe_trace_func(tu, func, regs, ucb, dsize, link->file);
825 rcu_read_unlock();
828 /* Event entry printers */
829 static enum print_line_t
830 print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event)
832 struct uprobe_trace_entry_head *entry;
833 struct trace_seq *s = &iter->seq;
834 struct trace_uprobe *tu;
835 u8 *data;
836 int i;
838 entry = (struct uprobe_trace_entry_head *)iter->ent;
839 tu = container_of(event, struct trace_uprobe, tp.call.event);
841 if (is_ret_probe(tu)) {
842 trace_seq_printf(s, "%s: (0x%lx <- 0x%lx)",
843 trace_event_name(&tu->tp.call),
844 entry->vaddr[1], entry->vaddr[0]);
845 data = DATAOF_TRACE_ENTRY(entry, true);
846 } else {
847 trace_seq_printf(s, "%s: (0x%lx)",
848 trace_event_name(&tu->tp.call),
849 entry->vaddr[0]);
850 data = DATAOF_TRACE_ENTRY(entry, false);
853 for (i = 0; i < tu->tp.nr_args; i++) {
854 struct probe_arg *parg = &tu->tp.args[i];
856 if (!parg->type->print(s, parg->name, data + parg->offset, entry))
857 goto out;
860 trace_seq_putc(s, '\n');
862 out:
863 return trace_handle_return(s);
866 typedef bool (*filter_func_t)(struct uprobe_consumer *self,
867 enum uprobe_filter_ctx ctx,
868 struct mm_struct *mm);
870 static int
871 probe_event_enable(struct trace_uprobe *tu, struct trace_event_file *file,
872 filter_func_t filter)
874 bool enabled = trace_probe_is_enabled(&tu->tp);
875 struct event_file_link *link = NULL;
876 int ret;
878 if (file) {
879 if (tu->tp.flags & TP_FLAG_PROFILE)
880 return -EINTR;
882 link = kmalloc(sizeof(*link), GFP_KERNEL);
883 if (!link)
884 return -ENOMEM;
886 link->file = file;
887 list_add_tail_rcu(&link->list, &tu->tp.files);
889 tu->tp.flags |= TP_FLAG_TRACE;
890 } else {
891 if (tu->tp.flags & TP_FLAG_TRACE)
892 return -EINTR;
894 tu->tp.flags |= TP_FLAG_PROFILE;
897 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
899 if (enabled)
900 return 0;
902 ret = uprobe_buffer_enable();
903 if (ret)
904 goto err_flags;
906 tu->consumer.filter = filter;
907 tu->inode = d_real_inode(tu->path.dentry);
908 ret = uprobe_register(tu->inode, tu->offset, &tu->consumer);
909 if (ret)
910 goto err_buffer;
912 return 0;
914 err_buffer:
915 uprobe_buffer_disable();
917 err_flags:
918 if (file) {
919 list_del(&link->list);
920 kfree(link);
921 tu->tp.flags &= ~TP_FLAG_TRACE;
922 } else {
923 tu->tp.flags &= ~TP_FLAG_PROFILE;
925 return ret;
928 static void
929 probe_event_disable(struct trace_uprobe *tu, struct trace_event_file *file)
931 if (!trace_probe_is_enabled(&tu->tp))
932 return;
934 if (file) {
935 struct event_file_link *link;
937 link = find_event_file_link(&tu->tp, file);
938 if (!link)
939 return;
941 list_del_rcu(&link->list);
942 /* synchronize with u{,ret}probe_trace_func */
943 synchronize_rcu();
944 kfree(link);
946 if (!list_empty(&tu->tp.files))
947 return;
950 WARN_ON(!uprobe_filter_is_empty(&tu->filter));
952 uprobe_unregister(tu->inode, tu->offset, &tu->consumer);
953 tu->inode = NULL;
954 tu->tp.flags &= file ? ~TP_FLAG_TRACE : ~TP_FLAG_PROFILE;
956 uprobe_buffer_disable();
959 static int uprobe_event_define_fields(struct trace_event_call *event_call)
961 int ret, i, size;
962 struct uprobe_trace_entry_head field;
963 struct trace_uprobe *tu = event_call->data;
965 if (is_ret_probe(tu)) {
966 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_FUNC, 0);
967 DEFINE_FIELD(unsigned long, vaddr[1], FIELD_STRING_RETIP, 0);
968 size = SIZEOF_TRACE_ENTRY(true);
969 } else {
970 DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0);
971 size = SIZEOF_TRACE_ENTRY(false);
973 /* Set argument names as fields */
974 for (i = 0; i < tu->tp.nr_args; i++) {
975 struct probe_arg *parg = &tu->tp.args[i];
977 ret = trace_define_field(event_call, parg->type->fmttype,
978 parg->name, size + parg->offset,
979 parg->type->size, parg->type->is_signed,
980 FILTER_OTHER);
982 if (ret)
983 return ret;
985 return 0;
988 #ifdef CONFIG_PERF_EVENTS
989 static bool
990 __uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm)
992 struct perf_event *event;
994 if (filter->nr_systemwide)
995 return true;
997 list_for_each_entry(event, &filter->perf_events, hw.tp_list) {
998 if (event->hw.target->mm == mm)
999 return true;
1002 return false;
1005 static inline bool
1006 uprobe_filter_event(struct trace_uprobe *tu, struct perf_event *event)
1008 return __uprobe_perf_filter(&tu->filter, event->hw.target->mm);
1011 static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event)
1013 bool done;
1015 write_lock(&tu->filter.rwlock);
1016 if (event->hw.target) {
1017 list_del(&event->hw.tp_list);
1018 done = tu->filter.nr_systemwide ||
1019 (event->hw.target->flags & PF_EXITING) ||
1020 uprobe_filter_event(tu, event);
1021 } else {
1022 tu->filter.nr_systemwide--;
1023 done = tu->filter.nr_systemwide;
1025 write_unlock(&tu->filter.rwlock);
1027 if (!done)
1028 return uprobe_apply(tu->inode, tu->offset, &tu->consumer, false);
1030 return 0;
1033 static int uprobe_perf_open(struct trace_uprobe *tu, struct perf_event *event)
1035 bool done;
1036 int err;
1038 write_lock(&tu->filter.rwlock);
1039 if (event->hw.target) {
1041 * event->parent != NULL means copy_process(), we can avoid
1042 * uprobe_apply(). current->mm must be probed and we can rely
1043 * on dup_mmap() which preserves the already installed bp's.
1045 * attr.enable_on_exec means that exec/mmap will install the
1046 * breakpoints we need.
1048 done = tu->filter.nr_systemwide ||
1049 event->parent || event->attr.enable_on_exec ||
1050 uprobe_filter_event(tu, event);
1051 list_add(&event->hw.tp_list, &tu->filter.perf_events);
1052 } else {
1053 done = tu->filter.nr_systemwide;
1054 tu->filter.nr_systemwide++;
1056 write_unlock(&tu->filter.rwlock);
1058 err = 0;
1059 if (!done) {
1060 err = uprobe_apply(tu->inode, tu->offset, &tu->consumer, true);
1061 if (err)
1062 uprobe_perf_close(tu, event);
1064 return err;
1067 static bool uprobe_perf_filter(struct uprobe_consumer *uc,
1068 enum uprobe_filter_ctx ctx, struct mm_struct *mm)
1070 struct trace_uprobe *tu;
1071 int ret;
1073 tu = container_of(uc, struct trace_uprobe, consumer);
1074 read_lock(&tu->filter.rwlock);
1075 ret = __uprobe_perf_filter(&tu->filter, mm);
1076 read_unlock(&tu->filter.rwlock);
1078 return ret;
1081 static void __uprobe_perf_func(struct trace_uprobe *tu,
1082 unsigned long func, struct pt_regs *regs,
1083 struct uprobe_cpu_buffer *ucb, int dsize)
1085 struct trace_event_call *call = &tu->tp.call;
1086 struct uprobe_trace_entry_head *entry;
1087 struct hlist_head *head;
1088 void *data;
1089 int size, esize;
1090 int rctx;
1092 if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs))
1093 return;
1095 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1097 size = esize + tu->tp.size + dsize;
1098 size = ALIGN(size + sizeof(u32), sizeof(u64)) - sizeof(u32);
1099 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough"))
1100 return;
1102 preempt_disable();
1103 head = this_cpu_ptr(call->perf_events);
1104 if (hlist_empty(head))
1105 goto out;
1107 entry = perf_trace_buf_alloc(size, NULL, &rctx);
1108 if (!entry)
1109 goto out;
1111 if (is_ret_probe(tu)) {
1112 entry->vaddr[0] = func;
1113 entry->vaddr[1] = instruction_pointer(regs);
1114 data = DATAOF_TRACE_ENTRY(entry, true);
1115 } else {
1116 entry->vaddr[0] = instruction_pointer(regs);
1117 data = DATAOF_TRACE_ENTRY(entry, false);
1120 memcpy(data, ucb->buf, tu->tp.size + dsize);
1122 if (size - esize > tu->tp.size + dsize) {
1123 int len = tu->tp.size + dsize;
1125 memset(data + len, 0, size - esize - len);
1128 perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1129 head, NULL);
1130 out:
1131 preempt_enable();
1134 /* uprobe profile handler */
1135 static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs,
1136 struct uprobe_cpu_buffer *ucb, int dsize)
1138 if (!uprobe_perf_filter(&tu->consumer, 0, current->mm))
1139 return UPROBE_HANDLER_REMOVE;
1141 if (!is_ret_probe(tu))
1142 __uprobe_perf_func(tu, 0, regs, ucb, dsize);
1143 return 0;
1146 static void uretprobe_perf_func(struct trace_uprobe *tu, unsigned long func,
1147 struct pt_regs *regs,
1148 struct uprobe_cpu_buffer *ucb, int dsize)
1150 __uprobe_perf_func(tu, func, regs, ucb, dsize);
1153 int bpf_get_uprobe_info(const struct perf_event *event, u32 *fd_type,
1154 const char **filename, u64 *probe_offset,
1155 bool perf_type_tracepoint)
1157 const char *pevent = trace_event_name(event->tp_event);
1158 const char *group = event->tp_event->class->system;
1159 struct trace_uprobe *tu;
1161 if (perf_type_tracepoint)
1162 tu = find_probe_event(pevent, group);
1163 else
1164 tu = event->tp_event->data;
1165 if (!tu)
1166 return -EINVAL;
1168 *fd_type = is_ret_probe(tu) ? BPF_FD_TYPE_URETPROBE
1169 : BPF_FD_TYPE_UPROBE;
1170 *filename = tu->filename;
1171 *probe_offset = tu->offset;
1172 return 0;
1174 #endif /* CONFIG_PERF_EVENTS */
1176 static int
1177 trace_uprobe_register(struct trace_event_call *event, enum trace_reg type,
1178 void *data)
1180 struct trace_uprobe *tu = event->data;
1181 struct trace_event_file *file = data;
1183 switch (type) {
1184 case TRACE_REG_REGISTER:
1185 return probe_event_enable(tu, file, NULL);
1187 case TRACE_REG_UNREGISTER:
1188 probe_event_disable(tu, file);
1189 return 0;
1191 #ifdef CONFIG_PERF_EVENTS
1192 case TRACE_REG_PERF_REGISTER:
1193 return probe_event_enable(tu, NULL, uprobe_perf_filter);
1195 case TRACE_REG_PERF_UNREGISTER:
1196 probe_event_disable(tu, NULL);
1197 return 0;
1199 case TRACE_REG_PERF_OPEN:
1200 return uprobe_perf_open(tu, data);
1202 case TRACE_REG_PERF_CLOSE:
1203 return uprobe_perf_close(tu, data);
1205 #endif
1206 default:
1207 return 0;
1209 return 0;
1212 static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs)
1214 struct trace_uprobe *tu;
1215 struct uprobe_dispatch_data udd;
1216 struct uprobe_cpu_buffer *ucb;
1217 int dsize, esize;
1218 int ret = 0;
1221 tu = container_of(con, struct trace_uprobe, consumer);
1222 tu->nhit++;
1224 udd.tu = tu;
1225 udd.bp_addr = instruction_pointer(regs);
1227 current->utask->vaddr = (unsigned long) &udd;
1229 if (WARN_ON_ONCE(!uprobe_cpu_buffer))
1230 return 0;
1232 dsize = __get_data_size(&tu->tp, regs);
1233 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1235 ucb = uprobe_buffer_get();
1236 store_trace_args(esize, &tu->tp, regs, ucb->buf, dsize);
1238 if (tu->tp.flags & TP_FLAG_TRACE)
1239 ret |= uprobe_trace_func(tu, regs, ucb, dsize);
1241 #ifdef CONFIG_PERF_EVENTS
1242 if (tu->tp.flags & TP_FLAG_PROFILE)
1243 ret |= uprobe_perf_func(tu, regs, ucb, dsize);
1244 #endif
1245 uprobe_buffer_put(ucb);
1246 return ret;
1249 static int uretprobe_dispatcher(struct uprobe_consumer *con,
1250 unsigned long func, struct pt_regs *regs)
1252 struct trace_uprobe *tu;
1253 struct uprobe_dispatch_data udd;
1254 struct uprobe_cpu_buffer *ucb;
1255 int dsize, esize;
1257 tu = container_of(con, struct trace_uprobe, consumer);
1259 udd.tu = tu;
1260 udd.bp_addr = func;
1262 current->utask->vaddr = (unsigned long) &udd;
1264 if (WARN_ON_ONCE(!uprobe_cpu_buffer))
1265 return 0;
1267 dsize = __get_data_size(&tu->tp, regs);
1268 esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1270 ucb = uprobe_buffer_get();
1271 store_trace_args(esize, &tu->tp, regs, ucb->buf, dsize);
1273 if (tu->tp.flags & TP_FLAG_TRACE)
1274 uretprobe_trace_func(tu, func, regs, ucb, dsize);
1276 #ifdef CONFIG_PERF_EVENTS
1277 if (tu->tp.flags & TP_FLAG_PROFILE)
1278 uretprobe_perf_func(tu, func, regs, ucb, dsize);
1279 #endif
1280 uprobe_buffer_put(ucb);
1281 return 0;
1284 static struct trace_event_functions uprobe_funcs = {
1285 .trace = print_uprobe_event
1288 static inline void init_trace_event_call(struct trace_uprobe *tu,
1289 struct trace_event_call *call)
1291 INIT_LIST_HEAD(&call->class->fields);
1292 call->event.funcs = &uprobe_funcs;
1293 call->class->define_fields = uprobe_event_define_fields;
1295 call->flags = TRACE_EVENT_FL_UPROBE;
1296 call->class->reg = trace_uprobe_register;
1297 call->data = tu;
1300 static int register_uprobe_event(struct trace_uprobe *tu)
1302 struct trace_event_call *call = &tu->tp.call;
1303 int ret = 0;
1305 init_trace_event_call(tu, call);
1307 if (set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0)
1308 return -ENOMEM;
1310 ret = register_trace_event(&call->event);
1311 if (!ret) {
1312 kfree(call->print_fmt);
1313 return -ENODEV;
1316 ret = trace_add_event_call(call);
1318 if (ret) {
1319 pr_info("Failed to register uprobe event: %s\n",
1320 trace_event_name(call));
1321 kfree(call->print_fmt);
1322 unregister_trace_event(&call->event);
1325 return ret;
1328 static int unregister_uprobe_event(struct trace_uprobe *tu)
1330 int ret;
1332 /* tu->event is unregistered in trace_remove_event_call() */
1333 ret = trace_remove_event_call(&tu->tp.call);
1334 if (ret)
1335 return ret;
1336 kfree(tu->tp.call.print_fmt);
1337 tu->tp.call.print_fmt = NULL;
1338 return 0;
1341 #ifdef CONFIG_PERF_EVENTS
1342 struct trace_event_call *
1343 create_local_trace_uprobe(char *name, unsigned long offs, bool is_return)
1345 struct trace_uprobe *tu;
1346 struct path path;
1347 int ret;
1349 ret = kern_path(name, LOOKUP_FOLLOW, &path);
1350 if (ret)
1351 return ERR_PTR(ret);
1353 if (!d_is_reg(path.dentry)) {
1354 path_put(&path);
1355 return ERR_PTR(-EINVAL);
1359 * local trace_kprobes are not added to probe_list, so they are never
1360 * searched in find_trace_kprobe(). Therefore, there is no concern of
1361 * duplicated name "DUMMY_EVENT" here.
1363 tu = alloc_trace_uprobe(UPROBE_EVENT_SYSTEM, "DUMMY_EVENT", 0,
1364 is_return);
1366 if (IS_ERR(tu)) {
1367 pr_info("Failed to allocate trace_uprobe.(%d)\n",
1368 (int)PTR_ERR(tu));
1369 path_put(&path);
1370 return ERR_CAST(tu);
1373 tu->offset = offs;
1374 tu->path = path;
1375 tu->filename = kstrdup(name, GFP_KERNEL);
1376 init_trace_event_call(tu, &tu->tp.call);
1378 if (set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0) {
1379 ret = -ENOMEM;
1380 goto error;
1383 return &tu->tp.call;
1384 error:
1385 free_trace_uprobe(tu);
1386 return ERR_PTR(ret);
1389 void destroy_local_trace_uprobe(struct trace_event_call *event_call)
1391 struct trace_uprobe *tu;
1393 tu = container_of(event_call, struct trace_uprobe, tp.call);
1395 kfree(tu->tp.call.print_fmt);
1396 tu->tp.call.print_fmt = NULL;
1398 free_trace_uprobe(tu);
1400 #endif /* CONFIG_PERF_EVENTS */
1402 /* Make a trace interface for controling probe points */
1403 static __init int init_uprobe_trace(void)
1405 struct dentry *d_tracer;
1407 d_tracer = tracing_init_dentry();
1408 if (IS_ERR(d_tracer))
1409 return 0;
1411 trace_create_file("uprobe_events", 0644, d_tracer,
1412 NULL, &uprobe_events_ops);
1413 /* Profile interface */
1414 trace_create_file("uprobe_profile", 0444, d_tracer,
1415 NULL, &uprobe_profile_ops);
1416 return 0;
1419 fs_initcall(init_uprobe_trace);