xref: /openbmc/linux/kernel/trace/trace_uprobe.c (revision 6e6c61d3)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * uprobes-based tracing events
4  *
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
9 
10 #include <linux/ctype.h>
11 #include <linux/module.h>
12 #include <linux/uaccess.h>
13 #include <linux/uprobes.h>
14 #include <linux/namei.h>
15 #include <linux/string.h>
16 #include <linux/rculist.h>
17 
18 #include "trace_dynevent.h"
19 #include "trace_probe.h"
20 #include "trace_probe_tmpl.h"
21 
22 #define UPROBE_EVENT_SYSTEM	"uprobes"
23 
24 struct uprobe_trace_entry_head {
25 	struct trace_entry	ent;
26 	unsigned long		vaddr[];
27 };
28 
29 #define SIZEOF_TRACE_ENTRY(is_return)			\
30 	(sizeof(struct uprobe_trace_entry_head) +	\
31 	 sizeof(unsigned long) * (is_return ? 2 : 1))
32 
33 #define DATAOF_TRACE_ENTRY(entry, is_return)		\
34 	((void*)(entry) + SIZEOF_TRACE_ENTRY(is_return))
35 
36 struct trace_uprobe_filter {
37 	rwlock_t		rwlock;
38 	int			nr_systemwide;
39 	struct list_head	perf_events;
40 };
41 
42 static int trace_uprobe_create(int argc, const char **argv);
43 static int trace_uprobe_show(struct seq_file *m, struct dyn_event *ev);
44 static int trace_uprobe_release(struct dyn_event *ev);
45 static bool trace_uprobe_is_busy(struct dyn_event *ev);
46 static bool trace_uprobe_match(const char *system, const char *event,
47 			       struct dyn_event *ev);
48 
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,
55 };
56 
57 /*
58  * uprobe event core functions
59  */
60 struct trace_uprobe {
61 	struct dyn_event		devent;
62 	struct trace_uprobe_filter	filter;
63 	struct uprobe_consumer		consumer;
64 	struct path			path;
65 	struct inode			*inode;
66 	char				*filename;
67 	unsigned long			offset;
68 	unsigned long			ref_ctr_offset;
69 	unsigned long			nhit;
70 	struct trace_probe		tp;
71 };
72 
73 static bool is_trace_uprobe(struct dyn_event *ev)
74 {
75 	return ev->ops == &trace_uprobe_ops;
76 }
77 
78 static struct trace_uprobe *to_trace_uprobe(struct dyn_event *ev)
79 {
80 	return container_of(ev, struct trace_uprobe, devent);
81 }
82 
83 /**
84  * for_each_trace_uprobe - iterate over the trace_uprobe list
85  * @pos:	the struct trace_uprobe * for each entry
86  * @dpos:	the struct dyn_event * to use as a loop cursor
87  */
88 #define for_each_trace_uprobe(pos, dpos)	\
89 	for_each_dyn_event(dpos)		\
90 		if (is_trace_uprobe(dpos) && (pos = to_trace_uprobe(dpos)))
91 
92 #define SIZEOF_TRACE_UPROBE(n)				\
93 	(offsetof(struct trace_uprobe, tp.args) +	\
94 	(sizeof(struct probe_arg) * (n)))
95 
96 static int register_uprobe_event(struct trace_uprobe *tu);
97 static int unregister_uprobe_event(struct trace_uprobe *tu);
98 
99 struct uprobe_dispatch_data {
100 	struct trace_uprobe	*tu;
101 	unsigned long		bp_addr;
102 };
103 
104 static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs);
105 static int uretprobe_dispatcher(struct uprobe_consumer *con,
106 				unsigned long func, struct pt_regs *regs);
107 
108 #ifdef CONFIG_STACK_GROWSUP
109 static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n)
110 {
111 	return addr - (n * sizeof(long));
112 }
113 #else
114 static unsigned long adjust_stack_addr(unsigned long addr, unsigned int n)
115 {
116 	return addr + (n * sizeof(long));
117 }
118 #endif
119 
120 static unsigned long get_user_stack_nth(struct pt_regs *regs, unsigned int n)
121 {
122 	unsigned long ret;
123 	unsigned long addr = user_stack_pointer(regs);
124 
125 	addr = adjust_stack_addr(addr, n);
126 
127 	if (copy_from_user(&ret, (void __force __user *) addr, sizeof(ret)))
128 		return 0;
129 
130 	return ret;
131 }
132 
133 /*
134  * Uprobes-specific fetch functions
135  */
136 static nokprobe_inline int
137 probe_mem_read(void *dest, void *src, size_t size)
138 {
139 	void __user *vaddr = (void __force __user *)src;
140 
141 	return copy_from_user(dest, vaddr, size) ? -EFAULT : 0;
142 }
143 /*
144  * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
145  * length and relative data location.
146  */
147 static nokprobe_inline int
148 fetch_store_string(unsigned long addr, void *dest, void *base)
149 {
150 	long ret;
151 	u32 loc = *(u32 *)dest;
152 	int maxlen  = get_loc_len(loc);
153 	u8 *dst = get_loc_data(dest, base);
154 	void __user *src = (void __force __user *) addr;
155 
156 	if (unlikely(!maxlen))
157 		return -ENOMEM;
158 
159 	ret = strncpy_from_user(dst, src, maxlen);
160 	if (ret >= 0) {
161 		if (ret == maxlen)
162 			dst[ret - 1] = '\0';
163 		*(u32 *)dest = make_data_loc(ret, (void *)dst - base);
164 	}
165 
166 	return ret;
167 }
168 
169 /* Return the length of string -- including null terminal byte */
170 static nokprobe_inline int
171 fetch_store_strlen(unsigned long addr)
172 {
173 	int len;
174 	void __user *vaddr = (void __force __user *) addr;
175 
176 	len = strnlen_user(vaddr, MAX_STRING_SIZE);
177 
178 	return (len > MAX_STRING_SIZE) ? 0 : len;
179 }
180 
181 static unsigned long translate_user_vaddr(unsigned long file_offset)
182 {
183 	unsigned long base_addr;
184 	struct uprobe_dispatch_data *udd;
185 
186 	udd = (void *) current->utask->vaddr;
187 
188 	base_addr = udd->bp_addr - udd->tu->offset;
189 	return base_addr + file_offset;
190 }
191 
192 /* Note that we don't verify it, since the code does not come from user space */
193 static int
194 process_fetch_insn(struct fetch_insn *code, struct pt_regs *regs, void *dest,
195 		   void *base)
196 {
197 	unsigned long val;
198 
199 	/* 1st stage: get value from context */
200 	switch (code->op) {
201 	case FETCH_OP_REG:
202 		val = regs_get_register(regs, code->param);
203 		break;
204 	case FETCH_OP_STACK:
205 		val = get_user_stack_nth(regs, code->param);
206 		break;
207 	case FETCH_OP_STACKP:
208 		val = user_stack_pointer(regs);
209 		break;
210 	case FETCH_OP_RETVAL:
211 		val = regs_return_value(regs);
212 		break;
213 	case FETCH_OP_IMM:
214 		val = code->immediate;
215 		break;
216 	case FETCH_OP_FOFFS:
217 		val = translate_user_vaddr(code->immediate);
218 		break;
219 	default:
220 		return -EILSEQ;
221 	}
222 	code++;
223 
224 	return process_fetch_insn_bottom(code, val, dest, base);
225 }
226 NOKPROBE_SYMBOL(process_fetch_insn)
227 
228 static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter)
229 {
230 	rwlock_init(&filter->rwlock);
231 	filter->nr_systemwide = 0;
232 	INIT_LIST_HEAD(&filter->perf_events);
233 }
234 
235 static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter *filter)
236 {
237 	return !filter->nr_systemwide && list_empty(&filter->perf_events);
238 }
239 
240 static inline bool is_ret_probe(struct trace_uprobe *tu)
241 {
242 	return tu->consumer.ret_handler != NULL;
243 }
244 
245 static bool trace_uprobe_is_busy(struct dyn_event *ev)
246 {
247 	struct trace_uprobe *tu = to_trace_uprobe(ev);
248 
249 	return trace_probe_is_enabled(&tu->tp);
250 }
251 
252 static bool trace_uprobe_match(const char *system, const char *event,
253 			       struct dyn_event *ev)
254 {
255 	struct trace_uprobe *tu = to_trace_uprobe(ev);
256 
257 	return strcmp(trace_event_name(&tu->tp.call), event) == 0 &&
258 		(!system || strcmp(tu->tp.call.class->system, system) == 0);
259 }
260 
261 /*
262  * Allocate new trace_uprobe and initialize it (including uprobes).
263  */
264 static struct trace_uprobe *
265 alloc_trace_uprobe(const char *group, const char *event, int nargs, bool is_ret)
266 {
267 	struct trace_uprobe *tu;
268 
269 	if (!event || !is_good_name(event))
270 		return ERR_PTR(-EINVAL);
271 
272 	if (!group || !is_good_name(group))
273 		return ERR_PTR(-EINVAL);
274 
275 	tu = kzalloc(SIZEOF_TRACE_UPROBE(nargs), GFP_KERNEL);
276 	if (!tu)
277 		return ERR_PTR(-ENOMEM);
278 
279 	tu->tp.call.class = &tu->tp.class;
280 	tu->tp.call.name = kstrdup(event, GFP_KERNEL);
281 	if (!tu->tp.call.name)
282 		goto error;
283 
284 	tu->tp.class.system = kstrdup(group, GFP_KERNEL);
285 	if (!tu->tp.class.system)
286 		goto error;
287 
288 	dyn_event_init(&tu->devent, &trace_uprobe_ops);
289 	INIT_LIST_HEAD(&tu->tp.files);
290 	tu->consumer.handler = uprobe_dispatcher;
291 	if (is_ret)
292 		tu->consumer.ret_handler = uretprobe_dispatcher;
293 	init_trace_uprobe_filter(&tu->filter);
294 	return tu;
295 
296 error:
297 	kfree(tu->tp.call.name);
298 	kfree(tu);
299 
300 	return ERR_PTR(-ENOMEM);
301 }
302 
303 static void free_trace_uprobe(struct trace_uprobe *tu)
304 {
305 	int i;
306 
307 	if (!tu)
308 		return;
309 
310 	for (i = 0; i < tu->tp.nr_args; i++)
311 		traceprobe_free_probe_arg(&tu->tp.args[i]);
312 
313 	path_put(&tu->path);
314 	kfree(tu->tp.call.class->system);
315 	kfree(tu->tp.call.name);
316 	kfree(tu->filename);
317 	kfree(tu);
318 }
319 
320 static struct trace_uprobe *find_probe_event(const char *event, const char *group)
321 {
322 	struct dyn_event *pos;
323 	struct trace_uprobe *tu;
324 
325 	for_each_trace_uprobe(tu, pos)
326 		if (strcmp(trace_event_name(&tu->tp.call), event) == 0 &&
327 		    strcmp(tu->tp.call.class->system, group) == 0)
328 			return tu;
329 
330 	return NULL;
331 }
332 
333 /* Unregister a trace_uprobe and probe_event */
334 static int unregister_trace_uprobe(struct trace_uprobe *tu)
335 {
336 	int ret;
337 
338 	ret = unregister_uprobe_event(tu);
339 	if (ret)
340 		return ret;
341 
342 	dyn_event_remove(&tu->devent);
343 	free_trace_uprobe(tu);
344 	return 0;
345 }
346 
347 /*
348  * Uprobe with multiple reference counter is not allowed. i.e.
349  * If inode and offset matches, reference counter offset *must*
350  * match as well. Though, there is one exception: If user is
351  * replacing old trace_uprobe with new one(same group/event),
352  * then we allow same uprobe with new reference counter as far
353  * as the new one does not conflict with any other existing
354  * ones.
355  */
356 static struct trace_uprobe *find_old_trace_uprobe(struct trace_uprobe *new)
357 {
358 	struct dyn_event *pos;
359 	struct trace_uprobe *tmp, *old = NULL;
360 	struct inode *new_inode = d_real_inode(new->path.dentry);
361 
362 	old = find_probe_event(trace_event_name(&new->tp.call),
363 				new->tp.call.class->system);
364 
365 	for_each_trace_uprobe(tmp, pos) {
366 		if ((old ? old != tmp : true) &&
367 		    new_inode == d_real_inode(tmp->path.dentry) &&
368 		    new->offset == tmp->offset &&
369 		    new->ref_ctr_offset != tmp->ref_ctr_offset) {
370 			pr_warn("Reference counter offset mismatch.");
371 			return ERR_PTR(-EINVAL);
372 		}
373 	}
374 	return old;
375 }
376 
377 /* Register a trace_uprobe and probe_event */
378 static int register_trace_uprobe(struct trace_uprobe *tu)
379 {
380 	struct trace_uprobe *old_tu;
381 	int ret;
382 
383 	mutex_lock(&event_mutex);
384 
385 	/* register as an event */
386 	old_tu = find_old_trace_uprobe(tu);
387 	if (IS_ERR(old_tu)) {
388 		ret = PTR_ERR(old_tu);
389 		goto end;
390 	}
391 
392 	if (old_tu) {
393 		/* delete old event */
394 		ret = unregister_trace_uprobe(old_tu);
395 		if (ret)
396 			goto end;
397 	}
398 
399 	ret = register_uprobe_event(tu);
400 	if (ret) {
401 		pr_warn("Failed to register probe event(%d)\n", ret);
402 		goto end;
403 	}
404 
405 	dyn_event_add(&tu->devent);
406 
407 end:
408 	mutex_unlock(&event_mutex);
409 
410 	return ret;
411 }
412 
413 /*
414  * Argument syntax:
415  *  - Add uprobe: p|r[:[GRP/]EVENT] PATH:OFFSET [FETCHARGS]
416  *
417  *  - Remove uprobe: -:[GRP/]EVENT
418  */
419 static int trace_uprobe_create(int argc, const char **argv)
420 {
421 	struct trace_uprobe *tu;
422 	const char *event = NULL, *group = UPROBE_EVENT_SYSTEM;
423 	char *arg, *filename, *rctr, *rctr_end, *tmp;
424 	char buf[MAX_EVENT_NAME_LEN];
425 	struct path path;
426 	unsigned long offset, ref_ctr_offset;
427 	bool is_return = false;
428 	int i, ret;
429 
430 	ret = 0;
431 	ref_ctr_offset = 0;
432 
433 	/* argc must be >= 1 */
434 	if (argv[0][0] == 'r')
435 		is_return = true;
436 	else if (argv[0][0] != 'p' || argc < 2)
437 		return -ECANCELED;
438 
439 	if (argv[0][1] == ':')
440 		event = &argv[0][2];
441 
442 	if (!strchr(argv[1], '/'))
443 		return -ECANCELED;
444 
445 	filename = kstrdup(argv[1], GFP_KERNEL);
446 	if (!filename)
447 		return -ENOMEM;
448 
449 	/* Find the last occurrence, in case the path contains ':' too. */
450 	arg = strrchr(filename, ':');
451 	if (!arg || !isdigit(arg[1])) {
452 		kfree(filename);
453 		return -ECANCELED;
454 	}
455 
456 	*arg++ = '\0';
457 	ret = kern_path(filename, LOOKUP_FOLLOW, &path);
458 	if (ret) {
459 		kfree(filename);
460 		return ret;
461 	}
462 	if (!d_is_reg(path.dentry)) {
463 		ret = -EINVAL;
464 		goto fail_address_parse;
465 	}
466 
467 	/* Parse reference counter offset if specified. */
468 	rctr = strchr(arg, '(');
469 	if (rctr) {
470 		rctr_end = strchr(rctr, ')');
471 		if (rctr > rctr_end || *(rctr_end + 1) != 0) {
472 			ret = -EINVAL;
473 			pr_info("Invalid reference counter offset.\n");
474 			goto fail_address_parse;
475 		}
476 
477 		*rctr++ = '\0';
478 		*rctr_end = '\0';
479 		ret = kstrtoul(rctr, 0, &ref_ctr_offset);
480 		if (ret) {
481 			pr_info("Invalid reference counter offset.\n");
482 			goto fail_address_parse;
483 		}
484 	}
485 
486 	/* Parse uprobe offset. */
487 	ret = kstrtoul(arg, 0, &offset);
488 	if (ret)
489 		goto fail_address_parse;
490 
491 	argc -= 2;
492 	argv += 2;
493 
494 	/* setup a probe */
495 	if (event) {
496 		ret = traceprobe_parse_event_name(&event, &group, buf);
497 		if (ret)
498 			goto fail_address_parse;
499 	} else {
500 		char *tail;
501 		char *ptr;
502 
503 		tail = kstrdup(kbasename(filename), GFP_KERNEL);
504 		if (!tail) {
505 			ret = -ENOMEM;
506 			goto fail_address_parse;
507 		}
508 
509 		ptr = strpbrk(tail, ".-_");
510 		if (ptr)
511 			*ptr = '\0';
512 
513 		snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_0x%lx", 'p', tail, offset);
514 		event = buf;
515 		kfree(tail);
516 	}
517 
518 	tu = alloc_trace_uprobe(group, event, argc, is_return);
519 	if (IS_ERR(tu)) {
520 		pr_info("Failed to allocate trace_uprobe.(%d)\n", (int)PTR_ERR(tu));
521 		ret = PTR_ERR(tu);
522 		goto fail_address_parse;
523 	}
524 	tu->offset = offset;
525 	tu->ref_ctr_offset = ref_ctr_offset;
526 	tu->path = path;
527 	tu->filename = filename;
528 
529 	/* parse arguments */
530 	for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
531 		tmp = kstrdup(argv[i], GFP_KERNEL);
532 		if (!tmp) {
533 			ret = -ENOMEM;
534 			goto error;
535 		}
536 
537 		ret = traceprobe_parse_probe_arg(&tu->tp, i, tmp,
538 					is_return ? TPARG_FL_RETURN : 0);
539 		kfree(tmp);
540 		if (ret)
541 			goto error;
542 	}
543 
544 	ret = register_trace_uprobe(tu);
545 	if (ret)
546 		goto error;
547 	return 0;
548 
549 error:
550 	free_trace_uprobe(tu);
551 	return ret;
552 
553 fail_address_parse:
554 	path_put(&path);
555 	kfree(filename);
556 
557 	pr_info("Failed to parse address or file.\n");
558 
559 	return ret;
560 }
561 
562 static int create_or_delete_trace_uprobe(int argc, char **argv)
563 {
564 	int ret;
565 
566 	if (argv[0][0] == '-')
567 		return dyn_event_release(argc, argv, &trace_uprobe_ops);
568 
569 	ret = trace_uprobe_create(argc, (const char **)argv);
570 	return ret == -ECANCELED ? -EINVAL : ret;
571 }
572 
573 static int trace_uprobe_release(struct dyn_event *ev)
574 {
575 	struct trace_uprobe *tu = to_trace_uprobe(ev);
576 
577 	return unregister_trace_uprobe(tu);
578 }
579 
580 /* Probes listing interfaces */
581 static int trace_uprobe_show(struct seq_file *m, struct dyn_event *ev)
582 {
583 	struct trace_uprobe *tu = to_trace_uprobe(ev);
584 	char c = is_ret_probe(tu) ? 'r' : 'p';
585 	int i;
586 
587 	seq_printf(m, "%c:%s/%s %s:0x%0*lx", c, tu->tp.call.class->system,
588 			trace_event_name(&tu->tp.call), tu->filename,
589 			(int)(sizeof(void *) * 2), tu->offset);
590 
591 	if (tu->ref_ctr_offset)
592 		seq_printf(m, "(0x%lx)", tu->ref_ctr_offset);
593 
594 	for (i = 0; i < tu->tp.nr_args; i++)
595 		seq_printf(m, " %s=%s", tu->tp.args[i].name, tu->tp.args[i].comm);
596 
597 	seq_putc(m, '\n');
598 	return 0;
599 }
600 
601 static int probes_seq_show(struct seq_file *m, void *v)
602 {
603 	struct dyn_event *ev = v;
604 
605 	if (!is_trace_uprobe(ev))
606 		return 0;
607 
608 	return trace_uprobe_show(m, ev);
609 }
610 
611 static const struct seq_operations probes_seq_op = {
612 	.start  = dyn_event_seq_start,
613 	.next   = dyn_event_seq_next,
614 	.stop   = dyn_event_seq_stop,
615 	.show   = probes_seq_show
616 };
617 
618 static int probes_open(struct inode *inode, struct file *file)
619 {
620 	int ret;
621 
622 	if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
623 		ret = dyn_events_release_all(&trace_uprobe_ops);
624 		if (ret)
625 			return ret;
626 	}
627 
628 	return seq_open(file, &probes_seq_op);
629 }
630 
631 static ssize_t probes_write(struct file *file, const char __user *buffer,
632 			    size_t count, loff_t *ppos)
633 {
634 	return trace_parse_run_command(file, buffer, count, ppos,
635 					create_or_delete_trace_uprobe);
636 }
637 
638 static const struct file_operations uprobe_events_ops = {
639 	.owner		= THIS_MODULE,
640 	.open		= probes_open,
641 	.read		= seq_read,
642 	.llseek		= seq_lseek,
643 	.release	= seq_release,
644 	.write		= probes_write,
645 };
646 
647 /* Probes profiling interfaces */
648 static int probes_profile_seq_show(struct seq_file *m, void *v)
649 {
650 	struct dyn_event *ev = v;
651 	struct trace_uprobe *tu;
652 
653 	if (!is_trace_uprobe(ev))
654 		return 0;
655 
656 	tu = to_trace_uprobe(ev);
657 	seq_printf(m, "  %s %-44s %15lu\n", tu->filename,
658 			trace_event_name(&tu->tp.call), tu->nhit);
659 	return 0;
660 }
661 
662 static const struct seq_operations profile_seq_op = {
663 	.start  = dyn_event_seq_start,
664 	.next   = dyn_event_seq_next,
665 	.stop   = dyn_event_seq_stop,
666 	.show	= probes_profile_seq_show
667 };
668 
669 static int profile_open(struct inode *inode, struct file *file)
670 {
671 	return seq_open(file, &profile_seq_op);
672 }
673 
674 static const struct file_operations uprobe_profile_ops = {
675 	.owner		= THIS_MODULE,
676 	.open		= profile_open,
677 	.read		= seq_read,
678 	.llseek		= seq_lseek,
679 	.release	= seq_release,
680 };
681 
682 struct uprobe_cpu_buffer {
683 	struct mutex mutex;
684 	void *buf;
685 };
686 static struct uprobe_cpu_buffer __percpu *uprobe_cpu_buffer;
687 static int uprobe_buffer_refcnt;
688 
689 static int uprobe_buffer_init(void)
690 {
691 	int cpu, err_cpu;
692 
693 	uprobe_cpu_buffer = alloc_percpu(struct uprobe_cpu_buffer);
694 	if (uprobe_cpu_buffer == NULL)
695 		return -ENOMEM;
696 
697 	for_each_possible_cpu(cpu) {
698 		struct page *p = alloc_pages_node(cpu_to_node(cpu),
699 						  GFP_KERNEL, 0);
700 		if (p == NULL) {
701 			err_cpu = cpu;
702 			goto err;
703 		}
704 		per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf = page_address(p);
705 		mutex_init(&per_cpu_ptr(uprobe_cpu_buffer, cpu)->mutex);
706 	}
707 
708 	return 0;
709 
710 err:
711 	for_each_possible_cpu(cpu) {
712 		if (cpu == err_cpu)
713 			break;
714 		free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer, cpu)->buf);
715 	}
716 
717 	free_percpu(uprobe_cpu_buffer);
718 	return -ENOMEM;
719 }
720 
721 static int uprobe_buffer_enable(void)
722 {
723 	int ret = 0;
724 
725 	BUG_ON(!mutex_is_locked(&event_mutex));
726 
727 	if (uprobe_buffer_refcnt++ == 0) {
728 		ret = uprobe_buffer_init();
729 		if (ret < 0)
730 			uprobe_buffer_refcnt--;
731 	}
732 
733 	return ret;
734 }
735 
736 static void uprobe_buffer_disable(void)
737 {
738 	int cpu;
739 
740 	BUG_ON(!mutex_is_locked(&event_mutex));
741 
742 	if (--uprobe_buffer_refcnt == 0) {
743 		for_each_possible_cpu(cpu)
744 			free_page((unsigned long)per_cpu_ptr(uprobe_cpu_buffer,
745 							     cpu)->buf);
746 
747 		free_percpu(uprobe_cpu_buffer);
748 		uprobe_cpu_buffer = NULL;
749 	}
750 }
751 
752 static struct uprobe_cpu_buffer *uprobe_buffer_get(void)
753 {
754 	struct uprobe_cpu_buffer *ucb;
755 	int cpu;
756 
757 	cpu = raw_smp_processor_id();
758 	ucb = per_cpu_ptr(uprobe_cpu_buffer, cpu);
759 
760 	/*
761 	 * Use per-cpu buffers for fastest access, but we might migrate
762 	 * so the mutex makes sure we have sole access to it.
763 	 */
764 	mutex_lock(&ucb->mutex);
765 
766 	return ucb;
767 }
768 
769 static void uprobe_buffer_put(struct uprobe_cpu_buffer *ucb)
770 {
771 	mutex_unlock(&ucb->mutex);
772 }
773 
774 static void __uprobe_trace_func(struct trace_uprobe *tu,
775 				unsigned long func, struct pt_regs *regs,
776 				struct uprobe_cpu_buffer *ucb, int dsize,
777 				struct trace_event_file *trace_file)
778 {
779 	struct uprobe_trace_entry_head *entry;
780 	struct ring_buffer_event *event;
781 	struct ring_buffer *buffer;
782 	void *data;
783 	int size, esize;
784 	struct trace_event_call *call = &tu->tp.call;
785 
786 	WARN_ON(call != trace_file->event_call);
787 
788 	if (WARN_ON_ONCE(tu->tp.size + dsize > PAGE_SIZE))
789 		return;
790 
791 	if (trace_trigger_soft_disabled(trace_file))
792 		return;
793 
794 	esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
795 	size = esize + tu->tp.size + dsize;
796 	event = trace_event_buffer_lock_reserve(&buffer, trace_file,
797 						call->event.type, size, 0, 0);
798 	if (!event)
799 		return;
800 
801 	entry = ring_buffer_event_data(event);
802 	if (is_ret_probe(tu)) {
803 		entry->vaddr[0] = func;
804 		entry->vaddr[1] = instruction_pointer(regs);
805 		data = DATAOF_TRACE_ENTRY(entry, true);
806 	} else {
807 		entry->vaddr[0] = instruction_pointer(regs);
808 		data = DATAOF_TRACE_ENTRY(entry, false);
809 	}
810 
811 	memcpy(data, ucb->buf, tu->tp.size + dsize);
812 
813 	event_trigger_unlock_commit(trace_file, buffer, event, entry, 0, 0);
814 }
815 
816 /* uprobe handler */
817 static int uprobe_trace_func(struct trace_uprobe *tu, struct pt_regs *regs,
818 			     struct uprobe_cpu_buffer *ucb, int dsize)
819 {
820 	struct event_file_link *link;
821 
822 	if (is_ret_probe(tu))
823 		return 0;
824 
825 	rcu_read_lock();
826 	list_for_each_entry_rcu(link, &tu->tp.files, list)
827 		__uprobe_trace_func(tu, 0, regs, ucb, dsize, link->file);
828 	rcu_read_unlock();
829 
830 	return 0;
831 }
832 
833 static void uretprobe_trace_func(struct trace_uprobe *tu, unsigned long func,
834 				 struct pt_regs *regs,
835 				 struct uprobe_cpu_buffer *ucb, int dsize)
836 {
837 	struct event_file_link *link;
838 
839 	rcu_read_lock();
840 	list_for_each_entry_rcu(link, &tu->tp.files, list)
841 		__uprobe_trace_func(tu, func, regs, ucb, dsize, link->file);
842 	rcu_read_unlock();
843 }
844 
845 /* Event entry printers */
846 static enum print_line_t
847 print_uprobe_event(struct trace_iterator *iter, int flags, struct trace_event *event)
848 {
849 	struct uprobe_trace_entry_head *entry;
850 	struct trace_seq *s = &iter->seq;
851 	struct trace_uprobe *tu;
852 	u8 *data;
853 
854 	entry = (struct uprobe_trace_entry_head *)iter->ent;
855 	tu = container_of(event, struct trace_uprobe, tp.call.event);
856 
857 	if (is_ret_probe(tu)) {
858 		trace_seq_printf(s, "%s: (0x%lx <- 0x%lx)",
859 				 trace_event_name(&tu->tp.call),
860 				 entry->vaddr[1], entry->vaddr[0]);
861 		data = DATAOF_TRACE_ENTRY(entry, true);
862 	} else {
863 		trace_seq_printf(s, "%s: (0x%lx)",
864 				 trace_event_name(&tu->tp.call),
865 				 entry->vaddr[0]);
866 		data = DATAOF_TRACE_ENTRY(entry, false);
867 	}
868 
869 	if (print_probe_args(s, tu->tp.args, tu->tp.nr_args, data, entry) < 0)
870 		goto out;
871 
872 	trace_seq_putc(s, '\n');
873 
874  out:
875 	return trace_handle_return(s);
876 }
877 
878 typedef bool (*filter_func_t)(struct uprobe_consumer *self,
879 				enum uprobe_filter_ctx ctx,
880 				struct mm_struct *mm);
881 
882 static int
883 probe_event_enable(struct trace_uprobe *tu, struct trace_event_file *file,
884 		   filter_func_t filter)
885 {
886 	bool enabled = trace_probe_is_enabled(&tu->tp);
887 	struct event_file_link *link = NULL;
888 	int ret;
889 
890 	if (file) {
891 		if (tu->tp.flags & TP_FLAG_PROFILE)
892 			return -EINTR;
893 
894 		link = kmalloc(sizeof(*link), GFP_KERNEL);
895 		if (!link)
896 			return -ENOMEM;
897 
898 		link->file = file;
899 		list_add_tail_rcu(&link->list, &tu->tp.files);
900 
901 		tu->tp.flags |= TP_FLAG_TRACE;
902 	} else {
903 		if (tu->tp.flags & TP_FLAG_TRACE)
904 			return -EINTR;
905 
906 		tu->tp.flags |= TP_FLAG_PROFILE;
907 	}
908 
909 	WARN_ON(!uprobe_filter_is_empty(&tu->filter));
910 
911 	if (enabled)
912 		return 0;
913 
914 	ret = uprobe_buffer_enable();
915 	if (ret)
916 		goto err_flags;
917 
918 	tu->consumer.filter = filter;
919 	tu->inode = d_real_inode(tu->path.dentry);
920 	if (tu->ref_ctr_offset) {
921 		ret = uprobe_register_refctr(tu->inode, tu->offset,
922 				tu->ref_ctr_offset, &tu->consumer);
923 	} else {
924 		ret = uprobe_register(tu->inode, tu->offset, &tu->consumer);
925 	}
926 
927 	if (ret)
928 		goto err_buffer;
929 
930 	return 0;
931 
932  err_buffer:
933 	uprobe_buffer_disable();
934 
935  err_flags:
936 	if (file) {
937 		list_del(&link->list);
938 		kfree(link);
939 		tu->tp.flags &= ~TP_FLAG_TRACE;
940 	} else {
941 		tu->tp.flags &= ~TP_FLAG_PROFILE;
942 	}
943 	return ret;
944 }
945 
946 static void
947 probe_event_disable(struct trace_uprobe *tu, struct trace_event_file *file)
948 {
949 	if (!trace_probe_is_enabled(&tu->tp))
950 		return;
951 
952 	if (file) {
953 		struct event_file_link *link;
954 
955 		link = find_event_file_link(&tu->tp, file);
956 		if (!link)
957 			return;
958 
959 		list_del_rcu(&link->list);
960 		/* synchronize with u{,ret}probe_trace_func */
961 		synchronize_rcu();
962 		kfree(link);
963 
964 		if (!list_empty(&tu->tp.files))
965 			return;
966 	}
967 
968 	WARN_ON(!uprobe_filter_is_empty(&tu->filter));
969 
970 	uprobe_unregister(tu->inode, tu->offset, &tu->consumer);
971 	tu->inode = NULL;
972 	tu->tp.flags &= file ? ~TP_FLAG_TRACE : ~TP_FLAG_PROFILE;
973 
974 	uprobe_buffer_disable();
975 }
976 
977 static int uprobe_event_define_fields(struct trace_event_call *event_call)
978 {
979 	int ret, size;
980 	struct uprobe_trace_entry_head field;
981 	struct trace_uprobe *tu = event_call->data;
982 
983 	if (is_ret_probe(tu)) {
984 		DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_FUNC, 0);
985 		DEFINE_FIELD(unsigned long, vaddr[1], FIELD_STRING_RETIP, 0);
986 		size = SIZEOF_TRACE_ENTRY(true);
987 	} else {
988 		DEFINE_FIELD(unsigned long, vaddr[0], FIELD_STRING_IP, 0);
989 		size = SIZEOF_TRACE_ENTRY(false);
990 	}
991 
992 	return traceprobe_define_arg_fields(event_call, size, &tu->tp);
993 }
994 
995 #ifdef CONFIG_PERF_EVENTS
996 static bool
997 __uprobe_perf_filter(struct trace_uprobe_filter *filter, struct mm_struct *mm)
998 {
999 	struct perf_event *event;
1000 
1001 	if (filter->nr_systemwide)
1002 		return true;
1003 
1004 	list_for_each_entry(event, &filter->perf_events, hw.tp_list) {
1005 		if (event->hw.target->mm == mm)
1006 			return true;
1007 	}
1008 
1009 	return false;
1010 }
1011 
1012 static inline bool
1013 uprobe_filter_event(struct trace_uprobe *tu, struct perf_event *event)
1014 {
1015 	return __uprobe_perf_filter(&tu->filter, event->hw.target->mm);
1016 }
1017 
1018 static int uprobe_perf_close(struct trace_uprobe *tu, struct perf_event *event)
1019 {
1020 	bool done;
1021 
1022 	write_lock(&tu->filter.rwlock);
1023 	if (event->hw.target) {
1024 		list_del(&event->hw.tp_list);
1025 		done = tu->filter.nr_systemwide ||
1026 			(event->hw.target->flags & PF_EXITING) ||
1027 			uprobe_filter_event(tu, event);
1028 	} else {
1029 		tu->filter.nr_systemwide--;
1030 		done = tu->filter.nr_systemwide;
1031 	}
1032 	write_unlock(&tu->filter.rwlock);
1033 
1034 	if (!done)
1035 		return uprobe_apply(tu->inode, tu->offset, &tu->consumer, false);
1036 
1037 	return 0;
1038 }
1039 
1040 static int uprobe_perf_open(struct trace_uprobe *tu, struct perf_event *event)
1041 {
1042 	bool done;
1043 	int err;
1044 
1045 	write_lock(&tu->filter.rwlock);
1046 	if (event->hw.target) {
1047 		/*
1048 		 * event->parent != NULL means copy_process(), we can avoid
1049 		 * uprobe_apply(). current->mm must be probed and we can rely
1050 		 * on dup_mmap() which preserves the already installed bp's.
1051 		 *
1052 		 * attr.enable_on_exec means that exec/mmap will install the
1053 		 * breakpoints we need.
1054 		 */
1055 		done = tu->filter.nr_systemwide ||
1056 			event->parent || event->attr.enable_on_exec ||
1057 			uprobe_filter_event(tu, event);
1058 		list_add(&event->hw.tp_list, &tu->filter.perf_events);
1059 	} else {
1060 		done = tu->filter.nr_systemwide;
1061 		tu->filter.nr_systemwide++;
1062 	}
1063 	write_unlock(&tu->filter.rwlock);
1064 
1065 	err = 0;
1066 	if (!done) {
1067 		err = uprobe_apply(tu->inode, tu->offset, &tu->consumer, true);
1068 		if (err)
1069 			uprobe_perf_close(tu, event);
1070 	}
1071 	return err;
1072 }
1073 
1074 static bool uprobe_perf_filter(struct uprobe_consumer *uc,
1075 				enum uprobe_filter_ctx ctx, struct mm_struct *mm)
1076 {
1077 	struct trace_uprobe *tu;
1078 	int ret;
1079 
1080 	tu = container_of(uc, struct trace_uprobe, consumer);
1081 	read_lock(&tu->filter.rwlock);
1082 	ret = __uprobe_perf_filter(&tu->filter, mm);
1083 	read_unlock(&tu->filter.rwlock);
1084 
1085 	return ret;
1086 }
1087 
1088 static void __uprobe_perf_func(struct trace_uprobe *tu,
1089 			       unsigned long func, struct pt_regs *regs,
1090 			       struct uprobe_cpu_buffer *ucb, int dsize)
1091 {
1092 	struct trace_event_call *call = &tu->tp.call;
1093 	struct uprobe_trace_entry_head *entry;
1094 	struct hlist_head *head;
1095 	void *data;
1096 	int size, esize;
1097 	int rctx;
1098 
1099 	if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs))
1100 		return;
1101 
1102 	esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1103 
1104 	size = esize + tu->tp.size + dsize;
1105 	size = ALIGN(size + sizeof(u32), sizeof(u64)) - sizeof(u32);
1106 	if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, "profile buffer not large enough"))
1107 		return;
1108 
1109 	preempt_disable();
1110 	head = this_cpu_ptr(call->perf_events);
1111 	if (hlist_empty(head))
1112 		goto out;
1113 
1114 	entry = perf_trace_buf_alloc(size, NULL, &rctx);
1115 	if (!entry)
1116 		goto out;
1117 
1118 	if (is_ret_probe(tu)) {
1119 		entry->vaddr[0] = func;
1120 		entry->vaddr[1] = instruction_pointer(regs);
1121 		data = DATAOF_TRACE_ENTRY(entry, true);
1122 	} else {
1123 		entry->vaddr[0] = instruction_pointer(regs);
1124 		data = DATAOF_TRACE_ENTRY(entry, false);
1125 	}
1126 
1127 	memcpy(data, ucb->buf, tu->tp.size + dsize);
1128 
1129 	if (size - esize > tu->tp.size + dsize) {
1130 		int len = tu->tp.size + dsize;
1131 
1132 		memset(data + len, 0, size - esize - len);
1133 	}
1134 
1135 	perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1136 			      head, NULL);
1137  out:
1138 	preempt_enable();
1139 }
1140 
1141 /* uprobe profile handler */
1142 static int uprobe_perf_func(struct trace_uprobe *tu, struct pt_regs *regs,
1143 			    struct uprobe_cpu_buffer *ucb, int dsize)
1144 {
1145 	if (!uprobe_perf_filter(&tu->consumer, 0, current->mm))
1146 		return UPROBE_HANDLER_REMOVE;
1147 
1148 	if (!is_ret_probe(tu))
1149 		__uprobe_perf_func(tu, 0, regs, ucb, dsize);
1150 	return 0;
1151 }
1152 
1153 static void uretprobe_perf_func(struct trace_uprobe *tu, unsigned long func,
1154 				struct pt_regs *regs,
1155 				struct uprobe_cpu_buffer *ucb, int dsize)
1156 {
1157 	__uprobe_perf_func(tu, func, regs, ucb, dsize);
1158 }
1159 
1160 int bpf_get_uprobe_info(const struct perf_event *event, u32 *fd_type,
1161 			const char **filename, u64 *probe_offset,
1162 			bool perf_type_tracepoint)
1163 {
1164 	const char *pevent = trace_event_name(event->tp_event);
1165 	const char *group = event->tp_event->class->system;
1166 	struct trace_uprobe *tu;
1167 
1168 	if (perf_type_tracepoint)
1169 		tu = find_probe_event(pevent, group);
1170 	else
1171 		tu = event->tp_event->data;
1172 	if (!tu)
1173 		return -EINVAL;
1174 
1175 	*fd_type = is_ret_probe(tu) ? BPF_FD_TYPE_URETPROBE
1176 				    : BPF_FD_TYPE_UPROBE;
1177 	*filename = tu->filename;
1178 	*probe_offset = tu->offset;
1179 	return 0;
1180 }
1181 #endif	/* CONFIG_PERF_EVENTS */
1182 
1183 static int
1184 trace_uprobe_register(struct trace_event_call *event, enum trace_reg type,
1185 		      void *data)
1186 {
1187 	struct trace_uprobe *tu = event->data;
1188 	struct trace_event_file *file = data;
1189 
1190 	switch (type) {
1191 	case TRACE_REG_REGISTER:
1192 		return probe_event_enable(tu, file, NULL);
1193 
1194 	case TRACE_REG_UNREGISTER:
1195 		probe_event_disable(tu, file);
1196 		return 0;
1197 
1198 #ifdef CONFIG_PERF_EVENTS
1199 	case TRACE_REG_PERF_REGISTER:
1200 		return probe_event_enable(tu, NULL, uprobe_perf_filter);
1201 
1202 	case TRACE_REG_PERF_UNREGISTER:
1203 		probe_event_disable(tu, NULL);
1204 		return 0;
1205 
1206 	case TRACE_REG_PERF_OPEN:
1207 		return uprobe_perf_open(tu, data);
1208 
1209 	case TRACE_REG_PERF_CLOSE:
1210 		return uprobe_perf_close(tu, data);
1211 
1212 #endif
1213 	default:
1214 		return 0;
1215 	}
1216 	return 0;
1217 }
1218 
1219 static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs)
1220 {
1221 	struct trace_uprobe *tu;
1222 	struct uprobe_dispatch_data udd;
1223 	struct uprobe_cpu_buffer *ucb;
1224 	int dsize, esize;
1225 	int ret = 0;
1226 
1227 
1228 	tu = container_of(con, struct trace_uprobe, consumer);
1229 	tu->nhit++;
1230 
1231 	udd.tu = tu;
1232 	udd.bp_addr = instruction_pointer(regs);
1233 
1234 	current->utask->vaddr = (unsigned long) &udd;
1235 
1236 	if (WARN_ON_ONCE(!uprobe_cpu_buffer))
1237 		return 0;
1238 
1239 	dsize = __get_data_size(&tu->tp, regs);
1240 	esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1241 
1242 	ucb = uprobe_buffer_get();
1243 	store_trace_args(ucb->buf, &tu->tp, regs, esize, dsize);
1244 
1245 	if (tu->tp.flags & TP_FLAG_TRACE)
1246 		ret |= uprobe_trace_func(tu, regs, ucb, dsize);
1247 
1248 #ifdef CONFIG_PERF_EVENTS
1249 	if (tu->tp.flags & TP_FLAG_PROFILE)
1250 		ret |= uprobe_perf_func(tu, regs, ucb, dsize);
1251 #endif
1252 	uprobe_buffer_put(ucb);
1253 	return ret;
1254 }
1255 
1256 static int uretprobe_dispatcher(struct uprobe_consumer *con,
1257 				unsigned long func, struct pt_regs *regs)
1258 {
1259 	struct trace_uprobe *tu;
1260 	struct uprobe_dispatch_data udd;
1261 	struct uprobe_cpu_buffer *ucb;
1262 	int dsize, esize;
1263 
1264 	tu = container_of(con, struct trace_uprobe, consumer);
1265 
1266 	udd.tu = tu;
1267 	udd.bp_addr = func;
1268 
1269 	current->utask->vaddr = (unsigned long) &udd;
1270 
1271 	if (WARN_ON_ONCE(!uprobe_cpu_buffer))
1272 		return 0;
1273 
1274 	dsize = __get_data_size(&tu->tp, regs);
1275 	esize = SIZEOF_TRACE_ENTRY(is_ret_probe(tu));
1276 
1277 	ucb = uprobe_buffer_get();
1278 	store_trace_args(ucb->buf, &tu->tp, regs, esize, dsize);
1279 
1280 	if (tu->tp.flags & TP_FLAG_TRACE)
1281 		uretprobe_trace_func(tu, func, regs, ucb, dsize);
1282 
1283 #ifdef CONFIG_PERF_EVENTS
1284 	if (tu->tp.flags & TP_FLAG_PROFILE)
1285 		uretprobe_perf_func(tu, func, regs, ucb, dsize);
1286 #endif
1287 	uprobe_buffer_put(ucb);
1288 	return 0;
1289 }
1290 
1291 static struct trace_event_functions uprobe_funcs = {
1292 	.trace		= print_uprobe_event
1293 };
1294 
1295 static inline void init_trace_event_call(struct trace_uprobe *tu,
1296 					 struct trace_event_call *call)
1297 {
1298 	INIT_LIST_HEAD(&call->class->fields);
1299 	call->event.funcs = &uprobe_funcs;
1300 	call->class->define_fields = uprobe_event_define_fields;
1301 
1302 	call->flags = TRACE_EVENT_FL_UPROBE;
1303 	call->class->reg = trace_uprobe_register;
1304 	call->data = tu;
1305 }
1306 
1307 static int register_uprobe_event(struct trace_uprobe *tu)
1308 {
1309 	struct trace_event_call *call = &tu->tp.call;
1310 	int ret = 0;
1311 
1312 	init_trace_event_call(tu, call);
1313 
1314 	if (traceprobe_set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0)
1315 		return -ENOMEM;
1316 
1317 	ret = register_trace_event(&call->event);
1318 	if (!ret) {
1319 		kfree(call->print_fmt);
1320 		return -ENODEV;
1321 	}
1322 
1323 	ret = trace_add_event_call(call);
1324 
1325 	if (ret) {
1326 		pr_info("Failed to register uprobe event: %s\n",
1327 			trace_event_name(call));
1328 		kfree(call->print_fmt);
1329 		unregister_trace_event(&call->event);
1330 	}
1331 
1332 	return ret;
1333 }
1334 
1335 static int unregister_uprobe_event(struct trace_uprobe *tu)
1336 {
1337 	int ret;
1338 
1339 	/* tu->event is unregistered in trace_remove_event_call() */
1340 	ret = trace_remove_event_call(&tu->tp.call);
1341 	if (ret)
1342 		return ret;
1343 	kfree(tu->tp.call.print_fmt);
1344 	tu->tp.call.print_fmt = NULL;
1345 	return 0;
1346 }
1347 
1348 #ifdef CONFIG_PERF_EVENTS
1349 struct trace_event_call *
1350 create_local_trace_uprobe(char *name, unsigned long offs,
1351 			  unsigned long ref_ctr_offset, bool is_return)
1352 {
1353 	struct trace_uprobe *tu;
1354 	struct path path;
1355 	int ret;
1356 
1357 	ret = kern_path(name, LOOKUP_FOLLOW, &path);
1358 	if (ret)
1359 		return ERR_PTR(ret);
1360 
1361 	if (!d_is_reg(path.dentry)) {
1362 		path_put(&path);
1363 		return ERR_PTR(-EINVAL);
1364 	}
1365 
1366 	/*
1367 	 * local trace_kprobes are not added to dyn_event, so they are never
1368 	 * searched in find_trace_kprobe(). Therefore, there is no concern of
1369 	 * duplicated name "DUMMY_EVENT" here.
1370 	 */
1371 	tu = alloc_trace_uprobe(UPROBE_EVENT_SYSTEM, "DUMMY_EVENT", 0,
1372 				is_return);
1373 
1374 	if (IS_ERR(tu)) {
1375 		pr_info("Failed to allocate trace_uprobe.(%d)\n",
1376 			(int)PTR_ERR(tu));
1377 		path_put(&path);
1378 		return ERR_CAST(tu);
1379 	}
1380 
1381 	tu->offset = offs;
1382 	tu->path = path;
1383 	tu->ref_ctr_offset = ref_ctr_offset;
1384 	tu->filename = kstrdup(name, GFP_KERNEL);
1385 	init_trace_event_call(tu, &tu->tp.call);
1386 
1387 	if (traceprobe_set_print_fmt(&tu->tp, is_ret_probe(tu)) < 0) {
1388 		ret = -ENOMEM;
1389 		goto error;
1390 	}
1391 
1392 	return &tu->tp.call;
1393 error:
1394 	free_trace_uprobe(tu);
1395 	return ERR_PTR(ret);
1396 }
1397 
1398 void destroy_local_trace_uprobe(struct trace_event_call *event_call)
1399 {
1400 	struct trace_uprobe *tu;
1401 
1402 	tu = container_of(event_call, struct trace_uprobe, tp.call);
1403 
1404 	kfree(tu->tp.call.print_fmt);
1405 	tu->tp.call.print_fmt = NULL;
1406 
1407 	free_trace_uprobe(tu);
1408 }
1409 #endif /* CONFIG_PERF_EVENTS */
1410 
1411 /* Make a trace interface for controling probe points */
1412 static __init int init_uprobe_trace(void)
1413 {
1414 	struct dentry *d_tracer;
1415 	int ret;
1416 
1417 	ret = dyn_event_register(&trace_uprobe_ops);
1418 	if (ret)
1419 		return ret;
1420 
1421 	d_tracer = tracing_init_dentry();
1422 	if (IS_ERR(d_tracer))
1423 		return 0;
1424 
1425 	trace_create_file("uprobe_events", 0644, d_tracer,
1426 				    NULL, &uprobe_events_ops);
1427 	/* Profile interface */
1428 	trace_create_file("uprobe_profile", 0444, d_tracer,
1429 				    NULL, &uprobe_profile_ops);
1430 	return 0;
1431 }
1432 
1433 fs_initcall(init_uprobe_trace);
1434