1
2
3
4
5
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(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 ret = -EINVAL;
68 goto out;
69 }
70
71 mutex_lock(&event_mutex);
72 for_each_dyn_event_safe(pos, n) {
73 if (type && type != pos->ops)
74 continue;
75 if (!pos->ops->match(system, event,
76 argc - 1, (const char **)argv + 1, pos))
77 continue;
78
79 ret = pos->ops->free(pos);
80 if (ret)
81 break;
82 }
83 mutex_unlock(&event_mutex);
84out:
85 argv_free(argv);
86 return ret;
87}
88
89static int create_dyn_event(const char *raw_command)
90{
91 struct dyn_event_operations *ops;
92 int ret = -ENODEV;
93
94 if (raw_command[0] == '-' || raw_command[0] == '!')
95 return dyn_event_release(raw_command, NULL);
96
97 mutex_lock(&dyn_event_ops_mutex);
98 list_for_each_entry(ops, &dyn_event_ops_list, list) {
99 ret = ops->create(raw_command);
100 if (!ret || ret != -ECANCELED)
101 break;
102 }
103 mutex_unlock(&dyn_event_ops_mutex);
104 if (ret == -ECANCELED)
105 ret = -EINVAL;
106
107 return ret;
108}
109
110
111LIST_HEAD(dyn_event_list);
112
113void *dyn_event_seq_start(struct seq_file *m, loff_t *pos)
114{
115 mutex_lock(&event_mutex);
116 return seq_list_start(&dyn_event_list, *pos);
117}
118
119void *dyn_event_seq_next(struct seq_file *m, void *v, loff_t *pos)
120{
121 return seq_list_next(v, &dyn_event_list, pos);
122}
123
124void dyn_event_seq_stop(struct seq_file *m, void *v)
125{
126 mutex_unlock(&event_mutex);
127}
128
129static int dyn_event_seq_show(struct seq_file *m, void *v)
130{
131 struct dyn_event *ev = v;
132
133 if (ev && ev->ops)
134 return ev->ops->show(m, ev);
135
136 return 0;
137}
138
139static const struct seq_operations dyn_event_seq_op = {
140 .start = dyn_event_seq_start,
141 .next = dyn_event_seq_next,
142 .stop = dyn_event_seq_stop,
143 .show = dyn_event_seq_show
144};
145
146
147
148
149
150
151
152
153
154
155
156
157int dyn_events_release_all(struct dyn_event_operations *type)
158{
159 struct dyn_event *ev, *tmp;
160 int ret = 0;
161
162 mutex_lock(&event_mutex);
163 for_each_dyn_event(ev) {
164 if (type && ev->ops != type)
165 continue;
166 if (ev->ops->is_busy(ev)) {
167 ret = -EBUSY;
168 goto out;
169 }
170 }
171 for_each_dyn_event_safe(ev, tmp) {
172 if (type && ev->ops != type)
173 continue;
174 ret = ev->ops->free(ev);
175 if (ret)
176 break;
177 }
178out:
179 mutex_unlock(&event_mutex);
180
181 return ret;
182}
183
184static int dyn_event_open(struct inode *inode, struct file *file)
185{
186 int ret;
187
188 ret = tracing_check_open_get_tr(NULL);
189 if (ret)
190 return ret;
191
192 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
193 ret = dyn_events_release_all(NULL);
194 if (ret < 0)
195 return ret;
196 }
197
198 return seq_open(file, &dyn_event_seq_op);
199}
200
201static ssize_t dyn_event_write(struct file *file, const char __user *buffer,
202 size_t count, loff_t *ppos)
203{
204 return trace_parse_run_command(file, buffer, count, ppos,
205 create_dyn_event);
206}
207
208static const struct file_operations dynamic_events_ops = {
209 .owner = THIS_MODULE,
210 .open = dyn_event_open,
211 .read = seq_read,
212 .llseek = seq_lseek,
213 .release = seq_release,
214 .write = dyn_event_write,
215};
216
217
218static __init int init_dynamic_event(void)
219{
220 struct dentry *entry;
221 int ret;
222
223 ret = tracing_init_dentry();
224 if (ret)
225 return 0;
226
227 entry = tracefs_create_file("dynamic_events", 0644, NULL,
228 NULL, &dynamic_events_ops);
229
230
231 if (!entry)
232 pr_warn("Could not create tracefs 'dynamic_events' entry\n");
233
234 return 0;
235}
236fs_initcall(init_dynamic_event);
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256int dynevent_arg_add(struct dynevent_cmd *cmd,
257 struct dynevent_arg *arg,
258 dynevent_check_arg_fn_t check_arg)
259{
260 int ret = 0;
261
262 if (check_arg) {
263 ret = check_arg(arg);
264 if (ret)
265 return ret;
266 }
267
268 ret = seq_buf_printf(&cmd->seq, " %s%c", arg->str, arg->separator);
269 if (ret) {
270 pr_err("String is too long: %s%c\n", arg->str, arg->separator);
271 return -E2BIG;
272 }
273
274 return ret;
275}
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301int dynevent_arg_pair_add(struct dynevent_cmd *cmd,
302 struct dynevent_arg_pair *arg_pair,
303 dynevent_check_arg_fn_t check_arg)
304{
305 int ret = 0;
306
307 if (check_arg) {
308 ret = check_arg(arg_pair);
309 if (ret)
310 return ret;
311 }
312
313 ret = seq_buf_printf(&cmd->seq, " %s%c%s%c", arg_pair->lhs,
314 arg_pair->operator, arg_pair->rhs,
315 arg_pair->separator);
316 if (ret) {
317 pr_err("field string is too long: %s%c%s%c\n", arg_pair->lhs,
318 arg_pair->operator, arg_pair->rhs,
319 arg_pair->separator);
320 return -E2BIG;
321 }
322
323 return ret;
324}
325
326
327
328
329
330
331
332
333
334
335
336int dynevent_str_add(struct dynevent_cmd *cmd, const char *str)
337{
338 int ret = 0;
339
340 ret = seq_buf_puts(&cmd->seq, str);
341 if (ret) {
342 pr_err("String is too long: %s\n", str);
343 return -E2BIG;
344 }
345
346 return ret;
347}
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369void dynevent_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen,
370 enum dynevent_type type,
371 dynevent_create_fn_t run_command)
372{
373 memset(cmd, '\0', sizeof(*cmd));
374
375 seq_buf_init(&cmd->seq, buf, maxlen);
376 cmd->type = type;
377 cmd->run_command = run_command;
378}
379
380
381
382
383
384
385
386
387
388
389
390
391
392void dynevent_arg_init(struct dynevent_arg *arg,
393 char separator)
394{
395 memset(arg, '\0', sizeof(*arg));
396
397 if (!separator)
398 separator = ' ';
399 arg->separator = separator;
400}
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419void dynevent_arg_pair_init(struct dynevent_arg_pair *arg_pair,
420 char operator, char separator)
421{
422 memset(arg_pair, '\0', sizeof(*arg_pair));
423
424 if (!operator)
425 operator = ' ';
426 arg_pair->operator = operator;
427
428 if (!separator)
429 separator = ' ';
430 arg_pair->separator = separator;
431}
432
433
434
435
436
437
438
439
440
441
442
443
444int dynevent_create(struct dynevent_cmd *cmd)
445{
446 return cmd->run_command(cmd);
447}
448EXPORT_SYMBOL_GPL(dynevent_create);
449