xref: /openbmc/linux/kernel/trace/trace_fprobe.c (revision 0e9a6b8a)
1334e5519SMasami Hiramatsu (Google) // SPDX-License-Identifier: GPL-2.0
2334e5519SMasami Hiramatsu (Google) /*
3334e5519SMasami Hiramatsu (Google)  * Fprobe-based tracing events
4334e5519SMasami Hiramatsu (Google)  * Copyright (C) 2022 Google LLC.
5334e5519SMasami Hiramatsu (Google)  */
6334e5519SMasami Hiramatsu (Google) #define pr_fmt(fmt)	"trace_fprobe: " fmt
7334e5519SMasami Hiramatsu (Google) 
8334e5519SMasami Hiramatsu (Google) #include <linux/fprobe.h>
9334e5519SMasami Hiramatsu (Google) #include <linux/module.h>
10334e5519SMasami Hiramatsu (Google) #include <linux/rculist.h>
11334e5519SMasami Hiramatsu (Google) #include <linux/security.h>
12e2d0d7b2SMasami Hiramatsu (Google) #include <linux/tracepoint.h>
13334e5519SMasami Hiramatsu (Google) #include <linux/uaccess.h>
14334e5519SMasami Hiramatsu (Google) 
15334e5519SMasami Hiramatsu (Google) #include "trace_dynevent.h"
16334e5519SMasami Hiramatsu (Google) #include "trace_probe.h"
17334e5519SMasami Hiramatsu (Google) #include "trace_probe_kernel.h"
18334e5519SMasami Hiramatsu (Google) #include "trace_probe_tmpl.h"
19334e5519SMasami Hiramatsu (Google) 
20334e5519SMasami Hiramatsu (Google) #define FPROBE_EVENT_SYSTEM "fprobes"
21e2d0d7b2SMasami Hiramatsu (Google) #define TRACEPOINT_EVENT_SYSTEM "tracepoints"
22334e5519SMasami Hiramatsu (Google) #define RETHOOK_MAXACTIVE_MAX 4096
23334e5519SMasami Hiramatsu (Google) 
24334e5519SMasami Hiramatsu (Google) static int trace_fprobe_create(const char *raw_command);
25334e5519SMasami Hiramatsu (Google) static int trace_fprobe_show(struct seq_file *m, struct dyn_event *ev);
26334e5519SMasami Hiramatsu (Google) static int trace_fprobe_release(struct dyn_event *ev);
27334e5519SMasami Hiramatsu (Google) static bool trace_fprobe_is_busy(struct dyn_event *ev);
28334e5519SMasami Hiramatsu (Google) static bool trace_fprobe_match(const char *system, const char *event,
29334e5519SMasami Hiramatsu (Google) 			int argc, const char **argv, struct dyn_event *ev);
30334e5519SMasami Hiramatsu (Google) 
31334e5519SMasami Hiramatsu (Google) static struct dyn_event_operations trace_fprobe_ops = {
32334e5519SMasami Hiramatsu (Google) 	.create = trace_fprobe_create,
33334e5519SMasami Hiramatsu (Google) 	.show = trace_fprobe_show,
34334e5519SMasami Hiramatsu (Google) 	.is_busy = trace_fprobe_is_busy,
35334e5519SMasami Hiramatsu (Google) 	.free = trace_fprobe_release,
36334e5519SMasami Hiramatsu (Google) 	.match = trace_fprobe_match,
37334e5519SMasami Hiramatsu (Google) };
38334e5519SMasami Hiramatsu (Google) 
39334e5519SMasami Hiramatsu (Google) /*
40334e5519SMasami Hiramatsu (Google)  * Fprobe event core functions
41334e5519SMasami Hiramatsu (Google)  */
42334e5519SMasami Hiramatsu (Google) struct trace_fprobe {
43334e5519SMasami Hiramatsu (Google) 	struct dyn_event	devent;
44334e5519SMasami Hiramatsu (Google) 	struct fprobe		fp;
45334e5519SMasami Hiramatsu (Google) 	const char		*symbol;
46e2d0d7b2SMasami Hiramatsu (Google) 	struct tracepoint	*tpoint;
47e2d0d7b2SMasami Hiramatsu (Google) 	struct module		*mod;
48334e5519SMasami Hiramatsu (Google) 	struct trace_probe	tp;
49334e5519SMasami Hiramatsu (Google) };
50334e5519SMasami Hiramatsu (Google) 
is_trace_fprobe(struct dyn_event * ev)51334e5519SMasami Hiramatsu (Google) static bool is_trace_fprobe(struct dyn_event *ev)
52334e5519SMasami Hiramatsu (Google) {
53334e5519SMasami Hiramatsu (Google) 	return ev->ops == &trace_fprobe_ops;
54334e5519SMasami Hiramatsu (Google) }
55334e5519SMasami Hiramatsu (Google) 
to_trace_fprobe(struct dyn_event * ev)56334e5519SMasami Hiramatsu (Google) static struct trace_fprobe *to_trace_fprobe(struct dyn_event *ev)
57334e5519SMasami Hiramatsu (Google) {
58334e5519SMasami Hiramatsu (Google) 	return container_of(ev, struct trace_fprobe, devent);
59334e5519SMasami Hiramatsu (Google) }
60334e5519SMasami Hiramatsu (Google) 
61334e5519SMasami Hiramatsu (Google) /**
62334e5519SMasami Hiramatsu (Google)  * for_each_trace_fprobe - iterate over the trace_fprobe list
63334e5519SMasami Hiramatsu (Google)  * @pos:	the struct trace_fprobe * for each entry
64334e5519SMasami Hiramatsu (Google)  * @dpos:	the struct dyn_event * to use as a loop cursor
65334e5519SMasami Hiramatsu (Google)  */
66334e5519SMasami Hiramatsu (Google) #define for_each_trace_fprobe(pos, dpos)	\
67334e5519SMasami Hiramatsu (Google) 	for_each_dyn_event(dpos)		\
68334e5519SMasami Hiramatsu (Google) 		if (is_trace_fprobe(dpos) && (pos = to_trace_fprobe(dpos)))
69334e5519SMasami Hiramatsu (Google) 
trace_fprobe_is_return(struct trace_fprobe * tf)70334e5519SMasami Hiramatsu (Google) static bool trace_fprobe_is_return(struct trace_fprobe *tf)
71334e5519SMasami Hiramatsu (Google) {
72334e5519SMasami Hiramatsu (Google) 	return tf->fp.exit_handler != NULL;
73334e5519SMasami Hiramatsu (Google) }
74334e5519SMasami Hiramatsu (Google) 
trace_fprobe_is_tracepoint(struct trace_fprobe * tf)75e2d0d7b2SMasami Hiramatsu (Google) static bool trace_fprobe_is_tracepoint(struct trace_fprobe *tf)
76e2d0d7b2SMasami Hiramatsu (Google) {
77e2d0d7b2SMasami Hiramatsu (Google) 	return tf->tpoint != NULL;
78e2d0d7b2SMasami Hiramatsu (Google) }
79e2d0d7b2SMasami Hiramatsu (Google) 
trace_fprobe_symbol(struct trace_fprobe * tf)80334e5519SMasami Hiramatsu (Google) static const char *trace_fprobe_symbol(struct trace_fprobe *tf)
81334e5519SMasami Hiramatsu (Google) {
82334e5519SMasami Hiramatsu (Google) 	return tf->symbol ? tf->symbol : "unknown";
83334e5519SMasami Hiramatsu (Google) }
84334e5519SMasami Hiramatsu (Google) 
trace_fprobe_is_busy(struct dyn_event * ev)85334e5519SMasami Hiramatsu (Google) static bool trace_fprobe_is_busy(struct dyn_event *ev)
86334e5519SMasami Hiramatsu (Google) {
87334e5519SMasami Hiramatsu (Google) 	struct trace_fprobe *tf = to_trace_fprobe(ev);
88334e5519SMasami Hiramatsu (Google) 
89334e5519SMasami Hiramatsu (Google) 	return trace_probe_is_enabled(&tf->tp);
90334e5519SMasami Hiramatsu (Google) }
91334e5519SMasami Hiramatsu (Google) 
trace_fprobe_match_command_head(struct trace_fprobe * tf,int argc,const char ** argv)92334e5519SMasami Hiramatsu (Google) static bool trace_fprobe_match_command_head(struct trace_fprobe *tf,
93334e5519SMasami Hiramatsu (Google) 					    int argc, const char **argv)
94334e5519SMasami Hiramatsu (Google) {
95334e5519SMasami Hiramatsu (Google) 	char buf[MAX_ARGSTR_LEN + 1];
96334e5519SMasami Hiramatsu (Google) 
97334e5519SMasami Hiramatsu (Google) 	if (!argc)
98334e5519SMasami Hiramatsu (Google) 		return true;
99334e5519SMasami Hiramatsu (Google) 
100334e5519SMasami Hiramatsu (Google) 	snprintf(buf, sizeof(buf), "%s", trace_fprobe_symbol(tf));
101334e5519SMasami Hiramatsu (Google) 	if (strcmp(buf, argv[0]))
102334e5519SMasami Hiramatsu (Google) 		return false;
103334e5519SMasami Hiramatsu (Google) 	argc--; argv++;
104334e5519SMasami Hiramatsu (Google) 
105334e5519SMasami Hiramatsu (Google) 	return trace_probe_match_command_args(&tf->tp, argc, argv);
106334e5519SMasami Hiramatsu (Google) }
107334e5519SMasami Hiramatsu (Google) 
trace_fprobe_match(const char * system,const char * event,int argc,const char ** argv,struct dyn_event * ev)108334e5519SMasami Hiramatsu (Google) static bool trace_fprobe_match(const char *system, const char *event,
109334e5519SMasami Hiramatsu (Google) 			int argc, const char **argv, struct dyn_event *ev)
110334e5519SMasami Hiramatsu (Google) {
111334e5519SMasami Hiramatsu (Google) 	struct trace_fprobe *tf = to_trace_fprobe(ev);
112334e5519SMasami Hiramatsu (Google) 
113334e5519SMasami Hiramatsu (Google) 	if (event[0] != '\0' && strcmp(trace_probe_name(&tf->tp), event))
114334e5519SMasami Hiramatsu (Google) 		return false;
115334e5519SMasami Hiramatsu (Google) 
116334e5519SMasami Hiramatsu (Google) 	if (system && strcmp(trace_probe_group_name(&tf->tp), system))
117334e5519SMasami Hiramatsu (Google) 		return false;
118334e5519SMasami Hiramatsu (Google) 
119334e5519SMasami Hiramatsu (Google) 	return trace_fprobe_match_command_head(tf, argc, argv);
120334e5519SMasami Hiramatsu (Google) }
121334e5519SMasami Hiramatsu (Google) 
trace_fprobe_is_registered(struct trace_fprobe * tf)122334e5519SMasami Hiramatsu (Google) static bool trace_fprobe_is_registered(struct trace_fprobe *tf)
123334e5519SMasami Hiramatsu (Google) {
124334e5519SMasami Hiramatsu (Google) 	return fprobe_is_registered(&tf->fp);
125334e5519SMasami Hiramatsu (Google) }
126334e5519SMasami Hiramatsu (Google) 
127334e5519SMasami Hiramatsu (Google) /*
128334e5519SMasami Hiramatsu (Google)  * Note that we don't verify the fetch_insn code, since it does not come
129334e5519SMasami Hiramatsu (Google)  * from user space.
130334e5519SMasami Hiramatsu (Google)  */
131334e5519SMasami Hiramatsu (Google) static int
process_fetch_insn(struct fetch_insn * code,void * rec,void * dest,void * base)132334e5519SMasami Hiramatsu (Google) process_fetch_insn(struct fetch_insn *code, void *rec, void *dest,
133334e5519SMasami Hiramatsu (Google) 		   void *base)
134334e5519SMasami Hiramatsu (Google) {
135334e5519SMasami Hiramatsu (Google) 	struct pt_regs *regs = rec;
136334e5519SMasami Hiramatsu (Google) 	unsigned long val;
137334e5519SMasami Hiramatsu (Google) 	int ret;
138334e5519SMasami Hiramatsu (Google) 
139334e5519SMasami Hiramatsu (Google) retry:
140334e5519SMasami Hiramatsu (Google) 	/* 1st stage: get value from context */
141334e5519SMasami Hiramatsu (Google) 	switch (code->op) {
142334e5519SMasami Hiramatsu (Google) 	case FETCH_OP_STACK:
143334e5519SMasami Hiramatsu (Google) 		val = regs_get_kernel_stack_nth(regs, code->param);
144334e5519SMasami Hiramatsu (Google) 		break;
145334e5519SMasami Hiramatsu (Google) 	case FETCH_OP_STACKP:
146334e5519SMasami Hiramatsu (Google) 		val = kernel_stack_pointer(regs);
147334e5519SMasami Hiramatsu (Google) 		break;
148334e5519SMasami Hiramatsu (Google) 	case FETCH_OP_RETVAL:
149334e5519SMasami Hiramatsu (Google) 		val = regs_return_value(regs);
150334e5519SMasami Hiramatsu (Google) 		break;
151334e5519SMasami Hiramatsu (Google) #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API
152334e5519SMasami Hiramatsu (Google) 	case FETCH_OP_ARG:
153334e5519SMasami Hiramatsu (Google) 		val = regs_get_kernel_argument(regs, code->param);
154334e5519SMasami Hiramatsu (Google) 		break;
155334e5519SMasami Hiramatsu (Google) #endif
156334e5519SMasami Hiramatsu (Google) 	case FETCH_NOP_SYMBOL:	/* Ignore a place holder */
157334e5519SMasami Hiramatsu (Google) 		code++;
158334e5519SMasami Hiramatsu (Google) 		goto retry;
159334e5519SMasami Hiramatsu (Google) 	default:
160334e5519SMasami Hiramatsu (Google) 		ret = process_common_fetch_insn(code, &val);
161334e5519SMasami Hiramatsu (Google) 		if (ret < 0)
162334e5519SMasami Hiramatsu (Google) 			return ret;
163334e5519SMasami Hiramatsu (Google) 	}
164334e5519SMasami Hiramatsu (Google) 	code++;
165334e5519SMasami Hiramatsu (Google) 
166334e5519SMasami Hiramatsu (Google) 	return process_fetch_insn_bottom(code, val, dest, base);
167334e5519SMasami Hiramatsu (Google) }
NOKPROBE_SYMBOL(process_fetch_insn)168334e5519SMasami Hiramatsu (Google) NOKPROBE_SYMBOL(process_fetch_insn)
169334e5519SMasami Hiramatsu (Google) 
170334e5519SMasami Hiramatsu (Google) /* function entry handler */
171334e5519SMasami Hiramatsu (Google) static nokprobe_inline void
172334e5519SMasami Hiramatsu (Google) __fentry_trace_func(struct trace_fprobe *tf, unsigned long entry_ip,
173334e5519SMasami Hiramatsu (Google) 		    struct pt_regs *regs,
174334e5519SMasami Hiramatsu (Google) 		    struct trace_event_file *trace_file)
175334e5519SMasami Hiramatsu (Google) {
176334e5519SMasami Hiramatsu (Google) 	struct fentry_trace_entry_head *entry;
177334e5519SMasami Hiramatsu (Google) 	struct trace_event_call *call = trace_probe_event_call(&tf->tp);
178334e5519SMasami Hiramatsu (Google) 	struct trace_event_buffer fbuffer;
179334e5519SMasami Hiramatsu (Google) 	int dsize;
180334e5519SMasami Hiramatsu (Google) 
181334e5519SMasami Hiramatsu (Google) 	if (WARN_ON_ONCE(call != trace_file->event_call))
182334e5519SMasami Hiramatsu (Google) 		return;
183334e5519SMasami Hiramatsu (Google) 
184334e5519SMasami Hiramatsu (Google) 	if (trace_trigger_soft_disabled(trace_file))
185334e5519SMasami Hiramatsu (Google) 		return;
186334e5519SMasami Hiramatsu (Google) 
187334e5519SMasami Hiramatsu (Google) 	dsize = __get_data_size(&tf->tp, regs);
188334e5519SMasami Hiramatsu (Google) 
189334e5519SMasami Hiramatsu (Google) 	entry = trace_event_buffer_reserve(&fbuffer, trace_file,
190334e5519SMasami Hiramatsu (Google) 					   sizeof(*entry) + tf->tp.size + dsize);
191334e5519SMasami Hiramatsu (Google) 	if (!entry)
192334e5519SMasami Hiramatsu (Google) 		return;
193334e5519SMasami Hiramatsu (Google) 
194334e5519SMasami Hiramatsu (Google) 	fbuffer.regs = regs;
195334e5519SMasami Hiramatsu (Google) 	entry = fbuffer.entry = ring_buffer_event_data(fbuffer.event);
196334e5519SMasami Hiramatsu (Google) 	entry->ip = entry_ip;
197334e5519SMasami Hiramatsu (Google) 	store_trace_args(&entry[1], &tf->tp, regs, sizeof(*entry), dsize);
198334e5519SMasami Hiramatsu (Google) 
199334e5519SMasami Hiramatsu (Google) 	trace_event_buffer_commit(&fbuffer);
200334e5519SMasami Hiramatsu (Google) }
201334e5519SMasami Hiramatsu (Google) 
202334e5519SMasami Hiramatsu (Google) static void
fentry_trace_func(struct trace_fprobe * tf,unsigned long entry_ip,struct pt_regs * regs)203334e5519SMasami Hiramatsu (Google) fentry_trace_func(struct trace_fprobe *tf, unsigned long entry_ip,
204334e5519SMasami Hiramatsu (Google) 		  struct pt_regs *regs)
205334e5519SMasami Hiramatsu (Google) {
206334e5519SMasami Hiramatsu (Google) 	struct event_file_link *link;
207334e5519SMasami Hiramatsu (Google) 
208334e5519SMasami Hiramatsu (Google) 	trace_probe_for_each_link_rcu(link, &tf->tp)
209334e5519SMasami Hiramatsu (Google) 		__fentry_trace_func(tf, entry_ip, regs, link->file);
210334e5519SMasami Hiramatsu (Google) }
211334e5519SMasami Hiramatsu (Google) NOKPROBE_SYMBOL(fentry_trace_func);
212334e5519SMasami Hiramatsu (Google) 
213334e5519SMasami Hiramatsu (Google) /* Kretprobe handler */
214334e5519SMasami Hiramatsu (Google) static nokprobe_inline void
__fexit_trace_func(struct trace_fprobe * tf,unsigned long entry_ip,unsigned long ret_ip,struct pt_regs * regs,struct trace_event_file * trace_file)215334e5519SMasami Hiramatsu (Google) __fexit_trace_func(struct trace_fprobe *tf, unsigned long entry_ip,
216334e5519SMasami Hiramatsu (Google) 		   unsigned long ret_ip, struct pt_regs *regs,
217334e5519SMasami Hiramatsu (Google) 		   struct trace_event_file *trace_file)
218334e5519SMasami Hiramatsu (Google) {
219334e5519SMasami Hiramatsu (Google) 	struct fexit_trace_entry_head *entry;
220334e5519SMasami Hiramatsu (Google) 	struct trace_event_buffer fbuffer;
221334e5519SMasami Hiramatsu (Google) 	struct trace_event_call *call = trace_probe_event_call(&tf->tp);
222334e5519SMasami Hiramatsu (Google) 	int dsize;
223334e5519SMasami Hiramatsu (Google) 
224334e5519SMasami Hiramatsu (Google) 	if (WARN_ON_ONCE(call != trace_file->event_call))
225334e5519SMasami Hiramatsu (Google) 		return;
226334e5519SMasami Hiramatsu (Google) 
227334e5519SMasami Hiramatsu (Google) 	if (trace_trigger_soft_disabled(trace_file))
228334e5519SMasami Hiramatsu (Google) 		return;
229334e5519SMasami Hiramatsu (Google) 
230334e5519SMasami Hiramatsu (Google) 	dsize = __get_data_size(&tf->tp, regs);
231334e5519SMasami Hiramatsu (Google) 
232334e5519SMasami Hiramatsu (Google) 	entry = trace_event_buffer_reserve(&fbuffer, trace_file,
233334e5519SMasami Hiramatsu (Google) 					   sizeof(*entry) + tf->tp.size + dsize);
234334e5519SMasami Hiramatsu (Google) 	if (!entry)
235334e5519SMasami Hiramatsu (Google) 		return;
236334e5519SMasami Hiramatsu (Google) 
237334e5519SMasami Hiramatsu (Google) 	fbuffer.regs = regs;
238334e5519SMasami Hiramatsu (Google) 	entry = fbuffer.entry = ring_buffer_event_data(fbuffer.event);
239334e5519SMasami Hiramatsu (Google) 	entry->func = entry_ip;
240334e5519SMasami Hiramatsu (Google) 	entry->ret_ip = ret_ip;
241334e5519SMasami Hiramatsu (Google) 	store_trace_args(&entry[1], &tf->tp, regs, sizeof(*entry), dsize);
242334e5519SMasami Hiramatsu (Google) 
243334e5519SMasami Hiramatsu (Google) 	trace_event_buffer_commit(&fbuffer);
244334e5519SMasami Hiramatsu (Google) }
245334e5519SMasami Hiramatsu (Google) 
246334e5519SMasami Hiramatsu (Google) static void
fexit_trace_func(struct trace_fprobe * tf,unsigned long entry_ip,unsigned long ret_ip,struct pt_regs * regs)247334e5519SMasami Hiramatsu (Google) fexit_trace_func(struct trace_fprobe *tf, unsigned long entry_ip,
248334e5519SMasami Hiramatsu (Google) 		 unsigned long ret_ip, struct pt_regs *regs)
249334e5519SMasami Hiramatsu (Google) {
250334e5519SMasami Hiramatsu (Google) 	struct event_file_link *link;
251334e5519SMasami Hiramatsu (Google) 
252334e5519SMasami Hiramatsu (Google) 	trace_probe_for_each_link_rcu(link, &tf->tp)
253334e5519SMasami Hiramatsu (Google) 		__fexit_trace_func(tf, entry_ip, ret_ip, regs, link->file);
254334e5519SMasami Hiramatsu (Google) }
255334e5519SMasami Hiramatsu (Google) NOKPROBE_SYMBOL(fexit_trace_func);
256334e5519SMasami Hiramatsu (Google) 
257334e5519SMasami Hiramatsu (Google) #ifdef CONFIG_PERF_EVENTS
258334e5519SMasami Hiramatsu (Google) 
fentry_perf_func(struct trace_fprobe * tf,unsigned long entry_ip,struct pt_regs * regs)259334e5519SMasami Hiramatsu (Google) static int fentry_perf_func(struct trace_fprobe *tf, unsigned long entry_ip,
260334e5519SMasami Hiramatsu (Google) 			    struct pt_regs *regs)
261334e5519SMasami Hiramatsu (Google) {
262334e5519SMasami Hiramatsu (Google) 	struct trace_event_call *call = trace_probe_event_call(&tf->tp);
263334e5519SMasami Hiramatsu (Google) 	struct fentry_trace_entry_head *entry;
264334e5519SMasami Hiramatsu (Google) 	struct hlist_head *head;
265334e5519SMasami Hiramatsu (Google) 	int size, __size, dsize;
266334e5519SMasami Hiramatsu (Google) 	int rctx;
267334e5519SMasami Hiramatsu (Google) 
268334e5519SMasami Hiramatsu (Google) 	head = this_cpu_ptr(call->perf_events);
269334e5519SMasami Hiramatsu (Google) 	if (hlist_empty(head))
270334e5519SMasami Hiramatsu (Google) 		return 0;
271334e5519SMasami Hiramatsu (Google) 
272334e5519SMasami Hiramatsu (Google) 	dsize = __get_data_size(&tf->tp, regs);
273334e5519SMasami Hiramatsu (Google) 	__size = sizeof(*entry) + tf->tp.size + dsize;
274334e5519SMasami Hiramatsu (Google) 	size = ALIGN(__size + sizeof(u32), sizeof(u64));
275334e5519SMasami Hiramatsu (Google) 	size -= sizeof(u32);
276334e5519SMasami Hiramatsu (Google) 
277334e5519SMasami Hiramatsu (Google) 	entry = perf_trace_buf_alloc(size, NULL, &rctx);
278334e5519SMasami Hiramatsu (Google) 	if (!entry)
279334e5519SMasami Hiramatsu (Google) 		return 0;
280334e5519SMasami Hiramatsu (Google) 
281334e5519SMasami Hiramatsu (Google) 	entry->ip = entry_ip;
282334e5519SMasami Hiramatsu (Google) 	memset(&entry[1], 0, dsize);
283334e5519SMasami Hiramatsu (Google) 	store_trace_args(&entry[1], &tf->tp, regs, sizeof(*entry), dsize);
284334e5519SMasami Hiramatsu (Google) 	perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
285334e5519SMasami Hiramatsu (Google) 			      head, NULL);
286334e5519SMasami Hiramatsu (Google) 	return 0;
287334e5519SMasami Hiramatsu (Google) }
288334e5519SMasami Hiramatsu (Google) NOKPROBE_SYMBOL(fentry_perf_func);
289334e5519SMasami Hiramatsu (Google) 
290334e5519SMasami Hiramatsu (Google) static void
fexit_perf_func(struct trace_fprobe * tf,unsigned long entry_ip,unsigned long ret_ip,struct pt_regs * regs)291334e5519SMasami Hiramatsu (Google) fexit_perf_func(struct trace_fprobe *tf, unsigned long entry_ip,
292334e5519SMasami Hiramatsu (Google) 		unsigned long ret_ip, struct pt_regs *regs)
293334e5519SMasami Hiramatsu (Google) {
294334e5519SMasami Hiramatsu (Google) 	struct trace_event_call *call = trace_probe_event_call(&tf->tp);
295334e5519SMasami Hiramatsu (Google) 	struct fexit_trace_entry_head *entry;
296334e5519SMasami Hiramatsu (Google) 	struct hlist_head *head;
297334e5519SMasami Hiramatsu (Google) 	int size, __size, dsize;
298334e5519SMasami Hiramatsu (Google) 	int rctx;
299334e5519SMasami Hiramatsu (Google) 
300334e5519SMasami Hiramatsu (Google) 	head = this_cpu_ptr(call->perf_events);
301334e5519SMasami Hiramatsu (Google) 	if (hlist_empty(head))
302334e5519SMasami Hiramatsu (Google) 		return;
303334e5519SMasami Hiramatsu (Google) 
304334e5519SMasami Hiramatsu (Google) 	dsize = __get_data_size(&tf->tp, regs);
305334e5519SMasami Hiramatsu (Google) 	__size = sizeof(*entry) + tf->tp.size + dsize;
306334e5519SMasami Hiramatsu (Google) 	size = ALIGN(__size + sizeof(u32), sizeof(u64));
307334e5519SMasami Hiramatsu (Google) 	size -= sizeof(u32);
308334e5519SMasami Hiramatsu (Google) 
309334e5519SMasami Hiramatsu (Google) 	entry = perf_trace_buf_alloc(size, NULL, &rctx);
310334e5519SMasami Hiramatsu (Google) 	if (!entry)
311334e5519SMasami Hiramatsu (Google) 		return;
312334e5519SMasami Hiramatsu (Google) 
313334e5519SMasami Hiramatsu (Google) 	entry->func = entry_ip;
314334e5519SMasami Hiramatsu (Google) 	entry->ret_ip = ret_ip;
315334e5519SMasami Hiramatsu (Google) 	store_trace_args(&entry[1], &tf->tp, regs, sizeof(*entry), dsize);
316334e5519SMasami Hiramatsu (Google) 	perf_trace_buf_submit(entry, size, rctx, call->event.type, 1, regs,
317334e5519SMasami Hiramatsu (Google) 			      head, NULL);
318334e5519SMasami Hiramatsu (Google) }
319334e5519SMasami Hiramatsu (Google) NOKPROBE_SYMBOL(fexit_perf_func);
320334e5519SMasami Hiramatsu (Google) #endif	/* CONFIG_PERF_EVENTS */
321334e5519SMasami Hiramatsu (Google) 
fentry_dispatcher(struct fprobe * fp,unsigned long entry_ip,unsigned long ret_ip,struct pt_regs * regs,void * entry_data)322334e5519SMasami Hiramatsu (Google) static int fentry_dispatcher(struct fprobe *fp, unsigned long entry_ip,
323334e5519SMasami Hiramatsu (Google) 			     unsigned long ret_ip, struct pt_regs *regs,
324334e5519SMasami Hiramatsu (Google) 			     void *entry_data)
325334e5519SMasami Hiramatsu (Google) {
326334e5519SMasami Hiramatsu (Google) 	struct trace_fprobe *tf = container_of(fp, struct trace_fprobe, fp);
327334e5519SMasami Hiramatsu (Google) 	int ret = 0;
328334e5519SMasami Hiramatsu (Google) 
329334e5519SMasami Hiramatsu (Google) 	if (trace_probe_test_flag(&tf->tp, TP_FLAG_TRACE))
330334e5519SMasami Hiramatsu (Google) 		fentry_trace_func(tf, entry_ip, regs);
331334e5519SMasami Hiramatsu (Google) #ifdef CONFIG_PERF_EVENTS
332334e5519SMasami Hiramatsu (Google) 	if (trace_probe_test_flag(&tf->tp, TP_FLAG_PROFILE))
333334e5519SMasami Hiramatsu (Google) 		ret = fentry_perf_func(tf, entry_ip, regs);
334334e5519SMasami Hiramatsu (Google) #endif
335334e5519SMasami Hiramatsu (Google) 	return ret;
336334e5519SMasami Hiramatsu (Google) }
337334e5519SMasami Hiramatsu (Google) NOKPROBE_SYMBOL(fentry_dispatcher);
338334e5519SMasami Hiramatsu (Google) 
fexit_dispatcher(struct fprobe * fp,unsigned long entry_ip,unsigned long ret_ip,struct pt_regs * regs,void * entry_data)339334e5519SMasami Hiramatsu (Google) static void fexit_dispatcher(struct fprobe *fp, unsigned long entry_ip,
340334e5519SMasami Hiramatsu (Google) 			     unsigned long ret_ip, struct pt_regs *regs,
341334e5519SMasami Hiramatsu (Google) 			     void *entry_data)
342334e5519SMasami Hiramatsu (Google) {
343334e5519SMasami Hiramatsu (Google) 	struct trace_fprobe *tf = container_of(fp, struct trace_fprobe, fp);
344334e5519SMasami Hiramatsu (Google) 
345334e5519SMasami Hiramatsu (Google) 	if (trace_probe_test_flag(&tf->tp, TP_FLAG_TRACE))
346334e5519SMasami Hiramatsu (Google) 		fexit_trace_func(tf, entry_ip, ret_ip, regs);
347334e5519SMasami Hiramatsu (Google) #ifdef CONFIG_PERF_EVENTS
348334e5519SMasami Hiramatsu (Google) 	if (trace_probe_test_flag(&tf->tp, TP_FLAG_PROFILE))
349334e5519SMasami Hiramatsu (Google) 		fexit_perf_func(tf, entry_ip, ret_ip, regs);
350334e5519SMasami Hiramatsu (Google) #endif
351334e5519SMasami Hiramatsu (Google) }
352334e5519SMasami Hiramatsu (Google) NOKPROBE_SYMBOL(fexit_dispatcher);
353334e5519SMasami Hiramatsu (Google) 
free_trace_fprobe(struct trace_fprobe * tf)354334e5519SMasami Hiramatsu (Google) static void free_trace_fprobe(struct trace_fprobe *tf)
355334e5519SMasami Hiramatsu (Google) {
356334e5519SMasami Hiramatsu (Google) 	if (tf) {
357334e5519SMasami Hiramatsu (Google) 		trace_probe_cleanup(&tf->tp);
358334e5519SMasami Hiramatsu (Google) 		kfree(tf->symbol);
359334e5519SMasami Hiramatsu (Google) 		kfree(tf);
360334e5519SMasami Hiramatsu (Google) 	}
361334e5519SMasami Hiramatsu (Google) }
362334e5519SMasami Hiramatsu (Google) 
363334e5519SMasami Hiramatsu (Google) /*
364334e5519SMasami Hiramatsu (Google)  * Allocate new trace_probe and initialize it (including fprobe).
365334e5519SMasami Hiramatsu (Google)  */
alloc_trace_fprobe(const char * group,const char * event,const char * symbol,struct tracepoint * tpoint,int maxactive,int nargs,bool is_return)366334e5519SMasami Hiramatsu (Google) static struct trace_fprobe *alloc_trace_fprobe(const char *group,
367334e5519SMasami Hiramatsu (Google) 					       const char *event,
368334e5519SMasami Hiramatsu (Google) 					       const char *symbol,
369b576e097SMasami Hiramatsu (Google) 					       struct tracepoint *tpoint,
370334e5519SMasami Hiramatsu (Google) 					       int maxactive,
371334e5519SMasami Hiramatsu (Google) 					       int nargs, bool is_return)
372334e5519SMasami Hiramatsu (Google) {
373334e5519SMasami Hiramatsu (Google) 	struct trace_fprobe *tf;
374334e5519SMasami Hiramatsu (Google) 	int ret = -ENOMEM;
375334e5519SMasami Hiramatsu (Google) 
376334e5519SMasami Hiramatsu (Google) 	tf = kzalloc(struct_size(tf, tp.args, nargs), GFP_KERNEL);
377334e5519SMasami Hiramatsu (Google) 	if (!tf)
378334e5519SMasami Hiramatsu (Google) 		return ERR_PTR(ret);
379334e5519SMasami Hiramatsu (Google) 
380334e5519SMasami Hiramatsu (Google) 	tf->symbol = kstrdup(symbol, GFP_KERNEL);
381334e5519SMasami Hiramatsu (Google) 	if (!tf->symbol)
382334e5519SMasami Hiramatsu (Google) 		goto error;
383334e5519SMasami Hiramatsu (Google) 
384334e5519SMasami Hiramatsu (Google) 	if (is_return)
385334e5519SMasami Hiramatsu (Google) 		tf->fp.exit_handler = fexit_dispatcher;
386334e5519SMasami Hiramatsu (Google) 	else
387334e5519SMasami Hiramatsu (Google) 		tf->fp.entry_handler = fentry_dispatcher;
388334e5519SMasami Hiramatsu (Google) 
389b576e097SMasami Hiramatsu (Google) 	tf->tpoint = tpoint;
390334e5519SMasami Hiramatsu (Google) 	tf->fp.nr_maxactive = maxactive;
391334e5519SMasami Hiramatsu (Google) 
392334e5519SMasami Hiramatsu (Google) 	ret = trace_probe_init(&tf->tp, event, group, false);
393334e5519SMasami Hiramatsu (Google) 	if (ret < 0)
394334e5519SMasami Hiramatsu (Google) 		goto error;
395334e5519SMasami Hiramatsu (Google) 
396334e5519SMasami Hiramatsu (Google) 	dyn_event_init(&tf->devent, &trace_fprobe_ops);
397334e5519SMasami Hiramatsu (Google) 	return tf;
398334e5519SMasami Hiramatsu (Google) error:
399334e5519SMasami Hiramatsu (Google) 	free_trace_fprobe(tf);
400334e5519SMasami Hiramatsu (Google) 	return ERR_PTR(ret);
401334e5519SMasami Hiramatsu (Google) }
402334e5519SMasami Hiramatsu (Google) 
find_trace_fprobe(const char * event,const char * group)403334e5519SMasami Hiramatsu (Google) static struct trace_fprobe *find_trace_fprobe(const char *event,
404334e5519SMasami Hiramatsu (Google) 					      const char *group)
405334e5519SMasami Hiramatsu (Google) {
406334e5519SMasami Hiramatsu (Google) 	struct dyn_event *pos;
407334e5519SMasami Hiramatsu (Google) 	struct trace_fprobe *tf;
408334e5519SMasami Hiramatsu (Google) 
409334e5519SMasami Hiramatsu (Google) 	for_each_trace_fprobe(tf, pos)
410334e5519SMasami Hiramatsu (Google) 		if (strcmp(trace_probe_name(&tf->tp), event) == 0 &&
411334e5519SMasami Hiramatsu (Google) 		    strcmp(trace_probe_group_name(&tf->tp), group) == 0)
412334e5519SMasami Hiramatsu (Google) 			return tf;
413334e5519SMasami Hiramatsu (Google) 	return NULL;
414334e5519SMasami Hiramatsu (Google) }
415334e5519SMasami Hiramatsu (Google) 
__enable_trace_fprobe(struct trace_fprobe * tf)416334e5519SMasami Hiramatsu (Google) static inline int __enable_trace_fprobe(struct trace_fprobe *tf)
417334e5519SMasami Hiramatsu (Google) {
418334e5519SMasami Hiramatsu (Google) 	if (trace_fprobe_is_registered(tf))
419334e5519SMasami Hiramatsu (Google) 		enable_fprobe(&tf->fp);
420334e5519SMasami Hiramatsu (Google) 
421334e5519SMasami Hiramatsu (Google) 	return 0;
422334e5519SMasami Hiramatsu (Google) }
423334e5519SMasami Hiramatsu (Google) 
__disable_trace_fprobe(struct trace_probe * tp)424334e5519SMasami Hiramatsu (Google) static void __disable_trace_fprobe(struct trace_probe *tp)
425334e5519SMasami Hiramatsu (Google) {
426334e5519SMasami Hiramatsu (Google) 	struct trace_fprobe *tf;
427334e5519SMasami Hiramatsu (Google) 
428334e5519SMasami Hiramatsu (Google) 	list_for_each_entry(tf, trace_probe_probe_list(tp), tp.list) {
429334e5519SMasami Hiramatsu (Google) 		if (!trace_fprobe_is_registered(tf))
430334e5519SMasami Hiramatsu (Google) 			continue;
431334e5519SMasami Hiramatsu (Google) 		disable_fprobe(&tf->fp);
432334e5519SMasami Hiramatsu (Google) 	}
433334e5519SMasami Hiramatsu (Google) }
434334e5519SMasami Hiramatsu (Google) 
435334e5519SMasami Hiramatsu (Google) /*
436334e5519SMasami Hiramatsu (Google)  * Enable trace_probe
437334e5519SMasami Hiramatsu (Google)  * if the file is NULL, enable "perf" handler, or enable "trace" handler.
438334e5519SMasami Hiramatsu (Google)  */
enable_trace_fprobe(struct trace_event_call * call,struct trace_event_file * file)439334e5519SMasami Hiramatsu (Google) static int enable_trace_fprobe(struct trace_event_call *call,
440334e5519SMasami Hiramatsu (Google) 			       struct trace_event_file *file)
441334e5519SMasami Hiramatsu (Google) {
442334e5519SMasami Hiramatsu (Google) 	struct trace_probe *tp;
443334e5519SMasami Hiramatsu (Google) 	struct trace_fprobe *tf;
444334e5519SMasami Hiramatsu (Google) 	bool enabled;
445334e5519SMasami Hiramatsu (Google) 	int ret = 0;
446334e5519SMasami Hiramatsu (Google) 
447334e5519SMasami Hiramatsu (Google) 	tp = trace_probe_primary_from_call(call);
448334e5519SMasami Hiramatsu (Google) 	if (WARN_ON_ONCE(!tp))
449334e5519SMasami Hiramatsu (Google) 		return -ENODEV;
450334e5519SMasami Hiramatsu (Google) 	enabled = trace_probe_is_enabled(tp);
451334e5519SMasami Hiramatsu (Google) 
452334e5519SMasami Hiramatsu (Google) 	/* This also changes "enabled" state */
453334e5519SMasami Hiramatsu (Google) 	if (file) {
454334e5519SMasami Hiramatsu (Google) 		ret = trace_probe_add_file(tp, file);
455334e5519SMasami Hiramatsu (Google) 		if (ret)
456334e5519SMasami Hiramatsu (Google) 			return ret;
457334e5519SMasami Hiramatsu (Google) 	} else
458334e5519SMasami Hiramatsu (Google) 		trace_probe_set_flag(tp, TP_FLAG_PROFILE);
459334e5519SMasami Hiramatsu (Google) 
460334e5519SMasami Hiramatsu (Google) 	if (!enabled) {
461334e5519SMasami Hiramatsu (Google) 		list_for_each_entry(tf, trace_probe_probe_list(tp), tp.list) {
462334e5519SMasami Hiramatsu (Google) 			/* TODO: check the fprobe is gone */
463334e5519SMasami Hiramatsu (Google) 			__enable_trace_fprobe(tf);
464334e5519SMasami Hiramatsu (Google) 		}
465334e5519SMasami Hiramatsu (Google) 	}
466334e5519SMasami Hiramatsu (Google) 
467334e5519SMasami Hiramatsu (Google) 	return 0;
468334e5519SMasami Hiramatsu (Google) }
469334e5519SMasami Hiramatsu (Google) 
470334e5519SMasami Hiramatsu (Google) /*
471334e5519SMasami Hiramatsu (Google)  * Disable trace_probe
472334e5519SMasami Hiramatsu (Google)  * if the file is NULL, disable "perf" handler, or disable "trace" handler.
473334e5519SMasami Hiramatsu (Google)  */
disable_trace_fprobe(struct trace_event_call * call,struct trace_event_file * file)474334e5519SMasami Hiramatsu (Google) static int disable_trace_fprobe(struct trace_event_call *call,
475334e5519SMasami Hiramatsu (Google) 				struct trace_event_file *file)
476334e5519SMasami Hiramatsu (Google) {
477334e5519SMasami Hiramatsu (Google) 	struct trace_probe *tp;
478334e5519SMasami Hiramatsu (Google) 
479334e5519SMasami Hiramatsu (Google) 	tp = trace_probe_primary_from_call(call);
480334e5519SMasami Hiramatsu (Google) 	if (WARN_ON_ONCE(!tp))
481334e5519SMasami Hiramatsu (Google) 		return -ENODEV;
482334e5519SMasami Hiramatsu (Google) 
483334e5519SMasami Hiramatsu (Google) 	if (file) {
484334e5519SMasami Hiramatsu (Google) 		if (!trace_probe_get_file_link(tp, file))
485334e5519SMasami Hiramatsu (Google) 			return -ENOENT;
486334e5519SMasami Hiramatsu (Google) 		if (!trace_probe_has_single_file(tp))
487334e5519SMasami Hiramatsu (Google) 			goto out;
488334e5519SMasami Hiramatsu (Google) 		trace_probe_clear_flag(tp, TP_FLAG_TRACE);
489334e5519SMasami Hiramatsu (Google) 	} else
490334e5519SMasami Hiramatsu (Google) 		trace_probe_clear_flag(tp, TP_FLAG_PROFILE);
491334e5519SMasami Hiramatsu (Google) 
492334e5519SMasami Hiramatsu (Google) 	if (!trace_probe_is_enabled(tp))
493334e5519SMasami Hiramatsu (Google) 		__disable_trace_fprobe(tp);
494334e5519SMasami Hiramatsu (Google) 
495334e5519SMasami Hiramatsu (Google)  out:
496334e5519SMasami Hiramatsu (Google) 	if (file)
497334e5519SMasami Hiramatsu (Google) 		/*
498334e5519SMasami Hiramatsu (Google) 		 * Synchronization is done in below function. For perf event,
499334e5519SMasami Hiramatsu (Google) 		 * file == NULL and perf_trace_event_unreg() calls
500334e5519SMasami Hiramatsu (Google) 		 * tracepoint_synchronize_unregister() to ensure synchronize
501334e5519SMasami Hiramatsu (Google) 		 * event. We don't need to care about it.
502334e5519SMasami Hiramatsu (Google) 		 */
503334e5519SMasami Hiramatsu (Google) 		trace_probe_remove_file(tp, file);
504334e5519SMasami Hiramatsu (Google) 
505334e5519SMasami Hiramatsu (Google) 	return 0;
506334e5519SMasami Hiramatsu (Google) }
507334e5519SMasami Hiramatsu (Google) 
508334e5519SMasami Hiramatsu (Google) /* Event entry printers */
509334e5519SMasami Hiramatsu (Google) static enum print_line_t
print_fentry_event(struct trace_iterator * iter,int flags,struct trace_event * event)510334e5519SMasami Hiramatsu (Google) print_fentry_event(struct trace_iterator *iter, int flags,
511334e5519SMasami Hiramatsu (Google) 		   struct trace_event *event)
512334e5519SMasami Hiramatsu (Google) {
513334e5519SMasami Hiramatsu (Google) 	struct fentry_trace_entry_head *field;
514334e5519SMasami Hiramatsu (Google) 	struct trace_seq *s = &iter->seq;
515334e5519SMasami Hiramatsu (Google) 	struct trace_probe *tp;
516334e5519SMasami Hiramatsu (Google) 
517334e5519SMasami Hiramatsu (Google) 	field = (struct fentry_trace_entry_head *)iter->ent;
518334e5519SMasami Hiramatsu (Google) 	tp = trace_probe_primary_from_call(
519334e5519SMasami Hiramatsu (Google) 		container_of(event, struct trace_event_call, event));
520334e5519SMasami Hiramatsu (Google) 	if (WARN_ON_ONCE(!tp))
521334e5519SMasami Hiramatsu (Google) 		goto out;
522334e5519SMasami Hiramatsu (Google) 
523334e5519SMasami Hiramatsu (Google) 	trace_seq_printf(s, "%s: (", trace_probe_name(tp));
524334e5519SMasami Hiramatsu (Google) 
525334e5519SMasami Hiramatsu (Google) 	if (!seq_print_ip_sym(s, field->ip, flags | TRACE_ITER_SYM_OFFSET))
526334e5519SMasami Hiramatsu (Google) 		goto out;
527334e5519SMasami Hiramatsu (Google) 
528334e5519SMasami Hiramatsu (Google) 	trace_seq_putc(s, ')');
529334e5519SMasami Hiramatsu (Google) 
530334e5519SMasami Hiramatsu (Google) 	if (trace_probe_print_args(s, tp->args, tp->nr_args,
531334e5519SMasami Hiramatsu (Google) 			     (u8 *)&field[1], field) < 0)
532334e5519SMasami Hiramatsu (Google) 		goto out;
533334e5519SMasami Hiramatsu (Google) 
534334e5519SMasami Hiramatsu (Google) 	trace_seq_putc(s, '\n');
535334e5519SMasami Hiramatsu (Google)  out:
536334e5519SMasami Hiramatsu (Google) 	return trace_handle_return(s);
537334e5519SMasami Hiramatsu (Google) }
538334e5519SMasami Hiramatsu (Google) 
539334e5519SMasami Hiramatsu (Google) static enum print_line_t
print_fexit_event(struct trace_iterator * iter,int flags,struct trace_event * event)540334e5519SMasami Hiramatsu (Google) print_fexit_event(struct trace_iterator *iter, int flags,
541334e5519SMasami Hiramatsu (Google) 		  struct trace_event *event)
542334e5519SMasami Hiramatsu (Google) {
543334e5519SMasami Hiramatsu (Google) 	struct fexit_trace_entry_head *field;
544334e5519SMasami Hiramatsu (Google) 	struct trace_seq *s = &iter->seq;
545334e5519SMasami Hiramatsu (Google) 	struct trace_probe *tp;
546334e5519SMasami Hiramatsu (Google) 
547334e5519SMasami Hiramatsu (Google) 	field = (struct fexit_trace_entry_head *)iter->ent;
548334e5519SMasami Hiramatsu (Google) 	tp = trace_probe_primary_from_call(
549334e5519SMasami Hiramatsu (Google) 		container_of(event, struct trace_event_call, event));
550334e5519SMasami Hiramatsu (Google) 	if (WARN_ON_ONCE(!tp))
551334e5519SMasami Hiramatsu (Google) 		goto out;
552334e5519SMasami Hiramatsu (Google) 
553334e5519SMasami Hiramatsu (Google) 	trace_seq_printf(s, "%s: (", trace_probe_name(tp));
554334e5519SMasami Hiramatsu (Google) 
555334e5519SMasami Hiramatsu (Google) 	if (!seq_print_ip_sym(s, field->ret_ip, flags | TRACE_ITER_SYM_OFFSET))
556334e5519SMasami Hiramatsu (Google) 		goto out;
557334e5519SMasami Hiramatsu (Google) 
558334e5519SMasami Hiramatsu (Google) 	trace_seq_puts(s, " <- ");
559334e5519SMasami Hiramatsu (Google) 
560334e5519SMasami Hiramatsu (Google) 	if (!seq_print_ip_sym(s, field->func, flags & ~TRACE_ITER_SYM_OFFSET))
561334e5519SMasami Hiramatsu (Google) 		goto out;
562334e5519SMasami Hiramatsu (Google) 
563334e5519SMasami Hiramatsu (Google) 	trace_seq_putc(s, ')');
564334e5519SMasami Hiramatsu (Google) 
565334e5519SMasami Hiramatsu (Google) 	if (trace_probe_print_args(s, tp->args, tp->nr_args,
566334e5519SMasami Hiramatsu (Google) 			     (u8 *)&field[1], field) < 0)
567334e5519SMasami Hiramatsu (Google) 		goto out;
568334e5519SMasami Hiramatsu (Google) 
569334e5519SMasami Hiramatsu (Google) 	trace_seq_putc(s, '\n');
570334e5519SMasami Hiramatsu (Google) 
571334e5519SMasami Hiramatsu (Google)  out:
572334e5519SMasami Hiramatsu (Google) 	return trace_handle_return(s);
573334e5519SMasami Hiramatsu (Google) }
574334e5519SMasami Hiramatsu (Google) 
fentry_event_define_fields(struct trace_event_call * event_call)575334e5519SMasami Hiramatsu (Google) static int fentry_event_define_fields(struct trace_event_call *event_call)
576334e5519SMasami Hiramatsu (Google) {
577334e5519SMasami Hiramatsu (Google) 	int ret;
578334e5519SMasami Hiramatsu (Google) 	struct fentry_trace_entry_head field;
579334e5519SMasami Hiramatsu (Google) 	struct trace_probe *tp;
580334e5519SMasami Hiramatsu (Google) 
581334e5519SMasami Hiramatsu (Google) 	tp = trace_probe_primary_from_call(event_call);
582334e5519SMasami Hiramatsu (Google) 	if (WARN_ON_ONCE(!tp))
583334e5519SMasami Hiramatsu (Google) 		return -ENOENT;
584334e5519SMasami Hiramatsu (Google) 
585334e5519SMasami Hiramatsu (Google) 	DEFINE_FIELD(unsigned long, ip, FIELD_STRING_IP, 0);
586334e5519SMasami Hiramatsu (Google) 
587334e5519SMasami Hiramatsu (Google) 	return traceprobe_define_arg_fields(event_call, sizeof(field), tp);
588334e5519SMasami Hiramatsu (Google) }
589334e5519SMasami Hiramatsu (Google) 
fexit_event_define_fields(struct trace_event_call * event_call)590334e5519SMasami Hiramatsu (Google) static int fexit_event_define_fields(struct trace_event_call *event_call)
591334e5519SMasami Hiramatsu (Google) {
592334e5519SMasami Hiramatsu (Google) 	int ret;
593334e5519SMasami Hiramatsu (Google) 	struct fexit_trace_entry_head field;
594334e5519SMasami Hiramatsu (Google) 	struct trace_probe *tp;
595334e5519SMasami Hiramatsu (Google) 
596334e5519SMasami Hiramatsu (Google) 	tp = trace_probe_primary_from_call(event_call);
597334e5519SMasami Hiramatsu (Google) 	if (WARN_ON_ONCE(!tp))
598334e5519SMasami Hiramatsu (Google) 		return -ENOENT;
599334e5519SMasami Hiramatsu (Google) 
600334e5519SMasami Hiramatsu (Google) 	DEFINE_FIELD(unsigned long, func, FIELD_STRING_FUNC, 0);
601334e5519SMasami Hiramatsu (Google) 	DEFINE_FIELD(unsigned long, ret_ip, FIELD_STRING_RETIP, 0);
602334e5519SMasami Hiramatsu (Google) 
603334e5519SMasami Hiramatsu (Google) 	return traceprobe_define_arg_fields(event_call, sizeof(field), tp);
604334e5519SMasami Hiramatsu (Google) }
605334e5519SMasami Hiramatsu (Google) 
606334e5519SMasami Hiramatsu (Google) static struct trace_event_functions fentry_funcs = {
607334e5519SMasami Hiramatsu (Google) 	.trace		= print_fentry_event
608334e5519SMasami Hiramatsu (Google) };
609334e5519SMasami Hiramatsu (Google) 
610334e5519SMasami Hiramatsu (Google) static struct trace_event_functions fexit_funcs = {
611334e5519SMasami Hiramatsu (Google) 	.trace		= print_fexit_event
612334e5519SMasami Hiramatsu (Google) };
613334e5519SMasami Hiramatsu (Google) 
614334e5519SMasami Hiramatsu (Google) static struct trace_event_fields fentry_fields_array[] = {
615334e5519SMasami Hiramatsu (Google) 	{ .type = TRACE_FUNCTION_TYPE,
616334e5519SMasami Hiramatsu (Google) 	  .define_fields = fentry_event_define_fields },
617334e5519SMasami Hiramatsu (Google) 	{}
618334e5519SMasami Hiramatsu (Google) };
619334e5519SMasami Hiramatsu (Google) 
620334e5519SMasami Hiramatsu (Google) static struct trace_event_fields fexit_fields_array[] = {
621334e5519SMasami Hiramatsu (Google) 	{ .type = TRACE_FUNCTION_TYPE,
622334e5519SMasami Hiramatsu (Google) 	  .define_fields = fexit_event_define_fields },
623334e5519SMasami Hiramatsu (Google) 	{}
624334e5519SMasami Hiramatsu (Google) };
625334e5519SMasami Hiramatsu (Google) 
626334e5519SMasami Hiramatsu (Google) static int fprobe_register(struct trace_event_call *event,
627334e5519SMasami Hiramatsu (Google) 			   enum trace_reg type, void *data);
628334e5519SMasami Hiramatsu (Google) 
init_trace_event_call(struct trace_fprobe * tf)629334e5519SMasami Hiramatsu (Google) static inline void init_trace_event_call(struct trace_fprobe *tf)
630334e5519SMasami Hiramatsu (Google) {
631334e5519SMasami Hiramatsu (Google) 	struct trace_event_call *call = trace_probe_event_call(&tf->tp);
632334e5519SMasami Hiramatsu (Google) 
633334e5519SMasami Hiramatsu (Google) 	if (trace_fprobe_is_return(tf)) {
634334e5519SMasami Hiramatsu (Google) 		call->event.funcs = &fexit_funcs;
635334e5519SMasami Hiramatsu (Google) 		call->class->fields_array = fexit_fields_array;
636334e5519SMasami Hiramatsu (Google) 	} else {
637334e5519SMasami Hiramatsu (Google) 		call->event.funcs = &fentry_funcs;
638334e5519SMasami Hiramatsu (Google) 		call->class->fields_array = fentry_fields_array;
639334e5519SMasami Hiramatsu (Google) 	}
640334e5519SMasami Hiramatsu (Google) 
641334e5519SMasami Hiramatsu (Google) 	call->flags = TRACE_EVENT_FL_FPROBE;
642334e5519SMasami Hiramatsu (Google) 	call->class->reg = fprobe_register;
643334e5519SMasami Hiramatsu (Google) }
644334e5519SMasami Hiramatsu (Google) 
register_fprobe_event(struct trace_fprobe * tf)645334e5519SMasami Hiramatsu (Google) static int register_fprobe_event(struct trace_fprobe *tf)
646334e5519SMasami Hiramatsu (Google) {
647334e5519SMasami Hiramatsu (Google) 	init_trace_event_call(tf);
648334e5519SMasami Hiramatsu (Google) 
649334e5519SMasami Hiramatsu (Google) 	return trace_probe_register_event_call(&tf->tp);
650334e5519SMasami Hiramatsu (Google) }
651334e5519SMasami Hiramatsu (Google) 
unregister_fprobe_event(struct trace_fprobe * tf)652334e5519SMasami Hiramatsu (Google) static int unregister_fprobe_event(struct trace_fprobe *tf)
653334e5519SMasami Hiramatsu (Google) {
654334e5519SMasami Hiramatsu (Google) 	return trace_probe_unregister_event_call(&tf->tp);
655334e5519SMasami Hiramatsu (Google) }
656334e5519SMasami Hiramatsu (Google) 
657334e5519SMasami Hiramatsu (Google) /* Internal register function - just handle fprobe and flags */
__register_trace_fprobe(struct trace_fprobe * tf)658334e5519SMasami Hiramatsu (Google) static int __register_trace_fprobe(struct trace_fprobe *tf)
659334e5519SMasami Hiramatsu (Google) {
660334e5519SMasami Hiramatsu (Google) 	int i, ret;
661334e5519SMasami Hiramatsu (Google) 
662334e5519SMasami Hiramatsu (Google) 	/* Should we need new LOCKDOWN flag for fprobe? */
663334e5519SMasami Hiramatsu (Google) 	ret = security_locked_down(LOCKDOWN_KPROBES);
664334e5519SMasami Hiramatsu (Google) 	if (ret)
665334e5519SMasami Hiramatsu (Google) 		return ret;
666334e5519SMasami Hiramatsu (Google) 
667334e5519SMasami Hiramatsu (Google) 	if (trace_fprobe_is_registered(tf))
668334e5519SMasami Hiramatsu (Google) 		return -EINVAL;
669334e5519SMasami Hiramatsu (Google) 
670334e5519SMasami Hiramatsu (Google) 	for (i = 0; i < tf->tp.nr_args; i++) {
671334e5519SMasami Hiramatsu (Google) 		ret = traceprobe_update_arg(&tf->tp.args[i]);
672334e5519SMasami Hiramatsu (Google) 		if (ret)
673334e5519SMasami Hiramatsu (Google) 			return ret;
674334e5519SMasami Hiramatsu (Google) 	}
675334e5519SMasami Hiramatsu (Google) 
676334e5519SMasami Hiramatsu (Google) 	/* Set/clear disabled flag according to tp->flag */
677334e5519SMasami Hiramatsu (Google) 	if (trace_probe_is_enabled(&tf->tp))
678334e5519SMasami Hiramatsu (Google) 		tf->fp.flags &= ~FPROBE_FL_DISABLED;
679334e5519SMasami Hiramatsu (Google) 	else
680334e5519SMasami Hiramatsu (Google) 		tf->fp.flags |= FPROBE_FL_DISABLED;
681334e5519SMasami Hiramatsu (Google) 
682e2d0d7b2SMasami Hiramatsu (Google) 	if (trace_fprobe_is_tracepoint(tf)) {
683e2d0d7b2SMasami Hiramatsu (Google) 		struct tracepoint *tpoint = tf->tpoint;
684e2d0d7b2SMasami Hiramatsu (Google) 		unsigned long ip = (unsigned long)tpoint->probestub;
685e2d0d7b2SMasami Hiramatsu (Google) 		/*
686e2d0d7b2SMasami Hiramatsu (Google) 		 * Here, we do 2 steps to enable fprobe on a tracepoint.
687e2d0d7b2SMasami Hiramatsu (Google) 		 * At first, put __probestub_##TP function on the tracepoint
688e2d0d7b2SMasami Hiramatsu (Google) 		 * and put a fprobe on the stub function.
689e2d0d7b2SMasami Hiramatsu (Google) 		 */
690e2d0d7b2SMasami Hiramatsu (Google) 		ret = tracepoint_probe_register_prio_may_exist(tpoint,
691e2d0d7b2SMasami Hiramatsu (Google) 					tpoint->probestub, NULL, 0);
692e2d0d7b2SMasami Hiramatsu (Google) 		if (ret < 0)
693e2d0d7b2SMasami Hiramatsu (Google) 			return ret;
694e2d0d7b2SMasami Hiramatsu (Google) 		return register_fprobe_ips(&tf->fp, &ip, 1);
695e2d0d7b2SMasami Hiramatsu (Google) 	}
696e2d0d7b2SMasami Hiramatsu (Google) 
697334e5519SMasami Hiramatsu (Google) 	/* TODO: handle filter, nofilter or symbol list */
698334e5519SMasami Hiramatsu (Google) 	return register_fprobe(&tf->fp, tf->symbol, NULL);
699334e5519SMasami Hiramatsu (Google) }
700334e5519SMasami Hiramatsu (Google) 
701334e5519SMasami Hiramatsu (Google) /* Internal unregister function - just handle fprobe and flags */
__unregister_trace_fprobe(struct trace_fprobe * tf)702334e5519SMasami Hiramatsu (Google) static void __unregister_trace_fprobe(struct trace_fprobe *tf)
703334e5519SMasami Hiramatsu (Google) {
704334e5519SMasami Hiramatsu (Google) 	if (trace_fprobe_is_registered(tf)) {
705334e5519SMasami Hiramatsu (Google) 		unregister_fprobe(&tf->fp);
706334e5519SMasami Hiramatsu (Google) 		memset(&tf->fp, 0, sizeof(tf->fp));
707e2d0d7b2SMasami Hiramatsu (Google) 		if (trace_fprobe_is_tracepoint(tf)) {
708e2d0d7b2SMasami Hiramatsu (Google) 			tracepoint_probe_unregister(tf->tpoint,
709e2d0d7b2SMasami Hiramatsu (Google) 					tf->tpoint->probestub, NULL);
710e2d0d7b2SMasami Hiramatsu (Google) 			tf->tpoint = NULL;
711e2d0d7b2SMasami Hiramatsu (Google) 			tf->mod = NULL;
712e2d0d7b2SMasami Hiramatsu (Google) 		}
713334e5519SMasami Hiramatsu (Google) 	}
714334e5519SMasami Hiramatsu (Google) }
715334e5519SMasami Hiramatsu (Google) 
716334e5519SMasami Hiramatsu (Google) /* TODO: make this trace_*probe common function */
717334e5519SMasami Hiramatsu (Google) /* Unregister a trace_probe and probe_event */
unregister_trace_fprobe(struct trace_fprobe * tf)718334e5519SMasami Hiramatsu (Google) static int unregister_trace_fprobe(struct trace_fprobe *tf)
719334e5519SMasami Hiramatsu (Google) {
720334e5519SMasami Hiramatsu (Google) 	/* If other probes are on the event, just unregister fprobe */
721334e5519SMasami Hiramatsu (Google) 	if (trace_probe_has_sibling(&tf->tp))
722334e5519SMasami Hiramatsu (Google) 		goto unreg;
723334e5519SMasami Hiramatsu (Google) 
724334e5519SMasami Hiramatsu (Google) 	/* Enabled event can not be unregistered */
725334e5519SMasami Hiramatsu (Google) 	if (trace_probe_is_enabled(&tf->tp))
726334e5519SMasami Hiramatsu (Google) 		return -EBUSY;
727334e5519SMasami Hiramatsu (Google) 
728334e5519SMasami Hiramatsu (Google) 	/* If there's a reference to the dynamic event */
729334e5519SMasami Hiramatsu (Google) 	if (trace_event_dyn_busy(trace_probe_event_call(&tf->tp)))
730334e5519SMasami Hiramatsu (Google) 		return -EBUSY;
731334e5519SMasami Hiramatsu (Google) 
732334e5519SMasami Hiramatsu (Google) 	/* Will fail if probe is being used by ftrace or perf */
733334e5519SMasami Hiramatsu (Google) 	if (unregister_fprobe_event(tf))
734334e5519SMasami Hiramatsu (Google) 		return -EBUSY;
735334e5519SMasami Hiramatsu (Google) 
736334e5519SMasami Hiramatsu (Google) unreg:
737334e5519SMasami Hiramatsu (Google) 	__unregister_trace_fprobe(tf);
738334e5519SMasami Hiramatsu (Google) 	dyn_event_remove(&tf->devent);
739334e5519SMasami Hiramatsu (Google) 	trace_probe_unlink(&tf->tp);
740334e5519SMasami Hiramatsu (Google) 
741334e5519SMasami Hiramatsu (Google) 	return 0;
742334e5519SMasami Hiramatsu (Google) }
743334e5519SMasami Hiramatsu (Google) 
trace_fprobe_has_same_fprobe(struct trace_fprobe * orig,struct trace_fprobe * comp)744334e5519SMasami Hiramatsu (Google) static bool trace_fprobe_has_same_fprobe(struct trace_fprobe *orig,
745334e5519SMasami Hiramatsu (Google) 					 struct trace_fprobe *comp)
746334e5519SMasami Hiramatsu (Google) {
747334e5519SMasami Hiramatsu (Google) 	struct trace_probe_event *tpe = orig->tp.event;
748334e5519SMasami Hiramatsu (Google) 	int i;
749334e5519SMasami Hiramatsu (Google) 
750334e5519SMasami Hiramatsu (Google) 	list_for_each_entry(orig, &tpe->probes, tp.list) {
751334e5519SMasami Hiramatsu (Google) 		if (strcmp(trace_fprobe_symbol(orig),
752334e5519SMasami Hiramatsu (Google) 			   trace_fprobe_symbol(comp)))
753334e5519SMasami Hiramatsu (Google) 			continue;
754334e5519SMasami Hiramatsu (Google) 
755334e5519SMasami Hiramatsu (Google) 		/*
756334e5519SMasami Hiramatsu (Google) 		 * trace_probe_compare_arg_type() ensured that nr_args and
757334e5519SMasami Hiramatsu (Google) 		 * each argument name and type are same. Let's compare comm.
758334e5519SMasami Hiramatsu (Google) 		 */
759334e5519SMasami Hiramatsu (Google) 		for (i = 0; i < orig->tp.nr_args; i++) {
760334e5519SMasami Hiramatsu (Google) 			if (strcmp(orig->tp.args[i].comm,
761334e5519SMasami Hiramatsu (Google) 				   comp->tp.args[i].comm))
762334e5519SMasami Hiramatsu (Google) 				break;
763334e5519SMasami Hiramatsu (Google) 		}
764334e5519SMasami Hiramatsu (Google) 
765334e5519SMasami Hiramatsu (Google) 		if (i == orig->tp.nr_args)
766334e5519SMasami Hiramatsu (Google) 			return true;
767334e5519SMasami Hiramatsu (Google) 	}
768334e5519SMasami Hiramatsu (Google) 
769334e5519SMasami Hiramatsu (Google) 	return false;
770334e5519SMasami Hiramatsu (Google) }
771334e5519SMasami Hiramatsu (Google) 
append_trace_fprobe(struct trace_fprobe * tf,struct trace_fprobe * to)772334e5519SMasami Hiramatsu (Google) static int append_trace_fprobe(struct trace_fprobe *tf, struct trace_fprobe *to)
773334e5519SMasami Hiramatsu (Google) {
774334e5519SMasami Hiramatsu (Google) 	int ret;
775334e5519SMasami Hiramatsu (Google) 
776e2d0d7b2SMasami Hiramatsu (Google) 	if (trace_fprobe_is_return(tf) != trace_fprobe_is_return(to) ||
777e2d0d7b2SMasami Hiramatsu (Google) 	    trace_fprobe_is_tracepoint(tf) != trace_fprobe_is_tracepoint(to)) {
778334e5519SMasami Hiramatsu (Google) 		trace_probe_log_set_index(0);
779334e5519SMasami Hiramatsu (Google) 		trace_probe_log_err(0, DIFF_PROBE_TYPE);
780334e5519SMasami Hiramatsu (Google) 		return -EEXIST;
781334e5519SMasami Hiramatsu (Google) 	}
782334e5519SMasami Hiramatsu (Google) 	ret = trace_probe_compare_arg_type(&tf->tp, &to->tp);
783334e5519SMasami Hiramatsu (Google) 	if (ret) {
784334e5519SMasami Hiramatsu (Google) 		/* Note that argument starts index = 2 */
785334e5519SMasami Hiramatsu (Google) 		trace_probe_log_set_index(ret + 1);
786334e5519SMasami Hiramatsu (Google) 		trace_probe_log_err(0, DIFF_ARG_TYPE);
787334e5519SMasami Hiramatsu (Google) 		return -EEXIST;
788334e5519SMasami Hiramatsu (Google) 	}
789334e5519SMasami Hiramatsu (Google) 	if (trace_fprobe_has_same_fprobe(to, tf)) {
790334e5519SMasami Hiramatsu (Google) 		trace_probe_log_set_index(0);
791334e5519SMasami Hiramatsu (Google) 		trace_probe_log_err(0, SAME_PROBE);
792334e5519SMasami Hiramatsu (Google) 		return -EEXIST;
793334e5519SMasami Hiramatsu (Google) 	}
794334e5519SMasami Hiramatsu (Google) 
795334e5519SMasami Hiramatsu (Google) 	/* Append to existing event */
796334e5519SMasami Hiramatsu (Google) 	ret = trace_probe_append(&tf->tp, &to->tp);
797334e5519SMasami Hiramatsu (Google) 	if (ret)
798334e5519SMasami Hiramatsu (Google) 		return ret;
799334e5519SMasami Hiramatsu (Google) 
800334e5519SMasami Hiramatsu (Google) 	ret = __register_trace_fprobe(tf);
801334e5519SMasami Hiramatsu (Google) 	if (ret)
802334e5519SMasami Hiramatsu (Google) 		trace_probe_unlink(&tf->tp);
803334e5519SMasami Hiramatsu (Google) 	else
804334e5519SMasami Hiramatsu (Google) 		dyn_event_add(&tf->devent, trace_probe_event_call(&tf->tp));
805334e5519SMasami Hiramatsu (Google) 
806334e5519SMasami Hiramatsu (Google) 	return ret;
807334e5519SMasami Hiramatsu (Google) }
808334e5519SMasami Hiramatsu (Google) 
809334e5519SMasami Hiramatsu (Google) /* Register a trace_probe and probe_event */
register_trace_fprobe(struct trace_fprobe * tf)810334e5519SMasami Hiramatsu (Google) static int register_trace_fprobe(struct trace_fprobe *tf)
811334e5519SMasami Hiramatsu (Google) {
812334e5519SMasami Hiramatsu (Google) 	struct trace_fprobe *old_tf;
813334e5519SMasami Hiramatsu (Google) 	int ret;
814334e5519SMasami Hiramatsu (Google) 
815334e5519SMasami Hiramatsu (Google) 	mutex_lock(&event_mutex);
816334e5519SMasami Hiramatsu (Google) 
817334e5519SMasami Hiramatsu (Google) 	old_tf = find_trace_fprobe(trace_probe_name(&tf->tp),
818334e5519SMasami Hiramatsu (Google) 				   trace_probe_group_name(&tf->tp));
819334e5519SMasami Hiramatsu (Google) 	if (old_tf) {
820334e5519SMasami Hiramatsu (Google) 		ret = append_trace_fprobe(tf, old_tf);
821334e5519SMasami Hiramatsu (Google) 		goto end;
822334e5519SMasami Hiramatsu (Google) 	}
823334e5519SMasami Hiramatsu (Google) 
824334e5519SMasami Hiramatsu (Google) 	/* Register new event */
825334e5519SMasami Hiramatsu (Google) 	ret = register_fprobe_event(tf);
826334e5519SMasami Hiramatsu (Google) 	if (ret) {
827334e5519SMasami Hiramatsu (Google) 		if (ret == -EEXIST) {
828334e5519SMasami Hiramatsu (Google) 			trace_probe_log_set_index(0);
829334e5519SMasami Hiramatsu (Google) 			trace_probe_log_err(0, EVENT_EXIST);
830334e5519SMasami Hiramatsu (Google) 		} else
831334e5519SMasami Hiramatsu (Google) 			pr_warn("Failed to register probe event(%d)\n", ret);
832334e5519SMasami Hiramatsu (Google) 		goto end;
833334e5519SMasami Hiramatsu (Google) 	}
834334e5519SMasami Hiramatsu (Google) 
835334e5519SMasami Hiramatsu (Google) 	/* Register fprobe */
836334e5519SMasami Hiramatsu (Google) 	ret = __register_trace_fprobe(tf);
837334e5519SMasami Hiramatsu (Google) 	if (ret < 0)
838334e5519SMasami Hiramatsu (Google) 		unregister_fprobe_event(tf);
839334e5519SMasami Hiramatsu (Google) 	else
840334e5519SMasami Hiramatsu (Google) 		dyn_event_add(&tf->devent, trace_probe_event_call(&tf->tp));
841334e5519SMasami Hiramatsu (Google) 
842334e5519SMasami Hiramatsu (Google) end:
843334e5519SMasami Hiramatsu (Google) 	mutex_unlock(&event_mutex);
844334e5519SMasami Hiramatsu (Google) 	return ret;
845334e5519SMasami Hiramatsu (Google) }
846334e5519SMasami Hiramatsu (Google) 
847e2d0d7b2SMasami Hiramatsu (Google) #ifdef CONFIG_MODULES
__tracepoint_probe_module_cb(struct notifier_block * self,unsigned long val,void * data)848e2d0d7b2SMasami Hiramatsu (Google) static int __tracepoint_probe_module_cb(struct notifier_block *self,
849e2d0d7b2SMasami Hiramatsu (Google) 					unsigned long val, void *data)
850e2d0d7b2SMasami Hiramatsu (Google) {
851e2d0d7b2SMasami Hiramatsu (Google) 	struct tp_module *tp_mod = data;
852e2d0d7b2SMasami Hiramatsu (Google) 	struct trace_fprobe *tf;
853e2d0d7b2SMasami Hiramatsu (Google) 	struct dyn_event *pos;
854e2d0d7b2SMasami Hiramatsu (Google) 
855e2d0d7b2SMasami Hiramatsu (Google) 	if (val != MODULE_STATE_GOING)
856e2d0d7b2SMasami Hiramatsu (Google) 		return NOTIFY_DONE;
857e2d0d7b2SMasami Hiramatsu (Google) 
858e2d0d7b2SMasami Hiramatsu (Google) 	mutex_lock(&event_mutex);
859e2d0d7b2SMasami Hiramatsu (Google) 	for_each_trace_fprobe(tf, pos) {
860e2d0d7b2SMasami Hiramatsu (Google) 		if (tp_mod->mod == tf->mod) {
861e2d0d7b2SMasami Hiramatsu (Google) 			tracepoint_probe_unregister(tf->tpoint,
862e2d0d7b2SMasami Hiramatsu (Google) 					tf->tpoint->probestub, NULL);
863e2d0d7b2SMasami Hiramatsu (Google) 			tf->tpoint = NULL;
864e2d0d7b2SMasami Hiramatsu (Google) 			tf->mod = NULL;
865e2d0d7b2SMasami Hiramatsu (Google) 		}
866e2d0d7b2SMasami Hiramatsu (Google) 	}
867e2d0d7b2SMasami Hiramatsu (Google) 	mutex_unlock(&event_mutex);
868e2d0d7b2SMasami Hiramatsu (Google) 
869e2d0d7b2SMasami Hiramatsu (Google) 	return NOTIFY_DONE;
870e2d0d7b2SMasami Hiramatsu (Google) }
871e2d0d7b2SMasami Hiramatsu (Google) 
872e2d0d7b2SMasami Hiramatsu (Google) static struct notifier_block tracepoint_module_nb = {
873e2d0d7b2SMasami Hiramatsu (Google) 	.notifier_call = __tracepoint_probe_module_cb,
874e2d0d7b2SMasami Hiramatsu (Google) };
875e2d0d7b2SMasami Hiramatsu (Google) #endif /* CONFIG_MODULES */
876e2d0d7b2SMasami Hiramatsu (Google) 
877e2d0d7b2SMasami Hiramatsu (Google) struct __find_tracepoint_cb_data {
878e2d0d7b2SMasami Hiramatsu (Google) 	const char *tp_name;
879e2d0d7b2SMasami Hiramatsu (Google) 	struct tracepoint *tpoint;
880e2d0d7b2SMasami Hiramatsu (Google) };
881e2d0d7b2SMasami Hiramatsu (Google) 
__find_tracepoint_cb(struct tracepoint * tp,void * priv)882e2d0d7b2SMasami Hiramatsu (Google) static void __find_tracepoint_cb(struct tracepoint *tp, void *priv)
883e2d0d7b2SMasami Hiramatsu (Google) {
884e2d0d7b2SMasami Hiramatsu (Google) 	struct __find_tracepoint_cb_data *data = priv;
885e2d0d7b2SMasami Hiramatsu (Google) 
886e2d0d7b2SMasami Hiramatsu (Google) 	if (!data->tpoint && !strcmp(data->tp_name, tp->name))
887e2d0d7b2SMasami Hiramatsu (Google) 		data->tpoint = tp;
888e2d0d7b2SMasami Hiramatsu (Google) }
889e2d0d7b2SMasami Hiramatsu (Google) 
find_tracepoint(const char * tp_name)890e2d0d7b2SMasami Hiramatsu (Google) static struct tracepoint *find_tracepoint(const char *tp_name)
891e2d0d7b2SMasami Hiramatsu (Google) {
892e2d0d7b2SMasami Hiramatsu (Google) 	struct __find_tracepoint_cb_data data = {
893e2d0d7b2SMasami Hiramatsu (Google) 		.tp_name = tp_name,
894e2d0d7b2SMasami Hiramatsu (Google) 	};
895e2d0d7b2SMasami Hiramatsu (Google) 
896e2d0d7b2SMasami Hiramatsu (Google) 	for_each_kernel_tracepoint(__find_tracepoint_cb, &data);
897e2d0d7b2SMasami Hiramatsu (Google) 
898e2d0d7b2SMasami Hiramatsu (Google) 	return data.tpoint;
899e2d0d7b2SMasami Hiramatsu (Google) }
900e2d0d7b2SMasami Hiramatsu (Google) 
parse_symbol_and_return(int argc,const char * argv[],char ** symbol,bool * is_return,bool is_tracepoint)90108c9306fSMasami Hiramatsu (Google) static int parse_symbol_and_return(int argc, const char *argv[],
90208c9306fSMasami Hiramatsu (Google) 				   char **symbol, bool *is_return,
90308c9306fSMasami Hiramatsu (Google) 				   bool is_tracepoint)
90408c9306fSMasami Hiramatsu (Google) {
90508c9306fSMasami Hiramatsu (Google) 	char *tmp = strchr(argv[1], '%');
90608c9306fSMasami Hiramatsu (Google) 	int i;
90708c9306fSMasami Hiramatsu (Google) 
90808c9306fSMasami Hiramatsu (Google) 	if (tmp) {
90908c9306fSMasami Hiramatsu (Google) 		int len = tmp - argv[1];
91008c9306fSMasami Hiramatsu (Google) 
91108c9306fSMasami Hiramatsu (Google) 		if (!is_tracepoint && !strcmp(tmp, "%return")) {
91208c9306fSMasami Hiramatsu (Google) 			*is_return = true;
91308c9306fSMasami Hiramatsu (Google) 		} else {
91408c9306fSMasami Hiramatsu (Google) 			trace_probe_log_err(len, BAD_ADDR_SUFFIX);
91508c9306fSMasami Hiramatsu (Google) 			return -EINVAL;
91608c9306fSMasami Hiramatsu (Google) 		}
91708c9306fSMasami Hiramatsu (Google) 		*symbol = kmemdup_nul(argv[1], len, GFP_KERNEL);
91808c9306fSMasami Hiramatsu (Google) 	} else
91908c9306fSMasami Hiramatsu (Google) 		*symbol = kstrdup(argv[1], GFP_KERNEL);
92008c9306fSMasami Hiramatsu (Google) 	if (!*symbol)
92108c9306fSMasami Hiramatsu (Google) 		return -ENOMEM;
92208c9306fSMasami Hiramatsu (Google) 
92308c9306fSMasami Hiramatsu (Google) 	if (*is_return)
92408c9306fSMasami Hiramatsu (Google) 		return 0;
92508c9306fSMasami Hiramatsu (Google) 
92608c9306fSMasami Hiramatsu (Google) 	/* If there is $retval, this should be a return fprobe. */
92708c9306fSMasami Hiramatsu (Google) 	for (i = 2; i < argc; i++) {
92808c9306fSMasami Hiramatsu (Google) 		tmp = strstr(argv[i], "$retval");
92908c9306fSMasami Hiramatsu (Google) 		if (tmp && !isalnum(tmp[7]) && tmp[7] != '_') {
930*0e9a6b8aSMasami Hiramatsu (Google) 			if (is_tracepoint) {
931*0e9a6b8aSMasami Hiramatsu (Google) 				trace_probe_log_set_index(i);
932*0e9a6b8aSMasami Hiramatsu (Google) 				trace_probe_log_err(tmp - argv[i], RETVAL_ON_PROBE);
933*0e9a6b8aSMasami Hiramatsu (Google) 				return -EINVAL;
934*0e9a6b8aSMasami Hiramatsu (Google) 			}
93508c9306fSMasami Hiramatsu (Google) 			*is_return = true;
93608c9306fSMasami Hiramatsu (Google) 			break;
93708c9306fSMasami Hiramatsu (Google) 		}
93808c9306fSMasami Hiramatsu (Google) 	}
93908c9306fSMasami Hiramatsu (Google) 	return 0;
94008c9306fSMasami Hiramatsu (Google) }
94108c9306fSMasami Hiramatsu (Google) 
__trace_fprobe_create(int argc,const char * argv[])942334e5519SMasami Hiramatsu (Google) static int __trace_fprobe_create(int argc, const char *argv[])
943334e5519SMasami Hiramatsu (Google) {
944334e5519SMasami Hiramatsu (Google) 	/*
945334e5519SMasami Hiramatsu (Google) 	 * Argument syntax:
946334e5519SMasami Hiramatsu (Google) 	 *  - Add fentry probe:
947334e5519SMasami Hiramatsu (Google) 	 *      f[:[GRP/][EVENT]] [MOD:]KSYM [FETCHARGS]
948334e5519SMasami Hiramatsu (Google) 	 *  - Add fexit probe:
949334e5519SMasami Hiramatsu (Google) 	 *      f[N][:[GRP/][EVENT]] [MOD:]KSYM%return [FETCHARGS]
950e2d0d7b2SMasami Hiramatsu (Google) 	 *  - Add tracepoint probe:
951e2d0d7b2SMasami Hiramatsu (Google) 	 *      t[:[GRP/][EVENT]] TRACEPOINT [FETCHARGS]
952334e5519SMasami Hiramatsu (Google) 	 *
953334e5519SMasami Hiramatsu (Google) 	 * Fetch args:
954334e5519SMasami Hiramatsu (Google) 	 *  $retval	: fetch return value
955334e5519SMasami Hiramatsu (Google) 	 *  $stack	: fetch stack address
956334e5519SMasami Hiramatsu (Google) 	 *  $stackN	: fetch Nth entry of stack (N:0-)
957334e5519SMasami Hiramatsu (Google) 	 *  $argN	: fetch Nth argument (N:1-)
958334e5519SMasami Hiramatsu (Google) 	 *  $comm       : fetch current task comm
959334e5519SMasami Hiramatsu (Google) 	 *  @ADDR	: fetch memory at ADDR (ADDR should be in kernel)
960334e5519SMasami Hiramatsu (Google) 	 *  @SYM[+|-offs] : fetch memory at SYM +|- offs (SYM is a data symbol)
961334e5519SMasami Hiramatsu (Google) 	 * Dereferencing memory fetch:
962334e5519SMasami Hiramatsu (Google) 	 *  +|-offs(ARG) : fetch memory at ARG +|- offs address.
963334e5519SMasami Hiramatsu (Google) 	 * Alias name of args:
964334e5519SMasami Hiramatsu (Google) 	 *  NAME=FETCHARG : set NAME as alias of FETCHARG.
965334e5519SMasami Hiramatsu (Google) 	 * Type of args:
966334e5519SMasami Hiramatsu (Google) 	 *  FETCHARG:TYPE : use TYPE instead of unsigned long.
967334e5519SMasami Hiramatsu (Google) 	 */
968334e5519SMasami Hiramatsu (Google) 	struct trace_fprobe *tf = NULL;
96918b1e870SMasami Hiramatsu (Google) 	int i, len, new_argc = 0, ret = 0;
970334e5519SMasami Hiramatsu (Google) 	bool is_return = false;
97108c9306fSMasami Hiramatsu (Google) 	char *symbol = NULL;
972334e5519SMasami Hiramatsu (Google) 	const char *event = NULL, *group = FPROBE_EVENT_SYSTEM;
97318b1e870SMasami Hiramatsu (Google) 	const char **new_argv = NULL;
974334e5519SMasami Hiramatsu (Google) 	int maxactive = 0;
975334e5519SMasami Hiramatsu (Google) 	char buf[MAX_EVENT_NAME_LEN];
976334e5519SMasami Hiramatsu (Google) 	char gbuf[MAX_EVENT_NAME_LEN];
977b576e097SMasami Hiramatsu (Google) 	char sbuf[KSYM_NAME_LEN];
97818b1e870SMasami Hiramatsu (Google) 	char abuf[MAX_BTF_ARGS_LEN];
979e2d0d7b2SMasami Hiramatsu (Google) 	bool is_tracepoint = false;
980b576e097SMasami Hiramatsu (Google) 	struct tracepoint *tpoint = NULL;
981b576e097SMasami Hiramatsu (Google) 	struct traceprobe_parse_context ctx = {
982b576e097SMasami Hiramatsu (Google) 		.flags = TPARG_FL_KERNEL | TPARG_FL_FPROBE,
983b576e097SMasami Hiramatsu (Google) 	};
984334e5519SMasami Hiramatsu (Google) 
985e2d0d7b2SMasami Hiramatsu (Google) 	if ((argv[0][0] != 'f' && argv[0][0] != 't') || argc < 2)
986334e5519SMasami Hiramatsu (Google) 		return -ECANCELED;
987334e5519SMasami Hiramatsu (Google) 
988e2d0d7b2SMasami Hiramatsu (Google) 	if (argv[0][0] == 't') {
989e2d0d7b2SMasami Hiramatsu (Google) 		is_tracepoint = true;
990e2d0d7b2SMasami Hiramatsu (Google) 		group = TRACEPOINT_EVENT_SYSTEM;
991e2d0d7b2SMasami Hiramatsu (Google) 	}
992e2d0d7b2SMasami Hiramatsu (Google) 
993334e5519SMasami Hiramatsu (Google) 	trace_probe_log_init("trace_fprobe", argc, argv);
994334e5519SMasami Hiramatsu (Google) 
995334e5519SMasami Hiramatsu (Google) 	event = strchr(&argv[0][1], ':');
996334e5519SMasami Hiramatsu (Google) 	if (event)
997334e5519SMasami Hiramatsu (Google) 		event++;
998334e5519SMasami Hiramatsu (Google) 
999334e5519SMasami Hiramatsu (Google) 	if (isdigit(argv[0][1])) {
1000334e5519SMasami Hiramatsu (Google) 		if (event)
1001334e5519SMasami Hiramatsu (Google) 			len = event - &argv[0][1] - 1;
1002334e5519SMasami Hiramatsu (Google) 		else
1003334e5519SMasami Hiramatsu (Google) 			len = strlen(&argv[0][1]);
1004334e5519SMasami Hiramatsu (Google) 		if (len > MAX_EVENT_NAME_LEN - 1) {
1005334e5519SMasami Hiramatsu (Google) 			trace_probe_log_err(1, BAD_MAXACT);
1006334e5519SMasami Hiramatsu (Google) 			goto parse_error;
1007334e5519SMasami Hiramatsu (Google) 		}
1008334e5519SMasami Hiramatsu (Google) 		memcpy(buf, &argv[0][1], len);
1009334e5519SMasami Hiramatsu (Google) 		buf[len] = '\0';
1010334e5519SMasami Hiramatsu (Google) 		ret = kstrtouint(buf, 0, &maxactive);
1011334e5519SMasami Hiramatsu (Google) 		if (ret || !maxactive) {
1012334e5519SMasami Hiramatsu (Google) 			trace_probe_log_err(1, BAD_MAXACT);
1013334e5519SMasami Hiramatsu (Google) 			goto parse_error;
1014334e5519SMasami Hiramatsu (Google) 		}
1015334e5519SMasami Hiramatsu (Google) 		/* fprobe rethook instances are iterated over via a list. The
1016334e5519SMasami Hiramatsu (Google) 		 * maximum should stay reasonable.
1017334e5519SMasami Hiramatsu (Google) 		 */
1018334e5519SMasami Hiramatsu (Google) 		if (maxactive > RETHOOK_MAXACTIVE_MAX) {
1019334e5519SMasami Hiramatsu (Google) 			trace_probe_log_err(1, MAXACT_TOO_BIG);
1020334e5519SMasami Hiramatsu (Google) 			goto parse_error;
1021334e5519SMasami Hiramatsu (Google) 		}
1022334e5519SMasami Hiramatsu (Google) 	}
1023334e5519SMasami Hiramatsu (Google) 
1024334e5519SMasami Hiramatsu (Google) 	trace_probe_log_set_index(1);
1025334e5519SMasami Hiramatsu (Google) 
1026e2d0d7b2SMasami Hiramatsu (Google) 	/* a symbol(or tracepoint) must be specified */
102708c9306fSMasami Hiramatsu (Google) 	ret = parse_symbol_and_return(argc, argv, &symbol, &is_return, is_tracepoint);
102808c9306fSMasami Hiramatsu (Google) 	if (ret < 0)
1029334e5519SMasami Hiramatsu (Google) 		goto parse_error;
103008c9306fSMasami Hiramatsu (Google) 
1031334e5519SMasami Hiramatsu (Google) 	if (!is_return && maxactive) {
1032334e5519SMasami Hiramatsu (Google) 		trace_probe_log_set_index(0);
1033334e5519SMasami Hiramatsu (Google) 		trace_probe_log_err(1, BAD_MAXACT_TYPE);
1034334e5519SMasami Hiramatsu (Google) 		goto parse_error;
1035334e5519SMasami Hiramatsu (Google) 	}
1036334e5519SMasami Hiramatsu (Google) 
1037334e5519SMasami Hiramatsu (Google) 	trace_probe_log_set_index(0);
1038334e5519SMasami Hiramatsu (Google) 	if (event) {
1039334e5519SMasami Hiramatsu (Google) 		ret = traceprobe_parse_event_name(&event, &group, gbuf,
1040334e5519SMasami Hiramatsu (Google) 						  event - argv[0]);
1041334e5519SMasami Hiramatsu (Google) 		if (ret)
1042334e5519SMasami Hiramatsu (Google) 			goto parse_error;
1043334e5519SMasami Hiramatsu (Google) 	}
1044334e5519SMasami Hiramatsu (Google) 
1045334e5519SMasami Hiramatsu (Google) 	if (!event) {
1046334e5519SMasami Hiramatsu (Google) 		/* Make a new event name */
1047e2d0d7b2SMasami Hiramatsu (Google) 		if (is_tracepoint)
1048b576e097SMasami Hiramatsu (Google) 			snprintf(buf, MAX_EVENT_NAME_LEN, "%s%s",
1049b576e097SMasami Hiramatsu (Google) 				 isdigit(*symbol) ? "_" : "", symbol);
1050e2d0d7b2SMasami Hiramatsu (Google) 		else
1051334e5519SMasami Hiramatsu (Google) 			snprintf(buf, MAX_EVENT_NAME_LEN, "%s__%s", symbol,
1052334e5519SMasami Hiramatsu (Google) 				 is_return ? "exit" : "entry");
1053334e5519SMasami Hiramatsu (Google) 		sanitize_event_name(buf);
1054334e5519SMasami Hiramatsu (Google) 		event = buf;
1055334e5519SMasami Hiramatsu (Google) 	}
1056334e5519SMasami Hiramatsu (Google) 
1057b576e097SMasami Hiramatsu (Google) 	if (is_return)
1058b576e097SMasami Hiramatsu (Google) 		ctx.flags |= TPARG_FL_RETURN;
1059b576e097SMasami Hiramatsu (Google) 	else
1060b576e097SMasami Hiramatsu (Google) 		ctx.flags |= TPARG_FL_FENTRY;
1061b576e097SMasami Hiramatsu (Google) 
1062b576e097SMasami Hiramatsu (Google) 	if (is_tracepoint) {
1063b576e097SMasami Hiramatsu (Google) 		ctx.flags |= TPARG_FL_TPOINT;
1064b576e097SMasami Hiramatsu (Google) 		tpoint = find_tracepoint(symbol);
1065b576e097SMasami Hiramatsu (Google) 		if (!tpoint) {
1066b576e097SMasami Hiramatsu (Google) 			trace_probe_log_set_index(1);
1067b576e097SMasami Hiramatsu (Google) 			trace_probe_log_err(0, NO_TRACEPOINT);
1068b576e097SMasami Hiramatsu (Google) 			goto parse_error;
1069b576e097SMasami Hiramatsu (Google) 		}
1070b576e097SMasami Hiramatsu (Google) 		ctx.funcname = kallsyms_lookup(
1071b576e097SMasami Hiramatsu (Google) 				(unsigned long)tpoint->probestub,
1072b576e097SMasami Hiramatsu (Google) 				NULL, NULL, NULL, sbuf);
1073b576e097SMasami Hiramatsu (Google) 	} else
1074b576e097SMasami Hiramatsu (Google) 		ctx.funcname = symbol;
1075b576e097SMasami Hiramatsu (Google) 
107618b1e870SMasami Hiramatsu (Google) 	argc -= 2; argv += 2;
107718b1e870SMasami Hiramatsu (Google) 	new_argv = traceprobe_expand_meta_args(argc, argv, &new_argc,
107818b1e870SMasami Hiramatsu (Google) 					       abuf, MAX_BTF_ARGS_LEN, &ctx);
107918b1e870SMasami Hiramatsu (Google) 	if (IS_ERR(new_argv)) {
108018b1e870SMasami Hiramatsu (Google) 		ret = PTR_ERR(new_argv);
108118b1e870SMasami Hiramatsu (Google) 		new_argv = NULL;
108218b1e870SMasami Hiramatsu (Google) 		goto out;
108318b1e870SMasami Hiramatsu (Google) 	}
108418b1e870SMasami Hiramatsu (Google) 	if (new_argv) {
108518b1e870SMasami Hiramatsu (Google) 		argc = new_argc;
108618b1e870SMasami Hiramatsu (Google) 		argv = new_argv;
108718b1e870SMasami Hiramatsu (Google) 	}
108818b1e870SMasami Hiramatsu (Google) 
1089334e5519SMasami Hiramatsu (Google) 	/* setup a probe */
1090b576e097SMasami Hiramatsu (Google) 	tf = alloc_trace_fprobe(group, event, symbol, tpoint, maxactive,
109118b1e870SMasami Hiramatsu (Google) 				argc, is_return);
1092334e5519SMasami Hiramatsu (Google) 	if (IS_ERR(tf)) {
1093334e5519SMasami Hiramatsu (Google) 		ret = PTR_ERR(tf);
1094334e5519SMasami Hiramatsu (Google) 		/* This must return -ENOMEM, else there is a bug */
1095334e5519SMasami Hiramatsu (Google) 		WARN_ON_ONCE(ret != -ENOMEM);
1096334e5519SMasami Hiramatsu (Google) 		goto out;	/* We know tf is not allocated */
1097334e5519SMasami Hiramatsu (Google) 	}
1098e2d0d7b2SMasami Hiramatsu (Google) 
1099b576e097SMasami Hiramatsu (Google) 	if (is_tracepoint)
1100e2d0d7b2SMasami Hiramatsu (Google) 		tf->mod = __module_text_address(
1101e2d0d7b2SMasami Hiramatsu (Google) 				(unsigned long)tf->tpoint->probestub);
1102e2d0d7b2SMasami Hiramatsu (Google) 
1103334e5519SMasami Hiramatsu (Google) 	/* parse arguments */
1104334e5519SMasami Hiramatsu (Google) 	for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) {
1105334e5519SMasami Hiramatsu (Google) 		trace_probe_log_set_index(i + 2);
1106b576e097SMasami Hiramatsu (Google) 		ctx.offset = 0;
11071b8b0cd7SMasami Hiramatsu (Google) 		ret = traceprobe_parse_probe_arg(&tf->tp, i, argv[i], &ctx);
1108334e5519SMasami Hiramatsu (Google) 		if (ret)
1109334e5519SMasami Hiramatsu (Google) 			goto error;	/* This can be -ENOMEM */
1110334e5519SMasami Hiramatsu (Google) 	}
1111334e5519SMasami Hiramatsu (Google) 
1112334e5519SMasami Hiramatsu (Google) 	ret = traceprobe_set_print_fmt(&tf->tp,
1113334e5519SMasami Hiramatsu (Google) 			is_return ? PROBE_PRINT_RETURN : PROBE_PRINT_NORMAL);
1114334e5519SMasami Hiramatsu (Google) 	if (ret < 0)
1115334e5519SMasami Hiramatsu (Google) 		goto error;
1116334e5519SMasami Hiramatsu (Google) 
1117334e5519SMasami Hiramatsu (Google) 	ret = register_trace_fprobe(tf);
1118334e5519SMasami Hiramatsu (Google) 	if (ret) {
1119334e5519SMasami Hiramatsu (Google) 		trace_probe_log_set_index(1);
1120334e5519SMasami Hiramatsu (Google) 		if (ret == -EILSEQ)
1121334e5519SMasami Hiramatsu (Google) 			trace_probe_log_err(0, BAD_INSN_BNDRY);
1122334e5519SMasami Hiramatsu (Google) 		else if (ret == -ENOENT)
1123334e5519SMasami Hiramatsu (Google) 			trace_probe_log_err(0, BAD_PROBE_ADDR);
1124334e5519SMasami Hiramatsu (Google) 		else if (ret != -ENOMEM && ret != -EEXIST)
1125334e5519SMasami Hiramatsu (Google) 			trace_probe_log_err(0, FAIL_REG_PROBE);
1126334e5519SMasami Hiramatsu (Google) 		goto error;
1127334e5519SMasami Hiramatsu (Google) 	}
1128334e5519SMasami Hiramatsu (Google) 
1129334e5519SMasami Hiramatsu (Google) out:
1130b1d1e904SMasami Hiramatsu (Google) 	traceprobe_finish_parse(&ctx);
1131334e5519SMasami Hiramatsu (Google) 	trace_probe_log_clear();
113218b1e870SMasami Hiramatsu (Google) 	kfree(new_argv);
1133334e5519SMasami Hiramatsu (Google) 	kfree(symbol);
1134334e5519SMasami Hiramatsu (Google) 	return ret;
1135334e5519SMasami Hiramatsu (Google) 
1136334e5519SMasami Hiramatsu (Google) parse_error:
1137334e5519SMasami Hiramatsu (Google) 	ret = -EINVAL;
1138334e5519SMasami Hiramatsu (Google) error:
1139334e5519SMasami Hiramatsu (Google) 	free_trace_fprobe(tf);
1140334e5519SMasami Hiramatsu (Google) 	goto out;
1141334e5519SMasami Hiramatsu (Google) }
1142334e5519SMasami Hiramatsu (Google) 
trace_fprobe_create(const char * raw_command)1143334e5519SMasami Hiramatsu (Google) static int trace_fprobe_create(const char *raw_command)
1144334e5519SMasami Hiramatsu (Google) {
1145334e5519SMasami Hiramatsu (Google) 	return trace_probe_create(raw_command, __trace_fprobe_create);
1146334e5519SMasami Hiramatsu (Google) }
1147334e5519SMasami Hiramatsu (Google) 
trace_fprobe_release(struct dyn_event * ev)1148334e5519SMasami Hiramatsu (Google) static int trace_fprobe_release(struct dyn_event *ev)
1149334e5519SMasami Hiramatsu (Google) {
1150334e5519SMasami Hiramatsu (Google) 	struct trace_fprobe *tf = to_trace_fprobe(ev);
1151334e5519SMasami Hiramatsu (Google) 	int ret = unregister_trace_fprobe(tf);
1152334e5519SMasami Hiramatsu (Google) 
1153334e5519SMasami Hiramatsu (Google) 	if (!ret)
1154334e5519SMasami Hiramatsu (Google) 		free_trace_fprobe(tf);
1155334e5519SMasami Hiramatsu (Google) 	return ret;
1156334e5519SMasami Hiramatsu (Google) }
1157334e5519SMasami Hiramatsu (Google) 
trace_fprobe_show(struct seq_file * m,struct dyn_event * ev)1158334e5519SMasami Hiramatsu (Google) static int trace_fprobe_show(struct seq_file *m, struct dyn_event *ev)
1159334e5519SMasami Hiramatsu (Google) {
1160334e5519SMasami Hiramatsu (Google) 	struct trace_fprobe *tf = to_trace_fprobe(ev);
1161334e5519SMasami Hiramatsu (Google) 	int i;
1162334e5519SMasami Hiramatsu (Google) 
1163e2d0d7b2SMasami Hiramatsu (Google) 	if (trace_fprobe_is_tracepoint(tf))
1164e2d0d7b2SMasami Hiramatsu (Google) 		seq_putc(m, 't');
1165e2d0d7b2SMasami Hiramatsu (Google) 	else
1166334e5519SMasami Hiramatsu (Google) 		seq_putc(m, 'f');
1167334e5519SMasami Hiramatsu (Google) 	if (trace_fprobe_is_return(tf) && tf->fp.nr_maxactive)
1168334e5519SMasami Hiramatsu (Google) 		seq_printf(m, "%d", tf->fp.nr_maxactive);
1169334e5519SMasami Hiramatsu (Google) 	seq_printf(m, ":%s/%s", trace_probe_group_name(&tf->tp),
1170334e5519SMasami Hiramatsu (Google) 				trace_probe_name(&tf->tp));
1171334e5519SMasami Hiramatsu (Google) 
1172334e5519SMasami Hiramatsu (Google) 	seq_printf(m, " %s%s", trace_fprobe_symbol(tf),
1173334e5519SMasami Hiramatsu (Google) 			       trace_fprobe_is_return(tf) ? "%return" : "");
1174334e5519SMasami Hiramatsu (Google) 
1175334e5519SMasami Hiramatsu (Google) 	for (i = 0; i < tf->tp.nr_args; i++)
1176334e5519SMasami Hiramatsu (Google) 		seq_printf(m, " %s=%s", tf->tp.args[i].name, tf->tp.args[i].comm);
1177334e5519SMasami Hiramatsu (Google) 	seq_putc(m, '\n');
1178334e5519SMasami Hiramatsu (Google) 
1179334e5519SMasami Hiramatsu (Google) 	return 0;
1180334e5519SMasami Hiramatsu (Google) }
1181334e5519SMasami Hiramatsu (Google) 
1182334e5519SMasami Hiramatsu (Google) /*
1183334e5519SMasami Hiramatsu (Google)  * called by perf_trace_init() or __ftrace_set_clr_event() under event_mutex.
1184334e5519SMasami Hiramatsu (Google)  */
fprobe_register(struct trace_event_call * event,enum trace_reg type,void * data)1185334e5519SMasami Hiramatsu (Google) static int fprobe_register(struct trace_event_call *event,
1186334e5519SMasami Hiramatsu (Google) 			   enum trace_reg type, void *data)
1187334e5519SMasami Hiramatsu (Google) {
1188334e5519SMasami Hiramatsu (Google) 	struct trace_event_file *file = data;
1189334e5519SMasami Hiramatsu (Google) 
1190334e5519SMasami Hiramatsu (Google) 	switch (type) {
1191334e5519SMasami Hiramatsu (Google) 	case TRACE_REG_REGISTER:
1192334e5519SMasami Hiramatsu (Google) 		return enable_trace_fprobe(event, file);
1193334e5519SMasami Hiramatsu (Google) 	case TRACE_REG_UNREGISTER:
1194334e5519SMasami Hiramatsu (Google) 		return disable_trace_fprobe(event, file);
1195334e5519SMasami Hiramatsu (Google) 
1196334e5519SMasami Hiramatsu (Google) #ifdef CONFIG_PERF_EVENTS
1197334e5519SMasami Hiramatsu (Google) 	case TRACE_REG_PERF_REGISTER:
1198334e5519SMasami Hiramatsu (Google) 		return enable_trace_fprobe(event, NULL);
1199334e5519SMasami Hiramatsu (Google) 	case TRACE_REG_PERF_UNREGISTER:
1200334e5519SMasami Hiramatsu (Google) 		return disable_trace_fprobe(event, NULL);
1201334e5519SMasami Hiramatsu (Google) 	case TRACE_REG_PERF_OPEN:
1202334e5519SMasami Hiramatsu (Google) 	case TRACE_REG_PERF_CLOSE:
1203334e5519SMasami Hiramatsu (Google) 	case TRACE_REG_PERF_ADD:
1204334e5519SMasami Hiramatsu (Google) 	case TRACE_REG_PERF_DEL:
1205334e5519SMasami Hiramatsu (Google) 		return 0;
1206334e5519SMasami Hiramatsu (Google) #endif
1207334e5519SMasami Hiramatsu (Google) 	}
1208334e5519SMasami Hiramatsu (Google) 	return 0;
1209334e5519SMasami Hiramatsu (Google) }
1210334e5519SMasami Hiramatsu (Google) 
1211334e5519SMasami Hiramatsu (Google) /*
1212334e5519SMasami Hiramatsu (Google)  * Register dynevent at core_initcall. This allows kernel to setup fprobe
1213334e5519SMasami Hiramatsu (Google)  * events in postcore_initcall without tracefs.
1214334e5519SMasami Hiramatsu (Google)  */
init_fprobe_trace_early(void)1215334e5519SMasami Hiramatsu (Google) static __init int init_fprobe_trace_early(void)
1216334e5519SMasami Hiramatsu (Google) {
1217334e5519SMasami Hiramatsu (Google) 	int ret;
1218334e5519SMasami Hiramatsu (Google) 
1219334e5519SMasami Hiramatsu (Google) 	ret = dyn_event_register(&trace_fprobe_ops);
1220334e5519SMasami Hiramatsu (Google) 	if (ret)
1221334e5519SMasami Hiramatsu (Google) 		return ret;
1222334e5519SMasami Hiramatsu (Google) 
1223e2d0d7b2SMasami Hiramatsu (Google) #ifdef CONFIG_MODULES
1224e2d0d7b2SMasami Hiramatsu (Google) 	ret = register_tracepoint_module_notifier(&tracepoint_module_nb);
1225e2d0d7b2SMasami Hiramatsu (Google) 	if (ret)
1226e2d0d7b2SMasami Hiramatsu (Google) 		return ret;
1227e2d0d7b2SMasami Hiramatsu (Google) #endif
1228e2d0d7b2SMasami Hiramatsu (Google) 
1229334e5519SMasami Hiramatsu (Google) 	return 0;
1230334e5519SMasami Hiramatsu (Google) }
1231334e5519SMasami Hiramatsu (Google) core_initcall(init_fprobe_trace_early);
1232