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