xref: /openbmc/linux/kernel/trace/trace_dynevent.c (revision a4e1d0b7)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Generic dynamic event control interface
4  *
5  * Copyright (C) 2018 Masami Hiramatsu <mhiramat@kernel.org>
6  */
7 
8 #include <linux/debugfs.h>
9 #include <linux/kernel.h>
10 #include <linux/list.h>
11 #include <linux/mm.h>
12 #include <linux/mutex.h>
13 #include <linux/tracefs.h>
14 
15 #include "trace.h"
16 #include "trace_output.h"	/* for trace_event_sem */
17 #include "trace_dynevent.h"
18 
19 static DEFINE_MUTEX(dyn_event_ops_mutex);
20 static LIST_HEAD(dyn_event_ops_list);
21 
22 bool trace_event_dyn_try_get_ref(struct trace_event_call *dyn_call)
23 {
24 	struct trace_event_call *call;
25 	bool ret = false;
26 
27 	if (WARN_ON_ONCE(!(dyn_call->flags & TRACE_EVENT_FL_DYNAMIC)))
28 		return false;
29 
30 	down_read(&trace_event_sem);
31 	list_for_each_entry(call, &ftrace_events, list) {
32 		if (call == dyn_call) {
33 			atomic_inc(&dyn_call->refcnt);
34 			ret = true;
35 		}
36 	}
37 	up_read(&trace_event_sem);
38 	return ret;
39 }
40 
41 void trace_event_dyn_put_ref(struct trace_event_call *call)
42 {
43 	if (WARN_ON_ONCE(!(call->flags & TRACE_EVENT_FL_DYNAMIC)))
44 		return;
45 
46 	if (WARN_ON_ONCE(atomic_read(&call->refcnt) <= 0)) {
47 		atomic_set(&call->refcnt, 0);
48 		return;
49 	}
50 
51 	atomic_dec(&call->refcnt);
52 }
53 
54 bool trace_event_dyn_busy(struct trace_event_call *call)
55 {
56 	return atomic_read(&call->refcnt) != 0;
57 }
58 
59 int dyn_event_register(struct dyn_event_operations *ops)
60 {
61 	if (!ops || !ops->create || !ops->show || !ops->is_busy ||
62 	    !ops->free || !ops->match)
63 		return -EINVAL;
64 
65 	INIT_LIST_HEAD(&ops->list);
66 	mutex_lock(&dyn_event_ops_mutex);
67 	list_add_tail(&ops->list, &dyn_event_ops_list);
68 	mutex_unlock(&dyn_event_ops_mutex);
69 	return 0;
70 }
71 
72 int dyn_event_release(const char *raw_command, struct dyn_event_operations *type)
73 {
74 	struct dyn_event *pos, *n;
75 	char *system = NULL, *event, *p;
76 	int argc, ret = -ENOENT;
77 	char **argv;
78 
79 	argv = argv_split(GFP_KERNEL, raw_command, &argc);
80 	if (!argv)
81 		return -ENOMEM;
82 
83 	if (argv[0][0] == '-') {
84 		if (argv[0][1] != ':') {
85 			ret = -EINVAL;
86 			goto out;
87 		}
88 		event = &argv[0][2];
89 	} else {
90 		event = strchr(argv[0], ':');
91 		if (!event) {
92 			ret = -EINVAL;
93 			goto out;
94 		}
95 		event++;
96 	}
97 
98 	p = strchr(event, '/');
99 	if (p) {
100 		system = event;
101 		event = p + 1;
102 		*p = '\0';
103 	}
104 	if (!system && event[0] == '\0') {
105 		ret = -EINVAL;
106 		goto out;
107 	}
108 
109 	mutex_lock(&event_mutex);
110 	for_each_dyn_event_safe(pos, n) {
111 		if (type && type != pos->ops)
112 			continue;
113 		if (!pos->ops->match(system, event,
114 				argc - 1, (const char **)argv + 1, pos))
115 			continue;
116 
117 		ret = pos->ops->free(pos);
118 		if (ret)
119 			break;
120 	}
121 	mutex_unlock(&event_mutex);
122 out:
123 	argv_free(argv);
124 	return ret;
125 }
126 
127 static int create_dyn_event(const char *raw_command)
128 {
129 	struct dyn_event_operations *ops;
130 	int ret = -ENODEV;
131 
132 	if (raw_command[0] == '-' || raw_command[0] == '!')
133 		return dyn_event_release(raw_command, NULL);
134 
135 	mutex_lock(&dyn_event_ops_mutex);
136 	list_for_each_entry(ops, &dyn_event_ops_list, list) {
137 		ret = ops->create(raw_command);
138 		if (!ret || ret != -ECANCELED)
139 			break;
140 	}
141 	mutex_unlock(&dyn_event_ops_mutex);
142 	if (ret == -ECANCELED)
143 		ret = -EINVAL;
144 
145 	return ret;
146 }
147 
148 /* Protected by event_mutex */
149 LIST_HEAD(dyn_event_list);
150 
151 void *dyn_event_seq_start(struct seq_file *m, loff_t *pos)
152 {
153 	mutex_lock(&event_mutex);
154 	return seq_list_start(&dyn_event_list, *pos);
155 }
156 
157 void *dyn_event_seq_next(struct seq_file *m, void *v, loff_t *pos)
158 {
159 	return seq_list_next(v, &dyn_event_list, pos);
160 }
161 
162 void dyn_event_seq_stop(struct seq_file *m, void *v)
163 {
164 	mutex_unlock(&event_mutex);
165 }
166 
167 static int dyn_event_seq_show(struct seq_file *m, void *v)
168 {
169 	struct dyn_event *ev = v;
170 
171 	if (ev && ev->ops)
172 		return ev->ops->show(m, ev);
173 
174 	return 0;
175 }
176 
177 static const struct seq_operations dyn_event_seq_op = {
178 	.start	= dyn_event_seq_start,
179 	.next	= dyn_event_seq_next,
180 	.stop	= dyn_event_seq_stop,
181 	.show	= dyn_event_seq_show
182 };
183 
184 /*
185  * dyn_events_release_all - Release all specific events
186  * @type:	the dyn_event_operations * which filters releasing events
187  *
188  * This releases all events which ->ops matches @type. If @type is NULL,
189  * all events are released.
190  * Return -EBUSY if any of them are in use, and return other errors when
191  * it failed to free the given event. Except for -EBUSY, event releasing
192  * process will be aborted at that point and there may be some other
193  * releasable events on the list.
194  */
195 int dyn_events_release_all(struct dyn_event_operations *type)
196 {
197 	struct dyn_event *ev, *tmp;
198 	int ret = 0;
199 
200 	mutex_lock(&event_mutex);
201 	for_each_dyn_event(ev) {
202 		if (type && ev->ops != type)
203 			continue;
204 		if (ev->ops->is_busy(ev)) {
205 			ret = -EBUSY;
206 			goto out;
207 		}
208 	}
209 	for_each_dyn_event_safe(ev, tmp) {
210 		if (type && ev->ops != type)
211 			continue;
212 		ret = ev->ops->free(ev);
213 		if (ret)
214 			break;
215 	}
216 out:
217 	mutex_unlock(&event_mutex);
218 
219 	return ret;
220 }
221 
222 static int dyn_event_open(struct inode *inode, struct file *file)
223 {
224 	int ret;
225 
226 	ret = tracing_check_open_get_tr(NULL);
227 	if (ret)
228 		return ret;
229 
230 	if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
231 		ret = dyn_events_release_all(NULL);
232 		if (ret < 0)
233 			return ret;
234 	}
235 
236 	return seq_open(file, &dyn_event_seq_op);
237 }
238 
239 static ssize_t dyn_event_write(struct file *file, const char __user *buffer,
240 				size_t count, loff_t *ppos)
241 {
242 	return trace_parse_run_command(file, buffer, count, ppos,
243 				       create_dyn_event);
244 }
245 
246 static const struct file_operations dynamic_events_ops = {
247 	.owner          = THIS_MODULE,
248 	.open           = dyn_event_open,
249 	.read           = seq_read,
250 	.llseek         = seq_lseek,
251 	.release        = seq_release,
252 	.write		= dyn_event_write,
253 };
254 
255 /* Make a tracefs interface for controlling dynamic events */
256 static __init int init_dynamic_event(void)
257 {
258 	int ret;
259 
260 	ret = tracing_init_dentry();
261 	if (ret)
262 		return 0;
263 
264 	trace_create_file("dynamic_events", TRACE_MODE_WRITE, NULL,
265 			  NULL, &dynamic_events_ops);
266 
267 	return 0;
268 }
269 fs_initcall(init_dynamic_event);
270 
271 /**
272  * dynevent_arg_add - Add an arg to a dynevent_cmd
273  * @cmd: A pointer to the dynevent_cmd struct representing the new event cmd
274  * @arg: The argument to append to the current cmd
275  * @check_arg: An (optional) pointer to a function checking arg sanity
276  *
277  * Append an argument to a dynevent_cmd.  The argument string will be
278  * appended to the current cmd string, followed by a separator, if
279  * applicable.  Before the argument is added, the @check_arg function,
280  * if present, will be used to check the sanity of the current arg
281  * string.
282  *
283  * The cmd string and separator should be set using the
284  * dynevent_arg_init() before any arguments are added using this
285  * function.
286  *
287  * Return: 0 if successful, error otherwise.
288  */
289 int dynevent_arg_add(struct dynevent_cmd *cmd,
290 		     struct dynevent_arg *arg,
291 		     dynevent_check_arg_fn_t check_arg)
292 {
293 	int ret = 0;
294 
295 	if (check_arg) {
296 		ret = check_arg(arg);
297 		if (ret)
298 			return ret;
299 	}
300 
301 	ret = seq_buf_printf(&cmd->seq, " %s%c", arg->str, arg->separator);
302 	if (ret) {
303 		pr_err("String is too long: %s%c\n", arg->str, arg->separator);
304 		return -E2BIG;
305 	}
306 
307 	return ret;
308 }
309 
310 /**
311  * dynevent_arg_pair_add - Add an arg pair to a dynevent_cmd
312  * @cmd: A pointer to the dynevent_cmd struct representing the new event cmd
313  * @arg_pair: The argument pair to append to the current cmd
314  * @check_arg: An (optional) pointer to a function checking arg sanity
315  *
316  * Append an argument pair to a dynevent_cmd.  An argument pair
317  * consists of a left-hand-side argument and a right-hand-side
318  * argument separated by an operator, which can be whitespace, all
319  * followed by a separator, if applicable.  This can be used to add
320  * arguments of the form 'type variable_name;' or 'x+y'.
321  *
322  * The lhs argument string will be appended to the current cmd string,
323  * followed by an operator, if applicable, followed by the rhs string,
324  * followed finally by a separator, if applicable.  Before the
325  * argument is added, the @check_arg function, if present, will be
326  * used to check the sanity of the current arg strings.
327  *
328  * The cmd strings, operator, and separator should be set using the
329  * dynevent_arg_pair_init() before any arguments are added using this
330  * function.
331  *
332  * Return: 0 if successful, error otherwise.
333  */
334 int dynevent_arg_pair_add(struct dynevent_cmd *cmd,
335 			  struct dynevent_arg_pair *arg_pair,
336 			  dynevent_check_arg_fn_t check_arg)
337 {
338 	int ret = 0;
339 
340 	if (check_arg) {
341 		ret = check_arg(arg_pair);
342 		if (ret)
343 			return ret;
344 	}
345 
346 	ret = seq_buf_printf(&cmd->seq, " %s%c%s%c", arg_pair->lhs,
347 			     arg_pair->operator, arg_pair->rhs,
348 			     arg_pair->separator);
349 	if (ret) {
350 		pr_err("field string is too long: %s%c%s%c\n", arg_pair->lhs,
351 		       arg_pair->operator, arg_pair->rhs,
352 		       arg_pair->separator);
353 		return -E2BIG;
354 	}
355 
356 	return ret;
357 }
358 
359 /**
360  * dynevent_str_add - Add a string to a dynevent_cmd
361  * @cmd: A pointer to the dynevent_cmd struct representing the new event cmd
362  * @str: The string to append to the current cmd
363  *
364  * Append a string to a dynevent_cmd.  The string will be appended to
365  * the current cmd string as-is, with nothing prepended or appended.
366  *
367  * Return: 0 if successful, error otherwise.
368  */
369 int dynevent_str_add(struct dynevent_cmd *cmd, const char *str)
370 {
371 	int ret = 0;
372 
373 	ret = seq_buf_puts(&cmd->seq, str);
374 	if (ret) {
375 		pr_err("String is too long: %s\n", str);
376 		return -E2BIG;
377 	}
378 
379 	return ret;
380 }
381 
382 /**
383  * dynevent_cmd_init - Initialize a dynevent_cmd object
384  * @cmd: A pointer to the dynevent_cmd struct representing the cmd
385  * @buf: A pointer to the buffer to generate the command into
386  * @maxlen: The length of the buffer the command will be generated into
387  * @type: The type of the cmd, checked against further operations
388  * @run_command: The type-specific function that will actually run the command
389  *
390  * Initialize a dynevent_cmd.  A dynevent_cmd is used to build up and
391  * run dynamic event creation commands, such as commands for creating
392  * synthetic and kprobe events.  Before calling any of the functions
393  * used to build the command, a dynevent_cmd object should be
394  * instantiated and initialized using this function.
395  *
396  * The initialization sets things up by saving a pointer to the
397  * user-supplied buffer and its length via the @buf and @maxlen
398  * params, and by saving the cmd-specific @type and @run_command
399  * params which are used to check subsequent dynevent_cmd operations
400  * and actually run the command when complete.
401  */
402 void dynevent_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen,
403 		       enum dynevent_type type,
404 		       dynevent_create_fn_t run_command)
405 {
406 	memset(cmd, '\0', sizeof(*cmd));
407 
408 	seq_buf_init(&cmd->seq, buf, maxlen);
409 	cmd->type = type;
410 	cmd->run_command = run_command;
411 }
412 
413 /**
414  * dynevent_arg_init - Initialize a dynevent_arg object
415  * @arg: A pointer to the dynevent_arg struct representing the arg
416  * @separator: An (optional) separator, appended after adding the arg
417  *
418  * Initialize a dynevent_arg object.  A dynevent_arg represents an
419  * object used to append single arguments to the current command
420  * string.  After the arg string is successfully appended to the
421  * command string, the optional @separator is appended.  If no
422  * separator was specified when initializing the arg, a space will be
423  * appended.
424  */
425 void dynevent_arg_init(struct dynevent_arg *arg,
426 		       char separator)
427 {
428 	memset(arg, '\0', sizeof(*arg));
429 
430 	if (!separator)
431 		separator = ' ';
432 	arg->separator = separator;
433 }
434 
435 /**
436  * dynevent_arg_pair_init - Initialize a dynevent_arg_pair object
437  * @arg_pair: A pointer to the dynevent_arg_pair struct representing the arg
438  * @operator: An (optional) operator, appended after adding the first arg
439  * @separator: An (optional) separator, appended after adding the second arg
440  *
441  * Initialize a dynevent_arg_pair object.  A dynevent_arg_pair
442  * represents an object used to append argument pairs such as 'type
443  * variable_name;' or 'x+y' to the current command string.  An
444  * argument pair consists of a left-hand-side argument and a
445  * right-hand-side argument separated by an operator, which can be
446  * whitespace, all followed by a separator, if applicable.  After the
447  * first arg string is successfully appended to the command string,
448  * the optional @operator is appended, followed by the second arg and
449  * optional @separator.  If no separator was specified when
450  * initializing the arg, a space will be appended.
451  */
452 void dynevent_arg_pair_init(struct dynevent_arg_pair *arg_pair,
453 			    char operator, char separator)
454 {
455 	memset(arg_pair, '\0', sizeof(*arg_pair));
456 
457 	if (!operator)
458 		operator = ' ';
459 	arg_pair->operator = operator;
460 
461 	if (!separator)
462 		separator = ' ';
463 	arg_pair->separator = separator;
464 }
465 
466 /**
467  * dynevent_create - Create the dynamic event contained in dynevent_cmd
468  * @cmd: The dynevent_cmd object containing the dynamic event creation command
469  *
470  * Once a dynevent_cmd object has been successfully built up via the
471  * dynevent_cmd_init(), dynevent_arg_add() and dynevent_arg_pair_add()
472  * functions, this function runs the final command to actually create
473  * the event.
474  *
475  * Return: 0 if the event was successfully created, error otherwise.
476  */
477 int dynevent_create(struct dynevent_cmd *cmd)
478 {
479 	return cmd->run_command(cmd);
480 }
481 EXPORT_SYMBOL_GPL(dynevent_create);
482