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 DEFINE_MUTEX(dyn_event_ops_mutex);
20 static LIST_HEAD(dyn_event_ops_list);
21
trace_event_dyn_try_get_ref(struct trace_event_call * dyn_call)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
trace_event_dyn_put_ref(struct trace_event_call * call)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
trace_event_dyn_busy(struct trace_event_call * call)54 bool trace_event_dyn_busy(struct trace_event_call *call)
55 {
56 return atomic_read(&call->refcnt) != 0;
57 }
58
dyn_event_register(struct dyn_event_operations * ops)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
dyn_event_release(const char * raw_command,struct dyn_event_operations * type)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 tracing_reset_all_online_cpus();
122 mutex_unlock(&event_mutex);
123 out:
124 argv_free(argv);
125 return ret;
126 }
127
128 /*
129 * Locked version of event creation. The event creation must be protected by
130 * dyn_event_ops_mutex because of protecting trace_probe_log.
131 */
dyn_event_create(const char * raw_command,struct dyn_event_operations * type)132 int dyn_event_create(const char *raw_command, struct dyn_event_operations *type)
133 {
134 int ret;
135
136 mutex_lock(&dyn_event_ops_mutex);
137 ret = type->create(raw_command);
138 mutex_unlock(&dyn_event_ops_mutex);
139 return ret;
140 }
141
create_dyn_event(const char * raw_command)142 static int create_dyn_event(const char *raw_command)
143 {
144 struct dyn_event_operations *ops;
145 int ret = -ENODEV;
146
147 if (raw_command[0] == '-' || raw_command[0] == '!')
148 return dyn_event_release(raw_command, NULL);
149
150 mutex_lock(&dyn_event_ops_mutex);
151 list_for_each_entry(ops, &dyn_event_ops_list, list) {
152 ret = ops->create(raw_command);
153 if (!ret || ret != -ECANCELED)
154 break;
155 }
156 mutex_unlock(&dyn_event_ops_mutex);
157 if (ret == -ECANCELED)
158 ret = -EINVAL;
159
160 return ret;
161 }
162
163 /* Protected by event_mutex */
164 LIST_HEAD(dyn_event_list);
165
dyn_event_seq_start(struct seq_file * m,loff_t * pos)166 void *dyn_event_seq_start(struct seq_file *m, loff_t *pos)
167 {
168 mutex_lock(&event_mutex);
169 return seq_list_start(&dyn_event_list, *pos);
170 }
171
dyn_event_seq_next(struct seq_file * m,void * v,loff_t * pos)172 void *dyn_event_seq_next(struct seq_file *m, void *v, loff_t *pos)
173 {
174 return seq_list_next(v, &dyn_event_list, pos);
175 }
176
dyn_event_seq_stop(struct seq_file * m,void * v)177 void dyn_event_seq_stop(struct seq_file *m, void *v)
178 {
179 mutex_unlock(&event_mutex);
180 }
181
dyn_event_seq_show(struct seq_file * m,void * v)182 static int dyn_event_seq_show(struct seq_file *m, void *v)
183 {
184 struct dyn_event *ev = v;
185
186 if (ev && ev->ops)
187 return ev->ops->show(m, ev);
188
189 return 0;
190 }
191
192 static const struct seq_operations dyn_event_seq_op = {
193 .start = dyn_event_seq_start,
194 .next = dyn_event_seq_next,
195 .stop = dyn_event_seq_stop,
196 .show = dyn_event_seq_show
197 };
198
199 /*
200 * dyn_events_release_all - Release all specific events
201 * @type: the dyn_event_operations * which filters releasing events
202 *
203 * This releases all events which ->ops matches @type. If @type is NULL,
204 * all events are released.
205 * Return -EBUSY if any of them are in use, and return other errors when
206 * it failed to free the given event. Except for -EBUSY, event releasing
207 * process will be aborted at that point and there may be some other
208 * releasable events on the list.
209 */
dyn_events_release_all(struct dyn_event_operations * type)210 int dyn_events_release_all(struct dyn_event_operations *type)
211 {
212 struct dyn_event *ev, *tmp;
213 int ret = 0;
214
215 mutex_lock(&event_mutex);
216 for_each_dyn_event(ev) {
217 if (type && ev->ops != type)
218 continue;
219 if (ev->ops->is_busy(ev)) {
220 ret = -EBUSY;
221 goto out;
222 }
223 }
224 for_each_dyn_event_safe(ev, tmp) {
225 if (type && ev->ops != type)
226 continue;
227 ret = ev->ops->free(ev);
228 if (ret)
229 break;
230 }
231 out:
232 tracing_reset_all_online_cpus();
233 mutex_unlock(&event_mutex);
234
235 return ret;
236 }
237
dyn_event_open(struct inode * inode,struct file * file)238 static int dyn_event_open(struct inode *inode, struct file *file)
239 {
240 int ret;
241
242 ret = security_locked_down(LOCKDOWN_TRACEFS);
243 if (ret)
244 return ret;
245
246 ret = tracing_check_open_get_tr(NULL);
247 if (ret)
248 return ret;
249
250 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
251 ret = dyn_events_release_all(NULL);
252 if (ret < 0)
253 return ret;
254 }
255
256 return seq_open(file, &dyn_event_seq_op);
257 }
258
dyn_event_write(struct file * file,const char __user * buffer,size_t count,loff_t * ppos)259 static ssize_t dyn_event_write(struct file *file, const char __user *buffer,
260 size_t count, loff_t *ppos)
261 {
262 return trace_parse_run_command(file, buffer, count, ppos,
263 create_dyn_event);
264 }
265
266 static const struct file_operations dynamic_events_ops = {
267 .owner = THIS_MODULE,
268 .open = dyn_event_open,
269 .read = seq_read,
270 .llseek = seq_lseek,
271 .release = seq_release,
272 .write = dyn_event_write,
273 };
274
275 /* Make a tracefs interface for controlling dynamic events */
init_dynamic_event(void)276 static __init int init_dynamic_event(void)
277 {
278 int ret;
279
280 ret = tracing_init_dentry();
281 if (ret)
282 return 0;
283
284 trace_create_file("dynamic_events", TRACE_MODE_WRITE, NULL,
285 NULL, &dynamic_events_ops);
286
287 return 0;
288 }
289 fs_initcall(init_dynamic_event);
290
291 /**
292 * dynevent_arg_add - Add an arg to a dynevent_cmd
293 * @cmd: A pointer to the dynevent_cmd struct representing the new event cmd
294 * @arg: The argument to append to the current cmd
295 * @check_arg: An (optional) pointer to a function checking arg sanity
296 *
297 * Append an argument to a dynevent_cmd. The argument string will be
298 * appended to the current cmd string, followed by a separator, if
299 * applicable. Before the argument is added, the @check_arg function,
300 * if present, will be used to check the sanity of the current arg
301 * string.
302 *
303 * The cmd string and separator should be set using the
304 * dynevent_arg_init() before any arguments are added using this
305 * function.
306 *
307 * Return: 0 if successful, error otherwise.
308 */
dynevent_arg_add(struct dynevent_cmd * cmd,struct dynevent_arg * arg,dynevent_check_arg_fn_t check_arg)309 int dynevent_arg_add(struct dynevent_cmd *cmd,
310 struct dynevent_arg *arg,
311 dynevent_check_arg_fn_t check_arg)
312 {
313 int ret = 0;
314
315 if (check_arg) {
316 ret = check_arg(arg);
317 if (ret)
318 return ret;
319 }
320
321 ret = seq_buf_printf(&cmd->seq, " %s%c", arg->str, arg->separator);
322 if (ret) {
323 pr_err("String is too long: %s%c\n", arg->str, arg->separator);
324 return -E2BIG;
325 }
326
327 return ret;
328 }
329
330 /**
331 * dynevent_arg_pair_add - Add an arg pair to a dynevent_cmd
332 * @cmd: A pointer to the dynevent_cmd struct representing the new event cmd
333 * @arg_pair: The argument pair to append to the current cmd
334 * @check_arg: An (optional) pointer to a function checking arg sanity
335 *
336 * Append an argument pair to a dynevent_cmd. An argument pair
337 * consists of a left-hand-side argument and a right-hand-side
338 * argument separated by an operator, which can be whitespace, all
339 * followed by a separator, if applicable. This can be used to add
340 * arguments of the form 'type variable_name;' or 'x+y'.
341 *
342 * The lhs argument string will be appended to the current cmd string,
343 * followed by an operator, if applicable, followed by the rhs string,
344 * followed finally by a separator, if applicable. Before the
345 * argument is added, the @check_arg function, if present, will be
346 * used to check the sanity of the current arg strings.
347 *
348 * The cmd strings, operator, and separator should be set using the
349 * dynevent_arg_pair_init() before any arguments are added using this
350 * function.
351 *
352 * Return: 0 if successful, error otherwise.
353 */
dynevent_arg_pair_add(struct dynevent_cmd * cmd,struct dynevent_arg_pair * arg_pair,dynevent_check_arg_fn_t check_arg)354 int dynevent_arg_pair_add(struct dynevent_cmd *cmd,
355 struct dynevent_arg_pair *arg_pair,
356 dynevent_check_arg_fn_t check_arg)
357 {
358 int ret = 0;
359
360 if (check_arg) {
361 ret = check_arg(arg_pair);
362 if (ret)
363 return ret;
364 }
365
366 ret = seq_buf_printf(&cmd->seq, " %s%c%s%c", arg_pair->lhs,
367 arg_pair->operator, arg_pair->rhs,
368 arg_pair->separator);
369 if (ret) {
370 pr_err("field string is too long: %s%c%s%c\n", arg_pair->lhs,
371 arg_pair->operator, arg_pair->rhs,
372 arg_pair->separator);
373 return -E2BIG;
374 }
375
376 return ret;
377 }
378
379 /**
380 * dynevent_str_add - Add a string to a dynevent_cmd
381 * @cmd: A pointer to the dynevent_cmd struct representing the new event cmd
382 * @str: The string to append to the current cmd
383 *
384 * Append a string to a dynevent_cmd. The string will be appended to
385 * the current cmd string as-is, with nothing prepended or appended.
386 *
387 * Return: 0 if successful, error otherwise.
388 */
dynevent_str_add(struct dynevent_cmd * cmd,const char * str)389 int dynevent_str_add(struct dynevent_cmd *cmd, const char *str)
390 {
391 int ret = 0;
392
393 ret = seq_buf_puts(&cmd->seq, str);
394 if (ret) {
395 pr_err("String is too long: %s\n", str);
396 return -E2BIG;
397 }
398
399 return ret;
400 }
401
402 /**
403 * dynevent_cmd_init - Initialize a dynevent_cmd object
404 * @cmd: A pointer to the dynevent_cmd struct representing the cmd
405 * @buf: A pointer to the buffer to generate the command into
406 * @maxlen: The length of the buffer the command will be generated into
407 * @type: The type of the cmd, checked against further operations
408 * @run_command: The type-specific function that will actually run the command
409 *
410 * Initialize a dynevent_cmd. A dynevent_cmd is used to build up and
411 * run dynamic event creation commands, such as commands for creating
412 * synthetic and kprobe events. Before calling any of the functions
413 * used to build the command, a dynevent_cmd object should be
414 * instantiated and initialized using this function.
415 *
416 * The initialization sets things up by saving a pointer to the
417 * user-supplied buffer and its length via the @buf and @maxlen
418 * params, and by saving the cmd-specific @type and @run_command
419 * params which are used to check subsequent dynevent_cmd operations
420 * and actually run the command when complete.
421 */
dynevent_cmd_init(struct dynevent_cmd * cmd,char * buf,int maxlen,enum dynevent_type type,dynevent_create_fn_t run_command)422 void dynevent_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen,
423 enum dynevent_type type,
424 dynevent_create_fn_t run_command)
425 {
426 memset(cmd, '\0', sizeof(*cmd));
427
428 seq_buf_init(&cmd->seq, buf, maxlen);
429 cmd->type = type;
430 cmd->run_command = run_command;
431 }
432
433 /**
434 * dynevent_arg_init - Initialize a dynevent_arg object
435 * @arg: A pointer to the dynevent_arg struct representing the arg
436 * @separator: An (optional) separator, appended after adding the arg
437 *
438 * Initialize a dynevent_arg object. A dynevent_arg represents an
439 * object used to append single arguments to the current command
440 * string. After the arg string is successfully appended to the
441 * command string, the optional @separator is appended. If no
442 * separator was specified when initializing the arg, a space will be
443 * appended.
444 */
dynevent_arg_init(struct dynevent_arg * arg,char separator)445 void dynevent_arg_init(struct dynevent_arg *arg,
446 char separator)
447 {
448 memset(arg, '\0', sizeof(*arg));
449
450 if (!separator)
451 separator = ' ';
452 arg->separator = separator;
453 }
454
455 /**
456 * dynevent_arg_pair_init - Initialize a dynevent_arg_pair object
457 * @arg_pair: A pointer to the dynevent_arg_pair struct representing the arg
458 * @operator: An (optional) operator, appended after adding the first arg
459 * @separator: An (optional) separator, appended after adding the second arg
460 *
461 * Initialize a dynevent_arg_pair object. A dynevent_arg_pair
462 * represents an object used to append argument pairs such as 'type
463 * variable_name;' or 'x+y' to the current command string. An
464 * argument pair consists of a left-hand-side argument and a
465 * right-hand-side argument separated by an operator, which can be
466 * whitespace, all followed by a separator, if applicable. After the
467 * first arg string is successfully appended to the command string,
468 * the optional @operator is appended, followed by the second arg and
469 * optional @separator. If no separator was specified when
470 * initializing the arg, a space will be appended.
471 */
dynevent_arg_pair_init(struct dynevent_arg_pair * arg_pair,char operator,char separator)472 void dynevent_arg_pair_init(struct dynevent_arg_pair *arg_pair,
473 char operator, char separator)
474 {
475 memset(arg_pair, '\0', sizeof(*arg_pair));
476
477 if (!operator)
478 operator = ' ';
479 arg_pair->operator = operator;
480
481 if (!separator)
482 separator = ' ';
483 arg_pair->separator = separator;
484 }
485
486 /**
487 * dynevent_create - Create the dynamic event contained in dynevent_cmd
488 * @cmd: The dynevent_cmd object containing the dynamic event creation command
489 *
490 * Once a dynevent_cmd object has been successfully built up via the
491 * dynevent_cmd_init(), dynevent_arg_add() and dynevent_arg_pair_add()
492 * functions, this function runs the final command to actually create
493 * the event.
494 *
495 * Return: 0 if the event was successfully created, error otherwise.
496 */
dynevent_create(struct dynevent_cmd * cmd)497 int dynevent_create(struct dynevent_cmd *cmd)
498 {
499 return cmd->run_command(cmd);
500 }
501 EXPORT_SYMBOL_GPL(dynevent_create);
502