Merge tag 'tty-5.10-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/tty
[platform/kernel/linux-rpi.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 = 0;
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                 ret = -ENOMEM;
622                 goto free;
623         }
624         if (!is_good_name(field->name)) {
625                 synth_err(SYNTH_ERR_BAD_NAME, errpos(field_name));
626                 ret = -EINVAL;
627                 goto free;
628         }
629
630         if (field_type[0] == ';')
631                 field_type++;
632         len = strlen(field_type) + 1;
633
634         if (array)
635                 len += strlen(array);
636
637         if (prefix)
638                 len += strlen(prefix);
639
640         field->type = kzalloc(len, GFP_KERNEL);
641         if (!field->type) {
642                 ret = -ENOMEM;
643                 goto free;
644         }
645         seq_buf_init(&s, field->type, len);
646         if (prefix)
647                 seq_buf_puts(&s, prefix);
648         seq_buf_puts(&s, field_type);
649         if (array) {
650                 seq_buf_puts(&s, array);
651                 if (s.buffer[s.len - 1] == ';')
652                         s.len--;
653         }
654         if (WARN_ON_ONCE(!seq_buf_buffer_left(&s)))
655                 goto free;
656         s.buffer[s.len] = '\0';
657
658         size = synth_field_size(field->type);
659         if (size < 0) {
660                 synth_err(SYNTH_ERR_INVALID_TYPE, errpos(field_type));
661                 ret = -EINVAL;
662                 goto free;
663         } else if (size == 0) {
664                 if (synth_field_is_string(field->type)) {
665                         char *type;
666
667                         len = sizeof("__data_loc ") + strlen(field->type) + 1;
668                         type = kzalloc(len, GFP_KERNEL);
669                         if (!type) {
670                                 ret = -ENOMEM;
671                                 goto free;
672                         }
673
674                         seq_buf_init(&s, type, len);
675                         seq_buf_puts(&s, "__data_loc ");
676                         seq_buf_puts(&s, field->type);
677
678                         if (WARN_ON_ONCE(!seq_buf_buffer_left(&s)))
679                                 goto free;
680                         s.buffer[s.len] = '\0';
681
682                         kfree(field->type);
683                         field->type = type;
684
685                         field->is_dynamic = true;
686                         size = sizeof(u64);
687                 } else {
688                         synth_err(SYNTH_ERR_INVALID_TYPE, errpos(field_type));
689                         ret = -EINVAL;
690                         goto free;
691                 }
692         }
693         field->size = size;
694
695         if (synth_field_is_string(field->type))
696                 field->is_string = true;
697
698         field->is_signed = synth_field_signed(field->type);
699  out:
700         return field;
701  free:
702         free_synth_field(field);
703         field = ERR_PTR(ret);
704         goto out;
705 }
706
707 static void free_synth_tracepoint(struct tracepoint *tp)
708 {
709         if (!tp)
710                 return;
711
712         kfree(tp->name);
713         kfree(tp);
714 }
715
716 static struct tracepoint *alloc_synth_tracepoint(char *name)
717 {
718         struct tracepoint *tp;
719
720         tp = kzalloc(sizeof(*tp), GFP_KERNEL);
721         if (!tp)
722                 return ERR_PTR(-ENOMEM);
723
724         tp->name = kstrdup(name, GFP_KERNEL);
725         if (!tp->name) {
726                 kfree(tp);
727                 return ERR_PTR(-ENOMEM);
728         }
729
730         return tp;
731 }
732
733 struct synth_event *find_synth_event(const char *name)
734 {
735         struct dyn_event *pos;
736         struct synth_event *event;
737
738         for_each_dyn_event(pos) {
739                 if (!is_synth_event(pos))
740                         continue;
741                 event = to_synth_event(pos);
742                 if (strcmp(event->name, name) == 0)
743                         return event;
744         }
745
746         return NULL;
747 }
748
749 static struct trace_event_fields synth_event_fields_array[] = {
750         { .type = TRACE_FUNCTION_TYPE,
751           .define_fields = synth_event_define_fields },
752         {}
753 };
754
755 static int register_synth_event(struct synth_event *event)
756 {
757         struct trace_event_call *call = &event->call;
758         int ret = 0;
759
760         event->call.class = &event->class;
761         event->class.system = kstrdup(SYNTH_SYSTEM, GFP_KERNEL);
762         if (!event->class.system) {
763                 ret = -ENOMEM;
764                 goto out;
765         }
766
767         event->tp = alloc_synth_tracepoint(event->name);
768         if (IS_ERR(event->tp)) {
769                 ret = PTR_ERR(event->tp);
770                 event->tp = NULL;
771                 goto out;
772         }
773
774         INIT_LIST_HEAD(&call->class->fields);
775         call->event.funcs = &synth_event_funcs;
776         call->class->fields_array = synth_event_fields_array;
777
778         ret = register_trace_event(&call->event);
779         if (!ret) {
780                 ret = -ENODEV;
781                 goto out;
782         }
783         call->flags = TRACE_EVENT_FL_TRACEPOINT;
784         call->class->reg = trace_event_reg;
785         call->class->probe = trace_event_raw_event_synth;
786         call->data = event;
787         call->tp = event->tp;
788
789         ret = trace_add_event_call(call);
790         if (ret) {
791                 pr_warn("Failed to register synthetic event: %s\n",
792                         trace_event_name(call));
793                 goto err;
794         }
795
796         ret = set_synth_event_print_fmt(call);
797         if (ret < 0) {
798                 trace_remove_event_call(call);
799                 goto err;
800         }
801  out:
802         return ret;
803  err:
804         unregister_trace_event(&call->event);
805         goto out;
806 }
807
808 static int unregister_synth_event(struct synth_event *event)
809 {
810         struct trace_event_call *call = &event->call;
811         int ret;
812
813         ret = trace_remove_event_call(call);
814
815         return ret;
816 }
817
818 static void free_synth_event(struct synth_event *event)
819 {
820         unsigned int i;
821
822         if (!event)
823                 return;
824
825         for (i = 0; i < event->n_fields; i++)
826                 free_synth_field(event->fields[i]);
827
828         kfree(event->fields);
829         kfree(event->dynamic_fields);
830         kfree(event->name);
831         kfree(event->class.system);
832         free_synth_tracepoint(event->tp);
833         free_synth_event_print_fmt(&event->call);
834         kfree(event);
835 }
836
837 static struct synth_event *alloc_synth_event(const char *name, int n_fields,
838                                              struct synth_field **fields)
839 {
840         unsigned int i, j, n_dynamic_fields = 0;
841         struct synth_event *event;
842
843         event = kzalloc(sizeof(*event), GFP_KERNEL);
844         if (!event) {
845                 event = ERR_PTR(-ENOMEM);
846                 goto out;
847         }
848
849         event->name = kstrdup(name, GFP_KERNEL);
850         if (!event->name) {
851                 kfree(event);
852                 event = ERR_PTR(-ENOMEM);
853                 goto out;
854         }
855
856         event->fields = kcalloc(n_fields, sizeof(*event->fields), GFP_KERNEL);
857         if (!event->fields) {
858                 free_synth_event(event);
859                 event = ERR_PTR(-ENOMEM);
860                 goto out;
861         }
862
863         for (i = 0; i < n_fields; i++)
864                 if (fields[i]->is_dynamic)
865                         n_dynamic_fields++;
866
867         if (n_dynamic_fields) {
868                 event->dynamic_fields = kcalloc(n_dynamic_fields,
869                                                 sizeof(*event->dynamic_fields),
870                                                 GFP_KERNEL);
871                 if (!event->dynamic_fields) {
872                         free_synth_event(event);
873                         event = ERR_PTR(-ENOMEM);
874                         goto out;
875                 }
876         }
877
878         dyn_event_init(&event->devent, &synth_event_ops);
879
880         for (i = 0, j = 0; i < n_fields; i++) {
881                 event->fields[i] = fields[i];
882
883                 if (fields[i]->is_dynamic) {
884                         event->dynamic_fields[j] = fields[i];
885                         event->dynamic_fields[j]->field_pos = i;
886                         event->dynamic_fields[j++] = fields[i];
887                         event->n_dynamic_fields++;
888                 }
889         }
890         event->n_fields = n_fields;
891  out:
892         return event;
893 }
894
895 static int synth_event_check_arg_fn(void *data)
896 {
897         struct dynevent_arg_pair *arg_pair = data;
898         int size;
899
900         size = synth_field_size((char *)arg_pair->lhs);
901         if (size == 0) {
902                 if (strstr((char *)arg_pair->lhs, "["))
903                         return 0;
904         }
905
906         return size ? 0 : -EINVAL;
907 }
908
909 /**
910  * synth_event_add_field - Add a new field to a synthetic event cmd
911  * @cmd: A pointer to the dynevent_cmd struct representing the new event
912  * @type: The type of the new field to add
913  * @name: The name of the new field to add
914  *
915  * Add a new field to a synthetic event cmd object.  Field ordering is in
916  * the same order the fields are added.
917  *
918  * See synth_field_size() for available types. If field_name contains
919  * [n] the field is considered to be an array.
920  *
921  * Return: 0 if successful, error otherwise.
922  */
923 int synth_event_add_field(struct dynevent_cmd *cmd, const char *type,
924                           const char *name)
925 {
926         struct dynevent_arg_pair arg_pair;
927         int ret;
928
929         if (cmd->type != DYNEVENT_TYPE_SYNTH)
930                 return -EINVAL;
931
932         if (!type || !name)
933                 return -EINVAL;
934
935         dynevent_arg_pair_init(&arg_pair, 0, ';');
936
937         arg_pair.lhs = type;
938         arg_pair.rhs = name;
939
940         ret = dynevent_arg_pair_add(cmd, &arg_pair, synth_event_check_arg_fn);
941         if (ret)
942                 return ret;
943
944         if (++cmd->n_fields > SYNTH_FIELDS_MAX)
945                 ret = -EINVAL;
946
947         return ret;
948 }
949 EXPORT_SYMBOL_GPL(synth_event_add_field);
950
951 /**
952  * synth_event_add_field_str - Add a new field to a synthetic event cmd
953  * @cmd: A pointer to the dynevent_cmd struct representing the new event
954  * @type_name: The type and name of the new field to add, as a single string
955  *
956  * Add a new field to a synthetic event cmd object, as a single
957  * string.  The @type_name string is expected to be of the form 'type
958  * name', which will be appended by ';'.  No sanity checking is done -
959  * what's passed in is assumed to already be well-formed.  Field
960  * ordering is in the same order the fields are added.
961  *
962  * See synth_field_size() for available types. If field_name contains
963  * [n] the field is considered to be an array.
964  *
965  * Return: 0 if successful, error otherwise.
966  */
967 int synth_event_add_field_str(struct dynevent_cmd *cmd, const char *type_name)
968 {
969         struct dynevent_arg arg;
970         int ret;
971
972         if (cmd->type != DYNEVENT_TYPE_SYNTH)
973                 return -EINVAL;
974
975         if (!type_name)
976                 return -EINVAL;
977
978         dynevent_arg_init(&arg, ';');
979
980         arg.str = type_name;
981
982         ret = dynevent_arg_add(cmd, &arg, NULL);
983         if (ret)
984                 return ret;
985
986         if (++cmd->n_fields > SYNTH_FIELDS_MAX)
987                 ret = -EINVAL;
988
989         return ret;
990 }
991 EXPORT_SYMBOL_GPL(synth_event_add_field_str);
992
993 /**
994  * synth_event_add_fields - Add multiple fields to a synthetic event cmd
995  * @cmd: A pointer to the dynevent_cmd struct representing the new event
996  * @fields: An array of type/name field descriptions
997  * @n_fields: The number of field descriptions contained in the fields array
998  *
999  * Add a new set of fields to a synthetic event cmd object.  The event
1000  * fields that will be defined for the event should be passed in as an
1001  * array of struct synth_field_desc, and the number of elements in the
1002  * array passed in as n_fields.  Field ordering will retain the
1003  * ordering given in the fields array.
1004  *
1005  * See synth_field_size() for available types. If field_name contains
1006  * [n] the field is considered to be an array.
1007  *
1008  * Return: 0 if successful, error otherwise.
1009  */
1010 int synth_event_add_fields(struct dynevent_cmd *cmd,
1011                            struct synth_field_desc *fields,
1012                            unsigned int n_fields)
1013 {
1014         unsigned int i;
1015         int ret = 0;
1016
1017         for (i = 0; i < n_fields; i++) {
1018                 if (fields[i].type == NULL || fields[i].name == NULL) {
1019                         ret = -EINVAL;
1020                         break;
1021                 }
1022
1023                 ret = synth_event_add_field(cmd, fields[i].type, fields[i].name);
1024                 if (ret)
1025                         break;
1026         }
1027
1028         return ret;
1029 }
1030 EXPORT_SYMBOL_GPL(synth_event_add_fields);
1031
1032 /**
1033  * __synth_event_gen_cmd_start - Start a synthetic event command from arg list
1034  * @cmd: A pointer to the dynevent_cmd struct representing the new event
1035  * @name: The name of the synthetic event
1036  * @mod: The module creating the event, NULL if not created from a module
1037  * @args: Variable number of arg (pairs), one pair for each field
1038  *
1039  * NOTE: Users normally won't want to call this function directly, but
1040  * rather use the synth_event_gen_cmd_start() wrapper, which
1041  * automatically adds a NULL to the end of the arg list.  If this
1042  * function is used directly, make sure the last arg in the variable
1043  * arg list is NULL.
1044  *
1045  * Generate a synthetic event command to be executed by
1046  * synth_event_gen_cmd_end().  This function can be used to generate
1047  * the complete command or only the first part of it; in the latter
1048  * case, synth_event_add_field(), synth_event_add_field_str(), or
1049  * synth_event_add_fields() can be used to add more fields following
1050  * this.
1051  *
1052  * There should be an even number variable args, each pair consisting
1053  * of a type followed by a field name.
1054  *
1055  * See synth_field_size() for available types. If field_name contains
1056  * [n] the field is considered to be an array.
1057  *
1058  * Return: 0 if successful, error otherwise.
1059  */
1060 int __synth_event_gen_cmd_start(struct dynevent_cmd *cmd, const char *name,
1061                                 struct module *mod, ...)
1062 {
1063         struct dynevent_arg arg;
1064         va_list args;
1065         int ret;
1066
1067         cmd->event_name = name;
1068         cmd->private_data = mod;
1069
1070         if (cmd->type != DYNEVENT_TYPE_SYNTH)
1071                 return -EINVAL;
1072
1073         dynevent_arg_init(&arg, 0);
1074         arg.str = name;
1075         ret = dynevent_arg_add(cmd, &arg, NULL);
1076         if (ret)
1077                 return ret;
1078
1079         va_start(args, mod);
1080         for (;;) {
1081                 const char *type, *name;
1082
1083                 type = va_arg(args, const char *);
1084                 if (!type)
1085                         break;
1086                 name = va_arg(args, const char *);
1087                 if (!name)
1088                         break;
1089
1090                 if (++cmd->n_fields > SYNTH_FIELDS_MAX) {
1091                         ret = -EINVAL;
1092                         break;
1093                 }
1094
1095                 ret = synth_event_add_field(cmd, type, name);
1096                 if (ret)
1097                         break;
1098         }
1099         va_end(args);
1100
1101         return ret;
1102 }
1103 EXPORT_SYMBOL_GPL(__synth_event_gen_cmd_start);
1104
1105 /**
1106  * synth_event_gen_cmd_array_start - Start synthetic event command from an array
1107  * @cmd: A pointer to the dynevent_cmd struct representing the new event
1108  * @name: The name of the synthetic event
1109  * @fields: An array of type/name field descriptions
1110  * @n_fields: The number of field descriptions contained in the fields array
1111  *
1112  * Generate a synthetic event command to be executed by
1113  * synth_event_gen_cmd_end().  This function can be used to generate
1114  * the complete command or only the first part of it; in the latter
1115  * case, synth_event_add_field(), synth_event_add_field_str(), or
1116  * synth_event_add_fields() can be used to add more fields following
1117  * this.
1118  *
1119  * The event fields that will be defined for the event should be
1120  * passed in as an array of struct synth_field_desc, and the number of
1121  * elements in the array passed in as n_fields.  Field ordering will
1122  * retain the ordering given in the fields array.
1123  *
1124  * See synth_field_size() for available types. If field_name contains
1125  * [n] the field is considered to be an array.
1126  *
1127  * Return: 0 if successful, error otherwise.
1128  */
1129 int synth_event_gen_cmd_array_start(struct dynevent_cmd *cmd, const char *name,
1130                                     struct module *mod,
1131                                     struct synth_field_desc *fields,
1132                                     unsigned int n_fields)
1133 {
1134         struct dynevent_arg arg;
1135         unsigned int i;
1136         int ret = 0;
1137
1138         cmd->event_name = name;
1139         cmd->private_data = mod;
1140
1141         if (cmd->type != DYNEVENT_TYPE_SYNTH)
1142                 return -EINVAL;
1143
1144         if (n_fields > SYNTH_FIELDS_MAX)
1145                 return -EINVAL;
1146
1147         dynevent_arg_init(&arg, 0);
1148         arg.str = name;
1149         ret = dynevent_arg_add(cmd, &arg, NULL);
1150         if (ret)
1151                 return ret;
1152
1153         for (i = 0; i < n_fields; i++) {
1154                 if (fields[i].type == NULL || fields[i].name == NULL)
1155                         return -EINVAL;
1156
1157                 ret = synth_event_add_field(cmd, fields[i].type, fields[i].name);
1158                 if (ret)
1159                         break;
1160         }
1161
1162         return ret;
1163 }
1164 EXPORT_SYMBOL_GPL(synth_event_gen_cmd_array_start);
1165
1166 static int save_cmdstr(int argc, const char *name, const char **argv)
1167 {
1168         struct seq_buf s;
1169         char *buf;
1170         int i;
1171
1172         buf = kzalloc(MAX_DYNEVENT_CMD_LEN, GFP_KERNEL);
1173         if (!buf)
1174                 return -ENOMEM;
1175
1176         seq_buf_init(&s, buf, MAX_DYNEVENT_CMD_LEN);
1177
1178         seq_buf_puts(&s, name);
1179
1180         for (i = 0; i < argc; i++) {
1181                 seq_buf_putc(&s, ' ');
1182                 seq_buf_puts(&s, argv[i]);
1183         }
1184
1185         if (!seq_buf_buffer_left(&s)) {
1186                 synth_err(SYNTH_ERR_CMD_TOO_LONG, 0);
1187                 kfree(buf);
1188                 return -EINVAL;
1189         }
1190         buf[s.len] = 0;
1191         last_cmd_set(buf);
1192
1193         kfree(buf);
1194         return 0;
1195 }
1196
1197 static int __create_synth_event(int argc, const char *name, const char **argv)
1198 {
1199         struct synth_field *field, *fields[SYNTH_FIELDS_MAX];
1200         struct synth_event *event = NULL;
1201         int i, consumed = 0, n_fields = 0, ret = 0;
1202
1203         ret = save_cmdstr(argc, name, argv);
1204         if (ret)
1205                 return ret;
1206
1207         /*
1208          * Argument syntax:
1209          *  - Add synthetic event: <event_name> field[;field] ...
1210          *  - Remove synthetic event: !<event_name> field[;field] ...
1211          *      where 'field' = type field_name
1212          */
1213
1214         if (name[0] == '\0' || argc < 1) {
1215                 synth_err(SYNTH_ERR_CMD_INCOMPLETE, 0);
1216                 return -EINVAL;
1217         }
1218
1219         mutex_lock(&event_mutex);
1220
1221         if (!is_good_name(name)) {
1222                 synth_err(SYNTH_ERR_BAD_NAME, errpos(name));
1223                 ret = -EINVAL;
1224                 goto out;
1225         }
1226
1227         event = find_synth_event(name);
1228         if (event) {
1229                 synth_err(SYNTH_ERR_EVENT_EXISTS, errpos(name));
1230                 ret = -EEXIST;
1231                 goto out;
1232         }
1233
1234         for (i = 0; i < argc - 1; i++) {
1235                 if (strcmp(argv[i], ";") == 0)
1236                         continue;
1237                 if (n_fields == SYNTH_FIELDS_MAX) {
1238                         synth_err(SYNTH_ERR_TOO_MANY_FIELDS, 0);
1239                         ret = -EINVAL;
1240                         goto err;
1241                 }
1242
1243                 field = parse_synth_field(argc - i, &argv[i], &consumed);
1244                 if (IS_ERR(field)) {
1245                         ret = PTR_ERR(field);
1246                         goto err;
1247                 }
1248                 fields[n_fields++] = field;
1249                 i += consumed - 1;
1250         }
1251
1252         if (i < argc && strcmp(argv[i], ";") != 0) {
1253                 synth_err(SYNTH_ERR_INVALID_FIELD, errpos(argv[i]));
1254                 ret = -EINVAL;
1255                 goto err;
1256         }
1257
1258         event = alloc_synth_event(name, n_fields, fields);
1259         if (IS_ERR(event)) {
1260                 ret = PTR_ERR(event);
1261                 event = NULL;
1262                 goto err;
1263         }
1264         ret = register_synth_event(event);
1265         if (!ret)
1266                 dyn_event_add(&event->devent);
1267         else
1268                 free_synth_event(event);
1269  out:
1270         mutex_unlock(&event_mutex);
1271
1272         return ret;
1273  err:
1274         for (i = 0; i < n_fields; i++)
1275                 free_synth_field(fields[i]);
1276
1277         goto out;
1278 }
1279
1280 /**
1281  * synth_event_create - Create a new synthetic event
1282  * @name: The name of the new sythetic event
1283  * @fields: An array of type/name field descriptions
1284  * @n_fields: The number of field descriptions contained in the fields array
1285  * @mod: The module creating the event, NULL if not created from a module
1286  *
1287  * Create a new synthetic event with the given name under the
1288  * trace/events/synthetic/ directory.  The event fields that will be
1289  * defined for the event should be passed in as an array of struct
1290  * synth_field_desc, and the number elements in the array passed in as
1291  * n_fields. Field ordering will retain the ordering given in the
1292  * fields array.
1293  *
1294  * If the new synthetic event is being created from a module, the mod
1295  * param must be non-NULL.  This will ensure that the trace buffer
1296  * won't contain unreadable events.
1297  *
1298  * The new synth event should be deleted using synth_event_delete()
1299  * function.  The new synthetic event can be generated from modules or
1300  * other kernel code using trace_synth_event() and related functions.
1301  *
1302  * Return: 0 if successful, error otherwise.
1303  */
1304 int synth_event_create(const char *name, struct synth_field_desc *fields,
1305                        unsigned int n_fields, struct module *mod)
1306 {
1307         struct dynevent_cmd cmd;
1308         char *buf;
1309         int ret;
1310
1311         buf = kzalloc(MAX_DYNEVENT_CMD_LEN, GFP_KERNEL);
1312         if (!buf)
1313                 return -ENOMEM;
1314
1315         synth_event_cmd_init(&cmd, buf, MAX_DYNEVENT_CMD_LEN);
1316
1317         ret = synth_event_gen_cmd_array_start(&cmd, name, mod,
1318                                               fields, n_fields);
1319         if (ret)
1320                 goto out;
1321
1322         ret = synth_event_gen_cmd_end(&cmd);
1323  out:
1324         kfree(buf);
1325
1326         return ret;
1327 }
1328 EXPORT_SYMBOL_GPL(synth_event_create);
1329
1330 static int destroy_synth_event(struct synth_event *se)
1331 {
1332         int ret;
1333
1334         if (se->ref)
1335                 ret = -EBUSY;
1336         else {
1337                 ret = unregister_synth_event(se);
1338                 if (!ret) {
1339                         dyn_event_remove(&se->devent);
1340                         free_synth_event(se);
1341                 }
1342         }
1343
1344         return ret;
1345 }
1346
1347 /**
1348  * synth_event_delete - Delete a synthetic event
1349  * @event_name: The name of the new sythetic event
1350  *
1351  * Delete a synthetic event that was created with synth_event_create().
1352  *
1353  * Return: 0 if successful, error otherwise.
1354  */
1355 int synth_event_delete(const char *event_name)
1356 {
1357         struct synth_event *se = NULL;
1358         struct module *mod = NULL;
1359         int ret = -ENOENT;
1360
1361         mutex_lock(&event_mutex);
1362         se = find_synth_event(event_name);
1363         if (se) {
1364                 mod = se->mod;
1365                 ret = destroy_synth_event(se);
1366         }
1367         mutex_unlock(&event_mutex);
1368
1369         if (mod) {
1370                 mutex_lock(&trace_types_lock);
1371                 /*
1372                  * It is safest to reset the ring buffer if the module
1373                  * being unloaded registered any events that were
1374                  * used. The only worry is if a new module gets
1375                  * loaded, and takes on the same id as the events of
1376                  * this module. When printing out the buffer, traced
1377                  * events left over from this module may be passed to
1378                  * the new module events and unexpected results may
1379                  * occur.
1380                  */
1381                 tracing_reset_all_online_cpus();
1382                 mutex_unlock(&trace_types_lock);
1383         }
1384
1385         return ret;
1386 }
1387 EXPORT_SYMBOL_GPL(synth_event_delete);
1388
1389 static int create_or_delete_synth_event(int argc, char **argv)
1390 {
1391         const char *name = argv[0];
1392         int ret;
1393
1394         /* trace_run_command() ensures argc != 0 */
1395         if (name[0] == '!') {
1396                 ret = synth_event_delete(name + 1);
1397                 return ret;
1398         }
1399
1400         ret = __create_synth_event(argc - 1, name, (const char **)argv + 1);
1401         return ret == -ECANCELED ? -EINVAL : ret;
1402 }
1403
1404 static int synth_event_run_command(struct dynevent_cmd *cmd)
1405 {
1406         struct synth_event *se;
1407         int ret;
1408
1409         ret = trace_run_command(cmd->seq.buffer, create_or_delete_synth_event);
1410         if (ret)
1411                 return ret;
1412
1413         se = find_synth_event(cmd->event_name);
1414         if (WARN_ON(!se))
1415                 return -ENOENT;
1416
1417         se->mod = cmd->private_data;
1418
1419         return ret;
1420 }
1421
1422 /**
1423  * synth_event_cmd_init - Initialize a synthetic event command object
1424  * @cmd: A pointer to the dynevent_cmd struct representing the new event
1425  * @buf: A pointer to the buffer used to build the command
1426  * @maxlen: The length of the buffer passed in @buf
1427  *
1428  * Initialize a synthetic event command object.  Use this before
1429  * calling any of the other dyenvent_cmd functions.
1430  */
1431 void synth_event_cmd_init(struct dynevent_cmd *cmd, char *buf, int maxlen)
1432 {
1433         dynevent_cmd_init(cmd, buf, maxlen, DYNEVENT_TYPE_SYNTH,
1434                           synth_event_run_command);
1435 }
1436 EXPORT_SYMBOL_GPL(synth_event_cmd_init);
1437
1438 static inline int
1439 __synth_event_trace_init(struct trace_event_file *file,
1440                          struct synth_event_trace_state *trace_state)
1441 {
1442         int ret = 0;
1443
1444         memset(trace_state, '\0', sizeof(*trace_state));
1445
1446         /*
1447          * Normal event tracing doesn't get called at all unless the
1448          * ENABLED bit is set (which attaches the probe thus allowing
1449          * this code to be called, etc).  Because this is called
1450          * directly by the user, we don't have that but we still need
1451          * to honor not logging when disabled.  For the iterated
1452          * trace case, we save the enabed state upon start and just
1453          * ignore the following data calls.
1454          */
1455         if (!(file->flags & EVENT_FILE_FL_ENABLED) ||
1456             trace_trigger_soft_disabled(file)) {
1457                 trace_state->disabled = true;
1458                 ret = -ENOENT;
1459                 goto out;
1460         }
1461
1462         trace_state->event = file->event_call->data;
1463 out:
1464         return ret;
1465 }
1466
1467 static inline int
1468 __synth_event_trace_start(struct trace_event_file *file,
1469                           struct synth_event_trace_state *trace_state,
1470                           int dynamic_fields_size)
1471 {
1472         int entry_size, fields_size = 0;
1473         int ret = 0;
1474
1475         fields_size = trace_state->event->n_u64 * sizeof(u64);
1476         fields_size += dynamic_fields_size;
1477
1478         /*
1479          * Avoid ring buffer recursion detection, as this event
1480          * is being performed within another event.
1481          */
1482         trace_state->buffer = file->tr->array_buffer.buffer;
1483         ring_buffer_nest_start(trace_state->buffer);
1484
1485         entry_size = sizeof(*trace_state->entry) + fields_size;
1486         trace_state->entry = trace_event_buffer_reserve(&trace_state->fbuffer,
1487                                                         file,
1488                                                         entry_size);
1489         if (!trace_state->entry) {
1490                 ring_buffer_nest_end(trace_state->buffer);
1491                 ret = -EINVAL;
1492         }
1493
1494         return ret;
1495 }
1496
1497 static inline void
1498 __synth_event_trace_end(struct synth_event_trace_state *trace_state)
1499 {
1500         trace_event_buffer_commit(&trace_state->fbuffer);
1501
1502         ring_buffer_nest_end(trace_state->buffer);
1503 }
1504
1505 /**
1506  * synth_event_trace - Trace a synthetic event
1507  * @file: The trace_event_file representing the synthetic event
1508  * @n_vals: The number of values in vals
1509  * @args: Variable number of args containing the event values
1510  *
1511  * Trace a synthetic event using the values passed in the variable
1512  * argument list.
1513  *
1514  * The argument list should be a list 'n_vals' u64 values.  The number
1515  * of vals must match the number of field in the synthetic event, and
1516  * must be in the same order as the synthetic event fields.
1517  *
1518  * All vals should be cast to u64, and string vals are just pointers
1519  * to strings, cast to u64.  Strings will be copied into space
1520  * reserved in the event for the string, using these pointers.
1521  *
1522  * Return: 0 on success, err otherwise.
1523  */
1524 int synth_event_trace(struct trace_event_file *file, unsigned int n_vals, ...)
1525 {
1526         unsigned int i, n_u64, len, data_size = 0;
1527         struct synth_event_trace_state state;
1528         va_list args;
1529         int ret;
1530
1531         ret = __synth_event_trace_init(file, &state);
1532         if (ret) {
1533                 if (ret == -ENOENT)
1534                         ret = 0; /* just disabled, not really an error */
1535                 return ret;
1536         }
1537
1538         if (state.event->n_dynamic_fields) {
1539                 va_start(args, n_vals);
1540
1541                 for (i = 0; i < state.event->n_fields; i++) {
1542                         u64 val = va_arg(args, u64);
1543
1544                         if (state.event->fields[i]->is_string &&
1545                             state.event->fields[i]->is_dynamic) {
1546                                 char *str_val = (char *)(long)val;
1547
1548                                 data_size += strlen(str_val) + 1;
1549                         }
1550                 }
1551
1552                 va_end(args);
1553         }
1554
1555         ret = __synth_event_trace_start(file, &state, data_size);
1556         if (ret)
1557                 return ret;
1558
1559         if (n_vals != state.event->n_fields) {
1560                 ret = -EINVAL;
1561                 goto out;
1562         }
1563
1564         data_size = 0;
1565
1566         va_start(args, n_vals);
1567         for (i = 0, n_u64 = 0; i < state.event->n_fields; i++) {
1568                 u64 val;
1569
1570                 val = va_arg(args, u64);
1571
1572                 if (state.event->fields[i]->is_string) {
1573                         char *str_val = (char *)(long)val;
1574
1575                         len = trace_string(state.entry, state.event, str_val,
1576                                            state.event->fields[i]->is_dynamic,
1577                                            data_size, &n_u64);
1578                         data_size += len; /* only dynamic string increments */
1579                 } else {
1580                         struct synth_field *field = state.event->fields[i];
1581
1582                         switch (field->size) {
1583                         case 1:
1584                                 *(u8 *)&state.entry->fields[n_u64] = (u8)val;
1585                                 break;
1586
1587                         case 2:
1588                                 *(u16 *)&state.entry->fields[n_u64] = (u16)val;
1589                                 break;
1590
1591                         case 4:
1592                                 *(u32 *)&state.entry->fields[n_u64] = (u32)val;
1593                                 break;
1594
1595                         default:
1596                                 state.entry->fields[n_u64] = val;
1597                                 break;
1598                         }
1599                         n_u64++;
1600                 }
1601         }
1602         va_end(args);
1603 out:
1604         __synth_event_trace_end(&state);
1605
1606         return ret;
1607 }
1608 EXPORT_SYMBOL_GPL(synth_event_trace);
1609
1610 /**
1611  * synth_event_trace_array - Trace a synthetic event from an array
1612  * @file: The trace_event_file representing the synthetic event
1613  * @vals: Array of values
1614  * @n_vals: The number of values in vals
1615  *
1616  * Trace a synthetic event using the values passed in as 'vals'.
1617  *
1618  * The 'vals' array is just an array of 'n_vals' u64.  The number of
1619  * vals must match the number of field in the synthetic event, and
1620  * must be in the same order as the synthetic event fields.
1621  *
1622  * All vals should be cast to u64, and string vals are just pointers
1623  * to strings, cast to u64.  Strings will be copied into space
1624  * reserved in the event for the string, using these pointers.
1625  *
1626  * Return: 0 on success, err otherwise.
1627  */
1628 int synth_event_trace_array(struct trace_event_file *file, u64 *vals,
1629                             unsigned int n_vals)
1630 {
1631         unsigned int i, n_u64, field_pos, len, data_size = 0;
1632         struct synth_event_trace_state state;
1633         char *str_val;
1634         int ret;
1635
1636         ret = __synth_event_trace_init(file, &state);
1637         if (ret) {
1638                 if (ret == -ENOENT)
1639                         ret = 0; /* just disabled, not really an error */
1640                 return ret;
1641         }
1642
1643         if (state.event->n_dynamic_fields) {
1644                 for (i = 0; i < state.event->n_dynamic_fields; i++) {
1645                         field_pos = state.event->dynamic_fields[i]->field_pos;
1646                         str_val = (char *)(long)vals[field_pos];
1647                         len = strlen(str_val) + 1;
1648                         data_size += len;
1649                 }
1650         }
1651
1652         ret = __synth_event_trace_start(file, &state, data_size);
1653         if (ret)
1654                 return ret;
1655
1656         if (n_vals != state.event->n_fields) {
1657                 ret = -EINVAL;
1658                 goto out;
1659         }
1660
1661         data_size = 0;
1662
1663         for (i = 0, n_u64 = 0; i < state.event->n_fields; i++) {
1664                 if (state.event->fields[i]->is_string) {
1665                         char *str_val = (char *)(long)vals[i];
1666
1667                         len = trace_string(state.entry, state.event, str_val,
1668                                            state.event->fields[i]->is_dynamic,
1669                                            data_size, &n_u64);
1670                         data_size += len; /* only dynamic string increments */
1671                 } else {
1672                         struct synth_field *field = state.event->fields[i];
1673                         u64 val = vals[i];
1674
1675                         switch (field->size) {
1676                         case 1:
1677                                 *(u8 *)&state.entry->fields[n_u64] = (u8)val;
1678                                 break;
1679
1680                         case 2:
1681                                 *(u16 *)&state.entry->fields[n_u64] = (u16)val;
1682                                 break;
1683
1684                         case 4:
1685                                 *(u32 *)&state.entry->fields[n_u64] = (u32)val;
1686                                 break;
1687
1688                         default:
1689                                 state.entry->fields[n_u64] = val;
1690                                 break;
1691                         }
1692                         n_u64++;
1693                 }
1694         }
1695 out:
1696         __synth_event_trace_end(&state);
1697
1698         return ret;
1699 }
1700 EXPORT_SYMBOL_GPL(synth_event_trace_array);
1701
1702 /**
1703  * synth_event_trace_start - Start piecewise synthetic event trace
1704  * @file: The trace_event_file representing the synthetic event
1705  * @trace_state: A pointer to object tracking the piecewise trace state
1706  *
1707  * Start the trace of a synthetic event field-by-field rather than all
1708  * at once.
1709  *
1710  * This function 'opens' an event trace, which means space is reserved
1711  * for the event in the trace buffer, after which the event's
1712  * individual field values can be set through either
1713  * synth_event_add_next_val() or synth_event_add_val().
1714  *
1715  * A pointer to a trace_state object is passed in, which will keep
1716  * track of the current event trace state until the event trace is
1717  * closed (and the event finally traced) using
1718  * synth_event_trace_end().
1719  *
1720  * Note that synth_event_trace_end() must be called after all values
1721  * have been added for each event trace, regardless of whether adding
1722  * all field values succeeded or not.
1723  *
1724  * Note also that for a given event trace, all fields must be added
1725  * using either synth_event_add_next_val() or synth_event_add_val()
1726  * but not both together or interleaved.
1727  *
1728  * Return: 0 on success, err otherwise.
1729  */
1730 int synth_event_trace_start(struct trace_event_file *file,
1731                             struct synth_event_trace_state *trace_state)
1732 {
1733         int ret;
1734
1735         if (!trace_state)
1736                 return -EINVAL;
1737
1738         ret = __synth_event_trace_init(file, trace_state);
1739         if (ret) {
1740                 if (ret == -ENOENT)
1741                         ret = 0; /* just disabled, not really an error */
1742                 return ret;
1743         }
1744
1745         if (trace_state->event->n_dynamic_fields)
1746                 return -ENOTSUPP;
1747
1748         ret = __synth_event_trace_start(file, trace_state, 0);
1749
1750         return ret;
1751 }
1752 EXPORT_SYMBOL_GPL(synth_event_trace_start);
1753
1754 static int __synth_event_add_val(const char *field_name, u64 val,
1755                                  struct synth_event_trace_state *trace_state)
1756 {
1757         struct synth_field *field = NULL;
1758         struct synth_trace_event *entry;
1759         struct synth_event *event;
1760         int i, ret = 0;
1761
1762         if (!trace_state) {
1763                 ret = -EINVAL;
1764                 goto out;
1765         }
1766
1767         /* can't mix add_next_synth_val() with add_synth_val() */
1768         if (field_name) {
1769                 if (trace_state->add_next) {
1770                         ret = -EINVAL;
1771                         goto out;
1772                 }
1773                 trace_state->add_name = true;
1774         } else {
1775                 if (trace_state->add_name) {
1776                         ret = -EINVAL;
1777                         goto out;
1778                 }
1779                 trace_state->add_next = true;
1780         }
1781
1782         if (trace_state->disabled)
1783                 goto out;
1784
1785         event = trace_state->event;
1786         if (trace_state->add_name) {
1787                 for (i = 0; i < event->n_fields; i++) {
1788                         field = event->fields[i];
1789                         if (strcmp(field->name, field_name) == 0)
1790                                 break;
1791                 }
1792                 if (!field) {
1793                         ret = -EINVAL;
1794                         goto out;
1795                 }
1796         } else {
1797                 if (trace_state->cur_field >= event->n_fields) {
1798                         ret = -EINVAL;
1799                         goto out;
1800                 }
1801                 field = event->fields[trace_state->cur_field++];
1802         }
1803
1804         entry = trace_state->entry;
1805         if (field->is_string) {
1806                 char *str_val = (char *)(long)val;
1807                 char *str_field;
1808
1809                 if (field->is_dynamic) { /* add_val can't do dynamic strings */
1810                         ret = -EINVAL;
1811                         goto out;
1812                 }
1813
1814                 if (!str_val) {
1815                         ret = -EINVAL;
1816                         goto out;
1817                 }
1818
1819                 str_field = (char *)&entry->fields[field->offset];
1820                 strscpy(str_field, str_val, STR_VAR_LEN_MAX);
1821         } else {
1822                 switch (field->size) {
1823                 case 1:
1824                         *(u8 *)&trace_state->entry->fields[field->offset] = (u8)val;
1825                         break;
1826
1827                 case 2:
1828                         *(u16 *)&trace_state->entry->fields[field->offset] = (u16)val;
1829                         break;
1830
1831                 case 4:
1832                         *(u32 *)&trace_state->entry->fields[field->offset] = (u32)val;
1833                         break;
1834
1835                 default:
1836                         trace_state->entry->fields[field->offset] = val;
1837                         break;
1838                 }
1839         }
1840  out:
1841         return ret;
1842 }
1843
1844 /**
1845  * synth_event_add_next_val - Add the next field's value to an open synth trace
1846  * @val: The value to set the next field to
1847  * @trace_state: A pointer to object tracking the piecewise trace state
1848  *
1849  * Set the value of the next field in an event that's been opened by
1850  * synth_event_trace_start().
1851  *
1852  * The val param should be the value cast to u64.  If the value points
1853  * to a string, the val param should be a char * cast to u64.
1854  *
1855  * This function assumes all the fields in an event are to be set one
1856  * after another - successive calls to this function are made, one for
1857  * each field, in the order of the fields in the event, until all
1858  * fields have been set.  If you'd rather set each field individually
1859  * without regard to ordering, synth_event_add_val() can be used
1860  * instead.
1861  *
1862  * Note however that synth_event_add_next_val() and
1863  * synth_event_add_val() can't be intermixed for a given event trace -
1864  * one or the other but not both can be used at the same time.
1865  *
1866  * Note also that synth_event_trace_end() must be called after all
1867  * values have been added for each event trace, regardless of whether
1868  * adding all field values succeeded or not.
1869  *
1870  * Return: 0 on success, err otherwise.
1871  */
1872 int synth_event_add_next_val(u64 val,
1873                              struct synth_event_trace_state *trace_state)
1874 {
1875         return __synth_event_add_val(NULL, val, trace_state);
1876 }
1877 EXPORT_SYMBOL_GPL(synth_event_add_next_val);
1878
1879 /**
1880  * synth_event_add_val - Add a named field's value to an open synth trace
1881  * @field_name: The name of the synthetic event field value to set
1882  * @val: The value to set the next field to
1883  * @trace_state: A pointer to object tracking the piecewise trace state
1884  *
1885  * Set the value of the named field in an event that's been opened by
1886  * synth_event_trace_start().
1887  *
1888  * The val param should be the value cast to u64.  If the value points
1889  * to a string, the val param should be a char * cast to u64.
1890  *
1891  * This function looks up the field name, and if found, sets the field
1892  * to the specified value.  This lookup makes this function more
1893  * expensive than synth_event_add_next_val(), so use that or the
1894  * none-piecewise synth_event_trace() instead if efficiency is more
1895  * important.
1896  *
1897  * Note however that synth_event_add_next_val() and
1898  * synth_event_add_val() can't be intermixed for a given event trace -
1899  * one or the other but not both can be used at the same time.
1900  *
1901  * Note also that synth_event_trace_end() must be called after all
1902  * values have been added for each event trace, regardless of whether
1903  * adding all field values succeeded or not.
1904  *
1905  * Return: 0 on success, err otherwise.
1906  */
1907 int synth_event_add_val(const char *field_name, u64 val,
1908                         struct synth_event_trace_state *trace_state)
1909 {
1910         return __synth_event_add_val(field_name, val, trace_state);
1911 }
1912 EXPORT_SYMBOL_GPL(synth_event_add_val);
1913
1914 /**
1915  * synth_event_trace_end - End piecewise synthetic event trace
1916  * @trace_state: A pointer to object tracking the piecewise trace state
1917  *
1918  * End the trace of a synthetic event opened by
1919  * synth_event_trace__start().
1920  *
1921  * This function 'closes' an event trace, which basically means that
1922  * it commits the reserved event and cleans up other loose ends.
1923  *
1924  * A pointer to a trace_state object is passed in, which will keep
1925  * track of the current event trace state opened with
1926  * synth_event_trace_start().
1927  *
1928  * Note that this function must be called after all values have been
1929  * added for each event trace, regardless of whether adding all field
1930  * values succeeded or not.
1931  *
1932  * Return: 0 on success, err otherwise.
1933  */
1934 int synth_event_trace_end(struct synth_event_trace_state *trace_state)
1935 {
1936         if (!trace_state)
1937                 return -EINVAL;
1938
1939         __synth_event_trace_end(trace_state);
1940
1941         return 0;
1942 }
1943 EXPORT_SYMBOL_GPL(synth_event_trace_end);
1944
1945 static int create_synth_event(int argc, const char **argv)
1946 {
1947         const char *name = argv[0];
1948         int len;
1949
1950         if (name[0] != 's' || name[1] != ':')
1951                 return -ECANCELED;
1952         name += 2;
1953
1954         /* This interface accepts group name prefix */
1955         if (strchr(name, '/')) {
1956                 len = str_has_prefix(name, SYNTH_SYSTEM "/");
1957                 if (len == 0)
1958                         return -EINVAL;
1959                 name += len;
1960         }
1961         return __create_synth_event(argc - 1, name, argv + 1);
1962 }
1963
1964 static int synth_event_release(struct dyn_event *ev)
1965 {
1966         struct synth_event *event = to_synth_event(ev);
1967         int ret;
1968
1969         if (event->ref)
1970                 return -EBUSY;
1971
1972         ret = unregister_synth_event(event);
1973         if (ret)
1974                 return ret;
1975
1976         dyn_event_remove(ev);
1977         free_synth_event(event);
1978         return 0;
1979 }
1980
1981 static int __synth_event_show(struct seq_file *m, struct synth_event *event)
1982 {
1983         struct synth_field *field;
1984         unsigned int i;
1985         char *type, *t;
1986
1987         seq_printf(m, "%s\t", event->name);
1988
1989         for (i = 0; i < event->n_fields; i++) {
1990                 field = event->fields[i];
1991
1992                 type = field->type;
1993                 t = strstr(type, "__data_loc");
1994                 if (t) { /* __data_loc belongs in format but not event desc */
1995                         t += sizeof("__data_loc");
1996                         type = t;
1997                 }
1998
1999                 /* parameter values */
2000                 seq_printf(m, "%s %s%s", type, field->name,
2001                            i == event->n_fields - 1 ? "" : "; ");
2002         }
2003
2004         seq_putc(m, '\n');
2005
2006         return 0;
2007 }
2008
2009 static int synth_event_show(struct seq_file *m, struct dyn_event *ev)
2010 {
2011         struct synth_event *event = to_synth_event(ev);
2012
2013         seq_printf(m, "s:%s/", event->class.system);
2014
2015         return __synth_event_show(m, event);
2016 }
2017
2018 static int synth_events_seq_show(struct seq_file *m, void *v)
2019 {
2020         struct dyn_event *ev = v;
2021
2022         if (!is_synth_event(ev))
2023                 return 0;
2024
2025         return __synth_event_show(m, to_synth_event(ev));
2026 }
2027
2028 static const struct seq_operations synth_events_seq_op = {
2029         .start  = dyn_event_seq_start,
2030         .next   = dyn_event_seq_next,
2031         .stop   = dyn_event_seq_stop,
2032         .show   = synth_events_seq_show,
2033 };
2034
2035 static int synth_events_open(struct inode *inode, struct file *file)
2036 {
2037         int ret;
2038
2039         ret = security_locked_down(LOCKDOWN_TRACEFS);
2040         if (ret)
2041                 return ret;
2042
2043         if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
2044                 ret = dyn_events_release_all(&synth_event_ops);
2045                 if (ret < 0)
2046                         return ret;
2047         }
2048
2049         return seq_open(file, &synth_events_seq_op);
2050 }
2051
2052 static ssize_t synth_events_write(struct file *file,
2053                                   const char __user *buffer,
2054                                   size_t count, loff_t *ppos)
2055 {
2056         return trace_parse_run_command(file, buffer, count, ppos,
2057                                        create_or_delete_synth_event);
2058 }
2059
2060 static const struct file_operations synth_events_fops = {
2061         .open           = synth_events_open,
2062         .write          = synth_events_write,
2063         .read           = seq_read,
2064         .llseek         = seq_lseek,
2065         .release        = seq_release,
2066 };
2067
2068 /*
2069  * Register dynevent at core_initcall. This allows kernel to setup kprobe
2070  * events in postcore_initcall without tracefs.
2071  */
2072 static __init int trace_events_synth_init_early(void)
2073 {
2074         int err = 0;
2075
2076         err = dyn_event_register(&synth_event_ops);
2077         if (err)
2078                 pr_warn("Could not register synth_event_ops\n");
2079
2080         return err;
2081 }
2082 core_initcall(trace_events_synth_init_early);
2083
2084 static __init int trace_events_synth_init(void)
2085 {
2086         struct dentry *entry = NULL;
2087         int err = 0;
2088         err = tracing_init_dentry();
2089         if (err)
2090                 goto err;
2091
2092         entry = tracefs_create_file("synthetic_events", 0644, NULL,
2093                                     NULL, &synth_events_fops);
2094         if (!entry) {
2095                 err = -ENODEV;
2096                 goto err;
2097         }
2098
2099         return err;
2100  err:
2101         pr_warn("Could not create tracefs 'synthetic_events' entry\n");
2102
2103         return err;
2104 }
2105
2106 fs_initcall(trace_events_synth_init);