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