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