xref: /linux/kernel/trace/trace_kprobe.c (revision 34f71a4a2de84dde52ccfcb96ce25240ea7981a8)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Kprobes-based tracing events
4  *
5  * Created by Masami Hiramatsu <mhiramat@redhat.com>
6  *
7  */
8 #define pr_fmt(fmt)	"trace_kprobe: " fmt
9 
10 #include <linux/security.h>
11 #include <linux/module.h>
12 #include <linux/uaccess.h>
13 #include <linux/rculist.h>
14 #include <linux/error-injection.h>
15 
16 #include <asm/setup.h>  /* for COMMAND_LINE_SIZE */
17 
18 #include "trace_dynevent.h"
19 #include "trace_kprobe_selftest.h"
20 #include "trace_probe.h"
21 #include "trace_probe_tmpl.h"
22 
23 #define KPROBE_EVENT_SYSTEM "kprobes"
24 #define KRETPROBE_MAXACTIVE_MAX 4096
25 
26 /* Kprobe early definition from command line */
27 static char kprobe_boot_events_buf[COMMAND_LINE_SIZE] __initdata;
28 static bool kprobe_boot_events_enabled __initdata;
29 
30 static int __init set_kprobe_boot_events(char *str)
31 {
32 	strlcpy(kprobe_boot_events_buf, str, COMMAND_LINE_SIZE);
33 	return 0;
34 }
35 __setup("kprobe_event=", set_kprobe_boot_events);
36 
37 static int trace_kprobe_create(int argc, const char **argv);
38 static int trace_kprobe_show(struct seq_file *m, struct dyn_event *ev);
39 static int trace_kprobe_release(struct dyn_event *ev);
40 static bool trace_kprobe_is_busy(struct dyn_event *ev);
41 static bool trace_kprobe_match(const char *system, const char *event,
42 			int argc, const char **argv, struct dyn_event *ev);
43 
44 static struct dyn_event_operations trace_kprobe_ops = {
45 	.create = trace_kprobe_create,
46 	.show = trace_kprobe_show,
47 	.is_busy = trace_kprobe_is_busy,
48 	.free = trace_kprobe_release,
49 	.match = trace_kprobe_match,
50 };
51 
52 /*
53  * Kprobe event core functions
54  */
55 struct trace_kprobe {
56 	struct dyn_event	devent;
57 	struct kretprobe	rp;	/* Use rp.kp for kprobe use */
58 	unsigned long __percpu *nhit;
59 	const char		*symbol;	/* symbol name */
60 	struct trace_probe	tp;
61 };
62 
63 static bool is_trace_kprobe(struct dyn_event *ev)
64 {
65 	return ev->ops == &trace_kprobe_ops;
66 }
67 
68 static struct trace_kprobe *to_trace_kprobe(struct dyn_event *ev)
69 {
70 	return container_of(ev, struct trace_kprobe, devent);
71 }
72 
73 /**
74  * for_each_trace_kprobe - iterate over the trace_kprobe list
75  * @pos:	the struct trace_kprobe * for each entry
76  * @dpos:	the struct dyn_event * to use as a loop cursor
77  */
78 #define for_each_trace_kprobe(pos, dpos)	\
79 	for_each_dyn_event(dpos)		\
80 		if (is_trace_kprobe(dpos) && (pos = to_trace_kprobe(dpos)))
81 
82 #define SIZEOF_TRACE_KPROBE(n)				\
83 	(offsetof(struct trace_kprobe, tp.args) +	\
84 	(sizeof(struct probe_arg) * (n)))
85 
86 static nokprobe_inline bool trace_kprobe_is_return(struct trace_kprobe *tk)
87 {
88 	return tk->rp.handler != NULL;
89 }
90 
91 static nokprobe_inline const char *trace_kprobe_symbol(struct trace_kprobe *tk)
92 {
93 	return tk->symbol ? tk->symbol : "unknown";
94 }
95 
96 static nokprobe_inline unsigned long trace_kprobe_offset(struct trace_kprobe *tk)
97 {
98 	return tk->rp.kp.offset;
99 }
100 
101 static nokprobe_inline bool trace_kprobe_has_gone(struct trace_kprobe *tk)
102 {
103 	return !!(kprobe_gone(&tk->rp.kp));
104 }
105 
106 static nokprobe_inline bool trace_kprobe_within_module(struct trace_kprobe *tk,
107 						 struct module *mod)
108 {
109 	int len = strlen(mod->name);
110 	const char *name = trace_kprobe_symbol(tk);
111 	return strncmp(mod->name, name, len) == 0 && name[len] == ':';
112 }
113 
114 static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprobe *tk)
115 {
116 	char *p;
117 	bool ret;
118 
119 	if (!tk->symbol)
120 		return false;
121 	p = strchr(tk->symbol, ':');
122 	if (!p)
123 		return true;
124 	*p = '\0';
125 	mutex_lock(&module_mutex);
126 	ret = !!find_module(tk->symbol);
127 	mutex_unlock(&module_mutex);
128 	*p = ':';
129 
130 	return ret;
131 }
132 
133 static bool trace_kprobe_is_busy(struct dyn_event *ev)
134 {
135 	struct trace_kprobe *tk = to_trace_kprobe(ev);
136 
137 	return trace_probe_is_enabled(&tk->tp);
138 }
139 
140 static bool trace_kprobe_match_command_head(struct trace_kprobe *tk,
141 					    int argc, const char **argv)
142 {
143 	char buf[MAX_ARGSTR_LEN + 1];
144 
145 	if (!argc)
146 		return true;
147 
148 	if (!tk->symbol)
149 		snprintf(buf, sizeof(buf), "0x%p", tk->rp.kp.addr);
150 	else if (tk->rp.kp.offset)
151 		snprintf(buf, sizeof(buf), "%s+%u",
152 			 trace_kprobe_symbol(tk), tk->rp.kp.offset);
153 	else
154 		snprintf(buf, sizeof(buf), "%s", trace_kprobe_symbol(tk));
155 	if (strcmp(buf, argv[0]))
156 		return false;
157 	argc--; argv++;
158 
159 	return trace_probe_match_command_args(&tk->tp, argc, argv);
160 }
161 
162 static bool trace_kprobe_match(const char *system, const char *event,
163 			int argc, const char **argv, struct dyn_event *ev)
164 {
165 	struct trace_kprobe *tk = to_trace_kprobe(ev);
166 
167 	return strcmp(trace_probe_name(&tk->tp), event) == 0 &&
168 	    (!system || strcmp(trace_probe_group_name(&tk->tp), system) == 0) &&
169 	    trace_kprobe_match_command_head(tk, argc, argv);
170 }
171 
172 static nokprobe_inline unsigned long trace_kprobe_nhit(struct trace_kprobe *tk)
173 {
174 	unsigned long nhit = 0;
175 	int cpu;
176 
177 	for_each_possible_cpu(cpu)
178 		nhit += *per_cpu_ptr(tk->nhit, cpu);
179 
180 	return nhit;
181 }
182 
183 static nokprobe_inline bool trace_kprobe_is_registered(struct trace_kprobe *tk)
184 {
185 	return !(list_empty(&tk->rp.kp.list) &&
186 		 hlist_unhashed(&tk->rp.kp.hlist));
187 }
188 
189 /* Return 0 if it fails to find the symbol address */
190 static nokprobe_inline
191 unsigned long trace_kprobe_address(struct trace_kprobe *tk)
192 {
193 	unsigned long addr;
194 
195 	if (tk->symbol) {
196 		addr = (unsigned long)
197 			kallsyms_lookup_name(trace_kprobe_symbol(tk));
198 		if (addr)
199 			addr += tk->rp.kp.offset;
200 	} else {
201 		addr = (unsigned long)tk->rp.kp.addr;
202 	}
203 	return addr;
204 }
205 
206 static nokprobe_inline struct trace_kprobe *
207 trace_kprobe_primary_from_call(struct trace_event_call *call)
208 {
209 	struct trace_probe *tp;
210 
211 	tp = trace_probe_primary_from_call(call);
212 	if (WARN_ON_ONCE(!tp))
213 		return NULL;
214 
215 	return container_of(tp, struct trace_kprobe, tp);
216 }
217 
218 bool trace_kprobe_on_func_entry(struct trace_event_call *call)
219 {
220 	struct trace_kprobe *tk = trace_kprobe_primary_from_call(call);
221 
222 	return tk ? kprobe_on_func_entry(tk->rp.kp.addr,
223 			tk->rp.kp.addr ? NULL : tk->rp.kp.symbol_name,
224 			tk->rp.kp.addr ? 0 : tk->rp.kp.offset) : false;
225 }
226 
227 bool trace_kprobe_error_injectable(struct trace_event_call *call)
228 {
229 	struct trace_kprobe *tk = trace_kprobe_primary_from_call(call);
230 
231 	return tk ? within_error_injection_list(trace_kprobe_address(tk)) :
232 	       false;
233 }
234 
235 static int register_kprobe_event(struct trace_kprobe *tk);
236 static int unregister_kprobe_event(struct trace_kprobe *tk);
237 
238 static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs);
239 static int kretprobe_dispatcher(struct kretprobe_instance *ri,
240 				struct pt_regs *regs);
241 
242 static void free_trace_kprobe(struct trace_kprobe *tk)
243 {
244 	if (tk) {
245 		trace_probe_cleanup(&tk->tp);
246 		kfree(tk->symbol);
247 		free_percpu(tk->nhit);
248 		kfree(tk);
249 	}
250 }
251 
252 /*
253  * Allocate new trace_probe and initialize it (including kprobes).
254  */
255 static struct trace_kprobe *alloc_trace_kprobe(const char *group,
256 					     const char *event,
257 					     void *addr,
258 					     const char *symbol,
259 					     unsigned long offs,
260 					     int maxactive,
261 					     int nargs, bool is_return)
262 {
263 	struct trace_kprobe *tk;
264 	int ret = -ENOMEM;
265 
266 	tk = kzalloc(SIZEOF_TRACE_KPROBE(nargs), GFP_KERNEL);
267 	if (!tk)
268 		return ERR_PTR(ret);
269 
270 	tk->nhit = alloc_percpu(unsigned long);
271 	if (!tk->nhit)
272 		goto error;
273 
274 	if (symbol) {
275 		tk->symbol = kstrdup(symbol, GFP_KERNEL);
276 		if (!tk->symbol)
277 			goto error;
278 		tk->rp.kp.symbol_name = tk->symbol;
279 		tk->rp.kp.offset = offs;
280 	} else
281 		tk->rp.kp.addr = addr;
282 
283 	if (is_return)
284 		tk->rp.handler = kretprobe_dispatcher;
285 	else
286 		tk->rp.kp.pre_handler = kprobe_dispatcher;
287 
288 	tk->rp.maxactive = maxactive;
289 	INIT_HLIST_NODE(&tk->rp.kp.hlist);
290 	INIT_LIST_HEAD(&tk->rp.kp.list);
291 
292 	ret = trace_probe_init(&tk->tp, event, group);
293 	if (ret < 0)
294 		goto error;
295 
296 	dyn_event_init(&tk->devent, &trace_kprobe_ops);
297 	return tk;
298 error:
299 	free_trace_kprobe(tk);
300 	return ERR_PTR(ret);
301 }
302 
303 static struct trace_kprobe *find_trace_kprobe(const char *event,
304 					      const char *group)
305 {
306 	struct dyn_event *pos;
307 	struct trace_kprobe *tk;
308 
309 	for_each_trace_kprobe(tk, pos)
310 		if (strcmp(trace_probe_name(&tk->tp), event) == 0 &&
311 		    strcmp(trace_probe_group_name(&tk->tp), group) == 0)
312 			return tk;
313 	return NULL;
314 }
315 
316 static inline int __enable_trace_kprobe(struct trace_kprobe *tk)
317 {
318 	int ret = 0;
319 
320 	if (trace_kprobe_is_registered(tk) && !trace_kprobe_has_gone(tk)) {
321 		if (trace_kprobe_is_return(tk))
322 			ret = enable_kretprobe(&tk->rp);
323 		else
324 			ret = enable_kprobe(&tk->rp.kp);
325 	}
326 
327 	return ret;
328 }
329 
330 static void __disable_trace_kprobe(struct trace_probe *tp)
331 {
332 	struct trace_probe *pos;
333 	struct trace_kprobe *tk;
334 
335 	list_for_each_entry(pos, trace_probe_probe_list(tp), list) {
336 		tk = container_of(pos, struct trace_kprobe, tp);
337 		if (!trace_kprobe_is_registered(tk))
338 			continue;
339 		if (trace_kprobe_is_return(tk))
340 			disable_kretprobe(&tk->rp);
341 		else
342 			disable_kprobe(&tk->rp.kp);
343 	}
344 }
345 
346 /*
347  * Enable trace_probe
348  * if the file is NULL, enable "perf" handler, or enable "trace" handler.
349  */
350 static int enable_trace_kprobe(struct trace_event_call *call,
351 				struct trace_event_file *file)
352 {
353 	struct trace_probe *pos, *tp;
354 	struct trace_kprobe *tk;
355 	bool enabled;
356 	int ret = 0;
357 
358 	tp = trace_probe_primary_from_call(call);
359 	if (WARN_ON_ONCE(!tp))
360 		return -ENODEV;
361 	enabled = trace_probe_is_enabled(tp);
362 
363 	/* This also changes "enabled" state */
364 	if (file) {
365 		ret = trace_probe_add_file(tp, file);
366 		if (ret)
367 			return ret;
368 	} else
369 		trace_probe_set_flag(tp, TP_FLAG_PROFILE);
370 
371 	if (enabled)
372 		return 0;
373 
374 	list_for_each_entry(pos, trace_probe_probe_list(tp), list) {
375 		tk = container_of(pos, struct trace_kprobe, tp);
376 		if (trace_kprobe_has_gone(tk))
377 			continue;
378 		ret = __enable_trace_kprobe(tk);
379 		if (ret)
380 			break;
381 		enabled = true;
382 	}
383 
384 	if (ret) {
385 		/* Failed to enable one of them. Roll back all */
386 		if (enabled)
387 			__disable_trace_kprobe(tp);
388 		if (file)
389 			trace_probe_remove_file(tp, file);
390 		else
391 			trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
392 	}
393 
394 	return ret;
395 }
396 
397 /*
398  * Disable trace_probe
399  * if the file is NULL, disable "perf" handler, or disable "trace" handler.
400  */
401 static int disable_trace_kprobe(struct trace_event_call *call,
402 				struct trace_event_file *file)
403 {
404 	struct trace_probe *tp;
405 
406 	tp = trace_probe_primary_from_call(call);
407 	if (WARN_ON_ONCE(!tp))
408 		return -ENODEV;
409 
410 	if (file) {
411 		if (!trace_probe_get_file_link(tp, file))
412 			return -ENOENT;
413 		if (!trace_probe_has_single_file(tp))
414 			goto out;
415 		trace_probe_clear_flag(tp, TP_FLAG_TRACE);
416 	} else
417 		trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
418 
419 	if (!trace_probe_is_enabled(tp))
420 		__disable_trace_kprobe(tp);
421 
422  out:
423 	if (file)
424 		/*
425 		 * Synchronization is done in below function. For perf event,
426 		 * file == NULL and perf_trace_event_unreg() calls
427 		 * tracepoint_synchronize_unregister() to ensure synchronize
428 		 * event. We don't need to care about it.
429 		 */
430 		trace_probe_remove_file(tp, file);
431 
432 	return 0;
433 }
434 
435 #if defined(CONFIG_KPROBES_ON_FTRACE) && \
436 	!defined(CONFIG_KPROBE_EVENTS_ON_NOTRACE)
437 static bool __within_notrace_func(unsigned long addr)
438 {
439 	unsigned long offset, size;
440 
441 	if (!addr || !kallsyms_lookup_size_offset(addr, &size, &offset))
442 		return false;
443 
444 	/* Get the entry address of the target function */
445 	addr -= offset;
446 
447 	/*
448 	 * Since ftrace_location_range() does inclusive range check, we need
449 	 * to subtract 1 byte from the end address.
450 	 */
451 	return !ftrace_location_range(addr, addr + size - 1);
452 }
453 
454 static bool within_notrace_func(struct trace_kprobe *tk)
455 {
456 	unsigned long addr = addr = trace_kprobe_address(tk);
457 	char symname[KSYM_NAME_LEN], *p;
458 
459 	if (!__within_notrace_func(addr))
460 		return false;
461 
462 	/* Check if the address is on a suffixed-symbol */
463 	if (!lookup_symbol_name(addr, symname)) {
464 		p = strchr(symname, '.');
465 		if (!p)
466 			return true;
467 		*p = '\0';
468 		addr = (unsigned long)kprobe_lookup_name(symname, 0);
469 		if (addr)
470 			return __within_notrace_func(addr);
471 	}
472 
473 	return true;
474 }
475 #else
476 #define within_notrace_func(tk)	(false)
477 #endif
478 
479 /* Internal register function - just handle k*probes and flags */
480 static int __register_trace_kprobe(struct trace_kprobe *tk)
481 {
482 	int i, ret;
483 
484 	ret = security_locked_down(LOCKDOWN_KPROBES);
485 	if (ret)
486 		return ret;
487 
488 	if (trace_kprobe_is_registered(tk))
489 		return -EINVAL;
490 
491 	if (within_notrace_func(tk)) {
492 		pr_warn("Could not probe notrace function %s\n",
493 			trace_kprobe_symbol(tk));
494 		return -EINVAL;
495 	}
496 
497 	for (i = 0; i < tk->tp.nr_args; i++) {
498 		ret = traceprobe_update_arg(&tk->tp.args[i]);
499 		if (ret)
500 			return ret;
501 	}
502 
503 	/* Set/clear disabled flag according to tp->flag */
504 	if (trace_probe_is_enabled(&tk->tp))
505 		tk->rp.kp.flags &= ~KPROBE_FLAG_DISABLED;
506 	else
507 		tk->rp.kp.flags |= KPROBE_FLAG_DISABLED;
508 
509 	if (trace_kprobe_is_return(tk))
510 		ret = register_kretprobe(&tk->rp);
511 	else
512 		ret = register_kprobe(&tk->rp.kp);
513 
514 	return ret;
515 }
516 
517 /* Internal unregister function - just handle k*probes and flags */
518 static void __unregister_trace_kprobe(struct trace_kprobe *tk)
519 {
520 	if (trace_kprobe_is_registered(tk)) {
521 		if (trace_kprobe_is_return(tk))
522 			unregister_kretprobe(&tk->rp);
523 		else
524 			unregister_kprobe(&tk->rp.kp);
525 		/* Cleanup kprobe for reuse and mark it unregistered */
526 		INIT_HLIST_NODE(&tk->rp.kp.hlist);
527 		INIT_LIST_HEAD(&tk->rp.kp.list);
528 		if (tk->rp.kp.symbol_name)
529 			tk->rp.kp.addr = NULL;
530 	}
531 }
532 
533 /* Unregister a trace_probe and probe_event */
534 static int unregister_trace_kprobe(struct trace_kprobe *tk)
535 {
536 	/* If other probes are on the event, just unregister kprobe */
537 	if (trace_probe_has_sibling(&tk->tp))
538 		goto unreg;
539 
540 	/* Enabled event can not be unregistered */
541 	if (trace_probe_is_enabled(&tk->tp))
542 		return -EBUSY;
543 
544 	/* Will fail if probe is being used by ftrace or perf */
545 	if (unregister_kprobe_event(tk))
546 		return -EBUSY;
547 
548 unreg:
549 	__unregister_trace_kprobe(tk);
550 	dyn_event_remove(&tk->devent);
551 	trace_probe_unlink(&tk->tp);
552 
553 	return 0;
554 }
555 
556 static bool trace_kprobe_has_same_kprobe(struct trace_kprobe *orig,
557 					 struct trace_kprobe *comp)
558 {
559 	struct trace_probe_event *tpe = orig->tp.event;
560 	struct trace_probe *pos;
561 	int i;
562 
563 	list_for_each_entry(pos, &tpe->probes, list) {
564 		orig = container_of(pos, struct trace_kprobe, tp);
565 		if (strcmp(trace_kprobe_symbol(orig),
566 			   trace_kprobe_symbol(comp)) ||
567 		    trace_kprobe_offset(orig) != trace_kprobe_offset(comp))
568 			continue;
569 
570 		/*
571 		 * trace_probe_compare_arg_type() ensured that nr_args and
572 		 * each argument name and type are same. Let's compare comm.
573 		 */
574 		for (i = 0; i < orig->tp.nr_args; i++) {
575 			if (strcmp(orig->tp.args[i].comm,
576 				   comp->tp.args[i].comm))
577 				break;
578 		}
579 
580 		if (i == orig->tp.nr_args)
581 			return true;
582 	}
583 
584 	return false;
585 }
586 
587 static int append_trace_kprobe(struct trace_kprobe *tk, struct trace_kprobe *to)
588 {
589 	int ret;
590 
591 	ret = trace_probe_compare_arg_type(&tk->tp, &to->tp);
592 	if (ret) {
593 		/* Note that argument starts index = 2 */
594 		trace_probe_log_set_index(ret + 1);
595 		trace_probe_log_err(0, DIFF_ARG_TYPE);
596 		return -EEXIST;
597 	}
598 	if (trace_kprobe_has_same_kprobe(to, tk)) {
599 		trace_probe_log_set_index(0);
600 		trace_probe_log_err(0, SAME_PROBE);
601 		return -EEXIST;
602 	}
603 
604 	/* Append to existing event */
605 	ret = trace_probe_append(&tk->tp, &to->tp);
606 	if (ret)
607 		return ret;
608 
609 	/* Register k*probe */
610 	ret = __register_trace_kprobe(tk);
611 	if (ret == -ENOENT && !trace_kprobe_module_exist(tk)) {
612 		pr_warn("This probe might be able to register after target module is loaded. Continue.\n");
613 		ret = 0;
614 	}
615 
616 	if (ret)
617 		trace_probe_unlink(&tk->tp);
618 	else
619 		dyn_event_add(&tk->devent);
620 
621 	return ret;
622 }
623 
624 /* Register a trace_probe and probe_event */
625 static int register_trace_kprobe(struct trace_kprobe *tk)
626 {
627 	struct trace_kprobe *old_tk;
628 	int ret;
629 
630 	mutex_lock(&event_mutex);
631 
632 	old_tk = find_trace_kprobe(trace_probe_name(&tk->tp),
633 				   trace_probe_group_name(&tk->tp));
634 	if (old_tk) {
635 		if (trace_kprobe_is_return(tk) != trace_kprobe_is_return(old_tk)) {
636 			trace_probe_log_set_index(0);
637 			trace_probe_log_err(0, DIFF_PROBE_TYPE);
638 			ret = -EEXIST;
639 		} else {
640 			ret = append_trace_kprobe(tk, old_tk);
641 		}
642 		goto end;
643 	}
644 
645 	/* Register new event */
646 	ret = register_kprobe_event(tk);
647 	if (ret) {
648 		pr_warn("Failed to register probe event(%d)\n", ret);
649 		goto end;
650 	}
651 
652 	/* Register k*probe */
653 	ret = __register_trace_kprobe(tk);
654 	if (ret == -ENOENT && !trace_kprobe_module_exist(tk)) {
655 		pr_warn("This probe might be able to register after target module is loaded. Continue.\n");
656 		ret = 0;
657 	}
658 
659 	if (ret < 0)
660 		unregister_kprobe_event(tk);
661 	else
662 		dyn_event_add(&tk->devent);
663 
664 end:
665 	mutex_unlock(&event_mutex);
666 	return ret;
667 }
668 
669 /* Module notifier call back, checking event on the module */
670 static int trace_kprobe_module_callback(struct notifier_block *nb,
671 				       unsigned long val, void *data)
672 {
673 	struct module *mod = data;
674 	struct dyn_event *pos;
675 	struct trace_kprobe *tk;
676 	int ret;
677 
678 	if (val != MODULE_STATE_COMING)
679 		return NOTIFY_DONE;
680 
681 	/* Update probes on coming module */
682 	mutex_lock(&event_mutex);
683 	for_each_trace_kprobe(tk, pos) {
684 		if (trace_kprobe_within_module(tk, mod)) {
685 			/* Don't need to check busy - this should have gone. */
686 			__unregister_trace_kprobe(tk);
687 			ret = __register_trace_kprobe(tk);
688 			if (ret)
689 				pr_warn("Failed to re-register probe %s on %s: %d\n",
690 					trace_probe_name(&tk->tp),
691 					mod->name, ret);
692 		}
693 	}
694 	mutex_unlock(&event_mutex);
695 
696 	return NOTIFY_DONE;
697 }
698 
699 static struct notifier_block trace_kprobe_module_nb = {
700 	.notifier_call = trace_kprobe_module_callback,
701 	.priority = 1	/* Invoked after kprobe module callback */
702 };
703 
704 /* Convert certain expected symbols into '_' when generating event names */
705 static inline void sanitize_event_name(char *name)
706 {
707 	while (*name++ != '\0')
708 		if (*name == ':' || *name == '.')
709 			*name = '_';
710 }
711 
712 static int trace_kprobe_create(int argc, const char *argv[])
713 {
714 	/*
715 	 * Argument syntax:
716 	 *  - Add kprobe:
717 	 *      p[:[GRP/]EVENT] [MOD:]KSYM[+OFFS]|KADDR [FETCHARGS]
718 	 *  - Add kretprobe:
719 	 *      r[MAXACTIVE][:[GRP/]EVENT] [MOD:]KSYM[+0] [FETCHARGS]
720 	 * Fetch args:
721 	 *  $retval	: fetch return value
722 	 *  $stack	: fetch stack address
723 	 *  $stackN	: fetch Nth of stack (N:0-)
724 	 *  $comm       : fetch current task comm
725 	 *  @ADDR	: fetch memory at ADDR (ADDR should be in kernel)
726 	 *  @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
727 	 *  %REG	: fetch register REG
728 	 * Dereferencing memory fetch:
729 	 *  +|-offs(ARG) : fetch memory at ARG +|- offs address.
730 	 * Alias name of args:
731 	 *  NAME=FETCHARG : set NAME as alias of FETCHARG.
732 	 * Type of args:
733 	 *  FETCHARG:TYPE : use TYPE instead of unsigned long.
734 	 */
735 	struct trace_kprobe *tk = NULL;
736 	int i, len, ret = 0;
737 	bool is_return = false;
738 	char *symbol = NULL, *tmp = NULL;
739 	const char *event = NULL, *group = KPROBE_EVENT_SYSTEM;
740 	int maxactive = 0;
741 	long offset = 0;
742 	void *addr = NULL;
743 	char buf[MAX_EVENT_NAME_LEN];
744 	unsigned int flags = TPARG_FL_KERNEL;
745 
746 	switch (argv[0][0]) {
747 	case 'r':
748 		is_return = true;
749 		flags |= TPARG_FL_RETURN;
750 		break;
751 	case 'p':
752 		break;
753 	default:
754 		return -ECANCELED;
755 	}
756 	if (argc < 2)
757 		return -ECANCELED;
758 
759 	trace_probe_log_init("trace_kprobe", argc, argv);
760 
761 	event = strchr(&argv[0][1], ':');
762 	if (event)
763 		event++;
764 
765 	if (isdigit(argv[0][1])) {
766 		if (!is_return) {
767 			trace_probe_log_err(1, MAXACT_NO_KPROBE);
768 			goto parse_error;
769 		}
770 		if (event)
771 			len = event - &argv[0][1] - 1;
772 		else
773 			len = strlen(&argv[0][1]);
774 		if (len > MAX_EVENT_NAME_LEN - 1) {
775 			trace_probe_log_err(1, BAD_MAXACT);
776 			goto parse_error;
777 		}
778 		memcpy(buf, &argv[0][1], len);
779 		buf[len] = '\0';
780 		ret = kstrtouint(buf, 0, &maxactive);
781 		if (ret || !maxactive) {
782 			trace_probe_log_err(1, BAD_MAXACT);
783 			goto parse_error;
784 		}
785 		/* kretprobes instances are iterated over via a list. The
786 		 * maximum should stay reasonable.
787 		 */
788 		if (maxactive > KRETPROBE_MAXACTIVE_MAX) {
789 			trace_probe_log_err(1, MAXACT_TOO_BIG);
790 			goto parse_error;
791 		}
792 	}
793 
794 	/* try to parse an address. if that fails, try to read the
795 	 * input as a symbol. */
796 	if (kstrtoul(argv[1], 0, (unsigned long *)&addr)) {
797 		trace_probe_log_set_index(1);
798 		/* Check whether uprobe event specified */
799 		if (strchr(argv[1], '/') && strchr(argv[1], ':')) {
800 			ret = -ECANCELED;
801 			goto error;
802 		}
803 		/* a symbol specified */
804 		symbol = kstrdup(argv[1], GFP_KERNEL);
805 		if (!symbol)
806 			return -ENOMEM;
807 		/* TODO: support .init module functions */
808 		ret = traceprobe_split_symbol_offset(symbol, &offset);
809 		if (ret || offset < 0 || offset > UINT_MAX) {
810 			trace_probe_log_err(0, BAD_PROBE_ADDR);
811 			goto parse_error;
812 		}
813 		if (kprobe_on_func_entry(NULL, symbol, offset))
814 			flags |= TPARG_FL_FENTRY;
815 		if (offset && is_return && !(flags & TPARG_FL_FENTRY)) {
816 			trace_probe_log_err(0, BAD_RETPROBE);
817 			goto parse_error;
818 		}
819 	}
820 
821 	trace_probe_log_set_index(0);
822 	if (event) {
823 		ret = traceprobe_parse_event_name(&event, &group, buf,
824 						  event - argv[0]);
825 		if (ret)
826 			goto parse_error;
827 	} else {
828 		/* Make a new event name */
829 		if (symbol)
830 			snprintf(buf, MAX_EVENT_NAME_LEN, "%c_%s_%ld",
831 				 is_return ? 'r' : 'p', symbol, offset);
832 		else
833 			snprintf(buf, MAX_EVENT_NAME_LEN, "%c_0x%p",
834 				 is_return ? 'r' : 'p', addr);
835 		sanitize_event_name(buf);
836 		event = buf;
837 	}
838 
839 	/* setup a probe */
840 	tk = alloc_trace_kprobe(group, event, addr, symbol, offset, maxactive,
841 			       argc - 2, is_return);
842 	if (IS_ERR(tk)) {
843 		ret = PTR_ERR(tk);
844 		/* This must return -ENOMEM, else there is a bug */
845 		WARN_ON_ONCE(ret != -ENOMEM);
846 		goto out;	/* We know tk is not allocated */
847 	}
848 	argc -= 2; argv += 2;
849 
850 	/* parse arguments */
851 	for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
852 		tmp = kstrdup(argv[i], GFP_KERNEL);
853 		if (!tmp) {
854 			ret = -ENOMEM;
855 			goto error;
856 		}
857 
858 		trace_probe_log_set_index(i + 2);
859 		ret = traceprobe_parse_probe_arg(&tk->tp, i, tmp, flags);
860 		kfree(tmp);
861 		if (ret)
862 			goto error;	/* This can be -ENOMEM */
863 	}
864 
865 	ret = traceprobe_set_print_fmt(&tk->tp, is_return);
866 	if (ret < 0)
867 		goto error;
868 
869 	ret = register_trace_kprobe(tk);
870 	if (ret) {
871 		trace_probe_log_set_index(1);
872 		if (ret == -EILSEQ)
873 			trace_probe_log_err(0, BAD_INSN_BNDRY);
874 		else if (ret == -ENOENT)
875 			trace_probe_log_err(0, BAD_PROBE_ADDR);
876 		else if (ret != -ENOMEM && ret != -EEXIST)
877 			trace_probe_log_err(0, FAIL_REG_PROBE);
878 		goto error;
879 	}
880 
881 out:
882 	trace_probe_log_clear();
883 	kfree(symbol);
884 	return ret;
885 
886 parse_error:
887 	ret = -EINVAL;
888 error:
889 	free_trace_kprobe(tk);
890 	goto out;
891 }
892 
893 static int create_or_delete_trace_kprobe(int argc, char **argv)
894 {
895 	int ret;
896 
897 	if (argv[0][0] == '-')
898 		return dyn_event_release(argc, argv, &trace_kprobe_ops);
899 
900 	ret = trace_kprobe_create(argc, (const char **)argv);
901 	return ret == -ECANCELED ? -EINVAL : ret;
902 }
903 
904 int trace_kprobe_run_command(const char *command)
905 {
906 	return trace_run_command(command, create_or_delete_trace_kprobe);
907 }
908 
909 static int trace_kprobe_release(struct dyn_event *ev)
910 {
911 	struct trace_kprobe *tk = to_trace_kprobe(ev);
912 	int ret = unregister_trace_kprobe(tk);
913 
914 	if (!ret)
915 		free_trace_kprobe(tk);
916 	return ret;
917 }
918 
919 static int trace_kprobe_show(struct seq_file *m, struct dyn_event *ev)
920 {
921 	struct trace_kprobe *tk = to_trace_kprobe(ev);
922 	int i;
923 
924 	seq_putc(m, trace_kprobe_is_return(tk) ? 'r' : 'p');
925 	seq_printf(m, ":%s/%s", trace_probe_group_name(&tk->tp),
926 				trace_probe_name(&tk->tp));
927 
928 	if (!tk->symbol)
929 		seq_printf(m, " 0x%p", tk->rp.kp.addr);
930 	else if (tk->rp.kp.offset)
931 		seq_printf(m, " %s+%u", trace_kprobe_symbol(tk),
932 			   tk->rp.kp.offset);
933 	else
934 		seq_printf(m, " %s", trace_kprobe_symbol(tk));
935 
936 	for (i = 0; i < tk->tp.nr_args; i++)
937 		seq_printf(m, " %s=%s", tk->tp.args[i].name, tk->tp.args[i].comm);
938 	seq_putc(m, '\n');
939 
940 	return 0;
941 }
942 
943 static int probes_seq_show(struct seq_file *m, void *v)
944 {
945 	struct dyn_event *ev = v;
946 
947 	if (!is_trace_kprobe(ev))
948 		return 0;
949 
950 	return trace_kprobe_show(m, ev);
951 }
952 
953 static const struct seq_operations probes_seq_op = {
954 	.start  = dyn_event_seq_start,
955 	.next   = dyn_event_seq_next,
956 	.stop   = dyn_event_seq_stop,
957 	.show   = probes_seq_show
958 };
959 
960 static int probes_open(struct inode *inode, struct file *file)
961 {
962 	int ret;
963 
964 	ret = security_locked_down(LOCKDOWN_TRACEFS);
965 	if (ret)
966 		return ret;
967 
968 	if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
969 		ret = dyn_events_release_all(&trace_kprobe_ops);
970 		if (ret < 0)
971 			return ret;
972 	}
973 
974 	return seq_open(file, &probes_seq_op);
975 }
976 
977 static ssize_t probes_write(struct file *file, const char __user *buffer,
978 			    size_t count, loff_t *ppos)
979 {
980 	return trace_parse_run_command(file, buffer, count, ppos,
981 				       create_or_delete_trace_kprobe);
982 }
983 
984 static const struct file_operations kprobe_events_ops = {
985 	.owner          = THIS_MODULE,
986 	.open           = probes_open,
987 	.read           = seq_read,
988 	.llseek         = seq_lseek,
989 	.release        = seq_release,
990 	.write		= probes_write,
991 };
992 
993 /* Probes profiling interfaces */
994 static int probes_profile_seq_show(struct seq_file *m, void *v)
995 {
996 	struct dyn_event *ev = v;
997 	struct trace_kprobe *tk;
998 
999 	if (!is_trace_kprobe(ev))
1000 		return 0;
1001 
1002 	tk = to_trace_kprobe(ev);
1003 	seq_printf(m, "  %-44s %15lu %15lu\n",
1004 		   trace_probe_name(&tk->tp),
1005 		   trace_kprobe_nhit(tk),
1006 		   tk->rp.kp.nmissed);
1007 
1008 	return 0;
1009 }
1010 
1011 static const struct seq_operations profile_seq_op = {
1012 	.start  = dyn_event_seq_start,
1013 	.next   = dyn_event_seq_next,
1014 	.stop   = dyn_event_seq_stop,
1015 	.show   = probes_profile_seq_show
1016 };
1017 
1018 static int profile_open(struct inode *inode, struct file *file)
1019 {
1020 	int ret;
1021 
1022 	ret = security_locked_down(LOCKDOWN_TRACEFS);
1023 	if (ret)
1024 		return ret;
1025 
1026 	return seq_open(file, &profile_seq_op);
1027 }
1028 
1029 static const struct file_operations kprobe_profile_ops = {
1030 	.owner          = THIS_MODULE,
1031 	.open           = profile_open,
1032 	.read           = seq_read,
1033 	.llseek         = seq_lseek,
1034 	.release        = seq_release,
1035 };
1036 
1037 /* Kprobe specific fetch functions */
1038 
1039 /* Return the length of string -- including null terminal byte */
1040 static nokprobe_inline int
1041 fetch_store_strlen(unsigned long addr)
1042 {
1043 	int ret, len = 0;
1044 	u8 c;
1045 
1046 	do {
1047 		ret = probe_kernel_read(&c, (u8 *)addr + len, 1);
1048 		len++;
1049 	} while (c && ret == 0 && len < MAX_STRING_SIZE);
1050 
1051 	return (ret < 0) ? ret : len;
1052 }
1053 
1054 /* Return the length of string -- including null terminal byte */
1055 static nokprobe_inline int
1056 fetch_store_strlen_user(unsigned long addr)
1057 {
1058 	const void __user *uaddr =  (__force const void __user *)addr;
1059 
1060 	return strnlen_unsafe_user(uaddr, MAX_STRING_SIZE);
1061 }
1062 
1063 /*
1064  * Fetch a null-terminated string. Caller MUST set *(u32 *)buf with max
1065  * length and relative data location.
1066  */
1067 static nokprobe_inline int
1068 fetch_store_string(unsigned long addr, void *dest, void *base)
1069 {
1070 	int maxlen = get_loc_len(*(u32 *)dest);
1071 	void *__dest;
1072 	long ret;
1073 
1074 	if (unlikely(!maxlen))
1075 		return -ENOMEM;
1076 
1077 	__dest = get_loc_data(dest, base);
1078 
1079 	/*
1080 	 * Try to get string again, since the string can be changed while
1081 	 * probing.
1082 	 */
1083 	ret = strncpy_from_unsafe(__dest, (void *)addr, maxlen);
1084 	if (ret >= 0)
1085 		*(u32 *)dest = make_data_loc(ret, __dest - base);
1086 
1087 	return ret;
1088 }
1089 
1090 /*
1091  * Fetch a null-terminated string from user. Caller MUST set *(u32 *)buf
1092  * with max length and relative data location.
1093  */
1094 static nokprobe_inline int
1095 fetch_store_string_user(unsigned long addr, void *dest, void *base)
1096 {
1097 	const void __user *uaddr =  (__force const void __user *)addr;
1098 	int maxlen = get_loc_len(*(u32 *)dest);
1099 	void *__dest;
1100 	long ret;
1101 
1102 	if (unlikely(!maxlen))
1103 		return -ENOMEM;
1104 
1105 	__dest = get_loc_data(dest, base);
1106 
1107 	ret = strncpy_from_unsafe_user(__dest, uaddr, maxlen);
1108 	if (ret >= 0)
1109 		*(u32 *)dest = make_data_loc(ret, __dest - base);
1110 
1111 	return ret;
1112 }
1113 
1114 static nokprobe_inline int
1115 probe_mem_read(void *dest, void *src, size_t size)
1116 {
1117 	return probe_kernel_read(dest, src, size);
1118 }
1119 
1120 static nokprobe_inline int
1121 probe_mem_read_user(void *dest, void *src, size_t size)
1122 {
1123 	const void __user *uaddr =  (__force const void __user *)src;
1124 
1125 	return probe_user_read(dest, uaddr, size);
1126 }
1127 
1128 /* Note that we don't verify it, since the code does not come from user space */
1129 static int
1130 process_fetch_insn(struct fetch_insn *code, struct pt_regs *regs, void *dest,
1131 		   void *base)
1132 {
1133 	unsigned long val;
1134 
1135 retry:
1136 	/* 1st stage: get value from context */
1137 	switch (code->op) {
1138 	case FETCH_OP_REG:
1139 		val = regs_get_register(regs, code->param);
1140 		break;
1141 	case FETCH_OP_STACK:
1142 		val = regs_get_kernel_stack_nth(regs, code->param);
1143 		break;
1144 	case FETCH_OP_STACKP:
1145 		val = kernel_stack_pointer(regs);
1146 		break;
1147 	case FETCH_OP_RETVAL:
1148 		val = regs_return_value(regs);
1149 		break;
1150 	case FETCH_OP_IMM:
1151 		val = code->immediate;
1152 		break;
1153 	case FETCH_OP_COMM:
1154 		val = (unsigned long)current->comm;
1155 		break;
1156 	case FETCH_OP_DATA:
1157 		val = (unsigned long)code->data;
1158 		break;
1159 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
1160 	case FETCH_OP_ARG:
1161 		val = regs_get_kernel_argument(regs, code->param);
1162 		break;
1163 #endif
1164 	case FETCH_NOP_SYMBOL:	/* Ignore a place holder */
1165 		code++;
1166 		goto retry;
1167 	default:
1168 		return -EILSEQ;
1169 	}
1170 	code++;
1171 
1172 	return process_fetch_insn_bottom(code, val, dest, base);
1173 }
1174 NOKPROBE_SYMBOL(process_fetch_insn)
1175 
1176 /* Kprobe handler */
1177 static nokprobe_inline void
1178 __kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs,
1179 		    struct trace_event_file *trace_file)
1180 {
1181 	struct kprobe_trace_entry_head *entry;
1182 	struct trace_event_call *call = trace_probe_event_call(&tk->tp);
1183 	struct trace_event_buffer fbuffer;
1184 	int dsize;
1185 
1186 	WARN_ON(call != trace_file->event_call);
1187 
1188 	if (trace_trigger_soft_disabled(trace_file))
1189 		return;
1190 
1191 	local_save_flags(fbuffer.flags);
1192 	fbuffer.pc = preempt_count();
1193 	fbuffer.trace_file = trace_file;
1194 
1195 	dsize = __get_data_size(&tk->tp, regs);
1196 
1197 	fbuffer.event =
1198 		trace_event_buffer_lock_reserve(&fbuffer.buffer, trace_file,
1199 					call->event.type,
1200 					sizeof(*entry) + tk->tp.size + dsize,
1201 					fbuffer.flags, fbuffer.pc);
1202 	if (!fbuffer.event)
1203 		return;
1204 
1205 	fbuffer.regs = regs;
1206 	entry = fbuffer.entry = ring_buffer_event_data(fbuffer.event);
1207 	entry->ip = (unsigned long)tk->rp.kp.addr;
1208 	store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
1209 
1210 	trace_event_buffer_commit(&fbuffer);
1211 }
1212 
1213 static void
1214 kprobe_trace_func(struct trace_kprobe *tk, struct pt_regs *regs)
1215 {
1216 	struct event_file_link *link;
1217 
1218 	trace_probe_for_each_link_rcu(link, &tk->tp)
1219 		__kprobe_trace_func(tk, regs, link->file);
1220 }
1221 NOKPROBE_SYMBOL(kprobe_trace_func);
1222 
1223 /* Kretprobe handler */
1224 static nokprobe_inline void
1225 __kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
1226 		       struct pt_regs *regs,
1227 		       struct trace_event_file *trace_file)
1228 {
1229 	struct kretprobe_trace_entry_head *entry;
1230 	struct trace_event_buffer fbuffer;
1231 	struct trace_event_call *call = trace_probe_event_call(&tk->tp);
1232 	int dsize;
1233 
1234 	WARN_ON(call != trace_file->event_call);
1235 
1236 	if (trace_trigger_soft_disabled(trace_file))
1237 		return;
1238 
1239 	local_save_flags(fbuffer.flags);
1240 	fbuffer.pc = preempt_count();
1241 	fbuffer.trace_file = trace_file;
1242 
1243 	dsize = __get_data_size(&tk->tp, regs);
1244 	fbuffer.event =
1245 		trace_event_buffer_lock_reserve(&fbuffer.buffer, trace_file,
1246 					call->event.type,
1247 					sizeof(*entry) + tk->tp.size + dsize,
1248 					fbuffer.flags, fbuffer.pc);
1249 	if (!fbuffer.event)
1250 		return;
1251 
1252 	fbuffer.regs = regs;
1253 	entry = fbuffer.entry = ring_buffer_event_data(fbuffer.event);
1254 	entry->func = (unsigned long)tk->rp.kp.addr;
1255 	entry->ret_ip = (unsigned long)ri->ret_addr;
1256 	store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
1257 
1258 	trace_event_buffer_commit(&fbuffer);
1259 }
1260 
1261 static void
1262 kretprobe_trace_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
1263 		     struct pt_regs *regs)
1264 {
1265 	struct event_file_link *link;
1266 
1267 	trace_probe_for_each_link_rcu(link, &tk->tp)
1268 		__kretprobe_trace_func(tk, ri, regs, link->file);
1269 }
1270 NOKPROBE_SYMBOL(kretprobe_trace_func);
1271 
1272 /* Event entry printers */
1273 static enum print_line_t
1274 print_kprobe_event(struct trace_iterator *iter, int flags,
1275 		   struct trace_event *event)
1276 {
1277 	struct kprobe_trace_entry_head *field;
1278 	struct trace_seq *s = &iter->seq;
1279 	struct trace_probe *tp;
1280 
1281 	field = (struct kprobe_trace_entry_head *)iter->ent;
1282 	tp = trace_probe_primary_from_call(
1283 		container_of(event, struct trace_event_call, event));
1284 	if (WARN_ON_ONCE(!tp))
1285 		goto out;
1286 
1287 	trace_seq_printf(s, "%s: (", trace_probe_name(tp));
1288 
1289 	if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
1290 		goto out;
1291 
1292 	trace_seq_putc(s, ')');
1293 
1294 	if (print_probe_args(s, tp->args, tp->nr_args,
1295 			     (u8 *)&field[1], field) < 0)
1296 		goto out;
1297 
1298 	trace_seq_putc(s, '\n');
1299  out:
1300 	return trace_handle_return(s);
1301 }
1302 
1303 static enum print_line_t
1304 print_kretprobe_event(struct trace_iterator *iter, int flags,
1305 		      struct trace_event *event)
1306 {
1307 	struct kretprobe_trace_entry_head *field;
1308 	struct trace_seq *s = &iter->seq;
1309 	struct trace_probe *tp;
1310 
1311 	field = (struct kretprobe_trace_entry_head *)iter->ent;
1312 	tp = trace_probe_primary_from_call(
1313 		container_of(event, struct trace_event_call, event));
1314 	if (WARN_ON_ONCE(!tp))
1315 		goto out;
1316 
1317 	trace_seq_printf(s, "%s: (", trace_probe_name(tp));
1318 
1319 	if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
1320 		goto out;
1321 
1322 	trace_seq_puts(s, " <- ");
1323 
1324 	if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
1325 		goto out;
1326 
1327 	trace_seq_putc(s, ')');
1328 
1329 	if (print_probe_args(s, tp->args, tp->nr_args,
1330 			     (u8 *)&field[1], field) < 0)
1331 		goto out;
1332 
1333 	trace_seq_putc(s, '\n');
1334 
1335  out:
1336 	return trace_handle_return(s);
1337 }
1338 
1339 
1340 static int kprobe_event_define_fields(struct trace_event_call *event_call)
1341 {
1342 	int ret;
1343 	struct kprobe_trace_entry_head field;
1344 	struct trace_probe *tp;
1345 
1346 	tp = trace_probe_primary_from_call(event_call);
1347 	if (WARN_ON_ONCE(!tp))
1348 		return -ENOENT;
1349 
1350 	DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
1351 
1352 	return traceprobe_define_arg_fields(event_call, sizeof(field), tp);
1353 }
1354 
1355 static int kretprobe_event_define_fields(struct trace_event_call *event_call)
1356 {
1357 	int ret;
1358 	struct kretprobe_trace_entry_head field;
1359 	struct trace_probe *tp;
1360 
1361 	tp = trace_probe_primary_from_call(event_call);
1362 	if (WARN_ON_ONCE(!tp))
1363 		return -ENOENT;
1364 
1365 	DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
1366 	DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
1367 
1368 	return traceprobe_define_arg_fields(event_call, sizeof(field), tp);
1369 }
1370 
1371 #ifdef CONFIG_PERF_EVENTS
1372 
1373 /* Kprobe profile handler */
1374 static int
1375 kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs)
1376 {
1377 	struct trace_event_call *call = trace_probe_event_call(&tk->tp);
1378 	struct kprobe_trace_entry_head *entry;
1379 	struct hlist_head *head;
1380 	int size, __size, dsize;
1381 	int rctx;
1382 
1383 	if (bpf_prog_array_valid(call)) {
1384 		unsigned long orig_ip = instruction_pointer(regs);
1385 		int ret;
1386 
1387 		ret = trace_call_bpf(call, regs);
1388 
1389 		/*
1390 		 * We need to check and see if we modified the pc of the
1391 		 * pt_regs, and if so return 1 so that we don't do the
1392 		 * single stepping.
1393 		 */
1394 		if (orig_ip != instruction_pointer(regs))
1395 			return 1;
1396 		if (!ret)
1397 			return 0;
1398 	}
1399 
1400 	head = this_cpu_ptr(call->perf_events);
1401 	if (hlist_empty(head))
1402 		return 0;
1403 
1404 	dsize = __get_data_size(&tk->tp, regs);
1405 	__size = sizeof(*entry) + tk->tp.size + dsize;
1406 	size = ALIGN(__size + sizeof(u32), sizeof(u64));
1407 	size -= sizeof(u32);
1408 
1409 	entry = perf_trace_buf_alloc(size, NULL, &rctx);
1410 	if (!entry)
1411 		return 0;
1412 
1413 	entry->ip = (unsigned long)tk->rp.kp.addr;
1414 	memset(&entry[1], 0, dsize);
1415 	store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
1416 	perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1417 			      head, NULL);
1418 	return 0;
1419 }
1420 NOKPROBE_SYMBOL(kprobe_perf_func);
1421 
1422 /* Kretprobe profile handler */
1423 static void
1424 kretprobe_perf_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
1425 		    struct pt_regs *regs)
1426 {
1427 	struct trace_event_call *call = trace_probe_event_call(&tk->tp);
1428 	struct kretprobe_trace_entry_head *entry;
1429 	struct hlist_head *head;
1430 	int size, __size, dsize;
1431 	int rctx;
1432 
1433 	if (bpf_prog_array_valid(call) && !trace_call_bpf(call, regs))
1434 		return;
1435 
1436 	head = this_cpu_ptr(call->perf_events);
1437 	if (hlist_empty(head))
1438 		return;
1439 
1440 	dsize = __get_data_size(&tk->tp, regs);
1441 	__size = sizeof(*entry) + tk->tp.size + dsize;
1442 	size = ALIGN(__size + sizeof(u32), sizeof(u64));
1443 	size -= sizeof(u32);
1444 
1445 	entry = perf_trace_buf_alloc(size, NULL, &rctx);
1446 	if (!entry)
1447 		return;
1448 
1449 	entry->func = (unsigned long)tk->rp.kp.addr;
1450 	entry->ret_ip = (unsigned long)ri->ret_addr;
1451 	store_trace_args(&entry[1], &tk->tp, regs, sizeof(*entry), dsize);
1452 	perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
1453 			      head, NULL);
1454 }
1455 NOKPROBE_SYMBOL(kretprobe_perf_func);
1456 
1457 int bpf_get_kprobe_info(const struct perf_event *event, u32 *fd_type,
1458 			const char **symbol, u64 *probe_offset,
1459 			u64 *probe_addr, bool perf_type_tracepoint)
1460 {
1461 	const char *pevent = trace_event_name(event->tp_event);
1462 	const char *group = event->tp_event->class->system;
1463 	struct trace_kprobe *tk;
1464 
1465 	if (perf_type_tracepoint)
1466 		tk = find_trace_kprobe(pevent, group);
1467 	else
1468 		tk = event->tp_event->data;
1469 	if (!tk)
1470 		return -EINVAL;
1471 
1472 	*fd_type = trace_kprobe_is_return(tk) ? BPF_FD_TYPE_KRETPROBE
1473 					      : BPF_FD_TYPE_KPROBE;
1474 	if (tk->symbol) {
1475 		*symbol = tk->symbol;
1476 		*probe_offset = tk->rp.kp.offset;
1477 		*probe_addr = 0;
1478 	} else {
1479 		*symbol = NULL;
1480 		*probe_offset = 0;
1481 		*probe_addr = (unsigned long)tk->rp.kp.addr;
1482 	}
1483 	return 0;
1484 }
1485 #endif	/* CONFIG_PERF_EVENTS */
1486 
1487 /*
1488  * called by perf_trace_init() or __ftrace_set_clr_event() under event_mutex.
1489  *
1490  * kprobe_trace_self_tests_init() does enable_trace_probe/disable_trace_probe
1491  * lockless, but we can't race with this __init function.
1492  */
1493 static int kprobe_register(struct trace_event_call *event,
1494 			   enum trace_reg type, void *data)
1495 {
1496 	struct trace_event_file *file = data;
1497 
1498 	switch (type) {
1499 	case TRACE_REG_REGISTER:
1500 		return enable_trace_kprobe(event, file);
1501 	case TRACE_REG_UNREGISTER:
1502 		return disable_trace_kprobe(event, file);
1503 
1504 #ifdef CONFIG_PERF_EVENTS
1505 	case TRACE_REG_PERF_REGISTER:
1506 		return enable_trace_kprobe(event, NULL);
1507 	case TRACE_REG_PERF_UNREGISTER:
1508 		return disable_trace_kprobe(event, NULL);
1509 	case TRACE_REG_PERF_OPEN:
1510 	case TRACE_REG_PERF_CLOSE:
1511 	case TRACE_REG_PERF_ADD:
1512 	case TRACE_REG_PERF_DEL:
1513 		return 0;
1514 #endif
1515 	}
1516 	return 0;
1517 }
1518 
1519 static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
1520 {
1521 	struct trace_kprobe *tk = container_of(kp, struct trace_kprobe, rp.kp);
1522 	int ret = 0;
1523 
1524 	raw_cpu_inc(*tk->nhit);
1525 
1526 	if (trace_probe_test_flag(&tk->tp, TP_FLAG_TRACE))
1527 		kprobe_trace_func(tk, regs);
1528 #ifdef CONFIG_PERF_EVENTS
1529 	if (trace_probe_test_flag(&tk->tp, TP_FLAG_PROFILE))
1530 		ret = kprobe_perf_func(tk, regs);
1531 #endif
1532 	return ret;
1533 }
1534 NOKPROBE_SYMBOL(kprobe_dispatcher);
1535 
1536 static int
1537 kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
1538 {
1539 	struct trace_kprobe *tk = container_of(ri->rp, struct trace_kprobe, rp);
1540 
1541 	raw_cpu_inc(*tk->nhit);
1542 
1543 	if (trace_probe_test_flag(&tk->tp, TP_FLAG_TRACE))
1544 		kretprobe_trace_func(tk, ri, regs);
1545 #ifdef CONFIG_PERF_EVENTS
1546 	if (trace_probe_test_flag(&tk->tp, TP_FLAG_PROFILE))
1547 		kretprobe_perf_func(tk, ri, regs);
1548 #endif
1549 	return 0;	/* We don't tweek kernel, so just return 0 */
1550 }
1551 NOKPROBE_SYMBOL(kretprobe_dispatcher);
1552 
1553 static struct trace_event_functions kretprobe_funcs = {
1554 	.trace		= print_kretprobe_event
1555 };
1556 
1557 static struct trace_event_functions kprobe_funcs = {
1558 	.trace		= print_kprobe_event
1559 };
1560 
1561 static inline void init_trace_event_call(struct trace_kprobe *tk)
1562 {
1563 	struct trace_event_call *call = trace_probe_event_call(&tk->tp);
1564 
1565 	if (trace_kprobe_is_return(tk)) {
1566 		call->event.funcs = &kretprobe_funcs;
1567 		call->class->define_fields = kretprobe_event_define_fields;
1568 	} else {
1569 		call->event.funcs = &kprobe_funcs;
1570 		call->class->define_fields = kprobe_event_define_fields;
1571 	}
1572 
1573 	call->flags = TRACE_EVENT_FL_KPROBE;
1574 	call->class->reg = kprobe_register;
1575 }
1576 
1577 static int register_kprobe_event(struct trace_kprobe *tk)
1578 {
1579 	init_trace_event_call(tk);
1580 
1581 	return trace_probe_register_event_call(&tk->tp);
1582 }
1583 
1584 static int unregister_kprobe_event(struct trace_kprobe *tk)
1585 {
1586 	return trace_probe_unregister_event_call(&tk->tp);
1587 }
1588 
1589 #ifdef CONFIG_PERF_EVENTS
1590 /* create a trace_kprobe, but don't add it to global lists */
1591 struct trace_event_call *
1592 create_local_trace_kprobe(char *func, void *addr, unsigned long offs,
1593 			  bool is_return)
1594 {
1595 	struct trace_kprobe *tk;
1596 	int ret;
1597 	char *event;
1598 
1599 	/*
1600 	 * local trace_kprobes are not added to dyn_event, so they are never
1601 	 * searched in find_trace_kprobe(). Therefore, there is no concern of
1602 	 * duplicated name here.
1603 	 */
1604 	event = func ? func : "DUMMY_EVENT";
1605 
1606 	tk = alloc_trace_kprobe(KPROBE_EVENT_SYSTEM, event, (void *)addr, func,
1607 				offs, 0 /* maxactive */, 0 /* nargs */,
1608 				is_return);
1609 
1610 	if (IS_ERR(tk)) {
1611 		pr_info("Failed to allocate trace_probe.(%d)\n",
1612 			(int)PTR_ERR(tk));
1613 		return ERR_CAST(tk);
1614 	}
1615 
1616 	init_trace_event_call(tk);
1617 
1618 	if (traceprobe_set_print_fmt(&tk->tp, trace_kprobe_is_return(tk)) < 0) {
1619 		ret = -ENOMEM;
1620 		goto error;
1621 	}
1622 
1623 	ret = __register_trace_kprobe(tk);
1624 	if (ret < 0)
1625 		goto error;
1626 
1627 	return trace_probe_event_call(&tk->tp);
1628 error:
1629 	free_trace_kprobe(tk);
1630 	return ERR_PTR(ret);
1631 }
1632 
1633 void destroy_local_trace_kprobe(struct trace_event_call *event_call)
1634 {
1635 	struct trace_kprobe *tk;
1636 
1637 	tk = trace_kprobe_primary_from_call(event_call);
1638 	if (unlikely(!tk))
1639 		return;
1640 
1641 	if (trace_probe_is_enabled(&tk->tp)) {
1642 		WARN_ON(1);
1643 		return;
1644 	}
1645 
1646 	__unregister_trace_kprobe(tk);
1647 
1648 	free_trace_kprobe(tk);
1649 }
1650 #endif /* CONFIG_PERF_EVENTS */
1651 
1652 static __init void enable_boot_kprobe_events(void)
1653 {
1654 	struct trace_array *tr = top_trace_array();
1655 	struct trace_event_file *file;
1656 	struct trace_kprobe *tk;
1657 	struct dyn_event *pos;
1658 
1659 	mutex_lock(&event_mutex);
1660 	for_each_trace_kprobe(tk, pos) {
1661 		list_for_each_entry(file, &tr->events, list)
1662 			if (file->event_call == trace_probe_event_call(&tk->tp))
1663 				trace_event_enable_disable(file, 1, 0);
1664 	}
1665 	mutex_unlock(&event_mutex);
1666 }
1667 
1668 static __init void setup_boot_kprobe_events(void)
1669 {
1670 	char *p, *cmd = kprobe_boot_events_buf;
1671 	int ret;
1672 
1673 	strreplace(kprobe_boot_events_buf, ',', ' ');
1674 
1675 	while (cmd && *cmd != '\0') {
1676 		p = strchr(cmd, ';');
1677 		if (p)
1678 			*p++ = '\0';
1679 
1680 		ret = trace_run_command(cmd, create_or_delete_trace_kprobe);
1681 		if (ret)
1682 			pr_warn("Failed to add event(%d): %s\n", ret, cmd);
1683 		else
1684 			kprobe_boot_events_enabled = true;
1685 
1686 		cmd = p;
1687 	}
1688 
1689 	enable_boot_kprobe_events();
1690 }
1691 
1692 /*
1693  * Register dynevent at subsys_initcall. This allows kernel to setup kprobe
1694  * events in fs_initcall without tracefs.
1695  */
1696 static __init int init_kprobe_trace_early(void)
1697 {
1698 	int ret;
1699 
1700 	ret = dyn_event_register(&trace_kprobe_ops);
1701 	if (ret)
1702 		return ret;
1703 
1704 	if (register_module_notifier(&trace_kprobe_module_nb))
1705 		return -EINVAL;
1706 
1707 	return 0;
1708 }
1709 subsys_initcall(init_kprobe_trace_early);
1710 
1711 /* Make a tracefs interface for controlling probe points */
1712 static __init int init_kprobe_trace(void)
1713 {
1714 	struct dentry *d_tracer;
1715 	struct dentry *entry;
1716 
1717 	d_tracer = tracing_init_dentry();
1718 	if (IS_ERR(d_tracer))
1719 		return 0;
1720 
1721 	entry = tracefs_create_file("kprobe_events", 0644, d_tracer,
1722 				    NULL, &kprobe_events_ops);
1723 
1724 	/* Event list interface */
1725 	if (!entry)
1726 		pr_warn("Could not create tracefs 'kprobe_events' entry\n");
1727 
1728 	/* Profile interface */
1729 	entry = tracefs_create_file("kprobe_profile", 0444, d_tracer,
1730 				    NULL, &kprobe_profile_ops);
1731 
1732 	if (!entry)
1733 		pr_warn("Could not create tracefs 'kprobe_profile' entry\n");
1734 
1735 	setup_boot_kprobe_events();
1736 
1737 	return 0;
1738 }
1739 fs_initcall(init_kprobe_trace);
1740 
1741 
1742 #ifdef CONFIG_FTRACE_STARTUP_TEST
1743 static __init struct trace_event_file *
1744 find_trace_probe_file(struct trace_kprobe *tk, struct trace_array *tr)
1745 {
1746 	struct trace_event_file *file;
1747 
1748 	list_for_each_entry(file, &tr->events, list)
1749 		if (file->event_call == trace_probe_event_call(&tk->tp))
1750 			return file;
1751 
1752 	return NULL;
1753 }
1754 
1755 /*
1756  * Nobody but us can call enable_trace_kprobe/disable_trace_kprobe at this
1757  * stage, we can do this lockless.
1758  */
1759 static __init int kprobe_trace_self_tests_init(void)
1760 {
1761 	int ret, warn = 0;
1762 	int (*target)(int, int, int, int, int, int);
1763 	struct trace_kprobe *tk;
1764 	struct trace_event_file *file;
1765 
1766 	if (tracing_is_disabled())
1767 		return -ENODEV;
1768 
1769 	if (kprobe_boot_events_enabled) {
1770 		pr_info("Skipping kprobe tests due to kprobe_event on cmdline\n");
1771 		return 0;
1772 	}
1773 
1774 	target = kprobe_trace_selftest_target;
1775 
1776 	pr_info("Testing kprobe tracing: ");
1777 
1778 	ret = trace_run_command("p:testprobe kprobe_trace_selftest_target $stack $stack0 +0($stack)",
1779 				create_or_delete_trace_kprobe);
1780 	if (WARN_ON_ONCE(ret)) {
1781 		pr_warn("error on probing function entry.\n");
1782 		warn++;
1783 	} else {
1784 		/* Enable trace point */
1785 		tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
1786 		if (WARN_ON_ONCE(tk == NULL)) {
1787 			pr_warn("error on getting new probe.\n");
1788 			warn++;
1789 		} else {
1790 			file = find_trace_probe_file(tk, top_trace_array());
1791 			if (WARN_ON_ONCE(file == NULL)) {
1792 				pr_warn("error on getting probe file.\n");
1793 				warn++;
1794 			} else
1795 				enable_trace_kprobe(
1796 					trace_probe_event_call(&tk->tp), file);
1797 		}
1798 	}
1799 
1800 	ret = trace_run_command("r:testprobe2 kprobe_trace_selftest_target $retval",
1801 				create_or_delete_trace_kprobe);
1802 	if (WARN_ON_ONCE(ret)) {
1803 		pr_warn("error on probing function return.\n");
1804 		warn++;
1805 	} else {
1806 		/* Enable trace point */
1807 		tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
1808 		if (WARN_ON_ONCE(tk == NULL)) {
1809 			pr_warn("error on getting 2nd new probe.\n");
1810 			warn++;
1811 		} else {
1812 			file = find_trace_probe_file(tk, top_trace_array());
1813 			if (WARN_ON_ONCE(file == NULL)) {
1814 				pr_warn("error on getting probe file.\n");
1815 				warn++;
1816 			} else
1817 				enable_trace_kprobe(
1818 					trace_probe_event_call(&tk->tp), file);
1819 		}
1820 	}
1821 
1822 	if (warn)
1823 		goto end;
1824 
1825 	ret = target(1, 2, 3, 4, 5, 6);
1826 
1827 	/*
1828 	 * Not expecting an error here, the check is only to prevent the
1829 	 * optimizer from removing the call to target() as otherwise there
1830 	 * are no side-effects and the call is never performed.
1831 	 */
1832 	if (ret != 21)
1833 		warn++;
1834 
1835 	/* Disable trace points before removing it */
1836 	tk = find_trace_kprobe("testprobe", KPROBE_EVENT_SYSTEM);
1837 	if (WARN_ON_ONCE(tk == NULL)) {
1838 		pr_warn("error on getting test probe.\n");
1839 		warn++;
1840 	} else {
1841 		if (trace_kprobe_nhit(tk) != 1) {
1842 			pr_warn("incorrect number of testprobe hits\n");
1843 			warn++;
1844 		}
1845 
1846 		file = find_trace_probe_file(tk, top_trace_array());
1847 		if (WARN_ON_ONCE(file == NULL)) {
1848 			pr_warn("error on getting probe file.\n");
1849 			warn++;
1850 		} else
1851 			disable_trace_kprobe(
1852 				trace_probe_event_call(&tk->tp), file);
1853 	}
1854 
1855 	tk = find_trace_kprobe("testprobe2", KPROBE_EVENT_SYSTEM);
1856 	if (WARN_ON_ONCE(tk == NULL)) {
1857 		pr_warn("error on getting 2nd test probe.\n");
1858 		warn++;
1859 	} else {
1860 		if (trace_kprobe_nhit(tk) != 1) {
1861 			pr_warn("incorrect number of testprobe2 hits\n");
1862 			warn++;
1863 		}
1864 
1865 		file = find_trace_probe_file(tk, top_trace_array());
1866 		if (WARN_ON_ONCE(file == NULL)) {
1867 			pr_warn("error on getting probe file.\n");
1868 			warn++;
1869 		} else
1870 			disable_trace_kprobe(
1871 				trace_probe_event_call(&tk->tp), file);
1872 	}
1873 
1874 	ret = trace_run_command("-:testprobe", create_or_delete_trace_kprobe);
1875 	if (WARN_ON_ONCE(ret)) {
1876 		pr_warn("error on deleting a probe.\n");
1877 		warn++;
1878 	}
1879 
1880 	ret = trace_run_command("-:testprobe2", create_or_delete_trace_kprobe);
1881 	if (WARN_ON_ONCE(ret)) {
1882 		pr_warn("error on deleting a probe.\n");
1883 		warn++;
1884 	}
1885 
1886 end:
1887 	ret = dyn_events_release_all(&trace_kprobe_ops);
1888 	if (WARN_ON_ONCE(ret)) {
1889 		pr_warn("error on cleaning up probes.\n");
1890 		warn++;
1891 	}
1892 	/*
1893 	 * Wait for the optimizer work to finish. Otherwise it might fiddle
1894 	 * with probes in already freed __init text.
1895 	 */
1896 	wait_for_kprobe_optimizer();
1897 	if (warn)
1898 		pr_cont("NG: Some tests are failed. Please check them.\n");
1899 	else
1900 		pr_cont("OK\n");
1901 	return 0;
1902 }
1903 
1904 late_initcall(kprobe_trace_self_tests_init);
1905 
1906 #endif
1907