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