xref: /openbmc/linux/kernel/trace/trace_dynevent.c (revision 2bdd5238)
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 (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 	struct dentry *entry;
259 	int ret;
260 
261 	ret = tracing_init_dentry();
262 	if (ret)
263 		return 0;
264 
265 	entry = tracefs_create_file("dynamic_events", 0644, NULL,
266 				    NULL, &dynamic_events_ops);
267 
268 	/* Event list interface */
269 	if (!entry)
270 		pr_warn("Could not create tracefs 'dynamic_events' entry\n");
271 
272 	return 0;
273 }
274 fs_initcall(init_dynamic_event);
275 
276 /**
277  * dynevent_arg_add - Add an arg to a dynevent_cmd
278  * @cmd: A pointer to the dynevent_cmd struct representing the new event cmd
279  * @arg: The argument to append to the current cmd
280  * @check_arg: An (optional) pointer to a function checking arg sanity
281  *
282  * Append an argument to a dynevent_cmd.  The argument string will be
283  * appended to the current cmd string, followed by a separator, if
284  * applicable.  Before the argument is added, the @check_arg function,
285  * if present, will be used to check the sanity of the current arg
286  * string.
287  *
288  * The cmd string and separator should be set using the
289  * dynevent_arg_init() before any arguments are added using this
290  * function.
291  *
292  * Return: 0 if successful, error otherwise.
293  */
294 int dynevent_arg_add(struct dynevent_cmd *cmd,
295 		     struct dynevent_arg *arg,
296 		     dynevent_check_arg_fn_t check_arg)
297 {
298 	int ret = 0;
299 
300 	if (check_arg) {
301 		ret = check_arg(arg);
302 		if (ret)
303 			return ret;
304 	}
305 
306 	ret = seq_buf_printf(&cmd->seq, " %s%c", arg->str, arg->separator);
307 	if (ret) {
308 		pr_err("String is too long: %s%c\n", arg->str, arg->separator);
309 		return -E2BIG;
310 	}
311 
312 	return ret;
313 }
314 
315 /**
316  * dynevent_arg_pair_add - Add an arg pair to a dynevent_cmd
317  * @cmd: A pointer to the dynevent_cmd struct representing the new event cmd
318  * @arg_pair: The argument pair to append to the current cmd
319  * @check_arg: An (optional) pointer to a function checking arg sanity
320  *
321  * Append an argument pair to a dynevent_cmd.  An argument pair
322  * consists of a left-hand-side argument and a right-hand-side
323  * argument separated by an operator, which can be whitespace, all
324  * followed by a separator, if applicable.  This can be used to add
325  * arguments of the form 'type variable_name;' or 'x+y'.
326  *
327  * The lhs argument string will be appended to the current cmd string,
328  * followed by an operator, if applicable, followed by the rhs string,
329  * followed finally by a separator, if applicable.  Before the
330  * argument is added, the @check_arg function, if present, will be
331  * used to check the sanity of the current arg strings.
332  *
333  * The cmd strings, operator, and separator should be set using the
334  * dynevent_arg_pair_init() before any arguments are added using this
335  * function.
336  *
337  * Return: 0 if successful, error otherwise.
338  */
339 int dynevent_arg_pair_add(struct dynevent_cmd *cmd,
340 			  struct dynevent_arg_pair *arg_pair,
341 			  dynevent_check_arg_fn_t check_arg)
342 {
343 	int ret = 0;
344 
345 	if (check_arg) {
346 		ret = check_arg(arg_pair);
347 		if (ret)
348 			return ret;
349 	}
350 
351 	ret = seq_buf_printf(&cmd->seq, " %s%c%s%c", arg_pair->lhs,
352 			     arg_pair->operator, arg_pair->rhs,
353 			     arg_pair->separator);
354 	if (ret) {
355 		pr_err("field string is too long: %s%c%s%c\n", arg_pair->lhs,
356 		       arg_pair->operator, arg_pair->rhs,
357 		       arg_pair->separator);
358 		return -E2BIG;
359 	}
360 
361 	return ret;
362 }
363 
364 /**
365  * dynevent_str_add - Add a string to a dynevent_cmd
366  * @cmd: A pointer to the dynevent_cmd struct representing the new event cmd
367  * @str: The string to append to the current cmd
368  *
369  * Append a string to a dynevent_cmd.  The string will be appended to
370  * the current cmd string as-is, with nothing prepended or appended.
371  *
372  * Return: 0 if successful, error otherwise.
373  */
374 int dynevent_str_add(struct dynevent_cmd *cmd, const char *str)
375 {
376 	int ret = 0;
377 
378 	ret = seq_buf_puts(&cmd->seq, str);
379 	if (ret) {
380 		pr_err("String is too long: %s\n", str);
381 		return -E2BIG;
382 	}
383 
384 	return ret;
385 }
386 
387 /**
388  * dynevent_cmd_init - Initialize a dynevent_cmd object
389  * @cmd: A pointer to the dynevent_cmd struct representing the cmd
390  * @buf: A pointer to the buffer to generate the command into
391  * @maxlen: The length of the buffer the command will be generated into
392  * @type: The type of the cmd, checked against further operations
393  * @run_command: The type-specific function that will actually run the command
394  *
395  * Initialize a dynevent_cmd.  A dynevent_cmd is used to build up and
396  * run dynamic event creation commands, such as commands for creating
397  * synthetic and kprobe events.  Before calling any of the functions
398  * used to build the command, a dynevent_cmd object should be
399  * instantiated and initialized using this function.
400  *
401  * The initialization sets things up by saving a pointer to the
402  * user-supplied buffer and its length via the @buf and @maxlen
403  * params, and by saving the cmd-specific @type and @run_command
404  * params which are used to check subsequent dynevent_cmd operations
405  * and actually run the command when complete.
406  */
407 void dynevent_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen,
408 		       enum dynevent_type type,
409 		       dynevent_create_fn_t run_command)
410 {
411 	memset(cmd, '\0', sizeof(*cmd));
412 
413 	seq_buf_init(&cmd->seq, buf, maxlen);
414 	cmd->type = type;
415 	cmd->run_command = run_command;
416 }
417 
418 /**
419  * dynevent_arg_init - Initialize a dynevent_arg object
420  * @arg: A pointer to the dynevent_arg struct representing the arg
421  * @separator: An (optional) separator, appended after adding the arg
422  *
423  * Initialize a dynevent_arg object.  A dynevent_arg represents an
424  * object used to append single arguments to the current command
425  * string.  After the arg string is successfully appended to the
426  * command string, the optional @separator is appended.  If no
427  * separator was specified when initializing the arg, a space will be
428  * appended.
429  */
430 void dynevent_arg_init(struct dynevent_arg *arg,
431 		       char separator)
432 {
433 	memset(arg, '\0', sizeof(*arg));
434 
435 	if (!separator)
436 		separator = ' ';
437 	arg->separator = separator;
438 }
439 
440 /**
441  * dynevent_arg_pair_init - Initialize a dynevent_arg_pair object
442  * @arg_pair: A pointer to the dynevent_arg_pair struct representing the arg
443  * @operator: An (optional) operator, appended after adding the first arg
444  * @separator: An (optional) separator, appended after adding the second arg
445  *
446  * Initialize a dynevent_arg_pair object.  A dynevent_arg_pair
447  * represents an object used to append argument pairs such as 'type
448  * variable_name;' or 'x+y' to the current command string.  An
449  * argument pair consists of a left-hand-side argument and a
450  * right-hand-side argument separated by an operator, which can be
451  * whitespace, all followed by a separator, if applicable.  After the
452  * first arg string is successfully appended to the command string,
453  * the optional @operator is appended, followed by the second arg and
454  * optional @separator.  If no separator was specified when
455  * initializing the arg, a space will be appended.
456  */
457 void dynevent_arg_pair_init(struct dynevent_arg_pair *arg_pair,
458 			    char operator, char separator)
459 {
460 	memset(arg_pair, '\0', sizeof(*arg_pair));
461 
462 	if (!operator)
463 		operator = ' ';
464 	arg_pair->operator = operator;
465 
466 	if (!separator)
467 		separator = ' ';
468 	arg_pair->separator = separator;
469 }
470 
471 /**
472  * dynevent_create - Create the dynamic event contained in dynevent_cmd
473  * @cmd: The dynevent_cmd object containing the dynamic event creation command
474  *
475  * Once a dynevent_cmd object has been successfully built up via the
476  * dynevent_cmd_init(), dynevent_arg_add() and dynevent_arg_pair_add()
477  * functions, this function runs the final command to actually create
478  * the event.
479  *
480  * Return: 0 if the event was successfully created, error otherwise.
481  */
482 int dynevent_create(struct dynevent_cmd *cmd)
483 {
484 	return cmd->run_command(cmd);
485 }
486 EXPORT_SYMBOL_GPL(dynevent_create);
487