blob: e34e8182ee4b5ecafe8f39b5ae0f779db6dc735d [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"
Steven Rostedt (VMware)1d185382021-08-16 23:42:57 -040016#include "trace_output.h" /* for trace_event_sem */
Masami Hiramatsu5448d442018-11-05 18:02:08 +090017#include "trace_dynevent.h"
18
19static DEFINE_MUTEX(dyn_event_ops_mutex);
20static LIST_HEAD(dyn_event_ops_list);
21
Steven Rostedt (VMware)1d185382021-08-16 23:42:57 -040022bool 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
41void 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
54bool trace_event_dyn_busy(struct trace_event_call *call)
55{
56 return atomic_read(&call->refcnt) != 0;
57}
58
Masami Hiramatsu5448d442018-11-05 18:02:08 +090059int 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
Masami Hiramatsud2622712021-02-01 13:48:11 -060072int dyn_event_release(const char *raw_command, struct dyn_event_operations *type)
Masami Hiramatsu5448d442018-11-05 18:02:08 +090073{
74 struct dyn_event *pos, *n;
75 char *system = NULL, *event, *p;
Masami Hiramatsud2622712021-02-01 13:48:11 -060076 int argc, ret = -ENOENT;
77 char **argv;
78
79 argv = argv_split(GFP_KERNEL, raw_command, &argc);
80 if (!argv)
81 return -ENOMEM;
Masami Hiramatsu5448d442018-11-05 18:02:08 +090082
Masami Hiramatsu1ce25e92018-11-05 18:04:57 +090083 if (argv[0][0] == '-') {
Masami Hiramatsud2622712021-02-01 13:48:11 -060084 if (argv[0][1] != ':') {
85 ret = -EINVAL;
86 goto out;
87 }
Masami Hiramatsu1ce25e92018-11-05 18:04:57 +090088 event = &argv[0][2];
89 } else {
90 event = strchr(argv[0], ':');
Masami Hiramatsud2622712021-02-01 13:48:11 -060091 if (!event) {
92 ret = -EINVAL;
93 goto out;
94 }
Masami Hiramatsu1ce25e92018-11-05 18:04:57 +090095 event++;
96 }
Masami Hiramatsu5448d442018-11-05 18:02:08 +090097
Masami Hiramatsu5448d442018-11-05 18:02:08 +090098 p = strchr(event, '/');
99 if (p) {
100 system = event;
101 event = p + 1;
102 *p = '\0';
103 }
Christophe JAILLET8db403b2021-04-11 12:21:54 +0200104 if (event[0] == '\0') {
105 ret = -EINVAL;
106 goto out;
107 }
Masami Hiramatsu5448d442018-11-05 18:02:08 +0900108
109 mutex_lock(&event_mutex);
110 for_each_dyn_event_safe(pos, n) {
111 if (type && type != pos->ops)
112 continue;
Masami Hiramatsu30199132019-06-20 00:07:39 +0900113 if (!pos->ops->match(system, event,
Masami Hiramatsud2622712021-02-01 13:48:11 -0600114 argc - 1, (const char **)argv + 1, pos))
Masami Hiramatsucb8e7a82019-06-20 00:07:29 +0900115 continue;
116
117 ret = pos->ops->free(pos);
118 if (ret)
Masami Hiramatsu5448d442018-11-05 18:02:08 +0900119 break;
Masami Hiramatsu5448d442018-11-05 18:02:08 +0900120 }
121 mutex_unlock(&event_mutex);
Masami Hiramatsud2622712021-02-01 13:48:11 -0600122out:
123 argv_free(argv);
Masami Hiramatsu5448d442018-11-05 18:02:08 +0900124 return ret;
125}
126
Masami Hiramatsud2622712021-02-01 13:48:11 -0600127static int create_dyn_event(const char *raw_command)
Masami Hiramatsu5448d442018-11-05 18:02:08 +0900128{
129 struct dyn_event_operations *ops;
Frank Rowand3dee10d2019-03-21 23:58:20 -0700130 int ret = -ENODEV;
Masami Hiramatsu5448d442018-11-05 18:02:08 +0900131
Masami Hiramatsud2622712021-02-01 13:48:11 -0600132 if (raw_command[0] == '-' || raw_command[0] == '!')
133 return dyn_event_release(raw_command, NULL);
Masami Hiramatsu5448d442018-11-05 18:02:08 +0900134
135 mutex_lock(&dyn_event_ops_mutex);
136 list_for_each_entry(ops, &dyn_event_ops_list, list) {
Masami Hiramatsud2622712021-02-01 13:48:11 -0600137 ret = ops->create(raw_command);
Masami Hiramatsu5448d442018-11-05 18:02:08 +0900138 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 */
149LIST_HEAD(dyn_event_list);
150
151void *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
157void *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
162void dyn_event_seq_stop(struct seq_file *m, void *v)
163{
164 mutex_unlock(&event_mutex);
165}
166
167static 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
177static 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 */
195int 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 }
216out:
217 mutex_unlock(&event_mutex);
218
219 return ret;
220}
221
222static int dyn_event_open(struct inode *inode, struct file *file)
223{
224 int ret;
225
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -0400226 ret = tracing_check_open_get_tr(NULL);
227 if (ret)
228 return ret;
229
Masami Hiramatsu5448d442018-11-05 18:02:08 +0900230 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
239static 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
246static 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 */
256static __init int init_dynamic_event(void)
257{
Masami Hiramatsu5448d442018-11-05 18:02:08 +0900258 struct dentry *entry;
Wei Yang22c36b12020-07-12 09:10:36 +0800259 int ret;
Masami Hiramatsu5448d442018-11-05 18:02:08 +0900260
Wei Yang22c36b12020-07-12 09:10:36 +0800261 ret = tracing_init_dentry();
262 if (ret)
Masami Hiramatsu5448d442018-11-05 18:02:08 +0900263 return 0;
264
Steven Rostedt (VMware)21ccc9c2021-08-18 11:24:51 -0400265 entry = tracefs_create_file("dynamic_events", TRACE_MODE_WRITE, NULL,
Masami Hiramatsu5448d442018-11-05 18:02:08 +0900266 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}
274fs_initcall(init_dynamic_event);
Tom Zanussi86c54262020-01-29 12:59:24 -0600275
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
Tom Zanussi74403b62020-01-31 15:55:32 -0600280 * @check_arg: An (optional) pointer to a function checking arg sanity
Tom Zanussi86c54262020-01-29 12:59:24 -0600281 *
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
Tom Zanussi74403b62020-01-31 15:55:32 -0600284 * 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.
Tom Zanussi86c54262020-01-29 12:59:24 -0600287 *
Tom Zanussi74403b62020-01-31 15:55:32 -0600288 * The cmd string and separator should be set using the
289 * dynevent_arg_init() before any arguments are added using this
290 * function.
Tom Zanussi86c54262020-01-29 12:59:24 -0600291 *
292 * Return: 0 if successful, error otherwise.
293 */
294int dynevent_arg_add(struct dynevent_cmd *cmd,
Tom Zanussi74403b62020-01-31 15:55:32 -0600295 struct dynevent_arg *arg,
296 dynevent_check_arg_fn_t check_arg)
Tom Zanussi86c54262020-01-29 12:59:24 -0600297{
298 int ret = 0;
Tom Zanussi86c54262020-01-29 12:59:24 -0600299
Tom Zanussi74403b62020-01-31 15:55:32 -0600300 if (check_arg) {
301 ret = check_arg(arg);
Tom Zanussi86c54262020-01-29 12:59:24 -0600302 if (ret)
303 return ret;
304 }
305
Tom Zanussi2b909272020-01-31 15:55:34 -0600306 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);
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_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
Tom Zanussi74403b62020-01-31 15:55:32 -0600319 * @check_arg: An (optional) pointer to a function checking arg sanity
Tom Zanussi86c54262020-01-29 12:59:24 -0600320 *
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,
Qiujun Huang2b5894c2020-10-29 23:05:54 +0800328 * followed by an operator, if applicable, followed by the rhs string,
Tom Zanussi74403b62020-01-31 15:55:32 -0600329 * 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.
Tom Zanussi86c54262020-01-29 12:59:24 -0600332 *
Tom Zanussi74403b62020-01-31 15:55:32 -0600333 * 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.
Tom Zanussi86c54262020-01-29 12:59:24 -0600336 *
337 * Return: 0 if successful, error otherwise.
338 */
339int dynevent_arg_pair_add(struct dynevent_cmd *cmd,
Tom Zanussi74403b62020-01-31 15:55:32 -0600340 struct dynevent_arg_pair *arg_pair,
341 dynevent_check_arg_fn_t check_arg)
Tom Zanussi86c54262020-01-29 12:59:24 -0600342{
343 int ret = 0;
Tom Zanussi86c54262020-01-29 12:59:24 -0600344
Tom Zanussi74403b62020-01-31 15:55:32 -0600345 if (check_arg) {
346 ret = check_arg(arg_pair);
Tom Zanussi86c54262020-01-29 12:59:24 -0600347 if (ret)
348 return ret;
349 }
350
Tom Zanussi2b909272020-01-31 15:55:34 -0600351 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);
Tom Zanussi86c54262020-01-29 12:59:24 -0600358 return -E2BIG;
359 }
Tom Zanussi86c54262020-01-29 12:59:24 -0600360
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 */
374int dynevent_str_add(struct dynevent_cmd *cmd, const char *str)
375{
376 int ret = 0;
Tom Zanussi86c54262020-01-29 12:59:24 -0600377
Tom Zanussi2b909272020-01-31 15:55:34 -0600378 ret = seq_buf_puts(&cmd->seq, str);
379 if (ret) {
Tom Zanussi86c54262020-01-29 12:59:24 -0600380 pr_err("String is too long: %s\n", str);
381 return -E2BIG;
382 }
Tom Zanussi86c54262020-01-29 12:59:24 -0600383
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 */
407void 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
Tom Zanussi2b909272020-01-31 15:55:34 -0600413 seq_buf_init(&cmd->seq, buf, maxlen);
Tom Zanussi86c54262020-01-29 12:59:24 -0600414 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
Tom Zanussi86c54262020-01-29 12:59:24 -0600421 * @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
Tom Zanussi74403b62020-01-31 15:55:32 -0600425 * string. After the arg string is successfully appended to the
Tom Zanussi86c54262020-01-29 12:59:24 -0600426 * command string, the optional @separator is appended. If no
427 * separator was specified when initializing the arg, a space will be
428 * appended.
429 */
430void dynevent_arg_init(struct dynevent_arg *arg,
Tom Zanussi86c54262020-01-29 12:59:24 -0600431 char separator)
432{
433 memset(arg, '\0', sizeof(*arg));
434
435 if (!separator)
436 separator = ' ';
437 arg->separator = separator;
Tom Zanussi86c54262020-01-29 12:59:24 -0600438}
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
Tom Zanussi86c54262020-01-29 12:59:24 -0600443 * @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
Tom Zanussi74403b62020-01-31 15:55:32 -0600451 * 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
Randy Dunlap5c8c2062020-08-06 20:32:59 -0700454 * optional @separator. If no separator was specified when
Tom Zanussi74403b62020-01-31 15:55:32 -0600455 * initializing the arg, a space will be appended.
Tom Zanussi86c54262020-01-29 12:59:24 -0600456 */
457void dynevent_arg_pair_init(struct dynevent_arg_pair *arg_pair,
Tom Zanussi86c54262020-01-29 12:59:24 -0600458 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;
Tom Zanussi86c54262020-01-29 12:59:24 -0600469}
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 */
482int dynevent_create(struct dynevent_cmd *cmd)
483{
484 return cmd->run_command(cmd);
485}
486EXPORT_SYMBOL_GPL(dynevent_create);