blob: 9f2e8520b7486edcd947c4040116b3e4399c2f42 [file] [log] [blame]
Masami Hiramatsu5448d442018-11-05 18:02:08 +09001// 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
18static DEFINE_MUTEX(dyn_event_ops_mutex);
19static LIST_HEAD(dyn_event_ops_list);
20
21int 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
34int dyn_event_release(int argc, char **argv, struct dyn_event_operations *type)
35{
36 struct dyn_event *pos, *n;
37 char *system = NULL, *event, *p;
38 int ret = -ENOENT;
39
Masami Hiramatsu1ce25e92018-11-05 18:04:57 +090040 if (argv[0][0] == '-') {
41 if (argv[0][1] != ':')
42 return -EINVAL;
43 event = &argv[0][2];
44 } else {
45 event = strchr(argv[0], ':');
46 if (!event)
47 return -EINVAL;
48 event++;
49 }
Masami Hiramatsu30199132019-06-20 00:07:39 +090050 argc--; argv++;
Masami Hiramatsu5448d442018-11-05 18:02:08 +090051
Masami Hiramatsu5448d442018-11-05 18:02:08 +090052 p = strchr(event, '/');
53 if (p) {
54 system = event;
55 event = p + 1;
56 *p = '\0';
57 }
58 if (event[0] == '\0')
59 return -EINVAL;
60
61 mutex_lock(&event_mutex);
62 for_each_dyn_event_safe(pos, n) {
63 if (type && type != pos->ops)
64 continue;
Masami Hiramatsu30199132019-06-20 00:07:39 +090065 if (!pos->ops->match(system, event,
66 argc, (const char **)argv, pos))
Masami Hiramatsucb8e7a82019-06-20 00:07:29 +090067 continue;
68
69 ret = pos->ops->free(pos);
70 if (ret)
Masami Hiramatsu5448d442018-11-05 18:02:08 +090071 break;
Masami Hiramatsu5448d442018-11-05 18:02:08 +090072 }
73 mutex_unlock(&event_mutex);
74
75 return ret;
76}
77
78static int create_dyn_event(int argc, char **argv)
79{
80 struct dyn_event_operations *ops;
Frank Rowand3dee10d2019-03-21 23:58:20 -070081 int ret = -ENODEV;
Masami Hiramatsu5448d442018-11-05 18:02:08 +090082
Masami Hiramatsu1ce25e92018-11-05 18:04:57 +090083 if (argv[0][0] == '-' || argv[0][0] == '!')
Masami Hiramatsu5448d442018-11-05 18:02:08 +090084 return dyn_event_release(argc, argv, NULL);
85
86 mutex_lock(&dyn_event_ops_mutex);
87 list_for_each_entry(ops, &dyn_event_ops_list, list) {
88 ret = ops->create(argc, (const char **)argv);
89 if (!ret || ret != -ECANCELED)
90 break;
91 }
92 mutex_unlock(&dyn_event_ops_mutex);
93 if (ret == -ECANCELED)
94 ret = -EINVAL;
95
96 return ret;
97}
98
99/* Protected by event_mutex */
100LIST_HEAD(dyn_event_list);
101
102void *dyn_event_seq_start(struct seq_file *m, loff_t *pos)
103{
104 mutex_lock(&event_mutex);
105 return seq_list_start(&dyn_event_list, *pos);
106}
107
108void *dyn_event_seq_next(struct seq_file *m, void *v, loff_t *pos)
109{
110 return seq_list_next(v, &dyn_event_list, pos);
111}
112
113void dyn_event_seq_stop(struct seq_file *m, void *v)
114{
115 mutex_unlock(&event_mutex);
116}
117
118static int dyn_event_seq_show(struct seq_file *m, void *v)
119{
120 struct dyn_event *ev = v;
121
122 if (ev && ev->ops)
123 return ev->ops->show(m, ev);
124
125 return 0;
126}
127
128static const struct seq_operations dyn_event_seq_op = {
129 .start = dyn_event_seq_start,
130 .next = dyn_event_seq_next,
131 .stop = dyn_event_seq_stop,
132 .show = dyn_event_seq_show
133};
134
135/*
136 * dyn_events_release_all - Release all specific events
137 * @type: the dyn_event_operations * which filters releasing events
138 *
139 * This releases all events which ->ops matches @type. If @type is NULL,
140 * all events are released.
141 * Return -EBUSY if any of them are in use, and return other errors when
142 * it failed to free the given event. Except for -EBUSY, event releasing
143 * process will be aborted at that point and there may be some other
144 * releasable events on the list.
145 */
146int dyn_events_release_all(struct dyn_event_operations *type)
147{
148 struct dyn_event *ev, *tmp;
149 int ret = 0;
150
151 mutex_lock(&event_mutex);
152 for_each_dyn_event(ev) {
153 if (type && ev->ops != type)
154 continue;
155 if (ev->ops->is_busy(ev)) {
156 ret = -EBUSY;
157 goto out;
158 }
159 }
160 for_each_dyn_event_safe(ev, tmp) {
161 if (type && ev->ops != type)
162 continue;
163 ret = ev->ops->free(ev);
164 if (ret)
165 break;
166 }
167out:
168 mutex_unlock(&event_mutex);
169
170 return ret;
171}
172
173static int dyn_event_open(struct inode *inode, struct file *file)
174{
175 int ret;
176
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -0400177 ret = tracing_check_open_get_tr(NULL);
178 if (ret)
179 return ret;
180
Masami Hiramatsu5448d442018-11-05 18:02:08 +0900181 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
182 ret = dyn_events_release_all(NULL);
183 if (ret < 0)
184 return ret;
185 }
186
187 return seq_open(file, &dyn_event_seq_op);
188}
189
190static ssize_t dyn_event_write(struct file *file, const char __user *buffer,
191 size_t count, loff_t *ppos)
192{
193 return trace_parse_run_command(file, buffer, count, ppos,
194 create_dyn_event);
195}
196
197static const struct file_operations dynamic_events_ops = {
198 .owner = THIS_MODULE,
199 .open = dyn_event_open,
200 .read = seq_read,
201 .llseek = seq_lseek,
202 .release = seq_release,
203 .write = dyn_event_write,
204};
205
206/* Make a tracefs interface for controlling dynamic events */
207static __init int init_dynamic_event(void)
208{
209 struct dentry *d_tracer;
210 struct dentry *entry;
211
212 d_tracer = tracing_init_dentry();
213 if (IS_ERR(d_tracer))
214 return 0;
215
216 entry = tracefs_create_file("dynamic_events", 0644, d_tracer,
217 NULL, &dynamic_events_ops);
218
219 /* Event list interface */
220 if (!entry)
221 pr_warn("Could not create tracefs 'dynamic_events' entry\n");
222
223 return 0;
224}
225fs_initcall(init_dynamic_event);
Tom Zanussi86c54262020-01-29 12:59:24 -0600226
227/**
228 * dynevent_arg_add - Add an arg to a dynevent_cmd
229 * @cmd: A pointer to the dynevent_cmd struct representing the new event cmd
230 * @arg: The argument to append to the current cmd
Tom Zanussi74403b62020-01-31 15:55:32 -0600231 * @check_arg: An (optional) pointer to a function checking arg sanity
Tom Zanussi86c54262020-01-29 12:59:24 -0600232 *
233 * Append an argument to a dynevent_cmd. The argument string will be
234 * appended to the current cmd string, followed by a separator, if
Tom Zanussi74403b62020-01-31 15:55:32 -0600235 * applicable. Before the argument is added, the @check_arg function,
236 * if present, will be used to check the sanity of the current arg
237 * string.
Tom Zanussi86c54262020-01-29 12:59:24 -0600238 *
Tom Zanussi74403b62020-01-31 15:55:32 -0600239 * The cmd string and separator should be set using the
240 * dynevent_arg_init() before any arguments are added using this
241 * function.
Tom Zanussi86c54262020-01-29 12:59:24 -0600242 *
243 * Return: 0 if successful, error otherwise.
244 */
245int dynevent_arg_add(struct dynevent_cmd *cmd,
Tom Zanussi74403b62020-01-31 15:55:32 -0600246 struct dynevent_arg *arg,
247 dynevent_check_arg_fn_t check_arg)
Tom Zanussi86c54262020-01-29 12:59:24 -0600248{
249 int ret = 0;
Tom Zanussi86c54262020-01-29 12:59:24 -0600250
Tom Zanussi74403b62020-01-31 15:55:32 -0600251 if (check_arg) {
252 ret = check_arg(arg);
Tom Zanussi86c54262020-01-29 12:59:24 -0600253 if (ret)
254 return ret;
255 }
256
Tom Zanussi2b909272020-01-31 15:55:34 -0600257 ret = seq_buf_printf(&cmd->seq, " %s%c", arg->str, arg->separator);
258 if (ret) {
259 pr_err("String is too long: %s%c\n", arg->str, arg->separator);
Tom Zanussi86c54262020-01-29 12:59:24 -0600260 return -E2BIG;
261 }
Tom Zanussi86c54262020-01-29 12:59:24 -0600262
263 return ret;
264}
265
266/**
267 * dynevent_arg_pair_add - Add an arg pair to a dynevent_cmd
268 * @cmd: A pointer to the dynevent_cmd struct representing the new event cmd
269 * @arg_pair: The argument pair to append to the current cmd
Tom Zanussi74403b62020-01-31 15:55:32 -0600270 * @check_arg: An (optional) pointer to a function checking arg sanity
Tom Zanussi86c54262020-01-29 12:59:24 -0600271 *
272 * Append an argument pair to a dynevent_cmd. An argument pair
273 * consists of a left-hand-side argument and a right-hand-side
274 * argument separated by an operator, which can be whitespace, all
275 * followed by a separator, if applicable. This can be used to add
276 * arguments of the form 'type variable_name;' or 'x+y'.
277 *
278 * The lhs argument string will be appended to the current cmd string,
279 * followed by an operator, if applicable, followd by the rhs string,
Tom Zanussi74403b62020-01-31 15:55:32 -0600280 * followed finally by a separator, if applicable. Before the
281 * argument is added, the @check_arg function, if present, will be
282 * used to check the sanity of the current arg strings.
Tom Zanussi86c54262020-01-29 12:59:24 -0600283 *
Tom Zanussi74403b62020-01-31 15:55:32 -0600284 * The cmd strings, operator, and separator should be set using the
285 * dynevent_arg_pair_init() before any arguments are added using this
286 * function.
Tom Zanussi86c54262020-01-29 12:59:24 -0600287 *
288 * Return: 0 if successful, error otherwise.
289 */
290int dynevent_arg_pair_add(struct dynevent_cmd *cmd,
Tom Zanussi74403b62020-01-31 15:55:32 -0600291 struct dynevent_arg_pair *arg_pair,
292 dynevent_check_arg_fn_t check_arg)
Tom Zanussi86c54262020-01-29 12:59:24 -0600293{
294 int ret = 0;
Tom Zanussi86c54262020-01-29 12:59:24 -0600295
Tom Zanussi74403b62020-01-31 15:55:32 -0600296 if (check_arg) {
297 ret = check_arg(arg_pair);
Tom Zanussi86c54262020-01-29 12:59:24 -0600298 if (ret)
299 return ret;
300 }
301
Tom Zanussi2b909272020-01-31 15:55:34 -0600302 ret = seq_buf_printf(&cmd->seq, " %s%c%s%c", arg_pair->lhs,
303 arg_pair->operator, arg_pair->rhs,
304 arg_pair->separator);
305 if (ret) {
306 pr_err("field string is too long: %s%c%s%c\n", arg_pair->lhs,
307 arg_pair->operator, arg_pair->rhs,
308 arg_pair->separator);
Tom Zanussi86c54262020-01-29 12:59:24 -0600309 return -E2BIG;
310 }
Tom Zanussi86c54262020-01-29 12:59:24 -0600311
312 return ret;
313}
314
315/**
316 * dynevent_str_add - Add a string to a dynevent_cmd
317 * @cmd: A pointer to the dynevent_cmd struct representing the new event cmd
318 * @str: The string to append to the current cmd
319 *
320 * Append a string to a dynevent_cmd. The string will be appended to
321 * the current cmd string as-is, with nothing prepended or appended.
322 *
323 * Return: 0 if successful, error otherwise.
324 */
325int dynevent_str_add(struct dynevent_cmd *cmd, const char *str)
326{
327 int ret = 0;
Tom Zanussi86c54262020-01-29 12:59:24 -0600328
Tom Zanussi2b909272020-01-31 15:55:34 -0600329 ret = seq_buf_puts(&cmd->seq, str);
330 if (ret) {
Tom Zanussi86c54262020-01-29 12:59:24 -0600331 pr_err("String is too long: %s\n", str);
332 return -E2BIG;
333 }
Tom Zanussi86c54262020-01-29 12:59:24 -0600334
335 return ret;
336}
337
338/**
339 * dynevent_cmd_init - Initialize a dynevent_cmd object
340 * @cmd: A pointer to the dynevent_cmd struct representing the cmd
341 * @buf: A pointer to the buffer to generate the command into
342 * @maxlen: The length of the buffer the command will be generated into
343 * @type: The type of the cmd, checked against further operations
344 * @run_command: The type-specific function that will actually run the command
345 *
346 * Initialize a dynevent_cmd. A dynevent_cmd is used to build up and
347 * run dynamic event creation commands, such as commands for creating
348 * synthetic and kprobe events. Before calling any of the functions
349 * used to build the command, a dynevent_cmd object should be
350 * instantiated and initialized using this function.
351 *
352 * The initialization sets things up by saving a pointer to the
353 * user-supplied buffer and its length via the @buf and @maxlen
354 * params, and by saving the cmd-specific @type and @run_command
355 * params which are used to check subsequent dynevent_cmd operations
356 * and actually run the command when complete.
357 */
358void dynevent_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen,
359 enum dynevent_type type,
360 dynevent_create_fn_t run_command)
361{
362 memset(cmd, '\0', sizeof(*cmd));
363
Tom Zanussi2b909272020-01-31 15:55:34 -0600364 seq_buf_init(&cmd->seq, buf, maxlen);
Tom Zanussi86c54262020-01-29 12:59:24 -0600365 cmd->type = type;
366 cmd->run_command = run_command;
367}
368
369/**
370 * dynevent_arg_init - Initialize a dynevent_arg object
371 * @arg: A pointer to the dynevent_arg struct representing the arg
Tom Zanussi86c54262020-01-29 12:59:24 -0600372 * @separator: An (optional) separator, appended after adding the arg
373 *
374 * Initialize a dynevent_arg object. A dynevent_arg represents an
375 * object used to append single arguments to the current command
Tom Zanussi74403b62020-01-31 15:55:32 -0600376 * string. After the arg string is successfully appended to the
Tom Zanussi86c54262020-01-29 12:59:24 -0600377 * command string, the optional @separator is appended. If no
378 * separator was specified when initializing the arg, a space will be
379 * appended.
380 */
381void dynevent_arg_init(struct dynevent_arg *arg,
Tom Zanussi86c54262020-01-29 12:59:24 -0600382 char separator)
383{
384 memset(arg, '\0', sizeof(*arg));
385
386 if (!separator)
387 separator = ' ';
388 arg->separator = separator;
Tom Zanussi86c54262020-01-29 12:59:24 -0600389}
390
391/**
392 * dynevent_arg_pair_init - Initialize a dynevent_arg_pair object
393 * @arg_pair: A pointer to the dynevent_arg_pair struct representing the arg
Tom Zanussi86c54262020-01-29 12:59:24 -0600394 * @operator: An (optional) operator, appended after adding the first arg
395 * @separator: An (optional) separator, appended after adding the second arg
396 *
397 * Initialize a dynevent_arg_pair object. A dynevent_arg_pair
398 * represents an object used to append argument pairs such as 'type
399 * variable_name;' or 'x+y' to the current command string. An
400 * argument pair consists of a left-hand-side argument and a
401 * right-hand-side argument separated by an operator, which can be
Tom Zanussi74403b62020-01-31 15:55:32 -0600402 * whitespace, all followed by a separator, if applicable. After the
403 * first arg string is successfully appended to the command string,
404 * the optional @operator is appended, followed by the second arg and
405 * and optional @separator. If no separator was specified when
406 * initializing the arg, a space will be appended.
Tom Zanussi86c54262020-01-29 12:59:24 -0600407 */
408void dynevent_arg_pair_init(struct dynevent_arg_pair *arg_pair,
Tom Zanussi86c54262020-01-29 12:59:24 -0600409 char operator, char separator)
410{
411 memset(arg_pair, '\0', sizeof(*arg_pair));
412
413 if (!operator)
414 operator = ' ';
415 arg_pair->operator = operator;
416
417 if (!separator)
418 separator = ' ';
419 arg_pair->separator = separator;
Tom Zanussi86c54262020-01-29 12:59:24 -0600420}
421
422/**
423 * dynevent_create - Create the dynamic event contained in dynevent_cmd
424 * @cmd: The dynevent_cmd object containing the dynamic event creation command
425 *
426 * Once a dynevent_cmd object has been successfully built up via the
427 * dynevent_cmd_init(), dynevent_arg_add() and dynevent_arg_pair_add()
428 * functions, this function runs the final command to actually create
429 * the event.
430 *
431 * Return: 0 if the event was successfully created, error otherwise.
432 */
433int dynevent_create(struct dynevent_cmd *cmd)
434{
435 return cmd->run_command(cmd);
436}
437EXPORT_SYMBOL_GPL(dynevent_create);