xref: /linux/kernel/trace/trace_kprobe.c (revision b9b77222d4ff6b5bb8f5d87fca20de0910618bb9)
1 /*
2  * Kprobes-based tracing events
3  *
4  * Created by Masami Hiramatsu <mhiramat@redhat.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19 #define pr_fmt(fmt)	"trace_kprobe: " fmt
20 
21 #include <linux/module.h>
22 #include <linux/uaccess.h>
23 #include <linux/rculist.h>
24 #include <linux/error-injection.h>
25 
26 #include "trace_probe.h"
27 
28 #define KPROBE_EVENT_SYSTEM "kprobes"
29 #define KRETPROBE_MAXACTIVE_MAX 4096
30 
31 /**
32  * Kprobe event core functions
33  */
34 struct trace_kprobe {
35 	struct list_head	list;
36 	struct kretprobe	rp;	/* Use rp.kp for kprobe use */
37 	unsigned long __percpu *nhit;
38 	const char		*symbol;	/* symbol name */
39 	struct trace_probe	tp;
40 };
41 
42 #define SIZEOF_TRACE_KPROBE(n)				\
43 	(offsetof(struct trace_kprobe, tp.args) +	\
44 	(sizeof(struct probe_arg) * (n)))
45 
46 static nokprobe_inline bool trace_kprobe_is_return(struct trace_kprobe *tk)
47 {
48 	return tk->rp.handler != NULL;
49 }
50 
51 static nokprobe_inline const char *trace_kprobe_symbol(struct trace_kprobe *tk)
52 {
53 	return tk->symbol ? tk->symbol : "unknown";
54 }
55 
56 static nokprobe_inline unsigned long trace_kprobe_offset(struct trace_kprobe *tk)
57 {
58 	return tk->rp.kp.offset;
59 }
60 
61 static nokprobe_inline bool trace_kprobe_has_gone(struct trace_kprobe *tk)
62 {
63 	return !!(kprobe_gone(&tk->rp.kp));
64 }
65 
66 static nokprobe_inline bool trace_kprobe_within_module(struct trace_kprobe *tk,
67 						 struct module *mod)
68 {
69 	int len = strlen(mod->name);
70 	const char *name = trace_kprobe_symbol(tk);
71 	return strncmp(mod->name, name, len) == 0 && name[len] == ':';
72 }
73 
74 static nokprobe_inline bool trace_kprobe_is_on_module(struct trace_kprobe *tk)
75 {
76 	return !!strchr(trace_kprobe_symbol(tk), ':');
77 }
78 
79 static nokprobe_inline unsigned long trace_kprobe_nhit(struct trace_kprobe *tk)
80 {
81 	unsigned long nhit = 0;
82 	int cpu;
83 
84 	for_each_possible_cpu(cpu)
85 		nhit += *per_cpu_ptr(tk->nhit, cpu);
86 
87 	return nhit;
88 }
89 
90 bool trace_kprobe_on_func_entry(struct trace_event_call *call)
91 {
92 	struct trace_kprobe *tk = (struct trace_kprobe *)call->data;
93 
94 	return kprobe_on_func_entry(tk->rp.kp.addr,
95 			tk->rp.kp.addr ? NULL : tk->rp.kp.symbol_name,
96 			tk->rp.kp.addr ? 0 : tk->rp.kp.offset);
97 }
98 
99 bool trace_kprobe_error_injectable(struct trace_event_call *call)
100 {
101 	struct trace_kprobe *tk = (struct trace_kprobe *)call->data;
102 	unsigned long addr;
103 
104 	if (tk->symbol) {
105 		addr = (unsigned long)
106 			kallsyms_lookup_name(trace_kprobe_symbol(tk));
107 		addr += tk->rp.kp.offset;
108 	} else {
109 		addr = (unsigned long)tk->rp.kp.addr;
110 	}
111 	return within_error_injection_list(addr);
112 }
113 
114 static int register_kprobe_event(struct trace_kprobe *tk);
115 static int unregister_kprobe_event(struct trace_kprobe *tk);
116 
117 static DEFINE_MUTEX(probe_lock);
118 static LIST_HEAD(probe_list);
119 
120 static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
121 static int kretprobe_dispatcher(struct kretprobe_instance *ri,
122 				struct pt_regs *regs);
123 
124 /* Memory fetching by symbol */
125 struct symbol_cache {
126 	char		*symbol;
127 	long		offset;
128 	unsigned long	addr;
129 };
130 
131 unsigned long update_symbol_cache(struct symbol_cache *sc)
132 {
133 	sc->addr = (unsigned long)kallsyms_lookup_name(sc->symbol);
134 
135 	if (sc->addr)
136 		sc->addr += sc->offset;
137 
138 	return sc->addr;
139 }
140 
141 void free_symbol_cache(struct symbol_cache *sc)
142 {
143 	kfree(sc->symbol);
144 	kfree(sc);
145 }
146 
147 struct symbol_cache *alloc_symbol_cache(const char *sym, long offset)
148 {
149 	struct symbol_cache *sc;
150 
151 	if (!sym || strlen(sym) == 0)
152 		return NULL;
153 
154 	sc = kzalloc(sizeof(struct symbol_cache), GFP_KERNEL);
155 	if (!sc)
156 		return NULL;
157 
158 	sc->symbol = kstrdup(sym, GFP_KERNEL);
159 	if (!sc->symbol) {
160 		kfree(sc);
161 		return NULL;
162 	}
163 	sc->offset = offset;
164 	update_symbol_cache(sc);
165 
166 	return sc;
167 }
168 
169 /*
170  * Kprobes-specific fetch functions
171  */
172 #define DEFINE_FETCH_stack(type)					\
173 static void FETCH_FUNC_NAME(stack, type)(struct pt_regs *regs,		\
174 					  void *offset, void *dest)	\
175 {									\
176 	*(type *)dest = (type)regs_get_kernel_stack_nth(regs,		\
177 				(unsigned int)((unsigned long)offset));	\
178 }									\
179 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(stack, type));
180 
181 DEFINE_BASIC_FETCH_FUNCS(stack)
182 /* No string on the stack entry */
183 #define fetch_stack_string	NULL
184 #define fetch_stack_string_size	NULL
185 
186 #define DEFINE_FETCH_memory(type)					\
187 static void FETCH_FUNC_NAME(memory, type)(struct pt_regs *regs,		\
188 					  void *addr, void *dest)	\
189 {									\
190 	type retval;							\
191 	if (probe_kernel_address(addr, retval))				\
192 		*(type *)dest = 0;					\
193 	else								\
194 		*(type *)dest = retval;					\
195 }									\
196 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(memory, type));
197 
198 DEFINE_BASIC_FETCH_FUNCS(memory)
199 /*
200  * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max
201  * length and relative data location.
202  */
203 static void FETCH_FUNC_NAME(memory, string)(struct pt_regs *regs,
204 					    void *addr, void *dest)
205 {
206 	int maxlen = get_rloc_len(*(u32 *)dest);
207 	u8 *dst = get_rloc_data(dest);
208 	long ret;
209 
210 	if (!maxlen)
211 		return;
212 
213 	/*
214 	 * Try to get string again, since the string can be changed while
215 	 * probing.
216 	 */
217 	ret = strncpy_from_unsafe(dst, addr, maxlen);
218 
219 	if (ret < 0) {	/* Failed to fetch string */
220 		dst[0] = '\0';
221 		*(u32 *)dest = make_data_rloc(0, get_rloc_offs(*(u32 *)dest));
222 	} else {
223 		*(u32 *)dest = make_data_rloc(ret, get_rloc_offs(*(u32 *)dest));
224 	}
225 }
226 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(memory, string));
227 
228 /* Return the length of string -- including null terminal byte */
229 static void FETCH_FUNC_NAME(memory, string_size)(struct pt_regs *regs,
230 						 void *addr, void *dest)
231 {
232 	mm_segment_t old_fs;
233 	int ret, len = 0;
234 	u8 c;
235 
236 	old_fs = get_fs();
237 	set_fs(KERNEL_DS);
238 	pagefault_disable();
239 
240 	do {
241 		ret = __copy_from_user_inatomic(&c, (u8 *)addr + len, 1);
242 		len++;
243 	} while (c && ret == 0 && len < MAX_STRING_SIZE);
244 
245 	pagefault_enable();
246 	set_fs(old_fs);
247 
248 	if (ret < 0)	/* Failed to check the length */
249 		*(u32 *)dest = 0;
250 	else
251 		*(u32 *)dest = len;
252 }
253 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(memory, string_size));
254 
255 #define DEFINE_FETCH_symbol(type)					\
256 void FETCH_FUNC_NAME(symbol, type)(struct pt_regs *regs, void *data, void *dest)\
257 {									\
258 	struct symbol_cache *sc = data;					\
259 	if (sc->addr)							\
260 		fetch_memory_##type(regs, (void *)sc->addr, dest);	\
261 	else								\
262 		*(type *)dest = 0;					\
263 }									\
264 NOKPROBE_SYMBOL(FETCH_FUNC_NAME(symbol, type));
265 
266 DEFINE_BASIC_FETCH_FUNCS(symbol)
267 DEFINE_FETCH_symbol(string)
268 DEFINE_FETCH_symbol(string_size)
269 
270 /* kprobes don't support file_offset fetch methods */
271 #define fetch_file_offset_u8		NULL
272 #define fetch_file_offset_u16		NULL
273 #define fetch_file_offset_u32		NULL
274 #define fetch_file_offset_u64		NULL
275 #define fetch_file_offset_string	NULL
276 #define fetch_file_offset_string_size	NULL
277 
278 /* Fetch type information table */
279 static const struct fetch_type kprobes_fetch_type_table[] = {
280 	/* Special types */
281 	[FETCH_TYPE_STRING] = __ASSIGN_FETCH_TYPE("string", string, string,
282 					sizeof(u32), 1, "__data_loc char[]"),
283 	[FETCH_TYPE_STRSIZE] = __ASSIGN_FETCH_TYPE("string_size", u32,
284 					string_size, sizeof(u32), 0, "u32"),
285 	/* Basic types */
286 	ASSIGN_FETCH_TYPE(u8,  u8,  0),
287 	ASSIGN_FETCH_TYPE(u16, u16, 0),
288 	ASSIGN_FETCH_TYPE(u32, u32, 0),
289 	ASSIGN_FETCH_TYPE(u64, u64, 0),
290 	ASSIGN_FETCH_TYPE(s8,  u8,  1),
291 	ASSIGN_FETCH_TYPE(s16, u16, 1),
292 	ASSIGN_FETCH_TYPE(s32, u32, 1),
293 	ASSIGN_FETCH_TYPE(s64, u64, 1),
294 	ASSIGN_FETCH_TYPE_ALIAS(x8,  u8,  u8,  0),
295 	ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0),
296 	ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0),
297 	ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0),
298 
299 	ASSIGN_FETCH_TYPE_END
300 };
301 
302 /*
303  * Allocate new trace_probe and initialize it (including kprobes).
304  */
305 static struct trace_kprobe *alloc_trace_kprobe(const char *group,
306 					     const char *event,
307 					     void *addr,
308 					     const char *symbol,
309 					     unsigned long offs,
310 					     int maxactive,
311 					     int nargs, bool is_return)
312 {
313 	struct trace_kprobe *tk;
314 	int ret = -ENOMEM;
315 
316 	tk = kzalloc(SIZEOF_TRACE_KPROBE(nargs), GFP_KERNEL);
317 	if (!tk)
318 		return ERR_PTR(ret);
319 
320 	tk->nhit = alloc_percpu(unsigned long);
321 	if (!tk->nhit)
322 		goto error;
323 
324 	if (symbol) {
325 		tk->symbol = kstrdup(symbol, GFP_KERNEL);
326 		if (!tk->symbol)
327 			goto error;
328 		tk->rp.kp.symbol_name = tk->symbol;
329 		tk->rp.kp.offset = offs;
330 	} else
331 		tk->rp.kp.addr = addr;
332 
333 	if (is_return)
334 		tk->rp.handler = kretprobe_dispatcher;
335 	else
336 		tk->rp.kp.pre_handler = kprobe_dispatcher;
337 
338 	tk->rp.maxactive = maxactive;
339 
340 	if (!event || !is_good_name(event)) {
341 		ret = -EINVAL;
342 		goto error;
343 	}
344 
345 	tk->tp.call.class = &tk->tp.class;
346 	tk->tp.call.name = kstrdup(event, GFP_KERNEL);
347 	if (!tk->tp.call.name)
348 		goto error;
349 
350 	if (!group || !is_good_name(group)) {
351 		ret = -EINVAL;
352 		goto error;
353 	}
354 
355 	tk->tp.class.system = kstrdup(group, GFP_KERNEL);
356 	if (!tk->tp.class.system)
357 		goto error;
358 
359 	INIT_LIST_HEAD(&tk->list);
360 	INIT_LIST_HEAD(&tk->tp.files);
361 	return tk;
362 error:
363 	kfree(tk->tp.call.name);
364 	kfree(tk->symbol);
365 	free_percpu(tk->nhit);
366 	kfree(tk);
367 	return ERR_PTR(ret);
368 }
369 
370 static void free_trace_kprobe(struct trace_kprobe *tk)
371 {
372 	int i;
373 
374 	for (i = 0; i < tk->tp.nr_args; i++)
375 		traceprobe_free_probe_arg(&tk->tp.args[i]);
376 
377 	kfree(tk->tp.call.class->system);
378 	kfree(tk->tp.call.name);
379 	kfree(tk->symbol);
380 	free_percpu(tk->nhit);
381 	kfree(tk);
382 }
383 
384 static struct trace_kprobe *find_trace_kprobe(const char *event,
385 					      const char *group)
386 {
387 	struct trace_kprobe *tk;
388 
389 	list_for_each_entry(tk, &probe_list, list)
390 		if (strcmp(trace_event_name(&tk->tp.call), event) == 0 &&
391 		    strcmp(tk->tp.call.class->system, group) == 0)
392 			return tk;
393 	return NULL;
394 }
395 
396 /*
397  * Enable trace_probe
398  * if the file is NULL, enable "perf" handler, or enable "trace" handler.
399  */
400 static int
401 enable_trace_kprobe(struct trace_kprobe *tk, struct trace_event_file *file)
402 {
403 	struct event_file_link *link = NULL;
404 	int ret = 0;
405 
406 	if (file) {
407 		link = kmalloc(sizeof(*link), GFP_KERNEL);
408 		if (!link) {
409 			ret = -ENOMEM;
410 			goto out;
411 		}
412 
413 		link->file = file;
414 		list_add_tail_rcu(&link->list, &tk->tp.files);
415 
416 		tk->tp.flags |= TP_FLAG_TRACE;
417 	} else
418 		tk->tp.flags |= TP_FLAG_PROFILE;
419 
420 	if (trace_probe_is_registered(&tk->tp) && !trace_kprobe_has_gone(tk)) {
421 		if (trace_kprobe_is_return(tk))
422 			ret = enable_kretprobe(&tk->rp);
423 		else
424 			ret = enable_kprobe(&tk->rp.kp);
425 	}
426 
427 	if (ret) {
428 		if (file) {
429 			/* Notice the if is true on not WARN() */
430 			if (!WARN_ON_ONCE(!link))
431 				list_del_rcu(&link->list);
432 			kfree(link);
433 			tk->tp.flags &= ~TP_FLAG_TRACE;
434 		} else {
435 			tk->tp.flags &= ~TP_FLAG_PROFILE;
436 		}
437 	}
438  out:
439 	return ret;
440 }
441 
442 /*
443  * Disable trace_probe
444  * if the file is NULL, disable "perf" handler, or disable "trace" handler.
445  */
446 static int
447 disable_trace_kprobe(struct trace_kprobe *tk, struct trace_event_file *file)
448 {
449 	struct event_file_link *link = NULL;
450 	int wait = 0;
451 	int ret = 0;
452 
453 	if (file) {
454 		link = find_event_file_link(&tk->tp, file);
455 		if (!link) {
456 			ret = -EINVAL;
457 			goto out;
458 		}
459 
460 		list_del_rcu(&link->list);
461 		wait = 1;
462 		if (!list_empty(&tk->tp.files))
463 			goto out;
464 
465 		tk->tp.flags &= ~TP_FLAG_TRACE;
466 	} else
467 		tk->tp.flags &= ~TP_FLAG_PROFILE;
468 
469 	if (!trace_probe_is_enabled(&tk->tp) && trace_probe_is_registered(&tk->tp)) {
470 		if (trace_kprobe_is_return(tk))
471 			disable_kretprobe(&tk->rp);
472 		else
473 			disable_kprobe(&tk->rp.kp);
474 		wait = 1;
475 	}
476 
477 	/*
478 	 * if tk is not added to any list, it must be a local trace_kprobe
479 	 * created with perf_event_open. We don't need to wait for these
480 	 * trace_kprobes
481 	 */
482 	if (list_empty(&tk->list))
483 		wait = 0;
484  out:
485 	if (wait) {
486 		/*
487 		 * Synchronize with kprobe_trace_func/kretprobe_trace_func
488 		 * to ensure disabled (all running handlers are finished).
489 		 * This is not only for kfree(), but also the caller,
490 		 * trace_remove_event_call() supposes it for releasing
491 		 * event_call related objects, which will be accessed in
492 		 * the kprobe_trace_func/kretprobe_trace_func.
493 		 */
494 		synchronize_sched();
495 		kfree(link);	/* Ignored if link == NULL */
496 	}
497 
498 	return ret;
499 }
500 
501 /* Internal register function - just handle k*probes and flags */
502 static int __register_trace_kprobe(struct trace_kprobe *tk)
503 {
504 	int i, ret;
505 
506 	if (trace_probe_is_registered(&tk->tp))
507 		return -EINVAL;
508 
509 	for (i = 0; i < tk->tp.nr_args; i++)
510 		traceprobe_update_arg(&tk->tp.args[i]);
511 
512 	/* Set/clear disabled flag according to tp->flag */
513 	if (trace_probe_is_enabled(&tk->tp))
514 		tk->rp.kp.flags &= ~KPROBE_FLAG_DISABLED;
515 	else
516 		tk->rp.kp.flags |= KPROBE_FLAG_DISABLED;
517 
518 	if (trace_kprobe_is_return(tk))
519 		ret = register_kretprobe(&tk->rp);
520 	else
521 		ret = register_kprobe(&tk->rp.kp);
522 
523 	if (ret == 0)
524 		tk->tp.flags |= TP_FLAG_REGISTERED;
525 	else {
526 		if (ret == -ENOENT && trace_kprobe_is_on_module(tk)) {
527 			pr_warn("This probe might be able to register after target module is loaded. Continue.\n");
528 			ret = 0;
529 		} else if (ret == -EILSEQ) {
530 			pr_warn("Probing address(0x%p) is not an instruction boundary.\n",
531 				tk->rp.kp.addr);
532 			ret = -EINVAL;
533 		}
534 	}
535 
536 	return ret;
537 }
538 
539 /* Internal unregister function - just handle k*probes and flags */
540 static void __unregister_trace_kprobe(struct trace_kprobe *tk)
541 {
542 	if (trace_probe_is_registered(&tk->tp)) {
543 		if (trace_kprobe_is_return(tk))
544 			unregister_kretprobe(&tk->rp);
545 		else
546 			unregister_kprobe(&tk->rp.kp);
547 		tk->tp.flags &= ~TP_FLAG_REGISTERED;
548 		/* Cleanup kprobe for reuse */
549 		if (tk->rp.kp.symbol_name)
550 			tk->rp.kp.addr = NULL;
551 	}
552 }
553 
554 /* Unregister a trace_probe and probe_event: call with locking probe_lock */
555 static int unregister_trace_kprobe(struct trace_kprobe *tk)
556 {
557 	/* Enabled event can not be unregistered */
558 	if (trace_probe_is_enabled(&tk->tp))
559 		return -EBUSY;
560 
561 	/* Will fail if probe is being used by ftrace or perf */
562 	if (unregister_kprobe_event(tk))
563 		return -EBUSY;
564 
565 	__unregister_trace_kprobe(tk);
566 	list_del(&tk->list);
567 
568 	return 0;
569 }
570 
571 /* Register a trace_probe and probe_event */
572 static int register_trace_kprobe(struct trace_kprobe *tk)
573 {
574 	struct trace_kprobe *old_tk;
575 	int ret;
576 
577 	mutex_lock(&probe_lock);
578 
579 	/* Delete old (same name) event if exist */
580 	old_tk = find_trace_kprobe(trace_event_name(&tk->tp.call),
581 			tk->tp.call.class->system);
582 	if (old_tk) {
583 		ret = unregister_trace_kprobe(old_tk);
584 		if (ret < 0)
585 			goto end;
586 		free_trace_kprobe(old_tk);
587 	}
588 
589 	/* Register new event */
590 	ret = register_kprobe_event(tk);
591 	if (ret) {
592 		pr_warn("Failed to register probe event(%d)\n", ret);
593 		goto end;
594 	}
595 
596 	/* Register k*probe */
597 	ret = __register_trace_kprobe(tk);
598 	if (ret < 0)
599 		unregister_kprobe_event(tk);
600 	else
601 		list_add_tail(&tk->list, &probe_list);
602 
603 end:
604 	mutex_unlock(&probe_lock);
605 	return ret;
606 }
607 
608 /* Module notifier call back, checking event on the module */
609 static int trace_kprobe_module_callback(struct notifier_block *nb,
610 				       unsigned long val, void *data)
611 {
612 	struct module *mod = data;
613 	struct trace_kprobe *tk;
614 	int ret;
615 
616 	if (val != MODULE_STATE_COMING)
617 		return NOTIFY_DONE;
618 
619 	/* Update probes on coming module */
620 	mutex_lock(&probe_lock);
621 	list_for_each_entry(tk, &probe_list, list) {
622 		if (trace_kprobe_within_module(tk, mod)) {
623 			/* Don't need to check busy - this should have gone. */
624 			__unregister_trace_kprobe(tk);
625 			ret = __register_trace_kprobe(tk);
626 			if (ret)
627 				pr_warn("Failed to re-register probe %s on %s: %d\n",
628 					trace_event_name(&tk->tp.call),
629 					mod->name, ret);
630 		}
631 	}
632 	mutex_unlock(&probe_lock);
633 
634 	return NOTIFY_DONE;
635 }
636 
637 static struct notifier_block trace_kprobe_module_nb = {
638 	.notifier_call = trace_kprobe_module_callback,
639 	.priority = 1	/* Invoked after kprobe module callback */
640 };
641 
642 /* Convert certain expected symbols into '_' when generating event names */
643 static inline void sanitize_event_name(char *name)
644 {
645 	while (*name++ != '\0')
646 		if (*name == ':' || *name == '.')
647 			*name = '_';
648 }
649 
650 static int create_trace_kprobe(int argc, char **argv)
651 {
652 	/*
653 	 * Argument syntax:
654 	 *  - Add kprobe:
655 	 *      p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS]
656 	 *  - Add kretprobe:
657 	 *      r[MAXACTIVE][:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS]
658 	 * Fetch args:
659 	 *  $retval	: fetch return value
660 	 *  $stack	: fetch stack address
661 	 *  $stackN	: fetch Nth of stack (N:0-)
662 	 *  $comm       : fetch current task comm
663 	 *  @ADDR	: fetch memory at ADDR (ADDR should be in kernel)
664 	 *  @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
665 	 *  %REG	: fetch register REG
666 	 * Dereferencing memory fetch:
667 	 *  +|-offs(ARG) : fetch memory at ARG +|- offs address.
668 	 * Alias name of args:
669 	 *  NAME=FETCHARG : set NAME as alias of FETCHARG.
670 	 * Type of args:
671 	 *  FETCHARG:TYPE : use TYPE instead of unsigned long.
672 	 */
673 	struct trace_kprobe *tk;
674 	int i, ret = 0;
675 	bool is_return = false, is_delete = false;
676 	char *symbol = NULL, *event = NULL, *group = NULL;
677 	int maxactive = 0;
678 	char *arg;
679 	long offset = 0;
680 	void *addr = NULL;
681 	char buf[MAX_EVENT_NAME_LEN];
682 
683 	/* argc must be >= 1 */
684 	if (argv[0][0] == 'p')
685 		is_return = false;
686 	else if (argv[0][0] == 'r')
687 		is_return = true;
688 	else if (argv[0][0] == '-')
689 		is_delete = true;
690 	else {
691 		pr_info("Probe definition must be started with 'p', 'r' or"
692 			" '-'.\n");
693 		return -EINVAL;
694 	}
695 
696 	event = strchr(&argv[0][1], ':');
697 	if (event) {
698 		event[0] = '\0';
699 		event++;
700 	}
701 	if (is_return && isdigit(argv[0][1])) {
702 		ret = kstrtouint(&argv[0][1], 0, &maxactive);
703 		if (ret) {
704 			pr_info("Failed to parse maxactive.\n");
705 			return ret;
706 		}
707 		/* kretprobes instances are iterated over via a list. The
708 		 * maximum should stay reasonable.
709 		 */
710 		if (maxactive > KRETPROBE_MAXACTIVE_MAX) {
711 			pr_info("Maxactive is too big (%d > %d).\n",
712 				maxactive, KRETPROBE_MAXACTIVE_MAX);
713 			return -E2BIG;
714 		}
715 	}
716 
717 	if (event) {
718 		if (strchr(event, '/')) {
719 			group = event;
720 			event = strchr(group, '/') + 1;
721 			event[-1] = '\0';
722 			if (strlen(group) == 0) {
723 				pr_info("Group name is not specified\n");
724 				return -EINVAL;
725 			}
726 		}
727 		if (strlen(event) == 0) {
728 			pr_info("Event name is not specified\n");
729 			return -EINVAL;
730 		}
731 	}
732 	if (!group)
733 		group = KPROBE_EVENT_SYSTEM;
734 
735 	if (is_delete) {
736 		if (!event) {
737 			pr_info("Delete command needs an event name.\n");
738 			return -EINVAL;
739 		}
740 		mutex_lock(&probe_lock);
741 		tk = find_trace_kprobe(event, group);
742 		if (!tk) {
743 			mutex_unlock(&probe_lock);
744 			pr_info("Event %s/%s doesn't exist.\n", group, event);
745 			return -ENOENT;
746 		}
747 		/* delete an event */
748 		ret = unregister_trace_kprobe(tk);
749 		if (ret == 0)
750 			free_trace_kprobe(tk);
751 		mutex_unlock(&probe_lock);
752 		return ret;
753 	}
754 
755 	if (argc < 2) {
756 		pr_info("Probe point is not specified.\n");
757 		return -EINVAL;
758 	}
759 
760 	/* try to parse an address. if that fails, try to read the
761 	 * input as a symbol. */
762 	if (kstrtoul(argv[1], 0, (unsigned long *)&addr)) {
763 		/* a symbol specified */
764 		symbol = argv[1];
765 		/* TODO: support .init module functions */
766 		ret = traceprobe_split_symbol_offset(symbol, &offset);
767 		if (ret || offset < 0 || offset > UINT_MAX) {
768 			pr_info("Failed to parse either an address or a symbol.\n");
769 			return ret;
770 		}
771 		if (offset && is_return &&
772 		    !kprobe_on_func_entry(NULL, symbol, offset)) {
773 			pr_info("Given offset is not valid for return probe.\n");
774 			return -EINVAL;
775 		}
776 	}
777 	argc -= 2; argv += 2;
778 
779 	/* setup a probe */
780 	if (!event) {
781 		/* Make a new event name */
782 		if (symbol)
783 			snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
784 				 is_return ? 'r' : 'p', symbol, offset);
785 		else
786 			snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
787 				 is_return ? 'r' : 'p', addr);
788 		sanitize_event_name(buf);
789 		event = buf;
790 	}
791 	tk = alloc_trace_kprobe(group, event, addr, symbol, offset, maxactive,
792 			       argc, is_return);
793 	if (IS_ERR(tk)) {
794 		pr_info("Failed to allocate trace_probe.(%d)\n",
795 			(int)PTR_ERR(tk));
796 		return PTR_ERR(tk);
797 	}
798 
799 	/* parse arguments */
800 	ret = 0;
801 	for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
802 		struct probe_arg *parg = &tk->tp.args[i];
803 
804 		/* Increment count for freeing args in error case */
805 		tk->tp.nr_args++;
806 
807 		/* Parse argument name */
808 		arg = strchr(argv[i], '=');
809 		if (arg) {
810 			*arg++ = '\0';
811 			parg->name = kstrdup(argv[i], GFP_KERNEL);
812 		} else {
813 			arg = argv[i];
814 			/* If argument name is omitted, set "argN" */
815 			snprintf(buf, MAX_EVENT_NAME_LEN, "arg%d", i + 1);
816 			parg->name = kstrdup(buf, GFP_KERNEL);
817 		}
818 
819 		if (!parg->name) {
820 			pr_info("Failed to allocate argument[%d] name.\n", i);
821 			ret = -ENOMEM;
822 			goto error;
823 		}
824 
825 		if (!is_good_name(parg->name)) {
826 			pr_info("Invalid argument[%d] name: %s\n",
827 				i, parg->name);
828 			ret = -EINVAL;
829 			goto error;
830 		}
831 
832 		if (traceprobe_conflict_field_name(parg->name,
833 							tk->tp.args, i)) {
834 			pr_info("Argument[%d] name '%s' conflicts with "
835 				"another field.\n", i, argv[i]);
836 			ret = -EINVAL;
837 			goto error;
838 		}
839 
840 		/* Parse fetch argument */
841 		ret = traceprobe_parse_probe_arg(arg, &tk->tp.size, parg,
842 						is_return, true,
843 						kprobes_fetch_type_table);
844 		if (ret) {
845 			pr_info("Parse error at argument[%d]. (%d)\n", i, ret);
846 			goto error;
847 		}
848 	}
849 
850 	ret = register_trace_kprobe(tk);
851 	if (ret)
852 		goto error;
853 	return 0;
854 
855 error:
856 	free_trace_kprobe(tk);
857 	return ret;
858 }
859 
860 static int release_all_trace_kprobes(void)
861 {
862 	struct trace_kprobe *tk;
863 	int ret = 0;
864 
865 	mutex_lock(&probe_lock);
866 	/* Ensure no probe is in use. */
867 	list_for_each_entry(tk, &probe_list, list)
868 		if (trace_probe_is_enabled(&tk->tp)) {
869 			ret = -EBUSY;
870 			goto end;
871 		}
872 	/* TODO: Use batch unregistration */
873 	while (!list_empty(&probe_list)) {
874 		tk = list_entry(probe_list.next, struct trace_kprobe, list);
875 		ret = unregister_trace_kprobe(tk);
876 		if (ret)
877 			goto end;
878 		free_trace_kprobe(tk);
879 	}
880 
881 end:
882 	mutex_unlock(&probe_lock);
883 
884 	return ret;
885 }
886 
887 /* Probes listing interfaces */
888 static void *probes_seq_start(struct seq_file *m, loff_t *pos)
889 {
890 	mutex_lock(&probe_lock);
891 	return seq_list_start(&probe_list, *pos);
892 }
893 
894 static void *probes_seq_next(struct seq_file *m, void *v, loff_t *pos)
895 {
896 	return seq_list_next(v, &probe_list, pos);
897 }
898 
899 static void probes_seq_stop(struct seq_file *m, void *v)
900 {
901 	mutex_unlock(&probe_lock);
902 }
903 
904 static int probes_seq_show(struct seq_file *m, void *v)
905 {
906 	struct trace_kprobe *tk = v;
907 	int i;
908 
909 	seq_putc(m, trace_kprobe_is_return(tk) ? 'r' : 'p');
910 	seq_printf(m, ":%s/%s", tk->tp.call.class->system,
911 			trace_event_name(&tk->tp.call));
912 
913 	if (!tk->symbol)
914 		seq_printf(m, " 0x%p", tk->rp.kp.addr);
915 	else if (tk->rp.kp.offset)
916 		seq_printf(m, " %s+%u", trace_kprobe_symbol(tk),
917 			   tk->rp.kp.offset);
918 	else
919 		seq_printf(m, " %s", trace_kprobe_symbol(tk));
920 
921 	for (i = 0; i < tk->tp.nr_args; i++)
922 		seq_printf(m, " %s=%s", tk->tp.args[i].name, tk->tp.args[i].comm);
923 	seq_putc(m, '\n');
924 
925 	return 0;
926 }
927 
928 static const struct seq_operations probes_seq_op = {
929 	.start  = probes_seq_start,
930 	.next   = probes_seq_next,
931 	.stop   = probes_seq_stop,
932 	.show   = probes_seq_show
933 };
934 
935 static int probes_open(struct inode *inode, struct file *file)
936 {
937 	int ret;
938 
939 	if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
940 		ret = release_all_trace_kprobes();
941 		if (ret < 0)
942 			return ret;
943 	}
944 
945 	return seq_open(file, &probes_seq_op);
946 }
947 
948 static ssize_t probes_write(struct file *file, const char __user *buffer,
949 			    size_t count, loff_t *ppos)
950 {
951 	return trace_parse_run_command(file, buffer, count, ppos,
952 				       create_trace_kprobe);
953 }
954 
955 static const struct file_operations kprobe_events_ops = {
956 	.owner          = THIS_MODULE,
957 	.open           = probes_open,
958 	.read           = seq_read,
959 	.llseek         = seq_lseek,
960 	.release        = seq_release,
961 	.write		= probes_write,
962 };
963 
964 /* Probes profiling interfaces */
965 static int probes_profile_seq_show(struct seq_file *m, void *v)
966 {
967 	struct trace_kprobe *tk = v;
968 
969 	seq_printf(m, "  %-44s %15lu %15lu\n",
970 		   trace_event_name(&tk->tp.call),
971 		   trace_kprobe_nhit(tk),
972 		   tk->rp.kp.nmissed);
973 
974 	return 0;
975 }
976 
977 static const struct seq_operations profile_seq_op = {
978 	.start  = probes_seq_start,
979 	.next   = probes_seq_next,
980 	.stop   = probes_seq_stop,
981 	.show   = probes_profile_seq_show
982 };
983 
984 static int profile_open(struct inode *inode, struct file *file)
985 {
986 	return seq_open(file, &profile_seq_op);
987 }
988 
989 static const struct file_operations kprobe_profile_ops = {
990 	.owner          = THIS_MODULE,
991 	.open           = profile_open,
992 	.read           = seq_read,
993 	.llseek         = seq_lseek,
994 	.release        = seq_release,
995 };
996 
997 /* Kprobe handler */
998 static nokprobe_inline void
999 __kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs,
1000 		    struct trace_event_file *trace_file)
1001 {
1002 	struct kprobe_trace_entry_head *entry;
1003 	struct ring_buffer_event *event;
1004 	struct ring_buffer *buffer;
1005 	int size, dsize, pc;
1006 	unsigned long irq_flags;
1007 	struct trace_event_call *call = &tk->tp.call;
1008 
1009 	WARN_ON(call != trace_file->event_call);
1010 
1011 	if (trace_trigger_soft_disabled(trace_file))
1012 		return;
1013 
1014 	local_save_flags(irq_flags);
1015 	pc = preempt_count();
1016 
1017 	dsize = __get_data_size(&tk->tp, regs);
1018 	size = sizeof(*entry) + tk->tp.size + dsize;
1019 
1020 	event = trace_event_buffer_lock_reserve(&buffer, trace_file,
1021 						call->event.type,
1022 						size, irq_flags, pc);
1023 	if (!event)
1024 		return;
1025 
1026 	entry = ring_buffer_event_data(event);
1027 	entry->ip = (unsigned long)tk->rp.kp.addr;
1028 	store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
1029 
1030 	event_trigger_unlock_commit_regs(trace_file, buffer, event,
1031 					 entry, irq_flags, pc, regs);
1032 }
1033 
1034 static void
1035 kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs)
1036 {
1037 	struct event_file_link *link;
1038 
1039 	list_for_each_entry_rcu(link, &tk->tp.files, list)
1040 		__kprobe_trace_func(tk, regs, link->file);
1041 }
1042 NOKPROBE_SYMBOL(kprobe_trace_func);
1043 
1044 /* Kretprobe handler */
1045 static nokprobe_inline void
1046 __kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
1047 		       struct pt_regs *regs,
1048 		       struct trace_event_file *trace_file)
1049 {
1050 	struct kretprobe_trace_entry_head *entry;
1051 	struct ring_buffer_event *event;
1052 	struct ring_buffer *buffer;
1053 	int size, pc, dsize;
1054 	unsigned long irq_flags;
1055 	struct trace_event_call *call = &tk->tp.call;
1056 
1057 	WARN_ON(call != trace_file->event_call);
1058 
1059 	if (trace_trigger_soft_disabled(trace_file))
1060 		return;
1061 
1062 	local_save_flags(irq_flags);
1063 	pc = preempt_count();
1064 
1065 	dsize = __get_data_size(&tk->tp, regs);
1066 	size = sizeof(*entry) + tk->tp.size + dsize;
1067 
1068 	event = trace_event_buffer_lock_reserve(&buffer, trace_file,
1069 						call->event.type,
1070 						size, irq_flags, pc);
1071 	if (!event)
1072 		return;
1073 
1074 	entry = ring_buffer_event_data(event);
1075 	entry->func = (unsigned long)tk->rp.kp.addr;
1076 	entry->ret_ip = (unsigned long)ri->ret_addr;
1077 	store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
1078 
1079 	event_trigger_unlock_commit_regs(trace_file, buffer, event,
1080 					 entry, irq_flags, pc, regs);
1081 }
1082 
1083 static void
1084 kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
1085 		     struct pt_regs *regs)
1086 {
1087 	struct event_file_link *link;
1088 
1089 	list_for_each_entry_rcu(link, &tk->tp.files, list)
1090 		__kretprobe_trace_func(tk, ri, regs, link->file);
1091 }
1092 NOKPROBE_SYMBOL(kretprobe_trace_func);
1093 
1094 /* Event entry printers */
1095 static enum print_line_t
1096 print_kprobe_event(struct trace_iterator *iter, int flags,
1097 		   struct trace_event *event)
1098 {
1099 	struct kprobe_trace_entry_head *field;
1100 	struct trace_seq *s = &iter->seq;
1101 	struct trace_probe *tp;
1102 	u8 *data;
1103 	int i;
1104 
1105 	field = (struct kprobe_trace_entry_head *)iter->ent;
1106 	tp = container_of(event, struct trace_probe, call.event);
1107 
1108 	trace_seq_printf(s, "%s: (", trace_event_name(&tp->call));
1109 
1110 	if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
1111 		goto out;
1112 
1113 	trace_seq_putc(s, ')');
1114 
1115 	data = (u8 *)&field[1];
1116 	for (i = 0; i < tp->nr_args; i++)
1117 		if (!tp->args[i].type->print(s, tp->args[i].name,
1118 					     data + tp->args[i].offset, field))
1119 			goto out;
1120 
1121 	trace_seq_putc(s, '\n');
1122  out:
1123 	return trace_handle_return(s);
1124 }
1125 
1126 static enum print_line_t
1127 print_kretprobe_event(struct trace_iterator *iter, int flags,
1128 		      struct trace_event *event)
1129 {
1130 	struct kretprobe_trace_entry_head *field;
1131 	struct trace_seq *s = &iter->seq;
1132 	struct trace_probe *tp;
1133 	u8 *data;
1134 	int i;
1135 
1136 	field = (struct kretprobe_trace_entry_head *)iter->ent;
1137 	tp = container_of(event, struct trace_probe, call.event);
1138 
1139 	trace_seq_printf(s, "%s: (", trace_event_name(&tp->call));
1140 
1141 	if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
1142 		goto out;
1143 
1144 	trace_seq_puts(s, " <- ");
1145 
1146 	if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
1147 		goto out;
1148 
1149 	trace_seq_putc(s, ')');
1150 
1151 	data = (u8 *)&field[1];
1152 	for (i = 0; i < tp->nr_args; i++)
1153 		if (!tp->args[i].type->print(s, tp->args[i].name,
1154 					     data + tp->args[i].offset, field))
1155 			goto out;
1156 
1157 	trace_seq_putc(s, '\n');
1158 
1159  out:
1160 	return trace_handle_return(s);
1161 }
1162 
1163 
1164 static int kprobe_event_define_fields(struct trace_event_call *event_call)
1165 {
1166 	int ret, i;
1167 	struct kprobe_trace_entry_head field;
1168 	struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data;
1169 
1170 	DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
1171 	/* Set argument names as fields */
1172 	for (i = 0; i < tk->tp.nr_args; i++) {
1173 		struct probe_arg *parg = &tk->tp.args[i];
1174 
1175 		ret = trace_define_field(event_call, parg->type->fmttype,
1176 					 parg->name,
1177 					 sizeof(field) + parg->offset,
1178 					 parg->type->size,
1179 					 parg->type->is_signed,
1180 					 FILTER_OTHER);
1181 		if (ret)
1182 			return ret;
1183 	}
1184 	return 0;
1185 }
1186 
1187 static int kretprobe_event_define_fields(struct trace_event_call *event_call)
1188 {
1189 	int ret, i;
1190 	struct kretprobe_trace_entry_head field;
1191 	struct trace_kprobe *tk = (struct trace_kprobe *)event_call->data;
1192 
1193 	DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
1194 	DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
1195 	/* Set argument names as fields */
1196 	for (i = 0; i < tk->tp.nr_args; i++) {
1197 		struct probe_arg *parg = &tk->tp.args[i];
1198 
1199 		ret = trace_define_field(event_call, parg->type->fmttype,
1200 					 parg->name,
1201 					 sizeof(field) + parg->offset,
1202 					 parg->type->size,
1203 					 parg->type->is_signed,
1204 					 FILTER_OTHER);
1205 		if (ret)
1206 			return ret;
1207 	}
1208 	return 0;
1209 }
1210 
1211 #ifdef CONFIG_PERF_EVENTS
1212 
1213 /* Kprobe profile handler */
1214 static int
1215 kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs)
1216 {
1217 	struct trace_event_call *call = &tk->tp.call;
1218 	struct kprobe_trace_entry_head *entry;
1219 	struct hlist_head *head;
1220 	int size, __size, dsize;
1221 	int rctx;
1222 
1223 	if (bpf_prog_array_valid(call)) {
1224 		unsigned long orig_ip = instruction_pointer(regs);
1225 		int ret;
1226 
1227 		ret = trace_call_bpf(call, regs);
1228 
1229 		/*
1230 		 * We need to check and see if we modified the pc of the
1231 		 * pt_regs, and if so return 1 so that we don't do the
1232 		 * single stepping.
1233 		 */
1234 		if (orig_ip != instruction_pointer(regs))
1235 			return 1;
1236 		if (!ret)
1237 			return 0;
1238 	}
1239 
1240 	head = this_cpu_ptr(call->perf_events);
1241 	if (hlist_empty(head))
1242 		return 0;
1243 
1244 	dsize = __get_data_size(&tk->tp, regs);
1245 	__size = sizeof(*entry) + tk->tp.size + dsize;
1246 	size = ALIGN(__size + sizeof(u32), sizeof(u64));
1247 	size -= sizeof(u32);
1248 
1249 	entry = perf_trace_buf_alloc(size, NULL, &rctx);
1250 	if (!entry)
1251 		return 0;
1252 
1253 	entry->ip = (unsigned long)tk->rp.kp.addr;
1254 	memset(&entry[1], 0, dsize);
1255 	store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
1256 	perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1257 			      head, NULL);
1258 	return 0;
1259 }
1260 NOKPROBE_SYMBOL(kprobe_perf_func);
1261 
1262 /* Kretprobe profile handler */
1263 static void
1264 kretprobe_perf_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
1265 		    struct pt_regs *regs)
1266 {
1267 	struct trace_event_call *call = &tk->tp.call;
1268 	struct kretprobe_trace_entry_head *entry;
1269 	struct hlist_head *head;
1270 	int size, __size, dsize;
1271 	int rctx;
1272 
1273 	if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs))
1274 		return;
1275 
1276 	head = this_cpu_ptr(call->perf_events);
1277 	if (hlist_empty(head))
1278 		return;
1279 
1280 	dsize = __get_data_size(&tk->tp, regs);
1281 	__size = sizeof(*entry) + tk->tp.size + dsize;
1282 	size = ALIGN(__size + sizeof(u32), sizeof(u64));
1283 	size -= sizeof(u32);
1284 
1285 	entry = perf_trace_buf_alloc(size, NULL, &rctx);
1286 	if (!entry)
1287 		return;
1288 
1289 	entry->func = (unsigned long)tk->rp.kp.addr;
1290 	entry->ret_ip = (unsigned long)ri->ret_addr;
1291 	store_trace_args(sizeof(*entry), &tk->tp, regs, (u8 *)&entry[1], dsize);
1292 	perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1293 			      head, NULL);
1294 }
1295 NOKPROBE_SYMBOL(kretprobe_perf_func);
1296 
1297 int bpf_get_kprobe_info(const struct perf_event *event, u32 *fd_type,
1298 			const char **symbol, u64 *probe_offset,
1299 			u64 *probe_addr, bool perf_type_tracepoint)
1300 {
1301 	const char *pevent = trace_event_name(event->tp_event);
1302 	const char *group = event->tp_event->class->system;
1303 	struct trace_kprobe *tk;
1304 
1305 	if (perf_type_tracepoint)
1306 		tk = find_trace_kprobe(pevent, group);
1307 	else
1308 		tk = event->tp_event->data;
1309 	if (!tk)
1310 		return -EINVAL;
1311 
1312 	*fd_type = trace_kprobe_is_return(tk) ? BPF_FD_TYPE_KRETPROBE
1313 					      : BPF_FD_TYPE_KPROBE;
1314 	if (tk->symbol) {
1315 		*symbol = tk->symbol;
1316 		*probe_offset = tk->rp.kp.offset;
1317 		*probe_addr = 0;
1318 	} else {
1319 		*symbol = NULL;
1320 		*probe_offset = 0;
1321 		*probe_addr = (unsigned long)tk->rp.kp.addr;
1322 	}
1323 	return 0;
1324 }
1325 #endif	/* CONFIG_PERF_EVENTS */
1326 
1327 /*
1328  * called by perf_trace_init() or __ftrace_set_clr_event() under event_mutex.
1329  *
1330  * kprobe_trace_self_tests_init() does enable_trace_probe/disable_trace_probe
1331  * lockless, but we can't race with this __init function.
1332  */
1333 static int kprobe_register(struct trace_event_call *event,
1334 			   enum trace_reg type, void *data)
1335 {
1336 	struct trace_kprobe *tk = (struct trace_kprobe *)event->data;
1337 	struct trace_event_file *file = data;
1338 
1339 	switch (type) {
1340 	case TRACE_REG_REGISTER:
1341 		return enable_trace_kprobe(tk, file);
1342 	case TRACE_REG_UNREGISTER:
1343 		return disable_trace_kprobe(tk, file);
1344 
1345 #ifdef CONFIG_PERF_EVENTS
1346 	case TRACE_REG_PERF_REGISTER:
1347 		return enable_trace_kprobe(tk, NULL);
1348 	case TRACE_REG_PERF_UNREGISTER:
1349 		return disable_trace_kprobe(tk, NULL);
1350 	case TRACE_REG_PERF_OPEN:
1351 	case TRACE_REG_PERF_CLOSE:
1352 	case TRACE_REG_PERF_ADD:
1353 	case TRACE_REG_PERF_DEL:
1354 		return 0;
1355 #endif
1356 	}
1357 	return 0;
1358 }
1359 
1360 static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
1361 {
1362 	struct trace_kprobe *tk = container_of(kp, struct trace_kprobe, rp.kp);
1363 	int ret = 0;
1364 
1365 	raw_cpu_inc(*tk->nhit);
1366 
1367 	if (tk->tp.flags & TP_FLAG_TRACE)
1368 		kprobe_trace_func(tk, regs);
1369 #ifdef CONFIG_PERF_EVENTS
1370 	if (tk->tp.flags & TP_FLAG_PROFILE)
1371 		ret = kprobe_perf_func(tk, regs);
1372 #endif
1373 	return ret;
1374 }
1375 NOKPROBE_SYMBOL(kprobe_dispatcher);
1376 
1377 static int
1378 kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
1379 {
1380 	struct trace_kprobe *tk = container_of(ri->rp, struct trace_kprobe, rp);
1381 
1382 	raw_cpu_inc(*tk->nhit);
1383 
1384 	if (tk->tp.flags & TP_FLAG_TRACE)
1385 		kretprobe_trace_func(tk, ri, regs);
1386 #ifdef CONFIG_PERF_EVENTS
1387 	if (tk->tp.flags & TP_FLAG_PROFILE)
1388 		kretprobe_perf_func(tk, ri, regs);
1389 #endif
1390 	return 0;	/* We don't tweek kernel, so just return 0 */
1391 }
1392 NOKPROBE_SYMBOL(kretprobe_dispatcher);
1393 
1394 static struct trace_event_functions kretprobe_funcs = {
1395 	.trace		= print_kretprobe_event
1396 };
1397 
1398 static struct trace_event_functions kprobe_funcs = {
1399 	.trace		= print_kprobe_event
1400 };
1401 
1402 static inline void init_trace_event_call(struct trace_kprobe *tk,
1403 					 struct trace_event_call *call)
1404 {
1405 	INIT_LIST_HEAD(&call->class->fields);
1406 	if (trace_kprobe_is_return(tk)) {
1407 		call->event.funcs = &kretprobe_funcs;
1408 		call->class->define_fields = kretprobe_event_define_fields;
1409 	} else {
1410 		call->event.funcs = &kprobe_funcs;
1411 		call->class->define_fields = kprobe_event_define_fields;
1412 	}
1413 
1414 	call->flags = TRACE_EVENT_FL_KPROBE;
1415 	call->class->reg = kprobe_register;
1416 	call->data = tk;
1417 }
1418 
1419 static int register_kprobe_event(struct trace_kprobe *tk)
1420 {
1421 	struct trace_event_call *call = &tk->tp.call;
1422 	int ret = 0;
1423 
1424 	init_trace_event_call(tk, call);
1425 
1426 	if (set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0)
1427 		return -ENOMEM;
1428 	ret = register_trace_event(&call->event);
1429 	if (!ret) {
1430 		kfree(call->print_fmt);
1431 		return -ENODEV;
1432 	}
1433 	ret = trace_add_event_call(call);
1434 	if (ret) {
1435 		pr_info("Failed to register kprobe event: %s\n",
1436 			trace_event_name(call));
1437 		kfree(call->print_fmt);
1438 		unregister_trace_event(&call->event);
1439 	}
1440 	return ret;
1441 }
1442 
1443 static int unregister_kprobe_event(struct trace_kprobe *tk)
1444 {
1445 	int ret;
1446 
1447 	/* tp->event is unregistered in trace_remove_event_call() */
1448 	ret = trace_remove_event_call(&tk->tp.call);
1449 	if (!ret)
1450 		kfree(tk->tp.call.print_fmt);
1451 	return ret;
1452 }
1453 
1454 #ifdef CONFIG_PERF_EVENTS
1455 /* create a trace_kprobe, but don't add it to global lists */
1456 struct trace_event_call *
1457 create_local_trace_kprobe(char *func, void *addr, unsigned long offs,
1458 			  bool is_return)
1459 {
1460 	struct trace_kprobe *tk;
1461 	int ret;
1462 	char *event;
1463 
1464 	/*
1465 	 * local trace_kprobes are not added to probe_list, so they are never
1466 	 * searched in find_trace_kprobe(). Therefore, there is no concern of
1467 	 * duplicated name here.
1468 	 */
1469 	event = func ? func : "DUMMY_EVENT";
1470 
1471 	tk = alloc_trace_kprobe(KPROBE_EVENT_SYSTEM, event, (void *)addr, func,
1472 				offs, 0 /* maxactive */, 0 /* nargs */,
1473 				is_return);
1474 
1475 	if (IS_ERR(tk)) {
1476 		pr_info("Failed to allocate trace_probe.(%d)\n",
1477 			(int)PTR_ERR(tk));
1478 		return ERR_CAST(tk);
1479 	}
1480 
1481 	init_trace_event_call(tk, &tk->tp.call);
1482 
1483 	if (set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0) {
1484 		ret = -ENOMEM;
1485 		goto error;
1486 	}
1487 
1488 	ret = __register_trace_kprobe(tk);
1489 	if (ret < 0) {
1490 		kfree(tk->tp.call.print_fmt);
1491 		goto error;
1492 	}
1493 
1494 	return &tk->tp.call;
1495 error:
1496 	free_trace_kprobe(tk);
1497 	return ERR_PTR(ret);
1498 }
1499 
1500 void destroy_local_trace_kprobe(struct trace_event_call *event_call)
1501 {
1502 	struct trace_kprobe *tk;
1503 
1504 	tk = container_of(event_call, struct trace_kprobe, tp.call);
1505 
1506 	if (trace_probe_is_enabled(&tk->tp)) {
1507 		WARN_ON(1);
1508 		return;
1509 	}
1510 
1511 	__unregister_trace_kprobe(tk);
1512 
1513 	kfree(tk->tp.call.print_fmt);
1514 	free_trace_kprobe(tk);
1515 }
1516 #endif /* CONFIG_PERF_EVENTS */
1517 
1518 /* Make a tracefs interface for controlling probe points */
1519 static __init int init_kprobe_trace(void)
1520 {
1521 	struct dentry *d_tracer;
1522 	struct dentry *entry;
1523 
1524 	if (register_module_notifier(&trace_kprobe_module_nb))
1525 		return -EINVAL;
1526 
1527 	d_tracer = tracing_init_dentry();
1528 	if (IS_ERR(d_tracer))
1529 		return 0;
1530 
1531 	entry = tracefs_create_file("kprobe_events", 0644, d_tracer,
1532 				    NULL, &kprobe_events_ops);
1533 
1534 	/* Event list interface */
1535 	if (!entry)
1536 		pr_warn("Could not create tracefs 'kprobe_events' entry\n");
1537 
1538 	/* Profile interface */
1539 	entry = tracefs_create_file("kprobe_profile", 0444, d_tracer,
1540 				    NULL, &kprobe_profile_ops);
1541 
1542 	if (!entry)
1543 		pr_warn("Could not create tracefs 'kprobe_profile' entry\n");
1544 	return 0;
1545 }
1546 fs_initcall(init_kprobe_trace);
1547 
1548 
1549 #ifdef CONFIG_FTRACE_STARTUP_TEST
1550 /*
1551  * The "__used" keeps gcc from removing the function symbol
1552  * from the kallsyms table. 'noinline' makes sure that there
1553  * isn't an inlined version used by the test method below
1554  */
1555 static __used __init noinline int
1556 kprobe_trace_selftest_target(int a1, int a2, int a3, int a4, int a5, int a6)
1557 {
1558 	return a1 + a2 + a3 + a4 + a5 + a6;
1559 }
1560 
1561 static __init struct trace_event_file *
1562 find_trace_probe_file(struct trace_kprobe *tk, struct trace_array *tr)
1563 {
1564 	struct trace_event_file *file;
1565 
1566 	list_for_each_entry(file, &tr->events, list)
1567 		if (file->event_call == &tk->tp.call)
1568 			return file;
1569 
1570 	return NULL;
1571 }
1572 
1573 /*
1574  * Nobody but us can call enable_trace_kprobe/disable_trace_kprobe at this
1575  * stage, we can do this lockless.
1576  */
1577 static __init int kprobe_trace_self_tests_init(void)
1578 {
1579 	int ret, warn = 0;
1580 	int (*target)(int, int, int, int, int, int);
1581 	struct trace_kprobe *tk;
1582 	struct trace_event_file *file;
1583 
1584 	if (tracing_is_disabled())
1585 		return -ENODEV;
1586 
1587 	target = kprobe_trace_selftest_target;
1588 
1589 	pr_info("Testing kprobe tracing: ");
1590 
1591 	ret = trace_run_command("p:testprobe kprobe_trace_selftest_target "
1592 				"$stack $stack0 +0($stack)",
1593 				create_trace_kprobe);
1594 	if (WARN_ON_ONCE(ret)) {
1595 		pr_warn("error on probing function entry.\n");
1596 		warn++;
1597 	} else {
1598 		/* Enable trace point */
1599 		tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
1600 		if (WARN_ON_ONCE(tk == NULL)) {
1601 			pr_warn("error on getting new probe.\n");
1602 			warn++;
1603 		} else {
1604 			file = find_trace_probe_file(tk, top_trace_array());
1605 			if (WARN_ON_ONCE(file == NULL)) {
1606 				pr_warn("error on getting probe file.\n");
1607 				warn++;
1608 			} else
1609 				enable_trace_kprobe(tk, file);
1610 		}
1611 	}
1612 
1613 	ret = trace_run_command("r:testprobe2 kprobe_trace_selftest_target "
1614 				"$retval", create_trace_kprobe);
1615 	if (WARN_ON_ONCE(ret)) {
1616 		pr_warn("error on probing function return.\n");
1617 		warn++;
1618 	} else {
1619 		/* Enable trace point */
1620 		tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
1621 		if (WARN_ON_ONCE(tk == NULL)) {
1622 			pr_warn("error on getting 2nd new probe.\n");
1623 			warn++;
1624 		} else {
1625 			file = find_trace_probe_file(tk, top_trace_array());
1626 			if (WARN_ON_ONCE(file == NULL)) {
1627 				pr_warn("error on getting probe file.\n");
1628 				warn++;
1629 			} else
1630 				enable_trace_kprobe(tk, file);
1631 		}
1632 	}
1633 
1634 	if (warn)
1635 		goto end;
1636 
1637 	ret = target(1, 2, 3, 4, 5, 6);
1638 
1639 	/*
1640 	 * Not expecting an error here, the check is only to prevent the
1641 	 * optimizer from removing the call to target() as otherwise there
1642 	 * are no side-effects and the call is never performed.
1643 	 */
1644 	if (ret != 21)
1645 		warn++;
1646 
1647 	/* Disable trace points before removing it */
1648 	tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
1649 	if (WARN_ON_ONCE(tk == NULL)) {
1650 		pr_warn("error on getting test probe.\n");
1651 		warn++;
1652 	} else {
1653 		if (trace_kprobe_nhit(tk) != 1) {
1654 			pr_warn("incorrect number of testprobe hits\n");
1655 			warn++;
1656 		}
1657 
1658 		file = find_trace_probe_file(tk, top_trace_array());
1659 		if (WARN_ON_ONCE(file == NULL)) {
1660 			pr_warn("error on getting probe file.\n");
1661 			warn++;
1662 		} else
1663 			disable_trace_kprobe(tk, file);
1664 	}
1665 
1666 	tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
1667 	if (WARN_ON_ONCE(tk == NULL)) {
1668 		pr_warn("error on getting 2nd test probe.\n");
1669 		warn++;
1670 	} else {
1671 		if (trace_kprobe_nhit(tk) != 1) {
1672 			pr_warn("incorrect number of testprobe2 hits\n");
1673 			warn++;
1674 		}
1675 
1676 		file = find_trace_probe_file(tk, top_trace_array());
1677 		if (WARN_ON_ONCE(file == NULL)) {
1678 			pr_warn("error on getting probe file.\n");
1679 			warn++;
1680 		} else
1681 			disable_trace_kprobe(tk, file);
1682 	}
1683 
1684 	ret = trace_run_command("-:testprobe", create_trace_kprobe);
1685 	if (WARN_ON_ONCE(ret)) {
1686 		pr_warn("error on deleting a probe.\n");
1687 		warn++;
1688 	}
1689 
1690 	ret = trace_run_command("-:testprobe2", create_trace_kprobe);
1691 	if (WARN_ON_ONCE(ret)) {
1692 		pr_warn("error on deleting a probe.\n");
1693 		warn++;
1694 	}
1695 
1696 end:
1697 	release_all_trace_kprobes();
1698 	/*
1699 	 * Wait for the optimizer work to finish. Otherwise it might fiddle
1700 	 * with probes in already freed __init text.
1701 	 */
1702 	wait_for_kprobe_optimizer();
1703 	if (warn)
1704 		pr_cont("NG: Some tests are failed. Please check them.\n");
1705 	else
1706 		pr_cont("OK\n");
1707 	return 0;
1708 }
1709 
1710 late_initcall(kprobe_trace_self_tests_init);
1711 
1712 #endif
1713