tracing/synthetic: Make lastcmd_mutex static
[platform/kernel/linux-starfive.git] / kernel / trace / trace_events_synth.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * trace_events_synth - synthetic trace events
4  *
5  * Copyright (C) 2015, 2020 Tom Zanussi <tom.zanussi@linux.intel.com>
6  */
7
8 #include <linux/module.h>
9 #include <linux/kallsyms.h>
10 #include <linux/security.h>
11 #include <linux/mutex.h>
12 #include <linux/slab.h>
13 #include <linux/stacktrace.h>
14 #include <linux/rculist.h>
15 #include <linux/tracefs.h>
16
17 /* for gfp flag names */
18 #include <linux/trace_events.h>
19 #include <trace/events/mmflags.h>
20 #include "trace_probe.h"
21 #include "trace_probe_kernel.h"
22
23 #include "trace_synth.h"
24
25 #undef ERRORS
26 #define ERRORS  \
27         C(BAD_NAME,             "Illegal name"),                \
28         C(INVALID_CMD,          "Command must be of the form: <name> field[;field] ..."),\
29         C(INVALID_DYN_CMD,      "Command must be of the form: s or -:[synthetic/]<name> field[;field] ..."),\
30         C(EVENT_EXISTS,         "Event already exists"),        \
31         C(TOO_MANY_FIELDS,      "Too many fields"),             \
32         C(INCOMPLETE_TYPE,      "Incomplete type"),             \
33         C(INVALID_TYPE,         "Invalid type"),                \
34         C(INVALID_FIELD,        "Invalid field"),               \
35         C(INVALID_ARRAY_SPEC,   "Invalid array specification"),
36
37 #undef C
38 #define C(a, b)         SYNTH_ERR_##a
39
40 enum { ERRORS };
41
42 #undef C
43 #define C(a, b)         b
44
45 static const char *err_text[] = { ERRORS };
46
47 static DEFINE_MUTEX(lastcmd_mutex);
48 static char *last_cmd;
49
50 static int errpos(const char *str)
51 {
52         int ret = 0;
53
54         mutex_lock(&lastcmd_mutex);
55         if (!str || !last_cmd)
56                 goto out;
57
58         ret = err_pos(last_cmd, str);
59  out:
60         mutex_unlock(&lastcmd_mutex);
61         return ret;
62 }
63
64 static void last_cmd_set(const char *str)
65 {
66         if (!str)
67                 return;
68
69         mutex_lock(&lastcmd_mutex);
70         kfree(last_cmd);
71         last_cmd = kstrdup(str, GFP_KERNEL);
72         mutex_unlock(&lastcmd_mutex);
73 }
74
75 static void synth_err(u8 err_type, u16 err_pos)
76 {
77         mutex_lock(&lastcmd_mutex);
78         if (!last_cmd)
79                 goto out;
80
81         tracing_log_err(NULL, "synthetic_events", last_cmd, err_text,
82                         err_type, err_pos);
83  out:
84         mutex_unlock(&lastcmd_mutex);
85 }
86
87 static int create_synth_event(const char *raw_command);
88 static int synth_event_show(struct seq_file *m, struct dyn_event *ev);
89 static int synth_event_release(struct dyn_event *ev);
90 static bool synth_event_is_busy(struct dyn_event *ev);
91 static bool synth_event_match(const char *system, const char *event,
92                         int argc, const char **argv, struct dyn_event *ev);
93
94 static struct dyn_event_operations synth_event_ops = {
95         .create = create_synth_event,
96         .show = synth_event_show,
97         .is_busy = synth_event_is_busy,
98         .free = synth_event_release,
99         .match = synth_event_match,
100 };
101
102 static bool is_synth_event(struct dyn_event *ev)
103 {
104         return ev->ops == &synth_event_ops;
105 }
106
107 static struct synth_event *to_synth_event(struct dyn_event *ev)
108 {
109         return container_of(ev, struct synth_event, devent);
110 }
111
112 static bool synth_event_is_busy(struct dyn_event *ev)
113 {
114         struct synth_event *event = to_synth_event(ev);
115
116         return event->ref != 0;
117 }
118
119 static bool synth_event_match(const char *system, const char *event,
120                         int argc, const char **argv, struct dyn_event *ev)
121 {
122         struct synth_event *sev = to_synth_event(ev);
123
124         return strcmp(sev->name, event) == 0 &&
125                 (!system || strcmp(system, SYNTH_SYSTEM) == 0);
126 }
127
128 struct synth_trace_event {
129         struct trace_entry      ent;
130         u64                     fields[];
131 };
132
133 static int synth_event_define_fields(struct trace_event_call *call)
134 {
135         struct synth_trace_event trace;
136         int offset = offsetof(typeof(trace), fields);
137         struct synth_event *event = call->data;
138         unsigned int i, size, n_u64;
139         char *name, *type;
140         bool is_signed;
141         int ret = 0;
142
143         for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
144                 size = event->fields[i]->size;
145                 is_signed = event->fields[i]->is_signed;
146                 type = event->fields[i]->type;
147                 name = event->fields[i]->name;
148                 ret = trace_define_field(call, type, name, offset, size,
149                                          is_signed, FILTER_OTHER);
150                 if (ret)
151                         break;
152
153                 event->fields[i]->offset = n_u64;
154
155                 if (event->fields[i]->is_string && !event->fields[i]->is_dynamic) {
156                         offset += STR_VAR_LEN_MAX;
157                         n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
158                 } else {
159                         offset += sizeof(u64);
160                         n_u64++;
161                 }
162         }
163
164         event->n_u64 = n_u64;
165
166         return ret;
167 }
168
169 static bool synth_field_signed(char *type)
170 {
171         if (str_has_prefix(type, "u"))
172                 return false;
173         if (strcmp(type, "gfp_t") == 0)
174                 return false;
175
176         return true;
177 }
178
179 static int synth_field_is_string(char *type)
180 {
181         if (strstr(type, "char[") != NULL)
182                 return true;
183
184         return false;
185 }
186
187 static int synth_field_string_size(char *type)
188 {
189         char buf[4], *end, *start;
190         unsigned int len;
191         int size, err;
192
193         start = strstr(type, "char[");
194         if (start == NULL)
195                 return -EINVAL;
196         start += sizeof("char[") - 1;
197
198         end = strchr(type, ']');
199         if (!end || end < start || type + strlen(type) > end + 1)
200                 return -EINVAL;
201
202         len = end - start;
203         if (len > 3)
204                 return -EINVAL;
205
206         if (len == 0)
207                 return 0; /* variable-length string */
208
209         strncpy(buf, start, len);
210         buf[len] = '\0';
211
212         err = kstrtouint(buf, 0, &size);
213         if (err)
214                 return err;
215
216         if (size > STR_VAR_LEN_MAX)
217                 return -EINVAL;
218
219         return size;
220 }
221
222 static int synth_field_size(char *type)
223 {
224         int size = 0;
225
226         if (strcmp(type, "s64") == 0)
227                 size = sizeof(s64);
228         else if (strcmp(type, "u64") == 0)
229                 size = sizeof(u64);
230         else if (strcmp(type, "s32") == 0)
231                 size = sizeof(s32);
232         else if (strcmp(type, "u32") == 0)
233                 size = sizeof(u32);
234         else if (strcmp(type, "s16") == 0)
235                 size = sizeof(s16);
236         else if (strcmp(type, "u16") == 0)
237                 size = sizeof(u16);
238         else if (strcmp(type, "s8") == 0)
239                 size = sizeof(s8);
240         else if (strcmp(type, "u8") == 0)
241                 size = sizeof(u8);
242         else if (strcmp(type, "char") == 0)
243                 size = sizeof(char);
244         else if (strcmp(type, "unsigned char") == 0)
245                 size = sizeof(unsigned char);
246         else if (strcmp(type, "int") == 0)
247                 size = sizeof(int);
248         else if (strcmp(type, "unsigned int") == 0)
249                 size = sizeof(unsigned int);
250         else if (strcmp(type, "long") == 0)
251                 size = sizeof(long);
252         else if (strcmp(type, "unsigned long") == 0)
253                 size = sizeof(unsigned long);
254         else if (strcmp(type, "bool") == 0)
255                 size = sizeof(bool);
256         else if (strcmp(type, "pid_t") == 0)
257                 size = sizeof(pid_t);
258         else if (strcmp(type, "gfp_t") == 0)
259                 size = sizeof(gfp_t);
260         else if (synth_field_is_string(type))
261                 size = synth_field_string_size(type);
262
263         return size;
264 }
265
266 static const char *synth_field_fmt(char *type)
267 {
268         const char *fmt = "%llu";
269
270         if (strcmp(type, "s64") == 0)
271                 fmt = "%lld";
272         else if (strcmp(type, "u64") == 0)
273                 fmt = "%llu";
274         else if (strcmp(type, "s32") == 0)
275                 fmt = "%d";
276         else if (strcmp(type, "u32") == 0)
277                 fmt = "%u";
278         else if (strcmp(type, "s16") == 0)
279                 fmt = "%d";
280         else if (strcmp(type, "u16") == 0)
281                 fmt = "%u";
282         else if (strcmp(type, "s8") == 0)
283                 fmt = "%d";
284         else if (strcmp(type, "u8") == 0)
285                 fmt = "%u";
286         else if (strcmp(type, "char") == 0)
287                 fmt = "%d";
288         else if (strcmp(type, "unsigned char") == 0)
289                 fmt = "%u";
290         else if (strcmp(type, "int") == 0)
291                 fmt = "%d";
292         else if (strcmp(type, "unsigned int") == 0)
293                 fmt = "%u";
294         else if (strcmp(type, "long") == 0)
295                 fmt = "%ld";
296         else if (strcmp(type, "unsigned long") == 0)
297                 fmt = "%lu";
298         else if (strcmp(type, "bool") == 0)
299                 fmt = "%d";
300         else if (strcmp(type, "pid_t") == 0)
301                 fmt = "%d";
302         else if (strcmp(type, "gfp_t") == 0)
303                 fmt = "%x";
304         else if (synth_field_is_string(type))
305                 fmt = "%.*s";
306
307         return fmt;
308 }
309
310 static void print_synth_event_num_val(struct trace_seq *s,
311                                       char *print_fmt, char *name,
312                                       int size, u64 val, char *space)
313 {
314         switch (size) {
315         case 1:
316                 trace_seq_printf(s, print_fmt, name, (u8)val, space);
317                 break;
318
319         case 2:
320                 trace_seq_printf(s, print_fmt, name, (u16)val, space);
321                 break;
322
323         case 4:
324                 trace_seq_printf(s, print_fmt, name, (u32)val, space);
325                 break;
326
327         default:
328                 trace_seq_printf(s, print_fmt, name, val, space);
329                 break;
330         }
331 }
332
333 static enum print_line_t print_synth_event(struct trace_iterator *iter,
334                                            int flags,
335                                            struct trace_event *event)
336 {
337         struct trace_array *tr = iter->tr;
338         struct trace_seq *s = &iter->seq;
339         struct synth_trace_event *entry;
340         struct synth_event *se;
341         unsigned int i, n_u64;
342         char print_fmt[32];
343         const char *fmt;
344
345         entry = (struct synth_trace_event *)iter->ent;
346         se = container_of(event, struct synth_event, call.event);
347
348         trace_seq_printf(s, "%s: ", se->name);
349
350         for (i = 0, n_u64 = 0; i < se->n_fields; i++) {
351                 if (trace_seq_has_overflowed(s))
352                         goto end;
353
354                 fmt = synth_field_fmt(se->fields[i]->type);
355
356                 /* parameter types */
357                 if (tr && tr->trace_flags & TRACE_ITER_VERBOSE)
358                         trace_seq_printf(s, "%s ", fmt);
359
360                 snprintf(print_fmt, sizeof(print_fmt), "%%s=%s%%s", fmt);
361
362                 /* parameter values */
363                 if (se->fields[i]->is_string) {
364                         if (se->fields[i]->is_dynamic) {
365                                 u32 offset, data_offset;
366                                 char *str_field;
367
368                                 offset = (u32)entry->fields[n_u64];
369                                 data_offset = offset & 0xffff;
370
371                                 str_field = (char *)entry + data_offset;
372
373                                 trace_seq_printf(s, print_fmt, se->fields[i]->name,
374                                                  STR_VAR_LEN_MAX,
375                                                  str_field,
376                                                  i == se->n_fields - 1 ? "" : " ");
377                                 n_u64++;
378                         } else {
379                                 trace_seq_printf(s, print_fmt, se->fields[i]->name,
380                                                  STR_VAR_LEN_MAX,
381                                                  (char *)&entry->fields[n_u64],
382                                                  i == se->n_fields - 1 ? "" : " ");
383                                 n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
384                         }
385                 } else {
386                         struct trace_print_flags __flags[] = {
387                             __def_gfpflag_names, {-1, NULL} };
388                         char *space = (i == se->n_fields - 1 ? "" : " ");
389
390                         print_synth_event_num_val(s, print_fmt,
391                                                   se->fields[i]->name,
392                                                   se->fields[i]->size,
393                                                   entry->fields[n_u64],
394                                                   space);
395
396                         if (strcmp(se->fields[i]->type, "gfp_t") == 0) {
397                                 trace_seq_puts(s, " (");
398                                 trace_print_flags_seq(s, "|",
399                                                       entry->fields[n_u64],
400                                                       __flags);
401                                 trace_seq_putc(s, ')');
402                         }
403                         n_u64++;
404                 }
405         }
406 end:
407         trace_seq_putc(s, '\n');
408
409         return trace_handle_return(s);
410 }
411
412 static struct trace_event_functions synth_event_funcs = {
413         .trace          = print_synth_event
414 };
415
416 static unsigned int trace_string(struct synth_trace_event *entry,
417                                  struct synth_event *event,
418                                  char *str_val,
419                                  bool is_dynamic,
420                                  unsigned int data_size,
421                                  unsigned int *n_u64)
422 {
423         unsigned int len = 0;
424         char *str_field;
425         int ret;
426
427         if (is_dynamic) {
428                 u32 data_offset;
429
430                 data_offset = offsetof(typeof(*entry), fields);
431                 data_offset += event->n_u64 * sizeof(u64);
432                 data_offset += data_size;
433
434                 len = kern_fetch_store_strlen((unsigned long)str_val);
435
436                 data_offset |= len << 16;
437                 *(u32 *)&entry->fields[*n_u64] = data_offset;
438
439                 ret = kern_fetch_store_string((unsigned long)str_val, &entry->fields[*n_u64], entry);
440
441                 (*n_u64)++;
442         } else {
443                 str_field = (char *)&entry->fields[*n_u64];
444
445 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
446                 if ((unsigned long)str_val < TASK_SIZE)
447                         ret = strncpy_from_user_nofault(str_field, str_val, STR_VAR_LEN_MAX);
448                 else
449 #endif
450                         ret = strncpy_from_kernel_nofault(str_field, str_val, STR_VAR_LEN_MAX);
451
452                 if (ret < 0)
453                         strcpy(str_field, FAULT_STRING);
454
455                 (*n_u64) += STR_VAR_LEN_MAX / sizeof(u64);
456         }
457
458         return len;
459 }
460
461 static notrace void trace_event_raw_event_synth(void *__data,
462                                                 u64 *var_ref_vals,
463                                                 unsigned int *var_ref_idx)
464 {
465         unsigned int i, n_u64, val_idx, len, data_size = 0;
466         struct trace_event_file *trace_file = __data;
467         struct synth_trace_event *entry;
468         struct trace_event_buffer fbuffer;
469         struct trace_buffer *buffer;
470         struct synth_event *event;
471         int fields_size = 0;
472
473         event = trace_file->event_call->data;
474
475         if (trace_trigger_soft_disabled(trace_file))
476                 return;
477
478         fields_size = event->n_u64 * sizeof(u64);
479
480         for (i = 0; i < event->n_dynamic_fields; i++) {
481                 unsigned int field_pos = event->dynamic_fields[i]->field_pos;
482                 char *str_val;
483
484                 val_idx = var_ref_idx[field_pos];
485                 str_val = (char *)(long)var_ref_vals[val_idx];
486
487                 len = kern_fetch_store_strlen((unsigned long)str_val);
488
489                 fields_size += len;
490         }
491
492         /*
493          * Avoid ring buffer recursion detection, as this event
494          * is being performed within another event.
495          */
496         buffer = trace_file->tr->array_buffer.buffer;
497         ring_buffer_nest_start(buffer);
498
499         entry = trace_event_buffer_reserve(&fbuffer, trace_file,
500                                            sizeof(*entry) + fields_size);
501         if (!entry)
502                 goto out;
503
504         for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
505                 val_idx = var_ref_idx[i];
506                 if (event->fields[i]->is_string) {
507                         char *str_val = (char *)(long)var_ref_vals[val_idx];
508
509                         len = trace_string(entry, event, str_val,
510                                            event->fields[i]->is_dynamic,
511                                            data_size, &n_u64);
512                         data_size += len; /* only dynamic string increments */
513                 } else {
514                         struct synth_field *field = event->fields[i];
515                         u64 val = var_ref_vals[val_idx];
516
517                         switch (field->size) {
518                         case 1:
519                                 *(u8 *)&entry->fields[n_u64] = (u8)val;
520                                 break;
521
522                         case 2:
523                                 *(u16 *)&entry->fields[n_u64] = (u16)val;
524                                 break;
525
526                         case 4:
527                                 *(u32 *)&entry->fields[n_u64] = (u32)val;
528                                 break;
529
530                         default:
531                                 entry->fields[n_u64] = val;
532                                 break;
533                         }
534                         n_u64++;
535                 }
536         }
537
538         trace_event_buffer_commit(&fbuffer);
539 out:
540         ring_buffer_nest_end(buffer);
541 }
542
543 static void free_synth_event_print_fmt(struct trace_event_call *call)
544 {
545         if (call) {
546                 kfree(call->print_fmt);
547                 call->print_fmt = NULL;
548         }
549 }
550
551 static int __set_synth_event_print_fmt(struct synth_event *event,
552                                        char *buf, int len)
553 {
554         const char *fmt;
555         int pos = 0;
556         int i;
557
558         /* When len=0, we just calculate the needed length */
559 #define LEN_OR_ZERO (len ? len - pos : 0)
560
561         pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
562         for (i = 0; i < event->n_fields; i++) {
563                 fmt = synth_field_fmt(event->fields[i]->type);
564                 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s=%s%s",
565                                 event->fields[i]->name, fmt,
566                                 i == event->n_fields - 1 ? "" : ", ");
567         }
568         pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
569
570         for (i = 0; i < event->n_fields; i++) {
571                 if (event->fields[i]->is_string &&
572                     event->fields[i]->is_dynamic)
573                         pos += snprintf(buf + pos, LEN_OR_ZERO,
574                                 ", __get_str(%s)", event->fields[i]->name);
575                 else
576                         pos += snprintf(buf + pos, LEN_OR_ZERO,
577                                         ", REC->%s", event->fields[i]->name);
578         }
579
580 #undef LEN_OR_ZERO
581
582         /* return the length of print_fmt */
583         return pos;
584 }
585
586 static int set_synth_event_print_fmt(struct trace_event_call *call)
587 {
588         struct synth_event *event = call->data;
589         char *print_fmt;
590         int len;
591
592         /* First: called with 0 length to calculate the needed length */
593         len = __set_synth_event_print_fmt(event, NULL, 0);
594
595         print_fmt = kmalloc(len + 1, GFP_KERNEL);
596         if (!print_fmt)
597                 return -ENOMEM;
598
599         /* Second: actually write the @print_fmt */
600         __set_synth_event_print_fmt(event, print_fmt, len + 1);
601         call->print_fmt = print_fmt;
602
603         return 0;
604 }
605
606 static void free_synth_field(struct synth_field *field)
607 {
608         kfree(field->type);
609         kfree(field->name);
610         kfree(field);
611 }
612
613 static int check_field_version(const char *prefix, const char *field_type,
614                                const char *field_name)
615 {
616         /*
617          * For backward compatibility, the old synthetic event command
618          * format did not require semicolons, and in order to not
619          * break user space, that old format must still work. If a new
620          * feature is added, then the format that uses the new feature
621          * will be required to have semicolons, as nothing that uses
622          * the old format would be using the new, yet to be created,
623          * feature. When a new feature is added, this will detect it,
624          * and return a number greater than 1, and require the format
625          * to use semicolons.
626          */
627         return 1;
628 }
629
630 static struct synth_field *parse_synth_field(int argc, char **argv,
631                                              int *consumed, int *field_version)
632 {
633         const char *prefix = NULL, *field_type = argv[0], *field_name, *array;
634         struct synth_field *field;
635         int len, ret = -ENOMEM;
636         struct seq_buf s;
637         ssize_t size;
638
639         if (!strcmp(field_type, "unsigned")) {
640                 if (argc < 3) {
641                         synth_err(SYNTH_ERR_INCOMPLETE_TYPE, errpos(field_type));
642                         return ERR_PTR(-EINVAL);
643                 }
644                 prefix = "unsigned ";
645                 field_type = argv[1];
646                 field_name = argv[2];
647                 *consumed += 3;
648         } else {
649                 field_name = argv[1];
650                 *consumed += 2;
651         }
652
653         if (!field_name) {
654                 synth_err(SYNTH_ERR_INVALID_FIELD, errpos(field_type));
655                 return ERR_PTR(-EINVAL);
656         }
657
658         *field_version = check_field_version(prefix, field_type, field_name);
659
660         field = kzalloc(sizeof(*field), GFP_KERNEL);
661         if (!field)
662                 return ERR_PTR(-ENOMEM);
663
664         len = strlen(field_name);
665         array = strchr(field_name, '[');
666         if (array)
667                 len -= strlen(array);
668
669         field->name = kmemdup_nul(field_name, len, GFP_KERNEL);
670         if (!field->name)
671                 goto free;
672
673         if (!is_good_name(field->name)) {
674                 synth_err(SYNTH_ERR_BAD_NAME, errpos(field_name));
675                 ret = -EINVAL;
676                 goto free;
677         }
678
679         len = strlen(field_type) + 1;
680
681         if (array)
682                 len += strlen(array);
683
684         if (prefix)
685                 len += strlen(prefix);
686
687         field->type = kzalloc(len, GFP_KERNEL);
688         if (!field->type)
689                 goto free;
690
691         seq_buf_init(&s, field->type, len);
692         if (prefix)
693                 seq_buf_puts(&s, prefix);
694         seq_buf_puts(&s, field_type);
695         if (array)
696                 seq_buf_puts(&s, array);
697         if (WARN_ON_ONCE(!seq_buf_buffer_left(&s)))
698                 goto free;
699
700         s.buffer[s.len] = '\0';
701
702         size = synth_field_size(field->type);
703         if (size < 0) {
704                 if (array)
705                         synth_err(SYNTH_ERR_INVALID_ARRAY_SPEC, errpos(field_name));
706                 else
707                         synth_err(SYNTH_ERR_INVALID_TYPE, errpos(field_type));
708                 ret = -EINVAL;
709                 goto free;
710         } else if (size == 0) {
711                 if (synth_field_is_string(field->type)) {
712                         char *type;
713
714                         len = sizeof("__data_loc ") + strlen(field->type) + 1;
715                         type = kzalloc(len, GFP_KERNEL);
716                         if (!type)
717                                 goto free;
718
719                         seq_buf_init(&s, type, len);
720                         seq_buf_puts(&s, "__data_loc ");
721                         seq_buf_puts(&s, field->type);
722
723                         if (WARN_ON_ONCE(!seq_buf_buffer_left(&s)))
724                                 goto free;
725                         s.buffer[s.len] = '\0';
726
727                         kfree(field->type);
728                         field->type = type;
729
730                         field->is_dynamic = true;
731                         size = sizeof(u64);
732                 } else {
733                         synth_err(SYNTH_ERR_INVALID_TYPE, errpos(field_type));
734                         ret = -EINVAL;
735                         goto free;
736                 }
737         }
738         field->size = size;
739
740         if (synth_field_is_string(field->type))
741                 field->is_string = true;
742
743         field->is_signed = synth_field_signed(field->type);
744  out:
745         return field;
746  free:
747         free_synth_field(field);
748         field = ERR_PTR(ret);
749         goto out;
750 }
751
752 static void free_synth_tracepoint(struct tracepoint *tp)
753 {
754         if (!tp)
755                 return;
756
757         kfree(tp->name);
758         kfree(tp);
759 }
760
761 static struct tracepoint *alloc_synth_tracepoint(char *name)
762 {
763         struct tracepoint *tp;
764
765         tp = kzalloc(sizeof(*tp), GFP_KERNEL);
766         if (!tp)
767                 return ERR_PTR(-ENOMEM);
768
769         tp->name = kstrdup(name, GFP_KERNEL);
770         if (!tp->name) {
771                 kfree(tp);
772                 return ERR_PTR(-ENOMEM);
773         }
774
775         return tp;
776 }
777
778 struct synth_event *find_synth_event(const char *name)
779 {
780         struct dyn_event *pos;
781         struct synth_event *event;
782
783         for_each_dyn_event(pos) {
784                 if (!is_synth_event(pos))
785                         continue;
786                 event = to_synth_event(pos);
787                 if (strcmp(event->name, name) == 0)
788                         return event;
789         }
790
791         return NULL;
792 }
793
794 static struct trace_event_fields synth_event_fields_array[] = {
795         { .type = TRACE_FUNCTION_TYPE,
796           .define_fields = synth_event_define_fields },
797         {}
798 };
799
800 static int register_synth_event(struct synth_event *event)
801 {
802         struct trace_event_call *call = &event->call;
803         int ret = 0;
804
805         event->call.class = &event->class;
806         event->class.system = kstrdup(SYNTH_SYSTEM, GFP_KERNEL);
807         if (!event->class.system) {
808                 ret = -ENOMEM;
809                 goto out;
810         }
811
812         event->tp = alloc_synth_tracepoint(event->name);
813         if (IS_ERR(event->tp)) {
814                 ret = PTR_ERR(event->tp);
815                 event->tp = NULL;
816                 goto out;
817         }
818
819         INIT_LIST_HEAD(&call->class->fields);
820         call->event.funcs = &synth_event_funcs;
821         call->class->fields_array = synth_event_fields_array;
822
823         ret = register_trace_event(&call->event);
824         if (!ret) {
825                 ret = -ENODEV;
826                 goto out;
827         }
828         call->flags = TRACE_EVENT_FL_TRACEPOINT;
829         call->class->reg = trace_event_reg;
830         call->class->probe = trace_event_raw_event_synth;
831         call->data = event;
832         call->tp = event->tp;
833
834         ret = trace_add_event_call(call);
835         if (ret) {
836                 pr_warn("Failed to register synthetic event: %s\n",
837                         trace_event_name(call));
838                 goto err;
839         }
840
841         ret = set_synth_event_print_fmt(call);
842         /* unregister_trace_event() will be called inside */
843         if (ret < 0)
844                 trace_remove_event_call(call);
845  out:
846         return ret;
847  err:
848         unregister_trace_event(&call->event);
849         goto out;
850 }
851
852 static int unregister_synth_event(struct synth_event *event)
853 {
854         struct trace_event_call *call = &event->call;
855         int ret;
856
857         ret = trace_remove_event_call(call);
858
859         return ret;
860 }
861
862 static void free_synth_event(struct synth_event *event)
863 {
864         unsigned int i;
865
866         if (!event)
867                 return;
868
869         for (i = 0; i < event->n_fields; i++)
870                 free_synth_field(event->fields[i]);
871
872         kfree(event->fields);
873         kfree(event->dynamic_fields);
874         kfree(event->name);
875         kfree(event->class.system);
876         free_synth_tracepoint(event->tp);
877         free_synth_event_print_fmt(&event->call);
878         kfree(event);
879 }
880
881 static struct synth_event *alloc_synth_event(const char *name, int n_fields,
882                                              struct synth_field **fields)
883 {
884         unsigned int i, j, n_dynamic_fields = 0;
885         struct synth_event *event;
886
887         event = kzalloc(sizeof(*event), GFP_KERNEL);
888         if (!event) {
889                 event = ERR_PTR(-ENOMEM);
890                 goto out;
891         }
892
893         event->name = kstrdup(name, GFP_KERNEL);
894         if (!event->name) {
895                 kfree(event);
896                 event = ERR_PTR(-ENOMEM);
897                 goto out;
898         }
899
900         event->fields = kcalloc(n_fields, sizeof(*event->fields), GFP_KERNEL);
901         if (!event->fields) {
902                 free_synth_event(event);
903                 event = ERR_PTR(-ENOMEM);
904                 goto out;
905         }
906
907         for (i = 0; i < n_fields; i++)
908                 if (fields[i]->is_dynamic)
909                         n_dynamic_fields++;
910
911         if (n_dynamic_fields) {
912                 event->dynamic_fields = kcalloc(n_dynamic_fields,
913                                                 sizeof(*event->dynamic_fields),
914                                                 GFP_KERNEL);
915                 if (!event->dynamic_fields) {
916                         free_synth_event(event);
917                         event = ERR_PTR(-ENOMEM);
918                         goto out;
919                 }
920         }
921
922         dyn_event_init(&event->devent, &synth_event_ops);
923
924         for (i = 0, j = 0; i < n_fields; i++) {
925                 fields[i]->field_pos = i;
926                 event->fields[i] = fields[i];
927
928                 if (fields[i]->is_dynamic)
929                         event->dynamic_fields[j++] = fields[i];
930         }
931         event->n_dynamic_fields = j;
932         event->n_fields = n_fields;
933  out:
934         return event;
935 }
936
937 static int synth_event_check_arg_fn(void *data)
938 {
939         struct dynevent_arg_pair *arg_pair = data;
940         int size;
941
942         size = synth_field_size((char *)arg_pair->lhs);
943         if (size == 0) {
944                 if (strstr((char *)arg_pair->lhs, "["))
945                         return 0;
946         }
947
948         return size ? 0 : -EINVAL;
949 }
950
951 /**
952  * synth_event_add_field - Add a new field to a synthetic event cmd
953  * @cmd: A pointer to the dynevent_cmd struct representing the new event
954  * @type: The type of the new field to add
955  * @name: The name of the new field to add
956  *
957  * Add a new field to a synthetic event cmd object.  Field ordering is in
958  * the same order the fields are added.
959  *
960  * See synth_field_size() for available types. If field_name contains
961  * [n] the field is considered to be an array.
962  *
963  * Return: 0 if successful, error otherwise.
964  */
965 int synth_event_add_field(struct dynevent_cmd *cmd, const char *type,
966                           const char *name)
967 {
968         struct dynevent_arg_pair arg_pair;
969         int ret;
970
971         if (cmd->type != DYNEVENT_TYPE_SYNTH)
972                 return -EINVAL;
973
974         if (!type || !name)
975                 return -EINVAL;
976
977         dynevent_arg_pair_init(&arg_pair, 0, ';');
978
979         arg_pair.lhs = type;
980         arg_pair.rhs = name;
981
982         ret = dynevent_arg_pair_add(cmd, &arg_pair, synth_event_check_arg_fn);
983         if (ret)
984                 return ret;
985
986         if (++cmd->n_fields > SYNTH_FIELDS_MAX)
987                 ret = -EINVAL;
988
989         return ret;
990 }
991 EXPORT_SYMBOL_GPL(synth_event_add_field);
992
993 /**
994  * synth_event_add_field_str - Add a new field to a synthetic event cmd
995  * @cmd: A pointer to the dynevent_cmd struct representing the new event
996  * @type_name: The type and name of the new field to add, as a single string
997  *
998  * Add a new field to a synthetic event cmd object, as a single
999  * string.  The @type_name string is expected to be of the form 'type
1000  * name', which will be appended by ';'.  No sanity checking is done -
1001  * what's passed in is assumed to already be well-formed.  Field
1002  * ordering is in the same order the fields are added.
1003  *
1004  * See synth_field_size() for available types. If field_name contains
1005  * [n] the field is considered to be an array.
1006  *
1007  * Return: 0 if successful, error otherwise.
1008  */
1009 int synth_event_add_field_str(struct dynevent_cmd *cmd, const char *type_name)
1010 {
1011         struct dynevent_arg arg;
1012         int ret;
1013
1014         if (cmd->type != DYNEVENT_TYPE_SYNTH)
1015                 return -EINVAL;
1016
1017         if (!type_name)
1018                 return -EINVAL;
1019
1020         dynevent_arg_init(&arg, ';');
1021
1022         arg.str = type_name;
1023
1024         ret = dynevent_arg_add(cmd, &arg, NULL);
1025         if (ret)
1026                 return ret;
1027
1028         if (++cmd->n_fields > SYNTH_FIELDS_MAX)
1029                 ret = -EINVAL;
1030
1031         return ret;
1032 }
1033 EXPORT_SYMBOL_GPL(synth_event_add_field_str);
1034
1035 /**
1036  * synth_event_add_fields - Add multiple fields to a synthetic event cmd
1037  * @cmd: A pointer to the dynevent_cmd struct representing the new event
1038  * @fields: An array of type/name field descriptions
1039  * @n_fields: The number of field descriptions contained in the fields array
1040  *
1041  * Add a new set of fields to a synthetic event cmd object.  The event
1042  * fields that will be defined for the event should be passed in as an
1043  * array of struct synth_field_desc, and the number of elements in the
1044  * array passed in as n_fields.  Field ordering will retain the
1045  * ordering given in the fields array.
1046  *
1047  * See synth_field_size() for available types. If field_name contains
1048  * [n] the field is considered to be an array.
1049  *
1050  * Return: 0 if successful, error otherwise.
1051  */
1052 int synth_event_add_fields(struct dynevent_cmd *cmd,
1053                            struct synth_field_desc *fields,
1054                            unsigned int n_fields)
1055 {
1056         unsigned int i;
1057         int ret = 0;
1058
1059         for (i = 0; i < n_fields; i++) {
1060                 if (fields[i].type == NULL || fields[i].name == NULL) {
1061                         ret = -EINVAL;
1062                         break;
1063                 }
1064
1065                 ret = synth_event_add_field(cmd, fields[i].type, fields[i].name);
1066                 if (ret)
1067                         break;
1068         }
1069
1070         return ret;
1071 }
1072 EXPORT_SYMBOL_GPL(synth_event_add_fields);
1073
1074 /**
1075  * __synth_event_gen_cmd_start - Start a synthetic event command from arg list
1076  * @cmd: A pointer to the dynevent_cmd struct representing the new event
1077  * @name: The name of the synthetic event
1078  * @mod: The module creating the event, NULL if not created from a module
1079  * @args: Variable number of arg (pairs), one pair for each field
1080  *
1081  * NOTE: Users normally won't want to call this function directly, but
1082  * rather use the synth_event_gen_cmd_start() wrapper, which
1083  * automatically adds a NULL to the end of the arg list.  If this
1084  * function is used directly, make sure the last arg in the variable
1085  * arg list is NULL.
1086  *
1087  * Generate a synthetic event command to be executed by
1088  * synth_event_gen_cmd_end().  This function can be used to generate
1089  * the complete command or only the first part of it; in the latter
1090  * case, synth_event_add_field(), synth_event_add_field_str(), or
1091  * synth_event_add_fields() can be used to add more fields following
1092  * this.
1093  *
1094  * There should be an even number variable args, each pair consisting
1095  * of a type followed by a field name.
1096  *
1097  * See synth_field_size() for available types. If field_name contains
1098  * [n] the field is considered to be an array.
1099  *
1100  * Return: 0 if successful, error otherwise.
1101  */
1102 int __synth_event_gen_cmd_start(struct dynevent_cmd *cmd, const char *name,
1103                                 struct module *mod, ...)
1104 {
1105         struct dynevent_arg arg;
1106         va_list args;
1107         int ret;
1108
1109         cmd->event_name = name;
1110         cmd->private_data = mod;
1111
1112         if (cmd->type != DYNEVENT_TYPE_SYNTH)
1113                 return -EINVAL;
1114
1115         dynevent_arg_init(&arg, 0);
1116         arg.str = name;
1117         ret = dynevent_arg_add(cmd, &arg, NULL);
1118         if (ret)
1119                 return ret;
1120
1121         va_start(args, mod);
1122         for (;;) {
1123                 const char *type, *name;
1124
1125                 type = va_arg(args, const char *);
1126                 if (!type)
1127                         break;
1128                 name = va_arg(args, const char *);
1129                 if (!name)
1130                         break;
1131
1132                 if (++cmd->n_fields > SYNTH_FIELDS_MAX) {
1133                         ret = -EINVAL;
1134                         break;
1135                 }
1136
1137                 ret = synth_event_add_field(cmd, type, name);
1138                 if (ret)
1139                         break;
1140         }
1141         va_end(args);
1142
1143         return ret;
1144 }
1145 EXPORT_SYMBOL_GPL(__synth_event_gen_cmd_start);
1146
1147 /**
1148  * synth_event_gen_cmd_array_start - Start synthetic event command from an array
1149  * @cmd: A pointer to the dynevent_cmd struct representing the new event
1150  * @name: The name of the synthetic event
1151  * @fields: An array of type/name field descriptions
1152  * @n_fields: The number of field descriptions contained in the fields array
1153  *
1154  * Generate a synthetic event command to be executed by
1155  * synth_event_gen_cmd_end().  This function can be used to generate
1156  * the complete command or only the first part of it; in the latter
1157  * case, synth_event_add_field(), synth_event_add_field_str(), or
1158  * synth_event_add_fields() can be used to add more fields following
1159  * this.
1160  *
1161  * The event fields that will be defined for the event should be
1162  * passed in as an array of struct synth_field_desc, and the number of
1163  * elements in the array passed in as n_fields.  Field ordering will
1164  * retain the ordering given in the fields array.
1165  *
1166  * See synth_field_size() for available types. If field_name contains
1167  * [n] the field is considered to be an array.
1168  *
1169  * Return: 0 if successful, error otherwise.
1170  */
1171 int synth_event_gen_cmd_array_start(struct dynevent_cmd *cmd, const char *name,
1172                                     struct module *mod,
1173                                     struct synth_field_desc *fields,
1174                                     unsigned int n_fields)
1175 {
1176         struct dynevent_arg arg;
1177         unsigned int i;
1178         int ret = 0;
1179
1180         cmd->event_name = name;
1181         cmd->private_data = mod;
1182
1183         if (cmd->type != DYNEVENT_TYPE_SYNTH)
1184                 return -EINVAL;
1185
1186         if (n_fields > SYNTH_FIELDS_MAX)
1187                 return -EINVAL;
1188
1189         dynevent_arg_init(&arg, 0);
1190         arg.str = name;
1191         ret = dynevent_arg_add(cmd, &arg, NULL);
1192         if (ret)
1193                 return ret;
1194
1195         for (i = 0; i < n_fields; i++) {
1196                 if (fields[i].type == NULL || fields[i].name == NULL)
1197                         return -EINVAL;
1198
1199                 ret = synth_event_add_field(cmd, fields[i].type, fields[i].name);
1200                 if (ret)
1201                         break;
1202         }
1203
1204         return ret;
1205 }
1206 EXPORT_SYMBOL_GPL(synth_event_gen_cmd_array_start);
1207
1208 static int __create_synth_event(const char *name, const char *raw_fields)
1209 {
1210         char **argv, *field_str, *tmp_fields, *saved_fields = NULL;
1211         struct synth_field *field, *fields[SYNTH_FIELDS_MAX];
1212         int consumed, cmd_version = 1, n_fields_this_loop;
1213         int i, argc, n_fields = 0, ret = 0;
1214         struct synth_event *event = NULL;
1215
1216         /*
1217          * Argument syntax:
1218          *  - Add synthetic event: <event_name> field[;field] ...
1219          *  - Remove synthetic event: !<event_name> field[;field] ...
1220          *      where 'field' = type field_name
1221          */
1222
1223         if (name[0] == '\0') {
1224                 synth_err(SYNTH_ERR_INVALID_CMD, 0);
1225                 return -EINVAL;
1226         }
1227
1228         if (!is_good_name(name)) {
1229                 synth_err(SYNTH_ERR_BAD_NAME, errpos(name));
1230                 return -EINVAL;
1231         }
1232
1233         mutex_lock(&event_mutex);
1234
1235         event = find_synth_event(name);
1236         if (event) {
1237                 synth_err(SYNTH_ERR_EVENT_EXISTS, errpos(name));
1238                 ret = -EEXIST;
1239                 goto err;
1240         }
1241
1242         tmp_fields = saved_fields = kstrdup(raw_fields, GFP_KERNEL);
1243         if (!tmp_fields) {
1244                 ret = -ENOMEM;
1245                 goto err;
1246         }
1247
1248         while ((field_str = strsep(&tmp_fields, ";")) != NULL) {
1249                 argv = argv_split(GFP_KERNEL, field_str, &argc);
1250                 if (!argv) {
1251                         ret = -ENOMEM;
1252                         goto err;
1253                 }
1254
1255                 if (!argc) {
1256                         argv_free(argv);
1257                         continue;
1258                 }
1259
1260                 n_fields_this_loop = 0;
1261                 consumed = 0;
1262                 while (argc > consumed) {
1263                         int field_version;
1264
1265                         field = parse_synth_field(argc - consumed,
1266                                                   argv + consumed, &consumed,
1267                                                   &field_version);
1268                         if (IS_ERR(field)) {
1269                                 ret = PTR_ERR(field);
1270                                 goto err_free_arg;
1271                         }
1272
1273                         /*
1274                          * Track the highest version of any field we
1275                          * found in the command.
1276                          */
1277                         if (field_version > cmd_version)
1278                                 cmd_version = field_version;
1279
1280                         /*
1281                          * Now sort out what is and isn't valid for
1282                          * each supported version.
1283                          *
1284                          * If we see more than 1 field per loop, it
1285                          * means we have multiple fields between
1286                          * semicolons, and that's something we no
1287                          * longer support in a version 2 or greater
1288                          * command.
1289                          */
1290                         if (cmd_version > 1 && n_fields_this_loop >= 1) {
1291                                 synth_err(SYNTH_ERR_INVALID_CMD, errpos(field_str));
1292                                 ret = -EINVAL;
1293                                 goto err_free_arg;
1294                         }
1295
1296                         if (n_fields == SYNTH_FIELDS_MAX) {
1297                                 synth_err(SYNTH_ERR_TOO_MANY_FIELDS, 0);
1298                                 ret = -EINVAL;
1299                                 goto err_free_arg;
1300                         }
1301                         fields[n_fields++] = field;
1302
1303                         n_fields_this_loop++;
1304                 }
1305                 argv_free(argv);
1306
1307                 if (consumed < argc) {
1308                         synth_err(SYNTH_ERR_INVALID_CMD, 0);
1309                         ret = -EINVAL;
1310                         goto err;
1311                 }
1312
1313         }
1314
1315         if (n_fields == 0) {
1316                 synth_err(SYNTH_ERR_INVALID_CMD, 0);
1317                 ret = -EINVAL;
1318                 goto err;
1319         }
1320
1321         event = alloc_synth_event(name, n_fields, fields);
1322         if (IS_ERR(event)) {
1323                 ret = PTR_ERR(event);
1324                 event = NULL;
1325                 goto err;
1326         }
1327         ret = register_synth_event(event);
1328         if (!ret)
1329                 dyn_event_add(&event->devent, &event->call);
1330         else
1331                 free_synth_event(event);
1332  out:
1333         mutex_unlock(&event_mutex);
1334
1335         kfree(saved_fields);
1336
1337         return ret;
1338  err_free_arg:
1339         argv_free(argv);
1340  err:
1341         for (i = 0; i < n_fields; i++)
1342                 free_synth_field(fields[i]);
1343
1344         goto out;
1345 }
1346
1347 /**
1348  * synth_event_create - Create a new synthetic event
1349  * @name: The name of the new synthetic event
1350  * @fields: An array of type/name field descriptions
1351  * @n_fields: The number of field descriptions contained in the fields array
1352  * @mod: The module creating the event, NULL if not created from a module
1353  *
1354  * Create a new synthetic event with the given name under the
1355  * trace/events/synthetic/ directory.  The event fields that will be
1356  * defined for the event should be passed in as an array of struct
1357  * synth_field_desc, and the number elements in the array passed in as
1358  * n_fields. Field ordering will retain the ordering given in the
1359  * fields array.
1360  *
1361  * If the new synthetic event is being created from a module, the mod
1362  * param must be non-NULL.  This will ensure that the trace buffer
1363  * won't contain unreadable events.
1364  *
1365  * The new synth event should be deleted using synth_event_delete()
1366  * function.  The new synthetic event can be generated from modules or
1367  * other kernel code using trace_synth_event() and related functions.
1368  *
1369  * Return: 0 if successful, error otherwise.
1370  */
1371 int synth_event_create(const char *name, struct synth_field_desc *fields,
1372                        unsigned int n_fields, struct module *mod)
1373 {
1374         struct dynevent_cmd cmd;
1375         char *buf;
1376         int ret;
1377
1378         buf = kzalloc(MAX_DYNEVENT_CMD_LEN, GFP_KERNEL);
1379         if (!buf)
1380                 return -ENOMEM;
1381
1382         synth_event_cmd_init(&cmd, buf, MAX_DYNEVENT_CMD_LEN);
1383
1384         ret = synth_event_gen_cmd_array_start(&cmd, name, mod,
1385                                               fields, n_fields);
1386         if (ret)
1387                 goto out;
1388
1389         ret = synth_event_gen_cmd_end(&cmd);
1390  out:
1391         kfree(buf);
1392
1393         return ret;
1394 }
1395 EXPORT_SYMBOL_GPL(synth_event_create);
1396
1397 static int destroy_synth_event(struct synth_event *se)
1398 {
1399         int ret;
1400
1401         if (se->ref)
1402                 return -EBUSY;
1403
1404         if (trace_event_dyn_busy(&se->call))
1405                 return -EBUSY;
1406
1407         ret = unregister_synth_event(se);
1408         if (!ret) {
1409                 dyn_event_remove(&se->devent);
1410                 free_synth_event(se);
1411         }
1412
1413         return ret;
1414 }
1415
1416 /**
1417  * synth_event_delete - Delete a synthetic event
1418  * @event_name: The name of the new synthetic event
1419  *
1420  * Delete a synthetic event that was created with synth_event_create().
1421  *
1422  * Return: 0 if successful, error otherwise.
1423  */
1424 int synth_event_delete(const char *event_name)
1425 {
1426         struct synth_event *se = NULL;
1427         struct module *mod = NULL;
1428         int ret = -ENOENT;
1429
1430         mutex_lock(&event_mutex);
1431         se = find_synth_event(event_name);
1432         if (se) {
1433                 mod = se->mod;
1434                 ret = destroy_synth_event(se);
1435         }
1436         mutex_unlock(&event_mutex);
1437
1438         if (mod) {
1439                 /*
1440                  * It is safest to reset the ring buffer if the module
1441                  * being unloaded registered any events that were
1442                  * used. The only worry is if a new module gets
1443                  * loaded, and takes on the same id as the events of
1444                  * this module. When printing out the buffer, traced
1445                  * events left over from this module may be passed to
1446                  * the new module events and unexpected results may
1447                  * occur.
1448                  */
1449                 tracing_reset_all_online_cpus();
1450         }
1451
1452         return ret;
1453 }
1454 EXPORT_SYMBOL_GPL(synth_event_delete);
1455
1456 static int check_command(const char *raw_command)
1457 {
1458         char **argv = NULL, *cmd, *saved_cmd, *name_and_field;
1459         int argc, ret = 0;
1460
1461         cmd = saved_cmd = kstrdup(raw_command, GFP_KERNEL);
1462         if (!cmd)
1463                 return -ENOMEM;
1464
1465         name_and_field = strsep(&cmd, ";");
1466         if (!name_and_field) {
1467                 ret = -EINVAL;
1468                 goto free;
1469         }
1470
1471         if (name_and_field[0] == '!')
1472                 goto free;
1473
1474         argv = argv_split(GFP_KERNEL, name_and_field, &argc);
1475         if (!argv) {
1476                 ret = -ENOMEM;
1477                 goto free;
1478         }
1479         argv_free(argv);
1480
1481         if (argc < 3)
1482                 ret = -EINVAL;
1483 free:
1484         kfree(saved_cmd);
1485
1486         return ret;
1487 }
1488
1489 static int create_or_delete_synth_event(const char *raw_command)
1490 {
1491         char *name = NULL, *fields, *p;
1492         int ret = 0;
1493
1494         raw_command = skip_spaces(raw_command);
1495         if (raw_command[0] == '\0')
1496                 return ret;
1497
1498         last_cmd_set(raw_command);
1499
1500         ret = check_command(raw_command);
1501         if (ret) {
1502                 synth_err(SYNTH_ERR_INVALID_CMD, 0);
1503                 return ret;
1504         }
1505
1506         p = strpbrk(raw_command, " \t");
1507         if (!p && raw_command[0] != '!') {
1508                 synth_err(SYNTH_ERR_INVALID_CMD, 0);
1509                 ret = -EINVAL;
1510                 goto free;
1511         }
1512
1513         name = kmemdup_nul(raw_command, p ? p - raw_command : strlen(raw_command), GFP_KERNEL);
1514         if (!name)
1515                 return -ENOMEM;
1516
1517         if (name[0] == '!') {
1518                 ret = synth_event_delete(name + 1);
1519                 goto free;
1520         }
1521
1522         fields = skip_spaces(p);
1523
1524         ret = __create_synth_event(name, fields);
1525 free:
1526         kfree(name);
1527
1528         return ret;
1529 }
1530
1531 static int synth_event_run_command(struct dynevent_cmd *cmd)
1532 {
1533         struct synth_event *se;
1534         int ret;
1535
1536         ret = create_or_delete_synth_event(cmd->seq.buffer);
1537         if (ret)
1538                 return ret;
1539
1540         se = find_synth_event(cmd->event_name);
1541         if (WARN_ON(!se))
1542                 return -ENOENT;
1543
1544         se->mod = cmd->private_data;
1545
1546         return ret;
1547 }
1548
1549 /**
1550  * synth_event_cmd_init - Initialize a synthetic event command object
1551  * @cmd: A pointer to the dynevent_cmd struct representing the new event
1552  * @buf: A pointer to the buffer used to build the command
1553  * @maxlen: The length of the buffer passed in @buf
1554  *
1555  * Initialize a synthetic event command object.  Use this before
1556  * calling any of the other dyenvent_cmd functions.
1557  */
1558 void synth_event_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen)
1559 {
1560         dynevent_cmd_init(cmd, buf, maxlen, DYNEVENT_TYPE_SYNTH,
1561                           synth_event_run_command);
1562 }
1563 EXPORT_SYMBOL_GPL(synth_event_cmd_init);
1564
1565 static inline int
1566 __synth_event_trace_init(struct trace_event_file *file,
1567                          struct synth_event_trace_state *trace_state)
1568 {
1569         int ret = 0;
1570
1571         memset(trace_state, '\0', sizeof(*trace_state));
1572
1573         /*
1574          * Normal event tracing doesn't get called at all unless the
1575          * ENABLED bit is set (which attaches the probe thus allowing
1576          * this code to be called, etc).  Because this is called
1577          * directly by the user, we don't have that but we still need
1578          * to honor not logging when disabled.  For the iterated
1579          * trace case, we save the enabled state upon start and just
1580          * ignore the following data calls.
1581          */
1582         if (!(file->flags & EVENT_FILE_FL_ENABLED) ||
1583             trace_trigger_soft_disabled(file)) {
1584                 trace_state->disabled = true;
1585                 ret = -ENOENT;
1586                 goto out;
1587         }
1588
1589         trace_state->event = file->event_call->data;
1590 out:
1591         return ret;
1592 }
1593
1594 static inline int
1595 __synth_event_trace_start(struct trace_event_file *file,
1596                           struct synth_event_trace_state *trace_state,
1597                           int dynamic_fields_size)
1598 {
1599         int entry_size, fields_size = 0;
1600         int ret = 0;
1601
1602         fields_size = trace_state->event->n_u64 * sizeof(u64);
1603         fields_size += dynamic_fields_size;
1604
1605         /*
1606          * Avoid ring buffer recursion detection, as this event
1607          * is being performed within another event.
1608          */
1609         trace_state->buffer = file->tr->array_buffer.buffer;
1610         ring_buffer_nest_start(trace_state->buffer);
1611
1612         entry_size = sizeof(*trace_state->entry) + fields_size;
1613         trace_state->entry = trace_event_buffer_reserve(&trace_state->fbuffer,
1614                                                         file,
1615                                                         entry_size);
1616         if (!trace_state->entry) {
1617                 ring_buffer_nest_end(trace_state->buffer);
1618                 ret = -EINVAL;
1619         }
1620
1621         return ret;
1622 }
1623
1624 static inline void
1625 __synth_event_trace_end(struct synth_event_trace_state *trace_state)
1626 {
1627         trace_event_buffer_commit(&trace_state->fbuffer);
1628
1629         ring_buffer_nest_end(trace_state->buffer);
1630 }
1631
1632 /**
1633  * synth_event_trace - Trace a synthetic event
1634  * @file: The trace_event_file representing the synthetic event
1635  * @n_vals: The number of values in vals
1636  * @args: Variable number of args containing the event values
1637  *
1638  * Trace a synthetic event using the values passed in the variable
1639  * argument list.
1640  *
1641  * The argument list should be a list 'n_vals' u64 values.  The number
1642  * of vals must match the number of field in the synthetic event, and
1643  * must be in the same order as the synthetic event fields.
1644  *
1645  * All vals should be cast to u64, and string vals are just pointers
1646  * to strings, cast to u64.  Strings will be copied into space
1647  * reserved in the event for the string, using these pointers.
1648  *
1649  * Return: 0 on success, err otherwise.
1650  */
1651 int synth_event_trace(struct trace_event_file *file, unsigned int n_vals, ...)
1652 {
1653         unsigned int i, n_u64, len, data_size = 0;
1654         struct synth_event_trace_state state;
1655         va_list args;
1656         int ret;
1657
1658         ret = __synth_event_trace_init(file, &state);
1659         if (ret) {
1660                 if (ret == -ENOENT)
1661                         ret = 0; /* just disabled, not really an error */
1662                 return ret;
1663         }
1664
1665         if (state.event->n_dynamic_fields) {
1666                 va_start(args, n_vals);
1667
1668                 for (i = 0; i < state.event->n_fields; i++) {
1669                         u64 val = va_arg(args, u64);
1670
1671                         if (state.event->fields[i]->is_string &&
1672                             state.event->fields[i]->is_dynamic) {
1673                                 char *str_val = (char *)(long)val;
1674
1675                                 data_size += strlen(str_val) + 1;
1676                         }
1677                 }
1678
1679                 va_end(args);
1680         }
1681
1682         ret = __synth_event_trace_start(file, &state, data_size);
1683         if (ret)
1684                 return ret;
1685
1686         if (n_vals != state.event->n_fields) {
1687                 ret = -EINVAL;
1688                 goto out;
1689         }
1690
1691         data_size = 0;
1692
1693         va_start(args, n_vals);
1694         for (i = 0, n_u64 = 0; i < state.event->n_fields; i++) {
1695                 u64 val;
1696
1697                 val = va_arg(args, u64);
1698
1699                 if (state.event->fields[i]->is_string) {
1700                         char *str_val = (char *)(long)val;
1701
1702                         len = trace_string(state.entry, state.event, str_val,
1703                                            state.event->fields[i]->is_dynamic,
1704                                            data_size, &n_u64);
1705                         data_size += len; /* only dynamic string increments */
1706                 } else {
1707                         struct synth_field *field = state.event->fields[i];
1708
1709                         switch (field->size) {
1710                         case 1:
1711                                 *(u8 *)&state.entry->fields[n_u64] = (u8)val;
1712                                 break;
1713
1714                         case 2:
1715                                 *(u16 *)&state.entry->fields[n_u64] = (u16)val;
1716                                 break;
1717
1718                         case 4:
1719                                 *(u32 *)&state.entry->fields[n_u64] = (u32)val;
1720                                 break;
1721
1722                         default:
1723                                 state.entry->fields[n_u64] = val;
1724                                 break;
1725                         }
1726                         n_u64++;
1727                 }
1728         }
1729         va_end(args);
1730 out:
1731         __synth_event_trace_end(&state);
1732
1733         return ret;
1734 }
1735 EXPORT_SYMBOL_GPL(synth_event_trace);
1736
1737 /**
1738  * synth_event_trace_array - Trace a synthetic event from an array
1739  * @file: The trace_event_file representing the synthetic event
1740  * @vals: Array of values
1741  * @n_vals: The number of values in vals
1742  *
1743  * Trace a synthetic event using the values passed in as 'vals'.
1744  *
1745  * The 'vals' array is just an array of 'n_vals' u64.  The number of
1746  * vals must match the number of field in the synthetic event, and
1747  * must be in the same order as the synthetic event fields.
1748  *
1749  * All vals should be cast to u64, and string vals are just pointers
1750  * to strings, cast to u64.  Strings will be copied into space
1751  * reserved in the event for the string, using these pointers.
1752  *
1753  * Return: 0 on success, err otherwise.
1754  */
1755 int synth_event_trace_array(struct trace_event_file *file, u64 *vals,
1756                             unsigned int n_vals)
1757 {
1758         unsigned int i, n_u64, field_pos, len, data_size = 0;
1759         struct synth_event_trace_state state;
1760         char *str_val;
1761         int ret;
1762
1763         ret = __synth_event_trace_init(file, &state);
1764         if (ret) {
1765                 if (ret == -ENOENT)
1766                         ret = 0; /* just disabled, not really an error */
1767                 return ret;
1768         }
1769
1770         if (state.event->n_dynamic_fields) {
1771                 for (i = 0; i < state.event->n_dynamic_fields; i++) {
1772                         field_pos = state.event->dynamic_fields[i]->field_pos;
1773                         str_val = (char *)(long)vals[field_pos];
1774                         len = strlen(str_val) + 1;
1775                         data_size += len;
1776                 }
1777         }
1778
1779         ret = __synth_event_trace_start(file, &state, data_size);
1780         if (ret)
1781                 return ret;
1782
1783         if (n_vals != state.event->n_fields) {
1784                 ret = -EINVAL;
1785                 goto out;
1786         }
1787
1788         data_size = 0;
1789
1790         for (i = 0, n_u64 = 0; i < state.event->n_fields; i++) {
1791                 if (state.event->fields[i]->is_string) {
1792                         char *str_val = (char *)(long)vals[i];
1793
1794                         len = trace_string(state.entry, state.event, str_val,
1795                                            state.event->fields[i]->is_dynamic,
1796                                            data_size, &n_u64);
1797                         data_size += len; /* only dynamic string increments */
1798                 } else {
1799                         struct synth_field *field = state.event->fields[i];
1800                         u64 val = vals[i];
1801
1802                         switch (field->size) {
1803                         case 1:
1804                                 *(u8 *)&state.entry->fields[n_u64] = (u8)val;
1805                                 break;
1806
1807                         case 2:
1808                                 *(u16 *)&state.entry->fields[n_u64] = (u16)val;
1809                                 break;
1810
1811                         case 4:
1812                                 *(u32 *)&state.entry->fields[n_u64] = (u32)val;
1813                                 break;
1814
1815                         default:
1816                                 state.entry->fields[n_u64] = val;
1817                                 break;
1818                         }
1819                         n_u64++;
1820                 }
1821         }
1822 out:
1823         __synth_event_trace_end(&state);
1824
1825         return ret;
1826 }
1827 EXPORT_SYMBOL_GPL(synth_event_trace_array);
1828
1829 /**
1830  * synth_event_trace_start - Start piecewise synthetic event trace
1831  * @file: The trace_event_file representing the synthetic event
1832  * @trace_state: A pointer to object tracking the piecewise trace state
1833  *
1834  * Start the trace of a synthetic event field-by-field rather than all
1835  * at once.
1836  *
1837  * This function 'opens' an event trace, which means space is reserved
1838  * for the event in the trace buffer, after which the event's
1839  * individual field values can be set through either
1840  * synth_event_add_next_val() or synth_event_add_val().
1841  *
1842  * A pointer to a trace_state object is passed in, which will keep
1843  * track of the current event trace state until the event trace is
1844  * closed (and the event finally traced) using
1845  * synth_event_trace_end().
1846  *
1847  * Note that synth_event_trace_end() must be called after all values
1848  * have been added for each event trace, regardless of whether adding
1849  * all field values succeeded or not.
1850  *
1851  * Note also that for a given event trace, all fields must be added
1852  * using either synth_event_add_next_val() or synth_event_add_val()
1853  * but not both together or interleaved.
1854  *
1855  * Return: 0 on success, err otherwise.
1856  */
1857 int synth_event_trace_start(struct trace_event_file *file,
1858                             struct synth_event_trace_state *trace_state)
1859 {
1860         int ret;
1861
1862         if (!trace_state)
1863                 return -EINVAL;
1864
1865         ret = __synth_event_trace_init(file, trace_state);
1866         if (ret) {
1867                 if (ret == -ENOENT)
1868                         ret = 0; /* just disabled, not really an error */
1869                 return ret;
1870         }
1871
1872         if (trace_state->event->n_dynamic_fields)
1873                 return -ENOTSUPP;
1874
1875         ret = __synth_event_trace_start(file, trace_state, 0);
1876
1877         return ret;
1878 }
1879 EXPORT_SYMBOL_GPL(synth_event_trace_start);
1880
1881 static int __synth_event_add_val(const char *field_name, u64 val,
1882                                  struct synth_event_trace_state *trace_state)
1883 {
1884         struct synth_field *field = NULL;
1885         struct synth_trace_event *entry;
1886         struct synth_event *event;
1887         int i, ret = 0;
1888
1889         if (!trace_state) {
1890                 ret = -EINVAL;
1891                 goto out;
1892         }
1893
1894         /* can't mix add_next_synth_val() with add_synth_val() */
1895         if (field_name) {
1896                 if (trace_state->add_next) {
1897                         ret = -EINVAL;
1898                         goto out;
1899                 }
1900                 trace_state->add_name = true;
1901         } else {
1902                 if (trace_state->add_name) {
1903                         ret = -EINVAL;
1904                         goto out;
1905                 }
1906                 trace_state->add_next = true;
1907         }
1908
1909         if (trace_state->disabled)
1910                 goto out;
1911
1912         event = trace_state->event;
1913         if (trace_state->add_name) {
1914                 for (i = 0; i < event->n_fields; i++) {
1915                         field = event->fields[i];
1916                         if (strcmp(field->name, field_name) == 0)
1917                                 break;
1918                 }
1919                 if (!field) {
1920                         ret = -EINVAL;
1921                         goto out;
1922                 }
1923         } else {
1924                 if (trace_state->cur_field >= event->n_fields) {
1925                         ret = -EINVAL;
1926                         goto out;
1927                 }
1928                 field = event->fields[trace_state->cur_field++];
1929         }
1930
1931         entry = trace_state->entry;
1932         if (field->is_string) {
1933                 char *str_val = (char *)(long)val;
1934                 char *str_field;
1935
1936                 if (field->is_dynamic) { /* add_val can't do dynamic strings */
1937                         ret = -EINVAL;
1938                         goto out;
1939                 }
1940
1941                 if (!str_val) {
1942                         ret = -EINVAL;
1943                         goto out;
1944                 }
1945
1946                 str_field = (char *)&entry->fields[field->offset];
1947                 strscpy(str_field, str_val, STR_VAR_LEN_MAX);
1948         } else {
1949                 switch (field->size) {
1950                 case 1:
1951                         *(u8 *)&trace_state->entry->fields[field->offset] = (u8)val;
1952                         break;
1953
1954                 case 2:
1955                         *(u16 *)&trace_state->entry->fields[field->offset] = (u16)val;
1956                         break;
1957
1958                 case 4:
1959                         *(u32 *)&trace_state->entry->fields[field->offset] = (u32)val;
1960                         break;
1961
1962                 default:
1963                         trace_state->entry->fields[field->offset] = val;
1964                         break;
1965                 }
1966         }
1967  out:
1968         return ret;
1969 }
1970
1971 /**
1972  * synth_event_add_next_val - Add the next field's value to an open synth trace
1973  * @val: The value to set the next field to
1974  * @trace_state: A pointer to object tracking the piecewise trace state
1975  *
1976  * Set the value of the next field in an event that's been opened by
1977  * synth_event_trace_start().
1978  *
1979  * The val param should be the value cast to u64.  If the value points
1980  * to a string, the val param should be a char * cast to u64.
1981  *
1982  * This function assumes all the fields in an event are to be set one
1983  * after another - successive calls to this function are made, one for
1984  * each field, in the order of the fields in the event, until all
1985  * fields have been set.  If you'd rather set each field individually
1986  * without regard to ordering, synth_event_add_val() can be used
1987  * instead.
1988  *
1989  * Note however that synth_event_add_next_val() and
1990  * synth_event_add_val() can't be intermixed for a given event trace -
1991  * one or the other but not both can be used at the same time.
1992  *
1993  * Note also that synth_event_trace_end() must be called after all
1994  * values have been added for each event trace, regardless of whether
1995  * adding all field values succeeded or not.
1996  *
1997  * Return: 0 on success, err otherwise.
1998  */
1999 int synth_event_add_next_val(u64 val,
2000                              struct synth_event_trace_state *trace_state)
2001 {
2002         return __synth_event_add_val(NULL, val, trace_state);
2003 }
2004 EXPORT_SYMBOL_GPL(synth_event_add_next_val);
2005
2006 /**
2007  * synth_event_add_val - Add a named field's value to an open synth trace
2008  * @field_name: The name of the synthetic event field value to set
2009  * @val: The value to set the named field to
2010  * @trace_state: A pointer to object tracking the piecewise trace state
2011  *
2012  * Set the value of the named field in an event that's been opened by
2013  * synth_event_trace_start().
2014  *
2015  * The val param should be the value cast to u64.  If the value points
2016  * to a string, the val param should be a char * cast to u64.
2017  *
2018  * This function looks up the field name, and if found, sets the field
2019  * to the specified value.  This lookup makes this function more
2020  * expensive than synth_event_add_next_val(), so use that or the
2021  * none-piecewise synth_event_trace() instead if efficiency is more
2022  * important.
2023  *
2024  * Note however that synth_event_add_next_val() and
2025  * synth_event_add_val() can't be intermixed for a given event trace -
2026  * one or the other but not both can be used at the same time.
2027  *
2028  * Note also that synth_event_trace_end() must be called after all
2029  * values have been added for each event trace, regardless of whether
2030  * adding all field values succeeded or not.
2031  *
2032  * Return: 0 on success, err otherwise.
2033  */
2034 int synth_event_add_val(const char *field_name, u64 val,
2035                         struct synth_event_trace_state *trace_state)
2036 {
2037         return __synth_event_add_val(field_name, val, trace_state);
2038 }
2039 EXPORT_SYMBOL_GPL(synth_event_add_val);
2040
2041 /**
2042  * synth_event_trace_end - End piecewise synthetic event trace
2043  * @trace_state: A pointer to object tracking the piecewise trace state
2044  *
2045  * End the trace of a synthetic event opened by
2046  * synth_event_trace__start().
2047  *
2048  * This function 'closes' an event trace, which basically means that
2049  * it commits the reserved event and cleans up other loose ends.
2050  *
2051  * A pointer to a trace_state object is passed in, which will keep
2052  * track of the current event trace state opened with
2053  * synth_event_trace_start().
2054  *
2055  * Note that this function must be called after all values have been
2056  * added for each event trace, regardless of whether adding all field
2057  * values succeeded or not.
2058  *
2059  * Return: 0 on success, err otherwise.
2060  */
2061 int synth_event_trace_end(struct synth_event_trace_state *trace_state)
2062 {
2063         if (!trace_state)
2064                 return -EINVAL;
2065
2066         __synth_event_trace_end(trace_state);
2067
2068         return 0;
2069 }
2070 EXPORT_SYMBOL_GPL(synth_event_trace_end);
2071
2072 static int create_synth_event(const char *raw_command)
2073 {
2074         char *fields, *p;
2075         const char *name;
2076         int len, ret = 0;
2077
2078         raw_command = skip_spaces(raw_command);
2079         if (raw_command[0] == '\0')
2080                 return ret;
2081
2082         last_cmd_set(raw_command);
2083
2084         name = raw_command;
2085
2086         /* Don't try to process if not our system */
2087         if (name[0] != 's' || name[1] != ':')
2088                 return -ECANCELED;
2089         name += 2;
2090
2091         p = strpbrk(raw_command, " \t");
2092         if (!p) {
2093                 synth_err(SYNTH_ERR_INVALID_CMD, 0);
2094                 return -EINVAL;
2095         }
2096
2097         fields = skip_spaces(p);
2098
2099         /* This interface accepts group name prefix */
2100         if (strchr(name, '/')) {
2101                 len = str_has_prefix(name, SYNTH_SYSTEM "/");
2102                 if (len == 0) {
2103                         synth_err(SYNTH_ERR_INVALID_DYN_CMD, 0);
2104                         return -EINVAL;
2105                 }
2106                 name += len;
2107         }
2108
2109         len = name - raw_command;
2110
2111         ret = check_command(raw_command + len);
2112         if (ret) {
2113                 synth_err(SYNTH_ERR_INVALID_CMD, 0);
2114                 return ret;
2115         }
2116
2117         name = kmemdup_nul(raw_command + len, p - raw_command - len, GFP_KERNEL);
2118         if (!name)
2119                 return -ENOMEM;
2120
2121         ret = __create_synth_event(name, fields);
2122
2123         kfree(name);
2124
2125         return ret;
2126 }
2127
2128 static int synth_event_release(struct dyn_event *ev)
2129 {
2130         struct synth_event *event = to_synth_event(ev);
2131         int ret;
2132
2133         if (event->ref)
2134                 return -EBUSY;
2135
2136         if (trace_event_dyn_busy(&event->call))
2137                 return -EBUSY;
2138
2139         ret = unregister_synth_event(event);
2140         if (ret)
2141                 return ret;
2142
2143         dyn_event_remove(ev);
2144         free_synth_event(event);
2145         return 0;
2146 }
2147
2148 static int __synth_event_show(struct seq_file *m, struct synth_event *event)
2149 {
2150         struct synth_field *field;
2151         unsigned int i;
2152         char *type, *t;
2153
2154         seq_printf(m, "%s\t", event->name);
2155
2156         for (i = 0; i < event->n_fields; i++) {
2157                 field = event->fields[i];
2158
2159                 type = field->type;
2160                 t = strstr(type, "__data_loc");
2161                 if (t) { /* __data_loc belongs in format but not event desc */
2162                         t += sizeof("__data_loc");
2163                         type = t;
2164                 }
2165
2166                 /* parameter values */
2167                 seq_printf(m, "%s %s%s", type, field->name,
2168                            i == event->n_fields - 1 ? "" : "; ");
2169         }
2170
2171         seq_putc(m, '\n');
2172
2173         return 0;
2174 }
2175
2176 static int synth_event_show(struct seq_file *m, struct dyn_event *ev)
2177 {
2178         struct synth_event *event = to_synth_event(ev);
2179
2180         seq_printf(m, "s:%s/", event->class.system);
2181
2182         return __synth_event_show(m, event);
2183 }
2184
2185 static int synth_events_seq_show(struct seq_file *m, void *v)
2186 {
2187         struct dyn_event *ev = v;
2188
2189         if (!is_synth_event(ev))
2190                 return 0;
2191
2192         return __synth_event_show(m, to_synth_event(ev));
2193 }
2194
2195 static const struct seq_operations synth_events_seq_op = {
2196         .start  = dyn_event_seq_start,
2197         .next   = dyn_event_seq_next,
2198         .stop   = dyn_event_seq_stop,
2199         .show   = synth_events_seq_show,
2200 };
2201
2202 static int synth_events_open(struct inode *inode, struct file *file)
2203 {
2204         int ret;
2205
2206         ret = security_locked_down(LOCKDOWN_TRACEFS);
2207         if (ret)
2208                 return ret;
2209
2210         if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
2211                 ret = dyn_events_release_all(&synth_event_ops);
2212                 if (ret < 0)
2213                         return ret;
2214         }
2215
2216         return seq_open(file, &synth_events_seq_op);
2217 }
2218
2219 static ssize_t synth_events_write(struct file *file,
2220                                   const char __user *buffer,
2221                                   size_t count, loff_t *ppos)
2222 {
2223         return trace_parse_run_command(file, buffer, count, ppos,
2224                                        create_or_delete_synth_event);
2225 }
2226
2227 static const struct file_operations synth_events_fops = {
2228         .open           = synth_events_open,
2229         .write          = synth_events_write,
2230         .read           = seq_read,
2231         .llseek         = seq_lseek,
2232         .release        = seq_release,
2233 };
2234
2235 /*
2236  * Register dynevent at core_initcall. This allows kernel to setup kprobe
2237  * events in postcore_initcall without tracefs.
2238  */
2239 static __init int trace_events_synth_init_early(void)
2240 {
2241         int err = 0;
2242
2243         err = dyn_event_register(&synth_event_ops);
2244         if (err)
2245                 pr_warn("Could not register synth_event_ops\n");
2246
2247         return err;
2248 }
2249 core_initcall(trace_events_synth_init_early);
2250
2251 static __init int trace_events_synth_init(void)
2252 {
2253         struct dentry *entry = NULL;
2254         int err = 0;
2255         err = tracing_init_dentry();
2256         if (err)
2257                 goto err;
2258
2259         entry = tracefs_create_file("synthetic_events", TRACE_MODE_WRITE,
2260                                     NULL, NULL, &synth_events_fops);
2261         if (!entry) {
2262                 err = -ENODEV;
2263                 goto err;
2264         }
2265
2266         return err;
2267  err:
2268         pr_warn("Could not create tracefs 'synthetic_events' entry\n");
2269
2270         return err;
2271 }
2272
2273 fs_initcall(trace_events_synth_init);