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