]> asedeno.scripts.mit.edu Git - linux.git/blob - kernel/trace/trace_events_hist.c
117a1202a6b96ef9b664ac2085135afda3e568dc
[linux.git] / kernel / trace / trace_events_hist.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * trace_events_hist - trace event hist triggers
4  *
5  * Copyright (C) 2015 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 "tracing_map.h"
22 #include "trace.h"
23 #include "trace_dynevent.h"
24
25 #define SYNTH_SYSTEM            "synthetic"
26 #define SYNTH_FIELDS_MAX        32
27
28 #define STR_VAR_LEN_MAX         32 /* must be multiple of sizeof(u64) */
29
30 #define ERRORS                                                          \
31         C(NONE,                 "No error"),                            \
32         C(DUPLICATE_VAR,        "Variable already defined"),            \
33         C(VAR_NOT_UNIQUE,       "Variable name not unique, need to use fully qualified name (subsys.event.var) for variable"), \
34         C(TOO_MANY_VARS,        "Too many variables defined"),          \
35         C(MALFORMED_ASSIGNMENT, "Malformed assignment"),                \
36         C(NAMED_MISMATCH,       "Named hist trigger doesn't match existing named trigger (includes variables)"), \
37         C(TRIGGER_EEXIST,       "Hist trigger already exists"),         \
38         C(TRIGGER_ENOENT_CLEAR, "Can't clear or continue a nonexistent hist trigger"), \
39         C(SET_CLOCK_FAIL,       "Couldn't set trace_clock"),            \
40         C(BAD_FIELD_MODIFIER,   "Invalid field modifier"),              \
41         C(TOO_MANY_SUBEXPR,     "Too many subexpressions (3 max)"),     \
42         C(TIMESTAMP_MISMATCH,   "Timestamp units in expression don't match"), \
43         C(TOO_MANY_FIELD_VARS,  "Too many field variables defined"),    \
44         C(EVENT_FILE_NOT_FOUND, "Event file not found"),                \
45         C(HIST_NOT_FOUND,       "Matching event histogram not found"),  \
46         C(HIST_CREATE_FAIL,     "Couldn't create histogram for field"), \
47         C(SYNTH_VAR_NOT_FOUND,  "Couldn't find synthetic variable"),    \
48         C(SYNTH_EVENT_NOT_FOUND,"Couldn't find synthetic event"),       \
49         C(SYNTH_TYPE_MISMATCH,  "Param type doesn't match synthetic event field type"), \
50         C(SYNTH_COUNT_MISMATCH, "Param count doesn't match synthetic event field count"), \
51         C(FIELD_VAR_PARSE_FAIL, "Couldn't parse field variable"),       \
52         C(VAR_CREATE_FIND_FAIL, "Couldn't create or find variable"),    \
53         C(ONX_NOT_VAR,          "For onmax(x) or onchange(x), x must be a variable"), \
54         C(ONX_VAR_NOT_FOUND,    "Couldn't find onmax or onchange variable"), \
55         C(ONX_VAR_CREATE_FAIL,  "Couldn't create onmax or onchange variable"), \
56         C(FIELD_VAR_CREATE_FAIL,"Couldn't create field variable"),      \
57         C(TOO_MANY_PARAMS,      "Too many action params"),              \
58         C(PARAM_NOT_FOUND,      "Couldn't find param"),                 \
59         C(INVALID_PARAM,        "Invalid action param"),                \
60         C(ACTION_NOT_FOUND,     "No action found"),                     \
61         C(NO_SAVE_PARAMS,       "No params found for save()"),          \
62         C(TOO_MANY_SAVE_ACTIONS,"Can't have more than one save() action per hist"), \
63         C(ACTION_MISMATCH,      "Handler doesn't support action"),      \
64         C(NO_CLOSING_PAREN,     "No closing paren found"),              \
65         C(SUBSYS_NOT_FOUND,     "Missing subsystem"),                   \
66         C(INVALID_SUBSYS_EVENT, "Invalid subsystem or event name"),     \
67         C(INVALID_REF_KEY,      "Using variable references in keys not supported"), \
68         C(VAR_NOT_FOUND,        "Couldn't find variable"),              \
69         C(FIELD_NOT_FOUND,      "Couldn't find field"),
70
71 #undef C
72 #define C(a, b)         HIST_ERR_##a
73
74 enum { ERRORS };
75
76 #undef C
77 #define C(a, b)         b
78
79 static const char *err_text[] = { ERRORS };
80
81 struct hist_field;
82
83 typedef u64 (*hist_field_fn_t) (struct hist_field *field,
84                                 struct tracing_map_elt *elt,
85                                 struct ring_buffer_event *rbe,
86                                 void *event);
87
88 #define HIST_FIELD_OPERANDS_MAX 2
89 #define HIST_FIELDS_MAX         (TRACING_MAP_FIELDS_MAX + TRACING_MAP_VARS_MAX)
90 #define HIST_ACTIONS_MAX        8
91
92 enum field_op_id {
93         FIELD_OP_NONE,
94         FIELD_OP_PLUS,
95         FIELD_OP_MINUS,
96         FIELD_OP_UNARY_MINUS,
97 };
98
99 /*
100  * A hist_var (histogram variable) contains variable information for
101  * hist_fields having the HIST_FIELD_FL_VAR or HIST_FIELD_FL_VAR_REF
102  * flag set.  A hist_var has a variable name e.g. ts0, and is
103  * associated with a given histogram trigger, as specified by
104  * hist_data.  The hist_var idx is the unique index assigned to the
105  * variable by the hist trigger's tracing_map.  The idx is what is
106  * used to set a variable's value and, by a variable reference, to
107  * retrieve it.
108  */
109 struct hist_var {
110         char                            *name;
111         struct hist_trigger_data        *hist_data;
112         unsigned int                    idx;
113 };
114
115 struct hist_field {
116         struct ftrace_event_field       *field;
117         unsigned long                   flags;
118         hist_field_fn_t                 fn;
119         unsigned int                    size;
120         unsigned int                    offset;
121         unsigned int                    is_signed;
122         const char                      *type;
123         struct hist_field               *operands[HIST_FIELD_OPERANDS_MAX];
124         struct hist_trigger_data        *hist_data;
125
126         /*
127          * Variable fields contain variable-specific info in var.
128          */
129         struct hist_var                 var;
130         enum field_op_id                operator;
131         char                            *system;
132         char                            *event_name;
133
134         /*
135          * The name field is used for EXPR and VAR_REF fields.  VAR
136          * fields contain the variable name in var.name.
137          */
138         char                            *name;
139
140         /*
141          * When a histogram trigger is hit, if it has any references
142          * to variables, the values of those variables are collected
143          * into a var_ref_vals array by resolve_var_refs().  The
144          * current value of each variable is read from the tracing_map
145          * using the hist field's hist_var.idx and entered into the
146          * var_ref_idx entry i.e. var_ref_vals[var_ref_idx].
147          */
148         unsigned int                    var_ref_idx;
149         bool                            read_once;
150 };
151
152 static u64 hist_field_none(struct hist_field *field,
153                            struct tracing_map_elt *elt,
154                            struct ring_buffer_event *rbe,
155                            void *event)
156 {
157         return 0;
158 }
159
160 static u64 hist_field_counter(struct hist_field *field,
161                               struct tracing_map_elt *elt,
162                               struct ring_buffer_event *rbe,
163                               void *event)
164 {
165         return 1;
166 }
167
168 static u64 hist_field_string(struct hist_field *hist_field,
169                              struct tracing_map_elt *elt,
170                              struct ring_buffer_event *rbe,
171                              void *event)
172 {
173         char *addr = (char *)(event + hist_field->field->offset);
174
175         return (u64)(unsigned long)addr;
176 }
177
178 static u64 hist_field_dynstring(struct hist_field *hist_field,
179                                 struct tracing_map_elt *elt,
180                                 struct ring_buffer_event *rbe,
181                                 void *event)
182 {
183         u32 str_item = *(u32 *)(event + hist_field->field->offset);
184         int str_loc = str_item & 0xffff;
185         char *addr = (char *)(event + str_loc);
186
187         return (u64)(unsigned long)addr;
188 }
189
190 static u64 hist_field_pstring(struct hist_field *hist_field,
191                               struct tracing_map_elt *elt,
192                               struct ring_buffer_event *rbe,
193                               void *event)
194 {
195         char **addr = (char **)(event + hist_field->field->offset);
196
197         return (u64)(unsigned long)*addr;
198 }
199
200 static u64 hist_field_log2(struct hist_field *hist_field,
201                            struct tracing_map_elt *elt,
202                            struct ring_buffer_event *rbe,
203                            void *event)
204 {
205         struct hist_field *operand = hist_field->operands[0];
206
207         u64 val = operand->fn(operand, elt, rbe, event);
208
209         return (u64) ilog2(roundup_pow_of_two(val));
210 }
211
212 static u64 hist_field_plus(struct hist_field *hist_field,
213                            struct tracing_map_elt *elt,
214                            struct ring_buffer_event *rbe,
215                            void *event)
216 {
217         struct hist_field *operand1 = hist_field->operands[0];
218         struct hist_field *operand2 = hist_field->operands[1];
219
220         u64 val1 = operand1->fn(operand1, elt, rbe, event);
221         u64 val2 = operand2->fn(operand2, elt, rbe, event);
222
223         return val1 + val2;
224 }
225
226 static u64 hist_field_minus(struct hist_field *hist_field,
227                             struct tracing_map_elt *elt,
228                             struct ring_buffer_event *rbe,
229                             void *event)
230 {
231         struct hist_field *operand1 = hist_field->operands[0];
232         struct hist_field *operand2 = hist_field->operands[1];
233
234         u64 val1 = operand1->fn(operand1, elt, rbe, event);
235         u64 val2 = operand2->fn(operand2, elt, rbe, event);
236
237         return val1 - val2;
238 }
239
240 static u64 hist_field_unary_minus(struct hist_field *hist_field,
241                                   struct tracing_map_elt *elt,
242                                   struct ring_buffer_event *rbe,
243                                   void *event)
244 {
245         struct hist_field *operand = hist_field->operands[0];
246
247         s64 sval = (s64)operand->fn(operand, elt, rbe, event);
248         u64 val = (u64)-sval;
249
250         return val;
251 }
252
253 #define DEFINE_HIST_FIELD_FN(type)                                      \
254         static u64 hist_field_##type(struct hist_field *hist_field,     \
255                                      struct tracing_map_elt *elt,       \
256                                      struct ring_buffer_event *rbe,     \
257                                      void *event)                       \
258 {                                                                       \
259         type *addr = (type *)(event + hist_field->field->offset);       \
260                                                                         \
261         return (u64)(unsigned long)*addr;                               \
262 }
263
264 DEFINE_HIST_FIELD_FN(s64);
265 DEFINE_HIST_FIELD_FN(u64);
266 DEFINE_HIST_FIELD_FN(s32);
267 DEFINE_HIST_FIELD_FN(u32);
268 DEFINE_HIST_FIELD_FN(s16);
269 DEFINE_HIST_FIELD_FN(u16);
270 DEFINE_HIST_FIELD_FN(s8);
271 DEFINE_HIST_FIELD_FN(u8);
272
273 #define for_each_hist_field(i, hist_data)       \
274         for ((i) = 0; (i) < (hist_data)->n_fields; (i)++)
275
276 #define for_each_hist_val_field(i, hist_data)   \
277         for ((i) = 0; (i) < (hist_data)->n_vals; (i)++)
278
279 #define for_each_hist_key_field(i, hist_data)   \
280         for ((i) = (hist_data)->n_vals; (i) < (hist_data)->n_fields; (i)++)
281
282 #define HIST_STACKTRACE_DEPTH   16
283 #define HIST_STACKTRACE_SIZE    (HIST_STACKTRACE_DEPTH * sizeof(unsigned long))
284 #define HIST_STACKTRACE_SKIP    5
285
286 #define HITCOUNT_IDX            0
287 #define HIST_KEY_SIZE_MAX       (MAX_FILTER_STR_VAL + HIST_STACKTRACE_SIZE)
288
289 enum hist_field_flags {
290         HIST_FIELD_FL_HITCOUNT          = 1 << 0,
291         HIST_FIELD_FL_KEY               = 1 << 1,
292         HIST_FIELD_FL_STRING            = 1 << 2,
293         HIST_FIELD_FL_HEX               = 1 << 3,
294         HIST_FIELD_FL_SYM               = 1 << 4,
295         HIST_FIELD_FL_SYM_OFFSET        = 1 << 5,
296         HIST_FIELD_FL_EXECNAME          = 1 << 6,
297         HIST_FIELD_FL_SYSCALL           = 1 << 7,
298         HIST_FIELD_FL_STACKTRACE        = 1 << 8,
299         HIST_FIELD_FL_LOG2              = 1 << 9,
300         HIST_FIELD_FL_TIMESTAMP         = 1 << 10,
301         HIST_FIELD_FL_TIMESTAMP_USECS   = 1 << 11,
302         HIST_FIELD_FL_VAR               = 1 << 12,
303         HIST_FIELD_FL_EXPR              = 1 << 13,
304         HIST_FIELD_FL_VAR_REF           = 1 << 14,
305         HIST_FIELD_FL_CPU               = 1 << 15,
306         HIST_FIELD_FL_ALIAS             = 1 << 16,
307 };
308
309 struct var_defs {
310         unsigned int    n_vars;
311         char            *name[TRACING_MAP_VARS_MAX];
312         char            *expr[TRACING_MAP_VARS_MAX];
313 };
314
315 struct hist_trigger_attrs {
316         char            *keys_str;
317         char            *vals_str;
318         char            *sort_key_str;
319         char            *name;
320         char            *clock;
321         bool            pause;
322         bool            cont;
323         bool            clear;
324         bool            ts_in_usecs;
325         unsigned int    map_bits;
326
327         char            *assignment_str[TRACING_MAP_VARS_MAX];
328         unsigned int    n_assignments;
329
330         char            *action_str[HIST_ACTIONS_MAX];
331         unsigned int    n_actions;
332
333         struct var_defs var_defs;
334 };
335
336 struct field_var {
337         struct hist_field       *var;
338         struct hist_field       *val;
339 };
340
341 struct field_var_hist {
342         struct hist_trigger_data        *hist_data;
343         char                            *cmd;
344 };
345
346 struct hist_trigger_data {
347         struct hist_field               *fields[HIST_FIELDS_MAX];
348         unsigned int                    n_vals;
349         unsigned int                    n_keys;
350         unsigned int                    n_fields;
351         unsigned int                    n_vars;
352         unsigned int                    key_size;
353         struct tracing_map_sort_key     sort_keys[TRACING_MAP_SORT_KEYS_MAX];
354         unsigned int                    n_sort_keys;
355         struct trace_event_file         *event_file;
356         struct hist_trigger_attrs       *attrs;
357         struct tracing_map              *map;
358         bool                            enable_timestamps;
359         bool                            remove;
360         struct hist_field               *var_refs[TRACING_MAP_VARS_MAX];
361         unsigned int                    n_var_refs;
362
363         struct action_data              *actions[HIST_ACTIONS_MAX];
364         unsigned int                    n_actions;
365
366         struct field_var                *field_vars[SYNTH_FIELDS_MAX];
367         unsigned int                    n_field_vars;
368         unsigned int                    n_field_var_str;
369         struct field_var_hist           *field_var_hists[SYNTH_FIELDS_MAX];
370         unsigned int                    n_field_var_hists;
371
372         struct field_var                *save_vars[SYNTH_FIELDS_MAX];
373         unsigned int                    n_save_vars;
374         unsigned int                    n_save_var_str;
375 };
376
377 static int synth_event_create(int argc, const char **argv);
378 static int synth_event_show(struct seq_file *m, struct dyn_event *ev);
379 static int synth_event_release(struct dyn_event *ev);
380 static bool synth_event_is_busy(struct dyn_event *ev);
381 static bool synth_event_match(const char *system, const char *event,
382                         int argc, const char **argv, struct dyn_event *ev);
383
384 static struct dyn_event_operations synth_event_ops = {
385         .create = synth_event_create,
386         .show = synth_event_show,
387         .is_busy = synth_event_is_busy,
388         .free = synth_event_release,
389         .match = synth_event_match,
390 };
391
392 struct synth_field {
393         char *type;
394         char *name;
395         size_t size;
396         bool is_signed;
397         bool is_string;
398 };
399
400 struct synth_event {
401         struct dyn_event                        devent;
402         int                                     ref;
403         char                                    *name;
404         struct synth_field                      **fields;
405         unsigned int                            n_fields;
406         unsigned int                            n_u64;
407         struct trace_event_class                class;
408         struct trace_event_call                 call;
409         struct tracepoint                       *tp;
410 };
411
412 static bool is_synth_event(struct dyn_event *ev)
413 {
414         return ev->ops == &synth_event_ops;
415 }
416
417 static struct synth_event *to_synth_event(struct dyn_event *ev)
418 {
419         return container_of(ev, struct synth_event, devent);
420 }
421
422 static bool synth_event_is_busy(struct dyn_event *ev)
423 {
424         struct synth_event *event = to_synth_event(ev);
425
426         return event->ref != 0;
427 }
428
429 static bool synth_event_match(const char *system, const char *event,
430                         int argc, const char **argv, struct dyn_event *ev)
431 {
432         struct synth_event *sev = to_synth_event(ev);
433
434         return strcmp(sev->name, event) == 0 &&
435                 (!system || strcmp(system, SYNTH_SYSTEM) == 0);
436 }
437
438 struct action_data;
439
440 typedef void (*action_fn_t) (struct hist_trigger_data *hist_data,
441                              struct tracing_map_elt *elt, void *rec,
442                              struct ring_buffer_event *rbe, void *key,
443                              struct action_data *data, u64 *var_ref_vals);
444
445 typedef bool (*check_track_val_fn_t) (u64 track_val, u64 var_val);
446
447 enum handler_id {
448         HANDLER_ONMATCH = 1,
449         HANDLER_ONMAX,
450         HANDLER_ONCHANGE,
451 };
452
453 enum action_id {
454         ACTION_SAVE = 1,
455         ACTION_TRACE,
456         ACTION_SNAPSHOT,
457 };
458
459 struct action_data {
460         enum handler_id         handler;
461         enum action_id          action;
462         char                    *action_name;
463         action_fn_t             fn;
464
465         unsigned int            n_params;
466         char                    *params[SYNTH_FIELDS_MAX];
467
468         /*
469          * When a histogram trigger is hit, the values of any
470          * references to variables, including variables being passed
471          * as parameters to synthetic events, are collected into a
472          * var_ref_vals array.  This var_ref_idx is the index of the
473          * first param in the array to be passed to the synthetic
474          * event invocation.
475          */
476         unsigned int            var_ref_idx;
477         struct synth_event      *synth_event;
478         bool                    use_trace_keyword;
479         char                    *synth_event_name;
480
481         union {
482                 struct {
483                         char                    *event;
484                         char                    *event_system;
485                 } match_data;
486
487                 struct {
488                         /*
489                          * var_str contains the $-unstripped variable
490                          * name referenced by var_ref, and used when
491                          * printing the action.  Because var_ref
492                          * creation is deferred to create_actions(),
493                          * we need a per-action way to save it until
494                          * then, thus var_str.
495                          */
496                         char                    *var_str;
497
498                         /*
499                          * var_ref refers to the variable being
500                          * tracked e.g onmax($var).
501                          */
502                         struct hist_field       *var_ref;
503
504                         /*
505                          * track_var contains the 'invisible' tracking
506                          * variable created to keep the current
507                          * e.g. max value.
508                          */
509                         struct hist_field       *track_var;
510
511                         check_track_val_fn_t    check_val;
512                         action_fn_t             save_data;
513                 } track_data;
514         };
515 };
516
517 struct track_data {
518         u64                             track_val;
519         bool                            updated;
520
521         unsigned int                    key_len;
522         void                            *key;
523         struct tracing_map_elt          elt;
524
525         struct action_data              *action_data;
526         struct hist_trigger_data        *hist_data;
527 };
528
529 struct hist_elt_data {
530         char *comm;
531         u64 *var_ref_vals;
532         char *field_var_str[SYNTH_FIELDS_MAX];
533 };
534
535 struct snapshot_context {
536         struct tracing_map_elt  *elt;
537         void                    *key;
538 };
539
540 static void track_data_free(struct track_data *track_data)
541 {
542         struct hist_elt_data *elt_data;
543
544         if (!track_data)
545                 return;
546
547         kfree(track_data->key);
548
549         elt_data = track_data->elt.private_data;
550         if (elt_data) {
551                 kfree(elt_data->comm);
552                 kfree(elt_data);
553         }
554
555         kfree(track_data);
556 }
557
558 static struct track_data *track_data_alloc(unsigned int key_len,
559                                            struct action_data *action_data,
560                                            struct hist_trigger_data *hist_data)
561 {
562         struct track_data *data = kzalloc(sizeof(*data), GFP_KERNEL);
563         struct hist_elt_data *elt_data;
564
565         if (!data)
566                 return ERR_PTR(-ENOMEM);
567
568         data->key = kzalloc(key_len, GFP_KERNEL);
569         if (!data->key) {
570                 track_data_free(data);
571                 return ERR_PTR(-ENOMEM);
572         }
573
574         data->key_len = key_len;
575         data->action_data = action_data;
576         data->hist_data = hist_data;
577
578         elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
579         if (!elt_data) {
580                 track_data_free(data);
581                 return ERR_PTR(-ENOMEM);
582         }
583         data->elt.private_data = elt_data;
584
585         elt_data->comm = kzalloc(TASK_COMM_LEN, GFP_KERNEL);
586         if (!elt_data->comm) {
587                 track_data_free(data);
588                 return ERR_PTR(-ENOMEM);
589         }
590
591         return data;
592 }
593
594 static char last_cmd[MAX_FILTER_STR_VAL];
595 static char last_cmd_loc[MAX_FILTER_STR_VAL];
596
597 static int errpos(char *str)
598 {
599         return err_pos(last_cmd, str);
600 }
601
602 static void last_cmd_set(struct trace_event_file *file, char *str)
603 {
604         const char *system = NULL, *name = NULL;
605         struct trace_event_call *call;
606
607         if (!str)
608                 return;
609
610         strncpy(last_cmd, str, MAX_FILTER_STR_VAL - 1);
611
612         if (file) {
613                 call = file->event_call;
614
615                 system = call->class->system;
616                 if (system) {
617                         name = trace_event_name(call);
618                         if (!name)
619                                 system = NULL;
620                 }
621         }
622
623         if (system)
624                 snprintf(last_cmd_loc, MAX_FILTER_STR_VAL, "hist:%s:%s", system, name);
625 }
626
627 static void hist_err(struct trace_array *tr, u8 err_type, u8 err_pos)
628 {
629         tracing_log_err(tr, last_cmd_loc, last_cmd, err_text,
630                         err_type, err_pos);
631 }
632
633 static void hist_err_clear(void)
634 {
635         last_cmd[0] = '\0';
636         last_cmd_loc[0] = '\0';
637 }
638
639 struct synth_trace_event {
640         struct trace_entry      ent;
641         u64                     fields[];
642 };
643
644 static int synth_event_define_fields(struct trace_event_call *call)
645 {
646         struct synth_trace_event trace;
647         int offset = offsetof(typeof(trace), fields);
648         struct synth_event *event = call->data;
649         unsigned int i, size, n_u64;
650         char *name, *type;
651         bool is_signed;
652         int ret = 0;
653
654         for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
655                 size = event->fields[i]->size;
656                 is_signed = event->fields[i]->is_signed;
657                 type = event->fields[i]->type;
658                 name = event->fields[i]->name;
659                 ret = trace_define_field(call, type, name, offset, size,
660                                          is_signed, FILTER_OTHER);
661                 if (ret)
662                         break;
663
664                 if (event->fields[i]->is_string) {
665                         offset += STR_VAR_LEN_MAX;
666                         n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
667                 } else {
668                         offset += sizeof(u64);
669                         n_u64++;
670                 }
671         }
672
673         event->n_u64 = n_u64;
674
675         return ret;
676 }
677
678 static bool synth_field_signed(char *type)
679 {
680         if (str_has_prefix(type, "u"))
681                 return false;
682         if (strcmp(type, "gfp_t") == 0)
683                 return false;
684
685         return true;
686 }
687
688 static int synth_field_is_string(char *type)
689 {
690         if (strstr(type, "char[") != NULL)
691                 return true;
692
693         return false;
694 }
695
696 static int synth_field_string_size(char *type)
697 {
698         char buf[4], *end, *start;
699         unsigned int len;
700         int size, err;
701
702         start = strstr(type, "char[");
703         if (start == NULL)
704                 return -EINVAL;
705         start += sizeof("char[") - 1;
706
707         end = strchr(type, ']');
708         if (!end || end < start)
709                 return -EINVAL;
710
711         len = end - start;
712         if (len > 3)
713                 return -EINVAL;
714
715         strncpy(buf, start, len);
716         buf[len] = '\0';
717
718         err = kstrtouint(buf, 0, &size);
719         if (err)
720                 return err;
721
722         if (size > STR_VAR_LEN_MAX)
723                 return -EINVAL;
724
725         return size;
726 }
727
728 static int synth_field_size(char *type)
729 {
730         int size = 0;
731
732         if (strcmp(type, "s64") == 0)
733                 size = sizeof(s64);
734         else if (strcmp(type, "u64") == 0)
735                 size = sizeof(u64);
736         else if (strcmp(type, "s32") == 0)
737                 size = sizeof(s32);
738         else if (strcmp(type, "u32") == 0)
739                 size = sizeof(u32);
740         else if (strcmp(type, "s16") == 0)
741                 size = sizeof(s16);
742         else if (strcmp(type, "u16") == 0)
743                 size = sizeof(u16);
744         else if (strcmp(type, "s8") == 0)
745                 size = sizeof(s8);
746         else if (strcmp(type, "u8") == 0)
747                 size = sizeof(u8);
748         else if (strcmp(type, "char") == 0)
749                 size = sizeof(char);
750         else if (strcmp(type, "unsigned char") == 0)
751                 size = sizeof(unsigned char);
752         else if (strcmp(type, "int") == 0)
753                 size = sizeof(int);
754         else if (strcmp(type, "unsigned int") == 0)
755                 size = sizeof(unsigned int);
756         else if (strcmp(type, "long") == 0)
757                 size = sizeof(long);
758         else if (strcmp(type, "unsigned long") == 0)
759                 size = sizeof(unsigned long);
760         else if (strcmp(type, "pid_t") == 0)
761                 size = sizeof(pid_t);
762         else if (strcmp(type, "gfp_t") == 0)
763                 size = sizeof(gfp_t);
764         else if (synth_field_is_string(type))
765                 size = synth_field_string_size(type);
766
767         return size;
768 }
769
770 static const char *synth_field_fmt(char *type)
771 {
772         const char *fmt = "%llu";
773
774         if (strcmp(type, "s64") == 0)
775                 fmt = "%lld";
776         else if (strcmp(type, "u64") == 0)
777                 fmt = "%llu";
778         else if (strcmp(type, "s32") == 0)
779                 fmt = "%d";
780         else if (strcmp(type, "u32") == 0)
781                 fmt = "%u";
782         else if (strcmp(type, "s16") == 0)
783                 fmt = "%d";
784         else if (strcmp(type, "u16") == 0)
785                 fmt = "%u";
786         else if (strcmp(type, "s8") == 0)
787                 fmt = "%d";
788         else if (strcmp(type, "u8") == 0)
789                 fmt = "%u";
790         else if (strcmp(type, "char") == 0)
791                 fmt = "%d";
792         else if (strcmp(type, "unsigned char") == 0)
793                 fmt = "%u";
794         else if (strcmp(type, "int") == 0)
795                 fmt = "%d";
796         else if (strcmp(type, "unsigned int") == 0)
797                 fmt = "%u";
798         else if (strcmp(type, "long") == 0)
799                 fmt = "%ld";
800         else if (strcmp(type, "unsigned long") == 0)
801                 fmt = "%lu";
802         else if (strcmp(type, "pid_t") == 0)
803                 fmt = "%d";
804         else if (strcmp(type, "gfp_t") == 0)
805                 fmt = "%x";
806         else if (synth_field_is_string(type))
807                 fmt = "%s";
808
809         return fmt;
810 }
811
812 static enum print_line_t print_synth_event(struct trace_iterator *iter,
813                                            int flags,
814                                            struct trace_event *event)
815 {
816         struct trace_array *tr = iter->tr;
817         struct trace_seq *s = &iter->seq;
818         struct synth_trace_event *entry;
819         struct synth_event *se;
820         unsigned int i, n_u64;
821         char print_fmt[32];
822         const char *fmt;
823
824         entry = (struct synth_trace_event *)iter->ent;
825         se = container_of(event, struct synth_event, call.event);
826
827         trace_seq_printf(s, "%s: ", se->name);
828
829         for (i = 0, n_u64 = 0; i < se->n_fields; i++) {
830                 if (trace_seq_has_overflowed(s))
831                         goto end;
832
833                 fmt = synth_field_fmt(se->fields[i]->type);
834
835                 /* parameter types */
836                 if (tr && tr->trace_flags & TRACE_ITER_VERBOSE)
837                         trace_seq_printf(s, "%s ", fmt);
838
839                 snprintf(print_fmt, sizeof(print_fmt), "%%s=%s%%s", fmt);
840
841                 /* parameter values */
842                 if (se->fields[i]->is_string) {
843                         trace_seq_printf(s, print_fmt, se->fields[i]->name,
844                                          (char *)&entry->fields[n_u64],
845                                          i == se->n_fields - 1 ? "" : " ");
846                         n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
847                 } else {
848                         struct trace_print_flags __flags[] = {
849                             __def_gfpflag_names, {-1, NULL} };
850
851                         trace_seq_printf(s, print_fmt, se->fields[i]->name,
852                                          entry->fields[n_u64],
853                                          i == se->n_fields - 1 ? "" : " ");
854
855                         if (strcmp(se->fields[i]->type, "gfp_t") == 0) {
856                                 trace_seq_puts(s, " (");
857                                 trace_print_flags_seq(s, "|",
858                                                       entry->fields[n_u64],
859                                                       __flags);
860                                 trace_seq_putc(s, ')');
861                         }
862                         n_u64++;
863                 }
864         }
865 end:
866         trace_seq_putc(s, '\n');
867
868         return trace_handle_return(s);
869 }
870
871 static struct trace_event_functions synth_event_funcs = {
872         .trace          = print_synth_event
873 };
874
875 static notrace void trace_event_raw_event_synth(void *__data,
876                                                 u64 *var_ref_vals,
877                                                 unsigned int var_ref_idx)
878 {
879         struct trace_event_file *trace_file = __data;
880         struct synth_trace_event *entry;
881         struct trace_event_buffer fbuffer;
882         struct trace_buffer *buffer;
883         struct synth_event *event;
884         unsigned int i, n_u64;
885         int fields_size = 0;
886
887         event = trace_file->event_call->data;
888
889         if (trace_trigger_soft_disabled(trace_file))
890                 return;
891
892         fields_size = event->n_u64 * sizeof(u64);
893
894         /*
895          * Avoid ring buffer recursion detection, as this event
896          * is being performed within another event.
897          */
898         buffer = trace_file->tr->array_buffer.buffer;
899         ring_buffer_nest_start(buffer);
900
901         entry = trace_event_buffer_reserve(&fbuffer, trace_file,
902                                            sizeof(*entry) + fields_size);
903         if (!entry)
904                 goto out;
905
906         for (i = 0, n_u64 = 0; i < event->n_fields; i++) {
907                 if (event->fields[i]->is_string) {
908                         char *str_val = (char *)(long)var_ref_vals[var_ref_idx + i];
909                         char *str_field = (char *)&entry->fields[n_u64];
910
911                         strscpy(str_field, str_val, STR_VAR_LEN_MAX);
912                         n_u64 += STR_VAR_LEN_MAX / sizeof(u64);
913                 } else {
914                         struct synth_field *field = event->fields[i];
915                         u64 val = var_ref_vals[var_ref_idx + i];
916
917                         switch (field->size) {
918                         case 1:
919                                 *(u8 *)&entry->fields[n_u64] = (u8)val;
920                                 break;
921
922                         case 2:
923                                 *(u16 *)&entry->fields[n_u64] = (u16)val;
924                                 break;
925
926                         case 4:
927                                 *(u32 *)&entry->fields[n_u64] = (u32)val;
928                                 break;
929
930                         default:
931                                 entry->fields[n_u64] = val;
932                                 break;
933                         }
934                         n_u64++;
935                 }
936         }
937
938         trace_event_buffer_commit(&fbuffer);
939 out:
940         ring_buffer_nest_end(buffer);
941 }
942
943 static void free_synth_event_print_fmt(struct trace_event_call *call)
944 {
945         if (call) {
946                 kfree(call->print_fmt);
947                 call->print_fmt = NULL;
948         }
949 }
950
951 static int __set_synth_event_print_fmt(struct synth_event *event,
952                                        char *buf, int len)
953 {
954         const char *fmt;
955         int pos = 0;
956         int i;
957
958         /* When len=0, we just calculate the needed length */
959 #define LEN_OR_ZERO (len ? len - pos : 0)
960
961         pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
962         for (i = 0; i < event->n_fields; i++) {
963                 fmt = synth_field_fmt(event->fields[i]->type);
964                 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s=%s%s",
965                                 event->fields[i]->name, fmt,
966                                 i == event->n_fields - 1 ? "" : ", ");
967         }
968         pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
969
970         for (i = 0; i < event->n_fields; i++) {
971                 pos += snprintf(buf + pos, LEN_OR_ZERO,
972                                 ", REC->%s", event->fields[i]->name);
973         }
974
975 #undef LEN_OR_ZERO
976
977         /* return the length of print_fmt */
978         return pos;
979 }
980
981 static int set_synth_event_print_fmt(struct trace_event_call *call)
982 {
983         struct synth_event *event = call->data;
984         char *print_fmt;
985         int len;
986
987         /* First: called with 0 length to calculate the needed length */
988         len = __set_synth_event_print_fmt(event, NULL, 0);
989
990         print_fmt = kmalloc(len + 1, GFP_KERNEL);
991         if (!print_fmt)
992                 return -ENOMEM;
993
994         /* Second: actually write the @print_fmt */
995         __set_synth_event_print_fmt(event, print_fmt, len + 1);
996         call->print_fmt = print_fmt;
997
998         return 0;
999 }
1000
1001 static void free_synth_field(struct synth_field *field)
1002 {
1003         kfree(field->type);
1004         kfree(field->name);
1005         kfree(field);
1006 }
1007
1008 static struct synth_field *parse_synth_field(int argc, const char **argv,
1009                                              int *consumed)
1010 {
1011         struct synth_field *field;
1012         const char *prefix = NULL, *field_type = argv[0], *field_name, *array;
1013         int len, ret = 0;
1014
1015         if (field_type[0] == ';')
1016                 field_type++;
1017
1018         if (!strcmp(field_type, "unsigned")) {
1019                 if (argc < 3)
1020                         return ERR_PTR(-EINVAL);
1021                 prefix = "unsigned ";
1022                 field_type = argv[1];
1023                 field_name = argv[2];
1024                 *consumed = 3;
1025         } else {
1026                 field_name = argv[1];
1027                 *consumed = 2;
1028         }
1029
1030         field = kzalloc(sizeof(*field), GFP_KERNEL);
1031         if (!field)
1032                 return ERR_PTR(-ENOMEM);
1033
1034         len = strlen(field_name);
1035         array = strchr(field_name, '[');
1036         if (array)
1037                 len -= strlen(array);
1038         else if (field_name[len - 1] == ';')
1039                 len--;
1040
1041         field->name = kmemdup_nul(field_name, len, GFP_KERNEL);
1042         if (!field->name) {
1043                 ret = -ENOMEM;
1044                 goto free;
1045         }
1046
1047         if (field_type[0] == ';')
1048                 field_type++;
1049         len = strlen(field_type) + 1;
1050         if (array)
1051                 len += strlen(array);
1052         if (prefix)
1053                 len += strlen(prefix);
1054
1055         field->type = kzalloc(len, GFP_KERNEL);
1056         if (!field->type) {
1057                 ret = -ENOMEM;
1058                 goto free;
1059         }
1060         if (prefix)
1061                 strcat(field->type, prefix);
1062         strcat(field->type, field_type);
1063         if (array) {
1064                 strcat(field->type, array);
1065                 if (field->type[len - 1] == ';')
1066                         field->type[len - 1] = '\0';
1067         }
1068
1069         field->size = synth_field_size(field->type);
1070         if (!field->size) {
1071                 ret = -EINVAL;
1072                 goto free;
1073         }
1074
1075         if (synth_field_is_string(field->type))
1076                 field->is_string = true;
1077
1078         field->is_signed = synth_field_signed(field->type);
1079
1080  out:
1081         return field;
1082  free:
1083         free_synth_field(field);
1084         field = ERR_PTR(ret);
1085         goto out;
1086 }
1087
1088 static void free_synth_tracepoint(struct tracepoint *tp)
1089 {
1090         if (!tp)
1091                 return;
1092
1093         kfree(tp->name);
1094         kfree(tp);
1095 }
1096
1097 static struct tracepoint *alloc_synth_tracepoint(char *name)
1098 {
1099         struct tracepoint *tp;
1100
1101         tp = kzalloc(sizeof(*tp), GFP_KERNEL);
1102         if (!tp)
1103                 return ERR_PTR(-ENOMEM);
1104
1105         tp->name = kstrdup(name, GFP_KERNEL);
1106         if (!tp->name) {
1107                 kfree(tp);
1108                 return ERR_PTR(-ENOMEM);
1109         }
1110
1111         return tp;
1112 }
1113
1114 typedef void (*synth_probe_func_t) (void *__data, u64 *var_ref_vals,
1115                                     unsigned int var_ref_idx);
1116
1117 static inline void trace_synth(struct synth_event *event, u64 *var_ref_vals,
1118                                unsigned int var_ref_idx)
1119 {
1120         struct tracepoint *tp = event->tp;
1121
1122         if (unlikely(atomic_read(&tp->key.enabled) > 0)) {
1123                 struct tracepoint_func *probe_func_ptr;
1124                 synth_probe_func_t probe_func;
1125                 void *__data;
1126
1127                 if (!(cpu_online(raw_smp_processor_id())))
1128                         return;
1129
1130                 probe_func_ptr = rcu_dereference_sched((tp)->funcs);
1131                 if (probe_func_ptr) {
1132                         do {
1133                                 probe_func = probe_func_ptr->func;
1134                                 __data = probe_func_ptr->data;
1135                                 probe_func(__data, var_ref_vals, var_ref_idx);
1136                         } while ((++probe_func_ptr)->func);
1137                 }
1138         }
1139 }
1140
1141 static struct synth_event *find_synth_event(const char *name)
1142 {
1143         struct dyn_event *pos;
1144         struct synth_event *event;
1145
1146         for_each_dyn_event(pos) {
1147                 if (!is_synth_event(pos))
1148                         continue;
1149                 event = to_synth_event(pos);
1150                 if (strcmp(event->name, name) == 0)
1151                         return event;
1152         }
1153
1154         return NULL;
1155 }
1156
1157 static int register_synth_event(struct synth_event *event)
1158 {
1159         struct trace_event_call *call = &event->call;
1160         int ret = 0;
1161
1162         event->call.class = &event->class;
1163         event->class.system = kstrdup(SYNTH_SYSTEM, GFP_KERNEL);
1164         if (!event->class.system) {
1165                 ret = -ENOMEM;
1166                 goto out;
1167         }
1168
1169         event->tp = alloc_synth_tracepoint(event->name);
1170         if (IS_ERR(event->tp)) {
1171                 ret = PTR_ERR(event->tp);
1172                 event->tp = NULL;
1173                 goto out;
1174         }
1175
1176         INIT_LIST_HEAD(&call->class->fields);
1177         call->event.funcs = &synth_event_funcs;
1178         call->class->define_fields = synth_event_define_fields;
1179
1180         ret = register_trace_event(&call->event);
1181         if (!ret) {
1182                 ret = -ENODEV;
1183                 goto out;
1184         }
1185         call->flags = TRACE_EVENT_FL_TRACEPOINT;
1186         call->class->reg = trace_event_reg;
1187         call->class->probe = trace_event_raw_event_synth;
1188         call->data = event;
1189         call->tp = event->tp;
1190
1191         ret = trace_add_event_call(call);
1192         if (ret) {
1193                 pr_warn("Failed to register synthetic event: %s\n",
1194                         trace_event_name(call));
1195                 goto err;
1196         }
1197
1198         ret = set_synth_event_print_fmt(call);
1199         if (ret < 0) {
1200                 trace_remove_event_call(call);
1201                 goto err;
1202         }
1203  out:
1204         return ret;
1205  err:
1206         unregister_trace_event(&call->event);
1207         goto out;
1208 }
1209
1210 static int unregister_synth_event(struct synth_event *event)
1211 {
1212         struct trace_event_call *call = &event->call;
1213         int ret;
1214
1215         ret = trace_remove_event_call(call);
1216
1217         return ret;
1218 }
1219
1220 static void free_synth_event(struct synth_event *event)
1221 {
1222         unsigned int i;
1223
1224         if (!event)
1225                 return;
1226
1227         for (i = 0; i < event->n_fields; i++)
1228                 free_synth_field(event->fields[i]);
1229
1230         kfree(event->fields);
1231         kfree(event->name);
1232         kfree(event->class.system);
1233         free_synth_tracepoint(event->tp);
1234         free_synth_event_print_fmt(&event->call);
1235         kfree(event);
1236 }
1237
1238 static struct synth_event *alloc_synth_event(const char *name, int n_fields,
1239                                              struct synth_field **fields)
1240 {
1241         struct synth_event *event;
1242         unsigned int i;
1243
1244         event = kzalloc(sizeof(*event), GFP_KERNEL);
1245         if (!event) {
1246                 event = ERR_PTR(-ENOMEM);
1247                 goto out;
1248         }
1249
1250         event->name = kstrdup(name, GFP_KERNEL);
1251         if (!event->name) {
1252                 kfree(event);
1253                 event = ERR_PTR(-ENOMEM);
1254                 goto out;
1255         }
1256
1257         event->fields = kcalloc(n_fields, sizeof(*event->fields), GFP_KERNEL);
1258         if (!event->fields) {
1259                 free_synth_event(event);
1260                 event = ERR_PTR(-ENOMEM);
1261                 goto out;
1262         }
1263
1264         dyn_event_init(&event->devent, &synth_event_ops);
1265
1266         for (i = 0; i < n_fields; i++)
1267                 event->fields[i] = fields[i];
1268
1269         event->n_fields = n_fields;
1270  out:
1271         return event;
1272 }
1273
1274 static void action_trace(struct hist_trigger_data *hist_data,
1275                          struct tracing_map_elt *elt, void *rec,
1276                          struct ring_buffer_event *rbe, void *key,
1277                          struct action_data *data, u64 *var_ref_vals)
1278 {
1279         struct synth_event *event = data->synth_event;
1280
1281         trace_synth(event, var_ref_vals, data->var_ref_idx);
1282 }
1283
1284 struct hist_var_data {
1285         struct list_head list;
1286         struct hist_trigger_data *hist_data;
1287 };
1288
1289 static int __create_synth_event(int argc, const char *name, const char **argv)
1290 {
1291         struct synth_field *field, *fields[SYNTH_FIELDS_MAX];
1292         struct synth_event *event = NULL;
1293         int i, consumed = 0, n_fields = 0, ret = 0;
1294
1295         /*
1296          * Argument syntax:
1297          *  - Add synthetic event: <event_name> field[;field] ...
1298          *  - Remove synthetic event: !<event_name> field[;field] ...
1299          *      where 'field' = type field_name
1300          */
1301
1302         if (name[0] == '\0' || argc < 1)
1303                 return -EINVAL;
1304
1305         mutex_lock(&event_mutex);
1306
1307         event = find_synth_event(name);
1308         if (event) {
1309                 ret = -EEXIST;
1310                 goto out;
1311         }
1312
1313         for (i = 0; i < argc - 1; i++) {
1314                 if (strcmp(argv[i], ";") == 0)
1315                         continue;
1316                 if (n_fields == SYNTH_FIELDS_MAX) {
1317                         ret = -EINVAL;
1318                         goto err;
1319                 }
1320
1321                 field = parse_synth_field(argc - i, &argv[i], &consumed);
1322                 if (IS_ERR(field)) {
1323                         ret = PTR_ERR(field);
1324                         goto err;
1325                 }
1326                 fields[n_fields++] = field;
1327                 i += consumed - 1;
1328         }
1329
1330         if (i < argc && strcmp(argv[i], ";") != 0) {
1331                 ret = -EINVAL;
1332                 goto err;
1333         }
1334
1335         event = alloc_synth_event(name, n_fields, fields);
1336         if (IS_ERR(event)) {
1337                 ret = PTR_ERR(event);
1338                 event = NULL;
1339                 goto err;
1340         }
1341         ret = register_synth_event(event);
1342         if (!ret)
1343                 dyn_event_add(&event->devent);
1344         else
1345                 free_synth_event(event);
1346  out:
1347         mutex_unlock(&event_mutex);
1348
1349         return ret;
1350  err:
1351         for (i = 0; i < n_fields; i++)
1352                 free_synth_field(fields[i]);
1353
1354         goto out;
1355 }
1356
1357 static int create_or_delete_synth_event(int argc, char **argv)
1358 {
1359         const char *name = argv[0];
1360         struct synth_event *event = NULL;
1361         int ret;
1362
1363         /* trace_run_command() ensures argc != 0 */
1364         if (name[0] == '!') {
1365                 mutex_lock(&event_mutex);
1366                 event = find_synth_event(name + 1);
1367                 if (event) {
1368                         if (event->ref)
1369                                 ret = -EBUSY;
1370                         else {
1371                                 ret = unregister_synth_event(event);
1372                                 if (!ret) {
1373                                         dyn_event_remove(&event->devent);
1374                                         free_synth_event(event);
1375                                 }
1376                         }
1377                 } else
1378                         ret = -ENOENT;
1379                 mutex_unlock(&event_mutex);
1380                 return ret;
1381         }
1382
1383         ret = __create_synth_event(argc - 1, name, (const char **)argv + 1);
1384         return ret == -ECANCELED ? -EINVAL : ret;
1385 }
1386
1387 int synth_event_run_command(const char *command)
1388 {
1389         return trace_run_command(command, create_or_delete_synth_event);
1390 }
1391
1392 static int synth_event_create(int argc, const char **argv)
1393 {
1394         const char *name = argv[0];
1395         int len;
1396
1397         if (name[0] != 's' || name[1] != ':')
1398                 return -ECANCELED;
1399         name += 2;
1400
1401         /* This interface accepts group name prefix */
1402         if (strchr(name, '/')) {
1403                 len = str_has_prefix(name, SYNTH_SYSTEM "/");
1404                 if (len == 0)
1405                         return -EINVAL;
1406                 name += len;
1407         }
1408         return __create_synth_event(argc - 1, name, argv + 1);
1409 }
1410
1411 static int synth_event_release(struct dyn_event *ev)
1412 {
1413         struct synth_event *event = to_synth_event(ev);
1414         int ret;
1415
1416         if (event->ref)
1417                 return -EBUSY;
1418
1419         ret = unregister_synth_event(event);
1420         if (ret)
1421                 return ret;
1422
1423         dyn_event_remove(ev);
1424         free_synth_event(event);
1425         return 0;
1426 }
1427
1428 static int __synth_event_show(struct seq_file *m, struct synth_event *event)
1429 {
1430         struct synth_field *field;
1431         unsigned int i;
1432
1433         seq_printf(m, "%s\t", event->name);
1434
1435         for (i = 0; i < event->n_fields; i++) {
1436                 field = event->fields[i];
1437
1438                 /* parameter values */
1439                 seq_printf(m, "%s %s%s", field->type, field->name,
1440                            i == event->n_fields - 1 ? "" : "; ");
1441         }
1442
1443         seq_putc(m, '\n');
1444
1445         return 0;
1446 }
1447
1448 static int synth_event_show(struct seq_file *m, struct dyn_event *ev)
1449 {
1450         struct synth_event *event = to_synth_event(ev);
1451
1452         seq_printf(m, "s:%s/", event->class.system);
1453
1454         return __synth_event_show(m, event);
1455 }
1456
1457 static int synth_events_seq_show(struct seq_file *m, void *v)
1458 {
1459         struct dyn_event *ev = v;
1460
1461         if (!is_synth_event(ev))
1462                 return 0;
1463
1464         return __synth_event_show(m, to_synth_event(ev));
1465 }
1466
1467 static const struct seq_operations synth_events_seq_op = {
1468         .start  = dyn_event_seq_start,
1469         .next   = dyn_event_seq_next,
1470         .stop   = dyn_event_seq_stop,
1471         .show   = synth_events_seq_show,
1472 };
1473
1474 static int synth_events_open(struct inode *inode, struct file *file)
1475 {
1476         int ret;
1477
1478         ret = security_locked_down(LOCKDOWN_TRACEFS);
1479         if (ret)
1480                 return ret;
1481
1482         if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
1483                 ret = dyn_events_release_all(&synth_event_ops);
1484                 if (ret < 0)
1485                         return ret;
1486         }
1487
1488         return seq_open(file, &synth_events_seq_op);
1489 }
1490
1491 static ssize_t synth_events_write(struct file *file,
1492                                   const char __user *buffer,
1493                                   size_t count, loff_t *ppos)
1494 {
1495         return trace_parse_run_command(file, buffer, count, ppos,
1496                                        create_or_delete_synth_event);
1497 }
1498
1499 static const struct file_operations synth_events_fops = {
1500         .open           = synth_events_open,
1501         .write          = synth_events_write,
1502         .read           = seq_read,
1503         .llseek         = seq_lseek,
1504         .release        = seq_release,
1505 };
1506
1507 static u64 hist_field_timestamp(struct hist_field *hist_field,
1508                                 struct tracing_map_elt *elt,
1509                                 struct ring_buffer_event *rbe,
1510                                 void *event)
1511 {
1512         struct hist_trigger_data *hist_data = hist_field->hist_data;
1513         struct trace_array *tr = hist_data->event_file->tr;
1514
1515         u64 ts = ring_buffer_event_time_stamp(rbe);
1516
1517         if (hist_data->attrs->ts_in_usecs && trace_clock_in_ns(tr))
1518                 ts = ns2usecs(ts);
1519
1520         return ts;
1521 }
1522
1523 static u64 hist_field_cpu(struct hist_field *hist_field,
1524                           struct tracing_map_elt *elt,
1525                           struct ring_buffer_event *rbe,
1526                           void *event)
1527 {
1528         int cpu = smp_processor_id();
1529
1530         return cpu;
1531 }
1532
1533 /**
1534  * check_field_for_var_ref - Check if a VAR_REF field references a variable
1535  * @hist_field: The VAR_REF field to check
1536  * @var_data: The hist trigger that owns the variable
1537  * @var_idx: The trigger variable identifier
1538  *
1539  * Check the given VAR_REF field to see whether or not it references
1540  * the given variable associated with the given trigger.
1541  *
1542  * Return: The VAR_REF field if it does reference the variable, NULL if not
1543  */
1544 static struct hist_field *
1545 check_field_for_var_ref(struct hist_field *hist_field,
1546                         struct hist_trigger_data *var_data,
1547                         unsigned int var_idx)
1548 {
1549         WARN_ON(!(hist_field && hist_field->flags & HIST_FIELD_FL_VAR_REF));
1550
1551         if (hist_field && hist_field->var.idx == var_idx &&
1552             hist_field->var.hist_data == var_data)
1553                 return hist_field;
1554
1555         return NULL;
1556 }
1557
1558 /**
1559  * find_var_ref - Check if a trigger has a reference to a trigger variable
1560  * @hist_data: The hist trigger that might have a reference to the variable
1561  * @var_data: The hist trigger that owns the variable
1562  * @var_idx: The trigger variable identifier
1563  *
1564  * Check the list of var_refs[] on the first hist trigger to see
1565  * whether any of them are references to the variable on the second
1566  * trigger.
1567  *
1568  * Return: The VAR_REF field referencing the variable if so, NULL if not
1569  */
1570 static struct hist_field *find_var_ref(struct hist_trigger_data *hist_data,
1571                                        struct hist_trigger_data *var_data,
1572                                        unsigned int var_idx)
1573 {
1574         struct hist_field *hist_field;
1575         unsigned int i;
1576
1577         for (i = 0; i < hist_data->n_var_refs; i++) {
1578                 hist_field = hist_data->var_refs[i];
1579                 if (check_field_for_var_ref(hist_field, var_data, var_idx))
1580                         return hist_field;
1581         }
1582
1583         return NULL;
1584 }
1585
1586 /**
1587  * find_any_var_ref - Check if there is a reference to a given trigger variable
1588  * @hist_data: The hist trigger
1589  * @var_idx: The trigger variable identifier
1590  *
1591  * Check to see whether the given variable is currently referenced by
1592  * any other trigger.
1593  *
1594  * The trigger the variable is defined on is explicitly excluded - the
1595  * assumption being that a self-reference doesn't prevent a trigger
1596  * from being removed.
1597  *
1598  * Return: The VAR_REF field referencing the variable if so, NULL if not
1599  */
1600 static struct hist_field *find_any_var_ref(struct hist_trigger_data *hist_data,
1601                                            unsigned int var_idx)
1602 {
1603         struct trace_array *tr = hist_data->event_file->tr;
1604         struct hist_field *found = NULL;
1605         struct hist_var_data *var_data;
1606
1607         list_for_each_entry(var_data, &tr->hist_vars, list) {
1608                 if (var_data->hist_data == hist_data)
1609                         continue;
1610                 found = find_var_ref(var_data->hist_data, hist_data, var_idx);
1611                 if (found)
1612                         break;
1613         }
1614
1615         return found;
1616 }
1617
1618 /**
1619  * check_var_refs - Check if there is a reference to any of trigger's variables
1620  * @hist_data: The hist trigger
1621  *
1622  * A trigger can define one or more variables.  If any one of them is
1623  * currently referenced by any other trigger, this function will
1624  * determine that.
1625
1626  * Typically used to determine whether or not a trigger can be removed
1627  * - if there are any references to a trigger's variables, it cannot.
1628  *
1629  * Return: True if there is a reference to any of trigger's variables
1630  */
1631 static bool check_var_refs(struct hist_trigger_data *hist_data)
1632 {
1633         struct hist_field *field;
1634         bool found = false;
1635         int i;
1636
1637         for_each_hist_field(i, hist_data) {
1638                 field = hist_data->fields[i];
1639                 if (field && field->flags & HIST_FIELD_FL_VAR) {
1640                         if (find_any_var_ref(hist_data, field->var.idx)) {
1641                                 found = true;
1642                                 break;
1643                         }
1644                 }
1645         }
1646
1647         return found;
1648 }
1649
1650 static struct hist_var_data *find_hist_vars(struct hist_trigger_data *hist_data)
1651 {
1652         struct trace_array *tr = hist_data->event_file->tr;
1653         struct hist_var_data *var_data, *found = NULL;
1654
1655         list_for_each_entry(var_data, &tr->hist_vars, list) {
1656                 if (var_data->hist_data == hist_data) {
1657                         found = var_data;
1658                         break;
1659                 }
1660         }
1661
1662         return found;
1663 }
1664
1665 static bool field_has_hist_vars(struct hist_field *hist_field,
1666                                 unsigned int level)
1667 {
1668         int i;
1669
1670         if (level > 3)
1671                 return false;
1672
1673         if (!hist_field)
1674                 return false;
1675
1676         if (hist_field->flags & HIST_FIELD_FL_VAR ||
1677             hist_field->flags & HIST_FIELD_FL_VAR_REF)
1678                 return true;
1679
1680         for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++) {
1681                 struct hist_field *operand;
1682
1683                 operand = hist_field->operands[i];
1684                 if (field_has_hist_vars(operand, level + 1))
1685                         return true;
1686         }
1687
1688         return false;
1689 }
1690
1691 static bool has_hist_vars(struct hist_trigger_data *hist_data)
1692 {
1693         struct hist_field *hist_field;
1694         int i;
1695
1696         for_each_hist_field(i, hist_data) {
1697                 hist_field = hist_data->fields[i];
1698                 if (field_has_hist_vars(hist_field, 0))
1699                         return true;
1700         }
1701
1702         return false;
1703 }
1704
1705 static int save_hist_vars(struct hist_trigger_data *hist_data)
1706 {
1707         struct trace_array *tr = hist_data->event_file->tr;
1708         struct hist_var_data *var_data;
1709
1710         var_data = find_hist_vars(hist_data);
1711         if (var_data)
1712                 return 0;
1713
1714         if (tracing_check_open_get_tr(tr))
1715                 return -ENODEV;
1716
1717         var_data = kzalloc(sizeof(*var_data), GFP_KERNEL);
1718         if (!var_data) {
1719                 trace_array_put(tr);
1720                 return -ENOMEM;
1721         }
1722
1723         var_data->hist_data = hist_data;
1724         list_add(&var_data->list, &tr->hist_vars);
1725
1726         return 0;
1727 }
1728
1729 static void remove_hist_vars(struct hist_trigger_data *hist_data)
1730 {
1731         struct trace_array *tr = hist_data->event_file->tr;
1732         struct hist_var_data *var_data;
1733
1734         var_data = find_hist_vars(hist_data);
1735         if (!var_data)
1736                 return;
1737
1738         if (WARN_ON(check_var_refs(hist_data)))
1739                 return;
1740
1741         list_del(&var_data->list);
1742
1743         kfree(var_data);
1744
1745         trace_array_put(tr);
1746 }
1747
1748 static struct hist_field *find_var_field(struct hist_trigger_data *hist_data,
1749                                          const char *var_name)
1750 {
1751         struct hist_field *hist_field, *found = NULL;
1752         int i;
1753
1754         for_each_hist_field(i, hist_data) {
1755                 hist_field = hist_data->fields[i];
1756                 if (hist_field && hist_field->flags & HIST_FIELD_FL_VAR &&
1757                     strcmp(hist_field->var.name, var_name) == 0) {
1758                         found = hist_field;
1759                         break;
1760                 }
1761         }
1762
1763         return found;
1764 }
1765
1766 static struct hist_field *find_var(struct hist_trigger_data *hist_data,
1767                                    struct trace_event_file *file,
1768                                    const char *var_name)
1769 {
1770         struct hist_trigger_data *test_data;
1771         struct event_trigger_data *test;
1772         struct hist_field *hist_field;
1773
1774         lockdep_assert_held(&event_mutex);
1775
1776         hist_field = find_var_field(hist_data, var_name);
1777         if (hist_field)
1778                 return hist_field;
1779
1780         list_for_each_entry(test, &file->triggers, list) {
1781                 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
1782                         test_data = test->private_data;
1783                         hist_field = find_var_field(test_data, var_name);
1784                         if (hist_field)
1785                                 return hist_field;
1786                 }
1787         }
1788
1789         return NULL;
1790 }
1791
1792 static struct trace_event_file *find_var_file(struct trace_array *tr,
1793                                               char *system,
1794                                               char *event_name,
1795                                               char *var_name)
1796 {
1797         struct hist_trigger_data *var_hist_data;
1798         struct hist_var_data *var_data;
1799         struct trace_event_file *file, *found = NULL;
1800
1801         if (system)
1802                 return find_event_file(tr, system, event_name);
1803
1804         list_for_each_entry(var_data, &tr->hist_vars, list) {
1805                 var_hist_data = var_data->hist_data;
1806                 file = var_hist_data->event_file;
1807                 if (file == found)
1808                         continue;
1809
1810                 if (find_var_field(var_hist_data, var_name)) {
1811                         if (found) {
1812                                 hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE, errpos(var_name));
1813                                 return NULL;
1814                         }
1815
1816                         found = file;
1817                 }
1818         }
1819
1820         return found;
1821 }
1822
1823 static struct hist_field *find_file_var(struct trace_event_file *file,
1824                                         const char *var_name)
1825 {
1826         struct hist_trigger_data *test_data;
1827         struct event_trigger_data *test;
1828         struct hist_field *hist_field;
1829
1830         lockdep_assert_held(&event_mutex);
1831
1832         list_for_each_entry(test, &file->triggers, list) {
1833                 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
1834                         test_data = test->private_data;
1835                         hist_field = find_var_field(test_data, var_name);
1836                         if (hist_field)
1837                                 return hist_field;
1838                 }
1839         }
1840
1841         return NULL;
1842 }
1843
1844 static struct hist_field *
1845 find_match_var(struct hist_trigger_data *hist_data, char *var_name)
1846 {
1847         struct trace_array *tr = hist_data->event_file->tr;
1848         struct hist_field *hist_field, *found = NULL;
1849         struct trace_event_file *file;
1850         unsigned int i;
1851
1852         for (i = 0; i < hist_data->n_actions; i++) {
1853                 struct action_data *data = hist_data->actions[i];
1854
1855                 if (data->handler == HANDLER_ONMATCH) {
1856                         char *system = data->match_data.event_system;
1857                         char *event_name = data->match_data.event;
1858
1859                         file = find_var_file(tr, system, event_name, var_name);
1860                         if (!file)
1861                                 continue;
1862                         hist_field = find_file_var(file, var_name);
1863                         if (hist_field) {
1864                                 if (found) {
1865                                         hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE,
1866                                                  errpos(var_name));
1867                                         return ERR_PTR(-EINVAL);
1868                                 }
1869
1870                                 found = hist_field;
1871                         }
1872                 }
1873         }
1874         return found;
1875 }
1876
1877 static struct hist_field *find_event_var(struct hist_trigger_data *hist_data,
1878                                          char *system,
1879                                          char *event_name,
1880                                          char *var_name)
1881 {
1882         struct trace_array *tr = hist_data->event_file->tr;
1883         struct hist_field *hist_field = NULL;
1884         struct trace_event_file *file;
1885
1886         if (!system || !event_name) {
1887                 hist_field = find_match_var(hist_data, var_name);
1888                 if (IS_ERR(hist_field))
1889                         return NULL;
1890                 if (hist_field)
1891                         return hist_field;
1892         }
1893
1894         file = find_var_file(tr, system, event_name, var_name);
1895         if (!file)
1896                 return NULL;
1897
1898         hist_field = find_file_var(file, var_name);
1899
1900         return hist_field;
1901 }
1902
1903 static u64 hist_field_var_ref(struct hist_field *hist_field,
1904                               struct tracing_map_elt *elt,
1905                               struct ring_buffer_event *rbe,
1906                               void *event)
1907 {
1908         struct hist_elt_data *elt_data;
1909         u64 var_val = 0;
1910
1911         if (WARN_ON_ONCE(!elt))
1912                 return var_val;
1913
1914         elt_data = elt->private_data;
1915         var_val = elt_data->var_ref_vals[hist_field->var_ref_idx];
1916
1917         return var_val;
1918 }
1919
1920 static bool resolve_var_refs(struct hist_trigger_data *hist_data, void *key,
1921                              u64 *var_ref_vals, bool self)
1922 {
1923         struct hist_trigger_data *var_data;
1924         struct tracing_map_elt *var_elt;
1925         struct hist_field *hist_field;
1926         unsigned int i, var_idx;
1927         bool resolved = true;
1928         u64 var_val = 0;
1929
1930         for (i = 0; i < hist_data->n_var_refs; i++) {
1931                 hist_field = hist_data->var_refs[i];
1932                 var_idx = hist_field->var.idx;
1933                 var_data = hist_field->var.hist_data;
1934
1935                 if (var_data == NULL) {
1936                         resolved = false;
1937                         break;
1938                 }
1939
1940                 if ((self && var_data != hist_data) ||
1941                     (!self && var_data == hist_data))
1942                         continue;
1943
1944                 var_elt = tracing_map_lookup(var_data->map, key);
1945                 if (!var_elt) {
1946                         resolved = false;
1947                         break;
1948                 }
1949
1950                 if (!tracing_map_var_set(var_elt, var_idx)) {
1951                         resolved = false;
1952                         break;
1953                 }
1954
1955                 if (self || !hist_field->read_once)
1956                         var_val = tracing_map_read_var(var_elt, var_idx);
1957                 else
1958                         var_val = tracing_map_read_var_once(var_elt, var_idx);
1959
1960                 var_ref_vals[i] = var_val;
1961         }
1962
1963         return resolved;
1964 }
1965
1966 static const char *hist_field_name(struct hist_field *field,
1967                                    unsigned int level)
1968 {
1969         const char *field_name = "";
1970
1971         if (level > 1)
1972                 return field_name;
1973
1974         if (field->field)
1975                 field_name = field->field->name;
1976         else if (field->flags & HIST_FIELD_FL_LOG2 ||
1977                  field->flags & HIST_FIELD_FL_ALIAS)
1978                 field_name = hist_field_name(field->operands[0], ++level);
1979         else if (field->flags & HIST_FIELD_FL_CPU)
1980                 field_name = "cpu";
1981         else if (field->flags & HIST_FIELD_FL_EXPR ||
1982                  field->flags & HIST_FIELD_FL_VAR_REF) {
1983                 if (field->system) {
1984                         static char full_name[MAX_FILTER_STR_VAL];
1985
1986                         strcat(full_name, field->system);
1987                         strcat(full_name, ".");
1988                         strcat(full_name, field->event_name);
1989                         strcat(full_name, ".");
1990                         strcat(full_name, field->name);
1991                         field_name = full_name;
1992                 } else
1993                         field_name = field->name;
1994         } else if (field->flags & HIST_FIELD_FL_TIMESTAMP)
1995                 field_name = "common_timestamp";
1996
1997         if (field_name == NULL)
1998                 field_name = "";
1999
2000         return field_name;
2001 }
2002
2003 static hist_field_fn_t select_value_fn(int field_size, int field_is_signed)
2004 {
2005         hist_field_fn_t fn = NULL;
2006
2007         switch (field_size) {
2008         case 8:
2009                 if (field_is_signed)
2010                         fn = hist_field_s64;
2011                 else
2012                         fn = hist_field_u64;
2013                 break;
2014         case 4:
2015                 if (field_is_signed)
2016                         fn = hist_field_s32;
2017                 else
2018                         fn = hist_field_u32;
2019                 break;
2020         case 2:
2021                 if (field_is_signed)
2022                         fn = hist_field_s16;
2023                 else
2024                         fn = hist_field_u16;
2025                 break;
2026         case 1:
2027                 if (field_is_signed)
2028                         fn = hist_field_s8;
2029                 else
2030                         fn = hist_field_u8;
2031                 break;
2032         }
2033
2034         return fn;
2035 }
2036
2037 static int parse_map_size(char *str)
2038 {
2039         unsigned long size, map_bits;
2040         int ret;
2041
2042         strsep(&str, "=");
2043         if (!str) {
2044                 ret = -EINVAL;
2045                 goto out;
2046         }
2047
2048         ret = kstrtoul(str, 0, &size);
2049         if (ret)
2050                 goto out;
2051
2052         map_bits = ilog2(roundup_pow_of_two(size));
2053         if (map_bits < TRACING_MAP_BITS_MIN ||
2054             map_bits > TRACING_MAP_BITS_MAX)
2055                 ret = -EINVAL;
2056         else
2057                 ret = map_bits;
2058  out:
2059         return ret;
2060 }
2061
2062 static void destroy_hist_trigger_attrs(struct hist_trigger_attrs *attrs)
2063 {
2064         unsigned int i;
2065
2066         if (!attrs)
2067                 return;
2068
2069         for (i = 0; i < attrs->n_assignments; i++)
2070                 kfree(attrs->assignment_str[i]);
2071
2072         for (i = 0; i < attrs->n_actions; i++)
2073                 kfree(attrs->action_str[i]);
2074
2075         kfree(attrs->name);
2076         kfree(attrs->sort_key_str);
2077         kfree(attrs->keys_str);
2078         kfree(attrs->vals_str);
2079         kfree(attrs->clock);
2080         kfree(attrs);
2081 }
2082
2083 static int parse_action(char *str, struct hist_trigger_attrs *attrs)
2084 {
2085         int ret = -EINVAL;
2086
2087         if (attrs->n_actions >= HIST_ACTIONS_MAX)
2088                 return ret;
2089
2090         if ((str_has_prefix(str, "onmatch(")) ||
2091             (str_has_prefix(str, "onmax(")) ||
2092             (str_has_prefix(str, "onchange("))) {
2093                 attrs->action_str[attrs->n_actions] = kstrdup(str, GFP_KERNEL);
2094                 if (!attrs->action_str[attrs->n_actions]) {
2095                         ret = -ENOMEM;
2096                         return ret;
2097                 }
2098                 attrs->n_actions++;
2099                 ret = 0;
2100         }
2101         return ret;
2102 }
2103
2104 static int parse_assignment(struct trace_array *tr,
2105                             char *str, struct hist_trigger_attrs *attrs)
2106 {
2107         int ret = 0;
2108
2109         if ((str_has_prefix(str, "key=")) ||
2110             (str_has_prefix(str, "keys="))) {
2111                 attrs->keys_str = kstrdup(str, GFP_KERNEL);
2112                 if (!attrs->keys_str) {
2113                         ret = -ENOMEM;
2114                         goto out;
2115                 }
2116         } else if ((str_has_prefix(str, "val=")) ||
2117                    (str_has_prefix(str, "vals=")) ||
2118                    (str_has_prefix(str, "values="))) {
2119                 attrs->vals_str = kstrdup(str, GFP_KERNEL);
2120                 if (!attrs->vals_str) {
2121                         ret = -ENOMEM;
2122                         goto out;
2123                 }
2124         } else if (str_has_prefix(str, "sort=")) {
2125                 attrs->sort_key_str = kstrdup(str, GFP_KERNEL);
2126                 if (!attrs->sort_key_str) {
2127                         ret = -ENOMEM;
2128                         goto out;
2129                 }
2130         } else if (str_has_prefix(str, "name=")) {
2131                 attrs->name = kstrdup(str, GFP_KERNEL);
2132                 if (!attrs->name) {
2133                         ret = -ENOMEM;
2134                         goto out;
2135                 }
2136         } else if (str_has_prefix(str, "clock=")) {
2137                 strsep(&str, "=");
2138                 if (!str) {
2139                         ret = -EINVAL;
2140                         goto out;
2141                 }
2142
2143                 str = strstrip(str);
2144                 attrs->clock = kstrdup(str, GFP_KERNEL);
2145                 if (!attrs->clock) {
2146                         ret = -ENOMEM;
2147                         goto out;
2148                 }
2149         } else if (str_has_prefix(str, "size=")) {
2150                 int map_bits = parse_map_size(str);
2151
2152                 if (map_bits < 0) {
2153                         ret = map_bits;
2154                         goto out;
2155                 }
2156                 attrs->map_bits = map_bits;
2157         } else {
2158                 char *assignment;
2159
2160                 if (attrs->n_assignments == TRACING_MAP_VARS_MAX) {
2161                         hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(str));
2162                         ret = -EINVAL;
2163                         goto out;
2164                 }
2165
2166                 assignment = kstrdup(str, GFP_KERNEL);
2167                 if (!assignment) {
2168                         ret = -ENOMEM;
2169                         goto out;
2170                 }
2171
2172                 attrs->assignment_str[attrs->n_assignments++] = assignment;
2173         }
2174  out:
2175         return ret;
2176 }
2177
2178 static struct hist_trigger_attrs *
2179 parse_hist_trigger_attrs(struct trace_array *tr, char *trigger_str)
2180 {
2181         struct hist_trigger_attrs *attrs;
2182         int ret = 0;
2183
2184         attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
2185         if (!attrs)
2186                 return ERR_PTR(-ENOMEM);
2187
2188         while (trigger_str) {
2189                 char *str = strsep(&trigger_str, ":");
2190
2191                 if (strchr(str, '=')) {
2192                         ret = parse_assignment(tr, str, attrs);
2193                         if (ret)
2194                                 goto free;
2195                 } else if (strcmp(str, "pause") == 0)
2196                         attrs->pause = true;
2197                 else if ((strcmp(str, "cont") == 0) ||
2198                          (strcmp(str, "continue") == 0))
2199                         attrs->cont = true;
2200                 else if (strcmp(str, "clear") == 0)
2201                         attrs->clear = true;
2202                 else {
2203                         ret = parse_action(str, attrs);
2204                         if (ret)
2205                                 goto free;
2206                 }
2207         }
2208
2209         if (!attrs->keys_str) {
2210                 ret = -EINVAL;
2211                 goto free;
2212         }
2213
2214         if (!attrs->clock) {
2215                 attrs->clock = kstrdup("global", GFP_KERNEL);
2216                 if (!attrs->clock) {
2217                         ret = -ENOMEM;
2218                         goto free;
2219                 }
2220         }
2221
2222         return attrs;
2223  free:
2224         destroy_hist_trigger_attrs(attrs);
2225
2226         return ERR_PTR(ret);
2227 }
2228
2229 static inline void save_comm(char *comm, struct task_struct *task)
2230 {
2231         if (!task->pid) {
2232                 strcpy(comm, "<idle>");
2233                 return;
2234         }
2235
2236         if (WARN_ON_ONCE(task->pid < 0)) {
2237                 strcpy(comm, "<XXX>");
2238                 return;
2239         }
2240
2241         strncpy(comm, task->comm, TASK_COMM_LEN);
2242 }
2243
2244 static void hist_elt_data_free(struct hist_elt_data *elt_data)
2245 {
2246         unsigned int i;
2247
2248         for (i = 0; i < SYNTH_FIELDS_MAX; i++)
2249                 kfree(elt_data->field_var_str[i]);
2250
2251         kfree(elt_data->comm);
2252         kfree(elt_data);
2253 }
2254
2255 static void hist_trigger_elt_data_free(struct tracing_map_elt *elt)
2256 {
2257         struct hist_elt_data *elt_data = elt->private_data;
2258
2259         hist_elt_data_free(elt_data);
2260 }
2261
2262 static int hist_trigger_elt_data_alloc(struct tracing_map_elt *elt)
2263 {
2264         struct hist_trigger_data *hist_data = elt->map->private_data;
2265         unsigned int size = TASK_COMM_LEN;
2266         struct hist_elt_data *elt_data;
2267         struct hist_field *key_field;
2268         unsigned int i, n_str;
2269
2270         elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL);
2271         if (!elt_data)
2272                 return -ENOMEM;
2273
2274         for_each_hist_key_field(i, hist_data) {
2275                 key_field = hist_data->fields[i];
2276
2277                 if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
2278                         elt_data->comm = kzalloc(size, GFP_KERNEL);
2279                         if (!elt_data->comm) {
2280                                 kfree(elt_data);
2281                                 return -ENOMEM;
2282                         }
2283                         break;
2284                 }
2285         }
2286
2287         n_str = hist_data->n_field_var_str + hist_data->n_save_var_str;
2288
2289         size = STR_VAR_LEN_MAX;
2290
2291         for (i = 0; i < n_str; i++) {
2292                 elt_data->field_var_str[i] = kzalloc(size, GFP_KERNEL);
2293                 if (!elt_data->field_var_str[i]) {
2294                         hist_elt_data_free(elt_data);
2295                         return -ENOMEM;
2296                 }
2297         }
2298
2299         elt->private_data = elt_data;
2300
2301         return 0;
2302 }
2303
2304 static void hist_trigger_elt_data_init(struct tracing_map_elt *elt)
2305 {
2306         struct hist_elt_data *elt_data = elt->private_data;
2307
2308         if (elt_data->comm)
2309                 save_comm(elt_data->comm, current);
2310 }
2311
2312 static const struct tracing_map_ops hist_trigger_elt_data_ops = {
2313         .elt_alloc      = hist_trigger_elt_data_alloc,
2314         .elt_free       = hist_trigger_elt_data_free,
2315         .elt_init       = hist_trigger_elt_data_init,
2316 };
2317
2318 static const char *get_hist_field_flags(struct hist_field *hist_field)
2319 {
2320         const char *flags_str = NULL;
2321
2322         if (hist_field->flags & HIST_FIELD_FL_HEX)
2323                 flags_str = "hex";
2324         else if (hist_field->flags & HIST_FIELD_FL_SYM)
2325                 flags_str = "sym";
2326         else if (hist_field->flags & HIST_FIELD_FL_SYM_OFFSET)
2327                 flags_str = "sym-offset";
2328         else if (hist_field->flags & HIST_FIELD_FL_EXECNAME)
2329                 flags_str = "execname";
2330         else if (hist_field->flags & HIST_FIELD_FL_SYSCALL)
2331                 flags_str = "syscall";
2332         else if (hist_field->flags & HIST_FIELD_FL_LOG2)
2333                 flags_str = "log2";
2334         else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP_USECS)
2335                 flags_str = "usecs";
2336
2337         return flags_str;
2338 }
2339
2340 static void expr_field_str(struct hist_field *field, char *expr)
2341 {
2342         if (field->flags & HIST_FIELD_FL_VAR_REF)
2343                 strcat(expr, "$");
2344
2345         strcat(expr, hist_field_name(field, 0));
2346
2347         if (field->flags && !(field->flags & HIST_FIELD_FL_VAR_REF)) {
2348                 const char *flags_str = get_hist_field_flags(field);
2349
2350                 if (flags_str) {
2351                         strcat(expr, ".");
2352                         strcat(expr, flags_str);
2353                 }
2354         }
2355 }
2356
2357 static char *expr_str(struct hist_field *field, unsigned int level)
2358 {
2359         char *expr;
2360
2361         if (level > 1)
2362                 return NULL;
2363
2364         expr = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
2365         if (!expr)
2366                 return NULL;
2367
2368         if (!field->operands[0]) {
2369                 expr_field_str(field, expr);
2370                 return expr;
2371         }
2372
2373         if (field->operator == FIELD_OP_UNARY_MINUS) {
2374                 char *subexpr;
2375
2376                 strcat(expr, "-(");
2377                 subexpr = expr_str(field->operands[0], ++level);
2378                 if (!subexpr) {
2379                         kfree(expr);
2380                         return NULL;
2381                 }
2382                 strcat(expr, subexpr);
2383                 strcat(expr, ")");
2384
2385                 kfree(subexpr);
2386
2387                 return expr;
2388         }
2389
2390         expr_field_str(field->operands[0], expr);
2391
2392         switch (field->operator) {
2393         case FIELD_OP_MINUS:
2394                 strcat(expr, "-");
2395                 break;
2396         case FIELD_OP_PLUS:
2397                 strcat(expr, "+");
2398                 break;
2399         default:
2400                 kfree(expr);
2401                 return NULL;
2402         }
2403
2404         expr_field_str(field->operands[1], expr);
2405
2406         return expr;
2407 }
2408
2409 static int contains_operator(char *str)
2410 {
2411         enum field_op_id field_op = FIELD_OP_NONE;
2412         char *op;
2413
2414         op = strpbrk(str, "+-");
2415         if (!op)
2416                 return FIELD_OP_NONE;
2417
2418         switch (*op) {
2419         case '-':
2420                 if (*str == '-')
2421                         field_op = FIELD_OP_UNARY_MINUS;
2422                 else
2423                         field_op = FIELD_OP_MINUS;
2424                 break;
2425         case '+':
2426                 field_op = FIELD_OP_PLUS;
2427                 break;
2428         default:
2429                 break;
2430         }
2431
2432         return field_op;
2433 }
2434
2435 static void __destroy_hist_field(struct hist_field *hist_field)
2436 {
2437         kfree(hist_field->var.name);
2438         kfree(hist_field->name);
2439         kfree(hist_field->type);
2440
2441         kfree(hist_field);
2442 }
2443
2444 static void destroy_hist_field(struct hist_field *hist_field,
2445                                unsigned int level)
2446 {
2447         unsigned int i;
2448
2449         if (level > 3)
2450                 return;
2451
2452         if (!hist_field)
2453                 return;
2454
2455         if (hist_field->flags & HIST_FIELD_FL_VAR_REF)
2456                 return; /* var refs will be destroyed separately */
2457
2458         for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++)
2459                 destroy_hist_field(hist_field->operands[i], level + 1);
2460
2461         __destroy_hist_field(hist_field);
2462 }
2463
2464 static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data,
2465                                             struct ftrace_event_field *field,
2466                                             unsigned long flags,
2467                                             char *var_name)
2468 {
2469         struct hist_field *hist_field;
2470
2471         if (field && is_function_field(field))
2472                 return NULL;
2473
2474         hist_field = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
2475         if (!hist_field)
2476                 return NULL;
2477
2478         hist_field->hist_data = hist_data;
2479
2480         if (flags & HIST_FIELD_FL_EXPR || flags & HIST_FIELD_FL_ALIAS)
2481                 goto out; /* caller will populate */
2482
2483         if (flags & HIST_FIELD_FL_VAR_REF) {
2484                 hist_field->fn = hist_field_var_ref;
2485                 goto out;
2486         }
2487
2488         if (flags & HIST_FIELD_FL_HITCOUNT) {
2489                 hist_field->fn = hist_field_counter;
2490                 hist_field->size = sizeof(u64);
2491                 hist_field->type = kstrdup("u64", GFP_KERNEL);
2492                 if (!hist_field->type)
2493                         goto free;
2494                 goto out;
2495         }
2496
2497         if (flags & HIST_FIELD_FL_STACKTRACE) {
2498                 hist_field->fn = hist_field_none;
2499                 goto out;
2500         }
2501
2502         if (flags & HIST_FIELD_FL_LOG2) {
2503                 unsigned long fl = flags & ~HIST_FIELD_FL_LOG2;
2504                 hist_field->fn = hist_field_log2;
2505                 hist_field->operands[0] = create_hist_field(hist_data, field, fl, NULL);
2506                 hist_field->size = hist_field->operands[0]->size;
2507                 hist_field->type = kstrdup(hist_field->operands[0]->type, GFP_KERNEL);
2508                 if (!hist_field->type)
2509                         goto free;
2510                 goto out;
2511         }
2512
2513         if (flags & HIST_FIELD_FL_TIMESTAMP) {
2514                 hist_field->fn = hist_field_timestamp;
2515                 hist_field->size = sizeof(u64);
2516                 hist_field->type = kstrdup("u64", GFP_KERNEL);
2517                 if (!hist_field->type)
2518                         goto free;
2519                 goto out;
2520         }
2521
2522         if (flags & HIST_FIELD_FL_CPU) {
2523                 hist_field->fn = hist_field_cpu;
2524                 hist_field->size = sizeof(int);
2525                 hist_field->type = kstrdup("unsigned int", GFP_KERNEL);
2526                 if (!hist_field->type)
2527                         goto free;
2528                 goto out;
2529         }
2530
2531         if (WARN_ON_ONCE(!field))
2532                 goto out;
2533
2534         if (is_string_field(field)) {
2535                 flags |= HIST_FIELD_FL_STRING;
2536
2537                 hist_field->size = MAX_FILTER_STR_VAL;
2538                 hist_field->type = kstrdup(field->type, GFP_KERNEL);
2539                 if (!hist_field->type)
2540                         goto free;
2541
2542                 if (field->filter_type == FILTER_STATIC_STRING)
2543                         hist_field->fn = hist_field_string;
2544                 else if (field->filter_type == FILTER_DYN_STRING)
2545                         hist_field->fn = hist_field_dynstring;
2546                 else
2547                         hist_field->fn = hist_field_pstring;
2548         } else {
2549                 hist_field->size = field->size;
2550                 hist_field->is_signed = field->is_signed;
2551                 hist_field->type = kstrdup(field->type, GFP_KERNEL);
2552                 if (!hist_field->type)
2553                         goto free;
2554
2555                 hist_field->fn = select_value_fn(field->size,
2556                                                  field->is_signed);
2557                 if (!hist_field->fn) {
2558                         destroy_hist_field(hist_field, 0);
2559                         return NULL;
2560                 }
2561         }
2562  out:
2563         hist_field->field = field;
2564         hist_field->flags = flags;
2565
2566         if (var_name) {
2567                 hist_field->var.name = kstrdup(var_name, GFP_KERNEL);
2568                 if (!hist_field->var.name)
2569                         goto free;
2570         }
2571
2572         return hist_field;
2573  free:
2574         destroy_hist_field(hist_field, 0);
2575         return NULL;
2576 }
2577
2578 static void destroy_hist_fields(struct hist_trigger_data *hist_data)
2579 {
2580         unsigned int i;
2581
2582         for (i = 0; i < HIST_FIELDS_MAX; i++) {
2583                 if (hist_data->fields[i]) {
2584                         destroy_hist_field(hist_data->fields[i], 0);
2585                         hist_data->fields[i] = NULL;
2586                 }
2587         }
2588
2589         for (i = 0; i < hist_data->n_var_refs; i++) {
2590                 WARN_ON(!(hist_data->var_refs[i]->flags & HIST_FIELD_FL_VAR_REF));
2591                 __destroy_hist_field(hist_data->var_refs[i]);
2592                 hist_data->var_refs[i] = NULL;
2593         }
2594 }
2595
2596 static int init_var_ref(struct hist_field *ref_field,
2597                         struct hist_field *var_field,
2598                         char *system, char *event_name)
2599 {
2600         int err = 0;
2601
2602         ref_field->var.idx = var_field->var.idx;
2603         ref_field->var.hist_data = var_field->hist_data;
2604         ref_field->size = var_field->size;
2605         ref_field->is_signed = var_field->is_signed;
2606         ref_field->flags |= var_field->flags &
2607                 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2608
2609         if (system) {
2610                 ref_field->system = kstrdup(system, GFP_KERNEL);
2611                 if (!ref_field->system)
2612                         return -ENOMEM;
2613         }
2614
2615         if (event_name) {
2616                 ref_field->event_name = kstrdup(event_name, GFP_KERNEL);
2617                 if (!ref_field->event_name) {
2618                         err = -ENOMEM;
2619                         goto free;
2620                 }
2621         }
2622
2623         if (var_field->var.name) {
2624                 ref_field->name = kstrdup(var_field->var.name, GFP_KERNEL);
2625                 if (!ref_field->name) {
2626                         err = -ENOMEM;
2627                         goto free;
2628                 }
2629         } else if (var_field->name) {
2630                 ref_field->name = kstrdup(var_field->name, GFP_KERNEL);
2631                 if (!ref_field->name) {
2632                         err = -ENOMEM;
2633                         goto free;
2634                 }
2635         }
2636
2637         ref_field->type = kstrdup(var_field->type, GFP_KERNEL);
2638         if (!ref_field->type) {
2639                 err = -ENOMEM;
2640                 goto free;
2641         }
2642  out:
2643         return err;
2644  free:
2645         kfree(ref_field->system);
2646         kfree(ref_field->event_name);
2647         kfree(ref_field->name);
2648
2649         goto out;
2650 }
2651
2652 /**
2653  * create_var_ref - Create a variable reference and attach it to trigger
2654  * @hist_data: The trigger that will be referencing the variable
2655  * @var_field: The VAR field to create a reference to
2656  * @system: The optional system string
2657  * @event_name: The optional event_name string
2658  *
2659  * Given a variable hist_field, create a VAR_REF hist_field that
2660  * represents a reference to it.
2661  *
2662  * This function also adds the reference to the trigger that
2663  * now references the variable.
2664  *
2665  * Return: The VAR_REF field if successful, NULL if not
2666  */
2667 static struct hist_field *create_var_ref(struct hist_trigger_data *hist_data,
2668                                          struct hist_field *var_field,
2669                                          char *system, char *event_name)
2670 {
2671         unsigned long flags = HIST_FIELD_FL_VAR_REF;
2672         struct hist_field *ref_field;
2673
2674         ref_field = create_hist_field(var_field->hist_data, NULL, flags, NULL);
2675         if (ref_field) {
2676                 if (init_var_ref(ref_field, var_field, system, event_name)) {
2677                         destroy_hist_field(ref_field, 0);
2678                         return NULL;
2679                 }
2680
2681                 hist_data->var_refs[hist_data->n_var_refs] = ref_field;
2682                 ref_field->var_ref_idx = hist_data->n_var_refs++;
2683         }
2684
2685         return ref_field;
2686 }
2687
2688 static bool is_var_ref(char *var_name)
2689 {
2690         if (!var_name || strlen(var_name) < 2 || var_name[0] != '$')
2691                 return false;
2692
2693         return true;
2694 }
2695
2696 static char *field_name_from_var(struct hist_trigger_data *hist_data,
2697                                  char *var_name)
2698 {
2699         char *name, *field;
2700         unsigned int i;
2701
2702         for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
2703                 name = hist_data->attrs->var_defs.name[i];
2704
2705                 if (strcmp(var_name, name) == 0) {
2706                         field = hist_data->attrs->var_defs.expr[i];
2707                         if (contains_operator(field) || is_var_ref(field))
2708                                 continue;
2709                         return field;
2710                 }
2711         }
2712
2713         return NULL;
2714 }
2715
2716 static char *local_field_var_ref(struct hist_trigger_data *hist_data,
2717                                  char *system, char *event_name,
2718                                  char *var_name)
2719 {
2720         struct trace_event_call *call;
2721
2722         if (system && event_name) {
2723                 call = hist_data->event_file->event_call;
2724
2725                 if (strcmp(system, call->class->system) != 0)
2726                         return NULL;
2727
2728                 if (strcmp(event_name, trace_event_name(call)) != 0)
2729                         return NULL;
2730         }
2731
2732         if (!!system != !!event_name)
2733                 return NULL;
2734
2735         if (!is_var_ref(var_name))
2736                 return NULL;
2737
2738         var_name++;
2739
2740         return field_name_from_var(hist_data, var_name);
2741 }
2742
2743 static struct hist_field *parse_var_ref(struct hist_trigger_data *hist_data,
2744                                         char *system, char *event_name,
2745                                         char *var_name)
2746 {
2747         struct hist_field *var_field = NULL, *ref_field = NULL;
2748         struct trace_array *tr = hist_data->event_file->tr;
2749
2750         if (!is_var_ref(var_name))
2751                 return NULL;
2752
2753         var_name++;
2754
2755         var_field = find_event_var(hist_data, system, event_name, var_name);
2756         if (var_field)
2757                 ref_field = create_var_ref(hist_data, var_field,
2758                                            system, event_name);
2759
2760         if (!ref_field)
2761                 hist_err(tr, HIST_ERR_VAR_NOT_FOUND, errpos(var_name));
2762
2763         return ref_field;
2764 }
2765
2766 static struct ftrace_event_field *
2767 parse_field(struct hist_trigger_data *hist_data, struct trace_event_file *file,
2768             char *field_str, unsigned long *flags)
2769 {
2770         struct ftrace_event_field *field = NULL;
2771         char *field_name, *modifier, *str;
2772         struct trace_array *tr = file->tr;
2773
2774         modifier = str = kstrdup(field_str, GFP_KERNEL);
2775         if (!modifier)
2776                 return ERR_PTR(-ENOMEM);
2777
2778         field_name = strsep(&modifier, ".");
2779         if (modifier) {
2780                 if (strcmp(modifier, "hex") == 0)
2781                         *flags |= HIST_FIELD_FL_HEX;
2782                 else if (strcmp(modifier, "sym") == 0)
2783                         *flags |= HIST_FIELD_FL_SYM;
2784                 else if (strcmp(modifier, "sym-offset") == 0)
2785                         *flags |= HIST_FIELD_FL_SYM_OFFSET;
2786                 else if ((strcmp(modifier, "execname") == 0) &&
2787                          (strcmp(field_name, "common_pid") == 0))
2788                         *flags |= HIST_FIELD_FL_EXECNAME;
2789                 else if (strcmp(modifier, "syscall") == 0)
2790                         *flags |= HIST_FIELD_FL_SYSCALL;
2791                 else if (strcmp(modifier, "log2") == 0)
2792                         *flags |= HIST_FIELD_FL_LOG2;
2793                 else if (strcmp(modifier, "usecs") == 0)
2794                         *flags |= HIST_FIELD_FL_TIMESTAMP_USECS;
2795                 else {
2796                         hist_err(tr, HIST_ERR_BAD_FIELD_MODIFIER, errpos(modifier));
2797                         field = ERR_PTR(-EINVAL);
2798                         goto out;
2799                 }
2800         }
2801
2802         if (strcmp(field_name, "common_timestamp") == 0) {
2803                 *flags |= HIST_FIELD_FL_TIMESTAMP;
2804                 hist_data->enable_timestamps = true;
2805                 if (*flags & HIST_FIELD_FL_TIMESTAMP_USECS)
2806                         hist_data->attrs->ts_in_usecs = true;
2807         } else if (strcmp(field_name, "cpu") == 0)
2808                 *flags |= HIST_FIELD_FL_CPU;
2809         else {
2810                 field = trace_find_event_field(file->event_call, field_name);
2811                 if (!field || !field->size) {
2812                         hist_err(tr, HIST_ERR_FIELD_NOT_FOUND, errpos(field_name));
2813                         field = ERR_PTR(-EINVAL);
2814                         goto out;
2815                 }
2816         }
2817  out:
2818         kfree(str);
2819
2820         return field;
2821 }
2822
2823 static struct hist_field *create_alias(struct hist_trigger_data *hist_data,
2824                                        struct hist_field *var_ref,
2825                                        char *var_name)
2826 {
2827         struct hist_field *alias = NULL;
2828         unsigned long flags = HIST_FIELD_FL_ALIAS | HIST_FIELD_FL_VAR;
2829
2830         alias = create_hist_field(hist_data, NULL, flags, var_name);
2831         if (!alias)
2832                 return NULL;
2833
2834         alias->fn = var_ref->fn;
2835         alias->operands[0] = var_ref;
2836
2837         if (init_var_ref(alias, var_ref, var_ref->system, var_ref->event_name)) {
2838                 destroy_hist_field(alias, 0);
2839                 return NULL;
2840         }
2841
2842         alias->var_ref_idx = var_ref->var_ref_idx;
2843
2844         return alias;
2845 }
2846
2847 static struct hist_field *parse_atom(struct hist_trigger_data *hist_data,
2848                                      struct trace_event_file *file, char *str,
2849                                      unsigned long *flags, char *var_name)
2850 {
2851         char *s, *ref_system = NULL, *ref_event = NULL, *ref_var = str;
2852         struct ftrace_event_field *field = NULL;
2853         struct hist_field *hist_field = NULL;
2854         int ret = 0;
2855
2856         s = strchr(str, '.');
2857         if (s) {
2858                 s = strchr(++s, '.');
2859                 if (s) {
2860                         ref_system = strsep(&str, ".");
2861                         if (!str) {
2862                                 ret = -EINVAL;
2863                                 goto out;
2864                         }
2865                         ref_event = strsep(&str, ".");
2866                         if (!str) {
2867                                 ret = -EINVAL;
2868                                 goto out;
2869                         }
2870                         ref_var = str;
2871                 }
2872         }
2873
2874         s = local_field_var_ref(hist_data, ref_system, ref_event, ref_var);
2875         if (!s) {
2876                 hist_field = parse_var_ref(hist_data, ref_system,
2877                                            ref_event, ref_var);
2878                 if (hist_field) {
2879                         if (var_name) {
2880                                 hist_field = create_alias(hist_data, hist_field, var_name);
2881                                 if (!hist_field) {
2882                                         ret = -ENOMEM;
2883                                         goto out;
2884                                 }
2885                         }
2886                         return hist_field;
2887                 }
2888         } else
2889                 str = s;
2890
2891         field = parse_field(hist_data, file, str, flags);
2892         if (IS_ERR(field)) {
2893                 ret = PTR_ERR(field);
2894                 goto out;
2895         }
2896
2897         hist_field = create_hist_field(hist_data, field, *flags, var_name);
2898         if (!hist_field) {
2899                 ret = -ENOMEM;
2900                 goto out;
2901         }
2902
2903         return hist_field;
2904  out:
2905         return ERR_PTR(ret);
2906 }
2907
2908 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
2909                                      struct trace_event_file *file,
2910                                      char *str, unsigned long flags,
2911                                      char *var_name, unsigned int level);
2912
2913 static struct hist_field *parse_unary(struct hist_trigger_data *hist_data,
2914                                       struct trace_event_file *file,
2915                                       char *str, unsigned long flags,
2916                                       char *var_name, unsigned int level)
2917 {
2918         struct hist_field *operand1, *expr = NULL;
2919         unsigned long operand_flags;
2920         int ret = 0;
2921         char *s;
2922
2923         /* we support only -(xxx) i.e. explicit parens required */
2924
2925         if (level > 3) {
2926                 hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
2927                 ret = -EINVAL;
2928                 goto free;
2929         }
2930
2931         str++; /* skip leading '-' */
2932
2933         s = strchr(str, '(');
2934         if (s)
2935                 str++;
2936         else {
2937                 ret = -EINVAL;
2938                 goto free;
2939         }
2940
2941         s = strrchr(str, ')');
2942         if (s)
2943                 *s = '\0';
2944         else {
2945                 ret = -EINVAL; /* no closing ')' */
2946                 goto free;
2947         }
2948
2949         flags |= HIST_FIELD_FL_EXPR;
2950         expr = create_hist_field(hist_data, NULL, flags, var_name);
2951         if (!expr) {
2952                 ret = -ENOMEM;
2953                 goto free;
2954         }
2955
2956         operand_flags = 0;
2957         operand1 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
2958         if (IS_ERR(operand1)) {
2959                 ret = PTR_ERR(operand1);
2960                 goto free;
2961         }
2962
2963         expr->flags |= operand1->flags &
2964                 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
2965         expr->fn = hist_field_unary_minus;
2966         expr->operands[0] = operand1;
2967         expr->operator = FIELD_OP_UNARY_MINUS;
2968         expr->name = expr_str(expr, 0);
2969         expr->type = kstrdup(operand1->type, GFP_KERNEL);
2970         if (!expr->type) {
2971                 ret = -ENOMEM;
2972                 goto free;
2973         }
2974
2975         return expr;
2976  free:
2977         destroy_hist_field(expr, 0);
2978         return ERR_PTR(ret);
2979 }
2980
2981 static int check_expr_operands(struct trace_array *tr,
2982                                struct hist_field *operand1,
2983                                struct hist_field *operand2)
2984 {
2985         unsigned long operand1_flags = operand1->flags;
2986         unsigned long operand2_flags = operand2->flags;
2987
2988         if ((operand1_flags & HIST_FIELD_FL_VAR_REF) ||
2989             (operand1_flags & HIST_FIELD_FL_ALIAS)) {
2990                 struct hist_field *var;
2991
2992                 var = find_var_field(operand1->var.hist_data, operand1->name);
2993                 if (!var)
2994                         return -EINVAL;
2995                 operand1_flags = var->flags;
2996         }
2997
2998         if ((operand2_flags & HIST_FIELD_FL_VAR_REF) ||
2999             (operand2_flags & HIST_FIELD_FL_ALIAS)) {
3000                 struct hist_field *var;
3001
3002                 var = find_var_field(operand2->var.hist_data, operand2->name);
3003                 if (!var)
3004                         return -EINVAL;
3005                 operand2_flags = var->flags;
3006         }
3007
3008         if ((operand1_flags & HIST_FIELD_FL_TIMESTAMP_USECS) !=
3009             (operand2_flags & HIST_FIELD_FL_TIMESTAMP_USECS)) {
3010                 hist_err(tr, HIST_ERR_TIMESTAMP_MISMATCH, 0);
3011                 return -EINVAL;
3012         }
3013
3014         return 0;
3015 }
3016
3017 static struct hist_field *parse_expr(struct hist_trigger_data *hist_data,
3018                                      struct trace_event_file *file,
3019                                      char *str, unsigned long flags,
3020                                      char *var_name, unsigned int level)
3021 {
3022         struct hist_field *operand1 = NULL, *operand2 = NULL, *expr = NULL;
3023         unsigned long operand_flags;
3024         int field_op, ret = -EINVAL;
3025         char *sep, *operand1_str;
3026
3027         if (level > 3) {
3028                 hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str));
3029                 return ERR_PTR(-EINVAL);
3030         }
3031
3032         field_op = contains_operator(str);
3033
3034         if (field_op == FIELD_OP_NONE)
3035                 return parse_atom(hist_data, file, str, &flags, var_name);
3036
3037         if (field_op == FIELD_OP_UNARY_MINUS)
3038                 return parse_unary(hist_data, file, str, flags, var_name, ++level);
3039
3040         switch (field_op) {
3041         case FIELD_OP_MINUS:
3042                 sep = "-";
3043                 break;
3044         case FIELD_OP_PLUS:
3045                 sep = "+";
3046                 break;
3047         default:
3048                 goto free;
3049         }
3050
3051         operand1_str = strsep(&str, sep);
3052         if (!operand1_str || !str)
3053                 goto free;
3054
3055         operand_flags = 0;
3056         operand1 = parse_atom(hist_data, file, operand1_str,
3057                               &operand_flags, NULL);
3058         if (IS_ERR(operand1)) {
3059                 ret = PTR_ERR(operand1);
3060                 operand1 = NULL;
3061                 goto free;
3062         }
3063
3064         /* rest of string could be another expression e.g. b+c in a+b+c */
3065         operand_flags = 0;
3066         operand2 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level);
3067         if (IS_ERR(operand2)) {
3068                 ret = PTR_ERR(operand2);
3069                 operand2 = NULL;
3070                 goto free;
3071         }
3072
3073         ret = check_expr_operands(file->tr, operand1, operand2);
3074         if (ret)
3075                 goto free;
3076
3077         flags |= HIST_FIELD_FL_EXPR;
3078
3079         flags |= operand1->flags &
3080                 (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS);
3081
3082         expr = create_hist_field(hist_data, NULL, flags, var_name);
3083         if (!expr) {
3084                 ret = -ENOMEM;
3085                 goto free;
3086         }
3087
3088         operand1->read_once = true;
3089         operand2->read_once = true;
3090
3091         expr->operands[0] = operand1;
3092         expr->operands[1] = operand2;
3093         expr->operator = field_op;
3094         expr->name = expr_str(expr, 0);
3095         expr->type = kstrdup(operand1->type, GFP_KERNEL);
3096         if (!expr->type) {
3097                 ret = -ENOMEM;
3098                 goto free;
3099         }
3100
3101         switch (field_op) {
3102         case FIELD_OP_MINUS:
3103                 expr->fn = hist_field_minus;
3104                 break;
3105         case FIELD_OP_PLUS:
3106                 expr->fn = hist_field_plus;
3107                 break;
3108         default:
3109                 ret = -EINVAL;
3110                 goto free;
3111         }
3112
3113         return expr;
3114  free:
3115         destroy_hist_field(operand1, 0);
3116         destroy_hist_field(operand2, 0);
3117         destroy_hist_field(expr, 0);
3118
3119         return ERR_PTR(ret);
3120 }
3121
3122 static char *find_trigger_filter(struct hist_trigger_data *hist_data,
3123                                  struct trace_event_file *file)
3124 {
3125         struct event_trigger_data *test;
3126
3127         lockdep_assert_held(&event_mutex);
3128
3129         list_for_each_entry(test, &file->triggers, list) {
3130                 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
3131                         if (test->private_data == hist_data)
3132                                 return test->filter_str;
3133                 }
3134         }
3135
3136         return NULL;
3137 }
3138
3139 static struct event_command trigger_hist_cmd;
3140 static int event_hist_trigger_func(struct event_command *cmd_ops,
3141                                    struct trace_event_file *file,
3142                                    char *glob, char *cmd, char *param);
3143
3144 static bool compatible_keys(struct hist_trigger_data *target_hist_data,
3145                             struct hist_trigger_data *hist_data,
3146                             unsigned int n_keys)
3147 {
3148         struct hist_field *target_hist_field, *hist_field;
3149         unsigned int n, i, j;
3150
3151         if (hist_data->n_fields - hist_data->n_vals != n_keys)
3152                 return false;
3153
3154         i = hist_data->n_vals;
3155         j = target_hist_data->n_vals;
3156
3157         for (n = 0; n < n_keys; n++) {
3158                 hist_field = hist_data->fields[i + n];
3159                 target_hist_field = target_hist_data->fields[j + n];
3160
3161                 if (strcmp(hist_field->type, target_hist_field->type) != 0)
3162                         return false;
3163                 if (hist_field->size != target_hist_field->size)
3164                         return false;
3165                 if (hist_field->is_signed != target_hist_field->is_signed)
3166                         return false;
3167         }
3168
3169         return true;
3170 }
3171
3172 static struct hist_trigger_data *
3173 find_compatible_hist(struct hist_trigger_data *target_hist_data,
3174                      struct trace_event_file *file)
3175 {
3176         struct hist_trigger_data *hist_data;
3177         struct event_trigger_data *test;
3178         unsigned int n_keys;
3179
3180         lockdep_assert_held(&event_mutex);
3181
3182         n_keys = target_hist_data->n_fields - target_hist_data->n_vals;
3183
3184         list_for_each_entry(test, &file->triggers, list) {
3185                 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
3186                         hist_data = test->private_data;
3187
3188                         if (compatible_keys(target_hist_data, hist_data, n_keys))
3189                                 return hist_data;
3190                 }
3191         }
3192
3193         return NULL;
3194 }
3195
3196 static struct trace_event_file *event_file(struct trace_array *tr,
3197                                            char *system, char *event_name)
3198 {
3199         struct trace_event_file *file;
3200
3201         file = __find_event_file(tr, system, event_name);
3202         if (!file)
3203                 return ERR_PTR(-EINVAL);
3204
3205         return file;
3206 }
3207
3208 static struct hist_field *
3209 find_synthetic_field_var(struct hist_trigger_data *target_hist_data,
3210                          char *system, char *event_name, char *field_name)
3211 {
3212         struct hist_field *event_var;
3213         char *synthetic_name;
3214
3215         synthetic_name = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
3216         if (!synthetic_name)
3217                 return ERR_PTR(-ENOMEM);
3218
3219         strcpy(synthetic_name, "synthetic_");
3220         strcat(synthetic_name, field_name);
3221
3222         event_var = find_event_var(target_hist_data, system, event_name, synthetic_name);
3223
3224         kfree(synthetic_name);
3225
3226         return event_var;
3227 }
3228
3229 /**
3230  * create_field_var_hist - Automatically create a histogram and var for a field
3231  * @target_hist_data: The target hist trigger
3232  * @subsys_name: Optional subsystem name
3233  * @event_name: Optional event name
3234  * @field_name: The name of the field (and the resulting variable)
3235  *
3236  * Hist trigger actions fetch data from variables, not directly from
3237  * events.  However, for convenience, users are allowed to directly
3238  * specify an event field in an action, which will be automatically
3239  * converted into a variable on their behalf.
3240
3241  * If a user specifies a field on an event that isn't the event the
3242  * histogram currently being defined (the target event histogram), the
3243  * only way that can be accomplished is if a new hist trigger is
3244  * created and the field variable defined on that.
3245  *
3246  * This function creates a new histogram compatible with the target
3247  * event (meaning a histogram with the same key as the target
3248  * histogram), and creates a variable for the specified field, but
3249  * with 'synthetic_' prepended to the variable name in order to avoid
3250  * collision with normal field variables.
3251  *
3252  * Return: The variable created for the field.
3253  */
3254 static struct hist_field *
3255 create_field_var_hist(struct hist_trigger_data *target_hist_data,
3256                       char *subsys_name, char *event_name, char *field_name)
3257 {
3258         struct trace_array *tr = target_hist_data->event_file->tr;
3259         struct hist_field *event_var = ERR_PTR(-EINVAL);
3260         struct hist_trigger_data *hist_data;
3261         unsigned int i, n, first = true;
3262         struct field_var_hist *var_hist;
3263         struct trace_event_file *file;
3264         struct hist_field *key_field;
3265         char *saved_filter;
3266         char *cmd;
3267         int ret;
3268
3269         if (target_hist_data->n_field_var_hists >= SYNTH_FIELDS_MAX) {
3270                 hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
3271                 return ERR_PTR(-EINVAL);
3272         }
3273
3274         file = event_file(tr, subsys_name, event_name);
3275
3276         if (IS_ERR(file)) {
3277                 hist_err(tr, HIST_ERR_EVENT_FILE_NOT_FOUND, errpos(field_name));
3278                 ret = PTR_ERR(file);
3279                 return ERR_PTR(ret);
3280         }
3281
3282         /*
3283          * Look for a histogram compatible with target.  We'll use the
3284          * found histogram specification to create a new matching
3285          * histogram with our variable on it.  target_hist_data is not
3286          * yet a registered histogram so we can't use that.
3287          */
3288         hist_data = find_compatible_hist(target_hist_data, file);
3289         if (!hist_data) {
3290                 hist_err(tr, HIST_ERR_HIST_NOT_FOUND, errpos(field_name));
3291                 return ERR_PTR(-EINVAL);
3292         }
3293
3294         /* See if a synthetic field variable has already been created */
3295         event_var = find_synthetic_field_var(target_hist_data, subsys_name,
3296                                              event_name, field_name);
3297         if (!IS_ERR_OR_NULL(event_var))
3298                 return event_var;
3299
3300         var_hist = kzalloc(sizeof(*var_hist), GFP_KERNEL);
3301         if (!var_hist)
3302                 return ERR_PTR(-ENOMEM);
3303
3304         cmd = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL);
3305         if (!cmd) {
3306                 kfree(var_hist);
3307                 return ERR_PTR(-ENOMEM);
3308         }
3309
3310         /* Use the same keys as the compatible histogram */
3311         strcat(cmd, "keys=");
3312
3313         for_each_hist_key_field(i, hist_data) {
3314                 key_field = hist_data->fields[i];
3315                 if (!first)
3316                         strcat(cmd, ",");
3317                 strcat(cmd, key_field->field->name);
3318                 first = false;
3319         }
3320
3321         /* Create the synthetic field variable specification */
3322         strcat(cmd, ":synthetic_");
3323         strcat(cmd, field_name);
3324         strcat(cmd, "=");
3325         strcat(cmd, field_name);
3326
3327         /* Use the same filter as the compatible histogram */
3328         saved_filter = find_trigger_filter(hist_data, file);
3329         if (saved_filter) {
3330                 strcat(cmd, " if ");
3331                 strcat(cmd, saved_filter);
3332         }
3333
3334         var_hist->cmd = kstrdup(cmd, GFP_KERNEL);
3335         if (!var_hist->cmd) {
3336                 kfree(cmd);
3337                 kfree(var_hist);
3338                 return ERR_PTR(-ENOMEM);
3339         }
3340
3341         /* Save the compatible histogram information */
3342         var_hist->hist_data = hist_data;
3343
3344         /* Create the new histogram with our variable */
3345         ret = event_hist_trigger_func(&trigger_hist_cmd, file,
3346                                       "", "hist", cmd);
3347         if (ret) {
3348                 kfree(cmd);
3349                 kfree(var_hist->cmd);
3350                 kfree(var_hist);
3351                 hist_err(tr, HIST_ERR_HIST_CREATE_FAIL, errpos(field_name));
3352                 return ERR_PTR(ret);
3353         }
3354
3355         kfree(cmd);
3356
3357         /* If we can't find the variable, something went wrong */
3358         event_var = find_synthetic_field_var(target_hist_data, subsys_name,
3359                                              event_name, field_name);
3360         if (IS_ERR_OR_NULL(event_var)) {
3361                 kfree(var_hist->cmd);
3362                 kfree(var_hist);
3363                 hist_err(tr, HIST_ERR_SYNTH_VAR_NOT_FOUND, errpos(field_name));
3364                 return ERR_PTR(-EINVAL);
3365         }
3366
3367         n = target_hist_data->n_field_var_hists;
3368         target_hist_data->field_var_hists[n] = var_hist;
3369         target_hist_data->n_field_var_hists++;
3370
3371         return event_var;
3372 }
3373
3374 static struct hist_field *
3375 find_target_event_var(struct hist_trigger_data *hist_data,
3376                       char *subsys_name, char *event_name, char *var_name)
3377 {
3378         struct trace_event_file *file = hist_data->event_file;
3379         struct hist_field *hist_field = NULL;
3380
3381         if (subsys_name) {
3382                 struct trace_event_call *call;
3383
3384                 if (!event_name)
3385                         return NULL;
3386
3387                 call = file->event_call;
3388
3389                 if (strcmp(subsys_name, call->class->system) != 0)
3390                         return NULL;
3391
3392                 if (strcmp(event_name, trace_event_name(call)) != 0)
3393                         return NULL;
3394         }
3395
3396         hist_field = find_var_field(hist_data, var_name);
3397
3398         return hist_field;
3399 }
3400
3401 static inline void __update_field_vars(struct tracing_map_elt *elt,
3402                                        struct ring_buffer_event *rbe,
3403                                        void *rec,
3404                                        struct field_var **field_vars,
3405                                        unsigned int n_field_vars,
3406                                        unsigned int field_var_str_start)
3407 {
3408         struct hist_elt_data *elt_data = elt->private_data;
3409         unsigned int i, j, var_idx;
3410         u64 var_val;
3411
3412         for (i = 0, j = field_var_str_start; i < n_field_vars; i++) {
3413                 struct field_var *field_var = field_vars[i];
3414                 struct hist_field *var = field_var->var;
3415                 struct hist_field *val = field_var->val;
3416
3417                 var_val = val->fn(val, elt, rbe, rec);
3418                 var_idx = var->var.idx;
3419
3420                 if (val->flags & HIST_FIELD_FL_STRING) {
3421                         char *str = elt_data->field_var_str[j++];
3422                         char *val_str = (char *)(uintptr_t)var_val;
3423
3424                         strscpy(str, val_str, STR_VAR_LEN_MAX);
3425                         var_val = (u64)(uintptr_t)str;
3426                 }
3427                 tracing_map_set_var(elt, var_idx, var_val);
3428         }
3429 }
3430
3431 static void update_field_vars(struct hist_trigger_data *hist_data,
3432                               struct tracing_map_elt *elt,
3433                               struct ring_buffer_event *rbe,
3434                               void *rec)
3435 {
3436         __update_field_vars(elt, rbe, rec, hist_data->field_vars,
3437                             hist_data->n_field_vars, 0);
3438 }
3439
3440 static void save_track_data_vars(struct hist_trigger_data *hist_data,
3441                                  struct tracing_map_elt *elt, void *rec,
3442                                  struct ring_buffer_event *rbe, void *key,
3443                                  struct action_data *data, u64 *var_ref_vals)
3444 {
3445         __update_field_vars(elt, rbe, rec, hist_data->save_vars,
3446                             hist_data->n_save_vars, hist_data->n_field_var_str);
3447 }
3448
3449 static struct hist_field *create_var(struct hist_trigger_data *hist_data,
3450                                      struct trace_event_file *file,
3451                                      char *name, int size, const char *type)
3452 {
3453         struct hist_field *var;
3454         int idx;
3455
3456         if (find_var(hist_data, file, name) && !hist_data->remove) {
3457                 var = ERR_PTR(-EINVAL);
3458                 goto out;
3459         }
3460
3461         var = kzalloc(sizeof(struct hist_field), GFP_KERNEL);
3462         if (!var) {
3463                 var = ERR_PTR(-ENOMEM);
3464                 goto out;
3465         }
3466
3467         idx = tracing_map_add_var(hist_data->map);
3468         if (idx < 0) {
3469                 kfree(var);
3470                 var = ERR_PTR(-EINVAL);
3471                 goto out;
3472         }
3473
3474         var->flags = HIST_FIELD_FL_VAR;
3475         var->var.idx = idx;
3476         var->var.hist_data = var->hist_data = hist_data;
3477         var->size = size;
3478         var->var.name = kstrdup(name, GFP_KERNEL);
3479         var->type = kstrdup(type, GFP_KERNEL);
3480         if (!var->var.name || !var->type) {
3481                 kfree(var->var.name);
3482                 kfree(var->type);
3483                 kfree(var);
3484                 var = ERR_PTR(-ENOMEM);
3485         }
3486  out:
3487         return var;
3488 }
3489
3490 static struct field_var *create_field_var(struct hist_trigger_data *hist_data,
3491                                           struct trace_event_file *file,
3492                                           char *field_name)
3493 {
3494         struct hist_field *val = NULL, *var = NULL;
3495         unsigned long flags = HIST_FIELD_FL_VAR;
3496         struct trace_array *tr = file->tr;
3497         struct field_var *field_var;
3498         int ret = 0;
3499
3500         if (hist_data->n_field_vars >= SYNTH_FIELDS_MAX) {
3501                 hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name));
3502                 ret = -EINVAL;
3503                 goto err;
3504         }
3505
3506         val = parse_atom(hist_data, file, field_name, &flags, NULL);
3507         if (IS_ERR(val)) {
3508                 hist_err(tr, HIST_ERR_FIELD_VAR_PARSE_FAIL, errpos(field_name));
3509                 ret = PTR_ERR(val);
3510                 goto err;
3511         }
3512
3513         var = create_var(hist_data, file, field_name, val->size, val->type);
3514         if (IS_ERR(var)) {
3515                 hist_err(tr, HIST_ERR_VAR_CREATE_FIND_FAIL, errpos(field_name));
3516                 kfree(val);
3517                 ret = PTR_ERR(var);
3518                 goto err;
3519         }
3520
3521         field_var = kzalloc(sizeof(struct field_var), GFP_KERNEL);
3522         if (!field_var) {
3523                 kfree(val);
3524                 kfree(var);
3525                 ret =  -ENOMEM;
3526                 goto err;
3527         }
3528
3529         field_var->var = var;
3530         field_var->val = val;
3531  out:
3532         return field_var;
3533  err:
3534         field_var = ERR_PTR(ret);
3535         goto out;
3536 }
3537
3538 /**
3539  * create_target_field_var - Automatically create a variable for a field
3540  * @target_hist_data: The target hist trigger
3541  * @subsys_name: Optional subsystem name
3542  * @event_name: Optional event name
3543  * @var_name: The name of the field (and the resulting variable)
3544  *
3545  * Hist trigger actions fetch data from variables, not directly from
3546  * events.  However, for convenience, users are allowed to directly
3547  * specify an event field in an action, which will be automatically
3548  * converted into a variable on their behalf.
3549
3550  * This function creates a field variable with the name var_name on
3551  * the hist trigger currently being defined on the target event.  If
3552  * subsys_name and event_name are specified, this function simply
3553  * verifies that they do in fact match the target event subsystem and
3554  * event name.
3555  *
3556  * Return: The variable created for the field.
3557  */
3558 static struct field_var *
3559 create_target_field_var(struct hist_trigger_data *target_hist_data,
3560                         char *subsys_name, char *event_name, char *var_name)
3561 {
3562         struct trace_event_file *file = target_hist_data->event_file;
3563
3564         if (subsys_name) {
3565                 struct trace_event_call *call;
3566
3567                 if (!event_name)
3568                         return NULL;
3569
3570                 call = file->event_call;
3571
3572                 if (strcmp(subsys_name, call->class->system) != 0)
3573                         return NULL;
3574
3575                 if (strcmp(event_name, trace_event_name(call)) != 0)
3576                         return NULL;
3577         }
3578
3579         return create_field_var(target_hist_data, file, var_name);
3580 }
3581
3582 static bool check_track_val_max(u64 track_val, u64 var_val)
3583 {
3584         if (var_val <= track_val)
3585                 return false;
3586
3587         return true;
3588 }
3589
3590 static bool check_track_val_changed(u64 track_val, u64 var_val)
3591 {
3592         if (var_val == track_val)
3593                 return false;
3594
3595         return true;
3596 }
3597
3598 static u64 get_track_val(struct hist_trigger_data *hist_data,
3599                          struct tracing_map_elt *elt,
3600                          struct action_data *data)
3601 {
3602         unsigned int track_var_idx = data->track_data.track_var->var.idx;
3603         u64 track_val;
3604
3605         track_val = tracing_map_read_var(elt, track_var_idx);
3606
3607         return track_val;
3608 }
3609
3610 static void save_track_val(struct hist_trigger_data *hist_data,
3611                            struct tracing_map_elt *elt,
3612                            struct action_data *data, u64 var_val)
3613 {
3614         unsigned int track_var_idx = data->track_data.track_var->var.idx;
3615
3616         tracing_map_set_var(elt, track_var_idx, var_val);
3617 }
3618
3619 static void save_track_data(struct hist_trigger_data *hist_data,
3620                             struct tracing_map_elt *elt, void *rec,
3621                             struct ring_buffer_event *rbe, void *key,
3622                             struct action_data *data, u64 *var_ref_vals)
3623 {
3624         if (data->track_data.save_data)
3625                 data->track_data.save_data(hist_data, elt, rec, rbe, key, data, var_ref_vals);
3626 }
3627
3628 static bool check_track_val(struct tracing_map_elt *elt,
3629                             struct action_data *data,
3630                             u64 var_val)
3631 {
3632         struct hist_trigger_data *hist_data;
3633         u64 track_val;
3634
3635         hist_data = data->track_data.track_var->hist_data;
3636         track_val = get_track_val(hist_data, elt, data);
3637
3638         return data->track_data.check_val(track_val, var_val);
3639 }
3640
3641 #ifdef CONFIG_TRACER_SNAPSHOT
3642 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
3643 {
3644         /* called with tr->max_lock held */
3645         struct track_data *track_data = tr->cond_snapshot->cond_data;
3646         struct hist_elt_data *elt_data, *track_elt_data;
3647         struct snapshot_context *context = cond_data;
3648         struct action_data *action;
3649         u64 track_val;
3650
3651         if (!track_data)
3652                 return false;
3653
3654         action = track_data->action_data;
3655
3656         track_val = get_track_val(track_data->hist_data, context->elt,
3657                                   track_data->action_data);
3658
3659         if (!action->track_data.check_val(track_data->track_val, track_val))
3660                 return false;
3661
3662         track_data->track_val = track_val;
3663         memcpy(track_data->key, context->key, track_data->key_len);
3664
3665         elt_data = context->elt->private_data;
3666         track_elt_data = track_data->elt.private_data;
3667         if (elt_data->comm)
3668                 strncpy(track_elt_data->comm, elt_data->comm, TASK_COMM_LEN);
3669
3670         track_data->updated = true;
3671
3672         return true;
3673 }
3674
3675 static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
3676                                      struct tracing_map_elt *elt, void *rec,
3677                                      struct ring_buffer_event *rbe, void *key,
3678                                      struct action_data *data,
3679                                      u64 *var_ref_vals)
3680 {
3681         struct trace_event_file *file = hist_data->event_file;
3682         struct snapshot_context context;
3683
3684         context.elt = elt;
3685         context.key = key;
3686
3687         tracing_snapshot_cond(file->tr, &context);
3688 }
3689
3690 static void hist_trigger_print_key(struct seq_file *m,
3691                                    struct hist_trigger_data *hist_data,
3692                                    void *key,
3693                                    struct tracing_map_elt *elt);
3694
3695 static struct action_data *snapshot_action(struct hist_trigger_data *hist_data)
3696 {
3697         unsigned int i;
3698
3699         if (!hist_data->n_actions)
3700                 return NULL;
3701
3702         for (i = 0; i < hist_data->n_actions; i++) {
3703                 struct action_data *data = hist_data->actions[i];
3704
3705                 if (data->action == ACTION_SNAPSHOT)
3706                         return data;
3707         }
3708
3709         return NULL;
3710 }
3711
3712 static void track_data_snapshot_print(struct seq_file *m,
3713                                       struct hist_trigger_data *hist_data)
3714 {
3715         struct trace_event_file *file = hist_data->event_file;
3716         struct track_data *track_data;
3717         struct action_data *action;
3718
3719         track_data = tracing_cond_snapshot_data(file->tr);
3720         if (!track_data)
3721                 return;
3722
3723         if (!track_data->updated)
3724                 return;
3725
3726         action = snapshot_action(hist_data);
3727         if (!action)
3728                 return;
3729
3730         seq_puts(m, "\nSnapshot taken (see tracing/snapshot).  Details:\n");
3731         seq_printf(m, "\ttriggering value { %s(%s) }: %10llu",
3732                    action->handler == HANDLER_ONMAX ? "onmax" : "onchange",
3733                    action->track_data.var_str, track_data->track_val);
3734
3735         seq_puts(m, "\ttriggered by event with key: ");
3736         hist_trigger_print_key(m, hist_data, track_data->key, &track_data->elt);
3737         seq_putc(m, '\n');
3738 }
3739 #else
3740 static bool cond_snapshot_update(struct trace_array *tr, void *cond_data)
3741 {
3742         return false;
3743 }
3744 static void save_track_data_snapshot(struct hist_trigger_data *hist_data,
3745                                      struct tracing_map_elt *elt, void *rec,
3746                                      struct ring_buffer_event *rbe, void *key,
3747                                      struct action_data *data,
3748                                      u64 *var_ref_vals) {}
3749 static void track_data_snapshot_print(struct seq_file *m,
3750                                       struct hist_trigger_data *hist_data) {}
3751 #endif /* CONFIG_TRACER_SNAPSHOT */
3752
3753 static void track_data_print(struct seq_file *m,
3754                              struct hist_trigger_data *hist_data,
3755                              struct tracing_map_elt *elt,
3756                              struct action_data *data)
3757 {
3758         u64 track_val = get_track_val(hist_data, elt, data);
3759         unsigned int i, save_var_idx;
3760
3761         if (data->handler == HANDLER_ONMAX)
3762                 seq_printf(m, "\n\tmax: %10llu", track_val);
3763         else if (data->handler == HANDLER_ONCHANGE)
3764                 seq_printf(m, "\n\tchanged: %10llu", track_val);
3765
3766         if (data->action == ACTION_SNAPSHOT)
3767                 return;
3768
3769         for (i = 0; i < hist_data->n_save_vars; i++) {
3770                 struct hist_field *save_val = hist_data->save_vars[i]->val;
3771                 struct hist_field *save_var = hist_data->save_vars[i]->var;
3772                 u64 val;
3773
3774                 save_var_idx = save_var->var.idx;
3775
3776                 val = tracing_map_read_var(elt, save_var_idx);
3777
3778                 if (save_val->flags & HIST_FIELD_FL_STRING) {
3779                         seq_printf(m, "  %s: %-32s", save_var->var.name,
3780                                    (char *)(uintptr_t)(val));
3781                 } else
3782                         seq_printf(m, "  %s: %10llu", save_var->var.name, val);
3783         }
3784 }
3785
3786 static void ontrack_action(struct hist_trigger_data *hist_data,
3787                            struct tracing_map_elt *elt, void *rec,
3788                            struct ring_buffer_event *rbe, void *key,
3789                            struct action_data *data, u64 *var_ref_vals)
3790 {
3791         u64 var_val = var_ref_vals[data->track_data.var_ref->var_ref_idx];
3792
3793         if (check_track_val(elt, data, var_val)) {
3794                 save_track_val(hist_data, elt, data, var_val);
3795                 save_track_data(hist_data, elt, rec, rbe, key, data, var_ref_vals);
3796         }
3797 }
3798
3799 static void action_data_destroy(struct action_data *data)
3800 {
3801         unsigned int i;
3802
3803         lockdep_assert_held(&event_mutex);
3804
3805         kfree(data->action_name);
3806
3807         for (i = 0; i < data->n_params; i++)
3808                 kfree(data->params[i]);
3809
3810         if (data->synth_event)
3811                 data->synth_event->ref--;
3812
3813         kfree(data->synth_event_name);
3814
3815         kfree(data);
3816 }
3817
3818 static void track_data_destroy(struct hist_trigger_data *hist_data,
3819                                struct action_data *data)
3820 {
3821         struct trace_event_file *file = hist_data->event_file;
3822
3823         destroy_hist_field(data->track_data.track_var, 0);
3824
3825         if (data->action == ACTION_SNAPSHOT) {
3826                 struct track_data *track_data;
3827
3828                 track_data = tracing_cond_snapshot_data(file->tr);
3829                 if (track_data && track_data->hist_data == hist_data) {
3830                         tracing_snapshot_cond_disable(file->tr);
3831                         track_data_free(track_data);
3832                 }
3833         }
3834
3835         kfree(data->track_data.var_str);
3836
3837         action_data_destroy(data);
3838 }
3839
3840 static int action_create(struct hist_trigger_data *hist_data,
3841                          struct action_data *data);
3842
3843 static int track_data_create(struct hist_trigger_data *hist_data,
3844                              struct action_data *data)
3845 {
3846         struct hist_field *var_field, *ref_field, *track_var = NULL;
3847         struct trace_event_file *file = hist_data->event_file;
3848         struct trace_array *tr = file->tr;
3849         char *track_data_var_str;
3850         int ret = 0;
3851
3852         track_data_var_str = data->track_data.var_str;
3853         if (track_data_var_str[0] != '$') {
3854                 hist_err(tr, HIST_ERR_ONX_NOT_VAR, errpos(track_data_var_str));
3855                 return -EINVAL;
3856         }
3857         track_data_var_str++;
3858
3859         var_field = find_target_event_var(hist_data, NULL, NULL, track_data_var_str);
3860         if (!var_field) {
3861                 hist_err(tr, HIST_ERR_ONX_VAR_NOT_FOUND, errpos(track_data_var_str));
3862                 return -EINVAL;
3863         }
3864
3865         ref_field = create_var_ref(hist_data, var_field, NULL, NULL);
3866         if (!ref_field)
3867                 return -ENOMEM;
3868
3869         data->track_data.var_ref = ref_field;
3870
3871         if (data->handler == HANDLER_ONMAX)
3872                 track_var = create_var(hist_data, file, "__max", sizeof(u64), "u64");
3873         if (IS_ERR(track_var)) {
3874                 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
3875                 ret = PTR_ERR(track_var);
3876                 goto out;
3877         }
3878
3879         if (data->handler == HANDLER_ONCHANGE)
3880                 track_var = create_var(hist_data, file, "__change", sizeof(u64), "u64");
3881         if (IS_ERR(track_var)) {
3882                 hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0);
3883                 ret = PTR_ERR(track_var);
3884                 goto out;
3885         }
3886         data->track_data.track_var = track_var;
3887
3888         ret = action_create(hist_data, data);
3889  out:
3890         return ret;
3891 }
3892
3893 static int parse_action_params(struct trace_array *tr, char *params,
3894                                struct action_data *data)
3895 {
3896         char *param, *saved_param;
3897         bool first_param = true;
3898         int ret = 0;
3899
3900         while (params) {
3901                 if (data->n_params >= SYNTH_FIELDS_MAX) {
3902                         hist_err(tr, HIST_ERR_TOO_MANY_PARAMS, 0);
3903                         goto out;
3904                 }
3905
3906                 param = strsep(&params, ",");
3907                 if (!param) {
3908                         hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, 0);
3909                         ret = -EINVAL;
3910                         goto out;
3911                 }
3912
3913                 param = strstrip(param);
3914                 if (strlen(param) < 2) {
3915                         hist_err(tr, HIST_ERR_INVALID_PARAM, errpos(param));
3916                         ret = -EINVAL;
3917                         goto out;
3918                 }
3919
3920                 saved_param = kstrdup(param, GFP_KERNEL);
3921                 if (!saved_param) {
3922                         ret = -ENOMEM;
3923                         goto out;
3924                 }
3925
3926                 if (first_param && data->use_trace_keyword) {
3927                         data->synth_event_name = saved_param;
3928                         first_param = false;
3929                         continue;
3930                 }
3931                 first_param = false;
3932
3933                 data->params[data->n_params++] = saved_param;
3934         }
3935  out:
3936         return ret;
3937 }
3938
3939 static int action_parse(struct trace_array *tr, char *str, struct action_data *data,
3940                         enum handler_id handler)
3941 {
3942         char *action_name;
3943         int ret = 0;
3944
3945         strsep(&str, ".");
3946         if (!str) {
3947                 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
3948                 ret = -EINVAL;
3949                 goto out;
3950         }
3951
3952         action_name = strsep(&str, "(");
3953         if (!action_name || !str) {
3954                 hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0);
3955                 ret = -EINVAL;
3956                 goto out;
3957         }
3958
3959         if (str_has_prefix(action_name, "save")) {
3960                 char *params = strsep(&str, ")");
3961
3962                 if (!params) {
3963                         hist_err(tr, HIST_ERR_NO_SAVE_PARAMS, 0);
3964                         ret = -EINVAL;
3965                         goto out;
3966                 }
3967
3968                 ret = parse_action_params(tr, params, data);
3969                 if (ret)
3970                         goto out;
3971
3972                 if (handler == HANDLER_ONMAX)
3973                         data->track_data.check_val = check_track_val_max;
3974                 else if (handler == HANDLER_ONCHANGE)
3975                         data->track_data.check_val = check_track_val_changed;
3976                 else {
3977                         hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
3978                         ret = -EINVAL;
3979                         goto out;
3980                 }
3981
3982                 data->track_data.save_data = save_track_data_vars;
3983                 data->fn = ontrack_action;
3984                 data->action = ACTION_SAVE;
3985         } else if (str_has_prefix(action_name, "snapshot")) {
3986                 char *params = strsep(&str, ")");
3987
3988                 if (!str) {
3989                         hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(params));
3990                         ret = -EINVAL;
3991                         goto out;
3992                 }
3993
3994                 if (handler == HANDLER_ONMAX)
3995                         data->track_data.check_val = check_track_val_max;
3996                 else if (handler == HANDLER_ONCHANGE)
3997                         data->track_data.check_val = check_track_val_changed;
3998                 else {
3999                         hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name));
4000                         ret = -EINVAL;
4001                         goto out;
4002                 }
4003
4004                 data->track_data.save_data = save_track_data_snapshot;
4005                 data->fn = ontrack_action;
4006                 data->action = ACTION_SNAPSHOT;
4007         } else {
4008                 char *params = strsep(&str, ")");
4009
4010                 if (str_has_prefix(action_name, "trace"))
4011                         data->use_trace_keyword = true;
4012
4013                 if (params) {
4014                         ret = parse_action_params(tr, params, data);
4015                         if (ret)
4016                                 goto out;
4017                 }
4018
4019                 if (handler == HANDLER_ONMAX)
4020                         data->track_data.check_val = check_track_val_max;
4021                 else if (handler == HANDLER_ONCHANGE)
4022                         data->track_data.check_val = check_track_val_changed;
4023
4024                 if (handler != HANDLER_ONMATCH) {
4025                         data->track_data.save_data = action_trace;
4026                         data->fn = ontrack_action;
4027                 } else
4028                         data->fn = action_trace;
4029
4030                 data->action = ACTION_TRACE;
4031         }
4032
4033         data->action_name = kstrdup(action_name, GFP_KERNEL);
4034         if (!data->action_name) {
4035                 ret = -ENOMEM;
4036                 goto out;
4037         }
4038
4039         data->handler = handler;
4040  out:
4041         return ret;
4042 }
4043
4044 static struct action_data *track_data_parse(struct hist_trigger_data *hist_data,
4045                                             char *str, enum handler_id handler)
4046 {
4047         struct action_data *data;
4048         int ret = -EINVAL;
4049         char *var_str;
4050
4051         data = kzalloc(sizeof(*data), GFP_KERNEL);
4052         if (!data)
4053                 return ERR_PTR(-ENOMEM);
4054
4055         var_str = strsep(&str, ")");
4056         if (!var_str || !str) {
4057                 ret = -EINVAL;
4058                 goto free;
4059         }
4060
4061         data->track_data.var_str = kstrdup(var_str, GFP_KERNEL);
4062         if (!data->track_data.var_str) {
4063                 ret = -ENOMEM;
4064                 goto free;
4065         }
4066
4067         ret = action_parse(hist_data->event_file->tr, str, data, handler);
4068         if (ret)
4069                 goto free;
4070  out:
4071         return data;
4072  free:
4073         track_data_destroy(hist_data, data);
4074         data = ERR_PTR(ret);
4075         goto out;
4076 }
4077
4078 static void onmatch_destroy(struct action_data *data)
4079 {
4080         kfree(data->match_data.event);
4081         kfree(data->match_data.event_system);
4082
4083         action_data_destroy(data);
4084 }
4085
4086 static void destroy_field_var(struct field_var *field_var)
4087 {
4088         if (!field_var)
4089                 return;
4090
4091         destroy_hist_field(field_var->var, 0);
4092         destroy_hist_field(field_var->val, 0);
4093
4094         kfree(field_var);
4095 }
4096
4097 static void destroy_field_vars(struct hist_trigger_data *hist_data)
4098 {
4099         unsigned int i;
4100
4101         for (i = 0; i < hist_data->n_field_vars; i++)
4102                 destroy_field_var(hist_data->field_vars[i]);
4103 }
4104
4105 static void save_field_var(struct hist_trigger_data *hist_data,
4106                            struct field_var *field_var)
4107 {
4108         hist_data->field_vars[hist_data->n_field_vars++] = field_var;
4109
4110         if (field_var->val->flags & HIST_FIELD_FL_STRING)
4111                 hist_data->n_field_var_str++;
4112 }
4113
4114
4115 static int check_synth_field(struct synth_event *event,
4116                              struct hist_field *hist_field,
4117                              unsigned int field_pos)
4118 {
4119         struct synth_field *field;
4120
4121         if (field_pos >= event->n_fields)
4122                 return -EINVAL;
4123
4124         field = event->fields[field_pos];
4125
4126         if (strcmp(field->type, hist_field->type) != 0) {
4127                 if (field->size != hist_field->size ||
4128                     field->is_signed != hist_field->is_signed)
4129                         return -EINVAL;
4130         }
4131
4132         return 0;
4133 }
4134
4135 static struct hist_field *
4136 trace_action_find_var(struct hist_trigger_data *hist_data,
4137                       struct action_data *data,
4138                       char *system, char *event, char *var)
4139 {
4140         struct trace_array *tr = hist_data->event_file->tr;
4141         struct hist_field *hist_field;
4142
4143         var++; /* skip '$' */
4144
4145         hist_field = find_target_event_var(hist_data, system, event, var);
4146         if (!hist_field) {
4147                 if (!system && data->handler == HANDLER_ONMATCH) {
4148                         system = data->match_data.event_system;
4149                         event = data->match_data.event;
4150                 }
4151
4152                 hist_field = find_event_var(hist_data, system, event, var);
4153         }
4154
4155         if (!hist_field)
4156                 hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, errpos(var));
4157
4158         return hist_field;
4159 }
4160
4161 static struct hist_field *
4162 trace_action_create_field_var(struct hist_trigger_data *hist_data,
4163                               struct action_data *data, char *system,
4164                               char *event, char *var)
4165 {
4166         struct hist_field *hist_field = NULL;
4167         struct field_var *field_var;
4168
4169         /*
4170          * First try to create a field var on the target event (the
4171          * currently being defined).  This will create a variable for
4172          * unqualified fields on the target event, or if qualified,
4173          * target fields that have qualified names matching the target.
4174          */
4175         field_var = create_target_field_var(hist_data, system, event, var);
4176
4177         if (field_var && !IS_ERR(field_var)) {
4178                 save_field_var(hist_data, field_var);
4179                 hist_field = field_var->var;
4180         } else {
4181                 field_var = NULL;
4182                 /*
4183                  * If no explicit system.event is specfied, default to
4184                  * looking for fields on the onmatch(system.event.xxx)
4185                  * event.
4186                  */
4187                 if (!system && data->handler == HANDLER_ONMATCH) {
4188                         system = data->match_data.event_system;
4189                         event = data->match_data.event;
4190                 }
4191
4192                 /*
4193                  * At this point, we're looking at a field on another
4194                  * event.  Because we can't modify a hist trigger on
4195                  * another event to add a variable for a field, we need
4196                  * to create a new trigger on that event and create the
4197                  * variable at the same time.
4198                  */
4199                 hist_field = create_field_var_hist(hist_data, system, event, var);
4200                 if (IS_ERR(hist_field))
4201                         goto free;
4202         }
4203  out:
4204         return hist_field;
4205  free:
4206         destroy_field_var(field_var);
4207         hist_field = NULL;
4208         goto out;
4209 }
4210
4211 static int trace_action_create(struct hist_trigger_data *hist_data,
4212                                struct action_data *data)
4213 {
4214         struct trace_array *tr = hist_data->event_file->tr;
4215         char *event_name, *param, *system = NULL;
4216         struct hist_field *hist_field, *var_ref;
4217         unsigned int i, var_ref_idx;
4218         unsigned int field_pos = 0;
4219         struct synth_event *event;
4220         char *synth_event_name;
4221         int ret = 0;
4222
4223         lockdep_assert_held(&event_mutex);
4224
4225         if (data->use_trace_keyword)
4226                 synth_event_name = data->synth_event_name;
4227         else
4228                 synth_event_name = data->action_name;
4229
4230         event = find_synth_event(synth_event_name);
4231         if (!event) {
4232                 hist_err(tr, HIST_ERR_SYNTH_EVENT_NOT_FOUND, errpos(synth_event_name));
4233                 return -EINVAL;
4234         }
4235
4236         event->ref++;
4237
4238         var_ref_idx = hist_data->n_var_refs;
4239
4240         for (i = 0; i < data->n_params; i++) {
4241                 char *p;
4242
4243                 p = param = kstrdup(data->params[i], GFP_KERNEL);
4244                 if (!param) {
4245                         ret = -ENOMEM;
4246                         goto err;
4247                 }
4248
4249                 system = strsep(&param, ".");
4250                 if (!param) {
4251                         param = (char *)system;
4252                         system = event_name = NULL;
4253                 } else {
4254                         event_name = strsep(&param, ".");
4255                         if (!param) {
4256                                 kfree(p);
4257                                 ret = -EINVAL;
4258                                 goto err;
4259                         }
4260                 }
4261
4262                 if (param[0] == '$')
4263                         hist_field = trace_action_find_var(hist_data, data,
4264                                                            system, event_name,
4265                                                            param);
4266                 else
4267                         hist_field = trace_action_create_field_var(hist_data,
4268                                                                    data,
4269                                                                    system,
4270                                                                    event_name,
4271                                                                    param);
4272
4273                 if (!hist_field) {
4274                         kfree(p);
4275                         ret = -EINVAL;
4276                         goto err;
4277                 }
4278
4279                 if (check_synth_field(event, hist_field, field_pos) == 0) {
4280                         var_ref = create_var_ref(hist_data, hist_field,
4281                                                  system, event_name);
4282                         if (!var_ref) {
4283                                 kfree(p);
4284                                 ret = -ENOMEM;
4285                                 goto err;
4286                         }
4287
4288                         field_pos++;
4289                         kfree(p);
4290                         continue;
4291                 }
4292
4293                 hist_err(tr, HIST_ERR_SYNTH_TYPE_MISMATCH, errpos(param));
4294                 kfree(p);
4295                 ret = -EINVAL;
4296                 goto err;
4297         }
4298
4299         if (field_pos != event->n_fields) {
4300                 hist_err(tr, HIST_ERR_SYNTH_COUNT_MISMATCH, errpos(event->name));
4301                 ret = -EINVAL;
4302                 goto err;
4303         }
4304
4305         data->synth_event = event;
4306         data->var_ref_idx = var_ref_idx;
4307  out:
4308         return ret;
4309  err:
4310         event->ref--;
4311
4312         goto out;
4313 }
4314
4315 static int action_create(struct hist_trigger_data *hist_data,
4316                          struct action_data *data)
4317 {
4318         struct trace_event_file *file = hist_data->event_file;
4319         struct trace_array *tr = file->tr;
4320         struct track_data *track_data;
4321         struct field_var *field_var;
4322         unsigned int i;
4323         char *param;
4324         int ret = 0;
4325
4326         if (data->action == ACTION_TRACE)
4327                 return trace_action_create(hist_data, data);
4328
4329         if (data->action == ACTION_SNAPSHOT) {
4330                 track_data = track_data_alloc(hist_data->key_size, data, hist_data);
4331                 if (IS_ERR(track_data)) {
4332                         ret = PTR_ERR(track_data);
4333                         goto out;
4334                 }
4335
4336                 ret = tracing_snapshot_cond_enable(file->tr, track_data,
4337                                                    cond_snapshot_update);
4338                 if (ret)
4339                         track_data_free(track_data);
4340
4341                 goto out;
4342         }
4343
4344         if (data->action == ACTION_SAVE) {
4345                 if (hist_data->n_save_vars) {
4346                         ret = -EEXIST;
4347                         hist_err(tr, HIST_ERR_TOO_MANY_SAVE_ACTIONS, 0);
4348                         goto out;
4349                 }
4350
4351                 for (i = 0; i < data->n_params; i++) {
4352                         param = kstrdup(data->params[i], GFP_KERNEL);
4353                         if (!param) {
4354                                 ret = -ENOMEM;
4355                                 goto out;
4356                         }
4357
4358                         field_var = create_target_field_var(hist_data, NULL, NULL, param);
4359                         if (IS_ERR(field_var)) {
4360                                 hist_err(tr, HIST_ERR_FIELD_VAR_CREATE_FAIL,
4361                                          errpos(param));
4362                                 ret = PTR_ERR(field_var);
4363                                 kfree(param);
4364                                 goto out;
4365                         }
4366
4367                         hist_data->save_vars[hist_data->n_save_vars++] = field_var;
4368                         if (field_var->val->flags & HIST_FIELD_FL_STRING)
4369                                 hist_data->n_save_var_str++;
4370                         kfree(param);
4371                 }
4372         }
4373  out:
4374         return ret;
4375 }
4376
4377 static int onmatch_create(struct hist_trigger_data *hist_data,
4378                           struct action_data *data)
4379 {
4380         return action_create(hist_data, data);
4381 }
4382
4383 static struct action_data *onmatch_parse(struct trace_array *tr, char *str)
4384 {
4385         char *match_event, *match_event_system;
4386         struct action_data *data;
4387         int ret = -EINVAL;
4388
4389         data = kzalloc(sizeof(*data), GFP_KERNEL);
4390         if (!data)
4391                 return ERR_PTR(-ENOMEM);
4392
4393         match_event = strsep(&str, ")");
4394         if (!match_event || !str) {
4395                 hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(match_event));
4396                 goto free;
4397         }
4398
4399         match_event_system = strsep(&match_event, ".");
4400         if (!match_event) {
4401                 hist_err(tr, HIST_ERR_SUBSYS_NOT_FOUND, errpos(match_event_system));
4402                 goto free;
4403         }
4404
4405         if (IS_ERR(event_file(tr, match_event_system, match_event))) {
4406                 hist_err(tr, HIST_ERR_INVALID_SUBSYS_EVENT, errpos(match_event));
4407                 goto free;
4408         }
4409
4410         data->match_data.event = kstrdup(match_event, GFP_KERNEL);
4411         if (!data->match_data.event) {
4412                 ret = -ENOMEM;
4413                 goto free;
4414         }
4415
4416         data->match_data.event_system = kstrdup(match_event_system, GFP_KERNEL);
4417         if (!data->match_data.event_system) {
4418                 ret = -ENOMEM;
4419                 goto free;
4420         }
4421
4422         ret = action_parse(tr, str, data, HANDLER_ONMATCH);
4423         if (ret)
4424                 goto free;
4425  out:
4426         return data;
4427  free:
4428         onmatch_destroy(data);
4429         data = ERR_PTR(ret);
4430         goto out;
4431 }
4432
4433 static int create_hitcount_val(struct hist_trigger_data *hist_data)
4434 {
4435         hist_data->fields[HITCOUNT_IDX] =
4436                 create_hist_field(hist_data, NULL, HIST_FIELD_FL_HITCOUNT, NULL);
4437         if (!hist_data->fields[HITCOUNT_IDX])
4438                 return -ENOMEM;
4439
4440         hist_data->n_vals++;
4441         hist_data->n_fields++;
4442
4443         if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX))
4444                 return -EINVAL;
4445
4446         return 0;
4447 }
4448
4449 static int __create_val_field(struct hist_trigger_data *hist_data,
4450                               unsigned int val_idx,
4451                               struct trace_event_file *file,
4452                               char *var_name, char *field_str,
4453                               unsigned long flags)
4454 {
4455         struct hist_field *hist_field;
4456         int ret = 0;
4457
4458         hist_field = parse_expr(hist_data, file, field_str, flags, var_name, 0);
4459         if (IS_ERR(hist_field)) {
4460                 ret = PTR_ERR(hist_field);
4461                 goto out;
4462         }
4463
4464         hist_data->fields[val_idx] = hist_field;
4465
4466         ++hist_data->n_vals;
4467         ++hist_data->n_fields;
4468
4469         if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
4470                 ret = -EINVAL;
4471  out:
4472         return ret;
4473 }
4474
4475 static int create_val_field(struct hist_trigger_data *hist_data,
4476                             unsigned int val_idx,
4477                             struct trace_event_file *file,
4478                             char *field_str)
4479 {
4480         if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX))
4481                 return -EINVAL;
4482
4483         return __create_val_field(hist_data, val_idx, file, NULL, field_str, 0);
4484 }
4485
4486 static int create_var_field(struct hist_trigger_data *hist_data,
4487                             unsigned int val_idx,
4488                             struct trace_event_file *file,
4489                             char *var_name, char *expr_str)
4490 {
4491         struct trace_array *tr = hist_data->event_file->tr;
4492         unsigned long flags = 0;
4493
4494         if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX))
4495                 return -EINVAL;
4496
4497         if (find_var(hist_data, file, var_name) && !hist_data->remove) {
4498                 hist_err(tr, HIST_ERR_DUPLICATE_VAR, errpos(var_name));
4499                 return -EINVAL;
4500         }
4501
4502         flags |= HIST_FIELD_FL_VAR;
4503         hist_data->n_vars++;
4504         if (WARN_ON(hist_data->n_vars > TRACING_MAP_VARS_MAX))
4505                 return -EINVAL;
4506
4507         return __create_val_field(hist_data, val_idx, file, var_name, expr_str, flags);
4508 }
4509
4510 static int create_val_fields(struct hist_trigger_data *hist_data,
4511                              struct trace_event_file *file)
4512 {
4513         char *fields_str, *field_str;
4514         unsigned int i, j = 1;
4515         int ret;
4516
4517         ret = create_hitcount_val(hist_data);
4518         if (ret)
4519                 goto out;
4520
4521         fields_str = hist_data->attrs->vals_str;
4522         if (!fields_str)
4523                 goto out;
4524
4525         strsep(&fields_str, "=");
4526         if (!fields_str)
4527                 goto out;
4528
4529         for (i = 0, j = 1; i < TRACING_MAP_VALS_MAX &&
4530                      j < TRACING_MAP_VALS_MAX; i++) {
4531                 field_str = strsep(&fields_str, ",");
4532                 if (!field_str)
4533                         break;
4534
4535                 if (strcmp(field_str, "hitcount") == 0)
4536                         continue;
4537
4538                 ret = create_val_field(hist_data, j++, file, field_str);
4539                 if (ret)
4540                         goto out;
4541         }
4542
4543         if (fields_str && (strcmp(fields_str, "hitcount") != 0))
4544                 ret = -EINVAL;
4545  out:
4546         return ret;
4547 }
4548
4549 static int create_key_field(struct hist_trigger_data *hist_data,
4550                             unsigned int key_idx,
4551                             unsigned int key_offset,
4552                             struct trace_event_file *file,
4553                             char *field_str)
4554 {
4555         struct trace_array *tr = hist_data->event_file->tr;
4556         struct hist_field *hist_field = NULL;
4557         unsigned long flags = 0;
4558         unsigned int key_size;
4559         int ret = 0;
4560
4561         if (WARN_ON(key_idx >= HIST_FIELDS_MAX))
4562                 return -EINVAL;
4563
4564         flags |= HIST_FIELD_FL_KEY;
4565
4566         if (strcmp(field_str, "stacktrace") == 0) {
4567                 flags |= HIST_FIELD_FL_STACKTRACE;
4568                 key_size = sizeof(unsigned long) * HIST_STACKTRACE_DEPTH;
4569                 hist_field = create_hist_field(hist_data, NULL, flags, NULL);
4570         } else {
4571                 hist_field = parse_expr(hist_data, file, field_str, flags,
4572                                         NULL, 0);
4573                 if (IS_ERR(hist_field)) {
4574                         ret = PTR_ERR(hist_field);
4575                         goto out;
4576                 }
4577
4578                 if (field_has_hist_vars(hist_field, 0)) {
4579                         hist_err(tr, HIST_ERR_INVALID_REF_KEY, errpos(field_str));
4580                         destroy_hist_field(hist_field, 0);
4581                         ret = -EINVAL;
4582                         goto out;
4583                 }
4584
4585                 key_size = hist_field->size;
4586         }
4587
4588         hist_data->fields[key_idx] = hist_field;
4589
4590         key_size = ALIGN(key_size, sizeof(u64));
4591         hist_data->fields[key_idx]->size = key_size;
4592         hist_data->fields[key_idx]->offset = key_offset;
4593
4594         hist_data->key_size += key_size;
4595
4596         if (hist_data->key_size > HIST_KEY_SIZE_MAX) {
4597                 ret = -EINVAL;
4598                 goto out;
4599         }
4600
4601         hist_data->n_keys++;
4602         hist_data->n_fields++;
4603
4604         if (WARN_ON(hist_data->n_keys > TRACING_MAP_KEYS_MAX))
4605                 return -EINVAL;
4606
4607         ret = key_size;
4608  out:
4609         return ret;
4610 }
4611
4612 static int create_key_fields(struct hist_trigger_data *hist_data,
4613                              struct trace_event_file *file)
4614 {
4615         unsigned int i, key_offset = 0, n_vals = hist_data->n_vals;
4616         char *fields_str, *field_str;
4617         int ret = -EINVAL;
4618
4619         fields_str = hist_data->attrs->keys_str;
4620         if (!fields_str)
4621                 goto out;
4622
4623         strsep(&fields_str, "=");
4624         if (!fields_str)
4625                 goto out;
4626
4627         for (i = n_vals; i < n_vals + TRACING_MAP_KEYS_MAX; i++) {
4628                 field_str = strsep(&fields_str, ",");
4629                 if (!field_str)
4630                         break;
4631                 ret = create_key_field(hist_data, i, key_offset,
4632                                        file, field_str);
4633                 if (ret < 0)
4634                         goto out;
4635                 key_offset += ret;
4636         }
4637         if (fields_str) {
4638                 ret = -EINVAL;
4639                 goto out;
4640         }
4641         ret = 0;
4642  out:
4643         return ret;
4644 }
4645
4646 static int create_var_fields(struct hist_trigger_data *hist_data,
4647                              struct trace_event_file *file)
4648 {
4649         unsigned int i, j = hist_data->n_vals;
4650         int ret = 0;
4651
4652         unsigned int n_vars = hist_data->attrs->var_defs.n_vars;
4653
4654         for (i = 0; i < n_vars; i++) {
4655                 char *var_name = hist_data->attrs->var_defs.name[i];
4656                 char *expr = hist_data->attrs->var_defs.expr[i];
4657
4658                 ret = create_var_field(hist_data, j++, file, var_name, expr);
4659                 if (ret)
4660                         goto out;
4661         }
4662  out:
4663         return ret;
4664 }
4665
4666 static void free_var_defs(struct hist_trigger_data *hist_data)
4667 {
4668         unsigned int i;
4669
4670         for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) {
4671                 kfree(hist_data->attrs->var_defs.name[i]);
4672                 kfree(hist_data->attrs->var_defs.expr[i]);
4673         }
4674
4675         hist_data->attrs->var_defs.n_vars = 0;
4676 }
4677
4678 static int parse_var_defs(struct hist_trigger_data *hist_data)
4679 {
4680         struct trace_array *tr = hist_data->event_file->tr;
4681         char *s, *str, *var_name, *field_str;
4682         unsigned int i, j, n_vars = 0;
4683         int ret = 0;
4684
4685         for (i = 0; i < hist_data->attrs->n_assignments; i++) {
4686                 str = hist_data->attrs->assignment_str[i];
4687                 for (j = 0; j < TRACING_MAP_VARS_MAX; j++) {
4688                         field_str = strsep(&str, ",");
4689                         if (!field_str)
4690                                 break;
4691
4692                         var_name = strsep(&field_str, "=");
4693                         if (!var_name || !field_str) {
4694                                 hist_err(tr, HIST_ERR_MALFORMED_ASSIGNMENT,
4695                                          errpos(var_name));
4696                                 ret = -EINVAL;
4697                                 goto free;
4698                         }
4699
4700                         if (n_vars == TRACING_MAP_VARS_MAX) {
4701                                 hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(var_name));
4702                                 ret = -EINVAL;
4703                                 goto free;
4704                         }
4705
4706                         s = kstrdup(var_name, GFP_KERNEL);
4707                         if (!s) {
4708                                 ret = -ENOMEM;
4709                                 goto free;
4710                         }
4711                         hist_data->attrs->var_defs.name[n_vars] = s;
4712
4713                         s = kstrdup(field_str, GFP_KERNEL);
4714                         if (!s) {
4715                                 kfree(hist_data->attrs->var_defs.name[n_vars]);
4716                                 ret = -ENOMEM;
4717                                 goto free;
4718                         }
4719                         hist_data->attrs->var_defs.expr[n_vars++] = s;
4720
4721                         hist_data->attrs->var_defs.n_vars = n_vars;
4722                 }
4723         }
4724
4725         return ret;
4726  free:
4727         free_var_defs(hist_data);
4728
4729         return ret;
4730 }
4731
4732 static int create_hist_fields(struct hist_trigger_data *hist_data,
4733                               struct trace_event_file *file)
4734 {
4735         int ret;
4736
4737         ret = parse_var_defs(hist_data);
4738         if (ret)
4739                 goto out;
4740
4741         ret = create_val_fields(hist_data, file);
4742         if (ret)
4743                 goto out;
4744
4745         ret = create_var_fields(hist_data, file);
4746         if (ret)
4747                 goto out;
4748
4749         ret = create_key_fields(hist_data, file);
4750         if (ret)
4751                 goto out;
4752  out:
4753         free_var_defs(hist_data);
4754
4755         return ret;
4756 }
4757
4758 static int is_descending(const char *str)
4759 {
4760         if (!str)
4761                 return 0;
4762
4763         if (strcmp(str, "descending") == 0)
4764                 return 1;
4765
4766         if (strcmp(str, "ascending") == 0)
4767                 return 0;
4768
4769         return -EINVAL;
4770 }
4771
4772 static int create_sort_keys(struct hist_trigger_data *hist_data)
4773 {
4774         char *fields_str = hist_data->attrs->sort_key_str;
4775         struct tracing_map_sort_key *sort_key;
4776         int descending, ret = 0;
4777         unsigned int i, j, k;
4778
4779         hist_data->n_sort_keys = 1; /* we always have at least one, hitcount */
4780
4781         if (!fields_str)
4782                 goto out;
4783
4784         strsep(&fields_str, "=");
4785         if (!fields_str) {
4786                 ret = -EINVAL;
4787                 goto out;
4788         }
4789
4790         for (i = 0; i < TRACING_MAP_SORT_KEYS_MAX; i++) {
4791                 struct hist_field *hist_field;
4792                 char *field_str, *field_name;
4793                 const char *test_name;
4794
4795                 sort_key = &hist_data->sort_keys[i];
4796
4797                 field_str = strsep(&fields_str, ",");
4798                 if (!field_str) {
4799                         if (i == 0)
4800                                 ret = -EINVAL;
4801                         break;
4802                 }
4803
4804                 if ((i == TRACING_MAP_SORT_KEYS_MAX - 1) && fields_str) {
4805                         ret = -EINVAL;
4806                         break;
4807                 }
4808
4809                 field_name = strsep(&field_str, ".");
4810                 if (!field_name) {
4811                         ret = -EINVAL;
4812                         break;
4813                 }
4814
4815                 if (strcmp(field_name, "hitcount") == 0) {
4816                         descending = is_descending(field_str);
4817                         if (descending < 0) {
4818                                 ret = descending;
4819                                 break;
4820                         }
4821                         sort_key->descending = descending;
4822                         continue;
4823                 }
4824
4825                 for (j = 1, k = 1; j < hist_data->n_fields; j++) {
4826                         unsigned int idx;
4827
4828                         hist_field = hist_data->fields[j];
4829                         if (hist_field->flags & HIST_FIELD_FL_VAR)
4830                                 continue;
4831
4832                         idx = k++;
4833
4834                         test_name = hist_field_name(hist_field, 0);
4835
4836                         if (strcmp(field_name, test_name) == 0) {
4837                                 sort_key->field_idx = idx;
4838                                 descending = is_descending(field_str);
4839                                 if (descending < 0) {
4840                                         ret = descending;
4841                                         goto out;
4842                                 }
4843                                 sort_key->descending = descending;
4844                                 break;
4845                         }
4846                 }
4847                 if (j == hist_data->n_fields) {
4848                         ret = -EINVAL;
4849                         break;
4850                 }
4851         }
4852
4853         hist_data->n_sort_keys = i;
4854  out:
4855         return ret;
4856 }
4857
4858 static void destroy_actions(struct hist_trigger_data *hist_data)
4859 {
4860         unsigned int i;
4861
4862         for (i = 0; i < hist_data->n_actions; i++) {
4863                 struct action_data *data = hist_data->actions[i];
4864
4865                 if (data->handler == HANDLER_ONMATCH)
4866                         onmatch_destroy(data);
4867                 else if (data->handler == HANDLER_ONMAX ||
4868                          data->handler == HANDLER_ONCHANGE)
4869                         track_data_destroy(hist_data, data);
4870                 else
4871                         kfree(data);
4872         }
4873 }
4874
4875 static int parse_actions(struct hist_trigger_data *hist_data)
4876 {
4877         struct trace_array *tr = hist_data->event_file->tr;
4878         struct action_data *data;
4879         unsigned int i;
4880         int ret = 0;
4881         char *str;
4882         int len;
4883
4884         for (i = 0; i < hist_data->attrs->n_actions; i++) {
4885                 str = hist_data->attrs->action_str[i];
4886
4887                 if ((len = str_has_prefix(str, "onmatch("))) {
4888                         char *action_str = str + len;
4889
4890                         data = onmatch_parse(tr, action_str);
4891                         if (IS_ERR(data)) {
4892                                 ret = PTR_ERR(data);
4893                                 break;
4894                         }
4895                 } else if ((len = str_has_prefix(str, "onmax("))) {
4896                         char *action_str = str + len;
4897
4898                         data = track_data_parse(hist_data, action_str,
4899                                                 HANDLER_ONMAX);
4900                         if (IS_ERR(data)) {
4901                                 ret = PTR_ERR(data);
4902                                 break;
4903                         }
4904                 } else if ((len = str_has_prefix(str, "onchange("))) {
4905                         char *action_str = str + len;
4906
4907                         data = track_data_parse(hist_data, action_str,
4908                                                 HANDLER_ONCHANGE);
4909                         if (IS_ERR(data)) {
4910                                 ret = PTR_ERR(data);
4911                                 break;
4912                         }
4913                 } else {
4914                         ret = -EINVAL;
4915                         break;
4916                 }
4917
4918                 hist_data->actions[hist_data->n_actions++] = data;
4919         }
4920
4921         return ret;
4922 }
4923
4924 static int create_actions(struct hist_trigger_data *hist_data)
4925 {
4926         struct action_data *data;
4927         unsigned int i;
4928         int ret = 0;
4929
4930         for (i = 0; i < hist_data->attrs->n_actions; i++) {
4931                 data = hist_data->actions[i];
4932
4933                 if (data->handler == HANDLER_ONMATCH) {
4934                         ret = onmatch_create(hist_data, data);
4935                         if (ret)
4936                                 break;
4937                 } else if (data->handler == HANDLER_ONMAX ||
4938                            data->handler == HANDLER_ONCHANGE) {
4939                         ret = track_data_create(hist_data, data);
4940                         if (ret)
4941                                 break;
4942                 } else {
4943                         ret = -EINVAL;
4944                         break;
4945                 }
4946         }
4947
4948         return ret;
4949 }
4950
4951 static void print_actions(struct seq_file *m,
4952                           struct hist_trigger_data *hist_data,
4953                           struct tracing_map_elt *elt)
4954 {
4955         unsigned int i;
4956
4957         for (i = 0; i < hist_data->n_actions; i++) {
4958                 struct action_data *data = hist_data->actions[i];
4959
4960                 if (data->action == ACTION_SNAPSHOT)
4961                         continue;
4962
4963                 if (data->handler == HANDLER_ONMAX ||
4964                     data->handler == HANDLER_ONCHANGE)
4965                         track_data_print(m, hist_data, elt, data);
4966         }
4967 }
4968
4969 static void print_action_spec(struct seq_file *m,
4970                               struct hist_trigger_data *hist_data,
4971                               struct action_data *data)
4972 {
4973         unsigned int i;
4974
4975         if (data->action == ACTION_SAVE) {
4976                 for (i = 0; i < hist_data->n_save_vars; i++) {
4977                         seq_printf(m, "%s", hist_data->save_vars[i]->var->var.name);
4978                         if (i < hist_data->n_save_vars - 1)
4979                                 seq_puts(m, ",");
4980                 }
4981         } else if (data->action == ACTION_TRACE) {
4982                 if (data->use_trace_keyword)
4983                         seq_printf(m, "%s", data->synth_event_name);
4984                 for (i = 0; i < data->n_params; i++) {
4985                         if (i || data->use_trace_keyword)
4986                                 seq_puts(m, ",");
4987                         seq_printf(m, "%s", data->params[i]);
4988                 }
4989         }
4990 }
4991
4992 static void print_track_data_spec(struct seq_file *m,
4993                                   struct hist_trigger_data *hist_data,
4994                                   struct action_data *data)
4995 {
4996         if (data->handler == HANDLER_ONMAX)
4997                 seq_puts(m, ":onmax(");
4998         else if (data->handler == HANDLER_ONCHANGE)
4999                 seq_puts(m, ":onchange(");
5000         seq_printf(m, "%s", data->track_data.var_str);
5001         seq_printf(m, ").%s(", data->action_name);
5002
5003         print_action_spec(m, hist_data, data);
5004
5005         seq_puts(m, ")");
5006 }
5007
5008 static void print_onmatch_spec(struct seq_file *m,
5009                                struct hist_trigger_data *hist_data,
5010                                struct action_data *data)
5011 {
5012         seq_printf(m, ":onmatch(%s.%s).", data->match_data.event_system,
5013                    data->match_data.event);
5014
5015         seq_printf(m, "%s(", data->action_name);
5016
5017         print_action_spec(m, hist_data, data);
5018
5019         seq_puts(m, ")");
5020 }
5021
5022 static bool actions_match(struct hist_trigger_data *hist_data,
5023                           struct hist_trigger_data *hist_data_test)
5024 {
5025         unsigned int i, j;
5026
5027         if (hist_data->n_actions != hist_data_test->n_actions)
5028                 return false;
5029
5030         for (i = 0; i < hist_data->n_actions; i++) {
5031                 struct action_data *data = hist_data->actions[i];
5032                 struct action_data *data_test = hist_data_test->actions[i];
5033                 char *action_name, *action_name_test;
5034
5035                 if (data->handler != data_test->handler)
5036                         return false;
5037                 if (data->action != data_test->action)
5038                         return false;
5039
5040                 if (data->n_params != data_test->n_params)
5041                         return false;
5042
5043                 for (j = 0; j < data->n_params; j++) {
5044                         if (strcmp(data->params[j], data_test->params[j]) != 0)
5045                                 return false;
5046                 }
5047
5048                 if (data->use_trace_keyword)
5049                         action_name = data->synth_event_name;
5050                 else
5051                         action_name = data->action_name;
5052
5053                 if (data_test->use_trace_keyword)
5054                         action_name_test = data_test->synth_event_name;
5055                 else
5056                         action_name_test = data_test->action_name;
5057
5058                 if (strcmp(action_name, action_name_test) != 0)
5059                         return false;
5060
5061                 if (data->handler == HANDLER_ONMATCH) {
5062                         if (strcmp(data->match_data.event_system,
5063                                    data_test->match_data.event_system) != 0)
5064                                 return false;
5065                         if (strcmp(data->match_data.event,
5066                                    data_test->match_data.event) != 0)
5067                                 return false;
5068                 } else if (data->handler == HANDLER_ONMAX ||
5069                            data->handler == HANDLER_ONCHANGE) {
5070                         if (strcmp(data->track_data.var_str,
5071                                    data_test->track_data.var_str) != 0)
5072                                 return false;
5073                 }
5074         }
5075
5076         return true;
5077 }
5078
5079
5080 static void print_actions_spec(struct seq_file *m,
5081                                struct hist_trigger_data *hist_data)
5082 {
5083         unsigned int i;
5084
5085         for (i = 0; i < hist_data->n_actions; i++) {
5086                 struct action_data *data = hist_data->actions[i];
5087
5088                 if (data->handler == HANDLER_ONMATCH)
5089                         print_onmatch_spec(m, hist_data, data);
5090                 else if (data->handler == HANDLER_ONMAX ||
5091                          data->handler == HANDLER_ONCHANGE)
5092                         print_track_data_spec(m, hist_data, data);
5093         }
5094 }
5095
5096 static void destroy_field_var_hists(struct hist_trigger_data *hist_data)
5097 {
5098         unsigned int i;
5099
5100         for (i = 0; i < hist_data->n_field_var_hists; i++) {
5101                 kfree(hist_data->field_var_hists[i]->cmd);
5102                 kfree(hist_data->field_var_hists[i]);
5103         }
5104 }
5105
5106 static void destroy_hist_data(struct hist_trigger_data *hist_data)
5107 {
5108         if (!hist_data)
5109                 return;
5110
5111         destroy_hist_trigger_attrs(hist_data->attrs);
5112         destroy_hist_fields(hist_data);
5113         tracing_map_destroy(hist_data->map);
5114
5115         destroy_actions(hist_data);
5116         destroy_field_vars(hist_data);
5117         destroy_field_var_hists(hist_data);
5118
5119         kfree(hist_data);
5120 }
5121
5122 static int create_tracing_map_fields(struct hist_trigger_data *hist_data)
5123 {
5124         struct tracing_map *map = hist_data->map;
5125         struct ftrace_event_field *field;
5126         struct hist_field *hist_field;
5127         int i, idx = 0;
5128
5129         for_each_hist_field(i, hist_data) {
5130                 hist_field = hist_data->fields[i];
5131                 if (hist_field->flags & HIST_FIELD_FL_KEY) {
5132                         tracing_map_cmp_fn_t cmp_fn;
5133
5134                         field = hist_field->field;
5135
5136                         if (hist_field->flags & HIST_FIELD_FL_STACKTRACE)
5137                                 cmp_fn = tracing_map_cmp_none;
5138                         else if (!field)
5139                                 cmp_fn = tracing_map_cmp_num(hist_field->size,
5140                                                              hist_field->is_signed);
5141                         else if (is_string_field(field))
5142                                 cmp_fn = tracing_map_cmp_string;
5143                         else
5144                                 cmp_fn = tracing_map_cmp_num(field->size,
5145                                                              field->is_signed);
5146                         idx = tracing_map_add_key_field(map,
5147                                                         hist_field->offset,
5148                                                         cmp_fn);
5149                 } else if (!(hist_field->flags & HIST_FIELD_FL_VAR))
5150                         idx = tracing_map_add_sum_field(map);
5151
5152                 if (idx < 0)
5153                         return idx;
5154
5155                 if (hist_field->flags & HIST_FIELD_FL_VAR) {
5156                         idx = tracing_map_add_var(map);
5157                         if (idx < 0)
5158                                 return idx;
5159                         hist_field->var.idx = idx;
5160                         hist_field->var.hist_data = hist_data;
5161                 }
5162         }
5163
5164         return 0;
5165 }
5166
5167 static struct hist_trigger_data *
5168 create_hist_data(unsigned int map_bits,
5169                  struct hist_trigger_attrs *attrs,
5170                  struct trace_event_file *file,
5171                  bool remove)
5172 {
5173         const struct tracing_map_ops *map_ops = NULL;
5174         struct hist_trigger_data *hist_data;
5175         int ret = 0;
5176
5177         hist_data = kzalloc(sizeof(*hist_data), GFP_KERNEL);
5178         if (!hist_data)
5179                 return ERR_PTR(-ENOMEM);
5180
5181         hist_data->attrs = attrs;
5182         hist_data->remove = remove;
5183         hist_data->event_file = file;
5184
5185         ret = parse_actions(hist_data);
5186         if (ret)
5187                 goto free;
5188
5189         ret = create_hist_fields(hist_data, file);
5190         if (ret)
5191                 goto free;
5192
5193         ret = create_sort_keys(hist_data);
5194         if (ret)
5195                 goto free;
5196
5197         map_ops = &hist_trigger_elt_data_ops;
5198
5199         hist_data->map = tracing_map_create(map_bits, hist_data->key_size,
5200                                             map_ops, hist_data);
5201         if (IS_ERR(hist_data->map)) {
5202                 ret = PTR_ERR(hist_data->map);
5203                 hist_data->map = NULL;
5204                 goto free;
5205         }
5206
5207         ret = create_tracing_map_fields(hist_data);
5208         if (ret)
5209                 goto free;
5210  out:
5211         return hist_data;
5212  free:
5213         hist_data->attrs = NULL;
5214
5215         destroy_hist_data(hist_data);
5216
5217         hist_data = ERR_PTR(ret);
5218
5219         goto out;
5220 }
5221
5222 static void hist_trigger_elt_update(struct hist_trigger_data *hist_data,
5223                                     struct tracing_map_elt *elt, void *rec,
5224                                     struct ring_buffer_event *rbe,
5225                                     u64 *var_ref_vals)
5226 {
5227         struct hist_elt_data *elt_data;
5228         struct hist_field *hist_field;
5229         unsigned int i, var_idx;
5230         u64 hist_val;
5231
5232         elt_data = elt->private_data;
5233         elt_data->var_ref_vals = var_ref_vals;
5234
5235         for_each_hist_val_field(i, hist_data) {
5236                 hist_field = hist_data->fields[i];
5237                 hist_val = hist_field->fn(hist_field, elt, rbe, rec);
5238                 if (hist_field->flags & HIST_FIELD_FL_VAR) {
5239                         var_idx = hist_field->var.idx;
5240                         tracing_map_set_var(elt, var_idx, hist_val);
5241                         continue;
5242                 }
5243                 tracing_map_update_sum(elt, i, hist_val);
5244         }
5245
5246         for_each_hist_key_field(i, hist_data) {
5247                 hist_field = hist_data->fields[i];
5248                 if (hist_field->flags & HIST_FIELD_FL_VAR) {
5249                         hist_val = hist_field->fn(hist_field, elt, rbe, rec);
5250                         var_idx = hist_field->var.idx;
5251                         tracing_map_set_var(elt, var_idx, hist_val);
5252                 }
5253         }
5254
5255         update_field_vars(hist_data, elt, rbe, rec);
5256 }
5257
5258 static inline void add_to_key(char *compound_key, void *key,
5259                               struct hist_field *key_field, void *rec)
5260 {
5261         size_t size = key_field->size;
5262
5263         if (key_field->flags & HIST_FIELD_FL_STRING) {
5264                 struct ftrace_event_field *field;
5265
5266                 field = key_field->field;
5267                 if (field->filter_type == FILTER_DYN_STRING)
5268                         size = *(u32 *)(rec + field->offset) >> 16;
5269                 else if (field->filter_type == FILTER_PTR_STRING)
5270                         size = strlen(key);
5271                 else if (field->filter_type == FILTER_STATIC_STRING)
5272                         size = field->size;
5273
5274                 /* ensure NULL-termination */
5275                 if (size > key_field->size - 1)
5276                         size = key_field->size - 1;
5277
5278                 strncpy(compound_key + key_field->offset, (char *)key, size);
5279         } else
5280                 memcpy(compound_key + key_field->offset, key, size);
5281 }
5282
5283 static void
5284 hist_trigger_actions(struct hist_trigger_data *hist_data,
5285                      struct tracing_map_elt *elt, void *rec,
5286                      struct ring_buffer_event *rbe, void *key,
5287                      u64 *var_ref_vals)
5288 {
5289         struct action_data *data;
5290         unsigned int i;
5291
5292         for (i = 0; i < hist_data->n_actions; i++) {
5293                 data = hist_data->actions[i];
5294                 data->fn(hist_data, elt, rec, rbe, key, data, var_ref_vals);
5295         }
5296 }
5297
5298 static void event_hist_trigger(struct event_trigger_data *data, void *rec,
5299                                struct ring_buffer_event *rbe)
5300 {
5301         struct hist_trigger_data *hist_data = data->private_data;
5302         bool use_compound_key = (hist_data->n_keys > 1);
5303         unsigned long entries[HIST_STACKTRACE_DEPTH];
5304         u64 var_ref_vals[TRACING_MAP_VARS_MAX];
5305         char compound_key[HIST_KEY_SIZE_MAX];
5306         struct tracing_map_elt *elt = NULL;
5307         struct hist_field *key_field;
5308         u64 field_contents;
5309         void *key = NULL;
5310         unsigned int i;
5311
5312         memset(compound_key, 0, hist_data->key_size);
5313
5314         for_each_hist_key_field(i, hist_data) {
5315                 key_field = hist_data->fields[i];
5316
5317                 if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
5318                         memset(entries, 0, HIST_STACKTRACE_SIZE);
5319                         stack_trace_save(entries, HIST_STACKTRACE_DEPTH,
5320                                          HIST_STACKTRACE_SKIP);
5321                         key = entries;
5322                 } else {
5323                         field_contents = key_field->fn(key_field, elt, rbe, rec);
5324                         if (key_field->flags & HIST_FIELD_FL_STRING) {
5325                                 key = (void *)(unsigned long)field_contents;
5326                                 use_compound_key = true;
5327                         } else
5328                                 key = (void *)&field_contents;
5329                 }
5330
5331                 if (use_compound_key)
5332                         add_to_key(compound_key, key, key_field, rec);
5333         }
5334
5335         if (use_compound_key)
5336                 key = compound_key;
5337
5338         if (hist_data->n_var_refs &&
5339             !resolve_var_refs(hist_data, key, var_ref_vals, false))
5340                 return;
5341
5342         elt = tracing_map_insert(hist_data->map, key);
5343         if (!elt)
5344                 return;
5345
5346         hist_trigger_elt_update(hist_data, elt, rec, rbe, var_ref_vals);
5347
5348         if (resolve_var_refs(hist_data, key, var_ref_vals, true))
5349                 hist_trigger_actions(hist_data, elt, rec, rbe, key, var_ref_vals);
5350 }
5351
5352 static void hist_trigger_stacktrace_print(struct seq_file *m,
5353                                           unsigned long *stacktrace_entries,
5354                                           unsigned int max_entries)
5355 {
5356         char str[KSYM_SYMBOL_LEN];
5357         unsigned int spaces = 8;
5358         unsigned int i;
5359
5360         for (i = 0; i < max_entries; i++) {
5361                 if (!stacktrace_entries[i])
5362                         return;
5363
5364                 seq_printf(m, "%*c", 1 + spaces, ' ');
5365                 sprint_symbol(str, stacktrace_entries[i]);
5366                 seq_printf(m, "%s\n", str);
5367         }
5368 }
5369
5370 static void hist_trigger_print_key(struct seq_file *m,
5371                                    struct hist_trigger_data *hist_data,
5372                                    void *key,
5373                                    struct tracing_map_elt *elt)
5374 {
5375         struct hist_field *key_field;
5376         char str[KSYM_SYMBOL_LEN];
5377         bool multiline = false;
5378         const char *field_name;
5379         unsigned int i;
5380         u64 uval;
5381
5382         seq_puts(m, "{ ");
5383
5384         for_each_hist_key_field(i, hist_data) {
5385                 key_field = hist_data->fields[i];
5386
5387                 if (i > hist_data->n_vals)
5388                         seq_puts(m, ", ");
5389
5390                 field_name = hist_field_name(key_field, 0);
5391
5392                 if (key_field->flags & HIST_FIELD_FL_HEX) {
5393                         uval = *(u64 *)(key + key_field->offset);
5394                         seq_printf(m, "%s: %llx", field_name, uval);
5395                 } else if (key_field->flags & HIST_FIELD_FL_SYM) {
5396                         uval = *(u64 *)(key + key_field->offset);
5397                         sprint_symbol_no_offset(str, uval);
5398                         seq_printf(m, "%s: [%llx] %-45s", field_name,
5399                                    uval, str);
5400                 } else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET) {
5401                         uval = *(u64 *)(key + key_field->offset);
5402                         sprint_symbol(str, uval);
5403                         seq_printf(m, "%s: [%llx] %-55s", field_name,
5404                                    uval, str);
5405                 } else if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
5406                         struct hist_elt_data *elt_data = elt->private_data;
5407                         char *comm;
5408
5409                         if (WARN_ON_ONCE(!elt_data))
5410                                 return;
5411
5412                         comm = elt_data->comm;
5413
5414                         uval = *(u64 *)(key + key_field->offset);
5415                         seq_printf(m, "%s: %-16s[%10llu]", field_name,
5416                                    comm, uval);
5417                 } else if (key_field->flags & HIST_FIELD_FL_SYSCALL) {
5418                         const char *syscall_name;
5419
5420                         uval = *(u64 *)(key + key_field->offset);
5421                         syscall_name = get_syscall_name(uval);
5422                         if (!syscall_name)
5423                                 syscall_name = "unknown_syscall";
5424
5425                         seq_printf(m, "%s: %-30s[%3llu]", field_name,
5426                                    syscall_name, uval);
5427                 } else if (key_field->flags & HIST_FIELD_FL_STACKTRACE) {
5428                         seq_puts(m, "stacktrace:\n");
5429                         hist_trigger_stacktrace_print(m,
5430                                                       key + key_field->offset,
5431                                                       HIST_STACKTRACE_DEPTH);
5432                         multiline = true;
5433                 } else if (key_field->flags & HIST_FIELD_FL_LOG2) {
5434                         seq_printf(m, "%s: ~ 2^%-2llu", field_name,
5435                                    *(u64 *)(key + key_field->offset));
5436                 } else if (key_field->flags & HIST_FIELD_FL_STRING) {
5437                         seq_printf(m, "%s: %-50s", field_name,
5438                                    (char *)(key + key_field->offset));
5439                 } else {
5440                         uval = *(u64 *)(key + key_field->offset);
5441                         seq_printf(m, "%s: %10llu", field_name, uval);
5442                 }
5443         }
5444
5445         if (!multiline)
5446                 seq_puts(m, " ");
5447
5448         seq_puts(m, "}");
5449 }
5450
5451 static void hist_trigger_entry_print(struct seq_file *m,
5452                                      struct hist_trigger_data *hist_data,
5453                                      void *key,
5454                                      struct tracing_map_elt *elt)
5455 {
5456         const char *field_name;
5457         unsigned int i;
5458
5459         hist_trigger_print_key(m, hist_data, key, elt);
5460
5461         seq_printf(m, " hitcount: %10llu",
5462                    tracing_map_read_sum(elt, HITCOUNT_IDX));
5463
5464         for (i = 1; i < hist_data->n_vals; i++) {
5465                 field_name = hist_field_name(hist_data->fields[i], 0);
5466
5467                 if (hist_data->fields[i]->flags & HIST_FIELD_FL_VAR ||
5468                     hist_data->fields[i]->flags & HIST_FIELD_FL_EXPR)
5469                         continue;
5470
5471                 if (hist_data->fields[i]->flags & HIST_FIELD_FL_HEX) {
5472                         seq_printf(m, "  %s: %10llx", field_name,
5473                                    tracing_map_read_sum(elt, i));
5474                 } else {
5475                         seq_printf(m, "  %s: %10llu", field_name,
5476                                    tracing_map_read_sum(elt, i));
5477                 }
5478         }
5479
5480         print_actions(m, hist_data, elt);
5481
5482         seq_puts(m, "\n");
5483 }
5484
5485 static int print_entries(struct seq_file *m,
5486                          struct hist_trigger_data *hist_data)
5487 {
5488         struct tracing_map_sort_entry **sort_entries = NULL;
5489         struct tracing_map *map = hist_data->map;
5490         int i, n_entries;
5491
5492         n_entries = tracing_map_sort_entries(map, hist_data->sort_keys,
5493                                              hist_data->n_sort_keys,
5494                                              &sort_entries);
5495         if (n_entries < 0)
5496                 return n_entries;
5497
5498         for (i = 0; i < n_entries; i++)
5499                 hist_trigger_entry_print(m, hist_data,
5500                                          sort_entries[i]->key,
5501                                          sort_entries[i]->elt);
5502
5503         tracing_map_destroy_sort_entries(sort_entries, n_entries);
5504
5505         return n_entries;
5506 }
5507
5508 static void hist_trigger_show(struct seq_file *m,
5509                               struct event_trigger_data *data, int n)
5510 {
5511         struct hist_trigger_data *hist_data;
5512         int n_entries;
5513
5514         if (n > 0)
5515                 seq_puts(m, "\n\n");
5516
5517         seq_puts(m, "# event histogram\n#\n# trigger info: ");
5518         data->ops->print(m, data->ops, data);
5519         seq_puts(m, "#\n\n");
5520
5521         hist_data = data->private_data;
5522         n_entries = print_entries(m, hist_data);
5523         if (n_entries < 0)
5524                 n_entries = 0;
5525
5526         track_data_snapshot_print(m, hist_data);
5527
5528         seq_printf(m, "\nTotals:\n    Hits: %llu\n    Entries: %u\n    Dropped: %llu\n",
5529                    (u64)atomic64_read(&hist_data->map->hits),
5530                    n_entries, (u64)atomic64_read(&hist_data->map->drops));
5531 }
5532
5533 static int hist_show(struct seq_file *m, void *v)
5534 {
5535         struct event_trigger_data *data;
5536         struct trace_event_file *event_file;
5537         int n = 0, ret = 0;
5538
5539         mutex_lock(&event_mutex);
5540
5541         event_file = event_file_data(m->private);
5542         if (unlikely(!event_file)) {
5543                 ret = -ENODEV;
5544                 goto out_unlock;
5545         }
5546
5547         list_for_each_entry(data, &event_file->triggers, list) {
5548                 if (data->cmd_ops->trigger_type == ETT_EVENT_HIST)
5549                         hist_trigger_show(m, data, n++);
5550         }
5551
5552  out_unlock:
5553         mutex_unlock(&event_mutex);
5554
5555         return ret;
5556 }
5557
5558 static int event_hist_open(struct inode *inode, struct file *file)
5559 {
5560         int ret;
5561
5562         ret = security_locked_down(LOCKDOWN_TRACEFS);
5563         if (ret)
5564                 return ret;
5565
5566         return single_open(file, hist_show, file);
5567 }
5568
5569 const struct file_operations event_hist_fops = {
5570         .open = event_hist_open,
5571         .read = seq_read,
5572         .llseek = seq_lseek,
5573         .release = single_release,
5574 };
5575
5576 static void hist_field_print(struct seq_file *m, struct hist_field *hist_field)
5577 {
5578         const char *field_name = hist_field_name(hist_field, 0);
5579
5580         if (hist_field->var.name)
5581                 seq_printf(m, "%s=", hist_field->var.name);
5582
5583         if (hist_field->flags & HIST_FIELD_FL_CPU)
5584                 seq_puts(m, "cpu");
5585         else if (field_name) {
5586                 if (hist_field->flags & HIST_FIELD_FL_VAR_REF ||
5587                     hist_field->flags & HIST_FIELD_FL_ALIAS)
5588                         seq_putc(m, '$');
5589                 seq_printf(m, "%s", field_name);
5590         } else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP)
5591                 seq_puts(m, "common_timestamp");
5592
5593         if (hist_field->flags) {
5594                 if (!(hist_field->flags & HIST_FIELD_FL_VAR_REF) &&
5595                     !(hist_field->flags & HIST_FIELD_FL_EXPR)) {
5596                         const char *flags = get_hist_field_flags(hist_field);
5597
5598                         if (flags)
5599                                 seq_printf(m, ".%s", flags);
5600                 }
5601         }
5602 }
5603
5604 static int event_hist_trigger_print(struct seq_file *m,
5605                                     struct event_trigger_ops *ops,
5606                                     struct event_trigger_data *data)
5607 {
5608         struct hist_trigger_data *hist_data = data->private_data;
5609         struct hist_field *field;
5610         bool have_var = false;
5611         unsigned int i;
5612
5613         seq_puts(m, "hist:");
5614
5615         if (data->name)
5616                 seq_printf(m, "%s:", data->name);
5617
5618         seq_puts(m, "keys=");
5619
5620         for_each_hist_key_field(i, hist_data) {
5621                 field = hist_data->fields[i];
5622
5623                 if (i > hist_data->n_vals)
5624                         seq_puts(m, ",");
5625
5626                 if (field->flags & HIST_FIELD_FL_STACKTRACE)
5627                         seq_puts(m, "stacktrace");
5628                 else
5629                         hist_field_print(m, field);
5630         }
5631
5632         seq_puts(m, ":vals=");
5633
5634         for_each_hist_val_field(i, hist_data) {
5635                 field = hist_data->fields[i];
5636                 if (field->flags & HIST_FIELD_FL_VAR) {
5637                         have_var = true;
5638                         continue;
5639                 }
5640
5641                 if (i == HITCOUNT_IDX)
5642                         seq_puts(m, "hitcount");
5643                 else {
5644                         seq_puts(m, ",");
5645                         hist_field_print(m, field);
5646                 }
5647         }
5648
5649         if (have_var) {
5650                 unsigned int n = 0;
5651
5652                 seq_puts(m, ":");
5653
5654                 for_each_hist_val_field(i, hist_data) {
5655                         field = hist_data->fields[i];
5656
5657                         if (field->flags & HIST_FIELD_FL_VAR) {
5658                                 if (n++)
5659                                         seq_puts(m, ",");
5660                                 hist_field_print(m, field);
5661                         }
5662                 }
5663         }
5664
5665         seq_puts(m, ":sort=");
5666
5667         for (i = 0; i < hist_data->n_sort_keys; i++) {
5668                 struct tracing_map_sort_key *sort_key;
5669                 unsigned int idx, first_key_idx;
5670
5671                 /* skip VAR vals */
5672                 first_key_idx = hist_data->n_vals - hist_data->n_vars;
5673
5674                 sort_key = &hist_data->sort_keys[i];
5675                 idx = sort_key->field_idx;
5676
5677                 if (WARN_ON(idx >= HIST_FIELDS_MAX))
5678                         return -EINVAL;
5679
5680                 if (i > 0)
5681                         seq_puts(m, ",");
5682
5683                 if (idx == HITCOUNT_IDX)
5684                         seq_puts(m, "hitcount");
5685                 else {
5686                         if (idx >= first_key_idx)
5687                                 idx += hist_data->n_vars;
5688                         hist_field_print(m, hist_data->fields[idx]);
5689                 }
5690
5691                 if (sort_key->descending)
5692                         seq_puts(m, ".descending");
5693         }
5694         seq_printf(m, ":size=%u", (1 << hist_data->map->map_bits));
5695         if (hist_data->enable_timestamps)
5696                 seq_printf(m, ":clock=%s", hist_data->attrs->clock);
5697
5698         print_actions_spec(m, hist_data);
5699
5700         if (data->filter_str)
5701                 seq_printf(m, " if %s", data->filter_str);
5702
5703         if (data->paused)
5704                 seq_puts(m, " [paused]");
5705         else
5706                 seq_puts(m, " [active]");
5707
5708         seq_putc(m, '\n');
5709
5710         return 0;
5711 }
5712
5713 static int event_hist_trigger_init(struct event_trigger_ops *ops,
5714                                    struct event_trigger_data *data)
5715 {
5716         struct hist_trigger_data *hist_data = data->private_data;
5717
5718         if (!data->ref && hist_data->attrs->name)
5719                 save_named_trigger(hist_data->attrs->name, data);
5720
5721         data->ref++;
5722
5723         return 0;
5724 }
5725
5726 static void unregister_field_var_hists(struct hist_trigger_data *hist_data)
5727 {
5728         struct trace_event_file *file;
5729         unsigned int i;
5730         char *cmd;
5731         int ret;
5732
5733         for (i = 0; i < hist_data->n_field_var_hists; i++) {
5734                 file = hist_data->field_var_hists[i]->hist_data->event_file;
5735                 cmd = hist_data->field_var_hists[i]->cmd;
5736                 ret = event_hist_trigger_func(&trigger_hist_cmd, file,
5737                                               "!hist", "hist", cmd);
5738         }
5739 }
5740
5741 static void event_hist_trigger_free(struct event_trigger_ops *ops,
5742                                     struct event_trigger_data *data)
5743 {
5744         struct hist_trigger_data *hist_data = data->private_data;
5745
5746         if (WARN_ON_ONCE(data->ref <= 0))
5747                 return;
5748
5749         data->ref--;
5750         if (!data->ref) {
5751                 if (data->name)
5752                         del_named_trigger(data);
5753
5754                 trigger_data_free(data);
5755
5756                 remove_hist_vars(hist_data);
5757
5758                 unregister_field_var_hists(hist_data);
5759
5760                 destroy_hist_data(hist_data);
5761         }
5762 }
5763
5764 static struct event_trigger_ops event_hist_trigger_ops = {
5765         .func                   = event_hist_trigger,
5766         .print                  = event_hist_trigger_print,
5767         .init                   = event_hist_trigger_init,
5768         .free                   = event_hist_trigger_free,
5769 };
5770
5771 static int event_hist_trigger_named_init(struct event_trigger_ops *ops,
5772                                          struct event_trigger_data *data)
5773 {
5774         data->ref++;
5775
5776         save_named_trigger(data->named_data->name, data);
5777
5778         event_hist_trigger_init(ops, data->named_data);
5779
5780         return 0;
5781 }
5782
5783 static void event_hist_trigger_named_free(struct event_trigger_ops *ops,
5784                                           struct event_trigger_data *data)
5785 {
5786         if (WARN_ON_ONCE(data->ref <= 0))
5787                 return;
5788
5789         event_hist_trigger_free(ops, data->named_data);
5790
5791         data->ref--;
5792         if (!data->ref) {
5793                 del_named_trigger(data);
5794                 trigger_data_free(data);
5795         }
5796 }
5797
5798 static struct event_trigger_ops event_hist_trigger_named_ops = {
5799         .func                   = event_hist_trigger,
5800         .print                  = event_hist_trigger_print,
5801         .init                   = event_hist_trigger_named_init,
5802         .free                   = event_hist_trigger_named_free,
5803 };
5804
5805 static struct event_trigger_ops *event_hist_get_trigger_ops(char *cmd,
5806                                                             char *param)
5807 {
5808         return &event_hist_trigger_ops;
5809 }
5810
5811 static void hist_clear(struct event_trigger_data *data)
5812 {
5813         struct hist_trigger_data *hist_data = data->private_data;
5814
5815         if (data->name)
5816                 pause_named_trigger(data);
5817
5818         tracepoint_synchronize_unregister();
5819
5820         tracing_map_clear(hist_data->map);
5821
5822         if (data->name)
5823                 unpause_named_trigger(data);
5824 }
5825
5826 static bool compatible_field(struct ftrace_event_field *field,
5827                              struct ftrace_event_field *test_field)
5828 {
5829         if (field == test_field)
5830                 return true;
5831         if (field == NULL || test_field == NULL)
5832                 return false;
5833         if (strcmp(field->name, test_field->name) != 0)
5834                 return false;
5835         if (strcmp(field->type, test_field->type) != 0)
5836                 return false;
5837         if (field->size != test_field->size)
5838                 return false;
5839         if (field->is_signed != test_field->is_signed)
5840                 return false;
5841
5842         return true;
5843 }
5844
5845 static bool hist_trigger_match(struct event_trigger_data *data,
5846                                struct event_trigger_data *data_test,
5847                                struct event_trigger_data *named_data,
5848                                bool ignore_filter)
5849 {
5850         struct tracing_map_sort_key *sort_key, *sort_key_test;
5851         struct hist_trigger_data *hist_data, *hist_data_test;
5852         struct hist_field *key_field, *key_field_test;
5853         unsigned int i;
5854
5855         if (named_data && (named_data != data_test) &&
5856             (named_data != data_test->named_data))
5857                 return false;
5858
5859         if (!named_data && is_named_trigger(data_test))
5860                 return false;
5861
5862         hist_data = data->private_data;
5863         hist_data_test = data_test->private_data;
5864
5865         if (hist_data->n_vals != hist_data_test->n_vals ||
5866             hist_data->n_fields != hist_data_test->n_fields ||
5867             hist_data->n_sort_keys != hist_data_test->n_sort_keys)
5868                 return false;
5869
5870         if (!ignore_filter) {
5871                 if ((data->filter_str && !data_test->filter_str) ||
5872                    (!data->filter_str && data_test->filter_str))
5873                         return false;
5874         }
5875
5876         for_each_hist_field(i, hist_data) {
5877                 key_field = hist_data->fields[i];
5878                 key_field_test = hist_data_test->fields[i];
5879
5880                 if (key_field->flags != key_field_test->flags)
5881                         return false;
5882                 if (!compatible_field(key_field->field, key_field_test->field))
5883                         return false;
5884                 if (key_field->offset != key_field_test->offset)
5885                         return false;
5886                 if (key_field->size != key_field_test->size)
5887                         return false;
5888                 if (key_field->is_signed != key_field_test->is_signed)
5889                         return false;
5890                 if (!!key_field->var.name != !!key_field_test->var.name)
5891                         return false;
5892                 if (key_field->var.name &&
5893                     strcmp(key_field->var.name, key_field_test->var.name) != 0)
5894                         return false;
5895         }
5896
5897         for (i = 0; i < hist_data->n_sort_keys; i++) {
5898                 sort_key = &hist_data->sort_keys[i];
5899                 sort_key_test = &hist_data_test->sort_keys[i];
5900
5901                 if (sort_key->field_idx != sort_key_test->field_idx ||
5902                     sort_key->descending != sort_key_test->descending)
5903                         return false;
5904         }
5905
5906         if (!ignore_filter && data->filter_str &&
5907             (strcmp(data->filter_str, data_test->filter_str) != 0))
5908                 return false;
5909
5910         if (!actions_match(hist_data, hist_data_test))
5911                 return false;
5912
5913         return true;
5914 }
5915
5916 static int hist_register_trigger(char *glob, struct event_trigger_ops *ops,
5917                                  struct event_trigger_data *data,
5918                                  struct trace_event_file *file)
5919 {
5920         struct hist_trigger_data *hist_data = data->private_data;
5921         struct event_trigger_data *test, *named_data = NULL;
5922         struct trace_array *tr = file->tr;
5923         int ret = 0;
5924
5925         if (hist_data->attrs->name) {
5926                 named_data = find_named_trigger(hist_data->attrs->name);
5927                 if (named_data) {
5928                         if (!hist_trigger_match(data, named_data, named_data,
5929                                                 true)) {
5930                                 hist_err(tr, HIST_ERR_NAMED_MISMATCH, errpos(hist_data->attrs->name));
5931                                 ret = -EINVAL;
5932                                 goto out;
5933                         }
5934                 }
5935         }
5936
5937         if (hist_data->attrs->name && !named_data)
5938                 goto new;
5939
5940         lockdep_assert_held(&event_mutex);
5941
5942         list_for_each_entry(test, &file->triggers, list) {
5943                 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
5944                         if (!hist_trigger_match(data, test, named_data, false))
5945                                 continue;
5946                         if (hist_data->attrs->pause)
5947                                 test->paused = true;
5948                         else if (hist_data->attrs->cont)
5949                                 test->paused = false;
5950                         else if (hist_data->attrs->clear)
5951                                 hist_clear(test);
5952                         else {
5953                                 hist_err(tr, HIST_ERR_TRIGGER_EEXIST, 0);
5954                                 ret = -EEXIST;
5955                         }
5956                         goto out;
5957                 }
5958         }
5959  new:
5960         if (hist_data->attrs->cont || hist_data->attrs->clear) {
5961                 hist_err(tr, HIST_ERR_TRIGGER_ENOENT_CLEAR, 0);
5962                 ret = -ENOENT;
5963                 goto out;
5964         }
5965
5966         if (hist_data->attrs->pause)
5967                 data->paused = true;
5968
5969         if (named_data) {
5970                 data->private_data = named_data->private_data;
5971                 set_named_trigger_data(data, named_data);
5972                 data->ops = &event_hist_trigger_named_ops;
5973         }
5974
5975         if (data->ops->init) {
5976                 ret = data->ops->init(data->ops, data);
5977                 if (ret < 0)
5978                         goto out;
5979         }
5980
5981         if (hist_data->enable_timestamps) {
5982                 char *clock = hist_data->attrs->clock;
5983
5984                 ret = tracing_set_clock(file->tr, hist_data->attrs->clock);
5985                 if (ret) {
5986                         hist_err(tr, HIST_ERR_SET_CLOCK_FAIL, errpos(clock));
5987                         goto out;
5988                 }
5989
5990                 tracing_set_time_stamp_abs(file->tr, true);
5991         }
5992
5993         if (named_data)
5994                 destroy_hist_data(hist_data);
5995
5996         ret++;
5997  out:
5998         return ret;
5999 }
6000
6001 static int hist_trigger_enable(struct event_trigger_data *data,
6002                                struct trace_event_file *file)
6003 {
6004         int ret = 0;
6005
6006         list_add_tail_rcu(&data->list, &file->triggers);
6007
6008         update_cond_flag(file);
6009
6010         if (trace_event_trigger_enable_disable(file, 1) < 0) {
6011                 list_del_rcu(&data->list);
6012                 update_cond_flag(file);
6013                 ret--;
6014         }
6015
6016         return ret;
6017 }
6018
6019 static bool have_hist_trigger_match(struct event_trigger_data *data,
6020                                     struct trace_event_file *file)
6021 {
6022         struct hist_trigger_data *hist_data = data->private_data;
6023         struct event_trigger_data *test, *named_data = NULL;
6024         bool match = false;
6025
6026         lockdep_assert_held(&event_mutex);
6027
6028         if (hist_data->attrs->name)
6029                 named_data = find_named_trigger(hist_data->attrs->name);
6030
6031         list_for_each_entry(test, &file->triggers, list) {
6032                 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6033                         if (hist_trigger_match(data, test, named_data, false)) {
6034                                 match = true;
6035                                 break;
6036                         }
6037                 }
6038         }
6039
6040         return match;
6041 }
6042
6043 static bool hist_trigger_check_refs(struct event_trigger_data *data,
6044                                     struct trace_event_file *file)
6045 {
6046         struct hist_trigger_data *hist_data = data->private_data;
6047         struct event_trigger_data *test, *named_data = NULL;
6048
6049         lockdep_assert_held(&event_mutex);
6050
6051         if (hist_data->attrs->name)
6052                 named_data = find_named_trigger(hist_data->attrs->name);
6053
6054         list_for_each_entry(test, &file->triggers, list) {
6055                 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6056                         if (!hist_trigger_match(data, test, named_data, false))
6057                                 continue;
6058                         hist_data = test->private_data;
6059                         if (check_var_refs(hist_data))
6060                                 return true;
6061                         break;
6062                 }
6063         }
6064
6065         return false;
6066 }
6067
6068 static void hist_unregister_trigger(char *glob, struct event_trigger_ops *ops,
6069                                     struct event_trigger_data *data,
6070                                     struct trace_event_file *file)
6071 {
6072         struct hist_trigger_data *hist_data = data->private_data;
6073         struct event_trigger_data *test, *named_data = NULL;
6074         bool unregistered = false;
6075
6076         lockdep_assert_held(&event_mutex);
6077
6078         if (hist_data->attrs->name)
6079                 named_data = find_named_trigger(hist_data->attrs->name);
6080
6081         list_for_each_entry(test, &file->triggers, list) {
6082                 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6083                         if (!hist_trigger_match(data, test, named_data, false))
6084                                 continue;
6085                         unregistered = true;
6086                         list_del_rcu(&test->list);
6087                         trace_event_trigger_enable_disable(file, 0);
6088                         update_cond_flag(file);
6089                         break;
6090                 }
6091         }
6092
6093         if (unregistered && test->ops->free)
6094                 test->ops->free(test->ops, test);
6095
6096         if (hist_data->enable_timestamps) {
6097                 if (!hist_data->remove || unregistered)
6098                         tracing_set_time_stamp_abs(file->tr, false);
6099         }
6100 }
6101
6102 static bool hist_file_check_refs(struct trace_event_file *file)
6103 {
6104         struct hist_trigger_data *hist_data;
6105         struct event_trigger_data *test;
6106
6107         lockdep_assert_held(&event_mutex);
6108
6109         list_for_each_entry(test, &file->triggers, list) {
6110                 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6111                         hist_data = test->private_data;
6112                         if (check_var_refs(hist_data))
6113                                 return true;
6114                 }
6115         }
6116
6117         return false;
6118 }
6119
6120 static void hist_unreg_all(struct trace_event_file *file)
6121 {
6122         struct event_trigger_data *test, *n;
6123         struct hist_trigger_data *hist_data;
6124         struct synth_event *se;
6125         const char *se_name;
6126
6127         lockdep_assert_held(&event_mutex);
6128
6129         if (hist_file_check_refs(file))
6130                 return;
6131
6132         list_for_each_entry_safe(test, n, &file->triggers, list) {
6133                 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6134                         hist_data = test->private_data;
6135                         list_del_rcu(&test->list);
6136                         trace_event_trigger_enable_disable(file, 0);
6137
6138                         se_name = trace_event_name(file->event_call);
6139                         se = find_synth_event(se_name);
6140                         if (se)
6141                                 se->ref--;
6142
6143                         update_cond_flag(file);
6144                         if (hist_data->enable_timestamps)
6145                                 tracing_set_time_stamp_abs(file->tr, false);
6146                         if (test->ops->free)
6147                                 test->ops->free(test->ops, test);
6148                 }
6149         }
6150 }
6151
6152 static int event_hist_trigger_func(struct event_command *cmd_ops,
6153                                    struct trace_event_file *file,
6154                                    char *glob, char *cmd, char *param)
6155 {
6156         unsigned int hist_trigger_bits = TRACING_MAP_BITS_DEFAULT;
6157         struct event_trigger_data *trigger_data;
6158         struct hist_trigger_attrs *attrs;
6159         struct event_trigger_ops *trigger_ops;
6160         struct hist_trigger_data *hist_data;
6161         struct synth_event *se;
6162         const char *se_name;
6163         bool remove = false;
6164         char *trigger, *p;
6165         int ret = 0;
6166
6167         lockdep_assert_held(&event_mutex);
6168
6169         if (glob && strlen(glob)) {
6170                 hist_err_clear();
6171                 last_cmd_set(file, param);
6172         }
6173
6174         if (!param)
6175                 return -EINVAL;
6176
6177         if (glob[0] == '!')
6178                 remove = true;
6179
6180         /*
6181          * separate the trigger from the filter (k:v [if filter])
6182          * allowing for whitespace in the trigger
6183          */
6184         p = trigger = param;
6185         do {
6186                 p = strstr(p, "if");
6187                 if (!p)
6188                         break;
6189                 if (p == param)
6190                         return -EINVAL;
6191                 if (*(p - 1) != ' ' && *(p - 1) != '\t') {
6192                         p++;
6193                         continue;
6194                 }
6195                 if (p >= param + strlen(param) - (sizeof("if") - 1) - 1)
6196                         return -EINVAL;
6197                 if (*(p + sizeof("if") - 1) != ' ' && *(p + sizeof("if") - 1) != '\t') {
6198                         p++;
6199                         continue;
6200                 }
6201                 break;
6202         } while (p);
6203
6204         if (!p)
6205                 param = NULL;
6206         else {
6207                 *(p - 1) = '\0';
6208                 param = strstrip(p);
6209                 trigger = strstrip(trigger);
6210         }
6211
6212         attrs = parse_hist_trigger_attrs(file->tr, trigger);
6213         if (IS_ERR(attrs))
6214                 return PTR_ERR(attrs);
6215
6216         if (attrs->map_bits)
6217                 hist_trigger_bits = attrs->map_bits;
6218
6219         hist_data = create_hist_data(hist_trigger_bits, attrs, file, remove);
6220         if (IS_ERR(hist_data)) {
6221                 destroy_hist_trigger_attrs(attrs);
6222                 return PTR_ERR(hist_data);
6223         }
6224
6225         trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
6226
6227         trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
6228         if (!trigger_data) {
6229                 ret = -ENOMEM;
6230                 goto out_free;
6231         }
6232
6233         trigger_data->count = -1;
6234         trigger_data->ops = trigger_ops;
6235         trigger_data->cmd_ops = cmd_ops;
6236
6237         INIT_LIST_HEAD(&trigger_data->list);
6238         RCU_INIT_POINTER(trigger_data->filter, NULL);
6239
6240         trigger_data->private_data = hist_data;
6241
6242         /* if param is non-empty, it's supposed to be a filter */
6243         if (param && cmd_ops->set_filter) {
6244                 ret = cmd_ops->set_filter(param, trigger_data, file);
6245                 if (ret < 0)
6246                         goto out_free;
6247         }
6248
6249         if (remove) {
6250                 if (!have_hist_trigger_match(trigger_data, file))
6251                         goto out_free;
6252
6253                 if (hist_trigger_check_refs(trigger_data, file)) {
6254                         ret = -EBUSY;
6255                         goto out_free;
6256                 }
6257
6258                 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
6259                 se_name = trace_event_name(file->event_call);
6260                 se = find_synth_event(se_name);
6261                 if (se)
6262                         se->ref--;
6263                 ret = 0;
6264                 goto out_free;
6265         }
6266
6267         ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
6268         /*
6269          * The above returns on success the # of triggers registered,
6270          * but if it didn't register any it returns zero.  Consider no
6271          * triggers registered a failure too.
6272          */
6273         if (!ret) {
6274                 if (!(attrs->pause || attrs->cont || attrs->clear))
6275                         ret = -ENOENT;
6276                 goto out_free;
6277         } else if (ret < 0)
6278                 goto out_free;
6279
6280         if (get_named_trigger_data(trigger_data))
6281                 goto enable;
6282
6283         if (has_hist_vars(hist_data))
6284                 save_hist_vars(hist_data);
6285
6286         ret = create_actions(hist_data);
6287         if (ret)
6288                 goto out_unreg;
6289
6290         ret = tracing_map_init(hist_data->map);
6291         if (ret)
6292                 goto out_unreg;
6293 enable:
6294         ret = hist_trigger_enable(trigger_data, file);
6295         if (ret)
6296                 goto out_unreg;
6297
6298         se_name = trace_event_name(file->event_call);
6299         se = find_synth_event(se_name);
6300         if (se)
6301                 se->ref++;
6302         /* Just return zero, not the number of registered triggers */
6303         ret = 0;
6304  out:
6305         if (ret == 0)
6306                 hist_err_clear();
6307
6308         return ret;
6309  out_unreg:
6310         cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
6311  out_free:
6312         if (cmd_ops->set_filter)
6313                 cmd_ops->set_filter(NULL, trigger_data, NULL);
6314
6315         remove_hist_vars(hist_data);
6316
6317         kfree(trigger_data);
6318
6319         destroy_hist_data(hist_data);
6320         goto out;
6321 }
6322
6323 static struct event_command trigger_hist_cmd = {
6324         .name                   = "hist",
6325         .trigger_type           = ETT_EVENT_HIST,
6326         .flags                  = EVENT_CMD_FL_NEEDS_REC,
6327         .func                   = event_hist_trigger_func,
6328         .reg                    = hist_register_trigger,
6329         .unreg                  = hist_unregister_trigger,
6330         .unreg_all              = hist_unreg_all,
6331         .get_trigger_ops        = event_hist_get_trigger_ops,
6332         .set_filter             = set_trigger_filter,
6333 };
6334
6335 __init int register_trigger_hist_cmd(void)
6336 {
6337         int ret;
6338
6339         ret = register_event_command(&trigger_hist_cmd);
6340         WARN_ON(ret < 0);
6341
6342         return ret;
6343 }
6344
6345 static void
6346 hist_enable_trigger(struct event_trigger_data *data, void *rec,
6347                     struct ring_buffer_event *event)
6348 {
6349         struct enable_trigger_data *enable_data = data->private_data;
6350         struct event_trigger_data *test;
6351
6352         list_for_each_entry_rcu(test, &enable_data->file->triggers, list,
6353                                 lockdep_is_held(&event_mutex)) {
6354                 if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) {
6355                         if (enable_data->enable)
6356                                 test->paused = false;
6357                         else
6358                                 test->paused = true;
6359                 }
6360         }
6361 }
6362
6363 static void
6364 hist_enable_count_trigger(struct event_trigger_data *data, void *rec,
6365                           struct ring_buffer_event *event)
6366 {
6367         if (!data->count)
6368                 return;
6369
6370         if (data->count != -1)
6371                 (data->count)--;
6372
6373         hist_enable_trigger(data, rec, event);
6374 }
6375
6376 static struct event_trigger_ops hist_enable_trigger_ops = {
6377         .func                   = hist_enable_trigger,
6378         .print                  = event_enable_trigger_print,
6379         .init                   = event_trigger_init,
6380         .free                   = event_enable_trigger_free,
6381 };
6382
6383 static struct event_trigger_ops hist_enable_count_trigger_ops = {
6384         .func                   = hist_enable_count_trigger,
6385         .print                  = event_enable_trigger_print,
6386         .init                   = event_trigger_init,
6387         .free                   = event_enable_trigger_free,
6388 };
6389
6390 static struct event_trigger_ops hist_disable_trigger_ops = {
6391         .func                   = hist_enable_trigger,
6392         .print                  = event_enable_trigger_print,
6393         .init                   = event_trigger_init,
6394         .free                   = event_enable_trigger_free,
6395 };
6396
6397 static struct event_trigger_ops hist_disable_count_trigger_ops = {
6398         .func                   = hist_enable_count_trigger,
6399         .print                  = event_enable_trigger_print,
6400         .init                   = event_trigger_init,
6401         .free                   = event_enable_trigger_free,
6402 };
6403
6404 static struct event_trigger_ops *
6405 hist_enable_get_trigger_ops(char *cmd, char *param)
6406 {
6407         struct event_trigger_ops *ops;
6408         bool enable;
6409
6410         enable = (strcmp(cmd, ENABLE_HIST_STR) == 0);
6411
6412         if (enable)
6413                 ops = param ? &hist_enable_count_trigger_ops :
6414                         &hist_enable_trigger_ops;
6415         else
6416                 ops = param ? &hist_disable_count_trigger_ops :
6417                         &hist_disable_trigger_ops;
6418
6419         return ops;
6420 }
6421
6422 static void hist_enable_unreg_all(struct trace_event_file *file)
6423 {
6424         struct event_trigger_data *test, *n;
6425
6426         list_for_each_entry_safe(test, n, &file->triggers, list) {
6427                 if (test->cmd_ops->trigger_type == ETT_HIST_ENABLE) {
6428                         list_del_rcu(&test->list);
6429                         update_cond_flag(file);
6430                         trace_event_trigger_enable_disable(file, 0);
6431                         if (test->ops->free)
6432                                 test->ops->free(test->ops, test);
6433                 }
6434         }
6435 }
6436
6437 static struct event_command trigger_hist_enable_cmd = {
6438         .name                   = ENABLE_HIST_STR,
6439         .trigger_type           = ETT_HIST_ENABLE,
6440         .func                   = event_enable_trigger_func,
6441         .reg                    = event_enable_register_trigger,
6442         .unreg                  = event_enable_unregister_trigger,
6443         .unreg_all              = hist_enable_unreg_all,
6444         .get_trigger_ops        = hist_enable_get_trigger_ops,
6445         .set_filter             = set_trigger_filter,
6446 };
6447
6448 static struct event_command trigger_hist_disable_cmd = {
6449         .name                   = DISABLE_HIST_STR,
6450         .trigger_type           = ETT_HIST_ENABLE,
6451         .func                   = event_enable_trigger_func,
6452         .reg                    = event_enable_register_trigger,
6453         .unreg                  = event_enable_unregister_trigger,
6454         .unreg_all              = hist_enable_unreg_all,
6455         .get_trigger_ops        = hist_enable_get_trigger_ops,
6456         .set_filter             = set_trigger_filter,
6457 };
6458
6459 static __init void unregister_trigger_hist_enable_disable_cmds(void)
6460 {
6461         unregister_event_command(&trigger_hist_enable_cmd);
6462         unregister_event_command(&trigger_hist_disable_cmd);
6463 }
6464
6465 __init int register_trigger_hist_enable_disable_cmds(void)
6466 {
6467         int ret;
6468
6469         ret = register_event_command(&trigger_hist_enable_cmd);
6470         if (WARN_ON(ret < 0))
6471                 return ret;
6472         ret = register_event_command(&trigger_hist_disable_cmd);
6473         if (WARN_ON(ret < 0))
6474                 unregister_trigger_hist_enable_disable_cmds();
6475
6476         return ret;
6477 }
6478
6479 static __init int trace_events_hist_init(void)
6480 {
6481         struct dentry *entry = NULL;
6482         struct dentry *d_tracer;
6483         int err = 0;
6484
6485         err = dyn_event_register(&synth_event_ops);
6486         if (err) {
6487                 pr_warn("Could not register synth_event_ops\n");
6488                 return err;
6489         }
6490
6491         d_tracer = tracing_init_dentry();
6492         if (IS_ERR(d_tracer)) {
6493                 err = PTR_ERR(d_tracer);
6494                 goto err;
6495         }
6496
6497         entry = tracefs_create_file("synthetic_events", 0644, d_tracer,
6498                                     NULL, &synth_events_fops);
6499         if (!entry) {
6500                 err = -ENODEV;
6501                 goto err;
6502         }
6503
6504         return err;
6505  err:
6506         pr_warn("Could not create tracefs 'synthetic_events' entry\n");
6507
6508         return err;
6509 }
6510
6511 fs_initcall(trace_events_hist_init);